UNPKG

@vibe-kit/grok-cli

Version:

An open-source AI agent that brings the power of Grok directly into your terminal.

146 lines (143 loc) 6.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConversationalAgent = void 0; const client_1 = require("../grok/client"); const tools_1 = require("../grok/tools"); const tools_2 = require("../tools"); class ConversationalAgent { constructor(apiKey) { this.conversation = []; this.messages = []; this.grokClient = new client_1.GrokClient(apiKey); this.textEditor = new tools_2.TextEditorTool(); this.bash = new tools_2.BashTool(); // 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. You have access to these tools: - view_file: View file contents or directory listings - create_file: Create new files with content - str_replace_editor: Replace text in existing files - bash: Execute bash commands When a user asks you to do something, use the appropriate tools to accomplish the task. Be helpful, direct, and efficient. Always explain what you're doing and show the results. Current working directory: ${process.cwd()}` }); } async processUserMessage(message) { // Add user message to conversation const userEntry = { type: 'user', content: message, timestamp: new Date() }; this.conversation.push(userEntry); this.messages.push({ role: 'user', content: message }); const newEntries = [userEntry]; try { // Get response from Grok const response = await this.grokClient.chat(this.messages, tools_1.textEditorTools); const assistantMessage = response.choices[0]?.message; if (!assistantMessage) { throw new Error('No response from Grok'); } // Handle tool calls if (assistantMessage.tool_calls && assistantMessage.tool_calls.length > 0) { // Add assistant message with tool calls const assistantEntry = { type: 'assistant', content: assistantMessage.content || 'Using tools to help you...', timestamp: new Date(), toolCalls: assistantMessage.tool_calls }; this.conversation.push(assistantEntry); newEntries.push(assistantEntry); // Execute tool calls for (const toolCall of assistantMessage.tool_calls) { const result = await this.executeTool(toolCall); const toolResultEntry = { type: 'tool_result', content: result.success ? result.output || 'Success' : result.error || 'Error occurred', timestamp: new Date() }; this.conversation.push(toolResultEntry); newEntries.push(toolResultEntry); // Add tool result to messages for context this.messages.push({ role: 'assistant', content: `Tool ${toolCall.function.name} executed: ${result.success ? 'Success' : 'Error'}\n${result.success ? result.output : result.error}` }); } // Get final response after tool execution const finalResponse = await this.grokClient.chat(this.messages); const finalMessage = finalResponse.choices[0]?.message; if (finalMessage?.content) { const finalEntry = { type: 'assistant', content: finalMessage.content, timestamp: new Date() }; this.conversation.push(finalEntry); this.messages.push({ role: 'assistant', content: finalMessage.content }); newEntries.push(finalEntry); } } else { // Regular response without tool calls const assistantEntry = { type: 'assistant', content: assistantMessage.content || 'I understand, but I don\'t have a specific response.', timestamp: new Date() }; this.conversation.push(assistantEntry); this.messages.push({ role: 'assistant', content: assistantMessage.content || '' }); newEntries.push(assistantEntry); } return newEntries; } catch (error) { const errorEntry = { type: 'assistant', content: `Sorry, I encountered an error: ${error.message}`, timestamp: new Date() }; this.conversation.push(errorEntry); return [userEntry, errorEntry]; } } async executeTool(toolCall) { try { const args = JSON.parse(toolCall.function.arguments); switch (toolCall.function.name) { case 'view_file': const range = args.start_line && args.end_line ? [args.start_line, args.end_line] : undefined; return await this.textEditor.view(args.path, range); case 'create_file': return await this.textEditor.create(args.path, args.content); case 'str_replace_editor': return await this.textEditor.strReplace(args.path, args.old_str, args.new_str); case 'bash': return await this.bash.execute(args.command); default: return { success: false, error: `Unknown tool: ${toolCall.function.name}` }; } } catch (error) { return { success: false, error: `Tool execution error: ${error.message}` }; } } getConversation() { return [...this.conversation]; } getCurrentDirectory() { return this.bash.getCurrentDirectory(); } } exports.ConversationalAgent = ConversationalAgent; //# sourceMappingURL=conversationalAgent.js.map