@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/testkit.cjs CHANGED
@@ -5220,10 +5220,7 @@ function createExecuteLlmStage(dependencies) {
5220
5220
  const promptBudget = ctx.promptBudget;
5221
5221
  const promptUsageMeasurementPolicy = ctx.promptUsageMeasurementPolicy;
5222
5222
  const streamEventHandler = ctx.input.stream && ctx.eventHandler ? (event) => {
5223
- const processedEvent = processStreamEvent(event, ctx.outputProcessor);
5224
- if (processedEvent) {
5225
- ctx.eventHandler?.(processedEvent);
5226
- }
5223
+ ctx.eventHandler?.(processStreamEvent(event, ctx.outputProcessor));
5227
5224
  } : void 0;
5228
5225
  const llmCallStartedAt = Date.now();
5229
5226
  let llmResp;
@@ -5379,9 +5376,6 @@ function processStreamEvent(event, outputProcessor) {
5379
5376
  return event;
5380
5377
  }
5381
5378
  const processedContent = outputProcessor.processStreamChunk(event.content);
5382
- if (processedContent.length === 0) {
5383
- return null;
5384
- }
5385
5379
  return {
5386
5380
  ...event,
5387
5381
  content: processedContent
@@ -6562,6 +6556,204 @@ var GraphAgentExecutor = class {
6562
6556
  }
6563
6557
  };
6564
6558
 
6559
+ // src/runtime-kernel/events/eventGovernance.ts
6560
+ var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
6561
+ "audit_envelope",
6562
+ "final_answer_chunk",
6563
+ "final_answer_reset",
6564
+ "subrun_trace",
6565
+ "context_usage_snapshot"
6566
+ ];
6567
+ function isRecord6(value) {
6568
+ return !!value && typeof value === "object" && !Array.isArray(value);
6569
+ }
6570
+ function isToolCallDecisionEvent(event) {
6571
+ return event.type === "tool_call_decision";
6572
+ }
6573
+ function isToolProcessEvent(event) {
6574
+ return event.type === "tool_process";
6575
+ }
6576
+ function isSubRunTraceRuntimeEvent(event) {
6577
+ return event.type === "subrun_trace";
6578
+ }
6579
+ function isRunExecutionMetricsRuntimeEvent(event) {
6580
+ return event.type === "run_execution_metrics";
6581
+ }
6582
+ function isContextUsageSnapshotRuntimeEvent(event) {
6583
+ return event.type === "context_usage_snapshot";
6584
+ }
6585
+ function isRequiresUserInteractionRuntimeEvent(event) {
6586
+ return event.type === "requires_user_interaction";
6587
+ }
6588
+ function isAuditEnvelopeRuntimeEvent(event) {
6589
+ return event.type === "audit_envelope";
6590
+ }
6591
+ function isControlRuntimeEvent(event) {
6592
+ return event.type === "control";
6593
+ }
6594
+ function isHiddenUserInputEvent(event) {
6595
+ if (event.type !== "user_input") {
6596
+ return false;
6597
+ }
6598
+ const metadata = isRecord6(event.metadata) ? event.metadata : void 0;
6599
+ const ui = metadata && isRecord6(metadata.ui) ? metadata.ui : void 0;
6600
+ return ui?.presentation === "hidden";
6601
+ }
6602
+ function isEmptyTerminalAssistantRuntimeEvent(event) {
6603
+ if (event.type !== "thought" && event.type !== "final_answer") {
6604
+ return false;
6605
+ }
6606
+ return event.content.trim().length === 0;
6607
+ }
6608
+ function isConversationUiRuntimeEvent(event) {
6609
+ return event.lane === "foreground" && event.visibility === "conversation";
6610
+ }
6611
+ function shouldPersistRuntimeEvent(event) {
6612
+ return describeRuntimeEventLifecycle(event).persist;
6613
+ }
6614
+ function requirePersistableRuntimeEvent(event) {
6615
+ if (!shouldPersistRuntimeEvent(event)) {
6616
+ throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
6617
+ }
6618
+ return event;
6619
+ }
6620
+ function requirePersistableRoutedRuntimeEvent(event) {
6621
+ requirePersistableRuntimeEvent(event);
6622
+ return parseRoutedRuntimeEvent(event);
6623
+ }
6624
+ function shouldReplayRuntimeEventToUi(event) {
6625
+ return describeRuntimeEventLifecycle(event).replayToUi;
6626
+ }
6627
+ function shouldEnterAgentContext(event) {
6628
+ return describeRuntimeEventLifecycle(event).enterAgentContext;
6629
+ }
6630
+ function getRuntimeEventUiProjectionKind(event) {
6631
+ if (isHiddenUserInputEvent(event)) {
6632
+ return "hidden";
6633
+ }
6634
+ switch (event.type) {
6635
+ case "user_input":
6636
+ return "user_input";
6637
+ case "thought":
6638
+ return "thought";
6639
+ case "final_answer":
6640
+ return "final_answer";
6641
+ case "final_answer_chunk":
6642
+ return "final_answer_chunk";
6643
+ case "final_answer_reset":
6644
+ return "final_answer_reset";
6645
+ case "tool_call_decision":
6646
+ return "tool_call_decision";
6647
+ case "tool_process":
6648
+ return "tool_process";
6649
+ case "tool_output":
6650
+ return "tool_output";
6651
+ case "requires_user_interaction":
6652
+ return "requires_user_interaction";
6653
+ case "audit_envelope":
6654
+ return "audit_envelope";
6655
+ case "subrun_trace":
6656
+ return "subrun_trace";
6657
+ case "error":
6658
+ return "error";
6659
+ case "context_usage_snapshot":
6660
+ return "context_usage_snapshot";
6661
+ case "run_execution_metrics":
6662
+ return "run_execution_metrics";
6663
+ case "history_summary":
6664
+ return "history_summary";
6665
+ default:
6666
+ return "unsupported";
6667
+ }
6668
+ }
6669
+ function describeRuntimeEventLifecycle(event) {
6670
+ const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
6671
+ const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
6672
+ const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
6673
+ const enterAgentContext = (() => {
6674
+ if (uiProjectionKind === "unsupported") {
6675
+ return false;
6676
+ }
6677
+ if (isSubRunTraceRuntimeEvent(event)) {
6678
+ return false;
6679
+ }
6680
+ if (isRunExecutionMetricsRuntimeEvent(event)) {
6681
+ return false;
6682
+ }
6683
+ if (isContextUsageSnapshotRuntimeEvent(event)) {
6684
+ return false;
6685
+ }
6686
+ if (event.type === "error") {
6687
+ return false;
6688
+ }
6689
+ if (isRequiresUserInteractionRuntimeEvent(event)) {
6690
+ return false;
6691
+ }
6692
+ if (isAuditEnvelopeRuntimeEvent(event)) {
6693
+ return false;
6694
+ }
6695
+ if (isControlRuntimeEvent(event)) {
6696
+ return false;
6697
+ }
6698
+ if (isHiddenUserInputEvent(event)) {
6699
+ return false;
6700
+ }
6701
+ if (isToolProcessEvent(event)) {
6702
+ return false;
6703
+ }
6704
+ if (event.type === "thought") {
6705
+ return false;
6706
+ }
6707
+ if (event.type === "final_answer_chunk") {
6708
+ return false;
6709
+ }
6710
+ if (event.type === "final_answer_reset") {
6711
+ return false;
6712
+ }
6713
+ if (isEmptyTerminalAssistantRuntimeEvent(event)) {
6714
+ return false;
6715
+ }
6716
+ return true;
6717
+ })();
6718
+ const realtimeChannel = (() => {
6719
+ switch (uiProjectionKind) {
6720
+ case "thought":
6721
+ case "final_answer":
6722
+ case "final_answer_chunk":
6723
+ case "final_answer_reset":
6724
+ case "tool_call_decision":
6725
+ case "tool_process":
6726
+ case "tool_output":
6727
+ case "requires_user_interaction":
6728
+ case "subrun_trace":
6729
+ case "error":
6730
+ case "context_usage_snapshot":
6731
+ case "run_execution_metrics":
6732
+ case "history_summary":
6733
+ return "event_bus_sse";
6734
+ case "audit_envelope":
6735
+ return "none";
6736
+ default:
6737
+ return "none";
6738
+ }
6739
+ })();
6740
+ return {
6741
+ uiProjectionKind,
6742
+ persist,
6743
+ replayToUi,
6744
+ enterAgentContext,
6745
+ realtimeChannel
6746
+ };
6747
+ }
6748
+ function shouldEmitRuntimeEventToSse(event) {
6749
+ return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
6750
+ }
6751
+
6752
+ // src/runtime-kernel/graph-engine/functions/appendWorkingHistory.ts
6753
+ function appendWorkingHistory(history, events) {
6754
+ return [...history.filter(shouldPersistRuntimeEvent), ...events.filter(shouldPersistRuntimeEvent)];
6755
+ }
6756
+
6565
6757
  // src/runtime-kernel/graph-engine/nodes/llmNode.state.ts
6566
6758
  function assertLlmNodeStreamChunkSequence(state, answerId, seq) {
6567
6759
  const answerChanged = state.answerId !== answerId;
@@ -6646,7 +6838,7 @@ function initLlmNodeState(init) {
6646
6838
  };
6647
6839
  }
6648
6840
  function buildLocalPatch(nodeState, ctx) {
6649
- const updatedHistory = [...ctx.history, ...nodeState.streamRuntimeEvents];
6841
+ const updatedHistory = appendWorkingHistory(ctx.history, nodeState.streamRuntimeEvents);
6650
6842
  const patch = {
6651
6843
  answerId: nodeState.answerId,
6652
6844
  chunkSeq: nodeState.chunkSeq,
@@ -6799,11 +6991,11 @@ var AgentEventSchema = zod.z.discriminatedUnion("type", [
6799
6991
  });
6800
6992
 
6801
6993
  // src/runtime-kernel/events/provider-sidecar.ts
6802
- var isRecord6 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6994
+ var isRecord7 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6803
6995
  function readMetaFromEvent(event) {
6804
- if (!isRecord6(event)) return void 0;
6996
+ if (!isRecord7(event)) return void 0;
6805
6997
  const value = event["meta"];
6806
- return isRecord6(value) ? value : void 0;
6998
+ return isRecord7(value) ? value : void 0;
6807
6999
  }
6808
7000
 
6809
7001
  // src/runtime-kernel/events/agent-to-runtime.ts
@@ -6811,7 +7003,7 @@ function agentEventToRuntime(agentEvent, context, options = {}) {
6811
7003
  return mapAgentEventToRuntime(agentEvent, context, options);
6812
7004
  }
6813
7005
  function mapAgentEventToRuntime(agentEvent, context, options) {
6814
- if (isRecord6(agentEvent) && agentEvent["type"] === "history_summary") {
7006
+ if (isRecord7(agentEvent) && agentEvent["type"] === "history_summary") {
6815
7007
  const event = RuntimeEvent.parse(agentEvent);
6816
7008
  return context.metadata ? {
6817
7009
  ...event,
@@ -6952,9 +7144,7 @@ function mapToolProgressToRuntime(toolEvent, context, options, id, timestamp) {
6952
7144
  return event;
6953
7145
  }
6954
7146
  function mapStreamChunkToRuntime(streamEvent, context, id, timestamp) {
6955
- const text = streamEvent.content ?? "";
6956
- if (!text) return null;
6957
- const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, text, {
7147
+ const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, streamEvent.content, {
6958
7148
  timestamp,
6959
7149
  is_last: streamEvent.is_last === true
6960
7150
  });
@@ -7474,7 +7664,7 @@ var LlmNode = class {
7474
7664
  ];
7475
7665
  logger7.info("[LlmNode] \u5386\u53F2\u4E8B\u4EF6\u5DF2\u66F4\u65B0", {
7476
7666
  previousCount: history.length,
7477
- nextCount: parseRuntimeEvents(patch.history).length,
7667
+ nextCount: patch.history.length,
7478
7668
  streamedEventCount: nodeState.streamRuntimeEvents.length
7479
7669
  });
7480
7670
  logger7.info("[LlmNode] \u6267\u884C\u5668\u51B3\u7B56\u5B8C\u6210", {
@@ -7521,7 +7711,7 @@ var ToolModelInputResolutionError = class extends Error {
7521
7711
  // src/runtime-kernel/tools/model-input/functions/parseToolModelInputDeclaration.ts
7522
7712
  var DECLARATION_KEYS = /* @__PURE__ */ new Set(["attachments"]);
7523
7713
  var SELECTION_KEYS = /* @__PURE__ */ new Set(["id", "uri", "label"]);
7524
- function isRecord7(value) {
7714
+ function isRecord8(value) {
7525
7715
  return value !== null && typeof value === "object" && !Array.isArray(value);
7526
7716
  }
7527
7717
  function hasOnlyKeys(value, allowed) {
@@ -7534,7 +7724,7 @@ function readTrimmedString(value, maxLength) {
7534
7724
  return value === value.trim() ? value : void 0;
7535
7725
  }
7536
7726
  function parseSelection(value, index) {
7537
- if (!isRecord7(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7727
+ if (!isRecord8(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7538
7728
  return { ok: false, reason: `modelInput.attachments[${index}] \u5FC5\u987B\u662F strict selection\u3002` };
7539
7729
  }
7540
7730
  const id = readTrimmedString(value.id, 200);
@@ -7555,7 +7745,7 @@ function parseSelection(value, index) {
7555
7745
  return Object.freeze({ id, uri });
7556
7746
  }
7557
7747
  function parseToolModelInputDeclaration(value) {
7558
- if (!isRecord7(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7748
+ if (!isRecord8(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7559
7749
  return { ok: false, reason: "modelInput \u5FC5\u987B\u662F\u53EA\u5305\u542B attachments \u7684 strict \u5BF9\u8C61\u3002" };
7560
7750
  }
7561
7751
  if (!Array.isArray(value.attachments) || value.attachments.length === 0) {
@@ -7631,7 +7821,7 @@ function resolveFinalAnswerFromToolControl(finalAnswer) {
7631
7821
  }
7632
7822
 
7633
7823
  // src/runtime-kernel/graph-engine/nodes/toolNode.helpers.ts
7634
- function isRecord8(value) {
7824
+ function isRecord9(value) {
7635
7825
  return !!value && typeof value === "object" && !Array.isArray(value);
7636
7826
  }
7637
7827
  function readString3(value) {
@@ -7674,7 +7864,7 @@ function resolveToolObservationGovernancePolicy(policy) {
7674
7864
  }
7675
7865
  function readObservationPreviewMeta(parsed) {
7676
7866
  const meta = parsed["observationPreviewMeta"];
7677
- if (!isRecord8(meta)) {
7867
+ if (!isRecord9(meta)) {
7678
7868
  return void 0;
7679
7869
  }
7680
7870
  const filename = readString3(meta["filename"]);
@@ -7692,7 +7882,7 @@ function readObservationPreviewMeta(parsed) {
7692
7882
  };
7693
7883
  }
7694
7884
  async function applyObservationGovernance(params) {
7695
- if (!params.structuredObservation || !isRecord8(params.parsed)) {
7885
+ if (!params.structuredObservation || !isRecord9(params.parsed)) {
7696
7886
  throw new Error("Observation governance requires a validated structured tool result.");
7697
7887
  }
7698
7888
  const policy = resolveToolObservationGovernancePolicy(params.policy);
@@ -7784,7 +7974,7 @@ function createToolProtocolFuseError(nextCount, error) {
7784
7974
  // src/runtime-kernel/graph-engine/nodes/toolNode.stateTransitions.ts
7785
7975
  function mergeHistory(local, runtimeEvents) {
7786
7976
  const history = parseRuntimeEvents(local.history ?? []);
7787
- return [...history, ...runtimeEvents];
7977
+ return appendWorkingHistory(history, runtimeEvents);
7788
7978
  }
7789
7979
  function stripAnswerState(local) {
7790
7980
  const nextLocal = { ...local };
@@ -7793,7 +7983,7 @@ function stripAnswerState(local) {
7793
7983
  return nextLocal;
7794
7984
  }
7795
7985
  function validateStructuredToolResultContract(parsed) {
7796
- if (!isRecord8(parsed)) {
7986
+ if (!isRecord9(parsed)) {
7797
7987
  return { ok: false, reason: "\u5DE5\u5177\u6210\u529F\u7ED3\u679C\u5FC5\u987B\u662F JSON \u5BF9\u8C61\u3002" };
7798
7988
  }
7799
7989
  if (!Object.prototype.hasOwnProperty.call(parsed, "data") || parsed.data === void 0) {
@@ -7821,7 +8011,7 @@ function validateStructuredToolResultContract(parsed) {
7821
8011
  };
7822
8012
  }
7823
8013
  function extractToolControlInfo(parsed) {
7824
- if (!isRecord8(parsed) || !isRecord8(parsed.control)) {
8014
+ if (!isRecord9(parsed) || !isRecord9(parsed.control)) {
7825
8015
  return void 0;
7826
8016
  }
7827
8017
  const control = parsed.control;
@@ -7845,7 +8035,7 @@ function buildRequireUserLocalState(params) {
7845
8035
  ...params.local,
7846
8036
  pendingToolCalls: params.remainingCalls,
7847
8037
  pendingInteractionSpec: {
7848
- ...isRecord8(params.parsed) && isRecord8(params.parsed.control) ? params.parsed.control : {},
8038
+ ...isRecord9(params.parsed) && isRecord9(params.parsed.control) ? params.parsed.control : {},
7849
8039
  // wait_user 必须携带工具已校验的结构化结果,UI 才能使用正式 form identity。
7850
8040
  form: params.parsed,
7851
8041
  toolCallId: params.toolCallId,
@@ -7884,7 +8074,7 @@ function buildErrorLocalState(params) {
7884
8074
 
7885
8075
  // src/runtime-kernel/tools/argNormalizer.ts
7886
8076
  var logger8 = new Logger("ToolArgNormalizer");
7887
- function isRecord9(v) {
8077
+ function isRecord10(v) {
7888
8078
  return !!v && typeof v === "object" && !Array.isArray(v);
7889
8079
  }
7890
8080
  function tryParseJsonEncodedValue(v) {
@@ -7977,12 +8167,12 @@ function normalizeByPropertySchema(prop, v, options) {
7977
8167
  }
7978
8168
  case "object": {
7979
8169
  const parsedValue = tryParseJsonEncodedValue(v);
7980
- if (parsedValue !== v && isRecord9(parsedValue)) {
8170
+ if (parsedValue !== v && isRecord10(parsedValue)) {
7981
8171
  logJsonEncodedNormalization("object", options);
7982
8172
  }
7983
- if (!isRecord9(parsedValue)) return parsedValue;
8173
+ if (!isRecord10(parsedValue)) return parsedValue;
7984
8174
  const selectedSchema = selectDiscriminatedObjectSchema(prop, parsedValue);
7985
- if (!selectedSchema.properties || !isRecord9(selectedSchema.properties)) return parsedValue;
8175
+ if (!selectedSchema.properties || !isRecord10(selectedSchema.properties)) return parsedValue;
7986
8176
  return normalizeToolArgs(
7987
8177
  { properties: selectedSchema.properties },
7988
8178
  parsedValue,
@@ -8004,7 +8194,7 @@ function normalizeToolArgs(schema, args, options = {}) {
8004
8194
  }
8005
8195
  return next;
8006
8196
  }
8007
- function isRecord10(v) {
8197
+ function isRecord11(v) {
8008
8198
  return !!v && typeof v === "object" && !Array.isArray(v);
8009
8199
  }
8010
8200
  function stableStringify(v) {
@@ -8014,7 +8204,7 @@ function stableStringify(v) {
8014
8204
  if (t === "string") return JSON.stringify(v);
8015
8205
  if (t === "number" || t === "boolean") return String(v);
8016
8206
  if (Array.isArray(v)) return `[${v.map(stableStringify).join(",")}]`;
8017
- if (isRecord10(v)) {
8207
+ if (isRecord11(v)) {
8018
8208
  const keys = Object.keys(v).sort();
8019
8209
  const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(v[k])}`);
8020
8210
  return `{${parts.join(",")}}`;
@@ -8051,13 +8241,13 @@ function findCachedToolOutputByIdempotencyKey(params) {
8051
8241
  if (e.tool_name !== params.toolName) continue;
8052
8242
  if (e.status !== "success") continue;
8053
8243
  const meta = e.metadata;
8054
- if (!isRecord10(meta)) continue;
8244
+ if (!isRecord11(meta)) continue;
8055
8245
  const idem = meta["idempotency"];
8056
- if (!isRecord10(idem)) continue;
8246
+ if (!isRecord11(idem)) continue;
8057
8247
  if (idem["key"] !== params.idempotencyKey) continue;
8058
8248
  const structuredResult = { data: e.data, observation: e.observation };
8059
8249
  const presentation = meta?.presentation;
8060
- if (isRecord10(presentation) && presentation["media"] !== void 0) {
8250
+ if (isRecord11(presentation) && presentation["media"] !== void 0) {
8061
8251
  structuredResult["media"] = presentation["media"];
8062
8252
  }
8063
8253
  return {
@@ -8392,7 +8582,7 @@ function parseToolArgs(call) {
8392
8582
  }
8393
8583
  try {
8394
8584
  const parsed = JSON.parse(rawArguments);
8395
- if (!isRecord8(parsed)) {
8585
+ if (!isRecord9(parsed)) {
8396
8586
  return {
8397
8587
  ok: false,
8398
8588
  error: "Tool arguments must decode to a JSON object."
@@ -8410,13 +8600,12 @@ function parseToolArgs(call) {
8410
8600
  }
8411
8601
  function prepareToolNodeContext(state) {
8412
8602
  const local = state.local || {};
8413
- const toolContext = isRecord8(local.toolContext) ? local.toolContext : {};
8414
- if (!isRecord8(local.toolContext)) {
8603
+ const toolContext = isRecord9(local.toolContext) ? local.toolContext : {};
8604
+ if (!isRecord9(local.toolContext)) {
8415
8605
  local.toolContext = toolContext;
8416
8606
  }
8417
8607
  const conversationId = requireRuntimeIdentity(local.conversationId, "conversationId");
8418
8608
  const turnId = requireRuntimeIdentity(local.turnId, "turnId");
8419
- parseRuntimeEvents(local.history ?? []);
8420
8609
  const existingConversationId = typeof toolContext.conversationId === "string" ? toolContext.conversationId.trim() : "";
8421
8610
  ensureToolContextRuntimeCapability({
8422
8611
  context: toolContext,
@@ -8684,11 +8873,11 @@ function createPendingToolCancellationEvents(params) {
8684
8873
  return events;
8685
8874
  }
8686
8875
  function buildCancelledToolBatchLocalState(local, runtimeEvents) {
8687
- const history = Array.isArray(local.history) ? local.history : [];
8876
+ const history = parseRuntimeEvents(local.history ?? []);
8688
8877
  return {
8689
8878
  ...stripAnswerState(local),
8690
8879
  pendingToolCalls: [],
8691
- history: [...history, ...runtimeEvents]
8880
+ history: appendWorkingHistory(history, runtimeEvents)
8692
8881
  };
8693
8882
  }
8694
8883
  function recordToolProtocolError(params) {
@@ -8757,14 +8946,14 @@ function readHistoryEvents(local) {
8757
8946
  }
8758
8947
  function readLastSuccessfulLlmModelId(local) {
8759
8948
  const executorLocal = local.executorLocal;
8760
- if (!isRecord8(executorLocal)) {
8949
+ if (!isRecord9(executorLocal)) {
8761
8950
  return void 0;
8762
8951
  }
8763
8952
  const value = executorLocal.lastSuccessfulLlmModelId;
8764
8953
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
8765
8954
  }
8766
8955
  function formatCapabilityError(error) {
8767
- if (isRecord8(error)) {
8956
+ if (isRecord9(error)) {
8768
8957
  const codeValue = error.errorCode ?? error.code;
8769
8958
  const code = typeof codeValue === "string" ? codeValue.trim() : "";
8770
8959
  const message = typeof error.message === "string" ? error.message.trim() : "";
@@ -9379,16 +9568,16 @@ var ToolNode = class {
9379
9568
  };
9380
9569
 
9381
9570
  // src/runtime-kernel/graph-engine/nodes/userNode.ts
9382
- function isRecord11(value) {
9571
+ function isRecord12(value) {
9383
9572
  return typeof value === "object" && value !== null;
9384
9573
  }
9385
9574
  function isRuntimeEvent(value) {
9386
- return isRecord11(value) && typeof value.type === "string";
9575
+ return isRecord12(value) && typeof value.type === "string";
9387
9576
  }
9388
9577
  var UserNode = class {
9389
9578
  id = "user";
9390
9579
  async run(state) {
9391
- const local = isRecord11(state.local) ? state.local : {};
9580
+ const local = isRecord12(state.local) ? state.local : {};
9392
9581
  const newEvents = Array.isArray(local.newEvents) ? local.newEvents.filter(isRuntimeEvent) : [];
9393
9582
  const userInputEvent = newEvents.find((event) => event.type === "user_input");
9394
9583
  if (!userInputEvent) {
@@ -9399,7 +9588,7 @@ var UserNode = class {
9399
9588
  };
9400
9589
 
9401
9590
  // src/runtime-kernel/graph-engine/nodes/waitUserNode.ts
9402
- function isRecord12(value) {
9591
+ function isRecord13(value) {
9403
9592
  return !!value && typeof value === "object" && !Array.isArray(value);
9404
9593
  }
9405
9594
  function requireNonEmptyString(value, field) {
@@ -9416,10 +9605,10 @@ var WaitUserNode = class {
9416
9605
  }
9417
9606
  async run(state) {
9418
9607
  const local = state.local ?? {};
9419
- const spec = isRecord12(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9608
+ const spec = isRecord13(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9420
9609
  const conversationId = requireNonEmptyString(local.conversationId, "conversationId");
9421
9610
  const turnId = requireNonEmptyString(local.turnId, "turnId");
9422
- const toolContext = isRecord12(local.toolContext) ? local.toolContext : {};
9611
+ const toolContext = isRecord13(local.toolContext) ? local.toolContext : {};
9423
9612
  const runId = RunIdSchema.parse(requireNonEmptyString(toolContext.runId, "toolContext.runId"));
9424
9613
  const parentRunId = toolContext.parentRunId === void 0 ? void 0 : RunIdSchema.parse(toolContext.parentRunId);
9425
9614
  const toolCallId = ToolCallIdSchema.parse(
@@ -9574,199 +9763,6 @@ __export(events_exports, {
9574
9763
  shouldReplayRuntimeEventToUi: () => shouldReplayRuntimeEventToUi
9575
9764
  });
9576
9765
 
9577
- // src/runtime-kernel/events/eventGovernance.ts
9578
- var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
9579
- "audit_envelope",
9580
- "final_answer_chunk",
9581
- "final_answer_reset",
9582
- "subrun_trace",
9583
- "context_usage_snapshot"
9584
- ];
9585
- function isRecord13(value) {
9586
- return !!value && typeof value === "object" && !Array.isArray(value);
9587
- }
9588
- function isToolCallDecisionEvent(event) {
9589
- return event.type === "tool_call_decision";
9590
- }
9591
- function isToolProcessEvent(event) {
9592
- return event.type === "tool_process";
9593
- }
9594
- function isSubRunTraceRuntimeEvent(event) {
9595
- return event.type === "subrun_trace";
9596
- }
9597
- function isRunExecutionMetricsRuntimeEvent(event) {
9598
- return event.type === "run_execution_metrics";
9599
- }
9600
- function isContextUsageSnapshotRuntimeEvent(event) {
9601
- return event.type === "context_usage_snapshot";
9602
- }
9603
- function isRequiresUserInteractionRuntimeEvent(event) {
9604
- return event.type === "requires_user_interaction";
9605
- }
9606
- function isAuditEnvelopeRuntimeEvent(event) {
9607
- return event.type === "audit_envelope";
9608
- }
9609
- function isControlRuntimeEvent(event) {
9610
- return event.type === "control";
9611
- }
9612
- function isHiddenUserInputEvent(event) {
9613
- if (event.type !== "user_input") {
9614
- return false;
9615
- }
9616
- const metadata = isRecord13(event.metadata) ? event.metadata : void 0;
9617
- const ui = metadata && isRecord13(metadata.ui) ? metadata.ui : void 0;
9618
- return ui?.presentation === "hidden";
9619
- }
9620
- function isEmptyTerminalAssistantRuntimeEvent(event) {
9621
- if (event.type !== "thought" && event.type !== "final_answer") {
9622
- return false;
9623
- }
9624
- return event.content.trim().length === 0;
9625
- }
9626
- function isConversationUiRuntimeEvent(event) {
9627
- return event.lane === "foreground" && event.visibility === "conversation";
9628
- }
9629
- function shouldPersistRuntimeEvent(event) {
9630
- return describeRuntimeEventLifecycle(event).persist;
9631
- }
9632
- function requirePersistableRuntimeEvent(event) {
9633
- if (!shouldPersistRuntimeEvent(event)) {
9634
- throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
9635
- }
9636
- return event;
9637
- }
9638
- function requirePersistableRoutedRuntimeEvent(event) {
9639
- requirePersistableRuntimeEvent(event);
9640
- return parseRoutedRuntimeEvent(event);
9641
- }
9642
- function shouldReplayRuntimeEventToUi(event) {
9643
- return describeRuntimeEventLifecycle(event).replayToUi;
9644
- }
9645
- function shouldEnterAgentContext(event) {
9646
- return describeRuntimeEventLifecycle(event).enterAgentContext;
9647
- }
9648
- function getRuntimeEventUiProjectionKind(event) {
9649
- if (isHiddenUserInputEvent(event)) {
9650
- return "hidden";
9651
- }
9652
- switch (event.type) {
9653
- case "user_input":
9654
- return "user_input";
9655
- case "thought":
9656
- return "thought";
9657
- case "final_answer":
9658
- return "final_answer";
9659
- case "final_answer_chunk":
9660
- return "final_answer_chunk";
9661
- case "final_answer_reset":
9662
- return "final_answer_reset";
9663
- case "tool_call_decision":
9664
- return "tool_call_decision";
9665
- case "tool_process":
9666
- return "tool_process";
9667
- case "tool_output":
9668
- return "tool_output";
9669
- case "requires_user_interaction":
9670
- return "requires_user_interaction";
9671
- case "audit_envelope":
9672
- return "audit_envelope";
9673
- case "subrun_trace":
9674
- return "subrun_trace";
9675
- case "error":
9676
- return "error";
9677
- case "context_usage_snapshot":
9678
- return "context_usage_snapshot";
9679
- case "run_execution_metrics":
9680
- return "run_execution_metrics";
9681
- case "history_summary":
9682
- return "history_summary";
9683
- default:
9684
- return "unsupported";
9685
- }
9686
- }
9687
- function describeRuntimeEventLifecycle(event) {
9688
- const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
9689
- const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
9690
- const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
9691
- const enterAgentContext = (() => {
9692
- if (uiProjectionKind === "unsupported") {
9693
- return false;
9694
- }
9695
- if (isSubRunTraceRuntimeEvent(event)) {
9696
- return false;
9697
- }
9698
- if (isRunExecutionMetricsRuntimeEvent(event)) {
9699
- return false;
9700
- }
9701
- if (isContextUsageSnapshotRuntimeEvent(event)) {
9702
- return false;
9703
- }
9704
- if (event.type === "error") {
9705
- return false;
9706
- }
9707
- if (isRequiresUserInteractionRuntimeEvent(event)) {
9708
- return false;
9709
- }
9710
- if (isAuditEnvelopeRuntimeEvent(event)) {
9711
- return false;
9712
- }
9713
- if (isControlRuntimeEvent(event)) {
9714
- return false;
9715
- }
9716
- if (isHiddenUserInputEvent(event)) {
9717
- return false;
9718
- }
9719
- if (isToolProcessEvent(event)) {
9720
- return false;
9721
- }
9722
- if (event.type === "thought") {
9723
- return false;
9724
- }
9725
- if (event.type === "final_answer_chunk") {
9726
- return false;
9727
- }
9728
- if (event.type === "final_answer_reset") {
9729
- return false;
9730
- }
9731
- if (isEmptyTerminalAssistantRuntimeEvent(event)) {
9732
- return false;
9733
- }
9734
- return true;
9735
- })();
9736
- const realtimeChannel = (() => {
9737
- switch (uiProjectionKind) {
9738
- case "thought":
9739
- case "final_answer":
9740
- case "final_answer_chunk":
9741
- case "final_answer_reset":
9742
- case "tool_call_decision":
9743
- case "tool_process":
9744
- case "tool_output":
9745
- case "requires_user_interaction":
9746
- case "subrun_trace":
9747
- case "error":
9748
- case "context_usage_snapshot":
9749
- case "run_execution_metrics":
9750
- case "history_summary":
9751
- return "event_bus_sse";
9752
- case "audit_envelope":
9753
- return "none";
9754
- default:
9755
- return "none";
9756
- }
9757
- })();
9758
- return {
9759
- uiProjectionKind,
9760
- persist,
9761
- replayToUi,
9762
- enterAgentContext,
9763
- realtimeChannel
9764
- };
9765
- }
9766
- function shouldEmitRuntimeEventToSse(event) {
9767
- return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
9768
- }
9769
-
9770
9766
  // src/runtime-kernel/events/finalAnswerCompletion.ts
9771
9767
  function findTerminalFinalAnswer(events) {
9772
9768
  for (let index = events.length - 1; index >= 0; index -= 1) {