@abelxiaoxing/cadence 2.1.10 → 2.1.13

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.
@@ -53,6 +53,7 @@ export class CaptureState {
53
53
  this.messages = new Map();
54
54
  this.calls = new Map();
55
55
  this.blocks = new Map();
56
+ this.thinkingEntries = 0;
56
57
  this.dataFrames = 0;
57
58
  this.controlFrames = 0;
58
59
  this.bytes = 0;
@@ -76,7 +77,8 @@ export class CaptureState {
76
77
  this.details.size +
77
78
  this.messages.size +
78
79
  this.calls.size +
79
- this.blocks.size >
80
+ this.blocks.size +
81
+ this.thinkingEntries >
80
82
  LIMITS.captureEntries
81
83
  )
82
84
  throw new ContractError("capacity");
@@ -138,6 +140,7 @@ export class CaptureState {
138
140
  finalHash: createHash("sha256"),
139
141
  finalBytes: 0,
140
142
  finalIndex: -1,
143
+ thinking: new Map(),
141
144
  });
142
145
  break;
143
146
  case "detail-begin":
@@ -219,6 +222,18 @@ export class CaptureState {
219
222
  this.details.delete(e.detailId);
220
223
  break;
221
224
  }
225
+ case "thinking-activity": {
226
+ const m = message(e.messageId);
227
+ requireFact(!m.ended && m.role === "assistant");
228
+ const previous = m.thinking.get(e.index);
229
+ requireFact(previous === undefined || previous === "active");
230
+ requireFact(
231
+ m.thinking.size < LIMITS.captureEntries || previous !== undefined,
232
+ );
233
+ if (previous === undefined) this.thinkingEntries++;
234
+ m.thinking.set(e.index, e.phase);
235
+ break;
236
+ }
222
237
  case "message-text": {
223
238
  const d = this.details.get(e.detailId);
224
239
  const o = d?.descriptor.owner;
@@ -333,6 +348,11 @@ export class CaptureState {
333
348
  m.finalIndices.size === e.finalBlockCount &&
334
349
  [...m.finalIndices].every((i) => i < e.finalBlockCount),
335
350
  );
351
+ requireFact(
352
+ [...m.thinking.values()].every((phase) => phase !== "active"),
353
+ );
354
+ this.thinkingEntries -= m.thinking.size;
355
+ m.thinking.clear();
336
356
  m.ended = true;
337
357
  m.final = e.finalDetailId;
338
358
  m.stopReason = e.stopReason;
@@ -593,17 +613,29 @@ function source(value, secrets) {
593
613
  }
594
614
  /** Actual awaited core-agent event adapter. Core emits one message occurrence
595
615
  * at a time; concurrent open occurrences refuse, never join by a nearest ID.
596
- * Tool joins require a unique raw ID AND name among genuine declarations/starts. */
616
+ * Declarations require a unique unresolved raw ID, then an exact private name;
617
+ * execution/result joins require a unique raw ID AND name among genuine starts. */
597
618
  export class SdkCapture {
598
619
  constructor(emitter, secrets = []) {
599
620
  this.emitter = emitter;
600
621
  this.secrets = secrets;
601
622
  this.current = null;
602
623
  this.calls = [];
624
+ // Retired declaration IDs still make a later declaration in this user turn
625
+ // ambiguous in Pi. Keep identity only, within the same unresolved window.
626
+ this.retiredDeclarations = new Map();
603
627
  this.selected = null;
604
628
  this.events = 0;
605
629
  this.updates = new WeakMap();
606
630
  }
631
+ get messageId() {
632
+ return this.current?.role === "system" ? null : (this.current?.id ?? null);
633
+ }
634
+ /** Only an actually linked execution without a declaration needs its own
635
+ * durable identity. Available at the existing pre-message_end marker write. */
636
+ get resultCallId() {
637
+ return this.current?.resultCallId ?? null;
638
+ }
607
639
  emit(e) {
608
640
  return this.emitter.emit(e);
609
641
  }
@@ -835,11 +867,15 @@ export class SdkCapture {
835
867
  name: sanitizeText(name, this.secrets) || "redacted",
836
868
  source: source(raw, this.secrets),
837
869
  declaration,
870
+ finalized: false,
838
871
  started: false,
839
872
  ended: false,
840
873
  resultLinked: false,
841
874
  };
842
- if (this.calls.length >= LIMITS.captureEntries)
875
+ if (
876
+ this.calls.length + this.retiredDeclarations.size >=
877
+ LIMITS.captureEntries
878
+ )
843
879
  throw new ContractError("capacity");
844
880
  this.calls.push(call);
845
881
  return call;
@@ -890,6 +926,7 @@ export class SdkCapture {
890
926
  call.name = sanitizeText(name, this.secrets) || "redacted";
891
927
  call.source = source(raw, this.secrets);
892
928
  }
929
+ call.finalized = true;
893
930
  const argsDetailId = await this.args(
894
931
  call,
895
932
  own(block, "arguments"),
@@ -906,6 +943,19 @@ export class SdkCapture {
906
943
  stage: "final",
907
944
  });
908
945
  } else {
946
+ if (
947
+ type === "thinking" &&
948
+ message.role === "assistant" &&
949
+ !message.thinking.has(index)
950
+ ) {
951
+ message.thinking.set(index, "complete");
952
+ await this.emit({
953
+ kind: "thinking-activity",
954
+ messageId: message.id,
955
+ index,
956
+ phase: "complete",
957
+ });
958
+ }
909
959
  const reason =
910
960
  type === "thinking"
911
961
  ? "hidden-thinking"
@@ -994,9 +1044,27 @@ export class SdkCapture {
994
1044
  case "message_start": {
995
1045
  requireFact(this.current === null);
996
1046
  const message = own(event, "message");
997
- if (own(message, "role") === "assistant")
998
- this.calls = this.calls.filter((c) => c.started && !c.ended);
1047
+ if (own(message, "role") === "assistant") {
1048
+ this.calls = this.calls.filter((c) => {
1049
+ if (c.started && !c.ended) return true;
1050
+ if (
1051
+ c.declaration &&
1052
+ c.finalized &&
1053
+ typeof c.raw === "string" &&
1054
+ c.raw
1055
+ )
1056
+ this.retiredDeclarations.set(
1057
+ c.raw,
1058
+ this.retiredDeclarations.has(c.raw) ? null : c.rawName,
1059
+ );
1060
+ return false;
1061
+ });
1062
+ }
999
1063
  const rawRole = own(message, "role");
1064
+ if (rawRole === "user") {
1065
+ this.retiredDeclarations.clear();
1066
+ this.calls = this.calls.filter((c) => c.started && !c.ended);
1067
+ }
1000
1068
  const role = rawRole === "toolResult" ? "tool-result" : rawRole;
1001
1069
  requireFact(
1002
1070
  ["system", "user", "assistant", "tool-result"].includes(role),
@@ -1010,6 +1078,27 @@ export class SdkCapture {
1010
1078
  )
1011
1079
  : [];
1012
1080
  const call = matches.length === 1 ? matches[0] : null;
1081
+ if (
1082
+ role === "tool-result" &&
1083
+ typeof own(message, "toolCallId") === "string" &&
1084
+ (!call || call.declaration)
1085
+ ) {
1086
+ // A result with no observed execution can still settle Pi's unique
1087
+ // declaration. Release that source ID for later legal reuse, without
1088
+ // fabricating an execution/resultCallId. An explicit independent
1089
+ // execution, however, never settles a declaration in Pi.
1090
+ const raw = own(message, "toolCallId"),
1091
+ name = own(message, "toolName");
1092
+ const declared = this.calls.filter(
1093
+ (c) => c.declaration && c.finalized && c.raw === raw,
1094
+ );
1095
+ if (this.retiredDeclarations.has(raw)) {
1096
+ if (!declared.length && this.retiredDeclarations.get(raw) === name)
1097
+ this.retiredDeclarations.delete(raw);
1098
+ } else if (declared.length === 1 && declared[0].rawName === name) {
1099
+ this.calls = this.calls.filter((c) => c !== declared[0]);
1100
+ }
1101
+ }
1013
1102
  if (call) {
1014
1103
  call.resultLinked = true;
1015
1104
  this.calls = this.calls.filter((c) => c !== call);
@@ -1017,8 +1106,10 @@ export class SdkCapture {
1017
1106
  this.current = {
1018
1107
  id: randomUUID(),
1019
1108
  role,
1109
+ resultCallId: call && !call.declaration ? call.id : null,
1020
1110
  streams: new Map(),
1021
1111
  declarations: new Map(),
1112
+ thinking: new Map(),
1022
1113
  };
1023
1114
  if (role === "system") {
1024
1115
  // Pi emits prompt/section/tool-loadout changes as paired system
@@ -1052,13 +1143,39 @@ export class SdkCapture {
1052
1143
  const update = own(event, "assistantMessageEvent"),
1053
1144
  type = own(update, "type");
1054
1145
  const index = own(update, "contentIndex");
1055
- if (["text_delta", "toolcall_start", "toolcall_delta"].includes(type))
1146
+ if (
1147
+ [
1148
+ "text_delta",
1149
+ "toolcall_start",
1150
+ "toolcall_delta",
1151
+ "thinking_start",
1152
+ "thinking_delta",
1153
+ "thinking_end",
1154
+ ].includes(type)
1155
+ )
1056
1156
  requireFact(
1057
1157
  Number.isSafeInteger(index) &&
1058
1158
  index >= 0 &&
1059
1159
  index <= LIMITS.dataFrames,
1060
1160
  );
1061
- if (type === "text_delta") {
1161
+ if (
1162
+ ["thinking_start", "thinking_delta", "thinking_end"].includes(type)
1163
+ ) {
1164
+ // Do not even access partial/content/delta: providers may carry opaque
1165
+ // thinking or getters here. Only the real lifecycle/index is public.
1166
+ const previous = m.thinking.get(index);
1167
+ requireFact(previous === undefined || previous === "active");
1168
+ if (previous === undefined || type !== "thinking_start") {
1169
+ const phase = type === "thinking_end" ? "complete" : "active";
1170
+ m.thinking.set(index, phase);
1171
+ await this.emit({
1172
+ kind: "thinking-activity",
1173
+ messageId: m.id,
1174
+ index,
1175
+ phase,
1176
+ });
1177
+ }
1178
+ } else if (type === "text_delta") {
1062
1179
  let stream = m.streams.get(index);
1063
1180
  if (!stream) {
1064
1181
  const blockId = randomUUID();
@@ -1138,6 +1255,20 @@ export class SdkCapture {
1138
1255
  own(message, "content"),
1139
1256
  "final",
1140
1257
  );
1258
+ for (const [index, phase] of m.thinking) {
1259
+ if (phase !== "active") continue;
1260
+ const ended = ["error", "aborted"].includes(
1261
+ own(message, "stopReason"),
1262
+ )
1263
+ ? "unavailable"
1264
+ : "complete";
1265
+ await this.emit({
1266
+ kind: "thinking-activity",
1267
+ messageId: m.id,
1268
+ index,
1269
+ phase: ended,
1270
+ });
1271
+ }
1141
1272
  const finalDetailId =
1142
1273
  m.role === "assistant"
1143
1274
  ? await this.detail(
@@ -1177,9 +1308,26 @@ export class SdkCapture {
1177
1308
  case "tool_execution_start": {
1178
1309
  const raw = own(event, "toolCallId"),
1179
1310
  name = own(event, "toolName");
1180
- const matches = this.matches(raw, name, (c) => !c.started && !c.ended);
1311
+ // Like Pi's projection, duplicate unresolved declaration IDs remain
1312
+ // ambiguous even when their full private names differ. Display names
1313
+ // (redacted/clipped on the wire) never participate in this decision.
1314
+ const declarations = this.calls.filter(
1315
+ (c) =>
1316
+ typeof raw === "string" &&
1317
+ raw.length > 0 &&
1318
+ c.raw === raw &&
1319
+ c.declaration &&
1320
+ c.finalized &&
1321
+ !c.resultLinked,
1322
+ );
1181
1323
  const call =
1182
- matches.length === 1 ? matches[0] : this.newCall(raw, name);
1324
+ !this.retiredDeclarations.has(raw) &&
1325
+ declarations.length === 1 &&
1326
+ declarations[0].rawName === name &&
1327
+ !declarations[0].started &&
1328
+ !declarations[0].ended
1329
+ ? declarations[0]
1330
+ : this.newCall(raw, name);
1183
1331
  const argsDetailId = await this.args(
1184
1332
  call,
1185
1333
  own(event, "args"),
@@ -1310,6 +1310,15 @@ export function decodeCaptureEvent(v) {
1310
1310
  resultCallId,
1311
1311
  };
1312
1312
  }
1313
+ case "thinking-activity": {
1314
+ const o = closed(v, ["kind", "messageId", "index", "phase"]);
1315
+ return {
1316
+ kind,
1317
+ messageId: decodeId(o.messageId),
1318
+ index: integer(o.index, 0, LIMITS.dataFrames),
1319
+ phase: oneOf(o.phase, ["active", "complete", "unavailable"]),
1320
+ };
1321
+ }
1313
1322
  case "message-text": {
1314
1323
  const o = closed(v, ["kind", "messageId", "blockId", "detailId"]);
1315
1324
  return {