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(agent): live control of DSH — steer, per-conversation stop and model, long commands in the background

Marlafe98f568e3ce
diff --git a/docs/development.md b/docs/development.md
index 2ad316b..9cb832d 100644
--- a/docs/development.md
+++ b/docs/development.md
@@ -96,9 +96,12 @@ docker exec amb cat /data/ui-token # 打开 http://<主机>:3600/?token=<令
- 设置 → Modelflare:账户、余额与充值、余额提醒、用量、通知、邀请;连接状态(只显示 Key 尾号);未登录时可更换密钥或断开(机器人与会话保留,断开后回到首次连接页);默认模型、刷新模型列表。
- 对话中输入框右侧的模型按钮可为当前会话切换模型(也可设为机器人默认);`/model`、`/reset`、`/new`、`/status`、`/stop`、`/help` 照常可用。
- 处理过程:代理回合的每一步(模型请求及用时与 tokens、工具调用及参数与输出、重试、子代理)折叠在回复上方的"用时"一行里,点开查看;微信 / 飞书会话在信息面板 → 最近会话里看,正在处理的那一轮实时显示并可中止。
-- 卡住的消息:`/stop`(任何渠道)或信息面板里的「中止」立即结束正在处理的回合;代理 10 分钟没有任何进展(模型不回、工具不动、只在重试;等你批准或回答不算)会自动中止,单回合最长 30 分钟(`AMB_AGENT_IDLE_TIMEOUT_MS`、`AMB_AGENT_TURN_TIMEOUT_MS` 可调)。DSH 没有中断单个回合的接口,中止时会重启该机器人的代理进程。前一条还在处理时发来的新消息会先收到一句提示;`/status` 显示它走到了哪一步、多久没有进展。网页对话里切到别的机器人或刷新窗口不会打断正在处理的消息,回来时接着显示(你的消息、到目前为止的回复和处理过程);停止按钮在排队、代理启动中、等你批准时也能用,纯对话也一样。
+- 卡住的消息:`/stop`(任何渠道)或信息面板里的「中止」立即结束正在处理的回合;代理 10 分钟没有任何进展(模型不回、工具不动、只在重试;等你批准或回答不算)会自动中止,单回合最长 30 分钟(`AMB_AGENT_IDLE_TIMEOUT_MS`、`AMB_AGENT_TURN_TIMEOUT_MS` 可调)。中止只停这个会话的回合(经代理进程里的 amb-control 插件取消它),同一机器人的其它会话照常进行;10 秒内没停下(`AMB_STOP_GRACE_MS`)才重启代理进程。`/status` 显示它走到了哪一步、多久没有进展。网页对话里切到别的机器人或刷新窗口不会打断正在处理的消息,回来时接着显示(你的消息、到目前为止的回复和处理过程);停止按钮在排队、代理启动中、等你批准时也能用,纯对话也一样。
+- 插话:代理正在处理时又发来的消息(文字连同图片 / 文件)直接并入这一轮,模型在下一步就看到,回复一起给(先回一句"收到");代理还没接手或正在收尾时照常排队,命令(`/model`、`/reset`…)也排队。网页对话在回复进行中同样可以接着发。
+- 长命令转后台:命令跑过 30 秒(Windows PowerShell 60 秒)就转成后台任务、不再卡住这一轮,代理先回复"还在跑";命令结束时代理被唤醒、拿到结果,回复作为一条新消息发到同一个会话(网页对话实时显示,飞书回复你最近的那条消息,微信占一条回复配额),处理过程里能看到后台命令的步骤,`/status`、中止、看门狗都认这一轮。
+- 每个会话用自己的模型:同一个代理进程里各会话各走各的模型和接口(每种接口一个 provider,上下文窗口、最大输出按模型),换模型不重启代理进程;补丁里没列出的模型在运行中补上。
- 重启后的提醒:应用退出或崩溃时还没处理完的消息(每个会话在数据库里留一个处理中的标记,处理完就删),下次启动时在原会话里说一次「上次的消息「…」因为应用重启没有处理完,请重新发送」,不会自动重发。网页对话记进会话历史;微信用存下的 context_token 发送、占用同一份回复配额,超过 24 小时窗口或配额用完时等你在那个会话里发下一条消息时先说;飞书发到原单聊。
-- 代理的对话接口:代理按 Modelflare 公开目录里该模型支持的接口选——有 Responses API 的(GPT-5.4 及以后、Grok 4.5 及以后、DeepSeek V4、GLM-5、Kimi K3 等)走 `/v1/responses`(推理模型默认推理强度 medium,加密推理随历史回放);没有 Responses、有 Anthropic Messages 的(Claude)走 `/v1/messages`;其余模型、拿不到目录时(离线、中国大陆线路)以及外部端点走 Chat Completions。接口在拉起代理进程时选定(故障排查的代理状态里显示 `api`);`AMB_AGENT_API=chat` 强制全部走 Chat Completions(排查用)。纯对话(非代理)仍走 Chat Completions。安全网:某个模型在 Responses / Messages 上的请求被上游拒绝(HTTP 400/404/405/415/422 或“不支持的参数 / 接口”,限流、余额、权限、上游故障、上下文过长不算)时,这个模型 24 小时内改走 Chat Completions(记在数据目录的 `dsh/api-fallback.json`,日志有警告,代理状态显示 `openai-completions (fallback: 原因)`);这一轮如果还没调用过工具,代理换到 Chat 上自动重做一遍,用户照常收到回复,已经调用过工具的则照常报错、下一轮起走 Chat。
+- 代理的对话接口:代理按 Modelflare 公开目录里该模型支持的接口选——有 Responses API 的(GPT-5.4 及以后、Grok 4.5 及以后、DeepSeek V4、GLM-5、Kimi K3 等)走 `/v1/responses`(推理模型默认推理强度 medium,加密推理随历史回放);没有 Responses、有 Anthropic Messages 的(Claude)走 `/v1/messages`;其余模型、拿不到目录时(离线、中国大陆线路)以及外部端点走 Chat Completions。接口按模型在代理进程里第一次用到它时选定(故障排查的代理状态里显示最近一轮的 `api` 和进程里的模型);`AMB_AGENT_API=chat` 强制全部走 Chat Completions(排查用)。纯对话(非代理)仍走 Chat Completions。安全网:某个模型在 Responses / Messages 上的请求被上游拒绝(HTTP 400/404/405/415/422 或“不支持的参数 / 接口”,限流、余额、权限、上游故障、上下文过长不算)时,这个模型 24 小时内改走 Chat Completions(记在数据目录的 `dsh/api-fallback.json`,日志有警告,代理状态显示 `openai-completions (fallback: 原因)`);这一轮如果还没调用过工具,这个会话换到 Chat 上自动重做一遍(不重启代理进程),用户照常收到回复,已经调用过工具的则照常报错、下一轮起走 Chat。
- 内置网关:magpie 以 `--gateway` 模式运行,使用本程序数据目录内独立的 HOME/配置目录;**不读取**你电脑上已有的 Claude/Codex 等订阅凭据,**不改写**任何 agent 配置,关闭自动更新与账户发现。它只供本进程内部调用,界面不再反代它的页面或 `/v1`。
- API Key 只保存在内置网关的隔离配置中,不经过命令行参数、不写日志,前端只拿到尾号。
- 开发/测试:设置 `AMB_MAGPIE_BASE_URL` 可直接使用一个 OpenAI 兼容端点(不启动内置网关,界面视为已连接,账户功能不可用);`AMB_MODELFLARE_BASE_URL` 可把 Modelflare 基址指向本地 mock,`AMB_MODELFLARE_ISSUER` 把登录与账户接口指向本地 mock。
diff --git a/docs/development_en.md b/docs/development_en.md
index 70cc113..dd028a5 100644
--- a/docs/development_en.md
+++ b/docs/development_en.md
@@ -96,9 +96,12 @@ Inside the container the console listens on 0.0.0.0, so token access is enforced
- Settings → Modelflare: account, balance and top-up, balance reminder, usage, notifications, invites; connection status (only the key's last characters are shown); when not signed in you can replace the key or disconnect (bots and sessions are kept; disconnecting returns to the first-connection screen); default model, refresh model list.
- The model button to the right of the composer switches the model for the current session (or sets it as the bot default); `/model`, `/reset`, `/new`, `/status`, `/stop` and `/help` all work.
- Activity: every step of an agent turn (model requests with timing and tokens, tool calls with arguments and output, retries, sub-agents) is folded into the "time" line above the reply; click to expand. For WeChat / Feishu sessions, see info panel → Recent sessions, where the turn in progress is shown live and can be aborted.
-- Stuck messages: `/stop` (any channel) or "Abort" in the info panel ends the current turn immediately. A turn with no progress for 10 minutes (no model reply, no tool activity, only retries; waiting for your approval or answer does not count) is aborted automatically, and a single turn is capped at 30 minutes (`AMB_AGENT_IDLE_TIMEOUT_MS`, `AMB_AGENT_TURN_TIMEOUT_MS`). DSH has no API to interrupt a single turn, so aborting restarts that bot's agent process. A new message that arrives while the previous one is still being handled gets a short notice first; `/status` shows which step it reached and how long since the last progress. In the web chat, switching to another bot or reloading the window does not interrupt a message in progress; when you return it picks up where it was (your message, the reply so far and the activity). The stop button also works while queued, while the agent is starting and while waiting for approval, and for plain chat too.
+- Stuck messages: `/stop` (any channel) or "Abort" in the info panel ends the current turn immediately. A turn with no progress for 10 minutes (no model reply, no tool activity, only retries; waiting for your approval or answer does not count) is aborted automatically, and a single turn is capped at 30 minutes (`AMB_AGENT_IDLE_TIMEOUT_MS`, `AMB_AGENT_TURN_TIMEOUT_MS`). Aborting stops only that conversation's turn (the amb-control plugin inside the agent process cancels it); the bot's other conversations keep running. Only if it has not stopped within 10 seconds (`AMB_STOP_GRACE_MS`) is the agent process restarted. `/status` shows which step it reached and how long since the last progress. In the web chat, switching to another bot or reloading the window does not interrupt a message in progress; when you return it picks up where it was (your message, the reply so far and the activity). The stop button also works while queued, while the agent is starting and while waiting for approval, and for plain chat too.
+- Steering: a message (text, images, files) sent while the agent is working joins the current turn; the model sees it at its next step and one reply covers both (you get a short "received" first). Before the agent has taken the previous message, or while the turn is finishing, it queues as before, and so do commands (`/model`, `/reset`…). In the web chat you can keep typing while a reply is in progress.
+- Long commands move to the background: a command still running after 30 seconds (60 for PowerShell on Windows) becomes a background job instead of blocking the turn, and the agent replies that it is still running. When it ends the agent is woken with the result and its reply arrives as a new message in the same conversation (live in the web chat, as a reply to your latest message on Feishu, using one WeChat reply from the quota). The activity shows the background command's steps, and `/status`, abort and the watchdog treat that turn like any other.
+- Each conversation uses its own model: inside one agent process every conversation runs on its own model and API (one provider per API, context window and max output per model). Switching models does not restart the agent process; a model the launch patch did not list is added at runtime.
- Notice after a restart: a message still being handled when the app quit or crashed (each conversation keeps an in-progress marker in the database, removed when the message is done) is reported once in its conversation at the next start — "上次的消息「…」因为应用重启没有处理完,请重新发送" (the last message was not finished because the app restarted; please send it again). Nothing is resent automatically. The web chat gets it in its history; WeChat uses the stored context_token and the same reply quota, and when the 24-hour window or the quota has run out it is said before the reply to your next message in that conversation; Feishu sends it to the original chat.
-- Agent API: the agent picks the API from what the Modelflare public catalog says the model supports — models with the Responses API (GPT-5.4 and later, Grok 4.5 and later, DeepSeek V4, GLM-5, Kimi K3 and others) use `/v1/responses` (reasoning models default to medium effort; encrypted reasoning is replayed with history); models without Responses but with Anthropic Messages (Claude) use `/v1/messages`; all other models, the case where the catalog is unavailable (offline, the mainland China route) and external endpoints use Chat Completions. The API is chosen when the agent process starts (shown as `api` in the agent status under Troubleshooting); `AMB_AGENT_API=chat` forces Chat Completions everywhere (for troubleshooting). Plain chat (non-agent) still uses Chat Completions. Safety net: when the upstream rejects a model's Responses / Messages request (HTTP 400/404/405/415/422 or "unsupported parameter / endpoint"; rate limits, balance, permissions, upstream outages and context overflows do not count), that model switches to Chat Completions for 24 hours (recorded in `dsh/api-fallback.json` in the data directory, with a warning in the log, and the agent status shows `openai-completions (fallback: reason)`). If the turn has not called any tool yet, the agent redoes it on Chat automatically and the user still gets a reply; if it already called tools, the error is reported and the next turn uses Chat.
+- Agent API: the agent picks the API from what the Modelflare public catalog says the model supports — models with the Responses API (GPT-5.4 and later, Grok 4.5 and later, DeepSeek V4, GLM-5, Kimi K3 and others) use `/v1/responses` (reasoning models default to medium effort; encrypted reasoning is replayed with history); models without Responses but with Anthropic Messages (Claude) use `/v1/messages`; all other models, the case where the catalog is unavailable (offline, the mainland China route) and external endpoints use Chat Completions. The API is chosen per model the first time the agent process uses it (the agent status under Troubleshooting shows the last turn's `api` and the models in the process); `AMB_AGENT_API=chat` forces Chat Completions everywhere (for troubleshooting). Plain chat (non-agent) still uses Chat Completions. Safety net: when the upstream rejects a model's Responses / Messages request (HTTP 400/404/405/415/422 or "unsupported parameter / endpoint"; rate limits, balance, permissions, upstream outages and context overflows do not count), that model switches to Chat Completions for 24 hours (recorded in `dsh/api-fallback.json` in the data directory, with a warning in the log, and the agent status shows `openai-completions (fallback: reason)`). If the turn has not called any tool yet, that conversation redoes it on Chat automatically (without restarting the agent process) and the user still gets a reply; if it already called tools, the error is reported and the next turn uses Chat.
- Built-in gateway: magpie runs in `--gateway` mode with its own HOME / config directory inside the app's data directory. It **does not read** existing Claude / Codex subscription credentials on your computer, **does not rewrite** any agent configuration, and has auto-update and account discovery turned off. It is only called from inside the app process; the console no longer proxies its pages or `/v1`.
- The API key is stored only in the gateway's isolated configuration — never on a command line, never in logs — and the front end only receives its last characters.
- Development / testing: `AMB_MAGPIE_BASE_URL` points the app at any OpenAI-compatible endpoint (the gateway is not started, the UI treats itself as connected, and account features are unavailable); `AMB_MODELFLARE_BASE_URL` points the Modelflare base URL at a local mock, and `AMB_MODELFLARE_ISSUER` points sign-in and account APIs at a local mock.
diff --git a/dsh-runtime/plugins/amb-control.mjs b/dsh-runtime/plugins/amb-control.mjs
new file mode 100644
index 0000000..9e9fbac
--- /dev/null
+++ b/dsh-runtime/plugins/amb-control.mjs
@@ -0,0 +1,283 @@
+// anymodel bot ⇄ DSH live control (MIT, part of anymodel bot).
+//
+// The npm 0.2.1-rc.4 SDK protocol only offers initialize / session/prompt / session/resume / shutdown. This
+// plugin gives the host a command channel into the running child: it long-polls the host over the loopback
+// bridge (POST /dsh/control/poll with AMB_CONTROL_TOKEN, a per-launch id; the bridge token is only ever sent to
+// AMB_BRIDGE_URL) and answers each command with POST /dsh/control/result. Commands:
+//
+// select {sessionId, provider, model} per-conversation model: every Agent's prompt assembly and request
+// config read this map (as DSH's installModelSelection does)
+// providers {providers} replace llm-pi-ai's (volatile) provider profiles, so a model the
+// launch patch did not list becomes routable without a restart
+// steer {sessionId, id, text, images} deliver a user message into the running turn (agent.steer: the
+// model sees it at its next step); refused when the agent is idle
+// cancel {sessionId} cancel just this session's turn (agent.cancel), its subagents
+// and the shell jobs the turn started
+//
+// Long shell commands move to background jobs (the host patches the shell timeouts); when one ends,
+// tool-jobs wakes the idle session with its result and the host follows that turn like any other.
+import { boundContextSummary, createUserMessage, freezeMessage } from '@deepseek-ai/dsh-llm'
+import { admitEncodedImages } from '@deepseek-ai/dsh-attachment'
+
+export const name = 'amb-control'
+
+/**
+ * Couple a session to the host's model selection, like DSH's installModelSelection (dsh-agent): prompt
+ * assembly snapshots the selection ({{model}}), the request config takes its provider/model, and a change of
+ * model adds a user-role notice to the next request. Unlike DSH's, a provider-only change (the same model moved
+ * to another wire API after an upstream rejection) adds no notice. No selection: the Agent's own options.
+ */
+function installSelection(agentCtx, current) {
+ let assembled
+ const offAssembly = agentCtx.on('system-prompt/assemble', async (_assembly, _context, next) => {
+ const selected = current()
+ const result = await next()
+ assembled = selected
+ if (!selected) return result
+ return { ...result, variables: { ...result.variables, provider: selected.provider, model: selected.model } }
+ })
+ const offRequest = agentCtx.on('agent/request', async (_payload, next) => {
+ const resolved = await next()
+ if (!assembled) return resolved
+ const { reasoningEffort: _inherited, ...rest } = resolved
+ return { ...rest, provider: assembled.provider, model: assembled.model }
+ })
+ const offNotice = agentCtx.on('agent/pre-step', async ({ agent, messages, signal, step }, next) => {
+ const decision = await next()
+ if (decision.kind === 'reject' || signal.aborted) return decision
+ if (decision.messages.length === 0 && (step === 1 || messages.length > 0)) return decision
+ const previous = agent.session.requestHeader()?.config
+ if (!assembled || !previous || previous.model === assembled.model) return decision
+ const from = previous.model, to = assembled.model
+ const notice = createUserMessage({
+ content: [{ type: 'text', text: `[model changed: assistant turns above this point were generated by ${from}; the session continues with ${to}]` }],
+ source: { kind: 'model-selection', form: 'notice', summary: boundContextSummary(`${from} → ${to}`) },
+ })
+ return { ...decision, messages: [...decision.messages, notice] }
+ }, { prepend: true })
+ return () => { offAssembly(); offRequest(); offNotice() }
+}
+
+/** How long a /stop waits for the turn's killed jobs to settle (so their end wakes no one). */
+const KILL_WAIT_MS = 5_000
+
+const sleep = (ms, signal) => new Promise((resolve) => {
+ const t = setTimeout(resolve, ms)
+ signal?.addEventListener('abort', () => { clearTimeout(t); resolve() }, { once: true })
+})
+const live = (status) => status === 'running' || status === 'stopping'
+
+function bridge() {
+ const url = process.env.AMB_BRIDGE_URL
+ const token = process.env.AMB_BRIDGE_TOKEN
+ if (!url || !token) return undefined
+ return async (path, body, signal) => {
+ const res = await fetch(url + path, {
+ method: 'POST',
+ headers: { authorization: `Bearer ${token}`, 'content-type': 'application/json' },
+ body: JSON.stringify(body),
+ signal,
+ })
+ if (!res.ok) throw new Error(`bridge ${path} ${res.status}`)
+ return res.json()
+ }
+}
+
+export function apply(ctx) {
+ /** sessionId → { provider, model } chosen by the host for that conversation */
+ const selections = new Map()
+ /** agent id → ids of jobs its shell moved to the background (the model wanted their result) */
+ const promoted = new Map()
+ /** agent id → when its current activity started (jobs started since then belong to the turn) */
+ const startedAt = new Map()
+ /** sessions being cancelled: no steering into them */
+ const cancelling = new Set()
+ /** steered message ids (idempotent retries) */
+ const delivered = new Set()
+
+ const agents = () => ctx.get('agents')
+ const jobs = () => ctx.get('jobs')
+
+ // ---------- per-conversation model ----------
+ ctx.on('agent/created', ({ agent }) => {
+ const id = String(agent.id)
+ installSelection(agent.ctx, () => selections.get(id))
+ })
+
+ ctx.on('agent/status', ({ agent, status }) => {
+ const id = String(agent?.id ?? '')
+ if (status === 'running') startedAt.set(id, Date.now())
+ else cancelling.delete(id)
+ })
+
+ ctx.on('agent/disposed', ({ agent }) => {
+ const id = String(agent?.id ?? '')
+ promoted.delete(id)
+ startedAt.delete(id)
+ cancelling.delete(id)
+ })
+
+ // ---------- auto-backgrounded commands ----------
+ ctx.on('tools/execute', async (exec, next) => {
+ const result = await next()
+ const value = result?.value
+ if (value && value.kind === 'promoted' && value.jobId && exec?.agent) {
+ const id = String(exec.agent.id)
+ if (!promoted.has(id)) promoted.set(id, new Set())
+ promoted.get(id).add(String(value.jobId))
+ }
+ return result
+ })
+
+ try {
+ ctx.inject?.(['systemPrompt'], (c) => {
+ c.systemPrompt.section({
+ name: 'amb:background',
+ order: c.systemPrompt.getSectionOrder('TOOL_JOBS'),
+ text: 'A shell command still running after its timeout moves to a background job by itself. You may end your turn while it runs: tell the user it is still running. When it finishes you are woken with its result, and your reply reaches the user as a follow-up message, so do not poll or sleep on it. Start servers and other processes that should keep running with run_in_background: true.',
+ })
+ })
+ } catch {}
+
+ // ---------- commands ----------
+ const steerable = (agent) => agent.status === 'running' && !cancelling.has(String(agent.id)) && !agent.phase?.abort?.signal?.aborted
+
+ const pendingIds = (agent) => {
+ try {
+ return new Set([...(agent.inbox?.nextTurn ?? []), ...(agent.inbox?.nextStep ?? [])].map((m) => m.id))
+ } catch {
+ return new Set()
+ }
+ }
+
+ async function updateProviders(providers) {
+ const loader = ctx.get('loader')
+ const entry = loader && [...loader.entries()].find((e) => e.options?.id === 'llm-pi-ai')
+ if (!entry) throw new Error('llm-pi-ai entry not found')
+ await entry.update({ config: { ...(entry.options.config ?? {}), providers } })
+ // a refused volatile update is only logged by the loader: check that every route resolves now
+ const llm = ctx.get('llm')
+ for (const [provider, profile] of Object.entries(providers)) {
+ for (const m of profile.models ?? []) await llm.resolveCallConfig({ provider, model: m.id })
+ }
+ }
+
+ /** jobs owned by the session that this turn started or auto-backgrounded, still running */
+ function turnJobs(agent) {
+ const registry = jobs()
+ if (!registry) return []
+ const id = String(agent.id)
+ const since = startedAt.get(id) ?? Infinity
+ const mine = promoted.get(id) ?? new Set()
+ return registry.list(id).filter((j) => j.owner === id && live(j.status) && (j.startedAt >= since || mine.has(j.id)))
+ }
+
+ async function killTurnJobs(agent) {
+ const registry = jobs()
+ const id = String(agent.id)
+ const list = turnJobs(agent)
+ // waits first, so each end counts as collected and tool-jobs injects no notice
+ const waits = list.map((j) => registry.wait(j.id, KILL_WAIT_MS, id).catch(() => undefined))
+ for (const j of list) try { registry.kill(j.id, id, 'stopped by the user') } catch {}
+ await Promise.all(waits)
+ }
+
+ function descendants(agent) {
+ const registry = agents()
+ const out = []
+ const walk = (parent) => {
+ for (const a of registry.list()) {
+ if (a !== parent && registry.isOwnedBy?.(a.id, parent)) {
+ out.push(a)
+ walk(a)
+ }
+ }
+ }
+ walk(agent)
+ return out
+ }
+
+ const handlers = {
+ async ping() {
+ return {}
+ },
+ async select({ sessionId, provider, model }) {
+ if (provider && model) selections.set(String(sessionId), { provider: String(provider), model: String(model) })
+ return {}
+ },
+ async providers({ providers }) {
+ await updateProviders(providers)
+ return {}
+ },
+ async steer({ sessionId, id, text, images }) {
+ const agent = agents()?.get(String(sessionId))
+ if (!agent) return { accepted: false, reason: 'no agent' }
+ if (delivered.has(id) || pendingIds(agent).has(id)) return { accepted: true, duplicate: true }
+ if (!steerable(agent)) return { accepted: false, reason: 'idle' }
+ const content = [{ type: 'text', text: String(text ?? '') }]
+ if (Array.isArray(images) && images.length) {
+ const attachments = ctx.get('attachments')
+ if (attachments) {
+ const refs = await admitEncodedImages(attachments, images.map((i) => ({ data: i.data, mediaType: i.mimeType })))
+ for (const ref of refs) content.push({ type: 'image', attachment: ref })
+ }
+ }
+ // re-check after the await: steering into an idle agent would start a turn nobody listens to
+ if (agents()?.get(String(sessionId)) !== agent || !steerable(agent)) return { accepted: false, reason: 'idle' }
+ agent.steer(freezeMessage({ id: String(id), role: 'user', content, source: { kind: 'user' } }))
+ delivered.add(id)
+ if (delivered.size > 1000) delivered.delete(delivered.values().next().value)
+ return { accepted: true }
+ },
+ async cancel({ sessionId }) {
+ const agent = agents()?.get(String(sessionId))
+ if (!agent) return { running: false }
+ const running = agent.status === 'running'
+ if (running) cancelling.add(String(agent.id))
+ for (const child of descendants(agent)) try { child.cancel({ kind: 'parent' }) } catch {}
+ agent.cancel({ kind: 'user' }, { keepInbox: false })
+ void killTurnJobs(agent).catch(() => {})
+ return { running }
+ },
+ }
+
+ // ---------- long poll ----------
+ const call = bridge()
+ const controlId = process.env.AMB_CONTROL_TOKEN
+ if (!call || !controlId) return
+ const botId = process.env.AMB_BOT_ID ?? ''
+ const stop = new AbortController()
+ try {
+ ctx.on('dispose', () => stop.abort())
+ } catch {}
+
+ const run = async (cmd) => {
+ let out
+ try {
+ const fn = handlers[cmd.op]
+ if (!fn) throw new Error(`unknown command ${cmd.op}`)
+ out = { ok: true, result: await fn(cmd) }
+ } catch (e) {
+ out = { ok: false, error: String(e?.message ?? e).slice(0, 500) }
+ }
+ await call('/dsh/control/result', { botId, controlId, id: cmd.id, ...out }).catch(() => {})
+ }
+
+ void (async () => {
+ let backoff = 250
+ while (!stop.signal.aborted) {
+ let res
+ try {
+ res = await call('/dsh/control/poll', { botId, controlId }, stop.signal)
+ backoff = 250
+ } catch {
+ if (stop.signal.aborted) return
+ await sleep(backoff, stop.signal)
+ backoff = Math.min(backoff * 2, 5_000)
+ continue
+ }
+ if (res?.stop) return
+ for (const cmd of res?.commands ?? []) void run(cmd)
+ }
+ })()
+}
diff --git a/src/agent/activity.ts b/src/agent/activity.ts
index d1481a8..3ec957b 100644
--- a/src/agent/activity.ts
+++ b/src/agent/activity.ts
@@ -7,7 +7,7 @@ import { summarizeCall } from "./host.js";
export interface ActivityStep {
/** 同一步的更新沿用同一个 id(工具调用 id、m<回合>.<步>…) */
id: string;
- kind: "model" | "tool" | "retry" | "subagent" | "compact" | "error";
+ kind: "model" | "tool" | "retry" | "subagent" | "compact" | "error" | "job" | "steer";
/** 模型名、工具名… */
title: string;
/** 一行摘要:命令、路径、回复开头、失败原因 */
@@ -38,6 +38,19 @@ function textOf(content: unknown): string {
.join("\n");
}
+/** 工具结果说命令转到了后台(前台等太久,DSH 把它改成后台任务):返回任务 id */
+export function promotedJob(data: any): string | undefined {
+ const text = textOf(data?.message?.content);
+ return /\[still running after \d+ms; moved to background job (\S+?)\]/.exec(text)?.[1];
+}
+
+/** 后台任务结束的通知(tool-jobs 在会话里插的用户消息):任务 id 与摘要 */
+function jobNotice(data: any): { id: string; summary: string } | undefined {
+ if (data?.source?.kind !== "tool-jobs") return undefined;
+ const id = /^background job (\S+)/.exec(textOf(data.content))?.[1];
+ return id ? { id, summary: String(data.source.summary ?? "") } : undefined;
+}
+
function prettyArgs(args: string): string {
try {
return JSON.stringify(JSON.parse(args || "{}"), null, 2);
@@ -122,8 +135,31 @@ export class ActivityLog {
const prev = this.find(String(msg.toolCallId ?? ""));
if (!prev) return true;
this.put({ ...prev, status: msg.isError ? "error" : "ok", ms: Math.max(0, time - prev.at), output: clip(textOf(msg.content), MAX_OUTPUT) || undefined });
+ // 命令跑得太久,转到了后台:单独记一步,结束时(job/settled 或结束通知)更新
+ const job = promotedJob(data);
+ if (job) this.put({ id: `j:${job}`, kind: "job", title: "后台命令", summary: prev.summary, input: prev.input, status: "running", at: time });
+ return true;
+ }
+ case "job/settled": {
+ const prev = this.find(`j:${data.jobId}`);
+ if (!prev || prev.status !== "running") return true;
+ const ok = data.status === "completed" && !/exit code: [1-9]/.test(String(data.detail ?? ""));
+ this.put({ ...prev, status: ok ? "ok" : "error", ms: Math.max(0, (Number(data.finishedAt) || time) - prev.at), output: data.detail ? String(data.detail) : prev.output });
return true;
}
+ case "user/message": {
+ // 后台命令结束,DSH 把结果交给代理(回合进行中并进下一步,空闲时唤醒代理开一轮)
+ const n = jobNotice(data);
+ if (!n) return true;
+ const prev = this.find(`j:${n.id}`);
+ if (prev?.status === "running") this.put({ ...prev, status: "ok", ms: Math.max(0, time - prev.at) });
+ else if (!prev) this.put({ id: `j:${n.id}`, kind: "job", title: "后台命令结束", summary: oneLine(n.summary) || undefined, status: "ok", at: time });
+ return true;
+ }
+ case "steer":
+ // 宿主发的:用户在回合进行中又发了一条,并进了这一轮
+ this.put({ id: `u${time}`, kind: "steer", title: "追加消息", summary: oneLine(String(data.text ?? "")), status: "ok", at: time });
+ return true;
case "llm/retry": {
const f = data.failure ?? {};
const why = String(f.message ?? f.code ?? "请求失败");
@@ -190,6 +226,11 @@ export class ActivityLog {
}
}
+ /** 回合正常结束:还在后台跑的命令不算失败,注明回复时它仍在运行(结束后代理会被唤醒,另发一条回复) */
+ finish(time = Date.now()): void {
+ for (const s of this.steps) if (s.kind === "job" && s.status === "running") this.put({ ...s, status: "ok", summary: [s.summary, "(回复时仍在后台运行)"].filter(Boolean).join(" "), ms: Math.max(0, time - s.at) });
+ }
+
/** 外部原因结束(/stop、看门狗、超时):还在进行的步骤标成失败,再记一条说明 */
end(why: string, time = Date.now()): void {
for (const s of this.steps) if (s.status === "running") this.put({ ...s, status: "error", cut: true, ms: Math.max(0, time - s.at) });
@@ -227,6 +268,10 @@ export function describeStep(s: ActivityStep): string {
return doing ? "子代理在工作" : `子代理结束${s.summary ? `:${s.summary}` : ""}`;
case "compact":
return doing ? "正在压缩上下文" : "压缩了上下文";
+ case "job":
+ return doing ? `后台命令在运行${s.summary ? `:${s.summary}` : ""}` : `${s.title}${s.summary ? `:${s.summary}` : ""}`;
+ case "steer":
+ return `收到追加消息${s.summary ? `:${s.summary}` : ""}`;
case "error":
return what;
}
diff --git a/src/agent/bridge.ts b/src/agent/bridge.ts
index cd3c93d..d69e66a 100644
--- a/src/agent/bridge.ts
+++ b/src/agent/bridge.ts
@@ -21,6 +21,8 @@ export interface BridgeHandlers {
steward?(body: any): Promise<unknown>;
/** amb-media 插件:生图 / 生视频 */
media?(body: any): Promise<unknown>;
+ /** amb-control 插件:长轮询取宿主的指令(signal = 连接断了)、回报结果 */
+ control?: { poll(body: any, signal: AbortSignal): Promise<unknown>; result(body: any): void };
/** 服务端压缩:长会话的 /v1/responses 换上上游压缩过的历史(见 server-compaction.ts) */
compaction?: ServerCompaction;
}
@@ -46,6 +48,7 @@ export const BOT_HEADER = "x-amb-bot";
* 仅回环的内部桥:DSH 子进程用每次启动随机生成的令牌访问。
* - /v1/*:模型代理(Chat Completions、Responses、Anthropic Messages),把令牌换成真实网关密钥(子进程环境里没有任何真实密钥)
* - /dsh/ask|wait|cancel:amb-bridge 插件的审批/提问长轮询
+ * - /dsh/control/poll|result:amb-control 插件取宿主的指令(中止、插话、换模型)并回报
*/
export class AgentBridge {
readonly token = crypto.randomBytes(24).toString("base64url");
@@ -263,6 +266,11 @@ export class AgentBridge {
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) return this.longCall(res, this.h.media(body));
+ else if (url === "/dsh/control/poll" && this.h.control) {
+ const ac = new AbortController();
+ res.on("close", () => ac.abort());
+ out = await this.h.control.poll(body, ac.signal);
+ } else if (url === "/dsh/control/result" && this.h.control) out = (this.h.control.result(body), { ok: true });
else return void res.writeHead(404).end();
res.writeHead(200, { "content-type": "application/json" }).end(JSON.stringify(out));
}
diff --git a/src/agent/control.ts b/src/agent/control.ts
new file mode 100644
index 0000000..04ffbc7
--- /dev/null
+++ b/src/agent/control.ts
@@ -0,0 +1,136 @@
+import crypto from "node:crypto";
+
+/** 发给代理进程里 amb-control 插件的一条指令 */
+export interface ControlCommand {
+ id: string;
+ op: string;
+ [k: string]: unknown;
+}
+
+/** 插件多久没来取指令就算断开(长轮询每 25 秒一轮) */
+const STALE_MS = 35_000;
+const POLL_MS = 25_000;
+
+/**
+ * 宿主 → 代理子进程的指令通道(SDK 只有 prompt/resume,没有中止、插话、换模型):
+ * 子进程里的 amb-control 插件经 bridge 长轮询 /dsh/control/poll 取指令,执行完 POST /dsh/control/result 回报。
+ * 每次拉起子进程换一个通道 id(AMB_CONTROL_TOKEN):旧进程的轮询拿到 stop,发出去没回音的指令随旧进程一起失败。
+ * 鉴权沿用 bridge 令牌;指令和结果里都没有令牌。
+ */
+export class ControlChannel {
+ /** 当前子进程的通道 id;空 = 没有子进程 */
+ id = "";
+ private queue: ControlCommand[] = [];
+ private waiter: ((cmds: ControlCommand[]) => void) | undefined;
+ private pending = new Map<string, { resolve: (v: any) => void; reject: (e: Error) => void; timer: NodeJS.Timeout }>();
+ private lastSeen = 0;
+ private seq = 0;
+ private onSeen = new Set<() => void>();
+
+ /** 新子进程:换通道 id,旧的指令作废 */
+ reset(): string {
+ this.close("代理进程已重启");
+ this.id = crypto.randomBytes(12).toString("hex");
+ return this.id;
+ }
+
+ /** 子进程没了:等着的指令全部失败 */
+ close(reason = "代理进程已退出"): void {
+ this.id = "";
+ this.lastSeen = 0;
+ this.queue = [];
+ const w = this.waiter;
+ this.waiter = undefined;
+ w?.([]);
+ for (const [, p] of this.pending) {
+ clearTimeout(p.timer);
+ p.reject(new Error(reason));
+ }
+ this.pending.clear();
+ }
+
+ /** 插件在线:正挂着一个轮询,或刚来过 */
+ get connected(): boolean {
+ return !!this.id && (!!this.waiter || Date.now() - this.lastSeen < STALE_MS);
+ }
+
+ /** 等插件第一次来取指令(子进程刚起来时) */
+ async ready(ms: number): Promise<boolean> {
+ if (this.connected) return true;
+ return new Promise<boolean>((resolve) => {
+ const done = (v: boolean) => {
+ clearTimeout(t);
+ this.onSeen.delete(seen);
+ resolve(v);
+ };
+ const seen = () => done(true);
+ const t = setTimeout(() => done(this.connected), ms);
+ this.onSeen.add(seen);
+ });
+ }
+
+ private seen(): void {
+ this.lastSeen = Date.now();
+ for (const fn of [...this.onSeen]) fn();
+ }
+
+ /** 插件的长轮询:有指令马上给,没有就最多挂 waitMs;signal = 连接断了 */
+ poll(body: any, signal?: AbortSignal, waitMs = POLL_MS): Promise<{ commands?: ControlCommand[]; stop?: true }> {
+ if (!this.id || String(body?.controlId ?? "") !== this.id) return Promise.resolve({ stop: true });
+ this.seen();
+ if (this.queue.length) return Promise.resolve({ commands: this.queue.splice(0) });
+ // 同一时刻只留一个轮询
+ const old = this.waiter;
+ this.waiter = undefined;
+ old?.([]);
+ return new Promise((resolve) => {
+ const done = (cmds: ControlCommand[]) => {
+ clearTimeout(timer);
+ signal?.removeEventListener("abort", gone);
+ if (this.waiter === done) this.waiter = undefined;
+ if (this.id) this.lastSeen = Date.now();
+ resolve({ commands: cmds });
+ };
+ // 连接断了(子进程退出):不再往这里派指令
+ const gone = () => {
+ if (this.waiter === done) this.waiter = undefined;
+ clearTimeout(timer);
+ resolve({ commands: [] });
+ };
+ const timer = setTimeout(() => done([]), waitMs);
+ signal?.addEventListener("abort", gone, { once: true });
+ this.waiter = done;
+ });
+ }
+
+ /** 插件回报一条指令的结果 */
+ result(body: any): void {
+ if (!this.id || String(body?.controlId ?? "") !== this.id) return;
+ this.seen();
+ const p = this.pending.get(String(body?.id ?? ""));
+ if (!p) return;
+ this.pending.delete(String(body.id));
+ clearTimeout(p.timer);
+ if (body.ok) p.resolve(body.result ?? {});
+ else p.reject(new Error(String(body.error ?? "control command failed").slice(0, 500)));
+ }
+
+ /** 发一条指令,等插件回报;超时或子进程退出时失败 */
+ send<T = Record<string, unknown>>(op: string, args: Record<string, unknown>, timeoutMs: number): Promise<T> {
+ if (!this.id) return Promise.reject(new Error("代理进程没有运行"));
+ const id = `c${++this.seq}`;
+ const cmd: ControlCommand = { ...args, id, op };
+ return new Promise<T>((resolve, reject) => {
+ const timer = setTimeout(() => {
+ this.pending.delete(id);
+ this.queue = this.queue.filter((c) => c !== cmd);
+ reject(new Error(`代理进程没有响应(${op})`));
+ }, timeoutMs);
+ this.pending.set(id, { resolve, reject, timer });
+ const w = this.waiter;
+ this.waiter = undefined;
+ if (w) w([cmd]);
+ else this.queue.push(cmd);
+ });
+ }
+}
diff --git a/src/agent/host.ts b/src/agent/host.ts
index 68bdb0f..f710f4a 100644
--- a/src/agent/host.ts
+++ b/src/agent/host.ts
@@ -6,6 +6,7 @@ import { lineLogger, logger } from "../log.js";
import type { Store } from "../store/db.js";
import { BOT_HEADER, type AgentBridge, type Rejection } from "./bridge.js";
import { ApiFallbacks, shapeRejection, type ApiFallback } from "./api-fallback.js";
+import { ControlChannel } from "./control.js";
import os from "node:os";
import { findAppRoot } from "../magpie/supervisor.js";
import { adaptSkill, mcpPatchLines, resolveComputerUse, type CuResolved } from "./computer-use.js";
@@ -140,12 +141,14 @@ export interface TurnObserver {
/**
* 回合被中止:用户 /stop、长时间没有进展(idle)、超过单回合上限(timeout)。
- * SDK 没有中断单个回合的接口,只能关掉子进程;ms 是触发时的时长(idle、timeout 为阈值)。
+ * 经指令通道只中止这个会话的回合(agent.cancel);宽限期内没停下才重启子进程(restarted)。
+ * ms 是触发时的时长(idle、timeout 为阈值)。
*/
export class TurnStopped extends Error {
constructor(
readonly reason: "user" | "idle" | "timeout",
readonly ms: number,
+ readonly restarted = false,
) {
super(`agent turn stopped (${reason})`);
}
@@ -189,6 +192,8 @@ export interface AgentHostOptions {
apiInfo?: (model: string) => Promise<ModelApiInfo | undefined>;
/** 被上游拒绝过的模型 → 改走 Chat Completions(所有机器人共用,存在数据目录);不给时只在本进程内记 */
apiFallbacks?: ApiFallbacks;
+ /** 代理自己醒来开了一轮(后台命令结束):调用方用 followTurn 跟踪它,把回复发到会话里 */
+ onWake?: (conversationId: string, sessionId: string) => void;
}
export type HostState = "stopped" | "starting" | "running" | "backoff" | "error";
@@ -271,26 +276,133 @@ export function agentApi(endpoints: string[] | undefined, env: NodeJS.ProcessEnv
}
/**
- * 补丁里随接口变化的几行(provider 一级、模型一级)。
- * - Anthropic SDK 自己拼 /v1/messages,baseURL 是 bridge 根地址;另两个是 …/v1。
+ * 一个模型在 DSH 里怎么走:哪个 provider(按接口分)、接口、上下文窗口与最大输出、是否推理。
+ * 同一个代理进程里每个会话用自己的模型(amb-control 插件按会话选 provider/model),不用为换模型重启。
+ */
+export interface ModelRoute {
+ model: string;
+ provider: string;
+ api: AgentApi;
+ /** 这个模型在选中的接口上被拒过、改走 Chat 的回退记录 */
+ fallback?: ApiFallback;
+ limits: { window: number; maxTokens: number };
+ reasoning?: boolean;
+}
+
+/**
+ * 补丁里的 provider:每种接口一个(Anthropic 的 baseURL 是 bridge 根地址,另两个是 …/v1)。Responses 再按推理分两个:
+ * provider 一级的推理强度(medium)遇到不推理的模型会被 pi-ai 拒绝。
+ */
+export function providerFor(api: AgentApi, reasoning?: boolean): string {
+ if (api === "openai-responses") return reasoning === false ? "magpie-responses-plain" : "magpie-responses";
+ if (api === "anthropic-messages") return "magpie-anthropic";
+ return "magpie";
+}
+
+/** llm-pi-ai 的一个 provider(补丁和运行时更新共用这份结构) */
+export interface ProviderProfile {
+ displayName: string;
+ api: AgentApi;
+ baseURL: string;
+ reasoning?: string;
+ apiKeyEnv: string;
+ headers: Record<string, string>;
+ models: Array<{
+ id: string;
+ name: string;
+ contextWindow: number;
+ maxTokens: number;
+ input?: string[];
+ reasoningEfforts?: Record<string, string>;
+ compat?: Record<string, boolean>;
+ }>;
+}
+
+/**
+ * 所有路线的 provider 配置。每个模型都同时列在 Chat Completions(magpie)下:被上游拒过的模型改走 Chat 时不用再注册。
* - Responses:store:false 下推理条目要带 encrypted_content 才能回放,而 DSH 只在指定了推理强度时才要它,
* 所以推理模型(或不知道是不是)声明 low/medium/high、默认 medium;明确不推理的不发推理参数。
* 系统提示用 system 角色(网关转给非 OpenAI 模型时不一定认 developer);工具显式 strict:false。
* - Anthropic、Chat 不声明推理(和以前一样,用模型默认)。
*/
-export function apiPatchLines(api: AgentApi, bridgeUrl: string, reasoning?: boolean): { provider: string[]; model: string[] } {
- const provider = [` api: ${api}`, ` baseURL: ${yamlStr(api === "anthropic-messages" ? bridgeUrl : bridgeUrl + "/v1")}`];
- if (api !== "openai-responses") return { provider, model: [] };
- const think = reasoning !== false;
- return {
- provider: [...provider, ...(think ? [" reasoning: medium"] : [])],
- model: [
- ...(think ? [" reasoningEfforts:", " low: low", " medium: medium", " high: high"] : []),
- " compat:",
- " supportsDeveloperRole: false",
- " supportsStrictMode: true",
- ],
+export function providerProfiles(routes: ModelRoute[], bridgeUrl: string, botId: string): Record<string, ProviderProfile> {
+ const out: Record<string, ProviderProfile> = {};
+ const add = (provider: string, api: AgentApi, r: ModelRoute, reasoning?: boolean) => {
+ const p = (out[provider] ??= {
+ displayName: "anymodel",
+ api,
+ baseURL: api === "anthropic-messages" ? bridgeUrl : bridgeUrl + "/v1",
+ ...(api === "openai-responses" && reasoning !== false ? { reasoning: "medium" } : {}),
+ apiKeyEnv: "AMB_MAGPIE_KEY",
+ // 标明是哪个机器人发的:bridge 据此把上游错误归到这个代理进程(不转给上游)
+ headers: { [BOT_HEADER]: botId },
+ models: [],
+ });
+ if (p.models.some((m) => m.id === r.model)) return;
+ p.models.push({
+ id: r.model,
+ name: r.model.split("/").pop() || r.model,
+ contextWindow: r.limits.window,
+ maxTokens: r.limits.maxTokens,
+ // 默认只收文字;能看图的模型声明图片输入,用户发来的图片才会作为图片交给它
+ ...(acceptsImages(r.model) ? { input: ["text", "image"] } : {}),
+ ...(api === "openai-responses"
+ ? {
+ ...(reasoning !== false ? { reasoningEfforts: { low: "low", medium: "medium", high: "high" } } : {}),
+ compat: { supportsDeveloperRole: false, supportsStrictMode: true },
+ }
+ : {}),
+ });
};
+ for (const r of routes) {
+ if (r.api !== "openai-completions") add(r.provider, r.api, r, r.reasoning);
+ add("magpie", "openai-completions", r);
+ }
+ return out;
+}
+
+/** provider 配置写成补丁里的 YAML 行(字符串都按 JSON 写,是合法的 YAML 标量) */
+export function providerPatchLines(profiles: Record<string, ProviderProfile>): string[] {
+ const lines = ["- id: llm-pi-ai", " config:", " providers:"];
+ for (const [name, p] of Object.entries(profiles)) {
+ lines.push(` ${name}:`, ` displayName: ${p.displayName}`, ` api: ${p.api}`, ` baseURL: ${yamlStr(p.baseURL)}`);
+ if (p.reasoning) lines.push(` reasoning: ${p.reasoning}`);
+ lines.push(` apiKeyEnv: ${p.apiKeyEnv}`, " headers:", ...Object.entries(p.headers).map(([k, v]) => ` ${k}: ${yamlStr(v)}`), " models:");
+ for (const m of p.models) {
+ lines.push(` - id: ${yamlStr(m.id)}`, ` name: ${yamlStr(m.name)}`, ` contextWindow: ${m.contextWindow}`, ` maxTokens: ${m.maxTokens}`);
+ if (m.input) lines.push(` input: [${m.input.join(", ")}]`);
+ if (m.reasoningEfforts) lines.push(" reasoningEfforts:", ...Object.entries(m.reasoningEfforts).map(([k, v]) => ` ${k}: ${v}`));
+ if (m.compat) lines.push(" compat:", ...Object.entries(m.compat).map(([k, v]) => ` ${k}: ${v}`));
+ }
+ }
+ return lines;
+}
+
+/**
+ * 长命令转后台:前台命令等这么久还没完就转成后台任务(不杀掉),代理收到任务 id,结束时在会话里收到结果。
+ * 模型可以要求更长的前台等待,但不超过上限;job_output 的等待同样有上限。这样一步最多卡一两分钟,
+ * 插进来的消息、/stop 都能及时生效。Windows 上 PowerShell 5.1 一条命令就要二三十秒,放宽一些。
+ */
+export const SHELL_LIMITS = { bashMs: 30_000, bashMaxMs: 60_000, pwshMs: 60_000, pwshMaxMs: 120_000, waitMs: 30_000, waitMaxMs: 60_000 };
+
+/** 命令与后台任务的补丁行(pwsh-sandbox 一行里还要带上检测到的 PowerShell:补丁按行整体替换 config) */
+export function shellPatchLines(pwshPath: string | undefined, l = SHELL_LIMITS): string[] {
+ return [
+ "- id: bash-sandbox",
+ " config:",
+ ` timeoutMs: ${l.bashMs}`,
+ ` maxTimeoutMs: ${l.bashMaxMs}`,
+ "- id: pwsh-sandbox",
+ " config:",
+ ` timeoutMs: ${l.pwshMs}`,
+ ` maxTimeoutMs: ${l.pwshMaxMs}`,
+ ...pwshPatchLines(pwshPath).slice(2),
+ // 后台任务结束时 DSH 默认唤醒空闲的代理开一轮(completionDelivery: wakeup),宿主跟着这一轮把回复发给用户(见 wake)
+ "- id: tool-jobs",
+ " config:",
+ ` waitTimeoutMs: ${l.waitMs}`,
+ ` maxWaitTimeoutMs: ${l.waitMaxMs}`,
+ ];
}
/** 随消息交给模型的图片(DSH SDK 的内联图片块:base64 + 格式,进会话前存入 DSH 的附件库) */
@@ -306,7 +418,7 @@ export interface TurnInput {
observer?: TurnObserver;
/** 只在模型能看图时传(acceptsImages),否则模型只拿到文字里的工作区路径 */
images?: TurnImage[];
- /** 中止这个回合(reason 为 TurnStopped):会关掉子进程,同一机器人其它进行中的回合也会失败 */
+ /** 中止这个回合(reason 为 TurnStopped):只中止这个会话的回合;宽限期内没停下才重启子进程 */
signal?: AbortSignal;
}
@@ -358,6 +470,78 @@ export function callDetail(name: string, argsJson: string): string {
return s === "{}" ? "" : s.slice(0, 300);
}
+/** 一个进行中的回合(插话、中止用) */
+interface HostTurn {
+ sessionId: string;
+ /** 这一轮的模型(插话带的图片只给能看图的模型) */
+ model: string;
+ /** 代理收下了这条消息(收到第一条会话事件):此后插话、中止才落得到这一轮上 */
+ accepted: Promise<void>;
+ /** run 已经结束(代理空闲了),正在收尾 */
+ finishing: boolean;
+ stopping: boolean;
+ /** 跟踪的是代理自己醒来的一轮(不是宿主发的消息) */
+ wake?: boolean;
+ /** 宿主发起的这一轮已经空闲过一次:之后再忙起来就是代理自己醒了 */
+ idleSeen?: boolean;
+}
+
+/** 一轮的结果 */
+export interface TurnResult {
+ text: string;
+ sessionId: string;
+ files: string[];
+ usage: TurnUsage;
+}
+
+/** 代理自己醒来的一轮:先缓存通知,有人跟踪时交给它;代理空闲时结束 */
+class Wake {
+ buffer: SdkNotification[] = [];
+ listener: ((n: SdkNotification) => void) | undefined;
+ ended = false;
+ /** 已经有人在跟踪 */
+ taken = false;
+ readonly done: Promise<void>;
+ private settle!: (err?: Error) => void;
+ constructor(
+ readonly conversationId: string,
+ readonly sessionId: string,
+ ) {
+ this.done = new Promise<void>((resolve, reject) => (this.settle = (err) => (err ? reject(err) : resolve())));
+ this.done.catch(() => undefined);
+ }
+ push(n: SdkNotification): void {
+ if (this.listener) this.listener(n);
+ else if (this.buffer.length < 5000) this.buffer.push(n);
+ }
+ attach(fn: (n: SdkNotification) => void): void {
+ this.listener = fn;
+ for (const n of this.buffer.splice(0)) fn(n);
+ }
+ end(err?: Error): void {
+ if (this.ended) return;
+ this.ended = true;
+ this.settle(err);
+ }
+}
+
+/** 中止信号的原因(manager 用 TurnStopped 中止;别的原因按用户中止算) */
+function stopReason(sig: AbortSignal): TurnStopped {
+ return sig.reason instanceof TurnStopped ? sig.reason : new TurnStopped("user", 0);
+}
+
+/** 一条 assistant/message 是不是完整的回答(不带工具调用、没被打断):是就返回它的文字 */
+export function answerText(data: any): string {
+ if (data?.interrupted) return "";
+ const content: any[] = Array.isArray(data?.message?.content) ? data.message.content : [];
+ if (content.some((b) => b?.type === "tool-call")) return "";
+ return content
+ .filter((b) => b?.type === "text")
+ .map((b) => String(b.text ?? ""))
+ .join("")
+ .trim();
+}
+
/**
* 每个 bot 一个 DSH 子进程(懒启动):DSH_HOME = <data>/dsh/<botId>,工作区 = <data>/workspaces/<botId>,
* workspace-write 沙箱;模型请求经本地 bridge 代理到 magpie(子进程拿不到真实密钥)。
@@ -367,14 +551,29 @@ export class AgentHost {
state: HostState = "stopped";
lastError = "";
restarts = 0;
+ /** 最近一轮用的模型(子进程启动时是启动用的模型) */
model = "";
- /** 当前子进程跟模型说话用的接口 */
+ /** 最近一轮跟模型说话用的接口 */
api: AgentApi | "" = "";
- /** 当前子进程因为回退才走 Chat Completions:回退记录(状态里显示原因) */
+ /** 最近一轮因为回退才走 Chat Completions:回退记录(状态里显示原因) */
fallback: ApiFallback | undefined;
private ownFallbacks: ApiFallbacks | undefined;
- /** 当前子进程补丁里写的上下文窗口与最大输出 */
- private limits: { window: number; maxTokens: number } | undefined;
+ /** 宿主 → 子进程的指令通道(amb-control 插件) */
+ readonly control = new ControlChannel();
+ /** 模型 → 路线(当前子进程内有效,换进程时重新查模型资料) */
+ private routes = new Map<string, Promise<ModelRoute>>();
+ /** 用过的模型:重新拉起子进程时都写进补丁 */
+ private known = new Set<string>();
+ /** 当前子进程已经能用的 provider/model(补丁里的,加上运行中补上的) */
+ private childRoutes = new Map<string, ModelRoute>();
+ /** 当前子进程启动时的默认路线(会话没选模型时用它) */
+ private initRoute: ModelRoute | undefined;
+ /** 会话 → 进行中的回合(插话、中止用) */
+ private turns = new Map<string, HostTurn>();
+ /** DSH session → 代理自己醒来的一轮 */
+ private wakes = new Map<string, Wake>();
+ /** 正在把消息交给代理的会话(还没登记成回合) */
+ private claimed = new Set<string>();
private harness: SdkHarness | undefined;
private starting: Promise<SdkHarness> | undefined;
private opened = new Set<string>();
@@ -454,36 +653,22 @@ export class AgentHost {
});
}
- private writePatch(model: string, apps: CuaApp[] = [], pwshPath?: string, lim = modelLimits(model), api: AgentApi = "openai-completions", reasoning?: boolean): string {
- this.limits = lim;
- const wire = apiPatchLines(api, this.o.bridge.url, reasoning);
+ /**
+ * routes:这个代理进程要能用的所有模型(每个接口一个 provider,见 providerProfiles);第一条是启动时的默认路线。
+ * 之后会话换到没列出的模型时由 amb-control 插件在运行中补上(不重启)。
+ */
+ private writePatch(routes: ModelRoute[] | string, apps: CuaApp[] = [], pwshPath?: string): string {
+ const list = typeof routes === "string" ? [this.fallbackRoute(routes)] : routes;
const plugin = path.join(this.o.runtimeDir, "plugins", "amb-bridge.mjs");
const memory = path.join(this.o.runtimeDir, "plugins", "amb-memory.mjs");
this.writeMediaSkill();
const cuOn = !!this.computerUseActive();
const text = [
"# 由 anymodel bot 每次启动生成;请勿手改。",
- "- id: llm-pi-ai",
- " config:",
- " providers:",
- " magpie:",
- " displayName: anymodel",
- // 接口按模型选(agentApi):Responses / Anthropic Messages / Chat Completions
- ...wire.provider,
- " apiKeyEnv: AMB_MAGPIE_KEY",
- // 标明是哪个机器人发的:bridge 据此把上游错误归到这个代理进程(不转给上游)
- " headers:",
- ` ${BOT_HEADER}: ${yamlStr(this.o.botId)}`,
- " models:",
- ` - id: ${yamlStr(model)}`,
- ` name: ${yamlStr(model.split("/").pop() || model)}`,
- ` contextWindow: ${lim.window}`,
- ` maxTokens: ${lim.maxTokens}`,
- // 默认只收文字;能看图的模型声明图片输入,用户发来的图片才会作为图片交给它
- ...(acceptsImages(model) ? [" input: [text, image]"] : []),
- ...wire.model,
- // Windows:命令用检测到的 PowerShell 7(DSH 自己只看 Program Files\PowerShell\7 和 PATH,找不到就退回 5.1)
- ...pwshPatchLines(pwshPath),
+ // 接口按模型选(agentApi):Responses / Anthropic Messages / Chat Completions,各一个 provider
+ ...providerPatchLines(providerProfiles(list, this.o.bridge.url, this.o.botId)),
+ // 长命令转后台;Windows:命令用检测到的 PowerShell 7(DSH 自己只看 Program Files\PowerShell\7 和 PATH,找不到就退回 5.1)
+ ...shellPatchLines(pwshPath),
// 人设:个人助手(替换 DSH 的 coding agent 人设),不要 "powered by DeepSeek Harness" 那句身份
"- id: system-prompt",
" config:",
@@ -510,6 +695,9 @@ export class AgentHost {
" name: '@deepseek-ai/dsh-tool-ask-user'",
" - id: amb-present",
" name: '@deepseek-ai/dsh-tool-present'",
+ // 宿主 → 代理进程的指令通道:按会话中止、插话、按会话选模型;长命令转后台后等它的结果
+ " - id: amb-control",
+ ` name: ${yamlStr(path.join(this.o.runtimeDir, "plugins", "amb-control.mjs"))}`,
...this.computerUseLines(apps),
...this.fullAccessLines(),
"",
@@ -687,6 +875,7 @@ exec "$REAL" "$@"
AMB_BRIDGE_TOKEN: this.o.bridge.token,
AMB_BOT_ID: this.o.botId,
AMB_PROMPT_FILE: this.promptFile,
+ AMB_CONTROL_TOKEN: this.control.id,
};
// 时区:本进程设了 TZ 才传下去;没设就不设,子进程用系统时区(各平台都是,Windows 上 TZ 也只认 POSIX 写法)
if (process.env.TZ) env.TZ = process.env.TZ;
@@ -769,6 +958,10 @@ exec "$REAL" "$@"
}
}
+ /**
+ * 确保子进程在跑(懒启动)。换模型不再重启:每个会话按自己的模型走(见 useRoute);
+ * 只有设置或 PowerShell 变了、而且没有回合在跑时才重启。
+ */
async ensure(model: string): Promise<SdkHarness> {
if (this.stopped) throw new Error("agent host stopped");
this.syncPrompt();
@@ -776,14 +969,6 @@ exec "$REAL" "$@"
if (!this.harness && !this.starting && this.state === "error" && Date.now() < this.retryAfter) {
throw new Error(`代理进程启动失败,${Math.ceil((this.retryAfter - Date.now()) / 60_000)} 分钟后再试(或在 设置 → 故障排查 里重启代理进程):${this.lastError}`);
}
- if (this.harness && this.model !== model && this.active === 0) {
- log.info("model changed, restarting dsh", { bot: this.o.botId, model });
- await this.closeHarness();
- }
- if (this.harness && this.api && this.api !== "openai-completions" && this.fallbacks.get(this.model) && this.active === 0) {
- log.info("model was rejected on its API before, restarting dsh on Chat Completions", { bot: this.o.botId, model: this.model, api: this.api });
- await this.closeHarness();
- }
if (this.harness && this.sig !== this.settingsSig() && this.active === 0) {
log.info("bot settings or PowerShell changed, restarting dsh", { bot: this.o.botId });
await this.closeHarness();
@@ -793,6 +978,34 @@ exec "$REAL" "$@"
return this.starting;
}
+ /** 没有模型资料时的路线(Chat Completions、内置兜底的窗口) */
+ private fallbackRoute(model: string): ModelRoute {
+ return { model, provider: "magpie", api: "openai-completions", limits: modelLimits(model) };
+ }
+
+ /**
+ * 模型的路线:按 Modelflare 模型资料选接口(agentApi)、定上下文窗口;在该接口上被拒过(24 小时内)就走 Chat。
+ * 模型资料在一个子进程的生命期内只查一次(和以前一样:接口在拉起进程时选定)。
+ */
+ async routeFor(model: string): Promise<ModelRoute> {
+ let p = this.routes.get(model);
+ if (!p) {
+ const ask = <T>(q: Promise<T | undefined> | undefined) => withTimeoutOr(q ?? Promise.resolve(undefined), 3000, undefined).catch(() => undefined);
+ p = Promise.all([ask(this.o.contextWindow?.(model)), ask(this.o.apiInfo?.(model))]).then(([known, info]) => {
+ const api = agentApi(info?.endpoints);
+ return { model, provider: providerFor(api, info?.reasoning), api, limits: modelLimits(model, known), reasoning: info?.reasoning };
+ });
+ this.routes.set(model, p);
+ }
+ const r = await p;
+ const fb = r.api !== "openai-completions" ? this.fallbacks.get(model) : undefined;
+ return fb ? { ...r, provider: "magpie", api: "openai-completions", fallback: fb } : r;
+ }
+
+ private routeKey(r: { provider: string; model: string }): string {
+ return `${r.provider}|${r.model}`;
+ }
+
/**
* 设置或 PowerShell 变了(比如内置 PowerShell 7 刚装好、或者坏了退回)而且空闲:在后台重启子进程,
* 下一条消息不用再等启动。正在跑的回合不打断,等下一条消息时由 ensure() 换。
@@ -854,19 +1067,19 @@ exec "$REAL" "$@"
const cu = this.computerUseActive();
const apps = cu && cu.backend === "cua" ? await discoverAppsCached() : [];
const pwshPath = await pwshProbe.dshPath();
- const ask = <T>(p: Promise<T | undefined> | undefined) => withTimeoutOr(p ?? Promise.resolve(undefined), 3000, undefined).catch(() => undefined);
- const [known, info] = await Promise.all([ask(this.o.contextWindow?.(model)), ask(this.o.apiInfo?.(model))]);
- const chosen = agentApi(info?.endpoints);
- // 这个模型在该接口上被拒过(24 小时内):直接走 Chat Completions
- const fb = chosen !== "openai-completions" ? this.fallbacks.get(model) : undefined;
- const api: AgentApi = fb ? "openai-completions" : chosen;
- this.fallback = fb;
- const patch = this.writePatch(model, apps, pwshPath, modelLimits(model, known), api, info?.reasoning);
+ // 模型资料在新进程里重新查;用过的模型都写进补丁(会话各用各的模型),启动用的模型排第一
+ this.routes.clear();
+ this.known.add(model);
+ const first = await this.routeFor(model);
+ const routes = [first, ...(await Promise.all([...this.known].filter((m) => m !== model).map((m) => this.routeFor(m))))];
+ const api = first.api;
+ const patch = this.writePatch(routes, apps, pwshPath);
this.syncPrompt(true);
this.pwshPath = pwshPath;
this.sig = this.settingsSig(pwshPath);
const timeoutMs = this.initTimeoutMs();
- this.stage("patch written", t0, { api, computerUse: cu ? cu.backend : "off", apps: apps.length, initTimeoutMs: timeoutMs, ...(process.platform === "win32" ? { pwsh: pwshPath ?? "dsh default (Windows PowerShell 5.1)" } : {}) });
+ this.control.reset();
+ this.stage("patch written", t0, { api, models: routes.length, computerUse: cu ? cu.backend : "off", apps: apps.length, initTimeoutMs: timeoutMs, ...(process.platform === "win32" ? { pwsh: pwshPath ?? "dsh default (Windows PowerShell 5.1)" } : {}) });
h = new sdk.DeepSeekHarness({
profile: "sdk",
patches: [patch],
@@ -874,7 +1087,7 @@ exec "$REAL" "$@"
env: this.env(),
cwd: this.workspace,
processCwd: this.workspace,
- provider: "magpie",
+ provider: first.provider,
model,
initializeTimeoutMs: timeoutMs,
shutdownTimeoutMs: 3000,
@@ -905,25 +1118,42 @@ exec "$REAL" "$@"
const child = h.client?.child;
if (child?.pid) fs.writeFileSync(this.pidFile, String(child.pid));
const harness = h;
+ // 所有通知先同步经过 tap(filter 回调按到达顺序调用;返回 false 不排队):认出代理自己醒来的一轮
+ h.client?.subscribe?.((n: SdkNotification) => {
+ if (this.harness === harness) {
+ try {
+ this.tap(n);
+ } catch (e) {
+ log.warn("notification tap failed", { message: (e as Error).message });
+ }
+ }
+ return false;
+ });
child?.once?.("exit", (code: number | null, sig: string | null) => {
if (this.harness !== harness) return;
this.harness = undefined;
this.opened.clear();
+ this.control.close();
+ this.endWakes();
if (this.stopped) return;
this.onFailure(new Error(`dsh exited (code=${code} signal=${sig})`));
});
this.harness = h;
+ this.childRoutes = new Map(routes.flatMap((r) => [[this.routeKey(r), r] as const, [this.routeKey({ provider: "magpie", model: r.model }), r] as const]));
+ this.initRoute = first;
this.model = model;
this.api = api;
+ this.fallback = first.fallback;
this.state = "running";
this.initStage = "";
this.lastError = "";
this.initFailures = 0;
this.startedAt = Date.now();
- log.info("dsh started", { bot: this.o.botId, pid: child?.pid, model, api, ms: Date.now() - t0, initMs });
+ log.info("dsh started", { bot: this.o.botId, pid: child?.pid, model, api, models: routes.length, ms: Date.now() - t0, initMs });
return h;
} catch (e) {
clearInterval(watchdog);
+ this.control.close("代理进程启动失败");
this.emit({ type: "failed", message: (e as Error).message });
this.initFailures++;
this.onFailure(e as Error);
@@ -958,10 +1188,18 @@ exec "$REAL" "$@"
log.warn("dsh failure, will restart on demand", { bot: this.o.botId, delayMs: delay, message: this.lastError });
}
+ /** 子进程没了:醒着的那几轮随之失败 */
+ private endWakes(): void {
+ for (const w of this.wakes.values()) w.end(new Error("代理进程已退出"));
+ this.wakes.clear();
+ }
+
private async closeHarness(): Promise<void> {
const h = this.harness;
this.harness = undefined;
this.opened.clear();
+ this.control.close("代理进程已重启");
+ this.endWakes();
if (!h) return;
try {
await h.close();
@@ -1011,22 +1249,22 @@ exec "$REAL" "$@"
/**
* 跑一个回合。Responses / Anthropic Messages 上的请求被上游拒绝(请求形状不被接受,不是限流、余额、故障)时:
- * 记下这个模型改走 Chat Completions;这一轮还没调用过工具的话,把代理进程换到 Chat 上重做一遍(只重做一次)。
+ * 记下这个模型改走 Chat Completions;这一轮还没调用过工具的话,把这个会话换到 Chat 上重做一遍(只重做一次,不重启进程)。
*/
- async runTurn(input: TurnInput): Promise<{ text: string; sessionId: string; files: string[]; usage: TurnUsage }> {
+ async runTurn(input: TurnInput): Promise<TurnResult> {
try {
return await this.runOnce(input);
} catch (e) {
if (!(e instanceof ApiRejected) || !e.retryable || input.signal?.aborted) throw e;
log.warn("agent turn rejected on its API; retrying it once over Chat Completions", { bot: this.o.botId, model: input.model, api: e.api, reason: e.reason });
input.observer?.onEvent?.("api/fallback", { from: e.api, to: "openai-completions", reason: e.reason }, Date.now());
- // ensure() 看到回退记录,空闲时把子进程换到 Chat 上。用户这条消息(连同图片)在被拒之前已经存进会话,
+ // routeFor() 看到回退记录,这个会话改选 Chat 的路线。用户这条消息(连同图片)在被拒之前已经存进会话,
// 不再发一遍,只请模型处理它
return await this.runOnce({ ...input, text: RETRY_NOTE, images: undefined });
}
}
- private async runOnce(input: TurnInput): Promise<{ text: string; sessionId: string; files: string[]; usage: TurnUsage }> {
+ private async runOnce(input: TurnInput): Promise<TurnResult> {
this.entering++;
try {
return await this.runEntered(input);
@@ -1037,102 +1275,342 @@ exec "$REAL" "$@"
}
}
- private async runEntered(input: TurnInput): Promise<{ text: string; sessionId: string; files: string[]; usage: TurnUsage }> {
- const h = await this.ensure(input.model);
- const s = await this.sessionFor(h, input.conversationId);
- // 在等进程 / 会话时就被中止了:消息还没交出去,不用重启子进程
- if (input.signal?.aborted) throw input.signal.reason instanceof TurnStopped ? input.signal.reason : new TurnStopped("user", 0);
+ /**
+ * 让这个会话用这条路线:子进程还不认识这个模型就经插件补进 provider 配置,再按会话选定。
+ * 插件不通、补不进去时返回 false(只有启动时的默认路线不用选)。
+ */
+ private async useRoute(sessionId: string, route: ModelRoute): Promise<boolean> {
+ const key = this.routeKey(route);
+ const isInit = !!this.initRoute && this.routeKey(this.initRoute) === key;
+ // 插件刚起来可能还没来取指令:要靠它时等一会儿;启动时的默认路线不靠它也能用
+ const ready = this.control.connected || (!(isInit && this.childRoutes.has(key)) && (await this.control.ready(5000)));
+ if (!this.childRoutes.has(key)) {
+ if (!ready) return false;
+ const list = [...new Set(this.childRoutes.values()), route];
+ try {
+ await this.control.send("providers", { providers: providerProfiles(list, this.o.bridge.url, this.o.botId) }, 20_000);
+ this.childRoutes.set(key, route);
+ this.childRoutes.set(this.routeKey({ provider: "magpie", model: route.model }), route);
+ log.info("model added to the running dsh", { bot: this.o.botId, model: route.model, provider: route.provider });
+ } catch (e) {
+ log.warn("could not add a model to the running dsh", { bot: this.o.botId, model: route.model, message: (e as Error).message.slice(0, 300) });
+ return false;
+ }
+ }
+ if (ready) {
+ try {
+ await this.control.send("select", { sessionId, provider: route.provider, model: route.model }, 10_000);
+ return true;
+ } catch (e) {
+ log.warn("model selection failed", { bot: this.o.botId, model: route.model, message: (e as Error).message.slice(0, 200) });
+ }
+ }
+ return isInit;
+ }
+
+ private async runEntered(input: TurnInput): Promise<TurnResult> {
+ const conv = input.conversationId;
+ // 这个会话正在交给代理:期间代理自己醒来(后台命令结束)不另开一轮跟踪,这一轮的 run 会收下它的事件
+ this.claimed.add(conv);
+ try {
+ return await this.runClaimed(input);
+ } finally {
+ this.claimed.delete(conv);
+ }
+ }
+
+ private async runClaimed(input: TurnInput): Promise<TurnResult> {
+ const route = await this.routeFor(input.model);
+ let s = await this.sessionFor(await this.ensure(input.model), input.conversationId);
+ // 在等进程 / 会话时就被中止了:消息还没交出去,不用动子进程
+ if (input.signal?.aborted) throw stopReason(input.signal);
+ if (!(await this.useRoute(s.id, route))) {
+ // 指令通道不通、或补不进这个模型:没有别的回合在跑时,带上这个模型重启子进程;忙的时候不能让这个会话悄悄用错模型
+ if (this.active > 0) throw new Error(`暂时无法切换到模型 ${input.model}(代理进程正忙,没能在运行中加上这个模型),请稍后再试`);
+ log.warn("restarting dsh to use a model it could not add at runtime", { bot: this.o.botId, model: input.model });
+ this.known.add(input.model);
+ await this.closeHarness();
+ this.state = "stopped";
+ s = await this.sessionFor(await this.ensure(input.model), input.conversationId);
+ if (!(await this.useRoute(s.id, route))) throw new Error(`代理进程无法使用模型 ${input.model}`);
+ }
+ // 代理自己醒着的一轮(后台命令结束)还没完:等它完了再发这条(它的回复由跟踪它的那一轮发出)
+ const woke = this.wakes.get(s.id);
+ if (woke && !woke.ended) await withTimeoutOr(woke.done.catch(() => undefined), this.o.turnTimeoutMs ?? 30 * 60_000, undefined);
+ if (input.signal?.aborted) throw stopReason(input.signal);
this.active++;
- let timer: NodeJS.Timeout | undefined;
- let turnError: string | undefined;
- const presented: string[] = [];
- const lim = input.model === this.model && this.limits ? this.limits : modelLimits(input.model);
- const usage: TurnUsage = { tokens: 0, window: lim.window, maxTokens: lim.maxTokens };
+ this.known.add(input.model);
+ this.model = input.model;
+ this.api = route.api;
+ this.fallback = route.fallback;
+ let accept!: () => void;
+ const turn: HostTurn = { sessionId: s.id, model: input.model, accepted: new Promise<void>((r) => (accept = r)), finishing: false, stopping: false };
+ this.turns.set(input.conversationId, turn);
+ this.claimed.delete(input.conversationId);
const images = this.imageInput ? (input.images ?? []) : [];
- let unlisten = () => {};
- // 这一轮:用的接口、调用过几次工具、上游拒绝过本机器人的哪些模型请求
- const api = this.api || "openai-completions";
- let toolCalls = 0;
+ // 这一轮:用的接口、上游拒绝过本机器人的哪些模型请求
+ const api = route.api;
+ const tr = this.tracker(s.id, route, input.observer, accept);
const rejections: Rejection[] = [];
const offRejection =
this.o.bridge.onRejection?.((r) => {
if (r.bot === this.o.botId && /\/v1\/(responses|messages)$/.test(r.path)) rejections.push(r);
}) ?? (() => {});
try {
- const timeoutMs = this.o.turnTimeoutMs ?? 30 * 60_000;
const prompt = images.length ? [{ type: "text", text: input.text }, ...images.map((i) => ({ type: "image", data: i.data, mimeType: i.mimeType }))] : input.text;
input.observer?.onStart?.();
- const run = s.run(prompt, {
- onNotification: (n) => {
- if (process.env.AMB_DSH_TRACE) fs.appendFileSync(process.env.AMB_DSH_TRACE, JSON.stringify(n).slice(0, 2000) + "\n");
- if (n.method === "subagent.started" || n.method === "subagent.finished") return input.observer?.onEvent?.(n.method.replace(".", "/"), n.params, Date.now());
- if (n.method !== "session.event") return;
- // 子代理会话的事件(SDK 只送来本会话和它的子代理的通知):不进本回合的用量和步骤,只说明子代理还在干活
- if (n.params?.sessionId !== s.id) return input.observer?.onEvent?.("subagent/event", { sessionId: n.params?.sessionId, event: n.params?.event }, Number(n.params?.event?.time) || Date.now());
- const ev = n.params.event;
- const data = ev?.data ?? {};
- if (ev?.type === "tool/call") {
- toolCalls++;
- this.calls.set(String(data.callId), { name: String(data.name), args: String(data.arguments ?? ""), at: Date.now() });
- if (this.calls.size > 500) this.calls.delete(this.calls.keys().next().value!);
- if (data.name === "present") presented.push(...presentPaths(String(data.arguments ?? "")));
- input.observer?.onProgress?.(`🔧 ${summarizeCall(String(data.name), String(data.arguments ?? "")).split(this.workspace + "/").join("")}`);
- }
- if (ev?.type === "tool/result") {
- // Windows:PowerShell 本身拉不起来(被杀毒软件删了、被策略拦了…)时退回下一个,不让代理一直用一个起不来的 shell(其他平台上探测器不管)
- const msg = data?.message ?? {};
- const call = this.calls.get(String(msg.toolCallId ?? ""));
- if (call && /^(pwsh|powershell)$/i.test(call.name)) pwshProbe.observeToolResult(this.pwshPath, contentText(msg.content));
- }
- if (ev?.type === "request/context" && Number(data?.contextWindow) > 0) usage.window = Number(data.contextWindow);
- if (ev?.type === "assistant/message" && data?.usage) {
- const u = data.usage;
- const t = Number(u.totalTokens) || (Number(u.inputTokens) || 0) + (Number(u.outputTokens) || 0);
- if (t > 0) usage.tokens = t;
- }
- if (ev?.type === "turn/end" && data?.reason?.kind === "error") turnError = String(data.reason.error?.message ?? "unknown error");
- input.observer?.onEvent?.(String(ev?.type), data, Number(ev?.time) || Date.now());
- },
- });
- // 中止后子进程被关掉,run 随后以"runtime closed"失败,已经没人等它
- run.catch(() => undefined);
- const stopped = new Promise<never>((_, rej) => {
- timer = setTimeout(() => rej(new TurnStopped("timeout", timeoutMs)), timeoutMs);
- const sig = input.signal;
- if (!sig) return;
- const onAbort = () => rej(sig.reason instanceof TurnStopped ? sig.reason : new TurnStopped("user", 0));
- if (sig.aborted) return onAbort();
- sig.addEventListener("abort", onAbort, { once: true });
- unlisten = () => sig.removeEventListener("abort", onAbort);
- });
- const r = await Promise.race([run, stopped]);
+ const run = s.run(prompt, { onNotification: tr.onNotification });
+ const r = await this.awaitTurn(turn, run, input.signal);
this.failures = 0;
+ const { turnError, toolCalls, presented, answers, usage } = tr.st;
const rejected = turnError !== undefined && api !== "openai-completions" ? shapeRejection(rejections, turnError) : undefined;
if (rejected) this.rememberFallback(input.model, api, rejected);
if (!r.finalResponse && turnError) {
// 已经调用过工具(命令可能已经执行)就不重做,照常报错;下一轮起走 Chat
- if (rejected) throw new ApiRejected(turnError, api, rejected, toolCalls === 0 && presented.length === 0 && this.active === 1);
+ if (rejected) throw new ApiRejected(turnError, api, rejected, toolCalls === 0 && presented.length === 0);
throw new Error(turnError);
}
- return { text: r.finalResponse, sessionId: s.id, files: deliverables(this.workspace, presented), usage };
+ return { text: answers.length > 1 ? answers.join("\n\n") : r.finalResponse, sessionId: s.id, files: deliverables(this.workspace, presented), usage };
} catch (e) {
if (e instanceof TurnStopped) {
- log.warn("agent turn stopped, restarting dsh", { bot: this.o.botId, reason: e.reason, ms: e.ms, otherTurns: this.active - 1 });
- await this.closeHarness();
- this.state = "stopped";
+ log.warn("agent turn stopped", { bot: this.o.botId, reason: e.reason, ms: e.ms, restarted: e.restarted, otherTurns: this.active - 1 });
} else if (images.length && this.state !== "running") {
this.imageInput = false;
log.warn("dsh died on a turn with images; sending images by path only until the app restarts", { bot: this.o.botId, message: (e as Error).message.slice(0, 200) });
}
throw e;
} finally {
- clearTimeout(timer);
- unlisten();
+ turn.finishing = true;
offRejection();
+ if (this.turns.get(input.conversationId) === turn) this.turns.delete(input.conversationId);
this.active--;
}
}
+ /**
+ * 一轮的事件处理:工具调用(审批提示用)、交付文件、用量、模型的完整回答、出错原因,再转给观察者(活动记录、看门狗)。
+ * accept:第一条本会话事件到达时调用(代理收下了消息)。
+ */
+ private tracker(sessionId: string, route: ModelRoute, observer: TurnObserver | undefined, accept?: () => void) {
+ const st = {
+ toolCalls: 0,
+ presented: [] as string[],
+ /** 这一轮里模型的每个完整回答(不带工具调用的回复):插进来的消息让回合多答了一次时,几次回答都交给用户 */
+ answers: [] as string[],
+ usage: { tokens: 0, window: route.limits.window, maxTokens: route.limits.maxTokens } as TurnUsage,
+ turnError: undefined as string | undefined,
+ };
+ const onNotification = (n: SdkNotification) => {
+ if (process.env.AMB_DSH_TRACE) fs.appendFileSync(process.env.AMB_DSH_TRACE, JSON.stringify(n).slice(0, 2000) + "\n");
+ if (n.method === "subagent.started" || n.method === "subagent.finished") return observer?.onEvent?.(n.method.replace(".", "/"), n.params, Date.now());
+ if (n.method !== "session.event") return;
+ // 子代理会话的事件(只送来本会话和它的子代理的通知):不进本回合的用量和步骤,只说明子代理还在干活
+ if (n.params?.sessionId !== sessionId) return observer?.onEvent?.("subagent/event", { sessionId: n.params?.sessionId, event: n.params?.event }, Number(n.params?.event?.time) || Date.now());
+ // 第一条通知就是代理收下这条消息的回执:此后才能插话、中止
+ accept?.();
+ const ev = n.params.event;
+ const data = ev?.data ?? {};
+ if (ev?.type === "tool/call") {
+ st.toolCalls++;
+ this.calls.set(String(data.callId), { name: String(data.name), args: String(data.arguments ?? ""), at: Date.now() });
+ if (this.calls.size > 500) this.calls.delete(this.calls.keys().next().value!);
+ if (data.name === "present") st.presented.push(...presentPaths(String(data.arguments ?? "")));
+ observer?.onProgress?.(`🔧 ${summarizeCall(String(data.name), String(data.arguments ?? "")).split(this.workspace + "/").join("")}`);
+ }
+ if (ev?.type === "tool/result") {
+ // Windows:PowerShell 本身拉不起来(被杀毒软件删了、被策略拦了…)时退回下一个,不让代理一直用一个起不来的 shell(其他平台上探测器不管)
+ const msg = data?.message ?? {};
+ const call = this.calls.get(String(msg.toolCallId ?? ""));
+ if (call && /^(pwsh|powershell)$/i.test(call.name)) pwshProbe.observeToolResult(this.pwshPath, contentText(msg.content));
+ }
+ if (ev?.type === "request/context" && Number(data?.contextWindow) > 0) st.usage.window = Number(data.contextWindow);
+ if (ev?.type === "assistant/message") {
+ if (data?.usage) {
+ const u = data.usage;
+ const t = Number(u.totalTokens) || (Number(u.inputTokens) || 0) + (Number(u.outputTokens) || 0);
+ if (t > 0) st.usage.tokens = t;
+ }
+ const answer = answerText(data);
+ if (answer) st.answers.push(answer);
+ }
+ if (ev?.type === "turn/end" && data?.reason?.kind === "error") st.turnError = String(data.reason.error?.message ?? "unknown error");
+ observer?.onEvent?.(String(ev?.type), data, Number(ev?.time) || Date.now());
+ };
+ return { st, onNotification };
+ }
+
+ /**
+ * 等一轮结束;中止(/stop、看门狗、单回合上限)时只中止这个会话(agent.cancel),宽限期内没停下才重启子进程。
+ * 中止了就抛 TurnStopped(restarted 说明有没有重启)。
+ */
+ private async awaitTurn<T>(turn: HostTurn, run: Promise<T>, signal: AbortSignal | undefined): Promise<T> {
+ // 宽限期内没停下、子进程被关掉时,run 随后以"runtime closed"失败,已经没人等它
+ run.catch(() => undefined);
+ const timeoutMs = this.o.turnTimeoutMs ?? 30 * 60_000;
+ let stopping: Promise<TurnStopped> | undefined;
+ let wake!: () => void;
+ const stopRequested = new Promise<void>((r) => (wake = r));
+ const stop = (why: TurnStopped) => {
+ if (stopping) return;
+ turn.stopping = true;
+ stopping = this.stopSession(turn, why, run);
+ wake();
+ };
+ const timer = setTimeout(() => stop(new TurnStopped("timeout", timeoutMs)), timeoutMs);
+ const onAbort = () => stop(stopReason(signal!));
+ if (signal?.aborted) onAbort();
+ else signal?.addEventListener("abort", onAbort, { once: true });
+ try {
+ const first = await Promise.race([run.then((r) => ({ r }), (e: unknown) => ({ e })), stopRequested.then(() => undefined)]);
+ turn.finishing = true;
+ if (stopping) throw await stopping;
+ if (!first) throw new Error("agent turn ended without a result");
+ if ("e" in first) throw first.e;
+ return first.r;
+ } finally {
+ clearTimeout(timer);
+ signal?.removeEventListener("abort", onAbort);
+ }
+ }
+
+ /**
+ * 所有通知都先经过这里(同步,按到达顺序):认出代理自己醒来的一轮(后台命令结束时 DSH 唤醒空闲的代理),
+ * 把这一轮的事件交给跟踪它的人。宿主发起的一轮在它第一次空闲之前不算。
+ */
+ private tap(n: SdkNotification): void {
+ for (const w of this.wakes.values()) if (!w.ended && this.relates(n, w.sessionId)) w.push(n);
+ if (n.method !== "session.status") return;
+ const sid = String(n.params?.sessionId ?? "");
+ const own = [...this.turns.values()].find((t) => t.sessionId === sid && !t.wake);
+ if (n.params?.status === "idle") {
+ if (own) own.idleSeen = true;
+ this.wakes.get(sid)?.end();
+ return;
+ }
+ if (n.params?.status !== "running" || (own && !own.idleSeen)) return;
+ const prev = this.wakes.get(sid);
+ if (prev && !prev.ended) return;
+ const conv = this.o.store.conversationForSession(sid);
+ // 只跟踪会话自己的 DSH session(不是子代理、不是归档了的旧段)
+ if (!conv || this.o.store.getAgentSession(conv) !== sid || this.claimed.has(conv)) return;
+ const w = new Wake(conv, sid);
+ this.wakes.set(sid, w);
+ log.info("agent woke up on its own (a background job ended); following that turn", { bot: this.o.botId, conversationId: conv });
+ // 没人接(没有渠道能回复):结束一分钟后丢掉
+ void w.done.then(
+ () => setTimeout(() => !w.taken && this.wakes.get(sid) === w && this.wakes.delete(sid), 60_000).unref?.(),
+ () => undefined,
+ );
+ try {
+ this.o.onWake?.(conv, sid);
+ } catch (e) {
+ log.warn("wake handler failed", { message: (e as Error).message });
+ }
+ }
+
+ /** 通知属于这个会话或它的子代理 */
+ private relates(n: SdkNotification, root: string): boolean {
+ const c = this.harness?.client;
+ const under = (id: string) => id === root || (typeof c?.isDescendantOf === "function" && !!c.isDescendantOf(id, root));
+ if (n.method === "subagent.started" || n.method === "subagent.finished") return under(String(n.params?.parentSessionId ?? "")) || n.params?.childSessionId === root;
+ return under(String(n.params?.sessionId ?? ""));
+ }
+
+ /** 这个会话的代理自己醒着(后台命令结束后被唤醒) */
+ woke(conversationId: string): boolean {
+ return [...this.wakes.values()].some((w) => w.conversationId === conversationId && !w.taken);
+ }
+
+ /**
+ * 跟踪代理自己醒来的一轮(见 tap):和普通回合一样记步骤、算用量、收交付文件,能插话、能中止,
+ * 结束时返回模型的回复,由运行时发到会话里。
+ */
+ async followTurn(input: { conversationId: string; model: string; observer?: TurnObserver; signal?: AbortSignal }): Promise<TurnResult> {
+ const w = [...this.wakes.values()].find((x) => x.conversationId === input.conversationId && !x.taken);
+ if (!w) throw new Error("no wake-up turn to follow");
+ w.taken = true;
+ const route = await this.routeFor(input.model);
+ this.active++;
+ const turn: HostTurn = { sessionId: w.sessionId, model: input.model, accepted: Promise.resolve(), finishing: false, stopping: false, wake: true };
+ this.turns.set(input.conversationId, turn);
+ const tr = this.tracker(w.sessionId, route, input.observer);
+ try {
+ input.observer?.onStart?.();
+ w.attach(tr.onNotification);
+ await this.awaitTurn(turn, w.done, input.signal);
+ const { turnError, answers, presented, usage } = tr.st;
+ if (!answers.length && turnError) throw new Error(turnError);
+ return { text: answers.join("\n\n"), sessionId: w.sessionId, files: deliverables(this.workspace, presented), usage };
+ } catch (e) {
+ if (e instanceof TurnStopped) log.warn("agent wake-up turn stopped", { bot: this.o.botId, reason: e.reason, restarted: e.restarted });
+ throw e;
+ } finally {
+ turn.finishing = true;
+ if (this.wakes.get(w.sessionId) === w) this.wakes.delete(w.sessionId);
+ if (this.turns.get(input.conversationId) === turn) this.turns.delete(input.conversationId);
+ this.active--;
+ }
+ }
+
+ /**
+ * 中止一个会话的回合:等代理收下这条消息(否则 cancel 落空),经指令通道 agent.cancel(连同它的子代理和这一轮起的命令),
+ * 等回合停下。宽限期(AMB_STOP_GRACE_MS,默认 10 秒)内没停下、或指令通道不通,才关掉整个子进程
+ * (同一机器人其它会话的回合也会失败)。
+ */
+ private async stopSession(turn: HostTurn, why: TurnStopped, run: Promise<unknown>): Promise<TurnStopped> {
+ const graceMs = Number(process.env.AMB_STOP_GRACE_MS) > 0 ? Number(process.env.AMB_STOP_GRACE_MS) : 10_000;
+ const deadline = Date.now() + graceMs;
+ const left = () => Math.max(0, deadline - Date.now());
+ const settled = run.then(
+ () => true,
+ () => true,
+ );
+ let ok = false;
+ try {
+ const accepted = await withTimeoutOr(Promise.race([turn.accepted.then(() => true), settled]), left(), false);
+ if (await withTimeoutOr(settled, 0, false)) ok = true;
+ else if (accepted && this.control.connected) {
+ await this.control.send("cancel", { sessionId: turn.sessionId }, Math.max(1000, left()));
+ ok = await withTimeoutOr(settled, left(), false);
+ }
+ } catch (e) {
+ log.warn("cancelling the session's turn failed", { bot: this.o.botId, message: (e as Error).message.slice(0, 200) });
+ }
+ if (ok) return new TurnStopped(why.reason, why.ms, false);
+ log.warn("agent turn did not stop in time; restarting dsh", { bot: this.o.botId, reason: why.reason, graceMs, otherTurns: this.active - 1 });
+ await this.closeHarness();
+ this.state = "stopped";
+ return new TurnStopped(why.reason, why.ms, true);
+ }
+
+ /** 这个会话有回合在跑、还没在收尾或中止、指令通道在线:新消息可以并进这一轮 */
+ canSteer(conversationId: string): boolean {
+ const t = this.turns.get(conversationId);
+ return !!t && !t.finishing && !t.stopping && this.control.connected;
+ }
+
+ /**
+ * 把一条新消息并入这个会话正在进行的回合(agent.steer:模型在下一步看到它)。代理已经空闲或正在收尾时插件拒绝,
+ * 返回 false,调用方照常排队。同一个 id 重发是幂等的(结果没收到时再问一次)。
+ */
+ async steer(conversationId: string, msg: { id: string; text: string; images?: TurnImage[] }): Promise<boolean> {
+ const t = this.turns.get(conversationId);
+ if (!t || !this.canSteer(conversationId)) return false;
+ await withTimeoutOr(t.accepted, 5000, undefined);
+ if (this.turns.get(conversationId) !== t || t.finishing || t.stopping) return false;
+ const images = this.imageInput && acceptsImages(t.model) ? (msg.images ?? []) : [];
+ for (let i = 0; i < 2 && this.control.connected; i++) {
+ try {
+ const r = await this.control.send<{ accepted?: boolean; reason?: string }>("steer", { sessionId: t.sessionId, id: msg.id, text: msg.text, images }, 15_000);
+ if (!r.accepted) log.info("steer refused; the message waits for the next turn", { bot: this.o.botId, reason: r.reason });
+ return !!r.accepted;
+ } catch (e) {
+ log.warn("steer failed", { bot: this.o.botId, attempt: i + 1, message: (e as Error).message.slice(0, 200) });
+ }
+ }
+ return false;
+ }
+
private rememberFallback(model: string, api: AgentApi, reason: string): void {
const fb = this.fallbacks.set(model, api, reason);
log.warn("upstream rejected the agent's requests on this API; using Chat Completions for this model", {
@@ -1146,7 +1624,8 @@ exec "$REAL" "$@"
status(): Record<string, unknown> {
const api = this.api ? (this.fallback ? `${this.api} (fallback: ${this.fallback.reason})` : this.api) : undefined;
- return { state: this.state, pid: this.pid, model: this.model, api, restarts: this.restarts, lastError: this.lastError, workspace: this.workspace, activeTurns: this.active };
+ const models = [...new Set([...this.childRoutes.values()].map((r) => r.model))];
+ return { state: this.state, pid: this.pid, model: this.model, api, models, control: this.control.connected, restarts: this.restarts, lastError: this.lastError, workspace: this.workspace, activeTurns: this.active };
}
/** 故障排查用的完整状态(含 stderr 尾部与最近一次退出) */
diff --git a/src/agent/manager.ts b/src/agent/manager.ts
index 36787e4..8236d84 100644
--- a/src/agent/manager.ts
+++ b/src/agent/manager.ts
@@ -1,11 +1,12 @@
+import crypto from "node:crypto";
import { resolveComputerUse } from "./computer-use.js";
import type { Store } from "../store/db.js";
import { logger } from "../log.js";
import { AgentBridge } from "./bridge.js";
import path from "node:path";
-import { AgentHost, TurnStopped, callDetail, modelLimits, type ModelApiInfo, type TurnInput, type TurnObserver, type TurnUsage } from "./host.js";
+import { AgentHost, TurnStopped, callDetail, modelLimits, type ModelApiInfo, type TurnImage, type TurnInput, type TurnObserver, type TurnResult, type TurnUsage } from "./host.js";
import { ServerCompaction, type CompactEndpoint, type CompactionEvent } from "./server-compaction.js";
-import { ActivityLog, type ActivityStep } from "./activity.js";
+import { ActivityLog, promotedJob, 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";
@@ -70,6 +71,8 @@ export interface TurnProgress {
queuedNotice?: boolean;
/** 正在运行的工具调用(含子代理的):工具运行期间 DSH 不发事件,看门狗不把它当成卡住 */
tools: Set<string>;
+ /** 转到后台、回合在等它结果的长命令:等的时候同样不算卡住 */
+ jobs: Set<string>;
/** 进行中的生图 / 生视频任务 */
media: number;
}
@@ -105,6 +108,16 @@ function trackTools(tools: Set<string>, session: string, type: string, data: any
else if (type === "step/start" || type === "turn/end") for (const k of tools) if (k.startsWith(`${session}:`)) tools.delete(k);
}
+/**
+ * 记下转到后台、回合在等它结果的命令(工具结果里"moved to background job"),结束(job/settled)时移除。
+ * 模型自己放到后台的(run_in_background,比如开着的服务)回合不等,不算。
+ */
+function trackJobs(jobs: Set<string>, type: string, data: any): void {
+ const id = type === "tool/result" ? promotedJob(data) : undefined;
+ if (id) jobs.add(id);
+ else if (type === "job/settled") jobs.delete(String(data?.jobId));
+}
+
function deferred<T>() {
let resolve!: (v: T) => void;
const promise = new Promise<T>((r) => (resolve = r));
@@ -163,6 +176,11 @@ export class AgentManager {
memory: (b) => this.memoryOp(b),
steward: (b) => this.stewardCall(b),
media: (b) => this.mediaOp(b),
+ // amb-control 插件按机器人取指令;不认识的机器人(已删除、已停止)让它别再来
+ control: {
+ poll: (b, signal) => this.hosts.get(String(b?.botId ?? ""))?.control.poll(b, signal) ?? Promise.resolve({ stop: true }),
+ result: (b) => this.hosts.get(String(b?.botId ?? ""))?.control.result(b),
+ },
compaction: this.serverCompaction,
});
this.memory = new MemoryStore(path.join(o.dataDir, "memory"));
@@ -200,8 +218,9 @@ export class AgentManager {
contextWindow: this.o.contextWindow,
apiInfo: this.o.apiInfo,
apiFallbacks: this.apiFallbacks,
+ onWake: (conv) => this.onWake?.(botId, conv),
});
- // 代理进程退出(/stop、看门狗、换模型或设置、崩溃、重启、停止):进程里的压缩跟着没了,不再等它
+ // 代理进程退出(中止宽限期内没停下、设置变化、崩溃、重启、停止):进程里的压缩跟着没了,不再等它
h.on((e) => {
if (e.type === "exit") this.endCompaction((c) => c.botId === botId);
});
@@ -260,15 +279,30 @@ export class AgentManager {
/** onStep:活动记录里每新增或更新一步就回调一次(网页对话实时显示、回合结束后存进消息) */
async runTurn(botId: string, input: TurnInput & { onStep?: (step: ActivityStep) => void }) {
+ return this.track(botId, input, (i) => this.runTurnInner(botId, i));
+ }
+
+ /**
+ * 代理自己醒来的一轮(后台命令结束,DSH 唤醒空闲的代理):和普通回合一样登记(/status、中止、看门狗、插话),
+ * 跟踪到结束,返回回复。onWake 由运行时设置,收到后调这个。
+ */
+ async followTurn(botId: string, input: { conversationId: string; model: string; observer?: TurnObserver; onStep?: (step: ActivityStep) => void }) {
+ return this.track(botId, { ...input, text: "" }, (i) => this.followInner(botId, i));
+ }
+
+ /** 代理自己醒来开了一轮:(botId, conversationId) */
+ onWake: ((botId: string, conversationId: string) => void) | undefined;
+
+ private async track(botId: string, input: TurnInput & { onStep?: (step: ActivityStep) => void }, run: (input: TurnInput) => Promise<TurnResult>): Promise<TurnResult> {
const conv = input.conversationId;
- const t: TurnProgress = { botId, since: Date.now(), lastProgress: 0, activity: new ActivityLog(input.onStep, input.model), abort: new AbortController(), tools: new Set(), media: 0 };
+ const t: TurnProgress = { botId, since: Date.now(), lastProgress: 0, activity: new ActivityLog(input.onStep, input.model), abort: new AbortController(), tools: new Set(), jobs: new Set(), media: 0 };
this.turnsInProgress.set(conv, t);
// 看门狗:代理已经在处理,却长时间没有进展(模型不回、只在重试),又不是在等用户批准或回答、
- // 也没有工具或生成任务在跑(生视频、长命令、子代理里的工具)→ 中止
+ // 也没有工具、命令或生成任务在跑(生视频、转到后台的长命令、子代理里的工具)→ 中止
const idleMs = this.o.idleTimeoutMs ?? 10 * 60_000;
const watchdog = setInterval(() => {
if (!t.lastProgress || t.abort.signal.aborted) return;
- if (this.approvals.pending(conv).length || t.tools.size || t.media) t.lastProgress = Date.now();
+ if (this.approvals.pending(conv).length || t.tools.size || t.jobs.size || t.media) t.lastProgress = Date.now();
else if (Date.now() - t.lastProgress > idleMs) {
log.warn("agent turn idle, stopping", { bot: botId, conversationId: conv, idleMs, lastStep: t.activity.last });
this.halt(conv, t, new TurnStopped("idle", idleMs));
@@ -282,28 +316,56 @@ export class AgentManager {
},
onEvent: (type, data, time) => {
if (type === "subagent/event") {
- // 子代理在干活:它的工具调用同样记下;它在重试模型请求不算进展
+ // 子代理在干活:它的工具调用、命令同样记下;它在重试模型请求不算进展
const ev = data?.event ?? {};
trackTools(t.tools, String(data?.sessionId ?? ""), String(ev.type), ev.data);
+ trackJobs(t.jobs, String(ev.type), ev.data);
if (ev.type !== "llm/retry" && ev.type !== "llm/retry-started") t.lastProgress = Date.now();
} else {
trackTools(t.tools, "", type, data);
+ trackJobs(t.jobs, type, data);
if (t.activity.apply(type, data, time)) t.lastProgress = Date.now();
}
input.observer?.onEvent?.(type, data, time);
},
};
try {
- return await this.runTurnInner(botId, { ...input, observer, signal: t.abort.signal });
+ const r = await run({ ...input, observer, signal: t.abort.signal });
+ t.activity.finish();
+ return r;
} catch (e) {
- if (e instanceof TurnStopped && e.reason === "timeout") t.activity.end("超过单回合上限");
+ if (e instanceof TurnStopped && e.reason === "timeout") {
+ t.activity.end("超过单回合上限");
+ for (const r of this.approvals.pending(conv)) this.approvals.cancel(r.id, "stopped");
+ }
throw e;
} finally {
clearInterval(watchdog);
- this.turnsInProgress.delete(conv);
+ if (this.turnsInProgress.get(conv) === t) this.turnsInProgress.delete(conv);
}
}
+ /**
+ * 新消息能不能并入这个会话正在进行的回合:代理已经收下了上一条、回合没在中止或收尾、指令通道在线。
+ * 不能的时候消息照常排队,等这一轮结束再处理。
+ */
+ canSteer(conversationId: string): boolean {
+ const t = this.turnsInProgress.get(conversationId);
+ return !!t && !!t.lastProgress && !t.abort.signal.aborted && !!this.hosts.get(t.botId)?.canSteer(conversationId);
+ }
+
+ /** 把新消息并入进行中的回合(模型在下一步看到它);并不进去返回 false */
+ async steer(botId: string, conversationId: string, msg: { text: string; images?: TurnImage[] }): Promise<boolean> {
+ const t = this.turnsInProgress.get(conversationId);
+ if (!t || t.botId !== botId || !this.canSteer(conversationId)) return false;
+ const ok = await this.hosts.get(botId)!.steer(conversationId, { id: `amb-steer:${crypto.randomUUID()}`, ...msg });
+ if (ok && this.turnsInProgress.get(conversationId) === t) {
+ t.activity.apply("steer", { text: msg.text }, Date.now());
+ t.lastProgress = Date.now();
+ }
+ return ok;
+ }
+
/** /stop、信息面板的"中止":返回有没有正在处理的回合 */
stopTurn(conversationId: string): boolean {
const t = this.turnsInProgress.get(conversationId);
@@ -315,10 +377,30 @@ export class AgentManager {
private halt(conv: string, t: TurnProgress, why: TurnStopped): void {
t.activity.end(why.reason === "user" ? "用户中止" : why.reason === "idle" ? "长时间没有进展,自动中止" : "超过单回合上限");
t.abort.abort(why);
- // 这一轮还在等的批准 / 回答随之作废(代理进程会被重启,没人再等它们)
+ // 这一轮还在等的批准 / 回答随之作废(回合被中止,没人再等它们)
for (const r of this.approvals.pending(conv)) this.approvals.cancel(r.id, "stopped");
}
+ /** 跟踪代理自己醒来的一轮:不用等管家闸门(代理已经在跑),结束后同样交给管家判定 */
+ private async followInner(botId: string, input: TurnInput): Promise<TurnResult> {
+ const conv = input.conversationId;
+ const set = this.activeConvs.get(botId) ?? new Set<string>();
+ this.activeConvs.set(botId, set);
+ set.add(conv);
+ this.decisions.get(conv)?.resolve(false);
+ const d = deferred<boolean>();
+ this.decisions.set(conv, d);
+ let r: TurnResult | undefined;
+ try {
+ r = await this.host(botId).followTurn(input);
+ return r;
+ } finally {
+ set.delete(conv);
+ if (r) this.afterTurn(botId, conv, r.sessionId, input.model, r.usage, d);
+ else d.resolve(false);
+ }
+ }
+
private async runTurnInner(botId: string, input: TurnInput) {
const conv = input.conversationId;
const gate = this.gates.get(conv);
diff --git a/src/app.ts b/src/app.ts
index b7d47b6..e1a7ff4 100644
--- a/src/app.ts
+++ b/src/app.ts
@@ -10,6 +10,7 @@ import { deferNotice, interruptedText, takeInterrupted, type Interrupted } from
import type { ChannelStatus } from "./core/types.js";
import { MagpieSupervisor } from "./magpie/supervisor.js";
import { FeishuChannel } from "./channels/feishu/channel.js";
+import { FeishuReply } from "./channels/feishu/card.js";
import { LarkTransport, type FeishuTransport } from "./channels/feishu/transport.js";
import { WeixinChannel } from "./channels/weixin/channel.js";
import { WeixinLogin } from "./channels/weixin/login.js";
@@ -589,6 +590,9 @@ export class App {
if (bot.feishuAppId && bot.feishuAppSecret && !ch.feishu) {
const transport = this.opts.feishuTransport?.(bot) ?? new LarkTransport(bot.feishuAppId, bot.feishuAppSecret);
ch.feishu = new FeishuChannel(bot.id, transport, this.runtime, this.queue, this.config.feishu.throttleMs);
+ // 代理自己醒来的一轮(后台命令结束):回复到这个会话最近收到的那条消息
+ const throttleMs = this.config.feishu.throttleMs;
+ this.runtime.registerReplier(bot.id, "feishu", (_conv, last) => (last?.replyHandle?.messageId ? { out: new FeishuReply(transport, last.replyHandle.messageId, throttleMs) } : undefined));
ch.feishuError = undefined;
ch.feishu.start().catch((e) => {
ch.feishuError = (e as Error).message;
@@ -603,6 +607,11 @@ export class App {
pollTimeoutMs: this.opts.weixinPollTimeoutMs,
});
ch.weixin.onExpired = () => this.notice("warn", `机器人「${bot.name}」微信登录已过期,请在信息面板的「渠道」里重新扫码`);
+ const wx = ch.weixin;
+ this.runtime.registerReplier(bot.id, "weixin", (conv) => {
+ const out = wx.replyTo(conv.peerId);
+ return out ? { out } : undefined;
+ });
ch.weixin.start();
}
}
@@ -611,6 +620,8 @@ export class App {
const ch = this.channels.get(botId);
if (!ch) return;
ch.weixinLogin?.cancel();
+ this.runtime.registerReplier(botId, "feishu", undefined);
+ this.runtime.registerReplier(botId, "weixin", undefined);
await ch.feishu?.stop().catch(() => {});
await ch.weixin?.stop().catch(() => {});
this.channels.delete(botId);
diff --git a/src/channels/weixin/channel.ts b/src/channels/weixin/channel.ts
index 1bf407d..396ab90 100644
--- a/src/channels/weixin/channel.ts
+++ b/src/channels/weixin/channel.ts
@@ -240,6 +240,12 @@ export class WeixinChannel {
return true;
}
+ /** 主动发到一个微信会话(代理自己醒来的一轮):用这个用户最近的 context_token,占同一份回复配额;登录不在时没有 */
+ replyTo(userId: string): ReplySession | undefined {
+ const token = this.store.getWeixinPeer(this.botId, userId)?.contextToken;
+ return this.client && token ? new WeixinReply(this.client, userId, token, this.quota) : undefined;
+ }
+
async stop(): Promise<void> {
this.abort?.abort();
await this.loop?.catch(() => {});
diff --git a/src/core/queue.ts b/src/core/queue.ts
index cb1523e..9d3d85e 100644
--- a/src/core/queue.ts
+++ b/src/core/queue.ts
@@ -1,15 +1,34 @@
-/** 按 key 串行执行(同一会话的消息按到达顺序处理),不同 key 并行。 */
+import { AsyncLocalStorage } from "node:async_hooks";
+
+/** 当前任务的"让位"函数(见 KeyedQueue.releaser) */
+const slot = new AsyncLocalStorage<() => void>();
+
+/**
+ * 按 key 串行执行(同一会话的消息按到达顺序处理),不同 key 并行。
+ * 任务可以提前让位(KeyedQueue.releaser()):之后同 key 的下一个任务不再等它结束,由任务自己保证先后
+ * (运行时在代理回合交给代理后让位,后来的消息才能并入进行中的回合)。
+ */
export class KeyedQueue {
private tails = new Map<string, Promise<void>>();
run(key: string, task: () => Promise<void>): Promise<void> {
const prev = this.tails.get(key) ?? Promise.resolve();
- const next = prev.then(task, task).catch(() => {});
+ let release!: () => void;
+ const released = new Promise<void>((r) => (release = r));
+ const start = () => slot.run(release, task);
+ const done = prev.then(start, start).catch(() => {});
+ // 下一个任务在这个结束或让位时开始
+ const next = Promise.race([done, released]);
this.tails.set(key, next);
void next.then(() => {
if (this.tails.get(key) === next) this.tails.delete(key);
});
- return next;
+ return done;
+ }
+
+ /** 正在执行的队列任务的让位函数(不在队列任务里时为 undefined);在任务开始处同步取 */
+ static releaser(): (() => void) | undefined {
+ return slot.getStore();
}
get size(): number {
diff --git a/src/core/runtime.ts b/src/core/runtime.ts
index b075636..9f06149 100644
--- a/src/core/runtime.ts
+++ b/src/core/runtime.ts
@@ -1,7 +1,8 @@
import crypto from "node:crypto";
import fs from "node:fs";
import path from "node:path";
-import type { InboundEvent, ReplySession } from "./types.js";
+import type { ChannelId, InboundEvent, ReplySession } from "./types.js";
+import { KeyedQueue } from "./queue.js";
import { eventText } from "./types.js";
import { acceptsImages, type ModelClient, type ChatMessage } from "../llm/client.js";
import { TurnStopped, type TurnImage } from "../agent/host.js";
@@ -14,7 +15,7 @@ import { segmentInput, type ChatPlace } from "../agent/prompts/index.js";
import { clearInflight, markInflight, takeNotice } from "./recovery.js";
import { pwshProbe, pwshStatusLine } from "../agent/pwsh.js";
import { isBotProviderModel } from "../modelflare.js";
-import type { ApprovalRow } from "../store/db.js";
+import type { ApprovalRow, Conversation } from "../store/db.js";
const log = logger("runtime");
@@ -105,7 +106,7 @@ const HELP = [
"/reset — 清空当前会话的上下文(代理模式下同时开启新的代理会话;长期记忆保留)",
"/new — 开始新的一段对话:旧对话归档并生成交接便条,长期记忆保留",
"/status — 查看当前模型、代理状态和正在处理的消息走到了哪一步",
- "/stop — 中止正在处理的消息(代理模式下会重启代理进程)",
+ "/stop — 中止正在处理的消息(只停这个会话,机器人的其它会话照常进行)",
"/help — 显示本帮助",
].join("\n");
@@ -116,11 +117,24 @@ interface Inflight {
abort: AbortController;
/** 被应用退出打断:不回复,标记留着,下次启动时告诉用户(recovery.ts) */
interrupted?: boolean;
+ /** 让出渠道的串行队列:代理收下这条消息(或这条并进了进行中的回合)后,后来的消息不必再等它结束 */
+ release?: () => void;
+ /** 用户消息已经存进历史(交给代理时就存,保证插进来的消息排在它后面) */
+ userStored?: boolean;
}
/** 回合还没交给代理(或纯对话还没出字)就被停止时的回复 */
export const stoppedText = (ms: number) => `已中止(处理了 ${fmtSecs(ms)})。`;
+/** 新消息并进了正在进行的代理回合:先回这一句,回复在那一轮结束时一起给 */
+export const STEER_ACK = "收到,这条会并入正在进行的处理,稍后一起回复。";
+
+/**
+ * 不是回复某条消息、而是主动发到会话里(代理自己醒来的一轮:后台命令结束):渠道给出这个会话的出站会话。
+ * last 是这个会话最近收到的一条消息(回复句柄);done 在这一轮结束时调用。没有办法发时返回 undefined。
+ */
+export type Replier = (conv: Conversation, last: InboundEvent | undefined) => { out: ReplySession; done?: () => void } | undefined;
+
/** 渠道无关的对话运行时:命令处理、上下文拼装、流式调用模型、持久化。 */
export class BotRuntime {
private modelCache: { at: number; list: string[] } | undefined;
@@ -128,7 +142,22 @@ export class BotRuntime {
private inflight = new Map<string, Set<Inflight>>();
/** 应用正在退出:正在处理的消息算被打断,不再回复也不改用纯对话 */
private closing = false;
- constructor(private opts: RuntimeOptions) {}
+ /** 会话 → 之前所有消息都处理完的时刻(同一会话的消息按到达顺序处理;并进进行中回合的不必等) */
+ private tails = new Map<string, Promise<void>>();
+ /** 会话 → 最近收到的一条消息(只留回复句柄):主动发消息时回复到它 */
+ private lastInbound = new Map<string, InboundEvent>();
+ /** "<botId>:<渠道>" → 主动发消息的出站会话 */
+ private repliers = new Map<string, Replier>();
+ constructor(private opts: RuntimeOptions) {
+ // 代理自己醒来开了一轮(后台命令结束):跟着它,把回复发到会话里
+ if (opts.agent) opts.agent.onWake = (botId, convId) => void this.wake(botId, convId).catch((e) => log.warn("wake-up turn failed", { message: (e as Error).message }));
+ }
+
+ /** 渠道登记怎么主动往它的会话里发消息(代理自己醒来的一轮用);botId 为 "*" 时对所有机器人有效 */
+ registerReplier(botId: string, channel: ChannelId, fn: Replier | undefined): void {
+ if (fn) this.repliers.set(`${botId}:${channel}`, fn);
+ else this.repliers.delete(`${botId}:${channel}`);
+ }
/** 应用退出前调用(App.stop):之后失败的回合留下重启标记,由下次启动告诉用户 */
shutdown(): void {
@@ -182,23 +211,21 @@ export class BotRuntime {
const bot = store.getBot(ev.botId);
if (!bot) throw new Error(`unknown bot ${ev.botId}`);
const conv = store.ensureConversation(bot.id, ev.channel, ev.conversation.id, ev.sender.name || ev.sender.id, ev.conversation.kind === "group" ? "group" : "dm");
- const t: Inflight = { botId: bot.id, since: Date.now(), abort: new AbortController() };
+ const t: Inflight = { botId: bot.id, since: Date.now(), abort: new AbortController(), release: KeyedQueue.releaser() };
const link = () => t.abort.abort();
if (opts.signal?.aborted) link();
else opts.signal?.addEventListener("abort", link, { once: true });
- const set = this.inflight.get(conv.id) ?? new Set<Inflight>();
- this.inflight.set(conv.id, set);
- set.add(t);
+ this.lastInbound.set(conv.id, { ...ev, parts: [] });
+ const done = this.enter(conv.id, t);
// 重启标记:应用退出或崩溃时还没处理完的,下次启动告诉用户
const text = eventText(ev);
if (text) markInflight(store, conv.id, text);
try {
- await this.handleMessage(ev, out, bot, conv.id, conv.model, t);
+ await this.handleMessage(ev, out, bot, conv.id, t, done.prior);
} finally {
opts.signal?.removeEventListener("abort", link);
- set.delete(t);
- if (!set.size && this.inflight.get(conv.id) === set) this.inflight.delete(conv.id);
- if (!set.size && !t.interrupted) {
+ done.leave();
+ if (!this.inflight.get(conv.id)?.size && !t.interrupted) {
try {
clearInflight(store, conv.id);
} catch {
@@ -208,9 +235,32 @@ export class BotRuntime {
}
}
- private async handleMessage(ev: InboundEvent, out: ReplySession, bot: NonNullable<ReturnType<Store["getBot"]>>, convId: string, convModel: string, t: Inflight): Promise<void> {
+ /**
+ * 登记一条正在处理的消息(或代理自己醒来的一轮):/stop 能停它;排进会话的先后(prior 是它前面那些都处理完)。
+ * 渠道的串行队列在代理收下消息后就让位(Inflight.release),之后的先后由这里保证。
+ */
+ private enter(convId: string, t: Inflight): { prior: Promise<void> | undefined; leave: () => void } {
+ const set = this.inflight.get(convId) ?? new Set<Inflight>();
+ this.inflight.set(convId, set);
+ set.add(t);
+ const prior = this.tails.get(convId);
+ let finish!: () => void;
+ const mine = new Promise<void>((r) => (finish = r));
+ const tail = (prior ?? Promise.resolve()).then(() => mine);
+ this.tails.set(convId, tail);
+ return {
+ prior,
+ leave: () => {
+ finish();
+ if (this.tails.get(convId) === tail) this.tails.delete(convId);
+ set.delete(t);
+ if (!set.size && this.inflight.get(convId) === set) this.inflight.delete(convId);
+ },
+ };
+ }
+
+ private async handleMessage(ev: InboundEvent, out: ReplySession, bot: NonNullable<ReturnType<Store["getBot"]>>, convId: string, t: Inflight, prior: Promise<void> | undefined): Promise<void> {
const { store } = this.opts;
- const conv = { id: convId, model: convModel };
const signal = t.abort.signal;
const agent = this.opts.agent;
// 代理模式:收到的图片/文件先进工作区,消息里写路径
@@ -219,14 +269,26 @@ export class BotRuntime {
if (!text) return;
const images = inboundImages(ev);
// 上次重启时没处理完、当时又没能告诉用户的:先说一声
- const notice = takeNotice(store, conv.id);
+ const notice = takeNotice(store, convId);
if (notice) await out.apply({ type: "text", text: notice }).catch((e) => log.warn("restart notice failed", { message: (e as Error).message.slice(0, 200) }));
+ // 前面还有消息在处理:代理回合进行中就把这条并进去(模型下一步看到,回复一起给),否则排在它们后面。
+ // 命令(/model、/reset…)不并入,等前面的处理完
+ if (prior) {
+ if (agent?.available && !signal.aborted && !text.startsWith("/") && (await this.steer(agent, bot.id, convId, text, images, out, t))) {
+ await prior;
+ return;
+ }
+ await prior;
+ }
+ // 前面的消息可能换了模型(/model)
+ const conv = { id: convId, model: store.getConversation(convId)?.model ?? "" };
+
// 排队时就被停止了:不再处理(命令也不执行),留一句"已中止"
if (signal.aborted) {
const msg = stoppedText(Date.now() - t.since);
if (!text.startsWith("/")) {
- store.addMessage(conv.id, "user", text);
+ if (!t.userStored) store.addMessage(conv.id, "user", text);
store.addMessage(conv.id, "assistant", msg);
}
await out.apply({ type: "text", text: msg });
@@ -264,7 +326,8 @@ export class BotRuntime {
if (handled === "starting") note = "(代理还在启动,这条消息先由模型直接回答;启动完成后会自动用回代理。)";
}
- store.addMessage(conv.id, "user", text);
+ if (!t.userStored) store.addMessage(conv.id, "user", text);
+ t.userStored = true;
const history = store.contextWindow(conv.id, this.opts.contextMessages, this.opts.contextChars);
const messages: ChatMessage[] = [];
if (bot.systemPrompt.trim()) messages.push({ role: "system", content: bot.systemPrompt });
@@ -325,9 +388,9 @@ export class BotRuntime {
if (!agent) return { handled: false };
const r = agent.approvals.handleReply(convId, text, `${ev.channel}:${ev.sender.id}`);
if (r.handled) return r;
- // 前一条还在处理:说一声这条排在后面、怎么中止(每个回合只说一次),消息照常入队
+ // 前一条还在处理:能并入进行中的代理回合时由 handle 并入并回一句;不能时说一声这条排在后面、怎么中止(每个回合只说一次),消息照常入队
const t = agent.turnsInProgress.get(convId);
- if (t && !t.queuedNotice) {
+ if (t && !t.queuedNotice && !agent.canSteer(convId)) {
t.queuedNotice = true;
const last = t.activity.last;
return { handled: false, ack: `上一条消息还在处理(已 ${fmtSecs(Date.now() - t.since)}${last ? `,现在:${clipLine(describeStep(last))}` : ""}),这条会排在它后面。发送 /stop 可以中止。` };
@@ -350,7 +413,7 @@ export class BotRuntime {
// Windows:代理执行命令用的 PowerShell;没有 7 时建议安装
const ps = a?.available ? pwshStatusLine(pwshProbe.snapshot()) : "";
const turn = a?.turnsInProgress.get(convId);
- const busy = turn ? [busyLine(turn.since, turn.lastProgress, turn.activity.steps)] : [];
+ const busy = turn ? [busyLine(turn.since, turn.lastProgress, turn.activity.steps, !!a?.canSteer(convId))] : [];
return [`当前模型:${m}${convModel ? "(会话设置)" : ""}`, `运行方式:${mode}`, ...full, ...host, ...(ps ? [ps] : []), ...busy, ...cu, ...(pend.length ? [`未决:${pend.join(",")}`] : [])].join("\n");
}
@@ -423,7 +486,8 @@ export class BotRuntime {
clearInterval(startup);
log.info("message stopped before the agent took it", { bot: botId, conversationId: convId });
const msg = stoppedText(Date.now() - inflight.since);
- store.addMessage(convId, "user", text);
+ if (!inflight.userStored) store.addMessage(convId, "user", text);
+ inflight.userStored = true;
store.addMessage(convId, "assistant", msg);
await out.apply({ type: "stream.finish", text: msg });
return true;
@@ -461,6 +525,12 @@ export class BotRuntime {
onProgress: () => {
visible = true;
},
+ // 代理收下了这条消息:先存用户消息(之后并进来的消息排在它后面),再让出渠道队列,后来的消息可以并入这一轮
+ onStart: () => {
+ if (!inflight.userStored) store.addMessage(convId, "user", text);
+ inflight.userStored = true;
+ inflight.release?.();
+ },
},
});
const halt = () => agent.stopTurn(convId);
@@ -469,7 +539,8 @@ export class BotRuntime {
clearInterval(startup);
signal.removeEventListener("abort", halt);
});
- store.addMessage(convId, "user", text);
+ if (!inflight.userStored) store.addMessage(convId, "user", text);
+ inflight.userStored = true;
const finalText = r.text.trim() || "(已完成,没有文字回复)";
store.addMessage(convId, "assistant", finalText, [...steps.values()]);
await out.apply({ type: "stream.finish", text: finalText });
@@ -494,7 +565,8 @@ export class BotRuntime {
return false;
}
const msg = turnFailedText(err, [...steps.values()]);
- store.addMessage(convId, "user", text);
+ if (!inflight.userStored) store.addMessage(convId, "user", text);
+ inflight.userStored = true;
store.addMessage(convId, "assistant", msg, [...steps.values()]);
await out.apply({ type: "stream.finish", text: msg });
return true;
@@ -505,6 +577,85 @@ export class BotRuntime {
}
}
+ /**
+ * 把新消息并入这个会话正在进行的代理回合:成功就存下用户消息(排在上一条后面)、让出渠道队列、回一句收到。
+ * 回合没在跑、正在收尾或中止、指令通道不通时返回 false,消息照常排队。图片只交给能看图的模型(由宿主判断)。
+ */
+ private async steer(agent: AgentManager, botId: string, convId: string, text: string, images: TurnImage[], out: ReplySession, t: Inflight): Promise<boolean> {
+ if (!agent.canSteer(convId)) return false;
+ const ok = await agent.steer(botId, convId, { text, ...(images.length ? { images } : {}) }).catch((e: Error) => {
+ log.warn("steer failed", { message: e.message.slice(0, 200) });
+ return false;
+ });
+ if (!ok) return false;
+ log.info("message steered into the running turn", { bot: botId, conversationId: convId });
+ this.opts.store.addMessage(convId, "user", text);
+ t.userStored = true;
+ t.release?.();
+ await out.apply({ type: "text", text: STEER_ACK });
+ return true;
+ }
+
+ /**
+ * 代理自己醒来的一轮(后台命令结束,DSH 唤醒了空闲的代理):像普通回合一样登记(/status、/stop、看门狗、插话都认它),
+ * 经渠道主动发到会话里(网页对话进 live 登记;飞书回复最近一条消息;微信占一条回复配额),回复与步骤存进历史。
+ */
+ async wake(botId: string, convId: string): Promise<void> {
+ const { store } = this.opts;
+ const agent = this.opts.agent;
+ const conv = store.getConversation(convId);
+ const bot = store.getBot(botId);
+ // 那一轮已经有人跟着、或者随代理进程一起没了:不发任何东西
+ if (this.closing || !agent?.available || !conv || !bot || !agent.host(botId).woke(convId)) return;
+ const r = (this.repliers.get(`${botId}:${conv.channel}`) ?? this.repliers.get(`*:${conv.channel}`))?.(conv, this.lastInbound.get(convId));
+ if (!r) log.warn("no way to reply in this conversation; the wake-up reply is only kept in its history", { bot: botId, conversationId: convId, channel: conv.channel });
+ const out: ReplySession = r?.out ?? { apply: async () => undefined };
+ const t: Inflight = { botId, since: Date.now(), abort: new AbortController(), userStored: true };
+ const done = this.enter(convId, t);
+ const steps = new Map<string, ActivityStep>();
+ const unbind = agent.approvals.bindSink(convId, async (row) => void (await out.apply(interactiveOp(row))));
+ const halt = () => agent.stopTurn(convId);
+ t.abort.signal.addEventListener("abort", halt, { once: true });
+ try {
+ const model = conv.model || bot.defaultModel || this.opts.defaultModel();
+ const routed = model && this.opts.route ? this.opts.route(botId, model) : model;
+ await out.apply({ type: "typing", on: true });
+ await out.apply({ type: "stream.start" });
+ let res: Awaited<ReturnType<AgentManager["followTurn"]>>;
+ try {
+ res = await agent.followTurn(botId, {
+ conversationId: convId,
+ model: routed,
+ onStep: (step) => {
+ steps.set(step.id, step);
+ void out.apply({ type: "activity", step });
+ },
+ });
+ } catch (e) {
+ const msg = turnFailedText(e as Error, [...steps.values()]);
+ store.addMessage(convId, "assistant", msg, [...steps.values()]);
+ await out.apply({ type: "stream.finish", text: msg });
+ return;
+ }
+ const finalText = res.text.trim() || "(后台任务已结束,没有文字回复)";
+ store.addMessage(convId, "assistant", finalText, [...steps.values()]);
+ await out.apply({ type: "stream.finish", text: finalText });
+ for (const f of res.files ?? []) {
+ try {
+ await out.apply({ type: "file", path: f, name: path.basename(f) });
+ } catch (e) {
+ log.warn("deliver file failed", { name: path.basename(f), message: (e as Error).message.slice(0, 200) });
+ }
+ }
+ } finally {
+ t.abort.signal.removeEventListener("abort", halt);
+ unbind();
+ await out.apply({ type: "typing", on: false }).catch(() => undefined);
+ done.leave();
+ r?.done?.();
+ }
+ }
+
/** 返回 null 表示不是已知命令,按普通消息处理。 */
private async command(text: string, convId: string, convModel: string, botModel: string, botId: string): Promise<string | null> {
const [cmd, ...rest] = text.trim().split(/\s+/);
@@ -602,8 +753,8 @@ export function interactiveOp(row: ApprovalRow) {
const clipLine = (s: string, n = 80) => (s.length > n ? s.slice(0, n - 1) + "…" : s);
-/** /status 里"上一条还在处理"那一行:处理了多久、现在在做什么、多久没动了 */
-export function busyLine(since: number, lastProgress: number, steps: ActivityStep[]): string {
+/** /status 里"上一条还在处理"那一行:处理了多久、现在在做什么、多久没动了;steer = 新消息会并入这一轮 */
+export function busyLine(since: number, lastProgress: number, steps: ActivityStep[], steer = false): string {
const now = Date.now();
const last = steps.at(-1);
const parts = [`已 ${fmtSecs(now - since)}`];
@@ -611,7 +762,7 @@ export function busyLine(since: number, lastProgress: number, steps: ActivitySte
if (!lastProgress) parts.push("还在等代理进程接手");
else if (last) parts.push(`现在:${clipLine(describeStep(last))}`);
if (lastProgress && now - lastProgress >= 60_000) parts.push(`${fmtSecs(now - lastProgress)}没有新进展`);
- return `本会话上一条消息还在处理(${parts.join(",")}),新消息会排在它后面。发送 /stop 可以中止`;
+ return `本会话上一条消息还在处理(${parts.join(",")}),${steer ? "新消息会并入正在进行的处理" : "新消息会排在它后面"}。发送 /stop 可以中止`;
}
/** 代理回合失败时给用户的话:中止(/stop、没进展、超时)各说清原因和下一步,进程被重启的另说 */
@@ -619,9 +770,11 @@ export function turnFailedText(err: Error, steps: ActivityStep[]): string {
const last = steps.filter((s) => s.kind !== "error").at(-1);
const where = last ? `${last.cut || last.status === "running" ? "卡在" : "最后一步"}:${clipLine(describeStep(last))}。` : "";
if (err instanceof TurnStopped) {
- if (err.reason === "user") return `已中止(处理了 ${fmtSecs(err.ms)})。代理进程已重启,可以继续发消息。`;
- if (err.reason === "idle") return `⚠️ 代理 ${fmtSecs(err.ms)}没有任何进展,已自动中止并重启代理进程。${where}可以重新发送,或用 /model 换个模型试试。`;
- return `⚠️ 这条消息处理超过了 ${fmtSecs(err.ms)}的上限,已中止并重启代理进程。${where}可以把任务拆小一点再发。`;
+ // 只中止了这个会话;宽限期内没停下才重启了代理进程(同一机器人的其它会话也被打断)
+ const restart = err.restarted ? "并重启代理进程" : "";
+ if (err.reason === "user") return `已中止(处理了 ${fmtSecs(err.ms)})。${err.restarted ? "代理进程已重启," : ""}可以继续发消息。`;
+ if (err.reason === "idle") return `⚠️ 代理 ${fmtSecs(err.ms)}没有任何进展,已自动中止${restart}。${where}可以重新发送,或用 /model 换个模型试试。`;
+ return `⚠️ 这条消息处理超过了 ${fmtSecs(err.ms)}的上限,已中止${restart}。${where}可以把任务拆小一点再发。`;
}
if (/runtime (closed|exited)/i.test(err.message)) return "⚠️ 代理进程重启了(可能是同一机器人的另一个会话中止了卡住的消息),这条消息没有处理完,请重新发送。";
return `⚠️ 代理执行失败:${err.message.slice(0, 300)}`;
diff --git a/src/diagnostics.ts b/src/diagnostics.ts
index bdddb40..0b1503e 100644
--- a/src/diagnostics.ts
+++ b/src/diagnostics.ts
@@ -239,10 +239,10 @@ export async function testAgent(app: App, opts: { botId?: string; model?: string
const memory = app.agent.settings(botId).memory.enabled;
const expectedTools = ["present", "ask_user_question", ...(memory ? ["remember"] : [])];
- // 同一机器人正在处理的回合:同一会话里后来的消息(包括用户看到"一直思考中"的那条)都排在它后面
+ // 同一机器人正在处理的回合:同一会话里后来的消息(包括用户看到"一直思考中"的那条)并入它(代理还没接手时排在它后面)
const turns = app.agent.diagnostics().turns.filter((t) => t.botId === botId);
if (!turns.length) mark("queue", "ok", "没有正在处理的回合", 0);
- else mark("queue", "warn", `${turns.length} 轮对话正在处理(最长已 ${Math.round(Math.max(...turns.map((t) => t.forMs)) / 1000)} 秒);同一会话的新消息会排在它后面。卡住时用下面的"重启代理进程"`, 0);
+ else mark("queue", "warn", `${turns.length} 轮对话正在处理(最长已 ${Math.round(Math.max(...turns.map((t) => t.forMs)) / 1000)} 秒);同一会话的新消息会并入正在进行的回合(代理还没接手时排在后面)。卡住时用 /stop,或下面的"重启代理进程"`, 0);
begun.spawn = Date.now();
if (host.state === "starting") {
diff --git a/src/web/live.ts b/src/web/live.ts
index 1ba63da..1ea1ae0 100644
--- a/src/web/live.ts
+++ b/src/web/live.ts
@@ -2,6 +2,7 @@ import crypto from "node:crypto";
import type { ActivityStep } from "../agent/activity.js";
import type { OutboundOp, ReplySession } from "../core/types.js";
import { opEvent } from "./sse-reply.js";
+import { STEER_ACK } from "../core/runtime.js";
export interface LivePrompt {
kind: string;
@@ -21,12 +22,15 @@ export interface LiveSnapshot {
files: string[];
/** 已经按了停止,正在收尾 */
stopping: boolean;
+ /** 这条并进了它前面那条正在进行的处理:没有自己的回复,回复在前面那条里 */
+ steered: boolean;
}
type Listener = (event: string, data: unknown) => void;
/**
* 网页控制台的一条消息,从收到到回复结束(排队、等代理启动、流式回复都算)。
+ * 代理自己醒来的一轮(后台命令结束)也登记成一条,userText 为空。
* 运行时把 OutboundOp 写进来:这里记下到目前为止的状态,再原样转给订阅者——发消息的那个请求,
* 以及切走再切回、刷新之后接上来的界面。
*/
@@ -38,6 +42,8 @@ export class LiveTurn implements ReplySession {
readonly prompts: LivePrompt[] = [];
readonly files: string[] = [];
stopping = false;
+ /** 并进了正在进行的处理(见 LiveSnapshot.steered) */
+ steered = false;
done = false;
/** 停止按钮:交给 runtime.handle 的 signal(排队中的也能停) */
readonly abort = new AbortController();
@@ -53,6 +59,7 @@ export class LiveTurn implements ReplySession {
async apply(op: OutboundOp): Promise<void> {
if (this.done) return;
+ if (op.type === "text" && op.text === STEER_ACK) this.steered = true;
if (op.type === "stream.update" || op.type === "stream.finish" || op.type === "text") this.text = op.text;
else if (op.type === "activity") this.steps.set(op.step.id, op.step);
else if (op.type === "file") this.files.push(op.name);
@@ -73,6 +80,7 @@ export class LiveTurn implements ReplySession {
prompts: [...this.prompts],
files: [...this.files],
stopping: this.stopping,
+ steered: this.steered,
};
}
diff --git a/src/web/server.ts b/src/web/server.ts
index 38f8b63..bb32b4d 100644
--- a/src/web/server.ts
+++ b/src/web/server.ts
@@ -180,6 +180,13 @@ export async function startWebServer(app: App, opts: { port?: number; host?: str
const last = app.store.getConversation(convId) ? (app.store.listMessages(convId, 1).at(-1)?.id ?? 0) : 0;
return live.start(convId, botId, text, last);
}
+ // 代理自己醒来的一轮(后台命令结束):像一条进行中的消息那样登记(没有用户消息),界面接上就能看到;结束时移出登记
+ app.runtime.registerReplier("*", "web", (conv) => {
+ if (conv.id !== consoleConvId(conv.botId)) return undefined;
+ const t = startLive(conv.botId, "");
+ return { out: t, done: () => live.end(t, "done", { conversationId: conv.id }) };
+ });
+
/** 排进会话队列交给运行时;结束时先移出登记,再通知发消息的请求和所有接上的界面。不抛错 */
async function runLive(t: LiveTurn, ev: InboundEvent, out: ReplySession = t): Promise<void> {
let err: Error | undefined;
diff --git a/test/activity.unit.test.ts b/test/activity.unit.test.ts
index 3e335e4..5bad284 100644
--- a/test/activity.unit.test.ts
+++ b/test/activity.unit.test.ts
@@ -120,10 +120,14 @@ describe("what the user is told", () => {
it("stopped / idle / timeout / restarted / other failures", () => {
const steps = [step({ kind: "model", title: "grok-4.7", status: "error", cut: true }), step({ kind: "error", title: "已中止", status: "error" })];
- expect(turnFailedText(new TurnStopped("user", 95_000), steps)).toBe("已中止(处理了 1 分 35 秒)。代理进程已重启,可以继续发消息。");
- expect(turnFailedText(new TurnStopped("idle", 600_000), steps)).toBe("⚠️ 代理 10 分钟没有任何进展,已自动中止并重启代理进程。卡在:等待 grok-4.7 回复。可以重新发送,或用 /model 换个模型试试。");
- expect(turnFailedText(new TurnStopped("timeout", 1_800_000), [step({ summary: "npm test", status: "ok" })])).toBe("⚠️ 这条消息处理超过了 30 分钟的上限,已中止并重启代理进程。最后一步:bash:npm test(完成)。可以把任务拆小一点再发。");
- expect(turnFailedText(new TurnStopped("timeout", 1_800_000), [])).toBe("⚠️ 这条消息处理超过了 30 分钟的上限,已中止并重启代理进程。可以把任务拆小一点再发。");
+ // 只中止了这个会话(agent.cancel)
+ expect(turnFailedText(new TurnStopped("user", 95_000), steps)).toBe("已中止(处理了 1 分 35 秒)。可以继续发消息。");
+ expect(turnFailedText(new TurnStopped("idle", 600_000), steps)).toBe("⚠️ 代理 10 分钟没有任何进展,已自动中止。卡在:等待 grok-4.7 回复。可以重新发送,或用 /model 换个模型试试。");
+ expect(turnFailedText(new TurnStopped("timeout", 1_800_000), [step({ summary: "npm test", status: "ok" })])).toBe("⚠️ 这条消息处理超过了 30 分钟的上限,已中止。最后一步:bash:npm test(完成)。可以把任务拆小一点再发。");
+ // 宽限期内没停下,退回重启子进程
+ expect(turnFailedText(new TurnStopped("user", 95_000, true), steps)).toBe("已中止(处理了 1 分 35 秒)。代理进程已重启,可以继续发消息。");
+ expect(turnFailedText(new TurnStopped("idle", 600_000, true), steps)).toBe("⚠️ 代理 10 分钟没有任何进展,已自动中止并重启代理进程。卡在:等待 grok-4.7 回复。可以重新发送,或用 /model 换个模型试试。");
+ expect(turnFailedText(new TurnStopped("timeout", 1_800_000, true), [])).toBe("⚠️ 这条消息处理超过了 30 分钟的上限,已中止并重启代理进程。可以把任务拆小一点再发。");
expect(turnFailedText(new Error("DeepSeek Harness runtime closed"), [])).toMatch(/^⚠️ 代理进程重启了/);
expect(turnFailedText(new Error("boom"), [])).toBe("⚠️ 代理执行失败:boom");
});
diff --git a/test/agent-manager.unit.test.ts b/test/agent-manager.unit.test.ts
index f640885..b1edb21 100644
--- a/test/agent-manager.unit.test.ts
+++ b/test/agent-manager.unit.test.ts
@@ -180,7 +180,9 @@ function setupHost(script: (emit: (n: any) => void, s: { mgr: AgentManager }) =>
setWorkingDirectory: async () => undefined,
};
const harness = { client: {}, start: async () => undefined, session: () => session, resumeSession: async () => session, close: async () => void closed.abort() };
- Object.assign(host as any, { harness, model: "m", sig: (host as any).settingsSig(), state: "running" });
+ // 启动时的默认路线就是 m(Chat Completions):不用指令通道也能跑
+ const route = { model: "m", provider: "magpie", api: "openai-completions", limits: { window: 128_000, maxTokens: 16_000 } };
+ Object.assign(host as any, { harness, model: "m", sig: (host as any).settingsSig(), state: "running", initRoute: route, childRoutes: new Map([["magpie|m", route]]) });
return {
mgr,
notices,
diff --git a/test/agent.integration.test.ts b/test/agent.integration.test.ts
index e222686..ef17d4b 100644
--- a/test/agent.integration.test.ts
+++ b/test/agent.integration.test.ts
@@ -6,8 +6,9 @@ import module from "node:module";
import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { App } from "../src/app.js";
import { loadConfig } from "../src/config.js";
-import { RETRY_NOTE, locateDshRuntime } from "../src/agent/host.js";
+import { RETRY_NOTE, SHELL_LIMITS, locateDshRuntime } from "../src/agent/host.js";
import { CHAT_RULES, MEMORY_GUIDE, botIdentity } from "../src/agent/prompts/index.js";
+import { STEER_ACK } from "../src/core/runtime.js";
import { findAppRoot } from "../src/magpie/supervisor.js";
import { listen } from "./helpers.js";
@@ -177,12 +178,29 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
function plan(messages: any[]): Step {
let lastUser = -1;
for (let i = messages.length - 1; i >= 0; i--) {
- if (messages[i].role === "user" && /QUICKSORT|ESCALATE|SECRET|READFILE|LOOPBACK|PRESENT|ASKQ|NATIVES|LOOKIMG|HANG|COUNTER|OUTWRITE|RMRF|RMWS|CHILDRM|SUBRM|SUBASK|SUBHELLO|PADDED|\[用户消息\]/.test(textOf(messages[i].content))) { lastUser = i; break; }
+ if (messages[i].role === "user" && /QUICKSORT|ESCALATE|SECRET|READFILE|LOOPBACK|PRESENT|ASKQ|NATIVES|LOOKIMG|HANG|COUNTER|OUTWRITE|RMRF|RMWS|CHILDRM|SUBRM|SUBASK|SUBHELLO|PADDED|STEERME|SLOWOK|BGJOB|TOKENS|^background job |\[用户消息\]/.test(textOf(messages[i].content))) { lastUser = i; break; }
}
const user = textOf(messages[lastUser]?.content);
const tools = messages.slice(lastUser + 1).filter((m) => m.role === "tool");
// 卡住的模型:请求收下了,一直不回
if (/HANG/.test(user)) return { hang: true };
+ // 插话:先跑一条几秒的命令;命令结束后看有没有并进来的用户消息(文字、图片)
+ if (/STEERME/.test(user)) {
+ const later = messages.slice(lastUser + 1).filter((m) => m.role === "user");
+ const img = later.find((m) => Array.isArray(m.content) && m.content.some((p: any) => p?.type === "image_url"));
+ if (img) return { text: `SAWIMG<<${img.content.filter((p: any) => p?.type === "image_url").map((p: any) => String(p.image_url?.url ?? "").split(",")[0]).join(" ")}|${/\[图片 inbox\/[^\]]+\.png\]/.test(textOf(img.content)) ? "path" : "nopath"}>>` };
+ const steered = later.find((m) => /STEERTEXT/.test(textOf(m.content)));
+ if (steered) return { text: `SAW<<${textOf(steered.content)}>>` };
+ if (!tools.length) return { tool: { name: "bash", args: { description: "run", command: "sleep 3 && echo first-done" } } };
+ return { text: "NOSTEER" };
+ }
+ // 后台命令结束,代理被唤醒:读它的输出,再跑一条一秒的命令(这一轮有时间看 /status),然后回复
+ if (/^background job /.test(user)) {
+ const id = /^background job (\S+)/.exec(user)?.[1];
+ if (tools.length === 0) return { tool: { name: "job_output", args: { job_id: id } } };
+ if (tools.length === 1) return { tool: { name: "bash", args: { description: "run", command: "sleep 1 && echo wake-step" } } };
+ return { text: `BGDONE<<${tools.map((t) => textOf(t.content)).join("|")}>>` };
+ }
// 长回复:让会话历史很快长过系统提示和工具(服务端压缩用例)
if (/PADDED/.test(user)) return { text: "PADDED-OK " + "z".repeat(12_000) };
// 看图:回报模型收到的图片(data URL 的头)和文字里有没有工作区路径
@@ -206,6 +224,10 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
...(canStripTypes ? [{ name: "workflow", args: { script: "return { answer: 6 * 7 }", meta: { name: "calc", description: "multiply" } } }] : []),
]],
[/COUNTER/, [{ name: "bash", args: { description: "run", command: "echo ran >> counter.txt && wc -l < counter.txt" } }]],
+ [/SLOWOK/, [{ name: "bash", args: { description: "run", command: "sleep 4 && echo slow-done" } }]],
+ // 跑得比转后台的阈值久:命令转到后台,这一轮先回复"还在跑"
+ [/BGJOB/, [{ name: "bash", args: { description: "run", command: "sleep 4 && echo bg-finished-42" } }]],
+ [/TOKENS/, [{ name: "bash", args: { description: "run", command: "echo token=${AMB_BRIDGE_TOKEN:-none} control=${AMB_CONTROL_TOKEN:-none} key=${AMB_MAGPIE_KEY:-none} url=${AMB_BRIDGE_URL:-none}" } }]],
[/LOOPBACK/, [{ name: "bash", args: { description: "run", command: `python3 -c "import urllib.request as u\ntry:\n print('API', u.urlopen('${base}/amb/api/status').status)\nexcept Exception as e:\n print('API', getattr(e,'code',e))"` } }]],
// 完全权限:写工作区外的目录、读宿主数据目录、显式要求 danger-full-access
[/OUTWRITE/, [
@@ -225,6 +247,7 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
for (const [re, steps] of scripts) {
if (!re.test(user)) continue;
if (tools.length < steps.length) return { tool: steps[tools.length] };
+ if (/BGJOB/.test(user)) return { text: `STILL-RUNNING<<${tools.map((t) => textOf(t.content)).join("|")}>>` };
return { text: `RESULT<<${tools.map((t) => textOf(t.content)).join("|")}>>` };
}
return { text: "好的" };
@@ -550,15 +573,19 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
const console_ = async () => (await (await fetch(`${base}/amb/api/bots/default/chat`)).json()) as { conversationId: string; messages: any[] };
const api = async (p: string, init?: RequestInit) => (await fetch(`${base}/amb/api${p}`, init)).json();
- it("a turn without progress is stopped by the watchdog: the agent restarts and the reply keeps its steps", async () => {
+ it("a turn without progress is stopped by the watchdog: only that conversation's turn is cancelled (no restart), the reply keeps its steps", async () => {
const o = (app.agent as any).o;
const prev = o.idleTimeoutMs;
o.idleTimeoutMs = 3000;
try {
+ expect((await chat("/reset")).reply).toMatch(/已清空/);
+ expect((await chat("hello before the hang")).reply).toBe("好的");
+ const pid = app.agent.host("default").pid;
const t0 = Date.now();
const r = await chat("HANG please");
- expect(r.reply).toBe("⚠️ 代理 3 秒没有任何进展,已自动中止并重启代理进程。卡在:等待 mock-agent 回复。可以重新发送,或用 /model 换个模型试试。");
+ expect(r.reply).toBe("⚠️ 代理 3 秒没有任何进展,已自动中止。卡在:等待 mock-agent 回复。可以重新发送,或用 /model 换个模型试试。");
expect(Date.now() - t0).toBeLessThan(30_000);
+ expect(app.agent.host("default").pid).toBe(pid);
} finally {
o.idleTimeoutMs = prev;
}
@@ -580,9 +607,12 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
turn = (await api(`/conversations/${conv}/turn`)).turn;
}
expect(turn.steps.at(-1)).toMatchObject({ kind: "model", title: "mock-agent", status: "running" });
- expect((await chat("/status")).reply).toMatch(/本会话上一条消息还在处理(已 \d+ 秒,1 步,现在:等待 mock-agent 回复),新消息会排在它后面。发送 \/stop 可以中止/);
+ expect((await chat("/status")).reply).toMatch(/本会话上一条消息还在处理(已 \d+ 秒,1 步,现在:等待 mock-agent 回复),新消息会并入正在进行的处理。发送 \/stop 可以中止/);
+ const pid = app.agent.host("default").pid;
expect((await chat("/stop")).reply).toMatch(/^正在中止上一条消息(已处理 \d+ 秒)…$/);
- expect((await pending).reply).toMatch(/^已中止(处理了 \d+ 秒)。代理进程已重启,可以继续发消息。$/);
+ expect((await pending).reply).toMatch(/^已中止(处理了 \d+ 秒)。可以继续发消息。$/);
+ // 只中止了这个会话的回合(agent.cancel),代理进程没有重启
+ expect(app.agent.host("default").pid).toBe(pid);
expect((await api(`/conversations/${conv}/turn`)).turn).toBeNull();
expect((await chat("/stop")).reply).toBe("当前没有正在处理的消息。");
expect((await chat("/reset")).reply).toMatch(/已清空/);
@@ -647,7 +677,7 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
expect(Date.now() - t0).toBeLessThan(15_000);
expect(a.events.map(([e]) => e)).toContain("stopping");
const final = a.texts().at(-1)!;
- expect(final).toMatch(/^已中止(处理了 \d+ 秒)。代理进程已重启,可以继续发消息。$/);
+ expect(final).toMatch(/^已中止(处理了 \d+ 秒)。可以继续发消息。$/);
expect(a.events.at(-1)![0]).toBe("done");
const after = await api("/bots/default/chat");
expect(after.live).toEqual([]);
@@ -695,7 +725,7 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
expect((await api(`/bots/default/chat`)).live[0].prompts.map((p: any) => p.requestId)).toEqual([id]);
expect(await stopWeb()).toEqual({ stopped: true });
await s.done;
- expect(s.texts().at(-1)).toMatch(/^已中止(处理了 \d+ 秒)。代理进程已重启,可以继续发消息。$/);
+ expect(s.texts().at(-1)).toMatch(/^已中止(处理了 \d+ 秒)。可以继续发消息。$/);
expect((await api(`/approvals/${id}`)).approval.status).toBe("cancelled");
expect((await chat("/reset")).reply).toMatch(/已清空/);
expect((await chat("hello once again")).reply).toBe("好的");
@@ -1091,4 +1121,190 @@ describe.skipIf(!runtime || !hasBwrap || process.platform !== "linux")("agent ho
await sc.settled();
}
}, 300_000);
+
+ // —— 实时控制:插话、按会话中止、同一进程里各会话各用各的模型、长命令转后台 ——
+ /** 一个微信会话发一条消息,不等它结束:返回 { done(回复文字), conv } */
+ const startOn = (user: string, model: string, text: string, images: Buffer[] = []) => {
+ const conv = app.store.ensureConversation("default", "weixin", user, user, "dm");
+ app.store.setConversationModel(conv.id, model);
+ const ops: any[] = [];
+ const parts = [{ type: "text" as const, text }, ...images.map((data) => ({ type: "image" as const, data, mime: "image/png" }))];
+ const done = app.runtime
+ .handle({ id: `${user}-${rnd()}`, channel: "weixin", botId: "default", conversation: { kind: "dm", id: user }, sender: { id: user }, parts, replyHandle: {}, receivedAt: Date.now() }, { apply: async (op) => void ops.push(op) })
+ .then(() => ops.findLast((o) => o.type === "stream.finish" || o.type === "text")?.text as string);
+ return { done, conv: conv.id, ops };
+ };
+ const toolRunning = async (conv: string) => ((await api(`/conversations/${encodeURIComponent(conv)}/turn`)) as any).turn?.steps?.some((s: any) => s.kind === "tool" && s.status === "running");
+
+ it("a message sent while a turn runs is steered into it: one reply, history in order, the web live registry consistent, no restart", async () => {
+ expect((await chat("/reset")).reply).toMatch(/已清空/);
+ expect((await chat("hello before steering")).reply).toBe("好的");
+ const host = app.agent.host("default");
+ const pid = host.pid;
+ const conv = (await console_()).conversationId;
+ const first = chat("STEERME please");
+ await waitFor(() => toolRunning(conv));
+ const before = calls.length;
+ const second = chat("STEERTEXT extra-xyz");
+ // 网页的 live 登记里两条都在:第二条并进了第一条,没有自己的回复
+ let d: any;
+ await waitFor(async () => (d = await api("/bots/default/chat")).live?.length === 2 && d.live[1].steered);
+ expect(d.live.map((t: any) => [t.userText, t.steered])).toEqual([
+ ["STEERME please", false],
+ ["STEERTEXT extra-xyz", true],
+ ]);
+ expect((await first).reply).toBe("SAW<<STEERTEXT extra-xyz>>");
+ expect((await second).reply).toBe(STEER_ACK);
+ // 第二条没有单独开一轮:之后的模型请求都带着第一条(同一轮里的下一步)
+ expect(calls.slice(before).every((c) => JSON.stringify(c.body).includes("STEERME please"))).toBe(true);
+ const after = await console_();
+ expect(after.messages.slice(-3).map((m: any) => [m.role, m.content])).toEqual([
+ ["user", "STEERME please"],
+ ["user", "STEERTEXT extra-xyz"],
+ ["assistant", "SAW<<STEERTEXT extra-xyz>>"],
+ ]);
+ expect(after.messages.filter((m: any) => m.content === "STEERTEXT extra-xyz")).toHaveLength(1);
+ expect(((await api("/bots/default/chat")) as any).live).toEqual([]);
+ const a = await api(`/conversations/${encodeURIComponent(conv)}/messages/${after.messages.at(-1).id}/activity`);
+ expect(a.steps.some((s: any) => s.kind === "steer" && s.summary === "STEERTEXT extra-xyz")).toBe(true);
+ expect(host.pid).toBe(pid);
+ }, 120_000);
+
+ it("an image sent while a turn runs reaches the agent with the steered message", async () => {
+ if (!app.agent.host("default").imageInput) return; // Linux 上的 Electron:只给路径
+ const a = startOn("steer-img", "gpt-5-mock", "STEERME with a picture");
+ await waitFor(() => toolRunning(a.conv));
+ const b = startOn("steer-img", "gpt-5-mock", "LOOKSTEER", [png]);
+ expect(await a.done).toMatch(/^SAWIMG<<data:image\/(png|jpeg|webp);base64\|path>>$/);
+ expect(await b.done).toBe(STEER_ACK);
+ const hist = app.store.listMessages(a.conv).map((m) => [m.role, m.content.replace(/inbox\/\S+\.png/, "inbox/X.png")]);
+ expect(hist.slice(-3)).toEqual([
+ ["user", "STEERME with a picture"],
+ ["user", "LOOKSTEER\n[图片 inbox/X.png]"],
+ ["assistant", expect.stringMatching(/^SAWIMG<</)],
+ ]);
+ }, 120_000);
+
+ it("/stop cancels only that conversation's turn: another conversation of the same bot keeps running, no restart", async () => {
+ const host = app.agent.host("default");
+ const pid = host.pid;
+ const restarts = host.restarts;
+ const b = startOn("stop-b", "mock-agent", "SLOWOK b");
+ const a = startOn("stop-a", "mock-agent", "HANG stop-a");
+ await waitFor(() => toolRunning(b.conv));
+ await waitFor(async () => ((await api(`/conversations/${encodeURIComponent(a.conv)}/turn`)) as any).turn?.steps?.length > 0);
+ expect(app.runtime.stop(a.conv)).toBe(true);
+ expect(await a.done).toMatch(/^已中止(处理了 \d+ 秒)。可以继续发消息。$/);
+ expect(await b.done).toContain("slow-done");
+ expect(host.pid).toBe(pid);
+ expect(host.restarts).toBe(restarts);
+ expect(host.state).toBe("running");
+ }, 120_000);
+
+ it("each conversation runs on its own model in one agent process: a switch, even to a model the patch did not list, needs no restart", async () => {
+ catalog["gpt-6-mock"] = ["openai", "openai-response", "openai-response-compact"];
+ catalog["claude-switch-mock"] = ["anthropic", "openai"];
+ const host = app.agent.host("default");
+ expect(host.state).toBe("running");
+ const pid = host.pid;
+ expect((host.status() as any).models).not.toContain("claude-switch-mock");
+ const from = calls.length;
+ // 会话 X 在 gpt-6-mock(Responses)上跑一条几秒的命令;同时会话 Y 用一个新模型(Anthropic Messages)
+ const x = startOn("switch-x", "gpt-6-mock", "SLOWOK on responses");
+ await waitFor(() => toolRunning(x.conv));
+ const y = await turnOn("switch-y", "claude-switch-mock", "QUICKSORT on a new model");
+ expect(y.reply).toContain("[1, 2, 3, 5, 7, 9]");
+ expect(await x.done).toContain("slow-done");
+ expect(callsFor("claude-switch-mock", from).map((c) => c.api)).toEqual(callsFor("claude-switch-mock", from).map(() => "messages"));
+ expect(callsFor("claude-switch-mock", from).length).toBeGreaterThan(0);
+ expect(callsFor("gpt-6-mock", from).filter((c) => c.body.tools).every((c) => c.api === "responses")).toBe(true);
+ // 每个请求都是它所在会话的模型
+ for (const c of calls.slice(from).filter((c) => c.body.tools)) {
+ const body = JSON.stringify(c.body);
+ if (body.includes("SLOWOK on responses")) expect(c.model).toBe("gpt-6-mock");
+ if (body.includes("QUICKSORT on a new model")) expect(c.model).toBe("claude-switch-mock");
+ }
+ // 会话 Y 再换回 gpt-6-mock:同一个会话、同一个进程
+ const again = calls.length;
+ expect((await turnOn("switch-y", "gpt-6-mock", "QUICKSORT after switching back")).reply).toContain("[1, 2, 3, 5, 7, 9]");
+ expect(callsFor("gpt-6-mock", again).filter((c) => c.body.tools).every((c) => c.api === "responses")).toBe(true);
+ expect(callsFor("claude-switch-mock", again)).toEqual([]);
+ expect(host.pid).toBe(pid);
+ expect((host.status() as any).models).toEqual(expect.arrayContaining(["gpt-6-mock", "claude-switch-mock"]));
+ }, 180_000);
+
+ it("tool processes never see the bridge token or the control channel id (DSH scrubs *TOKEN*/*KEY* from their environment)", async () => {
+ const r = await chat("TOKENS check");
+ expect(r.reply).toContain("token=none control=none key=none");
+ expect(r.reply).not.toContain(app.agent.bridge.token);
+ expect(r.reply).not.toContain(app.agent.host("default").control.id);
+ }, 120_000);
+
+ it("a long command moves to the background; the turn replies at once; when it ends the agent wakes and the follow-up reaches the chat", async () => {
+ const host = app.agent.host("default");
+ const saved = { ...SHELL_LIMITS };
+ // 测试里把转后台的阈值调到 1.5 秒(真实是 30 秒),重启代理进程让补丁生效
+ Object.assign(SHELL_LIMITS, { bashMs: 1500, bashMaxMs: 3000 });
+ try {
+ await host.restart();
+ expect((await chat("/reset")).reply).toMatch(/已清空/);
+ const conv = (await console_()).conversationId;
+ const t0 = Date.now();
+ const r = await chat("BGJOB now");
+ expect(r.reply).toMatch(/^STILL-RUNNING<<\[still running after 1500ms; moved to background job bash-\S+\]/);
+ // 没有等命令跑完(它要 4 秒):这一轮的回复里还没有它的输出
+ expect(r.reply).not.toContain("bg-finished-42");
+ expect(Date.now() - t0).toBeLessThan(30_000);
+ const first = await console_();
+ const steps1 = (await api(`/conversations/${encodeURIComponent(conv)}/messages/${first.messages.at(-1).id}/activity`)).steps;
+ expect(steps1.find((s: any) => s.kind === "job")).toMatchObject({ title: "后台命令", status: "ok", summary: expect.stringContaining("(回复时仍在后台运行)") });
+ // 命令结束:代理被唤醒,宿主像普通回合一样跟着这一轮:网页 live 里有一条(没有用户消息),/status 和信息面板都认它
+ let live: any;
+ await waitFor(async () => !!(live = ((await api("/bots/default/chat")) as any).live.find((t: any) => t.userText === "")), 30_000);
+ expect(live.steered).toBe(false);
+ await waitFor(async () => !!((await api(`/conversations/${encodeURIComponent(conv)}/turn`)) as any).turn);
+ expect((await chat("/status")).reply).toMatch(/本会话上一条消息还在处理/);
+ expect((await api("/agent")).busy).toEqual(["default"]);
+ // 回复存进历史、发到会话里
+ let d: any;
+ await waitFor(async () => /BGDONE/.test((d = await console_()).messages.at(-1)?.content ?? ""), 60_000);
+ const reply = d.messages.at(-1);
+ expect(reply.role).toBe("assistant");
+ expect(reply.content).toContain("bg-finished-42");
+ expect(reply.content).toContain("wake-step");
+ expect(d.messages.slice(-3).map((m: any) => m.role)).toEqual(["user", "assistant", "assistant"]);
+ const steps2 = (await api(`/conversations/${encodeURIComponent(conv)}/messages/${reply.id}/activity`)).steps;
+ expect(steps2.map((s: any) => s.kind)).toEqual(expect.arrayContaining(["job", "model", "tool"]));
+ await waitFor(async () => ((await api("/bots/default/chat")) as any).live.length === 0);
+ expect(((await api(`/conversations/${encodeURIComponent(conv)}/turn`)) as any).turn).toBeNull();
+ } finally {
+ Object.assign(SHELL_LIMITS, saved);
+ await host.restart();
+ }
+ }, 180_000);
+
+ it("a wake-up turn can be stopped like any other; nothing restarts", async () => {
+ const host = app.agent.host("default");
+ const saved = { ...SHELL_LIMITS };
+ Object.assign(SHELL_LIMITS, { bashMs: 1500, bashMaxMs: 3000 });
+ try {
+ await host.restart();
+ expect((await chat("/reset")).reply).toMatch(/已清空/);
+ const conv = (await console_()).conversationId;
+ expect((await chat("BGJOB again")).reply).toMatch(/^STILL-RUNNING/);
+ const pid = host.pid;
+ await waitFor(async () => !!((await api(`/conversations/${encodeURIComponent(conv)}/turn`)) as any).turn, 30_000);
+ expect((await chat("/stop")).reply).toMatch(/^正在中止上一条消息/);
+ let d: any;
+ await waitFor(async () => /^已中止/.test((d = await console_()).messages.at(-1)?.content ?? ""), 30_000);
+ expect(d.messages.at(-1).content).toMatch(/^已中止(处理了 \d+ 秒)。可以继续发消息。$/);
+ expect(host.pid).toBe(pid);
+ // 同一个会话、同一个代理进程照常接着用
+ expect((await chat("QUICKSORT after stopping the wake-up turn")).reply).toContain("[1, 2, 3, 5, 7, 9]");
+ expect(host.pid).toBe(pid);
+ } finally {
+ Object.assign(SHELL_LIMITS, saved);
+ await host.restart();
+ }
+ }, 180_000);
});
diff --git a/test/agent.unit.test.ts b/test/agent.unit.test.ts
index 25b10ac..1a6e2ed 100644
--- a/test/agent.unit.test.ts
+++ b/test/agent.unit.test.ts
@@ -6,7 +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, agentApi, modelLimits, type AgentApi } from "../src/agent/host.js";
+import { AgentHost, SHELL_LIMITS, agentApi, modelLimits, providerFor, providerProfiles, type AgentApi, type ModelRoute } from "../src/agent/host.js";
import { AgentBridge, BOT_HEADER, type ProxyEvent, type Rejection } from "../src/agent/bridge.js";
import { ApiFallbacks, shapeRejection } from "../src/agent/api-fallback.js";
import { ActivityLog } from "../src/agent/activity.js";
@@ -275,14 +275,23 @@ describe("agent API per model", () => {
expect(agentApi(claude, { AMB_AGENT_API: "" })).toBe("anthropic-messages");
});
- const patchFor = (model: string, api: AgentApi, reasoning?: boolean) => {
+ const route = (model: string, api: AgentApi, reasoning?: boolean): ModelRoute => ({ model, api, provider: providerFor(api, reasoning), reasoning, limits: modelLimits(model, 1_000_000) });
+ const patchOf = (routes: ModelRoute[], pwsh?: string) => {
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 text = fs.readFileSync((host as any).writePatch(model, [], undefined, modelLimits(model, 1_000_000), api, reasoning), "utf8");
+ const text = fs.readFileSync((host as any).writePatch(routes, [], pwsh), "utf8");
fs.rmSync(dataDir, { recursive: true, force: true });
return text;
};
+ /** 只有一个模型时:它所在 provider 的那一段 */
+ const patchFor = (model: string, api: AgentApi, reasoning?: boolean) => {
+ const text = patchOf([route(model, api, reasoning)]);
+ const name = providerFor(api, reasoning);
+ const start = text.indexOf(` ${name}:\n`);
+ const next = text.slice(start + 1).search(/\n {6}\S|\n- /);
+ return text.slice(start, start + 1 + next + 1);
+ };
it("the patch names the API; Anthropic's base URL is the bridge root (the SDK adds /v1/messages)", () => {
const chat = patchFor("mfb-b/gemini-3.6-flash", "openai-completions");
@@ -297,6 +306,32 @@ describe("agent API per model", () => {
expect(claude).not.toMatch(/reasoning|compat/);
});
+ it("one agent process serves every model: a provider per API (Responses split by reasoning), every model also under Chat for the fallback", () => {
+ const routes = [route("mfb-b/gpt-6.1-terra", "openai-responses", true), route("mfb-b/gpt-mini", "openai-responses", false), route("mfb-b/claude-sonnet-5", "anthropic-messages"), route("mfb-b/gemini-3.6-flash", "openai-completions")];
+ const p = providerProfiles(routes, "http://127.0.0.1:1", "b");
+ expect(Object.keys(p).sort()).toEqual(["magpie", "magpie-anthropic", "magpie-responses", "magpie-responses-plain"]);
+ expect(p.magpie.models.map((m) => m.id)).toEqual(routes.map((r) => r.model));
+ expect(p["magpie-responses"]).toMatchObject({ api: "openai-responses", reasoning: "medium", models: [{ id: "mfb-b/gpt-6.1-terra" }] });
+ expect(p["magpie-responses-plain"].reasoning).toBeUndefined();
+ expect(p["magpie-anthropic"]).toMatchObject({ api: "anthropic-messages", baseURL: "http://127.0.0.1:1", models: [{ id: "mfb-b/claude-sonnet-5" }] });
+ // 每个模型自己的上下文窗口和最大输出
+ const text = patchOf([{ ...routes[0], limits: { window: 1_050_000, maxTokens: 32_000 } }, { ...routes[3], limits: { window: 64_000, maxTokens: 16_000 } }]);
+ expect(text).toContain(' - id: "mfb-b/gemini-3.6-flash"\n name: "gemini-3.6-flash"\n contextWindow: 64000\n maxTokens: 16000\n');
+ expect(text).toContain(' - id: "mfb-b/gpt-6.1-terra"\n name: "gpt-6.1-terra"\n contextWindow: 1050000\n maxTokens: 32000\n');
+ // 指令通道插件
+ expect(text).toContain(' - id: amb-control\n name: "/rt/plugins/amb-control.mjs"\n');
+ });
+
+ it("long commands move to a background job: shell and job-wait limits sized for a chat assistant; PowerShell keeps its path", () => {
+ const text = patchOf([route("m", "openai-completions")], "C:\\PS7\\pwsh.exe");
+ expect(text).toContain(`- id: bash-sandbox\n config:\n timeoutMs: ${SHELL_LIMITS.bashMs}\n maxTimeoutMs: ${SHELL_LIMITS.bashMaxMs}\n`);
+ expect(text).toContain(`- id: pwsh-sandbox\n config:\n timeoutMs: ${SHELL_LIMITS.pwshMs}\n maxTimeoutMs: ${SHELL_LIMITS.pwshMaxMs}\n pwshPath: "C:\\\\PS7\\\\pwsh.exe"\n`);
+ expect(text).toContain(`- id: tool-jobs\n config:\n waitTimeoutMs: ${SHELL_LIMITS.waitMs}\n maxWaitTimeoutMs: ${SHELL_LIMITS.waitMaxMs}\n`);
+ // 结束时唤醒代理(DSH 默认),宿主跟着那一轮
+ expect(text).not.toContain("completionDelivery");
+ expect(SHELL_LIMITS.bashMs).toBeGreaterThan(5_000);
+ });
+
it("Responses: reasoning models ask for medium effort (encrypted reasoning for replay); system role; explicit strict:false", () => {
const resp = patchFor("mfb-b/gpt-6.1-terra", "openai-responses", true);
expect(resp).toContain(' api: openai-responses\n baseURL: "http://127.0.0.1:1/v1"\n reasoning: medium\n apiKeyEnv: AMB_MAGPIE_KEY\n');
diff --git a/test/live-chat.test.ts b/test/live-chat.test.ts
index 4bc307f..af92b75 100644
--- a/test/live-chat.test.ts
+++ b/test/live-chat.test.ts
@@ -326,7 +326,7 @@ describe("运行时的停止(代理路径)", () => {
ac.abort();
await p;
expect(seen.stopTurn).toBe(1);
- expect(texts().at(-1)).toMatch(/^已中止(处理了 \d+ 秒)。代理进程已重启,可以继续发消息。$/);
+ expect(texts().at(-1)).toMatch(/^已中止(处理了 \d+ 秒)。可以继续发消息。$/);
});
it("排队时就停了:不处理、命令也不执行,消息记成已中止", async () => {
diff --git a/test/live-control.unit.test.ts b/test/live-control.unit.test.ts
new file mode 100644
index 0000000..74123ab
--- /dev/null
+++ b/test/live-control.unit.test.ts
@@ -0,0 +1,265 @@
+/** 实时控制的零件:渠道队列让位、宿主 → 代理进程的指令通道(经 bridge)、后台命令与插话在活动记录里的样子、只算完整回答。 */
+import os from "node:os";
+import { afterEach, describe, expect, it } from "vitest";
+import { KeyedQueue } from "../src/core/queue.js";
+import { AgentBridge } from "../src/agent/bridge.js";
+import { ControlChannel } from "../src/agent/control.js";
+import { ActivityLog, describeStep, promotedJob } from "../src/agent/activity.js";
+import { answerText } from "../src/agent/host.js";
+import { BotRuntime, STEER_ACK } from "../src/core/runtime.js";
+import { Store } from "../src/store/db.js";
+import { DEFAULT_SETTINGS } from "../src/agent/settings.js";
+import type { AgentManager } from "../src/agent/manager.js";
+import type { InboundEvent, OutboundOp } from "../src/core/types.js";
+import { until } from "./helpers.js";
+
+const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms));
+
+describe("KeyedQueue: a task can give up its slot early", () => {
+ it("the next task of the same key starts once the first releases; run() still resolves when the task ends", async () => {
+ const q = new KeyedQueue();
+ const log: string[] = [];
+ let finishFirst!: () => void;
+ const first = q.run("k", async () => {
+ log.push("a+");
+ const release = KeyedQueue.releaser();
+ await sleep(10);
+ release?.();
+ await new Promise<void>((r) => (finishFirst = r));
+ log.push("a-");
+ });
+ const second = q.run("k", async () => void log.push("b"));
+ await second;
+ expect(log).toEqual(["a+", "b"]);
+ let firstDone = false;
+ void first.then(() => (firstDone = true));
+ await sleep(5);
+ expect(firstDone).toBe(false);
+ finishFirst();
+ await first;
+ expect(log).toEqual(["a+", "b", "a-"]);
+ });
+
+ it("outside a queued task there is nothing to release", () => {
+ expect(KeyedQueue.releaser()).toBeUndefined();
+ });
+});
+
+describe("control channel (host → amb-control plugin over the bridge)", () => {
+ let bridge: AgentBridge | undefined;
+ afterEach(async () => {
+ await bridge?.stop();
+ bridge = undefined;
+ });
+ const setup = async () => {
+ const ch = new ControlChannel();
+ bridge = new AgentBridge({ upstream: () => ({ baseUrl: "", apiKey: "" }), ask: async () => ({ id: "" }), wait: async () => ({}), cancel: async () => {}, control: { poll: (b, s) => ch.poll(b, s), result: (b) => ch.result(b) } });
+ await bridge.start();
+ const post = async (p: string, body: unknown, token = bridge!.token) =>
+ fetch(bridge!.url + p, { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json" }, body: JSON.stringify(body) });
+ return { ch, post };
+ };
+
+ it("a command reaches the waiting poll, its result comes back; nothing in either carries the bridge token", async () => {
+ const { ch, post } = await setup();
+ const id = ch.reset();
+ expect(ch.connected).toBe(false);
+ const polling = post("/dsh/control/poll", { botId: "b", controlId: id });
+ await sleep(50);
+ expect(ch.connected).toBe(true);
+ const sent = ch.send("steer", { sessionId: "s1", text: "hi" }, 5000);
+ const got = (await (await polling).json()) as any;
+ expect(got.commands).toEqual([{ sessionId: "s1", text: "hi", id: expect.any(String), op: "steer" }]);
+ expect(JSON.stringify(got)).not.toContain(bridge!.token);
+ expect((await post("/dsh/control/result", { botId: "b", controlId: id, id: got.commands[0].id, ok: true, result: { accepted: true } })).status).toBe(200);
+ expect(await sent).toEqual({ accepted: true });
+ });
+
+ it("queued commands wait for the next poll; a failed command rejects with the plugin's error", async () => {
+ const { ch, post } = await setup();
+ const id = ch.reset();
+ const sent = ch.send("providers", {}, 5000);
+ const got = (await (await post("/dsh/control/poll", { botId: "b", controlId: id })).json()) as any;
+ expect(got.commands.map((c: any) => c.op)).toEqual(["providers"]);
+ const failed = expect(sent).rejects.toThrow("llm-pi-ai entry not found");
+ await post("/dsh/control/result", { botId: "b", controlId: id, id: got.commands[0].id, ok: false, error: "llm-pi-ai entry not found" });
+ await failed;
+ });
+
+ it("a restarted child gets a new id: the old one's poll is told to stop, its pending commands fail at once", async () => {
+ const { ch, post } = await setup();
+ const old = ch.reset();
+ const sent = ch.send("cancel", { sessionId: "s" }, 60_000);
+ const fresh = ch.reset();
+ await expect(sent).rejects.toThrow("代理进程已重启");
+ expect(fresh).not.toBe(old);
+ expect(await (await post("/dsh/control/poll", { botId: "b", controlId: old })).json()).toEqual({ stop: true });
+ // 子进程退出:通道关闭,发不出去
+ ch.close();
+ await expect(ch.send("ping", {}, 1000)).rejects.toThrow("代理进程没有运行");
+ });
+
+ it("no answer within the timeout: the command fails and is not delivered later", async () => {
+ const { ch, post } = await setup();
+ const id = ch.reset();
+ await expect(ch.send("ping", {}, 30)).rejects.toThrow("代理进程没有响应(ping)");
+ const r = post("/dsh/control/poll", { botId: "b", controlId: id });
+ await sleep(50);
+ ch.close();
+ expect(((await (await r).json()) as any).commands).toEqual([]);
+ });
+
+ it("requests without the bridge token are refused", async () => {
+ const { ch, post } = await setup();
+ const id = ch.reset();
+ expect((await post("/dsh/control/poll", { botId: "b", controlId: id }, "wrong")).status).toBe(401);
+ });
+});
+
+describe("activity: background commands and steered messages", () => {
+ const T0 = 1_800_000_000_000;
+ const promoted = (id: string) => ({ message: { toolCallId: "c1", content: [{ type: "text", text: `[still running after 30000ms; moved to background job ${id}]\nThe command keeps running in the background.` }], isError: false } });
+
+ it("a command moved to the background gets its own step; it ends during the turn (job/settled) or is noted as still running", () => {
+ expect(promotedJob(promoted("bash-1"))).toBe("bash-1");
+ expect(promotedJob({ message: { content: [{ type: "text", text: "started background job bash-2" }] } })).toBeUndefined();
+ const log = new ActivityLog();
+ log.apply("tool/call", { callId: "c1", name: "bash", arguments: '{"command":"npm test"}' }, T0);
+ log.apply("tool/result", promoted("bash-1"), T0 + 30_000);
+ expect(log.steps.map((s) => [s.kind, s.title, s.status])).toEqual([
+ ["tool", "bash", "ok"],
+ ["job", "后台命令", "running"],
+ ]);
+ expect(describeStep(log.last!)).toBe("后台命令在运行:npm test");
+ log.apply("job/settled", { jobId: "bash-1", status: "completed", detail: "exit code: 1", finishedAt: T0 + 50_000 }, T0 + 50_000);
+ expect(log.last).toMatchObject({ kind: "job", status: "error", ms: 20_000, output: "exit code: 1" });
+
+ const still = new ActivityLog();
+ still.apply("tool/call", { callId: "c1", name: "bash", arguments: '{"command":"make"}' }, T0);
+ still.apply("tool/result", promoted("bash-3"), T0 + 30_000);
+ still.finish(T0 + 31_000);
+ expect(still.last).toMatchObject({ kind: "job", status: "ok", summary: "make (回复时仍在后台运行)" });
+ });
+
+ it("the wake-up turn shows the job's end from DSH's notice; a steered message is a step of its own", () => {
+ const log = new ActivityLog();
+ log.apply("user/message", { role: "user", content: [{ type: "text", text: "background job bash-9 (bash: make) finished [status: completed, exit code: 0]. Read its output with job_output." }], source: { kind: "tool-jobs", form: "notice", summary: "bash make [status: completed]" } }, T0);
+ expect(log.last).toMatchObject({ id: "j:bash-9", kind: "job", title: "后台命令结束", summary: "bash make [status: completed]", status: "ok" });
+ log.apply("user/message", { role: "user", content: [{ type: "text", text: "hi" }], source: { kind: "user" } }, T0 + 1);
+ expect(log.steps).toHaveLength(1);
+ log.apply("steer", { text: "also check the README" }, T0 + 2);
+ expect(log.last).toMatchObject({ kind: "steer", title: "追加消息", summary: "also check the README", status: "ok" });
+ expect(describeStep(log.last!)).toBe("收到追加消息:also check the README");
+ });
+});
+
+describe("the reply of a turn", () => {
+ it("only complete answers count: no tool calls, not interrupted", () => {
+ const msg = (content: unknown[], extra = {}) => ({ message: { role: "assistant", content }, ...extra });
+ expect(answerText(msg([{ type: "text", text: " done " }]))).toBe("done");
+ expect(answerText(msg([{ type: "text", text: "let me look" }, { type: "tool-call", id: "c", name: "bash", arguments: "{}" }]))).toBe("");
+ expect(answerText(msg([{ type: "text", text: "half" }], { interrupted: true }))).toBe("");
+ expect(answerText(msg([{ type: "reasoning", text: "hmm" }]))).toBe("");
+ });
+});
+
+describe("runtime: a message sent while an agent turn runs", () => {
+ const ev = (text: string): InboundEvent => ({ id: Math.random().toString(36), channel: "web", botId: "b1", conversation: { kind: "dm", id: "console" }, sender: { id: "owner" }, parts: [{ type: "text", text }], replyHandle: {}, receivedAt: Date.now() });
+ function setup(steerable: boolean) {
+ const store = new Store(":memory:");
+ store.createBot({ id: "b1", name: "测试" });
+ const turns = new Map<string, any>();
+ const steered: string[] = [];
+ const finishers: Array<(v: unknown) => void> = [];
+ let runs = 0;
+ const agent = {
+ available: true,
+ host: () => ({ state: "running", workspace: os.tmpdir(), diagnostics: () => ({}) }),
+ approvals: { bindSink: () => () => {}, handleReply: () => ({ handled: false }), pending: () => [] },
+ turnsInProgress: turns,
+ settings: () => ({ ...DEFAULT_SETTINGS, memory: { ...DEFAULT_SETTINGS.memory, enabled: false } }),
+ memoryPrefix: () => "",
+ takeSeed: () => "",
+ canSteer: (conv: string) => steerable && turns.has(conv),
+ steer: async (_bot: string, _conv: string, m: { text: string }) => (steered.push(m.text), true),
+ runTurn: (_bot: string, input: any) => {
+ runs++;
+ turns.set(input.conversationId, { since: Date.now(), lastProgress: Date.now(), activity: { steps: [] } });
+ input.observer?.onStart?.();
+ return new Promise((r) => finishers.push(r)).finally(() => turns.delete(input.conversationId));
+ },
+ stopTurn: () => false,
+ } as unknown as AgentManager;
+ const models = { listModels: async () => ["m1"], streamChat: async () => "纯对话" };
+ const rt = new BotRuntime({ store, models: models as any, defaultModel: () => "m1", contextMessages: 20, contextChars: 20_000, agent });
+ const q = new KeyedQueue();
+ const send = (text: string) => {
+ const ops: OutboundOp[] = [];
+ const done = q.run("web:b1:console", () => rt.handle(ev(text), { apply: async (op) => void ops.push(op) }));
+ return { ops, done, texts: () => ops.filter((o): o is Extract<OutboundOp, { text: string }> => "text" in o).map((o) => o.text) };
+ };
+ const history = () => store.listMessages("b1:web:console").map((m) => [m.role, m.content]);
+ return { rt, send, steered, finishers, runs: () => runs, history };
+ }
+ const reply = { text: "一起回答", sessionId: "S", files: [], usage: { tokens: 1, window: 10, maxTokens: 1 } };
+
+ it("is steered into it: the channel queue gives way once the agent took the first, one reply, history in order", async () => {
+ const s = setup(true);
+ const a = s.send("第一条");
+ await until(() => s.finishers.length === 1);
+ const b = s.send("补充一句");
+ await until(() => b.texts().length > 0);
+ expect(b.texts()).toEqual([STEER_ACK]);
+ expect(s.steered).toEqual(["补充一句"]);
+ // 第二条等第一条的回合结束才算处理完(网页 live 登记里它一直在)
+ let bDone = false;
+ void b.done.then(() => (bDone = true));
+ await new Promise((r) => setTimeout(r, 20));
+ expect(bDone).toBe(false);
+ s.finishers[0](reply);
+ await a.done;
+ await b.done;
+ expect(s.runs()).toBe(1);
+ expect(a.texts().at(-1)).toBe("一起回答");
+ expect(s.history()).toEqual([
+ ["user", "第一条"],
+ ["user", "补充一句"],
+ ["assistant", "一起回答"],
+ ]);
+ });
+
+ it("can't be steered (turn finishing, channel down): it waits and runs as its own turn afterwards", async () => {
+ const s = setup(false);
+ const a = s.send("第一条");
+ await until(() => s.finishers.length === 1);
+ const b = s.send("第二条");
+ await new Promise((r) => setTimeout(r, 30));
+ expect(s.runs()).toBe(1);
+ s.finishers[0](reply);
+ await a.done;
+ await until(() => s.finishers.length === 2);
+ s.finishers[1]({ ...reply, text: "第二个回答" });
+ await b.done;
+ expect(s.steered).toEqual([]);
+ expect(s.history()).toEqual([
+ ["user", "第一条"],
+ ["assistant", "一起回答"],
+ ["user", "第二条"],
+ ["assistant", "第二个回答"],
+ ]);
+ });
+
+ it("commands never steer: /model waits for the running turn", async () => {
+ const s = setup(true);
+ const a = s.send("第一条");
+ await until(() => s.finishers.length === 1);
+ const b = s.send("/help");
+ await new Promise((r) => setTimeout(r, 30));
+ expect(b.texts()).toEqual([]);
+ s.finishers[0](reply);
+ await a.done;
+ await b.done;
+ expect(b.texts()[0]).toMatch(/^可用命令/);
+ expect(s.steered).toEqual([]);
+ });
+});
diff --git a/test/pwsh-private.unit.test.ts b/test/pwsh-private.unit.test.ts
index 24cb2ba..8c7b1d4 100644
--- a/test/pwsh-private.unit.test.ts
+++ b/test/pwsh-private.unit.test.ts
@@ -563,6 +563,7 @@ describe("代理进程:PowerShell 换了就在空闲时重启;pwsh 工具结
};
host.ensure = async () => ({});
host.sessionFor = async () => session;
+ host.useRoute = async () => true;
const r = await host.runTurn({ conversationId: "c", text: "hi", model: "m" });
expect(r.text).toBe("done");
expect(seen).toHaveBeenCalledTimes(1);
diff --git a/test/server-compaction.unit.test.ts b/test/server-compaction.unit.test.ts
index 8cb9565..ce55f95 100644
--- a/test/server-compaction.unit.test.ts
+++ b/test/server-compaction.unit.test.ts
@@ -766,7 +766,7 @@ describe("服务端压缩:活动记录与模型资料", () => {
expect(mgr.serverCompaction).toBeDefined();
store.setAgentSession("c1", "b1", "sess-1");
const activity = new ActivityLog();
- mgr.turnsInProgress.set("c1", { botId: "b1", since: 0, lastProgress: 0, activity, abort: new AbortController(), tools: new Set(), media: 0 });
+ mgr.turnsInProgress.set("c1", { botId: "b1", since: 0, lastProgress: 0, activity, abort: new AbortController(), tools: new Set(), jobs: new Set(), media: 0 });
(mgr as any).onServerCompaction({ session: "sess-1", model: "mf/good", ok: true, before: 210_000, after: 31_000, at: 1000, ms: 4200 });
(mgr as any).onServerCompaction({ session: "sess-1", model: "mf/good", ok: false, before: 210_000, at: 2000, ms: 10, error: "HTTP 404" });
(mgr as any).onServerCompaction({ session: "other", model: "mf/good", ok: true, before: 1, after: 1, at: 3000, ms: 1 });
diff --git a/ui/components/activity.js b/ui/components/activity.js
index f9bb0bf..6941c6d 100644
--- a/ui/components/activity.js
+++ b/ui/components/activity.js
@@ -5,7 +5,7 @@ import { useEffect, useState } from "preact/hooks";
import { html, Icon } from "./ui.js";
import { fmtDur } from "../lib/format.js";
-const KIND_ICON = { model: "spark", tool: "code", retry: "refresh", subagent: "chat", compact: "database", error: "alert" };
+const KIND_ICON = { model: "spark", tool: "code", retry: "refresh", subagent: "chat", compact: "database", error: "alert", job: "clock", steer: "chat" };
const DOT = { ok: "ok", error: "err", running: "warn" };
/** 步骤用时:不到 1 秒显示一位小数 */
@@ -20,6 +20,7 @@ export function currentStep(steps) {
if (s.kind === "tool") return `运行 ${s.title}`;
if (s.kind === "subagent") return "子代理在工作";
if (s.kind === "compact") return "压缩上下文";
+ if (s.kind === "job") return "后台命令在运行";
return s.title;
}
diff --git a/ui/main.js b/ui/main.js
index 06479f6..e612bee 100644
--- a/ui/main.js
+++ b/ui/main.js
@@ -325,7 +325,7 @@ function App() {
<main class="main">
${view === "chat" && bot && html`<${ChatScreen} key=${bot.id} bot=${bot} models=${models} modelsError=${modelsError} reloadModels=${reloadModels}
defaultModel=${settings && settings.defaultModel} infoOpen=${infoOpen} onToggleInfo=${() => setInfoOpen((v) => !v)}
- onWorking=${onWorking} pending=${pending[bot.id] || 0} onOpenSettings=${openSettings} onOpenBotSettings=${openBotSettings} onBotUpdated=${onBotUpdated} />`}
+ onWorking=${onWorking} pending=${pending[bot.id] || 0} serverBusy=${!!agentWorking[bot.id]} onOpenSettings=${openSettings} onOpenBotSettings=${openBotSettings} onBotUpdated=${onBotUpdated} />`}
${(view === "new" || (view === "chat" && bots && !bots.length)) && html`<${NewBotScreen} first=${!!bots && !bots.length}
onCreated=${(b) => { reloadBots(); openChat(b.id); }} onCancel=${bots && bots.length ? () => openChat() : null} />`}
${view === "settings" && html`<${SettingsScreen} tab=${route.tab} query=${settingsQ} onQuery=${setSettingsQ} onTab=${(t) => openSettings(t)} focusRow=${route.row}
diff --git a/ui/screens/chat.js b/ui/screens/chat.js
index aac3255..2c6190d 100644
--- a/ui/screens/chat.js
+++ b/ui/screens/chat.js
@@ -39,12 +39,13 @@ function toItems(messages) {
);
}
-// 进行中的一条消息(服务端的 live 快照):用户消息 + 到目前为止的回复;turn 标记这些项属于哪条进行中的消息
+// 进行中的一条消息(服务端的 live 快照):用户消息 + 到目前为止的回复;turn 标记这些项属于哪条进行中的消息。
+// 代理自己醒来的一轮(后台命令结束)没有用户消息;并进前一条处理的消息没有自己的回复
function liveItems(t, cards = []) {
return [
- { kind: "msg", id: "u" + t.id, role: "user", text: t.userText, at: t.startedAt, turn: t.id },
+ ...(t.userText ? [{ kind: "msg", id: "u" + t.id, role: "user", text: t.userText, at: t.startedAt, turn: t.id }] : []),
...cards.map((c) => ({ ...c, turn: t.id })),
- { kind: "msg", id: "a" + t.id, role: "assistant", text: t.text, at: t.startedAt, live: true, startedAt: t.startedAt, activity: t.steps, turn: t.id },
+ ...(t.steered ? [] : [{ kind: "msg", id: "a" + t.id, role: "assistant", text: t.text, at: t.startedAt, live: true, startedAt: t.startedAt, activity: t.steps, turn: t.id }]),
...t.files.map((name, i) => ({ kind: "file", id: `f${t.id}-${i}`, name, turn: t.id })),
];
}
@@ -188,7 +189,7 @@ function ErrorNotice({ text, onRetry, onFixKey }) {
${onRetry && html`<button type="button" class="btn" onClick=${onRetry}>重试</button>`}`}>${auth ? "Modelflare 密钥失效或已被撤销。" : text}<//>`;
}
-export function ChatScreen({ bot, models, modelsError, reloadModels, defaultModel, infoOpen, onToggleInfo, onWorking, onOpenSettings, onOpenBotSettings, onBotUpdated, pending = 0 }) {
+export function ChatScreen({ bot, models, modelsError, reloadModels, defaultModel, infoOpen, onToggleInfo, onWorking, onOpenSettings, onOpenBotSettings, onBotUpdated, pending = 0, serverBusy = false }) {
const [items, setItems] = useState(null);
const [convModel, setConvModel] = useState("");
const [text, setText] = useState("");
@@ -216,6 +217,8 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
const lastUser = useRef("");
const convId = useRef("");
const attachRef = useRef(null);
+ // 处理中又发的消息(并进正在进行的回复)结束后重新读一次历史:存下的顺序是两条用户消息、一条回复
+ const reloadAfter = useRef(false);
// ended:刚接着的那条消息结束了 { since, ms },存下的回复照样显示"用时"(和一直停在这页时一样)
const load = useCallback(async (ended) => {
@@ -257,6 +260,10 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
api(`/bots/${encodeURIComponent(bot.id)}/settings`).then((d) => mounted.current && setPerm(d.settings.permission)).catch(() => {});
}, [bot.id]);
useEffect(() => onWorking(bot.id, generating), [generating]);
+ // 服务端开始处理这个机器人的消息,而本页没在跟(代理自己醒来的一轮、别的窗口发的):读一次,接上进行中的那条
+ useEffect(() => {
+ if (serverBusy && !following.current && mounted.current) load();
+ }, [serverBusy]);
useEffect(() => () => onWorking(bot.id, false), []);
// 只有用户本来就在底部附近时才跟随新内容;否则亮出"回到底部"
@@ -287,7 +294,7 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
* 切走了(组件卸载、事件流被断开)返回 null。liveId 是回复那一项,turn 是这条消息的几项共用的标记。
*/
const follow = async (liveId, turn, open) => {
- const f = { ac: new AbortController(), local: false };
+ const f = { ac: new AbortController(), local: false, turn };
following.current = f;
setGenerating(true);
let finalText = "";
@@ -337,6 +344,7 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
if (following.current) return;
stick.current = true;
const finalText = await follow("a" + t.id, t.id, (h, signal) => attachChat(bot.id, t.id, h, signal));
+ reloadAfter.current = false;
if (finalText === null) return;
if (isError(finalText)) {
setCue({ kind: "error", at: Date.now() });
@@ -346,8 +354,37 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
};
attachRef.current = attach;
+ /**
+ * 回复还在进行时又发一条(不是命令):交给服务端并进正在进行的处理,模型下一步就看到它,回复在那一条里一起给。
+ * 用户消息先插在进行中的回复上面;服务端没能并入(那一轮刚好结束)时它按普通消息处理,结束后重新读历史。
+ */
+ const steer = async (t) => {
+ setText("");
+ requestAnimationFrame(() => autoGrow(ta.current, LINE_PX * 8 + 8));
+ lastUser.current = t;
+ stick.current = true;
+ const turn = following.current?.turn;
+ setItems((l) => {
+ const list = l || [];
+ const user = { kind: "msg", id: uid(), role: "user", text: t, at: Date.now(), enter: true, turn };
+ const i = list.findIndex((x) => x.live && x.turn === turn);
+ return i < 0 ? [...list, user] : [...list.slice(0, i), user, ...list.slice(i)];
+ });
+ reloadAfter.current = true;
+ try {
+ await streamChat(bot.id, t, {}, new AbortController().signal);
+ } catch (e) {
+ failed(e);
+ }
+ if (mounted.current && !following.current && reloadAfter.current) {
+ reloadAfter.current = false;
+ load();
+ }
+ };
+
const send = async (raw) => {
const t = (raw ?? text).trim();
+ if (t && (generating || following.current) && !t.startsWith("/") && raw === undefined) return steer(t);
if (!t || generating || following.current) return;
if (raw === undefined) {
setText("");
@@ -361,6 +398,10 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
const finalText = await follow(liveId, liveId, (h, signal) => streamChat(bot.id, t, h, signal));
if (finalText === null) return;
if (isError(finalText)) setCue({ kind: "error", at: Date.now() });
+ if (reloadAfter.current) {
+ reloadAfter.current = false;
+ return load();
+ }
setItems((l) => {
const out = [];
for (const item of l || []) {
@@ -580,6 +621,9 @@ export function ChatScreen({ bot, models, modelsError, reloadModels, defaultMode
onClick=${() => setMenu(menu === "model" ? null : "model")}>
<span class="v">${effective ? shortModel(effective) : "选择模型"}</span><${Icon} name="chevDown" />
</button>
+ ${generating && text.trim() && !text.trim().startsWith("/") && !stopping
+ ? html`<button type="button" class="send" aria-label="补充给正在进行的回复" title="补充给正在进行的回复(Enter)" onClick=${() => send()}><${Icon} name="up" size=${15} stroke=${2} /></button>`
+ : null}
${generating
? html`<button type="button" class="send stop" aria-label=${stopping ? "正在停止…" : "停止生成(Esc)"} title=${stopping ? "正在停止…" : "停止(Esc)"}
disabled=${stopping} aria-busy=${stopping} onClick=${stop}>${stopping ? html`<${Spinner} />` : html`<${Icon} name="stop" size=${12} />`}</button>`