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.

fix(agent): a video generation longer than 5 minutes no longer fails its tool call

Marla9e6dc92fd177
diff --git a/src/agent/bridge.ts b/src/agent/bridge.ts
index cbef5c9..a7f1932 100644
--- a/src/agent/bridge.ts
+++ b/src/agent/bridge.ts
@@ -5,6 +5,8 @@ import type { AddressInfo } from "node:net";
import { logger, redactText, registerSecret } from "../log.js";
const log = logger("agent.bridge");
+/** 长调用的保活间隔:远小于 fetch 默认的 5 分钟数据间隔超时 */
+const KEEPALIVE_MS = 60_000;
export interface BridgeHandlers {
/** 模型上游(magpie 网关或外部端点),密钥只在本进程内使用 */
@@ -176,10 +178,28 @@ export class AgentBridge {
else if (url === "/dsh/cancel") out = (await this.h.cancel(body), { ok: true });
else if (url === "/dsh/memory" && this.h.memory) out = await this.h.memory(body);
else if (url === "/dsh/steward" && this.h.steward) out = await this.h.steward(body);
- else if (url === "/dsh/media" && this.h.media) out = await this.h.media(body);
+ else if (url === "/dsh/media" && this.h.media) return this.longCall(res, this.h.media(body));
else return void res.writeHead(404).end();
res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify(out));
}
+
+ /**
+ * 可能要十几分钟的调用(生视频):先回响应头,每分钟写一个空格,做完再写 JSON(前导空白不影响解析)。
+ * 子进程里的 fetch 等响应头、等下一段数据都只等 5 分钟,超时工具就失败了,生成却还在继续、照样扣费。
+ */
+ private async longCall(res: http.ServerResponse, p: Promise<unknown>): Promise<void> {
+ res.writeHead(200, { "content-type": "application/json" });
+ res.flushHeaders();
+ const keep = setInterval(() => {
+ if (!res.writableEnded && !res.destroyed) res.write(" ");
+ }, KEEPALIVE_MS);
+ res.on("close", () => clearInterval(keep));
+ try {
+ res.end(JSON.stringify(await p.catch((e: Error) => ({ error: e.message }))));
+ } finally {
+ clearInterval(keep);
+ }
+ }
}
/** 请求体里的模型名、工具名与第一条用户消息开头(只在有人订阅时解析;解析失败就不给) */
diff --git a/test/agent-bridge.unit.test.ts b/test/agent-bridge.unit.test.ts
new file mode 100644
index 0000000..0b5f1f5
--- /dev/null
+++ b/test/agent-bridge.unit.test.ts
@@ -0,0 +1,77 @@
+/** 内部 bridge:生视频这种长调用不能被子进程里 fetch 的 5 分钟超时打断。 */
+import http from "node:http";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { AgentBridge } from "../src/agent/bridge.js";
+
+let bridge: AgentBridge | undefined;
+afterEach(async () => {
+ vi.useRealTimers();
+ await bridge?.stop();
+ bridge = undefined;
+});
+
+/** 像 amb-media 插件那样 POST /dsh/media;返回响应头到达的时刻和逐段收到的正文 */
+function post(b: AgentBridge, path: string, body: unknown) {
+ const chunks: string[] = [];
+ let ended!: () => void;
+ const end = new Promise<void>((r) => (ended = r));
+ const response = new Promise<http.IncomingMessage>((resolve, reject) => {
+ const req = http.request(b.url + path, { method: "POST", headers: { authorization: `Bearer ${b.token}`, "content-type": "application/json" } }, (res) => {
+ res.setEncoding("utf8");
+ res.on("data", (c: string) => chunks.push(c));
+ res.on("end", ended);
+ resolve(res);
+ });
+ req.on("error", reject);
+ req.end(JSON.stringify(body));
+ });
+ return { response, chunks, end };
+}
+
+const within = <T>(p: Promise<T>, ms: number) => Promise.race([p, new Promise<"timeout">((r) => setTimeout(() => r("timeout"), ms))]);
+
+describe("bridge /dsh/media", () => {
+ it("answers with headers at once and keeps the connection alive until a long generation finishes", async () => {
+ vi.useFakeTimers({ toFake: ["setInterval", "clearInterval"] });
+ let finish!: (v: unknown) => void;
+ const seen: unknown[] = [];
+ bridge = new AgentBridge({
+ upstream: () => ({ baseUrl: "", apiKey: "" }),
+ ask: async () => ({ id: "" }),
+ wait: async () => ({}),
+ cancel: async () => undefined,
+ media: (b) => (seen.push(b), new Promise((r) => (finish = r))),
+ });
+ await bridge.start();
+ const r = post(bridge, "/dsh/media", { botId: "b", op: "video", args: { prompt: "a cat" } });
+ // 子进程里的 fetch 只等 5 分钟响应头:头必须在生成完成之前就到
+ const res = await within(r.response, 2000);
+ expect(res, "no response headers while the generation runs").not.toBe("timeout");
+ expect((res as http.IncomingMessage).statusCode).toBe(200);
+ expect(seen).toEqual([{ botId: "b", op: "video", args: { prompt: "a cat" } }]);
+ // 每分钟一个空格,fetch 两段数据之间也只等 5 分钟
+ for (let i = 1; i <= 6; i++) {
+ await vi.advanceTimersByTimeAsync(60_000);
+ await vi.waitFor(() => expect(r.chunks.join("")).toBe(" ".repeat(i)));
+ }
+ finish({ text: "已用 v1 生成视频,保存在工作区:media/video-1.mp4。" });
+ await r.end;
+ expect(JSON.parse(r.chunks.join(""))).toEqual({ text: "已用 v1 生成视频,保存在工作区:media/video-1.mp4。" });
+ });
+
+ it("a failing handler still ends with a JSON error the plugin can show", async () => {
+ bridge = new AgentBridge({
+ upstream: () => ({ baseUrl: "", apiKey: "" }),
+ ask: async () => ({ id: "" }),
+ wait: async () => ({}),
+ cancel: async () => undefined,
+ media: async () => {
+ throw new Error("boom");
+ },
+ });
+ await bridge.start();
+ const r = post(bridge, "/dsh/media", {});
+ await r.end;
+ expect(JSON.parse(r.chunks.join(""))).toEqual({ error: "boom" });
+ });
+});