diff --git a/internal/opencode/chat.go b/internal/opencode/chat.go index dd0cc386..75a89f2c 100644 --- a/internal/opencode/chat.go +++ b/internal/opencode/chat.go @@ -290,26 +290,98 @@ func stepIndex(data chatEvent) int { return data.Index } -// decodeChatMessages maps the native /message response onto the protocol chat -// history shape. Assistant content parts are text, reasoning and tool; a user -// turn is the prompt text. It never re-derives provider shapes. +// nativeChatMessage is one stored turn. The two surfaces return different +// envelopes for the same session and both decode into this one struct, so the +// mapping below reads whichever fields a record populated: +// +// - the V2 HTTP API (GET /api/session/{id}/message) puts id/type/agent/model, +// the timing and the parts array at the message top level, with the parts +// under "content" and a user turn's text as a bare string; +// - the native serve readback nests the same fields under "info" (role as +// "role", model as "modelID", timing as "created") with the parts under +// "parts". type nativeChatMessage struct { - Info struct { + ID string `json:"id"` + Type string `json:"type"` + Agent string `json:"agent"` + Model nativeChatModel `json:"model"` + Time *protocol.ChatTime `json:"time"` + Text string `json:"text"` + Content []nativeChatPart `json:"content"` + Info struct { ID string `json:"id"` Role string `json:"role"` ModelID string `json:"modelID"` Agent string `json:"agent"` CreatedAt int64 `json:"created"` } `json:"info"` - Parts []struct { - Type string `json:"type"` - ID string `json:"id"` - Text string `json:"text"` - Name string `json:"name"` - Executed bool `json:"executed"` - State *protocol.ChatToolState `json:"state"` - Time *protocol.ChatTime `json:"time"` - } `json:"parts"` + Parts []nativeChatPart `json:"parts"` +} + +// nativeChatModel is the nested Model.Ref a V2 turn carries ({id, providerID, +// variant}); only the model id is surfaced in the protocol message. +type nativeChatModel struct { + ID string `json:"id"` +} + +// nativeChatPart is one content element shared by both message envelopes. +type nativeChatPart struct { + Type string `json:"type"` + ID string `json:"id"` + Text string `json:"text"` + Name string `json:"name"` + Executed bool `json:"executed"` + State *protocol.ChatToolState `json:"state"` + Time *protocol.ChatTime `json:"time"` +} + +// role, parts and the accessor methods below read the V2 top-level fields first +// and fall back to the native serve "info"/"parts" fields, so one record maps +// regardless of which surface produced it. + +func (m nativeChatMessage) role() string { + if m.Info.Role != "" { + return m.Info.Role + } + return m.Type +} + +func (m nativeChatMessage) messageID() string { + if m.Info.ID != "" { + return m.Info.ID + } + return m.ID +} + +func (m nativeChatMessage) modelID() string { + if m.Info.ModelID != "" { + return m.Info.ModelID + } + return m.Model.ID +} + +func (m nativeChatMessage) agent() string { + if m.Info.Agent != "" { + return m.Info.Agent + } + return m.Agent +} + +func (m nativeChatMessage) createdAt() int64 { + if m.Info.CreatedAt != 0 { + return m.Info.CreatedAt + } + if m.Time != nil { + return m.Time.Created + } + return 0 +} + +func (m nativeChatMessage) contentParts() []nativeChatPart { + if m.Parts != nil { + return m.Parts + } + return m.Content } // chatMessageRecords accepts both message envelopes the two surfaces return: @@ -335,13 +407,13 @@ func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage, return nil, err } messages := make([]protocol.ChatMessage, 0, len(records)) - for _, message := range records { - role := message.Info.Role + for _, record := range records { + role := record.role() if role != protocol.ChatRoleUser && role != protocol.ChatRoleAssistant { continue } - out := protocol.ChatMessage{ID: message.Info.ID, SessionID: sessionID, Role: role, Model: message.Info.ModelID, Agent: message.Info.Agent, CreatedAt: message.Info.CreatedAt} - for _, part := range message.Parts { + out := protocol.ChatMessage{ID: record.messageID(), SessionID: sessionID, Role: role, Model: record.modelID(), Agent: record.agent(), CreatedAt: record.createdAt()} + for _, part := range record.contentParts() { switch part.Type { case protocol.ChatPartText, protocol.ChatPartReasoning: out.Parts = append(out.Parts, protocol.ChatPart{Type: part.Type, ID: part.ID, Text: part.Text}) @@ -349,6 +421,11 @@ func decodeChatMessages(sessionID string, data []byte) ([]protocol.ChatMessage, out.Parts = append(out.Parts, protocol.ChatPart{Type: protocol.ChatPartTool, ID: part.ID, Name: part.Name, Executed: part.Executed, State: part.State, Time: part.Time}) } } + // A V2 user turn carries its prompt text as a message-level string rather + // than a content array, so surface it as the turn's single text part. + if len(out.Parts) == 0 && role == protocol.ChatRoleUser && record.Text != "" { + out.Parts = append(out.Parts, protocol.ChatPart{Type: protocol.ChatPartText, Text: record.Text}) + } messages = append(messages, out) } return messages, nil diff --git a/internal/opencode/chat_test.go b/internal/opencode/chat_test.go index ac78a477..fca96c4d 100644 --- a/internal/opencode/chat_test.go +++ b/internal/opencode/chat_test.go @@ -165,18 +165,135 @@ func TestChatRejectsInvalidSessionWithoutCalling(t *testing.T) { } } +// TestDecodeChatMessages loads a seeded user + assistant session in both +// response envelopes the two surfaces return and asserts the turns come back +// with role, id, part order, part ids and the completed tool's input, content +// and status intact. The V2 HTTP API (GET /api/session/{id}/message) wraps the +// messages in {"data":[...]} with the fields at the message top level and the +// parts under "content"; the native serve readback returns a bare array with the +// fields under "info" and the parts under "parts". func TestDecodeChatMessages(t *testing.T) { - raw := []byte(`{"data":[{"info":{"id":"msg_1","role":"assistant","modelID":"m","agent":"build","created":10},"parts":[{"type":"text","id":"p1","text":"hi"},{"type":"reasoning","id":"p2","text":"hmm"},{"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}]},"time":{"created":1,"ran":2,"completed":3}}]}]}`) - messages, err := decodeChatMessages("ses_1", raw) + cases := []struct { + name string + raw string + wantUserPartID string + wantUserPartTxt string + wantTextPartID string + wantReasonPartID string + }{ + { + name: "v2 data envelope", + raw: `{"data":[ + {"id":"msg_user","time":{"created":10},"type":"user","text":"list the files"}, + {"id":"msg_assistant","time":{"created":20,"streamed":25,"completed":30},"type":"assistant","agent":"build","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"},"content":[ + {"type":"text","text":"here you go"}, + {"type":"reasoning","text":"thinking"}, + {"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}],"metadata":{"exit":0}},"time":{"created":1,"ran":2,"completed":3}} + ]} + ],"cursor":{"next":null}}`, + wantUserPartID: "", + wantUserPartTxt: "list the files", + wantTextPartID: "", + wantReasonPartID: "", + }, + { + name: "v1 bare array", + raw: `[ + {"info":{"id":"msg_user","role":"user","created":10},"parts":[{"type":"text","id":"p0","text":"list the files"}]}, + {"info":{"id":"msg_assistant","role":"assistant","modelID":"deepseek-v4.1-flash","agent":"build","created":20},"parts":[ + {"type":"text","id":"p1","text":"here you go"}, + {"type":"reasoning","id":"p2","text":"thinking"}, + {"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}],"metadata":{"exit":0}},"time":{"created":1,"ran":2,"completed":3}} + ]} + ]`, + wantUserPartID: "p0", + wantUserPartTxt: "list the files", + wantTextPartID: "p1", + wantReasonPartID: "p2", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + messages, err := decodeChatMessages("ses_1", []byte(tc.raw)) + if err != nil { + t.Fatalf("decode: %v", err) + } + if len(messages) != 2 { + t.Fatalf("got %d messages, want user+assistant: %+v", len(messages), messages) + } + user, assistant := messages[0], messages[1] + if user.ID != "msg_user" || user.Role != protocol.ChatRoleUser || user.SessionID != "ses_1" { + t.Fatalf("user turn = %+v", user) + } + if len(user.Parts) != 1 || user.Parts[0].Type != protocol.ChatPartText || user.Parts[0].ID != tc.wantUserPartID || user.Parts[0].Text != tc.wantUserPartTxt { + t.Fatalf("user parts = %+v", user.Parts) + } + if assistant.ID != "msg_assistant" || assistant.Role != protocol.ChatRoleAssistant || assistant.SessionID != "ses_1" { + t.Fatalf("assistant turn = %+v", assistant) + } + if assistant.Model != "deepseek-v4.1-flash" || assistant.Agent != "build" { + t.Fatalf("assistant model/agent = %q/%q", assistant.Model, assistant.Agent) + } + if len(assistant.Parts) != 3 { + t.Fatalf("got %d assistant parts, want text+reasoning+tool: %+v", len(assistant.Parts), assistant.Parts) + } + if p := assistant.Parts[0]; p.Type != protocol.ChatPartText || p.ID != tc.wantTextPartID || p.Text != "here you go" { + t.Fatalf("text part = %+v", p) + } + if p := assistant.Parts[1]; p.Type != protocol.ChatPartReasoning || p.ID != tc.wantReasonPartID || p.Text != "thinking" { + t.Fatalf("reasoning part = %+v", p) + } + tool := assistant.Parts[2] + if tool.Type != protocol.ChatPartTool || tool.ID != "call_1" || tool.Name != "shell" || !tool.Executed { + t.Fatalf("tool part = %+v", tool) + } + if tool.State == nil || tool.State.Status != protocol.ChatToolCompleted { + t.Fatalf("tool state = %+v", tool.State) + } + var input struct { + Command string `json:"command"` + } + if err := json.Unmarshal(tool.State.Input, &input); err != nil || input.Command != "ls" { + t.Fatalf("tool input = %s (err %v)", tool.State.Input, err) + } + if len(tool.State.Content) != 1 || tool.State.Content[0].Type != "text" || tool.State.Content[0].Text != "ok" { + t.Fatalf("tool content = %+v", tool.State.Content) + } + if tool.Time == nil || tool.Time.Completed != 3 { + t.Fatalf("tool time = %+v", tool.Time) + } + }) + } +} + +// TestDecodeChatMessagesSkipsUnknownPartsAndRoles pins that a non user/assistant +// turn and an unrecognised part type are skipped instead of failing the whole +// history, and that an unparseable body is an error rather than empty history. +func TestDecodeChatMessagesSkipsUnknownPartsAndRoles(t *testing.T) { + raw := `{"data":[ + {"id":"msg_system","type":"system","content":[{"type":"text","text":"ignore me"}]}, + {"id":"msg_user","time":{"created":1},"type":"user","text":"hi"}, + {"id":"msg_assistant","time":{"created":2},"type":"assistant","content":[ + {"type":"text","text":"ok"}, + {"type":"file","text":"ignored"}, + {"type":"step","text":"ignored"} + ]} + ]}` + messages, err := decodeChatMessages("ses_1", []byte(raw)) if err != nil { t.Fatal(err) } - if len(messages) != 1 || messages[0].Role != protocol.ChatRoleAssistant || len(messages[0].Parts) != 3 { - t.Fatalf("messages = %+v", messages) + if len(messages) != 2 { + t.Fatalf("got %d messages, want the system turn skipped: %+v", len(messages), messages) } - tool := messages[0].Parts[2] - if tool.Type != protocol.ChatPartTool || tool.Name != "shell" || tool.State == nil || tool.State.Status != protocol.ChatToolCompleted || tool.Time == nil { - t.Fatalf("tool part = %+v", tool) + if messages[0].Role != protocol.ChatRoleUser || len(messages[0].Parts) != 1 || messages[0].Parts[0].Text != "hi" { + t.Fatalf("user turn = %+v", messages[0]) + } + if messages[1].Role != protocol.ChatRoleAssistant || len(messages[1].Parts) != 1 || messages[1].Parts[0].Text != "ok" { + t.Fatalf("assistant turn = %+v", messages[1]) + } + if _, err := decodeChatMessages("ses_1", []byte("not json")); err == nil { + t.Fatal("an unparseable body must be an error, not empty history") } } diff --git a/internal/server/chat_test.go b/internal/server/chat_test.go index f300575b..38778702 100644 --- a/internal/server/chat_test.go +++ b/internal/server/chat_test.go @@ -26,6 +26,9 @@ type fakeChat struct { promptCalls int interrupts int replies int + messageCalls int + messageSessionID string + messageLimit int messages []protocol.ChatMessage script []protocol.ChatEventFrame started chan struct{} @@ -81,10 +84,23 @@ func (f *fakeChat) Interrupt(context.Context, string) error { return nil } -func (f *fakeChat) Messages(context.Context, string, int) ([]protocol.ChatMessage, error) { +func (f *fakeChat) Messages(_ context.Context, sessionID string, limit int) ([]protocol.ChatMessage, error) { + f.mu.Lock() + defer f.mu.Unlock() + f.messageCalls++ + f.messageSessionID = sessionID + f.messageLimit = limit return f.messages, nil } +// messageQuery reports the recorded chat_messages call count, session id and +// limit so a test can assert the handler forwarded the query. +func (f *fakeChat) messageQuery() (int, string, int) { + f.mu.Lock() + defer f.mu.Unlock() + return f.messageCalls, f.messageSessionID, f.messageLimit +} + func (f *fakeChat) Reply(context.Context, protocol.ChatReplyRequest) error { f.mu.Lock() defer f.mu.Unlock() @@ -271,6 +287,145 @@ func TestChatMessagesResumesRetainedState(t *testing.T) { } } +// seededChatMessages is the stored user + assistant + tool session the +// chat_messages evidence tests assert on, mirroring the OpenCode V2 shape of an +// assistant turn's completed tool part (input object, text content, status). +func seededChatMessages() []protocol.ChatMessage { + return []protocol.ChatMessage{ + {ID: "msg_user", SessionID: "ses_1", Role: protocol.ChatRoleUser, + Parts: []protocol.ChatPart{{Type: protocol.ChatPartText, Text: "list the files"}}}, + {ID: "msg_assistant", SessionID: "ses_1", Role: protocol.ChatRoleAssistant, Model: "deepseek-v4.1-flash", Agent: "build", + Parts: []protocol.ChatPart{ + {Type: protocol.ChatPartText, ID: "p1", Text: "here you go"}, + {Type: protocol.ChatPartReasoning, ID: "p2", Text: "thinking"}, + {Type: protocol.ChatPartTool, ID: "call_1", Name: "shell", Executed: true, + State: &protocol.ChatToolState{Status: protocol.ChatToolCompleted, Input: json.RawMessage(`{"command":"ls"}`), Content: []protocol.ChatToolContent{{Type: "text", Text: "ok"}}}, + Time: &protocol.ChatTime{Created: 1, Ran: 2, Completed: 3}}, + }}, + } +} + +// assertSeededChatMessages fails unless the user + assistant + tool session came +// back with both roles and the completed tool part's input, content and status. +func assertSeededChatMessages(t *testing.T, messages []protocol.ChatMessage) { + t.Helper() + if len(messages) != 2 { + t.Fatalf("got %d messages, want user+assistant: %+v", len(messages), messages) + } + user, assistant := messages[0], messages[1] + if user.ID != "msg_user" || user.Role != protocol.ChatRoleUser || len(user.Parts) != 1 || user.Parts[0].Type != protocol.ChatPartText || user.Parts[0].Text != "list the files" { + t.Fatalf("user turn = %+v", user) + } + if assistant.ID != "msg_assistant" || assistant.Role != protocol.ChatRoleAssistant || len(assistant.Parts) != 3 { + t.Fatalf("assistant turn = %+v", assistant) + } + if p := assistant.Parts[0]; p.Type != protocol.ChatPartText || p.Text != "here you go" { + t.Fatalf("assistant text part = %+v", p) + } + tool := assistant.Parts[2] + if tool.Type != protocol.ChatPartTool || tool.ID != "call_1" || tool.Name != "shell" || tool.State == nil || tool.State.Status != protocol.ChatToolCompleted { + t.Fatalf("assistant tool part = %+v", tool) + } + var input struct { + Command string `json:"command"` + } + if err := json.Unmarshal(tool.State.Input, &input); err != nil || input.Command != "ls" { + t.Fatalf("tool input = %s (err %v)", tool.State.Input, err) + } + if len(tool.State.Content) != 1 || tool.State.Content[0].Text != "ok" { + t.Fatalf("tool content = %+v", tool.State.Content) + } +} + +// TestChatMessagesReturnsBrokerHistory drives chat_messages through the handler +// with an empty hub, so it must fall through to broker.Messages and return the +// seeded user + assistant + tool session, forwarding the query limit unchanged. +func TestChatMessagesReturnsBrokerHistory(t *testing.T) { + broker := scriptedChat() + broker.messages = seededChatMessages() + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "r2", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1", Limit: 7}}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.ChatMessages || response.ID != "r2" { + t.Fatalf("response = %+v", response) + } + assertSeededChatMessages(t, response.ChatMessages) + if calls, sessionID, limit := broker.messageQuery(); calls != 1 || sessionID != "ses_1" || limit != 7 { + t.Fatalf("broker query = %d/%q/%d, want 1/ses_1/7", calls, sessionID, limit) + } +} + +// TestChatMessagesThroughRealAdapter loads a seeded session through the real +// opencode.Chat broker: the chat_messages handler reads the V2 +// GET /api/session/{id}/message fixture once and returns the user + assistant + +// tool turns decoded from it, exercising the full handler -> adapter -> decode +// path with no scripted broker. +func TestChatMessagesThroughRealAdapter(t *testing.T) { + var messageReads atomic.Int32 + fixture := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet || r.URL.Path != "/api/session/ses_1/message" { + t.Errorf("unexpected request %s %s", r.Method, r.URL.Path) + return + } + messageReads.Add(1) + w.Header().Set("Content-Type", "application/json") + _, _ = w.Write([]byte(`{"data":[ + {"id":"msg_user","time":{"created":10},"type":"user","text":"list the files"}, + {"id":"msg_assistant","time":{"created":20,"completed":30},"type":"assistant","agent":"build","model":{"id":"deepseek-v4.1-flash","providerID":"opencode-go"},"content":[ + {"type":"text","text":"here you go"}, + {"type":"reasoning","text":"thinking"}, + {"type":"tool","id":"call_1","name":"shell","executed":true,"state":{"status":"completed","input":{"command":"ls"},"content":[{"type":"text","text":"ok"}]},"time":{"created":1,"ran":2,"completed":3}} + ]} + ],"cursor":{"next":null}}`)) + })) + defer fixture.Close() + + broker := opencode.NewChat(&opencode.Connection{Backend: "v2", URL: fixture.URL}) + ctx, _, conn := dialChat(t, broker) + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "r2", ChatQuery: &protocol.ChatQuery{SessionID: "ses_1"}}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.ChatMessages || response.ID != "r2" { + t.Fatalf("response = %+v", response) + } + assertSeededChatMessages(t, response.ChatMessages) + if messageReads.Load() != 1 { + t.Fatalf("message endpoint read %d times, want 1", messageReads.Load()) + } +} + +// TestChatMessagesRejectsInvalidSession pins that a missing or malformed session +// id is rejected before any broker call. +func TestChatMessagesRejectsInvalidSession(t *testing.T) { + broker := scriptedChat() + broker.messages = seededChatMessages() + ctx, _, conn := dialChat(t, broker) + for _, query := range []*protocol.ChatQuery{nil, {SessionID: ""}, {SessionID: "not-a-session"}} { + if err := wsjson.Write(ctx, conn, protocol.Message{Type: protocol.ChatMessages, ID: "bad", ChatQuery: query}); err != nil { + t.Fatal(err) + } + var response protocol.Message + if err := wsjson.Read(ctx, conn, &response); err != nil { + t.Fatal(err) + } + if response.Type != protocol.Error || response.ID != "bad" { + t.Fatalf("query %+v not rejected: %+v", query, response) + } + } + if calls, _, _ := broker.messageQuery(); calls != 0 { + t.Fatalf("invalid chat_messages reached the broker: %d", calls) + } +} + func TestChatDisconnectCancelsStream(t *testing.T) { broker := scriptedChat() broker.block = true