Server: serve a Claude chat's history page from a per-file line index
Reading a history page parsed the whole session file (tens of MB) on every chat_messages. A per-file index now records which lines build each shown message; a page re-reads and replays only its own messages' lines, and the index follows appends by reading only the new bytes. Results match the whole-file read (compared page by page on synthetic transcripts); shapes the index cannot replay exactly fall back to the whole-file read. chat_subscribe may ask (history: true) for the newest page in its reply, read once the subscription is attached, saving the client a round trip. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,6 +2,7 @@ package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -233,7 +234,7 @@ func handleChatModelSwitch(ctx context.Context, request protocol.Message, broker
|
||||
// receives every frame any client's turn produces, its own or another device's.
|
||||
// It replays the session's still-pending approval requests, if the room holds
|
||||
// any, so a device that joins late still shows the cards.
|
||||
func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatHub, clientID string, write func(protocol.Message) error) {
|
||||
func handleChatSubscribe(ctx context.Context, request protocol.Message, broker chatBroker, hub *chatHub, clientID string, write func(protocol.Message) error) {
|
||||
// The wire body of chat_subscribe is the chat_subscribe field (the app's
|
||||
// subscribeRequest and docs/cli.md both use it), not the chat_query body the
|
||||
// other session-scoped commands carry.
|
||||
@@ -265,7 +266,20 @@ func handleChatSubscribe(request protocol.Message, broker chatBroker, hub *chatH
|
||||
// session, so a reloaded client draws them as Queued without any device
|
||||
// storage.
|
||||
queue := hub.queueFrame(query.SessionID)
|
||||
_ = write(protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID, ChatQueue: &queue, ChatShown: hub.shownFrame(query.SessionID)})
|
||||
reply := protocol.Message{Type: protocol.ChatSubscribe, ID: request.ID, ChatQueue: &queue, ChatShown: hub.shownFrame(query.SessionID)}
|
||||
if query.History {
|
||||
// Read after attaching, as a chat_messages sent after this reply would
|
||||
// be: a frame produced meanwhile waits in the subscription's queue.
|
||||
// A failed read leaves the page out; the client then asks for it with
|
||||
// chat_messages, which reports the error.
|
||||
history := protocol.Message{}
|
||||
if err := addChatHistory(ctx, &history, broker, hub, protocol.ChatQuery{SessionID: query.SessionID, Limit: query.Limit}); err == nil {
|
||||
reply.ChatMessages, reply.ChatHistory, reply.ChatTurnDiffList = history.ChatMessages, history.ChatHistory, history.ChatTurnDiffList
|
||||
} else {
|
||||
log.Printf("chat session %s: history for the subscribe reply: %v", query.SessionID, err)
|
||||
}
|
||||
}
|
||||
_ = write(reply)
|
||||
if fresh {
|
||||
go sub.run()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user