| /* | |
| * Licensed to the Apache Software Foundation (ASF) under one | |
| * or more contributor license agreements. See the NOTICE file | |
| * distributed with this work for additional information | |
| * regarding copyright ownership. The ASF licenses this file | |
| * to you under the Apache License, Version 2.0 (the | |
| * "License"); you may not use this file except in compliance | |
| * with the License. You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, | |
| * software distributed under the License is distributed on an | |
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
| * KIND, either express or implied. See the License for the | |
| * specific language governing permissions and limitations | |
| * under the License. | |
| */ | |
| use super::*; | |
| use maka_agent_facts::{ | |
| durable::{BackendSpec, Trigger}, | |
| input::RunInput, | |
| memory::MemoryAdmission, | |
| }; | |
| use maka_event_log::applications::operations::OperationDispatchScope; | |
| impl Owner { | |
| pub(crate) async fn remember_conversation( | |
| self: &Arc<Self>, | |
| request: RememberConversation, | |
| operation: &RecoveryRef, | |
| scope: &Scope, | |
| dispatch: OperationDispatchScope, | |
| ) -> Result<NativeMutation<MemoryBatchResult>, String> { | |
| let access = self.access_for_scope(scope).await?; | |
| let spec = access.spec(); | |
| let destination = match request.scope { | |
| ConversationMemoryScope::Project => MemoryScope::Project { | |
| session_id: spec.session_id.clone(), | |
| }, | |
| ConversationMemoryScope::Personal => MemoryScope::Personal, | |
| }; | |
| self.memory_read_access(scope, &destination).await?; | |
| let (BackendSpec::Model { descriptor, .. }, Trigger::Message { message_id }) = | |
| (&spec.backend, &spec.trigger) | |
| else { | |
| return Err("agent_memory_user_message_required".into()); | |
| }; | |
| if spec.memory_admission != MemoryAdmission::Enabled { | |
| return Err("agent_memory_model_read_not_admitted".into()); | |
| } | |
| let run = access.execution().run_id; | |
| let input: RunInput = self | |
| .run_content(run, &spec.input) | |
| .await | |
| .map_err(|e| e.to_string())?; | |
| let RunInput::Message { | |
| source_messages, .. | |
| } = input | |
| else { | |
| return Err("agent_memory_user_message_required".into()); | |
| }; | |
| // A mixed queue batch can contain commands from other producers. | |
| let [source] = source_messages.as_slice() else { | |
| return Err("agent_memory_single_user_message_required".into()); | |
| }; | |
| if source.message.origin.is_some() || source.message.message_id != *message_id { | |
| return Err("agent_memory_user_message_required".into()); | |
| } | |
| let original = &source.unprepared_content.text; | |
| let writer = self.extraction_writer(scope).await?; | |
| let history = self | |
| .durable | |
| .with_data_writer(writer.clone()) | |
| .map_err(|e| e.to_string())? | |
| .session_history(spec.owner.clone(), spec.session_id.clone()); | |
| let admitted = history.run_source(run).await.map_err(|e| e.to_string())?; | |
| let instruction = original.clone(); | |
| let (intent, organize_instruction) = match request.mode { | |
| ConversationMemoryMode::Remember => { | |
| (MemoryExtractionIntent::Remember { instruction }, None) | |
| } | |
| ConversationMemoryMode::Organize => { | |
| (MemoryExtractionIntent::Organize, Some(instruction)) | |
| } | |
| }; | |
| let bytes = self | |
| .durable | |
| .content(run, descriptor.clone()) | |
| .await | |
| .map_err(|e| e.to_string())?; | |
| let model = crate::model::AcceptedModel::from_backend(&spec.backend, &bytes) | |
| .map_err(|e| e.to_string())?; | |
| self.extract_memories_authorized( | |
| ExtractMemories { | |
| session_id: spec.session_id.clone(), | |
| scope: destination, | |
| model: model.selection(), | |
| range: MemoryExtractionRange { | |
| after_sequence: 0, | |
| through_sequence: admitted.admission_sequence, | |
| }, | |
| intent, | |
| }, | |
| writer, | |
| Some(workflow::ConversationCommand { | |
| model, | |
| organize_instruction, | |
| }), | |
| operation, | |
| scope, | |
| dispatch, | |
| ) | |
| .await | |
| } | |
| } |