bots-garden/mini-mepublic Fork
⑂ 5a47e54
Commits
⬇ Clone ▾
git clone https://git.rickub.com/bots-garden/mini-me.git
git clone ssh://git@rickub.com/bots-garden/mini-me.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP

♻️ Quality: internal/decide without smells (3 → 0)

- Decide as request, post and decode; Validate with validateType
- lines.go: the answers as lines (Lines, answerLine, spread, likeliest,
  MarkDisplay)

Checked: go test, and live old vs new binary with the d1-3B decider on
the host (mm -p on three commands): identical answers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
agent committed 2026-11-28T12:33:02+01:00 Browse files
5a47e54 parent: ac0bb79
modified internal/decide/decide.go +25 -97
@@ -35,7 +35,6 @@ import (
3535 "fmt"
3636 "io"
3737 "net/http"
38- "sort"
3938 "strings"
4039 )
4140
@@ -103,6 +102,12 @@ func (q Question) Validate() error {
103102 if strings.TrimSpace(q.Instructions) == "" {
104103 return fmt.Errorf("question %q: instructions are empty", q.Name)
105104 }
105+ return q.validateType()
106+}
107+
108+// validateType checks what the question's type needs: options for a
109+// choice, levels for a score, nothing for a noul.
110+func (q Question) validateType() error {
106111 switch q.Type {
107112 case Choice:
108113 if len(q.Options) < minOptions {
@@ -173,10 +178,19 @@ func (c Client) Decide(ctx context.Context, state string, questions []Question)
173178 if err != nil {
174179 return Response{}, err
175180 }
176- req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Endpoint(), bytes.NewReader(body))
181+ raw, status, err := c.post(ctx, body)
177182 if err != nil {
178183 return Response{}, err
179184 }
185+ return c.decode(raw, status)
186+}
187+
188+// post sends the request body, and returns the server's body and status.
189+func (c Client) post(ctx context.Context, body []byte) ([]byte, int, error) {
190+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Endpoint(), bytes.NewReader(body))
191+ if err != nil {
192+ return nil, 0, err
193+ }
180194 req.Header.Set("Content-Type", "application/json")
181195 if c.APIKey != "" {
182196 req.Header.Set("Authorization", "Bearer "+c.APIKey)
@@ -187,15 +201,18 @@ func (c Client) Decide(ctx context.Context, state string, questions []Question)
187201 }
188202 resp, err := httpClient.Do(req)
189203 if err != nil {
190- return Response{}, err
204+ return nil, 0, err
191205 }
192206 defer resp.Body.Close()
193207 raw, err := io.ReadAll(resp.Body)
194- if err != nil {
195- return Response{}, err
196- }
197- if resp.StatusCode != http.StatusOK {
198- return Response{}, fmt.Errorf("%s: HTTP %d: %s", c.Endpoint(), resp.StatusCode, serverMessage(raw))
208+ return raw, resp.StatusCode, err
209+}
210+
211+// decode reads the server's answer: its own message on an HTTP error, the
212+// answers otherwise, with the body kept as it came.
213+func (c Client) decode(raw []byte, status int) (Response, error) {
214+ if status != http.StatusOK {
215+ return Response{}, fmt.Errorf("%s: HTTP %d: %s", c.Endpoint(), status, serverMessage(raw))
199216 }
200217 var out Response
201218 if err := json.Unmarshal(raw, &out); err != nil {
@@ -297,92 +314,3 @@ func serverMessage(raw []byte) string {
297314 }
298315 return text
299316 }
300-
301-// Lines renders res for a person: one line per question, in the order they
302-// were asked, then the server's JSON in a fenced block — the window, the
303-// terminal and an editor show the lines, a script asking through /ask can
304-// still parse the block.
305-//
306-// skill: bg-jobs (confidence 0.84) — bg-jobs 0.88 · none 0.11 · lsp-navigation 0.01
307-// destructive: 0.02
308-// urgency: 0.65 → normal (confidence 0.35) — not urgent 0.39 · normal 0.57 · urgent 0.04
309-func Lines(res Response, questions []Question) string {
310- var b strings.Builder
311- for _, q := range questions {
312- a, ok := res.Answers[q.Name]
313- if !ok {
314- fmt.Fprintf(&b, "%s: (no answer)\n", q.Name)
315- continue
316- }
317- b.WriteString(q.Name + ": " + answerLine(q, a) + "\n")
318- }
319- b.WriteString("\n```json\n")
320- b.Write(bytes.TrimSpace(res.Raw))
321- b.WriteString("\n```")
322- return b.String()
323-}
324-
325-func answerLine(q Question, a Answer) string {
326- switch q.Type {
327- case Choice:
328- keys := make([]string, len(q.Options))
329- for i, o := range q.Options {
330- keys[i] = o.Key
331- }
332- return fmt.Sprintf("%s (confidence %.2f) — %s", a.Choice, a.Confidence, spread(keys, keys, a.Probabilities))
333- case Score:
334- indexes := make([]string, len(q.Levels))
335- for i := range q.Levels {
336- indexes[i] = fmt.Sprint(i)
337- }
338- return fmt.Sprintf("%.2f → %s (confidence %.2f) — %s", a.Score, likeliest(indexes, q.Levels, a.Probabilities), a.Confidence, spread(indexes, q.Levels, a.Probabilities))
339- default:
340- return fmt.Sprintf("%.2f", a.Noul)
341- }
342-}
343-
344-// spread lists "label probability" pairs, most probable first.
345-func spread(keys, labels []string, probabilities map[string]float64) string {
346- order := make([]int, len(keys))
347- for i := range order {
348- order[i] = i
349- }
350- sort.SliceStable(order, func(i, j int) bool { return probabilities[keys[order[i]]] > probabilities[keys[order[j]]] })
351- parts := make([]string, len(order))
352- for n, i := range order {
353- parts[n] = fmt.Sprintf("%s %.2f", labels[i], probabilities[keys[i]])
354- }
355- return strings.Join(parts, " · ")
356-}
357-
358-// likeliest is the label of the most probable level.
359-func likeliest(keys, labels []string, probabilities map[string]float64) string {
360- best := 0
361- for i := range keys {
362- if probabilities[keys[i]] > probabilities[keys[best]] {
363- best = i
364- }
365- }
366- return labels[best]
367-}
368-
369-// MarkDisplay returns the server's JSON object with `"display":true` added
370-// at its head — what `-p` prints for a decider whose file sets
371-// `decision.display: true`, so the hook that runs it knows the chat should
372-// show the answer. Everything else is left byte for byte; a payload that is
373-// not an object is returned unchanged.
374-//
375-// decide.MarkDisplay([]byte(`{"model":"d1","answers":{}}`))
376-// // {"display":true,"model":"d1","answers":{}}
377-func MarkDisplay(raw []byte) []byte {
378- trimmed := bytes.TrimSpace(raw)
379- if len(trimmed) < 2 || trimmed[0] != '{' {
380- return raw
381- }
382- rest := bytes.TrimSpace(trimmed[1:])
383- marked := []byte(`{"display":true`)
384- if rest[0] != '}' {
385- marked = append(marked, ',')
386- }
387- return append(marked, rest...)
388-}
@@ -35,7 +35,6 @@ import (
35 "fmt"35 "fmt"
36 "io"36 "io"
37 "net/http"37 "net/http"
38- "sort"
39 "strings"38 "strings"
40 )39 )
41 40
@@ -103,6 +102,12 @@ func (q Question) Validate() error {
103 if strings.TrimSpace(q.Instructions) == "" {102 if strings.TrimSpace(q.Instructions) == "" {
104 return fmt.Errorf("question %q: instructions are empty", q.Name)103 return fmt.Errorf("question %q: instructions are empty", q.Name)
105 }104 }
105+ return q.validateType()
106+}
107+
108+// validateType checks what the question's type needs: options for a
109+// choice, levels for a score, nothing for a noul.
110+func (q Question) validateType() error {
106 switch q.Type {111 switch q.Type {
107 case Choice:112 case Choice:
108 if len(q.Options) < minOptions {113 if len(q.Options) < minOptions {
@@ -173,10 +178,19 @@ func (c Client) Decide(ctx context.Context, state string, questions []Question)
173 if err != nil {178 if err != nil {
174 return Response{}, err179 return Response{}, err
175 }180 }
176- req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Endpoint(), bytes.NewReader(body))181+ raw, status, err := c.post(ctx, body)
177 if err != nil {182 if err != nil {
178 return Response{}, err183 return Response{}, err
179 }184 }
185+ return c.decode(raw, status)
186+}
187+
188+// post sends the request body, and returns the server's body and status.
189+func (c Client) post(ctx context.Context, body []byte) ([]byte, int, error) {
190+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.Endpoint(), bytes.NewReader(body))
191+ if err != nil {
192+ return nil, 0, err
193+ }
180 req.Header.Set("Content-Type", "application/json")194 req.Header.Set("Content-Type", "application/json")
181 if c.APIKey != "" {195 if c.APIKey != "" {
182 req.Header.Set("Authorization", "Bearer "+c.APIKey)196 req.Header.Set("Authorization", "Bearer "+c.APIKey)
@@ -187,15 +201,18 @@ func (c Client) Decide(ctx context.Context, state string, questions []Question)
187 }201 }
188 resp, err := httpClient.Do(req)202 resp, err := httpClient.Do(req)
189 if err != nil {203 if err != nil {
190- return Response{}, err204+ return nil, 0, err
191 }205 }
192 defer resp.Body.Close()206 defer resp.Body.Close()
193 raw, err := io.ReadAll(resp.Body)207 raw, err := io.ReadAll(resp.Body)
194- if err != nil {208+ return raw, resp.StatusCode, err
195- return Response{}, err209+}
196- }210+
197- if resp.StatusCode != http.StatusOK {211+// decode reads the server's answer: its own message on an HTTP error, the
198- return Response{}, fmt.Errorf("%s: HTTP %d: %s", c.Endpoint(), resp.StatusCode, serverMessage(raw))212+// answers otherwise, with the body kept as it came.
213+func (c Client) decode(raw []byte, status int) (Response, error) {
214+ if status != http.StatusOK {
215+ return Response{}, fmt.Errorf("%s: HTTP %d: %s", c.Endpoint(), status, serverMessage(raw))
199 }216 }
200 var out Response217 var out Response
201 if err := json.Unmarshal(raw, &out); err != nil {218 if err := json.Unmarshal(raw, &out); err != nil {
@@ -297,92 +314,3 @@ func serverMessage(raw []byte) string {
297 }314 }
298 return text315 return text
299 }316 }
300-
301-// Lines renders res for a person: one line per question, in the order they
302-// were asked, then the server's JSON in a fenced block — the window, the
303-// terminal and an editor show the lines, a script asking through /ask can
304-// still parse the block.
305-//
306-// skill: bg-jobs (confidence 0.84) — bg-jobs 0.88 · none 0.11 · lsp-navigation 0.01
307-// destructive: 0.02
308-// urgency: 0.65 → normal (confidence 0.35) — not urgent 0.39 · normal 0.57 · urgent 0.04
309-func Lines(res Response, questions []Question) string {
310- var b strings.Builder
311- for _, q := range questions {
312- a, ok := res.Answers[q.Name]
313- if !ok {
314- fmt.Fprintf(&b, "%s: (no answer)\n", q.Name)
315- continue
316- }
317- b.WriteString(q.Name + ": " + answerLine(q, a) + "\n")
318- }
319- b.WriteString("\n```json\n")
320- b.Write(bytes.TrimSpace(res.Raw))
321- b.WriteString("\n```")
322- return b.String()
323-}
324-
325-func answerLine(q Question, a Answer) string {
326- switch q.Type {
327- case Choice:
328- keys := make([]string, len(q.Options))
329- for i, o := range q.Options {
330- keys[i] = o.Key
331- }
332- return fmt.Sprintf("%s (confidence %.2f) — %s", a.Choice, a.Confidence, spread(keys, keys, a.Probabilities))
333- case Score:
334- indexes := make([]string, len(q.Levels))
335- for i := range q.Levels {
336- indexes[i] = fmt.Sprint(i)
337- }
338- return fmt.Sprintf("%.2f → %s (confidence %.2f) — %s", a.Score, likeliest(indexes, q.Levels, a.Probabilities), a.Confidence, spread(indexes, q.Levels, a.Probabilities))
339- default:
340- return fmt.Sprintf("%.2f", a.Noul)
341- }
342-}
343-
344-// spread lists "label probability" pairs, most probable first.
345-func spread(keys, labels []string, probabilities map[string]float64) string {
346- order := make([]int, len(keys))
347- for i := range order {
348- order[i] = i
349- }
350- sort.SliceStable(order, func(i, j int) bool { return probabilities[keys[order[i]]] > probabilities[keys[order[j]]] })
351- parts := make([]string, len(order))
352- for n, i := range order {
353- parts[n] = fmt.Sprintf("%s %.2f", labels[i], probabilities[keys[i]])
354- }
355- return strings.Join(parts, " · ")
356-}
357-
358-// likeliest is the label of the most probable level.
359-func likeliest(keys, labels []string, probabilities map[string]float64) string {
360- best := 0
361- for i := range keys {
362- if probabilities[keys[i]] > probabilities[keys[best]] {
363- best = i
364- }
365- }
366- return labels[best]
367-}
368-
369-// MarkDisplay returns the server's JSON object with `"display":true` added
370-// at its head — what `-p` prints for a decider whose file sets
371-// `decision.display: true`, so the hook that runs it knows the chat should
372-// show the answer. Everything else is left byte for byte; a payload that is
373-// not an object is returned unchanged.
374-//
375-// decide.MarkDisplay([]byte(`{"model":"d1","answers":{}}`))
376-// // {"display":true,"model":"d1","answers":{}}
377-func MarkDisplay(raw []byte) []byte {
378- trimmed := bytes.TrimSpace(raw)
379- if len(trimmed) < 2 || trimmed[0] != '{' {
380- return raw
381- }
382- rest := bytes.TrimSpace(trimmed[1:])
383- marked := []byte(`{"display":true`)
384- if rest[0] != '}' {
385- marked = append(marked, ',')
386- }
387- return append(marked, rest...)
388-}
added internal/decide/lines.go +97 -0
new file mode 100644
@@ -0,0 +1,97 @@
1+package decide
2+
3+import (
4+ "bytes"
5+ "fmt"
6+ "sort"
7+ "strings"
8+)
9+
10+// Lines renders res for a person: one line per question, in the order they
11+// were asked, then the server's JSON in a fenced block — the window, the
12+// terminal and an editor show the lines, a script asking through /ask can
13+// still parse the block.
14+//
15+// skill: bg-jobs (confidence 0.84) — bg-jobs 0.88 · none 0.11 · lsp-navigation 0.01
16+// destructive: 0.02
17+// urgency: 0.65 → normal (confidence 0.35) — not urgent 0.39 · normal 0.57 · urgent 0.04
18+func Lines(res Response, questions []Question) string {
19+ var b strings.Builder
20+ for _, q := range questions {
21+ a, ok := res.Answers[q.Name]
22+ if !ok {
23+ fmt.Fprintf(&b, "%s: (no answer)\n", q.Name)
24+ continue
25+ }
26+ b.WriteString(q.Name + ": " + answerLine(q, a) + "\n")
27+ }
28+ b.WriteString("\n```json\n")
29+ b.Write(bytes.TrimSpace(res.Raw))
30+ b.WriteString("\n```")
31+ return b.String()
32+}
33+
34+func answerLine(q Question, a Answer) string {
35+ switch q.Type {
36+ case Choice:
37+ keys := make([]string, len(q.Options))
38+ for i, o := range q.Options {
39+ keys[i] = o.Key
40+ }
41+ return fmt.Sprintf("%s (confidence %.2f) — %s", a.Choice, a.Confidence, spread(keys, keys, a.Probabilities))
42+ case Score:
43+ indexes := make([]string, len(q.Levels))
44+ for i := range q.Levels {
45+ indexes[i] = fmt.Sprint(i)
46+ }
47+ return fmt.Sprintf("%.2f → %s (confidence %.2f) — %s", a.Score, likeliest(indexes, q.Levels, a.Probabilities), a.Confidence, spread(indexes, q.Levels, a.Probabilities))
48+ default:
49+ return fmt.Sprintf("%.2f", a.Noul)
50+ }
51+}
52+
53+// spread lists "label probability" pairs, most probable first.
54+func spread(keys, labels []string, probabilities map[string]float64) string {
55+ order := make([]int, len(keys))
56+ for i := range order {
57+ order[i] = i
58+ }
59+ sort.SliceStable(order, func(i, j int) bool { return probabilities[keys[order[i]]] > probabilities[keys[order[j]]] })
60+ parts := make([]string, len(order))
61+ for n, i := range order {
62+ parts[n] = fmt.Sprintf("%s %.2f", labels[i], probabilities[keys[i]])
63+ }
64+ return strings.Join(parts, " · ")
65+}
66+
67+// likeliest is the label of the most probable level.
68+func likeliest(keys, labels []string, probabilities map[string]float64) string {
69+ best := 0
70+ for i := range keys {
71+ if probabilities[keys[i]] > probabilities[keys[best]] {
72+ best = i
73+ }
74+ }
75+ return labels[best]
76+}
77+
78+// MarkDisplay returns the server's JSON object with `"display":true` added
79+// at its head — what `-p` prints for a decider whose file sets
80+// `decision.display: true`, so the hook that runs it knows the chat should
81+// show the answer. Everything else is left byte for byte; a payload that is
82+// not an object is returned unchanged.
83+//
84+// decide.MarkDisplay([]byte(`{"model":"d1","answers":{}}`))
85+// // {"display":true,"model":"d1","answers":{}}
86+func MarkDisplay(raw []byte) []byte {
87+ trimmed := bytes.TrimSpace(raw)
88+ if len(trimmed) < 2 || trimmed[0] != '{' {
89+ return raw
90+ }
91+ rest := bytes.TrimSpace(trimmed[1:])
92+ marked := []byte(`{"display":true`)
93+ if rest[0] != '}' {
94+ marked = append(marked, ',')
95+ }
96+ return append(marked, rest...)
97+}
new file mode 100644
@@ -0,0 +1,97 @@
1+package decide
2+
3+import (
4+ "bytes"
5+ "fmt"
6+ "sort"
7+ "strings"
8+)
9+
10+// Lines renders res for a person: one line per question, in the order they
11+// were asked, then the server's JSON in a fenced block — the window, the
12+// terminal and an editor show the lines, a script asking through /ask can
13+// still parse the block.
14+//
15+// skill: bg-jobs (confidence 0.84) — bg-jobs 0.88 · none 0.11 · lsp-navigation 0.01
16+// destructive: 0.02
17+// urgency: 0.65 → normal (confidence 0.35) — not urgent 0.39 · normal 0.57 · urgent 0.04
18+func Lines(res Response, questions []Question) string {
19+ var b strings.Builder
20+ for _, q := range questions {
21+ a, ok := res.Answers[q.Name]
22+ if !ok {
23+ fmt.Fprintf(&b, "%s: (no answer)\n", q.Name)
24+ continue
25+ }
26+ b.WriteString(q.Name + ": " + answerLine(q, a) + "\n")
27+ }
28+ b.WriteString("\n```json\n")
29+ b.Write(bytes.TrimSpace(res.Raw))
30+ b.WriteString("\n```")
31+ return b.String()
32+}
33+
34+func answerLine(q Question, a Answer) string {
35+ switch q.Type {
36+ case Choice:
37+ keys := make([]string, len(q.Options))
38+ for i, o := range q.Options {
39+ keys[i] = o.Key
40+ }
41+ return fmt.Sprintf("%s (confidence %.2f) — %s", a.Choice, a.Confidence, spread(keys, keys, a.Probabilities))
42+ case Score:
43+ indexes := make([]string, len(q.Levels))
44+ for i := range q.Levels {
45+ indexes[i] = fmt.Sprint(i)
46+ }
47+ return fmt.Sprintf("%.2f → %s (confidence %.2f) — %s", a.Score, likeliest(indexes, q.Levels, a.Probabilities), a.Confidence, spread(indexes, q.Levels, a.Probabilities))
48+ default:
49+ return fmt.Sprintf("%.2f", a.Noul)
50+ }
51+}
52+
53+// spread lists "label probability" pairs, most probable first.
54+func spread(keys, labels []string, probabilities map[string]float64) string {
55+ order := make([]int, len(keys))
56+ for i := range order {
57+ order[i] = i
58+ }
59+ sort.SliceStable(order, func(i, j int) bool { return probabilities[keys[order[i]]] > probabilities[keys[order[j]]] })
60+ parts := make([]string, len(order))
61+ for n, i := range order {
62+ parts[n] = fmt.Sprintf("%s %.2f", labels[i], probabilities[keys[i]])
63+ }
64+ return strings.Join(parts, " · ")
65+}
66+
67+// likeliest is the label of the most probable level.
68+func likeliest(keys, labels []string, probabilities map[string]float64) string {
69+ best := 0
70+ for i := range keys {
71+ if probabilities[keys[i]] > probabilities[keys[best]] {
72+ best = i
73+ }
74+ }
75+ return labels[best]
76+}
77+
78+// MarkDisplay returns the server's JSON object with `"display":true` added
79+// at its head — what `-p` prints for a decider whose file sets
80+// `decision.display: true`, so the hook that runs it knows the chat should
81+// show the answer. Everything else is left byte for byte; a payload that is
82+// not an object is returned unchanged.
83+//
84+// decide.MarkDisplay([]byte(`{"model":"d1","answers":{}}`))
85+// // {"display":true,"model":"d1","answers":{}}
86+func MarkDisplay(raw []byte) []byte {
87+ trimmed := bytes.TrimSpace(raw)
88+ if len(trimmed) < 2 || trimmed[0] != '{' {
89+ return raw
90+ }
91+ rest := bytes.TrimSpace(trimmed[1:])
92+ marked := []byte(`{"display":true`)
93+ if rest[0] != '}' {
94+ marked = append(marked, ',')
95+ }
96+ return append(marked, rest...)
97+}