@tangle-network/agent-app 0.60.141 → 0.60.143

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.
@@ -570,6 +570,36 @@ function createProgressQueue() {
570
570
  };
571
571
  }
572
572
 
573
+ // src/chat-routes/reasoning-clock.ts
574
+ function createReasoningClock(now = Date.now) {
575
+ const runs = /* @__PURE__ */ new Map();
576
+ let open = null;
577
+ const settle = (at) => {
578
+ if (!open) return;
579
+ const time = runs.get(open.run);
580
+ if (time && time.end === void 0) time.end = Math.max(time.start ?? 0, at ?? now());
581
+ open = null;
582
+ };
583
+ return {
584
+ reasoning(run) {
585
+ if (open && open.run === run) return;
586
+ settle();
587
+ const time = runs.get(run);
588
+ if (time) delete time.end;
589
+ else runs.set(run, { start: now() });
590
+ open = { run };
591
+ },
592
+ settle,
593
+ time(run, own) {
594
+ const time = runs.get(run);
595
+ if (!time) return own;
596
+ const start = own?.start ?? time.start;
597
+ const end = own?.end ?? time.end;
598
+ return { ...start !== void 0 ? { start } : {}, ...end !== void 0 ? { end } : {} };
599
+ }
600
+ };
601
+ }
602
+
573
603
  // src/chat-routes/sandbox-turn-usage.ts
574
604
  function addStepFinishUsage(part, usage) {
575
605
  const tokens = asRecord(part.tokens);
@@ -780,6 +810,7 @@ function createSandboxChatProducer(options) {
780
810
  const partOrder = [];
781
811
  const partMap = /* @__PURE__ */ new Map();
782
812
  const tracker = { seen: /* @__PURE__ */ new Map() };
813
+ const reasoningClock = createReasoningClock();
783
814
  const usage = {};
784
815
  const announcedToolArgs = /* @__PURE__ */ new Map();
785
816
  const settledTools = /* @__PURE__ */ new Set();
@@ -864,12 +895,15 @@ function createSandboxChatProducer(options) {
864
895
  const delta = textDelta(tracker, key, part, rawDelta);
865
896
  recordPersistedPart(part, delta || void 0);
866
897
  if (delta) {
898
+ if (partType === "reasoning") reasoningClock.reasoning(key);
899
+ else reasoningClock.settle();
867
900
  if (partType === "text") fullText += delta;
868
901
  yield { type: partType, text: delta };
869
902
  }
870
903
  continue;
871
904
  }
872
905
  if (partType === "tool") {
906
+ reasoningClock.settle();
873
907
  recordPersistedPart(part, void 0);
874
908
  const persisted = partMap.get(getPartKey(part));
875
909
  const state = asRecord(persisted?.state);
@@ -1119,13 +1153,24 @@ ${diagnostic.userMessage}` : diagnostic.userMessage;
1119
1153
  details: { failureNote: diagnostic.failureNote }
1120
1154
  }
1121
1155
  };
1156
+ } finally {
1157
+ reasoningClock.settle();
1122
1158
  }
1123
1159
  }
1160
+ const timedParts = () => {
1161
+ const timed = new Map(partMap);
1162
+ for (const [key, part] of partMap) {
1163
+ if (part.type !== "reasoning") continue;
1164
+ const time = reasoningClock.time(key, asRecord(part.time));
1165
+ if (time) timed.set(key, { ...part, time });
1166
+ }
1167
+ return timed;
1168
+ };
1124
1169
  return {
1125
1170
  stream: stream(),
1126
1171
  finalText: () => fullText,
1127
1172
  assistantParts: () => finalizePendingInteractionParts(
1128
- finalizeAssistantParts(partOrder, partMap, fullText),
1173
+ finalizeAssistantParts(partOrder, timedParts(), fullText),
1129
1174
  interactionOutcome
1130
1175
  ),
1131
1176
  // Mid-stream snapshot for incremental persistence: the same accumulators,
@@ -1133,7 +1178,7 @@ ${diagnostic.userMessage}` : diagnostic.userMessage;
1133
1178
  // and `finalizePendingInteractionParts`). A running tool and an unanswered
1134
1179
  // ask are the correct live state; settling them early would persist
1135
1180
  // phantom failures the final write then reverses.
1136
- draftParts: () => draftAssistantParts(partOrder, partMap, fullText),
1181
+ draftParts: () => draftAssistantParts(partOrder, timedParts(), fullText),
1137
1182
  usage: () => usage,
1138
1183
  // A GETTER, not a captured value: `turn-routes` reads `producer.model` at
1139
1184
  // draft-snapshot and persist time, both of which happen after the stream
@@ -1167,6 +1212,7 @@ function createRouterTurnProducer(options) {
1167
1212
  let textPart = null;
1168
1213
  let textSegment = 0;
1169
1214
  let reasoningPart = null;
1215
+ const reasoningClock = createReasoningClock();
1170
1216
  const push = (event) => {
1171
1217
  queue.push(event);
1172
1218
  wake?.();
@@ -1182,6 +1228,7 @@ function createRouterTurnProducer(options) {
1182
1228
  }
1183
1229
  textPart.text += event.delta;
1184
1230
  reasoningPart = null;
1231
+ reasoningClock.settle();
1185
1232
  push({ type: "text", text: event.delta });
1186
1233
  } else if (event.type === "reasoning") {
1187
1234
  if (!reasoningPart) {
@@ -1189,9 +1236,11 @@ function createRouterTurnProducer(options) {
1189
1236
  parts.push(reasoningPart);
1190
1237
  }
1191
1238
  reasoningPart.text += event.delta;
1239
+ reasoningClock.reasoning(reasoningPart);
1192
1240
  push({ type: "reasoning", text: event.delta });
1193
1241
  } else if (event.type === "tool") {
1194
1242
  reasoningPart = null;
1243
+ reasoningClock.settle(event.startedAt);
1195
1244
  if (event.status === "running") {
1196
1245
  const part = {
1197
1246
  type: "tool",
@@ -1232,6 +1281,7 @@ function createRouterTurnProducer(options) {
1232
1281
  push({ type: "error", data: { message: result.failure.message, code: result.failure.code } });
1233
1282
  }
1234
1283
  }).finally(() => {
1284
+ reasoningClock.settle();
1235
1285
  done = true;
1236
1286
  wake?.();
1237
1287
  });
@@ -1246,6 +1296,7 @@ function createRouterTurnProducer(options) {
1246
1296
  }
1247
1297
  await run;
1248
1298
  }
1299
+ const snapshot = () => parts.map((part) => part.type === "reasoning" ? { ...part, time: reasoningClock.time(part) } : { ...part });
1249
1300
  const usage = () => ({
1250
1301
  inputTokens: outcome?.usage.promptTokens ?? 0,
1251
1302
  outputTokens: outcome?.usage.completionTokens ?? 0
@@ -1253,8 +1304,8 @@ function createRouterTurnProducer(options) {
1253
1304
  return {
1254
1305
  stream: stream(),
1255
1306
  finalText: () => outcome?.text ?? "",
1256
- assistantParts: () => parts.map((part) => ({ ...part })),
1257
- draftParts: () => parts.map((part) => ({ ...part })),
1307
+ assistantParts: snapshot,
1308
+ draftParts: snapshot,
1258
1309
  usage,
1259
1310
  get model() {
1260
1311
  return outcome?.servedModel ?? options.model;