-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsdk.mjs
More file actions
167 lines (164 loc) · 4.94 KB
/
sdk.mjs
File metadata and controls
167 lines (164 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// (c) Anthropic PBC. All rights reserved. Use is subject to Anthropic's Commercial Terms of Service (https://www.anthropic.com/legal/commercial-terms).
// Version: 1.0.41
// src/entrypoints/sdk.ts
import { spawn } from "child_process";
import { join } from "path";
import { fileURLToPath } from "url";
import { createInterface } from "readline";
import { existsSync } from "fs";
var __filename2 = fileURLToPath(import.meta.url);
var __dirname2 = join(__filename2, "..");
async function* query({
prompt,
options: {
abortController = new AbortController,
allowedTools = [],
appendSystemPrompt,
customSystemPrompt,
cwd,
disallowedTools = [],
executable = isRunningWithBun() ? "bun" : "node",
executableArgs = [],
maxTurns,
mcpServers,
pathToClaudeCodeExecutable = join(__dirname2, "cli.js"),
permissionMode = "default",
permissionPromptToolName,
continue: continueConversation,
resume,
model,
fallbackModel
} = {}
}) {
if (!process.env.CLAUDE_CODE_ENTRYPOINT) {
process.env.CLAUDE_CODE_ENTRYPOINT = "sdk-ts";
}
const args = ["--output-format", "stream-json", "--verbose"];
if (customSystemPrompt)
args.push("--system-prompt", customSystemPrompt);
if (appendSystemPrompt)
args.push("--append-system-prompt", appendSystemPrompt);
if (maxTurns)
args.push("--max-turns", maxTurns.toString());
if (model)
args.push("--model", model);
if (permissionPromptToolName)
args.push("--permission-prompt-tool", permissionPromptToolName);
if (continueConversation)
args.push("--continue");
if (resume)
args.push("--resume", resume);
if (allowedTools.length > 0) {
args.push("--allowedTools", allowedTools.join(","));
}
if (disallowedTools.length > 0) {
args.push("--disallowedTools", disallowedTools.join(","));
}
if (mcpServers && Object.keys(mcpServers).length > 0) {
args.push("--mcp-config", JSON.stringify({ mcpServers }));
}
if (permissionMode !== "default") {
args.push("--permission-mode", permissionMode);
}
if (fallbackModel) {
if (model && fallbackModel === model) {
throw new Error("Fallback model cannot be the same as the main model. Please specify a different model for fallbackModel option.");
}
args.push("--fallback-model", fallbackModel);
}
if (typeof prompt === "string") {
args.push("--print", prompt.trim());
} else {
args.push("--input-format", "stream-json");
}
if (!existsSync(pathToClaudeCodeExecutable)) {
throw new ReferenceError(`Claude Code executable not found at ${pathToClaudeCodeExecutable}. Is options.pathToClaudeCodeExecutable set?`);
}
logDebug(`Spawning Claude Code process: ${executable} ${[...executableArgs, pathToClaudeCodeExecutable, ...args].join(" ")}`);
const child = spawn(executable, [...executableArgs, pathToClaudeCodeExecutable, ...args], {
cwd,
stdio: ["pipe", "pipe", "pipe"],
signal: abortController.signal,
env: {
...process.env
}
});
if (typeof prompt === "string") {
child.stdin.end();
} else {
streamToStdin(prompt, child.stdin, abortController);
}
if (process.env.DEBUG) {
child.stderr.on("data", (data) => {
console.error("Claude Code stderr:", data.toString());
});
}
const cleanup = () => {
if (!child.killed) {
child.kill("SIGTERM");
}
};
abortController.signal.addEventListener("abort", cleanup);
process.on("exit", cleanup);
try {
let processError = null;
child.on("error", (error) => {
processError = new Error(`Failed to spawn Claude Code process: ${error.message}`);
});
const processExitPromise = new Promise((resolve, reject) => {
child.on("close", (code) => {
if (abortController.signal.aborted) {
reject(new AbortError("Claude Code process aborted by user"));
}
if (code !== 0) {
reject(new Error(`Claude Code process exited with code ${code}`));
} else {
resolve();
}
});
});
const rl = createInterface({ input: child.stdout });
try {
for await (const line of rl) {
if (processError) {
throw processError;
}
if (line.trim()) {
yield JSON.parse(line);
}
}
} finally {
rl.close();
}
await processExitPromise;
} finally {
cleanup();
abortController.signal.removeEventListener("abort", cleanup);
if (process.env.CLAUDE_SDK_MCP_SERVERS) {
delete process.env.CLAUDE_SDK_MCP_SERVERS;
}
}
}
async function streamToStdin(stream, stdin, abortController) {
for await (const message of stream) {
if (abortController.signal.aborted)
break;
stdin.write(JSON.stringify(message) + `
`);
}
stdin.end();
}
function logDebug(message) {
if (process.env.DEBUG) {
console.debug(message);
}
}
function isRunningWithBun() {
return process.versions.bun !== undefined || process.env.BUN_INSTALL !== undefined;
}
class AbortError extends Error {
}
export {
query,
AbortError
};