feat: add centralized mode validation for agent providers
Prevents server crashes when invalid modes are passed to agents by validating mode IDs before attempting to set them on the SDK. - Export VALID_CLAUDE_MODES and VALID_CODEX_MODES from provider files - Add validateMode() function in agent-manager.ts - Apply validation in createAgent(), resumeAgent(), and setAgentMode() - Throws clear error listing valid modes when validation fails This fixes the crash when 'read-only' (Codex mode) was passed to a Claude agent. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -22,6 +22,8 @@ import type {
|
|||||||
} from "./agent-sdk-types.js";
|
} from "./agent-sdk-types.js";
|
||||||
import { resolveAgentModel } from "./model-resolver.js";
|
import { resolveAgentModel } from "./model-resolver.js";
|
||||||
import type { AgentRegistry } from "./agent-registry.js";
|
import type { AgentRegistry } from "./agent-registry.js";
|
||||||
|
import { VALID_CLAUDE_MODES } from "./providers/claude-agent.js";
|
||||||
|
import { VALID_CODEX_MODES } from "./providers/codex-agent.js";
|
||||||
|
|
||||||
export const AGENT_LIFECYCLE_STATUSES = [
|
export const AGENT_LIFECYCLE_STATUSES = [
|
||||||
"initializing",
|
"initializing",
|
||||||
@@ -162,6 +164,27 @@ function isAgentBusy(status: AgentLifecycleStatus): boolean {
|
|||||||
return BUSY_STATUSES.includes(status);
|
return BUSY_STATUSES.includes(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getValidModesForProvider(provider: AgentProvider): Set<string> {
|
||||||
|
switch (provider) {
|
||||||
|
case "claude":
|
||||||
|
return VALID_CLAUDE_MODES;
|
||||||
|
case "codex":
|
||||||
|
return VALID_CODEX_MODES;
|
||||||
|
default:
|
||||||
|
return new Set();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateMode(provider: AgentProvider, modeId: string): void {
|
||||||
|
const validModes = getValidModesForProvider(provider);
|
||||||
|
if (!validModes.has(modeId)) {
|
||||||
|
const validModesList = Array.from(validModes).join(", ");
|
||||||
|
throw new Error(
|
||||||
|
`Invalid mode '${modeId}' for provider '${provider}'. Valid modes: ${validModesList}`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function createAbortError(
|
function createAbortError(
|
||||||
signal: AbortSignal | undefined,
|
signal: AbortSignal | undefined,
|
||||||
fallbackMessage: string
|
fallbackMessage: string
|
||||||
@@ -292,6 +315,12 @@ export class AgentManager {
|
|||||||
agentId?: string
|
agentId?: string
|
||||||
): Promise<ManagedAgent> {
|
): Promise<ManagedAgent> {
|
||||||
const normalizedConfig = await this.normalizeConfig(config);
|
const normalizedConfig = await this.normalizeConfig(config);
|
||||||
|
|
||||||
|
// Validate mode if provided
|
||||||
|
if (normalizedConfig.modeId) {
|
||||||
|
validateMode(normalizedConfig.provider, normalizedConfig.modeId);
|
||||||
|
}
|
||||||
|
|
||||||
const client = this.requireClient(normalizedConfig.provider);
|
const client = this.requireClient(normalizedConfig.provider);
|
||||||
const session = await client.createSession(normalizedConfig);
|
const session = await client.createSession(normalizedConfig);
|
||||||
return this.registerSession(
|
return this.registerSession(
|
||||||
@@ -313,6 +342,12 @@ export class AgentManager {
|
|||||||
provider: handle.provider,
|
provider: handle.provider,
|
||||||
} as AgentSessionConfig;
|
} as AgentSessionConfig;
|
||||||
const normalizedConfig = await this.normalizeConfig(mergedConfig);
|
const normalizedConfig = await this.normalizeConfig(mergedConfig);
|
||||||
|
|
||||||
|
// Validate mode if provided
|
||||||
|
if (normalizedConfig.modeId) {
|
||||||
|
validateMode(handle.provider, normalizedConfig.modeId);
|
||||||
|
}
|
||||||
|
|
||||||
const resumeOverrides =
|
const resumeOverrides =
|
||||||
normalizedConfig.model !== mergedConfig.model
|
normalizedConfig.model !== mergedConfig.model
|
||||||
? { ...overrides, model: normalizedConfig.model }
|
? { ...overrides, model: normalizedConfig.model }
|
||||||
@@ -374,6 +409,10 @@ export class AgentManager {
|
|||||||
|
|
||||||
async setAgentMode(agentId: string, modeId: string): Promise<void> {
|
async setAgentMode(agentId: string, modeId: string): Promise<void> {
|
||||||
const agent = this.requireAgent(agentId);
|
const agent = this.requireAgent(agentId);
|
||||||
|
|
||||||
|
// Validate mode before attempting to set it
|
||||||
|
validateMode(agent.provider, modeId);
|
||||||
|
|
||||||
await agent.session.setMode(modeId);
|
await agent.session.setMode(modeId);
|
||||||
agent.currentModeId = modeId;
|
agent.currentModeId = modeId;
|
||||||
this.emitState(agent);
|
this.emitState(agent);
|
||||||
|
|||||||
@@ -72,6 +72,10 @@ const DEFAULT_MODES: AgentMode[] = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export const VALID_CLAUDE_MODES = new Set(
|
||||||
|
DEFAULT_MODES.map((mode) => mode.id)
|
||||||
|
);
|
||||||
|
|
||||||
type ClaudeAgentConfig = AgentSessionConfig & { provider: "claude" };
|
type ClaudeAgentConfig = AgentSessionConfig & { provider: "claude" };
|
||||||
|
|
||||||
export type ClaudeContentChunk = { type: string; [key: string]: any };
|
export type ClaudeContentChunk = { type: string; [key: string]: any };
|
||||||
|
|||||||
@@ -69,6 +69,10 @@ const CODEX_MODES: AgentMode[] = [
|
|||||||
},
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
|
export const VALID_CODEX_MODES = new Set(
|
||||||
|
CODEX_MODES.map((mode) => mode.id)
|
||||||
|
);
|
||||||
|
|
||||||
const DEFAULT_CODEX_MODE_ID = "auto";
|
const DEFAULT_CODEX_MODE_ID = "auto";
|
||||||
|
|
||||||
const MODE_PRESETS: Record<
|
const MODE_PRESETS: Record<
|
||||||
|
|||||||
Reference in New Issue
Block a user