@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.cjs CHANGED
@@ -6032,10 +6032,7 @@ function createExecuteLlmStage(dependencies) {
6032
6032
  const promptBudget = ctx.promptBudget;
6033
6033
  const promptUsageMeasurementPolicy = ctx.promptUsageMeasurementPolicy;
6034
6034
  const streamEventHandler = ctx.input.stream && ctx.eventHandler ? (event) => {
6035
- const processedEvent = processStreamEvent(event, ctx.outputProcessor);
6036
- if (processedEvent) {
6037
- ctx.eventHandler?.(processedEvent);
6038
- }
6035
+ ctx.eventHandler?.(processStreamEvent(event, ctx.outputProcessor));
6039
6036
  } : void 0;
6040
6037
  const llmCallStartedAt = Date.now();
6041
6038
  let llmResp;
@@ -6191,9 +6188,6 @@ function processStreamEvent(event, outputProcessor) {
6191
6188
  return event;
6192
6189
  }
6193
6190
  const processedContent = outputProcessor.processStreamChunk(event.content);
6194
- if (processedContent.length === 0) {
6195
- return null;
6196
- }
6197
6191
  return {
6198
6192
  ...event,
6199
6193
  content: processedContent
@@ -7374,6 +7368,204 @@ var GraphAgentExecutor = class {
7374
7368
  }
7375
7369
  };
7376
7370
 
7371
+ // src/runtime-kernel/events/eventGovernance.ts
7372
+ var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
7373
+ "audit_envelope",
7374
+ "final_answer_chunk",
7375
+ "final_answer_reset",
7376
+ "subrun_trace",
7377
+ "context_usage_snapshot"
7378
+ ];
7379
+ function isRecord6(value) {
7380
+ return !!value && typeof value === "object" && !Array.isArray(value);
7381
+ }
7382
+ function isToolCallDecisionEvent(event) {
7383
+ return event.type === "tool_call_decision";
7384
+ }
7385
+ function isToolProcessEvent(event) {
7386
+ return event.type === "tool_process";
7387
+ }
7388
+ function isSubRunTraceRuntimeEvent(event) {
7389
+ return event.type === "subrun_trace";
7390
+ }
7391
+ function isRunExecutionMetricsRuntimeEvent(event) {
7392
+ return event.type === "run_execution_metrics";
7393
+ }
7394
+ function isContextUsageSnapshotRuntimeEvent(event) {
7395
+ return event.type === "context_usage_snapshot";
7396
+ }
7397
+ function isRequiresUserInteractionRuntimeEvent(event) {
7398
+ return event.type === "requires_user_interaction";
7399
+ }
7400
+ function isAuditEnvelopeRuntimeEvent(event) {
7401
+ return event.type === "audit_envelope";
7402
+ }
7403
+ function isControlRuntimeEvent(event) {
7404
+ return event.type === "control";
7405
+ }
7406
+ function isHiddenUserInputEvent(event) {
7407
+ if (event.type !== "user_input") {
7408
+ return false;
7409
+ }
7410
+ const metadata = isRecord6(event.metadata) ? event.metadata : void 0;
7411
+ const ui = metadata && isRecord6(metadata.ui) ? metadata.ui : void 0;
7412
+ return ui?.presentation === "hidden";
7413
+ }
7414
+ function isEmptyTerminalAssistantRuntimeEvent(event) {
7415
+ if (event.type !== "thought" && event.type !== "final_answer") {
7416
+ return false;
7417
+ }
7418
+ return event.content.trim().length === 0;
7419
+ }
7420
+ function isConversationUiRuntimeEvent(event) {
7421
+ return event.lane === "foreground" && event.visibility === "conversation";
7422
+ }
7423
+ function shouldPersistRuntimeEvent(event) {
7424
+ return describeRuntimeEventLifecycle(event).persist;
7425
+ }
7426
+ function requirePersistableRuntimeEvent(event) {
7427
+ if (!shouldPersistRuntimeEvent(event)) {
7428
+ throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
7429
+ }
7430
+ return event;
7431
+ }
7432
+ function requirePersistableRoutedRuntimeEvent(event) {
7433
+ requirePersistableRuntimeEvent(event);
7434
+ return parseRoutedRuntimeEvent(event);
7435
+ }
7436
+ function shouldReplayRuntimeEventToUi(event) {
7437
+ return describeRuntimeEventLifecycle(event).replayToUi;
7438
+ }
7439
+ function shouldEnterAgentContext(event) {
7440
+ return describeRuntimeEventLifecycle(event).enterAgentContext;
7441
+ }
7442
+ function getRuntimeEventUiProjectionKind(event) {
7443
+ if (isHiddenUserInputEvent(event)) {
7444
+ return "hidden";
7445
+ }
7446
+ switch (event.type) {
7447
+ case "user_input":
7448
+ return "user_input";
7449
+ case "thought":
7450
+ return "thought";
7451
+ case "final_answer":
7452
+ return "final_answer";
7453
+ case "final_answer_chunk":
7454
+ return "final_answer_chunk";
7455
+ case "final_answer_reset":
7456
+ return "final_answer_reset";
7457
+ case "tool_call_decision":
7458
+ return "tool_call_decision";
7459
+ case "tool_process":
7460
+ return "tool_process";
7461
+ case "tool_output":
7462
+ return "tool_output";
7463
+ case "requires_user_interaction":
7464
+ return "requires_user_interaction";
7465
+ case "audit_envelope":
7466
+ return "audit_envelope";
7467
+ case "subrun_trace":
7468
+ return "subrun_trace";
7469
+ case "error":
7470
+ return "error";
7471
+ case "context_usage_snapshot":
7472
+ return "context_usage_snapshot";
7473
+ case "run_execution_metrics":
7474
+ return "run_execution_metrics";
7475
+ case "history_summary":
7476
+ return "history_summary";
7477
+ default:
7478
+ return "unsupported";
7479
+ }
7480
+ }
7481
+ function describeRuntimeEventLifecycle(event) {
7482
+ const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
7483
+ const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
7484
+ const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
7485
+ const enterAgentContext = (() => {
7486
+ if (uiProjectionKind === "unsupported") {
7487
+ return false;
7488
+ }
7489
+ if (isSubRunTraceRuntimeEvent(event)) {
7490
+ return false;
7491
+ }
7492
+ if (isRunExecutionMetricsRuntimeEvent(event)) {
7493
+ return false;
7494
+ }
7495
+ if (isContextUsageSnapshotRuntimeEvent(event)) {
7496
+ return false;
7497
+ }
7498
+ if (event.type === "error") {
7499
+ return false;
7500
+ }
7501
+ if (isRequiresUserInteractionRuntimeEvent(event)) {
7502
+ return false;
7503
+ }
7504
+ if (isAuditEnvelopeRuntimeEvent(event)) {
7505
+ return false;
7506
+ }
7507
+ if (isControlRuntimeEvent(event)) {
7508
+ return false;
7509
+ }
7510
+ if (isHiddenUserInputEvent(event)) {
7511
+ return false;
7512
+ }
7513
+ if (isToolProcessEvent(event)) {
7514
+ return false;
7515
+ }
7516
+ if (event.type === "thought") {
7517
+ return false;
7518
+ }
7519
+ if (event.type === "final_answer_chunk") {
7520
+ return false;
7521
+ }
7522
+ if (event.type === "final_answer_reset") {
7523
+ return false;
7524
+ }
7525
+ if (isEmptyTerminalAssistantRuntimeEvent(event)) {
7526
+ return false;
7527
+ }
7528
+ return true;
7529
+ })();
7530
+ const realtimeChannel = (() => {
7531
+ switch (uiProjectionKind) {
7532
+ case "thought":
7533
+ case "final_answer":
7534
+ case "final_answer_chunk":
7535
+ case "final_answer_reset":
7536
+ case "tool_call_decision":
7537
+ case "tool_process":
7538
+ case "tool_output":
7539
+ case "requires_user_interaction":
7540
+ case "subrun_trace":
7541
+ case "error":
7542
+ case "context_usage_snapshot":
7543
+ case "run_execution_metrics":
7544
+ case "history_summary":
7545
+ return "event_bus_sse";
7546
+ case "audit_envelope":
7547
+ return "none";
7548
+ default:
7549
+ return "none";
7550
+ }
7551
+ })();
7552
+ return {
7553
+ uiProjectionKind,
7554
+ persist,
7555
+ replayToUi,
7556
+ enterAgentContext,
7557
+ realtimeChannel
7558
+ };
7559
+ }
7560
+ function shouldEmitRuntimeEventToSse(event) {
7561
+ return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
7562
+ }
7563
+
7564
+ // src/runtime-kernel/graph-engine/functions/appendWorkingHistory.ts
7565
+ function appendWorkingHistory(history, events) {
7566
+ return [...history.filter(shouldPersistRuntimeEvent), ...events.filter(shouldPersistRuntimeEvent)];
7567
+ }
7568
+
7377
7569
  // src/runtime-kernel/graph-engine/nodes/llmNode.state.ts
7378
7570
  function assertLlmNodeStreamChunkSequence(state, answerId, seq) {
7379
7571
  const answerChanged = state.answerId !== answerId;
@@ -7458,7 +7650,7 @@ function initLlmNodeState(init) {
7458
7650
  };
7459
7651
  }
7460
7652
  function buildLocalPatch(nodeState, ctx) {
7461
- const updatedHistory = [...ctx.history, ...nodeState.streamRuntimeEvents];
7653
+ const updatedHistory = appendWorkingHistory(ctx.history, nodeState.streamRuntimeEvents);
7462
7654
  const patch = {
7463
7655
  answerId: nodeState.answerId,
7464
7656
  chunkSeq: nodeState.chunkSeq,
@@ -7611,11 +7803,11 @@ var AgentEventSchema = zod.z.discriminatedUnion("type", [
7611
7803
  });
7612
7804
 
7613
7805
  // src/runtime-kernel/events/provider-sidecar.ts
7614
- var isRecord6 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
7806
+ var isRecord7 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
7615
7807
  function readMetaFromEvent(event) {
7616
- if (!isRecord6(event)) return void 0;
7808
+ if (!isRecord7(event)) return void 0;
7617
7809
  const value = event["meta"];
7618
- return isRecord6(value) ? value : void 0;
7810
+ return isRecord7(value) ? value : void 0;
7619
7811
  }
7620
7812
 
7621
7813
  // src/runtime-kernel/events/agent-to-runtime.ts
@@ -7623,7 +7815,7 @@ function agentEventToRuntime(agentEvent, context, options = {}) {
7623
7815
  return mapAgentEventToRuntime(agentEvent, context, options);
7624
7816
  }
7625
7817
  function mapAgentEventToRuntime(agentEvent, context, options) {
7626
- if (isRecord6(agentEvent) && agentEvent["type"] === "history_summary") {
7818
+ if (isRecord7(agentEvent) && agentEvent["type"] === "history_summary") {
7627
7819
  const event = RuntimeEvent.parse(agentEvent);
7628
7820
  return context.metadata ? {
7629
7821
  ...event,
@@ -7764,9 +7956,7 @@ function mapToolProgressToRuntime(toolEvent, context, options, id, timestamp) {
7764
7956
  return event;
7765
7957
  }
7766
7958
  function mapStreamChunkToRuntime(streamEvent, context, id, timestamp) {
7767
- const text = streamEvent.content ?? "";
7768
- if (!text) return null;
7769
- const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, text, {
7959
+ const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, streamEvent.content, {
7770
7960
  timestamp,
7771
7961
  is_last: streamEvent.is_last === true
7772
7962
  });
@@ -8286,7 +8476,7 @@ var LlmNode = class {
8286
8476
  ];
8287
8477
  logger7.info("[LlmNode] \u5386\u53F2\u4E8B\u4EF6\u5DF2\u66F4\u65B0", {
8288
8478
  previousCount: history.length,
8289
- nextCount: parseRuntimeEvents(patch.history).length,
8479
+ nextCount: patch.history.length,
8290
8480
  streamedEventCount: nodeState.streamRuntimeEvents.length
8291
8481
  });
8292
8482
  logger7.info("[LlmNode] \u6267\u884C\u5668\u51B3\u7B56\u5B8C\u6210", {
@@ -8333,7 +8523,7 @@ var ToolModelInputResolutionError = class extends Error {
8333
8523
  // src/runtime-kernel/tools/model-input/functions/parseToolModelInputDeclaration.ts
8334
8524
  var DECLARATION_KEYS = /* @__PURE__ */ new Set(["attachments"]);
8335
8525
  var SELECTION_KEYS = /* @__PURE__ */ new Set(["id", "uri", "label"]);
8336
- function isRecord7(value) {
8526
+ function isRecord8(value) {
8337
8527
  return value !== null && typeof value === "object" && !Array.isArray(value);
8338
8528
  }
8339
8529
  function hasOnlyKeys(value, allowed) {
@@ -8346,7 +8536,7 @@ function readTrimmedString(value, maxLength) {
8346
8536
  return value === value.trim() ? value : void 0;
8347
8537
  }
8348
8538
  function parseSelection(value, index) {
8349
- if (!isRecord7(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
8539
+ if (!isRecord8(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
8350
8540
  return { ok: false, reason: `modelInput.attachments[${index}] \u5FC5\u987B\u662F strict selection\u3002` };
8351
8541
  }
8352
8542
  const id = readTrimmedString(value.id, 200);
@@ -8367,7 +8557,7 @@ function parseSelection(value, index) {
8367
8557
  return Object.freeze({ id, uri });
8368
8558
  }
8369
8559
  function parseToolModelInputDeclaration(value) {
8370
- if (!isRecord7(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
8560
+ if (!isRecord8(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
8371
8561
  return { ok: false, reason: "modelInput \u5FC5\u987B\u662F\u53EA\u5305\u542B attachments \u7684 strict \u5BF9\u8C61\u3002" };
8372
8562
  }
8373
8563
  if (!Array.isArray(value.attachments) || value.attachments.length === 0) {
@@ -8443,7 +8633,7 @@ function resolveFinalAnswerFromToolControl(finalAnswer) {
8443
8633
  }
8444
8634
 
8445
8635
  // src/runtime-kernel/graph-engine/nodes/toolNode.helpers.ts
8446
- function isRecord8(value) {
8636
+ function isRecord9(value) {
8447
8637
  return !!value && typeof value === "object" && !Array.isArray(value);
8448
8638
  }
8449
8639
  function readString3(value) {
@@ -8486,7 +8676,7 @@ function resolveToolObservationGovernancePolicy(policy) {
8486
8676
  }
8487
8677
  function readObservationPreviewMeta(parsed) {
8488
8678
  const meta = parsed["observationPreviewMeta"];
8489
- if (!isRecord8(meta)) {
8679
+ if (!isRecord9(meta)) {
8490
8680
  return void 0;
8491
8681
  }
8492
8682
  const filename = readString3(meta["filename"]);
@@ -8504,7 +8694,7 @@ function readObservationPreviewMeta(parsed) {
8504
8694
  };
8505
8695
  }
8506
8696
  async function applyObservationGovernance(params) {
8507
- if (!params.structuredObservation || !isRecord8(params.parsed)) {
8697
+ if (!params.structuredObservation || !isRecord9(params.parsed)) {
8508
8698
  throw new Error("Observation governance requires a validated structured tool result.");
8509
8699
  }
8510
8700
  const policy = resolveToolObservationGovernancePolicy(params.policy);
@@ -8596,7 +8786,7 @@ function createToolProtocolFuseError(nextCount, error) {
8596
8786
  // src/runtime-kernel/graph-engine/nodes/toolNode.stateTransitions.ts
8597
8787
  function mergeHistory(local, runtimeEvents) {
8598
8788
  const history = parseRuntimeEvents(local.history ?? []);
8599
- return [...history, ...runtimeEvents];
8789
+ return appendWorkingHistory(history, runtimeEvents);
8600
8790
  }
8601
8791
  function stripAnswerState(local) {
8602
8792
  const nextLocal = { ...local };
@@ -8605,7 +8795,7 @@ function stripAnswerState(local) {
8605
8795
  return nextLocal;
8606
8796
  }
8607
8797
  function validateStructuredToolResultContract(parsed) {
8608
- if (!isRecord8(parsed)) {
8798
+ if (!isRecord9(parsed)) {
8609
8799
  return { ok: false, reason: "\u5DE5\u5177\u6210\u529F\u7ED3\u679C\u5FC5\u987B\u662F JSON \u5BF9\u8C61\u3002" };
8610
8800
  }
8611
8801
  if (!Object.prototype.hasOwnProperty.call(parsed, "data") || parsed.data === void 0) {
@@ -8633,7 +8823,7 @@ function validateStructuredToolResultContract(parsed) {
8633
8823
  };
8634
8824
  }
8635
8825
  function extractToolControlInfo(parsed) {
8636
- if (!isRecord8(parsed) || !isRecord8(parsed.control)) {
8826
+ if (!isRecord9(parsed) || !isRecord9(parsed.control)) {
8637
8827
  return void 0;
8638
8828
  }
8639
8829
  const control = parsed.control;
@@ -8657,7 +8847,7 @@ function buildRequireUserLocalState(params) {
8657
8847
  ...params.local,
8658
8848
  pendingToolCalls: params.remainingCalls,
8659
8849
  pendingInteractionSpec: {
8660
- ...isRecord8(params.parsed) && isRecord8(params.parsed.control) ? params.parsed.control : {},
8850
+ ...isRecord9(params.parsed) && isRecord9(params.parsed.control) ? params.parsed.control : {},
8661
8851
  // wait_user 必须携带工具已校验的结构化结果,UI 才能使用正式 form identity。
8662
8852
  form: params.parsed,
8663
8853
  toolCallId: params.toolCallId,
@@ -8696,7 +8886,7 @@ function buildErrorLocalState(params) {
8696
8886
 
8697
8887
  // src/runtime-kernel/tools/argNormalizer.ts
8698
8888
  var logger8 = new Logger("ToolArgNormalizer");
8699
- function isRecord9(v) {
8889
+ function isRecord10(v) {
8700
8890
  return !!v && typeof v === "object" && !Array.isArray(v);
8701
8891
  }
8702
8892
  function tryParseJsonEncodedValue(v) {
@@ -8789,12 +8979,12 @@ function normalizeByPropertySchema(prop, v, options) {
8789
8979
  }
8790
8980
  case "object": {
8791
8981
  const parsedValue = tryParseJsonEncodedValue(v);
8792
- if (parsedValue !== v && isRecord9(parsedValue)) {
8982
+ if (parsedValue !== v && isRecord10(parsedValue)) {
8793
8983
  logJsonEncodedNormalization("object", options);
8794
8984
  }
8795
- if (!isRecord9(parsedValue)) return parsedValue;
8985
+ if (!isRecord10(parsedValue)) return parsedValue;
8796
8986
  const selectedSchema = selectDiscriminatedObjectSchema(prop, parsedValue);
8797
- if (!selectedSchema.properties || !isRecord9(selectedSchema.properties)) return parsedValue;
8987
+ if (!selectedSchema.properties || !isRecord10(selectedSchema.properties)) return parsedValue;
8798
8988
  return normalizeToolArgs(
8799
8989
  { properties: selectedSchema.properties },
8800
8990
  parsedValue,
@@ -8816,7 +9006,7 @@ function normalizeToolArgs(schema, args, options = {}) {
8816
9006
  }
8817
9007
  return next;
8818
9008
  }
8819
- function isRecord10(v) {
9009
+ function isRecord11(v) {
8820
9010
  return !!v && typeof v === "object" && !Array.isArray(v);
8821
9011
  }
8822
9012
  function stableStringify(v) {
@@ -8826,7 +9016,7 @@ function stableStringify(v) {
8826
9016
  if (t === "string") return JSON.stringify(v);
8827
9017
  if (t === "number" || t === "boolean") return String(v);
8828
9018
  if (Array.isArray(v)) return `[${v.map(stableStringify).join(",")}]`;
8829
- if (isRecord10(v)) {
9019
+ if (isRecord11(v)) {
8830
9020
  const keys = Object.keys(v).sort();
8831
9021
  const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(v[k])}`);
8832
9022
  return `{${parts.join(",")}}`;
@@ -8863,13 +9053,13 @@ function findCachedToolOutputByIdempotencyKey(params) {
8863
9053
  if (e.tool_name !== params.toolName) continue;
8864
9054
  if (e.status !== "success") continue;
8865
9055
  const meta = e.metadata;
8866
- if (!isRecord10(meta)) continue;
9056
+ if (!isRecord11(meta)) continue;
8867
9057
  const idem = meta["idempotency"];
8868
- if (!isRecord10(idem)) continue;
9058
+ if (!isRecord11(idem)) continue;
8869
9059
  if (idem["key"] !== params.idempotencyKey) continue;
8870
9060
  const structuredResult = { data: e.data, observation: e.observation };
8871
9061
  const presentation = meta?.presentation;
8872
- if (isRecord10(presentation) && presentation["media"] !== void 0) {
9062
+ if (isRecord11(presentation) && presentation["media"] !== void 0) {
8873
9063
  structuredResult["media"] = presentation["media"];
8874
9064
  }
8875
9065
  return {
@@ -9204,7 +9394,7 @@ function parseToolArgs(call) {
9204
9394
  }
9205
9395
  try {
9206
9396
  const parsed = JSON.parse(rawArguments);
9207
- if (!isRecord8(parsed)) {
9397
+ if (!isRecord9(parsed)) {
9208
9398
  return {
9209
9399
  ok: false,
9210
9400
  error: "Tool arguments must decode to a JSON object."
@@ -9222,13 +9412,12 @@ function parseToolArgs(call) {
9222
9412
  }
9223
9413
  function prepareToolNodeContext(state) {
9224
9414
  const local = state.local || {};
9225
- const toolContext = isRecord8(local.toolContext) ? local.toolContext : {};
9226
- if (!isRecord8(local.toolContext)) {
9415
+ const toolContext = isRecord9(local.toolContext) ? local.toolContext : {};
9416
+ if (!isRecord9(local.toolContext)) {
9227
9417
  local.toolContext = toolContext;
9228
9418
  }
9229
9419
  const conversationId = requireRuntimeIdentity(local.conversationId, "conversationId");
9230
9420
  const turnId = requireRuntimeIdentity(local.turnId, "turnId");
9231
- parseRuntimeEvents(local.history ?? []);
9232
9421
  const existingConversationId = typeof toolContext.conversationId === "string" ? toolContext.conversationId.trim() : "";
9233
9422
  ensureToolContextRuntimeCapability({
9234
9423
  context: toolContext,
@@ -9496,11 +9685,11 @@ function createPendingToolCancellationEvents(params) {
9496
9685
  return events;
9497
9686
  }
9498
9687
  function buildCancelledToolBatchLocalState(local, runtimeEvents) {
9499
- const history = Array.isArray(local.history) ? local.history : [];
9688
+ const history = parseRuntimeEvents(local.history ?? []);
9500
9689
  return {
9501
9690
  ...stripAnswerState(local),
9502
9691
  pendingToolCalls: [],
9503
- history: [...history, ...runtimeEvents]
9692
+ history: appendWorkingHistory(history, runtimeEvents)
9504
9693
  };
9505
9694
  }
9506
9695
 
@@ -9576,14 +9765,14 @@ function readHistoryEvents(local) {
9576
9765
  }
9577
9766
  function readLastSuccessfulLlmModelId(local) {
9578
9767
  const executorLocal = local.executorLocal;
9579
- if (!isRecord8(executorLocal)) {
9768
+ if (!isRecord9(executorLocal)) {
9580
9769
  return void 0;
9581
9770
  }
9582
9771
  const value = executorLocal.lastSuccessfulLlmModelId;
9583
9772
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
9584
9773
  }
9585
9774
  function formatCapabilityError(error) {
9586
- if (isRecord8(error)) {
9775
+ if (isRecord9(error)) {
9587
9776
  const codeValue = error.errorCode ?? error.code;
9588
9777
  const code = typeof codeValue === "string" ? codeValue.trim() : "";
9589
9778
  const message = typeof error.message === "string" ? error.message.trim() : "";
@@ -10198,16 +10387,16 @@ var ToolNode = class {
10198
10387
  };
10199
10388
 
10200
10389
  // src/runtime-kernel/graph-engine/nodes/userNode.ts
10201
- function isRecord11(value) {
10390
+ function isRecord12(value) {
10202
10391
  return typeof value === "object" && value !== null;
10203
10392
  }
10204
10393
  function isRuntimeEvent(value) {
10205
- return isRecord11(value) && typeof value.type === "string";
10394
+ return isRecord12(value) && typeof value.type === "string";
10206
10395
  }
10207
10396
  var UserNode = class {
10208
10397
  id = "user";
10209
10398
  async run(state) {
10210
- const local = isRecord11(state.local) ? state.local : {};
10399
+ const local = isRecord12(state.local) ? state.local : {};
10211
10400
  const newEvents = Array.isArray(local.newEvents) ? local.newEvents.filter(isRuntimeEvent) : [];
10212
10401
  const userInputEvent = newEvents.find((event) => event.type === "user_input");
10213
10402
  if (!userInputEvent) {
@@ -10218,7 +10407,7 @@ var UserNode = class {
10218
10407
  };
10219
10408
 
10220
10409
  // src/runtime-kernel/graph-engine/nodes/waitUserNode.ts
10221
- function isRecord12(value) {
10410
+ function isRecord13(value) {
10222
10411
  return !!value && typeof value === "object" && !Array.isArray(value);
10223
10412
  }
10224
10413
  function requireNonEmptyString(value, field) {
@@ -10235,10 +10424,10 @@ var WaitUserNode = class {
10235
10424
  }
10236
10425
  async run(state) {
10237
10426
  const local = state.local ?? {};
10238
- const spec = isRecord12(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
10427
+ const spec = isRecord13(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
10239
10428
  const conversationId = requireNonEmptyString(local.conversationId, "conversationId");
10240
10429
  const turnId = requireNonEmptyString(local.turnId, "turnId");
10241
- const toolContext = isRecord12(local.toolContext) ? local.toolContext : {};
10430
+ const toolContext = isRecord13(local.toolContext) ? local.toolContext : {};
10242
10431
  const runId = RunIdSchema.parse(requireNonEmptyString(toolContext.runId, "toolContext.runId"));
10243
10432
  const parentRunId = toolContext.parentRunId === void 0 ? void 0 : RunIdSchema.parse(toolContext.parentRunId);
10244
10433
  const toolCallId = ToolCallIdSchema.parse(
@@ -10393,199 +10582,6 @@ __export(events_exports, {
10393
10582
  shouldReplayRuntimeEventToUi: () => shouldReplayRuntimeEventToUi
10394
10583
  });
10395
10584
 
10396
- // src/runtime-kernel/events/eventGovernance.ts
10397
- var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
10398
- "audit_envelope",
10399
- "final_answer_chunk",
10400
- "final_answer_reset",
10401
- "subrun_trace",
10402
- "context_usage_snapshot"
10403
- ];
10404
- function isRecord13(value) {
10405
- return !!value && typeof value === "object" && !Array.isArray(value);
10406
- }
10407
- function isToolCallDecisionEvent(event) {
10408
- return event.type === "tool_call_decision";
10409
- }
10410
- function isToolProcessEvent(event) {
10411
- return event.type === "tool_process";
10412
- }
10413
- function isSubRunTraceRuntimeEvent(event) {
10414
- return event.type === "subrun_trace";
10415
- }
10416
- function isRunExecutionMetricsRuntimeEvent(event) {
10417
- return event.type === "run_execution_metrics";
10418
- }
10419
- function isContextUsageSnapshotRuntimeEvent(event) {
10420
- return event.type === "context_usage_snapshot";
10421
- }
10422
- function isRequiresUserInteractionRuntimeEvent(event) {
10423
- return event.type === "requires_user_interaction";
10424
- }
10425
- function isAuditEnvelopeRuntimeEvent(event) {
10426
- return event.type === "audit_envelope";
10427
- }
10428
- function isControlRuntimeEvent(event) {
10429
- return event.type === "control";
10430
- }
10431
- function isHiddenUserInputEvent(event) {
10432
- if (event.type !== "user_input") {
10433
- return false;
10434
- }
10435
- const metadata = isRecord13(event.metadata) ? event.metadata : void 0;
10436
- const ui = metadata && isRecord13(metadata.ui) ? metadata.ui : void 0;
10437
- return ui?.presentation === "hidden";
10438
- }
10439
- function isEmptyTerminalAssistantRuntimeEvent(event) {
10440
- if (event.type !== "thought" && event.type !== "final_answer") {
10441
- return false;
10442
- }
10443
- return event.content.trim().length === 0;
10444
- }
10445
- function isConversationUiRuntimeEvent(event) {
10446
- return event.lane === "foreground" && event.visibility === "conversation";
10447
- }
10448
- function shouldPersistRuntimeEvent(event) {
10449
- return describeRuntimeEventLifecycle(event).persist;
10450
- }
10451
- function requirePersistableRuntimeEvent(event) {
10452
- if (!shouldPersistRuntimeEvent(event)) {
10453
- throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
10454
- }
10455
- return event;
10456
- }
10457
- function requirePersistableRoutedRuntimeEvent(event) {
10458
- requirePersistableRuntimeEvent(event);
10459
- return parseRoutedRuntimeEvent(event);
10460
- }
10461
- function shouldReplayRuntimeEventToUi(event) {
10462
- return describeRuntimeEventLifecycle(event).replayToUi;
10463
- }
10464
- function shouldEnterAgentContext(event) {
10465
- return describeRuntimeEventLifecycle(event).enterAgentContext;
10466
- }
10467
- function getRuntimeEventUiProjectionKind(event) {
10468
- if (isHiddenUserInputEvent(event)) {
10469
- return "hidden";
10470
- }
10471
- switch (event.type) {
10472
- case "user_input":
10473
- return "user_input";
10474
- case "thought":
10475
- return "thought";
10476
- case "final_answer":
10477
- return "final_answer";
10478
- case "final_answer_chunk":
10479
- return "final_answer_chunk";
10480
- case "final_answer_reset":
10481
- return "final_answer_reset";
10482
- case "tool_call_decision":
10483
- return "tool_call_decision";
10484
- case "tool_process":
10485
- return "tool_process";
10486
- case "tool_output":
10487
- return "tool_output";
10488
- case "requires_user_interaction":
10489
- return "requires_user_interaction";
10490
- case "audit_envelope":
10491
- return "audit_envelope";
10492
- case "subrun_trace":
10493
- return "subrun_trace";
10494
- case "error":
10495
- return "error";
10496
- case "context_usage_snapshot":
10497
- return "context_usage_snapshot";
10498
- case "run_execution_metrics":
10499
- return "run_execution_metrics";
10500
- case "history_summary":
10501
- return "history_summary";
10502
- default:
10503
- return "unsupported";
10504
- }
10505
- }
10506
- function describeRuntimeEventLifecycle(event) {
10507
- const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
10508
- const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
10509
- const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
10510
- const enterAgentContext = (() => {
10511
- if (uiProjectionKind === "unsupported") {
10512
- return false;
10513
- }
10514
- if (isSubRunTraceRuntimeEvent(event)) {
10515
- return false;
10516
- }
10517
- if (isRunExecutionMetricsRuntimeEvent(event)) {
10518
- return false;
10519
- }
10520
- if (isContextUsageSnapshotRuntimeEvent(event)) {
10521
- return false;
10522
- }
10523
- if (event.type === "error") {
10524
- return false;
10525
- }
10526
- if (isRequiresUserInteractionRuntimeEvent(event)) {
10527
- return false;
10528
- }
10529
- if (isAuditEnvelopeRuntimeEvent(event)) {
10530
- return false;
10531
- }
10532
- if (isControlRuntimeEvent(event)) {
10533
- return false;
10534
- }
10535
- if (isHiddenUserInputEvent(event)) {
10536
- return false;
10537
- }
10538
- if (isToolProcessEvent(event)) {
10539
- return false;
10540
- }
10541
- if (event.type === "thought") {
10542
- return false;
10543
- }
10544
- if (event.type === "final_answer_chunk") {
10545
- return false;
10546
- }
10547
- if (event.type === "final_answer_reset") {
10548
- return false;
10549
- }
10550
- if (isEmptyTerminalAssistantRuntimeEvent(event)) {
10551
- return false;
10552
- }
10553
- return true;
10554
- })();
10555
- const realtimeChannel = (() => {
10556
- switch (uiProjectionKind) {
10557
- case "thought":
10558
- case "final_answer":
10559
- case "final_answer_chunk":
10560
- case "final_answer_reset":
10561
- case "tool_call_decision":
10562
- case "tool_process":
10563
- case "tool_output":
10564
- case "requires_user_interaction":
10565
- case "subrun_trace":
10566
- case "error":
10567
- case "context_usage_snapshot":
10568
- case "run_execution_metrics":
10569
- case "history_summary":
10570
- return "event_bus_sse";
10571
- case "audit_envelope":
10572
- return "none";
10573
- default:
10574
- return "none";
10575
- }
10576
- })();
10577
- return {
10578
- uiProjectionKind,
10579
- persist,
10580
- replayToUi,
10581
- enterAgentContext,
10582
- realtimeChannel
10583
- };
10584
- }
10585
- function shouldEmitRuntimeEventToSse(event) {
10586
- return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
10587
- }
10588
-
10589
10585
  // src/runtime-kernel/events/finalAnswerCompletion.ts
10590
10586
  function findTerminalFinalAnswer(events) {
10591
10587
  for (let index = events.length - 1; index >= 0; index -= 1) {