@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/index.js CHANGED
@@ -7366,6 +7366,204 @@ var GraphAgentExecutor = class {
7366
7366
  }
7367
7367
  };
7368
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
+
7369
7567
  // src/runtime-kernel/graph-engine/nodes/llmNode.state.ts
7370
7568
  function assertLlmNodeStreamChunkSequence(state, answerId, seq) {
7371
7569
  const answerChanged = state.answerId !== answerId;
@@ -7450,7 +7648,7 @@ function initLlmNodeState(init) {
7450
7648
  };
7451
7649
  }
7452
7650
  function buildLocalPatch(nodeState, ctx) {
7453
- const updatedHistory = [...ctx.history, ...nodeState.streamRuntimeEvents];
7651
+ const updatedHistory = appendWorkingHistory(ctx.history, nodeState.streamRuntimeEvents);
7454
7652
  const patch = {
7455
7653
  answerId: nodeState.answerId,
7456
7654
  chunkSeq: nodeState.chunkSeq,
@@ -7603,11 +7801,11 @@ var AgentEventSchema = z.discriminatedUnion("type", [
7603
7801
  });
7604
7802
 
7605
7803
  // src/runtime-kernel/events/provider-sidecar.ts
7606
- var isRecord6 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
7804
+ var isRecord7 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
7607
7805
  function readMetaFromEvent(event) {
7608
- if (!isRecord6(event)) return void 0;
7806
+ if (!isRecord7(event)) return void 0;
7609
7807
  const value = event["meta"];
7610
- return isRecord6(value) ? value : void 0;
7808
+ return isRecord7(value) ? value : void 0;
7611
7809
  }
7612
7810
 
7613
7811
  // src/runtime-kernel/events/agent-to-runtime.ts
@@ -7615,7 +7813,7 @@ function agentEventToRuntime(agentEvent, context, options = {}) {
7615
7813
  return mapAgentEventToRuntime(agentEvent, context, options);
7616
7814
  }
7617
7815
  function mapAgentEventToRuntime(agentEvent, context, options) {
7618
- if (isRecord6(agentEvent) && agentEvent["type"] === "history_summary") {
7816
+ if (isRecord7(agentEvent) && agentEvent["type"] === "history_summary") {
7619
7817
  const event = RuntimeEvent.parse(agentEvent);
7620
7818
  return context.metadata ? {
7621
7819
  ...event,
@@ -8276,7 +8474,7 @@ var LlmNode = class {
8276
8474
  ];
8277
8475
  logger7.info("[LlmNode] \u5386\u53F2\u4E8B\u4EF6\u5DF2\u66F4\u65B0", {
8278
8476
  previousCount: history.length,
8279
- nextCount: parseRuntimeEvents(patch.history).length,
8477
+ nextCount: patch.history.length,
8280
8478
  streamedEventCount: nodeState.streamRuntimeEvents.length
8281
8479
  });
8282
8480
  logger7.info("[LlmNode] \u6267\u884C\u5668\u51B3\u7B56\u5B8C\u6210", {
@@ -8323,7 +8521,7 @@ var ToolModelInputResolutionError = class extends Error {
8323
8521
  // src/runtime-kernel/tools/model-input/functions/parseToolModelInputDeclaration.ts
8324
8522
  var DECLARATION_KEYS = /* @__PURE__ */ new Set(["attachments"]);
8325
8523
  var SELECTION_KEYS = /* @__PURE__ */ new Set(["id", "uri", "label"]);
8326
- function isRecord7(value) {
8524
+ function isRecord8(value) {
8327
8525
  return value !== null && typeof value === "object" && !Array.isArray(value);
8328
8526
  }
8329
8527
  function hasOnlyKeys(value, allowed) {
@@ -8336,7 +8534,7 @@ function readTrimmedString(value, maxLength) {
8336
8534
  return value === value.trim() ? value : void 0;
8337
8535
  }
8338
8536
  function parseSelection(value, index) {
8339
- if (!isRecord7(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
8537
+ if (!isRecord8(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
8340
8538
  return { ok: false, reason: `modelInput.attachments[${index}] \u5FC5\u987B\u662F strict selection\u3002` };
8341
8539
  }
8342
8540
  const id = readTrimmedString(value.id, 200);
@@ -8357,7 +8555,7 @@ function parseSelection(value, index) {
8357
8555
  return Object.freeze({ id, uri });
8358
8556
  }
8359
8557
  function parseToolModelInputDeclaration(value) {
8360
- if (!isRecord7(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
8558
+ if (!isRecord8(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
8361
8559
  return { ok: false, reason: "modelInput \u5FC5\u987B\u662F\u53EA\u5305\u542B attachments \u7684 strict \u5BF9\u8C61\u3002" };
8362
8560
  }
8363
8561
  if (!Array.isArray(value.attachments) || value.attachments.length === 0) {
@@ -8433,7 +8631,7 @@ function resolveFinalAnswerFromToolControl(finalAnswer) {
8433
8631
  }
8434
8632
 
8435
8633
  // src/runtime-kernel/graph-engine/nodes/toolNode.helpers.ts
8436
- function isRecord8(value) {
8634
+ function isRecord9(value) {
8437
8635
  return !!value && typeof value === "object" && !Array.isArray(value);
8438
8636
  }
8439
8637
  function readString3(value) {
@@ -8476,7 +8674,7 @@ function resolveToolObservationGovernancePolicy(policy) {
8476
8674
  }
8477
8675
  function readObservationPreviewMeta(parsed) {
8478
8676
  const meta = parsed["observationPreviewMeta"];
8479
- if (!isRecord8(meta)) {
8677
+ if (!isRecord9(meta)) {
8480
8678
  return void 0;
8481
8679
  }
8482
8680
  const filename = readString3(meta["filename"]);
@@ -8494,7 +8692,7 @@ function readObservationPreviewMeta(parsed) {
8494
8692
  };
8495
8693
  }
8496
8694
  async function applyObservationGovernance(params) {
8497
- if (!params.structuredObservation || !isRecord8(params.parsed)) {
8695
+ if (!params.structuredObservation || !isRecord9(params.parsed)) {
8498
8696
  throw new Error("Observation governance requires a validated structured tool result.");
8499
8697
  }
8500
8698
  const policy = resolveToolObservationGovernancePolicy(params.policy);
@@ -8586,7 +8784,7 @@ function createToolProtocolFuseError(nextCount, error) {
8586
8784
  // src/runtime-kernel/graph-engine/nodes/toolNode.stateTransitions.ts
8587
8785
  function mergeHistory(local, runtimeEvents) {
8588
8786
  const history = parseRuntimeEvents(local.history ?? []);
8589
- return [...history, ...runtimeEvents];
8787
+ return appendWorkingHistory(history, runtimeEvents);
8590
8788
  }
8591
8789
  function stripAnswerState(local) {
8592
8790
  const nextLocal = { ...local };
@@ -8595,7 +8793,7 @@ function stripAnswerState(local) {
8595
8793
  return nextLocal;
8596
8794
  }
8597
8795
  function validateStructuredToolResultContract(parsed) {
8598
- if (!isRecord8(parsed)) {
8796
+ if (!isRecord9(parsed)) {
8599
8797
  return { ok: false, reason: "\u5DE5\u5177\u6210\u529F\u7ED3\u679C\u5FC5\u987B\u662F JSON \u5BF9\u8C61\u3002" };
8600
8798
  }
8601
8799
  if (!Object.prototype.hasOwnProperty.call(parsed, "data") || parsed.data === void 0) {
@@ -8623,7 +8821,7 @@ function validateStructuredToolResultContract(parsed) {
8623
8821
  };
8624
8822
  }
8625
8823
  function extractToolControlInfo(parsed) {
8626
- if (!isRecord8(parsed) || !isRecord8(parsed.control)) {
8824
+ if (!isRecord9(parsed) || !isRecord9(parsed.control)) {
8627
8825
  return void 0;
8628
8826
  }
8629
8827
  const control = parsed.control;
@@ -8647,7 +8845,7 @@ function buildRequireUserLocalState(params) {
8647
8845
  ...params.local,
8648
8846
  pendingToolCalls: params.remainingCalls,
8649
8847
  pendingInteractionSpec: {
8650
- ...isRecord8(params.parsed) && isRecord8(params.parsed.control) ? params.parsed.control : {},
8848
+ ...isRecord9(params.parsed) && isRecord9(params.parsed.control) ? params.parsed.control : {},
8651
8849
  // wait_user 必须携带工具已校验的结构化结果,UI 才能使用正式 form identity。
8652
8850
  form: params.parsed,
8653
8851
  toolCallId: params.toolCallId,
@@ -8686,7 +8884,7 @@ function buildErrorLocalState(params) {
8686
8884
 
8687
8885
  // src/runtime-kernel/tools/argNormalizer.ts
8688
8886
  var logger8 = new Logger("ToolArgNormalizer");
8689
- function isRecord9(v) {
8887
+ function isRecord10(v) {
8690
8888
  return !!v && typeof v === "object" && !Array.isArray(v);
8691
8889
  }
8692
8890
  function tryParseJsonEncodedValue(v) {
@@ -8779,12 +8977,12 @@ function normalizeByPropertySchema(prop, v, options) {
8779
8977
  }
8780
8978
  case "object": {
8781
8979
  const parsedValue = tryParseJsonEncodedValue(v);
8782
- if (parsedValue !== v && isRecord9(parsedValue)) {
8980
+ if (parsedValue !== v && isRecord10(parsedValue)) {
8783
8981
  logJsonEncodedNormalization("object", options);
8784
8982
  }
8785
- if (!isRecord9(parsedValue)) return parsedValue;
8983
+ if (!isRecord10(parsedValue)) return parsedValue;
8786
8984
  const selectedSchema = selectDiscriminatedObjectSchema(prop, parsedValue);
8787
- if (!selectedSchema.properties || !isRecord9(selectedSchema.properties)) return parsedValue;
8985
+ if (!selectedSchema.properties || !isRecord10(selectedSchema.properties)) return parsedValue;
8788
8986
  return normalizeToolArgs(
8789
8987
  { properties: selectedSchema.properties },
8790
8988
  parsedValue,
@@ -8806,7 +9004,7 @@ function normalizeToolArgs(schema, args, options = {}) {
8806
9004
  }
8807
9005
  return next;
8808
9006
  }
8809
- function isRecord10(v) {
9007
+ function isRecord11(v) {
8810
9008
  return !!v && typeof v === "object" && !Array.isArray(v);
8811
9009
  }
8812
9010
  function stableStringify(v) {
@@ -8816,7 +9014,7 @@ function stableStringify(v) {
8816
9014
  if (t === "string") return JSON.stringify(v);
8817
9015
  if (t === "number" || t === "boolean") return String(v);
8818
9016
  if (Array.isArray(v)) return `[${v.map(stableStringify).join(",")}]`;
8819
- if (isRecord10(v)) {
9017
+ if (isRecord11(v)) {
8820
9018
  const keys = Object.keys(v).sort();
8821
9019
  const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(v[k])}`);
8822
9020
  return `{${parts.join(",")}}`;
@@ -8853,13 +9051,13 @@ function findCachedToolOutputByIdempotencyKey(params) {
8853
9051
  if (e.tool_name !== params.toolName) continue;
8854
9052
  if (e.status !== "success") continue;
8855
9053
  const meta = e.metadata;
8856
- if (!isRecord10(meta)) continue;
9054
+ if (!isRecord11(meta)) continue;
8857
9055
  const idem = meta["idempotency"];
8858
- if (!isRecord10(idem)) continue;
9056
+ if (!isRecord11(idem)) continue;
8859
9057
  if (idem["key"] !== params.idempotencyKey) continue;
8860
9058
  const structuredResult = { data: e.data, observation: e.observation };
8861
9059
  const presentation = meta?.presentation;
8862
- if (isRecord10(presentation) && presentation["media"] !== void 0) {
9060
+ if (isRecord11(presentation) && presentation["media"] !== void 0) {
8863
9061
  structuredResult["media"] = presentation["media"];
8864
9062
  }
8865
9063
  return {
@@ -9194,7 +9392,7 @@ function parseToolArgs(call) {
9194
9392
  }
9195
9393
  try {
9196
9394
  const parsed = JSON.parse(rawArguments);
9197
- if (!isRecord8(parsed)) {
9395
+ if (!isRecord9(parsed)) {
9198
9396
  return {
9199
9397
  ok: false,
9200
9398
  error: "Tool arguments must decode to a JSON object."
@@ -9212,13 +9410,12 @@ function parseToolArgs(call) {
9212
9410
  }
9213
9411
  function prepareToolNodeContext(state) {
9214
9412
  const local = state.local || {};
9215
- const toolContext = isRecord8(local.toolContext) ? local.toolContext : {};
9216
- if (!isRecord8(local.toolContext)) {
9413
+ const toolContext = isRecord9(local.toolContext) ? local.toolContext : {};
9414
+ if (!isRecord9(local.toolContext)) {
9217
9415
  local.toolContext = toolContext;
9218
9416
  }
9219
9417
  const conversationId = requireRuntimeIdentity(local.conversationId, "conversationId");
9220
9418
  const turnId = requireRuntimeIdentity(local.turnId, "turnId");
9221
- parseRuntimeEvents(local.history ?? []);
9222
9419
  const existingConversationId = typeof toolContext.conversationId === "string" ? toolContext.conversationId.trim() : "";
9223
9420
  ensureToolContextRuntimeCapability({
9224
9421
  context: toolContext,
@@ -9486,11 +9683,11 @@ function createPendingToolCancellationEvents(params) {
9486
9683
  return events;
9487
9684
  }
9488
9685
  function buildCancelledToolBatchLocalState(local, runtimeEvents) {
9489
- const history = Array.isArray(local.history) ? local.history : [];
9686
+ const history = parseRuntimeEvents(local.history ?? []);
9490
9687
  return {
9491
9688
  ...stripAnswerState(local),
9492
9689
  pendingToolCalls: [],
9493
- history: [...history, ...runtimeEvents]
9690
+ history: appendWorkingHistory(history, runtimeEvents)
9494
9691
  };
9495
9692
  }
9496
9693
 
@@ -9566,14 +9763,14 @@ function readHistoryEvents(local) {
9566
9763
  }
9567
9764
  function readLastSuccessfulLlmModelId(local) {
9568
9765
  const executorLocal = local.executorLocal;
9569
- if (!isRecord8(executorLocal)) {
9766
+ if (!isRecord9(executorLocal)) {
9570
9767
  return void 0;
9571
9768
  }
9572
9769
  const value = executorLocal.lastSuccessfulLlmModelId;
9573
9770
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
9574
9771
  }
9575
9772
  function formatCapabilityError(error) {
9576
- if (isRecord8(error)) {
9773
+ if (isRecord9(error)) {
9577
9774
  const codeValue = error.errorCode ?? error.code;
9578
9775
  const code = typeof codeValue === "string" ? codeValue.trim() : "";
9579
9776
  const message = typeof error.message === "string" ? error.message.trim() : "";
@@ -10188,16 +10385,16 @@ var ToolNode = class {
10188
10385
  };
10189
10386
 
10190
10387
  // src/runtime-kernel/graph-engine/nodes/userNode.ts
10191
- function isRecord11(value) {
10388
+ function isRecord12(value) {
10192
10389
  return typeof value === "object" && value !== null;
10193
10390
  }
10194
10391
  function isRuntimeEvent(value) {
10195
- return isRecord11(value) && typeof value.type === "string";
10392
+ return isRecord12(value) && typeof value.type === "string";
10196
10393
  }
10197
10394
  var UserNode = class {
10198
10395
  id = "user";
10199
10396
  async run(state) {
10200
- const local = isRecord11(state.local) ? state.local : {};
10397
+ const local = isRecord12(state.local) ? state.local : {};
10201
10398
  const newEvents = Array.isArray(local.newEvents) ? local.newEvents.filter(isRuntimeEvent) : [];
10202
10399
  const userInputEvent = newEvents.find((event) => event.type === "user_input");
10203
10400
  if (!userInputEvent) {
@@ -10208,7 +10405,7 @@ var UserNode = class {
10208
10405
  };
10209
10406
 
10210
10407
  // src/runtime-kernel/graph-engine/nodes/waitUserNode.ts
10211
- function isRecord12(value) {
10408
+ function isRecord13(value) {
10212
10409
  return !!value && typeof value === "object" && !Array.isArray(value);
10213
10410
  }
10214
10411
  function requireNonEmptyString(value, field) {
@@ -10225,10 +10422,10 @@ var WaitUserNode = class {
10225
10422
  }
10226
10423
  async run(state) {
10227
10424
  const local = state.local ?? {};
10228
- const spec = isRecord12(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
10425
+ const spec = isRecord13(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
10229
10426
  const conversationId = requireNonEmptyString(local.conversationId, "conversationId");
10230
10427
  const turnId = requireNonEmptyString(local.turnId, "turnId");
10231
- const toolContext = isRecord12(local.toolContext) ? local.toolContext : {};
10428
+ const toolContext = isRecord13(local.toolContext) ? local.toolContext : {};
10232
10429
  const runId = RunIdSchema.parse(requireNonEmptyString(toolContext.runId, "toolContext.runId"));
10233
10430
  const parentRunId = toolContext.parentRunId === void 0 ? void 0 : RunIdSchema.parse(toolContext.parentRunId);
10234
10431
  const toolCallId = ToolCallIdSchema.parse(
@@ -10383,199 +10580,6 @@ __export(events_exports, {
10383
10580
  shouldReplayRuntimeEventToUi: () => shouldReplayRuntimeEventToUi
10384
10581
  });
10385
10582
 
10386
- // src/runtime-kernel/events/eventGovernance.ts
10387
- var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
10388
- "audit_envelope",
10389
- "final_answer_chunk",
10390
- "final_answer_reset",
10391
- "subrun_trace",
10392
- "context_usage_snapshot"
10393
- ];
10394
- function isRecord13(value) {
10395
- return !!value && typeof value === "object" && !Array.isArray(value);
10396
- }
10397
- function isToolCallDecisionEvent(event) {
10398
- return event.type === "tool_call_decision";
10399
- }
10400
- function isToolProcessEvent(event) {
10401
- return event.type === "tool_process";
10402
- }
10403
- function isSubRunTraceRuntimeEvent(event) {
10404
- return event.type === "subrun_trace";
10405
- }
10406
- function isRunExecutionMetricsRuntimeEvent(event) {
10407
- return event.type === "run_execution_metrics";
10408
- }
10409
- function isContextUsageSnapshotRuntimeEvent(event) {
10410
- return event.type === "context_usage_snapshot";
10411
- }
10412
- function isRequiresUserInteractionRuntimeEvent(event) {
10413
- return event.type === "requires_user_interaction";
10414
- }
10415
- function isAuditEnvelopeRuntimeEvent(event) {
10416
- return event.type === "audit_envelope";
10417
- }
10418
- function isControlRuntimeEvent(event) {
10419
- return event.type === "control";
10420
- }
10421
- function isHiddenUserInputEvent(event) {
10422
- if (event.type !== "user_input") {
10423
- return false;
10424
- }
10425
- const metadata = isRecord13(event.metadata) ? event.metadata : void 0;
10426
- const ui = metadata && isRecord13(metadata.ui) ? metadata.ui : void 0;
10427
- return ui?.presentation === "hidden";
10428
- }
10429
- function isEmptyTerminalAssistantRuntimeEvent(event) {
10430
- if (event.type !== "thought" && event.type !== "final_answer") {
10431
- return false;
10432
- }
10433
- return event.content.trim().length === 0;
10434
- }
10435
- function isConversationUiRuntimeEvent(event) {
10436
- return event.lane === "foreground" && event.visibility === "conversation";
10437
- }
10438
- function shouldPersistRuntimeEvent(event) {
10439
- return describeRuntimeEventLifecycle(event).persist;
10440
- }
10441
- function requirePersistableRuntimeEvent(event) {
10442
- if (!shouldPersistRuntimeEvent(event)) {
10443
- throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
10444
- }
10445
- return event;
10446
- }
10447
- function requirePersistableRoutedRuntimeEvent(event) {
10448
- requirePersistableRuntimeEvent(event);
10449
- return parseRoutedRuntimeEvent(event);
10450
- }
10451
- function shouldReplayRuntimeEventToUi(event) {
10452
- return describeRuntimeEventLifecycle(event).replayToUi;
10453
- }
10454
- function shouldEnterAgentContext(event) {
10455
- return describeRuntimeEventLifecycle(event).enterAgentContext;
10456
- }
10457
- function getRuntimeEventUiProjectionKind(event) {
10458
- if (isHiddenUserInputEvent(event)) {
10459
- return "hidden";
10460
- }
10461
- switch (event.type) {
10462
- case "user_input":
10463
- return "user_input";
10464
- case "thought":
10465
- return "thought";
10466
- case "final_answer":
10467
- return "final_answer";
10468
- case "final_answer_chunk":
10469
- return "final_answer_chunk";
10470
- case "final_answer_reset":
10471
- return "final_answer_reset";
10472
- case "tool_call_decision":
10473
- return "tool_call_decision";
10474
- case "tool_process":
10475
- return "tool_process";
10476
- case "tool_output":
10477
- return "tool_output";
10478
- case "requires_user_interaction":
10479
- return "requires_user_interaction";
10480
- case "audit_envelope":
10481
- return "audit_envelope";
10482
- case "subrun_trace":
10483
- return "subrun_trace";
10484
- case "error":
10485
- return "error";
10486
- case "context_usage_snapshot":
10487
- return "context_usage_snapshot";
10488
- case "run_execution_metrics":
10489
- return "run_execution_metrics";
10490
- case "history_summary":
10491
- return "history_summary";
10492
- default:
10493
- return "unsupported";
10494
- }
10495
- }
10496
- function describeRuntimeEventLifecycle(event) {
10497
- const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
10498
- const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
10499
- const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
10500
- const enterAgentContext = (() => {
10501
- if (uiProjectionKind === "unsupported") {
10502
- return false;
10503
- }
10504
- if (isSubRunTraceRuntimeEvent(event)) {
10505
- return false;
10506
- }
10507
- if (isRunExecutionMetricsRuntimeEvent(event)) {
10508
- return false;
10509
- }
10510
- if (isContextUsageSnapshotRuntimeEvent(event)) {
10511
- return false;
10512
- }
10513
- if (event.type === "error") {
10514
- return false;
10515
- }
10516
- if (isRequiresUserInteractionRuntimeEvent(event)) {
10517
- return false;
10518
- }
10519
- if (isAuditEnvelopeRuntimeEvent(event)) {
10520
- return false;
10521
- }
10522
- if (isControlRuntimeEvent(event)) {
10523
- return false;
10524
- }
10525
- if (isHiddenUserInputEvent(event)) {
10526
- return false;
10527
- }
10528
- if (isToolProcessEvent(event)) {
10529
- return false;
10530
- }
10531
- if (event.type === "thought") {
10532
- return false;
10533
- }
10534
- if (event.type === "final_answer_chunk") {
10535
- return false;
10536
- }
10537
- if (event.type === "final_answer_reset") {
10538
- return false;
10539
- }
10540
- if (isEmptyTerminalAssistantRuntimeEvent(event)) {
10541
- return false;
10542
- }
10543
- return true;
10544
- })();
10545
- const realtimeChannel = (() => {
10546
- switch (uiProjectionKind) {
10547
- case "thought":
10548
- case "final_answer":
10549
- case "final_answer_chunk":
10550
- case "final_answer_reset":
10551
- case "tool_call_decision":
10552
- case "tool_process":
10553
- case "tool_output":
10554
- case "requires_user_interaction":
10555
- case "subrun_trace":
10556
- case "error":
10557
- case "context_usage_snapshot":
10558
- case "run_execution_metrics":
10559
- case "history_summary":
10560
- return "event_bus_sse";
10561
- case "audit_envelope":
10562
- return "none";
10563
- default:
10564
- return "none";
10565
- }
10566
- })();
10567
- return {
10568
- uiProjectionKind,
10569
- persist,
10570
- replayToUi,
10571
- enterAgentContext,
10572
- realtimeChannel
10573
- };
10574
- }
10575
- function shouldEmitRuntimeEventToSse(event) {
10576
- return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
10577
- }
10578
-
10579
10583
  // src/runtime-kernel/events/finalAnswerCompletion.ts
10580
10584
  function findTerminalFinalAnswer(events) {
10581
10585
  for (let index = events.length - 1; index >= 0; index -= 1) {