@@ -2,6 +2,7 @@ use std::collections::HashSet;
22use std:: fmt:: Display ;
33use std:: fs;
44use std:: path:: Path ;
5+ use std:: path:: PathBuf ;
56// Copyright © 2025 Huly Labs. Use of this source code is governed by the MIT license.
67use std:: sync:: Arc ;
78
@@ -87,12 +88,11 @@ struct AgentConfigState {
8788}
8889
8990impl AgentConfigState {
90- pub fn new ( ) -> Self {
91- if Path :: new ( CONFIG_STATE_FILE_PATH ) . exists ( ) {
92- serde_yaml:: from_str (
93- & std:: fs:: read_to_string ( CONFIG_STATE_FILE_PATH ) . unwrap_or_default ( ) ,
94- )
95- . unwrap_or_default ( )
91+ pub fn new ( data_dir : & str ) -> Self {
92+ let path = Path :: new ( data_dir) . join ( CONFIG_STATE_FILE_PATH ) ;
93+ if path. exists ( ) {
94+ serde_yaml:: from_str ( & std:: fs:: read_to_string ( path) . unwrap_or_default ( ) )
95+ . unwrap_or_default ( )
9696 } else {
9797 Self {
9898 approved_tools : HashSet :: default ( ) ,
@@ -132,6 +132,7 @@ fn pending_tool_id<'a>(messages: RwLockReadGuard<'a, Vec<Message>>) -> Option<St
132132
133133struct AgentContext {
134134 config : Config ,
135+ data_dir : PathBuf ,
135136 config_state : Arc < RwLock < AgentConfigState > > ,
136137 messages : Arc < RwLock < Vec < Message > > > ,
137138 state : Arc < RwLock < AgentState > > ,
@@ -144,11 +145,15 @@ struct AgentContext {
144145}
145146
146147impl Agent {
147- pub fn new ( config : Config , sender : mpsc:: UnboundedSender < AgentOutputEvent > ) -> Self {
148+ pub fn new (
149+ data_dir : & str ,
150+ config : Config ,
151+ sender : mpsc:: UnboundedSender < AgentOutputEvent > ,
152+ ) -> Self {
148153 Self {
149154 config,
150155 sender,
151- memory : Arc :: new ( RwLock :: new ( MemoryManager :: new ( false ) ) ) ,
156+ memory : Arc :: new ( RwLock :: new ( MemoryManager :: new ( data_dir , false ) ) ) ,
152157 process_registry : Arc :: new ( RwLock :: new ( ProcessRegistry :: default ( ) ) ) ,
153158 }
154159 }
@@ -407,6 +412,7 @@ impl Agent {
407412
408413 pub async fn run (
409414 & mut self ,
415+ data_dir : & str ,
410416 receiver : mpsc:: UnboundedReceiver < AgentControlEvent > ,
411417 messages : Vec < Message > ,
412418 memory_index : InMemoryVectorIndex < rig_fastembed:: EmbeddingModel , Entity > ,
@@ -463,10 +469,11 @@ impl Agent {
463469 let memory_index = Arc :: new ( RwLock :: new ( memory_index) ) ;
464470 let sender = self . sender . clone ( ) ;
465471 let state = Arc :: new ( RwLock :: new ( state) ) ;
466- let config_state = Arc :: new ( RwLock :: new ( AgentConfigState :: new ( ) ) ) ;
472+ let config_state = Arc :: new ( RwLock :: new ( AgentConfigState :: new ( data_dir ) ) ) ;
467473
468474 let events_context = AgentContext {
469475 config : self . config . clone ( ) ,
476+ data_dir : PathBuf :: from ( data_dir) ,
470477 config_state : config_state. clone ( ) ,
471478 messages : messages. clone ( ) ,
472479 state : state. clone ( ) ,
@@ -480,6 +487,7 @@ impl Agent {
480487
481488 let process_context = AgentContext {
482489 config : self . config . clone ( ) ,
490+ data_dir : PathBuf :: from ( data_dir) ,
483491 config_state : config_state. clone ( ) ,
484492 messages : messages. clone ( ) ,
485493 state : state. clone ( ) ,
@@ -584,14 +592,14 @@ impl AgentContext {
584592 async fn persist_history ( & self ) {
585593 tracing:: debug!( "persist_history" ) ;
586594 let messages = self . messages . read ( ) . await ;
587- persist_history ( & messages) ;
595+ persist_history ( & self . data_dir , & messages) ;
588596 }
589597
590598 async fn persist_config_state ( & self ) {
591599 tracing:: debug!( "persist_config_state" ) ;
592600 let state = self . config_state . read ( ) . await ;
593601 fs:: write (
594- CONFIG_STATE_FILE_PATH ,
602+ self . data_dir . join ( CONFIG_STATE_FILE_PATH ) ,
595603 serde_yaml:: to_string ( & * state) . unwrap ( ) ,
596604 )
597605 . unwrap ( ) ;
0 commit comments