@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
package/dist/index.js CHANGED
@@ -954,6 +954,7 @@ __export(contracts_exports, {
954
954
  SSEToolProcessEvent: () => SSEToolProcessEvent,
955
955
  SSETransportEndEvent: () => SSETransportEndEvent,
956
956
  SSETransportErrorEvent: () => SSETransportErrorEvent,
957
+ SSEUserInputEvent: () => SSEUserInputEvent,
957
958
  SYSTEM_REMINDER_BUILTIN_TRIGGER_KINDS: () => SYSTEM_REMINDER_BUILTIN_TRIGGER_KINDS,
958
959
  SerializableJsonRecord: () => SerializableJsonRecord,
959
960
  SerializableJsonValue: () => SerializableJsonValue,
@@ -1273,6 +1274,10 @@ var UserInputMessage = BaseMessage.extend({
1273
1274
  type: z.literal("user_input"),
1274
1275
  attachments: RuntimeResourceRefs.optional()
1275
1276
  });
1277
+ var UserSteerMessage = BaseMessage.extend({
1278
+ role: z.literal("user"),
1279
+ type: z.literal("user_steer")
1280
+ });
1276
1281
  var UserContextMessage = BaseMessage.extend({
1277
1282
  role: z.literal("user"),
1278
1283
  // 产品上下文种类由 Host fence metadata 表达,不进入框架消息枚举。
@@ -1281,7 +1286,7 @@ var UserContextMessage = BaseMessage.extend({
1281
1286
  "task_request"
1282
1287
  ])
1283
1288
  });
1284
- var UserMessage = z.union([UserInputMessage, UserContextMessage]);
1289
+ var UserMessage = z.union([UserInputMessage, UserSteerMessage, UserContextMessage]);
1285
1290
  var AssistantMessage = BaseMessage.extend({
1286
1291
  role: z.literal("assistant"),
1287
1292
  type: z.enum(["thought", "final_answer", "tool_code", "tool_calls", "task_completion"])
@@ -2096,7 +2101,12 @@ var RuntimeEventShape = z.discriminatedUnion("type", [
2096
2101
  content: z.string(),
2097
2102
  raw_content: z.string().optional(),
2098
2103
  source: z.enum(["user", "editor", "system"]).default("user"),
2099
- attachments: RuntimeResourceRefs.optional()
2104
+ attachments: RuntimeResourceRefs.optional(),
2105
+ /**
2106
+ * 运行中途送达的用户输入(插话)。缺省表示运行起点的输入。
2107
+ * 上下文里投影为 `user_steer`,不改变“当前轮从哪条 user_input 开始”的判定。
2108
+ */
2109
+ delivery: z.literal("mid_run").optional()
2100
2110
  }),
2101
2111
  BaseEvent.extend({
2102
2112
  type: z.literal("thought"),
@@ -2587,6 +2597,11 @@ function parseSSEExecutionScope(event) {
2587
2597
  execution_id: event.execution_id
2588
2598
  });
2589
2599
  }
2600
+ var SSEUserInputEvent = BaseSSEEvent.extend({
2601
+ type: z.literal("user_input"),
2602
+ content: z.string(),
2603
+ delivery: z.literal("mid_run")
2604
+ });
2590
2605
  var SSEThoughtEvent = BaseSSEEvent.extend({
2591
2606
  type: z.literal("thought"),
2592
2607
  thought_message_id: ThoughtMessageIdSchema.optional(),
@@ -2741,6 +2756,7 @@ var SSESummarizationErrorEvent = BaseSSEEvent.extend({
2741
2756
  error: z.string()
2742
2757
  });
2743
2758
  var SSEEvent = z.discriminatedUnion("type", [
2759
+ SSEUserInputEvent,
2744
2760
  SSEThoughtEvent,
2745
2761
  SSEFinalAnswerChunkEvent,
2746
2762
  SSEFinalAnswerResetEvent,
@@ -2812,6 +2828,7 @@ function runtimeEventToSSEEvent(event) {
2812
2828
  const base = toBaseSSEEvent(event);
2813
2829
  switch (event.type) {
2814
2830
  case "user_input":
2831
+ return event.delivery === "mid_run" ? { ...base, type: "user_input", content: event.raw_content ?? event.content, delivery: "mid_run" } : null;
2815
2832
  case "audit_envelope":
2816
2833
  case "control":
2817
2834
  return null;
@@ -3459,6 +3476,7 @@ var NON_CHECKPOINT_LOCAL_KEYS = [
3459
3476
  "runtimeEventCommitPort",
3460
3477
  "commitExecutionBoundary",
3461
3478
  "toolRecoveryPort",
3479
+ "pendingUserInputPort",
3462
3480
  "runtimeFailureFactSink",
3463
3481
  "signal",
3464
3482
  "summarizationCallbacks",
@@ -7679,6 +7697,8 @@ function describeRuntimeEventLifecycle(event) {
7679
7697
  case "run_execution_metrics":
7680
7698
  case "history_summary":
7681
7699
  return "event_bus_sse";
7700
+ case "user_input":
7701
+ return event.type === "user_input" && event.delivery === "mid_run" ? "event_bus_sse" : "none";
7682
7702
  case "audit_envelope":
7683
7703
  return "none";
7684
7704
  default:
@@ -8122,6 +8142,16 @@ function requireEventTimestamp(value) {
8122
8142
  function applyRuntimeEventToMemory(event, memory) {
8123
8143
  switch (event.type) {
8124
8144
  case "user_input": {
8145
+ if (event.delivery === "mid_run") {
8146
+ memory.appendMessage({
8147
+ id: event.id,
8148
+ role: "user",
8149
+ type: "user_steer",
8150
+ content: event.content,
8151
+ timestamp: event.timestamp
8152
+ });
8153
+ break;
8154
+ }
8125
8155
  memory.addUserMessage(event.content, event.id, event.attachments);
8126
8156
  break;
8127
8157
  }
@@ -9805,9 +9835,49 @@ function recordToolProtocolError(params) {
9805
9835
  }
9806
9836
 
9807
9837
  // src/runtime-kernel/graph-engine/nodes/toolNode.recovery.ts
9838
+ var DEFAULT_TOOL_ABORT_GRACE_MS = 3e3;
9839
+ function settleToolWithinAbortGrace(input) {
9840
+ const { execution, signal, graceMs } = input;
9841
+ if (!signal) return execution;
9842
+ return new Promise((resolve, reject) => {
9843
+ let timer;
9844
+ const cleanup = () => {
9845
+ if (timer !== void 0) clearTimeout(timer);
9846
+ signal.removeEventListener("abort", startGrace);
9847
+ };
9848
+ function startGrace() {
9849
+ timer = setTimeout(() => {
9850
+ cleanup();
9851
+ execution.catch(() => void 0);
9852
+ input.onAbandon?.();
9853
+ const error = new Error("Tool did not stop within the abort grace period; its late outcome is discarded.");
9854
+ error.name = "AbortError";
9855
+ reject(error);
9856
+ }, graceMs);
9857
+ }
9858
+ if (signal.aborted) startGrace();
9859
+ else signal.addEventListener("abort", startGrace, { once: true });
9860
+ execution.then(
9861
+ (value) => {
9862
+ cleanup();
9863
+ resolve(value);
9864
+ },
9865
+ (error) => {
9866
+ cleanup();
9867
+ reject(error);
9868
+ }
9869
+ );
9870
+ });
9871
+ }
9808
9872
  async function executeRecoverableTool(input) {
9809
9873
  const local = input.state.local;
9810
- if (!local?.commitExecutionBoundary) return input.execute();
9874
+ const run = () => settleToolWithinAbortGrace({
9875
+ execution: input.execute(),
9876
+ signal: local?.signal,
9877
+ graceMs: input.abortGraceMs ?? DEFAULT_TOOL_ABORT_GRACE_MS,
9878
+ onAbandon: input.onAbandon
9879
+ });
9880
+ if (!local?.commitExecutionBoundary) return run();
9811
9881
  if (local.executingToolCallId) {
9812
9882
  if (local.executingToolCallId !== input.call.id) {
9813
9883
  throw new RunRecoveryBlockedError("Pending tool identity does not match execution intent");
@@ -9820,7 +9890,7 @@ async function executeRecoverableTool(input) {
9820
9890
  }
9821
9891
  local.executingToolCallId = input.call.id;
9822
9892
  await local.commitExecutionBoundary(input.state);
9823
- const result = await input.execute();
9893
+ const result = await run();
9824
9894
  if (!result.success && isRunPauseSignal(local.signal)) {
9825
9895
  const error = new Error("Tool execution paused before its outcome was confirmed");
9826
9896
  error.name = "AbortError";
@@ -9836,6 +9906,62 @@ async function commitToolBoundary(state, result) {
9836
9906
  await state.local.commitExecutionBoundary(state);
9837
9907
  }
9838
9908
 
9909
+ // src/runtime-kernel/graph-engine/nodes/toolNode.pendingUserInput.ts
9910
+ function isPendingUserInputPort(value) {
9911
+ return !!value && typeof value === "object" && typeof Reflect.get(value, "claim") === "function" && typeof Reflect.get(value, "confirm") === "function" && typeof Reflect.get(value, "release") === "function";
9912
+ }
9913
+ function readRemainingSteps(state) {
9914
+ const value = state.local?.executorLocal?.remainingSteps;
9915
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
9916
+ }
9917
+ function readIdentity(value, field) {
9918
+ if (typeof value !== "string" || value.trim().length === 0) {
9919
+ throw new Error(`ToolNode pending user input requires ${field}`);
9920
+ }
9921
+ return value;
9922
+ }
9923
+ async function claimPendingUserInputs(state, result) {
9924
+ if (result.kind !== "route" || result.nextNodeId !== "llm") return void 0;
9925
+ const local = state.local;
9926
+ const port = local?.pendingUserInputPort;
9927
+ if (!isPendingUserInputPort(port)) return void 0;
9928
+ const signal = local?.signal;
9929
+ if (signal?.aborted || isRunPauseSignal(signal)) return void 0;
9930
+ const remainingSteps = readRemainingSteps(state);
9931
+ if (remainingSteps !== void 0 && remainingSteps < 1) return void 0;
9932
+ const inputs = await port.claim();
9933
+ if (inputs.length === 0) return void 0;
9934
+ const ids = inputs.map((input) => input.id);
9935
+ try {
9936
+ const conversationId = readIdentity(local?.conversationId, "conversationId");
9937
+ const turnId = readIdentity(local?.turnId, "turnId");
9938
+ const sink = requireRuntimeEventSink(local);
9939
+ const events = inputs.map((input) => sink(
9940
+ createUserInputEvent(readIdentity(input.id, "input id"), conversationId, turnId, input.content, {
9941
+ timestamp: input.timestamp ?? Date.now(),
9942
+ delivery: "mid_run"
9943
+ }),
9944
+ "ToolNode.pending_user_input"
9945
+ ));
9946
+ state.local = {
9947
+ ...state.local,
9948
+ history: appendWorkingHistory(parseRuntimeEvents(state.local?.history ?? []), events)
9949
+ };
9950
+ return {
9951
+ events,
9952
+ confirm: async () => {
9953
+ await port.confirm(ids);
9954
+ },
9955
+ release: async () => {
9956
+ await port.release(ids);
9957
+ }
9958
+ };
9959
+ } catch (error) {
9960
+ await port.release(ids);
9961
+ throw error;
9962
+ }
9963
+ }
9964
+
9839
9965
  // src/runtime-kernel/graph-engine/nodes/toolNode.ts
9840
9966
  var logger11 = new Logger("ToolNode");
9841
9967
  function isAbortSignal(value) {
@@ -9884,7 +10010,9 @@ var ToolNode = class {
9884
10010
  auditPort;
9885
10011
  modelInputResolver;
9886
10012
  idempotencyInFlight = /* @__PURE__ */ new Map();
10013
+ abortGraceMs;
9887
10014
  constructor(dependencies) {
10015
+ this.abortGraceMs = dependencies.abortGraceMs;
9888
10016
  this.toolRuntime = dependencies.toolRuntime;
9889
10017
  this.observationPreview = dependencies.observationPreview;
9890
10018
  this.telemetryPort = dependencies.telemetryPort ?? noopTelemetry;
@@ -10003,7 +10131,19 @@ var ToolNode = class {
10003
10131
  });
10004
10132
  throw result.error;
10005
10133
  }
10006
- await commitToolBoundary(state, result);
10134
+ const steering = await claimPendingUserInputs(state, result);
10135
+ if (steering) {
10136
+ result = { ...result, events: [...result.events ?? [], ...steering.events] };
10137
+ try {
10138
+ await commitToolBoundary(state, result);
10139
+ } catch (error) {
10140
+ await steering.release();
10141
+ throw error;
10142
+ }
10143
+ await steering.confirm();
10144
+ } else {
10145
+ await commitToolBoundary(state, result);
10146
+ }
10007
10147
  if (Array.isArray(result.events) && result.events.length > 0) {
10008
10148
  events.push(...result.events);
10009
10149
  }
@@ -10090,6 +10230,13 @@ var ToolNode = class {
10090
10230
  state,
10091
10231
  call,
10092
10232
  context: prepared.toolContext,
10233
+ abortGraceMs: this.abortGraceMs,
10234
+ 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", {
10235
+ toolName: execution.toolName,
10236
+ toolCallId: execution.toolCallId,
10237
+ conversationId: prepared.conversationId,
10238
+ turnId: prepared.turnId
10239
+ }),
10093
10240
  execute: () => executeToolWithIdempotency({
10094
10241
  idempotencyKey: execution.idempotencyKey,
10095
10242
  inFlight: this.idempotencyInFlight,
@@ -10761,7 +10908,8 @@ function parseEngineCheckpoint(value) {
10761
10908
  "runtimeFailureFactSink",
10762
10909
  "summarizationCallbacks",
10763
10910
  "commitExecutionBoundary",
10764
- "toolRecoveryPort"
10911
+ "toolRecoveryPort",
10912
+ "pendingUserInputPort"
10765
10913
  ]) {
10766
10914
  if (result.local && Object.hasOwn(result.local, key)) {
10767
10915
  throw new Error(`Checkpoint must not contain execution capability: ${key}`);