This repository was archived by the owner on May 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopencode_backend.go
More file actions
76 lines (70 loc) · 2.03 KB
/
Copy pathopencode_backend.go
File metadata and controls
76 lines (70 loc) · 2.03 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
package agentbridge
import (
"context"
"strings"
coreopencode "github.com/Alice-space/agentbridge/providers/opencode"
)
type opencodeBackend struct {
runner coreopencode.Runner
profileRunners map[string]coreopencode.Runner
}
func newOpenCodeBackend(cfg OpenCodeConfig) *opencodeBackend {
defaultRunner := coreopencode.Runner{
Command: cfg.Command,
Timeout: cfg.Timeout,
DefaultModel: cfg.Model,
DefaultVariant: cfg.Variant,
Env: cfg.Env,
WorkspaceDir: cfg.WorkspaceDir,
}
profileRunners := make(map[string]coreopencode.Runner, len(cfg.ProfileOverrides))
for name, override := range cfg.ProfileOverrides {
r := defaultRunner
if strings.TrimSpace(override.Command) != "" {
r.Command = strings.TrimSpace(override.Command)
}
if override.Timeout > 0 {
r.Timeout = override.Timeout
}
profileRunners[name] = r
}
return &opencodeBackend{runner: defaultRunner, profileRunners: profileRunners}
}
func (b *opencodeBackend) Run(ctx context.Context, req RunRequest) (RunResult, error) {
runner := b.runner
if profile := strings.TrimSpace(req.Profile); profile != "" {
if r, ok := b.profileRunners[profile]; ok {
runner = r
}
}
if strings.TrimSpace(req.WorkspaceDir) != "" {
runner.WorkspaceDir = strings.TrimSpace(req.WorkspaceDir)
}
var rawEventFn func(kind, line, detail string)
if req.OnRawEvent != nil {
fn := req.OnRawEvent
rawEventFn = func(kind, line, detail string) {
fn(RawEvent{Kind: kind, Line: line, Detail: detail})
}
}
reply, nextThreadID, inputTokens, cachedInputTokens, outputTokens, err := runner.RunWithThreadAndProgress(
ctx,
strings.TrimSpace(req.ThreadID),
req.UserText,
strings.TrimSpace(req.Model),
strings.TrimSpace(req.Variant),
req.Env,
req.OnProgress,
rawEventFn,
)
return RunResult{
Reply: reply,
NextThreadID: strings.TrimSpace(nextThreadID),
Usage: Usage{
InputTokens: inputTokens,
CachedInputTokens: cachedInputTokens,
OutputTokens: outputTokens,
},
}, err
}
var _ Backend = (*opencodeBackend)(nil)