diff --git a/CONFIGURATION.md b/CONFIGURATION.md index 5842c032ee..9b0cc3252b 100644 --- a/CONFIGURATION.md +++ b/CONFIGURATION.md @@ -284,6 +284,8 @@ This index is generated from `website/config-reference/*.yaml` — edit the seed | `imageTokenCap` | number | unset (uncapped) | — | Per-image token cost cap for this route. | | `resign` | scheme → { enabled?, passthrough?, credentialRef? } | {} (global map applies) | — | Per-route overrides of the global resign map (level 2, deepest wins). | | `bind` | string (named entries only) | unset | — | Deep-merge a named (non-URL) entry onto the bound URL lane as an alias; without bind a named entry stays routing-inert. | +| `apiKeyEnv` | string (env var name) | unset | — | Lane credential override (#2336): replace the client's credential with this env variable's value for this lane. Exactly one of apiKeyEnv/credentialRef; see [Lane credentials](#lane-credentials-apikeyenv--credentialref). | +| `credentialRef` | string (store name) | unset | — | Lane credential override via the private summary-credential store (secret:NAME semantics). | | `compactionOptIn` | boolean | false | BILI_NON_HTTP_PROVIDERS | Named entries only: opt a non-http(s)-baseUrl provider into compaction ownership (pi/omp lanes); unions with env BILI_NON_HTTP_PROVIDERS. | **Environment-only variables** @@ -866,7 +868,7 @@ Three more #2030 keys extend existing blocks: [`mitm.handshakeTimeoutMs`](#clien ## Providers -The `providers` block maps **upstream URLs** to per-provider configuration. Each key is a URL prefix; each value can declare model context windows, a per-provider proxy, a compression protocol, a wire-protocol declaration, compression overrides, an image billing mode, a per-route passthrough, and a client-side direct exemption. Non-URL **named** keys are also allowed: they are routing-inert on their own, and become real lanes via [`bind`](#named-provider-entries-bind). +The `providers` block maps **upstream URLs** to per-provider configuration. Each key is a URL prefix; each value can declare model context windows, a per-provider proxy, a compression protocol, a wire-protocol declaration, compression overrides, an image billing mode, a per-route passthrough, a client-side direct exemption, and a lane credential override ([`apiKeyEnv`](#lane-credentials-apikeyenv--credentialref)). Non-URL **named** keys are also allowed: they are routing-inert on their own, and become real lanes via [`bind`](#named-provider-entries-bind). ```jsonc { "providers": { @@ -934,6 +936,24 @@ A key that is not a URL (e.g. `"claude-bridge"`) is a **named** entry. On its ow } ``` +### Lane credentials (`apiKeyEnv` / `credentialRef`) + +A URL-keyed lane (or a named entry with `bind`) can send **its own** credential upstream instead of the client's — the lane owner's key replaces whatever the agent client put on the wire (#2336). Typical case: a shared machine proxy where the owner's `deepseek` lane authenticates with the owner's key no matter which client (and whose personal key) is talking through it. + +```jsonc +{ + "providers": { + "https://api.deepseek.com": { "apiKeyEnv": "DEEPSEEK_LANE_KEY" } + } +} +``` + +- **Type:** `string` — `apiKeyEnv` is an environment-variable NAME (`env:VAR` semantics), `credentialRef` is a NAME in the private [summary-credential store](#shared-external-summary-service) (`secret:NAME` semantics). Exactly one of the two per entry; invalid values reject the config (#1909 discipline — startup failure / web-UI 400). +- **Replacement rules:** `x-api-key` and `x-goog-api-key` are replaced outright. `authorization` is replaced **only** when the client's value is a Bearer token — any other scheme (SDK-HMAC signatures, mTLS fingerprints…) is signature-owned and left untouched, with a warning. A request carrying none of the three gains `authorization: Bearer `. +- **#1884 interplay:** skipped whenever the CodeArts re-sign arm is active on the request — the re-signer owns `Authorization` there. +- **Failure posture:** if the env var is unset or the secret is absent, bili keeps the client's own headers and warns once per lane+reference (visible, never silently degrading to a guaranteed 401). The warning re-arms after a successful resolution. +- Applied once to the shared forward-header set, so every egress of the request — initial send, role-ladder retry, overflow refold, compress-loop rounds, continuation refetch — carries the lane credential. + ### `models` - **Type:** `Record` @@ -1089,6 +1109,10 @@ A recipe carries: `baseUrl` (HTTPS; loopback HTTP allowed for local development; Up to 16 targets per chain are supported. Saving an enabled chain through the Web API validates that every reference resolves (unknown provider or model → HTTP 400). At runtime an unresolvable reference is logged once and disables the chain until fixed — it never falls back to summarizing with the main model. `secret:` values are stored separately from the main JSON configuration in the private `billion-context.json.summary-credentials.json` file and are never returned by the configuration API. On Windows, protect this file and its parent directory with an administrator-only ACL; stale `.lock` files require manual removal after confirming no proxy process is writing the store. The total budget is shared across all targets and compression entry points, and cancellation or session-state changes discard generated results without folding. +#### Agent-reported providers (fallback layer) + +An ACP-native agent (currently the `pi` extension) reports its own configured providers to the proxy once per process (`POST /__bili/agent-providers`): provider name, base URL, wire protocol, resolved API key, and model list. These recipes form a **fallback layer** — a chain may reference `"zhipu/glm-5"` without the dial fields being duplicated in the file, so a provider configured once in the agent's own config is directly usable as a summary target. Merge order is **file wins**: a file recipe with the same name shadows the agent's contribution for that provider entirely (including its models). The agent layer never persists to disk: keys live only in proxy process memory, are never returned by the configuration API, and never appear in logs. Skip rules on the reporting side: OAuth-authenticated providers, `auth.json` ("stored") credentials, providers whose endpoint points back at the proxy itself, and wire protocols without a summary dial (`bedrock`, `vertex`, `mistral`, `pi-messages`) are not reported. The Web UI marks agent-reported models with an `(agent)` badge in the target dropdown. + The three levels, from broadest to most specific: 1. **Global** — a top-level `"compress": { … }` key. Applies to every request. This is the only level where the `injectTool` / `injectNudge` toggles are honoured. diff --git a/CONFIGURATION.zh-CN.md b/CONFIGURATION.zh-CN.md index 6886bf8086..dd63ade154 100644 --- a/CONFIGURATION.zh-CN.md +++ b/CONFIGURATION.zh-CN.md @@ -284,6 +284,8 @@ | `imageTokenCap` | number | unset (uncapped) | — | 本路由单张图片 token 成本上限。 | | `resign` | scheme → { enabled?, passthrough?, credentialRef? } | {} (global map applies) | — | 按路由覆盖全局 resign 映射(第 2 级,最深层胜出)。 | | `bind` | string (named entries only) | unset | — | 把具名(非 URL)条目深合并到所绑定的 URL 通道作为别名;无 bind 的具名条目不参与路由。 | +| `apiKeyEnv` | string (env var name) | unset | — | 通道凭据覆盖(#2336):用该环境变量的值替换客户端凭据。与 credentialRef 二选一;见[通道凭据](#lane-credentials-apikeyenv--credentialref)。 | +| `credentialRef` | string (store name) | unset | — | 经私有摘要凭据存储的通道凭据覆盖(secret:NAME 语义)。 | | `compactionOptIn` | boolean | false | BILI_NON_HTTP_PROVIDERS | 仅命名条目:把非 http(s) baseUrl 供应商纳入压缩所有权(pi/omp 车道);与 env BILI_NON_HTTP_PROVIDERS 取并集。 | **仅环境变量(无配置文件键)** @@ -866,7 +868,7 @@ ## Providers -`providers` 块将**上游 URL** 映射到按 provider 的配置。每个键是一个 URL 前缀;每个值可以声明模型上下文窗口、按 provider 的代理、压缩协议、wire 协议声明、压缩覆盖项、图片计费模式、按路由的透传开关,以及客户端侧直连豁免。 也允许**命名**的非 URL 键:它们本身对路由惰性无效,可通过 [`bind`](#named-provider-entries-bind) 成为真实 lane。 +`providers` 块将**上游 URL** 映射到按 provider 的配置。每个键是一个 URL 前缀;每个值可以声明模型上下文窗口、按 provider 的代理、压缩协议、wire 协议声明、压缩覆盖项、图片计费模式、按路由的透传开关,客户端侧直连豁免,以及通道凭据覆盖([`apiKeyEnv`](#lane-credentials-apikeyenv--credentialref))。 也允许**命名**的非 URL 键:它们本身对路由惰性无效,可通过 [`bind`](#named-provider-entries-bind) 成为真实 lane。 ```jsonc { "providers": { @@ -934,6 +936,24 @@ } ``` +### 通道凭据(`apiKeyEnv` / `credentialRef`) + +URL 键通道(或带 `bind` 的命名条目)可以向上游发送**自己的**凭据而非客户端的 —— lane 主人的 key 替换 agent 客户端放在 wire 上的任何凭据(#2336)。典型场景:共享机器代理中,主人的 `deepseek` lane 无论哪个客户端(带谁的私人 key)接入,都用主人的 key 认证。 + +```jsonc +{ + "providers": { + "https://api.deepseek.com": { "apiKeyEnv": "DEEPSEEK_LANE_KEY" } + } +} +``` + +- **类型:** `string` —— `apiKeyEnv` 是环境变量名(`env:VAR` 语义);`credentialRef` 是私有[摘要凭据存储](#共享外部摘要服务)中的名称(`secret:NAME` 语义)。每个条目二选一;非法值拒绝整个配置(#1909 纪律 —— 启动失败 / Web UI 400)。 +- **替换规则:** `x-api-key` 与 `x-goog-api-key` 直接替换。`authorization` **仅当**客户端值是 Bearer token 时才替换 —— 其它方案(SDK-HMAC 签名、mTLS 指纹…)属签名所有,原样保留并告警。三者皆无的请求会注入 `authorization: Bearer `。 +- **#1884 交互:** 请求上 CodeArts 重签名臂活跃时跳过 —— 那里重签名器拥有 `Authorization`。 +- **失败姿态:** env 未设或 secret 缺失时,bili 保留客户端自己的头,并按通道+引用告警一次(可见,绝不静默退化成必 401)。解析成功后告警槽重新武装。 +- 在共享的转发头集合上一次性生效,请求的每次出站(初次发送、角色阶梯重试、溢出重折、压缩循环、续读重取)都携带通道凭据。 + ### `models` - **类型:** `Record` @@ -1092,6 +1112,10 @@ 每条链最多 16 个目标。通过 Web API 保存启用的链时会校验每个引用可解析(未知 provider 或 model → HTTP 400)。运行时遇到不可解析的引用会记录一次警告并禁用整条链直至修复 —— 绝不会回退用主模型写摘要。`secret:` 值单独存储在私有的 `billion-context.json.summary-credentials.json` 文件中,不随主 JSON 配置下发,配置 API 也不会返回。Windows 上请用仅管理员的 ACL 保护该文件及其父目录;确认没有代理进程在写存储后,残留的 `.lock` 文件需手工删除。总预算由全部目标与压缩入口共享;取消或会话状态变化会丢弃已生成但未落盘的结果。 +#### Agent 上报的 providers(兜底层) + +ACP 原生 agent(当前为 `pi` 扩展)会在每个进程内向代理上报自己已配置的 providers(`POST /__bili/agent-providers`):provider 名、base URL、线上协议、已解析的 API key 与模型清单。这些 recipes 构成一个**兜底层** —— 链可以直接引用 `"zhipu/glm-5"`,无需在文件里重复拨号字段;在 agent 自身配置过的 provider 可直接用作摘要目标。合并序为**文件优先**:同名 file recipe 会完全遮蔽 agent 对该 provider 的贡献(含模型清单)。agent 层永不落盘:key 只存在于代理进程内存,配置 API 不返回,也不进日志。上报侧的跳过规则:OAuth 认证的 provider、`auth.json`(“stored”)凭据、端点指回代理自身的 provider、以及没有摘要拨号协议的 provider(`bedrock`、`vertex`、`mistral`、`pi-messages`)都不会上报。Web 面板的目标下拉里 agent 上报的模型带 `(agent)` 标记。 + 三个层级,从最宽泛到最具体: 1. **全局(Global)** —— 顶层 `"compress": { … }` 键。应用于每个请求。这是唯一会生效 `injectTool` / `injectNudge` 开关的层级。 diff --git a/reference/external-summary.md b/reference/external-summary.md index d0f66839be..b46a5bd13c 100644 --- a/reference/external-summary.md +++ b/reference/external-summary.md @@ -6,7 +6,7 @@ Design discussion: [#2163](https://github.com/ranxianglei/billion-context/issues The implementation in PR #2167 wires the isolated executor in `src/external-summary.ts` into the host's compression paths through `src/external-summary-runtime.ts` and `src/external-summary-compress.ts`. `compress.externalSummary` settings are opt-in and disabled by default; the chain is three-level like every other `compress` field (whole-chain replace at provider/model level, no sub-merge) and rides the request Config rail (`ctx.config` / `effectiveConfig`, #833) — there is no side configuration file. This is a review-branch implementation, not a published release or production deployment; repository-owner approval of the configuration surface remains outstanding. -The chain is expressed as **references** (`"glm/glm-4.9-flash"`) into named provider *recipes* in the `providers` table (non-URL entries carrying `baseUrl`/`api`/credential/`models`, see `CONFIGURATION.md`). Parsing is split: `src/external-summary-settings.ts` does syntax-level chain parsing at config load (`parseExternalSummaryChain`) and reference expansion at request-apply time (`expandExternalSummaryChain` / `expandExternalSummaryChainTolerant` in `src/compress-settings.ts`), producing the expanded `ExternalSummarySettings` the executor rail consumes. An unresolvable reference in an enabled chain logs one warning and disables the chain — never a fallback to the main model. The Web API saves reject unresolvable enabled chains with HTTP 400; recipes are collected from the about-to-be-saved `providers` section (`collectNamedProviders` in `src/config.ts`). +The chain is expressed as **references** (`"glm/glm-4.9-flash"`) into named provider *recipes* in the `providers` table (non-URL entries carrying `baseUrl`/`api`/credential/`models`, see `CONFIGURATION.md`). Parsing is split: `src/external-summary-settings.ts` does syntax-level chain parsing at config load (`parseExternalSummaryChain`) and reference expansion at request-apply time (`expandExternalSummaryChain` / `expandExternalSummaryChainTolerant` in `src/compress-settings.ts`), producing the expanded `ExternalSummarySettings` the executor rail consumes. An unresolvable reference in an enabled chain logs one warning and disables the chain — never a fallback to the main model. The Web API saves reject unresolvable enabled chains with HTTP 400; recipes are collected from the about-to-be-saved `providers` section (`collectNamedProviders` in `src/config.ts`). #2336 adds an in-memory **agent-registry fallback layer** (`src/agent-providers.ts`): ACP-native agents POST their configured providers (`POST /__bili/agent-providers`, sanitized + validated by `parseAgentProviderReport`), and those recipes merge UNDER the file recipes at request-apply time (`{ ...agentProviderRecipes(), ...opts.namedProviders }`) — file wins on name collision. Agent recipes carry the resolved key inline (`apiKey` on the rail target, `XOR` with `credentialRef`; the file world still rejects plaintext keys — `parseExternalSummarySettings(value, { inlineKeys: true })` is the rail-only switch). The executor accepts immutable task text, instructions and optional read-only reference text; an ordered list of injected asynchronous candidates; and explicit internal budgets. Wire protocols, authentication and range selection remain caller responsibilities. No provider request or fold occurs without a caller. diff --git a/reference/external-summary.zh-CN.md b/reference/external-summary.zh-CN.md index b1576d7e21..f5efd078ea 100644 --- a/reference/external-summary.zh-CN.md +++ b/reference/external-summary.zh-CN.md @@ -6,7 +6,7 @@ PR #2167 已通过 `src/external-summary-runtime.ts` 和 `src/external-summary-compress.ts`,将 `src/external-summary.ts` 独立执行器接入宿主压缩路径。`compress.externalSummary` 默认关闭,须显式启用;该链与其余 `compress` 字段一样存在全部三个层级(provider/model 层整链替换,无子字段合并),且沿请求 Config 轨道(`ctx.config` / `effectiveConfig`,#833)流动,不再有旁路配置文件。当前是评审分支中的实现,不是已发布版本,也未部署生产;配置面仍待仓库 owner 明确确认。 -链以**引用**形式表达(`"glm/glm-4.9-flash"`),指向 `providers` 表中的具名拨号配方(带 `baseUrl`/`api`/凭据/`models` 的非 URL 条目,详见 `CONFIGURATION.zh-CN.md`)。解析分两层:`src/external-summary-settings.ts` 在配置加载时做语法层链解析(`parseExternalSummaryChain`),在请求应用时做引用展开(`expandExternalSummaryChain` / `src/compress-settings.ts` 的 tolerant 变体),产出执行器轨道消费的展开型 `ExternalSummarySettings`。启用链中的不可解析引用会记录一次警告并禁用整条链 —— 绝不回退主模型。Web API 保存时对不可解析的启用链返回 HTTP 400;配方从即将保存的 `providers` 段收集(`src/config.ts` 的 `collectNamedProviders`)。 +链以**引用**形式表达(`"glm/glm-4.9-flash"`),指向 `providers` 表中的具名拨号配方(带 `baseUrl`/`api`/凭据/`models` 的非 URL 条目,详见 `CONFIGURATION.zh-CN.md`)。解析分两层:`src/external-summary-settings.ts` 在配置加载时做语法层链解析(`parseExternalSummaryChain`),在请求应用时做引用展开(`expandExternalSummaryChain` / `src/compress-settings.ts` 的 tolerant 变体),产出执行器轨道消费的展开型 `ExternalSummarySettings`。启用链中的不可解析引用会记录一次警告并禁用整条链 —— 绝不回退主模型。Web API 保存时对不可解析的启用链返回 HTTP 400;配方从即将保存的 `providers` 段收集(`src/config.ts` 的 `collectNamedProviders`)。#2336 新增内存中的 **agent 注册表兜底层**(`src/agent-providers.ts`):ACP 原生 agent POST 自己已配置的 providers(`POST /__bili/agent-providers`,`parseAgentProviderReport` 消毒+校验),这些 recipes 在请求应用时合并在文件 recipes 之下(`{ ...agentProviderRecipes(), ...opts.namedProviders }`)——同名时文件胜出。agent recipes 携带已解析的 inline key(轨道 target 上的 `apiKey`,与 `credentialRef` 互斥;文件世界仍拒绝明文 key——`parseExternalSummarySettings(value, { inlineKeys: true })` 是轨道专用开关)。 执行器接收不可变的任务文本、指令、可选只读参考,有序的异步候选调用及明确的内部预算。协议、鉴权和范围选择由调用方负责;没有调用方就不会请求供应商或折叠历史。 diff --git a/src/agent-providers.ts b/src/agent-providers.ts new file mode 100644 index 0000000000..efbbbd0450 --- /dev/null +++ b/src/agent-providers.ts @@ -0,0 +1,99 @@ +import type { NamedProviderRecipe } from "./config.js"; +import { validSummaryCredentialName } from "./external-summary-settings.js"; + +/** + * #2336 agent-registry fallback: a plugin host (pi today) reports its own + * provider dialing recipes — baseUrl + api + the RESOLVED api key, in memory + * only — so bili's named-providers table gains an agent-side fallback layer. + * The owner's blueprint: bili's file recipes always WIN over the agent's + * (merge order `{...agentProviderRecipes(), ...opts.namedProviders}`), and an + * agent recipe never carries a credential REFERENCE — the key bytes are + * resolved in the agent's process and live only inside bili's memory, never + * in a config file, never in a log line, never in a GET response. + * + * Intake is the /__bili/agent-providers admin endpoint (loopback + + * trusted-origin gated like every /__bili/* route): the payload is validated + * structurally before it can influence an outbound summary request. + */ + +type AgentProviderRecipe = NamedProviderRecipe & { apiKey: string }; + +const byAgent = new Map>(); + +export function recordAgentProviders(agent: string, recipes: Record): void { + // An empty re-report clears that agent's layer entirely (equals absent). + if (Object.keys(recipes).length === 0) byAgent.delete(agent); + else byAgent.set(agent, recipes); +} + +/** Merged view across every registered agent (later agents overwrite earlier + * ones per provider name — one agent per process in practice). */ +export function agentProviderRecipes(): Record { + const out: Record = {}; + for (const recipes of byAgent.values()) Object.assign(out, recipes); + return out; +} + +/** For the web config GET: agent + provider NAMES only (with model ids so + * the summary panel can offer them as dropdown options) — no keys, no + * baseUrl. */ +export function agentRegistryStatus(): Array<{ agent: string; providers: Array<{ name: string; models: string[] }> }> { + return [...byAgent.entries()].map(([agent, recipes]) => ({ + agent, + providers: Object.entries(recipes) + .map(([name, recipe]) => ({ name, models: Object.keys(recipe.models).sort() })) + .sort((a, b) => a.name.localeCompare(b.name)), + })); +} + +const APIS = new Set(["anthropic", "openai", "responses", "google"]); + +/** Structural intake validation for a registry POST body: `{ agent, providers }`. + * Throws with a reason string on any violation; never inspects or echoes the + * key beyond shape (non-empty printable, sane length). */ +export function parseAgentProviderReport(payload: unknown): { agent: string; providers: Record } { + if (!payload || typeof payload !== "object" || Array.isArray(payload)) throw new Error("Expected a JSON object"); + const report = payload as { agent?: unknown; providers?: unknown }; + if (typeof report.agent !== "string" || !/^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$/.test(report.agent)) throw new Error("Invalid agent name"); + if (!report.providers || typeof report.providers !== "object" || Array.isArray(report.providers)) throw new Error("Expected a providers object"); + const entries = Object.entries(report.providers as Record); + if (entries.length === 0 || entries.length > 64) throw new Error("Expected 1 to 64 providers"); + const providers: Record = {}; + for (const [name, value] of entries) { + if (!validSummaryCredentialName(name)) throw new Error(`Invalid provider name "${name}"`); + if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error(`Provider "${name}" must be an object`); + const recipe = value as { baseUrl?: unknown; api?: unknown; apiKey?: unknown; models?: unknown }; + if (typeof recipe.baseUrl !== "string" || recipe.baseUrl.length === 0 || recipe.baseUrl.length > 2048) throw new Error(`Provider "${name}" needs a baseUrl`); + if (typeof recipe.api !== "string" || !APIS.has(recipe.api)) throw new Error(`Provider "${name}" api must be one of: anthropic, openai, responses, google`); + if (typeof recipe.apiKey !== "string" || recipe.apiKey.length === 0 || recipe.apiKey.length > 8192 || /[\x00-\x20\x7f]/.test(recipe.apiKey)) throw new Error(`Provider "${name}" needs an apiKey`); + let url: URL; + try { url = new URL(recipe.baseUrl); } catch { throw new Error(`Provider "${name}" baseUrl is not a valid URL`); } + const loopback = ["localhost", "127.0.0.1", "[::1]"].includes(url.hostname); + if ((url.protocol !== "https:" && !(url.protocol === "http:" && loopback)) || url.username || url.password || url.hash + || url.pathname.includes("/bili/") || url.pathname.startsWith("/__bili/")) throw new Error(`Provider "${name}" baseUrl must be HTTPS (or loopback HTTP) without credentials or proxy recursion`); + if (!recipe.models || typeof recipe.models !== "object" || Array.isArray(recipe.models)) throw new Error(`Provider "${name}" needs a models object`); + const models = recipe.models as Record; + const modelEntries = Object.entries(models); + if (modelEntries.length === 0 || modelEntries.length > 64) throw new Error(`Provider "${name}" needs 1 to 64 models`); + const outModels: NamedProviderRecipe["models"] = {}; + for (const [model, knobs] of modelEntries) { + if (model.length === 0 || model.length > 200) throw new Error(`Provider "${name}" has an invalid model name`); + const parsed: { contextWindow?: number; outputTokens?: number; stream?: boolean } = {}; + if (knobs.contextWindow !== undefined) { + if (typeof knobs.contextWindow !== "number" || !Number.isSafeInteger(knobs.contextWindow) || knobs.contextWindow < 2048 || knobs.contextWindow > 10_000_000) throw new Error(`Provider "${name}" model "${model}" has an invalid contextWindow`); + parsed.contextWindow = knobs.contextWindow; + } + if (knobs.outputTokens !== undefined) { + if (typeof knobs.outputTokens !== "number" || !Number.isSafeInteger(knobs.outputTokens) || knobs.outputTokens < 128) throw new Error(`Provider "${name}" model "${model}" has an invalid outputTokens`); + parsed.outputTokens = knobs.outputTokens; + } + if (knobs.stream !== undefined) { + if (typeof knobs.stream !== "boolean") throw new Error(`Provider "${name}" model "${model}" has an invalid stream flag`); + parsed.stream = knobs.stream; + } + outModels[model] = parsed; + } + providers[name] = { baseUrl: recipe.baseUrl.replace(/\/+$/, ""), api: recipe.api as NamedProviderRecipe["api"], apiKey: recipe.apiKey, models: outModels }; + } + return { agent: report.agent, providers }; +} diff --git a/src/agent/pi.ts b/src/agent/pi.ts index 40751cc0e1..def3187c57 100644 --- a/src/agent/pi.ts +++ b/src/agent/pi.ts @@ -15,7 +15,7 @@ import { isModelApiUrl, nativeInterceptInstalled } from "./native-intercept.js"; import { wirePiSubagents } from "./pi-subagents.js"; import { detectProxyBase, destinationRoutedThroughProxy, fetchManifest, forwardTool, fetchStatus, fetchProxyVersion, postIdentityRegister, reportRuntimeInfoOnChange, armedIdleNotice, noSessionWarning, nonHttpProvidersFromEnv, type ManifestTool } from "./shared.js"; -type Ctx = { +export type Ctx = { sessionManager?: { getSessionId?: () => string; getHeader?: () => unknown; getBranch?: () => unknown } | undefined; model?: { contextWindow?: number; baseUrl?: string; provider?: string; id?: string; api?: string; [key: string]: unknown } | undefined; // #2186: acp_delegate surface notifies stand-downs through the host toast @@ -25,7 +25,15 @@ type Ctx = { // optional because older hosts lack it. The real ModelRegistry surface is // find(provider, modelId) — there is no getModel (verified against pi // v0.99.1 packages/coding-agent/src/core/model-registry.ts). - modelRegistry?: { find?: (provider: string, modelId: string) => { baseUrl?: unknown } | undefined } | undefined; + modelRegistry?: { + find?: (provider: string, modelId: string) => { baseUrl?: unknown } | undefined; + // #2336 agent-registry fallback: the fuller surface (verified against + // pi v0.99.1 model-registry.d.ts) — all optional, older hosts lack them. + getAll?: () => Array<{ id: string; provider: string; api?: unknown; contextWindow?: unknown; maxTokens?: unknown }>; + getProvider?: (provider: string) => { baseUrl?: unknown; auth?: { oauth?: unknown; apiKey?: unknown } | undefined } | undefined; + getProviderAuthStatus?: (provider: string) => { configured?: unknown; source?: unknown } | undefined; + getApiKeyForProvider?: (provider: string) => Promise | undefined; + } | undefined; cwd?: string; }; @@ -112,6 +120,94 @@ function latestPhysicalResponse(branchEntries: unknown): { provider: string; mod return undefined; } +/** pi KnownApi → summary protocol (#2336). Unmappable apis (bedrock, vertex, + * mistral, pi-messages) are skipped — the summary chain only speaks the four + * wire protocols bili knows how to dial. */ +const AGENT_PROVIDER_API: Record = { + "anthropic-messages": "anthropic", + "openai-completions": "openai", + "openai-responses": "responses", + "azure-openai-responses": "responses", + "openai-codex-responses": "responses", + "google-generative-ai": "google", +}; + +/** #2336: report the host's own provider dialing recipes — baseUrl + api + + * the RESOLVED api key, in memory only — so summary chains can reference + * them ("glm/glm-5") without duplicating the dialing config in bili's + * file. Skips OAuth providers, auth.json credentials ("stored" — never + * collected by design), unmappable apis and baseUrls that point back at + * this proxy. Returns true when done (or permanently unavailable) so the + * caller can stop retrying; throws on transient failures. */ +export async function reportAgentProviders(ctx: Ctx, agent: string): Promise { + const proxyBase = proxyBaseForCtx(ctx); + if (proxyBase === undefined) return false; + const registry = ctx.modelRegistry; + if (registry?.getAll === undefined || registry.getProvider === undefined || registry.getProviderAuthStatus === undefined || registry.getApiKeyForProvider === undefined) return true; + type ProviderAcc = { api: "anthropic" | "openai" | "responses" | "google"; models: Array<{ id: string; contextWindow?: number; outputTokens?: number }> }; + const byProvider = new Map(); + for (const model of registry.getAll()) { + const api = typeof model.api === "string" ? AGENT_PROVIDER_API[model.api] : undefined; + if (api === undefined || typeof model.provider !== "string" || !/^[A-Za-z0-9][A-Za-z0-9_-]{0,63}$/.test(model.provider) || model.provider.length === 0 || typeof model.id !== "string" || model.id.length === 0 || model.id.length > 200) continue; + let entry = byProvider.get(model.provider); + if (entry === undefined) { + entry = { api, models: [] }; + byProvider.set(model.provider, entry); + } + // A provider speaking two wire apis dials them differently; the + // recipe carries ONE api, so keep only the first one's models. + if (entry.api !== api || entry.models.length >= 16) continue; + entry.models.push({ + id: model.id, + ...(typeof model.contextWindow === "number" && Number.isSafeInteger(model.contextWindow) && model.contextWindow >= 2048 && model.contextWindow <= 10_000_000 ? { contextWindow: model.contextWindow } : {}), + ...(typeof model.maxTokens === "number" && Number.isSafeInteger(model.maxTokens) && model.maxTokens >= 128 ? { outputTokens: model.maxTokens } : {}), + }); + } + const providers: Record }> = {}; + for (const [id, entry] of byProvider) { + if (entry.models.length === 0) continue; + const provider = registry.getProvider(id); + const baseUrl = provider?.baseUrl; + if (typeof baseUrl !== "string" || !(baseUrl.startsWith("https://") || baseUrl.startsWith("http://"))) continue; + // Self-loop guard: skip providers whose endpoint IS this proxy — in + // the /bili/ prefix form the baseUrl embeds the proxy + // origin, and in MITM form detectProxyBase(baseUrl) resolves EVERY + // url through the env base, so compare origins instead of just + // testing detectProxyBase() !== undefined (which would skip the + // whole table whenever BILLION_CONTEXT_PROXY is set). + let selfLoop = true; + try { selfLoop = new URL(baseUrl).origin === new URL(proxyBase).origin; } catch { selfLoop = true; } + if (selfLoop) continue; + const status = registry.getProviderAuthStatus(id); + if (status?.configured !== true || status.source === "stored") continue; + if (provider?.auth?.oauth !== undefined) continue; + let apiKey: string | undefined; + try { + apiKey = await registry.getApiKeyForProvider(id); + } catch { + continue; + } + if (typeof apiKey !== "string" || apiKey.length === 0) continue; + const models: Record = {}; + for (const model of entry.models) { + models[model.id] = { + ...(model.contextWindow !== undefined ? { contextWindow: model.contextWindow } : {}), + ...(model.outputTokens !== undefined ? { outputTokens: model.outputTokens } : {}), + }; + } + providers[id] = { baseUrl, api: entry.api, apiKey, models }; + } + if (Object.keys(providers).length === 0) return true; + const res = await fetch(`${proxyBase}/__bili/agent-providers`, { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ agent, providers }), + signal: AbortSignal.timeout(10_000), + }); + if (!res.ok) throw new Error(`agent-providers HTTP ${res.status}`); + return true; +} + function virtualModelProxyBase(ctx: Ctx, branchEntries: unknown): string | undefined { try { const last = latestPhysicalResponse(Array.isArray(branchEntries) ? branchEntries : ctx.sessionManager?.getBranch?.()); @@ -431,6 +527,10 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM return function biliPlugin(pi: ExtensionAPI): void { const agent = agentName(agentOverride); const state: RegisterState = { retryIntervalMs: opts?.retryIntervalMs ?? RETRY_INTERVAL_MS }; + // #2336: one successful agent-providers report per plugin instance; + // retried on later session_starts while the proxy base is unknown + // (native mode resolves it asynchronously). + let agentProvidersDone = false; // #2185 方案 A: this bundle's own file path, registered per session so // pi-subagents children load it deterministically (see module header). const subagentReg: SubagentSelfRegState = {}; @@ -889,6 +989,17 @@ export function createBiliPlugin(agentOverride?: string, opts?: { retryIntervalM return stampPromptCacheKey(event, ctx, agent); }); pi.on("session_start", (_event, ctx) => { + if (!agentProvidersDone) { + agentProvidersDone = true; + reportAgentProviders(ctx, agent).then((done) => { + if (done) return; + // Proxy base not resolvable yet — retry on the next session. + agentProvidersDone = false; + }).catch((err: unknown) => { + agentProvidersDone = false; + console.warn(`bili-plugin(${agent}): agent-providers report failed (${err instanceof Error ? err.message : String(err)}) — bili-side provider references to host models stay unresolved`); + }); + } if (agent === "pi") { const regReason = selfRegisterForSession(subagentReg, { env: process.env, diff --git a/src/config.ts b/src/config.ts index 6f9f83b3b2..588e66b4fa 100644 --- a/src/config.ts +++ b/src/config.ts @@ -106,6 +106,20 @@ export type ProviderRoute = { * both). The model-level knob lives one level deeper — * `models..benefit` on this same provider key (#1884 level 3). */ resign?: ResignSchemeMap; + /** #2336 lane credential override (forward path): name of the environment + * variable holding the key bili sends upstream INSTEAD of the client's + * own credential for this lane. Exactly one of apiKeyEnv/credentialRef; + * replacement rules — `authorization` is replaced only when the client's + * value is a Bearer token (other schemes are signature-owned and kept), + * `x-api-key`/`x-goog-api-key` are replaced outright, and a request with + * none of the three gains `authorization: Bearer `. Never applied + * when the #1884 re-sign arm owns the signature. Invalid values THROW + * (#1909 discipline). */ + apiKeyEnv?: string; + /** #2336 lane credential override: NAME in the private summary-credential + * store (`billion-context.json.summary-credentials.json`) — same + * semantics as apiKeyEnv, for keys kept out of the environment. */ + credentialRef?: string; }; export type ProviderRoutes = Record; // key = upstream URL prefix (the /bili/ string) @@ -2048,7 +2062,7 @@ export function parseRouteEntry(v: unknown): ProviderRoute | undefined { // is the KEY in the providers map (identical to the /bili/ string), // so it is NOT repeated inside the value. if (v && typeof v === "object" && !Array.isArray(v)) { - const obj = v as { models?: Record; proxy?: string; compressProtocol?: string; protocol?: unknown; compress?: CompressSettings; compat?: { roles?: unknown; dropFields?: unknown }; passthrough?: boolean; direct?: boolean; imageBilling?: unknown; imageTokenCap?: unknown }; + const obj = v as { models?: Record; proxy?: string; compressProtocol?: string; protocol?: unknown; compress?: CompressSettings; compat?: { roles?: unknown; dropFields?: unknown }; passthrough?: boolean; direct?: boolean; imageBilling?: unknown; imageTokenCap?: unknown; baseUrl?: unknown; api?: unknown; apiKeyEnv?: unknown; credentialRef?: unknown }; const route: ProviderRoute = { models: obj.models }; if (typeof obj.proxy === "string") route.proxy = obj.proxy; if (obj.compressProtocol === "marker" || obj.compressProtocol === "tools") route.compressProtocol = obj.compressProtocol; @@ -2067,6 +2081,27 @@ export function parseRouteEntry(v: unknown): ProviderRoute | undefined { if (imageBilling) route.imageBilling = imageBilling; const imageTokenCap = parseImageTokenCap(obj.imageTokenCap); if (imageTokenCap !== undefined) route.imageTokenCap = imageTokenCap; + // #2336 lane credential override. Only parsed for NON-recipe-shaped + // entries: a dialing recipe (baseUrl/api present) validates its own + // credential fields in parseNamedProviderRecipe, and letting this + // parser run first would surface recipe errors with route-flavored + // wording. A named non-recipe entry carrying `bind` folds these onto + // the bound lane via fillRouteGaps (generic field copy) — free. + if (obj.baseUrl === undefined && obj.api === undefined) { + if (obj.apiKeyEnv !== undefined || obj.credentialRef !== undefined) { + if (obj.apiKeyEnv !== undefined && obj.credentialRef !== undefined) + throw new Error("[acp-config] providers entry carries both apiKeyEnv and credentialRef — exactly one lane credential is allowed"); + if (obj.apiKeyEnv !== undefined) { + if (typeof obj.apiKeyEnv !== "string" || !/^[A-Za-z_][A-Za-z0-9_]*$/.test(obj.apiKeyEnv)) + throw new Error(`[acp-config] providers entry apiKeyEnv must be an environment variable name (got ${JSON.stringify(obj.apiKeyEnv)})`); + route.apiKeyEnv = obj.apiKeyEnv; + } else { + if (typeof obj.credentialRef !== "string" || !validSummaryCredentialName(obj.credentialRef)) + throw new Error(`[acp-config] providers entry credentialRef must be a name in the summary-credential store (got ${JSON.stringify(obj.credentialRef)})`); + route.credentialRef = obj.credentialRef; + } + } + } return route; } // A bare value (e.g. null) means "this upstream exists, no overrides". diff --git a/src/external-summary-runtime.ts b/src/external-summary-runtime.ts index fc208a4280..038aeabbb9 100644 --- a/src/external-summary-runtime.ts +++ b/src/external-summary-runtime.ts @@ -16,12 +16,15 @@ export class ConfiguredSummaryPlan { // from a hand-edited file — re-parse here so invalid settings fail // loudly at plan build, never silently use main-model summaries. constructor(raw: unknown, store = new SummaryCredentialStore(), env: NodeJS.ProcessEnv = process.env) { - this.settings = parseExternalSummarySettings(raw); + // inlineKeys: the rail may carry agent-registry recipes whose keys + // were resolved in the agent's memory (#2336) — literal apiKey bytes + // instead of a credentialRef. + this.settings = parseExternalSummarySettings(raw, { inlineKeys: true }); const proxyUrl = env.BILI_UPSTREAM_PROXY?.trim() || undefined; this.deadline = performance.now() + this.settings.budget.totalTimeoutMs; this.candidates = this.settings.targets.map((target) => { try { - const key = store.resolve(target.credentialRef, env); + const key = target.apiKey !== undefined ? target.apiKey : store.resolve(target.credentialRef ?? "", env); if (!key) throw new Error(); const headers: Record = target.protocol === "anthropic" ? { "x-api-key": key } : target.protocol === "google" ? { "x-goog-api-key": key } diff --git a/src/external-summary-settings.ts b/src/external-summary-settings.ts index bea12706f2..f753456f3a 100644 --- a/src/external-summary-settings.ts +++ b/src/external-summary-settings.ts @@ -8,7 +8,13 @@ interface ExternalSummaryTarget { protocol: PreflightProtocol; url: string; model: string; - credentialRef: string; + /** Reference form (env:VAR / secret:NAME) — exactly one of + * credentialRef/apiKey is set. apiKey is the #2336 agent-registry form: + * a key resolved in the agent's memory (never persisted to a file), + * accepted only on the request rail (parseExternalSummarySettings + * inlineKeys mode) — the file/legacy form rejects it. */ + credentialRef?: string; + apiKey?: string; contextWindow: number; outputTokens: number; stream: boolean; @@ -97,8 +103,10 @@ export function parseExternalSummaryChain(value: unknown): ExternalSummaryChain /** Expand chain references against the named providers table. THROWS on an * unresolvable reference — callers decide the failure policy (web save: * 400; request path: warn + treat the chain as disabled, never brick the - * request over a config typo). */ -export function expandExternalSummaryChain(chain: ExternalSummaryChain, recipes: Record): ExternalSummarySettings { + * request over a config typo). Recipes may carry an in-memory `apiKey` + * (#2336 agent-registry form) — it expands to an inline-key target instead + * of a credential reference. */ +export function expandExternalSummaryChain(chain: ExternalSummaryChain, recipes: Record): ExternalSummarySettings { if (!chain.enabled) return { enabled: false, targets: [], budget: chain.budget }; const names = new Set(); const targets = chain.targets.map((ref): ExternalSummaryTarget => { @@ -116,8 +124,9 @@ export function expandExternalSummaryChain(chain: ExternalSummaryChain, recipes: const outputTokens = integer(knobs.outputTokens, Math.min(8192, Math.floor(contextWindow / 4)), 128, contextWindow - 1); if (knobs.stream !== undefined && typeof knobs.stream !== "boolean") throw new Error("External summary stream must be boolean"); const stream = knobs.stream === true; + const inlineKey = recipe.apiKey; const credentialRef = recipe.apiKeyEnv ? `env:${recipe.apiKeyEnv}` : `secret:${recipe.credentialRef}`; - return { name, protocol: recipe.api, url: derivedSummaryEndpoint(recipe, model, stream), model, credentialRef, contextWindow, outputTokens, stream }; + return { name, protocol: recipe.api, url: derivedSummaryEndpoint(recipe, model, stream), model, ...(inlineKey !== undefined ? { apiKey: inlineKey } : { credentialRef }), contextWindow, outputTokens, stream }; }); return { enabled: true, targets, budget: chain.budget }; } @@ -148,8 +157,12 @@ function derivedSummaryEndpoint(recipe: NamedProviderRecipe, model: string, stre /** EXPANDED-shape parser — the settings object as it rides the request * Config rail after expandExternalSummaryChain. Re-parsed by * ConfiguredSummaryPlan at execution time so a hand-built rail value gets - * the same validation as a file-driven one. */ -export function parseExternalSummarySettings(value: unknown): ExternalSummarySettings { + * the same validation as a file-driven one. `inlineKeys` (#2336) admits + * literal `apiKey` values on targets — the agent-registry recipes resolve + * their key in memory and hand bili the bytes; the FILE form keeps + * rejecting them (a plaintext key must never enter a config file). */ +export function parseExternalSummarySettings(value: unknown, options: { inlineKeys?: boolean } = {}): ExternalSummarySettings { + const inlineKeys = options.inlineKeys === true; const settings = object(value); knownKeys(settings, ["enabled", "targets", "budget"]); if (settings.enabled !== undefined && typeof settings.enabled !== "boolean") throw new Error("External summary enabled must be boolean"); @@ -163,7 +176,7 @@ export function parseExternalSummarySettings(value: unknown): ExternalSummarySet const names = new Set(); const targets = values.map((value): ExternalSummaryTarget => { const target = object(value); - knownKeys(target, ["name", "protocol", "url", "model", "credentialRef", "contextWindow", "outputTokens", "stream"]); + knownKeys(target, inlineKeys ? ["name", "protocol", "url", "model", "credentialRef", "apiKey", "contextWindow", "outputTokens", "stream"] : ["name", "protocol", "url", "model", "credentialRef", "contextWindow", "outputTokens", "stream"]); const name = text(target.name, 64); if (!validSummaryCredentialName(name) || names.has(name)) throw new Error("External summary target names must be unique identifiers"); names.add(name); @@ -176,9 +189,15 @@ export function parseExternalSummarySettings(value: unknown): ExternalSummarySet if ((url.protocol !== "https:" && !(url.protocol === "http:" && loopback)) || url.username || url.password || url.hash || [...url.searchParams.keys()].some((key) => key !== "alt") || url.pathname.includes("/bili/") || url.pathname.startsWith("/__bili/")) throw new Error("Invalid external summary endpoint; use HTTPS and no embedded credentials or proxy recursion"); - const credentialRef = text(target.credentialRef, 128); - if (!/^env:[A-Za-z_][A-Za-z0-9_]*$/.test(credentialRef) - && !(credentialRef.startsWith("secret:") && validSummaryCredentialName(credentialRef.slice(7)))) throw new Error("Invalid external summary credential reference"); + const inlineKey = inlineKeys ? target.apiKey : undefined; + if (inlineKey !== undefined && (typeof inlineKey !== "string" || inlineKey.length === 0 || inlineKey.length > 8192 || /[\x00-\x20\x7f]/.test(inlineKey))) throw new Error("Invalid external summary api key"); + if (inlineKey !== undefined && target.credentialRef !== undefined) throw new Error("External summary target carries both apiKey and credentialRef — exactly one credential is allowed"); + let credentialRef: string | undefined; + if (inlineKey === undefined) { + credentialRef = text(target.credentialRef, 128); + if (!/^env:[A-Za-z_][A-Za-z0-9_]*$/.test(credentialRef) + && !(credentialRef.startsWith("secret:") && validSummaryCredentialName(credentialRef.slice(7)))) throw new Error("Invalid external summary credential reference"); + } const contextWindow = integer(target.contextWindow, 128_000, 2048, 10_000_000); const outputTokens = integer(target.outputTokens, Math.min(8192, Math.floor(contextWindow / 4)), 128, contextWindow - 1); if (target.stream !== undefined && typeof target.stream !== "boolean") throw new Error("External summary stream must be boolean"); @@ -187,7 +206,7 @@ export function parseExternalSummarySettings(value: unknown): ExternalSummarySet const suffix = target.stream === true ? "streamGenerateContent" : "generateContent"; if (!url.pathname.endsWith(`/models/${encodeURIComponent(model.replace(/^models\//, ""))}:${suffix}`)) throw new Error("Google summary endpoint must match the model and stream mode"); } - return { name, protocol, url: url.href, model, credentialRef, contextWindow, outputTokens, stream: target.stream === true }; + return { name, protocol, url: url.href, model, ...(inlineKey !== undefined ? { apiKey: inlineKey } : { credentialRef }), contextWindow, outputTokens, stream: target.stream === true }; }); return { enabled: true, targets, budget: parseBudget(settings.budget) }; } diff --git a/src/lane-credentials.ts b/src/lane-credentials.ts new file mode 100644 index 0000000000..6658bb94c9 --- /dev/null +++ b/src/lane-credentials.ts @@ -0,0 +1,92 @@ +import { findRoute, type ProxyOptions } from "./config.js"; +import { SummaryCredentialStore } from "./external-summary-credentials.js"; + +/** + * #2336 lane credential override (forward path). `providers[URL].apiKeyEnv` + * / `credentialRef` let a lane send ITS OWN key upstream instead of the + * client's credential — the owner's blueprint case: the agent-side registry + * (pi) supplies a provider entry without a URL, bili's URL-keyed lane names + * its own key, and the summary chain borrows the lane identity instead of the + * client's. Resolution is per-egress-request but cheap: route lookup + + * credential resolve, with the failure warning deduped per route+reference so + * a permanently unset variable cannot spam the log on every request. + * + * Failure posture: resolve-miss (env unset / secret absent / store unreadable) + * keeps the CLIENT's headers untouched and warns once — visible, never + * silently dropping the request's auth to a guaranteed 401. + */ + +export interface LaneCredential { + key: string; + reference: string; +} + +const warnedLaneCredentials = new Set(); +let store = new SummaryCredentialStore(); + +/** Test seam (same convention as persist.ts `_setStoreForTest`): unit tests + * stub the credential store instead of touching the real private file. */ +export function _setLaneCredentialStoreForTest(replacement: SummaryCredentialStore | undefined): void { + store = replacement ?? new SummaryCredentialStore(); +} + +/** Resolve the lane credential for an upstream URL, or undefined when the + * lane declares none / the declared credential cannot be resolved (warned + * once per route+reference; re-arms after a success so a LATER breakage + * warns again). */ +export function laneCredential( + routes: ProxyOptions["routes"], + upstreamUrl: string | undefined, + warn: (message: string) => void, + env: NodeJS.ProcessEnv = process.env, +): LaneCredential | undefined { + const route = findRoute(routes, upstreamUrl); + if (!route || (route.apiKeyEnv === undefined && route.credentialRef === undefined)) return undefined; + const reference = route.apiKeyEnv !== undefined ? `env:${route.apiKeyEnv}` : `secret:${route.credentialRef}`; + const signature = `${upstreamUrl ?? ""}|${reference}`; + const fail = (reason: string): undefined => { + if (!warnedLaneCredentials.has(signature)) { + warnedLaneCredentials.add(signature); + warn(`[lane-credential] ${reference} for ${upstreamUrl ?? "the lane"} ${reason} — keeping the client's own headers (#2336)`); + } + return undefined; + }; + let key: string | undefined; + try { + key = store.resolve(reference, env); + } catch (error) { + return fail(`cannot be read (${error instanceof Error ? error.message : String(error)})`); + } + if (key === undefined) return fail("is not set"); + warnedLaneCredentials.delete(signature); + return { key, reference }; +} + +/** Land a resolved lane credential on the shared forward-header object. + * Header-replacement rules (documented on ProviderRoute.apiKeyEnv): + * - `x-api-key` / `x-goog-api-key` present → replaced outright; + * - `authorization` present → replaced ONLY when it is a Bearer token + * (anything else is a signature the client computed — replacing it + * would desynchronize body and signature); + * - none of the three present → inject `authorization: Bearer `. + * Applied once to the shared `headers` record so every egress reusing it + * (initial send, role-retry, refold, compress-loop, continuation) inherits + * the override; call sites skip this when the #1884 re-sign arm is active. */ +export function applyLaneCredential(headers: Record, credential: LaneCredential, warn: (message: string) => void): void { + const keyOf = (name: string): string | undefined => { + if (name in headers) return name; + return Object.keys(headers).find((h) => h.toLowerCase() === name); + }; + const authorization = keyOf("authorization"); + const xApiKey = keyOf("x-api-key"); + const xGoogKey = keyOf("x-goog-api-key"); + if (xApiKey !== undefined) headers[xApiKey] = credential.key; + if (xGoogKey !== undefined) headers[xGoogKey] = credential.key; + if (authorization !== undefined) { + if (/^bearer\s/i.test(headers[authorization])) headers[authorization] = `Bearer ${credential.key}`; + else if (xApiKey === undefined && xGoogKey === undefined) + warn(`[lane-credential] authorization on this lane is not a Bearer token — signature-owned, left untouched (#2336)`); + } else if (xApiKey === undefined && xGoogKey === undefined) { + headers.authorization = `Bearer ${credential.key}`; + } +} diff --git a/src/server.ts b/src/server.ts index 6fcf2509a5..bdee7e3988 100644 --- a/src/server.ts +++ b/src/server.ts @@ -111,6 +111,8 @@ import { rewriteGoogleJsonResponseAsync } from "./stream-google.js"; import { rewriteResponsesJsonResponseAsync } from "./stream-responses.js"; import { observeResponsesTerminalState } from "./stream-terminal.js"; import { emitPreflightError, emitStreamError } from "./stream-error.js"; +import { applyLaneCredential, laneCredential } from "./lane-credentials.js"; +import { agentProviderRecipes } from "./agent-providers.js"; import { affinityToken, claudeSubagentAgentId, claudeSubagentSplit, clientConversationHeader, codexTurnIdentity, conversationHeaderSource, dshPersonaFingerprintApplies, instructionsFingerprintApplies, openaiSystemTextForPersona, preferPromptCacheKeyIdentity, shouldStampRelayAffinityPck, type ConversationIdentity } from "./session-id.js"; import { personaNamespace } from "./persona-anchor.js"; import { prefixAffinity, type AnonymousAffinity } from "./prefix-affinity.js"; @@ -1689,7 +1691,9 @@ async function handle( native = capRegistryWindowByStandard(model, await contextFromRegistry(model, host), hasTierEvidence); if (native) nativeFromFallback = false; } - reqConfig = resolveRequestConfig(config, opts.routes, embeddedUrl, model, native, opts.compress, opts.namedProviders ?? {}); + // #2336: agent-registry recipes are the FALLBACK layer — bili's + // own named providers (file) win per provider name. + reqConfig = resolveRequestConfig(config, opts.routes, embeddedUrl, model, native, opts.compress, { ...agentProviderRecipes(), ...opts.namedProviders ?? {} }); { const wsSource = betaWindow ? "anthropic-beta" : suffixWindow ? "model-suffix" : pluginWindow ? "plugin" : runtimeWindow ? "runtime-info" : launcherWindow ? "launcher" : configuredWindow ? "configured" : peekWindow ? "registry-peek" : native ? "table-or-registry" : "default"; wsSourceForLog = wsSource; @@ -6131,6 +6135,19 @@ async function forward( if (resignCtx !== undefined) { log("info", `[${prepared?.session.id ?? "passthrough"}] [resign] re-sign arm active (${APIG_RESIGN_SCHEME}) — every egress body is re-signed (#1884)`); } + // #2336 lane credential override: providers[URL].apiKeyEnv/credentialRef + // replaces the client's credential with the lane's own key. Applied ONCE + // to the shared `headers` record (every egress below reuses it), and + // skipped when the re-sign arm is active — the re-signer owns + // Authorization there. Resolution failures keep the client's headers + // (warned once per route+reference inside laneCredential). + if (resignCtx === undefined) { + const laneCred = laneCredential(opts.routes, upstreamUrl, (message) => log("warn", `[${prepared?.session.id ?? "passthrough"}] ${message}`)); + if (laneCred !== undefined) { + applyLaneCredential(headers, laneCred, (message) => log("warn", `[${prepared?.session.id ?? "passthrough"}] ${message}`)); + log("debug", `[${prepared?.session.id ?? "passthrough"}] [lane-credential] applied ${laneCred.reference} for ${upstreamUrl} (#2336)`); + } + } const applyResign = (hdrs: Record, bodyStr: string | Buffer): void => { if (resignCtx === undefined || req.method === "GET" || req.method === "HEAD") return; try { diff --git a/src/server/admin.ts b/src/server/admin.ts index 4a8972937d..f223bcc268 100644 --- a/src/server/admin.ts +++ b/src/server/admin.ts @@ -13,6 +13,7 @@ import { fetchWithTimeout } from "../fetch-util.js"; import { log as loggerLog, getLogPath } from "../logger.js"; import { getBlindTunnelStats } from "../mitm.js"; import { handlePluginCompact, handlePluginFork, handlePluginManifest, handlePluginRegister, handlePluginRuntimeInfo, handlePluginSnapshot, handlePluginStatus, handlePluginTool } from "../plugin.js"; +import { parseAgentProviderReport, recordAgentProviders, agentProviderRecipes } from "../agent-providers.js"; import { defaultLogFile } from "../paths.js"; import { getUnrecognizedPathStats } from "./observability.js"; import { BodyTooLargeError, headerValue, readBody, selfAdminProbePath } from "../server.js"; @@ -281,7 +282,7 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv // (DEFAULT_CCR_CONFIG et al. inside applyCompressSettings); per-request/route overrides // are still enforced at execution time, so the manifest stays conservative as #1192 // requires. Do not "simplify" this back to `config`. - return handlePluginManifest(res, applyCompressSettings(config, opts.modelContextLimit, opts.compress, opts.namedProviders ?? {})); + return handlePluginManifest(res, applyCompressSettings(config, opts.modelContextLimit, opts.compress, { ...agentProviderRecipes(), ...opts.namedProviders ?? {} })); } if (req.method === "GET" && req.url?.split("?")[0] === "/__bili/plugin/snapshot") { return await handlePluginSnapshot(new URL(req.url, "http://localhost").searchParams.get("conversationId") ?? "", res); @@ -358,6 +359,24 @@ export async function handleAdminRoute(req: http.IncomingMessage, res: http.Serv return; } } + if (req.method === "POST" && req.url === "/__bili/agent-providers") { + // #2336 agent-registry fallback: a plugin host reports its dialing + // recipes (key bytes resolved in the agent's memory). Names only in + // the response — the key never crosses a log or GET surface. + try { + const body = await readBody(req); + const report = parseAgentProviderReport(JSON.parse(body.toString("utf8"))); + recordAgentProviders(report.agent, report.providers); + log("info", `[agent-providers] ${report.agent} registered: ${Object.keys(report.providers).sort().join(", ")} (#2336)`); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: true, agent: report.agent, providers: Object.keys(report.providers) })); + return; + } catch (err) { + res.writeHead(err instanceof BodyTooLargeError ? 413 : 400, { "content-type": "application/json" }); + res.end(JSON.stringify({ ok: false, error: err instanceof Error ? err.message : String(err) })); + return; + } + } if (req.method === "POST" && req.url === "/__bili/plugin/runtime-info") { try { const body = await readBody(req); diff --git a/src/server/pi-subagent-channel.ts b/src/server/pi-subagent-channel.ts index e04caff2f2..404a5dd8f7 100644 --- a/src/server/pi-subagent-channel.ts +++ b/src/server/pi-subagent-channel.ts @@ -40,7 +40,7 @@ const BILI_INJECTABLE_TOOL_NAMES = new Set([ "image_full", ]); -export type PiSubagentChildSignal = { present: false } | { present: true; agent?: string }; +type PiSubagentChildSignal = { present: false } | { present: true; agent?: string }; /** Detect the pi-subagents child marker in the raw request body. The marker * lives inside a JSON string value; `<`, letters, spaces and `=` are never diff --git a/src/web/api.ts b/src/web/api.ts index e37b2e3878..69f4cd5a33 100644 --- a/src/web/api.ts +++ b/src/web/api.ts @@ -22,6 +22,7 @@ import { log } from "../logger.js"; import { validateHttpProxy } from "../upstream-proxy.js"; import { SummaryCredentialStore } from "../external-summary-credentials.js"; import { parseExternalSummaryChain, expandExternalSummaryChain } from "../external-summary-settings.js"; +import { agentProviderRecipes, agentRegistryStatus } from "../agent-providers.js"; type ConfigShape = Record & { providers?: Record; @@ -129,6 +130,7 @@ export async function handleConfigGet(res: ServerResponse): Promise { upstreamProxyMode: upstream.mode, compress: config.compress ?? null, externalSummaryCredentials: credentialStatus, + agentProviders: agentRegistryStatus(), passthrough: passthroughState(process.env), allowDshCompaction: allowDshCompactionState(process.env), ...(existsSync(configFile()) ? { raw: hideInvalidSummary ? JSON.stringify(config, null, 2) : readFileSync(configFile(), "utf8") } : {}), @@ -220,9 +222,14 @@ export async function handleConfigPut( if (next.compress !== undefined && next.compress !== null) { const parsed = parseCompressSettings(next.compress); if (parsed?.externalSummary?.enabled === true) { - const recipes = next.providers !== undefined && typeof next.providers === "object" && !Array.isArray(next.providers) + // #2336: agent-registry recipes are a fallback layer — a + // chain may reference them without duplicating the dialing + // config in the file (file recipes win on name collision, + // matching the request-path merge order). + const fileRecipes = next.providers !== undefined && typeof next.providers === "object" && !Array.isArray(next.providers) ? collectNamedProviders(next.providers as Record) : loadNamedProviders(); + const recipes = { ...agentProviderRecipes(), ...fileRecipes }; try { expandExternalSummaryChain(parsed.externalSummary, recipes); } catch (error) { return sendError(res, 400, `compress.externalSummary cannot be resolved: ${String(error)}`); } @@ -298,6 +305,18 @@ export async function handleConfigPut( if (hasCompress) { compress = body.compress === null ? {} : parseCompressSettings(body.compress); if (compress === undefined) return sendError(res, 400, "invalid compress settings"); + // Same dangling-reference guard as the whole-file save: the chain + // must expand against the providers BEING SAVED (or the on-disk + // table when providers are untouched) plus the agent-registry + // fallback layer (#2336 — file recipes win on name collision). + if (compress.externalSummary?.enabled === true) { + const fileRecipes = hasProviders && body.providers !== null && typeof body.providers === "object" && !Array.isArray(body.providers) + ? collectNamedProviders(body.providers as Record) + : loadNamedProviders(); + try { expandExternalSummaryChain(compress.externalSummary, { ...agentProviderRecipes(), ...fileRecipes }); } catch (error) { + return sendError(res, 400, `compress.externalSummary cannot be resolved: ${String(error)}`); + } + } } // #405: the panel must be able to READ and CLEAR passthrough. An env diff --git a/src/web/client.ts b/src/web/client.ts index 5ca90fa88b..c6f046600e 100644 --- a/src/web/client.ts +++ b/src/web/client.ts @@ -1763,7 +1763,7 @@ export const WEB_CLIENT = `(function () { lab.appendChild(title); const inp = document.createElement(options ? "select" : "input"); inp.id = id; inp.className = "field-input"; - if (options) options.forEach((v) => { const opt = document.createElement("option"); opt.value = v; opt.textContent = v; inp.appendChild(opt); }); + if (options) options.forEach((v) => { const opt = document.createElement("option"); opt.value = typeof v === "object" ? v.value : v; opt.textContent = typeof v === "object" ? v.label : v; inp.appendChild(opt); }); else { inp.type = type || "text"; if (type === "number") { inp.min = "1"; inp.step = "1"; } } inp.value = value === undefined ? "" : String(value); inp.addEventListener("change", () => change(type === "number" ? Number(inp.value) : inp.value)); @@ -1783,7 +1783,21 @@ export const WEB_CLIENT = `(function () { const recipes = Object.keys(providers).filter((name) => name.indexOf("http://") !== 0 && name.indexOf("https://") !== 0 && providers[name] && typeof providers[name] === "object" && !Array.isArray(providers[name]) && (providers[name].baseUrl !== undefined || providers[name].api !== undefined)).map((name) => ({ name, recipe: providers[name] })); const options = []; recipes.forEach(({ name, recipe }) => { Object.keys(recipe.models && typeof recipe.models === "object" ? recipe.models : {}).forEach((model) => options.push(name + "/" + model)); }); + // #2336: agent-registry fallback — providers the host agent + // reported live (dialing lives in its memory, not in this file). + // Offered with an agent marker so the operator knows editing them + // here is not possible; the ref itself saves as provider/model. + (Array.isArray(cfg.agentProviders) ? cfg.agentProviders : []).forEach((group) => { + (group.providers || []).forEach((p) => { + (p.models || []).forEach((model) => { + const ref = p.name + "/" + model; + if (options.some((o) => (typeof o === "object" ? o.value : o) === ref)) return; + options.push({ value: ref, label: ref + " (" + (group.agent || "agent") + ")" }); + }); + }); + }); const head = document.createElement("div"); head.className = "summary-actions"; + const plain = (v) => typeof v === "object" ? v.value : v; const enabled = document.createElement("label"); const toggle = document.createElement("input"); toggle.type = "checkbox"; toggle.id = "summary-enabled"; toggle.checked = s.enabled === true; toggle.disabled = !!cfg.parseError; @@ -1791,7 +1805,7 @@ export const WEB_CLIENT = `(function () { enabled.append(toggle, document.createTextNode(" " + t("summary.enabled"))); head.appendChild(enabled); const add = button(head, t("summary.add"), () => mutate((v) => { if (!v.targets) v.targets = []; - v.targets.push(options.find((option) => v.targets.indexOf(option) === -1) || ""); + v.targets.push(plain(options.find((option) => v.targets.indexOf(plain(option)) === -1)) || ""); }, true)); add.disabled = !!cfg.parseError || (s.targets || []).length >= 16 || (options.length === 0 && (s.targets || []).length > 0); box.appendChild(head); @@ -1803,7 +1817,7 @@ export const WEB_CLIENT = `(function () { const legend = document.createElement("legend"); legend.textContent = String(i + 1) + ". " + (ref || "—"); item.appendChild(legend); const fields = document.createElement("div"); fields.className = "summary-grid"; const refOptions = options.slice(); - if (ref && refOptions.indexOf(ref) === -1) refOptions.unshift(ref); + if (ref && refOptions.map(plain).indexOf(ref) === -1) refOptions.unshift(ref); const set = (value) => mutate((v) => { v.targets[i] = String(value).trim(); }, true); field(fields, "summary-" + i + "-ref", t("summary.ref"), ref, set, refOptions.length ? null : "text", refOptions.length ? refOptions : undefined); item.appendChild(fields); diff --git a/tests/agent-providers.test.ts b/tests/agent-providers.test.ts new file mode 100644 index 0000000000..8576830882 --- /dev/null +++ b/tests/agent-providers.test.ts @@ -0,0 +1,310 @@ +// #2336: agent-registry fallback. Pins the intake contract (POST +// /__bili/agent-providers), the registry primitives (in-memory only, file +// recipes win on the request path), the web surface (names only — a key +// never crosses GET), the inline-key rail (settings reject apiKey from the +// file world, the executor honors it), and the PUT validation merge (a +// chain may reference agent providers without duplicating them in the file). +import test from "node:test"; +import assert from "node:assert/strict"; +import http from "node:http"; +import { once } from "node:events"; +import { mkdtempSync, writeFileSync } from "node:fs"; +import { tmpdir } from "node:os"; +import path from "node:path"; +import { defaultConfig, type Config, type CoreMessage } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { applyConfiguredCompression } from "../src/external-summary-compress.ts"; +import { parseAgentProviderReport, recordAgentProviders, agentProviderRecipes, agentRegistryStatus } from "../src/agent-providers.ts"; +import { reportAgentProviders, type Ctx } from "../src/agent/pi.ts"; +import { parseExternalSummarySettings } from "../src/external-summary-settings.ts"; +import { createCore } from "acp-kernel"; +import { createSession, type Session } from "../src/session.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; +import type { RewriteCtx } from "../src/stream.ts"; +import { rmrf } from "./tmp-rm.ts"; + +process.env.NODE_ENV = "test"; + +const VALID_REPORT = { + agent: "pi", + providers: { + zhipu: { baseUrl: "https://open.bigmodel.cn/api/paas/v4/", api: "openai", apiKey: "agent-resolved-key", models: { "glm-5": { contextWindow: 200_000, outputTokens: 8192 }, "glm-5-air": {} } }, + claude: { baseUrl: "https://api.anthropic.com", api: "anthropic", apiKey: "sk-ant-agent", models: { "claude-haiku": { stream: true } } }, + }, +}; + +test("#2336 parseAgentProviderReport accepts and normalizes a valid report", () => { + const { agent, providers } = parseAgentProviderReport(VALID_REPORT); + assert.equal(agent, "pi"); + assert.deepEqual(Object.keys(providers).sort(), ["claude", "zhipu"]); + assert.equal(providers.zhipu.baseUrl, "https://open.bigmodel.cn/api/paas/v4", "trailing slash trimmed"); + assert.equal(providers.zhipu.api, "openai"); + assert.equal(providers.zhipu.models["glm-5-air"].contextWindow, undefined, "absent knobs stay absent"); +}); + +test("#2336 parseAgentProviderReport rejects malformed payloads", () => { + const rejects: Array<[string, unknown]> = [ + ["not an object", "nope"], + ["missing agent", { providers: { a: VALID_REPORT.providers.zhipu } }], + ["bad agent name", { agent: "-leading-dash", providers: { a: VALID_REPORT.providers.zhipu } }], + ["missing providers", { agent: "pi" }], + ["empty providers", { agent: "pi", providers: {} }], + ["bad provider name", { agent: "pi", providers: { "no/slash": VALID_REPORT.providers.zhipu } }], + ["missing baseUrl", { agent: "pi", providers: { a: { api: "openai", apiKey: "k", models: { m: {} } } } }], + ["remote http baseUrl", { agent: "pi", providers: { a: { baseUrl: "http://example.com", api: "openai", apiKey: "k", models: { m: {} } } } }], + ["bili recursion baseUrl", { agent: "pi", providers: { a: { baseUrl: "https://h/__bili/x", api: "openai", apiKey: "k", models: { m: {} } } } }], + ["bad api enum", { agent: "pi", providers: { a: { baseUrl: "https://h", api: "grpc", apiKey: "k", models: { m: {} } } } }], + ["empty apiKey", { agent: "pi", providers: { a: { baseUrl: "https://h", api: "openai", apiKey: "", models: { m: {} } } } }], + ["control chars in apiKey", { agent: "pi", providers: { a: { baseUrl: "https://h", api: "openai", apiKey: "bad\nkey", models: { m: {} } } } }], + ["empty models", { agent: "pi", providers: { a: { baseUrl: "https://h", api: "openai", apiKey: "k", models: {} } } }], + ["bad contextWindow", { agent: "pi", providers: { a: { baseUrl: "https://h", api: "openai", apiKey: "k", models: { m: { contextWindow: 10 } } } } }], + ["bad outputTokens", { agent: "pi", providers: { a: { baseUrl: "https://h", api: "openai", apiKey: "k", models: { m: { outputTokens: 1 } } } } }], + ["bad stream flag", { agent: "pi", providers: { a: { baseUrl: "https://h", api: "openai", apiKey: "k", models: { m: { stream: "yes" } } } } }], + ]; + for (const [label, payload] of rejects) { + assert.throws(() => parseAgentProviderReport(payload), /.*/, label); + } + // loopback http stays allowed (local summary servers) + const local = parseAgentProviderReport({ agent: "pi", providers: { a: { baseUrl: "http://127.0.0.1:9000", api: "openai", apiKey: "k", models: { m: {} } } } }); + assert.equal(local.providers.a.baseUrl, "http://127.0.0.1:9000"); +}); + +test("#2336 registry: record/merge/status never leak keys or baseUrl", () => { + recordAgentProviders("pi", parseAgentProviderReport(VALID_REPORT).providers); + assert.deepEqual(Object.keys(agentProviderRecipes()).sort(), ["claude", "zhipu"]); + const status = JSON.stringify(agentRegistryStatus()); + assert.ok(status.includes("zhipu") && status.includes("glm-5"), "names and models exposed for the panel"); + assert.ok(!status.includes("agent-resolved-key") && !status.includes("sk-ant-agent"), "no key bytes in status"); + assert.ok(!status.includes("api.anthropic.com"), "no baseUrl in status"); + // a second agent replacing its own contribution leaves the other intact + recordAgentProviders("opencode", { sub: { baseUrl: "https://sub.example", api: "openai", apiKey: "k2", models: { m: {} } } }); + assert.deepEqual(Object.keys(agentProviderRecipes()).sort(), ["claude", "sub", "zhipu"]); + recordAgentProviders("pi", {}); + assert.deepEqual(Object.keys(agentProviderRecipes()), ["sub"], "empty re-report clears that agent's layer"); + recordAgentProviders("opencode", {}); +}); + +test("#2336 inline-key rail: file-world parse rejects apiKey, inlineKeys accepts with XOR", () => { + const target = { name: "t", protocol: "responses", url: "https://h.example", model: "m", apiKey: "inline-key" }; + assert.throws(() => parseExternalSummarySettings({ enabled: true, targets: [target] }), /credentialRef/, "file world never accepts inline keys"); + const parsed = parseExternalSummarySettings({ enabled: true, targets: [target] }, { inlineKeys: true }); + assert.equal(parsed.targets[0].apiKey, "inline-key"); + assert.equal(parsed.targets[0].credentialRef, undefined); + assert.throws(() => parseExternalSummarySettings({ enabled: true, targets: [{ ...target, credentialRef: "env:X" }] }, { inlineKeys: true }), /both/); +}); + +// --------------------------------------------------------------------------- +// Integration: the live admin endpoint + web surfaces through a real server. + +function request(port: number, method: string, reqPath: string, payload?: unknown): Promise<{ status: number; body: string }> { + const { promise, resolve, reject } = Promise.withResolvers<{ status: number; body: string }>(); + const req = http.request({ host: "127.0.0.1", port, method, path: reqPath, headers: payload !== undefined ? { "content-type": "application/json" } : {} }, (res) => { + const chunks: Buffer[] = []; + res.on("data", (c: Buffer) => chunks.push(c)); + res.on("end", () => resolve({ status: res.statusCode ?? 0, body: Buffer.concat(chunks).toString("utf8") })); + }); + req.on("error", reject); + if (payload !== undefined) req.end(JSON.stringify(payload)); + else req.end(); + return promise; +} + +test("#2336 endpoint + web GET/PUT contract through a real server", async () => { + const dir = mkdtempSync(path.join(tmpdir(), "bili-agent-providers-")); + const file = path.join(dir, "billion-context.json"); + writeFileSync(file, JSON.stringify({ providers: { glm: { baseUrl: "https://open.bigmodel.cn/api/paas/v4", api: "openai", apiKeyEnv: "GLM_KEY", models: { "glm-5-file": {} } } }, compress: { externalSummary: { enabled: false, targets: ["glm/glm-5-file"] } } }), "utf8"); + const prevConfig = process.env.BILI_CONFIG_FILE; + process.env.BILI_CONFIG_FILE = file; + // Isolate from the machine's real session/prefix-affinity state so the + // server holds no long-lived handles (same discipline as #2028's suite). + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + const server = await startServer({ + 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: [] }, + port: 0, + host: "127.0.0.1", + } satisfies Parameters[0]); + await once(server, "listening"); + const port = (server.address() as import("node:net").AddressInfo).port; + try { + // intake: valid report accepted, malformed rejected + let r = await request(port, "POST", "/__bili/agent-providers", VALID_REPORT); + assert.equal(r.status, 200); + assert.equal(JSON.parse(r.body).ok, true); + assert.ok(!r.body.includes("agent-resolved-key"), "response carries names only"); + r = await request(port, "POST", "/__bili/agent-providers", { agent: "pi", providers: { a: { baseUrl: "https://h", api: "grpc", apiKey: "k", models: { m: {} } } } }); + assert.equal(r.status, 400); + + // GET exposes the registry layer without any secret material + r = await request(port, "GET", "/__bili/config"); + const cfg = JSON.parse(r.body) as { agentProviders?: Array<{ agent: string; providers: Array<{ name: string; models: string[] }> }> }; + assert.deepEqual(cfg.agentProviders, [{ agent: "pi", providers: [{ name: "claude", models: ["claude-haiku"] }, { name: "zhipu", models: ["glm-5", "glm-5-air"] }] }], "status shape feeds the panel dropdown"); + assert.ok(!r.body.includes("agent-resolved-key") && !r.body.includes("sk-ant-agent")); + + // PUT: a chain may reference an agent-only provider (zhipu lives only in the agent layer) + r = await request(port, "PUT", "/__bili/config", { compress: { externalSummary: { enabled: true, targets: ["zhipu/glm-5", "claude/claude-haiku"] } } }); + assert.equal(r.status, 200, `agent refs resolve at save time: ${r.body}`); + + // PUT: file recipes win on name collision — glm is file-owned (glm-5-file + // only), so the agent layer's glm models are shadowed, and a ref to a + // file-known provider's unknown model is rejected + r = await request(port, "PUT", "/__bili/config", { compress: { externalSummary: { enabled: true, targets: ["glm/glm-5-file"] } } }); + assert.equal(r.status, 200, `file-owned ref works: ${r.body}`); + r = await request(port, "PUT", "/__bili/config", { compress: { externalSummary: { enabled: true, targets: ["glm/glm-5"] } } }); + assert.equal(r.status, 400, "file recipe shadows the same-named agent provider entirely"); + + // PUT: unknown provider still rejected + r = await request(port, "PUT", "/__bili/config", { compress: { externalSummary: { enabled: true, targets: ["nobody/model"] } } }); + assert.equal(r.status, 400); + } finally { + process.env.BILI_CONFIG_FILE = prevConfig === undefined ? "" : prevConfig; + if (prevConfig === undefined) delete process.env.BILI_CONFIG_FILE; + server.closeAllConnections?.(); + await new Promise((resolve) => server.close(() => resolve())); + rmrf(dir); + } +}); + +// --------------------------------------------------------------------------- +// Inline-key rail end-to-end: an apiKey-bearing target dials out with the +// inline key and no credential store involved. + +const RAW = "Historical source for the inline-key dial test. ".repeat(120); + +function context(externalSummary: unknown): RewriteCtx { + const core = createCore(); + const config = defaultConfig(400_000) as Config & { externalSummary?: unknown }; + config.externalSummary = externalSummary; + config.preserveRecentMessages = 0; + config.preserveRecentTokens = 0; + config.compress.minCompressRange = 100; + const session: Session = createSession(`agent-providers-${Math.random()}`); + const messages: CoreMessage[] = [ + { id: "history", role: "assistant", contentType: "text", text: RAW }, + { id: "current", role: "user", contentType: "text", text: "Current task." }, + ]; + session.state = core.processTurn({ messages, state: session.state, config, tokenCount: 10000, renderTags: "text-only" }).state; + return { core, config, session, messages, log: () => {} }; +} + +test("#2336 inline apiKey target dials with the inline key (no store)", async () => { + const seen: string[] = []; + const upstream = http.createServer((req, res) => { + seen.push(String(req.headers.authorization ?? "")); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "chatcmpl-1", choices: [{ index: 0, message: { role: "assistant", content: "Inline summary of the range: the history was folded by the agent-reported provider with its in-memory key." }, finish_reason: "stop" }] })); + }); + upstream.listen(0, "127.0.0.1"); + await once(upstream, "listening"); + const base = `http://127.0.0.1:${(upstream.address() as import("node:net").AddressInfo).port}`; + try { + const ctx = context({ enabled: true, targets: [{ name: "agent-glm", protocol: "openai", url: `${base}/v1/chat/completions`, model: "glm-5", apiKey: "agent-resolved-key" }] }); + const startId = ctx.session.state.messageRefs.byRaw.history; + const result = await applyConfiguredCompression({ content: [{ startId, endId: startId }] }, ctx); + assert.equal(result.outcome, "applied", `compression applied: ${result.text}`); + assert.deepEqual(seen, ["Bearer agent-resolved-key"], "inline key reaches the wire verbatim"); + } finally { + upstream.closeAllConnections(); + await new Promise((resolve) => upstream.close(() => resolve())); + } +}); + +test("#2336 pi reportAgentProviders: sanitized table, skip rules, POST shape", async () => { + const realFetch = globalThis.fetch; + const prevProxy = process.env.BILLION_CONTEXT_PROXY; + process.env.BILLION_CONTEXT_PROXY = "http://127.0.0.1:8787"; + const calls: Array<{ url: string; body: unknown }> = []; + globalThis.fetch = (async (input: string | URL | Request, init?: RequestInit) => { + calls.push({ url: String(input), body: JSON.parse(String(init?.body)) }); + return new Response(JSON.stringify({ ok: true }), { status: 200 }); + }) as typeof fetch; + type FakeModel = { id: string; provider: string; api?: string; contextWindow?: number; maxTokens?: number }; + const models: FakeModel[] = [ + { id: "glm-5", provider: "zhipu", api: "openai-completions", contextWindow: 200_000, maxTokens: 8192 }, + { id: "glm-5-air", provider: "zhipu", api: "openai-completions" }, + { id: "claude-haiku", provider: "claude", api: "anthropic-messages" }, + { id: "gpt-x", provider: "openai", api: "openai-responses" }, // responses mapping + { id: "m1", provider: "bedrock", api: "bedrock-converse-stream" }, // unmapped api -> skip + { id: "m2", provider: "bad/name", api: "openai-completions" }, // bad provider name -> skip + { id: "m3", provider: "vertex", api: "google-vertex" }, // unmapped api -> skip + { id: "m4", provider: "mistral", api: "mistral-conversations" }, // unmapped api -> skip + { id: "m5", provider: "authjson", api: "openai-completions" }, // source=stored -> skip + { id: "m6", provider: "oauthp", api: "openai-completions" }, // oauth -> skip + { id: "m7", provider: "selfloop", api: "openai-completions" }, // baseUrl points at bili -> skip + { id: "m8", provider: "nokey", api: "openai-completions" }, // key resolution empty -> skip + ]; + const ctx = { + model: { baseUrl: "http://127.0.0.1:8787", api: "not-virtual" }, + modelRegistry: { + getAll: () => models, + getProvider: (p: string) => ({ + zhipu: { baseUrl: "https://open.bigmodel.cn/api/paas/v4" }, + claude: { baseUrl: "https://api.anthropic.com" }, + openai: { baseUrl: "https://api.openai.com/v1" }, + authjson: { baseUrl: "https://a.example" }, + oauthp: { baseUrl: "https://b.example", auth: { oauth: { kind: "oauth" } } }, + selfloop: { baseUrl: "http://127.0.0.1:8787" }, + nokey: { baseUrl: "https://c.example" }, + })[p], + getProviderAuthStatus: (p: string) => ({ + zhipu: { configured: true, source: "environment" }, + claude: { configured: true, source: "models_json_key" }, + openai: { configured: true, source: "runtime" }, + authjson: { configured: true, source: "stored" }, + oauthp: { configured: true, source: "environment" }, + selfloop: { configured: true, source: "environment" }, + nokey: { configured: false }, + })[p], + getApiKeyForProvider: async (p: string) => (p === "nokey" ? undefined : `key-${p}`), + }, + } as unknown as Ctx; + try { + const done = await reportAgentProviders(ctx, "pi"); + assert.equal(done, true); + assert.equal(calls.length, 1, "exactly one POST"); + assert.equal(calls[0].url, "http://127.0.0.1:8787/__bili/agent-providers"); + const body = calls[0].body as { agent: string; providers: Record }> }; + assert.equal(body.agent, "pi"); + assert.deepEqual(Object.keys(body.providers).sort(), ["claude", "openai", "zhipu"], "skip rules applied"); + assert.deepEqual(body.providers.zhipu.models, { "glm-5": { contextWindow: 200_000, outputTokens: 8192 }, "glm-5-air": {} }); + assert.equal(body.providers.zhipu.apiKey, "key-zhipu"); + assert.equal(body.providers.openai.api, "responses", "responses-family api mapping"); + // the report is accepted by the core parser as-is (client/server contract) + const parsed = parseAgentProviderReport(body); + assert.deepEqual(Object.keys(parsed.providers).sort(), ["claude", "openai", "zhipu"]); + } finally { + globalThis.fetch = realFetch; + if (prevProxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = prevProxy; + } +}); + +test("#2336 pi reportAgentProviders: no proxy base yet -> false (retry), missing registry face -> true (done)", async () => { + const prevProxy = process.env.BILLION_CONTEXT_PROXY; + delete process.env.BILLION_CONTEXT_PROXY; + try { + const retry = await reportAgentProviders({ model: { baseUrl: "https://api.openai.com", api: "not-virtual" } } as unknown as Ctx, "pi"); + assert.equal(retry, false, "no proxy base: report stays armed for the next session"); + process.env.BILLION_CONTEXT_PROXY = "http://127.0.0.1:8787"; + const done = await reportAgentProviders({ model: { baseUrl: "http://127.0.0.1:8787", api: "not-virtual" }, modelRegistry: { find: () => undefined } } as unknown as Ctx, "pi"); + assert.equal(done, true, "host without the full registry face: stop trying"); + } finally { + if (prevProxy === undefined) delete process.env.BILLION_CONTEXT_PROXY; + else process.env.BILLION_CONTEXT_PROXY = prevProxy; + } +}); diff --git a/tests/lane-credentials.test.ts b/tests/lane-credentials.test.ts new file mode 100644 index 0000000000..ce3eadf4db --- /dev/null +++ b/tests/lane-credentials.test.ts @@ -0,0 +1,195 @@ +import test from "node:test"; +import assert from "node:assert/strict"; +import nodeHttp from "node:http"; +import { once } from "node:events"; +import { defaultConfig } from "acp-kernel"; +import { startServer } from "../src/server.ts"; +import { SessionStore, _setStoreForTest } from "../src/persist.ts"; +import { parseRouteEntry, type ProviderRoutes } from "../src/config.ts"; +import { SummaryCredentialStore } from "../src/external-summary-credentials.ts"; +import { _setLaneCredentialStoreForTest, applyLaneCredential, laneCredential, type LaneCredential } from "../src/lane-credentials.ts"; +import { _setForTest as setRegistryForTest } from "../src/registry.ts"; + +const cred = (key: string, reference = "env:TEST"): LaneCredential => ({ key, reference }); + +test("applyLaneCredential: replaces a Bearer authorization (case-insensitive scheme)", () => { + const headers = { authorization: "bearer client-token" }; + applyLaneCredential(headers, cred("lane-key"), () => assert.fail("no warn expected")); + assert.equal(headers.authorization, "Bearer lane-key"); +}); + +test("applyLaneCredential: non-Bearer authorization is signature-owned and kept (warn)", () => { + const headers = { authorization: "SDK-HMAC-SHA256 signed=abc" }; + let warned = false; + applyLaneCredential(headers, cred("lane-key"), () => { warned = true; }); + assert.equal(headers.authorization, "SDK-HMAC-SHA256 signed=abc"); + assert.equal(warned, true); +}); + +test("applyLaneCredential: non-Bearer authorization + x-api-key — api key replaced, signature kept, no warn", () => { + const headers = { authorization: "SDK-HMAC-SHA256 signed=abc", "x-api-key": "client-token" }; + applyLaneCredential(headers, cred("lane-key"), () => assert.fail("no warn expected")); + assert.equal(headers.authorization, "SDK-HMAC-SHA256 signed=abc"); + assert.equal(headers["x-api-key"], "lane-key"); +}); + +test("applyLaneCredential: x-goog-api-key replaced outright", () => { + const headers = { "x-goog-api-key": "client-token" }; + applyLaneCredential(headers, cred("lane-key"), () => assert.fail("no warn expected")); + assert.equal(headers["x-goog-api-key"], "lane-key"); +}); + +test("applyLaneCredential: no auth headers at all — injects Bearer", () => { + const headers: Record = { "content-type": "application/json" }; + applyLaneCredential(headers, cred("lane-key"), () => assert.fail("no warn expected")); + assert.equal(headers.authorization, "Bearer lane-key"); +}); + +test("applyLaneCredential: bearer + x-api-key — both replaced", () => { + const headers = { authorization: "Bearer client-token", "x-api-key": "client-token" }; + applyLaneCredential(headers, cred("lane-key"), () => assert.fail("no warn expected")); + assert.equal(headers.authorization, "Bearer lane-key"); + assert.equal(headers["x-api-key"], "lane-key"); +}); + +test("laneCredential: env reference resolves from the passed env", () => { + const routes: ProviderRoutes = { "https://api.deepseek.com": { apiKeyEnv: "DEEPSEEK_LANE_KEY" } }; + const resolved = laneCredential(routes, "https://api.deepseek.com/v1/chat/completions", () => assert.fail("no warn expected"), { DEEPSEEK_LANE_KEY: "sk-lane" }); + assert.deepEqual(resolved, { key: "sk-lane", reference: "env:DEEPSEEK_LANE_KEY" }); +}); + +test("laneCredential: longest-prefix routing picks the most specific lane", () => { + const routes: ProviderRoutes = { + "https://api.deepseek.com": { apiKeyEnv: "A_LANE_KEY" }, + "https://api.deepseek.com/v2": { apiKeyEnv: "B_LANE_KEY" }, + }; + const first = laneCredential(routes, "https://api.deepseek.com/v1/chat", () => assert.fail("no warn expected"), { A_LANE_KEY: "a" }); + assert.deepEqual(first, { key: "a", reference: "env:A_LANE_KEY" }); + const second = laneCredential(routes, "https://api.deepseek.com/v2/chat", () => assert.fail("no warn expected"), { A_LANE_KEY: "a", B_LANE_KEY: "b" }); + assert.deepEqual(second, { key: "b", reference: "env:B_LANE_KEY" }); + const none = laneCredential(routes, "https://other.example.com/x", () => assert.fail("no warn expected"), { A_LANE_KEY: "a" }); + assert.equal(none, undefined); +}); + +test("laneCredential: unset env keeps client headers, warns ONCE per route+reference, re-arms after success", () => { + const routes: ProviderRoutes = { "https://api.example.com": { apiKeyEnv: "UNSET_LANE_KEY_X1" } }; + const warnings: string[] = []; + const warn = (m: string) => warnings.push(m); + const env = {}; + assert.equal(laneCredential(routes, "https://api.example.com/x", warn, env), undefined); + assert.equal(laneCredential(routes, "https://api.example.com/x", warn, env), undefined); + assert.equal(warnings.length, 1); + // env now set → resolves, warning slot re-armed + const resolved = laneCredential(routes, "https://api.example.com/x", warn, { UNSET_LANE_KEY_X1: "k" }); + assert.deepEqual(resolved, { key: "k", reference: "env:UNSET_LANE_KEY_X1" }); + assert.equal(laneCredential(routes, "https://api.example.com/x", warn, env), undefined); + assert.equal(warnings.length, 2); +}); + +test("laneCredential: secret: reference resolves through the (stubbed) store", () => { + const stub = { + resolve: (reference: string) => (reference === "secret:lane-primary" ? "sk-secret" : undefined), + } as unknown as SummaryCredentialStore; + _setLaneCredentialStoreForTest(stub); + try { + const routes: ProviderRoutes = { "https://api.example.com": { credentialRef: "lane-primary" } }; + const resolved = laneCredential(routes, "https://api.example.com/x", () => assert.fail("no warn expected"), {}); + assert.deepEqual(resolved, { key: "sk-secret", reference: "secret:lane-primary" }); + } finally { + _setLaneCredentialStoreForTest(undefined); + } +}); + +test("parseRouteEntry: lane credential fields parsed, validated, exactly-one (#2336)", () => { + assert.deepEqual(parseRouteEntry({ apiKeyEnv: "MY_KEY" }), { models: undefined, apiKeyEnv: "MY_KEY" }); + assert.deepEqual(parseRouteEntry({ credentialRef: "primary" }), { models: undefined, credentialRef: "primary" }); + assert.deepEqual(parseRouteEntry({ models: { m: { context: 8 } }, apiKeyEnv: "MY_KEY" }), { models: { m: { context: 8 } }, apiKeyEnv: "MY_KEY" }); + assert.throws(() => parseRouteEntry({ apiKeyEnv: "MY_KEY", credentialRef: "x" }), /exactly one lane credential/); + assert.throws(() => parseRouteEntry({ apiKeyEnv: "not a name" }), /apiKeyEnv must be an environment variable name/); + assert.throws(() => parseRouteEntry({ apiKeyEnv: 42 }), /apiKeyEnv must be an environment variable name/); + assert.throws(() => parseRouteEntry({ credentialRef: "has spaces" }), /credentialRef must be a name/); + // recipe-shaped entries keep their credential fields OUT of the route — + // parseNamedProviderRecipe owns them there (dialing recipes, not lanes) + const recipeRoute = parseRouteEntry({ baseUrl: "https://x", api: "openai", apiKeyEnv: "MY_KEY", models: { m: {} } }); + assert.equal((recipeRoute as { apiKeyEnv?: string }).apiKeyEnv, undefined); +}); + +function listen(server: nodeHttp.Server): Promise { + if (server.listening) return Promise.resolve(); + return once(server, "listening").then(() => undefined); +} + +test("forward: lane apiKeyEnv replaces the client's Bearer on the wire (#2336)", async () => { + _setStoreForTest(new SessionStore({ enabled: false })); + setRegistryForTest({}); + let seenAuth = ""; + let seenXKey = ""; + const upstream = nodeHttp.createServer((req, res) => { + seenAuth = String(req.headers["authorization"] ?? ""); + seenXKey = String(req.headers["x-api-key"] ?? ""); + res.writeHead(200, { "content-type": "application/json" }); + res.end(JSON.stringify({ id: "x", choices: [{ message: { role: "assistant", content: "ok" } }] })); + }); + upstream.listen(0, "127.0.0.1"); + await listen(upstream); + const upstreamPort = (upstream.address() as { port: number }).port; + const upstreamHost = `127.0.0.1:${upstreamPort}`; + + const opts = { + port: 0, + host: "127.0.0.1", + upstream: `http://${upstreamHost}`, + routes: { [`http://${upstreamHost}`]: { apiKeyEnv: "LANE_FORWARD_TEST_KEY" } }, + 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: [] }, + compat: { roles: {} }, streamErrorShape: "protocol" as const, passthroughSource: null, autoRestartOnUpdate: false, updateTag: "latest", advisoryCheck: false, releaseNotesCheck: false, + }; + const proxy = await startServer(opts as Parameters[0]); + await listen(proxy); + const proxyPort = (proxy.address() as { port: number }).port; + + const prev = process.env.LANE_FORWARD_TEST_KEY; + process.env.LANE_FORWARD_TEST_KEY = "sk-lane-forward"; + const body = JSON.stringify({ model: "gpt-test", stream: false, messages: [{ role: "user", content: "hi" }] }); + try { + await new Promise((resolve, reject) => { + const req = nodeHttp.request( + { + host: "127.0.0.1", + port: proxyPort, + method: "POST", + path: `http://${upstreamHost}/v1/chat/completions`, + headers: { + "content-type": "application/json", + host: upstreamHost, + "x-acp-session": "lane-forward-test", + "content-length": String(Buffer.byteLength(body)), + authorization: "Bearer sk-client-original", + "x-api-key": "sk-client-original", + }, + }, + (res) => { + res.resume(); + res.on("end", () => resolve()); + }, + ); + req.on("error", reject); + req.end(body); + }); + assert.equal(seenAuth, "Bearer sk-lane-forward"); + assert.equal(seenXKey, "sk-lane-forward"); + } finally { + if (prev === undefined) delete process.env.LANE_FORWARD_TEST_KEY; + else process.env.LANE_FORWARD_TEST_KEY = prev; + await new Promise((resolve) => proxy.close(() => resolve())); + await new Promise((resolve) => upstream.close(() => resolve())); + } +}); diff --git a/website/config-reference/providers.yaml b/website/config-reference/providers.yaml index 0034da0e36..dbd247c7af 100644 --- a/website/config-reference/providers.yaml +++ b/website/config-reference/providers.yaml @@ -146,6 +146,26 @@ entries: desc_zh: "把具名(非 URL)条目深合并到所绑定的 URL 通道作为别名;无 bind 的具名条目不参与路由。" site_en: "Named (non-URL) entry: deep-merged onto the bound URL lane at config load as an alias. Without bind a named entry stays routing-inert." site_zh: "具名(非 URL)条目:加载配置时深合并到所绑定的 URL 通道作为别名;无 bind 的具名条目不参与路由。" + - key: apiKeyEnv + type: "string (env var name)" + default: "unset" + group: providers + id: cr.lanekey + render: route-row + desc_en: "Lane credential override (#2336): replace the client's credential with this env variable's value for this lane. Exactly one of apiKeyEnv/credentialRef; see [Lane credentials](#lane-credentials-apikeyenv--credentialref)." + desc_zh: "通道凭据覆盖(#2336):用该环境变量的值替换客户端凭据。与 credentialRef 二选一;见[通道凭据](#lane-credentials-apikeyenv--credentialref)。" + site_en: "Lane credential override (#2336): requests forwarded through this lane carry this env variable's value instead of the client's own credential — x-api-key / x-goog-api-key replaced outright, Bearer authorization replaced, injected when absent. Exactly one of apiKeyEnv / credentialRef per entry." + site_zh: "通道凭据覆盖(#2336):经该通道转发的请求使用此环境变量的值取代客户端自带凭据——x-api-key / x-goog-api-key 直接替换、Bearer authorization 替换、缺失时注入。每个条目与 credentialRef 二选一。" + - key: credentialRef + type: "string (store name)" + default: "unset" + group: providers + id: cr.lanecred + render: route-row + desc_en: "Lane credential override via the private summary-credential store (secret:NAME semantics)." + desc_zh: "经私有摘要凭据存储的通道凭据覆盖(secret:NAME 语义)。" + site_en: "Lane credential override via the private summary-credential store (`secret:NAME` semantics); exactly one of apiKeyEnv / credentialRef per entry." + site_zh: "经私有摘要凭据存储的通道凭据覆盖(`secret:NAME` 语义);每个条目与 apiKeyEnv 二选一。" - key: compactionOptIn type: boolean default: "false" diff --git a/website/docs/index.html b/website/docs/index.html index 11e7f62ac3..57c35b84b6 100644 --- a/website/docs/index.html +++ b/website/docs/index.html @@ -235,6 +235,8 @@

providers — route table fields

imageTokenCapnumberPer-image token cost cap for this route; unset = uncapped. resignscheme → { enabled?, passthrough?, credentialRef? }Per-route overrides of the global resign map (level 2, deepest wins). bindstring (named entries only)Named (non-URL) entry: deep-merged onto the bound URL lane at config load as an alias. Without bind a named entry stays routing-inert. + apiKeyEnvstring (env var name)Lane credential override (#2336): requests forwarded through this lane carry this env variable's value instead of the client's own credential — x-api-key / x-goog-api-key replaced outright, Bearer authorization replaced, injected when absent. Exactly one of apiKeyEnv / credentialRef per entry. + credentialRefstring (store name)Lane credential override via the private summary-credential store (`secret:NAME` semantics); exactly one of apiKeyEnv / credentialRef per entry. compactionOptInbooleanNamed entries only: opt a non-http(s)-baseUrl provider into compaction ownership (pi/omp lanes); unions with env BILI_NON_HTTP_PROVIDERS. diff --git a/website/main.js b/website/main.js index 886834707a..c74740d9ce 100644 --- a/website/main.js +++ b/website/main.js @@ -105,6 +105,8 @@ "cr.itc": "Per-image token cost cap for this route; unset = uncapped.", "cr.resign": "Per-route overrides of the global resign map (level 2, deepest wins).", "cr.bind": "Named (non-URL) entry: deep-merged onto the bound URL lane at config load as an alias. Without bind a named entry stays routing-inert.", + "cr.lanekey": "Lane credential override (#2336): requests forwarded through this lane carry this env variable's value instead of the client's own credential — x-api-key / x-goog-api-key replaced outright, Bearer authorization replaced, injected when absent. Exactly one of apiKeyEnv / credentialRef per entry.", + "cr.lanecred": "Lane credential override via the private summary-credential store (`secret:NAME` semantics); exactly one of apiKeyEnv / credentialRef per entry.", "cr.optin": "Named entries only: opt a non-http(s)-baseUrl provider into compaction ownership (pi/omp lanes); unions with env BILI_NON_HTTP_PROVIDERS.", "tg.core": "Server & core", "tg.up": "Upstream & billing", @@ -336,6 +338,8 @@ "cr.itc": "本路由单张图片 token 成本上限;未设置 = 不限制。", "cr.resign": "按路由覆盖全局 resign 映射(第 2 级,最深层胜出)。", "cr.bind": "具名(非 URL)条目:加载配置时深合并到所绑定的 URL 通道作为别名;无 bind 的具名条目不参与路由。", + "cr.lanekey": "通道凭据覆盖(#2336):经该通道转发的请求使用此环境变量的值取代客户端自带凭据——x-api-key / x-goog-api-key 直接替换、Bearer authorization 替换、缺失时注入。每个条目与 credentialRef 二选一。", + "cr.lanecred": "经私有摘要凭据存储的通道凭据覆盖(`secret:NAME` 语义);每个条目与 apiKeyEnv 二选一。", "cr.optin": "仅命名条目:把非 http(s) baseUrl 供应商纳入压缩所有权(pi/omp 车道);与 env BILI_NON_HTTP_PROVIDERS 取并集。", "tg.core": "服务与核心", "tg.up": "上游与计费",