@linnlabs/linnkit 0.37.1 → 0.37.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/dist/cli.cjs +238 -242
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +238 -242
  5. package/dist/cli.js.map +1 -1
  6. package/dist/context-manager.cjs +198 -200
  7. package/dist/context-manager.cjs.map +1 -1
  8. package/dist/context-manager.js +198 -200
  9. package/dist/context-manager.js.map +1 -1
  10. package/dist/contracts/agentSpec.d.cts +30 -30
  11. package/dist/contracts/agentSpec.d.ts +30 -30
  12. package/dist/contracts/audit.d.cts +32 -32
  13. package/dist/contracts/audit.d.ts +32 -32
  14. package/dist/contracts/contextPolicy.d.cts +36 -36
  15. package/dist/contracts/contextPolicy.d.ts +36 -36
  16. package/dist/contracts/events.d.cts +1113 -1113
  17. package/dist/contracts/events.d.ts +1113 -1113
  18. package/dist/contracts/execution.d.cts +8 -8
  19. package/dist/contracts/execution.d.ts +8 -8
  20. package/dist/contracts/messages.d.cts +1178 -1178
  21. package/dist/contracts/messages.d.ts +1178 -1178
  22. package/dist/contracts/provider-continuation.d.cts +28 -28
  23. package/dist/contracts/provider-continuation.d.ts +28 -28
  24. package/dist/contracts/sse.d.cts +897 -897
  25. package/dist/contracts/sse.d.ts +897 -897
  26. package/dist/contracts/sub-run-trace-payload.d.cts +26 -26
  27. package/dist/contracts/sub-run-trace-payload.d.ts +26 -26
  28. package/dist/contracts/token-accounting.d.cts +38 -38
  29. package/dist/contracts/token-accounting.d.ts +38 -38
  30. package/dist/index.cjs +238 -242
  31. package/dist/index.cjs.map +1 -1
  32. package/dist/index.js +238 -242
  33. package/dist/index.js.map +1 -1
  34. package/dist/quickstart.cjs +238 -242
  35. package/dist/quickstart.cjs.map +1 -1
  36. package/dist/quickstart.js +238 -242
  37. package/dist/quickstart.js.map +1 -1
  38. package/dist/runtime-kernel/events/agentEvents.d.cts +172 -172
  39. package/dist/runtime-kernel/events/agentEvents.d.ts +172 -172
  40. package/dist/runtime-kernel/events.cjs +1 -3
  41. package/dist/runtime-kernel/events.cjs.map +1 -1
  42. package/dist/runtime-kernel/events.js +1 -3
  43. package/dist/runtime-kernel/events.js.map +1 -1
  44. package/dist/runtime-kernel/graph-engine/executorContextBuilder.d.cts +1 -0
  45. package/dist/runtime-kernel/graph-engine/executorContextBuilder.d.ts +1 -0
  46. package/dist/runtime-kernel.cjs +238 -242
  47. package/dist/runtime-kernel.cjs.map +1 -1
  48. package/dist/runtime-kernel.js +238 -242
  49. package/dist/runtime-kernel.js.map +1 -1
  50. package/dist/testkit.cjs +238 -242
  51. package/dist/testkit.cjs.map +1 -1
  52. package/dist/testkit.js +238 -242
  53. package/dist/testkit.js.map +1 -1
  54. package/docs/integration/llm-provider.md +1 -0
  55. package/docs/integration/persistence.md +10 -0
  56. package/docs/integration/realtime.md +5 -0
  57. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -6030,10 +6030,7 @@ function createExecuteLlmStage(dependencies) {
6030
6030
  const promptBudget = ctx.promptBudget;
6031
6031
  const promptUsageMeasurementPolicy = ctx.promptUsageMeasurementPolicy;
6032
6032
  const streamEventHandler = ctx.input.stream && ctx.eventHandler ? (event) => {
6033
- const processedEvent = processStreamEvent(event, ctx.outputProcessor);
6034
- if (processedEvent) {
6035
- ctx.eventHandler?.(processedEvent);
6036
- }
6033
+ ctx.eventHandler?.(processStreamEvent(event, ctx.outputProcessor));
6037
6034
  } : void 0;
6038
6035
  const llmCallStartedAt = Date.now();
6039
6036
  let llmResp;
@@ -6189,9 +6186,6 @@ function processStreamEvent(event, outputProcessor) {
6189
6186
  return event;
6190
6187
  }
6191
6188
  const processedContent = outputProcessor.processStreamChunk(event.content);
6192
- if (processedContent.length === 0) {
6193
- return null;
6194
- }
6195
6189
  return {
6196
6190
  ...event,
6197
6191
  content: processedContent
@@ -7372,6 +7366,204 @@ var GraphAgentExecutor = class {
7372
7366
  }
7373
7367
  };
7374
7368
 
7369
+ // src/runtime-kernel/events/eventGovernance.ts
7370
+ var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
7371
+ "audit_envelope",
7372
+ "final_answer_chunk",
7373
+ "final_answer_reset",
7374
+ "subrun_trace",
7375
+ "context_usage_snapshot"
7376
+ ];
7377
+ function isRecord6(value) {
7378
+ return !!value && typeof value === "object" && !Array.isArray(value);
7379
+ }
7380
+ function isToolCallDecisionEvent(event) {
7381
+ return event.type === "tool_call_decision";
7382
+ }
7383
+ function isToolProcessEvent(event) {
7384
+ return event.type === "tool_process";
7385
+ }
7386
+ function isSubRunTraceRuntimeEvent(event) {
7387
+ return event.type === "subrun_trace";
7388
+ }
7389
+ function isRunExecutionMetricsRuntimeEvent(event) {
7390
+ return event.type === "run_execution_metrics";
7391
+ }
7392
+ function isContextUsageSnapshotRuntimeEvent(event) {
7393
+ return event.type === "context_usage_snapshot";
7394
+ }
7395
+ function isRequiresUserInteractionRuntimeEvent(event) {
7396
+ return event.type === "requires_user_interaction";
7397
+ }
7398
+ function isAuditEnvelopeRuntimeEvent(event) {
7399
+ return event.type === "audit_envelope";
7400
+ }
7401
+ function isControlRuntimeEvent(event) {
7402
+ return event.type === "control";
7403
+ }
7404
+ function isHiddenUserInputEvent(event) {
7405
+ if (event.type !== "user_input") {
7406
+ return false;
7407
+ }
7408
+ const metadata = isRecord6(event.metadata) ? event.metadata : void 0;
7409
+ const ui = metadata && isRecord6(metadata.ui) ? metadata.ui : void 0;
7410
+ return ui?.presentation === "hidden";
7411
+ }
7412
+ function isEmptyTerminalAssistantRuntimeEvent(event) {
7413
+ if (event.type !== "thought" && event.type !== "final_answer") {
7414
+ return false;
7415
+ }
7416
+ return event.content.trim().length === 0;
7417
+ }
7418
+ function isConversationUiRuntimeEvent(event) {
7419
+ return event.lane === "foreground" && event.visibility === "conversation";
7420
+ }
7421
+ function shouldPersistRuntimeEvent(event) {
7422
+ return describeRuntimeEventLifecycle(event).persist;
7423
+ }
7424
+ function requirePersistableRuntimeEvent(event) {
7425
+ if (!shouldPersistRuntimeEvent(event)) {
7426
+ throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
7427
+ }
7428
+ return event;
7429
+ }
7430
+ function requirePersistableRoutedRuntimeEvent(event) {
7431
+ requirePersistableRuntimeEvent(event);
7432
+ return parseRoutedRuntimeEvent(event);
7433
+ }
7434
+ function shouldReplayRuntimeEventToUi(event) {
7435
+ return describeRuntimeEventLifecycle(event).replayToUi;
7436
+ }
7437
+ function shouldEnterAgentContext(event) {
7438
+ return describeRuntimeEventLifecycle(event).enterAgentContext;
7439
+ }
7440
+ function getRuntimeEventUiProjectionKind(event) {
7441
+ if (isHiddenUserInputEvent(event)) {
7442
+ return "hidden";
7443
+ }
7444
+ switch (event.type) {
7445
+ case "user_input":
7446
+ return "user_input";
7447
+ case "thought":
7448
+ return "thought";
7449
+ case "final_answer":
7450
+ return "final_answer";
7451
+ case "final_answer_chunk":
7452
+ return "final_answer_chunk";
7453
+ case "final_answer_reset":
7454
+ return "final_answer_reset";
7455
+ case "tool_call_decision":
7456
+ return "tool_call_decision";
7457
+ case "tool_process":
7458
+ return "tool_process";
7459
+ case "tool_output":
7460
+ return "tool_output";
7461
+ case "requires_user_interaction":
7462
+ return "requires_user_interaction";
7463
+ case "audit_envelope":
7464
+ return "audit_envelope";
7465
+ case "subrun_trace":
7466
+ return "subrun_trace";
7467
+ case "error":
7468
+ return "error";
7469
+ case "context_usage_snapshot":
7470
+ return "context_usage_snapshot";
7471
+ case "run_execution_metrics":
7472
+ return "run_execution_metrics";
7473
+ case "history_summary":
7474
+ return "history_summary";
7475
+ default:
7476
+ return "unsupported";
7477
+ }
7478
+ }
7479
+ function describeRuntimeEventLifecycle(event) {
7480
+ const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
7481
+ const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
7482
+ const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
7483
+ const enterAgentContext = (() => {
7484
+ if (uiProjectionKind === "unsupported") {
7485
+ return false;
7486
+ }
7487
+ if (isSubRunTraceRuntimeEvent(event)) {
7488
+ return false;
7489
+ }
7490
+ if (isRunExecutionMetricsRuntimeEvent(event)) {
7491
+ return false;
7492
+ }
7493
+ if (isContextUsageSnapshotRuntimeEvent(event)) {
7494
+ return false;
7495
+ }
7496
+ if (event.type === "error") {
7497
+ return false;
7498
+ }
7499
+ if (isRequiresUserInteractionRuntimeEvent(event)) {
7500
+ return false;
7501
+ }
7502
+ if (isAuditEnvelopeRuntimeEvent(event)) {
7503
+ return false;
7504
+ }
7505
+ if (isControlRuntimeEvent(event)) {
7506
+ return false;
7507
+ }
7508
+ if (isHiddenUserInputEvent(event)) {
7509
+ return false;
7510
+ }
7511
+ if (isToolProcessEvent(event)) {
7512
+ return false;
7513
+ }
7514
+ if (event.type === "thought") {
7515
+ return false;
7516
+ }
7517
+ if (event.type === "final_answer_chunk") {
7518
+ return false;
7519
+ }
7520
+ if (event.type === "final_answer_reset") {
7521
+ return false;
7522
+ }
7523
+ if (isEmptyTerminalAssistantRuntimeEvent(event)) {
7524
+ return false;
7525
+ }
7526
+ return true;
7527
+ })();
7528
+ const realtimeChannel = (() => {
7529
+ switch (uiProjectionKind) {
7530
+ case "thought":
7531
+ case "final_answer":
7532
+ case "final_answer_chunk":
7533
+ case "final_answer_reset":
7534
+ case "tool_call_decision":
7535
+ case "tool_process":
7536
+ case "tool_output":
7537
+ case "requires_user_interaction":
7538
+ case "subrun_trace":
7539
+ case "error":
7540
+ case "context_usage_snapshot":
7541
+ case "run_execution_metrics":
7542
+ case "history_summary":
7543
+ return "event_bus_sse";
7544
+ case "audit_envelope":
7545
+ return "none";
7546
+ default:
7547
+ return "none";
7548
+ }
7549
+ })();
7550
+ return {
7551
+ uiProjectionKind,
7552
+ persist,
7553
+ replayToUi,
7554
+ enterAgentContext,
7555
+ realtimeChannel
7556
+ };
7557
+ }
7558
+ function shouldEmitRuntimeEventToSse(event) {
7559
+ return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
7560
+ }
7561
+
7562
+ // src/runtime-kernel/graph-engine/functions/appendWorkingHistory.ts
7563
+ function appendWorkingHistory(history, events) {
7564
+ return [...history.filter(shouldPersistRuntimeEvent), ...events.filter(shouldPersistRuntimeEvent)];
7565
+ }
7566
+
7375
7567
  // src/runtime-kernel/graph-engine/nodes/llmNode.state.ts
7376
7568
  function assertLlmNodeStreamChunkSequence(state, answerId, seq) {
7377
7569
  const answerChanged = state.answerId !== answerId;
@@ -7456,7 +7648,7 @@ function initLlmNodeState(init) {
7456
7648
  };
7457
7649
  }
7458
7650
  function buildLocalPatch(nodeState, ctx) {
7459
- const updatedHistory = [...ctx.history, ...nodeState.streamRuntimeEvents];
7651
+ const updatedHistory = appendWorkingHistory(ctx.history, nodeState.streamRuntimeEvents);
7460
7652
  const patch = {
7461
7653
  answerId: nodeState.answerId,
7462
7654
  chunkSeq: nodeState.chunkSeq,
@@ -7609,11 +7801,11 @@ var AgentEventSchema = z.discriminatedUnion("type", [
7609
7801
  });
7610
7802
 
7611
7803
  // src/runtime-kernel/events/provider-sidecar.ts
7612
- var isRecord6 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
7804
+ var isRecord7 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
7613
7805
  function readMetaFromEvent(event) {
7614
- if (!isRecord6(event)) return void 0;
7806
+ if (!isRecord7(event)) return void 0;
7615
7807
  const value = event["meta"];
7616
- return isRecord6(value) ? value : void 0;
7808
+ return isRecord7(value) ? value : void 0;
7617
7809
  }
7618
7810
 
7619
7811
  // src/runtime-kernel/events/agent-to-runtime.ts
@@ -7621,7 +7813,7 @@ function agentEventToRuntime(agentEvent, context, options = {}) {
7621
7813
  return mapAgentEventToRuntime(agentEvent, context, options);
7622
7814
  }
7623
7815
  function mapAgentEventToRuntime(agentEvent, context, options) {
7624
- if (isRecord6(agentEvent) && agentEvent["type"] === "history_summary") {
7816
+ if (isRecord7(agentEvent) && agentEvent["type"] === "history_summary") {
7625
7817
  const event = RuntimeEvent.parse(agentEvent);
7626
7818
  return context.metadata ? {
7627
7819
  ...event,
@@ -7762,9 +7954,7 @@ function mapToolProgressToRuntime(toolEvent, context, options, id, timestamp) {
7762
7954
  return event;
7763
7955
  }
7764
7956
  function mapStreamChunkToRuntime(streamEvent, context, id, timestamp) {
7765
- const text = streamEvent.content ?? "";
7766
- if (!text) return null;
7767
- const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, text, {
7957
+ const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, streamEvent.content, {
7768
7958
  timestamp,
7769
7959
  is_last: streamEvent.is_last === true
7770
7960
  });
@@ -8284,7 +8474,7 @@ var LlmNode = class {
8284
8474
  ];
8285
8475
  logger7.info("[LlmNode] \u5386\u53F2\u4E8B\u4EF6\u5DF2\u66F4\u65B0", {
8286
8476
  previousCount: history.length,
8287
- nextCount: parseRuntimeEvents(patch.history).length,
8477
+ nextCount: patch.history.length,
8288
8478
  streamedEventCount: nodeState.streamRuntimeEvents.length
8289
8479
  });
8290
8480
  logger7.info("[LlmNode] \u6267\u884C\u5668\u51B3\u7B56\u5B8C\u6210", {
@@ -8331,7 +8521,7 @@ var ToolModelInputResolutionError = class extends Error {
8331
8521
  // src/runtime-kernel/tools/model-input/functions/parseToolModelInputDeclaration.ts
8332
8522
  var DECLARATION_KEYS = /* @__PURE__ */ new Set(["attachments"]);
8333
8523
  var SELECTION_KEYS = /* @__PURE__ */ new Set(["id", "uri", "label"]);
8334
- function isRecord7(value) {
8524
+ function isRecord8(value) {
8335
8525
  return value !== null && typeof value === "object" && !Array.isArray(value);
8336
8526
  }
8337
8527
  function hasOnlyKeys(value, allowed) {
@@ -8344,7 +8534,7 @@ function readTrimmedString(value, maxLength) {
8344
8534
  return value === value.trim() ? value : void 0;
8345
8535
  }
8346
8536
  function parseSelection(value, index) {
8347
- if (!isRecord7(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
8537
+ if (!isRecord8(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
8348
8538
  return { ok: false, reason: `modelInput.attachments[${index}] \u5FC5\u987B\u662F strict selection\u3002` };
8349
8539
  }
8350
8540
  const id = readTrimmedString(value.id, 200);
@@ -8365,7 +8555,7 @@ function parseSelection(value, index) {
8365
8555
  return Object.freeze({ id, uri });
8366
8556
  }
8367
8557
  function parseToolModelInputDeclaration(value) {
8368
- if (!isRecord7(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
8558
+ if (!isRecord8(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
8369
8559
  return { ok: false, reason: "modelInput \u5FC5\u987B\u662F\u53EA\u5305\u542B attachments \u7684 strict \u5BF9\u8C61\u3002" };
8370
8560
  }
8371
8561
  if (!Array.isArray(value.attachments) || value.attachments.length === 0) {
@@ -8441,7 +8631,7 @@ function resolveFinalAnswerFromToolControl(finalAnswer) {
8441
8631
  }
8442
8632
 
8443
8633
  // src/runtime-kernel/graph-engine/nodes/toolNode.helpers.ts
8444
- function isRecord8(value) {
8634
+ function isRecord9(value) {
8445
8635
  return !!value && typeof value === "object" && !Array.isArray(value);
8446
8636
  }
8447
8637
  function readString3(value) {
@@ -8484,7 +8674,7 @@ function resolveToolObservationGovernancePolicy(policy) {
8484
8674
  }
8485
8675
  function readObservationPreviewMeta(parsed) {
8486
8676
  const meta = parsed["observationPreviewMeta"];
8487
- if (!isRecord8(meta)) {
8677
+ if (!isRecord9(meta)) {
8488
8678
  return void 0;
8489
8679
  }
8490
8680
  const filename = readString3(meta["filename"]);
@@ -8502,7 +8692,7 @@ function readObservationPreviewMeta(parsed) {
8502
8692
  };
8503
8693
  }
8504
8694
  async function applyObservationGovernance(params) {
8505
- if (!params.structuredObservation || !isRecord8(params.parsed)) {
8695
+ if (!params.structuredObservation || !isRecord9(params.parsed)) {
8506
8696
  throw new Error("Observation governance requires a validated structured tool result.");
8507
8697
  }
8508
8698
  const policy = resolveToolObservationGovernancePolicy(params.policy);
@@ -8594,7 +8784,7 @@ function createToolProtocolFuseError(nextCount, error) {
8594
8784
  // src/runtime-kernel/graph-engine/nodes/toolNode.stateTransitions.ts
8595
8785
  function mergeHistory(local, runtimeEvents) {
8596
8786
  const history = parseRuntimeEvents(local.history ?? []);
8597
- return [...history, ...runtimeEvents];
8787
+ return appendWorkingHistory(history, runtimeEvents);
8598
8788
  }
8599
8789
  function stripAnswerState(local) {
8600
8790
  const nextLocal = { ...local };
@@ -8603,7 +8793,7 @@ function stripAnswerState(local) {
8603
8793
  return nextLocal;
8604
8794
  }
8605
8795
  function validateStructuredToolResultContract(parsed) {
8606
- if (!isRecord8(parsed)) {
8796
+ if (!isRecord9(parsed)) {
8607
8797
  return { ok: false, reason: "\u5DE5\u5177\u6210\u529F\u7ED3\u679C\u5FC5\u987B\u662F JSON \u5BF9\u8C61\u3002" };
8608
8798
  }
8609
8799
  if (!Object.prototype.hasOwnProperty.call(parsed, "data") || parsed.data === void 0) {
@@ -8631,7 +8821,7 @@ function validateStructuredToolResultContract(parsed) {
8631
8821
  };
8632
8822
  }
8633
8823
  function extractToolControlInfo(parsed) {
8634
- if (!isRecord8(parsed) || !isRecord8(parsed.control)) {
8824
+ if (!isRecord9(parsed) || !isRecord9(parsed.control)) {
8635
8825
  return void 0;
8636
8826
  }
8637
8827
  const control = parsed.control;
@@ -8655,7 +8845,7 @@ function buildRequireUserLocalState(params) {
8655
8845
  ...params.local,
8656
8846
  pendingToolCalls: params.remainingCalls,
8657
8847
  pendingInteractionSpec: {
8658
- ...isRecord8(params.parsed) && isRecord8(params.parsed.control) ? params.parsed.control : {},
8848
+ ...isRecord9(params.parsed) && isRecord9(params.parsed.control) ? params.parsed.control : {},
8659
8849
  // wait_user 必须携带工具已校验的结构化结果,UI 才能使用正式 form identity。
8660
8850
  form: params.parsed,
8661
8851
  toolCallId: params.toolCallId,
@@ -8694,7 +8884,7 @@ function buildErrorLocalState(params) {
8694
8884
 
8695
8885
  // src/runtime-kernel/tools/argNormalizer.ts
8696
8886
  var logger8 = new Logger("ToolArgNormalizer");
8697
- function isRecord9(v) {
8887
+ function isRecord10(v) {
8698
8888
  return !!v && typeof v === "object" && !Array.isArray(v);
8699
8889
  }
8700
8890
  function tryParseJsonEncodedValue(v) {
@@ -8787,12 +8977,12 @@ function normalizeByPropertySchema(prop, v, options) {
8787
8977
  }
8788
8978
  case "object": {
8789
8979
  const parsedValue = tryParseJsonEncodedValue(v);
8790
- if (parsedValue !== v && isRecord9(parsedValue)) {
8980
+ if (parsedValue !== v && isRecord10(parsedValue)) {
8791
8981
  logJsonEncodedNormalization("object", options);
8792
8982
  }
8793
- if (!isRecord9(parsedValue)) return parsedValue;
8983
+ if (!isRecord10(parsedValue)) return parsedValue;
8794
8984
  const selectedSchema = selectDiscriminatedObjectSchema(prop, parsedValue);
8795
- if (!selectedSchema.properties || !isRecord9(selectedSchema.properties)) return parsedValue;
8985
+ if (!selectedSchema.properties || !isRecord10(selectedSchema.properties)) return parsedValue;
8796
8986
  return normalizeToolArgs(
8797
8987
  { properties: selectedSchema.properties },
8798
8988
  parsedValue,
@@ -8814,7 +9004,7 @@ function normalizeToolArgs(schema, args, options = {}) {
8814
9004
  }
8815
9005
  return next;
8816
9006
  }
8817
- function isRecord10(v) {
9007
+ function isRecord11(v) {
8818
9008
  return !!v && typeof v === "object" && !Array.isArray(v);
8819
9009
  }
8820
9010
  function stableStringify(v) {
@@ -8824,7 +9014,7 @@ function stableStringify(v) {
8824
9014
  if (t === "string") return JSON.stringify(v);
8825
9015
  if (t === "number" || t === "boolean") return String(v);
8826
9016
  if (Array.isArray(v)) return `[${v.map(stableStringify).join(",")}]`;
8827
- if (isRecord10(v)) {
9017
+ if (isRecord11(v)) {
8828
9018
  const keys = Object.keys(v).sort();
8829
9019
  const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(v[k])}`);
8830
9020
  return `{${parts.join(",")}}`;
@@ -8861,13 +9051,13 @@ function findCachedToolOutputByIdempotencyKey(params) {
8861
9051
  if (e.tool_name !== params.toolName) continue;
8862
9052
  if (e.status !== "success") continue;
8863
9053
  const meta = e.metadata;
8864
- if (!isRecord10(meta)) continue;
9054
+ if (!isRecord11(meta)) continue;
8865
9055
  const idem = meta["idempotency"];
8866
- if (!isRecord10(idem)) continue;
9056
+ if (!isRecord11(idem)) continue;
8867
9057
  if (idem["key"] !== params.idempotencyKey) continue;
8868
9058
  const structuredResult = { data: e.data, observation: e.observation };
8869
9059
  const presentation = meta?.presentation;
8870
- if (isRecord10(presentation) && presentation["media"] !== void 0) {
9060
+ if (isRecord11(presentation) && presentation["media"] !== void 0) {
8871
9061
  structuredResult["media"] = presentation["media"];
8872
9062
  }
8873
9063
  return {
@@ -9202,7 +9392,7 @@ function parseToolArgs(call) {
9202
9392
  }
9203
9393
  try {
9204
9394
  const parsed = JSON.parse(rawArguments);
9205
- if (!isRecord8(parsed)) {
9395
+ if (!isRecord9(parsed)) {
9206
9396
  return {
9207
9397
  ok: false,
9208
9398
  error: "Tool arguments must decode to a JSON object."
@@ -9220,13 +9410,12 @@ function parseToolArgs(call) {
9220
9410
  }
9221
9411
  function prepareToolNodeContext(state) {
9222
9412
  const local = state.local || {};
9223
- const toolContext = isRecord8(local.toolContext) ? local.toolContext : {};
9224
- if (!isRecord8(local.toolContext)) {
9413
+ const toolContext = isRecord9(local.toolContext) ? local.toolContext : {};
9414
+ if (!isRecord9(local.toolContext)) {
9225
9415
  local.toolContext = toolContext;
9226
9416
  }
9227
9417
  const conversationId = requireRuntimeIdentity(local.conversationId, "conversationId");
9228
9418
  const turnId = requireRuntimeIdentity(local.turnId, "turnId");
9229
- parseRuntimeEvents(local.history ?? []);
9230
9419
  const existingConversationId = typeof toolContext.conversationId === "string" ? toolContext.conversationId.trim() : "";
9231
9420
  ensureToolContextRuntimeCapability({
9232
9421
  context: toolContext,
@@ -9494,11 +9683,11 @@ function createPendingToolCancellationEvents(params) {
9494
9683
  return events;
9495
9684
  }
9496
9685
  function buildCancelledToolBatchLocalState(local, runtimeEvents) {
9497
- const history = Array.isArray(local.history) ? local.history : [];
9686
+ const history = parseRuntimeEvents(local.history ?? []);
9498
9687
  return {
9499
9688
  ...stripAnswerState(local),
9500
9689
  pendingToolCalls: [],
9501
- history: [...history, ...runtimeEvents]
9690
+ history: appendWorkingHistory(history, runtimeEvents)
9502
9691
  };
9503
9692
  }
9504
9693
 
@@ -9574,14 +9763,14 @@ function readHistoryEvents(local) {
9574
9763
  }
9575
9764
  function readLastSuccessfulLlmModelId(local) {
9576
9765
  const executorLocal = local.executorLocal;
9577
- if (!isRecord8(executorLocal)) {
9766
+ if (!isRecord9(executorLocal)) {
9578
9767
  return void 0;
9579
9768
  }
9580
9769
  const value = executorLocal.lastSuccessfulLlmModelId;
9581
9770
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
9582
9771
  }
9583
9772
  function formatCapabilityError(error) {
9584
- if (isRecord8(error)) {
9773
+ if (isRecord9(error)) {
9585
9774
  const codeValue = error.errorCode ?? error.code;
9586
9775
  const code = typeof codeValue === "string" ? codeValue.trim() : "";
9587
9776
  const message = typeof error.message === "string" ? error.message.trim() : "";
@@ -10196,16 +10385,16 @@ var ToolNode = class {
10196
10385
  };
10197
10386
 
10198
10387
  // src/runtime-kernel/graph-engine/nodes/userNode.ts
10199
- function isRecord11(value) {
10388
+ function isRecord12(value) {
10200
10389
  return typeof value === "object" && value !== null;
10201
10390
  }
10202
10391
  function isRuntimeEvent(value) {
10203
- return isRecord11(value) && typeof value.type === "string";
10392
+ return isRecord12(value) && typeof value.type === "string";
10204
10393
  }
10205
10394
  var UserNode = class {
10206
10395
  id = "user";
10207
10396
  async run(state) {
10208
- const local = isRecord11(state.local) ? state.local : {};
10397
+ const local = isRecord12(state.local) ? state.local : {};
10209
10398
  const newEvents = Array.isArray(local.newEvents) ? local.newEvents.filter(isRuntimeEvent) : [];
10210
10399
  const userInputEvent = newEvents.find((event) => event.type === "user_input");
10211
10400
  if (!userInputEvent) {
@@ -10216,7 +10405,7 @@ var UserNode = class {
10216
10405
  };
10217
10406
 
10218
10407
  // src/runtime-kernel/graph-engine/nodes/waitUserNode.ts
10219
- function isRecord12(value) {
10408
+ function isRecord13(value) {
10220
10409
  return !!value && typeof value === "object" && !Array.isArray(value);
10221
10410
  }
10222
10411
  function requireNonEmptyString(value, field) {
@@ -10233,10 +10422,10 @@ var WaitUserNode = class {
10233
10422
  }
10234
10423
  async run(state) {
10235
10424
  const local = state.local ?? {};
10236
- const spec = isRecord12(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
10425
+ const spec = isRecord13(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
10237
10426
  const conversationId = requireNonEmptyString(local.conversationId, "conversationId");
10238
10427
  const turnId = requireNonEmptyString(local.turnId, "turnId");
10239
- const toolContext = isRecord12(local.toolContext) ? local.toolContext : {};
10428
+ const toolContext = isRecord13(local.toolContext) ? local.toolContext : {};
10240
10429
  const runId = RunIdSchema.parse(requireNonEmptyString(toolContext.runId, "toolContext.runId"));
10241
10430
  const parentRunId = toolContext.parentRunId === void 0 ? void 0 : RunIdSchema.parse(toolContext.parentRunId);
10242
10431
  const toolCallId = ToolCallIdSchema.parse(
@@ -10391,199 +10580,6 @@ __export(events_exports, {
10391
10580
  shouldReplayRuntimeEventToUi: () => shouldReplayRuntimeEventToUi
10392
10581
  });
10393
10582
 
10394
- // src/runtime-kernel/events/eventGovernance.ts
10395
- var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
10396
- "audit_envelope",
10397
- "final_answer_chunk",
10398
- "final_answer_reset",
10399
- "subrun_trace",
10400
- "context_usage_snapshot"
10401
- ];
10402
- function isRecord13(value) {
10403
- return !!value && typeof value === "object" && !Array.isArray(value);
10404
- }
10405
- function isToolCallDecisionEvent(event) {
10406
- return event.type === "tool_call_decision";
10407
- }
10408
- function isToolProcessEvent(event) {
10409
- return event.type === "tool_process";
10410
- }
10411
- function isSubRunTraceRuntimeEvent(event) {
10412
- return event.type === "subrun_trace";
10413
- }
10414
- function isRunExecutionMetricsRuntimeEvent(event) {
10415
- return event.type === "run_execution_metrics";
10416
- }
10417
- function isContextUsageSnapshotRuntimeEvent(event) {
10418
- return event.type === "context_usage_snapshot";
10419
- }
10420
- function isRequiresUserInteractionRuntimeEvent(event) {
10421
- return event.type === "requires_user_interaction";
10422
- }
10423
- function isAuditEnvelopeRuntimeEvent(event) {
10424
- return event.type === "audit_envelope";
10425
- }
10426
- function isControlRuntimeEvent(event) {
10427
- return event.type === "control";
10428
- }
10429
- function isHiddenUserInputEvent(event) {
10430
- if (event.type !== "user_input") {
10431
- return false;
10432
- }
10433
- const metadata = isRecord13(event.metadata) ? event.metadata : void 0;
10434
- const ui = metadata && isRecord13(metadata.ui) ? metadata.ui : void 0;
10435
- return ui?.presentation === "hidden";
10436
- }
10437
- function isEmptyTerminalAssistantRuntimeEvent(event) {
10438
- if (event.type !== "thought" && event.type !== "final_answer") {
10439
- return false;
10440
- }
10441
- return event.content.trim().length === 0;
10442
- }
10443
- function isConversationUiRuntimeEvent(event) {
10444
- return event.lane === "foreground" && event.visibility === "conversation";
10445
- }
10446
- function shouldPersistRuntimeEvent(event) {
10447
- return describeRuntimeEventLifecycle(event).persist;
10448
- }
10449
- function requirePersistableRuntimeEvent(event) {
10450
- if (!shouldPersistRuntimeEvent(event)) {
10451
- throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
10452
- }
10453
- return event;
10454
- }
10455
- function requirePersistableRoutedRuntimeEvent(event) {
10456
- requirePersistableRuntimeEvent(event);
10457
- return parseRoutedRuntimeEvent(event);
10458
- }
10459
- function shouldReplayRuntimeEventToUi(event) {
10460
- return describeRuntimeEventLifecycle(event).replayToUi;
10461
- }
10462
- function shouldEnterAgentContext(event) {
10463
- return describeRuntimeEventLifecycle(event).enterAgentContext;
10464
- }
10465
- function getRuntimeEventUiProjectionKind(event) {
10466
- if (isHiddenUserInputEvent(event)) {
10467
- return "hidden";
10468
- }
10469
- switch (event.type) {
10470
- case "user_input":
10471
- return "user_input";
10472
- case "thought":
10473
- return "thought";
10474
- case "final_answer":
10475
- return "final_answer";
10476
- case "final_answer_chunk":
10477
- return "final_answer_chunk";
10478
- case "final_answer_reset":
10479
- return "final_answer_reset";
10480
- case "tool_call_decision":
10481
- return "tool_call_decision";
10482
- case "tool_process":
10483
- return "tool_process";
10484
- case "tool_output":
10485
- return "tool_output";
10486
- case "requires_user_interaction":
10487
- return "requires_user_interaction";
10488
- case "audit_envelope":
10489
- return "audit_envelope";
10490
- case "subrun_trace":
10491
- return "subrun_trace";
10492
- case "error":
10493
- return "error";
10494
- case "context_usage_snapshot":
10495
- return "context_usage_snapshot";
10496
- case "run_execution_metrics":
10497
- return "run_execution_metrics";
10498
- case "history_summary":
10499
- return "history_summary";
10500
- default:
10501
- return "unsupported";
10502
- }
10503
- }
10504
- function describeRuntimeEventLifecycle(event) {
10505
- const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
10506
- const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
10507
- const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
10508
- const enterAgentContext = (() => {
10509
- if (uiProjectionKind === "unsupported") {
10510
- return false;
10511
- }
10512
- if (isSubRunTraceRuntimeEvent(event)) {
10513
- return false;
10514
- }
10515
- if (isRunExecutionMetricsRuntimeEvent(event)) {
10516
- return false;
10517
- }
10518
- if (isContextUsageSnapshotRuntimeEvent(event)) {
10519
- return false;
10520
- }
10521
- if (event.type === "error") {
10522
- return false;
10523
- }
10524
- if (isRequiresUserInteractionRuntimeEvent(event)) {
10525
- return false;
10526
- }
10527
- if (isAuditEnvelopeRuntimeEvent(event)) {
10528
- return false;
10529
- }
10530
- if (isControlRuntimeEvent(event)) {
10531
- return false;
10532
- }
10533
- if (isHiddenUserInputEvent(event)) {
10534
- return false;
10535
- }
10536
- if (isToolProcessEvent(event)) {
10537
- return false;
10538
- }
10539
- if (event.type === "thought") {
10540
- return false;
10541
- }
10542
- if (event.type === "final_answer_chunk") {
10543
- return false;
10544
- }
10545
- if (event.type === "final_answer_reset") {
10546
- return false;
10547
- }
10548
- if (isEmptyTerminalAssistantRuntimeEvent(event)) {
10549
- return false;
10550
- }
10551
- return true;
10552
- })();
10553
- const realtimeChannel = (() => {
10554
- switch (uiProjectionKind) {
10555
- case "thought":
10556
- case "final_answer":
10557
- case "final_answer_chunk":
10558
- case "final_answer_reset":
10559
- case "tool_call_decision":
10560
- case "tool_process":
10561
- case "tool_output":
10562
- case "requires_user_interaction":
10563
- case "subrun_trace":
10564
- case "error":
10565
- case "context_usage_snapshot":
10566
- case "run_execution_metrics":
10567
- case "history_summary":
10568
- return "event_bus_sse";
10569
- case "audit_envelope":
10570
- return "none";
10571
- default:
10572
- return "none";
10573
- }
10574
- })();
10575
- return {
10576
- uiProjectionKind,
10577
- persist,
10578
- replayToUi,
10579
- enterAgentContext,
10580
- realtimeChannel
10581
- };
10582
- }
10583
- function shouldEmitRuntimeEventToSse(event) {
10584
- return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
10585
- }
10586
-
10587
10583
  // src/runtime-kernel/events/finalAnswerCompletion.ts
10588
10584
  function findTerminalFinalAnswer(events) {
10589
10585
  for (let index = events.length - 1; index >= 0; index -= 1) {