@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/cli.js CHANGED
@@ -343,6 +343,10 @@ var UserInputMessage = BaseMessage.extend({
343
343
  type: z.literal("user_input"),
344
344
  attachments: RuntimeResourceRefs.optional()
345
345
  });
346
+ var UserSteerMessage = BaseMessage.extend({
347
+ role: z.literal("user"),
348
+ type: z.literal("user_steer")
349
+ });
346
350
  var UserContextMessage = BaseMessage.extend({
347
351
  role: z.literal("user"),
348
352
  // 产品上下文种类由 Host fence metadata 表达,不进入框架消息枚举。
@@ -351,7 +355,7 @@ var UserContextMessage = BaseMessage.extend({
351
355
  "task_request"
352
356
  ])
353
357
  });
354
- var UserMessage = z.union([UserInputMessage, UserContextMessage]);
358
+ var UserMessage = z.union([UserInputMessage, UserSteerMessage, UserContextMessage]);
355
359
  var AssistantMessage = BaseMessage.extend({
356
360
  role: z.literal("assistant"),
357
361
  type: z.enum(["thought", "final_answer", "tool_code", "tool_calls", "task_completion"])
@@ -968,7 +972,12 @@ var RuntimeEventShape = z.discriminatedUnion("type", [
968
972
  content: z.string(),
969
973
  raw_content: z.string().optional(),
970
974
  source: z.enum(["user", "editor", "system"]).default("user"),
971
- attachments: RuntimeResourceRefs.optional()
975
+ attachments: RuntimeResourceRefs.optional(),
976
+ /**
977
+ * 运行中途送达的用户输入(插话)。缺省表示运行起点的输入。
978
+ * 上下文里投影为 `user_steer`,不改变“当前轮从哪条 user_input 开始”的判定。
979
+ */
980
+ delivery: z.literal("mid_run").optional()
972
981
  }),
973
982
  BaseEvent.extend({
974
983
  type: z.literal("thought"),
@@ -1415,6 +1424,11 @@ z.object({
1415
1424
  run_id: RunIdSchema,
1416
1425
  execution_id: ExecutionIdSchema
1417
1426
  });
1427
+ var SSEUserInputEvent = BaseSSEEvent.extend({
1428
+ type: z.literal("user_input"),
1429
+ content: z.string(),
1430
+ delivery: z.literal("mid_run")
1431
+ });
1418
1432
  var SSEThoughtEvent = BaseSSEEvent.extend({
1419
1433
  type: z.literal("thought"),
1420
1434
  thought_message_id: ThoughtMessageIdSchema.optional(),
@@ -1569,6 +1583,7 @@ var SSESummarizationErrorEvent = BaseSSEEvent.extend({
1569
1583
  error: z.string()
1570
1584
  });
1571
1585
  z.discriminatedUnion("type", [
1586
+ SSEUserInputEvent,
1572
1587
  SSEThoughtEvent,
1573
1588
  SSEFinalAnswerChunkEvent,
1574
1589
  SSEFinalAnswerResetEvent,
@@ -2539,6 +2554,7 @@ var NON_CHECKPOINT_LOCAL_KEYS = [
2539
2554
  "runtimeEventCommitPort",
2540
2555
  "commitExecutionBoundary",
2541
2556
  "toolRecoveryPort",
2557
+ "pendingUserInputPort",
2542
2558
  "runtimeFailureFactSink",
2543
2559
  "signal",
2544
2560
  "summarizationCallbacks",
@@ -6531,6 +6547,8 @@ function describeRuntimeEventLifecycle(event) {
6531
6547
  case "run_execution_metrics":
6532
6548
  case "history_summary":
6533
6549
  return "event_bus_sse";
6550
+ case "user_input":
6551
+ return event.type === "user_input" && event.delivery === "mid_run" ? "event_bus_sse" : "none";
6534
6552
  case "audit_envelope":
6535
6553
  return "none";
6536
6554
  default:
@@ -6974,6 +6992,16 @@ function requireEventTimestamp(value) {
6974
6992
  function applyRuntimeEventToMemory(event, memory) {
6975
6993
  switch (event.type) {
6976
6994
  case "user_input": {
6995
+ if (event.delivery === "mid_run") {
6996
+ memory.appendMessage({
6997
+ id: event.id,
6998
+ role: "user",
6999
+ type: "user_steer",
7000
+ content: event.content,
7001
+ timestamp: event.timestamp
7002
+ });
7003
+ break;
7004
+ }
6977
7005
  memory.addUserMessage(event.content, event.id, event.attachments);
6978
7006
  break;
6979
7007
  }
@@ -8590,9 +8618,49 @@ function recordToolProtocolError(params) {
8590
8618
  }
8591
8619
 
8592
8620
  // src/runtime-kernel/graph-engine/nodes/toolNode.recovery.ts
8621
+ var DEFAULT_TOOL_ABORT_GRACE_MS = 3e3;
8622
+ function settleToolWithinAbortGrace(input) {
8623
+ const { execution, signal, graceMs } = input;
8624
+ if (!signal) return execution;
8625
+ return new Promise((resolve5, reject) => {
8626
+ let timer;
8627
+ const cleanup = () => {
8628
+ if (timer !== void 0) clearTimeout(timer);
8629
+ signal.removeEventListener("abort", startGrace);
8630
+ };
8631
+ function startGrace() {
8632
+ timer = setTimeout(() => {
8633
+ cleanup();
8634
+ execution.catch(() => void 0);
8635
+ input.onAbandon?.();
8636
+ const error = new Error("Tool did not stop within the abort grace period; its late outcome is discarded.");
8637
+ error.name = "AbortError";
8638
+ reject(error);
8639
+ }, graceMs);
8640
+ }
8641
+ if (signal.aborted) startGrace();
8642
+ else signal.addEventListener("abort", startGrace, { once: true });
8643
+ execution.then(
8644
+ (value) => {
8645
+ cleanup();
8646
+ resolve5(value);
8647
+ },
8648
+ (error) => {
8649
+ cleanup();
8650
+ reject(error);
8651
+ }
8652
+ );
8653
+ });
8654
+ }
8593
8655
  async function executeRecoverableTool(input) {
8594
8656
  const local = input.state.local;
8595
- if (!local?.commitExecutionBoundary) return input.execute();
8657
+ const run = () => settleToolWithinAbortGrace({
8658
+ execution: input.execute(),
8659
+ signal: local?.signal,
8660
+ graceMs: input.abortGraceMs ?? DEFAULT_TOOL_ABORT_GRACE_MS,
8661
+ onAbandon: input.onAbandon
8662
+ });
8663
+ if (!local?.commitExecutionBoundary) return run();
8596
8664
  if (local.executingToolCallId) {
8597
8665
  if (local.executingToolCallId !== input.call.id) {
8598
8666
  throw new RunRecoveryBlockedError("Pending tool identity does not match execution intent");
@@ -8605,7 +8673,7 @@ async function executeRecoverableTool(input) {
8605
8673
  }
8606
8674
  local.executingToolCallId = input.call.id;
8607
8675
  await local.commitExecutionBoundary(input.state);
8608
- const result = await input.execute();
8676
+ const result = await run();
8609
8677
  if (!result.success && isRunPauseSignal(local.signal)) {
8610
8678
  const error = new Error("Tool execution paused before its outcome was confirmed");
8611
8679
  error.name = "AbortError";
@@ -8621,6 +8689,62 @@ async function commitToolBoundary(state, result) {
8621
8689
  await state.local.commitExecutionBoundary(state);
8622
8690
  }
8623
8691
 
8692
+ // src/runtime-kernel/graph-engine/nodes/toolNode.pendingUserInput.ts
8693
+ function isPendingUserInputPort(value) {
8694
+ return !!value && typeof value === "object" && typeof Reflect.get(value, "claim") === "function" && typeof Reflect.get(value, "confirm") === "function" && typeof Reflect.get(value, "release") === "function";
8695
+ }
8696
+ function readRemainingSteps(state) {
8697
+ const value = state.local?.executorLocal?.remainingSteps;
8698
+ return typeof value === "number" && Number.isFinite(value) ? value : void 0;
8699
+ }
8700
+ function readIdentity(value, field) {
8701
+ if (typeof value !== "string" || value.trim().length === 0) {
8702
+ throw new Error(`ToolNode pending user input requires ${field}`);
8703
+ }
8704
+ return value;
8705
+ }
8706
+ async function claimPendingUserInputs(state, result) {
8707
+ if (result.kind !== "route" || result.nextNodeId !== "llm") return void 0;
8708
+ const local = state.local;
8709
+ const port = local?.pendingUserInputPort;
8710
+ if (!isPendingUserInputPort(port)) return void 0;
8711
+ const signal = local?.signal;
8712
+ if (signal?.aborted || isRunPauseSignal(signal)) return void 0;
8713
+ const remainingSteps = readRemainingSteps(state);
8714
+ if (remainingSteps !== void 0 && remainingSteps < 1) return void 0;
8715
+ const inputs = await port.claim();
8716
+ if (inputs.length === 0) return void 0;
8717
+ const ids = inputs.map((input) => input.id);
8718
+ try {
8719
+ const conversationId = readIdentity(local?.conversationId, "conversationId");
8720
+ const turnId = readIdentity(local?.turnId, "turnId");
8721
+ const sink = requireRuntimeEventSink(local);
8722
+ const events = inputs.map((input) => sink(
8723
+ createUserInputEvent(readIdentity(input.id, "input id"), conversationId, turnId, input.content, {
8724
+ timestamp: input.timestamp ?? Date.now(),
8725
+ delivery: "mid_run"
8726
+ }),
8727
+ "ToolNode.pending_user_input"
8728
+ ));
8729
+ state.local = {
8730
+ ...state.local,
8731
+ history: appendWorkingHistory(parseRuntimeEvents(state.local?.history ?? []), events)
8732
+ };
8733
+ return {
8734
+ events,
8735
+ confirm: async () => {
8736
+ await port.confirm(ids);
8737
+ },
8738
+ release: async () => {
8739
+ await port.release(ids);
8740
+ }
8741
+ };
8742
+ } catch (error) {
8743
+ await port.release(ids);
8744
+ throw error;
8745
+ }
8746
+ }
8747
+
8624
8748
  // src/runtime-kernel/graph-engine/nodes/toolNode.ts
8625
8749
  var logger11 = new Logger("ToolNode");
8626
8750
  function isAbortSignal(value) {
@@ -8669,7 +8793,9 @@ var ToolNode = class {
8669
8793
  auditPort;
8670
8794
  modelInputResolver;
8671
8795
  idempotencyInFlight = /* @__PURE__ */ new Map();
8796
+ abortGraceMs;
8672
8797
  constructor(dependencies) {
8798
+ this.abortGraceMs = dependencies.abortGraceMs;
8673
8799
  this.toolRuntime = dependencies.toolRuntime;
8674
8800
  this.observationPreview = dependencies.observationPreview;
8675
8801
  this.telemetryPort = dependencies.telemetryPort ?? noopTelemetry;
@@ -8788,7 +8914,19 @@ var ToolNode = class {
8788
8914
  });
8789
8915
  throw result.error;
8790
8916
  }
8791
- await commitToolBoundary(state, result);
8917
+ const steering = await claimPendingUserInputs(state, result);
8918
+ if (steering) {
8919
+ result = { ...result, events: [...result.events ?? [], ...steering.events] };
8920
+ try {
8921
+ await commitToolBoundary(state, result);
8922
+ } catch (error) {
8923
+ await steering.release();
8924
+ throw error;
8925
+ }
8926
+ await steering.confirm();
8927
+ } else {
8928
+ await commitToolBoundary(state, result);
8929
+ }
8792
8930
  if (Array.isArray(result.events) && result.events.length > 0) {
8793
8931
  events.push(...result.events);
8794
8932
  }
@@ -8875,6 +9013,13 @@ var ToolNode = class {
8875
9013
  state,
8876
9014
  call,
8877
9015
  context: prepared.toolContext,
9016
+ abortGraceMs: this.abortGraceMs,
9017
+ 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", {
9018
+ toolName: execution.toolName,
9019
+ toolCallId: execution.toolCallId,
9020
+ conversationId: prepared.conversationId,
9021
+ turnId: prepared.turnId
9022
+ }),
8878
9023
  execute: () => executeToolWithIdempotency({
8879
9024
  idempotencyKey: execution.idempotencyKey,
8880
9025
  inFlight: this.idempotencyInFlight,
@@ -9546,7 +9691,8 @@ function parseEngineCheckpoint(value) {
9546
9691
  "runtimeFailureFactSink",
9547
9692
  "summarizationCallbacks",
9548
9693
  "commitExecutionBoundary",
9549
- "toolRecoveryPort"
9694
+ "toolRecoveryPort",
9695
+ "pendingUserInputPort"
9550
9696
  ]) {
9551
9697
  if (result.local && Object.hasOwn(result.local, key)) {
9552
9698
  throw new Error(`Checkpoint must not contain execution capability: ${key}`);