Forward recorded Codex turn timestamps to chat history

This commit is contained in:
2026-11-06 00:00:21 +00:00
parent 6d4ed4ac67
commit 20a69b7e47
3 changed files with 63 additions and 5 deletions
+17 -5
View File
@@ -654,12 +654,13 @@ func (c *Chat) Messages(ctx context.Context, id string, limit int, cursor string
turn := result.Data[index]
for _, item := range turn.Items {
if msg, ok := item.chatMessage(id); ok {
msg.CreatedAt = turn.createdAtMillis()
messages = append(messages, msg)
}
}
if turn.ID != "" && rawText(turn.Error) != "" {
part := item{ID: turn.ID + "#error", Type: "Codex turn", Status: "failed", Error: turn.Error, Input: map[string]any{"turn_id": turn.ID}}.toolPart(true)
messages = append(messages, protocol.ChatMessage{ID: part.ID, SessionID: id, Role: protocol.ChatRoleAssistant, Parts: []protocol.ChatPart{part}})
messages = append(messages, protocol.ChatMessage{ID: part.ID, SessionID: id, Role: protocol.ChatRoleAssistant, CreatedAt: turn.createdAtMillis(), Parts: []protocol.ChatPart{part}})
}
}
page := protocol.ChatHistoryPage{Messages: messages}
@@ -975,10 +976,21 @@ type thread struct {
}
type turn struct {
ID string `json:"id"`
Status string `json:"status"`
Error json.RawMessage `json:"error"`
Items []item `json:"items"`
StartedAt int64 `json:"startedAt"`
ID string `json:"id"`
Status string `json:"status"`
Error json.RawMessage `json:"error"`
Items []item `json:"items"`
}
// Codex history exposes a recorded turn start in seconds, rather than item
// creation times. Use that shared turn time for its user and assistant rows.
// Older history without timing remains unknown; never substitute read time.
func (t turn) createdAtMillis() int64 {
if t.StartedAt <= 0 || t.StartedAt > 8640000000000 {
return 0
}
return t.StartedAt * 1000
}
type item struct {