@@ -38,9 +38,6 @@ type CachedComposioSession = {
3838 tools : ComposioToolDefinition [ ]
3939}
4040
41- const sessionsByUserId = new Map < string , Promise < CachedComposioSession > > ( )
42- const sessionsBySessionId = new Map < string , CachedComposioSession > ( )
43-
4441function parseEnvFileValue ( contents : string , key : string ) : string | undefined {
4542 for ( const rawLine of contents . split ( / \r ? \n / ) ) {
4643 const line = rawLine . trim ( )
@@ -188,8 +185,6 @@ async function createSessionForUser(params: {
188185 sessionId : session . sessionId ,
189186 } ) ,
190187 }
191-
192- sessionsBySessionId . set ( cachedSession . sessionId , cachedSession )
193188 return cachedSession
194189}
195190
@@ -212,59 +207,45 @@ async function rehydrateSession(params: {
212207 } )
213208 : [ ] ,
214209 }
215-
216- if ( params . includeTools ) {
217- sessionsByUserId . set ( params . userId , Promise . resolve ( cachedSession ) )
218- }
219- sessionsBySessionId . set ( params . sessionId , cachedSession )
220210 return cachedSession
221211}
222212
223- async function getCachedSession ( params : {
213+ async function getSessionForUser ( params : {
224214 db : CodebuffPgDatabase
225215 userId : string
226216 logger : Logger
227217} ) : Promise < CachedComposioSession | null > {
228218 const apiKey = getComposioApiKey ( )
229219 if ( ! apiKey ) return null
230220
231- let cached = sessionsByUserId . get ( params . userId )
232- if ( ! cached ) {
233- cached = ( async ( ) => {
234- const storedSession = await getStoredSessionByUser ( {
235- db : params . db ,
236- userId : params . userId ,
237- } )
238- if ( storedSession ) {
239- params . logger . info (
240- { userId : params . userId } ,
241- 'Rehydrating Composio session from database' ,
242- )
243- return rehydrateSession ( {
244- userId : params . userId ,
245- sessionId : storedSession . session_id ,
246- apiKey,
247- includeTools : true ,
248- } )
249- }
250-
221+ try {
222+ const storedSession = await getStoredSessionByUser ( {
223+ db : params . db ,
224+ userId : params . userId ,
225+ } )
226+ if ( storedSession ) {
251227 params . logger . info (
252228 { userId : params . userId } ,
253- 'Creating new Composio session' ,
229+ 'Rehydrating Composio session from database ' ,
254230 )
255- return createSessionForUser ( {
256- db : params . db ,
231+ return rehydrateSession ( {
257232 userId : params . userId ,
233+ sessionId : storedSession . session_id ,
258234 apiKey,
235+ includeTools : true ,
259236 } )
260- } ) ( )
261- sessionsByUserId . set ( params . userId , cached )
262- }
237+ }
263238
264- try {
265- return await cached
239+ params . logger . info (
240+ { userId : params . userId } ,
241+ 'Creating new Composio session' ,
242+ )
243+ return createSessionForUser ( {
244+ db : params . db ,
245+ userId : params . userId ,
246+ apiKey,
247+ } )
266248 } catch ( error ) {
267- sessionsByUserId . delete ( params . userId )
268249 params . logger . error (
269250 { error : getErrorObject ( error ) , userId : params . userId } ,
270251 'Failed to initialize Composio session' ,
@@ -278,7 +259,7 @@ export async function getComposioToolsForUser(params: {
278259 userId : string
279260 logger : Logger
280261} ) : Promise < { sessionId : string ; tools : ComposioToolDefinition [ ] } | null > {
281- const cached = await getCachedSession ( params )
262+ const cached = await getSessionForUser ( params )
282263 if ( ! cached ) return null
283264
284265 return {
@@ -308,28 +289,22 @@ export async function executeComposioTool(params: {
308289 const apiKey = getComposioApiKey ( )
309290 if ( ! apiKey ) return null
310291
311- let cached = sessionsBySessionId . get ( params . sessionId )
312- if ( ! cached ) {
313- const storedSession = await getStoredSessionById ( {
314- db : params . db ,
315- userId : params . userId ,
316- sessionId : params . sessionId ,
317- } )
318- if ( storedSession ) {
319- cached = await rehydrateSession ( {
320- userId : params . userId ,
321- sessionId : params . sessionId ,
322- apiKey,
323- includeTools : false ,
324- } )
325- }
326- }
327-
328- if ( ! cached || cached . userId !== params . userId ) {
292+ const storedSession = await getStoredSessionById ( {
293+ db : params . db ,
294+ userId : params . userId ,
295+ sessionId : params . sessionId ,
296+ } )
297+ if ( ! storedSession ) {
329298 return null
330299 }
331300
332301 try {
302+ const cached = await rehydrateSession ( {
303+ userId : params . userId ,
304+ sessionId : params . sessionId ,
305+ apiKey,
306+ includeTools : false ,
307+ } )
333308 const result = await cached . session . execute ( params . toolName , params . input )
334309 return [ { type : 'json' , value : toJsonValue ( result ) } ]
335310 } catch ( error ) {
0 commit comments