@@ -16,75 +16,45 @@ function messageId(idSeed, role, content) {
1616export async function searchMemories ( cfg , params , log = noop ) {
1717 const { memory_types, ...baseParams } = params ;
1818
19- // Backend only supports these types in hybrid/vector search.
20- // "profile" is handled separately by the backend (fetched from MongoDB, not ES/Milvus).
21- // "agent_case" / "agent_skill" do not exist in the backend MemoryType enum.
22- const SEARCHABLE_TYPES = new Set ( [ "episodic_memory" , "foresight" , "event_log" ] ) ;
19+ const SEARCHABLE = new Set ( [ "episodic_memory" ] ) ;
20+ const searchTypes = ( memory_types ?? [ ] ) . filter ( ( t ) => SEARCHABLE . has ( t ) ) ;
2321
24- const searchTypes = ( memory_types ?? [ ] ) . filter ( ( t ) => SEARCHABLE_TYPES . has ( t ) ) ;
25- // profile is passed alongside a searchable type so the backend can attach it via its own path
26- const wantProfile = ( memory_types ?? [ ] ) . includes ( "profile" ) ;
27-
28- if ( ! searchTypes . length && ! wantProfile ) {
29- return { status : "ok" , result : { profiles : [ ] , memories : [ ] , pending_messages : [ ] } } ;
22+ if ( ! searchTypes . length ) {
23+ return { status : "ok" , result : { memories : [ ] , pending_messages : [ ] } } ;
3024 }
3125
32- // Single search request with only valid searchable types.
33- // The backend's retrieve_mem always fetches pending_messages and (when profile is requested)
34- // attaches profile data automatically based on user_id/group_id.
35- const types = searchTypes . length ? searchTypes : [ "episodic_memory" ] ;
36- const p = { ...baseParams , memory_types : types } ;
37- log . info ( `${ TAG } GET /api/v1/memories/search` , JSON . stringify ( p ) ) ;
38- const searchResult = await request ( cfg , "GET" , "/api/v1/memories/search" , p ) ;
39- log . info ( `${ TAG } GET response` , JSON . stringify ( searchResult ) ) ;
40-
41- // If profile was requested, fetch it separately via the fetch endpoint
42- let profiles = [ ] ;
43- if ( wantProfile ) {
44- try {
45- const profileParams = {
46- user_id : baseParams . user_id ,
47- group_id : baseParams . group_id ,
48- memory_type : "profile" ,
49- limit : 1 ,
50- } ;
51- log . info ( `${ TAG } GET /api/v1/memories (profile)` , JSON . stringify ( profileParams ) ) ;
52- const profileResult = await request ( cfg , "GET" , "/api/v1/memories" , profileParams ) ;
53- log . info ( `${ TAG } GET response (profile)` , JSON . stringify ( profileResult ) ) ;
54- if ( profileResult ?. result ?. memories ?. length ) {
55- profiles = profileResult . result . memories ;
56- }
57- } catch ( err ) {
58- log . warn ( `${ TAG } profile fetch failed: ${ err . message } ` ) ;
59- }
60- }
26+ const p = { ...baseParams , memory_types : searchTypes } ;
27+ log . info ( `${ TAG } GET /api/v1/memories/search` ) ;
28+ const r = await request ( cfg , "GET" , "/api/v1/memories/search" , p ) ;
29+ log . info ( `${ TAG } GET response` ) ;
6130
62- const merged = {
31+ return {
6332 status : "ok" ,
6433 result : {
65- profiles,
66- memories : searchResult ?. result ?. memories ?? [ ] ,
67- pending_messages : searchResult ?. result ?. pending_messages ?? [ ] ,
34+ memories : r ?. result ?. memories ?? [ ] ,
35+ pending_messages : r ?. result ?. pending_messages ?? [ ] ,
6836 } ,
6937 } ;
70- return merged ;
7138}
7239
73- export async function saveMemories ( cfg , { userId, groupId, messages = [ ] , flush = false , idSeed = "" } , log = noop ) {
40+ export async function saveMemories ( cfg , { userId, groupId, messages = [ ] , flush = false , idSeed = "" } ) {
7441 if ( ! messages . length ) return ;
7542 const stamp = Date . now ( ) ;
7643
7744 const payloads = messages . map ( ( msg , i ) => {
7845 const { role = "user" , content = "" } = msg ;
79- const sender = role === "assistant" ? role : userId ;
46+ // Always use userId as sender so the backend stores a consistent user_id
47+ // for both user and assistant messages. The `role` field distinguishes who spoke.
48+ const sender = userId ;
49+ const senderName = role === "assistant" ? "assistant" : userId ;
8050 const isLast = i === messages . length - 1 ;
8151
8252 return {
8353 message_id : messageId ( idSeed , role , content ) ,
8454 create_time : new Date ( stamp + i ) . toISOString ( ) ,
8555 role,
8656 sender,
87- sender_name : sender ,
57+ sender_name : senderName ,
8858 content,
8959 group_id : groupId ,
9060 group_name : groupId ,
@@ -94,10 +64,7 @@ export async function saveMemories(cfg, { userId, groupId, messages = [], flush
9464 } ;
9565 } ) ;
9666
97- // Send sequentially to preserve message order on the backend
9867 for ( const payload of payloads ) {
99- log . info ( `${ TAG } POST /api/v1/memories` , JSON . stringify ( payload ) ) ;
100- const result = await request ( cfg , "POST" , "/api/v1/memories" , payload ) ;
101- log . info ( `${ TAG } POST response` , JSON . stringify ( result ) ) ;
68+ await request ( cfg , "POST" , "/api/v1/memories" , payload ) ;
10269 }
10370}
0 commit comments