🤖 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(agent): give DSH the model's real context window
Marla1be5a0d0ff6b
diff --git a/src/account/model-info.ts b/src/account/model-info.tsindex 62ff660..0611a58 100644--- a/src/account/model-info.ts+++ b/src/account/model-info.ts@@ -23,6 +23,7 @@ interface Profile {export interface PricingEntry {model_name: string;+ context_window?: number;quota_type: number;input_price?: number;model_ratio?: number;@@ -92,6 +93,8 @@ export async function loadModelInfo(o: {for (const entry of catalog?.data ?? []) {const price = referencePrice(entry, catalog!);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) };}return out;}diff --git a/src/agent/host.ts b/src/agent/host.tsindex e0fbb71..d2386af 100644--- a/src/agent/host.ts+++ b/src/agent/host.ts@@ -112,6 +112,12 @@ export function deliverables(workspace: string, paths: string[]): string[] {return out.slice(0, 8);}+/** p 在 ms 内没结果就用 fallback */+function withTimeoutOr<T>(p: Promise<T>, ms: number, fallback: T): Promise<T> {+ let t: NodeJS.Timeout | undefined;+ return Promise.race([p, new Promise<T>((r) => (t = setTimeout(() => r(fallback), ms)))]).finally(() => clearTimeout(t));+}+let sdkPromise: Promise<SdkModule> | undefined;function loadSdk(runtimeDir: string): Promise<SdkModule> {sdkPromise ??= import(pathToFileURL(path.join(runtimeDir, "node_modules", "@deepseek-ai", "dsh-sdk-client", "lib", "index.js")).href) as Promise<SdkModule>;@@ -154,6 +160,8 @@ export interface AgentHostOptions {onNotice?: (text: string) => void;/** 能生图 / 生视频(已连接 Modelflare):写入 media 技能说明 */media?: boolean;+ /** 模型的上下文长度(Modelflare 模型资料);不知道时 undefined,用内置兜底表 */+ contextWindow?: (model: string) => Promise<number | undefined>;}export type HostState = "stopped" | "starting" | "running" | "backoff" | "error";@@ -194,18 +202,25 @@ export type HostEvent =| { type: "failed"; message: string }| { type: "exit"; pid: number | undefined; code: number | null; signal: string | null };+/** 拿不到 Modelflare 的模型资料时的兜底(上下文窗口、最大输出) */const MODEL_LIMITS: Array<[RegExp, number, number]> = [+ [/-terra\b/i, 1_050_000, 32_000],[/grok-4/i, 256_000, 32_000],[/claude/i, 200_000, 32_000],- [/gpt-5|gpt-4\.1/i, 400_000, 32_000],+ [/gpt-[5-9]|gpt-4\.1/i, 400_000, 32_000],[/gemini/i, 1_000_000, 32_000],[/deepseek/i, 128_000, 32_000],];-/** 模型的上下文窗口与最大输出(与写入 DSH 补丁的数值一致)。 */-export function modelLimits(model: string): { window: number; maxTokens: number } {+/**+ * 模型的上下文窗口与最大输出(写进 DSH 补丁,上下文管家也按它算检查线)。+ * known:Modelflare 模型资料里的上下文长度,有就用它;窗口写小了,DSH 和管家会过早、反复地压缩上下文。+ */+export function modelLimits(model: string, known?: number): { window: number; maxTokens: number } {const lim = MODEL_LIMITS.find(([re]) => re.test(model)) ?? [/./, 128_000, 16_000];- return { window: lim[1], maxTokens: lim[2] };+ const window = Number(known) > 0 ? Number(known) : lim[1];+ // 最大输出不超过窗口的四分之一(小窗口模型留够消息空间)+ return { window, maxTokens: Math.min(lim[2], Math.floor(window / 4)) };}/** 随消息交给模型的图片(DSH SDK 的内联图片块:base64 + 格式,进会话前存入 DSH 的附件库) */@@ -276,6 +291,8 @@ export class AgentHost {lastError = "";restarts = 0;model = "";+ /** 当前子进程补丁里写的上下文窗口与最大输出 */+ private limits: { window: number; maxTokens: number } | undefined;private harness: SdkHarness | undefined;private starting: Promise<SdkHarness> | undefined;private opened = new Set<string>();@@ -347,8 +364,8 @@ export class AgentHost {});}- private writePatch(model: string, apps: CuaApp[] = [], pwshPath?: string): string {- const lim = MODEL_LIMITS.find(([re]) => re.test(model)) ?? [/./, 128_000, 16_000];+ private writePatch(model: string, apps: CuaApp[] = [], pwshPath?: string, lim = modelLimits(model)): string {+ this.limits = lim;const plugin = path.join(this.o.runtimeDir, "plugins", "amb-bridge.mjs");const memory = path.join(this.o.runtimeDir, "plugins", "amb-memory.mjs");this.writeMediaSkill();@@ -365,8 +382,8 @@ export class AgentHost {" models:",` - id: ${yamlStr(model)}`,` name: ${yamlStr(model.split("/").pop() || model)}`,- ` contextWindow: ${lim[1]}`,- ` maxTokens: ${lim[2]}`,+ ` contextWindow: ${lim.window}`,+ ` maxTokens: ${lim.maxTokens}`,// 默认只收文字;能看图的模型声明图片输入,用户发来的图片才会作为图片交给它...(acceptsImages(model) ? [" input: [text, image]"] : []),// Windows:命令用检测到的 PowerShell 7(DSH 自己只看 Program Files\PowerShell\7 和 PATH,找不到就退回 5.1)@@ -666,7 +683,8 @@ exec "$REAL" "$@"const cu = this.computerUseActive();const apps = cu && cu.backend === "cua" ? await discoverAppsCached() : [];const pwshPath = await pwshProbe.dshPath();- const patch = this.writePatch(model, apps, pwshPath);+ 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));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)" } : {}) });@@ -816,7 +834,7 @@ exec "$REAL" "$@"let timer: NodeJS.Timeout | undefined;let turnError: string | undefined;const presented: string[] = [];- const lim = modelLimits(input.model);+ const lim = input.model === this.model && this.limits ? this.limits : modelLimits(input.model);const usage: TurnUsage = { tokens: 0, window: lim.window, maxTokens: lim.maxTokens };const images = this.imageInput ? (input.images ?? []) : [];let unlisten = () => {};diff --git a/src/agent/manager.ts b/src/agent/manager.tsindex 2f08e7a..1bf3ad3 100644--- a/src/agent/manager.ts+++ b/src/agent/manager.ts@@ -42,6 +42,8 @@ export interface AgentManagerOptions {media?: MediaService;/** 需要让用户看到的提示(代理进程起不来等) */onNotice?: (text: string) => void;+ /** 模型的上下文长度(Modelflare 模型资料),写进 DSH 补丁 */+ contextWindow?: (model: string) => Promise<number | undefined>;}/** 一个进行中的回合(/status、/stop、排队提示、信息面板和故障排查用) */@@ -132,6 +134,7 @@ export class AgentManager {settings: () => this.settings(botId),onNotice: this.o.onNotice,media: !!this.o.media,+ contextWindow: this.o.contextWindow,});this.hosts.set(botId, h);}diff --git a/src/app.ts b/src/app.tsindex 8bc8fc9..f37410e 100644--- a/src/app.ts+++ b/src/app.ts@@ -22,6 +22,7 @@ import { findAppRoot } from "./magpie/supervisor.js";import {MODELFLARE_LINES,MODELFLARE_PROVIDER,+ bareModel,botProviderId,fetchKeyBalance,modelflareBaseUrl,@@ -158,6 +159,12 @@ export class App {turnTimeoutMs: config.agent.turnTimeoutMs,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;+ },// 后台小任务(管家判定/交接/记忆整理)直接走 magpie,不经代理子进程complete: (botId, model, system, user) =>this.models.streamChat(diff --git a/test/account.test.ts b/test/account.test.tsindex 5b0bd8d..3469fa7 100644--- a/test/account.test.ts+++ b/test/account.test.ts@@ -11,7 +11,7 @@ import { listen, mockModelflare, approveInBrowser, readJson } from "./helpers.jsimport { discover, startLogin, pkcePair } from "../src/account/oauth.js";import { AccountManager, summarizeUsage, type AccountHooks } from "../src/account/manager.js";import { AccountVault } from "../src/account/vault.js";-import { referencePrice } from "../src/account/model-info.js";+import { loadModelInfo, referencePrice } from "../src/account/model-info.js";import { MediaService, docSection } from "../src/agent/media.js";import { AgentManager } from "../src/agent/manager.js";import { locateDshRuntime } from "../src/agent/host.js";@@ -254,6 +254,21 @@ describe("参考价", () => {});});+describe("模型资料", () => {+ it("上下文长度:有 Key 用 model-profiles,没有就用公开价格目录里的", async () => {+ const fetchImpl = (async (url: string) => {+ if (url.endsWith("/model-profiles")) return Response.json({ data: { "gpt-6.1-terra": { context_window: 1_050_000, input_modalities: ["text", "image"] } } });+ return Response.json({ data: [{ model_name: "gpt-6.1-terra", quota_type: 0, context_window: 999 }, { model_name: "grok-4.7", quota_type: 0, context_window: 256_000 }] });+ }) as any;+ const base = { baseUrl: "http://mf/v1", headers: {}, pricingUrl: "http://mf/api/pricing", fetchImpl };+ const withKey = await loadModelInfo({ ...base, apiKey: "k" });+ expect(withKey["gpt-6.1-terra"]).toMatchObject({ context: 1_050_000, vision: true });+ expect(withKey["grok-4.7"].context).toBe(256_000);+ const noKey = await loadModelInfo({ ...base, apiKey: "" });+ expect(noKey["gpt-6.1-terra"].context).toBe(999);+ });+});+describe("生图 / 生视频", () => {const png = Buffer.from("89504e470d0a1a0a", "hex");it("用机器人的 Key 调 Images API,图片存进工作区 media/;视频建任务、轮询、下载", async () => {diff --git a/test/agent.unit.test.ts b/test/agent.unit.test.tsindex 276f942..04a0cd7 100644--- a/test/agent.unit.test.ts+++ b/test/agent.unit.test.ts@@ -6,6 +6,7 @@ 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";let dir: string;let store: Store;@@ -203,3 +204,24 @@ describe("WeChat media delivery", () => {expect(deliverables(ws, presentPaths(args))).toEqual([path.join(fs.realpathSync(ws), "ok.txt")]);});});++describe("context window written for DSH", () => {+ it("uses the window from Modelflare's model data; the built-in table is only a fallback", () => {+ expect(modelLimits("mfb-b/gpt-6.1-terra", 1_050_000)).toEqual({ window: 1_050_000, maxTokens: 32_000 });+ expect(modelLimits("modelflare/gpt-6.1-terra")).toEqual({ window: 1_050_000, maxTokens: 32_000 });+ expect(modelLimits("gpt-6.2-sol")).toEqual({ window: 400_000, maxTokens: 32_000 });+ expect(modelLimits("some-new-model")).toEqual({ window: 128_000, maxTokens: 16_000 });+ // 小窗口模型:最大输出不超过窗口的四分之一+ expect(modelLimits("claude-mini", 32_000)).toEqual({ window: 32_000, maxTokens: 8_000 });+ });++ it("the patch carries those numbers", () => {+ 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 file = (host as any).writePatch("mfb-b/gpt-6.1-terra", [], undefined, modelLimits("gpt-6.1-terra", 1_050_000));+ const text = fs.readFileSync(file, "utf8");+ expect(text).toMatch(/contextWindow: 1050000\n\s+maxTokens: 32000/);+ fs.rmSync(dataDir, { recursive: true, force: true });+ });+});