-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinteractive-shell-example.ts
More file actions
362 lines (321 loc) · 13.9 KB
/
interactive-shell-example.ts
File metadata and controls
362 lines (321 loc) · 13.9 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { createCodeExecutionTool, getImageForLanguage } from '../src/code-execution-tool';
import { v4 as uuidv4 } from 'uuid';
import { ContainerStrategy } from '../src/types';
import * as readline from 'readline';
import * as path from 'path';
import * as fs from 'fs';
import { createFileTools } from '../src/file-tools';
// Simple spinner animation
const spinner = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'];
let spinnerInterval: NodeJS.Timeout;
// Command history interface
interface CommandHistory {
input: string;
executionInfo?: any;
output?: {
stdout?: string;
stderr?: string;
generatedFiles?: string[];
};
error?: string;
timestamp: Date;
}
function startSpinner(message: string) {
let i = 0;
process.stdout.write('\r' + message + ' ' + spinner[0]);
spinnerInterval = setInterval(() => {
process.stdout.write('\r' + message + ' ' + spinner[i]);
i = (i + 1) % spinner.length;
}, 80);
}
function stopSpinner(success: boolean, message: string) {
clearInterval(spinnerInterval);
process.stdout.write('\r' + (success ? '✓' : '✗') + ' ' + message + '\n');
}
// Function to format history for AI context
function formatHistoryForAI(history: CommandHistory[]): string {
return history.map(entry => {
let formatted = `Command: ${entry.input}\n`;
if (entry.executionInfo) {
formatted += `Execution Details:\n`;
if (entry.executionInfo.runApp) {
formatted += `- Application: ${entry.executionInfo.runApp.entryFile}\n`;
formatted += `- Working directory: ${entry.executionInfo.runApp.cwd}\n`;
} else {
formatted += `- Language: ${entry.executionInfo.language}\n`;
if (entry.executionInfo.dependencies?.length > 0) {
formatted += `- Dependencies: ${entry.executionInfo.dependencies.join(', ')}\n`;
}
formatted += `- Code:\n\`\`\`${entry.executionInfo.language}\n${entry.executionInfo.code}\n\`\`\`\n`;
}
}
if (entry.output) {
if (entry.output.stdout) formatted += `Output:\n${entry.output.stdout}\n`;
if (entry.output.stderr) formatted += `Error Output:\n${entry.output.stderr}\n`;
if (entry.output.generatedFiles && entry.output.generatedFiles.length > 0) {
formatted += `Generated Files: ${entry.output.generatedFiles.join(', ')}\n`;
}
}
if (entry.error) formatted += `Error: ${entry.error}\n`;
formatted += `---\n`;
return formatted;
}).join('\n');
}
async function main() {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const sessionId = `interactive-${uuidv4()}`;
const commandHistory: CommandHistory[] = [];
// Create the execution tool with shared workspace
const { codeExecutionTool, executionEngine } = createCodeExecutionTool({
defaultStrategy: 'per_session',
sessionId,
verbosity: 'info',
workspaceSharing: 'shared'
});
// Create initial session
await executionEngine.createSession({
sessionId,
strategy: ContainerStrategy.PER_SESSION,
containerConfig: {
image: getImageForLanguage('shell')
}
});
// Get session info for workspace directory
const sessionInfo = await executionEngine.getSessionInfo(sessionId);
const workspaceDir = sessionInfo.currentContainer.meta?.workspaceDir;
// Create file tools for the workspace directory
const { createFileStructureTool, writeFileTool, readFileTool, listFilesTool } = createFileTools(workspaceDir ?? process.cwd());
console.log('⚡️ AI Shell ⚡️ - code-interpreter-tools interactive shell example');
console.log('GitHub: https://github.com/CatchTheTornado/code-interpreter-tools\n');
console.log('Workspace Directory:', workspaceDir);
console.log('Type your commands or AI prompts below.');
console.log('Special commands:');
console.log(' - "info" - Show session information and container history');
console.log(' - "history" - Show command history');
console.log(' - "quit" - Exit the shell');
console.log('\n');
const prompt = () => {
rl.question('> ', async (input) => {
if (input.toLowerCase() === 'quit') {
rl.close();
return;
}
if (input.toLowerCase() === 'info') {
const sessionInfo = await executionEngine.getSessionInfo(sessionId);
console.log('\n=== Session Information ===');
console.log('Session ID:', sessionInfo.sessionId);
console.log('Created:', sessionInfo.createdAt);
console.log('Last Executed:', sessionInfo.lastExecutedAt || 'Never');
console.log('Active:', sessionInfo.isActive ? 'Yes' : 'No');
console.log('\nCurrent Container:');
console.log('- Image:', sessionInfo.currentContainer.container ?
(await sessionInfo.currentContainer.container.inspect()).Config.Image : 'None');
console.log('- Running:', sessionInfo.currentContainer.meta?.isRunning ? 'Yes' : 'No');
console.log('- Created:', sessionInfo.currentContainer.meta?.createdAt);
console.log('- Last Executed:', sessionInfo.currentContainer.meta?.lastExecutedAt || 'Never');
console.log('\nContainer History:');
sessionInfo.containerHistory.forEach((meta, index) => {
console.log(`\nContainer ${index + 1}:`);
console.log('- Name:', meta.containerName);
console.log('- Image:', meta.imageName);
console.log('- Container ID:', meta.containerId);
console.log('- Created:', meta.createdAt);
console.log('- Last Executed:', meta.lastExecutedAt || 'Never');
console.log('- Generated Files:', meta.sessionGeneratedFiles.size);
});
console.log('\n');
prompt();
return;
}
if (input.toLowerCase() === 'history') {
console.log('\n=== Command History ===\n');
commandHistory.forEach((entry, index) => {
console.log(`[${index + 1}] ${entry.input}`);
if (entry.error) {
console.log(' Error:', entry.error);
}
console.log();
});
prompt();
return;
}
startSpinner('AI Thinking...');
try {
const historyContext = formatHistoryForAI(commandHistory);
const result = await generateText({
model: openai('gpt-4'),
maxSteps: 1,
messages: [
{
role: 'system',
content: `You are an AI assistant in an interactive shell environment. Shell is bash, you are on alpine linux. Use "apk" in case of missing shell commands. Here is the history of previous commands and their outputs:\n\n${historyContext}\n\nBased on this history, execute the following command or prompt. Use the "codeExecutionTool" to execute the code to achieve the user desired result. If not specified differently, try to use "shell" or "python" languages. If using non-standard modules, pass them as "dependencies" to be installed. If user asks about the history or context shared with the conversation and you can answer based on the information you already have you do not need to call any command - just answer, but do not assume anything which is dynamic - for example always check the files and folder content using "codeExecutionTool" and do not assume you know it.
When asked to generate a file structure or create multiple files, call the "createFileStructureTool" tool and pass the JSON object describing the structure as the "structure" argument. The JSON should be in this format:
{
"structure": {
"files": [
{
"path": "path/to/file",
"content": "file content",
"description": "what this file does"
}
],
"dependencies": ["list", "of", "dependencies"]
}
}
`
},
{
role: 'user',
content: input
}
],
tools: { codeExecutionTool, createFileStructureTool },
toolChoice: 'auto'
});
stopSpinner(true, 'AI Response received');
// Display execution results
const toolResult = (result.toolResults?.[0] as any)?.result;
const executionInfo = (result.toolCalls?.[0] as any)?.args;
// Remove debug logs; display handled below
// Store in history
const historyEntry: CommandHistory = {
input,
executionInfo,
output: toolResult,
timestamp: new Date()
};
commandHistory.push(historyEntry);
// Display AI textual response if provided
if (result.text && result.text.trim().length > 0) {
console.log('\n🤖 AI Response:');
console.log(result.text.trim());
}
// Handle createFileStructureTool custom display
if (toolResult && (toolResult.files || toolResult.dirs)) {
const filesArr: Array<{ path: string; description?: string }> = toolResult.files ?? [];
const dirsArr: string[] = toolResult.dirs ?? [];
// Build dir groups
const dirGroups: Record<string, Array<{ path: string; description?: string }>> = {};
for (const file of filesArr) {
const d = path.dirname(file.path);
if (!dirGroups[d]) dirGroups[d] = [];
dirGroups[d].push(file);
}
// Ensure empty dirs are included
for (const d of dirsArr) {
if (!dirGroups[d]) dirGroups[d] = [];
}
console.log('\n📂 Workspace changes:\n');
const sortedDirs = Object.keys(dirGroups).sort();
for (const dir of sortedDirs) {
const prettyDir = dir === '.' ? '' : `📁 ${dir}/`;
if (prettyDir) console.log(prettyDir);
const filesInDir = dirGroups[dir];
for (const f of filesInDir) {
const fileName = path.basename(f.path);
console.log(`${dir === '.' ? '' : ' '}📄 ${fileName}`);
if (f.description) console.log(`${dir === '.' ? '' : ' '}${f.description}`);
}
if (filesInDir.length === 0) console.log(`${dir === '.' ? '' : ' '}(empty)`);
console.log();
}
if (toolResult.dependencies && toolResult.dependencies.length > 0) {
console.log('📦 Dependencies:');
console.log(' ' + toolResult.dependencies.join(', '));
console.log();
}
console.log('✨ File structure updated successfully!');
} else if (toolResult) {
// Show what's being executed
if (executionInfo && executionInfo.language) {
console.log('\nExecuting in Docker sandbox:');
if (executionInfo.runApp) {
console.log(`Application: ${executionInfo.runApp.entryFile}`);
console.log(`Working directory: ${executionInfo.runApp.cwd}`);
} else {
console.log(`Language: ${executionInfo.language}`);
if (executionInfo.dependencies?.length > 0) {
console.log(`Dependencies: ${executionInfo.dependencies.join(', ')}`);
}
console.log('\nCode:');
console.log('```' + executionInfo.language);
console.log(executionInfo.code);
console.log('```\n');
}
}
console.log('\nOutput:');
if (toolResult.stdout) {
console.log(toolResult.stdout);
}
if (toolResult.stderr) {
console.error(toolResult.stderr);
}
if (toolResult.generatedFiles?.length > 0) {
console.log('\n[Generated files: ' + toolResult.generatedFiles.join(', ') + ']');
}
}
console.log(); // Add blank line for readability
prompt(); // Continue the loop
} catch (error) {
stopSpinner(false, 'Error occurred');
console.error('Error:', error);
// Store error in history
commandHistory.push({
input,
error: error instanceof Error ? error.message : String(error),
timestamp: new Date()
});
// Ask AI to fix the error
try {
console.log('\nAttempting to fix the error...');
const fixResult = await generateText({
model: openai('gpt-4'),
maxSteps: 1,
messages: [
{
role: 'system',
content: `Previous command failed with error: ${error}\n\nPlease analyze the error and provide a fixed version of the command.`
},
{
role: 'user',
content: input
}
],
tools: { codeExecutionTool, createFileStructureTool },
toolChoice: 'auto'
});
// Display AI's explanation if present
if (fixResult.text) {
console.log('\nAI Analysis:');
console.log(fixResult.text);
console.log();
}
const fixToolResult = (fixResult.toolResults?.[0] as any)?.result;
if (fixToolResult?.stdout || fixToolResult?.stderr) {
console.log('\nFixed command output:');
if (fixToolResult.stdout) console.log(fixToolResult.stdout);
if (fixToolResult.stderr) console.error(fixToolResult.stderr);
}
} catch (fixError) {
console.error('Failed to fix the error:', fixError);
}
console.log(); // Add blank line for readability
prompt(); // Continue the loop
}
});
};
// Start the interactive loop
prompt();
// Handle cleanup on exit
rl.on('close', async () => {
console.log('\n🧹 Cleaning up...');
await executionEngine.cleanupSession(sessionId);
process.exit(0);
});
}
main().catch(console.error);