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.

Merge origin/master into feat/server-compaction

Marla842ce545e98a
diff --cc src/agent/manager.ts
index b4ba8aa,87307e2..5c5ada6
--- a/src/agent/manager.ts
+++ b/src/agent/manager.ts
@@@ -3,9 -3,9 +3,10 @@@ import type { Store } from "../store/db
import { logger } from "../log.js";
import { AgentBridge } from "./bridge.js";
import path from "node:path";
- import { AgentHost, TurnStopped, callDetail, modelLimits, 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 { AgentHost, TurnStopped, callDetail, modelLimits, type ModelApiInfo, type TurnInput, type TurnObserver, type TurnUsage } from "./host.js";
+import { ServerCompaction, type CompactEndpoint, type CompactionEvent } from "./server-compaction.js";
import { ActivityLog, type ActivityStep } from "./activity.js";
+ import { ApiFallbacks, type ApiFallback } from "./api-fallback.js";
import type { MediaModel, MediaPlan, MediaService } from "./media.js";
import { APPROVAL_TTL_MS, ApprovalCenter, type QuestionItem } from "./approvals.js";
import { MemoryError, MemoryStore, type Scope } from "./memory.js";
@@@ -45,14 -45,8 +46,16 @@@ export interface AgentManagerOptions
onNotice?: (text: string) => void;
/** 模型的上下文长度(Modelflare 模型资料),写进 DSH 补丁 */
contextWindow?: (model: string) => Promise<number | undefined>;
+ /** 模型支持的接口与是否推理(Modelflare 模型资料),DSH 按它选对话接口 */
+ apiInfo?: (model: string) => Promise<ModelApiInfo | undefined>;
+ /**
+ * 服务端压缩(见 server-compaction.ts):listed = 价格目录是否列出该模型的压缩端点(只是提示,用前还要探测);
+ * endpoint = 该模型(网关里的名字)直连的压缩端点。缺省则不做。
+ */
+ serverCompaction?: {
+ listed(model: string): Promise<boolean | undefined>;
+ endpoint(model: string): CompactEndpoint | undefined;
+ };
}
/** 一个进行中的回合(/status、/stop、排队提示、信息面板和故障排查用) */
@@@ -125,9 -107,9 +130,10 @@@ export class AgentManager
memory: (b) => this.memoryOp(b),
steward: (b) => this.stewardCall(b),
media: (b) => this.mediaOp(b),
+ compaction: this.serverCompaction,
});
this.memory = new MemoryStore(path.join(o.dataDir, "memory"));
+ this.apiFallbacks = new ApiFallbacks(path.join(o.dataDir, "dsh", "api-fallback.json"));
}
/** 生图 / 生视频服务;未接内置网关时没有 */
@@@ -634,16 -610,15 +642,18 @@@
available: boolean;
runtimeDir?: string;
hosts: Record<string, Record<string, unknown>>;
+ apiFallbacks: Array<ApiFallback & { model: string }>;
turns: Array<{ conversationId: string; botId: string; forMs: number; idleMs?: number; lastStep?: ActivityStep }>;
+ serverCompaction?: { enabled: boolean; models: Record<string, { ok: boolean; at: number; reason: string }> };
} {
return {
enabled: this.o.enabled,
available: this.available,
runtimeDir: this.o.runtimeDir,
+ // 服务端压缩:各模型的探测结论(支持 / 不支持及原因)
+ ...(this.serverCompaction ? { serverCompaction: { enabled: this.serverCompaction.enabled, models: this.serverCompaction.status() } } : {}),
hosts: Object.fromEntries([...this.hosts].map(([id, h]) => [id, h.diagnostics()])),
+ apiFallbacks: this.apiFallbacks.list(),
turns: [...this.turnsInProgress].map(([conversationId, t]) => ({
conversationId,
botId: t.botId,
diff --cc src/app.ts
index fcee7ab,429a1e4..d287579
--- a/src/app.ts
+++ b/src/app.ts
@@@ -161,19 -161,13 +162,21 @@@ 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 };
},
+ // 服务端压缩:价格目录列出压缩端点只是提示,bridge 用之前会探测
+ serverCompaction: {
+ listed: async (model) => {
- const info = await this.modelInfoFor(model);
++ const info = await this.infoOf(model);
+ return info?.endpoints ? info.endpoints.includes(COMPACT_ENDPOINT) : undefined;
+ },
+ endpoint: (model) => this.compactEndpoint(model),
+ },
// 后台小任务(管家判定/交接/记忆整理)直接走 magpie,不经代理子进程
complete: (botId, model, system, user) =>
this.models.streamChat(
@@@ -219,35 -213,6 +222,25 @@@
}
}
- /** 某个模型(网关里的名字也行)的 Modelflare 模型资料 */
- private async modelInfoFor(model: string): Promise<ModelInfo | undefined> {
- const info = await this.modelInfo();
- const bare = bareModel(model);
- return info[bare] ?? info[bare.split("/").pop() ?? bare];
- }
-
+ /**
+ * 服务端压缩直连的端点:内置网关不转发 /v1/responses/compact(回 404),压缩请求直接发给模型所在的 Modelflare 连接
+ * (同一把 Key)。压缩后的请求仍经网关;网关要先开启 nativeCompaction,带压缩项的请求被拒时才会原样报错,
- * 而不是悄悄删掉压缩项重试(那样会丢掉之前的上下文)。外部端点模式直接用那个端点。
++ * 而不是悄悄删掉压缩项重试(那样会丢掉之前的上下文)。外部端点模式下代理只走 Chat(见 apiInfo),不做。
+ */
+ private compactEndpoint(model: string): CompactEndpoint | undefined {
- if (!this.magpie) {
- const baseUrl = this.config.magpie.externalBaseUrl.replace(/\/+$/, "");
- return baseUrl ? { baseUrl, apiKey: this.config.magpie.externalApiKey, model } : undefined;
- }
++ if (!this.magpie) return undefined;
+ // 网关里的模型名是 <provider id>/<上游模型名>;provider 必须正好是它(upstreamEndpoint 找不到时会退回共用连接)
+ const i = model.indexOf("/");
+ const id = i > 0 ? model.slice(0, i) : "";
+ const up = id ? this.magpie.upstreamEndpoint(id) : undefined;
+ if (!up || up.id !== id) return undefined;
+ if (!this.magpie.nativeCompaction(up.id)) {
+ void this.magpie.ensureNativeCompaction(up.id).catch((e) => log.warn("turning on native compaction failed", { provider: up.id, message: (e as Error).message }));
+ return undefined;
+ }
+ return { baseUrl: up.baseUrl, apiKey: up.apiKey, model: model.slice(i + 1), headers: this.requestHeaders() };
+ }
+
/** 每个经 Modelflare 的请求都带的请求头(调用归因) */
requestHeaders(): Record<string, string> {
return skillHeader(appVersion());