-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathmain.go
More file actions
98 lines (85 loc) · 2.41 KB
/
Copy pathmain.go
File metadata and controls
98 lines (85 loc) · 2.41 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
package main
import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"github.com/jingkaihe/matchlock/internal/errx"
"github.com/jingkaihe/matchlock/pkg/sdk"
)
var (
errCreateClient = errors.New("create client")
errLaunchSandbox = errors.New("launch sandbox")
errExecPythonVer = errors.New("exec python3 --version")
errExecPipInstall = errors.New("exec pip install uv")
errWriteFile = errors.New("write_file")
errExecStream = errors.New("exec_stream")
)
func main() {
if err := run(); err != nil {
slog.Error("fatal", "error", err)
os.Exit(1)
}
}
func run() error {
cfg := sdk.DefaultConfig()
if os.Getenv("MATCHLOCK_BIN") == "" {
cfg.BinaryPath = "./bin/matchlock"
}
client, err := sdk.NewClient(cfg)
if err != nil {
return errx.Wrap(errCreateClient, err)
}
defer client.Remove()
defer client.Close(0)
sandbox := sdk.New("python:3.12-alpine").
AllowHost(
"dl-cdn.alpinelinux.org",
"files.pythonhosted.org", "pypi.org",
"astral.sh", "github.com", "objects.githubusercontent.com",
"api.anthropic.com",
).
AddSecret("ANTHROPIC_API_KEY", os.Getenv("ANTHROPIC_API_KEY"), "api.anthropic.com")
vmID, err := client.Launch(sandbox)
if err != nil {
return errx.Wrap(errLaunchSandbox, err)
}
slog.Info("sandbox ready", "vm", vmID)
result, err := client.Exec(context.Background(), "python3 --version")
if err != nil {
return errx.Wrap(errExecPythonVer, err)
}
fmt.Print(result.Stdout)
if _, err := client.Exec(context.Background(), "pip install --quiet uv"); err != nil {
return errx.Wrap(errExecPipInstall, err)
}
script := `# /// script
# requires-python = ">=3.12"
# dependencies = ["anthropic"]
# ///
import anthropic, os
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
with client.messages.stream(
model="claude-haiku-4-5-20251001",
max_tokens=1000,
messages=[{"role": "user", "content": "Explain TCP to me"}],
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)
print()
`
if err := client.WriteFile(context.Background(), "/workspace/ask.py", []byte(script)); err != nil {
return errx.Wrap(errWriteFile, err)
}
streamResult, err := client.ExecStream(context.Background(),
"uv run /workspace/ask.py",
os.Stdout, os.Stderr,
)
if err != nil {
return errx.Wrap(errExecStream, err)
}
fmt.Println()
slog.Info("done", "exit_code", streamResult.ExitCode, "duration_ms", streamResult.DurationMS)
return nil
}