@ai-sdk/deepseek 3.0.81 → 3.0.83
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 +15 -0
- package/dist/index.d.ts +49 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +978 -5
- package/dist/index.js.map +1 -1
- package/docs/30-deepseek.mdx +91 -0
- package/package.json +2 -2
- package/src/deepseek-provider.ts +24 -0
- package/src/index.ts +1 -0
- package/src/responses/convert-deepseek-responses-usage.ts +44 -0
- package/src/responses/convert-to-deepseek-responses-input.ts +279 -0
- package/src/responses/deepseek-responses-api.ts +214 -0
- package/src/responses/deepseek-responses-language-model-options.ts +54 -0
- package/src/responses/deepseek-responses-language-model.ts +756 -0
- package/src/responses/deepseek-responses-prepare-tools.ts +87 -0
- package/src/responses/map-deepseek-responses-finish-reason.ts +26 -0
- package/src/tool/index.ts +7 -0
- package/src/tool/web-search.ts +47 -0
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { InvalidArgumentError, InvalidPromptError, InvalidResponseDataError, NoSuchModelError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
2
|
-
import { StreamingToolCallTracker, WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertInlineFileDataToUint8Array, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createLanguageModelResponseMetadata as getResponseMetadata, createNullLanguageModelUsage, createProviderStreamError, detectMediaType, generateId, getTopLevelMediaType, isCustomReasoning, lazySchema, loadApiKey, mapReasoningToProviderEffort, parseProviderOptions, postFormDataToApi, postJsonToApi, resolve, resolveFullMediaType, resolveProviderReference, resolveWith, secureJsonParse, serializeModelOptions, stringifyWithBigIntAndCycles, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
|
|
1
|
+
import { APICallError, InvalidArgumentError, InvalidPromptError, InvalidResponseDataError, NoSuchModelError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
|
|
2
|
+
import { StreamingToolCallTracker, WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertInlineFileDataToUint8Array, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createLanguageModelResponseMetadata, createLanguageModelResponseMetadata as getResponseMetadata, createNullLanguageModelUsage, createProviderExecutedToolFactory, createProviderStreamError, detectMediaType, generateId, getTopLevelMediaType, isCustomReasoning, lazySchema, loadApiKey, mapReasoningToProviderEffort, parseProviderOptions, postFormDataToApi, postJsonToApi, resolve, resolveFullMediaType, resolveProviderReference, resolveWith, secureJsonParse, serializeModelOptions, stringifyWithBigIntAndCycles, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
|
|
3
3
|
import { z } from "zod/v4";
|
|
4
4
|
//#region src/chat/deepseek-file-part-options.ts
|
|
5
5
|
const deepseekFilePartProviderOptions = lazySchema(() => zodSchema(z.object({
|
|
@@ -203,7 +203,7 @@ function isDeepSeekV4Model(modelId) {
|
|
|
203
203
|
}
|
|
204
204
|
//#endregion
|
|
205
205
|
//#region src/chat/convert-to-deepseek-chat-messages.ts
|
|
206
|
-
const supportedImageMediaTypes = /* @__PURE__ */ new Set([
|
|
206
|
+
const supportedImageMediaTypes$1 = /* @__PURE__ */ new Set([
|
|
207
207
|
"image/gif",
|
|
208
208
|
"image/jpeg",
|
|
209
209
|
"image/jpg",
|
|
@@ -212,7 +212,7 @@ const supportedImageMediaTypes = /* @__PURE__ */ new Set([
|
|
|
212
212
|
]);
|
|
213
213
|
function resolveDeepSeekImageMediaType(part) {
|
|
214
214
|
const resolvedMediaType = resolveFullMediaType({ part });
|
|
215
|
-
if (!supportedImageMediaTypes.has(resolvedMediaType)) throw new UnsupportedFunctionalityError({
|
|
215
|
+
if (!supportedImageMediaTypes$1.has(resolvedMediaType)) throw new UnsupportedFunctionalityError({
|
|
216
216
|
functionality: `DeepSeek image media type ${resolvedMediaType}`,
|
|
217
217
|
message: "DeepSeek supports JPEG, PNG, GIF, and WebP image inputs."
|
|
218
218
|
});
|
|
@@ -1290,8 +1290,973 @@ function hasSupportedFilenameExtension(filename) {
|
|
|
1290
1290
|
return extensionSeparatorIndex !== -1 && supportedFilenameExtensions.has(filename.slice(extensionSeparatorIndex + 1).toLowerCase());
|
|
1291
1291
|
}
|
|
1292
1292
|
//#endregion
|
|
1293
|
+
//#region src/responses/convert-to-deepseek-responses-input.ts
|
|
1294
|
+
async function convertToDeepSeekResponsesInput({ prompt, providerOptionsName }) {
|
|
1295
|
+
const input = [];
|
|
1296
|
+
const inputWarnings = [];
|
|
1297
|
+
for (const message of prompt) switch (message.role) {
|
|
1298
|
+
case "system":
|
|
1299
|
+
input.push({
|
|
1300
|
+
role: "system",
|
|
1301
|
+
content: message.content
|
|
1302
|
+
});
|
|
1303
|
+
break;
|
|
1304
|
+
case "user": {
|
|
1305
|
+
const content = [];
|
|
1306
|
+
for (const part of message.content) if (part.type === "text") content.push({
|
|
1307
|
+
type: "input_text",
|
|
1308
|
+
text: part.text
|
|
1309
|
+
});
|
|
1310
|
+
else if (part.type === "file" && getTopLevelMediaType(part.mediaType) === "image" && part.data.type !== "text") content.push(await convertImagePart({
|
|
1311
|
+
part,
|
|
1312
|
+
prompt,
|
|
1313
|
+
providerOptionsName
|
|
1314
|
+
}));
|
|
1315
|
+
else inputWarnings.push({
|
|
1316
|
+
type: "unsupported",
|
|
1317
|
+
feature: `user message part type: ${part.type}`
|
|
1318
|
+
});
|
|
1319
|
+
input.push({
|
|
1320
|
+
role: "user",
|
|
1321
|
+
content
|
|
1322
|
+
});
|
|
1323
|
+
break;
|
|
1324
|
+
}
|
|
1325
|
+
case "assistant":
|
|
1326
|
+
for (const part of message.content) switch (part.type) {
|
|
1327
|
+
case "text":
|
|
1328
|
+
if (part.text.length > 0) input.push({
|
|
1329
|
+
role: "assistant",
|
|
1330
|
+
content: part.text
|
|
1331
|
+
});
|
|
1332
|
+
break;
|
|
1333
|
+
case "reasoning": {
|
|
1334
|
+
const itemId = getStringOption(part.providerOptions?.[providerOptionsName]?.itemId);
|
|
1335
|
+
input.push({
|
|
1336
|
+
type: "reasoning",
|
|
1337
|
+
...itemId != null && { id: itemId },
|
|
1338
|
+
content: [{
|
|
1339
|
+
type: "reasoning_text",
|
|
1340
|
+
text: part.text
|
|
1341
|
+
}]
|
|
1342
|
+
});
|
|
1343
|
+
break;
|
|
1344
|
+
}
|
|
1345
|
+
case "tool-call":
|
|
1346
|
+
if (part.providerExecuted) {
|
|
1347
|
+
const webSearchCall = part.providerOptions?.[providerOptionsName]?.webSearchCall;
|
|
1348
|
+
if (isWebSearchCallItem(webSearchCall)) input.push(webSearchCall);
|
|
1349
|
+
break;
|
|
1350
|
+
}
|
|
1351
|
+
input.push({
|
|
1352
|
+
type: "function_call",
|
|
1353
|
+
call_id: part.toolCallId,
|
|
1354
|
+
name: part.toolName,
|
|
1355
|
+
arguments: typeof part.input === "string" ? part.input : JSON.stringify(part.input)
|
|
1356
|
+
});
|
|
1357
|
+
break;
|
|
1358
|
+
case "tool-result": break;
|
|
1359
|
+
default: inputWarnings.push({
|
|
1360
|
+
type: "unsupported",
|
|
1361
|
+
feature: `assistant message part type: ${part.type}`
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
break;
|
|
1365
|
+
case "tool": for (const part of message.content) {
|
|
1366
|
+
if (part.type === "tool-approval-response") continue;
|
|
1367
|
+
const output = part.output;
|
|
1368
|
+
let value;
|
|
1369
|
+
switch (output.type) {
|
|
1370
|
+
case "text":
|
|
1371
|
+
case "error-text":
|
|
1372
|
+
value = output.value;
|
|
1373
|
+
break;
|
|
1374
|
+
case "execution-denied":
|
|
1375
|
+
value = output.reason ?? "Tool call execution denied.";
|
|
1376
|
+
break;
|
|
1377
|
+
case "json":
|
|
1378
|
+
case "error-json":
|
|
1379
|
+
value = JSON.stringify(output.value);
|
|
1380
|
+
break;
|
|
1381
|
+
case "content":
|
|
1382
|
+
value = [];
|
|
1383
|
+
for (const item of output.value) if (item.type === "text") value.push({
|
|
1384
|
+
type: "input_text",
|
|
1385
|
+
text: item.text
|
|
1386
|
+
});
|
|
1387
|
+
else if (item.type === "file" && getTopLevelMediaType(item.mediaType) === "image" && item.data.type !== "text") value.push(await convertImagePart({
|
|
1388
|
+
part: item,
|
|
1389
|
+
prompt,
|
|
1390
|
+
providerOptionsName
|
|
1391
|
+
}));
|
|
1392
|
+
else inputWarnings.push({
|
|
1393
|
+
type: "unsupported",
|
|
1394
|
+
feature: `tool result content part type: ${item.type}`
|
|
1395
|
+
});
|
|
1396
|
+
}
|
|
1397
|
+
input.push({
|
|
1398
|
+
type: "function_call_output",
|
|
1399
|
+
call_id: part.toolCallId,
|
|
1400
|
+
output: value
|
|
1401
|
+
});
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
return {
|
|
1405
|
+
input,
|
|
1406
|
+
inputWarnings
|
|
1407
|
+
};
|
|
1408
|
+
}
|
|
1409
|
+
const supportedImageMediaTypes = /* @__PURE__ */ new Set([
|
|
1410
|
+
"image/gif",
|
|
1411
|
+
"image/jpeg",
|
|
1412
|
+
"image/jpg",
|
|
1413
|
+
"image/png",
|
|
1414
|
+
"image/webp"
|
|
1415
|
+
]);
|
|
1416
|
+
async function convertImagePart({ part, prompt, providerOptionsName }) {
|
|
1417
|
+
if (part.data.type === "reference") return {
|
|
1418
|
+
type: "input_image",
|
|
1419
|
+
file_id: resolveProviderReference({
|
|
1420
|
+
reference: part.data.reference,
|
|
1421
|
+
provider: "deepseek"
|
|
1422
|
+
})
|
|
1423
|
+
};
|
|
1424
|
+
const filePartOptions = await parseProviderOptions({
|
|
1425
|
+
provider: providerOptionsName,
|
|
1426
|
+
providerOptions: part.providerOptions,
|
|
1427
|
+
schema: deepseekFilePartProviderOptions
|
|
1428
|
+
});
|
|
1429
|
+
if (filePartOptions?.fileData === true) throw new UnsupportedFunctionalityError({ functionality: "DeepSeek `fileData` image parts with the Responses API" });
|
|
1430
|
+
const detail = filePartOptions?.imageDetail;
|
|
1431
|
+
if (part.data.type === "url") {
|
|
1432
|
+
const url = part.data.url.toString();
|
|
1433
|
+
if (url.length > 8192) throw new InvalidPromptError({
|
|
1434
|
+
prompt,
|
|
1435
|
+
message: "DeepSeek image URLs must not exceed 8192 characters."
|
|
1436
|
+
});
|
|
1437
|
+
return {
|
|
1438
|
+
type: "input_image",
|
|
1439
|
+
image_url: url,
|
|
1440
|
+
...detail != null && { detail }
|
|
1441
|
+
};
|
|
1442
|
+
}
|
|
1443
|
+
if (part.data.type !== "data") throw new UnsupportedFunctionalityError({ functionality: `DeepSeek image data type ${part.data.type}` });
|
|
1444
|
+
const mediaType = resolveFullMediaType({ part });
|
|
1445
|
+
if (!supportedImageMediaTypes.has(mediaType)) throw new UnsupportedFunctionalityError({
|
|
1446
|
+
functionality: `DeepSeek image media type ${mediaType}`,
|
|
1447
|
+
message: "DeepSeek supports JPEG, PNG, GIF, and WebP image inputs."
|
|
1448
|
+
});
|
|
1449
|
+
return {
|
|
1450
|
+
type: "input_image",
|
|
1451
|
+
image_url: `data:${mediaType === "image/jpg" ? "image/jpeg" : mediaType};base64,${convertToBase64(part.data.data)}`,
|
|
1452
|
+
...detail != null && { detail }
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
function getStringOption(value) {
|
|
1456
|
+
return typeof value === "string" ? value : void 0;
|
|
1457
|
+
}
|
|
1458
|
+
function isWebSearchCallItem(value) {
|
|
1459
|
+
return typeof value === "object" && value != null && !Array.isArray(value) && value.type === "web_search_call";
|
|
1460
|
+
}
|
|
1461
|
+
//#endregion
|
|
1462
|
+
//#region src/responses/convert-deepseek-responses-usage.ts
|
|
1463
|
+
function convertDeepSeekResponsesUsage(usage) {
|
|
1464
|
+
if (usage == null) return createNullLanguageModelUsage();
|
|
1465
|
+
const inputTokens = usage.input_tokens ?? 0;
|
|
1466
|
+
const outputTokens = usage.output_tokens ?? 0;
|
|
1467
|
+
const cacheReadTokens = usage.input_tokens_details?.cached_tokens ?? 0;
|
|
1468
|
+
const reasoningTokens = usage.output_tokens_details?.reasoning_tokens ?? 0;
|
|
1469
|
+
return {
|
|
1470
|
+
inputTokens: {
|
|
1471
|
+
total: inputTokens,
|
|
1472
|
+
noCache: inputTokens - cacheReadTokens,
|
|
1473
|
+
cacheRead: cacheReadTokens,
|
|
1474
|
+
cacheWrite: void 0
|
|
1475
|
+
},
|
|
1476
|
+
outputTokens: {
|
|
1477
|
+
total: outputTokens,
|
|
1478
|
+
text: Math.max(0, outputTokens - reasoningTokens),
|
|
1479
|
+
reasoning: reasoningTokens
|
|
1480
|
+
},
|
|
1481
|
+
raw: usage
|
|
1482
|
+
};
|
|
1483
|
+
}
|
|
1484
|
+
function getDeepSeekServerToolUse(usage) {
|
|
1485
|
+
const serverToolUse = usage?.server_tool_use;
|
|
1486
|
+
if (serverToolUse == null) return;
|
|
1487
|
+
return {
|
|
1488
|
+
toolSearch: serverToolUse.tool_search ?? 0,
|
|
1489
|
+
toolFetch: serverToolUse.tool_fetch ?? 0
|
|
1490
|
+
};
|
|
1491
|
+
}
|
|
1492
|
+
//#endregion
|
|
1493
|
+
//#region src/responses/deepseek-responses-api.ts
|
|
1494
|
+
const deepseekResponsesResponseSchema = lazySchema(() => zodSchema(createResponseSchema()));
|
|
1495
|
+
const deepseekResponsesChunkSchema = lazySchema(() => zodSchema(createChunkSchema()));
|
|
1496
|
+
function createUsageSchema() {
|
|
1497
|
+
return z.object({
|
|
1498
|
+
input_tokens: z.number().nullish(),
|
|
1499
|
+
input_tokens_details: z.object({ cached_tokens: z.number().nullish() }).nullish(),
|
|
1500
|
+
output_tokens: z.number().nullish(),
|
|
1501
|
+
output_tokens_details: z.object({ reasoning_tokens: z.number().nullish() }).nullish(),
|
|
1502
|
+
total_tokens: z.number().nullish(),
|
|
1503
|
+
server_tool_use: z.object({
|
|
1504
|
+
tool_search: z.number().nullish(),
|
|
1505
|
+
tool_fetch: z.number().nullish()
|
|
1506
|
+
}).nullish()
|
|
1507
|
+
});
|
|
1508
|
+
}
|
|
1509
|
+
function createLogprobSchema() {
|
|
1510
|
+
return z.object({
|
|
1511
|
+
token: z.string(),
|
|
1512
|
+
logprob: z.number(),
|
|
1513
|
+
bytes: z.array(z.number()).nullish(),
|
|
1514
|
+
top_logprobs: z.array(z.object({
|
|
1515
|
+
token: z.string(),
|
|
1516
|
+
logprob: z.number(),
|
|
1517
|
+
bytes: z.array(z.number()).nullish()
|
|
1518
|
+
})).nullish()
|
|
1519
|
+
});
|
|
1520
|
+
}
|
|
1521
|
+
function createOutputItemSchema() {
|
|
1522
|
+
return z.union([
|
|
1523
|
+
z.object({
|
|
1524
|
+
type: z.literal("message"),
|
|
1525
|
+
id: z.string().nullish(),
|
|
1526
|
+
content: z.array(z.object({
|
|
1527
|
+
type: z.string(),
|
|
1528
|
+
text: z.string().nullish(),
|
|
1529
|
+
logprobs: z.array(createLogprobSchema()).nullish()
|
|
1530
|
+
})).nullish()
|
|
1531
|
+
}),
|
|
1532
|
+
z.object({
|
|
1533
|
+
type: z.literal("reasoning"),
|
|
1534
|
+
id: z.string().nullish(),
|
|
1535
|
+
content: z.array(z.object({
|
|
1536
|
+
type: z.string(),
|
|
1537
|
+
text: z.string().nullish()
|
|
1538
|
+
})).nullish()
|
|
1539
|
+
}),
|
|
1540
|
+
z.object({
|
|
1541
|
+
type: z.literal("function_call"),
|
|
1542
|
+
id: z.string().nullish(),
|
|
1543
|
+
call_id: z.string(),
|
|
1544
|
+
name: z.string(),
|
|
1545
|
+
arguments: z.string().nullish()
|
|
1546
|
+
}),
|
|
1547
|
+
z.looseObject({
|
|
1548
|
+
type: z.literal("web_search_call"),
|
|
1549
|
+
id: z.string(),
|
|
1550
|
+
status: z.string().nullish(),
|
|
1551
|
+
action: z.record(z.string(), z.unknown()).nullish()
|
|
1552
|
+
}),
|
|
1553
|
+
z.looseObject({ type: z.string() })
|
|
1554
|
+
]);
|
|
1555
|
+
}
|
|
1556
|
+
function createResponseObjectSchema() {
|
|
1557
|
+
return z.object({
|
|
1558
|
+
id: z.string().nullish(),
|
|
1559
|
+
created_at: z.number().nullish(),
|
|
1560
|
+
model: z.string().nullish(),
|
|
1561
|
+
status: z.string().nullish(),
|
|
1562
|
+
error: z.object({
|
|
1563
|
+
code: z.string().nullish(),
|
|
1564
|
+
message: z.string().nullish()
|
|
1565
|
+
}).nullish(),
|
|
1566
|
+
incomplete_details: z.object({ reason: z.string().nullish() }).nullish(),
|
|
1567
|
+
output: z.array(createOutputItemSchema()).nullish(),
|
|
1568
|
+
usage: createUsageSchema().nullish()
|
|
1569
|
+
});
|
|
1570
|
+
}
|
|
1571
|
+
function createResponseSchema() {
|
|
1572
|
+
return createResponseObjectSchema();
|
|
1573
|
+
}
|
|
1574
|
+
function createChunkSchema() {
|
|
1575
|
+
const responseObject = createResponseObjectSchema();
|
|
1576
|
+
const outputItem = createOutputItemSchema();
|
|
1577
|
+
return z.union([
|
|
1578
|
+
z.object({
|
|
1579
|
+
type: z.enum(["response.created", "response.in_progress"]),
|
|
1580
|
+
response: responseObject
|
|
1581
|
+
}),
|
|
1582
|
+
z.object({
|
|
1583
|
+
type: z.enum([
|
|
1584
|
+
"response.completed",
|
|
1585
|
+
"response.incomplete",
|
|
1586
|
+
"response.failed"
|
|
1587
|
+
]),
|
|
1588
|
+
response: responseObject
|
|
1589
|
+
}),
|
|
1590
|
+
z.object({
|
|
1591
|
+
type: z.enum(["response.output_item.added", "response.output_item.done"]),
|
|
1592
|
+
output_index: z.number().nullish(),
|
|
1593
|
+
item: outputItem
|
|
1594
|
+
}),
|
|
1595
|
+
z.object({
|
|
1596
|
+
type: z.literal("response.output_text.delta"),
|
|
1597
|
+
item_id: z.string(),
|
|
1598
|
+
delta: z.string(),
|
|
1599
|
+
logprobs: z.array(createLogprobSchema()).nullish()
|
|
1600
|
+
}),
|
|
1601
|
+
z.object({
|
|
1602
|
+
type: z.literal("response.reasoning_text.delta"),
|
|
1603
|
+
item_id: z.string(),
|
|
1604
|
+
delta: z.string()
|
|
1605
|
+
}),
|
|
1606
|
+
z.object({
|
|
1607
|
+
type: z.literal("response.function_call_arguments.delta"),
|
|
1608
|
+
item_id: z.string(),
|
|
1609
|
+
delta: z.string()
|
|
1610
|
+
}),
|
|
1611
|
+
z.object({
|
|
1612
|
+
type: z.literal("error"),
|
|
1613
|
+
code: z.string().nullish(),
|
|
1614
|
+
message: z.string().nullish()
|
|
1615
|
+
}),
|
|
1616
|
+
z.looseObject({ type: z.string() })
|
|
1617
|
+
]);
|
|
1618
|
+
}
|
|
1619
|
+
//#endregion
|
|
1620
|
+
//#region src/responses/deepseek-responses-language-model-options.ts
|
|
1621
|
+
const deepseekLanguageModelResponsesOptions = lazySchema(() => zodSchema(z.object({
|
|
1622
|
+
thinking: z.object({ type: z.enum(["enabled", "disabled"]).optional() }).optional(),
|
|
1623
|
+
reasoningEffort: z.enum([
|
|
1624
|
+
"low",
|
|
1625
|
+
"high",
|
|
1626
|
+
"max"
|
|
1627
|
+
]).optional(),
|
|
1628
|
+
topLogprobs: z.number().int().min(0).max(20).optional(),
|
|
1629
|
+
userId: z.string().regex(/^[a-zA-Z0-9_-]+$/, "userId must match /^[a-zA-Z0-9_-]+$/").max(512, "userId must be at most 512 characters long").optional()
|
|
1630
|
+
})));
|
|
1631
|
+
//#endregion
|
|
1632
|
+
//#region src/responses/deepseek-responses-prepare-tools.ts
|
|
1633
|
+
function prepareDeepSeekResponsesTools({ tools, toolChoice }) {
|
|
1634
|
+
const toolWarnings = [];
|
|
1635
|
+
if (tools == null || tools.length === 0) return {
|
|
1636
|
+
tools: void 0,
|
|
1637
|
+
toolChoice: void 0,
|
|
1638
|
+
toolWarnings
|
|
1639
|
+
};
|
|
1640
|
+
const deepseekTools = [];
|
|
1641
|
+
for (const tool of tools) {
|
|
1642
|
+
if (tool.type === "provider") {
|
|
1643
|
+
if (tool.id === "deepseek.web_search") deepseekTools.push({ type: "web_search" });
|
|
1644
|
+
else toolWarnings.push({
|
|
1645
|
+
type: "unsupported",
|
|
1646
|
+
feature: `provider-defined tool ${tool.id}`
|
|
1647
|
+
});
|
|
1648
|
+
continue;
|
|
1649
|
+
}
|
|
1650
|
+
if (tool.strict != null) toolWarnings.push({
|
|
1651
|
+
type: "unsupported",
|
|
1652
|
+
feature: "strict tool calls",
|
|
1653
|
+
details: "The DeepSeek Responses API does not support strict mode."
|
|
1654
|
+
});
|
|
1655
|
+
deepseekTools.push({
|
|
1656
|
+
type: "function",
|
|
1657
|
+
name: tool.name,
|
|
1658
|
+
description: tool.description,
|
|
1659
|
+
parameters: tool.inputSchema
|
|
1660
|
+
});
|
|
1661
|
+
}
|
|
1662
|
+
if (toolChoice == null) return {
|
|
1663
|
+
tools: deepseekTools,
|
|
1664
|
+
toolChoice: void 0,
|
|
1665
|
+
toolWarnings
|
|
1666
|
+
};
|
|
1667
|
+
switch (toolChoice.type) {
|
|
1668
|
+
case "auto":
|
|
1669
|
+
case "none":
|
|
1670
|
+
case "required": return {
|
|
1671
|
+
tools: deepseekTools,
|
|
1672
|
+
toolChoice: toolChoice.type,
|
|
1673
|
+
toolWarnings
|
|
1674
|
+
};
|
|
1675
|
+
case "tool": {
|
|
1676
|
+
const selected = tools.find((tool) => tool.name === toolChoice.toolName);
|
|
1677
|
+
if (selected?.type === "provider") {
|
|
1678
|
+
toolWarnings.push({
|
|
1679
|
+
type: "unsupported",
|
|
1680
|
+
feature: `toolChoice for provider-defined tool ${selected.id}`
|
|
1681
|
+
});
|
|
1682
|
+
return {
|
|
1683
|
+
tools: deepseekTools,
|
|
1684
|
+
toolChoice: void 0,
|
|
1685
|
+
toolWarnings
|
|
1686
|
+
};
|
|
1687
|
+
}
|
|
1688
|
+
return {
|
|
1689
|
+
tools: deepseekTools,
|
|
1690
|
+
toolChoice: {
|
|
1691
|
+
type: "function",
|
|
1692
|
+
name: toolChoice.toolName
|
|
1693
|
+
},
|
|
1694
|
+
toolWarnings
|
|
1695
|
+
};
|
|
1696
|
+
}
|
|
1697
|
+
}
|
|
1698
|
+
}
|
|
1699
|
+
//#endregion
|
|
1700
|
+
//#region src/responses/map-deepseek-responses-finish-reason.ts
|
|
1701
|
+
function mapDeepSeekResponsesFinishReason({ status, incompleteReason, hasFunctionCall }) {
|
|
1702
|
+
switch (status) {
|
|
1703
|
+
case "completed": return hasFunctionCall ? "tool-calls" : "stop";
|
|
1704
|
+
case "incomplete": return incompleteReason === "max_output_tokens" ? "length" : incompleteReason === "content_filter" ? "content-filter" : "other";
|
|
1705
|
+
case "failed": return "error";
|
|
1706
|
+
default: return "other";
|
|
1707
|
+
}
|
|
1708
|
+
}
|
|
1709
|
+
//#endregion
|
|
1710
|
+
//#region src/responses/deepseek-responses-language-model.ts
|
|
1711
|
+
var DeepSeekResponsesLanguageModel = class DeepSeekResponsesLanguageModel {
|
|
1712
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
1713
|
+
return serializeModelOptions({
|
|
1714
|
+
modelId: model.modelId,
|
|
1715
|
+
config: model.config
|
|
1716
|
+
});
|
|
1717
|
+
}
|
|
1718
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
1719
|
+
return new DeepSeekResponsesLanguageModel(options.modelId, options.config);
|
|
1720
|
+
}
|
|
1721
|
+
constructor(modelId, config) {
|
|
1722
|
+
this.specificationVersion = "v4";
|
|
1723
|
+
this.supportedUrls = { "image/*": [/^https?:\/\/.*$/] };
|
|
1724
|
+
this.modelId = modelId;
|
|
1725
|
+
this.config = config;
|
|
1726
|
+
this.failedResponseHandler = createJsonErrorResponseHandler({
|
|
1727
|
+
errorSchema: deepSeekErrorSchema,
|
|
1728
|
+
errorToMessage: (error) => error.error.message
|
|
1729
|
+
});
|
|
1730
|
+
}
|
|
1731
|
+
get provider() {
|
|
1732
|
+
return this.config.provider;
|
|
1733
|
+
}
|
|
1734
|
+
get providerOptionsName() {
|
|
1735
|
+
return this.config.provider.split(".")[0].trim();
|
|
1736
|
+
}
|
|
1737
|
+
async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, stopSequences, seed, responseFormat, providerOptions, tools, toolChoice, reasoning }) {
|
|
1738
|
+
const warnings = [];
|
|
1739
|
+
const deepseekOptions = await parseProviderOptions({
|
|
1740
|
+
provider: this.providerOptionsName,
|
|
1741
|
+
providerOptions,
|
|
1742
|
+
schema: deepseekLanguageModelResponsesOptions
|
|
1743
|
+
}) ?? {};
|
|
1744
|
+
if (topK != null) warnings.push({
|
|
1745
|
+
type: "unsupported",
|
|
1746
|
+
feature: "topK"
|
|
1747
|
+
});
|
|
1748
|
+
if (seed != null) warnings.push({
|
|
1749
|
+
type: "unsupported",
|
|
1750
|
+
feature: "seed"
|
|
1751
|
+
});
|
|
1752
|
+
if (frequencyPenalty != null) warnings.push({
|
|
1753
|
+
type: "unsupported",
|
|
1754
|
+
feature: "frequencyPenalty"
|
|
1755
|
+
});
|
|
1756
|
+
if (presencePenalty != null) warnings.push({
|
|
1757
|
+
type: "unsupported",
|
|
1758
|
+
feature: "presencePenalty"
|
|
1759
|
+
});
|
|
1760
|
+
if (stopSequences != null && stopSequences.length > 0) warnings.push({
|
|
1761
|
+
type: "unsupported",
|
|
1762
|
+
feature: "stopSequences"
|
|
1763
|
+
});
|
|
1764
|
+
const { input, inputWarnings } = await convertToDeepSeekResponsesInput({
|
|
1765
|
+
prompt,
|
|
1766
|
+
providerOptionsName: this.providerOptionsName
|
|
1767
|
+
});
|
|
1768
|
+
warnings.push(...inputWarnings);
|
|
1769
|
+
const { tools: deepseekTools, toolChoice: deepseekToolChoice, toolWarnings } = prepareDeepSeekResponsesTools({
|
|
1770
|
+
tools,
|
|
1771
|
+
toolChoice
|
|
1772
|
+
});
|
|
1773
|
+
warnings.push(...toolWarnings);
|
|
1774
|
+
const webSearchToolName = tools?.find((tool) => tool.type === "provider" && tool.id === "deepseek.web_search")?.name;
|
|
1775
|
+
const effort = deepseekOptions.thinking?.type === "disabled" ? "none" : deepseekOptions.reasoningEffort ?? (isCustomReasoning(reasoning) ? reasoning === "none" ? "none" : mapReasoningToProviderEffort({
|
|
1776
|
+
reasoning,
|
|
1777
|
+
effortMap: {
|
|
1778
|
+
minimal: "low",
|
|
1779
|
+
low: "low",
|
|
1780
|
+
medium: "high",
|
|
1781
|
+
high: "high",
|
|
1782
|
+
xhigh: "max",
|
|
1783
|
+
max: "max"
|
|
1784
|
+
},
|
|
1785
|
+
warnings
|
|
1786
|
+
}) : void 0);
|
|
1787
|
+
return {
|
|
1788
|
+
args: {
|
|
1789
|
+
model: this.modelId,
|
|
1790
|
+
input,
|
|
1791
|
+
max_output_tokens: maxOutputTokens,
|
|
1792
|
+
temperature,
|
|
1793
|
+
top_p: topP,
|
|
1794
|
+
...deepseekOptions.topLogprobs != null && { top_logprobs: deepseekOptions.topLogprobs },
|
|
1795
|
+
...effort != null && { reasoning: { effort } },
|
|
1796
|
+
...responseFormat?.type === "json" && { text: { format: responseFormat.schema != null ? {
|
|
1797
|
+
type: "json_schema",
|
|
1798
|
+
name: responseFormat.name ?? "response",
|
|
1799
|
+
schema: responseFormat.schema
|
|
1800
|
+
} : { type: "json_object" } } },
|
|
1801
|
+
tools: deepseekTools,
|
|
1802
|
+
tool_choice: deepseekToolChoice,
|
|
1803
|
+
...deepseekOptions.userId != null && { user: deepseekOptions.userId }
|
|
1804
|
+
},
|
|
1805
|
+
warnings,
|
|
1806
|
+
webSearchToolName
|
|
1807
|
+
};
|
|
1808
|
+
}
|
|
1809
|
+
async doGenerate(options) {
|
|
1810
|
+
const { args, warnings, webSearchToolName } = await this.getArgs(options);
|
|
1811
|
+
const providerOptionsName = this.providerOptionsName;
|
|
1812
|
+
const url = this.config.url({
|
|
1813
|
+
path: "/responses",
|
|
1814
|
+
modelId: this.modelId
|
|
1815
|
+
});
|
|
1816
|
+
const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
|
|
1817
|
+
url,
|
|
1818
|
+
headers: combineHeaders(await resolve(this.config.headers), options.headers),
|
|
1819
|
+
body: args,
|
|
1820
|
+
failedResponseHandler: this.failedResponseHandler,
|
|
1821
|
+
successfulResponseHandler: createJsonResponseHandler(deepseekResponsesResponseSchema),
|
|
1822
|
+
abortSignal: options.abortSignal,
|
|
1823
|
+
fetch: this.config.fetch
|
|
1824
|
+
});
|
|
1825
|
+
if (response.status === "failed" || response.error != null) throw new APICallError({
|
|
1826
|
+
message: response.error?.message ?? "DeepSeek response failed",
|
|
1827
|
+
url,
|
|
1828
|
+
requestBodyValues: args,
|
|
1829
|
+
responseHeaders,
|
|
1830
|
+
responseBody: JSON.stringify(rawResponse),
|
|
1831
|
+
data: rawResponse,
|
|
1832
|
+
...getFailedResponseStatus(response.error?.code)
|
|
1833
|
+
});
|
|
1834
|
+
const content = [];
|
|
1835
|
+
const logprobs = [];
|
|
1836
|
+
let hasFunctionCall = false;
|
|
1837
|
+
for (const item of response.output ?? []) switch (item.type) {
|
|
1838
|
+
case "reasoning": {
|
|
1839
|
+
const reasoning = item;
|
|
1840
|
+
const text = joinText(reasoning.content);
|
|
1841
|
+
content.push({
|
|
1842
|
+
type: "reasoning",
|
|
1843
|
+
text,
|
|
1844
|
+
...reasoning.id != null && { providerMetadata: { [providerOptionsName]: { itemId: reasoning.id } } }
|
|
1845
|
+
});
|
|
1846
|
+
break;
|
|
1847
|
+
}
|
|
1848
|
+
case "message": {
|
|
1849
|
+
const message = item;
|
|
1850
|
+
for (const part of message.content ?? []) {
|
|
1851
|
+
if (part.logprobs != null) logprobs.push(...part.logprobs);
|
|
1852
|
+
if (part.text != null && part.text.length > 0) content.push({
|
|
1853
|
+
type: "text",
|
|
1854
|
+
text: part.text
|
|
1855
|
+
});
|
|
1856
|
+
}
|
|
1857
|
+
break;
|
|
1858
|
+
}
|
|
1859
|
+
case "function_call": {
|
|
1860
|
+
const call = item;
|
|
1861
|
+
hasFunctionCall = true;
|
|
1862
|
+
content.push({
|
|
1863
|
+
type: "tool-call",
|
|
1864
|
+
toolCallId: call.call_id,
|
|
1865
|
+
toolName: call.name,
|
|
1866
|
+
input: call.arguments ?? ""
|
|
1867
|
+
});
|
|
1868
|
+
break;
|
|
1869
|
+
}
|
|
1870
|
+
case "web_search_call": content.push(...webSearchCallContent({
|
|
1871
|
+
item,
|
|
1872
|
+
toolName: webSearchToolName ?? "web_search",
|
|
1873
|
+
providerOptionsName
|
|
1874
|
+
}));
|
|
1875
|
+
}
|
|
1876
|
+
return {
|
|
1877
|
+
content,
|
|
1878
|
+
finishReason: {
|
|
1879
|
+
unified: mapDeepSeekResponsesFinishReason({
|
|
1880
|
+
status: response.status,
|
|
1881
|
+
incompleteReason: response.incomplete_details?.reason,
|
|
1882
|
+
hasFunctionCall
|
|
1883
|
+
}),
|
|
1884
|
+
raw: response.incomplete_details?.reason ?? response.status ?? void 0
|
|
1885
|
+
},
|
|
1886
|
+
usage: convertDeepSeekResponsesUsage(response.usage),
|
|
1887
|
+
providerMetadata: buildProviderMetadata({
|
|
1888
|
+
providerOptionsName,
|
|
1889
|
+
responseId: response.id,
|
|
1890
|
+
usage: response.usage,
|
|
1891
|
+
logprobs
|
|
1892
|
+
}),
|
|
1893
|
+
request: { body: args },
|
|
1894
|
+
response: {
|
|
1895
|
+
...createLanguageModelResponseMetadata({
|
|
1896
|
+
id: response.id,
|
|
1897
|
+
model: response.model,
|
|
1898
|
+
created: response.created_at
|
|
1899
|
+
}),
|
|
1900
|
+
headers: responseHeaders,
|
|
1901
|
+
body: rawResponse
|
|
1902
|
+
},
|
|
1903
|
+
warnings
|
|
1904
|
+
};
|
|
1905
|
+
}
|
|
1906
|
+
async doStream(options) {
|
|
1907
|
+
const { args, warnings, webSearchToolName } = await this.getArgs(options);
|
|
1908
|
+
const providerOptionsName = this.providerOptionsName;
|
|
1909
|
+
const body = {
|
|
1910
|
+
...args,
|
|
1911
|
+
stream: true
|
|
1912
|
+
};
|
|
1913
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
1914
|
+
url: this.config.url({
|
|
1915
|
+
path: "/responses",
|
|
1916
|
+
modelId: this.modelId
|
|
1917
|
+
}),
|
|
1918
|
+
headers: combineHeaders(await resolve(this.config.headers), options.headers),
|
|
1919
|
+
body,
|
|
1920
|
+
failedResponseHandler: this.failedResponseHandler,
|
|
1921
|
+
successfulResponseHandler: createEventSourceResponseHandler(deepseekResponsesChunkSchema),
|
|
1922
|
+
abortSignal: options.abortSignal,
|
|
1923
|
+
fetch: this.config.fetch
|
|
1924
|
+
});
|
|
1925
|
+
let finishReason = {
|
|
1926
|
+
unified: "interrupted",
|
|
1927
|
+
raw: void 0
|
|
1928
|
+
};
|
|
1929
|
+
let usage;
|
|
1930
|
+
let responseId;
|
|
1931
|
+
let hasFunctionCall = false;
|
|
1932
|
+
const logprobs = [];
|
|
1933
|
+
const openText = /* @__PURE__ */ new Set();
|
|
1934
|
+
const openReasoning = /* @__PURE__ */ new Set();
|
|
1935
|
+
const functionCalls = /* @__PURE__ */ new Map();
|
|
1936
|
+
return {
|
|
1937
|
+
stream: response.pipeThrough(new TransformStream({
|
|
1938
|
+
start(controller) {
|
|
1939
|
+
controller.enqueue({
|
|
1940
|
+
type: "stream-start",
|
|
1941
|
+
warnings
|
|
1942
|
+
});
|
|
1943
|
+
},
|
|
1944
|
+
transform(chunk, controller) {
|
|
1945
|
+
if (options.includeRawChunks) controller.enqueue({
|
|
1946
|
+
type: "raw",
|
|
1947
|
+
rawValue: chunk.rawValue
|
|
1948
|
+
});
|
|
1949
|
+
if (!chunk.success) {
|
|
1950
|
+
finishReason = {
|
|
1951
|
+
unified: "error",
|
|
1952
|
+
raw: void 0
|
|
1953
|
+
};
|
|
1954
|
+
controller.enqueue({
|
|
1955
|
+
type: "error",
|
|
1956
|
+
error: chunk.error
|
|
1957
|
+
});
|
|
1958
|
+
return;
|
|
1959
|
+
}
|
|
1960
|
+
const value = chunk.value;
|
|
1961
|
+
switch (value.type) {
|
|
1962
|
+
case "response.created": {
|
|
1963
|
+
const created = value;
|
|
1964
|
+
responseId = created.response.id;
|
|
1965
|
+
controller.enqueue({
|
|
1966
|
+
type: "response-metadata",
|
|
1967
|
+
...createLanguageModelResponseMetadata({
|
|
1968
|
+
id: created.response.id,
|
|
1969
|
+
model: created.response.model,
|
|
1970
|
+
created: created.response.created_at
|
|
1971
|
+
})
|
|
1972
|
+
});
|
|
1973
|
+
return;
|
|
1974
|
+
}
|
|
1975
|
+
case "response.output_item.added": {
|
|
1976
|
+
const item = value.item;
|
|
1977
|
+
const itemId = getItemId(item);
|
|
1978
|
+
if (item.type === "message" && itemId != null) {
|
|
1979
|
+
openText.add(itemId);
|
|
1980
|
+
controller.enqueue({
|
|
1981
|
+
type: "text-start",
|
|
1982
|
+
id: itemId
|
|
1983
|
+
});
|
|
1984
|
+
} else if (item.type === "reasoning" && itemId != null) {
|
|
1985
|
+
openReasoning.add(itemId);
|
|
1986
|
+
controller.enqueue({
|
|
1987
|
+
type: "reasoning-start",
|
|
1988
|
+
id: itemId,
|
|
1989
|
+
providerMetadata: { [providerOptionsName]: { itemId } }
|
|
1990
|
+
});
|
|
1991
|
+
} else if (item.type === "function_call") {
|
|
1992
|
+
const call = item;
|
|
1993
|
+
functionCalls.set(call.id ?? call.call_id, {
|
|
1994
|
+
callId: call.call_id,
|
|
1995
|
+
toolName: call.name,
|
|
1996
|
+
arguments: call.arguments ?? ""
|
|
1997
|
+
});
|
|
1998
|
+
controller.enqueue({
|
|
1999
|
+
type: "tool-input-start",
|
|
2000
|
+
id: call.call_id,
|
|
2001
|
+
toolName: call.name
|
|
2002
|
+
});
|
|
2003
|
+
}
|
|
2004
|
+
return;
|
|
2005
|
+
}
|
|
2006
|
+
case "response.output_text.delta": {
|
|
2007
|
+
const delta = value;
|
|
2008
|
+
if (delta.logprobs != null) logprobs.push(...delta.logprobs);
|
|
2009
|
+
if (!openText.has(delta.item_id)) {
|
|
2010
|
+
openText.add(delta.item_id);
|
|
2011
|
+
controller.enqueue({
|
|
2012
|
+
type: "text-start",
|
|
2013
|
+
id: delta.item_id
|
|
2014
|
+
});
|
|
2015
|
+
}
|
|
2016
|
+
controller.enqueue({
|
|
2017
|
+
type: "text-delta",
|
|
2018
|
+
id: delta.item_id,
|
|
2019
|
+
delta: delta.delta
|
|
2020
|
+
});
|
|
2021
|
+
return;
|
|
2022
|
+
}
|
|
2023
|
+
case "response.reasoning_text.delta": {
|
|
2024
|
+
const delta = value;
|
|
2025
|
+
if (!openReasoning.has(delta.item_id)) {
|
|
2026
|
+
openReasoning.add(delta.item_id);
|
|
2027
|
+
controller.enqueue({
|
|
2028
|
+
type: "reasoning-start",
|
|
2029
|
+
id: delta.item_id,
|
|
2030
|
+
providerMetadata: { [providerOptionsName]: { itemId: delta.item_id } }
|
|
2031
|
+
});
|
|
2032
|
+
}
|
|
2033
|
+
controller.enqueue({
|
|
2034
|
+
type: "reasoning-delta",
|
|
2035
|
+
id: delta.item_id,
|
|
2036
|
+
delta: delta.delta
|
|
2037
|
+
});
|
|
2038
|
+
return;
|
|
2039
|
+
}
|
|
2040
|
+
case "response.function_call_arguments.delta": {
|
|
2041
|
+
const delta = value;
|
|
2042
|
+
const call = functionCalls.get(delta.item_id);
|
|
2043
|
+
if (call == null) return;
|
|
2044
|
+
call.arguments += delta.delta;
|
|
2045
|
+
controller.enqueue({
|
|
2046
|
+
type: "tool-input-delta",
|
|
2047
|
+
id: call.callId,
|
|
2048
|
+
delta: delta.delta
|
|
2049
|
+
});
|
|
2050
|
+
return;
|
|
2051
|
+
}
|
|
2052
|
+
case "response.output_item.done": {
|
|
2053
|
+
const item = value.item;
|
|
2054
|
+
const itemId = getItemId(item);
|
|
2055
|
+
if (item.type === "message" && itemId != null) {
|
|
2056
|
+
if (openText.delete(itemId)) controller.enqueue({
|
|
2057
|
+
type: "text-end",
|
|
2058
|
+
id: itemId
|
|
2059
|
+
});
|
|
2060
|
+
} else if (item.type === "reasoning" && itemId != null) {
|
|
2061
|
+
if (openReasoning.delete(itemId)) controller.enqueue({
|
|
2062
|
+
type: "reasoning-end",
|
|
2063
|
+
id: itemId,
|
|
2064
|
+
providerMetadata: { [providerOptionsName]: { itemId } }
|
|
2065
|
+
});
|
|
2066
|
+
} else if (item.type === "function_call") {
|
|
2067
|
+
const call = item;
|
|
2068
|
+
const key = call.id ?? call.call_id;
|
|
2069
|
+
const tracked = functionCalls.get(key);
|
|
2070
|
+
functionCalls.delete(key);
|
|
2071
|
+
hasFunctionCall = true;
|
|
2072
|
+
if (tracked == null) controller.enqueue({
|
|
2073
|
+
type: "tool-input-start",
|
|
2074
|
+
id: call.call_id,
|
|
2075
|
+
toolName: call.name
|
|
2076
|
+
});
|
|
2077
|
+
controller.enqueue({
|
|
2078
|
+
type: "tool-input-end",
|
|
2079
|
+
id: call.call_id
|
|
2080
|
+
});
|
|
2081
|
+
controller.enqueue({
|
|
2082
|
+
type: "tool-call",
|
|
2083
|
+
toolCallId: call.call_id,
|
|
2084
|
+
toolName: call.name,
|
|
2085
|
+
input: call.arguments ?? tracked?.arguments ?? ""
|
|
2086
|
+
});
|
|
2087
|
+
} else if (item.type === "web_search_call") for (const part of webSearchCallContent({
|
|
2088
|
+
item,
|
|
2089
|
+
toolName: webSearchToolName ?? "web_search",
|
|
2090
|
+
providerOptionsName
|
|
2091
|
+
})) controller.enqueue(part);
|
|
2092
|
+
return;
|
|
2093
|
+
}
|
|
2094
|
+
case "response.completed":
|
|
2095
|
+
case "response.incomplete":
|
|
2096
|
+
case "response.failed": {
|
|
2097
|
+
const done = value;
|
|
2098
|
+
usage = done.response.usage;
|
|
2099
|
+
responseId = done.response.id ?? responseId;
|
|
2100
|
+
finishReason = {
|
|
2101
|
+
unified: mapDeepSeekResponsesFinishReason({
|
|
2102
|
+
status: done.response.status,
|
|
2103
|
+
incompleteReason: done.response.incomplete_details?.reason,
|
|
2104
|
+
hasFunctionCall
|
|
2105
|
+
}),
|
|
2106
|
+
raw: done.response.incomplete_details?.reason ?? done.response.status ?? void 0
|
|
2107
|
+
};
|
|
2108
|
+
if (value.type === "response.failed") controller.enqueue({
|
|
2109
|
+
type: "error",
|
|
2110
|
+
error: createProviderStreamError({
|
|
2111
|
+
message: done.response.error?.message ?? "DeepSeek response failed",
|
|
2112
|
+
type: "response.failed",
|
|
2113
|
+
code: done.response.error?.code ?? void 0,
|
|
2114
|
+
...getFailedResponseStatus(done.response.error?.code),
|
|
2115
|
+
data: value
|
|
2116
|
+
})
|
|
2117
|
+
});
|
|
2118
|
+
return;
|
|
2119
|
+
}
|
|
2120
|
+
case "error": {
|
|
2121
|
+
const error = value;
|
|
2122
|
+
finishReason = {
|
|
2123
|
+
unified: "error",
|
|
2124
|
+
raw: void 0
|
|
2125
|
+
};
|
|
2126
|
+
controller.enqueue({
|
|
2127
|
+
type: "error",
|
|
2128
|
+
error: createProviderStreamError({
|
|
2129
|
+
message: error.message ?? "DeepSeek stream error",
|
|
2130
|
+
type: "error",
|
|
2131
|
+
code: error.code ?? void 0,
|
|
2132
|
+
data: value
|
|
2133
|
+
})
|
|
2134
|
+
});
|
|
2135
|
+
}
|
|
2136
|
+
}
|
|
2137
|
+
},
|
|
2138
|
+
flush(controller) {
|
|
2139
|
+
for (const id of openReasoning) controller.enqueue({
|
|
2140
|
+
type: "reasoning-end",
|
|
2141
|
+
id
|
|
2142
|
+
});
|
|
2143
|
+
for (const id of openText) controller.enqueue({
|
|
2144
|
+
type: "text-end",
|
|
2145
|
+
id
|
|
2146
|
+
});
|
|
2147
|
+
controller.enqueue({
|
|
2148
|
+
type: "finish",
|
|
2149
|
+
finishReason,
|
|
2150
|
+
usage: convertDeepSeekResponsesUsage(usage),
|
|
2151
|
+
providerMetadata: buildProviderMetadata({
|
|
2152
|
+
providerOptionsName,
|
|
2153
|
+
responseId,
|
|
2154
|
+
usage,
|
|
2155
|
+
logprobs
|
|
2156
|
+
})
|
|
2157
|
+
});
|
|
2158
|
+
}
|
|
2159
|
+
})),
|
|
2160
|
+
request: { body },
|
|
2161
|
+
response: { headers: responseHeaders }
|
|
2162
|
+
};
|
|
2163
|
+
}
|
|
2164
|
+
};
|
|
2165
|
+
function getFailedResponseStatus(code) {
|
|
2166
|
+
switch (code) {
|
|
2167
|
+
case "rate_limit_exceeded":
|
|
2168
|
+
case "rate_limit_error": return {
|
|
2169
|
+
statusCode: 429,
|
|
2170
|
+
isRetryable: true
|
|
2171
|
+
};
|
|
2172
|
+
case "insufficient_quota": return {
|
|
2173
|
+
statusCode: 429,
|
|
2174
|
+
isRetryable: false
|
|
2175
|
+
};
|
|
2176
|
+
case "authentication_error":
|
|
2177
|
+
case "invalid_api_key": return {
|
|
2178
|
+
statusCode: 401,
|
|
2179
|
+
isRetryable: false
|
|
2180
|
+
};
|
|
2181
|
+
case "permission_error": return {
|
|
2182
|
+
statusCode: 403,
|
|
2183
|
+
isRetryable: false
|
|
2184
|
+
};
|
|
2185
|
+
case "bad_request":
|
|
2186
|
+
case "context_length_exceeded":
|
|
2187
|
+
case "invalid_request_error": return {
|
|
2188
|
+
statusCode: 400,
|
|
2189
|
+
isRetryable: false
|
|
2190
|
+
};
|
|
2191
|
+
case "timeout":
|
|
2192
|
+
case "timeout_error": return {
|
|
2193
|
+
statusCode: 504,
|
|
2194
|
+
isRetryable: true
|
|
2195
|
+
};
|
|
2196
|
+
default: return {
|
|
2197
|
+
statusCode: 500,
|
|
2198
|
+
isRetryable: true
|
|
2199
|
+
};
|
|
2200
|
+
}
|
|
2201
|
+
}
|
|
2202
|
+
function getItemId(item) {
|
|
2203
|
+
return "id" in item && typeof item.id === "string" ? item.id : void 0;
|
|
2204
|
+
}
|
|
2205
|
+
function joinText(parts) {
|
|
2206
|
+
return (parts ?? []).map((part) => part.text ?? "").join("");
|
|
2207
|
+
}
|
|
2208
|
+
function webSearchCallContent({ item, toolName, providerOptionsName }) {
|
|
2209
|
+
return [{
|
|
2210
|
+
type: "tool-call",
|
|
2211
|
+
toolCallId: item.id,
|
|
2212
|
+
toolName,
|
|
2213
|
+
input: "{}",
|
|
2214
|
+
providerExecuted: true,
|
|
2215
|
+
providerMetadata: { [providerOptionsName]: { webSearchCall: item } }
|
|
2216
|
+
}, {
|
|
2217
|
+
type: "tool-result",
|
|
2218
|
+
toolCallId: item.id,
|
|
2219
|
+
toolName,
|
|
2220
|
+
result: {
|
|
2221
|
+
...item.status != null && { status: item.status },
|
|
2222
|
+
...item.action != null && { action: item.action }
|
|
2223
|
+
}
|
|
2224
|
+
}];
|
|
2225
|
+
}
|
|
2226
|
+
function buildProviderMetadata({ providerOptionsName, responseId, usage, logprobs }) {
|
|
2227
|
+
const serverToolUse = getDeepSeekServerToolUse(usage);
|
|
2228
|
+
return { [providerOptionsName]: {
|
|
2229
|
+
...responseId != null && { responseId },
|
|
2230
|
+
...serverToolUse != null && { serverToolUse },
|
|
2231
|
+
...logprobs.length > 0 && { logprobs }
|
|
2232
|
+
} };
|
|
2233
|
+
}
|
|
2234
|
+
//#endregion
|
|
2235
|
+
//#region src/tool/web-search.ts
|
|
2236
|
+
const webSearchOutputSchema = lazySchema(() => zodSchema(z.object({
|
|
2237
|
+
status: z.string().optional(),
|
|
2238
|
+
action: z.record(z.string(), z.unknown()).optional()
|
|
2239
|
+
})));
|
|
2240
|
+
const webSearchToolFactory = createProviderExecutedToolFactory({
|
|
2241
|
+
id: "deepseek.web_search",
|
|
2242
|
+
inputSchema: lazySchema(() => zodSchema(z.object({}))),
|
|
2243
|
+
outputSchema: webSearchOutputSchema
|
|
2244
|
+
});
|
|
2245
|
+
/**
|
|
2246
|
+
* DeepSeek's server-side web search for the Responses API. DeepSeek runs the
|
|
2247
|
+
* searches and page reads during generation and adds the results to the
|
|
2248
|
+
* model context. Supported with `deepseek-v4-pro`.
|
|
2249
|
+
*
|
|
2250
|
+
* Successful searches and page reads are billed per operation and reported in
|
|
2251
|
+
* `usage.raw.server_tool_use` and `providerMetadata.deepseek.serverToolUse`.
|
|
2252
|
+
*/
|
|
2253
|
+
const webSearch = (args = {}) => webSearchToolFactory(args);
|
|
2254
|
+
//#endregion
|
|
2255
|
+
//#region src/tool/index.ts
|
|
2256
|
+
const deepseekTools = { webSearch };
|
|
2257
|
+
//#endregion
|
|
1293
2258
|
//#region src/version.ts
|
|
1294
|
-
const VERSION = "3.0.
|
|
2259
|
+
const VERSION = "3.0.83";
|
|
1295
2260
|
//#endregion
|
|
1296
2261
|
//#region src/deepseek-provider.ts
|
|
1297
2262
|
function createDeepSeek(options = {}) {
|
|
@@ -1314,6 +2279,12 @@ function createDeepSeek(options = {}) {
|
|
|
1314
2279
|
supportsStrictToolCalls: baseURL.endsWith("/beta")
|
|
1315
2280
|
});
|
|
1316
2281
|
};
|
|
2282
|
+
const createResponsesModel = (modelId) => new DeepSeekResponsesLanguageModel(modelId, {
|
|
2283
|
+
provider: "deepseek.responses",
|
|
2284
|
+
url: ({ path }) => `${baseURL}${path}`,
|
|
2285
|
+
headers: getHeaders,
|
|
2286
|
+
fetch: options.fetch
|
|
2287
|
+
});
|
|
1317
2288
|
const createFiles = () => new DeepSeekFiles({
|
|
1318
2289
|
provider: "deepseek.files",
|
|
1319
2290
|
baseURL,
|
|
@@ -1324,6 +2295,8 @@ function createDeepSeek(options = {}) {
|
|
|
1324
2295
|
provider.specificationVersion = "v4";
|
|
1325
2296
|
provider.languageModel = createLanguageModel;
|
|
1326
2297
|
provider.chat = createLanguageModel;
|
|
2298
|
+
provider.responses = createResponsesModel;
|
|
2299
|
+
provider.tools = deepseekTools;
|
|
1327
2300
|
provider.files = createFiles;
|
|
1328
2301
|
provider.embeddingModel = (modelId) => {
|
|
1329
2302
|
throw new NoSuchModelError({
|