(
setAppState: SetAppState,
initialMessages: Message[] | undefined,
{ enabled = true }: { enabled?: boolean } = {},
)
| 28 | * - Fresh spawns where context is read from environment variables |
| 29 | */ |
| 30 | export function useSwarmInitialization( |
| 31 | setAppState: SetAppState, |
| 32 | initialMessages: Message[] | undefined, |
| 33 | { enabled = true }: { enabled?: boolean } = {}, |
| 34 | ): void { |
| 35 | useEffect(() => { |
| 36 | if (!enabled) return |
| 37 | if (isAgentSwarmsEnabled()) { |
| 38 | // Check if this is a resumed agent session (from --resume or /resume) |
| 39 | // Resumed sessions have teamName/agentName stored in transcript messages |
| 40 | const firstMessage = initialMessages?.[0] |
| 41 | const teamName = |
| 42 | firstMessage && 'teamName' in firstMessage |
| 43 | ? (firstMessage.teamName as string | undefined) |
| 44 | : undefined |
| 45 | const agentName = |
| 46 | firstMessage && 'agentName' in firstMessage |
| 47 | ? (firstMessage.agentName as string | undefined) |
| 48 | : undefined |
| 49 | |
| 50 | if (teamName && agentName) { |
| 51 | // Resumed agent session - set up team context from stored info |
| 52 | initializeTeammateContextFromSession(setAppState, teamName, agentName) |
| 53 | |
| 54 | // Get agentId from team file for hook initialization |
| 55 | const teamFile = readTeamFile(teamName) |
| 56 | const member = teamFile?.members.find( |
| 57 | (m: { name: string }) => m.name === agentName, |
| 58 | ) |
| 59 | if (member) { |
| 60 | initializeTeammateHooks(setAppState, getSessionId(), { |
| 61 | teamName, |
| 62 | agentId: member.agentId, |
| 63 | agentName, |
| 64 | }) |
| 65 | } |
| 66 | } else { |
| 67 | // Fresh spawn or standalone session |
| 68 | // teamContext is already computed in main.tsx via computeInitialTeamContext() |
| 69 | // and included in initialState, so we only need to initialize hooks here |
| 70 | const context = getDynamicTeamContext?.() |
| 71 | if (context?.teamName && context?.agentId && context?.agentName) { |
| 72 | initializeTeammateHooks(setAppState, getSessionId(), { |
| 73 | teamName: context.teamName, |
| 74 | agentId: context.agentId, |
| 75 | agentName: context.agentName, |
| 76 | }) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | }, [setAppState, initialMessages, enabled]) |
| 81 | } |
| 82 |
no test coverage detected