(options: CloudboxClientOptions = {})
| 24 | }; |
| 25 | |
| 26 | export function createAgentComputer(options: CloudboxClientOptions = {}) { |
| 27 | const cloudbox = createCloudbox(options); |
| 28 | |
| 29 | return { |
| 30 | async boot({ repo, timeoutMs = 30_000 }: BootOptions): Promise<AgentBox> { |
| 31 | const history: string[] = []; |
| 32 | let submittedArtifact = "HANDOFF.md"; |
| 33 | const run = (commands: string[], artifact = submittedArtifact) => cloudbox.run({ repo, commands, artifact, timeoutMs }); |
| 34 | const box: AgentBox = { |
| 35 | repo, |
| 36 | async shell(cmd) { |
| 37 | history.push(cmd); |
| 38 | return run([cmd]); |
| 39 | }, |
| 40 | async read(path) { |
| 41 | return run([`test -f ${quote(path)} && cat ${quote(path)}`]); |
| 42 | }, |
| 43 | async write(path, content) { |
| 44 | history.push(`write ${path}`); |
| 45 | return run([`cat > ${quote(path)} <<'CLOUDBOX_EOF'\n${content}\nCLOUDBOX_EOF`], path); |
| 46 | }, |
| 47 | tools(names = ["shell", "read", "write"]) { |
| 48 | const all: Record<ToolName, BoxTool> = { |
| 49 | shell: { description: "Run a shell command inside the Cloudbox computer.", parameters: { type: "object", properties: { cmd: { type: "string" } }, required: ["cmd"] }, execute: ({ cmd }) => box.shell(String(cmd)) }, |
| 50 | read: { description: "Read a file from the Cloudbox computer.", parameters: { type: "object", properties: { path: { type: "string" } }, required: ["path"] }, execute: ({ path }) => box.read(String(path)) }, |
| 51 | write: { description: "Write a file in the Cloudbox computer.", parameters: { type: "object", properties: { path: { type: "string" }, content: { type: "string" } }, required: ["path", "content"] }, execute: ({ path, content }) => box.write(String(path), String(content)) }, |
| 52 | }; |
| 53 | return Object.fromEntries(names.map((name) => [name, all[name]])) as Record<ToolName, BoxTool>; |
| 54 | }, |
| 55 | async submit(artifact) { |
| 56 | submittedArtifact = artifact; |
| 57 | return cloudbox.run({ repo, commands: history.length ? history : [`test -f ${quote(artifact)}`], verify: [`test -f ${quote(artifact)}`], artifact, timeoutMs }); |
| 58 | }, |
| 59 | }; |
| 60 | return box; |
| 61 | }, |
| 62 | }; |
| 63 | } |
| 64 | |
| 65 | function quote(value: string) { |
| 66 | return `'${value.replaceAll("'", "'\\''")}'`; |
no test coverage detected