@providerprotocol/ai 0.0.19 → 0.0.21

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 (49) hide show
  1. package/README.md +82 -9
  2. package/dist/anthropic/index.d.ts +184 -14
  3. package/dist/anthropic/index.js +214 -86
  4. package/dist/anthropic/index.js.map +1 -1
  5. package/dist/{chunk-5FEAOEXV.js → chunk-EDENPF3E.js} +57 -103
  6. package/dist/chunk-EDENPF3E.js.map +1 -0
  7. package/dist/{chunk-UMKWXGO3.js → chunk-M4BMM5IB.js} +86 -2
  8. package/dist/chunk-M4BMM5IB.js.map +1 -0
  9. package/dist/chunk-Y3GBJNA2.js +120 -0
  10. package/dist/chunk-Y3GBJNA2.js.map +1 -0
  11. package/dist/{chunk-U4JJC2YX.js → chunk-Z4ILICF5.js} +2 -2
  12. package/dist/chunk-Z4ILICF5.js.map +1 -0
  13. package/dist/google/index.d.ts +16 -19
  14. package/dist/google/index.js +18 -40
  15. package/dist/google/index.js.map +1 -1
  16. package/dist/http/index.d.ts +2 -2
  17. package/dist/http/index.js +5 -4
  18. package/dist/index.d.ts +101 -38
  19. package/dist/index.js +69 -43
  20. package/dist/index.js.map +1 -1
  21. package/dist/ollama/index.d.ts +14 -16
  22. package/dist/ollama/index.js +9 -11
  23. package/dist/ollama/index.js.map +1 -1
  24. package/dist/openai/index.d.ts +25 -133
  25. package/dist/openai/index.js +31 -85
  26. package/dist/openai/index.js.map +1 -1
  27. package/dist/openrouter/index.d.ts +28 -53
  28. package/dist/openrouter/index.js +24 -47
  29. package/dist/openrouter/index.js.map +1 -1
  30. package/dist/provider-DGQHYE6I.d.ts +1319 -0
  31. package/dist/proxy/index.d.ts +194 -12
  32. package/dist/proxy/index.js +37 -65
  33. package/dist/proxy/index.js.map +1 -1
  34. package/dist/{retry-DR7YRJDz.d.ts → retry-Pcs3hnbu.d.ts} +2 -2
  35. package/dist/{stream-DRHy6q1a.d.ts → stream-Di9acos2.d.ts} +1 -1
  36. package/dist/xai/index.d.ts +16 -88
  37. package/dist/xai/index.js +34 -62
  38. package/dist/xai/index.js.map +1 -1
  39. package/package.json +4 -1
  40. package/dist/chunk-5FEAOEXV.js.map +0 -1
  41. package/dist/chunk-DZQHVGNV.js +0 -71
  42. package/dist/chunk-DZQHVGNV.js.map +0 -1
  43. package/dist/chunk-MSR5P65T.js +0 -39
  44. package/dist/chunk-MSR5P65T.js.map +0 -1
  45. package/dist/chunk-U4JJC2YX.js.map +0 -1
  46. package/dist/chunk-UMKWXGO3.js.map +0 -1
  47. package/dist/content-DEl3z_W2.d.ts +0 -276
  48. package/dist/image-Dhq-Yuq4.d.ts +0 -456
  49. package/dist/provider-BBMBZuGn.d.ts +0 -570
@@ -1,29 +1,151 @@
1
- import {
2
- createProvider
3
- } from "../chunk-MSR5P65T.js";
4
1
  import {
5
2
  AssistantMessage,
3
+ createProvider,
6
4
  isAssistantMessage,
7
5
  isToolResultMessage,
8
6
  isUserMessage
9
- } from "../chunk-UMKWXGO3.js";
7
+ } from "../chunk-M4BMM5IB.js";
10
8
  import {
11
9
  parseSSEStream
12
10
  } from "../chunk-Z7RBRCRN.js";
13
11
  import {
14
- doFetch,
15
- doStreamFetch,
16
- normalizeHttpError,
17
12
  resolveApiKey
18
- } from "../chunk-5FEAOEXV.js";
13
+ } from "../chunk-Y3GBJNA2.js";
19
14
  import {
20
- UPPError
21
- } from "../chunk-DZQHVGNV.js";
15
+ UPPError,
16
+ doFetch,
17
+ doStreamFetch,
18
+ normalizeHttpError
19
+ } from "../chunk-EDENPF3E.js";
20
+
21
+ // src/providers/anthropic/types.ts
22
+ var betas = {
23
+ // Structured Outputs
24
+ /** Guaranteed JSON schema conformance for responses. Available for Claude Sonnet 4.5+. */
25
+ structuredOutputs: "structured-outputs-2025-11-13",
26
+ // Extended Thinking / Reasoning
27
+ /** Enables Claude to think between tool calls in Claude 4 models. */
28
+ interleavedThinking: "interleaved-thinking-2025-05-14",
29
+ /** Developer mode for full thinking output visibility. */
30
+ devFullThinking: "dev-full-thinking-2025-05-14",
31
+ /** Effort parameter for Claude Opus 4.5 - controls response thoroughness vs efficiency. */
32
+ effort: "effort-2025-11-24",
33
+ // Computer Use
34
+ /** Legacy computer use tool (Claude 3.x models). */
35
+ computerUseLegacy: "computer-use-2024-10-22",
36
+ /** Computer use tool for Claude 4 models (mouse, keyboard, screenshots). */
37
+ computerUse: "computer-use-2025-01-24",
38
+ /** Computer use tool for Claude Opus 4.5 with additional commands. */
39
+ computerUseOpus: "computer-use-2025-11-24",
40
+ // Extended Output / Context
41
+ /** Enables up to 8,192 output tokens from Claude Sonnet 3.5. */
42
+ maxTokens35Sonnet: "max-tokens-3-5-sonnet-2024-07-15",
43
+ /** Enables 128K token output length. */
44
+ output128k: "output-128k-2025-02-19",
45
+ /** Enables 1 million token context window for Claude Sonnet 4. */
46
+ context1m: "context-1m-2025-08-07",
47
+ // Token Efficiency
48
+ /** Reduces output token consumption by up to 70% for tool calls. */
49
+ tokenEfficientTools: "token-efficient-tools-2025-02-19",
50
+ /** Streams tool use parameters without buffering/JSON validation. */
51
+ fineGrainedToolStreaming: "fine-grained-tool-streaming-2025-05-14",
52
+ // Code Execution
53
+ /** Code execution tool for running Python/Bash in secure sandbox. */
54
+ codeExecution: "code-execution-2025-08-25",
55
+ // Advanced Tool Use
56
+ /** Advanced tool use: Tool Search, Programmatic Tool Calling, Tool Use Examples. */
57
+ advancedToolUse: "advanced-tool-use-2025-11-20",
58
+ // Files & Documents
59
+ /** Files API for uploading and managing files. */
60
+ filesApi: "files-api-2025-04-14",
61
+ /** PDF document support. */
62
+ pdfs: "pdfs-2024-09-25",
63
+ // MCP (Model Context Protocol)
64
+ /** MCP connector to connect to remote MCP servers. */
65
+ mcpClient: "mcp-client-2025-04-04",
66
+ /** Updated MCP client. */
67
+ mcpClientLatest: "mcp-client-2025-11-20",
68
+ // Caching
69
+ /** Prompt caching for reduced latency and costs. Now works automatically with cache_control. */
70
+ promptCaching: "prompt-caching-2024-07-31",
71
+ /** Enables 1-hour cache TTL (vs default 5-minute). */
72
+ extendedCacheTtl: "extended-cache-ttl-2025-04-11",
73
+ // Context Management
74
+ /** Automatic tool call clearing for context management. */
75
+ contextManagement: "context-management-2025-06-27",
76
+ /** Handling for when model context window is exceeded. */
77
+ modelContextWindowExceeded: "model-context-window-exceeded-2025-08-26",
78
+ // Message Batches (generally available but may still need header)
79
+ /** Message Batches API for async processing at 50% cost. */
80
+ messageBatches: "message-batches-2024-09-24",
81
+ // Token Counting (generally available)
82
+ /** Token counting endpoint. */
83
+ tokenCounting: "token-counting-2024-11-01",
84
+ // Skills
85
+ /** Agent Skills for specialized tasks (PowerPoint, Excel, Word, PDF). */
86
+ skills: "skills-2025-10-02"
87
+ };
88
+ function webSearchTool(options) {
89
+ return {
90
+ type: "web_search_20250305",
91
+ name: "web_search",
92
+ ...options
93
+ };
94
+ }
95
+ function computerTool(options) {
96
+ const { version = "20250124", ...rest } = options;
97
+ return {
98
+ type: version === "20251124" ? "computer_20251124" : "computer_20250124",
99
+ name: "computer",
100
+ ...rest
101
+ };
102
+ }
103
+ function textEditorTool(options) {
104
+ const version = options?.version ?? "20250728";
105
+ return {
106
+ type: version === "20250728" ? "text_editor_20250728" : "text_editor_20250124",
107
+ name: version === "20250728" ? "str_replace_based_edit_tool" : "str_replace_editor",
108
+ ...options?.max_characters !== void 0 && { max_characters: options.max_characters }
109
+ };
110
+ }
111
+ function bashTool() {
112
+ return {
113
+ type: "bash_20250124",
114
+ name: "bash"
115
+ };
116
+ }
117
+ function codeExecutionTool() {
118
+ return {
119
+ type: "code_execution_20250825",
120
+ name: "code_execution"
121
+ };
122
+ }
123
+ function toolSearchTool(options) {
124
+ const mode = options?.mode ?? "regex";
125
+ return {
126
+ type: mode === "regex" ? "tool_search_tool_regex_20251119" : "tool_search_tool_bm25_20251119",
127
+ name: mode === "regex" ? "tool_search_tool_regex" : "tool_search_tool_bm25"
128
+ };
129
+ }
130
+ var tools = {
131
+ /** Creates a web search tool configuration */
132
+ webSearch: webSearchTool,
133
+ /** Creates a computer use tool configuration */
134
+ computer: computerTool,
135
+ /** Creates a text editor tool configuration */
136
+ textEditor: textEditorTool,
137
+ /** Creates a bash tool configuration */
138
+ bash: bashTool,
139
+ /** Creates a code execution tool configuration */
140
+ codeExecution: codeExecutionTool,
141
+ /** Creates a tool search tool configuration */
142
+ toolSearch: toolSearchTool
143
+ };
22
144
 
23
145
  // src/providers/anthropic/transform.ts
24
- function transformRequest(request, modelId) {
146
+ function transformRequest(request, modelId, useNativeStructuredOutput = false) {
25
147
  const params = request.params ?? {};
26
- const { builtInTools, ...restParams } = params;
148
+ const { tools: builtInTools, ...restParams } = params;
27
149
  const anthropicRequest = {
28
150
  ...restParams,
29
151
  model: modelId,
@@ -44,17 +166,30 @@ function transformRequest(request, modelId) {
44
166
  anthropicRequest.tool_choice = { type: "auto" };
45
167
  }
46
168
  if (request.structure) {
47
- const structuredTool = {
48
- name: "json_response",
49
- description: "Return the response in the specified JSON format. You MUST use this tool to provide your response.",
50
- input_schema: {
51
- type: "object",
52
- properties: request.structure.properties,
53
- required: request.structure.required
54
- }
55
- };
56
- anthropicRequest.tools = [...anthropicRequest.tools ?? [], structuredTool];
57
- anthropicRequest.tool_choice = { type: "tool", name: "json_response" };
169
+ if (useNativeStructuredOutput) {
170
+ const outputFormat = {
171
+ type: "json_schema",
172
+ schema: {
173
+ type: "object",
174
+ properties: request.structure.properties,
175
+ required: request.structure.required,
176
+ additionalProperties: false
177
+ }
178
+ };
179
+ anthropicRequest.output_format = outputFormat;
180
+ } else {
181
+ const structuredTool = {
182
+ name: "json_response",
183
+ description: "Return the response in the specified JSON format. You MUST use this tool to provide your response.",
184
+ input_schema: {
185
+ type: "object",
186
+ properties: request.structure.properties,
187
+ required: request.structure.required
188
+ }
189
+ };
190
+ anthropicRequest.tools = [...anthropicRequest.tools ?? [], structuredTool];
191
+ anthropicRequest.tool_choice = { type: "tool", name: "json_response" };
192
+ }
58
193
  }
59
194
  return anthropicRequest;
60
195
  }
@@ -182,13 +317,19 @@ function transformTool(tool) {
182
317
  ...cacheControl ? { cache_control: cacheControl } : {}
183
318
  };
184
319
  }
185
- function transformResponse(data) {
320
+ function transformResponse(data, useNativeStructuredOutput = false) {
186
321
  const textContent = [];
187
322
  const toolCalls = [];
188
323
  let structuredData;
189
324
  for (const block of data.content) {
190
325
  if (block.type === "text") {
191
326
  textContent.push({ type: "text", text: block.text });
327
+ if (useNativeStructuredOutput && structuredData === void 0) {
328
+ try {
329
+ structuredData = JSON.parse(block.text);
330
+ } catch {
331
+ }
332
+ }
192
333
  } else if (block.type === "tool_use") {
193
334
  if (block.name === "json_response") {
194
335
  structuredData = block.input;
@@ -346,7 +487,7 @@ function transformStreamEvent(event, state) {
346
487
  return null;
347
488
  }
348
489
  }
349
- function buildResponseFromState(state) {
490
+ function buildResponseFromState(state, useNativeStructuredOutput = false) {
350
491
  const textContent = [];
351
492
  const toolCalls = [];
352
493
  let structuredData;
@@ -354,6 +495,12 @@ function buildResponseFromState(state) {
354
495
  if (!block) continue;
355
496
  if (block.type === "text" && block.text) {
356
497
  textContent.push({ type: "text", text: block.text });
498
+ if (useNativeStructuredOutput && structuredData === void 0) {
499
+ try {
500
+ structuredData = JSON.parse(block.text);
501
+ } catch {
502
+ }
503
+ }
357
504
  } else if (block.type === "tool_use" && block.id && block.name) {
358
505
  let args = {};
359
506
  if (block.input) {
@@ -411,6 +558,16 @@ ${block.fileContent}\`\`\`
411
558
  }
412
559
 
413
560
  // src/providers/anthropic/llm.ts
561
+ function shouldUseNativeStructuredOutput(config, structure) {
562
+ if (!structure) {
563
+ return false;
564
+ }
565
+ const betaHeader = config.headers?.["anthropic-beta"];
566
+ if (!betaHeader) {
567
+ return false;
568
+ }
569
+ return betaHeader.includes(betas.structuredOutputs);
570
+ }
414
571
  var ANTHROPIC_API_URL = "https://api.anthropic.com/v1/messages";
415
572
  var ANTHROPIC_VERSION = "2023-06-01";
416
573
  var ANTHROPIC_CAPABILITIES = {
@@ -449,8 +606,12 @@ function createLLMHandler() {
449
606
  "anthropic",
450
607
  "llm"
451
608
  );
609
+ const useNativeStructuredOutput = shouldUseNativeStructuredOutput(
610
+ request.config,
611
+ request.structure
612
+ );
452
613
  const baseUrl = request.config.baseUrl ?? ANTHROPIC_API_URL;
453
- const body = transformRequest(request, modelId);
614
+ const body = transformRequest(request, modelId, useNativeStructuredOutput);
454
615
  const headers = {
455
616
  "Content-Type": "application/json",
456
617
  "x-api-key": apiKey,
@@ -476,10 +637,14 @@ function createLLMHandler() {
476
637
  "llm"
477
638
  );
478
639
  const data = await response.json();
479
- return transformResponse(data);
640
+ return transformResponse(data, useNativeStructuredOutput);
480
641
  },
481
642
  stream(request) {
482
643
  const state = createStreamState();
644
+ const useNativeStructuredOutput = shouldUseNativeStructuredOutput(
645
+ request.config,
646
+ request.structure
647
+ );
483
648
  let responseResolve;
484
649
  let responseReject;
485
650
  const responsePromise = new Promise((resolve, reject) => {
@@ -495,7 +660,7 @@ function createLLMHandler() {
495
660
  "llm"
496
661
  );
497
662
  const baseUrl = request.config.baseUrl ?? ANTHROPIC_API_URL;
498
- const body = transformRequest(request, modelId);
663
+ const body = transformRequest(request, modelId, useNativeStructuredOutput);
499
664
  body.stream = true;
500
665
  const headers = {
501
666
  "Content-Type": "application/json",
@@ -555,7 +720,7 @@ function createLLMHandler() {
555
720
  }
556
721
  }
557
722
  }
558
- responseResolve(buildResponseFromState(state));
723
+ responseResolve(buildResponseFromState(state, useNativeStructuredOutput));
559
724
  } catch (error) {
560
725
  responseReject(error);
561
726
  throw error;
@@ -574,74 +739,37 @@ function createLLMHandler() {
574
739
  };
575
740
  }
576
741
 
577
- // src/providers/anthropic/types.ts
578
- function webSearchTool(options) {
579
- return {
580
- type: "web_search_20250305",
581
- name: "web_search",
582
- ...options
583
- };
584
- }
585
- function computerTool(options) {
586
- const { version = "20250124", ...rest } = options;
587
- return {
588
- type: version === "20251124" ? "computer_20251124" : "computer_20250124",
589
- name: "computer",
590
- ...rest
591
- };
592
- }
593
- function textEditorTool(options) {
594
- const version = options?.version ?? "20250728";
595
- return {
596
- type: version === "20250728" ? "text_editor_20250728" : "text_editor_20250124",
597
- name: version === "20250728" ? "str_replace_based_edit_tool" : "str_replace_editor",
598
- ...options?.max_characters !== void 0 && { max_characters: options.max_characters }
599
- };
600
- }
601
- function bashTool() {
602
- return {
603
- type: "bash_20250124",
604
- name: "bash"
605
- };
606
- }
607
- function codeExecutionTool() {
608
- return {
609
- type: "code_execution_20250825",
610
- name: "code_execution"
611
- };
612
- }
613
- function toolSearchTool(options) {
614
- const mode = options?.mode ?? "regex";
742
+ // src/providers/anthropic/index.ts
743
+ function createProviderConfig(options) {
744
+ if (!options?.betas || options.betas.length === 0) {
745
+ return void 0;
746
+ }
747
+ const betaHeader = options.betas.join(",");
615
748
  return {
616
- type: mode === "regex" ? "tool_search_tool_regex_20251119" : "tool_search_tool_bm25_20251119",
617
- name: mode === "regex" ? "tool_search_tool_regex" : "tool_search_tool_bm25"
749
+ headers: {
750
+ "anthropic-beta": betaHeader
751
+ }
618
752
  };
619
753
  }
620
- var tools = {
621
- /** Creates a web search tool configuration */
622
- webSearch: webSearchTool,
623
- /** Creates a computer use tool configuration */
624
- computer: computerTool,
625
- /** Creates a text editor tool configuration */
626
- textEditor: textEditorTool,
627
- /** Creates a bash tool configuration */
628
- bash: bashTool,
629
- /** Creates a code execution tool configuration */
630
- codeExecution: codeExecutionTool,
631
- /** Creates a tool search tool configuration */
632
- toolSearch: toolSearchTool
633
- };
634
-
635
- // src/providers/anthropic/index.ts
636
754
  var anthropic = createProvider({
637
755
  name: "anthropic",
638
756
  version: "1.0.0",
639
- modalities: {
757
+ handlers: {
640
758
  llm: createLLMHandler()
759
+ },
760
+ createModelReference: (modelId, options, provider) => {
761
+ const providerConfig = createProviderConfig(options);
762
+ return {
763
+ modelId,
764
+ provider,
765
+ options,
766
+ ...providerConfig && { providerConfig }
767
+ };
641
768
  }
642
769
  });
643
770
  export {
644
771
  anthropic,
772
+ betas,
645
773
  tools
646
774
  };
647
775
  //# sourceMappingURL=index.js.map