Files
slacker-bot/slackerbot/internal/testutil/goose.go
T
2026-11-01 15:52:46 -05:00

134 lines
3.1 KiB
Go

package testutil
import (
"context"
"encoding/json"
"errors"
"fmt"
"sync"
"slackerbot/internal/node"
)
// FakeGoose speaks just enough ACP for the bot's turns. Held prompts don't
// finish until they're cancelled, like an agent in the middle of a long turn.
type FakeGoose struct {
mu sync.Mutex
prompts []string
hold bool
sessions int
// Gets each held prompt's text as it starts.
Holding chan string
}
func NewFakeGoose() *FakeGoose { return &FakeGoose{Holding: make(chan string, 8)} }
func (g *FakeGoose) Hold(on bool) {
g.mu.Lock()
defer g.mu.Unlock()
g.hold = on
}
func (g *FakeGoose) Prompts() []string {
g.mu.Lock()
defer g.mu.Unlock()
return append([]string(nil), g.prompts...)
}
func (g *FakeGoose) Conn() node.MsgConn {
return &gooseConn{g: g, out: make(chan []byte, 64), closed: make(chan struct{}), held: map[string]json.RawMessage{}}
}
type gooseConn struct {
g *FakeGoose
out chan []byte
closed chan struct{}
once sync.Once
mu sync.Mutex
held map[string]json.RawMessage
}
func (c *gooseConn) Read(ctx context.Context) ([]byte, error) {
select {
case b := <-c.out:
return b, nil
case <-c.closed:
return nil, errors.New("closed")
case <-ctx.Done():
return nil, ctx.Err()
}
}
func (c *gooseConn) Close() error { c.once.Do(func() { close(c.closed) }); return nil }
func (c *gooseConn) send(v any) {
b, _ := json.Marshal(v)
select {
case c.out <- b:
case <-c.closed:
}
}
func (c *gooseConn) reply(id json.RawMessage, result any) {
c.send(map[string]any{"jsonrpc": "2.0", "id": id, "result": result})
}
func (c *gooseConn) Write(_ context.Context, b []byte) error {
var m struct {
ID json.RawMessage `json:"id"`
Method string `json:"method"`
Params struct {
SessionID string `json:"sessionId"`
Prompt []struct {
Text string `json:"text"`
} `json:"prompt"`
} `json:"params"`
}
if err := json.Unmarshal(b, &m); err != nil {
return err
}
g := c.g
switch m.Method {
case "initialize":
c.reply(m.ID, map[string]any{"protocolVersion": 1, "agentCapabilities": map[string]any{"loadSession": true}})
case "session/new":
g.mu.Lock()
g.sessions++
id := fmt.Sprintf("sess-%d", g.sessions)
g.mu.Unlock()
c.reply(m.ID, map[string]any{"sessionId": id})
case "session/load":
c.reply(m.ID, nil)
case "session/prompt":
text := ""
if len(m.Params.Prompt) > 0 {
text = m.Params.Prompt[0].Text
}
g.mu.Lock()
g.prompts = append(g.prompts, text)
hold := g.hold
g.mu.Unlock()
if hold {
c.mu.Lock()
c.held[m.Params.SessionID] = m.ID
c.mu.Unlock()
g.Holding <- text
return nil
}
c.send(map[string]any{"jsonrpc": "2.0", "method": "session/update", "params": map[string]any{
"sessionId": m.Params.SessionID, "update": map[string]any{"sessionUpdate": "agent_message_chunk",
"content": map[string]any{"type": "text", "text": "done"}}}})
c.reply(m.ID, map[string]any{"stopReason": "end_turn"})
case "session/cancel":
c.mu.Lock()
id, ok := c.held[m.Params.SessionID]
delete(c.held, m.Params.SessionID)
c.mu.Unlock()
if ok {
c.reply(id, map[string]any{"stopReason": "cancelled"})
}
}
return nil
}