@ai-sdk/deepseek 3.0.81 → 3.0.84

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,756 @@
1
+ import {
2
+ APICallError,
3
+ type JSONObject,
4
+ type LanguageModelV4,
5
+ type LanguageModelV4CallOptions,
6
+ type LanguageModelV4Content,
7
+ type LanguageModelV4FinishReason,
8
+ type LanguageModelV4GenerateResult,
9
+ type LanguageModelV4StreamPart,
10
+ type LanguageModelV4StreamResult,
11
+ type SharedV4ProviderMetadata,
12
+ type SharedV4Warning,
13
+ } from '@ai-sdk/provider';
14
+ import {
15
+ type Resolvable,
16
+ resolve,
17
+ combineHeaders,
18
+ createEventSourceResponseHandler,
19
+ createJsonErrorResponseHandler,
20
+ createJsonResponseHandler,
21
+ createLanguageModelResponseMetadata,
22
+ createProviderStreamError,
23
+ isCustomReasoning,
24
+ mapReasoningToProviderEffort,
25
+ parseProviderOptions,
26
+ postJsonToApi,
27
+ serializeModelOptions,
28
+ WORKFLOW_SERIALIZE,
29
+ WORKFLOW_DESERIALIZE,
30
+ type FetchFunction,
31
+ type InferSchema,
32
+ type ParseResult,
33
+ type ResponseHandler,
34
+ } from '@ai-sdk/provider-utils';
35
+ import { deepSeekErrorSchema } from '../chat/deepseek-chat-api-types';
36
+ import { convertToDeepSeekResponsesInput } from './convert-to-deepseek-responses-input';
37
+ import {
38
+ convertDeepSeekResponsesUsage,
39
+ getDeepSeekServerToolUse,
40
+ } from './convert-deepseek-responses-usage';
41
+ import {
42
+ deepseekResponsesChunkSchema,
43
+ deepseekResponsesResponseSchema,
44
+ type DeepSeekResponsesOutputItem,
45
+ type DeepSeekResponsesUsage,
46
+ } from './deepseek-responses-api';
47
+ import {
48
+ deepseekLanguageModelResponsesOptions,
49
+ type DeepSeekResponsesModelId,
50
+ } from './deepseek-responses-language-model-options';
51
+ import { prepareDeepSeekResponsesTools } from './deepseek-responses-prepare-tools';
52
+ import { mapDeepSeekResponsesFinishReason } from './map-deepseek-responses-finish-reason';
53
+
54
+ export type DeepSeekResponsesConfig = {
55
+ provider: string;
56
+ headers?: Resolvable<Record<string, string | undefined>>;
57
+ url: (options: { modelId: string; path: string }) => string;
58
+ fetch?: FetchFunction;
59
+ };
60
+
61
+ export class DeepSeekResponsesLanguageModel implements LanguageModelV4 {
62
+ readonly specificationVersion = 'v4';
63
+
64
+ readonly modelId: DeepSeekResponsesModelId;
65
+
66
+ readonly supportedUrls = {
67
+ 'image/*': [/^https?:\/\/.*$/],
68
+ };
69
+
70
+ private readonly config: DeepSeekResponsesConfig;
71
+ private readonly failedResponseHandler: ResponseHandler<APICallError>;
72
+
73
+ static [WORKFLOW_SERIALIZE](model: DeepSeekResponsesLanguageModel) {
74
+ return serializeModelOptions({
75
+ modelId: model.modelId,
76
+ config: model.config,
77
+ });
78
+ }
79
+
80
+ static [WORKFLOW_DESERIALIZE](options: {
81
+ modelId: DeepSeekResponsesModelId;
82
+ config: DeepSeekResponsesConfig;
83
+ }) {
84
+ return new DeepSeekResponsesLanguageModel(options.modelId, options.config);
85
+ }
86
+
87
+ constructor(
88
+ modelId: DeepSeekResponsesModelId,
89
+ config: DeepSeekResponsesConfig,
90
+ ) {
91
+ this.modelId = modelId;
92
+ this.config = config;
93
+ this.failedResponseHandler = createJsonErrorResponseHandler({
94
+ errorSchema: deepSeekErrorSchema,
95
+ errorToMessage: (error: InferSchema<typeof deepSeekErrorSchema>) =>
96
+ error.error.message,
97
+ });
98
+ }
99
+
100
+ get provider(): string {
101
+ return this.config.provider;
102
+ }
103
+
104
+ private get providerOptionsName(): string {
105
+ return this.config.provider.split('.')[0].trim();
106
+ }
107
+
108
+ private async getArgs({
109
+ prompt,
110
+ maxOutputTokens,
111
+ temperature,
112
+ topP,
113
+ topK,
114
+ frequencyPenalty,
115
+ presencePenalty,
116
+ stopSequences,
117
+ seed,
118
+ responseFormat,
119
+ providerOptions,
120
+ tools,
121
+ toolChoice,
122
+ reasoning,
123
+ }: LanguageModelV4CallOptions) {
124
+ const warnings: SharedV4Warning[] = [];
125
+
126
+ const deepseekOptions =
127
+ (await parseProviderOptions({
128
+ provider: this.providerOptionsName,
129
+ providerOptions,
130
+ schema: deepseekLanguageModelResponsesOptions,
131
+ })) ?? {};
132
+
133
+ if (topK != null) {
134
+ warnings.push({ type: 'unsupported', feature: 'topK' });
135
+ }
136
+ if (seed != null) {
137
+ warnings.push({ type: 'unsupported', feature: 'seed' });
138
+ }
139
+ if (frequencyPenalty != null) {
140
+ warnings.push({ type: 'unsupported', feature: 'frequencyPenalty' });
141
+ }
142
+ if (presencePenalty != null) {
143
+ warnings.push({ type: 'unsupported', feature: 'presencePenalty' });
144
+ }
145
+ if (stopSequences != null && stopSequences.length > 0) {
146
+ warnings.push({ type: 'unsupported', feature: 'stopSequences' });
147
+ }
148
+
149
+ const { input, inputWarnings } = await convertToDeepSeekResponsesInput({
150
+ prompt,
151
+ providerOptionsName: this.providerOptionsName,
152
+ });
153
+ warnings.push(...inputWarnings);
154
+
155
+ const {
156
+ tools: deepseekTools,
157
+ toolChoice: deepseekToolChoice,
158
+ toolWarnings,
159
+ } = prepareDeepSeekResponsesTools({ tools, toolChoice });
160
+ warnings.push(...toolWarnings);
161
+
162
+ const webSearchToolName = tools?.find(
163
+ tool => tool.type === 'provider' && tool.id === 'deepseek.web_search',
164
+ )?.name;
165
+
166
+ const effort =
167
+ deepseekOptions.thinking?.type === 'disabled'
168
+ ? 'none'
169
+ : (deepseekOptions.reasoningEffort ??
170
+ (isCustomReasoning(reasoning)
171
+ ? reasoning === 'none'
172
+ ? 'none'
173
+ : mapReasoningToProviderEffort({
174
+ reasoning,
175
+ effortMap: {
176
+ minimal: 'low',
177
+ low: 'low',
178
+ medium: 'high',
179
+ high: 'high',
180
+ xhigh: 'max',
181
+ max: 'max',
182
+ },
183
+ warnings,
184
+ })
185
+ : undefined));
186
+
187
+ return {
188
+ args: {
189
+ model: this.modelId,
190
+ input,
191
+ max_output_tokens: maxOutputTokens,
192
+ temperature,
193
+ top_p: topP,
194
+ ...(deepseekOptions.topLogprobs != null && {
195
+ top_logprobs: deepseekOptions.topLogprobs,
196
+ }),
197
+ ...(effort != null && { reasoning: { effort } }),
198
+ ...(responseFormat?.type === 'json' && {
199
+ text: {
200
+ format:
201
+ responseFormat.schema != null
202
+ ? {
203
+ type: 'json_schema',
204
+ name: responseFormat.name ?? 'response',
205
+ schema: responseFormat.schema,
206
+ }
207
+ : { type: 'json_object' },
208
+ },
209
+ }),
210
+ tools: deepseekTools,
211
+ tool_choice: deepseekToolChoice,
212
+ ...(deepseekOptions.userId != null && { user: deepseekOptions.userId }),
213
+ },
214
+ warnings,
215
+ webSearchToolName,
216
+ };
217
+ }
218
+
219
+ async doGenerate(
220
+ options: LanguageModelV4CallOptions,
221
+ ): Promise<LanguageModelV4GenerateResult> {
222
+ const { args, warnings, webSearchToolName } = await this.getArgs(options);
223
+ const providerOptionsName = this.providerOptionsName;
224
+ const url = this.config.url({ path: '/responses', modelId: this.modelId });
225
+
226
+ const {
227
+ responseHeaders,
228
+ value: response,
229
+ rawValue: rawResponse,
230
+ } = await postJsonToApi({
231
+ url,
232
+ headers: combineHeaders(
233
+ await resolve(this.config.headers),
234
+ options.headers,
235
+ ),
236
+ body: args,
237
+ failedResponseHandler: this.failedResponseHandler,
238
+ successfulResponseHandler: createJsonResponseHandler(
239
+ deepseekResponsesResponseSchema,
240
+ ),
241
+ abortSignal: options.abortSignal,
242
+ fetch: this.config.fetch,
243
+ });
244
+
245
+ if (response.status === 'failed' || response.error != null) {
246
+ throw new APICallError({
247
+ message: response.error?.message ?? 'DeepSeek response failed',
248
+ url,
249
+ requestBodyValues: args,
250
+ responseHeaders,
251
+ responseBody: JSON.stringify(rawResponse),
252
+ data: rawResponse,
253
+ ...getFailedResponseStatus(response.error?.code),
254
+ });
255
+ }
256
+
257
+ const content: Array<LanguageModelV4Content> = [];
258
+ const logprobs: unknown[] = [];
259
+ let hasFunctionCall = false;
260
+
261
+ for (const item of response.output ?? []) {
262
+ switch (item.type) {
263
+ case 'reasoning': {
264
+ const reasoning = item as ReasoningItem;
265
+ const text = joinText(reasoning.content);
266
+ content.push({
267
+ type: 'reasoning',
268
+ text,
269
+ ...(reasoning.id != null && {
270
+ providerMetadata: {
271
+ [providerOptionsName]: { itemId: reasoning.id },
272
+ },
273
+ }),
274
+ });
275
+ break;
276
+ }
277
+
278
+ case 'message': {
279
+ const message = item as MessageItem;
280
+ for (const part of message.content ?? []) {
281
+ if (part.logprobs != null) {
282
+ logprobs.push(...part.logprobs);
283
+ }
284
+ if (part.text != null && part.text.length > 0) {
285
+ content.push({ type: 'text', text: part.text });
286
+ }
287
+ }
288
+ break;
289
+ }
290
+
291
+ case 'function_call': {
292
+ const call = item as FunctionCallItem;
293
+ hasFunctionCall = true;
294
+ content.push({
295
+ type: 'tool-call',
296
+ toolCallId: call.call_id,
297
+ toolName: call.name,
298
+ input: call.arguments ?? '',
299
+ });
300
+ break;
301
+ }
302
+
303
+ case 'web_search_call': {
304
+ content.push(
305
+ ...webSearchCallContent({
306
+ item: item as WebSearchCallItem,
307
+ toolName: webSearchToolName ?? 'web_search',
308
+ providerOptionsName,
309
+ }),
310
+ );
311
+ break;
312
+ }
313
+ }
314
+ }
315
+
316
+ return {
317
+ content,
318
+ finishReason: {
319
+ unified: mapDeepSeekResponsesFinishReason({
320
+ status: response.status,
321
+ incompleteReason: response.incomplete_details?.reason,
322
+ hasFunctionCall,
323
+ }),
324
+ raw:
325
+ response.incomplete_details?.reason ?? response.status ?? undefined,
326
+ },
327
+ usage: convertDeepSeekResponsesUsage(response.usage),
328
+ providerMetadata: buildProviderMetadata({
329
+ providerOptionsName,
330
+ responseId: response.id,
331
+ usage: response.usage,
332
+ logprobs,
333
+ }),
334
+ request: { body: args },
335
+ response: {
336
+ ...createLanguageModelResponseMetadata({
337
+ id: response.id,
338
+ model: response.model,
339
+ created: response.created_at,
340
+ }),
341
+ headers: responseHeaders,
342
+ body: rawResponse,
343
+ },
344
+ warnings,
345
+ };
346
+ }
347
+
348
+ async doStream(
349
+ options: LanguageModelV4CallOptions,
350
+ ): Promise<LanguageModelV4StreamResult> {
351
+ const { args, warnings, webSearchToolName } = await this.getArgs(options);
352
+ const providerOptionsName = this.providerOptionsName;
353
+ const body = { ...args, stream: true };
354
+
355
+ const { responseHeaders, value: response } = await postJsonToApi({
356
+ url: this.config.url({ path: '/responses', modelId: this.modelId }),
357
+ headers: combineHeaders(
358
+ await resolve(this.config.headers),
359
+ options.headers,
360
+ ),
361
+ body,
362
+ failedResponseHandler: this.failedResponseHandler,
363
+ successfulResponseHandler: createEventSourceResponseHandler(
364
+ deepseekResponsesChunkSchema,
365
+ ),
366
+ abortSignal: options.abortSignal,
367
+ fetch: this.config.fetch,
368
+ });
369
+
370
+ let finishReason: LanguageModelV4FinishReason = {
371
+ unified: 'interrupted',
372
+ raw: undefined,
373
+ };
374
+ let usage: DeepSeekResponsesUsage | null | undefined;
375
+ let responseId: string | null | undefined;
376
+ let hasFunctionCall = false;
377
+ const logprobs: unknown[] = [];
378
+ const openText = new Set<string>();
379
+ const openReasoning = new Set<string>();
380
+ const functionCalls = new Map<
381
+ string,
382
+ { callId: string; toolName: string; arguments: string }
383
+ >();
384
+
385
+ return {
386
+ stream: response.pipeThrough(
387
+ new TransformStream<
388
+ ParseResult<InferSchema<typeof deepseekResponsesChunkSchema>>,
389
+ LanguageModelV4StreamPart
390
+ >({
391
+ start(controller) {
392
+ controller.enqueue({ type: 'stream-start', warnings });
393
+ },
394
+
395
+ transform(chunk, controller) {
396
+ if (options.includeRawChunks) {
397
+ controller.enqueue({ type: 'raw', rawValue: chunk.rawValue });
398
+ }
399
+
400
+ if (!chunk.success) {
401
+ finishReason = { unified: 'error', raw: undefined };
402
+ controller.enqueue({ type: 'error', error: chunk.error });
403
+ return;
404
+ }
405
+
406
+ const value = chunk.value as StreamChunk;
407
+
408
+ switch (value.type) {
409
+ case 'response.created': {
410
+ const created = value as ResponseChunk;
411
+ responseId = created.response.id;
412
+ controller.enqueue({
413
+ type: 'response-metadata',
414
+ ...createLanguageModelResponseMetadata({
415
+ id: created.response.id,
416
+ model: created.response.model,
417
+ created: created.response.created_at,
418
+ }),
419
+ });
420
+ return;
421
+ }
422
+
423
+ case 'response.output_item.added': {
424
+ const item = (value as OutputItemChunk).item;
425
+ const itemId = getItemId(item);
426
+ if (item.type === 'message' && itemId != null) {
427
+ openText.add(itemId);
428
+ controller.enqueue({ type: 'text-start', id: itemId });
429
+ } else if (item.type === 'reasoning' && itemId != null) {
430
+ openReasoning.add(itemId);
431
+ controller.enqueue({
432
+ type: 'reasoning-start',
433
+ id: itemId,
434
+ providerMetadata: {
435
+ [providerOptionsName]: { itemId },
436
+ },
437
+ });
438
+ } else if (item.type === 'function_call') {
439
+ const call = item as FunctionCallItem;
440
+ functionCalls.set(call.id ?? call.call_id, {
441
+ callId: call.call_id,
442
+ toolName: call.name,
443
+ arguments: call.arguments ?? '',
444
+ });
445
+ controller.enqueue({
446
+ type: 'tool-input-start',
447
+ id: call.call_id,
448
+ toolName: call.name,
449
+ });
450
+ }
451
+ return;
452
+ }
453
+
454
+ case 'response.output_text.delta': {
455
+ const delta = value as TextDeltaChunk;
456
+ if (delta.logprobs != null) {
457
+ logprobs.push(...delta.logprobs);
458
+ }
459
+ if (!openText.has(delta.item_id)) {
460
+ openText.add(delta.item_id);
461
+ controller.enqueue({ type: 'text-start', id: delta.item_id });
462
+ }
463
+ controller.enqueue({
464
+ type: 'text-delta',
465
+ id: delta.item_id,
466
+ delta: delta.delta,
467
+ });
468
+ return;
469
+ }
470
+
471
+ case 'response.reasoning_text.delta': {
472
+ const delta = value as TextDeltaChunk;
473
+ if (!openReasoning.has(delta.item_id)) {
474
+ openReasoning.add(delta.item_id);
475
+ controller.enqueue({
476
+ type: 'reasoning-start',
477
+ id: delta.item_id,
478
+ providerMetadata: {
479
+ [providerOptionsName]: { itemId: delta.item_id },
480
+ },
481
+ });
482
+ }
483
+ controller.enqueue({
484
+ type: 'reasoning-delta',
485
+ id: delta.item_id,
486
+ delta: delta.delta,
487
+ });
488
+ return;
489
+ }
490
+
491
+ case 'response.function_call_arguments.delta': {
492
+ const delta = value as TextDeltaChunk;
493
+ const call = functionCalls.get(delta.item_id);
494
+ if (call == null) {
495
+ return;
496
+ }
497
+ call.arguments += delta.delta;
498
+ controller.enqueue({
499
+ type: 'tool-input-delta',
500
+ id: call.callId,
501
+ delta: delta.delta,
502
+ });
503
+ return;
504
+ }
505
+
506
+ case 'response.output_item.done': {
507
+ const item = (value as OutputItemChunk).item;
508
+ const itemId = getItemId(item);
509
+ if (item.type === 'message' && itemId != null) {
510
+ if (openText.delete(itemId)) {
511
+ controller.enqueue({ type: 'text-end', id: itemId });
512
+ }
513
+ } else if (item.type === 'reasoning' && itemId != null) {
514
+ if (openReasoning.delete(itemId)) {
515
+ controller.enqueue({
516
+ type: 'reasoning-end',
517
+ id: itemId,
518
+ providerMetadata: {
519
+ [providerOptionsName]: { itemId },
520
+ },
521
+ });
522
+ }
523
+ } else if (item.type === 'function_call') {
524
+ const call = item as FunctionCallItem;
525
+ const key = call.id ?? call.call_id;
526
+ const tracked = functionCalls.get(key);
527
+ functionCalls.delete(key);
528
+ hasFunctionCall = true;
529
+ if (tracked == null) {
530
+ controller.enqueue({
531
+ type: 'tool-input-start',
532
+ id: call.call_id,
533
+ toolName: call.name,
534
+ });
535
+ }
536
+ controller.enqueue({
537
+ type: 'tool-input-end',
538
+ id: call.call_id,
539
+ });
540
+ controller.enqueue({
541
+ type: 'tool-call',
542
+ toolCallId: call.call_id,
543
+ toolName: call.name,
544
+ input: call.arguments ?? tracked?.arguments ?? '',
545
+ });
546
+ } else if (item.type === 'web_search_call') {
547
+ for (const part of webSearchCallContent({
548
+ item: item as WebSearchCallItem,
549
+ toolName: webSearchToolName ?? 'web_search',
550
+ providerOptionsName,
551
+ })) {
552
+ controller.enqueue(part);
553
+ }
554
+ }
555
+ return;
556
+ }
557
+
558
+ case 'response.completed':
559
+ case 'response.incomplete':
560
+ case 'response.failed': {
561
+ const done = value as ResponseChunk;
562
+ usage = done.response.usage;
563
+ responseId = done.response.id ?? responseId;
564
+ finishReason = {
565
+ unified: mapDeepSeekResponsesFinishReason({
566
+ status: done.response.status,
567
+ incompleteReason: done.response.incomplete_details?.reason,
568
+ hasFunctionCall,
569
+ }),
570
+ raw:
571
+ done.response.incomplete_details?.reason ??
572
+ done.response.status ??
573
+ undefined,
574
+ };
575
+ if (value.type === 'response.failed') {
576
+ controller.enqueue({
577
+ type: 'error',
578
+ error: createProviderStreamError({
579
+ message:
580
+ done.response.error?.message ??
581
+ 'DeepSeek response failed',
582
+ type: 'response.failed',
583
+ code: done.response.error?.code ?? undefined,
584
+ ...getFailedResponseStatus(done.response.error?.code),
585
+ data: value,
586
+ }),
587
+ });
588
+ }
589
+ return;
590
+ }
591
+
592
+ case 'error': {
593
+ const error = value as ErrorChunk;
594
+ finishReason = { unified: 'error', raw: undefined };
595
+ controller.enqueue({
596
+ type: 'error',
597
+ error: createProviderStreamError({
598
+ message: error.message ?? 'DeepSeek stream error',
599
+ type: 'error',
600
+ code: error.code ?? undefined,
601
+ data: value,
602
+ }),
603
+ });
604
+ }
605
+ }
606
+ },
607
+
608
+ flush(controller) {
609
+ for (const id of openReasoning) {
610
+ controller.enqueue({ type: 'reasoning-end', id });
611
+ }
612
+ for (const id of openText) {
613
+ controller.enqueue({ type: 'text-end', id });
614
+ }
615
+
616
+ controller.enqueue({
617
+ type: 'finish',
618
+ finishReason,
619
+ usage: convertDeepSeekResponsesUsage(usage),
620
+ providerMetadata: buildProviderMetadata({
621
+ providerOptionsName,
622
+ responseId,
623
+ usage,
624
+ logprobs,
625
+ }),
626
+ });
627
+ },
628
+ }),
629
+ ),
630
+ request: { body },
631
+ response: { headers: responseHeaders },
632
+ };
633
+ }
634
+ }
635
+
636
+ type StreamChunk = InferSchema<typeof deepseekResponsesChunkSchema>;
637
+
638
+ type ResponseChunk = Extract<StreamChunk, { response: unknown }>;
639
+
640
+ type OutputItemChunk = Extract<StreamChunk, { item: unknown }>;
641
+
642
+ type TextDeltaChunk = Extract<
643
+ StreamChunk,
644
+ { type: 'response.output_text.delta' }
645
+ >;
646
+
647
+ type ErrorChunk = Extract<StreamChunk, { type: 'error' }>;
648
+
649
+ type MessageItem = Extract<DeepSeekResponsesOutputItem, { type: 'message' }>;
650
+
651
+ type ReasoningItem = Extract<
652
+ DeepSeekResponsesOutputItem,
653
+ { type: 'reasoning' }
654
+ >;
655
+
656
+ type FunctionCallItem = Extract<
657
+ DeepSeekResponsesOutputItem,
658
+ { type: 'function_call' }
659
+ >;
660
+
661
+ type WebSearchCallItem = Extract<
662
+ DeepSeekResponsesOutputItem,
663
+ { type: 'web_search_call' }
664
+ >;
665
+
666
+ function getFailedResponseStatus(code: string | null | undefined): {
667
+ statusCode: number;
668
+ isRetryable: boolean;
669
+ } {
670
+ switch (code) {
671
+ case 'rate_limit_exceeded':
672
+ case 'rate_limit_error':
673
+ return { statusCode: 429, isRetryable: true };
674
+ case 'insufficient_quota':
675
+ return { statusCode: 429, isRetryable: false };
676
+ case 'authentication_error':
677
+ case 'invalid_api_key':
678
+ return { statusCode: 401, isRetryable: false };
679
+ case 'permission_error':
680
+ return { statusCode: 403, isRetryable: false };
681
+ case 'bad_request':
682
+ case 'context_length_exceeded':
683
+ case 'invalid_request_error':
684
+ return { statusCode: 400, isRetryable: false };
685
+ case 'timeout':
686
+ case 'timeout_error':
687
+ return { statusCode: 504, isRetryable: true };
688
+ default:
689
+ return { statusCode: 500, isRetryable: true };
690
+ }
691
+ }
692
+
693
+ function getItemId(item: DeepSeekResponsesOutputItem): string | undefined {
694
+ return 'id' in item && typeof item.id === 'string' ? item.id : undefined;
695
+ }
696
+
697
+ function joinText(
698
+ parts: Array<{ text?: string | null }> | null | undefined,
699
+ ): string {
700
+ return (parts ?? []).map(part => part.text ?? '').join('');
701
+ }
702
+
703
+ function webSearchCallContent({
704
+ item,
705
+ toolName,
706
+ providerOptionsName,
707
+ }: {
708
+ item: WebSearchCallItem;
709
+ toolName: string;
710
+ providerOptionsName: string;
711
+ }): Array<
712
+ Extract<LanguageModelV4Content, { type: 'tool-call' | 'tool-result' }>
713
+ > {
714
+ return [
715
+ {
716
+ type: 'tool-call',
717
+ toolCallId: item.id,
718
+ toolName,
719
+ input: '{}',
720
+ providerExecuted: true,
721
+ providerMetadata: {
722
+ [providerOptionsName]: { webSearchCall: item as JSONObject },
723
+ },
724
+ },
725
+ {
726
+ type: 'tool-result',
727
+ toolCallId: item.id,
728
+ toolName,
729
+ result: {
730
+ ...(item.status != null && { status: item.status }),
731
+ ...(item.action != null && { action: item.action as JSONObject }),
732
+ },
733
+ },
734
+ ];
735
+ }
736
+
737
+ function buildProviderMetadata({
738
+ providerOptionsName,
739
+ responseId,
740
+ usage,
741
+ logprobs,
742
+ }: {
743
+ providerOptionsName: string;
744
+ responseId: string | null | undefined;
745
+ usage: DeepSeekResponsesUsage | null | undefined;
746
+ logprobs: unknown[];
747
+ }): SharedV4ProviderMetadata {
748
+ const serverToolUse = getDeepSeekServerToolUse(usage);
749
+ return {
750
+ [providerOptionsName]: {
751
+ ...(responseId != null && { responseId }),
752
+ ...(serverToolUse != null && { serverToolUse }),
753
+ ...(logprobs.length > 0 && { logprobs: logprobs as JSONObject[] }),
754
+ },
755
+ };
756
+ }