Expose OpenCode conversation history in chat_messages

decodeChatMessages only understood the native-serve {info,parts} envelope,
so records from the V2 HTTP API (GET /api/session/{id}/message) decoded
with an empty role and were silently dropped, leaving chat_messages with
empty history. Teach nativeChatMessage to read the V2 top-level shape
(id/type/agent/model/time, parts under "content", user prompt as a bare
string) while keeping the native {info,parts} path via role/id/model/agent/
created accessors and a contentParts fallback.

Add seeded-session evidence: a decoder table test covering both envelopes
(user + assistant with text, reasoning and a completed tool part carrying
input, content and status) plus handler tests that drive chat_messages
through the broker fallthrough and through the real adapter.
This commit is contained in:
2026-10-16 05:24:00 +01:00
parent 315788b3cb
commit 915016df0b
3 changed files with 374 additions and 25 deletions
+124 -7
View File
@@ -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")
}
}