@drsamonline/providers 0.0.0-stage → 0.12.3
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/package.json +29 -4
- package/src/index.js +563 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,31 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@drsamonline/providers",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.12.3",
|
|
4
|
+
"description": "Provider adapters: openai, anthropic, gemini, mock.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./src/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js",
|
|
10
|
+
"./src/*": "./src/*"
|
|
11
|
+
},
|
|
12
|
+
"files": [
|
|
13
|
+
"src",
|
|
14
|
+
"web",
|
|
15
|
+
"bin",
|
|
16
|
+
"LICENSE"
|
|
17
|
+
],
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git://github.com/drsamonline/sui.git",
|
|
21
|
+
"directory": "packages/providers"
|
|
22
|
+
},
|
|
23
|
+
"homepage": "https://github.com/drsamonline/sui#readme",
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public",
|
|
26
|
+
"registry": "https://registry.npmjs.org/"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"@drsamonline/core": "^0.12.3"
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,563 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider adapters.
|
|
3
|
+
*
|
|
4
|
+
* Three wire formats cover essentially everything people route to today:
|
|
5
|
+
* - openai OpenAI, Groq, Together, Fireworks, OpenRouter, DeepSeek, vLLM, Ollama, LM Studio
|
|
6
|
+
* - anthropic Claude
|
|
7
|
+
* - gemini Google AI Studio / Vertex express
|
|
8
|
+
* plus `mock`, which answers in-process so the router can be tested and demoed
|
|
9
|
+
* with no keys and no network.
|
|
10
|
+
*
|
|
11
|
+
* Every adapter returns the same normalised shape, so routing never has to
|
|
12
|
+
* care which format the other end speaks.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { ProviderError } from '@drsamonline/core';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @typedef {{
|
|
19
|
+
* text: string, usage: {prompt_tokens: number, completion_tokens: number},
|
|
20
|
+
* finishReason: string|null, raw: any
|
|
21
|
+
* }} NormalisedResult
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const DEFAULT_TIMEOUT_MS = 60000;
|
|
25
|
+
|
|
26
|
+
/** Human names, so error messages read like sentences. */
|
|
27
|
+
const DISPLAY = {
|
|
28
|
+
groq: 'Groq',
|
|
29
|
+
gemini: 'Gemini',
|
|
30
|
+
openai: 'OpenAI',
|
|
31
|
+
anthropic: 'Anthropic',
|
|
32
|
+
openrouter: 'OpenRouter',
|
|
33
|
+
ollama: 'Ollama',
|
|
34
|
+
mock: 'the mock provider',
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export function displayName(providerId) {
|
|
38
|
+
return DISPLAY[providerId] ?? providerId;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* @param {string} providerId
|
|
43
|
+
* @param {any} provider
|
|
44
|
+
* @param {NodeJS.ProcessEnv} [env]
|
|
45
|
+
*/
|
|
46
|
+
/**
|
|
47
|
+
* B9.5 — access tokens for OAuth-style providers.
|
|
48
|
+
*
|
|
49
|
+
* A provider that hands out a short-lived token is not a provider with a broken
|
|
50
|
+
* key. On a 401 we refresh **once**, retry **once**, and cache the token until
|
|
51
|
+
* its stated expiry. A refresh loop would turn one bad credential into an outage.
|
|
52
|
+
*
|
|
53
|
+
* @type {Map<string, {token: string, expiresAt: number}>}
|
|
54
|
+
*/
|
|
55
|
+
const tokenCache = new Map();
|
|
56
|
+
|
|
57
|
+
/** @param {string} providerId */
|
|
58
|
+
export function clearToken(providerId) {
|
|
59
|
+
tokenCache.delete(providerId);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* @param {{providerId: string, provider: any, env: NodeJS.ProcessEnv, fetchImpl?: typeof fetch, now?: () => number}} input
|
|
64
|
+
*/
|
|
65
|
+
export async function refreshAccessToken(input) {
|
|
66
|
+
const oauth = input.provider?.oauth;
|
|
67
|
+
if (!oauth?.tokenUrl) throw new ProviderError(`Provider "${input.providerId}" has no tokenUrl to refresh against.`, { code: 'no_oauth' });
|
|
68
|
+
|
|
69
|
+
const clientId = input.env[oauth.clientIdEnv ?? ''];
|
|
70
|
+
const clientSecret = input.env[oauth.clientSecretEnv ?? ''];
|
|
71
|
+
const refreshToken = input.env[oauth.refreshTokenEnv ?? ''];
|
|
72
|
+
if (!clientId || !clientSecret || !refreshToken) {
|
|
73
|
+
throw new ProviderError(
|
|
74
|
+
`${displayName(input.providerId)} needs a refresh token: set ${oauth.clientIdEnv}, ${oauth.clientSecretEnv} and ${oauth.refreshTokenEnv}.`,
|
|
75
|
+
{ code: 'no_refresh_credentials', provider: input.providerId, status: 401 },
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
const body = new URLSearchParams({ grant_type: 'refresh_token', refresh_token: refreshToken, client_id: clientId, client_secret: clientSecret });
|
|
80
|
+
if (oauth.scope) body.set('scope', oauth.scope);
|
|
81
|
+
|
|
82
|
+
const doFetch = input.fetchImpl ?? fetch;
|
|
83
|
+
const res = await doFetch(oauth.tokenUrl, { method: 'POST', headers: { 'content-type': 'application/x-www-form-urlencoded' }, body });
|
|
84
|
+
if (!res.ok) {
|
|
85
|
+
const detail = await safeText(res);
|
|
86
|
+
throw new ProviderError(`${displayName(input.providerId)} refused to refresh its token (${res.status}).`, {
|
|
87
|
+
code: 'refresh_failed',
|
|
88
|
+
status: 401,
|
|
89
|
+
provider: input.providerId,
|
|
90
|
+
detail: detail.slice(0, 200),
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const json = await res.json();
|
|
95
|
+
const token = json.access_token;
|
|
96
|
+
if (!token) throw new ProviderError(`${displayName(input.providerId)} returned no access_token.`, { code: 'refresh_failed', status: 401 });
|
|
97
|
+
|
|
98
|
+
const now = input.now ?? Date.now;
|
|
99
|
+
// refresh a minute early so a request cannot expire mid-flight
|
|
100
|
+
const expiresAt = now() + Math.max(0, (Number(json.expires_in ?? 3600) - 60) * 1000);
|
|
101
|
+
tokenCache.set(input.providerId, { token, expiresAt });
|
|
102
|
+
return { token, expiresAt };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A cached, still-valid token for this provider, or null. */
|
|
106
|
+
export function cachedToken(providerId, now = Date.now) {
|
|
107
|
+
const entry = tokenCache.get(providerId);
|
|
108
|
+
if (!entry) return null;
|
|
109
|
+
if (entry.expiresAt <= now()) {
|
|
110
|
+
tokenCache.delete(providerId);
|
|
111
|
+
return null;
|
|
112
|
+
}
|
|
113
|
+
return entry.token;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function createAdapter(providerId, provider, env = process.env) {
|
|
117
|
+
const initialKey = cachedToken(providerId) ?? provider.apiKey ?? (provider.apiKeyEnv ? env[provider.apiKeyEnv] : undefined);
|
|
118
|
+
const base = { id: providerId, type: provider.type, name: displayName(providerId), hasKey: Boolean(initialKey) };
|
|
119
|
+
|
|
120
|
+
// An MCP server is a provider like any other (B5.4). Imported lazily because
|
|
121
|
+
// @drsamonline/mcp depends on the router, which depends on this package.
|
|
122
|
+
if (provider.type === 'mcp') {
|
|
123
|
+
return {
|
|
124
|
+
...base,
|
|
125
|
+
type: 'mcp',
|
|
126
|
+
chat: async (req, opts = {}) => {
|
|
127
|
+
const { createMcpAdapter } = await import('@drsamonline/mcp');
|
|
128
|
+
return createMcpAdapter(providerId, provider, env).chat(req, opts);
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Build the concrete call for a given key. */
|
|
134
|
+
const build = (key) => {
|
|
135
|
+
if (provider.type === 'openai') return (req, opts) => openaiChat(providerId, provider, key, req, opts);
|
|
136
|
+
if (provider.type === 'anthropic') return (req, opts) => anthropicChat(providerId, provider, key, req, opts);
|
|
137
|
+
if (provider.type === 'gemini') return (req, opts) => geminiChat(providerId, provider, key, req, opts);
|
|
138
|
+
if (provider.type === 'mock') return (req, opts) => mockChat(providerId, provider, req, opts);
|
|
139
|
+
throw new ProviderError(`Provider "${providerId}" has an unknown type "${provider.type}".`, {
|
|
140
|
+
code: 'bad_provider_type',
|
|
141
|
+
provider: providerId,
|
|
142
|
+
});
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* B9.5: one refresh, one retry, and only for a provider that has an OAuth
|
|
147
|
+
* config. A refresh loop would turn one expired token into an outage.
|
|
148
|
+
*/
|
|
149
|
+
const chat = async (req, opts = {}) => {
|
|
150
|
+
const key = cachedToken(providerId) ?? initialKey;
|
|
151
|
+
try {
|
|
152
|
+
return await build(key)(req, opts);
|
|
153
|
+
} catch (err) {
|
|
154
|
+
const isAuth = /** @type {any} */ (err)?.status === 401;
|
|
155
|
+
if (!isAuth || !provider.oauth) throw err;
|
|
156
|
+
await refreshAccessToken({ providerId, provider, env });
|
|
157
|
+
return await build(cachedToken(providerId))(req, opts);
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
return { ...base, chat };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/* ------------------------------------------------------------------ openai */
|
|
165
|
+
|
|
166
|
+
async function openaiChat(providerId, provider, apiKey, req, opts = {}) {
|
|
167
|
+
const url = `${trimSlash(provider.baseUrl)}/chat/completions`;
|
|
168
|
+
/** @type {Record<string, string>} */
|
|
169
|
+
const headers = { 'content-type': 'application/json' };
|
|
170
|
+
if (apiKey) headers.authorization = `Bearer ${apiKey}`;
|
|
171
|
+
if (provider.headers) Object.assign(headers, provider.headers);
|
|
172
|
+
|
|
173
|
+
const body = {
|
|
174
|
+
model: req.model,
|
|
175
|
+
messages: req.messages,
|
|
176
|
+
stream: Boolean(req.stream),
|
|
177
|
+
};
|
|
178
|
+
// B4.6: OpenAI caches prefixes automatically above a threshold; the key only
|
|
179
|
+
// helps route the request to the same cache.
|
|
180
|
+
if (req.cacheHints?.openai?.promptCacheKey) body.prompt_cache_key = req.cacheHints.openai.promptCacheKey;
|
|
181
|
+
if (req.temperature != null) body.temperature = req.temperature;
|
|
182
|
+
if (req.max_tokens != null) body.max_tokens = req.max_tokens;
|
|
183
|
+
if (req.top_p != null) body.top_p = req.top_p;
|
|
184
|
+
if (req.stop != null) body.stop = req.stop;
|
|
185
|
+
if (req.stream) body.stream_options = { include_usage: true };
|
|
186
|
+
|
|
187
|
+
const res = await fetchJson(url, { method: 'POST', headers, body }, provider, providerId, req.model);
|
|
188
|
+
|
|
189
|
+
if (req.stream) {
|
|
190
|
+
return readOpenAIStream(res, opts, req);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const json = await res.json();
|
|
194
|
+
const choice = json.choices?.[0];
|
|
195
|
+
const text = choice?.message?.content ?? '';
|
|
196
|
+
return {
|
|
197
|
+
text: typeof text === 'string' ? text : flattenParts(text),
|
|
198
|
+
usage: {
|
|
199
|
+
prompt_tokens: json.usage?.prompt_tokens ?? estimateMessages(req.messages),
|
|
200
|
+
completion_tokens: json.usage?.completion_tokens ?? estimateText(text),
|
|
201
|
+
},
|
|
202
|
+
finishReason: choice?.finish_reason ?? null,
|
|
203
|
+
raw: json,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
async function readOpenAIStream(res, opts, req) {
|
|
208
|
+
let text = '';
|
|
209
|
+
let usage = null;
|
|
210
|
+
let finishReason = null;
|
|
211
|
+
await readSSE(res, (payload) => {
|
|
212
|
+
const choice = payload.choices?.[0];
|
|
213
|
+
const delta = choice?.delta?.content;
|
|
214
|
+
if (typeof delta === 'string' && delta.length) {
|
|
215
|
+
text += delta;
|
|
216
|
+
opts.onToken?.(delta);
|
|
217
|
+
}
|
|
218
|
+
if (choice?.finish_reason) finishReason = choice.finish_reason;
|
|
219
|
+
if (payload.usage) usage = payload.usage;
|
|
220
|
+
});
|
|
221
|
+
return {
|
|
222
|
+
text,
|
|
223
|
+
usage: {
|
|
224
|
+
prompt_tokens: usage?.prompt_tokens ?? estimateMessages(req.messages),
|
|
225
|
+
completion_tokens: usage?.completion_tokens ?? estimateText(text),
|
|
226
|
+
},
|
|
227
|
+
finishReason,
|
|
228
|
+
raw: null,
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
/* --------------------------------------------------------------- anthropic */
|
|
233
|
+
|
|
234
|
+
async function anthropicChat(providerId, provider, apiKey, req, opts = {}) {
|
|
235
|
+
const url = `${trimSlash(provider.baseUrl)}/messages`;
|
|
236
|
+
const system = req.messages
|
|
237
|
+
.filter((m) => m.role === 'system')
|
|
238
|
+
.map((m) => (typeof m.content === 'string' ? m.content : flattenParts(m.content)))
|
|
239
|
+
.join('\n\n');
|
|
240
|
+
const messages = req.messages
|
|
241
|
+
.filter((m) => m.role !== 'system')
|
|
242
|
+
.map((m) => ({
|
|
243
|
+
role: m.role === 'assistant' ? 'assistant' : 'user',
|
|
244
|
+
content: typeof m.content === 'string' ? m.content : flattenParts(m.content),
|
|
245
|
+
}));
|
|
246
|
+
|
|
247
|
+
const body = {
|
|
248
|
+
model: req.model,
|
|
249
|
+
max_tokens: req.max_tokens ?? 1024,
|
|
250
|
+
messages,
|
|
251
|
+
stream: Boolean(req.stream),
|
|
252
|
+
};
|
|
253
|
+
if (system) {
|
|
254
|
+
// B4.6: Anthropic wants an explicit breakpoint, on a content block.
|
|
255
|
+
body.system = req.cacheHints?.anthropic?.breakpoint
|
|
256
|
+
? [{ type: 'text', text: system, cache_control: { type: 'ephemeral' } }]
|
|
257
|
+
: system;
|
|
258
|
+
}
|
|
259
|
+
if (req.temperature != null) body.temperature = req.temperature;
|
|
260
|
+
if (req.stop != null) body.stop_sequences = Array.isArray(req.stop) ? req.stop : [req.stop];
|
|
261
|
+
|
|
262
|
+
const headers = {
|
|
263
|
+
'content-type': 'application/json',
|
|
264
|
+
'anthropic-version': '2023-06-01',
|
|
265
|
+
};
|
|
266
|
+
if (apiKey) headers['x-api-key'] = apiKey;
|
|
267
|
+
|
|
268
|
+
const res = await fetchJson(url, { method: 'POST', headers, body }, provider, providerId, req.model);
|
|
269
|
+
|
|
270
|
+
if (req.stream) {
|
|
271
|
+
let text = '';
|
|
272
|
+
let usage = { prompt_tokens: 0, completion_tokens: 0 };
|
|
273
|
+
let finishReason = null;
|
|
274
|
+
await readSSE(res, (payload) => {
|
|
275
|
+
if (payload.type === 'content_block_delta' && payload.delta?.text) {
|
|
276
|
+
text += payload.delta.text;
|
|
277
|
+
opts.onToken?.(payload.delta.text);
|
|
278
|
+
}
|
|
279
|
+
if (payload.type === 'message_start' && payload.message?.usage) {
|
|
280
|
+
usage.prompt_tokens = payload.message.usage.input_tokens ?? 0;
|
|
281
|
+
}
|
|
282
|
+
if (payload.type === 'message_delta') {
|
|
283
|
+
if (payload.usage?.output_tokens != null) usage.completion_tokens = payload.usage.output_tokens;
|
|
284
|
+
if (payload.delta?.stop_reason) finishReason = payload.delta.stop_reason;
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
if (!usage.completion_tokens) usage.completion_tokens = estimateText(text);
|
|
288
|
+
if (!usage.prompt_tokens) usage.prompt_tokens = estimateMessages(req.messages);
|
|
289
|
+
return { text, usage, finishReason, raw: null };
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
const json = await res.json();
|
|
293
|
+
const text = Array.isArray(json.content)
|
|
294
|
+
? json.content.filter((c) => c.type === 'text').map((c) => c.text).join('')
|
|
295
|
+
: '';
|
|
296
|
+
return {
|
|
297
|
+
text,
|
|
298
|
+
usage: {
|
|
299
|
+
prompt_tokens: json.usage?.input_tokens ?? estimateMessages(req.messages),
|
|
300
|
+
completion_tokens: json.usage?.output_tokens ?? estimateText(text),
|
|
301
|
+
},
|
|
302
|
+
finishReason: json.stop_reason ?? null,
|
|
303
|
+
raw: json,
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/* ------------------------------------------------------------------ gemini */
|
|
308
|
+
|
|
309
|
+
async function geminiChat(providerId, provider, apiKey, req, opts = {}) {
|
|
310
|
+
const root = trimSlash(provider.baseUrl ?? 'https://generativelanguage.googleapis.com/v1beta');
|
|
311
|
+
const method = req.stream ? 'streamGenerateContent' : 'generateContent';
|
|
312
|
+
const url = `${root}/models/${encodeURIComponent(req.model)}:${method}${req.stream ? '?alt=sse' : ''}`;
|
|
313
|
+
|
|
314
|
+
const system = req.messages
|
|
315
|
+
.filter((m) => m.role === 'system')
|
|
316
|
+
.map((m) => (typeof m.content === 'string' ? m.content : flattenParts(m.content)))
|
|
317
|
+
.join('\n\n');
|
|
318
|
+
|
|
319
|
+
const contents = req.messages
|
|
320
|
+
.filter((m) => m.role !== 'system')
|
|
321
|
+
.map((m) => ({
|
|
322
|
+
role: m.role === 'assistant' ? 'model' : 'user',
|
|
323
|
+
parts: [{ text: typeof m.content === 'string' ? m.content : flattenParts(m.content) }],
|
|
324
|
+
}));
|
|
325
|
+
|
|
326
|
+
const body = {
|
|
327
|
+
contents,
|
|
328
|
+
generationConfig: {
|
|
329
|
+
...(req.temperature != null ? { temperature: req.temperature } : {}),
|
|
330
|
+
...(req.max_tokens != null ? { maxOutputTokens: req.max_tokens } : {}),
|
|
331
|
+
...(req.top_p != null ? { topP: req.top_p } : {}),
|
|
332
|
+
...(req.stop ? { stopSequences: Array.isArray(req.stop) ? req.stop : [req.stop] } : {}),
|
|
333
|
+
},
|
|
334
|
+
};
|
|
335
|
+
if (system) body.systemInstruction = { parts: [{ text: system }] };
|
|
336
|
+
|
|
337
|
+
/** @type {Record<string, string>} */
|
|
338
|
+
const headers = { 'content-type': 'application/json' };
|
|
339
|
+
if (apiKey) headers['x-goog-api-key'] = apiKey;
|
|
340
|
+
|
|
341
|
+
const res = await fetchJson(url, { method: 'POST', headers, body }, provider, providerId, req.model);
|
|
342
|
+
|
|
343
|
+
if (req.stream) {
|
|
344
|
+
let text = '';
|
|
345
|
+
let usage = { prompt_tokens: 0, completion_tokens: 0 };
|
|
346
|
+
let finishReason = null;
|
|
347
|
+
await readSSE(res, (payload) => {
|
|
348
|
+
const parts = payload.candidates?.[0]?.content?.parts ?? [];
|
|
349
|
+
for (const p of parts) {
|
|
350
|
+
if (p.text) {
|
|
351
|
+
text += p.text;
|
|
352
|
+
opts.onToken?.(p.text);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (payload.candidates?.[0]?.finishReason) finishReason = payload.candidates[0].finishReason;
|
|
356
|
+
if (payload.usageMetadata) {
|
|
357
|
+
usage.prompt_tokens = payload.usageMetadata.promptTokenCount ?? usage.prompt_tokens;
|
|
358
|
+
usage.completion_tokens = payload.usageMetadata.candidatesTokenCount ?? usage.completion_tokens;
|
|
359
|
+
}
|
|
360
|
+
});
|
|
361
|
+
if (!usage.completion_tokens) usage.completion_tokens = estimateText(text);
|
|
362
|
+
if (!usage.prompt_tokens) usage.prompt_tokens = estimateMessages(req.messages);
|
|
363
|
+
return { text, usage, finishReason, raw: null };
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const json = await res.json();
|
|
367
|
+
const parts = json.candidates?.[0]?.content?.parts ?? [];
|
|
368
|
+
const text = parts.map((p) => p.text ?? '').join('');
|
|
369
|
+
return {
|
|
370
|
+
text,
|
|
371
|
+
usage: {
|
|
372
|
+
prompt_tokens: json.usageMetadata?.promptTokenCount ?? estimateMessages(req.messages),
|
|
373
|
+
completion_tokens: json.usageMetadata?.candidatesTokenCount ?? estimateText(text),
|
|
374
|
+
},
|
|
375
|
+
finishReason: json.candidates?.[0]?.finishReason ?? null,
|
|
376
|
+
raw: json,
|
|
377
|
+
};
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
/* -------------------------------------------------------------------- mock */
|
|
381
|
+
|
|
382
|
+
async function mockChat(providerId, provider, req, opts = {}) {
|
|
383
|
+
const modelName = String(req.model ?? 'mock');
|
|
384
|
+
const behaviour = provider.mock ?? {};
|
|
385
|
+
|
|
386
|
+
if (behaviour.failWith) {
|
|
387
|
+
throw new ProviderError(behaviour.failWith, {
|
|
388
|
+
code: behaviour.failCode ?? 'mock_failure',
|
|
389
|
+
status: behaviour.failStatus ?? 503,
|
|
390
|
+
retryable: behaviour.failRetryable ?? true,
|
|
391
|
+
provider: providerId,
|
|
392
|
+
model: modelName,
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
if (/fail/.test(modelName)) {
|
|
396
|
+
throw new ProviderError(`${displayName(providerId)} refused the request (simulated outage).`, {
|
|
397
|
+
code: 'mock_outage',
|
|
398
|
+
status: 503,
|
|
399
|
+
retryable: true,
|
|
400
|
+
provider: providerId,
|
|
401
|
+
model: modelName,
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
const lastUser = [...req.messages].reverse().find((m) => m.role === 'user');
|
|
406
|
+
const prompt = typeof lastUser?.content === 'string' ? lastUser.content : flattenParts(lastUser?.content);
|
|
407
|
+
const delay = Number(behaviour.delayMs ?? 5);
|
|
408
|
+
if (delay) await new Promise((r) => setTimeout(r, delay));
|
|
409
|
+
|
|
410
|
+
const text = `Mock reply from ${modelName}. You said: ${prompt.slice(0, 120)}`;
|
|
411
|
+
if (req.stream) {
|
|
412
|
+
for (const chunk of text.match(/\S+\s*/g) ?? []) {
|
|
413
|
+
opts.onToken?.(chunk);
|
|
414
|
+
await new Promise((r) => setTimeout(r, 1));
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
return {
|
|
418
|
+
text,
|
|
419
|
+
usage: { prompt_tokens: estimateMessages(req.messages), completion_tokens: estimateText(text) },
|
|
420
|
+
finishReason: 'stop',
|
|
421
|
+
raw: { mock: true, provider: providerId },
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/* ----------------------------------------------------------------- helpers */
|
|
426
|
+
|
|
427
|
+
async function fetchJson(url, init, provider, providerId, modelName) {
|
|
428
|
+
const timeoutMs = Number(provider.timeoutMs ?? DEFAULT_TIMEOUT_MS);
|
|
429
|
+
let res;
|
|
430
|
+
try {
|
|
431
|
+
res = await fetch(url, {
|
|
432
|
+
...init,
|
|
433
|
+
body: JSON.stringify(init.body),
|
|
434
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
435
|
+
});
|
|
436
|
+
} catch (err) {
|
|
437
|
+
const e = /** @type {any} */ (err);
|
|
438
|
+
const timedOut = e?.name === 'TimeoutError' || e?.name === 'AbortError';
|
|
439
|
+
throw new ProviderError(
|
|
440
|
+
timedOut
|
|
441
|
+
? `${displayName(providerId)} took longer than ${Math.round(timeoutMs / 1000)}s to answer.`
|
|
442
|
+
: `Could not reach ${displayName(providerId)}${provider.baseUrl ? ` at ${provider.baseUrl}` : ''}.`,
|
|
443
|
+
{
|
|
444
|
+
code: timedOut ? 'provider_timeout' : 'provider_unreachable',
|
|
445
|
+
retryable: true,
|
|
446
|
+
provider: providerId,
|
|
447
|
+
model: modelName,
|
|
448
|
+
detail: e?.message,
|
|
449
|
+
cause: err,
|
|
450
|
+
},
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
if (!res.ok) {
|
|
455
|
+
const detail = await safeText(res);
|
|
456
|
+
// B9.4: a 429 that says when to come back is a fact worth keeping. Without
|
|
457
|
+
// it we would keep knocking on a door that is closed for an hour.
|
|
458
|
+
const retryAfterMs = parseRetryAfter(res.headers, detail);
|
|
459
|
+
throw new ProviderError(explainHttpError(providerId, res.status, detail), {
|
|
460
|
+
code: `http_${res.status}`,
|
|
461
|
+
status: res.status,
|
|
462
|
+
retryable: isRetryableStatus(res.status),
|
|
463
|
+
provider: providerId,
|
|
464
|
+
model: modelName,
|
|
465
|
+
detail: detail.slice(0, 500),
|
|
466
|
+
...(retryAfterMs ? { retryAfterMs } : {}),
|
|
467
|
+
});
|
|
468
|
+
}
|
|
469
|
+
return res;
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/** `Retry-After` in seconds, or a reset timestamp in the error body. */
|
|
473
|
+
export function parseRetryAfter(headers, detail = '') {
|
|
474
|
+
const header = headers?.get?.('retry-after');
|
|
475
|
+
if (header) {
|
|
476
|
+
const seconds = Number(header);
|
|
477
|
+
if (Number.isFinite(seconds) && seconds > 0) return Math.round(seconds * 1000);
|
|
478
|
+
const date = Date.parse(header);
|
|
479
|
+
if (Number.isFinite(date)) return Math.max(0, date - Date.now());
|
|
480
|
+
}
|
|
481
|
+
// some providers put the wait in the body instead of the header
|
|
482
|
+
const body = /retry[-_ ]after[^0-9]{0,10}(\d+(?:\.\d+)?)\s*(ms|s|seconds?)?/i.exec(String(detail));
|
|
483
|
+
if (body) {
|
|
484
|
+
const value = Number(body[1]);
|
|
485
|
+
const unit = (body[2] ?? 's').toLowerCase();
|
|
486
|
+
return Math.round(unit.startsWith('ms') ? value : value * 1000);
|
|
487
|
+
}
|
|
488
|
+
return 0;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
function explainHttpError(providerId, status, detail) {
|
|
492
|
+
const name = displayName(providerId);
|
|
493
|
+
if (status === 401 || status === 403) return `${name} rejected the API key.`;
|
|
494
|
+
if (status === 404) return `${name} says that model does not exist.`;
|
|
495
|
+
if (status === 429) return `${name} is rate-limiting requests right now.`;
|
|
496
|
+
if (status === 402) return `${name} says the account is out of credit.`;
|
|
497
|
+
if (status >= 500) return `${name} is having trouble on their side (HTTP ${status}).`;
|
|
498
|
+
if (status === 400) return `${name} rejected the request (HTTP 400): ${firstLine(detail)}`;
|
|
499
|
+
return `${name} returned HTTP ${status}: ${firstLine(detail)}`;
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function isRetryableStatus(status) {
|
|
503
|
+
// 400/401/403/404/422 are our fault or our credentials: retrying elsewhere is
|
|
504
|
+
// still worthwhile, but the same target will not fix itself.
|
|
505
|
+
return status === 408 || status === 409 || status === 425 || status === 429 || status >= 500;
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
async function readSSE(res, onData) {
|
|
509
|
+
const decoder = new TextDecoder();
|
|
510
|
+
let buffer = '';
|
|
511
|
+
for await (const chunk of res.body) {
|
|
512
|
+
buffer += decoder.decode(chunk, { stream: true });
|
|
513
|
+
let idx;
|
|
514
|
+
while ((idx = buffer.indexOf('\n\n')) !== -1) {
|
|
515
|
+
const block = buffer.slice(0, idx);
|
|
516
|
+
buffer = buffer.slice(idx + 2);
|
|
517
|
+
for (const line of block.split('\n')) {
|
|
518
|
+
const trimmed = line.trim();
|
|
519
|
+
if (!trimmed.startsWith('data:')) continue;
|
|
520
|
+
const data = trimmed.slice(5).trim();
|
|
521
|
+
if (!data || data === '[DONE]') continue;
|
|
522
|
+
try {
|
|
523
|
+
onData(JSON.parse(data));
|
|
524
|
+
} catch {
|
|
525
|
+
/* providers occasionally send keepalive frames; ignore them */
|
|
526
|
+
}
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
async function safeText(res) {
|
|
533
|
+
try {
|
|
534
|
+
return await res.text();
|
|
535
|
+
} catch {
|
|
536
|
+
return '';
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
function firstLine(s) {
|
|
541
|
+
return String(s ?? '').split('\n')[0].slice(0, 200);
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function trimSlash(u) {
|
|
545
|
+
return String(u ?? '').replace(/\/+$/, '');
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
function flattenParts(content) {
|
|
549
|
+
if (!content) return '';
|
|
550
|
+
if (typeof content === 'string') return content;
|
|
551
|
+
if (Array.isArray(content)) return content.map((p) => p?.text ?? '').join('');
|
|
552
|
+
return String(content);
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
function estimateText(text) {
|
|
556
|
+
return Math.max(1, Math.ceil(String(text ?? '').length / 4));
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
function estimateMessages(messages) {
|
|
560
|
+
return Math.max(1, Math.ceil(JSON.stringify(messages ?? '').length / 4));
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
export const __test = { explainHttpError, isRetryableStatus, estimateMessages, estimateText };
|
package/README.md
DELETED