-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
294 lines (246 loc) · 9.46 KB
/
index.js
File metadata and controls
294 lines (246 loc) · 9.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env node
/**
* AI Agent Console Application
* Run with: node index.js
*/
// Prevent multiple execution
if (global.__verysimpleaiagent_running) {
console.error('Warning: Application already running!');
process.exit(1);
}
global.__verysimpleaiagent_running = true;
const { TerminalUI, BG_COLORS } = require('./terminal-ui');
const { Agent } = require('./agent-node');
const { JavaScriptTool, ShellCommandTool, ThinkTool } = require('./tools-node');
const { LLMClient, LLMConfig } = require('./llm-node');
// Create terminal UI
const ui = new TerminalUI();
// Redirect console.log to UI with special handling for tool output
// Track if we're inside a tool execution block
let inToolExecution = false;
let toolExecutionColor = 'CYAN';
let inInspectSection = false;
console.log = (...args) => {
const text = args.join(' ');
// Detect inspect section start
if (text.includes('INSPECT PROMPT') || text.includes('INSPECT RESPONSE')) {
inInspectSection = true;
ui.setBgColor('BLACK');
}
// Detect tool execution start
if (text.includes('EXECUTION START:')) {
inToolExecution = true;
inInspectSection = false; // Exit inspect if we were in it
}
// If in inspect section, add to top panel (Agent Inner Workings)
if (inInspectSection) {
ui.addToTopPanel(text);
} else {
ui.log(text);
}
// Detect tool execution end
if (text.includes('TOOL EXECUTION END:')) {
inToolExecution = false;
ui.resetBgColor();
}
// Detect inspect section end (when "Continuing" message appears)
if (inInspectSection && (text.includes('Continuing with request') || text.includes('Continuing...'))) {
inInspectSection = false;
ui.resetBgColor();
}
};
console.error = (...args) => {
ui.setBgColor('RED');
ui.log('❌ ' + args.join(' '));
ui.resetBgColor();
};
// Initialize the agent
let agent;
let config;
// Display welcome message
ui.log('VerySimpleAIAgent with Tools: JavaScriptTool + ShellCommandTool + ThinkTool');
ui.log('Commands: bye, config, history, help');
ui.log('');
// Initialize the agent with configuration
function initializeAgent() {
try {
config = new LLMConfig();
if (!config.isValid()) {
ui.log('⚠️ LLM not configured. Agent in simple mode.');
ui.log('Type "config" to set up your LLM API.');
}
const llmClient = new LLMClient(config, ui); // Pass the UI interface
const jsTool = new JavaScriptTool(ui); // Pass the UI interface
const shellTool = new ShellCommandTool(ui); // Pass the UI interface
const thinkTool = new ThinkTool();
agent = new Agent([jsTool, shellTool, thinkTool], llmClient, ui);
// Initialize visualization with actual system and tool tokens
ui.initializeVisualizationFromAgent(agent);
ui.log('VerySimpleAIAgent initialized successfully!');
if (config.isValid()) {
ui.log(`Provider: ${config.provider} | Model: ${config.model}`);
} else {
ui.log('💡 Tip: Use "config" command to configure LLM');
}
} catch (error) {
ui.log(`❌ Error initializing agent: ${error.message}`);
process.exit(1);
}
}
// Handle configuration
async function configureAgent() {
const { LLMConfig } = require('./llm-node');
const presets = LLMConfig.getPresets();
ui.log('╔════════════════════════════════════════════════════════════╗');
ui.log('║ Configuration Menu ║');
ui.log('╚════════════════════════════════════════════════════════════╝');
ui.log('Available providers:');
ui.log(' 1. OpenAI 2. Azure 3. Anthropic 4. Ollama 5. Custom');
return new Promise((resolve) => {
ui.question('Select provider (1-5): ', (choice) => {
const providers = ['openai', 'azure', 'anthropic', 'ollama', 'custom'];
const provider = providers[parseInt(choice) - 1] || 'openai';
const preset = presets[provider];
ui.question(`API Key${provider === 'ollama' ? ' (Enter to skip)' : ''}: `, (apiKey) => {
const actualApiKey = provider === 'ollama' ? 'not-required' : apiKey;
ui.question(`Endpoint (default: ${preset.endpoint}): `, (endpoint) => {
const actualEndpoint = endpoint || preset.endpoint;
ui.question(`Model (default: ${preset.defaultModel}): `, (model) => {
const actualModel = model || preset.defaultModel;
config.update({
provider,
apiKey: actualApiKey,
endpoint: actualEndpoint,
model: actualModel
});
ui.log('✅ Configuration saved!');
initializeAgent();
resolve();
});
});
});
});
});
}
// Process user input
async function processInput(input) {
const trimmedInput = input.trim();
if (!trimmedInput) {
return;
}
// Handle special commands
if (trimmedInput === 'bye') {
ui.log('👋 Goodbye!');
ui.close();
process.exit(0);
}
if (trimmedInput === 'config') {
// Show user input in green background
ui.setBgColor('GREEN');
ui.log(`🐱 You: ${trimmedInput}`);
ui.resetBgColor();
await configureAgent();
// Show breakpoint before waiting for next input
await agent.breakpoint('Waiting for question from user', {
file: __filename,
phase: 'wait for a user\'s question',
skipWait: true
});
return;
}
if (trimmedInput === 'help') {
// Show user input in green background
ui.setBgColor('GREEN');
ui.log(`🐱 You: ${trimmedInput}`);
ui.resetBgColor();
ui.log('Available commands:');
ui.log(' config - Configure LLM settings');
ui.log(' history - Show conversation history');
ui.log(' help - Show this help');
ui.log(' bye - Exit application');
// Show breakpoint before waiting for next input
await agent.breakpoint('Waiting for question from user', {
file: __filename,
phase: 'wait for a user\'s question',
skipWait: true
});
return;
}
if (trimmedInput === 'history') {
// Show user input in green background
ui.setBgColor('GREEN');
ui.log(`🐱 You: ${trimmedInput}`);
ui.resetBgColor();
agent.printHistorySummary();
// Show breakpoint before waiting for next input
await agent.breakpoint('Waiting for question from user', {
file: __filename,
phase: 'wait for a user\'s question',
skipWait: true
});
return;
}
// Process with agent - show user input and start thinking animation
try {
ui.setBgColor('GREEN');
ui.log(`🐱 You: ${trimmedInput}`);
ui.resetBgColor();
// Start thinking animation (just the rotating character, no label)
ui.startThinking();
const response = await agent.processQuestion(trimmedInput);
// Stop thinking animation
ui.stopThinking();
await agent.breakpoint('Return final answer to user', {
file: __filename,
phase: 'display response'
});
// Set green background for agent response
ui.setBgColor('GREEN');
// Phase: display response
ui.log(`🤖 Agent: ${response}`);
ui.resetBgColor();
} catch (error) {
// Stop thinking animation on error
ui.stopThinking();
ui.setBgColor('RED');
ui.log(`❌ Error: ${error.message}`);
ui.resetBgColor();
}
// Show breakpoint before waiting for next input
await agent.breakpoint('Waiting for question from user', {
file: __filename,
phase: 'wait for a user\'s question',
skipWait: true
});
}
// Initialize agent on startup
initializeAgent();
// Show initial state - waiting for user question (right before event loop becomes active)
agent.breakpoint('Waiting for question from user', {
file: __filename,
phase: 'wait for a user\'s question',
skipWait: true // Don't wait for Enter since we're already waiting for user input
});
// Track if we're currently processing input to prevent re-entry
let isProcessingInput = false;
// Handle user input (register only once)
if (!global.__verysimpleaiagent_handler_registered) {
global.__verysimpleaiagent_handler_registered = true;
// Phase: wait for a user's question
ui.on('line', async (input) => {
// Only process if there's actual input
// (empty input still needs to propagate to other handlers like question())
if (input && input.trim()) {
// Prevent re-entrant calls
if (isProcessingInput) {
return;
}
isProcessingInput = true;
try {
await processInput(input);
} finally {
isProcessingInput = false;
}
}
});
}