@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.
@@ -807,6 +807,90 @@ var ErrorCategory;
807
807
  ErrorCategory["Unknown"] = "unknown";
808
808
  })(ErrorCategory || (ErrorCategory = {}));
809
809
 
810
+ /**
811
+ * Transient network error detection utility.
812
+ *
813
+ * Detects low-level Node.js/undici network errors that indicate
814
+ * a temporary network issue (not a provider API error).
815
+ * These errors should always be retried.
816
+ */
817
+ //
818
+ //
819
+ // Constants
820
+ //
821
+ /** Error codes from Node.js net/dns subsystems that indicate transient failures */
822
+ const TRANSIENT_ERROR_CODES = new Set([
823
+ "ECONNREFUSED",
824
+ "ECONNRESET",
825
+ "EAI_AGAIN",
826
+ "ENETRESET",
827
+ "ENETUNREACH",
828
+ "ENOTFOUND",
829
+ "EPIPE",
830
+ "ETIMEDOUT",
831
+ ]);
832
+ /** Substrings in error messages that indicate transient network issues */
833
+ const TRANSIENT_MESSAGE_PATTERNS = [
834
+ "network",
835
+ "socket hang up",
836
+ "terminated",
837
+ ];
838
+ //
839
+ //
840
+ // Helpers
841
+ //
842
+ /**
843
+ * Check a single error (without walking the cause chain)
844
+ */
845
+ function matchesSingleError(error) {
846
+ if (!(error instanceof Error))
847
+ return false;
848
+ // Check error code (e.g., ECONNRESET)
849
+ const code = error.code;
850
+ if (code && TRANSIENT_ERROR_CODES.has(code)) {
851
+ return true;
852
+ }
853
+ // Check error message for transient patterns
854
+ const message = error.message.toLowerCase();
855
+ for (const pattern of TRANSIENT_MESSAGE_PATTERNS) {
856
+ if (message.includes(pattern)) {
857
+ return true;
858
+ }
859
+ }
860
+ return false;
861
+ }
862
+ //
863
+ //
864
+ // Main
865
+ //
866
+ /**
867
+ * Detect transient network errors by inspecting the error and its cause chain.
868
+ *
869
+ * Undici (Node.js fetch) wraps low-level errors like ECONNRESET inside
870
+ * `TypeError: terminated`. This function recursively walks `error.cause`
871
+ * to detect these wrapped errors.
872
+ *
873
+ * @param error - The error to inspect
874
+ * @returns true if the error (or any cause in its chain) is a transient network error
875
+ */
876
+ function isTransientNetworkError(error) {
877
+ let current = error;
878
+ while (current) {
879
+ if (matchesSingleError(current)) {
880
+ return true;
881
+ }
882
+ // Walk the cause chain (cause is ES2022, cast for compatibility)
883
+ const cause = current.cause;
884
+ if (current instanceof Error && cause) {
885
+ current = cause;
886
+ }
887
+ else {
888
+ break;
889
+ }
890
+ }
891
+ return false;
892
+ }
893
+
810
894
  //
811
895
  //
812
896
  // Constants
@@ -1268,6 +1352,14 @@ class AnthropicAdapter extends BaseProviderAdapter {
1268
1352
  shouldRetry: false,
1269
1353
  };
1270
1354
  }
1355
+ // Check for transient network errors (ECONNRESET, etc.)
1356
+ if (isTransientNetworkError(error)) {
1357
+ return {
1358
+ error,
1359
+ category: ErrorCategory.Retryable,
1360
+ shouldRetry: true,
1361
+ };
1362
+ }
1271
1363
  // Unknown error - treat as potentially retryable
1272
1364
  return {
1273
1365
  error,
@@ -1750,6 +1842,14 @@ class GeminiAdapter extends BaseProviderAdapter {
1750
1842
  shouldRetry: true,
1751
1843
  };
1752
1844
  }
1845
+ // Check for transient network errors (ECONNRESET, etc.)
1846
+ if (isTransientNetworkError(error)) {
1847
+ return {
1848
+ error,
1849
+ category: ErrorCategory.Retryable,
1850
+ shouldRetry: true,
1851
+ };
1852
+ }
1753
1853
  // Unknown error - treat as potentially retryable
1754
1854
  return {
1755
1855
  error,
@@ -2352,6 +2452,14 @@ class OpenAiAdapter extends BaseProviderAdapter {
2352
2452
  };
2353
2453
  }
2354
2454
  }
2455
+ // Check for transient network errors (ECONNRESET, etc.)
2456
+ if (isTransientNetworkError(error)) {
2457
+ return {
2458
+ error,
2459
+ category: ErrorCategory.Retryable,
2460
+ shouldRetry: true,
2461
+ };
2462
+ }
2355
2463
  // Unknown error - treat as potentially retryable
2356
2464
  return {
2357
2465
  error,
@@ -2565,7 +2673,7 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2565
2673
  //
2566
2674
  // API Execution
2567
2675
  //
2568
- async executeRequest(client, request) {
2676
+ async executeRequest(client, request, signal) {
2569
2677
  const openRouter = client;
2570
2678
  const openRouterRequest = request;
2571
2679
  const response = await openRouter.chat.send({
@@ -2574,10 +2682,10 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2574
2682
  tools: openRouterRequest.tools,
2575
2683
  toolChoice: openRouterRequest.tool_choice,
2576
2684
  user: openRouterRequest.user,
2577
- });
2685
+ }, signal ? { signal } : undefined);
2578
2686
  return response;
2579
2687
  }
2580
- async *executeStreamRequest(client, request) {
2688
+ async *executeStreamRequest(client, request, signal) {
2581
2689
  const openRouter = client;
2582
2690
  const openRouterRequest = request;
2583
2691
  // Use chat.send with stream: true for streaming responses
@@ -2588,7 +2696,7 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2588
2696
  toolChoice: openRouterRequest.tool_choice,
2589
2697
  user: openRouterRequest.user,
2590
2698
  stream: true,
2591
- });
2699
+ }, signal ? { signal } : undefined);
2592
2700
  // Track current tool call being built
2593
2701
  let currentToolCall = null;
2594
2702
  // Track usage for final chunk
@@ -2838,6 +2946,14 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2838
2946
  suggestedDelayMs: 60000,
2839
2947
  };
2840
2948
  }
2949
+ // Check for transient network errors (ECONNRESET, etc.)
2950
+ if (isTransientNetworkError(error)) {
2951
+ return {
2952
+ error,
2953
+ category: ErrorCategory.Retryable,
2954
+ shouldRetry: true,
2955
+ };
2956
+ }
2841
2957
  // Unknown error - treat as potentially retryable
2842
2958
  return {
2843
2959
  error,
@@ -3791,9 +3907,12 @@ class RetryExecutor {
3791
3907
  this.errorClassifier = config.errorClassifier;
3792
3908
  }
3793
3909
  /**
3794
- * Execute an operation with retry logic
3910
+ * Execute an operation with retry logic.
3911
+ * Each attempt receives an AbortSignal. On failure, the signal is aborted
3912
+ * before sleeping — this kills lingering socket callbacks from the previous
3913
+ * request and prevents stale async errors from escaping the retry loop.
3795
3914
  *
3796
- * @param operation - The async operation to execute
3915
+ * @param operation - The async operation to execute (receives AbortSignal)
3797
3916
  * @param options - Execution options including context and hooks
3798
3917
  * @returns The result of the operation
3799
3918
  * @throws BadGatewayError if all retries are exhausted or error is not retryable
@@ -3801,14 +3920,17 @@ class RetryExecutor {
3801
3920
  async execute(operation, options) {
3802
3921
  let attempt = 0;
3803
3922
  while (true) {
3923
+ const controller = new AbortController();
3804
3924
  try {
3805
- const result = await operation();
3925
+ const result = await operation(controller.signal);
3806
3926
  if (attempt > 0) {
3807
3927
  log$1.debug(`API call succeeded after ${attempt} retries`);
3808
3928
  }
3809
3929
  return result;
3810
3930
  }
3811
3931
  catch (error) {
3932
+ // Abort the previous request to kill lingering socket callbacks
3933
+ controller.abort("retry");
3812
3934
  // Check if we've exhausted retries
3813
3935
  if (!this.policy.shouldRetry(attempt)) {
3814
3936
  log$1.error(`API call failed after ${this.policy.maxRetries} retries`);
@@ -4033,7 +4155,7 @@ class OperateLoop {
4033
4155
  providerRequest,
4034
4156
  });
4035
4157
  // Execute with retry (RetryExecutor handles error hooks and throws appropriate errors)
4036
- const response = await retryExecutor.execute(() => this.adapter.executeRequest(this.client, providerRequest), {
4158
+ const response = await retryExecutor.execute((signal) => this.adapter.executeRequest(this.client, providerRequest, signal), {
4037
4159
  context: {
4038
4160
  input: state.currentInput,
4039
4161
  options,
@@ -4289,6 +4411,7 @@ class StreamLoop {
4289
4411
  this.client = config.client;
4290
4412
  this.hookRunnerInstance = config.hookRunner ?? hookRunner;
4291
4413
  this.inputProcessorInstance = config.inputProcessor ?? inputProcessor;
4414
+ this.retryPolicy = config.retryPolicy ?? defaultRetryPolicy;
4292
4415
  }
4293
4416
  /**
4294
4417
  * Execute the streaming loop for multi-turn conversations with tool calling.
@@ -4421,30 +4544,78 @@ class StreamLoop {
4421
4544
  });
4422
4545
  // Collect tool calls from the stream
4423
4546
  const collectedToolCalls = [];
4424
- // Execute streaming request
4425
- const streamGenerator = this.adapter.executeStreamRequest(this.client, providerRequest);
4426
- for await (const chunk of streamGenerator) {
4427
- // Pass through text chunks
4428
- if (chunk.type === exports.LlmStreamChunkType.Text) {
4429
- yield chunk;
4430
- }
4431
- // Collect tool calls
4432
- if (chunk.type === exports.LlmStreamChunkType.ToolCall) {
4433
- collectedToolCalls.push({
4434
- callId: chunk.toolCall.id,
4435
- name: chunk.toolCall.name,
4436
- arguments: chunk.toolCall.arguments,
4437
- raw: chunk.toolCall,
4438
- });
4439
- yield chunk;
4440
- }
4441
- // Track usage from done chunk (but don't yield it yet - we'll emit our own)
4442
- if (chunk.type === exports.LlmStreamChunkType.Done && chunk.usage) {
4443
- state.usageItems.push(...chunk.usage);
4547
+ // Retry loop for connection-level failures
4548
+ let attempt = 0;
4549
+ let chunksYielded = false;
4550
+ while (true) {
4551
+ const controller = new AbortController();
4552
+ try {
4553
+ // Execute streaming request
4554
+ const streamGenerator = this.adapter.executeStreamRequest(this.client, providerRequest, controller.signal);
4555
+ for await (const chunk of streamGenerator) {
4556
+ // Pass through text chunks
4557
+ if (chunk.type === exports.LlmStreamChunkType.Text) {
4558
+ chunksYielded = true;
4559
+ yield chunk;
4560
+ }
4561
+ // Collect tool calls
4562
+ if (chunk.type === exports.LlmStreamChunkType.ToolCall) {
4563
+ chunksYielded = true;
4564
+ collectedToolCalls.push({
4565
+ callId: chunk.toolCall.id,
4566
+ name: chunk.toolCall.name,
4567
+ arguments: chunk.toolCall.arguments,
4568
+ raw: chunk.toolCall,
4569
+ });
4570
+ yield chunk;
4571
+ }
4572
+ // Track usage from done chunk (but don't yield it yet - we'll emit our own)
4573
+ if (chunk.type === exports.LlmStreamChunkType.Done && chunk.usage) {
4574
+ state.usageItems.push(...chunk.usage);
4575
+ }
4576
+ // Pass through error chunks
4577
+ if (chunk.type === exports.LlmStreamChunkType.Error) {
4578
+ chunksYielded = true;
4579
+ yield chunk;
4580
+ }
4581
+ }
4582
+ // Stream completed successfully
4583
+ if (attempt > 0) {
4584
+ log$1.debug(`Stream request succeeded after ${attempt} retries`);
4585
+ }
4586
+ break;
4444
4587
  }
4445
- // Pass through error chunks
4446
- if (chunk.type === exports.LlmStreamChunkType.Error) {
4447
- yield chunk;
4588
+ catch (error) {
4589
+ // Abort the previous request to kill lingering socket callbacks
4590
+ controller.abort("retry");
4591
+ // If chunks were already yielded, we can't transparently retry
4592
+ if (chunksYielded) {
4593
+ const errorMessage = error instanceof Error ? error.message : String(error);
4594
+ log$1.error("Stream failed after partial data was delivered");
4595
+ log$1.var({ error });
4596
+ yield {
4597
+ type: exports.LlmStreamChunkType.Error,
4598
+ error: {
4599
+ detail: errorMessage,
4600
+ status: 502,
4601
+ title: "Stream Error",
4602
+ },
4603
+ };
4604
+ return { shouldContinue: false };
4605
+ }
4606
+ // Check if we've exhausted retries or error is not retryable
4607
+ if (!this.retryPolicy.shouldRetry(attempt) ||
4608
+ !this.adapter.isRetryableError(error)) {
4609
+ log$1.error(`Stream request failed after ${this.retryPolicy.maxRetries} retries`);
4610
+ log$1.var({ error });
4611
+ const errorMessage = error instanceof Error ? error.message : String(error);
4612
+ throw new errors.BadGatewayError(errorMessage);
4613
+ }
4614
+ const delay = this.retryPolicy.getDelayForAttempt(attempt);
4615
+ log$1.warn(`Stream request failed. Retrying in ${delay}ms...`);
4616
+ log$1.var({ error });
4617
+ await kit.sleep(delay);
4618
+ attempt++;
4448
4619
  }
4449
4620
  }
4450
4621
  // Execute afterEachModelResponse hook