@linnlabs/linnkit 0.43.0 → 0.45.0

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 (93) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/dist/cli.cjs +152 -6
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +152 -6
  5. package/dist/cli.js.map +1 -1
  6. package/dist/context-manager/profiles/agent/context/config.d.cts +1 -1
  7. package/dist/context-manager/profiles/agent/context/config.d.ts +1 -1
  8. package/dist/context-manager.cjs +37 -4
  9. package/dist/context-manager.cjs.map +1 -1
  10. package/dist/context-manager.js +37 -4
  11. package/dist/context-manager.js.map +1 -1
  12. package/dist/contracts/agentSpec.d.cts +5 -5
  13. package/dist/contracts/agentSpec.d.ts +5 -5
  14. package/dist/contracts/audit.d.cts +10 -10
  15. package/dist/contracts/audit.d.ts +10 -10
  16. package/dist/contracts/contextPolicy.d.cts +14 -14
  17. package/dist/contracts/contextPolicy.d.ts +14 -14
  18. package/dist/contracts/events.d.cts +121 -117
  19. package/dist/contracts/events.d.ts +121 -117
  20. package/dist/contracts/execution.d.cts +2 -2
  21. package/dist/contracts/execution.d.ts +2 -2
  22. package/dist/contracts/llm-retry.d.cts +2 -2
  23. package/dist/contracts/llm-retry.d.ts +2 -2
  24. package/dist/contracts/messages.d.cts +256 -32
  25. package/dist/contracts/messages.d.ts +256 -32
  26. package/dist/contracts/sse.d.cts +161 -97
  27. package/dist/contracts/sse.d.ts +161 -97
  28. package/dist/contracts/sub-run-trace-payload.d.cts +8 -8
  29. package/dist/contracts/sub-run-trace-payload.d.ts +8 -8
  30. package/dist/contracts/token-accounting.d.cts +12 -12
  31. package/dist/contracts/token-accounting.d.ts +12 -12
  32. package/dist/contracts.cjs +19 -2
  33. package/dist/contracts.cjs.map +1 -1
  34. package/dist/contracts.d.cts +1 -1
  35. package/dist/contracts.d.ts +1 -1
  36. package/dist/contracts.js +19 -3
  37. package/dist/contracts.js.map +1 -1
  38. package/dist/index.cjs +154 -6
  39. package/dist/index.cjs.map +1 -1
  40. package/dist/index.js +154 -6
  41. package/dist/index.js.map +1 -1
  42. package/dist/quickstart.cjs +152 -6
  43. package/dist/quickstart.cjs.map +1 -1
  44. package/dist/quickstart.js +152 -6
  45. package/dist/quickstart.js.map +1 -1
  46. package/dist/runtime-kernel/events/agentEvents.d.cts +2 -2
  47. package/dist/runtime-kernel/events/agentEvents.d.ts +2 -2
  48. package/dist/runtime-kernel/events.cjs +29 -2
  49. package/dist/runtime-kernel/events.cjs.map +1 -1
  50. package/dist/runtime-kernel/events.js +29 -2
  51. package/dist/runtime-kernel/events.js.map +1 -1
  52. package/dist/runtime-kernel/graph-engine/definitions/pendingUserInput.d.cts +27 -0
  53. package/dist/runtime-kernel/graph-engine/definitions/pendingUserInput.d.ts +27 -0
  54. package/dist/runtime-kernel/graph-engine/engine.d.cts +1 -1
  55. package/dist/runtime-kernel/graph-engine/engine.d.ts +1 -1
  56. package/dist/runtime-kernel/graph-engine/index.d.cts +1 -0
  57. package/dist/runtime-kernel/graph-engine/index.d.ts +1 -0
  58. package/dist/runtime-kernel/graph-engine/nodes/toolNode.d.cts +3 -0
  59. package/dist/runtime-kernel/graph-engine/nodes/toolNode.d.ts +3 -0
  60. package/dist/runtime-kernel/graph-engine/types.d.cts +3 -0
  61. package/dist/runtime-kernel/graph-engine/types.d.ts +3 -0
  62. package/dist/runtime-kernel.cjs +152 -6
  63. package/dist/runtime-kernel.cjs.map +1 -1
  64. package/dist/runtime-kernel.d.cts +1 -0
  65. package/dist/runtime-kernel.d.ts +1 -0
  66. package/dist/runtime-kernel.js +152 -6
  67. package/dist/runtime-kernel.js.map +1 -1
  68. package/dist/testkit/agent-harness/scriptedInferenceHarness.d.cts +24 -6
  69. package/dist/testkit/agent-harness/scriptedInferenceHarness.d.ts +24 -6
  70. package/dist/testkit/scenario/graphScenario.d.cts +89 -0
  71. package/dist/testkit/scenario/graphScenario.d.ts +89 -0
  72. package/dist/testkit/scenario/pendingUserInputQueue.d.cts +26 -0
  73. package/dist/testkit/scenario/pendingUserInputQueue.d.ts +26 -0
  74. package/dist/testkit/scenario/runtimeEventRecorder.d.cts +36 -0
  75. package/dist/testkit/scenario/runtimeEventRecorder.d.ts +36 -0
  76. package/dist/testkit/scenario/scriptedToolRuntime.d.cts +51 -0
  77. package/dist/testkit/scenario/scriptedToolRuntime.d.ts +51 -0
  78. package/dist/testkit/scenario/steeringInvariants.d.cts +33 -0
  79. package/dist/testkit/scenario/steeringInvariants.d.ts +33 -0
  80. package/dist/testkit/scenario/timeline.d.cts +27 -0
  81. package/dist/testkit/scenario/timeline.d.ts +27 -0
  82. package/dist/testkit.cjs +722 -19
  83. package/dist/testkit.cjs.map +1 -1
  84. package/dist/testkit.d.cts +7 -1
  85. package/dist/testkit.d.ts +7 -1
  86. package/dist/testkit.js +714 -20
  87. package/dist/testkit.js.map +1 -1
  88. package/docs/integration/README.md +1 -0
  89. package/docs/integration/mid-run-user-input.md +73 -0
  90. package/docs/integration/realtime.md +4 -0
  91. package/docs/integration/run-supervisor.md +1 -0
  92. package/docs/integration/testing.md +40 -4
  93. package/package.json +2 -2
@@ -311,6 +311,10 @@ var UserInputMessage = BaseMessage.extend({
311
311
  type: zod.z.literal("user_input"),
312
312
  attachments: RuntimeResourceRefs.optional()
313
313
  });
314
+ var UserSteerMessage = BaseMessage.extend({
315
+ role: zod.z.literal("user"),
316
+ type: zod.z.literal("user_steer")
317
+ });
314
318
  var UserContextMessage = BaseMessage.extend({
315
319
  role: zod.z.literal("user"),
316
320
  // 产品上下文种类由 Host fence metadata 表达,不进入框架消息枚举。
@@ -319,7 +323,7 @@ var UserContextMessage = BaseMessage.extend({
319
323
  "task_request"
320
324
  ])
321
325
  });
322
- var UserMessage = zod.z.union([UserInputMessage, UserContextMessage]);
326
+ var UserMessage = zod.z.union([UserInputMessage, UserSteerMessage, UserContextMessage]);
323
327
  var AssistantMessage = BaseMessage.extend({
324
328
  role: zod.z.literal("assistant"),
325
329
  type: zod.z.enum(["thought", "final_answer", "tool_code", "tool_calls", "task_completion"])
@@ -1036,7 +1040,12 @@ var RuntimeEventShape = zod.z.discriminatedUnion("type", [
1036
1040
  content: zod.z.string(),
1037
1041
  raw_content: zod.z.string().optional(),
1038
1042
  source: zod.z.enum(["user", "editor", "system"]).default("user"),
1039
- attachments: RuntimeResourceRefs.optional()
1043
+ attachments: RuntimeResourceRefs.optional(),
1044
+ /**
1045
+ * 运行中途送达的用户输入(插话)。缺省表示运行起点的输入。
1046
+ * 上下文里投影为 `user_steer`,不改变“当前轮从哪条 user_input 开始”的判定。
1047
+ */
1048
+ delivery: zod.z.literal("mid_run").optional()
1040
1049
  }),
1041
1050
  BaseEvent.extend({
1042
1051
  type: zod.z.literal("thought"),
@@ -1483,6 +1492,11 @@ zod.z.object({
1483
1492
  run_id: RunIdSchema,
1484
1493
  execution_id: ExecutionIdSchema
1485
1494
  });
1495
+ var SSEUserInputEvent = BaseSSEEvent.extend({
1496
+ type: zod.z.literal("user_input"),
1497
+ content: zod.z.string(),
1498
+ delivery: zod.z.literal("mid_run")
1499
+ });
1486
1500
  var SSEThoughtEvent = BaseSSEEvent.extend({
1487
1501
  type: zod.z.literal("thought"),
1488
1502
  thought_message_id: ThoughtMessageIdSchema.optional(),
@@ -1637,6 +1651,7 @@ var SSESummarizationErrorEvent = BaseSSEEvent.extend({
1637
1651
  error: zod.z.string()
1638
1652
  });
1639
1653
  zod.z.discriminatedUnion("type", [
1654
+ SSEUserInputEvent,
1640
1655
  SSEThoughtEvent,
1641
1656
  SSEFinalAnswerChunkEvent,
1642
1657
  SSEFinalAnswerResetEvent,
@@ -2655,6 +2670,7 @@ var NON_CHECKPOINT_LOCAL_KEYS = [
2655
2670
  "runtimeEventCommitPort",
2656
2671
  "commitExecutionBoundary",
2657
2672
  "toolRecoveryPort",
2673
+ "pendingUserInputPort",
2658
2674
  "runtimeFailureFactSink",
2659
2675
  "signal",
2660
2676
  "summarizationCallbacks",
@@ -6647,6 +6663,8 @@ function describeRuntimeEventLifecycle(event) {
6647
6663
  case "run_execution_metrics":
6648
6664
  case "history_summary":
6649
6665
  return "event_bus_sse";
6666
+ case "user_input":
6667
+ return event.type === "user_input" && event.delivery === "mid_run" ? "event_bus_sse" : "none";
6650
6668
  case "audit_envelope":
6651
6669
  return "none";
6652
6670
  default:
@@ -7090,6 +7108,16 @@ function requireEventTimestamp(value) {
7090
7108
  function applyRuntimeEventToMemory(event, memory) {
7091
7109
  switch (event.type) {
7092
7110
  case "user_input": {
7111
+ if (event.delivery === "mid_run") {
7112
+ memory.appendMessage({
7113
+ id: event.id,
7114
+ role: "user",
7115
+ type: "user_steer",
7116
+ content: event.content,
7117
+ timestamp: event.timestamp
7118
+ });
7119
+ break;
7120
+ }
7093
7121
  memory.addUserMessage(event.content, event.id, event.attachments);
7094
7122
  break;
7095
7123
  }
@@ -8706,9 +8734,49 @@ function recordToolProtocolError(params) {
8706
8734
  }
8707
8735
 
8708
8736
  // src/runtime-kernel/graph-engine/nodes/toolNode.recovery.ts
8737
+ var DEFAULT_TOOL_ABORT_GRACE_MS = 3e3;
8738
+ function settleToolWithinAbortGrace(input) {
8739
+ const { execution, signal, graceMs } = input;
8740
+ if (!signal) return execution;
8741
+ return new Promise((resolve, reject) => {
8742
+ let timer;
8743
+ const cleanup = () => {
8744
+ if (timer !== void 0) clearTimeout(timer);
8745
+ signal.removeEventListener("abort", startGrace);
8746
+ };
8747
+ function startGrace() {
8748
+ timer = setTimeout(() => {
8749
+ cleanup();
8750
+ execution.catch(() => void 0);
8751
+ input.onAbandon?.();
8752
+ const error = new Error("Tool did not stop within the abort grace period; its late outcome is discarded.");
8753
+ error.name = "AbortError";
8754
+ reject(error);
8755
+ }, graceMs);
8756
+ }
8757
+ if (signal.aborted) startGrace();
8758
+ else signal.addEventListener("abort", startGrace, { once: true });
8759
+ execution.then(
8760
+ (value) => {
8761
+ cleanup();
8762
+ resolve(value);
8763
+ },
8764
+ (error) => {
8765
+ cleanup();
8766
+ reject(error);
8767
+ }
8768
+ );
8769
+ });
8770
+ }
8709
8771
  async function executeRecoverableTool(input) {
8710
8772
  const local = input.state.local;
8711
- if (!local?.commitExecutionBoundary) return input.execute();
8773
+ const run = () => settleToolWithinAbortGrace({
8774
+ execution: input.execute(),
8775
+ signal: local?.signal,
8776
+ graceMs: input.abortGraceMs ?? DEFAULT_TOOL_ABORT_GRACE_MS,
8777
+ onAbandon: input.onAbandon
8778
+ });
8779
+ if (!local?.commitExecutionBoundary) return run();
8712
8780
  if (local.executingToolCallId) {
8713
8781
  if (local.executingToolCallId !== input.call.id) {
8714
8782
  throw new RunRecoveryBlockedError("Pending tool identity does not match execution intent");
@@ -8721,7 +8789,7 @@ async function executeRecoverableTool(input) {
8721
8789
  }
8722
8790
  local.executingToolCallId = input.call.id;
8723
8791
  await local.commitExecutionBoundary(input.state);
8724
- const result = await input.execute();
8792
+ const result = await run();
8725
8793
  if (!result.success && isRunPauseSignal(local.signal)) {
8726
8794
  const error = new Error("Tool execution paused before its outcome was confirmed");
8727
8795
  error.name = "AbortError";
@@ -8737,6 +8805,62 @@ async function commitToolBoundary(state, result) {
8737
8805
  await state.local.commitExecutionBoundary(state);
8738
8806
  }
8739
8807
 
8808
+ // src/runtime-kernel/graph-engine/nodes/toolNode.pendingUserInput.ts
8809
+ function isPendingUserInputPort(value) {
8810
+ return !!value && typeof value === "object" && typeof Reflect.get(value, "claim") === "function" && typeof Reflect.get(value, "confirm") === "function" && typeof Reflect.get(value, "release") === "function";
8811
+ }
8812
+ function readRemainingSteps(state) {
8813
+ const value = state.local?.executorLocal?.remainingSteps;
8814
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
8815
+ }
8816
+ function readIdentity(value, field) {
8817
+ if (typeof value !== "string" || value.trim().length === 0) {
8818
+ throw new Error(`ToolNode pending user input requires ${field}`);
8819
+ }
8820
+ return value;
8821
+ }
8822
+ async function claimPendingUserInputs(state, result) {
8823
+ if (result.kind !== "route" || result.nextNodeId !== "llm") return void 0;
8824
+ const local = state.local;
8825
+ const port = local?.pendingUserInputPort;
8826
+ if (!isPendingUserInputPort(port)) return void 0;
8827
+ const signal = local?.signal;
8828
+ if (signal?.aborted || isRunPauseSignal(signal)) return void 0;
8829
+ const remainingSteps = readRemainingSteps(state);
8830
+ if (remainingSteps !== void 0 && remainingSteps < 1) return void 0;
8831
+ const inputs = await port.claim();
8832
+ if (inputs.length === 0) return void 0;
8833
+ const ids = inputs.map((input) => input.id);
8834
+ try {
8835
+ const conversationId = readIdentity(local?.conversationId, "conversationId");
8836
+ const turnId = readIdentity(local?.turnId, "turnId");
8837
+ const sink = requireRuntimeEventSink(local);
8838
+ const events = inputs.map((input) => sink(
8839
+ createUserInputEvent(readIdentity(input.id, "input id"), conversationId, turnId, input.content, {
8840
+ timestamp: input.timestamp ?? Date.now(),
8841
+ delivery: "mid_run"
8842
+ }),
8843
+ "ToolNode.pending_user_input"
8844
+ ));
8845
+ state.local = {
8846
+ ...state.local,
8847
+ history: appendWorkingHistory(parseRuntimeEvents(state.local?.history ?? []), events)
8848
+ };
8849
+ return {
8850
+ events,
8851
+ confirm: async () => {
8852
+ await port.confirm(ids);
8853
+ },
8854
+ release: async () => {
8855
+ await port.release(ids);
8856
+ }
8857
+ };
8858
+ } catch (error) {
8859
+ await port.release(ids);
8860
+ throw error;
8861
+ }
8862
+ }
8863
+
8740
8864
  // src/runtime-kernel/graph-engine/nodes/toolNode.ts
8741
8865
  var logger11 = new Logger("ToolNode");
8742
8866
  function isAbortSignal(value) {
@@ -8785,7 +8909,9 @@ var ToolNode = class {
8785
8909
  auditPort;
8786
8910
  modelInputResolver;
8787
8911
  idempotencyInFlight = /* @__PURE__ */ new Map();
8912
+ abortGraceMs;
8788
8913
  constructor(dependencies) {
8914
+ this.abortGraceMs = dependencies.abortGraceMs;
8789
8915
  this.toolRuntime = dependencies.toolRuntime;
8790
8916
  this.observationPreview = dependencies.observationPreview;
8791
8917
  this.telemetryPort = dependencies.telemetryPort ?? noopTelemetry;
@@ -8904,7 +9030,19 @@ var ToolNode = class {
8904
9030
  });
8905
9031
  throw result.error;
8906
9032
  }
8907
- await commitToolBoundary(state, result);
9033
+ const steering = await claimPendingUserInputs(state, result);
9034
+ if (steering) {
9035
+ result = { ...result, events: [...result.events ?? [], ...steering.events] };
9036
+ try {
9037
+ await commitToolBoundary(state, result);
9038
+ } catch (error) {
9039
+ await steering.release();
9040
+ throw error;
9041
+ }
9042
+ await steering.confirm();
9043
+ } else {
9044
+ await commitToolBoundary(state, result);
9045
+ }
8908
9046
  if (Array.isArray(result.events) && result.events.length > 0) {
8909
9047
  events.push(...result.events);
8910
9048
  }
@@ -8991,6 +9129,13 @@ var ToolNode = class {
8991
9129
  state,
8992
9130
  call,
8993
9131
  context: prepared.toolContext,
9132
+ abortGraceMs: this.abortGraceMs,
9133
+ onAbandon: () => logger11.warn("[ToolNode] \u5DE5\u5177\u672A\u5728\u505C\u6B62\u671F\u9650\u5185\u7ED3\u675F\uFF0C\u6309\u4E2D\u65AD\u5904\u7406\u5E76\u4E22\u5F03\u8FDF\u5230\u7ED3\u679C", {
9134
+ toolName: execution.toolName,
9135
+ toolCallId: execution.toolCallId,
9136
+ conversationId: prepared.conversationId,
9137
+ turnId: prepared.turnId
9138
+ }),
8994
9139
  execute: () => executeToolWithIdempotency({
8995
9140
  idempotencyKey: execution.idempotencyKey,
8996
9141
  inFlight: this.idempotencyInFlight,
@@ -9662,7 +9807,8 @@ function parseEngineCheckpoint(value) {
9662
9807
  "runtimeFailureFactSink",
9663
9808
  "summarizationCallbacks",
9664
9809
  "commitExecutionBoundary",
9665
- "toolRecoveryPort"
9810
+ "toolRecoveryPort",
9811
+ "pendingUserInputPort"
9666
9812
  ]) {
9667
9813
  if (result.local && Object.hasOwn(result.local, key)) {
9668
9814
  throw new Error(`Checkpoint must not contain execution capability: ${key}`);