@jaypie/llm 1.2.9 → 1.2.10

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,
@@ -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,
@@ -4289,6 +4405,7 @@ class StreamLoop {
4289
4405
  this.client = config.client;
4290
4406
  this.hookRunnerInstance = config.hookRunner ?? hookRunner;
4291
4407
  this.inputProcessorInstance = config.inputProcessor ?? inputProcessor;
4408
+ this.retryPolicy = config.retryPolicy ?? defaultRetryPolicy;
4292
4409
  }
4293
4410
  /**
4294
4411
  * Execute the streaming loop for multi-turn conversations with tool calling.
@@ -4421,30 +4538,75 @@ class StreamLoop {
4421
4538
  });
4422
4539
  // Collect tool calls from the stream
4423
4540
  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);
4541
+ // Retry loop for connection-level failures
4542
+ let attempt = 0;
4543
+ let chunksYielded = false;
4544
+ while (true) {
4545
+ try {
4546
+ // Execute streaming request
4547
+ const streamGenerator = this.adapter.executeStreamRequest(this.client, providerRequest);
4548
+ for await (const chunk of streamGenerator) {
4549
+ // Pass through text chunks
4550
+ if (chunk.type === exports.LlmStreamChunkType.Text) {
4551
+ chunksYielded = true;
4552
+ yield chunk;
4553
+ }
4554
+ // Collect tool calls
4555
+ if (chunk.type === exports.LlmStreamChunkType.ToolCall) {
4556
+ chunksYielded = true;
4557
+ collectedToolCalls.push({
4558
+ callId: chunk.toolCall.id,
4559
+ name: chunk.toolCall.name,
4560
+ arguments: chunk.toolCall.arguments,
4561
+ raw: chunk.toolCall,
4562
+ });
4563
+ yield chunk;
4564
+ }
4565
+ // Track usage from done chunk (but don't yield it yet - we'll emit our own)
4566
+ if (chunk.type === exports.LlmStreamChunkType.Done && chunk.usage) {
4567
+ state.usageItems.push(...chunk.usage);
4568
+ }
4569
+ // Pass through error chunks
4570
+ if (chunk.type === exports.LlmStreamChunkType.Error) {
4571
+ chunksYielded = true;
4572
+ yield chunk;
4573
+ }
4574
+ }
4575
+ // Stream completed successfully
4576
+ if (attempt > 0) {
4577
+ log$1.debug(`Stream request succeeded after ${attempt} retries`);
4578
+ }
4579
+ break;
4444
4580
  }
4445
- // Pass through error chunks
4446
- if (chunk.type === exports.LlmStreamChunkType.Error) {
4447
- yield chunk;
4581
+ catch (error) {
4582
+ // If chunks were already yielded, we can't transparently retry
4583
+ if (chunksYielded) {
4584
+ const errorMessage = error instanceof Error ? error.message : String(error);
4585
+ log$1.error("Stream failed after partial data was delivered");
4586
+ log$1.var({ error });
4587
+ yield {
4588
+ type: exports.LlmStreamChunkType.Error,
4589
+ error: {
4590
+ detail: errorMessage,
4591
+ status: 502,
4592
+ title: "Stream Error",
4593
+ },
4594
+ };
4595
+ return { shouldContinue: false };
4596
+ }
4597
+ // Check if we've exhausted retries or error is not retryable
4598
+ if (!this.retryPolicy.shouldRetry(attempt) ||
4599
+ !this.adapter.isRetryableError(error)) {
4600
+ log$1.error(`Stream request failed after ${this.retryPolicy.maxRetries} retries`);
4601
+ log$1.var({ error });
4602
+ const errorMessage = error instanceof Error ? error.message : String(error);
4603
+ throw new errors.BadGatewayError(errorMessage);
4604
+ }
4605
+ const delay = this.retryPolicy.getDelayForAttempt(attempt);
4606
+ log$1.warn(`Stream request failed. Retrying in ${delay}ms...`);
4607
+ log$1.var({ error });
4608
+ await kit.sleep(delay);
4609
+ attempt++;
4448
4610
  }
4449
4611
  }
4450
4612
  // Execute afterEachModelResponse hook