refactor(chatpb): Simplify AddFile for new log format
- Remove all backward-compatibility logic from the AddFile function. - The function now assumes all input logs use the modern, nested 'Chat -> Entries' structure. - This simplifies the codebase significantly but requires that all old log files be deprecated or converted. - Note: This commit leaves the build in a broken state, as other files still reference the now-removed fields.
This commit is contained in:
parent
21ca3300d4
commit
4575c97aae
84
helpers.go
84
helpers.go
|
@ -68,88 +68,16 @@ func (all *Chats) AddFile(filename string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// New logic to handle both flat and nested formats
|
// New, simplified logic assumes all files use the new nested format.
|
||||||
for _, chatOrGroup := range logData.GetChats() {
|
for _, chatGroup := range logData.GetChats() {
|
||||||
if len(chatOrGroup.GetEntries()) > 0 {
|
// It's now a direct append since the structure is the same.
|
||||||
// This is a new-style Chat group with entries
|
// We still clone to ensure the appended data is a safe copy.
|
||||||
for _, entry := range chatOrGroup.GetEntries() {
|
newChatGroup := proto.Clone(chatGroup).(*Chat)
|
||||||
// Convert the ChatEntry into a new Chat object for the flat list.
|
all.AppendByUuid(newChatGroup)
|
||||||
newChat := convertEntryToChat(entry, filename)
|
|
||||||
if newChat != nil {
|
|
||||||
newChat.VerifyUuid()
|
|
||||||
all.AppendByUuid(newChat)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// This is an old-style flat Chat entry.
|
|
||||||
// We still process it to handle its external content file correctly.
|
|
||||||
newChat := convertChatToChat(chatOrGroup, filename)
|
|
||||||
if newChat != nil {
|
|
||||||
newChat.VerifyUuid()
|
|
||||||
all.AppendByUuid(newChat)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// convertChatToChat handles an old-style Chat message. It creates a clean
|
|
||||||
// copy and resolves its external content file.
|
|
||||||
func convertChatToChat(chat *Chat, filename string) *Chat {
|
|
||||||
if chat == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Manually create a ChatEntry from the Chat fields to reuse the logic.
|
|
||||||
entry := &ChatEntry{
|
|
||||||
From: chat.GetFrom(),
|
|
||||||
Ctime: chat.GetCtime(),
|
|
||||||
Content: chat.GetContent(),
|
|
||||||
Table: chat.GetTable(),
|
|
||||||
ToolCalls: chat.GetToolCalls(),
|
|
||||||
ContentFile: chat.GetContentFile(),
|
|
||||||
Uuid: chat.GetUuid(),
|
|
||||||
Snippets: chat.GetSnippets(),
|
|
||||||
}
|
|
||||||
return convertEntryToChat(entry, filename)
|
|
||||||
}
|
|
||||||
|
|
||||||
// convertEntryToChat creates a new Chat object from a ChatEntry's data
|
|
||||||
// and resolves its external content file.
|
|
||||||
func convertEntryToChat(entry *ChatEntry, filename string) *Chat {
|
|
||||||
if entry == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a new Chat object and copy the fields.
|
|
||||||
newChat := &Chat{
|
|
||||||
From: entry.GetFrom(),
|
|
||||||
Ctime: entry.GetCtime(),
|
|
||||||
Table: entry.GetTable(),
|
|
||||||
ToolCalls: entry.GetToolCalls(),
|
|
||||||
Snippets: entry.GetSnippets(),
|
|
||||||
Uuid: entry.GetUuid(),
|
|
||||||
}
|
|
||||||
|
|
||||||
// Handle content: prefer content_file, fallback to content.
|
|
||||||
var content string
|
|
||||||
if contentFile := entry.GetContentFile(); contentFile != "" {
|
|
||||||
logDir := filepath.Dir(filename)
|
|
||||||
contentPath := filepath.Join(logDir, contentFile)
|
|
||||||
contentBytes, err := os.ReadFile(contentPath)
|
|
||||||
if err != nil {
|
|
||||||
content = fmt.Sprintf("--- ERROR: Could not read content file %s: %v ---", contentPath, err)
|
|
||||||
} else {
|
|
||||||
content = string(contentBytes)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
content = entry.GetContent()
|
|
||||||
}
|
|
||||||
newChat.Content = content
|
|
||||||
|
|
||||||
return newChat
|
|
||||||
}
|
|
||||||
|
|
||||||
func (chats *Chats) VerifyUuids() bool {
|
func (chats *Chats) VerifyUuids() bool {
|
||||||
var changed bool
|
var changed bool
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue