coding-agents-sdk 0.2.0 → 0.3.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.
Files changed (59) hide show
  1. package/README.md +32 -0
  2. package/dist/SdkAgent-a97nkDuR.mjs +1 -0
  3. package/dist/adapters/claude-code-cli/index.d.mts +1 -1
  4. package/dist/adapters/claude-code-cli/index.mjs +2 -490
  5. package/dist/adapters/claude-code-sdk/index.d.mts +1 -1
  6. package/dist/adapters/claude-code-sdk/index.mjs +4 -483
  7. package/dist/adapters/codex-cli/index.d.mts +1 -1
  8. package/dist/adapters/codex-cli/index.mjs +2 -626
  9. package/dist/adapters/codex-sdk/index.d.mts +1 -1
  10. package/dist/adapters/codex-sdk/index.mjs +1 -286
  11. package/dist/adapters/gemini-cli/index.d.mts +1 -1
  12. package/dist/adapters/gemini-cli/index.mjs +1 -292
  13. package/dist/classify-error-V6x6jP_R.mjs +8 -0
  14. package/dist/container/index.d.mts +1 -1
  15. package/dist/container/index.mjs +1 -24
  16. package/dist/container-BY7JDOGo.mjs +1 -0
  17. package/dist/container-DWTITDhL.mjs +1 -0
  18. package/dist/container-vkn5-3Xl.mjs +1 -0
  19. package/dist/diff-BCfLDXNN.mjs +1 -0
  20. package/dist/env-Dkd8Sgjf.mjs +1 -0
  21. package/dist/errors-XV0frS3r.mjs +1 -0
  22. package/dist/execution-target-BJ5mWjDM.mjs +4 -0
  23. package/dist/{index-C3ZxLAd0.d.mts → index-COHwWTg_.d.mts} +6 -6
  24. package/dist/{index-B3YqrgIp.d.mts → index-CzvanLE_.d.mts} +3 -2
  25. package/dist/index-DCaZeLrI.d.mts +51 -0
  26. package/dist/{index-dRVpEAr8.d.mts → index-DNyKelMh.d.mts} +5 -5
  27. package/dist/{index-nzo1sBiK.d.mts → index-DP4Jxoax.d.mts} +7 -7
  28. package/dist/{index-ByAOGMUM.d.mts → index-R77YR8ak.d.mts} +1 -1
  29. package/dist/{index-CFpNOmdA.d.mts → index-jUwKDQ34.d.mts} +2 -1
  30. package/dist/index.d.mts +10 -9
  31. package/dist/index.mjs +1 -61
  32. package/dist/oci-C_S_FKHj.mjs +1 -0
  33. package/dist/sandbox/index.d.mts +2 -0
  34. package/dist/sandbox/index.mjs +1 -0
  35. package/dist/sandbox-DeKpyenZ.mjs +1 -0
  36. package/dist/schemas/index.d.mts +1 -1
  37. package/dist/schemas/index.mjs +1 -2
  38. package/dist/schemas-BM6qORw3.mjs +1 -0
  39. package/dist/spawner-BJesLc8I.mjs +1 -0
  40. package/dist/structured-output-DEkBQ-1Z.mjs +1 -0
  41. package/dist/{types-Cb_EXIEe.d.mts → types-COlDAzs5.d.mts} +9 -24
  42. package/dist/types-CwvWHEpy.mjs +1 -0
  43. package/dist/util-CGg8cK92.mjs +1 -0
  44. package/package.json +5 -1
  45. package/dist/Agent-D8WkUilj.mjs +0 -262
  46. package/dist/SdkAgent-B47mJiIE.mjs +0 -38
  47. package/dist/classify-error-pL6jeu4T.mjs +0 -456
  48. package/dist/container-2UmPZ0CI.mjs +0 -22
  49. package/dist/container-CHxKIonn.mjs +0 -440
  50. package/dist/container-D2Z0ITDJ.mjs +0 -22
  51. package/dist/diff-De8d3MVb.mjs +0 -333
  52. package/dist/errors-BAmHDQu8.mjs +0 -45
  53. package/dist/oci-DMZZQZ47.mjs +0 -438
  54. package/dist/schemas-DwD4pwJB.mjs +0 -96
  55. package/dist/spawner-Bw9UBEGX.mjs +0 -54
  56. package/dist/structured-output-BHtr_zpz.mjs +0 -19
  57. package/dist/types-aNMD8h3x.mjs +0 -19
  58. package/dist/util-B4RQZkKr.mjs +0 -77
  59. /package/dist/{events-nxuRbYIu.d.mts → events-D31_b0sA.d.mts} +0 -0
@@ -1,262 +0,0 @@
1
- import { t as formatError } from "./util-B4RQZkKr.mjs";
2
- import { a as buildBaseProcessEnv, i as hasMeaningfulEvent, n as AgentCore, o as createAgentEvent, t as classifyRunError } from "./classify-error-pL6jeu4T.mjs";
3
- import { t as DEFAULT_SPAWNER } from "./spawner-Bw9UBEGX.mjs";
4
- import { createWriteStream, mkdirSync } from "node:fs";
5
- import { dirname } from "node:path";
6
- //#region src/core/process/read-lines.ts
7
- async function* readDecodedLines(stream) {
8
- const decoder = new TextDecoder();
9
- let buffer = "";
10
- for await (const chunk of stream) {
11
- const text = decoder.decode(chunk, { stream: true });
12
- buffer += text;
13
- const lines = buffer.split("\n");
14
- buffer = lines.pop() ?? "";
15
- for (const line of lines) {
16
- const trimmed = line.trim();
17
- if (trimmed) yield trimmed;
18
- }
19
- }
20
- const tail = decoder.decode();
21
- if (tail) buffer += tail;
22
- const remaining = buffer.trim();
23
- if (remaining) yield remaining;
24
- }
25
- //#endregion
26
- //#region src/core/agent/Agent.ts
27
- const SIGKILL_TIMEOUT_MS = 5e3;
28
- const rejectedMessage = (result) => {
29
- return result.status === "rejected" ? formatError(result.reason) : void 0;
30
- };
31
- const resolveFatalError = (run, settled) => {
32
- if (run.stopReason) return void 0;
33
- const stdoutError = rejectedMessage(settled.stdoutResult);
34
- if (stdoutError) return {
35
- kind: "process",
36
- message: stdoutError
37
- };
38
- const stderrError = rejectedMessage(settled.stderrResult);
39
- if (stderrError) return {
40
- kind: "process",
41
- message: stderrError
42
- };
43
- const spawnError = rejectedMessage(settled.processResult);
44
- if (spawnError) return {
45
- kind: "spawn",
46
- message: spawnError
47
- };
48
- if (!run.status && !run.error) {
49
- const processResult = run.processResult;
50
- const stderrText = run.stderrLines.join("\n");
51
- if (processResult && processResult.exitCode !== null && processResult.exitCode !== 0) return {
52
- kind: "process",
53
- message: stderrText || `Process exited with code ${processResult.exitCode}.`
54
- };
55
- if (!hasMeaningfulEvent(run) && run.stderrLines.length > 0) return {
56
- kind: "process",
57
- message: stderrText
58
- };
59
- }
60
- };
61
- const classifyAgentRuntimeError = (error) => {
62
- const classified = classifyRunError(error);
63
- return classified.kind === "provider" ? {
64
- kind: "process",
65
- message: classified.message
66
- } : classified;
67
- };
68
- var Agent = class extends AgentCore {
69
- spawner;
70
- defaults;
71
- containerRef;
72
- autoStartContainer;
73
- autoStopContainer;
74
- containerStopPromise;
75
- logStream;
76
- sigkillTimer;
77
- constructor(options) {
78
- super({
79
- type: options.type,
80
- capabilities: options.capabilities,
81
- sessionId: options.defaults?.sessionId,
82
- onEvent: options.onEvent
83
- });
84
- this.spawner = options.spawner ?? DEFAULT_SPAWNER;
85
- this.defaults = options.defaults ?? {};
86
- this.containerRef = options.container;
87
- this.autoStartContainer = options.autoStartContainer ?? true;
88
- this.autoStopContainer = options.autoStopContainer ?? false;
89
- if (options.logPath) this.initLogStream(options.logPath);
90
- }
91
- onParseError(line, error) {
92
- this.appendRawLog(`[parse-error] ${formatError(error)} ${line.slice(0, 200)}`);
93
- }
94
- buildDefaultEnv(propagateEnv) {
95
- const env = buildBaseProcessEnv({ propagateEnv });
96
- env.CI = env.CI ?? "true";
97
- if (this.defaults.env) Object.assign(env, this.defaults.env);
98
- return env;
99
- }
100
- async executeRunCore(run, request) {
101
- const lifecycle = this.setupRunLifecycle(run);
102
- try {
103
- if (lifecycle.abortedBeforeStart) return this.buildResult(run, /* @__PURE__ */ new Date());
104
- try {
105
- await this.onBeforeRun(run);
106
- const mergedRequest = {
107
- ...request,
108
- ...run.normalizedRequest
109
- };
110
- const args = this.buildArgs(mergedRequest, run);
111
- const env = this.buildEnv(mergedRequest, run);
112
- const command = this.defaults.command?.[0] ?? "claude";
113
- const commandArgs = [...this.defaults.command?.slice(1) ?? [], ...args];
114
- if (run.stopReason) return this.buildResult(run, /* @__PURE__ */ new Date());
115
- this.emitSessionStart(run);
116
- run.process = this.spawner.spawn(command, commandArgs, {
117
- cwd: run.normalizedRequest.cwd,
118
- env,
119
- stdio: [
120
- "pipe",
121
- "pipe",
122
- "pipe"
123
- ]
124
- });
125
- run.process.stdin?.end();
126
- const [stdoutResult, stderrResult, processResult] = await Promise.allSettled([
127
- this.watchStdout(run),
128
- this.watchStderr(run),
129
- run.process.wait()
130
- ]);
131
- if (processResult.status === "fulfilled") run.processResult = processResult.value;
132
- const fatalError = resolveFatalError(run, {
133
- stdoutResult,
134
- stderrResult,
135
- processResult
136
- });
137
- if (fatalError) run.error = fatalError;
138
- } catch (error) {
139
- if (!run.stopReason && !run.error) run.error = classifyAgentRuntimeError(error);
140
- }
141
- return this.buildResult(run, /* @__PURE__ */ new Date());
142
- } finally {
143
- lifecycle.cleanup();
144
- if (this.sigkillTimer) {
145
- clearTimeout(this.sigkillTimer);
146
- this.sigkillTimer = void 0;
147
- }
148
- try {
149
- await this.onRunFinished(run);
150
- } catch (error) {
151
- this.appendRawLog(`[cleanup-error] ${formatError(error)}`);
152
- }
153
- }
154
- }
155
- async watchStdout(run) {
156
- const stdout = run.process?.stdout;
157
- if (!stdout) return;
158
- for await (const line of readDecodedLines(stdout)) {
159
- this.appendLog("stdout", line);
160
- this.processLine(run, line);
161
- }
162
- }
163
- async watchStderr(run) {
164
- const stderr = run.process?.stderr;
165
- if (!stderr) return;
166
- for await (const line of readDecodedLines(stderr)) {
167
- this.appendLog("stderr", line);
168
- run.stderrLines.push(line);
169
- this.emit(run, createAgentEvent(run.runId, "stderr", {
170
- sessionId: run.sessionId,
171
- text: line
172
- }));
173
- }
174
- }
175
- processLine(run, line) {
176
- let parsed;
177
- try {
178
- parsed = JSON.parse(line);
179
- } catch (error) {
180
- this.onParseError(line, error);
181
- return;
182
- }
183
- let batch;
184
- try {
185
- batch = this.mapRawEvent(parsed, {
186
- runId: run.runId,
187
- requestedSessionId: run.requestedSessionId,
188
- sessionId: run.sessionId,
189
- schemaRequested: run.normalizedRequest.schema.kind !== "none",
190
- toolCalls: run.toolCalls
191
- });
192
- } catch (error) {
193
- this.onParseError(line, error);
194
- return;
195
- }
196
- this.applyBatch(run, batch);
197
- }
198
- get runRequestDefaults() {
199
- return this.defaults;
200
- }
201
- onHandlerError(error) {
202
- this.appendRawLog(`[handler-error] ${formatError(error)}`);
203
- }
204
- async onDispose() {
205
- const logStream = this.logStream;
206
- this.logStream = void 0;
207
- if (!logStream) return;
208
- await new Promise((resolve) => {
209
- logStream.end(resolve);
210
- });
211
- }
212
- onStopRequested(run) {
213
- run.process?.kill();
214
- this.sigkillTimer = setTimeout(() => {
215
- run.process?.kill("SIGKILL");
216
- }, SIGKILL_TIMEOUT_MS);
217
- this.sigkillTimer.unref?.();
218
- }
219
- onBeforeRun(_run) {
220
- return this.ensureContainer();
221
- }
222
- async onRunFinished(_run) {
223
- if (this.autoStopContainer && this.containerRef && this.containerRef.status === "running") {
224
- this.containerStopPromise = this.containerRef.stop().catch((error) => {
225
- this.appendRawLog(`[auto-stop-error] ${formatError(error)}`);
226
- });
227
- await this.containerStopPromise;
228
- }
229
- }
230
- appendRawLog(data) {
231
- if (!this.logStream) return;
232
- const text = typeof data === "string" ? data : JSON.stringify(data);
233
- this.logStream.write(`${text}\n`);
234
- }
235
- initLogStream(logPath) {
236
- try {
237
- mkdirSync(dirname(logPath), { recursive: true });
238
- this.logStream = createWriteStream(logPath, { flags: "a" });
239
- this.logStream.write(`\n--- ${(/* @__PURE__ */ new Date()).toISOString()} ---\n`);
240
- } catch {
241
- this.logStream = void 0;
242
- }
243
- }
244
- appendLog(source, chunk) {
245
- if (!this.logStream || !chunk) return;
246
- const label = source === "stderr" ? "STDERR" : "STDOUT";
247
- const ts = (/* @__PURE__ */ new Date()).toISOString();
248
- for (const line of chunk.split("\n")) {
249
- if (!line) continue;
250
- this.logStream.write(`[${ts}] ${label} ${line}\n`);
251
- }
252
- }
253
- async ensureContainer() {
254
- if (this.containerStopPromise) {
255
- await this.containerStopPromise;
256
- this.containerStopPromise = void 0;
257
- }
258
- if (this.autoStartContainer && this.containerRef && this.containerRef.status !== "running") await this.containerRef.start();
259
- }
260
- };
261
- //#endregion
262
- export { Agent as t };
@@ -1,38 +0,0 @@
1
- import { n as AgentCore, t as classifyRunError } from "./classify-error-pL6jeu4T.mjs";
2
- //#region src/core/agent/SdkAgent.ts
3
- /**
4
- * Base class for SDK-based agent adapters (claude-code-sdk, codex-sdk).
5
- * Provides the shared run lifecycle: abort/timeout wiring, session-start/end emission,
6
- * error classification, and cleanup. Subclasses implement only the SDK-specific iteration.
7
- */
8
- var SdkAgent = class extends AgentCore {
9
- currentAbortController;
10
- /** optional cleanup called in finally block (e.g. close SDK query) */
11
- cleanupSdkResources() {}
12
- onStopRequested(_run) {
13
- this.currentAbortController?.abort();
14
- }
15
- async executeRunCore(run, request) {
16
- const runAbortController = new AbortController();
17
- this.currentAbortController = runAbortController;
18
- const lifecycle = this.setupRunLifecycle(run);
19
- try {
20
- if (lifecycle.abortedBeforeStart) return this.buildResult(run, /* @__PURE__ */ new Date());
21
- if (run.stopReason) return this.buildResult(run, /* @__PURE__ */ new Date());
22
- this.emitSessionStart(run);
23
- await this.executeSdkRun(run, request, runAbortController);
24
- return this.buildResult(run, /* @__PURE__ */ new Date());
25
- } catch (error) {
26
- if (!run.stopReason && !run.error) run.error = classifyRunError(error);
27
- return this.buildResult(run, /* @__PURE__ */ new Date());
28
- } finally {
29
- lifecycle.cleanup();
30
- try {
31
- await this.cleanupSdkResources();
32
- } catch {}
33
- if (this.currentAbortController === runAbortController) this.currentAbortController = void 0;
34
- }
35
- }
36
- };
37
- //#endregion
38
- export { SdkAgent as t };