mirror of
https://github.com/stablyai/orca.git
synced 2026-10-19 16:03:26 +00:00
66 lines
1.8 KiB
TypeScript
66 lines
1.8 KiB
TypeScript
import { tokenizeCustomCommandTemplate } from './commit-message-prompt'
|
|
|
|
export type AgentStartupShell = 'posix' | 'powershell' | 'cmd'
|
|
|
|
export function resolveStartupShell(
|
|
platform: NodeJS.Platform,
|
|
shell?: AgentStartupShell
|
|
): AgentStartupShell {
|
|
return shell ?? (platform === 'win32' ? 'powershell' : 'posix')
|
|
}
|
|
|
|
export function quoteStartupArg(value: string, shell: AgentStartupShell): string {
|
|
if (shell === 'powershell') {
|
|
return `'${value.replace(/'/g, "''")}'`
|
|
}
|
|
if (shell === 'cmd') {
|
|
return `"${value.replace(/([\^&|<>()%!"])/g, '^$1')}"`
|
|
}
|
|
return `'${value.replace(/'/g, `'\\''`)}'`
|
|
}
|
|
|
|
export function buildShellCommandFromArgv(
|
|
args: readonly string[],
|
|
shell: AgentStartupShell
|
|
): string {
|
|
const command = args.map((arg) => quoteStartupArg(arg, shell)).join(' ')
|
|
if (shell === 'powershell' && command) {
|
|
return `& ${command}`
|
|
}
|
|
return command
|
|
}
|
|
|
|
export function clearEnvCommand(name: string, shell: AgentStartupShell): string {
|
|
if (shell === 'powershell') {
|
|
return `Remove-Item Env:${name} -ErrorAction SilentlyContinue`
|
|
}
|
|
if (shell === 'cmd') {
|
|
return `set "${name}="`
|
|
}
|
|
return `unset ${name}`
|
|
}
|
|
|
|
export function commandSeparator(shell: AgentStartupShell): string {
|
|
return shell === 'cmd' ? ' & ' : '; '
|
|
}
|
|
|
|
export type AgentCliArgsPlan = { ok: true; suffix: string } | { ok: false; error: string }
|
|
|
|
export function planAgentCliArgsSuffix(
|
|
agentArgs: string | null | undefined,
|
|
shell: AgentStartupShell
|
|
): AgentCliArgsPlan {
|
|
const trimmed = agentArgs?.trim()
|
|
if (!trimmed) {
|
|
return { ok: true, suffix: '' }
|
|
}
|
|
const tokenized = tokenizeCustomCommandTemplate(trimmed)
|
|
if (!tokenized.ok) {
|
|
return { ok: false, error: `CLI arguments are invalid: ${tokenized.error}` }
|
|
}
|
|
return {
|
|
ok: true,
|
|
suffix: tokenized.tokens.map((token) => quoteStartupArg(token, shell)).join(' ')
|
|
}
|
|
}
|