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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
package chatpb
import (
"fmt"
"os"
"github.com/google/uuid"
"go.wit.com/log"
"google.golang.org/protobuf/proto"
)
func (c *Chats) AddRegexComment(s string) *ChatEntry {
log.Info("FIX")
return nil
}
func (c *Chats) AddUserComment(s string) *ChatEntry {
log.Info("FIX")
return nil
}
func UnmarshalChats(data []byte) (*Chats, error) {
c := new(Chats)
err := c.Unmarshal(data)
return c, err
}
func UnmarshalChatsTEXT(data []byte) (*Chats, error) {
c := new(Chats)
err := c.UnmarshalTEXT(data)
return c, err
}
func (all *Chats) AddFile(filename string) error {
// Nil checks for safety.
if all == nil {
return fmt.Errorf("cannot call AddFile on a nil *Chats object")
}
if all.Chats == nil {
all.Chats = make([]*Chat, 0)
}
data, err := os.ReadFile(filename)
if err != nil {
log.Fatalf("Error reading file %s: %v", filename, err)
return err
}
logData, err := UnmarshalChatsTEXT(data)
if err != nil {
log.Fatalf("Error unmarshaling log file %s: %v", filename, err)
return err
}
// New, simplified logic assumes all files use the new nested format.
for _, chatGroup := range logData.GetChats() {
// It's now a direct append since the structure is the same.
// We still clone to ensure the appended data is a safe copy.
newChatGroup := proto.Clone(chatGroup).(*Chat)
all.AppendByUuid(newChatGroup)
}
return nil
}
func (chats *Chats) VerifyUuids() bool {
var changed bool
all := chats.SortByUuid()
for all.Scan() {
chat := all.Next()
if chat.Uuid == "" {
chat.Uuid = uuid.New().String()
changed = true
}
}
return changed
}
func (c *Chat) VerifyUuid() bool {
if c.Uuid == "" {
c.Uuid = uuid.New().String()
return true
}
return false
}
func (x *Chats) AppendNew(y *Chat) {
x.Lock()
defer x.Unlock()
var chat *Chat
chat = proto.Clone(y).(*Chat)
x.Chats = append(x.Chats, chat)
}
// a Append() shortcut (that does Clone() with a mutex) notsure if it really works
func (x *Chat) AppendEntry(y *ChatEntry) {
chatMu.Lock()
defer chatMu.Unlock()
x.Entries = append(x.Entries, proto.Clone(y).(*ChatEntry))
}
// returns true if the pb is probably the same
func (c *ChatEntry) VerifyGeminiRequest(pb *GeminiRequest) (int, int, bool) {
if c.GeminiRequest == nil {
log.Warn("There is no GeminiRequest in the chat protobuf")
return -1, -1, false
}
if pb == nil {
log.Warn("There is no GeminiRequest in the passed in protobuf")
return -1, -1, false
}
if len(c.GeminiRequest.GetContents()) != len(pb.GetContents()) {
log.Warn("c != pb", len(c.GeminiRequest.GetContents()), len(pb.GetContents()))
return -1, -1, false
}
var cCount int // # of Parts in all the Contents
var pbCount int // # of Parts in all the Contents
for _, grc := range c.GeminiRequest.GetContents() {
cCount += len(grc.GetParts())
}
for _, grc := range pb.GetContents() {
pbCount += len(grc.GetParts())
}
if cCount != pbCount {
log.Warn("c != pb", cCount, pbCount)
return len(c.GeminiRequest.GetContents()), cCount, false
}
log.Info("EVERYTHING MATCHED", cCount, pbCount, len(c.GeminiRequest.GetContents()), len(pb.GetContents()))
return len(c.GeminiRequest.GetContents()), cCount, true
}
|