@yourgpt/llm-sdk 2.5.1-beta.6 → 2.6.0-beta.1
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/README.md +77 -0
- package/dist/adapters/index.d.mts +54 -5
- package/dist/adapters/index.d.ts +54 -5
- package/dist/adapters/index.js +466 -169
- package/dist/adapters/index.mjs +466 -169
- package/dist/{base-C58Dsr9p.d.ts → base-BdTpUAWI.d.ts} +2 -1
- package/dist/{base-tNgbBaSo.d.mts → base-CkeT7u-O.d.mts} +2 -1
- package/dist/fallback/index.d.mts +4 -4
- package/dist/fallback/index.d.ts +4 -4
- package/dist/fallback/index.js +75 -39
- package/dist/fallback/index.mjs +75 -39
- package/dist/index.d.mts +8 -7
- package/dist/index.d.ts +8 -7
- package/dist/index.js +89 -30
- package/dist/index.mjs +89 -30
- package/dist/providers/anthropic/index.d.mts +3 -3
- package/dist/providers/anthropic/index.d.ts +3 -3
- package/dist/providers/azure/index.d.mts +3 -3
- package/dist/providers/azure/index.d.ts +3 -3
- package/dist/providers/azure/index.js +8 -3
- package/dist/providers/azure/index.mjs +8 -3
- package/dist/providers/fireworks/index.d.mts +3 -3
- package/dist/providers/fireworks/index.d.ts +3 -3
- package/dist/providers/fireworks/index.js +466 -169
- package/dist/providers/fireworks/index.mjs +466 -169
- package/dist/providers/google/index.d.mts +3 -3
- package/dist/providers/google/index.d.ts +3 -3
- package/dist/providers/google/index.js +466 -169
- package/dist/providers/google/index.mjs +466 -169
- package/dist/providers/ollama/index.d.mts +4 -4
- package/dist/providers/ollama/index.d.ts +4 -4
- package/dist/providers/openai/index.d.mts +6 -23
- package/dist/providers/openai/index.d.ts +6 -23
- package/dist/providers/openai/index.js +912 -500
- package/dist/providers/openai/index.mjs +912 -500
- package/dist/providers/openrouter/index.d.mts +3 -3
- package/dist/providers/openrouter/index.d.ts +3 -3
- package/dist/providers/openrouter/index.js +466 -169
- package/dist/providers/openrouter/index.mjs +466 -169
- package/dist/providers/togetherai/index.d.mts +3 -3
- package/dist/providers/togetherai/index.d.ts +3 -3
- package/dist/providers/togetherai/index.js +466 -169
- package/dist/providers/togetherai/index.mjs +466 -169
- package/dist/providers/xai/index.d.mts +3 -3
- package/dist/providers/xai/index.d.ts +3 -3
- package/dist/providers/xai/index.js +466 -169
- package/dist/providers/xai/index.mjs +466 -169
- package/dist/{types-BkQCSiIt.d.mts → types-BW3csYo5.d.mts} +22 -1
- package/dist/{types-BkQCSiIt.d.ts → types-BW3csYo5.d.ts} +22 -1
- package/dist/{types-B6dhnguR.d.mts → types-BWRYIvl_.d.mts} +11 -4
- package/dist/{types-CCxPmkmK.d.ts → types-CpXcT-tY.d.ts} +11 -4
- package/dist/{types-BQ31QIsA.d.ts → types-DA54bSmX.d.ts} +3 -11
- package/dist/{types-BSSiJW2o.d.mts → types-NOswlhm9.d.mts} +3 -11
- package/dist/yourgpt/index.d.mts +1 -1
- package/dist/yourgpt/index.d.ts +1 -1
- package/package.json +115 -48
- package/LICENSE +0 -21
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { M as Message, A as ActionDefinition, d as ToolDefinition, R as ResponseFormat, W as WebSearchConfig, P as ProviderToolRuntimeOptions, g as StreamEvent, J as TokenUsage,
|
|
1
|
+
import { M as Message, A as ActionDefinition, d as ToolDefinition, O as OpenAIReasoningEffort, R as ResponseFormat, W as WebSearchConfig, P as ProviderToolRuntimeOptions, g as StreamEvent, J as TokenUsage, Y as LLMConfig, a8 as MessageAttachment } from './types-BW3csYo5.js';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Request-level LLM configuration overrides
|
|
5
5
|
*/
|
|
6
6
|
interface RequestLLMConfig {
|
|
7
7
|
model?: string;
|
|
8
|
+
reasoningEffort?: OpenAIReasoningEffort;
|
|
8
9
|
temperature?: number;
|
|
9
10
|
maxTokens?: number;
|
|
10
11
|
responseFormat?: ResponseFormat;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { M as Message, A as ActionDefinition, d as ToolDefinition, R as ResponseFormat, W as WebSearchConfig, P as ProviderToolRuntimeOptions, g as StreamEvent, J as TokenUsage,
|
|
1
|
+
import { M as Message, A as ActionDefinition, d as ToolDefinition, O as OpenAIReasoningEffort, R as ResponseFormat, W as WebSearchConfig, P as ProviderToolRuntimeOptions, g as StreamEvent, J as TokenUsage, Y as LLMConfig, a8 as MessageAttachment } from './types-BW3csYo5.mjs';
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
4
|
* Request-level LLM configuration overrides
|
|
5
5
|
*/
|
|
6
6
|
interface RequestLLMConfig {
|
|
7
7
|
model?: string;
|
|
8
|
+
reasoningEffort?: OpenAIReasoningEffort;
|
|
8
9
|
temperature?: number;
|
|
9
10
|
maxTokens?: number;
|
|
10
11
|
responseFormat?: ResponseFormat;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { L as LLMAdapter } from '../base-
|
|
2
|
-
import { F as FallbackChainConfig, c as FallbackFailure, R as RoutingStore } from '../types-
|
|
3
|
-
export { d as FallbackInfo, b as RetryBackoff, e as RetryInfo, a as RoutingStrategy } from '../types-
|
|
4
|
-
import '../types-
|
|
1
|
+
import { L as LLMAdapter } from '../base-CkeT7u-O.mjs';
|
|
2
|
+
import { F as FallbackChainConfig, c as FallbackFailure, R as RoutingStore } from '../types-BWRYIvl_.mjs';
|
|
3
|
+
export { d as FallbackInfo, b as RetryBackoff, e as RetryInfo, a as RoutingStrategy } from '../types-BWRYIvl_.mjs';
|
|
4
|
+
import '../types-BW3csYo5.mjs';
|
|
5
5
|
import 'zod';
|
|
6
6
|
|
|
7
7
|
/**
|
package/dist/fallback/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { L as LLMAdapter } from '../base-
|
|
2
|
-
import { F as FallbackChainConfig, c as FallbackFailure, R as RoutingStore } from '../types-
|
|
3
|
-
export { d as FallbackInfo, b as RetryBackoff, e as RetryInfo, a as RoutingStrategy } from '../types-
|
|
4
|
-
import '../types-
|
|
1
|
+
import { L as LLMAdapter } from '../base-BdTpUAWI.js';
|
|
2
|
+
import { F as FallbackChainConfig, c as FallbackFailure, R as RoutingStore } from '../types-CpXcT-tY.js';
|
|
3
|
+
export { d as FallbackInfo, b as RetryBackoff, e as RetryInfo, a as RoutingStrategy } from '../types-CpXcT-tY.js';
|
|
4
|
+
import '../types-BW3csYo5.js';
|
|
5
5
|
import 'zod';
|
|
6
6
|
|
|
7
7
|
/**
|
package/dist/fallback/index.js
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
// src/core/provider-error.ts
|
|
4
|
+
function errorFromEvent(event) {
|
|
5
|
+
if (event.cause instanceof Error) return event.cause;
|
|
6
|
+
return Object.assign(new Error(event.message), {
|
|
7
|
+
status: event.status,
|
|
8
|
+
code: event.providerCode ?? event.code,
|
|
9
|
+
param: event.param,
|
|
10
|
+
request_id: event.requestId
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
function throwIfAborted(signal) {
|
|
14
|
+
if (!signal?.aborted) return;
|
|
15
|
+
const error = new Error("The operation was aborted");
|
|
16
|
+
error.name = "AbortError";
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
function isAbortError(error) {
|
|
20
|
+
if (!(error instanceof Error)) return false;
|
|
21
|
+
return error.name === "AbortError" || error.constructor.name === "APIUserAbortError" || /^(aborted|the operation was aborted|the user aborted a request)/i.test(
|
|
22
|
+
error.message
|
|
23
|
+
);
|
|
24
|
+
}
|
|
25
|
+
|
|
3
26
|
// src/fallback/errors.ts
|
|
4
27
|
var FallbackExhaustedError = class extends Error {
|
|
5
28
|
constructor(failures) {
|
|
@@ -74,8 +97,24 @@ function calcDelay(base, attempt, backoff) {
|
|
|
74
97
|
if (backoff === "fixed") return base;
|
|
75
98
|
return base * Math.pow(2, attempt - 1);
|
|
76
99
|
}
|
|
77
|
-
function sleep(ms) {
|
|
78
|
-
|
|
100
|
+
function sleep(ms, signal) {
|
|
101
|
+
throwIfAborted(signal);
|
|
102
|
+
return new Promise((resolve, reject) => {
|
|
103
|
+
const onAbort = () => {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
signal?.removeEventListener("abort", onAbort);
|
|
106
|
+
try {
|
|
107
|
+
throwIfAborted(signal);
|
|
108
|
+
} catch (error) {
|
|
109
|
+
reject(error);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const timer = setTimeout(() => {
|
|
113
|
+
signal?.removeEventListener("abort", onAbort);
|
|
114
|
+
resolve();
|
|
115
|
+
}, ms);
|
|
116
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
117
|
+
});
|
|
79
118
|
}
|
|
80
119
|
var FallbackChain = class {
|
|
81
120
|
constructor(config) {
|
|
@@ -91,7 +130,8 @@ var FallbackChain = class {
|
|
|
91
130
|
retryBackoff: config.retryBackoff ?? "exponential",
|
|
92
131
|
onFallback: config.onFallback,
|
|
93
132
|
onRetry: config.onRetry,
|
|
94
|
-
retryableErrors: config.retryableErrors
|
|
133
|
+
retryableErrors: config.retryableErrors,
|
|
134
|
+
fallbackErrors: config.fallbackErrors
|
|
95
135
|
};
|
|
96
136
|
}
|
|
97
137
|
get provider() {
|
|
@@ -111,8 +151,12 @@ var FallbackChain = class {
|
|
|
111
151
|
await this._config.store.set(ROUND_ROBIN_KEY, next);
|
|
112
152
|
}
|
|
113
153
|
_isRetryable(error) {
|
|
154
|
+
if (isAbortError(error)) return false;
|
|
114
155
|
return this._config.retryableErrors ? this._config.retryableErrors(error) : defaultIsRetryable(error);
|
|
115
156
|
}
|
|
157
|
+
_canFallback(error) {
|
|
158
|
+
return !isAbortError(error) && (this._isRetryable(error) || this._config.fallbackErrors?.(error) === true);
|
|
159
|
+
}
|
|
116
160
|
/**
|
|
117
161
|
* Try streaming from a single adapter, with per-model retries.
|
|
118
162
|
*
|
|
@@ -125,65 +169,52 @@ var FallbackChain = class {
|
|
|
125
169
|
*/
|
|
126
170
|
async *_streamWithRetries(adapter, request, out) {
|
|
127
171
|
const { retries, retryDelay, retryBackoff, onRetry } = this._config;
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
let failureError = null;
|
|
172
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
173
|
+
throwIfAborted(request.signal);
|
|
174
|
+
out.retriesAttempted = attempt;
|
|
132
175
|
try {
|
|
133
176
|
for await (const chunk of adapter.stream(request)) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
const msg = chunk.message ?? "Unknown error";
|
|
137
|
-
failureError = new Error(msg);
|
|
138
|
-
break;
|
|
139
|
-
}
|
|
140
|
-
yield chunk;
|
|
141
|
-
return;
|
|
142
|
-
}
|
|
177
|
+
throwIfAborted(request.signal);
|
|
178
|
+
if (chunk.type === "error") throw errorFromEvent(chunk);
|
|
143
179
|
if (chunk.type === "message:start") continue;
|
|
144
|
-
contentStarted = true;
|
|
180
|
+
out.contentStarted = true;
|
|
145
181
|
yield chunk;
|
|
146
182
|
}
|
|
183
|
+
throwIfAborted(request.signal);
|
|
184
|
+
return;
|
|
147
185
|
} catch (error) {
|
|
148
|
-
|
|
149
|
-
if (!this._isRetryable(error)
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
if (!this._isRetryable(failureError)) throw failureError;
|
|
154
|
-
out.retriesAttempted = attempt - 1;
|
|
155
|
-
if (attempt < maxAttempts) {
|
|
156
|
-
const delayMs = calcDelay(retryDelay, attempt, retryBackoff);
|
|
157
|
-
const retryInfo = {
|
|
186
|
+
throwIfAborted(request.signal);
|
|
187
|
+
if (out.contentStarted || !this._isRetryable(error) || attempt === retries)
|
|
188
|
+
throw error;
|
|
189
|
+
const delayMs = calcDelay(retryDelay, attempt + 1, retryBackoff);
|
|
190
|
+
onRetry?.({
|
|
158
191
|
model: adapter.model,
|
|
159
192
|
provider: adapter.provider,
|
|
160
|
-
error:
|
|
161
|
-
retryAttempt: attempt,
|
|
193
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
194
|
+
retryAttempt: attempt + 1,
|
|
162
195
|
maxRetries: retries,
|
|
163
196
|
delayMs
|
|
164
|
-
};
|
|
165
|
-
|
|
166
|
-
await sleep(delayMs);
|
|
167
|
-
continue;
|
|
197
|
+
});
|
|
198
|
+
await sleep(delayMs, request.signal);
|
|
168
199
|
}
|
|
169
|
-
out.retriesAttempted = retries;
|
|
170
|
-
throw failureError;
|
|
171
200
|
}
|
|
172
201
|
}
|
|
173
202
|
async *stream(request) {
|
|
174
203
|
const { models, onFallback } = this._config;
|
|
204
|
+
throwIfAborted(request.signal);
|
|
175
205
|
const startIndex = await this._startIndex();
|
|
176
206
|
const failures = [];
|
|
177
207
|
for (let i = 0; i < models.length; i++) {
|
|
178
208
|
const index = (startIndex + i) % models.length;
|
|
179
209
|
const adapter = models[index];
|
|
180
|
-
const out = { retriesAttempted: 0 };
|
|
210
|
+
const out = { retriesAttempted: 0, contentStarted: false };
|
|
181
211
|
try {
|
|
182
212
|
yield* this._streamWithRetries(adapter, request, out);
|
|
183
213
|
await this._advanceIndex(index);
|
|
184
214
|
return;
|
|
185
215
|
} catch (error) {
|
|
186
|
-
|
|
216
|
+
throwIfAborted(request.signal);
|
|
217
|
+
if (out.contentStarted || !this._canFallback(error)) throw error;
|
|
187
218
|
const failure = {
|
|
188
219
|
model: adapter.model,
|
|
189
220
|
provider: adapter.provider,
|
|
@@ -208,6 +239,7 @@ var FallbackChain = class {
|
|
|
208
239
|
}
|
|
209
240
|
async complete(request) {
|
|
210
241
|
const { models, onFallback, retries, retryDelay, retryBackoff, onRetry } = this._config;
|
|
242
|
+
throwIfAborted(request.signal);
|
|
211
243
|
const startIndex = await this._startIndex();
|
|
212
244
|
const failures = [];
|
|
213
245
|
for (let i = 0; i < models.length; i++) {
|
|
@@ -229,14 +261,18 @@ var FallbackChain = class {
|
|
|
229
261
|
let lastError = null;
|
|
230
262
|
let retriesAttempted = 0;
|
|
231
263
|
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
264
|
+
throwIfAborted(request.signal);
|
|
232
265
|
try {
|
|
233
266
|
const result = await adapter.complete(request);
|
|
267
|
+
throwIfAborted(request.signal);
|
|
234
268
|
await this._advanceIndex(index);
|
|
235
269
|
return result;
|
|
236
270
|
} catch (error) {
|
|
237
|
-
|
|
271
|
+
throwIfAborted(request.signal);
|
|
272
|
+
if (!this._canFallback(error)) throw error;
|
|
238
273
|
lastError = error instanceof Error ? error : new Error(String(error));
|
|
239
274
|
retriesAttempted = attempt - 1;
|
|
275
|
+
if (!this._isRetryable(error)) break;
|
|
240
276
|
if (attempt < maxAttempts) {
|
|
241
277
|
const delayMs = calcDelay(retryDelay, attempt, retryBackoff);
|
|
242
278
|
onRetry?.({
|
|
@@ -247,7 +283,7 @@ var FallbackChain = class {
|
|
|
247
283
|
maxRetries: retries,
|
|
248
284
|
delayMs
|
|
249
285
|
});
|
|
250
|
-
await sleep(delayMs);
|
|
286
|
+
await sleep(delayMs, request.signal);
|
|
251
287
|
}
|
|
252
288
|
}
|
|
253
289
|
}
|
package/dist/fallback/index.mjs
CHANGED
|
@@ -1,3 +1,26 @@
|
|
|
1
|
+
// src/core/provider-error.ts
|
|
2
|
+
function errorFromEvent(event) {
|
|
3
|
+
if (event.cause instanceof Error) return event.cause;
|
|
4
|
+
return Object.assign(new Error(event.message), {
|
|
5
|
+
status: event.status,
|
|
6
|
+
code: event.providerCode ?? event.code,
|
|
7
|
+
param: event.param,
|
|
8
|
+
request_id: event.requestId
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
function throwIfAborted(signal) {
|
|
12
|
+
if (!signal?.aborted) return;
|
|
13
|
+
const error = new Error("The operation was aborted");
|
|
14
|
+
error.name = "AbortError";
|
|
15
|
+
throw error;
|
|
16
|
+
}
|
|
17
|
+
function isAbortError(error) {
|
|
18
|
+
if (!(error instanceof Error)) return false;
|
|
19
|
+
return error.name === "AbortError" || error.constructor.name === "APIUserAbortError" || /^(aborted|the operation was aborted|the user aborted a request)/i.test(
|
|
20
|
+
error.message
|
|
21
|
+
);
|
|
22
|
+
}
|
|
23
|
+
|
|
1
24
|
// src/fallback/errors.ts
|
|
2
25
|
var FallbackExhaustedError = class extends Error {
|
|
3
26
|
constructor(failures) {
|
|
@@ -72,8 +95,24 @@ function calcDelay(base, attempt, backoff) {
|
|
|
72
95
|
if (backoff === "fixed") return base;
|
|
73
96
|
return base * Math.pow(2, attempt - 1);
|
|
74
97
|
}
|
|
75
|
-
function sleep(ms) {
|
|
76
|
-
|
|
98
|
+
function sleep(ms, signal) {
|
|
99
|
+
throwIfAborted(signal);
|
|
100
|
+
return new Promise((resolve, reject) => {
|
|
101
|
+
const onAbort = () => {
|
|
102
|
+
clearTimeout(timer);
|
|
103
|
+
signal?.removeEventListener("abort", onAbort);
|
|
104
|
+
try {
|
|
105
|
+
throwIfAborted(signal);
|
|
106
|
+
} catch (error) {
|
|
107
|
+
reject(error);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
const timer = setTimeout(() => {
|
|
111
|
+
signal?.removeEventListener("abort", onAbort);
|
|
112
|
+
resolve();
|
|
113
|
+
}, ms);
|
|
114
|
+
signal?.addEventListener("abort", onAbort, { once: true });
|
|
115
|
+
});
|
|
77
116
|
}
|
|
78
117
|
var FallbackChain = class {
|
|
79
118
|
constructor(config) {
|
|
@@ -89,7 +128,8 @@ var FallbackChain = class {
|
|
|
89
128
|
retryBackoff: config.retryBackoff ?? "exponential",
|
|
90
129
|
onFallback: config.onFallback,
|
|
91
130
|
onRetry: config.onRetry,
|
|
92
|
-
retryableErrors: config.retryableErrors
|
|
131
|
+
retryableErrors: config.retryableErrors,
|
|
132
|
+
fallbackErrors: config.fallbackErrors
|
|
93
133
|
};
|
|
94
134
|
}
|
|
95
135
|
get provider() {
|
|
@@ -109,8 +149,12 @@ var FallbackChain = class {
|
|
|
109
149
|
await this._config.store.set(ROUND_ROBIN_KEY, next);
|
|
110
150
|
}
|
|
111
151
|
_isRetryable(error) {
|
|
152
|
+
if (isAbortError(error)) return false;
|
|
112
153
|
return this._config.retryableErrors ? this._config.retryableErrors(error) : defaultIsRetryable(error);
|
|
113
154
|
}
|
|
155
|
+
_canFallback(error) {
|
|
156
|
+
return !isAbortError(error) && (this._isRetryable(error) || this._config.fallbackErrors?.(error) === true);
|
|
157
|
+
}
|
|
114
158
|
/**
|
|
115
159
|
* Try streaming from a single adapter, with per-model retries.
|
|
116
160
|
*
|
|
@@ -123,65 +167,52 @@ var FallbackChain = class {
|
|
|
123
167
|
*/
|
|
124
168
|
async *_streamWithRetries(adapter, request, out) {
|
|
125
169
|
const { retries, retryDelay, retryBackoff, onRetry } = this._config;
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
let failureError = null;
|
|
170
|
+
for (let attempt = 0; attempt <= retries; attempt++) {
|
|
171
|
+
throwIfAborted(request.signal);
|
|
172
|
+
out.retriesAttempted = attempt;
|
|
130
173
|
try {
|
|
131
174
|
for await (const chunk of adapter.stream(request)) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const msg = chunk.message ?? "Unknown error";
|
|
135
|
-
failureError = new Error(msg);
|
|
136
|
-
break;
|
|
137
|
-
}
|
|
138
|
-
yield chunk;
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
175
|
+
throwIfAborted(request.signal);
|
|
176
|
+
if (chunk.type === "error") throw errorFromEvent(chunk);
|
|
141
177
|
if (chunk.type === "message:start") continue;
|
|
142
|
-
contentStarted = true;
|
|
178
|
+
out.contentStarted = true;
|
|
143
179
|
yield chunk;
|
|
144
180
|
}
|
|
181
|
+
throwIfAborted(request.signal);
|
|
182
|
+
return;
|
|
145
183
|
} catch (error) {
|
|
146
|
-
|
|
147
|
-
if (!this._isRetryable(error)
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
if (!this._isRetryable(failureError)) throw failureError;
|
|
152
|
-
out.retriesAttempted = attempt - 1;
|
|
153
|
-
if (attempt < maxAttempts) {
|
|
154
|
-
const delayMs = calcDelay(retryDelay, attempt, retryBackoff);
|
|
155
|
-
const retryInfo = {
|
|
184
|
+
throwIfAborted(request.signal);
|
|
185
|
+
if (out.contentStarted || !this._isRetryable(error) || attempt === retries)
|
|
186
|
+
throw error;
|
|
187
|
+
const delayMs = calcDelay(retryDelay, attempt + 1, retryBackoff);
|
|
188
|
+
onRetry?.({
|
|
156
189
|
model: adapter.model,
|
|
157
190
|
provider: adapter.provider,
|
|
158
|
-
error:
|
|
159
|
-
retryAttempt: attempt,
|
|
191
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
192
|
+
retryAttempt: attempt + 1,
|
|
160
193
|
maxRetries: retries,
|
|
161
194
|
delayMs
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
await sleep(delayMs);
|
|
165
|
-
continue;
|
|
195
|
+
});
|
|
196
|
+
await sleep(delayMs, request.signal);
|
|
166
197
|
}
|
|
167
|
-
out.retriesAttempted = retries;
|
|
168
|
-
throw failureError;
|
|
169
198
|
}
|
|
170
199
|
}
|
|
171
200
|
async *stream(request) {
|
|
172
201
|
const { models, onFallback } = this._config;
|
|
202
|
+
throwIfAborted(request.signal);
|
|
173
203
|
const startIndex = await this._startIndex();
|
|
174
204
|
const failures = [];
|
|
175
205
|
for (let i = 0; i < models.length; i++) {
|
|
176
206
|
const index = (startIndex + i) % models.length;
|
|
177
207
|
const adapter = models[index];
|
|
178
|
-
const out = { retriesAttempted: 0 };
|
|
208
|
+
const out = { retriesAttempted: 0, contentStarted: false };
|
|
179
209
|
try {
|
|
180
210
|
yield* this._streamWithRetries(adapter, request, out);
|
|
181
211
|
await this._advanceIndex(index);
|
|
182
212
|
return;
|
|
183
213
|
} catch (error) {
|
|
184
|
-
|
|
214
|
+
throwIfAborted(request.signal);
|
|
215
|
+
if (out.contentStarted || !this._canFallback(error)) throw error;
|
|
185
216
|
const failure = {
|
|
186
217
|
model: adapter.model,
|
|
187
218
|
provider: adapter.provider,
|
|
@@ -206,6 +237,7 @@ var FallbackChain = class {
|
|
|
206
237
|
}
|
|
207
238
|
async complete(request) {
|
|
208
239
|
const { models, onFallback, retries, retryDelay, retryBackoff, onRetry } = this._config;
|
|
240
|
+
throwIfAborted(request.signal);
|
|
209
241
|
const startIndex = await this._startIndex();
|
|
210
242
|
const failures = [];
|
|
211
243
|
for (let i = 0; i < models.length; i++) {
|
|
@@ -227,14 +259,18 @@ var FallbackChain = class {
|
|
|
227
259
|
let lastError = null;
|
|
228
260
|
let retriesAttempted = 0;
|
|
229
261
|
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
262
|
+
throwIfAborted(request.signal);
|
|
230
263
|
try {
|
|
231
264
|
const result = await adapter.complete(request);
|
|
265
|
+
throwIfAborted(request.signal);
|
|
232
266
|
await this._advanceIndex(index);
|
|
233
267
|
return result;
|
|
234
268
|
} catch (error) {
|
|
235
|
-
|
|
269
|
+
throwIfAborted(request.signal);
|
|
270
|
+
if (!this._canFallback(error)) throw error;
|
|
236
271
|
lastError = error instanceof Error ? error : new Error(String(error));
|
|
237
272
|
retriesAttempted = attempt - 1;
|
|
273
|
+
if (!this._isRetryable(error)) break;
|
|
238
274
|
if (attempt < maxAttempts) {
|
|
239
275
|
const delayMs = calcDelay(retryDelay, attempt, retryBackoff);
|
|
240
276
|
onRetry?.({
|
|
@@ -245,7 +281,7 @@ var FallbackChain = class {
|
|
|
245
281
|
maxRetries: retries,
|
|
246
282
|
delayMs
|
|
247
283
|
});
|
|
248
|
-
await sleep(delayMs);
|
|
284
|
+
await sleep(delayMs, request.signal);
|
|
249
285
|
}
|
|
250
286
|
}
|
|
251
287
|
}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool, A as ActionDefinition, d as ToolDefinition, e as ToolProfile, K as KnowledgeBaseConfig, W as WebSearchConfig, f as StorageAdapter, R as ResponseFormat, D as DoneEventMessage, g as StreamEvent, h as ToolCallInfo, i as TokenUsageRaw, P as ProviderToolRuntimeOptions, M as Message, j as ToolResponse, k as StorageMessage } from './types-
|
|
2
|
-
export {
|
|
1
|
+
import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool, A as ActionDefinition, d as ToolDefinition, e as ToolProfile, K as KnowledgeBaseConfig, W as WebSearchConfig, f as StorageAdapter, O as OpenAIReasoningEffort, R as ResponseFormat, D as DoneEventMessage, g as StreamEvent, h as ToolCallInfo, i as TokenUsageRaw, P as ProviderToolRuntimeOptions, M as Message, j as ToolResponse, k as StorageMessage } from './types-BW3csYo5.mjs';
|
|
2
|
+
export { a5 as AnthropicProviderToolOptions, a2 as AnthropicToolSelectionHints, p as AssistantMessage, a6 as Citation, C as CoreMessage, X as DEFAULT_CAPABILITIES, m as DoGenerateParams, n as DoGenerateResult, H as ErrorChunk, F as FilePart, E as FinishChunk, N as FinishReason, v as GenerateStep, I as ImagePart, Y as LLMConfig, L as LanguageModel, l as ModelCapabilities, a4 as OpenAIProviderToolOptions, a1 as OpenAIToolSelectionHints, Q as ResponseOptions, V as StorageFile, x as StreamChunk, w as StreamPart, o as SystemMessage, y as TextDeltaChunk, s as TextPart, J as TokenUsage, t as ToolCall, z as ToolCallChunk, a0 as ToolExecution, Z as ToolLocation, q as ToolMessage, a3 as ToolNativeProviderHints, u as ToolResult, B as ToolResultChunk, _ as UnifiedToolCall, $ as UnifiedToolResult, r as UserContentPart, U as UserMessage } from './types-BW3csYo5.mjs';
|
|
3
3
|
import { z } from 'zod';
|
|
4
|
-
import { A as AIProvider } from './types-
|
|
5
|
-
export { a as AnthropicProviderConfig, f as AnthropicTool, h as AnthropicToolResult, g as AnthropicToolUse, b as AzureProviderConfig, B as BaseProviderConfig, m as GeminiFunctionCall, l as GeminiFunctionDeclaration, n as GeminiFunctionResponse, G as GoogleProviderConfig, d as OllamaModelOptions, c as OllamaProviderConfig, O as OpenAIProviderConfig, i as OpenAITool, j as OpenAIToolCall, k as OpenAIToolResult, P as ProviderCapabilities, e as ProviderFormatter, X as XAIProviderConfig } from './types-
|
|
6
|
-
import { L as LLMAdapter } from './base-
|
|
7
|
-
export { A as AdapterFactory, C as ChatCompletionRequest } from './base-
|
|
4
|
+
import { A as AIProvider } from './types-NOswlhm9.mjs';
|
|
5
|
+
export { a as AnthropicProviderConfig, f as AnthropicTool, h as AnthropicToolResult, g as AnthropicToolUse, b as AzureProviderConfig, B as BaseProviderConfig, m as GeminiFunctionCall, l as GeminiFunctionDeclaration, n as GeminiFunctionResponse, G as GoogleProviderConfig, d as OllamaModelOptions, c as OllamaProviderConfig, O as OpenAIProviderConfig, i as OpenAITool, j as OpenAIToolCall, k as OpenAIToolResult, P as ProviderCapabilities, e as ProviderFormatter, X as XAIProviderConfig } from './types-NOswlhm9.mjs';
|
|
6
|
+
import { L as LLMAdapter } from './base-CkeT7u-O.mjs';
|
|
7
|
+
export { A as AdapterFactory, C as ChatCompletionRequest } from './base-CkeT7u-O.mjs';
|
|
8
8
|
import * as hono from 'hono';
|
|
9
9
|
import { Hono } from 'hono';
|
|
10
|
-
export { F as FallbackChainConfig, c as FallbackFailure, d as FallbackInfo, b as RetryBackoff, e as RetryInfo, R as RoutingStore, a as RoutingStrategy } from './types-
|
|
10
|
+
export { F as FallbackChainConfig, c as FallbackFailure, d as FallbackInfo, b as RetryBackoff, e as RetryInfo, R as RoutingStore, a as RoutingStrategy } from './types-BWRYIvl_.mjs';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* generateText - Generate text using a language model
|
|
@@ -386,6 +386,7 @@ interface ChatRequest {
|
|
|
386
386
|
botId?: string;
|
|
387
387
|
/** LLM config overrides */
|
|
388
388
|
config?: {
|
|
389
|
+
reasoningEffort?: OpenAIReasoningEffort;
|
|
389
390
|
temperature?: number;
|
|
390
391
|
maxTokens?: number;
|
|
391
392
|
responseFormat?: ResponseFormat;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool, A as ActionDefinition, d as ToolDefinition, e as ToolProfile, K as KnowledgeBaseConfig, W as WebSearchConfig, f as StorageAdapter, R as ResponseFormat, D as DoneEventMessage, g as StreamEvent, h as ToolCallInfo, i as TokenUsageRaw, P as ProviderToolRuntimeOptions, M as Message, j as ToolResponse, k as StorageMessage } from './types-
|
|
2
|
-
export {
|
|
1
|
+
import { G as GenerateTextParams, a as GenerateTextResult, S as StreamTextParams, b as StreamTextResult, T as ToolContext, c as Tool, A as ActionDefinition, d as ToolDefinition, e as ToolProfile, K as KnowledgeBaseConfig, W as WebSearchConfig, f as StorageAdapter, O as OpenAIReasoningEffort, R as ResponseFormat, D as DoneEventMessage, g as StreamEvent, h as ToolCallInfo, i as TokenUsageRaw, P as ProviderToolRuntimeOptions, M as Message, j as ToolResponse, k as StorageMessage } from './types-BW3csYo5.js';
|
|
2
|
+
export { a5 as AnthropicProviderToolOptions, a2 as AnthropicToolSelectionHints, p as AssistantMessage, a6 as Citation, C as CoreMessage, X as DEFAULT_CAPABILITIES, m as DoGenerateParams, n as DoGenerateResult, H as ErrorChunk, F as FilePart, E as FinishChunk, N as FinishReason, v as GenerateStep, I as ImagePart, Y as LLMConfig, L as LanguageModel, l as ModelCapabilities, a4 as OpenAIProviderToolOptions, a1 as OpenAIToolSelectionHints, Q as ResponseOptions, V as StorageFile, x as StreamChunk, w as StreamPart, o as SystemMessage, y as TextDeltaChunk, s as TextPart, J as TokenUsage, t as ToolCall, z as ToolCallChunk, a0 as ToolExecution, Z as ToolLocation, q as ToolMessage, a3 as ToolNativeProviderHints, u as ToolResult, B as ToolResultChunk, _ as UnifiedToolCall, $ as UnifiedToolResult, r as UserContentPart, U as UserMessage } from './types-BW3csYo5.js';
|
|
3
3
|
import { z } from 'zod';
|
|
4
|
-
import { A as AIProvider } from './types-
|
|
5
|
-
export { a as AnthropicProviderConfig, f as AnthropicTool, h as AnthropicToolResult, g as AnthropicToolUse, b as AzureProviderConfig, B as BaseProviderConfig, m as GeminiFunctionCall, l as GeminiFunctionDeclaration, n as GeminiFunctionResponse, G as GoogleProviderConfig, d as OllamaModelOptions, c as OllamaProviderConfig, O as OpenAIProviderConfig, i as OpenAITool, j as OpenAIToolCall, k as OpenAIToolResult, P as ProviderCapabilities, e as ProviderFormatter, X as XAIProviderConfig } from './types-
|
|
6
|
-
import { L as LLMAdapter } from './base-
|
|
7
|
-
export { A as AdapterFactory, C as ChatCompletionRequest } from './base-
|
|
4
|
+
import { A as AIProvider } from './types-DA54bSmX.js';
|
|
5
|
+
export { a as AnthropicProviderConfig, f as AnthropicTool, h as AnthropicToolResult, g as AnthropicToolUse, b as AzureProviderConfig, B as BaseProviderConfig, m as GeminiFunctionCall, l as GeminiFunctionDeclaration, n as GeminiFunctionResponse, G as GoogleProviderConfig, d as OllamaModelOptions, c as OllamaProviderConfig, O as OpenAIProviderConfig, i as OpenAITool, j as OpenAIToolCall, k as OpenAIToolResult, P as ProviderCapabilities, e as ProviderFormatter, X as XAIProviderConfig } from './types-DA54bSmX.js';
|
|
6
|
+
import { L as LLMAdapter } from './base-BdTpUAWI.js';
|
|
7
|
+
export { A as AdapterFactory, C as ChatCompletionRequest } from './base-BdTpUAWI.js';
|
|
8
8
|
import * as hono from 'hono';
|
|
9
9
|
import { Hono } from 'hono';
|
|
10
|
-
export { F as FallbackChainConfig, c as FallbackFailure, d as FallbackInfo, b as RetryBackoff, e as RetryInfo, R as RoutingStore, a as RoutingStrategy } from './types-
|
|
10
|
+
export { F as FallbackChainConfig, c as FallbackFailure, d as FallbackInfo, b as RetryBackoff, e as RetryInfo, R as RoutingStore, a as RoutingStrategy } from './types-CpXcT-tY.js';
|
|
11
11
|
|
|
12
12
|
/**
|
|
13
13
|
* generateText - Generate text using a language model
|
|
@@ -386,6 +386,7 @@ interface ChatRequest {
|
|
|
386
386
|
botId?: string;
|
|
387
387
|
/** LLM config overrides */
|
|
388
388
|
config?: {
|
|
389
|
+
reasoningEffort?: OpenAIReasoningEffort;
|
|
389
390
|
temperature?: number;
|
|
390
391
|
maxTokens?: number;
|
|
391
392
|
responseFormat?: ResponseFormat;
|