mirror of
https://github.com/rowboatlabs/rowboat.git
synced 2026-04-25 00:16:29 +02:00
Merge 793a07904f into 51f2ad6e8a
This commit is contained in:
commit
7d8575009d
1 changed files with 41 additions and 13 deletions
|
|
@ -528,7 +528,8 @@ function formatBytes(bytes: number): string {
|
||||||
|
|
||||||
export function convertFromMessages(messages: z.infer<typeof Message>[]): ModelMessage[] {
|
export function convertFromMessages(messages: z.infer<typeof Message>[]): ModelMessage[] {
|
||||||
const result: ModelMessage[] = [];
|
const result: ModelMessage[] = [];
|
||||||
for (const msg of messages) {
|
for (let i = 0; i < messages.length; i++) {
|
||||||
|
const msg = messages[i];
|
||||||
const { providerOptions } = msg;
|
const { providerOptions } = msg;
|
||||||
switch (msg.role) {
|
switch (msg.role) {
|
||||||
case "assistant":
|
case "assistant":
|
||||||
|
|
@ -602,23 +603,50 @@ export function convertFromMessages(messages: z.infer<typeof Message>[]): ModelM
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "tool":
|
case "tool": {
|
||||||
|
// Collect all consecutive tool messages into a single message
|
||||||
|
// This is required by Anthropic's API which expects all tool_result blocks
|
||||||
|
// for parallel tool calls to be in a single message
|
||||||
|
const toolResults: Array<{
|
||||||
|
type: "tool-result";
|
||||||
|
toolCallId: string;
|
||||||
|
toolName: string;
|
||||||
|
output: { type: "text"; value: string };
|
||||||
|
}> = [];
|
||||||
|
|
||||||
|
// Add current tool message
|
||||||
|
toolResults.push({
|
||||||
|
type: "tool-result",
|
||||||
|
toolCallId: msg.toolCallId,
|
||||||
|
toolName: msg.toolName,
|
||||||
|
output: {
|
||||||
|
type: "text",
|
||||||
|
value: msg.content,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Collect any consecutive tool messages
|
||||||
|
while (i + 1 < messages.length && messages[i + 1].role === "tool") {
|
||||||
|
i++;
|
||||||
|
const nextMsg = messages[i] as z.infer<typeof ToolMessage>;
|
||||||
|
toolResults.push({
|
||||||
|
type: "tool-result",
|
||||||
|
toolCallId: nextMsg.toolCallId,
|
||||||
|
toolName: nextMsg.toolName,
|
||||||
|
output: {
|
||||||
|
type: "text",
|
||||||
|
value: nextMsg.content,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
result.push({
|
result.push({
|
||||||
role: "tool",
|
role: "tool",
|
||||||
content: [
|
content: toolResults,
|
||||||
{
|
|
||||||
type: "tool-result",
|
|
||||||
toolCallId: msg.toolCallId,
|
|
||||||
toolName: msg.toolName,
|
|
||||||
output: {
|
|
||||||
type: "text",
|
|
||||||
value: msg.content,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
providerOptions,
|
providerOptions,
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// doing this because: https://github.com/OpenRouterTeam/ai-sdk-provider/issues/262
|
// doing this because: https://github.com/OpenRouterTeam/ai-sdk-provider/issues/262
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue