@@ -4,8 +4,8 @@ use openrouter_api::{
44 types:: chat:: { ChatCompletionRequest , Message , MessageContent } ,
55 OpenRouterClient ,
66} ;
7- use r2d2_sqlite :: SqliteConnectionManager ;
8- use serde_rusqlite :: from_row ;
7+ use deadpool_postgres :: Pool ;
8+ use crate :: db ;
99use tokio:: sync:: mpsc;
1010
1111use crate :: {
@@ -79,54 +79,6 @@ fn build_authors_note() -> &'static str {
7979**Current Mode:** Trickster (unless Sebook detected)]"#
8080}
8181
82- /// Retrieves relevant memories for the user from the database
83- /// Reference: AGENT_GUIDE.md section on Memory Context Injection
84- fn get_user_memories ( database : & r2d2:: Pool < SqliteConnectionManager > , user_id : u64 ) -> Result < Vec < Memory > > {
85- let db = database. get ( ) ?;
86- let mut statement = db. prepare ( "SELECT * FROM memory WHERE user_id = ? ORDER BY id DESC LIMIT 5" ) ?;
87-
88- let memories: Vec < Memory > = statement
89- . query_map ( [ user_id. to_string ( ) ] , |row| {
90- from_row :: < Memory > ( row) . map_err ( |_| rusqlite:: Error :: QueryReturnedNoRows )
91- } ) ?
92- . filter_map ( Result :: ok)
93- . collect ( ) ;
94-
95- Ok ( memories)
96- }
97-
98- /// Fetches relationships for users mentioned in the conversation
99- fn get_users_with_relationships ( database : & r2d2:: Pool < SqliteConnectionManager > , context : & str ) -> Result < Vec < ( String , String ) > > {
100- let db = database. get ( ) ?;
101- let mut statement = db. prepare ( "SELECT name, relationship FROM user WHERE relationship != ''" ) ?;
102-
103- let users: Vec < ( String , String ) > = statement
104- . query_map ( [ ] , |row| {
105- Ok ( ( row. get :: < _ , String > ( 0 ) ?, row. get :: < _ , String > ( 1 ) ?) )
106- } ) ?
107- . filter_map ( Result :: ok)
108- . filter ( |( name, _) | context. contains ( name. as_str ( ) ) ) // Only include users in the conversation
109- . collect ( ) ;
110-
111- Ok ( users)
112- }
113-
114- /// Fetches examples for users mentioned in the conversation
115- fn get_users_with_examples ( database : & r2d2:: Pool < SqliteConnectionManager > , context : & str ) -> Result < Vec < ( String , String , String ) > > {
116- let db = database. get ( ) ?;
117- let mut statement = db. prepare ( "SELECT name, example_input, example_output FROM user WHERE example_input != '' AND example_output != ''" ) ?;
118-
119- let users: Vec < ( String , String , String ) > = statement
120- . query_map ( [ ] , |row| {
121- Ok ( ( row. get :: < _ , String > ( 0 ) ?, row. get :: < _ , String > ( 1 ) ?, row. get :: < _ , String > ( 2 ) ?) )
122- } ) ?
123- . filter_map ( Result :: ok)
124- . filter ( |( name, _, _) | context. contains ( name. as_str ( ) ) ) // Only include users in the conversation
125- . collect ( ) ;
126-
127- Ok ( users)
128- }
129-
13082/// Formats memories into natural language for prompt injection with usage guidelines
13183fn format_memories ( memories : & [ Memory ] ) -> String {
13284 if memories. is_empty ( ) {
@@ -218,52 +170,6 @@ Quality over quantity - one great response beats three mediocre fragments."#,
218170 )
219171}
220172
221- /// Get user from database or return default
222- fn get_user_or_default ( database : & r2d2:: Pool < SqliteConnectionManager > , user_id : u64 ) -> User {
223- database
224- . get ( )
225- . ok ( )
226- . and_then ( |db| {
227- db. prepare ( "SELECT * FROM user WHERE id = ?" ) . ok ( ) . and_then ( |mut stmt| {
228- stmt. query_one ( [ user_id. to_string ( ) ] , |row| {
229- from_row :: < User > ( row) . map_err ( |_| rusqlite:: Error :: QueryReturnedNoRows )
230- } )
231- . ok ( )
232- } )
233- } )
234- . unwrap_or_else ( || User {
235- id : user_id,
236- level : 0 ,
237- xp : 0 ,
238- social_credit : 0 ,
239- name : "Unknown" . to_owned ( ) ,
240- relationship : String :: new ( ) ,
241- example_input : String :: new ( ) ,
242- example_output : String :: new ( ) ,
243- } )
244- }
245-
246- /// Replace user mentions in context with actual usernames
247- fn replace_mentions (
248- context : String ,
249- user_mentions : & HashMap < String , u64 > ,
250- database : & r2d2:: Pool < SqliteConnectionManager > ,
251- ) -> String {
252- let mut processed = context;
253- for ( mention, user_id) in user_mentions {
254- if let Ok ( db) = database. get ( ) {
255- if let Ok ( mut stmt) = db. prepare ( "SELECT * FROM user WHERE id = ?" ) {
256- if let Ok ( user) = stmt. query_one ( [ user_id. to_string ( ) ] , |row| {
257- from_row :: < User > ( row) . map_err ( |_| rusqlite:: Error :: QueryReturnedNoRows )
258- } ) {
259- processed = processed. replace ( mention, & user. name ) ;
260- }
261- }
262- }
263- }
264- processed
265- }
266-
267173/// Stream AI response chunks through a channel
268174async fn stream_ai_response (
269175 client : OpenRouterClient < openrouter_api:: Ready > ,
@@ -313,7 +219,7 @@ async fn stream_ai_response(
313219}
314220
315221pub async fn main (
316- database : r2d2 :: Pool < SqliteConnectionManager > ,
222+ database : Pool ,
317223 user_id : u64 ,
318224 message : & str ,
319225 context : & str ,
@@ -339,14 +245,32 @@ pub async fn main(
339245 ) ?;
340246
341247 // Process context and get user info
342- let processed_context = replace_mentions ( context. to_string ( ) , & user_mentions, & database) ;
343- let user = get_user_or_default ( & database, user_id) ;
344- let memories = get_user_memories ( & database, user_id) ?;
248+ // Replace user mentions with names
249+ let mut processed_context = context. to_string ( ) ;
250+ for ( mention, & user_id_ref) in & user_mentions {
251+ if let Ok ( Some ( u) ) = db:: get_user ( & database, user_id_ref) . await {
252+ processed_context = processed_context. replace ( mention, & u. name ) ;
253+ }
254+ }
255+
256+ let user = db:: get_user ( & database, user_id) . await ?. unwrap_or_else ( || crate :: database:: User {
257+ id : user_id as i64 ,
258+ level : 0 ,
259+ xp : 0 ,
260+ social_credit : 0 ,
261+ name : "Unknown" . to_owned ( ) ,
262+ relationship : String :: new ( ) ,
263+ example_input : String :: new ( ) ,
264+ example_output : String :: new ( ) ,
265+ } ) ;
266+ let memories = db:: get_memories ( & database, user_id) . await . unwrap_or_default ( ) ;
345267 let formatted_memories = format_memories ( & memories) ;
346268
347269 // Fetch dynamic relationship and example data for users in the conversation
348- let users_with_relationships = get_users_with_relationships ( & database, & processed_context) . unwrap_or_default ( ) ;
349- let users_with_examples = get_users_with_examples ( & database, & processed_context) . unwrap_or_default ( ) ;
270+ let users_with_relationships = db:: get_users_with_relationships ( & database) . await . unwrap_or_default ( )
271+ . into_iter ( ) . filter ( |( name, _) | processed_context. contains ( name. as_str ( ) ) ) . collect :: < Vec < _ > > ( ) ;
272+ let users_with_examples = db:: get_users_with_examples ( & database) . await . unwrap_or_default ( )
273+ . into_iter ( ) . filter ( |( name, _, _) | processed_context. contains ( name. as_str ( ) ) ) . collect :: < Vec < _ > > ( ) ;
350274
351275 // Build prompt
352276 let system_prompt = build_character_prompt (
0 commit comments