@linnlabs/linnkit 0.42.0 → 0.44.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 (75) hide show
  1. package/CHANGELOG.md +64 -0
  2. package/dist/cli.cjs +113 -5
  3. package/dist/cli.cjs.map +1 -1
  4. package/dist/cli.js +113 -5
  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/events.d.cts +4 -0
  13. package/dist/contracts/events.d.ts +4 -0
  14. package/dist/contracts/messages.d.cts +226 -2
  15. package/dist/contracts/messages.d.ts +226 -2
  16. package/dist/contracts/sse.d.cts +65 -1
  17. package/dist/contracts/sse.d.ts +65 -1
  18. package/dist/contracts.cjs +19 -2
  19. package/dist/contracts.cjs.map +1 -1
  20. package/dist/contracts.d.cts +1 -1
  21. package/dist/contracts.d.ts +1 -1
  22. package/dist/contracts.js +19 -3
  23. package/dist/contracts.js.map +1 -1
  24. package/dist/index.cjs +115 -5
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.js +115 -5
  27. package/dist/index.js.map +1 -1
  28. package/dist/quickstart.cjs +113 -5
  29. package/dist/quickstart.cjs.map +1 -1
  30. package/dist/quickstart.js +113 -5
  31. package/dist/quickstart.js.map +1 -1
  32. package/dist/runtime-kernel/events.cjs +29 -2
  33. package/dist/runtime-kernel/events.cjs.map +1 -1
  34. package/dist/runtime-kernel/events.js +29 -2
  35. package/dist/runtime-kernel/events.js.map +1 -1
  36. package/dist/runtime-kernel/graph-engine/definitions/pendingUserInput.d.cts +27 -0
  37. package/dist/runtime-kernel/graph-engine/definitions/pendingUserInput.d.ts +27 -0
  38. package/dist/runtime-kernel/graph-engine/engine.d.cts +2 -1
  39. package/dist/runtime-kernel/graph-engine/engine.d.ts +2 -1
  40. package/dist/runtime-kernel/graph-engine/index.d.cts +1 -0
  41. package/dist/runtime-kernel/graph-engine/index.d.ts +1 -0
  42. package/dist/runtime-kernel/graph-engine/types.d.cts +3 -0
  43. package/dist/runtime-kernel/graph-engine/types.d.ts +3 -0
  44. package/dist/runtime-kernel.cjs +113 -5
  45. package/dist/runtime-kernel.cjs.map +1 -1
  46. package/dist/runtime-kernel.d.cts +1 -0
  47. package/dist/runtime-kernel.d.ts +1 -0
  48. package/dist/runtime-kernel.js +113 -5
  49. package/dist/runtime-kernel.js.map +1 -1
  50. package/dist/testkit/agent-harness/scriptedInferenceHarness.d.cts +24 -6
  51. package/dist/testkit/agent-harness/scriptedInferenceHarness.d.ts +24 -6
  52. package/dist/testkit/scenario/graphScenario.d.cts +89 -0
  53. package/dist/testkit/scenario/graphScenario.d.ts +89 -0
  54. package/dist/testkit/scenario/pendingUserInputQueue.d.cts +26 -0
  55. package/dist/testkit/scenario/pendingUserInputQueue.d.ts +26 -0
  56. package/dist/testkit/scenario/runtimeEventRecorder.d.cts +36 -0
  57. package/dist/testkit/scenario/runtimeEventRecorder.d.ts +36 -0
  58. package/dist/testkit/scenario/scriptedToolRuntime.d.cts +51 -0
  59. package/dist/testkit/scenario/scriptedToolRuntime.d.ts +51 -0
  60. package/dist/testkit/scenario/steeringInvariants.d.cts +33 -0
  61. package/dist/testkit/scenario/steeringInvariants.d.ts +33 -0
  62. package/dist/testkit/scenario/timeline.d.cts +27 -0
  63. package/dist/testkit/scenario/timeline.d.ts +27 -0
  64. package/dist/testkit.cjs +683 -18
  65. package/dist/testkit.cjs.map +1 -1
  66. package/dist/testkit.d.cts +7 -1
  67. package/dist/testkit.d.ts +7 -1
  68. package/dist/testkit.js +675 -19
  69. package/dist/testkit.js.map +1 -1
  70. package/docs/integration/README.md +1 -0
  71. package/docs/integration/mid-run-user-input.md +73 -0
  72. package/docs/integration/realtime.md +4 -0
  73. package/docs/integration/testing.md +40 -4
  74. package/docs/integration/tool-development-guide.md +1 -0
  75. 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",
@@ -4024,6 +4042,16 @@ function mergeResumeSessionLocal(persistedLocal, localPatch) {
4024
4042
  }
4025
4043
  };
4026
4044
  }
4045
+ function resolveResumeNodeId(local, requested) {
4046
+ const pending = local?.pendingToolCalls;
4047
+ if (!Array.isArray(pending) || pending.length === 0) return requested ?? "llm";
4048
+ if (requested !== void 0 && requested !== "tool") {
4049
+ throw new Error(
4050
+ `Graph resume must first run ${pending.length} pending tool call(s) from the interrupted batch: requested node=${requested}`
4051
+ );
4052
+ }
4053
+ return "tool";
4054
+ }
4027
4055
  var GraphExecutor = class {
4028
4056
  constructor(checkpointer, config = {}) {
4029
4057
  this.checkpointer = checkpointer;
@@ -4136,13 +4164,14 @@ var GraphExecutor = class {
4136
4164
  );
4137
4165
  }
4138
4166
  const resumedLocal = mergeResumeSessionLocal(current.local, input.localPatch);
4167
+ const nodeId = resolveResumeNodeId(resumedLocal, input.nodeId);
4139
4168
  if (this.executionCheckpointPort && current.local?.executorLocal) {
4140
4169
  resumedLocal.executorLocal = { ...current.local.executorLocal };
4141
4170
  }
4142
4171
  this.ephemeralLocals.set(checkpointKey, resumedLocal);
4143
4172
  await this.commitCheckpoint(checkpointKey, {
4144
4173
  ...current,
4145
- nodeId: input.nodeId ?? "llm",
4174
+ nodeId,
4146
4175
  ...this.executionCheckpointPort ? { executionStatus: "ready" } : {},
4147
4176
  revision: currentRevision + 1,
4148
4177
  local: sanitizeCheckpointLocal(resumedLocal)
@@ -7670,6 +7699,8 @@ function describeRuntimeEventLifecycle(event) {
7670
7699
  case "run_execution_metrics":
7671
7700
  case "history_summary":
7672
7701
  return "event_bus_sse";
7702
+ case "user_input":
7703
+ return event.type === "user_input" && event.delivery === "mid_run" ? "event_bus_sse" : "none";
7673
7704
  case "audit_envelope":
7674
7705
  return "none";
7675
7706
  default:
@@ -8113,6 +8144,16 @@ function requireEventTimestamp(value) {
8113
8144
  function applyRuntimeEventToMemory(event, memory) {
8114
8145
  switch (event.type) {
8115
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
+ }
8116
8157
  memory.addUserMessage(event.content, event.id, event.attachments);
8117
8158
  break;
8118
8159
  }
@@ -9827,6 +9868,62 @@ async function commitToolBoundary(state, result) {
9827
9868
  await state.local.commitExecutionBoundary(state);
9828
9869
  }
9829
9870
 
9871
+ // src/runtime-kernel/graph-engine/nodes/toolNode.pendingUserInput.ts
9872
+ function isPendingUserInputPort(value) {
9873
+ return !!value && typeof value === "object" && typeof Reflect.get(value, "claim") === "function" && typeof Reflect.get(value, "confirm") === "function" && typeof Reflect.get(value, "release") === "function";
9874
+ }
9875
+ function readRemainingSteps(state) {
9876
+ const value = state.local?.executorLocal?.remainingSteps;
9877
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
9878
+ }
9879
+ function readIdentity(value, field) {
9880
+ if (typeof value !== "string" || value.trim().length === 0) {
9881
+ throw new Error(`ToolNode pending user input requires ${field}`);
9882
+ }
9883
+ return value;
9884
+ }
9885
+ async function claimPendingUserInputs(state, result) {
9886
+ if (result.kind !== "route" || result.nextNodeId !== "llm") return void 0;
9887
+ const local = state.local;
9888
+ const port = local?.pendingUserInputPort;
9889
+ if (!isPendingUserInputPort(port)) return void 0;
9890
+ const signal = local?.signal;
9891
+ if (signal?.aborted || isRunPauseSignal(signal)) return void 0;
9892
+ const remainingSteps = readRemainingSteps(state);
9893
+ if (remainingSteps !== void 0 && remainingSteps < 1) return void 0;
9894
+ const inputs = await port.claim();
9895
+ if (inputs.length === 0) return void 0;
9896
+ const ids = inputs.map((input) => input.id);
9897
+ try {
9898
+ const conversationId = readIdentity(local?.conversationId, "conversationId");
9899
+ const turnId = readIdentity(local?.turnId, "turnId");
9900
+ const sink = requireRuntimeEventSink(local);
9901
+ const events = inputs.map((input) => sink(
9902
+ createUserInputEvent(readIdentity(input.id, "input id"), conversationId, turnId, input.content, {
9903
+ timestamp: input.timestamp ?? Date.now(),
9904
+ delivery: "mid_run"
9905
+ }),
9906
+ "ToolNode.pending_user_input"
9907
+ ));
9908
+ state.local = {
9909
+ ...state.local,
9910
+ history: appendWorkingHistory(parseRuntimeEvents(state.local?.history ?? []), events)
9911
+ };
9912
+ return {
9913
+ events,
9914
+ confirm: async () => {
9915
+ await port.confirm(ids);
9916
+ },
9917
+ release: async () => {
9918
+ await port.release(ids);
9919
+ }
9920
+ };
9921
+ } catch (error) {
9922
+ await port.release(ids);
9923
+ throw error;
9924
+ }
9925
+ }
9926
+
9830
9927
  // src/runtime-kernel/graph-engine/nodes/toolNode.ts
9831
9928
  var logger11 = new Logger("ToolNode");
9832
9929
  function isAbortSignal(value) {
@@ -9994,7 +10091,19 @@ var ToolNode = class {
9994
10091
  });
9995
10092
  throw result.error;
9996
10093
  }
9997
- await commitToolBoundary(state, result);
10094
+ const steering = await claimPendingUserInputs(state, result);
10095
+ if (steering) {
10096
+ result = { ...result, events: [...result.events ?? [], ...steering.events] };
10097
+ try {
10098
+ await commitToolBoundary(state, result);
10099
+ } catch (error) {
10100
+ await steering.release();
10101
+ throw error;
10102
+ }
10103
+ await steering.confirm();
10104
+ } else {
10105
+ await commitToolBoundary(state, result);
10106
+ }
9998
10107
  if (Array.isArray(result.events) && result.events.length > 0) {
9999
10108
  events.push(...result.events);
10000
10109
  }
@@ -10752,7 +10861,8 @@ function parseEngineCheckpoint(value) {
10752
10861
  "runtimeFailureFactSink",
10753
10862
  "summarizationCallbacks",
10754
10863
  "commitExecutionBoundary",
10755
- "toolRecoveryPort"
10864
+ "toolRecoveryPort",
10865
+ "pendingUserInputPort"
10756
10866
  ]) {
10757
10867
  if (result.local && Object.hasOwn(result.local, key)) {
10758
10868
  throw new Error(`Checkpoint must not contain execution capability: ${key}`);