fix: sanitize bedrock tool call ids (#781)
This commit is contained in:
@@ -317,6 +317,11 @@ function buildSystemPrompt(
|
|||||||
return blocks;
|
return blocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function sanitizeToolCallId(id: string): string {
|
||||||
|
const sanitized = id.replace(/[^a-zA-Z0-9_-]/g, "_");
|
||||||
|
return sanitized.length > 64 ? sanitized.slice(0, 64) : sanitized;
|
||||||
|
}
|
||||||
|
|
||||||
function convertMessages(context: Context, model: Model<"bedrock-converse-stream">): Message[] {
|
function convertMessages(context: Context, model: Model<"bedrock-converse-stream">): Message[] {
|
||||||
const result: Message[] = [];
|
const result: Message[] = [];
|
||||||
const transformedMessages = transformMessages(context.messages, model);
|
const transformedMessages = transformMessages(context.messages, model);
|
||||||
@@ -359,7 +364,7 @@ function convertMessages(context: Context, model: Model<"bedrock-converse-stream
|
|||||||
break;
|
break;
|
||||||
case "toolCall":
|
case "toolCall":
|
||||||
contentBlocks.push({
|
contentBlocks.push({
|
||||||
toolUse: { toolUseId: c.id, name: c.name, input: c.arguments },
|
toolUse: { toolUseId: sanitizeToolCallId(c.id), name: c.name, input: c.arguments },
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case "thinking":
|
case "thinking":
|
||||||
@@ -404,7 +409,7 @@ function convertMessages(context: Context, model: Model<"bedrock-converse-stream
|
|||||||
// Add current tool result with all content blocks combined
|
// Add current tool result with all content blocks combined
|
||||||
toolResults.push({
|
toolResults.push({
|
||||||
toolResult: {
|
toolResult: {
|
||||||
toolUseId: m.toolCallId,
|
toolUseId: sanitizeToolCallId(m.toolCallId),
|
||||||
content: m.content.map((c) =>
|
content: m.content.map((c) =>
|
||||||
c.type === "image"
|
c.type === "image"
|
||||||
? { image: createImageBlock(c.mimeType, c.data) }
|
? { image: createImageBlock(c.mimeType, c.data) }
|
||||||
@@ -420,7 +425,7 @@ function convertMessages(context: Context, model: Model<"bedrock-converse-stream
|
|||||||
const nextMsg = transformedMessages[j] as ToolResultMessage;
|
const nextMsg = transformedMessages[j] as ToolResultMessage;
|
||||||
toolResults.push({
|
toolResults.push({
|
||||||
toolResult: {
|
toolResult: {
|
||||||
toolUseId: nextMsg.toolCallId,
|
toolUseId: sanitizeToolCallId(nextMsg.toolCallId),
|
||||||
content: nextMsg.content.map((c) =>
|
content: nextMsg.content.map((c) =>
|
||||||
c.type === "image"
|
c.type === "image"
|
||||||
? { image: createImageBlock(c.mimeType, c.data) }
|
? { image: createImageBlock(c.mimeType, c.data) }
|
||||||
|
|||||||
Reference in New Issue
Block a user