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