@ai-sdk/deepseek 2.0.35 → 2.0.37

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