# @ai-assistant/core Core engine package for AI Terminal Assistant - provides the AI agent, tools, and integrations. ## ๐Ÿ“ฆ Installation ```bash pnpm add @ai-assistant/core ``` ## ๐ŸŒŸ Features - **AI Agent Engine** - Streaming text generation with Claude API - **Tool System** - Extensible tool registry with Zod validation - **Editor Modes** - Multiple editing strategies (whole, diff, search-replace) - **Language Server Protocol** - Code intelligence and analysis - **Checkpoint System** - Shadow Git for safe experimentation - **Hook System** - Pre/post execution customization - **Model Context Protocol** - MCP client for external tools - **Repository Mapping** - Tree-sitter based code analysis ## ๐Ÿ—๏ธ Architecture ``` src/ โ”œโ”€โ”€ core/ โ”‚ โ”œโ”€โ”€ agent.ts # Main Agent class with streaming โ”‚ โ”œโ”€โ”€ config.ts # Configuration management โ”‚ โ””โ”€โ”€ types.ts # Core type definitions โ”œโ”€โ”€ tools/ โ”‚ โ”œโ”€โ”€ registry.ts # Tool registration system โ”‚ โ”œโ”€โ”€ bash.ts # Bash command execution โ”‚ โ”œโ”€โ”€ file.ts # File operations โ”‚ โ”œโ”€โ”€ search.ts # Code search utilities โ”‚ โ””โ”€โ”€ index.ts # Tool exports โ”œโ”€โ”€ editors/ โ”‚ โ”œโ”€โ”€ base.ts # Base editor interface โ”‚ โ”œโ”€โ”€ whole.ts # Whole file replacement โ”‚ โ”œโ”€โ”€ diff.ts # Diff-based editing โ”‚ โ””โ”€โ”€ search-replace.ts # Search and replace โ”œโ”€โ”€ lsp/ โ”‚ โ”œโ”€โ”€ client.ts # LSP client implementation โ”‚ โ””โ”€โ”€ server.ts # LSP server management โ”œโ”€โ”€ checkpoint/ โ”‚ โ”œโ”€โ”€ manager.ts # Checkpoint management โ”‚ โ””โ”€โ”€ git.ts # Shadow Git operations โ”œโ”€โ”€ hooks/ โ”‚ โ”œโ”€โ”€ manager.ts # Hook execution system โ”‚ โ””โ”€โ”€ types.ts # Hook type definitions โ”œโ”€โ”€ mcp/ โ”‚ โ”œโ”€โ”€ client.ts # MCP client implementation โ”‚ โ””โ”€โ”€ protocol.ts # Protocol definitions โ””โ”€โ”€ repomap/ โ”œโ”€โ”€ generator.ts # Repository map generation โ””โ”€โ”€ parser.ts # Tree-sitter parsing ``` ## ๐Ÿš€ Quick Start ### Basic Usage ```typescript import { Agent, toolRegistry } from '@ai-assistant/core'; // Create an agent instance const agent = new Agent({ apiKey: process.env.ANTHROPIC_API_KEY, model: 'claude-sonnet-4-20250514', maxTokens: 4096 }); // Chat with the agent const response = await agent.chat('Help me write a TypeScript function', { onChunk: (chunk) => console.log(chunk), onComplete: (response) => console.log('Done:', response) }); ``` ### Tool Registration ```typescript import { toolRegistry } from '@ai-assistant/core/tools'; import { z } from 'zod'; // Register a custom tool toolRegistry.register({ name: 'my_tool', description: 'Does something useful', parameters: z.object({ input: z.string().describe('Input parameter') }), execute: async (params) => { // Tool implementation return { success: true, result: params.input.toUpperCase() }; } }); ``` ### Editor Modes ```typescript import { createEditor } from '@ai-assistant/core/editors'; // Whole file replacement const wholeEditor = createEditor('whole'); await wholeEditor.edit('file.ts', 'new content'); // Diff-based editing const diffEditor = createEditor('diff'); await diffEditor.edit('file.ts', '--- old\n+++ new\n...'); // Search and replace const searchEditor = createEditor('search-replace'); await searchEditor.edit('file.ts', { search: 'oldFunction', replace: 'newFunction' }); ``` ### Checkpoint System ```typescript import { CheckpointManager } from '@ai-assistant/core/checkpoint'; const manager = new CheckpointManager('/project/path'); // Create a checkpoint await manager.create('before-refactoring'); // Make changes... // Restore if needed await manager.restore('before-refactoring'); // List checkpoints const checkpoints = await manager.list(); ``` ### Language Server Protocol ```typescript import { LSPClient } from '@ai-assistant/core/lsp'; const lsp = new LSPClient({ serverCommand: 'typescript-language-server', serverArgs: ['--stdio'] }); await lsp.initialize('/project/path'); // Get diagnostics const diagnostics = await lsp.getDiagnostics('file.ts'); // Get completions const completions = await lsp.getCompletions('file.ts', { line: 10, character: 5 }); // Go to definition const definition = await lsp.getDefinition('file.ts', { line: 10, character: 5 }); ``` ## ๐Ÿ”ง Configuration ```typescript interface AgentConfig { apiKey: string; // Anthropic API key model?: string; // Model name (default: claude-sonnet-4-20250514) maxTokens?: number; // Max response tokens (default: 4096) temperature?: number; // Sampling temperature (default: 0.7) systemPrompt?: string; // System prompt tools?: Tool[]; // Available tools hooks?: HookConfig; // Hook configuration checkpointDir?: string; // Checkpoint directory } ``` ## ๐Ÿ“š API Reference ### Agent ```typescript class Agent { constructor(config: AgentConfig); chat(message: string, options?: ChatOptions): Promise; streamChat(message: string, options?: StreamOptions): AsyncIterator; reset(): void; getHistory(): Message[]; } ``` ### Tool Registry ```typescript class ToolRegistry { register(tool: ToolDefinition): void; unregister(name: string): void; get(name: string): Tool | undefined; list(): Tool[]; execute(name: string, params: any): Promise; } ``` ### Editor Interface ```typescript interface Editor { edit(path: string, content: string | EditParams): Promise; validate(content: string): boolean; preview(path: string, content: string): string; } ``` ## ๐Ÿงช Testing ```bash # Run tests pnpm test # Run with coverage pnpm test:coverage # Watch mode pnpm test:watch ``` ## ๐Ÿค Contributing Contributions are welcome! Please follow these steps: 1. Fork the repository 2. Create a feature branch 3. Make your changes 4. Add tests for new functionality 5. Ensure all tests pass 6. Submit a pull request ## ๐Ÿ“„ License MIT License - see [LICENSE](../../LICENSE) for details. ## ๐Ÿ”— Links - [Main Repository](https://github.com/username/ai-terminal-assistant) - [API Documentation](https://docs.example.com/core) - [Issue Tracker](https://github.com/username/ai-terminal-assistant/issues)