11use anda_cognitive_nexus:: { CognitiveNexus , ConceptPK } ;
22use anda_core:: {
3- Agent , AgentContext , AgentOutput , BoxError , CompletionRequest , Document , Documents , Message ,
4- Principal , Resource , StateFeatures , Tool , ToolSet , Usage , evaluate_tokens, update_resources,
3+ Agent , AgentContext , AgentOutput , BoxError , CacheExpiry , CacheFeatures , CompletionRequest ,
4+ Document , Documents , Message , Principal , Resource , StateFeatures , Tool , ToolSet , Usage ,
5+ evaluate_tokens, update_resources,
56} ;
67use anda_db:: { database:: AndaDB , index:: BTree } ;
78use anda_engine:: {
@@ -18,7 +19,7 @@ use anda_kip::{
1819 META_SELF_NAME , PERSON_SELF_KIP , PERSON_SYSTEM_KIP , PERSON_TYPE , SELF_INSTRUCTIONS ,
1920 SYSTEM_INSTRUCTIONS , parse_kml,
2021} ;
21- use std:: { collections:: BTreeMap , sync:: Arc } ;
22+ use std:: { collections:: BTreeMap , sync:: Arc , time :: Duration } ;
2223
2324#[ derive( Clone ) ]
2425pub struct Assistant {
@@ -188,29 +189,36 @@ impl Agent<AgentCtx> for Assistant {
188189 return Err ( "anonymous caller not allowed" . into ( ) ) ;
189190 }
190191
191- let user_name = ctx
192- . meta ( )
193- . user
194- . clone ( )
195- . unwrap_or_else ( || caller. to_string ( ) ) ;
192+ let caller_key = format ! ( "Running:{}" , caller) ;
193+ let created_at = unix_ms ( ) ;
194+ let ok = ctx
195+ . cache_set_if_not_exists (
196+ & caller_key,
197+ ( created_at, Some ( CacheExpiry :: TTL ( Duration :: from_secs ( 300 ) ) ) ) ,
198+ )
199+ . await ;
200+ if !ok {
201+ return Err ( "Only one prompt can run at a time for you" . into ( ) ) ;
202+ }
203+
196204 let caller_info = self
197205 . memory
198206 . describe_caller ( caller)
199207 . await
200208 . unwrap_or_else ( |_| {
201209 serde_json:: json!( {
202- "id" : caller. to_string( ) ,
203- "name" : user_name
210+ "type" : "Person" ,
211+ "name" : caller. to_string( ) ,
212+ "attributes" : { } ,
204213 } )
205214 } ) ;
206215
207216 let created_at = unix_ms ( ) ;
208217 let primer = self . memory . describe_primer ( ) . await ?;
209218 let instructions = format ! (
210219 "{}\n ---\n # Your Identity & Knowledge Domain Map\n {}\n " ,
211- SYSTEM_INSTRUCTIONS , primer
220+ self . system_instructions , primer
212221 ) ;
213-
214222 let ( mut conversations, mut cursor) = self
215223 . memory
216224 . list_conversations_by_user ( caller, None , Some ( 7 ) )
@@ -280,7 +288,7 @@ impl Agent<AgentCtx> for Assistant {
280288 format!(
281289 "Current Datetime: {}\n ---\n {}" ,
282290 rfc3339_datetime( created_at) . unwrap_or_else( || format!( "unix_ms:{created_at}" ) ) ,
283- Documents :: new( "history " . to_string( ) , history_docs)
291+ Documents :: new( "user_context " . to_string( ) , history_docs)
284292 )
285293 . into( ) ,
286294 ] ,
@@ -424,6 +432,7 @@ impl Agent<AgentCtx> for Assistant {
424432 Ok :: < ( ) , BoxError > ( ( ) )
425433 } ;
426434
435+ ctx. cache_delete ( & caller_key) . await ;
427436 match rt ( ) . await {
428437 Ok ( _) => { }
429438 Err ( err) => {
0 commit comments