🤖 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
fix(models): 连接 Modelflare 时启用全部对话模型
dyk98ccfd68e29f2c
diff --git a/src/app.ts b/src/app.tsindex 51cc6de..01940b3 100644--- a/src/app.ts+++ b/src/app.ts@@ -109,6 +109,7 @@ export class App {account!: AccountManager;private notices: Array<{ at: number; level: "info" | "warn"; text: string }> = [];private accountTimer: NodeJS.Timeout | undefined;+ private picksTimer: NodeJS.Timeout | undefined;private modelInfoCache: { at: number; data: Record<string, ModelInfo> } | undefined;constructor(readonly config: AppConfig, readonly opts: AppOptions = {}) {@@ -176,6 +177,27 @@ export class App {return routeModel(model, this.magpie.hasProvider(own) ? own : (this.magpie.modelflareProviderId ?? MODELFLARE_PROVIDER));}+ /**+ * 让每个 Modelflare 连接启用的模型等于 Modelflare 当前列出的全部对话模型(见 setModelPicks):+ * 0.1.8 及以前连接的补上,新上架的加入,下架的移出。读不到列表时什么都不改,下次再试。+ */+ private async syncModelPicks(): Promise<void> {+ const magpie = this.magpie;+ if (!magpie || magpie.state !== "running") return;+ const baseUrl = modelflareBaseUrl(this.config.modelflare.line);+ const ids = [magpie.modelflareProviderId, ...magpie.botProviderIds()].filter((id): id is string => !!id);+ for (const id of ids) {+ const key = magpie.providerKey(id);+ if (!key) continue;+ try {+ const r = await verifyModelflareKey(baseUrl, key, { line: this.config.modelflare.line });+ if (r.ok) await magpie.setModelPicks(id, r.chatModels);+ } catch (e) {+ log.warn("sync model picks failed", { provider: id, message: (e as Error).message });+ }+ }+ }+/** 该机器人请求 Modelflare 用的 Key(只在本进程内使用) */private botKey(botId: string): string {if (!this.magpie) return "";@@ -202,7 +224,10 @@ export class App {disconnect: () => this.disconnectModelflare(),setBotKey: async (botId, key) => {if (!this.magpie) throw new Error("外部模型端点模式下不管理 Modelflare 连接");- await this.magpie.setBotProvider(botId, key, modelflareBaseUrl(this.config.modelflare.line), this.allowedModelflareUrls(), this.requestHeaders());+ const baseUrl = modelflareBaseUrl(this.config.modelflare.line);+ const r = await verifyModelflareKey(baseUrl, key, { line: this.config.modelflare.line });+ if (!r.ok) throw new Error(r.message);+ await this.magpie.setBotProvider(botId, key, baseUrl, this.allowedModelflareUrls(), this.requestHeaders(), r.chatModels);},removeBotKey: async (botId) => {await this.magpie?.removeBotProvider(botId);@@ -293,6 +318,12 @@ export class App {this.seedDefaultBot();for (const bot of this.store.listBots()) await this.startBot(bot.id);if (!this.opts.noWeb) this.web = await startWebServer(this);+ if (this.magpie) {+ // 启动时和之后每小时核对一次启用的模型(新上架的加入,下架的移出)+ void this.syncModelPicks();+ this.picksTimer = setInterval(() => void this.syncModelPicks(), 3600_000);+ this.picksTimer.unref();+ }if (this.magpie && !this.opts.noWeb) {// 余额与低余额提醒:启动后读一次,之后每 5 分钟一次const tick = () => {@@ -306,6 +337,7 @@ export class App {async stop(): Promise<void> {if (this.accountTimer) clearInterval(this.accountTimer);+ if (this.picksTimer) clearInterval(this.picksTimer);this.account?.cancelLogin();await this.web?.close();for (const id of [...this.channels.keys()]) await this.stopBot(id);@@ -340,7 +372,7 @@ export class App {const baseUrl = modelflareBaseUrl(line);const r = await verifyModelflareKey(baseUrl, apiKey.trim(), { line });if (!r.ok) return r;- await this.magpie.connectModelflare(apiKey, baseUrl, this.allowedModelflareUrls(), this.requestHeaders());+ await this.magpie.connectModelflare(apiKey, baseUrl, this.allowedModelflareUrls(), this.requestHeaders(), r.chatModels);this.modelInfoCache = undefined;this.setModelflareLineConfig(line);await this.runtime.listModels(true).catch(() => []);diff --git a/src/magpie/supervisor.ts b/src/magpie/supervisor.tsindex 6c3a5da..cd19dac 100644--- a/src/magpie/supervisor.ts+++ b/src/magpie/supervisor.ts@@ -366,17 +366,33 @@ export class MagpieSupervisor {* (key 不出现在进程参数里)。重复连接 = 覆盖。baseUrl 必须是白名单里的地址。* headers 是每个请求额外带给 Modelflare 的请求头(调用归因)。*/- async connectModelflare(apiKey: string, baseUrl: string, allowed: string[], headers: Record<string, string> = {}): Promise<void> {- await this.upsertProvider(this.modelflareId(), MODELFLARE_PROVIDER, "Modelflare", apiKey, baseUrl, allowed, headers);+ async connectModelflare(apiKey: string, baseUrl: string, allowed: string[], headers: Record<string, string> = {}, picks: string[] = []): Promise<void> {+ await this.upsertProvider(this.modelflareId(), MODELFLARE_PROVIDER, "Modelflare", apiKey, baseUrl, allowed, headers, picks);}/** 机器人自己的 provider(预算 Key):id 固定为 mfb-<botId>,机器人的请求都显式发到这个 provider */- async setBotProvider(botId: string, apiKey: string, baseUrl: string, allowed: string[], headers: Record<string, string> = {}): Promise<void> {+ async setBotProvider(botId: string, apiKey: string, baseUrl: string, allowed: string[], headers: Record<string, string> = {}, picks: string[] = []): Promise<void> {const id = botProviderId(botId);- await this.upsertProvider(this.hasProvider(id) ? id : undefined, id, `Modelflare ${botId}`, apiKey, baseUrl, allowed, headers);+ await this.upsertProvider(this.hasProvider(id) ? id : undefined, id, `Modelflare ${botId}`, apiKey, baseUrl, allowed, headers, picks);}- private async upsertProvider(existing: string | undefined, wantId: string, name: string, apiKey: string, baseUrl: string, allowed: string[], headers: Record<string, string>): Promise<void> {+ /**+ * 启用的模型(picks)。内置网关对超过 24 个模型、又没有 picks 的服务商只放出列表前 24 个,没放出的+ * 连 "provider/model" 也不服务,所以由我们写明全部对话模型。pickedFrom 由网关自己记录;Modelflare 的+ * 列表还有不能对话的决策模型不在 picks 里,网关因此不会自动加入新模型,由 App 定时核对(syncModelPicks)。+ */+ async setModelPicks(id: string, picks: string[]): Promise<void> {+ const cur = this.modelPicks(id);+ if (!picks.length || (cur.length === picks.length && picks.every((m) => cur.includes(m)))) return;+ await this.saveProvider(id, { models: picks });+ }++ modelPicks(id: string): string[] {+ const p = this.readProviderRaw(id);+ return Array.isArray(p?.models) ? p.models.map(String) : [];+ }++ private async upsertProvider(existing: string | undefined, wantId: string, name: string, apiKey: string, baseUrl: string, allowed: string[], headers: Record<string, string>, picks: string[]): Promise<void> {if (!allowed.includes(baseUrl)) throw new Error("不支持的 Modelflare 地址");if (!apiKey.trim()) throw new Error("缺少 API Key");let id = existing;@@ -385,7 +401,7 @@ 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 });+ await this.saveProvider(id, { chat: baseUrl, key: apiKey.trim(), headers, ...(picks.length ? { models: picks } : {}) });}/** 切换线路:只改地址,key 不变(机器人自己的 provider 一起切) */diff --git a/src/modelflare.ts b/src/modelflare.tsindex a1017c8..f80f534 100644--- a/src/modelflare.ts+++ b/src/modelflare.ts@@ -90,7 +90,16 @@ export function modelflareBaseUrl(line: ModelflareLine, env: NodeJS.ProcessEnv =export type VerifyFailure = "invalid_key" | "forbidden" | "rate_limited" | "upstream" | "network" | "bad_response";export type VerifyResult =- | { ok: true; models: string[] }+ | {+ ok: true;+ models: string[];+ /**+ * 能对话的模型(Modelflare 标为 kind=text 的;旧版网关没有 kind 时为全部)。+ * 连接时把它们全部写进内置网关的 picks:网关对超过 24 个模型、又没有 picks 的服务商+ * 只放出列表前 24 个,而 Modelflare 的列表按字母排,后面的模型会被截掉。+ */+ chatModels: string[];+ }| { ok: false; code: VerifyFailure; status?: number; message: string; suggestLine?: ModelflareLine };/** 校验密钥:只发一次 GET /models,不重试、不换线路。 */@@ -120,9 +129,15 @@ export async function verifyModelflareKey(if (res.status === 429) return { ok: false, code: "rate_limited", status: 429, message: `Modelflare 暂时不可用(HTTP 429)${retryAfter(res)}` };if (!res.ok) return { ok: false, code: "upstream", status: res.status, message: `Modelflare 暂时不可用(HTTP ${res.status})` };try {- const body = (await res.json()) as { data?: Array<{ id?: unknown }> };+ const body = (await res.json()) as { data?: Array<{ id?: unknown; kind?: unknown }> };if (!Array.isArray(body.data)) throw new Error("no data[]");- return { ok: true, models: body.data.map((m) => String(m.id ?? "")).filter(Boolean).sort() };+ const rows = body.data.map((m) => ({ id: String(m.id ?? ""), kind: typeof m.kind === "string" ? m.kind : "" })).filter((m) => m.id);+ const classified = rows.some((m) => m.kind);+ return {+ ok: true,+ models: rows.map((m) => m.id).sort(),+ chatModels: rows.filter((m) => !classified || m.kind === "text").map((m) => m.id).sort(),+ };} catch {return { ok: false, code: "bad_response", status: res.status, message: "Modelflare 返回了无法识别的模型列表" };}diff --git a/test/helpers.ts b/test/helpers.tsindex 2ba1624..a09e44f 100644--- a/test/helpers.ts+++ b/test/helpers.ts@@ -23,7 +23,7 @@ async function body(req: http.IncomingMessage): Promise<any> {}/** 模拟 OpenAI 兼容端点:/v1/models + 流式 /v1/chat/completions(回声 + 模型名) */-export async function mockOpenAI(opts: { models?: string[]; key?: string | string[]; reply?: (msgs: any[], model: string) => string } = {}) {+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 }> = [];@@ -34,7 +34,9 @@ export async function mockOpenAI(opts: { models?: string[]; key?: string | strinreturn;}if (req.url?.endsWith("/models") && req.method === "GET") {- res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify({ object: "list", data: models.map((id) => ({ id, object: "model" })) }));+ res.writeHead(200, { "content-type": "application/json" }).end(+ JSON.stringify({ object: "list", data: models.map((id) => ({ id, object: "model", ...(opts.kinds?.[id] !== undefined ? { kind: opts.kinds[id] } : {}) })) }),+ );return;}if (req.url?.endsWith("/chat/completions") && req.method === "POST") {diff --git a/test/model-picks.integration.test.ts b/test/model-picks.integration.test.tsnew file mode 100644index 0000000..53057a8--- /dev/null+++ b/test/model-picks.integration.test.ts@@ -0,0 +1,56 @@+/**+ * 集成测试:真实内置 magpie。它对超过 24 个模型、又没有 picks 的服务商只放出列表前 24 个(没放出的+ * 连 "provider/model" 也不服务);连接 Modelflare 时要把全部对话模型写成 picks,决策模型不算。+ */+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;++// 按字母排序时第 24 个之后的模型,正是以前被截掉的那些+const chat = Array.from({ length: 30 }, (_, i) => `model-${String(i).padStart(2, "0")}`);+const kinds: Record<string, string> = { ...Object.fromEntries(chat.map((m) => [m, "text"])), "jev-decide": "", "img-gen": "image" };++d("Modelflare 模型很多时全部启用", () => {+ let llm: Awaited<ReturnType<typeof mockOpenAI>>;+ let app: App;++ beforeAll(async () => {+ llm = await mockOpenAI({ key: "mf-many-key", models: [...chat, "jev-decide", "img-gen"], kinds });+ process.env.AMB_MODELFLARE_BASE_URL = llm.url + "/v1";+ const dataDir = fs.mkdtempSync(path.join(os.tmpdir(), "amb-picks-"));+ 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();+ }, 60_000);++ afterAll(async () => {+ delete process.env.AMB_MODELFLARE_BASE_URL;+ await app?.stop();+ await llm?.close();+ });++ it("列出全部 30 个对话模型,不含决策与生图模型;第 24 个之后的也能对话", async () => {+ const r = await app.connectModelflare("mf-many-key", "auto");+ expect(r.ok && r.chatModels).toEqual(chat);+ let list: string[] = [];+ await until(async () => {+ list = await app.runtime.listModels(true).catch(() => []);+ return list.length >= chat.length;+ }, 15_000, 200);+ expect(list.map((m) => m.replace(/^modelflare\//, "")).sort()).toEqual(chat);+ app.store.updateBot("default", { defaultModel: "model-29" });+ const out = { text: "", async apply(op: any) { if (op.type === "stream.finish") this.text = op.text; } };+ await app.runtime.handle({ id: "e1", channel: "web", botId: "default", conversation: { kind: "dm", id: "c" }, sender: { id: "u" }, parts: [{ type: "text", text: "你好" }], replyHandle: {}, receivedAt: Date.now() }, out);+ expect(out.text).toContain("[model-29] 你说:你好");+ }, 40_000);+});diff --git a/test/unit.test.ts b/test/unit.test.tsindex 7c92925..0ac90b4 100644--- a/test/unit.test.ts+++ b/test/unit.test.ts@@ -249,10 +249,14 @@ describe("Modelflare", () => {});try {const base = srv.url + "/v1";- expect(await verifyModelflareKey(base, "k1")).toEqual({ ok: true, models: ["a", "b"] });+ // 没有 kind 的旧版网关:全部算对话模型+ expect(await verifyModelflareKey(base, "k1")).toEqual({ ok: true, models: ["a", "b"], chatModels: ["a", "b"] });expect(auth.at(-1)).toBe("Bearer k1");+ // 有 kind 时只有 text 是对话模型(生图、生视频、决策模型不算)+ body = JSON.stringify({ data: [{ id: "t", kind: "text" }, { id: "i", kind: "image" }, { id: "v", kind: "video" }, { id: "d" }] });+ expect(await verifyModelflareKey(base, "k1")).toEqual({ ok: true, models: ["d", "i", "t", "v"], chatModels: ["t"] });body = JSON.stringify({ data: [] });- expect(await verifyModelflareKey(base, "k1")).toEqual({ ok: true, models: [] });+ expect(await verifyModelflareKey(base, "k1")).toEqual({ ok: true, models: [], chatModels: [] });status = 401;expect(await verifyModelflareKey(base, "k")).toMatchObject({ ok: false, code: "invalid_key", status: 401 });status = 403;