-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathmain.ts
More file actions
76 lines (63 loc) · 1.94 KB
/
main.ts
File metadata and controls
76 lines (63 loc) · 1.94 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
import { Client, Sandbox } from "matchlock-sdk";
const SCRIPT = `import Anthropic from "@anthropic-ai/sdk";
const anthropic = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY,
});
const stream = anthropic.messages
.stream({
model: "claude-haiku-4-5-20251001",
max_tokens: 1024,
messages: [{ role: "user", content: "Explain TCP/IP." }],
})
.on("text", (text) => {
process.stdout.write(text);
});
await stream.finalMessage();
process.stdout.write("\\n");
`;
function ensureSuccess(
step: string,
result: { exitCode: number; stdout: string; stderr: string },
): void {
if (result.exitCode === 0) {
return;
}
throw new Error(
`${step} failed (exit=${result.exitCode})\nstdout:\n${result.stdout}\nstderr:\n${result.stderr}`,
);
}
async function main(): Promise<void> {
const apiKey = process.env.ANTHROPIC_API_KEY;
if (!apiKey) {
throw new Error("ANTHROPIC_API_KEY is required");
}
const client = new Client();
try {
const sandbox = new Sandbox("node:22-alpine")
.withCPUs(1)
.withMemory(512)
.allowHost("registry.npmjs.org", "*.npmjs.org", "api.anthropic.com")
.addSecret("ANTHROPIC_API_KEY", apiKey, "api.anthropic.com");
const vmId = await client.launch(sandbox);
console.log(`launched ${vmId}`);
const install = await client.exec(
"npm init -y >/dev/null 2>&1 && npm install --quiet --no-bin-links @anthropic-ai/sdk",
{ workingDir: "/workspace" },
);
ensureSuccess("install @anthropic-ai/sdk", install);
await client.writeFile("/workspace/ask.mjs", SCRIPT);
const stream = await client.execStream("node ask.mjs", {
workingDir: "/workspace",
stdout: process.stdout,
stderr: process.stderr,
});
console.log(`exit=${stream.exitCode} duration_ms=${stream.durationMs}`);
} finally {
await client.close();
await client.remove();
}
}
void main().catch((error) => {
console.error(error);
process.exit(1);
});