-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime.go
More file actions
173 lines (160 loc) · 4.54 KB
/
Copy pathruntime.go
File metadata and controls
173 lines (160 loc) · 4.54 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"os/exec"
"sort"
"strings"
"time"
)
const runtimeDiscoveryTimeout = 2 * time.Second
// runtimeTarget is the small, versioned contract exposed by an external runtime
// broker. The broker owns hardware detection, provisioning, secrets, and the
// model server; code only discovers applicable targets and delegates launches.
type runtimeTarget struct {
SchemaVersion int `json:"schemaVersion"`
Name string `json:"name"`
Label string `json:"label"`
Phase string `json:"phase"`
Reason string `json:"reason"`
Model string `json:"model"`
ContextWindow int `json:"contextWindow"`
Applicable bool `json:"applicable"`
Provisioned bool `json:"provisioned"`
Running bool `json:"running"`
Healthy bool `json:"healthy"`
DiskBytes int64 `json:"diskBytes"`
EstimatedDiskBytes int64 `json:"estimatedDiskBytes"`
}
func loadRuntimeTargets() []runtimeTarget {
broker := strings.TrimSpace(os.Getenv("CODE_RUNTIME_BROKER"))
if broker == "" {
return nil
}
path, err := exec.LookPath(broker)
if err != nil {
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), runtimeDiscoveryTimeout)
defer cancel()
out, err := exec.CommandContext(ctx, path, "runtime", "list", "--json").Output()
if err != nil {
return nil
}
return parseRuntimeTargets(out)
}
func parseRuntimeTargets(data []byte) []runtimeTarget {
var targets []runtimeTarget
if json.Unmarshal(data, &targets) != nil {
return nil
}
out := targets[:0]
for _, target := range targets {
if target.SchemaVersion != 1 || strings.TrimSpace(target.Name) == "" || !target.Applicable {
continue
}
if target.Label == "" {
target.Label = target.Name
}
out = append(out, target)
}
sort.SliceStable(out, func(i, j int) bool { return out[i].Label < out[j].Label })
return out
}
func runtimeFacet(glyph string, targets []runtimeTarget) facet {
values := []string{"hosted"}
for _, target := range targets {
values = append(values, target.Name)
}
return facet{key: "runtime", values: values, glyph: glyph}
}
func (m model) selectedRuntime() (runtimeTarget, bool) {
selected := m.sel["runtime"]
if selected == "" || selected == "hosted" {
return runtimeTarget{}, false
}
for _, target := range m.runtimeTargets {
if target.Name == selected {
return target, true
}
}
return runtimeTarget{}, false
}
func (m model) runtimeValueLabel(value string) string {
if value == "hosted" {
return value
}
for _, target := range m.runtimeTargets {
if target.Name == value {
return target.Label
}
}
return value
}
func runtimeLaunchArgv(path, target, thinking string, forwarded []string, prompt string) []string {
args := []string{"runtime", "run", target, "--", "--thinking", thinking}
args = append(args, stripRuntimeArgs(forwarded)...)
if prompt != "" {
args = append(args, prompt)
}
return append([]string{path}, args...)
}
// stripRuntimeArgs removes caller-supplied routing flags. A local runtime owns
// both its OMP profile and config; arguments after -- remain literal prompt text.
func stripRuntimeArgs(args []string) []string {
clean := make([]string, 0, len(args))
for i := 0; i < len(args); i++ {
arg := args[i]
if arg == "--" {
return append(clean, args[i:]...)
}
if arg == "--profile" || arg == "--config" {
if i+1 < len(args) {
i++
}
continue
}
if strings.HasPrefix(arg, "--profile=") || strings.HasPrefix(arg, "--config=") {
continue
}
clean = append(clean, arg)
}
return clean
}
func runRuntimeTarget(target, thinking, prompt string) int {
path, err := resolveLaunchPath("CODE_RUNTIME_BROKER", nil)
if err != nil {
fmt.Fprintln(os.Stderr, "code: runtime broker not found:", err)
return 1
}
err = runChild(path, runtimeLaunchArgv(path, target, thinking, os.Args[1:], prompt), withoutAuthEnv(os.Environ()))
if err != nil {
fmt.Fprintln(os.Stderr, "code: local runtime:", err)
}
return childStatus(err)
}
func formatBytes(n int64) string {
if n <= 0 {
return ""
}
const gib = int64(1024 * 1024 * 1024)
return fmt.Sprintf("%.0f GiB", float64(n)/float64(gib))
}
func (target runtimeTarget) statusLine() string {
switch {
case target.Healthy:
return "ready"
case target.Running:
return "starting"
case target.Provisioned:
return "installed · starts on launch"
default:
size := formatBytes(target.EstimatedDiskBytes)
if size != "" {
return "downloads on first launch · about " + size
}
return "downloads on first launch"
}
}