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