@ai-sdk/alibaba 2.0.0-beta.3 → 2.0.0-beta.31
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 +251 -0
- package/README.md +2 -0
- package/dist/index.js +259 -281
- package/dist/index.js.map +1 -1
- package/package.json +8 -10
- package/src/alibaba-chat-language-model.ts +81 -119
- package/src/alibaba-config.ts +1 -1
- package/src/convert-to-alibaba-chat-messages.ts +17 -26
- package/dist/index.d.mts +0 -119
- package/dist/index.mjs +0 -1054
- package/dist/index.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,69 +1,64 @@
|
|
|
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
1
|
// src/alibaba-provider.ts
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
2
|
+
import {
|
|
3
|
+
NoSuchModelError
|
|
4
|
+
} from "@ai-sdk/provider";
|
|
5
|
+
import {
|
|
6
|
+
createJsonErrorResponseHandler as createJsonErrorResponseHandler2,
|
|
7
|
+
loadApiKey,
|
|
8
|
+
withoutTrailingSlash,
|
|
9
|
+
withUserAgentSuffix
|
|
10
|
+
} from "@ai-sdk/provider-utils";
|
|
11
|
+
import { z as z4 } from "zod/v4";
|
|
33
12
|
|
|
34
13
|
// src/alibaba-chat-language-model.ts
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
14
|
+
import {
|
|
15
|
+
getResponseMetadata,
|
|
16
|
+
mapOpenAICompatibleFinishReason,
|
|
17
|
+
prepareTools
|
|
18
|
+
} from "@ai-sdk/openai-compatible/internal";
|
|
19
|
+
import {
|
|
20
|
+
combineHeaders,
|
|
21
|
+
createEventSourceResponseHandler,
|
|
22
|
+
createJsonResponseHandler,
|
|
23
|
+
generateId,
|
|
24
|
+
isCustomReasoning,
|
|
25
|
+
mapReasoningToProviderBudget,
|
|
26
|
+
parseProviderOptions,
|
|
27
|
+
postJsonToApi,
|
|
28
|
+
serializeModelOptions,
|
|
29
|
+
StreamingToolCallTracker,
|
|
30
|
+
WORKFLOW_SERIALIZE,
|
|
31
|
+
WORKFLOW_DESERIALIZE
|
|
32
|
+
} from "@ai-sdk/provider-utils";
|
|
33
|
+
import { z as z2 } from "zod/v4";
|
|
39
34
|
|
|
40
35
|
// src/alibaba-chat-options.ts
|
|
41
|
-
|
|
42
|
-
var alibabaLanguageModelOptions =
|
|
36
|
+
import { z } from "zod/v4";
|
|
37
|
+
var alibabaLanguageModelOptions = z.object({
|
|
43
38
|
/**
|
|
44
39
|
* Enable thinking/reasoning mode for supported models.
|
|
45
40
|
* When enabled, the model generates reasoning content before the response.
|
|
46
41
|
*
|
|
47
42
|
* @default false
|
|
48
43
|
*/
|
|
49
|
-
enableThinking:
|
|
44
|
+
enableThinking: z.boolean().optional(),
|
|
50
45
|
/**
|
|
51
46
|
* Maximum number of reasoning tokens to generate.
|
|
52
47
|
*/
|
|
53
|
-
thinkingBudget:
|
|
48
|
+
thinkingBudget: z.number().positive().optional(),
|
|
54
49
|
/**
|
|
55
50
|
* Whether to enable parallel function calling during tool use.
|
|
56
51
|
*
|
|
57
52
|
* @default true
|
|
58
53
|
*/
|
|
59
|
-
parallelToolCalls:
|
|
54
|
+
parallelToolCalls: z.boolean().optional()
|
|
60
55
|
});
|
|
61
56
|
|
|
62
57
|
// src/convert-alibaba-usage.ts
|
|
63
|
-
|
|
58
|
+
import { convertOpenAICompatibleChatUsage } from "@ai-sdk/openai-compatible/internal";
|
|
64
59
|
function convertAlibabaUsage(usage) {
|
|
65
60
|
var _a, _b, _c, _d;
|
|
66
|
-
const baseUsage =
|
|
61
|
+
const baseUsage = convertOpenAICompatibleChatUsage(usage);
|
|
67
62
|
const cacheWriteTokens = (_b = (_a = usage == null ? void 0 : usage.prompt_tokens_details) == null ? void 0 : _a.cache_creation_input_tokens) != null ? _b : 0;
|
|
68
63
|
const noCacheTokens = ((_c = baseUsage.inputTokens.total) != null ? _c : 0) - ((_d = baseUsage.inputTokens.cacheRead) != null ? _d : 0) - cacheWriteTokens;
|
|
69
64
|
return {
|
|
@@ -77,19 +72,21 @@ function convertAlibabaUsage(usage) {
|
|
|
77
72
|
}
|
|
78
73
|
|
|
79
74
|
// src/convert-to-alibaba-chat-messages.ts
|
|
80
|
-
|
|
81
|
-
|
|
75
|
+
import {
|
|
76
|
+
UnsupportedFunctionalityError
|
|
77
|
+
} from "@ai-sdk/provider";
|
|
78
|
+
import { convertToBase64, isProviderReference } from "@ai-sdk/provider-utils";
|
|
82
79
|
function formatImageUrl({
|
|
83
80
|
data,
|
|
84
81
|
mediaType
|
|
85
82
|
}) {
|
|
86
|
-
return data instanceof URL ? data.toString() : `data:${mediaType};base64,${
|
|
83
|
+
return data instanceof URL ? data.toString() : `data:${mediaType};base64,${convertToBase64(data)}`;
|
|
87
84
|
}
|
|
88
85
|
function convertToAlibabaChatMessages({
|
|
89
86
|
prompt,
|
|
90
87
|
cacheControlValidator
|
|
91
88
|
}) {
|
|
92
|
-
var _a;
|
|
89
|
+
var _a, _b;
|
|
93
90
|
const messages = [];
|
|
94
91
|
for (const { role, content, ...message } of prompt) {
|
|
95
92
|
const messageCacheControl = cacheControlValidator == null ? void 0 : cacheControlValidator.getCacheControl(
|
|
@@ -114,18 +111,12 @@ function convertToAlibabaChatMessages({
|
|
|
114
111
|
break;
|
|
115
112
|
}
|
|
116
113
|
case "user": {
|
|
117
|
-
const isSinglePart = content.length === 1;
|
|
118
|
-
if (isSinglePart && content[0].type === "text" && !messageCacheControl) {
|
|
119
|
-
messages.push({
|
|
120
|
-
role: "user",
|
|
121
|
-
content: content[0].text
|
|
122
|
-
});
|
|
123
|
-
break;
|
|
124
|
-
}
|
|
125
114
|
messages.push({
|
|
126
115
|
role: "user",
|
|
127
|
-
content: content.map((part) => {
|
|
128
|
-
|
|
116
|
+
content: content.map((part, index) => {
|
|
117
|
+
var _a2;
|
|
118
|
+
const isLastPart = index === content.length - 1;
|
|
119
|
+
const partCacheControl = (_a2 = cacheControlValidator == null ? void 0 : cacheControlValidator.getCacheControl(part.providerOptions)) != null ? _a2 : isLastPart ? messageCacheControl : void 0;
|
|
129
120
|
switch (part.type) {
|
|
130
121
|
case "text": {
|
|
131
122
|
return {
|
|
@@ -135,6 +126,11 @@ function convertToAlibabaChatMessages({
|
|
|
135
126
|
};
|
|
136
127
|
}
|
|
137
128
|
case "file": {
|
|
129
|
+
if (isProviderReference(part.data)) {
|
|
130
|
+
throw new UnsupportedFunctionalityError({
|
|
131
|
+
functionality: "file parts with provider references"
|
|
132
|
+
});
|
|
133
|
+
}
|
|
138
134
|
if (part.mediaType.startsWith("image/")) {
|
|
139
135
|
const mediaType = part.mediaType === "image/*" ? "image/jpeg" : part.mediaType;
|
|
140
136
|
return {
|
|
@@ -145,7 +141,7 @@ function convertToAlibabaChatMessages({
|
|
|
145
141
|
...partCacheControl ? { cache_control: partCacheControl } : {}
|
|
146
142
|
};
|
|
147
143
|
} else {
|
|
148
|
-
throw new
|
|
144
|
+
throw new UnsupportedFunctionalityError({
|
|
149
145
|
functionality: "Only image file parts are supported"
|
|
150
146
|
});
|
|
151
147
|
}
|
|
@@ -192,13 +188,13 @@ function convertToAlibabaChatMessages({
|
|
|
192
188
|
const toolResponses = content.filter(
|
|
193
189
|
(r) => r.type !== "tool-approval-response"
|
|
194
190
|
);
|
|
195
|
-
const isSinglePart = toolResponses.length === 1;
|
|
196
191
|
for (let i = 0; i < toolResponses.length; i++) {
|
|
197
192
|
const toolResponse = toolResponses[i];
|
|
198
193
|
const output = toolResponse.output;
|
|
199
|
-
const
|
|
194
|
+
const isLastPart = i === toolResponses.length - 1;
|
|
195
|
+
const partCacheControl = (_a = cacheControlValidator == null ? void 0 : cacheControlValidator.getCacheControl(
|
|
200
196
|
toolResponse.providerOptions
|
|
201
|
-
);
|
|
197
|
+
)) != null ? _a : isLastPart ? messageCacheControl : void 0;
|
|
202
198
|
let contentValue;
|
|
203
199
|
switch (output.type) {
|
|
204
200
|
case "text":
|
|
@@ -206,7 +202,7 @@ function convertToAlibabaChatMessages({
|
|
|
206
202
|
contentValue = output.value;
|
|
207
203
|
break;
|
|
208
204
|
case "execution-denied":
|
|
209
|
-
contentValue = (
|
|
205
|
+
contentValue = (_b = output.reason) != null ? _b : "Tool execution denied.";
|
|
210
206
|
break;
|
|
211
207
|
case "content":
|
|
212
208
|
case "json":
|
|
@@ -270,7 +266,7 @@ var CacheControlValidator = class {
|
|
|
270
266
|
};
|
|
271
267
|
|
|
272
268
|
// src/alibaba-chat-language-model.ts
|
|
273
|
-
var AlibabaLanguageModel = class {
|
|
269
|
+
var AlibabaLanguageModel = class _AlibabaLanguageModel {
|
|
274
270
|
constructor(modelId, config) {
|
|
275
271
|
this.specificationVersion = "v4";
|
|
276
272
|
this.supportedUrls = {
|
|
@@ -279,6 +275,15 @@ var AlibabaLanguageModel = class {
|
|
|
279
275
|
this.modelId = modelId;
|
|
280
276
|
this.config = config;
|
|
281
277
|
}
|
|
278
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
279
|
+
return serializeModelOptions({
|
|
280
|
+
modelId: model.modelId,
|
|
281
|
+
config: model.config
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
285
|
+
return new _AlibabaLanguageModel(options.modelId, options.config);
|
|
286
|
+
}
|
|
282
287
|
get provider() {
|
|
283
288
|
return this.config.provider;
|
|
284
289
|
}
|
|
@@ -297,6 +302,7 @@ var AlibabaLanguageModel = class {
|
|
|
297
302
|
stopSequences,
|
|
298
303
|
responseFormat,
|
|
299
304
|
seed,
|
|
305
|
+
reasoning,
|
|
300
306
|
providerOptions,
|
|
301
307
|
tools,
|
|
302
308
|
toolChoice
|
|
@@ -304,7 +310,7 @@ var AlibabaLanguageModel = class {
|
|
|
304
310
|
var _a;
|
|
305
311
|
const warnings = [];
|
|
306
312
|
const cacheControlValidator = new CacheControlValidator();
|
|
307
|
-
const alibabaOptions = await
|
|
313
|
+
const alibabaOptions = await parseProviderOptions({
|
|
308
314
|
provider: "alibaba",
|
|
309
315
|
providerOptions,
|
|
310
316
|
schema: alibabaLanguageModelOptions
|
|
@@ -329,9 +335,11 @@ var AlibabaLanguageModel = class {
|
|
|
329
335
|
description: responseFormat.description
|
|
330
336
|
}
|
|
331
337
|
} : { type: "json_object" } : void 0,
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
338
|
+
...resolveAlibabaThinking({
|
|
339
|
+
reasoning,
|
|
340
|
+
alibabaOptions,
|
|
341
|
+
warnings
|
|
342
|
+
}),
|
|
335
343
|
// Convert messages with cache control support
|
|
336
344
|
messages: convertToAlibabaChatMessages({
|
|
337
345
|
prompt,
|
|
@@ -342,7 +350,7 @@ var AlibabaLanguageModel = class {
|
|
|
342
350
|
tools: alibabaTools,
|
|
343
351
|
toolChoice: alibabaToolChoice,
|
|
344
352
|
toolWarnings
|
|
345
|
-
} =
|
|
353
|
+
} = prepareTools({ tools, toolChoice });
|
|
346
354
|
warnings.push(...cacheControlValidator.getWarnings());
|
|
347
355
|
return {
|
|
348
356
|
args: {
|
|
@@ -355,18 +363,18 @@ var AlibabaLanguageModel = class {
|
|
|
355
363
|
};
|
|
356
364
|
}
|
|
357
365
|
async doGenerate(options) {
|
|
358
|
-
var _a;
|
|
366
|
+
var _a, _b, _c, _d;
|
|
359
367
|
const { args, warnings } = await this.getArgs(options);
|
|
360
368
|
const {
|
|
361
369
|
responseHeaders,
|
|
362
370
|
value: response,
|
|
363
371
|
rawValue: rawResponse
|
|
364
|
-
} = await
|
|
372
|
+
} = await postJsonToApi({
|
|
365
373
|
url: `${this.config.baseURL}/chat/completions`,
|
|
366
|
-
headers: (
|
|
374
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
367
375
|
body: args,
|
|
368
376
|
failedResponseHandler: alibabaFailedResponseHandler,
|
|
369
|
-
successfulResponseHandler:
|
|
377
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
370
378
|
alibabaChatResponseSchema
|
|
371
379
|
),
|
|
372
380
|
abortSignal: options.abortSignal,
|
|
@@ -389,7 +397,7 @@ var AlibabaLanguageModel = class {
|
|
|
389
397
|
for (const toolCall of choice.message.tool_calls) {
|
|
390
398
|
content.push({
|
|
391
399
|
type: "tool-call",
|
|
392
|
-
toolCallId: toolCall.id,
|
|
400
|
+
toolCallId: (_c = toolCall.id) != null ? _c : generateId(),
|
|
393
401
|
toolName: toolCall.function.name,
|
|
394
402
|
input: toolCall.function.arguments
|
|
395
403
|
});
|
|
@@ -398,13 +406,13 @@ var AlibabaLanguageModel = class {
|
|
|
398
406
|
return {
|
|
399
407
|
content,
|
|
400
408
|
finishReason: {
|
|
401
|
-
unified:
|
|
402
|
-
raw: (
|
|
409
|
+
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
410
|
+
raw: (_d = choice.finish_reason) != null ? _d : void 0
|
|
403
411
|
},
|
|
404
412
|
usage: convertAlibabaUsage(response.usage),
|
|
405
413
|
request: { body: JSON.stringify(args) },
|
|
406
414
|
response: {
|
|
407
|
-
...
|
|
415
|
+
...getResponseMetadata(response),
|
|
408
416
|
headers: responseHeaders,
|
|
409
417
|
body: rawResponse
|
|
410
418
|
},
|
|
@@ -412,18 +420,19 @@ var AlibabaLanguageModel = class {
|
|
|
412
420
|
};
|
|
413
421
|
}
|
|
414
422
|
async doStream(options) {
|
|
423
|
+
var _a, _b;
|
|
415
424
|
const { args, warnings } = await this.getArgs(options);
|
|
416
425
|
const body = {
|
|
417
426
|
...args,
|
|
418
427
|
stream: true,
|
|
419
428
|
stream_options: this.config.includeUsage ? { include_usage: true } : void 0
|
|
420
429
|
};
|
|
421
|
-
const { responseHeaders, value: response } = await
|
|
430
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
422
431
|
url: `${this.config.baseURL}/chat/completions`,
|
|
423
|
-
headers: (
|
|
432
|
+
headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
|
|
424
433
|
body,
|
|
425
434
|
failedResponseHandler: alibabaFailedResponseHandler,
|
|
426
|
-
successfulResponseHandler:
|
|
435
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
427
436
|
alibabaChatChunkSchema
|
|
428
437
|
),
|
|
429
438
|
abortSignal: options.abortSignal,
|
|
@@ -437,7 +446,7 @@ var AlibabaLanguageModel = class {
|
|
|
437
446
|
let isFirstChunk = true;
|
|
438
447
|
let activeText = false;
|
|
439
448
|
let activeReasoningId = null;
|
|
440
|
-
const
|
|
449
|
+
const toolCallTracker = new StreamingToolCallTracker({ generateId });
|
|
441
450
|
return {
|
|
442
451
|
stream: response.pipeThrough(
|
|
443
452
|
new TransformStream({
|
|
@@ -445,7 +454,6 @@ var AlibabaLanguageModel = class {
|
|
|
445
454
|
controller.enqueue({ type: "stream-start", warnings });
|
|
446
455
|
},
|
|
447
456
|
transform(chunk, controller) {
|
|
448
|
-
var _a, _b, _c, _d;
|
|
449
457
|
if (options.includeRawChunks) {
|
|
450
458
|
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
451
459
|
}
|
|
@@ -458,7 +466,7 @@ var AlibabaLanguageModel = class {
|
|
|
458
466
|
isFirstChunk = false;
|
|
459
467
|
controller.enqueue({
|
|
460
468
|
type: "response-metadata",
|
|
461
|
-
...
|
|
469
|
+
...getResponseMetadata(value)
|
|
462
470
|
});
|
|
463
471
|
}
|
|
464
472
|
if (value.usage != null) {
|
|
@@ -475,7 +483,7 @@ var AlibabaLanguageModel = class {
|
|
|
475
483
|
controller.enqueue({ type: "text-end", id: "0" });
|
|
476
484
|
activeText = false;
|
|
477
485
|
}
|
|
478
|
-
activeReasoningId =
|
|
486
|
+
activeReasoningId = generateId();
|
|
479
487
|
controller.enqueue({
|
|
480
488
|
type: "reasoning-start",
|
|
481
489
|
id: activeReasoningId
|
|
@@ -518,87 +526,15 @@ var AlibabaLanguageModel = class {
|
|
|
518
526
|
activeText = false;
|
|
519
527
|
}
|
|
520
528
|
for (const toolCallDelta of delta.tool_calls) {
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
data: toolCallDelta,
|
|
526
|
-
message: `Expected 'id' to be a string.`
|
|
527
|
-
});
|
|
528
|
-
}
|
|
529
|
-
if (((_b = toolCallDelta.function) == null ? void 0 : _b.name) == null) {
|
|
530
|
-
throw new import_provider2.InvalidResponseDataError({
|
|
531
|
-
data: toolCallDelta,
|
|
532
|
-
message: `Expected 'function.name' to be a string.`
|
|
533
|
-
});
|
|
534
|
-
}
|
|
535
|
-
controller.enqueue({
|
|
536
|
-
type: "tool-input-start",
|
|
537
|
-
id: toolCallDelta.id,
|
|
538
|
-
toolName: toolCallDelta.function.name
|
|
539
|
-
});
|
|
540
|
-
toolCalls[index] = {
|
|
541
|
-
id: toolCallDelta.id,
|
|
542
|
-
type: "function",
|
|
543
|
-
function: {
|
|
544
|
-
name: toolCallDelta.function.name,
|
|
545
|
-
arguments: (_c = toolCallDelta.function.arguments) != null ? _c : ""
|
|
546
|
-
},
|
|
547
|
-
hasFinished: false
|
|
548
|
-
};
|
|
549
|
-
const toolCall2 = toolCalls[index];
|
|
550
|
-
if (toolCall2.function.arguments.length > 0) {
|
|
551
|
-
controller.enqueue({
|
|
552
|
-
type: "tool-input-delta",
|
|
553
|
-
id: toolCall2.id,
|
|
554
|
-
delta: toolCall2.function.arguments
|
|
555
|
-
});
|
|
556
|
-
}
|
|
557
|
-
if ((0, import_provider_utils2.isParsableJson)(toolCall2.function.arguments)) {
|
|
558
|
-
controller.enqueue({
|
|
559
|
-
type: "tool-input-end",
|
|
560
|
-
id: toolCall2.id
|
|
561
|
-
});
|
|
562
|
-
controller.enqueue({
|
|
563
|
-
type: "tool-call",
|
|
564
|
-
toolCallId: toolCall2.id,
|
|
565
|
-
toolName: toolCall2.function.name,
|
|
566
|
-
input: toolCall2.function.arguments
|
|
567
|
-
});
|
|
568
|
-
toolCall2.hasFinished = true;
|
|
569
|
-
}
|
|
570
|
-
continue;
|
|
571
|
-
}
|
|
572
|
-
const toolCall = toolCalls[index];
|
|
573
|
-
if (toolCall.hasFinished) {
|
|
574
|
-
continue;
|
|
575
|
-
}
|
|
576
|
-
if (((_d = toolCallDelta.function) == null ? void 0 : _d.arguments) != null) {
|
|
577
|
-
toolCall.function.arguments += toolCallDelta.function.arguments;
|
|
578
|
-
controller.enqueue({
|
|
579
|
-
type: "tool-input-delta",
|
|
580
|
-
id: toolCall.id,
|
|
581
|
-
delta: toolCallDelta.function.arguments
|
|
582
|
-
});
|
|
583
|
-
}
|
|
584
|
-
if ((0, import_provider_utils2.isParsableJson)(toolCall.function.arguments)) {
|
|
585
|
-
controller.enqueue({
|
|
586
|
-
type: "tool-input-end",
|
|
587
|
-
id: toolCall.id
|
|
588
|
-
});
|
|
589
|
-
controller.enqueue({
|
|
590
|
-
type: "tool-call",
|
|
591
|
-
toolCallId: toolCall.id,
|
|
592
|
-
toolName: toolCall.function.name,
|
|
593
|
-
input: toolCall.function.arguments
|
|
594
|
-
});
|
|
595
|
-
toolCall.hasFinished = true;
|
|
596
|
-
}
|
|
529
|
+
toolCallTracker.processDelta(
|
|
530
|
+
toolCallDelta,
|
|
531
|
+
controller.enqueue.bind(controller)
|
|
532
|
+
);
|
|
597
533
|
}
|
|
598
534
|
}
|
|
599
535
|
if (choice.finish_reason != null) {
|
|
600
536
|
finishReason = {
|
|
601
|
-
unified:
|
|
537
|
+
unified: mapOpenAICompatibleFinishReason(choice.finish_reason),
|
|
602
538
|
raw: choice.finish_reason
|
|
603
539
|
};
|
|
604
540
|
}
|
|
@@ -613,6 +549,7 @@ var AlibabaLanguageModel = class {
|
|
|
613
549
|
if (activeText) {
|
|
614
550
|
controller.enqueue({ type: "text-end", id: "0" });
|
|
615
551
|
}
|
|
552
|
+
toolCallTracker.flush(controller.enqueue.bind(controller));
|
|
616
553
|
controller.enqueue({
|
|
617
554
|
type: "finish",
|
|
618
555
|
finishReason,
|
|
@@ -626,72 +563,100 @@ var AlibabaLanguageModel = class {
|
|
|
626
563
|
};
|
|
627
564
|
}
|
|
628
565
|
};
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
566
|
+
function resolveAlibabaThinking({
|
|
567
|
+
reasoning,
|
|
568
|
+
alibabaOptions,
|
|
569
|
+
warnings
|
|
570
|
+
}) {
|
|
571
|
+
if ((alibabaOptions == null ? void 0 : alibabaOptions.enableThinking) != null || (alibabaOptions == null ? void 0 : alibabaOptions.thinkingBudget) != null) {
|
|
572
|
+
return {
|
|
573
|
+
...alibabaOptions.enableThinking != null ? { enable_thinking: alibabaOptions.enableThinking } : {},
|
|
574
|
+
...alibabaOptions.thinkingBudget != null ? { thinking_budget: alibabaOptions.thinkingBudget } : {}
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
if (!isCustomReasoning(reasoning)) {
|
|
578
|
+
return {};
|
|
579
|
+
}
|
|
580
|
+
if (reasoning === "none") {
|
|
581
|
+
return { enable_thinking: false };
|
|
582
|
+
}
|
|
583
|
+
const thinkingBudget = mapReasoningToProviderBudget({
|
|
584
|
+
reasoning,
|
|
585
|
+
maxOutputTokens: 16384,
|
|
586
|
+
maxReasoningBudget: 16384,
|
|
587
|
+
warnings
|
|
588
|
+
});
|
|
589
|
+
return {
|
|
590
|
+
enable_thinking: true,
|
|
591
|
+
...thinkingBudget != null ? { thinking_budget: thinkingBudget } : {}
|
|
592
|
+
};
|
|
593
|
+
}
|
|
594
|
+
var alibabaUsageSchema = z2.object({
|
|
595
|
+
prompt_tokens: z2.number(),
|
|
596
|
+
completion_tokens: z2.number(),
|
|
597
|
+
total_tokens: z2.number(),
|
|
598
|
+
prompt_tokens_details: z2.object({
|
|
599
|
+
cached_tokens: z2.number().nullish(),
|
|
600
|
+
cache_creation_input_tokens: z2.number().nullish()
|
|
636
601
|
}).nullish(),
|
|
637
|
-
completion_tokens_details:
|
|
638
|
-
reasoning_tokens:
|
|
602
|
+
completion_tokens_details: z2.object({
|
|
603
|
+
reasoning_tokens: z2.number().nullish()
|
|
639
604
|
}).nullish()
|
|
640
605
|
});
|
|
641
|
-
var alibabaChatResponseSchema =
|
|
642
|
-
id:
|
|
643
|
-
created:
|
|
644
|
-
model:
|
|
645
|
-
choices:
|
|
646
|
-
|
|
647
|
-
message:
|
|
648
|
-
role:
|
|
649
|
-
content:
|
|
650
|
-
reasoning_content:
|
|
606
|
+
var alibabaChatResponseSchema = z2.object({
|
|
607
|
+
id: z2.string().nullish(),
|
|
608
|
+
created: z2.number().nullish(),
|
|
609
|
+
model: z2.string().nullish(),
|
|
610
|
+
choices: z2.array(
|
|
611
|
+
z2.object({
|
|
612
|
+
message: z2.object({
|
|
613
|
+
role: z2.literal("assistant").nullish(),
|
|
614
|
+
content: z2.string().nullish(),
|
|
615
|
+
reasoning_content: z2.string().nullish(),
|
|
651
616
|
// Alibaba thinking mode
|
|
652
|
-
tool_calls:
|
|
653
|
-
|
|
654
|
-
id:
|
|
655
|
-
type:
|
|
656
|
-
function:
|
|
657
|
-
name:
|
|
658
|
-
arguments:
|
|
617
|
+
tool_calls: z2.array(
|
|
618
|
+
z2.object({
|
|
619
|
+
id: z2.string(),
|
|
620
|
+
type: z2.literal("function"),
|
|
621
|
+
function: z2.object({
|
|
622
|
+
name: z2.string(),
|
|
623
|
+
arguments: z2.string()
|
|
659
624
|
})
|
|
660
625
|
})
|
|
661
626
|
).nullish()
|
|
662
627
|
}),
|
|
663
|
-
finish_reason:
|
|
664
|
-
index:
|
|
628
|
+
finish_reason: z2.string().nullish(),
|
|
629
|
+
index: z2.number()
|
|
665
630
|
})
|
|
666
631
|
),
|
|
667
632
|
usage: alibabaUsageSchema.nullish()
|
|
668
633
|
});
|
|
669
|
-
var alibabaChatChunkSchema =
|
|
670
|
-
id:
|
|
671
|
-
created:
|
|
672
|
-
model:
|
|
673
|
-
choices:
|
|
674
|
-
|
|
675
|
-
delta:
|
|
676
|
-
role:
|
|
677
|
-
content:
|
|
678
|
-
reasoning_content:
|
|
634
|
+
var alibabaChatChunkSchema = z2.object({
|
|
635
|
+
id: z2.string().nullish(),
|
|
636
|
+
created: z2.number().nullish(),
|
|
637
|
+
model: z2.string().nullish(),
|
|
638
|
+
choices: z2.array(
|
|
639
|
+
z2.object({
|
|
640
|
+
delta: z2.object({
|
|
641
|
+
role: z2.enum(["assistant"]).nullish(),
|
|
642
|
+
content: z2.string().nullish(),
|
|
643
|
+
reasoning_content: z2.string().nullish(),
|
|
679
644
|
// Alibaba thinking mode delta
|
|
680
|
-
tool_calls:
|
|
681
|
-
|
|
682
|
-
index:
|
|
645
|
+
tool_calls: z2.array(
|
|
646
|
+
z2.object({
|
|
647
|
+
index: z2.number().nullish(),
|
|
683
648
|
// Index for accumulating tool calls
|
|
684
|
-
id:
|
|
685
|
-
type:
|
|
686
|
-
function:
|
|
687
|
-
name:
|
|
688
|
-
arguments:
|
|
649
|
+
id: z2.string().nullish(),
|
|
650
|
+
type: z2.literal("function").nullish(),
|
|
651
|
+
function: z2.object({
|
|
652
|
+
name: z2.string().nullish(),
|
|
653
|
+
arguments: z2.string().nullish()
|
|
689
654
|
}).nullish()
|
|
690
655
|
})
|
|
691
656
|
).nullish()
|
|
692
657
|
}),
|
|
693
|
-
finish_reason:
|
|
694
|
-
index:
|
|
658
|
+
finish_reason: z2.string().nullish(),
|
|
659
|
+
index: z2.number()
|
|
695
660
|
})
|
|
696
661
|
),
|
|
697
662
|
usage: alibabaUsageSchema.nullish()
|
|
@@ -699,60 +664,74 @@ var alibabaChatChunkSchema = import_v42.z.object({
|
|
|
699
664
|
});
|
|
700
665
|
|
|
701
666
|
// src/alibaba-video-model.ts
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
667
|
+
import {
|
|
668
|
+
AISDKError
|
|
669
|
+
} from "@ai-sdk/provider";
|
|
670
|
+
import {
|
|
671
|
+
combineHeaders as combineHeaders2,
|
|
672
|
+
convertUint8ArrayToBase64,
|
|
673
|
+
createJsonErrorResponseHandler,
|
|
674
|
+
createJsonResponseHandler as createJsonResponseHandler2,
|
|
675
|
+
delay,
|
|
676
|
+
getFromApi,
|
|
677
|
+
lazySchema,
|
|
678
|
+
parseProviderOptions as parseProviderOptions2,
|
|
679
|
+
postJsonToApi as postJsonToApi2,
|
|
680
|
+
resolve,
|
|
681
|
+
zodSchema
|
|
682
|
+
} from "@ai-sdk/provider-utils";
|
|
683
|
+
import { z as z3 } from "zod/v4";
|
|
684
|
+
var alibabaVideoModelOptionsSchema = lazySchema(
|
|
685
|
+
() => zodSchema(
|
|
686
|
+
z3.object({
|
|
687
|
+
negativePrompt: z3.string().nullish(),
|
|
688
|
+
audioUrl: z3.string().nullish(),
|
|
689
|
+
promptExtend: z3.boolean().nullish(),
|
|
690
|
+
shotType: z3.enum(["single", "multi"]).nullish(),
|
|
691
|
+
watermark: z3.boolean().nullish(),
|
|
692
|
+
audio: z3.boolean().nullish(),
|
|
693
|
+
referenceUrls: z3.array(z3.string()).nullish(),
|
|
694
|
+
pollIntervalMs: z3.number().positive().nullish(),
|
|
695
|
+
pollTimeoutMs: z3.number().positive().nullish()
|
|
717
696
|
}).passthrough()
|
|
718
697
|
)
|
|
719
698
|
);
|
|
720
|
-
var alibabaVideoErrorSchema =
|
|
721
|
-
code:
|
|
722
|
-
message:
|
|
723
|
-
request_id:
|
|
699
|
+
var alibabaVideoErrorSchema = z3.object({
|
|
700
|
+
code: z3.string().nullish(),
|
|
701
|
+
message: z3.string(),
|
|
702
|
+
request_id: z3.string().nullish()
|
|
724
703
|
});
|
|
725
|
-
var alibabaVideoFailedResponseHandler =
|
|
704
|
+
var alibabaVideoFailedResponseHandler = createJsonErrorResponseHandler({
|
|
726
705
|
errorSchema: alibabaVideoErrorSchema,
|
|
727
706
|
errorToMessage: (data) => data.message
|
|
728
707
|
});
|
|
729
|
-
var alibabaVideoCreateTaskSchema =
|
|
730
|
-
output:
|
|
731
|
-
task_status:
|
|
732
|
-
task_id:
|
|
708
|
+
var alibabaVideoCreateTaskSchema = z3.object({
|
|
709
|
+
output: z3.object({
|
|
710
|
+
task_status: z3.string(),
|
|
711
|
+
task_id: z3.string()
|
|
733
712
|
}).nullish(),
|
|
734
|
-
request_id:
|
|
713
|
+
request_id: z3.string().nullish()
|
|
735
714
|
});
|
|
736
|
-
var alibabaVideoTaskStatusSchema =
|
|
737
|
-
output:
|
|
738
|
-
task_id:
|
|
739
|
-
task_status:
|
|
740
|
-
video_url:
|
|
741
|
-
submit_time:
|
|
742
|
-
scheduled_time:
|
|
743
|
-
end_time:
|
|
744
|
-
orig_prompt:
|
|
745
|
-
actual_prompt:
|
|
746
|
-
code:
|
|
747
|
-
message:
|
|
715
|
+
var alibabaVideoTaskStatusSchema = z3.object({
|
|
716
|
+
output: z3.object({
|
|
717
|
+
task_id: z3.string(),
|
|
718
|
+
task_status: z3.string(),
|
|
719
|
+
video_url: z3.string().nullish(),
|
|
720
|
+
submit_time: z3.string().nullish(),
|
|
721
|
+
scheduled_time: z3.string().nullish(),
|
|
722
|
+
end_time: z3.string().nullish(),
|
|
723
|
+
orig_prompt: z3.string().nullish(),
|
|
724
|
+
actual_prompt: z3.string().nullish(),
|
|
725
|
+
code: z3.string().nullish(),
|
|
726
|
+
message: z3.string().nullish()
|
|
748
727
|
}).nullish(),
|
|
749
|
-
usage:
|
|
750
|
-
duration:
|
|
751
|
-
output_video_duration:
|
|
752
|
-
SR:
|
|
753
|
-
size:
|
|
728
|
+
usage: z3.object({
|
|
729
|
+
duration: z3.number().nullish(),
|
|
730
|
+
output_video_duration: z3.number().nullish(),
|
|
731
|
+
SR: z3.number().nullish(),
|
|
732
|
+
size: z3.string().nullish()
|
|
754
733
|
}).nullish(),
|
|
755
|
-
request_id:
|
|
734
|
+
request_id: z3.string().nullish()
|
|
756
735
|
});
|
|
757
736
|
function detectMode(modelId) {
|
|
758
737
|
if (modelId.includes("-i2v")) return "i2v";
|
|
@@ -774,7 +753,7 @@ var AlibabaVideoModel = class {
|
|
|
774
753
|
const currentDate = (_c = (_b = (_a = this.config._internal) == null ? void 0 : _a.currentDate) == null ? void 0 : _b.call(_a)) != null ? _c : /* @__PURE__ */ new Date();
|
|
775
754
|
const warnings = [];
|
|
776
755
|
const mode = detectMode(this.modelId);
|
|
777
|
-
const alibabaOptions = await (
|
|
756
|
+
const alibabaOptions = await parseProviderOptions2({
|
|
778
757
|
provider: "alibaba",
|
|
779
758
|
providerOptions: options.providerOptions,
|
|
780
759
|
schema: alibabaVideoModelOptionsSchema
|
|
@@ -793,7 +772,7 @@ var AlibabaVideoModel = class {
|
|
|
793
772
|
if (options.image.type === "url") {
|
|
794
773
|
input.img_url = options.image.url;
|
|
795
774
|
} else {
|
|
796
|
-
const base64Data = typeof options.image.data === "string" ? options.image.data :
|
|
775
|
+
const base64Data = typeof options.image.data === "string" ? options.image.data : convertUint8ArrayToBase64(options.image.data);
|
|
797
776
|
input.img_url = base64Data;
|
|
798
777
|
}
|
|
799
778
|
}
|
|
@@ -862,10 +841,10 @@ var AlibabaVideoModel = class {
|
|
|
862
841
|
details: "Alibaba video models only support generating 1 video per call."
|
|
863
842
|
});
|
|
864
843
|
}
|
|
865
|
-
const { value: createResponse } = await (
|
|
844
|
+
const { value: createResponse } = await postJsonToApi2({
|
|
866
845
|
url: `${this.config.baseURL}/api/v1/services/aigc/video-generation/video-synthesis`,
|
|
867
|
-
headers: (
|
|
868
|
-
await
|
|
846
|
+
headers: combineHeaders2(
|
|
847
|
+
await resolve(this.config.headers),
|
|
869
848
|
options.headers,
|
|
870
849
|
{
|
|
871
850
|
"X-DashScope-Async": "enable"
|
|
@@ -876,7 +855,7 @@ var AlibabaVideoModel = class {
|
|
|
876
855
|
input,
|
|
877
856
|
parameters
|
|
878
857
|
},
|
|
879
|
-
successfulResponseHandler: (
|
|
858
|
+
successfulResponseHandler: createJsonResponseHandler2(
|
|
880
859
|
alibabaVideoCreateTaskSchema
|
|
881
860
|
),
|
|
882
861
|
failedResponseHandler: alibabaVideoFailedResponseHandler,
|
|
@@ -885,7 +864,7 @@ var AlibabaVideoModel = class {
|
|
|
885
864
|
});
|
|
886
865
|
const taskId = (_d = createResponse.output) == null ? void 0 : _d.task_id;
|
|
887
866
|
if (!taskId) {
|
|
888
|
-
throw new
|
|
867
|
+
throw new AISDKError({
|
|
889
868
|
name: "ALIBABA_VIDEO_GENERATION_ERROR",
|
|
890
869
|
message: `No task_id returned from Alibaba API. Response: ${JSON.stringify(createResponse)}`
|
|
891
870
|
});
|
|
@@ -896,20 +875,20 @@ var AlibabaVideoModel = class {
|
|
|
896
875
|
let finalResponse;
|
|
897
876
|
let responseHeaders;
|
|
898
877
|
while (true) {
|
|
899
|
-
await
|
|
878
|
+
await delay(pollIntervalMs, { abortSignal: options.abortSignal });
|
|
900
879
|
if (Date.now() - startTime > pollTimeoutMs) {
|
|
901
|
-
throw new
|
|
880
|
+
throw new AISDKError({
|
|
902
881
|
name: "ALIBABA_VIDEO_GENERATION_TIMEOUT",
|
|
903
882
|
message: `Video generation timed out after ${pollTimeoutMs}ms`
|
|
904
883
|
});
|
|
905
884
|
}
|
|
906
|
-
const { value: statusResponse, responseHeaders: pollHeaders } = await
|
|
885
|
+
const { value: statusResponse, responseHeaders: pollHeaders } = await getFromApi({
|
|
907
886
|
url: `${this.config.baseURL}/api/v1/tasks/${taskId}`,
|
|
908
|
-
headers: (
|
|
909
|
-
await
|
|
887
|
+
headers: combineHeaders2(
|
|
888
|
+
await resolve(this.config.headers),
|
|
910
889
|
options.headers
|
|
911
890
|
),
|
|
912
|
-
successfulResponseHandler: (
|
|
891
|
+
successfulResponseHandler: createJsonResponseHandler2(
|
|
913
892
|
alibabaVideoTaskStatusSchema
|
|
914
893
|
),
|
|
915
894
|
failedResponseHandler: alibabaVideoFailedResponseHandler,
|
|
@@ -923,7 +902,7 @@ var AlibabaVideoModel = class {
|
|
|
923
902
|
break;
|
|
924
903
|
}
|
|
925
904
|
if (taskStatus === "FAILED" || taskStatus === "CANCELED") {
|
|
926
|
-
throw new
|
|
905
|
+
throw new AISDKError({
|
|
927
906
|
name: "ALIBABA_VIDEO_GENERATION_FAILED",
|
|
928
907
|
message: `Video generation ${taskStatus.toLowerCase()}. Task ID: ${taskId}. ${(_i = (_h = statusResponse.output) == null ? void 0 : _h.message) != null ? _i : ""}`
|
|
929
908
|
});
|
|
@@ -931,7 +910,7 @@ var AlibabaVideoModel = class {
|
|
|
931
910
|
}
|
|
932
911
|
const videoUrl = (_j = finalResponse == null ? void 0 : finalResponse.output) == null ? void 0 : _j.video_url;
|
|
933
912
|
if (!videoUrl) {
|
|
934
|
-
throw new
|
|
913
|
+
throw new AISDKError({
|
|
935
914
|
name: "ALIBABA_VIDEO_GENERATION_ERROR",
|
|
936
915
|
message: `No video URL in response. Task ID: ${taskId}`
|
|
937
916
|
});
|
|
@@ -970,27 +949,27 @@ var AlibabaVideoModel = class {
|
|
|
970
949
|
};
|
|
971
950
|
|
|
972
951
|
// src/version.ts
|
|
973
|
-
var VERSION = "2.0.0-beta.
|
|
952
|
+
var VERSION = "2.0.0-beta.31";
|
|
974
953
|
|
|
975
954
|
// src/alibaba-provider.ts
|
|
976
|
-
var alibabaErrorDataSchema =
|
|
977
|
-
error:
|
|
978
|
-
message:
|
|
979
|
-
code:
|
|
980
|
-
type:
|
|
955
|
+
var alibabaErrorDataSchema = z4.object({
|
|
956
|
+
error: z4.object({
|
|
957
|
+
message: z4.string(),
|
|
958
|
+
code: z4.string().nullish(),
|
|
959
|
+
type: z4.string().nullish()
|
|
981
960
|
})
|
|
982
961
|
});
|
|
983
|
-
var alibabaFailedResponseHandler = (
|
|
962
|
+
var alibabaFailedResponseHandler = createJsonErrorResponseHandler2({
|
|
984
963
|
errorSchema: alibabaErrorDataSchema,
|
|
985
964
|
errorToMessage: (data) => data.error.message
|
|
986
965
|
});
|
|
987
966
|
function createAlibaba(options = {}) {
|
|
988
967
|
var _a, _b;
|
|
989
|
-
const baseURL = (_a =
|
|
990
|
-
const videoBaseURL = (_b =
|
|
991
|
-
const getHeaders = () =>
|
|
968
|
+
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://dashscope-intl.aliyuncs.com/compatible-mode/v1";
|
|
969
|
+
const videoBaseURL = (_b = withoutTrailingSlash(options.videoBaseURL)) != null ? _b : "https://dashscope-intl.aliyuncs.com";
|
|
970
|
+
const getHeaders = () => withUserAgentSuffix(
|
|
992
971
|
{
|
|
993
|
-
Authorization: `Bearer ${
|
|
972
|
+
Authorization: `Bearer ${loadApiKey({
|
|
994
973
|
apiKey: options.apiKey,
|
|
995
974
|
environmentVariableName: "ALIBABA_API_KEY",
|
|
996
975
|
description: "Alibaba Cloud (DashScope)"
|
|
@@ -1029,18 +1008,17 @@ function createAlibaba(options = {}) {
|
|
|
1029
1008
|
provider.video = createVideoModel;
|
|
1030
1009
|
provider.videoModel = createVideoModel;
|
|
1031
1010
|
provider.imageModel = (modelId) => {
|
|
1032
|
-
throw new
|
|
1011
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
1033
1012
|
};
|
|
1034
1013
|
provider.embeddingModel = (modelId) => {
|
|
1035
|
-
throw new
|
|
1014
|
+
throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
|
|
1036
1015
|
};
|
|
1037
1016
|
return provider;
|
|
1038
1017
|
}
|
|
1039
1018
|
var alibaba = createAlibaba();
|
|
1040
|
-
|
|
1041
|
-
0 && (module.exports = {
|
|
1019
|
+
export {
|
|
1042
1020
|
VERSION,
|
|
1043
1021
|
alibaba,
|
|
1044
1022
|
createAlibaba
|
|
1045
|
-
}
|
|
1023
|
+
};
|
|
1046
1024
|
//# sourceMappingURL=index.js.map
|