diff --git a/README.md b/README.md index b2ccbb4338..93c975820e 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,31 @@ base_url = "http://localhost:8787/bili/https://api.openai.com/v1" upstream URL is configured, prepend `http://localhost:8787/bili/` to it. Nothing else changes. -#### B. Login/subscription clients (MITM transparent proxy) +#### B. Codex Route (API key or ChatGPT subscription) + +Codex can be connected without changing its active `model_provider` and +without trusting a MITM certificate. Start bili, open +[http://localhost:8787/__bili/](http://localhost:8787/__bili/), then enable +**Codex Route** on the Routing page. For scripted startup, enable it before +starting the owning server: + +```bash +bili codex enable --port 8787 +bili start --port 8787 +``` + +billion-context resolves only the active Codex provider, records its real +`base_url`, and temporarily replaces that one field with +`http://127.0.0.1:8787/codex`. The provider id never changes, so existing Codex +history stays in the same bucket. Disabling the route or gracefully stopping +the owning bili process restores the original field; edits made by the user +while routing was active are preserved. A stale takeover is safely recovered +the next time bili starts. + +The Routing page also offers a manual, backup-first repair for sessions created +by older bili test provider ids. Normal routing never migrates Codex history. + +#### C. Other login/subscription clients (MITM transparent proxy) Clients you sign **into an account** (ChatGPT Plus/Pro, Claude, ZCode coding plan, …) authenticate via **OAuth and hardcode the endpoint** — you can't @@ -126,7 +150,7 @@ Supported login clients: | Client | Login | Endpoint hardcoded | Status | |---|---|---|---| | **ZCode** | bigmodel coding plan (OAuth) | `open.bigmodel.cn` (builtin provider) | ✅ tested | -| **Codex** | ChatGPT account (OAuth) | `chatgpt.com/backend-api` | ❓ untested (may not work — needs verification) | +| **Codex** | ChatGPT account (OAuth) | `chatgpt.com/backend-api` | ✅ use Codex Route above; MITM is fallback only | | **Claude Code** | Claude subscription (OAuth) | `api.anthropic.com` | ❓ untested (may not work — needs verification) | How MITM mode works: the client only offers an **HTTP proxy** setting, so it @@ -153,8 +177,8 @@ non-model traffic. - **HTTP Proxy**: `http://127.0.0.1:8787` - **Proxy CA certificate path**: `~/.local/share/billion-context/ca/root-ca.pem` - (optional) **No-proxy list**: `localhost,127.0.0.1` - - (For ZCode specifically: **Settings → Network**. For Codex/Claude Code: - set the `HTTPS_PROXY` env var and `NODE_EXTRA_CA_CERTS` to the CA path.) + - (For ZCode specifically: **Settings → Network**. For Claude Code, set the + `HTTPS_PROXY` env var and `NODE_EXTRA_CA_CERTS` to the CA path.) 3. Restart the client. Its model traffic now flows through billion-context with compression injected. Send a message and check the proxy log @@ -435,17 +459,23 @@ If the proxy's own outbound connections to a model provider are blocked ``` Rules: -- **Global `proxy`** (top level) applies to every provider's outbound. -- **Per-URL `proxy`** overrides the global for that host. +- **Per-URL `proxy`** has the highest priority for its matching provider URL. +- Remaining priority is `BILI_UPSTREAM_PROXY` → Web UI manual proxy → top-level + `proxy` → `HTTPS_PROXY` / `HTTP_PROXY` / `ALL_PROXY` → Windows system proxy + → direct. - Empty string `""` means **explicitly direct** (override-and-disable). -- Neither set = direct connect. -- Only HTTP proxies (`http://host:port`). SOCKS5 is not supported yet. +- Auto mode honors `NO_PROXY` and the Windows proxy bypass list for + environment/system fallbacks. A proxy pointing back to bili's own local port + is ignored or rejected to prevent a loop. +- HTTP and HTTPS proxy origins are supported. SOCKS5 is not supported yet. - Both outbound paths are covered: `/bili/` path-mode (fetch) AND MITM CONNECT tunnels (the proxy's connection to the real upstream goes through the HTTP CONNECT proxy). -Env override: `BILI_UPSTREAM_PROXY=http://127.0.0.1:20172` (same as global -`proxy`; config file wins over env if both set). +Env override: `BILI_UPSTREAM_PROXY=http://127.0.0.1:20172` (higher priority than +the config file). On Windows, common Clash/Mihomo static system proxies are +discovered automatically; the Web UI shows the effective source and any PAC +URL detected in Internet Settings. **MITM vs `/bili/` — distinguishing the key scheme.** A login client (ZCode via MITM) and an API-key client can both hit the same host diff --git a/README.zh-CN.md b/README.zh-CN.md index 0aaa3792ef..e5183cc082 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -96,7 +96,27 @@ base_url = "http://localhost:8787/bili/https://api.openai.com/v1" **其他 API-key 客户端(Cursor / Aider / Continue ……)** —— 只要配置了上游 URL,前面加 `http://localhost:8787/bili/` 就行,其他都不用改。 -#### B. 登录/订阅客户端(MITM 透明代理) +#### B. Codex 路由(API key 或 ChatGPT 订阅) + +Codex 不需要改 `model_provider`,也不需要信任 MITM 证书。启动 bili,打开 +[http://localhost:8787/__bili/](http://localhost:8787/__bili/),在“路由”页启用 +**Codex 路由**。脚本化启动时,先启用路由,再启动拥有它的服务: + +```bash +bili codex enable --port 8787 +bili start --port 8787 +``` + +billion-context 只解析当前激活的 Codex provider,记录它的真实 `base_url`, +并临时只把这个字段替换成 `http://127.0.0.1:8787/codex`。provider id 始终 +不变,所以现有 Codex 历史仍在同一个分桶。关闭路由或正常停止拥有该路由的 +bili 进程时会恢复原字段;路由期间用户自己做的新修改会被保留。异常退出留下 +的接管状态会在下次启动 bili 时安全恢复。 + +“路由”页还提供旧版 bili 测试 provider 会话的手动修复:先预览、先备份、再 +修复。正常路由绝不会自动迁移 Codex 历史。 + +#### C. 其他登录/订阅客户端(MITM 透明代理) 需要 **登录账号**的客户端(ChatGPT Plus/Pro、Claude、ZCode coding plan ……)通过 **OAuth 认证,且硬编码了端点**——你改不了 baseURL,所以 `/bili/` @@ -107,7 +127,7 @@ base_url = "http://localhost:8787/bili/https://api.openai.com/v1" | 客户端 | 登录方式 | 硬编码端点 | 状态 | |---|---|---|---| | **ZCode** | 智谱 coding plan(OAuth) | `open.bigmodel.cn`(内置 provider) | ✅ 已测试 | -| **Codex** | ChatGPT 账号(OAuth) | `chatgpt.com/backend-api` | ❓ 未测试(可能不支持,需验证) | +| **Codex** | ChatGPT 账号(OAuth) | `chatgpt.com/backend-api` | ✅ 优先使用上面的 Codex 路由;MITM 仅作 fallback | | **Claude Code** | Claude 订阅(OAuth) | `api.anthropic.com` | ❓ 未测试(可能不支持,需验证) | MITM 模式原理:这类客户端只提供 **HTTP 代理**设置,所以它发送 @@ -130,8 +150,8 @@ MITM 默认开启,且只对一份 **白名单**中的模型域名(`open.bigmodel - **HTTP 代理**:`http://127.0.0.1:8787` - **代理 CA 证书路径**:`~/.local/share/billion-context/ca/root-ca.pem` - (可选)**不走代理列表**:`localhost,127.0.0.1` - - (ZCode 具体位置:**设置 → 网络**。Codex/Claude Code:设 `HTTPS_PROXY` - 环境变量 + `NODE_EXTRA_CA_CERTS` 指向 CA 路径。) + - (ZCode 具体位置:**设置 → 网络**。Claude Code:设 `HTTPS_PROXY` 环境变量 + + `NODE_EXTRA_CA_CERTS` 指向 CA 路径。) 3. 重启客户端。它的模型流量现在会经过 billion-context 并注入压缩。发一条消息,看 proxy 日志(`~/.local/state/billion-context/bili.log`)应出现 `mitm <域名>:443 tunnel established`。 @@ -368,14 +388,18 @@ key 就是客户端写在 `/bili/` 后面的那个字符串: ``` 规则: -- **全局 `proxy`**(顶层)对所有提供商的出站生效。 -- **按 URL 的 `proxy`** 覆盖该域名的全局设置。 +- **按 URL 的 `proxy`** 对匹配的 provider URL 优先级最高。 +- 其余优先级为:`BILI_UPSTREAM_PROXY` → Web UI 手动代理 → 顶层 `proxy` → + `HTTPS_PROXY` / `HTTP_PROXY` / `ALL_PROXY` → Windows 系统代理 → 直连。 - 空字符串 `""` 表示**明确直连**(覆盖并禁用)。 -- 都不配 = 直连。 -- 只支持 HTTP 代理(`http://host:port`)。SOCKS5 暂不支持。 +- 自动模式会让环境/系统 fallback 遵守 `NO_PROXY` 与 Windows 绕过列表。 + 指回 bili 自己本地端口的代理会被跳过或拒绝,防止自环。 +- 支持 HTTP 和 HTTPS 代理 origin。SOCKS5 暂不支持。 - 两条出站路径都覆盖:`/bili/` 路径模式(fetch)和 MITM CONNECT 隧道(代理连接真实上游的链路走 HTTP CONNECT 代理)。 -环境变量覆盖:`BILI_UPSTREAM_PROXY=http://127.0.0.1:20172`(等同全局 `proxy`;两者都设时配置文件优先)。 +环境变量覆盖:`BILI_UPSTREAM_PROXY=http://127.0.0.1:20172`(优先于配置文件)。 +Windows 下会自动发现常见 Clash/Mihomo 静态系统代理;Web UI 会显示实际来源, +以及 Internet Settings 中检测到的 PAC URL。 **MITM 与 `/bili/` —— 用 scheme 区分。** 登录客户端(ZCode 走 MITM)和 API-key 客户端可能连同一个域名(`open.bigmodel.cn`)。为了让它们的配置能区分,MITM 流量在查找键里用 `mitm://` scheme,`/bili/` 流量用真实的 `https://`: diff --git a/package-lock.json b/package-lock.json index d0518cbc6f..793b257cfb 100644 --- a/package-lock.json +++ b/package-lock.json @@ -16,6 +16,7 @@ "@types/node": "^22.0.0", "@types/node-forge": "^1.3.14", "acp-kernel": "0.0.17", + "fzstd": "0.1.1", "node-forge": "^1.4.0", "tar": "^7.5.22", "tsup": "^8.0.0", @@ -1171,6 +1172,13 @@ "node": "^8.16.0 || ^10.6.0 || >=11.0.0" } }, + "node_modules/fzstd": { + "version": "0.1.1", + "resolved": "https://registry.npmmirror.com/fzstd/-/fzstd-0.1.1.tgz", + "integrity": "sha512-dkuVSOKKwh3eas5VkJy1AW1vFpet8TA/fGmVA5krThl8YcOVE/8ZIoEA1+U1vEn5ckxxhLirSdY837azmbaNHA==", + "dev": true, + "license": "MIT" + }, "node_modules/joycon": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/joycon/-/joycon-3.1.1.tgz", diff --git a/package.json b/package.json index 7e78c672ca..07c66550d6 100644 --- a/package.json +++ b/package.json @@ -42,6 +42,7 @@ "@types/node": "^22.0.0", "@types/node-forge": "^1.3.14", "acp-kernel": "0.0.17", + "fzstd": "0.1.1", "node-forge": "^1.4.0", "tar": "^7.5.22", "tsup": "^8.0.0", diff --git a/src/cli.ts b/src/cli.ts index 6ce81d2b93..ad3253f00b 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -24,6 +24,13 @@ import { checkForUpdate, startAutoUpdate } from "./update.js"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import path from "node:path"; +import { + claimCodexTakeover, + disableCodexTakeover, + enableCodexTakeover, + getCodexTakeoverStatus, + recoverStaleCodexTakeover, +} from "./codex-takeover.js"; const VERSION = (() => { try { @@ -51,6 +58,9 @@ const HELP = `bili ${VERSION} — billion-context proxy Usage: bili [start] [options] start the proxy (default: reads ${defaultConfigFile()}) + bili codex enable [--port N] route Codex Desktop through the local proxy + bili codex disable restore the active Codex provider endpoint + bili codex status show Codex Desktop local-route status bili update check for & install a newer version now bili --version print version bili --help show this help @@ -72,7 +82,8 @@ Docs: https://github.com/ranxianglei/billion-context `; type Parsed = { - command: "start" | "update" | "help" | "version"; + command: "start" | "update" | "codex" | "help" | "version"; + codexAction?: "enable" | "disable" | "status"; overrides: Record; }; @@ -133,7 +144,7 @@ function parseArgs(argv: string[]): Parsed { } } - // First positional (if any) is the command. "start" | "update" are recognized; + // First positional (if any) is the command. "start" | "update" | "codex" are recognized; // an unknown command is an error. if (positional.length > 0) { const cmd = positional[0]!; @@ -141,17 +152,34 @@ function parseArgs(argv: string[]): Parsed { command = command === "help" || command === "version" ? command : "start"; } else if (cmd === "update") { command = "update"; + } else if (cmd === "codex") { + command = "codex"; } else { console.error(`bili: unknown command "${cmd}" (try "bili --help")`); process.exit(2); } } - return { command, overrides }; + let codexAction: Parsed["codexAction"]; + if (positional[0] === "codex") { + command = "codex"; + const action = positional[1]; + if (action !== "enable" && action !== "disable" && action !== "status") { + console.error('bili: "codex" requires enable, disable, or status'); + process.exit(2); + } + if (positional.length > 2) { + console.error(`bili: unexpected argument "${positional[2]}"`); + process.exit(2); + } + codexAction = action; + } + + return { command, codexAction, overrides }; } export async function main(): Promise { - const { command, overrides } = parseArgs(process.argv.slice(2)); + const { command, codexAction, overrides } = parseArgs(process.argv.slice(2)); if (command === "help") { process.stdout.write(HELP); return; @@ -166,15 +194,36 @@ export async function main(): Promise { return; } - // CLI flags override env (which overrides the config file inside - // loadOptions). Merge into process.env so loadOptions picks them up. for (const [k, v] of Object.entries(overrides)) { if (v !== undefined) process.env[k] = v; } + if (command === "codex") { + if (codexAction === "enable") recoverStaleCodexTakeover(); + const status = codexAction === "enable" + ? enableCodexTakeover(loadOptions().port) + : codexAction === "disable" + ? disableCodexTakeover() + : getCodexTakeoverStatus(); + if (status.state === "enabled") { + process.stdout.write(`Codex route enabled for ${status.providerId}: ${status.baseUrl} → ${status.originalBaseUrl}\n`); + } else if (status.state === "disabled") { + const provider = status.provider ? ` (provider ${status.provider.id})` : ""; + process.stdout.write(`Codex route disabled${provider}: ${status.configPath}${status.detail ? `\n${status.detail}` : ""}\n`); + } else { + process.stdout.write(`Codex Desktop local route conflict: ${status.detail ?? status.configPath}\n`); + process.exitCode = 1; + } + return; + } + + // CLI flags override env (which overrides the config file inside + // loadOptions). Merge into process.env so loadOptions picks them up. // First run: seed a template config so the user has a file to edit rather // than a bare error. No-op if it already exists. + recoverStaleCodexTakeover(); ensureConfigTemplate(); const opts = loadOptions(); + claimCodexTakeover(process.pid); await startServer(opts); // Start background auto-update after the server is listening so a slow diff --git a/src/codex-history.ts b/src/codex-history.ts new file mode 100644 index 0000000000..04e44a6cf4 --- /dev/null +++ b/src/codex-history.ts @@ -0,0 +1,300 @@ +import { + copyFileSync, + existsSync, + mkdirSync, + readFileSync, + readdirSync, + renameSync, + rmSync, + statSync, + writeFileSync, +} from "node:fs"; +import path from "node:path"; +import { createHash, randomUUID } from "node:crypto"; +import { homedir } from "node:os"; +import { dataDir } from "./paths.js"; +import { codexConfigFile, readCodexConfig, resolveActiveCodexProvider } from "./codex-provider.js"; + +export const LEGACY_BILI_CODEX_PROVIDER_IDS = ["billion-context-codex", "bili_chatgpt"] as const; + +export type CodexHistoryPreview = { + targetProviderId: string; + sourceProviderIds: string[]; + jsonlFiles: number; + sessions: number; + stateRows: number; + stateDbSupported: boolean; + stateDbError?: string; +}; + +export type CodexHistoryRepairResult = CodexHistoryPreview & { + backupPath: string; + migratedJsonlFiles: number; + migratedStateRows: number; +}; + +type SessionFile = { + path: string; + relative: string; + matches: number; + size: number; + mtimeMs: number; + mode: number; +}; + +type SqliteModule = typeof import("node:sqlite"); +type SqliteDatabase = import("node:sqlite").DatabaseSync; + +function codexHome(configPath: string): string { + return path.dirname(configPath); +} + +function collectJsonlFiles(root: string, current: string = root, depth: number = 0): string[] { + if (depth > 10 || !existsSync(current)) return []; + const out: string[] = []; + for (const entry of readdirSync(current, { withFileTypes: true })) { + const filePath = path.join(current, entry.name); + if (entry.isDirectory()) out.push(...collectJsonlFiles(root, filePath, depth + 1)); + else if (entry.isFile() && entry.name.endsWith(".jsonl")) out.push(filePath); + } + return out; +} + +function legacySessionMeta(line: string): boolean { + if (!line.includes('"session_meta"') || !line.includes('"model_provider"')) return false; + try { + const value = JSON.parse(line) as Record; + if (value.type !== "session_meta" || !value.payload || typeof value.payload !== "object") return false; + const provider = (value.payload as Record).model_provider; + return typeof provider === "string" && (LEGACY_BILI_CODEX_PROVIDER_IDS as readonly string[]).includes(provider); + } catch { + return false; + } +} + +function sessionFiles(configPath: string): SessionFile[] { + const root = codexHome(configPath); + const files = [ + ...collectJsonlFiles(path.join(root, "sessions")), + ...collectJsonlFiles(path.join(root, "archived_sessions")), + ]; + return files.flatMap((filePath) => { + const before = statSync(filePath); + const matches = readFileSync(filePath, "utf8").split(/\r\n|\r|\n/).filter(legacySessionMeta).length; + const after = statSync(filePath); + if (before.size !== after.size || before.mtimeMs !== after.mtimeMs) { + throw new Error(`Codex session file changed while being inspected: ${filePath}`); + } + return matches > 0 ? [{ + path: filePath, + relative: path.relative(root, filePath), + matches, + size: after.size, + mtimeMs: after.mtimeMs, + mode: after.mode, + }] : []; + }); +} + +function parseTopLevelString(configText: string, key: string): string | undefined { + const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + for (const line of configText.split(/\r\n|\r|\n/)) { + if (/^\s*\[/.test(line)) break; + const match = line.match(new RegExp(`^\\s*${escaped}\\s*=\\s*(\"(?:\\\\.|[^\"\\\\])*\"|'[^']*')\\s*(?:#.*)?$`)); + if (!match) continue; + if (match[1]!.startsWith("'")) return match[1]!.slice(1, -1); + try { + const value = JSON.parse(match[1]!) as unknown; + return typeof value === "string" ? value : undefined; + } catch { + return undefined; + } + } + return undefined; +} + +function resolveUserPath(value: string): string { + if (value === "~") return homedir(); + if (value.startsWith("~/") || value.startsWith("~\\")) { + return path.join(homedir(), value.slice(2)); + } + return path.resolve(value); +} + +function stateDbPaths(configPath: string): string[] { + const home = codexHome(configPath); + const paths = [path.join(home, "state_5.sqlite")]; + const configText = readCodexConfig(configPath); + const configured = parseTopLevelString(configText, "sqlite_home"); + const environment = process.env.CODEX_SQLITE_HOME?.trim(); + const extra = configured ?? environment; + if (extra) { + const candidate = path.join(resolveUserPath(extra), "state_5.sqlite"); + if (!paths.includes(candidate)) paths.push(candidate); + } + return paths.filter(existsSync); +} + +async function sqliteModule(): Promise { + // Indirect specifier so the bundler cannot rewrite this into a + // __require("sqlite") shim (which strips the node: prefix and fails to + // resolve). Left as a native dynamic import(), resolved by Node 22.5+. + const specifier = "node:sqlite"; + return (await import(specifier)) as SqliteModule; +} + +function hasProviderColumn(database: SqliteDatabase): boolean { + return database.prepare("PRAGMA table_info(threads)").all().some((row) => row.name === "model_provider"); +} + +function placeholders(count: number): string { + return Array.from({ length: count }, () => "?").join(","); +} + +async function countStateRows(configPath: string): Promise<{ count: number; supported: boolean; error?: string }> { + const dbPaths = stateDbPaths(configPath); + if (dbPaths.length === 0) return { count: 0, supported: true }; + let sqlite: SqliteModule; + try { + sqlite = await sqliteModule(); + } catch (error) { + return { count: 0, supported: false, error: `state DB repair requires a Node runtime with node:sqlite: ${String(error)}` }; + } + let count = 0; + try { + for (const dbPath of dbPaths) { + const database = new sqlite.DatabaseSync(dbPath); + try { + database.exec("PRAGMA busy_timeout = 5000"); + if (!hasProviderColumn(database)) continue; + const row = database.prepare( + `SELECT COUNT(*) AS count FROM threads WHERE model_provider IN (${placeholders(LEGACY_BILI_CODEX_PROVIDER_IDS.length)})`, + ).get(...LEGACY_BILI_CODEX_PROVIDER_IDS); + const value = row?.count; + if (typeof value === "number") count += value; + else if (typeof value === "bigint") count += Number(value); + } finally { + database.close(); + } + } + return { count, supported: true }; + } catch (error) { + return { count, supported: false, error: String(error) }; + } +} + +export async function previewLegacyCodexHistory(configPath: string = codexConfigFile()): Promise { + const provider = resolveActiveCodexProvider(configPath); + const files = sessionFiles(configPath); + const state = await countStateRows(configPath); + return { + targetProviderId: provider.id, + sourceProviderIds: [...LEGACY_BILI_CODEX_PROVIDER_IDS], + jsonlFiles: files.length, + sessions: files.reduce((sum, file) => sum + file.matches, 0), + stateRows: state.count, + stateDbSupported: state.supported, + ...(state.error ? { stateDbError: state.error } : {}), + }; +} + +function assertSessionFileUnchanged(file: SessionFile): void { + const current = statSync(file.path); + if (current.size !== file.size || current.mtimeMs !== file.mtimeMs) { + throw new Error(`Codex session file changed during repair: ${file.path}`); + } +} + +function rewriteSessionFile(file: SessionFile, targetProviderId: string): boolean { + assertSessionFileUnchanged(file); + const before = readFileSync(file.path, "utf8"); + assertSessionFileUnchanged(file); + const eol = before.includes("\r\n") ? "\r\n" : "\n"; + const endsWithEol = /(?:\r\n|\r|\n)$/.test(before); + let changed = false; + const lines = before.split(/\r\n|\r|\n/); + if (endsWithEol) lines.pop(); + const after = lines.map((line) => { + if (!legacySessionMeta(line)) return line; + const value = JSON.parse(line) as Record; + const payload = value.payload as Record; + payload.model_provider = targetProviderId; + changed = true; + return JSON.stringify(value); + }).join(eol) + (endsWithEol ? eol : ""); + if (!changed) return false; + const tempPath = `${file.path}.${process.pid}.${randomUUID()}.tmp`; + try { + writeFileSync(tempPath, after, { encoding: "utf8", mode: file.mode }); + assertSessionFileUnchanged(file); + renameSync(tempPath, file.path); + } catch (error) { + rmSync(tempPath, { force: true }); + throw error; + } + return true; +} + +function stateBackupPath(backupPath: string, configPath: string, dbPath: string): string { + const root = codexHome(configPath); + const relative = path.relative(root, dbPath); + if (relative && !relative.startsWith(`..${path.sep}`) && relative !== ".." && !path.isAbsolute(relative)) { + return path.join(backupPath, "state", relative); + } + const digest = createHash("sha256").update(path.resolve(dbPath)).digest("hex").slice(0, 16); + return path.join(backupPath, "state", `external-${digest}`, path.basename(dbPath)); +} + +export async function repairLegacyCodexHistory(configPath: string = codexConfigFile()): Promise { + const preview = await previewLegacyCodexHistory(configPath); + if (!preview.stateDbSupported) throw new Error(preview.stateDbError ?? "Codex state DB cannot be repaired safely"); + const files = sessionFiles(configPath); + const databases = stateDbPaths(configPath); + const stamp = new Date().toISOString().replace(/[:.]/g, "-"); + const backupPath = path.join(dataDir(), "backups", "codex-history-repair", stamp); + mkdirSync(backupPath, { recursive: true }); + for (const file of files) { + assertSessionFileUnchanged(file); + const destination = path.join(backupPath, "jsonl", file.relative); + mkdirSync(path.dirname(destination), { recursive: true }); + copyFileSync(file.path, destination); + assertSessionFileUnchanged(file); + } + const sqlite = databases.length > 0 ? await sqliteModule() : undefined; + const opened: Array<{ database: SqliteDatabase; path: string }> = []; + try { + if (sqlite) { + for (const dbPath of databases) { + const database = new sqlite.DatabaseSync(dbPath); + opened.push({ database, path: dbPath }); + database.exec("PRAGMA busy_timeout = 5000"); + const destination = stateBackupPath(backupPath, configPath, dbPath); + mkdirSync(path.dirname(destination), { recursive: true }); + await sqlite.backup(database, destination); + } + } + let migratedJsonlFiles = 0; + for (const file of files) if (rewriteSessionFile(file, preview.targetProviderId)) migratedJsonlFiles++; + let migratedStateRows = 0; + for (const entry of opened) { + if (!hasProviderColumn(entry.database)) continue; + entry.database.exec("BEGIN IMMEDIATE"); + try { + const result = entry.database.prepare( + `UPDATE threads SET model_provider = ? WHERE model_provider IN (${placeholders(LEGACY_BILI_CODEX_PROVIDER_IDS.length)})`, + ).run(preview.targetProviderId, ...LEGACY_BILI_CODEX_PROVIDER_IDS); + migratedStateRows += Number(result.changes); + entry.database.exec("COMMIT"); + } catch (error) { + entry.database.exec("ROLLBACK"); + throw error; + } + } + return { ...preview, backupPath, migratedJsonlFiles, migratedStateRows }; + } catch (error) { + throw new Error(`Codex history repair stopped; backups are at ${backupPath}: ${String(error)}`, { cause: error }); + } finally { + for (const entry of opened) entry.database.close(); + if (files.length === 0 && databases.length === 0) rmSync(backupPath, { recursive: true, force: true }); + } +} diff --git a/src/codex-provider.ts b/src/codex-provider.ts new file mode 100644 index 0000000000..09231252e2 --- /dev/null +++ b/src/codex-provider.ts @@ -0,0 +1,321 @@ +import { existsSync, readFileSync } from "node:fs"; +import { homedir } from "node:os"; +import path from "node:path"; + +export const CODEX_DEFAULT_PROVIDER_ID = "openai"; + +export type ActiveCodexProvider = { + id: string; + explicit: boolean; + sectionExists: boolean; + name?: string; + baseUrl: string; + wireApi: string; + requiresOpenaiAuth: boolean; + supportsWebsockets: boolean; + configPath: string; +}; + +export type ProviderFieldValue = string | boolean; + +type Line = { + start: number; + contentEnd: number; + end: number; + content: string; + eol: string; +}; + +type Assignment = { + value: ProviderFieldValue; + encoded: string; + start: number; + end: number; + lineStart: number; + lineEnd: number; +}; + +export type ProviderFieldSnapshot = { + value: ProviderFieldValue; + encoded: string; +}; + +type Section = { + id: string; + headerStart: number; + bodyStart: number; + end: number; +}; + +const BUILTIN_PROVIDERS: Record> = { + openai: { + name: "OpenAI", + baseUrl: "https://chatgpt.com/backend-api/codex", + wireApi: "responses", + requiresOpenaiAuth: true, + supportsWebsockets: true, + }, +}; + +function linesOf(text: string): Line[] { + const lines: Line[] = []; + const re = /[^\r\n]*(?:\r\n|\r|\n|$)/g; + for (;;) { + const match = re.exec(text); + if (!match || match[0] === "") break; + const raw = match[0]; + const eol = raw.match(/(\r\n|\r|\n)$/)?.[1] ?? ""; + const content = eol ? raw.slice(0, -eol.length) : raw; + lines.push({ + start: match.index, + contentEnd: match.index + content.length, + end: match.index + raw.length, + content, + eol, + }); + if (match.index + raw.length >= text.length) break; + } + return lines; +} + +function preferredEol(text: string): string { + return text.match(/\r\n|\r|\n/)?.[0] ?? (process.platform === "win32" ? "\r\n" : "\n"); +} + +function parseTomlString(raw: string): string | undefined { + if (raw.startsWith("\"") && raw.endsWith("\"")) { + try { + const parsed = JSON.parse(raw) as unknown; + return typeof parsed === "string" ? parsed : undefined; + } catch { + return undefined; + } + } + if (raw.startsWith("'") && raw.endsWith("'")) return raw.slice(1, -1); + return undefined; +} + +function parseProviderId(raw: string): string | undefined { + const trimmed = raw.trim(); + const quoted = parseTomlString(trimmed); + if (quoted !== undefined) return quoted; + return /^[A-Za-z0-9_-]+$/.test(trimmed) ? trimmed : undefined; +} + +function sections(text: string): Section[] { + const found: Section[] = []; + const lines = linesOf(text); + for (let index = 0; index < lines.length; index++) { + const line = lines[index]; + const match = line.content.match(/^\s*\[\s*model_providers\s*\.\s*("(?:\\.|[^"\\])*"|'[^']*'|[A-Za-z0-9_-]+)\s*\]\s*(?:#.*)?$/); + if (!match) continue; + const id = parseProviderId(match[1]!); + if (!id) continue; + let end = text.length; + for (let next = index + 1; next < lines.length; next++) { + if (/^\s*\[/.test(lines[next].content)) { + end = lines[next].start; + break; + } + } + found.push({ id, headerStart: line.start, bodyStart: line.end, end }); + } + return found; +} + +function assignmentInRange(text: string, key: string, start: number, end: number): Assignment | undefined { + const escaped = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + const pattern = new RegExp(`^(\\s*${escaped}\\s*=\\s*)(\"(?:\\\\.|[^\"\\\\])*\"|'[^']*'|true|false)(\\s*(?:#.*)?)$`); + for (const line of linesOf(text)) { + if (line.start < start || line.start >= end) continue; + const match = line.content.match(pattern); + if (!match) continue; + const raw = match[2]!; + const value = raw === "true" ? true : raw === "false" ? false : parseTomlString(raw); + if (value === undefined) continue; + const valueStart = line.start + match[1]!.length; + return { + value, + encoded: raw, + start: valueStart, + end: valueStart + raw.length, + lineStart: line.start, + lineEnd: line.end, + }; + } + return undefined; +} + +function rootAssignment(text: string, key: string): Assignment | undefined { + const firstSection = linesOf(text).find((line) => /^\s*\[/.test(line.content)); + return assignmentInRange(text, key, 0, firstSection?.start ?? text.length); +} + +function sectionFor(text: string, providerId: string): Section | undefined { + return sections(text).find((section) => section.id === providerId); +} + +function fieldFor(text: string, providerId: string, key: string): Assignment | undefined { + const section = sectionFor(text, providerId); + return section ? assignmentInRange(text, key, section.bodyStart, section.end) : undefined; +} + +function encodeValue(value: ProviderFieldValue): string { + return typeof value === "boolean" ? String(value) : JSON.stringify(value); +} + +export function codexConfigFile(env: NodeJS.ProcessEnv = process.env): string { + const codexHome = env.CODEX_HOME?.trim(); + return path.join(codexHome ? path.resolve(codexHome) : path.join(homedir(), ".codex"), "config.toml"); +} + +export function readCodexConfig(configPath: string = codexConfigFile()): string { + return existsSync(configPath) ? readFileSync(configPath, "utf8") : ""; +} + +export function resolveActiveCodexProvider(configPath: string = codexConfigFile()): ActiveCodexProvider { + const text = readCodexConfig(configPath); + const providerAssignment = rootAssignment(text, "model_provider"); + if (providerAssignment && typeof providerAssignment.value !== "string") { + throw new Error("Codex model_provider is not a string"); + } + const explicit = typeof providerAssignment?.value === "string"; + const id = explicit ? providerAssignment.value as string : CODEX_DEFAULT_PROVIDER_ID; + const section = sectionFor(text, id); + const builtin = BUILTIN_PROVIDERS[id]; + if (!section && !builtin) { + throw new Error(`cannot safely resolve active Codex provider \"${id}\": [model_providers.${id}] is missing`); + } + + const stringField = (key: string, fallback?: string): string | undefined => { + const assignment = fieldFor(text, id, key); + if (assignment && typeof assignment.value !== "string") { + throw new Error(`Codex provider \"${id}\" has a non-string ${key}`); + } + return typeof assignment?.value === "string" ? assignment.value : fallback; + }; + const boolField = (key: string, fallback: boolean): boolean => { + const assignment = fieldFor(text, id, key); + if (assignment && typeof assignment.value !== "boolean") { + throw new Error(`Codex provider \"${id}\" has a non-boolean ${key}`); + } + return typeof assignment?.value === "boolean" ? assignment.value : fallback; + }; + const baseUrl = stringField("base_url", builtin?.baseUrl)?.replace(/\/+$/, ""); + const wireApi = stringField("wire_api", builtin?.wireApi); + const requiresOpenaiAuth = boolField("requires_openai_auth", builtin?.requiresOpenaiAuth ?? false); + const supportsWebsockets = boolField("supports_websockets", builtin?.supportsWebsockets ?? false); + if (!baseUrl || !wireApi) { + throw new Error(`cannot safely resolve active Codex provider \"${id}\": base_url/wire_api fields are incomplete`); + } + let parsed: URL; + try { + parsed = new URL(baseUrl); + } catch { + throw new Error(`Codex provider \"${id}\" has an invalid base_url`); + } + if (parsed.protocol !== "http:" && parsed.protocol !== "https:") { + throw new Error(`Codex provider \"${id}\" base_url must use http:// or https://`); + } + if (parsed.search || parsed.hash) { + throw new Error(`Codex provider \"${id}\" base_url cannot contain a query string or fragment`); + } + return { + id, + explicit, + sectionExists: !!section, + name: stringField("name", builtin?.name), + baseUrl, + wireApi, + requiresOpenaiAuth, + supportsWebsockets, + configPath, + }; +} + +export function getCodexProviderField(text: string, providerId: string, key: string): ProviderFieldValue | undefined { + return fieldFor(text, providerId, key)?.value; +} + +export function getCodexProviderFieldSnapshot( + text: string, + providerId: string, + key: string, +): ProviderFieldSnapshot | undefined { + const field = fieldFor(text, providerId, key); + return field ? { value: field.value, encoded: field.encoded } : undefined; +} + +export function setCodexProviderField( + text: string, + providerId: string, + key: string, + value: ProviderFieldValue, +): { text: string; added: boolean } { + const existing = fieldFor(text, providerId, key); + if (existing) { + return { + text: text.slice(0, existing.start) + encodeValue(value) + text.slice(existing.end), + added: false, + }; + } + const section = sectionFor(text, providerId); + if (!section) throw new Error(`Codex provider section \"${providerId}\" is missing`); + const eol = preferredEol(text); + const prefix = section.end > 0 && !/(?:\r\n|\r|\n)$/.test(text.slice(0, section.end)) ? eol : ""; + const line = `${key} = ${encodeValue(value)}${eol}`; + return { text: text.slice(0, section.end) + prefix + line + text.slice(section.end), added: true }; +} + +export function restoreCodexProviderField( + text: string, + providerId: string, + key: string, + snapshot: ProviderFieldSnapshot, +): string { + const existing = fieldFor(text, providerId, key); + if (!existing) throw new Error(`Codex provider field \"${providerId}.${key}\" is missing`); + return text.slice(0, existing.start) + snapshot.encoded + text.slice(existing.end); +} + +export function removeCodexProviderField(text: string, providerId: string, key: string): string { + const existing = fieldFor(text, providerId, key); + return existing ? text.slice(0, existing.lineStart) + text.slice(existing.lineEnd) : text; +} + +export function appendCodexProviderSection( + text: string, + providerId: string, + fields: Record, +): string { + if (sectionFor(text, providerId)) throw new Error(`Codex provider section \"${providerId}\" already exists`); + const eol = preferredEol(text); + const separator = text.length === 0 ? "" : /(?:\r\n|\r|\n)$/.test(text) ? eol : eol + eol; + const body = Object.entries(fields).map(([key, value]) => `${key} = ${encodeValue(value)}`).join(eol); + return `${text}${separator}[model_providers.${providerId}]${eol}${body}${eol}`; +} + +export function removeCodexProviderSectionIfEmpty(text: string, providerId: string): string { + const section = sectionFor(text, providerId); + if (!section) return text; + const body = text.slice(section.bodyStart, section.end); + const meaningful = body.split(/\r\n|\r|\n/).some((line) => { + const trimmed = line.trim(); + return trimmed.length > 0; + }); + if (meaningful) return text; + return text.slice(0, section.headerStart) + text.slice(section.end); +} + +export function removeCodexProviderSectionWithPrefixIfEmpty( + text: string, + providerId: string, + prefixLength: number, +): string { + const section = sectionFor(text, providerId); + if (!section) return text; + const withoutSection = removeCodexProviderSectionIfEmpty(text, providerId); + if (withoutSection === text) return text; + const start = Math.max(0, section.headerStart - prefixLength); + return text.slice(0, start) + text.slice(section.end); +} diff --git a/src/codex-takeover.ts b/src/codex-takeover.ts new file mode 100644 index 0000000000..aea5a87b38 --- /dev/null +++ b/src/codex-takeover.ts @@ -0,0 +1,398 @@ +import { + closeSync, + existsSync, + fsyncSync, + mkdirSync, + openSync, + readFileSync, + renameSync, + statSync, + unlinkSync, + writeFileSync, +} from "node:fs"; +import { createHash, randomUUID } from "node:crypto"; +import path from "node:path"; +import { dataDir } from "./paths.js"; +import { + appendCodexProviderSection, + codexConfigFile, + getCodexProviderField, + getCodexProviderFieldSnapshot, + readCodexConfig, + removeCodexProviderField, + removeCodexProviderSectionWithPrefixIfEmpty, + restoreCodexProviderField, + resolveActiveCodexProvider, + setCodexProviderField, + type ActiveCodexProvider, + type ProviderFieldValue, +} from "./codex-provider.js"; + +export const CODEX_ROUTE_STATE_VERSION = 1; + +const INSTALLED_BASE_FIELDS: Record = { + name: "OpenAI", + wire_api: "responses", + requires_openai_auth: true, +}; + +type StoredField = { + present: boolean; + value?: ProviderFieldValue; + encoded?: string; +}; + +export type CodexRouteState = { + version: typeof CODEX_ROUTE_STATE_VERSION; + active: true; + pid?: number; + providerId: string; + configPath: string; + sectionAdded: boolean; + sectionPrefixLength: number; + insertedFields: string[]; + originalFields: Record; + original: { + baseUrl: string; + supportsWebsockets: boolean; + }; + installed: { + baseUrl: string; + supportsWebsockets: false; + }; + configHashBefore: string; + startedAt: string; + lastRequestAt?: string; + lastRequestPath?: string; +}; + +export type CodexTakeoverStatus = { + state: "enabled" | "disabled" | "conflict"; + configPath: string; + statePath: string; + provider?: ActiveCodexProvider; + providerId?: string; + port?: number; + baseUrl?: string; + originalBaseUrl?: string; + lastRequestAt?: string; + lastRequestPath?: string; + detail?: string; +}; + +export type CodexTakeoverPaths = { + configPath?: string; + statePath?: string; + ownerPid?: number; +}; + +export function codexRouteStateFile(): string { + const override = process.env.BILI_CODEX_ROUTE_STATE?.trim(); + return override ? path.resolve(override) : path.join(dataDir(), "codex-route-state.json"); +} + +function resolvePaths(options: CodexTakeoverPaths = {}): Required> & Pick { + return { + configPath: options.configPath ?? codexConfigFile(), + statePath: options.statePath ?? codexRouteStateFile(), + ownerPid: options.ownerPid, + }; +} + +function sha256(text: string): string { + return createHash("sha256").update(text).digest("hex"); +} + +function atomicWrite(filePath: string, text: string): void { + mkdirSync(path.dirname(filePath), { recursive: true }); + const mode = existsSync(filePath) ? statSync(filePath).mode : 0o600; + const tempPath = path.join(path.dirname(filePath), `.${path.basename(filePath)}.${process.pid}.${randomUUID()}.tmp`); + let fd: number | undefined; + try { + fd = openSync(tempPath, "wx", mode); + writeFileSync(fd, text, "utf8"); + fsyncSync(fd); + closeSync(fd); + fd = undefined; + renameSync(tempPath, filePath); + } catch (error) { + if (fd !== undefined) closeSync(fd); + try { unlinkSync(tempPath); } catch { } + throw error; + } +} + +function parseState(statePath: string): CodexRouteState | undefined { + if (!existsSync(statePath)) return undefined; + try { + const value = JSON.parse(readFileSync(statePath, "utf8")) as Partial; + if ( + value.version === CODEX_ROUTE_STATE_VERSION && + value.active === true && + typeof value.providerId === "string" && + typeof value.configPath === "string" && + typeof value.original?.baseUrl === "string" && + typeof value.installed?.baseUrl === "string" && + value.installed.supportsWebsockets === false && + value.originalFields && typeof value.originalFields === "object" && + Array.isArray(value.insertedFields) && + typeof value.sectionPrefixLength === "number" && value.sectionPrefixLength >= 0 + ) return value as CodexRouteState; + } catch { + } + return undefined; +} + +function writeState(statePath: string, state: CodexRouteState): void { + atomicWrite(statePath, JSON.stringify(state, null, 2) + "\n"); +} + +function removeState(statePath: string): void { + try { unlinkSync(statePath); } catch (error) { + if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw error; + } +} + +function installedBaseUrl(port: number): string { + if (!Number.isInteger(port) || port < 1 || port > 65535) throw new Error(`invalid bili port: ${port}`); + return `http://127.0.0.1:${port}/codex`; +} + +function isInstalledBaseUrl(value: string): boolean { + try { + const url = new URL(value); + const host = url.hostname.toLowerCase(); + return url.protocol === "http:" && + (host === "127.0.0.1" || host === "localhost" || host === "::1") && + url.pathname.replace(/\/+$/, "") === "/codex"; + } catch { + return false; + } +} + +function storedField(text: string, providerId: string, key: string): StoredField { + const snapshot = getCodexProviderFieldSnapshot(text, providerId, key); + return snapshot === undefined ? { present: false } : { present: true, ...snapshot }; +} + +function pidAlive(pid: number): boolean { + if (!Number.isInteger(pid) || pid <= 0) return false; + try { + process.kill(pid, 0); + return true; + } catch (error) { + return (error as NodeJS.ErrnoException).code === "EPERM"; + } +} + +export function getCodexRouteState(options: CodexTakeoverPaths = {}): CodexRouteState | undefined { + return parseState(resolvePaths(options).statePath); +} + +export function getCodexTakeoverStatus(options: CodexTakeoverPaths = {}): CodexTakeoverStatus { + const paths = resolvePaths(options); + const state = parseState(paths.statePath); + if (!state) { + try { + return { + state: "disabled", + configPath: paths.configPath, + statePath: paths.statePath, + provider: resolveActiveCodexProvider(paths.configPath), + }; + } catch (error) { + return { + state: "disabled", + configPath: paths.configPath, + statePath: paths.statePath, + detail: String(error), + }; + } + } + if (path.resolve(state.configPath) !== path.resolve(paths.configPath)) { + return { + state: "conflict", + configPath: paths.configPath, + statePath: paths.statePath, + providerId: state.providerId, + detail: `route state belongs to a different Codex config: ${state.configPath}`, + }; + } + const text = readCodexConfig(paths.configPath); + const currentBase = getCodexProviderField(text, state.providerId, "base_url"); + const currentWebsockets = getCodexProviderField(text, state.providerId, "supports_websockets"); + const matches = currentBase === state.installed.baseUrl && currentWebsockets === false; + const port = Number.parseInt(new URL(state.installed.baseUrl).port, 10); + return { + state: matches ? "enabled" : "conflict", + configPath: paths.configPath, + statePath: paths.statePath, + providerId: state.providerId, + port, + baseUrl: state.installed.baseUrl, + originalBaseUrl: state.original.baseUrl, + lastRequestAt: state.lastRequestAt, + lastRequestPath: state.lastRequestPath, + ...(matches ? {} : { detail: "managed Codex route fields changed; restore will preserve user edits" }), + }; +} + +export function enableCodexTakeover(port: number, options: CodexTakeoverPaths = {}): CodexTakeoverStatus { + const paths = resolvePaths(options); + const existing = parseState(paths.statePath); + if (existing) { + const status = getCodexTakeoverStatus(paths); + const otherLiveOwner = existing.pid && existing.pid !== paths.ownerPid && pidAlive(existing.pid); + if (status.state === "enabled" && status.port === port) { + if (otherLiveOwner && paths.ownerPid) { + throw new Error(`Codex route is owned by running bili process ${existing.pid}`); + } + if (paths.ownerPid && existing.pid !== paths.ownerPid) { + existing.pid = paths.ownerPid; + writeState(paths.statePath, existing); + return getCodexTakeoverStatus(paths); + } + return status; + } + if (otherLiveOwner) { + throw new Error(`Codex route is owned by running bili process ${existing.pid}`); + } + disableCodexTakeover(paths); + } + const provider = resolveActiveCodexProvider(paths.configPath); + if (provider.wireApi !== "responses") { + throw new Error(`Codex provider \"${provider.id}\" uses wire_api=${provider.wireApi}; local route requires responses`); + } + if (isInstalledBaseUrl(provider.baseUrl)) { + throw new Error("Codex provider already points at a local /codex route without billion-context ownership state"); + } + const before = readCodexConfig(paths.configPath); + const installedUrl = installedBaseUrl(port); + const managedKeys = ["base_url", "supports_websockets"]; + const originalFields: Record = {}; + for (const key of managedKeys) originalFields[key] = storedField(before, provider.id, key); + let after = before; + const insertedFields: string[] = []; + let sectionAdded = false; + if (!provider.sectionExists) { + sectionAdded = true; + const fields = { + ...INSTALLED_BASE_FIELDS, + base_url: installedUrl, + supports_websockets: false, + }; + after = appendCodexProviderSection(after, provider.id, fields); + insertedFields.push(...Object.keys(fields)); + } else { + const baseResult = setCodexProviderField(after, provider.id, "base_url", installedUrl); + after = baseResult.text; + if (baseResult.added) insertedFields.push("base_url"); + const websocketResult = setCodexProviderField(after, provider.id, "supports_websockets", false); + after = websocketResult.text; + if (websocketResult.added) insertedFields.push("supports_websockets"); + } + const state: CodexRouteState = { + version: CODEX_ROUTE_STATE_VERSION, + active: true, + ...(paths.ownerPid ? { pid: paths.ownerPid } : {}), + providerId: provider.id, + configPath: paths.configPath, + sectionAdded, + sectionPrefixLength: sectionAdded + ? before.length === 0 + ? 0 + : /(?:\r\n|\r|\n)$/.test(before) + ? (before.match(/(\r\n|\r|\n)$/)?.[0].length ?? 1) + : 2 * (before.match(/\r\n|\r|\n/)?.[0].length ?? (process.platform === "win32" ? 2 : 1)) + : 0, + insertedFields, + originalFields, + original: { baseUrl: provider.baseUrl, supportsWebsockets: provider.supportsWebsockets }, + installed: { baseUrl: installedUrl, supportsWebsockets: false }, + configHashBefore: sha256(before), + startedAt: new Date().toISOString(), + }; + writeState(paths.statePath, state); + try { + atomicWrite(paths.configPath, after); + } catch (error) { + removeState(paths.statePath); + throw error; + } + return getCodexTakeoverStatus(paths); +} + +export function disableCodexTakeover(options: CodexTakeoverPaths = {}): CodexTakeoverStatus { + const paths = resolvePaths(options); + const state = parseState(paths.statePath); + if (!state) return getCodexTakeoverStatus(paths); + let text = readCodexConfig(state.configPath); + const preserved: string[] = []; + const installedValues: Record = { + ...INSTALLED_BASE_FIELDS, + base_url: state.installed.baseUrl, + supports_websockets: false, + }; + for (const [key, installedValue] of Object.entries(installedValues)) { + if (!state.sectionAdded && key !== "base_url" && key !== "supports_websockets") continue; + const current = getCodexProviderField(text, state.providerId, key); + if (current !== installedValue) { + if (current !== undefined) preserved.push(key); + continue; + } + const original = state.originalFields[key]; + if (original?.present && original.value !== undefined) { + text = typeof original.encoded === "string" + ? restoreCodexProviderField(text, state.providerId, key, { + value: original.value, + encoded: original.encoded, + }) + : setCodexProviderField(text, state.providerId, key, original.value).text; + } else { + text = removeCodexProviderField(text, state.providerId, key); + } + } + if (state.sectionAdded) { + text = removeCodexProviderSectionWithPrefixIfEmpty(text, state.providerId, state.sectionPrefixLength ?? 0); + } + atomicWrite(state.configPath, text); + removeState(paths.statePath); + const status = getCodexTakeoverStatus({ ...paths, configPath: state.configPath }); + if (preserved.length > 0) status.detail = `检测到用户修改,已保留:${preserved.join(", ")}`; + return status; +} + +export function recoverStaleCodexTakeover(options: CodexTakeoverPaths = {}): CodexTakeoverStatus { + const paths = resolvePaths(options); + const state = parseState(paths.statePath); + if (state?.pid && !pidAlive(state.pid)) return disableCodexTakeover(paths); + return getCodexTakeoverStatus(paths); +} + +export function claimCodexTakeover(ownerPid: number = process.pid, options: CodexTakeoverPaths = {}): CodexTakeoverStatus { + const paths = resolvePaths(options); + const state = parseState(paths.statePath); + if (!state) return getCodexTakeoverStatus(paths); + if (state.pid && state.pid !== ownerPid && pidAlive(state.pid)) { + throw new Error(`Codex route is owned by running bili process ${state.pid}`); + } + state.pid = ownerPid; + writeState(paths.statePath, state); + return getCodexTakeoverStatus(paths); +} + +export function restoreCodexTakeoverOwnedBy(ownerPid: number = process.pid, options: CodexTakeoverPaths = {}): CodexTakeoverStatus { + const paths = resolvePaths(options); + const state = parseState(paths.statePath); + if (!state || state.pid !== ownerPid) return getCodexTakeoverStatus(paths); + return disableCodexTakeover(paths); +} + +export function recordCodexRouteRequest(requestPath: string, options: CodexTakeoverPaths = {}): void { + const paths = resolvePaths(options); + const state = parseState(paths.statePath); + if (!state) return; + state.lastRequestAt = new Date().toISOString(); + state.lastRequestPath = requestPath; + writeState(paths.statePath, state); +} diff --git a/src/compress-loop-responses.ts b/src/compress-loop-responses.ts index 9ebe7e0fc2..85a9a42c40 100644 --- a/src/compress-loop-responses.ts +++ b/src/compress-loop-responses.ts @@ -67,6 +67,7 @@ interface CompressLoopResponsesCtx { log: (msg: string) => void; /** Resolved upstream proxy URL (http://host:port) or undefined for direct. */ proxyUrl?: string; + textProtocol?: boolean; } interface RequestOptions { @@ -149,6 +150,14 @@ interface ResponsesEventDisposition { terminalRaw?: string; responseObj?: Record | null; isMeta?: boolean; + /** Bypass the text-protocol realtime buffer for host tool calls (Codex + * code_mode `custom_tool_call`). They never carry an ACP compress trigger, + * so buffering only drops them — the end-of-turn rebuild re-emits output_text + * only. Required for Codex tools to work in text-protocol mode. */ + noBuffer?: boolean; + /** A `custom_tool_call` output_item finalized this round (for [acp-diag]; + * the function_call-only allCalls would otherwise hide code_mode usage). */ + customToolCallDone?: boolean; } function classifyResponsesSseEvent(eventStr: string): ResponsesEventDisposition { @@ -182,6 +191,7 @@ function classifyResponsesSseEvent(eventStr: string): ResponsesEventDisposition }; return out; } + if (item?.type === "custom_tool_call") out.noBuffer = true; out.yieldChunk = Buffer.from(eventStr + "\n\n", "utf8"); return out; } @@ -210,6 +220,10 @@ function classifyResponsesSseEvent(eventStr: string): ResponsesEventDisposition out.fcDone = { itemId: typeof item.id === "string" ? item.id : "" }; return out; } + if (item?.type === "custom_tool_call") { + out.noBuffer = true; + out.customToolCallDone = true; + } out.yieldChunk = Buffer.from(eventStr + "\n\n", "utf8"); return out; } @@ -238,6 +252,7 @@ function classifyResponsesSseEvent(eventStr: string): ResponsesEventDisposition out.terminalRaw = eventStr; return out; default: + if (type.startsWith("response.custom_tool_call.")) out.noBuffer = true; out.yieldChunk = Buffer.from(eventStr + "\n\n", "utf8"); return out; } @@ -296,12 +311,104 @@ function buildCompleted(responseObj: Record | null): string { })}\n\n`; } +function responsesJsonOutput(response: Record): { + text: string; + textParts: Array>; + calls: FunctionCallAccumulator[]; +} { + const textParts: Array> = []; + const calls: FunctionCallAccumulator[] = []; + for (const item of Array.isArray(response.output) ? response.output : []) { + if (!item || typeof item !== "object") continue; + const value = item as Record; + if (value.type === "message") { + for (const part of Array.isArray(value.content) ? value.content : []) { + if (part && typeof part === "object" && (part as Record).type === "output_text") { + textParts.push(part as Record); + } + } + } else if (value.type === "function_call") { + calls.push({ + itemId: typeof value.id === "string" ? value.id : "", + callId: typeof value.call_id === "string" ? value.call_id : "", + name: typeof value.name === "string" ? value.name : "", + arguments: typeof value.arguments === "string" ? value.arguments : "", + }); + } + } + return { + text: textParts.map((part) => typeof part.text === "string" ? part.text : "").join(""), + textParts, + calls, + }; +} + +function replaceResponsesJsonText(parts: Array>, text: string): void { + parts.forEach((part, index) => { + part.text = index === 0 ? text : ""; + }); +} + +export async function compressLoopResponsesJson( + initialResponse: Record, + ctx: CompressLoopResponsesCtx, + requestBody: Record, + requestOptions: RequestOptions, +): Promise> { + let current = initialResponse; + for (let loopCount = 1; loopCount <= 5; loopCount++) { + const output = responsesJsonOutput(current); + const extracted = extractTextTriggers(output.text); + const allCalls = [...output.calls, ...extracted.calls].filter((call) => call.name.length > 0); + const proxyCalls = allCalls.filter((call) => PROXY_TOOL_NAMES.has(call.name)); + const realCalls = allCalls.filter((call) => !PROXY_TOOL_NAMES.has(call.name)); + if (proxyCalls.length === 0 || realCalls.length > 0) { + if (proxyCalls.length > 0) replaceResponsesJsonText(output.textParts, extracted.clean); + return current; + } + const inputItems = Array.isArray(requestBody.input) ? [...(requestBody.input as unknown[])] : []; + if (extracted.clean.trim()) { + inputItems.push({ type: "message", role: "assistant", content: [{ type: "output_text", text: extracted.clean }] }); + } + for (const call of proxyCalls) { + let args: Record = {}; + try { + args = JSON.parse(call.arguments) as Record; + } catch (error) { + loggerLog("warn", `[acp-compress-args] ${call.name} JSON.parse failed: ${String(error)}`); + } + const result = executeProxyTool(call.name, args, ctx); + ctx.log(`[acp-proxy: responses JSON ${call.name} → ${result.slice(0, 120).replace(/\n/g, " ")}]`); + inputItems.push({ type: "message", role: "user", content: buildVisibilityMarker(call.name, result) }); + } + requestBody.input = inputItems; + const { response, clearTimer } = await fetchWithTimeout(requestOptions.url, { + method: "POST", + headers: requestOptions.headers, + body: JSON.stringify(requestBody), + ...(ctx.proxyUrl ? { dispatcher: proxyDispatcher(ctx.proxyUrl) } : {}), + }); + try { + if (!response.ok) { + const detail = await response.text().catch(() => "upstream error"); + throw new Error(`responses compress loop upstream error ${response.status}: ${detail.slice(0, 200)}`); + } + current = await response.json() as Record; + } finally { + clearTimer(); + } + } + ctx.log("[acp-proxy: responses JSON compress loop limit (5) reached]"); + return current; +} + export async function* compressLoopResponsesStream( initialUpstream: ReadableStream, ctx: CompressLoopResponsesCtx, requestBody: Record, requestOptions: RequestOptions, ): AsyncGenerator { + const textProtocol = ctx.textProtocol ?? TEXT_PROTOCOL; let upstream = initialUpstream; let loopCount = 0; let responseObj: Record | null = null; @@ -320,6 +427,7 @@ export async function* compressLoopResponsesStream( const fcByItemId = new Map(); let contentText = ""; + let customToolCalls = 0; let completed = false; let terminalKind: "completed" | "incomplete" | "failed" | "error" | null = null; let terminalRaw: string | null = null; @@ -352,7 +460,7 @@ export async function* compressLoopResponsesStream( // strip the trigger before the client sees it; // we re-emit a clean message item at round end. Meta-start // events still open the stream in round 1. - if (d.yieldChunk && (isFirstRound || !d.isMeta) && !(TEXT_PROTOCOL && !d.isMeta)) { + if (d.yieldChunk && (isFirstRound || !d.isMeta) && !(textProtocol && !d.isMeta && !d.noBuffer)) { yield d.yieldChunk; } if (d.contentDelta) contentText += d.contentDelta; @@ -376,6 +484,7 @@ export async function* compressLoopResponsesStream( existing.arguments = args; } } + if (d.customToolCallDone) customToolCalls++; if (d.terminal) { completed = true; terminalKind = d.terminalKind ?? null; @@ -416,7 +525,7 @@ export async function* compressLoopResponsesStream( // text and treat them as proxy compress calls so the loop executes // them and continues. The cleaned text replaces contentText so the // client never sees the raw trigger. - if (TEXT_PROTOCOL) { + if (textProtocol) { const extracted = extractTextTriggers(contentText); contentText = extracted.clean; for (const c of extracted.calls) { @@ -432,7 +541,7 @@ export async function* compressLoopResponsesStream( const proxyCalls = allCalls.filter((c) => PROXY_TOOL_NAMES.has(c.name)); const realCalls = allCalls.filter((c) => !PROXY_TOOL_NAMES.has(c.name)); // DIAG: log what tools the upstream returned this round. - loggerLog("debug", `[acp-diag] round ${loopCount} allCalls=[${allCalls.map((c) => c.name).join(",")}] realCalls=[${realCalls.map((c) => c.name).join(",")}] text=${JSON.stringify(contentText.slice(0, 120))}`); + loggerLog("debug", `[acp-diag] round ${loopCount} allCalls=[${allCalls.map((c) => c.name).join(",")}] realCalls=[${realCalls.map((c) => c.name).join(",")}] customToolCalls=${customToolCalls} text=${JSON.stringify(contentText.slice(0, 120))}`); const hasOnlyProxy = proxyCalls.length > 0 && realCalls.length === 0; if (!hasOnlyProxy) { @@ -467,14 +576,16 @@ export async function* compressLoopResponsesStream( content: [{ type: "output_text", text: contentText }], }); } - for (const fc of proxyCalls) { - inputItems.push({ - type: "function_call", - id: fc.itemId || `fc_${Date.now()}`, - call_id: fc.callId || `call_${Date.now()}`, - name: fc.name, - arguments: fc.arguments, - }); + if (!textProtocol) { + for (const fc of proxyCalls) { + inputItems.push({ + type: "function_call", + id: fc.itemId || `fc_${Date.now()}`, + call_id: fc.callId || `call_${Date.now()}`, + name: fc.name, + arguments: fc.arguments, + }); + } } for (const fc of proxyCalls) { let args: Record = {}; @@ -495,11 +606,9 @@ export async function* compressLoopResponsesStream( buildMessageItemSequence(markerItemId, nextOutputIndex++, buildVisibilityMarker(fc.name, result)), "utf8", ); - inputItems.push({ - type: "function_call_output", - call_id: fc.callId || `call_${Date.now()}`, - output: result, - }); + inputItems.push(textProtocol + ? { type: "message", role: "user", content: buildVisibilityMarker(fc.name, result) } + : { type: "function_call_output", call_id: fc.callId || `call_${Date.now()}`, output: result }); } requestBody.input = inputItems; diff --git a/src/config.ts b/src/config.ts index 4c8c464e00..bd70e0fcfe 100644 --- a/src/config.ts +++ b/src/config.ts @@ -3,6 +3,7 @@ import { readFileSync, existsSync, mkdirSync, writeFileSync } from "node:fs"; import { dirname } from "node:path"; import { configFile } from "./paths.js"; import { log as loggerLog } from "./logger.js"; +import { validateHttpProxy, type ProxyFallbackOptions } from "./upstream-proxy.js"; export function safeReadJson(path: string): unknown { try { @@ -22,20 +23,12 @@ export function safeReadJson(path: string): unknown { } } -/** Provider route: a short name (e.g. "glm") mapped to an upstream URL. - * Agents point their base URL at the proxy using the name as a path segment: - * http://localhost:8788/v1/glm → routes.glm - * http://localhost:8788/anthropic → routes.anthropic - * The API key is NOT stored here — the proxy passes the agent's key through - * untouched, so secrets only live in the agent's config. - * - * Each route may declare per-model context/output limits, mirroring the +/** Each upstream URL may declare per-model context/output limits, mirroring the * structure agents like opencode carry in their own model registry. This is * the source of truth for the proxy: the LLM `/models` endpoint does NOT * return context windows (verified across OpenAI/Anthropic/zhipu/comfly), * so the proxy cannot discover them at runtime — the user must declare them. - * - * Backward compatible: a bare string is accepted and treated as { url }. */ + */ export type ProviderRoute = { models?: Record; /** Per-URL upstream HTTP proxy. Overrides the global `proxy`. Empty string @@ -44,6 +37,8 @@ export type ProviderRoute = { proxy?: string; }; export type ProviderRoutes = Record; // key = upstream URL prefix (the /bili/ string) +export type PromptCacheRouting = "auto" | "enabled" | "disabled"; +export type UpstreamProxyMode = "auto" | "manual" | "direct"; /** Built-in context window for common model families, keyed by a lowercase * prefix. This is a FALLBACK used when the per-route model declaration in @@ -90,7 +85,15 @@ export function resolveContextLimit( upstreamUrl: string | undefined, model: string | undefined, ): number | undefined { - if (!model || !upstreamUrl) return lookupContextLimit(model); + return resolveConfiguredContextLimit(routes, upstreamUrl, model) ?? lookupContextLimit(model); +} + +export function resolveConfiguredContextLimit( + routes: ProviderRoutes, + upstreamUrl: string | undefined, + model: string | undefined, +): number | undefined { + if (!model || !upstreamUrl) return undefined; // Longest-prefix match: collect all keys that are a prefix of upstreamUrl, // pick the longest (most specific). A key matches if upstreamUrl === key // OR upstreamUrl starts with key + ("/" or key being the full origin). This @@ -106,7 +109,7 @@ export function resolveContextLimit( const m = routes[bestKey].models?.[model]; if (m?.context && m.context > 0) return m.context; } - return lookupContextLimit(model); + return undefined; } export type ProxyOptions = { @@ -115,14 +118,18 @@ export type ProxyOptions = { upstream: string; routes: ProviderRoutes; /** Global default upstream HTTP proxy. Per-URL `proxy` overrides this. - * Empty string disables. `http://host:port` format. */ + * Empty string explicitly disables environment/system proxy fallback. */ proxy?: string; + proxyMode?: UpstreamProxyMode; + proxySource?: "bili-env" | "web-manual" | "config" | "auto" | "direct"; + proxyFallback?: ProxyFallbackOptions; modelContextLimit: number; kernelConfig: Config; compress: { injectTool: boolean; injectNudge: boolean; }; + promptCache: { routing: PromptCacheRouting }; sessionHeader: string; log: boolean; debug: boolean; @@ -153,6 +160,7 @@ export function loadRoutes(env: NodeJS.ProcessEnv = process.env): ProviderRoutes const parsed = safeReadJson(routesPath); if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { for (const [k, v] of Object.entries(parsed as Record)) { + rejectLegacyRoute(k, v); const route = parseRouteEntry(v); if (route) routes[normalizeUrlKey(k)] = route; } @@ -160,6 +168,7 @@ export function loadRoutes(env: NodeJS.ProcessEnv = process.env): ProviderRoutes } if (fileConfig.providers) { for (const [k, v] of Object.entries(fileConfig.providers)) { + rejectLegacyRoute(k, v); const route = parseRouteEntry(v); if (route && !routes[normalizeUrlKey(k)]) routes[normalizeUrlKey(k)] = route; } @@ -184,6 +193,7 @@ export function loadOptions(env: NodeJS.ProcessEnv = process.env): ProxyOptions const parsed = safeReadJson(routesPath); if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { for (const [k, v] of Object.entries(parsed as Record)) { + rejectLegacyRoute(k, v); const route = parseRouteEntry(v); if (route) routes[normalizeUrlKey(k)] = route; } @@ -192,23 +202,66 @@ export function loadOptions(env: NodeJS.ProcessEnv = process.env): ProxyOptions // Also accept providers inline in the config file. if (fileConfig.providers) { for (const [k, v] of Object.entries(fileConfig.providers)) { + rejectLegacyRoute(k, v); const route = parseRouteEntry(v); if (route && !routes[normalizeUrlKey(k)]) routes[normalizeUrlKey(k)] = route; } } const modelContextLimit = parseInt(env.ACP_MODEL_CONTEXT_LIMIT ?? `${fileConfig.modelContextLimit ?? 200000}`, 10); + const biliProxy = nonEmpty(env.BILI_UPSTREAM_PROXY); + const webProxy = nonEmpty(fileConfig.upstreamProxy); + const configProxy = nonEmpty(fileConfig.proxy); + const proxyMode = parseUpstreamProxyMode( + env.BILI_UPSTREAM_PROXY_MODE ?? fileConfig.upstreamProxyMode ?? (webProxy ? "manual" : undefined), + ); + const proxy = biliProxy ?? (proxyMode === "direct" ? "" : proxyMode === "manual" ? webProxy ?? configProxy : configProxy); + const proxySource: ProxyOptions["proxySource"] = biliProxy + ? "bili-env" + : proxyMode === "direct" + ? "direct" + : proxyMode === "manual" && webProxy + ? "web-manual" + : configProxy + ? "config" + : "auto"; + const httpProxy = nonEmpty(env.HTTP_PROXY ?? env.http_proxy); + const httpsProxy = nonEmpty(env.HTTPS_PROXY ?? env.https_proxy); + const allProxy = nonEmpty(env.ALL_PROXY ?? env.all_proxy); + const noProxy = nonEmpty(env.NO_PROXY ?? env.no_proxy); + const proxyFallback: ProxyFallbackOptions = { + ...(httpProxy ? { httpProxy } : {}), + ...(httpsProxy ? { httpsProxy } : {}), + ...(allProxy ? { allProxy } : {}), + ...(noProxy ? { noProxy } : {}), + biliPort: Number.isFinite(port) ? port : 8787, + globalSource: proxySource, + }; + validateHttpProxy(proxy, proxyFallback.biliPort); + for (const [url, route] of Object.entries(routes)) { + try { + validateHttpProxy(route.proxy, proxyFallback.biliPort); + } catch (error) { + throw new Error(`[acp-config] invalid upstream proxy for ${url}: ${String(error)}`); + } + } return { port: Number.isFinite(port) ? port : 8787, host, upstream, routes, - proxy: env.BILI_UPSTREAM_PROXY ?? fileConfig.proxy, + proxy, + proxyMode, + proxySource, + proxyFallback, modelContextLimit, kernelConfig: defaultConfig(modelContextLimit), compress: { injectTool: (env.ACP_COMPRESS_TOOL ?? (fileConfig.compress?.injectTool === false ? "0" : "1")) !== "0", injectNudge: (env.ACP_COMPRESS_NUDGE ?? (fileConfig.compress?.injectNudge === false ? "0" : "1")) !== "0", }, + promptCache: { + routing: parsePromptCacheRouting(env.ACP_PROMPT_CACHE_ROUTING ?? fileConfig.promptCache?.routing), + }, sessionHeader: env.ACP_SESSION_HEADER ?? fileConfig.sessionHeader ?? "x-acp-session", log: env.ACP_LOG !== "0" && fileConfig.log !== false, debug: (env.ACP_DEBUG ?? (fileConfig.debug ? "1" : "0")) === "1", @@ -243,11 +296,19 @@ type FileConfig = { dumpSse?: string; passthrough?: boolean; autoUpdate?: boolean; + upstreamProxy?: string; + upstreamProxyMode?: string; logFile?: string; compress?: { injectTool?: boolean; injectNudge?: boolean }; + promptCache?: { routing?: string }; mitm?: { enabled?: boolean; domains?: string[] }; }; +function nonEmpty(value: string | undefined): string | undefined { + const trimmed = value?.trim(); + return trimmed ? trimmed : undefined; +} + function loadConfigFile(): FileConfig { const parsed = safeReadJson(configFile()); if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { @@ -304,3 +365,19 @@ export function parseRouteEntry(v: unknown): ProviderRoute | undefined { if (v === null) return {}; return undefined; } + +export function parsePromptCacheRouting(value: string | undefined): PromptCacheRouting { + return value === "enabled" || value === "disabled" ? value : "auto"; +} + +export function parseUpstreamProxyMode(value: string | undefined): UpstreamProxyMode { + return value === "manual" || value === "auto" ? value : "direct"; +} + +function rejectLegacyRoute(key: string, value: unknown): void { + if (typeof value !== "string") return; + throw new Error( + `[acp-config] legacy provider route \"${key}\": \"${value}\" is no longer valid; ` + + `use the upstream URL as the key, for example { \"${value.replace(/\/+$/, "")}\": {} }`, + ); +} diff --git a/src/content-encoding.ts b/src/content-encoding.ts new file mode 100644 index 0000000000..68e4f1a6fe --- /dev/null +++ b/src/content-encoding.ts @@ -0,0 +1,55 @@ +import { promisify } from "node:util"; +import { brotliDecompress, gunzip, inflate, inflateRaw } from "node:zlib"; +import { Decompress as ZstdDecompress } from "fzstd"; + +const gunzipAsync = promisify(gunzip); +const inflateAsync = promisify(inflate); +const inflateRawAsync = promisify(inflateRaw); +const brotliAsync = promisify(brotliDecompress); + +async function decodeZstd(input: Buffer, maxOutputBytes: number): Promise { + const chunks: Buffer[] = []; + let size = 0; + const decoder = new ZstdDecompress((chunk) => { + size += chunk.byteLength; + if (size > maxOutputBytes) throw new Error(`decompressed request exceeds ${maxOutputBytes} bytes`); + chunks.push(Buffer.from(chunk)); + }); + decoder.push(input, true); + return Buffer.concat(chunks, size); +} + +async function decodeOne(coding: string, input: Buffer, maxOutputBytes: number): Promise { + const options = { maxOutputLength: maxOutputBytes }; + if (coding === "gzip" || coding === "x-gzip") return gunzipAsync(input, options); + if (coding === "br") return brotliAsync(input, options); + if (coding === "zstd") return decodeZstd(input, maxOutputBytes); + if (coding === "deflate") { + try { + return await inflateAsync(input, options); + } catch { + return inflateRawAsync(input, options); + } + } + throw new Error(`unsupported request content-encoding: ${coding}`); +} + +export async function decodeRequestBody( + contentEncoding: string | undefined, + input: Buffer, + maxOutputBytes: number, +): Promise<{ body: Buffer; decoded: boolean }> { + const codings = (contentEncoding ?? "") + .split(",") + .map((coding) => coding.trim().toLowerCase()) + .filter((coding) => coding && coding !== "identity"); + if (codings.length === 0) return { body: input, decoded: false }; + let body = input; + for (const coding of codings.reverse()) { + body = await decodeOne(coding, body, maxOutputBytes); + if (body.byteLength > maxOutputBytes) { + throw new Error(`decompressed request exceeds ${maxOutputBytes} bytes`); + } + } + return { body, decoded: true }; +} diff --git a/src/registry.ts b/src/registry.ts index 34fc79aab6..7494db5f81 100644 --- a/src/registry.ts +++ b/src/registry.ts @@ -144,3 +144,8 @@ export function _resetForTest(): void { cache = null; loading = null; } + +export function _setForTest(data: RegistryShape): void { + cache = data; + loading = null; +} diff --git a/src/responses.ts b/src/responses.ts index 70776955b9..ca885f6017 100644 --- a/src/responses.ts +++ b/src/responses.ts @@ -1,30 +1,20 @@ import type { CoreMessage } from "acp-kernel"; -import { hashId } from "./util.js"; import { ClusterCounter, deriveMessageId } from "./message-id.js"; +import type { ConversationIdentity } from "./session-id.js"; +import { hashId } from "./util.js"; import { parseDataUrl, type BiliMessage } from "./bili-message.js"; -/** - * OpenAI Responses API protocol shapes. - * - * The Responses API replaces the flat `messages` array with `input` — an - * ordered list of typed items (message / function_call / function_call_output - * / computer_call / ...). Each item carries an explicit `type` discriminator, - * which actually maps closer to core messages than OpenAI chat completions - * (where assistant text + tool_calls are fused into one message). - * - * See https://platform.openai.com/docs/api-reference/responses - */ - export type ResponseContentPart = - | { type: "input_text"; text: string } - | { type: "output_text"; text: string } - | { type: "input_image"; image_url: string } - | { type: string; [k: string]: unknown }; + | { type: "input_text"; text: string; [key: string]: unknown } + | { type: "output_text"; text: string; [key: string]: unknown } + | { type: "input_image"; image_url: string; [key: string]: unknown } + | { type: string; [key: string]: unknown }; export type ResponseInputMessage = { type: "message"; role: "system" | "developer" | "user" | "assistant"; content: string | ResponseContentPart[]; + [key: string]: unknown; }; export type ResponseFunctionCall = { @@ -33,19 +23,21 @@ export type ResponseFunctionCall = { call_id: string; name: string; arguments: string; + [key: string]: unknown; }; export type ResponseFunctionCallOutput = { type: "function_call_output"; call_id: string; output: string; + [key: string]: unknown; }; export type ResponseInputItem = | ResponseInputMessage | ResponseFunctionCall | ResponseFunctionCallOutput - | { type: string; [k: string]: unknown }; + | { type: string; [key: string]: unknown }; export type ResponsesRequestBody = { model?: string; @@ -53,23 +45,25 @@ export type ResponsesRequestBody = { instructions?: string; tools?: unknown[]; stream?: boolean; - /** Codex 0.147+ sends a per-conversation UUID here. This is the most - * explicit conversation identifier any client sends — prefer it over - * previous_response_id and content hashing. */ session_id?: string; previous_response_id?: string; + prompt_cache_key?: string; + metadata?: Record; [key: string]: unknown; }; -type Flat = { +type ResponseLayoutSlot = { + original: ResponseInputItem; + coreId?: string; +}; + +export type ResponsesProjection = { msgs: BiliMessage[]; systemParts: string[]; preamble: ResponseInputItem[]; - /** call_ids that arrived as custom_tool_call / custom_tool_call_output. - * Used by coreToResponses to emit the correct item type on the way back - * out — a standard `function_call` must NOT be rewritten as - * `custom_tool_call` (different Responses API semantics). */ customToolCallIds: Set; + layout: ResponseLayoutSlot[]; + stringInput?: { original: string; coreId: string }; /** Reasoning items dropped because ACP_REASONING_KEEP=none. 0 by default — * reasoning is normally routed through the compression pipeline so it is * hidden automatically once its turn is summarized. */ @@ -79,366 +73,323 @@ type Flat = { /** Item types that are host DIRECTIVES (tool/definition listings), not * conversation history: preserved verbatim and re-prepended at input[0..]. * `additional_tools` carries the Codex code_mode exec/wait tool definitions - * and MUST stay at input[0] (see openai/codex client.rs splice(0..0, prefix)). - * `mcp_list_tools` is a stable per-session tool listing. + * and MUST stay at input[0]. `mcp_list_tools` is a stable per-session listing. * * Only definitions belong here. Output/action items from a prior response * (reasoning, computer_call, function_call, mcp_call, ...) ARE conversation - * history and are routed as tracked BiliMessages via pushVerbatimItem / - * the message/function cases, so the compression pipeline hides them once - * their turn is summarized — preserving them verbatim in the preamble instead - * made them accumulate unbounded every turn and broke Codex's prompt-cache - * prefix. */ + * history and are routed as tracked BiliMessages, so the compression pipeline + * hides them once their turn is summarized — preserving them verbatim in the + * preamble instead made them accumulate unbounded every turn and broke Codex's + * prompt-cache prefix. */ const OPAQUE_ITEM_TYPES = new Set([ "additional_tools", "mcp_list_tools", ]); -function isOpaqueItem(it: ResponseInputItem): boolean { - return OPAQUE_ITEM_TYPES.has(it.type); +function isOpaqueItem(item: ResponseInputItem): boolean { + return OPAQUE_ITEM_TYPES.has(item.type); } -/** Escape hatch: when ACP_REASONING_KEEP=none, drop reasoning items entirely - * instead of routing them through compression. Default (any other value) - * keeps reasoning until its turn is summarized, preserving chain-of-thought - * continuity for the responses that are still live. */ function shouldDropAllReasoning(): boolean { return (process.env.ACP_REASONING_KEEP ?? "").trim().toLowerCase() === "none"; } -/** Route an opaque Responses output item — reasoning, or a tool-call-shaped - * action item (computer_call / file_search_call / mcp_call / ...) — through - * the compression pipeline so it is hidden once its turn is summarized. The - * item's own structure is opaque to us, so we carry it verbatim in - * rawResponsesItem and re-emit it unchanged via coreToResponses while the turn - * is still live. `kind` only namespaces the derived id (so reasoning and call - * items never collide). contentType "reasoning" is the only safe bucket: the - * kernel filters/pairs tool-call & tool-result by toolCallId and injects ACP - * tags into text, while reasoning items pass through untouched (no tag, no - * filtering) and are still covered by compression blocks. */ -function pushVerbatimItem( - msgs: BiliMessage[], - clusters: ClusterCounter, - it: ResponseInputItem, - kind: string, -): void { - const rawId = - typeof (it as { id?: unknown }).id === "string" - ? String((it as { id?: string }).id) - : hashId(JSON.stringify(it)); - const base = deriveMessageId("assistant", kind, rawId); - msgs.push({ - id: clusters.next(base), - role: "assistant", - contentType: "reasoning", - text: rawId, - rawResponsesItem: it, - }); -} - -function partText(p: ResponseContentPart): string { - if (p.type === "input_text" || p.type === "output_text") { - const t = (p as { text?: string }).text; - return typeof t === "string" ? t : ""; +function partText(part: ResponseContentPart): string { + if (part.type === "input_text" || part.type === "output_text") { + return typeof part.text === "string" ? part.text : ""; } return ""; } -function messageContent(c: string | ResponseContentPart[]): string { - if (typeof c === "string") return c; - if (Array.isArray(c)) return c.map(partText).join("\n"); - return ""; +function messageContent(content: string | ResponseContentPart[]): string { + return typeof content === "string" ? content : content.map(partText).join("\n"); } -function findInputImage(c: string | ResponseContentPart[]): { mediaType?: string; base64?: string } | undefined { - if (!Array.isArray(c)) return undefined; - for (const p of c) { - if (p && typeof p === "object" && p.type === "input_image") { - const url = (p as { image_url?: string }).image_url; - if (typeof url === "string") { - const parsed = parseDataUrl(url); - if (parsed) return { mediaType: parsed.mediaType, base64: parsed.base64 }; - } - return {}; - } - } - return undefined; -} - -export function responsesToCore(body: ResponsesRequestBody): Flat { +export function responsesToCore(body: ResponsesRequestBody): ResponsesProjection { const msgs: BiliMessage[] = []; const systemParts: string[] = []; const preamble: ResponseInputItem[] = []; const customToolCallIds = new Set(); + const layout: ResponseLayoutSlot[] = []; let droppedReasoning = 0; - if (typeof body.instructions === "string" && body.instructions.trim()) { - systemParts.push(body.instructions); - } - let idx = 0; const clusters = new ClusterCounter(); - const items = Array.isArray(body.input) ? body.input : []; + let idx = 0; + if (typeof body.instructions === "string" && body.instructions.trim()) systemParts.push(body.instructions); if (typeof body.input === "string") { - const base = deriveMessageId("user", "text", body.input); - msgs.push({ id: clusters.next(base), role: "user", contentType: "text", text: body.input }); - idx++; - return { msgs, systemParts, preamble, customToolCallIds, droppedReasoning }; + const id = clusters.next(deriveMessageId("user", "text", body.input)); + msgs.push({ id, role: "user", contentType: "text", text: body.input }); + return { msgs, systemParts, preamble, customToolCallIds, layout, droppedReasoning, stringInput: { original: body.input, coreId: id } }; } - for (const it of items) { - // Preserve opaque host-directive items verbatim. They are never - // conversation history: capture them so the caller re-prepends them - // unchanged (additional_tools MUST stay at input[0]). - if (isOpaqueItem(it)) { - preamble.push(it); - continue; - } - switch (it.type) { + for (const item of body.input) { + let coreId: string | undefined; + if (isOpaqueItem(item)) preamble.push(item); + switch (item.type) { case "reasoning": { - // shouldDropAllReasoning() is checked ONLY here, not in the - // call-item cases below: dropping chain-of-thought is safe, but - // a computer_call/mcp_call is replay-critical. Call items reuse - // "reasoning" only as a compression-safe contentType bucket. if (shouldDropAllReasoning()) { droppedReasoning++; - break; + continue; } - pushVerbatimItem(msgs, clusters, it, "reasoning"); - idx++; - break; - } - // Prior-response action items whose structure is opaque to us. - // Routed through compression (same mechanism as reasoning) instead - // of being pinned in the preamble, which made them pile up every - // turn and break the prompt-cache prefix. Re-emitted verbatim via - // rawResponsesItem while their turn is live. - case "computer_call": - case "computer_call_output": - case "file_search_call": - case "web_search_call": - case "image_generation_call": - case "code_interpreter_call": - case "mcp_call": { - pushVerbatimItem(msgs, clusters, it, "responses-call"); - idx++; + const rid = + typeof (item as { id?: unknown }).id === "string" + ? String((item as { id?: string }).id) + : hashId(JSON.stringify(item)); + coreId = clusters.next(deriveMessageId("assistant", "reasoning", rid)); + msgs.push({ + id: coreId, + role: "assistant", + contentType: "reasoning", + text: rid, + rawResponsesItem: item, + }); break; } case "message": { - const m = it as ResponseInputMessage; - const text = messageContent(m.content); - if (m.role === "system" || m.role === "developer") { + const message = item as ResponseInputMessage; + const text = messageContent(message.content); + if (message.role === "system" || message.role === "developer") { systemParts.push(text); - } else if (m.role === "user") { - const img = findInputImage(m.content); - const base = deriveMessageId("user", "text", text); + idx++; + continue; + } else if (message.role === "user" || (message.role === "assistant" && text)) { + const role = message.role; + coreId = clusters.next(deriveMessageId(role, "text", text)); + const imageUrl = Array.isArray(message.content) + ? message.content.find((part) => part.type === "input_image" && typeof part.image_url === "string")?.image_url + : undefined; + const image = typeof imageUrl === "string" ? parseDataUrl(imageUrl) : undefined; msgs.push({ - id: clusters.next(base), - role: "user", + id: coreId, + role, contentType: "text", text, - ...(img - ? { - rawResponsesItem: it, - ...(img.mediaType && img.base64 - ? { imageMediaType: img.mediaType, imageBase64: img.base64 } - : {}), - } - : {}), + rawResponsesItem: item, + ...(image ? { imageMediaType: image.mediaType, imageBase64: image.base64 } : {}), }); - idx++; - } else if (m.role === "assistant") { - if (text) { - const base = deriveMessageId("assistant", "text", text); - msgs.push({ id: clusters.next(base), role: "assistant", contentType: "text", text }); - idx++; - } } break; } case "function_call": { - const fc = it as ResponseFunctionCall; - const base = deriveMessageId("assistant", "tool-call", fc.arguments ?? "", { - toolCallId: fc.call_id, - toolName: fc.name, - }); + const call = item as ResponseFunctionCall; + coreId = clusters.next(deriveMessageId("assistant", "tool-call", call.arguments ?? "", { + toolCallId: call.call_id, + toolName: call.name, + })); msgs.push({ - id: clusters.next(base), + id: coreId, role: "assistant", contentType: "tool-call", - toolName: fc.name, - toolCallId: fc.call_id, - text: fc.arguments ?? "", + toolName: call.name, + toolCallId: call.call_id, + text: call.arguments ?? "", + rawResponsesItem: item, }); - idx++; break; } case "function_call_output": { - const fco = it as ResponseFunctionCallOutput; - const outText = typeof fco.output === "string" ? fco.output : JSON.stringify(fco.output); - const base = deriveMessageId("tool", "tool-result", outText, { toolCallId: fco.call_id }); - msgs.push({ - id: clusters.next(base), - role: "tool", - contentType: "tool-result", - toolCallId: fco.call_id, - text: outText, - }); - idx++; + const output = item as ResponseFunctionCallOutput; + const text = typeof output.output === "string" ? output.output : JSON.stringify(output.output); + coreId = clusters.next(deriveMessageId("tool", "tool-result", text, { toolCallId: output.call_id })); + msgs.push({ id: coreId, role: "tool", contentType: "tool-result", toolCallId: output.call_id, text, rawResponsesItem: item }); + break; + } + case "computer_call": + case "computer_call_output": + case "file_search_call": + case "web_search_call": + case "image_generation_call": + case "code_interpreter_call": + case "mcp_call": { + const rid = + typeof (item as { id?: unknown }).id === "string" + ? String((item as { id?: string }).id) + : hashId(JSON.stringify(item)); + coreId = clusters.next(deriveMessageId("assistant", "responses-call", rid)); + msgs.push({ id: coreId, role: "assistant", contentType: "reasoning", text: rid, rawResponsesItem: item }); break; } case "custom_tool_call": { - const ctc = it as { call_id?: string; name?: string; input?: string; arguments?: string }; + const ctc = item as { call_id?: string; name?: string; input?: string; arguments?: string }; const callId = ctc.call_id ?? `call_${idx}`; customToolCallIds.add(callId); const argText = ctc.input ?? ctc.arguments ?? ""; - const base = deriveMessageId("assistant", "tool-call", argText, { - toolCallId: callId, - toolName: ctc.name ?? "custom", - }); - msgs.push({ - id: clusters.next(base), - role: "assistant", - contentType: "tool-call", - toolName: ctc.name ?? "custom", - toolCallId: callId, - text: argText, - }); - idx++; + coreId = clusters.next(deriveMessageId("assistant", "tool-call", argText, { toolCallId: callId, toolName: ctc.name ?? "custom" })); + msgs.push({ id: coreId, role: "assistant", contentType: "tool-call", toolName: ctc.name ?? "custom", toolCallId: callId, text: argText, rawResponsesItem: item }); break; } case "custom_tool_call_output": { - const ctco = it as { call_id?: string; output?: string }; + const ctco = item as { call_id?: string; output?: string }; const callId = ctco.call_id ?? `call_${idx}`; customToolCallIds.add(callId); const outText = typeof ctco.output === "string" ? ctco.output : JSON.stringify(ctco.output ?? ""); - const base = deriveMessageId("tool", "tool-result", outText, { toolCallId: callId }); - msgs.push({ - id: clusters.next(base), - role: "tool", - contentType: "tool-result", - toolCallId: callId, - text: outText, - }); - idx++; + coreId = clusters.next(deriveMessageId("tool", "tool-result", outText, { toolCallId: callId })); + msgs.push({ id: coreId, role: "tool", contentType: "tool-result", toolCallId: callId, text: outText, rawResponsesItem: item }); break; } default: - // Unknown / future item type (forward-compat): preserve verbatim - // rather than drop. Placed in preamble so it stays in the - // request uncompressed. Reordering is acceptable vs data loss. - preamble.push(it); + if (!isOpaqueItem(item)) preamble.push(item); break; } + layout.push({ original: item, coreId }); + idx++; } - return { msgs, systemParts, preamble, customToolCallIds, droppedReasoning }; + return { msgs, systemParts, preamble, customToolCallIds, layout, droppedReasoning }; +} + +function patchTextParts(parts: ResponseContentPart[], text: string): ResponseContentPart[] { + const textIndexes = parts.flatMap((part, index) => + part.type === "input_text" || part.type === "output_text" ? [index] : [], + ); + if (textIndexes.length === 0) return [{ type: "input_text", text }, ...parts]; + const first = textIndexes[0]; + const remaining = new Set(textIndexes.slice(1)); + return parts.map((part, index) => { + if (index === first) return { ...part, text }; + if (remaining.has(index)) return { ...part, text: "" }; + return part; + }); +} + +function patchOriginalItem(original: ResponseInputItem, source: CoreMessage, next: CoreMessage): ResponseInputItem { + if ( + source.text === next.text && + source.toolName === next.toolName && + source.toolCallId === next.toolCallId && + source.role === next.role && + source.contentType === next.contentType + ) return original; + if (original.type === "message") { + const message = original as ResponseInputMessage; + const content = typeof message.content === "string" ? next.text ?? "" : patchTextParts(message.content, next.text ?? ""); + return { ...message, content }; + } + if (original.type === "function_call") { + return { + ...original, + name: next.toolName ?? String(original.name ?? "unknown"), + call_id: next.toolCallId ?? String(original.call_id ?? ""), + arguments: next.text ?? "", + }; + } + if (original.type === "function_call_output") { + return { ...original, call_id: next.toolCallId ?? String(original.call_id ?? ""), output: next.text ?? "" }; + } + return original; +} + +export function patchResponsesInput(projection: ResponsesProjection, messages: CoreMessage[]): string | ResponseInputItem[] { + if (projection.stringInput) { + const original = projection.msgs.find((message) => message.id === projection.stringInput?.coreId); + const next = messages.find((message) => message.id === projection.stringInput?.coreId); + if (original && next && messages.length === 1 && next.role === "user" && next.contentType === "text") { + return next.text === original.text ? projection.stringInput.original : next.text ?? ""; + } + return coreToResponses(messages, projection.customToolCallIds); + } + const sourceById = new Map(projection.msgs.map((message) => [message.id, message])); + const nextById = new Map(messages.map((message) => [message.id, message])); + const slotById = new Map(); + projection.layout.forEach((slot, index) => { + if (slot.coreId) slotById.set(slot.coreId, index); + }); + const insertions = new Map(); + for (let index = 0; index < messages.length; index++) { + const message = messages[index]; + if (sourceById.has(message.id)) continue; + let target = projection.layout.length; + for (let nextIndex = index + 1; nextIndex < messages.length; nextIndex++) { + const slot = slotById.get(messages[nextIndex].id); + if (slot !== undefined) { + target = slot; + break; + } + } + const generated = coreToResponses([message], projection.customToolCallIds); + if (generated.length > 0) insertions.set(target, [...(insertions.get(target) ?? []), ...generated]); + } + const out: ResponseInputItem[] = []; + projection.layout.forEach((slot, index) => { + out.push(...(insertions.get(index) ?? [])); + if (!slot.coreId) { + out.push(slot.original); + return; + } + const source = sourceById.get(slot.coreId); + const next = nextById.get(slot.coreId); + if (source && next) out.push(patchOriginalItem(slot.original, source, next)); + }); + out.push(...(insertions.get(projection.layout.length) ?? [])); + return out; } export function coreToResponses( - messages: BiliMessage[], + messages: CoreMessage[], customToolCallIds: Set = new Set(), ): ResponseInputItem[] { const out: ResponseInputItem[] = []; - for (const m of messages) { - if (m.role === "system") { - out.push({ type: "message", role: "developer", content: m.text ?? "" }); - } else if (m.role === "user") { - if (m.rawResponsesItem) { - out.push(m.rawResponsesItem as ResponseInputItem); - } else { - out.push({ type: "message", role: "user", content: m.text ?? "" }); - } - } else if (m.role === "assistant") { - if (m.contentType === "text") { - out.push({ type: "message", role: "assistant", content: m.text ?? "" }); - } else if (m.contentType === "tool-call") { - // Preserve the ORIGINAL item type by call_id membership: a host's - // standard function tool (e.g. get_weather) must stay - // function_call, not be rewritten as custom_tool_call (which - // changes Responses API semantics and may be rejected). - const callId = m.toolCallId ?? `call_${m.id}`; + for (const message of messages) { + const biliMessage = message as BiliMessage; + const raw = biliMessage.rawResponsesItem as ResponseInputItem | undefined; + if (message.role === "system") { + out.push({ type: "message", role: "developer", content: message.text ?? "" }); + } else if (message.role === "user") { + if (raw?.type === "message" && messageContent((raw as ResponseInputMessage).content) === (message.text ?? "")) out.push(raw); + else out.push({ type: "message", role: "user", content: message.text ?? "" }); + } else if (message.role === "assistant") { + if (message.contentType === "text") { + out.push({ type: "message", role: "assistant", content: message.text ?? "" }); + } else if (message.contentType === "tool-call") { + const callId = message.toolCallId ?? `call_${message.id}`; if (customToolCallIds.has(callId)) { - out.push({ - type: "custom_tool_call", - call_id: callId, - name: m.toolName ?? "unknown", - input: m.text ?? "", - status: "completed", - } as ResponseInputItem); + out.push({ type: "custom_tool_call", call_id: callId, name: message.toolName ?? "unknown", input: message.text ?? "", status: "completed" } as ResponseInputItem); } else { - out.push({ - type: "function_call", - call_id: callId, - name: m.toolName ?? "unknown", - arguments: m.text ?? "", - }); - } - } else if (m.contentType === "reasoning") { - // Re-emit the carried raw item verbatim (reasoning's - // encrypted_content, or an opaque call item like - // computer_call / mcp_call). Only reached for items whose turn - // has NOT been compressed — compressed items are hidden by the - // kernel and never get here. - if (m.rawResponsesItem) { - out.push(m.rawResponsesItem as ResponseInputItem); + out.push({ type: "function_call", call_id: callId, name: message.toolName ?? "unknown", arguments: message.text ?? "" }); } + } else if (message.contentType === "reasoning") { + if (raw) out.push(raw); } - } else if (m.role === "tool") { - // Match the tool-call type by call_id so outputs pair correctly. - const callId = m.toolCallId ?? ""; + } else if (message.role === "tool") { + const callId = message.toolCallId ?? ""; if (customToolCallIds.has(callId)) { - out.push({ - type: "custom_tool_call_output", - call_id: callId, - output: m.text ?? "", - } as ResponseInputItem); + out.push({ type: "custom_tool_call_output", call_id: callId, output: message.text ?? "" } as ResponseInputItem); } else { - out.push({ - type: "function_call_output", - call_id: callId, - output: m.text ?? "", - }); + out.push({ type: "function_call_output", call_id: callId, output: message.text ?? "" }); } } } return out; } -export function injectResponsesInstructions( - body: ResponsesRequestBody, - extraParts: string[], -): ResponsesRequestBody { - if (extraParts.length === 0) return body; - const extra = extraParts.join("\n\n"); - const existing = typeof body.instructions === "string" ? body.instructions : ""; - return { - ...body, - instructions: existing ? `${existing}\n\n---\n\n${extra}` : extra, - }; +export function injectResponsesDeveloperMessage( + input: string | ResponseInputItem[], + content: string, +): ResponseInputItem[] { + const items: ResponseInputItem[] = typeof input === "string" + ? [{ type: "message", role: "user", content: input }] + : [...input]; + let index = 0; + while (items[index]?.type === "additional_tools") index++; + items.splice(index, 0, { type: "message", role: "developer", content }); + return items; } -/** Extract the conversation dimension for Responses: a client-provided - * session header if present, else the previous_response_id chain (Responses' - * native conversation linkage) if present, else a content fingerprint of - * the first user input. See conversationSignalAnthropic for the full - * rationale. */ -export function conversationSignalResponses(body: ResponsesRequestBody, headerValue?: string): string { - if (headerValue && headerValue.trim()) return headerValue.trim(); - // Codex 0.147+ sends body.session_id (a per-conversation UUID). Prefer it - // over previous_response_id (may be absent on the first turn) and over - // content hashing (collides on identical openers). See README "Session - // identity" for the per-client story. - if (typeof body.session_id === "string" && body.session_id.length > 0) { - return `codex-${body.session_id}`; +export function conversationIdentityResponses( + body: ResponsesRequestBody, + headerValue?: string, +): ConversationIdentity { + if (headerValue?.trim()) return { value: headerValue.trim(), source: "header", clientProvided: true }; + if (typeof body.session_id === "string" && body.session_id.trim()) { + return { value: body.session_id.trim(), source: "body-session", clientProvided: true }; } - if (typeof body.previous_response_id === "string" && body.previous_response_id.length > 0) { - return `resp-${body.previous_response_id}`; + const metadataSession = body.metadata?.session_id; + if (typeof metadataSession === "string" && metadataSession.trim()) { + return { value: metadataSession.trim(), source: "metadata-session", clientProvided: true }; } - let seed = "default"; - if (Array.isArray(body.input)) { - const firstUser = body.input.find( - (i) => i.type === "message" && (i as ResponseInputMessage).role === "user", - ) as ResponseInputMessage | undefined; - if (firstUser) seed = messageContent(firstUser.content).slice(0, 200); - } else if (typeof body.input === "string") { - seed = body.input.slice(0, 200); + if (typeof body.previous_response_id === "string" && body.previous_response_id.trim()) { + return { value: body.previous_response_id.trim(), source: "previous-response", clientProvided: false }; } - return hashId(seed); + return { value: hashId(JSON.stringify(body.input ?? [])), source: "content-fingerprint", clientProvided: false }; +} + +export function conversationSignalResponses(body: ResponsesRequestBody, headerValue?: string): string { + return conversationIdentityResponses(body, headerValue).value; } diff --git a/src/server.ts b/src/server.ts index b4e668650a..baf1263ec5 100644 --- a/src/server.ts +++ b/src/server.ts @@ -3,12 +3,12 @@ import fs from "node:fs"; import { tmpdir } from "node:os"; import { createCore, type CompressionCore, type Config, type CoreMessage, type NudgeDecision, estimateTokensFast, renderNudgeText, deactivateBlock } from "acp-kernel"; import type { ProxyOptions } from "./config.js"; -import { loadRoutes } from "./config.js"; +import { loadOptions, loadRoutes } from "./config.js"; import { resetProxyCache } from "./upstream-proxy.js"; import { resolveContextLimit } from "./config.js"; import { contextFromRegistry, loadRegistry } from "./registry.js"; import { fetchWithTimeout, MAX_REQUEST_BYTES } from "./fetch-util.js"; -import { resolveProxy, proxyDispatcher } from "./upstream-proxy.js"; +import { formatUpstreamError, getUpstreamConnectionStatus, recordUpstreamConnection, resolveProxy, resolveProxyDecision, proxyDispatcher } from "./upstream-proxy.js"; import { anthropicToCore, coreToAnthropic, @@ -29,28 +29,38 @@ import { type ResponsesRequestBody, type ResponseInputItem, responsesToCore, - coreToResponses, - injectResponsesInstructions, + patchResponsesInput, + injectResponsesDeveloperMessage, + conversationIdentityResponses, conversationSignalResponses, } from "./responses.js"; -import { getSession, listSessions, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, withSessionLock } from "./session.js"; +import { getSession, listSessions, type Session, initSessions, markDirty, flushAllSessions, acquireInFlight, releaseInFlight, withSessionLock, markNativeCompactionBoundary, reconcileNativeCompactionBoundary } from "./session.js"; import { COMPRESS_TOOL, ACP_TOOLS_ANTHROPIC, ACP_TOOLS_OPENAI, ACP_TOOLS_RESPONSES, COMPRESS_TOOL_NAME, buildCompressSystemPrompt, buildCompressTextSystemPrompt } from "./compress-tool.js"; import { rewriteSseStream, rewriteJsonResponse, type RewriteCtx } from "./stream.js"; import { applyRanges } from "./stream.js"; -import { renderUI, handleConfigGet, handleConfigPut } from "./web.js"; +import { renderUI, handleCodexHistoryGet, handleCodexHistoryRepair, handleConfigGet, handleConfigPut } from "./web/index.js"; import { reapOrphanBlocks } from "./orphan-gc.js"; import { getStore } from "./persist.js"; import { compressLoopStream } from "./compress-loop.js"; import { compressLoopAnthropicStream } from "./compress-loop-anthropic.js"; import { log as loggerLog, configureLogger, getLogPath, closeLogger } from "./logger.js"; import { defaultLogFile } from "./paths.js"; -import { compressLoopResponsesStream } from "./compress-loop-responses.js"; +import { compressLoopResponsesJson, compressLoopResponsesStream } from "./compress-loop-responses.js"; import { rewriteOpenaiJsonResponse } from "./stream-openai.js"; import { rewriteResponsesSseStream, rewriteResponsesJsonResponse } from "./stream-responses.js"; import { emitStreamError } from "./stream-error.js"; -import { deriveSessionId as deriveProxySessionId, affinityToken, clientConversationHeader } from "./session-id.js"; +import { deriveSessionId as deriveProxySessionId, affinityToken, clientConversationHeader, type ConversationIdentity } from "./session-id.js"; import { setupMitm, readMitmUpstream } from "./mitm.js"; import type { BiliMessage } from "./bili-message.js"; +import { + disableCodexTakeover, + enableCodexTakeover, + getCodexRouteState, + getCodexTakeoverStatus, + recordCodexRouteRequest, + restoreCodexTakeoverOwnedBy, +} from "./codex-takeover.js"; +import { decodeRequestBody } from "./content-encoding.js"; const UPSTREAM_HOP_HEADERS = new Set([ "host", @@ -83,6 +93,13 @@ export function resolveUpstream(_opts: ProxyOptions, reqUrl: string, req?: http. const mitmKey = mitmUpstream.replace(/^https:\/\//, "mitm://"); return { upstream: mitmUpstream, rewrittenUrl: mitmKey + (reqUrl ?? "") }; } + if (reqUrl === "/codex" || reqUrl.startsWith("/codex/") || reqUrl.startsWith("/codex?")) { + const state = getCodexRouteState(); + if (!state) return undefined; + const suffix = reqUrl.slice("/codex".length); + const baseUrl = state.original.baseUrl.replace(/\/+$/, ""); + return { upstream: baseUrl, rewrittenUrl: baseUrl + suffix }; + } // Zero-config mode: a request like `/bili/https://open.bigmodel.cn/api/anthropic` // embeds the full upstream URL after the `/bili/` prefix. Strip the prefix, // take the rest verbatim as the upstream. This is the ONLY routing mode — @@ -137,7 +154,7 @@ export async function startServer(opts: ProxyOptions): Promise { } }); if (opts.mitm.enabled) { - setupMitm(server, opts.mitm.domains, (msg) => log("info", msg), (host) => resolveProxy(opts.routes, opts.proxy, `https://${host}`)); + setupMitm(server, opts.mitm.domains, (msg) => log("info", msg), (host) => resolveProxy(opts.routes, opts.proxy, `https://${host}`, opts.proxyFallback)); } server.listen(opts.port, opts.host, () => { const displayHost = opts.host === "0.0.0.0" ? "localhost" : opts.host; @@ -217,11 +234,18 @@ export async function startServer(opts: ProxyOptions): Promise { if (process.platform === "win32") { process.on("SIGBREAK", () => shutdown("SIGBREAK")); } + server.once("close", () => { + try { + restoreCodexTakeoverOwnedBy(process.pid); + } catch (error) { + log("warn", `[codex-route] restore on server close failed: ${String(error)}`); + } + }); return server; } type Prepared = { - body: string; + body: string | Buffer; session: Session; processedMessages: CoreMessage[]; /** Original CoreMessages from the protocol conversion, BEFORE processTurn @@ -232,6 +256,8 @@ type Prepared = { protocol: "anthropic" | "openai" | "responses"; stream: boolean; compressInjected: boolean; + responsesTextProtocol?: boolean; + resetAfterSuccess?: boolean; }; /** True if `addr` is a loopback (IPv4 127.x or IPv6 ::1 / ::ffff:127.0.0.1). @@ -241,6 +267,17 @@ function isLoopback(addr: string | undefined): boolean { return addr === "::1" || addr === "127.0.0.1" || addr.startsWith("127.") || addr.startsWith("::ffff:127."); } +function isTrustedAdminOrigin(origin: string | undefined, host: string | undefined): boolean { + if (!origin) return true; + if (!host) return false; + try { + const parsed = new URL(origin); + return (parsed.protocol === "http:" || parsed.protocol === "https:") && parsed.host === host; + } catch { + return false; + } +} + async function handle( req: http.IncomingMessage, res: http.ServerResponse, @@ -262,6 +299,11 @@ async function handle( res.end(JSON.stringify({ error: "management endpoints are loopback-only; access denied for " + (req.socket.remoteAddress ?? "unknown") })); return; } + if (isAdminPath && !isTrustedAdminOrigin(req.headers.origin, req.headers.host)) { + res.writeHead(403, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "management request origin does not match the local bili UI" })); + return; + } if (req.method === "GET" && req.url === "/__bili/stats") return sendStats(res); if (req.method === "GET" && req.url === "/") { // Browser visits root → redirect to the web UI. curl / health probes @@ -290,11 +332,95 @@ async function handle( return; } if (req.method === "GET" && req.url === "/__bili/config") return handleConfigGet(res); - if (req.method === "PUT" && req.url === "/__bili/config") return handleConfigPut(req, res); + if (req.method === "PUT" && req.url === "/__bili/config") { + return handleConfigPut(req, res, () => { + const fresh = loadOptions(); + opts.proxy = fresh.proxy; + opts.proxyMode = fresh.proxyMode; + opts.proxySource = fresh.proxySource; + opts.proxyFallback = fresh.proxyFallback; + resetProxyCache(); + for (const k of Object.keys(opts.routes)) delete opts.routes[k]; + Object.assign(opts.routes, loadRoutes()); + }, opts.port); + } if (req.method === "POST" && req.url === "/__bili/config/reload") return handleConfigReload(opts, res, log); + if (req.method === "GET" && req.url === "/__bili/codex") { + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify(getCodexTakeoverStatus())); + return; + } + if (req.method === "POST" && (req.url === "/__bili/codex/enable" || req.url === "/__bili/codex/disable")) { + try { + const status = req.url.endsWith("/enable") + ? enableCodexTakeover(opts.port, { ownerPid: process.pid }) + : disableCodexTakeover(); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify(status)); + } catch (error) { + res.writeHead(409, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: String(error) })); + } + return; + } + if (req.method === "GET" && req.url === "/__bili/upstream") { + const routeState = getCodexRouteState(); + const target = routeState?.original.baseUrl ?? opts.upstream; + const decision = resolveProxyDecision(opts.routes, opts.proxy, target, opts.proxyFallback); + const connection = getUpstreamConnectionStatus(); + const connectionMatchesTarget = (() => { + if (!connection.url) return false; + try { return new URL(connection.url).origin === new URL(target).origin; } catch { return false; } + })(); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + target, + proxy: decision.proxy ?? null, + source: decision.source, + mode: opts.proxyMode ?? "auto", + autoConfigUrl: decision.autoConfigUrl ?? null, + connected: connectionMatchesTarget ? connection.connected : undefined, + error: connectionMatchesTarget ? connection.error : undefined, + checkedAt: connectionMatchesTarget ? connection.checkedAt : undefined, + connectionUrl: connection.url, + connectionProxy: connection.proxy, + })); + return; + } + if (req.method === "POST" && req.url === "/__bili/upstream/test") { + const routeState = getCodexRouteState(); + const target = routeState?.original.baseUrl ?? opts.upstream; + const targetUrl = new URL(target).origin; + const proxyUrl = resolveProxyDecision(opts.routes, opts.proxy, target, opts.proxyFallback).proxy; + try { + const result = await fetchWithTimeout(targetUrl, { + method: "HEAD", + ...(proxyUrl ? { dispatcher: proxyDispatcher(proxyUrl) } : {}), + }, 15_000); + result.clearTimer(); + recordUpstreamConnection(targetUrl, proxyUrl); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, status: result.response.status, target: targetUrl, proxy: proxyUrl ?? null })); + } catch (error) { + recordUpstreamConnection(targetUrl, proxyUrl, error); + res.writeHead(502, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: formatUpstreamError(error, targetUrl, proxyUrl) })); + } + return; + } + if (req.method === "GET" && req.url === "/__bili/codex-history") return handleCodexHistoryGet(res); + if (req.method === "POST" && req.url === "/__bili/codex-history/repair") return handleCodexHistoryRepair(res); + if ((req.url === "/codex" || req.url?.startsWith("/codex/") || req.url?.startsWith("/codex?")) && !getCodexRouteState()) { + res.writeHead(503, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "Codex local route is not enabled; run bili codex enable" })); + return; + } let bodyBuffer: Buffer; try { bodyBuffer = await readBody(req); + const decoded = await decodeRequestBody(headerValue(req, "content-encoding"), bodyBuffer, MAX_REQUEST_BYTES); + bodyBuffer = decoded.body; + if (decoded.decoded) delete req.headers["content-encoding"]; } catch (err) { if (err instanceof BodyTooLargeError) { log("warn", `413: request body exceeds ${err.limit} bytes`); @@ -302,7 +428,7 @@ async function handle( res.end(JSON.stringify({ error: { type: "request_too_large", message: err.message } })); return; } - log("warn", `read body failed: ${String(err)}`); + log("warn", `read/decode body failed: ${String(err)}`); res.writeHead(400, { "content-type": "application/json" }); res.end(JSON.stringify({ error: { type: "invalid_request", message: String(err) } })); return; @@ -311,13 +437,7 @@ async function handle( // Strip query string before matching path suffixes: a request like // `/v1/responses?foo=1` must still be detected as the responses protocol. const urlPath = url.split("?", 2)[0]; - // count_tokens (Anthropic /v1/messages/count_tokens) is routed through the - // anthropic session path so the proxy can apply the SAME compression it - // would forward on the next /v1/messages turn — the relay then returns the - // compressed-context token count. CC sizes its own autocompact window off - // this count, so reporting the post-compression count keeps CC from ever - // tripping autocompact (zero task drift). ACP_COUNT_TOKENS_PASSTHROUGH=1 - // restores the old "forward unchanged" behavior. + const responsesCompact = urlPath.endsWith("/responses/compact"); const countTokens = isCountTokensRequest(req.method ?? "GET", urlPath, bodyBuffer.length > 0); const protocol: "anthropic" | "openai" | "responses" | null = req.method === "POST" && bodyBuffer.length > 0 @@ -325,17 +445,18 @@ async function handle( ? "openai" : urlPath.endsWith("/v1/messages") || urlPath.endsWith("/messages") ? "anthropic" - : urlPath.endsWith("/responses") + : urlPath.endsWith("/responses") || responsesCompact ? "responses" - : countTokens - ? "anthropic" - : null + : null : null; // Resolve the upstream route once here so both the session id (needs the // upstream ORIGIN for cross-provider isolation) and forward() (needs the // full rewritten URL) use the same decision. Computed before prepare() so // the session can embed the provider origin. const route = resolveUpstream(opts, req.url ?? "", req); + if (route && (req.url === "/codex" || req.url?.startsWith("/codex/") || req.url?.startsWith("/codex?"))) { + recordCodexRouteRequest(req.url ?? "/codex"); + } const upstreamOrigin = route ? route.upstream : opts.upstream; // Per-request context limit: look up body.model against the per-route model // declaration in providers.json first (same model can have different @@ -381,12 +502,14 @@ async function handle( // shared 200-char prefix and leak compression state across sessions. const clientConv = clientConversationHeader(req.headers); const convHeader = clientConv ?? sessionHeader; - const conversation = - protocol === "anthropic" - ? conversationSignalAnthropic(parsed as AnthropicRequestBody, convHeader) - : protocol === "openai" - ? conversationSignalOpenai(parsed as OpenAIRequestBody, convHeader) - : conversationSignalResponses(parsed as ResponsesRequestBody, convHeader); + const responsesIdentity = protocol === "responses" + ? conversationIdentityResponses(parsed as ResponsesRequestBody, convHeader) + : undefined; + const conversation = protocol === "anthropic" + ? conversationSignalAnthropic(parsed as AnthropicRequestBody, convHeader) + : protocol === "openai" + ? conversationSignalOpenai(parsed as OpenAIRequestBody, convHeader) + : responsesIdentity?.value ?? conversationSignalResponses(parsed as ResponsesRequestBody, convHeader); const sessionId = deriveProxySessionId(req.headers, protocol, upstreamOrigin, conversation); // Two separate uses of the conversation signal: // - `affinity`: header value forwarded upstream for sticky-routing / @@ -397,20 +520,29 @@ async function handle( // body.session_id) — never the synthetic one — so a user can tell // at a glance which client owns a session. pi sends nothing, so its // label stays empty (shown as "—" in the UI). - const affinity = affinityToken(req.headers, conversation); - const clientLabel = clientConversationHeader(req.headers); + const affinity = affinityToken(responsesIdentity ?? { + value: clientConv ?? conversation, + source: clientConv ? "header" : "generated", + clientProvided: !!clientConv, + }); + const clientLabel = responsesIdentity?.clientProvided + ? responsesIdentity.value + : clientConversationHeader(req.headers); const session = getSession(sessionId, { protocol, upstreamOrigin, label: clientLabel ?? undefined }); // Serialize per-session: prepare (processTurn mutates state) + forward // (stream rewriter mutates state via compress/decompress) must not // interleave across concurrent requests on the same session. await withSessionLock(session, async () => { - prepared = countTokens - ? prepareCountTokens(parsed as AnthropicRequestBody, core, reqConfig, log, session) - : protocol === "anthropic" - ? prepareAnthropic(parsed as AnthropicRequestBody, req, opts, core, reqConfig, log, session) - : protocol === "openai" - ? prepareOpenai(parsed as OpenAIRequestBody, req, opts, core, reqConfig, log, session) - : prepareResponses(parsed as ResponsesRequestBody, req, opts, core, reqConfig, log, session); + prepared = + countTokens + ? prepareCountTokens(parsed as AnthropicRequestBody, core, reqConfig, log, session) + : protocol === "anthropic" + ? prepareAnthropic(parsed as AnthropicRequestBody, req, opts, core, reqConfig, log, session) + : protocol === "openai" + ? prepareOpenai(parsed as OpenAIRequestBody, req, opts, core, reqConfig, log, session) + : responsesCompact + ? prepareResponsesCompact(bodyBuffer, parsed as ResponsesRequestBody, session) + : prepareResponses(parsed as ResponsesRequestBody, req, opts, core, reqConfig, log, session, responsesIdentity!); acquireInFlight(session); try { await forward(req, res, opts, prepared!.body, prepared!, core, reqConfig, log, route, affinity); @@ -421,7 +553,7 @@ async function handle( } if (!prepared) { if (protocol === null && !opts.passthrough) { - log("warn", `unrecognized path ${url} — not a known protocol (/chat/completions, /v1/messages, /responses); forwarding unchanged`); + log("warn", `unrecognized path ${url} — not a known protocol (/chat/completions, /v1/messages, /responses, /responses/compact); forwarding unchanged`); } await forward(req, res, opts, bodyBuffer, null, core, reqConfig, log, route, undefined); } @@ -455,62 +587,6 @@ function diagNudge(turn: { nudge?: { shouldInject: boolean; reason: string; cont return `[${sessionId}] nudge ${inject}: usage=${pct} (${tokenCount}/${limit}), growth=${growth}/${floor} (ref=${ref}, interval=${interval}), pendingT1=${pendingT1}/${interval}, reason="${n.reason.slice(0, 120)}"`; } -export function isCountTokensRequest(method: string, urlPath: string, hasBody: boolean): boolean { - return ( - method === "POST" && - hasBody && - process.env.ACP_COUNT_TOKENS_PASSTHROUGH !== "1" && - urlPath.endsWith("/messages/count_tokens") - ); -} - -export function prepareCountTokens( - parsed: AnthropicRequestBody, - core: CompressionCore, - config: Config, - log: (level: string, msg: string) => void, - session: Session, -): Prepared { - const sessionId = session.id; - try { - const { msgs, cacheControls } = anthropicToCore(parsed); - // READ-ONLY mirror of the next /v1/messages turn (same processTurn: - // prune covered msgs, inject summaries, tag refs) so the relay counts - // the COMPRESSED context. Safety depends on acp-kernel syncBlocks - // deep-cloning input state (it bumps block.survivedCount in place); - // turn.state is discarded so nothing is stamped/created. If a future - // acp-kernel change reverts that clone this would silently corrupt - // session.state — the READ-ONLY test guards it. - const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: session.stats.lastInputTokens, renderTags: "text-only" }); - const rebuiltMessages = coreToAnthropic(turn.messages as BiliMessage[], cacheControls); - log("info", `[${sessionId}] count_tokens pruned: ${msgs.length} → ${turn.messages.length} msgs`); - return { - body: JSON.stringify({ ...parsed, messages: rebuiltMessages }), - session, - processedMessages: [], - originalMessages: msgs, - protocol: "anthropic", - stream: false, - // MUST stay false + empty processedMessages: keeps forward() on - // plain-pipeThrough so usage-capture/markDirty stays unreachable - // (else it poisons lastInputTokens with the compressed count and - // suppresses the proxy's own nudges). - compressInjected: false, - }; - } catch (err) { - log("warn", `[${sessionId}] count_tokens prune failed, forwarding unchanged: ${String(err)}`); - return { - body: JSON.stringify(parsed), - session, - processedMessages: [], - originalMessages: [], - protocol: "anthropic", - stream: false, - compressInjected: false, - }; - } -} - function prepareAnthropic( parsed: AnthropicRequestBody, req: http.IncomingMessage, @@ -677,10 +753,14 @@ function prepareResponses( config: Config, log: (level: string, msg: string) => void, session: Session, + identity: ConversationIdentity, ): Prepared { const sessionId = session.id; const stream = parsed.stream === true; ++session.stats.requests; + if (reconcileNativeCompactionBoundary(session)) { + log("info", `[${sessionId}] reconciled ACP state after native Responses compact boundary`); + } let processedMessages: CoreMessage[] = []; let originalMessages: CoreMessage[] = []; @@ -688,17 +768,17 @@ function prepareResponses( let toolsOut = parsed.tools; const shouldInject = opts.compress.injectTool; + const responsesTextProtocol = FORCE_TEXT_PROTOCOL || + isChatGptCodexUpstream(session.meta.upstreamOrigin) || + isCodexResponsesLite(req.headers, parsed); try { - const { msgs, systemParts, preamble, customToolCallIds } = responsesToCore(parsed); + const projection = responsesToCore(parsed); + const { msgs } = projection; originalMessages = msgs; if (process.env.ACP_DEBUG) { log("info", `[${sessionId}] input items: ${Array.isArray(parsed.input) ? parsed.input.map((i: ResponseInputItem) => i.type).join(",") : "(string)"}`); } - // tokenCount = upstream's real input_tokens from the previous turn - // (see anthropic branch comment). Never an estimate. systemParts is - // still extracted (used by injectResponsesInstructions later). - systemParts.join("\n\n"); const tokenCount = session.stats.lastInputTokens; const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount, renderTags: process.env.ACP_RENDER_NONE ? "none" : "text-only" }); session.state = turn.state; @@ -711,51 +791,41 @@ function prepareResponses( log("info", diagNudge(turn, sessionId, tokenCount, config.modelContextLimit)); processedMessages = turn.messages; reapOrphanBlocks(session, msgs, deactivateBlock); - // Rebuild input preserving the responses_lite contract: - // input[0] = additional_tools (host directive, verbatim) - // input[1] = developer message (base instructions + compress prompt) - // input[2..] = conversation history (compressed) - // top-level `instructions` is NEVER touched — it must stay empty for - // responses_lite. Setting it non-empty breaks code_mode tool exposure. - const conversationItems = coreToResponses(processedMessages as BiliMessage[], customToolCallIds); - if (preamble.length > 0) { - log("info", `[${sessionId}] preserved ${preamble.length} opaque preamble item(s): ${preamble.map((p) => p.type).join(",")}`); - } - - const inputItems: ResponseInputItem[] = [...preamble]; + rebuiltInput = patchResponsesInput(projection, processedMessages); if (shouldInject && !process.env.ACP_NO_COMPRESS_PROMPT) { - const sysParts = [...systemParts, buildCompressSystemPrompt()]; - inputItems.push({ type: "message", role: "developer", content: sysParts.join("\n\n---\n\n") }); - if (!process.env.ACP_NO_INJECT_TOOL) { - toolsOut = injectResponsesTool(parsed.tools); - } - } else if (systemParts.length > 0) { - // No compress injection, but still restore the base instructions - // that responsesToCore lifted out of the input. - inputItems.push({ type: "message", role: "developer", content: systemParts.join("\n\n---\n\n") }); - } - inputItems.push(...conversationItems); - if (process.env.ACP_DEBUG) { - const ctcs = conversationItems.filter((i) => i.type === "custom_tool_call").length; - const ctcos = conversationItems.filter((i) => i.type === "custom_tool_call_output").length; - log("info", `[${sessionId}] rebuilt: msgs=${msgs.length} -> conv=${conversationItems.length} (custom_tool_call=${ctcs} custom_tool_call_output=${ctcos})`); + const prompt = responsesTextProtocol ? buildCompressTextSystemPrompt() : buildCompressSystemPrompt(); + const devContent = [...projection.systemParts, prompt].join("\n\n---\n\n"); + rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, devContent); + if (!responsesTextProtocol && !process.env.ACP_NO_INJECT_TOOL) toolsOut = injectResponsesTool(parsed.tools); + } else if (projection.systemParts.length > 0) { + rebuiltInput = injectResponsesDeveloperMessage(rebuiltInput, projection.systemParts.join("\n\n---\n\n")); } if (turn.nudge?.shouldInject && shouldInject) { try { const rendered = renderNudgeText(turn.nudge); if (rendered.text) { + const inputItems: ResponseInputItem[] = typeof rebuiltInput === "string" + ? [{ type: "message", role: "user", content: rebuiltInput }] + : rebuiltInput; inputItems.push({ type: "message", role: "user", content: rendered.text }); + rebuiltInput = inputItems; } } catch { } } - rebuiltInput = inputItems; } catch (err) { log("warn", `[${sessionId}] kernel transform failed, forwarding unchanged: ${String(err)}`); processedMessages = []; } const rebuilt: ResponsesRequestBody = { ...parsed, input: rebuiltInput, tools: toolsOut }; + const promptCacheKey = resolvePromptCacheKey( + rebuilt.prompt_cache_key, + identity, + opts.promptCache.routing, + session.meta.upstreamOrigin, + ); + if (promptCacheKey && !rebuilt.prompt_cache_key) rebuilt.prompt_cache_key = promptCacheKey; // This adapter is stateless: we replay the FULL conversation in `input`. // Strip Responses' native chaining fields so the upstream does not resolve // stored server-side state on top of the input we already sent. Forwarding @@ -778,7 +848,114 @@ function prepareResponses( log("info", `[${sessionId}] responses forward tools=[${fwdTools.join(",")}] injectTool=${shouldInject} NO_INJECT_TOOL=${!!process.env.ACP_NO_INJECT_TOOL} NO_COMPRESS_PROMPT=${!!process.env.ACP_NO_COMPRESS_PROMPT}`); } markDirty(session); - return { body: JSON.stringify(rebuilt), session, processedMessages, originalMessages, protocol: "responses", stream, compressInjected: shouldInject } as Prepared; + return { + body: JSON.stringify(rebuilt), + session, + processedMessages, + originalMessages, + protocol: "responses", + stream, + compressInjected: shouldInject, + responsesTextProtocol, + }; +} + +export function isCountTokensRequest(method: string, urlPath: string, hasBody: boolean): boolean { + return ( + method === "POST" && + hasBody && + process.env.ACP_COUNT_TOKENS_PASSTHROUGH !== "1" && + urlPath.endsWith("/messages/count_tokens") + ); +} + +export function prepareCountTokens( + parsed: AnthropicRequestBody, + core: CompressionCore, + config: Config, + log: (level: string, msg: string) => void, + session: Session, +): Prepared { + const sessionId = session.id; + try { + const { msgs, cacheControls } = anthropicToCore(parsed); + const turn = core.processTurn({ messages: msgs, state: session.state, config, tokenCount: session.stats.lastInputTokens, renderTags: "text-only" }); + const rebuiltMessages = coreToAnthropic(turn.messages as BiliMessage[], cacheControls); + log("info", `[${sessionId}] count_tokens pruned: ${msgs.length} → ${turn.messages.length} msgs`); + return { + body: JSON.stringify({ ...parsed, messages: rebuiltMessages }), + session, + processedMessages: [], + originalMessages: msgs, + protocol: "anthropic", + stream: false, + compressInjected: false, + }; + } catch (err) { + log("warn", `[${sessionId}] count_tokens prune failed, forwarding unchanged: ${String(err)}`); + return { + body: JSON.stringify(parsed), + session, + processedMessages: [], + originalMessages: [], + protocol: "anthropic", + stream: false, + compressInjected: false, + }; + } +} + +function prepareResponsesCompact(body: Buffer, parsed: ResponsesRequestBody, session: Session): Prepared { + ++session.stats.requests; + return { + body, + session, + processedMessages: [], + originalMessages: [], + protocol: "responses", + stream: parsed.stream === true, + compressInjected: false, + resetAfterSuccess: true, + }; +} + +export function isChatGptCodexUpstream(upstream: string | undefined): boolean { + if (!upstream) return false; + try { + return new URL(upstream).hostname.toLowerCase() === "chatgpt.com"; + } catch { + return false; + } +} + +export function isCodexResponsesLite(headers: http.IncomingHttpHeaders, body: ResponsesRequestBody): boolean { + if (headers["x-openai-internal-codex-responses-lite"] !== undefined) return true; + if (Object.prototype.hasOwnProperty.call(body, "additional_tools")) return true; + return Array.isArray(body.input) && body.input.some((item) => item.type === "additional_tools"); +} + +export function shouldInjectPromptCacheKey( + routing: ProxyOptions["promptCache"]["routing"], + upstream: string | undefined, +): boolean { + if (routing === "enabled") return true; + if (routing === "disabled" || !upstream) return false; + try { + return new URL(upstream).hostname.toLowerCase() === "api.openai.com"; + } catch { + return false; + } +} + +export function resolvePromptCacheKey( + explicit: string | undefined, + identity: ConversationIdentity, + routing: ProxyOptions["promptCache"]["routing"], + upstream: string | undefined, +): string | undefined { + if (explicit?.trim()) return explicit; + if (!identity.clientProvided || !shouldInjectPromptCacheKey(routing, upstream)) return undefined; + return identity.value; } function injectSystem( @@ -820,7 +997,7 @@ function injectOpenaiTool(tools: OpenAITool[] | undefined): OpenAITool[] { * server-side tools are disabled the moment any `tools` entry is declared. * In text mode we keep `tools` untouched (undefined) so code_mode stays * active, and detect the trigger in the output_text stream instead. */ -const TEXT_PROTOCOL = process.env.ACP_COMPRESS_PROTOCOL === "text"; +const FORCE_TEXT_PROTOCOL = process.env.ACP_COMPRESS_PROTOCOL === "text"; /** Inject all ACP tools (compress/decompress/search_context/acp_status) in * Responses API flat format, matching the PROXY_TOOL_NAMES set the compress * loop dispatches on. Idempotent. */ @@ -895,23 +1072,28 @@ async function forward( headers[k] = Array.isArray(v) ? v.join(", ") : v; } headers["host"] = new URL(route ? route.upstream : opts.upstream).host; - // Inject a synthesized session-affinity header for clients that send none - // (pi). opencode already sends x-session-affinity (passed through above); - // codex carries identity in body.session_id (passes through in the body). - // pi sends nothing, so upstream sticky-routing/cache pools would fall back - // to content fingerprinting — give them a stable key instead. Only inject - // when the client sent NO session header at all (don't shadow opencode's). + // Forward a client-provided Responses session identity only when it was + // carried in the body rather than an existing request header. if (affinity && !clientConversationHeader(req.headers)) { headers["x-session-id"] = affinity; } - const dispatcher = proxyDispatcher(resolveProxy(opts.routes, opts.proxy, route?.rewrittenUrl)); + const proxyUrl = resolveProxy(opts.routes, opts.proxy, route?.rewrittenUrl ?? upstreamUrl, opts.proxyFallback); + const dispatcher = proxyDispatcher(proxyUrl); const init: Omit & { dispatcher?: object } = { method: req.method ?? "GET", headers, body: req.method === "GET" || req.method === "HEAD" ? undefined : body, }; if (dispatcher) init.dispatcher = dispatcher; - const { response: upstream, clearTimer: clearUpstreamTimer } = await fetchWithTimeout(upstreamUrl, init); + let upstreamResult: Awaited>; + try { + upstreamResult = await fetchWithTimeout(upstreamUrl, init); + recordUpstreamConnection(upstreamUrl, proxyUrl); + } catch (error) { + recordUpstreamConnection(upstreamUrl, proxyUrl, error); + throw new Error(`upstream request failed: ${formatUpstreamError(error, upstreamUrl, proxyUrl)}`, { cause: error }); + } + const { response: upstream, clearTimer: clearUpstreamTimer } = upstreamResult; const respHeaders: Record = {}; upstream.headers.forEach((v, k) => { if (UPSTREAM_HOP_HEADERS.has(k.toLowerCase())) return; @@ -934,6 +1116,10 @@ async function forward( if (!upstream.body) { res.end(); clearUpstreamTimer(); + if (prepared?.resetAfterSuccess) { + markNativeCompactionBoundary(prepared.session); + log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`); + } return; } // We only rewrite when THIS request actually had the compress tool @@ -949,6 +1135,10 @@ async function forward( if (!useRewriter || prepared === null) { await pipeThrough(upstream.body, res); clearUpstreamTimer(); + if (prepared?.resetAfterSuccess) { + markNativeCompactionBoundary(prepared.session); + log("info", `[${prepared.session.id}] native Responses compact completed; rebase scheduled for next Responses turn`); + } return; } const ctx: RewriteCtx = { @@ -982,7 +1172,7 @@ async function forward( reqHeaders["content-type"] = "application/json"; const loop = compressLoopStream( streamToRead, - { core, config, messages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl: resolveProxy(opts.routes, opts.proxy, route?.rewrittenUrl) }, + { core, config, messages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl }, parsedReq, { url: upstreamUrl, headers: reqHeaders }, ); @@ -1005,7 +1195,7 @@ async function forward( reqHeaders["content-type"] = "application/json"; const loop = compressLoopResponsesStream( streamToRead, - { core, config, messages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl: resolveProxy(opts.routes, opts.proxy, route?.rewrittenUrl) }, + { core, config, messages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl, textProtocol: prepared.responsesTextProtocol }, parsedReq, { url: upstreamUrl, headers: reqHeaders }, ); @@ -1028,7 +1218,7 @@ async function forward( reqHeaders["content-type"] = "application/json"; const loop = compressLoopAnthropicStream( streamToRead, - { core, config, messages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl: resolveProxy(opts.routes, opts.proxy, route?.rewrittenUrl) }, + { core, config, messages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl }, parsedReq, { url: upstreamUrl, headers: reqHeaders }, ); @@ -1059,7 +1249,22 @@ async function forward( const buf = await upstream.arrayBuffer(); const text = Buffer.from(buf).toString("utf8"); try { - const json = JSON.parse(text); + let json = JSON.parse(text) as Record; + if (prepared.protocol === "responses" && prepared.responsesTextProtocol) { + const requestBody = JSON.parse(typeof body === "string" ? body : body.toString("utf8")) as Record; + const requestHeaders: Record = {}; + for (const [key, value] of Object.entries(headers)) { + if (key.toLowerCase() === "content-length" || key.toLowerCase() === "host") continue; + requestHeaders[key] = value; + } + requestHeaders["content-type"] = "application/json"; + json = await compressLoopResponsesJson( + json, + { core, config, messages: prepared.originalMessages, session: prepared.session, log: ctx.log, proxyUrl, textProtocol: true }, + requestBody, + { url: upstreamUrl, headers: requestHeaders }, + ); + } // Capture upstream usage so tokenCount (which drives nudge + // emergency-truncate) reflects reality for non-streaming // sessions too. The streaming loops do this in their SSE @@ -1069,11 +1274,13 @@ async function forward( // Anthropic: input_tokens / cache_read_input_tokens / output_tokens // OpenAI: prompt_tokens / prompt_tokens_details.cached_tokens / completion_tokens // Responses: input_tokens / input_tokens_details.cached_tokens / output_tokens - const u = (json?.usage ?? {}) as Record; + const u = (json.usage ?? {}) as Record; const prompt = u.prompt_tokens ?? u.input_tokens; if (typeof prompt === "number") { prepared.session.stats.inputTokens += prompt; - const cached = u.prompt_tokens_details?.cached_tokens ?? u.input_tokens_details?.cached_tokens ?? u.cache_read_input_tokens; + const promptDetails = u.prompt_tokens_details as Record | undefined; + const inputDetails = u.input_tokens_details as Record | undefined; + const cached = promptDetails?.cached_tokens ?? inputDetails?.cached_tokens ?? u.cache_read_input_tokens; // tokenCount = TOTAL context (new + cached); see anthropic branch. prepared.session.stats.lastInputTokens = prompt + (typeof cached === "number" ? cached : 0); if (typeof cached === "number") { diff --git a/src/session-id.ts b/src/session-id.ts index 24202fbfe0..ea23677dea 100644 --- a/src/session-id.ts +++ b/src/session-id.ts @@ -1,5 +1,11 @@ import { hashId } from "./util.js"; +export type ConversationIdentity = { + value: string; + source: "header" | "body-session" | "metadata-session" | "previous-response" | "content-fingerprint" | "generated"; + clientProvided: boolean; +}; + /** * Session identity for the proxy's OWN compression state. * @@ -19,11 +25,8 @@ import { hashId } from "./util.js"; * compression-state store). It is NEVER sent upstream — it embeds the key and * upstream origin, which the upstream either already knows or must not see. * - * If a separate per-conversation signal is needed for upstream routing (e.g. - * to keep a multi-account load-balancer's prefix cache warm), use - * `affinityToken()` below — it is the conversation dimension alone, formatted - * to mirror OpenCode's `x-session-affinity` header so upstreams/LBs that - * understand that convention work without bespoke support. + * If a client-provided per-conversation signal is needed for upstream routing, + * use `affinityToken()` below. Generated proxy identities are never forwarded. */ /** Extract the account credential from common auth headers, verbatim. @@ -44,7 +47,7 @@ function extractKey(headers: Record): str /** Pull a client-provided conversation signal from headers, if any. */ export function clientConversationHeader(headers: Record): string | undefined { - const names = ["x-session-affinity", "x-acp-session", "x-session-id", "x-opencode-session"]; + const names = ["x-session-affinity", "x-acp-session", "x-session-id", "x-opencode-session", "session-id", "session_id"]; for (const name of names) { const v = headers[name]; if (typeof v === "string" && v.trim().length > 0) return v.trim(); @@ -73,17 +76,9 @@ export function deriveSessionId( } /** - * The conversation dimension alone, formatted as OpenCode's - * `x-session-affinity: ses_` value. Suitable for forwarding to an - * upstream that understands that convention (e.g. a multi-account - * load-balancer doing sticky routing). Contains NO key or upstream, so it is - * safe to send upstream and stable across account rotations. + * Return only an identity the client already supplied. Generated identities + * remain proxy-internal so billion-context does not invent upstream headers. */ -export function affinityToken( - headers: Record, - conversation: string, -): string { - const client = clientConversationHeader(headers); - if (client) return client; - return `ses_${conversation}`; +export function affinityToken(identity: ConversationIdentity): string | undefined { + return identity.clientProvided ? identity.value : undefined; } diff --git a/src/session.ts b/src/session.ts index cd89a4c2ef..960be494c9 100644 --- a/src/session.ts +++ b/src/session.ts @@ -195,6 +195,38 @@ export function cacheBlockContent(session: Session, blockId: string, content: Bl session.blockContents.set(blockId, content); } +export function resetSessionCompression(session: Session): void { + session.state = createInitialState(); + session.blockContents.clear(); + session.stats.lastInputTokens = 0; + session.stats.contextTokens = 0; + session.metadata.nativeCompactionAt = Date.now(); + markDirty(session); +} + +export function markNativeCompactionBoundary(session: Session): void { + session.metadata.nativeCompactionBoundary = { + at: Date.now(), + pendingRebase: true, + }; + markDirty(session); +} + +export function reconcileNativeCompactionBoundary(session: Session): boolean { + const boundary = session.metadata.nativeCompactionBoundary; + if (!boundary || typeof boundary !== "object" || !(boundary as Record).pendingRebase) { + return false; + } + resetSessionCompression(session); + session.metadata.nativeCompactionBoundary = { + ...(boundary as Record), + pendingRebase: false, + rebasedAt: Date.now(), + }; + markDirty(session); + return true; +} + /** Flush a session to disk and drop it from memory (LRU eviction). Refuses to * evict sessions that are in-flight or whose flush failed (would lose a * never-persisted session permanently). */ diff --git a/src/upstream-proxy.ts b/src/upstream-proxy.ts index d9b349d080..e4b6178697 100644 --- a/src/upstream-proxy.ts +++ b/src/upstream-proxy.ts @@ -1,81 +1,278 @@ -/** - * Upstream proxy support. - * - * Allows billion-context to route its OWN outbound connections (to the model - * providers) through an HTTP proxy. Typical use case: a Codex user behind the - * GFW already runs v2rayA; configuring billion-context's upstream proxy lets - * `api.openai.com` traffic reach the model instead of timing out. - * - * TWO outbound paths must support this: - * 1. `/bili/` path-mode requests — forwarded via `fetch()`. We inject an - * undici `ProxyAgent` as the fetch `dispatcher`. - * 2. MITM mode — `CONNECT :443` tunnels. We establish the outbound - * connection by talking CONNECT to the proxy, then run TLS over the - * resulting tunnel. - * - * Only HTTP proxies are supported (`http://host:port`). SOCKS5 is intentionally - * out of scope for now. - */ +import { execFileSync } from "node:child_process"; import net from "node:net"; import tls from "node:tls"; import { ProxyAgent } from "undici"; import type { ProviderRoutes } from "./config.js"; -/** Per-URL proxy cache so we don't construct a new ProxyAgent per request. */ +export type ParsedHttpProxy = { + url: string; + host: string; + port: number; + protocol: "http:" | "https:"; + authorization?: string; +}; + +export type ProxyFallbackOptions = { + httpProxy?: string; + httpsProxy?: string; + allProxy?: string; + noProxy?: string; + biliPort?: number; + systemProxy?: WindowsSystemProxy; + globalSource?: "bili-env" | "web-manual" | "config" | "auto" | "direct"; +}; + +export type UpstreamProxyDecision = { + proxy?: string; + source: string; + autoConfigUrl?: string; +}; + +export type WindowsSystemProxy = { + enabled: boolean; + http?: string; + https?: string; + bypass?: string; + autoConfigUrl?: string; +}; + +export type UpstreamConnectionStatus = { + url?: string; + proxy?: string; + connected?: boolean; + error?: string; + checkedAt?: string; +}; + const dispatcherCache = new Map(); +let lastConnection: UpstreamConnectionStatus = {}; +let windowsProxyCache: { at: number; value: WindowsSystemProxy } | undefined; + +function defaultPort(protocol: string): number { + return protocol === "https:" ? 443 : 80; +} + +function hostIsLoopback(host: string): boolean { + const normalized = host.replace(/^\[|\]$/g, "").toLowerCase(); + return normalized === "localhost" || + normalized === "0.0.0.0" || + normalized === "::" || + normalized === "::1" || + normalized === "0:0:0:0:0:0:0:1" || + normalized.startsWith("127.") || + normalized.startsWith("::ffff:127.") || + /^::ffff:7f[0-9a-f]{2}:/.test(normalized); +} + +function proxyAuthorization(url: URL): string | undefined { + if (!url.username && !url.password) return undefined; + const username = decodeURIComponent(url.username); + const password = decodeURIComponent(url.password); + return `Basic ${Buffer.from(`${username}:${password}`).toString("base64")}`; +} + +export function redactProxyUrl(value: string | undefined): string | undefined { + if (!value) return undefined; + try { + const url = new URL(value); + if (url.username || url.password) { + url.username = "***"; + url.password = "***"; + } + return url.href; + } catch { + return "(invalid proxy URL)"; + } +} -/** Parse a proxy URL. Returns undefined if `proxy` is empty or not an http(s) - * URL (we only support HTTP forward proxies, not SOCKS, for now). */ -export function parseHttpProxy(proxy?: string): { url: string; host: string; port: number } | undefined { - if (!proxy || typeof proxy !== "string") return undefined; +export function parseHttpProxy(proxy?: string, biliPort?: number): ParsedHttpProxy | undefined { + if (!proxy || typeof proxy !== "string" || !proxy.trim()) return undefined; + const candidate = /^[A-Za-z][A-Za-z0-9+.-]*:\/\//.test(proxy.trim()) ? proxy.trim() : `http://${proxy.trim()}`; + let url: URL; try { - const u = new URL(proxy); - // We accept http:// (standard HTTP forward proxy). https:// would mean - // TLS to the proxy itself — uncommon for local proxies; reject to keep - // the MITM path simple. socks5:// is not supported yet. - if (u.protocol !== "http:") return undefined; - const port = u.port ? parseInt(u.port, 10) : 80; - if (!Number.isFinite(port)) return undefined; - return { url: `${u.protocol}//${u.hostname}:${port}`, host: u.hostname, port }; + url = new URL(candidate); } catch { return undefined; } + if (url.protocol !== "http:" && url.protocol !== "https:") return undefined; + if ((url.pathname && url.pathname !== "/") || url.search || url.hash) return undefined; + const port = url.port ? Number.parseInt(url.port, 10) : defaultPort(url.protocol); + if (!Number.isInteger(port) || port < 1 || port > 65535) return undefined; + if (biliPort && hostIsLoopback(url.hostname) && port === biliPort) { + throw new Error(`upstream proxy would loop back into bili: ${redactProxyUrl(url.href)}`); + } + return { + url: url.href, + host: url.hostname, + port, + protocol: url.protocol, + ...(proxyAuthorization(url) ? { authorization: proxyAuthorization(url) } : {}), + }; } -/** - * Resolve the effective proxy for a given upstream URL. - * - * Rule: per-URL `route.proxy` overrides the global `globalProxy`. Neither set - * → undefined (direct connect). Uses the same longest-prefix match as - * resolveContextLimit so `https://api.openai.com/v1` and a MITM virtual URL - * `https://api.openai.com` both match a key like `https://api.openai.com`. - */ -export function resolveProxy( +export function validateHttpProxy(proxy: string | undefined, biliPort?: number): void { + if (!proxy?.trim()) return; + if (!parseHttpProxy(proxy, biliPort)) { + throw new Error(`upstream proxy must be an HTTP/HTTPS proxy origin: ${redactProxyUrl(proxy)}`); + } +} + +function targetUrl(upstreamUrl: string | undefined): URL | undefined { + if (!upstreamUrl) return undefined; + try { + return new URL(upstreamUrl.replace(/^mitm:\/\//, "https://")); + } catch { + return undefined; + } +} + +function parseFallbackProxy(proxy: string | undefined, biliPort?: number): ParsedHttpProxy | undefined { + try { + return parseHttpProxy(proxy, biliPort); + } catch { + return undefined; + } +} + +export function matchesNoProxy(target: URL, noProxy: string | undefined): boolean { + if (!noProxy) return false; + const host = target.hostname.replace(/^\[|\]$/g, "").toLowerCase(); + const port = target.port ? Number.parseInt(target.port, 10) : defaultPort(target.protocol); + for (const raw of noProxy.split(/[;,]/)) { + let token = raw.trim().toLowerCase(); + if (!token) continue; + if (token === "*") return true; + if (token === "" && !host.includes(".")) return true; + let tokenPort: number | undefined; + if (token.startsWith("[")) { + const bracket = token.indexOf("]"); + if (bracket > 0 && token[bracket + 1] === ":") { + tokenPort = Number.parseInt(token.slice(bracket + 2), 10); + token = token.slice(1, bracket); + } else token = token.replace(/^\[|\]$/g, ""); + } else { + const colon = token.lastIndexOf(":"); + if (colon > 0 && token.indexOf(":") === colon && /^\d+$/.test(token.slice(colon + 1))) { + tokenPort = Number.parseInt(token.slice(colon + 1), 10); + token = token.slice(0, colon); + } + } + if (tokenPort !== undefined && tokenPort !== port) continue; + const suffix = token.startsWith("*.") ? token.slice(1) : token.startsWith(".") ? token : undefined; + if (suffix ? host.endsWith(suffix) || host === suffix.slice(1) : host === token) return true; + } + return false; +} + +function parseWindowsProxyServer(value: string): Pick { + const trimmed = value.trim(); + if (!trimmed) return {}; + if (!trimmed.includes("=")) { + const normalized = parseHttpProxy(trimmed)?.url; + return normalized ? { http: normalized, https: normalized } : {}; + } + const result: Pick = {}; + for (const item of trimmed.split(";")) { + const [scheme, address] = item.split("=", 2).map((part) => part?.trim()); + const normalizedScheme = scheme?.toLowerCase(); + if ((normalizedScheme === "http" || normalizedScheme === "https") && address) { + const normalized = parseHttpProxy(address)?.url; + if (normalized) result[normalizedScheme] = normalized; + } + } + return result; +} + +function registryValue(output: string, name: string): string | undefined { + const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); + return output.match(new RegExp(`^\\s*${escaped}\\s+REG_\\w+\\s+(.+)$`, "mi"))?.[1]?.trim(); +} + +export function readWindowsSystemProxy(): WindowsSystemProxy { + if (process.platform !== "win32") return { enabled: false }; + const now = Date.now(); + if (windowsProxyCache && now - windowsProxyCache.at < 5000) return windowsProxyCache.value; + let value: WindowsSystemProxy = { enabled: false }; + try { + const output = execFileSync("reg", [ + "query", + "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", + ], { encoding: "utf8", windowsHide: true, timeout: 3000 }); + const enabledRaw = registryValue(output, "ProxyEnable"); + const enabled = enabledRaw === "0x1" || enabledRaw === "1"; + const staticProxy = enabled ? parseWindowsProxyServer(registryValue(output, "ProxyServer") ?? "") : {}; + value = { + enabled, + ...staticProxy, + ...(registryValue(output, "ProxyOverride") ? { bypass: registryValue(output, "ProxyOverride") } : {}), + ...(registryValue(output, "AutoConfigURL") ? { autoConfigUrl: registryValue(output, "AutoConfigURL") } : {}), + }; + } catch { + } + windowsProxyCache = { at: now, value }; + return value; +} + +export function resolveProxyDecision( routes: ProviderRoutes, globalProxy: string | undefined, upstreamUrl: string | undefined, -): string | undefined { - if (!upstreamUrl) return parseHttpProxy(globalProxy)?.url; + fallback: ProxyFallbackOptions = {}, +): UpstreamProxyDecision { + const target = targetUrl(upstreamUrl); let bestKey = ""; - for (const key of Object.keys(routes)) { - if (upstreamUrl === key || upstreamUrl.startsWith(key + "/")) { - if (key.length > bestKey.length) bestKey = key; + if (upstreamUrl) { + for (const key of Object.keys(routes)) { + if (upstreamUrl === key || upstreamUrl.startsWith(key + "/")) { + if (key.length > bestKey.length) bestKey = key; + } } } - // Child overrides parent. Empty string on the route means "explicitly - // direct" (override the global). Otherwise fall back to global. if (bestKey) { const routeProxy = routes[bestKey].proxy; - if (routeProxy !== undefined) return parseHttpProxy(routeProxy)?.url; + if (routeProxy !== undefined) { + if (!routeProxy.trim()) return { source: "provider-direct" }; + const parsed = parseHttpProxy(routeProxy, fallback.biliPort); + if (!parsed) throw new Error(`invalid per-URL upstream proxy for ${bestKey}: ${redactProxyUrl(routeProxy)}`); + return { proxy: parsed.url, source: "provider" }; + } + } + if (globalProxy === "") return { source: "direct" }; + const explicit = parseHttpProxy(globalProxy, fallback.biliPort)?.url; + if (explicit) return { proxy: explicit, source: fallback.globalSource ?? "global" }; + if (target && matchesNoProxy(target, fallback.noProxy)) return { source: "no-proxy" }; + const environmentCandidates: Array<[string, string | undefined]> = target?.protocol === "http:" + ? [["HTTP_PROXY", fallback.httpProxy], ["ALL_PROXY", fallback.allProxy]] + : [["HTTPS_PROXY", fallback.httpsProxy], ["HTTP_PROXY", fallback.httpProxy], ["ALL_PROXY", fallback.allProxy]]; + for (const [source, value] of environmentCandidates) { + const parsed = parseFallbackProxy(value, fallback.biliPort); + if (parsed) return { proxy: parsed.url, source }; + } + const system = fallback.systemProxy ?? readWindowsSystemProxy(); + if (target && matchesNoProxy(target, system.bypass)) { + return { source: "windows-bypass", ...(system.autoConfigUrl ? { autoConfigUrl: system.autoConfigUrl } : {}) }; + } + const systemValue = target?.protocol === "http:" ? system.http : system.https ?? system.http; + const systemProxy = parseFallbackProxy(systemValue, fallback.biliPort)?.url; + if (systemProxy) { + return { + proxy: systemProxy, + source: "windows-system", + ...(system.autoConfigUrl ? { autoConfigUrl: system.autoConfigUrl } : {}), + }; } - return parseHttpProxy(globalProxy)?.url; + return { source: "direct", ...(system.autoConfigUrl ? { autoConfigUrl: system.autoConfigUrl } : {}) }; +} + +export function resolveProxy( + routes: ProviderRoutes, + globalProxy: string | undefined, + upstreamUrl: string | undefined, + fallback: ProxyFallbackOptions = {}, +): string | undefined { + return resolveProxyDecision(routes, globalProxy, upstreamUrl, fallback).proxy; } -/** Get (or create) a cached undici ProxyAgent for `proxyUrl`. Returns undefined - * for direct connect. Used by the fetch (`/bili/`) path. Returned as a plain - * object so callers don't need to import the undici Dispatcher type. - * Call `resetProxyCache()` on config hot-reload to release ProxyAgents whose - * URL was removed/changed (otherwise they'd leak for the process lifetime). */ export function proxyDispatcher(proxyUrl: string | undefined): object | undefined { if (!proxyUrl) return undefined; let agent = dispatcherCache.get(proxyUrl); @@ -86,120 +283,144 @@ export function proxyDispatcher(proxyUrl: string | undefined): object | undefine return agent; } -/** Release all cached ProxyAgents. Called on config hot-reload so agents for - * proxy URLs that were removed/changed don't leak. Safe to call anytime; - * the next proxyDispatcher() call re-creates the needed agent lazily. */ export function resetProxyCache(): void { for (const agent of dispatcherCache.values()) { - try { agent.close(); } catch { /* best-effort */ } + try { void agent.close().catch(() => undefined); } catch { } } dispatcherCache.clear(); + lastConnection = {}; } -/** Establish a TCP connection to `host:port`, through an HTTP proxy if one is - * configured. `proxyUrl` is the resolved proxy (from resolveProxy) or - * undefined for direct connect. - * - * For direct connect: `net.connect(port, host)`. - * For proxied connect: connect to the proxy, send - * `CONNECT host:port HTTP/1.1\r\nHost: host:port\r\n\r\n`, wait for - * `HTTP/1.1 200`, then return the socket (the tunnel is now transparent). - * Throws on any failure (proxy refused, non-200, connection reset). */ -export function connectThroughProxy( - host: string, - port: number, - proxyUrl: string | undefined, -): Promise { +function openProxySocket(proxy: ParsedHttpProxy): net.Socket { + if (proxy.protocol === "https:") { + return tls.connect({ host: proxy.host, port: proxy.port, servername: net.isIP(proxy.host) ? undefined : proxy.host }); + } + return net.connect(proxy.port, proxy.host); +} + +export function connectThroughProxy(host: string, port: number, proxyUrl: string | undefined): Promise { if (!proxyUrl) { - // Direct connect — the common, fast path. return new Promise((resolve, reject) => { - const sock = net.connect(port, host); - sock.once("connect", () => resolve(sock)); - sock.once("error", reject); + const socket = net.connect(port, host); + socket.once("connect", () => resolve(socket)); + socket.once("error", reject); }); } const proxy = parseHttpProxy(proxyUrl); - if (!proxy) { - // Malformed proxy URL — fall back to direct rather than crash. - return new Promise((resolve, reject) => { - const sock = net.connect(port, host); - sock.once("connect", () => resolve(sock)); - sock.once("error", reject); - }); - } + if (!proxy) return Promise.reject(new Error(`invalid upstream proxy: ${redactProxyUrl(proxyUrl)}`)); return new Promise((resolve, reject) => { - const sock = net.connect(proxy.port, proxy.host); - let established = false; - // Internal timeout for the CONNECT handshake (proxy → upstream). If the - // proxy is slow/unresponsive the outer 15s tunnel timer would still - // fire, but a dedicated handshake timeout fails faster and surfaces a - // clearer error ("CONNECT handshake timeout") in the log. - const handshakeTimer = setTimeout(() => { - if (!established) { - sock.destroy(); - reject(new Error(`upstream proxy CONNECT ${host}:${port} handshake timeout`)); - } - }, 10000); - const cleanup = (err: Error) => { - clearTimeout(handshakeTimer); - if (!established) sock.destroy(); - reject(err); + const socket = openProxySocket(proxy); + let settled = false; + const finishError = (error: Error) => { + if (settled) return; + settled = true; + clearTimeout(timer); + socket.destroy(); + reject(error); }; - sock.once("error", cleanup); - sock.once("connect", () => { - // Send CONNECT to the proxy to open a tunnel to host:port. - const connectReq = - `CONNECT ${host}:${port} HTTP/1.1\r\n` + - `Host: ${host}:${port}\r\n` + - `Proxy-Connection: Keep-Alive\r\n\r\n`; - sock.write(connectReq); - // Read the proxy's HTTP response. We only need the status line + - // the blank-line terminator; body (if any) is not expected for a - // successful CONNECT. - let buf = ""; - const onLine = (data: Buffer) => { - buf += data.toString("utf8"); - const end = buf.indexOf("\r\n\r\n"); - if (end === -1) return; - sock.removeListener("data", onLine); - const head = buf.slice(0, end); - const rest = Buffer.from(buf.slice(end + 4), "utf8"); - // Status line: "HTTP/1.1 200 Connection Established\r\n..." - const statusLine = head.split("\r\n")[0] ?? ""; - if (!/^HTTP\/1\.[01]\s+2\d\d/.test(statusLine)) { - cleanup(new Error(`upstream proxy CONNECT ${host}:${port} failed: ${statusLine}`)); + const timer = setTimeout(() => finishError(new Error(`upstream proxy CONNECT ${host}:${port} handshake timeout`)), 10_000); + socket.once("error", finishError); + const connectedEvent = proxy.protocol === "https:" ? "secureConnect" : "connect"; + socket.once(connectedEvent, () => { + const headers = [ + `CONNECT ${host}:${port} HTTP/1.1`, + `Host: ${host}:${port}`, + "Proxy-Connection: Keep-Alive", + ...(proxy.authorization ? [`Proxy-Authorization: ${proxy.authorization}`] : []), + "", + "", + ]; + socket.write(headers.join("\r\n")); + let buffer = Buffer.alloc(0); + const onData = (chunk: Buffer) => { + buffer = Buffer.concat([buffer, chunk]); + const end = buffer.indexOf("\r\n\r\n"); + if (end < 0) { + if (buffer.length > 64 * 1024) finishError(new Error("upstream proxy CONNECT response header is too large")); return; } - established = true; - clearTimeout(handshakeTimer); - // If the proxy pipelined any early bytes after the 200, push - // them back so the TLS handshake sees them. - if (rest.length > 0) sock.unshift(rest); - resolve(sock); + socket.removeListener("data", onData); + const head = buffer.subarray(0, end).toString("latin1"); + const statusLine = head.split("\r\n", 1)[0] ?? ""; + if (!/^HTTP\/1\.[01]\s+2\d\d(?:\s|$)/.test(statusLine)) { + finishError(new Error(`upstream proxy CONNECT ${host}:${port} failed: ${statusLine}`)); + return; + } + settled = true; + clearTimeout(timer); + socket.removeListener("error", finishError); + const rest = buffer.subarray(end + 4); + if (rest.length > 0) socket.unshift(rest); + resolve(socket); }; - sock.on("data", onLine); - sock.once("error", cleanup); + socket.on("data", onData); }); }); } -/** Connect to `host:port` over TLS, through a proxy if configured. Used by the - * MITM path: after terminating the client's TLS, we re-establish a TLS - * connection to the REAL upstream. If a proxy is configured, the TLS runs - * over a CONNECT tunnel to that proxy. */ export async function connectTlsThroughProxy( host: string, port: number, proxyUrl: string | undefined, servername?: string, ): Promise { - const sock = await connectThroughProxy(host, port, proxyUrl); - return tls.connect({ - socket: sock, - servername: servername ?? host, - // Let Node pick ALPN; we must accept h2 here because the real upstream - // (api.openai.com etc.) may negotiate it and we forward raw bytes in - // the blind-tunnel path. The MITM-decrypt path forces http/1.1 on the - // CLIENT side but the UPSTREAM side can be anything. - }); + const socket = await connectThroughProxy(host, port, proxyUrl); + return tls.connect({ socket, servername: servername ?? host }); +} + +function errorChain(error: unknown): Array> { + const chain: Array> = []; + const seen = new Set(); + let current: unknown = error; + while (current && !seen.has(current)) { + seen.add(current); + if (current instanceof Error) { + const record: Record = { message: current.message }; + for (const key of ["code", "errno", "syscall", "address", "port"]) { + const value = (current as unknown as Record)[key]; + if (value !== undefined) record[key] = value; + } + chain.push(record); + current = current.cause; + } else { + chain.push({ message: String(current) }); + break; + } + } + return chain; +} + +export function formatUpstreamError(error: unknown, url: string, proxyUrl?: string): string { + const chain = errorChain(error); + const fields = ["code", "errno", "syscall", "address", "port"]; + const parts: string[] = []; + for (const field of fields) { + const value = chain.find((entry) => entry[field] !== undefined)?.[field]; + if (value !== undefined) parts.push(`${field}=${String(value)}`); + } + const messages = chain.map((entry) => String(entry.message ?? "")).filter(Boolean); + if (messages.length > 0) parts.push(`message=${messages.join(" <- ")}`); + parts.push(`url=${url}`); + parts.push(`proxy=${redactProxyUrl(proxyUrl) ?? "direct"}`); + return parts.join(" "); +} + +export function recordUpstreamConnection(url: string, proxyUrl: string | undefined, error?: unknown): void { + lastConnection = { + url, + proxy: redactProxyUrl(proxyUrl), + connected: error === undefined, + ...(error === undefined ? {} : { error: formatUpstreamError(error, url, proxyUrl) }), + checkedAt: new Date().toISOString(), + }; +} + +export function getUpstreamConnectionStatus(): UpstreamConnectionStatus { + return { ...lastConnection }; +} + +export function _resetUpstreamProxyForTest(): void { + resetProxyCache(); + lastConnection = {}; + windowsProxyCache = undefined; } diff --git a/src/web.ts b/src/web.ts deleted file mode 100644 index 87ff3efc86..0000000000 --- a/src/web.ts +++ /dev/null @@ -1,459 +0,0 @@ -import { readFileSync, writeFileSync, mkdirSync } from "node:fs"; -import { dirname, join } from "node:path"; -import { fileURLToPath } from "node:url"; -import type { IncomingMessage, ServerResponse } from "node:http"; -import { configFile } from "./paths.js"; -import { safeReadJson, parseRouteEntry, normalizeUrlKey, type ProviderRoute, type ProviderRoutes } from "./config.js"; -import { log } from "./logger.js"; - -function getVersion(): string { - try { - const here = fileURLToPath(import.meta.url); - const pkg = join(dirname(here), "..", "package.json"); - return (JSON.parse(readFileSync(pkg, "utf8")).version as string) ?? "dev"; - } catch { - return "dev"; - } -} - -/** Read the raw `providers` block from the config file (inline form only). - * Returns whatever is there, normalized to ProviderRoutes. */ -export function readProviders(): ProviderRoutes { - const parsed = safeReadJson(configFile()); - const routes: ProviderRoutes = {}; - if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) { - const providers = (parsed as { providers?: Record }).providers; - if (providers) { - for (const [k, v] of Object.entries(providers)) { - const route = parseRouteEntry(v); - if (route) routes[k] = route; - } - } - } - return routes; -} - -// ─── API handlers ─────────────────────────────────────────────────────────── - -export async function handleConfigGet(res: ServerResponse): Promise { - res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ path: configFile(), providers: readProviders() }, null, 2)); -} - -export async function handleConfigPut(req: IncomingMessage, res: ServerResponse): Promise { - const raw = await readJsonBody(req); - if (!raw || typeof raw !== "object") { - res.writeHead(400, { "content-type": "application/json" }); - res.end(JSON.stringify({ error: "expected JSON body" })); - return; - } - const body = raw as Record; - if (!body.providers || typeof body.providers !== "object" || Array.isArray(body.providers)) { - res.writeHead(400, { "content-type": "application/json" }); - res.end(JSON.stringify({ error: "expected JSON: { \"providers\": { ... } }" })); - return; - } - // Validate each route entry before touching the file. The KEY is the - // upstream URL (what the client puts after /bili/); the VALUE is {models}. - const routes: Record = {}; - for (const [url, val] of Object.entries(body.providers as Record)) { - if (!url || typeof url !== "string") { - res.writeHead(400, { "content-type": "application/json" }); - res.end(JSON.stringify({ error: `invalid provider key: ${JSON.stringify(url)}` })); - return; - } - const route = parseRouteEntry(val); - if (!route) { - res.writeHead(400, { "content-type": "application/json" }); - res.end(JSON.stringify({ error: `invalid provider "${url}": expected { "models": { ... } } or null (the key is the upstream URL)` })); - return; - } - routes[normalizeUrlKey(url)] = route; - } - // Merge into the existing file so we never clobber port/host/debug/etc. - // Write the normalized routes (validated + trailing slashes stripped) so - // the on-disk file stays clean regardless of what the client sent. - const existing = (safeReadJson(configFile()) ?? {}) as Record; - existing.providers = routes; - try { - mkdirSync(dirname(configFile()), { recursive: true }); - writeFileSync(configFile(), JSON.stringify(existing, null, 2) + "\n", "utf8"); - } catch (e) { - res.writeHead(500, { "content-type": "application/json" }); - res.end(JSON.stringify({ error: `failed to write: ${String(e)}` })); - return; - } - log("info", `[acp-web] providers updated via web UI (${Object.keys(routes).length} upstream URL(s)) — restart to apply`); - res.writeHead(200, { "content-type": "application/json" }); - res.end(JSON.stringify({ ok: true, count: Object.keys(routes).length, note: "restart bili to apply" })); -} - -function readJsonBody(req: IncomingMessage): Promise { - return new Promise((resolve) => { - const chunks: Buffer[] = []; - let size = 0; - req.on("data", (c: Buffer) => { - size += c.length; - if (size > 256 * 1024) { - req.destroy(); - resolve(undefined); - return; - } - chunks.push(c); - }); - req.on("end", () => { - try { - resolve(JSON.parse(Buffer.concat(chunks).toString("utf8"))); - } catch { - resolve(undefined); - } - }); - req.on("error", () => resolve(undefined)); - }); -} - -// ─── HTML UI ──────────────────────────────────────────────────────────────── - -export function renderUI(origin: string): string { - return HTML_UI.replace(/__ORIGIN__/g, origin).replace(/__VERSION__/g, getVersion()); -} - -// Single-page UI. No framework, no build step, no external assets. -// Escaped backticks/dollar-braces so the outer TS template literal is clean. -const HTML_UI = ` - - - - -billion-context - - - -
- -
v__VERSION__ · __ORIGIN__
- - - GitHub - -
- -
- -
- -
-
- -
-
- - -
-
- - -
-
- Auto-refreshes every 5s - -
-
-
-
-
- - - -`; diff --git a/src/web/api.ts b/src/web/api.ts new file mode 100644 index 0000000000..8bf69fbd89 --- /dev/null +++ b/src/web/api.ts @@ -0,0 +1,187 @@ +import { closeSync, existsSync, fsyncSync, mkdirSync, openSync, readFileSync, renameSync, unlinkSync, writeFileSync } from "node:fs"; +import { dirname } from "node:path"; +import { randomUUID } from "node:crypto"; +import type { IncomingMessage, ServerResponse } from "node:http"; +import { configFile } from "../paths.js"; +import { + loadRoutes, + normalizeUrlKey, + parseRouteEntry, + parseUpstreamProxyMode, + safeReadJson, + type ProviderRoute, + type ProviderRoutes, + type UpstreamProxyMode, +} from "../config.js"; +import { log } from "../logger.js"; +import { validateHttpProxy } from "../upstream-proxy.js"; +import { previewLegacyCodexHistory, repairLegacyCodexHistory } from "../codex-history.js"; + +type ConfigShape = Record & { + providers?: Record; + upstreamProxy?: string; + upstreamProxyMode?: string; +}; + +function readConfig(): ConfigShape { + const parsed = safeReadJson(configFile()); + return parsed && typeof parsed === "object" && !Array.isArray(parsed) ? parsed as ConfigShape : {}; +} + +export function readProviders(): ProviderRoutes { + return loadRoutes(); +} + +export function readUpstreamSettings(): { mode: UpstreamProxyMode; proxy?: string } { + const config = readConfig(); + const proxy = typeof config.upstreamProxy === "string" && config.upstreamProxy.trim() + ? config.upstreamProxy.trim() + : undefined; + return { + mode: parseUpstreamProxyMode(config.upstreamProxyMode ?? (proxy ? "manual" : undefined)), + ...(proxy ? { proxy } : {}), + }; +} + +function atomicWriteConfig(config: ConfigShape): void { + const filePath = configFile(); + mkdirSync(dirname(filePath), { recursive: true }); + const tempPath = `${filePath}.${process.pid}.${randomUUID()}.tmp`; + let descriptor: number | undefined; + try { + descriptor = openSync(tempPath, "wx", 0o600); + writeFileSync(descriptor, JSON.stringify(config, null, 2) + "\n", "utf8"); + fsyncSync(descriptor); + closeSync(descriptor); + descriptor = undefined; + renameSync(tempPath, filePath); + } catch (error) { + if (descriptor !== undefined) closeSync(descriptor); + try { unlinkSync(tempPath); } catch { } + throw error; + } +} + +export async function handleConfigGet(res: ServerResponse): Promise { + const upstream = readUpstreamSettings(); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ + path: configFile(), + providers: readProviders(), + upstreamProxy: upstream.proxy ?? null, + upstreamProxyMode: upstream.mode, + }, null, 2)); +} + +export async function handleConfigPut( + req: IncomingMessage, + res: ServerResponse, + onChanged?: () => void, + biliPort: number = 8787, +): Promise { + const raw = await readJsonBody(req); + if (!raw || typeof raw !== "object" || Array.isArray(raw)) return sendError(res, 400, "expected JSON object"); + const body = raw as Record; + const hasProviders = Object.prototype.hasOwnProperty.call(body, "providers"); + const hasProxy = Object.prototype.hasOwnProperty.call(body, "upstreamProxy"); + const hasMode = Object.prototype.hasOwnProperty.call(body, "upstreamProxyMode"); + if (!hasProviders && !hasProxy && !hasMode) return sendError(res, 400, "expected providers or upstream proxy settings"); + + const routes: Record = {}; + if (hasProviders) { + if (!body.providers || typeof body.providers !== "object" || Array.isArray(body.providers)) { + return sendError(res, 400, "providers must be an object"); + } + for (const [url, value] of Object.entries(body.providers as Record)) { + const route = parseRouteEntry(value); + if (!url || !route) return sendError(res, 400, `invalid provider entry: ${url || "(empty)"}`); + try { validateHttpProxy(route.proxy, biliPort); } catch (error) { + return sendError(res, 400, `invalid provider proxy for ${url}: ${String(error)}`); + } + routes[normalizeUrlKey(url)] = route; + } + } + + let proxy: string | undefined; + if (hasProxy) { + if (body.upstreamProxy !== null && typeof body.upstreamProxy !== "string") { + return sendError(res, 400, "upstreamProxy must be a string or null"); + } + proxy = typeof body.upstreamProxy === "string" ? body.upstreamProxy.trim() || undefined : undefined; + try { validateHttpProxy(proxy, biliPort); } catch (error) { + return sendError(res, 400, String(error)); + } + } + let mode: UpstreamProxyMode | undefined; + if (hasMode) { + if (typeof body.upstreamProxyMode !== "string" || !["auto", "manual", "direct"].includes(body.upstreamProxyMode)) { + return sendError(res, 400, "upstreamProxyMode must be auto, manual, or direct"); + } + mode = parseUpstreamProxyMode(body.upstreamProxyMode); + } + if (mode === "manual" && !proxy && !readUpstreamSettings().proxy) { + return sendError(res, 400, "manual mode requires an upstream proxy URL"); + } + + const config = readConfig(); + if (hasProviders) config.providers = routes; + if (hasProxy) { + if (proxy) config.upstreamProxy = proxy; + else delete config.upstreamProxy; + } + if (hasMode && mode) config.upstreamProxyMode = mode; + try { + atomicWriteConfig(config); + onChanged?.(); + } catch (error) { + return sendError(res, 500, `failed to apply config: ${String(error)}`); + } + log("info", `[acp-web] configuration updated (${hasProviders ? `${Object.keys(routes).length} routes` : "network only"})`); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, providers: hasProviders ? Object.keys(routes).length : undefined })); +} + +export async function handleCodexHistoryGet(res: ServerResponse): Promise { + try { + const preview = await previewLegacyCodexHistory(); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify(preview)); + } catch (error) { + sendError(res, 409, String(error)); + } +} + +export async function handleCodexHistoryRepair(res: ServerResponse): Promise { + try { + const result = await repairLegacyCodexHistory(); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify(result)); + } catch (error) { + sendError(res, 409, String(error)); + } +} + +function sendError(res: ServerResponse, status: number, message: string): void { + res.writeHead(status, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: message })); +} + +function readJsonBody(req: IncomingMessage): Promise { + return new Promise((resolve) => { + const chunks: Buffer[] = []; + let size = 0; + req.on("data", (chunk: Buffer) => { + size += chunk.length; + if (size > 256 * 1024) { + req.destroy(); + resolve(undefined); + return; + } + chunks.push(chunk); + }); + req.on("end", () => { + try { resolve(JSON.parse(Buffer.concat(chunks).toString("utf8"))); } catch { resolve(undefined); } + }); + req.on("error", () => resolve(undefined)); + }); +} diff --git a/src/web/client.ts b/src/web/client.ts new file mode 100644 index 0000000000..9c394686d2 --- /dev/null +++ b/src/web/client.ts @@ -0,0 +1,26 @@ +export const WEB_CLIENT = ` +const byId=(id)=>document.getElementById(id);let routeState="disabled"; +const escapeHtml=(value)=>String(value??"").replace(/[&<>"']/g,(char)=>({"&":"&","<":"<",">":">",'"':""","'":"'"})[char]); +function toast(message,error){const node=byId("toast");node.textContent=message;node.className="toast show"+(error?" err":"");setTimeout(()=>node.className="toast"+(error?" err":""),2600)} +function busy(button,on,label){if(!button)return;if(on){button.dataset.label=button.textContent;button.textContent=label||"处理中…";button.disabled=true;return}if(button.dataset.label){button.textContent=button.dataset.label;delete button.dataset.label}button.disabled=false} +async function json(url,options){const response=await fetch(url,options);const data=await response.json().catch(()=>({}));if(!response.ok)throw new Error(data.error||data.detail||("HTTP "+response.status));return data} +function showPage(name){document.querySelectorAll(".page").forEach((node)=>node.classList.toggle("active",node.id==="page-"+name));document.querySelectorAll(".nav button").forEach((node)=>node.classList.toggle("active",node.dataset.page===name));if(name==="sessions")loadSessions();if(name==="routing")loadRouting();if(name==="upstream"){loadUpstream();loadOverrides()}} +document.querySelectorAll(".nav button").forEach((button)=>button.addEventListener("click",()=>showPage(button.dataset.page))); +async function loadConfig(){const data=await json("/__bili/config");byId("providers-json").value=JSON.stringify(data.providers||{},null,2);byId("proxy-url").value=data.upstreamProxy||"";document.querySelectorAll('input[name="proxy-mode"]').forEach((input)=>input.checked=input.value===(data.upstreamProxyMode||"direct"));return data} +async function loadRouting(){try{const data=await json("/__bili/codex");routeState=data.state;const badge=byId("route-badge");const used=data.state==="enabled"&&data.lastRequestAt;badge.className="badge "+(data.state==="enabled"?(used?"ok":"warn"):data.state==="conflict"?"err":"");badge.textContent=data.state==="enabled"?(used?"使用中":"等待首次请求"):data.state==="conflict"?"配置冲突":"未启用";byId("route-provider").textContent=data.providerId||(data.provider&&data.provider.id)||"—";byId("route-real").textContent=data.originalBaseUrl||(data.provider&&data.provider.baseUrl)||"—";byId("route-local").textContent=data.baseUrl||"—";byId("route-detail").textContent=data.detail||(data.state==="enabled"&&!used?"若当前 Codex 尚未接入,可重新打开 Codex。":"");const button=byId("route-toggle");button.textContent=data.state==="disabled"?"启用 Codex 路由":"恢复原配置";button.className="btn "+(data.state==="disabled"?"primary":"danger");button.disabled=false}catch(error){byId("route-detail").textContent=String(error)}} +async function toggleRoute(){const button=byId("route-toggle");const action=routeState==="disabled"?"enable":"disable";busy(button,true,action==="enable"?"启用中…":"恢复中…");try{const data=await json("/__bili/codex/"+action,{method:"POST"});busy(button,false);await loadRouting();toast(data.detail||(action==="enable"?"Codex 路由已启用":"原配置已恢复"))}catch(error){toast(String(error),true)}finally{busy(button,false)}} +async function copyRoute(){const button=byId("copy-route");busy(button,true,"复制中…");try{await navigator.clipboard.writeText(byId("route-local").textContent);toast("路由地址已复制")}catch(error){toast(String(error),true)}finally{busy(button,false)}} +async function loadUpstream(){try{const data=await json("/__bili/upstream");const labels={"provider":"Provider 单独代理","provider-direct":"Provider 直连","bili-env":"BILI_UPSTREAM_PROXY","web-manual":"Web 手动代理","config":"配置文件代理","HTTPS_PROXY":"HTTPS_PROXY","HTTP_PROXY":"HTTP_PROXY","ALL_PROXY":"ALL_PROXY","windows-system":"Windows 系统代理","windows-bypass":"Windows 绕过列表","no-proxy":"NO_PROXY","direct":"直连"};byId("upstream-source").textContent=labels[data.source]||data.source||"直连";byId("upstream-effective").textContent=data.proxy||"direct";byId("upstream-pac").textContent=data.autoConfigUrl||"—";const state=byId("upstream-state");state.className="status "+(data.connected===true?"ok":data.connected===false?"err":"");state.textContent=data.connected===true?"CONNECT 正常":data.connected===false?(data.error||"连接失败"):"尚未观察到上游连接"}catch(error){byId("upstream-state").textContent=String(error)}} +async function saveUpstream(){const button=byId("save-upstream");busy(button,true,"保存中…");const mode=document.querySelector('input[name="proxy-mode"]:checked').value;try{await json("/__bili/config",{method:"PUT",headers:{"content-type":"application/json"},body:JSON.stringify({upstreamProxyMode:mode,upstreamProxy:byId("proxy-url").value.trim()||null})});toast("上游设置已热更新");await loadUpstream()}catch(error){toast(String(error),true)}finally{busy(button,false)}} +async function testUpstream(){const button=byId("test-upstream");busy(button,true,"测试中…");try{const data=await json("/__bili/upstream/test",{method:"POST"});toast("连接成功,HTTP "+data.status);await loadUpstream()}catch(error){toast(String(error),true);await loadUpstream()}finally{busy(button,false)}} +async function loadHistory(){try{const data=await json("/__bili/codex-history");byId("history-summary").textContent="当前分桶:"+data.targetProviderId+"\\n检测到旧版 bili 路由会话:"+data.sessions+"("+data.jsonlFiles+" 个 JSONL,"+data.stateRows+" 条 state DB)";byId("repair-history").disabled=data.sessions===0&&data.stateRows===0;if(data.stateDbError)byId("history-summary").textContent+="\\n"+data.stateDbError;return data}catch(error){byId("history-summary").textContent=String(error);throw error}} +async function previewHistory(){const button=byId("preview-history");busy(button,true,"预览中…");try{await loadHistory();toast("历史记录预览已刷新")}catch(error){toast(String(error),true)}finally{busy(button,false)}} +async function repairHistory(){const button=byId("repair-history");if(!confirm("将先备份,再把确认属于旧 bili 路由的会话迁移到当前 provider。继续?"))return;busy(button,true,"修复中…");let result;try{result=await json("/__bili/codex-history/repair",{method:"POST"});toast("历史记录已修复,备份:"+result.backupPath)}catch(error){toast(String(error),true)}finally{busy(button,false)}if(result)await loadHistory().catch(()=>{})} +async function saveProviders(){const button=byId("save-providers");busy(button,true,"保存中…");try{const providers=JSON.parse(byId("providers-json").value);await json("/__bili/config",{method:"PUT",headers:{"content-type":"application/json"},body:JSON.stringify({providers})});await json("/__bili/config/reload",{method:"POST"});toast("Provider 配置已热更新")}catch(error){toast(String(error),true)}finally{busy(button,false)}} +async function loadOverrides(){try{const data=await loadConfig();const providers=data.providers||{};const box=byId("upstream-overrides");const entries=Object.entries(providers);if(entries.length===0){box.innerHTML='

暂无 Provider。在"高级设置"页添加 Provider 后可在此按 URL 配置代理。

';return}box.innerHTML=''+entries.map(([url,route])=>'').join("")+'
Provider URL上游代理(空=继承全局)
'+escapeHtml(url)+'
'}catch(error){byId("upstream-overrides").textContent=String(error)}} +async function saveOverrides(){const button=byId("save-overrides");busy(button,true,"保存中…");try{const data=await loadConfig();const providers=data.providers||{};document.querySelectorAll(".override-proxy").forEach((input)=>{const url=input.dataset.url;if(!url)return;if(!providers[url])providers[url]={};const val=input.value.trim();if(val)providers[url].proxy=val;else delete providers[url].proxy});await json("/__bili/config",{method:"PUT",headers:{"content-type":"application/json"},body:JSON.stringify({providers})});await json("/__bili/config/reload",{method:"POST"});toast("按 URL 覆盖已热更新");await loadOverrides()}catch(error){toast(String(error),true)}finally{busy(button,false)}} +async function loadSessions(){try{const data=await json("/__bili/stats");const rows=(data.sessions||[]).map((item)=>""+escapeHtml(item.title||"—")+""+escapeHtml(item.protocol||"—")+""+escapeHtml(item.label||"—")+""+escapeHtml(item.requests)+""+escapeHtml(item.contextTokens)+""+escapeHtml(new Date(item.lastSeen).toLocaleString())+"").join("");byId("sessions-body").innerHTML=rows||'暂无会话'}catch(error){toast(String(error),true);throw error}} +async function refreshSessions(){const button=byId("refresh-sessions");busy(button,true,"刷新中…");try{await loadSessions();toast("会话列表已刷新")}catch{}finally{busy(button,false)}} +byId("route-toggle").addEventListener("click",toggleRoute);byId("copy-route").addEventListener("click",copyRoute);document.querySelectorAll(".copy-btn").forEach((btn)=>btn.addEventListener("click",async()=>{busy(btn,true,"复制中…");try{await navigator.clipboard.writeText(btn.dataset.copy||"");toast("已复制")}catch(e){toast(String(e),true)}finally{busy(btn,false)}}));byId("save-upstream").addEventListener("click",saveUpstream);byId("test-upstream").addEventListener("click",testUpstream);byId("preview-history").addEventListener("click",previewHistory);byId("repair-history").addEventListener("click",repairHistory);byId("save-providers").addEventListener("click",saveProviders);byId("save-overrides").addEventListener("click",saveOverrides);byId("refresh-sessions").addEventListener("click",refreshSessions); +Promise.all([loadConfig(),loadRouting(),loadUpstream(),loadHistory()]).catch((error)=>toast(String(error),true));setInterval(()=>{if(byId("page-sessions").classList.contains("active"))loadSessions().catch(()=>{})},5000); +`; diff --git a/src/web/index.ts b/src/web/index.ts new file mode 100644 index 0000000000..62e37b12a2 --- /dev/null +++ b/src/web/index.ts @@ -0,0 +1,27 @@ +import { readFileSync } from "node:fs"; +import { dirname, join } from "node:path"; +import { fileURLToPath } from "node:url"; +import { renderPage } from "./page.js"; + +export { + handleCodexHistoryGet, + handleCodexHistoryRepair, + handleConfigGet, + handleConfigPut, + readProviders, + readUpstreamSettings, +} from "./api.js"; + +function version(): string { + try { + const here = fileURLToPath(import.meta.url); + const packagePath = join(dirname(here), "..", "..", "package.json"); + return (JSON.parse(readFileSync(packagePath, "utf8")) as { version?: string }).version ?? "dev"; + } catch { + return "dev"; + } +} + +export function renderUI(origin: string): string { + return renderPage(origin, version()); +} diff --git a/src/web/page.ts b/src/web/page.ts new file mode 100644 index 0000000000..810be7a631 --- /dev/null +++ b/src/web/page.ts @@ -0,0 +1,25 @@ +import { WEB_CLIENT } from "./client.js"; +import { WEB_STYLES } from "./styles.js"; + +export function renderPage(origin: string, version: string): string { + return `billion-context +
+

概览

本地路由与上游网络相互独立,ACP 压缩运行在两者之间。

服务地址

${origin}

客户端接入

在"接入"页将各客户端(Codex / Claude Code / OpenCode / Pi 等)的流量路由到 bili。

+

客户端接入

两类接入:A. API-key 客户端加 /bili/ 前缀(零配置);B. 订阅/登录客户端用 MITM 或路由接管。同一客户端两种方式都可能用到。

+

A. API-key 客户端(/bili/ 前缀)

+

在客户端配置里把原 baseURL 前加上 ${origin}/bili/ 即可,其余不变。上下文窗口默认按模型名匹配内置表,可在「设置」页的 providers JSON 里按 URL + 模型精确覆盖。

+

OpenCode

配置文件
~/.config/opencode/opencode.json
baseURL
${origin}/bili/https://open.bigmodel.cn/api/coding/paas/v4
+

Codex(API key)

配置文件
~/.codex/config.toml
base_url
${origin}/bili/https://api.openai.com/v1
+

Pi

配置文件
~/.pi/agent/models.json
baseUrl
${origin}/bili/https://api.anthropic.com
+

Claude Code(API key)

环境变量
ANTHROPIC_BASE_URL=${origin}/bili/https://api.anthropic.com
+

通用(Cursor / Aider / Continue)

规则
任意 baseURL 前加 ${origin}/bili/
示例
${origin}/bili/https://api.openai.com/v1
+

B. 订阅/登录客户端(MITM / 路由接管)

+

硬编码 endpoint 的 OAuth 客户端无法改 baseURL,需 MITM 透明代理或路由接管。

+

Codex(ChatGPT 订阅)

加载中
方式
本地路由接管(自动改写 config.toml)
当前 Provider
—
真实端点
—
路由端点
—
+

Claude Code(订阅)

MITM
方式
MITM 透明代理(拦截 HTTPS)
HTTP Proxy
${origin}
环境变量
HTTPS_PROXY=${origin}
NODE_EXTRA_CA_CERTS=~/.local/share/billion-context/ca/root-ca.pem
+

ZCode(编程套餐)

MITM
方式
MITM 透明代理
设置
Settings → Network → HTTP Proxy = ${origin}
CA 证书
~/.local/share/billion-context/ca/root-ca.pem
+

Codex 历史记录

加载中
+

上游网络

控制 bili 如何访问真实 Provider,不会改变客户端的本地路由地址。

全局代理

当前来源
直连
有效代理
direct
系统 PAC
—
状态
尚未测试

按 URL 覆盖

为特定 Provider 单独设置上游代理,覆盖全局设置。留空 = 继承全局。

加载中
+

会话

ACP 压缩状态与上游用量。

标题协议标识请求上下文最后活动
+

高级设置

直接编辑 providers JSON(添加新 Provider、配置模型上下文窗口、按 URL 代理等);保存后立即热更新。

`; +} diff --git a/src/web/styles.ts b/src/web/styles.ts new file mode 100644 index 0000000000..075bb91355 --- /dev/null +++ b/src/web/styles.ts @@ -0,0 +1,6 @@ +export const WEB_STYLES = ` +:root{--bg:#0f1115;--panel:#171a21;--panel2:#1e222c;--line:#2b313d;--text:#e7eaf0;--muted:#8f98a8;--blue:#6ea8fe;--green:#55c98b;--yellow:#e9b949;--red:#ef6b73;--radius:10px} +*{box-sizing:border-box}body{margin:0;background:var(--bg);color:var(--text);font:14px/1.5 -apple-system,BlinkMacSystemFont,"Segoe UI",sans-serif;min-height:100vh}.app{display:grid;grid-template-columns:220px 1fr;min-height:100vh}.sidebar{border-right:1px solid var(--line);padding:22px 14px;background:#12151b;position:sticky;top:0;height:100vh}.brand{font-size:18px;font-weight:700;padding:0 10px 22px}.brand span{color:var(--blue)}.version{display:block;color:var(--muted);font-size:11px;font-weight:400;margin-top:2px}.nav{display:grid;gap:4px}.nav button{appearance:none;border:0;background:transparent;color:var(--muted);text-align:left;padding:10px 12px;border-radius:7px;cursor:pointer;font:inherit}.nav button:hover,.nav button.active{background:var(--panel2);color:var(--text)}.main{padding:32px;max-width:1050px;width:100%}.page{display:none}.page.active{display:block}h1{font-size:23px;margin:0 0 4px}h2{font-size:15px;margin:0}.lead{color:var(--muted);margin:0 0 24px}.grid{display:grid;grid-template-columns:repeat(2,minmax(0,1fr));gap:14px}.card{background:var(--panel);border:1px solid var(--line);border-radius:var(--radius);padding:18px;margin-bottom:14px}.card-head{display:flex;justify-content:space-between;align-items:center;margin-bottom:16px}.badge{border-radius:99px;padding:3px 9px;font-size:12px;background:var(--panel2);color:var(--muted)}.badge.ok{color:var(--green)}.badge.warn{color:var(--yellow)}.badge.err{color:var(--red)}.kv{display:grid;grid-template-columns:135px 1fr;gap:10px 14px;margin:0}.kv dt{color:var(--muted)}.kv dd{margin:0;word-break:break-all}.mono{font-family:"Cascadia Code",Consolas,monospace;font-size:12px}.actions{display:flex;gap:9px;flex-wrap:wrap;margin-top:18px}.btn{border:1px solid var(--line);background:var(--panel2);color:var(--text);border-radius:7px;padding:8px 13px;cursor:pointer;font:inherit}.btn:hover{border-color:#4b5668}.btn.primary{background:var(--blue);border-color:var(--blue);color:#0b1220;font-weight:600}.btn.danger{color:var(--red)}.btn:disabled{opacity:.55;cursor:wait}.field{display:grid;gap:6px;margin:12px 0}.field label{color:var(--muted);font-size:12px}input,textarea,select{width:100%;background:#101319;color:var(--text);border:1px solid var(--line);border-radius:7px;padding:9px 10px;font:inherit}textarea{min-height:250px;font-family:"Cascadia Code",Consolas,monospace;font-size:12px;resize:vertical}.modes{display:flex;gap:18px;margin:12px 0}.modes label{display:flex;gap:7px;align-items:center;color:var(--muted)}.modes input{width:auto}.notice{border-left:3px solid var(--yellow);padding:10px 12px;background:rgba(233,185,73,.08);color:#d8c185;margin-top:14px}.status{color:var(--muted);white-space:pre-wrap}.status.ok{color:var(--green)}.status.err{color:var(--red)}table{width:100%;border-collapse:collapse}th,td{text-align:left;border-bottom:1px solid var(--line);padding:9px 8px;font-size:12px}th{color:var(--muted);font-weight:500}.toast{position:fixed;right:24px;bottom:24px;background:var(--green);color:#07140d;padding:10px 15px;border-radius:8px;opacity:0;transform:translateY(8px);transition:.2s;pointer-events:none}.toast.show{opacity:1;transform:none}.toast.err{background:var(--red);color:#1b0608} +.section-title{font-size:17px;margin:28px 0 4px}.hint{color:var(--muted);margin:0 0 14px;font-size:13px}.btn.small{padding:4px 10px;font-size:12px} +@media(max-width:760px){.app{grid-template-columns:1fr}.sidebar{height:auto;position:static;border-right:0;border-bottom:1px solid var(--line)}.nav{display:flex;overflow:auto}.main{padding:20px}.grid{grid-template-columns:1fr}.kv{grid-template-columns:110px 1fr}} +`; diff --git a/tests/codex-history.test.ts b/tests/codex-history.test.ts new file mode 100644 index 0000000000..9444d55a0e --- /dev/null +++ b/tests/codex-history.test.ts @@ -0,0 +1,95 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { previewLegacyCodexHistory, repairLegacyCodexHistory } from "../src/codex-history.ts"; + +function fixture(): { root: string; configPath: string; sessionPath: string } { + const root = path.join(tmpdir(), `bili-history-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`); + const codex = path.join(root, "codex"); + const sessions = path.join(codex, "sessions", "2026", "08", "09"); + mkdirSync(sessions, { recursive: true }); + const configPath = path.join(codex, "config.toml"); + writeFileSync(configPath, [ + 'model_provider = "openai_http"', + "[model_providers.openai_http]", + 'name = "OpenAI HTTP"', + 'base_url = "https://chatgpt.com/backend-api/codex"', + 'wire_api = "responses"', + "requires_openai_auth = true", + "supports_websockets = false", + ].join("\n"), "utf8"); + return { root, configPath, sessionPath: path.join(sessions, "rollout-test.jsonl") }; +} + +test("legacy Codex history repair is preview-only until explicitly requested and keeps unrelated buckets", async () => { + const files = fixture(); + const lines = [ + { timestamp: "1", type: "session_meta", payload: { id: "old-1", model_provider: "billion-context-codex", cwd: "C:/work" } }, + { timestamp: "2", type: "response_item", payload: { text: "keep" } }, + { timestamp: "3", type: "session_meta", payload: { id: "custom-1", model_provider: "cc-switch-custom" } }, + ]; + const original = lines.map((line) => JSON.stringify(line)).join("\n") + "\n"; + writeFileSync(files.sessionPath, original, "utf8"); + const unrelatedPath = path.join(path.dirname(files.configPath), "unrelated.jsonl"); + const unrelated = JSON.stringify({ type: "session_meta", payload: { model_provider: "bili_chatgpt" } }) + "\n"; + writeFileSync(unrelatedPath, unrelated, "utf8"); + const oldData = process.env.XDG_DATA_HOME; + process.env.XDG_DATA_HOME = path.join(files.root, "data"); + try { + const preview = await previewLegacyCodexHistory(files.configPath); + assert.equal(preview.targetProviderId, "openai_http"); + assert.equal(preview.sessions, 1); + assert.equal(preview.jsonlFiles, 1); + assert.equal(readFileSync(files.sessionPath, "utf8"), original, "preview must not modify history"); + + const result = await repairLegacyCodexHistory(files.configPath); + assert.equal(result.migratedJsonlFiles, 1); + assert.ok(existsSync(result.backupPath)); + const repaired = readFileSync(files.sessionPath, "utf8").trim().split("\n").map((line) => JSON.parse(line) as Record); + assert.equal((repaired[0].payload as Record).model_provider, "openai_http"); + assert.equal((repaired[2].payload as Record).model_provider, "cc-switch-custom"); + assert.equal(readFileSync(unrelatedPath, "utf8"), unrelated); + const backup = path.join(result.backupPath, "jsonl", path.relative(path.dirname(files.configPath), files.sessionPath)); + assert.equal(readFileSync(backup, "utf8"), original); + } finally { + if (oldData === undefined) delete process.env.XDG_DATA_HOME; else process.env.XDG_DATA_HOME = oldData; + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("legacy repair updates state_5.sqlite when node:sqlite is available", async (context) => { + let sqlite: typeof import("node:sqlite"); + try { + sqlite = await import("node:sqlite"); + } catch { + context.skip("node:sqlite is unavailable on this Node runtime"); + return; + } + const files = fixture(); + const dbPath = path.join(path.dirname(files.configPath), "state_5.sqlite"); + const database = new sqlite.DatabaseSync(dbPath); + database.exec("CREATE TABLE threads (id TEXT PRIMARY KEY, model_provider TEXT NOT NULL)"); + database.prepare("INSERT INTO threads (id, model_provider) VALUES (?, ?)").run("old", "bili_chatgpt"); + database.prepare("INSERT INTO threads (id, model_provider) VALUES (?, ?)").run("keep", "other-provider"); + database.close(); + const oldData = process.env.XDG_DATA_HOME; + process.env.XDG_DATA_HOME = path.join(files.root, "data"); + try { + const preview = await previewLegacyCodexHistory(files.configPath); + assert.equal(preview.stateRows, 1); + const result = await repairLegacyCodexHistory(files.configPath); + assert.equal(result.migratedStateRows, 1); + const verify = new sqlite.DatabaseSync(dbPath); + try { + assert.equal(verify.prepare("SELECT model_provider FROM threads WHERE id = ?").get("old")?.model_provider, "openai_http"); + assert.equal(verify.prepare("SELECT model_provider FROM threads WHERE id = ?").get("keep")?.model_provider, "other-provider"); + } finally { + verify.close(); + } + } finally { + if (oldData === undefined) delete process.env.XDG_DATA_HOME; else process.env.XDG_DATA_HOME = oldData; + rmSync(files.root, { recursive: true, force: true }); + } +}); diff --git a/tests/codex-official.test.ts b/tests/codex-official.test.ts new file mode 100644 index 0000000000..01f6dbcf48 --- /dev/null +++ b/tests/codex-official.test.ts @@ -0,0 +1,189 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { gzipSync } from "node:zlib"; +import { createInitialState, defaultConfig } from "acp-kernel"; +import { startServer, isChatGptCodexUpstream, isCodexResponsesLite, shouldInjectPromptCacheKey, resolvePromptCacheKey } from "../src/server.ts"; +import { listSessions } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +type Captured = { url: string; headers: http.IncomingHttpHeaders; body: Buffer }; + +function listen(server: http.Server): Promise { + if (server.listening) return Promise.resolve(); + return once(server, "listening").then(() => undefined); +} + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve())); +} + +test("Codex official transport preserves OAuth headers, decodes bodies, and rebases after compact", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const captured: Captured[] = []; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + captured.push({ url: req.url ?? "", headers: req.headers, body: Buffer.concat(chunks) }); + if (req.url?.includes("fail=1")) { + res.writeHead(500, { "content-type": "application/json" }); + res.end(JSON.stringify({ error: "compact failed" })); + return; + } + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "resp_test", status: "completed", output: [], usage: { input_tokens: 10, output_tokens: 1 } })); + }); + }); + upstream.listen(0, "127.0.0.1"); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + const opts: ProxyOptions = { + port: 0, + host: "127.0.0.1", + upstream: "http://127.0.0.1", + routes: { + [`http://127.0.0.1:${upstreamPort}`]: { models: { "gpt-5": { context: 400_000 } } }, + }, + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + await listen(proxy); + const proxyPort = (proxy.address() as { port: number }).port; + const base = `http://127.0.0.1:${proxyPort}/bili/http://127.0.0.1:${upstreamPort}`; + const sessionId = "019fdc81-a420-7a00-bbd1-0a64e3eb772c"; + const requestBody = { + model: "gpt-5", + stream: false, + instructions: "keep native Codex instructions", + additional_tools: [{ type: "custom", name: "apply_patch", vendor: { keep: true } }], + tools: [{ type: "custom", name: "shell", format: { type: "grammar", syntax: "lark" } }], + input: [ + { type: "additional_tools", tools: [{ type: "custom", name: "exec" }] }, + { type: "reasoning", id: "r1", encrypted_content: "ciphertext" }, + { type: "message", id: "m1", status: "completed", role: "user", content: [ + { type: "input_text", text: "hello" }, + { type: "input_image", image_url: "data:image/png;base64,AA==", detail: "high" }, + ] }, + ], + }; + try { + const first = await fetch(`${base}/responses`, { + method: "POST", + headers: { + authorization: "Bearer AbCdEf", + "chatgpt-account-id": "account-123", + "session-id": sessionId, + "x-openai-internal-codex-responses-lite": "1", + "content-encoding": "gzip", + "content-type": "application/json", + }, + body: gzipSync(Buffer.from(JSON.stringify(requestBody))), + }); + assert.equal(first.status, 200); + await first.arrayBuffer(); + assert.equal(captured[0].url, "/responses"); + assert.equal(captured[0].headers.authorization, "Bearer AbCdEf"); + assert.equal(captured[0].headers["chatgpt-account-id"], "account-123"); + assert.equal(captured[0].headers["content-encoding"], undefined); + assert.equal(captured[0].headers["session-id"], sessionId); + assert.equal(captured[0].headers["x-session-id"], undefined); + const forwarded = JSON.parse(captured[0].body.toString("utf8")) as { + input: Array>; + prompt_cache_key?: string; + instructions?: string; + additional_tools: unknown[]; + tools: unknown[]; + }; + assert.deepEqual(forwarded.input.map((item) => item.type), ["additional_tools", "message", "reasoning", "message"]); + assert.match(String(forwarded.input[1].content), /acp_compress/); + assert.match(String(forwarded.input[1].content), /keep native Codex instructions/); + assert.equal(forwarded.input[2].encrypted_content, "ciphertext"); + assert.equal((forwarded.input[3].content as Array>)[1].type, "input_image"); + assert.equal(forwarded.prompt_cache_key, undefined); + assert.equal(forwarded.instructions, undefined); + assert.deepEqual(forwarded.additional_tools, requestBody.additional_tools); + assert.deepEqual(forwarded.tools, requestBody.tools); + + const session = listSessions().find((candidate) => candidate.meta.label === sessionId); + assert.ok(session); + session.state.nextBlockId = 7; + session.blockContents.set("b1", { one: { text: "one", count: 1 }, full: { text: "full", count: 1 } }); + const compact = await fetch(`${base}/responses/compact`, { + method: "POST", + headers: { + authorization: "Bearer AbCdEf", + "chatgpt-account-id": "account-123", + "session-id": sessionId, + "content-type": "application/json", + }, + body: JSON.stringify({ model: "gpt-5" }), + }); + assert.equal(compact.status, 200); + await compact.arrayBuffer(); + assert.equal(captured[1].url, "/responses/compact"); + assert.deepEqual(JSON.parse(captured[1].body.toString("utf8")), { model: "gpt-5" }); + assert.equal(session.state.nextBlockId, 7); + assert.equal(session.blockContents.size, 1); + assert.equal((session.metadata.nativeCompactionBoundary as Record).pendingRebase, true); + + const afterCompact = await fetch(`${base}/responses`, { + method: "POST", + headers: { + authorization: "Bearer AbCdEf", + "chatgpt-account-id": "account-123", + "session-id": sessionId, + "x-openai-internal-codex-responses-lite": "1", + "content-type": "application/json", + }, + body: JSON.stringify(requestBody), + }); + assert.equal(afterCompact.status, 200); + await afterCompact.arrayBuffer(); + assert.equal(session.state.nextBlockId, createInitialState().nextBlockId); + assert.equal(session.blockContents.size, 0); + assert.equal((session.metadata.nativeCompactionBoundary as Record).pendingRebase, false); + + session.state.nextBlockId = 8; + const failedCompact = await fetch(`${base}/responses/compact?fail=1`, { + method: "POST", + headers: { authorization: "Bearer AbCdEf", "session-id": sessionId, "content-type": "application/json" }, + body: JSON.stringify({ model: "gpt-5" }), + }); + assert.equal(failedCompact.status, 500); + await failedCompact.arrayBuffer(); + assert.equal(session.state.nextBlockId, 8); + } finally { + await close(proxy); + await close(upstream); + } +}); + +test("Codex official profile uses text protocol and prompt-cache auto stays native", () => { + assert.equal(isChatGptCodexUpstream("https://chatgpt.com"), true); + assert.equal(shouldInjectPromptCacheKey("auto", "https://chatgpt.com"), false); + assert.equal(shouldInjectPromptCacheKey("auto", "https://api.openai.com"), true); + assert.equal(shouldInjectPromptCacheKey("enabled", "https://strict.example"), true); + assert.equal(shouldInjectPromptCacheKey("disabled", "https://api.openai.com"), false); + const identity = { value: "real-session", source: "body-session", clientProvided: true } as const; + assert.equal(resolvePromptCacheKey("explicit-key", identity, "enabled", "https://api.openai.com"), "explicit-key"); + assert.equal(resolvePromptCacheKey(undefined, identity, "auto", "https://api.openai.com"), "real-session"); + assert.equal(resolvePromptCacheKey(undefined, { ...identity, clientProvided: false }, "enabled", "https://api.openai.com"), undefined); + assert.equal(isCodexResponsesLite({ "x-openai-internal-codex-responses-lite": "1" }, { input: [] }), true); + assert.equal(isCodexResponsesLite({}, { input: [], additional_tools: [] }), true); + assert.equal(isCodexResponsesLite({}, { input: [{ type: "additional_tools", tools: [] }] }), true); + assert.equal(isCodexResponsesLite({}, { input: [] }), false); +}); diff --git a/tests/codex-takeover.test.ts b/tests/codex-takeover.test.ts new file mode 100644 index 0000000000..8e6244a4f7 --- /dev/null +++ b/tests/codex-takeover.test.ts @@ -0,0 +1,253 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { existsSync, mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { + disableCodexTakeover, + enableCodexTakeover, + getCodexTakeoverStatus, + recoverStaleCodexTakeover, +} from "../src/codex-takeover.ts"; +import { resolveActiveCodexProvider } from "../src/codex-provider.ts"; + +function fixture(): { root: string; configPath: string; statePath: string; authPath: string } { + const root = path.join(tmpdir(), `bili-codex-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`); + const codex = path.join(root, ".codex"); + mkdirSync(codex, { recursive: true }); + return { + root, + configPath: path.join(codex, "config.toml"), + statePath: path.join(root, "data", "codex-route-state.json"), + authPath: path.join(codex, "auth.json"), + }; +} + +test("Codex route patches the active provider in place and restores byte-for-byte", () => { + const files = fixture(); + const original = "\uFEFF" + [ + 'model = "gpt-5.4"', + 'model_provider = "openai_http"', + "", + "[model_providers.openai_http]", + 'name = "OpenAI HTTP"', + "base_url = 'https://chatgpt.com/backend-api/codex' # keep comment", + 'wire_api = "responses"', + "requires_openai_auth = true", + "supports_websockets = false", + "", + "[mcp_servers.keep]", + 'command = "keep"', + "", + ].join("\r\n"); + const auth = '{"tokens":{"access_token":"secret"}}\n'; + writeFileSync(files.configPath, original, "utf8"); + writeFileSync(files.authPath, auth, "utf8"); + try { + const provider = resolveActiveCodexProvider(files.configPath); + assert.equal(provider.id, "openai_http"); + assert.equal(provider.baseUrl, "https://chatgpt.com/backend-api/codex"); + const enabled = enableCodexTakeover(8787, files); + assert.equal(enabled.state, "enabled"); + assert.equal(enabled.providerId, "openai_http"); + const active = readFileSync(files.configPath, "utf8"); + assert.match(active, /model_provider = "openai_http"/); + assert.match(active, /base_url = "http:\/\/127\.0\.0\.1:8787\/codex" # keep comment/); + assert.doesNotMatch(active, /billion-context-codex/); + assert.equal(readFileSync(files.authPath, "utf8"), auth); + disableCodexTakeover(files); + assert.equal(readFileSync(files.configPath, "utf8"), original); + assert.equal(readFileSync(files.authPath, "utf8"), auth); + assert.equal(existsSync(files.statePath), false); + } finally { + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("built-in openai takeover keeps model_provider absent and removes its temporary section", () => { + const files = fixture(); + const original = 'model = "gpt-5.4"\n[mcp_servers.keep]\ncommand = "keep"'; + writeFileSync(files.configPath, original, "utf8"); + try { + const enabled = enableCodexTakeover(9123, files); + assert.equal(enabled.providerId, "openai"); + const active = readFileSync(files.configPath, "utf8"); + assert.doesNotMatch(active, /^\s*model_provider\s*=/m); + assert.match(active, /\[model_providers\.openai\]/); + assert.match(active, /base_url = "http:\/\/127\.0\.0\.1:9123\/codex"/); + disableCodexTakeover(files); + assert.equal(readFileSync(files.configPath, "utf8"), original); + } finally { + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("restore uses a three-way comparison and preserves user endpoint edits", () => { + const files = fixture(); + writeFileSync(files.configPath, [ + 'model_provider = "relay"', + "[model_providers.relay]", + 'name = "Relay"', + 'base_url = "https://relay.example/v1"', + 'wire_api = "responses"', + "requires_openai_auth = true", + "supports_websockets = true", + "", + ].join("\n"), "utf8"); + try { + enableCodexTakeover(8787, files); + const edited = readFileSync(files.configPath, "utf8").replace( + 'base_url = "http://127.0.0.1:8787/codex"', + 'base_url = "https://user-new.example/v1"', + ); + writeFileSync(files.configPath, edited, "utf8"); + const restored = disableCodexTakeover(files); + assert.match(restored.detail ?? "", /检测到用户修改,已保留:base_url/); + const final = readFileSync(files.configPath, "utf8"); + assert.match(final, /base_url = "https:\/\/user-new\.example\/v1"/); + assert.match(final, /supports_websockets = true/); + } finally { + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("takeover refuses an unresolved custom active provider", () => { + const files = fixture(); + writeFileSync(files.configPath, 'model_provider = "missing"\n', "utf8"); + try { + assert.throws(() => enableCodexTakeover(8787, files), /cannot safely resolve active Codex provider/); + assert.equal(getCodexTakeoverStatus(files).state, "disabled"); + } finally { + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("stale process ownership is recovered on the next bili start", () => { + const files = fixture(); + const original = ""; + writeFileSync(files.configPath, original, "utf8"); + try { + enableCodexTakeover(8787, { ...files, ownerPid: 2_000_000_000 }); + const recovered = recoverStaleCodexTakeover(files); + assert.equal(recovered.state, "disabled"); + assert.equal(readFileSync(files.configPath, "utf8"), original); + } finally { + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("a running bili owner cannot be displaced by another process", () => { + const files = fixture(); + writeFileSync(files.configPath, "", "utf8"); + try { + enableCodexTakeover(8787, { ...files, ownerPid: process.pid }); + assert.throws( + () => enableCodexTakeover(9999, { ...files, ownerPid: process.pid + 1 }), + /owned by running bili process/, + ); + assert.equal(getCodexTakeoverStatus(files).port, 8787); + } finally { + disableCodexTakeover(files); + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("comments added to a temporary built-in provider section survive restore", () => { + const files = fixture(); + writeFileSync(files.configPath, 'model = "gpt-5.4"\n', "utf8"); + try { + enableCodexTakeover(8787, files); + const active = readFileSync(files.configPath, "utf8").replace( + "[model_providers.openai]\n", + "[model_providers.openai]\n# user note added while routed\n", + ); + writeFileSync(files.configPath, active, "utf8"); + disableCodexTakeover(files); + const restored = readFileSync(files.configPath, "utf8"); + assert.match(restored, /\[model_providers\.openai\]\n# user note added while routed/); + assert.doesNotMatch(restored, /base_url|supports_websockets/); + } finally { + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("takeover rejects a provider base URL with query or fragment routing ambiguity", () => { + const files = fixture(); + writeFileSync(files.configPath, [ + 'model_provider = "relay"', + "[model_providers.relay]", + 'base_url = "https://relay.example/v1?tenant=unsafe"', + 'wire_api = "responses"', + "requires_openai_auth = true", + "supports_websockets = false", + ].join("\n"), "utf8"); + try { + assert.throws(() => enableCodexTakeover(8787, files), /cannot contain a query string or fragment/); + } finally { + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("custom provider without optional auth/websocket fields resolves with safe defaults", () => { + const files = fixture(); + writeFileSync(files.configPath, [ + 'model_provider = "my-relay"', + "[model_providers.my-relay]", + 'name = "my-relay"', + 'base_url = "http://127.0.0.1:8787/bili/https://relay.example/v1"', + 'wire_api = "responses"', + 'env_key = "OPENAI_API_KEY"', + ].join("\n"), "utf8"); + try { + const provider = resolveActiveCodexProvider(files.configPath); + assert.equal(provider.id, "my-relay"); + assert.equal(provider.requiresOpenaiAuth, false, "defaults to false when unspecified"); + assert.equal(provider.supportsWebsockets, false, "defaults to false when unspecified"); + assert.equal(provider.baseUrl, "http://127.0.0.1:8787/bili/https://relay.example/v1"); + } finally { + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("corrupted state file degrades gracefully instead of throwing", () => { + const files = fixture(); + const configContent = '[model_providers.openai]\nname = "OpenAI"\nbase_url = "https://api.openai.com/v1"\n'; + writeFileSync(files.configPath, configContent, "utf8"); + mkdirSync(path.dirname(files.statePath), { recursive: true }); + writeFileSync(files.statePath, "{garbage not valid json", "utf8"); + try { + assert.doesNotThrow(() => getCodexTakeoverStatus(files), "corrupt state must not throw"); + assert.equal(getCodexTakeoverStatus(files).state, "disabled"); + assert.equal(readFileSync(files.configPath, "utf8"), configContent, "config.toml left intact"); + } finally { + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("non-numeric sectionPrefixLength in state is rejected without wiping config.toml", () => { + const files = fixture(); + const configContent = '[model_providers.openai]\nname = "OpenAI"\nbase_url = "https://api.openai.com/v1"\n'; + writeFileSync(files.configPath, configContent, "utf8"); + mkdirSync(path.dirname(files.statePath), { recursive: true }); + writeFileSync(files.statePath, JSON.stringify({ + version: 1, + active: true, + providerId: "openai", + configPath: files.configPath, + sectionAdded: false, + sectionPrefixLength: "not-a-number", + insertedFields: ["base_url"], + originalFields: { base_url: "https://api.openai.com/v1" }, + original: { baseUrl: "https://api.openai.com/v1", supportsWebsockets: false }, + installed: { baseUrl: "http://127.0.0.1:8787", supportsWebsockets: false }, + configHashBefore: "abc", + startedAt: new Date().toISOString(), + }), "utf8"); + try { + assert.doesNotThrow(() => getCodexTakeoverStatus(files)); + assert.equal(getCodexTakeoverStatus(files).state, "disabled", "invalid sectionPrefixLength rejects state"); + assert.equal(readFileSync(files.configPath, "utf8"), configContent, "config.toml not wiped"); + } finally { + rmSync(files.root, { recursive: true, force: true }); + } +}); diff --git a/tests/content-encoding.test.ts b/tests/content-encoding.test.ts new file mode 100644 index 0000000000..88e116d7ab --- /dev/null +++ b/tests/content-encoding.test.ts @@ -0,0 +1,29 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import { brotliCompressSync, gzipSync } from "node:zlib"; +import { decodeRequestBody } from "../src/content-encoding.ts"; + +const JSON_BODY = Buffer.from('{"model":"gpt-5","input":"hello"}'); + +test("decodeRequestBody leaves identity requests untouched", async () => { + const decoded = await decodeRequestBody(undefined, JSON_BODY, 1024); + assert.equal(decoded.body, JSON_BODY); + assert.equal(decoded.decoded, false); +}); + +test("decodeRequestBody supports gzip and stacked codings", async () => { + const gzip = gzipSync(JSON_BODY); + assert.deepEqual((await decodeRequestBody("gzip", gzip, 1024)).body, JSON_BODY); + const stacked = brotliCompressSync(gzip); + assert.deepEqual((await decodeRequestBody("gzip, br", stacked, 1024)).body, JSON_BODY); +}); + +test("decodeRequestBody supports Codex Desktop zstd bodies on Node 20+", async () => { + const zstd = Buffer.from("KLUv/SAhCQEAeyJtb2RlbCI6ImdwdC01IiwiaW5wdXQiOiJoZWxsbyJ9", "base64"); + assert.deepEqual((await decodeRequestBody("zstd", zstd, 1024)).body, JSON_BODY); +}); + +test("decodeRequestBody rejects unsupported encodings and oversized output", async () => { + await assert.rejects(() => decodeRequestBody("snappy", Buffer.from("x"), 1024), /unsupported/); + await assert.rejects(() => decodeRequestBody("gzip", gzipSync(Buffer.alloc(4096)), 128)); +}); diff --git a/tests/mitm.test.ts b/tests/mitm.test.ts index 8891b9c182..42bfff8f88 100644 --- a/tests/mitm.test.ts +++ b/tests/mitm.test.ts @@ -74,8 +74,10 @@ await test("ensureRootCA: generates a PEM cert + key on first call", async () => const keyPem = fs.readFileSync(keyPath, "utf8"); assert.ok(keyPem.includes("BEGIN PRIVATE KEY") || keyPem.includes("BEGIN RSA PRIVATE KEY")); // Key must NOT be world-readable. - const mode = (fs.statSync(keyPath).mode & 0o777); - assert.equal(mode & 0o077, 0, `key file mode ${mode.toString(8)} leaks group/other bits`); + if (process.platform !== "win32") { + const mode = (fs.statSync(keyPath).mode & 0o777); + assert.equal(mode & 0o077, 0, `key file mode ${mode.toString(8)} leaks group/other bits`); + } }); }); diff --git a/tests/proxy-persist.test.ts b/tests/proxy-persist.test.ts index da2541cff5..39f31411b8 100644 --- a/tests/proxy-persist.test.ts +++ b/tests/proxy-persist.test.ts @@ -2,7 +2,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtempSync, rmSync, readdirSync, readFileSync, statSync } from "node:fs"; import { tmpdir } from "node:os"; -import { join } from "node:path"; +import { join, relative, sep } from "node:path"; import { SessionStore } from "../src/persist.ts"; import { createInitialState } from "acp-kernel"; import type { Session, BlockContent } from "../src/session.ts"; @@ -241,7 +241,7 @@ await withTempStore("sessions are namespaced by protocol + provider on disk", as const all = jsonFilesUnder(dir); assert.equal(all.length, 3, "three sessions written"); - const rel = all.map((f) => f.slice(dir.length + 1)); + const rel = all.map((f) => relative(dir, f).split(sep).join("/")); assert.ok(rel.some((p) => p.startsWith("anthropic/") && /dashscope/.test(p)), `anthropic/dashscope path: ${rel.join(", ")}`); assert.ok(rel.some((p) => p.startsWith("openai/") && /bigmodel/.test(p)), `openai/bigmodel path: ${rel.join(", ")}`); assert.ok(rel.some((p) => p.startsWith("responses/") && /comfly/.test(p)), `responses/comfly path: ${rel.join(", ")}`); diff --git a/tests/proxy-responses-review.test.ts b/tests/proxy-responses-review.test.ts index 2cf3a2b291..02454edd30 100644 --- a/tests/proxy-responses-review.test.ts +++ b/tests/proxy-responses-review.test.ts @@ -1,7 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; -import { responsesToCore, coreToResponses } from "../src/responses.ts"; -import { compressLoopResponsesStream } from "../src/compress-loop-responses.ts"; +import { responsesToCore, coreToResponses, patchResponsesInput, injectResponsesDeveloperMessage } from "../src/responses.ts"; +import { compressLoopResponsesJson, compressLoopResponsesStream } from "../src/compress-loop-responses.ts"; import type { Config, CoreMessage } from "acp-kernel"; import { createCore, createInitialState } from "acp-kernel"; import type { Session } from "../src/session.ts"; @@ -29,9 +29,9 @@ function sse(type: string, data: unknown, eol = "\n"): string { return `event: ${type}${eol}data: ${JSON.stringify(data)}${eol}${eol}`; } -// Review #1: standard function_call must round-trip as function_call (NOT be -// rewritten as custom_tool_call), and custom_tool_call must stay custom. -test("responses: preserves original tool-call item type by call_id", () => { +// Standard function calls remain compressible. Codex custom items stay in the +// lossless sidecar and never enter ACP's mutable projection. +test("responses: preserves standard and custom tool-call item types", () => { const body = { model: "gpt-5.6-sol", input: [ @@ -52,10 +52,8 @@ test("responses: preserves original tool-call item type by call_id", () => { assert.equal(getWeather?.type, "function_call", "standard function tool stays function_call"); const getWeatherOut = rebuilt.find((i) => i.type === "function_call_output"); assert.ok(getWeatherOut, "standard function tool output is function_call_output"); - const exec = rebuilt.find((i) => (i as { name?: string }).name === "exec"); - assert.equal(exec?.type, "custom_tool_call", "custom tool stays custom_tool_call"); - const execOut = rebuilt.find((i) => i.type === "custom_tool_call_output"); - assert.ok(execOut, "custom tool output is custom_tool_call_output"); + assert.equal(rebuilt.some((i) => i.type === "custom_tool_call"), true, "custom calls ARE projected into tracked CoreMessages (PR#75)"); + assert.deepEqual(patchResponsesInput(responsesToCore(body as never), msgs), body.input); }); // Review #4: unknown / future item type is preserved (not dropped). @@ -73,6 +71,56 @@ test("responses: unknown item type is preserved in preamble", () => { assert.equal(msgs.length, 1, "only the real message enters compression"); }); +test("responses: no-op projection is byte-structure lossless", () => { + const input = [ + { type: "additional_tools", tools: [{ type: "custom", name: "exec", format: { type: "grammar", syntax: "lark", definition: "start: /.+/" } }] }, + { type: "message", id: "msg_u1", status: "completed", role: "user", content: [ + { type: "input_text", text: "inspect this", annotations: [] }, + { type: "input_image", image_url: "data:image/png;base64,AA==", detail: "high" }, + ] }, + { type: "reasoning", id: "rs_1", summary: [], encrypted_content: "ciphertext", status: "completed" }, + { type: "function_call", id: "fc_1", call_id: "call_1", name: "read_file", arguments: "{\"path\":\"a\"}", status: "completed" }, + { type: "function_call_output", id: "out_1", call_id: "call_1", output: "ok", status: "completed" }, + { type: "custom_tool_call", id: "ctc_1", call_id: "custom_1", name: "exec", input: "dir", status: "completed" }, + { type: "custom_tool_call_output", id: "ctco_1", call_id: "custom_1", output: "file.txt", status: "completed" }, + { type: "program", id: "future_1", code: "print(1)", vendor: { untouched: true } }, + ]; + const projection = responsesToCore({ model: "gpt-5", input } as never); + const patched = patchResponsesInput(projection, projection.msgs); + assert.deepEqual(patched, input); +}); + +test("responses: text patch preserves images, ids, status, opaque items and order", () => { + const input = [ + { type: "reasoning", id: "r1", encrypted_content: "secret" }, + { type: "message", id: "m1", status: "completed", role: "user", content: [ + { type: "input_text", text: "hello", annotations: ["keep"] }, + { type: "input_image", image_url: "https://example.test/image.png", detail: "high" }, + ] }, + { type: "future_item", id: "z1", payload: 7 }, + ]; + const projection = responsesToCore({ input } as never); + const changed = projection.msgs.map((message) => ({ ...message, text: "tagged hello" })); + const patched = patchResponsesInput(projection, changed) as typeof input; + assert.equal(patched[0], input[0]); + assert.equal(patched[2], input[2]); + assert.equal(patched[1].id, "m1"); + assert.equal(patched[1].status, "completed"); + assert.deepEqual((patched[1].content as Array>)[1], input[1].content[1]); + assert.equal((patched[1].content as Array>)[0].text, "tagged hello"); +}); + +test("responses: ACP developer prompt is inserted after leading additional_tools only", () => { + const input = [ + { type: "additional_tools", tools: [] }, + { type: "reasoning", id: "r1", encrypted_content: "secret" }, + { type: "message", role: "user", content: "hello" }, + ]; + const out = injectResponsesDeveloperMessage(input as never, "ACP prompt"); + assert.deepEqual(out.map((item) => item.type), ["additional_tools", "message", "reasoning", "message"]); + assert.equal(out[2], input[1]); +}); + // Review #2: response.failed terminal is replayed verbatim and NOT followed by // a fabricated response.completed (which would contradict the failure). test("compressLoopResponsesStream: response.failed is replayed without a contradictory completed", async () => { @@ -107,3 +155,45 @@ test("compressLoopResponsesStream: CRLF (\\r\\n) SSE line endings are parsed", a // Must not fabricate a fallback id (sign the separator was missed). assert.ok(!out.includes("resp-proxy-"), "no synthesized fallback response id"); }); + +test("compressLoopResponsesJson: Codex text trigger is intercepted and re-requested", async () => { + const previousFetch = globalThis.fetch; + let forwarded: Record | undefined; + globalThis.fetch = (async (_url: string | URL | Request, init?: RequestInit) => { + forwarded = JSON.parse(String(init?.body)) as Record; + return new Response(JSON.stringify({ + id: "resp_final", + status: "completed", + output: [{ type: "message", role: "assistant", content: [{ type: "output_text", text: "done" }] }], + }), { status: 200, headers: { "content-type": "application/json" } }); + }) as typeof fetch; + try { + const initial = { + id: "resp_trigger", + status: "completed", + output: [{ + type: "message", + role: "assistant", + content: [{ type: "output_text", text: '{"content":[]}' }], + }], + }; + const request = { + model: "gpt-5", + input: [{ type: "additional_tools", tools: [{ type: "custom", name: "exec" }] }, { type: "message", role: "user", content: "hello" }], + }; + const output = await compressLoopResponsesJson(initial, { ...makeCtx(() => {}), textProtocol: true }, request, { + url: "https://unused.example/responses", + headers: { "content-type": "application/json" }, + }); + const message = (output.output as Array>)[0]!; + const part = (message.content as Array>)[0]!; + assert.equal(part.text, "done"); + assert.ok(forwarded); + const items = forwarded.input as Array>; + assert.equal(items[0]!.type, "additional_tools"); + assert.equal(items.some((item) => item.type === "function_call" || item.type === "function_call_output"), false); + assert.match(String(items.at(-1)?.content), /ACP/); + } finally { + globalThis.fetch = previousFetch; + } +}); diff --git a/tests/proxy-responses-stream.test.ts b/tests/proxy-responses-stream.test.ts index 4175172795..daa43b833c 100644 --- a/tests/proxy-responses-stream.test.ts +++ b/tests/proxy-responses-stream.test.ts @@ -4,6 +4,7 @@ import { compressLoopResponsesStream } from "../src/compress-loop-responses.ts"; import type { Config, CoreMessage } from "acp-kernel"; import { createCore, createInitialState } from "acp-kernel"; import type { Session } from "../src/session.ts"; +import { ACP_TEXT_CLOSE, ACP_TEXT_OPEN } from "../src/compress-tool.ts"; function makeCtx(log: (m: string) => void): { core: ReturnType; config: Config; messages: CoreMessage[]; session: Session; log: (m: string) => void } { return { @@ -117,3 +118,64 @@ test("compressLoopResponsesStream: compress-only round executes tool and re-requ globalThis.fetch = originalFetch; } }); + +test("compressLoopResponsesStream: Codex text protocol never sends synthetic function items", async () => { + const trigger = `${ACP_TEXT_OPEN}${JSON.stringify({ content: [{ startId: "m00001", endId: "m00002", summary: "sum" }] })}${ACP_TEXT_CLOSE}`; + const round1 = [ + sse("response.created", { response: { id: "resp_text_1", status: "in_progress" } }), + sse("response.output_text.delta", { item_id: "msg_text_1", output_index: 0, delta: trigger }), + sse("response.completed", { response: { id: "resp_text_1", status: "completed", output: [] } }), + ].join(""); + const round2 = [ + sse("response.created", { response: { id: "resp_text_2", status: "in_progress" } }), + sse("response.output_text.delta", { item_id: "msg_text_2", output_index: 0, delta: "Done." }), + sse("response.completed", { response: { id: "resp_text_2", status: "completed", output: [] } }), + ].join(""); + const originalFetch = globalThis.fetch; + let forwarded: Record | undefined; + globalThis.fetch = (async (_url: unknown, init?: RequestInit) => { + forwarded = JSON.parse(String(init?.body)) as Record; + return new Response(round2, { status: 200, headers: { "content-type": "text/event-stream" } }); + }) as typeof fetch; + try { + const ctx = { ...makeCtx(() => {}), textProtocol: true }; + const out = await drain( + new Response(round1, { status: 200, headers: { "content-type": "text/event-stream" } }).body!, + ctx, + { model: "gpt-5", input: [{ type: "message", role: "user", content: "hi" }], stream: true }, + { url: "http://mock", headers: {} }, + ); + const types = (forwarded?.input as Array<{ type: string }>).map((item) => item.type); + assert.ok(!types.includes("function_call")); + assert.ok(!types.includes("function_call_output")); + assert.ok(types.every((type) => type === "message")); + assert.ok(out.includes("Done.")); + assert.ok(!out.includes(ACP_TEXT_OPEN)); + } finally { + globalThis.fetch = originalFetch; + } +}); + +test("compressLoopResponsesStream: Codex code_mode custom_tool_call passes through in text-protocol (regression for 无法使用工具)", async () => { + const events = [ + sse("response.created", { response: { id: "resp_ctc", status: "in_progress" } }), + sse("response.output_item.added", { item: { type: "message", id: "msg_ctc", role: "assistant", content: [] }, output_index: 0 }), + sse("response.output_text.delta", { item_id: "msg_ctc", output_index: 0, delta: "Running ls." }), + sse("response.output_item.done", { item: { type: "message", id: "msg_ctc" }, output_index: 0 }), + sse("response.output_item.added", { item: { type: "custom_tool_call", id: "ctc_1", call_id: "call_ctc_1", name: "shell", input: "" }, output_index: 1 }), + sse("response.custom_tool_call.input_text.delta", { item_id: "ctc_1", delta: '{"cmd":"ls"}' }), + sse("response.output_item.done", { item: { type: "custom_tool_call", id: "ctc_1", call_id: "call_ctc_1", name: "shell", input: '{"cmd":"ls"}' }, output_index: 1 }), + sse("response.completed", { response: { id: "resp_ctc", status: "completed", output: [] } }), + ].join(""); + const ctx = { ...makeCtx(() => {}), textProtocol: true }; + const out = await drain( + new Response(events).body!, + ctx, + { model: "gpt-5", input: [{ type: "message", role: "user", content: "list files" }], stream: true }, + { url: "http://unused", headers: {} }, + ); + assert.ok(out.includes("custom_tool_call"), "custom_tool_call item reaches the client in text-protocol"); + assert.ok(out.includes('"shell"'), "code_mode tool name preserved"); + assert.ok(out.includes("Running ls."), "assistant text still emitted"); + assert.ok(out.includes("response.completed"), "completion emitted"); +}); diff --git a/tests/proxy-session-id.test.ts b/tests/proxy-session-id.test.ts index 2aeb40c3fc..aaacd6e72a 100644 --- a/tests/proxy-session-id.test.ts +++ b/tests/proxy-session-id.test.ts @@ -1,7 +1,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { deriveSessionId, affinityToken, clientConversationHeader } from "../src/session-id.ts"; -import { conversationSignalResponses } from "../src/responses.ts"; +import { conversationIdentityResponses, conversationSignalResponses } from "../src/responses.ts"; /** Helper: build a minimal headers object. */ function hdrs(auth?: string, sessionAffinity?: string): Record { @@ -29,6 +29,12 @@ test("deriveSessionId: different API key → different session (no cross-account assert.notEqual(a, b); }); +test("deriveSessionId: credentials remain case-sensitive opaque values", () => { + const upper = deriveSessionId(hdrs("Bearer AbCd"), "responses", "https://chatgpt.com", "session"); + const lower = deriveSessionId(hdrs("Bearer abcd"), "responses", "https://chatgpt.com", "session"); + assert.notEqual(upper, lower); +}); + test("deriveSessionId: different upstream origin → different session (no cross-provider bleed)", () => { const a = deriveSessionId(hdrs("Bearer keyA"), "openai", "https://zhipu.example", "hello"); const b = deriveSessionId(hdrs("Bearer keyA"), "openai", "https://bailian.example", "hello"); @@ -73,20 +79,12 @@ test("deriveSessionId: empty conversation dimension THROWS (no silent collapse)" }); test("affinityToken: uses client signal when present (passthrough, preserves ses_ format)", () => { - const t = affinityToken(hdrs("Bearer k", "ses_opencode_xyz"), "convo-1"); + const t = affinityToken({ value: "ses_opencode_xyz", source: "header", clientProvided: true }); assert.equal(t, "ses_opencode_xyz"); }); -test("affinityToken: falls back to ses_ when no client signal", () => { - const t = affinityToken(hdrs("Bearer k"), "convo-hash-xyz"); - assert.equal(t, "ses_convo-hash-xyz"); - // Different conversation → different token. - assert.notEqual(t, affinityToken(hdrs("Bearer k"), "other-convo")); -}); - -test("affinityToken: protocolConversation (Responses previous_response_id) is just the conversation arg", () => { - const t = affinityToken(hdrs("Bearer k"), "resp_resp_abc123"); - assert.equal(t, "ses_resp_resp_abc123"); +test("affinityToken: generated identities are never forwarded upstream", () => { + assert.equal(affinityToken({ value: "generated-random", source: "generated", clientProvided: false }), undefined); }); test("clientConversationHeader: reads known session header names in priority order", () => { @@ -94,15 +92,17 @@ test("clientConversationHeader: reads known session header names in priority ord assert.equal(clientConversationHeader({ "x-acp-session": "B" }), "B"); assert.equal(clientConversationHeader({ "x-session-id": "C" }), "C"); assert.equal(clientConversationHeader({ "x-opencode-session": "D" }), "D"); + assert.equal(clientConversationHeader({ "session-id": "E" }), "E"); + assert.equal(clientConversationHeader({ session_id: "F" }), "F"); assert.equal(clientConversationHeader({}), undefined); }); -test("affinityToken is safe to send upstream: does NOT embed the API key", () => { - // The affinity token must be derivable from the conversation alone — an - // upstream that receives it should not be able to learn the key. - const withKeyA = affinityToken(hdrs("Bearer keyA"), "hello"); - const withKeyB = affinityToken(hdrs("Bearer keyB"), "hello"); - assert.equal(withKeyA, withKeyB, "affinity token identical regardless of key"); +test("affinityToken: client-provided identity passes through verbatim (credentials never in scope)", () => { + // After the refactor, affinityToken receives only the resolved identity + // object ({ value, source, clientProvided }) — never raw headers or the + // API key — so credential leakage is impossible by construction. + const token = affinityToken({ value: "client-session", source: "body-session", clientProvided: true }); + assert.equal(token, "client-session"); }); test("conversationSignalResponses: prefers codex body.session_id (UUID) over previous_response_id and content hash", () => { @@ -114,7 +114,10 @@ test("conversationSignalResponses: prefers codex body.session_id (UUID) over pre previous_response_id: "resp_abc", } as unknown as Parameters[0]; const sig = conversationSignalResponses(body, undefined); - assert.equal(sig, "codex-019fdc81-a420-7a00-bbd1-0a64e3eb772c"); + assert.equal(sig, "019fdc81-a420-7a00-bbd1-0a64e3eb772c"); + const identity = conversationIdentityResponses(body, undefined); + assert.equal(identity.source, "body-session"); + assert.equal(identity.clientProvided, true); }); test("conversationSignalResponses: header (opencode x-session-affinity) still wins over body.session_id", () => { @@ -128,19 +131,21 @@ test("conversationSignalResponses: header (opencode x-session-affinity) still wi assert.equal(sig, "ses_opencode-123"); }); -test("conversationSignalResponses: falls back to previous_response_id when no session_id and no header", () => { +test("conversationIdentityResponses: previous_response_id provides a stable conversation link", () => { const body = { input: "hello", previous_response_id: "resp_xyz", } as unknown as Parameters[0]; - assert.equal(conversationSignalResponses(body, undefined), "resp-resp_xyz"); + const identity = conversationIdentityResponses(body, undefined); + assert.equal(identity.source, "previous-response"); + assert.equal(identity.value, "resp_xyz"); + assert.equal(identity.clientProvided, false); }); -test("conversationSignalResponses: falls back to content hash when nothing else (pi path)", () => { +test("conversationIdentityResponses: identical anonymous openers share a content fingerprint (enables compression)", () => { const body = { input: "hello world" } as unknown as Parameters[0]; - // No header, no session_id, no previous_response_id → deterministic hash. const a = conversationSignalResponses(body, undefined); const b = conversationSignalResponses({ input: "hello world" } as never, undefined); assert.equal(a, b); - assert.ok(/^[0-9a-f]{16}$/.test(a), `expected content-hash id, got ${a}`); + assert.match(a, /^[0-9a-f]{16}$/); }); diff --git a/tests/routing.test.ts b/tests/routing.test.ts index 2f7954242d..65788ede9f 100644 --- a/tests/routing.test.ts +++ b/tests/routing.test.ts @@ -1,9 +1,11 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { writeFileSync, unlinkSync } from "node:fs"; -import { loadOptions, lookupContextLimit, resolveContextLimit, parseRouteEntry } from "../src/config.ts"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; +import { loadOptions, lookupContextLimit, resolveContextLimit, resolveConfiguredContextLimit, parseRouteEntry, parsePromptCacheRouting } from "../src/config.ts"; -const TMP = (s: string) => `/tmp/test-acp-${s}.json`; +const TMP = (s: string) => join(tmpdir(), `test-acp-${process.pid}-${s}.json`); const writeRoutes = (name: string, obj: unknown) => { const p = TMP(name); writeFileSync(p, JSON.stringify(obj)); @@ -49,6 +51,22 @@ test("parseRouteEntry: invalid values return undefined", () => { assert.equal(parseRouteEntry("string"), undefined); // url is the KEY, not the value }); +test("legacy named-route config fails with an actionable migration error", () => { + const p = writeRoutes("legacy-string", { openai: "https://api.openai.com/v1" }); + try { + assert.throws(() => loadOptions({ ACP_PROVIDERS: p }), /legacy provider route.*use the upstream URL as the key/); + } finally { + unlinkSync(p); + } +}); + +test("prompt-cache routing accepts the tri-state and defaults invalid values to auto", () => { + assert.equal(parsePromptCacheRouting("enabled"), "enabled"); + assert.equal(parsePromptCacheRouting("disabled"), "disabled"); + assert.equal(parsePromptCacheRouting("auto"), "auto"); + assert.equal(parsePromptCacheRouting("unknown"), "auto"); +}); + test("lookupContextLimit returns known windows", () => { assert.equal(lookupContextLimit("claude-sonnet-4-20250514"), 200_000); assert.equal(lookupContextLimit("gpt-4o"), 128_000); @@ -133,6 +151,12 @@ test("model not in route falls through to lookup table", () => { assert.equal(resolveContextLimit(routes, "https://api.deepseek.com", "deepseek-chat"), 64000); }); +test("configured context lookup stays separate from registry/built-in fallbacks", () => { + const routes = { "https://api.openai.com": { models: {} } }; + assert.equal(resolveConfiguredContextLimit(routes, "https://api.openai.com/v1/responses", "gpt-5"), undefined); + assert.equal(resolveContextLimit(routes, "https://api.openai.com/v1/responses", "gpt-5"), 400_000); +}); + test("no matching key and unknown model returns undefined", () => { const routes = { "https://open.bigmodel.cn": { models: { "glm-5.2": { context: 1000000 } } }, diff --git a/tests/upstream-proxy-routing.test.ts b/tests/upstream-proxy-routing.test.ts new file mode 100644 index 0000000000..879ae5e68f --- /dev/null +++ b/tests/upstream-proxy-routing.test.ts @@ -0,0 +1,268 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import net from "node:net"; +import { once } from "node:events"; +import { mkdirSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { loadOptions, type ProxyOptions } from "../src/config.ts"; +import { resolveUpstream, startServer } from "../src/server.ts"; +import { enableCodexTakeover, disableCodexTakeover } from "../src/codex-takeover.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { fetchWithTimeout } from "../src/fetch-util.ts"; +import { + formatUpstreamError, + matchesNoProxy, + parseHttpProxy, + proxyDispatcher, + resetProxyCache, + resolveProxy, + resolveProxyDecision, +} from "../src/upstream-proxy.ts"; + +function listen(server: http.Server, port: number = 0): Promise { + server.listen(port, "127.0.0.1"); + return once(server, "listening").then(() => undefined); +} + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve())); +} + +function routeFixture(): { root: string; configPath: string; statePath: string } { + const root = path.join(tmpdir(), `bili-route-${process.pid}-${Date.now()}-${Math.random().toString(16).slice(2)}`); + const codex = path.join(root, "codex"); + mkdirSync(codex, { recursive: true }); + const configPath = path.join(codex, "config.toml"); + const statePath = path.join(root, "codex-route-state.json"); + return { root, configPath, statePath }; +} + +test("/codex resolves every future subpath against the captured active provider", () => { + const files = routeFixture(); + writeFileSync(files.configPath, [ + 'model_provider = "relay"', + "[model_providers.relay]", + 'name = "Relay"', + 'base_url = "https://relay.example/openai/v1"', + 'wire_api = "responses"', + "requires_openai_auth = true", + "supports_websockets = false", + ].join("\n"), "utf8"); + const oldState = process.env.BILI_CODEX_ROUTE_STATE; + process.env.BILI_CODEX_ROUTE_STATE = files.statePath; + try { + enableCodexTakeover(8787, files); + const opts = loadOptions({ ACP_PORT: "8787" }); + assert.deepEqual(resolveUpstream(opts, "/codex/responses?foo=a%2Fb"), { + upstream: "https://relay.example/openai/v1", + rewrittenUrl: "https://relay.example/openai/v1/responses?foo=a%2Fb", + }); + assert.deepEqual(resolveUpstream(opts, "/codex/future/unknown?x=1"), { + upstream: "https://relay.example/openai/v1", + rewrittenUrl: "https://relay.example/openai/v1/future/unknown?x=1", + }); + assert.equal(resolveUpstream(opts, "/codex-not-owned/responses"), undefined); + } finally { + try { disableCodexTakeover(files); } catch { } + if (oldState === undefined) delete process.env.BILI_CODEX_ROUTE_STATE; + else process.env.BILI_CODEX_ROUTE_STATE = oldState; + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("/codex integration preserves query, subscription, account and thread headers", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + let captured: { url: string; headers: http.IncomingHttpHeaders; body: string } | undefined; + const upstream = http.createServer((req, res) => { + const chunks: Buffer[] = []; + req.on("data", (chunk: Buffer) => chunks.push(chunk)); + req.on("end", () => { + captured = { url: req.url ?? "", headers: req.headers, body: Buffer.concat(chunks).toString("utf8") }; + res.writeHead(200, { "content-type": "application/json" }); + res.end('{"ok":true}'); + }); + }); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + const files = routeFixture(); + writeFileSync(files.configPath, [ + 'model_provider = "relay"', + "[model_providers.relay]", + 'name = "Relay"', + `base_url = "http://127.0.0.1:${upstreamPort}/backend-api/codex"`, + 'wire_api = "responses"', + "requires_openai_auth = true", + "supports_websockets = false", + ].join("\n"), "utf8"); + const oldState = process.env.BILI_CODEX_ROUTE_STATE; + process.env.BILI_CODEX_ROUTE_STATE = files.statePath; + const probe = http.createServer(); + await listen(probe); + const biliPort = (probe.address() as { port: number }).port; + await close(probe); + enableCodexTakeover(biliPort, files); + const opts: ProxyOptions = { + port: biliPort, + host: "127.0.0.1", + upstream: "http://unused.invalid", + routes: {}, + proxy: "", + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: true, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const bili = await startServer(opts); + if (!bili.listening) await once(bili, "listening"); + try { + const response = await fetch(`http://127.0.0.1:${biliPort}/codex/future/unknown?x=1&encoded=a%2Fb`, { + method: "POST", + headers: { + authorization: "Bearer OfficialSubscription", + "chatgpt-account-id": "account-1", + "session-id": "session-1", + "x-thread-id": "thread-1", + "content-type": "application/json", + }, + body: '{"future":true}', + }); + assert.equal(response.status, 200); + assert.ok(captured); + assert.equal(captured.url, "/backend-api/codex/future/unknown?x=1&encoded=a%2Fb"); + assert.equal(captured.headers.authorization, "Bearer OfficialSubscription"); + assert.equal(captured.headers["chatgpt-account-id"], "account-1"); + assert.equal(captured.headers["session-id"], "session-1"); + assert.equal(captured.headers["x-thread-id"], "thread-1"); + assert.equal(captured.body, '{"future":true}'); + } finally { + await close(bili); + await close(upstream); + try { disableCodexTakeover(files); } catch { } + if (oldState === undefined) delete process.env.BILI_CODEX_ROUTE_STATE; + else process.env.BILI_CODEX_ROUTE_STATE = oldState; + rmSync(files.root, { recursive: true, force: true }); + } +}); + +test("proxy precedence, NO_PROXY, HTTPS proxies and self-loop detection are deterministic", () => { + const target = new URL("https://api.example.com:8443/v1"); + assert.equal(matchesNoProxy(target, "localhost,.internal.example,api.example.com:8443"), true); + assert.equal(resolveProxy({}, undefined, target.href, { + httpsProxy: "https://env.example:9443", + systemProxy: { enabled: true, https: "http://system.example:8080" }, + biliPort: 8787, + }), "https://env.example:9443/"); + assert.equal(resolveProxy({}, undefined, target.href, { + systemProxy: { enabled: true, https: "http://system.example:8080" }, + biliPort: 8787, + }), "http://system.example:8080/"); + assert.equal(resolveProxy({}, "http://explicit.example:8080", target.href, { + noProxy: "api.example.com", + biliPort: 8787, + }), "http://explicit.example:8080/"); + assert.equal(resolveProxy({ "https://api.example.com:8443": { proxy: "http://provider.example:8080" } }, "http://explicit.example:8080", target.href, { + biliPort: 8787, + }), "http://provider.example:8080/"); + assert.deepEqual(resolveProxyDecision({}, undefined, target.href, { + httpsProxy: "http://127.0.0.1:8787", + systemProxy: { enabled: true, https: "http://system.example:8080" }, + biliPort: 8787, + }), { proxy: "http://system.example:8080/", source: "windows-system" }); + assert.throws(() => parseHttpProxy("http://127.0.0.1:8787", 8787), /loop back into bili/); + assert.throws(() => parseHttpProxy("http://[::ffff:7f00:1]:8787", 8787), /loop back into bili/); + assert.equal(parseHttpProxy("https://proxy.example:9443")?.protocol, "https:"); +}); + +test("loadOptions keeps BILI_UPSTREAM_PROXY above config/environment fallback", () => { + const opts = loadOptions({ + ACP_PORT: "9100", + BILI_UPSTREAM_PROXY: "https://explicit.example:9443", + HTTPS_PROXY: "http://fallback.example:8080", + ALL_PROXY: "http://all.example:8080", + NO_PROXY: "localhost,127.0.0.1", + }); + assert.equal(opts.proxy, "https://explicit.example:9443"); + assert.equal(opts.proxySource, "bili-env"); + assert.deepEqual(opts.proxyFallback, { + httpsProxy: "http://fallback.example:8080", + allProxy: "http://all.example:8080", + noProxy: "localhost,127.0.0.1", + biliPort: 9100, + globalSource: "bili-env", + }); +}); + +test("PR #67 ProxyAgent remains the sole HTTP egress transport", async () => { + const upstream = http.createServer((_req, res) => { + res.writeHead(201, { "content-type": "text/plain" }); + res.end("via proxy"); + }); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + let capturedConnect = ""; + const tunnels = new Set(); + const proxy = http.createServer(); + proxy.on("connect", (req, clientSocket, head) => { + capturedConnect = req.url ?? ""; + const socket = net.connect(upstreamPort, "127.0.0.1", () => { + clientSocket.write("HTTP/1.1 200 Connection Established\r\n\r\n"); + if (head.length > 0) socket.write(head); + clientSocket.pipe(socket); + socket.pipe(clientSocket); + }); + tunnels.add(clientSocket); + tunnels.add(socket); + clientSocket.once("close", () => tunnels.delete(clientSocket)); + socket.once("close", () => tunnels.delete(socket)); + }); + await listen(proxy); + const port = (proxy.address() as { port: number }).port; + const proxyUrl = `http://127.0.0.1:${port}`; + try { + const result = await fetchWithTimeout("http://upstream.invalid/future?q=1", { + method: "POST", + body: "payload", + dispatcher: proxyDispatcher(proxyUrl), + }); + assert.equal(result.response.status, 201); + assert.equal(await result.response.text(), "via proxy"); + result.clearTimer(); + assert.equal(capturedConnect, "upstream.invalid:80"); + } finally { + resetProxyCache(); + for (const socket of tunnels) socket.destroy(); + proxy.closeAllConnections(); + await close(proxy); + upstream.closeAllConnections(); + await close(upstream); + } +}); + +test("upstream failures expand nested causes with redacted proxy context", () => { + const cause = Object.assign(new Error("connect timed out"), { + code: "UND_ERR_CONNECT_TIMEOUT", + errno: -4039, + syscall: "connect", + address: "203.0.113.7", + port: 443, + }); + const detail = formatUpstreamError( + new TypeError("fetch failed", { cause }), + "https://chatgpt.com/backend-api/codex/responses", + "http://user:secret@proxy.example:8080", + ); + for (const expected of ["code=UND_ERR_CONNECT_TIMEOUT", "errno=-4039", "syscall=connect", "address=203.0.113.7", "port=443", "message=fetch failed <- connect timed out", "url=https://chatgpt.com/backend-api/codex/responses", "proxy=http://***:***@proxy.example:8080/"]) { + assert.ok(detail.includes(expected), expected); + } +}); diff --git a/tests/web-routing.test.ts b/tests/web-routing.test.ts new file mode 100644 index 0000000000..d134a215ec --- /dev/null +++ b/tests/web-routing.test.ts @@ -0,0 +1,187 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { mkdirSync, readFileSync, rmSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import type { ProxyOptions } from "../src/config.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import { WEB_CLIENT } from "../src/web/client.ts"; + +function close(server: http.Server): Promise { + return new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve())); +} + +test("embedded Web client is valid JavaScript", () => { + assert.doesNotThrow(() => new Function(WEB_CLIENT)); +}); + +async function freePort(): Promise { + const server = http.createServer(); + server.listen(0, "127.0.0.1"); + await once(server, "listening"); + const port = (server.address() as { port: number }).port; + await close(server); + return port; +} + +test("Web UI exposes route, upstream and history controls without inline handlers", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const root = path.join(tmpdir(), `bili-web-routing-${process.pid}-${Date.now()}`); + const codexHome = path.join(root, "codex"); + const codexConfig = path.join(codexHome, "config.toml"); + const authPath = path.join(codexHome, "auth.json"); + const routeState = path.join(root, "data", "codex-route-state.json"); + const biliConfig = path.join(root, "billion-context.json"); + mkdirSync(codexHome, { recursive: true }); + const originalCodex = 'model = "gpt-5.4"\n[mcp_servers.keep]\ncommand = "keep"\n'; + const originalAuth = '{"tokens":{"access_token":"keep-secret"}}\n'; + writeFileSync(codexConfig, originalCodex, "utf8"); + writeFileSync(authPath, originalAuth, "utf8"); + writeFileSync(biliConfig, '{"providers":{}}\n', "utf8"); + + const previous = { + codexHome: process.env.CODEX_HOME, + config: process.env.BILI_CONFIG_FILE, + state: process.env.BILI_CODEX_ROUTE_STATE, + }; + process.env.CODEX_HOME = codexHome; + process.env.BILI_CONFIG_FILE = biliConfig; + process.env.BILI_CODEX_ROUTE_STATE = routeState; + const port = await freePort(); + const opts: ProxyOptions = { + port, + host: "127.0.0.1", + upstream: "http://127.0.0.1:1", + routes: {}, + proxy: "", + proxyMode: "direct", + proxySource: "direct", + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + const base = `http://127.0.0.1:${port}`; + try { + const ui = await (await fetch(`${base}/__bili/`)).text(); + assert.match(ui, /Codex 路由/); + assert.match(ui, /上游网络/); + assert.match(ui, /修复旧版路由会话/); + assert.match(ui, /addEventListener/); + assert.doesNotMatch(ui, /\sonclick=/i); + assert.match(ui, /escapeHtml/); + + const initial = await (await fetch(`${base}/__bili/codex`)).json() as { state: string; provider?: { id: string } }; + assert.equal(initial.state, "disabled"); + assert.equal(initial.provider?.id, "openai"); + const crossOrigin = await fetch(`${base}/__bili/codex/enable`, { + method: "POST", + headers: { origin: "https://attacker.example" }, + }); + assert.equal(crossOrigin.status, 403); + assert.equal((await (await fetch(`${base}/__bili/codex`)).json() as { state: string }).state, "disabled"); + const enabledResponse = await fetch(`${base}/__bili/codex/enable`, { method: "POST" }); + assert.equal(enabledResponse.status, 200); + const enabled = await enabledResponse.json() as { state: string; providerId: string; baseUrl: string }; + assert.equal(enabled.state, "enabled"); + assert.equal(enabled.providerId, "openai"); + assert.equal(enabled.baseUrl, `http://127.0.0.1:${port}/codex`); + assert.equal(readFileSync(authPath, "utf8"), originalAuth); + + const saveProxy = await fetch(`${base}/__bili/config`, { + method: "PUT", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ upstreamProxyMode: "manual", upstreamProxy: "http://127.0.0.1:9999" }), + }); + assert.equal(saveProxy.status, 200); + const config = await (await fetch(`${base}/__bili/config`)).json() as { upstreamProxy: string; upstreamProxyMode: string }; + assert.equal(config.upstreamProxy, "http://127.0.0.1:9999"); + assert.equal(config.upstreamProxyMode, "manual"); + const upstream = await (await fetch(`${base}/__bili/upstream`)).json() as { proxy: string; source: string }; + assert.equal(upstream.proxy, "http://127.0.0.1:9999/"); + assert.equal(upstream.source, "web-manual"); + + const history = await (await fetch(`${base}/__bili/codex-history`)).json() as { targetProviderId: string; sessions: number }; + assert.equal(history.targetProviderId, "openai"); + assert.equal(history.sessions, 0); + + const disabledResponse = await fetch(`${base}/__bili/codex/disable`, { method: "POST" }); + assert.equal(disabledResponse.status, 200); + assert.equal(readFileSync(codexConfig, "utf8"), originalCodex); + assert.equal(readFileSync(authPath, "utf8"), originalAuth); + + const enabledForShutdown = await fetch(`${base}/__bili/codex/enable`, { method: "POST" }); + assert.equal(enabledForShutdown.status, 200); + } finally { + await close(proxy); + const restoredOnClose = readFileSync(codexConfig, "utf8"); + if (previous.codexHome === undefined) delete process.env.CODEX_HOME; else process.env.CODEX_HOME = previous.codexHome; + if (previous.config === undefined) delete process.env.BILI_CONFIG_FILE; else process.env.BILI_CONFIG_FILE = previous.config; + if (previous.state === undefined) delete process.env.BILI_CODEX_ROUTE_STATE; else process.env.BILI_CODEX_ROUTE_STATE = previous.state; + rmSync(root, { recursive: true, force: true }); + assert.equal(restoredOnClose, originalCodex); + } +}); + +test("PUT /__bili/config with providers takes effect without a separate reload call", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const root = path.join(tmpdir(), `bili-put-providers-${process.pid}-${Date.now()}`); + mkdirSync(root, { recursive: true }); + const biliConfig = path.join(root, "billion-context.json"); + writeFileSync(biliConfig, '{"providers":{}}\n', "utf8"); + const prevConfig = process.env.BILI_CONFIG_FILE; + process.env.BILI_CONFIG_FILE = biliConfig; + const port = await freePort(); + const opts: ProxyOptions = { + port, + host: "127.0.0.1", + upstream: "http://127.0.0.1:1", + routes: {}, + proxy: "", + proxyMode: "direct", + proxySource: "direct", + modelContextLimit: 400_000, + kernelConfig: defaultConfig(400_000), + compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, + sessionHeader: "x-acp-session", + log: false, + debug: false, + passthrough: false, + autoUpdate: false, + mitm: { enabled: false, domains: [] }, + }; + const proxy = await startServer(opts); + if (!proxy.listening) await once(proxy, "listening"); + const base = `http://127.0.0.1:${port}`; + try { + const providers = { "https://api.example.com/v1": { models: { "gpt-test": { context: 123456 } } } }; + const put = await fetch(`${base}/__bili/config`, { + method: "PUT", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ providers }), + }); + assert.equal(put.status, 200); + const after = await (await fetch(`${base}/__bili/config`)).json() as { providers: Record }; + assert.deepEqual(after.providers, providers, "providers saved and visible after PUT without /reload"); + } finally { + await close(proxy); + if (prevConfig === undefined) delete process.env.BILI_CONFIG_FILE; else process.env.BILI_CONFIG_FILE = prevConfig; + rmSync(root, { recursive: true, force: true }); + } +}); diff --git a/tests/zero-config-routing.test.ts b/tests/zero-config-routing.test.ts index cd2e5f5c32..0a34e108e0 100644 --- a/tests/zero-config-routing.test.ts +++ b/tests/zero-config-routing.test.ts @@ -11,6 +11,7 @@ const BASE_OPTS: ProxyOptions = { modelContextLimit: 200000, kernelConfig: { blocks: [], messageRefs: [], nudge: {}, stats: {}, nextBlockId: 0, nextRunId: 0 } as never, compress: { injectTool: true, injectNudge: true }, + promptCache: { routing: "auto" }, sessionHeader: "", log: false, debug: false, diff --git a/tsup.config.ts b/tsup.config.ts index 4d09508d6a..d3234ec9ba 100644 --- a/tsup.config.ts +++ b/tsup.config.ts @@ -15,7 +15,7 @@ export default defineConfig({ // noExternal, esbuild keeps `import ... from "acp-kernel"` in dist, and // npm then installs acp-kernel as a runtime dep — breaking the // "dist/index.js is self-contained" contract (AGENTS.md §2.1). - noExternal: ["acp-kernel", "node-forge", "undici"], + noExternal: ["acp-kernel", "fzstd", "node-forge", "tar", "undici"], banner: { // node-forge is a CommonJS dependency that calls require("crypto") etc. // inlined into our ESM output, esbuild's __require shim throws in an