mirror of
https://github.com/wu736139669/hapi.git
synced 2026-11-18 03:27:08 +00:00
* feat(cli): add hapi set-mode to change a session's permission mode (#2094) The CLI could only pick a permission mode at launch; changing it on an existing session required the web UI or a hand-rolled JWT + curl call. `hapi set-mode <session-id-or-prefix> <mode>` resolves the session like ping-peer / inspect-peer, validates the mode against the session flavor's catalog (printing the allowed list on mismatch), and calls the existing POST /api/sessions/:id/permission-mode route. Active sessions switch live; inactive ones keep the mode for the next resume. No MCP tool is added: raising a peer's mode stays behind the agent's normal shell-approval path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix: reject Safe Yolo for shared sessions on the permission-mode route Shared (concurrentClients) Codex sessions cannot launch with safe-yolo. The web picker hides it, but the route only checked the flavor catalog, so `hapi set-mode` (or any API client) could store it on an inactive shared session and break its next resume. Guard the route and apply the same filter to the CLI's allowed-mode list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
103 lines
3.0 KiB
TypeScript
103 lines
3.0 KiB
TypeScript
import chalk from 'chalk'
|
|
import { initializeToken } from '@/ui/tokenInit'
|
|
import {
|
|
PingPeerError,
|
|
exitCodeForPingPeerError,
|
|
setPeerPermissionMode
|
|
} from '@/modules/pingPeer/pingPeer'
|
|
import type { CommandDefinition } from './types'
|
|
|
|
type ParsedSetModeArgs = {
|
|
help: boolean
|
|
sessionIdPrefix?: string
|
|
mode?: string
|
|
}
|
|
|
|
function showHelp(): void {
|
|
console.log(`
|
|
${chalk.bold('hapi set-mode')} - Change the permission mode of an existing HAPI session
|
|
|
|
${chalk.bold('Usage:')}
|
|
hapi set-mode <session-id-or-prefix> <mode>
|
|
|
|
${chalk.bold('Notes:')}
|
|
Same as the web permission picker. Active sessions switch immediately;
|
|
inactive sessions use the mode on their next resume.
|
|
Allowed modes depend on the agent; an invalid mode prints the allowed list.
|
|
Resolves by id prefix (8 chars OK). Same hub token/namespace as this CLI.
|
|
|
|
${chalk.bold('Env:')}
|
|
HAPI_API_URL / CLI_API_TOKEN (or ~/.hapi/settings.json via \`hapi auth login\`)
|
|
`)
|
|
}
|
|
|
|
export function parseSetModeArgs(args: string[]): ParsedSetModeArgs {
|
|
const result: ParsedSetModeArgs = { help: false }
|
|
|
|
for (const arg of args) {
|
|
if (arg === '--help' || arg === '-h') {
|
|
result.help = true
|
|
continue
|
|
}
|
|
if (arg.startsWith('-')) {
|
|
throw new PingPeerError('bad_args', `unexpected flag: ${arg}`)
|
|
}
|
|
if (!result.sessionIdPrefix) {
|
|
result.sessionIdPrefix = arg
|
|
continue
|
|
}
|
|
if (!result.mode) {
|
|
result.mode = arg
|
|
continue
|
|
}
|
|
throw new PingPeerError('bad_args', `unexpected arg: ${arg}`)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
export async function handleSetModeCommand(args: string[]): Promise<void> {
|
|
const parsed = parseSetModeArgs(args)
|
|
if (parsed.help) {
|
|
showHelp()
|
|
return
|
|
}
|
|
|
|
if (!parsed.sessionIdPrefix || !parsed.mode) {
|
|
showHelp()
|
|
throw new PingPeerError('bad_args', 'usage: hapi set-mode <session-id> <mode>')
|
|
}
|
|
|
|
await initializeToken()
|
|
|
|
const result = await setPeerPermissionMode({
|
|
sessionIdPrefix: parsed.sessionIdPrefix,
|
|
mode: parsed.mode
|
|
})
|
|
const when = result.active ? '' : ' (applies on next resume)'
|
|
console.log(chalk.green(`hapi set-mode: OK - ${result.sessionId} (${result.flavor}) is now ${result.mode}${when}`))
|
|
}
|
|
|
|
export const setModeCommand: CommandDefinition = {
|
|
name: 'set-mode',
|
|
requiresRuntimeAssets: false,
|
|
run: async ({ commandArgs }) => {
|
|
try {
|
|
await handleSetModeCommand(commandArgs)
|
|
} catch (error) {
|
|
if (error instanceof PingPeerError) {
|
|
console.error(chalk.red('hapi set-mode:'), error.message)
|
|
process.exit(exitCodeForPingPeerError(error))
|
|
}
|
|
console.error(
|
|
chalk.red('hapi set-mode:'),
|
|
error instanceof Error ? error.message : 'Unknown error'
|
|
)
|
|
if (process.env.DEBUG) {
|
|
console.error(error)
|
|
}
|
|
process.exit(1)
|
|
}
|
|
}
|
|
}
|