Skip to content

Commit 7769bb3

Browse files
committed
Address Composio review feedback
1 parent 0da2d5d commit 7769bb3

5 files changed

Lines changed: 51 additions & 96 deletions

File tree

packages/internal/src/db/migrations/0055_common_avengers.sql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,4 @@ CREATE TABLE "composio_session" (
66
CONSTRAINT "composio_session_session_id_unique" UNIQUE("session_id")
77
);
88
--> statement-breakpoint
9-
ALTER TABLE "composio_session" ADD CONSTRAINT "composio_session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
10-
CREATE INDEX "idx_composio_session_session_id" ON "composio_session" USING btree ("session_id");
9+
ALTER TABLE "composio_session" ADD CONSTRAINT "composio_session_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;

packages/internal/src/db/migrations/meta/0055_snapshot.json

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -991,23 +991,7 @@
991991
"default": "now()"
992992
}
993993
},
994-
"indexes": {
995-
"idx_composio_session_session_id": {
996-
"name": "idx_composio_session_session_id",
997-
"columns": [
998-
{
999-
"expression": "session_id",
1000-
"isExpression": false,
1001-
"asc": true,
1002-
"nulls": "last"
1003-
}
1004-
],
1005-
"isUnique": false,
1006-
"concurrently": false,
1007-
"method": "btree",
1008-
"with": {}
1009-
}
1010-
},
994+
"indexes": {},
1011995
"foreignKeys": {
1012996
"composio_session_user_id_user_id_fk": {
1013997
"name": "composio_session_user_id_user_id_fk",
@@ -3632,4 +3616,4 @@
36323616
"schemas": {},
36333617
"tables": {}
36343618
}
3635-
}
3619+
}

packages/internal/src/db/schema.ts

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -304,22 +304,18 @@ export const encryptedApiKeys = pgTable(
304304
}),
305305
)
306306

307-
export const composioSession = pgTable(
308-
'composio_session',
309-
{
310-
user_id: text('user_id')
311-
.primaryKey()
312-
.references(() => user.id, { onDelete: 'cascade' }),
313-
session_id: text('session_id').notNull().unique(),
314-
created_at: timestamp('created_at', { mode: 'date', withTimezone: true })
315-
.notNull()
316-
.defaultNow(),
317-
updated_at: timestamp('updated_at', { mode: 'date', withTimezone: true })
318-
.notNull()
319-
.defaultNow(),
320-
},
321-
(table) => [index('idx_composio_session_session_id').on(table.session_id)],
322-
)
307+
export const composioSession = pgTable('composio_session', {
308+
user_id: text('user_id')
309+
.primaryKey()
310+
.references(() => user.id, { onDelete: 'cascade' }),
311+
session_id: text('session_id').notNull().unique(),
312+
created_at: timestamp('created_at', { mode: 'date', withTimezone: true })
313+
.notNull()
314+
.defaultNow(),
315+
updated_at: timestamp('updated_at', { mode: 'date', withTimezone: true })
316+
.notNull()
317+
.defaultNow(),
318+
})
323319

324320
// Organization tables
325321
export const orgRoleEnum = pgEnum('org_role', ['owner', 'admin', 'member'])

sdk/src/run.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ async function runOnce({
295295
// Comparing array identity detects progress more robustly than length:
296296
// context pruning could shrink history below its starting length without
297297
// meaning the runtime never ran.
298-
const initialMessageHistory = sessionState.mainAgentState.messageHistory
298+
let initialMessageHistory = sessionState.mainAgentState.messageHistory
299299

300300
/** Calculates the current session state if cancelled.
301301
*
@@ -533,6 +533,7 @@ async function runOnce({
533533
sessionState = await applyOverridesToSessionState(cwd, sessionState, {
534534
customToolDefinitions: activeCustomToolDefinitions,
535535
})
536+
initialMessageHistory = sessionState.mainAgentState.messageHistory
536537
}
537538

538539
if (signal?.aborted) {

web/src/server/composio.ts

Lines changed: 34 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -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-
4441
function 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

Comments
 (0)