fix: 修复画布聊天面板发送消息格式

1. 修复 handleSend 中 parts 数组的顺序(文件在前,文本在后)
2. 添加 videoParams: undefined 确保 schema 兼容
3. 添加调试日志帮助排查问题

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Your Name 2026-02-01 12:22:41 +08:00
parent 057014900c
commit 339bf91019

View File

@ -153,13 +153,20 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
api: "/api/chat",
fetch: fetchWithErrorHandlers,
prepareSendMessagesRequest(request) {
console.log("[CanvasChat] Preparing request:", {
id: request.id,
modelId: currentModelIdRef.current,
imageParams: imageParamsRef.current,
lastMessage: request.messages.at(-1),
});
return {
body: {
id: request.id,
message: request.messages.at(-1),
selectedChatModel: currentModelIdRef.current,
selectedVisibilityType: "private",
imageParams: imageParamsRef.current, // 传递图片参数
videoParams: undefined, // 必须传递,即使是 undefined
imageParams: imageParamsRef.current || undefined, // 传递图片参数
...request.body,
},
};
@ -252,31 +259,28 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
return;
}
// 构建 parts 数组:文本 + 文件
// 构建 parts 数组:文件在前,文本在后(与 multimodal-input 一致)
const parts: any[] = [];
// 添加文本部分
if (input.trim()) {
parts.push({ type: "text", text: input });
}
// 添加文件部分(作为 file 类型的 part)
attachments.forEach(a => {
parts.push({
type: "file",
type: "file" as const,
mediaType: a.contentType,
name: a.name,
url: a.url,
});
});
// 如果没有文本但有附件,添加一个空的文本提示
if (!input.trim() && attachments.length > 0) {
parts.unshift({ type: "text", text: "请处理这张图片" });
// 添加文本部分
const textContent = input.trim() || (attachments.length > 0 ? "请处理这张图片" : "");
if (textContent) {
parts.push({ type: "text" as const, text: textContent });
}
// 使用与 multimodal-input 相同的格式发送消息
sendMessage({
role: "user" as const,
role: "user",
parts: parts,
});