@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.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",
@@ -4022,6 +4040,16 @@ function mergeResumeSessionLocal(persistedLocal, localPatch) {
4022
4040
  }
4023
4041
  };
4024
4042
  }
4043
+ function resolveResumeNodeId(local, requested) {
4044
+ const pending = local?.pendingToolCalls;
4045
+ if (!Array.isArray(pending) || pending.length === 0) return requested ?? "llm";
4046
+ if (requested !== void 0 && requested !== "tool") {
4047
+ throw new Error(
4048
+ `Graph resume must first run ${pending.length} pending tool call(s) from the interrupted batch: requested node=${requested}`
4049
+ );
4050
+ }
4051
+ return "tool";
4052
+ }
4025
4053
  var GraphExecutor = class {
4026
4054
  constructor(checkpointer, config = {}) {
4027
4055
  this.checkpointer = checkpointer;
@@ -4134,13 +4162,14 @@ var GraphExecutor = class {
4134
4162
  );
4135
4163
  }
4136
4164
  const resumedLocal = mergeResumeSessionLocal(current.local, input.localPatch);
4165
+ const nodeId = resolveResumeNodeId(resumedLocal, input.nodeId);
4137
4166
  if (this.executionCheckpointPort && current.local?.executorLocal) {
4138
4167
  resumedLocal.executorLocal = { ...current.local.executorLocal };
4139
4168
  }
4140
4169
  this.ephemeralLocals.set(checkpointKey, resumedLocal);
4141
4170
  await this.commitCheckpoint(checkpointKey, {
4142
4171
  ...current,
4143
- nodeId: input.nodeId ?? "llm",
4172
+ nodeId,
4144
4173
  ...this.executionCheckpointPort ? { executionStatus: "ready" } : {},
4145
4174
  revision: currentRevision + 1,
4146
4175
  local: sanitizeCheckpointLocal(resumedLocal)
@@ -7668,6 +7697,8 @@ function describeRuntimeEventLifecycle(event) {
7668
7697
  case "run_execution_metrics":
7669
7698
  case "history_summary":
7670
7699
  return "event_bus_sse";
7700
+ case "user_input":
7701
+ return event.type === "user_input" && event.delivery === "mid_run" ? "event_bus_sse" : "none";
7671
7702
  case "audit_envelope":
7672
7703
  return "none";
7673
7704
  default:
@@ -8111,6 +8142,16 @@ function requireEventTimestamp(value) {
8111
8142
  function applyRuntimeEventToMemory(event, memory) {
8112
8143
  switch (event.type) {
8113
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
+ }
8114
8155
  memory.addUserMessage(event.content, event.id, event.attachments);
8115
8156
  break;
8116
8157
  }
@@ -9825,6 +9866,62 @@ async function commitToolBoundary(state, result) {
9825
9866
  await state.local.commitExecutionBoundary(state);
9826
9867
  }
9827
9868
 
9869
+ // src/runtime-kernel/graph-engine/nodes/toolNode.pendingUserInput.ts
9870
+ function isPendingUserInputPort(value) {
9871
+ return !!value && typeof value === "object" && typeof Reflect.get(value, "claim") === "function" && typeof Reflect.get(value, "confirm") === "function" && typeof Reflect.get(value, "release") === "function";
9872
+ }
9873
+ function readRemainingSteps(state) {
9874
+ const value = state.local?.executorLocal?.remainingSteps;
9875
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
9876
+ }
9877
+ function readIdentity(value, field) {
9878
+ if (typeof value !== "string" || value.trim().length === 0) {
9879
+ throw new Error(`ToolNode pending user input requires ${field}`);
9880
+ }
9881
+ return value;
9882
+ }
9883
+ async function claimPendingUserInputs(state, result) {
9884
+ if (result.kind !== "route" || result.nextNodeId !== "llm") return void 0;
9885
+ const local = state.local;
9886
+ const port = local?.pendingUserInputPort;
9887
+ if (!isPendingUserInputPort(port)) return void 0;
9888
+ const signal = local?.signal;
9889
+ if (signal?.aborted || isRunPauseSignal(signal)) return void 0;
9890
+ const remainingSteps = readRemainingSteps(state);
9891
+ if (remainingSteps !== void 0 && remainingSteps < 1) return void 0;
9892
+ const inputs = await port.claim();
9893
+ if (inputs.length === 0) return void 0;
9894
+ const ids = inputs.map((input) => input.id);
9895
+ try {
9896
+ const conversationId = readIdentity(local?.conversationId, "conversationId");
9897
+ const turnId = readIdentity(local?.turnId, "turnId");
9898
+ const sink = requireRuntimeEventSink(local);
9899
+ const events = inputs.map((input) => sink(
9900
+ createUserInputEvent(readIdentity(input.id, "input id"), conversationId, turnId, input.content, {
9901
+ timestamp: input.timestamp ?? Date.now(),
9902
+ delivery: "mid_run"
9903
+ }),
9904
+ "ToolNode.pending_user_input"
9905
+ ));
9906
+ state.local = {
9907
+ ...state.local,
9908
+ history: appendWorkingHistory(parseRuntimeEvents(state.local?.history ?? []), events)
9909
+ };
9910
+ return {
9911
+ events,
9912
+ confirm: async () => {
9913
+ await port.confirm(ids);
9914
+ },
9915
+ release: async () => {
9916
+ await port.release(ids);
9917
+ }
9918
+ };
9919
+ } catch (error) {
9920
+ await port.release(ids);
9921
+ throw error;
9922
+ }
9923
+ }
9924
+
9828
9925
  // src/runtime-kernel/graph-engine/nodes/toolNode.ts
9829
9926
  var logger11 = new Logger("ToolNode");
9830
9927
  function isAbortSignal(value) {
@@ -9992,7 +10089,19 @@ var ToolNode = class {
9992
10089
  });
9993
10090
  throw result.error;
9994
10091
  }
9995
- await commitToolBoundary(state, result);
10092
+ const steering = await claimPendingUserInputs(state, result);
10093
+ if (steering) {
10094
+ result = { ...result, events: [...result.events ?? [], ...steering.events] };
10095
+ try {
10096
+ await commitToolBoundary(state, result);
10097
+ } catch (error) {
10098
+ await steering.release();
10099
+ throw error;
10100
+ }
10101
+ await steering.confirm();
10102
+ } else {
10103
+ await commitToolBoundary(state, result);
10104
+ }
9996
10105
  if (Array.isArray(result.events) && result.events.length > 0) {
9997
10106
  events.push(...result.events);
9998
10107
  }
@@ -10750,7 +10859,8 @@ function parseEngineCheckpoint(value) {
10750
10859
  "runtimeFailureFactSink",
10751
10860
  "summarizationCallbacks",
10752
10861
  "commitExecutionBoundary",
10753
- "toolRecoveryPort"
10862
+ "toolRecoveryPort",
10863
+ "pendingUserInputPort"
10754
10864
  ]) {
10755
10865
  if (result.local && Object.hasOwn(result.local, key)) {
10756
10866
  throw new Error(`Checkpoint must not contain execution capability: ${key}`);