@open-insight/core 0.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/dist/agent-2V5_4nlu.mjs +676 -0
- package/dist/agent-2V5_4nlu.mjs.map +1 -0
- package/dist/index-CmxU7-9a.d.mts +725 -0
- package/dist/index-CmxU7-9a.d.mts.map +1 -0
- package/dist/index.d.mts +238 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +585 -0
- package/dist/index.mjs.map +1 -0
- package/dist/internal.d.mts +2 -0
- package/dist/internal.mjs +2 -0
- package/dist/rolldown-runtime-BBjsoOtd.mjs +27 -0
- package/package.json +52 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
import { t as __exportAll } from "./rolldown-runtime-BBjsoOtd.mjs";
|
|
2
|
+
import { A as derive, B as Instructions, C as Script, D as resolve, E as makeDist, F as encode, I as Image, L as Snapshot, M as fromImage, N as hash, O as ContextSchema, P as makeName, R as make$5, S as Cwd, T as makeDir, V as instruction_exports, _ as bashQuote, a as Trajectory, b as SandboxError, c as ProviderService$1, d as ResourceLimitsSchema, f as make$3, g as provider_exports, h as spawn_exports, i as ToolMessage, j as extend, k as ModeSchema, l as AgentError, m as asPromise, n as Message, o as UserMessage, p as makeName$1, r as SystemMessage, t as AssistantMessage, v as formatBash, w as make$4, x as snapshot_exports, y as ProviderService, z as Instruction } from "./agent-2V5_4nlu.mjs";
|
|
3
|
+
import { Context, Crypto, Effect, FileSystem, Option, Path, Ref, Schema, Stream } from "effect";
|
|
4
|
+
import { HttpClient } from "effect/unstable/http";
|
|
5
|
+
import { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner";
|
|
6
|
+
import { ChildProcess } from "effect/unstable/process";
|
|
7
|
+
import { Chat, LanguageModel, Response, Toolkit } from "effect/unstable/ai";
|
|
8
|
+
//#region src/sandbox/provider/builtin/docker/utils.ts
|
|
9
|
+
const makeRuntime = Effect.fn("sandbox/docker/makeRuntime")(function* () {
|
|
10
|
+
const runtimes = [
|
|
11
|
+
"docker",
|
|
12
|
+
"podman",
|
|
13
|
+
"nerdctl"
|
|
14
|
+
];
|
|
15
|
+
const spawner = yield* spawn_exports.SpawnService;
|
|
16
|
+
const found = (yield* Effect.all(runtimes.map((runtime) => spawner.string(ChildProcess.make`command -v ${runtime}`).pipe(Effect.map(Option.some), Effect.catchIf((error) => Boolean(error && typeof error === "object" && "reason" in error), () => Effect.succeed(Option.none())))), { concurrency: "unbounded" })).find((o) => Option.isSome(o));
|
|
17
|
+
if (found === void 0) return yield* Effect.fail(/* @__PURE__ */ new Error("No container runtime found."));
|
|
18
|
+
return ChildProcess.prefix(found.value.trim());
|
|
19
|
+
});
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/sandbox/provider/builtin/docker/index.ts
|
|
22
|
+
const formatResources = (resources) => {
|
|
23
|
+
if (resources === null) return [];
|
|
24
|
+
const { numCPUs, memoryMiB, numGPUs, diskMiB } = resources;
|
|
25
|
+
const resourceArgs = [];
|
|
26
|
+
if (numCPUs !== void 0) resourceArgs.push("--cpus", `${numCPUs}`);
|
|
27
|
+
if (memoryMiB !== void 0) resourceArgs.push("--memory", `${memoryMiB}m`);
|
|
28
|
+
if (numGPUs !== void 0 && numGPUs > 0) resourceArgs.push("--gpus", `count=${numGPUs}`);
|
|
29
|
+
if (diskMiB !== void 0) resourceArgs.push("--storage-opt", `size=${diskMiB}m`);
|
|
30
|
+
return resourceArgs;
|
|
31
|
+
};
|
|
32
|
+
const make$2 = Effect.fn(function* ({ portMappings = [] }) {
|
|
33
|
+
const runtime = yield* makeRuntime().pipe(Effect.mapError(SandboxError.provider("docker")));
|
|
34
|
+
const crypto = yield* Crypto.Crypto;
|
|
35
|
+
const spawner = yield* spawn_exports.SpawnService;
|
|
36
|
+
const fs = yield* FileSystem.FileSystem;
|
|
37
|
+
const path = yield* Path.Path;
|
|
38
|
+
const httpClient = yield* HttpClient.HttpClient;
|
|
39
|
+
const childProcessSpawner = yield* ChildProcessSpawner;
|
|
40
|
+
const serviceContext = Context.mergeAll(Context.make(Crypto.Crypto, crypto), Context.make(FileSystem.FileSystem, fs), Context.make(Path.Path, path), Context.make(HttpClient.HttpClient, httpClient), Context.make(ChildProcessSpawner, childProcessSpawner));
|
|
41
|
+
const ensureSnapshot = Effect.fn(function* ({ snapshot, context }) {
|
|
42
|
+
const mapBuildError = Effect.mapError(SandboxError.snapshotBuild(snapshot));
|
|
43
|
+
const name = yield* makeName(snapshot).pipe(Effect.provideService(Crypto.Crypto, crypto), mapBuildError);
|
|
44
|
+
if (yield* spawner.string(ChildProcess.make`image inspect ${name}`.pipe(runtime)).pipe(Effect.as(true), Effect.catch(() => Effect.succeed(false)))) return;
|
|
45
|
+
const contextDir = yield* resolve(context).pipe(Effect.provideContext(serviceContext), mapBuildError);
|
|
46
|
+
const containerfilePath = yield* fs.makeTempFile({
|
|
47
|
+
prefix: "open-insight-",
|
|
48
|
+
suffix: ".Containerfile"
|
|
49
|
+
}).pipe(mapBuildError);
|
|
50
|
+
const command = ChildProcess.make`build -f ${containerfilePath} -t ${name} ${contextDir}`.pipe(runtime);
|
|
51
|
+
const containerfile = yield* encode(snapshot).pipe(mapBuildError);
|
|
52
|
+
yield* fs.writeFileString(containerfilePath, containerfile).pipe(mapBuildError);
|
|
53
|
+
yield* spawner.string(command).pipe(mapBuildError);
|
|
54
|
+
});
|
|
55
|
+
const removeSnapshot = Effect.fn(function* ({ snapshot }) {
|
|
56
|
+
const mapBuildError = Effect.mapError(SandboxError.snapshotBuild(snapshot));
|
|
57
|
+
const name = yield* makeName(snapshot).pipe(Effect.provideService(Crypto.Crypto, crypto), mapBuildError);
|
|
58
|
+
const command = ChildProcess.make`rmi ${name}`.pipe(runtime);
|
|
59
|
+
yield* spawner.string(command).pipe(mapBuildError);
|
|
60
|
+
});
|
|
61
|
+
return {
|
|
62
|
+
ensureSnapshot,
|
|
63
|
+
deriveSnapshot: Effect.fn(function* ({ snapshot, context, instructions }) {
|
|
64
|
+
yield* ensureSnapshot({
|
|
65
|
+
snapshot,
|
|
66
|
+
context
|
|
67
|
+
});
|
|
68
|
+
const name = yield* makeName(snapshot).pipe(Effect.provideService(Crypto.Crypto, crypto), Effect.mapError(SandboxError.snapshotBuild(snapshot)));
|
|
69
|
+
yield* ensureSnapshot({
|
|
70
|
+
snapshot: Snapshot.make({
|
|
71
|
+
image: name,
|
|
72
|
+
instructions
|
|
73
|
+
}),
|
|
74
|
+
context
|
|
75
|
+
});
|
|
76
|
+
}),
|
|
77
|
+
removeSnapshot,
|
|
78
|
+
runSandbox: Effect.fn(function* ({ snapshot, resources }) {
|
|
79
|
+
const mapUsageError = Effect.mapError(SandboxError.snapshotUsage(snapshot));
|
|
80
|
+
const name = yield* makeName(snapshot).pipe(Effect.provideService(Crypto.Crypto, crypto), mapUsageError);
|
|
81
|
+
const sandboxName = yield* makeName$1(snapshot).pipe(Effect.provideService(Crypto.Crypto, crypto), mapUsageError);
|
|
82
|
+
const portMappingArgs = portMappings.flatMap((mapping) => ["-p", `${mapping.hostPort}:${mapping.sandboxPort}`]);
|
|
83
|
+
const run = ChildProcess.make("run", [
|
|
84
|
+
"-it",
|
|
85
|
+
"--rm",
|
|
86
|
+
"--detach",
|
|
87
|
+
"--name",
|
|
88
|
+
sandboxName,
|
|
89
|
+
...portMappingArgs,
|
|
90
|
+
...formatResources(resources),
|
|
91
|
+
name,
|
|
92
|
+
"sleep",
|
|
93
|
+
"infinity"
|
|
94
|
+
]).pipe(runtime);
|
|
95
|
+
yield* spawner.string(run).pipe(Effect.mapError(SandboxError.provider("docker")));
|
|
96
|
+
yield* Effect.addFinalizer(() => spawner.string(ChildProcess.make`rm --force ${sandboxName}`.pipe(runtime)).pipe(Effect.ignore));
|
|
97
|
+
const makeExecCommand = (command, input) => {
|
|
98
|
+
const args = [];
|
|
99
|
+
if (input !== void 0) args.push("-i");
|
|
100
|
+
for (const [key, value] of Object.entries(command.options.env ?? {})) if (value !== void 0) args.push("-e", `${key}=${value}`);
|
|
101
|
+
if (command.options.cwd !== void 0) args.push("-w", command.options.cwd);
|
|
102
|
+
args.push(sandboxName, "sh", "-c", formatBash(command));
|
|
103
|
+
const options = input === void 0 ? {} : { stdin: { stream: Stream.make(input).pipe(Stream.encodeText) } };
|
|
104
|
+
return ChildProcess.make("exec", args, options).pipe(runtime);
|
|
105
|
+
};
|
|
106
|
+
return yield* make$3({
|
|
107
|
+
$(cmd, input) {
|
|
108
|
+
const bash = formatBash(cmd);
|
|
109
|
+
const execCommand = makeExecCommand(cmd, input);
|
|
110
|
+
return spawner.string(execCommand).pipe(Effect.mapError(SandboxError.sandboxExec({
|
|
111
|
+
name,
|
|
112
|
+
operation: bash
|
|
113
|
+
})));
|
|
114
|
+
},
|
|
115
|
+
expose: Effect.fn(function* ({ sandboxPort, hostPort }) {
|
|
116
|
+
if (!portMappings.some((mapping) => mapping.sandboxPort === sandboxPort && mapping.hostPort === hostPort)) return yield* Effect.fail(SandboxError.sandboxExpose({
|
|
117
|
+
name,
|
|
118
|
+
sandboxPort,
|
|
119
|
+
hostPort
|
|
120
|
+
})("Expected port mapping cannot be exposed because it was not specified in the configuration. Containers cannot exposing arbitrary ports that were not mapped when the container was created."));
|
|
121
|
+
return { hostUrl: `http://localhost:${hostPort}` };
|
|
122
|
+
}),
|
|
123
|
+
download: Effect.fn(function* ({ sandboxPath, hostPath }) {
|
|
124
|
+
const from = `${sandboxName}:${sandboxPath}`;
|
|
125
|
+
const command = ChildProcess.make`cp ${from} ${hostPath}`;
|
|
126
|
+
yield* spawner.string(command.pipe(runtime)).pipe(Effect.mapError(SandboxError.sandboxExec({
|
|
127
|
+
name,
|
|
128
|
+
operation: formatBash(command)
|
|
129
|
+
})));
|
|
130
|
+
}),
|
|
131
|
+
upload: Effect.fn(function* ({ sandboxPath, hostPath }) {
|
|
132
|
+
const to = `${sandboxName}:${sandboxPath}`;
|
|
133
|
+
const command = ChildProcess.make`cp ${hostPath} ${to}`;
|
|
134
|
+
yield* spawner.string(command.pipe(runtime)).pipe(Effect.mapError(SandboxError.sandboxExec({
|
|
135
|
+
name,
|
|
136
|
+
operation: formatBash(command)
|
|
137
|
+
})));
|
|
138
|
+
}),
|
|
139
|
+
readFile: "cat",
|
|
140
|
+
writeFile: "tee"
|
|
141
|
+
}).pipe(Effect.provideService(spawn_exports.SpawnService, spawner));
|
|
142
|
+
})
|
|
143
|
+
};
|
|
144
|
+
}, (effect) => effect.pipe(Effect.provide(spawn_exports.SpawnService.layer)));
|
|
145
|
+
//#endregion
|
|
146
|
+
//#region src/sandbox/provider/builtin/docker/export.ts
|
|
147
|
+
var export_exports$6 = /* @__PURE__ */ __exportAll({ make: () => make$2 });
|
|
148
|
+
//#endregion
|
|
149
|
+
//#region src/sandbox/context/export.ts
|
|
150
|
+
var export_exports$5 = /* @__PURE__ */ __exportAll({
|
|
151
|
+
ContextSchema: () => ContextSchema,
|
|
152
|
+
Cwd: () => Cwd,
|
|
153
|
+
ModeSchema: () => ModeSchema,
|
|
154
|
+
Script: () => Script,
|
|
155
|
+
make: () => make$4,
|
|
156
|
+
makeDir: () => makeDir,
|
|
157
|
+
makeDist: () => makeDist
|
|
158
|
+
});
|
|
159
|
+
//#endregion
|
|
160
|
+
//#region src/sandbox/snapshot/export.ts
|
|
161
|
+
var export_exports$4 = /* @__PURE__ */ __exportAll({
|
|
162
|
+
Image: () => Image,
|
|
163
|
+
Inst: () => instruction_exports,
|
|
164
|
+
Instruction: () => Instruction,
|
|
165
|
+
Instructions: () => Instructions,
|
|
166
|
+
Internal: () => snapshot_exports,
|
|
167
|
+
Snapshot: () => Snapshot,
|
|
168
|
+
derive: () => derive,
|
|
169
|
+
extend: () => extend,
|
|
170
|
+
fromImage: () => fromImage,
|
|
171
|
+
hash: () => hash,
|
|
172
|
+
make: () => make$5,
|
|
173
|
+
snapshotName: () => makeName
|
|
174
|
+
});
|
|
175
|
+
//#endregion
|
|
176
|
+
//#region src/sandbox/export.ts
|
|
177
|
+
var export_exports$1 = /* @__PURE__ */ __exportAll({
|
|
178
|
+
Context: () => export_exports$5,
|
|
179
|
+
Docker: () => export_exports$6,
|
|
180
|
+
Internal: () => provider_exports,
|
|
181
|
+
ProviderService: () => ProviderService,
|
|
182
|
+
ResourceLimitsSchema: () => ResourceLimitsSchema,
|
|
183
|
+
SandboxError: () => SandboxError,
|
|
184
|
+
Snapshot: () => export_exports$4,
|
|
185
|
+
asPromise: () => asPromise,
|
|
186
|
+
bashQuote: () => bashQuote,
|
|
187
|
+
formatBash: () => formatBash,
|
|
188
|
+
make: () => make$3
|
|
189
|
+
});
|
|
190
|
+
//#endregion
|
|
191
|
+
//#region src/agent/builtin/effect/index.ts
|
|
192
|
+
const makeAgent$1 = Effect.fn(function* ({ sandbox: _sandbox, chat = Chat.empty, toolkit = Toolkit.empty }) {
|
|
193
|
+
const llm = yield* LanguageModel.LanguageModel;
|
|
194
|
+
const service = yield* chat;
|
|
195
|
+
return {
|
|
196
|
+
trajectory: () => Ref.get(service.history),
|
|
197
|
+
prompt: ({ prompt }) => Effect.succeed(service.streamText({
|
|
198
|
+
prompt,
|
|
199
|
+
toolkit
|
|
200
|
+
}).pipe(Stream.mapError(AgentError.stream), Stream.provideService(LanguageModel.LanguageModel, llm)))
|
|
201
|
+
};
|
|
202
|
+
});
|
|
203
|
+
const make$1 = Effect.fn(function* ({ chat = Chat.empty, toolkit = Toolkit.empty }) {
|
|
204
|
+
const llm = yield* LanguageModel.LanguageModel;
|
|
205
|
+
return {
|
|
206
|
+
runSession: Effect.fn(function* ({ sandbox }) {
|
|
207
|
+
return yield* makeAgent$1({
|
|
208
|
+
sandbox,
|
|
209
|
+
chat,
|
|
210
|
+
toolkit
|
|
211
|
+
});
|
|
212
|
+
}, (effect) => effect.pipe(Effect.provideService(LanguageModel.LanguageModel, llm))),
|
|
213
|
+
deriveSnapshot: Effect.fn(function* ({ snapshot }) {
|
|
214
|
+
return yield* Effect.succeed(snapshot);
|
|
215
|
+
})
|
|
216
|
+
};
|
|
217
|
+
});
|
|
218
|
+
//#endregion
|
|
219
|
+
//#region src/agent/builtin/effect/export.ts
|
|
220
|
+
var export_exports$3 = /* @__PURE__ */ __exportAll({ make: () => make$1 });
|
|
221
|
+
//#endregion
|
|
222
|
+
//#region src/agent/builtin/dummy/index.ts
|
|
223
|
+
var dummy_exports = /* @__PURE__ */ __exportAll({
|
|
224
|
+
make: () => make,
|
|
225
|
+
makeAgent: () => makeAgent
|
|
226
|
+
});
|
|
227
|
+
const randomText = Effect.fn(function* () {
|
|
228
|
+
return yield* Effect.sync(() => crypto.randomUUID().replaceAll("-", ""));
|
|
229
|
+
});
|
|
230
|
+
const finishPart = {
|
|
231
|
+
type: "finish",
|
|
232
|
+
reason: "stop",
|
|
233
|
+
usage: {
|
|
234
|
+
inputTokens: {
|
|
235
|
+
uncached: 0,
|
|
236
|
+
total: 0,
|
|
237
|
+
cacheRead: void 0,
|
|
238
|
+
cacheWrite: void 0
|
|
239
|
+
},
|
|
240
|
+
outputTokens: {
|
|
241
|
+
total: 0,
|
|
242
|
+
text: void 0,
|
|
243
|
+
reasoning: void 0
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
response: void 0
|
|
247
|
+
};
|
|
248
|
+
const makeDummyLanguageModel = Effect.fn(function* () {
|
|
249
|
+
return yield* LanguageModel.make({
|
|
250
|
+
generateText: () => randomText().pipe(Effect.map((text) => [{
|
|
251
|
+
type: "text",
|
|
252
|
+
text
|
|
253
|
+
}, finishPart])),
|
|
254
|
+
streamText: () => Stream.unwrap(randomText().pipe(Effect.map((text) => Stream.fromIterable([
|
|
255
|
+
{
|
|
256
|
+
type: "text-start",
|
|
257
|
+
id: "dummy"
|
|
258
|
+
},
|
|
259
|
+
{
|
|
260
|
+
type: "text-delta",
|
|
261
|
+
id: "dummy",
|
|
262
|
+
delta: text
|
|
263
|
+
},
|
|
264
|
+
{
|
|
265
|
+
type: "text-end",
|
|
266
|
+
id: "dummy"
|
|
267
|
+
},
|
|
268
|
+
finishPart
|
|
269
|
+
]))))
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
const makeAgent = Effect.fn(function* ({ chat = Chat.empty, toolkit = Toolkit.empty }) {
|
|
273
|
+
const llm = yield* makeDummyLanguageModel();
|
|
274
|
+
const service = yield* chat;
|
|
275
|
+
return {
|
|
276
|
+
trajectory: () => Ref.get(service.history),
|
|
277
|
+
prompt: ({ prompt }) => Effect.succeed(service.streamText({
|
|
278
|
+
prompt,
|
|
279
|
+
toolkit
|
|
280
|
+
}).pipe(Stream.mapError(AgentError.stream), Stream.provideService(LanguageModel.LanguageModel, llm)))
|
|
281
|
+
};
|
|
282
|
+
});
|
|
283
|
+
const make = Effect.fn(function* ({ chat = Chat.empty, toolkit = Toolkit.empty }) {
|
|
284
|
+
const llm = yield* makeDummyLanguageModel();
|
|
285
|
+
return {
|
|
286
|
+
runSession: Effect.fn(function* ({ sandbox: _sandbox }) {
|
|
287
|
+
return yield* makeAgent({
|
|
288
|
+
chat,
|
|
289
|
+
toolkit
|
|
290
|
+
});
|
|
291
|
+
}, (effect) => effect.pipe(Effect.provideService(LanguageModel.LanguageModel, llm))),
|
|
292
|
+
deriveSnapshot: Effect.fn(function* ({ snapshot }) {
|
|
293
|
+
return yield* Effect.succeed(snapshot);
|
|
294
|
+
})
|
|
295
|
+
};
|
|
296
|
+
});
|
|
297
|
+
//#endregion
|
|
298
|
+
//#region src/agent/builtin/ai-sdk/stream.ts
|
|
299
|
+
var AiSdkStreamError = class extends Schema.TaggedErrorClass()("AiSdkStreamError", { cause: Schema.Defect() }) {};
|
|
300
|
+
const decodeJsonOption = Schema.decodeUnknownOption(Schema.Json);
|
|
301
|
+
const decodeDocumentSourcePart = Schema.decodeUnknownOption(Response.DocumentSourcePart);
|
|
302
|
+
const decodeUrlSourcePart = Schema.decodeUnknownOption(Response.UrlSourcePart);
|
|
303
|
+
const jsonOrOmitted = (value) => Option.getOrElse(decodeJsonOption(value), () => ({
|
|
304
|
+
omitted: true,
|
|
305
|
+
reason: "non_json_value"
|
|
306
|
+
}));
|
|
307
|
+
const metadataPart = (part, details = {}) => Response.makePart("response-metadata", {
|
|
308
|
+
id: void 0,
|
|
309
|
+
modelId: void 0,
|
|
310
|
+
timestamp: void 0,
|
|
311
|
+
request: void 0,
|
|
312
|
+
metadata: aiSdkMetadata(part, details)
|
|
313
|
+
});
|
|
314
|
+
const aiSdkMetadata = (part, details = {}) => {
|
|
315
|
+
const partDetails = {
|
|
316
|
+
...details,
|
|
317
|
+
type: part.type
|
|
318
|
+
};
|
|
319
|
+
const metadata = part.providerMetadata;
|
|
320
|
+
return { aiSdk: metadata === void 0 ? { part: partDetails } : {
|
|
321
|
+
part: partDetails,
|
|
322
|
+
providerMetadata: jsonOrOmitted(metadata)
|
|
323
|
+
} };
|
|
324
|
+
};
|
|
325
|
+
const usageFromAiSdk = (usage) => new Response.Usage({
|
|
326
|
+
inputTokens: {
|
|
327
|
+
uncached: usage.inputTokenDetails.noCacheTokens,
|
|
328
|
+
total: usage.inputTokens,
|
|
329
|
+
cacheRead: usage.inputTokenDetails.cacheReadTokens,
|
|
330
|
+
cacheWrite: usage.inputTokenDetails.cacheWriteTokens
|
|
331
|
+
},
|
|
332
|
+
outputTokens: {
|
|
333
|
+
total: usage.outputTokens,
|
|
334
|
+
text: usage.outputTokenDetails.textTokens,
|
|
335
|
+
reasoning: usage.outputTokenDetails.reasoningTokens
|
|
336
|
+
}
|
|
337
|
+
});
|
|
338
|
+
const emptyUsage = () => new Response.Usage({
|
|
339
|
+
inputTokens: {
|
|
340
|
+
uncached: void 0,
|
|
341
|
+
total: void 0,
|
|
342
|
+
cacheRead: void 0,
|
|
343
|
+
cacheWrite: void 0
|
|
344
|
+
},
|
|
345
|
+
outputTokens: {
|
|
346
|
+
total: void 0,
|
|
347
|
+
text: void 0,
|
|
348
|
+
reasoning: void 0
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
const finishReason = (reason) => reason;
|
|
352
|
+
const filePart = (part) => {
|
|
353
|
+
const file = fileFromPart(part.file);
|
|
354
|
+
return file === void 0 ? [metadataPart(part, { reason: "file_data_unavailable" })] : [Response.makePart("file", {
|
|
355
|
+
mediaType: file.mediaType,
|
|
356
|
+
data: file.data,
|
|
357
|
+
metadata: aiSdkMetadata(part, part.type === "reasoning-file" ? { content: "reasoning" } : {})
|
|
358
|
+
})];
|
|
359
|
+
};
|
|
360
|
+
const fileFromPart = (file) => {
|
|
361
|
+
try {
|
|
362
|
+
return {
|
|
363
|
+
mediaType: file.mediaType,
|
|
364
|
+
data: file.uint8Array
|
|
365
|
+
};
|
|
366
|
+
} catch {
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
const sourcePart = (part) => {
|
|
371
|
+
switch (part.sourceType) {
|
|
372
|
+
case "url": return urlSourcePart(part);
|
|
373
|
+
case "document": return documentSourcePart(part);
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
const urlSourcePart = (part) => {
|
|
377
|
+
const source = decodeUrlSourcePart({
|
|
378
|
+
type: "source",
|
|
379
|
+
sourceType: "url",
|
|
380
|
+
id: part.id,
|
|
381
|
+
url: part.url,
|
|
382
|
+
title: part.title ?? part.url,
|
|
383
|
+
metadata: aiSdkMetadata(part)
|
|
384
|
+
});
|
|
385
|
+
return Option.match(source, {
|
|
386
|
+
onNone: () => [metadataPart(part, { reason: "invalid_source_url" })],
|
|
387
|
+
onSome: (sourcePart) => [sourcePart]
|
|
388
|
+
});
|
|
389
|
+
};
|
|
390
|
+
const documentSourcePart = (part) => {
|
|
391
|
+
const source = decodeDocumentSourcePart({
|
|
392
|
+
type: "source",
|
|
393
|
+
sourceType: "document",
|
|
394
|
+
id: part.id,
|
|
395
|
+
mediaType: part.mediaType,
|
|
396
|
+
title: part.title,
|
|
397
|
+
fileName: part.filename,
|
|
398
|
+
metadata: aiSdkMetadata(part)
|
|
399
|
+
});
|
|
400
|
+
return Option.match(source, {
|
|
401
|
+
onNone: () => [metadataPart(part, { reason: "invalid_document_source" })],
|
|
402
|
+
onSome: (sourcePart) => [sourcePart]
|
|
403
|
+
});
|
|
404
|
+
};
|
|
405
|
+
const toolCallDetails = (part) => ({
|
|
406
|
+
dynamic: part.dynamic ?? false,
|
|
407
|
+
title: part.title ?? null,
|
|
408
|
+
toolMetadata: jsonOrOmitted(part.toolMetadata)
|
|
409
|
+
});
|
|
410
|
+
const partToParts = (part) => {
|
|
411
|
+
switch (part.type) {
|
|
412
|
+
case "text-start": return [Response.makePart("text-start", {
|
|
413
|
+
id: part.id,
|
|
414
|
+
metadata: aiSdkMetadata(part)
|
|
415
|
+
})];
|
|
416
|
+
case "text-delta": return [Response.makePart("text-delta", {
|
|
417
|
+
id: part.id,
|
|
418
|
+
delta: part.text,
|
|
419
|
+
metadata: aiSdkMetadata(part)
|
|
420
|
+
})];
|
|
421
|
+
case "text-end": return [Response.makePart("text-end", {
|
|
422
|
+
id: part.id,
|
|
423
|
+
metadata: aiSdkMetadata(part)
|
|
424
|
+
})];
|
|
425
|
+
case "reasoning-start": return [Response.makePart("reasoning-start", {
|
|
426
|
+
id: part.id,
|
|
427
|
+
metadata: aiSdkMetadata(part)
|
|
428
|
+
})];
|
|
429
|
+
case "reasoning-delta": return [Response.makePart("reasoning-delta", {
|
|
430
|
+
id: part.id,
|
|
431
|
+
delta: part.text,
|
|
432
|
+
metadata: aiSdkMetadata(part)
|
|
433
|
+
})];
|
|
434
|
+
case "reasoning-end": return [Response.makePart("reasoning-end", {
|
|
435
|
+
id: part.id,
|
|
436
|
+
metadata: aiSdkMetadata(part)
|
|
437
|
+
})];
|
|
438
|
+
case "tool-input-start": return [Response.makePart("tool-params-start", {
|
|
439
|
+
id: part.id,
|
|
440
|
+
name: part.toolName,
|
|
441
|
+
providerExecuted: part.providerExecuted ?? false,
|
|
442
|
+
metadata: aiSdkMetadata(part, {
|
|
443
|
+
dynamic: part.dynamic ?? false,
|
|
444
|
+
title: part.title ?? null,
|
|
445
|
+
toolMetadata: jsonOrOmitted(part.toolMetadata)
|
|
446
|
+
})
|
|
447
|
+
})];
|
|
448
|
+
case "tool-input-delta": return [Response.makePart("tool-params-delta", {
|
|
449
|
+
id: part.id,
|
|
450
|
+
delta: part.delta,
|
|
451
|
+
metadata: aiSdkMetadata(part)
|
|
452
|
+
})];
|
|
453
|
+
case "tool-input-end": return [Response.makePart("tool-params-end", {
|
|
454
|
+
id: part.id,
|
|
455
|
+
metadata: aiSdkMetadata(part)
|
|
456
|
+
})];
|
|
457
|
+
case "tool-call": return [Response.toolCallPart({
|
|
458
|
+
id: part.toolCallId,
|
|
459
|
+
name: part.toolName,
|
|
460
|
+
params: part.input,
|
|
461
|
+
providerExecuted: part.providerExecuted ?? false,
|
|
462
|
+
metadata: aiSdkMetadata(part, toolCallDetails(part))
|
|
463
|
+
})];
|
|
464
|
+
case "tool-result": return [Response.toolResultPart({
|
|
465
|
+
id: part.toolCallId,
|
|
466
|
+
name: part.toolName,
|
|
467
|
+
isFailure: false,
|
|
468
|
+
result: part.output,
|
|
469
|
+
encodedResult: part.output,
|
|
470
|
+
providerExecuted: part.providerExecuted ?? false,
|
|
471
|
+
preliminary: part.preliminary ?? false,
|
|
472
|
+
metadata: aiSdkMetadata(part, toolCallDetails(part))
|
|
473
|
+
})];
|
|
474
|
+
case "tool-error": {
|
|
475
|
+
const result = {
|
|
476
|
+
error: part.error,
|
|
477
|
+
input: part.input
|
|
478
|
+
};
|
|
479
|
+
return [Response.toolResultPart({
|
|
480
|
+
id: part.toolCallId,
|
|
481
|
+
name: part.toolName,
|
|
482
|
+
isFailure: true,
|
|
483
|
+
result,
|
|
484
|
+
encodedResult: result,
|
|
485
|
+
providerExecuted: part.providerExecuted ?? false,
|
|
486
|
+
preliminary: false,
|
|
487
|
+
metadata: aiSdkMetadata(part, toolCallDetails(part))
|
|
488
|
+
})];
|
|
489
|
+
}
|
|
490
|
+
case "tool-output-denied": {
|
|
491
|
+
const result = { denied: true };
|
|
492
|
+
return [Response.toolResultPart({
|
|
493
|
+
id: part.toolCallId,
|
|
494
|
+
name: part.toolName,
|
|
495
|
+
isFailure: true,
|
|
496
|
+
result,
|
|
497
|
+
encodedResult: result,
|
|
498
|
+
providerExecuted: part.providerExecuted ?? false,
|
|
499
|
+
preliminary: false,
|
|
500
|
+
metadata: aiSdkMetadata(part, { dynamic: part.dynamic ?? false })
|
|
501
|
+
})];
|
|
502
|
+
}
|
|
503
|
+
case "tool-approval-request": return [Response.toolApprovalRequestPart({
|
|
504
|
+
approvalId: part.approvalId,
|
|
505
|
+
toolCallId: part.toolCall.toolCallId,
|
|
506
|
+
metadata: aiSdkMetadata(part, {
|
|
507
|
+
isAutomatic: part.isAutomatic ?? false,
|
|
508
|
+
signature: part.signature ?? null,
|
|
509
|
+
toolCall: jsonOrOmitted(part.toolCall)
|
|
510
|
+
})
|
|
511
|
+
})];
|
|
512
|
+
case "tool-approval-response": return [metadataPart(part, {
|
|
513
|
+
approvalId: part.approvalId,
|
|
514
|
+
approved: part.approved,
|
|
515
|
+
providerExecuted: part.providerExecuted ?? false,
|
|
516
|
+
reason: part.reason ?? null,
|
|
517
|
+
toolCall: jsonOrOmitted(part.toolCall)
|
|
518
|
+
})];
|
|
519
|
+
case "file":
|
|
520
|
+
case "reasoning-file": return filePart(part);
|
|
521
|
+
case "source": return sourcePart(part);
|
|
522
|
+
case "finish": return [Response.makePart("finish", {
|
|
523
|
+
reason: finishReason(part.finishReason),
|
|
524
|
+
usage: usageFromAiSdk(part.totalUsage),
|
|
525
|
+
response: void 0,
|
|
526
|
+
metadata: aiSdkMetadata(part, { rawFinishReason: part.rawFinishReason ?? null })
|
|
527
|
+
})];
|
|
528
|
+
case "abort": return [Response.makePart("finish", {
|
|
529
|
+
reason: "unknown",
|
|
530
|
+
usage: emptyUsage(),
|
|
531
|
+
response: void 0,
|
|
532
|
+
metadata: aiSdkMetadata(part, { reason: part.reason ?? null })
|
|
533
|
+
})];
|
|
534
|
+
case "error": return [Response.makePart("error", {
|
|
535
|
+
error: part.error,
|
|
536
|
+
metadata: aiSdkMetadata(part)
|
|
537
|
+
})];
|
|
538
|
+
case "start": return [metadataPart(part)];
|
|
539
|
+
case "start-step": return [metadataPart(part, {
|
|
540
|
+
request: jsonOrOmitted(part.request),
|
|
541
|
+
warnings: jsonOrOmitted(part.warnings)
|
|
542
|
+
})];
|
|
543
|
+
case "finish-step": return [metadataPart(part, {
|
|
544
|
+
finishReason: part.finishReason,
|
|
545
|
+
performance: jsonOrOmitted(part.performance),
|
|
546
|
+
rawFinishReason: part.rawFinishReason ?? null,
|
|
547
|
+
response: jsonOrOmitted(part.response),
|
|
548
|
+
usage: jsonOrOmitted(part.usage)
|
|
549
|
+
})];
|
|
550
|
+
case "custom": return [metadataPart(part, { kind: part.kind })];
|
|
551
|
+
case "raw": return [metadataPart(part, { rawValue: jsonOrOmitted(part.rawValue) })];
|
|
552
|
+
}
|
|
553
|
+
};
|
|
554
|
+
const transform = Effect.fn(function* (stream) {
|
|
555
|
+
return yield* Effect.succeed(stream.pipe(Stream.flatMap((part) => Stream.fromIterable(partToParts(part)))));
|
|
556
|
+
}, Stream.unwrap);
|
|
557
|
+
const fromAiStream = Effect.fn(function* (stream) {
|
|
558
|
+
return yield* Effect.succeed(Stream.fromAsyncIterable(stream, (cause) => AiSdkStreamError.make({ cause })).pipe(transform));
|
|
559
|
+
}, Stream.unwrap);
|
|
560
|
+
//#endregion
|
|
561
|
+
//#region src/agent/builtin/ai-sdk/export.ts
|
|
562
|
+
var export_exports$2 = /* @__PURE__ */ __exportAll({
|
|
563
|
+
AiSdkStreamError: () => AiSdkStreamError,
|
|
564
|
+
fromAiStream: () => fromAiStream,
|
|
565
|
+
transform: () => transform
|
|
566
|
+
});
|
|
567
|
+
//#endregion
|
|
568
|
+
//#region src/agent/export.ts
|
|
569
|
+
var export_exports = /* @__PURE__ */ __exportAll({
|
|
570
|
+
AgentError: () => AgentError,
|
|
571
|
+
Ai: () => export_exports$2,
|
|
572
|
+
AssistantMessage: () => AssistantMessage,
|
|
573
|
+
Dummy: () => dummy_exports,
|
|
574
|
+
Effect: () => export_exports$3,
|
|
575
|
+
Message: () => Message,
|
|
576
|
+
ProviderService: () => ProviderService$1,
|
|
577
|
+
SystemMessage: () => SystemMessage,
|
|
578
|
+
ToolMessage: () => ToolMessage,
|
|
579
|
+
Trajectory: () => Trajectory,
|
|
580
|
+
UserMessage: () => UserMessage
|
|
581
|
+
});
|
|
582
|
+
//#endregion
|
|
583
|
+
export { export_exports as Agent, export_exports$1 as Sandbox };
|
|
584
|
+
|
|
585
|
+
//# sourceMappingURL=index.mjs.map
|