Add agent permission mode controls (#5440)
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { isTuiAgent } from './tui-agent-config'
|
||||
import { YOLO_TUI_AGENT_ARGS, YOLO_TUI_AGENT_ENV } from './tui-agent-permissions'
|
||||
import type { TuiAgent } from './types'
|
||||
|
||||
const UNSUPPORTED_TUI_AGENT_ARGS: Partial<Record<TuiAgent, readonly string[]>> = {
|
||||
@@ -6,35 +7,10 @@ const UNSUPPORTED_TUI_AGENT_ARGS: Partial<Record<TuiAgent, readonly string[]>> =
|
||||
kilo: ['--dangerously-skip-permissions']
|
||||
}
|
||||
|
||||
export const DEFAULT_TUI_AGENT_ARGS: Partial<Record<TuiAgent, string>> = {
|
||||
claude: '--dangerously-skip-permissions',
|
||||
'claude-agent-teams': '--dangerously-skip-permissions',
|
||||
openclaude: '--dangerously-skip-permissions',
|
||||
codex: '--dangerously-bypass-approvals-and-sandbox',
|
||||
gemini: '--yolo',
|
||||
antigravity: '--dangerously-skip-permissions',
|
||||
aider: '--yes-always',
|
||||
amp: '--dangerously-allow-all',
|
||||
kiro: '--trust-all-tools',
|
||||
crush: '--yolo',
|
||||
autohand: '--unrestricted',
|
||||
cline: '--auto-approve true',
|
||||
'command-code': '--yolo',
|
||||
continue: '--allow "*"',
|
||||
cursor: '--yolo',
|
||||
kimi: '--yolo',
|
||||
'mistral-vibe': '--agent auto-approve',
|
||||
'qwen-code': '--approval-mode yolo',
|
||||
rovo: '--yolo',
|
||||
hermes: '--yolo',
|
||||
copilot: '--yolo',
|
||||
grok: '--permission-mode bypassPermissions',
|
||||
devin: '--permission-mode bypass'
|
||||
}
|
||||
export const DEFAULT_TUI_AGENT_ARGS: Partial<Record<TuiAgent, string>> = YOLO_TUI_AGENT_ARGS
|
||||
|
||||
export const DEFAULT_TUI_AGENT_ENV: Partial<Record<TuiAgent, Record<string, string>>> = {
|
||||
goose: { GOOSE_MODE: 'auto' }
|
||||
}
|
||||
export const DEFAULT_TUI_AGENT_ENV: Partial<Record<TuiAgent, Record<string, string>>> =
|
||||
YOLO_TUI_AGENT_ENV
|
||||
|
||||
function argPattern(arg: string): RegExp {
|
||||
return new RegExp(`(^|\\s)${arg.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}(?=\\s|$)`, 'g')
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import {
|
||||
applyAgentPermissionMode,
|
||||
resolveAgentPermissionModeSummary,
|
||||
YOLO_TUI_AGENT_ARGS,
|
||||
YOLO_TUI_AGENT_ENV
|
||||
} from './tui-agent-permissions'
|
||||
|
||||
describe('tui agent permissions', () => {
|
||||
it('recognizes the current default profile as yolo', () => {
|
||||
expect(
|
||||
resolveAgentPermissionModeSummary({
|
||||
agentDefaultArgs: YOLO_TUI_AGENT_ARGS,
|
||||
agentDefaultEnv: YOLO_TUI_AGENT_ENV
|
||||
})
|
||||
).toBe('yolo')
|
||||
})
|
||||
|
||||
it('recognizes an empty profile as manual', () => {
|
||||
expect(resolveAgentPermissionModeSummary({ agentDefaultArgs: {}, agentDefaultEnv: {} })).toBe(
|
||||
'manual'
|
||||
)
|
||||
})
|
||||
|
||||
it('preserves custom agent arguments when applying manual mode', () => {
|
||||
const result = applyAgentPermissionMode({
|
||||
mode: 'manual',
|
||||
agentDefaultArgs: {
|
||||
claude: '--dangerously-skip-permissions',
|
||||
codex: '--model gpt-5'
|
||||
},
|
||||
agentDefaultEnv: YOLO_TUI_AGENT_ENV
|
||||
})
|
||||
|
||||
expect(result.agentDefaultArgs.claude).toBe('')
|
||||
expect(result.agentDefaultArgs.codex).toBe('--model gpt-5')
|
||||
expect(result.agentDefaultEnv.goose).toEqual({})
|
||||
})
|
||||
|
||||
it('reports mixed when custom arguments are present', () => {
|
||||
expect(
|
||||
resolveAgentPermissionModeSummary({
|
||||
agentDefaultArgs: {
|
||||
...YOLO_TUI_AGENT_ARGS,
|
||||
codex: '--model gpt-5'
|
||||
},
|
||||
agentDefaultEnv: YOLO_TUI_AGENT_ENV
|
||||
})
|
||||
).toBe('mixed')
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,143 @@
|
||||
import { TUI_AGENT_CONFIG } from './tui-agent-config'
|
||||
import type { TuiAgent } from './types'
|
||||
|
||||
export type AgentPermissionMode = 'yolo' | 'manual' | 'mixed'
|
||||
|
||||
export const YOLO_TUI_AGENT_ARGS: Partial<Record<TuiAgent, string>> = {
|
||||
claude: '--dangerously-skip-permissions',
|
||||
'claude-agent-teams': '--dangerously-skip-permissions',
|
||||
openclaude: '--dangerously-skip-permissions',
|
||||
codex: '--dangerously-bypass-approvals-and-sandbox',
|
||||
gemini: '--yolo',
|
||||
antigravity: '--dangerously-skip-permissions',
|
||||
aider: '--yes-always',
|
||||
amp: '--dangerously-allow-all',
|
||||
kiro: '--trust-all-tools',
|
||||
crush: '--yolo',
|
||||
autohand: '--unrestricted',
|
||||
cline: '--auto-approve true',
|
||||
'command-code': '--yolo',
|
||||
continue: '--allow "*"',
|
||||
cursor: '--yolo',
|
||||
kimi: '--yolo',
|
||||
'mistral-vibe': '--agent auto-approve',
|
||||
'qwen-code': '--approval-mode yolo',
|
||||
rovo: '--yolo',
|
||||
hermes: '--yolo',
|
||||
copilot: '--yolo',
|
||||
grok: '--permission-mode bypassPermissions',
|
||||
devin: '--permission-mode bypass'
|
||||
}
|
||||
|
||||
export const YOLO_TUI_AGENT_ENV: Partial<Record<TuiAgent, Record<string, string>>> = {
|
||||
goose: { GOOSE_MODE: 'auto' }
|
||||
}
|
||||
|
||||
const PERMISSION_AGENT_IDS = Object.keys(TUI_AGENT_CONFIG).filter(
|
||||
(agent): agent is TuiAgent => agent in YOLO_TUI_AGENT_ARGS || agent in YOLO_TUI_AGENT_ENV
|
||||
)
|
||||
|
||||
function normalizeArgs(value: string | null | undefined): string {
|
||||
return value?.trim() ?? ''
|
||||
}
|
||||
|
||||
function sameEnv(
|
||||
left: Record<string, string> | null | undefined,
|
||||
right: Record<string, string> | null | undefined
|
||||
): boolean {
|
||||
const leftEntries = Object.entries(left ?? {})
|
||||
const rightEntries = Object.entries(right ?? {})
|
||||
if (leftEntries.length !== rightEntries.length) {
|
||||
return false
|
||||
}
|
||||
return leftEntries.every(([name, value]) => right?.[name] === value)
|
||||
}
|
||||
|
||||
function resolveAgentPermissionMode(args: string, yoloArgs: string): AgentPermissionMode {
|
||||
if (!args) {
|
||||
return 'manual'
|
||||
}
|
||||
return args === yoloArgs ? 'yolo' : 'mixed'
|
||||
}
|
||||
|
||||
function resolveAgentEnvPermissionMode(
|
||||
env: Record<string, string> | null | undefined,
|
||||
yoloEnv: Record<string, string> | undefined
|
||||
): AgentPermissionMode {
|
||||
if (sameEnv(env, {})) {
|
||||
return 'manual'
|
||||
}
|
||||
return sameEnv(env, yoloEnv) ? 'yolo' : 'mixed'
|
||||
}
|
||||
|
||||
export function resolveAgentPermissionModeSummary(args: {
|
||||
agentDefaultArgs?: Partial<Record<TuiAgent, string>> | null
|
||||
agentDefaultEnv?: Partial<Record<TuiAgent, Record<string, string>>> | null
|
||||
}): AgentPermissionMode {
|
||||
let sawYolo = false
|
||||
let sawManual = false
|
||||
let sawMixed = false
|
||||
|
||||
for (const agent of PERMISSION_AGENT_IDS) {
|
||||
const modes: AgentPermissionMode[] = []
|
||||
if (agent in YOLO_TUI_AGENT_ARGS) {
|
||||
modes.push(
|
||||
resolveAgentPermissionMode(
|
||||
normalizeArgs(args.agentDefaultArgs?.[agent]),
|
||||
YOLO_TUI_AGENT_ARGS[agent] ?? ''
|
||||
)
|
||||
)
|
||||
}
|
||||
if (agent in YOLO_TUI_AGENT_ENV) {
|
||||
modes.push(
|
||||
resolveAgentEnvPermissionMode(args.agentDefaultEnv?.[agent], YOLO_TUI_AGENT_ENV[agent])
|
||||
)
|
||||
}
|
||||
for (const mode of modes) {
|
||||
if (mode === 'yolo') {
|
||||
sawYolo = true
|
||||
} else if (mode === 'manual') {
|
||||
sawManual = true
|
||||
} else {
|
||||
sawMixed = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (sawMixed || (sawYolo && sawManual)) {
|
||||
return 'mixed'
|
||||
}
|
||||
return sawYolo ? 'yolo' : 'manual'
|
||||
}
|
||||
|
||||
export function applyAgentPermissionMode(args: {
|
||||
mode: Exclude<AgentPermissionMode, 'mixed'>
|
||||
agentDefaultArgs?: Partial<Record<TuiAgent, string>> | null
|
||||
agentDefaultEnv?: Partial<Record<TuiAgent, Record<string, string>>> | null
|
||||
}): {
|
||||
agentDefaultArgs: Partial<Record<TuiAgent, string>>
|
||||
agentDefaultEnv: Partial<Record<TuiAgent, Record<string, string>>>
|
||||
} {
|
||||
const nextArgs = { ...args.agentDefaultArgs }
|
||||
const nextEnv = { ...args.agentDefaultEnv }
|
||||
|
||||
for (const agent of PERMISSION_AGENT_IDS) {
|
||||
if (agent in YOLO_TUI_AGENT_ARGS) {
|
||||
const yoloArgs = YOLO_TUI_AGENT_ARGS[agent] ?? ''
|
||||
const currentArgs = normalizeArgs(nextArgs[agent])
|
||||
if (!currentArgs || currentArgs === yoloArgs) {
|
||||
nextArgs[agent] = args.mode === 'yolo' ? yoloArgs : ''
|
||||
}
|
||||
}
|
||||
|
||||
if (agent in YOLO_TUI_AGENT_ENV) {
|
||||
const yoloEnv = YOLO_TUI_AGENT_ENV[agent]
|
||||
const currentEnv = nextEnv[agent]
|
||||
if (sameEnv(currentEnv, {}) || sameEnv(currentEnv, yoloEnv)) {
|
||||
nextEnv[agent] = args.mode === 'yolo' ? { ...yoloEnv } : {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { agentDefaultArgs: nextArgs, agentDefaultEnv: nextEnv }
|
||||
}
|
||||
Reference in New Issue
Block a user