diff --git a/src/tools/switch-agent/tools.ts b/src/tools/switch-agent/tools.ts index d143bba15..76dbe2878 100644 --- a/src/tools/switch-agent/tools.ts +++ b/src/tools/switch-agent/tools.ts @@ -1,7 +1,7 @@ import { tool, type ToolDefinition } from "@opencode-ai/plugin" import { normalizeAgentForPrompt } from "../../shared/agent-display-names" import { log } from "../../shared/logger" -import type { SwitchAgentArgs } from "./types" +import { SWITCHABLE_AGENT_NAMES, type SwitchAgentArgs } from "./types" const DESCRIPTION = "Switch the active session agent. After calling this tool, the session will transition to the specified agent " + @@ -10,7 +10,15 @@ const DESCRIPTION = "Permanent one-way handoff. Use ONLY when you're the wrong agent for the overall job, NEVER for subtasks (use task()). " + "Targets: atlas, prometheus, sisyphus, hephaestus." -const ALLOWED_AGENTS = new Set(["atlas", "prometheus", "sisyphus", "hephaestus"]) +const ALLOWED_AGENTS = new Set(SWITCHABLE_AGENT_NAMES) + +type TuiClient = { + post: (input: { + url: string + body: { sessionID: string } + headers?: Record + }) => Promise +} type SessionClient = { session: { @@ -44,9 +52,20 @@ function extractSessionId(response: unknown): string | undefined { return undefined } +function hasTuiClient(client: SessionClient): client is SessionClient & { _client: TuiClient } { + const maybeClient = Reflect.get(client as object, "_client") + if (typeof maybeClient !== "object" || maybeClient === null) { + return false + } + return typeof Reflect.get(maybeClient, "post") === "function" +} + async function navigateTuiToSession(client: SessionClient, sessionID: string): Promise { + if (!hasTuiClient(client)) { + return false + } try { - await (client as any)._client.post({ + await client._client.post({ url: "/tui/select-session", body: { sessionID }, headers: { "Content-Type": "application/json" }, diff --git a/src/tools/switch-agent/types.ts b/src/tools/switch-agent/types.ts index 15ae6d905..e2bb0c763 100644 --- a/src/tools/switch-agent/types.ts +++ b/src/tools/switch-agent/types.ts @@ -1,3 +1,7 @@ +export const SWITCHABLE_AGENT_NAMES = ["atlas", "prometheus", "sisyphus", "hephaestus"] as const + +export type SwitchableAgentName = (typeof SWITCHABLE_AGENT_NAMES)[number] + export interface SwitchAgentArgs { agent: string context: string