🤖 Open-source, local-first alternative to Grok Bot, OpenAI dots and Claude Duet. Your own AI bots on any model: GPT, Claude, Gemini, Grok, DeepSeek, Kimi, GLM and 70+ more through one Modelflare key. Reach them in the app, WeChat or Feishu; they remember you, work in the background, run commands and use your computer. Pay per use.
Python ai-agentai-assistantchatbotclaudecomputer-usedeepseekdesktop-appdshelectronfeishugeminigptgrokllmlocal-firstmodelflaremulti-modelopenai-compatiblepersonal-ai-agentwechat
feat(agent): talk to each model over its best Modelflare API
Marla73b6398e9fcc
diff --git a/README.md b/README.mdindex 52b9ad0..5180ad9 100644--- a/README.md+++ b/README.md@@ -90,6 +90,7 @@ docker exec amb cat /data/ui-token # 打开 http://<主机>:3600/?token=<令- 对话中输入框右侧的模型按钮可为当前会话切换模型(也可设为机器人默认);`/model`、`/reset`、`/new`、`/status`、`/stop`、`/help` 照常可用。- 处理过程:代理回合的每一步(模型请求及用时与 tokens、工具调用及参数与输出、重试、子代理)折叠在回复上方的"用时"一行里,点开查看;微信 / 飞书会话在信息面板 → 最近会话里看,正在处理的那一轮实时显示并可中止。- 卡住的消息:`/stop`(任何渠道)或信息面板里的「中止」立即结束正在处理的回合;代理 10 分钟没有任何进展(模型不回、工具不动、只在重试;等你批准或回答不算)会自动中止,单回合最长 30 分钟(`AMB_AGENT_IDLE_TIMEOUT_MS`、`AMB_AGENT_TURN_TIMEOUT_MS` 可调)。DSH 没有中断单个回合的接口,中止时会重启该机器人的代理进程。前一条还在处理时发来的新消息会先收到一句提示;`/status` 显示它走到了哪一步、多久没有进展。+- 代理的对话接口:代理按 Modelflare 公开目录里该模型支持的接口选——有 Responses API 的(GPT-5.4 及以后、Grok 4.5 及以后、DeepSeek V4、GLM-5、Kimi K3 等)走 `/v1/responses`(推理模型默认推理强度 medium,加密推理随历史回放);没有 Responses、有 Anthropic Messages 的(Claude)走 `/v1/messages`;其余模型、拿不到目录时(离线、中国大陆线路)以及外部端点走 Chat Completions。接口在拉起代理进程时选定(故障排查的代理状态里显示 `api`);`AMB_AGENT_API=chat` 强制全部走 Chat Completions(排查用)。纯对话(非代理)仍走 Chat Completions。- 内置网关:magpie 以 `--gateway` 模式运行,使用本程序数据目录内独立的 HOME/配置目录;**不读取**你电脑上已有的 Claude/Codex 等订阅凭据,**不改写**任何 agent 配置,关闭自动更新与账户发现。它只供本进程内部调用,界面不再反代它的页面或 `/v1`。- API Key 只保存在内置网关的隔离配置中,不经过命令行参数、不写日志,前端只拿到尾号。- 开发/测试:设置 `AMB_MAGPIE_BASE_URL` 可直接使用一个 OpenAI 兼容端点(不启动内置网关,界面视为已连接,账户功能不可用);`AMB_MODELFLARE_BASE_URL` 可把 Modelflare 基址指向本地 mock,`AMB_MODELFLARE_ISSUER` 把登录与账户接口指向本地 mock。diff --git a/src/account/model-info.ts b/src/account/model-info.tsindex 0611a58..9c5a027 100644--- a/src/account/model-info.ts+++ b/src/account/model-info.ts@@ -1,7 +1,7 @@/*** 模型选择器里的能力与参考价:* - 能力来自 GET {线路}/v1/model-profiles(带 Key;上下文、视觉、工具调用、推理)。- * - 价格来自公开价格目录 GET https://modelflare.dev/api/pricing(不带 Key,国内线路不开放)。+ * - 价格与模型支持的接口来自公开价格目录 GET https://modelflare.dev/api/pricing(不带 Key,国内线路不开放)。* 只算按 token 计费和按次计费的模型:价格 = 基础价 × 该模型可用公开分组里最低的分组倍率 ×* 模型售价倍率,单位美元 / 百万 tokens(按次为美元 / 次)。这是“起步参考价”,实际以账单为准;* 阶梯计费、图片、视频等复杂计费不展示价格。@@ -12,6 +12,8 @@ export interface ModelInfo {tools?: boolean;reasoning?: boolean;price?: { input?: number; output?: number; perCall?: number };+ /** 支持的接口(目录的 supported_endpoint_types:openai、openai-response、openai-response-compact、anthropic、gemini…) */+ endpoints?: string[];}interface Profile {@@ -33,6 +35,7 @@ export interface PricingEntry {billing_mode?: string;pricing_rules?: unknown;sale_price_multiplier?: number;+ supported_endpoint_types?: string[];}export interface PricingCatalog {@@ -95,6 +98,7 @@ export async function loadModelInfo(o: {if (price) out[entry.model_name] = { ...out[entry.model_name], price };// 没有 Key(拿不到 model-profiles)时,上下文长度用公开目录里的if (Number(entry.context_window) > 0 && !out[entry.model_name]?.context) out[entry.model_name] = { ...out[entry.model_name], context: Number(entry.context_window) };+ if (Array.isArray(entry.supported_endpoint_types)) out[entry.model_name] = { ...out[entry.model_name], endpoints: entry.supported_endpoint_types.map(String) };}return out;}diff --git a/src/agent/bridge.ts b/src/agent/bridge.tsindex cbef5c9..12c4ad9 100644--- a/src/agent/bridge.ts+++ b/src/agent/bridge.ts@@ -28,7 +28,7 @@ export type ProxyEvent =/*** 仅回环的内部桥:DSH 子进程用每次启动随机生成的令牌访问。- * - /v1/*:模型代理,替换 Authorization 为真实网关密钥(子进程环境里没有任何真实密钥)+ * - /v1/*:模型代理(Chat Completions、Responses、Anthropic Messages),把令牌换成真实网关密钥(子进程环境里没有任何真实密钥)* - /dsh/ask|wait|cancel:amb-bridge 插件的审批/提问长轮询*/export class AgentBridge {@@ -61,10 +61,14 @@ export class AgentBridge {return `http://127.0.0.1:${this.port}`;}+ /** OpenAI 兼容客户端用 Authorization: Bearer 带令牌,Anthropic 客户端用 x-api-key */private authorized(req: http.IncomingMessage): boolean {- const got = Buffer.from(String(req.headers.authorization || ""));- const want = Buffer.from(`Bearer ${this.token}`);- return got.length === want.length && crypto.timingSafeEqual(got, want);+ const same = (got: unknown, want: string) => {+ const a = Buffer.from(String(got || ""));+ const b = Buffer.from(want);+ return a.length === b.length && crypto.timingSafeEqual(a, b);+ };+ return same(req.headers.authorization, `Bearer ${this.token}`) || same(req.headers["x-api-key"], this.token);}async start(): Promise<void> {@@ -113,10 +117,14 @@ export class AgentBridge {const target = up.baseUrl.replace(/\/+$/, "").replace(/\/v1$/, "") + url;const headers: Record<string, string> = {};for (const [k, v] of Object.entries(req.headers)) {- if (["host", "authorization", "connection", "content-length", "transfer-encoding", "cookie"].includes(k) || v === undefined) continue;+ if (["host", "authorization", "x-api-key", "connection", "content-length", "transfer-encoding", "cookie"].includes(k) || v === undefined) continue;headers[k] = Array.isArray(v) ? v.join(", ") : v;}- if (up.apiKey) headers.authorization = `Bearer ${up.apiKey}`;+ // 令牌只在本机有效:换成真实密钥;Anthropic 客户端发来的 x-api-key 也换掉(magpie 两种都认)+ if (up.apiKey) {+ headers.authorization = `Bearer ${up.apiKey}`;+ if (req.headers["x-api-key"] !== undefined) headers["x-api-key"] = up.apiKey;+ }const body = req.method === "GET" || req.method === "HEAD" ? undefined : await this.readBody(req);const id = ++this.seq;const p = url.split("?")[0];@@ -182,13 +190,20 @@ export class AgentBridge {}}+/** 模型请求的路径:Chat Completions、Responses、Anthropic Messages(代理按模型选其一) */+export function isModelPath(p: string): boolean {+ return /\/v1\/(chat\/completions|responses|messages)$/.test(p);+}+/** 请求体里的模型名、工具名与第一条用户消息开头(只在有人订阅时解析;解析失败就不给) */function requestInfo(body: Buffer | undefined): { model?: string; tools?: string[]; lastUser?: string } {if (!body || body.length > 8 * 1024 * 1024) return {};try {const j = JSON.parse(body.toString("utf8"));const tools = Array.isArray(j?.tools) ? j.tools.map((t: any) => String(t?.function?.name ?? t?.name ?? "")).filter(Boolean) : undefined;- const users = Array.isArray(j?.messages) ? j.messages.filter((m: any) => m?.role === "user") : [];+ // Chat、Anthropic 是 messages;Responses 是 input(字符串,或条目列表)+ const items = Array.isArray(j?.messages) ? j.messages : Array.isArray(j?.input) ? j.input : typeof j?.input === "string" ? [{ role: "user", content: j.input }] : [];+ const users = items.filter((m: any) => m?.role === "user");const text = (c: unknown): string => (typeof c === "string" ? c : Array.isArray(c) ? c.map((p: any) => (typeof p === "string" ? p : String(p?.text ?? ""))).join("") : "");const lastUser = users.length ? users.map((m: any) => text(m.content).slice(0, 2000)).join("\n") : undefined;return { model: typeof j?.model === "string" ? j.model : undefined, tools, lastUser };diff --git a/src/agent/host.ts b/src/agent/host.tsindex d2386af..16d1e74 100644--- a/src/agent/host.ts+++ b/src/agent/host.ts@@ -162,6 +162,8 @@ export interface AgentHostOptions {media?: boolean;/** 模型的上下文长度(Modelflare 模型资料);不知道时 undefined,用内置兜底表 */contextWindow?: (model: string) => Promise<number | undefined>;+ /** 模型支持的接口与是否推理(Modelflare 模型资料),用来选对话接口;不知道时 undefined,走 Chat Completions */+ apiInfo?: (model: string) => Promise<ModelApiInfo | undefined>;}export type HostState = "stopped" | "starting" | "running" | "backoff" | "error";@@ -223,6 +225,49 @@ export function modelLimits(model: string, known?: number): { window: number; mareturn { window, maxTokens: Math.min(lim[2], Math.floor(window / 4)) };}+/** DSH 跟模型说话用的接口(DSH 补丁里 provider 的 api) */+export type AgentApi = "openai-completions" | "openai-responses" | "anthropic-messages";++/** 选接口用的模型资料:endpoints 是 Modelflare 目录里的 supported_endpoint_types,reasoning 来自 model-profiles */+export interface ModelApiInfo {+ endpoints?: string[];+ reasoning?: boolean;+}++/**+ * 代理用哪个接口:模型支持 Responses 就用 Responses,否则支持 Anthropic Messages 就用它(Claude),+ * 都没有或不知道(离线、目录拿不到)时用 Chat Completions。AMB_AGENT_API=chat 强制 Chat(排查用)。+ */+export function agentApi(endpoints: string[] | undefined, env: NodeJS.ProcessEnv = process.env): AgentApi {+ if (env.AMB_AGENT_API?.trim().toLowerCase() === "chat") return "openai-completions";+ if (endpoints?.includes("openai-response")) return "openai-responses";+ if (endpoints?.includes("anthropic")) return "anthropic-messages";+ return "openai-completions";+}++/**+ * 补丁里随接口变化的几行(provider 一级、模型一级)。+ * - Anthropic SDK 自己拼 /v1/messages,baseURL 是 bridge 根地址;另两个是 …/v1。+ * - Responses:store:false 下推理条目要带 encrypted_content 才能回放,而 DSH 只在指定了推理强度时才要它,+ * 所以推理模型(或不知道是不是)声明 low/medium/high、默认 medium;明确不推理的不发推理参数。+ * 系统提示用 system 角色(网关转给非 OpenAI 模型时不一定认 developer);工具显式 strict:false。+ * - Anthropic、Chat 不声明推理(和以前一样,用模型默认)。+ */+export function apiPatchLines(api: AgentApi, bridgeUrl: string, reasoning?: boolean): { provider: string[]; model: string[] } {+ const provider = [` api: ${api}`, ` baseURL: ${yamlStr(api === "anthropic-messages" ? bridgeUrl : bridgeUrl + "/v1")}`];+ if (api !== "openai-responses") return { provider, model: [] };+ const think = reasoning !== false;+ return {+ provider: [...provider, ...(think ? [" reasoning: medium"] : [])],+ model: [+ ...(think ? [" reasoningEfforts:", " low: low", " medium: medium", " high: high"] : []),+ " compat:",+ " supportsDeveloperRole: false",+ " supportsStrictMode: true",+ ],+ };+}+/** 随消息交给模型的图片(DSH SDK 的内联图片块:base64 + 格式,进会话前存入 DSH 的附件库) */export interface TurnImage {data: string;@@ -291,6 +336,8 @@ export class AgentHost {lastError = "";restarts = 0;model = "";+ /** 当前子进程跟模型说话用的接口 */+ api: AgentApi | "" = "";/** 当前子进程补丁里写的上下文窗口与最大输出 */private limits: { window: number; maxTokens: number } | undefined;private harness: SdkHarness | undefined;@@ -364,8 +411,9 @@ export class AgentHost {});}- private writePatch(model: string, apps: CuaApp[] = [], pwshPath?: string, lim = modelLimits(model)): string {+ private writePatch(model: string, apps: CuaApp[] = [], pwshPath?: string, lim = modelLimits(model), api: AgentApi = "openai-completions", reasoning?: boolean): string {this.limits = lim;+ const wire = apiPatchLines(api, this.o.bridge.url, reasoning);const plugin = path.join(this.o.runtimeDir, "plugins", "amb-bridge.mjs");const memory = path.join(this.o.runtimeDir, "plugins", "amb-memory.mjs");this.writeMediaSkill();@@ -376,8 +424,8 @@ export class AgentHost {" providers:"," magpie:"," displayName: anymodel",- " api: openai-completions",- ` baseURL: ${yamlStr(this.o.bridge.url + "/v1")}`,+ // 接口按模型选(agentApi):Responses / Anthropic Messages / Chat Completions+ ...wire.provider," apiKeyEnv: AMB_MAGPIE_KEY"," models:",` - id: ${yamlStr(model)}`,@@ -386,6 +434,7 @@ export class AgentHost {` maxTokens: ${lim.maxTokens}`,// 默认只收文字;能看图的模型声明图片输入,用户发来的图片才会作为图片交给它...(acceptsImages(model) ? [" input: [text, image]"] : []),+ ...wire.model,// Windows:命令用检测到的 PowerShell 7(DSH 自己只看 Program Files\PowerShell\7 和 PATH,找不到就退回 5.1)...pwshPatchLines(pwshPath),"- insert:",@@ -683,11 +732,13 @@ exec "$REAL" "$@"const cu = this.computerUseActive();const apps = cu && cu.backend === "cua" ? await discoverAppsCached() : [];const pwshPath = await pwshProbe.dshPath();- const known = await withTimeoutOr(this.o.contextWindow?.(model) ?? Promise.resolve(undefined), 3000, undefined).catch(() => undefined);- const patch = this.writePatch(model, apps, pwshPath, modelLimits(model, known));+ const ask = <T>(p: Promise<T | undefined> | undefined) => withTimeoutOr(p ?? Promise.resolve(undefined), 3000, undefined).catch(() => undefined);+ const [known, info] = await Promise.all([ask(this.o.contextWindow?.(model)), ask(this.o.apiInfo?.(model))]);+ const api = agentApi(info?.endpoints);+ const patch = this.writePatch(model, apps, pwshPath, modelLimits(model, known), api, info?.reasoning);this.sig = this.settingsSig();const timeoutMs = this.initTimeoutMs();- this.stage("patch written", t0, { computerUse: cu ? cu.backend : "off", apps: apps.length, initTimeoutMs: timeoutMs, ...(process.platform === "win32" ? { pwsh: pwshPath ?? "dsh default (Windows PowerShell 5.1)" } : {}) });+ this.stage("patch written", t0, { api, computerUse: cu ? cu.backend : "off", apps: apps.length, initTimeoutMs: timeoutMs, ...(process.platform === "win32" ? { pwsh: pwshPath ?? "dsh default (Windows PowerShell 5.1)" } : {}) });h = new sdk.DeepSeekHarness({profile: "sdk",patches: [patch],@@ -735,12 +786,13 @@ exec "$REAL" "$@"});this.harness = h;this.model = model;+ this.api = api;this.state = "running";this.initStage = "";this.lastError = "";this.initFailures = 0;this.startedAt = Date.now();- log.info("dsh started", { bot: this.o.botId, pid: child?.pid, model, ms: Date.now() - t0, initMs });+ log.info("dsh started", { bot: this.o.botId, pid: child?.pid, model, api, ms: Date.now() - t0, initMs });return h;} catch (e) {clearInterval(watchdog);@@ -898,7 +950,7 @@ exec "$REAL" "$@"}status(): Record<string, unknown> {- return { state: this.state, pid: this.pid, model: this.model, restarts: this.restarts, lastError: this.lastError, workspace: this.workspace, activeTurns: this.active };+ return { state: this.state, pid: this.pid, model: this.model, api: this.api || undefined, restarts: this.restarts, lastError: this.lastError, workspace: this.workspace, activeTurns: this.active };}/** 故障排查用的完整状态(含 stderr 尾部与最近一次退出) */diff --git a/src/agent/manager.ts b/src/agent/manager.tsindex 1bf3ad3..879c8fc 100644--- a/src/agent/manager.ts+++ b/src/agent/manager.ts@@ -3,7 +3,7 @@ import type { Store } from "../store/db.js";import { logger } from "../log.js";import { AgentBridge } from "./bridge.js";import path from "node:path";-import { AgentHost, TurnStopped, callDetail, type TurnInput, type TurnObserver, type TurnUsage } from "./host.js";+import { AgentHost, TurnStopped, callDetail, type ModelApiInfo, type TurnInput, type TurnObserver, type TurnUsage } from "./host.js";import { ActivityLog, type ActivityStep } from "./activity.js";import type { MediaModel, MediaPlan, MediaService } from "./media.js";import { APPROVAL_TTL_MS, ApprovalCenter, type QuestionItem } from "./approvals.js";@@ -44,6 +44,8 @@ export interface AgentManagerOptions {onNotice?: (text: string) => void;/** 模型的上下文长度(Modelflare 模型资料),写进 DSH 补丁 */contextWindow?: (model: string) => Promise<number | undefined>;+ /** 模型支持的接口与是否推理(Modelflare 模型资料),DSH 按它选对话接口 */+ apiInfo?: (model: string) => Promise<ModelApiInfo | undefined>;}/** 一个进行中的回合(/status、/stop、排队提示、信息面板和故障排查用) */@@ -135,6 +137,7 @@ export class AgentManager {onNotice: this.o.onNotice,media: !!this.o.media,contextWindow: this.o.contextWindow,+ apiInfo: this.o.apiInfo,});this.hosts.set(botId, h);}diff --git a/src/app.ts b/src/app.tsindex f37410e..429a1e4 100644--- a/src/app.ts+++ b/src/app.ts@@ -117,6 +117,7 @@ export class App {private accountTimer: NodeJS.Timeout | undefined;private picksTimer: NodeJS.Timeout | undefined;private modelInfoCache: { at: number; data: Record<string, ModelInfo> } | undefined;+ private modelInfoLoading: Promise<Record<string, ModelInfo>> | undefined;constructor(readonly config: AppConfig, readonly opts: AppOptions = {}) {// 日志文件:<dataDir>/logs/app.log(打包后的桌面版看不到 stdout,故障排查全靠它)@@ -160,10 +161,12 @@ export class App {idleTimeoutMs: config.agent.idleTimeoutMs,approvalTtlMs: config.agent.approvalTtlMs,// 上下文长度来自 Modelflare 的模型资料(和模型选择器里显示的一致);内置兜底表跟不上新模型- contextWindow: async (model) => {- const info = await this.modelInfo();- const bare = bareModel(model);- return (info[bare] ?? info[bare.split("/").pop() ?? bare])?.context;+ contextWindow: async (model) => (await this.infoOf(model))?.context,+ // 代理的对话接口按 Modelflare 目录选(Responses / Anthropic Messages / Chat);外部端点只当它会 Chat+ apiInfo: async (model) => {+ if (!this.magpie) return undefined;+ const info = await this.infoOf(model);+ return info && { endpoints: info.endpoints, reasoning: info.reasoning };},// 后台小任务(管家判定/交接/记忆整理)直接走 magpie,不经代理子进程complete: (botId, model, system, user) =>@@ -334,15 +337,26 @@ export class App {/** 模型选择器里的能力与参考价(缓存一小时) */async modelInfo(force = false): Promise<Record<string, ModelInfo>> {if (!force && this.modelInfoCache && Date.now() - this.modelInfoCache.at < 3600_000) return this.modelInfoCache.data;- const key = this.magpie?.providerKey(this.magpie.modelflareProviderId ?? MODELFLARE_PROVIDER) ?? "";- const data = await loadModelInfo({- baseUrl: modelflareBaseUrl(this.config.modelflare.line),- apiKey: key,- headers: this.requestHeaders(),- pricingUrl: `${modelflareIssuer()}/api/pricing`,- });- this.modelInfoCache = { at: Date.now(), data };- return data;+ // 同时来的几次(代理启动时上下文长度和接口一起问)共用一次加载+ this.modelInfoLoading ??= (async () => {+ const key = this.magpie?.providerKey(this.magpie.modelflareProviderId ?? MODELFLARE_PROVIDER) ?? "";+ const data = await loadModelInfo({+ baseUrl: modelflareBaseUrl(this.config.modelflare.line),+ apiKey: key,+ headers: this.requestHeaders(),+ pricingUrl: `${modelflareIssuer()}/api/pricing`,+ });+ this.modelInfoCache = { at: Date.now(), data };+ return data;+ })().finally(() => (this.modelInfoLoading = undefined));+ return this.modelInfoLoading;+ }++ /** 某个模型的资料:去掉内置网关的 provider 前缀,再试最后一段(openai/x → x) */+ private async infoOf(model: string): Promise<ModelInfo | undefined> {+ const info = await this.modelInfo();+ const bare = bareModel(model);+ return info[bare] ?? info[bare.split("/").pop() ?? bare];}notice(level: "info" | "warn", text: string): void {diff --git a/src/diagnostics.ts b/src/diagnostics.tsindex d64821c..6b71273 100644--- a/src/diagnostics.ts+++ b/src/diagnostics.ts@@ -6,7 +6,7 @@ import crypto from "node:crypto";import os from "node:os";import { appVersion, type App } from "./app.js";import type { HostEvent } from "./agent/host.js";-import type { ProxyEvent } from "./agent/bridge.js";+import { isModelPath, type ProxyEvent } from "./agent/bridge.js";import { logFilePath, logger, readLogTail, redactText, redactValue } from "./log.js";import { bareModel } from "./modelflare.js";import { pwshProbe, pwshReportLine } from "./agent/pwsh.js";@@ -258,7 +258,7 @@ export async function testAgent(app: App, opts: { botId?: string; model?: string}});const offProxy = app.agent.bridge.onProxy((e: ProxyEvent) => {- if (!e.path.endsWith("/chat/completions")) return;+ if (!isModelPath(e.path)) return;if (e.phase === "request") {if (!e.lastUser?.includes(marker)) return;ours.add(e.id);diff --git a/test/account.test.ts b/test/account.test.tsindex 3469fa7..757b705 100644--- a/test/account.test.ts+++ b/test/account.test.ts@@ -267,6 +267,23 @@ describe("模型资料", () => {const noKey = await loadModelInfo({ ...base, apiKey: "" });expect(noKey["gpt-6.1-terra"].context).toBe(999);});++ it("支持的接口:公开目录里的 supported_endpoint_types;推理能力来自 model-profiles", async () => {+ const fetchImpl = (async (url: string) => {+ if (url.endsWith("/model-profiles")) return Response.json({ data: { "gpt-6.1-terra": { reasoning: true }, "claude-sonnet-5": { reasoning: true } } });+ return Response.json({+ data: [+ { model_name: "gpt-6.1-terra", quota_type: 0, supported_endpoint_types: ["openai-response", "openai-response-compact", "openai"] },+ { model_name: "claude-sonnet-5", quota_type: 0, supported_endpoint_types: ["anthropic", "openai"] },+ { model_name: "old-model", quota_type: 0 },+ ],+ });+ }) as any;+ const info = await loadModelInfo({ baseUrl: "http://mf/v1", apiKey: "k", headers: {}, pricingUrl: "http://mf/api/pricing", fetchImpl });+ expect(info["gpt-6.1-terra"]).toMatchObject({ reasoning: true, endpoints: ["openai-response", "openai-response-compact", "openai"] });+ expect(info["claude-sonnet-5"].endpoints).toEqual(["anthropic", "openai"]);+ expect(info["old-model"]?.endpoints).toBeUndefined();+ });});describe("生图 / 生视频", () => {diff --git a/test/agent.integration.test.ts b/test/agent.integration.test.tsindex bdef5cf..90ff9cd 100644--- a/test/agent.integration.test.ts+++ b/test/agent.integration.test.ts@@ -11,8 +11,8 @@ import { findAppRoot } from "../src/magpie/supervisor.js";import { listen } from "./helpers.js";/**- * 真实 DSH 子进程 + 脚本化的 OpenAI 兼容模型(会发出工具调用)。- * 验证:插件加载、bridge 注入密钥、bash 沙箱、审批流程(API 批准/拒绝)、读围栏、回环 API 防护。+ * 真实 DSH 子进程 + 脚本化的模型(会发出工具调用),Chat Completions、Responses、Anthropic Messages 三种接口都能答。+ * 验证:插件加载、bridge 注入密钥、bash 沙箱、审批流程(API 批准/拒绝)、读围栏、回环 API 防护、按模型选接口。*/const runtime = locateDshRuntime(findAppRoot());/** workflow 脚本先剥离 TS 类型;Electron 自带的 Node 支持,部分发行版的系统 Node 不带 amaro。 */@@ -38,16 +38,131 @@ function textOf(c: any): string {return "";}+const rnd = () => Math.random().toString(36).slice(2, 10);+const imageUrl = (url: string) => ({ type: "image_url", image_url: { url } });++/** Responses 的 input → Chat 形状的消息(脚本只看 user / tool) */+function fromResponses(input: any[]): any[] {+ const out: any[] = [];+ for (const it of input ?? []) {+ if (it.type === "function_call_output") out.push({ role: "tool", content: typeof it.output === "string" ? it.output : textOf(it.output) });+ else if (it.role && (it.type === undefined || it.type === "message"))+ out.push({ role: it.role, content: typeof it.content === "string" ? it.content : it.content.map((p: any) => (p.type === "input_image" ? imageUrl(p.image_url) : { type: "text", text: p.text ?? "" })) });+ }+ return out;+}++/** Anthropic 的 messages → Chat 形状:tool_result 拆成 tool 消息 */+function fromAnthropic(messages: any[]): any[] {+ const out: any[] = [];+ for (const m of messages ?? []) {+ const blocks: any[] = typeof m.content === "string" ? [{ type: "text", text: m.content }] : m.content;+ for (const r of blocks.filter((b) => b.type === "tool_result")) out.push({ role: "tool", content: textOf(r.content) });+ const rest = blocks.filter((b) => b.type === "text" || b.type === "image");+ if (rest.length) out.push({ role: m.role, content: rest.map((b) => (b.type === "image" ? imageUrl(`data:${b.source.media_type};base64,${b.source.data}`) : { type: "text", text: b.text })) });+ }+ return out;+}++/** 像 OpenAI 那样校验 Responses 请求(store:false 时推理条目必须带 encrypted_content,工具结果要对得上调用) */+function responsesError(b: any): string | undefined {+ if (b.store !== false) return "store must be false";+ const calls = new Set<string>();+ for (const it of b.input ?? []) {+ if (it.type === "reasoning" && !it.encrypted_content) return `Item with id '${it.id}' not found. Items are not persisted when \`store\` is set to false.`;+ if (it.type === "function_call") {+ if (it.id !== undefined && !String(it.id).startsWith("fc")) return `Invalid 'input.id': '${it.id}'. Expected an ID that begins with 'fc'.`;+ calls.add(it.call_id);+ }+ if (it.type === "function_call_output" && !calls.has(it.call_id)) return `No tool call found for function call output with call_id ${it.call_id}.`;+ }+ return undefined;+}++/** 像 Anthropic 那样校验:max_tokens 必填,tool_result 紧跟在对应的 tool_use 之后 */+function messagesError(b: any): string | undefined {+ if (!(b.max_tokens > 0)) return "max_tokens: Field required";+ let pending = new Set<string>();+ for (const m of b.messages ?? []) {+ const blocks: any[] = typeof m.content === "string" ? [] : m.content;+ for (const r of blocks.filter((x) => x.type === "tool_result")) if (m.role !== "user" || !pending.has(r.tool_use_id)) return `unexpected tool_use_id found in tool_result blocks: ${r.tool_use_id}`;+ for (const t of blocks.filter((x) => x.type === "tool_use")) if (!/^[a-zA-Z0-9_-]+$/.test(t.id)) return `tool_use.id: String should match pattern '^[a-zA-Z0-9_-]+$'`;+ pending = new Set(m.role === "assistant" ? blocks.filter((x) => x.type === "tool_use").map((x) => x.id) : []);+ }+ return undefined;+}++type Step = { tool?: { name: string; args: any }; text?: string; hang?: boolean };++/** OpenAI Responses 的流式事件;请求要了 encrypted_content 时先给一条加密推理 */+function responsesStream(res: http.ServerResponse, b: any, step: Step): void {+ res.writeHead(200, { "content-type": "text/event-stream" });+ const ev = (type: string, o: object) => res.write(`event: ${type}\ndata: ${JSON.stringify({ type, ...o })}\n\n`);+ const id = "resp_" + rnd();+ const output: any[] = [];+ const item = (done: any, added: any, between: (i: number) => void = () => {}) => {+ const i = output.length;+ ev("response.output_item.added", { output_index: i, item: added });+ between(i);+ ev("response.output_item.done", { output_index: i, item: done });+ output.push(done);+ };+ ev("response.created", { response: { id, object: "response", status: "in_progress", model: b.model, output: [] } });+ if (b.include?.includes("reasoning.encrypted_content")) {+ const rs = { type: "reasoning", id: "rs_" + rnd(), summary: [{ type: "summary_text", text: "想一想" }], encrypted_content: "enc-" + rnd() };+ item(rs, { type: "reasoning", id: rs.id, summary: [] }, (i) => ev("response.reasoning_summary_text.delta", { output_index: i, item_id: rs.id, summary_index: 0, delta: "想一想" }));+ }+ if (step.tool) {+ const args = JSON.stringify(step.tool.args);+ const fc = { type: "function_call", id: "fc_" + rnd(), call_id: "call_" + rnd(), name: step.tool.name, arguments: args, status: "completed" };+ item(fc, { ...fc, arguments: "", status: "in_progress" }, (i) => {+ ev("response.function_call_arguments.delta", { output_index: i, item_id: fc.id, delta: args });+ ev("response.function_call_arguments.done", { output_index: i, item_id: fc.id, arguments: args });+ });+ } else {+ const text = step.text ?? "";+ const msg = { type: "message", id: "msg_" + rnd(), role: "assistant", status: "completed", content: [{ type: "output_text", text, annotations: [] }] };+ item(msg, { ...msg, status: "in_progress", content: [] }, (i) => ev("response.output_text.delta", { output_index: i, item_id: msg.id, content_index: 0, delta: text }));+ }+ const usage = { input_tokens: 10, output_tokens: 5, total_tokens: 15, input_tokens_details: { cached_tokens: 0 }, output_tokens_details: { reasoning_tokens: 0 } };+ ev("response.completed", { response: { id, object: "response", status: "completed", model: b.model, output, usage } });+ res.end();+}++/** Anthropic Messages 的流式事件 */+function messagesStream(res: http.ServerResponse, b: any, step: Step): void {+ res.writeHead(200, { "content-type": "text/event-stream" });+ const ev = (type: string, o: object) => res.write(`event: ${type}\ndata: ${JSON.stringify({ type, ...o })}\n\n`);+ ev("message_start", { message: { id: "msg_" + rnd(), type: "message", role: "assistant", model: b.model, content: [], stop_reason: null, stop_sequence: null, usage: { input_tokens: 10, output_tokens: 0 } } });+ if (step.tool) {+ ev("content_block_start", { index: 0, content_block: { type: "tool_use", id: "toolu_" + rnd(), name: step.tool.name, input: {} } });+ ev("content_block_delta", { index: 0, delta: { type: "input_json_delta", partial_json: JSON.stringify(step.tool.args) } });+ } else {+ ev("content_block_start", { index: 0, content_block: { type: "text", text: "" } });+ ev("content_block_delta", { index: 0, delta: { type: "text_delta", text: step.text ?? "" } });+ }+ ev("content_block_stop", { index: 0 });+ ev("message_delta", { delta: { stop_reason: step.tool ? "tool_use" : "end_turn", stop_sequence: null }, usage: { output_tokens: 5 } });+ ev("message_stop", {});+ res.end();+}+describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent host (DSH)", () => {let app: App;let dataDir: string;let llm: Awaited<ReturnType<typeof listen>>;const seenAuth = new Set<string>();+ /** 上游收到的每个模型请求:走的接口、模型、请求体与请求头 */+ const calls: Array<{ api: "chat" | "responses" | "messages"; model: string; body: any; headers: http.IncomingHttpHeaders; error?: string }> = [];+ /** 测试注入的 Modelflare 目录:模型 → 支持的接口(外部端点模式下 App 不查目录) */+ const catalog: Record<string, string[]> = {};+ /** FLAKY:每条这样的用户消息第一次请求先失败一次(Responses 503、Messages 429),看代理会不会重试 */+ const flaked = new Set<string>();const hung: http.ServerResponse[] = [];let base = "";/** 根据最后一条用户消息与之后的工具结果数量决定下一步 */- function plan(messages: any[]): { tool?: { name: string; args: any }; text?: string; hang?: boolean } {+ function plan(messages: any[]): Step {let lastUser = -1;for (let i = messages.length - 1; i >= 0; i--) {if (messages[i].role === "user" && /QUICKSORT|ESCALATE|SECRET|READFILE|LOOPBACK|PRESENT|ASKQ|NATIVES|LOOKIMG|HANG|\[用户消息\]/.test(textOf(messages[i].content))) { lastUser = i; break; }@@ -89,14 +204,25 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent hobeforeAll(async () => {llm = await listen(async (req, res) => {seenAuth.add(String(req.headers.authorization));- if (req.url?.endsWith("/models")) {+ const url = req.url?.split("?")[0] ?? "";+ if (url.endsWith("/models")) {res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ object: "list", data: [{ id: "mock-agent", object: "model" }] }));return;}const b = await body(req);- const step = b.tools ? plan(b.messages) : { text: "标题" };+ const api = url.endsWith("/v1/responses") ? "responses" : url.endsWith("/v1/messages") ? "messages" : "chat";+ const msgs = api === "responses" ? fromResponses(b.input) : api === "messages" ? fromAnthropic(b.messages) : b.messages;+ const error = api === "responses" ? responsesError(b) : api === "messages" ? messagesError(b) : undefined;+ const said = (msgs ?? []).filter((m: any) => m.role === "user").map((m: any) => textOf(m.content)).findLast((t: string) => /FLAKY/.test(t));+ const flaky = api !== "chat" && b.tools && said && !flaked.has(said) ? (flaked.add(said), api === "responses" ? 503 : 429) : 0;+ calls.push({ api, model: b.model, body: b, headers: req.headers, error: error ?? (flaky ? `${flaky}` : undefined) });+ if (error) return void res.writeHead(400, { "content-type": "application/json" }).end(JSON.stringify({ type: "error", error: { type: "invalid_request_error", message: error } }));+ if (flaky) return void res.writeHead(flaky, { "content-type": "application/json" }).end(JSON.stringify({ type: "error", error: { type: flaky === 429 ? "rate_limit_error" : "api_error", message: "slow down" } }));+ const step = b.tools ? plan(msgs) : { text: "标题" };if ("hang" in step && step.hang) return void hung.push(res);- if (process.env.AMB_TEST_LLM_LOG) fs.appendFileSync(process.env.AMB_TEST_LLM_LOG, JSON.stringify({ url: req.url, stream: b.stream, tools: (b.tools ?? []).map((t: any) => t.function?.name), msgs: b.messages.map((m: any) => [m.role, textOf(m.content).slice(0, 200), m.tool_calls?.map((t: any) => t.function?.name)]), step }) + "\n");+ if (process.env.AMB_TEST_LLM_LOG) fs.appendFileSync(process.env.AMB_TEST_LLM_LOG, JSON.stringify({ url: req.url, stream: b.stream, tools: (b.tools ?? []).map((t: any) => t.function?.name ?? t.name), msgs: msgs.map((m: any) => [m.role, textOf(m.content).slice(0, 200), m.tool_calls?.map((t: any) => t.function?.name)]), step }) + "\n");+ if (api === "responses") return responsesStream(res, b, step);+ if (api === "messages") return messagesStream(res, b, step);if (!b.stream) {res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ choices: [{ message: { role: "assistant", content: step.text ?? "" }, finish_reason: "stop" }] }));return;@@ -119,6 +245,8 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent holoadConfig({ dataDir, env: { AMB_MAGPIE_BASE_URL: llm.url + "/v1", AMB_MAGPIE_API_KEY: "gw-key", AMB_UI_PORT: "0", AMB_WEIXIN_API_BASE: "http://127.0.0.1:9", AMB_DEFAULT_MODEL: "mock-agent" } }),{ printQrToTerminal: false },);+ // 外部端点模式下 App 不查 Modelflare 目录(只当它会 Chat);这里注入目录,让模型名决定接口+ (app.agent as any).o.apiInfo = async (model: string) => (catalog[model] ? { endpoints: catalog[model] } : undefined);await app.start();base = app.web!.url.replace(/\/+$/, "");}, 60_000);@@ -327,4 +455,109 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent hoexpect(a.steps.map((s: any) => [s.kind, s.status])).toEqual([["model", "ok"], ["tool", "ok"], ["model", "ok"]]);expect(a.steps[1]).toMatchObject({ title: "bash", output: expect.stringContaining("[1, 2, 3, 5, 7, 9]") });}, 120_000);++ // —— 按模型选接口:Modelflare 目录里有 Responses 就走 Responses,只有 Anthropic 的(Claude)走 Messages ——+ const png = Buffer.from("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==", "base64");+ /** 用一个微信会话(可单独设模型)发一条消息:回复与这一轮的步骤 */+ const turnOn = async (user: string, model: string, text: string, images: Buffer[] = []) => {+ const conv = app.store.ensureConversation("default", "weixin", user, user, "dm");+ app.store.setConversationModel(conv.id, model);+ const ops: any[] = [];+ const parts = [{ type: "text" as const, text }, ...images.map((data) => ({ type: "image" as const, data, mime: "image/png" }))];+ await app.runtime.handle({ id: `${user}-${rnd()}`, channel: "weixin", botId: "default", conversation: { kind: "dm", id: user }, sender: { id: user }, parts, replyHandle: {}, receivedAt: Date.now() }, { apply: async (op) => void ops.push(op) });+ const msgs = (await api(`/conversations/${encodeURIComponent(conv.id)}/messages`)).messages;+ const activity = await api(`/conversations/${encodeURIComponent(conv.id)}/messages/${msgs.at(-1).id}/activity`);+ return { reply: ops.find((o) => o.type === "stream.finish")?.text as string, steps: (activity.steps ?? []) as any[] };+ };+ const callsFor = (model: string, from: number) => calls.slice(from).filter((c) => c.model === model);++ it("a model offering the Responses API is driven over /v1/responses: tool calls, encrypted reasoning replay, usage", async () => {+ catalog["gpt-6-mock"] = ["openai", "openai-response", "openai-response-compact"];+ const from = calls.length;+ const r = await turnOn("resp-user", "gpt-6-mock", "QUICKSORT over responses");+ expect(r.reply).toContain("[1, 2, 3, 5, 7, 9]");+ expect(app.agent.host("default").api).toBe("openai-responses");+ const mine = callsFor("gpt-6-mock", from);+ expect(mine.map((c) => `${c.api}:${c.error ?? "ok"}`)).toEqual(mine.map(() => "responses:ok"));+ const [first, second] = mine.filter((c) => c.body.tools);+ // 推理模型:要加密推理(store:false 下回放靠它),默认强度 medium+ expect(first.body).toMatchObject({ stream: true, store: false, reasoning: { effort: "medium" }, include: ["reasoning.encrypted_content"] });+ // 系统提示是 system 角色;工具显式 strict:false+ expect(first.body.input[0].role).toBe("system");+ expect(first.body.input.some((i: any) => i.role === "developer")).toBe(false);+ expect(first.body.tools.every((t: any) => t.type === "function" && t.strict === false)).toBe(true);+ // 第二次请求带回推理(含 encrypted_content)、工具调用与结果+ expect(second.body.input.find((i: any) => i.type === "reasoning")?.encrypted_content).toMatch(/^enc-/);+ const fc = second.body.input.find((i: any) => i.type === "function_call");+ expect(fc).toMatchObject({ name: "bash", id: expect.stringMatching(/^fc_/) });+ expect(second.body.input.find((i: any) => i.type === "function_call_output")).toMatchObject({ call_id: fc.call_id, output: expect.stringContaining("[1, 2, 3, 5, 7, 9]") });+ expect(mine.every((c) => c.headers.authorization === "Bearer gw-key")).toBe(true);+ // 步骤与用量照常记录+ expect(r.steps.map((s) => [s.kind, s.status])).toEqual([["model", "ok"], ["tool", "ok"], ["model", "ok"]]);+ expect(r.steps[0].tokens).toEqual({ input: 10, output: 5 });+ }, 180_000);++ it("Claude (Anthropic but no Responses) is driven over /v1/messages; the bridge swaps x-api-key for the real key", async () => {+ catalog["claude-mock"] = ["anthropic", "openai"];+ const from = calls.length;+ const r = await turnOn("claude-user", "claude-mock", "QUICKSORT over messages");+ expect(r.reply).toContain("[1, 2, 3, 5, 7, 9]");+ expect(app.agent.host("default").api).toBe("anthropic-messages");+ const mine = callsFor("claude-mock", from);+ expect(mine.map((c) => `${c.api}:${c.error ?? "ok"}`)).toEqual(mine.map(() => "messages:ok"));+ const [first, second] = mine.filter((c) => c.body.tools);+ expect(first.body).toMatchObject({ stream: true, max_tokens: 32_000 });+ expect(first.body.thinking).toBeUndefined();+ expect(second.body.messages.at(-1).content.some((b: any) => b.type === "tool_result")).toBe(true);+ for (const c of mine) {+ expect(c.headers["anthropic-version"]).toBeTruthy();+ expect(c.headers.authorization).toBe("Bearer gw-key");+ expect(c.headers["x-api-key"]).toBe("gw-key");+ // 子进程只有 bridge 令牌,它不会到上游+ expect(JSON.stringify(c.headers)).not.toContain(app.agent.bridge.token);+ }+ expect(r.steps.map((s) => [s.kind, s.status])).toEqual([["model", "ok"], ["tool", "ok"], ["model", "ok"]]);+ expect(r.steps[0].tokens).toEqual({ input: 10, output: 5 });+ }, 180_000);++ it("transient upstream errors are retried over Responses and Messages as over Chat", async () => {+ for (const [model, api, status] of [["gpt-6-mock", "responses", "503"], ["claude-mock", "messages", "429"]]) {+ const from = calls.length;+ const r = await turnOn(`flaky-${api}`, model, `FLAKY over ${api}`);+ expect(r.reply).toBe("好的");+ expect(callsFor(model, from).filter((c) => c.body.tools).map((c) => `${c.api}:${c.error ?? "ok"}`)).toEqual([`${api}:${status}`, `${api}:ok`]);+ expect(r.steps.some((s) => s.kind === "retry")).toBe(true);+ }+ }, 180_000);++ it("vision models get the image itself over Responses and Messages too", async () => {+ if (!app.agent.host("default").imageInput) return; // Linux 上的 Electron:只给路径(见上一个图片用例)+ expect((await turnOn("resp-img", "gpt-6-mock", "LOOKIMG", [png])).reply).toMatch(/^SEEN<<data:image\/(png|jpeg|webp);base64\|path>>$/);+ expect(calls.at(-1)?.api).toBe("responses");+ expect((await turnOn("claude-img", "claude-mock", "LOOKIMG", [png])).reply).toMatch(/^SEEN<<data:image\/(png|jpeg|webp);base64\|path>>$/);+ expect(calls.at(-1)?.api).toBe("messages");+ }, 180_000);++ it("AMB_AGENT_API=chat forces Chat Completions; a conversation started over Chat continues over Responses", async () => {+ catalog["gpt-7-switch"] = ["openai", "openai-response"];+ const host = app.agent.host("default");+ process.env.AMB_AGENT_API = "chat";+ try {+ const r = await turnOn("switch-user", "gpt-7-switch", "QUICKSORT first");+ expect(r.reply).toContain("[1, 2, 3, 5, 7, 9]");+ expect(host.api).toBe("openai-completions");+ expect(calls.at(-1)).toMatchObject({ api: "chat", model: "gpt-7-switch" });+ } finally {+ delete process.env.AMB_AGENT_API;+ }+ // 接口只在拉起子进程时选:重启后同一会话改走 Responses,上一轮 Chat 里的工具调用照样回放+ await host.restart();+ const from = calls.length;+ const r = await turnOn("switch-user", "gpt-7-switch", "QUICKSORT second");+ expect(r.reply).toContain("[1, 2, 3, 5, 7, 9]");+ expect(host.api).toBe("openai-responses");+ const mine = callsFor("gpt-7-switch", from).filter((c) => c.body.tools);+ expect(mine.map((c) => `${c.api}:${c.error ?? "ok"}`)).toEqual(mine.map(() => "responses:ok"));+ expect(mine[0].body.input.filter((i: any) => i.type === "function_call_output")).toHaveLength(1);+ }, 180_000);});diff --git a/test/agent.unit.test.ts b/test/agent.unit.test.tsindex 04a0cd7..2edadbf 100644--- a/test/agent.unit.test.ts+++ b/test/agent.unit.test.ts@@ -6,7 +6,9 @@ import { Store } from "../src/store/db.js";import { ApprovalCenter, parseAnswer, promptText } from "../src/agent/approvals.js";import { WeixinReply } from "../src/channels/weixin/channel.js";import { WeixinQuota } from "../src/channels/weixin/quota.js";-import { AgentHost, modelLimits } from "../src/agent/host.js";+import { AgentHost, agentApi, modelLimits, type AgentApi } from "../src/agent/host.js";+import { AgentBridge, type ProxyEvent } from "../src/agent/bridge.js";+import { listen } from "./helpers.js";let dir: string;let store: Store;@@ -225,3 +227,124 @@ describe("context window written for DSH", () => {fs.rmSync(dataDir, { recursive: true, force: true });});});++describe("agent API per model", () => {+ // Modelflare 公开目录里的 supported_endpoint_types(2026-10)+ const gpt = ["openai", "openai-response", "openai-response-compact"];+ const deepseek = ["openai", "openai-response", "anthropic", "gemini"];+ const claude = ["anthropic", "openai"];+ const gemini = ["gemini", "openai"];++ it("Responses when offered, else Anthropic Messages, else Chat Completions", () => {+ expect(agentApi(gpt, {})).toBe("openai-responses");+ expect(agentApi(["openai-response", "openai-response-compact", "openai"], {})).toBe("openai-responses");+ expect(agentApi(deepseek, {})).toBe("openai-responses");+ expect(agentApi(claude, {})).toBe("anthropic-messages");+ expect(agentApi(gemini, {})).toBe("openai-completions");+ expect(agentApi(["openai"], {})).toBe("openai-completions");+ });++ it("unknown model or no catalog (offline, China line) stays on Chat Completions", () => {+ expect(agentApi(undefined, {})).toBe("openai-completions");+ expect(agentApi([], {})).toBe("openai-completions");+ });++ it("AMB_AGENT_API=chat forces Chat Completions", () => {+ expect(agentApi(gpt, { AMB_AGENT_API: "chat" })).toBe("openai-completions");+ expect(agentApi(claude, { AMB_AGENT_API: " Chat " })).toBe("openai-completions");+ expect(agentApi(claude, { AMB_AGENT_API: "" })).toBe("anthropic-messages");+ });++ const patchFor = (model: string, api: AgentApi, reasoning?: boolean) => {+ const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "amb-patch-"));+ const host = new AgentHost({ botId: "b", dataDir, runtimeDir: "/rt", bridge: { url: "http://127.0.0.1:1" } as any, store: new Store(":memory:") });+ fs.mkdirSync(host.home, { recursive: true });+ const text = fs.readFileSync((host as any).writePatch(model, [], undefined, modelLimits(model, 1_000_000), api, reasoning), "utf8");+ fs.rmSync(dataDir, { recursive: true, force: true });+ return text;+ };++ it("the patch names the API; Anthropic's base URL is the bridge root (the SDK adds /v1/messages)", () => {+ const chat = patchFor("mfb-b/gemini-3.6-flash", "openai-completions");+ expect(chat).toContain(' api: openai-completions\n baseURL: "http://127.0.0.1:1/v1"\n');+ expect(chat).not.toMatch(/reasoning|compat/);+ const claude = patchFor("mfb-b/claude-sonnet-5", "anthropic-messages", true);+ expect(claude).toContain(' api: anthropic-messages\n baseURL: "http://127.0.0.1:1"\n');+ expect(claude).toContain(" input: [text, image]\n");+ // Claude 不开思考(和以前一样用模型默认)+ expect(claude).not.toMatch(/reasoning|compat/);+ });++ it("Responses: reasoning models ask for medium effort (encrypted reasoning for replay); system role; explicit strict:false", () => {+ const resp = patchFor("mfb-b/gpt-6.1-terra", "openai-responses", true);+ expect(resp).toContain(' api: openai-responses\n baseURL: "http://127.0.0.1:1/v1"\n reasoning: medium\n apiKeyEnv: AMB_MAGPIE_KEY\n');+ expect(resp).toContain(+ " maxTokens: 32000\n input: [text, image]\n reasoningEfforts:\n low: low\n medium: medium\n high: high\n compat:\n supportsDeveloperRole: false\n supportsStrictMode: true\n",+ );+ // 不知道是不是推理模型:按推理模型处理(store:false 下推理条目没有 encrypted_content 就回放不了)+ expect(patchFor("mfb-b/gpt-6.1-terra", "openai-responses")).toContain(" reasoning: medium\n");+ // 明确不推理:不发推理参数+ const plain = patchFor("mfb-b/gpt-6.1-terra", "openai-responses", false);+ expect(plain).not.toMatch(/reasoning/);+ expect(plain).toContain(" compat:\n supportsDeveloperRole: false\n supportsStrictMode: true\n");+ });+});++describe("agent bridge model proxy", () => {+ let up: Awaited<ReturnType<typeof listen>>;+ let bridge: AgentBridge;+ const seen: Array<{ path: string; headers: Record<string, unknown>; body: string }> = [];+ beforeEach(async () => {+ seen.length = 0;+ up = await listen(async (req, res) => {+ const chunks: Buffer[] = [];+ for await (const c of req) chunks.push(c as Buffer);+ seen.push({ path: req.url!, headers: req.headers, body: Buffer.concat(chunks).toString("utf8") });+ res.writeHead(200, { "content-type": "text/event-stream" }).end("event: message_stop\ndata: {}\n\n");+ });+ bridge = new AgentBridge({ upstream: () => ({ baseUrl: up.url + "/v1", apiKey: "real-key" }), ask: async () => ({ id: "" }), wait: async () => ({}), cancel: async () => {} });+ await bridge.start();+ });+ afterEach(async () => {+ await bridge.stop();+ await up.close();+ });+ const post = (p: string, headers: Record<string, string>, body: unknown) => fetch(bridge.url + p, { method: "POST", headers: { "content-type": "application/json", ...headers }, body: JSON.stringify(body) });++ it("Anthropic clients authenticate with x-api-key; upstream gets the real key, never the bridge token", async () => {+ const r = await post("/v1/messages?beta=true", { "x-api-key": bridge.token, "anthropic-version": "2023-06-01" }, { model: "m", messages: [] });+ expect(r.status).toBe(200);+ expect(await r.text()).toContain("message_stop");+ expect(seen[0].path).toBe("/v1/messages?beta=true");+ expect(seen[0].headers).toMatchObject({ authorization: "Bearer real-key", "x-api-key": "real-key", "anthropic-version": "2023-06-01" });+ expect(JSON.stringify(seen[0].headers)).not.toContain(bridge.token);+ });++ it("OpenAI clients keep using Authorization; no x-api-key is invented for them", async () => {+ expect((await post("/v1/responses", { authorization: `Bearer ${bridge.token}` }, { model: "m", input: [] })).status).toBe(200);+ expect(seen[0].headers.authorization).toBe("Bearer real-key");+ expect(seen[0].headers["x-api-key"]).toBeUndefined();+ });++ it("rejects a wrong token in either header", async () => {+ expect((await post("/v1/messages", { "x-api-key": "nope" }, {})).status).toBe(401);+ expect((await post("/v1/responses", { authorization: "Bearer nope" }, {})).status).toBe(401);+ expect((await post("/v1/messages", { "x-api-key": bridge.token.slice(1) }, {})).status).toBe(401);+ expect(seen).toHaveLength(0);+ });++ it("troubleshooting sees model requests on every API, with the first user message", async () => {+ const events: ProxyEvent[] = [];+ bridge.onProxy((e) => void events.push(e));+ const auth = { authorization: `Bearer ${bridge.token}` };+ await post("/v1/chat/completions", auth, { model: "a", tools: [{ type: "function", function: { name: "bash" } }], messages: [{ role: "user", content: "hi chat" }] });+ await post("/v1/responses", auth, { model: "b", tools: [{ type: "function", name: "bash" }], input: [{ role: "system", content: "sys" }, { role: "user", content: [{ type: "input_text", text: "hi responses" }] }] });+ await post("/v1/messages?beta=true", { "x-api-key": bridge.token }, { model: "c", tools: [{ name: "bash" }], messages: [{ role: "user", content: [{ type: "text", text: "hi messages" }] }] });+ const reqs = events.filter((e) => e.phase === "request") as Array<Extract<ProxyEvent, { phase: "request" }>>;+ expect(reqs.map((e) => [e.path, e.model, e.tools, e.lastUser])).toEqual([+ ["/v1/chat/completions", "a", ["bash"], "hi chat"],+ ["/v1/responses", "b", ["bash"], "hi responses"],+ ["/v1/messages", "c", ["bash"], "hi messages"],+ ]);+ });+});