-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprobe_local.go
More file actions
106 lines (94 loc) · 3.1 KB
/
Copy pathprobe_local.go
File metadata and controls
106 lines (94 loc) · 3.1 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
package cmd
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/5uck1ess/devkit/runners"
"github.com/spf13/cobra"
)
var errProbeFailed = errors.New("probe failed")
func formatHuman(r runners.ProbeResult) string {
var b strings.Builder
fmt.Fprintf(&b, "endpoint: %s\n", r.Endpoint)
fmt.Fprintf(&b, "model: %s\n", r.Model)
if !r.Enabled {
fmt.Fprintln(&b, "enabled: no — disabled (set DEVKIT_LOCAL_ENABLED=1 to enable)")
return b.String()
}
fmt.Fprintln(&b, "enabled: yes")
switch r.Status {
case runners.ProbeHealthy:
fmt.Fprintf(&b, "reachable: yes (HTTP %d, %dms)\n", r.HTTPStatus, r.LatencyMS)
fmt.Fprintf(&b, "models seen: %s\n", strings.Join(r.ModelsSeen, ", "))
fmt.Fprintln(&b, "model match: OK (configured model present in /models)")
case runners.ProbeModelMissing:
fmt.Fprintf(&b, "reachable: yes (HTTP %d, %dms)\n", r.HTTPStatus, r.LatencyMS)
if len(r.ModelsSeen) > 0 {
fmt.Fprintf(&b, "models seen: %s\n", strings.Join(r.ModelsSeen, ", "))
} else {
fmt.Fprintln(&b, "models seen: (none returned)")
}
fmt.Fprintln(&b, "model match: MISSING")
if r.Hint != "" {
fmt.Fprintf(&b, "hint: %s\n", r.Hint)
}
default:
if r.HTTPStatus > 0 {
fmt.Fprintf(&b, "reachable: NO (HTTP %d in %dms)\n", r.HTTPStatus, r.LatencyMS)
} else {
fmt.Fprintf(&b, "reachable: NO (%dms)\n", r.LatencyMS)
}
if r.Hint != "" {
fmt.Fprintf(&b, "hint: %s\n", r.Hint)
}
if r.ErrorMsg != "" {
fmt.Fprintf(&b, "body: %s\n", r.ErrorMsg)
}
}
return b.String()
}
func formatJSON(r runners.ProbeResult) ([]byte, error) {
return json.MarshalIndent(r, "", " ")
}
var probeLocalJSON bool
var probeLocalCmd = &cobra.Command{
Use: "probe-local",
Short: "Probe the configured local inference endpoint",
Long: `Probe the OpenAI-compatible endpoint configured via DEVKIT_LOCAL_* env vars.
Reports endpoint, model, reachability, and whether the configured model is
present in the server's /v1/models response. Exit 0 on healthy or when the
local runner is disabled (DEVKIT_LOCAL_ENABLED != 1); exit 1 otherwise.`,
// Override root's PersistentPreRunE — this probe doesn't need a git repo or DB.
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { return nil },
SilenceUsage: true,
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
cfg := runners.ProbeConfig{
Enabled: runners.LocalEnabled(),
Endpoint: runners.LocalEndpoint(),
Model: runners.LocalModel(),
APIKey: runners.LocalAPIKey(),
Timeout: 3 * time.Second,
}
result := runners.Probe(cmd.Context(), cfg)
if probeLocalJSON {
out, err := formatJSON(result)
if err != nil {
return fmt.Errorf("formatting JSON: %w", err)
}
fmt.Println(string(out))
} else {
fmt.Print(formatHuman(result))
}
if result.Status == runners.ProbeDisabled || result.Status == runners.ProbeHealthy {
return nil
}
return errProbeFailed
},
}
func init() {
probeLocalCmd.Flags().BoolVar(&probeLocalJSON, "json", false, "emit structured JSON instead of human-readable text")
rootCmd.AddCommand(probeLocalCmd)
}