@linnlabs/linnkit 0.37.2 → 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.
package/dist/testkit.js CHANGED
@@ -6554,6 +6554,204 @@ var GraphAgentExecutor = class {
6554
6554
  }
6555
6555
  };
6556
6556
 
6557
+ // src/runtime-kernel/events/eventGovernance.ts
6558
+ var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
6559
+ "audit_envelope",
6560
+ "final_answer_chunk",
6561
+ "final_answer_reset",
6562
+ "subrun_trace",
6563
+ "context_usage_snapshot"
6564
+ ];
6565
+ function isRecord6(value) {
6566
+ return !!value && typeof value === "object" && !Array.isArray(value);
6567
+ }
6568
+ function isToolCallDecisionEvent(event) {
6569
+ return event.type === "tool_call_decision";
6570
+ }
6571
+ function isToolProcessEvent(event) {
6572
+ return event.type === "tool_process";
6573
+ }
6574
+ function isSubRunTraceRuntimeEvent(event) {
6575
+ return event.type === "subrun_trace";
6576
+ }
6577
+ function isRunExecutionMetricsRuntimeEvent(event) {
6578
+ return event.type === "run_execution_metrics";
6579
+ }
6580
+ function isContextUsageSnapshotRuntimeEvent(event) {
6581
+ return event.type === "context_usage_snapshot";
6582
+ }
6583
+ function isRequiresUserInteractionRuntimeEvent(event) {
6584
+ return event.type === "requires_user_interaction";
6585
+ }
6586
+ function isAuditEnvelopeRuntimeEvent(event) {
6587
+ return event.type === "audit_envelope";
6588
+ }
6589
+ function isControlRuntimeEvent(event) {
6590
+ return event.type === "control";
6591
+ }
6592
+ function isHiddenUserInputEvent(event) {
6593
+ if (event.type !== "user_input") {
6594
+ return false;
6595
+ }
6596
+ const metadata = isRecord6(event.metadata) ? event.metadata : void 0;
6597
+ const ui = metadata && isRecord6(metadata.ui) ? metadata.ui : void 0;
6598
+ return ui?.presentation === "hidden";
6599
+ }
6600
+ function isEmptyTerminalAssistantRuntimeEvent(event) {
6601
+ if (event.type !== "thought" && event.type !== "final_answer") {
6602
+ return false;
6603
+ }
6604
+ return event.content.trim().length === 0;
6605
+ }
6606
+ function isConversationUiRuntimeEvent(event) {
6607
+ return event.lane === "foreground" && event.visibility === "conversation";
6608
+ }
6609
+ function shouldPersistRuntimeEvent(event) {
6610
+ return describeRuntimeEventLifecycle(event).persist;
6611
+ }
6612
+ function requirePersistableRuntimeEvent(event) {
6613
+ if (!shouldPersistRuntimeEvent(event)) {
6614
+ throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
6615
+ }
6616
+ return event;
6617
+ }
6618
+ function requirePersistableRoutedRuntimeEvent(event) {
6619
+ requirePersistableRuntimeEvent(event);
6620
+ return parseRoutedRuntimeEvent(event);
6621
+ }
6622
+ function shouldReplayRuntimeEventToUi(event) {
6623
+ return describeRuntimeEventLifecycle(event).replayToUi;
6624
+ }
6625
+ function shouldEnterAgentContext(event) {
6626
+ return describeRuntimeEventLifecycle(event).enterAgentContext;
6627
+ }
6628
+ function getRuntimeEventUiProjectionKind(event) {
6629
+ if (isHiddenUserInputEvent(event)) {
6630
+ return "hidden";
6631
+ }
6632
+ switch (event.type) {
6633
+ case "user_input":
6634
+ return "user_input";
6635
+ case "thought":
6636
+ return "thought";
6637
+ case "final_answer":
6638
+ return "final_answer";
6639
+ case "final_answer_chunk":
6640
+ return "final_answer_chunk";
6641
+ case "final_answer_reset":
6642
+ return "final_answer_reset";
6643
+ case "tool_call_decision":
6644
+ return "tool_call_decision";
6645
+ case "tool_process":
6646
+ return "tool_process";
6647
+ case "tool_output":
6648
+ return "tool_output";
6649
+ case "requires_user_interaction":
6650
+ return "requires_user_interaction";
6651
+ case "audit_envelope":
6652
+ return "audit_envelope";
6653
+ case "subrun_trace":
6654
+ return "subrun_trace";
6655
+ case "error":
6656
+ return "error";
6657
+ case "context_usage_snapshot":
6658
+ return "context_usage_snapshot";
6659
+ case "run_execution_metrics":
6660
+ return "run_execution_metrics";
6661
+ case "history_summary":
6662
+ return "history_summary";
6663
+ default:
6664
+ return "unsupported";
6665
+ }
6666
+ }
6667
+ function describeRuntimeEventLifecycle(event) {
6668
+ const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
6669
+ const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
6670
+ const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
6671
+ const enterAgentContext = (() => {
6672
+ if (uiProjectionKind === "unsupported") {
6673
+ return false;
6674
+ }
6675
+ if (isSubRunTraceRuntimeEvent(event)) {
6676
+ return false;
6677
+ }
6678
+ if (isRunExecutionMetricsRuntimeEvent(event)) {
6679
+ return false;
6680
+ }
6681
+ if (isContextUsageSnapshotRuntimeEvent(event)) {
6682
+ return false;
6683
+ }
6684
+ if (event.type === "error") {
6685
+ return false;
6686
+ }
6687
+ if (isRequiresUserInteractionRuntimeEvent(event)) {
6688
+ return false;
6689
+ }
6690
+ if (isAuditEnvelopeRuntimeEvent(event)) {
6691
+ return false;
6692
+ }
6693
+ if (isControlRuntimeEvent(event)) {
6694
+ return false;
6695
+ }
6696
+ if (isHiddenUserInputEvent(event)) {
6697
+ return false;
6698
+ }
6699
+ if (isToolProcessEvent(event)) {
6700
+ return false;
6701
+ }
6702
+ if (event.type === "thought") {
6703
+ return false;
6704
+ }
6705
+ if (event.type === "final_answer_chunk") {
6706
+ return false;
6707
+ }
6708
+ if (event.type === "final_answer_reset") {
6709
+ return false;
6710
+ }
6711
+ if (isEmptyTerminalAssistantRuntimeEvent(event)) {
6712
+ return false;
6713
+ }
6714
+ return true;
6715
+ })();
6716
+ const realtimeChannel = (() => {
6717
+ switch (uiProjectionKind) {
6718
+ case "thought":
6719
+ case "final_answer":
6720
+ case "final_answer_chunk":
6721
+ case "final_answer_reset":
6722
+ case "tool_call_decision":
6723
+ case "tool_process":
6724
+ case "tool_output":
6725
+ case "requires_user_interaction":
6726
+ case "subrun_trace":
6727
+ case "error":
6728
+ case "context_usage_snapshot":
6729
+ case "run_execution_metrics":
6730
+ case "history_summary":
6731
+ return "event_bus_sse";
6732
+ case "audit_envelope":
6733
+ return "none";
6734
+ default:
6735
+ return "none";
6736
+ }
6737
+ })();
6738
+ return {
6739
+ uiProjectionKind,
6740
+ persist,
6741
+ replayToUi,
6742
+ enterAgentContext,
6743
+ realtimeChannel
6744
+ };
6745
+ }
6746
+ function shouldEmitRuntimeEventToSse(event) {
6747
+ return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
6748
+ }
6749
+
6750
+ // src/runtime-kernel/graph-engine/functions/appendWorkingHistory.ts
6751
+ function appendWorkingHistory(history, events) {
6752
+ return [...history.filter(shouldPersistRuntimeEvent), ...events.filter(shouldPersistRuntimeEvent)];
6753
+ }
6754
+
6557
6755
  // src/runtime-kernel/graph-engine/nodes/llmNode.state.ts
6558
6756
  function assertLlmNodeStreamChunkSequence(state, answerId, seq) {
6559
6757
  const answerChanged = state.answerId !== answerId;
@@ -6638,7 +6836,7 @@ function initLlmNodeState(init) {
6638
6836
  };
6639
6837
  }
6640
6838
  function buildLocalPatch(nodeState, ctx) {
6641
- const updatedHistory = [...ctx.history, ...nodeState.streamRuntimeEvents];
6839
+ const updatedHistory = appendWorkingHistory(ctx.history, nodeState.streamRuntimeEvents);
6642
6840
  const patch = {
6643
6841
  answerId: nodeState.answerId,
6644
6842
  chunkSeq: nodeState.chunkSeq,
@@ -6791,11 +6989,11 @@ var AgentEventSchema = z.discriminatedUnion("type", [
6791
6989
  });
6792
6990
 
6793
6991
  // src/runtime-kernel/events/provider-sidecar.ts
6794
- var isRecord6 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6992
+ var isRecord7 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6795
6993
  function readMetaFromEvent(event) {
6796
- if (!isRecord6(event)) return void 0;
6994
+ if (!isRecord7(event)) return void 0;
6797
6995
  const value = event["meta"];
6798
- return isRecord6(value) ? value : void 0;
6996
+ return isRecord7(value) ? value : void 0;
6799
6997
  }
6800
6998
 
6801
6999
  // src/runtime-kernel/events/agent-to-runtime.ts
@@ -6803,7 +7001,7 @@ function agentEventToRuntime(agentEvent, context, options = {}) {
6803
7001
  return mapAgentEventToRuntime(agentEvent, context, options);
6804
7002
  }
6805
7003
  function mapAgentEventToRuntime(agentEvent, context, options) {
6806
- if (isRecord6(agentEvent) && agentEvent["type"] === "history_summary") {
7004
+ if (isRecord7(agentEvent) && agentEvent["type"] === "history_summary") {
6807
7005
  const event = RuntimeEvent.parse(agentEvent);
6808
7006
  return context.metadata ? {
6809
7007
  ...event,
@@ -7464,7 +7662,7 @@ var LlmNode = class {
7464
7662
  ];
7465
7663
  logger7.info("[LlmNode] \u5386\u53F2\u4E8B\u4EF6\u5DF2\u66F4\u65B0", {
7466
7664
  previousCount: history.length,
7467
- nextCount: parseRuntimeEvents(patch.history).length,
7665
+ nextCount: patch.history.length,
7468
7666
  streamedEventCount: nodeState.streamRuntimeEvents.length
7469
7667
  });
7470
7668
  logger7.info("[LlmNode] \u6267\u884C\u5668\u51B3\u7B56\u5B8C\u6210", {
@@ -7511,7 +7709,7 @@ var ToolModelInputResolutionError = class extends Error {
7511
7709
  // src/runtime-kernel/tools/model-input/functions/parseToolModelInputDeclaration.ts
7512
7710
  var DECLARATION_KEYS = /* @__PURE__ */ new Set(["attachments"]);
7513
7711
  var SELECTION_KEYS = /* @__PURE__ */ new Set(["id", "uri", "label"]);
7514
- function isRecord7(value) {
7712
+ function isRecord8(value) {
7515
7713
  return value !== null && typeof value === "object" && !Array.isArray(value);
7516
7714
  }
7517
7715
  function hasOnlyKeys(value, allowed) {
@@ -7524,7 +7722,7 @@ function readTrimmedString(value, maxLength) {
7524
7722
  return value === value.trim() ? value : void 0;
7525
7723
  }
7526
7724
  function parseSelection(value, index) {
7527
- if (!isRecord7(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7725
+ if (!isRecord8(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7528
7726
  return { ok: false, reason: `modelInput.attachments[${index}] \u5FC5\u987B\u662F strict selection\u3002` };
7529
7727
  }
7530
7728
  const id = readTrimmedString(value.id, 200);
@@ -7545,7 +7743,7 @@ function parseSelection(value, index) {
7545
7743
  return Object.freeze({ id, uri });
7546
7744
  }
7547
7745
  function parseToolModelInputDeclaration(value) {
7548
- if (!isRecord7(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7746
+ if (!isRecord8(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7549
7747
  return { ok: false, reason: "modelInput \u5FC5\u987B\u662F\u53EA\u5305\u542B attachments \u7684 strict \u5BF9\u8C61\u3002" };
7550
7748
  }
7551
7749
  if (!Array.isArray(value.attachments) || value.attachments.length === 0) {
@@ -7621,7 +7819,7 @@ function resolveFinalAnswerFromToolControl(finalAnswer) {
7621
7819
  }
7622
7820
 
7623
7821
  // src/runtime-kernel/graph-engine/nodes/toolNode.helpers.ts
7624
- function isRecord8(value) {
7822
+ function isRecord9(value) {
7625
7823
  return !!value && typeof value === "object" && !Array.isArray(value);
7626
7824
  }
7627
7825
  function readString3(value) {
@@ -7664,7 +7862,7 @@ function resolveToolObservationGovernancePolicy(policy) {
7664
7862
  }
7665
7863
  function readObservationPreviewMeta(parsed) {
7666
7864
  const meta = parsed["observationPreviewMeta"];
7667
- if (!isRecord8(meta)) {
7865
+ if (!isRecord9(meta)) {
7668
7866
  return void 0;
7669
7867
  }
7670
7868
  const filename = readString3(meta["filename"]);
@@ -7682,7 +7880,7 @@ function readObservationPreviewMeta(parsed) {
7682
7880
  };
7683
7881
  }
7684
7882
  async function applyObservationGovernance(params) {
7685
- if (!params.structuredObservation || !isRecord8(params.parsed)) {
7883
+ if (!params.structuredObservation || !isRecord9(params.parsed)) {
7686
7884
  throw new Error("Observation governance requires a validated structured tool result.");
7687
7885
  }
7688
7886
  const policy = resolveToolObservationGovernancePolicy(params.policy);
@@ -7774,7 +7972,7 @@ function createToolProtocolFuseError(nextCount, error) {
7774
7972
  // src/runtime-kernel/graph-engine/nodes/toolNode.stateTransitions.ts
7775
7973
  function mergeHistory(local, runtimeEvents) {
7776
7974
  const history = parseRuntimeEvents(local.history ?? []);
7777
- return [...history, ...runtimeEvents];
7975
+ return appendWorkingHistory(history, runtimeEvents);
7778
7976
  }
7779
7977
  function stripAnswerState(local) {
7780
7978
  const nextLocal = { ...local };
@@ -7783,7 +7981,7 @@ function stripAnswerState(local) {
7783
7981
  return nextLocal;
7784
7982
  }
7785
7983
  function validateStructuredToolResultContract(parsed) {
7786
- if (!isRecord8(parsed)) {
7984
+ if (!isRecord9(parsed)) {
7787
7985
  return { ok: false, reason: "\u5DE5\u5177\u6210\u529F\u7ED3\u679C\u5FC5\u987B\u662F JSON \u5BF9\u8C61\u3002" };
7788
7986
  }
7789
7987
  if (!Object.prototype.hasOwnProperty.call(parsed, "data") || parsed.data === void 0) {
@@ -7811,7 +8009,7 @@ function validateStructuredToolResultContract(parsed) {
7811
8009
  };
7812
8010
  }
7813
8011
  function extractToolControlInfo(parsed) {
7814
- if (!isRecord8(parsed) || !isRecord8(parsed.control)) {
8012
+ if (!isRecord9(parsed) || !isRecord9(parsed.control)) {
7815
8013
  return void 0;
7816
8014
  }
7817
8015
  const control = parsed.control;
@@ -7835,7 +8033,7 @@ function buildRequireUserLocalState(params) {
7835
8033
  ...params.local,
7836
8034
  pendingToolCalls: params.remainingCalls,
7837
8035
  pendingInteractionSpec: {
7838
- ...isRecord8(params.parsed) && isRecord8(params.parsed.control) ? params.parsed.control : {},
8036
+ ...isRecord9(params.parsed) && isRecord9(params.parsed.control) ? params.parsed.control : {},
7839
8037
  // wait_user 必须携带工具已校验的结构化结果,UI 才能使用正式 form identity。
7840
8038
  form: params.parsed,
7841
8039
  toolCallId: params.toolCallId,
@@ -7874,7 +8072,7 @@ function buildErrorLocalState(params) {
7874
8072
 
7875
8073
  // src/runtime-kernel/tools/argNormalizer.ts
7876
8074
  var logger8 = new Logger("ToolArgNormalizer");
7877
- function isRecord9(v) {
8075
+ function isRecord10(v) {
7878
8076
  return !!v && typeof v === "object" && !Array.isArray(v);
7879
8077
  }
7880
8078
  function tryParseJsonEncodedValue(v) {
@@ -7967,12 +8165,12 @@ function normalizeByPropertySchema(prop, v, options) {
7967
8165
  }
7968
8166
  case "object": {
7969
8167
  const parsedValue = tryParseJsonEncodedValue(v);
7970
- if (parsedValue !== v && isRecord9(parsedValue)) {
8168
+ if (parsedValue !== v && isRecord10(parsedValue)) {
7971
8169
  logJsonEncodedNormalization("object", options);
7972
8170
  }
7973
- if (!isRecord9(parsedValue)) return parsedValue;
8171
+ if (!isRecord10(parsedValue)) return parsedValue;
7974
8172
  const selectedSchema = selectDiscriminatedObjectSchema(prop, parsedValue);
7975
- if (!selectedSchema.properties || !isRecord9(selectedSchema.properties)) return parsedValue;
8173
+ if (!selectedSchema.properties || !isRecord10(selectedSchema.properties)) return parsedValue;
7976
8174
  return normalizeToolArgs(
7977
8175
  { properties: selectedSchema.properties },
7978
8176
  parsedValue,
@@ -7994,7 +8192,7 @@ function normalizeToolArgs(schema, args, options = {}) {
7994
8192
  }
7995
8193
  return next;
7996
8194
  }
7997
- function isRecord10(v) {
8195
+ function isRecord11(v) {
7998
8196
  return !!v && typeof v === "object" && !Array.isArray(v);
7999
8197
  }
8000
8198
  function stableStringify(v) {
@@ -8004,7 +8202,7 @@ function stableStringify(v) {
8004
8202
  if (t === "string") return JSON.stringify(v);
8005
8203
  if (t === "number" || t === "boolean") return String(v);
8006
8204
  if (Array.isArray(v)) return `[${v.map(stableStringify).join(",")}]`;
8007
- if (isRecord10(v)) {
8205
+ if (isRecord11(v)) {
8008
8206
  const keys = Object.keys(v).sort();
8009
8207
  const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(v[k])}`);
8010
8208
  return `{${parts.join(",")}}`;
@@ -8041,13 +8239,13 @@ function findCachedToolOutputByIdempotencyKey(params) {
8041
8239
  if (e.tool_name !== params.toolName) continue;
8042
8240
  if (e.status !== "success") continue;
8043
8241
  const meta = e.metadata;
8044
- if (!isRecord10(meta)) continue;
8242
+ if (!isRecord11(meta)) continue;
8045
8243
  const idem = meta["idempotency"];
8046
- if (!isRecord10(idem)) continue;
8244
+ if (!isRecord11(idem)) continue;
8047
8245
  if (idem["key"] !== params.idempotencyKey) continue;
8048
8246
  const structuredResult = { data: e.data, observation: e.observation };
8049
8247
  const presentation = meta?.presentation;
8050
- if (isRecord10(presentation) && presentation["media"] !== void 0) {
8248
+ if (isRecord11(presentation) && presentation["media"] !== void 0) {
8051
8249
  structuredResult["media"] = presentation["media"];
8052
8250
  }
8053
8251
  return {
@@ -8382,7 +8580,7 @@ function parseToolArgs(call) {
8382
8580
  }
8383
8581
  try {
8384
8582
  const parsed = JSON.parse(rawArguments);
8385
- if (!isRecord8(parsed)) {
8583
+ if (!isRecord9(parsed)) {
8386
8584
  return {
8387
8585
  ok: false,
8388
8586
  error: "Tool arguments must decode to a JSON object."
@@ -8400,13 +8598,12 @@ function parseToolArgs(call) {
8400
8598
  }
8401
8599
  function prepareToolNodeContext(state) {
8402
8600
  const local = state.local || {};
8403
- const toolContext = isRecord8(local.toolContext) ? local.toolContext : {};
8404
- if (!isRecord8(local.toolContext)) {
8601
+ const toolContext = isRecord9(local.toolContext) ? local.toolContext : {};
8602
+ if (!isRecord9(local.toolContext)) {
8405
8603
  local.toolContext = toolContext;
8406
8604
  }
8407
8605
  const conversationId = requireRuntimeIdentity(local.conversationId, "conversationId");
8408
8606
  const turnId = requireRuntimeIdentity(local.turnId, "turnId");
8409
- parseRuntimeEvents(local.history ?? []);
8410
8607
  const existingConversationId = typeof toolContext.conversationId === "string" ? toolContext.conversationId.trim() : "";
8411
8608
  ensureToolContextRuntimeCapability({
8412
8609
  context: toolContext,
@@ -8674,11 +8871,11 @@ function createPendingToolCancellationEvents(params) {
8674
8871
  return events;
8675
8872
  }
8676
8873
  function buildCancelledToolBatchLocalState(local, runtimeEvents) {
8677
- const history = Array.isArray(local.history) ? local.history : [];
8874
+ const history = parseRuntimeEvents(local.history ?? []);
8678
8875
  return {
8679
8876
  ...stripAnswerState(local),
8680
8877
  pendingToolCalls: [],
8681
- history: [...history, ...runtimeEvents]
8878
+ history: appendWorkingHistory(history, runtimeEvents)
8682
8879
  };
8683
8880
  }
8684
8881
  function recordToolProtocolError(params) {
@@ -8747,14 +8944,14 @@ function readHistoryEvents(local) {
8747
8944
  }
8748
8945
  function readLastSuccessfulLlmModelId(local) {
8749
8946
  const executorLocal = local.executorLocal;
8750
- if (!isRecord8(executorLocal)) {
8947
+ if (!isRecord9(executorLocal)) {
8751
8948
  return void 0;
8752
8949
  }
8753
8950
  const value = executorLocal.lastSuccessfulLlmModelId;
8754
8951
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
8755
8952
  }
8756
8953
  function formatCapabilityError(error) {
8757
- if (isRecord8(error)) {
8954
+ if (isRecord9(error)) {
8758
8955
  const codeValue = error.errorCode ?? error.code;
8759
8956
  const code = typeof codeValue === "string" ? codeValue.trim() : "";
8760
8957
  const message = typeof error.message === "string" ? error.message.trim() : "";
@@ -9369,16 +9566,16 @@ var ToolNode = class {
9369
9566
  };
9370
9567
 
9371
9568
  // src/runtime-kernel/graph-engine/nodes/userNode.ts
9372
- function isRecord11(value) {
9569
+ function isRecord12(value) {
9373
9570
  return typeof value === "object" && value !== null;
9374
9571
  }
9375
9572
  function isRuntimeEvent(value) {
9376
- return isRecord11(value) && typeof value.type === "string";
9573
+ return isRecord12(value) && typeof value.type === "string";
9377
9574
  }
9378
9575
  var UserNode = class {
9379
9576
  id = "user";
9380
9577
  async run(state) {
9381
- const local = isRecord11(state.local) ? state.local : {};
9578
+ const local = isRecord12(state.local) ? state.local : {};
9382
9579
  const newEvents = Array.isArray(local.newEvents) ? local.newEvents.filter(isRuntimeEvent) : [];
9383
9580
  const userInputEvent = newEvents.find((event) => event.type === "user_input");
9384
9581
  if (!userInputEvent) {
@@ -9389,7 +9586,7 @@ var UserNode = class {
9389
9586
  };
9390
9587
 
9391
9588
  // src/runtime-kernel/graph-engine/nodes/waitUserNode.ts
9392
- function isRecord12(value) {
9589
+ function isRecord13(value) {
9393
9590
  return !!value && typeof value === "object" && !Array.isArray(value);
9394
9591
  }
9395
9592
  function requireNonEmptyString(value, field) {
@@ -9406,10 +9603,10 @@ var WaitUserNode = class {
9406
9603
  }
9407
9604
  async run(state) {
9408
9605
  const local = state.local ?? {};
9409
- const spec = isRecord12(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9606
+ const spec = isRecord13(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9410
9607
  const conversationId = requireNonEmptyString(local.conversationId, "conversationId");
9411
9608
  const turnId = requireNonEmptyString(local.turnId, "turnId");
9412
- const toolContext = isRecord12(local.toolContext) ? local.toolContext : {};
9609
+ const toolContext = isRecord13(local.toolContext) ? local.toolContext : {};
9413
9610
  const runId = RunIdSchema.parse(requireNonEmptyString(toolContext.runId, "toolContext.runId"));
9414
9611
  const parentRunId = toolContext.parentRunId === void 0 ? void 0 : RunIdSchema.parse(toolContext.parentRunId);
9415
9612
  const toolCallId = ToolCallIdSchema.parse(
@@ -9564,199 +9761,6 @@ __export(events_exports, {
9564
9761
  shouldReplayRuntimeEventToUi: () => shouldReplayRuntimeEventToUi
9565
9762
  });
9566
9763
 
9567
- // src/runtime-kernel/events/eventGovernance.ts
9568
- var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
9569
- "audit_envelope",
9570
- "final_answer_chunk",
9571
- "final_answer_reset",
9572
- "subrun_trace",
9573
- "context_usage_snapshot"
9574
- ];
9575
- function isRecord13(value) {
9576
- return !!value && typeof value === "object" && !Array.isArray(value);
9577
- }
9578
- function isToolCallDecisionEvent(event) {
9579
- return event.type === "tool_call_decision";
9580
- }
9581
- function isToolProcessEvent(event) {
9582
- return event.type === "tool_process";
9583
- }
9584
- function isSubRunTraceRuntimeEvent(event) {
9585
- return event.type === "subrun_trace";
9586
- }
9587
- function isRunExecutionMetricsRuntimeEvent(event) {
9588
- return event.type === "run_execution_metrics";
9589
- }
9590
- function isContextUsageSnapshotRuntimeEvent(event) {
9591
- return event.type === "context_usage_snapshot";
9592
- }
9593
- function isRequiresUserInteractionRuntimeEvent(event) {
9594
- return event.type === "requires_user_interaction";
9595
- }
9596
- function isAuditEnvelopeRuntimeEvent(event) {
9597
- return event.type === "audit_envelope";
9598
- }
9599
- function isControlRuntimeEvent(event) {
9600
- return event.type === "control";
9601
- }
9602
- function isHiddenUserInputEvent(event) {
9603
- if (event.type !== "user_input") {
9604
- return false;
9605
- }
9606
- const metadata = isRecord13(event.metadata) ? event.metadata : void 0;
9607
- const ui = metadata && isRecord13(metadata.ui) ? metadata.ui : void 0;
9608
- return ui?.presentation === "hidden";
9609
- }
9610
- function isEmptyTerminalAssistantRuntimeEvent(event) {
9611
- if (event.type !== "thought" && event.type !== "final_answer") {
9612
- return false;
9613
- }
9614
- return event.content.trim().length === 0;
9615
- }
9616
- function isConversationUiRuntimeEvent(event) {
9617
- return event.lane === "foreground" && event.visibility === "conversation";
9618
- }
9619
- function shouldPersistRuntimeEvent(event) {
9620
- return describeRuntimeEventLifecycle(event).persist;
9621
- }
9622
- function requirePersistableRuntimeEvent(event) {
9623
- if (!shouldPersistRuntimeEvent(event)) {
9624
- throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
9625
- }
9626
- return event;
9627
- }
9628
- function requirePersistableRoutedRuntimeEvent(event) {
9629
- requirePersistableRuntimeEvent(event);
9630
- return parseRoutedRuntimeEvent(event);
9631
- }
9632
- function shouldReplayRuntimeEventToUi(event) {
9633
- return describeRuntimeEventLifecycle(event).replayToUi;
9634
- }
9635
- function shouldEnterAgentContext(event) {
9636
- return describeRuntimeEventLifecycle(event).enterAgentContext;
9637
- }
9638
- function getRuntimeEventUiProjectionKind(event) {
9639
- if (isHiddenUserInputEvent(event)) {
9640
- return "hidden";
9641
- }
9642
- switch (event.type) {
9643
- case "user_input":
9644
- return "user_input";
9645
- case "thought":
9646
- return "thought";
9647
- case "final_answer":
9648
- return "final_answer";
9649
- case "final_answer_chunk":
9650
- return "final_answer_chunk";
9651
- case "final_answer_reset":
9652
- return "final_answer_reset";
9653
- case "tool_call_decision":
9654
- return "tool_call_decision";
9655
- case "tool_process":
9656
- return "tool_process";
9657
- case "tool_output":
9658
- return "tool_output";
9659
- case "requires_user_interaction":
9660
- return "requires_user_interaction";
9661
- case "audit_envelope":
9662
- return "audit_envelope";
9663
- case "subrun_trace":
9664
- return "subrun_trace";
9665
- case "error":
9666
- return "error";
9667
- case "context_usage_snapshot":
9668
- return "context_usage_snapshot";
9669
- case "run_execution_metrics":
9670
- return "run_execution_metrics";
9671
- case "history_summary":
9672
- return "history_summary";
9673
- default:
9674
- return "unsupported";
9675
- }
9676
- }
9677
- function describeRuntimeEventLifecycle(event) {
9678
- const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
9679
- const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
9680
- const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
9681
- const enterAgentContext = (() => {
9682
- if (uiProjectionKind === "unsupported") {
9683
- return false;
9684
- }
9685
- if (isSubRunTraceRuntimeEvent(event)) {
9686
- return false;
9687
- }
9688
- if (isRunExecutionMetricsRuntimeEvent(event)) {
9689
- return false;
9690
- }
9691
- if (isContextUsageSnapshotRuntimeEvent(event)) {
9692
- return false;
9693
- }
9694
- if (event.type === "error") {
9695
- return false;
9696
- }
9697
- if (isRequiresUserInteractionRuntimeEvent(event)) {
9698
- return false;
9699
- }
9700
- if (isAuditEnvelopeRuntimeEvent(event)) {
9701
- return false;
9702
- }
9703
- if (isControlRuntimeEvent(event)) {
9704
- return false;
9705
- }
9706
- if (isHiddenUserInputEvent(event)) {
9707
- return false;
9708
- }
9709
- if (isToolProcessEvent(event)) {
9710
- return false;
9711
- }
9712
- if (event.type === "thought") {
9713
- return false;
9714
- }
9715
- if (event.type === "final_answer_chunk") {
9716
- return false;
9717
- }
9718
- if (event.type === "final_answer_reset") {
9719
- return false;
9720
- }
9721
- if (isEmptyTerminalAssistantRuntimeEvent(event)) {
9722
- return false;
9723
- }
9724
- return true;
9725
- })();
9726
- const realtimeChannel = (() => {
9727
- switch (uiProjectionKind) {
9728
- case "thought":
9729
- case "final_answer":
9730
- case "final_answer_chunk":
9731
- case "final_answer_reset":
9732
- case "tool_call_decision":
9733
- case "tool_process":
9734
- case "tool_output":
9735
- case "requires_user_interaction":
9736
- case "subrun_trace":
9737
- case "error":
9738
- case "context_usage_snapshot":
9739
- case "run_execution_metrics":
9740
- case "history_summary":
9741
- return "event_bus_sse";
9742
- case "audit_envelope":
9743
- return "none";
9744
- default:
9745
- return "none";
9746
- }
9747
- })();
9748
- return {
9749
- uiProjectionKind,
9750
- persist,
9751
- replayToUi,
9752
- enterAgentContext,
9753
- realtimeChannel
9754
- };
9755
- }
9756
- function shouldEmitRuntimeEventToSse(event) {
9757
- return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
9758
- }
9759
-
9760
9764
  // src/runtime-kernel/events/finalAnswerCompletion.ts
9761
9765
  function findTerminalFinalAnswer(events) {
9762
9766
  for (let index = events.length - 1; index >= 0; index -= 1) {