@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.
@@ -4,7 +4,6 @@ import {
4
4
  prepareTools,
5
5
  } from '@ai-sdk/openai-compatible/internal';
6
6
  import {
7
- InvalidResponseDataError,
8
7
  type LanguageModelV4,
9
8
  type LanguageModelV4CallOptions,
10
9
  type LanguageModelV4Content,
@@ -19,10 +18,16 @@ import {
19
18
  createEventSourceResponseHandler,
20
19
  createJsonResponseHandler,
21
20
  generateId,
22
- isParsableJson,
21
+ type InferSchema,
22
+ isCustomReasoning,
23
+ mapReasoningToProviderBudget,
23
24
  parseProviderOptions,
24
25
  postJsonToApi,
25
26
  type ParseResult,
27
+ serializeModelOptions,
28
+ StreamingToolCallTracker,
29
+ WORKFLOW_SERIALIZE,
30
+ WORKFLOW_DESERIALIZE,
26
31
  } from '@ai-sdk/provider-utils';
27
32
  import { z } from 'zod/v4';
28
33
  import {
@@ -50,6 +55,20 @@ export class AlibabaLanguageModel implements LanguageModelV4 {
50
55
 
51
56
  private readonly config: AlibabaConfig;
52
57
 
58
+ static [WORKFLOW_SERIALIZE](model: AlibabaLanguageModel) {
59
+ return serializeModelOptions({
60
+ modelId: model.modelId,
61
+ config: model.config,
62
+ });
63
+ }
64
+
65
+ static [WORKFLOW_DESERIALIZE](options: {
66
+ modelId: AlibabaChatModelId;
67
+ config: AlibabaConfig;
68
+ }) {
69
+ return new AlibabaLanguageModel(options.modelId, options.config);
70
+ }
71
+
53
72
  constructor(modelId: AlibabaChatModelId, config: AlibabaConfig) {
54
73
  this.modelId = modelId;
55
74
  this.config = config;
@@ -78,6 +97,7 @@ export class AlibabaLanguageModel implements LanguageModelV4 {
78
97
  stopSequences,
79
98
  responseFormat,
80
99
  seed,
100
+ reasoning,
81
101
  providerOptions,
82
102
  tools,
83
103
  toolChoice,
@@ -121,13 +141,11 @@ export class AlibabaLanguageModel implements LanguageModelV4 {
121
141
  : { type: 'json_object' }
122
142
  : undefined,
123
143
 
124
- // Alibaba-specific options
125
- ...(alibabaOptions?.enableThinking != null
126
- ? { enable_thinking: alibabaOptions.enableThinking }
127
- : {}),
128
- ...(alibabaOptions?.thinkingBudget != null
129
- ? { thinking_budget: alibabaOptions.thinkingBudget }
130
- : {}),
144
+ ...resolveAlibabaThinking({
145
+ reasoning,
146
+ alibabaOptions,
147
+ warnings,
148
+ }),
131
149
 
132
150
  // Convert messages with cache control support
133
151
  messages: convertToAlibabaChatMessages({
@@ -170,7 +188,7 @@ export class AlibabaLanguageModel implements LanguageModelV4 {
170
188
  rawValue: rawResponse,
171
189
  } = await postJsonToApi({
172
190
  url: `${this.config.baseURL}/chat/completions`,
173
- headers: combineHeaders(this.config.headers(), options.headers),
191
+ headers: combineHeaders(this.config.headers?.(), options.headers),
174
192
  body: args,
175
193
  failedResponseHandler: alibabaFailedResponseHandler,
176
194
  successfulResponseHandler: createJsonResponseHandler(
@@ -203,7 +221,7 @@ export class AlibabaLanguageModel implements LanguageModelV4 {
203
221
  for (const toolCall of choice.message.tool_calls) {
204
222
  content.push({
205
223
  type: 'tool-call',
206
- toolCallId: toolCall.id,
224
+ toolCallId: toolCall.id ?? generateId(),
207
225
  toolName: toolCall.function.name,
208
226
  input: toolCall.function.arguments!,
209
227
  });
@@ -241,7 +259,7 @@ export class AlibabaLanguageModel implements LanguageModelV4 {
241
259
 
242
260
  const { responseHeaders, value: response } = await postJsonToApi({
243
261
  url: `${this.config.baseURL}/chat/completions`,
244
- headers: combineHeaders(this.config.headers(), options.headers),
262
+ headers: combineHeaders(this.config.headers?.(), options.headers),
245
263
  body,
246
264
  failedResponseHandler: alibabaFailedResponseHandler,
247
265
  successfulResponseHandler: createEventSourceResponseHandler(
@@ -262,13 +280,7 @@ export class AlibabaLanguageModel implements LanguageModelV4 {
262
280
  let activeText = false;
263
281
  let activeReasoningId: string | null = null;
264
282
 
265
- // Track tool calls for accumulation across chunks
266
- const toolCalls: Array<{
267
- id: string;
268
- type: 'function';
269
- function: { name: string; arguments: string };
270
- hasFinished: boolean;
271
- }> = [];
283
+ const toolCallTracker = new StreamingToolCallTracker({ generateId });
272
284
 
273
285
  return {
274
286
  stream: response.pipeThrough(
@@ -381,106 +393,10 @@ export class AlibabaLanguageModel implements LanguageModelV4 {
381
393
  }
382
394
 
383
395
  for (const toolCallDelta of delta.tool_calls) {
384
- const index = toolCallDelta.index ?? toolCalls.length;
385
-
386
- // New tool call - first chunk with id and name
387
- if (toolCalls[index] == null) {
388
- if (toolCallDelta.id == null) {
389
- throw new InvalidResponseDataError({
390
- data: toolCallDelta,
391
- message: `Expected 'id' to be a string.`,
392
- });
393
- }
394
-
395
- if (toolCallDelta.function?.name == null) {
396
- throw new InvalidResponseDataError({
397
- data: toolCallDelta,
398
- message: `Expected 'function.name' to be a string.`,
399
- });
400
- }
401
-
402
- controller.enqueue({
403
- type: 'tool-input-start',
404
- id: toolCallDelta.id,
405
- toolName: toolCallDelta.function.name,
406
- });
407
-
408
- toolCalls[index] = {
409
- id: toolCallDelta.id,
410
- type: 'function',
411
- function: {
412
- name: toolCallDelta.function.name,
413
- arguments: toolCallDelta.function.arguments ?? '',
414
- },
415
- hasFinished: false,
416
- };
417
-
418
- const toolCall = toolCalls[index];
419
-
420
- // Send initial delta if arguments started
421
- if (toolCall.function.arguments.length > 0) {
422
- controller.enqueue({
423
- type: 'tool-input-delta',
424
- id: toolCall.id,
425
- delta: toolCall.function.arguments,
426
- });
427
- }
428
-
429
- // Check if already complete (some providers send full tool call at once)
430
- if (isParsableJson(toolCall.function.arguments)) {
431
- controller.enqueue({
432
- type: 'tool-input-end',
433
- id: toolCall.id,
434
- });
435
-
436
- controller.enqueue({
437
- type: 'tool-call',
438
- toolCallId: toolCall.id,
439
- toolName: toolCall.function.name,
440
- input: toolCall.function.arguments,
441
- });
442
-
443
- toolCall.hasFinished = true;
444
- }
445
-
446
- continue;
447
- }
448
-
449
- // Existing tool call - accumulate arguments
450
- const toolCall = toolCalls[index];
451
-
452
- if (toolCall.hasFinished) {
453
- continue;
454
- }
455
-
456
- // Append arguments if not null (skip arguments: null chunks)
457
- if (toolCallDelta.function?.arguments != null) {
458
- toolCall.function.arguments +=
459
- toolCallDelta.function.arguments;
460
-
461
- controller.enqueue({
462
- type: 'tool-input-delta',
463
- id: toolCall.id,
464
- delta: toolCallDelta.function.arguments,
465
- });
466
- }
467
-
468
- // Check if tool call is now complete
469
- if (isParsableJson(toolCall.function.arguments)) {
470
- controller.enqueue({
471
- type: 'tool-input-end',
472
- id: toolCall.id,
473
- });
474
-
475
- controller.enqueue({
476
- type: 'tool-call',
477
- toolCallId: toolCall.id,
478
- toolName: toolCall.function.name,
479
- input: toolCall.function.arguments,
480
- });
481
-
482
- toolCall.hasFinished = true;
483
- }
396
+ toolCallTracker.processDelta(
397
+ toolCallDelta,
398
+ controller.enqueue.bind(controller),
399
+ );
484
400
  }
485
401
  }
486
402
 
@@ -505,6 +421,8 @@ export class AlibabaLanguageModel implements LanguageModelV4 {
505
421
  controller.enqueue({ type: 'text-end', id: '0' });
506
422
  }
507
423
 
424
+ toolCallTracker.flush(controller.enqueue.bind(controller));
425
+
508
426
  controller.enqueue({
509
427
  type: 'finish',
510
428
  finishReason,
@@ -519,6 +437,50 @@ export class AlibabaLanguageModel implements LanguageModelV4 {
519
437
  }
520
438
  }
521
439
 
440
+ function resolveAlibabaThinking({
441
+ reasoning,
442
+ alibabaOptions,
443
+ warnings,
444
+ }: {
445
+ reasoning: LanguageModelV4CallOptions['reasoning'];
446
+ alibabaOptions: InferSchema<typeof alibabaLanguageModelOptions> | undefined;
447
+ warnings: SharedV4Warning[];
448
+ }): { enable_thinking?: boolean; thinking_budget?: number } {
449
+ if (
450
+ alibabaOptions?.enableThinking != null ||
451
+ alibabaOptions?.thinkingBudget != null
452
+ ) {
453
+ return {
454
+ ...(alibabaOptions.enableThinking != null
455
+ ? { enable_thinking: alibabaOptions.enableThinking }
456
+ : {}),
457
+ ...(alibabaOptions.thinkingBudget != null
458
+ ? { thinking_budget: alibabaOptions.thinkingBudget }
459
+ : {}),
460
+ };
461
+ }
462
+
463
+ if (!isCustomReasoning(reasoning)) {
464
+ return {};
465
+ }
466
+
467
+ if (reasoning === 'none') {
468
+ return { enable_thinking: false };
469
+ }
470
+
471
+ const thinkingBudget = mapReasoningToProviderBudget({
472
+ reasoning,
473
+ maxOutputTokens: 16384,
474
+ maxReasoningBudget: 16384,
475
+ warnings,
476
+ });
477
+
478
+ return {
479
+ enable_thinking: true,
480
+ ...(thinkingBudget != null ? { thinking_budget: thinkingBudget } : {}),
481
+ };
482
+ }
483
+
522
484
  /**
523
485
  * Reference for schemas below:
524
486
  * https://www.alibabacloud.com/help/en/model-studio/qwen-api-via-openai-chat-completions
@@ -3,7 +3,7 @@ import type { FetchFunction } from '@ai-sdk/provider-utils';
3
3
  export interface AlibabaConfig {
4
4
  provider: string;
5
5
  baseURL: string;
6
- headers: () => Record<string, string | undefined>;
6
+ headers?: () => Record<string, string | undefined>;
7
7
  fetch?: FetchFunction;
8
8
  includeUsage?: boolean;
9
9
  }
@@ -3,7 +3,7 @@ import {
3
3
  type LanguageModelV4Prompt,
4
4
  UnsupportedFunctionalityError,
5
5
  } from '@ai-sdk/provider';
6
- import { convertToBase64 } from '@ai-sdk/provider-utils';
6
+ import { convertToBase64, isProviderReference } from '@ai-sdk/provider-utils';
7
7
  import type { AlibabaChatPrompt } from './alibaba-chat-prompt';
8
8
  import type { CacheControlValidator } from './get-cache-control';
9
9
 
@@ -53,26 +53,13 @@ export function convertToAlibabaChatMessages({
53
53
  }
54
54
 
55
55
  case 'user': {
56
- const isSinglePart = content.length === 1;
57
-
58
- if (
59
- isSinglePart &&
60
- content[0].type === 'text' &&
61
- !messageCacheControl
62
- ) {
63
- messages.push({
64
- role: 'user',
65
- content: content[0].text,
66
- });
67
- break;
68
- }
69
-
70
56
  messages.push({
71
57
  role: 'user',
72
- content: content.map(part => {
73
- const partCacheControl = isSinglePart
74
- ? messageCacheControl
75
- : cacheControlValidator?.getCacheControl(part.providerOptions);
58
+ content: content.map((part, index) => {
59
+ const isLastPart = index === content.length - 1;
60
+ const partCacheControl =
61
+ cacheControlValidator?.getCacheControl(part.providerOptions) ??
62
+ (isLastPart ? messageCacheControl : undefined);
76
63
 
77
64
  switch (part.type) {
78
65
  case 'text': {
@@ -86,6 +73,12 @@ export function convertToAlibabaChatMessages({
86
73
  }
87
74
 
88
75
  case 'file': {
76
+ if (isProviderReference(part.data)) {
77
+ throw new UnsupportedFunctionalityError({
78
+ functionality: 'file parts with provider references',
79
+ });
80
+ }
81
+
89
82
  if (part.mediaType.startsWith('image/')) {
90
83
  const mediaType =
91
84
  part.mediaType === 'image/*'
@@ -163,17 +156,15 @@ export function convertToAlibabaChatMessages({
163
156
  r => r.type !== 'tool-approval-response',
164
157
  );
165
158
 
166
- const isSinglePart = toolResponses.length === 1;
167
-
168
159
  for (let i = 0; i < toolResponses.length; i++) {
169
160
  const toolResponse = toolResponses[i];
170
161
  const output = toolResponse.output;
162
+ const isLastPart = i === toolResponses.length - 1;
171
163
 
172
- const partCacheControl = isSinglePart
173
- ? messageCacheControl
174
- : cacheControlValidator?.getCacheControl(
175
- (toolResponse as any).providerOptions,
176
- );
164
+ const partCacheControl =
165
+ cacheControlValidator?.getCacheControl(
166
+ toolResponse.providerOptions,
167
+ ) ?? (isLastPart ? messageCacheControl : undefined);
177
168
 
178
169
  let contentValue: string;
179
170
  switch (output.type) {
package/dist/index.d.mts DELETED
@@ -1,119 +0,0 @@
1
- import { z } from 'zod/v4';
2
- import { FetchFunction } from '@ai-sdk/provider-utils';
3
- import { ProviderV4, LanguageModelV4, Experimental_VideoModelV4 } from '@ai-sdk/provider';
4
-
5
- type AlibabaChatModelId = 'qwen3-max' | 'qwen3-max-preview' | 'qwen-plus' | 'qwen-plus-latest' | 'qwen-flash' | 'qwen-turbo' | 'qwen-turbo-latest' | 'qwen3-235b-a22b' | 'qwen3-32b' | 'qwen3-30b-a3b' | 'qwen3-14b' | 'qwen3-next-80b-a3b-thinking' | 'qwen3-235b-a22b-thinking-2507' | 'qwen3-30b-a3b-thinking-2507' | 'qwq-plus' | 'qwq-plus-latest' | 'qwq-32b' | 'qwen-coder' | 'qwen3-coder-plus' | 'qwen3-coder-flash' | (string & {});
6
- declare const alibabaLanguageModelOptions: z.ZodObject<{
7
- enableThinking: z.ZodOptional<z.ZodBoolean>;
8
- thinkingBudget: z.ZodOptional<z.ZodNumber>;
9
- parallelToolCalls: z.ZodOptional<z.ZodBoolean>;
10
- }, z.core.$strip>;
11
- type AlibabaLanguageModelOptions = z.infer<typeof alibabaLanguageModelOptions>;
12
-
13
- type AlibabaCacheControl = {
14
- type: string;
15
- };
16
-
17
- type AlibabaVideoModelId = 'wan2.6-t2v' | 'wan2.5-t2v-preview' | 'wan2.6-i2v' | 'wan2.6-i2v-flash' | 'wan2.6-r2v' | 'wan2.6-r2v-flash' | (string & {});
18
-
19
- interface AlibabaProvider extends ProviderV4 {
20
- (modelId: AlibabaChatModelId): LanguageModelV4;
21
- /**
22
- * Creates a model for text generation.
23
- */
24
- languageModel(modelId: AlibabaChatModelId): LanguageModelV4;
25
- /**
26
- * Creates a chat model for text generation.
27
- */
28
- chatModel(modelId: AlibabaChatModelId): LanguageModelV4;
29
- /**
30
- * Creates a model for video generation.
31
- */
32
- video(modelId: AlibabaVideoModelId): Experimental_VideoModelV4;
33
- /**
34
- * Creates a model for video generation.
35
- */
36
- videoModel(modelId: AlibabaVideoModelId): Experimental_VideoModelV4;
37
- }
38
- interface AlibabaProviderSettings {
39
- /**
40
- * Use a different URL prefix for API calls, e.g. to use proxy servers or regional endpoints.
41
- * The default prefix is `https://dashscope-intl.aliyuncs.com/compatible-mode/v1`.
42
- */
43
- baseURL?: string;
44
- /**
45
- * Use a different URL prefix for video generation API calls.
46
- * The video API uses the DashScope native endpoint (not the OpenAI-compatible endpoint).
47
- * The default prefix is `https://dashscope-intl.aliyuncs.com`.
48
- */
49
- videoBaseURL?: string;
50
- /**
51
- * API key that is being sent using the `Authorization` header.
52
- * It defaults to the `ALIBABA_API_KEY` environment variable.
53
- */
54
- apiKey?: string;
55
- /**
56
- * Custom headers to include in the requests.
57
- */
58
- headers?: Record<string, string>;
59
- /**
60
- * Custom fetch implementation. You can use it as a middleware to intercept requests,
61
- * or to provide a custom fetch implementation for e.g. testing.
62
- */
63
- fetch?: FetchFunction;
64
- /**
65
- * Include usage information in streaming responses.
66
- * When enabled, token usage will be included in the final chunk.
67
- *
68
- * @default true
69
- */
70
- includeUsage?: boolean;
71
- }
72
- /**
73
- * Create an Alibaba Cloud (Qwen) provider instance.
74
- */
75
- declare function createAlibaba(options?: AlibabaProviderSettings): AlibabaProvider;
76
- declare const alibaba: AlibabaProvider;
77
-
78
- type AlibabaVideoModelOptions = {
79
- /** Negative prompt to specify what to avoid (max 500 chars). */
80
- negativePrompt?: string | null;
81
- /** URL to audio file for audio-video sync (WAV/MP3, 3-30s, max 15MB). */
82
- audioUrl?: string | null;
83
- /** Enable prompt extension/rewriting for better generation. Defaults to true. */
84
- promptExtend?: boolean | null;
85
- /** Shot type: 'single' for single-shot or 'multi' for multi-shot narrative. */
86
- shotType?: 'single' | 'multi' | null;
87
- /** Whether to add watermark to generated video. Defaults to false. */
88
- watermark?: boolean | null;
89
- /** Enable audio generation (for I2V/R2V models). */
90
- audio?: boolean | null;
91
- /**
92
- * Reference URLs for reference-to-video mode.
93
- * Array of URLs to images (0-5) and/or videos (0-3), max 5 total.
94
- * Use character identifiers (character1, character2) in prompts to reference them.
95
- */
96
- referenceUrls?: string[] | null;
97
- /** Polling interval in milliseconds. Defaults to 5000 (5 seconds). */
98
- pollIntervalMs?: number | null;
99
- /** Maximum wait time in milliseconds for video generation. Defaults to 600000 (10 minutes). */
100
- pollTimeoutMs?: number | null;
101
- [key: string]: unknown;
102
- };
103
-
104
- type AlibabaUsage = {
105
- prompt_tokens?: number | null;
106
- completion_tokens?: number | null;
107
- total_tokens?: number | null;
108
- prompt_tokens_details?: {
109
- cached_tokens?: number | null;
110
- cache_creation_input_tokens?: number | null;
111
- } | null;
112
- completion_tokens_details?: {
113
- reasoning_tokens?: number | null;
114
- } | null;
115
- };
116
-
117
- declare const VERSION: string;
118
-
119
- export { type AlibabaCacheControl, type AlibabaChatModelId, type AlibabaLanguageModelOptions, type AlibabaProvider, type AlibabaLanguageModelOptions as AlibabaProviderOptions, type AlibabaProviderSettings, type AlibabaUsage, type AlibabaVideoModelId, type AlibabaVideoModelOptions, type AlibabaVideoModelOptions as AlibabaVideoProviderOptions, VERSION, alibaba, createAlibaba };