grok-cli-hurry-mode 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.grok/settings.json +3 -0
- package/LICENSE +21 -0
- package/README.md +452 -0
- package/dist/agent/grok-agent.d.ts +54 -0
- package/dist/agent/grok-agent.js +674 -0
- package/dist/agent/grok-agent.js.map +1 -0
- package/dist/agent/index.d.ts +14 -0
- package/dist/agent/index.js +137 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/commands/mcp.d.ts +2 -0
- package/dist/commands/mcp.js +245 -0
- package/dist/commands/mcp.js.map +1 -0
- package/dist/grok/client.d.ts +49 -0
- package/dist/grok/client.js +85 -0
- package/dist/grok/client.js.map +1 -0
- package/dist/grok/tools.d.ts +8 -0
- package/dist/grok/tools.js +357 -0
- package/dist/grok/tools.js.map +1 -0
- package/dist/hooks/use-enhanced-input.d.ts +37 -0
- package/dist/hooks/use-enhanced-input.js +217 -0
- package/dist/hooks/use-enhanced-input.js.map +1 -0
- package/dist/hooks/use-input-handler.d.ts +34 -0
- package/dist/hooks/use-input-handler.js +611 -0
- package/dist/hooks/use-input-handler.js.map +1 -0
- package/dist/hooks/use-input-history.d.ts +9 -0
- package/dist/hooks/use-input-history.js +64 -0
- package/dist/hooks/use-input-history.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +151949 -0
- package/dist/index.js.map +1 -0
- package/dist/mcp/client.d.ts +29 -0
- package/dist/mcp/client.js +167 -0
- package/dist/mcp/client.js.map +1 -0
- package/dist/mcp/config.d.ts +13 -0
- package/dist/mcp/config.js +51 -0
- package/dist/mcp/config.js.map +1 -0
- package/dist/mcp/transports.d.ts +51 -0
- package/dist/mcp/transports.js +229 -0
- package/dist/mcp/transports.js.map +1 -0
- package/dist/node_modules/react/index.js +1841 -0
- package/dist/tools/bash.d.ts +10 -0
- package/dist/tools/bash.js +81 -0
- package/dist/tools/bash.js.map +1 -0
- package/dist/tools/confirmation-tool.d.ts +16 -0
- package/dist/tools/confirmation-tool.js +75 -0
- package/dist/tools/confirmation-tool.js.map +1 -0
- package/dist/tools/index.d.ts +6 -0
- package/dist/tools/index.js +16 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/morph-editor.d.ts +36 -0
- package/dist/tools/morph-editor.js +347 -0
- package/dist/tools/morph-editor.js.map +1 -0
- package/dist/tools/search.d.ts +69 -0
- package/dist/tools/search.js +341 -0
- package/dist/tools/search.js.map +1 -0
- package/dist/tools/text-editor.d.ts +16 -0
- package/dist/tools/text-editor.js +565 -0
- package/dist/tools/text-editor.js.map +1 -0
- package/dist/tools/todo-tool.d.ts +20 -0
- package/dist/tools/todo-tool.js +135 -0
- package/dist/tools/todo-tool.js.map +1 -0
- package/dist/types/index.d.ts +30 -0
- package/dist/types/index.js +3 -0
- package/dist/types/index.js.map +1 -0
- package/dist/ui/app.d.ts +7 -0
- package/dist/ui/app.js +160 -0
- package/dist/ui/app.js.map +1 -0
- package/dist/ui/components/api-key-input.d.ts +7 -0
- package/dist/ui/components/api-key-input.js +124 -0
- package/dist/ui/components/api-key-input.js.map +1 -0
- package/dist/ui/components/chat-history.d.ts +8 -0
- package/dist/ui/components/chat-history.js +177 -0
- package/dist/ui/components/chat-history.js.map +1 -0
- package/dist/ui/components/chat-input.d.ts +9 -0
- package/dist/ui/components/chat-input.js +87 -0
- package/dist/ui/components/chat-input.js.map +1 -0
- package/dist/ui/components/chat-interface.d.ts +8 -0
- package/dist/ui/components/chat-interface.js +344 -0
- package/dist/ui/components/chat-interface.js.map +1 -0
- package/dist/ui/components/command-suggestions.d.ts +17 -0
- package/dist/ui/components/command-suggestions.js +68 -0
- package/dist/ui/components/command-suggestions.js.map +1 -0
- package/dist/ui/components/confirmation-dialog.d.ts +11 -0
- package/dist/ui/components/confirmation-dialog.js +167 -0
- package/dist/ui/components/confirmation-dialog.js.map +1 -0
- package/dist/ui/components/diff-renderer.d.ts +13 -0
- package/dist/ui/components/diff-renderer.js +217 -0
- package/dist/ui/components/diff-renderer.js.map +1 -0
- package/dist/ui/components/loading-spinner.d.ts +8 -0
- package/dist/ui/components/loading-spinner.js +92 -0
- package/dist/ui/components/loading-spinner.js.map +1 -0
- package/dist/ui/components/mcp-status.d.ts +5 -0
- package/dist/ui/components/mcp-status.js +74 -0
- package/dist/ui/components/mcp-status.js.map +1 -0
- package/dist/ui/components/model-selection.d.ts +12 -0
- package/dist/ui/components/model-selection.js +28 -0
- package/dist/ui/components/model-selection.js.map +1 -0
- package/dist/ui/shared/max-sized-box.d.ts +8 -0
- package/dist/ui/shared/max-sized-box.js +15 -0
- package/dist/ui/shared/max-sized-box.js.map +1 -0
- package/dist/ui/utils/code-colorizer.d.ts +2 -0
- package/dist/ui/utils/code-colorizer.js +18 -0
- package/dist/ui/utils/code-colorizer.js.map +1 -0
- package/dist/ui/utils/colors.d.ts +14 -0
- package/dist/ui/utils/colors.js +18 -0
- package/dist/ui/utils/colors.js.map +1 -0
- package/dist/ui/utils/markdown-renderer.d.ts +4 -0
- package/dist/ui/utils/markdown-renderer.js +29 -0
- package/dist/ui/utils/markdown-renderer.js.map +1 -0
- package/dist/utils/confirmation-service.d.ts +33 -0
- package/dist/utils/confirmation-service.js +113 -0
- package/dist/utils/confirmation-service.js.map +1 -0
- package/dist/utils/custom-instructions.d.ts +1 -0
- package/dist/utils/custom-instructions.js +53 -0
- package/dist/utils/custom-instructions.js.map +1 -0
- package/dist/utils/model-config.d.ts +28 -0
- package/dist/utils/model-config.js +49 -0
- package/dist/utils/model-config.js.map +1 -0
- package/dist/utils/settings-manager.d.ts +94 -0
- package/dist/utils/settings-manager.js +275 -0
- package/dist/utils/settings-manager.js.map +1 -0
- package/dist/utils/settings.d.ts +1 -0
- package/dist/utils/settings.js +8 -0
- package/dist/utils/settings.js.map +1 -0
- package/dist/utils/text-utils.d.ts +80 -0
- package/dist/utils/text-utils.js +197 -0
- package/dist/utils/text-utils.js.map +1 -0
- package/dist/utils/token-counter.d.ts +33 -0
- package/dist/utils/token-counter.js +83 -0
- package/dist/utils/token-counter.js.map +1 -0
- package/dist/yoga.wasm +0 -0
- package/eslint.config.mjs +28 -0
- package/fix.awk +41 -0
- package/grok-logo-screenshot.png +0 -0
- package/package.json +68 -0
- package/sed_cmd.sh +35 -0
- package/temp.txt +29 -0
@@ -0,0 +1,674 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.GrokAgent = void 0;
|
4
|
+
const client_js_1 = require("../grok/client.js");
|
5
|
+
const tools_js_1 = require("../grok/tools.js");
|
6
|
+
const config_js_1 = require("../mcp/config.js");
|
7
|
+
const index_js_1 = require("../tools/index.js");
|
8
|
+
const events_1 = require("events");
|
9
|
+
const token_counter_js_1 = require("../utils/token-counter.js");
|
10
|
+
const custom_instructions_js_1 = require("../utils/custom-instructions.js");
|
11
|
+
const settings_manager_js_1 = require("../utils/settings-manager.js");
|
12
|
+
class GrokAgent extends events_1.EventEmitter {
|
13
|
+
constructor(apiKey, baseURL, model, maxToolRounds) {
|
14
|
+
super();
|
15
|
+
this.chatHistory = [];
|
16
|
+
this.messages = [];
|
17
|
+
this.abortController = null;
|
18
|
+
this.mcpInitialized = false;
|
19
|
+
const manager = (0, settings_manager_js_1.getSettingsManager)();
|
20
|
+
const savedModel = manager.getCurrentModel();
|
21
|
+
const modelToUse = model || savedModel || "grok-code-fast-1";
|
22
|
+
this.maxToolRounds = maxToolRounds || 400;
|
23
|
+
this.grokClient = new client_js_1.GrokClient(apiKey, modelToUse, baseURL);
|
24
|
+
this.textEditor = new index_js_1.TextEditorTool();
|
25
|
+
this.morphEditor = process.env.MORPH_API_KEY ? new index_js_1.MorphEditorTool() : null;
|
26
|
+
this.bash = new index_js_1.BashTool();
|
27
|
+
this.todoTool = new index_js_1.TodoTool();
|
28
|
+
this.confirmationTool = new index_js_1.ConfirmationTool();
|
29
|
+
this.search = new index_js_1.SearchTool();
|
30
|
+
this.tokenCounter = (0, token_counter_js_1.createTokenCounter)(modelToUse);
|
31
|
+
// Initialize MCP servers if configured
|
32
|
+
this.initializeMCP();
|
33
|
+
// Load custom instructions
|
34
|
+
const customInstructions = (0, custom_instructions_js_1.loadCustomInstructions)();
|
35
|
+
const customInstructionsSection = customInstructions
|
36
|
+
? `\n\nCUSTOM INSTRUCTIONS:\n${customInstructions}\n\nThe above custom instructions should be followed alongside the standard instructions below.`
|
37
|
+
: "";
|
38
|
+
// Initialize with system message
|
39
|
+
this.messages.push({
|
40
|
+
role: "system",
|
41
|
+
content: `You are Grok CLI, an AI assistant that helps with file editing, coding tasks, and system operations.${customInstructionsSection}
|
42
|
+
|
43
|
+
You have access to these tools:
|
44
|
+
- view_file: View file contents or directory listings
|
45
|
+
- create_file: Create new files with content (ONLY use this for files that don't exist yet)
|
46
|
+
- str_replace_editor: Replace text in existing files (ALWAYS use this to edit or update existing files)${this.morphEditor
|
47
|
+
? "\n- edit_file: High-speed file editing with Morph Fast Apply (4,500+ tokens/sec with 98% accuracy)"
|
48
|
+
: ""}
|
49
|
+
- bash: Execute bash commands (use for searching, file discovery, navigation, and system operations)
|
50
|
+
- search: Unified search tool for finding text content or files (similar to Cursor's search functionality)
|
51
|
+
- create_todo_list: Create a visual todo list for planning and tracking tasks
|
52
|
+
- update_todo_list: Update existing todos in your todo list
|
53
|
+
|
54
|
+
REAL-TIME INFORMATION:
|
55
|
+
You have access to real-time web search and X (Twitter) data. When users ask for current information, latest news, or recent events, you automatically have access to up-to-date information from the web and social media.
|
56
|
+
|
57
|
+
IMPORTANT TOOL USAGE RULES:
|
58
|
+
- NEVER use create_file on files that already exist - this will overwrite them completely
|
59
|
+
- ALWAYS use str_replace_editor to modify existing files, even for small changes
|
60
|
+
- Before editing a file, use view_file to see its current contents
|
61
|
+
- Use create_file ONLY when creating entirely new files that don't exist
|
62
|
+
|
63
|
+
SEARCHING AND EXPLORATION:
|
64
|
+
- Use search for fast, powerful text search across files or finding files by name (unified search tool)
|
65
|
+
- Examples: search for text content like "import.*react", search for files like "component.tsx"
|
66
|
+
- Use bash with commands like 'find', 'grep', 'rg', 'ls' for complex file operations and navigation
|
67
|
+
- view_file is best for reading specific files you already know exist
|
68
|
+
|
69
|
+
When a user asks you to edit, update, modify, or change an existing file:
|
70
|
+
1. First use view_file to see the current contents
|
71
|
+
2. Then use str_replace_editor to make the specific changes
|
72
|
+
3. Never use create_file for existing files
|
73
|
+
|
74
|
+
When a user asks you to create a new file that doesn't exist:
|
75
|
+
1. Use create_file with the full content
|
76
|
+
|
77
|
+
TASK PLANNING WITH TODO LISTS:
|
78
|
+
- For complex requests with multiple steps, ALWAYS create a todo list first to plan your approach
|
79
|
+
- Use create_todo_list to break down tasks into manageable items with priorities
|
80
|
+
- Mark tasks as 'in_progress' when you start working on them (only one at a time)
|
81
|
+
- Mark tasks as 'completed' immediately when finished
|
82
|
+
- Use update_todo_list to track your progress throughout the task
|
83
|
+
- Todo lists provide visual feedback with colors: ✅ Green (completed), 🔄 Cyan (in progress), ⏳ Yellow (pending)
|
84
|
+
- Always create todos with priorities: 'high' (🔴), 'medium' (🟡), 'low' (🟢)
|
85
|
+
|
86
|
+
USER CONFIRMATION SYSTEM:
|
87
|
+
File operations (create_file, str_replace_editor) and bash commands will automatically request user confirmation before execution. The confirmation system will show users the actual content or command before they decide. Users can choose to approve individual operations or approve all operations of that type for the session.
|
88
|
+
|
89
|
+
If a user rejects an operation, the tool will return an error and you should not proceed with that specific operation.
|
90
|
+
|
91
|
+
Be helpful, direct, and efficient. Always explain what you're doing and show the results.
|
92
|
+
|
93
|
+
IMPORTANT RESPONSE GUIDELINES:
|
94
|
+
- After using tools, do NOT respond with pleasantries like "Thanks for..." or "Great!"
|
95
|
+
- Only provide necessary explanations or next steps if relevant to the task
|
96
|
+
- Keep responses concise and focused on the actual work being done
|
97
|
+
- If a tool execution completes the user's request, you can remain silent or give a brief confirmation
|
98
|
+
|
99
|
+
Current working directory: ${process.cwd()}`,
|
100
|
+
});
|
101
|
+
}
|
102
|
+
async initializeMCP() {
|
103
|
+
// Initialize MCP in the background without blocking
|
104
|
+
Promise.resolve().then(async () => {
|
105
|
+
try {
|
106
|
+
const config = (0, config_js_1.loadMCPConfig)();
|
107
|
+
if (config.servers.length > 0) {
|
108
|
+
await (0, tools_js_1.initializeMCPServers)();
|
109
|
+
}
|
110
|
+
}
|
111
|
+
catch (error) {
|
112
|
+
console.warn("MCP initialization failed:", error);
|
113
|
+
}
|
114
|
+
finally {
|
115
|
+
this.mcpInitialized = true;
|
116
|
+
}
|
117
|
+
});
|
118
|
+
}
|
119
|
+
isGrokModel() {
|
120
|
+
const currentModel = this.grokClient.getCurrentModel();
|
121
|
+
return currentModel.toLowerCase().includes("grok");
|
122
|
+
}
|
123
|
+
// Heuristic: enable web search only when likely needed
|
124
|
+
shouldUseSearchFor(message) {
|
125
|
+
const q = message.toLowerCase();
|
126
|
+
const keywords = [
|
127
|
+
"today",
|
128
|
+
"latest",
|
129
|
+
"news",
|
130
|
+
"trending",
|
131
|
+
"breaking",
|
132
|
+
"current",
|
133
|
+
"now",
|
134
|
+
"recent",
|
135
|
+
"x.com",
|
136
|
+
"twitter",
|
137
|
+
"tweet",
|
138
|
+
"what happened",
|
139
|
+
"as of",
|
140
|
+
"update on",
|
141
|
+
"release notes",
|
142
|
+
"changelog",
|
143
|
+
"price",
|
144
|
+
];
|
145
|
+
if (keywords.some((k) => q.includes(k)))
|
146
|
+
return true;
|
147
|
+
// crude date pattern (e.g., 2024/2025) may imply recency
|
148
|
+
if (/(20\d{2})/.test(q))
|
149
|
+
return true;
|
150
|
+
return false;
|
151
|
+
}
|
152
|
+
async processUserMessage(message) {
|
153
|
+
// Add user message to conversation
|
154
|
+
const userEntry = {
|
155
|
+
type: "user",
|
156
|
+
content: message,
|
157
|
+
timestamp: new Date(),
|
158
|
+
};
|
159
|
+
this.chatHistory.push(userEntry);
|
160
|
+
this.messages.push({ role: "user", content: message });
|
161
|
+
const newEntries = [userEntry];
|
162
|
+
const maxToolRounds = this.maxToolRounds; // Prevent infinite loops
|
163
|
+
let toolRounds = 0;
|
164
|
+
try {
|
165
|
+
const tools = await (0, tools_js_1.getAllGrokTools)();
|
166
|
+
let currentResponse = await this.grokClient.chat(this.messages, tools, undefined, this.isGrokModel() && this.shouldUseSearchFor(message)
|
167
|
+
? { search_parameters: { mode: "auto" } }
|
168
|
+
: { search_parameters: { mode: "off" } });
|
169
|
+
// Agent loop - continue until no more tool calls or max rounds reached
|
170
|
+
while (toolRounds < maxToolRounds) {
|
171
|
+
const assistantMessage = currentResponse.choices[0]?.message;
|
172
|
+
if (!assistantMessage) {
|
173
|
+
throw new Error("No response from Grok");
|
174
|
+
}
|
175
|
+
// Handle tool calls
|
176
|
+
if (assistantMessage.tool_calls &&
|
177
|
+
assistantMessage.tool_calls.length > 0) {
|
178
|
+
toolRounds++;
|
179
|
+
// Add assistant message with tool calls
|
180
|
+
const assistantEntry = {
|
181
|
+
type: "assistant",
|
182
|
+
content: assistantMessage.content || "Using tools to help you...",
|
183
|
+
timestamp: new Date(),
|
184
|
+
toolCalls: assistantMessage.tool_calls,
|
185
|
+
};
|
186
|
+
this.chatHistory.push(assistantEntry);
|
187
|
+
newEntries.push(assistantEntry);
|
188
|
+
// Add assistant message to conversation
|
189
|
+
this.messages.push({
|
190
|
+
role: "assistant",
|
191
|
+
content: assistantMessage.content || "",
|
192
|
+
tool_calls: assistantMessage.tool_calls,
|
193
|
+
});
|
194
|
+
// Create initial tool call entries to show tools are being executed
|
195
|
+
assistantMessage.tool_calls.forEach((toolCall) => {
|
196
|
+
const toolCallEntry = {
|
197
|
+
type: "tool_call",
|
198
|
+
content: "Executing...",
|
199
|
+
timestamp: new Date(),
|
200
|
+
toolCall: toolCall,
|
201
|
+
};
|
202
|
+
this.chatHistory.push(toolCallEntry);
|
203
|
+
newEntries.push(toolCallEntry);
|
204
|
+
});
|
205
|
+
// Execute tool calls and update the entries
|
206
|
+
for (const toolCall of assistantMessage.tool_calls) {
|
207
|
+
const result = await this.executeTool(toolCall);
|
208
|
+
// Update the existing tool_call entry with the result
|
209
|
+
const entryIndex = this.chatHistory.findIndex((entry) => entry.type === "tool_call" && entry.toolCall?.id === toolCall.id);
|
210
|
+
if (entryIndex !== -1) {
|
211
|
+
const updatedEntry = {
|
212
|
+
...this.chatHistory[entryIndex],
|
213
|
+
type: "tool_result",
|
214
|
+
content: result.success
|
215
|
+
? result.output || "Success"
|
216
|
+
: result.error || "Error occurred",
|
217
|
+
toolResult: result,
|
218
|
+
};
|
219
|
+
this.chatHistory[entryIndex] = updatedEntry;
|
220
|
+
// Also update in newEntries for return value
|
221
|
+
const newEntryIndex = newEntries.findIndex((entry) => entry.type === "tool_call" &&
|
222
|
+
entry.toolCall?.id === toolCall.id);
|
223
|
+
if (newEntryIndex !== -1) {
|
224
|
+
newEntries[newEntryIndex] = updatedEntry;
|
225
|
+
}
|
226
|
+
}
|
227
|
+
// Add tool result to messages with proper format (needed for AI context)
|
228
|
+
this.messages.push({
|
229
|
+
role: "tool",
|
230
|
+
content: result.success
|
231
|
+
? result.output || "Success"
|
232
|
+
: result.error || "Error",
|
233
|
+
tool_call_id: toolCall.id,
|
234
|
+
});
|
235
|
+
}
|
236
|
+
// Get next response - this might contain more tool calls
|
237
|
+
currentResponse = await this.grokClient.chat(this.messages, tools, undefined, this.isGrokModel() && this.shouldUseSearchFor(message)
|
238
|
+
? { search_parameters: { mode: "auto" } }
|
239
|
+
: { search_parameters: { mode: "off" } });
|
240
|
+
}
|
241
|
+
else {
|
242
|
+
// No more tool calls, add final response
|
243
|
+
const finalEntry = {
|
244
|
+
type: "assistant",
|
245
|
+
content: assistantMessage.content ||
|
246
|
+
"I understand, but I don't have a specific response.",
|
247
|
+
timestamp: new Date(),
|
248
|
+
};
|
249
|
+
this.chatHistory.push(finalEntry);
|
250
|
+
this.messages.push({
|
251
|
+
role: "assistant",
|
252
|
+
content: assistantMessage.content || "",
|
253
|
+
});
|
254
|
+
newEntries.push(finalEntry);
|
255
|
+
break; // Exit the loop
|
256
|
+
}
|
257
|
+
}
|
258
|
+
if (toolRounds >= maxToolRounds) {
|
259
|
+
const warningEntry = {
|
260
|
+
type: "assistant",
|
261
|
+
content: "Maximum tool execution rounds reached. Stopping to prevent infinite loops.",
|
262
|
+
timestamp: new Date(),
|
263
|
+
};
|
264
|
+
this.chatHistory.push(warningEntry);
|
265
|
+
newEntries.push(warningEntry);
|
266
|
+
}
|
267
|
+
return newEntries;
|
268
|
+
}
|
269
|
+
catch (error) {
|
270
|
+
const errorEntry = {
|
271
|
+
type: "assistant",
|
272
|
+
content: `Sorry, I encountered an error: ${error.message}`,
|
273
|
+
timestamp: new Date(),
|
274
|
+
};
|
275
|
+
this.chatHistory.push(errorEntry);
|
276
|
+
return [userEntry, errorEntry];
|
277
|
+
}
|
278
|
+
}
|
279
|
+
messageReducer(previous, item) {
|
280
|
+
const reduce = (acc, delta) => {
|
281
|
+
acc = { ...acc };
|
282
|
+
for (const [key, value] of Object.entries(delta)) {
|
283
|
+
if (acc[key] === undefined || acc[key] === null) {
|
284
|
+
acc[key] = value;
|
285
|
+
// Clean up index properties from tool calls
|
286
|
+
if (Array.isArray(acc[key])) {
|
287
|
+
for (const arr of acc[key]) {
|
288
|
+
delete arr.index;
|
289
|
+
}
|
290
|
+
}
|
291
|
+
}
|
292
|
+
else if (typeof acc[key] === "string" && typeof value === "string") {
|
293
|
+
acc[key] += value;
|
294
|
+
}
|
295
|
+
else if (Array.isArray(acc[key]) && Array.isArray(value)) {
|
296
|
+
const accArray = acc[key];
|
297
|
+
for (let i = 0; i < value.length; i++) {
|
298
|
+
if (!accArray[i])
|
299
|
+
accArray[i] = {};
|
300
|
+
accArray[i] = reduce(accArray[i], value[i]);
|
301
|
+
}
|
302
|
+
}
|
303
|
+
else if (typeof acc[key] === "object" && typeof value === "object") {
|
304
|
+
acc[key] = reduce(acc[key], value);
|
305
|
+
}
|
306
|
+
}
|
307
|
+
return acc;
|
308
|
+
};
|
309
|
+
return reduce(previous, item.choices[0]?.delta || {});
|
310
|
+
}
|
311
|
+
async *processUserMessageStream(message) {
|
312
|
+
// Create new abort controller for this request
|
313
|
+
this.abortController = new AbortController();
|
314
|
+
// Add user message to conversation
|
315
|
+
const userEntry = {
|
316
|
+
type: "user",
|
317
|
+
content: message,
|
318
|
+
timestamp: new Date(),
|
319
|
+
};
|
320
|
+
this.chatHistory.push(userEntry);
|
321
|
+
this.messages.push({ role: "user", content: message });
|
322
|
+
// Calculate input tokens
|
323
|
+
let inputTokens = this.tokenCounter.countMessageTokens(this.messages);
|
324
|
+
yield {
|
325
|
+
type: "token_count",
|
326
|
+
tokenCount: inputTokens,
|
327
|
+
};
|
328
|
+
const maxToolRounds = this.maxToolRounds; // Prevent infinite loops
|
329
|
+
let toolRounds = 0;
|
330
|
+
let totalOutputTokens = 0;
|
331
|
+
let lastTokenUpdate = 0;
|
332
|
+
try {
|
333
|
+
// Agent loop - continue until no more tool calls or max rounds reached
|
334
|
+
while (toolRounds < maxToolRounds) {
|
335
|
+
// Check if operation was cancelled
|
336
|
+
if (this.abortController?.signal.aborted) {
|
337
|
+
yield {
|
338
|
+
type: "content",
|
339
|
+
content: "\n\n[Operation cancelled by user]",
|
340
|
+
};
|
341
|
+
yield { type: "done" };
|
342
|
+
return;
|
343
|
+
}
|
344
|
+
// Stream response and accumulate
|
345
|
+
const tools = await (0, tools_js_1.getAllGrokTools)();
|
346
|
+
const stream = this.grokClient.chatStream(this.messages, tools, undefined, this.isGrokModel() && this.shouldUseSearchFor(message)
|
347
|
+
? { search_parameters: { mode: "auto" } }
|
348
|
+
: { search_parameters: { mode: "off" } });
|
349
|
+
let accumulatedMessage = {};
|
350
|
+
let accumulatedContent = "";
|
351
|
+
let toolCallsYielded = false;
|
352
|
+
for await (const chunk of stream) {
|
353
|
+
// Check for cancellation in the streaming loop
|
354
|
+
if (this.abortController?.signal.aborted) {
|
355
|
+
yield {
|
356
|
+
type: "content",
|
357
|
+
content: "\n\n[Operation cancelled by user]",
|
358
|
+
};
|
359
|
+
yield { type: "done" };
|
360
|
+
return;
|
361
|
+
}
|
362
|
+
if (!chunk.choices?.[0])
|
363
|
+
continue;
|
364
|
+
// Accumulate the message using reducer
|
365
|
+
accumulatedMessage = this.messageReducer(accumulatedMessage, chunk);
|
366
|
+
// Check for tool calls - yield when we have complete tool calls with function names
|
367
|
+
if (!toolCallsYielded && accumulatedMessage.tool_calls?.length > 0) {
|
368
|
+
// Check if we have at least one complete tool call with a function name
|
369
|
+
const hasCompleteTool = accumulatedMessage.tool_calls.some((tc) => tc.function?.name);
|
370
|
+
if (hasCompleteTool) {
|
371
|
+
yield {
|
372
|
+
type: "tool_calls",
|
373
|
+
toolCalls: accumulatedMessage.tool_calls,
|
374
|
+
};
|
375
|
+
toolCallsYielded = true;
|
376
|
+
}
|
377
|
+
}
|
378
|
+
// Stream content as it comes
|
379
|
+
if (chunk.choices[0].delta?.content) {
|
380
|
+
accumulatedContent += chunk.choices[0].delta.content;
|
381
|
+
// Update token count in real-time including accumulated content and any tool calls
|
382
|
+
const currentOutputTokens = this.tokenCounter.estimateStreamingTokens(accumulatedContent) +
|
383
|
+
(accumulatedMessage.tool_calls
|
384
|
+
? this.tokenCounter.countTokens(JSON.stringify(accumulatedMessage.tool_calls))
|
385
|
+
: 0);
|
386
|
+
totalOutputTokens = currentOutputTokens;
|
387
|
+
yield {
|
388
|
+
type: "content",
|
389
|
+
content: chunk.choices[0].delta.content,
|
390
|
+
};
|
391
|
+
// Emit token count update
|
392
|
+
const now = Date.now();
|
393
|
+
if (now - lastTokenUpdate > 250) {
|
394
|
+
lastTokenUpdate = now;
|
395
|
+
yield {
|
396
|
+
type: "token_count",
|
397
|
+
tokenCount: inputTokens + totalOutputTokens,
|
398
|
+
};
|
399
|
+
}
|
400
|
+
}
|
401
|
+
}
|
402
|
+
// Add assistant entry to history
|
403
|
+
const assistantEntry = {
|
404
|
+
type: "assistant",
|
405
|
+
content: accumulatedMessage.content || "Using tools to help you...",
|
406
|
+
timestamp: new Date(),
|
407
|
+
toolCalls: accumulatedMessage.tool_calls || undefined,
|
408
|
+
};
|
409
|
+
this.chatHistory.push(assistantEntry);
|
410
|
+
// Add accumulated message to conversation
|
411
|
+
this.messages.push({
|
412
|
+
role: "assistant",
|
413
|
+
content: accumulatedMessage.content || "",
|
414
|
+
tool_calls: accumulatedMessage.tool_calls,
|
415
|
+
});
|
416
|
+
// Handle tool calls if present
|
417
|
+
if (accumulatedMessage.tool_calls?.length > 0) {
|
418
|
+
toolRounds++;
|
419
|
+
// Only yield tool_calls if we haven't already yielded them during streaming
|
420
|
+
if (!toolCallsYielded) {
|
421
|
+
yield {
|
422
|
+
type: "tool_calls",
|
423
|
+
toolCalls: accumulatedMessage.tool_calls,
|
424
|
+
};
|
425
|
+
}
|
426
|
+
// Execute tools
|
427
|
+
for (const toolCall of accumulatedMessage.tool_calls) {
|
428
|
+
// Check for cancellation before executing each tool
|
429
|
+
if (this.abortController?.signal.aborted) {
|
430
|
+
yield {
|
431
|
+
type: "content",
|
432
|
+
content: "\n\n[Operation cancelled by user]",
|
433
|
+
};
|
434
|
+
yield { type: "done" };
|
435
|
+
return;
|
436
|
+
}
|
437
|
+
const result = await this.executeTool(toolCall);
|
438
|
+
const toolResultEntry = {
|
439
|
+
type: "tool_result",
|
440
|
+
content: result.success
|
441
|
+
? result.output || "Success"
|
442
|
+
: result.error || "Error occurred",
|
443
|
+
timestamp: new Date(),
|
444
|
+
toolCall: toolCall,
|
445
|
+
toolResult: result,
|
446
|
+
};
|
447
|
+
this.chatHistory.push(toolResultEntry);
|
448
|
+
yield {
|
449
|
+
type: "tool_result",
|
450
|
+
toolCall,
|
451
|
+
toolResult: result,
|
452
|
+
};
|
453
|
+
// Add tool result with proper format (needed for AI context)
|
454
|
+
this.messages.push({
|
455
|
+
role: "tool",
|
456
|
+
content: result.success
|
457
|
+
? result.output || "Success"
|
458
|
+
: result.error || "Error",
|
459
|
+
tool_call_id: toolCall.id,
|
460
|
+
});
|
461
|
+
}
|
462
|
+
// Update token count after processing all tool calls to include tool results
|
463
|
+
inputTokens = this.tokenCounter.countMessageTokens(this.messages);
|
464
|
+
// Final token update after tools processed
|
465
|
+
yield {
|
466
|
+
type: "token_count",
|
467
|
+
tokenCount: inputTokens + totalOutputTokens,
|
468
|
+
};
|
469
|
+
// Continue the loop to get the next response (which might have more tool calls)
|
470
|
+
}
|
471
|
+
else {
|
472
|
+
// No tool calls, we're done
|
473
|
+
break;
|
474
|
+
}
|
475
|
+
}
|
476
|
+
if (toolRounds >= maxToolRounds) {
|
477
|
+
yield {
|
478
|
+
type: "content",
|
479
|
+
content: "\n\nMaximum tool execution rounds reached. Stopping to prevent infinite loops.",
|
480
|
+
};
|
481
|
+
}
|
482
|
+
yield { type: "done" };
|
483
|
+
}
|
484
|
+
catch (error) {
|
485
|
+
// Check if this was a cancellation
|
486
|
+
if (this.abortController?.signal.aborted) {
|
487
|
+
yield {
|
488
|
+
type: "content",
|
489
|
+
content: "\n\n[Operation cancelled by user]",
|
490
|
+
};
|
491
|
+
yield { type: "done" };
|
492
|
+
return;
|
493
|
+
}
|
494
|
+
const errorEntry = {
|
495
|
+
type: "assistant",
|
496
|
+
content: `Sorry, I encountered an error: ${error.message}`,
|
497
|
+
timestamp: new Date(),
|
498
|
+
};
|
499
|
+
this.chatHistory.push(errorEntry);
|
500
|
+
yield {
|
501
|
+
type: "content",
|
502
|
+
content: errorEntry.content,
|
503
|
+
};
|
504
|
+
yield { type: "done" };
|
505
|
+
}
|
506
|
+
finally {
|
507
|
+
// Clean up abort controller
|
508
|
+
this.abortController = null;
|
509
|
+
}
|
510
|
+
}
|
511
|
+
async executeTool(toolCall) {
|
512
|
+
try {
|
513
|
+
const args = JSON.parse(toolCall.function.arguments);
|
514
|
+
switch (toolCall.function.name) {
|
515
|
+
case "view_file":
|
516
|
+
try {
|
517
|
+
const range = args.start_line && args.end_line
|
518
|
+
? [args.start_line, args.end_line]
|
519
|
+
: undefined;
|
520
|
+
return await this.textEditor.view(args.path, range);
|
521
|
+
}
|
522
|
+
catch (error) {
|
523
|
+
console.warn(`view_file tool failed, falling back to bash: ${error.message}`);
|
524
|
+
// Fallback to bash cat/head/tail
|
525
|
+
const path = args.path;
|
526
|
+
let command = `cat "${path}"`;
|
527
|
+
if (args.start_line && args.end_line) {
|
528
|
+
command = `sed -n '${args.start_line},${args.end_line}p' "${path}"`;
|
529
|
+
}
|
530
|
+
return await this.bash.execute(command);
|
531
|
+
}
|
532
|
+
case "create_file":
|
533
|
+
try {
|
534
|
+
return await this.textEditor.create(args.path, args.content);
|
535
|
+
}
|
536
|
+
catch (error) {
|
537
|
+
console.warn(`create_file tool failed, falling back to bash: ${error.message}`);
|
538
|
+
// Fallback to bash echo/redirect
|
539
|
+
const command = `cat > "${args.path}" << 'EOF'\n${args.content}\nEOF`;
|
540
|
+
return await this.bash.execute(command);
|
541
|
+
}
|
542
|
+
case "str_replace_editor":
|
543
|
+
try {
|
544
|
+
return await this.textEditor.strReplace(args.path, args.old_str, args.new_str, args.replace_all);
|
545
|
+
}
|
546
|
+
catch (error) {
|
547
|
+
console.warn(`str_replace_editor tool failed, falling back to bash: ${error.message}`);
|
548
|
+
// Fallback to bash sed for replacement
|
549
|
+
const escapedOld = args.old_str.replace(/[\/&]/g, '\\$&');
|
550
|
+
const escapedNew = args.new_str.replace(/[\/&]/g, '\\$&');
|
551
|
+
const sedCommand = args.replace_all
|
552
|
+
? `sed -i 's/${escapedOld}/${escapedNew}/g' "${args.path}"`
|
553
|
+
: `sed -i '0,/${escapedOld}/s/${escapedOld}/${escapedNew}/' "${args.path}"`;
|
554
|
+
return await this.bash.execute(sedCommand);
|
555
|
+
}
|
556
|
+
case "edit_file":
|
557
|
+
if (!this.morphEditor) {
|
558
|
+
return {
|
559
|
+
success: false,
|
560
|
+
error: "Morph Fast Apply not available. Please set MORPH_API_KEY environment variable to use this feature.",
|
561
|
+
};
|
562
|
+
}
|
563
|
+
return await this.morphEditor.editFile(args.target_file, args.instructions, args.code_edit);
|
564
|
+
case "bash":
|
565
|
+
return await this.bash.execute(args.command);
|
566
|
+
case "create_todo_list":
|
567
|
+
return await this.todoTool.createTodoList(args.todos);
|
568
|
+
case "update_todo_list":
|
569
|
+
return await this.todoTool.updateTodoList(args.updates);
|
570
|
+
case "search":
|
571
|
+
try {
|
572
|
+
return await this.search.search(args.query, {
|
573
|
+
searchType: args.search_type,
|
574
|
+
includePattern: args.include_pattern,
|
575
|
+
excludePattern: args.exclude_pattern,
|
576
|
+
caseSensitive: args.case_sensitive,
|
577
|
+
wholeWord: args.whole_word,
|
578
|
+
regex: args.regex,
|
579
|
+
maxResults: args.max_results,
|
580
|
+
fileTypes: args.file_types,
|
581
|
+
includeHidden: args.include_hidden,
|
582
|
+
});
|
583
|
+
}
|
584
|
+
catch (error) {
|
585
|
+
console.warn(`search tool failed, falling back to bash: ${error.message}`);
|
586
|
+
// Fallback to bash grep/find
|
587
|
+
let command = `grep -r "${args.query}" .`;
|
588
|
+
if (args.include_pattern) {
|
589
|
+
command += ` --include="${args.include_pattern}"`;
|
590
|
+
}
|
591
|
+
if (args.exclude_pattern) {
|
592
|
+
command += ` --exclude="${args.exclude_pattern}"`;
|
593
|
+
}
|
594
|
+
return await this.bash.execute(command);
|
595
|
+
}
|
596
|
+
default:
|
597
|
+
// Check if this is an MCP tool
|
598
|
+
if (toolCall.function.name.startsWith("mcp__")) {
|
599
|
+
return await this.executeMCPTool(toolCall);
|
600
|
+
}
|
601
|
+
return {
|
602
|
+
success: false,
|
603
|
+
error: `Unknown tool: ${toolCall.function.name}`,
|
604
|
+
};
|
605
|
+
}
|
606
|
+
}
|
607
|
+
catch (error) {
|
608
|
+
return {
|
609
|
+
success: false,
|
610
|
+
error: `Tool execution error: ${error.message}`,
|
611
|
+
};
|
612
|
+
}
|
613
|
+
}
|
614
|
+
async executeMCPTool(toolCall) {
|
615
|
+
try {
|
616
|
+
const args = JSON.parse(toolCall.function.arguments);
|
617
|
+
const mcpManager = (0, tools_js_1.getMCPManager)();
|
618
|
+
const result = await mcpManager.callTool(toolCall.function.name, args);
|
619
|
+
if (result.isError) {
|
620
|
+
return {
|
621
|
+
success: false,
|
622
|
+
error: result.content[0]?.text || "MCP tool error",
|
623
|
+
};
|
624
|
+
}
|
625
|
+
// Extract content from result
|
626
|
+
const output = result.content
|
627
|
+
.map((item) => {
|
628
|
+
if (item.type === "text") {
|
629
|
+
return item.text;
|
630
|
+
}
|
631
|
+
else if (item.type === "resource") {
|
632
|
+
return `Resource: ${item.resource?.uri || "Unknown"}`;
|
633
|
+
}
|
634
|
+
return String(item);
|
635
|
+
})
|
636
|
+
.join("\n");
|
637
|
+
return {
|
638
|
+
success: true,
|
639
|
+
output: output || "Success",
|
640
|
+
};
|
641
|
+
}
|
642
|
+
catch (error) {
|
643
|
+
return {
|
644
|
+
success: false,
|
645
|
+
error: `MCP tool execution error: ${error.message}`,
|
646
|
+
};
|
647
|
+
}
|
648
|
+
}
|
649
|
+
getChatHistory() {
|
650
|
+
return [...this.chatHistory];
|
651
|
+
}
|
652
|
+
getCurrentDirectory() {
|
653
|
+
return this.bash.getCurrentDirectory();
|
654
|
+
}
|
655
|
+
async executeBashCommand(command) {
|
656
|
+
return await this.bash.execute(command);
|
657
|
+
}
|
658
|
+
getCurrentModel() {
|
659
|
+
return this.grokClient.getCurrentModel();
|
660
|
+
}
|
661
|
+
setModel(model) {
|
662
|
+
this.grokClient.setModel(model);
|
663
|
+
// Update token counter for new model
|
664
|
+
this.tokenCounter.dispose();
|
665
|
+
this.tokenCounter = (0, token_counter_js_1.createTokenCounter)(model);
|
666
|
+
}
|
667
|
+
abortCurrentOperation() {
|
668
|
+
if (this.abortController) {
|
669
|
+
this.abortController.abort();
|
670
|
+
}
|
671
|
+
}
|
672
|
+
}
|
673
|
+
exports.GrokAgent = GrokAgent;
|
674
|
+
//# sourceMappingURL=grok-agent.js.map
|