-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathactions.ts
117 lines (99 loc) · 3.24 KB
/
actions.ts
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
import { Sandbox } from '@e2b/sdk'
import path from 'path'
import { sandboxLog } from './log'
import { branchID, repoDirPath } from './gh'
export async function makeCommit(sandbox: Sandbox, { message }: { message: string }): Promise<string> {
sandboxLog(`Making commit with message ${message}`)
try {
const processAdd = await sandbox.process.start({
cmd: 'git add .',
cwd: repoDirPath,
})
await processAdd.wait()
const processCommit = await sandbox.process.start({
cmd: `git commit -m '${message}'`,
cwd: repoDirPath,
})
await processCommit.wait()
return 'success'
} catch (e) {
return `Error: ${e.message}}`
}
}
export async function makePullRequest(sandbox: Sandbox, { title }: { title: string }): Promise<string> {
sandboxLog(`Making pull request with title ${title}`)
try {
const processPush = await sandbox.process.start({
cmd: `git push -u origin ai-developer-${branchID}`,
cwd: repoDirPath,
})
await processPush.wait()
const processPR = await sandbox.process.start({
cmd: `gh pr create --title '${title}' --fill`,
cwd: repoDirPath,
})
await processPR.wait()
return 'success'
} catch (e) {
return `Error: ${e.message}}`
}
}
export async function saveCodeToFile(
sandbox: Sandbox,
{ code, filename }: { code: string; filename: string },
): Promise<string> {
sandboxLog(`Saving code to file ${filename}`)
try {
const dir = path.dirname(filename)
await sandbox.filesystem.makeDir(dir)
await sandbox.filesystem.write(filename, code)
return 'success'
} catch (e) {
return `Error: ${e.message}}`
}
}
export async function makeDir(sandbox: Sandbox, { path }: { path: string }): Promise<string> {
sandboxLog(`Creating dir ${path}`)
try {
await sandbox.filesystem.makeDir(path)
return 'success'
} catch (e) {
return `Error: ${e.message}}`
}
}
export async function listFiles(sandbox: Sandbox, { path }: { path: string }): Promise<string> {
sandboxLog(`Listing files in ${path}`)
try {
const files = await sandbox.filesystem.list(path)
const response = files.map(file => (file.isDir ? `dir: ${file.name}` : file.name)).join('\n')
return response
} catch (e) {
return `Error: ${e.message}}`
}
}
export async function readFile(sandbox: Sandbox, { path }: { path: string }): Promise<string> {
sandboxLog(`Reading file ${path}`)
try {
return await sandbox.filesystem.read(path)
} catch (e) {
return `Error: ${e.message}}`
}
}
export async function runCode(sandbox: Sandbox, { command }: { command: string }): Promise<string> {
sandboxLog(`Running command ${command}`)
try {
const process = await sandbox.process.start({ cmd: command, cwd: repoDirPath })
const output = await process.wait()
if (output.exitCode !== 0) {
throw new Error(`Command failed with exit code ${output.exitCode}. Error: ${output.stderr}`)
}
if (!output.stdout) {
console.log(`Command did not produce any output. Error: ${output.stderr}`)
}
// Replace all non-ASCII characters in the output
const cleanedOutput = output.stdout.replace(/[^\x00-\x7F]/g, '')
return cleanedOutput
} catch (e) {
return `Error: ${e.message}`
}
}