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