@ai-sdk/alibaba 0.0.1
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.
- package/CHANGELOG.md +7 -0
- package/LICENSE +13 -0
- package/README.md +121 -0
- package/dist/index.d.mts +77 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +797 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +792 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +70 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,792 @@
|
|
|
1
|
+
// src/alibaba-provider.ts
|
|
2
|
+
import {
|
|
3
|
+
NoSuchModelError
|
|
4
|
+
} from "@ai-sdk/provider";
|
|
5
|
+
import {
|
|
6
|
+
createJsonErrorResponseHandler,
|
|
7
|
+
loadApiKey,
|
|
8
|
+
withoutTrailingSlash,
|
|
9
|
+
withUserAgentSuffix
|
|
10
|
+
} from "@ai-sdk/provider-utils";
|
|
11
|
+
import { z as z3 } from "zod/v4";
|
|
12
|
+
|
|
13
|
+
// src/alibaba-chat-language-model.ts
|
|
14
|
+
import {
|
|
15
|
+
getResponseMetadata,
|
|
16
|
+
mapOpenAICompatibleFinishReason
|
|
17
|
+
} from "@ai-sdk/openai-compatible/internal";
|
|
18
|
+
import {
|
|
19
|
+
InvalidResponseDataError
|
|
20
|
+
} from "@ai-sdk/provider";
|
|
21
|
+
import {
|
|
22
|
+
combineHeaders,
|
|
23
|
+
createEventSourceResponseHandler,
|
|
24
|
+
createJsonResponseHandler,
|
|
25
|
+
generateId,
|
|
26
|
+
isParsableJson,
|
|
27
|
+
parseProviderOptions,
|
|
28
|
+
postJsonToApi
|
|
29
|
+
} from "@ai-sdk/provider-utils";
|
|
30
|
+
import { z as z2 } from "zod/v4";
|
|
31
|
+
|
|
32
|
+
// src/alibaba-chat-options.ts
|
|
33
|
+
import { z } from "zod/v4";
|
|
34
|
+
var alibabaLanguageModelOptions = z.object({
|
|
35
|
+
/**
|
|
36
|
+
* Enable thinking/reasoning mode for supported models.
|
|
37
|
+
* When enabled, the model generates reasoning content before the response.
|
|
38
|
+
*
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
enableThinking: z.boolean().optional(),
|
|
42
|
+
/**
|
|
43
|
+
* Maximum number of reasoning tokens to generate.
|
|
44
|
+
*/
|
|
45
|
+
thinkingBudget: z.number().positive().optional(),
|
|
46
|
+
/**
|
|
47
|
+
* Whether to enable parallel function calling during tool use.
|
|
48
|
+
*
|
|
49
|
+
* @default true
|
|
50
|
+
*/
|
|
51
|
+
parallelToolCalls: z.boolean().optional()
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// src/convert-alibaba-usage.ts
|
|
55
|
+
function convertAlibabaUsage(usage) {
|
|
56
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
57
|
+
return {
|
|
58
|
+
inputTokens: (_a = usage == null ? void 0 : usage.prompt_tokens) != null ? _a : void 0,
|
|
59
|
+
outputTokens: (_b = usage == null ? void 0 : usage.completion_tokens) != null ? _b : void 0,
|
|
60
|
+
totalTokens: (_c = usage == null ? void 0 : usage.total_tokens) != null ? _c : void 0,
|
|
61
|
+
reasoningTokens: (_e = (_d = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _d.reasoning_tokens) != null ? _e : void 0,
|
|
62
|
+
cachedInputTokens: (_g = (_f = usage == null ? void 0 : usage.prompt_tokens_details) == null ? void 0 : _f.cached_tokens) != null ? _g : void 0
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// src/convert-to-alibaba-chat-messages.ts
|
|
67
|
+
import {
|
|
68
|
+
UnsupportedFunctionalityError
|
|
69
|
+
} from "@ai-sdk/provider";
|
|
70
|
+
import { convertToBase64 } from "@ai-sdk/provider-utils";
|
|
71
|
+
function formatImageUrl({
|
|
72
|
+
data,
|
|
73
|
+
mediaType
|
|
74
|
+
}) {
|
|
75
|
+
return data instanceof URL ? data.toString() : `data:${mediaType};base64,${convertToBase64(data)}`;
|
|
76
|
+
}
|
|
77
|
+
function convertToAlibabaChatMessages({
|
|
78
|
+
prompt,
|
|
79
|
+
cacheControlValidator
|
|
80
|
+
}) {
|
|
81
|
+
const messages = [];
|
|
82
|
+
for (const { role, content, ...message } of prompt) {
|
|
83
|
+
switch (role) {
|
|
84
|
+
case "system": {
|
|
85
|
+
const cacheControl = cacheControlValidator == null ? void 0 : cacheControlValidator.getCacheControl(
|
|
86
|
+
message.providerOptions
|
|
87
|
+
);
|
|
88
|
+
if (cacheControl) {
|
|
89
|
+
messages.push({
|
|
90
|
+
role: "system",
|
|
91
|
+
content: [
|
|
92
|
+
{
|
|
93
|
+
type: "text",
|
|
94
|
+
text: content,
|
|
95
|
+
cache_control: cacheControl
|
|
96
|
+
}
|
|
97
|
+
]
|
|
98
|
+
});
|
|
99
|
+
} else {
|
|
100
|
+
messages.push({ role: "system", content });
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
case "user": {
|
|
105
|
+
if (content.length === 1 && content[0].type === "text") {
|
|
106
|
+
messages.push({
|
|
107
|
+
role: "user",
|
|
108
|
+
content: content[0].text
|
|
109
|
+
});
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
messages.push({
|
|
113
|
+
role: "user",
|
|
114
|
+
content: content.map((part) => {
|
|
115
|
+
switch (part.type) {
|
|
116
|
+
case "text": {
|
|
117
|
+
return { type: "text", text: part.text };
|
|
118
|
+
}
|
|
119
|
+
case "file": {
|
|
120
|
+
if (part.mediaType.startsWith("image/")) {
|
|
121
|
+
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
122
|
+
return {
|
|
123
|
+
type: "image_url",
|
|
124
|
+
image_url: {
|
|
125
|
+
url: formatImageUrl({ data: part.data, mediaType })
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
} else {
|
|
129
|
+
throw new UnsupportedFunctionalityError({
|
|
130
|
+
functionality: "Only image file parts are supported"
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
})
|
|
136
|
+
});
|
|
137
|
+
break;
|
|
138
|
+
}
|
|
139
|
+
case "assistant": {
|
|
140
|
+
let text = "";
|
|
141
|
+
const toolCalls = [];
|
|
142
|
+
for (const part of content) {
|
|
143
|
+
switch (part.type) {
|
|
144
|
+
case "text": {
|
|
145
|
+
text += part.text;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
case "tool-call": {
|
|
149
|
+
toolCalls.push({
|
|
150
|
+
id: part.toolCallId,
|
|
151
|
+
type: "function",
|
|
152
|
+
function: {
|
|
153
|
+
name: part.toolName,
|
|
154
|
+
arguments: JSON.stringify(part.input)
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
break;
|
|
158
|
+
}
|
|
159
|
+
case "reasoning": {
|
|
160
|
+
text += part.text;
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
messages.push({
|
|
166
|
+
role: "assistant",
|
|
167
|
+
content: text || null,
|
|
168
|
+
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
169
|
+
});
|
|
170
|
+
break;
|
|
171
|
+
}
|
|
172
|
+
case "tool": {
|
|
173
|
+
for (const toolResponse of content) {
|
|
174
|
+
const output = toolResponse.output;
|
|
175
|
+
let contentValue;
|
|
176
|
+
switch (output.type) {
|
|
177
|
+
case "text":
|
|
178
|
+
case "error-text":
|
|
179
|
+
contentValue = output.value;
|
|
180
|
+
break;
|
|
181
|
+
case "content":
|
|
182
|
+
case "json":
|
|
183
|
+
case "error-json":
|
|
184
|
+
contentValue = JSON.stringify(output.value);
|
|
185
|
+
break;
|
|
186
|
+
}
|
|
187
|
+
messages.push({
|
|
188
|
+
role: "tool",
|
|
189
|
+
tool_call_id: toolResponse.toolCallId,
|
|
190
|
+
content: contentValue
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
default: {
|
|
196
|
+
const _exhaustiveCheck = role;
|
|
197
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return messages;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// src/get-cache-control.ts
|
|
205
|
+
var MAX_CACHE_BREAKPOINTS = 4;
|
|
206
|
+
function getCacheControl(providerMetadata) {
|
|
207
|
+
var _a;
|
|
208
|
+
const alibaba2 = providerMetadata == null ? void 0 : providerMetadata.alibaba;
|
|
209
|
+
const cacheControlValue = (_a = alibaba2 == null ? void 0 : alibaba2.cacheControl) != null ? _a : alibaba2 == null ? void 0 : alibaba2.cache_control;
|
|
210
|
+
return cacheControlValue;
|
|
211
|
+
}
|
|
212
|
+
var CacheControlValidator = class {
|
|
213
|
+
constructor() {
|
|
214
|
+
this.breakpointCount = 0;
|
|
215
|
+
this.warnings = [];
|
|
216
|
+
}
|
|
217
|
+
getCacheControl(providerMetadata) {
|
|
218
|
+
const cacheControlValue = getCacheControl(providerMetadata);
|
|
219
|
+
if (!cacheControlValue) {
|
|
220
|
+
return void 0;
|
|
221
|
+
}
|
|
222
|
+
this.breakpointCount++;
|
|
223
|
+
if (this.breakpointCount > MAX_CACHE_BREAKPOINTS) {
|
|
224
|
+
this.warnings.push({
|
|
225
|
+
type: "other",
|
|
226
|
+
message: `cacheControl breakpoint limit: Maximum ${MAX_CACHE_BREAKPOINTS} cache breakpoints exceeded (found ${this.breakpointCount}). This breakpoint will be ignored.`
|
|
227
|
+
});
|
|
228
|
+
return void 0;
|
|
229
|
+
}
|
|
230
|
+
return cacheControlValue;
|
|
231
|
+
}
|
|
232
|
+
getWarnings() {
|
|
233
|
+
return this.warnings;
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
// src/alibaba-prepare-tools.ts
|
|
238
|
+
import {
|
|
239
|
+
UnsupportedFunctionalityError as UnsupportedFunctionalityError2
|
|
240
|
+
} from "@ai-sdk/provider";
|
|
241
|
+
function prepareTools({
|
|
242
|
+
tools,
|
|
243
|
+
toolChoice
|
|
244
|
+
}) {
|
|
245
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
246
|
+
const toolWarnings = [];
|
|
247
|
+
if (tools == null) {
|
|
248
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings };
|
|
249
|
+
}
|
|
250
|
+
const openaiCompatTools = [];
|
|
251
|
+
for (const tool of tools) {
|
|
252
|
+
if (tool.type === "provider-defined") {
|
|
253
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
254
|
+
} else {
|
|
255
|
+
openaiCompatTools.push({
|
|
256
|
+
type: "function",
|
|
257
|
+
function: {
|
|
258
|
+
name: tool.name,
|
|
259
|
+
description: tool.description,
|
|
260
|
+
parameters: tool.inputSchema
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
if (toolChoice == null) {
|
|
266
|
+
return { tools: openaiCompatTools, toolChoice: void 0, toolWarnings };
|
|
267
|
+
}
|
|
268
|
+
const type = toolChoice.type;
|
|
269
|
+
switch (type) {
|
|
270
|
+
case "auto":
|
|
271
|
+
case "none":
|
|
272
|
+
case "required":
|
|
273
|
+
return { tools: openaiCompatTools, toolChoice: type, toolWarnings };
|
|
274
|
+
case "tool":
|
|
275
|
+
return {
|
|
276
|
+
tools: openaiCompatTools,
|
|
277
|
+
toolChoice: {
|
|
278
|
+
type: "function",
|
|
279
|
+
function: { name: toolChoice.toolName }
|
|
280
|
+
},
|
|
281
|
+
toolWarnings
|
|
282
|
+
};
|
|
283
|
+
default: {
|
|
284
|
+
const _exhaustiveCheck = type;
|
|
285
|
+
throw new UnsupportedFunctionalityError2({
|
|
286
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
287
|
+
});
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// src/alibaba-chat-language-model.ts
|
|
293
|
+
var AlibabaLanguageModel = class {
|
|
294
|
+
constructor(modelId, config) {
|
|
295
|
+
this.specificationVersion = "v2";
|
|
296
|
+
this.supportedUrls = {
|
|
297
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
298
|
+
};
|
|
299
|
+
this.modelId = modelId;
|
|
300
|
+
this.config = config;
|
|
301
|
+
}
|
|
302
|
+
get provider() {
|
|
303
|
+
return this.config.provider;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Builds request arguments for Alibaba API call.
|
|
307
|
+
* Converts AI SDK options to Alibaba API format.
|
|
308
|
+
*/
|
|
309
|
+
async getArgs({
|
|
310
|
+
prompt,
|
|
311
|
+
maxOutputTokens,
|
|
312
|
+
temperature,
|
|
313
|
+
topP,
|
|
314
|
+
topK,
|
|
315
|
+
frequencyPenalty,
|
|
316
|
+
presencePenalty,
|
|
317
|
+
stopSequences,
|
|
318
|
+
responseFormat,
|
|
319
|
+
seed,
|
|
320
|
+
providerOptions,
|
|
321
|
+
tools,
|
|
322
|
+
toolChoice
|
|
323
|
+
}) {
|
|
324
|
+
var _a;
|
|
325
|
+
const warnings = [];
|
|
326
|
+
const cacheControlValidator = new CacheControlValidator();
|
|
327
|
+
const alibabaOptions = await parseProviderOptions({
|
|
328
|
+
provider: "alibaba",
|
|
329
|
+
providerOptions,
|
|
330
|
+
schema: alibabaLanguageModelOptions
|
|
331
|
+
});
|
|
332
|
+
if (frequencyPenalty != null) {
|
|
333
|
+
warnings.push({
|
|
334
|
+
type: "unsupported-setting",
|
|
335
|
+
setting: "frequencyPenalty"
|
|
336
|
+
});
|
|
337
|
+
}
|
|
338
|
+
const baseArgs = {
|
|
339
|
+
model: this.modelId,
|
|
340
|
+
max_tokens: maxOutputTokens,
|
|
341
|
+
temperature,
|
|
342
|
+
top_p: topP,
|
|
343
|
+
top_k: topK,
|
|
344
|
+
presence_penalty: presencePenalty,
|
|
345
|
+
stop: stopSequences,
|
|
346
|
+
seed,
|
|
347
|
+
response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? responseFormat.schema != null ? {
|
|
348
|
+
type: "json_schema",
|
|
349
|
+
json_schema: {
|
|
350
|
+
schema: responseFormat.schema,
|
|
351
|
+
name: (_a = responseFormat.name) != null ? _a : "response",
|
|
352
|
+
description: responseFormat.description
|
|
353
|
+
}
|
|
354
|
+
} : { type: "json_object" } : void 0,
|
|
355
|
+
// Alibaba-specific options
|
|
356
|
+
...(alibabaOptions == null ? void 0 : alibabaOptions.enableThinking) != null ? { enable_thinking: alibabaOptions.enableThinking } : {},
|
|
357
|
+
...(alibabaOptions == null ? void 0 : alibabaOptions.thinkingBudget) != null ? { thinking_budget: alibabaOptions.thinkingBudget } : {},
|
|
358
|
+
// Convert messages with cache control support
|
|
359
|
+
messages: convertToAlibabaChatMessages({
|
|
360
|
+
prompt,
|
|
361
|
+
cacheControlValidator
|
|
362
|
+
})
|
|
363
|
+
};
|
|
364
|
+
const {
|
|
365
|
+
tools: alibabaTools,
|
|
366
|
+
toolChoice: alibabaToolChoice,
|
|
367
|
+
toolWarnings
|
|
368
|
+
} = prepareTools({ tools, toolChoice });
|
|
369
|
+
warnings.push(...cacheControlValidator.getWarnings());
|
|
370
|
+
return {
|
|
371
|
+
args: {
|
|
372
|
+
...baseArgs,
|
|
373
|
+
tools: alibabaTools,
|
|
374
|
+
tool_choice: alibabaToolChoice,
|
|
375
|
+
...alibabaTools != null && (alibabaOptions == null ? void 0 : alibabaOptions.parallelToolCalls) !== void 0 ? { parallel_tool_calls: alibabaOptions.parallelToolCalls } : {}
|
|
376
|
+
},
|
|
377
|
+
warnings: [...warnings, ...toolWarnings]
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
async doGenerate(options) {
|
|
381
|
+
var _a, _b, _c;
|
|
382
|
+
const { args, warnings } = await this.getArgs(options);
|
|
383
|
+
const {
|
|
384
|
+
responseHeaders,
|
|
385
|
+
value: response,
|
|
386
|
+
rawValue: rawResponse
|
|
387
|
+
} = await postJsonToApi({
|
|
388
|
+
url: `${this.config.baseURL}/chat/completions`,
|
|
389
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
390
|
+
body: args,
|
|
391
|
+
failedResponseHandler: alibabaFailedResponseHandler,
|
|
392
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
393
|
+
alibabaChatResponseSchema
|
|
394
|
+
),
|
|
395
|
+
abortSignal: options.abortSignal,
|
|
396
|
+
fetch: this.config.fetch
|
|
397
|
+
});
|
|
398
|
+
const choice = response.choices[0];
|
|
399
|
+
const content = [];
|
|
400
|
+
const text = choice.message.content;
|
|
401
|
+
if (text != null && text.length > 0) {
|
|
402
|
+
content.push({ type: "text", text });
|
|
403
|
+
}
|
|
404
|
+
const reasoning = choice.message.reasoning_content;
|
|
405
|
+
if (reasoning != null && reasoning.length > 0) {
|
|
406
|
+
content.push({
|
|
407
|
+
type: "reasoning",
|
|
408
|
+
text: reasoning
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
if (choice.message.tool_calls != null) {
|
|
412
|
+
for (const toolCall of choice.message.tool_calls) {
|
|
413
|
+
content.push({
|
|
414
|
+
type: "tool-call",
|
|
415
|
+
toolCallId: toolCall.id,
|
|
416
|
+
toolName: toolCall.function.name,
|
|
417
|
+
input: toolCall.function.arguments
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
return {
|
|
422
|
+
content,
|
|
423
|
+
finishReason: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
424
|
+
usage: convertAlibabaUsage(response.usage),
|
|
425
|
+
request: { body: JSON.stringify(args) },
|
|
426
|
+
response: {
|
|
427
|
+
...getResponseMetadata(response),
|
|
428
|
+
headers: responseHeaders,
|
|
429
|
+
body: rawResponse
|
|
430
|
+
},
|
|
431
|
+
warnings,
|
|
432
|
+
providerMetadata: {
|
|
433
|
+
alibaba: {
|
|
434
|
+
cacheCreationInputTokens: (_c = (_b = (_a = response.usage) == null ? void 0 : _a.prompt_tokens_details) == null ? void 0 : _b.cache_creation_input_tokens) != null ? _c : null
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
async doStream(options) {
|
|
440
|
+
const { args, warnings } = await this.getArgs(options);
|
|
441
|
+
const body = {
|
|
442
|
+
...args,
|
|
443
|
+
stream: true,
|
|
444
|
+
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
445
|
+
};
|
|
446
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
447
|
+
url: `${this.config.baseURL}/chat/completions`,
|
|
448
|
+
headers: combineHeaders(this.config.headers(), options.headers),
|
|
449
|
+
body,
|
|
450
|
+
failedResponseHandler: alibabaFailedResponseHandler,
|
|
451
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
452
|
+
alibabaChatChunkSchema
|
|
453
|
+
),
|
|
454
|
+
abortSignal: options.abortSignal,
|
|
455
|
+
fetch: this.config.fetch
|
|
456
|
+
});
|
|
457
|
+
let finishReason = "other";
|
|
458
|
+
let usage = void 0;
|
|
459
|
+
let isFirstChunk = true;
|
|
460
|
+
let activeText = false;
|
|
461
|
+
let activeReasoningId = null;
|
|
462
|
+
const toolCalls = [];
|
|
463
|
+
return {
|
|
464
|
+
stream: response.pipeThrough(
|
|
465
|
+
new TransformStream({
|
|
466
|
+
start(controller) {
|
|
467
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
468
|
+
},
|
|
469
|
+
transform(chunk, controller) {
|
|
470
|
+
var _a, _b, _c, _d;
|
|
471
|
+
if (options.includeRawChunks) {
|
|
472
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
473
|
+
}
|
|
474
|
+
if (!chunk.success) {
|
|
475
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
476
|
+
return;
|
|
477
|
+
}
|
|
478
|
+
const value = chunk.value;
|
|
479
|
+
if (isFirstChunk) {
|
|
480
|
+
isFirstChunk = false;
|
|
481
|
+
controller.enqueue({
|
|
482
|
+
type: "response-metadata",
|
|
483
|
+
...getResponseMetadata(value)
|
|
484
|
+
});
|
|
485
|
+
}
|
|
486
|
+
if (value.usage != null) {
|
|
487
|
+
usage = value.usage;
|
|
488
|
+
}
|
|
489
|
+
if (value.choices.length === 0) {
|
|
490
|
+
return;
|
|
491
|
+
}
|
|
492
|
+
const choice = value.choices[0];
|
|
493
|
+
const delta = choice.delta;
|
|
494
|
+
if (delta.reasoning_content != null && delta.reasoning_content.length > 0) {
|
|
495
|
+
if (activeReasoningId == null) {
|
|
496
|
+
if (activeText) {
|
|
497
|
+
controller.enqueue({ type: "text-end", id: "0" });
|
|
498
|
+
activeText = false;
|
|
499
|
+
}
|
|
500
|
+
activeReasoningId = generateId();
|
|
501
|
+
controller.enqueue({
|
|
502
|
+
type: "reasoning-start",
|
|
503
|
+
id: activeReasoningId
|
|
504
|
+
});
|
|
505
|
+
}
|
|
506
|
+
controller.enqueue({
|
|
507
|
+
type: "reasoning-delta",
|
|
508
|
+
id: activeReasoningId,
|
|
509
|
+
delta: delta.reasoning_content
|
|
510
|
+
});
|
|
511
|
+
}
|
|
512
|
+
if (delta.content != null && delta.content.length > 0) {
|
|
513
|
+
if (activeReasoningId != null) {
|
|
514
|
+
controller.enqueue({
|
|
515
|
+
type: "reasoning-end",
|
|
516
|
+
id: activeReasoningId
|
|
517
|
+
});
|
|
518
|
+
activeReasoningId = null;
|
|
519
|
+
}
|
|
520
|
+
if (!activeText) {
|
|
521
|
+
controller.enqueue({ type: "text-start", id: "0" });
|
|
522
|
+
activeText = true;
|
|
523
|
+
}
|
|
524
|
+
controller.enqueue({
|
|
525
|
+
type: "text-delta",
|
|
526
|
+
id: "0",
|
|
527
|
+
delta: delta.content
|
|
528
|
+
});
|
|
529
|
+
}
|
|
530
|
+
if (delta.tool_calls != null) {
|
|
531
|
+
if (activeReasoningId != null) {
|
|
532
|
+
controller.enqueue({
|
|
533
|
+
type: "reasoning-end",
|
|
534
|
+
id: activeReasoningId
|
|
535
|
+
});
|
|
536
|
+
activeReasoningId = null;
|
|
537
|
+
}
|
|
538
|
+
if (activeText) {
|
|
539
|
+
controller.enqueue({ type: "text-end", id: "0" });
|
|
540
|
+
activeText = false;
|
|
541
|
+
}
|
|
542
|
+
for (const toolCallDelta of delta.tool_calls) {
|
|
543
|
+
const index = (_a = toolCallDelta.index) != null ? _a : toolCalls.length;
|
|
544
|
+
if (toolCalls[index] == null) {
|
|
545
|
+
if (toolCallDelta.id == null) {
|
|
546
|
+
throw new InvalidResponseDataError({
|
|
547
|
+
data: toolCallDelta,
|
|
548
|
+
message: `Expected 'id' to be a string.`
|
|
549
|
+
});
|
|
550
|
+
}
|
|
551
|
+
if (((_b = toolCallDelta.function) == null ? void 0 : _b.name) == null) {
|
|
552
|
+
throw new InvalidResponseDataError({
|
|
553
|
+
data: toolCallDelta,
|
|
554
|
+
message: `Expected 'function.name' to be a string.`
|
|
555
|
+
});
|
|
556
|
+
}
|
|
557
|
+
controller.enqueue({
|
|
558
|
+
type: "tool-input-start",
|
|
559
|
+
id: toolCallDelta.id,
|
|
560
|
+
toolName: toolCallDelta.function.name
|
|
561
|
+
});
|
|
562
|
+
toolCalls[index] = {
|
|
563
|
+
id: toolCallDelta.id,
|
|
564
|
+
type: "function",
|
|
565
|
+
function: {
|
|
566
|
+
name: toolCallDelta.function.name,
|
|
567
|
+
arguments: (_c = toolCallDelta.function.arguments) != null ? _c : ""
|
|
568
|
+
},
|
|
569
|
+
hasFinished: false
|
|
570
|
+
};
|
|
571
|
+
const toolCall2 = toolCalls[index];
|
|
572
|
+
if (toolCall2.function.arguments.length > 0) {
|
|
573
|
+
controller.enqueue({
|
|
574
|
+
type: "tool-input-delta",
|
|
575
|
+
id: toolCall2.id,
|
|
576
|
+
delta: toolCall2.function.arguments
|
|
577
|
+
});
|
|
578
|
+
}
|
|
579
|
+
if (isParsableJson(toolCall2.function.arguments)) {
|
|
580
|
+
controller.enqueue({
|
|
581
|
+
type: "tool-input-end",
|
|
582
|
+
id: toolCall2.id
|
|
583
|
+
});
|
|
584
|
+
controller.enqueue({
|
|
585
|
+
type: "tool-call",
|
|
586
|
+
toolCallId: toolCall2.id,
|
|
587
|
+
toolName: toolCall2.function.name,
|
|
588
|
+
input: toolCall2.function.arguments
|
|
589
|
+
});
|
|
590
|
+
toolCall2.hasFinished = true;
|
|
591
|
+
}
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
const toolCall = toolCalls[index];
|
|
595
|
+
if (toolCall.hasFinished) {
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
if (((_d = toolCallDelta.function) == null ? void 0 : _d.arguments) != null) {
|
|
599
|
+
toolCall.function.arguments += toolCallDelta.function.arguments;
|
|
600
|
+
controller.enqueue({
|
|
601
|
+
type: "tool-input-delta",
|
|
602
|
+
id: toolCall.id,
|
|
603
|
+
delta: toolCallDelta.function.arguments
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
if (isParsableJson(toolCall.function.arguments)) {
|
|
607
|
+
controller.enqueue({
|
|
608
|
+
type: "tool-input-end",
|
|
609
|
+
id: toolCall.id
|
|
610
|
+
});
|
|
611
|
+
controller.enqueue({
|
|
612
|
+
type: "tool-call",
|
|
613
|
+
toolCallId: toolCall.id,
|
|
614
|
+
toolName: toolCall.function.name,
|
|
615
|
+
input: toolCall.function.arguments
|
|
616
|
+
});
|
|
617
|
+
toolCall.hasFinished = true;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
if (choice.finish_reason != null) {
|
|
622
|
+
finishReason = mapOpenAICompatibleFinishReason(
|
|
623
|
+
choice.finish_reason
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
},
|
|
627
|
+
flush(controller) {
|
|
628
|
+
var _a, _b;
|
|
629
|
+
if (activeReasoningId != null) {
|
|
630
|
+
controller.enqueue({
|
|
631
|
+
type: "reasoning-end",
|
|
632
|
+
id: activeReasoningId
|
|
633
|
+
});
|
|
634
|
+
}
|
|
635
|
+
if (activeText) {
|
|
636
|
+
controller.enqueue({ type: "text-end", id: "0" });
|
|
637
|
+
}
|
|
638
|
+
controller.enqueue({
|
|
639
|
+
type: "finish",
|
|
640
|
+
finishReason,
|
|
641
|
+
usage: convertAlibabaUsage(usage),
|
|
642
|
+
providerMetadata: {
|
|
643
|
+
alibaba: {
|
|
644
|
+
cacheCreationInputTokens: (_b = (_a = usage == null ? void 0 : usage.prompt_tokens_details) == null ? void 0 : _a.cache_creation_input_tokens) != null ? _b : null
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
}
|
|
649
|
+
})
|
|
650
|
+
),
|
|
651
|
+
request: { body: JSON.stringify(body) },
|
|
652
|
+
response: { headers: responseHeaders }
|
|
653
|
+
};
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
var alibabaUsageSchema = z2.object({
|
|
657
|
+
prompt_tokens: z2.number(),
|
|
658
|
+
completion_tokens: z2.number(),
|
|
659
|
+
total_tokens: z2.number(),
|
|
660
|
+
prompt_tokens_details: z2.object({
|
|
661
|
+
cached_tokens: z2.number().nullish(),
|
|
662
|
+
cache_creation_input_tokens: z2.number().nullish()
|
|
663
|
+
}).nullish(),
|
|
664
|
+
completion_tokens_details: z2.object({
|
|
665
|
+
reasoning_tokens: z2.number().nullish()
|
|
666
|
+
}).nullish()
|
|
667
|
+
});
|
|
668
|
+
var alibabaChatResponseSchema = z2.object({
|
|
669
|
+
id: z2.string().nullish(),
|
|
670
|
+
created: z2.number().nullish(),
|
|
671
|
+
model: z2.string().nullish(),
|
|
672
|
+
choices: z2.array(
|
|
673
|
+
z2.object({
|
|
674
|
+
message: z2.object({
|
|
675
|
+
role: z2.literal("assistant").nullish(),
|
|
676
|
+
content: z2.string().nullish(),
|
|
677
|
+
reasoning_content: z2.string().nullish(),
|
|
678
|
+
// Alibaba thinking mode
|
|
679
|
+
tool_calls: z2.array(
|
|
680
|
+
z2.object({
|
|
681
|
+
id: z2.string(),
|
|
682
|
+
type: z2.literal("function"),
|
|
683
|
+
function: z2.object({
|
|
684
|
+
name: z2.string(),
|
|
685
|
+
arguments: z2.string()
|
|
686
|
+
})
|
|
687
|
+
})
|
|
688
|
+
).nullish()
|
|
689
|
+
}),
|
|
690
|
+
finish_reason: z2.string().nullish(),
|
|
691
|
+
index: z2.number()
|
|
692
|
+
})
|
|
693
|
+
),
|
|
694
|
+
usage: alibabaUsageSchema.nullish()
|
|
695
|
+
});
|
|
696
|
+
var alibabaChatChunkSchema = z2.object({
|
|
697
|
+
id: z2.string().nullish(),
|
|
698
|
+
created: z2.number().nullish(),
|
|
699
|
+
model: z2.string().nullish(),
|
|
700
|
+
choices: z2.array(
|
|
701
|
+
z2.object({
|
|
702
|
+
delta: z2.object({
|
|
703
|
+
role: z2.enum(["assistant"]).nullish(),
|
|
704
|
+
content: z2.string().nullish(),
|
|
705
|
+
reasoning_content: z2.string().nullish(),
|
|
706
|
+
// Alibaba thinking mode delta
|
|
707
|
+
tool_calls: z2.array(
|
|
708
|
+
z2.object({
|
|
709
|
+
index: z2.number().nullish(),
|
|
710
|
+
// Index for accumulating tool calls
|
|
711
|
+
id: z2.string().nullish(),
|
|
712
|
+
type: z2.literal("function").nullish(),
|
|
713
|
+
function: z2.object({
|
|
714
|
+
name: z2.string().nullish(),
|
|
715
|
+
arguments: z2.string().nullish()
|
|
716
|
+
}).nullish()
|
|
717
|
+
})
|
|
718
|
+
).nullish()
|
|
719
|
+
}),
|
|
720
|
+
finish_reason: z2.string().nullish(),
|
|
721
|
+
index: z2.number()
|
|
722
|
+
})
|
|
723
|
+
),
|
|
724
|
+
usage: alibabaUsageSchema.nullish()
|
|
725
|
+
// Usage only appears in final chunk
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
// src/version.ts
|
|
729
|
+
var VERSION = "0.0.1";
|
|
730
|
+
|
|
731
|
+
// src/alibaba-provider.ts
|
|
732
|
+
var alibabaErrorDataSchema = z3.object({
|
|
733
|
+
error: z3.object({
|
|
734
|
+
message: z3.string(),
|
|
735
|
+
code: z3.string().nullish(),
|
|
736
|
+
type: z3.string().nullish()
|
|
737
|
+
})
|
|
738
|
+
});
|
|
739
|
+
var alibabaFailedResponseHandler = createJsonErrorResponseHandler({
|
|
740
|
+
errorSchema: alibabaErrorDataSchema,
|
|
741
|
+
errorToMessage: (data) => data.error.message
|
|
742
|
+
});
|
|
743
|
+
function createAlibaba(options = {}) {
|
|
744
|
+
var _a;
|
|
745
|
+
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://dashscope-intl.aliyuncs.com/compatible-mode/v1";
|
|
746
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
747
|
+
{
|
|
748
|
+
Authorization: `Bearer ${loadApiKey({
|
|
749
|
+
apiKey: options.apiKey,
|
|
750
|
+
environmentVariableName: "ALIBABA_API_KEY",
|
|
751
|
+
description: "Alibaba Cloud (DashScope)"
|
|
752
|
+
})}`,
|
|
753
|
+
...options.headers
|
|
754
|
+
},
|
|
755
|
+
`ai-sdk/alibaba/${VERSION}`
|
|
756
|
+
);
|
|
757
|
+
const createLanguageModel = (modelId) => {
|
|
758
|
+
var _a2;
|
|
759
|
+
return new AlibabaLanguageModel(modelId, {
|
|
760
|
+
provider: "alibaba.chat",
|
|
761
|
+
baseURL,
|
|
762
|
+
headers: getHeaders,
|
|
763
|
+
fetch: options.fetch,
|
|
764
|
+
includeUsage: (_a2 = options.includeUsage) != null ? _a2 : true
|
|
765
|
+
});
|
|
766
|
+
};
|
|
767
|
+
const provider = function(modelId) {
|
|
768
|
+
if (new.target) {
|
|
769
|
+
throw new Error(
|
|
770
|
+
"The Alibaba model function cannot be called with the new keyword."
|
|
771
|
+
);
|
|
772
|
+
}
|
|
773
|
+
return createLanguageModel(modelId);
|
|
774
|
+
};
|
|
775
|
+
provider.specificationVersion = "v2";
|
|
776
|
+
provider.languageModel = createLanguageModel;
|
|
777
|
+
provider.chatModel = createLanguageModel;
|
|
778
|
+
provider.imageModel = (modelId) => {
|
|
779
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
780
|
+
};
|
|
781
|
+
provider.textEmbeddingModel = (modelId) => {
|
|
782
|
+
throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
|
783
|
+
};
|
|
784
|
+
return provider;
|
|
785
|
+
}
|
|
786
|
+
var alibaba = createAlibaba();
|
|
787
|
+
export {
|
|
788
|
+
VERSION,
|
|
789
|
+
alibaba,
|
|
790
|
+
createAlibaba
|
|
791
|
+
};
|
|
792
|
+
//# sourceMappingURL=index.mjs.map
|