@jaypie/llm 1.2.8 → 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;
@@ -70,6 +70,8 @@ export interface OperateRequest {
70
70
  format?: JsonObject;
71
71
  /** Provider-specific options */
72
72
  providerOptions?: JsonObject;
73
+ /** Sampling temperature (0-2 for most providers) */
74
+ temperature?: number;
73
75
  /** User identifier for tracking */
74
76
  user?: string;
75
77
  }
@@ -223,6 +223,7 @@ export interface LlmOperateOptions {
223
223
  };
224
224
  providerOptions?: JsonObject;
225
225
  system?: string;
226
+ temperature?: number;
226
227
  tools?: LlmTool[] | Toolkit;
227
228
  turns?: boolean | number;
228
229
  user?: string;
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
@@ -990,6 +1074,10 @@ class AnthropicAdapter extends BaseProviderAdapter {
990
1074
  if (request.providerOptions) {
991
1075
  Object.assign(anthropicRequest, request.providerOptions);
992
1076
  }
1077
+ // First-class temperature takes precedence over providerOptions
1078
+ if (request.temperature !== undefined) {
1079
+ anthropicRequest.temperature = request.temperature;
1080
+ }
993
1081
  return anthropicRequest;
994
1082
  }
995
1083
  formatTools(toolkit, outputSchema) {
@@ -1262,6 +1350,14 @@ class AnthropicAdapter extends BaseProviderAdapter {
1262
1350
  shouldRetry: false,
1263
1351
  };
1264
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
+ }
1265
1361
  // Unknown error - treat as potentially retryable
1266
1362
  return {
1267
1363
  error,
@@ -1415,6 +1511,13 @@ class GeminiAdapter extends BaseProviderAdapter {
1415
1511
  ...request.providerOptions,
1416
1512
  };
1417
1513
  }
1514
+ // First-class temperature takes precedence over providerOptions
1515
+ if (request.temperature !== undefined) {
1516
+ geminiRequest.config = {
1517
+ ...geminiRequest.config,
1518
+ temperature: request.temperature,
1519
+ };
1520
+ }
1418
1521
  return geminiRequest;
1419
1522
  }
1420
1523
  formatTools(toolkit, outputSchema) {
@@ -1737,6 +1840,14 @@ class GeminiAdapter extends BaseProviderAdapter {
1737
1840
  shouldRetry: true,
1738
1841
  };
1739
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
+ }
1740
1851
  // Unknown error - treat as potentially retryable
1741
1852
  return {
1742
1853
  error,
@@ -2066,6 +2177,10 @@ class OpenAiAdapter extends BaseProviderAdapter {
2066
2177
  if (request.providerOptions) {
2067
2178
  Object.assign(openaiRequest, request.providerOptions);
2068
2179
  }
2180
+ // First-class temperature takes precedence over providerOptions
2181
+ if (request.temperature !== undefined) {
2182
+ openaiRequest.temperature = request.temperature;
2183
+ }
2069
2184
  return openaiRequest;
2070
2185
  }
2071
2186
  formatTools(toolkit, _outputSchema) {
@@ -2335,6 +2450,14 @@ class OpenAiAdapter extends BaseProviderAdapter {
2335
2450
  };
2336
2451
  }
2337
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
+ }
2338
2461
  // Unknown error - treat as potentially retryable
2339
2462
  return {
2340
2463
  error,
@@ -2496,6 +2619,11 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2496
2619
  if (request.providerOptions) {
2497
2620
  Object.assign(openRouterRequest, request.providerOptions);
2498
2621
  }
2622
+ // First-class temperature takes precedence over providerOptions
2623
+ if (request.temperature !== undefined) {
2624
+ openRouterRequest.temperature =
2625
+ request.temperature;
2626
+ }
2499
2627
  return openRouterRequest;
2500
2628
  }
2501
2629
  formatTools(toolkit, outputSchema) {
@@ -2816,6 +2944,14 @@ class OpenRouterAdapter extends BaseProviderAdapter {
2816
2944
  suggestedDelayMs: 60000,
2817
2945
  };
2818
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
+ }
2819
2955
  // Unknown error - treat as potentially retryable
2820
2956
  return {
2821
2957
  error,
@@ -3905,6 +4041,7 @@ class OperateLoop {
3905
4041
  model: options.model ?? this.adapter.defaultModel,
3906
4042
  providerOptions: options.providerOptions,
3907
4043
  system: options.system,
4044
+ temperature: options.temperature,
3908
4045
  tools: state.formattedTools,
3909
4046
  user: options.user,
3910
4047
  };
@@ -3984,6 +4121,7 @@ class OperateLoop {
3984
4121
  model: options.model ?? this.adapter.defaultModel,
3985
4122
  providerOptions: options.providerOptions,
3986
4123
  system: options.system,
4124
+ temperature: options.temperature,
3987
4125
  tools: state.formattedTools,
3988
4126
  user: options.user,
3989
4127
  };
@@ -4265,6 +4403,7 @@ class StreamLoop {
4265
4403
  this.client = config.client;
4266
4404
  this.hookRunnerInstance = config.hookRunner ?? hookRunner;
4267
4405
  this.inputProcessorInstance = config.inputProcessor ?? inputProcessor;
4406
+ this.retryPolicy = config.retryPolicy ?? defaultRetryPolicy;
4268
4407
  }
4269
4408
  /**
4270
4409
  * Execute the streaming loop for multi-turn conversations with tool calling.
@@ -4314,6 +4453,7 @@ class StreamLoop {
4314
4453
  model: options.model ?? this.adapter.defaultModel,
4315
4454
  providerOptions: options.providerOptions,
4316
4455
  system: options.system,
4456
+ temperature: options.temperature,
4317
4457
  tools: state.formattedTools,
4318
4458
  user: options.user,
4319
4459
  };
@@ -4380,6 +4520,7 @@ class StreamLoop {
4380
4520
  model: options.model ?? this.adapter.defaultModel,
4381
4521
  providerOptions: options.providerOptions,
4382
4522
  system: options.system,
4523
+ temperature: options.temperature,
4383
4524
  tools: state.formattedTools,
4384
4525
  user: options.user,
4385
4526
  };
@@ -4395,30 +4536,75 @@ class StreamLoop {
4395
4536
  });
4396
4537
  // Collect tool calls from the stream
4397
4538
  const collectedToolCalls = [];
4398
- // Execute streaming request
4399
- const streamGenerator = this.adapter.executeStreamRequest(this.client, providerRequest);
4400
- for await (const chunk of streamGenerator) {
4401
- // Pass through text chunks
4402
- if (chunk.type === LlmStreamChunkType.Text) {
4403
- yield chunk;
4404
- }
4405
- // Collect tool calls
4406
- if (chunk.type === LlmStreamChunkType.ToolCall) {
4407
- collectedToolCalls.push({
4408
- callId: chunk.toolCall.id,
4409
- name: chunk.toolCall.name,
4410
- arguments: chunk.toolCall.arguments,
4411
- raw: chunk.toolCall,
4412
- });
4413
- yield chunk;
4414
- }
4415
- // Track usage from done chunk (but don't yield it yet - we'll emit our own)
4416
- if (chunk.type === LlmStreamChunkType.Done && chunk.usage) {
4417
- 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;
4418
4578
  }
4419
- // Pass through error chunks
4420
- if (chunk.type === LlmStreamChunkType.Error) {
4421
- 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++;
4422
4608
  }
4423
4609
  }
4424
4610
  // Execute afterEachModelResponse hook