1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
package main
import (
"os"
"path/filepath"
"go.wit.com/lib/protobuf/chatpb"
"go.wit.com/log"
)
// addFile reads a protobuf text file, inlines all external content, and returns
// a self-contained *chatpb.Chats object.
func addFile(filename string) (*chatpb.Chats, error) {
data, err := os.ReadFile(filename)
if err != nil {
return nil, log.Errorf("failed to read file %s: %w", filename, err)
}
logData, err := chatpb.UnmarshalChatsTEXT(data)
if err != nil {
return nil, log.Errorf("failed to unmarshal log file %s: %w", filename, err)
}
log.Info("Successfully parsed log file: %s", filename)
// Get the directory of the log file to resolve relative content paths.
logDir := filepath.Dir(filename)
// Iterate through the structure to inline all external content.
for _, chat := range logData.Chats {
for _, entry := range chat.Entries {
// Inline main content from ContentFile
if contentFile := entry.ContentFile; contentFile != "" {
contentPath := filepath.Join(logDir, contentFile)
contentBytes, err := os.ReadFile(contentPath)
if err != nil {
entry.Content = log.Sprintf("--- ERROR: Could not read content file %s: %v ---", contentPath, err)
} else {
entry.Content = string(contentBytes)
}
}
// Inline snippet content from snippet files
if snippets := entry.Snippets; snippets != nil {
for _, snippet := range snippets {
if snippetFile := snippet.Filename; snippetFile != "" {
snippetPath := filepath.Join(logDir, snippetFile)
contentBytes, err := os.ReadFile(snippetPath)
if err != nil {
snippet.Content = log.Sprintf("--- ERROR: Could not read snippet file %s: %v ---", snippetPath, err)
} else {
snippet.Content = string(contentBytes)
}
}
}
}
}
}
log.Info("Successfully inlined all external content.")
return logData, nil
}
|