diff --git a/packages/navie/src/command.ts b/packages/navie/src/command.ts index 4baace09fa..b0411a3cdb 100644 --- a/packages/navie/src/command.ts +++ b/packages/navie/src/command.ts @@ -12,6 +12,7 @@ export enum CommandMode { Suggest = 'suggest', Observe = 'observe', Review = 'review', + Diff = 'diff', Welcome = 'welcome', } diff --git a/packages/navie/src/commands/diff-command.ts b/packages/navie/src/commands/diff-command.ts new file mode 100644 index 0000000000..3563444a35 --- /dev/null +++ b/packages/navie/src/commands/diff-command.ts @@ -0,0 +1,47 @@ +import Command, { CommandRequest } from '../command'; +import { ProjectInfo } from '../project-info'; +import ProjectInfoService from '../services/project-info-service'; + +// Define output formats: text, json, jsonl +export type DiffOutputFormat = 'text' | 'json' | 'jsonl'; + +export interface ProjectDiffInfo { + directory: string; + diff?: string; +} + +// Transform ProjectInfo to ProjectDiffInfo +export function projectInfoToProjectDiffInfo(projectInfo: ProjectInfo): ProjectDiffInfo { + return { + directory: projectInfo.directory, + diff: projectInfo.diff, + }; +} + +export default class DiffCommand implements Command { + constructor(private readonly projectInfoService: ProjectInfoService) {} + + async *execute(request: CommandRequest): AsyncIterable { + const outputFormat = request.userOptions.stringValue('format') || 'text'; + const baseBranch = request.userOptions.stringValue('base'); + + const projectInfoResponse = await this.projectInfoService.lookupProjectInfo(true, baseBranch); + const projectInfo = Array.isArray(projectInfoResponse) + ? projectInfoResponse + : [projectInfoResponse]; + + if (outputFormat === 'text') { + for (const info of projectInfo) { + yield `Changes in ${info.directory}:\n`; + yield info.diff || 'No changes detected.'; + yield '\n'; + } + } else if (outputFormat === 'json') { + yield JSON.stringify(projectInfo.map(projectInfoToProjectDiffInfo), null, 2); + } else if (outputFormat === 'jsonl') { + for (const info of projectInfo) { + yield JSON.stringify(projectInfoToProjectDiffInfo(info)) + '\n'; + } + } + } +} diff --git a/packages/navie/src/navie.ts b/packages/navie/src/navie.ts index 1bb6ebf9f1..bd1b59e1ce 100644 --- a/packages/navie/src/navie.ts +++ b/packages/navie/src/navie.ts @@ -41,6 +41,7 @@ import ReviewCommand from './commands/review-command'; import WelcomeCommand from './commands/welcome-command'; import InvokeTestsService from './services/invoke-tests-service'; import { TestInvocationProvider } from './test-invocation'; +import DiffCommand from './commands/diff-command'; export type ChatHistory = Message[]; @@ -195,6 +196,8 @@ export default function navie( projectInfoService ); + const buildDiffCommand = () => new DiffCommand(projectInfoService); + const buildReviewCommand = () => new ReviewCommand( options, @@ -217,6 +220,7 @@ export default function navie( [CommandMode.Suggest]: buildSuggestCommand, [CommandMode.Observe]: buildObserveCommand, [CommandMode.Review]: buildReviewCommand, + [CommandMode.Diff]: buildDiffCommand, [CommandMode.Welcome]: buildWelcomeCommand, };