@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
@@ -5023,10 +5023,7 @@ function createExecuteLlmStage(dependencies) {
5023
5023
  const promptBudget = ctx.promptBudget;
5024
5024
  const promptUsageMeasurementPolicy = ctx.promptUsageMeasurementPolicy;
5025
5025
  const streamEventHandler = ctx.input.stream && ctx.eventHandler ? (event) => {
5026
- const processedEvent = processStreamEvent(event, ctx.outputProcessor);
5027
- if (processedEvent) {
5028
- ctx.eventHandler?.(processedEvent);
5029
- }
5026
+ ctx.eventHandler?.(processStreamEvent(event, ctx.outputProcessor));
5030
5027
  } : void 0;
5031
5028
  const llmCallStartedAt = Date.now();
5032
5029
  let llmResp;
@@ -5182,9 +5179,6 @@ function processStreamEvent(event, outputProcessor) {
5182
5179
  return event;
5183
5180
  }
5184
5181
  const processedContent = outputProcessor.processStreamChunk(event.content);
5185
- if (processedContent.length === 0) {
5186
- return null;
5187
- }
5188
5182
  return {
5189
5183
  ...event,
5190
5184
  content: processedContent
@@ -6365,6 +6359,204 @@ var GraphAgentExecutor = class {
6365
6359
  }
6366
6360
  };
6367
6361
 
6362
+ // src/runtime-kernel/events/eventGovernance.ts
6363
+ var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
6364
+ "audit_envelope",
6365
+ "final_answer_chunk",
6366
+ "final_answer_reset",
6367
+ "subrun_trace",
6368
+ "context_usage_snapshot"
6369
+ ];
6370
+ function isRecord6(value) {
6371
+ return !!value && typeof value === "object" && !Array.isArray(value);
6372
+ }
6373
+ function isToolCallDecisionEvent(event) {
6374
+ return event.type === "tool_call_decision";
6375
+ }
6376
+ function isToolProcessEvent(event) {
6377
+ return event.type === "tool_process";
6378
+ }
6379
+ function isSubRunTraceRuntimeEvent(event) {
6380
+ return event.type === "subrun_trace";
6381
+ }
6382
+ function isRunExecutionMetricsRuntimeEvent(event) {
6383
+ return event.type === "run_execution_metrics";
6384
+ }
6385
+ function isContextUsageSnapshotRuntimeEvent(event) {
6386
+ return event.type === "context_usage_snapshot";
6387
+ }
6388
+ function isRequiresUserInteractionRuntimeEvent(event) {
6389
+ return event.type === "requires_user_interaction";
6390
+ }
6391
+ function isAuditEnvelopeRuntimeEvent(event) {
6392
+ return event.type === "audit_envelope";
6393
+ }
6394
+ function isControlRuntimeEvent(event) {
6395
+ return event.type === "control";
6396
+ }
6397
+ function isHiddenUserInputEvent(event) {
6398
+ if (event.type !== "user_input") {
6399
+ return false;
6400
+ }
6401
+ const metadata = isRecord6(event.metadata) ? event.metadata : void 0;
6402
+ const ui = metadata && isRecord6(metadata.ui) ? metadata.ui : void 0;
6403
+ return ui?.presentation === "hidden";
6404
+ }
6405
+ function isEmptyTerminalAssistantRuntimeEvent(event) {
6406
+ if (event.type !== "thought" && event.type !== "final_answer") {
6407
+ return false;
6408
+ }
6409
+ return event.content.trim().length === 0;
6410
+ }
6411
+ function isConversationUiRuntimeEvent(event) {
6412
+ return event.lane === "foreground" && event.visibility === "conversation";
6413
+ }
6414
+ function shouldPersistRuntimeEvent(event) {
6415
+ return describeRuntimeEventLifecycle(event).persist;
6416
+ }
6417
+ function requirePersistableRuntimeEvent(event) {
6418
+ if (!shouldPersistRuntimeEvent(event)) {
6419
+ throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
6420
+ }
6421
+ return event;
6422
+ }
6423
+ function requirePersistableRoutedRuntimeEvent(event) {
6424
+ requirePersistableRuntimeEvent(event);
6425
+ return parseRoutedRuntimeEvent(event);
6426
+ }
6427
+ function shouldReplayRuntimeEventToUi(event) {
6428
+ return describeRuntimeEventLifecycle(event).replayToUi;
6429
+ }
6430
+ function shouldEnterAgentContext(event) {
6431
+ return describeRuntimeEventLifecycle(event).enterAgentContext;
6432
+ }
6433
+ function getRuntimeEventUiProjectionKind(event) {
6434
+ if (isHiddenUserInputEvent(event)) {
6435
+ return "hidden";
6436
+ }
6437
+ switch (event.type) {
6438
+ case "user_input":
6439
+ return "user_input";
6440
+ case "thought":
6441
+ return "thought";
6442
+ case "final_answer":
6443
+ return "final_answer";
6444
+ case "final_answer_chunk":
6445
+ return "final_answer_chunk";
6446
+ case "final_answer_reset":
6447
+ return "final_answer_reset";
6448
+ case "tool_call_decision":
6449
+ return "tool_call_decision";
6450
+ case "tool_process":
6451
+ return "tool_process";
6452
+ case "tool_output":
6453
+ return "tool_output";
6454
+ case "requires_user_interaction":
6455
+ return "requires_user_interaction";
6456
+ case "audit_envelope":
6457
+ return "audit_envelope";
6458
+ case "subrun_trace":
6459
+ return "subrun_trace";
6460
+ case "error":
6461
+ return "error";
6462
+ case "context_usage_snapshot":
6463
+ return "context_usage_snapshot";
6464
+ case "run_execution_metrics":
6465
+ return "run_execution_metrics";
6466
+ case "history_summary":
6467
+ return "history_summary";
6468
+ default:
6469
+ return "unsupported";
6470
+ }
6471
+ }
6472
+ function describeRuntimeEventLifecycle(event) {
6473
+ const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
6474
+ const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
6475
+ const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
6476
+ const enterAgentContext = (() => {
6477
+ if (uiProjectionKind === "unsupported") {
6478
+ return false;
6479
+ }
6480
+ if (isSubRunTraceRuntimeEvent(event)) {
6481
+ return false;
6482
+ }
6483
+ if (isRunExecutionMetricsRuntimeEvent(event)) {
6484
+ return false;
6485
+ }
6486
+ if (isContextUsageSnapshotRuntimeEvent(event)) {
6487
+ return false;
6488
+ }
6489
+ if (event.type === "error") {
6490
+ return false;
6491
+ }
6492
+ if (isRequiresUserInteractionRuntimeEvent(event)) {
6493
+ return false;
6494
+ }
6495
+ if (isAuditEnvelopeRuntimeEvent(event)) {
6496
+ return false;
6497
+ }
6498
+ if (isControlRuntimeEvent(event)) {
6499
+ return false;
6500
+ }
6501
+ if (isHiddenUserInputEvent(event)) {
6502
+ return false;
6503
+ }
6504
+ if (isToolProcessEvent(event)) {
6505
+ return false;
6506
+ }
6507
+ if (event.type === "thought") {
6508
+ return false;
6509
+ }
6510
+ if (event.type === "final_answer_chunk") {
6511
+ return false;
6512
+ }
6513
+ if (event.type === "final_answer_reset") {
6514
+ return false;
6515
+ }
6516
+ if (isEmptyTerminalAssistantRuntimeEvent(event)) {
6517
+ return false;
6518
+ }
6519
+ return true;
6520
+ })();
6521
+ const realtimeChannel = (() => {
6522
+ switch (uiProjectionKind) {
6523
+ case "thought":
6524
+ case "final_answer":
6525
+ case "final_answer_chunk":
6526
+ case "final_answer_reset":
6527
+ case "tool_call_decision":
6528
+ case "tool_process":
6529
+ case "tool_output":
6530
+ case "requires_user_interaction":
6531
+ case "subrun_trace":
6532
+ case "error":
6533
+ case "context_usage_snapshot":
6534
+ case "run_execution_metrics":
6535
+ case "history_summary":
6536
+ return "event_bus_sse";
6537
+ case "audit_envelope":
6538
+ return "none";
6539
+ default:
6540
+ return "none";
6541
+ }
6542
+ })();
6543
+ return {
6544
+ uiProjectionKind,
6545
+ persist,
6546
+ replayToUi,
6547
+ enterAgentContext,
6548
+ realtimeChannel
6549
+ };
6550
+ }
6551
+ function shouldEmitRuntimeEventToSse(event) {
6552
+ return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
6553
+ }
6554
+
6555
+ // src/runtime-kernel/graph-engine/functions/appendWorkingHistory.ts
6556
+ function appendWorkingHistory(history, events) {
6557
+ return [...history.filter(shouldPersistRuntimeEvent), ...events.filter(shouldPersistRuntimeEvent)];
6558
+ }
6559
+
6368
6560
  // src/runtime-kernel/graph-engine/nodes/llmNode.state.ts
6369
6561
  function assertLlmNodeStreamChunkSequence(state, answerId, seq) {
6370
6562
  const answerChanged = state.answerId !== answerId;
@@ -6449,7 +6641,7 @@ function initLlmNodeState(init) {
6449
6641
  };
6450
6642
  }
6451
6643
  function buildLocalPatch(nodeState, ctx) {
6452
- const updatedHistory = [...ctx.history, ...nodeState.streamRuntimeEvents];
6644
+ const updatedHistory = appendWorkingHistory(ctx.history, nodeState.streamRuntimeEvents);
6453
6645
  const patch = {
6454
6646
  answerId: nodeState.answerId,
6455
6647
  chunkSeq: nodeState.chunkSeq,
@@ -6602,11 +6794,11 @@ var AgentEventSchema = zod.z.discriminatedUnion("type", [
6602
6794
  });
6603
6795
 
6604
6796
  // src/runtime-kernel/events/provider-sidecar.ts
6605
- var isRecord6 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6797
+ var isRecord7 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6606
6798
  function readMetaFromEvent(event) {
6607
- if (!isRecord6(event)) return void 0;
6799
+ if (!isRecord7(event)) return void 0;
6608
6800
  const value = event["meta"];
6609
- return isRecord6(value) ? value : void 0;
6801
+ return isRecord7(value) ? value : void 0;
6610
6802
  }
6611
6803
 
6612
6804
  // src/runtime-kernel/events/agent-to-runtime.ts
@@ -6614,7 +6806,7 @@ function agentEventToRuntime(agentEvent, context, options = {}) {
6614
6806
  return mapAgentEventToRuntime(agentEvent, context, options);
6615
6807
  }
6616
6808
  function mapAgentEventToRuntime(agentEvent, context, options) {
6617
- if (isRecord6(agentEvent) && agentEvent["type"] === "history_summary") {
6809
+ if (isRecord7(agentEvent) && agentEvent["type"] === "history_summary") {
6618
6810
  const event = RuntimeEvent.parse(agentEvent);
6619
6811
  return context.metadata ? {
6620
6812
  ...event,
@@ -6755,9 +6947,7 @@ function mapToolProgressToRuntime(toolEvent, context, options, id, timestamp) {
6755
6947
  return event;
6756
6948
  }
6757
6949
  function mapStreamChunkToRuntime(streamEvent, context, id, timestamp) {
6758
- const text = streamEvent.content ?? "";
6759
- if (!text) return null;
6760
- const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, text, {
6950
+ const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, streamEvent.content, {
6761
6951
  timestamp,
6762
6952
  is_last: streamEvent.is_last === true
6763
6953
  });
@@ -7277,7 +7467,7 @@ var LlmNode = class {
7277
7467
  ];
7278
7468
  logger7.info("[LlmNode] \u5386\u53F2\u4E8B\u4EF6\u5DF2\u66F4\u65B0", {
7279
7469
  previousCount: history.length,
7280
- nextCount: parseRuntimeEvents(patch.history).length,
7470
+ nextCount: patch.history.length,
7281
7471
  streamedEventCount: nodeState.streamRuntimeEvents.length
7282
7472
  });
7283
7473
  logger7.info("[LlmNode] \u6267\u884C\u5668\u51B3\u7B56\u5B8C\u6210", {
@@ -7324,7 +7514,7 @@ var ToolModelInputResolutionError = class extends Error {
7324
7514
  // src/runtime-kernel/tools/model-input/functions/parseToolModelInputDeclaration.ts
7325
7515
  var DECLARATION_KEYS = /* @__PURE__ */ new Set(["attachments"]);
7326
7516
  var SELECTION_KEYS = /* @__PURE__ */ new Set(["id", "uri", "label"]);
7327
- function isRecord7(value) {
7517
+ function isRecord8(value) {
7328
7518
  return value !== null && typeof value === "object" && !Array.isArray(value);
7329
7519
  }
7330
7520
  function hasOnlyKeys(value, allowed) {
@@ -7337,7 +7527,7 @@ function readTrimmedString(value, maxLength) {
7337
7527
  return value === value.trim() ? value : void 0;
7338
7528
  }
7339
7529
  function parseSelection(value, index) {
7340
- if (!isRecord7(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7530
+ if (!isRecord8(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7341
7531
  return { ok: false, reason: `modelInput.attachments[${index}] \u5FC5\u987B\u662F strict selection\u3002` };
7342
7532
  }
7343
7533
  const id = readTrimmedString(value.id, 200);
@@ -7358,7 +7548,7 @@ function parseSelection(value, index) {
7358
7548
  return Object.freeze({ id, uri });
7359
7549
  }
7360
7550
  function parseToolModelInputDeclaration(value) {
7361
- if (!isRecord7(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7551
+ if (!isRecord8(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7362
7552
  return { ok: false, reason: "modelInput \u5FC5\u987B\u662F\u53EA\u5305\u542B attachments \u7684 strict \u5BF9\u8C61\u3002" };
7363
7553
  }
7364
7554
  if (!Array.isArray(value.attachments) || value.attachments.length === 0) {
@@ -7434,7 +7624,7 @@ function resolveFinalAnswerFromToolControl(finalAnswer) {
7434
7624
  }
7435
7625
 
7436
7626
  // src/runtime-kernel/graph-engine/nodes/toolNode.helpers.ts
7437
- function isRecord8(value) {
7627
+ function isRecord9(value) {
7438
7628
  return !!value && typeof value === "object" && !Array.isArray(value);
7439
7629
  }
7440
7630
  function readString3(value) {
@@ -7477,7 +7667,7 @@ function resolveToolObservationGovernancePolicy(policy) {
7477
7667
  }
7478
7668
  function readObservationPreviewMeta(parsed) {
7479
7669
  const meta = parsed["observationPreviewMeta"];
7480
- if (!isRecord8(meta)) {
7670
+ if (!isRecord9(meta)) {
7481
7671
  return void 0;
7482
7672
  }
7483
7673
  const filename = readString3(meta["filename"]);
@@ -7495,7 +7685,7 @@ function readObservationPreviewMeta(parsed) {
7495
7685
  };
7496
7686
  }
7497
7687
  async function applyObservationGovernance(params) {
7498
- if (!params.structuredObservation || !isRecord8(params.parsed)) {
7688
+ if (!params.structuredObservation || !isRecord9(params.parsed)) {
7499
7689
  throw new Error("Observation governance requires a validated structured tool result.");
7500
7690
  }
7501
7691
  const policy = resolveToolObservationGovernancePolicy(params.policy);
@@ -7587,7 +7777,7 @@ function createToolProtocolFuseError(nextCount, error) {
7587
7777
  // src/runtime-kernel/graph-engine/nodes/toolNode.stateTransitions.ts
7588
7778
  function mergeHistory(local, runtimeEvents) {
7589
7779
  const history = parseRuntimeEvents(local.history ?? []);
7590
- return [...history, ...runtimeEvents];
7780
+ return appendWorkingHistory(history, runtimeEvents);
7591
7781
  }
7592
7782
  function stripAnswerState(local) {
7593
7783
  const nextLocal = { ...local };
@@ -7596,7 +7786,7 @@ function stripAnswerState(local) {
7596
7786
  return nextLocal;
7597
7787
  }
7598
7788
  function validateStructuredToolResultContract(parsed) {
7599
- if (!isRecord8(parsed)) {
7789
+ if (!isRecord9(parsed)) {
7600
7790
  return { ok: false, reason: "\u5DE5\u5177\u6210\u529F\u7ED3\u679C\u5FC5\u987B\u662F JSON \u5BF9\u8C61\u3002" };
7601
7791
  }
7602
7792
  if (!Object.prototype.hasOwnProperty.call(parsed, "data") || parsed.data === void 0) {
@@ -7624,7 +7814,7 @@ function validateStructuredToolResultContract(parsed) {
7624
7814
  };
7625
7815
  }
7626
7816
  function extractToolControlInfo(parsed) {
7627
- if (!isRecord8(parsed) || !isRecord8(parsed.control)) {
7817
+ if (!isRecord9(parsed) || !isRecord9(parsed.control)) {
7628
7818
  return void 0;
7629
7819
  }
7630
7820
  const control = parsed.control;
@@ -7648,7 +7838,7 @@ function buildRequireUserLocalState(params) {
7648
7838
  ...params.local,
7649
7839
  pendingToolCalls: params.remainingCalls,
7650
7840
  pendingInteractionSpec: {
7651
- ...isRecord8(params.parsed) && isRecord8(params.parsed.control) ? params.parsed.control : {},
7841
+ ...isRecord9(params.parsed) && isRecord9(params.parsed.control) ? params.parsed.control : {},
7652
7842
  // wait_user 必须携带工具已校验的结构化结果,UI 才能使用正式 form identity。
7653
7843
  form: params.parsed,
7654
7844
  toolCallId: params.toolCallId,
@@ -7687,7 +7877,7 @@ function buildErrorLocalState(params) {
7687
7877
 
7688
7878
  // src/runtime-kernel/tools/argNormalizer.ts
7689
7879
  var logger8 = new Logger("ToolArgNormalizer");
7690
- function isRecord9(v) {
7880
+ function isRecord10(v) {
7691
7881
  return !!v && typeof v === "object" && !Array.isArray(v);
7692
7882
  }
7693
7883
  function tryParseJsonEncodedValue(v) {
@@ -7780,12 +7970,12 @@ function normalizeByPropertySchema(prop, v, options) {
7780
7970
  }
7781
7971
  case "object": {
7782
7972
  const parsedValue = tryParseJsonEncodedValue(v);
7783
- if (parsedValue !== v && isRecord9(parsedValue)) {
7973
+ if (parsedValue !== v && isRecord10(parsedValue)) {
7784
7974
  logJsonEncodedNormalization("object", options);
7785
7975
  }
7786
- if (!isRecord9(parsedValue)) return parsedValue;
7976
+ if (!isRecord10(parsedValue)) return parsedValue;
7787
7977
  const selectedSchema = selectDiscriminatedObjectSchema(prop, parsedValue);
7788
- if (!selectedSchema.properties || !isRecord9(selectedSchema.properties)) return parsedValue;
7978
+ if (!selectedSchema.properties || !isRecord10(selectedSchema.properties)) return parsedValue;
7789
7979
  return normalizeToolArgs(
7790
7980
  { properties: selectedSchema.properties },
7791
7981
  parsedValue,
@@ -7807,7 +7997,7 @@ function normalizeToolArgs(schema, args, options = {}) {
7807
7997
  }
7808
7998
  return next;
7809
7999
  }
7810
- function isRecord10(v) {
8000
+ function isRecord11(v) {
7811
8001
  return !!v && typeof v === "object" && !Array.isArray(v);
7812
8002
  }
7813
8003
  function stableStringify(v) {
@@ -7817,7 +8007,7 @@ function stableStringify(v) {
7817
8007
  if (t === "string") return JSON.stringify(v);
7818
8008
  if (t === "number" || t === "boolean") return String(v);
7819
8009
  if (Array.isArray(v)) return `[${v.map(stableStringify).join(",")}]`;
7820
- if (isRecord10(v)) {
8010
+ if (isRecord11(v)) {
7821
8011
  const keys = Object.keys(v).sort();
7822
8012
  const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(v[k])}`);
7823
8013
  return `{${parts.join(",")}}`;
@@ -7854,13 +8044,13 @@ function findCachedToolOutputByIdempotencyKey(params) {
7854
8044
  if (e.tool_name !== params.toolName) continue;
7855
8045
  if (e.status !== "success") continue;
7856
8046
  const meta = e.metadata;
7857
- if (!isRecord10(meta)) continue;
8047
+ if (!isRecord11(meta)) continue;
7858
8048
  const idem = meta["idempotency"];
7859
- if (!isRecord10(idem)) continue;
8049
+ if (!isRecord11(idem)) continue;
7860
8050
  if (idem["key"] !== params.idempotencyKey) continue;
7861
8051
  const structuredResult = { data: e.data, observation: e.observation };
7862
8052
  const presentation = meta?.presentation;
7863
- if (isRecord10(presentation) && presentation["media"] !== void 0) {
8053
+ if (isRecord11(presentation) && presentation["media"] !== void 0) {
7864
8054
  structuredResult["media"] = presentation["media"];
7865
8055
  }
7866
8056
  return {
@@ -8134,7 +8324,7 @@ function parseToolArgs(call) {
8134
8324
  }
8135
8325
  try {
8136
8326
  const parsed = JSON.parse(rawArguments);
8137
- if (!isRecord8(parsed)) {
8327
+ if (!isRecord9(parsed)) {
8138
8328
  return {
8139
8329
  ok: false,
8140
8330
  error: "Tool arguments must decode to a JSON object."
@@ -8152,13 +8342,12 @@ function parseToolArgs(call) {
8152
8342
  }
8153
8343
  function prepareToolNodeContext(state) {
8154
8344
  const local = state.local || {};
8155
- const toolContext = isRecord8(local.toolContext) ? local.toolContext : {};
8156
- if (!isRecord8(local.toolContext)) {
8345
+ const toolContext = isRecord9(local.toolContext) ? local.toolContext : {};
8346
+ if (!isRecord9(local.toolContext)) {
8157
8347
  local.toolContext = toolContext;
8158
8348
  }
8159
8349
  const conversationId = requireRuntimeIdentity(local.conversationId, "conversationId");
8160
8350
  const turnId = requireRuntimeIdentity(local.turnId, "turnId");
8161
- parseRuntimeEvents(local.history ?? []);
8162
8351
  const existingConversationId = typeof toolContext.conversationId === "string" ? toolContext.conversationId.trim() : "";
8163
8352
  ensureToolContextRuntimeCapability({
8164
8353
  context: toolContext,
@@ -8426,11 +8615,11 @@ function createPendingToolCancellationEvents(params) {
8426
8615
  return events;
8427
8616
  }
8428
8617
  function buildCancelledToolBatchLocalState(local, runtimeEvents) {
8429
- const history = Array.isArray(local.history) ? local.history : [];
8618
+ const history = parseRuntimeEvents(local.history ?? []);
8430
8619
  return {
8431
8620
  ...stripAnswerState(local),
8432
8621
  pendingToolCalls: [],
8433
- history: [...history, ...runtimeEvents]
8622
+ history: appendWorkingHistory(history, runtimeEvents)
8434
8623
  };
8435
8624
  }
8436
8625
  function recordToolProtocolError(params) {
@@ -8499,14 +8688,14 @@ function readHistoryEvents(local) {
8499
8688
  }
8500
8689
  function readLastSuccessfulLlmModelId(local) {
8501
8690
  const executorLocal = local.executorLocal;
8502
- if (!isRecord8(executorLocal)) {
8691
+ if (!isRecord9(executorLocal)) {
8503
8692
  return void 0;
8504
8693
  }
8505
8694
  const value = executorLocal.lastSuccessfulLlmModelId;
8506
8695
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
8507
8696
  }
8508
8697
  function formatCapabilityError(error) {
8509
- if (isRecord8(error)) {
8698
+ if (isRecord9(error)) {
8510
8699
  const codeValue = error.errorCode ?? error.code;
8511
8700
  const code = typeof codeValue === "string" ? codeValue.trim() : "";
8512
8701
  const message = typeof error.message === "string" ? error.message.trim() : "";
@@ -9121,16 +9310,16 @@ var ToolNode = class {
9121
9310
  };
9122
9311
 
9123
9312
  // src/runtime-kernel/graph-engine/nodes/userNode.ts
9124
- function isRecord11(value) {
9313
+ function isRecord12(value) {
9125
9314
  return typeof value === "object" && value !== null;
9126
9315
  }
9127
9316
  function isRuntimeEvent(value) {
9128
- return isRecord11(value) && typeof value.type === "string";
9317
+ return isRecord12(value) && typeof value.type === "string";
9129
9318
  }
9130
9319
  var UserNode = class {
9131
9320
  id = "user";
9132
9321
  async run(state) {
9133
- const local = isRecord11(state.local) ? state.local : {};
9322
+ const local = isRecord12(state.local) ? state.local : {};
9134
9323
  const newEvents = Array.isArray(local.newEvents) ? local.newEvents.filter(isRuntimeEvent) : [];
9135
9324
  const userInputEvent = newEvents.find((event) => event.type === "user_input");
9136
9325
  if (!userInputEvent) {
@@ -9141,7 +9330,7 @@ var UserNode = class {
9141
9330
  };
9142
9331
 
9143
9332
  // src/runtime-kernel/graph-engine/nodes/waitUserNode.ts
9144
- function isRecord12(value) {
9333
+ function isRecord13(value) {
9145
9334
  return !!value && typeof value === "object" && !Array.isArray(value);
9146
9335
  }
9147
9336
  function requireNonEmptyString(value, field) {
@@ -9158,10 +9347,10 @@ var WaitUserNode = class {
9158
9347
  }
9159
9348
  async run(state) {
9160
9349
  const local = state.local ?? {};
9161
- const spec = isRecord12(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9350
+ const spec = isRecord13(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9162
9351
  const conversationId = requireNonEmptyString(local.conversationId, "conversationId");
9163
9352
  const turnId = requireNonEmptyString(local.turnId, "turnId");
9164
- const toolContext = isRecord12(local.toolContext) ? local.toolContext : {};
9353
+ const toolContext = isRecord13(local.toolContext) ? local.toolContext : {};
9165
9354
  const runId = RunIdSchema.parse(requireNonEmptyString(toolContext.runId, "toolContext.runId"));
9166
9355
  const parentRunId = toolContext.parentRunId === void 0 ? void 0 : RunIdSchema.parse(toolContext.parentRunId);
9167
9356
  const toolCallId = ToolCallIdSchema.parse(
@@ -9316,199 +9505,6 @@ __export(events_exports, {
9316
9505
  shouldReplayRuntimeEventToUi: () => shouldReplayRuntimeEventToUi
9317
9506
  });
9318
9507
 
9319
- // src/runtime-kernel/events/eventGovernance.ts
9320
- var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
9321
- "audit_envelope",
9322
- "final_answer_chunk",
9323
- "final_answer_reset",
9324
- "subrun_trace",
9325
- "context_usage_snapshot"
9326
- ];
9327
- function isRecord13(value) {
9328
- return !!value && typeof value === "object" && !Array.isArray(value);
9329
- }
9330
- function isToolCallDecisionEvent(event) {
9331
- return event.type === "tool_call_decision";
9332
- }
9333
- function isToolProcessEvent(event) {
9334
- return event.type === "tool_process";
9335
- }
9336
- function isSubRunTraceRuntimeEvent(event) {
9337
- return event.type === "subrun_trace";
9338
- }
9339
- function isRunExecutionMetricsRuntimeEvent(event) {
9340
- return event.type === "run_execution_metrics";
9341
- }
9342
- function isContextUsageSnapshotRuntimeEvent(event) {
9343
- return event.type === "context_usage_snapshot";
9344
- }
9345
- function isRequiresUserInteractionRuntimeEvent(event) {
9346
- return event.type === "requires_user_interaction";
9347
- }
9348
- function isAuditEnvelopeRuntimeEvent(event) {
9349
- return event.type === "audit_envelope";
9350
- }
9351
- function isControlRuntimeEvent(event) {
9352
- return event.type === "control";
9353
- }
9354
- function isHiddenUserInputEvent(event) {
9355
- if (event.type !== "user_input") {
9356
- return false;
9357
- }
9358
- const metadata = isRecord13(event.metadata) ? event.metadata : void 0;
9359
- const ui = metadata && isRecord13(metadata.ui) ? metadata.ui : void 0;
9360
- return ui?.presentation === "hidden";
9361
- }
9362
- function isEmptyTerminalAssistantRuntimeEvent(event) {
9363
- if (event.type !== "thought" && event.type !== "final_answer") {
9364
- return false;
9365
- }
9366
- return event.content.trim().length === 0;
9367
- }
9368
- function isConversationUiRuntimeEvent(event) {
9369
- return event.lane === "foreground" && event.visibility === "conversation";
9370
- }
9371
- function shouldPersistRuntimeEvent(event) {
9372
- return describeRuntimeEventLifecycle(event).persist;
9373
- }
9374
- function requirePersistableRuntimeEvent(event) {
9375
- if (!shouldPersistRuntimeEvent(event)) {
9376
- throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
9377
- }
9378
- return event;
9379
- }
9380
- function requirePersistableRoutedRuntimeEvent(event) {
9381
- requirePersistableRuntimeEvent(event);
9382
- return parseRoutedRuntimeEvent(event);
9383
- }
9384
- function shouldReplayRuntimeEventToUi(event) {
9385
- return describeRuntimeEventLifecycle(event).replayToUi;
9386
- }
9387
- function shouldEnterAgentContext(event) {
9388
- return describeRuntimeEventLifecycle(event).enterAgentContext;
9389
- }
9390
- function getRuntimeEventUiProjectionKind(event) {
9391
- if (isHiddenUserInputEvent(event)) {
9392
- return "hidden";
9393
- }
9394
- switch (event.type) {
9395
- case "user_input":
9396
- return "user_input";
9397
- case "thought":
9398
- return "thought";
9399
- case "final_answer":
9400
- return "final_answer";
9401
- case "final_answer_chunk":
9402
- return "final_answer_chunk";
9403
- case "final_answer_reset":
9404
- return "final_answer_reset";
9405
- case "tool_call_decision":
9406
- return "tool_call_decision";
9407
- case "tool_process":
9408
- return "tool_process";
9409
- case "tool_output":
9410
- return "tool_output";
9411
- case "requires_user_interaction":
9412
- return "requires_user_interaction";
9413
- case "audit_envelope":
9414
- return "audit_envelope";
9415
- case "subrun_trace":
9416
- return "subrun_trace";
9417
- case "error":
9418
- return "error";
9419
- case "context_usage_snapshot":
9420
- return "context_usage_snapshot";
9421
- case "run_execution_metrics":
9422
- return "run_execution_metrics";
9423
- case "history_summary":
9424
- return "history_summary";
9425
- default:
9426
- return "unsupported";
9427
- }
9428
- }
9429
- function describeRuntimeEventLifecycle(event) {
9430
- const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
9431
- const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
9432
- const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
9433
- const enterAgentContext = (() => {
9434
- if (uiProjectionKind === "unsupported") {
9435
- return false;
9436
- }
9437
- if (isSubRunTraceRuntimeEvent(event)) {
9438
- return false;
9439
- }
9440
- if (isRunExecutionMetricsRuntimeEvent(event)) {
9441
- return false;
9442
- }
9443
- if (isContextUsageSnapshotRuntimeEvent(event)) {
9444
- return false;
9445
- }
9446
- if (event.type === "error") {
9447
- return false;
9448
- }
9449
- if (isRequiresUserInteractionRuntimeEvent(event)) {
9450
- return false;
9451
- }
9452
- if (isAuditEnvelopeRuntimeEvent(event)) {
9453
- return false;
9454
- }
9455
- if (isControlRuntimeEvent(event)) {
9456
- return false;
9457
- }
9458
- if (isHiddenUserInputEvent(event)) {
9459
- return false;
9460
- }
9461
- if (isToolProcessEvent(event)) {
9462
- return false;
9463
- }
9464
- if (event.type === "thought") {
9465
- return false;
9466
- }
9467
- if (event.type === "final_answer_chunk") {
9468
- return false;
9469
- }
9470
- if (event.type === "final_answer_reset") {
9471
- return false;
9472
- }
9473
- if (isEmptyTerminalAssistantRuntimeEvent(event)) {
9474
- return false;
9475
- }
9476
- return true;
9477
- })();
9478
- const realtimeChannel = (() => {
9479
- switch (uiProjectionKind) {
9480
- case "thought":
9481
- case "final_answer":
9482
- case "final_answer_chunk":
9483
- case "final_answer_reset":
9484
- case "tool_call_decision":
9485
- case "tool_process":
9486
- case "tool_output":
9487
- case "requires_user_interaction":
9488
- case "subrun_trace":
9489
- case "error":
9490
- case "context_usage_snapshot":
9491
- case "run_execution_metrics":
9492
- case "history_summary":
9493
- return "event_bus_sse";
9494
- case "audit_envelope":
9495
- return "none";
9496
- default:
9497
- return "none";
9498
- }
9499
- })();
9500
- return {
9501
- uiProjectionKind,
9502
- persist,
9503
- replayToUi,
9504
- enterAgentContext,
9505
- realtimeChannel
9506
- };
9507
- }
9508
- function shouldEmitRuntimeEventToSse(event) {
9509
- return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
9510
- }
9511
-
9512
9508
  // src/runtime-kernel/events/finalAnswerCompletion.ts
9513
9509
  function findTerminalFinalAnswer(events) {
9514
9510
  for (let index = events.length - 1; index >= 0; index -= 1) {