@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/cli.js CHANGED
@@ -4907,10 +4907,7 @@ function createExecuteLlmStage(dependencies) {
4907
4907
  const promptBudget = ctx.promptBudget;
4908
4908
  const promptUsageMeasurementPolicy = ctx.promptUsageMeasurementPolicy;
4909
4909
  const streamEventHandler = ctx.input.stream && ctx.eventHandler ? (event) => {
4910
- const processedEvent = processStreamEvent(event, ctx.outputProcessor);
4911
- if (processedEvent) {
4912
- ctx.eventHandler?.(processedEvent);
4913
- }
4910
+ ctx.eventHandler?.(processStreamEvent(event, ctx.outputProcessor));
4914
4911
  } : void 0;
4915
4912
  const llmCallStartedAt = Date.now();
4916
4913
  let llmResp;
@@ -5066,9 +5063,6 @@ function processStreamEvent(event, outputProcessor) {
5066
5063
  return event;
5067
5064
  }
5068
5065
  const processedContent = outputProcessor.processStreamChunk(event.content);
5069
- if (processedContent.length === 0) {
5070
- return null;
5071
- }
5072
5066
  return {
5073
5067
  ...event,
5074
5068
  content: processedContent
@@ -6249,6 +6243,204 @@ var GraphAgentExecutor = class {
6249
6243
  }
6250
6244
  };
6251
6245
 
6246
+ // src/runtime-kernel/events/eventGovernance.ts
6247
+ var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
6248
+ "audit_envelope",
6249
+ "final_answer_chunk",
6250
+ "final_answer_reset",
6251
+ "subrun_trace",
6252
+ "context_usage_snapshot"
6253
+ ];
6254
+ function isRecord6(value) {
6255
+ return !!value && typeof value === "object" && !Array.isArray(value);
6256
+ }
6257
+ function isToolCallDecisionEvent(event) {
6258
+ return event.type === "tool_call_decision";
6259
+ }
6260
+ function isToolProcessEvent(event) {
6261
+ return event.type === "tool_process";
6262
+ }
6263
+ function isSubRunTraceRuntimeEvent(event) {
6264
+ return event.type === "subrun_trace";
6265
+ }
6266
+ function isRunExecutionMetricsRuntimeEvent(event) {
6267
+ return event.type === "run_execution_metrics";
6268
+ }
6269
+ function isContextUsageSnapshotRuntimeEvent(event) {
6270
+ return event.type === "context_usage_snapshot";
6271
+ }
6272
+ function isRequiresUserInteractionRuntimeEvent(event) {
6273
+ return event.type === "requires_user_interaction";
6274
+ }
6275
+ function isAuditEnvelopeRuntimeEvent(event) {
6276
+ return event.type === "audit_envelope";
6277
+ }
6278
+ function isControlRuntimeEvent(event) {
6279
+ return event.type === "control";
6280
+ }
6281
+ function isHiddenUserInputEvent(event) {
6282
+ if (event.type !== "user_input") {
6283
+ return false;
6284
+ }
6285
+ const metadata = isRecord6(event.metadata) ? event.metadata : void 0;
6286
+ const ui = metadata && isRecord6(metadata.ui) ? metadata.ui : void 0;
6287
+ return ui?.presentation === "hidden";
6288
+ }
6289
+ function isEmptyTerminalAssistantRuntimeEvent(event) {
6290
+ if (event.type !== "thought" && event.type !== "final_answer") {
6291
+ return false;
6292
+ }
6293
+ return event.content.trim().length === 0;
6294
+ }
6295
+ function isConversationUiRuntimeEvent(event) {
6296
+ return event.lane === "foreground" && event.visibility === "conversation";
6297
+ }
6298
+ function shouldPersistRuntimeEvent(event) {
6299
+ return describeRuntimeEventLifecycle(event).persist;
6300
+ }
6301
+ function requirePersistableRuntimeEvent(event) {
6302
+ if (!shouldPersistRuntimeEvent(event)) {
6303
+ throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
6304
+ }
6305
+ return event;
6306
+ }
6307
+ function requirePersistableRoutedRuntimeEvent(event) {
6308
+ requirePersistableRuntimeEvent(event);
6309
+ return parseRoutedRuntimeEvent(event);
6310
+ }
6311
+ function shouldReplayRuntimeEventToUi(event) {
6312
+ return describeRuntimeEventLifecycle(event).replayToUi;
6313
+ }
6314
+ function shouldEnterAgentContext(event) {
6315
+ return describeRuntimeEventLifecycle(event).enterAgentContext;
6316
+ }
6317
+ function getRuntimeEventUiProjectionKind(event) {
6318
+ if (isHiddenUserInputEvent(event)) {
6319
+ return "hidden";
6320
+ }
6321
+ switch (event.type) {
6322
+ case "user_input":
6323
+ return "user_input";
6324
+ case "thought":
6325
+ return "thought";
6326
+ case "final_answer":
6327
+ return "final_answer";
6328
+ case "final_answer_chunk":
6329
+ return "final_answer_chunk";
6330
+ case "final_answer_reset":
6331
+ return "final_answer_reset";
6332
+ case "tool_call_decision":
6333
+ return "tool_call_decision";
6334
+ case "tool_process":
6335
+ return "tool_process";
6336
+ case "tool_output":
6337
+ return "tool_output";
6338
+ case "requires_user_interaction":
6339
+ return "requires_user_interaction";
6340
+ case "audit_envelope":
6341
+ return "audit_envelope";
6342
+ case "subrun_trace":
6343
+ return "subrun_trace";
6344
+ case "error":
6345
+ return "error";
6346
+ case "context_usage_snapshot":
6347
+ return "context_usage_snapshot";
6348
+ case "run_execution_metrics":
6349
+ return "run_execution_metrics";
6350
+ case "history_summary":
6351
+ return "history_summary";
6352
+ default:
6353
+ return "unsupported";
6354
+ }
6355
+ }
6356
+ function describeRuntimeEventLifecycle(event) {
6357
+ const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
6358
+ const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
6359
+ const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
6360
+ const enterAgentContext = (() => {
6361
+ if (uiProjectionKind === "unsupported") {
6362
+ return false;
6363
+ }
6364
+ if (isSubRunTraceRuntimeEvent(event)) {
6365
+ return false;
6366
+ }
6367
+ if (isRunExecutionMetricsRuntimeEvent(event)) {
6368
+ return false;
6369
+ }
6370
+ if (isContextUsageSnapshotRuntimeEvent(event)) {
6371
+ return false;
6372
+ }
6373
+ if (event.type === "error") {
6374
+ return false;
6375
+ }
6376
+ if (isRequiresUserInteractionRuntimeEvent(event)) {
6377
+ return false;
6378
+ }
6379
+ if (isAuditEnvelopeRuntimeEvent(event)) {
6380
+ return false;
6381
+ }
6382
+ if (isControlRuntimeEvent(event)) {
6383
+ return false;
6384
+ }
6385
+ if (isHiddenUserInputEvent(event)) {
6386
+ return false;
6387
+ }
6388
+ if (isToolProcessEvent(event)) {
6389
+ return false;
6390
+ }
6391
+ if (event.type === "thought") {
6392
+ return false;
6393
+ }
6394
+ if (event.type === "final_answer_chunk") {
6395
+ return false;
6396
+ }
6397
+ if (event.type === "final_answer_reset") {
6398
+ return false;
6399
+ }
6400
+ if (isEmptyTerminalAssistantRuntimeEvent(event)) {
6401
+ return false;
6402
+ }
6403
+ return true;
6404
+ })();
6405
+ const realtimeChannel = (() => {
6406
+ switch (uiProjectionKind) {
6407
+ case "thought":
6408
+ case "final_answer":
6409
+ case "final_answer_chunk":
6410
+ case "final_answer_reset":
6411
+ case "tool_call_decision":
6412
+ case "tool_process":
6413
+ case "tool_output":
6414
+ case "requires_user_interaction":
6415
+ case "subrun_trace":
6416
+ case "error":
6417
+ case "context_usage_snapshot":
6418
+ case "run_execution_metrics":
6419
+ case "history_summary":
6420
+ return "event_bus_sse";
6421
+ case "audit_envelope":
6422
+ return "none";
6423
+ default:
6424
+ return "none";
6425
+ }
6426
+ })();
6427
+ return {
6428
+ uiProjectionKind,
6429
+ persist,
6430
+ replayToUi,
6431
+ enterAgentContext,
6432
+ realtimeChannel
6433
+ };
6434
+ }
6435
+ function shouldEmitRuntimeEventToSse(event) {
6436
+ return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
6437
+ }
6438
+
6439
+ // src/runtime-kernel/graph-engine/functions/appendWorkingHistory.ts
6440
+ function appendWorkingHistory(history, events) {
6441
+ return [...history.filter(shouldPersistRuntimeEvent), ...events.filter(shouldPersistRuntimeEvent)];
6442
+ }
6443
+
6252
6444
  // src/runtime-kernel/graph-engine/nodes/llmNode.state.ts
6253
6445
  function assertLlmNodeStreamChunkSequence(state, answerId, seq) {
6254
6446
  const answerChanged = state.answerId !== answerId;
@@ -6333,7 +6525,7 @@ function initLlmNodeState(init) {
6333
6525
  };
6334
6526
  }
6335
6527
  function buildLocalPatch(nodeState, ctx) {
6336
- const updatedHistory = [...ctx.history, ...nodeState.streamRuntimeEvents];
6528
+ const updatedHistory = appendWorkingHistory(ctx.history, nodeState.streamRuntimeEvents);
6337
6529
  const patch = {
6338
6530
  answerId: nodeState.answerId,
6339
6531
  chunkSeq: nodeState.chunkSeq,
@@ -6486,11 +6678,11 @@ var AgentEventSchema = z.discriminatedUnion("type", [
6486
6678
  });
6487
6679
 
6488
6680
  // src/runtime-kernel/events/provider-sidecar.ts
6489
- var isRecord6 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6681
+ var isRecord7 = (value) => !!value && typeof value === "object" && !Array.isArray(value);
6490
6682
  function readMetaFromEvent(event) {
6491
- if (!isRecord6(event)) return void 0;
6683
+ if (!isRecord7(event)) return void 0;
6492
6684
  const value = event["meta"];
6493
- return isRecord6(value) ? value : void 0;
6685
+ return isRecord7(value) ? value : void 0;
6494
6686
  }
6495
6687
 
6496
6688
  // src/runtime-kernel/events/agent-to-runtime.ts
@@ -6498,7 +6690,7 @@ function agentEventToRuntime(agentEvent, context, options = {}) {
6498
6690
  return mapAgentEventToRuntime(agentEvent, context, options);
6499
6691
  }
6500
6692
  function mapAgentEventToRuntime(agentEvent, context, options) {
6501
- if (isRecord6(agentEvent) && agentEvent["type"] === "history_summary") {
6693
+ if (isRecord7(agentEvent) && agentEvent["type"] === "history_summary") {
6502
6694
  const event = RuntimeEvent.parse(agentEvent);
6503
6695
  return context.metadata ? {
6504
6696
  ...event,
@@ -6639,9 +6831,7 @@ function mapToolProgressToRuntime(toolEvent, context, options, id, timestamp) {
6639
6831
  return event;
6640
6832
  }
6641
6833
  function mapStreamChunkToRuntime(streamEvent, context, id, timestamp) {
6642
- const text = streamEvent.content ?? "";
6643
- if (!text) return null;
6644
- const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, text, {
6834
+ const chunkEvent = createFinalAnswerChunkEvent(id, context.conversationId, context.turnId, streamEvent.answer_id, streamEvent.seq, streamEvent.content, {
6645
6835
  timestamp,
6646
6836
  is_last: streamEvent.is_last === true
6647
6837
  });
@@ -7161,7 +7351,7 @@ var LlmNode = class {
7161
7351
  ];
7162
7352
  logger7.info("[LlmNode] \u5386\u53F2\u4E8B\u4EF6\u5DF2\u66F4\u65B0", {
7163
7353
  previousCount: history.length,
7164
- nextCount: parseRuntimeEvents(patch.history).length,
7354
+ nextCount: patch.history.length,
7165
7355
  streamedEventCount: nodeState.streamRuntimeEvents.length
7166
7356
  });
7167
7357
  logger7.info("[LlmNode] \u6267\u884C\u5668\u51B3\u7B56\u5B8C\u6210", {
@@ -7208,7 +7398,7 @@ var ToolModelInputResolutionError = class extends Error {
7208
7398
  // src/runtime-kernel/tools/model-input/functions/parseToolModelInputDeclaration.ts
7209
7399
  var DECLARATION_KEYS = /* @__PURE__ */ new Set(["attachments"]);
7210
7400
  var SELECTION_KEYS = /* @__PURE__ */ new Set(["id", "uri", "label"]);
7211
- function isRecord7(value) {
7401
+ function isRecord8(value) {
7212
7402
  return value !== null && typeof value === "object" && !Array.isArray(value);
7213
7403
  }
7214
7404
  function hasOnlyKeys(value, allowed) {
@@ -7221,7 +7411,7 @@ function readTrimmedString(value, maxLength) {
7221
7411
  return value === value.trim() ? value : void 0;
7222
7412
  }
7223
7413
  function parseSelection(value, index) {
7224
- if (!isRecord7(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7414
+ if (!isRecord8(value) || !hasOnlyKeys(value, SELECTION_KEYS)) {
7225
7415
  return { ok: false, reason: `modelInput.attachments[${index}] \u5FC5\u987B\u662F strict selection\u3002` };
7226
7416
  }
7227
7417
  const id = readTrimmedString(value.id, 200);
@@ -7242,7 +7432,7 @@ function parseSelection(value, index) {
7242
7432
  return Object.freeze({ id, uri });
7243
7433
  }
7244
7434
  function parseToolModelInputDeclaration(value) {
7245
- if (!isRecord7(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7435
+ if (!isRecord8(value) || !hasOnlyKeys(value, DECLARATION_KEYS)) {
7246
7436
  return { ok: false, reason: "modelInput \u5FC5\u987B\u662F\u53EA\u5305\u542B attachments \u7684 strict \u5BF9\u8C61\u3002" };
7247
7437
  }
7248
7438
  if (!Array.isArray(value.attachments) || value.attachments.length === 0) {
@@ -7318,7 +7508,7 @@ function resolveFinalAnswerFromToolControl(finalAnswer) {
7318
7508
  }
7319
7509
 
7320
7510
  // src/runtime-kernel/graph-engine/nodes/toolNode.helpers.ts
7321
- function isRecord8(value) {
7511
+ function isRecord9(value) {
7322
7512
  return !!value && typeof value === "object" && !Array.isArray(value);
7323
7513
  }
7324
7514
  function readString3(value) {
@@ -7361,7 +7551,7 @@ function resolveToolObservationGovernancePolicy(policy) {
7361
7551
  }
7362
7552
  function readObservationPreviewMeta(parsed) {
7363
7553
  const meta = parsed["observationPreviewMeta"];
7364
- if (!isRecord8(meta)) {
7554
+ if (!isRecord9(meta)) {
7365
7555
  return void 0;
7366
7556
  }
7367
7557
  const filename = readString3(meta["filename"]);
@@ -7379,7 +7569,7 @@ function readObservationPreviewMeta(parsed) {
7379
7569
  };
7380
7570
  }
7381
7571
  async function applyObservationGovernance(params) {
7382
- if (!params.structuredObservation || !isRecord8(params.parsed)) {
7572
+ if (!params.structuredObservation || !isRecord9(params.parsed)) {
7383
7573
  throw new Error("Observation governance requires a validated structured tool result.");
7384
7574
  }
7385
7575
  const policy = resolveToolObservationGovernancePolicy(params.policy);
@@ -7471,7 +7661,7 @@ function createToolProtocolFuseError(nextCount, error) {
7471
7661
  // src/runtime-kernel/graph-engine/nodes/toolNode.stateTransitions.ts
7472
7662
  function mergeHistory(local, runtimeEvents) {
7473
7663
  const history = parseRuntimeEvents(local.history ?? []);
7474
- return [...history, ...runtimeEvents];
7664
+ return appendWorkingHistory(history, runtimeEvents);
7475
7665
  }
7476
7666
  function stripAnswerState(local) {
7477
7667
  const nextLocal = { ...local };
@@ -7480,7 +7670,7 @@ function stripAnswerState(local) {
7480
7670
  return nextLocal;
7481
7671
  }
7482
7672
  function validateStructuredToolResultContract(parsed) {
7483
- if (!isRecord8(parsed)) {
7673
+ if (!isRecord9(parsed)) {
7484
7674
  return { ok: false, reason: "\u5DE5\u5177\u6210\u529F\u7ED3\u679C\u5FC5\u987B\u662F JSON \u5BF9\u8C61\u3002" };
7485
7675
  }
7486
7676
  if (!Object.prototype.hasOwnProperty.call(parsed, "data") || parsed.data === void 0) {
@@ -7508,7 +7698,7 @@ function validateStructuredToolResultContract(parsed) {
7508
7698
  };
7509
7699
  }
7510
7700
  function extractToolControlInfo(parsed) {
7511
- if (!isRecord8(parsed) || !isRecord8(parsed.control)) {
7701
+ if (!isRecord9(parsed) || !isRecord9(parsed.control)) {
7512
7702
  return void 0;
7513
7703
  }
7514
7704
  const control = parsed.control;
@@ -7532,7 +7722,7 @@ function buildRequireUserLocalState(params) {
7532
7722
  ...params.local,
7533
7723
  pendingToolCalls: params.remainingCalls,
7534
7724
  pendingInteractionSpec: {
7535
- ...isRecord8(params.parsed) && isRecord8(params.parsed.control) ? params.parsed.control : {},
7725
+ ...isRecord9(params.parsed) && isRecord9(params.parsed.control) ? params.parsed.control : {},
7536
7726
  // wait_user 必须携带工具已校验的结构化结果,UI 才能使用正式 form identity。
7537
7727
  form: params.parsed,
7538
7728
  toolCallId: params.toolCallId,
@@ -7571,7 +7761,7 @@ function buildErrorLocalState(params) {
7571
7761
 
7572
7762
  // src/runtime-kernel/tools/argNormalizer.ts
7573
7763
  var logger8 = new Logger("ToolArgNormalizer");
7574
- function isRecord9(v) {
7764
+ function isRecord10(v) {
7575
7765
  return !!v && typeof v === "object" && !Array.isArray(v);
7576
7766
  }
7577
7767
  function tryParseJsonEncodedValue(v) {
@@ -7664,12 +7854,12 @@ function normalizeByPropertySchema(prop, v, options) {
7664
7854
  }
7665
7855
  case "object": {
7666
7856
  const parsedValue = tryParseJsonEncodedValue(v);
7667
- if (parsedValue !== v && isRecord9(parsedValue)) {
7857
+ if (parsedValue !== v && isRecord10(parsedValue)) {
7668
7858
  logJsonEncodedNormalization("object", options);
7669
7859
  }
7670
- if (!isRecord9(parsedValue)) return parsedValue;
7860
+ if (!isRecord10(parsedValue)) return parsedValue;
7671
7861
  const selectedSchema = selectDiscriminatedObjectSchema(prop, parsedValue);
7672
- if (!selectedSchema.properties || !isRecord9(selectedSchema.properties)) return parsedValue;
7862
+ if (!selectedSchema.properties || !isRecord10(selectedSchema.properties)) return parsedValue;
7673
7863
  return normalizeToolArgs(
7674
7864
  { properties: selectedSchema.properties },
7675
7865
  parsedValue,
@@ -7691,7 +7881,7 @@ function normalizeToolArgs(schema, args, options = {}) {
7691
7881
  }
7692
7882
  return next;
7693
7883
  }
7694
- function isRecord10(v) {
7884
+ function isRecord11(v) {
7695
7885
  return !!v && typeof v === "object" && !Array.isArray(v);
7696
7886
  }
7697
7887
  function stableStringify(v) {
@@ -7701,7 +7891,7 @@ function stableStringify(v) {
7701
7891
  if (t === "string") return JSON.stringify(v);
7702
7892
  if (t === "number" || t === "boolean") return String(v);
7703
7893
  if (Array.isArray(v)) return `[${v.map(stableStringify).join(",")}]`;
7704
- if (isRecord10(v)) {
7894
+ if (isRecord11(v)) {
7705
7895
  const keys = Object.keys(v).sort();
7706
7896
  const parts = keys.map((k) => `${JSON.stringify(k)}:${stableStringify(v[k])}`);
7707
7897
  return `{${parts.join(",")}}`;
@@ -7738,13 +7928,13 @@ function findCachedToolOutputByIdempotencyKey(params) {
7738
7928
  if (e.tool_name !== params.toolName) continue;
7739
7929
  if (e.status !== "success") continue;
7740
7930
  const meta = e.metadata;
7741
- if (!isRecord10(meta)) continue;
7931
+ if (!isRecord11(meta)) continue;
7742
7932
  const idem = meta["idempotency"];
7743
- if (!isRecord10(idem)) continue;
7933
+ if (!isRecord11(idem)) continue;
7744
7934
  if (idem["key"] !== params.idempotencyKey) continue;
7745
7935
  const structuredResult = { data: e.data, observation: e.observation };
7746
7936
  const presentation = meta?.presentation;
7747
- if (isRecord10(presentation) && presentation["media"] !== void 0) {
7937
+ if (isRecord11(presentation) && presentation["media"] !== void 0) {
7748
7938
  structuredResult["media"] = presentation["media"];
7749
7939
  }
7750
7940
  return {
@@ -8018,7 +8208,7 @@ function parseToolArgs(call) {
8018
8208
  }
8019
8209
  try {
8020
8210
  const parsed = JSON.parse(rawArguments);
8021
- if (!isRecord8(parsed)) {
8211
+ if (!isRecord9(parsed)) {
8022
8212
  return {
8023
8213
  ok: false,
8024
8214
  error: "Tool arguments must decode to a JSON object."
@@ -8036,13 +8226,12 @@ function parseToolArgs(call) {
8036
8226
  }
8037
8227
  function prepareToolNodeContext(state) {
8038
8228
  const local = state.local || {};
8039
- const toolContext = isRecord8(local.toolContext) ? local.toolContext : {};
8040
- if (!isRecord8(local.toolContext)) {
8229
+ const toolContext = isRecord9(local.toolContext) ? local.toolContext : {};
8230
+ if (!isRecord9(local.toolContext)) {
8041
8231
  local.toolContext = toolContext;
8042
8232
  }
8043
8233
  const conversationId = requireRuntimeIdentity(local.conversationId, "conversationId");
8044
8234
  const turnId = requireRuntimeIdentity(local.turnId, "turnId");
8045
- parseRuntimeEvents(local.history ?? []);
8046
8235
  const existingConversationId = typeof toolContext.conversationId === "string" ? toolContext.conversationId.trim() : "";
8047
8236
  ensureToolContextRuntimeCapability({
8048
8237
  context: toolContext,
@@ -8310,11 +8499,11 @@ function createPendingToolCancellationEvents(params) {
8310
8499
  return events;
8311
8500
  }
8312
8501
  function buildCancelledToolBatchLocalState(local, runtimeEvents) {
8313
- const history = Array.isArray(local.history) ? local.history : [];
8502
+ const history = parseRuntimeEvents(local.history ?? []);
8314
8503
  return {
8315
8504
  ...stripAnswerState(local),
8316
8505
  pendingToolCalls: [],
8317
- history: [...history, ...runtimeEvents]
8506
+ history: appendWorkingHistory(history, runtimeEvents)
8318
8507
  };
8319
8508
  }
8320
8509
  function recordToolProtocolError(params) {
@@ -8383,14 +8572,14 @@ function readHistoryEvents(local) {
8383
8572
  }
8384
8573
  function readLastSuccessfulLlmModelId(local) {
8385
8574
  const executorLocal = local.executorLocal;
8386
- if (!isRecord8(executorLocal)) {
8575
+ if (!isRecord9(executorLocal)) {
8387
8576
  return void 0;
8388
8577
  }
8389
8578
  const value = executorLocal.lastSuccessfulLlmModelId;
8390
8579
  return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
8391
8580
  }
8392
8581
  function formatCapabilityError(error) {
8393
- if (isRecord8(error)) {
8582
+ if (isRecord9(error)) {
8394
8583
  const codeValue = error.errorCode ?? error.code;
8395
8584
  const code = typeof codeValue === "string" ? codeValue.trim() : "";
8396
8585
  const message = typeof error.message === "string" ? error.message.trim() : "";
@@ -9005,16 +9194,16 @@ var ToolNode = class {
9005
9194
  };
9006
9195
 
9007
9196
  // src/runtime-kernel/graph-engine/nodes/userNode.ts
9008
- function isRecord11(value) {
9197
+ function isRecord12(value) {
9009
9198
  return typeof value === "object" && value !== null;
9010
9199
  }
9011
9200
  function isRuntimeEvent(value) {
9012
- return isRecord11(value) && typeof value.type === "string";
9201
+ return isRecord12(value) && typeof value.type === "string";
9013
9202
  }
9014
9203
  var UserNode = class {
9015
9204
  id = "user";
9016
9205
  async run(state) {
9017
- const local = isRecord11(state.local) ? state.local : {};
9206
+ const local = isRecord12(state.local) ? state.local : {};
9018
9207
  const newEvents = Array.isArray(local.newEvents) ? local.newEvents.filter(isRuntimeEvent) : [];
9019
9208
  const userInputEvent = newEvents.find((event) => event.type === "user_input");
9020
9209
  if (!userInputEvent) {
@@ -9025,7 +9214,7 @@ var UserNode = class {
9025
9214
  };
9026
9215
 
9027
9216
  // src/runtime-kernel/graph-engine/nodes/waitUserNode.ts
9028
- function isRecord12(value) {
9217
+ function isRecord13(value) {
9029
9218
  return !!value && typeof value === "object" && !Array.isArray(value);
9030
9219
  }
9031
9220
  function requireNonEmptyString(value, field) {
@@ -9042,10 +9231,10 @@ var WaitUserNode = class {
9042
9231
  }
9043
9232
  async run(state) {
9044
9233
  const local = state.local ?? {};
9045
- const spec = isRecord12(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9234
+ const spec = isRecord13(local.pendingInteractionSpec) ? local.pendingInteractionSpec : {};
9046
9235
  const conversationId = requireNonEmptyString(local.conversationId, "conversationId");
9047
9236
  const turnId = requireNonEmptyString(local.turnId, "turnId");
9048
- const toolContext = isRecord12(local.toolContext) ? local.toolContext : {};
9237
+ const toolContext = isRecord13(local.toolContext) ? local.toolContext : {};
9049
9238
  const runId = RunIdSchema.parse(requireNonEmptyString(toolContext.runId, "toolContext.runId"));
9050
9239
  const parentRunId = toolContext.parentRunId === void 0 ? void 0 : RunIdSchema.parse(toolContext.parentRunId);
9051
9240
  const toolCallId = ToolCallIdSchema.parse(
@@ -9200,199 +9389,6 @@ __export(events_exports, {
9200
9389
  shouldReplayRuntimeEventToUi: () => shouldReplayRuntimeEventToUi
9201
9390
  });
9202
9391
 
9203
- // src/runtime-kernel/events/eventGovernance.ts
9204
- var RUNTIME_EVENT_TYPES_NEVER_REPLAYED_TO_UI = [
9205
- "audit_envelope",
9206
- "final_answer_chunk",
9207
- "final_answer_reset",
9208
- "subrun_trace",
9209
- "context_usage_snapshot"
9210
- ];
9211
- function isRecord13(value) {
9212
- return !!value && typeof value === "object" && !Array.isArray(value);
9213
- }
9214
- function isToolCallDecisionEvent(event) {
9215
- return event.type === "tool_call_decision";
9216
- }
9217
- function isToolProcessEvent(event) {
9218
- return event.type === "tool_process";
9219
- }
9220
- function isSubRunTraceRuntimeEvent(event) {
9221
- return event.type === "subrun_trace";
9222
- }
9223
- function isRunExecutionMetricsRuntimeEvent(event) {
9224
- return event.type === "run_execution_metrics";
9225
- }
9226
- function isContextUsageSnapshotRuntimeEvent(event) {
9227
- return event.type === "context_usage_snapshot";
9228
- }
9229
- function isRequiresUserInteractionRuntimeEvent(event) {
9230
- return event.type === "requires_user_interaction";
9231
- }
9232
- function isAuditEnvelopeRuntimeEvent(event) {
9233
- return event.type === "audit_envelope";
9234
- }
9235
- function isControlRuntimeEvent(event) {
9236
- return event.type === "control";
9237
- }
9238
- function isHiddenUserInputEvent(event) {
9239
- if (event.type !== "user_input") {
9240
- return false;
9241
- }
9242
- const metadata = isRecord13(event.metadata) ? event.metadata : void 0;
9243
- const ui = metadata && isRecord13(metadata.ui) ? metadata.ui : void 0;
9244
- return ui?.presentation === "hidden";
9245
- }
9246
- function isEmptyTerminalAssistantRuntimeEvent(event) {
9247
- if (event.type !== "thought" && event.type !== "final_answer") {
9248
- return false;
9249
- }
9250
- return event.content.trim().length === 0;
9251
- }
9252
- function isConversationUiRuntimeEvent(event) {
9253
- return event.lane === "foreground" && event.visibility === "conversation";
9254
- }
9255
- function shouldPersistRuntimeEvent(event) {
9256
- return describeRuntimeEventLifecycle(event).persist;
9257
- }
9258
- function requirePersistableRuntimeEvent(event) {
9259
- if (!shouldPersistRuntimeEvent(event)) {
9260
- throw new Error(`RuntimeEvent ${event.id} (${event.type}) is not eligible for persistence.`);
9261
- }
9262
- return event;
9263
- }
9264
- function requirePersistableRoutedRuntimeEvent(event) {
9265
- requirePersistableRuntimeEvent(event);
9266
- return parseRoutedRuntimeEvent(event);
9267
- }
9268
- function shouldReplayRuntimeEventToUi(event) {
9269
- return describeRuntimeEventLifecycle(event).replayToUi;
9270
- }
9271
- function shouldEnterAgentContext(event) {
9272
- return describeRuntimeEventLifecycle(event).enterAgentContext;
9273
- }
9274
- function getRuntimeEventUiProjectionKind(event) {
9275
- if (isHiddenUserInputEvent(event)) {
9276
- return "hidden";
9277
- }
9278
- switch (event.type) {
9279
- case "user_input":
9280
- return "user_input";
9281
- case "thought":
9282
- return "thought";
9283
- case "final_answer":
9284
- return "final_answer";
9285
- case "final_answer_chunk":
9286
- return "final_answer_chunk";
9287
- case "final_answer_reset":
9288
- return "final_answer_reset";
9289
- case "tool_call_decision":
9290
- return "tool_call_decision";
9291
- case "tool_process":
9292
- return "tool_process";
9293
- case "tool_output":
9294
- return "tool_output";
9295
- case "requires_user_interaction":
9296
- return "requires_user_interaction";
9297
- case "audit_envelope":
9298
- return "audit_envelope";
9299
- case "subrun_trace":
9300
- return "subrun_trace";
9301
- case "error":
9302
- return "error";
9303
- case "context_usage_snapshot":
9304
- return "context_usage_snapshot";
9305
- case "run_execution_metrics":
9306
- return "run_execution_metrics";
9307
- case "history_summary":
9308
- return "history_summary";
9309
- default:
9310
- return "unsupported";
9311
- }
9312
- }
9313
- function describeRuntimeEventLifecycle(event) {
9314
- const uiProjectionKind = getRuntimeEventUiProjectionKind(event);
9315
- const persist = event.ephemeral !== true && !isToolProcessEvent(event) && !isSubRunTraceRuntimeEvent(event);
9316
- const replayToUi = persist && isConversationUiRuntimeEvent(event) && uiProjectionKind !== "hidden" && uiProjectionKind !== "audit_envelope" && uiProjectionKind !== "final_answer_chunk" && uiProjectionKind !== "final_answer_reset" && uiProjectionKind !== "unsupported";
9317
- const enterAgentContext = (() => {
9318
- if (uiProjectionKind === "unsupported") {
9319
- return false;
9320
- }
9321
- if (isSubRunTraceRuntimeEvent(event)) {
9322
- return false;
9323
- }
9324
- if (isRunExecutionMetricsRuntimeEvent(event)) {
9325
- return false;
9326
- }
9327
- if (isContextUsageSnapshotRuntimeEvent(event)) {
9328
- return false;
9329
- }
9330
- if (event.type === "error") {
9331
- return false;
9332
- }
9333
- if (isRequiresUserInteractionRuntimeEvent(event)) {
9334
- return false;
9335
- }
9336
- if (isAuditEnvelopeRuntimeEvent(event)) {
9337
- return false;
9338
- }
9339
- if (isControlRuntimeEvent(event)) {
9340
- return false;
9341
- }
9342
- if (isHiddenUserInputEvent(event)) {
9343
- return false;
9344
- }
9345
- if (isToolProcessEvent(event)) {
9346
- return false;
9347
- }
9348
- if (event.type === "thought") {
9349
- return false;
9350
- }
9351
- if (event.type === "final_answer_chunk") {
9352
- return false;
9353
- }
9354
- if (event.type === "final_answer_reset") {
9355
- return false;
9356
- }
9357
- if (isEmptyTerminalAssistantRuntimeEvent(event)) {
9358
- return false;
9359
- }
9360
- return true;
9361
- })();
9362
- const realtimeChannel = (() => {
9363
- switch (uiProjectionKind) {
9364
- case "thought":
9365
- case "final_answer":
9366
- case "final_answer_chunk":
9367
- case "final_answer_reset":
9368
- case "tool_call_decision":
9369
- case "tool_process":
9370
- case "tool_output":
9371
- case "requires_user_interaction":
9372
- case "subrun_trace":
9373
- case "error":
9374
- case "context_usage_snapshot":
9375
- case "run_execution_metrics":
9376
- case "history_summary":
9377
- return "event_bus_sse";
9378
- case "audit_envelope":
9379
- return "none";
9380
- default:
9381
- return "none";
9382
- }
9383
- })();
9384
- return {
9385
- uiProjectionKind,
9386
- persist,
9387
- replayToUi,
9388
- enterAgentContext,
9389
- realtimeChannel
9390
- };
9391
- }
9392
- function shouldEmitRuntimeEventToSse(event) {
9393
- return describeRuntimeEventLifecycle(event).realtimeChannel === "event_bus_sse";
9394
- }
9395
-
9396
9392
  // src/runtime-kernel/events/finalAnswerCompletion.ts
9397
9393
  function findTerminalFinalAnswer(events) {
9398
9394
  for (let index = events.length - 1; index >= 0; index -= 1) {