refactor(server): route draft command listing through agent manager

This commit is contained in:
Mohamed Boudra
2026-02-17 23:08:33 +07:00
parent 6aa5e97d75
commit ecd276e3d2
2 changed files with 42 additions and 52 deletions
@@ -11,6 +11,7 @@ import { z } from "zod";
import type { import type {
AgentCapabilityFlags, AgentCapabilityFlags,
AgentClient, AgentClient,
AgentSlashCommand,
AgentMode, AgentMode,
AgentPermissionRequest, AgentPermissionRequest,
AgentPermissionResponse, AgentPermissionResponse,
@@ -437,6 +438,36 @@ export class AgentManager {
return Promise.all(checks); return Promise.all(checks);
} }
async listDraftCommands(config: AgentSessionConfig): Promise<AgentSlashCommand[]> {
const normalizedConfig = await this.normalizeConfig(config);
const client = this.requireClient(normalizedConfig.provider);
const available = await client.isAvailable();
if (!available) {
throw new Error(
`Provider '${normalizedConfig.provider}' is not available. Please ensure the CLI is installed.`
);
}
const session = await client.createSession(normalizedConfig);
try {
if (!session.listCommands) {
throw new Error(
`Provider '${normalizedConfig.provider}' does not support listing commands`
);
}
return await session.listCommands();
} finally {
try {
await session.close();
} catch (error) {
this.logger.warn(
{ err: error, provider: normalizedConfig.provider },
"Failed to close draft command listing session"
);
}
}
}
getAgent(id: string): ManagedAgent | null { getAgent(id: string): ManagedAgent | null {
const agent = this.agents.get(id); const agent = this.agents.get(id);
return agent ? { ...agent } : null; return agent ? { ...agent } : null;
+11 -52
View File
@@ -3501,24 +3501,8 @@ export class Session {
} }
if (!agent && draftConfig) { if (!agent && draftConfig) {
const provider = draftConfig.provider;
const client = this.providerRegistry[provider].createClient(this.sessionLogger);
const available = await client.isAvailable();
if (!available) {
this.emit({
type: "list_commands_response",
payload: {
agentId,
commands: [],
error: `Provider '${provider}' is not available`,
requestId,
},
});
return;
}
const sessionConfig: AgentSessionConfig = { const sessionConfig: AgentSessionConfig = {
provider, provider: draftConfig.provider,
cwd: expandTilde(draftConfig.cwd), cwd: expandTilde(draftConfig.cwd),
...(draftConfig.modeId ? { modeId: draftConfig.modeId } : {}), ...(draftConfig.modeId ? { modeId: draftConfig.modeId } : {}),
...(draftConfig.model ? { model: draftConfig.model } : {}), ...(draftConfig.model ? { model: draftConfig.model } : {}),
@@ -3527,41 +3511,16 @@ export class Session {
: {}), : {}),
}; };
const ephemeralSession = await client.createSession(sessionConfig); const commands = await this.agentManager.listDraftCommands(sessionConfig);
try { this.emit({
if (!ephemeralSession.listCommands) { type: "list_commands_response",
this.emit({ payload: {
type: "list_commands_response", agentId,
payload: { commands,
agentId, error: null,
commands: [], requestId,
error: `Provider '${provider}' does not support listing commands`, },
requestId, });
},
});
return;
}
const commands = await ephemeralSession.listCommands();
this.emit({
type: "list_commands_response",
payload: {
agentId,
commands,
error: null,
requestId,
},
});
} finally {
try {
await ephemeralSession.close();
} catch (closeError) {
this.sessionLogger.warn(
{ err: closeError, provider, agentId },
"Failed to close ephemeral command listing session"
);
}
}
return; return;
} }