@njuptlzf/agent-chat-server 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/dist/branding-ui.d.ts +16 -0
  2. package/dist/branding-ui.js +119 -0
  3. package/dist/checkpoint-store.d.ts +78 -0
  4. package/dist/checkpoint-store.js +52 -0
  5. package/dist/components.d.ts +15 -0
  6. package/dist/components.js +50 -0
  7. package/dist/extension-admission.d.ts +32 -0
  8. package/dist/extension-admission.js +155 -0
  9. package/dist/flows.d.ts +49 -0
  10. package/dist/flows.js +208 -0
  11. package/dist/ledger.d.ts +90 -0
  12. package/dist/ledger.js +115 -0
  13. package/dist/main.js +14 -2
  14. package/dist/manifest.js +27 -1
  15. package/dist/orchestrator/engine.d.ts +132 -0
  16. package/dist/orchestrator/engine.js +1 -0
  17. package/dist/orchestrator/engines/fake.d.ts +14 -0
  18. package/dist/orchestrator/engines/fake.js +282 -0
  19. package/dist/orchestrator/engines/taskflow.d.ts +4 -0
  20. package/dist/orchestrator/engines/taskflow.js +541 -0
  21. package/dist/orchestrator/run-spec.d.ts +154 -0
  22. package/dist/orchestrator/run-spec.js +11 -0
  23. package/dist/persona-config.js +12 -0
  24. package/dist/server.d.ts +27 -2
  25. package/dist/server.js +595 -187
  26. package/dist/session.js +2 -1
  27. package/dist/sqlite-stores.d.ts +46 -6
  28. package/dist/sqlite-stores.js +292 -33
  29. package/dist/stores.d.ts +0 -36
  30. package/dist/stores.js +0 -27
  31. package/dist/suggestions.d.ts +60 -0
  32. package/dist/suggestions.js +107 -0
  33. package/dist/testing.d.ts +5 -4
  34. package/dist/testing.js +5 -4
  35. package/package.json +65 -64
  36. package/scripts/agentchat-integrate.mjs +852 -852
  37. package/src/branding-ui.ts +126 -0
  38. package/src/checkpoint-store.ts +122 -0
  39. package/src/components.ts +66 -0
  40. package/src/extension-admission.ts +131 -0
  41. package/src/flows.ts +244 -0
  42. package/src/ledger.ts +171 -0
  43. package/src/main.ts +16 -2
  44. package/src/manifest.ts +27 -1
  45. package/src/orchestrator/engine.ts +134 -0
  46. package/src/orchestrator/engines/fake.ts +330 -0
  47. package/src/orchestrator/engines/taskflow.ts +626 -0
  48. package/src/orchestrator/run-spec.ts +171 -0
  49. package/src/persona-config.ts +12 -1
  50. package/src/server.ts +611 -183
  51. package/src/session.ts +2 -1
  52. package/src/sqlite-stores.ts +343 -35
  53. package/src/stores.ts +0 -41
  54. package/src/suggestions.ts +146 -0
  55. package/src/testing.ts +4 -4
package/src/server.ts CHANGED
@@ -1,13 +1,14 @@
1
1
  import { createHash, randomUUID } from "node:crypto";
2
2
  import { createServer, type IncomingMessage, type Server, type ServerResponse } from "node:http";
3
- import type { ModelRuntime } from "@earendil-works/pi-coding-agent";
4
- import { PROTOCOL_VERSION, type BrandingResponse, type BillingUsageReport, type ChatEvent, type ChatRequest, type ErrorCode, type ProductManifest, type WhoamiResponse } from "@njuptlzf/agent-chat-protocol";
3
+ import type { FileEntry, ModelRuntime } from "@earendil-works/pi-coding-agent";
4
+ import { PROTOCOL_VERSION, type BrandingResponse, type BillingUsageReport, type ChatEvent, type ChatRequest, type ErrorCode, type ProductManifest, type TokenUsage, type WhoamiResponse } from "@njuptlzf/agent-chat-protocol";
5
5
  import { capsForPersona, limitsForPersona, poolSplit, type AuthzLike } from "./authz.ts";
6
6
  import type { McpServerDecl, PersonaUi } from "@njuptlzf/agent-chat-protocol";
7
7
  import { createEventMapper, createRunAccounting } from "./events.ts";
8
8
  import { agentFor, type LoadedProduct, personaIdsOf, type ToolContext } from "./manifest.ts";
9
9
  import { appendVersion, effectivePersonaVersion, PROMPT_MAX_CHARS, savePersonaConfig, validatePersona, versionEntry, versionsOf, type PersonaConfigStore, type PersonaOverride } from "./persona-config.ts";
10
10
  import { runPersonaTrial, TRIAL_MAX_INPUT_CHARS, type TrialInput, type TrialResult } from "./persona-trial.ts";
11
+ import { runSuggestions, clipMessages, SUGGESTIONS_DEFAULTS, type SuggestionsRunner } from "./suggestions.ts";
11
12
  import { MODEL_SWITCH } from "./audit.ts";
12
13
  import { FAILOVER_NOTICE, candidateKey, isRetryableModelError, orderChain, processCooldown, rawModelError, type CooldownRegistry } from "./model-failover.ts";
13
14
  import { resolveChain, resolveRunTier, saveModelConfig, type ModelCandidate, type ModelConfig, type ModelConfigStore } from "./model-config.ts";
@@ -15,7 +16,14 @@ import { MCP_SPEC_ALLOWLIST_ENV, checkMcpDecl, mergedToolLabels, probeMcpProxy,
15
16
  import { baseUrlFromModelsJson, deleteProvider, keyEnvOf, modelToProbe, patchProvider, patchRouting, patchTiers, refreshProvider, testProvider, toModelsResponse, type ApiError } from "./models-api.ts";
16
17
  import { buildCommands, compileSystemPrompt, createProductSession, disposeProductSession, expandCommand, exportHistory, loadProductSkills, narrowMcpExposure, type UIPromptAnswer } from "./session.ts";
17
18
  import { auditPageWindow, toHistoryMessages, truncateLastTurn } from "./stores.ts";
18
- import type { AccountStateStore, AuditEvent, AuditRow, AuditStore, Authenticator, BillingOrder, BillingStore, HistoryStore, MembershipStore, MemberPoolStore, QuotaGate, RunLogStore } from "./stores.ts";
19
+ import type { AccountStateStore, AuditEvent, AuditRow, AuditStore, Authenticator, BillingOrder, BillingStore, HistoryStore, MembershipStore, MemberPoolStore, RunLogStore } from "./stores.ts";
20
+ import type { Ledger } from "./ledger.ts";
21
+ import type { Engine, EngineEvent, JobContext, JobRunResult } from "./orchestrator/engine.ts";
22
+ import type { JobSpec, RunOutcome, RunSpec } from "./orchestrator/run-spec.ts";
23
+ import { createTaskflowEngine } from "./orchestrator/engines/taskflow.ts";
24
+ import type { CheckpointStore } from "./checkpoint-store.ts";
25
+ import { persistCheckpoints, resumeFrom as loadResumeFrom } from "./checkpoint-store.ts";
26
+ import { toRunSpec } from "./flows.ts";
19
27
 
20
28
  /**
21
29
  * 产品业务路由接缝。路由体住在 apps/<product>/server/routes.ts(manifest
@@ -66,6 +74,11 @@ export interface ServerDeps {
66
74
  * 测试注入替身,避免为了验证"旁路"而真的花钱。
67
75
  */
68
76
  trial?: (input: TrialInput) => Promise<TrialResult>;
77
+ /**
78
+ * 下一步建议生成器。omitted ⇒ 真调模型(`runSuggestions`);
79
+ * 测试注入替身,避免为了验证旁路而真的花钱。
80
+ */
81
+ suggestions?: SuggestionsRunner;
69
82
  /**
70
83
  * Optional 候选冷却表。omitted ⇒ 进程级默认实例(重启清空、不进 DB);
71
84
  * 测试注入自己的实例以隔离。
@@ -73,7 +86,23 @@ export interface ServerDeps {
73
86
  modelCooldown?: CooldownRegistry;
74
87
  toolFactory: (ctx: ToolContext) => unknown[];
75
88
  history: HistoryStore;
76
- quota: QuotaGate;
89
+ /**
90
+ * 预留-结算额度账本(M-14 W3)。替换了旧的 `QuotaGate.check/record`:
91
+ * 先占位再开跑,并发下不会超发(旧模型实测超发 140%)。
92
+ */
93
+ ledger: Ledger;
94
+ /**
95
+ * 编排引擎。omitted ⇒ 默认 `taskflow` 适配器(W1:单 phase 通路)。
96
+ * 测试注入 `createFakeEngine()`,**不建 pi 会话、不调模型**——
97
+ * 与 `trial` / `suggestions` 的替身模式同源。
98
+ */
99
+ engine?: Engine;
100
+ /**
101
+ * 多 phase Run 的断点存储(W4)。omitted ⇒ resume 不可用(带 `resumeRunId`
102
+ * 的请求 400);主链 main.ts 恒装配 SQLite 版,测试注入 Memory 版。
103
+ * 单 phase 路径根本不读它,所以缺它不影响档 1 的任何行为。
104
+ */
105
+ checkpoints?: CheckpointStore;
77
106
  authenticate: Authenticator;
78
107
  dataDir: string;
79
108
  /** Optional per-run log backing GET /api/usage (-11). */
@@ -1045,6 +1074,8 @@ async function handleAdminPersonas(
1045
1074
  problems: cfg.problems,
1046
1075
  // 默认助手的 id:页面用它在列表里显示「默认助手」并区分展示字段的落点。
1047
1076
  defaultPersona: product.defaultPersona,
1077
+ // 下一步建议的全局模式(只读):全局 off 是硬地板,页面据此禁用人格级下拉。
1078
+ globalFollowUpMode: product.manifest.branding.ui?.followUp?.mode ?? "llm",
1048
1079
  personas: ids.map((id) => {
1049
1080
  const override = cfg.personas[id];
1050
1081
  // 生效文案(含运行时覆盖)——列表徽标与名称要用"用户看到的",不是 manifest 原文。
@@ -1532,6 +1563,28 @@ export function createChatServer(deps: ServerDeps): Server {
1532
1563
  // 定死——用户投诉「昨天不是这么说的」时,要查的是**当时生效的那一版**,不是现在这版。
1533
1564
  const personaVersion = deps.personaConfig ? effectivePersonaVersion(deps.personaConfig.get(), persona) : undefined;
1534
1565
 
1566
+ // 多 phase 入口(W4):`flow` 指向 manifest 声明的 `agent.flows.<id>`。
1567
+ // 不传 = 单 phase 老路径(档 1 字节流不变)。传了但 id 不存在 = 400(fail-closed,
1568
+ // 不让一个拼错的流程名静默退化成单助手回答——用户会以为流程跑过了)。
1569
+ if (body.flow !== undefined && (typeof body.flow !== "string" || !/^[a-z][a-z0-9_-]*$/.test(body.flow))) {
1570
+ return sendError(res, 400, "bad_request", "`flow` has an invalid format");
1571
+ }
1572
+ const flow = body.flow;
1573
+ if (flow !== undefined && !product.manifest.agent.flows?.[flow]) {
1574
+ return sendError(res, 400, "bad_request", `unknown flow "${flow}"`);
1575
+ }
1576
+ // resume 归属:`runId` 形如 `<sessionId>:<timestamp>`,前缀必须是**本会话**,
1577
+ // 否则等于用一个猜来的 id 把别人的产物塞进自己的回答(跨会话注入)。
1578
+ // 只在 flow 请求上有意义——单 phase 没有 phase checkpoint 可恢复。
1579
+ if (body.resumeRunId !== undefined) {
1580
+ if (flow === undefined) {
1581
+ return sendError(res, 400, "bad_request", "`resumeRunId` requires `flow`");
1582
+ }
1583
+ if (typeof body.resumeRunId !== "string" || !body.resumeRunId.startsWith(`${sessionId}:`)) {
1584
+ return sendError(res, 400, "bad_request", "`resumeRunId` does not belong to this session");
1585
+ }
1586
+ }
1587
+
1535
1588
  // A leading "/" is UI semantics: expand it here so the command name (and the
1536
1589
  // skill's file path) never reaches the model. Checked before quota/billing so
1537
1590
  // a typo costs nothing and never opens a run. The surface is the scope's: an
@@ -1539,6 +1592,39 @@ export function createChatServer(deps: ServerDeps): Server {
1539
1592
  const surface = surfaceOf(scope);
1540
1593
  const expanded = expandCommand(body.message, surface.commands, surface.skills);
1541
1594
  if ("error" in expanded) return sendError(res, 400, "bad_request", expanded.error);
1595
+ // prompt 文本在**建 RunSpec 之前**就要定下来:RunSpec 的 `JobSpec.task` 会被
1596
+ // 引擎的 validate 检查「agent phase 必须有 task」,算晚了就恒被拒(见下方注释)。
1597
+ // 附件/上下文的编织是纯函数,提前算不改变任何行为。
1598
+ const promptText = withContext(withAttachments(expanded.message, att.attachments), body.context);
1599
+
1600
+ // ---- RunSpec 提前构造(W4)----
1601
+ // 提前到 reserve / writeHead 之前,是为了让 flow 的非法声明与无断点 resume
1602
+ // 能以 **400** 拒绝(开了 SSE 流就改不了状态码了)。档 1(无 flow)的形状
1603
+ // 与接入编排层之前逐字相同——这是 I8 字节流基线的另一半保证。
1604
+ const startedAt = new Date();
1605
+ const runId = `${sessionId}:${startedAt.getTime()}`;
1606
+ const engine = deps.engine ?? createTaskflowEngine({ cwd: deps.dataDir });
1607
+
1608
+ const translated: RunSpec | { readonly error: string } =
1609
+ flow === undefined
1610
+ ? { runId, phases: [{ id: "chat", kind: "agent", job: { persona, task: promptText }, final: true }] }
1611
+ : toRunSpec(product, flow, promptText, runId);
1612
+ // flow 声明的 phase 形状非法 ⇒ 400 fail-closed,而不是回退成单助手回答让用户误以为跑过。
1613
+ // (启动期 validateFlows 已拦过绝大多数;这里是第二道闸,也覆盖运行时注入。)
1614
+ if ("error" in translated) return sendError(res, 400, "bad_request", translated.error);
1615
+ let runSpec: RunSpec = translated;
1616
+ if (flow !== undefined && body.resumeRunId !== undefined) {
1617
+ if (!deps.checkpoints) {
1618
+ return sendError(res, 400, "bad_request", "resume is unavailable (no checkpoint store configured)");
1619
+ }
1620
+ const cp = await loadResumeFrom(deps.checkpoints, body.resumeRunId);
1621
+ if (!cp) return sendError(res, 400, "bad_request", "`resumeRunId` has no checkpoints to resume from");
1622
+ runSpec = { ...translated, resumeFrom: cp };
1623
+ }
1624
+ if (flow !== undefined) {
1625
+ const report = engine.validate(runSpec);
1626
+ if (!report.ok) return sendError(res, 400, "bad_request", `invalid flow: ${report.errors.join("; ")}`);
1627
+ }
1542
1628
 
1543
1629
  // 角色决定当日上限与池。authz 未装配 = 全员
1544
1630
  // user、caps 不传(现状);池只在会员在期时 lazy 发满当月。
@@ -1551,23 +1637,31 @@ export function createChatServer(deps: ServerDeps): Server {
1551
1637
  const memberActive = memberUntil !== null && Date.parse(memberUntil) > Date.now();
1552
1638
  const personaMember = !!deps.authz && (roles.includes("admin") || memberActive);
1553
1639
  const poolCfg = deps.memberPool && memberActive ? (limitsForPersona(limits, persona).memberMonthlyTokenPool ?? 0) : 0;
1554
- const decision = await deps.quota.check(userId, deps.authz ? capsForPersona(limits, persona, personaMember) : undefined, persona);
1555
- if (!decision.ok) return sendError(res, 429, "quota_exceeded", decision.reason);
1640
+ const caps = deps.authz ? capsForPersona(limits, persona, personaMember) : undefined;
1641
+ // 预留(I4):占位成功才允许调模型。operationId 一请求一个,是幂等键(I6)。
1642
+ const operationId = randomUUID();
1643
+ const reserved = await deps.ledger.reserve({ userId, persona, caps, operationId });
1644
+ if (!reserved.ok) return sendError(res, 429, "quota_exceeded", reserved.reason);
1556
1645
 
1557
1646
  // Billing gate: starting costs money, so a depleted (or never topped-up) balance stops here —
1558
1647
  // 但池内免余额:只要当月池还有剩,余额见底也能开跑,池尽才提示充值。
1648
+ // 以下三条早退都在**占位之后**,所以必须先归还占位——否则用户被 402/409 拒一次,
1649
+ // 额度要被白占一小时(预留 TTL)。
1559
1650
  if (billingActive && poolCfg <= 0 && (await deps.billing!.balance(userId)) <= 0) {
1651
+ await deps.ledger.release(operationId);
1560
1652
  return sendError(res, 402, "payment_required", "余额不足,请先充值");
1561
1653
  }
1562
1654
  if (billingActive && poolCfg > 0) {
1563
1655
  const row = await deps.memberPool!.getOrGrant(userId, persona, monthNow(), poolCfg);
1564
1656
  if (Math.max(0, row.granted - row.used) <= 0 && (await deps.billing!.balance(userId)) <= 0) {
1657
+ await deps.ledger.release(operationId);
1565
1658
  return sendError(res, 402, "payment_required", "本月发放池已用尽且余额不足,请先充值");
1566
1659
  }
1567
1660
  }
1568
1661
 
1569
1662
  const runKey = `${userId}\u0000${sessionId}`;
1570
1663
  if (running.has(runKey)) {
1664
+ await deps.ledger.release(operationId);
1571
1665
  return sendError(res, 409, "bad_request", "this session already has a run in progress");
1572
1666
  }
1573
1667
  running.add(runKey);
@@ -1593,7 +1687,6 @@ export function createChatServer(deps: ServerDeps): Server {
1593
1687
  let aborted = false;
1594
1688
  let limitSent = false;
1595
1689
  let runError: string | undefined;
1596
- const startedAt = new Date();
1597
1690
  const confirms = new Map<string, (allow: boolean) => void>();
1598
1691
  pendingRuns.set(runKey, confirms);
1599
1692
  const uiPrompts = new Map<string, (answer: UIPromptAnswer) => void>();
@@ -1609,205 +1702,435 @@ export function createChatServer(deps: ServerDeps): Server {
1609
1702
  }
1610
1703
  }
1611
1704
 
1612
- try {
1613
- send({ type: "ready", v: PROTOCOL_VERSION, sessionId, product: product.manifest.id });
1614
- // 工具中文名随首帧一次性下发(用户视角不显示 mcp__ 声明名)。
1615
- send({ type: "tools_meta", labels: mergedToolLabels(deps.mcpConfig?.get(), product.manifest.agent.toolLabels) });
1616
-
1617
- const history = await deps.history.load(userId, scope, sessionId);
1618
- // persona 与 createFeedback 一起注入:产品据此决定造哪些工具,工具本身碰不到 store。
1619
- // 工单正文由服务端截断、补 ref、按 roles 定优先级——模型只能递内容,改不了归属。
1620
- const feedbackSink = deps.feedback as FeedbackSubmitSink | undefined;
1621
- const tools = deps.toolFactory({
1622
- productId: product.manifest.id,
1623
- productDir: product.dir,
1624
- userId,
1625
- dataDir: deps.dataDir,
1626
- qual: scope,
1627
- persona,
1628
- ...(feedbackSink
1629
- ? {
1630
- createFeedback: async (content: string) => {
1631
- const row = await feedbackSink.submit({
1632
- userId,
1633
- kind: "other",
1634
- content,
1635
- sessionId,
1636
- ref: `${persona}:${sessionId}`,
1637
- priority: roles.includes("member") || roles.includes("admin") ? "high" : "normal",
1705
+ // 单 phase 通路:把原来这一整段 try 体原样搬进 runSingleJob(缩进变了、语义没变)。
1706
+ // 为什么搬而不重写:里面的 pendingFailover 吞批、toolRoundsAtStart 判定、
1707
+ // limitSent 后 abort 都是逐次修出来的,重写必回退旧 bug。
1708
+ // runSingleJob 不发任何编排事件,SSE 字节流与接入编排层之前完全一致(I8)。
1709
+ let lastText = "";
1710
+ /** runSingleJob 是否已跑过——引擎抛错时据此判断要不要 fail-open 补跑一次。 */
1711
+ let started = false;
1712
+ const runSingleJob = async (): Promise<JobRunResult> => {
1713
+ started = true;
1714
+ try {
1715
+ send({ type: "ready", v: PROTOCOL_VERSION, sessionId, product: product.manifest.id });
1716
+ // 工具中文名随首帧一次性下发(用户视角不显示 mcp__ 声明名)。
1717
+ send({ type: "tools_meta", labels: mergedToolLabels(deps.mcpConfig?.get(), product.manifest.agent.toolLabels) });
1718
+
1719
+ const history = await deps.history.load(userId, scope, sessionId);
1720
+ // persona 与 createFeedback 一起注入:产品据此决定造哪些工具,工具本身碰不到 store。
1721
+ // 工单正文由服务端截断、补 ref、按 roles 定优先级——模型只能递内容,改不了归属。
1722
+ const feedbackSink = deps.feedback as FeedbackSubmitSink | undefined;
1723
+ const tools = deps.toolFactory({
1724
+ productId: product.manifest.id,
1725
+ productDir: product.dir,
1726
+ userId,
1727
+ dataDir: deps.dataDir,
1728
+ qual: scope,
1729
+ persona,
1730
+ ...(feedbackSink
1731
+ ? {
1732
+ createFeedback: async (content: string) => {
1733
+ const row = await feedbackSink.submit({
1734
+ userId,
1735
+ kind: "other",
1736
+ content,
1737
+ sessionId,
1738
+ ref: `${persona}:${sessionId}`,
1739
+ priority: roles.includes("member") || roles.includes("admin") ? "high" : "normal",
1740
+ });
1741
+ return { id: row.id ?? 0 };
1742
+ },
1743
+ }
1744
+ : {}),
1745
+ });
1746
+
1747
+ // ---- 失败切换(设计稿):run 开始定一条有序候选链,模型调用失败换下一条。
1748
+ // 链来自 model-config 的档位绑定;没配置就是 manifest 的单模型(链长 1 ⇒ 永不切换)。
1749
+ const modelCfg = deps.modelConfig?.get();
1750
+ const cooldown = deps.modelCooldown ?? processCooldown;
1751
+ const chain = orderChain(resolveChain(product, body.tier, modelCfg), cooldown);
1752
+ const failoverOn = modelCfg?.routing.failover ?? true;
1753
+ const cooldownSeconds = modelCfg?.routing.cooldownSeconds ?? 120;
1754
+ const promptImages = parsed.images.length > 0 ? { images: parsed.images } : undefined;
1755
+ const buildSession = (candidate: ModelCandidate) =>
1756
+ createProductSession({
1757
+ product,
1758
+ modelRuntime: deps.modelRuntime,
1759
+ tools,
1760
+ userId,
1761
+ personaConfig: deps.personaConfig,
1762
+ dataDir: deps.dataDir,
1763
+ // 档位语义(label/thinking/multiplier)仍来自 manifest,只有物理模型跟着候选走。
1764
+ tier: candidate.provider === tier.provider && candidate.model === tier.model
1765
+ ? tier
1766
+ : { ...tier, provider: candidate.provider, model: candidate.model },
1767
+ qual: scope,
1768
+ persona,
1769
+ history,
1770
+ mcpConfig: deps.mcpConfig,
1771
+ requestConfirm: (info) =>
1772
+ new Promise<boolean>((resolve) => {
1773
+ const settle = (allow: boolean) => {
1774
+ clearTimeout(timer);
1775
+ confirms.delete(info.toolCallId);
1776
+ resolve(allow);
1777
+ };
1778
+ const timer = setTimeout(() => settle(false), CONFIRM_TIMEOUT_MS);
1779
+ confirms.set(info.toolCallId, settle);
1780
+ send({ type: "tool_confirm", toolCallId: info.toolCallId, name: info.name, args: info.args });
1781
+ }),
1782
+ // Extension dialogs (batch 3-4b): same pause-and-answer shape as the
1783
+ // confirm gate, but answered through POST /api/ui-response. Nobody
1784
+ // answering means the fail-closed default, never a hung run.
1785
+ requestUI: (info, signal) =>
1786
+ new Promise<UIPromptAnswer>((resolve) => {
1787
+ const settle = (answer: UIPromptAnswer) => {
1788
+ clearTimeout(timer);
1789
+ signal?.removeEventListener("abort", onAbort);
1790
+ uiPrompts.delete(info.id);
1791
+ resolve(answer);
1792
+ };
1793
+ const onAbort = () => settle({ cancelled: true });
1794
+ const timeoutMs = info.timeoutMs
1795
+ ? Math.min(Math.max(info.timeoutMs, UI_TIMEOUT_MIN_MS), UI_TIMEOUT_MAX_MS)
1796
+ : UI_TIMEOUT_MS;
1797
+ const timer = setTimeout(() => settle({ cancelled: true }), timeoutMs);
1798
+ signal?.addEventListener("abort", onAbort, { once: true });
1799
+ uiPrompts.set(info.id, settle);
1800
+ send({
1801
+ type: "ui_prompt",
1802
+ id: info.id,
1803
+ kind: info.kind,
1804
+ title: info.title,
1805
+ ...(info.message !== undefined ? { message: info.message } : {}),
1806
+ ...(info.options !== undefined ? { options: info.options } : {}),
1807
+ ...(info.placeholder !== undefined ? { placeholder: info.placeholder } : {}),
1638
1808
  });
1639
- return { id: row.id ?? 0 };
1640
- },
1809
+ }),
1810
+ onUINotify: (message, level) => send({ type: "ui_notify", message, level }),
1811
+ });
1812
+
1813
+ // Client went away: stop spending tokens(读的是当前那一次尝试的会话)。
1814
+ res.on("close", () => {
1815
+ if (!res.writableEnded) void session?.abort();
1816
+ });
1817
+
1818
+ for (let attempt = 0; ; attempt++) {
1819
+ const candidate = chain[attempt]!;
1820
+ session = await buildSession(candidate);
1821
+
1822
+ // 一次尝试内只有「还没吐字、还没跑工具」才允许换候选:重发会把已经流出去的
1823
+ // 半句话再发一遍,或把已经发生的工具副作用再执行一遍。这条边界是 v1 的口径。
1824
+ let pendingFailover: { message: string } | undefined;
1825
+ let attemptText = false;
1826
+ const toolRoundsAtStart = accounting.toolRounds();
1827
+
1828
+ session.subscribe((event) => {
1829
+ const outs = mapper.map(event);
1830
+ // 同一批里出现 model_error 且这次还能切:**整批吞掉**——包括那条空的
1831
+ // message_end,否则前端会先落一个空气泡再被真正的回答顶下去。
1832
+ const errOut = outs.find((o) => o.type === "error" && o.code === "model_error");
1833
+ if (errOut && errOut.type === "error") {
1834
+ // 判定用 pi 原始报错(rawModelError),不是折叠后的用户文案——文案里
1835
+ // 已经没有 JSON/网络词,拿它匹配会让 failover 失明(2026-11-04)。
1836
+ const canSwitch =
1837
+ failoverOn && attempt < chain.length - 1 && !attemptText && accounting.toolRounds() === toolRoundsAtStart;
1838
+ if (canSwitch && isRetryableModelError(rawModelError(event))) {
1839
+ // 用户不该看到一次注定会被掩盖的失败(也不显示模型名)。
1840
+ pendingFailover = { message: rawModelError(event) };
1841
+ void session?.abort();
1842
+ return;
1843
+ }
1641
1844
  }
1642
- : {}),
1643
- });
1845
+ for (const out of outs) {
1846
+ if (out.type === "text_delta" || out.type === "thinking_delta") attemptText = true;
1847
+ // 引擎的 RunOutcome.output 要能反映这轮回答;取最后一条 message_end 的全文。
1848
+ if (out.type === "message_end") lastText = out.text;
1849
+ if (out.type === "done") {
1850
+ if (pendingFailover) return; // abort 带出来的 done 不发——真正的收口在最后一次尝试
1851
+ doneSent = true;
1852
+ aborted = out.aborted;
1853
+ }
1854
+ accounting.observe(out);
1855
+ if (out.type === "tool_end") toolCalls[out.name] = (toolCalls[out.name] ?? 0) + 1;
1856
+ send(out);
1857
+ if (accounting.limitHit() && !limitSent) {
1858
+ // One prompt must not loop forever: stop it at the cap, with a reason.
1859
+ limitSent = true;
1860
+ send({
1861
+ type: "error",
1862
+ code: "limit_exceeded",
1863
+ message: `已达到单次运行 ${limits.maxToolRounds} 轮工具调用上限,已中止`,
1864
+ retryable: false,
1865
+ });
1866
+ void session?.abort();
1867
+ }
1868
+ }
1869
+ });
1870
+
1871
+ try {
1872
+ await session.prompt(promptText, promptImages);
1873
+ await session.waitForIdle();
1874
+ } catch (err) {
1875
+ // abort 引起的抛出是切换路径的一部分;没有待切换的错误就照旧冒泡。
1876
+ if (!pendingFailover) throw err;
1877
+ }
1878
+ if (!pendingFailover) break;
1644
1879
 
1645
- // ---- 失败切换(设计稿):run 开始定一条有序候选链,模型调用失败换下一条。
1646
- // 链来自 model-config 的档位绑定;没配置就是 manifest 的单模型(链长 1 ⇒ 永不切换)。
1647
- const modelCfg = deps.modelConfig?.get();
1648
- const cooldown = deps.modelCooldown ?? processCooldown;
1649
- const chain = orderChain(resolveChain(product, body.tier, modelCfg), cooldown);
1650
- const failoverOn = modelCfg?.routing.failover ?? true;
1651
- const cooldownSeconds = modelCfg?.routing.cooldownSeconds ?? 120;
1652
- const promptText = withContext(withAttachments(expanded.message, att.attachments), body.context);
1653
- const promptImages = parsed.images.length > 0 ? { images: parsed.images } : undefined;
1654
- const buildSession = (candidate: ModelCandidate) =>
1655
- createProductSession({
1880
+ // 换下一条:打冷却、留痕、告知用户,然后**重建会话**重发同一个 prompt。
1881
+ // 不用 session.setModel 原地重发——会话里已经记了这次失败的用户消息,重发会让
1882
+ // 历史多一条一模一样的提问;重建用的是 run 开始时那份 history,干净。
1883
+ cooldown.mark(candidate, cooldownSeconds);
1884
+ send({ type: "ui_notify", message: FAILOVER_NOTICE, level: "warning" });
1885
+ if (deps.audit) {
1886
+ void deps.audit
1887
+ .append({
1888
+ actor: userId,
1889
+ result: "ok",
1890
+ action: MODEL_SWITCH,
1891
+ target: `session:${sessionId}`,
1892
+ meta: { from: candidateKey(candidate), to: candidateKey(chain[attempt + 1]!), reason: pendingFailover.message.slice(0, 200) },
1893
+ })
1894
+ .catch((err) => console.error("[audit] model.switch failed (fail-open):", err));
1895
+ }
1896
+ disposeProductSession(session!);
1897
+ session = undefined;
1898
+ }
1899
+ } catch (err) {
1900
+ runError = err instanceof Error ? err.message : String(err);
1901
+ console.error("[chat] run failed:", err);
1902
+ send({
1903
+ type: "error",
1904
+ code: "internal",
1905
+ message: "服务暂时不可用,请稍后重试",
1906
+ retryable: true,
1907
+ });
1908
+ }
1909
+ return {
1910
+ output: lastText,
1911
+ ok: runError === undefined,
1912
+ usage: mapper.totalUsage(),
1913
+ stopReason: runError === undefined ? "end_turn" : "error",
1914
+ ...(runError !== undefined ? { error: runError } : {}),
1915
+ };
1916
+ };
1917
+
1918
+ // 一次 Run = 一个 agent phase。引擎只负责「校验 + 调 runJob + 汇总 usage」,
1919
+ // 真正的会话逻辑全在 runSingleJob 里,所以档 1 的行为与接入前逐字节一致。
1920
+ //
1921
+ // `JobSpec.task` 必须是真实的 prompt:它是 `RunSpec` 里唯一描述「这个 phase
1922
+ // 要干什么」的字段,引擎的静态校验会拒空 task。曾经这里是 `promptForEngine`
1923
+ // 变量、在 runSingleJob 内部才赋值,于是构造 runSpec 时它恒为空串 ⇒ validate
1924
+ // 恒拒 ⇒ 每次都静默 fail-open 回直连,**编排层在 W1/W2/W3 期间一次都没真正执行过**。
1925
+ // 由 `wire-baseline.test.ts` 的快照基线抓出来(它同时钉住档 1 的字节流)。
1926
+ const runAbort = new AbortController();
1927
+ const isFlow = flow !== undefined;
1928
+ /** 多 phase 的成员会话各自独立;档 1 恒为空(单 phase 仍用上面的 `session`)。 */
1929
+ const memberSessions: Array<Awaited<ReturnType<typeof createProductSession>>> = [];
1930
+ /** 入口 phase(无 dependsOn)才继承会话历史,其余成员从空上下文起跑。 */
1931
+ const entryPhaseIds = new Set(runSpec.phases.filter((p) => (p.dependsOn ?? []).length === 0).map((p) => p.id));
1932
+ /** 引擎收口产物;档 1 不用它,多 phase 的 usage/run_done 都从这里出。 */
1933
+ let runOutcome: RunOutcome | undefined;
1934
+
1935
+ /**
1936
+ * 多 phase 的一个成员 worker(W4)。与 `runSingleJob` 刻意分开写:
1937
+ * 那一路的每一帧都被 `wire-baseline.test.ts` 逐字节钉住,塞分支进来必回退。
1938
+ *
1939
+ * 成员路径的四条纪律:
1940
+ * ① 每个 job 一个独立 `createProductSession`(persona/task 来自 JobSpec);
1941
+ * ② 文本只走 `ctx.onDelta`(→ `member_delta`),**不发** text_delta / thinking_delta——
1942
+ * 多 phase 的用户视图是成员卡片,不是一根合并的气泡;
1943
+ * ③ 工具/确认/UI 事件一律不转发主线程(成员是后台 worker,不该弹确认卡);
1944
+ * ④ usage 只记在**局部** mapper 上,由引擎汇总,不碰主 `mapper`/`accounting`。
1945
+ */
1946
+ const runMemberJob = async (job: JobSpec, ctx: JobContext): Promise<JobRunResult> => {
1947
+ const memberMapper = createEventMapper();
1948
+ let memberText = "";
1949
+ let memberError: string | undefined;
1950
+ try {
1951
+ const history = entryPhaseIds.has(ctx.phaseId) ? ((await deps.history.load(userId, scope, sessionId)) ?? []) : [];
1952
+ const feedbackSink = deps.feedback as FeedbackSubmitSink | undefined;
1953
+ const tools = deps.toolFactory({
1954
+ productId: product.manifest.id,
1955
+ productDir: product.dir,
1956
+ userId,
1957
+ dataDir: deps.dataDir,
1958
+ qual: scope,
1959
+ persona: job.persona,
1960
+ ...(feedbackSink
1961
+ ? {
1962
+ createFeedback: async (content: string) => {
1963
+ const row = await feedbackSink.submit({
1964
+ userId,
1965
+ kind: "other",
1966
+ content,
1967
+ sessionId,
1968
+ ref: `${job.persona}:${sessionId}`,
1969
+ priority: roles.includes("member") || roles.includes("admin") ? "high" : "normal",
1970
+ });
1971
+ return { id: row.id ?? 0 };
1972
+ },
1973
+ }
1974
+ : {}),
1975
+ });
1976
+ const ms = await createProductSession({
1656
1977
  product,
1657
1978
  modelRuntime: deps.modelRuntime,
1658
1979
  tools,
1659
1980
  userId,
1660
1981
  personaConfig: deps.personaConfig,
1661
1982
  dataDir: deps.dataDir,
1662
- // 档位语义(label/thinking/multiplier)仍来自 manifest,只有物理模型跟着候选走。
1663
- tier: candidate.provider === tier.provider && candidate.model === tier.model
1664
- ? tier
1665
- : { ...tier, provider: candidate.provider, model: candidate.model },
1983
+ tier,
1666
1984
  qual: scope,
1667
- persona,
1985
+ persona: job.persona,
1668
1986
  history,
1669
1987
  mcpConfig: deps.mcpConfig,
1670
- requestConfirm: (info) =>
1671
- new Promise<boolean>((resolve) => {
1672
- const settle = (allow: boolean) => {
1673
- clearTimeout(timer);
1674
- confirms.delete(info.toolCallId);
1675
- resolve(allow);
1676
- };
1677
- const timer = setTimeout(() => settle(false), CONFIRM_TIMEOUT_MS);
1678
- confirms.set(info.toolCallId, settle);
1679
- send({ type: "tool_confirm", toolCallId: info.toolCallId, name: info.name, args: info.args });
1680
- }),
1681
- // Extension dialogs (batch 3-4b): same pause-and-answer shape as the
1682
- // confirm gate, but answered through POST /api/ui-response. Nobody
1683
- // answering means the fail-closed default, never a hung run.
1684
- requestUI: (info, signal) =>
1685
- new Promise<UIPromptAnswer>((resolve) => {
1686
- const settle = (answer: UIPromptAnswer) => {
1687
- clearTimeout(timer);
1688
- signal?.removeEventListener("abort", onAbort);
1689
- uiPrompts.delete(info.id);
1690
- resolve(answer);
1691
- };
1692
- const onAbort = () => settle({ cancelled: true });
1693
- const timeoutMs = info.timeoutMs
1694
- ? Math.min(Math.max(info.timeoutMs, UI_TIMEOUT_MIN_MS), UI_TIMEOUT_MAX_MS)
1695
- : UI_TIMEOUT_MS;
1696
- const timer = setTimeout(() => settle({ cancelled: true }), timeoutMs);
1697
- signal?.addEventListener("abort", onAbort, { once: true });
1698
- uiPrompts.set(info.id, settle);
1699
- send({
1700
- type: "ui_prompt",
1701
- id: info.id,
1702
- kind: info.kind,
1703
- title: info.title,
1704
- ...(info.message !== undefined ? { message: info.message } : {}),
1705
- ...(info.options !== undefined ? { options: info.options } : {}),
1706
- ...(info.placeholder !== undefined ? { placeholder: info.placeholder } : {}),
1707
- });
1708
- }),
1709
- onUINotify: (message, level) => send({ type: "ui_notify", message, level }),
1988
+ // 成员是后台 worker:没人盯着确认卡,fail-closed 拒绝才是正确默认。
1989
+ requestConfirm: async () => false,
1710
1990
  });
1711
-
1712
- // Client went away: stop spending tokens(读的是当前那一次尝试的会话)。
1713
- res.on("close", () => {
1714
- if (!res.writableEnded) void session?.abort();
1715
- });
1716
-
1717
- for (let attempt = 0; ; attempt++) {
1718
- const candidate = chain[attempt]!;
1719
- session = await buildSession(candidate);
1720
-
1721
- // 一次尝试内只有「还没吐字、还没跑工具」才允许换候选:重发会把已经流出去的
1722
- // 半句话再发一遍,或把已经发生的工具副作用再执行一遍。这条边界是 v1 的口径。
1723
- let pendingFailover: { message: string } | undefined;
1724
- let attemptText = false;
1725
- const toolRoundsAtStart = accounting.toolRounds();
1726
-
1727
- session.subscribe((event) => {
1728
- const outs = mapper.map(event);
1729
- // 同一批里出现 model_error 且这次还能切:**整批吞掉**——包括那条空的
1730
- // message_end,否则前端会先落一个空气泡再被真正的回答顶下去。
1731
- const errOut = outs.find((o) => o.type === "error" && o.code === "model_error");
1732
- if (errOut && errOut.type === "error") {
1733
- // 判定用 pi 原始报错(rawModelError),不是折叠后的用户文案——文案里
1734
- // 已经没有 JSON/网络词,拿它匹配会让 failover 失明(2026-11-04)。
1735
- const canSwitch =
1736
- failoverOn && attempt < chain.length - 1 && !attemptText && accounting.toolRounds() === toolRoundsAtStart;
1737
- if (canSwitch && isRetryableModelError(rawModelError(event))) {
1738
- // 用户不该看到一次注定会被掩盖的失败(也不显示模型名)。
1739
- pendingFailover = { message: rawModelError(event) };
1740
- void session?.abort();
1741
- return;
1742
- }
1743
- }
1744
- for (const out of outs) {
1745
- if (out.type === "text_delta" || out.type === "thinking_delta") attemptText = true;
1746
- if (out.type === "done") {
1747
- if (pendingFailover) return; // abort 带出来的 done 不发——真正的收口在最后一次尝试
1748
- doneSent = true;
1749
- aborted = out.aborted;
1750
- }
1751
- accounting.observe(out);
1752
- if (out.type === "tool_end") toolCalls[out.name] = (toolCalls[out.name] ?? 0) + 1;
1753
- send(out);
1754
- if (accounting.limitHit() && !limitSent) {
1755
- // One prompt must not loop forever: stop it at the cap, with a reason.
1756
- limitSent = true;
1757
- send({
1758
- type: "error",
1759
- code: "limit_exceeded",
1760
- message: `已达到单次运行 ${limits.maxToolRounds} 轮工具调用上限,已中止`,
1761
- retryable: false,
1762
- });
1763
- void session?.abort();
1764
- }
1991
+ memberSessions.push(ms);
1992
+ const onAbort = () => void ms.abort();
1993
+ if (ctx.signal.aborted) onAbort();
1994
+ else ctx.signal.addEventListener("abort", onAbort, { once: true });
1995
+ ms.subscribe((event) => {
1996
+ for (const out of memberMapper.map(event)) {
1997
+ if (out.type === "text_delta" || out.type === "thinking_delta") ctx.onDelta?.(out.delta);
1998
+ if (out.type === "message_end") memberText = out.text;
1999
+ // tool_* / done / error:不转发,成员气泡只显示文本与状态。
1765
2000
  }
1766
2001
  });
2002
+ await ms.prompt(job.task, undefined);
2003
+ await ms.waitForIdle();
2004
+ ctx.signal.removeEventListener("abort", onAbort);
2005
+ } catch (err) {
2006
+ memberError = err instanceof Error ? err.message : String(err);
2007
+ console.error("[orchestrator] member job failed:", err);
2008
+ send({ type: "error", code: "internal", message: `成员 ${job.persona} 执行失败`, retryable: false });
2009
+ }
2010
+ const usage = memberMapper.totalUsage();
2011
+ return {
2012
+ output: memberText,
2013
+ ok: memberError === undefined,
2014
+ usage: {
2015
+ input: usage.input,
2016
+ output: usage.output,
2017
+ cacheRead: usage.cacheRead,
2018
+ cacheWrite: usage.cacheWrite,
2019
+ total: usage.total,
2020
+ costUsd: usage.costUsd,
2021
+ },
2022
+ stopReason: memberError === undefined ? "end_turn" : "error",
2023
+ ...(memberError !== undefined ? { error: memberError } : {}),
2024
+ };
2025
+ };
1767
2026
 
1768
- try {
1769
- await session.prompt(promptText, promptImages);
1770
- await session.waitForIdle();
1771
- } catch (err) {
1772
- // abort 引起的抛出是切换路径的一部分;没有待切换的错误就照旧冒泡。
1773
- if (!pendingFailover) throw err;
2027
+ /** EngineEvent(内部)→ ChatEvent(协议)+ runId。单 phase 时引擎不发事件,这个桥用不到。 */
2028
+ const bridgeEngineEvent = (ev: EngineEvent): void => {
2029
+ switch (ev.type) {
2030
+ case "member_started":
2031
+ send({ type: "member_started", runId, phaseId: ev.phaseId, memberId: ev.memberId, task: ev.task });
2032
+ if (deps.checkpoints) {
2033
+ void deps.checkpoints
2034
+ .saveJob(runId, {
2035
+ runId,
2036
+ phaseId: ev.phaseId,
2037
+ jobId: ev.jobId,
2038
+ state: "running",
2039
+ usage: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0, costUsd: 0 },
2040
+ leaseUntil: Date.now() + 5 * 60_000,
2041
+ })
2042
+ .catch((err) => console.error("[checkpoint] saveJob(running) failed:", err));
2043
+ }
2044
+ break;
2045
+ case "member_delta":
2046
+ send({ type: "member_delta", runId, phaseId: ev.phaseId, memberId: ev.memberId, delta: ev.delta });
2047
+ break;
2048
+ case "member_done": {
2049
+ const u = ev.usage;
2050
+ const usage: TokenUsage = {
2051
+ input: u.input,
2052
+ output: u.output,
2053
+ cacheRead: u.cacheRead ?? 0,
2054
+ cacheWrite: u.cacheWrite ?? 0,
2055
+ total: u.total,
2056
+ costUsd: u.costUsd ?? 0,
2057
+ };
2058
+ send({ type: "member_done", runId, phaseId: ev.phaseId, memberId: ev.memberId, state: ev.state as "completed" | "failed" | "stopped" | "blocked", usage });
2059
+ if (deps.checkpoints) {
2060
+ void deps.checkpoints
2061
+ .saveJob(runId, { runId, phaseId: ev.phaseId, jobId: ev.jobId, state: ev.state, usage })
2062
+ .catch((err) => console.error("[checkpoint] saveJob(done) failed:", err));
2063
+ }
2064
+ break;
1774
2065
  }
1775
- if (!pendingFailover) break;
2066
+ default:
2067
+ // phase_started / phase_settled:协议里没有对应事件,刻意不下发。
2068
+ break;
2069
+ }
2070
+ };
1776
2071
 
1777
- // 换下一条:打冷却、留痕、告知用户,然后**重建会话**重发同一个 prompt。
1778
- // 不用 session.setModel 原地重发——会话里已经记了这次失败的用户消息,重发会让
1779
- // 历史多一条一模一样的提问;重建用的是 run 开始时那份 history,干净。
1780
- cooldown.mark(candidate, cooldownSeconds);
1781
- send({ type: "ui_notify", message: FAILOVER_NOTICE, level: "warning" });
1782
- if (deps.audit) {
1783
- void deps.audit
1784
- .append({
1785
- actor: userId,
1786
- result: "ok",
1787
- action: MODEL_SWITCH,
1788
- target: `session:${sessionId}`,
1789
- meta: { from: candidateKey(candidate), to: candidateKey(chain[attempt + 1]!), reason: pendingFailover.message.slice(0, 200) },
1790
- })
1791
- .catch((err) => console.error("[audit] model.switch failed (fail-open):", err));
2072
+ // 客户端断开 → abort 所有成员会话(单 phase 的会话由 runSingleJob 内部的 close 监听处理)。
2073
+ if (isFlow) {
2074
+ res.on("close", () => {
2075
+ if (!res.writableEnded) for (const ms of memberSessions) void ms.abort();
2076
+ });
2077
+ }
2078
+
2079
+ try {
2080
+ if (isFlow) {
2081
+ // 多 phase 也发 ready/tools_meta 作为传输头——前端的 sessionId 绑定与工具名
2082
+ // 都依赖它们;但成员事件从 member_started 开始,不给单 phase 气泡添帧。
2083
+ send({ type: "ready", v: PROTOCOL_VERSION, sessionId, product: product.manifest.id });
2084
+ send({ type: "tools_meta", labels: mergedToolLabels(deps.mcpConfig?.get(), product.manifest.agent.toolLabels) });
2085
+ const outcome = await engine.execute(runSpec, {
2086
+ runJob: runMemberJob,
2087
+ signal: runAbort.signal,
2088
+ emit: bridgeEngineEvent,
2089
+ ...(deps.checkpoints
2090
+ ? { persist: (o) => void persistCheckpoints(deps.checkpoints!, o.runId, o.checkpoints).catch((err) => console.error("[checkpoint] persist failed:", err)) }
2091
+ : {}),
2092
+ });
2093
+ runOutcome = outcome;
2094
+ doneSent = true;
2095
+ aborted = outcome.state === "stopped";
2096
+ runError = outcome.error;
2097
+ lastText = outcome.output;
2098
+ send({
2099
+ type: "run_done",
2100
+ runId,
2101
+ state: outcome.state as "completed" | "failed" | "stopped" | "blocked",
2102
+ usage: outcome.usage,
2103
+ output: outcome.output,
2104
+ durationMs: Date.now() - startedAt.getTime(),
2105
+ });
2106
+ } else {
2107
+ const report = engine.validate(runSpec);
2108
+ if (!report.ok) {
2109
+ // 单 phase 恒能通过;真不通过说明调用方给了非法组合。
2110
+ // **fail-open 回旧路径**:不能让客户端收到一个没有 ready 的空连接。
2111
+ console.error("[orchestrator] rejected run, falling back to direct path:", report.errors.join("; "));
2112
+ await runSingleJob();
2113
+ } else {
2114
+ await engine.execute(runSpec, { runJob: runSingleJob, signal: runAbort.signal });
1792
2115
  }
1793
- disposeProductSession(session!);
1794
- session = undefined;
1795
2116
  }
1796
2117
  } catch (err) {
1797
- runError = err instanceof Error ? err.message : String(err);
1798
- console.error("[chat] run failed:", err);
1799
- send({
1800
- type: "error",
1801
- code: "internal",
1802
- message: "服务暂时不可用,请稍后重试",
1803
- retryable: true,
1804
- });
2118
+ // 编排层自身出错不得破坏已经流出去的响应:业务错误已由 runSingleJob 内部
2119
+ // 发成 error 事件,这里只留痕。
2120
+ console.error("[orchestrator] execute failed (fail-open):", err);
2121
+ if (!started && !isFlow) await runSingleJob();
2122
+ if (isFlow && !doneSent) {
2123
+ runError = err instanceof Error ? err.message : String(err);
2124
+ send({ type: "error", code: "internal", message: "流程执行失败", retryable: true });
2125
+ }
1805
2126
  } finally {
1806
2127
  clearInterval(heartbeat);
1807
- const usage = mapper.totalUsage();
2128
+ // 多 phase 的账走引擎汇总(含 resume 恢复的历史消耗,I3);档 1 仍走主 mapper。
2129
+ const usage = isFlow && runOutcome ? runOutcome.usage : mapper.totalUsage();
1808
2130
  const durationMs = Date.now() - startedAt.getTime();
1809
2131
  try {
1810
- if (usage.total > 0) await deps.quota.record(userId, usage, persona);
2132
+ // 结算(I4):把占位换成实际消耗,多退少补。条件更新保证幂等(I6)。
2133
+ await deps.ledger.settle({ operationId, userId, persona, tokens: usage.total });
1811
2134
  // Charge after the fact: the run already spent the tokens; the balance gate stops the next one.
1812
2135
  if (billingActive) {
1813
2136
  // 池优先拆账(结算=发放检查点之二):结算时**重读**池——
@@ -1831,7 +2154,50 @@ export function createChatServer(deps: ServerDeps): Server {
1831
2154
  }
1832
2155
  // persona 在这里首次落库(与 scope 同一行、同一时刻):没跑起来的会话不该有绑定。
1833
2156
  // persona_version 与它同一次写入、同样只绑一次。
1834
- if (session) await deps.history.save(userId, scope, sessionId, exportHistory(session), persona, personaVersion);
2157
+ //
2158
+ // 多 phase 的历史只落「用户原话 + Run 汇总输出」两条:成员的内部转录是后台
2159
+ // worker 的中间产物,混进用户历史会让下次续聊把整段分工都重放一遍。成员会话
2160
+ // 各自 dispose,不 exportHistory——它们的 token 已经记在 runOutcome.usage 里。
2161
+ if (isFlow) {
2162
+ if (runOutcome) {
2163
+ const now = new Date(startedAt.getTime()).toISOString();
2164
+ const entries: FileEntry[] = [
2165
+ {
2166
+ type: "message",
2167
+ id: `${runId}:user`,
2168
+ parentId: null,
2169
+ timestamp: now,
2170
+ message: { role: "user", content: promptText, timestamp: startedAt.getTime() } as never,
2171
+ },
2172
+ {
2173
+ type: "message",
2174
+ id: `${runId}:answer`,
2175
+ parentId: `${runId}:user`,
2176
+ timestamp: now,
2177
+ message: {
2178
+ role: "assistant",
2179
+ content: [{ type: "text", text: runOutcome.output }],
2180
+ api: "openai-completions",
2181
+ provider: tier.provider,
2182
+ model: tier.model,
2183
+ usage: {
2184
+ input: usage.input,
2185
+ output: usage.output,
2186
+ cacheRead: usage.cacheRead,
2187
+ cacheWrite: usage.cacheWrite,
2188
+ totalTokens: usage.total,
2189
+ cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: usage.costUsd },
2190
+ },
2191
+ stopReason: "stop",
2192
+ timestamp: startedAt.getTime(),
2193
+ } as never,
2194
+ },
2195
+ ];
2196
+ await deps.history.save(userId, scope, sessionId, entries, persona, personaVersion);
2197
+ }
2198
+ } else if (session) {
2199
+ await deps.history.save(userId, scope, sessionId, exportHistory(session), persona, personaVersion);
2200
+ }
1835
2201
  if (deps.runLog) {
1836
2202
  await deps.runLog.record({
1837
2203
  userId,
@@ -1882,6 +2248,7 @@ export function createChatServer(deps: ServerDeps): Server {
1882
2248
  for (const settle of uiPrompts.values()) settle({ cancelled: true });
1883
2249
  uiPrompts.clear();
1884
2250
  if (session) await disposeProductSession(session);
2251
+ for (const ms of memberSessions) await disposeProductSession(ms);
1885
2252
  running.delete(runKey);
1886
2253
  pendingRuns.delete(runKey);
1887
2254
  pendingUI.delete(runKey);
@@ -1957,6 +2324,17 @@ export function createChatServer(deps: ServerDeps): Server {
1957
2324
  // 默认助手的人格 id(通用化):前端靠它把「老会话(persona 为 null)」
1958
2325
  // 认成默认助手那一批。只是一个 id,不带能力信息。
1959
2326
  defaultPersona: product.defaultPersona,
2327
+ // 多 phase 流程的**展示字段**(W4):前端据此渲染流程选择器。
2328
+ // 只出 id/label/description——phase/member/task 这些编制细节永不下发。
2329
+ ...(product.manifest.agent.flows
2330
+ ? {
2331
+ flows: Object.entries(product.manifest.agent.flows).map(([id, f]) => ({
2332
+ id,
2333
+ ...(f?.label ? { label: f.label } : {}),
2334
+ ...(f?.description ? { description: f.description } : {}),
2335
+ })),
2336
+ }
2337
+ : {}),
1960
2338
  };
1961
2339
  return sendJson(res, 200, body);
1962
2340
  }
@@ -2021,6 +2399,56 @@ export function createChatServer(deps: ServerDeps): Server {
2021
2399
  return sendJson(res, 200, { sessionId: body.sessionId, messages: toHistoryMessages(kept) });
2022
2400
  }
2023
2401
  if (req.method === "POST" && url.pathname === "/api/chat") return await handleChat(req, res);
2402
+ if (req.method === "POST" && url.pathname === "/api/suggestions") {
2403
+ const auth = await deps.authenticate(req);
2404
+ if (!auth) return deny401(deps, req, res);
2405
+ let body: { messages?: unknown; persona?: unknown };
2406
+ try {
2407
+ body = (await readJson(req)) as typeof body;
2408
+ } catch {
2409
+ return sendError(res, 400, "bad_request", "body must be valid JSON under 64KB");
2410
+ }
2411
+ // 人格 id 合法性与 /api/chat 同一条规矩(400,不静默)。默认助手也显式带 id:
2412
+ // 这里没有会话绑定,服务端要按同一个 id 复核人格级覆盖。
2413
+ if (
2414
+ body.persona !== undefined &&
2415
+ (typeof body.persona !== "string" || !personaIdsOf(deps.product).has(body.persona))
2416
+ ) {
2417
+ return sendError(res, 400, "bad_request", `unknown persona "${String(body.persona)}"`);
2418
+ }
2419
+ const personaId = (typeof body.persona === "string" ? body.persona : undefined) ?? deps.product.defaultPersona;
2420
+ const followUp = deps.product.manifest.branding.ui?.followUp;
2421
+ // 生效模式(前后端同一套优先级):全局 off 是硬地板——人格不能把全局关闭
2422
+ // 重新打开;否则人格 followUpMode > 全局 mode > 缺省 llm。
2423
+ const globalMode = followUp?.mode ?? "llm";
2424
+ if (globalMode === "off") return sendJson(res, 200, { suggestions: [] });
2425
+ const personaMode = effectiveUiOf(deps.product, deps.personaConfig?.get(), personaId)?.followUpMode;
2426
+ const mode = personaMode ?? globalMode;
2427
+ const count = followUp?.count ?? SUGGESTIONS_DEFAULTS.count;
2428
+ const staticFallback = Array.isArray(followUp?.static) ? followUp.static : [];
2429
+ if (mode === "off") return sendJson(res, 200, { suggestions: [] });
2430
+ if (mode === "static") return sendJson(res, 200, { suggestions: staticFallback.slice(0, count) });
2431
+ // llm:这是一轮问答的附属产物,任何失败(模型不可用/超时/输入非法)都静默
2432
+ // 回落 static——绝不 4xx/5xx 冒泡,也不接配额与计费。
2433
+ let suggestions = staticFallback.slice(0, count);
2434
+ try {
2435
+ suggestions = (
2436
+ await (deps.suggestions ?? runSuggestions)(
2437
+ { product: deps.product, modelRuntime: deps.modelRuntime },
2438
+ {
2439
+ messages: clipMessages(body.messages, followUp?.maxMessages ?? SUGGESTIONS_DEFAULTS.maxMessages),
2440
+ count,
2441
+ staticFallback,
2442
+ ...(followUp?.systemPrompt !== undefined ? { systemPrompt: followUp.systemPrompt } : {}),
2443
+ timeoutMs: followUp?.timeoutMs ?? SUGGESTIONS_DEFAULTS.timeoutMs,
2444
+ },
2445
+ )
2446
+ ).slice(0, count);
2447
+ } catch {
2448
+ // 静默:回落内容已在初始值里。
2449
+ }
2450
+ return sendJson(res, 200, { suggestions });
2451
+ }
2024
2452
  if (req.method === "POST" && url.pathname === "/api/decision") {
2025
2453
  const auth = await deps.authenticate(req);
2026
2454
  if (!auth) return deny401(deps, req, res);
@@ -2359,7 +2787,7 @@ export function createChatServer(deps: ServerDeps): Server {
2359
2787
  const memberActive = memberUntil !== null && Date.parse(memberUntil) > Date.now();
2360
2788
  const isMember = roles.includes("admin") || memberActive;
2361
2789
  // 配额展示看的是**这个人格**那本账(产品声明的 id)——core 不假定它叫什么。
2362
- const stats = (await deps.quota.todayStats?.(auth.userId, personaParam)) ?? { runsToday: 0, tokensToday: 0 };
2790
+ const stats = (await deps.ledger.todayStats(auth.userId, personaParam)) ?? { runsToday: 0, tokensToday: 0 };
2363
2791
  const caps = capsForPersona(limits, personaParam, isMember);
2364
2792
  // 池视图:whoami 本身就是「检查点」——会员打开订阅页即 lazy 发满当月池。
2365
2793
  let pool: WhoamiResponse["pool"] = null;