@jaypie/llm 1.2.9 → 1.2.11

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.
@@ -3,11 +3,13 @@ import { LlmStreamChunk } from "../types/LlmStreamChunk.interface.js";
3
3
  import { ProviderAdapter } from "./adapters/ProviderAdapter.interface.js";
4
4
  import { HookRunner } from "./hooks/index.js";
5
5
  import { InputProcessor } from "./input/index.js";
6
+ import { RetryPolicy } from "./retry/index.js";
6
7
  export interface StreamLoopConfig {
7
8
  adapter: ProviderAdapter;
8
9
  client: unknown;
9
10
  hookRunner?: HookRunner;
10
11
  inputProcessor?: InputProcessor;
12
+ retryPolicy?: RetryPolicy;
11
13
  }
12
14
  /**
13
15
  * StreamLoop implements streaming multi-turn conversation loop.
@@ -19,6 +21,7 @@ export declare class StreamLoop {
19
21
  private readonly client;
20
22
  private readonly hookRunnerInstance;
21
23
  private readonly inputProcessorInstance;
24
+ private readonly retryPolicy;
22
25
  constructor(config: StreamLoopConfig);
23
26
  /**
24
27
  * Execute the streaming loop for multi-turn conversations with tool calling.
@@ -88,8 +88,8 @@ export declare class OpenRouterAdapter extends BaseProviderAdapter {
88
88
  buildRequest(request: OperateRequest): OpenRouterRequest;
89
89
  formatTools(toolkit: Toolkit, outputSchema?: JsonObject): ProviderToolDefinition[];
90
90
  formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
91
- executeRequest(client: unknown, request: unknown): Promise<OpenRouterResponse>;
92
- executeStreamRequest(client: unknown, request: unknown): AsyncIterable<LlmStreamChunk>;
91
+ executeRequest(client: unknown, request: unknown, signal?: AbortSignal): Promise<OpenRouterResponse>;
92
+ executeStreamRequest(client: unknown, request: unknown, signal?: AbortSignal): AsyncIterable<LlmStreamChunk>;
93
93
  parseResponse(response: unknown, _options?: LlmOperateOptions): ParsedResponse;
94
94
  extractToolCalls(response: unknown): StandardToolCall[];
95
95
  extractUsage(response: unknown, model: string): LlmUsageItem;
@@ -47,17 +47,19 @@ export interface ProviderAdapter {
47
47
  *
48
48
  * @param client - The provider's SDK client instance
49
49
  * @param request - Provider-specific request object (from buildRequest)
50
+ * @param signal - Optional AbortSignal to cancel the request on retry
50
51
  * @returns Raw provider response
51
52
  */
52
- executeRequest(client: unknown, request: unknown): Promise<unknown>;
53
+ executeRequest(client: unknown, request: unknown, signal?: AbortSignal): Promise<unknown>;
53
54
  /**
54
55
  * Execute a streaming API request to the provider
55
56
  *
56
57
  * @param client - The provider's SDK client instance
57
58
  * @param request - Provider-specific request object (from buildRequest)
59
+ * @param signal - Optional AbortSignal to cancel the request on retry
58
60
  * @returns AsyncIterable of stream chunks
59
61
  */
60
- executeStreamRequest?(client: unknown, request: unknown): AsyncIterable<LlmStreamChunk>;
62
+ executeStreamRequest?(client: unknown, request: unknown, signal?: AbortSignal): AsyncIterable<LlmStreamChunk>;
61
63
  /**
62
64
  * Parse a provider response into standardized format
63
65
  *
@@ -158,7 +160,7 @@ export declare abstract class BaseProviderAdapter implements ProviderAdapter {
158
160
  abstract buildRequest(request: OperateRequest): unknown;
159
161
  abstract formatTools(toolkit: Toolkit, outputSchema?: JsonObject): ProviderToolDefinition[];
160
162
  abstract formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
161
- abstract executeRequest(client: unknown, request: unknown): Promise<unknown>;
163
+ abstract executeRequest(client: unknown, request: unknown, signal?: AbortSignal): Promise<unknown>;
162
164
  abstract parseResponse(response: unknown, options?: LlmOperateOptions): ParsedResponse;
163
165
  abstract extractToolCalls(response: unknown): StandardToolCall[];
164
166
  abstract extractUsage(response: unknown, model: string): LlmUsageItem;
@@ -28,12 +28,15 @@ export declare class RetryExecutor {
28
28
  private readonly errorClassifier;
29
29
  constructor(config: RetryExecutorConfig);
30
30
  /**
31
- * Execute an operation with retry logic
31
+ * Execute an operation with retry logic.
32
+ * Each attempt receives an AbortSignal. On failure, the signal is aborted
33
+ * before sleeping — this kills lingering socket callbacks from the previous
34
+ * request and prevents stale async errors from escaping the retry loop.
32
35
  *
33
- * @param operation - The async operation to execute
36
+ * @param operation - The async operation to execute (receives AbortSignal)
34
37
  * @param options - Execution options including context and hooks
35
38
  * @returns The result of the operation
36
39
  * @throws BadGatewayError if all retries are exhausted or error is not retryable
37
40
  */
38
- execute<T>(operation: () => Promise<T>, options: ExecuteOptions): Promise<T>;
41
+ execute<T>(operation: ((signal: AbortSignal) => Promise<T>) | (() => Promise<T>), options: ExecuteOptions): Promise<T>;
39
42
  }
@@ -1,4 +1,5 @@
1
1
  export { DEFAULT_BACKOFF_FACTOR, DEFAULT_INITIAL_DELAY_MS, DEFAULT_MAX_DELAY_MS, DEFAULT_MAX_RETRIES, defaultRetryPolicy, MAX_RETRIES_ABSOLUTE_LIMIT, RetryPolicy, } from "./RetryPolicy.js";
2
2
  export type { RetryPolicyConfig } from "./RetryPolicy.js";
3
+ export { isTransientNetworkError } from "./isTransientNetworkError.js";
3
4
  export { RetryExecutor } from "./RetryExecutor.js";
4
5
  export type { ErrorClassifier, ExecuteOptions, RetryContext, RetryExecutorConfig, } from "./RetryExecutor.js";
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Transient network error detection utility.
3
+ *
4
+ * Detects low-level Node.js/undici network errors that indicate
5
+ * a temporary network issue (not a provider API error).
6
+ * These errors should always be retried.
7
+ */
8
+ /**
9
+ * Detect transient network errors by inspecting the error and its cause chain.
10
+ *
11
+ * Undici (Node.js fetch) wraps low-level errors like ECONNRESET inside
12
+ * `TypeError: terminated`. This function recursively walks `error.cause`
13
+ * to detect these wrapped errors.
14
+ *
15
+ * @param error - The error to inspect
16
+ * @returns true if the error (or any cause in its chain) is a transient network error
17
+ */
18
+ export declare function isTransientNetworkError(error: unknown): boolean;
package/dist/esm/index.js CHANGED
@@ -805,6 +805,90 @@ var ErrorCategory;
805
805
  ErrorCategory["Unknown"] = "unknown";
806
806
  })(ErrorCategory || (ErrorCategory = {}));
807
807
 
808
+ /**
809
+ * Transient network error detection utility.
810
+ *
811
+ * Detects low-level Node.js/undici network errors that indicate
812
+ * a temporary network issue (not a provider API error).
813
+ * These errors should always be retried.
814
+ */
815
+ //
816
+ //
817
+ // Constants
818
+ //
819
+ /** Error codes from Node.js net/dns subsystems that indicate transient failures */
820
+ const TRANSIENT_ERROR_CODES = new Set([
821
+ "ECONNREFUSED",
822
+ "ECONNRESET",
823
+ "EAI_AGAIN",
824
+ "ENETRESET",
825
+ "ENETUNREACH",
826
+ "ENOTFOUND",
827
+ "EPIPE",
828
+ "ETIMEDOUT",
829
+ ]);
830
+ /** Substrings in error messages that indicate transient network issues */
831
+ const TRANSIENT_MESSAGE_PATTERNS = [
832
+ "network",
833
+ "socket hang up",
834
+ "terminated",
835
+ ];
836
+ //
837
+ //
838
+ // Helpers
839
+ //
840
+ /**
841
+ * Check a single error (without walking the cause chain)
842
+ */
843
+ function matchesSingleError(error) {
844
+ if (!(error instanceof Error))
845
+ return false;
846
+ // Check error code (e.g., ECONNRESET)
847
+ const code = error.code;
848
+ if (code && TRANSIENT_ERROR_CODES.has(code)) {
849
+ return true;
850
+ }
851
+ // Check error message for transient patterns
852
+ const message = error.message.toLowerCase();
853
+ for (const pattern of TRANSIENT_MESSAGE_PATTERNS) {
854
+ if (message.includes(pattern)) {
855
+ return true;
856
+ }
857
+ }
858
+ return false;
859
+ }
860
+ //
861
+ //
862
+ // Main
863
+ //
864
+ /**
865
+ * Detect transient network errors by inspecting the error and its cause chain.
866
+ *
867
+ * Undici (Node.js fetch) wraps low-level errors like ECONNRESET inside
868
+ * `TypeError: terminated`. This function recursively walks `error.cause`
869
+ * to detect these wrapped errors.
870
+ *
871
+ * @param error - The error to inspect
872
+ * @returns true if the error (or any cause in its chain) is a transient network error
873
+ */
874
+ function isTransientNetworkError(error) {
875
+ let current = error;
876
+ while (current) {
877
+ if (matchesSingleError(current)) {
878
+ return true;
879
+ }
880
+ // Walk the cause chain (cause is ES2022, cast for compatibility)
881
+ const cause = current.cause;
882
+ if (current instanceof Error && cause) {
883
+ current = cause;
884
+ }
885
+ else {
886
+ break;
887
+ }
888
+ }
889
+ return false;
890
+ }
891
+
808
892
  //
809
893
  //
810
894
  // Constants
@@ -1266,6 +1350,14 @@ class AnthropicAdapter extends BaseProviderAdapter {
1266
1350
  shouldRetry: false,
1267
1351
  };
1268
1352
  }
1353
+ // Check for transient network errors (ECONNRESET, etc.)
1354
+ if (isTransientNetworkError(error)) {
1355
+ return {
1356
+ error,
1357
+ category: ErrorCategory.Retryable,
1358
+ shouldRetry: true,
1359
+ };
1360
+ }
1269
1361
  // Unknown error - treat as potentially retryable
1270
1362
  return {
1271
1363
  error,
@@ -1748,6 +1840,14 @@ class GeminiAdapter extends BaseProviderAdapter {
1748
1840
  shouldRetry: true,
1749
1841
  };
1750
1842
  }
1843
+ // Check for transient network errors (ECONNRESET, etc.)
1844
+ if (isTransientNetworkError(error)) {
1845
+ return {
1846
+ error,
1847
+ category: ErrorCategory.Retryable,
1848
+ shouldRetry: true,
1849
+ };
1850
+ }
1751
1851
  // Unknown error - treat as potentially retryable
1752
1852
  return {
1753
1853
  error,
@@ -2350,6 +2450,14 @@ class OpenAiAdapter extends BaseProviderAdapter {
2350
2450
  };
2351
2451
  }
2352
2452
  }
2453
+ // Check for transient network errors (ECONNRESET, etc.)
2454
+ if (isTransientNetworkError(error)) {
2455
+ return {
2456
+ error,
2457
+ category: ErrorCategory.Retryable,
2458
+ shouldRetry: true,
2459
+ };
2460
+ }
2353
2461
  // Unknown error - treat as potentially retryable
2354
2462
  return {
2355
2463
  error,
@@ -2563,7 +2671,7 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2563
2671
  //
2564
2672
  // API Execution
2565
2673
  //
2566
- async executeRequest(client, request) {
2674
+ async executeRequest(client, request, signal) {
2567
2675
  const openRouter = client;
2568
2676
  const openRouterRequest = request;
2569
2677
  const response = await openRouter.chat.send({
@@ -2572,10 +2680,10 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2572
2680
  tools: openRouterRequest.tools,
2573
2681
  toolChoice: openRouterRequest.tool_choice,
2574
2682
  user: openRouterRequest.user,
2575
- });
2683
+ }, signal ? { signal } : undefined);
2576
2684
  return response;
2577
2685
  }
2578
- async *executeStreamRequest(client, request) {
2686
+ async *executeStreamRequest(client, request, signal) {
2579
2687
  const openRouter = client;
2580
2688
  const openRouterRequest = request;
2581
2689
  // Use chat.send with stream: true for streaming responses
@@ -2586,7 +2694,7 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2586
2694
  toolChoice: openRouterRequest.tool_choice,
2587
2695
  user: openRouterRequest.user,
2588
2696
  stream: true,
2589
- });
2697
+ }, signal ? { signal } : undefined);
2590
2698
  // Track current tool call being built
2591
2699
  let currentToolCall = null;
2592
2700
  // Track usage for final chunk
@@ -2836,6 +2944,14 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2836
2944
  suggestedDelayMs: 60000,
2837
2945
  };
2838
2946
  }
2947
+ // Check for transient network errors (ECONNRESET, etc.)
2948
+ if (isTransientNetworkError(error)) {
2949
+ return {
2950
+ error,
2951
+ category: ErrorCategory.Retryable,
2952
+ shouldRetry: true,
2953
+ };
2954
+ }
2839
2955
  // Unknown error - treat as potentially retryable
2840
2956
  return {
2841
2957
  error,
@@ -3789,9 +3905,12 @@ class RetryExecutor {
3789
3905
  this.errorClassifier = config.errorClassifier;
3790
3906
  }
3791
3907
  /**
3792
- * Execute an operation with retry logic
3908
+ * Execute an operation with retry logic.
3909
+ * Each attempt receives an AbortSignal. On failure, the signal is aborted
3910
+ * before sleeping — this kills lingering socket callbacks from the previous
3911
+ * request and prevents stale async errors from escaping the retry loop.
3793
3912
  *
3794
- * @param operation - The async operation to execute
3913
+ * @param operation - The async operation to execute (receives AbortSignal)
3795
3914
  * @param options - Execution options including context and hooks
3796
3915
  * @returns The result of the operation
3797
3916
  * @throws BadGatewayError if all retries are exhausted or error is not retryable
@@ -3799,14 +3918,17 @@ class RetryExecutor {
3799
3918
  async execute(operation, options) {
3800
3919
  let attempt = 0;
3801
3920
  while (true) {
3921
+ const controller = new AbortController();
3802
3922
  try {
3803
- const result = await operation();
3923
+ const result = await operation(controller.signal);
3804
3924
  if (attempt > 0) {
3805
3925
  log$1.debug(`API call succeeded after ${attempt} retries`);
3806
3926
  }
3807
3927
  return result;
3808
3928
  }
3809
3929
  catch (error) {
3930
+ // Abort the previous request to kill lingering socket callbacks
3931
+ controller.abort("retry");
3810
3932
  // Check if we've exhausted retries
3811
3933
  if (!this.policy.shouldRetry(attempt)) {
3812
3934
  log$1.error(`API call failed after ${this.policy.maxRetries} retries`);
@@ -4031,7 +4153,7 @@ class OperateLoop {
4031
4153
  providerRequest,
4032
4154
  });
4033
4155
  // Execute with retry (RetryExecutor handles error hooks and throws appropriate errors)
4034
- const response = await retryExecutor.execute(() => this.adapter.executeRequest(this.client, providerRequest), {
4156
+ const response = await retryExecutor.execute((signal) => this.adapter.executeRequest(this.client, providerRequest, signal), {
4035
4157
  context: {
4036
4158
  input: state.currentInput,
4037
4159
  options,
@@ -4287,6 +4409,7 @@ class StreamLoop {
4287
4409
  this.client = config.client;
4288
4410
  this.hookRunnerInstance = config.hookRunner ?? hookRunner;
4289
4411
  this.inputProcessorInstance = config.inputProcessor ?? inputProcessor;
4412
+ this.retryPolicy = config.retryPolicy ?? defaultRetryPolicy;
4290
4413
  }
4291
4414
  /**
4292
4415
  * Execute the streaming loop for multi-turn conversations with tool calling.
@@ -4419,30 +4542,78 @@ class StreamLoop {
4419
4542
  });
4420
4543
  // Collect tool calls from the stream
4421
4544
  const collectedToolCalls = [];
4422
- // Execute streaming request
4423
- const streamGenerator = this.adapter.executeStreamRequest(this.client, providerRequest);
4424
- for await (const chunk of streamGenerator) {
4425
- // Pass through text chunks
4426
- if (chunk.type === LlmStreamChunkType.Text) {
4427
- yield chunk;
4428
- }
4429
- // Collect tool calls
4430
- if (chunk.type === LlmStreamChunkType.ToolCall) {
4431
- collectedToolCalls.push({
4432
- callId: chunk.toolCall.id,
4433
- name: chunk.toolCall.name,
4434
- arguments: chunk.toolCall.arguments,
4435
- raw: chunk.toolCall,
4436
- });
4437
- yield chunk;
4438
- }
4439
- // Track usage from done chunk (but don't yield it yet - we'll emit our own)
4440
- if (chunk.type === LlmStreamChunkType.Done && chunk.usage) {
4441
- state.usageItems.push(...chunk.usage);
4545
+ // Retry loop for connection-level failures
4546
+ let attempt = 0;
4547
+ let chunksYielded = false;
4548
+ while (true) {
4549
+ const controller = new AbortController();
4550
+ try {
4551
+ // Execute streaming request
4552
+ const streamGenerator = this.adapter.executeStreamRequest(this.client, providerRequest, controller.signal);
4553
+ for await (const chunk of streamGenerator) {
4554
+ // Pass through text chunks
4555
+ if (chunk.type === LlmStreamChunkType.Text) {
4556
+ chunksYielded = true;
4557
+ yield chunk;
4558
+ }
4559
+ // Collect tool calls
4560
+ if (chunk.type === LlmStreamChunkType.ToolCall) {
4561
+ chunksYielded = true;
4562
+ collectedToolCalls.push({
4563
+ callId: chunk.toolCall.id,
4564
+ name: chunk.toolCall.name,
4565
+ arguments: chunk.toolCall.arguments,
4566
+ raw: chunk.toolCall,
4567
+ });
4568
+ yield chunk;
4569
+ }
4570
+ // Track usage from done chunk (but don't yield it yet - we'll emit our own)
4571
+ if (chunk.type === LlmStreamChunkType.Done && chunk.usage) {
4572
+ state.usageItems.push(...chunk.usage);
4573
+ }
4574
+ // Pass through error chunks
4575
+ if (chunk.type === LlmStreamChunkType.Error) {
4576
+ chunksYielded = true;
4577
+ yield chunk;
4578
+ }
4579
+ }
4580
+ // Stream completed successfully
4581
+ if (attempt > 0) {
4582
+ log$1.debug(`Stream request succeeded after ${attempt} retries`);
4583
+ }
4584
+ break;
4442
4585
  }
4443
- // Pass through error chunks
4444
- if (chunk.type === LlmStreamChunkType.Error) {
4445
- yield chunk;
4586
+ catch (error) {
4587
+ // Abort the previous request to kill lingering socket callbacks
4588
+ controller.abort("retry");
4589
+ // If chunks were already yielded, we can't transparently retry
4590
+ if (chunksYielded) {
4591
+ const errorMessage = error instanceof Error ? error.message : String(error);
4592
+ log$1.error("Stream failed after partial data was delivered");
4593
+ log$1.var({ error });
4594
+ yield {
4595
+ type: LlmStreamChunkType.Error,
4596
+ error: {
4597
+ detail: errorMessage,
4598
+ status: 502,
4599
+ title: "Stream Error",
4600
+ },
4601
+ };
4602
+ return { shouldContinue: false };
4603
+ }
4604
+ // Check if we've exhausted retries or error is not retryable
4605
+ if (!this.retryPolicy.shouldRetry(attempt) ||
4606
+ !this.adapter.isRetryableError(error)) {
4607
+ log$1.error(`Stream request failed after ${this.retryPolicy.maxRetries} retries`);
4608
+ log$1.var({ error });
4609
+ const errorMessage = error instanceof Error ? error.message : String(error);
4610
+ throw new BadGatewayError(errorMessage);
4611
+ }
4612
+ const delay = this.retryPolicy.getDelayForAttempt(attempt);
4613
+ log$1.warn(`Stream request failed. Retrying in ${delay}ms...`);
4614
+ log$1.var({ error });
4615
+ await sleep(delay);
4616
+ attempt++;
4446
4617
  }
4447
4618
  }
4448
4619
  // Execute afterEachModelResponse hook