mirror of
https://github.com/stablyai/orca.git
synced 2026-09-22 00:02:31 +00:00
79 lines
3.3 KiB
TypeScript
79 lines
3.3 KiB
TypeScript
import { tokenizeStartupCommand, type AgentStartupShell } from './tui-agent-startup-shell'
|
|
|
|
export const ORCA_OMP_FRESH_CONFIG_ENV = 'ORCA_OMP_FRESH_CONFIG'
|
|
export const OMP_FRESH_CONFIG_FILENAME = 'fresh-session.yml'
|
|
export const OMP_FRESH_CONFIG_SOURCE = 'autoResume: false\n'
|
|
|
|
// Unknown flags may consume values or select a subcommand; leave those commands intact.
|
|
const VALUE_FLAGS = new Set([
|
|
'--model',
|
|
'--provider',
|
|
'--thinking',
|
|
'--config',
|
|
'--profile',
|
|
'--extension',
|
|
'-e',
|
|
'--system-prompt',
|
|
'--append-system-prompt',
|
|
'--tools',
|
|
'--skill',
|
|
'--theme',
|
|
'--api-key'
|
|
])
|
|
const SWITCH_FLAGS = new Set(['--no-extensions', '--no-skills', '--no-prompt-templates'])
|
|
|
|
/** Apply fresh intent to one launch command, never the saved resume configuration. */
|
|
export function withFreshOmpLaunch(command: string, shell: AgentStartupShell, suffix = ''): string {
|
|
const parsed = tokenizeStartupCommand(command, shell)
|
|
if (!parsed.ok || parsed.spans.some((span) => span.divergesFromShell)) {
|
|
return command + suffix
|
|
}
|
|
const executable = parsed.tokens[0]?.split(/[\\/]/).at(-1)?.toLowerCase()
|
|
if (!['omp', 'omp.exe', 'omp.cmd', 'omp.bat', 'omp.sh', 'omp.js'].includes(executable ?? '')) {
|
|
return command + suffix
|
|
}
|
|
let index = parsed.tokens[1] === 'launch' ? 2 : 1
|
|
for (; index < parsed.tokens.length; index++) {
|
|
const token = parsed.tokens[index]
|
|
const equals = token.indexOf('=')
|
|
const flag = equals === -1 ? token : token.slice(0, equals)
|
|
if (VALUE_FLAGS.has(flag)) {
|
|
if (equals === -1 && ++index >= parsed.tokens.length) {
|
|
return command + suffix
|
|
}
|
|
} else if (!SWITCH_FLAGS.has(token)) {
|
|
return command + suffix
|
|
}
|
|
}
|
|
const path =
|
|
shell === 'cmd'
|
|
? `"%${ORCA_OMP_FRESH_CONFIG_ENV}%"`
|
|
: shell === 'powershell'
|
|
? `"$env:${ORCA_OMP_FRESH_CONFIG_ENV}"`
|
|
: `"$${ORCA_OMP_FRESH_CONFIG_ENV}"`
|
|
const launch = `${command} --config ${path}${suffix}`
|
|
const error =
|
|
'Orca: fresh OMP settings are unavailable on this host. Restart the terminal after updating Orca.'
|
|
if (shell === 'powershell') {
|
|
return `if (${path} -and (Test-Path -LiteralPath ${path} -PathType Leaf)) { ${launch} } else { $global:LASTEXITCODE = 1; Write-Error '${error}' }`
|
|
}
|
|
if (shell === 'cmd') {
|
|
return `if exist ${path} (if not exist "${path.slice(1, -1)}\\*" (${launch}) else (echo ${error} 1>&2 & verify invalid 2>nul)) else (echo ${error} 1>&2 & verify invalid 2>nul)`
|
|
}
|
|
const available = `printenv ${ORCA_OMP_FRESH_CONFIG_ENV} >/dev/null && test -f ${path}`
|
|
// && is supported by bash, zsh, and fish; invoking directly preserves shell aliases/functions.
|
|
return `${available} || printf '%s\\n' '${error}' >&2; ${available} && ${launch}`
|
|
}
|
|
|
|
/** Recognize only the host-settings guards generated by this module. */
|
|
export function isFreshOmpLaunchCommand(command: string | undefined): boolean {
|
|
return Boolean(
|
|
command &&
|
|
[
|
|
`printenv ${ORCA_OMP_FRESH_CONFIG_ENV} >/dev/null && test -f "$${ORCA_OMP_FRESH_CONFIG_ENV}"`,
|
|
`if ("$env:${ORCA_OMP_FRESH_CONFIG_ENV}" -and (Test-Path -LiteralPath "$env:${ORCA_OMP_FRESH_CONFIG_ENV}" -PathType Leaf))`,
|
|
`if exist "%${ORCA_OMP_FRESH_CONFIG_ENV}%" (if not exist "%${ORCA_OMP_FRESH_CONFIG_ENV}%\\*"`
|
|
].some((prefix) => command.startsWith(prefix))
|
|
)
|
|
}
|