@eserstack/ai 0.0.0-bootstrap.0 → 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 +498 -0
- 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 +63 -1
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
/**
|
|
3
|
+
* Ajan FFI bridge adapter for @eserstack/ai.
|
|
4
|
+
*
|
|
5
|
+
* Routes AI calls through the Go `pkg/ajan/aifx` implementation via the
|
|
6
|
+
* `@eserstack/ajan` C-shared library. Falls back gracefully when native
|
|
7
|
+
* FFI is unavailable.
|
|
8
|
+
*
|
|
9
|
+
* @module
|
|
10
|
+
*/
|
|
11
|
+
import * as results from "@eserstack/primitives/results";
|
|
12
|
+
import * as errors from "../errors.js";
|
|
13
|
+
// =============================================================================
|
|
14
|
+
// Helpers
|
|
15
|
+
// =============================================================================
|
|
16
|
+
/**
|
|
17
|
+
* Base64-encode binary content for the wire.
|
|
18
|
+
*
|
|
19
|
+
* Chunked rather than `btoa(String.fromCharCode(...bytes))`, which the rest of
|
|
20
|
+
* the repo uses: spreading a multi-megabyte array into a call blows the
|
|
21
|
+
* argument limit and throws, and image payloads are exactly that size.
|
|
22
|
+
*/
|
|
23
|
+
const encodeBase64 = (bytes) => {
|
|
24
|
+
const chunkSize = 0x8000;
|
|
25
|
+
let binary = "";
|
|
26
|
+
for (let i = 0; i < bytes.length; i += chunkSize) {
|
|
27
|
+
binary += String.fromCharCode(...bytes.subarray(i, i + chunkSize));
|
|
28
|
+
}
|
|
29
|
+
return btoa(binary);
|
|
30
|
+
};
|
|
31
|
+
const mapStopReason = (raw) => {
|
|
32
|
+
switch (raw) {
|
|
33
|
+
case "end_turn":
|
|
34
|
+
return "end_turn";
|
|
35
|
+
case "max_tokens":
|
|
36
|
+
return "max_tokens";
|
|
37
|
+
case "tool_use":
|
|
38
|
+
return "tool_use";
|
|
39
|
+
case "refusal":
|
|
40
|
+
return "refusal";
|
|
41
|
+
default:
|
|
42
|
+
return "stop";
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
/** Rebuilds a thinking part, keeping only the fields Go sent. */
|
|
46
|
+
const mapThinking = (part) => {
|
|
47
|
+
return {
|
|
48
|
+
text: part.text ?? "",
|
|
49
|
+
...(part.signature === undefined ? {} : { signature: part.signature }),
|
|
50
|
+
...(part.redactedData === undefined
|
|
51
|
+
? {}
|
|
52
|
+
: { redactedData: part.redactedData }),
|
|
53
|
+
};
|
|
54
|
+
};
|
|
55
|
+
const mapUsage = (u) => {
|
|
56
|
+
return {
|
|
57
|
+
inputTokens: u?.inputTokens ?? 0,
|
|
58
|
+
outputTokens: u?.outputTokens ?? 0,
|
|
59
|
+
totalTokens: u?.totalTokens ?? 0,
|
|
60
|
+
thinkingTokens: u?.thinkingTokens,
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
const mapContent = (blocks) => {
|
|
64
|
+
if (blocks === undefined) {
|
|
65
|
+
return [];
|
|
66
|
+
}
|
|
67
|
+
const result = [];
|
|
68
|
+
for (const block of blocks) {
|
|
69
|
+
if (block.type === "text" && block.text !== undefined) {
|
|
70
|
+
result.push({ kind: "text", text: block.text });
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
// Tool calls used to be dropped here silently. A model that answered by
|
|
74
|
+
// calling a tool therefore produced an empty content array, which reads as
|
|
75
|
+
// "the model said nothing" rather than "the transport discarded it".
|
|
76
|
+
if (block.type === "tool_call" &&
|
|
77
|
+
block.id !== undefined &&
|
|
78
|
+
block.name !== undefined) {
|
|
79
|
+
result.push({
|
|
80
|
+
kind: "tool_call",
|
|
81
|
+
toolCall: {
|
|
82
|
+
id: block.id,
|
|
83
|
+
name: block.name,
|
|
84
|
+
arguments: block.arguments ?? {},
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
continue;
|
|
88
|
+
}
|
|
89
|
+
if (block.type === "thinking" && block.thinking !== undefined) {
|
|
90
|
+
result.push({ kind: "thinking", thinking: mapThinking(block.thinking) });
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
if (block.type === "tool_result" && block.id !== undefined) {
|
|
94
|
+
result.push({
|
|
95
|
+
kind: "tool_result",
|
|
96
|
+
toolResult: {
|
|
97
|
+
toolCallId: block.id,
|
|
98
|
+
content: block.text ?? "",
|
|
99
|
+
isError: false,
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return result;
|
|
105
|
+
};
|
|
106
|
+
const mapBatchJob = (j) => {
|
|
107
|
+
return {
|
|
108
|
+
id: j.id,
|
|
109
|
+
status: j.status,
|
|
110
|
+
createdAt: new Date(j.createdAt),
|
|
111
|
+
completedAt: j.completedAt !== undefined ? new Date(j.completedAt) : undefined,
|
|
112
|
+
totalCount: j.totalCount,
|
|
113
|
+
doneCount: j.doneCount,
|
|
114
|
+
failedCount: j.failedCount,
|
|
115
|
+
storage: j.storage,
|
|
116
|
+
error: j.error,
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
/**
|
|
120
|
+
* Serialize one content block for the wire.
|
|
121
|
+
*
|
|
122
|
+
* Every kind carries its payload. The previous `{ type: b.kind }` fallthrough
|
|
123
|
+
* named the kind and dropped everything in it, so an image reached the provider
|
|
124
|
+
* as the bare word "image" and the model answered confidently about a picture
|
|
125
|
+
* it was never shown.
|
|
126
|
+
*/
|
|
127
|
+
const buildContentBlock = (block) => {
|
|
128
|
+
switch (block.kind) {
|
|
129
|
+
case "text":
|
|
130
|
+
return { type: "text", text: block.text };
|
|
131
|
+
case "image":
|
|
132
|
+
return {
|
|
133
|
+
type: "image",
|
|
134
|
+
image: {
|
|
135
|
+
url: block.image.url,
|
|
136
|
+
mimeType: block.image.mimeType,
|
|
137
|
+
detail: block.image.detail,
|
|
138
|
+
// Base64: a Uint8Array does not survive JSON.stringify, which turns
|
|
139
|
+
// it into {"0":..,"1":..} rather than anything Go can decode.
|
|
140
|
+
data: block.image.data === undefined
|
|
141
|
+
? undefined
|
|
142
|
+
: encodeBase64(block.image.data),
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
case "audio":
|
|
146
|
+
return {
|
|
147
|
+
type: "audio",
|
|
148
|
+
audio: {
|
|
149
|
+
url: block.audio.url,
|
|
150
|
+
mimeType: block.audio.mimeType,
|
|
151
|
+
data: block.audio.data === undefined
|
|
152
|
+
? undefined
|
|
153
|
+
: encodeBase64(block.audio.data),
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
case "file":
|
|
157
|
+
return {
|
|
158
|
+
type: "file",
|
|
159
|
+
file: { uri: block.file.uri, mimeType: block.file.mimeType },
|
|
160
|
+
};
|
|
161
|
+
case "tool_call":
|
|
162
|
+
return {
|
|
163
|
+
type: "tool_call",
|
|
164
|
+
toolCall: {
|
|
165
|
+
id: block.toolCall.id,
|
|
166
|
+
name: block.toolCall.name,
|
|
167
|
+
arguments: block.toolCall.arguments,
|
|
168
|
+
},
|
|
169
|
+
};
|
|
170
|
+
case "tool_result":
|
|
171
|
+
return {
|
|
172
|
+
type: "tool_result",
|
|
173
|
+
toolResult: {
|
|
174
|
+
toolCallId: block.toolResult.toolCallId,
|
|
175
|
+
content: block.toolResult.content,
|
|
176
|
+
isError: block.toolResult.isError,
|
|
177
|
+
},
|
|
178
|
+
};
|
|
179
|
+
case "thinking":
|
|
180
|
+
// Sent back unchanged: the provider checks the signature.
|
|
181
|
+
return {
|
|
182
|
+
type: "thinking",
|
|
183
|
+
thinking: {
|
|
184
|
+
text: block.thinking.text,
|
|
185
|
+
signature: block.thinking.signature,
|
|
186
|
+
redactedData: block.thinking.redactedData,
|
|
187
|
+
},
|
|
188
|
+
};
|
|
189
|
+
default:
|
|
190
|
+
return { type: block.kind };
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
/** Rebuild the typed error a bridge envelope describes. */
|
|
194
|
+
const wireError = (provider, resp, fallback) => errors.classifyWireError(provider, resp.error ?? fallback, resp.errorKind, resp.errorStatus);
|
|
195
|
+
/** The error a caller gets when their AbortSignal fired. */
|
|
196
|
+
const abortError = (provider) => new errors.AiError("request aborted", { provider });
|
|
197
|
+
/**
|
|
198
|
+
* Awaits one bridge operation that runs with the caller's signal.
|
|
199
|
+
*
|
|
200
|
+
* The AI names are blocking: the library runs them in a worker thread, so the
|
|
201
|
+
* event loop stays free and the signal can fire. On abort it sends
|
|
202
|
+
* aiCancelRequest for the request's id, which stops the Go call, and rejects
|
|
203
|
+
* with the signal's reason; that becomes this provider's abort error.
|
|
204
|
+
*/
|
|
205
|
+
const abortable = async (provider, signal, pending) => {
|
|
206
|
+
try {
|
|
207
|
+
return (await pending);
|
|
208
|
+
}
|
|
209
|
+
catch (error) {
|
|
210
|
+
if (signal?.aborted === true) {
|
|
211
|
+
throw abortError(provider);
|
|
212
|
+
}
|
|
213
|
+
throw error;
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
const buildOptions = (options) => {
|
|
217
|
+
const messages = options.messages.map((m) => ({
|
|
218
|
+
role: m.role,
|
|
219
|
+
content: m.content.map(buildContentBlock),
|
|
220
|
+
}));
|
|
221
|
+
// Optionals are passed through as-is rather than defaulted. The request is
|
|
222
|
+
// sent as JSON, which omits `undefined`, so an unset option stays absent and
|
|
223
|
+
// the Go pointer stays nil -- whereas `?? 0` would pin every request to
|
|
224
|
+
// temperature 0, which is a real setting and not the same as "unspecified".
|
|
225
|
+
// No requestId: the library adds one to every AI request, so the request
|
|
226
|
+
// can be cancelled while it runs.
|
|
227
|
+
return {
|
|
228
|
+
messages,
|
|
229
|
+
system: options.system ?? "",
|
|
230
|
+
maxTokens: options.maxTokens ?? 0,
|
|
231
|
+
toolChoice: options.toolChoice ?? "",
|
|
232
|
+
tools: options.tools,
|
|
233
|
+
temperature: options.temperature,
|
|
234
|
+
topP: options.topP,
|
|
235
|
+
stopWords: options.stopWords,
|
|
236
|
+
responseFormat: options.responseFormat,
|
|
237
|
+
thinkingBudget: options.thinkingBudget,
|
|
238
|
+
effort: options.effort,
|
|
239
|
+
safetySettings: options.safetySettings,
|
|
240
|
+
extensions: options.extensions,
|
|
241
|
+
};
|
|
242
|
+
};
|
|
243
|
+
// =============================================================================
|
|
244
|
+
// AjanBridgeModel
|
|
245
|
+
// =============================================================================
|
|
246
|
+
export class AjanBridgeModel {
|
|
247
|
+
capabilities;
|
|
248
|
+
provider;
|
|
249
|
+
modelId;
|
|
250
|
+
#lib;
|
|
251
|
+
#modelHandle;
|
|
252
|
+
constructor(lib, modelHandle, provider, modelId, capabilities) {
|
|
253
|
+
this.#lib = lib;
|
|
254
|
+
this.#modelHandle = modelHandle;
|
|
255
|
+
this.provider = provider;
|
|
256
|
+
this.modelId = modelId;
|
|
257
|
+
this.capabilities = capabilities;
|
|
258
|
+
}
|
|
259
|
+
async generateText(options, signal) {
|
|
260
|
+
let resp;
|
|
261
|
+
try {
|
|
262
|
+
resp = await abortable(this.provider, signal, this.#lib.call("aiGenerateText", { modelHandle: this.#modelHandle, options: buildOptions(options) }, { signal }));
|
|
263
|
+
}
|
|
264
|
+
catch (error) {
|
|
265
|
+
if (error instanceof errors.AiError) {
|
|
266
|
+
return results.fail(error);
|
|
267
|
+
}
|
|
268
|
+
throw error;
|
|
269
|
+
}
|
|
270
|
+
if (resp.error !== undefined) {
|
|
271
|
+
// A cancelled request surfaces as a Go context error. Report the
|
|
272
|
+
// caller's own intent rather than the plumbing that carried it out.
|
|
273
|
+
return results.fail(signal?.aborted === true
|
|
274
|
+
? abortError(this.provider)
|
|
275
|
+
: wireError(this.provider, resp, "generation failed"));
|
|
276
|
+
}
|
|
277
|
+
return results.ok({
|
|
278
|
+
content: mapContent(resp.content),
|
|
279
|
+
stopReason: mapStopReason(resp.stopReason),
|
|
280
|
+
usage: mapUsage(resp.usage),
|
|
281
|
+
modelId: resp.modelId ?? this.modelId,
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
async *streamText(options, signal) {
|
|
285
|
+
// The open and every read run in the stream's own worker thread.
|
|
286
|
+
let stream;
|
|
287
|
+
try {
|
|
288
|
+
stream = await abortable(this.provider, signal, this.#lib.stream("aiTextStream", { modelHandle: this.#modelHandle, options: buildOptions(options) }, { signal }));
|
|
289
|
+
}
|
|
290
|
+
catch (error) {
|
|
291
|
+
if (error instanceof errors.AiError) {
|
|
292
|
+
yield { kind: "error", error };
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
throw error;
|
|
296
|
+
}
|
|
297
|
+
const streamResp = stream.opened;
|
|
298
|
+
if (streamResp.error !== undefined || streamResp.handle === undefined) {
|
|
299
|
+
yield {
|
|
300
|
+
kind: "error",
|
|
301
|
+
error: signal?.aborted === true
|
|
302
|
+
? abortError(this.provider)
|
|
303
|
+
: wireError(this.provider, streamResp, "stream start failed"),
|
|
304
|
+
};
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
try {
|
|
308
|
+
while (true) {
|
|
309
|
+
// Stop before asking for another event rather than after receiving one:
|
|
310
|
+
// a consumer that aborted does not want one more token, and the read
|
|
311
|
+
// below would otherwise wait on the provider for it.
|
|
312
|
+
if (signal?.aborted === true) {
|
|
313
|
+
yield { kind: "error", error: abortError(this.provider) };
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
// The bridge hands out one stream event per read, in order, and the
|
|
317
|
+
// abort check above must run before each read.
|
|
318
|
+
let event;
|
|
319
|
+
try {
|
|
320
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
321
|
+
event = await abortable(this.provider, signal, stream.read({ signal }));
|
|
322
|
+
}
|
|
323
|
+
catch (error) {
|
|
324
|
+
if (error instanceof errors.AiError) {
|
|
325
|
+
yield { kind: "error", error };
|
|
326
|
+
return;
|
|
327
|
+
}
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
if (event === null) {
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
switch (event.type) {
|
|
334
|
+
case "content_delta":
|
|
335
|
+
yield { kind: "content_delta", textDelta: event.textDelta ?? "" };
|
|
336
|
+
break;
|
|
337
|
+
case "tool_call_delta":
|
|
338
|
+
// The tool call arrives in its own fields. Go used to put the
|
|
339
|
+
// tool's NAME in textDelta, so a consumer concatenating deltas
|
|
340
|
+
// spliced tool names into the prose and never saw the arguments.
|
|
341
|
+
yield {
|
|
342
|
+
kind: "tool_call_delta",
|
|
343
|
+
textDelta: event.textDelta,
|
|
344
|
+
toolCall: event.toolCallId === undefined
|
|
345
|
+
? undefined
|
|
346
|
+
: {
|
|
347
|
+
id: event.toolCallId,
|
|
348
|
+
name: event.toolCallName ?? "",
|
|
349
|
+
arguments: event.argumentsDelta ?? {},
|
|
350
|
+
},
|
|
351
|
+
};
|
|
352
|
+
break;
|
|
353
|
+
case "thinking":
|
|
354
|
+
if (event.thinking !== undefined) {
|
|
355
|
+
yield { kind: "thinking", thinking: mapThinking(event.thinking) };
|
|
356
|
+
}
|
|
357
|
+
break;
|
|
358
|
+
case "message_done":
|
|
359
|
+
yield {
|
|
360
|
+
kind: "message_done",
|
|
361
|
+
stopReason: mapStopReason(event.stopReason),
|
|
362
|
+
usage: mapUsage(event.usage),
|
|
363
|
+
};
|
|
364
|
+
return;
|
|
365
|
+
case "error":
|
|
366
|
+
yield {
|
|
367
|
+
kind: "error",
|
|
368
|
+
error: wireError(this.provider, event, "stream error"),
|
|
369
|
+
};
|
|
370
|
+
return;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
finally {
|
|
375
|
+
// Closing the stream cancels the underlying context, drops the handle
|
|
376
|
+
// and ends the stream's worker.
|
|
377
|
+
stream.close();
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
async submitBatch(request, signal) {
|
|
381
|
+
const items = request.items.map((item) => ({
|
|
382
|
+
customId: item.customId,
|
|
383
|
+
options: buildOptions(item.options),
|
|
384
|
+
}));
|
|
385
|
+
const resp = await abortable(this.provider, signal, this.#lib.call("aiBatchCreate", {
|
|
386
|
+
modelHandle: this.#modelHandle,
|
|
387
|
+
items,
|
|
388
|
+
}, { signal }));
|
|
389
|
+
if (resp.error !== undefined || resp.job === undefined) {
|
|
390
|
+
throw signal?.aborted === true
|
|
391
|
+
? abortError(this.provider)
|
|
392
|
+
: wireError(this.provider, resp, "batch create failed");
|
|
393
|
+
}
|
|
394
|
+
return mapBatchJob(resp.job);
|
|
395
|
+
}
|
|
396
|
+
async getBatchJob(jobId, signal) {
|
|
397
|
+
const resp = await abortable(this.provider, signal, this.#lib.call("aiBatchGet", {
|
|
398
|
+
modelHandle: this.#modelHandle,
|
|
399
|
+
jobId,
|
|
400
|
+
}, { signal }));
|
|
401
|
+
if (resp.error !== undefined || resp.job === undefined) {
|
|
402
|
+
throw signal?.aborted === true
|
|
403
|
+
? abortError(this.provider)
|
|
404
|
+
: wireError(this.provider, resp, "batch get failed");
|
|
405
|
+
}
|
|
406
|
+
return mapBatchJob(resp.job);
|
|
407
|
+
}
|
|
408
|
+
async listBatchJobs(options, signal) {
|
|
409
|
+
const resp = await abortable(this.provider, signal, this.#lib.call("aiBatchList", {
|
|
410
|
+
modelHandle: this.#modelHandle,
|
|
411
|
+
limit: options?.limit,
|
|
412
|
+
after: options?.after,
|
|
413
|
+
}, { signal }));
|
|
414
|
+
if (resp.error !== undefined) {
|
|
415
|
+
throw signal?.aborted === true
|
|
416
|
+
? abortError(this.provider)
|
|
417
|
+
: wireError(this.provider, resp, "batch call failed");
|
|
418
|
+
}
|
|
419
|
+
return (resp.jobs ?? []).map(mapBatchJob);
|
|
420
|
+
}
|
|
421
|
+
async downloadBatchResults(job, signal) {
|
|
422
|
+
const bridgeJob = {
|
|
423
|
+
id: job.id,
|
|
424
|
+
status: job.status,
|
|
425
|
+
createdAt: job.createdAt.toISOString(),
|
|
426
|
+
completedAt: job.completedAt?.toISOString(),
|
|
427
|
+
totalCount: job.totalCount,
|
|
428
|
+
doneCount: job.doneCount,
|
|
429
|
+
failedCount: job.failedCount,
|
|
430
|
+
storage: job.storage,
|
|
431
|
+
error: job.error,
|
|
432
|
+
};
|
|
433
|
+
const resp = await abortable(this.provider, signal, this.#lib.call("aiBatchDownload", {
|
|
434
|
+
modelHandle: this.#modelHandle,
|
|
435
|
+
job: bridgeJob,
|
|
436
|
+
}, { signal }));
|
|
437
|
+
if (resp.error !== undefined) {
|
|
438
|
+
throw signal?.aborted === true
|
|
439
|
+
? abortError(this.provider)
|
|
440
|
+
: wireError(this.provider, resp, "batch call failed");
|
|
441
|
+
}
|
|
442
|
+
return (resp.results ?? []).map((item) => ({
|
|
443
|
+
customId: item.customId,
|
|
444
|
+
result: item.result !== undefined
|
|
445
|
+
? {
|
|
446
|
+
content: mapContent(item.result.content),
|
|
447
|
+
stopReason: mapStopReason(item.result.stopReason),
|
|
448
|
+
usage: mapUsage(item.result.usage),
|
|
449
|
+
modelId: item.result.modelId ?? this.modelId,
|
|
450
|
+
}
|
|
451
|
+
: undefined,
|
|
452
|
+
error: item.error,
|
|
453
|
+
}));
|
|
454
|
+
}
|
|
455
|
+
/**
|
|
456
|
+
* Asks the provider to cancel a batch job.
|
|
457
|
+
*
|
|
458
|
+
* Distinct from aborting this call. `signal` aborts the local request that
|
|
459
|
+
* asks for the cancellation; the cancellation itself is a durable change of
|
|
460
|
+
* state at the provider, and aborting the ask does not undo it if it already
|
|
461
|
+
* landed.
|
|
462
|
+
*/
|
|
463
|
+
async cancelBatchJob(jobId, signal) {
|
|
464
|
+
const resp = await abortable(this.provider, signal, this.#lib.call("aiBatchCancel", {
|
|
465
|
+
modelHandle: this.#modelHandle,
|
|
466
|
+
jobId,
|
|
467
|
+
}, { signal }));
|
|
468
|
+
if (resp.error !== undefined) {
|
|
469
|
+
throw signal?.aborted === true
|
|
470
|
+
? abortError(this.provider)
|
|
471
|
+
: wireError(this.provider, resp, "batch call failed");
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
close() {
|
|
475
|
+
this.#lib.call("aiCloseModel", this.#modelHandle);
|
|
476
|
+
return Promise.resolve();
|
|
477
|
+
}
|
|
478
|
+
[Symbol.asyncDispose]() {
|
|
479
|
+
return this.close();
|
|
480
|
+
}
|
|
481
|
+
[Symbol.dispose]() {
|
|
482
|
+
this.#lib.call("aiCloseModel", this.#modelHandle);
|
|
483
|
+
}
|
|
484
|
+
getRawClient() {
|
|
485
|
+
return null;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
// =============================================================================
|
|
489
|
+
// Factory
|
|
490
|
+
// =============================================================================
|
|
491
|
+
export class AjanBridgeModelFactory {
|
|
492
|
+
provider;
|
|
493
|
+
#lib;
|
|
494
|
+
#capabilities;
|
|
495
|
+
constructor(lib, provider, capabilities) {
|
|
496
|
+
this.#lib = lib;
|
|
497
|
+
this.provider = provider;
|
|
498
|
+
this.#capabilities = capabilities;
|
|
499
|
+
}
|
|
500
|
+
createModel(cfg) {
|
|
501
|
+
const resp = this.#lib.call("aiCreateModel", {
|
|
502
|
+
provider: cfg.provider,
|
|
503
|
+
apiKey: cfg.apiKey ?? "",
|
|
504
|
+
model: cfg.model,
|
|
505
|
+
baseUrl: cfg.baseUrl ?? "",
|
|
506
|
+
projectId: cfg.projectId ?? "",
|
|
507
|
+
location: cfg.location ?? "",
|
|
508
|
+
maxTokens: cfg.maxTokens,
|
|
509
|
+
temperature: cfg.temperature,
|
|
510
|
+
requestTimeoutMs: cfg.requestTimeoutMs,
|
|
511
|
+
properties: cfg.properties ?? {},
|
|
512
|
+
});
|
|
513
|
+
if (resp.error !== undefined || resp.handle === undefined) {
|
|
514
|
+
return Promise.reject(wireError(cfg.provider, resp, "failed to create model"));
|
|
515
|
+
}
|
|
516
|
+
return Promise.resolve(new AjanBridgeModel(this.#lib, resp.handle, cfg.provider, cfg.model, this.#capabilities));
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
// =============================================================================
|
|
520
|
+
// Bridge registry setup
|
|
521
|
+
// =============================================================================
|
|
522
|
+
/** Providers supported by the Go bridge and their default capabilities. */
|
|
523
|
+
// `vision` is declared only because image payloads genuinely cross the wire
|
|
524
|
+
// now. It was removed for a while: buildOptions used to reduce an image
|
|
525
|
+
// block to `{ type: "image" }` with no payload and Go had no field to receive
|
|
526
|
+
// one, so a caller who attached an image got a confident answer about nothing
|
|
527
|
+
// while the capability said that should work. The declaration follows the
|
|
528
|
+
// transport, in both directions.
|
|
529
|
+
//
|
|
530
|
+
// A bridge model is the Go model behind it, so each list matches what that
|
|
531
|
+
// model's GetCapabilities in pkg/ajan/aifx declares, and the TypeScript
|
|
532
|
+
// adapter of the same provider declares the same list. capabilities.test.ts
|
|
533
|
+
// checks all three.
|
|
534
|
+
const BRIDGE_PROVIDERS = [
|
|
535
|
+
{
|
|
536
|
+
provider: "anthropic",
|
|
537
|
+
capabilities: [
|
|
538
|
+
"text_generation",
|
|
539
|
+
"streaming",
|
|
540
|
+
"tool_calling",
|
|
541
|
+
"vision",
|
|
542
|
+
"batch_processing",
|
|
543
|
+
"structured_output",
|
|
544
|
+
"reasoning",
|
|
545
|
+
],
|
|
546
|
+
},
|
|
547
|
+
{
|
|
548
|
+
provider: "openai",
|
|
549
|
+
capabilities: [
|
|
550
|
+
"text_generation",
|
|
551
|
+
"streaming",
|
|
552
|
+
"tool_calling",
|
|
553
|
+
"vision",
|
|
554
|
+
"audio",
|
|
555
|
+
"batch_processing",
|
|
556
|
+
"structured_output",
|
|
557
|
+
"reasoning",
|
|
558
|
+
],
|
|
559
|
+
},
|
|
560
|
+
{
|
|
561
|
+
provider: "gemini",
|
|
562
|
+
capabilities: [
|
|
563
|
+
"text_generation",
|
|
564
|
+
"streaming",
|
|
565
|
+
"tool_calling",
|
|
566
|
+
"vision",
|
|
567
|
+
"audio",
|
|
568
|
+
"structured_output",
|
|
569
|
+
"reasoning",
|
|
570
|
+
],
|
|
571
|
+
},
|
|
572
|
+
{
|
|
573
|
+
// No batch_processing: the Go Vertex AI model has no batch methods.
|
|
574
|
+
provider: "vertexai",
|
|
575
|
+
capabilities: [
|
|
576
|
+
"text_generation",
|
|
577
|
+
"streaming",
|
|
578
|
+
"tool_calling",
|
|
579
|
+
"vision",
|
|
580
|
+
"audio",
|
|
581
|
+
"structured_output",
|
|
582
|
+
"reasoning",
|
|
583
|
+
],
|
|
584
|
+
},
|
|
585
|
+
{
|
|
586
|
+
provider: "ollama",
|
|
587
|
+
capabilities: ["text_generation", "streaming", "vision"],
|
|
588
|
+
},
|
|
589
|
+
{
|
|
590
|
+
provider: "claude-code",
|
|
591
|
+
capabilities: [
|
|
592
|
+
"text_generation",
|
|
593
|
+
"streaming",
|
|
594
|
+
"tool_calling",
|
|
595
|
+
"structured_output",
|
|
596
|
+
],
|
|
597
|
+
},
|
|
598
|
+
{
|
|
599
|
+
provider: "opencode",
|
|
600
|
+
capabilities: ["text_generation", "streaming"],
|
|
601
|
+
},
|
|
602
|
+
{
|
|
603
|
+
provider: "kiro",
|
|
604
|
+
capabilities: ["text_generation", "streaming"],
|
|
605
|
+
},
|
|
606
|
+
];
|
|
607
|
+
/**
|
|
608
|
+
* Creates AjanBridgeModelFactory instances for all supported providers.
|
|
609
|
+
*
|
|
610
|
+
* Every provider the bridge knows, unconditionally — there is nothing that can
|
|
611
|
+
* be independently absent, because every backend is Go code inside the very
|
|
612
|
+
* library that just loaded. The vendor CLI that claude-code, kiro and opencode
|
|
613
|
+
* each drive (claude, kiro, opencode) must still be installed, but that is a
|
|
614
|
+
* spawn-time failure with its own message, not something a PATH probe can
|
|
615
|
+
* predict.
|
|
616
|
+
*/
|
|
617
|
+
export const createBridgeFactories = (lib) => BRIDGE_PROVIDERS.map(({ provider, capabilities }) => new AjanBridgeModelFactory(lib, provider, capabilities));
|
|
618
|
+
/**
|
|
619
|
+
* Attempts to load the ajan FFI library and returns bridge factories.
|
|
620
|
+
* Returns an empty array on failure (FFI not available).
|
|
621
|
+
*/
|
|
622
|
+
/** Set when the bridge declines to load; read through getBridgeLoadError. */
|
|
623
|
+
let bridgeLoadError = null;
|
|
624
|
+
export const tryLoadBridgeFactories = async () => {
|
|
625
|
+
try {
|
|
626
|
+
const ffiMod = await import("@eserstack/ajan/ffi");
|
|
627
|
+
// Native only. loadEserAjan otherwise falls back to WASM, which LOADS
|
|
628
|
+
// successfully and then fails every AI call — so the bridge factories would
|
|
629
|
+
// register, displace the pure-TS adapters that do work, and turn a missing
|
|
630
|
+
// native binary into a provider that is present and broken rather than
|
|
631
|
+
// absent. Refusing here lets defaultFactories keep the TS adapter for that
|
|
632
|
+
// provider, which is the whole point of the per-provider merge.
|
|
633
|
+
const lib = await ffiMod.loadEserAjan({ wasm: false });
|
|
634
|
+
lib.call("init");
|
|
635
|
+
// No availability probe. claude-code/opencode/kiro are Go code linked into
|
|
636
|
+
// the library that just loaded, so if the library is here the backends are
|
|
637
|
+
// here. The vendor CLI each one drives still has to be installed, but that
|
|
638
|
+
// is a spawn-time failure with its own message, not something a PATH lookup
|
|
639
|
+
// can predict.
|
|
640
|
+
bridgeLoadError = null;
|
|
641
|
+
return createBridgeFactories(lib);
|
|
642
|
+
}
|
|
643
|
+
catch (cause) {
|
|
644
|
+
// Record rather than discard. Falling back to the pure-TS adapters is
|
|
645
|
+
// correct, but it silently changes which implementation answers every AI
|
|
646
|
+
// call, and the reason -- no native binary for this platform, an FFI
|
|
647
|
+
// permission that was not granted, a library that failed to open -- was
|
|
648
|
+
// thrown away here. `ai doctor`-style diagnostics and bug reports had
|
|
649
|
+
// nothing to go on beyond "it used the TS path".
|
|
650
|
+
bridgeLoadError = cause instanceof Error ? cause : new Error(String(cause));
|
|
651
|
+
return [];
|
|
652
|
+
}
|
|
653
|
+
};
|
|
654
|
+
/**
|
|
655
|
+
* Why the Go bridge is not in use, or `null` when it loaded or was never tried.
|
|
656
|
+
*
|
|
657
|
+
* The underlying loader's own diagnostic (the checked paths, the native-vs-WASM
|
|
658
|
+
* cause) is preserved as this error's `cause`, so a caller can report the whole
|
|
659
|
+
* chain rather than just the last frame.
|
|
660
|
+
*/
|
|
661
|
+
export const getBridgeLoadError = () => bridgeLoadError;
|
|
@@ -0,0 +1,25 @@
|
|
|
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 * as generation from "../generation.ts";
|
|
5
|
+
import type * as batch from "../batch.ts";
|
|
6
|
+
import type * as model from "../model.ts";
|
|
7
|
+
import * as errors from "../errors.ts";
|
|
8
|
+
export declare class AnthropicModel implements model.LanguageModel, model.BatchCapableModel {
|
|
9
|
+
readonly capabilities: readonly types.ProviderCapability[];
|
|
10
|
+
readonly provider = "anthropic";
|
|
11
|
+
readonly modelId: string;
|
|
12
|
+
private readonly client;
|
|
13
|
+
private readonly config;
|
|
14
|
+
constructor(client: any, cfg: config.ResolvedConfigTarget);
|
|
15
|
+
generateText(options: generation.GenerateTextOptions, signal?: AbortSignal): Promise<results.Result<generation.GenerateTextResult, errors.AiError>>;
|
|
16
|
+
streamText(options: generation.GenerateTextOptions, signal?: AbortSignal): AsyncIterable<generation.StreamEvent>;
|
|
17
|
+
submitBatch(request: batch.BatchRequest, signal?: AbortSignal): Promise<batch.BatchJob>;
|
|
18
|
+
getBatchJob(jobId: string, signal?: AbortSignal): Promise<batch.BatchJob>;
|
|
19
|
+
listBatchJobs(options?: batch.ListBatchOptions, signal?: AbortSignal): Promise<readonly batch.BatchJob[]>;
|
|
20
|
+
downloadBatchResults(job: batch.BatchJob, signal?: AbortSignal): Promise<readonly batch.BatchResult[]>;
|
|
21
|
+
cancelBatchJob(jobId: string, signal?: AbortSignal): Promise<void>;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
getRawClient(): unknown;
|
|
24
|
+
}
|
|
25
|
+
export declare const anthropicFactory: model.ProviderFactory;
|