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