@eserstack/ai 0.0.0-stage → 5.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/LICENSE +13 -0
- package/README.md +497 -2
- package/dist/adapters/ajan-bridge.d.ts +69 -0
- package/dist/adapters/ajan-bridge.js +661 -0
- package/dist/adapters/anthropic.d.ts +25 -0
- package/dist/adapters/anthropic.js +567 -0
- package/dist/adapters/claude-code.d.ts +28 -0
- package/dist/adapters/claude-code.js +462 -0
- package/dist/adapters/cli-shared.d.ts +36 -0
- package/dist/adapters/cli-shared.js +207 -0
- package/dist/adapters/gemini.d.ts +19 -0
- package/dist/adapters/gemini.js +91 -0
- package/dist/adapters/google-shared.d.ts +58 -0
- package/dist/adapters/google-shared.js +424 -0
- package/dist/adapters/kiro.d.ts +19 -0
- package/dist/adapters/kiro.js +195 -0
- package/dist/adapters/mod.d.ts +61 -0
- package/dist/adapters/mod.js +129 -0
- package/dist/adapters/ollama.d.ts +20 -0
- package/dist/adapters/ollama.js +226 -0
- package/dist/adapters/openai.d.ts +25 -0
- package/dist/adapters/openai.js +553 -0
- package/dist/adapters/opencode.d.ts +19 -0
- package/dist/adapters/opencode.js +158 -0
- package/dist/adapters/vertexai.d.ts +19 -0
- package/dist/adapters/vertexai.js +96 -0
- package/dist/batch.d.ts +35 -0
- package/dist/batch.js +2 -0
- package/dist/commands/ask.d.ts +30 -0
- package/dist/commands/ask.js +404 -0
- package/dist/commands/list.d.ts +2 -0
- package/dist/commands/list.js +35 -0
- package/dist/config.d.ts +21 -0
- package/dist/config.js +16 -0
- package/dist/content.d.ts +14 -0
- package/dist/content.js +103 -0
- package/dist/detect.d.ts +9 -0
- package/dist/detect.js +125 -0
- package/dist/errors.d.ts +62 -0
- package/dist/errors.js +135 -0
- package/dist/generation.d.ts +126 -0
- package/dist/generation.js +96 -0
- package/dist/main.d.ts +22 -0
- package/dist/main.js +42 -0
- package/dist/mod.d.ts +16 -0
- package/dist/mod.js +11 -0
- package/dist/model.d.ts +27 -0
- package/dist/model.js +12 -0
- package/dist/module.d.ts +10 -0
- package/dist/module.js +21 -0
- package/dist/package.json +74 -0
- package/dist/registry.d.ts +22 -0
- package/dist/registry.js +97 -0
- package/dist/streams/layers.d.ts +4 -0
- package/dist/streams/layers.js +33 -0
- package/dist/streams/mod.d.ts +2 -0
- package/dist/streams/mod.js +5 -0
- package/dist/streams/sources.d.ts +6 -0
- package/dist/streams/sources.js +76 -0
- package/dist/types.d.ts +79 -0
- package/dist/types.js +2 -0
- package/package.json +60 -3
|
@@ -0,0 +1,553 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
import * as results from "@eserstack/primitives/results";
|
|
3
|
+
import * as generation from "../generation.js";
|
|
4
|
+
import * as errors from "../errors.js";
|
|
5
|
+
// =============================================================================
|
|
6
|
+
// OpenAI Model
|
|
7
|
+
// =============================================================================
|
|
8
|
+
export class OpenAIModel {
|
|
9
|
+
capabilities = [
|
|
10
|
+
"text_generation",
|
|
11
|
+
"streaming",
|
|
12
|
+
"tool_calling",
|
|
13
|
+
"vision",
|
|
14
|
+
// Audio blocks are sent as input_audio.
|
|
15
|
+
"audio",
|
|
16
|
+
"batch_processing",
|
|
17
|
+
"structured_output",
|
|
18
|
+
"reasoning",
|
|
19
|
+
];
|
|
20
|
+
provider = "openai";
|
|
21
|
+
modelId;
|
|
22
|
+
// The SDK is loaded lazily by the factory; its client type is not in scope.
|
|
23
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
24
|
+
client;
|
|
25
|
+
config;
|
|
26
|
+
// Takes the lazily imported SDK client stored in the field above.
|
|
27
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
28
|
+
constructor(client, cfg) {
|
|
29
|
+
this.client = client;
|
|
30
|
+
this.config = cfg;
|
|
31
|
+
this.modelId = cfg.model;
|
|
32
|
+
}
|
|
33
|
+
async generateText(options, signal) {
|
|
34
|
+
try {
|
|
35
|
+
const params = buildParams(this.config, options);
|
|
36
|
+
const response = await this.client.chat.completions.create(params, {
|
|
37
|
+
signal,
|
|
38
|
+
timeout: this.config.requestTimeoutMs,
|
|
39
|
+
});
|
|
40
|
+
return results.ok(mapResponse(response, this.modelId));
|
|
41
|
+
}
|
|
42
|
+
catch (err) {
|
|
43
|
+
return results.fail(classifyError(err));
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async *streamText(options, signal) {
|
|
47
|
+
try {
|
|
48
|
+
const params = buildParams(this.config, options);
|
|
49
|
+
const stream = await this.client.chat.completions.create({ ...params, stream: true }, { signal, timeout: this.config.requestTimeoutMs });
|
|
50
|
+
const toolCalls = new Map();
|
|
51
|
+
let inputTokens = 0;
|
|
52
|
+
let outputTokens = 0;
|
|
53
|
+
let refused = false;
|
|
54
|
+
for await (const chunk of stream) {
|
|
55
|
+
const choice = chunk.choices?.[0];
|
|
56
|
+
if (choice === undefined) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const delta = choice.delta;
|
|
60
|
+
if (delta?.content !== undefined && delta.content !== null) {
|
|
61
|
+
yield { kind: "content_delta", textDelta: delta.content };
|
|
62
|
+
}
|
|
63
|
+
// A refusal arrives in its own field and explains why there is no
|
|
64
|
+
// answer, so it streams as text too.
|
|
65
|
+
if (delta?.refusal !== undefined &&
|
|
66
|
+
delta.refusal !== null &&
|
|
67
|
+
delta.refusal !== "") {
|
|
68
|
+
refused = true;
|
|
69
|
+
yield { kind: "content_delta", textDelta: delta.refusal };
|
|
70
|
+
}
|
|
71
|
+
if (delta?.tool_calls !== undefined) {
|
|
72
|
+
for (const tc of delta.tool_calls) {
|
|
73
|
+
const index = tc.index ?? 0;
|
|
74
|
+
const existing = toolCalls.get(index);
|
|
75
|
+
if (existing === undefined) {
|
|
76
|
+
toolCalls.set(index, {
|
|
77
|
+
id: tc.id ?? "",
|
|
78
|
+
name: tc.function?.name ?? "",
|
|
79
|
+
args: tc.function?.arguments ?? "",
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
existing.args += tc.function?.arguments ?? "";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (choice.finish_reason !== undefined &&
|
|
88
|
+
choice.finish_reason !== null) {
|
|
89
|
+
// Emit completed tool calls
|
|
90
|
+
for (const [, tc] of toolCalls) {
|
|
91
|
+
let args = {};
|
|
92
|
+
if (tc.args !== "") {
|
|
93
|
+
try {
|
|
94
|
+
args = JSON.parse(tc.args);
|
|
95
|
+
}
|
|
96
|
+
catch {
|
|
97
|
+
// Keep empty args on parse failure
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
yield {
|
|
101
|
+
kind: "tool_call_delta",
|
|
102
|
+
toolCall: {
|
|
103
|
+
id: tc.id,
|
|
104
|
+
name: tc.name,
|
|
105
|
+
arguments: args,
|
|
106
|
+
},
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if (chunk.usage !== undefined) {
|
|
110
|
+
inputTokens = chunk.usage.prompt_tokens ?? 0;
|
|
111
|
+
outputTokens = chunk.usage.completion_tokens ?? 0;
|
|
112
|
+
}
|
|
113
|
+
yield {
|
|
114
|
+
kind: "message_done",
|
|
115
|
+
stopReason: refused
|
|
116
|
+
? "refusal"
|
|
117
|
+
: mapStopReason(choice.finish_reason),
|
|
118
|
+
usage: {
|
|
119
|
+
inputTokens,
|
|
120
|
+
outputTokens,
|
|
121
|
+
totalTokens: inputTokens + outputTokens,
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
catch (err) {
|
|
128
|
+
yield { kind: "error", error: classifyError(err) };
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
async submitBatch(request, signal) {
|
|
132
|
+
const lines = request.items.map((item) => {
|
|
133
|
+
const params = buildParams(this.config, item.options);
|
|
134
|
+
return JSON.stringify({
|
|
135
|
+
custom_id: item.customId,
|
|
136
|
+
method: "POST",
|
|
137
|
+
url: "/v1/chat/completions",
|
|
138
|
+
body: params,
|
|
139
|
+
});
|
|
140
|
+
});
|
|
141
|
+
const jsonlContent = lines.join("\n");
|
|
142
|
+
const blob = new Blob([jsonlContent], { type: "application/jsonl" });
|
|
143
|
+
try {
|
|
144
|
+
const file = await this.client.files.create({ file: blob, purpose: "batch" }, { signal });
|
|
145
|
+
const batchResult = await this.client.batches.create({
|
|
146
|
+
input_file_id: file.id,
|
|
147
|
+
endpoint: "/v1/chat/completions",
|
|
148
|
+
completion_window: "24h",
|
|
149
|
+
}, { signal });
|
|
150
|
+
return mapBatchJob(batchResult);
|
|
151
|
+
}
|
|
152
|
+
catch (err) {
|
|
153
|
+
throw classifyError(err);
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
async getBatchJob(jobId, signal) {
|
|
157
|
+
try {
|
|
158
|
+
const result = await this.client.batches.retrieve(jobId, { signal });
|
|
159
|
+
return mapBatchJob(result);
|
|
160
|
+
}
|
|
161
|
+
catch (err) {
|
|
162
|
+
throw classifyError(err);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
async listBatchJobs(options, signal) {
|
|
166
|
+
try {
|
|
167
|
+
const result = await this.client.batches.list({ limit: options?.limit, after: options?.after }, { signal });
|
|
168
|
+
const jobs = [];
|
|
169
|
+
for (const item of result.data ?? []) {
|
|
170
|
+
jobs.push(mapBatchJob(item));
|
|
171
|
+
}
|
|
172
|
+
return jobs;
|
|
173
|
+
}
|
|
174
|
+
catch (err) {
|
|
175
|
+
throw classifyError(err);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
async downloadBatchResults(job, signal) {
|
|
179
|
+
const outputRef = job.storage?.outputRef;
|
|
180
|
+
if (outputRef === undefined) {
|
|
181
|
+
return [];
|
|
182
|
+
}
|
|
183
|
+
try {
|
|
184
|
+
const fileContent = await this.client.files.content(outputRef, {
|
|
185
|
+
signal,
|
|
186
|
+
});
|
|
187
|
+
const text = await fileContent.text();
|
|
188
|
+
const lines = text.split("\n").filter((line) => line.length > 0);
|
|
189
|
+
const results = [];
|
|
190
|
+
for (const line of lines) {
|
|
191
|
+
const entry = JSON.parse(line);
|
|
192
|
+
if (entry.response?.status_code === 200) {
|
|
193
|
+
results.push({
|
|
194
|
+
customId: entry.custom_id,
|
|
195
|
+
result: mapResponse(entry.response.body, this.modelId),
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
results.push({
|
|
200
|
+
customId: entry.custom_id,
|
|
201
|
+
error: entry.error?.message ?? "Unknown error",
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return results;
|
|
206
|
+
}
|
|
207
|
+
catch (err) {
|
|
208
|
+
throw classifyError(err);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
async cancelBatchJob(jobId, signal) {
|
|
212
|
+
try {
|
|
213
|
+
await this.client.batches.cancel(jobId, { signal });
|
|
214
|
+
}
|
|
215
|
+
catch (err) {
|
|
216
|
+
throw classifyError(err);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
async close() {
|
|
220
|
+
// OpenAI SDK doesn't require explicit cleanup
|
|
221
|
+
}
|
|
222
|
+
getRawClient() {
|
|
223
|
+
return this.client;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// =============================================================================
|
|
227
|
+
// Factory
|
|
228
|
+
// =============================================================================
|
|
229
|
+
export const openaiFactory = {
|
|
230
|
+
provider: "openai",
|
|
231
|
+
createModel: async (cfg) => {
|
|
232
|
+
const openaiSdk = await import("openai");
|
|
233
|
+
const clientOptions = {};
|
|
234
|
+
if (cfg.apiKey !== undefined) {
|
|
235
|
+
clientOptions["apiKey"] = cfg.apiKey;
|
|
236
|
+
}
|
|
237
|
+
if (cfg.baseUrl !== undefined) {
|
|
238
|
+
clientOptions["baseURL"] = cfg.baseUrl;
|
|
239
|
+
}
|
|
240
|
+
const client = new openaiSdk.OpenAI(clientOptions);
|
|
241
|
+
return new OpenAIModel(client, cfg);
|
|
242
|
+
},
|
|
243
|
+
};
|
|
244
|
+
// =============================================================================
|
|
245
|
+
// Internal Helpers
|
|
246
|
+
// =============================================================================
|
|
247
|
+
const buildParams = (cfg, options) => {
|
|
248
|
+
const messages = mapMessages(options.messages, options.system);
|
|
249
|
+
// Keys follow the OpenAI Chat Completions request body.
|
|
250
|
+
const params = {
|
|
251
|
+
model: cfg.model,
|
|
252
|
+
messages,
|
|
253
|
+
max_tokens: options.maxTokens ?? cfg.maxTokens,
|
|
254
|
+
};
|
|
255
|
+
if (options.temperature !== undefined) {
|
|
256
|
+
params["temperature"] = options.temperature;
|
|
257
|
+
}
|
|
258
|
+
if (options.topP !== undefined) {
|
|
259
|
+
params["top_p"] = options.topP;
|
|
260
|
+
}
|
|
261
|
+
if (options.stopWords !== undefined) {
|
|
262
|
+
params["stop"] = options.stopWords;
|
|
263
|
+
}
|
|
264
|
+
if (options.tools !== undefined) {
|
|
265
|
+
params["tools"] = options.tools.map((tool) => ({
|
|
266
|
+
type: "function",
|
|
267
|
+
function: {
|
|
268
|
+
name: tool.name,
|
|
269
|
+
description: tool.description,
|
|
270
|
+
parameters: tool.parameters ?? { type: "object", properties: {} },
|
|
271
|
+
},
|
|
272
|
+
}));
|
|
273
|
+
}
|
|
274
|
+
if (options.toolChoice !== undefined) {
|
|
275
|
+
params["tool_choice"] = options.toolChoice;
|
|
276
|
+
}
|
|
277
|
+
if (options.responseFormat !== undefined) {
|
|
278
|
+
if (options.responseFormat.type === "json_schema") {
|
|
279
|
+
params["response_format"] = {
|
|
280
|
+
type: "json_schema",
|
|
281
|
+
json_schema: {
|
|
282
|
+
name: options.responseFormat.name ?? "response",
|
|
283
|
+
schema: generation.requireJsonSchema(options.responseFormat, "openai"),
|
|
284
|
+
strict: true,
|
|
285
|
+
},
|
|
286
|
+
};
|
|
287
|
+
}
|
|
288
|
+
else if (options.responseFormat.type === "json_object") {
|
|
289
|
+
params["response_format"] = { type: "json_object" };
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
// The effort levels are OpenAI's own reasoning_effort values. An explicit
|
|
293
|
+
// effort wins over the level the budget maps to.
|
|
294
|
+
if (options.effort !== undefined) {
|
|
295
|
+
params["reasoning_effort"] = options.effort;
|
|
296
|
+
}
|
|
297
|
+
else if (options.thinkingBudget !== undefined) {
|
|
298
|
+
params["reasoning_effort"] = mapReasoningEffort(options.thinkingBudget);
|
|
299
|
+
}
|
|
300
|
+
return params;
|
|
301
|
+
};
|
|
302
|
+
const mapMessages = (messages, system) => {
|
|
303
|
+
const result = [];
|
|
304
|
+
if (system !== undefined) {
|
|
305
|
+
result.push({ role: "system", content: system });
|
|
306
|
+
}
|
|
307
|
+
for (const message of messages) {
|
|
308
|
+
if (message.role === "system") {
|
|
309
|
+
const textParts = [];
|
|
310
|
+
for (const block of message.content) {
|
|
311
|
+
if (block.kind === "text") {
|
|
312
|
+
textParts.push(block.text);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
result.push({ role: "system", content: textParts.join("\n") });
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
if (message.role === "tool") {
|
|
319
|
+
for (const block of message.content) {
|
|
320
|
+
if (block.kind === "tool_result") {
|
|
321
|
+
result.push({
|
|
322
|
+
role: "tool",
|
|
323
|
+
tool_call_id: block.toolResult.toolCallId,
|
|
324
|
+
content: block.toolResult.content,
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
// Thinking blocks are left out: OpenAI has no field for another
|
|
331
|
+
// provider's reasoning, and as text they would become part of the turn.
|
|
332
|
+
const contentParts = message.content
|
|
333
|
+
.filter((block) => block.kind !== "thinking")
|
|
334
|
+
.map(mapContentBlock);
|
|
335
|
+
if (contentParts.length === 1 && contentParts[0]?.["type"] === "text") {
|
|
336
|
+
result.push({ role: message.role, content: contentParts[0]["text"] });
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
result.push({ role: message.role, content: contentParts });
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
return result;
|
|
343
|
+
};
|
|
344
|
+
const mapContentBlock = (block) => {
|
|
345
|
+
switch (block.kind) {
|
|
346
|
+
case "text": {
|
|
347
|
+
return { type: "text", text: block.text };
|
|
348
|
+
}
|
|
349
|
+
case "image": {
|
|
350
|
+
let url;
|
|
351
|
+
if (block.image.url !== undefined) {
|
|
352
|
+
url = block.image.url;
|
|
353
|
+
}
|
|
354
|
+
else if (block.image.data !== undefined) {
|
|
355
|
+
const mimeType = block.image.mimeType ?? "image/png";
|
|
356
|
+
url = `data:${mimeType};base64,${encodeBase64(block.image.data)}`;
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
return { type: "text", text: "[unsupported image]" };
|
|
360
|
+
}
|
|
361
|
+
return {
|
|
362
|
+
type: "image_url",
|
|
363
|
+
image_url: {
|
|
364
|
+
url,
|
|
365
|
+
detail: block.image.detail ?? "auto",
|
|
366
|
+
},
|
|
367
|
+
};
|
|
368
|
+
}
|
|
369
|
+
case "audio": {
|
|
370
|
+
if (block.audio.data !== undefined) {
|
|
371
|
+
const mimeType = block.audio.mimeType ?? "audio/mpeg";
|
|
372
|
+
const format = mimeTypeToAudioFormat(mimeType);
|
|
373
|
+
return {
|
|
374
|
+
type: "input_audio",
|
|
375
|
+
input_audio: {
|
|
376
|
+
data: encodeBase64(block.audio.data),
|
|
377
|
+
format,
|
|
378
|
+
},
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
return { type: "text", text: "[unsupported audio format]" };
|
|
382
|
+
}
|
|
383
|
+
case "tool_call": {
|
|
384
|
+
return {
|
|
385
|
+
type: "function",
|
|
386
|
+
id: block.toolCall.id,
|
|
387
|
+
function: {
|
|
388
|
+
name: block.toolCall.name,
|
|
389
|
+
arguments: JSON.stringify(block.toolCall.arguments),
|
|
390
|
+
},
|
|
391
|
+
};
|
|
392
|
+
}
|
|
393
|
+
default: {
|
|
394
|
+
return {
|
|
395
|
+
type: "text",
|
|
396
|
+
text: `[unsupported content: ${block.kind}]`,
|
|
397
|
+
};
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
};
|
|
401
|
+
const mapResponse = (
|
|
402
|
+
// The response is untyped vendor JSON; only the mapped fields are read.
|
|
403
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
404
|
+
response, modelId) => {
|
|
405
|
+
const contentBlocks = [];
|
|
406
|
+
let stopReason = "end_turn";
|
|
407
|
+
const choice = response.choices?.[0];
|
|
408
|
+
if (choice !== undefined) {
|
|
409
|
+
if (choice.message?.content !== undefined &&
|
|
410
|
+
choice.message.content !== null) {
|
|
411
|
+
contentBlocks.push({ kind: "text", text: choice.message.content });
|
|
412
|
+
}
|
|
413
|
+
// A model that declines a structured-output request sets message.refusal
|
|
414
|
+
// and still finishes with "stop". The refusal explains why there is no
|
|
415
|
+
// answer, so it is kept as text.
|
|
416
|
+
const refusal = choice.message?.refusal;
|
|
417
|
+
const refused = typeof refusal === "string" && refusal !== "";
|
|
418
|
+
if (refused) {
|
|
419
|
+
contentBlocks.push({ kind: "text", text: refusal });
|
|
420
|
+
}
|
|
421
|
+
if (choice.message?.tool_calls !== undefined) {
|
|
422
|
+
for (const tc of choice.message.tool_calls) {
|
|
423
|
+
let args = {};
|
|
424
|
+
try {
|
|
425
|
+
args = JSON.parse(tc.function?.arguments ?? "{}");
|
|
426
|
+
}
|
|
427
|
+
catch {
|
|
428
|
+
// Keep empty args on parse failure
|
|
429
|
+
}
|
|
430
|
+
contentBlocks.push({
|
|
431
|
+
kind: "tool_call",
|
|
432
|
+
toolCall: {
|
|
433
|
+
id: tc.id,
|
|
434
|
+
name: tc.function?.name ?? "",
|
|
435
|
+
arguments: args,
|
|
436
|
+
},
|
|
437
|
+
});
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
stopReason = refused ? "refusal" : mapStopReason(choice.finish_reason);
|
|
441
|
+
}
|
|
442
|
+
return {
|
|
443
|
+
content: contentBlocks,
|
|
444
|
+
stopReason,
|
|
445
|
+
usage: {
|
|
446
|
+
inputTokens: response.usage?.prompt_tokens ?? 0,
|
|
447
|
+
outputTokens: response.usage?.completion_tokens ?? 0,
|
|
448
|
+
totalTokens: response.usage?.total_tokens ?? 0,
|
|
449
|
+
},
|
|
450
|
+
modelId,
|
|
451
|
+
rawResponse: response,
|
|
452
|
+
};
|
|
453
|
+
};
|
|
454
|
+
/**
|
|
455
|
+
* Maps OpenAI's finish_reason. `content_filter` means a filter withheld the
|
|
456
|
+
* answer, so it is a refusal; `function_call` is the deprecated name of
|
|
457
|
+
* `tool_calls`. A missing reason, which some OpenAI-compatible servers send, is
|
|
458
|
+
* taken as a finished answer; any other reason has no closer match than `stop`.
|
|
459
|
+
*/
|
|
460
|
+
const mapStopReason = (reason) => {
|
|
461
|
+
switch (reason) {
|
|
462
|
+
case "stop":
|
|
463
|
+
case null:
|
|
464
|
+
case undefined:
|
|
465
|
+
case "":
|
|
466
|
+
return "end_turn";
|
|
467
|
+
case "length":
|
|
468
|
+
return "max_tokens";
|
|
469
|
+
case "tool_calls":
|
|
470
|
+
case "function_call":
|
|
471
|
+
return "tool_use";
|
|
472
|
+
case "content_filter":
|
|
473
|
+
return "refusal";
|
|
474
|
+
default:
|
|
475
|
+
return "stop";
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
// The batch job is untyped vendor JSON; only the mapped fields are read.
|
|
479
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
480
|
+
const mapBatchJob = (result) => {
|
|
481
|
+
return {
|
|
482
|
+
id: result.id,
|
|
483
|
+
status: mapBatchStatus(result.status),
|
|
484
|
+
createdAt: new Date(result.created_at * 1000),
|
|
485
|
+
completedAt: result.completed_at !== undefined
|
|
486
|
+
? new Date(result.completed_at * 1000)
|
|
487
|
+
: undefined,
|
|
488
|
+
totalCount: result.request_counts?.total ?? 0,
|
|
489
|
+
doneCount: result.request_counts?.completed ?? 0,
|
|
490
|
+
failedCount: result.request_counts?.failed ?? 0,
|
|
491
|
+
storage: {
|
|
492
|
+
type: "openai_file",
|
|
493
|
+
inputRef: result.input_file_id,
|
|
494
|
+
outputRef: result.output_file_id,
|
|
495
|
+
},
|
|
496
|
+
};
|
|
497
|
+
};
|
|
498
|
+
const mapBatchStatus = (status) => {
|
|
499
|
+
if (status === "completed") {
|
|
500
|
+
return "completed";
|
|
501
|
+
}
|
|
502
|
+
if (status === "failed") {
|
|
503
|
+
return "failed";
|
|
504
|
+
}
|
|
505
|
+
if (status === "cancelled" || status === "cancelling") {
|
|
506
|
+
return "cancelled";
|
|
507
|
+
}
|
|
508
|
+
if (status === "in_progress" || status === "finalizing") {
|
|
509
|
+
return "processing";
|
|
510
|
+
}
|
|
511
|
+
return "pending";
|
|
512
|
+
};
|
|
513
|
+
const mapReasoningEffort = (budget) => {
|
|
514
|
+
if (budget <= 1000) {
|
|
515
|
+
return "low";
|
|
516
|
+
}
|
|
517
|
+
if (budget >= 10000) {
|
|
518
|
+
return "high";
|
|
519
|
+
}
|
|
520
|
+
return "medium";
|
|
521
|
+
};
|
|
522
|
+
const mimeTypeToAudioFormat = (mimeType) => {
|
|
523
|
+
if (mimeType.includes("mp3") || mimeType.includes("mpeg")) {
|
|
524
|
+
return "mp3";
|
|
525
|
+
}
|
|
526
|
+
if (mimeType.includes("wav")) {
|
|
527
|
+
return "wav";
|
|
528
|
+
}
|
|
529
|
+
if (mimeType.includes("flac")) {
|
|
530
|
+
return "flac";
|
|
531
|
+
}
|
|
532
|
+
if (mimeType.includes("ogg")) {
|
|
533
|
+
return "ogg";
|
|
534
|
+
}
|
|
535
|
+
return "mp3";
|
|
536
|
+
};
|
|
537
|
+
const classifyError = (err) => {
|
|
538
|
+
if (err instanceof errors.AiError) {
|
|
539
|
+
return err;
|
|
540
|
+
}
|
|
541
|
+
const error = err instanceof Error ? err : new Error(String(err));
|
|
542
|
+
// SDK errors carry the HTTP status on a field their type does not declare.
|
|
543
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
544
|
+
const statusCode = err?.status ?? err?.statusCode ?? 500;
|
|
545
|
+
return errors.classifyAndWrap("openai", statusCode, error);
|
|
546
|
+
};
|
|
547
|
+
const encodeBase64 = (data) => {
|
|
548
|
+
let binary = "";
|
|
549
|
+
for (let i = 0; i < data.length; i++) {
|
|
550
|
+
binary += String.fromCharCode(data[i]);
|
|
551
|
+
}
|
|
552
|
+
return btoa(binary);
|
|
553
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as results from "@eserstack/primitives/results";
|
|
2
|
+
import type * as types from "../types.ts";
|
|
3
|
+
import type * as config from "../config.ts";
|
|
4
|
+
import type * as generation from "../generation.ts";
|
|
5
|
+
import type * as model from "../model.ts";
|
|
6
|
+
import * as errors from "../errors.ts";
|
|
7
|
+
export declare class OpenCodeModel implements model.LanguageModel {
|
|
8
|
+
readonly capabilities: readonly types.ProviderCapability[];
|
|
9
|
+
readonly provider = "opencode";
|
|
10
|
+
readonly modelId: string;
|
|
11
|
+
private readonly binary;
|
|
12
|
+
private readonly config;
|
|
13
|
+
constructor(binary: string, cfg: config.ResolvedConfigTarget);
|
|
14
|
+
generateText(options: generation.GenerateTextOptions, signal?: AbortSignal): Promise<results.Result<generation.GenerateTextResult, errors.AiError>>;
|
|
15
|
+
streamText(options: generation.GenerateTextOptions, signal?: AbortSignal): AsyncIterable<generation.StreamEvent>;
|
|
16
|
+
close(): Promise<void>;
|
|
17
|
+
getRawClient(): unknown;
|
|
18
|
+
}
|
|
19
|
+
export declare const openCodeFactory: model.ProviderFactory;
|