@ai-sdk/alibaba 1.0.53 → 1.0.55
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 +12 -0
- package/README.md +54 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +62 -10
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -10
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
- package/src/alibaba-chat-language-model.ts +14 -1
- package/src/alibaba-chat-options.ts +10 -0
- package/src/alibaba-chat-prompt.ts +1 -0
- package/src/convert-to-alibaba-chat-messages.ts +39 -6
- package/src/supports-preserved-thinking.ts +30 -0
package/dist/index.mjs
CHANGED
|
@@ -41,6 +41,15 @@ var alibabaLanguageModelOptions = z.object({
|
|
|
41
41
|
* Maximum number of reasoning tokens to generate.
|
|
42
42
|
*/
|
|
43
43
|
thinkingBudget: z.number().positive().optional(),
|
|
44
|
+
/**
|
|
45
|
+
* Whether to preserve reasoning from previous assistant messages.
|
|
46
|
+
*
|
|
47
|
+
* When enabled, historical reasoning is sent separately as
|
|
48
|
+
* `reasoning_content` and Alibaba receives `preserve_thinking: true`.
|
|
49
|
+
* Defaults to `true` for models that support preserved thinking; set to
|
|
50
|
+
* `false` to opt out.
|
|
51
|
+
*/
|
|
52
|
+
preserveThinking: z.boolean().optional(),
|
|
44
53
|
/**
|
|
45
54
|
* Whether to enable parallel function calling during tool use.
|
|
46
55
|
*
|
|
@@ -94,11 +103,21 @@ function formatImageUrl({
|
|
|
94
103
|
}
|
|
95
104
|
function convertToAlibabaChatMessages({
|
|
96
105
|
prompt,
|
|
97
|
-
cacheControlValidator
|
|
106
|
+
cacheControlValidator,
|
|
107
|
+
preserveThinking = false
|
|
98
108
|
}) {
|
|
99
109
|
var _a, _b;
|
|
100
110
|
const messages = [];
|
|
111
|
+
let lastUserMessageIndex = -1;
|
|
112
|
+
for (let i = prompt.length - 1; i >= 0; i--) {
|
|
113
|
+
if (prompt[i].role === "user") {
|
|
114
|
+
lastUserMessageIndex = i;
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
let index = -1;
|
|
101
119
|
for (const { role, content, ...message } of prompt) {
|
|
120
|
+
index++;
|
|
102
121
|
const messageCacheControl = cacheControlValidator == null ? void 0 : cacheControlValidator.getCacheControl(
|
|
103
122
|
message.providerOptions
|
|
104
123
|
);
|
|
@@ -123,9 +142,9 @@ function convertToAlibabaChatMessages({
|
|
|
123
142
|
case "user": {
|
|
124
143
|
messages.push({
|
|
125
144
|
role: "user",
|
|
126
|
-
content: content.map((part,
|
|
145
|
+
content: content.map((part, index2) => {
|
|
127
146
|
var _a2;
|
|
128
|
-
const isLastPart =
|
|
147
|
+
const isLastPart = index2 === content.length - 1;
|
|
129
148
|
const partCacheControl = (_a2 = cacheControlValidator == null ? void 0 : cacheControlValidator.getCacheControl(part.providerOptions)) != null ? _a2 : isLastPart ? messageCacheControl : void 0;
|
|
130
149
|
switch (part.type) {
|
|
131
150
|
case "text": {
|
|
@@ -158,6 +177,7 @@ function convertToAlibabaChatMessages({
|
|
|
158
177
|
}
|
|
159
178
|
case "assistant": {
|
|
160
179
|
let text = "";
|
|
180
|
+
let reasoningContent = "";
|
|
161
181
|
const toolCalls = [];
|
|
162
182
|
for (const part of content) {
|
|
163
183
|
switch (part.type) {
|
|
@@ -177,14 +197,20 @@ function convertToAlibabaChatMessages({
|
|
|
177
197
|
break;
|
|
178
198
|
}
|
|
179
199
|
case "reasoning": {
|
|
180
|
-
|
|
200
|
+
if (preserveThinking || index > lastUserMessageIndex) {
|
|
201
|
+
reasoningContent += part.text;
|
|
202
|
+
}
|
|
181
203
|
break;
|
|
182
204
|
}
|
|
183
205
|
}
|
|
184
206
|
}
|
|
207
|
+
if (text.length === 0 && toolCalls.length === 0 && reasoningContent.length === 0) {
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
185
210
|
messages.push({
|
|
186
211
|
role: "assistant",
|
|
187
|
-
content: messageCacheControl ? [{ type: "text", text, cache_control: messageCacheControl }] : text || null,
|
|
212
|
+
content: text.length === 0 && toolCalls.length === 0 && reasoningContent.length > 0 ? null : messageCacheControl ? [{ type: "text", text, cache_control: messageCacheControl }] : text || null,
|
|
213
|
+
...reasoningContent.length > 0 ? { reasoning_content: reasoningContent } : {},
|
|
188
214
|
tool_calls: toolCalls.length > 0 ? toolCalls : void 0
|
|
189
215
|
});
|
|
190
216
|
break;
|
|
@@ -270,6 +296,29 @@ var CacheControlValidator = class {
|
|
|
270
296
|
}
|
|
271
297
|
};
|
|
272
298
|
|
|
299
|
+
// src/supports-preserved-thinking.ts
|
|
300
|
+
var preservedThinkingModelIds = /* @__PURE__ */ new Set([
|
|
301
|
+
"kimi-k2.7-code",
|
|
302
|
+
"qwen3.6-max-preview",
|
|
303
|
+
"qwen3.6-plus",
|
|
304
|
+
"qwen3.6-plus-2026-04-02",
|
|
305
|
+
"qwen3.7-flash",
|
|
306
|
+
"qwen3.7-flash-2026-07-15",
|
|
307
|
+
"qwen3.7-max",
|
|
308
|
+
"qwen3.7-max-2026-05-17",
|
|
309
|
+
"qwen3.7-max-2026-05-20",
|
|
310
|
+
"qwen3.7-max-2026-06-08",
|
|
311
|
+
"qwen3.7-max-preview",
|
|
312
|
+
"qwen3.7-plus",
|
|
313
|
+
"qwen3.7-plus-2026-05-26",
|
|
314
|
+
"qwen3.8-flash",
|
|
315
|
+
"qwen3.8-max",
|
|
316
|
+
"qwen3.8-max-0902"
|
|
317
|
+
]);
|
|
318
|
+
function supportsPreservedThinking(modelId) {
|
|
319
|
+
return preservedThinkingModelIds.has(modelId);
|
|
320
|
+
}
|
|
321
|
+
|
|
273
322
|
// src/alibaba-chat-language-model.ts
|
|
274
323
|
var AlibabaLanguageModel = class {
|
|
275
324
|
constructor(modelId, config) {
|
|
@@ -302,7 +351,7 @@ var AlibabaLanguageModel = class {
|
|
|
302
351
|
tools,
|
|
303
352
|
toolChoice
|
|
304
353
|
}) {
|
|
305
|
-
var _a;
|
|
354
|
+
var _a, _b;
|
|
306
355
|
const warnings = [];
|
|
307
356
|
const cacheControlValidator = new CacheControlValidator();
|
|
308
357
|
const alibabaOptions = await parseProviderOptions({
|
|
@@ -313,6 +362,7 @@ var AlibabaLanguageModel = class {
|
|
|
313
362
|
if (frequencyPenalty != null) {
|
|
314
363
|
warnings.push({ type: "unsupported", feature: "frequencyPenalty" });
|
|
315
364
|
}
|
|
365
|
+
const preserveThinking = (_a = alibabaOptions == null ? void 0 : alibabaOptions.preserveThinking) != null ? _a : supportsPreservedThinking(this.modelId) ? true : void 0;
|
|
316
366
|
const baseArgs = {
|
|
317
367
|
model: this.modelId,
|
|
318
368
|
max_tokens: maxOutputTokens,
|
|
@@ -326,17 +376,19 @@ var AlibabaLanguageModel = class {
|
|
|
326
376
|
type: "json_schema",
|
|
327
377
|
json_schema: {
|
|
328
378
|
schema: responseFormat.schema,
|
|
329
|
-
name: (
|
|
379
|
+
name: (_b = responseFormat.name) != null ? _b : "response",
|
|
330
380
|
description: responseFormat.description
|
|
331
381
|
}
|
|
332
382
|
} : { type: "json_object" } : void 0,
|
|
333
383
|
// Alibaba-specific options
|
|
334
384
|
...(alibabaOptions == null ? void 0 : alibabaOptions.enableThinking) != null ? { enable_thinking: alibabaOptions.enableThinking } : {},
|
|
335
385
|
...(alibabaOptions == null ? void 0 : alibabaOptions.thinkingBudget) != null ? { thinking_budget: alibabaOptions.thinkingBudget } : {},
|
|
386
|
+
...preserveThinking != null ? { preserve_thinking: preserveThinking } : {},
|
|
336
387
|
// Convert messages with cache control support
|
|
337
388
|
messages: convertToAlibabaChatMessages({
|
|
338
389
|
prompt,
|
|
339
|
-
cacheControlValidator
|
|
390
|
+
cacheControlValidator,
|
|
391
|
+
preserveThinking: preserveThinking != null ? preserveThinking : false
|
|
340
392
|
})
|
|
341
393
|
};
|
|
342
394
|
const {
|
|
@@ -506,7 +558,7 @@ var AlibabaLanguageModel = class {
|
|
|
506
558
|
delta: delta.content
|
|
507
559
|
});
|
|
508
560
|
}
|
|
509
|
-
if (delta.tool_calls != null) {
|
|
561
|
+
if (delta.tool_calls != null && delta.tool_calls.length > 0) {
|
|
510
562
|
if (activeReasoningId != null) {
|
|
511
563
|
controller.enqueue({
|
|
512
564
|
type: "reasoning-end",
|
|
@@ -1329,7 +1381,7 @@ var AlibabaVideoModel = class {
|
|
|
1329
1381
|
};
|
|
1330
1382
|
|
|
1331
1383
|
// src/version.ts
|
|
1332
|
-
var VERSION = true ? "1.0.
|
|
1384
|
+
var VERSION = true ? "1.0.55" : "0.0.0-test";
|
|
1333
1385
|
|
|
1334
1386
|
// src/alibaba-provider.ts
|
|
1335
1387
|
function createAlibaba(options = {}) {
|