@ai-sdk/anthropic
Advanced tools
Comparing version 0.0.23 to 0.0.24
@@ -59,13 +59,15 @@ "use strict"; | ||
}) { | ||
const blocks = groupIntoBlocks(prompt); | ||
let system = void 0; | ||
const messages = []; | ||
for (const { role, content } of prompt) { | ||
switch (role) { | ||
for (const block of blocks) { | ||
const type = block.type; | ||
switch (type) { | ||
case "system": { | ||
if (system != null) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Multiple system messages" | ||
functionality: "Multiple system messages that are separated by user/assistant messages" | ||
}); | ||
} | ||
system = content; | ||
system = block.messages.map(({ content }) => content).join("\n"); | ||
break; | ||
@@ -75,31 +77,53 @@ } | ||
const anthropicContent = []; | ||
for (const part of content) { | ||
switch (part.type) { | ||
case "text": { | ||
anthropicContent.push({ type: "text", text: part.text }); | ||
for (const { role, content } of block.messages) { | ||
switch (role) { | ||
case "user": { | ||
for (const part of content) { | ||
switch (part.type) { | ||
case "text": { | ||
anthropicContent.push({ type: "text", text: part.text }); | ||
break; | ||
} | ||
case "image": { | ||
let data; | ||
let mimeType; | ||
if (part.image instanceof URL) { | ||
const downloadResult = await downloadImplementation({ | ||
url: part.image | ||
}); | ||
data = downloadResult.data; | ||
mimeType = downloadResult.mimeType; | ||
} else { | ||
data = part.image; | ||
mimeType = part.mimeType; | ||
} | ||
anthropicContent.push({ | ||
type: "image", | ||
source: { | ||
type: "base64", | ||
media_type: mimeType != null ? mimeType : "image/jpeg", | ||
data: (0, import_provider_utils2.convertUint8ArrayToBase64)(data) | ||
} | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
case "image": { | ||
let data; | ||
let mimeType; | ||
if (part.image instanceof URL) { | ||
const downloadResult = await downloadImplementation({ | ||
url: part.image | ||
case "tool": { | ||
for (const part of content) { | ||
anthropicContent.push({ | ||
type: "tool_result", | ||
tool_use_id: part.toolCallId, | ||
content: JSON.stringify(part.result), | ||
is_error: part.isError | ||
}); | ||
data = downloadResult.data; | ||
mimeType = downloadResult.mimeType; | ||
} else { | ||
data = part.image; | ||
mimeType = part.mimeType; | ||
} | ||
anthropicContent.push({ | ||
type: "image", | ||
source: { | ||
type: "base64", | ||
media_type: mimeType != null ? mimeType : "image/jpeg", | ||
data: (0, import_provider_utils2.convertUint8ArrayToBase64)(data) | ||
} | ||
}); | ||
break; | ||
} | ||
default: { | ||
const _exhaustiveCheck = role; | ||
throw new Error(`Unsupported role: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
@@ -111,2 +135,8 @@ } | ||
case "assistant": { | ||
if (block.messages.length > 1) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Multiple assistant messages in block" | ||
}); | ||
} | ||
const { content } = block.messages[0]; | ||
messages.push({ | ||
@@ -132,12 +162,48 @@ role: "assistant", | ||
} | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported type: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
return { | ||
system, | ||
messages | ||
}; | ||
} | ||
function groupIntoBlocks(prompt) { | ||
const blocks = []; | ||
let currentBlock = void 0; | ||
for (const { role, content } of prompt) { | ||
switch (role) { | ||
case "system": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "system") { | ||
currentBlock = { type: "system", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push({ role, content }); | ||
break; | ||
} | ||
case "assistant": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "assistant") { | ||
currentBlock = { type: "assistant", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push({ role, content }); | ||
break; | ||
} | ||
case "user": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") { | ||
currentBlock = { type: "user", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push({ role, content }); | ||
break; | ||
} | ||
case "tool": { | ||
messages.push({ | ||
role: "user", | ||
content: content.map((part) => ({ | ||
type: "tool_result", | ||
tool_use_id: part.toolCallId, | ||
content: JSON.stringify(part.result), | ||
is_error: part.isError | ||
})) | ||
}); | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") { | ||
currentBlock = { type: "user", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push({ role, content }); | ||
break; | ||
@@ -151,6 +217,3 @@ } | ||
} | ||
return { | ||
system, | ||
messages | ||
}; | ||
return blocks; | ||
} | ||
@@ -157,0 +220,0 @@ |
@@ -59,13 +59,15 @@ "use strict"; | ||
}) { | ||
const blocks = groupIntoBlocks(prompt); | ||
let system = void 0; | ||
const messages = []; | ||
for (const { role, content } of prompt) { | ||
switch (role) { | ||
for (const block of blocks) { | ||
const type = block.type; | ||
switch (type) { | ||
case "system": { | ||
if (system != null) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Multiple system messages" | ||
functionality: "Multiple system messages that are separated by user/assistant messages" | ||
}); | ||
} | ||
system = content; | ||
system = block.messages.map(({ content }) => content).join("\n"); | ||
break; | ||
@@ -75,31 +77,53 @@ } | ||
const anthropicContent = []; | ||
for (const part of content) { | ||
switch (part.type) { | ||
case "text": { | ||
anthropicContent.push({ type: "text", text: part.text }); | ||
for (const { role, content } of block.messages) { | ||
switch (role) { | ||
case "user": { | ||
for (const part of content) { | ||
switch (part.type) { | ||
case "text": { | ||
anthropicContent.push({ type: "text", text: part.text }); | ||
break; | ||
} | ||
case "image": { | ||
let data; | ||
let mimeType; | ||
if (part.image instanceof URL) { | ||
const downloadResult = await downloadImplementation({ | ||
url: part.image | ||
}); | ||
data = downloadResult.data; | ||
mimeType = downloadResult.mimeType; | ||
} else { | ||
data = part.image; | ||
mimeType = part.mimeType; | ||
} | ||
anthropicContent.push({ | ||
type: "image", | ||
source: { | ||
type: "base64", | ||
media_type: mimeType != null ? mimeType : "image/jpeg", | ||
data: (0, import_provider_utils2.convertUint8ArrayToBase64)(data) | ||
} | ||
}); | ||
break; | ||
} | ||
} | ||
} | ||
break; | ||
} | ||
case "image": { | ||
let data; | ||
let mimeType; | ||
if (part.image instanceof URL) { | ||
const downloadResult = await downloadImplementation({ | ||
url: part.image | ||
case "tool": { | ||
for (const part of content) { | ||
anthropicContent.push({ | ||
type: "tool_result", | ||
tool_use_id: part.toolCallId, | ||
content: JSON.stringify(part.result), | ||
is_error: part.isError | ||
}); | ||
data = downloadResult.data; | ||
mimeType = downloadResult.mimeType; | ||
} else { | ||
data = part.image; | ||
mimeType = part.mimeType; | ||
} | ||
anthropicContent.push({ | ||
type: "image", | ||
source: { | ||
type: "base64", | ||
media_type: mimeType != null ? mimeType : "image/jpeg", | ||
data: (0, import_provider_utils2.convertUint8ArrayToBase64)(data) | ||
} | ||
}); | ||
break; | ||
} | ||
default: { | ||
const _exhaustiveCheck = role; | ||
throw new Error(`Unsupported role: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
@@ -111,2 +135,8 @@ } | ||
case "assistant": { | ||
if (block.messages.length > 1) { | ||
throw new import_provider.UnsupportedFunctionalityError({ | ||
functionality: "Multiple assistant messages in block" | ||
}); | ||
} | ||
const { content } = block.messages[0]; | ||
messages.push({ | ||
@@ -132,12 +162,48 @@ role: "assistant", | ||
} | ||
default: { | ||
const _exhaustiveCheck = type; | ||
throw new Error(`Unsupported type: ${_exhaustiveCheck}`); | ||
} | ||
} | ||
} | ||
return { | ||
system, | ||
messages | ||
}; | ||
} | ||
function groupIntoBlocks(prompt) { | ||
const blocks = []; | ||
let currentBlock = void 0; | ||
for (const { role, content } of prompt) { | ||
switch (role) { | ||
case "system": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "system") { | ||
currentBlock = { type: "system", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push({ role, content }); | ||
break; | ||
} | ||
case "assistant": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "assistant") { | ||
currentBlock = { type: "assistant", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push({ role, content }); | ||
break; | ||
} | ||
case "user": { | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") { | ||
currentBlock = { type: "user", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push({ role, content }); | ||
break; | ||
} | ||
case "tool": { | ||
messages.push({ | ||
role: "user", | ||
content: content.map((part) => ({ | ||
type: "tool_result", | ||
tool_use_id: part.toolCallId, | ||
content: JSON.stringify(part.result), | ||
is_error: part.isError | ||
})) | ||
}); | ||
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") { | ||
currentBlock = { type: "user", messages: [] }; | ||
blocks.push(currentBlock); | ||
} | ||
currentBlock.messages.push({ role, content }); | ||
break; | ||
@@ -151,6 +217,3 @@ } | ||
} | ||
return { | ||
system, | ||
messages | ||
}; | ||
return blocks; | ||
} | ||
@@ -157,0 +220,0 @@ |
{ | ||
"name": "@ai-sdk/anthropic", | ||
"version": "0.0.23", | ||
"version": "0.0.24", | ||
"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
157009
2141