Skip to content

Commit

Permalink
feat: Add --redo functionality (#65)
Browse files Browse the repository at this point in the history
* chore: add the task cache file to gitignore

* feat: add --redo feature

* test: add test coverage

* test: fix windows paths

* test: fix windows

* fix: use the home directory to store the task cache file

this is so it isn't deleted by an --undo operation

* chore: add a better error message for diff mode for hallucination

* docs: document the redo option
  • Loading branch information
gmickel authored Aug 3, 2024
1 parent edcca5f commit 60c262a
Show file tree
Hide file tree
Showing 11 changed files with 938 additions and 332 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,4 @@ ElPlan.md
ElPlanFilter.md
codewhisper-task-output.json
demotask.md
.codewhisper-task-cache.json
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ codewhisper task -m ollama:llama3.1:70b --context-window 131072 --max-tokens 819

# To undo changes made by an AI-assisted task, use the --undo option
codewhisper task --undo

# To redo the last task with the option to change the model, file selection or plan, use the --redo option
codewhisper task --redo
```

> Note: If you are using CodeWhisper's LLM integration with `codewhisper task` you will need to set the respective environment variable for the model you want to use (e.g. `export ANTHROPIC_API_KEY=your_api_key` or `export OPENAI_API_KEY=your_api_key` or `export GROQ_API_KEY=your_api_key` ).
Expand Down Expand Up @@ -282,6 +285,43 @@ The command will always ask for confirmation before making any changes. It will

Always review the proposed changes carefully before confirming, as this operation may result in loss of work.

Here's the updated section for the README.md file that includes information about the new --redo functionality:

### Redoing AI-Assisted Tasks

CodeWhisper now supports redoing AI-assisted tasks from the review plan stage. This feature allows you to restart your last task with the option to modify the generated plan as well as the model and file selection. To use this feature, you can use the `--redo` option with the `task` command:

```bash
codewhisper task --redo
```

When you use the `--redo` option:

1. CodeWhisper will retrieve the last task data for the current project directory.
2. It will display the details of the last task, including the task description, instructions, model used, and files included.
3. You'll be given the option to change the AI model for code generation.
4. You'll also have the opportunity to modify the file selection for the task.
5. The task will then continue from the review plan stage, where you can then modify the plan to your liking.

This feature is particularly useful when:
- You want to try a different AI model for the same task
- You need to adjust the file selection for the same task
- You want to quickly tweak the plan without starting from scratch

Note: The redo functionality uses a cache stored in your home directory, so it persists across different sessions and is not affected by git operations like branch switching or resetting.

Example workflow:

1. Run an initial task: `codewhisper task`
2. Review the code modifications and decide you want to tweak the plan or try a different model
3. Undo the task: `codewhisper task --undo`
3. Redo the task: `codewhisper task --redo`
4. Optionally change the model when prompted
5. Optionally adjust the file selection
6. Adjust the plan as needed

This feature enhances the flexibility of CodeWhisper's AI-assisted workflow, allowing for quick iterations and experimentation with different models or scopes for your tasks.

## 📝 Templates

CodeWhisper uses Handlebars templates to generate output. You can use pre-defined templates or create custom ones. For in-depth information on templating, see [TEMPLATES.md](TEMPLATES.md).
Expand Down
32 changes: 32 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ codewhisper task [options]
| `-max, --max-cost-threshold <number>` | Set a maximum cost threshold for AI operations in USD (e.g., 0.5 for $0.50) |
| `--auto-commit` | Automatically commit changes |
| `--undo` | Undo AI-assisted task changes |
| `--redo` | Redo the last task for the specified path with the option to change the generated plan as well as the model and file selection (see below) |

#### Model Selection

Expand Down Expand Up @@ -366,6 +367,37 @@ This command will:

The command will always ask for confirmation before making any changes.

25. Redo the last task with the option to change the generated plan as well as the model and file selection:

```bash
codewhisper task --redo
```

This command will:

- Retrieve the last task data for the current project directory
- Display the details of the last task, including the task description, instructions, model used, and files included
- You'll be given the option to change the AI model for code generation
- You'll also have the opportunity to modify the file selection for the task
- The task will then continue from the review plan stage, where you can then modify the plan to your liking

This feature is particularly useful when:
- You want to try a different AI model for the same task
- You need to adjust the file selection for the same task
- You want to quickly tweak the plan without starting from scratch

Note: The redo functionality uses a cache stored in your home directory, so it persists across different sessions and is not affected by git operations like branch switching or resetting.

Example workflow:

1. Run an initial task: `codewhisper task`
2. Review the code modifications and decide you want to tweak the plan or try a different model
3. Undo the task: `codewhisper task --undo`
3. Redo the task: `codewhisper task --redo`
4. Optionally change the model when prompted
5. Optionally adjust the file selection
6. Adjust the plan as needed

## CI/CD Integration

CodeWhisper can be easily integrated into your CI/CD pipeline. Here's an example of how to use CodeWhisper in a GitHub Actions workflow:
Expand Down
94 changes: 94 additions & 0 deletions src/ai/redo-task.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import path from 'node:path';
import { confirm } from '@inquirer/prompts';
import chalk from 'chalk';
import ora from 'ora';
import { selectFilesPrompt } from '../interactive/select-files-prompt';
import { selectModelPrompt } from '../interactive/select-model-prompt';
import type { AiAssistedTaskOptions } from '../types';
import { TaskCache } from '../utils/task-cache';
import { getModelConfig } from './model-config';
import { continueTaskWorkflow } from './task-workflow';

export async function redoLastTask(options: AiAssistedTaskOptions) {
const spinner = ora();
try {
const basePath = path.resolve(options.path ?? '.');
const taskCache = new TaskCache(basePath);

const lastTaskData = taskCache.getLastTaskData(basePath);

if (!lastTaskData) {
console.log(
chalk.yellow(
'No previous task found for this directory. Please run a new task.',
),
);
process.exit(0);
}

console.log(chalk.blue('Redoing last task:'));
console.log(chalk.cyan('Task Description:'), lastTaskData.taskDescription);
console.log(chalk.cyan('Instructions:'), lastTaskData.instructions);
console.log(chalk.cyan('Model:'), lastTaskData.model);
console.log(chalk.cyan('Files included:'));
for (const file of lastTaskData.selectedFiles) {
console.log(chalk.cyan(` ${file}`));
}

let modelKey = lastTaskData.model || options.model;
let selectedFiles = lastTaskData.selectedFiles;

// Confirmation for changing the model
const changeModel = await confirm({
message: 'Do you want to change the AI model for code generation?',
default: false,
});

if (changeModel) {
modelKey = await selectModelPrompt();
console.log(
chalk.blue(`Using new model: ${getModelConfig(modelKey).modelName}`),
);
}

// Confirmation for changing file selection
const changeFiles = await confirm({
message: 'Do you want to change the file selection for code generation?',
default: false,
});

if (changeFiles) {
selectedFiles = await selectFilesPrompt(
basePath,
options.invert ?? false,
);
console.log(chalk.cyan('New file selection:'));
for (const file of selectedFiles) {
console.log(chalk.cyan(` ${file}`));
}
}

// Update the task cache with new selections if changed
if (changeModel || changeFiles) {
taskCache.setTaskData(basePath, {
...lastTaskData,
model: modelKey,
selectedFiles,
});
}

await continueTaskWorkflow(
{ ...options, model: modelKey },
basePath,
taskCache,
lastTaskData.generatedPlan,
modelKey,
);
} catch (error) {
spinner.fail('Error in redoing AI-assisted task');
console.error(
chalk.red(error instanceof Error ? error.message : String(error)),
);
process.exit(1);
}
}
Loading

0 comments on commit 60c262a

Please sign in to comment.