Skip to content

🤖 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.

feat(models): Modelflare 连接带上 Responses 和 Messages 地址,Claude 走 Messages

dyk987b43e816168f
diff --git a/src/app.ts b/src/app.ts
index 01940b3..f3e38ca 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -180,6 +180,7 @@ export class App {
/**
* 让每个 Modelflare 连接启用的模型等于 Modelflare 当前列出的全部对话模型(见 setModelPicks):
* 0.1.8 及以前连接的补上,新上架的加入,下架的移出。读不到列表时什么都不改,下次再试。
+ * 顺带给 0.1.8 及以前的连接补上 Responses 和 Messages 地址(ensureModelflareApis)。
*/
private async syncModelPicks(): Promise<void> {
const magpie = this.magpie;
@@ -190,6 +191,7 @@ export class App {
const key = magpie.providerKey(id);
if (!key) continue;
try {
+ await magpie.ensureModelflareApis(id);
const r = await verifyModelflareKey(baseUrl, key, { line: this.config.modelflare.line });
if (r.ok) await magpie.setModelPicks(id, r.chatModels);
} catch (e) {
diff --git a/src/magpie/supervisor.ts b/src/magpie/supervisor.ts
index cd19dac..15f9b0c 100644
--- a/src/magpie/supervisor.ts
+++ b/src/magpie/supervisor.ts
@@ -5,7 +5,7 @@ import net from "node:net";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { logger } from "../log.js";
-import { BOT_PROVIDER_PREFIX, MODELFLARE_PROVIDER, botProviderId, keyTail } from "../modelflare.js";
+import { BOT_PROVIDER_PREFIX, MODELFLARE_PROVIDER, botProviderId, keyTail, modelflareApiBases } from "../modelflare.js";
const log = logger("magpie");
@@ -401,13 +401,22 @@ export class MagpieSupervisor {
if (add.code !== 0) throw new Error(`创建 Modelflare 连接失败:${(add.stderr || add.stdout).trim().slice(0, 300)}`);
id = this.hasProvider(wantId) ? wantId : await this.findProviderId(wantId);
}
- await this.saveProvider(id, { chat: baseUrl, key: apiKey.trim(), headers, ...(picks.length ? { models: picks } : {}) });
+ await this.saveProvider(id, { ...modelflareApiBases(baseUrl), key: apiKey.trim(), headers, ...(picks.length ? { models: picks } : {}) });
}
/** 切换线路:只改地址,key 不变(机器人自己的 provider 一起切) */
async setModelflareUrl(baseUrl: string, allowed: string[]): Promise<void> {
if (!allowed.includes(baseUrl)) throw new Error("不支持的 Modelflare 地址");
- for (const id of [this.modelflareId(), ...this.botProviderIds()]) if (id) await this.saveProvider(id, { chat: baseUrl });
+ for (const id of [this.modelflareId(), ...this.botProviderIds()]) if (id) await this.saveProvider(id, modelflareApiBases(baseUrl));
+ }
+
+ /** 0.1.8 及以前的连接只写了 Chat 地址:补上 Responses 和 Messages(跟 Chat 同一条线路) */
+ async ensureModelflareApis(id: string): Promise<void> {
+ const p = this.readProviderRaw(id);
+ const chat = typeof p?.chat === "string" ? p.chat : "";
+ if (!chat) return;
+ const want = modelflareApiBases(chat);
+ if (p?.responses !== want.responses || p?.anthropic !== want.anthropic) await this.saveProvider(id, want);
}
/** 断开 Modelflare:删除共用连接和所有机器人自己的 provider */
diff --git a/src/modelflare.ts b/src/modelflare.ts
index f80f534..f8c3cf0 100644
--- a/src/modelflare.ts
+++ b/src/modelflare.ts
@@ -87,6 +87,15 @@ export function modelflareBaseUrl(line: ModelflareLine, env: NodeJS.ProcessEnv =
return line === "cn" ? MODELFLARE_LINES.cn : MODELFLARE_LINES.global;
}
+/**
+ * 交给内置网关的三种对话接口地址:Chat Completions、Responses 都在 {base}(…/v1)下,Anthropic Messages
+ * 的地址是根(网关自己拼 /v1/messages)。网关按模型挑:Claude 走 Messages(提示缓存和思考过程只有它保留),
+ * 其余走 Chat;上游说某个模型不在这个接口上时,网关换另一个接口再试并记住。
+ */
+export function modelflareApiBases(baseUrl: string): { chat: string; responses: string; anthropic: string } {
+ return { chat: baseUrl, responses: baseUrl, anthropic: baseUrl.replace(/\/v1\/?$/, "") };
+}
+
export type VerifyFailure = "invalid_key" | "forbidden" | "rate_limited" | "upstream" | "network" | "bad_response";
export type VerifyResult =
diff --git a/test/helpers.ts b/test/helpers.ts
index a09e44f..6300baf 100644
--- a/test/helpers.ts
+++ b/test/helpers.ts
@@ -22,11 +22,17 @@ async function body(req: http.IncomingMessage): Promise<any> {
return s ? JSON.parse(s) : {};
}
-/** 模拟 OpenAI 兼容端点:/v1/models + 流式 /v1/chat/completions(回声 + 模型名) */
+/** Anthropic 的 content 可以是字符串或内容块数组:取出文字 */
+const textOf = (c: any): string => (typeof c === "string" ? c : Array.isArray(c) ? c.map((b) => (b?.type === "text" ? b.text : "")).join("") : "");
+
+/**
+ * 模拟 Modelflare 的对话接口:/v1/models、流式 /v1/chat/completions,以及 Claude 用的 /v1/messages
+ * (Anthropic 格式)。回声 + 模型名;calls[].api 记下走的是哪个接口。
+ */
export async function mockOpenAI(opts: { models?: string[]; key?: string | string[]; reply?: (msgs: any[], model: string) => string; kinds?: Record<string, string> } = {}) {
const models = opts.models ?? ["mock-a", "mock-b"];
const keys = opts.key === undefined ? undefined : ([] as string[]).concat(opts.key);
- const calls: Array<{ model: string; messages: any[]; auth?: string; skill?: string }> = [];
+ const calls: Array<{ model: string; messages: any[]; auth?: string; skill?: string; api: "chat" | "messages" }> = [];
const stats = { aborted: 0 };
const srv = await listen(async (req, res) => {
if (keys && !keys.some((k) => req.headers.authorization === `Bearer ${k}`)) {
@@ -41,7 +47,7 @@ export async function mockOpenAI(opts: { models?: string[]; key?: string | strin
}
if (req.url?.endsWith("/chat/completions") && req.method === "POST") {
const b = await body(req);
- calls.push({ model: b.model, messages: b.messages, auth: req.headers.authorization, skill: req.headers["x-modelflare-skill"] as string | undefined });
+ calls.push({ model: b.model, messages: b.messages, auth: req.headers.authorization, skill: req.headers["x-modelflare-skill"] as string | undefined, api: "chat" });
const last = b.messages[b.messages.length - 1]?.content ?? "";
const text = opts.reply ? opts.reply(b.messages, b.model) : `[${b.model}] 你说:${last}`;
if (!b.stream) {
@@ -63,6 +69,29 @@ export async function mockOpenAI(opts: { models?: string[]; key?: string | strin
res.end();
return;
}
+ if (req.url?.split("?")[0].endsWith("/v1/messages") && req.method === "POST") {
+ const b = await body(req);
+ const messages = (b.messages ?? []).map((m: any) => ({ role: m.role, content: textOf(m.content) }));
+ calls.push({ model: b.model, messages, auth: req.headers.authorization, skill: req.headers["x-modelflare-skill"] as string | undefined, api: "messages" });
+ const text = opts.reply ? opts.reply(messages, b.model) : `[${b.model}] 你说:${messages.at(-1)?.content ?? ""}`;
+ const usage = { input_tokens: 1, output_tokens: 1 };
+ if (!b.stream) {
+ res.writeHead(200, { "content-type": "application/json" }).end(
+ JSON.stringify({ id: "msg_mock", type: "message", role: "assistant", model: b.model, content: [{ type: "text", text }], stop_reason: "end_turn", stop_sequence: null, usage }),
+ );
+ return;
+ }
+ res.writeHead(200, { "content-type": "text/event-stream" });
+ const ev = (type: string, data: object) => res.write(`event: ${type}\ndata: ${JSON.stringify({ type, ...data })}\n\n`);
+ ev("message_start", { message: { id: "msg_mock", type: "message", role: "assistant", model: b.model, content: [], stop_reason: null, stop_sequence: null, usage: { ...usage, output_tokens: 0 } } });
+ ev("content_block_start", { index: 0, content_block: { type: "text", text: "" } });
+ for (const p of text.match(/.{1,4}/gsu) ?? []) ev("content_block_delta", { index: 0, delta: { type: "text_delta", text: p } });
+ ev("content_block_stop", { index: 0 });
+ ev("message_delta", { delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 1 } });
+ ev("message_stop", {});
+ res.end();
+ return;
+ }
res.writeHead(404).end();
});
return {
diff --git a/test/modelflare-apis.integration.test.ts b/test/modelflare-apis.integration.test.ts
new file mode 100644
index 0000000..87b209c
--- /dev/null
+++ b/test/modelflare-apis.integration.test.ts
@@ -0,0 +1,81 @@
+/**
+ * 集成测试:真实内置 magpie + 模拟的 Modelflare。连接时把 Chat、Responses、Messages 三个地址都交给网关:
+ * Claude 走 /v1/messages,其余走 /v1/chat/completions;0.1.8 及以前只写了 Chat 地址的连接在启动时补上。
+ */
+import { describe, it, expect, beforeAll, afterAll } from "vitest";
+import fs from "node:fs";
+import os from "node:os";
+import path from "node:path";
+import { App } from "../src/app.js";
+import { loadConfig } from "../src/config.js";
+import { resolveMagpieBinary } from "../src/magpie/supervisor.js";
+import { FakeFeishu, mockOpenAI, until } from "./helpers.js";
+
+const d = resolveMagpieBinary() ? describe : describe.skip;
+
+const models = ["claude-opus-5-5", "gpt-6-sol"];
+
+d("Modelflare 的三种对话接口", () => {
+ let llm: Awaited<ReturnType<typeof mockOpenAI>>;
+ let app: App;
+ let dataDir: string;
+ const file = () => path.join(dataDir, "magpie", "config", "magpie", "providers.json");
+ const provider = () => {
+ const j = JSON.parse(fs.readFileSync(file(), "utf8"));
+ return (Array.isArray(j) ? j : j.providers).find((p: any) => p.id === "modelflare");
+ };
+ const boot = async () => {
+ app = new App(loadConfig({ dataDir, env: { AMB_UI_PORT: "0", AMB_WEIXIN_API_BASE: "http://127.0.0.1:9", AMB_AGENT: "off" } }), {
+ feishuTransport: () => new FakeFeishu(),
+ printQrToTerminal: false,
+ noWeb: true,
+ });
+ await app.start();
+ };
+ const say = async (model: string, text: string) => {
+ app.store.updateBot("default", { defaultModel: model });
+ const out = { text: "", async apply(op: any) { if (op.type === "stream.finish") this.text = op.text; } };
+ await app.runtime.handle({ id: `e-${model}`, channel: "web", botId: "default", conversation: { kind: "dm", id: `c-${model}` }, sender: { id: "u" }, parts: [{ type: "text", text }], replyHandle: {}, receivedAt: Date.now() }, out);
+ return out.text;
+ };
+
+ beforeAll(async () => {
+ llm = await mockOpenAI({ key: "mf-api-key", models, kinds: Object.fromEntries(models.map((m) => [m, "text"])) });
+ process.env.AMB_MODELFLARE_BASE_URL = llm.url + "/v1";
+ dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "amb-apis-"));
+ await boot();
+ }, 60_000);
+
+ afterAll(async () => {
+ delete process.env.AMB_MODELFLARE_BASE_URL;
+ await app?.stop();
+ await llm?.close();
+ });
+
+ it("连接时写入三个地址;Claude 走 Messages,GPT 走 Chat", async () => {
+ const r = await app.connectModelflare("mf-api-key", "auto");
+ expect(r.ok).toBe(true);
+ expect(provider()).toMatchObject({ chat: llm.url + "/v1", responses: llm.url + "/v1", anthropic: llm.url });
+ await until(async () => (await app.runtime.listModels(true).catch(() => [])).length >= models.length, 15_000, 200);
+
+ expect(await say("claude-opus-5-5", "你好")).toContain("[claude-opus-5-5] 你说:你好");
+ expect(llm.calls.at(-1)).toMatchObject({ api: "messages", model: "claude-opus-5-5", auth: "Bearer mf-api-key" });
+
+ expect(await say("gpt-6-sol", "在吗")).toContain("[gpt-6-sol] 你说:在吗");
+ expect(llm.calls.at(-1)).toMatchObject({ api: "chat", model: "gpt-6-sol" });
+ }, 40_000);
+
+ it("只写了 Chat 地址的旧连接:重启后补上 Responses 和 Messages", async () => {
+ await app.stop();
+ const j = JSON.parse(fs.readFileSync(file(), "utf8"));
+ for (const p of Array.isArray(j) ? j : j.providers) if (p.id === "modelflare") delete p.responses, delete p.anthropic;
+ fs.writeFileSync(file(), JSON.stringify(j, null, 2));
+ expect(provider().anthropic).toBeUndefined();
+
+ await boot();
+ await until(async () => provider().anthropic === llm.url, 20_000, 200);
+ expect(provider()).toMatchObject({ chat: llm.url + "/v1", responses: llm.url + "/v1", anthropic: llm.url });
+ expect(await say("claude-opus-5-5", "又见面了")).toContain("[claude-opus-5-5] 你说:又见面了");
+ expect(llm.calls.at(-1)?.api).toBe("messages");
+ }, 60_000);
+});