include attachments

This commit is contained in:
2026-07-10 15:38:52 +08:00
parent 2b7f4ffe19
commit c077767e0d
@@ -32,7 +32,13 @@ use crate::auth::AuthenticatedAccessToken;
use crate::editor_agent::agent::LlmChatAgentBuilder;
use crate::editor_agent::editor_tools::common::EditorToolContext;
use crate::editor_agent::editor_tools::edit_image::{EditImageTool, EditImageToolArgs};
use crate::editor_agent::utils::{EditorAgentMessageResponse, ImageId, ImageMetadata, build_editor_agent_canvas_completion, conversation_detail_from_record, conversation_summary_from_record, editor_agent_bad_request, empty_messages_document, ensure_editor_project_access, normalize_editor_agent_attachments, now_rfc3339, read_messages_document, require_editor_agent_sidebar_enabled, write_messages_document, IntoImageId};
use crate::editor_agent::utils::{
EditorAgentMessageResponse, ImageId, ImageMetadata, IntoImageId,
build_editor_agent_canvas_completion, conversation_detail_from_record,
conversation_summary_from_record, editor_agent_bad_request, empty_messages_document,
ensure_editor_project_access, normalize_editor_agent_attachments, now_rfc3339,
read_messages_document, require_editor_agent_sidebar_enabled, write_messages_document,
};
use crate::editor_project::{EditorGenerationCaller, current_utc_micros, map_editor_project_error};
use crate::http_error::AppError;
use crate::request_context::RequestContext;
@@ -72,20 +78,19 @@ pub async fn editor_agent_message(
let now = now_rfc3339();
if !attachments.is_empty() {
let mut attachment_info = String::new();
attachment_info.push_str("user has just uploaded attachments of the order: ");
for a in &attachments {
attachment_info.push_str(&format!("{} ,", a.clone().into_image_id()))
}
document.messages.push(EditorAgentMessage{
id: 0,
role: EditorAgentMessageRole::System,
text: attachment_info,
attachments:Vec::new(),
tool_call: None,
created_at: now.clone(),
});
let mut attachment_info = String::new();
attachment_info.push_str("user has just uploaded attachments of the order: ");
for a in &attachments {
attachment_info.push_str(&format!("{} ,", a.clone().into_image_id()))
}
document.messages.push(EditorAgentMessage {
id: 0,
role: EditorAgentMessageRole::System,
text: attachment_info,
attachments: Vec::new(),
tool_call: None,
created_at: now.clone(),
});
}
// Build conversation history as LlmMessage vec
@@ -230,6 +235,13 @@ fn build_tool_context(document: &EditorAgentConversationMessagesDocument) -> Edi
break;
}
}
for a in &msg.attachments {
let image_id = ImageId {
id: a.object_key.clone().unwrap_or_default(),
};
let metadata = ImageMetadata { tag: String::new() };
images.insert(image_id, metadata);
}
}
EditorToolContext { images }
@@ -410,9 +422,10 @@ pub async fn cancel_editor_agent_tool_call(
if msg.role != EditorAgentMessageRole::System {
return Err(editor_agent_bad_request("message is not a system message"));
}
let tc = msg.tool_call.as_mut().ok_or_else(|| {
editor_agent_bad_request("message has no tool call")
})?;
let tc = msg
.tool_call
.as_mut()
.ok_or_else(|| editor_agent_bad_request("message has no tool call"))?;
if tc.status != EditorAgentToolCallStatus::PendingConfirmation {
return Err(editor_agent_bad_request(
"tool call is not in PendingConfirmation status",
@@ -474,9 +487,10 @@ pub async fn confirm_editor_agent_tool_call(
if msg.role != EditorAgentMessageRole::System {
return Err(editor_agent_bad_request("message is not a system message"));
}
let tc = msg.tool_call.as_ref().ok_or_else(|| {
editor_agent_bad_request("message has no tool call")
})?;
let tc = msg
.tool_call
.as_ref()
.ok_or_else(|| editor_agent_bad_request("message has no tool call"))?;
if tc.status != EditorAgentToolCallStatus::PendingConfirmation {
return Err(editor_agent_bad_request(
"tool call is not in PendingConfirmation status",
@@ -487,9 +501,7 @@ pub async fn confirm_editor_agent_tool_call(
match tc.tool_name.as_str() {
EditImageTool::NAME => {
let args: EditImageToolArgs = serde_json::from_value(tc.args.clone())
.map_err(|e| {
editor_agent_bad_request(format!("invalid tool call args: {e}"))
})?;
.map_err(|e| editor_agent_bad_request(format!("invalid tool call args: {e}")))?;
// Get project
let project = state
@@ -518,18 +530,20 @@ pub async fn confirm_editor_agent_tool_call(
// Execute the real generation
let tool_context = build_tool_context(&document);
let edit_tool = EditImageTool { context: tool_context };
let edit_tool = EditImageTool {
context: tool_context,
};
let result = edit_tool
.execute(
&state,
&request_context,
caller,
args.clone(),
None, // model
None, // model
Some(conversation.project_id.clone()), // project_id
None, // generation_inputs
None, // asset_label
None, // source_resource_id
None, // generation_inputs
None, // asset_label
None, // source_resource_id
Some(canvas_completion),
)
.await?;