@ai-sdk/deepseek 2.0.0-beta.42 → 2.0.0-beta.43

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 CHANGED
@@ -27,75 +27,725 @@ __export(src_exports, {
27
27
  module.exports = __toCommonJS(src_exports);
28
28
 
29
29
  // src/deepseek-provider.ts
30
- var import_openai_compatible = require("@ai-sdk/openai-compatible");
31
- var import_provider = require("@ai-sdk/provider");
30
+ var import_provider3 = require("@ai-sdk/provider");
31
+ var import_provider_utils3 = require("@ai-sdk/provider-utils");
32
+
33
+ // src/chat/deepseek-chat-language-model.ts
34
+ var import_provider2 = require("@ai-sdk/provider");
32
35
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
33
36
 
34
- // src/deepseek-metadata-extractor.ts
37
+ // src/chat/convert-to-deepseek-chat-messages.ts
38
+ function convertToDeepSeekChatMessages({
39
+ prompt,
40
+ responseFormat
41
+ }) {
42
+ var _a;
43
+ const messages = [];
44
+ const warnings = [];
45
+ if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
46
+ if (responseFormat.schema == null) {
47
+ messages.push({
48
+ role: "system",
49
+ content: "Return JSON."
50
+ });
51
+ } else {
52
+ messages.push({
53
+ role: "system",
54
+ content: "Return JSON that conforms to the following schema: " + JSON.stringify(responseFormat.schema)
55
+ });
56
+ warnings.push({
57
+ type: "compatibility",
58
+ feature: "responseFormat JSON schema",
59
+ details: "JSON response schema is injected into the system message."
60
+ });
61
+ }
62
+ }
63
+ let lastUserMessageIndex = -1;
64
+ for (let i = prompt.length - 1; i >= 0; i--) {
65
+ if (prompt[i].role === "user") {
66
+ lastUserMessageIndex = i;
67
+ break;
68
+ }
69
+ }
70
+ let index = -1;
71
+ for (const { role, content } of prompt) {
72
+ index++;
73
+ switch (role) {
74
+ case "system": {
75
+ messages.push({ role: "system", content });
76
+ break;
77
+ }
78
+ case "user": {
79
+ let userContent = "";
80
+ for (const part of content) {
81
+ if (part.type === "text") {
82
+ userContent += part.text;
83
+ } else {
84
+ warnings.push({
85
+ type: "unsupported",
86
+ feature: `user message part type: ${part.type}`
87
+ });
88
+ }
89
+ }
90
+ messages.push({
91
+ role: "user",
92
+ content: userContent
93
+ });
94
+ break;
95
+ }
96
+ case "assistant": {
97
+ let text = "";
98
+ let reasoning;
99
+ const toolCalls = [];
100
+ for (const part of content) {
101
+ switch (part.type) {
102
+ case "text": {
103
+ text += part.text;
104
+ break;
105
+ }
106
+ case "reasoning": {
107
+ if (index <= lastUserMessageIndex) {
108
+ break;
109
+ }
110
+ if (reasoning == null) {
111
+ reasoning = part.text;
112
+ } else {
113
+ reasoning += part.text;
114
+ }
115
+ break;
116
+ }
117
+ case "tool-call": {
118
+ toolCalls.push({
119
+ id: part.toolCallId,
120
+ type: "function",
121
+ function: {
122
+ name: part.toolName,
123
+ arguments: JSON.stringify(part.input)
124
+ }
125
+ });
126
+ break;
127
+ }
128
+ }
129
+ }
130
+ messages.push({
131
+ role: "assistant",
132
+ content: text,
133
+ reasoning_content: reasoning,
134
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0
135
+ });
136
+ break;
137
+ }
138
+ case "tool": {
139
+ for (const toolResponse of content) {
140
+ const output = toolResponse.output;
141
+ let contentValue;
142
+ switch (output.type) {
143
+ case "text":
144
+ case "error-text":
145
+ contentValue = output.value;
146
+ break;
147
+ case "execution-denied":
148
+ contentValue = (_a = output.reason) != null ? _a : "Tool execution denied.";
149
+ break;
150
+ case "content":
151
+ case "json":
152
+ case "error-json":
153
+ contentValue = JSON.stringify(output.value);
154
+ break;
155
+ }
156
+ messages.push({
157
+ role: "tool",
158
+ tool_call_id: toolResponse.toolCallId,
159
+ content: contentValue
160
+ });
161
+ }
162
+ break;
163
+ }
164
+ default: {
165
+ warnings.push({
166
+ type: "unsupported",
167
+ feature: `message role: ${role}`
168
+ });
169
+ break;
170
+ }
171
+ }
172
+ }
173
+ return { messages, warnings };
174
+ }
175
+
176
+ // src/chat/deepseek-chat-api-types.ts
35
177
  var import_provider_utils = require("@ai-sdk/provider-utils");
36
178
  var import_v4 = require("zod/v4");
37
- var buildDeepseekMetadata = (usage) => {
38
- var _a, _b;
39
- return usage == null ? void 0 : {
40
- deepseek: {
41
- promptCacheHitTokens: (_a = usage.prompt_cache_hit_tokens) != null ? _a : NaN,
42
- promptCacheMissTokens: (_b = usage.prompt_cache_miss_tokens) != null ? _b : NaN
179
+ var tokenUsageSchema = import_v4.z.object({
180
+ prompt_tokens: import_v4.z.number().nullish(),
181
+ completion_tokens: import_v4.z.number().nullish(),
182
+ prompt_cache_hit_tokens: import_v4.z.number().nullish(),
183
+ prompt_cache_miss_tokens: import_v4.z.number().nullish(),
184
+ total_tokens: import_v4.z.number().nullish(),
185
+ completion_tokens_details: import_v4.z.object({
186
+ reasoning_tokens: import_v4.z.number().nullish()
187
+ }).nullish()
188
+ }).nullish();
189
+ var deepSeekErrorSchema = import_v4.z.object({
190
+ error: import_v4.z.object({
191
+ message: import_v4.z.string(),
192
+ type: import_v4.z.string().nullish(),
193
+ param: import_v4.z.any().nullish(),
194
+ code: import_v4.z.union([import_v4.z.string(), import_v4.z.number()]).nullish()
195
+ })
196
+ });
197
+ var deepseekChatResponseSchema = import_v4.z.object({
198
+ id: import_v4.z.string().nullish(),
199
+ created: import_v4.z.number().nullish(),
200
+ model: import_v4.z.string().nullish(),
201
+ choices: import_v4.z.array(
202
+ import_v4.z.object({
203
+ message: import_v4.z.object({
204
+ role: import_v4.z.literal("assistant").nullish(),
205
+ content: import_v4.z.string().nullish(),
206
+ reasoning_content: import_v4.z.string().nullish(),
207
+ tool_calls: import_v4.z.array(
208
+ import_v4.z.object({
209
+ id: import_v4.z.string().nullish(),
210
+ function: import_v4.z.object({
211
+ name: import_v4.z.string(),
212
+ arguments: import_v4.z.string()
213
+ })
214
+ })
215
+ ).nullish()
216
+ }),
217
+ finish_reason: import_v4.z.string().nullish()
218
+ })
219
+ ),
220
+ usage: tokenUsageSchema
221
+ });
222
+ var deepseekChatChunkSchema = (0, import_provider_utils.lazySchema)(
223
+ () => (0, import_provider_utils.zodSchema)(
224
+ import_v4.z.union([
225
+ import_v4.z.object({
226
+ id: import_v4.z.string().nullish(),
227
+ created: import_v4.z.number().nullish(),
228
+ model: import_v4.z.string().nullish(),
229
+ choices: import_v4.z.array(
230
+ import_v4.z.object({
231
+ delta: import_v4.z.object({
232
+ role: import_v4.z.enum(["assistant"]).nullish(),
233
+ content: import_v4.z.string().nullish(),
234
+ reasoning_content: import_v4.z.string().nullish(),
235
+ tool_calls: import_v4.z.array(
236
+ import_v4.z.object({
237
+ index: import_v4.z.number(),
238
+ id: import_v4.z.string().nullish(),
239
+ function: import_v4.z.object({
240
+ name: import_v4.z.string().nullish(),
241
+ arguments: import_v4.z.string().nullish()
242
+ })
243
+ })
244
+ ).nullish()
245
+ }).nullish(),
246
+ finish_reason: import_v4.z.string().nullish()
247
+ })
248
+ ),
249
+ usage: tokenUsageSchema
250
+ }),
251
+ deepSeekErrorSchema
252
+ ])
253
+ )
254
+ );
255
+
256
+ // src/chat/deepseek-chat-options.ts
257
+ var import_v42 = require("zod/v4");
258
+ var deepseekChatOptions = import_v42.z.object({
259
+ /**
260
+ * Type of thinking to use. Defaults to `enabled`.
261
+ */
262
+ thinking: import_v42.z.object({
263
+ type: import_v42.z.enum(["enabled", "disabled"]).optional()
264
+ }).optional()
265
+ });
266
+
267
+ // src/chat/deepseek-prepare-tools.ts
268
+ var import_provider = require("@ai-sdk/provider");
269
+ function prepareTools({
270
+ tools,
271
+ toolChoice
272
+ }) {
273
+ tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
274
+ const toolWarnings = [];
275
+ if (tools == null) {
276
+ return { tools: void 0, toolChoice: void 0, toolWarnings };
277
+ }
278
+ const deepseekTools = [];
279
+ for (const tool of tools) {
280
+ if (tool.type === "provider") {
281
+ toolWarnings.push({
282
+ type: "unsupported",
283
+ feature: `provider-defined tool ${tool.id}`
284
+ });
285
+ } else {
286
+ deepseekTools.push({
287
+ type: "function",
288
+ function: {
289
+ name: tool.name,
290
+ description: tool.description,
291
+ parameters: tool.inputSchema
292
+ }
293
+ });
43
294
  }
295
+ }
296
+ if (toolChoice == null) {
297
+ return { tools: deepseekTools, toolChoice: void 0, toolWarnings };
298
+ }
299
+ const type = toolChoice.type;
300
+ switch (type) {
301
+ case "auto":
302
+ case "none":
303
+ case "required":
304
+ return { tools: deepseekTools, toolChoice: type, toolWarnings };
305
+ case "tool":
306
+ return {
307
+ tools: deepseekTools,
308
+ toolChoice: {
309
+ type: "function",
310
+ function: { name: toolChoice.toolName }
311
+ },
312
+ toolWarnings
313
+ };
314
+ default: {
315
+ const _exhaustiveCheck = type;
316
+ throw new import_provider.UnsupportedFunctionalityError({
317
+ functionality: `tool choice type: ${_exhaustiveCheck}`
318
+ });
319
+ }
320
+ }
321
+ }
322
+
323
+ // src/chat/get-response-metadata.ts
324
+ function getResponseMetadata({
325
+ id,
326
+ model,
327
+ created
328
+ }) {
329
+ return {
330
+ id: id != null ? id : void 0,
331
+ modelId: model != null ? model : void 0,
332
+ timestamp: created != null ? new Date(created * 1e3) : void 0
44
333
  };
45
- };
46
- var deepSeekMetadataExtractor = {
47
- extractMetadata: async ({ parsedBody }) => {
48
- const parsed = await (0, import_provider_utils.safeValidateTypes)({
49
- value: parsedBody,
50
- schema: deepSeekResponseSchema
334
+ }
335
+
336
+ // src/chat/map-deepseek-finish-reason.ts
337
+ function mapDeepSeekFinishReason(finishReason) {
338
+ switch (finishReason) {
339
+ case "stop":
340
+ return "stop";
341
+ case "length":
342
+ return "length";
343
+ case "content_filter":
344
+ return "content-filter";
345
+ case "tool_calls":
346
+ return "tool-calls";
347
+ case "insufficient_system_resource":
348
+ return "error";
349
+ default:
350
+ return "unknown";
351
+ }
352
+ }
353
+
354
+ // src/chat/deepseek-chat-language-model.ts
355
+ var DeepSeekChatLanguageModel = class {
356
+ constructor(modelId, config) {
357
+ this.specificationVersion = "v3";
358
+ this.supportedUrls = {};
359
+ this.modelId = modelId;
360
+ this.config = config;
361
+ this.failedResponseHandler = (0, import_provider_utils2.createJsonErrorResponseHandler)({
362
+ errorSchema: deepSeekErrorSchema,
363
+ errorToMessage: (error) => error.error.message
364
+ });
365
+ }
366
+ get provider() {
367
+ return this.config.provider;
368
+ }
369
+ get providerOptionsName() {
370
+ return this.config.provider.split(".")[0].trim();
371
+ }
372
+ async getArgs({
373
+ prompt,
374
+ maxOutputTokens,
375
+ temperature,
376
+ topP,
377
+ topK,
378
+ frequencyPenalty,
379
+ presencePenalty,
380
+ providerOptions,
381
+ stopSequences,
382
+ responseFormat,
383
+ seed,
384
+ toolChoice,
385
+ tools
386
+ }) {
387
+ var _a, _b;
388
+ const deepseekOptions = (_a = await (0, import_provider_utils2.parseProviderOptions)({
389
+ provider: this.providerOptionsName,
390
+ providerOptions,
391
+ schema: deepseekChatOptions
392
+ })) != null ? _a : {};
393
+ const { messages, warnings } = convertToDeepSeekChatMessages({
394
+ prompt,
395
+ responseFormat
396
+ });
397
+ if (topK != null) {
398
+ warnings.push({ type: "unsupported", feature: "topK" });
399
+ }
400
+ if (seed != null) {
401
+ warnings.push({ type: "unsupported", feature: "seed" });
402
+ }
403
+ const {
404
+ tools: deepseekTools,
405
+ toolChoice: deepseekToolChoices,
406
+ toolWarnings
407
+ } = prepareTools({
408
+ tools,
409
+ toolChoice
51
410
  });
52
- return !parsed.success || parsed.value.usage == null ? void 0 : buildDeepseekMetadata(parsed.value.usage);
53
- },
54
- createStreamExtractor: () => {
55
- let usage;
56
411
  return {
57
- processChunk: async (chunk) => {
58
- var _a, _b;
59
- const parsed = await (0, import_provider_utils.safeValidateTypes)({
60
- value: chunk,
61
- schema: deepSeekStreamChunkSchema
412
+ args: {
413
+ model: this.modelId,
414
+ max_tokens: maxOutputTokens,
415
+ temperature,
416
+ top_p: topP,
417
+ frequency_penalty: frequencyPenalty,
418
+ presence_penalty: presencePenalty,
419
+ response_format: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? { type: "json_object" } : void 0,
420
+ stop: stopSequences,
421
+ messages,
422
+ tools: deepseekTools,
423
+ tool_choice: deepseekToolChoices,
424
+ thinking: ((_b = deepseekOptions.thinking) == null ? void 0 : _b.type) != null ? { type: deepseekOptions.thinking.type } : void 0
425
+ },
426
+ warnings: [...warnings, ...toolWarnings]
427
+ };
428
+ }
429
+ async doGenerate(options) {
430
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
431
+ const { args, warnings } = await this.getArgs({ ...options });
432
+ const {
433
+ responseHeaders,
434
+ value: responseBody,
435
+ rawValue: rawResponse
436
+ } = await (0, import_provider_utils2.postJsonToApi)({
437
+ url: this.config.url({
438
+ path: "/chat/completions",
439
+ modelId: this.modelId
440
+ }),
441
+ headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
442
+ body: args,
443
+ failedResponseHandler: this.failedResponseHandler,
444
+ successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
445
+ deepseekChatResponseSchema
446
+ ),
447
+ abortSignal: options.abortSignal,
448
+ fetch: this.config.fetch
449
+ });
450
+ const choice = responseBody.choices[0];
451
+ const content = [];
452
+ const reasoning = choice.message.reasoning_content;
453
+ if (reasoning != null && reasoning.length > 0) {
454
+ content.push({
455
+ type: "reasoning",
456
+ text: reasoning
457
+ });
458
+ }
459
+ if (choice.message.tool_calls != null) {
460
+ for (const toolCall of choice.message.tool_calls) {
461
+ content.push({
462
+ type: "tool-call",
463
+ toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils2.generateId)(),
464
+ toolName: toolCall.function.name,
465
+ input: toolCall.function.arguments
62
466
  });
63
- if (parsed.success && ((_b = (_a = parsed.value.choices) == null ? void 0 : _a[0]) == null ? void 0 : _b.finish_reason) === "stop" && parsed.value.usage) {
64
- usage = parsed.value.usage;
467
+ }
468
+ }
469
+ const text = choice.message.content;
470
+ if (text != null && text.length > 0) {
471
+ content.push({ type: "text", text });
472
+ }
473
+ return {
474
+ content,
475
+ finishReason: mapDeepSeekFinishReason(choice.finish_reason),
476
+ usage: {
477
+ inputTokens: (_c = (_b = responseBody.usage) == null ? void 0 : _b.prompt_tokens) != null ? _c : void 0,
478
+ outputTokens: (_e = (_d = responseBody.usage) == null ? void 0 : _d.completion_tokens) != null ? _e : void 0,
479
+ totalTokens: (_g = (_f = responseBody.usage) == null ? void 0 : _f.total_tokens) != null ? _g : void 0,
480
+ reasoningTokens: (_j = (_i = (_h = responseBody.usage) == null ? void 0 : _h.completion_tokens_details) == null ? void 0 : _i.reasoning_tokens) != null ? _j : void 0,
481
+ cachedInputTokens: (_l = (_k = responseBody.usage) == null ? void 0 : _k.prompt_cache_hit_tokens) != null ? _l : void 0
482
+ },
483
+ providerMetadata: {
484
+ [this.providerOptionsName]: {
485
+ promptCacheHitTokens: (_m = responseBody.usage) == null ? void 0 : _m.prompt_cache_hit_tokens,
486
+ promptCacheMissTokens: (_n = responseBody.usage) == null ? void 0 : _n.prompt_cache_miss_tokens
65
487
  }
66
488
  },
67
- buildMetadata: () => buildDeepseekMetadata(usage)
489
+ request: { body: args },
490
+ response: {
491
+ ...getResponseMetadata(responseBody),
492
+ headers: responseHeaders,
493
+ body: rawResponse
494
+ },
495
+ warnings
496
+ };
497
+ }
498
+ async doStream(options) {
499
+ const { args, warnings } = await this.getArgs({ ...options });
500
+ const body = {
501
+ ...args,
502
+ stream: true,
503
+ stream_options: { include_usage: true }
504
+ };
505
+ const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
506
+ url: this.config.url({
507
+ path: "/chat/completions",
508
+ modelId: this.modelId
509
+ }),
510
+ headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
511
+ body,
512
+ failedResponseHandler: this.failedResponseHandler,
513
+ successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
514
+ deepseekChatChunkSchema
515
+ ),
516
+ abortSignal: options.abortSignal,
517
+ fetch: this.config.fetch
518
+ });
519
+ const toolCalls = [];
520
+ let finishReason = "unknown";
521
+ let usage = void 0;
522
+ let isFirstChunk = true;
523
+ const providerOptionsName = this.providerOptionsName;
524
+ let isActiveReasoning = false;
525
+ let isActiveText = false;
526
+ return {
527
+ stream: response.pipeThrough(
528
+ new TransformStream({
529
+ start(controller) {
530
+ controller.enqueue({ type: "stream-start", warnings });
531
+ },
532
+ transform(chunk, controller) {
533
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
534
+ if (options.includeRawChunks) {
535
+ controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
536
+ }
537
+ if (!chunk.success) {
538
+ finishReason = "error";
539
+ controller.enqueue({ type: "error", error: chunk.error });
540
+ return;
541
+ }
542
+ const value = chunk.value;
543
+ if ("error" in value) {
544
+ finishReason = "error";
545
+ controller.enqueue({ type: "error", error: value.error.message });
546
+ return;
547
+ }
548
+ if (isFirstChunk) {
549
+ isFirstChunk = false;
550
+ controller.enqueue({
551
+ type: "response-metadata",
552
+ ...getResponseMetadata(value)
553
+ });
554
+ }
555
+ if (value.usage != null) {
556
+ usage = value.usage;
557
+ }
558
+ const choice = value.choices[0];
559
+ if ((choice == null ? void 0 : choice.finish_reason) != null) {
560
+ finishReason = mapDeepSeekFinishReason(choice.finish_reason);
561
+ }
562
+ if ((choice == null ? void 0 : choice.delta) == null) {
563
+ return;
564
+ }
565
+ const delta = choice.delta;
566
+ const reasoningContent = delta.reasoning_content;
567
+ if (reasoningContent) {
568
+ if (!isActiveReasoning) {
569
+ controller.enqueue({
570
+ type: "reasoning-start",
571
+ id: "reasoning-0"
572
+ });
573
+ isActiveReasoning = true;
574
+ }
575
+ controller.enqueue({
576
+ type: "reasoning-delta",
577
+ id: "reasoning-0",
578
+ delta: reasoningContent
579
+ });
580
+ }
581
+ if (delta.content) {
582
+ if (!isActiveText) {
583
+ controller.enqueue({ type: "text-start", id: "txt-0" });
584
+ isActiveText = true;
585
+ }
586
+ if (isActiveReasoning) {
587
+ controller.enqueue({
588
+ type: "reasoning-end",
589
+ id: "reasoning-0"
590
+ });
591
+ isActiveReasoning = false;
592
+ }
593
+ controller.enqueue({
594
+ type: "text-delta",
595
+ id: "txt-0",
596
+ delta: delta.content
597
+ });
598
+ }
599
+ if (delta.tool_calls != null) {
600
+ if (isActiveReasoning) {
601
+ controller.enqueue({
602
+ type: "reasoning-end",
603
+ id: "reasoning-0"
604
+ });
605
+ isActiveReasoning = false;
606
+ }
607
+ for (const toolCallDelta of delta.tool_calls) {
608
+ const index = toolCallDelta.index;
609
+ if (toolCalls[index] == null) {
610
+ if (toolCallDelta.id == null) {
611
+ throw new import_provider2.InvalidResponseDataError({
612
+ data: toolCallDelta,
613
+ message: `Expected 'id' to be a string.`
614
+ });
615
+ }
616
+ if (((_a = toolCallDelta.function) == null ? void 0 : _a.name) == null) {
617
+ throw new import_provider2.InvalidResponseDataError({
618
+ data: toolCallDelta,
619
+ message: `Expected 'function.name' to be a string.`
620
+ });
621
+ }
622
+ controller.enqueue({
623
+ type: "tool-input-start",
624
+ id: toolCallDelta.id,
625
+ toolName: toolCallDelta.function.name
626
+ });
627
+ toolCalls[index] = {
628
+ id: toolCallDelta.id,
629
+ type: "function",
630
+ function: {
631
+ name: toolCallDelta.function.name,
632
+ arguments: (_b = toolCallDelta.function.arguments) != null ? _b : ""
633
+ },
634
+ hasFinished: false
635
+ };
636
+ const toolCall2 = toolCalls[index];
637
+ if (((_c = toolCall2.function) == null ? void 0 : _c.name) != null && ((_d = toolCall2.function) == null ? void 0 : _d.arguments) != null) {
638
+ if (toolCall2.function.arguments.length > 0) {
639
+ controller.enqueue({
640
+ type: "tool-input-delta",
641
+ id: toolCall2.id,
642
+ delta: toolCall2.function.arguments
643
+ });
644
+ }
645
+ if ((0, import_provider_utils2.isParsableJson)(toolCall2.function.arguments)) {
646
+ controller.enqueue({
647
+ type: "tool-input-end",
648
+ id: toolCall2.id
649
+ });
650
+ controller.enqueue({
651
+ type: "tool-call",
652
+ toolCallId: (_e = toolCall2.id) != null ? _e : (0, import_provider_utils2.generateId)(),
653
+ toolName: toolCall2.function.name,
654
+ input: toolCall2.function.arguments
655
+ });
656
+ toolCall2.hasFinished = true;
657
+ }
658
+ }
659
+ continue;
660
+ }
661
+ const toolCall = toolCalls[index];
662
+ if (toolCall.hasFinished) {
663
+ continue;
664
+ }
665
+ if (((_f = toolCallDelta.function) == null ? void 0 : _f.arguments) != null) {
666
+ toolCall.function.arguments += (_h = (_g = toolCallDelta.function) == null ? void 0 : _g.arguments) != null ? _h : "";
667
+ }
668
+ controller.enqueue({
669
+ type: "tool-input-delta",
670
+ id: toolCall.id,
671
+ delta: (_i = toolCallDelta.function.arguments) != null ? _i : ""
672
+ });
673
+ if (((_j = toolCall.function) == null ? void 0 : _j.name) != null && ((_k = toolCall.function) == null ? void 0 : _k.arguments) != null && (0, import_provider_utils2.isParsableJson)(toolCall.function.arguments)) {
674
+ controller.enqueue({
675
+ type: "tool-input-end",
676
+ id: toolCall.id
677
+ });
678
+ controller.enqueue({
679
+ type: "tool-call",
680
+ toolCallId: (_l = toolCall.id) != null ? _l : (0, import_provider_utils2.generateId)(),
681
+ toolName: toolCall.function.name,
682
+ input: toolCall.function.arguments
683
+ });
684
+ toolCall.hasFinished = true;
685
+ }
686
+ }
687
+ }
688
+ },
689
+ flush(controller) {
690
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
691
+ if (isActiveReasoning) {
692
+ controller.enqueue({ type: "reasoning-end", id: "reasoning-0" });
693
+ }
694
+ if (isActiveText) {
695
+ controller.enqueue({ type: "text-end", id: "txt-0" });
696
+ }
697
+ for (const toolCall of toolCalls.filter(
698
+ (toolCall2) => !toolCall2.hasFinished
699
+ )) {
700
+ controller.enqueue({
701
+ type: "tool-input-end",
702
+ id: toolCall.id
703
+ });
704
+ controller.enqueue({
705
+ type: "tool-call",
706
+ toolCallId: (_a = toolCall.id) != null ? _a : (0, import_provider_utils2.generateId)(),
707
+ toolName: toolCall.function.name,
708
+ input: toolCall.function.arguments
709
+ });
710
+ }
711
+ controller.enqueue({
712
+ type: "finish",
713
+ finishReason,
714
+ usage: {
715
+ inputTokens: (_b = usage == null ? void 0 : usage.prompt_tokens) != null ? _b : void 0,
716
+ outputTokens: (_c = usage == null ? void 0 : usage.completion_tokens) != null ? _c : void 0,
717
+ totalTokens: (_d = usage == null ? void 0 : usage.total_tokens) != null ? _d : void 0,
718
+ reasoningTokens: (_f = (_e = usage == null ? void 0 : usage.completion_tokens_details) == null ? void 0 : _e.reasoning_tokens) != null ? _f : void 0,
719
+ cachedInputTokens: (_g = usage == null ? void 0 : usage.prompt_cache_hit_tokens) != null ? _g : void 0
720
+ },
721
+ providerMetadata: {
722
+ [providerOptionsName]: {
723
+ promptCacheHitTokens: (_h = usage == null ? void 0 : usage.prompt_cache_hit_tokens) != null ? _h : void 0,
724
+ promptCacheMissTokens: (_i = usage == null ? void 0 : usage.prompt_cache_miss_tokens) != null ? _i : void 0
725
+ }
726
+ }
727
+ });
728
+ }
729
+ })
730
+ ),
731
+ request: { body },
732
+ response: { headers: responseHeaders }
68
733
  };
69
734
  }
70
735
  };
71
- var deepSeekUsageSchema = import_v4.z.object({
72
- prompt_cache_hit_tokens: import_v4.z.number().nullish(),
73
- prompt_cache_miss_tokens: import_v4.z.number().nullish()
74
- });
75
- var deepSeekResponseSchema = import_v4.z.object({
76
- usage: deepSeekUsageSchema.nullish()
77
- });
78
- var deepSeekStreamChunkSchema = import_v4.z.object({
79
- choices: import_v4.z.array(
80
- import_v4.z.object({
81
- finish_reason: import_v4.z.string().nullish()
82
- })
83
- ).nullish(),
84
- usage: deepSeekUsageSchema.nullish()
85
- });
86
736
 
87
737
  // src/version.ts
88
- var VERSION = true ? "2.0.0-beta.42" : "0.0.0-test";
738
+ var VERSION = true ? "2.0.0-beta.43" : "0.0.0-test";
89
739
 
90
740
  // src/deepseek-provider.ts
91
741
  function createDeepSeek(options = {}) {
92
742
  var _a;
93
- const baseURL = (0, import_provider_utils2.withoutTrailingSlash)(
94
- (_a = options.baseURL) != null ? _a : "https://api.deepseek.com/v1"
743
+ const baseURL = (0, import_provider_utils3.withoutTrailingSlash)(
744
+ (_a = options.baseURL) != null ? _a : "https://api.deepseek.com"
95
745
  );
96
- const getHeaders = () => (0, import_provider_utils2.withUserAgentSuffix)(
746
+ const getHeaders = () => (0, import_provider_utils3.withUserAgentSuffix)(
97
747
  {
98
- Authorization: `Bearer ${(0, import_provider_utils2.loadApiKey)({
748
+ Authorization: `Bearer ${(0, import_provider_utils3.loadApiKey)({
99
749
  apiKey: options.apiKey,
100
750
  environmentVariableName: "DEEPSEEK_API_KEY",
101
751
  description: "DeepSeek API key"
@@ -104,32 +754,12 @@ function createDeepSeek(options = {}) {
104
754
  },
105
755
  `ai-sdk/deepseek/${VERSION}`
106
756
  );
107
- class DeepSeekChatLanguageModel extends import_openai_compatible.OpenAICompatibleChatLanguageModel {
108
- addJsonInstruction(opts) {
109
- var _a2;
110
- if (((_a2 = opts.responseFormat) == null ? void 0 : _a2.type) !== "json") return opts;
111
- const promptArray = Array.isArray(opts.prompt) ? opts.prompt : [];
112
- const instruction = "Return ONLY a valid JSON object.";
113
- const adjustedPrompt = [
114
- ...promptArray,
115
- { role: "user", content: [{ type: "text", text: instruction }] }
116
- ];
117
- return { ...opts, prompt: adjustedPrompt };
118
- }
119
- async doGenerate(options2) {
120
- return super.doGenerate(this.addJsonInstruction(options2));
121
- }
122
- async doStream(options2) {
123
- return super.doStream(this.addJsonInstruction(options2));
124
- }
125
- }
126
757
  const createLanguageModel = (modelId) => {
127
758
  return new DeepSeekChatLanguageModel(modelId, {
128
759
  provider: `deepseek.chat`,
129
760
  url: ({ path }) => `${baseURL}${path}`,
130
761
  headers: getHeaders,
131
- fetch: options.fetch,
132
- metadataExtractor: deepSeekMetadataExtractor
762
+ fetch: options.fetch
133
763
  });
134
764
  };
135
765
  const provider = (modelId) => createLanguageModel(modelId);
@@ -137,10 +767,10 @@ function createDeepSeek(options = {}) {
137
767
  provider.languageModel = createLanguageModel;
138
768
  provider.chat = createLanguageModel;
139
769
  provider.embeddingModel = (modelId) => {
140
- throw new import_provider.NoSuchModelError({ modelId, modelType: "embeddingModel" });
770
+ throw new import_provider3.NoSuchModelError({ modelId, modelType: "embeddingModel" });
141
771
  };
142
772
  provider.imageModel = (modelId) => {
143
- throw new import_provider.NoSuchModelError({ modelId, modelType: "imageModel" });
773
+ throw new import_provider3.NoSuchModelError({ modelId, modelType: "imageModel" });
144
774
  };
145
775
  return provider;
146
776
  }