UNPKG

@iqai/adk-cli

Version:

CLI tool for creating, running, and testing ADK-TS agents

114 lines 5.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.hashState = hashState; exports.getExistingSession = getExistingSession; exports.getOrCreateSession = getOrCreateSession; exports.createRunnerWithSession = createRunnerWithSession; exports.storeLoadedAgent = storeLoadedAgent; exports.clearAgentSessions = clearAgentSessions; const node_crypto_1 = require("node:crypto"); const adk_1 = require("@iqai/adk"); const constants_1 = require("../../../common/constants"); function hashState(state) { if (!state || Object.keys(state).length === 0) return "empty"; return (0, node_crypto_1.createHash)("sha256").update(JSON.stringify(state)).digest("hex"); } async function getExistingSession(sessionService, agentPath, sessionId, logger) { const userId = `${constants_1.USER_ID_PREFIX}${agentPath}`; const appName = constants_1.DEFAULT_APP_NAME; try { const session = await sessionService.getSession(appName, userId, sessionId); if (session) { logger.log(`Restored existing session: ${sessionId}`); return session; } } catch (error) { logger.warn(`Failed to restore session ${sessionId}, creating new one: ${error instanceof Error ? error.message : String(error)}`); } return sessionService.createSession(appName, userId); } async function getOrCreateSession(sessionService, agentPath, agentResult, extractInitialStateFn, logger) { const userId = `${constants_1.USER_ID_PREFIX}${agentPath}`; const appName = constants_1.DEFAULT_APP_NAME; const existingSessions = await sessionService.listSessions(appName, userId); if (existingSessions.sessions.length > 0) { const mostRecentSession = existingSessions.sessions.reduce((latest, current) => current.lastUpdateTime > latest.lastUpdateTime ? current : latest); const hasState = mostRecentSession.state && Object.keys(mostRecentSession.state).length > 0; if (!hasState) { const initialState = extractInitialStateFn(agentResult); if (initialState) { logger.log(`Existing session has no state, initializing with agent's initial state: ${Object.keys(initialState)}`); await sessionService.createSession(appName, userId, initialState, mostRecentSession.id); mostRecentSession.state = initialState; } } logger.log(`Reusing existing session: ${JSON.stringify({ sessionId: mostRecentSession.id })}`); return mostRecentSession; } const initialState = extractInitialStateFn(agentResult); return sessionService.createSession(appName, userId, initialState); } async function createRunnerWithSession(sessionService, baseAgent, sessionToUse, agentPath) { const userId = `${constants_1.USER_ID_PREFIX}${agentPath}`; const appName = constants_1.DEFAULT_APP_NAME; const agentBuilder = adk_1.AgentBuilder.create(baseAgent.name).withAgent(baseAgent); agentBuilder.withSessionService(sessionService, { userId, appName, sessionId: sessionToUse.id, state: sessionToUse.state, }); const { runner } = await agentBuilder.build(); return runner; } async function storeLoadedAgent(sessionService, agentPath, agentResult, runner, sessionToUse, agent, setLoaded, logger, setBuilt) { const userId = `${constants_1.USER_ID_PREFIX}${agentPath}`; const appName = constants_1.DEFAULT_APP_NAME; setLoaded(agentPath, { agent: agentResult.agent, runner, sessionId: sessionToUse.id, userId, appName, }); agent.instance = agentResult.agent; agent.name = agentResult.agent.name; if (agentResult.builtAgent && setBuilt) setBuilt(agentPath, agentResult.builtAgent); try { const existingSession = await sessionService.getSession(appName, userId, sessionToUse.id); if (!existingSession) { logger.log(`Creating session in sessionService: ${sessionToUse.id}`); await sessionService.createSession(appName, userId, sessionToUse.state, sessionToUse.id); } else { logger.log(`Session already exists in sessionService: ${sessionToUse.id}`); } } catch (error) { logger.error("Error ensuring session exists:", error); } } async function clearAgentSessions(sessionService, agentPath, logger) { try { const userId = `${constants_1.USER_ID_PREFIX}${agentPath}`; const appName = constants_1.DEFAULT_APP_NAME; const sessions = await sessionService.listSessions(appName, userId); for (const session of sessions.sessions) { try { await sessionService.deleteSession(appName, userId, session.id); logger.log(`Cleared session ${session.id} for agent ${agentPath}`); } catch (error) { logger.warn(`Failed to clear session ${session.id}: ${error instanceof Error ? error.message : String(error)}`); } } } catch (error) { logger.warn(`Failed to list sessions for clearing: ${error instanceof Error ? error.message : String(error)}`); } } //# sourceMappingURL=sessions.js.map