import { isTuiAgent } from './tui-agent-config' import { YOLO_TUI_AGENT_ARGS, YOLO_TUI_AGENT_ENV } from './tui-agent-permissions' import type { TuiAgent } from './tui-agent' const UNSUPPORTED_TUI_AGENT_ARGS: Partial> = { opencode: ['--dangerously-skip-permissions'], kilo: ['--dangerously-skip-permissions'] } export const DEFAULT_TUI_AGENT_ARGS: Partial> = YOLO_TUI_AGENT_ARGS export const DEFAULT_TUI_AGENT_ENV: Partial>> = YOLO_TUI_AGENT_ENV function argPattern(arg: string): RegExp { return new RegExp(`(^|\\s)${arg.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?=\\s|$)`, 'g') } export function hasUnsupportedTuiAgentArgs(agent: TuiAgent, value: unknown): boolean { if (typeof value !== 'string') { return false } return (UNSUPPORTED_TUI_AGENT_ARGS[agent] ?? []).some((arg) => argPattern(arg).test(value)) } function sanitizeTuiAgentLaunchArgs(agent: TuiAgent, args: string): string { const unsupportedArgs = UNSUPPORTED_TUI_AGENT_ARGS[agent] if (!unsupportedArgs) { return args.trim() } // Why: a few agents have removed, relocated, or never exposed Claude-style // skip-permission flags on the interactive TUI command Orca launches. return unsupportedArgs.reduce((next, arg) => next.replace(argPattern(arg), ' '), args).trim() } export function normalizeTuiAgentArgsRecord(value: unknown): Partial> { const normalized: Partial> = {} if (!value || typeof value !== 'object') { return normalized } for (const [agent, args] of Object.entries(value)) { if (!isTuiAgent(agent) || typeof args !== 'string') { continue } normalized[agent] = sanitizeTuiAgentLaunchArgs(agent, args) } return normalized } export function normalizeTuiAgentEnvRecord( value: unknown ): Partial>> { const normalized: Partial>> = {} if (!value || typeof value !== 'object') { return normalized } for (const [agent, env] of Object.entries(value)) { if (!isTuiAgent(agent) || !env || typeof env !== 'object') { continue } const nextEnv: Record = {} for (const [name, raw] of Object.entries(env)) { const key = name.trim() if (!key || typeof raw !== 'string') { continue } nextEnv[key] = raw } normalized[agent] = nextEnv } return normalized } export function getTuiAgentDefaultArgs(agent: TuiAgent): string { return DEFAULT_TUI_AGENT_ARGS[agent] ?? '' } export function getTuiAgentDefaultEnv(agent: TuiAgent): Record { return { ...DEFAULT_TUI_AGENT_ENV[agent] } } export function resolveTuiAgentLaunchArgs( agent: TuiAgent, configuredArgs: Partial> | null | undefined ): string { if ( configuredArgs && Object.hasOwn(configuredArgs, agent) && typeof configuredArgs[agent] === 'string' ) { return configuredArgs[agent] ?? '' } return getTuiAgentDefaultArgs(agent) } export function resolveTuiAgentLaunchEnv( agent: TuiAgent, configuredEnv: Partial>> | null | undefined ): Record { if (configuredEnv && Object.hasOwn(configuredEnv, agent)) { return { ...configuredEnv[agent] } } return getTuiAgentDefaultEnv(agent) }