@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,424 @@
|
|
|
1
|
+
// Copyright 2023-present Eser Ozvataf and other contributors. All rights reserved. Apache-2.0 license.
|
|
2
|
+
import * as generation from "../generation.js";
|
|
3
|
+
import * as content from "../content.js";
|
|
4
|
+
import * as errors from "../errors.js";
|
|
5
|
+
// =============================================================================
|
|
6
|
+
// Role Mapping
|
|
7
|
+
// =============================================================================
|
|
8
|
+
export const mapRoleToGenAI = (role) => {
|
|
9
|
+
if (role === "assistant") {
|
|
10
|
+
return "model";
|
|
11
|
+
}
|
|
12
|
+
if (role === "tool") {
|
|
13
|
+
return "user";
|
|
14
|
+
}
|
|
15
|
+
// "user" stays "user", "system" is handled separately
|
|
16
|
+
return role;
|
|
17
|
+
};
|
|
18
|
+
// =============================================================================
|
|
19
|
+
// Content Block Mapping → genai Parts
|
|
20
|
+
// =============================================================================
|
|
21
|
+
export const mapContentBlockToGenAIPart = (block) => {
|
|
22
|
+
switch (block.kind) {
|
|
23
|
+
case "text": {
|
|
24
|
+
return { text: block.text };
|
|
25
|
+
}
|
|
26
|
+
case "image": {
|
|
27
|
+
if (block.image.data !== undefined) {
|
|
28
|
+
return {
|
|
29
|
+
inlineData: {
|
|
30
|
+
mimeType: block.image.mimeType ?? "image/png",
|
|
31
|
+
data: encodeBase64(block.image.data),
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
if (block.image.url !== undefined) {
|
|
36
|
+
if (content.isDataUrl(block.image.url)) {
|
|
37
|
+
const decoded = content.decodeDataUrl(block.image.url);
|
|
38
|
+
if (decoded !== null) {
|
|
39
|
+
return {
|
|
40
|
+
inlineData: {
|
|
41
|
+
mimeType: decoded.mimeType,
|
|
42
|
+
data: encodeBase64(decoded.data),
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
fileData: {
|
|
49
|
+
mimeType: block.image.mimeType ??
|
|
50
|
+
content.detectMimeFromUrl(block.image.url) ??
|
|
51
|
+
"image/png",
|
|
52
|
+
fileUri: block.image.url,
|
|
53
|
+
},
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
return { text: "[unsupported image]" };
|
|
57
|
+
}
|
|
58
|
+
case "audio": {
|
|
59
|
+
if (block.audio.data !== undefined) {
|
|
60
|
+
return {
|
|
61
|
+
inlineData: {
|
|
62
|
+
mimeType: block.audio.mimeType ?? "audio/mpeg",
|
|
63
|
+
data: encodeBase64(block.audio.data),
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
if (block.audio.url !== undefined) {
|
|
68
|
+
return {
|
|
69
|
+
fileData: {
|
|
70
|
+
mimeType: block.audio.mimeType ??
|
|
71
|
+
content.detectMimeFromUrl(block.audio.url) ??
|
|
72
|
+
"audio/mpeg",
|
|
73
|
+
fileUri: block.audio.url,
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return { text: "[unsupported audio]" };
|
|
78
|
+
}
|
|
79
|
+
case "file": {
|
|
80
|
+
return {
|
|
81
|
+
fileData: {
|
|
82
|
+
mimeType: block.file.mimeType ?? "application/octet-stream",
|
|
83
|
+
fileUri: block.file.uri,
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
case "tool_call": {
|
|
88
|
+
return {
|
|
89
|
+
functionCall: {
|
|
90
|
+
name: block.toolCall.name,
|
|
91
|
+
args: block.toolCall.arguments,
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
case "tool_result": {
|
|
96
|
+
return {
|
|
97
|
+
functionResponse: {
|
|
98
|
+
name: block.toolResult.toolCallId,
|
|
99
|
+
response: { content: block.toolResult.content },
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
case "thinking": {
|
|
104
|
+
return { text: block.thinking.text, thought: true };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
// =============================================================================
|
|
109
|
+
// Message Mapping
|
|
110
|
+
// =============================================================================
|
|
111
|
+
export const mapMessagesToGenAI = (messages) => {
|
|
112
|
+
let systemInstruction = null;
|
|
113
|
+
const contents = [];
|
|
114
|
+
for (const message of messages) {
|
|
115
|
+
if (message.role === "system") {
|
|
116
|
+
const textParts = [];
|
|
117
|
+
for (const block of message.content) {
|
|
118
|
+
if (block.kind === "text") {
|
|
119
|
+
textParts.push(block.text);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
systemInstruction = textParts.join("\n");
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
// Thinking blocks are left out: they come from a provider whose signature
|
|
126
|
+
// Gemini cannot check, and Gemini replays its own reasoning differently.
|
|
127
|
+
const parts = message.content
|
|
128
|
+
.filter((block) => block.kind !== "thinking")
|
|
129
|
+
.map(mapContentBlockToGenAIPart);
|
|
130
|
+
contents.push({
|
|
131
|
+
role: mapRoleToGenAI(message.role),
|
|
132
|
+
parts,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
return { contents, systemInstruction };
|
|
136
|
+
};
|
|
137
|
+
// =============================================================================
|
|
138
|
+
// Tool Definition Mapping
|
|
139
|
+
// =============================================================================
|
|
140
|
+
export const mapToolsToGenAI = (tools) => {
|
|
141
|
+
return tools.map((tool) => ({
|
|
142
|
+
functionDeclarations: [
|
|
143
|
+
{
|
|
144
|
+
name: tool.name,
|
|
145
|
+
description: tool.description,
|
|
146
|
+
parameters: tool.parameters,
|
|
147
|
+
},
|
|
148
|
+
],
|
|
149
|
+
}));
|
|
150
|
+
};
|
|
151
|
+
/**
|
|
152
|
+
* Maps an effort level to a Gemini thinking level. Gemini's levels stop at
|
|
153
|
+
* `HIGH`, so `"xhigh"` and `"max"` send `HIGH`. The levels a model takes differ
|
|
154
|
+
* per model (gemini-3-pro-preview has no `MEDIUM`) and are not checked here;
|
|
155
|
+
* the API answers for the model, as on `openai`.
|
|
156
|
+
*
|
|
157
|
+
* @throws {errors.BadRequestError} when `effort` is not one of the five
|
|
158
|
+
* levels, which a JavaScript caller can pass.
|
|
159
|
+
*/
|
|
160
|
+
export const mapEffortToThinkingLevel = (effort, provider) => {
|
|
161
|
+
switch (effort) {
|
|
162
|
+
case "low":
|
|
163
|
+
return "LOW";
|
|
164
|
+
case "medium":
|
|
165
|
+
return "MEDIUM";
|
|
166
|
+
case "high":
|
|
167
|
+
case "xhigh":
|
|
168
|
+
case "max":
|
|
169
|
+
return "HIGH";
|
|
170
|
+
default:
|
|
171
|
+
throw new errors.BadRequestError(`unknown effort level ${JSON.stringify(effort)}; use low, medium, high, xhigh or max, or leave it unset for the provider default`, { provider });
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Builds the genai `generateContent` request of `gemini` and `vertexai`. The
|
|
176
|
+
* SDK reads the system instruction, tools, safety settings and abort signal
|
|
177
|
+
* from `config` only, and drops a field set next to it without an error.
|
|
178
|
+
*
|
|
179
|
+
* An explicit `effort` is sent as `thinkingConfig.thinkingLevel` and wins over
|
|
180
|
+
* `thinkingBudget`, since the API rejects a request that sets both.
|
|
181
|
+
*
|
|
182
|
+
* @throws {errors.BadRequestError} when a `json_schema` response format has no
|
|
183
|
+
* object schema, or `effort` is not a level.
|
|
184
|
+
*/
|
|
185
|
+
export const buildGenAIRequest = (cfg, options, provider, signal) => {
|
|
186
|
+
const { contents, systemInstruction } = mapMessagesToGenAI(options.messages);
|
|
187
|
+
const requestConfig = {
|
|
188
|
+
maxOutputTokens: options.maxTokens ?? cfg.maxTokens,
|
|
189
|
+
};
|
|
190
|
+
if (options.temperature !== undefined) {
|
|
191
|
+
requestConfig["temperature"] = options.temperature;
|
|
192
|
+
}
|
|
193
|
+
if (options.topP !== undefined) {
|
|
194
|
+
requestConfig["topP"] = options.topP;
|
|
195
|
+
}
|
|
196
|
+
if (options.stopWords !== undefined) {
|
|
197
|
+
requestConfig["stopSequences"] = options.stopWords;
|
|
198
|
+
}
|
|
199
|
+
// As in Go: json_schema is sent as responseJsonSchema, which takes the
|
|
200
|
+
// schema as is (the API takes one of it and responseSchema), json_object as
|
|
201
|
+
// the JSON MIME type alone, and text as the plain text MIME type.
|
|
202
|
+
switch (options.responseFormat?.type) {
|
|
203
|
+
case "json_schema":
|
|
204
|
+
requestConfig["responseMimeType"] = "application/json";
|
|
205
|
+
requestConfig["responseJsonSchema"] = generation.requireJsonSchema(options.responseFormat, provider);
|
|
206
|
+
break;
|
|
207
|
+
case "json_object":
|
|
208
|
+
requestConfig["responseMimeType"] = "application/json";
|
|
209
|
+
break;
|
|
210
|
+
case "text":
|
|
211
|
+
requestConfig["responseMimeType"] = "text/plain";
|
|
212
|
+
break;
|
|
213
|
+
}
|
|
214
|
+
if (options.effort !== undefined) {
|
|
215
|
+
requestConfig["thinkingConfig"] = {
|
|
216
|
+
thinkingLevel: mapEffortToThinkingLevel(options.effort, provider),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
else if (options.thinkingBudget !== undefined) {
|
|
220
|
+
requestConfig["thinkingConfig"] = {
|
|
221
|
+
thinkingBudget: options.thinkingBudget,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
const system = options.system ?? systemInstruction;
|
|
225
|
+
if (system !== null) {
|
|
226
|
+
requestConfig["systemInstruction"] = system;
|
|
227
|
+
}
|
|
228
|
+
if (options.tools !== undefined) {
|
|
229
|
+
requestConfig["tools"] = mapToolsToGenAI(options.tools);
|
|
230
|
+
}
|
|
231
|
+
if (options.safetySettings !== undefined) {
|
|
232
|
+
requestConfig["safetySettings"] = options.safetySettings;
|
|
233
|
+
}
|
|
234
|
+
if (signal !== undefined) {
|
|
235
|
+
requestConfig["abortSignal"] = signal;
|
|
236
|
+
}
|
|
237
|
+
return { contents, config: requestConfig };
|
|
238
|
+
};
|
|
239
|
+
// =============================================================================
|
|
240
|
+
// Response Mapping
|
|
241
|
+
// =============================================================================
|
|
242
|
+
/**
|
|
243
|
+
* The finish reasons of the content filters: safety, blocklist, prohibited
|
|
244
|
+
* content, personal data, recitation and their image variants. Each one
|
|
245
|
+
* withholds the answer, so it maps to `"refusal"`.
|
|
246
|
+
*/
|
|
247
|
+
const FILTER_FINISH_REASONS = new Set([
|
|
248
|
+
"SAFETY",
|
|
249
|
+
"BLOCKLIST",
|
|
250
|
+
"PROHIBITED_CONTENT",
|
|
251
|
+
"SPII",
|
|
252
|
+
"RECITATION",
|
|
253
|
+
"IMAGE_SAFETY",
|
|
254
|
+
"IMAGE_PROHIBITED_CONTENT",
|
|
255
|
+
"IMAGE_RECITATION",
|
|
256
|
+
]);
|
|
257
|
+
/**
|
|
258
|
+
* Maps a genai finishReason. Gemini ends a turn that called a tool with
|
|
259
|
+
* `STOP`, so `calledTool` turns that into `"tool_use"`. Any other reason has no
|
|
260
|
+
* closer match than `"stop"`.
|
|
261
|
+
*/
|
|
262
|
+
export const mapGenAIFinishReason = (finishReason, calledTool) => {
|
|
263
|
+
if (finishReason === "STOP") {
|
|
264
|
+
return calledTool ? "tool_use" : "end_turn";
|
|
265
|
+
}
|
|
266
|
+
if (finishReason === "MAX_TOKENS") {
|
|
267
|
+
return "max_tokens";
|
|
268
|
+
}
|
|
269
|
+
if (FILTER_FINISH_REASONS.has(finishReason)) {
|
|
270
|
+
return "refusal";
|
|
271
|
+
}
|
|
272
|
+
return "stop";
|
|
273
|
+
};
|
|
274
|
+
/**
|
|
275
|
+
* Reports whether a filter blocked the prompt itself. The response then has no
|
|
276
|
+
* candidates and names the filter in `promptFeedback.blockReason`.
|
|
277
|
+
*/
|
|
278
|
+
export const isGenAIPromptBlocked = (
|
|
279
|
+
// The response is untyped vendor JSON; only the mapped fields are read.
|
|
280
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
281
|
+
response) => {
|
|
282
|
+
const reason = response?.promptFeedback?.blockReason;
|
|
283
|
+
return (typeof reason === "string" &&
|
|
284
|
+
reason !== "" &&
|
|
285
|
+
reason !== "BLOCKED_REASON_UNSPECIFIED");
|
|
286
|
+
};
|
|
287
|
+
/** Maps a genai part's function call to a tool call. */
|
|
288
|
+
const mapFunctionCall = (
|
|
289
|
+
// The call is untyped vendor JSON; only the mapped fields are read.
|
|
290
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
291
|
+
functionCall) => ({
|
|
292
|
+
id: functionCall.name,
|
|
293
|
+
name: functionCall.name,
|
|
294
|
+
arguments: functionCall.args ?? {},
|
|
295
|
+
});
|
|
296
|
+
export const mapGenAIResponseToResult = (
|
|
297
|
+
// The response is untyped vendor JSON; only the mapped fields are read.
|
|
298
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
299
|
+
response, modelId) => {
|
|
300
|
+
const contentBlocks = [];
|
|
301
|
+
let stopReason = "end_turn";
|
|
302
|
+
const candidate = response?.candidates?.[0];
|
|
303
|
+
if (candidate === undefined || candidate === null) {
|
|
304
|
+
// A blocked prompt gets no candidates.
|
|
305
|
+
if (isGenAIPromptBlocked(response)) {
|
|
306
|
+
stopReason = "refusal";
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
else {
|
|
310
|
+
let calledTool = false;
|
|
311
|
+
for (const part of candidate.content?.parts ?? []) {
|
|
312
|
+
if (part.text !== undefined) {
|
|
313
|
+
contentBlocks.push({ kind: "text", text: part.text });
|
|
314
|
+
}
|
|
315
|
+
if (part.functionCall !== undefined) {
|
|
316
|
+
contentBlocks.push({
|
|
317
|
+
kind: "tool_call",
|
|
318
|
+
toolCall: mapFunctionCall(part.functionCall),
|
|
319
|
+
});
|
|
320
|
+
calledTool = true;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
const finishReason = candidate.finishReason;
|
|
324
|
+
if (typeof finishReason === "string") {
|
|
325
|
+
stopReason = mapGenAIFinishReason(finishReason, calledTool);
|
|
326
|
+
}
|
|
327
|
+
else if (calledTool) {
|
|
328
|
+
stopReason = "tool_use";
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const usageMetadata = response?.usageMetadata;
|
|
332
|
+
return {
|
|
333
|
+
content: contentBlocks,
|
|
334
|
+
stopReason,
|
|
335
|
+
usage: {
|
|
336
|
+
inputTokens: usageMetadata?.promptTokenCount ?? 0,
|
|
337
|
+
outputTokens: usageMetadata?.candidatesTokenCount ?? 0,
|
|
338
|
+
totalTokens: usageMetadata?.totalTokenCount ?? 0,
|
|
339
|
+
},
|
|
340
|
+
modelId,
|
|
341
|
+
rawResponse: response,
|
|
342
|
+
};
|
|
343
|
+
};
|
|
344
|
+
/**
|
|
345
|
+
* Maps the chunks of a genai stream to stream events. A tool call is
|
|
346
|
+
* remembered across chunks, since the chunk that ends the turn need not be the
|
|
347
|
+
* one with the call. That chunk yields `message_done` even without content: a
|
|
348
|
+
* filtered answer can arrive as a finish reason with no parts, and a blocked
|
|
349
|
+
* prompt as a chunk with no candidates.
|
|
350
|
+
*/
|
|
351
|
+
export async function* mapGenAIStream(
|
|
352
|
+
// The chunks are untyped vendor JSON; only the mapped fields are read.
|
|
353
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
354
|
+
stream) {
|
|
355
|
+
let inputTokens = 0;
|
|
356
|
+
let outputTokens = 0;
|
|
357
|
+
let calledTool = false;
|
|
358
|
+
for await (const chunk of stream) {
|
|
359
|
+
const candidate = chunk?.candidates?.[0];
|
|
360
|
+
let stopReason = null;
|
|
361
|
+
if (candidate === undefined || candidate === null) {
|
|
362
|
+
if (isGenAIPromptBlocked(chunk)) {
|
|
363
|
+
stopReason = "refusal";
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
for (const part of candidate.content?.parts ?? []) {
|
|
368
|
+
if (part.text !== undefined) {
|
|
369
|
+
yield { kind: "content_delta", textDelta: part.text };
|
|
370
|
+
}
|
|
371
|
+
if (part.functionCall !== undefined) {
|
|
372
|
+
calledTool = true;
|
|
373
|
+
yield {
|
|
374
|
+
kind: "tool_call_delta",
|
|
375
|
+
toolCall: mapFunctionCall(part.functionCall),
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
const finishReason = candidate.finishReason;
|
|
380
|
+
if (typeof finishReason === "string") {
|
|
381
|
+
stopReason = mapGenAIFinishReason(finishReason, calledTool);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
if (stopReason !== null) {
|
|
385
|
+
const usageMetadata = chunk?.usageMetadata;
|
|
386
|
+
inputTokens = usageMetadata?.promptTokenCount ?? inputTokens;
|
|
387
|
+
outputTokens = usageMetadata?.candidatesTokenCount ?? outputTokens;
|
|
388
|
+
yield {
|
|
389
|
+
kind: "message_done",
|
|
390
|
+
stopReason,
|
|
391
|
+
usage: {
|
|
392
|
+
inputTokens,
|
|
393
|
+
outputTokens,
|
|
394
|
+
totalTokens: inputTokens + outputTokens,
|
|
395
|
+
},
|
|
396
|
+
};
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
// =============================================================================
|
|
401
|
+
// Error Classification
|
|
402
|
+
// =============================================================================
|
|
403
|
+
export const classifyGenAIError = (
|
|
404
|
+
// SDK errors carry status or code fields with no exported type.
|
|
405
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
406
|
+
error) => {
|
|
407
|
+
if (error?.status !== undefined) {
|
|
408
|
+
return error.status;
|
|
409
|
+
}
|
|
410
|
+
if (error?.code !== undefined) {
|
|
411
|
+
return error.code;
|
|
412
|
+
}
|
|
413
|
+
return 500;
|
|
414
|
+
};
|
|
415
|
+
// =============================================================================
|
|
416
|
+
// Helpers
|
|
417
|
+
// =============================================================================
|
|
418
|
+
const encodeBase64 = (data) => {
|
|
419
|
+
let binary = "";
|
|
420
|
+
for (let i = 0; i < data.length; i++) {
|
|
421
|
+
binary += String.fromCharCode(data[i]);
|
|
422
|
+
}
|
|
423
|
+
return btoa(binary);
|
|
424
|
+
};
|
|
@@ -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 KiroModel implements model.LanguageModel {
|
|
8
|
+
readonly capabilities: readonly types.ProviderCapability[];
|
|
9
|
+
readonly provider = "kiro";
|
|
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 kiroFactory: model.ProviderFactory;
|
|
@@ -0,0 +1,195 @@
|
|
|
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 errors from "../errors.js";
|
|
4
|
+
import * as cliShared from "./cli-shared.js";
|
|
5
|
+
// =============================================================================
|
|
6
|
+
// Kiro Model
|
|
7
|
+
// =============================================================================
|
|
8
|
+
export class KiroModel {
|
|
9
|
+
capabilities = [
|
|
10
|
+
"text_generation",
|
|
11
|
+
"streaming",
|
|
12
|
+
];
|
|
13
|
+
provider = "kiro";
|
|
14
|
+
modelId;
|
|
15
|
+
binary;
|
|
16
|
+
config;
|
|
17
|
+
constructor(binary, cfg) {
|
|
18
|
+
this.binary = binary;
|
|
19
|
+
this.config = cfg;
|
|
20
|
+
this.modelId = cfg.model;
|
|
21
|
+
}
|
|
22
|
+
async generateText(options, signal) {
|
|
23
|
+
const args = buildArgs(this.config, options);
|
|
24
|
+
const prompt = cliShared.formatMessagesAsText(options.messages, options.system);
|
|
25
|
+
const process = cliShared.spawnCliProcess(this.binary, args, {
|
|
26
|
+
signal,
|
|
27
|
+
stdinData: prompt,
|
|
28
|
+
});
|
|
29
|
+
const stderrPromise = cliShared.captureStderr(process.stderr);
|
|
30
|
+
try {
|
|
31
|
+
const result = await cliShared.parseTextOutput(process.stdout, this.modelId);
|
|
32
|
+
const exit = await process.waitForExit();
|
|
33
|
+
const stderr = await stderrPromise;
|
|
34
|
+
const exitError = cliShared.classifyExitCode("kiro", exit.code, stderr);
|
|
35
|
+
if (exitError !== null) {
|
|
36
|
+
return results.fail(exitError);
|
|
37
|
+
}
|
|
38
|
+
return results.ok(result);
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
await cliShared.stopCliProcess(process, stderrPromise);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
async *streamText(options, signal) {
|
|
45
|
+
const args = buildArgs(this.config, options);
|
|
46
|
+
const prompt = cliShared.formatMessagesAsText(options.messages, options.system);
|
|
47
|
+
const process = cliShared.spawnCliProcess(this.binary, args, {
|
|
48
|
+
signal,
|
|
49
|
+
stdinData: prompt,
|
|
50
|
+
});
|
|
51
|
+
const stderrPromise = cliShared.captureStderr(process.stderr);
|
|
52
|
+
try {
|
|
53
|
+
// Try parsing as JSONL first; fall back to plain text streaming
|
|
54
|
+
const decoder = new TextDecoder();
|
|
55
|
+
const reader = process.stdout.getReader();
|
|
56
|
+
let buffer = "";
|
|
57
|
+
try {
|
|
58
|
+
let readResult = await reader.read();
|
|
59
|
+
while (readResult.done !== true) {
|
|
60
|
+
buffer += decoder.decode(readResult.value, { stream: true });
|
|
61
|
+
const lines = buffer.split("\n");
|
|
62
|
+
buffer = lines.pop() ?? "";
|
|
63
|
+
for (const line of lines) {
|
|
64
|
+
const trimmed = line.trim();
|
|
65
|
+
if (trimmed.length === 0) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
// Try JSON parse for structured output
|
|
69
|
+
try {
|
|
70
|
+
const parsed = JSON.parse(trimmed);
|
|
71
|
+
const streamEvent = mapStreamEvent(parsed);
|
|
72
|
+
if (streamEvent !== null) {
|
|
73
|
+
yield streamEvent;
|
|
74
|
+
continue;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
// Not JSON — emit as text delta
|
|
79
|
+
}
|
|
80
|
+
yield { kind: "content_delta", textDelta: trimmed + "\n" };
|
|
81
|
+
}
|
|
82
|
+
// Reads stay sequential: each chunk extends the line buffer, and the
|
|
83
|
+
// consumer pulls events one at a time.
|
|
84
|
+
// oxlint-disable-next-line no-await-in-loop
|
|
85
|
+
readResult = await reader.read();
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
finally {
|
|
89
|
+
reader.releaseLock();
|
|
90
|
+
}
|
|
91
|
+
// Flush remaining buffer
|
|
92
|
+
if (buffer.trim().length > 0) {
|
|
93
|
+
yield { kind: "content_delta", textDelta: buffer.trim() };
|
|
94
|
+
}
|
|
95
|
+
const exit = await process.waitForExit();
|
|
96
|
+
const stderr = await stderrPromise;
|
|
97
|
+
const exitError = cliShared.classifyExitCode("kiro", exit.code, stderr);
|
|
98
|
+
if (exitError !== null) {
|
|
99
|
+
yield { kind: "error", error: exitError };
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
yield {
|
|
103
|
+
kind: "message_done",
|
|
104
|
+
stopReason: "end_turn",
|
|
105
|
+
usage: { inputTokens: 0, outputTokens: 0, totalTokens: 0 },
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
catch (err) {
|
|
110
|
+
if (err instanceof errors.AiError) {
|
|
111
|
+
yield { kind: "error", error: err };
|
|
112
|
+
}
|
|
113
|
+
else {
|
|
114
|
+
yield {
|
|
115
|
+
kind: "error",
|
|
116
|
+
error: new errors.AiError(err instanceof Error ? err.message : String(err), { provider: "kiro", cause: err instanceof Error ? err : undefined }),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
// Also reached when the consumer stops reading early.
|
|
122
|
+
await cliShared.stopCliProcess(process, stderrPromise);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
close() {
|
|
126
|
+
return Promise.resolve();
|
|
127
|
+
}
|
|
128
|
+
getRawClient() {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// =============================================================================
|
|
133
|
+
// Factory
|
|
134
|
+
// =============================================================================
|
|
135
|
+
export const kiroFactory = {
|
|
136
|
+
provider: "kiro",
|
|
137
|
+
createModel: async (cfg) => {
|
|
138
|
+
const binary = await cliShared.resolveBinary("kiro", cfg);
|
|
139
|
+
return new KiroModel(binary, cfg);
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
// =============================================================================
|
|
143
|
+
// Internal Helpers
|
|
144
|
+
// =============================================================================
|
|
145
|
+
/** Builds the CLI arguments. options.effort is ignored: no effort flag is sent. */
|
|
146
|
+
const buildArgs = (cfg, options) => {
|
|
147
|
+
const args = [];
|
|
148
|
+
args.push("--output", "json");
|
|
149
|
+
args.push("--model", cfg.model);
|
|
150
|
+
if (options.maxTokens !== undefined) {
|
|
151
|
+
args.push("--max-tokens", String(options.maxTokens));
|
|
152
|
+
}
|
|
153
|
+
// Pass through additional CLI flags from properties
|
|
154
|
+
const extraArgs = cfg.properties?.["args"];
|
|
155
|
+
if (extraArgs !== undefined) {
|
|
156
|
+
args.push(...extraArgs);
|
|
157
|
+
}
|
|
158
|
+
return args;
|
|
159
|
+
};
|
|
160
|
+
const mapStreamEvent = (event) => {
|
|
161
|
+
if (event === null || typeof event !== "object") {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
// Stream events are untyped JSON; each field is checked before use.
|
|
165
|
+
// oxlint-disable-next-line typescript/no-explicit-any
|
|
166
|
+
const obj = event;
|
|
167
|
+
// Content
|
|
168
|
+
if (obj.type === "content" || obj.type === "text") {
|
|
169
|
+
const text = obj.text ?? obj.content;
|
|
170
|
+
if (text !== undefined) {
|
|
171
|
+
return { kind: "content_delta", textDelta: String(text) };
|
|
172
|
+
}
|
|
173
|
+
return null;
|
|
174
|
+
}
|
|
175
|
+
// Done
|
|
176
|
+
if (obj.type === "done" || obj.type === "result" || obj.done === true) {
|
|
177
|
+
return {
|
|
178
|
+
kind: "message_done",
|
|
179
|
+
stopReason: "end_turn",
|
|
180
|
+
usage: {
|
|
181
|
+
inputTokens: obj.usage?.input_tokens ?? 0,
|
|
182
|
+
outputTokens: obj.usage?.output_tokens ?? 0,
|
|
183
|
+
totalTokens: (obj.usage?.input_tokens ?? 0) + (obj.usage?.output_tokens ?? 0),
|
|
184
|
+
},
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
// Error
|
|
188
|
+
if (obj.type === "error") {
|
|
189
|
+
return {
|
|
190
|
+
kind: "error",
|
|
191
|
+
error: new errors.AiError(obj.error?.message ?? obj.message ?? "Unknown Kiro error", { provider: "kiro" }),
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
return null;
|
|
195
|
+
};
|