@@ -12,11 +12,17 @@ export class GeminiClient {
1212 }
1313
1414 /**
15- * Check if combined documentation has been loaded on the backend
15+ * Check if combined documentation has been loaded on the backend for this session
1616 */
17- async getDocsStatus ( ) : Promise < { loaded : boolean ; uri ?: string } > {
17+ async getDocsStatus ( sessionId : string ) : Promise < { loaded : boolean ; uri ?: string } > {
1818 try {
19- const response = await fetch ( `${ this . backendUrl } /docs/status` ) ;
19+ const response = await fetch ( `${ this . backendUrl } /docs/status` , {
20+ method : 'POST' ,
21+ headers : {
22+ 'Content-Type' : 'application/json' ,
23+ } ,
24+ body : JSON . stringify ( { session_id : sessionId } )
25+ } ) ;
2026 if ( ! response . ok ) {
2127 throw new Error ( `Failed to get docs status: ${ response . statusText } ` ) ;
2228 }
@@ -29,12 +35,16 @@ export class GeminiClient {
2935 }
3036
3137 /**
32- * Request backend to load combined documentation into model context
38+ * Request backend to load combined documentation into model context for this session
3339 */
34- async loadDocs ( ) : Promise < { success : boolean ; status : string ; uri ?: string } > {
40+ async loadDocs ( sessionId : string ) : Promise < { success : boolean ; status : string ; uri ?: string } > {
3541 try {
3642 const response = await fetch ( `${ this . backendUrl } /docs/load` , {
37- method : 'POST'
43+ method : 'POST' ,
44+ headers : {
45+ 'Content-Type' : 'application/json' ,
46+ } ,
47+ body : JSON . stringify ( { session_id : sessionId } )
3848 } ) ;
3949 if ( ! response . ok ) {
4050 const err = await response . json ( ) . catch ( ( ) => ( { } ) ) ;
@@ -65,10 +75,30 @@ export class GeminiClient {
6575 return 'XRPCode Buddy' ; // Fallback
6676 }
6777
78+ /**
79+ * Clean up a session on the backend
80+ */
81+ async cleanupSession ( sessionId : string ) : Promise < void > {
82+ try {
83+ const response = await fetch ( `${ this . backendUrl } /session/${ sessionId } ` , {
84+ method : 'DELETE'
85+ } ) ;
86+ if ( response . ok ) {
87+ const data = await response . json ( ) ;
88+ console . log ( `Session ${ sessionId . substring ( 0 , 8 ) } ... cleaned up:` , data . message ) ;
89+ } else {
90+ console . warn ( `Failed to cleanup session ${ sessionId . substring ( 0 , 8 ) } ...` ) ;
91+ }
92+ } catch ( error ) {
93+ console . warn ( 'Failed to cleanup session on backend:' , error ) ;
94+ }
95+ }
96+
6897 /**
6998 * Send a simplified chat request with user message and context
7099 */
71100 async chatWithContext (
101+ sessionId : string ,
72102 userMessage : string ,
73103 conversationHistory : ChatMessage [ ] = [ ] ,
74104 editorContext : string = '' ,
@@ -84,6 +114,7 @@ export class GeminiClient {
84114
85115 // Prepare simplified request payload
86116 const payload = {
117+ session_id : sessionId ,
87118 user_message : userMessage ,
88119 conversation_history : conversationHistory . map ( msg => ( {
89120 role : msg . role === 'assistant' ? 'model' : msg . role ,
@@ -232,15 +263,27 @@ export class GeminiClient {
232263 signal ?: AbortSignal
233264 ) : Promise < string > {
234265 console . warn ( 'chatCompletion is deprecated, use chatWithContext instead' ) ;
235-
266+
236267 if ( messages . length === 0 ) {
237268 throw new Error ( 'No messages provided' ) ;
238269 }
239270
271+ // Generate a temporary session ID for legacy calls
272+ const randomArray = new Uint32Array ( 2 ) ;
273+ ( typeof window !== 'undefined' && window . crypto
274+ ? window . crypto . getRandomValues ( randomArray )
275+ : ( typeof crypto !== 'undefined' && typeof crypto . getRandomValues === 'function'
276+ ? crypto . getRandomValues ( randomArray )
277+ : ( ( ) => { throw new Error ( 'No secure random generator available' ) ; } ) ( )
278+ )
279+ ) ;
280+ const randomString = Array . from ( randomArray ) . map ( n => n . toString ( 36 ) ) . join ( '' ) . substr ( 0 , 9 ) ;
281+ const tempSessionId = `legacy-${ Date . now ( ) } -${ randomString } ` ;
240282 const userMessage = messages [ messages . length - 1 ] . content ;
241283 const conversationHistory = messages . slice ( 0 , - 1 ) ;
242284
243285 return this . chatWithContext (
286+ tempSessionId ,
244287 userMessage ,
245288 conversationHistory ,
246289 '' , // No editor context in legacy mode
0 commit comments