diff --git a/README.md b/README.md index d317d23..e6c29e9 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,27 @@ Or specify a working directory: grok -d /path/to/project ``` +### Custom Instructions + +You can provide custom instructions to tailor Grok's behavior to your project by creating a `.grok/GROK.md` file in your project directory: + +```bash +mkdir .grok +``` + +Create `.grok/GROK.md` with your custom instructions: +```markdown +# Custom Instructions for Grok CLI + +Always use TypeScript for any new code files. +When creating React components, use functional components with hooks. +Prefer const assertions and explicit typing over inference where it improves clarity. +Always add JSDoc comments for public functions and interfaces. +Follow the existing code style and patterns in this project. +``` + +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. + ## Example Conversations Instead of typing commands, just tell Grok what you want to do: diff --git a/src/agent/grok-agent.ts b/src/agent/grok-agent.ts index d48b9ae..5009066 100644 --- a/src/agent/grok-agent.ts +++ b/src/agent/grok-agent.ts @@ -4,6 +4,7 @@ import { TextEditorTool, BashTool, TodoTool, ConfirmationTool } from '../tools'; import { ToolResult } from '../types'; import { EventEmitter } from 'events'; import { createTokenCounter, TokenCounter } from '../utils/token-counter'; +import { loadCustomInstructions } from '../utils/custom-instructions'; export interface ChatEntry { type: 'user' | 'assistant' | 'tool_result'; @@ -43,10 +44,16 @@ export class GrokAgent extends EventEmitter { this.confirmationTool = new ConfirmationTool(); this.tokenCounter = createTokenCounter('grok-4-latest'); + // Load custom instructions + const customInstructions = loadCustomInstructions(); + const customInstructionsSection = customInstructions + ? `\n\nCUSTOM INSTRUCTIONS:\n${customInstructions}\n\nThe above custom instructions should be followed alongside the standard instructions below.` + : ''; + // Initialize with system message this.messages.push({ role: 'system', - content: `You are Grok CLI, an AI assistant that helps with file editing, coding tasks, and system operations. + content: `You are Grok CLI, an AI assistant that helps with file editing, coding tasks, and system operations.${customInstructionsSection} You have access to these tools: - view_file: View file contents or directory listings diff --git a/src/utils/custom-instructions.ts b/src/utils/custom-instructions.ts new file mode 100644 index 0000000..f95f081 --- /dev/null +++ b/src/utils/custom-instructions.ts @@ -0,0 +1,18 @@ +import * as fs from 'fs'; +import * as path from 'path'; + +export function loadCustomInstructions(workingDirectory: string = process.cwd()): string | null { + try { + const instructionsPath = path.join(workingDirectory, '.grok', 'GROK.md'); + + if (!fs.existsSync(instructionsPath)) { + return null; + } + + const customInstructions = fs.readFileSync(instructionsPath, 'utf-8'); + return customInstructions.trim(); + } catch (error) { + console.warn('Failed to load custom instructions:', error); + return null; + } +} \ No newline at end of file