@linnlabs/linnkit 0.36.0 → 0.37.1

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.
@@ -34,6 +34,7 @@ declare class ToolNode implements GraphNode {
34
34
  private emitToolDecisionAudit;
35
35
  private emitToolProtocolErrorAudit;
36
36
  run(state: EngineState): Promise<NodeResult>;
37
+ cancel(state: EngineState): Promise<void>;
37
38
  private runNextPendingToolCall;
38
39
  private handleSuccess;
39
40
  private handleRequireUserSuccess;
@@ -34,6 +34,7 @@ declare class ToolNode implements GraphNode {
34
34
  private emitToolDecisionAudit;
35
35
  private emitToolProtocolErrorAudit;
36
36
  run(state: EngineState): Promise<NodeResult>;
37
+ cancel(state: EngineState): Promise<void>;
37
38
  private runNextPendingToolCall;
38
39
  private handleSuccess;
39
40
  private handleRequireUserSuccess;
@@ -137,6 +137,8 @@ interface NodeResult {
137
137
  interface GraphNode {
138
138
  id: string;
139
139
  run(state: EngineState): Promise<NodeResult>;
140
+ /** 节点尚未进入时结算取消;只收尾已接纳事实,不启动工作。暂停不会调用此入口。 */
141
+ cancel?(state: EngineState): Promise<void>;
140
142
  }
141
143
  interface StandardToolCall {
142
144
  id: ToolCallId;
@@ -137,6 +137,8 @@ interface NodeResult {
137
137
  interface GraphNode {
138
138
  id: string;
139
139
  run(state: EngineState): Promise<NodeResult>;
140
+ /** 节点尚未进入时结算取消;只收尾已接纳事实,不启动工作。暂停不会调用此入口。 */
141
+ cancel?(state: EngineState): Promise<void>;
140
142
  }
141
143
  interface StandardToolCall {
142
144
  id: ToolCallId;
@@ -2911,6 +2911,26 @@ function resolveAssistantReplayParts(response) {
2911
2911
  return response.assistant_replay_parts;
2912
2912
  }
2913
2913
 
2914
+ // src/runtime-kernel/graph-engine/definitions/runContinuation.ts
2915
+ var RunRecoveryBlockedError = class extends Error {
2916
+ code = "RUN_RECOVERY_BLOCKED";
2917
+ constructor(message) {
2918
+ super(message);
2919
+ this.name = "RunRecoveryBlockedError";
2920
+ }
2921
+ };
2922
+ var RunPauseRequested = class extends Error {
2923
+ code = "RUN_PAUSE_REQUESTED";
2924
+ constructor(message = "Run paused by user") {
2925
+ super(message);
2926
+ this.name = "RunPauseRequested";
2927
+ }
2928
+ };
2929
+ function isRunPauseSignal(signal) {
2930
+ const reason = signal?.reason;
2931
+ return signal?.aborted === true && typeof reason === "object" && reason !== null && "code" in reason && reason.code === "RUN_PAUSE_REQUESTED";
2932
+ }
2933
+
2914
2934
  // src/runtime-kernel/graph-engine/engine.ts
2915
2935
  var logger2 = new Logger("GraphExecutor");
2916
2936
  function isRecord4(value) {
@@ -3220,6 +3240,7 @@ var GraphExecutor = class {
3220
3240
  reportStepsUsed(stepCount);
3221
3241
  const signalRaw = state.local?.signal;
3222
3242
  if (isAbortSignal2(signalRaw) && signalRaw.aborted) {
3243
+ if (!isRunPauseSignal(signalRaw)) await this.nodes.get(state.nodeId)?.cancel?.(state);
3223
3244
  logger2.warn("[GraphExecutor] \u6536\u5230 AbortSignal\uFF0C\u7ACB\u5373\u505C\u6B62\u63A8\u7406\u5FAA\u73AF");
3224
3245
  this.ephemeralLocals.delete(checkpointKey);
3225
3246
  throwAbortError();
@@ -3270,6 +3291,12 @@ var GraphExecutor = class {
3270
3291
  allEvents.push(...stepResolution.events);
3271
3292
  }
3272
3293
  state = stepResolution.state;
3294
+ const completedSignal = state.local?.signal;
3295
+ if (isAbortSignal2(completedSignal) && completedSignal.aborted && !isRunPauseSignal(completedSignal)) {
3296
+ await this.nodes.get(state.nodeId)?.cancel?.(state);
3297
+ this.ephemeralLocals.delete(checkpointKey);
3298
+ throwAbortError();
3299
+ }
3273
3300
  if (stepResolution.action.kind === "route") {
3274
3301
  logger2.info("[GraphExecutor] \u8DEF\u7531\u5207\u6362", {
3275
3302
  fromNodeId: stepResolution.action.fromNodeId,
@@ -8479,26 +8506,6 @@ function buildCancelledToolBatchLocalState(local, runtimeEvents) {
8479
8506
  function recordToolProtocolError(params) {
8480
8507
  }
8481
8508
 
8482
- // src/runtime-kernel/graph-engine/definitions/runContinuation.ts
8483
- var RunRecoveryBlockedError = class extends Error {
8484
- code = "RUN_RECOVERY_BLOCKED";
8485
- constructor(message) {
8486
- super(message);
8487
- this.name = "RunRecoveryBlockedError";
8488
- }
8489
- };
8490
- var RunPauseRequested = class extends Error {
8491
- code = "RUN_PAUSE_REQUESTED";
8492
- constructor(message = "Run paused by user") {
8493
- super(message);
8494
- this.name = "RunPauseRequested";
8495
- }
8496
- };
8497
- function isRunPauseSignal(signal) {
8498
- const reason = signal?.reason;
8499
- return signal?.aborted === true && typeof reason === "object" && reason !== null && "code" in reason && reason.code === "RUN_PAUSE_REQUESTED";
8500
- }
8501
-
8502
8509
  // src/runtime-kernel/graph-engine/nodes/toolNode.recovery.ts
8503
8510
  async function executeRecoverableTool(input) {
8504
8511
  const local = input.state.local;
@@ -8700,7 +8707,30 @@ var ToolNode = class {
8700
8707
  async run(state) {
8701
8708
  const events = [];
8702
8709
  while (true) {
8703
- const result = await this.runNextPendingToolCall(state);
8710
+ let result;
8711
+ try {
8712
+ result = await this.runNextPendingToolCall(state);
8713
+ } catch (error) {
8714
+ if (isToolExecutionAbort(error) && !isRunPauseSignal(state.local?.signal)) {
8715
+ await this.cancel(state);
8716
+ }
8717
+ throw error;
8718
+ }
8719
+ const signal = state.local?.signal;
8720
+ if (isAbortSignal(signal) && signal.aborted && !isRunPauseSignal(signal)) {
8721
+ await this.cancel(state);
8722
+ const error = new Error("The user aborted a request.");
8723
+ error.name = "AbortError";
8724
+ throw error;
8725
+ }
8726
+ if (result.kind === "interrupted") {
8727
+ await commitToolBoundary(state, {
8728
+ kind: "route",
8729
+ nextNodeId: "llm",
8730
+ events: result.events
8731
+ });
8732
+ throw result.error;
8733
+ }
8704
8734
  await commitToolBoundary(state, result);
8705
8735
  if (Array.isArray(result.events) && result.events.length > 0) {
8706
8736
  events.push(...result.events);
@@ -8714,6 +8744,14 @@ var ToolNode = class {
8714
8744
  };
8715
8745
  }
8716
8746
  }
8747
+ async cancel(state) {
8748
+ settlePendingToolCallsAfterAbort({
8749
+ state,
8750
+ calls: parsePendingToolCalls(state.local?.pendingToolCalls ?? []),
8751
+ toolCatalog: this.toolRuntime
8752
+ });
8753
+ await commitToolBoundary(state, { kind: "yield"});
8754
+ }
8717
8755
  async runNextPendingToolCall(state) {
8718
8756
  const calls = parsePendingToolCalls(state.local?.pendingToolCalls ?? []);
8719
8757
  const signalRaw = state.local?.signal;
@@ -9133,7 +9171,11 @@ var ToolNode = class {
9133
9171
  nextProtocolErrorCount: fuse.nextCount
9134
9172
  });
9135
9173
  if (fuse.shouldFuse && remainingCalls.length === 0) {
9136
- throw createToolProtocolFuseError(fuse.nextCount, context.exec.error);
9174
+ return {
9175
+ kind: "interrupted",
9176
+ error: createToolProtocolFuseError(fuse.nextCount, context.exec.error),
9177
+ events: context.bridge.getRuntimeEvents()
9178
+ };
9137
9179
  }
9138
9180
  if (remainingCalls.length === 0 && context.local.toolBatchCompletionMode === "yield_after_batch") {
9139
9181
  return { kind: "yield", events: context.bridge.getRuntimeEvents() };