|
| 1 | +import type { TextEditor, Uri } from 'vscode'; |
| 2 | +import { ProgressLocation } from 'vscode'; |
| 3 | +import type { Container } from '../container'; |
| 4 | +import { GitUri } from '../git/gitUri'; |
| 5 | +import { uncommitted, uncommittedStaged } from '../git/models/revision'; |
| 6 | +import { showGenericErrorMessage } from '../messages'; |
| 7 | +import type { AIExplainSource } from '../plus/ai/aiProviderService'; |
| 8 | +import { getBestRepositoryOrShowPicker } from '../quickpicks/repositoryPicker'; |
| 9 | +import { command } from '../system/-webview/command'; |
| 10 | +import { showMarkdownPreview } from '../system/-webview/markdown'; |
| 11 | +import { Logger } from '../system/logger'; |
| 12 | +import { GlCommandBase } from './commandBase'; |
| 13 | +import { getCommandUri } from './commandBase.utils'; |
| 14 | +import type { CommandContext } from './commandContext'; |
| 15 | +import { isCommandContextViewNodeHasRepoPath, isCommandContextViewNodeHasRepository } from './commandContext.utils'; |
| 16 | + |
| 17 | +export interface ExplainWipCommandArgs { |
| 18 | + repoPath?: string | Uri; |
| 19 | + staged?: boolean; |
| 20 | + source?: AIExplainSource; |
| 21 | +} |
| 22 | + |
| 23 | +@command() |
| 24 | +export class ExplainWipCommand extends GlCommandBase { |
| 25 | + constructor(private readonly container: Container) { |
| 26 | + super('gitlens.ai.explainWip'); |
| 27 | + } |
| 28 | + |
| 29 | + protected override preExecute(context: CommandContext, args?: ExplainWipCommandArgs): Promise<void> { |
| 30 | + if (isCommandContextViewNodeHasRepository(context)) { |
| 31 | + args = { ...args }; |
| 32 | + args.repoPath = context.node.repo.path; |
| 33 | + args.source = args.source ?? { source: 'view', type: 'wip' }; |
| 34 | + } else if (isCommandContextViewNodeHasRepoPath(context)) { |
| 35 | + args = { ...args }; |
| 36 | + args.repoPath = context.node.repoPath; |
| 37 | + args.source = args.source ?? { source: 'view', type: 'wip' }; |
| 38 | + } |
| 39 | + |
| 40 | + return this.execute(context.editor, context.uri, args); |
| 41 | + } |
| 42 | + |
| 43 | + async execute(editor?: TextEditor, uri?: Uri, args?: ExplainWipCommandArgs): Promise<void> { |
| 44 | + args = { ...args }; |
| 45 | + |
| 46 | + let repository; |
| 47 | + if (args?.repoPath != null) { |
| 48 | + repository = this.container.git.getRepository(args.repoPath); |
| 49 | + } |
| 50 | + |
| 51 | + if (repository == null) { |
| 52 | + uri = getCommandUri(uri, editor); |
| 53 | + const gitUri = uri != null ? await GitUri.fromUri(uri) : undefined; |
| 54 | + repository = await getBestRepositoryOrShowPicker( |
| 55 | + gitUri, |
| 56 | + editor, |
| 57 | + 'Explain Working Changes', |
| 58 | + 'Choose which repository to explain working changes from', |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + if (repository == null) return; |
| 63 | + |
| 64 | + try { |
| 65 | + // Get the diff of working changes |
| 66 | + const diffService = repository.git.diff(); |
| 67 | + if (diffService?.getDiff === undefined) { |
| 68 | + void showGenericErrorMessage('Unable to get diff service'); |
| 69 | + return; |
| 70 | + } |
| 71 | + |
| 72 | + // If args?.staged is undefined, should we get all changes (staged and unstaged)? |
| 73 | + let stagedLabel; |
| 74 | + let to; |
| 75 | + if (args?.staged === true) { |
| 76 | + stagedLabel = 'Staged'; |
| 77 | + to = uncommittedStaged; |
| 78 | + } else { |
| 79 | + stagedLabel = 'Unstaged'; |
| 80 | + to = uncommitted; |
| 81 | + } |
| 82 | + |
| 83 | + const diff = await diffService.getDiff(to); |
| 84 | + if (!diff?.contents) { |
| 85 | + void showGenericErrorMessage('No working changes found to explain'); |
| 86 | + return; |
| 87 | + } |
| 88 | + |
| 89 | + // Call the AI service to explain the changes |
| 90 | + const result = await this.container.ai.explainChanges( |
| 91 | + { |
| 92 | + diff: diff.contents, |
| 93 | + message: `${stagedLabel} working changes`, |
| 94 | + }, |
| 95 | + args.source ?? { source: 'commandPalette', type: 'wip' }, |
| 96 | + { |
| 97 | + progress: { location: ProgressLocation.Notification, title: 'Explaining working changes...' }, |
| 98 | + }, |
| 99 | + ); |
| 100 | + |
| 101 | + // Display the result |
| 102 | + let content = `# Working Changes Summary\n\n`; |
| 103 | + if (result != null) { |
| 104 | + content += `> Generated by ${result.model.name}\n\n## ${stagedLabel} Changes\n\n${result?.parsed.summary}\n\n${result?.parsed.body}`; |
| 105 | + } else { |
| 106 | + content += `> No changes found to explain.`; |
| 107 | + } |
| 108 | + void showMarkdownPreview(content); |
| 109 | + } catch (ex) { |
| 110 | + Logger.error(ex, 'ExplainWipCommand', 'execute'); |
| 111 | + void showGenericErrorMessage('Unable to explain working changes'); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments