@providerprotocol/ai 0.0.14 → 0.0.15

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.
@@ -50,7 +50,432 @@ interface AnthropicLLMParams {
50
50
  * - `standard_only`: Only use standard capacity, never priority
51
51
  */
52
52
  service_tier?: 'auto' | 'standard_only';
53
+ /**
54
+ * Built-in tools for server-side execution.
55
+ *
56
+ * Use the tool helper constructors from the `tools` namespace:
57
+ * - `tools.webSearch()` - Web search capability
58
+ * - `tools.computer()` - Computer use (mouse, keyboard, screenshots)
59
+ * - `tools.textEditor()` - File viewing and editing
60
+ * - `tools.bash()` - Shell command execution
61
+ * - `tools.codeExecution()` - Sandboxed Python/Bash execution
62
+ * - `tools.toolSearch()` - Dynamic tool catalog search
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * import { anthropic, tools } from 'provider-protocol/anthropic';
67
+ *
68
+ * const model = llm({
69
+ * model: anthropic('claude-sonnet-4-20250514'),
70
+ * params: {
71
+ * builtInTools: [
72
+ * tools.webSearch({ max_uses: 5 }),
73
+ * tools.codeExecution(),
74
+ * ],
75
+ * },
76
+ * });
77
+ * ```
78
+ */
79
+ builtInTools?: AnthropicBuiltInTool[];
80
+ /**
81
+ * Container ID for code execution tool reuse.
82
+ * Pass the container ID from a previous response to reuse the same environment.
83
+ */
84
+ container?: string;
85
+ }
86
+ /**
87
+ * Anthropic-specific HTTP headers for API requests.
88
+ *
89
+ * @example
90
+ * ```typescript
91
+ * const headers: AnthropicHeaders = {
92
+ * 'anthropic-beta': 'extended-cache-ttl-2025-04-11',
93
+ * };
94
+ * ```
95
+ */
96
+ interface AnthropicHeaders {
97
+ /**
98
+ * Beta features header.
99
+ *
100
+ * Comma-separated list of beta feature flags:
101
+ * - `extended-cache-ttl-2025-04-11` - Enable 1-hour cache TTL
102
+ * - `token-efficient-tools-2025-02-19` - Token-efficient tool encoding
103
+ * - `computer-use-2025-01-24` - Computer use tool (Claude 4 models)
104
+ * - `computer-use-2025-11-24` - Computer use tool (Claude Opus 4.5)
105
+ * - `code-execution-2025-08-25` - Code execution tool
106
+ * - `advanced-tool-use-2025-11-20` - Tool search tool
107
+ */
108
+ 'anthropic-beta'?: string;
109
+ [key: string]: string | undefined;
110
+ }
111
+ /**
112
+ * User location for web search context.
113
+ *
114
+ * Used to localize web search results based on the user's approximate location.
115
+ */
116
+ interface AnthropicUserLocation {
117
+ /** Location type - must be 'approximate' */
118
+ type: 'approximate';
119
+ /** City name */
120
+ city?: string;
121
+ /** Region/state name */
122
+ region?: string;
123
+ /** ISO 3166-1 alpha-2 country code (e.g., "US") */
124
+ country?: string;
125
+ /** IANA timezone (e.g., "America/New_York") */
126
+ timezone?: string;
127
+ }
128
+ /**
129
+ * Web search tool for real-time web information retrieval.
130
+ *
131
+ * Enables Claude to search the web for up-to-date information.
132
+ * No beta header required - this is a GA feature.
133
+ *
134
+ * @example
135
+ * ```typescript
136
+ * const tool: AnthropicWebSearchTool = {
137
+ * type: 'web_search_20250305',
138
+ * name: 'web_search',
139
+ * max_uses: 5,
140
+ * allowed_domains: ['wikipedia.org', 'github.com'],
141
+ * };
142
+ * ```
143
+ */
144
+ interface AnthropicWebSearchTool {
145
+ /** Tool type identifier */
146
+ type: 'web_search_20250305';
147
+ /** Tool name - must be 'web_search' */
148
+ name: 'web_search';
149
+ /** Maximum searches per request (default: unlimited) */
150
+ max_uses?: number;
151
+ /** Whitelist domains (mutually exclusive with blocked_domains) */
152
+ allowed_domains?: string[];
153
+ /** Blacklist domains (mutually exclusive with allowed_domains) */
154
+ blocked_domains?: string[];
155
+ /** User location for localized results */
156
+ user_location?: AnthropicUserLocation;
157
+ }
158
+ /**
159
+ * Computer use tool for desktop automation.
160
+ *
161
+ * Enables Claude to interact with computer interfaces through
162
+ * mouse clicks, keyboard input, and screenshots.
163
+ *
164
+ * Requires beta header:
165
+ * - `computer-use-2025-11-24` for Claude Opus 4.5
166
+ * - `computer-use-2025-01-24` for other Claude 4 models
167
+ *
168
+ * @example
169
+ * ```typescript
170
+ * const tool: AnthropicComputerTool = {
171
+ * type: 'computer_20250124',
172
+ * name: 'computer',
173
+ * display_width_px: 1920,
174
+ * display_height_px: 1080,
175
+ * };
176
+ * ```
177
+ */
178
+ interface AnthropicComputerTool {
179
+ /** Tool type identifier (version-specific) */
180
+ type: 'computer_20251124' | 'computer_20250124';
181
+ /** Tool name - must be 'computer' */
182
+ name: 'computer';
183
+ /** Display width in pixels */
184
+ display_width_px: number;
185
+ /** Display height in pixels */
186
+ display_height_px: number;
187
+ /** X11 display number (optional) */
188
+ display_number?: number;
189
+ /** Enable zoom action (Opus 4.5 only with 20251124 version) */
190
+ enable_zoom?: boolean;
191
+ }
192
+ /**
193
+ * Text editor tool for file viewing and editing.
194
+ *
195
+ * Enables Claude to view, create, and edit files with
196
+ * commands like view, str_replace, create, and insert.
197
+ *
198
+ * No beta header required.
199
+ *
200
+ * @example
201
+ * ```typescript
202
+ * const tool: AnthropicTextEditorTool = {
203
+ * type: 'text_editor_20250728',
204
+ * name: 'str_replace_based_edit_tool',
205
+ * max_characters: 10000,
206
+ * };
207
+ * ```
208
+ */
209
+ interface AnthropicTextEditorTool {
210
+ /** Tool type identifier (version-specific) */
211
+ type: 'text_editor_20250728' | 'text_editor_20250124';
212
+ /** Tool name (version-specific) */
213
+ name: 'str_replace_based_edit_tool' | 'str_replace_editor';
214
+ /** Max characters for view truncation (20250728+ only) */
215
+ max_characters?: number;
216
+ }
217
+ /**
218
+ * Bash tool for shell command execution.
219
+ *
220
+ * Enables Claude to execute bash commands in a shell session.
221
+ * The session persists within the conversation.
222
+ *
223
+ * No beta header required.
224
+ *
225
+ * @example
226
+ * ```typescript
227
+ * const tool: AnthropicBashTool = {
228
+ * type: 'bash_20250124',
229
+ * name: 'bash',
230
+ * };
231
+ * ```
232
+ */
233
+ interface AnthropicBashTool {
234
+ /** Tool type identifier */
235
+ type: 'bash_20250124';
236
+ /** Tool name - must be 'bash' */
237
+ name: 'bash';
53
238
  }
239
+ /**
240
+ * Code execution tool for sandboxed Python/Bash execution.
241
+ *
242
+ * Enables Claude to write and execute code in a secure container
243
+ * with pre-installed data science libraries.
244
+ *
245
+ * Requires beta header: `code-execution-2025-08-25`
246
+ *
247
+ * @example
248
+ * ```typescript
249
+ * const tool: AnthropicCodeExecutionTool = {
250
+ * type: 'code_execution_20250825',
251
+ * name: 'code_execution',
252
+ * };
253
+ * ```
254
+ */
255
+ interface AnthropicCodeExecutionTool {
256
+ /** Tool type identifier */
257
+ type: 'code_execution_20250825';
258
+ /** Tool name - must be 'code_execution' */
259
+ name: 'code_execution';
260
+ }
261
+ /**
262
+ * Tool search tool for dynamic tool discovery.
263
+ *
264
+ * Enables Claude to search through large tool catalogs
265
+ * using regex or natural language (BM25) queries.
266
+ *
267
+ * Requires beta header: `advanced-tool-use-2025-11-20`
268
+ *
269
+ * @example
270
+ * ```typescript
271
+ * const tool: AnthropicToolSearchTool = {
272
+ * type: 'tool_search_tool_regex_20251119',
273
+ * name: 'tool_search_tool_regex',
274
+ * };
275
+ * ```
276
+ */
277
+ interface AnthropicToolSearchTool {
278
+ /** Tool type identifier (regex or BM25 variant) */
279
+ type: 'tool_search_tool_regex_20251119' | 'tool_search_tool_bm25_20251119';
280
+ /** Tool name (must match type variant) */
281
+ name: 'tool_search_tool_regex' | 'tool_search_tool_bm25';
282
+ }
283
+ /**
284
+ * Union type for all Anthropic built-in tools.
285
+ *
286
+ * Built-in tools run server-side and have special handling
287
+ * different from user-defined function tools.
288
+ */
289
+ type AnthropicBuiltInTool = AnthropicWebSearchTool | AnthropicComputerTool | AnthropicTextEditorTool | AnthropicBashTool | AnthropicCodeExecutionTool | AnthropicToolSearchTool;
290
+ /**
291
+ * Creates a web search tool configuration.
292
+ *
293
+ * The web search tool enables Claude to search the web for up-to-date information.
294
+ * Pricing: $10 per 1,000 searches plus standard token costs.
295
+ *
296
+ * @param options - Optional configuration for search behavior
297
+ * @returns A web search tool configuration object
298
+ *
299
+ * @example
300
+ * ```typescript
301
+ * // Basic web search
302
+ * const search = webSearchTool();
303
+ *
304
+ * // With configuration
305
+ * const searchWithOptions = webSearchTool({
306
+ * max_uses: 5,
307
+ * allowed_domains: ['wikipedia.org', 'github.com'],
308
+ * user_location: {
309
+ * type: 'approximate',
310
+ * city: 'San Francisco',
311
+ * country: 'US',
312
+ * },
313
+ * });
314
+ * ```
315
+ */
316
+ declare function webSearchTool(options?: {
317
+ max_uses?: number;
318
+ allowed_domains?: string[];
319
+ blocked_domains?: string[];
320
+ user_location?: AnthropicUserLocation;
321
+ }): AnthropicWebSearchTool;
322
+ /**
323
+ * Creates a computer use tool configuration.
324
+ *
325
+ * The computer tool enables Claude to interact with computer interfaces
326
+ * through mouse clicks, keyboard input, and screenshots.
327
+ *
328
+ * Requires beta header (automatically injected when using this tool):
329
+ * - `computer-use-2025-11-24` for Claude Opus 4.5
330
+ * - `computer-use-2025-01-24` for other models
331
+ *
332
+ * @param options - Display configuration and optional settings
333
+ * @returns A computer tool configuration object
334
+ *
335
+ * @example
336
+ * ```typescript
337
+ * const computer = computerTool({
338
+ * display_width_px: 1920,
339
+ * display_height_px: 1080,
340
+ * });
341
+ *
342
+ * // For Opus 4.5 with zoom support
343
+ * const computerOpus = computerTool({
344
+ * display_width_px: 1920,
345
+ * display_height_px: 1080,
346
+ * version: '20251124',
347
+ * enable_zoom: true,
348
+ * });
349
+ * ```
350
+ */
351
+ declare function computerTool(options: {
352
+ display_width_px: number;
353
+ display_height_px: number;
354
+ display_number?: number;
355
+ enable_zoom?: boolean;
356
+ /** Use '20251124' for Claude Opus 4.5, '20250124' for other models */
357
+ version?: '20251124' | '20250124';
358
+ }): AnthropicComputerTool;
359
+ /**
360
+ * Creates a text editor tool configuration.
361
+ *
362
+ * The text editor tool enables Claude to view, create, and edit files
363
+ * using commands like view, str_replace, create, and insert.
364
+ *
365
+ * Token overhead: ~700 tokens per tool definition.
366
+ *
367
+ * @param options - Optional configuration
368
+ * @returns A text editor tool configuration object
369
+ *
370
+ * @example
371
+ * ```typescript
372
+ * const editor = textEditorTool();
373
+ *
374
+ * // With max characters for view truncation
375
+ * const editorWithLimit = textEditorTool({
376
+ * max_characters: 10000,
377
+ * });
378
+ * ```
379
+ */
380
+ declare function textEditorTool(options?: {
381
+ max_characters?: number;
382
+ /** Use '20250728' for Claude 4, '20250124' for Claude 3.7 */
383
+ version?: '20250728' | '20250124';
384
+ }): AnthropicTextEditorTool;
385
+ /**
386
+ * Creates a bash tool configuration.
387
+ *
388
+ * The bash tool enables Claude to execute shell commands.
389
+ * Sessions persist within the conversation.
390
+ *
391
+ * Token overhead: ~245 tokens per tool definition.
392
+ *
393
+ * @returns A bash tool configuration object
394
+ *
395
+ * @example
396
+ * ```typescript
397
+ * const bash = bashTool();
398
+ * ```
399
+ */
400
+ declare function bashTool(): AnthropicBashTool;
401
+ /**
402
+ * Creates a code execution tool configuration.
403
+ *
404
+ * The code execution tool enables Claude to write and execute
405
+ * Python/Bash code in a secure sandboxed container.
406
+ *
407
+ * Requires beta header: `code-execution-2025-08-25` (automatically injected).
408
+ *
409
+ * Pricing:
410
+ * - Free tier: 1,550 hours/month per organization
411
+ * - Additional: $0.05 per hour, per container
412
+ *
413
+ * @returns A code execution tool configuration object
414
+ *
415
+ * @example
416
+ * ```typescript
417
+ * const codeExec = codeExecutionTool();
418
+ * ```
419
+ */
420
+ declare function codeExecutionTool(): AnthropicCodeExecutionTool;
421
+ /**
422
+ * Creates a tool search tool configuration.
423
+ *
424
+ * The tool search tool enables Claude to search through large
425
+ * tool catalogs (up to 10,000 tools) using regex or natural language.
426
+ *
427
+ * Requires beta header: `advanced-tool-use-2025-11-20` (automatically injected).
428
+ *
429
+ * @param options - Optional mode selection
430
+ * @returns A tool search tool configuration object
431
+ *
432
+ * @example
433
+ * ```typescript
434
+ * // Regex-based search (default)
435
+ * const search = toolSearchTool();
436
+ *
437
+ * // Natural language (BM25) search
438
+ * const nlSearch = toolSearchTool({ mode: 'bm25' });
439
+ * ```
440
+ */
441
+ declare function toolSearchTool(options?: {
442
+ /** Search mode: 'regex' for pattern matching, 'bm25' for natural language */
443
+ mode?: 'regex' | 'bm25';
444
+ }): AnthropicToolSearchTool;
445
+ /**
446
+ * Namespace object containing all Anthropic tool helper constructors.
447
+ *
448
+ * Provides a convenient way to create built-in tool configurations.
449
+ *
450
+ * @example
451
+ * ```typescript
452
+ * import { anthropic, tools } from 'provider-protocol/anthropic';
453
+ *
454
+ * const model = llm({
455
+ * model: anthropic('claude-sonnet-4-20250514'),
456
+ * params: {
457
+ * builtInTools: [
458
+ * tools.webSearch({ max_uses: 5 }),
459
+ * tools.codeExecution(),
460
+ * ],
461
+ * },
462
+ * });
463
+ * ```
464
+ */
465
+ declare const tools: {
466
+ /** Creates a web search tool configuration */
467
+ webSearch: typeof webSearchTool;
468
+ /** Creates a computer use tool configuration */
469
+ computer: typeof computerTool;
470
+ /** Creates a text editor tool configuration */
471
+ textEditor: typeof textEditorTool;
472
+ /** Creates a bash tool configuration */
473
+ bash: typeof bashTool;
474
+ /** Creates a code execution tool configuration */
475
+ codeExecution: typeof codeExecutionTool;
476
+ /** Creates a tool search tool configuration */
477
+ toolSearch: typeof toolSearchTool;
478
+ };
54
479
 
55
480
  /**
56
481
  * Anthropic provider instance for the Universal Provider Protocol.
@@ -74,4 +499,4 @@ interface AnthropicLLMParams {
74
499
  */
75
500
  declare const anthropic: Provider<unknown>;
76
501
 
77
- export { type AnthropicLLMParams, anthropic };
502
+ export { type AnthropicBashTool, type AnthropicBuiltInTool, type AnthropicCodeExecutionTool, type AnthropicComputerTool, type AnthropicHeaders, type AnthropicLLMParams, type AnthropicTextEditorTool, type AnthropicToolSearchTool, type AnthropicUserLocation, type AnthropicWebSearchTool, anthropic, tools };
@@ -21,16 +21,24 @@ import {
21
21
  // src/providers/anthropic/transform.ts
22
22
  function transformRequest(request, modelId) {
23
23
  const params = request.params ?? {};
24
+ const { builtInTools, ...restParams } = params;
24
25
  const anthropicRequest = {
25
- ...params,
26
+ ...restParams,
26
27
  model: modelId,
27
28
  messages: request.messages.map(transformMessage)
28
29
  };
29
30
  if (request.system) {
30
31
  anthropicRequest.system = request.system;
31
32
  }
33
+ const allTools = [];
32
34
  if (request.tools && request.tools.length > 0) {
33
- anthropicRequest.tools = request.tools.map(transformTool);
35
+ allTools.push(...request.tools.map(transformTool));
36
+ }
37
+ if (builtInTools && builtInTools.length > 0) {
38
+ allTools.push(...builtInTools);
39
+ }
40
+ if (allTools.length > 0) {
41
+ anthropicRequest.tools = allTools;
34
42
  anthropicRequest.tool_choice = { type: "auto" };
35
43
  }
36
44
  if (request.structure) {
@@ -188,6 +196,20 @@ function transformResponse(data) {
188
196
  toolName: block.name,
189
197
  arguments: block.input
190
198
  });
199
+ } else if (block.type === "bash_code_execution_tool_result") {
200
+ if (block.content.type === "bash_code_execution_result" && block.content.stdout) {
201
+ textContent.push({ type: "text", text: `
202
+ \`\`\`
203
+ ${block.content.stdout}\`\`\`
204
+ ` });
205
+ }
206
+ } else if (block.type === "text_editor_code_execution_tool_result") {
207
+ if (block.content.type === "text_editor_code_execution_result" && block.content.content) {
208
+ textContent.push({ type: "text", text: `
209
+ \`\`\`
210
+ ${block.content.content}\`\`\`
211
+ ` });
212
+ }
191
213
  }
192
214
  }
193
215
  const message = new AssistantMessage(
@@ -249,6 +271,27 @@ function transformStreamEvent(event, state) {
249
271
  name: event.content_block.name,
250
272
  input: ""
251
273
  };
274
+ } else if (event.content_block.type === "server_tool_use") {
275
+ state.content[event.index] = {
276
+ type: "server_tool_use",
277
+ id: event.content_block.id,
278
+ name: event.content_block.name,
279
+ input: ""
280
+ };
281
+ } else if (event.content_block.type === "bash_code_execution_tool_result") {
282
+ const resultBlock = event.content_block;
283
+ state.content[event.index] = {
284
+ type: "bash_code_execution_tool_result",
285
+ tool_use_id: resultBlock.tool_use_id,
286
+ stdout: resultBlock.content?.stdout ?? ""
287
+ };
288
+ } else if (event.content_block.type === "text_editor_code_execution_tool_result") {
289
+ const resultBlock = event.content_block;
290
+ state.content[event.index] = {
291
+ type: "text_editor_code_execution_tool_result",
292
+ tool_use_id: resultBlock.tool_use_id,
293
+ fileContent: resultBlock.content?.content ?? ""
294
+ };
252
295
  }
253
296
  return { type: "content_block_start", index: event.index, delta: {} };
254
297
  case "content_block_delta": {
@@ -306,6 +349,7 @@ function buildResponseFromState(state) {
306
349
  const toolCalls = [];
307
350
  let structuredData;
308
351
  for (const block of state.content) {
352
+ if (!block) continue;
309
353
  if (block.type === "text" && block.text) {
310
354
  textContent.push({ type: "text", text: block.text });
311
355
  } else if (block.type === "tool_use" && block.id && block.name) {
@@ -324,6 +368,16 @@ function buildResponseFromState(state) {
324
368
  toolName: block.name,
325
369
  arguments: args
326
370
  });
371
+ } else if (block.type === "bash_code_execution_tool_result" && block.stdout) {
372
+ textContent.push({ type: "text", text: `
373
+ \`\`\`
374
+ ${block.stdout}\`\`\`
375
+ ` });
376
+ } else if (block.type === "text_editor_code_execution_tool_result" && block.fileContent) {
377
+ textContent.push({ type: "text", text: `
378
+ \`\`\`
379
+ ${block.fileContent}\`\`\`
380
+ ` });
327
381
  }
328
382
  }
329
383
  const message = new AssistantMessage(
@@ -518,6 +572,64 @@ function createLLMHandler() {
518
572
  };
519
573
  }
520
574
 
575
+ // src/providers/anthropic/types.ts
576
+ function webSearchTool(options) {
577
+ return {
578
+ type: "web_search_20250305",
579
+ name: "web_search",
580
+ ...options
581
+ };
582
+ }
583
+ function computerTool(options) {
584
+ const { version = "20250124", ...rest } = options;
585
+ return {
586
+ type: version === "20251124" ? "computer_20251124" : "computer_20250124",
587
+ name: "computer",
588
+ ...rest
589
+ };
590
+ }
591
+ function textEditorTool(options) {
592
+ const version = options?.version ?? "20250728";
593
+ return {
594
+ type: version === "20250728" ? "text_editor_20250728" : "text_editor_20250124",
595
+ name: version === "20250728" ? "str_replace_based_edit_tool" : "str_replace_editor",
596
+ ...options?.max_characters !== void 0 && { max_characters: options.max_characters }
597
+ };
598
+ }
599
+ function bashTool() {
600
+ return {
601
+ type: "bash_20250124",
602
+ name: "bash"
603
+ };
604
+ }
605
+ function codeExecutionTool() {
606
+ return {
607
+ type: "code_execution_20250825",
608
+ name: "code_execution"
609
+ };
610
+ }
611
+ function toolSearchTool(options) {
612
+ const mode = options?.mode ?? "regex";
613
+ return {
614
+ type: mode === "regex" ? "tool_search_tool_regex_20251119" : "tool_search_tool_bm25_20251119",
615
+ name: mode === "regex" ? "tool_search_tool_regex" : "tool_search_tool_bm25"
616
+ };
617
+ }
618
+ var tools = {
619
+ /** Creates a web search tool configuration */
620
+ webSearch: webSearchTool,
621
+ /** Creates a computer use tool configuration */
622
+ computer: computerTool,
623
+ /** Creates a text editor tool configuration */
624
+ textEditor: textEditorTool,
625
+ /** Creates a bash tool configuration */
626
+ bash: bashTool,
627
+ /** Creates a code execution tool configuration */
628
+ codeExecution: codeExecutionTool,
629
+ /** Creates a tool search tool configuration */
630
+ toolSearch: toolSearchTool
631
+ };
632
+
521
633
  // src/providers/anthropic/index.ts
522
634
  var anthropic = createProvider({
523
635
  name: "anthropic",
@@ -527,6 +639,7 @@ var anthropic = createProvider({
527
639
  }
528
640
  });
529
641
  export {
530
- anthropic
642
+ anthropic,
643
+ tools
531
644
  };
532
645
  //# sourceMappingURL=index.js.map