122 lines
3.9 KiB
Go
122 lines
3.9 KiB
Go
package acp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
)
|
|
|
|
func fakeGoose(t *testing.T) *httptest.Server {
|
|
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
if r.URL.Path != "/acp" || r.Header.Get("X-Secret-Key") != "s3cret" {
|
|
http.Error(w, "nope", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
c, err := websocket.Accept(w, r, nil)
|
|
if err != nil {
|
|
return
|
|
}
|
|
ctx := context.Background()
|
|
send := func(v any) { b, _ := json.Marshal(v); _ = c.Write(ctx, websocket.MessageText, b) }
|
|
var promptID json.RawMessage
|
|
|
|
for {
|
|
_, data, err := c.Read(ctx)
|
|
if err != nil {
|
|
return
|
|
}
|
|
var m rpcMsg
|
|
_ = json.Unmarshal(data, &m)
|
|
switch m.Method {
|
|
case "initialize":
|
|
send(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": map[string]any{
|
|
"protocolVersion": 1, "agentCapabilities": map[string]any{"loadSession": true}}})
|
|
case "session/new":
|
|
send(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": map[string]any{"sessionId": "sess-1"}})
|
|
case "session/load":
|
|
send(map[string]any{"jsonrpc": "2.0", "method": "session/update", "params": map[string]any{
|
|
"sessionId": "sess-old", "update": map[string]any{"sessionUpdate": "agent_message_chunk",
|
|
"content": map[string]any{"type": "text", "text": "old history"}}}})
|
|
send(map[string]any{"jsonrpc": "2.0", "id": m.ID, "result": nil})
|
|
case "session/prompt":
|
|
promptID = m.ID
|
|
upd := func(u map[string]any) {
|
|
send(map[string]any{"jsonrpc": "2.0", "method": "session/update",
|
|
"params": map[string]any{"sessionId": "sess-1", "update": u}})
|
|
}
|
|
upd(map[string]any{"sessionUpdate": "agent_message_chunk", "content": map[string]any{"type": "text", "text": "Hello "}})
|
|
upd(map[string]any{"sessionUpdate": "tool_call", "toolCallId": "t1", "title": "ls /root", "status": "pending"})
|
|
send(map[string]any{"jsonrpc": "2.0", "id": "perm-1", "method": "session/request_permission",
|
|
"params": map[string]any{"sessionId": "sess-1", "options": []map[string]any{
|
|
{"optionId": "no", "name": "Reject", "kind": "reject_once"},
|
|
{"optionId": "yes", "name": "Allow", "kind": "allow_once"}}}})
|
|
case "":
|
|
var res struct {
|
|
Outcome struct{ Outcome, OptionID string } `json:"outcome"`
|
|
}
|
|
_ = json.Unmarshal(m.Result, &res)
|
|
text := "world"
|
|
if res.Outcome.OptionID != "yes" {
|
|
text = "PERMISSION WRONG"
|
|
}
|
|
send(map[string]any{"jsonrpc": "2.0", "method": "session/update", "params": map[string]any{
|
|
"sessionId": "sess-1", "update": map[string]any{"sessionUpdate": "agent_message_chunk",
|
|
"content": map[string]any{"type": "text", "text": text}}}})
|
|
send(map[string]any{"jsonrpc": "2.0", "id": promptID, "result": map[string]any{"stopReason": "end_turn"}})
|
|
}
|
|
}
|
|
}))
|
|
}
|
|
|
|
func TestACP(t *testing.T) {
|
|
srv := fakeGoose(t)
|
|
defer srv.Close()
|
|
addr := strings.TrimPrefix(srv.URL, "http://")
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
defer cancel()
|
|
|
|
if _, err := DialACP(ctx, addr, "wrong"); err == nil {
|
|
t.Fatal("wrong secret should fail")
|
|
}
|
|
a, err := DialACP(ctx, addr, "s3cret")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer a.Close()
|
|
if !a.CanLoad {
|
|
t.Fatal("loadSession capability not read")
|
|
}
|
|
id, err := a.NewSession(ctx, "/root")
|
|
if err != nil || id != "sess-1" {
|
|
t.Fatalf("new session: %q %v", id, err)
|
|
}
|
|
if err := a.LoadSession(ctx, "sess-old", "/root"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
var mu sync.Mutex
|
|
var text strings.Builder
|
|
var tools []string
|
|
stop, err := a.Prompt(ctx, id, "hi", func(u Update) {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
text.WriteString(u.Text)
|
|
if u.Kind == "tool_call" {
|
|
tools = append(tools, u.Title)
|
|
}
|
|
})
|
|
if err != nil || stop != "end_turn" {
|
|
t.Fatalf("prompt: %q %v", stop, err)
|
|
}
|
|
if text.String() != "Hello world" || len(tools) != 1 || tools[0] != "ls /root" {
|
|
t.Fatalf("got text %q tools %v", text.String(), tools)
|
|
}
|
|
}
|