@databricks/ai-sdk-provider 0.1.0 → 0.2.0
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/README.md +56 -15
- package/dist/index.cjs +163 -198
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -3
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +163 -198
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -146,13 +146,13 @@ const convertLanguageModelV2PromptToChatAgentResponse = (prompt) => {
|
|
|
146
146
|
for (const msg of prompt) switch (msg.role) {
|
|
147
147
|
case "system": break;
|
|
148
148
|
case "user": {
|
|
149
|
-
const converted = convertUserMessage(msg, messageIndex);
|
|
149
|
+
const converted = convertUserMessage$1(msg, messageIndex);
|
|
150
150
|
messages.push(converted);
|
|
151
151
|
messageIndex++;
|
|
152
152
|
break;
|
|
153
153
|
}
|
|
154
154
|
case "assistant": {
|
|
155
|
-
const converted = convertAssistantMessage(msg, messageIndex);
|
|
155
|
+
const converted = convertAssistantMessage$1(msg, messageIndex);
|
|
156
156
|
messages.push(...converted);
|
|
157
157
|
messageIndex += converted.length;
|
|
158
158
|
break;
|
|
@@ -166,7 +166,7 @@ const convertLanguageModelV2PromptToChatAgentResponse = (prompt) => {
|
|
|
166
166
|
}
|
|
167
167
|
return messages;
|
|
168
168
|
};
|
|
169
|
-
const convertUserMessage = (msg, messageIndex) => {
|
|
169
|
+
const convertUserMessage$1 = (msg, messageIndex) => {
|
|
170
170
|
const text = (msg.content ?? []).filter((part) => part.type === "text").map((part) => part.text).join("\n");
|
|
171
171
|
return {
|
|
172
172
|
role: "user",
|
|
@@ -174,7 +174,7 @@ const convertUserMessage = (msg, messageIndex) => {
|
|
|
174
174
|
id: `user-${messageIndex}`
|
|
175
175
|
};
|
|
176
176
|
};
|
|
177
|
-
const convertAssistantMessage = (msg, startIndex) => {
|
|
177
|
+
const convertAssistantMessage$1 = (msg, startIndex) => {
|
|
178
178
|
const messages = [];
|
|
179
179
|
let messageIndex = startIndex;
|
|
180
180
|
const textContent = (msg.content ?? []).filter((part) => part.type === "text" || part.type === "reasoning").map((part) => part.type === "text" ? part.text : part.text).join("\n");
|
|
@@ -904,9 +904,12 @@ const convertResponsesAgentResponseToMessagePart = (response) => {
|
|
|
904
904
|
parts.push({
|
|
905
905
|
type: "tool-call",
|
|
906
906
|
toolCallId: output.call_id,
|
|
907
|
-
toolName:
|
|
907
|
+
toolName: DATABRICKS_TOOL_CALL_ID,
|
|
908
908
|
input: output.arguments,
|
|
909
|
-
providerMetadata: { databricks: {
|
|
909
|
+
providerMetadata: { databricks: {
|
|
910
|
+
toolName: output.name,
|
|
911
|
+
itemId: output.id
|
|
912
|
+
} }
|
|
910
913
|
});
|
|
911
914
|
break;
|
|
912
915
|
case "reasoning":
|
|
@@ -1011,7 +1014,7 @@ async function convertToResponsesInput({ prompt, systemMessageMode }) {
|
|
|
1011
1014
|
const providerOptions = await (0, __ai_sdk_provider_utils.parseProviderOptions)({
|
|
1012
1015
|
provider: "databricks",
|
|
1013
1016
|
providerOptions: part.providerOptions,
|
|
1014
|
-
schema: ProviderOptionsSchema
|
|
1017
|
+
schema: ProviderOptionsSchema$1
|
|
1015
1018
|
});
|
|
1016
1019
|
const itemId = providerOptions?.itemId ?? void 0;
|
|
1017
1020
|
switch (part.type) {
|
|
@@ -1123,7 +1126,7 @@ async function convertToResponsesInput({ prompt, systemMessageMode }) {
|
|
|
1123
1126
|
warnings
|
|
1124
1127
|
};
|
|
1125
1128
|
}
|
|
1126
|
-
const ProviderOptionsSchema = zod_v4.z.object({
|
|
1129
|
+
const ProviderOptionsSchema$1 = zod_v4.z.object({
|
|
1127
1130
|
itemId: zod_v4.z.string().nullish(),
|
|
1128
1131
|
toolName: zod_v4.z.string().nullish(),
|
|
1129
1132
|
type: zod_v4.z.enum(["mcp_approval_request", "mcp_approval_response"]).nullish(),
|
|
@@ -1140,6 +1143,52 @@ const convertToolResultOutputToString = (output) => {
|
|
|
1140
1143
|
}
|
|
1141
1144
|
};
|
|
1142
1145
|
|
|
1146
|
+
//#endregion
|
|
1147
|
+
//#region src/responses-agent-language-model/responses-prepare-tools.ts
|
|
1148
|
+
/**
|
|
1149
|
+
* Prepare tools for the Responses API format.
|
|
1150
|
+
* Unlike the chat completions API, the responses API expects function tools
|
|
1151
|
+
* with name, description, and parameters at the top level (not nested under 'function').
|
|
1152
|
+
*/
|
|
1153
|
+
function prepareResponsesTools({ tools, toolChoice }) {
|
|
1154
|
+
if (!tools || tools.length === 0) return {
|
|
1155
|
+
tools: void 0,
|
|
1156
|
+
toolChoice: void 0
|
|
1157
|
+
};
|
|
1158
|
+
const responsesTools = [];
|
|
1159
|
+
for (const tool of tools) {
|
|
1160
|
+
if (tool.type === "provider-defined" || tool.name === DATABRICKS_TOOL_CALL_ID) continue;
|
|
1161
|
+
responsesTools.push({
|
|
1162
|
+
type: "function",
|
|
1163
|
+
name: tool.name,
|
|
1164
|
+
description: tool.description,
|
|
1165
|
+
parameters: tool.inputSchema
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
if (responsesTools.length === 0) return {
|
|
1169
|
+
tools: void 0,
|
|
1170
|
+
toolChoice: void 0
|
|
1171
|
+
};
|
|
1172
|
+
const convertedToolChoice = convertResponsesToolChoice(toolChoice);
|
|
1173
|
+
return {
|
|
1174
|
+
tools: responsesTools,
|
|
1175
|
+
toolChoice: convertedToolChoice
|
|
1176
|
+
};
|
|
1177
|
+
}
|
|
1178
|
+
function convertResponsesToolChoice(toolChoice) {
|
|
1179
|
+
if (!toolChoice) return void 0;
|
|
1180
|
+
switch (toolChoice.type) {
|
|
1181
|
+
case "auto": return "auto";
|
|
1182
|
+
case "none": return "none";
|
|
1183
|
+
case "required": return "required";
|
|
1184
|
+
case "tool": return {
|
|
1185
|
+
type: "function",
|
|
1186
|
+
name: toolChoice.toolName
|
|
1187
|
+
};
|
|
1188
|
+
default: return void 0;
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1143
1192
|
//#endregion
|
|
1144
1193
|
//#region src/responses-agent-language-model/responses-agent-language-model.ts
|
|
1145
1194
|
function mapResponsesFinishReason({ finishReason, hasToolCalls }) {
|
|
@@ -1295,13 +1344,19 @@ var DatabricksResponsesAgentLanguageModel = class {
|
|
|
1295
1344
|
prompt: options.prompt,
|
|
1296
1345
|
systemMessageMode: "system"
|
|
1297
1346
|
});
|
|
1347
|
+
const { tools, toolChoice } = prepareResponsesTools({
|
|
1348
|
+
tools: options.tools,
|
|
1349
|
+
toolChoice: options.toolChoice
|
|
1350
|
+
});
|
|
1298
1351
|
return {
|
|
1299
1352
|
url: config.url({ path: "/responses" }),
|
|
1300
1353
|
headers: (0, __ai_sdk_provider_utils.combineHeaders)(config.headers(), options.headers),
|
|
1301
1354
|
body: {
|
|
1302
1355
|
model: modelId,
|
|
1303
1356
|
input,
|
|
1304
|
-
stream
|
|
1357
|
+
stream,
|
|
1358
|
+
...tools ? { tools } : {},
|
|
1359
|
+
...toolChoice && tools ? { tool_choice: toolChoice } : {}
|
|
1305
1360
|
},
|
|
1306
1361
|
fetch: config.fetch
|
|
1307
1362
|
};
|
|
@@ -1404,9 +1459,9 @@ const fmapiChunkSchema = zod_v4.z.object({
|
|
|
1404
1459
|
created: zod_v4.z.number(),
|
|
1405
1460
|
model: zod_v4.z.string(),
|
|
1406
1461
|
usage: zod_v4.z.object({
|
|
1407
|
-
prompt_tokens: zod_v4.z.number(),
|
|
1408
|
-
completion_tokens: zod_v4.z.number(),
|
|
1409
|
-
total_tokens: zod_v4.z.number()
|
|
1462
|
+
prompt_tokens: zod_v4.z.number().nullable().optional(),
|
|
1463
|
+
completion_tokens: zod_v4.z.number().nullable().optional(),
|
|
1464
|
+
total_tokens: zod_v4.z.number().nullable().optional()
|
|
1410
1465
|
}).nullable().optional(),
|
|
1411
1466
|
object: zod_v4.z.literal("chat.completion.chunk"),
|
|
1412
1467
|
choices: zod_v4.z.array(zod_v4.z.object({
|
|
@@ -1462,68 +1517,6 @@ const fmapiResponseSchema = zod_v4.z.object({
|
|
|
1462
1517
|
}))
|
|
1463
1518
|
});
|
|
1464
1519
|
|
|
1465
|
-
//#endregion
|
|
1466
|
-
//#region src/fmapi-language-model/fmapi-tags.ts
|
|
1467
|
-
const TAGS = {
|
|
1468
|
-
LEGACY_CALL_OPEN: "<uc_function_call>",
|
|
1469
|
-
LEGACY_CALL_CLOSE: "</uc_function_call>",
|
|
1470
|
-
LEGACY_RESULT_OPEN: "<uc_function_result>",
|
|
1471
|
-
LEGACY_RESULT_CLOSE: "</uc_function_result>",
|
|
1472
|
-
CALL_OPEN: "<tool_call>",
|
|
1473
|
-
CALL_CLOSE: "</tool_call>",
|
|
1474
|
-
RESULT_OPEN: "<tool_call_result>",
|
|
1475
|
-
RESULT_CLOSE: "</tool_call_result>"
|
|
1476
|
-
};
|
|
1477
|
-
const tagSplitRegex = new RegExp(`(${escapeRegex(TAGS.LEGACY_CALL_OPEN)}.*?${escapeRegex(TAGS.LEGACY_CALL_CLOSE)}|${escapeRegex(TAGS.LEGACY_RESULT_OPEN)}.*?${escapeRegex(TAGS.LEGACY_RESULT_CLOSE)}|${escapeRegex(TAGS.CALL_OPEN)}.*?${escapeRegex(TAGS.CALL_CLOSE)}|${escapeRegex(TAGS.RESULT_OPEN)}.*?${escapeRegex(TAGS.RESULT_CLOSE)})`, "g");
|
|
1478
|
-
function parseTaggedToolCall(text) {
|
|
1479
|
-
const inner = stripEnclosingTag(text, TAGS.LEGACY_CALL_OPEN, TAGS.LEGACY_CALL_CLOSE) ?? stripEnclosingTag(text, TAGS.CALL_OPEN, TAGS.CALL_CLOSE);
|
|
1480
|
-
if (inner == null) return null;
|
|
1481
|
-
try {
|
|
1482
|
-
const parsed = JSON.parse(inner);
|
|
1483
|
-
if (parsed && typeof parsed === "object" && "id" in parsed && "name" in parsed) return {
|
|
1484
|
-
id: String(parsed.id),
|
|
1485
|
-
name: String(parsed.name),
|
|
1486
|
-
arguments: parsed.arguments
|
|
1487
|
-
};
|
|
1488
|
-
} catch {}
|
|
1489
|
-
return null;
|
|
1490
|
-
}
|
|
1491
|
-
function parseTaggedToolResult(text) {
|
|
1492
|
-
const inner = stripEnclosingTag(text, TAGS.LEGACY_RESULT_OPEN, TAGS.LEGACY_RESULT_CLOSE) ?? stripEnclosingTag(text, TAGS.RESULT_OPEN, TAGS.RESULT_CLOSE);
|
|
1493
|
-
if (inner == null) return null;
|
|
1494
|
-
try {
|
|
1495
|
-
const parsed = JSON.parse(inner);
|
|
1496
|
-
if (parsed && typeof parsed === "object" && "id" in parsed) return {
|
|
1497
|
-
id: String(parsed.id),
|
|
1498
|
-
content: parsed.content
|
|
1499
|
-
};
|
|
1500
|
-
} catch {}
|
|
1501
|
-
return null;
|
|
1502
|
-
}
|
|
1503
|
-
function serializeToolCall(value) {
|
|
1504
|
-
const payload = JSON.stringify({
|
|
1505
|
-
id: value.id,
|
|
1506
|
-
name: value.name,
|
|
1507
|
-
arguments: value.arguments
|
|
1508
|
-
});
|
|
1509
|
-
return `${TAGS.CALL_OPEN}${payload}${TAGS.CALL_CLOSE}`;
|
|
1510
|
-
}
|
|
1511
|
-
function serializeToolResult(value) {
|
|
1512
|
-
const payload = JSON.stringify({
|
|
1513
|
-
id: value.id,
|
|
1514
|
-
content: value.content
|
|
1515
|
-
});
|
|
1516
|
-
return `${TAGS.RESULT_OPEN}${payload}${TAGS.RESULT_CLOSE}`;
|
|
1517
|
-
}
|
|
1518
|
-
function stripEnclosingTag(text, open, close) {
|
|
1519
|
-
const trimmed = text.trim();
|
|
1520
|
-
if (trimmed.startsWith(open) && trimmed.endsWith(close)) return trimmed.slice(open.length, trimmed.length - close.length);
|
|
1521
|
-
return null;
|
|
1522
|
-
}
|
|
1523
|
-
function escapeRegex(str) {
|
|
1524
|
-
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
1525
|
-
}
|
|
1526
|
-
|
|
1527
1520
|
//#endregion
|
|
1528
1521
|
//#region src/fmapi-language-model/fmapi-convert-to-message-parts.ts
|
|
1529
1522
|
const convertFmapiChunkToMessagePart = (chunk, toolCallIdsByIndex) => {
|
|
@@ -1550,13 +1543,11 @@ const convertFmapiChunkToMessagePart = (chunk, toolCallIdsByIndex) => {
|
|
|
1550
1543
|
}
|
|
1551
1544
|
}
|
|
1552
1545
|
if (typeof choice.delta.content === "string") {
|
|
1553
|
-
|
|
1554
|
-
for (const part of extracted) if (part.type === "text") parts.push({
|
|
1546
|
+
if (choice.delta.content) parts.push({
|
|
1555
1547
|
type: "text-delta",
|
|
1556
1548
|
id: chunk.id,
|
|
1557
|
-
delta:
|
|
1549
|
+
delta: choice.delta.content
|
|
1558
1550
|
});
|
|
1559
|
-
else parts.push(part);
|
|
1560
1551
|
} else if (Array.isArray(choice.delta.content)) parts.push(...mapContentItemsToStreamParts(choice.delta.content, chunk.id));
|
|
1561
1552
|
return parts;
|
|
1562
1553
|
};
|
|
@@ -1572,56 +1563,22 @@ const convertFmapiResponseToMessagePart = (response) => {
|
|
|
1572
1563
|
});
|
|
1573
1564
|
return parts;
|
|
1574
1565
|
}
|
|
1575
|
-
if (typeof choice.message.content === "string") {
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1579
|
-
|
|
1580
|
-
text: choice.message.content
|
|
1581
|
-
});
|
|
1582
|
-
} else parts.push(...mapContentItemsToProviderContent(choice.message.content ?? []));
|
|
1566
|
+
if (typeof choice.message.content === "string") parts.push({
|
|
1567
|
+
type: "text",
|
|
1568
|
+
text: choice.message.content
|
|
1569
|
+
});
|
|
1570
|
+
else parts.push(...mapContentItemsToProviderContent(choice.message.content ?? []));
|
|
1583
1571
|
return parts;
|
|
1584
1572
|
};
|
|
1585
1573
|
const convertToolCallToContent = (toolCall) => {
|
|
1586
1574
|
return {
|
|
1587
1575
|
type: "tool-call",
|
|
1588
1576
|
toolCallId: toolCall.id,
|
|
1589
|
-
toolName:
|
|
1590
|
-
input: toolCall.function.arguments
|
|
1577
|
+
toolName: DATABRICKS_TOOL_CALL_ID,
|
|
1578
|
+
input: toolCall.function.arguments,
|
|
1579
|
+
providerMetadata: { databricks: { toolName: toolCall.function.name } }
|
|
1591
1580
|
};
|
|
1592
1581
|
};
|
|
1593
|
-
const extractPartsFromTextCompletion = (text) => {
|
|
1594
|
-
const parts = text.split(tagSplitRegex);
|
|
1595
|
-
const accumulated = [];
|
|
1596
|
-
for (const segment of parts.filter((p) => p !== "")) {
|
|
1597
|
-
const toolParts = extractToolPartsFromText(segment);
|
|
1598
|
-
if (toolParts) accumulated.push(...toolParts);
|
|
1599
|
-
else accumulated.push({
|
|
1600
|
-
type: "text",
|
|
1601
|
-
text: segment
|
|
1602
|
-
});
|
|
1603
|
-
}
|
|
1604
|
-
return accumulated;
|
|
1605
|
-
};
|
|
1606
|
-
const extractToolPartsFromText = (text) => {
|
|
1607
|
-
const trimmed = text.trim();
|
|
1608
|
-
const call = parseTaggedToolCall(trimmed);
|
|
1609
|
-
if (call) return [{
|
|
1610
|
-
type: "tool-call",
|
|
1611
|
-
input: typeof call.arguments === "string" ? call.arguments : JSON.stringify(call.arguments),
|
|
1612
|
-
toolName: call.name,
|
|
1613
|
-
toolCallId: call.id,
|
|
1614
|
-
providerExecuted: true
|
|
1615
|
-
}];
|
|
1616
|
-
const result = parseTaggedToolResult(trimmed);
|
|
1617
|
-
if (result) return [{
|
|
1618
|
-
type: "tool-result",
|
|
1619
|
-
result: result.content,
|
|
1620
|
-
toolCallId: result.id,
|
|
1621
|
-
toolName: DATABRICKS_TOOL_CALL_ID
|
|
1622
|
-
}];
|
|
1623
|
-
return null;
|
|
1624
|
-
};
|
|
1625
1582
|
const mapContentItemsToStreamParts = (items, id) => {
|
|
1626
1583
|
const parts = [];
|
|
1627
1584
|
for (const item of items) switch (item.type) {
|
|
@@ -1667,43 +1624,38 @@ const mapContentItemsToProviderContent = (items) => {
|
|
|
1667
1624
|
|
|
1668
1625
|
//#endregion
|
|
1669
1626
|
//#region src/fmapi-language-model/fmapi-convert-to-input.ts
|
|
1670
|
-
const convertPromptToFmapiMessages = (prompt) => {
|
|
1671
|
-
const messages =
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
break;
|
|
1687
|
-
}
|
|
1688
|
-
const content = contentItems.length === 0 ? "" : contentItems;
|
|
1689
|
-
return {
|
|
1690
|
-
role,
|
|
1691
|
-
content
|
|
1692
|
-
};
|
|
1693
|
-
});
|
|
1627
|
+
const convertPromptToFmapiMessages = async (prompt) => {
|
|
1628
|
+
const messages = [];
|
|
1629
|
+
for (const message of prompt) switch (message.role) {
|
|
1630
|
+
case "system":
|
|
1631
|
+
messages.push(convertSystemMessage(message));
|
|
1632
|
+
break;
|
|
1633
|
+
case "user":
|
|
1634
|
+
messages.push(convertUserMessage(message));
|
|
1635
|
+
break;
|
|
1636
|
+
case "assistant":
|
|
1637
|
+
messages.push(await convertAssistantMessage(message));
|
|
1638
|
+
break;
|
|
1639
|
+
case "tool":
|
|
1640
|
+
messages.push(...convertToolMessages(message));
|
|
1641
|
+
break;
|
|
1642
|
+
}
|
|
1694
1643
|
return { messages };
|
|
1695
1644
|
};
|
|
1696
|
-
const
|
|
1697
|
-
return
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1645
|
+
const convertSystemMessage = (message) => {
|
|
1646
|
+
return {
|
|
1647
|
+
role: "system",
|
|
1648
|
+
content: [{
|
|
1649
|
+
type: "text",
|
|
1650
|
+
text: message.content
|
|
1651
|
+
}]
|
|
1652
|
+
};
|
|
1701
1653
|
};
|
|
1702
|
-
const
|
|
1703
|
-
const
|
|
1654
|
+
const convertUserMessage = (message) => {
|
|
1655
|
+
const content = [];
|
|
1704
1656
|
for (const part of message.content) switch (part.type) {
|
|
1705
1657
|
case "text":
|
|
1706
|
-
|
|
1658
|
+
content.push({
|
|
1707
1659
|
type: "text",
|
|
1708
1660
|
text: part.text
|
|
1709
1661
|
});
|
|
@@ -1711,20 +1663,24 @@ const convertUserContent = (message) => {
|
|
|
1711
1663
|
case "file":
|
|
1712
1664
|
if (part.mediaType.startsWith("image/")) {
|
|
1713
1665
|
const url = toHttpUrlString(part.data);
|
|
1714
|
-
if (url)
|
|
1666
|
+
if (url) content.push({
|
|
1715
1667
|
type: "image",
|
|
1716
1668
|
image_url: url
|
|
1717
1669
|
});
|
|
1718
1670
|
}
|
|
1719
1671
|
break;
|
|
1720
1672
|
}
|
|
1721
|
-
return
|
|
1673
|
+
return {
|
|
1674
|
+
role: "user",
|
|
1675
|
+
content
|
|
1676
|
+
};
|
|
1722
1677
|
};
|
|
1723
|
-
const
|
|
1724
|
-
const
|
|
1678
|
+
const convertAssistantMessage = async (message) => {
|
|
1679
|
+
const contentItems = [];
|
|
1680
|
+
const toolCalls = [];
|
|
1725
1681
|
for (const part of message.content) switch (part.type) {
|
|
1726
1682
|
case "text":
|
|
1727
|
-
|
|
1683
|
+
contentItems.push({
|
|
1728
1684
|
type: "text",
|
|
1729
1685
|
text: part.text
|
|
1730
1686
|
});
|
|
@@ -1732,14 +1688,14 @@ const convertAssistantContent = (message) => {
|
|
|
1732
1688
|
case "file":
|
|
1733
1689
|
if (part.mediaType.startsWith("image/")) {
|
|
1734
1690
|
const url = toHttpUrlString(part.data);
|
|
1735
|
-
if (url)
|
|
1691
|
+
if (url) contentItems.push({
|
|
1736
1692
|
type: "image",
|
|
1737
1693
|
image_url: url
|
|
1738
1694
|
});
|
|
1739
1695
|
}
|
|
1740
1696
|
break;
|
|
1741
1697
|
case "reasoning":
|
|
1742
|
-
|
|
1698
|
+
contentItems.push({
|
|
1743
1699
|
type: "reasoning",
|
|
1744
1700
|
summary: [{
|
|
1745
1701
|
type: "summary_text",
|
|
@@ -1747,38 +1703,36 @@ const convertAssistantContent = (message) => {
|
|
|
1747
1703
|
}]
|
|
1748
1704
|
});
|
|
1749
1705
|
break;
|
|
1750
|
-
case "tool-call":
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
break;
|
|
1760
|
-
case "tool-result":
|
|
1761
|
-
items.push({
|
|
1762
|
-
type: "text",
|
|
1763
|
-
text: serializeToolResult({
|
|
1764
|
-
id: part.toolCallId,
|
|
1765
|
-
content: convertToolResultOutputToContentValue(part.output)
|
|
1766
|
-
})
|
|
1706
|
+
case "tool-call": {
|
|
1707
|
+
const toolName = await getToolNameFromPart(part);
|
|
1708
|
+
toolCalls.push({
|
|
1709
|
+
id: part.toolCallId,
|
|
1710
|
+
type: "function",
|
|
1711
|
+
function: {
|
|
1712
|
+
name: toolName,
|
|
1713
|
+
arguments: typeof part.input === "string" ? part.input : JSON.stringify(part.input)
|
|
1714
|
+
}
|
|
1767
1715
|
});
|
|
1768
1716
|
break;
|
|
1717
|
+
}
|
|
1769
1718
|
}
|
|
1770
|
-
return
|
|
1719
|
+
return {
|
|
1720
|
+
role: "assistant",
|
|
1721
|
+
content: contentItems.length === 0 ? null : contentItems,
|
|
1722
|
+
...toolCalls.length > 0 ? { tool_calls: toolCalls } : {}
|
|
1723
|
+
};
|
|
1771
1724
|
};
|
|
1772
|
-
const
|
|
1773
|
-
const
|
|
1774
|
-
for (const part of message.content) if (part.type === "tool-result")
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1725
|
+
const convertToolMessages = (message) => {
|
|
1726
|
+
const messages = [];
|
|
1727
|
+
for (const part of message.content) if (part.type === "tool-result") {
|
|
1728
|
+
const content = convertToolResultOutputToContentValue(part.output);
|
|
1729
|
+
messages.push({
|
|
1730
|
+
role: "tool",
|
|
1731
|
+
tool_call_id: part.toolCallId,
|
|
1732
|
+
content: typeof content === "string" ? content : JSON.stringify(content)
|
|
1733
|
+
});
|
|
1734
|
+
}
|
|
1735
|
+
return messages;
|
|
1782
1736
|
};
|
|
1783
1737
|
const toHttpUrlString = (data) => {
|
|
1784
1738
|
if (data instanceof URL) return data.toString();
|
|
@@ -1797,6 +1751,15 @@ const convertToolResultOutputToContentValue = (output) => {
|
|
|
1797
1751
|
default: return null;
|
|
1798
1752
|
}
|
|
1799
1753
|
};
|
|
1754
|
+
const ProviderOptionsSchema = zod_v4.z.object({ toolName: zod_v4.z.string().nullish() });
|
|
1755
|
+
const getToolNameFromPart = async (part) => {
|
|
1756
|
+
const providerOptions = await (0, __ai_sdk_provider_utils.parseProviderOptions)({
|
|
1757
|
+
provider: "databricks",
|
|
1758
|
+
providerOptions: part.providerOptions,
|
|
1759
|
+
schema: ProviderOptionsSchema
|
|
1760
|
+
});
|
|
1761
|
+
return providerOptions?.toolName ?? part.toolName;
|
|
1762
|
+
};
|
|
1800
1763
|
|
|
1801
1764
|
//#endregion
|
|
1802
1765
|
//#region src/fmapi-language-model/fmapi-language-model.ts
|
|
@@ -1813,7 +1776,7 @@ var DatabricksFmapiLanguageModel = class {
|
|
|
1813
1776
|
}
|
|
1814
1777
|
supportedUrls = {};
|
|
1815
1778
|
async doGenerate(options) {
|
|
1816
|
-
const networkArgs = this.getArgs({
|
|
1779
|
+
const networkArgs = await this.getArgs({
|
|
1817
1780
|
config: this.config,
|
|
1818
1781
|
options,
|
|
1819
1782
|
stream: false,
|
|
@@ -1843,7 +1806,7 @@ var DatabricksFmapiLanguageModel = class {
|
|
|
1843
1806
|
};
|
|
1844
1807
|
}
|
|
1845
1808
|
async doStream(options) {
|
|
1846
|
-
const networkArgs = this.getArgs({
|
|
1809
|
+
const networkArgs = await this.getArgs({
|
|
1847
1810
|
config: this.config,
|
|
1848
1811
|
options,
|
|
1849
1812
|
stream: true,
|
|
@@ -1893,9 +1856,9 @@ var DatabricksFmapiLanguageModel = class {
|
|
|
1893
1856
|
if (choice?.finish_reason === "stop") finishReason = "stop";
|
|
1894
1857
|
else if (choice?.finish_reason === "tool_calls") finishReason = "tool-calls";
|
|
1895
1858
|
if (chunk.value.usage) usage = {
|
|
1896
|
-
inputTokens: chunk.value.usage.prompt_tokens,
|
|
1897
|
-
outputTokens: chunk.value.usage.completion_tokens,
|
|
1898
|
-
totalTokens: chunk.value.usage.total_tokens
|
|
1859
|
+
inputTokens: chunk.value.usage.prompt_tokens ?? 0,
|
|
1860
|
+
outputTokens: chunk.value.usage.completion_tokens ?? 0,
|
|
1861
|
+
totalTokens: chunk.value.usage.total_tokens ?? 0
|
|
1899
1862
|
};
|
|
1900
1863
|
const parts = convertFmapiChunkToMessagePart(chunk.value, toolCallIdsByIndex);
|
|
1901
1864
|
for (const part of parts) {
|
|
@@ -1920,8 +1883,9 @@ var DatabricksFmapiLanguageModel = class {
|
|
|
1920
1883
|
controller.enqueue({
|
|
1921
1884
|
type: "tool-call",
|
|
1922
1885
|
toolCallId,
|
|
1923
|
-
toolName,
|
|
1924
|
-
input: inputText
|
|
1886
|
+
toolName: DATABRICKS_TOOL_CALL_ID,
|
|
1887
|
+
input: inputText,
|
|
1888
|
+
providerMetadata: { databricks: { toolName } }
|
|
1925
1889
|
});
|
|
1926
1890
|
}
|
|
1927
1891
|
}
|
|
@@ -1936,14 +1900,15 @@ var DatabricksFmapiLanguageModel = class {
|
|
|
1936
1900
|
response: { headers: responseHeaders }
|
|
1937
1901
|
};
|
|
1938
1902
|
}
|
|
1939
|
-
getArgs({ config, options, stream, modelId }) {
|
|
1903
|
+
async getArgs({ config, options, stream, modelId }) {
|
|
1940
1904
|
const tools = options.tools?.map((tool) => convertToolToOpenAIFormat(tool)).filter((tool) => tool !== void 0);
|
|
1941
1905
|
const toolChoice = options.toolChoice ? convertToolChoiceToOpenAIFormat(options.toolChoice) : void 0;
|
|
1906
|
+
const { messages } = await convertPromptToFmapiMessages(options.prompt);
|
|
1942
1907
|
return {
|
|
1943
1908
|
url: config.url({ path: "/chat/completions" }),
|
|
1944
1909
|
headers: (0, __ai_sdk_provider_utils.combineHeaders)(config.headers(), options.headers),
|
|
1945
1910
|
body: {
|
|
1946
|
-
messages
|
|
1911
|
+
messages,
|
|
1947
1912
|
stream,
|
|
1948
1913
|
model: modelId,
|
|
1949
1914
|
...tools && tools.length > 0 ? { tools } : {},
|
|
@@ -2019,9 +1984,9 @@ const createDatabricksProvider = (settings) => {
|
|
|
2019
1984
|
};
|
|
2020
1985
|
};
|
|
2021
1986
|
return {
|
|
1987
|
+
responses: createResponsesAgent,
|
|
1988
|
+
chatCompletions: createFmapi,
|
|
2022
1989
|
chatAgent: createChatAgent,
|
|
2023
|
-
responsesAgent: createResponsesAgent,
|
|
2024
|
-
fmapi: createFmapi,
|
|
2025
1990
|
imageModel: notImplemented("ImageModel"),
|
|
2026
1991
|
textEmbeddingModel: notImplemented("TextEmbeddingModel"),
|
|
2027
1992
|
languageModel: notImplemented("LanguageModel")
|