@jaypie/llm 1.3.9 → 1.3.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/Llm.d.ts +19 -11
- package/dist/cjs/constants.d.ts +53 -4
- package/dist/cjs/errors/LlmError.d.ts +62 -0
- package/dist/cjs/errors/toLlmError.d.ts +11 -0
- package/dist/cjs/index.cjs +739 -177
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +177 -19
- package/dist/cjs/index.d.ts +5 -1
- package/dist/cjs/operate/adapters/AnthropicAdapter.d.ts +3 -2
- package/dist/cjs/operate/adapters/BedrockAdapter.d.ts +1 -1
- package/dist/cjs/operate/adapters/GoogleAdapter.d.ts +1 -1
- package/dist/cjs/operate/adapters/OpenAiAdapter.d.ts +7 -1
- package/dist/cjs/operate/adapters/OpenRouterAdapter.d.ts +5 -1
- package/dist/cjs/operate/adapters/XaiAdapter.d.ts +11 -1
- package/dist/cjs/operate/retry/RetryExecutor.d.ts +13 -0
- package/dist/cjs/operate/types.d.ts +15 -1
- package/dist/cjs/providers/google/types.d.ts +5 -0
- package/dist/cjs/types/LlmProvider.interface.d.ts +18 -0
- package/dist/cjs/util/classifyProviderError.d.ts +13 -0
- package/dist/cjs/util/effort.d.ts +42 -0
- package/dist/cjs/util/resolveModelChain.d.ts +17 -0
- package/dist/esm/Llm.d.ts +19 -11
- package/dist/esm/constants.d.ts +53 -4
- package/dist/esm/errors/LlmError.d.ts +62 -0
- package/dist/esm/errors/toLlmError.d.ts +11 -0
- package/dist/esm/index.d.ts +177 -19
- package/dist/esm/index.js +705 -148
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/operate/adapters/AnthropicAdapter.d.ts +3 -2
- package/dist/esm/operate/adapters/BedrockAdapter.d.ts +1 -1
- package/dist/esm/operate/adapters/GoogleAdapter.d.ts +1 -1
- package/dist/esm/operate/adapters/OpenAiAdapter.d.ts +7 -1
- package/dist/esm/operate/adapters/OpenRouterAdapter.d.ts +5 -1
- package/dist/esm/operate/adapters/XaiAdapter.d.ts +11 -1
- package/dist/esm/operate/retry/RetryExecutor.d.ts +13 -0
- package/dist/esm/operate/types.d.ts +15 -1
- package/dist/esm/providers/google/types.d.ts +5 -0
- package/dist/esm/types/LlmProvider.interface.d.ts +18 -0
- package/dist/esm/util/classifyProviderError.d.ts +13 -0
- package/dist/esm/util/effort.d.ts +42 -0
- package/dist/esm/util/resolveModelChain.d.ts +17 -0
- package/package.json +1 -1
|
@@ -16,7 +16,8 @@ import { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
|
|
|
16
16
|
*/
|
|
17
17
|
type AnthropicRequestParams = Anthropic.MessageCreateParams & {
|
|
18
18
|
output_config?: {
|
|
19
|
-
|
|
19
|
+
effort?: string;
|
|
20
|
+
format?: {
|
|
20
21
|
type: "json_schema";
|
|
21
22
|
schema: JsonObject;
|
|
22
23
|
};
|
|
@@ -29,7 +30,7 @@ type AnthropicRequestParams = Anthropic.MessageCreateParams & {
|
|
|
29
30
|
*/
|
|
30
31
|
export declare class AnthropicAdapter extends BaseProviderAdapter {
|
|
31
32
|
readonly name: "anthropic";
|
|
32
|
-
readonly defaultModel:
|
|
33
|
+
readonly defaultModel: string;
|
|
33
34
|
private runtimeNoTemperatureModels;
|
|
34
35
|
private runtimeNoStructuredOutputModels;
|
|
35
36
|
rememberModelRejectsTemperature(model: string): void;
|
|
@@ -49,7 +49,7 @@ type AnnotatedBedrockResponse = ConverseCommandOutput & {
|
|
|
49
49
|
};
|
|
50
50
|
export declare class BedrockAdapter extends BaseProviderAdapter {
|
|
51
51
|
readonly name: "bedrock";
|
|
52
|
-
readonly defaultModel: "amazon.nova-
|
|
52
|
+
readonly defaultModel: "amazon.nova-pro-v1:0";
|
|
53
53
|
private _modelsFallbackToStructuredOutputTool;
|
|
54
54
|
private _modelsWithoutTemperature;
|
|
55
55
|
private rememberModelRejectsOutputConfig;
|
|
@@ -13,7 +13,7 @@ import { GeminiPart, GeminiRawResponse, GeminiRequest } from "../../providers/go
|
|
|
13
13
|
*/
|
|
14
14
|
export declare class GoogleAdapter extends BaseProviderAdapter {
|
|
15
15
|
readonly name: "google";
|
|
16
|
-
readonly defaultModel:
|
|
16
|
+
readonly defaultModel: string;
|
|
17
17
|
private runtimeNoStructuredOutputComboModels;
|
|
18
18
|
rememberModelRejectsStructuredOutputCombo(model: string): void;
|
|
19
19
|
clearRuntimeNoStructuredOutputComboModels(): void;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { JsonObject, NaturalSchema } from "@jaypie/types";
|
|
2
2
|
import { z } from "zod/v4";
|
|
3
|
+
import { type LlmEffort } from "../../constants.js";
|
|
4
|
+
import { type LlmEffortMapping } from "../../util/effort.js";
|
|
3
5
|
import { Toolkit } from "../../tools/Toolkit.class.js";
|
|
4
6
|
import { LlmHistory, LlmOperateOptions, LlmToolResult, LlmUsageItem } from "../../types/LlmProvider.interface.js";
|
|
5
7
|
import { LlmStreamChunk } from "../../types/LlmStreamChunk.interface.js";
|
|
@@ -12,11 +14,15 @@ import { BaseProviderAdapter } from "./ProviderAdapter.interface.js";
|
|
|
12
14
|
*/
|
|
13
15
|
export declare class OpenAiAdapter extends BaseProviderAdapter {
|
|
14
16
|
readonly name: "openai";
|
|
15
|
-
readonly defaultModel:
|
|
17
|
+
readonly defaultModel: string;
|
|
16
18
|
private runtimeNoTemperatureModels;
|
|
17
19
|
rememberModelRejectsTemperature(model: string): void;
|
|
18
20
|
clearRuntimeNoTemperatureModels(): void;
|
|
19
21
|
private supportsTemperature;
|
|
22
|
+
/** Whether `reasoning.effort` may be sent for this model. Overridden by xAI. */
|
|
23
|
+
protected supportsReasoningEffort(model: string): boolean;
|
|
24
|
+
/** Translate a normalized effort to this provider's `reasoning.effort` value. */
|
|
25
|
+
protected mapReasoningEffort(effort: LlmEffort, model: string): LlmEffortMapping;
|
|
20
26
|
buildRequest(request: OperateRequest): unknown;
|
|
21
27
|
formatTools(toolkit: Toolkit, _outputSchema?: JsonObject): ProviderToolDefinition[];
|
|
22
28
|
formatOutputSchema(schema: JsonObject | NaturalSchema | z.ZodType): JsonObject;
|
|
@@ -79,6 +79,10 @@ interface OpenRouterRequest {
|
|
|
79
79
|
tools?: OpenRouterTool[];
|
|
80
80
|
tool_choice?: "auto" | "none" | "required";
|
|
81
81
|
response_format?: OpenRouterResponseFormat;
|
|
82
|
+
reasoning?: {
|
|
83
|
+
effort?: string;
|
|
84
|
+
max_tokens?: number;
|
|
85
|
+
};
|
|
82
86
|
user?: string;
|
|
83
87
|
}
|
|
84
88
|
/**
|
|
@@ -111,7 +115,7 @@ type OpenRouterContentPart = {
|
|
|
111
115
|
*/
|
|
112
116
|
export declare class OpenRouterAdapter extends BaseProviderAdapter {
|
|
113
117
|
readonly name: "openrouter";
|
|
114
|
-
readonly defaultModel:
|
|
118
|
+
readonly defaultModel: string;
|
|
115
119
|
private runtimeNoStructuredOutputModels;
|
|
116
120
|
rememberModelRejectsStructuredOutput(model: string): void;
|
|
117
121
|
clearRuntimeNoStructuredOutputModels(): void;
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { type LlmEffort } from "../../constants.js";
|
|
2
|
+
import { type LlmEffortMapping } from "../../util/effort.js";
|
|
1
3
|
import { ClassifiedError } from "../types.js";
|
|
2
4
|
import { OpenAiAdapter } from "./OpenAiAdapter.js";
|
|
3
5
|
/**
|
|
@@ -8,7 +10,15 @@ import { OpenAiAdapter } from "./OpenAiAdapter.js";
|
|
|
8
10
|
*/
|
|
9
11
|
export declare class XaiAdapter extends OpenAiAdapter {
|
|
10
12
|
readonly name: "xai";
|
|
11
|
-
readonly defaultModel:
|
|
13
|
+
readonly defaultModel: string;
|
|
14
|
+
/**
|
|
15
|
+
* Grok gates reasoning effort by model, not by the OpenAI `gpt-*`/`o*`
|
|
16
|
+
* patterns. Only explicit `*-reasoning` models accept `reasoning_effort`;
|
|
17
|
+
* bare grok-4 reasons implicitly and rejects it, so we stay conservative and
|
|
18
|
+
* only opt in models whose name advertises reasoning.
|
|
19
|
+
*/
|
|
20
|
+
protected supportsReasoningEffort(model: string): boolean;
|
|
21
|
+
protected mapReasoningEffort(effort: LlmEffort): LlmEffortMapping;
|
|
12
22
|
classifyError(error: unknown): ClassifiedError;
|
|
13
23
|
}
|
|
14
24
|
export declare const xaiAdapter: XaiAdapter;
|
|
@@ -1,13 +1,20 @@
|
|
|
1
1
|
import { HookRunner, LlmHooks } from "../hooks/HookRunner.js";
|
|
2
2
|
import { RetryPolicy } from "./RetryPolicy.js";
|
|
3
|
+
import { ClassifiedError } from "../types.js";
|
|
3
4
|
export interface RetryContext {
|
|
4
5
|
input: unknown;
|
|
5
6
|
options?: unknown;
|
|
6
7
|
providerRequest: unknown;
|
|
8
|
+
/** Provider name, carried onto the thrown LlmError */
|
|
9
|
+
provider?: string;
|
|
10
|
+
/** Model in use, carried onto the thrown LlmError */
|
|
11
|
+
model?: string;
|
|
7
12
|
}
|
|
8
13
|
export interface ErrorClassifier {
|
|
9
14
|
isRetryable(error: unknown): boolean;
|
|
10
15
|
isKnownError(error: unknown): boolean;
|
|
16
|
+
/** Full classification, used to throw a typed LlmError on terminal failure */
|
|
17
|
+
classify(error: unknown): ClassifiedError;
|
|
11
18
|
}
|
|
12
19
|
export interface RetryExecutorConfig {
|
|
13
20
|
errorClassifier: ErrorClassifier;
|
|
@@ -27,6 +34,12 @@ export declare class RetryExecutor {
|
|
|
27
34
|
private readonly hookRunner;
|
|
28
35
|
private readonly errorClassifier;
|
|
29
36
|
constructor(config: RetryExecutorConfig);
|
|
37
|
+
/**
|
|
38
|
+
* Build the typed, provider-agnostic error thrown when a request cannot be
|
|
39
|
+
* completed — classified (rate limit / quota / unrecoverable / transient)
|
|
40
|
+
* and carrying the provider, model, and original error as `cause`.
|
|
41
|
+
*/
|
|
42
|
+
private toTerminalError;
|
|
30
43
|
/**
|
|
31
44
|
* Execute an operation with retry logic.
|
|
32
45
|
* Each attempt receives an AbortSignal. On failure, the signal is aborted
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { JsonObject } from "@jaypie/types";
|
|
2
|
+
import { type LlmEffort } from "../constants.js";
|
|
2
3
|
import { LlmHistory, LlmMessageType, LlmOperateOptions, LlmUsageItem } from "../types/LlmProvider.interface.js";
|
|
3
4
|
import { Toolkit } from "../tools/Toolkit.class.js";
|
|
4
5
|
/**
|
|
@@ -68,6 +69,8 @@ export interface OperateRequest {
|
|
|
68
69
|
tools?: ProviderToolDefinition[];
|
|
69
70
|
/** Structured output format */
|
|
70
71
|
format?: JsonObject;
|
|
72
|
+
/** Normalized reasoning effort; adapters translate to provider-native control */
|
|
73
|
+
effort?: LlmEffort;
|
|
71
74
|
/** Provider-specific options */
|
|
72
75
|
providerOptions?: JsonObject;
|
|
73
76
|
/** Whether the request will execute over a streaming transport */
|
|
@@ -94,8 +97,14 @@ export interface ProviderToolDefinition {
|
|
|
94
97
|
export declare enum ErrorCategory {
|
|
95
98
|
/** Error is transient and can be retried */
|
|
96
99
|
Retryable = "retryable",
|
|
97
|
-
/** Error is due to rate limiting */
|
|
100
|
+
/** Error is due to short-term rate limiting (retry after a delay) */
|
|
98
101
|
RateLimit = "rate_limit",
|
|
102
|
+
/**
|
|
103
|
+
* Provider quota is exhausted or the account cannot be billed
|
|
104
|
+
* (insufficient funds, daily quota, plan limit). Terminal and actionable —
|
|
105
|
+
* retrying within the request budget will not help.
|
|
106
|
+
*/
|
|
107
|
+
Quota = "quota",
|
|
99
108
|
/** Error cannot be recovered from */
|
|
100
109
|
Unrecoverable = "unrecoverable",
|
|
101
110
|
/** Error type is unknown */
|
|
@@ -113,6 +122,11 @@ export interface ClassifiedError {
|
|
|
113
122
|
shouldRetry: boolean;
|
|
114
123
|
/** Suggested delay before retry (if applicable) */
|
|
115
124
|
suggestedDelayMs?: number;
|
|
125
|
+
/**
|
|
126
|
+
* For {@link ErrorCategory.Quota}, distinguishes an exhausted usage quota
|
|
127
|
+
* from an account that cannot be billed (insufficient funds).
|
|
128
|
+
*/
|
|
129
|
+
reason?: "quota" | "billing";
|
|
116
130
|
}
|
|
117
131
|
import type { ResponseBuilder } from "./response/ResponseBuilder.js";
|
|
118
132
|
/**
|
|
@@ -73,6 +73,11 @@ export interface GeminiGenerateContentConfig {
|
|
|
73
73
|
responseMimeType?: string;
|
|
74
74
|
responseJsonSchema?: JsonObject;
|
|
75
75
|
responseSchema?: JsonObject;
|
|
76
|
+
thinkingConfig?: {
|
|
77
|
+
thinkingLevel?: string;
|
|
78
|
+
thinkingBudget?: number;
|
|
79
|
+
includeThoughts?: boolean;
|
|
80
|
+
};
|
|
76
81
|
temperature?: number;
|
|
77
82
|
topP?: number;
|
|
78
83
|
topK?: number;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AnyValue, JsonObject, JsonReturn, NaturalMap, NaturalSchema } from "@jaypie/types";
|
|
2
2
|
import { z } from "zod/v4";
|
|
3
|
+
import { type LlmEffort } from "../constants.js";
|
|
3
4
|
import { LlmTool } from "./LlmTool.interface.js";
|
|
4
5
|
import { LlmStreamChunk } from "./LlmStreamChunk.interface.js";
|
|
5
6
|
import { Toolkit } from "../tools/Toolkit.class.js";
|
|
@@ -213,6 +214,14 @@ export interface LlmProgressEvent {
|
|
|
213
214
|
export type LlmProgressCallback = (event: LlmProgressEvent) => unknown | Promise<unknown>;
|
|
214
215
|
export interface LlmOperateOptions {
|
|
215
216
|
data?: NaturalMap;
|
|
217
|
+
/**
|
|
218
|
+
* Provider-neutral reasoning effort (lowest | low | medium | high | highest).
|
|
219
|
+
* Each provider translates it to its native control, spreading the scale
|
|
220
|
+
* across the provider's range; omitting it leaves the provider default
|
|
221
|
+
* untouched, so it is safe across a fallback chain. Providers without
|
|
222
|
+
* reasoning control ignore it.
|
|
223
|
+
*/
|
|
224
|
+
effort?: LlmEffort;
|
|
216
225
|
explain?: boolean;
|
|
217
226
|
/** Chain of fallback providers to try if primary fails. Set to false to disable instance-level fallback. */
|
|
218
227
|
fallback?: LlmFallbackConfig[] | false;
|
|
@@ -286,6 +295,15 @@ export interface LlmOperateOptions {
|
|
|
286
295
|
turns?: boolean | number;
|
|
287
296
|
user?: string;
|
|
288
297
|
}
|
|
298
|
+
/**
|
|
299
|
+
* A single model name, or a preference-ordered array of model names.
|
|
300
|
+
* An array is interpreted as a fallback chain: index 0 is primary, later
|
|
301
|
+
* entries are tried in order when earlier ones fail (provider auto-detected
|
|
302
|
+
* per entry). Accepted by the `Llm` constructor and static/instance
|
|
303
|
+
* `operate`/`send`/`stream` methods; normalized to a scalar model plus a
|
|
304
|
+
* derived fallback chain before reaching providers.
|
|
305
|
+
*/
|
|
306
|
+
export type LlmModelOption = string | string[];
|
|
289
307
|
export interface LlmOptions {
|
|
290
308
|
apiKey?: string;
|
|
291
309
|
/** Chain of fallback providers to try if primary fails */
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ClassifiedError } from "../operate/types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Shared, provider-agnostic first pass over an error. Returns a
|
|
4
|
+
* {@link ClassifiedError} when the message unambiguously identifies a
|
|
5
|
+
* cross-provider condition (retryable structured-output timeout, exhausted
|
|
6
|
+
* quota, or a billing failure), or `undefined` to defer to the adapter's own
|
|
7
|
+
* status/name classification.
|
|
8
|
+
*
|
|
9
|
+
* Adapters call this before their existing logic so that, for example, a
|
|
10
|
+
* `429` carrying a daily-quota message is classified as {@link
|
|
11
|
+
* ErrorCategory.Quota} rather than {@link ErrorCategory.RateLimit}.
|
|
12
|
+
*/
|
|
13
|
+
export declare function classifyProviderError(error: unknown): ClassifiedError | undefined;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { type LlmEffort } from "../constants.js";
|
|
2
|
+
/**
|
|
3
|
+
* Result of translating the provider-neutral {@link LlmEffort} scale to a
|
|
4
|
+
* provider's native reasoning control.
|
|
5
|
+
*/
|
|
6
|
+
export interface LlmEffortMapping<T extends string | number = string> {
|
|
7
|
+
/**
|
|
8
|
+
* True when the requested neutral level had no distinct native rung and was
|
|
9
|
+
* collapsed or clamped onto a neighbor (e.g. `highest` -> Grok `high`, or
|
|
10
|
+
* `highest` -> OpenAI `high` on a model predating `xhigh`). Adapters log
|
|
11
|
+
* these at debug so a papered-over request stays on the record.
|
|
12
|
+
*/
|
|
13
|
+
papered: boolean;
|
|
14
|
+
/** Native effort value for the target provider. */
|
|
15
|
+
value: T;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Per-provider translation of the provider-neutral {@link LlmEffort} scale.
|
|
19
|
+
*
|
|
20
|
+
* The neutral enum (lowest → low → medium → high → highest) is a relative
|
|
21
|
+
* five-point scale. Each table below maps it onto the target provider's native
|
|
22
|
+
* effort control, keeping `medium`/`high` semantically aligned across providers
|
|
23
|
+
* and using each provider's extra bottom (`minimal`) or top (`xhigh`/`max`)
|
|
24
|
+
* rung where one exists. Providers with fewer levels collapse the ends and mark
|
|
25
|
+
* the result `papered`. A single `effort` value is therefore safe to reuse
|
|
26
|
+
* across providers and fallback chains.
|
|
27
|
+
*/
|
|
28
|
+
/** Consistent debug message for a papered-over effort level. */
|
|
29
|
+
export declare function paperedEffortMessage({ model, provider, requested, value, }: {
|
|
30
|
+
model: string;
|
|
31
|
+
provider: string;
|
|
32
|
+
requested: LlmEffort;
|
|
33
|
+
value: string | number;
|
|
34
|
+
}): string;
|
|
35
|
+
export declare function toOpenAiEffort(effort: LlmEffort, { model }: {
|
|
36
|
+
model: string;
|
|
37
|
+
}): LlmEffortMapping;
|
|
38
|
+
export declare function toXaiEffort(effort: LlmEffort): LlmEffortMapping;
|
|
39
|
+
export declare function toAnthropicEffort(effort: LlmEffort): LlmEffortMapping;
|
|
40
|
+
export declare function toGeminiThinkingLevel(effort: LlmEffort): LlmEffortMapping;
|
|
41
|
+
export declare function toGeminiThinkingBudget(effort: LlmEffort): LlmEffortMapping<number>;
|
|
42
|
+
export declare function toOpenRouterEffort(effort: LlmEffort): LlmEffortMapping;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { LlmFallbackConfig } from "../types/LlmProvider.interface.js";
|
|
2
|
+
/**
|
|
3
|
+
* Normalizes a `model` option that may be a single model name or a
|
|
4
|
+
* preference-ordered array of model names.
|
|
5
|
+
*
|
|
6
|
+
* A `string[]` is interpreted as a fallback chain: index `0` is the primary
|
|
7
|
+
* model, indices `1..` become fallback entries with an auto-detected provider
|
|
8
|
+
* (reusing `determineModelProvider`, exactly as the constructor's first
|
|
9
|
+
* argument does).
|
|
10
|
+
*
|
|
11
|
+
* Returns the primary model plus the derived fallback chain. A bare string
|
|
12
|
+
* yields an empty chain and is unchanged.
|
|
13
|
+
*/
|
|
14
|
+
export declare function resolveModelChain(model?: string | string[]): {
|
|
15
|
+
model?: string;
|
|
16
|
+
fallback: LlmFallbackConfig[];
|
|
17
|
+
};
|