diff --git a/packages/shared/src/contracts/editorAgent.ts b/packages/shared/src/contracts/editorAgent.ts index b85143eae..34185b756 100644 --- a/packages/shared/src/contracts/editorAgent.ts +++ b/packages/shared/src/contracts/editorAgent.ts @@ -1,6 +1,7 @@ // 画布Agent对话契约:会话元数据存 SpacetimeDB,消息正文整体存 OSS(editor-agent/{conversationId}.json)。 export const EDITOR_AGENT_MAX_ATTACHMENTS = 9; +export const EDITOR_AGENT_ATTACHMENT_LABEL_MAX_CODE_POINTS = 10; export const EDITOR_AGENT_ERROR_MESSAGE_PREFIX = 'ERROR '; export type EditorAgentMessageRole = 'user' | 'assistant' | 'system'; @@ -13,17 +14,39 @@ export type EditorAgentToolCallStatus = export type EditorAgentAttachmentSource = 'canvas_resource' | 'library_asset'; +function normalizeEditorAgentAttachmentLabel( + label: string | null | undefined, + fallback: string, +) { + const normalized = label?.trim() || fallback.trim() || '图片'; + return Array.from(normalized) + .slice(0, EDITOR_AGENT_ATTACHMENT_LABEL_MAX_CODE_POINTS) + .join(''); +} + export interface EditorAgentAttachmentRef { source: EditorAgentAttachmentSource; referenceId: string; objectKey?: string | null; imageSrc: string; thumbnailSrc?: string | null; - label?: string | null; + label: string; width?: number | null; height?: number | null; } +export function createEditorAgentAttachmentRef( + input: Omit & { + label?: string | null; + }, + fallbackLabel: string = input.referenceId, +): EditorAgentAttachmentRef { + return { + ...input, + label: normalizeEditorAgentAttachmentLabel(input.label, fallbackLabel), + }; +} + export interface EditorAgentGeneratedImage { resourceId?: string | null; objectKey?: string | null; diff --git a/server-rs/crates/api-server/src/editor_agent/api.rs b/server-rs/crates/api-server/src/editor_agent/api.rs index 3e075a3bb..a95d9870c 100644 --- a/server-rs/crates/api-server/src/editor_agent/api.rs +++ b/server-rs/crates/api-server/src/editor_agent/api.rs @@ -159,8 +159,14 @@ pub async fn editor_agent_message( let mut attachment_info = String::new(); attachment_info.push_str("user added these image ids to context: "); for (i, attachment) in attachments.iter().enumerate() { - attachment_info - .push_str(&format!("[{i}]: {}, ", attachment.clone().into_image_id())); + let image_label_str = match &attachment.label { + None => "".to_string(), + Some(label) => { + format!(" {label}") + } + }; + let image_id = attachment.clone().into_image_id(); + attachment_info.push_str(&format!("({i}{image_label_str}): {image_id}, ")); } document.messages.push(EditorAgentMessage { id: document.messages.len(), diff --git a/src/components/image-editor/EditorAgentConversation/AttachmentChip.tsx b/src/components/image-editor/EditorAgentConversation/AttachmentChip.tsx index 81b327f57..5d5d1f4a7 100644 --- a/src/components/image-editor/EditorAgentConversation/AttachmentChip.tsx +++ b/src/components/image-editor/EditorAgentConversation/AttachmentChip.tsx @@ -10,7 +10,7 @@ function AttachmentChip({ attachment: EditorAgentAttachmentRef; onRemove?: () => void; }) { - const label = attachment.label?.trim() || attachment.referenceId; + const label = attachment.label; return (