diff --git a/src/agent/index.ts b/src/agent/index.ts index d24210b2..dc829d65 100644 --- a/src/agent/index.ts +++ b/src/agent/index.ts @@ -1,6 +1,27 @@ import { getSessionManager } from './session-manager'; +import type { AgentSessionStatus } from './session-manager'; import type { AgentStatus, AgentStatusInfo } from './types'; +function mapSessionStatusToAgentStatus(status: AgentSessionStatus): AgentStatus { + switch (status) { + case 'reading': + case 'thinking': + case 'acting': + case 'waiting': + return 'running'; + case 'completed': + return 'completed'; + case 'cancelled': + return 'cancelled'; + case 'error': + case 'interrupted': + return 'error'; + case 'idle': + default: + return 'idle'; + } +} + export type AgentSessionSummary = { id: string; status: string; @@ -10,9 +31,21 @@ export type AgentSessionSummary = { }; export function getAgentStatus(): AgentStatusInfo { - const hasActive = getSessionManager().getActiveSessions().length > 0; - const status: AgentStatus = hasActive ? 'running' : 'offline'; - return { status }; + const sessions = getSessionManager().getAllSessions(); + if (sessions.length === 0) { + return { status: 'offline' }; + } + // Report the most recently active session so callers can identify which + // session the status refers to (the AgentStatusInfo.sessionId contract), + // rather than collapsing every session into a single running/offline flag. + const mostRecent = sessions.reduce((latest, session) => ( + session.lastActivity.getTime() > latest.lastActivity.getTime() ? session : latest + )); + return { + status: mapSessionStatusToAgentStatus(mostRecent.status), + sessionId: mostRecent.id, + startedAt: mostRecent.startTime.getTime(), + }; } export function getAgentSessionsSummary(): AgentSessionSummary[] {