fix: lazy agent loading and type safety improvements

1. Lazy agent loading - fix race condition where prompts sent before initialization completes:
   - Add ensureAgentLoaded() helper that deduplicates initialization requests
   - Update handleSendAgentMessage/Audio to await agent initialization before streaming
   - Fix status updates being sent to client after initialization

2. Type safety - remove unsafe 'as any' casts for agent status:
   - Create AGENT_LIFECYCLE_STATUSES constant as single source of truth
   - Export AgentStatusSchema from messages.ts for reuse
   - Update registry schema to validate lastStatus against AgentStatusSchema
   - Add .default("closed") to handle missing status values from legacy files
   - Remove (record.lastStatus as any) cast in buildStoredAgentPayload

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Mohamed Boudra
2025-11-29 22:44:03 +00:00
co-authored by Claude
parent 5660935a4b
commit 898b8ce5a1
87 changed files with 157670 additions and 39 deletions
@@ -22,12 +22,16 @@ import type {
} from "./agent-sdk-types.js";
import { resolveAgentModel } from "./model-resolver.js";
export const AGENT_LIFECYCLE_STATUSES = [
"initializing",
"idle",
"running",
"error",
"closed",
] as const;
export type AgentLifecycleStatus =
| "initializing"
| "idle"
| "running"
| "error"
| "closed";
(typeof AGENT_LIFECYCLE_STATUSES)[number];
export type AgentSnapshot = {
id: string;