@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
@@ -5030,10 +5030,7 @@ function createExecuteLlmStage(dependencies) {
5030
5030
  const promptBudget = ctx.promptBudget;
5031
5031
  const promptUsageMeasurementPolicy = ctx.promptUsageMeasurementPolicy;
5032
5032
  const streamEventHandler = ctx.input.stream && ctx.eventHandler ? (event) => {
5033
- const processedEvent = processStreamEvent(event, ctx.outputProcessor);
5034
- if (processedEvent) {
5035
- ctx.eventHandler?.(processedEvent);
5036
- }
5033
+ ctx.eventHandler?.(processStreamEvent(event, ctx.outputProcessor));
5037
5034
  } : void 0;
5038
5035
  const llmCallStartedAt = Date.now();
5039
5036
  let llmResp;
@@ -5189,9 +5186,6 @@ function processStreamEvent(event, outputProcessor) {
5189
5186
  return event;
5190
5187
  }
5191
5188
  const processedContent = outputProcessor.processStreamChunk(event.content);
5192
- if (processedContent.length === 0) {
5193
- return null;
5194
- }
5195
5189
  return {
5196
5190
  ...event,
5197
5191
  content: processedContent
@@ -6372,6 +6366,204 @@ var GraphAgentExecutor = class {
6372
6366
  }
6373
6367
  };
6374
6368
 
6369
+ // src/runtime-kernel/events/eventGovernance.ts
6370
+ var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
6371
+ "audit_envelope",
6372
+ "final_answer_chunk",
6373
+ "final_answer_reset",
6374
+ "subrun_trace",
6375
+ "context_usage_snapshot"
6376
+ ];
6377
+ function isRecord6(value) {
6378
+ return !!value && typeof value === "object" && !Array.isArray(value);
6379
+ }
6380
+ function isToolCallDecisionEvent(event) {
6381
+ return event.type === "tool_call_decision";
6382
+ }
6383
+ function isToolProcessEvent(event) {
6384
+ return event.type === "tool_process";
6385
+ }
6386
+ function isSubRunTraceRuntimeEvent(event) {
6387
+ return event.type === "subrun_trace";
6388
+ }
6389
+ function isRunExecutionMetricsRuntimeEvent(event) {
6390
+ return event.type === "run_execution_metrics";
6391
+ }
6392
+ function isContextUsageSnapshotRuntimeEvent(event) {
6393
+ return event.type === "context_usage_snapshot";
6394
+ }
6395
+ function isRequiresUserInteractionRuntimeEvent(event) {
6396
+ return event.type === "requires_user_interaction";
6397
+ }
6398
+ function isAuditEnvelopeRuntimeEvent(event) {
6399
+ return event.type === "audit_envelope";
6400
+ }
6401
+ function isControlRuntimeEvent(event) {
6402
+ return event.type === "control";
6403
+ }
6404
+ function isHiddenUserInputEvent(event) {
6405
+ if (event.type !== "user_input") {
6406
+ return false;
6407
+ }
6408
+ const metadata = isRecord6(event.metadata) ? event.metadata : void 0;
6409
+ const ui = metadata && isRecord6(metadata.ui) ? metadata.ui : void 0;
6410
+ return ui?.presentation === "hidden";
6411
+ }
6412
+ function isEmptyTerminalAssistantRuntimeEvent(event) {
6413
+ if (event.type !== "thought" && event.type !== "final_answer") {
6414
+ return false;
6415
+ }
6416
+ return event.content.trim().length === 0;
6417
+ }
6418
+ function isConversationUiRuntimeEvent(event) {
6419
+ return event.lane === "foreground" && event.visibility === "conversation";
6420
+ }
6421
+ function shouldPersistRuntimeEvent(event) {
6422
+ return describeRuntimeEventLifecycle(event).persist;
6423
+ }
6424
+ function requirePersistableRuntimeEvent(event) {
6425
+ if (!shouldPersistRuntimeEvent(event)) {
6426
+ throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
6427
+ }
6428
+ return event;
6429
+ }
6430
+ function requirePersistableRoutedRuntimeEvent(event) {
6431
+ requirePersistableRuntimeEvent(event);
6432
+ return parseRoutedRuntimeEvent(event);
6433
+ }
6434
+ function shouldReplayRuntimeEventToUi(event) {
6435
+ return describeRuntimeEventLifecycle(event).replayToUi;
6436
+ }
6437
+ function shouldEnterAgentContext(event) {
6438
+ return describeRuntimeEventLifecycle(event).enterAgentContext;
6439
+ }
6440
+ function getRuntimeEventUiProjectionKind(event) {
6441
+ if (isHiddenUserInputEvent(event)) {
6442
+ return "hidden";
6443
+ }
6444
+ switch (event.type) {
6445
+ case "user_input":
6446
+ return "user_input";
6447
+ case "thought":
6448
+ return "thought";
6449
+ case "final_answer":
6450
+ return "final_answer";
6451
+ case "final_answer_chunk":
6452
+ return "final_answer_chunk";
6453
+ case "final_answer_reset":
6454
+ return "final_answer_reset";
6455
+ case "tool_call_decision":
6456
+ return "tool_call_decision";
6457
+ case "tool_process":
6458
+ return "tool_process";
6459
+ case "tool_output":
6460
+ return "tool_output";
6461
+ case "requires_user_interaction":
6462
+ return "requires_user_interaction";
6463
+ case "audit_envelope":
6464
+ return "audit_envelope";
6465
+ case "subrun_trace":
6466
+ return "subrun_trace";
6467
+ case "error":
6468
+ return "error";
6469
+ case "context_usage_snapshot":
6470
+ return "context_usage_snapshot";
6471
+ case "run_execution_metrics":
6472
+ return "run_execution_metrics";
6473
+ case "history_summary":
6474
+ return "history_summary";
6475
+ default:
6476
+ return "unsupported";
6477
+ }
6478
+ }
6479
+ function describeRuntimeEventLifecycle(event) {
6480
+ const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
6481
+ const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
6482
+ const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
6483
+ const enterAgentContext = (() => {
6484
+ if (uiProjectionKind === "unsupported") {
6485
+ return false;
6486
+ }
6487
+ if (isSubRunTraceRuntimeEvent(event)) {
6488
+ return false;
6489
+ }
6490
+ if (isRunExecutionMetricsRuntimeEvent(event)) {
6491
+ return false;
6492
+ }
6493
+ if (isContextUsageSnapshotRuntimeEvent(event)) {
6494
+ return false;
6495
+ }
6496
+ if (event.type === "error") {
6497
+ return false;
6498
+ }
6499
+ if (isRequiresUserInteractionRuntimeEvent(event)) {
6500
+ return false;
6501
+ }
6502
+ if (isAuditEnvelopeRuntimeEvent(event)) {
6503
+ return false;
6504
+ }
6505
+ if (isControlRuntimeEvent(event)) {
6506
+ return false;
6507
+ }
6508
+ if (isHiddenUserInputEvent(event)) {
6509
+ return false;
6510
+ }
6511
+ if (isToolProcessEvent(event)) {
6512
+ return false;
6513
+ }
6514
+ if (event.type === "thought") {
6515
+ return false;
6516
+ }
6517
+ if (event.type === "final_answer_chunk") {
6518
+ return false;
6519
+ }
6520
+ if (event.type === "final_answer_reset") {
6521
+ return false;
6522
+ }
6523
+ if (isEmptyTerminalAssistantRuntimeEvent(event)) {
6524
+ return false;
6525
+ }
6526
+ return true;
6527
+ })();
6528
+ const realtimeChannel = (() => {
6529
+ switch (uiProjectionKind) {
6530
+ case "thought":
6531
+ case "final_answer":
6532
+ case "final_answer_chunk":
6533
+ case "final_answer_reset":
6534
+ case "tool_call_decision":
6535
+ case "tool_process":
6536
+ case "tool_output":
6537
+ case "requires_user_interaction":
6538
+ case "subrun_trace":
6539
+ case "error":
6540
+ case "context_usage_snapshot":
6541
+ case "run_execution_metrics":
6542
+ case "history_summary":
6543
+ return "event_bus_sse";
6544
+ case "audit_envelope":
6545
+ return "none";
6546
+ default:
6547
+ return "none";
6548
+ }
6549
+ })();
6550
+ return {
6551
+ uiProjectionKind,
6552
+ persist,
6553
+ replayToUi,
6554
+ enterAgentContext,
6555
+ realtimeChannel
6556
+ };
6557
+ }
6558
+ function shouldEmitRuntimeEventToSse(event) {
6559
+ return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
6560
+ }
6561
+
6562
+ // src/runtime-kernel/graph-engine/functions/appendWorkingHistory.ts
6563
+ function appendWorkingHistory(history, events) {
6564
+ return [...history.filter(shouldPersistRuntimeEvent), ...events.filter(shouldPersistRuntimeEvent)];
6565
+ }
6566
+
6375
6567
  // src/runtime-kernel/graph-engine/nodes/llmNode.state.ts
6376
6568
  function assertLlmNodeStreamChunkSequence(state, answerId, seq) {
6377
6569
  const answerChanged = state.answerId !== answerId;
@@ -6456,7 +6648,7 @@ function initLlmNodeState(init) {
6456
6648
  };
6457
6649
  }
6458
6650
  function buildLocalPatch(nodeState, ctx) {
6459
- const updatedHistory = [...ctx.history, ...nodeState.streamRuntimeEvents];
6651
+ const updatedHistory = appendWorkingHistory(ctx.history, nodeState.streamRuntimeEvents);
6460
6652
  const patch = {
6461
6653
  answerId: nodeState.answerId,
6462
6654
  chunkSeq: nodeState.chunkSeq,
@@ -6609,11 +6801,11 @@ var AgentEventSchema = z.discriminatedUnion("type", [
6609
6801
  });
6610
6802
 
6611
6803
  // src/runtime-kernel/events/provider-sidecar.ts
6612
- var isRecord6 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6804
+ var isRecord7 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6613
6805
  function readMetaFromEvent(event) {
6614
- if (!isRecord6(event)) return void 0;
6806
+ if (!isRecord7(event)) return void 0;
6615
6807
  const value = event["meta"];
6616
- return isRecord6(value) ? value : void 0;
6808
+ return isRecord7(value) ? value : void 0;
6617
6809
  }
6618
6810
 
6619
6811
  // src/runtime-kernel/events/agent-to-runtime.ts
@@ -6621,7 +6813,7 @@ function agentEventToRuntime(agentEvent, context, options = {}) {
6621
6813
  return mapAgentEventToRuntime(agentEvent, context, options);
6622
6814
  }
6623
6815
  function mapAgentEventToRuntime(agentEvent, context, options) {
6624
- if (isRecord6(agentEvent) && agentEvent["type"] === "history_summary") {
6816
+ if (isRecord7(agentEvent) && agentEvent["type"] === "history_summary") {
6625
6817
  const event = RuntimeEvent.parse(agentEvent);
6626
6818
  return context.metadata ? {
6627
6819
  ...event,
@@ -6762,9 +6954,7 @@ function mapToolProgressToRuntime(toolEvent, context, options, id, timestamp) {
6762
6954
  return event;
6763
6955
  }
6764
6956
  function mapStreamChunkToRuntime(streamEvent, context, id, timestamp) {
6765
- const text = streamEvent.content ?? "";
6766
- if (!text) return null;
6767
- const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, text, {
6957
+ const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, streamEvent.content, {
6768
6958
  timestamp,
6769
6959
  is_last: streamEvent.is_last === true
6770
6960
  });
@@ -7284,7 +7474,7 @@ var LlmNode = class {
7284
7474
  ];
7285
7475
  logger7.info("[LlmNode] \u5386\u53F2\u4E8B\u4EF6\u5DF2\u66F4\u65B0", {
7286
7476
  previousCount: history.length,
7287
- nextCount: parseRuntimeEvents(patch.history).length,
7477
+ nextCount: patch.history.length,
7288
7478
  streamedEventCount: nodeState.streamRuntimeEvents.length
7289
7479
  });
7290
7480
  logger7.info("[LlmNode] \u6267\u884C\u5668\u51B3\u7B56\u5B8C\u6210", {
@@ -7331,7 +7521,7 @@ var ToolModelInputResolutionError = class extends Error {
7331
7521
  // src/runtime-kernel/tools/model-input/functions/parseToolModelInputDeclaration.ts
7332
7522
  var DECLARATION_KEYS = /* @__PURE__ */ new Set(["attachments"]);
7333
7523
  var SELECTION_KEYS = /* @__PURE__ */ new Set(["id", "uri", "label"]);
7334
- function isRecord7(value) {
7524
+ function isRecord8(value) {
7335
7525
  return value !== null && typeof value === "object" && !Array.isArray(value);
7336
7526
  }
7337
7527
  function hasOnlyKeys(value, allowed) {
@@ -7344,7 +7534,7 @@ function readTrimmedString(value, maxLength) {
7344
7534
  return value === value.trim() ? value : void 0;
7345
7535
  }
7346
7536
  function parseSelection(value, index) {
7347
- if (!isRecord7(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7537
+ if (!isRecord8(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7348
7538
  return { ok: false, reason: `modelInput.attachments[${index}] \u5FC5\u987B\u662F strict selection\u3002` };
7349
7539
  }
7350
7540
  const id = readTrimmedString(value.id, 200);
@@ -7365,7 +7555,7 @@ function parseSelection(value, index) {
7365
7555
  return Object.freeze({ id, uri });
7366
7556
  }
7367
7557
  function parseToolModelInputDeclaration(value) {
7368
- if (!isRecord7(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7558
+ if (!isRecord8(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7369
7559
  return { ok: false, reason: "modelInput \u5FC5\u987B\u662F\u53EA\u5305\u542B attachments \u7684 strict \u5BF9\u8C61\u3002" };
7370
7560
  }
7371
7561
  if (!Array.isArray(value.attachments) || value.attachments.length === 0) {
@@ -7441,7 +7631,7 @@ function resolveFinalAnswerFromToolControl(finalAnswer) {
7441
7631
  }
7442
7632
 
7443
7633
  // src/runtime-kernel/graph-engine/nodes/toolNode.helpers.ts
7444
- function isRecord8(value) {
7634
+ function isRecord9(value) {
7445
7635
  return !!value && typeof value === "object" && !Array.isArray(value);
7446
7636
  }
7447
7637
  function readString3(value) {
@@ -7484,7 +7674,7 @@ function resolveToolObservationGovernancePolicy(policy) {
7484
7674
  }
7485
7675
  function readObservationPreviewMeta(parsed) {
7486
7676
  const meta = parsed["observationPreviewMeta"];
7487
- if (!isRecord8(meta)) {
7677
+ if (!isRecord9(meta)) {
7488
7678
  return void 0;
7489
7679
  }
7490
7680
  const filename = readString3(meta["filename"]);
@@ -7502,7 +7692,7 @@ function readObservationPreviewMeta(parsed) {
7502
7692
  };
7503
7693
  }
7504
7694
  async function applyObservationGovernance(params) {
7505
- if (!params.structuredObservation || !isRecord8(params.parsed)) {
7695
+ if (!params.structuredObservation || !isRecord9(params.parsed)) {
7506
7696
  throw new Error("Observation governance requires a validated structured tool result.");
7507
7697
  }
7508
7698
  const policy = resolveToolObservationGovernancePolicy(params.policy);
@@ -7594,7 +7784,7 @@ function createToolProtocolFuseError(nextCount, error) {
7594
7784
  // src/runtime-kernel/graph-engine/nodes/toolNode.stateTransitions.ts
7595
7785
  function mergeHistory(local, runtimeEvents) {
7596
7786
  const history = parseRuntimeEvents(local.history ?? []);
7597
- return [...history, ...runtimeEvents];
7787
+ return appendWorkingHistory(history, runtimeEvents);
7598
7788
  }
7599
7789
  function stripAnswerState(local) {
7600
7790
  const nextLocal = { ...local };
@@ -7603,7 +7793,7 @@ function stripAnswerState(local) {
7603
7793
  return nextLocal;
7604
7794
  }
7605
7795
  function validateStructuredToolResultContract(parsed) {
7606
- if (!isRecord8(parsed)) {
7796
+ if (!isRecord9(parsed)) {
7607
7797
  return { ok: false, reason: "\u5DE5\u5177\u6210\u529F\u7ED3\u679C\u5FC5\u987B\u662F JSON \u5BF9\u8C61\u3002" };
7608
7798
  }
7609
7799
  if (!Object.prototype.hasOwnProperty.call(parsed, "data") || parsed.data === void 0) {
@@ -7631,7 +7821,7 @@ function validateStructuredToolResultContract(parsed) {
7631
7821
  };
7632
7822
  }
7633
7823
  function extractToolControlInfo(parsed) {
7634
- if (!isRecord8(parsed) || !isRecord8(parsed.control)) {
7824
+ if (!isRecord9(parsed) || !isRecord9(parsed.control)) {
7635
7825
  return void 0;
7636
7826
  }
7637
7827
  const control = parsed.control;
@@ -7655,7 +7845,7 @@ function buildRequireUserLocalState(params) {
7655
7845
  ...params.local,
7656
7846
  pendingToolCalls: params.remainingCalls,
7657
7847
  pendingInteractionSpec: {
7658
- ...isRecord8(params.parsed) && isRecord8(params.parsed.control) ? params.parsed.control : {},
7848
+ ...isRecord9(params.parsed) && isRecord9(params.parsed.control) ? params.parsed.control : {},
7659
7849
  // wait_user 必须携带工具已校验的结构化结果,UI 才能使用正式 form identity。
7660
7850
  form: params.parsed,
7661
7851
  toolCallId: params.toolCallId,
@@ -7694,7 +7884,7 @@ function buildErrorLocalState(params) {
7694
7884
 
7695
7885
  // src/runtime-kernel/tools/argNormalizer.ts
7696
7886
  var logger8 = new Logger("ToolArgNormalizer");
7697
- function isRecord9(v) {
7887
+ function isRecord10(v) {
7698
7888
  return !!v && typeof v === "object" && !Array.isArray(v);
7699
7889
  }
7700
7890
  function tryParseJsonEncodedValue(v) {
@@ -7787,12 +7977,12 @@ function normalizeByPropertySchema(prop, v, options) {
7787
7977
  }
7788
7978
  case "object": {
7789
7979
  const parsedValue = tryParseJsonEncodedValue(v);
7790
- if (parsedValue !== v && isRecord9(parsedValue)) {
7980
+ if (parsedValue !== v && isRecord10(parsedValue)) {
7791
7981
  logJsonEncodedNormalization("object", options);
7792
7982
  }
7793
- if (!isRecord9(parsedValue)) return parsedValue;
7983
+ if (!isRecord10(parsedValue)) return parsedValue;
7794
7984
  const selectedSchema = selectDiscriminatedObjectSchema(prop, parsedValue);
7795
- if (!selectedSchema.properties || !isRecord9(selectedSchema.properties)) return parsedValue;
7985
+ if (!selectedSchema.properties || !isRecord10(selectedSchema.properties)) return parsedValue;
7796
7986
  return normalizeToolArgs(
7797
7987
  { properties: selectedSchema.properties },
7798
7988
  parsedValue,
@@ -7814,7 +8004,7 @@ function normalizeToolArgs(schema, args, options = {}) {
7814
8004
  }
7815
8005
  return next;
7816
8006
  }
7817
- function isRecord10(v) {
8007
+ function isRecord11(v) {
7818
8008
  return !!v && typeof v === "object" && !Array.isArray(v);
7819
8009
  }
7820
8010
  function stableStringify(v) {
@@ -7824,7 +8014,7 @@ function stableStringify(v) {
7824
8014
  if (t === "string") return JSON.stringify(v);
7825
8015
  if (t === "number" || t === "boolean") return String(v);
7826
8016
  if (Array.isArray(v)) return `[${v.map(stableStringify).join(",")}]`;
7827
- if (isRecord10(v)) {
8017
+ if (isRecord11(v)) {
7828
8018
  const keys = Object.keys(v).sort();
7829
8019
  const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(v[k])}`);
7830
8020
  return `{${parts.join(",")}}`;
@@ -7861,13 +8051,13 @@ function findCachedToolOutputByIdempotencyKey(params) {
7861
8051
  if (e.tool_name !== params.toolName) continue;
7862
8052
  if (e.status !== "success") continue;
7863
8053
  const meta = e.metadata;
7864
- if (!isRecord10(meta)) continue;
8054
+ if (!isRecord11(meta)) continue;
7865
8055
  const idem = meta["idempotency"];
7866
- if (!isRecord10(idem)) continue;
8056
+ if (!isRecord11(idem)) continue;
7867
8057
  if (idem["key"] !== params.idempotencyKey) continue;
7868
8058
  const structuredResult = { data: e.data, observation: e.observation };
7869
8059
  const presentation = meta?.presentation;
7870
- if (isRecord10(presentation) && presentation["media"] !== void 0) {
8060
+ if (isRecord11(presentation) && presentation["media"] !== void 0) {
7871
8061
  structuredResult["media"] = presentation["media"];
7872
8062
  }
7873
8063
  return {
@@ -8202,7 +8392,7 @@ function parseToolArgs(call) {
8202
8392
  }
8203
8393
  try {
8204
8394
  const parsed = JSON.parse(rawArguments);
8205
- if (!isRecord8(parsed)) {
8395
+ if (!isRecord9(parsed)) {
8206
8396
  return {
8207
8397
  ok: false,
8208
8398
  error: "Tool arguments must decode to a JSON object."
@@ -8220,13 +8410,12 @@ function parseToolArgs(call) {
8220
8410
  }
8221
8411
  function prepareToolNodeContext(state) {
8222
8412
  const local = state.local || {};
8223
- const toolContext = isRecord8(local.toolContext) ? local.toolContext : {};
8224
- if (!isRecord8(local.toolContext)) {
8413
+ const toolContext = isRecord9(local.toolContext) ? local.toolContext : {};
8414
+ if (!isRecord9(local.toolContext)) {
8225
8415
  local.toolContext = toolContext;
8226
8416
  }
8227
8417
  const conversationId = requireRuntimeIdentity(local.conversationId, "conversationId");
8228
8418
  const turnId = requireRuntimeIdentity(local.turnId, "turnId");
8229
- parseRuntimeEvents(local.history ?? []);
8230
8419
  const existingConversationId = typeof toolContext.conversationId === "string" ? toolContext.conversationId.trim() : "";
8231
8420
  ensureToolContextRuntimeCapability({
8232
8421
  context: toolContext,
@@ -8494,11 +8683,11 @@ function createPendingToolCancellationEvents(params) {
8494
8683
  return events;
8495
8684
  }
8496
8685
  function buildCancelledToolBatchLocalState(local, runtimeEvents) {
8497
- const history = Array.isArray(local.history) ? local.history : [];
8686
+ const history = parseRuntimeEvents(local.history ?? []);
8498
8687
  return {
8499
8688
  ...stripAnswerState(local),
8500
8689
  pendingToolCalls: [],
8501
- history: [...history, ...runtimeEvents]
8690
+ history: appendWorkingHistory(history, runtimeEvents)
8502
8691
  };
8503
8692
  }
8504
8693
  function recordToolProtocolError(params) {
@@ -8567,14 +8756,14 @@ function readHistoryEvents(local) {
8567
8756
  }
8568
8757
  function readLastSuccessfulLlmModelId(local) {
8569
8758
  const executorLocal = local.executorLocal;
8570
- if (!isRecord8(executorLocal)) {
8759
+ if (!isRecord9(executorLocal)) {
8571
8760
  return void 0;
8572
8761
  }
8573
8762
  const value = executorLocal.lastSuccessfulLlmModelId;
8574
8763
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
8575
8764
  }
8576
8765
  function formatCapabilityError(error) {
8577
- if (isRecord8(error)) {
8766
+ if (isRecord9(error)) {
8578
8767
  const codeValue = error.errorCode ?? error.code;
8579
8768
  const code = typeof codeValue === "string" ? codeValue.trim() : "";
8580
8769
  const message = typeof error.message === "string" ? error.message.trim() : "";
@@ -9189,16 +9378,16 @@ var ToolNode = class {
9189
9378
  };
9190
9379
 
9191
9380
  // src/runtime-kernel/graph-engine/nodes/userNode.ts
9192
- function isRecord11(value) {
9381
+ function isRecord12(value) {
9193
9382
  return typeof value === "object" && value !== null;
9194
9383
  }
9195
9384
  function isRuntimeEvent(value) {
9196
- return isRecord11(value) && typeof value.type === "string";
9385
+ return isRecord12(value) && typeof value.type === "string";
9197
9386
  }
9198
9387
  var UserNode = class {
9199
9388
  id = "user";
9200
9389
  async run(state) {
9201
- const local = isRecord11(state.local) ? state.local : {};
9390
+ const local = isRecord12(state.local) ? state.local : {};
9202
9391
  const newEvents = Array.isArray(local.newEvents) ? local.newEvents.filter(isRuntimeEvent) : [];
9203
9392
  const userInputEvent = newEvents.find((event) => event.type === "user_input");
9204
9393
  if (!userInputEvent) {
@@ -9209,7 +9398,7 @@ var UserNode = class {
9209
9398
  };
9210
9399
 
9211
9400
  // src/runtime-kernel/graph-engine/nodes/waitUserNode.ts
9212
- function isRecord12(value) {
9401
+ function isRecord13(value) {
9213
9402
  return !!value && typeof value === "object" && !Array.isArray(value);
9214
9403
  }
9215
9404
  function requireNonEmptyString(value, field) {
@@ -9226,10 +9415,10 @@ var WaitUserNode = class {
9226
9415
  }
9227
9416
  async run(state) {
9228
9417
  const local = state.local ?? {};
9229
- const spec = isRecord12(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9418
+ const spec = isRecord13(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9230
9419
  const conversationId = requireNonEmptyString(local.conversationId, "conversationId");
9231
9420
  const turnId = requireNonEmptyString(local.turnId, "turnId");
9232
- const toolContext = isRecord12(local.toolContext) ? local.toolContext : {};
9421
+ const toolContext = isRecord13(local.toolContext) ? local.toolContext : {};
9233
9422
  const runId = RunIdSchema.parse(requireNonEmptyString(toolContext.runId, "toolContext.runId"));
9234
9423
  const parentRunId = toolContext.parentRunId === void 0 ? void 0 : RunIdSchema.parse(toolContext.parentRunId);
9235
9424
  const toolCallId = ToolCallIdSchema.parse(
@@ -9384,199 +9573,6 @@ __export(events_exports, {
9384
9573
  shouldReplayRuntimeEventToUi: () => shouldReplayRuntimeEventToUi
9385
9574
  });
9386
9575
 
9387
- // src/runtime-kernel/events/eventGovernance.ts
9388
- var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
9389
- "audit_envelope",
9390
- "final_answer_chunk",
9391
- "final_answer_reset",
9392
- "subrun_trace",
9393
- "context_usage_snapshot"
9394
- ];
9395
- function isRecord13(value) {
9396
- return !!value && typeof value === "object" && !Array.isArray(value);
9397
- }
9398
- function isToolCallDecisionEvent(event) {
9399
- return event.type === "tool_call_decision";
9400
- }
9401
- function isToolProcessEvent(event) {
9402
- return event.type === "tool_process";
9403
- }
9404
- function isSubRunTraceRuntimeEvent(event) {
9405
- return event.type === "subrun_trace";
9406
- }
9407
- function isRunExecutionMetricsRuntimeEvent(event) {
9408
- return event.type === "run_execution_metrics";
9409
- }
9410
- function isContextUsageSnapshotRuntimeEvent(event) {
9411
- return event.type === "context_usage_snapshot";
9412
- }
9413
- function isRequiresUserInteractionRuntimeEvent(event) {
9414
- return event.type === "requires_user_interaction";
9415
- }
9416
- function isAuditEnvelopeRuntimeEvent(event) {
9417
- return event.type === "audit_envelope";
9418
- }
9419
- function isControlRuntimeEvent(event) {
9420
- return event.type === "control";
9421
- }
9422
- function isHiddenUserInputEvent(event) {
9423
- if (event.type !== "user_input") {
9424
- return false;
9425
- }
9426
- const metadata = isRecord13(event.metadata) ? event.metadata : void 0;
9427
- const ui = metadata && isRecord13(metadata.ui) ? metadata.ui : void 0;
9428
- return ui?.presentation === "hidden";
9429
- }
9430
- function isEmptyTerminalAssistantRuntimeEvent(event) {
9431
- if (event.type !== "thought" && event.type !== "final_answer") {
9432
- return false;
9433
- }
9434
- return event.content.trim().length === 0;
9435
- }
9436
- function isConversationUiRuntimeEvent(event) {
9437
- return event.lane === "foreground" && event.visibility === "conversation";
9438
- }
9439
- function shouldPersistRuntimeEvent(event) {
9440
- return describeRuntimeEventLifecycle(event).persist;
9441
- }
9442
- function requirePersistableRuntimeEvent(event) {
9443
- if (!shouldPersistRuntimeEvent(event)) {
9444
- throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
9445
- }
9446
- return event;
9447
- }
9448
- function requirePersistableRoutedRuntimeEvent(event) {
9449
- requirePersistableRuntimeEvent(event);
9450
- return parseRoutedRuntimeEvent(event);
9451
- }
9452
- function shouldReplayRuntimeEventToUi(event) {
9453
- return describeRuntimeEventLifecycle(event).replayToUi;
9454
- }
9455
- function shouldEnterAgentContext(event) {
9456
- return describeRuntimeEventLifecycle(event).enterAgentContext;
9457
- }
9458
- function getRuntimeEventUiProjectionKind(event) {
9459
- if (isHiddenUserInputEvent(event)) {
9460
- return "hidden";
9461
- }
9462
- switch (event.type) {
9463
- case "user_input":
9464
- return "user_input";
9465
- case "thought":
9466
- return "thought";
9467
- case "final_answer":
9468
- return "final_answer";
9469
- case "final_answer_chunk":
9470
- return "final_answer_chunk";
9471
- case "final_answer_reset":
9472
- return "final_answer_reset";
9473
- case "tool_call_decision":
9474
- return "tool_call_decision";
9475
- case "tool_process":
9476
- return "tool_process";
9477
- case "tool_output":
9478
- return "tool_output";
9479
- case "requires_user_interaction":
9480
- return "requires_user_interaction";
9481
- case "audit_envelope":
9482
- return "audit_envelope";
9483
- case "subrun_trace":
9484
- return "subrun_trace";
9485
- case "error":
9486
- return "error";
9487
- case "context_usage_snapshot":
9488
- return "context_usage_snapshot";
9489
- case "run_execution_metrics":
9490
- return "run_execution_metrics";
9491
- case "history_summary":
9492
- return "history_summary";
9493
- default:
9494
- return "unsupported";
9495
- }
9496
- }
9497
- function describeRuntimeEventLifecycle(event) {
9498
- const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
9499
- const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
9500
- const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
9501
- const enterAgentContext = (() => {
9502
- if (uiProjectionKind === "unsupported") {
9503
- return false;
9504
- }
9505
- if (isSubRunTraceRuntimeEvent(event)) {
9506
- return false;
9507
- }
9508
- if (isRunExecutionMetricsRuntimeEvent(event)) {
9509
- return false;
9510
- }
9511
- if (isContextUsageSnapshotRuntimeEvent(event)) {
9512
- return false;
9513
- }
9514
- if (event.type === "error") {
9515
- return false;
9516
- }
9517
- if (isRequiresUserInteractionRuntimeEvent(event)) {
9518
- return false;
9519
- }
9520
- if (isAuditEnvelopeRuntimeEvent(event)) {
9521
- return false;
9522
- }
9523
- if (isControlRuntimeEvent(event)) {
9524
- return false;
9525
- }
9526
- if (isHiddenUserInputEvent(event)) {
9527
- return false;
9528
- }
9529
- if (isToolProcessEvent(event)) {
9530
- return false;
9531
- }
9532
- if (event.type === "thought") {
9533
- return false;
9534
- }
9535
- if (event.type === "final_answer_chunk") {
9536
- return false;
9537
- }
9538
- if (event.type === "final_answer_reset") {
9539
- return false;
9540
- }
9541
- if (isEmptyTerminalAssistantRuntimeEvent(event)) {
9542
- return false;
9543
- }
9544
- return true;
9545
- })();
9546
- const realtimeChannel = (() => {
9547
- switch (uiProjectionKind) {
9548
- case "thought":
9549
- case "final_answer":
9550
- case "final_answer_chunk":
9551
- case "final_answer_reset":
9552
- case "tool_call_decision":
9553
- case "tool_process":
9554
- case "tool_output":
9555
- case "requires_user_interaction":
9556
- case "subrun_trace":
9557
- case "error":
9558
- case "context_usage_snapshot":
9559
- case "run_execution_metrics":
9560
- case "history_summary":
9561
- return "event_bus_sse";
9562
- case "audit_envelope":
9563
- return "none";
9564
- default:
9565
- return "none";
9566
- }
9567
- })();
9568
- return {
9569
- uiProjectionKind,
9570
- persist,
9571
- replayToUi,
9572
- enterAgentContext,
9573
- realtimeChannel
9574
- };
9575
- }
9576
- function shouldEmitRuntimeEventToSse(event) {
9577
- return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
9578
- }
9579
-
9580
9576
  // src/runtime-kernel/events/finalAnswerCompletion.ts
9581
9577
  function findTerminalFinalAnswer(events) {
9582
9578
  for (let index = events.length - 1; index >= 0; index -= 1) {