Serve the Pipelines chat tools over a loopback MCP endpoint and register it with Claude Code, Codex and OpenCode chats
This commit is contained in:
@@ -0,0 +1,100 @@
|
||||
package codex
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/chattools"
|
||||
)
|
||||
|
||||
type toolRegistry struct {
|
||||
mu sync.Mutex
|
||||
bound []string
|
||||
}
|
||||
|
||||
func (r *toolRegistry) SessionURL(id string) (string, error) {
|
||||
return "http://127.0.0.1:9/mcp/chat/session-" + id, nil
|
||||
}
|
||||
|
||||
func (r *toolRegistry) PendingURL() (string, func(string) error, error) {
|
||||
return "http://127.0.0.1:9/mcp/chat/pending", func(id string) error {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
r.bound = append(r.bound, id)
|
||||
return nil
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *toolRegistry) InstanceURL(string) (string, error) { return "", errors.New("unused") }
|
||||
|
||||
func mcpURL(t *testing.T, raw json.RawMessage) string {
|
||||
t.Helper()
|
||||
var params struct {
|
||||
Config struct {
|
||||
MCPServers map[string]struct {
|
||||
URL string `json:"url"`
|
||||
} `json:"mcp_servers"`
|
||||
} `json:"config"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, ¶ms); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return params.Config.MCPServers["pipelines"].URL
|
||||
}
|
||||
|
||||
func TestThreadStartRegistersAndBindsThePipelinesTools(t *testing.T) {
|
||||
registry := &toolRegistry{}
|
||||
chattools.Configure(registry)
|
||||
t.Cleanup(func() { chattools.Configure(nil) })
|
||||
var urls []string
|
||||
chat := modelTestChat(t, func(method string, raw json.RawMessage) any {
|
||||
switch method {
|
||||
case "thread/start":
|
||||
urls = append(urls, mcpURL(t, raw))
|
||||
return json.RawMessage(`{"thread":{"id":"thr-1","cwd":"/w"},"model":"gpt","modelProvider":"openai"}`)
|
||||
case "thread/resume":
|
||||
urls = append(urls, mcpURL(t, raw))
|
||||
return json.RawMessage(`{"thread":{"id":"thr-1"}}`)
|
||||
}
|
||||
return json.RawMessage(`{}`)
|
||||
})
|
||||
if _, err := chat.Create(context.Background(), "", "/w"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := chat.resumeThread(context.Background(), map[string]any{"threadId": "thr-1"}, "thr-1"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(urls) != 2 || urls[0] != "http://127.0.0.1:9/mcp/chat/pending" || urls[1] != "http://127.0.0.1:9/mcp/chat/session-thr-1" {
|
||||
t.Fatalf("registered urls = %v", urls)
|
||||
}
|
||||
if len(registry.bound) != 1 || registry.bound[0] != "thr-1" {
|
||||
t.Fatalf("the pending endpoint must be bound to the new thread: %v", registry.bound)
|
||||
}
|
||||
}
|
||||
|
||||
func TestThreadStartWithoutToolsWhenTheAppServerRefusesThem(t *testing.T) {
|
||||
chattools.Configure(&toolRegistry{})
|
||||
t.Cleanup(func() { chattools.Configure(nil) })
|
||||
var attempts []string
|
||||
chat := modelTestChat(t, func(method string, raw json.RawMessage) any {
|
||||
if method != "thread/start" {
|
||||
return json.RawMessage(`{}`)
|
||||
}
|
||||
url := mcpURL(t, raw)
|
||||
attempts = append(attempts, url)
|
||||
if url != "" {
|
||||
return &rpcError{Code: -32602, Message: "invalid config: mcp_servers"}
|
||||
}
|
||||
return json.RawMessage(`{"thread":{"id":"thr-2","cwd":"/w"}}`)
|
||||
})
|
||||
session, err := chat.Create(context.Background(), "", "/w")
|
||||
if err != nil || session.ID != "thr-2" {
|
||||
t.Fatalf("the chat must still start: %v %v", session, err)
|
||||
}
|
||||
if len(attempts) != 2 || attempts[0] == "" || attempts[1] != "" {
|
||||
t.Fatalf("attempts = %q", attempts)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user