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