(agentId: string)
| 207 | } |
| 208 | |
| 209 | export async function stopAgentLoop(agentId: string): Promise<void> { |
| 210 | const loop = activeLoops[agentId]; |
| 211 | if (loop?.isRunning) { |
| 212 | if (loop.intervalId !== null) window.clearInterval(loop.intervalId); |
| 213 | |
| 214 | // Clear any active sleep state |
| 215 | wakeAgentLoop(agentId); |
| 216 | |
| 217 | // --- SUBSCRIBER CLEANUP --- |
| 218 | // Destroy all transcription subscribers for this agent |
| 219 | StreamManager.destroySubscribersForAgent(agentId); |
| 220 | |
| 221 | // --- STREAM MANAGEMENT --- |
| 222 | // Tell the StreamManager this agent no longer needs these streams. |
| 223 | // The manager will handle stopping the hardware if it's the last user. |
| 224 | Logger.debug(agentId, "Releasing all potential streams for stopping agent."); |
| 225 | |
| 226 | StreamManager.releaseStreamsForAgent(agentId); |
| 227 | |
| 228 | // Clear change detection data |
| 229 | clearAgentChangeData(agentId); |
| 230 | resetNoulTrend(agentId); |
| 231 | |
| 232 | // Note: Agent crop configurations are preserved across stop/start cycles |
| 233 | |
| 234 | |
| 235 | // End the current session and save to IndexedDB |
| 236 | await IterationStore.endSession(agentId); |
| 237 | |
| 238 | // ------------------------- |
| 239 | |
| 240 | activeLoops[agentId] = { ...loop, isRunning: false, intervalId: null, sleepUntil: null }; |
| 241 | |
| 242 | if (getRunningAgentIds().length === 0) { |
| 243 | // This was the last running agent, so shut down the recorder. |
| 244 | recordingManager.forceStop(); |
| 245 | } |
| 246 | |
| 247 | // Logger dispatches the window event automatically |
| 248 | Logger.info(agentId, 'Agent stopped', { |
| 249 | logType: 'agent-status-changed', |
| 250 | content: { agentId, status: 'stopped' } |
| 251 | }); |
| 252 | } else { |
| 253 | Logger.warn(agentId, `Attempted to stop agent that wasn't running`); |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Execute a single iteration of the agent's loop |
no test coverage detected