dsh-multigameassistant 2.2.0 → 2.2.2

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.
@@ -2,7 +2,7 @@ import { createRequire as __adgCreateRequire } from 'node:module'; import { dirn
2
2
  import {
3
3
  SUMMARIES_FILE,
4
4
  summariesReader
5
- } from "./chunk-ICJLH3DP.js";
5
+ } from "./chunk-KMVQK5CI.js";
6
6
  import {
7
7
  writeFileAtomic
8
8
  } from "./chunk-RESS7WVY.js";
@@ -21,8 +21,10 @@ import {
21
21
  MAX_MEMORY_TEXT,
22
22
  MAX_SKILL_NAME,
23
23
  MEMORY_CAPABILITY,
24
+ MODEL_FALLBACK_SOURCE,
24
25
  MemoryEntry,
25
26
  MemoryKind,
27
+ ModelFallbackNotice,
26
28
  PERMISSIONS_CAPABILITY,
27
29
  PERSONA_CAPABILITY,
28
30
  Persona,
@@ -41,7 +43,7 @@ import {
41
43
  personaPrompt,
42
44
  threadsBetween,
43
45
  timetableText
44
- } from "./chunk-NEUZQ7TI.js";
46
+ } from "./chunk-LXHF6AKU.js";
45
47
  import {
46
48
  external_exports
47
49
  } from "./chunk-6DSSLS7I.js";
@@ -3723,7 +3725,7 @@ var require_websocket_server = __commonJS({
3723
3725
 
3724
3726
  // packages/gateway/lib/index.js
3725
3727
  import { execFile as execFile2 } from "node:child_process";
3726
- import { randomBytes as randomBytes2 } from "node:crypto";
3728
+ import { randomBytes as randomBytes2, randomUUID as randomUUID5 } from "node:crypto";
3727
3729
  import { existsSync, mkdirSync as mkdirSync6, readFileSync as readFileSync3, writeFileSync } from "node:fs";
3728
3730
  import { dirname as dirname7, join as join8 } from "node:path";
3729
3731
  import { fileURLToPath as fileURLToPath2 } from "node:url";
@@ -6481,6 +6483,10 @@ var RecallLog = class {
6481
6483
  };
6482
6484
 
6483
6485
  // packages/gateway/lib/convert.js
6486
+ function fallbackOf(value) {
6487
+ const parsed = ModelFallbackNotice.safeParse(value);
6488
+ return parsed.success ? parsed.data : void 0;
6489
+ }
6484
6490
  var isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
6485
6491
  var str2 = (v) => typeof v === "string" ? v : void 0;
6486
6492
  var num = (v) => typeof v === "number" && Number.isFinite(v) ? v : void 0;
@@ -6642,7 +6648,9 @@ function convertEvent(event) {
6642
6648
  origin: {
6643
6649
  kind,
6644
6650
  human: HUMAN_SOURCES.has(kind),
6645
- ...opt("summary", source.form === "notice" ? str2(source.summary) : void 0)
6651
+ ...opt("summary", source.form === "notice" ? str2(source.summary) : void 0),
6652
+ ...opt("childId", kind === "adg-task-dispatched" ? str2(source.childId) : void 0),
6653
+ ...opt("fallback", kind === MODEL_FALLBACK_SOURCE ? fallbackOf(source.fallback) : void 0)
6646
6654
  },
6647
6655
  ...opt("requestId", str2(source.rpcId)),
6648
6656
  ...replaces ? { replacesHistory: true } : {}
@@ -6680,7 +6688,7 @@ function convertEvent(event) {
6680
6688
  if (turn === void 0 || step === void 0)
6681
6689
  return unknown();
6682
6690
  const facts = streamFacts(d.stream);
6683
- return { type: "assistant/attempt", seq, time, turn, step, timing: facts.timing, ...opt("error", facts.failure) };
6691
+ return { type: "assistant/attempt", seq, time, turn, step, timing: facts.timing, ...opt("usage", convertUsage(d.usage)), ...opt("error", facts.failure) };
6684
6692
  }
6685
6693
  case "tool/call":
6686
6694
  if (turn === void 0 || step === void 0)
@@ -7475,7 +7483,7 @@ var HandOffs = class {
7475
7483
  }
7476
7484
  /** Those from or to this conversation, oldest first. */
7477
7485
  of(sessionId) {
7478
- return [...this.#items.values()].filter((h) => h.fromSession === sessionId || h.toSession === sessionId);
7486
+ return [...this.#items.values()].filter((h) => h.fromSession === sessionId || h.toSession === sessionId || h.ownerSession === sessionId);
7479
7487
  }
7480
7488
  get(id) {
7481
7489
  return this.#items.get(id);
@@ -7694,7 +7702,7 @@ function keeps(tools, name) {
7694
7702
  function matches(patterns, name) {
7695
7703
  return patterns.some((pattern) => pattern.endsWith("*") ? name.startsWith(pattern.slice(0, -1)) : name === pattern);
7696
7704
  }
7697
- var NOBODY = ["subagent_fork"];
7705
+ var NOBODY = ["subagent_fork", "wait_agent"];
7698
7706
  var TOGETHER = ["agent_delegate", "agent_message", "agent_threads"];
7699
7707
  var MEMORY_TOOLS = ["memory_save", "memory_forget", "memory_search", "history_search", "history_read"];
7700
7708
  var ALWAYS = [...TOGETHER, ...MEMORY_TOOLS];
@@ -7748,7 +7756,7 @@ function teamPrompt(self, main, leads, options = {}) {
7748
7756
  isMain ? `\u4F60\u662F\u300C${self.name}\u300D\uFF08${self.id}\uFF09\uFF0C\u662F\u6700\u4E0A\u7EA7\u7684 agent\uFF1A\u548C\u7528\u6237\u5BF9\u8BDD\uFF0C\u628A\u4E8B\u5206\u7ED9\u4E0B\u9762\u7684\u4E3B agent\uFF0C\u6C47\u603B\u7ED3\u679C\u5E76\u505A\u51B3\u5B9A\u3002` : `\u4F60\u662F\u300C${self.name}\u300D\uFF08${self.id}\uFF09\uFF0C\u662F\u4E00\u4E2A\u4E3B agent\uFF1A\u8D1F\u8D23\u81EA\u5DF1\u8FD9\u5757\u4E8B\uFF0C\u505A\u51B3\u5B9A\u3001\u6C47\u603B\u7ED3\u679C\u3002\u4E0A\u7EA7\u662F${who(main)}\uFF1A\u5B83\u548C\u7528\u6237\u5BF9\u8BDD\u3001\u5206\u53D1\u4EFB\u52A1\u3001\u505A\u51B3\u5B9A\u3002`,
7749
7757
  colleagues.length === 0 ? isMain ? "\u73B0\u5728\u6CA1\u6709\u5176\u4ED6\u4E3B agent\u3002" : "\u73B0\u5728\u6CA1\u6709\u5176\u4ED6\u540C\u4E8B\u3002" : `${isMain ? "\u4E0B\u9762\u7684\u4E3B agent" : "\u540C\u4E8B"}\uFF1A
7750
7758
  ${colleagues.map(line2).join("\n")}`,
7751
- "\u5177\u4F53\u7684\u6D3B\u90FD\u7531\u5B50 agent \u505A\uFF08subagent\uFF09\uFF1A\u8FD0\u884C\u547D\u4EE4\u3001\u6539\u6587\u4EF6\u3001\u622A\u56FE\u70B9\u51FB\u3001\u8FD0\u884C\u548C\u5B89\u88C5\u811A\u672C\u3001\u542F\u52A8\u6E38\u620F\uFF0C\u8FD9\u4E9B\u5DE5\u5177\u4E0D\u5728\u4F60\u7684\u5BF9\u8BDD\u91CC\uFF0C\u53EA\u6709\u5B50 agent \u6709\uFF1B\u522B\u5904\u63D0\u5230\u8FD9\u4E9B\u5DE5\u5177\uFF0C\u4E5F\u662F\u8BA9\u5B50 agent \u53BB\u7528\u3002\u4F60\u7684\u5BF9\u8BDD\u7559\u7ED9\u5224\u65AD\u548C\u6C47\u603B\uFF1A\u8981\u505A\u4EC0\u4E48\u5C31\u5F00\u4E00\u4E2A\u5B50 agent\uFF0C\u628A\u4EFB\u52A1\u5199\u5B8C\u6574\uFF08\u5B83\u770B\u4E0D\u5230\u4F60\u7684\u5BF9\u8BDD\uFF09\uFF0C\u7ED3\u679C\u4EA4\u56DE\u540E\u7531\u4F60\u5224\u65AD\u3001\u6C47\u603B\u3002\u67E5\u72B6\u6001\u3001\u8BFB\u4E00\u4E0B\u6587\u4EF6\u8FD9\u79CD\u770B\u4E00\u773C\u7684\u4E8B\u53EF\u4EE5\u81EA\u5DF1\u6765\u3002\u51E0\u4EF6\u4E92\u4E0D\u76F8\u5173\u7684\u4E8B\u53EF\u4EE5\u540C\u65F6\u5F00\u3002\u5F00\u4E86\u4EE5\u540E\u53EA\u5269\u7B49\u7ED3\u679C\u65F6\uFF0C\u7528 wait_agent \u7B49\uFF08timeout_ms \u6309\u5B83\u5927\u6982\u8981\u591A\u4E45\u586B\uFF09\uFF0C\u6216\u8005\u7ED3\u675F\u8FD9\u4E00\u8F6E\uFF0C\u7ED3\u679C\u4F1A\u4F5C\u4E3A\u65B0\u6D88\u606F\u53D1\u6765\uFF1B\u8981\u7B49\u4E00\u6B21\u811A\u672C\u8FD0\u884C\u7ED3\u675F\uFF0C\u7528 script_runs \u5E26\u4E0A run_id \u548C wait_seconds\uFF0C\u5B83\u7B49\u5230\u7ED3\u675F\uFF08\u91CD\u65B0\u8FD0\u884C\u7684\u8BDD\u8DDF\u5230\u6700\u540E\u90A3\u6B21\uFF09\u624D\u8FD4\u56DE\u3002\u4E0D\u8981\u7528 glob\u3001list_agents\u3001script_runs\u3001window_list \u4E00\u904D\u904D\u67E5\u505A\u5230\u54EA\u4E86\uFF1A\u4F60\u5BF9\u8BDD\u91CC\u7684\u6BCF\u4E00\u6B65\u90FD\u8981\u628A\u6574\u4E2A\u5BF9\u8BDD\u91CD\u65B0\u53D1\u7ED9\u6A21\u578B\u4E00\u6B21\uFF0C\u8FD9\u6837\u7B49\u6700\u8D39\u3002\u5B50 agent \u53EA\u5BF9\u4F60\u8D1F\u8D23\uFF0C\u53EA\u628A\u7ED3\u679C\u4EA4\u56DE\u7ED9\u4F60\uFF1B\u5B83\u8981\u7528\u6237\u6279\u51C6\u7684\u64CD\u4F5C\uFF0C\u4F1A\u5728\u4F60\u7684\u5BF9\u8BDD\u91CC\u8BF7\u7528\u6237\u6279\u51C6\u3002",
7759
+ "\u5177\u4F53\u7684\u6D3B\u90FD\u7531\u5B50 agent \u505A\uFF08subagent\uFF09\uFF1A\u8FD0\u884C\u547D\u4EE4\u3001\u6539\u6587\u4EF6\u3001\u622A\u56FE\u70B9\u51FB\u3001\u8FD0\u884C\u548C\u5B89\u88C5\u811A\u672C\u3001\u542F\u52A8\u6E38\u620F\uFF0C\u8FD9\u4E9B\u5DE5\u5177\u4E0D\u5728\u4F60\u7684\u5BF9\u8BDD\u91CC\uFF0C\u53EA\u6709\u5B50 agent \u6709\uFF1B\u522B\u5904\u63D0\u5230\u8FD9\u4E9B\u5DE5\u5177\uFF0C\u4E5F\u662F\u8BA9\u5B50 agent \u53BB\u7528\u3002\u4F60\u7684\u5BF9\u8BDD\u7559\u7ED9\u5224\u65AD\u548C\u6C47\u603B\uFF1A\u8981\u505A\u4EC0\u4E48\u5C31\u5F00\u4E00\u4E2A\u5B50 agent\uFF0C\u628A\u4EFB\u52A1\u5199\u5B8C\u6574\uFF08\u5B83\u770B\u4E0D\u5230\u4F60\u7684\u5BF9\u8BDD\uFF09\uFF0C\u7ED3\u679C\u4EA4\u56DE\u540E\u7531\u4F60\u5224\u65AD\u3001\u6C47\u603B\u3002\u67E5\u72B6\u6001\u3001\u8BFB\u4E00\u4E0B\u6587\u4EF6\u8FD9\u79CD\u770B\u4E00\u773C\u7684\u4E8B\u53EF\u4EE5\u81EA\u5DF1\u6765\u3002\u51E0\u4EF6\u4E92\u4E0D\u76F8\u5173\u7684\u4E8B\u53EF\u4EE5\u540C\u65F6\u5F00\u3002\u6D3E\u53D1\u540E\u53EF\u4EE5\u7EE7\u7EED\u5176\u4ED6\u5DE5\u4F5C\uFF1B\u53EA\u5269\u5B50\u4EFB\u52A1\u672A\u4EA4\u4ED8\u65F6\u5C31\u7ED3\u675F\u5F53\u524D\u8F6E\u6B21\u3002\u5B50\u4EFB\u52A1\u5B8C\u6210\u3001\u5931\u8D25\u6216\u88AB\u505C\u6B62\u65F6\uFF0C\u7CFB\u7EDF\u4F1A\u81EA\u52A8\u628A\u5E26\u6709\u5B50\u4EFB\u52A1\u8EAB\u4EFD\u7684\u7EC8\u6001\u548C\u7ED3\u679C\u9001\u5230\u4F60\u7684\u5BF9\u8BDD\u3002\u6536\u5230\u56DE\u6267\u5C31\u76F4\u63A5\u7528\u5B83\u7EE7\u7EED\u5DE5\u4F5C\uFF1B\u591A\u4E2A\u5B50\u4EFB\u52A1\u5206\u522B\u4EA4\u4ED8\uFF0C\u6309\u5404\u81EA\u7684\u56DE\u6267\u5904\u7406\u3002\u8981\u7B49\u4E00\u6B21\u811A\u672C\u8FD0\u884C\u7ED3\u675F\uFF0C\u7528 script_runs \u5E26\u4E0A run_id \u548C wait_seconds\uFF0C\u5B83\u7B49\u5230\u7ED3\u675F\uFF08\u91CD\u65B0\u8FD0\u884C\u7684\u8BDD\u8DDF\u5230\u6700\u540E\u90A3\u6B21\uFF09\u624D\u8FD4\u56DE\u3002\u4E0D\u8981\u7528 glob\u3001list_agents\u3001script_runs\u3001window_list \u4E00\u904D\u904D\u67E5\u505A\u5230\u54EA\u4E86\uFF1A\u4F60\u5BF9\u8BDD\u91CC\u7684\u6BCF\u4E00\u6B65\u90FD\u8981\u628A\u6574\u4E2A\u5BF9\u8BDD\u91CD\u65B0\u53D1\u7ED9\u6A21\u578B\u4E00\u6B21\uFF0C\u8FD9\u6837\u7B49\u6700\u8D39\u3002\u5B50 agent \u53EA\u5BF9\u4F60\u8D1F\u8D23\uFF0C\u53EA\u628A\u7ED3\u679C\u4EA4\u56DE\u7ED9\u4F60\uFF1B\u5B83\u8981\u7528\u6237\u6279\u51C6\u7684\u64CD\u4F5C\uFF0C\u4F1A\u5728\u4F60\u7684\u5BF9\u8BDD\u91CC\u8BF7\u7528\u6237\u6279\u51C6\u3002",
7752
7760
  options.browser === true && keeps(self.tools, "browser_navigate") ? "\u7F51\u9875\u4E5F\u4E00\u6837\uFF1Aweb_fetch \u53EA\u62FF\u5230\u7A7A\u58F3\uFF08\u524D\u7AEF\u6E32\u67D3\u7684\u5B98\u7F51\u3001\u516C\u544A\u9875\u5E38\u8FD9\u6837\uFF09\u6216\u8981\u70B9\u5F00\u3001\u7FFB\u9875\u624D\u770B\u5F97\u5230\u65F6\uFF0C\u5F00\u5B50 agent \u7528\u5185\u7F6E\u6D4F\u89C8\u5668\u53BB\u8BFB\uFF0C\u4EFB\u52A1\u91CC\u5199\u660E\u300C\u7528\u5185\u7F6E\u6D4F\u89C8\u5668\uFF08browser_navigate \u6253\u5F00\uFF0Cbrowser_text \u8BFB\u6E32\u67D3\u540E\u7684\u6587\u5B57\uFF09\u300D\u3002\u5B83\u6253\u5F00\u7684\u9875\u9762\u7528\u6237\u5728\u6D4F\u89C8\u5668\u9762\u677F\u91CC\u770B\u5F97\u5230\u3002" : "",
7753
7761
  isMain ? "" : `\u9700\u8981\u540C\u4E8B\u7684\u90A3\u90E8\u5206\uFF0C\u7528 agent_delegate \u76F4\u63A5\u4EA4\u7ED9\u5B83\uFF0C\u4E0D\u7528\u7ED5\u8FC7\u300C${main.name}\u300D\u3002\u4EA4\u7ED9\u4F60\u7684\u4E8B\u6CA1\u8BF4\u6E05\u695A\u65F6\uFF0C\u7528 agent_message \u95EE\u4EA4\u7ED9\u4F60\u7684\u4E00\u65B9\uFF0C\u4E0D\u8981\u81EA\u5DF1\u53BB\u95EE\u7528\u6237\uFF08\u8981\u4E0D\u8981\u95EE\u7528\u6237\u7531\u5BF9\u65B9\u51B3\u5B9A\uFF09\uFF0C\u95EE\u6E05\u695A\u518D\u505A\uFF1B\u505A\u5B8C\u7528 agent_message \u7684 done \u4EA4\u56DE\u7ED3\u8BBA\uFF0C\u6216\u8005\u628A\u7ED3\u8BBA\u5199\u5728\u8FD9\u4E00\u8F6E\u6700\u540E\u4E00\u6BB5\u56DE\u590D\u91CC\u3002`,
7754
7762
  "\u548C\u5176\u4ED6 agent \u8BF4\u8BDD\u4E0D\u662F\u4E00\u95EE\u4E00\u7B54\uFF1A\u505A\u7684\u8FC7\u7A0B\u4E2D\u8981\u8BA9\u5BF9\u65B9\u77E5\u9053\u7684\uFF0C\u7528 agent_message \u7684 update\uFF0C\u9A6C\u4E0A\u9001\u5230\u5B83\u7684\u5BF9\u8BDD\u91CC\uFF0C\u8BF4\u51E0\u6B21\u90FD\u884C\uFF1B\u4E00\u4EF6\u4E8B\u4EA4\u56DE\u7ED3\u8BBA\u4EE5\u540E\u8FD8\u6709\u8BDD\uFF08\u6BD4\u5982\u5B9A\u65F6\u63D0\u9192\u5230\u4E86\uFF09\uFF0C\u540C\u6837\u7528 agent_message \u53D1\u5230\u90A3\u6761\u7EBF\u7D22\uFF0C\u4F60\u7684\u666E\u901A\u56DE\u590D\u5BF9\u65B9\u770B\u4E0D\u5230\u3002",
@@ -7866,7 +7874,7 @@ import { readFile as readFile9 } from "node:fs/promises";
7866
7874
  import { mkdirSync as mkdirSync3 } from "node:fs";
7867
7875
  import { dirname as dirname4 } from "node:path";
7868
7876
  var LEGACY_ROLES = ["intervene", "games", "scripts", "pipelines"];
7869
- var EMPTY = { agents: {}, subAgents: null, subAgentsOf: {} };
7877
+ var EMPTY = { agents: {}, subAgents: null, subAgentsOf: {}, fallbacks: { agents: [], subAgents: [] } };
7870
7878
  var AgentModels = class {
7871
7879
  #file;
7872
7880
  #legacyFile;
@@ -7889,6 +7897,17 @@ var AgentModels = class {
7889
7897
  subOf(agentId) {
7890
7898
  return this.#state.subAgentsOf[agentId] ?? this.#state.subAgents ?? void 0;
7891
7899
  }
7900
+ /** The fallback list for the agents' own conversations (`sub` false) or for sub-agents. */
7901
+ fallbacksOf(sub) {
7902
+ return sub ? this.#state.fallbacks.subAgents : this.#state.fallbacks.agents;
7903
+ }
7904
+ /** Replace one fallback list; a model listed twice is kept once (the first). */
7905
+ async setFallbacks(sub, models) {
7906
+ await this.loaded;
7907
+ const list = distinct(models.map(clean3));
7908
+ this.#state = { ...this.#state, fallbacks: { ...this.#state.fallbacks, [sub ? "subAgents" : "agents"]: list } };
7909
+ await this.#save();
7910
+ }
7892
7911
  /** @param selection - null: follow the layer above again. */
7893
7912
  async set(target, selection) {
7894
7913
  await this.loaded;
@@ -7899,6 +7918,9 @@ var AgentModels = class {
7899
7918
  const put = (record, key) => value === void 0 ? without(record, key) : { ...record, [key]: value };
7900
7919
  const state = this.#state;
7901
7920
  this.#state = target.sub !== true ? { ...state, agents: put(state.agents, target.agentId) } : target.agentId === void 0 ? { ...state, subAgents: value ?? null } : { ...state, subAgentsOf: put(state.subAgentsOf, target.agentId) };
7921
+ await this.#save();
7922
+ }
7923
+ async #save() {
7902
7924
  if (this.#file === void 0)
7903
7925
  return;
7904
7926
  mkdirSync3(dirname4(this.#file), { recursive: true });
@@ -7914,8 +7936,15 @@ var AgentModels = class {
7914
7936
  const selection = selectionOf(s);
7915
7937
  return selection === void 0 ? [] : [[id, selection]];
7916
7938
  }));
7939
+ const list = (value) => distinct(Array.isArray(value) ? value.flatMap((s) => selectionOf(s) ?? []) : []);
7917
7940
  const state = isObject(saved) ? saved : {};
7918
- this.#state = { agents: record(state["agents"]), subAgents: selectionOf(state["subAgents"]) ?? null, subAgentsOf: record(state["subAgentsOf"]) };
7941
+ const fallbacks = isObject(state["fallbacks"]) ? state["fallbacks"] : {};
7942
+ this.#state = {
7943
+ agents: record(state["agents"]),
7944
+ subAgents: selectionOf(state["subAgents"]) ?? null,
7945
+ subAgentsOf: record(state["subAgentsOf"]),
7946
+ fallbacks: { agents: list(fallbacks["agents"]), subAgents: list(fallbacks["subAgents"]) }
7947
+ };
7919
7948
  return;
7920
7949
  }
7921
7950
  const legacy = this.#legacyFile === void 0 ? void 0 : selectionOf(await readJson(this.#legacyFile));
@@ -7941,6 +7970,192 @@ function selectionOf(value) {
7941
7970
  function clean3(selection) {
7942
7971
  return { provider: selection.provider, model: selection.model, ...typeof selection.reasoningEffort === "string" ? { reasoningEffort: selection.reasoningEffort } : {} };
7943
7972
  }
7973
+ function distinct(list) {
7974
+ const seen = /* @__PURE__ */ new Set();
7975
+ return list.filter((s) => {
7976
+ const key = `${s.provider}/${s.model}`;
7977
+ if (seen.has(key))
7978
+ return false;
7979
+ seen.add(key);
7980
+ return true;
7981
+ });
7982
+ }
7983
+
7984
+ // packages/gateway/lib/model-fallback.js
7985
+ import { randomUUID as randomUUID4 } from "node:crypto";
7986
+ function failureOf(error) {
7987
+ if (error === null || typeof error !== "object")
7988
+ return void 0;
7989
+ const e = error;
7990
+ const failure = e.failure !== null && typeof e.failure === "object" ? e.failure : void 0;
7991
+ const code = typeof failure?.code === "string" ? failure.code : typeof e.code === "string" ? e.code : void 0;
7992
+ if (code === void 0)
7993
+ return void 0;
7994
+ const message = typeof failure?.message === "string" ? failure.message : typeof e.message === "string" ? e.message : "";
7995
+ return { code, message, ...typeof failure?.status === "number" ? { status: failure.status } : {} };
7996
+ }
7997
+ var QUOTA_CODES = /* @__PURE__ */ new Set(["QUOTA", "ACCOUNT_QUOTA", "HTTP_402"]);
7998
+ var AUTH_CODES = /* @__PURE__ */ new Set(["AUTH", "INVALID_CREDENTIAL", "MISSING_CREDENTIAL", "ACCOUNT_TOKEN_INVALID", "ACCOUNT_SIGN_IN_REQUIRED", "NO_CREDENTIAL_STORE", "HTTP_401", "HTTP_403"]);
7999
+ var GONE_CODES = /* @__PURE__ */ new Set(["NO_ADAPTER", "UNKNOWN_MODEL", "MODEL_UNSUPPORTED", "HTTP_404"]);
8000
+ var LIMIT_CODES = /* @__PURE__ */ new Set(["RATE_LIMIT", "HTTP_429"]);
8001
+ var QUOTA_WORDS = /insufficient[\s_-]*(account[\s_-]*)?(balance|quota|funds|credit)|exceeded your current quota|quota[\s_-]*(exceeded|exhausted)|out of credits|credit balance is too low|arrears|余额不足|欠费|额度(已)?(用完|用尽|不足|耗尽)|账户余额/i;
8002
+ function fallbackReason(failure) {
8003
+ const { code } = failure;
8004
+ if (QUOTA_CODES.has(code) || failure.status === 402 || QUOTA_WORDS.test(failure.message))
8005
+ return "\u4F59\u989D\u6216\u989D\u5EA6\u4E0D\u8DB3";
8006
+ if (AUTH_CODES.has(code))
8007
+ return "Key \u65E0\u6548\u6216\u6CA1\u6709\u6743\u9650";
8008
+ if (GONE_CODES.has(code))
8009
+ return "\u6A21\u578B\u4E0D\u53EF\u7528";
8010
+ if (LIMIT_CODES.has(code))
8011
+ return "\u8BF7\u6C42\u592A\u9891\u7E41\uFF0C\u91CD\u8BD5\u540E\u4ECD\u88AB\u9650\u6D41";
8012
+ if (code === "TIMEOUT")
8013
+ return "\u6A21\u578B\u8BF7\u6C42\u8D85\u65F6\uFF0C\u91CD\u8BD5\u540E\u4ECD\u65E0\u54CD\u5E94";
8014
+ if (code === "SERVER" || /^HTTP_5\d\d$/.test(code))
8015
+ return "\u6A21\u578B\u670D\u52A1\u51FA\u9519\uFF0C\u91CD\u8BD5\u540E\u4ECD\u7136\u5931\u8D25";
8016
+ return void 0;
8017
+ }
8018
+ var keyOf2 = (s) => `${s.provider}/${s.model}`;
8019
+ function nextFallback(current, list, tried, usable) {
8020
+ const at = list.findIndex((s) => keyOf2(s) === keyOf2(current));
8021
+ const order = at < 0 ? list : [...list.slice(at + 1), ...list.slice(0, at)];
8022
+ return order.find((s) => keyOf2(s) !== keyOf2(current) && !tried.has(keyOf2(s)) && usable(s));
8023
+ }
8024
+ function fallbackText(notice) {
8025
+ const name = (r) => `\u300C${r.providerName}\u300D\u7684 ${r.modelName}`;
8026
+ return [
8027
+ `\uFF08\u8F6F\u4EF6\u901A\u77E5\uFF1A\u521A\u624D\u7528\u7684${name(notice.from)}\u51FA\u9519\u4E86\uFF0C\u539F\u56E0\uFF1A${notice.reason}\uFF08${notice.error.trim() || notice.code}\uFF09\u3002`,
8028
+ `\u5DF2\u7ECF\u81EA\u52A8\u6362\u6210\u5907\u7528\u6A21\u578B${name(notice.to)}\uFF0C\u8FD9\u4E2A\u5BF9\u8BDD\u63A5\u4E0B\u6765\u90FD\u7528\u5B83\u3002\u7528\u6237\u80FD\u770B\u5230\u8FD9\u6B21\u5207\u6362\u7684\u5361\u7247\u3002`,
8029
+ "\u63A5\u7740\u505A\u521A\u624D\u6CA1\u505A\u5B8C\u7684\u4E8B\uFF0C\u5DF2\u7ECF\u505A\u5B8C\u7684\u6B65\u9AA4\u4E0D\u8981\u91CD\u505A\uFF1B\u9700\u8981\u7684\u8BDD\u53EF\u4EE5\u5728\u56DE\u590D\u91CC\u63D0\u4E00\u53E5\u6362\u4E86\u6A21\u578B\u3002\uFF09"
8030
+ ].join("");
8031
+ }
8032
+ function fallbackSummary(notice) {
8033
+ const line2 = `${notice.from.modelName} ${notice.reason}\uFF0C\u5DF2\u6362\u6210 ${notice.to.providerName} / ${notice.to.modelName}`;
8034
+ return line2.length > 120 ? `${line2.slice(0, 119)}\u2026` : line2;
8035
+ }
8036
+ function fallbackMessage(notice) {
8037
+ return Object.freeze({
8038
+ id: randomUUID4(),
8039
+ role: "user",
8040
+ content: Object.freeze([Object.freeze({ type: "text", text: fallbackText(notice) })]),
8041
+ source: Object.freeze({ kind: MODEL_FALLBACK_SOURCE, form: "notice", summary: fallbackSummary(notice), fallback: notice })
8042
+ });
8043
+ }
8044
+ var ModelFallback = class {
8045
+ #deps;
8046
+ /** Sessions whose request of their own model is under way (from `agent/request` until its reply is logged). */
8047
+ #requesting = /* @__PURE__ */ new Set();
8048
+ /** The models each session has tried since its last completed turn. */
8049
+ #tried = /* @__PURE__ */ new Map();
8050
+ /** The switch decided as the turn failed, to be announced as the agent goes idle. */
8051
+ #pending = /* @__PURE__ */ new WeakMap();
8052
+ constructor(deps) {
8053
+ this.#deps = deps;
8054
+ }
8055
+ /** The agent is about to call its model (dsh's `agent/request`). */
8056
+ requested(sessionId) {
8057
+ this.#requesting.add(sessionId);
8058
+ }
8059
+ /** A session's event was logged: its model answered, or a turn ended. */
8060
+ logged(sessionId, event) {
8061
+ if (event.type === "assistant/message")
8062
+ this.#requesting.delete(sessionId);
8063
+ else if (event.type === "turn/end") {
8064
+ const reason = event.data?.reason;
8065
+ if (reason?.kind === "completed")
8066
+ this.#tried.delete(sessionId);
8067
+ }
8068
+ }
8069
+ /**
8070
+ * The agent's turn failed (dsh's `agent/error`, before the turn's end is
8071
+ * logged). A failure of its own model's request that another model would
8072
+ * not have: the next model is chosen now (so the log shows the switch
8073
+ * before the turn's end) and announced as the agent goes idle.
8074
+ * @returns the switch, if one was made.
8075
+ */
8076
+ failed(agent, error) {
8077
+ const sessionId = agent.session.id;
8078
+ const requesting = this.#requesting.delete(sessionId);
8079
+ if (!requesting || agent.model === void 0 || this.#deps.isOwn?.(sessionId) === false)
8080
+ return void 0;
8081
+ const failure = failureOf(error);
8082
+ const reason = failure === void 0 ? void 0 : fallbackReason(failure);
8083
+ const current = agent.model.current();
8084
+ if (failure === void 0 || reason === void 0 || current === void 0)
8085
+ return void 0;
8086
+ const sub = agent.session.header?.origin === "subagent";
8087
+ let tried = this.#tried.get(sessionId);
8088
+ if (tried === void 0)
8089
+ this.#tried.set(sessionId, tried = /* @__PURE__ */ new Set());
8090
+ tried.add(keyOf2(current));
8091
+ const list = this.#deps.list(sub);
8092
+ const next = nextFallback(current, list, tried, (s) => this.#deps.route(s) !== void 0);
8093
+ if (next === void 0) {
8094
+ if (list.length > 0)
8095
+ this.#deps.log?.warn(`${sessionId}: ${current.provider}/${current.model} failed (${failure.code}) and no fallback model is left to try`);
8096
+ return void 0;
8097
+ }
8098
+ const route = (s) => {
8099
+ const names = this.#deps.route(s);
8100
+ return { provider: s.provider, model: s.model, providerName: names?.providerName ?? s.provider, modelName: names?.modelName ?? s.model };
8101
+ };
8102
+ const notice = {
8103
+ from: route(current),
8104
+ to: route(next),
8105
+ reason,
8106
+ error: failure.message,
8107
+ code: failure.code,
8108
+ ...sub ? { sub: true } : {}
8109
+ };
8110
+ try {
8111
+ agent.model.select({ provider: next.provider, model: next.model, ...next.reasoningEffort === void 0 ? {} : { reasoningEffort: next.reasoningEffort } });
8112
+ } catch (selectError) {
8113
+ this.#deps.log?.warn(`${sessionId}: could not switch to the fallback model ${next.provider}/${next.model}: ${selectError instanceof Error ? selectError.message : String(selectError)}`);
8114
+ return void 0;
8115
+ }
8116
+ this.#pending.set(agent, notice);
8117
+ this.#deps.log?.info(`${sessionId}: ${current.provider}/${current.model} failed (${failure.code}), going on with ${next.provider}/${next.model}`);
8118
+ return notice;
8119
+ }
8120
+ /**
8121
+ * The agent went idle (dsh's `agent/status`): after a switch, a new turn
8122
+ * opens with the person's lost messages and the card. Not inside dsh's
8123
+ * emit (the listeners after this one would hear 「idle」 after the new turn's
8124
+ * 「running」) but in the microtask right after it: still before the end of
8125
+ * the failed turn resolves, so whoever waits for the agent (`whenIdle`,
8126
+ * dsh's sub-agent service) finds it running again and waits on.
8127
+ * @returns whether a new turn is about to open.
8128
+ */
8129
+ idle(agent) {
8130
+ const notice = this.#pending.get(agent);
8131
+ if (notice === void 0 || agent.status !== "idle")
8132
+ return false;
8133
+ this.#pending.delete(agent);
8134
+ queueMicrotask(() => this.#goOn(agent, notice));
8135
+ return true;
8136
+ }
8137
+ #goOn(agent, notice) {
8138
+ if (agent.lifetimeSignal?.aborted === true)
8139
+ return;
8140
+ try {
8141
+ const messages = [...this.#deps.lost?.(agent.session.id) ?? [], fallbackMessage(notice)];
8142
+ for (const message of messages.slice(0, -1)) {
8143
+ if (agent.send !== void 0)
8144
+ agent.send(message, "next-turn", false);
8145
+ else
8146
+ agent.followup(message);
8147
+ }
8148
+ agent.followup(messages.at(-1));
8149
+ } catch (error) {
8150
+ this.#deps.log?.warn(`${agent.session.id}: could not go on with the fallback model: ${error instanceof Error ? error.message : String(error)}`);
8151
+ }
8152
+ }
8153
+ /** The session is gone. */
8154
+ forget(sessionId) {
8155
+ this.#requesting.delete(sessionId);
8156
+ this.#tried.delete(sessionId);
8157
+ }
8158
+ };
7944
8159
 
7945
8160
  // packages/gateway/lib/peer.js
7946
8161
  var CANCEL_REQUEST = "$/cancelRequest";
@@ -8822,6 +9037,7 @@ function ownerInfo(owner) {
8822
9037
  return { owner: { ...owner.scripts === void 0 ? {} : { scripts: [...owner.scripts] }, ...owner.games === void 0 ? {} : { games: [...owner.games] } } };
8823
9038
  }
8824
9039
  var ROLE_CLOSING = [
9040
+ "\u8F6F\u4EF6\u6D3E\u53D1\u7684\u4EFB\u52A1\u7531\u4F60\u7684\u539F\u751F\u5B50 agent \u6267\u884C\u3002\u53EA\u6536\u5230\u6D3E\u53D1\u901A\u77E5\u65F6\u53EF\u4EE5\u7ED3\u675F\u672C\u8F6E\uFF0C\u6536\u5230\u5B50\u4EFB\u52A1\u7684\u4EA4\u4ED8\u901A\u77E5\u540E\u76F4\u63A5\u5904\u7406\u7ED3\u679C\u5E76\u6C47\u62A5\u3002\u81EA\u884C\u5B89\u6392\u672C\u89D2\u8272\u5DE5\u4F5C\u65F6\u7528 subagent\uFF1Bagent_delegate \u7528\u4E8E\u4E0D\u540C\u89D2\u8272\u4E4B\u95F4\u7684\u534F\u4F5C\u3002",
8825
9041
  "\u8FD9\u662F\u4F60\u552F\u4E00\u7684\u5BF9\u8BDD\uFF1A\u7528\u6237\u76F4\u63A5\u627E\u4F60\u8BF4\u7684\u8BDD\u3001\u522B\u7684 agent \u548C\u8F6F\u4EF6\u4EA4\u7ED9\u4F60\u7684\u4E8B\uFF08\u6D88\u606F\u5F00\u5934\u5199\u7740\u300C\uFF08\u2026\u4EA4\u7ED9\u4F60\u7684\u4E8B\uFF1A\u2026\uFF09\u300D\uFF09\u90FD\u5728\u8FD9\u91CC\uFF0C\u4E00\u4EF6\u63A5\u4E00\u4EF6\u3002",
8826
9042
  "\u80FD\u7528\u5DE5\u5177\u3001\u547D\u4EE4\u884C\u3001\u8BFB\u6587\u4EF6\uFF08\u914D\u7F6E\u3001\u65E5\u5FD7\u3001\u7248\u672C\u53F7\uFF09\u6216\u811A\u672C\u81EA\u5DF1\u7684\u547D\u4EE4\u548C\u8BBE\u7F6E\u505A\u5230\u3001\u786E\u8BA4\u5230\u7684\uFF0C\u5C31\u8FD9\u6837\u505A\uFF1B\u622A\u56FE\u3001\u770B\u753B\u9762\u3001\u70B9\u5C4F\u5E55\u662F\u6700\u540E\u7684\u529E\u6CD5\u2014\u2014\u53EA\u6709\u753B\u9762\u4E0A\u624D\u770B\u5F97\u5230\u3001\u522B\u7684\u529E\u6CD5\u90FD\u786E\u8BA4\u4E0D\u4E86\u65F6\u624D\u7528\uFF0C\u7528\u4E4B\u524D\u5148\u8BF4\u4E3A\u4EC0\u4E48\u3002",
8827
9043
  "\u4F60\u6BCF\u4E00\u8F6E\u6700\u540E\u4E00\u6BB5\u56DE\u590D\u5C31\u662F\u8FD9\u4EF6\u4E8B\u7684\u7ED3\u8BBA\uFF1A\u5199\u6E05\u695A\u7ED3\u679C\uFF0C\u4EE5\u53CA\u9700\u8981\u7528\u6237\u505A\u7684\u4E8B\uFF08\u6CA1\u6709\u5C31\u4E0D\u5199\uFF09\u3002\u522B\u4EBA\u4EA4\u7ED9\u4F60\u7684\u4E8B\uFF0C\u8FD9\u6BB5\u56DE\u590D\u4F1A\u4F5C\u4E3A\u7ED3\u8BBA\u4EA4\u56DE\u7ED9\u5B83\uFF1B\u5728\u90A3\u4E4B\u524D\u3001\u5728\u90A3\u4E4B\u540E\u8981\u8BA9\u5B83\u77E5\u9053\u7684\uFF0C\u7528 agent_message \u53D1\u5230\u90A3\u6761\u7EBF\u7D22\u3002"
@@ -8918,6 +9134,10 @@ var AdgGateway = class extends Service {
8918
9134
  roles;
8919
9135
  /** Which model each agent and its sub-agents use (agent-models.ts). */
8920
9136
  agentModels;
9137
+ /** For 备用模型 (model-fallback.ts), the model catalog as last read: services and their models by id, with their names; undefined: not read yet. */
9138
+ catalogNames;
9139
+ catalogReadAt = 0;
9140
+ catalogReading = false;
8921
9141
  /** Choosing a session's model runs one at a time: each puts the default back as it found it (selectSessionModel). */
8922
9142
  modelChoices = Promise.resolve();
8923
9143
  /** Each agent's one conversation, and the work they hand each other (homes.ts). */
@@ -8935,6 +9155,8 @@ var AdgGateway = class extends Service {
8935
9155
  /** Live agents by session, and what narrows each to its role (`narrow`). */
8936
9156
  agents = /* @__PURE__ */ new Map();
8937
9157
  narrowed = /* @__PURE__ */ new Map();
9158
+ appChildren = /* @__PURE__ */ new Set();
9159
+ assignedSpecs = /* @__PURE__ */ new WeakMap();
8938
9160
  subAgentSpecs = /* @__PURE__ */ new WeakMap();
8939
9161
  persona;
8940
9162
  /**
@@ -9128,6 +9350,18 @@ var AdgGateway = class extends Service {
9128
9350
  await this.routeSubAgent(payload.agent);
9129
9351
  }));
9130
9352
  ctx.on("tools/execute", ((exec, next) => {
9353
+ if ((exec.name === "subagent" || exec.name === "list_subagent_models") && exec.agent !== void 0) {
9354
+ const parent = this.specOf(exec.agent.session.id);
9355
+ if (parent !== void 0) {
9356
+ const pinned = exec.name === "subagent" && this.subAgentModel(memoryOwnerOf(parent)) !== void 0;
9357
+ const args = { ...exec.arguments };
9358
+ for (const field of ["provider", "model", "reasoning_effort"]) {
9359
+ if (pinned || args[field] === "")
9360
+ delete args[field];
9361
+ }
9362
+ exec.arguments = args;
9363
+ }
9364
+ }
9131
9365
  if (exec.name === "schedule_create" && exec.agent !== void 0 && typeof exec.arguments.prompt === "string") {
9132
9366
  const desk = this.handOffDesk;
9133
9367
  const noted = desk?.reminderNote?.(exec.agent.session.id, exec.arguments.prompt);
@@ -9267,6 +9501,13 @@ var AdgGateway = class extends Service {
9267
9501
  if (sub !== true && agentId !== void 0)
9268
9502
  void this.refreshHomes([agentId]);
9269
9503
  return model === null ? {} : await this.visionWarning(model, target);
9504
+ },
9505
+ "models/setFallbacks": async (params) => {
9506
+ const { sub, models } = params;
9507
+ await this.agentModels.setFallbacks(sub, models);
9508
+ this.broadcast("models/agentModelsChanged", this.agentModels.state());
9509
+ this.readCatalog(true);
9510
+ return {};
9270
9511
  }
9271
9512
  }
9272
9513
  }), "adg-gateway capability agent-models");
@@ -9516,6 +9757,43 @@ var AdgGateway = class extends Service {
9516
9757
  off();
9517
9758
  };
9518
9759
  });
9760
+ const fallback = new ModelFallback({
9761
+ list: (sub) => this.agentModels.fallbacksOf(sub),
9762
+ route: (selection) => this.routeNames(selection),
9763
+ lost: (sessionId) => this.taken.lost(sessionId),
9764
+ isOwn: (sessionId) => this.isOwn(sessionId),
9765
+ log
9766
+ });
9767
+ this.agentPart("adg-gateway model fallback", (scope) => {
9768
+ const offs = [
9769
+ scope.on("agent/request", ((payload, next) => {
9770
+ if (payload.agent !== void 0) {
9771
+ fallback.requested(payload.agent.session.id);
9772
+ this.readCatalog(false);
9773
+ }
9774
+ return next();
9775
+ }), true),
9776
+ scope.on("session/event", ((session, event) => {
9777
+ fallback.logged(session.id, event);
9778
+ })),
9779
+ scope.on("agent/error", (({ agent, error }) => {
9780
+ fallback.failed(agent, error);
9781
+ })),
9782
+ scope.on("agent/status", (({ agent, status }) => {
9783
+ if (status === "idle")
9784
+ fallback.idle(agent);
9785
+ })),
9786
+ scope.on("agent/disposed", (({ agent }) => {
9787
+ fallback.forget(agent.session.id);
9788
+ }))
9789
+ ];
9790
+ return () => {
9791
+ for (const off of offs)
9792
+ off();
9793
+ };
9794
+ });
9795
+ ctx.on("llm/adapters-updated", (() => this.readCatalog(true)));
9796
+ void this.agentModels.loaded.then(() => this.readCatalog(true));
9519
9797
  if (config.dataDir !== void 0) {
9520
9798
  const persona = new PersonaStore(config.dataDir);
9521
9799
  const loaded = persona.load();
@@ -9904,8 +10182,13 @@ Content-Length: 0
9904
10182
  refused.add(name);
9905
10183
  }
9906
10184
  }
9907
- if (refused.size > 0)
9908
- disposers.push(tools.guard((exec) => !refused.has(exec.name) ? void 0 : home && handsOn(exec.name) ? HANDS_ON_REFUSAL(exec.name) : `${spec.name}\u4E0D\u80FD\u7528 ${exec.name}`));
10185
+ disposers.push(tools.guard((exec) => {
10186
+ if (exec.name === "wait_agent")
10187
+ return "\u5B50\u4EFB\u52A1\u7684\u7EC8\u6001\u4F1A\u81EA\u52A8\u9001\u8FBE\u3002\u5DF2\u6536\u5230\u4EA4\u4ED8\u901A\u77E5\u5C31\u76F4\u63A5\u5904\u7406\u7ED3\u679C\uFF1B\u53EA\u5269\u672A\u4EA4\u4ED8\u7684\u4EFB\u52A1\u65F6\u7ED3\u675F\u672C\u8F6E\u3002";
10188
+ if (refused.has(exec.name))
10189
+ return home && handsOn(exec.name) ? HANDS_ON_REFUSAL(exec.name) : `${spec.name}\u4E0D\u80FD\u7528 ${exec.name}`;
10190
+ return void 0;
10191
+ }));
9909
10192
  if (spec.subAgentOf === void 0) {
9910
10193
  disposers.push(tools.guard((exec) => exec.name === "ask_user_question" ? this.handOffDesk?.askRefusal?.(agent.session.id) : void 0));
9911
10194
  }
@@ -10015,7 +10298,8 @@ Content-Length: 0
10015
10298
  }
10016
10299
  /** The agent working in a session, as other agents address it. A sub-agent is nobody to them. */
10017
10300
  agentOfSession(sessionId) {
10018
- const spec = this.specOf(sessionId);
10301
+ const owner = this.appChildren.has(sessionId) ? this.parentSession(sessionId) : void 0;
10302
+ const spec = this.specOf(owner ?? sessionId);
10019
10303
  return spec === void 0 || spec.subAgentOf !== void 0 ? void 0 : { id: spec.id, name: spec.name };
10020
10304
  }
10021
10305
  /**
@@ -10063,6 +10347,20 @@ Content-Length: 0
10063
10347
  return void 0;
10064
10348
  if (parent.subAgentOf !== void 0)
10065
10349
  return parent;
10350
+ if (this.appChildren.has(sessionId)) {
10351
+ let assigned = this.assignedSpecs.get(parent);
10352
+ if (!assigned) {
10353
+ const child2 = subAgentSpec(parent);
10354
+ const tools = /* @__PURE__ */ new Set(["agent_message", "ask_user_question", ...parent.id === "intervene" ? ["intervention_done"] : []]);
10355
+ assigned = {
10356
+ ...child2,
10357
+ hide: (child2.hide ?? []).filter((name) => !tools.has(name)),
10358
+ duty: child2.duty?.replace("\u4F60\u95EE\u4E0D\u4E86\u7528\u6237\uFF0C\u4E5F\u4E0D\u80FD\u4EA4\u7ED9\u522B\u7684 agent\uFF1A\u7F3A\u4EC0\u4E48\u5C31\u5199\u8FDB\u56DE\u590D\uFF0C\u7531\u300C" + parent.name + "\u300D\u53BB\u95EE\u3002", "\u9700\u8981\u8865\u5145\u4FE1\u606F\u65F6\u901A\u8FC7\u5F53\u524D\u59D4\u6D3E\u7EBF\u7D22\u8054\u7CFB\u53D1\u8D77\u65B9\uFF1B\u76F4\u63A5\u7531\u8F6F\u4EF6\u53D1\u8D77\u7684\u4EFB\u52A1\u53EF\u4EE5\u8BE2\u95EE\u7528\u6237\u3002") ?? ""
10359
+ };
10360
+ this.assignedSpecs.set(parent, assigned);
10361
+ }
10362
+ return assigned;
10363
+ }
10066
10364
  let child = this.subAgentSpecs.get(parent);
10067
10365
  if (child === void 0)
10068
10366
  this.subAgentSpecs.set(parent, child = subAgentSpec(parent));
@@ -10102,7 +10400,76 @@ Content-Length: 0
10102
10400
  return;
10103
10401
  }
10104
10402
  }
10105
- /** Stop one sub-agent, on behalf of the agent that opened it. */
10403
+ /** App actions are addressed to an agent's home. dsh owns child membership, model selection, settlement and parent delivery. */
10404
+ async runChild(owner, childId, title, instruction, signal, images = []) {
10405
+ if (!this.homes.agentOf(owner))
10406
+ throw new Error("\u4EFB\u52A1\u5FC5\u987B\u4ECE\u52A9\u624B\u7684\u4E3B\u5BF9\u8BDD\u6D3E\u53D1");
10407
+ const service = this.ctx.get("subagents");
10408
+ if (!service)
10409
+ throw new Error("\u539F\u751F\u5B50\u4EE3\u7406\u670D\u52A1\u672A\u52A0\u8F7D");
10410
+ const resolved = await this.ctx.sessionController.resolveAgent(owner);
10411
+ if (!("agent" in resolved))
10412
+ throw new Error("\u65E0\u6CD5\u6062\u590D\u8D1F\u8D23\u8FD9\u9879\u4EFB\u52A1\u7684\u4E3B\u52A9\u624B");
10413
+ const parent = resolved.agent;
10414
+ signal.throwIfAborted();
10415
+ const ptc = this.compaction?.state().settings.ptcSubagents ?? true;
10416
+ this.appChildren.add(childId);
10417
+ try {
10418
+ const activation = await service.startActivation({
10419
+ provider: "spawn",
10420
+ parent,
10421
+ childId,
10422
+ label: title,
10423
+ ...ptc ? { agentPreset: this.shared ? "mga-ptc" : "ptc" } : {},
10424
+ prompt: [{ type: "text", text: instruction }, ...images.map((png) => ({ type: "image", mediaType: "image/png", data: png.toString("base64") }))],
10425
+ delivery: "parent",
10426
+ signal
10427
+ });
10428
+ let stopping;
10429
+ const cancel = () => {
10430
+ stopping ??= activation.dispose();
10431
+ void stopping.catch(() => {
10432
+ });
10433
+ };
10434
+ signal.addEventListener("abort", cancel, { once: true });
10435
+ try {
10436
+ if (signal.aborted) {
10437
+ cancel();
10438
+ signal.throwIfAborted();
10439
+ }
10440
+ await parent.send({
10441
+ id: randomUUID5(),
10442
+ role: "user",
10443
+ content: [{ type: "text", text: `\uFF08\u4F60\u8D1F\u8D23\u7684\u5B50\u4EFB\u52A1\uFF1A${title}\uFF09
10444
+ \u5DF2\u5C06\u4E0B\u5217\u6307\u4EE4\u6D3E\u7ED9\u4F60\u7684\u5B50 agent\u300C${activation.name}\u300D\u3002\u5B83\u4F1A\u76F4\u63A5\u5411\u4F60\u4EA4\u4ED8\u7ED3\u679C\u3002\u7ED3\u8BBA\u4F1A\u81EA\u52A8\u9001\u8FBE\uFF0C\u6536\u5230\u540E\u5411\u7528\u6237\u6C47\u62A5\u3002\u7B49\u5F85\u671F\u95F4\u53EF\u4EE5\u5904\u7406\u5176\u4ED6\u6D88\u606F\uFF0C\u65E0\u9700\u8F6E\u8BE2\u6216\u91CD\u590D\u6D3E\u53D1\u3002
10445
+
10446
+ ${instruction}` }],
10447
+ source: { kind: "adg-task-dispatched", form: "notice", summary: title, childId }
10448
+ }, "next-step", false);
10449
+ const result = await activation.result;
10450
+ signal.throwIfAborted();
10451
+ if (result.stopReason === "aborted" || result.stopReason === "interrupted")
10452
+ throw new DOMException(result.diagnostic || "\u5B50\u4EFB\u52A1\u5DF2\u505C\u6B62", "AbortError");
10453
+ if (result.stopReason !== "completed")
10454
+ throw new Error(result.diagnostic || `\u5B50\u4EFB\u52A1\u4E2D\u65AD\uFF08${result.stopReason}\uFF09`);
10455
+ const answer = result.output.filter((p) => p.type === "text").map((p) => p.text ?? "").join("\n").trim();
10456
+ if (!answer)
10457
+ throw new Error("\u5B50\u4EE3\u7406\u7ED3\u675F\u65F6\u6CA1\u6709\u4EA4\u56DE\u7ED3\u8BBA");
10458
+ return answer;
10459
+ } catch (error) {
10460
+ cancel();
10461
+ throw error;
10462
+ } finally {
10463
+ signal.removeEventListener("abort", cancel);
10464
+ await stopping;
10465
+ }
10466
+ } finally {
10467
+ this.appChildren.delete(childId);
10468
+ const child = this.agents.get(childId);
10469
+ if (child)
10470
+ this.narrow(child);
10471
+ }
10472
+ }
10106
10473
  stopSubAgent(sessionId) {
10107
10474
  const parent = this.agents.get(sessionId)?.session.header?.parentSession;
10108
10475
  if (parent === void 0)
@@ -10112,7 +10479,7 @@ Content-Length: 0
10112
10479
  /** Stop the sub-agents working under this session's agent (a hand-off called off stops them with it). */
10113
10480
  stopSubAgents(sessionId) {
10114
10481
  for (const agent of this.agents.values()) {
10115
- if (agent.session.header?.parentSession !== sessionId || agent.session.header.origin !== "subagent")
10482
+ if (this.appChildren.has(agent.session.id) || agent.session.header?.parentSession !== sessionId || agent.session.header.origin !== "subagent")
10116
10483
  continue;
10117
10484
  try {
10118
10485
  this.stopSubAgent(agent.session.id);
@@ -10136,6 +10503,29 @@ Content-Length: 0
10136
10503
  return void 0;
10137
10504
  return this.agentModels.of(agentId);
10138
10505
  }
10506
+ /**
10507
+ * Read the model catalog again for the fallback card's names and which
10508
+ * fallback models can be used; `force` false: at most once a minute. Never throws.
10509
+ */
10510
+ readCatalog(force) {
10511
+ if (this.catalogReading || !force && Date.now() - this.catalogReadAt < 6e4)
10512
+ return;
10513
+ this.catalogReading = true;
10514
+ this.catalogReadAt = Date.now();
10515
+ void this.ctx.sessionController.modelCatalog().then((catalog) => {
10516
+ this.catalogNames = new Map(catalog.groups.map((g) => [g.id, { name: g.name, models: new Map(g.models.map((m) => [m.id, m.name])) }]));
10517
+ }).catch((error) => this.log.warn(`reading the model catalog for the fallback models: ${error instanceof Error ? error.message : String(error)}`)).finally(() => {
10518
+ this.catalogReading = false;
10519
+ });
10520
+ }
10521
+ /** A model's service and model names; undefined: the catalog was read and has no such model now. */
10522
+ routeNames(selection) {
10523
+ if (this.catalogNames === void 0)
10524
+ return { providerName: selection.provider, modelName: selection.model };
10525
+ const group = this.catalogNames.get(selection.provider);
10526
+ const model = group?.models.get(selection.model);
10527
+ return group === void 0 || model === void 0 ? void 0 : { providerName: group.name, modelName: model };
10528
+ }
10139
10529
  /** The model an agent's sub-agents are given: theirs, else every sub-agent's; undefined: whatever the agent is using. */
10140
10530
  subAgentModel(agentId) {
10141
10531
  return this.agentModels.subOf(agentId);
@@ -10190,8 +10580,7 @@ Content-Length: 0
10190
10580
  * (agent-models.ts); else dsh gives it its parent's. The choice is made as
10191
10581
  * the sub-agent is created (agent.model, as dsh's own model choice is), so its
10192
10582
  * steps use that model from the first and its system prompt names it. A model
10193
- * that cannot be used now (its service gone) leaves it on its parent's rather
10194
- * than failing the work. Never throws: a throw would fail the sub-agent.
10583
+ * that cannot be resolved is tried against the configured child fallback list.
10195
10584
  */
10196
10585
  async routeSubAgent(agent) {
10197
10586
  if (agent.session.header?.origin !== "subagent" || !this.isOwn(agent.session.id))
@@ -10200,18 +10589,26 @@ Content-Length: 0
10200
10589
  if (spec?.subAgentOf === void 0)
10201
10590
  return;
10202
10591
  const model = this.subAgentModel(memoryOwnerOf(spec));
10203
- if (model === void 0 || agent.model === void 0)
10592
+ if (model === void 0)
10204
10593
  return;
10205
- const route = { provider: model.provider, model: model.model, ...model.reasoningEffort === void 0 ? {} : { reasoningEffort: model.reasoningEffort } };
10206
10594
  const llm = this.ctx.get("llm");
10207
- if (llm === void 0)
10208
- return;
10209
- try {
10210
- await llm.resolveCallConfig(route, AbortSignal.timeout(1e4));
10211
- agent.model.select(route);
10212
- } catch (error) {
10213
- this.log.warn(`the sub-agent model ${model.provider}/${model.model} cannot be used now, ${agent.session.id} keeps its parent's: ${error instanceof Error ? error.message : String(error)}`);
10595
+ if (llm === void 0 || agent.model === void 0)
10596
+ throw new Error("\u5B50\u4EE3\u7406\u6A21\u578B\u670D\u52A1\u5C1A\u672A\u5C31\u7EEA");
10597
+ const tried = /* @__PURE__ */ new Set();
10598
+ for (const route of [model, ...this.agentModels.fallbacksOf(true)]) {
10599
+ const key = `${route.provider}/${route.model}`;
10600
+ if (tried.has(key))
10601
+ continue;
10602
+ tried.add(key);
10603
+ try {
10604
+ await llm.resolveCallConfig(route, AbortSignal.timeout(1e4));
10605
+ agent.model.select({ provider: route.provider, model: route.model, ...route.reasoningEffort === void 0 ? {} : { reasoningEffort: route.reasoningEffort } });
10606
+ return;
10607
+ } catch (error) {
10608
+ this.log.warn(`\u5B50\u4EE3\u7406\u6A21\u578B ${key} \u65E0\u6CD5\u4F7F\u7528\uFF1A${String(error)}`);
10609
+ }
10214
10610
  }
10611
+ throw new Error(`\u5B50\u4EE3\u7406\u6A21\u578B\u5747\u4E0D\u53EF\u7528\uFF1A${[...tried].join("\u3001")}`);
10215
10612
  }
10216
10613
  /** Why a model chosen for this layer will not do: it cannot see images and the work there looks at screenshots. */
10217
10614
  async visionWarning(model, target) {