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,96 @@
|
|
|
1
|
+
import { z } from "zod/v4";
|
|
2
|
+
//#region src/schemas/agent.ts
|
|
3
|
+
const textContentPartSchema = z.object({
|
|
4
|
+
type: z.literal("text"),
|
|
5
|
+
text: z.string()
|
|
6
|
+
});
|
|
7
|
+
const imageContentPartSchema = z.object({
|
|
8
|
+
type: z.literal("image"),
|
|
9
|
+
path: z.string(),
|
|
10
|
+
mediaType: z.string().optional()
|
|
11
|
+
});
|
|
12
|
+
const contentPartSchema = z.discriminatedUnion("type", [textContentPartSchema, imageContentPartSchema]);
|
|
13
|
+
const agentCapabilitiesSchema = z.object({
|
|
14
|
+
structuredOutput: z.boolean(),
|
|
15
|
+
sessionResume: z.boolean(),
|
|
16
|
+
imageInput: z.boolean(),
|
|
17
|
+
mcp: z.boolean(),
|
|
18
|
+
eventStreaming: z.boolean(),
|
|
19
|
+
sessionFork: z.boolean().optional()
|
|
20
|
+
});
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/schemas/events.ts
|
|
23
|
+
const baseEventSchema = z.object({
|
|
24
|
+
id: z.string(),
|
|
25
|
+
runId: z.string(),
|
|
26
|
+
timestamp: z.date()
|
|
27
|
+
});
|
|
28
|
+
const sessionStartEventSchema = baseEventSchema.extend({
|
|
29
|
+
type: z.literal("session-start"),
|
|
30
|
+
requestedSessionId: z.string().optional(),
|
|
31
|
+
sessionId: z.string().optional(),
|
|
32
|
+
resumed: z.boolean()
|
|
33
|
+
});
|
|
34
|
+
const messageEventSchema = baseEventSchema.extend({
|
|
35
|
+
type: z.literal("message"),
|
|
36
|
+
sessionId: z.string().optional(),
|
|
37
|
+
model: z.string().nullable(),
|
|
38
|
+
role: z.enum([
|
|
39
|
+
"system",
|
|
40
|
+
"user",
|
|
41
|
+
"assistant"
|
|
42
|
+
]),
|
|
43
|
+
content: z.array(contentPartSchema),
|
|
44
|
+
text: z.string().optional()
|
|
45
|
+
});
|
|
46
|
+
const reasoningEventSchema = baseEventSchema.extend({
|
|
47
|
+
type: z.literal("reasoning"),
|
|
48
|
+
sessionId: z.string().optional(),
|
|
49
|
+
model: z.string().nullable(),
|
|
50
|
+
content: z.array(contentPartSchema),
|
|
51
|
+
text: z.string().optional()
|
|
52
|
+
});
|
|
53
|
+
const toolCallEventSchema = baseEventSchema.extend({
|
|
54
|
+
type: z.literal("tool-call"),
|
|
55
|
+
sessionId: z.string().optional(),
|
|
56
|
+
model: z.string().nullable(),
|
|
57
|
+
toolCallId: z.string(),
|
|
58
|
+
toolName: z.string(),
|
|
59
|
+
input: z.unknown().optional()
|
|
60
|
+
});
|
|
61
|
+
const toolResultEventSchema = baseEventSchema.extend({
|
|
62
|
+
type: z.literal("tool-result"),
|
|
63
|
+
sessionId: z.string().optional(),
|
|
64
|
+
toolCallId: z.string(),
|
|
65
|
+
toolName: z.string().optional(),
|
|
66
|
+
input: z.unknown().optional(),
|
|
67
|
+
output: z.unknown(),
|
|
68
|
+
isError: z.boolean().optional(),
|
|
69
|
+
error: z.string().optional()
|
|
70
|
+
});
|
|
71
|
+
const stderrEventSchema = baseEventSchema.extend({
|
|
72
|
+
type: z.literal("stderr"),
|
|
73
|
+
sessionId: z.string().optional(),
|
|
74
|
+
text: z.string()
|
|
75
|
+
});
|
|
76
|
+
const sessionEndEventSchema = baseEventSchema.extend({
|
|
77
|
+
type: z.literal("session-end"),
|
|
78
|
+
sessionId: z.string().optional(),
|
|
79
|
+
status: z.enum([
|
|
80
|
+
"completed",
|
|
81
|
+
"failed",
|
|
82
|
+
"cancelled"
|
|
83
|
+
]),
|
|
84
|
+
error: z.string().optional()
|
|
85
|
+
});
|
|
86
|
+
const eventSchema = z.discriminatedUnion("type", [
|
|
87
|
+
sessionStartEventSchema,
|
|
88
|
+
messageEventSchema,
|
|
89
|
+
reasoningEventSchema,
|
|
90
|
+
toolCallEventSchema,
|
|
91
|
+
toolResultEventSchema,
|
|
92
|
+
stderrEventSchema,
|
|
93
|
+
sessionEndEventSchema
|
|
94
|
+
]);
|
|
95
|
+
//#endregion
|
|
96
|
+
export { sessionEndEventSchema as a, toolCallEventSchema as c, contentPartSchema as d, imageContentPartSchema as f, reasoningEventSchema as i, toolResultEventSchema as l, eventSchema as n, sessionStartEventSchema as o, textContentPartSchema as p, messageEventSchema as r, stderrEventSchema as s, baseEventSchema as t, agentCapabilitiesSchema as u };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
//#region src/core/process/spawner.ts
|
|
3
|
+
const streamToAsyncIterable = (stream) => ({ async *[Symbol.asyncIterator]() {
|
|
4
|
+
for await (const chunk of stream) {
|
|
5
|
+
if (chunk instanceof Buffer) {
|
|
6
|
+
yield new Uint8Array(chunk);
|
|
7
|
+
continue;
|
|
8
|
+
}
|
|
9
|
+
if (typeof chunk === "string") {
|
|
10
|
+
yield new TextEncoder().encode(chunk);
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
if (chunk instanceof Uint8Array) {
|
|
14
|
+
yield chunk;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
throw new Error(`Unsupported stdout/stderr chunk type: ${typeof chunk}`);
|
|
18
|
+
}
|
|
19
|
+
} });
|
|
20
|
+
const spawnProcess = (command, args, options = {}) => {
|
|
21
|
+
const child = spawn(command, args, {
|
|
22
|
+
cwd: options.cwd,
|
|
23
|
+
env: options.env,
|
|
24
|
+
stdio: options.stdio
|
|
25
|
+
});
|
|
26
|
+
const waitPromise = new Promise((resolve, reject) => {
|
|
27
|
+
const onClose = (exitCode, signal) => {
|
|
28
|
+
child.removeListener("error", onError);
|
|
29
|
+
resolve({
|
|
30
|
+
exitCode,
|
|
31
|
+
signal
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
const onError = (err) => {
|
|
35
|
+
child.removeListener("close", onClose);
|
|
36
|
+
reject(err);
|
|
37
|
+
};
|
|
38
|
+
child.once("error", onError);
|
|
39
|
+
child.once("close", onClose);
|
|
40
|
+
});
|
|
41
|
+
return {
|
|
42
|
+
stdout: child.stdout ? streamToAsyncIterable(child.stdout) : null,
|
|
43
|
+
stderr: child.stderr ? streamToAsyncIterable(child.stderr) : null,
|
|
44
|
+
stdin: child.stdin ? {
|
|
45
|
+
write: (chunk) => child.stdin?.write(chunk),
|
|
46
|
+
end: () => child.stdin?.end()
|
|
47
|
+
} : null,
|
|
48
|
+
kill: (signal) => child.kill(signal),
|
|
49
|
+
wait: () => waitPromise
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
const DEFAULT_SPAWNER = { spawn: spawnProcess };
|
|
53
|
+
//#endregion
|
|
54
|
+
export { DEFAULT_SPAWNER as t };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/core/structured-output.ts
|
|
2
|
+
const findStructuredOutputToolCallInput = (toolCalls) => {
|
|
3
|
+
let latestInput;
|
|
4
|
+
for (const [, record] of toolCalls) if (record.toolName === "StructuredOutput" && record.input !== void 0) latestInput = record.input;
|
|
5
|
+
return latestInput;
|
|
6
|
+
};
|
|
7
|
+
const extractClaudeStructuredOutput = (structuredOutput, context, eventMeta, errorMessage) => {
|
|
8
|
+
if (structuredOutput !== void 0) return { value: structuredOutput };
|
|
9
|
+
if (!context.schemaRequested) return {};
|
|
10
|
+
const toolCallOutput = findStructuredOutputToolCallInput(context.toolCalls);
|
|
11
|
+
if (toolCallOutput !== void 0) return { value: toolCallOutput };
|
|
12
|
+
if (eventMeta.isSuccess) return { error: {
|
|
13
|
+
kind: "parse",
|
|
14
|
+
message: errorMessage
|
|
15
|
+
} };
|
|
16
|
+
return {};
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { extractClaudeStructuredOutput as t };
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { t as AgentEvent, v as AgentCapabilities, x as JsonSchema, y as ContentPart } from "./events-nxuRbYIu.mjs";
|
|
2
|
+
import { ZodType } from "zod/v4";
|
|
3
|
+
|
|
4
|
+
//#region src/core/process/types.d.ts
|
|
5
|
+
type StdioOption = "ignore" | "inherit" | "pipe" | number;
|
|
6
|
+
type ProcessSignal = NodeJS.Signals;
|
|
7
|
+
interface SpawnOptions {
|
|
8
|
+
cwd?: string;
|
|
9
|
+
env?: Record<string, string>;
|
|
10
|
+
stdio?: [StdioOption, StdioOption, StdioOption];
|
|
11
|
+
}
|
|
12
|
+
interface ProcessResult {
|
|
13
|
+
exitCode: number | null;
|
|
14
|
+
signal: ProcessSignal | null;
|
|
15
|
+
}
|
|
16
|
+
interface AgentProcess {
|
|
17
|
+
stdout: AsyncIterable<Uint8Array> | null;
|
|
18
|
+
stderr: AsyncIterable<Uint8Array> | null;
|
|
19
|
+
stdin: {
|
|
20
|
+
write?: (chunk: Uint8Array | string) => void;
|
|
21
|
+
end: () => void;
|
|
22
|
+
} | null;
|
|
23
|
+
kill: (signal?: ProcessSignal | number) => void;
|
|
24
|
+
wait: () => Promise<ProcessResult>;
|
|
25
|
+
}
|
|
26
|
+
interface ProcessSpawner {
|
|
27
|
+
spawn: (command: string, args: string[], options?: SpawnOptions) => AgentProcess;
|
|
28
|
+
}
|
|
29
|
+
//#endregion
|
|
30
|
+
//#region src/core/agent/types.d.ts
|
|
31
|
+
/** Supported agents */
|
|
32
|
+
type AgentType = "claude-code-cli" | "claude-code-sdk" | "codex-cli" | "codex-sdk" | "gemini-cli";
|
|
33
|
+
/** Terminal status of a completed agent run */
|
|
34
|
+
type RunStatus = "cancelled" | "completed" | "failed";
|
|
35
|
+
/** Reason an agent run was stopped before completion */
|
|
36
|
+
type StopReason = "abort" | "timeout" | "user";
|
|
37
|
+
/** Usage and performance statistics from an agent run */
|
|
38
|
+
interface RunStats {
|
|
39
|
+
durationMs?: number;
|
|
40
|
+
apiDurationMs?: number;
|
|
41
|
+
turns: number;
|
|
42
|
+
costUsd: number;
|
|
43
|
+
inputTokens?: number;
|
|
44
|
+
cacheCreationInputTokens?: number;
|
|
45
|
+
cacheReadInputTokens?: number;
|
|
46
|
+
outputTokens?: number;
|
|
47
|
+
totalTokens?: number;
|
|
48
|
+
}
|
|
49
|
+
/** Structured error from a failed or cancelled agent run */
|
|
50
|
+
interface RunError {
|
|
51
|
+
/** Error classification: abort (externally stopped), parse (output parsing), process (non-zero exit), provider (api error), spawn (binary not found) */
|
|
52
|
+
kind: "abort" | "parse" | "process" | "provider" | "spawn";
|
|
53
|
+
message: string;
|
|
54
|
+
}
|
|
55
|
+
/** Input parameters for a plain-text agent run. */
|
|
56
|
+
interface RunRequest {
|
|
57
|
+
input: ContentPart[] | string;
|
|
58
|
+
cwd?: string;
|
|
59
|
+
model?: string;
|
|
60
|
+
systemPrompt?: string;
|
|
61
|
+
timeoutMs?: number;
|
|
62
|
+
signal?: AbortSignal;
|
|
63
|
+
}
|
|
64
|
+
/** Input parameters for a raw-JSON-Schema structured-output run. */
|
|
65
|
+
interface JsonSchemaRunRequest extends RunRequest {
|
|
66
|
+
schema: JsonSchema;
|
|
67
|
+
}
|
|
68
|
+
/** Input parameters for a Zod-backed structured-output run. */
|
|
69
|
+
interface ZodRunRequest<TOutput = unknown> extends RunRequest {
|
|
70
|
+
schema: ZodType<TOutput>;
|
|
71
|
+
}
|
|
72
|
+
/** Input parameters for a structured-output run. */
|
|
73
|
+
type StructuredRunRequest<TOutput = unknown> = JsonSchemaRunRequest | ZodRunRequest<TOutput>;
|
|
74
|
+
/** Any supported run request shape. */
|
|
75
|
+
type AnyRunRequest = RunRequest | StructuredRunRequest;
|
|
76
|
+
interface RunTiming {
|
|
77
|
+
startedAt: Date;
|
|
78
|
+
endedAt: Date;
|
|
79
|
+
durationMs: number;
|
|
80
|
+
}
|
|
81
|
+
interface RunProcessInfo {
|
|
82
|
+
exitCode: number | null;
|
|
83
|
+
signal: ProcessSignal | null;
|
|
84
|
+
stderr: string;
|
|
85
|
+
}
|
|
86
|
+
interface RunResultBase {
|
|
87
|
+
type: AgentType;
|
|
88
|
+
runId: string;
|
|
89
|
+
sessionId?: string;
|
|
90
|
+
text: string;
|
|
91
|
+
events: readonly AgentEvent[];
|
|
92
|
+
stats?: RunStats;
|
|
93
|
+
timing: RunTiming;
|
|
94
|
+
process?: RunProcessInfo;
|
|
95
|
+
}
|
|
96
|
+
interface CancelledRunResult extends RunResultBase {
|
|
97
|
+
status: "cancelled";
|
|
98
|
+
output?: undefined;
|
|
99
|
+
error: RunError;
|
|
100
|
+
cancelReason: StopReason;
|
|
101
|
+
}
|
|
102
|
+
interface FailedRunResult extends RunResultBase {
|
|
103
|
+
status: "failed";
|
|
104
|
+
output?: undefined;
|
|
105
|
+
error: RunError;
|
|
106
|
+
cancelReason?: undefined;
|
|
107
|
+
}
|
|
108
|
+
type CompletedRunResult<TOutput = never> = RunResultBase & ([TOutput] extends [never] ? {} : {
|
|
109
|
+
output: TOutput;
|
|
110
|
+
}) & {
|
|
111
|
+
status: "completed";
|
|
112
|
+
error?: undefined;
|
|
113
|
+
cancelReason?: undefined;
|
|
114
|
+
};
|
|
115
|
+
/** Discriminated union result of an agent run. narrow on `status` to access variant-specific fields. */
|
|
116
|
+
type RunResult<TOutput = never> = CancelledRunResult | CompletedRunResult<TOutput> | FailedRunResult;
|
|
117
|
+
/** Result of a run when the request shape is not known ahead of time. */
|
|
118
|
+
type AnyRunResult = RunResult | RunResult<unknown>;
|
|
119
|
+
/** Completed result of a run when the request shape is not known ahead of time. */
|
|
120
|
+
type AnyCompletedRunResult = CompletedRunResult | CompletedRunResult<unknown>;
|
|
121
|
+
type AgentRun<TRunOptions extends object = {}> = {
|
|
122
|
+
<TOutput>(request: TRunOptions & ZodRunRequest<TOutput>): Promise<RunResult<TOutput>>;
|
|
123
|
+
(request: JsonSchemaRunRequest & TRunOptions): Promise<RunResult<unknown>>;
|
|
124
|
+
(request: RunRequest & TRunOptions): Promise<RunResult>;
|
|
125
|
+
};
|
|
126
|
+
type AgentRunOrThrow<TRunOptions extends object = {}> = {
|
|
127
|
+
<TOutput>(request: TRunOptions & ZodRunRequest<TOutput>): Promise<CompletedRunResult<TOutput>>;
|
|
128
|
+
(request: JsonSchemaRunRequest & TRunOptions): Promise<CompletedRunResult<unknown>>;
|
|
129
|
+
(request: RunRequest & TRunOptions): Promise<CompletedRunResult>;
|
|
130
|
+
};
|
|
131
|
+
/** Thrown by `runOrThrow()` when the run does not complete successfully. carries the full `RunResult` on `.result`. */
|
|
132
|
+
declare class AgentRunError<TOutput = never> extends Error {
|
|
133
|
+
readonly result: RunResult<TOutput> & {
|
|
134
|
+
status: "cancelled" | "failed";
|
|
135
|
+
};
|
|
136
|
+
constructor(result: RunResult<TOutput> & {
|
|
137
|
+
status: "cancelled" | "failed";
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
/** Thrown when agent configuration or request parameters are invalid (e.g. unsupported capability, empty input) */
|
|
141
|
+
declare class AgentValidationError extends Error {
|
|
142
|
+
constructor(message: string);
|
|
143
|
+
}
|
|
144
|
+
type AgentEventHandler = (event: AgentEvent) => void;
|
|
145
|
+
interface AgentMetrics {
|
|
146
|
+
runCount: number;
|
|
147
|
+
messagesReceived: number;
|
|
148
|
+
toolCalls: number;
|
|
149
|
+
errors: number;
|
|
150
|
+
startedAt: Date;
|
|
151
|
+
lastActivityAt: Date;
|
|
152
|
+
}
|
|
153
|
+
/** Excludes keys typed as never (e.g. `systemPrompt?: never` blocker fields) */
|
|
154
|
+
type EffectiveKeys<T> = keyof { [K in keyof T as Required<T>[K] extends never ? never : K]: unknown };
|
|
155
|
+
/** Resolves to T if T has no effective keys overlapping with RunRequest, otherwise never */
|
|
156
|
+
type EnsureDisjoint<T extends object> = EffectiveKeys<T> & keyof RunRequest extends never ? T : never;
|
|
157
|
+
/**
|
|
158
|
+
* Base interface for all agent types. TRunOptions adds adapter-specific fields to the run request.
|
|
159
|
+
* TRunOptions keys must not overlap with RunRequest keys (enforced via `EnsureDisjoint`).
|
|
160
|
+
*/
|
|
161
|
+
interface BaseAgent<TRunOptions extends object = {}> {
|
|
162
|
+
readonly type: AgentType;
|
|
163
|
+
readonly capabilities: Readonly<AgentCapabilities>;
|
|
164
|
+
readonly sessionId?: string;
|
|
165
|
+
readonly isRunning: boolean;
|
|
166
|
+
readonly metrics: Readonly<AgentMetrics>;
|
|
167
|
+
run: AgentRun<TRunOptions>;
|
|
168
|
+
runOrThrow: AgentRunOrThrow<TRunOptions>;
|
|
169
|
+
stop: () => Promise<AnyRunResult | undefined>;
|
|
170
|
+
reset: () => void;
|
|
171
|
+
wait: () => Promise<AnyRunResult>;
|
|
172
|
+
dispose: () => Promise<void>;
|
|
173
|
+
[Symbol.asyncDispose]: () => Promise<void>;
|
|
174
|
+
onEvent: (handler: AgentEventHandler) => () => void;
|
|
175
|
+
}
|
|
176
|
+
//#endregion
|
|
177
|
+
export { StopReason as C, ProcessSpawner as E, RunTiming as S, ZodRunRequest as T, RunRequest as _, AgentValidationError as a, RunStats as b, AnyRunResult as c, CompletedRunResult as d, EnsureDisjoint as f, RunProcessInfo as g, RunError as h, AgentType as i, BaseAgent as l, JsonSchemaRunRequest as m, AgentMetrics as n, AnyCompletedRunResult as o, FailedRunResult as p, AgentRunError as r, AnyRunRequest as s, AgentEventHandler as t, CancelledRunResult as u, RunResult as v, StructuredRunRequest as w, RunStatus as x, RunResultBase as y };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/core/agent/types.ts
|
|
2
|
+
/** Thrown by `runOrThrow()` when the run does not complete successfully. carries the full `RunResult` on `.result`. */
|
|
3
|
+
var AgentRunError = class extends Error {
|
|
4
|
+
result;
|
|
5
|
+
constructor(result) {
|
|
6
|
+
super(result.error.message);
|
|
7
|
+
this.name = "AgentRunError";
|
|
8
|
+
this.result = result;
|
|
9
|
+
}
|
|
10
|
+
};
|
|
11
|
+
/** Thrown when agent configuration or request parameters are invalid (e.g. unsupported capability, empty input) */
|
|
12
|
+
var AgentValidationError = class extends Error {
|
|
13
|
+
constructor(message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.name = "AgentValidationError";
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { AgentValidationError as n, AgentRunError as t };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
//#region src/core/util.ts
|
|
2
|
+
const toArray = (value) => {
|
|
3
|
+
return Array.isArray(value) ? value : [value];
|
|
4
|
+
};
|
|
5
|
+
const formatError = (error) => error instanceof Error ? error.message : String(error);
|
|
6
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
7
|
+
const sumTokens = (...values) => values.reduce((sum, v) => sum + (v ?? 0), 0);
|
|
8
|
+
const stripMarkdownCodeFence = (value) => {
|
|
9
|
+
const trimmed = value.trim();
|
|
10
|
+
if (!trimmed.startsWith("```")) return trimmed;
|
|
11
|
+
return trimmed.replace(/^```[\w-]*\s*/i, "").replace(/\s*```$/i, "").trim();
|
|
12
|
+
};
|
|
13
|
+
const extractMarkdownCodeBlock = (value) => {
|
|
14
|
+
return /```[\w-]*\s*([\S\s]*?)\s*```/i.exec(value)?.[1]?.trim();
|
|
15
|
+
};
|
|
16
|
+
const extractBalancedJson = (value) => {
|
|
17
|
+
let start = -1;
|
|
18
|
+
const stack = [];
|
|
19
|
+
let inString = false;
|
|
20
|
+
let escaped = false;
|
|
21
|
+
for (let index = 0; index < value.length; index++) {
|
|
22
|
+
const char = value[index];
|
|
23
|
+
if (!char) continue;
|
|
24
|
+
if (start === -1) {
|
|
25
|
+
if (char === "{" || char === "[") {
|
|
26
|
+
start = index;
|
|
27
|
+
stack.push(char);
|
|
28
|
+
}
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (inString) {
|
|
32
|
+
if (escaped) {
|
|
33
|
+
escaped = false;
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
if (char === "\\") {
|
|
37
|
+
escaped = true;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
if (char === "\"") inString = false;
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (char === "\"") {
|
|
44
|
+
inString = true;
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (char === "{" || char === "[") {
|
|
48
|
+
stack.push(char);
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
if (char === "}" || char === "]") {
|
|
52
|
+
const open = stack.pop();
|
|
53
|
+
if (open === "{" && char !== "}" || open === "[" && char !== "]") return;
|
|
54
|
+
if (stack.length === 0) return value.slice(start, index + 1).trim();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
const parseJsonResponseText = (value) => {
|
|
59
|
+
const trimmed = value.trim();
|
|
60
|
+
const candidates = [
|
|
61
|
+
stripMarkdownCodeFence(trimmed),
|
|
62
|
+
extractMarkdownCodeBlock(trimmed),
|
|
63
|
+
extractBalancedJson(trimmed)
|
|
64
|
+
];
|
|
65
|
+
let lastError;
|
|
66
|
+
for (const candidate of new Set(candidates)) {
|
|
67
|
+
if (!candidate) continue;
|
|
68
|
+
try {
|
|
69
|
+
return JSON.parse(candidate);
|
|
70
|
+
} catch (error) {
|
|
71
|
+
lastError = error;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
throw lastError instanceof Error ? lastError : /* @__PURE__ */ new SyntaxError("Failed to parse JSON response text.");
|
|
75
|
+
};
|
|
76
|
+
//#endregion
|
|
77
|
+
export { toArray as a, sumTokens as i, isRecord as n, parseJsonResponseText as r, formatError as t };
|
package/package.json
CHANGED
|
@@ -1,15 +1,92 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "coding-agents-sdk",
|
|
3
|
-
"version": "0.0
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"types": "./dist/index.d.mts",
|
|
6
6
|
"exports": {
|
|
7
|
-
".":
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./dist/index.d.mts",
|
|
9
|
+
"default": "./dist/index.mjs"
|
|
10
|
+
},
|
|
11
|
+
"./schemas": {
|
|
12
|
+
"types": "./dist/schemas/index.d.mts",
|
|
13
|
+
"default": "./dist/schemas/index.mjs"
|
|
14
|
+
},
|
|
15
|
+
"./container": {
|
|
16
|
+
"types": "./dist/container/index.d.mts",
|
|
17
|
+
"default": "./dist/container/index.mjs"
|
|
18
|
+
},
|
|
19
|
+
"./adapters/claude-code-cli": {
|
|
20
|
+
"types": "./dist/adapters/claude-code-cli/index.d.mts",
|
|
21
|
+
"default": "./dist/adapters/claude-code-cli/index.mjs"
|
|
22
|
+
},
|
|
23
|
+
"./adapters/claude-code-sdk": {
|
|
24
|
+
"types": "./dist/adapters/claude-code-sdk/index.d.mts",
|
|
25
|
+
"default": "./dist/adapters/claude-code-sdk/index.mjs"
|
|
26
|
+
},
|
|
27
|
+
"./adapters/codex-cli": {
|
|
28
|
+
"types": "./dist/adapters/codex-cli/index.d.mts",
|
|
29
|
+
"default": "./dist/adapters/codex-cli/index.mjs"
|
|
30
|
+
},
|
|
31
|
+
"./adapters/codex-sdk": {
|
|
32
|
+
"types": "./dist/adapters/codex-sdk/index.d.mts",
|
|
33
|
+
"default": "./dist/adapters/codex-sdk/index.mjs"
|
|
34
|
+
},
|
|
35
|
+
"./adapters/gemini-cli": {
|
|
36
|
+
"types": "./dist/adapters/gemini-cli/index.d.mts",
|
|
37
|
+
"default": "./dist/adapters/gemini-cli/index.mjs"
|
|
38
|
+
},
|
|
39
|
+
"./package.json": "./package.json"
|
|
11
40
|
},
|
|
12
41
|
"files": [
|
|
13
|
-
"
|
|
14
|
-
|
|
42
|
+
"dist",
|
|
43
|
+
"README.md"
|
|
44
|
+
],
|
|
45
|
+
"sideEffects": false,
|
|
46
|
+
"scripts": {
|
|
47
|
+
"build": "tsdown && bun run test:package",
|
|
48
|
+
"start": "bun run examples/claude-code-cli/simple.ts",
|
|
49
|
+
"test": "bun test",
|
|
50
|
+
"test:agent": "bun test ./tests",
|
|
51
|
+
"test:package": "bun run test:package:runtime",
|
|
52
|
+
"test:package:runtime": "bun test ./tests/package-exports.smoke.ts",
|
|
53
|
+
"test:agent:claude-code-cli": "CODE_WORKFLOW_TEST_AGENTS=claude-code-cli bun run test:agent",
|
|
54
|
+
"test:agent:claude-code-sdk": "CODE_WORKFLOW_TEST_AGENTS=claude-code-sdk bun run test:agent",
|
|
55
|
+
"test:agent:codex-cli": "CODE_WORKFLOW_TEST_AGENTS=codex-cli bun run test:agent",
|
|
56
|
+
"test:agent:codex-sdk": "CODE_WORKFLOW_TEST_AGENTS=codex-sdk bun run test:agent",
|
|
57
|
+
"test:agent:gemini-cli": "CODE_WORKFLOW_TEST_AGENTS=gemini-cli bun run test:agent",
|
|
58
|
+
"knip": "knip",
|
|
59
|
+
"tsc": "tsc --noEmit --pretty",
|
|
60
|
+
"typecheck": "bun run tsc"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@anthropic-ai/claude-agent-sdk": "^0.2.74",
|
|
64
|
+
"@openai/codex-sdk": "^0.114.0",
|
|
65
|
+
"@types/bun": "latest",
|
|
66
|
+
"@types/node": "^25.5.0",
|
|
67
|
+
"e2b": "^2.14.1",
|
|
68
|
+
"knip": "^5.87.0",
|
|
69
|
+
"picoprint": "^1.0.6",
|
|
70
|
+
"tsdown": "^0.21.4"
|
|
71
|
+
},
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"@anthropic-ai/claude-agent-sdk": "^0.2.74",
|
|
74
|
+
"@openai/codex-sdk": "^0.114.0",
|
|
75
|
+
"e2b": "^2.14.1",
|
|
76
|
+
"typescript": "^5"
|
|
77
|
+
},
|
|
78
|
+
"peerDependenciesMeta": {
|
|
79
|
+
"@anthropic-ai/claude-agent-sdk": {
|
|
80
|
+
"optional": true
|
|
81
|
+
},
|
|
82
|
+
"@openai/codex-sdk": {
|
|
83
|
+
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"e2b": {
|
|
86
|
+
"optional": true
|
|
87
|
+
}
|
|
88
|
+
},
|
|
89
|
+
"dependencies": {
|
|
90
|
+
"zod": "^4.3.6"
|
|
91
|
+
}
|
|
15
92
|
}
|