Files
OmniRoute/open-sse/services/tokenExtractionConfig.ts
backryun bd472200d5 [v3.8.50] Fix Z.ai web browser transport and model capabilities (#8451)
* fix: complete Z.ai web browser transport

* refactor: address Z.ai review feedback

* test(zai-web): reconcile the #8014 endpoint guard with the chats/new + signed flow

Rebasing onto release/v3.8.49 pulled in #8503, which repointed CHAT_URL to
/api/v2/chat/completions and added an endpoint probe. This branch already
targets v2, so the executor conflict resolved to this branch's superset
(NEW_CHAT_URL + signature constants alongside the same v2 CHAT_URL). The two
tests needed adapting, because #8503's assertions assume the pre-rework flow:

- executor-zai-web.test.ts: the completion URL now carries the request
  signature as a query string, so an exact-equality check on the endpoint can
  never match. Assert the v2 prefix instead.
- zai-web-chat-endpoint-8014-probe.test.ts: the probe drove the executor with a
  bare cookie credential and no captcha proof, which now routes through the
  browser transport — fetch was never called and the probe captured nothing.
  Supplied a direct-path credential, and matched on pathname across all
  requests (the executor also probes the homepage for the frontend version and
  calls /api/v1/chats/new first).

The guard's intent is unchanged and slightly strengthened: it now asserts no
request reaches the stale unversioned path and that exactly one completions
request is issued, against v2.

54/54 across the zai suites; typecheck:core and eslint clean.

* fix(zai-web): surface upstream error frames instead of finishing empty

Reported on this PR: HTTP 200, `out=0`, stream "complete", no content and no
diagnosis.

Cause. HTTP-level failures are already handled — fetchUpstream turns any !ok
response into a makeErrorResult with the sanitized body. The gap is a 200 whose
SSE body carries an error payload: parseZaiFrame returns null for it,
drainSseDeltas drops it, and buildZaiStreamingBody then closes with an empty
assistant message + stop + [DONE]. The caller reads that as a successful empty
completion, so a rejected signature, an expired captcha and a stale token all
look identical — which is why this had to be diagnosed by reading code rather
than logs. Hard Rule #6.

Fix. parseZaiFrame now classifies an affirmatively error-shaped frame
(`error` at the top level or under `data`, string or {detail|message|msg}) as a
terminal delta, checked before the delta paths so it cannot fall through to the
"no usable delta" null. The stream emits it as `[Z.ai error] <message>`,
matching the mid-stream convention the other web executors already use
(zed-hosted's createErrorChunk) — the 200 is on the wire, so the status cannot
change, but the caller must not be left reading a blank success. Content
streamed before the failure is preserved. Message goes through
sanitizeErrorMessage (Rule #12).

Deliberately NOT changed: a contentless frame still parses to null. That is
live-validated behaviour, not an oversight — z.ai emits phase frames with no
delta_content, and executor-zai-web.test.ts pins it ("returns null for frames
with no usable delta"). Treating "nothing parseable arrived" as a failure would
invent policy on top of an observed protocol and risk false errors on the happy
path, so this only adds recognition of explicit error frames.

Tests (TDD, RED then GREEN): zai-web-silent-empty-repro.test.ts — 7 cases.
Error frame classified and terminal; surfaced through the stream with the
upstream's own text; surfaced after partial content without losing it; plus a
REGRESSION GUARD that contentless/phase-only frames are still skipped, and two
controls that the happy path and reasoning-only output are untouched. The guard
and controls passed before the fix; the four error cases did not.

94/94 across the zai + stream suites; typecheck:core, eslint and check:file-size
clean.

* refactor(sse): extract the zai-web transports so the complexity ratchet holds

The v3.8.49 merge-train rebaseline (#8686) set the ceiling to the tip's own
measurement, leaving zero headroom, so this branch's +5 cyclomatic / +3 cognitive
own-growth had nowhere to sit once rebased onto it.

Eight violations, all in code this branch introduces, resolved by extraction —
no behaviour change:

- `execute` (152 lines, complexity 25, cognitive 20) now delegates to
  `resolveZaiRequest()` for the four client-error rejections and to a
  `fetchViaSignedApi()` method for the CAPTCHA/signature path, so it reads as
  "validate, pick a transport, shape the response".
- `fetchThroughBrowser` (126 lines, cognitive 16) hands its image decoding to
  `resolveZaiBrowserAttachments()`, its Playwright options to
  `buildZaiBrowserChatOptions()`, and its call-log payload to
  `buildZaiBrowserAuditBody()`.
- `configureZaiBrowserEffort` (cognitive 35 — the worst of the set) repeated a
  wrap-and-relabel try/catch four times inside an if/else. `runStage`, which
  already existed one function below, is now module-scoped and reused, and the
  toggle collapses to `checked !== config.enabled` (same four cases).
- `validateWebCookieProvider` (complexity 19) moves its can-we-probe-this
  cascade into `resolveWebCookieProbe()`, which returns either a rejection or
  the URL + headers to use.
- `acquireBrowserContext`'s creation closure (complexity 17) hands cookie and
  localStorage seeding to `seedContextSession()`.

That last extraction also clears a violation that predates this branch —
`acquireBrowserContext` was already over the 80-line ceiling — so cyclomatic
lands at 2187 against a baseline of 2188.

Verified: check:complexity-ratchets green both metrics; typecheck:core clean;
ESLint clean on all four files; 85 tests across the zai-web, web-cookie
validation, browser-pool and model-test-runner suites pass.

* fix(zai-web): surface upstream errors on the non-streaming path

collectZaiNonStreaming ignored delta.error — a 200 whose SSE body carries
an error frame (rejected signature, expired captcha, stale token) came
back as a successful empty completion. Now it throws on an error frame,
matching the streaming path's [Z.ai error] convention; the caller's
existing try/catch returns makeErrorResult(502) instead of an empty 200.

Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>

---------

Co-authored-by: backryun <busan011@ormbiz.co.kr>
Co-authored-by: diegosouzapw <8016841+diegosouzapw@users.noreply.github.com>
2026-08-12 08:41:03 -03:00

411 lines
16 KiB
TypeScript

/**
* TokenExtractionConfig — Login & cookie extraction configs for web-cookie providers
*
* Each config describes how to:
* 1. Open a browser window/navigate to the provider's login page
* 2. Detect successful login (URL change + token presence)
* 3. Extract session cookies / tokens from the browser context
*
* Used by InAppLoginService (Electron BrowserWindow path) and
* the Playwright-based login flow (dashboard API).
*/
// ─── Types ──────────────────────────────────────────────────────────────────
/** Describes where to extract credential data from after login */
export type TokenSource =
| { type: "cookie"; name: string; domain?: string }
| { type: "localStorage"; key: string }
| { type: "sessionStorage"; key: string }
| { type: "header"; name: string };
export interface PollingConfig {
/** Milliseconds between extraction polls (default 1000) */
pollInterval: number;
/** Total timeout in ms (default 300000 = 5 min) */
timeout: number;
/** Minimum time in ms before first extraction attempt (default 5000) */
minLoginTime: number;
}
export interface TokenExtractionConfig {
/** Matches the executor's provider ID (e.g. "claude-web", "gemini-web") */
providerId: string;
/** Human-readable name shown in dashboard UI */
displayName: string;
/** The URL to navigate to for login */
loginUrl: string;
/** The provider's home page URL (for cookie domain binding) */
homeUrl: string;
/** Optional regex. If current URL matches → login is likely complete */
successUrlPattern?: RegExp;
/** Sources to extract credentials from after login */
tokenSources: TokenSource[];
/** Polling behaviour */
pollingConfig: PollingConfig;
/** Short instructions shown to the user in the login modal */
instructions: string;
/** Optional: cookie domain override for cookie injection */
cookieDomain?: string;
}
// ─── Defaults ───────────────────────────────────────────────────────────────
const DEFAULT_POLLING: PollingConfig = {
pollInterval: 1000,
timeout: 300_000,
minLoginTime: 5000,
};
const QUICK_POLLING: PollingConfig = {
pollInterval: 800,
timeout: 120_000,
minLoginTime: 3000,
};
// ─── Helper ──────────────────────────────────────────────────────────────────
function config(
providerId: string,
displayName: string,
loginUrl: string,
homeUrl: string,
tokenSources: TokenSource[],
instructions: string,
opts?: {
successUrlPattern?: RegExp;
pollingConfig?: Partial<PollingConfig>;
cookieDomain?: string;
}
): TokenExtractionConfig {
return {
providerId,
displayName,
loginUrl,
homeUrl,
tokenSources,
instructions,
pollingConfig: { ...DEFAULT_POLLING, ...opts?.pollingConfig },
successUrlPattern: opts?.successUrlPattern,
cookieDomain: opts?.cookieDomain,
};
}
// ─── Configuration Map ──────────────────────────────────────────────────────
const RAW_CONFIGS: TokenExtractionConfig[] = [
// ── Claude Web ────────────────────────────────────────────
config(
"claude-web",
"Claude Web",
"https://claude.ai/login",
"https://claude.ai",
[{ type: "cookie", name: "sessionKey", domain: ".claude.ai" }],
"Log in to your Claude account at claude.ai. After login, the session cookie will be extracted automatically."
),
// ── ChatGPT Web ───────────────────────────────────────────
config(
"chatgpt-web",
"ChatGPT Web",
"https://chatgpt.com/auth/login",
"https://chatgpt.com",
[{ type: "cookie", name: "__Secure-next-auth.session-token", domain: ".chatgpt.com" }],
"Log in to ChatGPT. The __Secure-next-auth.session-token cookie will be extracted after login."
),
// ── Gemini Web ────────────────────────────────────────────
config(
"gemini-web",
"Gemini Web",
"https://gemini.google.com/app",
"https://gemini.google.com",
[
{ type: "cookie", name: "__Secure-1PSID", domain: ".google.com" },
{ type: "cookie", name: "__Secure-1PSIDTS", domain: ".google.com" },
],
"Log in to your Google account at gemini.google.com. Both __Secure-1PSID and __Secure-1PSIDTS cookies will be extracted.",
{ cookieDomain: ".google.com" }
),
// ── Grok Web ──────────────────────────────────────────────
config(
"grok-web",
"Grok Web",
"https://grok.com/login",
"https://grok.com",
[{ type: "cookie", name: "sso", domain: ".grok.com" }],
"Log in to your xAI account at grok.com. The sso session cookie will be extracted."
),
// ── Perplexity Web ────────────────────────────────────────
config(
"perplexity-web",
"Perplexity Web",
"https://www.perplexity.ai/login",
"https://www.perplexity.ai",
[{ type: "cookie", name: "__Secure-next-auth.session-token", domain: ".perplexity.ai" }],
"Log in to Perplexity. The __Secure-next-auth.session-token cookie will be extracted.",
{ cookieDomain: ".perplexity.ai" }
),
// ── DeepSeek Web ──────────────────────────────────────────
config(
"deepseek-web",
"DeepSeek Web",
"https://chat.deepseek.com/sign_in",
"https://chat.deepseek.com",
[
{ type: "cookie", name: "user-token", domain: ".deepseek.com" },
{ type: "localStorage", key: "userToken" },
],
"Log in to DeepSeek at chat.deepseek.com. The user-token cookie will be extracted.",
{ cookieDomain: ".deepseek.com" }
),
// ── Qwen Web ──────────────────────────────────────────────
// The v2 API sits behind Alibaba's "baxia" WAF, which needs the full browser
// cookie jar (cna + ssxmod_itna/itna2 + token), not just the bearer token.
// Capture the WAF cookies alongside the localStorage token (#3288).
config(
"qwen-web",
"Qwen Web (Tongyi)",
"https://chat.qwen.ai/",
"https://chat.qwen.ai",
[
{ type: "localStorage", key: "token" },
{ type: "cookie", name: "token", domain: ".chat.qwen.ai" },
{ type: "cookie", name: "cna", domain: ".chat.qwen.ai" },
{ type: "cookie", name: "ssxmod_itna", domain: ".chat.qwen.ai" },
{ type: "cookie", name: "ssxmod_itna2", domain: ".chat.qwen.ai" },
{ type: "cookie", name: "XSRF_TOKEN", domain: ".chat.qwen.ai" },
],
"Log in to Qwen at chat.qwen.ai using your Alibaba account. The session token and the " +
"Alibaba WAF cookies (cna, ssxmod_itna) will be extracted — all are required by the v2 API.",
{ cookieDomain: ".chat.qwen.ai" }
),
// ── Kimi Web ──────────────────────────────────────────────
config(
"kimi-web",
"Kimi (Moonshot)",
"https://www.kimi.com/",
"https://www.kimi.com",
[
{ type: "localStorage", key: "access_token" },
{ type: "cookie", name: "kimi-auth", domain: ".kimi.com" },
],
"Log in to Kimi at www.kimi.com. The current access_token will be extracted from localStorage; kimi-auth remains a legacy fallback.",
{ cookieDomain: ".kimi.com" }
),
// ── Blackbox Web ──────────────────────────────────────────
config(
"blackbox-web",
"Blackbox AI",
"https://app.blackbox.ai/login",
"https://app.blackbox.ai",
[
{ type: "cookie", name: "connect.sid", domain: ".blackbox.ai" },
{ type: "localStorage", key: "token" },
],
"Log in to Blackbox AI at app.blackbox.ai using Google/GitHub. The session cookie will be extracted.",
{ cookieDomain: ".blackbox.ai" }
),
// ── Poe Web ───────────────────────────────────────────────
config(
"poe-web",
"Poe (Quora)",
"https://poe.com/login",
"https://poe.com",
[{ type: "cookie", name: "p-b", domain: ".poe.com" }],
"Log in to Poe at poe.com. The session cookie will be extracted.",
{ cookieDomain: ".poe.com" }
),
// ── Copilot Web ───────────────────────────────────────────
config(
"copilot-web",
"Microsoft Copilot",
"https://copilot.microsoft.com/",
"https://copilot.microsoft.com",
[{ type: "header", name: "Authorization" }],
"Log in with your Microsoft account at copilot.microsoft.com. The bearer access token will be extracted from an authenticated request."
),
// ── DuckDuckGo Web ────────────────────────────────────────
config(
"duckduckgo-web",
"DuckDuckGo AI Chat",
"https://duckduckgo.com/?q=DuckDuckGo+AI+Chat&ia=chat&duckai=1",
"https://duckduckgo.com",
[{ type: "cookie", name: "duckai", domain: ".duckduckgo.com" }],
"Open DuckDuckGo AI Chat. Some models may require a free account. The duckai cookie will be extracted.",
{
cookieDomain: ".duckduckgo.com",
pollingConfig: QUICK_POLLING,
}
),
// ── Dola Web ──────────────────────────────────────────────
config(
"doubao-web",
"Dola (ByteDance)",
"https://www.dola.com/",
"https://www.dola.com",
[
{ type: "cookie", name: "sessionid", domain: ".dola.com" },
{ type: "cookie", name: "ttwid", domain: ".dola.com" },
{ type: "cookie", name: "s_v_web_id", domain: ".dola.com" },
],
"Log in to Dola at www.dola.com with your ByteDance account. sessionid, ttwid, and s_v_web_id will be extracted.",
{ cookieDomain: ".dola.com" }
),
// ── T3 Chat Web ───────────────────────────────────────────
config(
"t3-chat-web",
"T3 Chat",
"https://t3.chat/login",
"https://t3.chat",
[{ type: "localStorage", key: "token" }],
"Log in to T3 Chat at t3.chat using Google/GitHub. The token from localStorage will be extracted.",
{ pollingConfig: QUICK_POLLING }
),
// ── Venice Web ────────────────────────────────────────────
config(
"venice-web",
"Venice AI",
"https://venice.ai/login",
"https://venice.ai",
[
{ type: "cookie", name: "venice_session", domain: ".venice.ai" },
{ type: "localStorage", key: "token" },
],
"Log in to Venice AI at venice.ai. The session cookie will be extracted.",
{ cookieDomain: ".venice.ai" }
),
// ── v0 Dev Web ────────────────────────────────────────────
config(
"v0-vercel-web",
"v0 by Vercel",
"https://v0.dev/login",
"https://v0.dev",
[{ type: "cookie", name: "__Secure-next-auth.session-token", domain: ".v0.dev" }],
"Log in to v0.dev with your Vercel/Google/GitHub account. The session cookie will be extracted.",
{ cookieDomain: ".v0.dev" }
),
// ── Muse / Spark Web ──────────────────────────────────────
config(
"muse-spark-web",
"Meta AI (Muse)",
"https://www.meta.ai/",
"https://www.meta.ai",
[{ type: "cookie", name: "session", domain: ".meta.ai" }],
"Log in to Meta AI at meta.ai with your Facebook/Instagram account. The session cookie will be extracted.",
{ cookieDomain: ".meta.ai" }
),
// ── Adapta Web ────────────────────────────────────────────
config(
"adapta-web",
"Adapta AI",
"https://agent.adapta.one/login",
"https://agent.adapta.one",
[{ type: "cookie", name: "__session", domain: ".adapta.one" }],
"Log in to Adapta at agent.adapta.one. The session token will be extracted.",
{ cookieDomain: ".adapta.one" }
),
// ── VeoAI Free Web ────────────────────────────────────────
config(
"veoaifree-web",
"VeoAI Free",
"https://veoaifree.com/",
"https://veoaifree.com",
[{ type: "cookie", name: "wordpress_logged_in", domain: ".veoaifree.com" }],
"Log in to VeoAI Free at veoaifree.com. The WordPress session cookie will be extracted.",
{
cookieDomain: ".veoaifree.com",
pollingConfig: QUICK_POLLING,
}
),
// ── Missing Provider: ChatGLM (Zhipu) ──────────────────────
config(
"chatglm-web",
"ChatGLM (Zhipu AI)",
"https://chatglm.cn/",
"https://chatglm.cn",
[
{ type: "cookie", name: "chatglm_session", domain: ".chatglm.cn" },
{ type: "localStorage", key: "token" },
],
"Log in to ChatGLM at chatglm.cn with your phone number. The session token will be extracted.",
{ cookieDomain: ".chatglm.cn" }
),
// ── Missing Provider: Xiaomi MiMo ──────────────────────────
config(
"xiaomimimo-web",
"Xiaomi MiMo AI Studio",
"https://aistudio.xiaomimimo.com/login",
"https://aistudio.xiaomimimo.com",
[
{ type: "cookie", name: "session", domain: ".xiaomimimo.com" },
{ type: "localStorage", key: "access_token" },
],
"Log in to Xiaomi MiMo AI Studio at aistudio.xiaomimimo.com. The session token will be extracted.",
{ cookieDomain: ".xiaomimimo.com" }
),
// ── Missing Provider: Manus ────────────────────────────────
config(
"manus-web",
"Manus AI",
"https://manus.im/login",
"https://manus.im",
[
{ type: "cookie", name: "manus_session", domain: ".manus.im" },
{ type: "localStorage", key: "auth_token" },
],
"Log in to Manus at manus.im. The session cookie will be extracted.",
{ cookieDomain: ".manus.im" }
),
// ── Z.ai Web (#4056) ────────────────────────────────────────
config(
"zai-web",
"Z.ai Web",
"https://chat.z.ai/",
"https://chat.z.ai",
[{ type: "localStorage", key: "token" }],
'Log in to Z.ai at chat.z.ai. OmniRoute extracts the Local Storage value named "token"; chat CAPTCHA is handled by the browser transport.'
),
];
// ─── Registry ───────────────────────────────────────────────────────────────
const CONFIG_MAP = new Map<string, TokenExtractionConfig>();
for (const cfg of RAW_CONFIGS) {
CONFIG_MAP.set(cfg.providerId, cfg);
}
/** Get extraction config for a specific provider */
export function getExtractionConfig(providerId: string): TokenExtractionConfig | undefined {
return CONFIG_MAP.get(providerId);
}
/** List all registered extraction configs */
export function listExtractionConfigs(): TokenExtractionConfig[] {
return [...RAW_CONFIGS];
}
/** The shared config map — used by LoginManager and InAppLoginService */
export const TOKEN_EXTRACTION_CONFIGS = CONFIG_MAP;