coding-agents-sdk 0.0.1 → 0.2.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/README.md +242 -0
- package/dist/Agent-D8WkUilj.mjs +262 -0
- package/dist/SdkAgent-B47mJiIE.mjs +38 -0
- package/dist/adapters/claude-code-cli/index.d.mts +2 -0
- package/dist/adapters/claude-code-cli/index.mjs +490 -0
- package/dist/adapters/claude-code-sdk/index.d.mts +2 -0
- package/dist/adapters/claude-code-sdk/index.mjs +483 -0
- package/dist/adapters/codex-cli/index.d.mts +2 -0
- package/dist/adapters/codex-cli/index.mjs +626 -0
- package/dist/adapters/codex-sdk/index.d.mts +2 -0
- package/dist/adapters/codex-sdk/index.mjs +286 -0
- package/dist/adapters/gemini-cli/index.d.mts +2 -0
- package/dist/adapters/gemini-cli/index.mjs +292 -0
- package/dist/classify-error-pL6jeu4T.mjs +456 -0
- package/dist/container/index.d.mts +2 -0
- package/dist/container/index.mjs +24 -0
- package/dist/container-2UmPZ0CI.mjs +22 -0
- package/dist/container-CHxKIonn.mjs +440 -0
- package/dist/container-D2Z0ITDJ.mjs +22 -0
- package/dist/diff-De8d3MVb.mjs +333 -0
- package/dist/errors-BAmHDQu8.mjs +45 -0
- package/dist/events-nxuRbYIu.d.mts +239 -0
- package/dist/index-B3YqrgIp.d.mts +45 -0
- package/dist/index-ByAOGMUM.d.mts +224 -0
- package/dist/index-C3ZxLAd0.d.mts +315 -0
- package/dist/index-CFpNOmdA.d.mts +145 -0
- package/dist/index-dRVpEAr8.d.mts +39 -0
- package/dist/index-nzo1sBiK.d.mts +110 -0
- package/dist/index.d.mts +16 -0
- package/dist/index.mjs +61 -0
- package/dist/oci-DMZZQZ47.mjs +438 -0
- package/dist/schemas/index.d.mts +2 -0
- package/dist/schemas/index.mjs +2 -0
- package/dist/schemas-DwD4pwJB.mjs +96 -0
- package/dist/spawner-Bw9UBEGX.mjs +54 -0
- package/dist/structured-output-BHtr_zpz.mjs +19 -0
- package/dist/types-Cb_EXIEe.d.mts +177 -0
- package/dist/types-aNMD8h3x.mjs +19 -0
- package/dist/util-B4RQZkKr.mjs +77 -0
- package/package.json +86 -9
- package/index.js +0 -7
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { n as AgentValidationError, t as AgentRunError } from "./types-aNMD8h3x.mjs";
|
|
2
|
+
import { z } from "zod/v4";
|
|
3
|
+
import { delimiter } from "node:path";
|
|
4
|
+
//#region src/core/events.ts
|
|
5
|
+
const generateRunId = () => crypto.randomUUID();
|
|
6
|
+
const generateEventId = () => `evt_${crypto.randomUUID()}`;
|
|
7
|
+
const toTextPart = (text) => ({
|
|
8
|
+
type: "text",
|
|
9
|
+
text
|
|
10
|
+
});
|
|
11
|
+
const getTextFromContent = (content) => {
|
|
12
|
+
return content.flatMap((part) => part.type === "text" ? [part.text] : []).join("\n");
|
|
13
|
+
};
|
|
14
|
+
const getInputText = (input) => {
|
|
15
|
+
return input.flatMap((part) => part.type === "text" ? [part.text] : []).join("\n\n");
|
|
16
|
+
};
|
|
17
|
+
const createEventParser = (schema) => (raw) => {
|
|
18
|
+
const result = schema.safeParse(raw);
|
|
19
|
+
return result.success ? result.data : null;
|
|
20
|
+
};
|
|
21
|
+
const createAgentEvent = (runId, type, fields) => {
|
|
22
|
+
return {
|
|
23
|
+
id: fields.id ?? generateEventId(),
|
|
24
|
+
runId,
|
|
25
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
26
|
+
type,
|
|
27
|
+
...fields
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
//#endregion
|
|
31
|
+
//#region src/core/process/env.ts
|
|
32
|
+
const resolvePathKey = (env) => Object.keys(env).find((key) => key.toLowerCase() === "path") ?? "PATH";
|
|
33
|
+
const buildBaseProcessEnv = (options = {}) => {
|
|
34
|
+
const env = options.propagateEnv === false ? {} : Object.fromEntries(Object.entries(process.env).filter((entry) => entry[1] !== void 0));
|
|
35
|
+
const extraPaths = options.extraPaths ?? [];
|
|
36
|
+
if (extraPaths.length > 0) {
|
|
37
|
+
const pathKey = resolvePathKey(env);
|
|
38
|
+
const basePath = env[pathKey] ?? "";
|
|
39
|
+
env[pathKey] = [...extraPaths, basePath].filter(Boolean).join(delimiter);
|
|
40
|
+
}
|
|
41
|
+
return env;
|
|
42
|
+
};
|
|
43
|
+
//#endregion
|
|
44
|
+
//#region src/core/agent/request.ts
|
|
45
|
+
const isZodSchema = (schema) => schema instanceof z.ZodType;
|
|
46
|
+
const normalizeSchema = (request) => {
|
|
47
|
+
if (!("schema" in request)) return { kind: "none" };
|
|
48
|
+
const { schema } = request;
|
|
49
|
+
if (isZodSchema(schema)) return {
|
|
50
|
+
kind: "zod",
|
|
51
|
+
jsonSchema: z.toJSONSchema(schema),
|
|
52
|
+
zodSchema: schema
|
|
53
|
+
};
|
|
54
|
+
return {
|
|
55
|
+
kind: "json-schema",
|
|
56
|
+
jsonSchema: schema
|
|
57
|
+
};
|
|
58
|
+
};
|
|
59
|
+
const normalizeRunRequest = (type, capabilities, defaults, request) => {
|
|
60
|
+
const input = typeof request.input === "string" ? [{
|
|
61
|
+
type: "text",
|
|
62
|
+
text: request.input
|
|
63
|
+
}] : request.input;
|
|
64
|
+
if (input.length === 0) throw new AgentValidationError("Run input cannot be empty.");
|
|
65
|
+
if (input.every((part) => part.type === "text" && part.text.trim() === "")) throw new AgentValidationError("Run input cannot be empty.");
|
|
66
|
+
if (!capabilities.imageInput && input.some((part) => part.type === "image")) throw new AgentValidationError(`${type} does not support image input.`);
|
|
67
|
+
const schema = normalizeSchema(request);
|
|
68
|
+
if (!capabilities.structuredOutput && schema.kind !== "none") throw new AgentValidationError(`${type} does not support structured output.`);
|
|
69
|
+
if (request.timeoutMs !== void 0 && (!Number.isFinite(request.timeoutMs) || request.timeoutMs <= 0)) throw new AgentValidationError("timeoutMs must be a positive finite number.");
|
|
70
|
+
return {
|
|
71
|
+
input,
|
|
72
|
+
schema,
|
|
73
|
+
cwd: request.cwd ?? defaults?.cwd,
|
|
74
|
+
model: request.model ?? defaults?.model,
|
|
75
|
+
systemPrompt: request.systemPrompt ?? defaults?.systemPrompt,
|
|
76
|
+
timeoutMs: request.timeoutMs,
|
|
77
|
+
signal: request.signal
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
//#endregion
|
|
81
|
+
//#region src/core/agent/run-state.ts
|
|
82
|
+
const finalizeStats = (run, endedAt) => {
|
|
83
|
+
if (!run.stats) return;
|
|
84
|
+
const durationMs = Math.max(0, endedAt.getTime() - run.startedAt.getTime());
|
|
85
|
+
run.stats = {
|
|
86
|
+
...run.stats,
|
|
87
|
+
durationMs: run.stats.durationMs ?? durationMs,
|
|
88
|
+
apiDurationMs: run.stats.apiDurationMs ?? durationMs
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
const applyStructuredOutput = (run, structuredOutput) => {
|
|
92
|
+
const schema = run.normalizedRequest.schema;
|
|
93
|
+
if (schema.kind === "none") return;
|
|
94
|
+
if (schema.kind === "zod") {
|
|
95
|
+
const parsed = schema.zodSchema.safeParse(structuredOutput);
|
|
96
|
+
if (!parsed.success) {
|
|
97
|
+
run.status = "failed";
|
|
98
|
+
run.error = {
|
|
99
|
+
kind: "parse",
|
|
100
|
+
message: parsed.error.message
|
|
101
|
+
};
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
run.output.value = parsed.data;
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
run.output.value = structuredOutput;
|
|
108
|
+
};
|
|
109
|
+
const recordRunEvent = (run, event) => {
|
|
110
|
+
run.events.push(event);
|
|
111
|
+
if (event.type !== "message" || event.role !== "assistant") return;
|
|
112
|
+
const text = event.text ?? getTextFromContent(event.content);
|
|
113
|
+
if (text) run.assistantTexts.push(text);
|
|
114
|
+
};
|
|
115
|
+
const applyMappedEventBatch = (run, batch, options = {}) => {
|
|
116
|
+
if (batch.sessionId) {
|
|
117
|
+
run.sessionId = batch.sessionId;
|
|
118
|
+
options.onSessionId?.(batch.sessionId);
|
|
119
|
+
}
|
|
120
|
+
for (const toolCall of batch.toolCalls ?? []) run.toolCalls.set(toolCall.toolCallId, {
|
|
121
|
+
toolName: toolCall.toolName,
|
|
122
|
+
input: toolCall.input
|
|
123
|
+
});
|
|
124
|
+
if (!batch.state) return;
|
|
125
|
+
const patch = batch.state;
|
|
126
|
+
if (patch.output) {
|
|
127
|
+
if ("text" in patch.output) run.output.text = patch.output.text;
|
|
128
|
+
if ("value" in patch.output) applyStructuredOutput(run, patch.output.value);
|
|
129
|
+
}
|
|
130
|
+
if ("stats" in patch) run.stats = patch.stats;
|
|
131
|
+
if ("status" in patch) run.status = patch.status;
|
|
132
|
+
if ("error" in patch) run.error = patch.error;
|
|
133
|
+
};
|
|
134
|
+
const hasMeaningfulEvent = (run) => {
|
|
135
|
+
return run.events.some((event) => event.type === "message" || event.type === "reasoning" || event.type === "tool-call" || event.type === "tool-result");
|
|
136
|
+
};
|
|
137
|
+
const resolveRunStatus = (run) => {
|
|
138
|
+
const missingStructuredOutput = run.normalizedRequest.schema.kind !== "none" && run.output.value === void 0;
|
|
139
|
+
if (run.stopReason === "user" || run.stopReason === "abort" || run.stopReason === "timeout") return "cancelled";
|
|
140
|
+
if (run.error) return run.error.kind === "abort" ? "cancelled" : "failed";
|
|
141
|
+
if (run.status) {
|
|
142
|
+
if (run.status === "completed" && missingStructuredOutput) return "failed";
|
|
143
|
+
return run.status;
|
|
144
|
+
}
|
|
145
|
+
if (run.processResult && run.processResult.exitCode !== null && run.processResult.exitCode !== 0) return "failed";
|
|
146
|
+
if (!hasMeaningfulEvent(run) && run.stderrLines.length > 0) return "failed";
|
|
147
|
+
if (missingStructuredOutput) return "failed";
|
|
148
|
+
return "completed";
|
|
149
|
+
};
|
|
150
|
+
const resolveRunError = (run, status) => {
|
|
151
|
+
const processResult = run.processResult;
|
|
152
|
+
const stderrText = run.stderrLines.join("\n");
|
|
153
|
+
if (run.stopReason === "user") return {
|
|
154
|
+
kind: "abort",
|
|
155
|
+
message: "Run stopped by user."
|
|
156
|
+
};
|
|
157
|
+
if (run.stopReason === "abort") return {
|
|
158
|
+
kind: "abort",
|
|
159
|
+
message: "Run aborted."
|
|
160
|
+
};
|
|
161
|
+
if (run.stopReason === "timeout") return {
|
|
162
|
+
kind: "abort",
|
|
163
|
+
message: `Run timed out after ${run.normalizedRequest.timeoutMs}ms.`
|
|
164
|
+
};
|
|
165
|
+
if (run.error) return run.error;
|
|
166
|
+
if (processResult && processResult.exitCode !== null && processResult.exitCode !== 0) return {
|
|
167
|
+
kind: "process",
|
|
168
|
+
message: stderrText || `Process exited with code ${processResult.exitCode}.`
|
|
169
|
+
};
|
|
170
|
+
if (status === "failed" && run.stderrLines.length > 0) return {
|
|
171
|
+
kind: "process",
|
|
172
|
+
message: stderrText
|
|
173
|
+
};
|
|
174
|
+
if (status === "failed" && run.normalizedRequest.schema.kind !== "none" && run.output.value === void 0) return {
|
|
175
|
+
kind: "parse",
|
|
176
|
+
message: "Structured output was requested but no structured output value was produced."
|
|
177
|
+
};
|
|
178
|
+
};
|
|
179
|
+
const buildRunResult = (type, run, endedAt, status, error) => {
|
|
180
|
+
const stderrText = run.stderrLines.join("\n");
|
|
181
|
+
const base = {
|
|
182
|
+
type,
|
|
183
|
+
runId: run.runId,
|
|
184
|
+
sessionId: run.sessionId,
|
|
185
|
+
text: run.output.text ?? run.assistantTexts.join("\n\n"),
|
|
186
|
+
events: [...run.events],
|
|
187
|
+
stats: run.stats,
|
|
188
|
+
timing: {
|
|
189
|
+
startedAt: run.startedAt,
|
|
190
|
+
endedAt,
|
|
191
|
+
durationMs: endedAt.getTime() - run.startedAt.getTime()
|
|
192
|
+
},
|
|
193
|
+
process: run.processResult ? {
|
|
194
|
+
exitCode: run.processResult.exitCode,
|
|
195
|
+
signal: run.processResult.signal,
|
|
196
|
+
stderr: stderrText
|
|
197
|
+
} : void 0
|
|
198
|
+
};
|
|
199
|
+
switch (status) {
|
|
200
|
+
case "completed":
|
|
201
|
+
if (run.normalizedRequest.schema.kind === "none") return {
|
|
202
|
+
...base,
|
|
203
|
+
status,
|
|
204
|
+
error: void 0,
|
|
205
|
+
cancelReason: void 0
|
|
206
|
+
};
|
|
207
|
+
return {
|
|
208
|
+
...base,
|
|
209
|
+
status,
|
|
210
|
+
output: run.output.value,
|
|
211
|
+
error: void 0,
|
|
212
|
+
cancelReason: void 0
|
|
213
|
+
};
|
|
214
|
+
case "failed": return {
|
|
215
|
+
...base,
|
|
216
|
+
status,
|
|
217
|
+
output: void 0,
|
|
218
|
+
error: error ?? {
|
|
219
|
+
kind: "process",
|
|
220
|
+
message: "Unknown error."
|
|
221
|
+
},
|
|
222
|
+
cancelReason: void 0
|
|
223
|
+
};
|
|
224
|
+
case "cancelled": return {
|
|
225
|
+
...base,
|
|
226
|
+
status,
|
|
227
|
+
output: void 0,
|
|
228
|
+
error: error ?? {
|
|
229
|
+
kind: "abort",
|
|
230
|
+
message: "Run cancelled."
|
|
231
|
+
},
|
|
232
|
+
cancelReason: run.stopReason ?? "user"
|
|
233
|
+
};
|
|
234
|
+
default: throw new Error("Unsupported run status.");
|
|
235
|
+
}
|
|
236
|
+
};
|
|
237
|
+
//#endregion
|
|
238
|
+
//#region src/core/agent/AgentCore.ts
|
|
239
|
+
const createInitialMetrics = () => {
|
|
240
|
+
const now = /* @__PURE__ */ new Date();
|
|
241
|
+
return {
|
|
242
|
+
runCount: 0,
|
|
243
|
+
messagesReceived: 0,
|
|
244
|
+
toolCalls: 0,
|
|
245
|
+
errors: 0,
|
|
246
|
+
startedAt: now,
|
|
247
|
+
lastActivityAt: now
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
var AgentCore = class {
|
|
251
|
+
type;
|
|
252
|
+
capabilities;
|
|
253
|
+
currentRun;
|
|
254
|
+
eventHandlers = [];
|
|
255
|
+
_sessionId;
|
|
256
|
+
_metrics;
|
|
257
|
+
constructor(options) {
|
|
258
|
+
this.type = options.type;
|
|
259
|
+
this.capabilities = options.capabilities;
|
|
260
|
+
this._sessionId = options.sessionId;
|
|
261
|
+
this._metrics = createInitialMetrics();
|
|
262
|
+
if (options.onEvent) this.onEvent(options.onEvent);
|
|
263
|
+
}
|
|
264
|
+
onValidateRun(_request, _normalizedRequest) {}
|
|
265
|
+
async onDispose() {}
|
|
266
|
+
onHandlerError(_error) {}
|
|
267
|
+
get sessionId() {
|
|
268
|
+
return this._sessionId;
|
|
269
|
+
}
|
|
270
|
+
get metrics() {
|
|
271
|
+
return this._metrics;
|
|
272
|
+
}
|
|
273
|
+
get isRunning() {
|
|
274
|
+
return this.currentRun?.running ?? false;
|
|
275
|
+
}
|
|
276
|
+
onEvent(handler) {
|
|
277
|
+
this.eventHandlers.push(handler);
|
|
278
|
+
return () => {
|
|
279
|
+
const index = this.eventHandlers.indexOf(handler);
|
|
280
|
+
if (index >= 0) this.eventHandlers.splice(index, 1);
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
async run(request) {
|
|
284
|
+
if (this.currentRun?.running) throw new AgentValidationError("This agent is already running. Wait for it to finish first.");
|
|
285
|
+
const normalizedRequest = normalizeRunRequest(this.type, this.capabilities, this.runRequestDefaults, request);
|
|
286
|
+
this.onValidateRun(request, normalizedRequest);
|
|
287
|
+
const context = {
|
|
288
|
+
runId: generateRunId(),
|
|
289
|
+
startedAt: /* @__PURE__ */ new Date(),
|
|
290
|
+
requestedSessionId: this._sessionId,
|
|
291
|
+
sessionId: this._sessionId,
|
|
292
|
+
normalizedRequest,
|
|
293
|
+
events: [],
|
|
294
|
+
stderrLines: [],
|
|
295
|
+
assistantTexts: [],
|
|
296
|
+
toolCalls: /* @__PURE__ */ new Map(),
|
|
297
|
+
output: {}
|
|
298
|
+
};
|
|
299
|
+
const activeRun = {
|
|
300
|
+
context,
|
|
301
|
+
running: true,
|
|
302
|
+
promise: new Promise(() => {})
|
|
303
|
+
};
|
|
304
|
+
this._metrics.runCount++;
|
|
305
|
+
const promise = Promise.resolve().then(() => this.executeRunCore(context, request)).finally(() => {
|
|
306
|
+
activeRun.running = false;
|
|
307
|
+
});
|
|
308
|
+
activeRun.promise = promise;
|
|
309
|
+
this.currentRun = activeRun;
|
|
310
|
+
return promise;
|
|
311
|
+
}
|
|
312
|
+
async runOrThrow(request) {
|
|
313
|
+
const result = await this.run(request);
|
|
314
|
+
if (result.status !== "completed") throw new AgentRunError(result);
|
|
315
|
+
return result;
|
|
316
|
+
}
|
|
317
|
+
async stop() {
|
|
318
|
+
const run = this.currentRun?.context;
|
|
319
|
+
if (!run || !this.currentRun?.running) return void 0;
|
|
320
|
+
this.requestStop(run, "user");
|
|
321
|
+
try {
|
|
322
|
+
return await this.currentRun.promise;
|
|
323
|
+
} catch {
|
|
324
|
+
return;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
reset() {
|
|
328
|
+
if (this.currentRun?.running) throw new AgentValidationError("Cannot reset agent state while a run is active. Call stop() and wait() first.");
|
|
329
|
+
this.currentRun = void 0;
|
|
330
|
+
this._sessionId = void 0;
|
|
331
|
+
this._metrics = createInitialMetrics();
|
|
332
|
+
}
|
|
333
|
+
wait() {
|
|
334
|
+
if (!this.currentRun) throw new AgentValidationError("There is no run to wait for.");
|
|
335
|
+
return this.currentRun.promise;
|
|
336
|
+
}
|
|
337
|
+
async dispose() {
|
|
338
|
+
if (this.currentRun?.running) {
|
|
339
|
+
this.requestStop(this.currentRun.context, "user");
|
|
340
|
+
try {
|
|
341
|
+
await this.currentRun.promise;
|
|
342
|
+
} catch {}
|
|
343
|
+
}
|
|
344
|
+
await this.onDispose();
|
|
345
|
+
}
|
|
346
|
+
async [Symbol.asyncDispose]() {
|
|
347
|
+
await this.dispose();
|
|
348
|
+
}
|
|
349
|
+
applyBatch(run, batch) {
|
|
350
|
+
applyMappedEventBatch(run, batch, { onSessionId: (sessionId) => {
|
|
351
|
+
this.setSessionId(sessionId);
|
|
352
|
+
} });
|
|
353
|
+
for (const event of batch.events) this.emit(run, event);
|
|
354
|
+
}
|
|
355
|
+
setupRunLifecycle(run) {
|
|
356
|
+
let runSettled = false;
|
|
357
|
+
const abortHandler = () => {
|
|
358
|
+
if (!runSettled) this.requestStop(run, "abort");
|
|
359
|
+
};
|
|
360
|
+
const timeoutHandle = run.normalizedRequest.timeoutMs === void 0 ? void 0 : setTimeout(() => {
|
|
361
|
+
if (!runSettled) this.requestStop(run, "timeout");
|
|
362
|
+
}, run.normalizedRequest.timeoutMs);
|
|
363
|
+
timeoutHandle?.unref?.();
|
|
364
|
+
run.normalizedRequest.signal?.addEventListener("abort", abortHandler, { once: true });
|
|
365
|
+
const abortedBeforeStart = run.normalizedRequest.signal?.aborted === true;
|
|
366
|
+
if (abortedBeforeStart) this.requestStop(run, "abort");
|
|
367
|
+
const cleanup = () => {
|
|
368
|
+
runSettled = true;
|
|
369
|
+
if (timeoutHandle) clearTimeout(timeoutHandle);
|
|
370
|
+
run.normalizedRequest.signal?.removeEventListener("abort", abortHandler);
|
|
371
|
+
};
|
|
372
|
+
return {
|
|
373
|
+
abortedBeforeStart,
|
|
374
|
+
cleanup
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
emitSessionStart(run) {
|
|
378
|
+
if (run.sessionStarted) return;
|
|
379
|
+
run.sessionStarted = true;
|
|
380
|
+
this.emit(run, createAgentEvent(run.runId, "session-start", {
|
|
381
|
+
requestedSessionId: run.requestedSessionId,
|
|
382
|
+
sessionId: run.sessionId,
|
|
383
|
+
resumed: run.requestedSessionId !== void 0
|
|
384
|
+
}));
|
|
385
|
+
}
|
|
386
|
+
emit(run, event) {
|
|
387
|
+
recordRunEvent(run, event);
|
|
388
|
+
this.updateMetrics(event);
|
|
389
|
+
for (const handler of this.eventHandlers) try {
|
|
390
|
+
handler(event);
|
|
391
|
+
} catch (error) {
|
|
392
|
+
this.onHandlerError(error);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
requestStop(run, reason) {
|
|
396
|
+
if (run.stopReason) return;
|
|
397
|
+
run.stopReason = reason;
|
|
398
|
+
this.onStopRequested(run);
|
|
399
|
+
}
|
|
400
|
+
buildResult(run, endedAt) {
|
|
401
|
+
finalizeStats(run, endedAt);
|
|
402
|
+
const status = resolveRunStatus(run);
|
|
403
|
+
const error = resolveRunError(run, status);
|
|
404
|
+
if (run.sessionStarted) this.emit(run, createAgentEvent(run.runId, "session-end", {
|
|
405
|
+
sessionId: run.sessionId,
|
|
406
|
+
status,
|
|
407
|
+
error: error?.message
|
|
408
|
+
}));
|
|
409
|
+
return buildRunResult(this.type, run, endedAt, status, error);
|
|
410
|
+
}
|
|
411
|
+
setSessionId(sessionId) {
|
|
412
|
+
this._sessionId = sessionId;
|
|
413
|
+
}
|
|
414
|
+
updateMetrics(event) {
|
|
415
|
+
this._metrics.lastActivityAt = event.timestamp;
|
|
416
|
+
switch (event.type) {
|
|
417
|
+
case "message":
|
|
418
|
+
if (event.role === "assistant") this._metrics.messagesReceived++;
|
|
419
|
+
break;
|
|
420
|
+
case "tool-call":
|
|
421
|
+
this._metrics.toolCalls++;
|
|
422
|
+
break;
|
|
423
|
+
case "session-end":
|
|
424
|
+
if (event.status === "failed") this._metrics.errors++;
|
|
425
|
+
break;
|
|
426
|
+
default: break;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
};
|
|
430
|
+
//#endregion
|
|
431
|
+
//#region src/core/agent/classify-error.ts
|
|
432
|
+
const SPAWN_CODES = new Set(["ENOENT", "ERR_MODULE_NOT_FOUND"]);
|
|
433
|
+
const hasCode = (error) => "code" in error;
|
|
434
|
+
const classifyRunError = (error, message) => {
|
|
435
|
+
const msg = message ?? (error instanceof Error ? error.message : String(error));
|
|
436
|
+
if (error instanceof Error) {
|
|
437
|
+
if (error.name === "AbortError") return {
|
|
438
|
+
kind: "abort",
|
|
439
|
+
message: msg
|
|
440
|
+
};
|
|
441
|
+
if (hasCode(error) && SPAWN_CODES.has(String(error.code))) return {
|
|
442
|
+
kind: "spawn",
|
|
443
|
+
message: msg
|
|
444
|
+
};
|
|
445
|
+
if (error instanceof SyntaxError) return {
|
|
446
|
+
kind: "parse",
|
|
447
|
+
message: msg
|
|
448
|
+
};
|
|
449
|
+
}
|
|
450
|
+
return {
|
|
451
|
+
kind: "provider",
|
|
452
|
+
message: msg
|
|
453
|
+
};
|
|
454
|
+
};
|
|
455
|
+
//#endregion
|
|
456
|
+
export { buildBaseProcessEnv as a, getInputText as c, hasMeaningfulEvent as i, toTextPart as l, AgentCore as n, createAgentEvent as o, applyMappedEventBatch as r, createEventParser as s, classifyRunError as t };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { A as ContainerBase, B as ExecResult, C as E2BProviderConfig, D as DockerContainerBase, E as DockerContainer, F as ContainerLifecycle, G as WorkdirDiffStats, H as VolumeMount, I as ContainerObservability, J as WorkdirSnapshotOptions, K as WorkdirFileChange, L as ContainerStatus, M as ContainerExecution, N as ContainerFileSystem, O as DockerContainerConfig, P as ContainerIdentity, R as ContainerType, S as E2BCreateConfig, T as DockerBuildConfig, U as WorkdirChangeKind, V as PortMapping, W as WorkdirDiff, _ as E2BConnectConfig, a as ContainerDiffError, b as E2BContainerBase, c as ContainerExecError, d as PodmanBuildBase, f as PodmanBuildConfig, g as PodmanProviderConfig, h as PodmanContainerConfig, i as ContainerCreateError, j as ContainerConfigBase, k as DockerProviderConfig, l as ContainerNotRunningError, m as PodmanContainerBase, n as ContainerConfig, o as ContainerDisposedError, p as PodmanContainer, q as WorkdirSnapshot, r as createContainer, s as ContainerError, t as Container, u as ContainerStartError, v as E2BConnectionConfig, w as DockerBuildBase, x as E2BContainerConfig, y as E2BContainer, z as ExecOptions } from "../index-ByAOGMUM.mjs";
|
|
2
|
+
export { Container, ContainerBase, ContainerConfig, ContainerConfigBase, ContainerCreateError, ContainerDiffError, ContainerDisposedError, ContainerError, ContainerExecError, ContainerExecution, ContainerFileSystem, ContainerIdentity, ContainerLifecycle, ContainerNotRunningError, ContainerObservability, ContainerStartError, ContainerStatus, ContainerType, DockerBuildBase, DockerBuildConfig, DockerContainer, DockerContainerBase, DockerContainerConfig, DockerProviderConfig, E2BConnectConfig, E2BConnectionConfig, E2BContainer, E2BContainerBase, E2BContainerConfig, E2BCreateConfig, E2BProviderConfig, ExecOptions, ExecResult, PodmanBuildBase, PodmanBuildConfig, PodmanContainer, PodmanContainerBase, PodmanContainerConfig, PodmanProviderConfig, PortMapping, VolumeMount, WorkdirChangeKind, WorkdirDiff, WorkdirDiffStats, WorkdirFileChange, WorkdirSnapshot, WorkdirSnapshotOptions, createContainer };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { a as ContainerExecError, i as ContainerError, n as ContainerDiffError, o as ContainerNotRunningError, r as ContainerDisposedError, s as ContainerStartError, t as ContainerCreateError } from "../errors-BAmHDQu8.mjs";
|
|
2
|
+
//#region src/container/index.ts
|
|
3
|
+
async function createContainer(config) {
|
|
4
|
+
switch (config.type) {
|
|
5
|
+
case "docker": {
|
|
6
|
+
const { createDockerContainer } = await import("../container-2UmPZ0CI.mjs");
|
|
7
|
+
return createDockerContainer(config);
|
|
8
|
+
}
|
|
9
|
+
case "e2b": try {
|
|
10
|
+
const { createE2BContainer } = await import("../container-CHxKIonn.mjs");
|
|
11
|
+
return await createE2BContainer(config);
|
|
12
|
+
} catch (error) {
|
|
13
|
+
if (error instanceof Error && error.message.includes("e2b") && (error.code === "ERR_MODULE_NOT_FOUND" || error.code === "MODULE_NOT_FOUND")) throw new ContainerCreateError("Container type \"e2b\" requires the optional peer dependency \"e2b\". Install it to use this provider.");
|
|
14
|
+
throw error;
|
|
15
|
+
}
|
|
16
|
+
case "podman": {
|
|
17
|
+
const { createPodmanContainer } = await import("../container-D2Z0ITDJ.mjs");
|
|
18
|
+
return createPodmanContainer(config);
|
|
19
|
+
}
|
|
20
|
+
default: throw new ContainerCreateError("Unsupported container type.");
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
//#endregion
|
|
24
|
+
export { ContainerCreateError, ContainerDiffError, ContainerDisposedError, ContainerError, ContainerExecError, ContainerNotRunningError, ContainerStartError, createContainer };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { n as resolveOciImage, t as createOciContainer } from "./oci-DMZZQZ47.mjs";
|
|
2
|
+
//#region src/container/docker/container.ts
|
|
3
|
+
const createDockerContainer = async (config) => {
|
|
4
|
+
const binary = config.binary ?? "docker";
|
|
5
|
+
const image = await resolveOciImage(binary, config);
|
|
6
|
+
return createOciContainer("docker", {
|
|
7
|
+
binary,
|
|
8
|
+
containerName: config.name,
|
|
9
|
+
workdir: config.workdir,
|
|
10
|
+
user: config.user,
|
|
11
|
+
network: config.network,
|
|
12
|
+
platform: config.platform,
|
|
13
|
+
env: config.env,
|
|
14
|
+
ports: config.ports,
|
|
15
|
+
mounts: config.mounts,
|
|
16
|
+
extraArgs: config.extraArgs,
|
|
17
|
+
image,
|
|
18
|
+
builtImage: config.build !== void 0 && !config.build.tag
|
|
19
|
+
}, config);
|
|
20
|
+
};
|
|
21
|
+
//#endregion
|
|
22
|
+
export { createDockerContainer };
|