@tangle-network/agent-app 0.63.67 → 0.63.70

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 {
2
2
  ReasoningRow,
3
3
  chatToolCallPart,
4
4
  resolveToolPresentation
5
- } from "../chunk-73AVOSH6.js";
5
+ } from "../chunk-YG3DSMPS.js";
6
6
  import "../chunk-UHSUY6H6.js";
7
7
  import "../chunk-3HUFJ5LO.js";
8
8
  import "../chunk-7PULOECS.js";
@@ -1,16 +1,19 @@
1
1
  /**
2
2
  * A turn addressed to several agents runs them at the same time, each as its
3
- * own producer with the same thread history, and presents them one after
4
- * another in the order the mentions were typed: the route sees one stream,
3
+ * own producer with the same thread history: the route sees one stream,
5
4
  * persists one assistant row, and the row's parts carry a `speaker` part
6
- * before each responder's parts so a transcript can draw each answer under
7
- * its author. The live stream carries the same boundary as a `speaker`
8
- * event, so a viewer opens a new block as each agent's answer arrives.
5
+ * before each responder's parts, in the order the mentions were typed, so a
6
+ * transcript draws each answer under its author.
9
7
  *
10
- * Parallel on purpose: the agents are addressed, not chained, so each answers
11
- * independently and none waits on another. Ordered presentation on purpose:
12
- * one live block at a time is what a reader can follow, and the stored order
13
- * is the order typed, so the transcript rereads the same way every time.
8
+ * Live, every responder is announced up front with a `speaker` event, in
9
+ * mention order, and then every event carries `speaker` (the responder's
10
+ * agent id) as it arrives, so a viewer draws one live block per responder and
11
+ * sees them answer at the same time. The stored order stays the order typed,
12
+ * so the transcript rereads the same way every time.
13
+ *
14
+ * Parallel and independent on purpose: the agents are addressed, not chained.
15
+ * A responder that fails leaves a notice under its own name and the others
16
+ * still answer; the turn fails only when every responder did.
14
17
  */
15
18
  import type { ChatTurnIdentity } from '@tangle-network/agent-runtime/durable';
16
19
  import type { TurnTrace } from '../turn-trace/recorder.js';
@@ -82,7 +82,7 @@ import {
82
82
  turnCarriesFiles,
83
83
  turnPromptText,
84
84
  withProducerStream
85
- } from "../chunk-3F5LGESB.js";
85
+ } from "../chunk-RG2CF4CS.js";
86
86
  import "../chunk-SBHFMDR7.js";
87
87
  import "../chunk-RKNNMC6T.js";
88
88
  import "../chunk-XBBXPZJI.js";
@@ -2221,68 +2221,143 @@ var SPEAKER_EVENT = "speaker";
2221
2221
  function speakerPart(agent) {
2222
2222
  return { type: "speaker", id: agent.id, name: agent.name, handle: agent.handle };
2223
2223
  }
2224
+ function addressedTurnText(content, agent, agents) {
2225
+ let text = content;
2226
+ for (const target of agents) {
2227
+ text = text.replace(new RegExp(`(^|\\s)@${escapeRegExp(target.handle)}(?=\\s|$|[.,;:!?])`, "g"), "$1");
2228
+ }
2229
+ text = text.replace(/[ \t]{2,}/g, " ").trim();
2230
+ const others = agents.filter((target) => target.id !== agent.id).map((target) => `${target.name} (@${target.handle})`);
2231
+ const line = others.length === 0 ? `You answer this message as ${agent.name} (@${agent.handle}).` : `You answer this message as ${agent.name} (@${agent.handle}). ${others.join(", ")} ${others.length === 1 ? "answers" : "answer"} it separately at the same time; do your part and leave theirs to them.`;
2232
+ return text ? `${line}
2233
+
2234
+ ${text}` : line;
2235
+ }
2236
+ function escapeRegExp(value) {
2237
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
2238
+ }
2239
+ function responderArgs(args, agent, index) {
2240
+ const agents = args.agents ?? [];
2241
+ const address = (text) => addressedTurnText(text, agent, agents);
2242
+ const prompt = typeof args.prompt === "string" ? address(args.prompt) : (() => {
2243
+ let done = false;
2244
+ return args.prompt.map((part) => {
2245
+ if (done || part.type !== "text" || typeof part.text !== "string") return part;
2246
+ done = true;
2247
+ return { ...part, text: address(part.text) };
2248
+ });
2249
+ })();
2250
+ return {
2251
+ ...args,
2252
+ agent,
2253
+ executionId: `${args.executionId}-${index + 1}`,
2254
+ prompt,
2255
+ body: { ...args.body, ...typeof args.body.content === "string" ? { content: address(args.body.content) } : {} }
2256
+ };
2257
+ }
2224
2258
  async function composeAgentProducers(args, produce) {
2225
2259
  const agents = args.agents ?? [];
2226
2260
  if (agents.length === 0) throw new Error("composeAgentProducers needs at least one responder");
2227
2261
  const producers = agents.map(() => void 0);
2228
2262
  const cleanups = [];
2229
- const slots = agents.map(() => ({ queue: [], done: false, error: void 0, waiters: [] }));
2230
- const notify = (slot) => {
2231
- for (const wake of slot.waiters.splice(0)) wake();
2263
+ const slots = agents.map(() => ({ queue: [], done: false, error: void 0, failure: void 0 }));
2264
+ const failedNotice = (agent, message) => ({ type: "notice", id: `speaker-failed:${agent.id}`, noticeKind: "error", text: `${agent.name} could not answer: ${message}`, speaker: agent.id });
2265
+ let wake;
2266
+ const notify = () => {
2267
+ const w = wake;
2268
+ wake = void 0;
2269
+ w?.();
2232
2270
  };
2233
2271
  agents.forEach((agent, index) => {
2234
2272
  const slot = slots[index];
2235
2273
  void (async () => {
2236
2274
  try {
2237
- const producer = await produce({ ...args, agent, executionId: `${args.executionId}-${index + 1}` });
2275
+ const producer = await produce(responderArgs(args, agent, index));
2238
2276
  producers[index] = producer;
2239
2277
  if (producer.cleanup) cleanups.push(producer.cleanup);
2240
2278
  for await (const event of producer.stream) {
2241
- slot.queue.push(event);
2242
- notify(slot);
2279
+ if (event.type === "error" || event.type === "session.run.failed") {
2280
+ slot.failure = { ...event, speaker: agent.id };
2281
+ const message = typeof event.data?.message === "string" ? event.data.message : "the run failed";
2282
+ slot.queue.push(failedNotice(agent, message));
2283
+ } else {
2284
+ slot.queue.push({ ...event, speaker: agent.id });
2285
+ }
2286
+ notify();
2243
2287
  }
2244
2288
  } catch (error) {
2245
2289
  slot.error = error;
2246
2290
  } finally {
2247
2291
  slot.done = true;
2248
- notify(slot);
2292
+ notify();
2249
2293
  }
2250
2294
  })();
2251
2295
  });
2252
2296
  const stream = (async function* () {
2253
- for (const [index, agent] of agents.entries()) {
2254
- if (args.admissionSignal?.aborted) return;
2297
+ for (const agent of agents) {
2255
2298
  yield { type: SPEAKER_EVENT, id: agent.id, name: agent.name, handle: agent.handle };
2256
- const slot = slots[index];
2257
- for (; ; ) {
2258
- if (slot.queue.length > 0) {
2299
+ }
2300
+ const reported = /* @__PURE__ */ new Set();
2301
+ for (; ; ) {
2302
+ if (args.admissionSignal?.aborted) return;
2303
+ let yielded = false;
2304
+ for (const [index, slot] of slots.entries()) {
2305
+ while (slot.queue.length > 0) {
2259
2306
  yield slot.queue.shift();
2260
- continue;
2307
+ yielded = true;
2261
2308
  }
2262
- if (slot.done) {
2263
- if (slot.error !== void 0) throw slot.error;
2264
- break;
2309
+ if (slot.done && slot.error !== void 0 && !reported.has(index)) {
2310
+ reported.add(index);
2311
+ const agent = agents[index];
2312
+ yield failedNotice(agent, slot.error instanceof Error ? slot.error.message : String(slot.error));
2313
+ yielded = true;
2265
2314
  }
2266
- await new Promise((wake) => {
2267
- slot.waiters.push(wake);
2268
- if (slot.queue.length > 0 || slot.done) notify(slot);
2269
- });
2270
2315
  }
2316
+ if (slots.every((slot) => slot.done && slot.queue.length === 0)) {
2317
+ const failed = slots.every((slot) => slot.error !== void 0 || slot.failure !== void 0);
2318
+ if (failed) {
2319
+ const thrown = slots.find((slot) => slot.error !== void 0)?.error;
2320
+ if (thrown !== void 0) throw thrown;
2321
+ const failure = slots.map((slot) => slot.failure).find((event) => event !== void 0);
2322
+ if (failure) yield failure;
2323
+ }
2324
+ return;
2325
+ }
2326
+ if (!yielded) await new Promise((resolve) => {
2327
+ wake = resolve;
2328
+ if (slots.some((slot) => slot.queue.length > 0 || slot.done)) {
2329
+ const pending = slots.some((slot) => slot.queue.length > 0 || slot.done && slot.error !== void 0 && !reported.has(slots.indexOf(slot)));
2330
+ if (pending || slots.every((slot) => slot.done)) {
2331
+ wake = void 0;
2332
+ resolve();
2333
+ }
2334
+ }
2335
+ });
2271
2336
  }
2272
2337
  })();
2273
2338
  const entries = () => agents.flatMap((agent, index) => producers[index] ? [{ agent, producer: producers[index] }] : []);
2274
- const partsOf = (entry, read) => {
2275
- const own = entry.producer[read]?.();
2276
- const text = entry.producer.finalText();
2339
+ const failureOf = (index) => {
2340
+ const slot = slots[index];
2341
+ if (slot.error !== void 0) return slot.error instanceof Error ? slot.error.message : String(slot.error);
2342
+ const data = slot.failure?.data;
2343
+ return slot.failure ? typeof data?.message === "string" ? data.message : "the run failed" : void 0;
2344
+ };
2345
+ const partsOf = (index, read) => {
2346
+ const agent = agents[index];
2347
+ const producer = producers[index];
2348
+ const own = producer?.[read]?.();
2349
+ const text = producer?.finalText() ?? "";
2277
2350
  const parts = own ?? (text ? [{ type: "text", text }] : []);
2278
- return [speakerPart(entry.agent), ...parts];
2351
+ const failure = failureOf(index);
2352
+ const notice = failure === void 0 ? [] : [{ type: "notice", noticeKind: "error", id: `speaker-failed:${agent.id}`, text: `${agent.name} could not answer: ${failure}` }];
2353
+ return [speakerPart(agent), ...parts, ...notice];
2279
2354
  };
2280
2355
  const last = () => entries().at(-1)?.producer;
2281
2356
  return {
2282
2357
  stream,
2283
2358
  finalText: () => entries().map(({ producer }) => producer.finalText()).filter((text) => text.trim() !== "").join("\n\n"),
2284
- assistantParts: () => entries().flatMap((entry) => partsOf(entry, "assistantParts")),
2285
- draftParts: () => entries().flatMap((entry) => partsOf(entry, "draftParts")),
2359
+ assistantParts: () => agents.flatMap((_, index) => partsOf(index, "assistantParts")),
2360
+ draftParts: () => agents.flatMap((_, index) => partsOf(index, "draftParts")),
2286
2361
  usage: () => entries().reduce((sum, { producer }) => {
2287
2362
  const usage = producer.usage?.() ?? {};
2288
2363
  for (const [key, value] of Object.entries(usage)) {
@@ -4080,4 +4155,4 @@ export {
4080
4155
  createChatTurnRoutes,
4081
4156
  streamChatRouteAsSandboxEvents
4082
4157
  };
4083
- //# sourceMappingURL=chunk-3F5LGESB.js.map
4158
+ //# sourceMappingURL=chunk-RG2CF4CS.js.map