@linnlabs/linnkit 0.37.1 → 0.37.3

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.
Files changed (57) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/cli.cjs +238 -242
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +238 -242
  5. package/dist/cli.js.map +1 -1
  6. package/dist/context-manager.cjs +198 -200
  7. package/dist/context-manager.cjs.map +1 -1
  8. package/dist/context-manager.js +198 -200
  9. package/dist/context-manager.js.map +1 -1
  10. package/dist/contracts/agentSpec.d.cts +30 -30
  11. package/dist/contracts/agentSpec.d.ts +30 -30
  12. package/dist/contracts/audit.d.cts +32 -32
  13. package/dist/contracts/audit.d.ts +32 -32
  14. package/dist/contracts/contextPolicy.d.cts +36 -36
  15. package/dist/contracts/contextPolicy.d.ts +36 -36
  16. package/dist/contracts/events.d.cts +1113 -1113
  17. package/dist/contracts/events.d.ts +1113 -1113
  18. package/dist/contracts/execution.d.cts +8 -8
  19. package/dist/contracts/execution.d.ts +8 -8
  20. package/dist/contracts/messages.d.cts +1178 -1178
  21. package/dist/contracts/messages.d.ts +1178 -1178
  22. package/dist/contracts/provider-continuation.d.cts +28 -28
  23. package/dist/contracts/provider-continuation.d.ts +28 -28
  24. package/dist/contracts/sse.d.cts +897 -897
  25. package/dist/contracts/sse.d.ts +897 -897
  26. package/dist/contracts/sub-run-trace-payload.d.cts +26 -26
  27. package/dist/contracts/sub-run-trace-payload.d.ts +26 -26
  28. package/dist/contracts/token-accounting.d.cts +38 -38
  29. package/dist/contracts/token-accounting.d.ts +38 -38
  30. package/dist/index.cjs +238 -242
  31. package/dist/index.cjs.map +1 -1
  32. package/dist/index.js +238 -242
  33. package/dist/index.js.map +1 -1
  34. package/dist/quickstart.cjs +238 -242
  35. package/dist/quickstart.cjs.map +1 -1
  36. package/dist/quickstart.js +238 -242
  37. package/dist/quickstart.js.map +1 -1
  38. package/dist/runtime-kernel/events/agentEvents.d.cts +172 -172
  39. package/dist/runtime-kernel/events/agentEvents.d.ts +172 -172
  40. package/dist/runtime-kernel/events.cjs +1 -3
  41. package/dist/runtime-kernel/events.cjs.map +1 -1
  42. package/dist/runtime-kernel/events.js +1 -3
  43. package/dist/runtime-kernel/events.js.map +1 -1
  44. package/dist/runtime-kernel/graph-engine/executorContextBuilder.d.cts +1 -0
  45. package/dist/runtime-kernel/graph-engine/executorContextBuilder.d.ts +1 -0
  46. package/dist/runtime-kernel.cjs +238 -242
  47. package/dist/runtime-kernel.cjs.map +1 -1
  48. package/dist/runtime-kernel.js +238 -242
  49. package/dist/runtime-kernel.js.map +1 -1
  50. package/dist/testkit.cjs +238 -242
  51. package/dist/testkit.cjs.map +1 -1
  52. package/dist/testkit.js +238 -242
  53. package/dist/testkit.js.map +1 -1
  54. package/docs/integration/llm-provider.md +1 -0
  55. package/docs/integration/persistence.md +10 -0
  56. package/docs/integration/realtime.md +5 -0
  57. package/package.json +2 -2
package/dist/cli.cjs CHANGED
@@ -4910,10 +4910,7 @@ function createExecuteLlmStage(dependencies) {
4910
4910
  const promptBudget = ctx.promptBudget;
4911
4911
  const promptUsageMeasurementPolicy = ctx.promptUsageMeasurementPolicy;
4912
4912
  const streamEventHandler = ctx.input.stream && ctx.eventHandler ? (event) => {
4913
- const processedEvent = processStreamEvent(event, ctx.outputProcessor);
4914
- if (processedEvent) {
4915
- ctx.eventHandler?.(processedEvent);
4916
- }
4913
+ ctx.eventHandler?.(processStreamEvent(event, ctx.outputProcessor));
4917
4914
  } : void 0;
4918
4915
  const llmCallStartedAt = Date.now();
4919
4916
  let llmResp;
@@ -5069,9 +5066,6 @@ function processStreamEvent(event, outputProcessor) {
5069
5066
  return event;
5070
5067
  }
5071
5068
  const processedContent = outputProcessor.processStreamChunk(event.content);
5072
- if (processedContent.length === 0) {
5073
- return null;
5074
- }
5075
5069
  return {
5076
5070
  ...event,
5077
5071
  content: processedContent
@@ -6252,6 +6246,204 @@ var GraphAgentExecutor = class {
6252
6246
  }
6253
6247
  };
6254
6248
 
6249
+ // src/runtime-kernel/events/eventGovernance.ts
6250
+ var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
6251
+ "audit_envelope",
6252
+ "final_answer_chunk",
6253
+ "final_answer_reset",
6254
+ "subrun_trace",
6255
+ "context_usage_snapshot"
6256
+ ];
6257
+ function isRecord6(value) {
6258
+ return !!value && typeof value === "object" && !Array.isArray(value);
6259
+ }
6260
+ function isToolCallDecisionEvent(event) {
6261
+ return event.type === "tool_call_decision";
6262
+ }
6263
+ function isToolProcessEvent(event) {
6264
+ return event.type === "tool_process";
6265
+ }
6266
+ function isSubRunTraceRuntimeEvent(event) {
6267
+ return event.type === "subrun_trace";
6268
+ }
6269
+ function isRunExecutionMetricsRuntimeEvent(event) {
6270
+ return event.type === "run_execution_metrics";
6271
+ }
6272
+ function isContextUsageSnapshotRuntimeEvent(event) {
6273
+ return event.type === "context_usage_snapshot";
6274
+ }
6275
+ function isRequiresUserInteractionRuntimeEvent(event) {
6276
+ return event.type === "requires_user_interaction";
6277
+ }
6278
+ function isAuditEnvelopeRuntimeEvent(event) {
6279
+ return event.type === "audit_envelope";
6280
+ }
6281
+ function isControlRuntimeEvent(event) {
6282
+ return event.type === "control";
6283
+ }
6284
+ function isHiddenUserInputEvent(event) {
6285
+ if (event.type !== "user_input") {
6286
+ return false;
6287
+ }
6288
+ const metadata = isRecord6(event.metadata) ? event.metadata : void 0;
6289
+ const ui = metadata && isRecord6(metadata.ui) ? metadata.ui : void 0;
6290
+ return ui?.presentation === "hidden";
6291
+ }
6292
+ function isEmptyTerminalAssistantRuntimeEvent(event) {
6293
+ if (event.type !== "thought" && event.type !== "final_answer") {
6294
+ return false;
6295
+ }
6296
+ return event.content.trim().length === 0;
6297
+ }
6298
+ function isConversationUiRuntimeEvent(event) {
6299
+ return event.lane === "foreground" && event.visibility === "conversation";
6300
+ }
6301
+ function shouldPersistRuntimeEvent(event) {
6302
+ return describeRuntimeEventLifecycle(event).persist;
6303
+ }
6304
+ function requirePersistableRuntimeEvent(event) {
6305
+ if (!shouldPersistRuntimeEvent(event)) {
6306
+ throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
6307
+ }
6308
+ return event;
6309
+ }
6310
+ function requirePersistableRoutedRuntimeEvent(event) {
6311
+ requirePersistableRuntimeEvent(event);
6312
+ return parseRoutedRuntimeEvent(event);
6313
+ }
6314
+ function shouldReplayRuntimeEventToUi(event) {
6315
+ return describeRuntimeEventLifecycle(event).replayToUi;
6316
+ }
6317
+ function shouldEnterAgentContext(event) {
6318
+ return describeRuntimeEventLifecycle(event).enterAgentContext;
6319
+ }
6320
+ function getRuntimeEventUiProjectionKind(event) {
6321
+ if (isHiddenUserInputEvent(event)) {
6322
+ return "hidden";
6323
+ }
6324
+ switch (event.type) {
6325
+ case "user_input":
6326
+ return "user_input";
6327
+ case "thought":
6328
+ return "thought";
6329
+ case "final_answer":
6330
+ return "final_answer";
6331
+ case "final_answer_chunk":
6332
+ return "final_answer_chunk";
6333
+ case "final_answer_reset":
6334
+ return "final_answer_reset";
6335
+ case "tool_call_decision":
6336
+ return "tool_call_decision";
6337
+ case "tool_process":
6338
+ return "tool_process";
6339
+ case "tool_output":
6340
+ return "tool_output";
6341
+ case "requires_user_interaction":
6342
+ return "requires_user_interaction";
6343
+ case "audit_envelope":
6344
+ return "audit_envelope";
6345
+ case "subrun_trace":
6346
+ return "subrun_trace";
6347
+ case "error":
6348
+ return "error";
6349
+ case "context_usage_snapshot":
6350
+ return "context_usage_snapshot";
6351
+ case "run_execution_metrics":
6352
+ return "run_execution_metrics";
6353
+ case "history_summary":
6354
+ return "history_summary";
6355
+ default:
6356
+ return "unsupported";
6357
+ }
6358
+ }
6359
+ function describeRuntimeEventLifecycle(event) {
6360
+ const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
6361
+ const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
6362
+ const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
6363
+ const enterAgentContext = (() => {
6364
+ if (uiProjectionKind === "unsupported") {
6365
+ return false;
6366
+ }
6367
+ if (isSubRunTraceRuntimeEvent(event)) {
6368
+ return false;
6369
+ }
6370
+ if (isRunExecutionMetricsRuntimeEvent(event)) {
6371
+ return false;
6372
+ }
6373
+ if (isContextUsageSnapshotRuntimeEvent(event)) {
6374
+ return false;
6375
+ }
6376
+ if (event.type === "error") {
6377
+ return false;
6378
+ }
6379
+ if (isRequiresUserInteractionRuntimeEvent(event)) {
6380
+ return false;
6381
+ }
6382
+ if (isAuditEnvelopeRuntimeEvent(event)) {
6383
+ return false;
6384
+ }
6385
+ if (isControlRuntimeEvent(event)) {
6386
+ return false;
6387
+ }
6388
+ if (isHiddenUserInputEvent(event)) {
6389
+ return false;
6390
+ }
6391
+ if (isToolProcessEvent(event)) {
6392
+ return false;
6393
+ }
6394
+ if (event.type === "thought") {
6395
+ return false;
6396
+ }
6397
+ if (event.type === "final_answer_chunk") {
6398
+ return false;
6399
+ }
6400
+ if (event.type === "final_answer_reset") {
6401
+ return false;
6402
+ }
6403
+ if (isEmptyTerminalAssistantRuntimeEvent(event)) {
6404
+ return false;
6405
+ }
6406
+ return true;
6407
+ })();
6408
+ const realtimeChannel = (() => {
6409
+ switch (uiProjectionKind) {
6410
+ case "thought":
6411
+ case "final_answer":
6412
+ case "final_answer_chunk":
6413
+ case "final_answer_reset":
6414
+ case "tool_call_decision":
6415
+ case "tool_process":
6416
+ case "tool_output":
6417
+ case "requires_user_interaction":
6418
+ case "subrun_trace":
6419
+ case "error":
6420
+ case "context_usage_snapshot":
6421
+ case "run_execution_metrics":
6422
+ case "history_summary":
6423
+ return "event_bus_sse";
6424
+ case "audit_envelope":
6425
+ return "none";
6426
+ default:
6427
+ return "none";
6428
+ }
6429
+ })();
6430
+ return {
6431
+ uiProjectionKind,
6432
+ persist,
6433
+ replayToUi,
6434
+ enterAgentContext,
6435
+ realtimeChannel
6436
+ };
6437
+ }
6438
+ function shouldEmitRuntimeEventToSse(event) {
6439
+ return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
6440
+ }
6441
+
6442
+ // src/runtime-kernel/graph-engine/functions/appendWorkingHistory.ts
6443
+ function appendWorkingHistory(history, events) {
6444
+ return [...history.filter(shouldPersistRuntimeEvent), ...events.filter(shouldPersistRuntimeEvent)];
6445
+ }
6446
+
6255
6447
  // src/runtime-kernel/graph-engine/nodes/llmNode.state.ts
6256
6448
  function assertLlmNodeStreamChunkSequence(state, answerId, seq) {
6257
6449
  const answerChanged = state.answerId !== answerId;
@@ -6336,7 +6528,7 @@ function initLlmNodeState(init) {
6336
6528
  };
6337
6529
  }
6338
6530
  function buildLocalPatch(nodeState, ctx) {
6339
- const updatedHistory = [...ctx.history, ...nodeState.streamRuntimeEvents];
6531
+ const updatedHistory = appendWorkingHistory(ctx.history, nodeState.streamRuntimeEvents);
6340
6532
  const patch = {
6341
6533
  answerId: nodeState.answerId,
6342
6534
  chunkSeq: nodeState.chunkSeq,
@@ -6489,11 +6681,11 @@ var AgentEventSchema = zod.z.discriminatedUnion("type", [
6489
6681
  });
6490
6682
 
6491
6683
  // src/runtime-kernel/events/provider-sidecar.ts
6492
- var isRecord6 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6684
+ var isRecord7 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6493
6685
  function readMetaFromEvent(event) {
6494
- if (!isRecord6(event)) return void 0;
6686
+ if (!isRecord7(event)) return void 0;
6495
6687
  const value = event["meta"];
6496
- return isRecord6(value) ? value : void 0;
6688
+ return isRecord7(value) ? value : void 0;
6497
6689
  }
6498
6690
 
6499
6691
  // src/runtime-kernel/events/agent-to-runtime.ts
@@ -6501,7 +6693,7 @@ function agentEventToRuntime(agentEvent, context, options = {}) {
6501
6693
  return mapAgentEventToRuntime(agentEvent, context, options);
6502
6694
  }
6503
6695
  function mapAgentEventToRuntime(agentEvent, context, options) {
6504
- if (isRecord6(agentEvent) && agentEvent["type"] === "history_summary") {
6696
+ if (isRecord7(agentEvent) && agentEvent["type"] === "history_summary") {
6505
6697
  const event = RuntimeEvent.parse(agentEvent);
6506
6698
  return context.metadata ? {
6507
6699
  ...event,
@@ -6642,9 +6834,7 @@ function mapToolProgressToRuntime(toolEvent, context, options, id, timestamp) {
6642
6834
  return event;
6643
6835
  }
6644
6836
  function mapStreamChunkToRuntime(streamEvent, context, id, timestamp) {
6645
- const text = streamEvent.content ?? "";
6646
- if (!text) return null;
6647
- const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, text, {
6837
+ const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, streamEvent.content, {
6648
6838
  timestamp,
6649
6839
  is_last: streamEvent.is_last === true
6650
6840
  });
@@ -7164,7 +7354,7 @@ var LlmNode = class {
7164
7354
  ];
7165
7355
  logger7.info("[LlmNode] \u5386\u53F2\u4E8B\u4EF6\u5DF2\u66F4\u65B0", {
7166
7356
  previousCount: history.length,
7167
- nextCount: parseRuntimeEvents(patch.history).length,
7357
+ nextCount: patch.history.length,
7168
7358
  streamedEventCount: nodeState.streamRuntimeEvents.length
7169
7359
  });
7170
7360
  logger7.info("[LlmNode] \u6267\u884C\u5668\u51B3\u7B56\u5B8C\u6210", {
@@ -7211,7 +7401,7 @@ var ToolModelInputResolutionError = class extends Error {
7211
7401
  // src/runtime-kernel/tools/model-input/functions/parseToolModelInputDeclaration.ts
7212
7402
  var DECLARATION_KEYS = /* @__PURE__ */ new Set(["attachments"]);
7213
7403
  var SELECTION_KEYS = /* @__PURE__ */ new Set(["id", "uri", "label"]);
7214
- function isRecord7(value) {
7404
+ function isRecord8(value) {
7215
7405
  return value !== null && typeof value === "object" && !Array.isArray(value);
7216
7406
  }
7217
7407
  function hasOnlyKeys(value, allowed) {
@@ -7224,7 +7414,7 @@ function readTrimmedString(value, maxLength) {
7224
7414
  return value === value.trim() ? value : void 0;
7225
7415
  }
7226
7416
  function parseSelection(value, index) {
7227
- if (!isRecord7(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7417
+ if (!isRecord8(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7228
7418
  return { ok: false, reason: `modelInput.attachments[${index}] \u5FC5\u987B\u662F strict selection\u3002` };
7229
7419
  }
7230
7420
  const id = readTrimmedString(value.id, 200);
@@ -7245,7 +7435,7 @@ function parseSelection(value, index) {
7245
7435
  return Object.freeze({ id, uri });
7246
7436
  }
7247
7437
  function parseToolModelInputDeclaration(value) {
7248
- if (!isRecord7(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7438
+ if (!isRecord8(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7249
7439
  return { ok: false, reason: "modelInput \u5FC5\u987B\u662F\u53EA\u5305\u542B attachments \u7684 strict \u5BF9\u8C61\u3002" };
7250
7440
  }
7251
7441
  if (!Array.isArray(value.attachments) || value.attachments.length === 0) {
@@ -7321,7 +7511,7 @@ function resolveFinalAnswerFromToolControl(finalAnswer) {
7321
7511
  }
7322
7512
 
7323
7513
  // src/runtime-kernel/graph-engine/nodes/toolNode.helpers.ts
7324
- function isRecord8(value) {
7514
+ function isRecord9(value) {
7325
7515
  return !!value && typeof value === "object" && !Array.isArray(value);
7326
7516
  }
7327
7517
  function readString3(value) {
@@ -7364,7 +7554,7 @@ function resolveToolObservationGovernancePolicy(policy) {
7364
7554
  }
7365
7555
  function readObservationPreviewMeta(parsed) {
7366
7556
  const meta = parsed["observationPreviewMeta"];
7367
- if (!isRecord8(meta)) {
7557
+ if (!isRecord9(meta)) {
7368
7558
  return void 0;
7369
7559
  }
7370
7560
  const filename = readString3(meta["filename"]);
@@ -7382,7 +7572,7 @@ function readObservationPreviewMeta(parsed) {
7382
7572
  };
7383
7573
  }
7384
7574
  async function applyObservationGovernance(params) {
7385
- if (!params.structuredObservation || !isRecord8(params.parsed)) {
7575
+ if (!params.structuredObservation || !isRecord9(params.parsed)) {
7386
7576
  throw new Error("Observation governance requires a validated structured tool result.");
7387
7577
  }
7388
7578
  const policy = resolveToolObservationGovernancePolicy(params.policy);
@@ -7474,7 +7664,7 @@ function createToolProtocolFuseError(nextCount, error) {
7474
7664
  // src/runtime-kernel/graph-engine/nodes/toolNode.stateTransitions.ts
7475
7665
  function mergeHistory(local, runtimeEvents) {
7476
7666
  const history = parseRuntimeEvents(local.history ?? []);
7477
- return [...history, ...runtimeEvents];
7667
+ return appendWorkingHistory(history, runtimeEvents);
7478
7668
  }
7479
7669
  function stripAnswerState(local) {
7480
7670
  const nextLocal = { ...local };
@@ -7483,7 +7673,7 @@ function stripAnswerState(local) {
7483
7673
  return nextLocal;
7484
7674
  }
7485
7675
  function validateStructuredToolResultContract(parsed) {
7486
- if (!isRecord8(parsed)) {
7676
+ if (!isRecord9(parsed)) {
7487
7677
  return { ok: false, reason: "\u5DE5\u5177\u6210\u529F\u7ED3\u679C\u5FC5\u987B\u662F JSON \u5BF9\u8C61\u3002" };
7488
7678
  }
7489
7679
  if (!Object.prototype.hasOwnProperty.call(parsed, "data") || parsed.data === void 0) {
@@ -7511,7 +7701,7 @@ function validateStructuredToolResultContract(parsed) {
7511
7701
  };
7512
7702
  }
7513
7703
  function extractToolControlInfo(parsed) {
7514
- if (!isRecord8(parsed) || !isRecord8(parsed.control)) {
7704
+ if (!isRecord9(parsed) || !isRecord9(parsed.control)) {
7515
7705
  return void 0;
7516
7706
  }
7517
7707
  const control = parsed.control;
@@ -7535,7 +7725,7 @@ function buildRequireUserLocalState(params) {
7535
7725
  ...params.local,
7536
7726
  pendingToolCalls: params.remainingCalls,
7537
7727
  pendingInteractionSpec: {
7538
- ...isRecord8(params.parsed) && isRecord8(params.parsed.control) ? params.parsed.control : {},
7728
+ ...isRecord9(params.parsed) && isRecord9(params.parsed.control) ? params.parsed.control : {},
7539
7729
  // wait_user 必须携带工具已校验的结构化结果,UI 才能使用正式 form identity。
7540
7730
  form: params.parsed,
7541
7731
  toolCallId: params.toolCallId,
@@ -7574,7 +7764,7 @@ function buildErrorLocalState(params) {
7574
7764
 
7575
7765
  // src/runtime-kernel/tools/argNormalizer.ts
7576
7766
  var logger8 = new Logger("ToolArgNormalizer");
7577
- function isRecord9(v) {
7767
+ function isRecord10(v) {
7578
7768
  return !!v && typeof v === "object" && !Array.isArray(v);
7579
7769
  }
7580
7770
  function tryParseJsonEncodedValue(v) {
@@ -7667,12 +7857,12 @@ function normalizeByPropertySchema(prop, v, options) {
7667
7857
  }
7668
7858
  case "object": {
7669
7859
  const parsedValue = tryParseJsonEncodedValue(v);
7670
- if (parsedValue !== v && isRecord9(parsedValue)) {
7860
+ if (parsedValue !== v && isRecord10(parsedValue)) {
7671
7861
  logJsonEncodedNormalization("object", options);
7672
7862
  }
7673
- if (!isRecord9(parsedValue)) return parsedValue;
7863
+ if (!isRecord10(parsedValue)) return parsedValue;
7674
7864
  const selectedSchema = selectDiscriminatedObjectSchema(prop, parsedValue);
7675
- if (!selectedSchema.properties || !isRecord9(selectedSchema.properties)) return parsedValue;
7865
+ if (!selectedSchema.properties || !isRecord10(selectedSchema.properties)) return parsedValue;
7676
7866
  return normalizeToolArgs(
7677
7867
  { properties: selectedSchema.properties },
7678
7868
  parsedValue,
@@ -7694,7 +7884,7 @@ function normalizeToolArgs(schema, args, options = {}) {
7694
7884
  }
7695
7885
  return next;
7696
7886
  }
7697
- function isRecord10(v) {
7887
+ function isRecord11(v) {
7698
7888
  return !!v && typeof v === "object" && !Array.isArray(v);
7699
7889
  }
7700
7890
  function stableStringify(v) {
@@ -7704,7 +7894,7 @@ function stableStringify(v) {
7704
7894
  if (t === "string") return JSON.stringify(v);
7705
7895
  if (t === "number" || t === "boolean") return String(v);
7706
7896
  if (Array.isArray(v)) return `[${v.map(stableStringify).join(",")}]`;
7707
- if (isRecord10(v)) {
7897
+ if (isRecord11(v)) {
7708
7898
  const keys = Object.keys(v).sort();
7709
7899
  const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(v[k])}`);
7710
7900
  return `{${parts.join(",")}}`;
@@ -7741,13 +7931,13 @@ function findCachedToolOutputByIdempotencyKey(params) {
7741
7931
  if (e.tool_name !== params.toolName) continue;
7742
7932
  if (e.status !== "success") continue;
7743
7933
  const meta = e.metadata;
7744
- if (!isRecord10(meta)) continue;
7934
+ if (!isRecord11(meta)) continue;
7745
7935
  const idem = meta["idempotency"];
7746
- if (!isRecord10(idem)) continue;
7936
+ if (!isRecord11(idem)) continue;
7747
7937
  if (idem["key"] !== params.idempotencyKey) continue;
7748
7938
  const structuredResult = { data: e.data, observation: e.observation };
7749
7939
  const presentation = meta?.presentation;
7750
- if (isRecord10(presentation) && presentation["media"] !== void 0) {
7940
+ if (isRecord11(presentation) && presentation["media"] !== void 0) {
7751
7941
  structuredResult["media"] = presentation["media"];
7752
7942
  }
7753
7943
  return {
@@ -8021,7 +8211,7 @@ function parseToolArgs(call) {
8021
8211
  }
8022
8212
  try {
8023
8213
  const parsed = JSON.parse(rawArguments);
8024
- if (!isRecord8(parsed)) {
8214
+ if (!isRecord9(parsed)) {
8025
8215
  return {
8026
8216
  ok: false,
8027
8217
  error: "Tool arguments must decode to a JSON object."
@@ -8039,13 +8229,12 @@ function parseToolArgs(call) {
8039
8229
  }
8040
8230
  function prepareToolNodeContext(state) {
8041
8231
  const local = state.local || {};
8042
- const toolContext = isRecord8(local.toolContext) ? local.toolContext : {};
8043
- if (!isRecord8(local.toolContext)) {
8232
+ const toolContext = isRecord9(local.toolContext) ? local.toolContext : {};
8233
+ if (!isRecord9(local.toolContext)) {
8044
8234
  local.toolContext = toolContext;
8045
8235
  }
8046
8236
  const conversationId = requireRuntimeIdentity(local.conversationId, "conversationId");
8047
8237
  const turnId = requireRuntimeIdentity(local.turnId, "turnId");
8048
- parseRuntimeEvents(local.history ?? []);
8049
8238
  const existingConversationId = typeof toolContext.conversationId === "string" ? toolContext.conversationId.trim() : "";
8050
8239
  ensureToolContextRuntimeCapability({
8051
8240
  context: toolContext,
@@ -8313,11 +8502,11 @@ function createPendingToolCancellationEvents(params) {
8313
8502
  return events;
8314
8503
  }
8315
8504
  function buildCancelledToolBatchLocalState(local, runtimeEvents) {
8316
- const history = Array.isArray(local.history) ? local.history : [];
8505
+ const history = parseRuntimeEvents(local.history ?? []);
8317
8506
  return {
8318
8507
  ...stripAnswerState(local),
8319
8508
  pendingToolCalls: [],
8320
- history: [...history, ...runtimeEvents]
8509
+ history: appendWorkingHistory(history, runtimeEvents)
8321
8510
  };
8322
8511
  }
8323
8512
  function recordToolProtocolError(params) {
@@ -8386,14 +8575,14 @@ function readHistoryEvents(local) {
8386
8575
  }
8387
8576
  function readLastSuccessfulLlmModelId(local) {
8388
8577
  const executorLocal = local.executorLocal;
8389
- if (!isRecord8(executorLocal)) {
8578
+ if (!isRecord9(executorLocal)) {
8390
8579
  return void 0;
8391
8580
  }
8392
8581
  const value = executorLocal.lastSuccessfulLlmModelId;
8393
8582
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
8394
8583
  }
8395
8584
  function formatCapabilityError(error) {
8396
- if (isRecord8(error)) {
8585
+ if (isRecord9(error)) {
8397
8586
  const codeValue = error.errorCode ?? error.code;
8398
8587
  const code = typeof codeValue === "string" ? codeValue.trim() : "";
8399
8588
  const message = typeof error.message === "string" ? error.message.trim() : "";
@@ -9008,16 +9197,16 @@ var ToolNode = class {
9008
9197
  };
9009
9198
 
9010
9199
  // src/runtime-kernel/graph-engine/nodes/userNode.ts
9011
- function isRecord11(value) {
9200
+ function isRecord12(value) {
9012
9201
  return typeof value === "object" && value !== null;
9013
9202
  }
9014
9203
  function isRuntimeEvent(value) {
9015
- return isRecord11(value) && typeof value.type === "string";
9204
+ return isRecord12(value) && typeof value.type === "string";
9016
9205
  }
9017
9206
  var UserNode = class {
9018
9207
  id = "user";
9019
9208
  async run(state) {
9020
- const local = isRecord11(state.local) ? state.local : {};
9209
+ const local = isRecord12(state.local) ? state.local : {};
9021
9210
  const newEvents = Array.isArray(local.newEvents) ? local.newEvents.filter(isRuntimeEvent) : [];
9022
9211
  const userInputEvent = newEvents.find((event) => event.type === "user_input");
9023
9212
  if (!userInputEvent) {
@@ -9028,7 +9217,7 @@ var UserNode = class {
9028
9217
  };
9029
9218
 
9030
9219
  // src/runtime-kernel/graph-engine/nodes/waitUserNode.ts
9031
- function isRecord12(value) {
9220
+ function isRecord13(value) {
9032
9221
  return !!value && typeof value === "object" && !Array.isArray(value);
9033
9222
  }
9034
9223
  function requireNonEmptyString(value, field) {
@@ -9045,10 +9234,10 @@ var WaitUserNode = class {
9045
9234
  }
9046
9235
  async run(state) {
9047
9236
  const local = state.local ?? {};
9048
- const spec = isRecord12(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9237
+ const spec = isRecord13(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9049
9238
  const conversationId = requireNonEmptyString(local.conversationId, "conversationId");
9050
9239
  const turnId = requireNonEmptyString(local.turnId, "turnId");
9051
- const toolContext = isRecord12(local.toolContext) ? local.toolContext : {};
9240
+ const toolContext = isRecord13(local.toolContext) ? local.toolContext : {};
9052
9241
  const runId = RunIdSchema.parse(requireNonEmptyString(toolContext.runId, "toolContext.runId"));
9053
9242
  const parentRunId = toolContext.parentRunId === void 0 ? void 0 : RunIdSchema.parse(toolContext.parentRunId);
9054
9243
  const toolCallId = ToolCallIdSchema.parse(
@@ -9203,199 +9392,6 @@ __export(events_exports, {
9203
9392
  shouldReplayRuntimeEventToUi: () => shouldReplayRuntimeEventToUi
9204
9393
  });
9205
9394
 
9206
- // src/runtime-kernel/events/eventGovernance.ts
9207
- var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
9208
- "audit_envelope",
9209
- "final_answer_chunk",
9210
- "final_answer_reset",
9211
- "subrun_trace",
9212
- "context_usage_snapshot"
9213
- ];
9214
- function isRecord13(value) {
9215
- return !!value && typeof value === "object" && !Array.isArray(value);
9216
- }
9217
- function isToolCallDecisionEvent(event) {
9218
- return event.type === "tool_call_decision";
9219
- }
9220
- function isToolProcessEvent(event) {
9221
- return event.type === "tool_process";
9222
- }
9223
- function isSubRunTraceRuntimeEvent(event) {
9224
- return event.type === "subrun_trace";
9225
- }
9226
- function isRunExecutionMetricsRuntimeEvent(event) {
9227
- return event.type === "run_execution_metrics";
9228
- }
9229
- function isContextUsageSnapshotRuntimeEvent(event) {
9230
- return event.type === "context_usage_snapshot";
9231
- }
9232
- function isRequiresUserInteractionRuntimeEvent(event) {
9233
- return event.type === "requires_user_interaction";
9234
- }
9235
- function isAuditEnvelopeRuntimeEvent(event) {
9236
- return event.type === "audit_envelope";
9237
- }
9238
- function isControlRuntimeEvent(event) {
9239
- return event.type === "control";
9240
- }
9241
- function isHiddenUserInputEvent(event) {
9242
- if (event.type !== "user_input") {
9243
- return false;
9244
- }
9245
- const metadata = isRecord13(event.metadata) ? event.metadata : void 0;
9246
- const ui = metadata && isRecord13(metadata.ui) ? metadata.ui : void 0;
9247
- return ui?.presentation === "hidden";
9248
- }
9249
- function isEmptyTerminalAssistantRuntimeEvent(event) {
9250
- if (event.type !== "thought" && event.type !== "final_answer") {
9251
- return false;
9252
- }
9253
- return event.content.trim().length === 0;
9254
- }
9255
- function isConversationUiRuntimeEvent(event) {
9256
- return event.lane === "foreground" && event.visibility === "conversation";
9257
- }
9258
- function shouldPersistRuntimeEvent(event) {
9259
- return describeRuntimeEventLifecycle(event).persist;
9260
- }
9261
- function requirePersistableRuntimeEvent(event) {
9262
- if (!shouldPersistRuntimeEvent(event)) {
9263
- throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
9264
- }
9265
- return event;
9266
- }
9267
- function requirePersistableRoutedRuntimeEvent(event) {
9268
- requirePersistableRuntimeEvent(event);
9269
- return parseRoutedRuntimeEvent(event);
9270
- }
9271
- function shouldReplayRuntimeEventToUi(event) {
9272
- return describeRuntimeEventLifecycle(event).replayToUi;
9273
- }
9274
- function shouldEnterAgentContext(event) {
9275
- return describeRuntimeEventLifecycle(event).enterAgentContext;
9276
- }
9277
- function getRuntimeEventUiProjectionKind(event) {
9278
- if (isHiddenUserInputEvent(event)) {
9279
- return "hidden";
9280
- }
9281
- switch (event.type) {
9282
- case "user_input":
9283
- return "user_input";
9284
- case "thought":
9285
- return "thought";
9286
- case "final_answer":
9287
- return "final_answer";
9288
- case "final_answer_chunk":
9289
- return "final_answer_chunk";
9290
- case "final_answer_reset":
9291
- return "final_answer_reset";
9292
- case "tool_call_decision":
9293
- return "tool_call_decision";
9294
- case "tool_process":
9295
- return "tool_process";
9296
- case "tool_output":
9297
- return "tool_output";
9298
- case "requires_user_interaction":
9299
- return "requires_user_interaction";
9300
- case "audit_envelope":
9301
- return "audit_envelope";
9302
- case "subrun_trace":
9303
- return "subrun_trace";
9304
- case "error":
9305
- return "error";
9306
- case "context_usage_snapshot":
9307
- return "context_usage_snapshot";
9308
- case "run_execution_metrics":
9309
- return "run_execution_metrics";
9310
- case "history_summary":
9311
- return "history_summary";
9312
- default:
9313
- return "unsupported";
9314
- }
9315
- }
9316
- function describeRuntimeEventLifecycle(event) {
9317
- const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
9318
- const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
9319
- const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
9320
- const enterAgentContext = (() => {
9321
- if (uiProjectionKind === "unsupported") {
9322
- return false;
9323
- }
9324
- if (isSubRunTraceRuntimeEvent(event)) {
9325
- return false;
9326
- }
9327
- if (isRunExecutionMetricsRuntimeEvent(event)) {
9328
- return false;
9329
- }
9330
- if (isContextUsageSnapshotRuntimeEvent(event)) {
9331
- return false;
9332
- }
9333
- if (event.type === "error") {
9334
- return false;
9335
- }
9336
- if (isRequiresUserInteractionRuntimeEvent(event)) {
9337
- return false;
9338
- }
9339
- if (isAuditEnvelopeRuntimeEvent(event)) {
9340
- return false;
9341
- }
9342
- if (isControlRuntimeEvent(event)) {
9343
- return false;
9344
- }
9345
- if (isHiddenUserInputEvent(event)) {
9346
- return false;
9347
- }
9348
- if (isToolProcessEvent(event)) {
9349
- return false;
9350
- }
9351
- if (event.type === "thought") {
9352
- return false;
9353
- }
9354
- if (event.type === "final_answer_chunk") {
9355
- return false;
9356
- }
9357
- if (event.type === "final_answer_reset") {
9358
- return false;
9359
- }
9360
- if (isEmptyTerminalAssistantRuntimeEvent(event)) {
9361
- return false;
9362
- }
9363
- return true;
9364
- })();
9365
- const realtimeChannel = (() => {
9366
- switch (uiProjectionKind) {
9367
- case "thought":
9368
- case "final_answer":
9369
- case "final_answer_chunk":
9370
- case "final_answer_reset":
9371
- case "tool_call_decision":
9372
- case "tool_process":
9373
- case "tool_output":
9374
- case "requires_user_interaction":
9375
- case "subrun_trace":
9376
- case "error":
9377
- case "context_usage_snapshot":
9378
- case "run_execution_metrics":
9379
- case "history_summary":
9380
- return "event_bus_sse";
9381
- case "audit_envelope":
9382
- return "none";
9383
- default:
9384
- return "none";
9385
- }
9386
- })();
9387
- return {
9388
- uiProjectionKind,
9389
- persist,
9390
- replayToUi,
9391
- enterAgentContext,
9392
- realtimeChannel
9393
- };
9394
- }
9395
- function shouldEmitRuntimeEventToSse(event) {
9396
- return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
9397
- }
9398
-
9399
9395
  // src/runtime-kernel/events/finalAnswerCompletion.ts
9400
9396
  function findTerminalFinalAnswer(events) {
9401
9397
  for (let index = events.length - 1; index >= 0; index -= 1) {