@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
@@ -70,6 +70,7 @@ export { ModelReasoningConfig, REASONING_EFFORTS, ReasoningEffort, isValidReason
70
70
  export { ModelResolver } from './runtime-kernel/llm/modelResolver.cjs';
71
71
  export { ObservationPreviewContext, ObservationPreviewMeta, ObservationPreviewPort, ObservationPreviewResult, ToolCatalogPort, ToolExecutionPort, ToolExecutionResult, ToolRuntimeDefinition, ToolRuntimePort, ToolSchemaBuildRequest } from './runtime-kernel/tools/ports.cjs';
72
72
  export { PRIMARY_PROMPT_CAPACITY_ERROR_CODE, PrimaryPromptCapacityError, PrimaryPromptCapacityErrorMetadata } from './runtime-kernel/graph-engine/definitions/primaryPromptCapacityError.cjs';
73
+ export { PendingUserInput, PendingUserInputPort } from './runtime-kernel/graph-engine/definitions/pendingUserInput.cjs';
73
74
  export { PrimaryPromptCapacityAdmission, evaluatePrimaryPromptCapacity } from './runtime-kernel/graph-engine/functions/evaluatePrimaryPromptCapacity.cjs';
74
75
  export { ProviderContinuation } from './contracts/provider-continuation.cjs';
75
76
  export { RuntimeEventSubRunTracePublisher, RuntimeEventSubRunTracePublisherOptions } from './runtime-kernel/child-run-trace/runtimeEventSubRunTracePublisher.cjs';
@@ -70,6 +70,7 @@ export { ModelReasoningConfig, REASONING_EFFORTS, ReasoningEffort, isValidReason
70
70
  export { ModelResolver } from './runtime-kernel/llm/modelResolver.js';
71
71
  export { ObservationPreviewContext, ObservationPreviewMeta, ObservationPreviewPort, ObservationPreviewResult, ToolCatalogPort, ToolExecutionPort, ToolExecutionResult, ToolRuntimeDefinition, ToolRuntimePort, ToolSchemaBuildRequest } from './runtime-kernel/tools/ports.js';
72
72
  export { PRIMARY_PROMPT_CAPACITY_ERROR_CODE, PrimaryPromptCapacityError, PrimaryPromptCapacityErrorMetadata } from './runtime-kernel/graph-engine/definitions/primaryPromptCapacityError.js';
73
+ export { PendingUserInput, PendingUserInputPort } from './runtime-kernel/graph-engine/definitions/pendingUserInput.js';
73
74
  export { PrimaryPromptCapacityAdmission, evaluatePrimaryPromptCapacity } from './runtime-kernel/graph-engine/functions/evaluatePrimaryPromptCapacity.js';
74
75
  export { ProviderContinuation } from './contracts/provider-continuation.js';
75
76
  export { RuntimeEventSubRunTracePublisher, RuntimeEventSubRunTracePublisherOptions } from './runtime-kernel/child-run-trace/runtimeEventSubRunTracePublisher.js';
@@ -900,6 +900,10 @@ var UserInputMessage = BaseMessage.extend({
900
900
  type: z.literal("user_input"),
901
901
  attachments: RuntimeResourceRefs.optional()
902
902
  });
903
+ var UserSteerMessage = BaseMessage.extend({
904
+ role: z.literal("user"),
905
+ type: z.literal("user_steer")
906
+ });
903
907
  var UserContextMessage = BaseMessage.extend({
904
908
  role: z.literal("user"),
905
909
  // 产品上下文种类由 Host fence metadata 表达,不进入框架消息枚举。
@@ -908,7 +912,7 @@ var UserContextMessage = BaseMessage.extend({
908
912
  "task_request"
909
913
  ])
910
914
  });
911
- var UserMessage = z.union([UserInputMessage, UserContextMessage]);
915
+ var UserMessage = z.union([UserInputMessage, UserSteerMessage, UserContextMessage]);
912
916
  var AssistantMessage = BaseMessage.extend({
913
917
  role: z.literal("assistant"),
914
918
  type: z.enum(["thought", "final_answer", "tool_code", "tool_calls", "task_completion"])
@@ -1510,7 +1514,12 @@ var RuntimeEventShape = z.discriminatedUnion("type", [
1510
1514
  content: z.string(),
1511
1515
  raw_content: z.string().optional(),
1512
1516
  source: z.enum(["user", "editor", "system"]).default("user"),
1513
- attachments: RuntimeResourceRefs.optional()
1517
+ attachments: RuntimeResourceRefs.optional(),
1518
+ /**
1519
+ * 运行中途送达的用户输入(插话)。缺省表示运行起点的输入。
1520
+ * 上下文里投影为 `user_steer`,不改变“当前轮从哪条 user_input 开始”的判定。
1521
+ */
1522
+ delivery: z.literal("mid_run").optional()
1514
1523
  }),
1515
1524
  BaseEvent.extend({
1516
1525
  type: z.literal("thought"),
@@ -1970,6 +1979,11 @@ z.object({
1970
1979
  run_id: RunIdSchema,
1971
1980
  execution_id: ExecutionIdSchema
1972
1981
  });
1982
+ var SSEUserInputEvent = BaseSSEEvent.extend({
1983
+ type: z.literal("user_input"),
1984
+ content: z.string(),
1985
+ delivery: z.literal("mid_run")
1986
+ });
1973
1987
  var SSEThoughtEvent = BaseSSEEvent.extend({
1974
1988
  type: z.literal("thought"),
1975
1989
  thought_message_id: ThoughtMessageIdSchema.optional(),
@@ -2124,6 +2138,7 @@ var SSESummarizationErrorEvent = BaseSSEEvent.extend({
2124
2138
  error: z.string()
2125
2139
  });
2126
2140
  z.discriminatedUnion("type", [
2141
+ SSEUserInputEvent,
2127
2142
  SSEThoughtEvent,
2128
2143
  SSEFinalAnswerChunkEvent,
2129
2144
  SSEFinalAnswerResetEvent,
@@ -2446,6 +2461,7 @@ var NON_CHECKPOINT_LOCAL_KEYS = [
2446
2461
  "runtimeEventCommitPort",
2447
2462
  "commitExecutionBoundary",
2448
2463
  "toolRecoveryPort",
2464
+ "pendingUserInputPort",
2449
2465
  "runtimeFailureFactSink",
2450
2466
  "signal",
2451
2467
  "summarizationCallbacks",
@@ -3009,6 +3025,16 @@ function mergeResumeSessionLocal(persistedLocal, localPatch) {
3009
3025
  }
3010
3026
  };
3011
3027
  }
3028
+ function resolveResumeNodeId(local, requested) {
3029
+ const pending = local?.pendingToolCalls;
3030
+ if (!Array.isArray(pending) || pending.length === 0) return requested ?? "llm";
3031
+ if (requested !== void 0 && requested !== "tool") {
3032
+ throw new Error(
3033
+ `Graph resume must first run ${pending.length} pending tool call(s) from the interrupted batch: requested node=${requested}`
3034
+ );
3035
+ }
3036
+ return "tool";
3037
+ }
3012
3038
  var GraphExecutor = class {
3013
3039
  constructor(checkpointer, config = {}) {
3014
3040
  this.checkpointer = checkpointer;
@@ -3121,13 +3147,14 @@ var GraphExecutor = class {
3121
3147
  );
3122
3148
  }
3123
3149
  const resumedLocal = mergeResumeSessionLocal(current.local, input.localPatch);
3150
+ const nodeId = resolveResumeNodeId(resumedLocal, input.nodeId);
3124
3151
  if (this.executionCheckpointPort && current.local?.executorLocal) {
3125
3152
  resumedLocal.executorLocal = { ...current.local.executorLocal };
3126
3153
  }
3127
3154
  this.ephemeralLocals.set(checkpointKey, resumedLocal);
3128
3155
  await this.commitCheckpoint(checkpointKey, {
3129
3156
  ...current,
3130
- nodeId: input.nodeId ?? "llm",
3157
+ nodeId,
3131
3158
  ...this.executionCheckpointPort ? { executionStatus: "ready" } : {},
3132
3159
  revision: currentRevision + 1,
3133
3160
  local: sanitizeCheckpointLocal(resumedLocal)
@@ -6645,6 +6672,8 @@ function describeRuntimeEventLifecycle(event) {
6645
6672
  case "run_execution_metrics":
6646
6673
  case "history_summary":
6647
6674
  return "event_bus_sse";
6675
+ case "user_input":
6676
+ return event.type === "user_input" && event.delivery === "mid_run" ? "event_bus_sse" : "none";
6648
6677
  case "audit_envelope":
6649
6678
  return "none";
6650
6679
  default:
@@ -7088,6 +7117,16 @@ function requireEventTimestamp(value) {
7088
7117
  function applyRuntimeEventToMemory(event, memory) {
7089
7118
  switch (event.type) {
7090
7119
  case "user_input": {
7120
+ if (event.delivery === "mid_run") {
7121
+ memory.appendMessage({
7122
+ id: event.id,
7123
+ role: "user",
7124
+ type: "user_steer",
7125
+ content: event.content,
7126
+ timestamp: event.timestamp
7127
+ });
7128
+ break;
7129
+ }
7091
7130
  memory.addUserMessage(event.content, event.id, event.attachments);
7092
7131
  break;
7093
7132
  }
@@ -8795,6 +8834,62 @@ async function commitToolBoundary(state, result) {
8795
8834
  await state.local.commitExecutionBoundary(state);
8796
8835
  }
8797
8836
 
8837
+ // src/runtime-kernel/graph-engine/nodes/toolNode.pendingUserInput.ts
8838
+ function isPendingUserInputPort(value) {
8839
+ return !!value && typeof value === "object" && typeof Reflect.get(value, "claim") === "function" && typeof Reflect.get(value, "confirm") === "function" && typeof Reflect.get(value, "release") === "function";
8840
+ }
8841
+ function readRemainingSteps(state) {
8842
+ const value = state.local?.executorLocal?.remainingSteps;
8843
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
8844
+ }
8845
+ function readIdentity(value, field) {
8846
+ if (typeof value !== "string" || value.trim().length === 0) {
8847
+ throw new Error(`ToolNode pending user input requires ${field}`);
8848
+ }
8849
+ return value;
8850
+ }
8851
+ async function claimPendingUserInputs(state, result) {
8852
+ if (result.kind !== "route" || result.nextNodeId !== "llm") return void 0;
8853
+ const local = state.local;
8854
+ const port = local?.pendingUserInputPort;
8855
+ if (!isPendingUserInputPort(port)) return void 0;
8856
+ const signal = local?.signal;
8857
+ if (signal?.aborted || isRunPauseSignal(signal)) return void 0;
8858
+ const remainingSteps = readRemainingSteps(state);
8859
+ if (remainingSteps !== void 0 && remainingSteps < 1) return void 0;
8860
+ const inputs = await port.claim();
8861
+ if (inputs.length === 0) return void 0;
8862
+ const ids = inputs.map((input) => input.id);
8863
+ try {
8864
+ const conversationId = readIdentity(local?.conversationId, "conversationId");
8865
+ const turnId = readIdentity(local?.turnId, "turnId");
8866
+ const sink = requireRuntimeEventSink(local);
8867
+ const events = inputs.map((input) => sink(
8868
+ createUserInputEvent(readIdentity(input.id, "input id"), conversationId, turnId, input.content, {
8869
+ timestamp: input.timestamp ?? Date.now(),
8870
+ delivery: "mid_run"
8871
+ }),
8872
+ "ToolNode.pending_user_input"
8873
+ ));
8874
+ state.local = {
8875
+ ...state.local,
8876
+ history: appendWorkingHistory(parseRuntimeEvents(state.local?.history ?? []), events)
8877
+ };
8878
+ return {
8879
+ events,
8880
+ confirm: async () => {
8881
+ await port.confirm(ids);
8882
+ },
8883
+ release: async () => {
8884
+ await port.release(ids);
8885
+ }
8886
+ };
8887
+ } catch (error) {
8888
+ await port.release(ids);
8889
+ throw error;
8890
+ }
8891
+ }
8892
+
8798
8893
  // src/runtime-kernel/graph-engine/nodes/toolNode.ts
8799
8894
  var logger11 = new Logger("ToolNode");
8800
8895
  function isAbortSignal(value) {
@@ -8962,7 +9057,19 @@ var ToolNode = class {
8962
9057
  });
8963
9058
  throw result.error;
8964
9059
  }
8965
- await commitToolBoundary(state, result);
9060
+ const steering = await claimPendingUserInputs(state, result);
9061
+ if (steering) {
9062
+ result = { ...result, events: [...result.events ?? [], ...steering.events] };
9063
+ try {
9064
+ await commitToolBoundary(state, result);
9065
+ } catch (error) {
9066
+ await steering.release();
9067
+ throw error;
9068
+ }
9069
+ await steering.confirm();
9070
+ } else {
9071
+ await commitToolBoundary(state, result);
9072
+ }
8966
9073
  if (Array.isArray(result.events) && result.events.length > 0) {
8967
9074
  events.push(...result.events);
8968
9075
  }
@@ -9720,7 +9827,8 @@ function parseEngineCheckpoint(value) {
9720
9827
  "runtimeFailureFactSink",
9721
9828
  "summarizationCallbacks",
9722
9829
  "commitExecutionBoundary",
9723
- "toolRecoveryPort"
9830
+ "toolRecoveryPort",
9831
+ "pendingUserInputPort"
9724
9832
  ]) {
9725
9833
  if (result.local && Object.hasOwn(result.local, key)) {
9726
9834
  throw new Error(`Checkpoint must not contain execution capability: ${key}`);