@sandagent/sdk 0.2.0-beta.5
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 +201 -0
- package/dist/index.d.ts +158 -0
- package/dist/index.js +631 -0
- package/dist/index.js.map +1 -0
- package/dist/react/index.d.ts +437 -0
- package/dist/react/index.js +421 -0
- package/dist/react/index.js.map +1 -0
- package/package.json +67 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
// src/provider/sandagent-provider.ts
|
|
2
|
+
import { NoSuchModelError } from "@ai-sdk/provider";
|
|
3
|
+
|
|
4
|
+
// src/provider/sandagent-language-model.ts
|
|
5
|
+
import { SandAgent } from "@sandagent/manager";
|
|
6
|
+
|
|
7
|
+
// src/provider/types.ts
|
|
8
|
+
function resolveModelId(modelId) {
|
|
9
|
+
switch (modelId) {
|
|
10
|
+
case "sonnet":
|
|
11
|
+
return "claude-sonnet-4-20250514";
|
|
12
|
+
case "opus":
|
|
13
|
+
return "claude-opus-4-20250514";
|
|
14
|
+
case "haiku":
|
|
15
|
+
return "claude-3-5-haiku-20241022";
|
|
16
|
+
default:
|
|
17
|
+
return modelId;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function getRunnerKindForModel(modelId) {
|
|
21
|
+
const resolvedId = resolveModelId(modelId);
|
|
22
|
+
if (resolvedId.startsWith("claude") || resolvedId.includes("anthropic") || resolvedId.startsWith("us.anthropic")) {
|
|
23
|
+
return "claude-agent-sdk";
|
|
24
|
+
}
|
|
25
|
+
return "claude-agent-sdk";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// src/provider/sandagent-language-model.ts
|
|
29
|
+
function getLogger(settings) {
|
|
30
|
+
if (settings.logger === false) {
|
|
31
|
+
return {
|
|
32
|
+
debug: () => {
|
|
33
|
+
},
|
|
34
|
+
info: () => {
|
|
35
|
+
},
|
|
36
|
+
warn: () => {
|
|
37
|
+
},
|
|
38
|
+
error: () => {
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
if (settings.logger) {
|
|
43
|
+
return settings.logger;
|
|
44
|
+
}
|
|
45
|
+
const isVerbose = settings.verbose ?? false;
|
|
46
|
+
return {
|
|
47
|
+
debug: (msg) => isVerbose && console.debug(msg),
|
|
48
|
+
info: (msg) => isVerbose && console.info(msg),
|
|
49
|
+
warn: (msg) => console.warn(msg),
|
|
50
|
+
error: (msg) => console.error(msg)
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function createEmptyUsage() {
|
|
54
|
+
return {
|
|
55
|
+
inputTokens: {
|
|
56
|
+
total: 0,
|
|
57
|
+
noCache: 0,
|
|
58
|
+
cacheRead: 0,
|
|
59
|
+
cacheWrite: 0
|
|
60
|
+
},
|
|
61
|
+
outputTokens: {
|
|
62
|
+
total: 0,
|
|
63
|
+
text: void 0,
|
|
64
|
+
reasoning: void 0
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
var SandAgentLanguageModel = class {
|
|
69
|
+
specificationVersion = "v3";
|
|
70
|
+
provider = "sandagent";
|
|
71
|
+
modelId;
|
|
72
|
+
supportedUrls = {
|
|
73
|
+
"image/*": [/.*/]
|
|
74
|
+
};
|
|
75
|
+
options;
|
|
76
|
+
logger;
|
|
77
|
+
sessionId;
|
|
78
|
+
toolNameMap = /* @__PURE__ */ new Map();
|
|
79
|
+
constructor(modelOptions) {
|
|
80
|
+
this.modelId = resolveModelId(modelOptions.id);
|
|
81
|
+
this.options = modelOptions.options;
|
|
82
|
+
this.logger = getLogger(modelOptions.options);
|
|
83
|
+
}
|
|
84
|
+
async doGenerate(options) {
|
|
85
|
+
const { stream, request } = await this.doStream(options);
|
|
86
|
+
const reader = stream.getReader();
|
|
87
|
+
const content = [];
|
|
88
|
+
const warnings = [];
|
|
89
|
+
let finishReason = {
|
|
90
|
+
unified: "other",
|
|
91
|
+
raw: void 0
|
|
92
|
+
};
|
|
93
|
+
let usage = createEmptyUsage();
|
|
94
|
+
let providerMetadata;
|
|
95
|
+
const textParts = /* @__PURE__ */ new Map();
|
|
96
|
+
const toolInputs = /* @__PURE__ */ new Map();
|
|
97
|
+
try {
|
|
98
|
+
while (true) {
|
|
99
|
+
const { done, value } = await reader.read();
|
|
100
|
+
if (done) break;
|
|
101
|
+
switch (value.type) {
|
|
102
|
+
case "text-start": {
|
|
103
|
+
textParts.set(value.id, { text: "" });
|
|
104
|
+
break;
|
|
105
|
+
}
|
|
106
|
+
case "text-delta": {
|
|
107
|
+
const part = textParts.get(value.id);
|
|
108
|
+
if (part) {
|
|
109
|
+
part.text += value.delta;
|
|
110
|
+
}
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
case "text-end": {
|
|
114
|
+
const part = textParts.get(value.id);
|
|
115
|
+
if (part) {
|
|
116
|
+
content.push({
|
|
117
|
+
type: "text",
|
|
118
|
+
text: part.text
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case "tool-input-start": {
|
|
124
|
+
toolInputs.set(value.id, { toolName: value.toolName, input: "" });
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
case "tool-input-delta": {
|
|
128
|
+
const tool = toolInputs.get(value.id);
|
|
129
|
+
if (tool) {
|
|
130
|
+
tool.input += value.delta;
|
|
131
|
+
}
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
case "tool-input-end": {
|
|
135
|
+
break;
|
|
136
|
+
}
|
|
137
|
+
case "tool-call": {
|
|
138
|
+
content.push({
|
|
139
|
+
type: "tool-call",
|
|
140
|
+
toolCallId: value.toolCallId,
|
|
141
|
+
toolName: value.toolName,
|
|
142
|
+
input: value.input,
|
|
143
|
+
providerExecuted: value.providerExecuted
|
|
144
|
+
});
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case "stream-start": {
|
|
148
|
+
warnings.push(...value.warnings);
|
|
149
|
+
break;
|
|
150
|
+
}
|
|
151
|
+
case "finish": {
|
|
152
|
+
finishReason = value.finishReason;
|
|
153
|
+
usage = value.usage;
|
|
154
|
+
providerMetadata = value.providerMetadata;
|
|
155
|
+
break;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
} finally {
|
|
160
|
+
reader.releaseLock();
|
|
161
|
+
}
|
|
162
|
+
return {
|
|
163
|
+
content,
|
|
164
|
+
finishReason,
|
|
165
|
+
usage,
|
|
166
|
+
providerMetadata,
|
|
167
|
+
request,
|
|
168
|
+
warnings
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
async doStream(options) {
|
|
172
|
+
const { prompt, abortSignal } = options;
|
|
173
|
+
const messages = this.convertPromptToMessages(prompt);
|
|
174
|
+
this.logger.debug(
|
|
175
|
+
`[sandagent] Starting stream with ${messages.length} messages`
|
|
176
|
+
);
|
|
177
|
+
const sandbox = this.options.sandbox;
|
|
178
|
+
const sandboxEnv = sandbox.getEnv?.() ?? {};
|
|
179
|
+
const sandboxWorkdir = this.options.cwd ?? sandbox.getWorkdir?.() ?? "/workspace";
|
|
180
|
+
const agent = new SandAgent({
|
|
181
|
+
sandbox: this.options.sandbox,
|
|
182
|
+
runner: this.options.runner,
|
|
183
|
+
env: { ...sandboxEnv, ...this.options.env }
|
|
184
|
+
});
|
|
185
|
+
try {
|
|
186
|
+
const stream = await agent.stream({
|
|
187
|
+
messages,
|
|
188
|
+
workspace: {
|
|
189
|
+
path: sandboxWorkdir
|
|
190
|
+
},
|
|
191
|
+
resume: this.options.resume,
|
|
192
|
+
signal: abortSignal
|
|
193
|
+
});
|
|
194
|
+
const self = this;
|
|
195
|
+
const reader = stream.getReader();
|
|
196
|
+
if (!reader) {
|
|
197
|
+
throw new Error("Response body is not readable");
|
|
198
|
+
}
|
|
199
|
+
const outputStream = new ReadableStream({
|
|
200
|
+
async start(controller) {
|
|
201
|
+
try {
|
|
202
|
+
let buffer = "";
|
|
203
|
+
while (true) {
|
|
204
|
+
const { done, value } = await reader.read();
|
|
205
|
+
if (done) {
|
|
206
|
+
if (buffer.trim()) {
|
|
207
|
+
const parts = self.parseSSEBuffer(buffer);
|
|
208
|
+
for (const part of parts) {
|
|
209
|
+
controller.enqueue(part);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
controller.close();
|
|
213
|
+
break;
|
|
214
|
+
}
|
|
215
|
+
const text = new TextDecoder().decode(value);
|
|
216
|
+
buffer += text;
|
|
217
|
+
const lines = buffer.split("\n");
|
|
218
|
+
buffer = lines.pop() ?? "";
|
|
219
|
+
let foundDone = false;
|
|
220
|
+
for (const line of lines) {
|
|
221
|
+
if (line.startsWith("data: ")) {
|
|
222
|
+
const data = line.slice(6);
|
|
223
|
+
if (data === "[DONE]") {
|
|
224
|
+
foundDone = true;
|
|
225
|
+
continue;
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
const parts = self.parseSSEData(data);
|
|
229
|
+
for (const part of parts) {
|
|
230
|
+
controller.enqueue(part);
|
|
231
|
+
if (self.options.artifactProcessors?.length && self.sessionId) {
|
|
232
|
+
const sessionId = self.sessionId;
|
|
233
|
+
for (const processor of self.options.artifactProcessors) {
|
|
234
|
+
Promise.resolve().then(() => processor.onChange(part, sessionId)).catch((e) => {
|
|
235
|
+
self.logger.error(
|
|
236
|
+
`[sandagent] Artifact processor error: ${e}`
|
|
237
|
+
);
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
} catch (e) {
|
|
243
|
+
self.logger.error(
|
|
244
|
+
`[sandagent] Failed to parse SSE data: ${e}`
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
if (foundDone) {
|
|
250
|
+
controller.close();
|
|
251
|
+
return;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
} catch (error) {
|
|
255
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
256
|
+
self.logger.info("[sandagent] Stream aborted by user");
|
|
257
|
+
} else {
|
|
258
|
+
self.logger.error(`[sandagent] Stream error: ${error}`);
|
|
259
|
+
}
|
|
260
|
+
controller.error(error);
|
|
261
|
+
}
|
|
262
|
+
},
|
|
263
|
+
cancel() {
|
|
264
|
+
reader.cancel();
|
|
265
|
+
}
|
|
266
|
+
});
|
|
267
|
+
return {
|
|
268
|
+
stream: outputStream,
|
|
269
|
+
request: {
|
|
270
|
+
body: JSON.stringify({ messages })
|
|
271
|
+
}
|
|
272
|
+
};
|
|
273
|
+
} catch (error) {
|
|
274
|
+
await agent.destroy().catch(() => {
|
|
275
|
+
});
|
|
276
|
+
throw error;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
parseSSEBuffer(buffer) {
|
|
280
|
+
const parts = [];
|
|
281
|
+
const lines = buffer.split("\n");
|
|
282
|
+
for (const line of lines) {
|
|
283
|
+
if (line.startsWith("data: ")) {
|
|
284
|
+
const data = line.slice(6);
|
|
285
|
+
if (data === "[DONE]") {
|
|
286
|
+
continue;
|
|
287
|
+
}
|
|
288
|
+
try {
|
|
289
|
+
const parsedParts = this.parseSSEData(data);
|
|
290
|
+
parts.push(...parsedParts);
|
|
291
|
+
} catch (e) {
|
|
292
|
+
this.logger.error(`[sandagent] Failed to parse SSE data: ${e}`);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
return parts;
|
|
297
|
+
}
|
|
298
|
+
parseSSEData(data) {
|
|
299
|
+
const parts = [];
|
|
300
|
+
const parsed = JSON.parse(data);
|
|
301
|
+
switch (parsed.type) {
|
|
302
|
+
case "start": {
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
case "message-metadata": {
|
|
306
|
+
const metadata = parsed.messageMetadata;
|
|
307
|
+
if (metadata?.sessionId && typeof metadata.sessionId === "string") {
|
|
308
|
+
this.sessionId = metadata.sessionId;
|
|
309
|
+
this.logger.debug(
|
|
310
|
+
`[sandagent] Session ID extracted: ${this.sessionId}`
|
|
311
|
+
);
|
|
312
|
+
parts.push({
|
|
313
|
+
type: "raw",
|
|
314
|
+
rawValue: this.sessionId
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
case "text-start": {
|
|
320
|
+
parts.push({
|
|
321
|
+
type: "text-start",
|
|
322
|
+
id: parsed.id,
|
|
323
|
+
providerMetadata: {
|
|
324
|
+
"claude-code": {
|
|
325
|
+
sessionId: this.sessionId
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
});
|
|
329
|
+
break;
|
|
330
|
+
}
|
|
331
|
+
case "text-delta": {
|
|
332
|
+
parts.push({
|
|
333
|
+
type: "text-delta",
|
|
334
|
+
id: parsed.id,
|
|
335
|
+
delta: parsed.delta
|
|
336
|
+
});
|
|
337
|
+
break;
|
|
338
|
+
}
|
|
339
|
+
case "text-end": {
|
|
340
|
+
parts.push({
|
|
341
|
+
type: "text-end",
|
|
342
|
+
id: parsed.id
|
|
343
|
+
});
|
|
344
|
+
break;
|
|
345
|
+
}
|
|
346
|
+
case "tool-input-start": {
|
|
347
|
+
parts.push({
|
|
348
|
+
type: "tool-input-start",
|
|
349
|
+
id: parsed.toolCallId,
|
|
350
|
+
toolName: parsed.toolName,
|
|
351
|
+
dynamic: parsed.dynamic,
|
|
352
|
+
providerExecuted: parsed.providerExecuted
|
|
353
|
+
});
|
|
354
|
+
break;
|
|
355
|
+
}
|
|
356
|
+
case "tool-input-delta": {
|
|
357
|
+
parts.push({
|
|
358
|
+
type: "tool-input-delta",
|
|
359
|
+
id: parsed.toolCallId,
|
|
360
|
+
delta: parsed.inputTextDelta
|
|
361
|
+
});
|
|
362
|
+
break;
|
|
363
|
+
}
|
|
364
|
+
case "tool-input-available": {
|
|
365
|
+
const toolCallId = parsed.toolCallId;
|
|
366
|
+
const toolName = parsed.toolName;
|
|
367
|
+
const input = parsed.input;
|
|
368
|
+
this.toolNameMap.set(toolCallId, toolName);
|
|
369
|
+
parts.push({
|
|
370
|
+
type: "tool-call",
|
|
371
|
+
toolCallId,
|
|
372
|
+
toolName,
|
|
373
|
+
input: JSON.stringify(input),
|
|
374
|
+
dynamic: parsed.dynamic,
|
|
375
|
+
providerExecuted: parsed.providerExecuted
|
|
376
|
+
});
|
|
377
|
+
break;
|
|
378
|
+
}
|
|
379
|
+
case "tool-output-available": {
|
|
380
|
+
const toolName = this.toolNameMap.get(parsed.toolCallId);
|
|
381
|
+
parts.push({
|
|
382
|
+
type: "tool-result",
|
|
383
|
+
toolCallId: parsed.toolCallId,
|
|
384
|
+
toolName: toolName ?? "",
|
|
385
|
+
result: parsed.output,
|
|
386
|
+
isError: parsed.isError,
|
|
387
|
+
dynamic: parsed.dynamic
|
|
388
|
+
});
|
|
389
|
+
break;
|
|
390
|
+
}
|
|
391
|
+
case "error": {
|
|
392
|
+
parts.push({
|
|
393
|
+
type: "error",
|
|
394
|
+
error: new Error(parsed.errorText)
|
|
395
|
+
});
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
case "finish": {
|
|
399
|
+
const rawFinishReason = parsed.finishReason;
|
|
400
|
+
let finishReason;
|
|
401
|
+
if (typeof rawFinishReason === "object" && rawFinishReason !== null && "unified" in rawFinishReason) {
|
|
402
|
+
finishReason = rawFinishReason;
|
|
403
|
+
} else {
|
|
404
|
+
finishReason = this.mapFinishReason(rawFinishReason);
|
|
405
|
+
}
|
|
406
|
+
const rawUsage = parsed.usage ?? parsed.messageMetadata;
|
|
407
|
+
const usage = this.convertUsage(
|
|
408
|
+
rawUsage
|
|
409
|
+
);
|
|
410
|
+
parts.push({
|
|
411
|
+
type: "finish",
|
|
412
|
+
finishReason,
|
|
413
|
+
usage,
|
|
414
|
+
providerMetadata: {
|
|
415
|
+
sandagent: parsed.messageMetadata
|
|
416
|
+
}
|
|
417
|
+
});
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return parts;
|
|
422
|
+
}
|
|
423
|
+
convertPromptToMessages(prompt) {
|
|
424
|
+
const messages = [];
|
|
425
|
+
for (const message of prompt) {
|
|
426
|
+
switch (message.role) {
|
|
427
|
+
case "system": {
|
|
428
|
+
messages.push({
|
|
429
|
+
role: "system",
|
|
430
|
+
content: message.content
|
|
431
|
+
});
|
|
432
|
+
break;
|
|
433
|
+
}
|
|
434
|
+
case "user": {
|
|
435
|
+
const textParts = message.content.filter(
|
|
436
|
+
(part) => part.type === "text"
|
|
437
|
+
).map((part) => part.text);
|
|
438
|
+
if (textParts.length > 0) {
|
|
439
|
+
messages.push({
|
|
440
|
+
role: "user",
|
|
441
|
+
content: textParts.join("\n")
|
|
442
|
+
});
|
|
443
|
+
}
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
case "assistant": {
|
|
447
|
+
const textParts = message.content.filter(
|
|
448
|
+
(part) => part.type === "text"
|
|
449
|
+
).map((part) => part.text);
|
|
450
|
+
if (textParts.length > 0) {
|
|
451
|
+
messages.push({
|
|
452
|
+
role: "assistant",
|
|
453
|
+
content: textParts.join("\n")
|
|
454
|
+
});
|
|
455
|
+
}
|
|
456
|
+
break;
|
|
457
|
+
}
|
|
458
|
+
case "tool": {
|
|
459
|
+
break;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return messages;
|
|
464
|
+
}
|
|
465
|
+
mapFinishReason(reason) {
|
|
466
|
+
switch (reason) {
|
|
467
|
+
case "stop":
|
|
468
|
+
return { unified: "stop", raw: reason };
|
|
469
|
+
case "length":
|
|
470
|
+
return { unified: "length", raw: reason };
|
|
471
|
+
case "tool_calls":
|
|
472
|
+
case "tool-calls":
|
|
473
|
+
return { unified: "tool-calls", raw: reason };
|
|
474
|
+
case "content_filter":
|
|
475
|
+
case "content-filter":
|
|
476
|
+
return { unified: "content-filter", raw: reason };
|
|
477
|
+
case "error":
|
|
478
|
+
return { unified: "error", raw: reason };
|
|
479
|
+
default:
|
|
480
|
+
return { unified: "other", raw: reason ?? "unknown" };
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
convertUsage(data) {
|
|
484
|
+
if (!data) {
|
|
485
|
+
return createEmptyUsage();
|
|
486
|
+
}
|
|
487
|
+
if ("inputTokens" in data && "outputTokens" in data) {
|
|
488
|
+
const inputTokens = data.inputTokens;
|
|
489
|
+
const outputTokens = data.outputTokens;
|
|
490
|
+
return {
|
|
491
|
+
inputTokens: {
|
|
492
|
+
total: inputTokens.total ?? 0,
|
|
493
|
+
noCache: inputTokens.noCache ?? 0,
|
|
494
|
+
cacheRead: inputTokens.cacheRead ?? 0,
|
|
495
|
+
cacheWrite: inputTokens.cacheWrite ?? 0
|
|
496
|
+
},
|
|
497
|
+
outputTokens: {
|
|
498
|
+
total: outputTokens.total ?? 0,
|
|
499
|
+
text: void 0,
|
|
500
|
+
reasoning: void 0
|
|
501
|
+
}
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
const usage = data.usage ?? data;
|
|
505
|
+
if ("input_tokens" in usage || "output_tokens" in usage) {
|
|
506
|
+
const inputTokens = usage.input_tokens ?? 0;
|
|
507
|
+
const outputTokens = usage.output_tokens ?? 0;
|
|
508
|
+
const cacheWrite = usage.cache_creation_input_tokens ?? 0;
|
|
509
|
+
const cacheRead = usage.cache_read_input_tokens ?? 0;
|
|
510
|
+
return {
|
|
511
|
+
inputTokens: {
|
|
512
|
+
total: inputTokens + cacheWrite + cacheRead,
|
|
513
|
+
noCache: inputTokens,
|
|
514
|
+
cacheRead,
|
|
515
|
+
cacheWrite
|
|
516
|
+
},
|
|
517
|
+
outputTokens: {
|
|
518
|
+
total: outputTokens,
|
|
519
|
+
text: void 0,
|
|
520
|
+
reasoning: void 0
|
|
521
|
+
}
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
return createEmptyUsage();
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
// src/provider/sandagent-provider.ts
|
|
529
|
+
function getLogger2(settings) {
|
|
530
|
+
if (settings.logger === false) {
|
|
531
|
+
return {
|
|
532
|
+
debug: () => {
|
|
533
|
+
},
|
|
534
|
+
info: () => {
|
|
535
|
+
},
|
|
536
|
+
warn: () => {
|
|
537
|
+
},
|
|
538
|
+
error: () => {
|
|
539
|
+
}
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
if (settings.logger) {
|
|
543
|
+
return settings.logger;
|
|
544
|
+
}
|
|
545
|
+
const isVerbose = settings.verbose ?? false;
|
|
546
|
+
return {
|
|
547
|
+
debug: (msg) => isVerbose && console.debug(msg),
|
|
548
|
+
info: (msg) => isVerbose && console.info(msg),
|
|
549
|
+
warn: (msg) => console.warn(msg),
|
|
550
|
+
error: (msg) => console.error(msg)
|
|
551
|
+
};
|
|
552
|
+
}
|
|
553
|
+
function createSandAgent(defaultOptions) {
|
|
554
|
+
const logger = getLogger2(defaultOptions);
|
|
555
|
+
if (!defaultOptions.sandbox) {
|
|
556
|
+
throw new Error(
|
|
557
|
+
"SandAgent provider requires a sandbox adapter. Please provide one, e.g.: new E2BSandbox({ apiKey: 'xxx' })"
|
|
558
|
+
);
|
|
559
|
+
}
|
|
560
|
+
const createModel = (modelId, options = {}) => {
|
|
561
|
+
const runnerKind = getRunnerKindForModel(modelId);
|
|
562
|
+
const resolvedModelId = resolveModelId(modelId);
|
|
563
|
+
const runner = {
|
|
564
|
+
kind: runnerKind,
|
|
565
|
+
model: resolvedModelId,
|
|
566
|
+
outputFormat: "stream"
|
|
567
|
+
};
|
|
568
|
+
const mergedOptions = {
|
|
569
|
+
...defaultOptions,
|
|
570
|
+
...options,
|
|
571
|
+
runner,
|
|
572
|
+
env: {
|
|
573
|
+
...defaultOptions.env,
|
|
574
|
+
...options.env
|
|
575
|
+
},
|
|
576
|
+
artifactProcessors: [
|
|
577
|
+
...defaultOptions.artifactProcessors ?? [],
|
|
578
|
+
...options.artifactProcessors ?? []
|
|
579
|
+
]
|
|
580
|
+
};
|
|
581
|
+
logger.debug(
|
|
582
|
+
`[sandagent] Creating model: ${modelId} with runner: ${runner.kind}`
|
|
583
|
+
);
|
|
584
|
+
return new SandAgentLanguageModel({
|
|
585
|
+
id: modelId,
|
|
586
|
+
options: mergedOptions
|
|
587
|
+
});
|
|
588
|
+
};
|
|
589
|
+
const provider = function(modelId, options) {
|
|
590
|
+
if (new.target) {
|
|
591
|
+
throw new Error(
|
|
592
|
+
"The SandAgent model function cannot be called with the new keyword."
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
return createModel(modelId, options);
|
|
596
|
+
};
|
|
597
|
+
provider.languageModel = createModel;
|
|
598
|
+
provider.chat = createModel;
|
|
599
|
+
provider.specificationVersion = "v3";
|
|
600
|
+
provider.embeddingModel = (modelId) => {
|
|
601
|
+
throw new NoSuchModelError({
|
|
602
|
+
modelId,
|
|
603
|
+
modelType: "embeddingModel"
|
|
604
|
+
});
|
|
605
|
+
};
|
|
606
|
+
provider.textEmbeddingModel = (modelId) => {
|
|
607
|
+
throw new NoSuchModelError({
|
|
608
|
+
modelId,
|
|
609
|
+
modelType: "embeddingModel"
|
|
610
|
+
});
|
|
611
|
+
};
|
|
612
|
+
provider.imageModel = (modelId) => {
|
|
613
|
+
throw new NoSuchModelError({
|
|
614
|
+
modelId,
|
|
615
|
+
modelType: "imageModel"
|
|
616
|
+
});
|
|
617
|
+
};
|
|
618
|
+
return provider;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
// src/index.ts
|
|
622
|
+
import { LocalSandbox } from "@sandagent/manager";
|
|
623
|
+
var VERSION = "0.1.0";
|
|
624
|
+
export {
|
|
625
|
+
LocalSandbox,
|
|
626
|
+
SandAgentLanguageModel,
|
|
627
|
+
VERSION,
|
|
628
|
+
createSandAgent,
|
|
629
|
+
resolveModelId
|
|
630
|
+
};
|
|
631
|
+
//# sourceMappingURL=index.js.map
|