-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathshell-json-example.ts
More file actions
102 lines (88 loc) · 3.15 KB
/
shell-json-example.ts
File metadata and controls
102 lines (88 loc) · 3.15 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
import { generateText } from 'ai';
import { openai } from "@ai-sdk/openai";
import { createCodeExecutionTool } from '../src/code-execution-tool';
import * as fs from 'fs';
import * as path from 'path';
import { v4 as uuidv4 } from 'uuid';
async function main() {
try {
// First, ask AI to generate a shell script that creates folders and JSON files
console.log('Generating shell script...');
const result = await generateText({
model: openai('gpt-4'),
maxSteps: 10,
messages: [
{
role: 'user',
content: `Create a shell script that:
1. Creates 3 directories: data1, data2, and data3
2. Creates a JSON file in each directory with different data
3. Uses jq to read and display one node from each JSON file
4. Include the necessary Alpine package installation command for jq
The script should:
- Install jq using apk
- Create directories and JSON files
- Use jq to extract and display data from each file
- Handle errors appropriately
Please format your response as a shell script with comments explaining each step.`
}
],
tools: { codeExecutionTool: createCodeExecutionTool().codeExecutionTool },
toolChoice: 'auto'
});
console.log('AI Response:', result.text);
// Extract the actual script content from between the markdown code block markers
const scriptContent = result.text.match(/```(?:sh|bash)?\n([\s\S]*?)```/)?.[1];
if (!scriptContent) {
throw new Error('Failed to extract script content from AI response');
}
// Create a temporary directory for the script
const scriptDir = path.join('/tmp', uuidv4());
fs.mkdirSync(scriptDir, { recursive: true });
// Write the shell script to a file
const scriptPath = path.join(scriptDir, 'process_json.sh');
fs.writeFileSync(scriptPath, scriptContent);
fs.chmodSync(scriptPath, '755');
console.log('** Script Directory:', scriptDir);
console.log('** Created script file:', scriptPath);
// Create a code execution tool with the script directory mounted
const { codeExecutionTool }= createCodeExecutionTool({
mounts: [{
type: 'directory',
source: scriptDir,
target: '/project'
}]
});
console.log('Executing script...');
// Execute the script using codeExecutionTool
const executionResult = await codeExecutionTool.execute({
language: 'shell',
code: '',
runApp: {
entryFile: 'process_json.sh',
cwd: '/project'
},
dependencies: ['jq'],
streamOutput: {
dependencyStdout: (data) => {
console.log('Dependency stdout:', data);
},
dependencyStderr: (data) => {
console.error('Dependency stderr:', data);
},
stdout: (data) => {
console.log('Container stdout:', data);
},
stderr: (data) => {
console.error('Container stderr:', data);
}
}
});
console.log('Script Execution Result:');
console.log('Exit Code:', executionResult.exitCode);
console.log('Execution Time:', executionResult.executionTime, 'ms');
} catch (error) {
console.error('Error:', error);
}
}
main();