clean code
This commit is contained in:
Generated
-2
@@ -3152,12 +3152,10 @@ dependencies = [
|
||||
name = "module-editor-agent"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"platform-llm",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"shared-kernel",
|
||||
"spacetimedb",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
||||
@@ -6,20 +6,20 @@ use module_editor_agent::agent::agent_builder::AgentBuilder;
|
||||
use module_editor_agent::agent::error::PromptError;
|
||||
use module_editor_agent::agent::memory::VecMemory;
|
||||
use module_editor_agent::agent::run::PromptOutput;
|
||||
use module_editor_agent::agent::tool::Tool;
|
||||
use module_editor_agent::{
|
||||
derive_conversation_title, editor_agent_messages_object_key,
|
||||
EDITOR_AGENT_CONVERSATION_ID_PREFIX, EDITOR_AGENT_DEFAULT_CONVERSATION_TITLE,
|
||||
derive_conversation_title, editor_agent_messages_object_key,
|
||||
};
|
||||
use platform_llm::LlmMessage;
|
||||
use serde::Serialize;
|
||||
use serde_json::{Value, json};
|
||||
use module_editor_agent::agent::tool::Tool;
|
||||
use shared_contracts::editor_agent::{
|
||||
CreateEditorAgentConversationRequest, EditorAgentConversationListResponse,
|
||||
EditorAgentConversationMessagesDocument, EditorAgentConversationResponse,
|
||||
EditorAgentConversationSummary, EditorAgentGeneratedImage, EditorAgentMessage,
|
||||
EditorAgentMessageRole, EditorAgentToolCall, EditorAgentToolCallStatus,
|
||||
EditorAgentMessageRequest,
|
||||
CreateEditorAgentConversationRequest, EditorAgentConversationListResponse,
|
||||
EditorAgentConversationMessagesDocument, EditorAgentConversationResponse,
|
||||
EditorAgentConversationSummary, EditorAgentGeneratedImage, EditorAgentMessage,
|
||||
EditorAgentMessageRequest, EditorAgentMessageRole, EditorAgentToolCall,
|
||||
EditorAgentToolCallStatus,
|
||||
};
|
||||
use spacetime_client::{
|
||||
EditorAgentConversationCreateRecordInput, EditorAgentConversationDeleteRecordInput,
|
||||
@@ -27,29 +27,23 @@ use spacetime_client::{
|
||||
EditorProjectGetRecordInput,
|
||||
};
|
||||
|
||||
use crate::api_response::json_success_body;
|
||||
use crate::auth::AuthenticatedAccessToken;
|
||||
use crate::editor_agent::agent::LlmChatAgentBuilder;
|
||||
use crate::editor_agent::editor_tools::edit_image::{EditImageTool, EditImageToolArgs};
|
||||
use crate::editor_agent::utils::{
|
||||
build_editor_agent_canvas_completion, conversation_detail_from_record,
|
||||
conversation_summary_from_record, editor_agent_bad_request, empty_messages_document,
|
||||
ensure_editor_project_access, EditorAgentMessageResponse, ImageId, ImageMetadata,
|
||||
normalize_editor_agent_attachments, now_rfc3339, read_messages_document,
|
||||
require_editor_agent_sidebar_enabled, write_messages_document,
|
||||
};
|
||||
use crate::editor_project::{current_utc_micros, map_editor_project_error, EditorGenerationCaller};
|
||||
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_project::{EditorGenerationCaller, current_utc_micros, map_editor_project_error};
|
||||
use crate::http_error::AppError;
|
||||
use crate::request_context::RequestContext;
|
||||
use crate::api_response::json_success_body;
|
||||
use crate::state::AppState;
|
||||
use shared_kernel::{build_prefixed_uuid_id, normalize_optional_string};
|
||||
|
||||
pub async fn editor_agent_message(
|
||||
State(state): State<AppState>,
|
||||
Path(conversation_id): Path<String>,
|
||||
Extension(_request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
Json(payload): Json<EditorAgentMessageRequest>,
|
||||
State(state): State<AppState>,
|
||||
Path(conversation_id): Path<String>,
|
||||
Extension(_request_context): Extension<RequestContext>,
|
||||
Extension(authenticated): Extension<AuthenticatedAccessToken>,
|
||||
Json(payload): Json<EditorAgentMessageRequest>,
|
||||
) -> Result<Json<EditorAgentMessageResponse>, AppError> {
|
||||
let owner_user_id = authenticated.claims().user_id().to_string();
|
||||
require_editor_agent_sidebar_enabled(&state, owner_user_id.as_str()).await?;
|
||||
@@ -63,12 +57,9 @@ pub async fn editor_agent_message(
|
||||
.with_details(json!({ "message": format!("conversation not found: {e}") }))
|
||||
})?;
|
||||
|
||||
let attachments = normalize_editor_agent_attachments(
|
||||
&state,
|
||||
&conversation,
|
||||
payload.attachments.as_slice(),
|
||||
)
|
||||
.await?;
|
||||
let attachments =
|
||||
normalize_editor_agent_attachments(&state, &conversation, payload.attachments.as_slice())
|
||||
.await?;
|
||||
|
||||
let conversation_lock = crate::editor_agent::utils::editor_agent_conversation_lock(
|
||||
conversation.conversation_id.as_str(),
|
||||
@@ -78,6 +69,14 @@ pub async fn editor_agent_message(
|
||||
let mut document: EditorAgentConversationMessagesDocument =
|
||||
read_messages_document(&state, &conversation).await?;
|
||||
|
||||
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()))
|
||||
}
|
||||
}
|
||||
|
||||
// Build conversation history as LlmMessage vec
|
||||
let previous_messages: Vec<LlmMessage> = document
|
||||
.messages
|
||||
@@ -422,7 +421,10 @@ pub async fn cancel_editor_agent_tool_call(
|
||||
|
||||
write_messages_document(&state, &conversation, &document).await?;
|
||||
|
||||
Ok(json_success_body(Some(&request_context), &document.messages[message_id]))
|
||||
Ok(json_success_body(
|
||||
Some(&request_context),
|
||||
&document.messages[message_id],
|
||||
))
|
||||
}
|
||||
|
||||
pub async fn confirm_editor_agent_tool_call(
|
||||
|
||||
@@ -12,6 +12,7 @@ use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Value, json};
|
||||
use shared_contracts::api::ApiSuccessEnvelope;
|
||||
use shared_contracts::assets::EditorCanvasGenerationCompletionPayload;
|
||||
use std::collections::HashMap;
|
||||
use std::error::Error;
|
||||
use std::fmt::Display;
|
||||
|
||||
@@ -190,6 +191,7 @@ impl EditImageTool {
|
||||
let name = Self::NAME.to_string();
|
||||
let arg_json = serde_json::to_string(&args).unwrap_or_default();
|
||||
|
||||
// TODO use the trait
|
||||
let image_id: ImageId = ImageId {
|
||||
id: result
|
||||
.object_key
|
||||
@@ -217,6 +219,7 @@ impl EditImageTool {
|
||||
source_resource_id: Option<String>,
|
||||
canvas_completion: Option<EditorCanvasGenerationCompletionPayload>,
|
||||
) -> Result<EditorImageEditResult, AppError> {
|
||||
// TODO id should not be assumed as src key
|
||||
let source_image_src = args.object_image_id.id;
|
||||
let reference_image_srcs: Vec<String> = args
|
||||
.reference_image_ids
|
||||
|
||||
@@ -23,7 +23,7 @@ use std::collections::BTreeMap;
|
||||
use std::fmt::Display;
|
||||
use std::sync::{Arc, Mutex, OnceLock};
|
||||
|
||||
trait IntoDataKey {
|
||||
pub trait IntoDataKey {
|
||||
fn into_data_key(self) -> String;
|
||||
}
|
||||
impl IntoDataKey for EditorAgentAttachmentRef {
|
||||
@@ -59,7 +59,7 @@ impl IntoDataKey for EditorAssetRecord {
|
||||
}
|
||||
}
|
||||
|
||||
trait IntoImageId {
|
||||
pub trait IntoImageId {
|
||||
fn into_image_id(self) -> ImageId;
|
||||
}
|
||||
impl IntoImageId for EditorAgentAttachmentRef {
|
||||
@@ -84,7 +84,7 @@ impl IntoImageId for EditorAssetRecord {
|
||||
}
|
||||
}
|
||||
|
||||
trait IntoResourceId {
|
||||
pub trait IntoResourceId {
|
||||
fn into_resource_id(self) -> String;
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ pub struct EditorAgentMessageResponse {
|
||||
type EditorAgentConversationLockMap = Mutex<BTreeMap<String, Arc<tokio::sync::Mutex<()>>>>;
|
||||
static EDITOR_AGENT_CONVERSATION_LOCKS: OnceLock<EditorAgentConversationLockMap> = OnceLock::new();
|
||||
|
||||
pub(crate) fn editor_agent_conversation_lock(conversation_id: &str) -> Arc<tokio::sync::Mutex<()>> {
|
||||
pub fn editor_agent_conversation_lock(conversation_id: &str) -> Arc<tokio::sync::Mutex<()>> {
|
||||
let locks = EDITOR_AGENT_CONVERSATION_LOCKS.get_or_init(|| Mutex::new(BTreeMap::new()));
|
||||
let mut locks = locks
|
||||
.lock()
|
||||
@@ -277,7 +277,7 @@ pub async fn write_messages_document(
|
||||
|
||||
const EDITOR_AGENT_MESSAGES_READ_EXPIRE_SECONDS: u64 = 60;
|
||||
|
||||
pub(crate) async fn read_messages_document(
|
||||
pub async fn read_messages_document(
|
||||
state: &AppState,
|
||||
conversation: &EditorAgentConversationRecord,
|
||||
) -> Result<EditorAgentConversationMessagesDocument, AppError> {
|
||||
|
||||
Reference in New Issue
Block a user