@ai-sdk/amazon-bedrock
Advanced tools
Comparing version 0.0.26 to 0.0.27
@@ -41,98 +41,164 @@ "use strict"; | ||
var _a, _b, _c; | ||
const blocks = groupIntoBlocks(prompt); | ||
let system = void 0; | ||
const messages = []; | ||
for (const { role, content } of prompt) { | ||
switch (role) { | ||
for (let i = 0; i < blocks.length; i++) { | ||
const block = blocks[i]; | ||
const type = block.type; | ||
switch (type) { | ||
case "system": { | ||
if (messages.length > 0) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "System message after non-system message" | ||
functionality: "Multiple system messages that are separated by user/assistant messages" | ||
}); | ||
} | ||
system = system === void 0 ? content : `${system} | ||
${content}`; | ||
system = block.messages.map(({ content }) => content).join("\n"); | ||
break; | ||
} | ||
case "user": { | ||
const bedrockMessageContent = []; | ||
for (const part of content) { | ||
switch (part.type) { | ||
case "text": { | ||
bedrockMessageContent.push({ text: part.text }); | ||
const bedrockContent = []; | ||
for (const message of block.messages) { | ||
const { role, content } = message; | ||
switch (role) { | ||
case "user": { | ||
for (let j = 0; j < content.length; j++) { | ||
const part = content[j]; | ||
switch (part.type) { | ||
case "text": { | ||
bedrockContent.push({ | ||
text: part.text | ||
}); | ||
break; | ||
} | ||
case "image": { | ||
if (part.image instanceof URL) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Image URLs in user messages" | ||
}); | ||
} | ||
bedrockContent.push({ | ||
image: { | ||
format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1], | ||
source: { | ||
bytes: (_c = part.image) != null ? _c : part.image | ||
} | ||
} | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
case "image": { | ||
if (part.image instanceof URL) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Image URLs in user messages" | ||
case "tool": { | ||
for (let i2 = 0; i2 < content.length; i2++) { | ||
const part = content[i2]; | ||
bedrockContent.push({ | ||
toolResult: { | ||
toolUseId: part.toolCallId, | ||
content: [{ text: JSON.stringify(part.result) }] | ||
} | ||
}); | ||
} | ||
bedrockMessageContent.push({ | ||
image: { | ||
format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1], | ||
source: { | ||
bytes: (_c = part.image) != null ? _c : part.image | ||
} | ||
} | ||
}); | ||
break; | ||
} | ||
default: { | ||
const _exhaustiveCheck = role; | ||
throw new Error(`Unsupported role: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
messages.push({ | ||
role: "user", | ||
content: bedrockMessageContent | ||
}); | ||
messages.push({ role: "user", content: bedrockContent }); | ||
break; | ||
} | ||
case "assistant": { | ||
const toolUse = []; | ||
let text = ""; | ||
for (const part of content) { | ||
switch (part.type) { | ||
case "text": { | ||
text += part.text; | ||
break; | ||
const bedrockContent = []; | ||
for (const message of block.messages) { | ||
const { content } = message; | ||
for (let j = 0; j < content.length; j++) { | ||
const part = content[j]; | ||
switch (part.type) { | ||
case "text": { | ||
bedrockContent.push({ | ||
text: ( | ||
// trim the last text part if it's the last message in the block | ||
// because Bedrock does not allow trailing whitespace | ||
// in pre-filled assistant responses | ||
i === blocks.length - 1 && j === block.messages.length - 1 ? part.text.trim() : part.text | ||
) | ||
}); | ||
break; | ||
} | ||
case "tool-call": { | ||
bedrockContent.push({ | ||
toolUse: { | ||
toolUseId: part.toolCallId, | ||
name: part.toolName, | ||
input: part.args | ||
} | ||
}); | ||
break; | ||
} | ||
} | ||
case "tool-call": { | ||
toolUse.push({ | ||
toolUseId: part.toolCallId, | ||
name: part.toolName, | ||
input: part.args | ||
}); | ||
break; | ||
} | ||
default: { | ||
const _exhaustiveCheck = part; | ||
throw new Error(`Unsupported part: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
messages.push({ | ||
role: "assistant", | ||
content: [ | ||
...text ? [{ text }] : [], | ||
...toolUse.map((toolUse2) => ({ toolUse: toolUse2 })) | ||
] | ||
}); | ||
messages.push({ role: "assistant", content: bedrockContent }); | ||
break; | ||
} | ||
case "tool": | ||
messages.push({ | ||
role: "user", | ||
content: content.map((part) => ({ | ||
toolResult: { | ||
toolUseId: part.toolCallId, | ||
status: part.isError ? "error" : "success", | ||
content: [{ text: JSON.stringify(part.result) }] | ||
} | ||
})) | ||
}); | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported type: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
return { | ||
system, | ||
messages | ||
}; | ||
} | ||
function groupIntoBlocks(prompt) { | ||
const blocks = []; | ||
let currentBlock = void 0; | ||
for (const message of prompt) { | ||
const { role } = message; | ||
switch (role) { | ||
case "system": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "system") { | ||
currentBlock = { type: "system", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push(message); | ||
break; | ||
} | ||
case "assistant": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "assistant") { | ||
currentBlock = { type: "assistant", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push(message); | ||
break; | ||
} | ||
case "user": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") { | ||
currentBlock = { type: "user", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push(message); | ||
break; | ||
} | ||
case "tool": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") { | ||
currentBlock = { type: "user", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push(message); | ||
break; | ||
} | ||
default: { | ||
throw new Error(`Unsupported role: ${role}`); | ||
const _exhaustiveCheck = role; | ||
throw new Error(`Unsupported role: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
return { system, messages }; | ||
return blocks; | ||
} | ||
@@ -139,0 +205,0 @@ |
# @ai-sdk/amazon-bedrock | ||
## 0.0.27 | ||
### Patch Changes | ||
- 8a15307: fix (provider/bedrock): support assistant messages with trailing whitespace | ||
## 0.0.26 | ||
@@ -4,0 +10,0 @@ |
@@ -41,98 +41,164 @@ "use strict"; | ||
var _a, _b, _c; | ||
const blocks = groupIntoBlocks(prompt); | ||
let system = void 0; | ||
const messages = []; | ||
for (const { role, content } of prompt) { | ||
switch (role) { | ||
for (let i = 0; i < blocks.length; i++) { | ||
const block = blocks[i]; | ||
const type = block.type; | ||
switch (type) { | ||
case "system": { | ||
if (messages.length > 0) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "System message after non-system message" | ||
functionality: "Multiple system messages that are separated by user/assistant messages" | ||
}); | ||
} | ||
system = system === void 0 ? content : `${system} | ||
${content}`; | ||
system = block.messages.map(({ content }) => content).join("\n"); | ||
break; | ||
} | ||
case "user": { | ||
const bedrockMessageContent = []; | ||
for (const part of content) { | ||
switch (part.type) { | ||
case "text": { | ||
bedrockMessageContent.push({ text: part.text }); | ||
const bedrockContent = []; | ||
for (const message of block.messages) { | ||
const { role, content } = message; | ||
switch (role) { | ||
case "user": { | ||
for (let j = 0; j < content.length; j++) { | ||
const part = content[j]; | ||
switch (part.type) { | ||
case "text": { | ||
bedrockContent.push({ | ||
text: part.text | ||
}); | ||
break; | ||
} | ||
case "image": { | ||
if (part.image instanceof URL) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Image URLs in user messages" | ||
}); | ||
} | ||
bedrockContent.push({ | ||
image: { | ||
format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1], | ||
source: { | ||
bytes: (_c = part.image) != null ? _c : part.image | ||
} | ||
} | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
case "image": { | ||
if (part.image instanceof URL) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Image URLs in user messages" | ||
case "tool": { | ||
for (let i2 = 0; i2 < content.length; i2++) { | ||
const part = content[i2]; | ||
bedrockContent.push({ | ||
toolResult: { | ||
toolUseId: part.toolCallId, | ||
content: [{ text: JSON.stringify(part.result) }] | ||
} | ||
}); | ||
} | ||
bedrockMessageContent.push({ | ||
image: { | ||
format: (_b = (_a = part.mimeType) == null ? void 0 : _a.split("/")) == null ? void 0 : _b[1], | ||
source: { | ||
bytes: (_c = part.image) != null ? _c : part.image | ||
} | ||
} | ||
}); | ||
break; | ||
} | ||
default: { | ||
const _exhaustiveCheck = role; | ||
throw new Error(`Unsupported role: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
messages.push({ | ||
role: "user", | ||
content: bedrockMessageContent | ||
}); | ||
messages.push({ role: "user", content: bedrockContent }); | ||
break; | ||
} | ||
case "assistant": { | ||
const toolUse = []; | ||
let text = ""; | ||
for (const part of content) { | ||
switch (part.type) { | ||
case "text": { | ||
text += part.text; | ||
break; | ||
const bedrockContent = []; | ||
for (const message of block.messages) { | ||
const { content } = message; | ||
for (let j = 0; j < content.length; j++) { | ||
const part = content[j]; | ||
switch (part.type) { | ||
case "text": { | ||
bedrockContent.push({ | ||
text: ( | ||
// trim the last text part if it's the last message in the block | ||
// because Bedrock does not allow trailing whitespace | ||
// in pre-filled assistant responses | ||
i === blocks.length - 1 && j === block.messages.length - 1 ? part.text.trim() : part.text | ||
) | ||
}); | ||
break; | ||
} | ||
case "tool-call": { | ||
bedrockContent.push({ | ||
toolUse: { | ||
toolUseId: part.toolCallId, | ||
name: part.toolName, | ||
input: part.args | ||
} | ||
}); | ||
break; | ||
} | ||
} | ||
case "tool-call": { | ||
toolUse.push({ | ||
toolUseId: part.toolCallId, | ||
name: part.toolName, | ||
input: part.args | ||
}); | ||
break; | ||
} | ||
default: { | ||
const _exhaustiveCheck = part; | ||
throw new Error(`Unsupported part: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
messages.push({ | ||
role: "assistant", | ||
content: [ | ||
...text ? [{ text }] : [], | ||
...toolUse.map((toolUse2) => ({ toolUse: toolUse2 })) | ||
] | ||
}); | ||
messages.push({ role: "assistant", content: bedrockContent }); | ||
break; | ||
} | ||
case "tool": | ||
messages.push({ | ||
role: "user", | ||
content: content.map((part) => ({ | ||
toolResult: { | ||
toolUseId: part.toolCallId, | ||
status: part.isError ? "error" : "success", | ||
content: [{ text: JSON.stringify(part.result) }] | ||
} | ||
})) | ||
}); | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported type: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
return { | ||
system, | ||
messages | ||
}; | ||
} | ||
function groupIntoBlocks(prompt) { | ||
const blocks = []; | ||
let currentBlock = void 0; | ||
for (const message of prompt) { | ||
const { role } = message; | ||
switch (role) { | ||
case "system": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "system") { | ||
currentBlock = { type: "system", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push(message); | ||
break; | ||
} | ||
case "assistant": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "assistant") { | ||
currentBlock = { type: "assistant", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push(message); | ||
break; | ||
} | ||
case "user": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") { | ||
currentBlock = { type: "user", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push(message); | ||
break; | ||
} | ||
case "tool": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") { | ||
currentBlock = { type: "user", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push(message); | ||
break; | ||
} | ||
default: { | ||
throw new Error(`Unsupported role: ${role}`); | ||
const _exhaustiveCheck = role; | ||
throw new Error(`Unsupported role: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
return { system, messages }; | ||
return blocks; | ||
} | ||
@@ -139,0 +205,0 @@ |
{ | ||
"name": "@ai-sdk/amazon-bedrock", | ||
"version": "0.0.26", | ||
"version": "0.0.27", | ||
"license": "Apache-2.0", | ||
@@ -5,0 +5,0 @@ "sideEffects": false, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
141077
1798