export const DEFAULT_AGENT_ID = 'main'; export const DEFAULT_MAIN_SESSION_SUFFIX = 'main'; export interface AgentSummary { id: string; name: string; isDefault: boolean; providerAccountId: string | null; modelRef: string | null; modelDisplay: string; mainSessionKey: string; vendorId?: string | null; source?: 'synthetic-main' | 'provider-account'; } export interface AgentsSnapshot { agents: AgentSummary[]; defaultAgentId: string; defaultProviderAccountId: string | null; defaultModelRef: string | null; mainSessionSuffix: string; configuredChannelTypes: string[]; channelOwners: Record; channelAccountOwners: Record; } export interface ParsedSessionKey { sessionKey: string; agentId: string; sessionId: string; isAgentSession: boolean; } export function normalizeAgentId(value: string | null | undefined): string { const normalized = String(value ?? '').trim().toLowerCase(); return normalized || DEFAULT_AGENT_ID; } export function normalizeSessionSuffix(value: string | null | undefined): string { const normalized = String(value ?? '').trim().toLowerCase(); return normalized || DEFAULT_MAIN_SESSION_SUFFIX; } export function buildAgentSessionKey(agentId: string, sessionId: string): string { return `agent:${normalizeAgentId(agentId)}:${normalizeSessionSuffix(sessionId)}`; } export function buildMainSessionKey( agentId: string, sessionId = DEFAULT_MAIN_SESSION_SUFFIX, ): string { return buildAgentSessionKey(agentId, sessionId); } export function parseSessionKey(sessionKey: string): ParsedSessionKey { const trimmed = String(sessionKey ?? '').trim(); if (trimmed.startsWith('agent:')) { const parts = trimmed.split(':'); const agentId = normalizeAgentId(parts[1]); const sessionId = normalizeSessionSuffix(parts.slice(2).join(':')); return { sessionKey: buildAgentSessionKey(agentId, sessionId), agentId, sessionId, isAgentSession: true, }; } if (trimmed.startsWith('local:')) { const parts = trimmed.split(':'); const agentId = normalizeAgentId(parts[1]); const sessionId = normalizeSessionSuffix(parts.slice(2).join(':')); return { sessionKey: buildAgentSessionKey(agentId, sessionId), agentId, sessionId, isAgentSession: true, }; } return { sessionKey: trimmed, agentId: DEFAULT_AGENT_ID, sessionId: normalizeSessionSuffix(trimmed), isAgentSession: false, }; } export function normalizeAgentSessionKey(sessionKey: string): string { return parseSessionKey(sessionKey).sessionKey; }