补全画布历史图片的原始提示词回填
This commit is contained in:
parent
18e2e09bb9
commit
35c3a8d619
@ -162,6 +162,21 @@ function findNearestUserPrompt(messages: ChatMessage[], assistantIndex: number):
|
||||
return "";
|
||||
}
|
||||
|
||||
const GENERIC_CANVAS_PROMPT_SET = new Set([
|
||||
"对话生成",
|
||||
"图片生成",
|
||||
"AI生成",
|
||||
"智能生成",
|
||||
"自动生成",
|
||||
]);
|
||||
|
||||
function isGenericCanvasPrompt(prompt: unknown): boolean {
|
||||
if (typeof prompt !== "string") return true;
|
||||
const normalized = prompt.trim();
|
||||
if (!normalized) return true;
|
||||
return GENERIC_CANVAS_PROMPT_SET.has(normalized);
|
||||
}
|
||||
|
||||
// 🆕 拾取物品的附件类型扩展
|
||||
interface PickedObjectAttachment extends Attachment {
|
||||
pickedObject?: {
|
||||
@ -241,6 +256,7 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
|
||||
|
||||
// Canvas store
|
||||
const addNode = useCanvasStore((state) => state.addNode);
|
||||
const updateNode = useCanvasStore((state) => state.updateNode);
|
||||
const nodes = useCanvasStore((state) => state.nodes);
|
||||
const selectedNodeIds = useCanvasStore((state) => state.selectedNodeIds);
|
||||
const clearSelection = useCanvasStore((state) => state.clearSelection);
|
||||
@ -756,6 +772,50 @@ export function CanvasChatPanel({ chatId, initialMessages = [], onClose, onOpenF
|
||||
});
|
||||
}, [messages, addNode, nodes]);
|
||||
|
||||
// 为历史节点补全可编辑的原始提示词(旧数据里可能只有“对话生成”占位文案)
|
||||
useEffect(() => {
|
||||
if (!messages.length || !nodes.length) return;
|
||||
|
||||
const promptByImageUrl = new Map<string, string>();
|
||||
messages.forEach((message, messageIndex) => {
|
||||
if (message.role !== "assistant") return;
|
||||
const content = message.parts?.map((p: any) => p.text || "").join("") || "";
|
||||
const imageUrls = extractImageUrls(content);
|
||||
if (!imageUrls.length) return;
|
||||
|
||||
const prompt = findNearestUserPrompt(messages, messageIndex);
|
||||
if (!prompt) return;
|
||||
|
||||
imageUrls.forEach((url) => {
|
||||
if (!promptByImageUrl.has(url)) {
|
||||
promptByImageUrl.set(url, prompt);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
nodes.forEach((node) => {
|
||||
if (node.type !== "image") return;
|
||||
const data = (node.data as any) || {};
|
||||
const src = data.src as string | undefined;
|
||||
if (!src) return;
|
||||
if (!isGenericCanvasPrompt(data.prompt)) return;
|
||||
|
||||
const prompt = promptByImageUrl.get(src);
|
||||
if (!prompt) return;
|
||||
|
||||
updateNode(node.id, {
|
||||
data: {
|
||||
...data,
|
||||
prompt,
|
||||
metadata: {
|
||||
...(data.metadata || {}),
|
||||
generationPrompt: prompt,
|
||||
},
|
||||
},
|
||||
});
|
||||
});
|
||||
}, [messages, nodes, updateNode]);
|
||||
|
||||
// 滚动到底部
|
||||
useEffect(() => {
|
||||
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user