-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathagent.ts
More file actions
218 lines (191 loc) · 6.1 KB
/
Copy pathagent.ts
File metadata and controls
218 lines (191 loc) · 6.1 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
import { Runloop } from "@runloop/api-client";
import type { DevboxView } from "@runloop/api-client/resources";
import dotenv from "dotenv";
import OpenAI from "openai";
import type { ChatCompletion, ChatCompletionMessage } from "openai/resources";
dotenv.config();
const OPENAI_API_KEY = process.env.OPENAI_API_KEY;
const RUNLOOP_API_KEY = process.env.RUNLOOP_API_KEY;
const GITHUB_TOKEN = process.env.GITHUB_TOKEN;
if (!OPENAI_API_KEY) {
throw new Error("OPENAI_API_KEY is not set");
}
if (!RUNLOOP_API_KEY) {
throw new Error("RUNLOOP_API_KEY is not set");
}
if (!GITHUB_TOKEN) {
throw new Error("GITHUB_TOKEN is not set");
}
// Initialize clients
const runloop = new Runloop({ bearerToken: RUNLOOP_API_KEY });
const openai = new OpenAI({ apiKey: OPENAI_API_KEY });
const MAX_ITERATIONS = 10;
const SYSTEM_PROMPT = `
You are an expert coder and git user.
`.trim();
const USER_PROMPT = `
Find and navigate to the code repository located in a subdirectory of /home/user and perform cleanups.
- If the repository has a README.md file, use the \`npx doctoc README.md --github\` command to add or update the table of contents.
- If the repository appears to be a Javascript or Typescript project, install and run \`npx @biomejs/biome lint --write .\` to fix all lint errors.
Once you've made all the fixes, stage all changes with git, then show the diff with \`git diff --cached\`.
`.trim();
// Tool definitions
const tools = [
{
type: "function",
function: {
name: "executeShellCommand",
description: "Run a shell command in the devbox",
parameters: {
type: "object",
properties: {
command: { type: "string" },
},
required: ["command"],
additionalProperties: false,
},
strict: true,
},
} as const,
{
type: "function",
function: {
name: "readFile",
description: "Reads a file on the devbox",
parameters: {
type: "object",
properties: {
filename: { type: "string" },
},
required: ["filename"],
additionalProperties: false,
},
strict: true,
},
} as const,
{
type: "function",
function: {
name: "writeFile",
description: "Writes a file on the devbox",
parameters: {
type: "object",
properties: {
filename: { type: "string" },
contents: { type: "string" },
},
required: ["filename", "contents"],
additionalProperties: false,
},
strict: true,
},
} as const,
];
async function runAgent(devbox: DevboxView) {
const messageHistory: ChatCompletionMessage[] = [
{ role: "assistant", content: SYSTEM_PROMPT, refusal: null },
{
role: "user",
content: USER_PROMPT,
refusal: null,
} as unknown as ChatCompletionMessage,
];
let numIterations = 0;
let response: ChatCompletion | null = null;
while (numIterations < MAX_ITERATIONS) {
try {
response = await openai.chat.completions.create({
messages: messageHistory,
model: "gpt-4-turbo",
tools: tools,
tool_choice: "auto",
});
const latestMessage: ChatCompletionMessage = response.choices[0].message;
messageHistory.push(latestMessage);
if (!latestMessage.tool_calls) break;
const toolResponses: ChatCompletionMessage[] = [];
// Process function calls
for (const toolCall of latestMessage.tool_calls) {
const functionArgs = JSON.parse(toolCall.function.arguments);
let result: string;
switch (toolCall.function.name) {
case "executeShellCommand":
result = (
await runloop.devboxes.executeSync(devbox.id, {
command: functionArgs.command,
})
).stdout;
break;
case "readFile":
result = await runloop.devboxes.readFileContents(devbox.id, {
file_path: functionArgs.filename,
});
break;
case "writeFile":
await runloop.devboxes.writeFileContents(devbox.id, {
file_path: functionArgs.filename,
contents: functionArgs.contents,
});
result = "File written successfully.";
break;
default:
throw new Error(`Unknown tool ${toolCall.function.name}`);
}
// Add a tool response message for each tool call
toolResponses.push({
role: "tool",
tool_call_id: toolCall.id,
content: result,
refusal: null,
} as unknown as ChatCompletionMessage);
}
messageHistory.push(...toolResponses);
} catch (error) {
console.error("Error during OpenAI API call:", error);
}
numIterations++;
}
if (!response) {
console.error("Did not receive a response, something went wrong...");
return;
}
console.log(response.choices[0].message.content);
}
function parseGithubUrl(url: string): [string, string] {
if (!url.startsWith("https://github.com/")) {
throw new Error("Invalid github url");
}
const parsed = new URL(url);
const path = parsed.pathname.replace(/^\/|\/$/g, "");
const parts = path.split("/");
if (parts.length !== 2) {
throw new Error("Invalid github url - must contain owner and repo");
}
const repoName = parts[1].endsWith(".git") ? parts[1].replace(/\.git$/, "") : parts[1];
return [parts[0], repoName];
}
async function main() {
if (process.argv.length !== 3) {
console.error("Usage: ts-node agent.ts <github url> \nUsage: npm run start -- <github url> ");
process.exit(1);
}
const [repoOwner, repoName] = parseGithubUrl(process.argv[2]);
console.log(`Repository owner: ${repoOwner}, repository name: ${repoName}`);
console.log("Creating devbox ...");
const devbox = await runloop.devboxes.createAndAwaitRunning({
code_mounts: [
{
repo_name: repoName,
repo_owner: repoOwner,
token: GITHUB_TOKEN,
},
],
});
try {
await runAgent(devbox);
} finally {
console.log(`Destroying devbox ${devbox.id}...`);
await runloop.devboxes.shutdown(devbox.id);
}
}
main().catch(console.error);