anyclaude-sdk 0.1.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/LICENSE +21 -0
- package/README.md +295 -0
- package/dist/agent.d.ts +110 -0
- package/dist/agent.js +897 -0
- package/dist/background/index.d.ts +3 -0
- package/dist/background/index.js +9 -0
- package/dist/background/manager.d.ts +32 -0
- package/dist/background/manager.js +108 -0
- package/dist/background/tools.d.ts +5 -0
- package/dist/background/tools.js +98 -0
- package/dist/background/worker.d.ts +19 -0
- package/dist/background/worker.js +30 -0
- package/dist/commands/builtins.d.ts +2 -0
- package/dist/commands/builtins.js +306 -0
- package/dist/commands/index.d.ts +21 -0
- package/dist/commands/index.js +56 -0
- package/dist/commands/types.d.ts +110 -0
- package/dist/commands/types.js +5 -0
- package/dist/compact.d.ts +22 -0
- package/dist/compact.js +67 -0
- package/dist/fs/dexie.d.ts +57 -0
- package/dist/fs/dexie.js +243 -0
- package/dist/fs/index.d.ts +4 -0
- package/dist/fs/index.js +13 -0
- package/dist/fs/linuxTree.d.ts +11 -0
- package/dist/fs/linuxTree.js +43 -0
- package/dist/fs/opfs.d.ts +23 -0
- package/dist/fs/opfs.js +112 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +29 -0
- package/dist/llm/anthropic.d.ts +24 -0
- package/dist/llm/anthropic.js +280 -0
- package/dist/llm/index.d.ts +3 -0
- package/dist/llm/index.js +3 -0
- package/dist/llm/inlineTools.d.ts +11 -0
- package/dist/llm/inlineTools.js +72 -0
- package/dist/llm/openai.d.ts +29 -0
- package/dist/llm/openai.js +224 -0
- package/dist/llm/responses.d.ts +18 -0
- package/dist/llm/responses.js +256 -0
- package/dist/mcp/client.d.ts +20 -0
- package/dist/mcp/client.js +156 -0
- package/dist/mcp/index.d.ts +24 -0
- package/dist/mcp/index.js +157 -0
- package/dist/mcp/proxy.d.ts +3 -0
- package/dist/mcp/proxy.js +25 -0
- package/dist/mcp/sdkServer.d.ts +21 -0
- package/dist/mcp/sdkServer.js +28 -0
- package/dist/mcp/types.d.ts +92 -0
- package/dist/mcp/types.js +5 -0
- package/dist/memory/index.d.ts +4 -0
- package/dist/memory/index.js +5 -0
- package/dist/memory/render.d.ts +7 -0
- package/dist/memory/render.js +46 -0
- package/dist/memory/store.d.ts +20 -0
- package/dist/memory/store.js +79 -0
- package/dist/memory/tools.d.ts +5 -0
- package/dist/memory/tools.js +95 -0
- package/dist/memory/types.d.ts +15 -0
- package/dist/memory/types.js +4 -0
- package/dist/permissions/dangerous.d.ts +4 -0
- package/dist/permissions/dangerous.js +24 -0
- package/dist/permissions/gate.d.ts +21 -0
- package/dist/permissions/gate.js +66 -0
- package/dist/permissions/index.d.ts +5 -0
- package/dist/permissions/index.js +6 -0
- package/dist/permissions/match.d.ts +19 -0
- package/dist/permissions/match.js +104 -0
- package/dist/permissions/planMode.d.ts +3 -0
- package/dist/permissions/planMode.js +33 -0
- package/dist/permissions/types.d.ts +19 -0
- package/dist/permissions/types.js +2 -0
- package/dist/persist.d.ts +15 -0
- package/dist/persist.js +58 -0
- package/dist/prompt.d.ts +6 -0
- package/dist/prompt.js +34 -0
- package/dist/query.d.ts +105 -0
- package/dist/query.js +115 -0
- package/dist/queue.d.ts +23 -0
- package/dist/queue.js +43 -0
- package/dist/sandbox/cloudflare.d.ts +48 -0
- package/dist/sandbox/cloudflare.js +124 -0
- package/dist/sandbox/daytona.d.ts +48 -0
- package/dist/sandbox/daytona.js +79 -0
- package/dist/sandbox/e2b.d.ts +54 -0
- package/dist/sandbox/e2b.js +87 -0
- package/dist/sandbox/index.d.ts +8 -0
- package/dist/sandbox/index.js +19 -0
- package/dist/sandbox/local.d.ts +51 -0
- package/dist/sandbox/local.js +155 -0
- package/dist/sandbox/types.d.ts +18 -0
- package/dist/sandbox/types.js +27 -0
- package/dist/sandbox/util.d.ts +15 -0
- package/dist/sandbox/util.js +100 -0
- package/dist/sandbox/vercel.d.ts +48 -0
- package/dist/sandbox/vercel.js +130 -0
- package/dist/session/index.d.ts +2 -0
- package/dist/session/index.js +6 -0
- package/dist/session/store.d.ts +28 -0
- package/dist/session/store.js +122 -0
- package/dist/session/types.d.ts +22 -0
- package/dist/session/types.js +2 -0
- package/dist/settings/index.d.ts +3 -0
- package/dist/settings/index.js +3 -0
- package/dist/settings/load.d.ts +20 -0
- package/dist/settings/load.js +36 -0
- package/dist/settings/merge.d.ts +13 -0
- package/dist/settings/merge.js +65 -0
- package/dist/settings/types.d.ts +17 -0
- package/dist/settings/types.js +3 -0
- package/dist/skills/index.d.ts +4 -0
- package/dist/skills/index.js +5 -0
- package/dist/skills/load.d.ts +23 -0
- package/dist/skills/load.js +54 -0
- package/dist/skills/parse.d.ts +7 -0
- package/dist/skills/parse.js +40 -0
- package/dist/skills/tool.d.ts +2 -0
- package/dist/skills/tool.js +39 -0
- package/dist/skills/types.d.ts +10 -0
- package/dist/skills/types.js +4 -0
- package/dist/team/dispatch.d.ts +2 -0
- package/dist/team/dispatch.js +41 -0
- package/dist/team/index.d.ts +9 -0
- package/dist/team/index.js +11 -0
- package/dist/team/mailbox.d.ts +24 -0
- package/dist/team/mailbox.js +33 -0
- package/dist/team/prompt.d.ts +1 -0
- package/dist/team/prompt.js +12 -0
- package/dist/team/runner.d.ts +20 -0
- package/dist/team/runner.js +45 -0
- package/dist/team/taskBoard.d.ts +41 -0
- package/dist/team/taskBoard.js +73 -0
- package/dist/team/tools.d.ts +7 -0
- package/dist/team/tools.js +190 -0
- package/dist/tools/bash.d.ts +2 -0
- package/dist/tools/bash.js +45 -0
- package/dist/tools/config.d.ts +2 -0
- package/dist/tools/config.js +44 -0
- package/dist/tools/define.d.ts +18 -0
- package/dist/tools/define.js +21 -0
- package/dist/tools/delete_file.d.ts +2 -0
- package/dist/tools/delete_file.js +33 -0
- package/dist/tools/edit_file.d.ts +2 -0
- package/dist/tools/edit_file.js +93 -0
- package/dist/tools/fileTypes.d.ts +32 -0
- package/dist/tools/fileTypes.js +166 -0
- package/dist/tools/glob.d.ts +2 -0
- package/dist/tools/glob.js +53 -0
- package/dist/tools/grep.d.ts +2 -0
- package/dist/tools/grep.js +110 -0
- package/dist/tools/imageProcessor.d.ts +15 -0
- package/dist/tools/imageProcessor.js +83 -0
- package/dist/tools/index.d.ts +28 -0
- package/dist/tools/index.js +45 -0
- package/dist/tools/list_files.d.ts +2 -0
- package/dist/tools/list_files.js +42 -0
- package/dist/tools/multi_edit.d.ts +2 -0
- package/dist/tools/multi_edit.js +112 -0
- package/dist/tools/notebook_edit.d.ts +2 -0
- package/dist/tools/notebook_edit.js +118 -0
- package/dist/tools/plan_mode.d.ts +4 -0
- package/dist/tools/plan_mode.js +44 -0
- package/dist/tools/read_file.d.ts +2 -0
- package/dist/tools/read_file.js +193 -0
- package/dist/tools/task.d.ts +2 -0
- package/dist/tools/task.js +77 -0
- package/dist/tools/todo_write.d.ts +2 -0
- package/dist/tools/todo_write.js +104 -0
- package/dist/tools/tool_search.d.ts +2 -0
- package/dist/tools/tool_search.js +49 -0
- package/dist/tools/types.d.ts +82 -0
- package/dist/tools/types.js +1 -0
- package/dist/tools/walk.d.ts +29 -0
- package/dist/tools/walk.js +82 -0
- package/dist/tools/web_fetch.d.ts +2 -0
- package/dist/tools/web_fetch.js +76 -0
- package/dist/tools/web_search.d.ts +22 -0
- package/dist/tools/web_search.js +195 -0
- package/dist/tools/write_file.d.ts +2 -0
- package/dist/tools/write_file.js +39 -0
- package/dist/types/index.d.ts +363 -0
- package/dist/types/index.js +9 -0
- package/dist/util/ids.d.ts +3 -0
- package/dist/util/ids.js +22 -0
- package/dist/util/paths.d.ts +16 -0
- package/dist/util/paths.js +72 -0
- package/dist/util/pricing.d.ts +15 -0
- package/dist/util/pricing.js +81 -0
- package/dist/workspace/index.d.ts +2 -0
- package/dist/workspace/index.js +2 -0
- package/dist/workspace/memory.d.ts +28 -0
- package/dist/workspace/memory.js +97 -0
- package/dist/workspace/webcontainer.d.ts +65 -0
- package/dist/workspace/webcontainer.js +156 -0
- package/package.json +78 -0
|
@@ -0,0 +1,363 @@
|
|
|
1
|
+
export type TextBlock = {
|
|
2
|
+
type: 'text';
|
|
3
|
+
text: string;
|
|
4
|
+
};
|
|
5
|
+
export type ThinkingBlock = {
|
|
6
|
+
type: 'thinking';
|
|
7
|
+
thinking: string;
|
|
8
|
+
signature?: string;
|
|
9
|
+
};
|
|
10
|
+
export type ToolUseBlock = {
|
|
11
|
+
type: 'tool_use';
|
|
12
|
+
id: string;
|
|
13
|
+
name: string;
|
|
14
|
+
input: Record<string, unknown>;
|
|
15
|
+
};
|
|
16
|
+
export type ImageBlock = {
|
|
17
|
+
type: 'image';
|
|
18
|
+
source: {
|
|
19
|
+
type: 'base64';
|
|
20
|
+
media_type: string;
|
|
21
|
+
data: string;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
/** PDF / document block (Anthropic-native; matches @anthropic-ai/sdk DocumentBlockParam). */
|
|
25
|
+
export type DocumentBlock = {
|
|
26
|
+
type: 'document';
|
|
27
|
+
source: {
|
|
28
|
+
type: 'base64';
|
|
29
|
+
media_type: 'application/pdf';
|
|
30
|
+
data: string;
|
|
31
|
+
} | {
|
|
32
|
+
type: 'text';
|
|
33
|
+
media_type: 'text/plain';
|
|
34
|
+
data: string;
|
|
35
|
+
};
|
|
36
|
+
title?: string;
|
|
37
|
+
context?: string;
|
|
38
|
+
};
|
|
39
|
+
export type ToolResultBlock = {
|
|
40
|
+
type: 'tool_result';
|
|
41
|
+
tool_use_id: string;
|
|
42
|
+
content: string | Array<TextBlock | ImageBlock | DocumentBlock>;
|
|
43
|
+
is_error?: boolean;
|
|
44
|
+
};
|
|
45
|
+
export type ContentBlockParam = TextBlock | ThinkingBlock | ToolUseBlock | ToolResultBlock | ImageBlock | DocumentBlock;
|
|
46
|
+
export type StopReason = 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'pause_turn' | 'refusal' | null;
|
|
47
|
+
/** Mirrors @anthropic-ai/sdk MessageParam for role: 'user'. */
|
|
48
|
+
export type APIUserMessage = {
|
|
49
|
+
role: 'user';
|
|
50
|
+
content: string | ContentBlockParam[];
|
|
51
|
+
};
|
|
52
|
+
/** Mirrors @anthropic-ai/sdk Message (assistant turn). */
|
|
53
|
+
export type APIAssistantMessage = {
|
|
54
|
+
id: string;
|
|
55
|
+
type: 'message';
|
|
56
|
+
role: 'assistant';
|
|
57
|
+
model: string;
|
|
58
|
+
content: ContentBlockParam[];
|
|
59
|
+
stop_reason: StopReason;
|
|
60
|
+
stop_sequence: string | null;
|
|
61
|
+
usage: Usage;
|
|
62
|
+
};
|
|
63
|
+
export type Usage = {
|
|
64
|
+
input_tokens: number;
|
|
65
|
+
output_tokens: number;
|
|
66
|
+
cache_read_input_tokens?: number;
|
|
67
|
+
cache_creation_input_tokens?: number;
|
|
68
|
+
};
|
|
69
|
+
export type ModelUsage = {
|
|
70
|
+
inputTokens: number;
|
|
71
|
+
outputTokens: number;
|
|
72
|
+
cacheReadInputTokens: number;
|
|
73
|
+
cacheCreationInputTokens: number;
|
|
74
|
+
webSearchRequests: number;
|
|
75
|
+
costUSD: number;
|
|
76
|
+
contextWindow: number;
|
|
77
|
+
maxOutputTokens: number;
|
|
78
|
+
};
|
|
79
|
+
export type ApiKeySource = 'user' | 'project' | 'org' | 'temporary' | 'oauth' | 'none';
|
|
80
|
+
export type PermissionMode = 'default' | 'acceptEdits' | 'bypassPermissions' | 'plan' | 'dontAsk';
|
|
81
|
+
export type SDKUserMessage = {
|
|
82
|
+
type: 'user';
|
|
83
|
+
message: APIUserMessage;
|
|
84
|
+
parent_tool_use_id: string | null;
|
|
85
|
+
/** ISO 8601 timestamp; optional to match the SDK (consumers fall back to receive time). */
|
|
86
|
+
timestamp?: string;
|
|
87
|
+
isSynthetic?: boolean;
|
|
88
|
+
uuid?: string;
|
|
89
|
+
session_id?: string;
|
|
90
|
+
};
|
|
91
|
+
export type SDKAssistantMessageError = 'authentication_failed' | 'billing_error' | 'rate_limit' | 'invalid_request' | 'server_error' | 'unknown' | 'max_output_tokens';
|
|
92
|
+
export type SDKAssistantMessage = {
|
|
93
|
+
type: 'assistant';
|
|
94
|
+
message: APIAssistantMessage;
|
|
95
|
+
parent_tool_use_id: string | null;
|
|
96
|
+
error?: SDKAssistantMessageError;
|
|
97
|
+
uuid: string;
|
|
98
|
+
session_id: string;
|
|
99
|
+
};
|
|
100
|
+
/** A streaming delta event (mirrors SDKPartialAssistantMessage / 'stream_event'). */
|
|
101
|
+
export type SDKPartialAssistantMessage = {
|
|
102
|
+
type: 'stream_event';
|
|
103
|
+
event: RawMessageStreamEvent;
|
|
104
|
+
parent_tool_use_id: string | null;
|
|
105
|
+
uuid: string;
|
|
106
|
+
session_id: string;
|
|
107
|
+
};
|
|
108
|
+
/** Minimal stream-event shape; matches the Anthropic streaming event tags we emit. */
|
|
109
|
+
export type RawMessageStreamEvent = {
|
|
110
|
+
type: 'message_start';
|
|
111
|
+
message: Partial<APIAssistantMessage>;
|
|
112
|
+
} | {
|
|
113
|
+
type: 'content_block_start';
|
|
114
|
+
index: number;
|
|
115
|
+
content_block: ContentBlockParam;
|
|
116
|
+
} | {
|
|
117
|
+
type: 'content_block_delta';
|
|
118
|
+
index: number;
|
|
119
|
+
delta: {
|
|
120
|
+
type: 'text_delta';
|
|
121
|
+
text: string;
|
|
122
|
+
} | {
|
|
123
|
+
type: 'input_json_delta';
|
|
124
|
+
partial_json: string;
|
|
125
|
+
} | {
|
|
126
|
+
type: 'thinking_delta';
|
|
127
|
+
thinking: string;
|
|
128
|
+
};
|
|
129
|
+
} | {
|
|
130
|
+
type: 'content_block_stop';
|
|
131
|
+
index: number;
|
|
132
|
+
} | {
|
|
133
|
+
type: 'message_delta';
|
|
134
|
+
delta: {
|
|
135
|
+
stop_reason?: StopReason;
|
|
136
|
+
};
|
|
137
|
+
usage?: Partial<Usage>;
|
|
138
|
+
} | {
|
|
139
|
+
type: 'message_stop';
|
|
140
|
+
};
|
|
141
|
+
export type SDKSystemMessage = {
|
|
142
|
+
type: 'system';
|
|
143
|
+
subtype: 'init';
|
|
144
|
+
apiKeySource: ApiKeySource;
|
|
145
|
+
cwd: string;
|
|
146
|
+
tools: string[];
|
|
147
|
+
mcp_servers: Array<{
|
|
148
|
+
name: string;
|
|
149
|
+
status: string;
|
|
150
|
+
}>;
|
|
151
|
+
model: string;
|
|
152
|
+
permissionMode: PermissionMode;
|
|
153
|
+
slash_commands: string[];
|
|
154
|
+
output_style: string;
|
|
155
|
+
skills: string[];
|
|
156
|
+
agents?: string[];
|
|
157
|
+
uuid: string;
|
|
158
|
+
session_id: string;
|
|
159
|
+
};
|
|
160
|
+
export type SDKCompactBoundaryMessage = {
|
|
161
|
+
type: 'system';
|
|
162
|
+
subtype: 'compact_boundary';
|
|
163
|
+
compact_metadata: {
|
|
164
|
+
trigger: 'manual' | 'auto';
|
|
165
|
+
pre_tokens: number;
|
|
166
|
+
};
|
|
167
|
+
uuid: string;
|
|
168
|
+
session_id: string;
|
|
169
|
+
};
|
|
170
|
+
/** Output of a local slash command (e.g. /help, /cost). */
|
|
171
|
+
export type SDKLocalCommandOutputMessage = {
|
|
172
|
+
type: 'system';
|
|
173
|
+
subtype: 'local_command_output';
|
|
174
|
+
content: string;
|
|
175
|
+
uuid: string;
|
|
176
|
+
session_id: string;
|
|
177
|
+
};
|
|
178
|
+
export type SDKPermissionDenial = {
|
|
179
|
+
tool_name: string;
|
|
180
|
+
tool_use_id: string;
|
|
181
|
+
tool_input: Record<string, unknown>;
|
|
182
|
+
};
|
|
183
|
+
export type SDKResultMessage = {
|
|
184
|
+
type: 'result';
|
|
185
|
+
subtype: 'success';
|
|
186
|
+
duration_ms: number;
|
|
187
|
+
duration_api_ms: number;
|
|
188
|
+
is_error: boolean;
|
|
189
|
+
num_turns: number;
|
|
190
|
+
result: string;
|
|
191
|
+
stop_reason: string | null;
|
|
192
|
+
total_cost_usd: number;
|
|
193
|
+
usage: Usage;
|
|
194
|
+
modelUsage: Record<string, ModelUsage>;
|
|
195
|
+
permission_denials: SDKPermissionDenial[];
|
|
196
|
+
uuid: string;
|
|
197
|
+
session_id: string;
|
|
198
|
+
} | {
|
|
199
|
+
type: 'result';
|
|
200
|
+
subtype: 'error_during_execution' | 'error_max_turns' | 'error_max_budget_usd';
|
|
201
|
+
duration_ms: number;
|
|
202
|
+
duration_api_ms: number;
|
|
203
|
+
is_error: boolean;
|
|
204
|
+
num_turns: number;
|
|
205
|
+
stop_reason: string | null;
|
|
206
|
+
total_cost_usd: number;
|
|
207
|
+
usage: Usage;
|
|
208
|
+
modelUsage: Record<string, ModelUsage>;
|
|
209
|
+
permission_denials: SDKPermissionDenial[];
|
|
210
|
+
errors: string[];
|
|
211
|
+
uuid: string;
|
|
212
|
+
session_id: string;
|
|
213
|
+
};
|
|
214
|
+
export type SDKMessage = SDKUserMessage | SDKAssistantMessage | SDKPartialAssistantMessage | SDKSystemMessage | SDKCompactBoundaryMessage | SDKLocalCommandOutputMessage | SDKResultMessage;
|
|
215
|
+
/** A todo item managed by the todo_write tool. */
|
|
216
|
+
export type TodoItem = {
|
|
217
|
+
content: string;
|
|
218
|
+
status: 'pending' | 'in_progress' | 'completed';
|
|
219
|
+
activeForm?: string;
|
|
220
|
+
};
|
|
221
|
+
/**
|
|
222
|
+
* Definition of a custom sub-agent invokable via the `task` tool (mirrors the
|
|
223
|
+
* official SDK's AgentDefinition subset).
|
|
224
|
+
*/
|
|
225
|
+
export type AgentDefinition = {
|
|
226
|
+
/** Natural-language description of when to use this agent. */
|
|
227
|
+
description: string;
|
|
228
|
+
/** The sub-agent's system prompt. */
|
|
229
|
+
prompt: string;
|
|
230
|
+
/** Allowed tool names. If omitted, inherits the parent's tools (minus `task`). */
|
|
231
|
+
tools?: string[];
|
|
232
|
+
/** Model override for this sub-agent. If omitted, inherits the parent model. */
|
|
233
|
+
model?: string;
|
|
234
|
+
};
|
|
235
|
+
/** Mutable per-session state shared with tools (todos, config, etc.). */
|
|
236
|
+
export type AgentStore = {
|
|
237
|
+
todos: TodoItem[];
|
|
238
|
+
/** Free-form session config key/value store (used by the `config` tool). */
|
|
239
|
+
config?: Record<string, unknown>;
|
|
240
|
+
};
|
|
241
|
+
/** Result of a permission check — allow (optionally with modified input) or deny. */
|
|
242
|
+
export type PermissionResult = {
|
|
243
|
+
behavior: 'allow';
|
|
244
|
+
updatedInput?: Record<string, unknown>;
|
|
245
|
+
} | {
|
|
246
|
+
behavior: 'deny';
|
|
247
|
+
message: string;
|
|
248
|
+
interrupt?: boolean;
|
|
249
|
+
};
|
|
250
|
+
/**
|
|
251
|
+
* Permission gate invoked before each tool runs. Return allow/deny.
|
|
252
|
+
* Mirrors the official SDK's `canUseTool` option.
|
|
253
|
+
*/
|
|
254
|
+
export type CanUseTool = (toolName: string, input: Record<string, unknown>, options: {
|
|
255
|
+
signal?: AbortSignal;
|
|
256
|
+
toolUseId: string;
|
|
257
|
+
}) => Promise<PermissionResult> | PermissionResult;
|
|
258
|
+
/** Lifecycle hook events we surface (subset of the SDK's 27 hook events). */
|
|
259
|
+
export type HookEvent = 'PreToolUse' | 'PostToolUse' | 'PostToolUseFailure' | 'PermissionRequest' | 'PermissionDenied' | 'FileChanged' | 'UserPromptSubmit' | 'Notification' | 'Stop' | 'SessionStart' | 'SessionEnd' | 'SubagentStart' | 'SubagentStop' | 'PreCompact' | 'PostCompact';
|
|
260
|
+
export type PreToolUseHookInput = {
|
|
261
|
+
hook_event_name: 'PreToolUse';
|
|
262
|
+
tool_name: string;
|
|
263
|
+
tool_input: Record<string, unknown>;
|
|
264
|
+
tool_use_id: string;
|
|
265
|
+
};
|
|
266
|
+
export type PostToolUseHookInput = {
|
|
267
|
+
hook_event_name: 'PostToolUse';
|
|
268
|
+
tool_name: string;
|
|
269
|
+
tool_input: Record<string, unknown>;
|
|
270
|
+
tool_response: unknown;
|
|
271
|
+
tool_use_id: string;
|
|
272
|
+
};
|
|
273
|
+
/** Generic lifecycle hook input (session/subagent/compaction/prompt/permission/file events). */
|
|
274
|
+
export type LifecycleHookInput = {
|
|
275
|
+
hook_event_name: 'UserPromptSubmit' | 'Notification' | 'Stop' | 'SessionStart' | 'SessionEnd' | 'SubagentStart' | 'SubagentStop' | 'PreCompact' | 'PostCompact' | 'PermissionRequest' | 'PermissionDenied' | 'PostToolUseFailure' | 'FileChanged';
|
|
276
|
+
[key: string]: unknown;
|
|
277
|
+
};
|
|
278
|
+
export type HookInput = PreToolUseHookInput | PostToolUseHookInput | LifecycleHookInput;
|
|
279
|
+
/** A permission update a PermissionRequest hook may suggest. */
|
|
280
|
+
export type PermissionUpdateSuggestion = {
|
|
281
|
+
type: 'addRules' | 'replaceRules' | 'removeRules';
|
|
282
|
+
behavior?: 'allow' | 'deny' | 'ask';
|
|
283
|
+
rules?: Array<{
|
|
284
|
+
toolName: string;
|
|
285
|
+
ruleContent?: string;
|
|
286
|
+
}>;
|
|
287
|
+
};
|
|
288
|
+
/**
|
|
289
|
+
* Hook callback output. `permissionDecision` on PreToolUse can short-circuit a
|
|
290
|
+
* tool ('deny'/'ask'→deny); `additionalContext` is appended to the tool result.
|
|
291
|
+
*/
|
|
292
|
+
export type HookOutput = {
|
|
293
|
+
decision?: 'approve' | 'block';
|
|
294
|
+
permissionDecision?: 'allow' | 'deny' | 'ask';
|
|
295
|
+
permissionDecisionReason?: string;
|
|
296
|
+
additionalContext?: string;
|
|
297
|
+
systemMessage?: string;
|
|
298
|
+
/** Permission rule updates suggested by a PermissionRequest hook. */
|
|
299
|
+
permissionUpdates?: PermissionUpdateSuggestion[];
|
|
300
|
+
} | void;
|
|
301
|
+
export type HookCallback = (input: HookInput, options: {
|
|
302
|
+
signal?: AbortSignal;
|
|
303
|
+
}) => Promise<HookOutput> | HookOutput;
|
|
304
|
+
export type ToolDef = {
|
|
305
|
+
type: 'function';
|
|
306
|
+
function: {
|
|
307
|
+
name: string;
|
|
308
|
+
description: string;
|
|
309
|
+
parameters: {
|
|
310
|
+
type: 'object';
|
|
311
|
+
properties: Record<string, unknown>;
|
|
312
|
+
required?: string[];
|
|
313
|
+
};
|
|
314
|
+
};
|
|
315
|
+
};
|
|
316
|
+
export type ToolCall = {
|
|
317
|
+
id: string;
|
|
318
|
+
type: 'function';
|
|
319
|
+
function: {
|
|
320
|
+
name: string;
|
|
321
|
+
arguments: string;
|
|
322
|
+
};
|
|
323
|
+
};
|
|
324
|
+
export interface FileSystem {
|
|
325
|
+
readFile(path: string): Promise<string | null>;
|
|
326
|
+
readBinary(path: string): Promise<Uint8Array | null>;
|
|
327
|
+
writeFile(path: string, contents: string): Promise<void>;
|
|
328
|
+
writeBinary(path: string, data: Uint8Array): Promise<void>;
|
|
329
|
+
deleteFile(path: string): Promise<void>;
|
|
330
|
+
readdir(path: string): Promise<Array<{
|
|
331
|
+
name: string;
|
|
332
|
+
isDir: boolean;
|
|
333
|
+
}> | null>;
|
|
334
|
+
mkdir(path: string): Promise<void>;
|
|
335
|
+
}
|
|
336
|
+
export interface CommandExecutor {
|
|
337
|
+
exec(command: string, timeoutMs?: number, env?: Record<string, string>): Promise<{
|
|
338
|
+
output: string;
|
|
339
|
+
exitCode: number;
|
|
340
|
+
}>;
|
|
341
|
+
}
|
|
342
|
+
export interface LLMClient {
|
|
343
|
+
streamChat(messages: ChatMsg[], opts: {
|
|
344
|
+
model?: string;
|
|
345
|
+
tools?: ToolDef[];
|
|
346
|
+
signal?: AbortSignal;
|
|
347
|
+
onToken: (delta: string) => void;
|
|
348
|
+
onTool?: (calls: ToolCall[]) => void;
|
|
349
|
+
}): Promise<StreamResult>;
|
|
350
|
+
}
|
|
351
|
+
export type ChatMsg = {
|
|
352
|
+
role: 'system' | 'user' | 'assistant' | 'tool';
|
|
353
|
+
content: string | ContentBlockParam[];
|
|
354
|
+
tool_calls?: ToolCall[];
|
|
355
|
+
tool_call_id?: string;
|
|
356
|
+
};
|
|
357
|
+
export type StreamResult = {
|
|
358
|
+
text: string;
|
|
359
|
+
toolCalls: ToolCall[];
|
|
360
|
+
model: string;
|
|
361
|
+
usage?: Usage;
|
|
362
|
+
stopReason?: StopReason;
|
|
363
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Core types for browser-claude-sdk — a local, browser-compatible implementation
|
|
2
|
+
// of Claude Code agent capabilities.
|
|
3
|
+
//
|
|
4
|
+
// The `SDK*` message family mirrors the official `@anthropic-ai/claude-agent-sdk`
|
|
5
|
+
// message envelope (see Claude Code's sdk/coreSchemas.ts) so consumers written
|
|
6
|
+
// against the real SDK can iterate our `query()` output unchanged. The
|
|
7
|
+
// transport-layer interfaces (LLMClient / ChatMsg / ToolDef / ToolCall /
|
|
8
|
+
// FileSystem / CommandExecutor) are our own OpenAI-compatible additions.
|
|
9
|
+
export {};
|
package/dist/util/ids.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Small id/uuid helpers, browser-safe with graceful fallback.
|
|
2
|
+
function rand() {
|
|
3
|
+
// crypto.randomUUID is available in all modern browsers and Node 19+.
|
|
4
|
+
const c = globalThis.crypto;
|
|
5
|
+
if (c?.randomUUID)
|
|
6
|
+
return c.randomUUID();
|
|
7
|
+
// Fallback: not cryptographically strong, only used for correlation ids.
|
|
8
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (ch) => {
|
|
9
|
+
const r = (Math.random() * 16) | 0;
|
|
10
|
+
const v = ch === 'x' ? r : (r & 0x3) | 0x8;
|
|
11
|
+
return v.toString(16);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
export function uuid() {
|
|
15
|
+
return rand();
|
|
16
|
+
}
|
|
17
|
+
export function toolCallId() {
|
|
18
|
+
return 'call_' + rand().replace(/-/g, '').slice(0, 24);
|
|
19
|
+
}
|
|
20
|
+
export function now() {
|
|
21
|
+
return Date.now();
|
|
22
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** Collapse `.`/`..` segments and duplicate slashes. Preserves leading `/`. */
|
|
2
|
+
export declare function normalizePath(p: string): string;
|
|
3
|
+
/** Join segments with `/` and normalize. */
|
|
4
|
+
export declare function joinPath(...parts: string[]): string;
|
|
5
|
+
/** Resolve `p` against `cwd` when relative; normalize either way. */
|
|
6
|
+
export declare function resolvePath(cwd: string, p: string): string;
|
|
7
|
+
/** Directory portion of a path (like POSIX dirname). */
|
|
8
|
+
export declare function dirname(p: string): string;
|
|
9
|
+
/** Final path segment (like POSIX basename). */
|
|
10
|
+
export declare function basename(p: string): string;
|
|
11
|
+
/** Lowercased extension including the dot (e.g. ".ts"), or "" if none. */
|
|
12
|
+
export declare function extname(p: string): string;
|
|
13
|
+
/** Split an absolute/relative path into its non-empty segments. */
|
|
14
|
+
export declare function segments(p: string): string[];
|
|
15
|
+
/** Every ancestor directory of `p`, from `/` (or first segment) down to the parent. */
|
|
16
|
+
export declare function ancestors(p: string): string[];
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// Shared POSIX-style path utilities (browser-safe, no node:path dependency).
|
|
2
|
+
// Used by the persistent filesystems and sandbox adapters so path handling is
|
|
3
|
+
// consistent across the SDK.
|
|
4
|
+
/** Collapse `.`/`..` segments and duplicate slashes. Preserves leading `/`. */
|
|
5
|
+
export function normalizePath(p) {
|
|
6
|
+
const isAbs = p.startsWith('/');
|
|
7
|
+
const out = [];
|
|
8
|
+
for (const seg of p.split('/')) {
|
|
9
|
+
if (seg === '' || seg === '.')
|
|
10
|
+
continue;
|
|
11
|
+
if (seg === '..') {
|
|
12
|
+
if (out.length && out[out.length - 1] !== '..')
|
|
13
|
+
out.pop();
|
|
14
|
+
else if (!isAbs)
|
|
15
|
+
out.push('..');
|
|
16
|
+
}
|
|
17
|
+
else {
|
|
18
|
+
out.push(seg);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return (isAbs ? '/' : '') + out.join('/');
|
|
22
|
+
}
|
|
23
|
+
/** Join segments with `/` and normalize. */
|
|
24
|
+
export function joinPath(...parts) {
|
|
25
|
+
return normalizePath(parts.filter(Boolean).join('/'));
|
|
26
|
+
}
|
|
27
|
+
/** Resolve `p` against `cwd` when relative; normalize either way. */
|
|
28
|
+
export function resolvePath(cwd, p) {
|
|
29
|
+
if (p.startsWith('/'))
|
|
30
|
+
return normalizePath(p);
|
|
31
|
+
return normalizePath(`${cwd}/${p}`);
|
|
32
|
+
}
|
|
33
|
+
/** Directory portion of a path (like POSIX dirname). */
|
|
34
|
+
export function dirname(p) {
|
|
35
|
+
const n = normalizePath(p);
|
|
36
|
+
const i = n.lastIndexOf('/');
|
|
37
|
+
if (i < 0)
|
|
38
|
+
return '.';
|
|
39
|
+
if (i === 0)
|
|
40
|
+
return '/';
|
|
41
|
+
return n.slice(0, i);
|
|
42
|
+
}
|
|
43
|
+
/** Final path segment (like POSIX basename). */
|
|
44
|
+
export function basename(p) {
|
|
45
|
+
const n = normalizePath(p);
|
|
46
|
+
const i = n.lastIndexOf('/');
|
|
47
|
+
return i < 0 ? n : n.slice(i + 1);
|
|
48
|
+
}
|
|
49
|
+
/** Lowercased extension including the dot (e.g. ".ts"), or "" if none. */
|
|
50
|
+
export function extname(p) {
|
|
51
|
+
const base = basename(p);
|
|
52
|
+
const i = base.lastIndexOf('.');
|
|
53
|
+
return i > 0 ? base.slice(i).toLowerCase() : '';
|
|
54
|
+
}
|
|
55
|
+
/** Split an absolute/relative path into its non-empty segments. */
|
|
56
|
+
export function segments(p) {
|
|
57
|
+
return normalizePath(p)
|
|
58
|
+
.split('/')
|
|
59
|
+
.filter((s) => s && s !== '.');
|
|
60
|
+
}
|
|
61
|
+
/** Every ancestor directory of `p`, from `/` (or first segment) down to the parent. */
|
|
62
|
+
export function ancestors(p) {
|
|
63
|
+
const segs = segments(p);
|
|
64
|
+
const isAbs = normalizePath(p).startsWith('/');
|
|
65
|
+
const out = [];
|
|
66
|
+
let cur = isAbs ? '' : '';
|
|
67
|
+
for (let i = 0; i < segs.length - 1; i++) {
|
|
68
|
+
cur = cur + '/' + segs[i];
|
|
69
|
+
out.push(normalizePath((isAbs ? '' : '') + cur));
|
|
70
|
+
}
|
|
71
|
+
return isAbs ? out.map((a) => (a.startsWith('/') ? a : '/' + a)) : out;
|
|
72
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Usage } from '../types/index.js';
|
|
2
|
+
export type Pricing = {
|
|
3
|
+
inputPerM: number;
|
|
4
|
+
outputPerM: number;
|
|
5
|
+
/** Cost of cache-read (cached input) tokens per 1M. Defaults to a fraction of input. */
|
|
6
|
+
cacheReadPerM?: number;
|
|
7
|
+
/** Cost of cache-write (cache creation) tokens per 1M. */
|
|
8
|
+
cacheWritePerM?: number;
|
|
9
|
+
};
|
|
10
|
+
/** Context window for a model id by longest-prefix match (0 if unknown). */
|
|
11
|
+
export declare function contextWindowFor(model: string): number;
|
|
12
|
+
/** Find pricing for a model id by longest-prefix match. */
|
|
13
|
+
export declare function priceFor(model: string): Pricing | undefined;
|
|
14
|
+
/** Estimate USD cost for a usage record under a model's pricing. 0 if unknown. */
|
|
15
|
+
export declare function computeCostUSD(model: string, usage: Usage): number;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Best-effort token pricing for cost accounting. USD per 1M tokens.
|
|
2
|
+
// Matched by longest-prefix against the model id; unknown models cost 0 (and
|
|
3
|
+
// callers can detect that via `priceFor` returning undefined).
|
|
4
|
+
//
|
|
5
|
+
// These are list prices and may drift — treat total_cost_usd as an estimate.
|
|
6
|
+
// Keys are prefixes; the longest matching prefix wins.
|
|
7
|
+
const TABLE = {
|
|
8
|
+
// Anthropic
|
|
9
|
+
'claude-opus-4': { inputPerM: 15, outputPerM: 75, cacheReadPerM: 1.5, cacheWritePerM: 18.75 },
|
|
10
|
+
'claude-sonnet-4': { inputPerM: 3, outputPerM: 15, cacheReadPerM: 0.3, cacheWritePerM: 3.75 },
|
|
11
|
+
'claude-haiku-4': { inputPerM: 1, outputPerM: 5, cacheReadPerM: 0.1, cacheWritePerM: 1.25 },
|
|
12
|
+
'claude-3-5-haiku': { inputPerM: 0.8, outputPerM: 4, cacheReadPerM: 0.08, cacheWritePerM: 1 },
|
|
13
|
+
'claude-3-5-sonnet': { inputPerM: 3, outputPerM: 15, cacheReadPerM: 0.3, cacheWritePerM: 3.75 },
|
|
14
|
+
'claude-3-opus': { inputPerM: 15, outputPerM: 75 },
|
|
15
|
+
// OpenAI
|
|
16
|
+
'gpt-4o-mini': { inputPerM: 0.15, outputPerM: 0.6, cacheReadPerM: 0.075 },
|
|
17
|
+
'gpt-4o': { inputPerM: 2.5, outputPerM: 10, cacheReadPerM: 1.25 },
|
|
18
|
+
'gpt-4.1-mini': { inputPerM: 0.4, outputPerM: 1.6 },
|
|
19
|
+
'gpt-4.1': { inputPerM: 2, outputPerM: 8 },
|
|
20
|
+
'o3-mini': { inputPerM: 1.1, outputPerM: 4.4 },
|
|
21
|
+
'o3': { inputPerM: 2, outputPerM: 8 },
|
|
22
|
+
'o1': { inputPerM: 15, outputPerM: 60 },
|
|
23
|
+
// xAI (Grok). Cached-input rate not published per-model → defaults apply.
|
|
24
|
+
'grok-build': { inputPerM: 1, outputPerM: 2, cacheReadPerM: 0.25 },
|
|
25
|
+
'grok-code': { inputPerM: 0.2, outputPerM: 1.5 },
|
|
26
|
+
'grok-4': { inputPerM: 3, outputPerM: 15 },
|
|
27
|
+
'grok-3-mini': { inputPerM: 0.3, outputPerM: 0.5 },
|
|
28
|
+
'grok-3': { inputPerM: 3, outputPerM: 15 },
|
|
29
|
+
};
|
|
30
|
+
// Context window (max input+output tokens) by model prefix. Best-effort.
|
|
31
|
+
const CONTEXT_WINDOWS = {
|
|
32
|
+
'grok-build': 256_000,
|
|
33
|
+
'grok-code': 256_000,
|
|
34
|
+
'grok-4': 256_000,
|
|
35
|
+
'grok-3': 131_072,
|
|
36
|
+
'claude-sonnet-4': 200_000,
|
|
37
|
+
'claude-opus-4': 200_000,
|
|
38
|
+
'claude-haiku-4': 200_000,
|
|
39
|
+
'claude-3': 200_000,
|
|
40
|
+
'gpt-4o': 128_000,
|
|
41
|
+
'gpt-4.1': 1_047_576,
|
|
42
|
+
o3: 200_000,
|
|
43
|
+
o1: 200_000,
|
|
44
|
+
};
|
|
45
|
+
/** Context window for a model id by longest-prefix match (0 if unknown). */
|
|
46
|
+
export function contextWindowFor(model) {
|
|
47
|
+
const id = model.toLowerCase();
|
|
48
|
+
let best;
|
|
49
|
+
for (const [key, v] of Object.entries(CONTEXT_WINDOWS)) {
|
|
50
|
+
if (id.includes(key) && (!best || key.length > best.key.length))
|
|
51
|
+
best = { key, v };
|
|
52
|
+
}
|
|
53
|
+
return best?.v ?? 0;
|
|
54
|
+
}
|
|
55
|
+
/** Find pricing for a model id by longest-prefix match. */
|
|
56
|
+
export function priceFor(model) {
|
|
57
|
+
const id = model.toLowerCase();
|
|
58
|
+
let best;
|
|
59
|
+
for (const [key, p] of Object.entries(TABLE)) {
|
|
60
|
+
if (id.includes(key) && (!best || key.length > best.key.length))
|
|
61
|
+
best = { key, p };
|
|
62
|
+
}
|
|
63
|
+
return best?.p;
|
|
64
|
+
}
|
|
65
|
+
/** Estimate USD cost for a usage record under a model's pricing. 0 if unknown. */
|
|
66
|
+
export function computeCostUSD(model, usage) {
|
|
67
|
+
const p = priceFor(model);
|
|
68
|
+
if (!p)
|
|
69
|
+
return 0;
|
|
70
|
+
const cacheRead = usage.cache_read_input_tokens ?? 0;
|
|
71
|
+
const cacheWrite = usage.cache_creation_input_tokens ?? 0;
|
|
72
|
+
// input_tokens from providers typically excludes cached/cache-creation; bill
|
|
73
|
+
// those at their own rates and the remainder at the input rate.
|
|
74
|
+
const plainInput = Math.max(0, usage.input_tokens - cacheRead - cacheWrite);
|
|
75
|
+
const cost = (plainInput * p.inputPerM +
|
|
76
|
+
usage.output_tokens * p.outputPerM +
|
|
77
|
+
cacheRead * (p.cacheReadPerM ?? p.inputPerM * 0.1) +
|
|
78
|
+
cacheWrite * (p.cacheWritePerM ?? p.inputPerM * 1.25)) /
|
|
79
|
+
1_000_000;
|
|
80
|
+
return cost;
|
|
81
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { CommandExecutor, FileSystem } from '../types/index.js';
|
|
2
|
+
/**
|
|
3
|
+
* A pure in-memory FileSystem. Useful for tests and environments without a
|
|
4
|
+
* WebContainer. Does not implement CommandExecutor — pair it with a custom
|
|
5
|
+
* executor or `NoopCommandExecutor` if bash is not needed.
|
|
6
|
+
*/
|
|
7
|
+
export declare class MemoryFileSystem implements FileSystem {
|
|
8
|
+
private files;
|
|
9
|
+
private dirs;
|
|
10
|
+
private norm;
|
|
11
|
+
readFile(path: string): Promise<string | null>;
|
|
12
|
+
readBinary(path: string): Promise<Uint8Array | null>;
|
|
13
|
+
writeFile(path: string, contents: string): Promise<void>;
|
|
14
|
+
writeBinary(path: string, data: Uint8Array): Promise<void>;
|
|
15
|
+
deleteFile(path: string): Promise<void>;
|
|
16
|
+
readdir(path: string): Promise<Array<{
|
|
17
|
+
name: string;
|
|
18
|
+
isDir: boolean;
|
|
19
|
+
}> | null>;
|
|
20
|
+
mkdir(path: string): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
/** A CommandExecutor that refuses to run anything — for FS-only workspaces. */
|
|
23
|
+
export declare class NoopCommandExecutor implements CommandExecutor {
|
|
24
|
+
exec(): Promise<{
|
|
25
|
+
output: string;
|
|
26
|
+
exitCode: number;
|
|
27
|
+
}>;
|
|
28
|
+
}
|