refactor(server): implement lazy agent initialization with state machine pattern

This commit is contained in:
Mohamed Boudra
2025-10-24 20:18:25 +02:00
parent 8d93eeb5c5
commit 3115c702bf
5 changed files with 591 additions and 402 deletions

View File

@@ -2,7 +2,7 @@
{
"id": "b203dc68-03e0-4971-a3d7-a4822c940b50",
"title": "Agent b203dc68",
"sessionId": "019a16ed-36da-7407-aa21-b56f860f2db7",
"sessionId": "07f421be-98c1-4f23-9604-8f00274956c7",
"options": {
"type": "claude",
"sessionId": "07f421be-98c1-4f23-9604-8f00274956c7"
@@ -13,7 +13,7 @@
{
"id": "d0633729-2f74-426a-a3a8-05e8c7206703",
"title": "Assist with Voice Development",
"sessionId": "019a16f0-18d3-7656-85a8-dae38035049d",
"sessionId": "79c9a8e0-8a24-4851-baad-62747b815442",
"options": {
"type": "claude",
"sessionId": "79c9a8e0-8a24-4851-baad-62747b815442"

File diff suppressed because it is too large Load Diff

View File

@@ -22,7 +22,7 @@ export type AgentOptions = z.infer<typeof AgentOptionsSchema>;
export const PersistedAgentSchema = z.object({
id: z.string(),
title: z.string(),
sessionId: z.string(), // ACP protocol session ID (for backward compatibility)
sessionId: z.string().nullable(), // ACP protocol session ID (null for uninitialized agents)
options: AgentOptionsSchema, // Required field with discriminated union
createdAt: z.string(),
cwd: z.string(),

View File

@@ -364,7 +364,7 @@ export async function createAgentMcpServer(
},
},
async ({ agentId, format = "curated", limit }) => {
const updates = agentManager.getAgentUpdates(agentId);
const updates = await agentManager.getAgentUpdates(agentId);
const currentModeId = agentManager.getCurrentMode(agentId);
if (format === "curated") {

View File

@@ -1,4 +1,5 @@
import type { SessionNotification, RequestPermissionRequest } from "@agentclientprotocol/sdk";
import type { SessionNotification, RequestPermissionRequest, ClientSideConnection } from "@agentclientprotocol/sdk";
import type { ChildProcess } from "child_process";
/**
* Extended update types with messageId for proper deduplication
@@ -31,6 +32,7 @@ export type AgentNotification =
* Status of an agent
*/
export type AgentStatus =
| "uninitialized"
| "initializing"
| "ready"
| "processing"
@@ -38,6 +40,38 @@ export type AgentStatus =
| "failed"
| "killed";
/**
* Session mode definition from ACP
*/
export interface SessionMode {
id: string;
name: string;
description?: string | null;
}
/**
* Runtime state for an initialized agent
*/
export interface AgentRuntime {
process: ChildProcess;
connection: ClientSideConnection;
sessionId: string;
currentModeId: string | null;
availableModes: SessionMode[] | null;
}
/**
* Discriminated union for agent state
*/
export type ManagedAgentState =
| { type: "uninitialized"; persistedSessionId: string | null; lastError?: string }
| { type: "initializing"; persistedSessionId: string | null; initPromise: Promise<void>; initStartedAt: Date }
| { type: "ready"; runtime: AgentRuntime }
| { type: "processing"; runtime: AgentRuntime }
| { type: "completed"; runtime: AgentRuntime; stopReason?: string }
| { type: "failed"; lastError: string; runtime?: AgentRuntime }
| { type: "killed" };
/**
* Information about an agent
*/
@@ -64,15 +98,6 @@ export interface AgentUpdate {
notification: AgentNotification;
}
/**
* Session mode definition from ACP
*/
export interface SessionMode {
id: string;
name: string;
description?: string | null;
}
/**
* Options for creating an agent
*/