refactor(server): route draft command listing through agent manager
This commit is contained in:
@@ -11,6 +11,7 @@ import { z } from "zod";
|
||||
import type {
|
||||
AgentCapabilityFlags,
|
||||
AgentClient,
|
||||
AgentSlashCommand,
|
||||
AgentMode,
|
||||
AgentPermissionRequest,
|
||||
AgentPermissionResponse,
|
||||
@@ -437,6 +438,36 @@ export class AgentManager {
|
||||
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 {
|
||||
const agent = this.agents.get(id);
|
||||
return agent ? { ...agent } : null;
|
||||
|
||||
@@ -3501,24 +3501,8 @@ export class Session {
|
||||
}
|
||||
|
||||
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 = {
|
||||
provider,
|
||||
provider: draftConfig.provider,
|
||||
cwd: expandTilde(draftConfig.cwd),
|
||||
...(draftConfig.modeId ? { modeId: draftConfig.modeId } : {}),
|
||||
...(draftConfig.model ? { model: draftConfig.model } : {}),
|
||||
@@ -3527,41 +3511,16 @@ export class Session {
|
||||
: {}),
|
||||
};
|
||||
|
||||
const ephemeralSession = await client.createSession(sessionConfig);
|
||||
try {
|
||||
if (!ephemeralSession.listCommands) {
|
||||
this.emit({
|
||||
type: "list_commands_response",
|
||||
payload: {
|
||||
agentId,
|
||||
commands: [],
|
||||
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"
|
||||
);
|
||||
}
|
||||
}
|
||||
const commands = await this.agentManager.listDraftCommands(sessionConfig);
|
||||
this.emit({
|
||||
type: "list_commands_response",
|
||||
payload: {
|
||||
agentId,
|
||||
commands,
|
||||
error: null,
|
||||
requestId,
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user