import Foundation public enum TerminalKind: String, Sendable, Equatable, CaseIterable { /// The top pane: a shell that runs the `claude` CLI. case agent /// The bottom pane: a plain login shell. case shell /// The Explorer's log block: `tail -f` on one file. case tail } /// Executable, arguments and environment for one terminal pane. public struct TerminalLaunch: Sendable, Equatable { public var executable: String public var arguments: [String] public var environment: [String: String] public var workingDirectory: String public init(executable: String, arguments: [String], environment: [String: String], workingDirectory: String) { self.executable = executable self.arguments = arguments self.environment = environment self.workingDirectory = workingDirectory } /// The launch for the agent or shell pane. The agent runs as `$SHELL -i -c `: `-i` still sources the rc /// files (nvm etc.) but leaves no prompt line or echoed command behind. public static func make(kind: TerminalKind, config: HelderConfig, projectRoot: String?, environment base: [String: String]) -> TerminalLaunch { let shell = config.terminal.shell ?? base["SHELL"] ?? "/bin/zsh" let args = kind == .agent && config.ai.autoLaunch ? ["-i", "-c", config.ai.command] : [] let cwd = projectRoot ?? base["HOME"] ?? "/" return TerminalLaunch(executable: shell, arguments: args, environment: environment(from: base), workingDirectory: cwd) } /// `tail -f` on a project-relative path, run directly without a shell. public static func tail(path: String, projectRoot: String?, environment base: [String: String]) -> TerminalLaunch { TerminalLaunch(executable: "/usr/bin/tail", arguments: ["-f", path], environment: environment(from: base), workingDirectory: projectRoot ?? base["HOME"] ?? "/") } /// The child environment. A GUI launch inherits no locale and no COLORTERM, which turns /// multibyte output into mojibake and shifts the CLI's 24-bit colours; npm's prefix /// variables make nvm refuse to load. `TERM_PROGRAM` tells the agent it runs in Helder. public static func environment(from base: [String: String]) -> [String: String] { var env = base env["npm_config_prefix"] = nil env["npm_config_globalconfig"] = nil if env["LC_ALL"] == nil && env["LC_CTYPE"] == nil && env["LANG"] == nil { env["LANG"] = "en_US.UTF-8" } if env["COLORTERM"] == nil { env["COLORTERM"] = "truecolor" } env["TERM"] = "xterm-256color" env["TERM_PROGRAM"] = "Helder" env["TERM_PROGRAM_VERSION"] = nil return env } }