@ai-sdk/moonshotai 2.0.43 → 2.0.45

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.
@@ -0,0 +1,98 @@
1
+ import {
2
+ UnsupportedFunctionalityError,
3
+ type LanguageModelV3CallOptions,
4
+ type SharedV3Warning,
5
+ } from '@ai-sdk/provider';
6
+
7
+ export function prepareTools({
8
+ tools,
9
+ toolChoice,
10
+ }: {
11
+ tools: LanguageModelV3CallOptions['tools'];
12
+ toolChoice?: LanguageModelV3CallOptions['toolChoice'];
13
+ }): {
14
+ tools:
15
+ | undefined
16
+ | Array<{
17
+ type: 'function';
18
+ function: {
19
+ name: string;
20
+ description: string | undefined;
21
+ parameters: unknown;
22
+ strict?: boolean;
23
+ };
24
+ }>;
25
+ toolChoice:
26
+ | { type: 'function'; function: { name: string } }
27
+ | 'auto'
28
+ | 'none'
29
+ | 'required'
30
+ | undefined;
31
+ toolWarnings: SharedV3Warning[];
32
+ } {
33
+ // when the tools array is empty, change it to undefined to prevent errors:
34
+ tools = tools?.length ? tools : undefined;
35
+
36
+ const toolWarnings: SharedV3Warning[] = [];
37
+
38
+ if (tools == null) {
39
+ return { tools: undefined, toolChoice: undefined, toolWarnings };
40
+ }
41
+
42
+ const moonshotTools: Array<{
43
+ type: 'function';
44
+ function: {
45
+ name: string;
46
+ description: string | undefined;
47
+ parameters: unknown;
48
+ strict?: boolean;
49
+ };
50
+ }> = [];
51
+
52
+ for (const tool of tools) {
53
+ if (tool.type === 'provider') {
54
+ toolWarnings.push({
55
+ type: 'unsupported',
56
+ feature: `provider-defined tool ${tool.id}`,
57
+ });
58
+ } else {
59
+ moonshotTools.push({
60
+ type: 'function',
61
+ function: {
62
+ name: tool.name,
63
+ description: tool.description,
64
+ parameters: tool.inputSchema,
65
+ ...(tool.strict != null ? { strict: tool.strict } : {}),
66
+ },
67
+ });
68
+ }
69
+ }
70
+
71
+ if (toolChoice == null) {
72
+ return { tools: moonshotTools, toolChoice: undefined, toolWarnings };
73
+ }
74
+
75
+ const type = toolChoice.type;
76
+
77
+ switch (type) {
78
+ case 'auto':
79
+ case 'none':
80
+ case 'required':
81
+ return { tools: moonshotTools, toolChoice: type, toolWarnings };
82
+ case 'tool':
83
+ return {
84
+ tools: moonshotTools,
85
+ toolChoice: {
86
+ type: 'function',
87
+ function: { name: toolChoice.toolName },
88
+ },
89
+ toolWarnings,
90
+ };
91
+ default: {
92
+ const _exhaustiveCheck: never = type;
93
+ throw new UnsupportedFunctionalityError({
94
+ functionality: `tool choice type: ${_exhaustiveCheck}`,
95
+ });
96
+ }
97
+ }
98
+ }
@@ -1,4 +1,3 @@
1
- import type { ProviderErrorStructure } from '@ai-sdk/openai-compatible';
2
1
  import {
3
2
  NoSuchModelError,
4
3
  type LanguageModelV3,
@@ -10,24 +9,11 @@ import {
10
9
  withUserAgentSuffix,
11
10
  type FetchFunction,
12
11
  } from '@ai-sdk/provider-utils';
13
- import { z } from 'zod/v4';
14
12
  import { MoonshotAIChatLanguageModel } from './moonshotai-chat-language-model';
15
13
  import type { MoonshotAIChatModelId } from './moonshotai-chat-options';
16
14
  import { VERSION } from './version';
17
15
 
18
- export type MoonshotAIErrorData = z.infer<typeof moonshotaiErrorSchema>;
19
-
20
- const moonshotaiErrorSchema = z.object({
21
- error: z.object({
22
- message: z.string(),
23
- type: z.string().nullish(),
24
- }),
25
- });
26
-
27
- const moonshotaiErrorStructure: ProviderErrorStructure<MoonshotAIErrorData> = {
28
- errorSchema: moonshotaiErrorSchema,
29
- errorToMessage: data => data.error.message,
30
- };
16
+ export type { MoonshotAIErrorData } from './moonshotai-chat-api-types';
31
17
 
32
18
  export interface MoonshotAIProviderSettings {
33
19
  /**
@@ -93,65 +79,14 @@ export function createMoonshotAI(
93
79
  `ai-sdk/moonshotai/${VERSION}`,
94
80
  );
95
81
 
96
- interface CommonModelConfig {
97
- provider: string;
98
- url: ({ path }: { path: string }) => string;
99
- headers: () => Record<string, string>;
100
- fetch?: FetchFunction;
101
- }
102
-
103
- const getCommonModelConfig = (modelType: string): CommonModelConfig => ({
104
- provider: `moonshotai.${modelType}`,
105
- url: ({ path }) => `${baseURL}${path}`,
106
- headers: getHeaders,
107
- fetch: options.fetch,
108
- });
109
-
110
82
  const createChatModel = (modelId: MoonshotAIChatModelId) => {
111
83
  return new MoonshotAIChatLanguageModel(modelId, {
112
- ...getCommonModelConfig('chat'),
84
+ provider: 'moonshotai.chat',
85
+ url: ({ path }) => `${baseURL}${path}`,
86
+ headers: getHeaders,
87
+ fetch: options.fetch,
113
88
  includeUsage: true,
114
- errorStructure: moonshotaiErrorStructure,
115
89
  supportsStructuredOutputs: getModelStructuredOutputSupport(modelId),
116
- transformRequestBody: (args: Record<string, any>) => {
117
- const thinking = args.thinking as
118
- | { type?: string; budgetTokens?: number }
119
- | undefined;
120
- const reasoningHistory = args.reasoningHistory as string | undefined;
121
-
122
- const { thinking: _, reasoningHistory: __, ...rest } = args;
123
-
124
- const schema = rest.response_format?.json_schema?.schema;
125
- if (schema != null) {
126
- // kimi-k2.5 produces nonsensical output when the top-level `$schema`
127
- // keyword injected by the AI SDK is present, even though it otherwise
128
- // supports structured outputs. Strip it from the schema sent to
129
- // Moonshot; the full original schema is still used for result validation.
130
- const { $schema: _$schema, ...schemaWithoutDollarSchema } = schema;
131
- rest.response_format = {
132
- ...rest.response_format,
133
- json_schema: {
134
- ...rest.response_format.json_schema,
135
- schema: schemaWithoutDollarSchema,
136
- },
137
- };
138
- }
139
-
140
- return {
141
- ...rest,
142
- ...(thinking && {
143
- thinking: {
144
- type: thinking.type,
145
- ...(thinking.budgetTokens !== undefined && {
146
- budget_tokens: thinking.budgetTokens,
147
- }),
148
- },
149
- }),
150
- ...(reasoningHistory && {
151
- reasoning_history: reasoningHistory,
152
- }),
153
- };
154
- },
155
90
  });
156
91
  };
157
92