Files
hapi/cli/src/claude/loop.ts
T
weishu 7a487269ea refactor: replace hardcoded permission/model modes with centralized schema validation
Extract permission and model mode validation into reusable functions utilizing
@hapi/protocol schemas. Use isPermissionModeAllowedForFlavor and
isModelModeAllowedForFlavor to validate modes based on session flavor (claude/codex).
Replace inline type definitions with shared types from @hapi/protocol.
2026-01-04 22:07:03 +08:00

80 lines
2.5 KiB
TypeScript

import { ApiSessionClient } from "@/api/apiSession"
import { MessageQueue2 } from "@/utils/MessageQueue2"
import { logger } from "@/ui/logger"
import { runLocalRemoteSession } from "@/agent/loopBase"
import { Session } from "./session"
import { claudeLocalLauncher } from "./claudeLocalLauncher"
import { claudeRemoteLauncher } from "./claudeRemoteLauncher"
import { ApiClient } from "@/lib"
import type { SessionModelMode } from "@/api/types"
import type { ClaudePermissionMode } from "@hapi/protocol/types"
export type PermissionMode = ClaudePermissionMode;
export interface EnhancedMode {
permissionMode: PermissionMode;
model?: string;
fallbackModel?: string;
customSystemPrompt?: string;
appendSystemPrompt?: string;
allowedTools?: string[];
disallowedTools?: string[];
}
interface LoopOptions {
path: string
model?: string
permissionMode?: PermissionMode
startingMode?: 'local' | 'remote'
startedBy?: 'daemon' | 'terminal'
onModeChange: (mode: 'local' | 'remote') => void
mcpServers: Record<string, any>
session: ApiSessionClient
api: ApiClient,
claudeEnvVars?: Record<string, string>
claudeArgs?: string[]
messageQueue: MessageQueue2<EnhancedMode>
allowedTools?: string[]
onSessionReady?: (session: Session) => void
hookSettingsPath: string
}
export async function loop(opts: LoopOptions) {
// Get log path for debug display
const logPath = logger.logFilePath;
const startedBy = opts.startedBy ?? 'terminal';
const startingMode = opts.startingMode ?? 'local';
const modelMode: SessionModelMode = opts.model === 'sonnet' || opts.model === 'opus'
? opts.model
: 'default';
const session = new Session({
api: opts.api,
client: opts.session,
path: opts.path,
sessionId: null,
claudeEnvVars: opts.claudeEnvVars,
claudeArgs: opts.claudeArgs,
mcpServers: opts.mcpServers,
logPath: logPath,
messageQueue: opts.messageQueue,
allowedTools: opts.allowedTools,
onModeChange: opts.onModeChange,
mode: startingMode,
startedBy,
startingMode,
hookSettingsPath: opts.hookSettingsPath,
permissionMode: opts.permissionMode ?? 'default',
modelMode
});
await runLocalRemoteSession({
session,
startingMode: opts.startingMode,
logTag: 'loop',
runLocal: claudeLocalLauncher,
runRemote: claudeRemoteLauncher,
onSessionReady: opts.onSessionReady
});
}