ai 3.0.18 → 3.0.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,24 @@
1
- type JsonSchema = Record<string, unknown>;
1
+ import { JSONSchema7 } from 'json-schema';
2
+
3
+ /**
4
+ A tool has a name, a description, and a set of parameters.
5
+
6
+ Note: this is **not** the user-facing tool definition. The AI SDK methods will
7
+ map the user-facing tool definitions to this format.
8
+ */
9
+ type LanguageModelV1FunctionTool = {
10
+ /**
11
+ The type of the tool. Only functions for now, but this gives us room to
12
+ add more specific tool types in the future and use a discriminated union.
13
+ */
14
+ type: 'function';
15
+ /**
16
+ The name of the tool. Unique within this model call.
17
+ */
18
+ name: string;
19
+ description?: string;
20
+ parameters: JSONSchema7;
21
+ };
2
22
 
3
23
  type LanguageModelV1CallSettings = {
4
24
  /**
@@ -54,26 +74,6 @@ type LanguageModelV1CallSettings = {
54
74
  abortSignal?: AbortSignal;
55
75
  };
56
76
 
57
- /**
58
- * A tool has a name, a description, and a set of parameters.
59
- *
60
- * Note: this is **not** the user-facing tool definition. The AI SDK methods will
61
- * map the user-facing tool definitions to this format.
62
- */
63
- type LanguageModelV1FunctionTool = {
64
- /**
65
- * The type of the tool. Only functions for now, but this gives us room to
66
- * add more specific tool types in the future and use a discriminated union.
67
- */
68
- type: 'function';
69
- /**
70
- * The name of the tool. Unique within this model call.
71
- */
72
- name: string;
73
- description?: string;
74
- parameters: JsonSchema;
75
- };
76
-
77
77
  /**
78
78
  * A prompt is a list of messages.
79
79
  *
@@ -151,7 +151,7 @@ type LanguageModelV1CallOptions = LanguageModelV1CallSettings & {
151
151
  type: 'object-json';
152
152
  } | {
153
153
  type: 'object-grammar';
154
- schema: JsonSchema;
154
+ schema: JSONSchema7;
155
155
  } | {
156
156
  type: 'object-tool';
157
157
  tool: LanguageModelV1FunctionTool;
@@ -333,11 +333,10 @@ type AnthropicMessagesConfig = {
333
333
  provider: string;
334
334
  baseUrl: string;
335
335
  headers: () => Record<string, string | undefined>;
336
- generateId: () => string;
337
336
  };
338
337
  declare class AnthropicMessagesLanguageModel implements LanguageModelV1 {
339
338
  readonly specificationVersion = "v1";
340
- readonly defaultObjectGenerationMode = "json";
339
+ readonly defaultObjectGenerationMode = "tool";
341
340
  readonly modelId: AnthropicMessagesModelId;
342
341
  readonly settings: AnthropicMessagesSettings;
343
342
  private readonly config;
@@ -354,7 +353,6 @@ declare class AnthropicMessagesLanguageModel implements LanguageModelV1 {
354
353
  declare class Anthropic {
355
354
  readonly baseUrl?: string;
356
355
  readonly apiKey?: string;
357
- private readonly generateId;
358
356
  constructor(options?: {
359
357
  baseUrl?: string;
360
358
  apiKey?: string;
@@ -1,4 +1,24 @@
1
- type JsonSchema = Record<string, unknown>;
1
+ import { JSONSchema7 } from 'json-schema';
2
+
3
+ /**
4
+ A tool has a name, a description, and a set of parameters.
5
+
6
+ Note: this is **not** the user-facing tool definition. The AI SDK methods will
7
+ map the user-facing tool definitions to this format.
8
+ */
9
+ type LanguageModelV1FunctionTool = {
10
+ /**
11
+ The type of the tool. Only functions for now, but this gives us room to
12
+ add more specific tool types in the future and use a discriminated union.
13
+ */
14
+ type: 'function';
15
+ /**
16
+ The name of the tool. Unique within this model call.
17
+ */
18
+ name: string;
19
+ description?: string;
20
+ parameters: JSONSchema7;
21
+ };
2
22
 
3
23
  type LanguageModelV1CallSettings = {
4
24
  /**
@@ -54,26 +74,6 @@ type LanguageModelV1CallSettings = {
54
74
  abortSignal?: AbortSignal;
55
75
  };
56
76
 
57
- /**
58
- * A tool has a name, a description, and a set of parameters.
59
- *
60
- * Note: this is **not** the user-facing tool definition. The AI SDK methods will
61
- * map the user-facing tool definitions to this format.
62
- */
63
- type LanguageModelV1FunctionTool = {
64
- /**
65
- * The type of the tool. Only functions for now, but this gives us room to
66
- * add more specific tool types in the future and use a discriminated union.
67
- */
68
- type: 'function';
69
- /**
70
- * The name of the tool. Unique within this model call.
71
- */
72
- name: string;
73
- description?: string;
74
- parameters: JsonSchema;
75
- };
76
-
77
77
  /**
78
78
  * A prompt is a list of messages.
79
79
  *
@@ -151,7 +151,7 @@ type LanguageModelV1CallOptions = LanguageModelV1CallSettings & {
151
151
  type: 'object-json';
152
152
  } | {
153
153
  type: 'object-grammar';
154
- schema: JsonSchema;
154
+ schema: JSONSchema7;
155
155
  } | {
156
156
  type: 'object-tool';
157
157
  tool: LanguageModelV1FunctionTool;
@@ -333,11 +333,10 @@ type AnthropicMessagesConfig = {
333
333
  provider: string;
334
334
  baseUrl: string;
335
335
  headers: () => Record<string, string | undefined>;
336
- generateId: () => string;
337
336
  };
338
337
  declare class AnthropicMessagesLanguageModel implements LanguageModelV1 {
339
338
  readonly specificationVersion = "v1";
340
- readonly defaultObjectGenerationMode = "json";
339
+ readonly defaultObjectGenerationMode = "tool";
341
340
  readonly modelId: AnthropicMessagesModelId;
342
341
  readonly settings: AnthropicMessagesSettings;
343
342
  private readonly config;
@@ -354,7 +353,6 @@ declare class AnthropicMessagesLanguageModel implements LanguageModelV1 {
354
353
  declare class Anthropic {
355
354
  readonly baseUrl?: string;
356
355
  readonly apiKey?: string;
357
- private readonly generateId;
358
356
  constructor(options?: {
359
357
  baseUrl?: string;
360
358
  apiKey?: string;
@@ -79,13 +79,6 @@ var APICallError = class extends Error {
79
79
  }
80
80
  };
81
81
 
82
- // spec/util/generate-id.ts
83
- var import_non_secure = require("nanoid/non-secure");
84
- var generateId = (0, import_non_secure.customAlphabet)(
85
- "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
86
- 7
87
- );
88
-
89
82
  // spec/util/get-error-message.ts
90
83
  function getErrorMessage(error) {
91
84
  if (error == null) {
@@ -541,19 +534,6 @@ var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
541
534
  errorToMessage: (data) => data.error.message
542
535
  });
543
536
 
544
- // anthropic/map-anthropic-finish-reason.ts
545
- function mapAnthropicFinishReason(finishReason) {
546
- switch (finishReason) {
547
- case "end_turn":
548
- case "stop_sequence":
549
- return "stop";
550
- case "max_tokens":
551
- return "length";
552
- default:
553
- return "other";
554
- }
555
- }
556
-
557
537
  // anthropic/convert-to-anthropic-messages-prompt.ts
558
538
  function convertToAnthropicMessagesPrompt({
559
539
  prompt,
@@ -599,36 +579,36 @@ function convertToAnthropicMessagesPrompt({
599
579
  break;
600
580
  }
601
581
  case "assistant": {
602
- let text = "";
603
- for (const part of content) {
604
- switch (part.type) {
605
- case "text": {
606
- text += part.text;
607
- break;
608
- }
609
- case "tool-call": {
610
- throw new UnsupportedFunctionalityError({
611
- provider,
612
- functionality: "tool-call-part"
613
- });
614
- }
615
- default: {
616
- const _exhaustiveCheck = part;
617
- throw new Error(`Unsupported part: ${_exhaustiveCheck}`);
618
- }
619
- }
620
- }
621
582
  messages.push({
622
583
  role: "assistant",
623
- content: text
584
+ content: content.map((part) => {
585
+ switch (part.type) {
586
+ case "text": {
587
+ return { type: "text", text: part.text };
588
+ }
589
+ case "tool-call": {
590
+ return {
591
+ type: "tool_use",
592
+ id: part.toolCallId,
593
+ name: part.toolName,
594
+ input: part.args
595
+ };
596
+ }
597
+ }
598
+ })
624
599
  });
625
600
  break;
626
601
  }
627
602
  case "tool": {
628
- throw new UnsupportedFunctionalityError({
629
- provider,
630
- functionality: "tool-role"
603
+ messages.push({
604
+ role: "user",
605
+ content: content.map((part) => ({
606
+ type: "tool_result",
607
+ tool_use_id: part.toolCallId,
608
+ content: JSON.stringify(part.result)
609
+ }))
631
610
  });
611
+ break;
632
612
  }
633
613
  default: {
634
614
  const _exhaustiveCheck = role;
@@ -642,11 +622,26 @@ function convertToAnthropicMessagesPrompt({
642
622
  };
643
623
  }
644
624
 
625
+ // anthropic/map-anthropic-stop-reason.ts
626
+ function mapAnthropicStopReason(finishReason) {
627
+ switch (finishReason) {
628
+ case "end_turn":
629
+ case "stop_sequence":
630
+ return "stop";
631
+ case "tool_use":
632
+ return "tool-calls";
633
+ case "max_tokens":
634
+ return "length";
635
+ default:
636
+ return "other";
637
+ }
638
+ }
639
+
645
640
  // anthropic/anthropic-messages-language-model.ts
646
641
  var AnthropicMessagesLanguageModel = class {
647
642
  constructor(modelId, settings, config) {
648
643
  this.specificationVersion = "v1";
649
- this.defaultObjectGenerationMode = "json";
644
+ this.defaultObjectGenerationMode = "tool";
650
645
  this.modelId = modelId;
651
646
  this.settings = settings;
652
647
  this.config = config;
@@ -711,39 +706,38 @@ var AnthropicMessagesLanguageModel = class {
711
706
  args: {
712
707
  ...baseArgs,
713
708
  tools: tools == null ? void 0 : tools.map((tool) => ({
714
- type: "function",
715
- function: {
716
- name: tool.name,
717
- description: tool.description,
718
- parameters: tool.parameters
719
- }
709
+ name: tool.name,
710
+ description: tool.description,
711
+ input_schema: tool.parameters
720
712
  }))
721
713
  },
722
714
  warnings
723
715
  };
724
716
  }
725
717
  case "object-json": {
726
- return {
727
- args: {
728
- ...baseArgs,
729
- response_format: { type: "json_object" }
730
- },
731
- warnings
732
- };
718
+ throw new UnsupportedFunctionalityError({
719
+ functionality: "json-mode object generation",
720
+ provider: this.provider
721
+ });
733
722
  }
734
723
  case "object-tool": {
735
724
  return {
736
725
  args: {
737
726
  ...baseArgs,
738
- tool_choice: "any",
739
- tools: [{ type: "function", function: mode.tool }]
727
+ tools: [
728
+ {
729
+ name: mode.tool.name,
730
+ description: mode.tool.description,
731
+ input_schema: mode.tool.parameters
732
+ }
733
+ ]
740
734
  },
741
735
  warnings
742
736
  };
743
737
  }
744
738
  case "object-grammar": {
745
739
  throw new UnsupportedFunctionalityError({
746
- functionality: "object-grammar mode",
740
+ functionality: "grammar-mode object generation",
747
741
  provider: this.provider
748
742
  });
749
743
  }
@@ -766,9 +760,30 @@ var AnthropicMessagesLanguageModel = class {
766
760
  abortSignal: options.abortSignal
767
761
  });
768
762
  const { messages: rawPrompt, ...rawSettings } = args;
763
+ let text = "";
764
+ for (const content of response.content) {
765
+ if (content.type === "text") {
766
+ text += content.text;
767
+ }
768
+ }
769
+ let toolCalls = void 0;
770
+ if (response.content.some((content) => content.type === "tool_use")) {
771
+ toolCalls = [];
772
+ for (const content of response.content) {
773
+ if (content.type === "tool_use") {
774
+ toolCalls.push({
775
+ toolCallType: "function",
776
+ toolCallId: content.id,
777
+ toolName: content.name,
778
+ args: JSON.stringify(content.input)
779
+ });
780
+ }
781
+ }
782
+ }
769
783
  return {
770
- text: response.content.map(({ text }) => text).join(""),
771
- finishReason: mapAnthropicFinishReason(response.stop_reason),
784
+ text,
785
+ toolCalls,
786
+ finishReason: mapAnthropicStopReason(response.stop_reason),
772
787
  usage: {
773
788
  promptTokens: response.usage.input_tokens,
774
789
  completionTokens: response.usage.output_tokens
@@ -798,7 +813,6 @@ var AnthropicMessagesLanguageModel = class {
798
813
  promptTokens: Number.NaN,
799
814
  completionTokens: Number.NaN
800
815
  };
801
- const generateId2 = this.config.generateId;
802
816
  return {
803
817
  stream: response.pipeThrough(
804
818
  new TransformStream({
@@ -828,9 +842,7 @@ var AnthropicMessagesLanguageModel = class {
828
842
  }
829
843
  case "message_delta": {
830
844
  usage.completionTokens = value.usage.output_tokens;
831
- finishReason = mapAnthropicFinishReason(
832
- value.delta.stop_reason
833
- );
845
+ finishReason = mapAnthropicStopReason(value.delta.stop_reason);
834
846
  return;
835
847
  }
836
848
  case "message_stop": {
@@ -853,10 +865,18 @@ var AnthropicMessagesLanguageModel = class {
853
865
  var anthropicMessagesResponseSchema = import_zod2.z.object({
854
866
  type: import_zod2.z.literal("message"),
855
867
  content: import_zod2.z.array(
856
- import_zod2.z.object({
857
- type: import_zod2.z.literal("text"),
858
- text: import_zod2.z.string()
859
- })
868
+ import_zod2.z.discriminatedUnion("type", [
869
+ import_zod2.z.object({
870
+ type: import_zod2.z.literal("text"),
871
+ text: import_zod2.z.string()
872
+ }),
873
+ import_zod2.z.object({
874
+ type: import_zod2.z.literal("tool_use"),
875
+ id: import_zod2.z.string(),
876
+ name: import_zod2.z.string(),
877
+ input: import_zod2.z.unknown()
878
+ })
879
+ ])
860
880
  ),
861
881
  stop_reason: import_zod2.z.string().optional().nullable(),
862
882
  usage: import_zod2.z.object({
@@ -910,10 +930,8 @@ var anthropicMessagesChunkSchema = import_zod2.z.discriminatedUnion("type", [
910
930
  // anthropic/anthropic-facade.ts
911
931
  var Anthropic = class {
912
932
  constructor(options = {}) {
913
- var _a;
914
933
  this.baseUrl = options.baseUrl;
915
934
  this.apiKey = options.apiKey;
916
- this.generateId = (_a = options.generateId) != null ? _a : generateId;
917
935
  }
918
936
  get baseConfig() {
919
937
  var _a;
@@ -921,6 +939,7 @@ var Anthropic = class {
921
939
  baseUrl: (_a = this.baseUrl) != null ? _a : "https://api.anthropic.com/v1",
922
940
  headers: () => ({
923
941
  "anthropic-version": "2023-06-01",
942
+ "anthropic-beta": "tools-2024-04-04",
924
943
  "x-api-key": loadApiKey({
925
944
  apiKey: this.apiKey,
926
945
  environmentVariableName: "ANTHROPIC_API_KEY",
@@ -932,8 +951,7 @@ var Anthropic = class {
932
951
  messages(modelId, settings = {}) {
933
952
  return new AnthropicMessagesLanguageModel(modelId, settings, {
934
953
  provider: "anthropic.messages",
935
- ...this.baseConfig,
936
- generateId: this.generateId
954
+ ...this.baseConfig
937
955
  });
938
956
  }
939
957
  };