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