import { readLaunchPermissionChoice } from './tui-agent-permission-choice' import type { GlobalSettings } from './global-settings-types' import type { SessionOptionValue } from './native-chat/input/native-chat-session-options' import type { TuiAgent } from './tui-agent' import { agentLaunchPermissionSettings, resolveTuiAgentLaunchArgs, resolveTuiAgentLaunchEnv } from './tui-agent-launch-defaults' import { resolveStartupShell, type AgentStartupShell } from './tui-agent-startup-shell' import { hasExtraAgentArgs, mergeExtraAgentArgs } from './automation-extra-agent-args' import { resolveLocalWindowsAgentStartupShell } from './windows-terminal-shell' /** The settings slice a launch reads; hosts pass their whole `GlobalSettings` row. */ export type AgentStartupSettings = Partial< Pick< GlobalSettings, | 'agentCmdOverrides' | 'agentDefaultArgs' | 'agentDefaultEnv' | 'agentPermissionDefault' | 'agentPermissionOverrides' | 'terminalWindowsShell' > > /** Exactly the `buildAgentStartupPlan` / `buildAgentDraftLaunchPlan` inputs that settings decide. */ export type AgentStartupPlanInputs = { agent: TuiAgent cmdOverrides: Partial> agentArgs: string | null agentEnv: Record platform: NodeJS.Platform shell: AgentStartupShell | undefined isRemote: boolean sessionOptions?: Record sessionOptionsOverrideAgentArgs: boolean suppressStartupUpdatePrompt?: boolean } /** Share settings-to-launch mapping so hosts cannot omit precedence flags and let configured args override explicit model picks. Keep host, actual PTY shell and launch overrides as parameters. */ export function resolveAgentStartupPlanInputs(args: { agent: TuiAgent settings: AgentStartupSettings platform: NodeJS.Platform isRemote: boolean /** Replaces the configured default args for this launch; `null` is "no arguments". */ agentArgs?: string | null /** An automation's saved extras, merged over the resolved args; invalid extras throw. */ extraAgentArgs?: string /** Pass the actual PTY shell; omission is valid only when the Settings shell is what the PTY receives. */ shell: AgentStartupShell | undefined sessionOptions?: Record | undefined /** Orca types into this launch after start (worker, automation, handoff), not a user. */ suppressStartupUpdatePrompt?: boolean }): AgentStartupPlanInputs { const { agent, platform, isRemote, sessionOptions } = args const settings = agentLaunchPermissionSettings(agent, args.settings, sessionOptions) // Test absence: launch overrides win and null explicitly means no arguments. let agentArgs = args.agentArgs !== undefined ? args.agentArgs : resolveTuiAgentLaunchArgs( agent, settings, readLaunchPermissionChoice(sessionOptions) ? 'own' : undefined ) const shell = args.shell ?? resolveLocalWindowsAgentStartupShell({ platform, isRemote, terminalWindowsShell: settings.terminalWindowsShell }) if (hasExtraAgentArgs(args.extraAgentArgs)) { const merged = mergeExtraAgentArgs({ agent, defaultArgs: agentArgs, extraAgentArgs: args.extraAgentArgs, shell: resolveStartupShell(platform, shell) }) if (!merged.ok) { throw new Error(merged.error) } agentArgs = merged.agentArgs } return { agent, cmdOverrides: settings.agentCmdOverrides ?? {}, agentArgs, agentEnv: resolveTuiAgentLaunchEnv(agent, settings, platform), platform, shell, isRemote, ...(sessionOptions ? { sessionOptions } : {}), // Explicit session picks outrank configured args; duplicate flag spellings would otherwise let argv order decide. sessionOptionsOverrideAgentArgs: Boolean(sessionOptions), ...(args.suppressStartupUpdatePrompt ? { suppressStartupUpdatePrompt: true } : {}) } }