@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.
- package/dist/cjs/index.cjs +202 -31
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/operate/StreamLoop.d.ts +3 -0
- package/dist/cjs/operate/adapters/OpenRouterAdapter.d.ts +2 -2
- package/dist/cjs/operate/adapters/ProviderAdapter.interface.d.ts +5 -3
- package/dist/cjs/operate/retry/RetryExecutor.d.ts +6 -3
- package/dist/cjs/operate/retry/index.d.ts +1 -0
- package/dist/cjs/operate/retry/isTransientNetworkError.d.ts +18 -0
- package/dist/esm/index.js +202 -31
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/operate/StreamLoop.d.ts +3 -0
- package/dist/esm/operate/adapters/OpenRouterAdapter.d.ts +2 -2
- package/dist/esm/operate/adapters/ProviderAdapter.interface.d.ts +5 -3
- package/dist/esm/operate/retry/RetryExecutor.d.ts +6 -3
- package/dist/esm/operate/retry/index.d.ts +1 -0
- package/dist/esm/operate/retry/isTransientNetworkError.d.ts +18 -0
- package/package.json +1 -1
|
@@ -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
|
|
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;
|