Skip to content

Commit 3c8a286

Browse files
Optional Fast Apply (#61)
1 parent 3ed04b3 commit 3c8a286

File tree

5 files changed

+502
-3
lines changed

5 files changed

+502
-3
lines changed

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ A conversational AI CLI tool powered by Grok with intelligent text editor capabi
1010
- **📝 Smart File Operations**: AI automatically uses tools to view, create, and edit files
1111
- **⚡ Bash Integration**: Execute shell commands through natural conversation
1212
- **🔧 Automatic Tool Selection**: AI intelligently chooses the right tools for your requests
13+
- **🚀 Morph Fast Apply**: Optional high-speed code editing at 4,500+ tokens/sec with 98% accuracy
1314
- **🔌 MCP Tools**: Extend capabilities with Model Context Protocol servers (Linear, GitHub, etc.)
1415
- **💬 Interactive UI**: Beautiful terminal interface built with Ink
1516
- **🌍 Global Installation**: Install and use anywhere with `npm i -g @vibe-kit/grok-cli`
@@ -19,6 +20,7 @@ A conversational AI CLI tool powered by Grok with intelligent text editor capabi
1920
### Prerequisites
2021
- Node.js 16+
2122
- Grok API key from X.AI
23+
- (Optional, Recommended) Morph API key for Fast Apply editing
2224

2325
### Global Installation (Recommended)
2426
```bash
@@ -64,6 +66,21 @@ Create `~/.grok/user-settings.json`:
6466
}
6567
```
6668

69+
3. (Optional, Recommended) Get your Morph API key from [Morph Dashboard](https://morphllm.com/dashboard/api-keys)
70+
71+
4. Set up your Morph API key for Fast Apply editing (choose one method):
72+
73+
**Method 1: Environment Variable**
74+
```bash
75+
export MORPH_API_KEY=your_morph_api_key_here
76+
```
77+
78+
**Method 2: .env File**
79+
```bash
80+
# Add to your .env file
81+
MORPH_API_KEY=your_morph_api_key_here
82+
```
83+
6784
### Custom Base URL (Optional)
6885

6986
By default, the CLI uses `https://api.x.ai/v1` as the Grok API endpoint. You can configure a custom endpoint if needed (choose one method):
@@ -291,6 +308,35 @@ Follow the existing code style and patterns in this project.
291308

292309
Grok will automatically load and follow these instructions when working in your project directory. The custom instructions are added to Grok's system prompt and take priority over default behavior.
293310

311+
## Morph Fast Apply (Optional)
312+
313+
Grok CLI supports Morph's Fast Apply model for high-speed code editing at **4,500+ tokens/sec with 98% accuracy**. This is an optional feature that provides lightning-fast file editing capabilities.
314+
315+
**Setup**: Configure your Morph API key following the [setup instructions](#setup) above.
316+
317+
### How It Works
318+
319+
When `MORPH_API_KEY` is configured:
320+
- **`edit_file` tool becomes available** alongside the standard `str_replace_editor`
321+
- **Optimized for complex edits**: Use for multi-line changes, refactoring, and large modifications
322+
- **Intelligent editing**: Uses abbreviated edit format with `// ... existing code ...` comments
323+
- **Fallback support**: Standard tools remain available if Morph is unavailable
324+
325+
**When to use each tool:**
326+
- **`edit_file`** (Morph): Complex edits, refactoring, multi-line changes
327+
- **`str_replace_editor`**: Simple text replacements, single-line edits
328+
329+
### Example Usage
330+
331+
With Morph Fast Apply configured, you can request complex code changes:
332+
333+
```bash
334+
grok --prompt "refactor this function to use async/await and add error handling"
335+
grok -p "convert this class to TypeScript and add proper type annotations"
336+
```
337+
338+
The AI will automatically choose between `edit_file` (Morph) for complex changes or `str_replace_editor` for simple replacements.
339+
294340
## MCP Tools
295341

296342
Grok CLI supports MCP (Model Context Protocol) servers, allowing you to extend the AI assistant with additional tools and capabilities.

src/agent/grok-agent.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { loadMCPConfig } from "../mcp/config";
1010
import {
1111
TextEditorTool,
12+
MorphEditorTool,
1213
BashTool,
1314
TodoTool,
1415
ConfirmationTool,
@@ -42,6 +43,7 @@ export interface StreamingChunk {
4243
export class GrokAgent extends EventEmitter {
4344
private grokClient: GrokClient;
4445
private textEditor: TextEditorTool;
46+
private morphEditor: MorphEditorTool | null;
4547
private bash: BashTool;
4648
private todoTool: TodoTool;
4749
private confirmationTool: ConfirmationTool;
@@ -61,6 +63,7 @@ export class GrokAgent extends EventEmitter {
6163
this.maxToolRounds = maxToolRounds || 400;
6264
this.grokClient = new GrokClient(apiKey, modelToUse, baseURL);
6365
this.textEditor = new TextEditorTool();
66+
this.morphEditor = process.env.MORPH_API_KEY ? new MorphEditorTool() : null;
6467
this.bash = new BashTool();
6568
this.todoTool = new TodoTool();
6669
this.confirmationTool = new ConfirmationTool();
@@ -84,7 +87,7 @@ export class GrokAgent extends EventEmitter {
8487
You have access to these tools:
8588
- view_file: View file contents or directory listings
8689
- create_file: Create new files with content (ONLY use this for files that don't exist yet)
87-
- str_replace_editor: Replace text in existing files (ALWAYS use this to edit or update existing files)
90+
- str_replace_editor: Replace text in existing files (ALWAYS use this to edit or update existing files)${this.morphEditor ? '\n- edit_file: High-speed file editing with Morph Fast Apply (4,500+ tokens/sec with 98% accuracy)' : ''}
8891
- bash: Execute bash commands (use for searching, file discovery, navigation, and system operations)
8992
- search: Unified search tool for finding text content or files (similar to Cursor's search functionality)
9093
- create_todo_list: Create a visual todo list for planning and tracking tasks
@@ -618,6 +621,19 @@ Current working directory: ${process.cwd()}`,
618621
args.replace_all
619622
);
620623

624+
case "edit_file":
625+
if (!this.morphEditor) {
626+
return {
627+
success: false,
628+
error: "Morph Fast Apply not available. Please set MORPH_API_KEY environment variable to use this feature.",
629+
};
630+
}
631+
return await this.morphEditor.editFile(
632+
args.target_file,
633+
args.instructions,
634+
args.code_edit
635+
);
636+
621637
case "bash":
622638
return await this.bash.execute(args.command);
623639

src/grok/tools.ts

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { GrokTool } from "./client";
22
import { MCPManager, MCPTool } from "../mcp/client";
33
import { loadMCPConfig } from "../mcp/config";
44

5-
export const GROK_TOOLS: GrokTool[] = [
5+
const BASE_GROK_TOOLS: GrokTool[] = [
66
{
77
type: "function",
88
function: {
@@ -54,7 +54,7 @@ export const GROK_TOOLS: GrokTool[] = [
5454
type: "function",
5555
function: {
5656
name: "str_replace_editor",
57-
description: "Replace specific text in a file",
57+
description: "Replace specific text in a file. Use this for single line edits only",
5858
parameters: {
5959
type: "object",
6060
properties: {
@@ -81,6 +81,7 @@ export const GROK_TOOLS: GrokTool[] = [
8181
},
8282
},
8383
},
84+
8485
{
8586
type: "function",
8687
function: {
@@ -242,6 +243,48 @@ export const GROK_TOOLS: GrokTool[] = [
242243
},
243244
];
244245

246+
// Morph Fast Apply tool (conditional)
247+
const MORPH_EDIT_TOOL: GrokTool = {
248+
type: "function",
249+
function: {
250+
name: "edit_file",
251+
description: "Use this tool to make an edit to an existing file.\n\nThis will be read by a less intelligent model, which will quickly apply the edit. You should make it clear what the edit is, while also minimizing the unchanged code you write.\nWhen writing the edit, you should specify each edit in sequence, with the special comment // ... existing code ... to represent unchanged code in between edited lines.\n\nFor example:\n\n// ... existing code ...\nFIRST_EDIT\n// ... existing code ...\nSECOND_EDIT\n// ... existing code ...\nTHIRD_EDIT\n// ... existing code ...\n\nYou should still bias towards repeating as few lines of the original file as possible to convey the change.\nBut, each edit should contain sufficient context of unchanged lines around the code you're editing to resolve ambiguity.\nDO NOT omit spans of pre-existing code (or comments) without using the // ... existing code ... comment to indicate its absence. If you omit the existing code comment, the model may inadvertently delete these lines.\nIf you plan on deleting a section, you must provide context before and after to delete it. If the initial code is ```code \\n Block 1 \\n Block 2 \\n Block 3 \\n code```, and you want to remove Block 2, you would output ```// ... existing code ... \\n Block 1 \\n Block 3 \\n // ... existing code ...```.\nMake sure it is clear what the edit should be, and where it should be applied.\nMake edits to a file in a single edit_file call instead of multiple edit_file calls to the same file. The apply model can handle many distinct edits at once.",
252+
parameters: {
253+
type: "object",
254+
properties: {
255+
target_file: {
256+
type: "string",
257+
description: "The target file to modify."
258+
},
259+
instructions: {
260+
type: "string",
261+
description: "A single sentence instruction describing what you are going to do for the sketched edit. This is used to assist the less intelligent model in applying the edit. Use the first person to describe what you are going to do. Use it to disambiguate uncertainty in the edit."
262+
},
263+
code_edit: {
264+
type: "string",
265+
description: "Specify ONLY the precise lines of code that you wish to edit. NEVER specify or write out unchanged code. Instead, represent all unchanged code using the comment of the language you're editing in - example: // ... existing code ..."
266+
}
267+
},
268+
required: ["target_file", "instructions", "code_edit"]
269+
}
270+
}
271+
};
272+
273+
// Function to build tools array conditionally
274+
function buildGrokTools(): GrokTool[] {
275+
const tools = [...BASE_GROK_TOOLS];
276+
277+
// Add Morph Fast Apply tool if API key is available
278+
if (process.env.MORPH_API_KEY) {
279+
tools.splice(3, 0, MORPH_EDIT_TOOL); // Insert after str_replace_editor
280+
}
281+
282+
return tools;
283+
}
284+
285+
// Export dynamic tools array
286+
export const GROK_TOOLS: GrokTool[] = buildGrokTools();
287+
245288
// Global MCP manager instance
246289
let mcpManager: MCPManager | null = null;
247290

src/tools/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { BashTool } from "./bash";
22
export { TextEditorTool } from "./text-editor";
3+
export { MorphEditorTool } from "./morph-editor";
34
export { TodoTool } from "./todo-tool";
45
export { ConfirmationTool } from "./confirmation-tool";
56
export { SearchTool } from "./search";

0 commit comments

Comments
 (0)