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