@12-apps/llm 0.0.0-stage → 1.0.0
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 +289 -2
- package/dist/index.d.ts +586 -0
- package/dist/index.js +1115 -0
- package/dist/index.js.map +1 -0
- package/package.json +71 -4
package/dist/index.js
ADDED
|
@@ -0,0 +1,1115 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
|
|
4
|
+
// src/errors.ts
|
|
5
|
+
var LlmError = class extends Error {
|
|
6
|
+
static {
|
|
7
|
+
__name(this, "LlmError");
|
|
8
|
+
}
|
|
9
|
+
kind;
|
|
10
|
+
provider;
|
|
11
|
+
/** The provider's HTTP status, when there was one. */
|
|
12
|
+
status;
|
|
13
|
+
constructor(kind, provider, message, status) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "LlmError";
|
|
16
|
+
this.kind = kind;
|
|
17
|
+
this.provider = provider;
|
|
18
|
+
this.status = status;
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
var AuthError = class extends LlmError {
|
|
22
|
+
static {
|
|
23
|
+
__name(this, "AuthError");
|
|
24
|
+
}
|
|
25
|
+
constructor(provider, message, status) {
|
|
26
|
+
super("auth", provider, message, status);
|
|
27
|
+
this.name = "AuthError";
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
var CredentialRevokedError = class extends AuthError {
|
|
31
|
+
static {
|
|
32
|
+
__name(this, "CredentialRevokedError");
|
|
33
|
+
}
|
|
34
|
+
constructor(provider = "credential") {
|
|
35
|
+
super(provider, "The credential was revoked by the host.");
|
|
36
|
+
this.name = "CredentialRevokedError";
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
var RateLimitError = class extends LlmError {
|
|
40
|
+
static {
|
|
41
|
+
__name(this, "RateLimitError");
|
|
42
|
+
}
|
|
43
|
+
retryAfterMs;
|
|
44
|
+
constructor(provider, message, retryAfterMs) {
|
|
45
|
+
super("rate_limit", provider, message, 429);
|
|
46
|
+
this.name = "RateLimitError";
|
|
47
|
+
this.retryAfterMs = retryAfterMs;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
var RefusalError = class extends LlmError {
|
|
51
|
+
static {
|
|
52
|
+
__name(this, "RefusalError");
|
|
53
|
+
}
|
|
54
|
+
refusal;
|
|
55
|
+
constructor(provider, refusal) {
|
|
56
|
+
super("refusal", provider, `The model declined the request (${refusal.category ?? "uncategorized"}).`);
|
|
57
|
+
this.name = "RefusalError";
|
|
58
|
+
this.refusal = refusal;
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
var InvalidRequestError = class extends LlmError {
|
|
62
|
+
static {
|
|
63
|
+
__name(this, "InvalidRequestError");
|
|
64
|
+
}
|
|
65
|
+
constructor(provider, message, status) {
|
|
66
|
+
super("invalid_request", provider, message, status);
|
|
67
|
+
this.name = "InvalidRequestError";
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var ProviderUnavailableError = class extends LlmError {
|
|
71
|
+
static {
|
|
72
|
+
__name(this, "ProviderUnavailableError");
|
|
73
|
+
}
|
|
74
|
+
constructor(provider, message, status) {
|
|
75
|
+
super("unavailable", provider, message, status);
|
|
76
|
+
this.name = "ProviderUnavailableError";
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
var InvalidResponseError = class extends LlmError {
|
|
80
|
+
static {
|
|
81
|
+
__name(this, "InvalidResponseError");
|
|
82
|
+
}
|
|
83
|
+
constructor(provider, message) {
|
|
84
|
+
super("invalid_response", provider, message);
|
|
85
|
+
this.name = "InvalidResponseError";
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
var UnsupportedError = class extends LlmError {
|
|
89
|
+
static {
|
|
90
|
+
__name(this, "UnsupportedError");
|
|
91
|
+
}
|
|
92
|
+
constructor(provider, message) {
|
|
93
|
+
super("unsupported", provider, message);
|
|
94
|
+
this.name = "UnsupportedError";
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
function headerValue(headers, name) {
|
|
98
|
+
if (!headers || typeof headers !== "object") return void 0;
|
|
99
|
+
const getter = headers.get;
|
|
100
|
+
if (typeof getter === "function") {
|
|
101
|
+
const value2 = getter.call(headers, name);
|
|
102
|
+
return typeof value2 === "string" ? value2 : void 0;
|
|
103
|
+
}
|
|
104
|
+
const value = headers[name];
|
|
105
|
+
return typeof value === "string" ? value : void 0;
|
|
106
|
+
}
|
|
107
|
+
__name(headerValue, "headerValue");
|
|
108
|
+
function retryAfterFrom(headers, now = Date.now) {
|
|
109
|
+
const ms = Number(headerValue(headers, "retry-after-ms"));
|
|
110
|
+
if (Number.isFinite(ms) && ms >= 0 && headerValue(headers, "retry-after-ms") !== void 0) return ms;
|
|
111
|
+
const raw = headerValue(headers, "retry-after");
|
|
112
|
+
if (raw === void 0) return void 0;
|
|
113
|
+
const seconds = Number(raw);
|
|
114
|
+
if (Number.isFinite(seconds)) return Math.max(0, seconds * 1e3);
|
|
115
|
+
const date = Date.parse(raw);
|
|
116
|
+
return Number.isNaN(date) ? void 0 : Math.max(0, date - now());
|
|
117
|
+
}
|
|
118
|
+
__name(retryAfterFrom, "retryAfterFrom");
|
|
119
|
+
function redact(text, secrets) {
|
|
120
|
+
return secrets.reduce(
|
|
121
|
+
(out, secret) => secret.length >= 4 ? out.split(secret).join("[redacted]") : out,
|
|
122
|
+
text
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
__name(redact, "redact");
|
|
126
|
+
var CONNECTION_ERROR = /connection|timeout|fetch failed|network|ECONN|ETIMEDOUT|ENOTFOUND|socket/i;
|
|
127
|
+
function shapeOf(error) {
|
|
128
|
+
return error && typeof error === "object" ? error : {};
|
|
129
|
+
}
|
|
130
|
+
__name(shapeOf, "shapeOf");
|
|
131
|
+
function messageOf(shape, error) {
|
|
132
|
+
return typeof shape.message === "string" ? shape.message : String(error);
|
|
133
|
+
}
|
|
134
|
+
__name(messageOf, "messageOf");
|
|
135
|
+
function errorForStatus(provider, status, message, headers) {
|
|
136
|
+
if (status === 401 || status === 403) return new AuthError(provider, message, status);
|
|
137
|
+
if (status === 429) return new RateLimitError(provider, message, retryAfterFrom(headers));
|
|
138
|
+
if (status >= 500 || status === 408) return new ProviderUnavailableError(provider, message, status);
|
|
139
|
+
if (status >= 400) return new InvalidRequestError(provider, message, status);
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
__name(errorForStatus, "errorForStatus");
|
|
143
|
+
function mapProviderError(provider, error, secrets = []) {
|
|
144
|
+
if (error instanceof LlmError) return error;
|
|
145
|
+
const shape = shapeOf(error);
|
|
146
|
+
if (shape.name === "AbortError" || shape.name === "APIUserAbortError") return error;
|
|
147
|
+
const text = redact(messageOf(shape, error), secrets);
|
|
148
|
+
const message = `${provider}: ${text}`;
|
|
149
|
+
if (typeof shape.status === "number") {
|
|
150
|
+
return errorForStatus(provider, shape.status, message, shape.headers) ?? error;
|
|
151
|
+
}
|
|
152
|
+
return CONNECTION_ERROR.test(`${String(shape.name)} ${text}`) ? new ProviderUnavailableError(provider, message) : error;
|
|
153
|
+
}
|
|
154
|
+
__name(mapProviderError, "mapProviderError");
|
|
155
|
+
|
|
156
|
+
// src/adapter.ts
|
|
157
|
+
function createAdapterRegistry(adapters = []) {
|
|
158
|
+
const byId = /* @__PURE__ */ new Map();
|
|
159
|
+
const registry = {
|
|
160
|
+
register(adapter, options = {}) {
|
|
161
|
+
if (byId.has(adapter.id) && !options.replace) {
|
|
162
|
+
throw new TypeError(
|
|
163
|
+
`@12-apps/llm: an adapter for "${adapter.id}" is already registered; pass { replace: true } to replace it.`
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
byId.set(adapter.id, adapter);
|
|
167
|
+
return registry;
|
|
168
|
+
},
|
|
169
|
+
get(id) {
|
|
170
|
+
const adapter = byId.get(id);
|
|
171
|
+
if (!adapter) {
|
|
172
|
+
throw new UnsupportedError(id, `No adapter is registered for provider "${id}" (registered: ${[...byId.keys()].join(", ") || "none"}).`);
|
|
173
|
+
}
|
|
174
|
+
return adapter;
|
|
175
|
+
},
|
|
176
|
+
has: /* @__PURE__ */ __name((id) => byId.has(id), "has"),
|
|
177
|
+
ids: /* @__PURE__ */ __name(() => [...byId.keys()], "ids")
|
|
178
|
+
};
|
|
179
|
+
for (const adapter of adapters) registry.register(adapter);
|
|
180
|
+
return registry;
|
|
181
|
+
}
|
|
182
|
+
__name(createAdapterRegistry, "createAdapterRegistry");
|
|
183
|
+
|
|
184
|
+
// src/adapters/load.ts
|
|
185
|
+
async function loadSdk(provider, packageName, load) {
|
|
186
|
+
try {
|
|
187
|
+
return await load();
|
|
188
|
+
} catch (error) {
|
|
189
|
+
const code = error.code;
|
|
190
|
+
if (code === "ERR_MODULE_NOT_FOUND" || code === "MODULE_NOT_FOUND") {
|
|
191
|
+
throw new UnsupportedError(provider, `The "${provider}" adapter needs the optional peer "${packageName}"; install it, or pass a client.`);
|
|
192
|
+
}
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
__name(loadSdk, "loadSdk");
|
|
197
|
+
|
|
198
|
+
// src/adapters/sealed-fetch.ts
|
|
199
|
+
var ACCOUNT_HEADERS = [
|
|
200
|
+
"authorization",
|
|
201
|
+
"api-key",
|
|
202
|
+
"x-api-key",
|
|
203
|
+
"x-goog-api-key",
|
|
204
|
+
"x-goog-user-project",
|
|
205
|
+
"openai-organization",
|
|
206
|
+
"openai-project"
|
|
207
|
+
];
|
|
208
|
+
var CUSTOM_HEADER_VARIABLES = ["OPENAI_CUSTOM_HEADERS", "ANTHROPIC_CUSTOM_HEADERS"];
|
|
209
|
+
function parseLine(line) {
|
|
210
|
+
const colon = line.indexOf(":");
|
|
211
|
+
if (colon < 0) return null;
|
|
212
|
+
const name = line.slice(0, colon).trim().toLowerCase();
|
|
213
|
+
const value = line.slice(colon + 1).trim();
|
|
214
|
+
try {
|
|
215
|
+
new Headers().set(name, value);
|
|
216
|
+
} catch {
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
return [name, value];
|
|
220
|
+
}
|
|
221
|
+
__name(parseLine, "parseLine");
|
|
222
|
+
function parseCustomHeaders() {
|
|
223
|
+
const lines = CUSTOM_HEADER_VARIABLES.flatMap(
|
|
224
|
+
(variable) => (typeof process === "undefined" ? "" : process.env[variable] ?? "").split(/\r?\n/)
|
|
225
|
+
);
|
|
226
|
+
return lines.map(parseLine).filter((pair) => pair !== null);
|
|
227
|
+
}
|
|
228
|
+
__name(parseCustomHeaders, "parseCustomHeaders");
|
|
229
|
+
function dropPlatformHeaders(headers) {
|
|
230
|
+
for (const [name, value] of parseCustomHeaders()) {
|
|
231
|
+
if (headers.get(name) === value) headers.delete(name);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
__name(dropPlatformHeaders, "dropPlatformHeaders");
|
|
235
|
+
function within(url, base) {
|
|
236
|
+
const prefix = base.pathname.endsWith("/") ? base.pathname : `${base.pathname}/`;
|
|
237
|
+
return url.origin === base.origin && (url.pathname === base.pathname || url.pathname.startsWith(prefix));
|
|
238
|
+
}
|
|
239
|
+
__name(within, "within");
|
|
240
|
+
function relativePath(url, base) {
|
|
241
|
+
const root = base.pathname.replace(/\/$/, "");
|
|
242
|
+
return url.pathname.slice(root.length) || "/";
|
|
243
|
+
}
|
|
244
|
+
__name(relativePath, "relativePath");
|
|
245
|
+
function allowed(routes, method, path) {
|
|
246
|
+
return routes.some((route) => route.method === method && (typeof route.path === "string" ? route.path === path : route.path.test(path)));
|
|
247
|
+
}
|
|
248
|
+
__name(allowed, "allowed");
|
|
249
|
+
function sealedFetch(options) {
|
|
250
|
+
const base = new URL(options.baseURL);
|
|
251
|
+
const send = options.fetch ?? globalThis.fetch;
|
|
252
|
+
const sealed = /* @__PURE__ */ __name(async (input, init) => {
|
|
253
|
+
const outgoing = new Request(input, init);
|
|
254
|
+
if (!within(new URL(outgoing.url), base)) {
|
|
255
|
+
throw new InvalidRequestError(options.provider, `Refused a request outside the adapter's base URL (${base.origin}${base.pathname}).`);
|
|
256
|
+
}
|
|
257
|
+
if (!allowed(options.routes, outgoing.method, relativePath(new URL(outgoing.url), base))) {
|
|
258
|
+
throw new InvalidRequestError(options.provider, `Refused a ${outgoing.method} request to a path the adapter does not use.`);
|
|
259
|
+
}
|
|
260
|
+
const headers = new Headers(outgoing.headers);
|
|
261
|
+
for (const name of ACCOUNT_HEADERS) headers.delete(name);
|
|
262
|
+
dropPlatformHeaders(headers);
|
|
263
|
+
for (const [name, value] of Object.entries(options.auth)) headers.set(name, value);
|
|
264
|
+
const body = outgoing.method === "GET" || outgoing.method === "HEAD" ? void 0 : await outgoing.arrayBuffer();
|
|
265
|
+
const response = await send(outgoing.url, { method: outgoing.method, headers, body, signal: outgoing.signal, redirect: "manual" });
|
|
266
|
+
if (response.type === "opaqueredirect" || response.status >= 300 && response.status < 400) {
|
|
267
|
+
throw new InvalidRequestError(options.provider, "The provider answered with a redirect, which is never followed.", response.status);
|
|
268
|
+
}
|
|
269
|
+
return response;
|
|
270
|
+
}, "sealed");
|
|
271
|
+
return sealed;
|
|
272
|
+
}
|
|
273
|
+
__name(sealedFetch, "sealedFetch");
|
|
274
|
+
|
|
275
|
+
// src/adapters/util.ts
|
|
276
|
+
function count(value) {
|
|
277
|
+
return typeof value === "number" && Number.isFinite(value) ? value : 0;
|
|
278
|
+
}
|
|
279
|
+
__name(count, "count");
|
|
280
|
+
function refusalOf(stopReason, category, explanation) {
|
|
281
|
+
return stopReason === "refusal" ? { category, explanation } : null;
|
|
282
|
+
}
|
|
283
|
+
__name(refusalOf, "refusalOf");
|
|
284
|
+
|
|
285
|
+
// src/adapters/anthropic.ts
|
|
286
|
+
var ANTHROPIC_PROVIDER = "anthropic";
|
|
287
|
+
var ANTHROPIC_DEFAULT_MODEL = "claude-haiku-5-5";
|
|
288
|
+
var OAUTH_BETA = "oauth-2025-04-20";
|
|
289
|
+
var MAX_BREAKPOINTS = 4;
|
|
290
|
+
var ANTHROPIC_BASE_URL = "https://api.anthropic.com";
|
|
291
|
+
var ANTHROPIC_ROUTES = [{ method: "POST", path: "/v1/messages" }];
|
|
292
|
+
async function defaultClient(credential, options) {
|
|
293
|
+
const sdk = await loadSdk(ANTHROPIC_PROVIDER, "@anthropic-ai/sdk", () => import("@anthropic-ai/sdk"));
|
|
294
|
+
const Client = sdk.default;
|
|
295
|
+
const baseURL = options.baseURL ?? ANTHROPIC_BASE_URL;
|
|
296
|
+
const apiKey = credential.kind === "apiKey";
|
|
297
|
+
const auth = apiKey ? { "x-api-key": credential.secret } : { authorization: `Bearer ${credential.secret}` };
|
|
298
|
+
const common = { baseURL, fetch: sealedFetch({ provider: ANTHROPIC_PROVIDER, baseURL, routes: ANTHROPIC_ROUTES, auth, fetch: options.fetch }), maxRetries: 0 };
|
|
299
|
+
return apiKey ? new Client({ ...common, apiKey: credential.secret, authToken: null }) : new Client({ ...common, apiKey: null, authToken: credential.secret, defaultHeaders: { "anthropic-beta": OAUTH_BETA } });
|
|
300
|
+
}
|
|
301
|
+
__name(defaultClient, "defaultClient");
|
|
302
|
+
function cacheControl(cache) {
|
|
303
|
+
if (!cache) return void 0;
|
|
304
|
+
return cache === true ? { type: "ephemeral" } : { type: "ephemeral", ttl: cache.ttl };
|
|
305
|
+
}
|
|
306
|
+
__name(cacheControl, "cacheControl");
|
|
307
|
+
function toSystem(blocks) {
|
|
308
|
+
return blocks.map((block) => {
|
|
309
|
+
const control = cacheControl(block.cache);
|
|
310
|
+
return control ? { type: "text", text: block.text, cache_control: control } : { type: "text", text: block.text };
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
__name(toSystem, "toSystem");
|
|
314
|
+
function assistantContent(message) {
|
|
315
|
+
if (message.raw?.provider === ANTHROPIC_PROVIDER && Array.isArray(message.raw.content)) {
|
|
316
|
+
return message.raw.content;
|
|
317
|
+
}
|
|
318
|
+
const blocks = message.text ? [{ type: "text", text: message.text }] : [];
|
|
319
|
+
for (const call of message.toolCalls) {
|
|
320
|
+
blocks.push({ type: "tool_use", id: call.id, name: call.name, input: call.input });
|
|
321
|
+
}
|
|
322
|
+
return blocks;
|
|
323
|
+
}
|
|
324
|
+
__name(assistantContent, "assistantContent");
|
|
325
|
+
function toMessages(messages) {
|
|
326
|
+
return messages.map((message) => {
|
|
327
|
+
if (message.role === "user") return { role: "user", content: message.text };
|
|
328
|
+
if (message.role === "assistant") return { role: "assistant", content: assistantContent(message) };
|
|
329
|
+
return {
|
|
330
|
+
role: "user",
|
|
331
|
+
content: message.results.map((result) => ({
|
|
332
|
+
type: "tool_result",
|
|
333
|
+
tool_use_id: result.callId,
|
|
334
|
+
content: result.output,
|
|
335
|
+
...result.isError ? { is_error: true } : {}
|
|
336
|
+
}))
|
|
337
|
+
};
|
|
338
|
+
});
|
|
339
|
+
}
|
|
340
|
+
__name(toMessages, "toMessages");
|
|
341
|
+
function toToolChoice(choice) {
|
|
342
|
+
if (choice === "none") return { type: "none" };
|
|
343
|
+
if (choice === "required") return { type: "any" };
|
|
344
|
+
return choice === "auto" ? { type: "auto" } : void 0;
|
|
345
|
+
}
|
|
346
|
+
__name(toToolChoice, "toToolChoice");
|
|
347
|
+
function toAnthropicParams(call) {
|
|
348
|
+
const { request, model } = call;
|
|
349
|
+
const system = request.system ?? [];
|
|
350
|
+
const breakpoints = system.filter((block) => block.cache).length + (request.cacheConversation ? 1 : 0);
|
|
351
|
+
if (breakpoints > MAX_BREAKPOINTS) {
|
|
352
|
+
throw new InvalidRequestError(ANTHROPIC_PROVIDER, `At most ${MAX_BREAKPOINTS} cache breakpoints per request; this one has ${breakpoints}.`);
|
|
353
|
+
}
|
|
354
|
+
const params = {
|
|
355
|
+
model,
|
|
356
|
+
max_tokens: request.maxOutputTokens,
|
|
357
|
+
messages: toMessages(request.messages)
|
|
358
|
+
};
|
|
359
|
+
if (system.length > 0) params.system = toSystem(system);
|
|
360
|
+
if (request.tools && request.tools.length > 0) {
|
|
361
|
+
params.tools = request.tools.map((tool) => ({
|
|
362
|
+
name: tool.name,
|
|
363
|
+
description: tool.description,
|
|
364
|
+
input_schema: tool.inputSchema,
|
|
365
|
+
strict: true
|
|
366
|
+
}));
|
|
367
|
+
}
|
|
368
|
+
const toolChoice = toToolChoice(request.toolChoice);
|
|
369
|
+
if (toolChoice) params.tool_choice = toolChoice;
|
|
370
|
+
if (request.effort) params.output_config = { effort: request.effort };
|
|
371
|
+
if (request.cacheConversation) params.cache_control = { type: "ephemeral" };
|
|
372
|
+
return params;
|
|
373
|
+
}
|
|
374
|
+
__name(toAnthropicParams, "toAnthropicParams");
|
|
375
|
+
var STOP_REASONS = {
|
|
376
|
+
end_turn: "end",
|
|
377
|
+
stop_sequence: "end",
|
|
378
|
+
tool_use: "tool_use",
|
|
379
|
+
max_tokens: "max_tokens",
|
|
380
|
+
model_context_window_exceeded: "max_tokens",
|
|
381
|
+
refusal: "refusal"
|
|
382
|
+
};
|
|
383
|
+
function toUsage(usage) {
|
|
384
|
+
return {
|
|
385
|
+
inputTokens: count(usage?.input_tokens),
|
|
386
|
+
cacheCreationTokens: count(usage?.cache_creation_input_tokens),
|
|
387
|
+
cacheReadTokens: count(usage?.cache_read_input_tokens),
|
|
388
|
+
outputTokens: count(usage?.output_tokens),
|
|
389
|
+
raw: usage ?? null
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
__name(toUsage, "toUsage");
|
|
393
|
+
function textOf(content) {
|
|
394
|
+
return content.filter((block) => block.type === "text").map((block) => block.text).join("");
|
|
395
|
+
}
|
|
396
|
+
__name(textOf, "textOf");
|
|
397
|
+
function toolCallsOf(content) {
|
|
398
|
+
return content.filter((block) => block.type === "tool_use").map((block) => ({ id: block.id, name: block.name, input: block.input }));
|
|
399
|
+
}
|
|
400
|
+
__name(toolCallsOf, "toolCallsOf");
|
|
401
|
+
function stopReasonOf(reason) {
|
|
402
|
+
return reason && STOP_REASONS[reason] || "other";
|
|
403
|
+
}
|
|
404
|
+
__name(stopReasonOf, "stopReasonOf");
|
|
405
|
+
function refusalDetails(details) {
|
|
406
|
+
return [details?.category ?? null, details?.explanation ?? null];
|
|
407
|
+
}
|
|
408
|
+
__name(refusalDetails, "refusalDetails");
|
|
409
|
+
function fromAnthropicMessage(response, model) {
|
|
410
|
+
if (!response || !Array.isArray(response.content)) {
|
|
411
|
+
throw new InvalidResponseError(ANTHROPIC_PROVIDER, "The response carries no content array.");
|
|
412
|
+
}
|
|
413
|
+
const text = textOf(response.content);
|
|
414
|
+
const toolCalls = toolCallsOf(response.content);
|
|
415
|
+
const providerStopReason = response.stop_reason ?? null;
|
|
416
|
+
const stopReason = stopReasonOf(providerStopReason);
|
|
417
|
+
return {
|
|
418
|
+
provider: ANTHROPIC_PROVIDER,
|
|
419
|
+
model: response.model || model,
|
|
420
|
+
text,
|
|
421
|
+
toolCalls,
|
|
422
|
+
stopReason,
|
|
423
|
+
providerStopReason,
|
|
424
|
+
refusal: refusalOf(stopReason, ...refusalDetails(response.stop_details)),
|
|
425
|
+
usage: toUsage(response.usage),
|
|
426
|
+
message: { role: "assistant", text, toolCalls, raw: { provider: ANTHROPIC_PROVIDER, content: response.content } }
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
__name(fromAnthropicMessage, "fromAnthropicMessage");
|
|
430
|
+
function anthropicAdapter(options = {}) {
|
|
431
|
+
const build = options.client ?? ((credential) => defaultClient(credential, options));
|
|
432
|
+
return {
|
|
433
|
+
id: ANTHROPIC_PROVIDER,
|
|
434
|
+
defaultModel: options.defaultModel ?? ANTHROPIC_DEFAULT_MODEL,
|
|
435
|
+
capabilities: ["chat"],
|
|
436
|
+
credentialKinds: ["apiKey", "oauth"],
|
|
437
|
+
async chat(call) {
|
|
438
|
+
const params = toAnthropicParams(call);
|
|
439
|
+
const client = await build(call.credential);
|
|
440
|
+
let response;
|
|
441
|
+
try {
|
|
442
|
+
response = await client.messages.create(params, call.request.signal ? { signal: call.request.signal } : void 0);
|
|
443
|
+
} catch (error) {
|
|
444
|
+
throw mapProviderError(ANTHROPIC_PROVIDER, error, [call.credential.secret]);
|
|
445
|
+
}
|
|
446
|
+
return fromAnthropicMessage(response, call.model);
|
|
447
|
+
}
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
__name(anthropicAdapter, "anthropicAdapter");
|
|
451
|
+
|
|
452
|
+
// src/adapters/gemini.ts
|
|
453
|
+
var GEMINI_PROVIDER = "gemini";
|
|
454
|
+
function bearerAuthClient(token) {
|
|
455
|
+
return { getRequestHeaders: /* @__PURE__ */ __name(async () => new Headers({ Authorization: `Bearer ${token}` }), "getRequestHeaders") };
|
|
456
|
+
}
|
|
457
|
+
__name(bearerAuthClient, "bearerAuthClient");
|
|
458
|
+
var GEMINI_BASE_URL = "https://generativelanguage.googleapis.com/";
|
|
459
|
+
var GEMINI_ROUTES = [{ method: "POST", path: /^\/v1(?:beta)?\/models\/[^/:]+:generateContent$/ }];
|
|
460
|
+
async function defaultClient2(credential, options) {
|
|
461
|
+
const sdk = await loadSdk(GEMINI_PROVIDER, "@google/genai", () => import("@google/genai"));
|
|
462
|
+
const baseUrl = options.baseURL ?? GEMINI_BASE_URL;
|
|
463
|
+
const auth = credential.kind === "apiKey" ? { "x-goog-api-key": credential.secret } : { authorization: `Bearer ${credential.secret}` };
|
|
464
|
+
const httpOptions = { baseUrl, fetch: sealedFetch({ provider: GEMINI_PROVIDER, baseURL: baseUrl, routes: GEMINI_ROUTES, auth, fetch: options.fetch }) };
|
|
465
|
+
if (credential.kind === "apiKey") return new sdk.GoogleGenAI({ vertexai: false, apiKey: credential.secret, httpOptions });
|
|
466
|
+
const client = new sdk.GoogleGenAI({
|
|
467
|
+
vertexai: false,
|
|
468
|
+
httpOptions,
|
|
469
|
+
googleAuthOptions: { authClient: bearerAuthClient(credential.secret) }
|
|
470
|
+
});
|
|
471
|
+
if (client.apiKey !== void 0) {
|
|
472
|
+
throw new InvalidRequestError(GEMINI_PROVIDER, "An API key in the environment would replace the OAuth token; unset it or use apiKey credentials.");
|
|
473
|
+
}
|
|
474
|
+
return client;
|
|
475
|
+
}
|
|
476
|
+
__name(defaultClient2, "defaultClient");
|
|
477
|
+
function assistantContent2(message) {
|
|
478
|
+
if (message.raw?.provider === GEMINI_PROVIDER && message.raw.content && typeof message.raw.content === "object") {
|
|
479
|
+
return message.raw.content;
|
|
480
|
+
}
|
|
481
|
+
const parts = message.text ? [{ text: message.text }] : [];
|
|
482
|
+
for (const call of message.toolCalls) {
|
|
483
|
+
parts.push({ functionCall: { id: call.id, name: call.name, args: call.input ?? {} } });
|
|
484
|
+
}
|
|
485
|
+
return { role: "model", parts };
|
|
486
|
+
}
|
|
487
|
+
__name(assistantContent2, "assistantContent");
|
|
488
|
+
function toContents(messages) {
|
|
489
|
+
return messages.map((message) => {
|
|
490
|
+
if (message.role === "user") return { role: "user", parts: [{ text: message.text }] };
|
|
491
|
+
if (message.role === "assistant") return assistantContent2(message);
|
|
492
|
+
return {
|
|
493
|
+
role: "user",
|
|
494
|
+
parts: message.results.map((result) => ({
|
|
495
|
+
functionResponse: {
|
|
496
|
+
id: result.callId,
|
|
497
|
+
name: result.name,
|
|
498
|
+
response: result.isError ? { error: result.output } : { output: result.output }
|
|
499
|
+
}
|
|
500
|
+
}))
|
|
501
|
+
};
|
|
502
|
+
});
|
|
503
|
+
}
|
|
504
|
+
__name(toContents, "toContents");
|
|
505
|
+
var MODES = { auto: "AUTO", none: "NONE", required: "ANY" };
|
|
506
|
+
var LEVELS = { low: "LOW", medium: "MEDIUM", high: "HIGH" };
|
|
507
|
+
function toGeminiParams(call) {
|
|
508
|
+
const { request, model } = call;
|
|
509
|
+
const config = { maxOutputTokens: request.maxOutputTokens };
|
|
510
|
+
if (request.system && request.system.length > 0) {
|
|
511
|
+
config.systemInstruction = { parts: request.system.map((block) => ({ text: block.text })) };
|
|
512
|
+
}
|
|
513
|
+
if (request.tools && request.tools.length > 0) {
|
|
514
|
+
config.tools = [
|
|
515
|
+
{
|
|
516
|
+
functionDeclarations: request.tools.map((tool) => ({
|
|
517
|
+
name: tool.name,
|
|
518
|
+
description: tool.description,
|
|
519
|
+
parametersJsonSchema: tool.inputSchema
|
|
520
|
+
}))
|
|
521
|
+
}
|
|
522
|
+
];
|
|
523
|
+
}
|
|
524
|
+
if (request.toolChoice) {
|
|
525
|
+
config.toolConfig = { functionCallingConfig: { mode: MODES[request.toolChoice] } };
|
|
526
|
+
}
|
|
527
|
+
if (request.effort) config.thinkingConfig = { thinkingLevel: LEVELS[request.effort] };
|
|
528
|
+
if (request.temperature !== void 0) config.temperature = request.temperature;
|
|
529
|
+
if (request.signal) config.abortSignal = request.signal;
|
|
530
|
+
return { model, contents: toContents(request.messages), config };
|
|
531
|
+
}
|
|
532
|
+
__name(toGeminiParams, "toGeminiParams");
|
|
533
|
+
var REFUSALS = /* @__PURE__ */ new Set(["SAFETY", "RECITATION", "BLOCKLIST", "PROHIBITED_CONTENT", "SPII", "IMAGE_SAFETY"]);
|
|
534
|
+
function stopReasonOf2(finish, hasCalls) {
|
|
535
|
+
if (finish && REFUSALS.has(finish)) return "refusal";
|
|
536
|
+
if (finish === "MAX_TOKENS") return "max_tokens";
|
|
537
|
+
if (hasCalls) return "tool_use";
|
|
538
|
+
return finish === "STOP" ? "end" : "other";
|
|
539
|
+
}
|
|
540
|
+
__name(stopReasonOf2, "stopReasonOf");
|
|
541
|
+
function toUsage2(usage) {
|
|
542
|
+
const cacheRead = count(usage?.cachedContentTokenCount);
|
|
543
|
+
const prompt = count(usage?.promptTokenCount) + count(usage?.toolUsePromptTokenCount);
|
|
544
|
+
return {
|
|
545
|
+
inputTokens: Math.max(0, prompt - cacheRead),
|
|
546
|
+
cacheCreationTokens: 0,
|
|
547
|
+
cacheReadTokens: cacheRead,
|
|
548
|
+
outputTokens: count(usage?.candidatesTokenCount) + count(usage?.thoughtsTokenCount),
|
|
549
|
+
raw: usage ?? null
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
__name(toUsage2, "toUsage");
|
|
553
|
+
function textOf2(parts) {
|
|
554
|
+
return parts.filter((part) => typeof part.text === "string" && !part.thought).map((part) => part.text).join("");
|
|
555
|
+
}
|
|
556
|
+
__name(textOf2, "textOf");
|
|
557
|
+
function toolCallsOf2(parts) {
|
|
558
|
+
return parts.flatMap((part, index) => {
|
|
559
|
+
const call = part.functionCall;
|
|
560
|
+
if (!call) return [];
|
|
561
|
+
return [{ id: call.id ?? `gemini_call_${index}`, name: call.name ?? "", input: call.args ?? {} }];
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
__name(toolCallsOf2, "toolCallsOf");
|
|
565
|
+
function stopOf(response, hasCalls) {
|
|
566
|
+
const blocked = response.promptFeedback?.blockReason;
|
|
567
|
+
if (blocked) return { stopReason: "refusal", provider: blocked };
|
|
568
|
+
const finish = response.candidates?.[0]?.finishReason ?? null;
|
|
569
|
+
return { stopReason: stopReasonOf2(finish ?? void 0, hasCalls), provider: finish };
|
|
570
|
+
}
|
|
571
|
+
__name(stopOf, "stopOf");
|
|
572
|
+
function fromGeminiResponse(response, model) {
|
|
573
|
+
if (!response || typeof response !== "object") {
|
|
574
|
+
throw new InvalidResponseError(GEMINI_PROVIDER, "The response is empty.");
|
|
575
|
+
}
|
|
576
|
+
const parts = response.candidates?.[0]?.content?.parts ?? [];
|
|
577
|
+
const text = textOf2(parts);
|
|
578
|
+
const toolCalls = toolCallsOf2(parts);
|
|
579
|
+
const { stopReason, provider } = stopOf(response, toolCalls.length > 0);
|
|
580
|
+
const content = { role: "model", parts };
|
|
581
|
+
return {
|
|
582
|
+
provider: GEMINI_PROVIDER,
|
|
583
|
+
model: response.modelVersion || model,
|
|
584
|
+
text,
|
|
585
|
+
toolCalls,
|
|
586
|
+
stopReason,
|
|
587
|
+
providerStopReason: provider,
|
|
588
|
+
refusal: refusalOf(stopReason, provider, response.promptFeedback?.blockReasonMessage ?? null),
|
|
589
|
+
usage: toUsage2(response.usageMetadata),
|
|
590
|
+
message: { role: "assistant", text, toolCalls, raw: { provider: GEMINI_PROVIDER, content } }
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
__name(fromGeminiResponse, "fromGeminiResponse");
|
|
594
|
+
function geminiAdapter(options = {}) {
|
|
595
|
+
const build = options.client ?? ((credential) => defaultClient2(credential, options));
|
|
596
|
+
return {
|
|
597
|
+
id: GEMINI_PROVIDER,
|
|
598
|
+
defaultModel: options.defaultModel,
|
|
599
|
+
capabilities: ["chat"],
|
|
600
|
+
credentialKinds: ["apiKey", "oauth"],
|
|
601
|
+
async chat(call) {
|
|
602
|
+
const params = toGeminiParams(call);
|
|
603
|
+
const client = await build(call.credential);
|
|
604
|
+
let response;
|
|
605
|
+
try {
|
|
606
|
+
response = await client.models.generateContent(params);
|
|
607
|
+
} catch (error) {
|
|
608
|
+
throw mapProviderError(GEMINI_PROVIDER, error, [call.credential.secret]);
|
|
609
|
+
}
|
|
610
|
+
return fromGeminiResponse(response, call.model);
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
}
|
|
614
|
+
__name(geminiAdapter, "geminiAdapter");
|
|
615
|
+
|
|
616
|
+
// src/adapters/openai.ts
|
|
617
|
+
var OPENAI_PROVIDER = "openai";
|
|
618
|
+
var OPENAI_BASE_URL = "https://api.openai.com/v1";
|
|
619
|
+
var OPENAI_ROUTES = [{ method: "POST", path: "/chat/completions" }];
|
|
620
|
+
async function defaultClient3(credential, options) {
|
|
621
|
+
const sdk = await loadSdk(options.id, "openai", () => import("openai"));
|
|
622
|
+
const baseURL = options.baseURL ?? options.defaultBaseURL;
|
|
623
|
+
return new sdk.default({
|
|
624
|
+
apiKey: credential.secret,
|
|
625
|
+
organization: null,
|
|
626
|
+
project: null,
|
|
627
|
+
adminAPIKey: null,
|
|
628
|
+
baseURL,
|
|
629
|
+
fetch: sealedFetch({ provider: options.id, baseURL, routes: OPENAI_ROUTES, auth: { authorization: `Bearer ${credential.secret}` }, fetch: options.fetch }),
|
|
630
|
+
maxRetries: 0
|
|
631
|
+
});
|
|
632
|
+
}
|
|
633
|
+
__name(defaultClient3, "defaultClient");
|
|
634
|
+
var ERROR_PREFIX = "[tool error] ";
|
|
635
|
+
function toMessages2(messages) {
|
|
636
|
+
return messages.flatMap((message) => {
|
|
637
|
+
if (message.role === "user") return [{ role: "user", content: message.text }];
|
|
638
|
+
if (message.role === "assistant") {
|
|
639
|
+
const toolCalls = message.toolCalls.map((call) => ({
|
|
640
|
+
id: call.id,
|
|
641
|
+
type: "function",
|
|
642
|
+
function: { name: call.name, arguments: JSON.stringify(call.input ?? {}) }
|
|
643
|
+
}));
|
|
644
|
+
return [{ role: "assistant", content: message.text || null, ...toolCalls.length > 0 ? { tool_calls: toolCalls } : {} }];
|
|
645
|
+
}
|
|
646
|
+
return message.results.map((result) => ({
|
|
647
|
+
role: "tool",
|
|
648
|
+
tool_call_id: result.callId,
|
|
649
|
+
content: result.isError ? `${ERROR_PREFIX}${result.output}` : result.output
|
|
650
|
+
}));
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
__name(toMessages2, "toMessages");
|
|
654
|
+
function toOpenAIParams(call, promptCacheKey = true) {
|
|
655
|
+
const { request, model } = call;
|
|
656
|
+
const messages = [];
|
|
657
|
+
if (request.system && request.system.length > 0) {
|
|
658
|
+
messages.push({ role: "system", content: request.system.map((block) => ({ type: "text", text: block.text })) });
|
|
659
|
+
}
|
|
660
|
+
messages.push(...toMessages2(request.messages));
|
|
661
|
+
const params = { model, messages, max_completion_tokens: request.maxOutputTokens };
|
|
662
|
+
if (request.tools && request.tools.length > 0) {
|
|
663
|
+
params.tools = request.tools.map((tool) => ({
|
|
664
|
+
type: "function",
|
|
665
|
+
function: { name: tool.name, description: tool.description, parameters: tool.inputSchema, strict: true }
|
|
666
|
+
}));
|
|
667
|
+
}
|
|
668
|
+
return withHints(params, request, promptCacheKey);
|
|
669
|
+
}
|
|
670
|
+
__name(toOpenAIParams, "toOpenAIParams");
|
|
671
|
+
function withHints(params, request, promptCacheKey) {
|
|
672
|
+
if (request.toolChoice) params.tool_choice = request.toolChoice;
|
|
673
|
+
if (request.effort) params.reasoning_effort = request.effort;
|
|
674
|
+
if (request.temperature !== void 0) params.temperature = request.temperature;
|
|
675
|
+
if (request.cacheKey && promptCacheKey) params.prompt_cache_key = request.cacheKey;
|
|
676
|
+
return params;
|
|
677
|
+
}
|
|
678
|
+
__name(withHints, "withHints");
|
|
679
|
+
var STOP_REASONS2 = {
|
|
680
|
+
stop: "end",
|
|
681
|
+
tool_calls: "tool_use",
|
|
682
|
+
function_call: "tool_use",
|
|
683
|
+
length: "max_tokens",
|
|
684
|
+
content_filter: "refusal"
|
|
685
|
+
};
|
|
686
|
+
function toUsage3(usage) {
|
|
687
|
+
const details = usage?.prompt_tokens_details;
|
|
688
|
+
const cacheRead = count(details?.cached_tokens);
|
|
689
|
+
const cacheWrite = count(details?.cache_write_tokens);
|
|
690
|
+
return {
|
|
691
|
+
inputTokens: Math.max(0, count(usage?.prompt_tokens) - cacheRead - cacheWrite),
|
|
692
|
+
cacheCreationTokens: cacheWrite,
|
|
693
|
+
cacheReadTokens: cacheRead,
|
|
694
|
+
outputTokens: count(usage?.completion_tokens),
|
|
695
|
+
raw: usage ?? null
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
__name(toUsage3, "toUsage");
|
|
699
|
+
function parseArguments(provider, call) {
|
|
700
|
+
try {
|
|
701
|
+
return JSON.parse(call.function.arguments || "{}");
|
|
702
|
+
} catch {
|
|
703
|
+
throw new InvalidResponseError(provider, `Tool call ${call.id} (${call.function.name}) carries arguments that are not JSON.`);
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
__name(parseArguments, "parseArguments");
|
|
707
|
+
function toolCallsOf3(provider, message) {
|
|
708
|
+
return (message.tool_calls ?? []).filter((call) => call.type === "function").map((call) => ({ id: call.id, name: call.function.name, input: parseArguments(provider, call) }));
|
|
709
|
+
}
|
|
710
|
+
__name(toolCallsOf3, "toolCallsOf");
|
|
711
|
+
function refusalText(message) {
|
|
712
|
+
return typeof message.refusal === "string" && message.refusal.length > 0 ? message.refusal : null;
|
|
713
|
+
}
|
|
714
|
+
__name(refusalText, "refusalText");
|
|
715
|
+
function fromOpenAICompletion(provider, response, model) {
|
|
716
|
+
const choice = response?.choices?.[0];
|
|
717
|
+
if (!choice) throw new InvalidResponseError(provider, "The response carries no choice.");
|
|
718
|
+
const { message } = choice;
|
|
719
|
+
const toolCalls = toolCallsOf3(provider, message);
|
|
720
|
+
const refused = refusalText(message);
|
|
721
|
+
const stopReason = refused ? "refusal" : STOP_REASONS2[choice.finish_reason] ?? "other";
|
|
722
|
+
const text = message.content ?? "";
|
|
723
|
+
return {
|
|
724
|
+
provider,
|
|
725
|
+
model: response.model || model,
|
|
726
|
+
text,
|
|
727
|
+
toolCalls,
|
|
728
|
+
stopReason,
|
|
729
|
+
providerStopReason: choice.finish_reason ?? null,
|
|
730
|
+
refusal: refused ? refusalOf(stopReason, null, refused) : refusalOf(stopReason, choice.finish_reason, null),
|
|
731
|
+
usage: toUsage3(response.usage),
|
|
732
|
+
message: { role: "assistant", text, toolCalls, raw: { provider, content: message } }
|
|
733
|
+
};
|
|
734
|
+
}
|
|
735
|
+
__name(fromOpenAICompletion, "fromOpenAICompletion");
|
|
736
|
+
function openAICompatibleAdapter(options) {
|
|
737
|
+
const build = options.client ?? ((credential) => defaultClient3(credential, options));
|
|
738
|
+
const provider = options.id;
|
|
739
|
+
return {
|
|
740
|
+
id: provider,
|
|
741
|
+
defaultModel: options.defaultModel,
|
|
742
|
+
capabilities: ["chat"],
|
|
743
|
+
credentialKinds: ["apiKey"],
|
|
744
|
+
async chat(call) {
|
|
745
|
+
const params = toOpenAIParams(call, options.promptCacheKey);
|
|
746
|
+
const client = await build(call.credential);
|
|
747
|
+
let response;
|
|
748
|
+
try {
|
|
749
|
+
response = await client.chat.completions.create(params, call.request.signal ? { signal: call.request.signal } : void 0);
|
|
750
|
+
} catch (error) {
|
|
751
|
+
throw mapProviderError(provider, error, [call.credential.secret]);
|
|
752
|
+
}
|
|
753
|
+
return fromOpenAICompletion(provider, response, call.model);
|
|
754
|
+
}
|
|
755
|
+
};
|
|
756
|
+
}
|
|
757
|
+
__name(openAICompatibleAdapter, "openAICompatibleAdapter");
|
|
758
|
+
function openaiAdapter(options = {}) {
|
|
759
|
+
return openAICompatibleAdapter({ ...options, id: OPENAI_PROVIDER, defaultBaseURL: OPENAI_BASE_URL, promptCacheKey: true });
|
|
760
|
+
}
|
|
761
|
+
__name(openaiAdapter, "openaiAdapter");
|
|
762
|
+
|
|
763
|
+
// src/adapters/xai.ts
|
|
764
|
+
var XAI_PROVIDER = "xai";
|
|
765
|
+
var XAI_BASE_URL = "https://api.x.ai/v1";
|
|
766
|
+
function xaiAdapter(options = {}) {
|
|
767
|
+
return openAICompatibleAdapter({ ...options, id: XAI_PROVIDER, defaultBaseURL: XAI_BASE_URL, promptCacheKey: false });
|
|
768
|
+
}
|
|
769
|
+
__name(xaiAdapter, "xaiAdapter");
|
|
770
|
+
|
|
771
|
+
// src/adapters/index.ts
|
|
772
|
+
function builtInAdapters() {
|
|
773
|
+
return [anthropicAdapter(), openaiAdapter(), geminiAdapter(), xaiAdapter()];
|
|
774
|
+
}
|
|
775
|
+
__name(builtInAdapters, "builtInAdapters");
|
|
776
|
+
|
|
777
|
+
// src/credentials.ts
|
|
778
|
+
var INSPECT = /* @__PURE__ */ Symbol.for("nodejs.util.inspect.custom");
|
|
779
|
+
var ResolvedCredential = class {
|
|
780
|
+
static {
|
|
781
|
+
__name(this, "ResolvedCredential");
|
|
782
|
+
}
|
|
783
|
+
kind;
|
|
784
|
+
constructor(kind, secret) {
|
|
785
|
+
this.kind = kind;
|
|
786
|
+
Object.defineProperty(this, "secret", { value: secret, enumerable: false });
|
|
787
|
+
}
|
|
788
|
+
toJSON() {
|
|
789
|
+
return this.toString();
|
|
790
|
+
}
|
|
791
|
+
toString() {
|
|
792
|
+
return `[credential ${this.kind}]`;
|
|
793
|
+
}
|
|
794
|
+
[INSPECT]() {
|
|
795
|
+
return this.toString();
|
|
796
|
+
}
|
|
797
|
+
};
|
|
798
|
+
var generations = /* @__PURE__ */ new WeakMap();
|
|
799
|
+
function nonEmpty(value, what) {
|
|
800
|
+
if (typeof value !== "string" || value.length === 0) {
|
|
801
|
+
throw new TypeError(`@12-apps/llm: the host's ${what} returned no value.`);
|
|
802
|
+
}
|
|
803
|
+
return value;
|
|
804
|
+
}
|
|
805
|
+
__name(nonEmpty, "nonEmpty");
|
|
806
|
+
function createCredentialSession(credentials, options = {}) {
|
|
807
|
+
const now = options.now ?? Date.now;
|
|
808
|
+
if (credentials.kind === "apiKey") {
|
|
809
|
+
const read = /* @__PURE__ */ __name(async () => nonEmpty(
|
|
810
|
+
typeof credentials.apiKey === "function" ? await credentials.apiKey() : credentials.apiKey,
|
|
811
|
+
"API key"
|
|
812
|
+
), "read");
|
|
813
|
+
return {
|
|
814
|
+
kind: "apiKey",
|
|
815
|
+
current: /* @__PURE__ */ __name(async () => new ResolvedCredential("apiKey", await read()), "current"),
|
|
816
|
+
afterUnauthorized: /* @__PURE__ */ __name(async () => null, "afterUnauthorized")
|
|
817
|
+
};
|
|
818
|
+
}
|
|
819
|
+
const skew = credentials.refreshSkewMs ?? 6e4;
|
|
820
|
+
const oauth = credentials;
|
|
821
|
+
let generation = 0;
|
|
822
|
+
let latest = null;
|
|
823
|
+
let inFlight = null;
|
|
824
|
+
let revoked = false;
|
|
825
|
+
const assertLive = /* @__PURE__ */ __name(async () => {
|
|
826
|
+
if (revoked || await oauth.isRevoked?.()) {
|
|
827
|
+
revoked = true;
|
|
828
|
+
latest = null;
|
|
829
|
+
throw new CredentialRevokedError();
|
|
830
|
+
}
|
|
831
|
+
}, "assertLive");
|
|
832
|
+
const stamped = /* @__PURE__ */ __name((secret, of) => {
|
|
833
|
+
const credential = new ResolvedCredential("oauth", secret);
|
|
834
|
+
generations.set(credential, of);
|
|
835
|
+
return credential;
|
|
836
|
+
}, "stamped");
|
|
837
|
+
const refresh = /* @__PURE__ */ __name((reason) => {
|
|
838
|
+
inFlight ??= (async () => {
|
|
839
|
+
await assertLive();
|
|
840
|
+
const secret = nonEmpty((await oauth.refresh(reason)).accessToken, "OAuth refresh");
|
|
841
|
+
await assertLive();
|
|
842
|
+
generation += 1;
|
|
843
|
+
latest = stamped(secret, generation);
|
|
844
|
+
return latest;
|
|
845
|
+
})().finally(() => {
|
|
846
|
+
inFlight = null;
|
|
847
|
+
});
|
|
848
|
+
return inFlight;
|
|
849
|
+
}, "refresh");
|
|
850
|
+
return {
|
|
851
|
+
kind: "oauth",
|
|
852
|
+
async current() {
|
|
853
|
+
await assertLive();
|
|
854
|
+
if (inFlight) return inFlight;
|
|
855
|
+
const of = generation;
|
|
856
|
+
const token = await oauth.token();
|
|
857
|
+
const expiring = token.expiresAt !== void 0 && token.expiresAt - now() <= skew;
|
|
858
|
+
if (expiring) return refresh("expiring");
|
|
859
|
+
return stamped(nonEmpty(token.accessToken, "OAuth token provider"), of);
|
|
860
|
+
},
|
|
861
|
+
async afterUnauthorized(used) {
|
|
862
|
+
await assertLive();
|
|
863
|
+
if (inFlight) return inFlight;
|
|
864
|
+
if (latest && (generations.get(used) ?? 0) < generation) return latest;
|
|
865
|
+
return refresh("unauthorized");
|
|
866
|
+
}
|
|
867
|
+
};
|
|
868
|
+
}
|
|
869
|
+
__name(createCredentialSession, "createCredentialSession");
|
|
870
|
+
|
|
871
|
+
// src/schema.ts
|
|
872
|
+
var NESTED_SINGLE = ["items", "not", "additionalItems"];
|
|
873
|
+
var NESTED_LIST = ["anyOf", "oneOf", "allOf", "prefixItems"];
|
|
874
|
+
var NESTED_MAP = ["properties", "$defs", "definitions"];
|
|
875
|
+
function isObjectSchema(schema) {
|
|
876
|
+
const type = schema.type;
|
|
877
|
+
return type === "object" || Array.isArray(type) && type.includes("object") || "properties" in schema;
|
|
878
|
+
}
|
|
879
|
+
__name(isObjectSchema, "isObjectSchema");
|
|
880
|
+
function asSchema(value) {
|
|
881
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : null;
|
|
882
|
+
}
|
|
883
|
+
__name(asSchema, "asSchema");
|
|
884
|
+
function objectProblems(schema, path) {
|
|
885
|
+
const problems = [];
|
|
886
|
+
if (schema.additionalProperties !== false) {
|
|
887
|
+
problems.push(`${path}: an object must set additionalProperties: false`);
|
|
888
|
+
}
|
|
889
|
+
const properties = asSchema(schema.properties) ?? {};
|
|
890
|
+
const required = Array.isArray(schema.required) ? schema.required : [];
|
|
891
|
+
for (const key of Object.keys(properties)) {
|
|
892
|
+
if (!required.includes(key)) {
|
|
893
|
+
problems.push(`${path}: property "${key}" must be listed in required (use a nullable type for an optional field)`);
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
return problems;
|
|
897
|
+
}
|
|
898
|
+
__name(objectProblems, "objectProblems");
|
|
899
|
+
function nestedSchemas(schema, path) {
|
|
900
|
+
const single = NESTED_SINGLE.map((key) => [schema[key], `${path}.${key}`]);
|
|
901
|
+
const listed = NESTED_LIST.flatMap((key) => {
|
|
902
|
+
const list = schema[key];
|
|
903
|
+
return Array.isArray(list) ? list.map((item, index) => [item, `${path}.${key}[${index}]`]) : [];
|
|
904
|
+
});
|
|
905
|
+
const mapped = NESTED_MAP.flatMap(
|
|
906
|
+
(key) => Object.entries(asSchema(schema[key]) ?? {}).map(([name, value]) => [value, `${path}.${key}.${name}`])
|
|
907
|
+
);
|
|
908
|
+
return [...single, ...listed, ...mapped].flatMap(([value, at]) => {
|
|
909
|
+
const child = asSchema(value);
|
|
910
|
+
return child ? [[child, at]] : [];
|
|
911
|
+
});
|
|
912
|
+
}
|
|
913
|
+
__name(nestedSchemas, "nestedSchemas");
|
|
914
|
+
function strictSchemaProblems(schema, path = "$") {
|
|
915
|
+
const own = isObjectSchema(schema) ? objectProblems(schema, path) : [];
|
|
916
|
+
return own.concat(...nestedSchemas(schema, path).map(([child, at]) => strictSchemaProblems(child, at)));
|
|
917
|
+
}
|
|
918
|
+
__name(strictSchemaProblems, "strictSchemaProblems");
|
|
919
|
+
function toolSchemaProblems(tools) {
|
|
920
|
+
const seen = /* @__PURE__ */ new Set();
|
|
921
|
+
const problems = [];
|
|
922
|
+
for (const tool of tools) {
|
|
923
|
+
if (seen.has(tool.name)) problems.push(`tool "${tool.name}" is defined twice`);
|
|
924
|
+
seen.add(tool.name);
|
|
925
|
+
if (tool.inputSchema.type !== "object") problems.push(`tool "${tool.name}": the input schema root must be type "object"`);
|
|
926
|
+
problems.push(...strictSchemaProblems(tool.inputSchema).map((p) => `tool "${tool.name}" ${p}`));
|
|
927
|
+
}
|
|
928
|
+
return problems;
|
|
929
|
+
}
|
|
930
|
+
__name(toolSchemaProblems, "toolSchemaProblems");
|
|
931
|
+
function assertStrictTools(tools) {
|
|
932
|
+
const problems = toolSchemaProblems(tools);
|
|
933
|
+
if (problems.length > 0) {
|
|
934
|
+
throw new TypeError(`@12-apps/llm: tool schemas are not strict:
|
|
935
|
+
- ${problems.join("\n- ")}`);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
__name(assertStrictTools, "assertStrictTools");
|
|
939
|
+
|
|
940
|
+
// src/client.ts
|
|
941
|
+
function retryTransient(options = {}) {
|
|
942
|
+
const maxAttempts = options.maxAttempts ?? 3;
|
|
943
|
+
const base = options.baseDelayMs ?? 1e3;
|
|
944
|
+
return (error, attempt) => {
|
|
945
|
+
if (attempt >= maxAttempts) return null;
|
|
946
|
+
if (error.kind === "rate_limit") {
|
|
947
|
+
const hinted = error.retryAfterMs;
|
|
948
|
+
return hinted ?? base * 2 ** (attempt - 1);
|
|
949
|
+
}
|
|
950
|
+
return error.kind === "unavailable" ? base * 2 ** (attempt - 1) : null;
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
__name(retryTransient, "retryTransient");
|
|
954
|
+
var neverRetry = /* @__PURE__ */ __name(() => null, "neverRetry");
|
|
955
|
+
var defaultSleep = /* @__PURE__ */ __name((ms) => new Promise((resolve) => setTimeout(resolve, ms)), "defaultSleep");
|
|
956
|
+
function checkRequest(adapter, request) {
|
|
957
|
+
const problems = toolSchemaProblems(request.tools ?? []);
|
|
958
|
+
if (!Number.isInteger(request.maxOutputTokens) || request.maxOutputTokens <= 0) {
|
|
959
|
+
problems.push("maxOutputTokens must be a positive integer");
|
|
960
|
+
}
|
|
961
|
+
if (problems.length > 0) {
|
|
962
|
+
throw new InvalidRequestError(adapter.id, `Invalid chat request:
|
|
963
|
+
- ${problems.join("\n- ")}`);
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
__name(checkRequest, "checkRequest");
|
|
967
|
+
async function callWithAuth(adapter, session, model, request) {
|
|
968
|
+
const credential = await session.current();
|
|
969
|
+
try {
|
|
970
|
+
return await adapter.chat({ model, request, credential });
|
|
971
|
+
} catch (error) {
|
|
972
|
+
if (!(error instanceof AuthError) || error.status !== 401) throw error;
|
|
973
|
+
const renewed = await session.afterUnauthorized(credential);
|
|
974
|
+
if (!renewed) throw error;
|
|
975
|
+
return adapter.chat({ model, request, credential: renewed });
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
__name(callWithAuth, "callWithAuth");
|
|
979
|
+
function createModelClient(options) {
|
|
980
|
+
const registry = options.registry ?? createAdapterRegistry(builtInAdapters());
|
|
981
|
+
const adapter = registry.get(options.provider);
|
|
982
|
+
if (!adapter.credentialKinds.includes(options.credentials.kind)) {
|
|
983
|
+
throw new UnsupportedError(
|
|
984
|
+
adapter.id,
|
|
985
|
+
`The "${adapter.id}" adapter does not accept "${options.credentials.kind}" credentials (it accepts: ${adapter.credentialKinds.join(", ")}).`
|
|
986
|
+
);
|
|
987
|
+
}
|
|
988
|
+
const session = createCredentialSession(options.credentials, { now: options.now });
|
|
989
|
+
const model = options.model ?? adapter.defaultModel;
|
|
990
|
+
if (!model) {
|
|
991
|
+
throw new InvalidRequestError(adapter.id, `The "${adapter.id}" adapter has no default model; pass \`model\`.`);
|
|
992
|
+
}
|
|
993
|
+
const retry = options.retry ?? neverRetry;
|
|
994
|
+
const sleep = options.sleep ?? defaultSleep;
|
|
995
|
+
return {
|
|
996
|
+
provider: adapter.id,
|
|
997
|
+
model,
|
|
998
|
+
capabilities: adapter.capabilities,
|
|
999
|
+
supports: /* @__PURE__ */ __name((capability) => adapter.capabilities.includes(capability), "supports"),
|
|
1000
|
+
async chat(request) {
|
|
1001
|
+
checkRequest(adapter, request);
|
|
1002
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
1003
|
+
try {
|
|
1004
|
+
return await callWithAuth(adapter, session, request.model ?? model, request);
|
|
1005
|
+
} catch (error) {
|
|
1006
|
+
if (!(error instanceof LlmError)) throw error;
|
|
1007
|
+
const delay = retry(error, attempt);
|
|
1008
|
+
if (delay === null || request.signal?.aborted) throw error;
|
|
1009
|
+
await sleep(delay);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
}
|
|
1013
|
+
};
|
|
1014
|
+
}
|
|
1015
|
+
__name(createModelClient, "createModelClient");
|
|
1016
|
+
|
|
1017
|
+
// src/loop.ts
|
|
1018
|
+
var UNKNOWN_TOOL = "This tool does not exist.";
|
|
1019
|
+
function addUsage(total, usage) {
|
|
1020
|
+
return {
|
|
1021
|
+
inputTokens: total.inputTokens + usage.inputTokens,
|
|
1022
|
+
cacheCreationTokens: total.cacheCreationTokens + usage.cacheCreationTokens,
|
|
1023
|
+
cacheReadTokens: total.cacheReadTokens + usage.cacheReadTokens,
|
|
1024
|
+
outputTokens: total.outputTokens + usage.outputTokens
|
|
1025
|
+
};
|
|
1026
|
+
}
|
|
1027
|
+
__name(addUsage, "addUsage");
|
|
1028
|
+
async function runCall(call, turn, executors) {
|
|
1029
|
+
const executor = Object.hasOwn(executors, call.name) ? executors[call.name] : void 0;
|
|
1030
|
+
if (!executor) return { callId: call.id, name: call.name, output: UNKNOWN_TOOL, isError: true };
|
|
1031
|
+
try {
|
|
1032
|
+
const out = await executor(call.input, { call, turn });
|
|
1033
|
+
return typeof out === "string" ? { callId: call.id, name: call.name, output: out } : { callId: call.id, name: call.name, output: out.output, ...out.isError ? { isError: true } : {} };
|
|
1034
|
+
} catch (error) {
|
|
1035
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1036
|
+
return { callId: call.id, name: call.name, output: message, isError: true };
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
__name(runCall, "runCall");
|
|
1040
|
+
function settled(message) {
|
|
1041
|
+
if (message.toolCalls.length === 0) return message.text.length > 0 ? message : null;
|
|
1042
|
+
return message.text.length > 0 ? { role: "assistant", text: message.text, toolCalls: [] } : null;
|
|
1043
|
+
}
|
|
1044
|
+
__name(settled, "settled");
|
|
1045
|
+
function ended(status, messages, final, turns, usage) {
|
|
1046
|
+
const last = settled(final.message);
|
|
1047
|
+
if (last) messages.push(last);
|
|
1048
|
+
return { status, messages, final, turns, usage, refusal: status === "refused" ? final.refusal : null };
|
|
1049
|
+
}
|
|
1050
|
+
__name(ended, "ended");
|
|
1051
|
+
function statusOf(result) {
|
|
1052
|
+
if (result.stopReason === "refusal") return "refused";
|
|
1053
|
+
if (result.stopReason === "max_tokens") return "max_tokens";
|
|
1054
|
+
if (result.toolCalls.length === 0) return result.stopReason === "end" ? "completed" : "stopped";
|
|
1055
|
+
return null;
|
|
1056
|
+
}
|
|
1057
|
+
__name(statusOf, "statusOf");
|
|
1058
|
+
async function runToolLoop(options) {
|
|
1059
|
+
const maxTurns = options.maxTurns ?? 8;
|
|
1060
|
+
if (!Number.isInteger(maxTurns) || maxTurns < 1) throw new TypeError("@12-apps/llm: maxTurns must be a positive integer.");
|
|
1061
|
+
const messages = [...options.request.messages];
|
|
1062
|
+
let usage = { inputTokens: 0, cacheCreationTokens: 0, cacheReadTokens: 0, outputTokens: 0 };
|
|
1063
|
+
for (let turn = 1; ; turn += 1) {
|
|
1064
|
+
const final = await options.client.chat({ ...options.request, messages: [...messages] });
|
|
1065
|
+
usage = addUsage(usage, final.usage);
|
|
1066
|
+
await options.onUsage?.(final.usage, turn, final);
|
|
1067
|
+
const status = statusOf(final) ?? (turn >= maxTurns ? "max_turns" : null);
|
|
1068
|
+
if (status) return ended(status, messages, final, turn, usage);
|
|
1069
|
+
messages.push(final.message);
|
|
1070
|
+
const results = await Promise.all(final.toolCalls.map((call) => runCall(call, turn, options.executors)));
|
|
1071
|
+
messages.push({ role: "tool", results });
|
|
1072
|
+
}
|
|
1073
|
+
}
|
|
1074
|
+
__name(runToolLoop, "runToolLoop");
|
|
1075
|
+
function ensureNotRefused(result) {
|
|
1076
|
+
if (result.stopReason === "refusal") {
|
|
1077
|
+
throw new RefusalError(result.provider, result.refusal ?? { category: null, explanation: null });
|
|
1078
|
+
}
|
|
1079
|
+
return result;
|
|
1080
|
+
}
|
|
1081
|
+
__name(ensureNotRefused, "ensureNotRefused");
|
|
1082
|
+
export {
|
|
1083
|
+
ANTHROPIC_DEFAULT_MODEL,
|
|
1084
|
+
ANTHROPIC_PROVIDER,
|
|
1085
|
+
AuthError,
|
|
1086
|
+
CredentialRevokedError,
|
|
1087
|
+
GEMINI_PROVIDER,
|
|
1088
|
+
InvalidRequestError,
|
|
1089
|
+
InvalidResponseError,
|
|
1090
|
+
LlmError,
|
|
1091
|
+
OPENAI_PROVIDER,
|
|
1092
|
+
ProviderUnavailableError,
|
|
1093
|
+
RateLimitError,
|
|
1094
|
+
RefusalError,
|
|
1095
|
+
ResolvedCredential,
|
|
1096
|
+
UnsupportedError,
|
|
1097
|
+
XAI_PROVIDER,
|
|
1098
|
+
anthropicAdapter,
|
|
1099
|
+
assertStrictTools,
|
|
1100
|
+
builtInAdapters,
|
|
1101
|
+
createAdapterRegistry,
|
|
1102
|
+
createCredentialSession,
|
|
1103
|
+
createModelClient,
|
|
1104
|
+
ensureNotRefused,
|
|
1105
|
+
geminiAdapter,
|
|
1106
|
+
mapProviderError,
|
|
1107
|
+
openaiAdapter,
|
|
1108
|
+
retryAfterFrom,
|
|
1109
|
+
retryTransient,
|
|
1110
|
+
runToolLoop,
|
|
1111
|
+
strictSchemaProblems,
|
|
1112
|
+
toolSchemaProblems,
|
|
1113
|
+
xaiAdapter
|
|
1114
|
+
};
|
|
1115
|
+
//# sourceMappingURL=index.js.map
|