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:
+94
-17
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user