@r-cli/sdk 1.0.113 → 1.0.115

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 (2) hide show
  1. package/package.json +7 -3
  2. package/sdk.d.ts +568 -0
package/package.json CHANGED
@@ -1,14 +1,19 @@
1
1
  {
2
2
  "name": "@r-cli/sdk",
3
- "version": "1.0.113",
3
+ "version": "1.0.115",
4
4
  "description": "SDK for Claude Tools with tool, query, and createSdkMcpServer exports",
5
5
  "type": "module",
6
6
  "main": "./sdk.mjs",
7
+ "types": "./sdk.d.ts",
7
8
  "exports": {
8
- ".": "./sdk.mjs"
9
+ ".": {
10
+ "import": "./sdk.mjs",
11
+ "types": "./sdk.d.ts"
12
+ }
9
13
  },
10
14
  "files": [
11
15
  "sdk.mjs",
16
+ "sdk.d.ts",
12
17
  "README.md"
13
18
  ],
14
19
  "keywords": [
@@ -31,4 +36,3 @@
31
36
  "access": "public"
32
37
  }
33
38
  }
34
-
package/sdk.d.ts ADDED
@@ -0,0 +1,568 @@
1
+ import type { MessageParam as APIUserMessage } from "@anthropic-ai/sdk/resources";
2
+ import type {
3
+ BetaMessage as APIAssistantMessage,
4
+ BetaRawMessageStreamEvent as RawMessageStreamEvent,
5
+ BetaUsage as Usage,
6
+ } from "@anthropic-ai/sdk/resources/beta/messages/messages.mjs";
7
+ import { type McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
8
+ import type { CallToolResult } from "@modelcontextprotocol/sdk/types.js";
9
+ import type { UUID } from "crypto";
10
+ import { type z, type ZodObject, type ZodRawShape } from "zod";
11
+ export type NonNullableUsage = {
12
+ [K in keyof Usage]: NonNullable<Usage[K]>;
13
+ };
14
+ export type ModelUsage = {
15
+ inputTokens: number;
16
+ outputTokens: number;
17
+ cacheReadInputTokens: number;
18
+ cacheCreationInputTokens: number;
19
+ webSearchRequests: number;
20
+ costUSD: number;
21
+ contextWindow: number;
22
+ };
23
+ export type ApiKeySource = "user" | "project" | "org" | "temporary";
24
+ export type ConfigScope = "local" | "user" | "project";
25
+ export type McpStdioServerConfig = {
26
+ type?: "stdio";
27
+ command: string;
28
+ args?: string[];
29
+ env?: Record<string, string>;
30
+ };
31
+ export type McpSSEServerConfig = {
32
+ type: "sse";
33
+ url: string;
34
+ headers?: Record<string, string>;
35
+ };
36
+ export type McpHttpServerConfig = {
37
+ type: "http";
38
+ url: string;
39
+ headers?: Record<string, string>;
40
+ };
41
+ export type McpSdkServerConfig = {
42
+ type: "sdk";
43
+ name: string;
44
+ };
45
+ export type McpSdkServerConfigWithInstance = McpSdkServerConfig & {
46
+ instance: McpServer;
47
+ };
48
+ export type McpServerConfig =
49
+ | McpStdioServerConfig
50
+ | McpSSEServerConfig
51
+ | McpHttpServerConfig
52
+ | McpSdkServerConfigWithInstance;
53
+ export type McpServerConfigForProcessTransport =
54
+ | McpStdioServerConfig
55
+ | McpSSEServerConfig
56
+ | McpHttpServerConfig
57
+ | McpSdkServerConfig;
58
+ type PermissionUpdateDestination =
59
+ | "userSettings"
60
+ | "projectSettings"
61
+ | "localSettings"
62
+ | "session";
63
+ export type PermissionBehavior = "allow" | "deny" | "ask";
64
+ export type PermissionUpdate =
65
+ | {
66
+ type: "addRules";
67
+ rules: PermissionRuleValue[];
68
+ behavior: PermissionBehavior;
69
+ destination: PermissionUpdateDestination;
70
+ }
71
+ | {
72
+ type: "replaceRules";
73
+ rules: PermissionRuleValue[];
74
+ behavior: PermissionBehavior;
75
+ destination: PermissionUpdateDestination;
76
+ }
77
+ | {
78
+ type: "removeRules";
79
+ rules: PermissionRuleValue[];
80
+ behavior: PermissionBehavior;
81
+ destination: PermissionUpdateDestination;
82
+ }
83
+ | {
84
+ type: "setMode";
85
+ mode: PermissionMode;
86
+ destination: PermissionUpdateDestination;
87
+ }
88
+ | {
89
+ type: "addDirectories";
90
+ directories: string[];
91
+ destination: PermissionUpdateDestination;
92
+ }
93
+ | {
94
+ type: "removeDirectories";
95
+ directories: string[];
96
+ destination: PermissionUpdateDestination;
97
+ };
98
+ export type PermissionResult =
99
+ | {
100
+ behavior: "allow";
101
+ /**
102
+ * Updated tool input to use, if any changes are needed.
103
+ *
104
+ * For example if the user was given the option to update the tool use
105
+ * input before approving, then this would be the updated input which
106
+ * would be executed by the tool.
107
+ */
108
+ updatedInput: Record<string, unknown>;
109
+ /**
110
+ * Permissions updates to be applied as part of accepting this tool use.
111
+ *
112
+ * Typically this is used as part of the 'always allow' flow and these
113
+ * permission updates are from the `suggestions` field from the
114
+ * CanUseTool callback.
115
+ *
116
+ * It is recommended that you use these suggestions rather than
117
+ * attempting to re-derive them from the tool use input, as the
118
+ * suggestions may include other permission changes such as adding
119
+ * directories or incorporate complex tool-use logic such as bash
120
+ * commands.
121
+ */
122
+ updatedPermissions?: PermissionUpdate[];
123
+ }
124
+ | {
125
+ behavior: "deny";
126
+ /**
127
+ * Message indicating the reason for denial, or guidance of what the
128
+ * model should do instead.
129
+ */
130
+ message: string;
131
+ /**
132
+ * If true, interrupt execution and do not continue.
133
+ *
134
+ * Typically this should be set to true when the user says 'no' with no
135
+ * further guidance. Leave unset or false if the user provides guidance
136
+ * which the model should incorporate and continue.
137
+ */
138
+ interrupt?: boolean;
139
+ };
140
+ export type PermissionRuleValue = {
141
+ toolName: string;
142
+ ruleContent?: string;
143
+ };
144
+ export type CanUseTool = (
145
+ toolName: string,
146
+ input: Record<string, unknown>,
147
+ options: {
148
+ /** Signaled if the operation should be aborted. */
149
+ signal: AbortSignal;
150
+ /**
151
+ * Suggestions for updating permissions so that the user will not be
152
+ * prompted again for this tool during this session.
153
+ *
154
+ * Typically if presenting the user an option 'always allow' or similar,
155
+ * then this full set of suggestions should be returned as the
156
+ * `updatedPermissions` in the PermissionResult.
157
+ */
158
+ suggestions?: PermissionUpdate[];
159
+ /**
160
+ * Unique identifier for this specific tool call within the assistant message.
161
+ * Multiple tool calls in the same assistant message will have different toolUseIDs.
162
+ */
163
+ toolUseID: string;
164
+ }
165
+ ) => Promise<PermissionResult>;
166
+ export declare const HOOK_EVENTS: readonly [
167
+ "PreToolUse",
168
+ "PostToolUse",
169
+ "Notification",
170
+ "UserPromptSubmit",
171
+ "SessionStart",
172
+ "SessionEnd",
173
+ "Stop",
174
+ "SubagentStop",
175
+ "PreCompact"
176
+ ];
177
+ export type HookEvent = (typeof HOOK_EVENTS)[number];
178
+ export type HookCallback = (
179
+ input: HookInput,
180
+ toolUseID: string | undefined,
181
+ options: {
182
+ signal: AbortSignal;
183
+ }
184
+ ) => Promise<HookJSONOutput>;
185
+ export interface HookCallbackMatcher {
186
+ matcher?: string;
187
+ hooks: HookCallback[];
188
+ }
189
+ export type BaseHookInput = {
190
+ session_id: string;
191
+ transcript_path: string;
192
+ cwd: string;
193
+ permission_mode?: string;
194
+ };
195
+ export type PreToolUseHookInput = BaseHookInput & {
196
+ hook_event_name: "PreToolUse";
197
+ tool_name: string;
198
+ tool_input: unknown;
199
+ };
200
+ export type PostToolUseHookInput = BaseHookInput & {
201
+ hook_event_name: "PostToolUse";
202
+ tool_name: string;
203
+ tool_input: unknown;
204
+ tool_response: unknown;
205
+ };
206
+ export type NotificationHookInput = BaseHookInput & {
207
+ hook_event_name: "Notification";
208
+ message: string;
209
+ title?: string;
210
+ };
211
+ export type UserPromptSubmitHookInput = BaseHookInput & {
212
+ hook_event_name: "UserPromptSubmit";
213
+ prompt: string;
214
+ };
215
+ export type SessionStartHookInput = BaseHookInput & {
216
+ hook_event_name: "SessionStart";
217
+ source: "startup" | "resume" | "clear" | "compact";
218
+ };
219
+ export type StopHookInput = BaseHookInput & {
220
+ hook_event_name: "Stop";
221
+ stop_hook_active: boolean;
222
+ };
223
+ export type SubagentStopHookInput = BaseHookInput & {
224
+ hook_event_name: "SubagentStop";
225
+ stop_hook_active: boolean;
226
+ };
227
+ export type PreCompactHookInput = BaseHookInput & {
228
+ hook_event_name: "PreCompact";
229
+ trigger: "manual" | "auto";
230
+ custom_instructions: string | null;
231
+ };
232
+ export declare const EXIT_REASONS: string[];
233
+ export type ExitReason = (typeof EXIT_REASONS)[number];
234
+ export type SessionEndHookInput = BaseHookInput & {
235
+ hook_event_name: "SessionEnd";
236
+ reason: ExitReason;
237
+ };
238
+ export type HookInput =
239
+ | PreToolUseHookInput
240
+ | PostToolUseHookInput
241
+ | NotificationHookInput
242
+ | UserPromptSubmitHookInput
243
+ | SessionStartHookInput
244
+ | SessionEndHookInput
245
+ | StopHookInput
246
+ | SubagentStopHookInput
247
+ | PreCompactHookInput;
248
+ export type AsyncHookJSONOutput = {
249
+ async: true;
250
+ asyncTimeout?: number;
251
+ };
252
+ export type SyncHookJSONOutput = {
253
+ continue?: boolean;
254
+ suppressOutput?: boolean;
255
+ stopReason?: string;
256
+ decision?: "approve" | "block";
257
+ systemMessage?: string;
258
+ reason?: string;
259
+ hookSpecificOutput?:
260
+ | {
261
+ hookEventName: "PreToolUse";
262
+ permissionDecision?: "allow" | "deny" | "ask";
263
+ permissionDecisionReason?: string;
264
+ updatedInput?: Record<string, unknown>;
265
+ }
266
+ | {
267
+ hookEventName: "UserPromptSubmit";
268
+ additionalContext?: string;
269
+ }
270
+ | {
271
+ hookEventName: "SessionStart";
272
+ additionalContext?: string;
273
+ }
274
+ | {
275
+ hookEventName: "PostToolUse";
276
+ additionalContext?: string;
277
+ };
278
+ };
279
+ export type HookJSONOutput = AsyncHookJSONOutput | SyncHookJSONOutput;
280
+ export type PermissionMode =
281
+ | "default"
282
+ | "acceptEdits"
283
+ | "bypassPermissions"
284
+ | "plan";
285
+ export type SlashCommand = {
286
+ name: string;
287
+ description: string;
288
+ argumentHint: string;
289
+ };
290
+ export type ModelInfo = {
291
+ value: string;
292
+ displayName: string;
293
+ description: string;
294
+ };
295
+ /** Information about the logged in user's account. */
296
+ export type AccountInfo = {
297
+ email?: string;
298
+ organization?: string;
299
+ subscriptionType?: string;
300
+ tokenSource?: string;
301
+ apiKeySource?: string;
302
+ };
303
+ export type McpServerStatus = {
304
+ name: string;
305
+ status: "connected" | "failed" | "needs-auth" | "pending";
306
+ serverInfo?: {
307
+ name: string;
308
+ version: string;
309
+ };
310
+ };
311
+ export type SDKMessageBase = {
312
+ uuid: UUID;
313
+ session_id: string;
314
+ };
315
+ type SDKUserMessageContent = {
316
+ type: "user";
317
+ message: APIUserMessage;
318
+ parent_tool_use_id: string | null;
319
+ /**
320
+ * True if this is a 'synthetic' user message which did not originate from
321
+ * the user directly, but instead was generated by the system.
322
+ */
323
+ isSynthetic?: boolean;
324
+ };
325
+ export type SDKUserMessage = SDKUserMessageContent & {
326
+ uuid?: UUID;
327
+ session_id: string;
328
+ };
329
+ export type SDKUserMessageReplay = SDKMessageBase &
330
+ SDKUserMessageContent & {
331
+ /**
332
+ * True if this is a replay/acknowledgment of a user message that was already
333
+ * added to the messages array. Used internally to prevent duplicate messages.
334
+ */
335
+ isReplay: true;
336
+ };
337
+ export type SDKAssistantMessage = SDKMessageBase & {
338
+ type: "assistant";
339
+ message: APIAssistantMessage;
340
+ parent_tool_use_id: string | null;
341
+ };
342
+ export type SDKPermissionDenial = {
343
+ tool_name: string;
344
+ tool_use_id: string;
345
+ tool_input: Record<string, unknown>;
346
+ };
347
+ export type SDKResultMessage =
348
+ | (SDKMessageBase & {
349
+ type: "result";
350
+ subtype: "success";
351
+ duration_ms: number;
352
+ duration_api_ms: number;
353
+ is_error: boolean;
354
+ num_turns: number;
355
+ result: string;
356
+ total_cost_usd: number;
357
+ usage: NonNullableUsage;
358
+ modelUsage: {
359
+ [modelName: string]: ModelUsage;
360
+ };
361
+ permission_denials: SDKPermissionDenial[];
362
+ })
363
+ | (SDKMessageBase & {
364
+ type: "result";
365
+ subtype: "error_max_turns" | "error_during_execution";
366
+ duration_ms: number;
367
+ duration_api_ms: number;
368
+ is_error: boolean;
369
+ num_turns: number;
370
+ total_cost_usd: number;
371
+ usage: NonNullableUsage;
372
+ modelUsage: {
373
+ [modelName: string]: ModelUsage;
374
+ };
375
+ permission_denials: SDKPermissionDenial[];
376
+ });
377
+ export type SDKSystemMessage = SDKMessageBase & {
378
+ type: "system";
379
+ subtype: "init";
380
+ agents?: string[];
381
+ apiKeySource: ApiKeySource;
382
+ claude_code_version: string;
383
+ cwd: string;
384
+ tools: string[];
385
+ mcp_servers: {
386
+ name: string;
387
+ status: string;
388
+ }[];
389
+ model: string;
390
+ permissionMode: PermissionMode;
391
+ slash_commands: string[];
392
+ output_style: string;
393
+ skills: string[];
394
+ plugins: {
395
+ name: string;
396
+ path: string;
397
+ }[];
398
+ };
399
+ export type SDKPartialAssistantMessage = SDKMessageBase & {
400
+ type: "stream_event";
401
+ event: RawMessageStreamEvent;
402
+ parent_tool_use_id: string | null;
403
+ };
404
+ export type SDKCompactBoundaryMessage = SDKMessageBase & {
405
+ type: "system";
406
+ subtype: "compact_boundary";
407
+ compact_metadata: {
408
+ trigger: "manual" | "auto";
409
+ pre_tokens: number;
410
+ };
411
+ };
412
+ export type SDKHookResponseMessage = SDKMessageBase & {
413
+ type: "system";
414
+ subtype: "hook_response";
415
+ hook_name: string;
416
+ hook_event: string;
417
+ stdout: string;
418
+ stderr: string;
419
+ exit_code?: number;
420
+ };
421
+ export type SDKMessage =
422
+ | SDKAssistantMessage
423
+ | SDKUserMessage
424
+ | SDKUserMessageReplay
425
+ | SDKResultMessage
426
+ | SDKSystemMessage
427
+ | SDKPartialAssistantMessage
428
+ | SDKCompactBoundaryMessage
429
+ | SDKHookResponseMessage;
430
+ export interface Query extends AsyncGenerator<SDKMessage, void> {
431
+ /**
432
+ * Control Requests
433
+ * The following methods are control requests, and are only supported when
434
+ * streaming input/output is used.
435
+ */
436
+ interrupt(): Promise<void>;
437
+ setPermissionMode(mode: PermissionMode): Promise<void>;
438
+ setModel(model?: string): Promise<void>;
439
+ /**
440
+ * Set the maximum number of thinking tokens the model is allowed to use
441
+ * when generating its response. This can be used to limit the amount of
442
+ * tokens the model uses for its response, which can help control cost and
443
+ * latency.
444
+ *
445
+ * Use `null` to clear any previously set limit and allow the model to
446
+ * use the default maximum thinking tokens.
447
+ */
448
+ setMaxThinkingTokens(maxThinkingTokens: number | null): Promise<void>;
449
+ supportedCommands(): Promise<SlashCommand[]>;
450
+ supportedModels(): Promise<ModelInfo[]>;
451
+ mcpServerStatus(): Promise<McpServerStatus[]>;
452
+ accountInfo(): Promise<AccountInfo>;
453
+ }
454
+ type SdkMcpToolDefinition<Schema extends ZodRawShape = ZodRawShape> = {
455
+ name: string;
456
+ description: string;
457
+ inputSchema: Schema;
458
+ handler: (
459
+ args: z.infer<ZodObject<Schema>>,
460
+ extra: unknown
461
+ ) => Promise<CallToolResult>;
462
+ };
463
+ export declare function tool<Schema extends ZodRawShape>(
464
+ _name: string,
465
+ _description: string,
466
+ _inputSchema: Schema,
467
+ _handler: (
468
+ args: z.infer<ZodObject<Schema>>,
469
+ extra: unknown
470
+ ) => Promise<CallToolResult>
471
+ ): SdkMcpToolDefinition<Schema>;
472
+ type CreateSdkMcpServerOptions = {
473
+ name: string;
474
+ version?: string;
475
+ tools?: Array<SdkMcpToolDefinition<any>>;
476
+ };
477
+ /**
478
+ * Creates an MCP server instance that can be used with the SDK transport.
479
+ * This allows SDK users to define custom tools that run in the same process.
480
+ *
481
+ * If your SDK MCP calls will run longer than 60s, override CLAUDE_CODE_STREAM_CLOSE_TIMEOUT
482
+ */
483
+ export declare function createSdkMcpServer(
484
+ _options: CreateSdkMcpServerOptions
485
+ ): McpSdkServerConfigWithInstance;
486
+ export declare class AbortError extends Error {}
487
+ export type AgentDefinition = {
488
+ description: string;
489
+ tools?: string[];
490
+ prompt: string;
491
+ model?: "sonnet" | "opus" | "haiku" | "inherit";
492
+ };
493
+ export type SettingSource = "user" | "project" | "local";
494
+ export type SdkPluginConfig = {
495
+ type: "local";
496
+ path: string;
497
+ };
498
+ export type Options = {
499
+ abortController?: AbortController;
500
+ additionalDirectories?: string[];
501
+ agents?: Record<string, AgentDefinition>;
502
+ allowedTools?: string[];
503
+ canUseTool?: CanUseTool;
504
+ continue?: boolean;
505
+ cwd?: string;
506
+ disallowedTools?: string[];
507
+ env?: {
508
+ [envVar: string]: string | undefined;
509
+ };
510
+ executable?: "bun" | "deno" | "node";
511
+ executableArgs?: string[];
512
+ extraArgs?: Record<string, string | null>;
513
+ fallbackModel?: string;
514
+ /**
515
+ * When true resumed sessions will fork to a new session ID rather than
516
+ * continuing the previous session. Use with --resume.
517
+ */
518
+ forkSession?: boolean;
519
+ hooks?: Partial<Record<HookEvent, HookCallbackMatcher[]>>;
520
+ includePartialMessages?: boolean;
521
+ maxThinkingTokens?: number;
522
+ maxTurns?: number;
523
+ mcpServers?: Record<string, McpServerConfig>;
524
+ model?: string;
525
+ pathToClaudeCodeExecutable?: string;
526
+ permissionMode?: PermissionMode;
527
+ allowDangerouslySkipPermissions?: boolean;
528
+ permissionPromptToolName?: string;
529
+ /**
530
+ * Load plugins for this session. Plugins provide custom commands, agents,
531
+ * skills, and hooks that extend Claude Code's capabilities.
532
+ *
533
+ * Currently only local plugins are supported via the 'local' type.
534
+ *
535
+ * @example
536
+ * ```typescript
537
+ * plugins: [
538
+ * { type: 'local', path: './my-plugin' },
539
+ * { type: 'local', path: '/absolute/path/to/plugin' }
540
+ * ]
541
+ * ```
542
+ */
543
+ plugins?: SdkPluginConfig[];
544
+ resume?: string;
545
+ /**
546
+ * When resuming, only resume messages up to and including the assistant
547
+ * message with this message.id. Use with --resume.
548
+ * This allows you to resume from a specific point in the conversation.
549
+ * The message ID is expected to be from SDKAssistantMessage.message.id.
550
+ */
551
+ resumeSessionAt?: string;
552
+ settingSources?: SettingSource[];
553
+ stderr?: (data: string) => void;
554
+ strictMcpConfig?: boolean;
555
+ systemPrompt?:
556
+ | string
557
+ | {
558
+ type: "preset";
559
+ preset: "claude_code";
560
+ append?: string;
561
+ };
562
+ };
563
+ export declare function query(_params: {
564
+ prompt: string | AsyncIterable<SDKUserMessage>;
565
+ options?: Options;
566
+ }): Query;
567
+ export { };
568
+