-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodeinterpreter.go
More file actions
141 lines (116 loc) · 3.74 KB
/
Copy pathcodeinterpreter.go
File metadata and controls
141 lines (116 loc) · 3.74 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
package leap0
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/leap0dev/leap0-go/internal/transport"
)
// --- Code Interpreter Types ---
type Language string
const (
Python Language = "python"
TypeScript Language = "typescript"
)
type CodeContext struct {
ID string `json:"id"`
Language Language `json:"language"`
Cwd string `json:"cwd,omitempty"`
}
type RunCodeParams struct {
Code string `json:"code"`
Language Language `json:"language,omitempty"`
ContextID string `json:"context_id,omitempty"`
EnvVars map[string]string `json:"env_vars,omitempty"`
TimeoutMs int `json:"timeout_ms,omitempty"`
}
type RunCodeResult struct {
ExitCode int `json:"exit_code"`
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
}
type StreamEventKind string
const (
EventStdout StreamEventKind = "stdout"
EventStderr StreamEventKind = "stderr"
EventExit StreamEventKind = "exit"
EventError StreamEventKind = "error"
)
type StreamEvent struct {
Type StreamEventKind `json:"type"`
Data string `json:"data,omitempty"`
ExitCode int `json:"exit_code,omitempty"`
}
// --- CodeInterpreterService ---
type CodeInterpreterService struct{ t *transport.Client }
func (s *CodeInterpreterService) url(id, path string) string {
return s.t.SandboxURL(id, 0, path)
}
func (s *CodeInterpreterService) Health(ctx context.Context, id string) (bool, error) {
resp, err := s.t.Raw(ctx, http.MethodGet, s.url(id, "/healthz"), nil, nil)
if err != nil {
return false, err
}
resp.Body.Close()
return resp.StatusCode == 200, nil
}
func (s *CodeInterpreterService) CreateContext(ctx context.Context, id string, lang Language, cwd string) (*CodeContext, error) {
body := map[string]any{}
if lang != "" {
body["language"] = lang
}
if cwd != "" {
body["cwd"] = cwd
}
var r CodeContext
return &r, wrapErr("create context", s.t.JSONAbsolute(ctx, http.MethodPost, s.url(id, "/contexts"), body, &r, nil))
}
func (s *CodeInterpreterService) ListContexts(ctx context.Context, id string) ([]CodeContext, error) {
var r []CodeContext
return r, wrapErr("list contexts", s.t.JSONAbsolute(ctx, http.MethodGet, s.url(id, "/contexts"), nil, &r, nil))
}
func (s *CodeInterpreterService) GetContext(ctx context.Context, id, contextID string) (*CodeContext, error) {
var r CodeContext
return &r, wrapErr("get context", s.t.JSONAbsolute(ctx, http.MethodGet, s.url(id, "/contexts/"+contextID), nil, &r, nil))
}
func (s *CodeInterpreterService) DeleteContext(ctx context.Context, id, contextID string) error {
return wrapErr("delete context", s.t.JSONAbsolute(ctx, http.MethodDelete, s.url(id, "/contexts/"+contextID), nil, nil, nil))
}
func (s *CodeInterpreterService) Run(ctx context.Context, id string, p *RunCodeParams) (*RunCodeResult, error) {
var r RunCodeResult
return &r, wrapErr("run code", s.t.JSONAbsolute(ctx, http.MethodPost, s.url(id, "/execute"), p, &r, nil))
}
func (s *CodeInterpreterService) RunStream(ctx context.Context, id string, p *RunCodeParams) (<-chan StreamEvent, <-chan error, error) {
stream, err := s.t.SSEAbsolute(ctx, http.MethodPost, s.url(id, "/execute/async"), p, nil)
if err != nil {
return nil, nil, wrapErr("run stream", err)
}
events := make(chan StreamEvent)
errs := make(chan error, 1)
go func() {
defer close(events)
defer close(errs)
defer stream.Close()
for {
raw, err := stream.Next()
if err != nil {
if err != io.EOF {
errs <- err
}
return
}
var ev StreamEvent
if err := json.Unmarshal(raw, &ev); err != nil {
errs <- fmt.Errorf("unmarshal event: %w", err)
return
}
select {
case events <- ev:
case <-ctx.Done():
return
}
}
}()
return events, errs, nil
}