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