refactor: extract shared flag processing logic to eliminate duplication

Extract common flag processing logic from TUI and headless code paths into
shared flagProcessor module. This eliminates code duplication where both
paths independently converted readonly/auto flags to mode and structured
permission overrides.

Changes:
- Add src/flags/flagProcessor.ts with shared flag processing functions
- Replace duplicated logic in chat.ts (lines 168-173 and 259-264)
- Add comprehensive unit tests for all flag processing scenarios
- Add validation for conflicting --readonly and --auto flags

This is a pure refactor with no behavior changes. All existing functionality
preserved while eliminating 20+ lines of duplicated code.
This commit is contained in:
Chad Metcalf 2025-08-03 15:14:54 -07:00
parent 48fd27d6bb
commit f1961e7e54
No known key found for this signature in database
3 changed files with 234 additions and 26 deletions

View file

@ -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<void> {
// 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;
}

View file

@ -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,
},
});
});
});

View file

@ -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,
};
}