@anthropic-ai/claude-code 1.0.18 → 1.0.19

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/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@anthropic-ai/claude-code",
3
- "version": "1.0.18",
3
+ "version": "1.0.19",
4
+ "main": "sdk.mjs",
4
5
  "bin": {
5
6
  "claude": "cli.js"
6
7
  },
package/sdk.d.ts ADDED
@@ -0,0 +1,145 @@
1
+ import type {
2
+ Message as APIAssistantMessage,
3
+ MessageParam as APIUserMessage,
4
+ Usage,
5
+ } from '@anthropic-ai/sdk/resources/index.mjs'
6
+
7
+ export type NonNullableUsage = {
8
+ [K in keyof Usage]: NonNullable<Usage[K]>
9
+ }
10
+
11
+ export type ApiKeySource = 'user' | 'project' | 'org' | 'temporary'
12
+
13
+ export type ConfigScope = 'local' | 'user' | 'project'
14
+
15
+ export type McpStdioServerConfig = {
16
+ type?: 'stdio' // Optional for backwards compatibility
17
+ command: string
18
+ args?: string[]
19
+ env?: Record<string, string>
20
+ }
21
+
22
+ export type McpSSEServerConfig = {
23
+ type: 'sse'
24
+ url: string
25
+ headers?: Record<string, string>
26
+ }
27
+
28
+ export type McpServerConfig = McpStdioServerConfig | McpSSEServerConfig
29
+
30
+ export type Options = {
31
+ abortController?: AbortController
32
+ allowedTools?: string[]
33
+ appendSystemPrompt?: string
34
+ customSystemPrompt?: string
35
+ cwd?: string
36
+ disallowedTools?: string[]
37
+ executable?: 'bun' | 'deno' | 'node'
38
+ executableArgs?: string[]
39
+ maxThinkingTokens?: number
40
+ maxTurns?: number
41
+ mcpServers?: Record<string, McpServerConfig>
42
+ pathToClaudeCodeExecutable?: string
43
+ permissionMode?: PermissionMode
44
+ permissionPromptToolName?: string
45
+ continue?: boolean
46
+ resume?: string
47
+ userSpecifiedModel?: string
48
+ }
49
+
50
+ export type PermissionMode =
51
+ | 'default'
52
+ | 'acceptEdits'
53
+ | 'bypassPermissions'
54
+ | 'plan'
55
+
56
+ export type SDKUserMessage = {
57
+ type: 'user'
58
+ message: APIUserMessage
59
+ parent_tool_use_id: string | null
60
+ session_id: string
61
+ }
62
+
63
+ export type SDKAssistantMessage = {
64
+ type: 'assistant'
65
+ message: APIAssistantMessage
66
+ parent_tool_use_id: string | null
67
+ session_id: string
68
+ }
69
+
70
+ export type SDKResultMessage =
71
+ | {
72
+ type: 'result'
73
+ subtype: 'success'
74
+ cost_usd: number
75
+ duration_ms: number
76
+ duration_api_ms: number
77
+ is_error: boolean
78
+ num_turns: number
79
+ result: string
80
+ session_id: string
81
+ total_cost: number
82
+ usage: NonNullableUsage
83
+ }
84
+ | {
85
+ type: 'result'
86
+ subtype: 'error_max_turns' | 'error_during_execution'
87
+ cost_usd: number
88
+ duration_ms: number
89
+ duration_api_ms: number
90
+ is_error: boolean
91
+ num_turns: number
92
+ session_id: string
93
+ total_cost: number
94
+ usage: NonNullableUsage
95
+ }
96
+
97
+ export type SDKSystemMessage = {
98
+ type: 'system'
99
+ subtype: 'init'
100
+ apiKeySource: ApiKeySource
101
+ cwd: string
102
+ session_id: string
103
+ tools: string[]
104
+ mcp_servers: {
105
+ name: string
106
+ status: string
107
+ }[]
108
+ model: string
109
+ permissionMode: PermissionMode
110
+ }
111
+
112
+ export type SDKMessage =
113
+ | SDKAssistantMessage
114
+ | SDKUserMessage
115
+ | SDKResultMessage
116
+ | SDKSystemMessage
117
+
118
+ type Props = {
119
+ prompt: string
120
+ abortController?: AbortController
121
+ options?: Options
122
+ }
123
+
124
+ /**
125
+ * Query Claude Code
126
+ *
127
+ * Behavior:
128
+ * - Yields a message at a time
129
+ * - Uses the tools and commands you give it
130
+ *
131
+ * Usage:
132
+ * ```ts
133
+ * const response = query({ prompt: "Help me write a function", options: {} })
134
+ * for await (const message of response) {
135
+ * console.log(message)
136
+ * }
137
+ * ```
138
+ */
139
+ export function query({
140
+ prompt,
141
+ abortController,
142
+ options,
143
+ }: Props): AsyncGenerator<SDKMessage>
144
+
145
+ export class AbortError extends Error {}
package/sdk.mjs ADDED
@@ -0,0 +1,141 @@
1
+ // (c) Anthropic PBC. All rights reserved. Use is subject to Anthropic's Commercial Terms of Service (https://www.anthropic.com/legal/commercial-terms).
2
+
3
+ // Version: 1.0.19
4
+
5
+ // src/entrypoints/sdk.ts
6
+ import { spawn } from "child_process";
7
+ import { join } from "path";
8
+ import { fileURLToPath } from "url";
9
+ import { createInterface } from "readline";
10
+ import { existsSync } from "fs";
11
+ var __filename2 = fileURLToPath(import.meta.url);
12
+ var __dirname2 = join(__filename2, "..");
13
+ async function* query({
14
+ prompt,
15
+ options: {
16
+ abortController = new AbortController,
17
+ allowedTools = [],
18
+ appendSystemPrompt,
19
+ customSystemPrompt,
20
+ cwd,
21
+ disallowedTools = [],
22
+ executable = "node",
23
+ executableArgs = [],
24
+ maxTurns,
25
+ mcpServers,
26
+ pathToClaudeCodeExecutable = join(__dirname2, "cli.js"),
27
+ permissionMode = "default",
28
+ permissionPromptToolName,
29
+ continue: continueConversation,
30
+ resume,
31
+ userSpecifiedModel
32
+ } = {}
33
+ }) {
34
+ process.env.CLAUDE_CODE_ENTRYPOINT = "sdk-ts";
35
+ const args = ["--output-format", "stream-json", "--verbose"];
36
+ if (customSystemPrompt)
37
+ args.push("--system-prompt", customSystemPrompt);
38
+ if (appendSystemPrompt)
39
+ args.push("--append-system-prompt", appendSystemPrompt);
40
+ if (maxTurns)
41
+ args.push("--max-turns", maxTurns.toString());
42
+ if (userSpecifiedModel)
43
+ args.push("--model", userSpecifiedModel);
44
+ if (permissionPromptToolName)
45
+ args.push("--permission-prompt-tool", permissionPromptToolName);
46
+ if (continueConversation)
47
+ args.push("--continue");
48
+ if (resume)
49
+ args.push("--resume", resume);
50
+ if (allowedTools.length > 0) {
51
+ args.push("--allowedTools", allowedTools.join(","));
52
+ }
53
+ if (disallowedTools.length > 0) {
54
+ args.push("--disallowedTools", disallowedTools.join(","));
55
+ }
56
+ if (mcpServers && Object.keys(mcpServers).length > 0) {
57
+ args.push("--mcp-config", JSON.stringify({ mcpServers }));
58
+ }
59
+ if (permissionMode !== "default") {
60
+ args.push("--permission-mode", permissionMode);
61
+ }
62
+ if (!prompt.trim()) {
63
+ throw new RangeError("Prompt is required");
64
+ }
65
+ args.push("--print", prompt.trim());
66
+ if (!existsSync(pathToClaudeCodeExecutable)) {
67
+ throw new ReferenceError(`Claude Code executable not found at ${pathToClaudeCodeExecutable}. Is options.pathToClaudeCodeExecutable set?`);
68
+ }
69
+ logDebug(`Spawning Claude Code process: ${executable} ${[...executableArgs, pathToClaudeCodeExecutable, ...args].join(" ")}`);
70
+ const child = spawn(executable, [...executableArgs, pathToClaudeCodeExecutable, ...args], {
71
+ cwd,
72
+ stdio: ["pipe", "pipe", "pipe"],
73
+ signal: abortController.signal,
74
+ env: {
75
+ ...process.env
76
+ }
77
+ });
78
+ child.stdin.end();
79
+ if (process.env.DEBUG) {
80
+ child.stderr.on("data", (data) => {
81
+ console.error("Claude Code stderr:", data.toString());
82
+ });
83
+ }
84
+ const cleanup = () => {
85
+ if (!child.killed) {
86
+ child.kill("SIGTERM");
87
+ }
88
+ };
89
+ abortController.signal.addEventListener("abort", cleanup);
90
+ process.on("exit", cleanup);
91
+ try {
92
+ let processError = null;
93
+ child.on("error", (error) => {
94
+ processError = new Error(`Failed to spawn Claude Code process: ${error.message}`);
95
+ });
96
+ const processExitPromise = new Promise((resolve, reject) => {
97
+ child.on("close", (code) => {
98
+ if (abortController.signal.aborted) {
99
+ reject(new AbortError("Claude Code process aborted by user"));
100
+ }
101
+ if (code !== 0) {
102
+ reject(new Error(`Claude Code process exited with code ${code}`));
103
+ } else {
104
+ resolve();
105
+ }
106
+ });
107
+ });
108
+ const rl = createInterface({ input: child.stdout });
109
+ try {
110
+ for await (const line of rl) {
111
+ if (processError) {
112
+ throw processError;
113
+ }
114
+ if (line.trim()) {
115
+ yield JSON.parse(line);
116
+ }
117
+ }
118
+ } finally {
119
+ rl.close();
120
+ }
121
+ await processExitPromise;
122
+ } finally {
123
+ cleanup();
124
+ abortController.signal.removeEventListener("abort", cleanup);
125
+ if (process.env.CLAUDE_SDK_MCP_SERVERS) {
126
+ delete process.env.CLAUDE_SDK_MCP_SERVERS;
127
+ }
128
+ }
129
+ }
130
+ function logDebug(message) {
131
+ if (process.env.DEBUG) {
132
+ console.debug(message);
133
+ }
134
+ }
135
+
136
+ class AbortError extends Error {
137
+ }
138
+ export {
139
+ query,
140
+ AbortError
141
+ };
Binary file