Skip to content

Commit 01798ff

Browse files
committed
feat: add go sdk for code interpreter
Signed-off-by: joey <zchengjoey@gmail.com> (cherry picked from commit 01e1125)
1 parent 6d54054 commit 01798ff

21 files changed

Lines changed: 3737 additions & 1 deletion

.github/workflows/go_tests.yml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
name: Test Go SDK
2+
3+
on:
4+
workflow_call:
5+
secrets:
6+
E2B_API_KEY:
7+
required: false
8+
inputs:
9+
E2B_DOMAIN:
10+
required: false
11+
type: string
12+
E2B_TESTS_TEMPLATE:
13+
required: false
14+
type: string
15+
16+
permissions:
17+
contents: read
18+
19+
jobs:
20+
test:
21+
defaults:
22+
run:
23+
working-directory: ./go
24+
name: Go SDK - Build and test
25+
runs-on: ubuntu-22.04
26+
steps:
27+
- name: Checkout repository
28+
uses: actions/checkout@v4
29+
30+
- name: Parse .tool-versions
31+
uses: wistia/parse-tool-versions@v2.1.1
32+
with:
33+
filename: '.tool-versions'
34+
uppercase: 'true'
35+
prefix: 'tool_version_'
36+
37+
- name: Set up Go
38+
uses: actions/setup-go@v5
39+
with:
40+
go-version: '${{ env.TOOL_VERSION_GOLANG }}'
41+
cache-dependency-path: go/go.sum
42+
43+
- name: Download dependencies
44+
run: go mod download
45+
46+
- name: Verify dependencies
47+
run: go mod verify
48+
49+
- name: Build
50+
run: go build ./...
51+
52+
- name: Vet
53+
run: go vet ./...
54+
55+
- name: Run tests
56+
run: go test -race -coverprofile=coverage.out -covermode=atomic ./...
57+
env:
58+
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}
59+
E2B_DOMAIN: ${{ inputs.E2B_DOMAIN }}
60+
E2B_TESTS_TEMPLATE: ${{ inputs.E2B_TESTS_TEMPLATE }}
61+
62+
- name: Upload coverage report
63+
if: always()
64+
uses: actions/upload-artifact@v4
65+
with:
66+
name: go-coverage
67+
path: go/coverage.out
68+
if-no-files-found: ignore

.github/workflows/lint.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,27 @@ jobs:
5959
poetry install --with dev
6060
pip install ruff=="0.11.12"
6161
62+
- name: Set up Go
63+
uses: actions/setup-go@v5
64+
with:
65+
go-version: '${{ env.TOOL_VERSION_GOLANG }}'
66+
cache-dependency-path: go/go.sum
67+
68+
- name: Check Go formatting
69+
working-directory: go
70+
run: |
71+
if [[ -n "$(gofmt -l .)" ]]; then
72+
echo "❌ Go files are not formatted properly:"
73+
gofmt -d .
74+
exit 1
75+
else
76+
echo "✅ Go files are properly formatted."
77+
fi
78+
79+
- name: Go vet
80+
working-directory: go
81+
run: go vet ./...
82+
6283
- name: Run linting
6384
run: |
6485
pnpm run lint

.github/workflows/pull_request.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ jobs:
3636
with:
3737
E2B_DOMAIN: ${{ vars.E2B_DOMAIN }}
3838
E2B_TESTS_TEMPLATE: ${{ needs.build-template.outputs.template_id }}
39+
go-sdk:
40+
uses: ./.github/workflows/go_tests.yml
41+
needs: build-template
42+
secrets:
43+
E2B_API_KEY: ${{ secrets.E2B_API_KEY }}
44+
with:
45+
E2B_DOMAIN: ${{ vars.E2B_DOMAIN }}
46+
E2B_TESTS_TEMPLATE: ${{ needs.build-template.outputs.template_id }}
3947
performance-tests:
4048
uses: ./.github/workflows/performance_tests.yml
4149
needs: build-template
@@ -46,7 +54,7 @@ jobs:
4654
E2B_TESTS_TEMPLATE: ${{ needs.build-template.outputs.template_id }}
4755
cleanup-build-template:
4856
uses: ./.github/workflows/cleanup_build_template.yml
49-
needs: [build-template, js-sdk, python-sdk, performance-tests]
57+
needs: [build-template, js-sdk, python-sdk, go-sdk, performance-tests]
5058
if: always() && !contains(needs.build-template.result, 'failure') && !contains(needs.build-template.result, 'cancelled')
5159
secrets:
5260
E2B_TESTS_ACCESS_TOKEN: ${{ secrets.E2B_TESTS_ACCESS_TOKEN }}

.tool-versions

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
python 3.10
22
poetry 1.8.5
3+
golang 1.21.5

go/README.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
# E2B Code Interpreter — Go SDK
2+
3+
Go SDK for the [E2B](https://e2b.dev) Code Interpreter. It lets you run
4+
AI-generated code inside secure, isolated E2B sandboxes and get back rich,
5+
structured results (text, HTML, images, chart data, …).
6+
7+
This package mirrors the features of the official
8+
[Python](../python) and [JavaScript](../js) SDKs.
9+
10+
## Install
11+
12+
```bash
13+
go get github.com/e2b-dev/codeinterpreter-go
14+
```
15+
16+
> **Go version**: 1.21+
17+
18+
## Get your API key
19+
20+
1. Sign up at [e2b.dev](https://e2b.dev).
21+
2. Grab an API key from the [dashboard](https://e2b.dev/dashboard?tab=keys).
22+
3. Export it:
23+
24+
```bash
25+
export E2B_API_KEY=e2b_***
26+
```
27+
28+
## Quick start
29+
30+
```go
31+
package main
32+
33+
import (
34+
"context"
35+
"fmt"
36+
"log"
37+
"time"
38+
39+
codeinterpreter "github.com/e2b-dev/codeinterpreter-go"
40+
)
41+
42+
func main() {
43+
ctx := context.Background()
44+
45+
sbx, err := codeinterpreter.Create(ctx, &codeinterpreter.SandboxOpts{
46+
Timeout: 60 * time.Second,
47+
})
48+
if err != nil { log.Fatal(err) }
49+
defer sbx.Kill(ctx)
50+
51+
_, _ = sbx.RunCode(ctx, "x = 1", nil)
52+
53+
exec, err := sbx.RunCode(ctx, "x += 1; x", nil)
54+
if err != nil { log.Fatal(err) }
55+
56+
fmt.Println(exec.Text()) // "2"
57+
}
58+
```
59+
60+
## Features
61+
62+
The Go SDK exposes the same surface as the Python / JS SDKs:
63+
64+
### Sandbox lifecycle
65+
66+
| Function | Description |
67+
|---|---|
68+
| `Create(ctx, opts)` | Start a new sandbox. |
69+
| `Connect(ctx, id, opts)` | Attach to a running sandbox. |
70+
| `List(ctx, opts)` | List sandboxes for the API key. |
71+
| `Sandbox.Kill(ctx)` | Terminate the sandbox. |
72+
| `Sandbox.SetTimeout(ctx, d)` | Extend the sandbox lifetime. |
73+
| `Sandbox.IsRunning(ctx)` | Health check. |
74+
| `Sandbox.GetInfo(ctx)` | Metadata, start time, etc. |
75+
| `Sandbox.GetHost(port)` | Hostname for a port exposed by the sandbox. |
76+
77+
### Code execution
78+
79+
| Function | Description |
80+
|---|---|
81+
| `Sandbox.RunCode(ctx, code, opts)` | Execute code (any supported language). |
82+
83+
`RunCodeOpts` lets you pass:
84+
85+
* `Language``"python"` / `"javascript"` / `"typescript"` / `"r"` / `"java"` / `"bash"` or any custom kernel id.
86+
* `Context` — a pre-created `*Context` (mutually exclusive with `Language`).
87+
* `Envs` — extra environment variables.
88+
* `Timeout` / `RequestTimeout` — execution / request timeouts.
89+
* `OnStdout`, `OnStderr`, `OnResult`, `OnError` — streaming callbacks.
90+
91+
### Code contexts (Jupyter kernels)
92+
93+
| Function | Description |
94+
|---|---|
95+
| `Sandbox.CreateCodeContext(ctx, opts)` | Create a fresh kernel. |
96+
| `Sandbox.ListCodeContexts(ctx)` | List known kernels. |
97+
| `Sandbox.RestartCodeContext(ctx, c)` | Restart a kernel (clears state). |
98+
| `Sandbox.RemoveCodeContext(ctx, c)` | Terminate a kernel. |
99+
100+
### Result / Execution model
101+
102+
Every `RunCode` call returns an `*Execution`:
103+
104+
```go
105+
type Execution struct {
106+
Results []*Result
107+
Logs Logs // stdout / stderr lines
108+
Error *ExecutionError // nil on success
109+
ExecutionCount int
110+
}
111+
```
112+
113+
Each `Result` may carry multiple representations of the same value: `Text`,
114+
`HTML`, `Markdown`, `SVG`, `PNG`, `JPEG`, `PDF`, `LaTeX`, `JSON`, `JavaScript`,
115+
`Data`, `Chart`, plus arbitrary `Extra` MIME types.
116+
117+
### Charts
118+
119+
`Result.Chart` is a `Chart` interface — type-assert it to inspect the
120+
structured data:
121+
122+
```go
123+
switch c := result.Chart.(type) {
124+
case *codeinterpreter.LineChart:
125+
for _, series := range c.Points { ... }
126+
case *codeinterpreter.BarChart:
127+
for _, bar := range c.Bars { ... }
128+
case *codeinterpreter.PieChart:
129+
for _, slice := range c.Slices { ... }
130+
case *codeinterpreter.BoxAndWhiskerChart:
131+
...
132+
case *codeinterpreter.ScatterChart:
133+
...
134+
case *codeinterpreter.SuperChart:
135+
for _, sub := range c.Charts { ... }
136+
}
137+
```
138+
139+
## Streaming output
140+
141+
```go
142+
exec, err := sbx.RunCode(ctx, "for i in range(5): print(i)", &codeinterpreter.RunCodeOpts{
143+
OnStdout: func(msg codeinterpreter.OutputMessage) {
144+
fmt.Println(">", msg.Line)
145+
},
146+
OnResult: func(r *codeinterpreter.Result) {
147+
fmt.Println("got result with formats:", r.Formats())
148+
},
149+
OnError: func(e *codeinterpreter.ExecutionError) {
150+
fmt.Println("error:", e.Name, e.Value)
151+
},
152+
})
153+
```
154+
155+
## Error types
156+
157+
| Error | When |
158+
|---|---|
159+
| `*AuthenticationError` | Invalid / missing API key. |
160+
| `*NotFoundError` | Resource (sandbox, context) not found. |
161+
| `*TimeoutError` | Request or execution timed out. |
162+
| `*RateLimitError` | Hit E2B rate limit. |
163+
| `*InvalidArgumentError` | Bad arguments (e.g. both `Language` + `Context`). |
164+
| `*SandboxError` | Generic error from the backend. |
165+
166+
Use Go's type assertion / `errors.As` to discriminate between them.
167+
168+
## Check docs
169+
170+
Visit the [E2B documentation](https://e2b.dev/docs) for more details.
171+

0 commit comments

Comments
 (0)