30c1ed442e
The installed OpenCode 2.0.20 exposes the authenticated SSE event firehose at
/api/event and the /api HTTP surface, but Detect selects backend "v2" and Open
returned no owned process/URL, so chat stayed disabled and every chat_* request
was answered with "chat is not configured on this server".
Add opencode.OpenStream/OpenChat: for either backend they start the single owned
opencode serve process, discover the loopback URL and verify the backend health
contract (/api/info for v2, /global/health for v1). main and the live test share
OpenChat so the selection cannot drift. Chat now streams real incremental
session.text.delta/session.reasoning.delta frames via the existing
Connection.EventStream + NormalizeChatEvent, with no polling and no fake
streaming.
- Authenticate the owned endpoint with the per-backend user (opencode for v2,
pipelines for v1); the v2 server ignores OPENCODE_SERVER_USERNAME.
- Send the V2 prompt shape {"text":...}, which the v1 translation also accepts.
- Skip firehose event types the adapter does not surface before decoding their
data, so session.instructions.updated (delta is an object) no longer fails the
stream.
Unit tests cover the per-backend user, the prompt shape/auth, the captured real
V2 event shapes and the selection guards. An opt-in live test drives
chat_sessions/chat_messages/a completed chat_prompt/an interrupted chat_prompt
against the installed OpenCode. Update the packaged operations reference with the
realized capability, dated evidence and deployment state.
Checks before commit: the packaged authoring reference
internal/author/skill/references/operations.md is updated in this change; no new
reference file was added, so internal/author/install.go's managed inventory is
unchanged and the internal/author tests pass. No packaged template describes the
brokered chat surface, and neither README.md nor the topic docs under docs/ (nor
web/) document it, so none of them needed updating.
251 lines
9.1 KiB
Go
251 lines
9.1 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/coder/websocket"
|
|
"github.com/coder/websocket/wsjson"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/opencode"
|
|
"git.a-wan.net/alan/pipelines/internal/pipeline"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// TestChatLiveEndToEnd drives the real brokered chat path against the installed
|
|
// OpenCode: it opens the server's single owned endpoint, creates a scratch
|
|
// session, and pushes chat_sessions, chat_messages, a completed chat_prompt and
|
|
// an interrupted chat_prompt through a real /connect websocket. It is opt-in
|
|
// (real inference) and never touches the deployed server, ntfy or any lane; it
|
|
// runs on an in-process handler and a t.TempDir workspace. The model defaults to
|
|
// the pipeline's own and is overridable with
|
|
// PIPELINES_TEST_OPENCODE_CHAT_MODEL=provider/model.
|
|
func TestChatLiveEndToEnd(t *testing.T) {
|
|
if os.Getenv("PIPELINES_TEST_OPENCODE_CHAT_LIVE") != "1" {
|
|
t.Skip("set PIPELINES_TEST_OPENCODE_CHAT_LIVE=1 to run the live OpenCode chat end-to-end check")
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
|
defer cancel()
|
|
dir := t.TempDir()
|
|
conn, err := opencode.OpenChat(ctx, opencode.Executable(), dir)
|
|
if err != nil {
|
|
t.Fatalf("OpenChat: %v", err)
|
|
}
|
|
defer conn.Close()
|
|
t.Logf("owned OpenCode endpoint %s (backend %s)", conn.URL, conn.Backend)
|
|
|
|
provider, model := "opencode-go", "deepseek-v4.1-flash"
|
|
if override := os.Getenv("PIPELINES_TEST_OPENCODE_CHAT_MODEL"); override != "" {
|
|
if parts := strings.SplitN(override, "/", 2); len(parts) == 2 {
|
|
provider, model = parts[0], parts[1]
|
|
}
|
|
}
|
|
created, err := conn.Call(ctx, "post", "/api/session", map[string]any{
|
|
"title": "pipelines chat live check",
|
|
"location": map[string]string{"directory": dir},
|
|
"model": map[string]string{"providerID": provider, "id": model},
|
|
"permissions": []map[string]string{{"action": "*", "resource": "*", "effect": "deny"}},
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("create session: %v", err)
|
|
}
|
|
var session struct {
|
|
Data struct {
|
|
ID string `json:"id"`
|
|
} `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(created, &session); err != nil || session.Data.ID == "" {
|
|
t.Fatalf("created session response %s (err %v)", created, err)
|
|
}
|
|
sid := session.Data.ID
|
|
defer conn.Call(context.Background(), "delete", "/api/session/"+sid, nil)
|
|
t.Logf("scratch session %s on %s/%s", sid, provider, model)
|
|
|
|
options := Options{
|
|
Chat: opencode.NewChat(conn),
|
|
OwnedSessions: func(context.Context) ([]pipeline.SessionOwner, error) {
|
|
return []pipeline.SessionOwner{{ID: sid, Directory: dir}}, nil
|
|
},
|
|
}
|
|
srv := httptest.NewServer(HandlerWithOptions(ctx, options))
|
|
defer srv.Close()
|
|
ws, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect", nil)
|
|
if err != nil {
|
|
t.Fatalf("dial: %v", err)
|
|
}
|
|
defer ws.CloseNow()
|
|
|
|
// chat_sessions must list exactly the owned scratch session.
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatSessions, ID: "s1"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, resp := readChatResponse(t, ctx, ws)
|
|
if resp.Type != protocol.ChatSessions || resp.ID != "s1" {
|
|
t.Fatalf("chat_sessions response = %+v", resp)
|
|
}
|
|
if len(resp.ChatSessions) != 1 || resp.ChatSessions[0].ID != sid {
|
|
t.Fatalf("chat_sessions = %+v, want the owned session %s", resp.ChatSessions, sid)
|
|
}
|
|
t.Logf("chat_sessions listed %d owned session(s): %+v", len(resp.ChatSessions), resp.ChatSessions)
|
|
|
|
// chat_messages resumes retained/readback history without error.
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatMessages, ID: "m1", ChatQuery: &protocol.ChatQuery{SessionID: sid}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
_, resp = readChatResponse(t, ctx, ws)
|
|
if resp.Type != protocol.ChatMessages {
|
|
t.Fatalf("chat_messages response = %+v", resp)
|
|
}
|
|
t.Logf("chat_messages returned %d turn(s)", len(resp.ChatMessages))
|
|
|
|
// A completed turn: the ack echoes the request id and the reply arrives as
|
|
// chat_event frames with stable session/message ids.
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatPrompt, ID: "p1", ChatPrompt: &protocol.ChatPromptRequest{SessionID: sid, Text: "Reply with exactly one word: pong"}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
frames, ack := readChatResponse(t, ctx, ws)
|
|
if ack.Type != protocol.ChatPrompt || ack.ID != "p1" {
|
|
t.Fatalf("prompt ack = %+v, want the echoed request id p1", ack)
|
|
}
|
|
completed := collectUntilStepEnded(t, ctx, ws, frames)
|
|
assertLiveFrames(t, sid, completed)
|
|
t.Logf("completed turn: %d chat_event frame(s) [%s]; first text delta %q", len(completed), frameKinds(completed), firstDelta(completed))
|
|
|
|
// An interrupted turn: post a long prompt, wait for its ack, then answer it
|
|
// with chat_interrupt and confirm the ack.
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatPrompt, ID: "p2", ChatPrompt: &protocol.ChatPromptRequest{SessionID: sid, Text: "Write a 400 line epic poem about the sea. Output every line as it comes."}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, ack = readChatResponse(t, ctx, ws); ack.Type != protocol.ChatPrompt || ack.ID != "p2" {
|
|
t.Fatalf("second prompt ack = %+v", ack)
|
|
}
|
|
if err := wsjson.Write(ctx, ws, protocol.Message{Type: protocol.ChatInterrupt, ID: "i1", ChatQuery: &protocol.ChatQuery{SessionID: sid}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for {
|
|
message := readChatMessage(t, ctx, ws)
|
|
if message.Type == protocol.ChatInterrupt {
|
|
if message.ID != "i1" {
|
|
t.Fatalf("interrupt ack = %+v, want i1", message)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
// The interrupt must actually end the running turn: the session leaves the
|
|
// V2 active set. This is the observable proof the ack alone is not.
|
|
deadline := time.Now().Add(30 * time.Second)
|
|
for {
|
|
active, err := conn.Call(ctx, "get", "/api/session/active", nil)
|
|
if err != nil {
|
|
t.Fatalf("active check: %v", err)
|
|
}
|
|
var state struct {
|
|
Data map[string]json.RawMessage `json:"data"`
|
|
}
|
|
if err := json.Unmarshal(active, &state); err != nil {
|
|
t.Fatalf("active response %s (err %v)", active, err)
|
|
}
|
|
if _, running := state.Data[sid]; !running {
|
|
break
|
|
}
|
|
if time.Now().After(deadline) {
|
|
t.Fatalf("session %s is still active after chat_interrupt", sid)
|
|
}
|
|
time.Sleep(500 * time.Millisecond)
|
|
}
|
|
t.Logf("chat_interrupt acked and the interrupted turn left the active set")
|
|
}
|
|
|
|
// readChatResponse reads until a non-event response message arrives, returning
|
|
// that message and any interleaved chat_event frames.
|
|
func readChatResponse(t *testing.T, ctx context.Context, ws *websocket.Conn) ([]protocol.ChatEventFrame, protocol.Message) {
|
|
t.Helper()
|
|
var frames []protocol.ChatEventFrame
|
|
for {
|
|
message := readChatMessage(t, ctx, ws)
|
|
if message.Type == protocol.ChatEvent && message.ChatEvent != nil {
|
|
frames = append(frames, *message.ChatEvent)
|
|
continue
|
|
}
|
|
return frames, message
|
|
}
|
|
}
|
|
|
|
// readChatMessage reads one frame and fails on a transport error or a server
|
|
// error frame, so the test never silently accepts a failure.
|
|
func readChatMessage(t *testing.T, ctx context.Context, ws *websocket.Conn) protocol.Message {
|
|
t.Helper()
|
|
var message protocol.Message
|
|
if err := wsjson.Read(ctx, ws, &message); err != nil {
|
|
t.Fatalf("read: %v", err)
|
|
}
|
|
if message.Type == protocol.Error {
|
|
t.Fatalf("server error frame: %s", message.Error)
|
|
}
|
|
return message
|
|
}
|
|
|
|
// collectUntilStepEnded keeps reading event frames until the turn's step ends.
|
|
func collectUntilStepEnded(t *testing.T, ctx context.Context, ws *websocket.Conn, frames []protocol.ChatEventFrame) []protocol.ChatEventFrame {
|
|
t.Helper()
|
|
for {
|
|
message := readChatMessage(t, ctx, ws)
|
|
if message.Type != protocol.ChatEvent || message.ChatEvent == nil {
|
|
continue
|
|
}
|
|
frames = append(frames, *message.ChatEvent)
|
|
if last := frames[len(frames)-1]; last.Kind == protocol.ChatEventStep && last.Step != nil && last.Step.Status == protocol.ChatStepEnded {
|
|
return frames
|
|
}
|
|
}
|
|
}
|
|
|
|
// assertLiveFrames fails unless every frame carries the session id and a stable
|
|
// message id, and the turn produced at least one text frame.
|
|
func assertLiveFrames(t *testing.T, sid string, frames []protocol.ChatEventFrame) {
|
|
t.Helper()
|
|
sawText := false
|
|
for _, frame := range frames {
|
|
if frame.SessionID != sid {
|
|
t.Fatalf("frame session %q, want %s: %+v", frame.SessionID, sid, frame)
|
|
}
|
|
if frame.MessageID == "" {
|
|
t.Fatalf("frame missing a stable message id: %+v", frame)
|
|
}
|
|
if frame.Kind == protocol.ChatEventText {
|
|
sawText = true
|
|
}
|
|
}
|
|
if !sawText {
|
|
t.Fatalf("the completed turn produced no text frame: %+v", frames)
|
|
}
|
|
}
|
|
|
|
func firstDelta(frames []protocol.ChatEventFrame) string {
|
|
for _, frame := range frames {
|
|
if frame.Kind == protocol.ChatEventText && frame.Delta != "" {
|
|
return frame.Delta
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// frameKinds summarises the frame sequence (step frames carry their status) so
|
|
// the live run's observed output is legible evidence.
|
|
func frameKinds(frames []protocol.ChatEventFrame) string {
|
|
kinds := make([]string, 0, len(frames))
|
|
for _, frame := range frames {
|
|
if frame.Kind == protocol.ChatEventStep && frame.Step != nil {
|
|
kinds = append(kinds, frame.Kind+":"+frame.Step.Status)
|
|
continue
|
|
}
|
|
kinds = append(kinds, frame.Kind)
|
|
}
|
|
return strings.Join(kinds, ",")
|
|
}
|