dsh-multigameassistant 2.0.1 → 2.0.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.
@@ -1,7 +1,7 @@
1
1
  import { createRequire as __adgCreateRequire } from 'node:module'; import { dirname as __adgDirname } from 'node:path'; import { fileURLToPath as __adgFileURLToPath } from 'node:url'; const require = __adgCreateRequire(import.meta.url); const __filename = __adgFileURLToPath(import.meta.url); const __dirname = __adgDirname(__filename);
2
2
  import {
3
3
  convertRecord
4
- } from "./chunk-ZQ6KFVEO.js";
4
+ } from "./chunk-OPV336S5.js";
5
5
  import {
6
6
  writeFileAtomic
7
7
  } from "./chunk-CCAB6XXZ.js";
@@ -12,12 +12,16 @@ import {
12
12
  isQuiet,
13
13
  parseRelay,
14
14
  parseTask,
15
+ parseThreadNote,
15
16
  relayMessage,
16
17
  reminderLine,
17
18
  reminderPrompts,
18
19
  summarizeToolArgs,
19
- taskMessage
20
- } from "./chunk-HPQCV6WO.js";
20
+ taskMessage,
21
+ threadMessage,
22
+ threadReminder,
23
+ threadsBetween
24
+ } from "./chunk-QAP7IYBS.js";
21
25
 
22
26
  // packages/notify/lib/index.js
23
27
  import { readFile } from "node:fs/promises";
@@ -116,66 +120,294 @@ function tally(reply) {
116
120
  // packages/notify/lib/handoff.js
117
121
  var MAIN = "main";
118
122
  var RELAY_TIMEOUT_MS = 20 * 6e4;
119
- async function handOff(deps, input) {
123
+ var PAST_SHOWN = 5;
124
+ var PAST_CHARS = 120;
125
+ function pastBetween(all, a, b, name) {
126
+ const between = threadsBetween(all, a, b).filter((h) => h.state !== "running" && h.state !== "waiting");
127
+ return between.slice(-PAST_SHOWN).map((h) => {
128
+ const when = new Date(h.endedAt ?? h.startedAt);
129
+ const said = (h.reply ?? "").replace(/\s+/g, " ").trim();
130
+ const end = said === "" ? "" : `\uFF1A${said.length > PAST_CHARS ? `${said.slice(0, PAST_CHARS)}\u2026` : said}`;
131
+ return `- ${when.getMonth() + 1}-${when.getDate()} ${h.by}\u4EA4\u7ED9${name(h.to)}\u300C${h.title}\u300D${h.state === "failed" ? "\uFF08\u6CA1\u505A\u6210\uFF09" : ""}${end}\uFF08\u7EBF\u7D22 ${h.id}\uFF09`;
132
+ });
133
+ }
134
+ var MAX_THREAD_MESSAGES = 10;
135
+ var MAX_HOPS = 3;
136
+ var MAX_CHAIN_MESSAGES = 30;
137
+ function createHandOffDesk(deps) {
120
138
  const now = deps.now ?? Date.now;
121
- const toSession = await deps.home(input.to.id, input.focus === void 0 ? {} : { focus: input.focus });
122
- const relay = input.relay === true;
123
- const fromSession = input.from?.sessionId ?? (relay ? await deps.home(MAIN) : "");
124
- const asked = {
125
- id: input.from?.callId ?? `h-${randomUUID2().slice(0, 8)}`,
126
- fromSession,
127
- ...input.from?.callId === void 0 ? {} : { callId: input.from.callId },
128
- by: input.by,
129
- to: input.to.id,
130
- toSession,
131
- title: input.title,
132
- task: input.task,
133
- ...input.expecting === void 0 ? {} : { expecting: input.expecting },
134
- state: "running",
135
- startedAt: now()
139
+ const live = /* @__PURE__ */ new Map();
140
+ const workingOn = (sessionId) => [...live.values()].filter((t) => t.item.toSession === sessionId && t.conclusion === void 0).at(-1);
141
+ const chainTop = (thread) => thread.root ?? thread;
142
+ const save = async (thread, patch) => {
143
+ thread.item = { ...thread.item, ...patch };
144
+ await deps.record(thread.item);
145
+ };
146
+ const say = async (thread, from, kind, text, patch = {}) => {
147
+ chainTop(thread).said++;
148
+ await save(thread, { ...patch, messages: [...thread.item.messages ?? [], { from, kind, text, at: now() }] });
149
+ };
150
+ const toWorker = (thread, message2) => {
151
+ thread.turns.push(deps.converse(thread.item.toSession, message2, thread.signal, [], { insert: true }));
152
+ thread.wake();
153
+ };
154
+ const askerKind = (agent) => agent === void 0 || agent === MAIN ? "main" : "lead";
155
+ const late = async ({ thread: id, fromSession, from, kind, text }) => {
156
+ const item = deps.threads?.().find((h) => h.id === id);
157
+ if (item === void 0)
158
+ throw new Error(`\u6CA1\u6709\u7EBF\u7D22 ${id}\uFF08\u53EF\u80FD\u592A\u4E45\u4EE5\u524D\uFF0C\u5DF2\u7ECF\u6E05\u6389\u4E86\uFF09\u3002\u8FD8\u6709\u4E8B\u5C31\u91CD\u65B0\u4EA4\u4E00\u6B21`);
159
+ if ((item.messages ?? []).length >= MAX_THREAD_MESSAGES)
160
+ throw new Error(`\u8FD9\u6761\u7EBF\u7D22\u7684\u6765\u56DE\u5DF2\u7ECF\u591F\u591A\u4E86\uFF08${MAX_THREAD_MESSAGES} \u6761\uFF09\uFF1A\u8FD8\u6709\u4E8B\u5C31\u91CD\u65B0\u4EA4\u4E00\u6B21`);
161
+ const askerAgent = item.chain?.at(-2);
162
+ const askerNow = askerAgent === void 0 ? item.fromSession : await deps.home(askerAgent);
163
+ const workerNow = await deps.home(item.to);
164
+ const worker = fromSession === item.toSession || fromSession === workerNow;
165
+ if (!worker && fromSession !== item.fromSession && fromSession !== askerNow)
166
+ throw new Error(`\u7EBF\u7D22 ${id} \u4E0D\u662F\u4F60\u548C\u5BF9\u65B9\u4E4B\u95F4\u7684`);
167
+ if (worker && askerNow === "")
168
+ throw new Error(`\u8FD9\u4EF6\u4E8B\u662F${item.by}\u4EA4\u7684\uFF0C\u6CA1\u6709\u4EBA\u5728\u7B49\u6D88\u606F\uFF1A\u8981\u544A\u8BC9\u8C01\u5C31\u91CD\u65B0\u4EA4\u7ED9\u5B83`);
169
+ const said = kind === "done" ? "update" : kind;
170
+ await deps.record({ ...item, messages: [...item.messages ?? [], { from, kind: said, text, at: now() }] });
171
+ const message2 = threadMessage({ from, kind: said, thread: id, title: item.title, text, reader: worker ? askerKind(askerAgent) : "worker", closed: true });
172
+ void deps.converse(worker ? askerNow : workerNow, message2, AbortSignal.timeout(RELAY_TIMEOUT_MS), [], { insert: true }).catch(() => {
173
+ });
174
+ return "\u5DF2\u7ECF\u9001\u5230\u5BF9\u65B9\u90A3\u91CC\u4E86\u3002";
175
+ };
176
+ const toAsker = (thread, message2, asked) => {
177
+ void deps.converse(thread.item.fromSession, message2, AbortSignal.any([thread.signal, AbortSignal.timeout(RELAY_TIMEOUT_MS)]), [], { insert: true }).then((answer) => {
178
+ if (!asked || !thread.awaiting || thread.deferred || !live.has(thread.item.id))
179
+ return;
180
+ const said = finalText(answer).trim();
181
+ void answered(thread, thread.item.by, said === "" || isQuiet(said) ? "\uFF08\u5B83\u6CA1\u6709\u56DE\u7B54\uFF1A\u6309\u4F60\u81EA\u5DF1\u7684\u5224\u65AD\u63A5\u7740\u505A\u3002\uFF09" : said);
182
+ }, () => {
183
+ if (asked && thread.awaiting && live.has(thread.item.id))
184
+ void answered(thread, thread.item.by, "\uFF08\u6CA1\u6709\u7B49\u5230\u56DE\u7B54\uFF1A\u6309\u4F60\u81EA\u5DF1\u7684\u5224\u65AD\u63A5\u7740\u505A\u3002\uFF09");
185
+ });
186
+ };
187
+ const answered = async (thread, from, text) => {
188
+ thread.awaiting = false;
189
+ thread.deferred = false;
190
+ toWorker(thread, threadMessage({ from, kind: "answer", thread: thread.item.id, title: thread.item.title, text }));
191
+ await say(thread, from, "answer", text, { state: "running" });
136
192
  };
137
- await deps.record(asked);
138
- const body = input.expecting === void 0 ? input.task : `${input.task}
193
+ return {
194
+ refusal(fromSession, to, fromAgent, toName = to) {
195
+ const parent = workingOn(fromSession);
196
+ if (parent === void 0)
197
+ return to === fromAgent ? "\u4E0D\u80FD\u4EA4\u7ED9\u81EA\u5DF1" : void 0;
198
+ const chain = parent.item.chain ?? [];
199
+ if (to === fromAgent)
200
+ return "\u4E0D\u80FD\u4EA4\u7ED9\u81EA\u5DF1";
201
+ if (chain.includes(to))
202
+ return `\u300C${toName}\u300D\u6B63\u5728\u7B49\u4F60\u8FD9\u6761\u7EBF\u7684\u7ED3\u679C\uFF0C\u518D\u4EA4\u7ED9\u5B83\u5C31\u7ED5\u56DE\u53BB\u4E86\uFF1A\u5728\u7EBF\u7D22 ${parent.item.id} \u91CC\u7528 agent_message \u76F4\u63A5\u95EE\u5B83`;
203
+ if (chain.length > MAX_HOPS)
204
+ return `\u8FD9\u4EF6\u4E8B\u5DF2\u7ECF\u8F6C\u4E86 ${MAX_HOPS} \u624B\uFF0C\u4E0D\u80FD\u518D\u5F80\u4E0B\u4EA4\u4E86\uFF1A\u7528\u4F60\u624B\u4E0A\u7684\u4FE1\u606F\u505A\u5B8C\uFF0C\u5728\u7ED3\u8BBA\u91CC\u8BF4\u6E05\u695A\u7F3A\u4EC0\u4E48`;
205
+ if (chainTop(parent).said >= MAX_CHAIN_MESSAGES)
206
+ return `\u8FD9\u4EF6\u4E8B\u7684\u6765\u56DE\u5DF2\u7ECF\u591F\u591A\u4E86\uFF08${MAX_CHAIN_MESSAGES} \u6761\uFF09\uFF1A\u7528\u4F60\u624B\u4E0A\u7684\u4FE1\u606F\u505A\u5B8C\uFF0C\u5728\u7ED3\u8BBA\u91CC\u8BF4\u6E05\u695A\u7F3A\u4EC0\u4E48`;
207
+ return void 0;
208
+ },
209
+ askRefusal(sessionId) {
210
+ const thread = workingOn(sessionId);
211
+ if (thread?.item.callId === void 0 || thread.item.fromSession === "")
212
+ return void 0;
213
+ return `\u8FD9\u4EF6\u4E8B\u662F${thread.item.by}\u4EA4\u7ED9\u4F60\u7684\uFF1A\u8981\u95EE\u6E05\u695A\u7684\u7528 agent_message \u95EE\u5B83\uFF08thread \u586B ${thread.item.id}\uFF0Ckind \u586B question\uFF09\uFF0C\u8981\u4E0D\u8981\u53BB\u95EE\u7528\u6237\u7531\u5B83\u51B3\u5B9A\u3002\u4E0D\u8981\u731C\uFF0C\u4E5F\u4E0D\u8981\u81EA\u5DF1\u95EE\u7528\u6237\u3002`;
214
+ },
215
+ reminderNote(sessionId, prompt) {
216
+ const thread = workingOn(sessionId);
217
+ return thread === void 0 || thread.item.fromSession === "" ? void 0 : threadReminder(prompt, thread.item.id, thread.item.by);
218
+ },
219
+ async message(input) {
220
+ const { thread: id, fromSession, from, kind, text } = input;
221
+ const thread = live.get(id);
222
+ if (thread === void 0)
223
+ return late(input);
224
+ const worker = fromSession === thread.item.toSession;
225
+ if (!worker && fromSession !== thread.item.fromSession)
226
+ throw new Error(`\u7EBF\u7D22 ${id} \u4E0D\u662F\u4F60\u548C\u5BF9\u65B9\u4E4B\u95F4\u7684`);
227
+ if (kind !== "done") {
228
+ if ((thread.item.messages ?? []).length >= MAX_THREAD_MESSAGES)
229
+ throw new Error(`\u8FD9\u6761\u7EBF\u7D22\u7684\u6765\u56DE\u5DF2\u7ECF\u591F\u591A\u4E86\uFF08${MAX_THREAD_MESSAGES} \u6761\uFF09\uFF1A\u7528\u624B\u4E0A\u7684\u4FE1\u606F\u505A\u4E2A\u7ED3\u8BBA`);
230
+ if (chainTop(thread).said >= MAX_CHAIN_MESSAGES)
231
+ throw new Error(`\u8FD9\u4EF6\u4E8B\u7684\u6765\u56DE\u5DF2\u7ECF\u591F\u591A\u4E86\uFF08${MAX_CHAIN_MESSAGES} \u6761\uFF09\uFF1A\u7528\u624B\u4E0A\u7684\u4FE1\u606F\u505A\u4E2A\u7ED3\u8BBA`);
232
+ }
233
+ const asker = thread.item.by;
234
+ switch (kind) {
235
+ case "done":
236
+ if (!worker)
237
+ throw new Error("\u53EA\u6709\u505A\u8FD9\u4EF6\u4E8B\u7684\u4E00\u65B9\u80FD\u4E0B\u7ED3\u8BBA");
238
+ thread.conclusion = text;
239
+ await say(thread, from, "done", text);
240
+ thread.wake();
241
+ return "\u7ED3\u8BBA\u5DF2\u7ECF\u4EA4\u56DE\u53BB\u4E86\u3002\u8FD9\u4E00\u8F6E\u53EF\u4EE5\u7ED3\u675F\u4E86\u3002";
242
+ case "update":
243
+ if (!worker) {
244
+ thread.deferred = true;
245
+ await say(thread, from, "update", text);
246
+ toWorker(thread, threadMessage({ from, kind, thread: id, title: thread.item.title, text }));
247
+ return "\u5DF2\u7ECF\u544A\u8BC9\u5B83\u4E86\u3002\u6709\u7ED3\u679C\u8981\u56DE\u7B54\u5B83\u7684\u8BDD\uFF0C\u7528 kind: answer\u3002";
248
+ }
249
+ await say(thread, from, "update", text);
250
+ if (thread.item.fromSession === "")
251
+ return "\u8BB0\u4E0B\u4E86\u3002\u63A5\u7740\u505A\u3002";
252
+ toAsker(thread, threadMessage({ from, kind, thread: id, title: thread.item.title, text, reader: askerKind(thread.input.from?.agent) }), false);
253
+ return "\u5DF2\u7ECF\u9001\u5230\u5BF9\u65B9\u90A3\u91CC\u4E86\u3002\u63A5\u7740\u505A\u3002";
254
+ case "question":
255
+ if (worker) {
256
+ if (thread.item.fromSession === "")
257
+ throw new Error(`\u8FD9\u4EF6\u4E8B\u662F${asker}\u4EA4\u7684\uFF0C\u6CA1\u6709\u4EBA\u7B49\u7740\u56DE\u7B54\uFF1A\u6309\u4F60\u81EA\u5DF1\u7684\u5224\u65AD\u505A\uFF0C\u628A\u8981\u95EE\u7684\u5199\u8FDB\u7ED3\u8BBA`);
258
+ thread.awaiting = true;
259
+ await say(thread, from, "question", text, { state: "waiting" });
260
+ toAsker(thread, threadMessage({ from, kind, thread: id, title: thread.item.title, text }), true);
261
+ return `\u5DF2\u7ECF\u95EE\u4E86${asker}\u3002\u5B83\u56DE\u7B54\u540E\u4F1A\u4F5C\u4E3A\u4E00\u6761\u65B0\u6D88\u606F\u53D1\u5230\u8FD9\u91CC\uFF1A\u73B0\u5728\u7ED3\u675F\u8FD9\u4E00\u8F6E\uFF0C\u4E0D\u8981\u66FF\u5B83\u56DE\u7B54\u3002`;
262
+ }
263
+ toWorker(thread, threadMessage({ from, kind, thread: id, title: thread.item.title, text }));
264
+ await say(thread, from, "question", text);
265
+ return `\u5DF2\u7ECF\u95EE\u4E86\u300C${thread.item.to}\u300D\u3002\u5B83\u56DE\u7B54\u540E\u4F1A\u4F5C\u4E3A\u4E00\u6761\u65B0\u6D88\u606F\u53D1\u5230\u8FD9\u91CC\uFF1A\u73B0\u5728\u7ED3\u675F\u8FD9\u4E00\u8F6E\u3002`;
266
+ case "answer":
267
+ if (!worker) {
268
+ await answered(thread, from, text);
269
+ return "\u5DF2\u7ECF\u56DE\u7B54\u4E86\u3002\u73B0\u5728\u7ED3\u675F\u8FD9\u4E00\u8F6E\u3002";
270
+ }
271
+ await say(thread, from, "answer", text);
272
+ toAsker(thread, threadMessage({ from, kind, thread: id, title: thread.item.title, text, reader: askerKind(thread.input.from?.agent) }), false);
273
+ return "\u5DF2\u7ECF\u56DE\u7B54\u4E86\u3002\u63A5\u7740\u505A\u3002";
274
+ }
275
+ },
276
+ async handOff(input) {
277
+ const parent = input.from === void 0 ? void 0 : workingOn(input.from.sessionId);
278
+ if (parent !== void 0) {
279
+ parent.open++;
280
+ parent.consulted.add(input.to.name);
281
+ }
282
+ const toSession = await deps.home(input.to.id, input.focus === void 0 ? {} : { focus: input.focus });
283
+ const relay = input.relay === true;
284
+ const fromSession = input.from?.sessionId ?? (relay ? await deps.home(MAIN) : "");
285
+ const id = input.from?.callId ?? `h-${randomUUID2().slice(0, 8)}`;
286
+ const chain = [...parent?.item.chain ?? (input.from?.agent === void 0 ? [] : [input.from.agent]), input.to.id];
287
+ const item = {
288
+ id,
289
+ fromSession,
290
+ ...input.from?.callId === void 0 ? {} : { callId: input.from.callId },
291
+ by: input.by,
292
+ to: input.to.id,
293
+ toSession,
294
+ title: input.title,
295
+ task: input.task,
296
+ ...input.expecting === void 0 ? {} : { expecting: input.expecting },
297
+ chain,
298
+ ...parent === void 0 ? {} : { parent: parent.item.id },
299
+ state: "running",
300
+ startedAt: now()
301
+ };
302
+ const body = input.expecting === void 0 ? input.task : `${input.task}
139
303
 
140
304
  \u8981\u4EA4\u56DE\u7684\uFF1A${input.expecting}`;
141
- let reply;
142
- let failure;
143
- let thrown;
144
- try {
145
- reply = await deps.converse(toSession, taskMessage(input.by, input.title, body, relay), input.signal, input.images);
146
- const said = finalText(reply);
147
- if (reply.ended !== void 0)
148
- failure = `${reply.ended.replace(/^(|)$/g, "")}${said === "" ? "" : `\u3002\u5B83\u6700\u540E\u8BF4\uFF1A${said}`}`;
149
- else if (said === "")
150
- failure = "\u5B83\u6CA1\u6709\u7559\u4E0B\u56DE\u590D";
151
- } catch (error) {
152
- thrown = error;
153
- failure = error instanceof Error ? error.message : String(error);
154
- }
155
- const text = failure ?? finalText(reply);
156
- await deps.record({ ...asked, state: failure === void 0 ? "done" : "failed", reply: text, endedAt: now() });
157
- if (relay && fromSession !== "") {
158
- const message2 = relayMessage({ agent: input.to.name, asker: input.by === input.from?.asker ? "\u4F60" : input.by, title: input.title, ok: failure === void 0, reply: text });
159
- void deps.converse(fromSession, message2, AbortSignal.timeout(RELAY_TIMEOUT_MS)).then((answer) => input.relayed?.(answer), () => {
160
- });
161
- }
162
- if (thrown !== void 0)
163
- throw thrown;
164
- return reply;
305
+ const signal = parent === void 0 ? input.signal : AbortSignal.any([input.signal, parent.signal]);
306
+ let wake = () => {
307
+ };
308
+ const thread = {
309
+ item,
310
+ input,
311
+ signal,
312
+ root: parent === void 0 ? void 0 : chainTop(parent),
313
+ turns: [],
314
+ awaiting: false,
315
+ deferred: false,
316
+ conclusion: void 0,
317
+ open: 0,
318
+ consulted: /* @__PURE__ */ new Set(),
319
+ said: 1,
320
+ wake: () => wake()
321
+ };
322
+ if (parent !== void 0)
323
+ chainTop(parent).said++;
324
+ live.set(id, thread);
325
+ const stopped = new Promise((_, reject) => {
326
+ if (signal.aborted)
327
+ reject(signal.reason);
328
+ else
329
+ signal.addEventListener("abort", () => reject(signal.reason), { once: true });
330
+ });
331
+ stopped.catch(() => {
332
+ });
333
+ await deps.record(item);
334
+ const asker = input.from?.agent;
335
+ const past = asker === void 0 || deps.threads === void 0 ? [] : pastBetween(deps.threads(), asker, input.to.id, (agent) => agent === input.to.id ? input.to.name : input.by);
336
+ thread.turns.push(deps.converse(toSession, taskMessage(input.by, input.title, body, relay, id, { who: `\u300C${input.by}\u300D`, lines: past }), signal, input.images));
337
+ let reply;
338
+ let failure;
339
+ let thrown;
340
+ try {
341
+ for (; ; ) {
342
+ while (thread.turns.length > 0) {
343
+ const turn = await thread.turns.shift();
344
+ if (reply === void 0 || finalText(turn) !== "" || turn.ended !== void 0)
345
+ reply = turn;
346
+ }
347
+ if (thread.conclusion !== void 0 && thread.open === 0)
348
+ break;
349
+ if (!thread.awaiting && thread.open === 0)
350
+ break;
351
+ await Promise.race([new Promise((resolve) => {
352
+ wake = resolve;
353
+ }), stopped]);
354
+ }
355
+ const said = finalText(reply);
356
+ if (thread.conclusion === void 0) {
357
+ if (reply.ended !== void 0)
358
+ failure = `${reply.ended.replace(/^(|)$/g, "")}${said === "" ? "" : `\u3002\u5B83\u6700\u540E\u8BF4\uFF1A${said}`}`;
359
+ else if (said === "")
360
+ failure = "\u5B83\u6CA1\u6709\u7559\u4E0B\u56DE\u590D";
361
+ }
362
+ } catch (error) {
363
+ thrown = error;
364
+ failure = error instanceof Error ? error.message : String(error);
365
+ }
366
+ live.delete(id);
367
+ const text = failure ?? thread.conclusion ?? finalText(reply);
368
+ await save(thread, { state: failure === void 0 ? "done" : "failed", reply: text, endedAt: now() });
369
+ if (relay && fromSession !== "") {
370
+ const message2 = relayMessage({
371
+ agent: input.to.name,
372
+ asker: input.by === input.from?.asker ? "\u4F60" : input.by,
373
+ title: input.title,
374
+ ok: failure === void 0,
375
+ reply: text,
376
+ ...thread.consulted.size === 0 ? {} : { consulted: [...thread.consulted] },
377
+ ...input.from?.agent === void 0 || input.from.agent === MAIN ? {} : { toLead: true }
378
+ });
379
+ const back = deps.converse(fromSession, message2, AbortSignal.timeout(RELAY_TIMEOUT_MS));
380
+ if (parent !== void 0 && live.has(parent.item.id))
381
+ parent.turns.push(back.catch(() => ({ blocks: [] })));
382
+ void back.then((answer) => input.relayed?.(answer), () => {
383
+ });
384
+ }
385
+ if (parent !== void 0) {
386
+ parent.open--;
387
+ parent.wake();
388
+ }
389
+ if (thrown !== void 0)
390
+ throw thrown;
391
+ return reply;
392
+ }
393
+ };
165
394
  }
166
395
 
167
396
  // packages/notify/lib/conversations.js
397
+ var SUB_AGENT_TOOLS = /* @__PURE__ */ new Set(["subagent", "subagent_fork"]);
398
+ var QUIET_MS = 250;
168
399
  function sessionConversations(sessions, cwd = () => void 0, focus, app) {
169
- const converse = async (sessionId, text, signal, images = []) => {
400
+ const converse = async (sessionId, text, signal, images = [], options = {}) => {
170
401
  const controller = new AbortController();
171
402
  const requestId = `notify-${crypto.randomUUID()}`;
172
403
  const turn = { started: false };
173
404
  const abort = () => {
174
405
  controller.abort(signal.reason);
175
406
  void Promise.resolve().then(async () => {
176
- if (turn.started)
407
+ if (turn.started) {
177
408
  sessions.cancel?.({ sessionId });
178
- else
409
+ app?.()?.stopSubAgents?.(sessionId);
410
+ } else
179
411
  await unqueue(sessions, sessionId, requestId);
180
412
  }).catch(() => {
181
413
  });
@@ -189,11 +421,12 @@ function sessionConversations(sessions, cwd = () => void 0, focus, app) {
189
421
  await sessions.prompt({
190
422
  requestId,
191
423
  sessionId,
192
- mode: "queue",
424
+ mode: options.insert === true && await running(sessions, sessionId, controller.signal) ? "steer" : "queue",
193
425
  content: [{ type: "text", text }, ...images.map((png) => ({ type: "image", mediaType: "image/png", data: png.toString("base64") }))],
194
426
  clientTimeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
195
427
  }, controller.signal);
196
- return await turnReply(iterator, requestId, text, turn);
428
+ const settled = app?.()?.settled;
429
+ return await workReply(iterator, requestId, text, turn, settled === void 0 ? void 0 : () => settled.call(app(), sessionId, controller.signal));
197
430
  } finally {
198
431
  signal.removeEventListener("abort", abort);
199
432
  controller.abort();
@@ -211,9 +444,22 @@ function sessionConversations(sessions, cwd = () => void 0, focus, app) {
211
444
  await focus?.(sessionId, options.focus);
212
445
  return sessionId;
213
446
  };
447
+ let own;
448
+ const desk = () => {
449
+ const found = app?.();
450
+ const make = () => createHandOffDesk({ home, converse, record: (item) => gateway().handOffs.put(item), threads: () => gateway().handOffs.all() });
451
+ if (found === void 0)
452
+ return own ??= make();
453
+ return found.handOffDesk ??= make();
454
+ };
214
455
  return {
215
456
  home,
216
- handOff: (input) => handOff({ home, converse, record: (item) => gateway().handOffs.put(item) }, input),
457
+ get desk() {
458
+ return desk();
459
+ },
460
+ handOff: (input) => desk().handOff(input),
461
+ handOffRefusal: (fromSession, to, fromAgent, toName) => desk().refusal(fromSession, to, fromAgent, toName),
462
+ askRefusal: (sessionId) => desk().askRefusal(sessionId),
217
463
  async create(title, options = {}) {
218
464
  const chosen = options.role === void 0 ? void 0 : app?.()?.roleModel(options.role);
219
465
  const roleModel = chosen === void 0 ? void 0 : { provider: chosen.provider, model: chosen.model, ...chosen.reasoningEffort === void 0 ? {} : { reasoningEffort: chosen.reasoningEffort } };
@@ -242,6 +488,10 @@ function sessionConversations(sessions, cwd = () => void 0, focus, app) {
242
488
  converse
243
489
  };
244
490
  }
491
+ async function running(sessions, sessionId, signal) {
492
+ const { items } = await sessions.list({}, signal);
493
+ return items.find((s) => s.sessionId === sessionId)?.running === true;
494
+ }
245
495
  async function unqueue(sessions, sessionId, requestId) {
246
496
  if (sessions.updateQueue === void 0)
247
497
  return;
@@ -335,33 +585,82 @@ var TurnReply = class {
335
585
  return this.#ended === void 0 ? { blocks: [...this.#blocks] } : { blocks: [...this.#blocks], ended: this.#ended };
336
586
  }
337
587
  };
338
- async function turnReply(iterator, requestId, text, turn) {
588
+ async function workReply(iterator, requestId, text, turn, settled) {
339
589
  let mine = false;
340
- const reply = new TurnReply();
590
+ let other = false;
591
+ let opened = false;
592
+ let cut = false;
593
+ const turns = [];
594
+ let reply = new TurnReply();
595
+ const take = (frame) => {
596
+ if (frame.type !== "event")
597
+ return false;
598
+ const event = convertRecord(frame);
599
+ if (event === null)
600
+ return false;
601
+ if (event.type === "user/message") {
602
+ const body = event.content.map((p) => p.type === "text" ? p.text : "").join("");
603
+ if (event.requestId === requestId || event.requestId === void 0 && !mine && body.trim() === text.trim())
604
+ mine = turn.started = true;
605
+ else if (mine && turns.length > 0)
606
+ other = event.requestId !== void 0;
607
+ return false;
608
+ }
609
+ if (!mine || other) {
610
+ if (event.type === "turn/end")
611
+ other = false;
612
+ return false;
613
+ }
614
+ if (event.type === "tool/call" && SUB_AGENT_TOOLS.has(event.name))
615
+ opened = true;
616
+ if (event.type === "turn/end" && event.reason.kind !== "completed" && event.reason.kind !== "stop")
617
+ cut = true;
618
+ if (!reply.add(event))
619
+ return false;
620
+ turns.push(reply.reply);
621
+ reply = new TurnReply();
622
+ return true;
623
+ };
341
624
  for (; ; ) {
342
625
  const next = await iterator.next();
343
626
  if (next.done === true)
627
+ return joined(turns, reply);
628
+ if (take(next.value))
344
629
  break;
345
- if (next.value.type !== "event")
346
- continue;
347
- const event = convertRecord(next.value);
348
- if (event === null)
630
+ }
631
+ if (!opened || settled === void 0 || cut)
632
+ return joined(turns, reply);
633
+ let done = false;
634
+ const finished = settled().then(() => {
635
+ done = true;
636
+ });
637
+ let pending;
638
+ for (; ; ) {
639
+ pending ??= iterator.next();
640
+ const raced = await Promise.race(done ? [pending, new Promise((resolve) => setTimeout(() => resolve(null), QUIET_MS))] : [pending, finished.then(() => void 0)]);
641
+ if (raced === null)
642
+ break;
643
+ if (raced === void 0)
349
644
  continue;
350
- if (event.type === "user/message") {
351
- const body = event.content.map((p) => p.type === "text" ? p.text : "").join("");
352
- if (event.requestId === requestId || event.requestId === void 0 && body.trim() === text.trim())
353
- mine = turn.started = true;
354
- } else if (mine && reply.add(event))
645
+ pending = void 0;
646
+ if (raced.done === true)
355
647
  break;
648
+ take(raced.value);
356
649
  }
357
- return reply.reply;
650
+ return joined(turns, reply);
651
+ }
652
+ function joined(turns, unfinished) {
653
+ const open = unfinished.reply;
654
+ const all = open.blocks.length > 0 ? [...turns, open] : turns;
655
+ const ended = all.at(-1)?.ended;
656
+ return { blocks: all.flatMap((r) => r.blocks), ...ended === void 0 ? {} : { ended } };
358
657
  }
359
658
 
360
659
  // packages/notify/lib/delegate.js
361
- var MAX_OUT = 3;
660
+ var MAX_OUT = 4;
362
661
  var DELEGATOR = "\u52A9\u624B";
363
662
  var TIMEOUT_MS = 2 * 60 * 6e4;
364
- var HANDED = (agent) => `\u5DF2\u4EA4\u7ED9\u300C${agent}\u300D\u3002\u5B83\u5728\u81EA\u5DF1\u7684\u5BF9\u8BDD\u91CC\u505A\uFF0C\u505A\u5B8C\u540E\u5B83\u7684\u56DE\u590D\u4F1A\u4F5C\u4E3A\u4E00\u6761\u65B0\u6D88\u606F\u53D1\u5230\u8FD9\u4E2A\u5BF9\u8BDD\u91CC\u3002\u73B0\u5728\u7528\u4E00\u53E5\u8BDD\u544A\u8BC9\u7528\u6237\u4F60\u4EA4\u7ED9\u4E86\u8C01\u3001\u505A\u4EC0\u4E48\uFF0C\u7136\u540E\u7ED3\u675F\u8FD9\u4E00\u8F6E\uFF1A\u4E0D\u8981\u7B49\u5B83\uFF0C\u4E5F\u4E0D\u8981\u66FF\u5B83\u56DE\u7B54\u3002`;
663
+ var HANDED = (agent) => `\u5DF2\u4EA4\u7ED9\u300C${agent}\u300D\u3002\u5B83\u5728\u81EA\u5DF1\u7684\u5BF9\u8BDD\u91CC\u505A\uFF0C\u5B83\u505A\u7684\u8FC7\u7A0B\u4E2D\u8BF4\u7684\u8BDD\u548C\u6700\u540E\u7684\u7ED3\u8BBA\u90FD\u4F1A\u4F5C\u4E3A\u65B0\u6D88\u606F\u53D1\u5230\u8FD9\u4E2A\u5BF9\u8BDD\u91CC\u3002\u73B0\u5728\u7528\u4E00\u53E5\u8BDD\u8BF4\u4F60\u4EA4\u7ED9\u4E86\u8C01\u3001\u505A\u4EC0\u4E48\uFF0C\u7136\u540E\u7ED3\u675F\u8FD9\u4E00\u8F6E\uFF1A\u4E0D\u8981\u7B49\u5B83\uFF0C\u4E5F\u4E0D\u8981\u66FF\u5B83\u56DE\u7B54\u3002`;
365
664
  var Args2 = external_exports.object({
366
665
  agent: external_exports.string().trim().min(1).max(40),
367
666
  task: external_exports.string().trim().min(1).max(8e3),
@@ -379,15 +678,15 @@ function delegateTool(deps) {
379
678
  return {
380
679
  name: "agent_delegate",
381
680
  description: [
382
- "\u628A\u4E00\u4EF6\u4E8B\u4EA4\u7ED9\u53E6\u4E00\u4E2A agent \u53BB\u505A\u3002\u53EF\u4EE5\u4EA4\u7ED9\u8C01\u3001\u5404\u81EA\u505A\u4EC0\u4E48\uFF0C\u89C1\u7CFB\u7EDF\u63D0\u793A\u91CC\u7684\u300C\u53EF\u4EE5\u4EA4\u7ED9\u7684 agent\u300D\u3002",
383
- "\u4EA4\u51FA\u53BB\u5C31\u8FD4\u56DE\uFF0C\u4E0D\u7B49\u5B83\u505A\u5B8C\uFF1A\u5B83\u5728\u81EA\u5DF1\u7684\u5BF9\u8BDD\u91CC\u505A\uFF0C\u505A\u5B8C\u540E\u5B83\u7684\u56DE\u590D\u4F1A\u4F5C\u4E3A\u4E00\u6761\u65B0\u6D88\u606F\u53D1\u5230\u8FD9\u4E2A\u5BF9\u8BDD\u91CC\u3002",
384
- "task \u8981\u5199\u5B8C\u6574\uFF1A\u5BF9\u65B9\u770B\u4E0D\u5230\u4F60\u548C\u7528\u6237\u7684\u5BF9\u8BDD\uFF0C\u53EA\u770B\u5230 task\uFF08\u8981\u505A\u4EC0\u4E48\u3001\u5728\u54EA\u4E2A\u6E38\u620F\u6216\u811A\u672C\u3001\u505A\u5230\u54EA\u4E00\u6B65\u4E3A\u6B62\uFF09\u3002expecting \u5199\u4F60\u8981\u5B83\u4EA4\u56DE\u4EC0\u4E48\uFF08\u53EF\u9009\uFF09\u3002",
681
+ "\u628A\u4E00\u4EF6\u4E8B\u4EA4\u7ED9\u53E6\u4E00\u4E2A agent \u53BB\u505A\u3002\u53EF\u4EE5\u4EA4\u7ED9\u8C01\u3001\u5404\u81EA\u505A\u4EC0\u4E48\uFF0C\u89C1\u7CFB\u7EDF\u63D0\u793A\u91CC\u7684\u300C\u56E2\u961F\u300D\u3002",
682
+ "\u4EA4\u51FA\u53BB\u5C31\u8FD4\u56DE\uFF0C\u4E0D\u7B49\u5B83\u505A\u5B8C\uFF1A\u5B83\u5728\u81EA\u5DF1\u7684\u5BF9\u8BDD\u91CC\u505A\uFF0C\u505A\u7684\u8FC7\u7A0B\u4E2D\u8BF4\u7684\u8BDD\u548C\u6700\u540E\u7684\u7ED3\u8BBA\u90FD\u4F1A\u4F5C\u4E3A\u65B0\u6D88\u606F\u53D1\u5230\u8FD9\u4E2A\u5BF9\u8BDD\u91CC\u3002",
683
+ "task \u8981\u5199\u5B8C\u6574\uFF1A\u5BF9\u65B9\u770B\u4E0D\u5230\u4F60\u7684\u5BF9\u8BDD\uFF0C\u53EA\u770B\u5230 task\uFF08\u8981\u505A\u4EC0\u4E48\u3001\u5728\u54EA\u4E2A\u6E38\u620F\u6216\u811A\u672C\u3001\u505A\u5230\u54EA\u4E00\u6B65\u4E3A\u6B62\uFF09\u3002expecting \u5199\u4F60\u8981\u5B83\u4EA4\u56DE\u4EC0\u4E48\uFF08\u53EF\u9009\uFF09\u3002",
385
684
  `\u51E0\u4EF6\u4E92\u4E0D\u76F8\u5173\u7684\u4E8B\u53EF\u4EE5\u540C\u65F6\u4EA4\u51FA\u53BB\uFF0C\u4E00\u4E2A\u5BF9\u8BDD\u540C\u65F6\u6700\u591A ${MAX_OUT} \u4EF6\u3002\u5B83\u8981\u7528\u6237\u5BA1\u6279\u6216\u56DE\u7B54\u95EE\u9898\u65F6\u4F1A\u505C\u4E0B\u7B49\u7528\u6237\u3002`
386
685
  ].join(""),
387
686
  parameters: {
388
687
  type: "object",
389
688
  properties: {
390
- agent: { type: "string", description: "agent \u7684\u540D\u5B57\uFF0C\u4F8B\u5982\u300C\u6E38\u620F\u300D\u300C\u811A\u672C\u300D\u3002" },
689
+ agent: { type: "string", description: "agent \u7684\u540D\u5B57\u6216 id\uFF0C\u4F8B\u5982\u300C\u6E38\u620F\u300D\u300C\u811A\u672C\u300D\u3002" },
391
690
  task: { type: "string", description: "\u8981\u5B83\u505A\u7684\u4E8B\uFF0C\u5199\u5B8C\u6574\u3002" },
392
691
  expecting: { type: "string", description: "\u8981\u5B83\u4EA4\u56DE\u4EC0\u4E48\uFF0C\u4F8B\u5982\u300C\u54EA\u4E9B\u6D3B\u52A8\u4ECA\u5929\u7ED3\u675F\uFF0C\u6CA1\u6709\u5C31\u8BF4\u6CA1\u6709\u300D\uFF08\u53EF\u9009\uFF09\u3002" }
393
692
  },
@@ -404,7 +703,8 @@ function delegateTool(deps) {
404
703
  const parent = exec.agent?.session.id;
405
704
  if (parent === void 0)
406
705
  throw new Error("\u53EA\u80FD\u5728\u4F1A\u8BDD\u91CC\u4EA4\u51FA\u53BB");
407
- const agents = deps.agents();
706
+ const caller = deps.agentOf?.(parent);
707
+ const agents = deps.agents().filter((a) => a.id !== caller?.id);
408
708
  const agent = agents.find((a) => a.name === wanted || a.id === wanted);
409
709
  if (agent === void 0)
410
710
  throw new Error(`\u6CA1\u6709\u53EB\u300C${wanted}\u300D\u7684 agent\u3002\u53EF\u4EE5\u4EA4\u7ED9\uFF1A${agents.map((a) => a.name).join("\u3001") || "\uFF08\u6CA1\u6709\uFF09"}`);
@@ -413,18 +713,22 @@ function delegateTool(deps) {
413
713
  throw new Error("agent \u670D\u52A1\u8FD8\u6CA1\u6709\u51C6\u5907\u597D");
414
714
  if ((out.get(parent) ?? 0) >= MAX_OUT)
415
715
  throw new Error(`\u8FD9\u4E2A\u5BF9\u8BDD\u5DF2\u7ECF\u6709 ${MAX_OUT} \u4EF6\u4E8B\u5728\u522B\u7684 agent \u90A3\u91CC\uFF0C\u7B49\u5176\u4E2D\u4E00\u4EF6\u56DE\u6765\u518D\u4EA4`);
716
+ const refusal = conversations.handOffRefusal?.(parent, agent.id, caller?.id ?? "", agent.name);
717
+ if (refusal !== void 0)
718
+ throw new Error(refusal);
416
719
  out.set(parent, (out.get(parent) ?? 0) + 1);
417
720
  const line = task.split("\n").find((l) => l.trim() !== "").trim();
418
721
  const callId = String(exec.callId);
419
722
  const relayed = deps.relayed?.(parent);
723
+ const by = caller?.name ?? DELEGATOR;
420
724
  void conversations.handOff({
421
725
  to: agent,
422
- by: DELEGATOR,
726
+ by,
423
727
  title: line.length > 30 ? `${line.slice(0, 29)}\u2026` : line,
424
728
  task,
425
729
  ...expecting ? { expecting } : {},
426
730
  signal: AbortSignal.timeout(TIMEOUT_MS),
427
- from: { sessionId: parent, callId, asker: DELEGATOR },
731
+ from: { sessionId: parent, callId, asker: by, ...caller === void 0 ? {} : { agent: caller.id } },
428
732
  relay: true,
429
733
  ...relayed === void 0 ? {} : { relayed }
430
734
  }).catch(() => {
@@ -437,6 +741,87 @@ function delegateTool(deps) {
437
741
  }
438
742
  };
439
743
  }
744
+ var MessageArgs = external_exports.object({
745
+ thread: external_exports.string().trim().min(1),
746
+ kind: external_exports.enum(["question", "answer", "update", "done"]),
747
+ text: external_exports.string().trim().min(1).max(8e3)
748
+ });
749
+ function messageTool(deps) {
750
+ return {
751
+ name: "agent_message",
752
+ description: [
753
+ "\u5728\u4E00\u6761\u7EBF\u7D22\u91CC\u548C\u5BF9\u65B9\u8BF4\u8BDD\uFF0C\u8BF4\u51E0\u6B21\u90FD\u884C\uFF0C\u6BCF\u53E5\u90FD\u9A6C\u4E0A\u9001\u5230\u5BF9\u65B9\u7684\u5BF9\u8BDD\u91CC\u3002\u7EBF\u7D22\u7684 id \u5199\u5728\u4EA4\u7ED9\u4F60\u7684\u4E8B\u5F00\u5934\uFF0C\u6216\u8005\u5BF9\u65B9\u53D1\u6765\u7684\u6D88\u606F\u5F00\u5934\uFF1Bagent_threads \u4E5F\u67E5\u5F97\u5230\u3002",
754
+ "kind\uFF1Aquestion \u95EE\u5BF9\u65B9\uFF08\u5BF9\u65B9\u56DE\u7B54\u540E\u4F1A\u4F5C\u4E3A\u65B0\u6D88\u606F\u53D1\u5230\u8FD9\u91CC\uFF0C\u6240\u4EE5\u95EE\u5B8C\u5C31\u7ED3\u675F\u8FD9\u4E00\u8F6E\uFF0C\u4E0D\u8981\u731C\u7B54\u6848\uFF09\uFF1Banswer \u56DE\u7B54\u5BF9\u65B9\uFF1Bupdate \u544A\u8BC9\u5BF9\u65B9\u4E00\u4EF6\u4E8B\uFF0C\u4E0D\u7528\u5B83\u56DE\uFF1Bdone \u4EA4\u56DE\u7ED3\u8BBA\u3002",
755
+ "\u4E0D\u7528 done \u4E5F\u53EF\u4EE5\uFF1A\u4F60\u8FD9\u4E00\u8F6E\u7684\u6700\u540E\u4E00\u6BB5\u56DE\u590D\u5C31\u662F\u7ED3\u8BBA\u3002\u4EA4\u56DE\u7ED3\u8BBA\u4EE5\u540E\u7EBF\u7D22\u8FD8\u5728\uFF1A\u4E4B\u540E\u8FD8\u6709\u8BDD\uFF08\u6BD4\u5982\u5B9A\u65F6\u63D0\u9192\u5230\u4E86\uFF09\u7167\u6837\u53D1\u5230\u8FD9\u91CC\uFF0C\u666E\u901A\u56DE\u590D\u5BF9\u65B9\u662F\u770B\u4E0D\u5230\u7684\u3002\u6BCF\u53E5\u8BDD\u90FD\u8981\u5BF9\u65B9\u82B1\u4E00\u8F6E\u53BB\u770B\uFF0C\u6CA1\u6709\u8981\u7D27\u7684\u5C31\u4E0D\u8BF4\u3002"
756
+ ].join(""),
757
+ parameters: {
758
+ type: "object",
759
+ properties: {
760
+ thread: { type: "string", description: "\u7EBF\u7D22\u7684 id\u3002" },
761
+ kind: { type: "string", enum: ["question", "answer", "update", "done"], description: "question \u95EE\u5BF9\u65B9\uFF1Banswer \u56DE\u7B54\u5BF9\u65B9\uFF1Bupdate \u544A\u8BC9\u5BF9\u65B9\uFF1Bdone \u4EA4\u56DE\u7ED3\u8BBA\u3002" },
762
+ text: { type: "string", description: "\u8981\u8BF4\u7684\u8BDD\uFF0C\u5199\u5B8C\u6574\uFF1A\u5BF9\u65B9\u770B\u4E0D\u5230\u4F60\u7684\u5BF9\u8BDD\u3002" }
763
+ },
764
+ required: ["thread", "kind", "text"],
765
+ additionalProperties: false
766
+ },
767
+ output: { schema: {}, render: (_args, value) => [{ type: "text", text: String(value) }] },
768
+ isConcurrencySafe: () => false,
769
+ execute: async (args, exec) => {
770
+ const parsed = MessageArgs.safeParse(args ?? {});
771
+ if (!parsed.success)
772
+ throw new Error(`\u53C2\u6570\u65E0\u6548\uFF1A${parsed.error.issues.map((i) => `${i.path.join(".") || "(root)"} ${i.message}`).join("; ")}`);
773
+ const fromSession = exec.agent?.session.id;
774
+ if (fromSession === void 0)
775
+ throw new Error("\u53EA\u80FD\u5728\u4F1A\u8BDD\u91CC\u8BF4");
776
+ const desk = deps.desk();
777
+ if (desk === void 0)
778
+ throw new Error("agent \u670D\u52A1\u8FD8\u6CA1\u6709\u51C6\u5907\u597D");
779
+ return await desk.message({ ...parsed.data, fromSession, from: deps.agentOf(fromSession)?.name ?? DELEGATOR });
780
+ },
781
+ presentCall: (args) => {
782
+ const a = args ?? {};
783
+ return { card: "generic", title: `${String(a.kind ?? "")}\uFF1A${String(a.text ?? "").split("\n")[0]}`, kind: "other" };
784
+ }
785
+ };
786
+ }
787
+ var ThreadsArgs = external_exports.object({ id: external_exports.string().trim().min(1).nullish(), with: external_exports.string().trim().min(1).nullish() });
788
+ function threadsTool(deps) {
789
+ const line = (h) => `${h.id} [${h.state}] ${h.by} \u2192 ${deps.name(h.to)}\uFF1A${h.title}${h.reply === void 0 ? "" : `\uFF5C${h.reply.replace(/\s+/g, " ").slice(0, 120)}`}`;
790
+ return {
791
+ name: "agent_threads",
792
+ description: "\u770B agent \u4E4B\u95F4\u7684\u7EBF\u7D22\uFF0C\u5305\u62EC\u4EE5\u524D\u7684\uFF1A\u5BF9\u8BDD\u88AB\u538B\u7F29\u6216\u6362\u8FC7\u4E5F\u8FD8\u5728\u3002\u4E0D\u5E26 id \u5217\u51FA\u6700\u8FD1\u7684\uFF08\u8C01\u4EA4\u7ED9\u8C01\u3001\u4EC0\u4E48\u4E8B\u3001\u505A\u5230\u54EA\u4E86\u3001\u7ED3\u8BBA\u7684\u5F00\u5934\uFF09\uFF0Cwith \u53EA\u770B\u548C\u67D0\u4E2A agent \u6709\u5173\u7684\uFF1B\u5E26 id \u770B\u4E00\u6761\u7684\u5B8C\u6574\u5F80\u6765\u3002\u63A5\u7740\u4EE5\u524D\u7684\u4E8B\u627E\u540C\u4E8B\u4E4B\u524D\u5148\u770B\u4E00\u773C\uFF1B\u4E3B agent \u4E4B\u95F4\u76F4\u63A5\u8BF4\u7684\u8BDD\u4E0D\u4F1A\u53EB\u9192\u4F60\uFF0C\u8981\u77E5\u9053\u4E5F\u7528\u8FD9\u4E2A\u3002",
793
+ parameters: {
794
+ type: "object",
795
+ properties: {
796
+ id: { type: "string", description: "\u7EBF\u7D22\u7684 id\uFF08\u53EF\u9009\uFF09\u3002" },
797
+ with: { type: "string", description: "\u53EA\u770B\u548C\u8FD9\u4E2A agent \u6709\u5173\u7684\uFF1A\u540D\u5B57\u6216 id\uFF08\u53EF\u9009\uFF09\u3002" }
798
+ },
799
+ additionalProperties: false
800
+ },
801
+ output: { schema: {}, render: (_args, value) => [{ type: "text", text: String(value) }] },
802
+ isConcurrencySafe: () => true,
803
+ execute: async (args) => {
804
+ const parsed = ThreadsArgs.safeParse(args ?? {});
805
+ if (!parsed.success)
806
+ throw new Error("\u53C2\u6570\u65E0\u6548");
807
+ const all = deps.list();
808
+ if (parsed.data.id == null) {
809
+ const who = parsed.data.with;
810
+ const about = who == null ? all : all.filter((h) => h.by === who || h.to === who || h.by === deps.name(who) || deps.name(h.to) === who);
811
+ return about.slice(-20).reverse().map(line).join("\n") || (who == null ? "\uFF08\u8FD8\u6CA1\u6709\u7EBF\u7D22\uFF09" : `\uFF08\u548C\u300C${who}\u300D\u8FD8\u6CA1\u6709\u7EBF\u7D22\uFF09`);
812
+ }
813
+ const found = all.find((h) => h.id === parsed.data.id);
814
+ if (found === void 0)
815
+ throw new Error(`\u6CA1\u6709\u7EBF\u7D22 ${parsed.data.id}`);
816
+ return [
817
+ line(found),
818
+ `\u4EA4\u7684\u4E8B\uFF1A${found.task}`,
819
+ ...(found.messages ?? []).map((m) => `${m.from}\uFF08${m.kind}\uFF09\uFF1A${m.text}`),
820
+ ...found.reply === void 0 ? [] : [`\u7ED3\u8BBA\uFF1A${found.reply}`]
821
+ ].join("\n");
822
+ }
823
+ };
824
+ }
440
825
 
441
826
  // packages/notify/lib/notify.js
442
827
  import { randomUUID as randomUUID3 } from "node:crypto";
@@ -655,7 +1040,7 @@ ${prompt}`;
655
1040
  this.#origins.set(prompt.trim(), message_.actor);
656
1041
  while (this.#origins.size > KEEP_ORIGINS)
657
1042
  this.#origins.delete(this.#origins.keys().next().value);
658
- return conversations.converse !== void 0 ? await conversations.converse(sessionId, prompt, signal) : textReply(await conversations.ask(sessionId, prompt, signal));
1043
+ return conversations.converse !== void 0 ? await conversations.converse(sessionId, prompt, signal, [], { insert: true }) : textReply(await conversations.ask(sessionId, prompt, signal));
659
1044
  }
660
1045
  /** The agent waits for this person's answer (a question, an approval): their next message is it, not one to queue. */
661
1046
  waiting(actor) {
@@ -965,7 +1350,9 @@ var HomeMirror = class {
965
1350
  }
966
1351
  /** A hand-off (the gateway's); 助手's (from its conversation) become lines. */
967
1352
  handOff(item) {
968
- if (this.#home === void 0 || item.fromSession !== this.#home || this.#seen.get(item.id) === item.state)
1353
+ if (item.state === "waiting" || this.#home === void 0 || item.fromSession !== this.#home || this.#seen.get(item.id) === item.state)
1354
+ return;
1355
+ if (item.state === "running" && this.#seen.has(item.id))
969
1356
  return;
970
1357
  this.#seen.set(item.id, item.state);
971
1358
  while (this.#seen.size > KEEP_SEEN)
@@ -977,6 +1364,11 @@ var HomeMirror = class {
977
1364
  #message(text) {
978
1365
  if (parseRelay(text) !== void 0)
979
1366
  return void 0;
1367
+ const said = parseThreadNote(text);
1368
+ if (said !== void 0) {
1369
+ this.#deps.post({ text: `${said.from} \u2192 ${MAIN_NAME}\uFF1A${{ question: "\u95EE", answer: "\u7B54", update: "\u8BF4" }[said.kind]}\uFF1A${said.text}` });
1370
+ return void 0;
1371
+ }
980
1372
  const task = parseTask(text);
981
1373
  if (task !== void 0) {
982
1374
  this.#deps.post({ text: task.by === BY_YOU ? `\u4F60\uFF08\u8F6F\u4EF6\uFF09\u70B9\u4E86\uFF1A${task.title}` : `${task.by} \u2192 ${MAIN_NAME}\uFF1A${task.title}` });
@@ -1079,7 +1471,11 @@ var AdgNotify = class extends Service {
1079
1471
  asked: (request) => notify.askApproval(request),
1080
1472
  resolved: (id, outcome) => notify.approvalResolved(id, outcome)
1081
1473
  });
1082
- scoped.adgGateway.agentPart("adg-notify agent_delegate", (agent) => agent.tools.register(delegateTool({ agents: () => scoped.adgGateway.roles.list(), conversations })));
1474
+ const agentOf = (sessionId) => scoped.adgGateway.agentOfSession(sessionId);
1475
+ const agentName = (id) => id === MAIN ? "\u52A9\u624B" : scoped.adgGateway.roles.list().find((r) => r.id === id)?.name ?? id;
1476
+ scoped.adgGateway.agentPart("adg-notify agent_delegate", (agent) => agent.tools.register(delegateTool({ agents: () => [{ id: MAIN, name: "\u52A9\u624B" }, ...scoped.adgGateway.roles.list()], conversations, agentOf })));
1477
+ scoped.adgGateway.agentPart("adg-notify agent_message", (agent) => agent.tools.register(messageTool({ desk: () => conversations()?.desk, agentOf })));
1478
+ scoped.adgGateway.agentPart("adg-notify agent_threads", (agent) => agent.tools.register(threadsTool({ list: () => scoped.adgGateway.handOffs.all(), name: agentName })));
1083
1479
  scoped.adgGateway.agentPart("adg-notify notice_act", (agent) => agent.tools.register(noticeActTool(notify, gateway.roles.list().find((r) => r.id === MAIN)?.name ?? "\u52A9\u624B")));
1084
1480
  const timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
1085
1481
  scoped.adgGateway.agentPart("adg-notify notices note", (agent) => agent.on("agent/pre-step", (async (payload, next) => {