diff --git a/src/commands/chat.ts b/src/commands/chat.ts index 72848affe..82c3fa222 100644 --- a/src/commands/chat.ts +++ b/src/commands/chat.ts @@ -18,6 +18,7 @@ import { startTUIChat } from "../ui/index.js"; import { safeStdout } from "../util/consoleOverride.js"; import { formatError } from "../util/formatError.js"; import logger from "../util/logger.js"; +import { processCommandFlags } from "../flags/flagProcessor.js"; import { ExtendedCommandOptions } from "./BaseCommandOptions.js"; /** @@ -164,24 +165,13 @@ async function runHeadlessMode( options: ChatOptions ): Promise { // Initialize services for headless mode - // Convert legacy flags to mode - let mode: any = undefined; - if (options.readonly) { - mode = "plan"; - } else if (options.auto) { - mode = "auto"; - } + const { permissionOverrides } = processCommandFlags(options); await initializeServices({ configPath: options.config, rules: options.rule, headless: true, - toolPermissionOverrides: { - allow: options.allow, - ask: options.ask, - exclude: options.exclude, - mode: mode, - }, + toolPermissionOverrides: permissionOverrides, }); // Get required services from the service container @@ -255,21 +245,11 @@ export async function chat(prompt?: string, options: ChatOptions = {}) { // Show ASCII art and version for TUI mode console.log(CONTINUE_ASCII_ART); - // Convert legacy flags to mode for TUI - let mode: any = undefined; - if (options.readonly) { - mode = "plan"; - } else if (options.auto) { - mode = "auto"; - } + // Process flags for TUI mode + const { permissionOverrides } = processCommandFlags(options); // Start TUI immediately - it will handle service loading - await startTUIChat(prompt, options.resume, options.config, options.rule, { - allow: options.allow, - ask: options.ask, - exclude: options.exclude, - mode: mode, - }); + await startTUIChat(prompt, options.resume, options.config, options.rule, permissionOverrides); return; } diff --git a/src/flags/flagProcessor.test.ts b/src/flags/flagProcessor.test.ts new file mode 100644 index 000000000..252c54448 --- /dev/null +++ b/src/flags/flagProcessor.test.ts @@ -0,0 +1,145 @@ +import { + convertLegacyModeFlags, + buildPermissionOverrides, + processCommandFlags, +} from "./flagProcessor.js"; + +describe("convertLegacyModeFlags", () => { + test("should return 'plan' when readonly is true", () => { + expect(convertLegacyModeFlags(true, false)).toBe("plan"); + }); + + test("should return 'auto' when auto is true", () => { + expect(convertLegacyModeFlags(false, true)).toBe("auto"); + }); + + test("should return undefined when both flags are false", () => { + expect(convertLegacyModeFlags(false, false)).toBeUndefined(); + }); + + test("should return undefined when no flags provided", () => { + expect(convertLegacyModeFlags()).toBeUndefined(); + }); + + test("should throw error when both readonly and auto are true", () => { + expect(() => convertLegacyModeFlags(true, true)).toThrow( + "Cannot use both --readonly and --auto flags together" + ); + }); +}); + +describe("buildPermissionOverrides", () => { + test("should build permission overrides with all parameters", () => { + const result = buildPermissionOverrides( + ["readFile"], + ["writeFile"], + ["runTerminalCommand"], + "plan" + ); + + expect(result).toEqual({ + allow: ["readFile"], + ask: ["writeFile"], + exclude: ["runTerminalCommand"], + mode: "plan", + }); + }); + + test("should handle undefined parameters", () => { + const result = buildPermissionOverrides(); + + expect(result).toEqual({ + allow: undefined, + ask: undefined, + exclude: undefined, + mode: undefined, + }); + }); + + test("should handle partial parameters", () => { + const result = buildPermissionOverrides(["readFile"], undefined, ["Write"]); + + expect(result).toEqual({ + allow: ["readFile"], + ask: undefined, + exclude: ["Write"], + mode: undefined, + }); + }); +}); + +describe("processCommandFlags", () => { + test("should process readonly flag correctly", () => { + const result = processCommandFlags({ + readonly: true, + allow: ["readFile"], + exclude: ["Write"], + }); + + expect(result).toEqual({ + mode: "plan", + permissionOverrides: { + allow: ["readFile"], + ask: undefined, + exclude: ["Write"], + mode: "plan", + }, + }); + }); + + test("should process auto flag correctly", () => { + const result = processCommandFlags({ + auto: true, + ask: ["writeFile"], + }); + + expect(result).toEqual({ + mode: "auto", + permissionOverrides: { + allow: undefined, + ask: ["writeFile"], + exclude: undefined, + mode: "auto", + }, + }); + }); + + test("should handle no mode flags", () => { + const result = processCommandFlags({ + allow: ["readFile", "searchCode"], + }); + + expect(result).toEqual({ + mode: undefined, + permissionOverrides: { + allow: ["readFile", "searchCode"], + ask: undefined, + exclude: undefined, + mode: undefined, + }, + }); + }); + + test("should throw error for conflicting flags", () => { + expect(() => + processCommandFlags({ + readonly: true, + auto: true, + }) + ).toThrow("Cannot use both --readonly and --auto flags together"); + }); + + test("should handle empty options", () => { + const result = processCommandFlags({}); + + expect(result).toEqual({ + mode: undefined, + permissionOverrides: { + allow: undefined, + ask: undefined, + exclude: undefined, + mode: undefined, + }, + }); + }); +}); \ No newline at end of file diff --git a/src/flags/flagProcessor.ts b/src/flags/flagProcessor.ts new file mode 100644 index 000000000..be6bf1394 --- /dev/null +++ b/src/flags/flagProcessor.ts @@ -0,0 +1,83 @@ +/** + * Shared flag processing utilities to eliminate duplication between TUI and headless modes + */ + +import { PermissionMode } from "../permissions/types.js"; + +export interface PermissionOverrides { + allow?: string[]; + ask?: string[]; + exclude?: string[]; + mode?: PermissionMode; +} + +export interface ProcessedFlags { + mode?: PermissionMode; + permissionOverrides: PermissionOverrides; +} + +/** + * Converts legacy readonly/auto flags to the new mode system + * This logic was previously duplicated in chat.ts lines 168-173 and 259-264 + */ +export function convertLegacyModeFlags(readonly?: boolean, auto?: boolean): PermissionMode | undefined { + if (readonly && auto) { + throw new Error("Cannot use both --readonly and --auto flags together"); + } + + if (readonly) { + return "plan"; + } + + if (auto) { + return "auto"; + } + + return undefined; +} + +/** + * Structures permission overrides for service initialization + * This logic was previously duplicated between TUI and headless paths + */ +export function buildPermissionOverrides( + allow?: string[], + ask?: string[], + exclude?: string[], + mode?: PermissionMode +): PermissionOverrides { + return { + allow, + ask, + exclude, + mode, + }; +} + +/** + * Processes all command line flags into a consistent structure + * Eliminates duplication and provides single source of truth for flag processing + */ +export function processCommandFlags(options: { + readonly?: boolean; + auto?: boolean; + allow?: string[]; + ask?: string[]; + exclude?: string[]; +}): ProcessedFlags { + // Convert legacy flags to mode + const mode = convertLegacyModeFlags(options.readonly, options.auto); + + // Build permission overrides + const permissionOverrides = buildPermissionOverrides( + options.allow, + options.ask, + options.exclude, + mode + ); + + return { + mode, + permissionOverrides, + }; +} \ No newline at end of file