aboutsummaryrefslogtreecommitdiff
path: root/internal/claude/search.go
blob: 7959983f03bf05d0e45050487f9d2e30e67328cc (plain) (blame)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package claude

import (
	"bufio"
	"encoding/json"
	"os"
	"strings"
)

type SearchResult struct {
	Session       *Session
	MessageIndex  int
	Role          string
	Content       string
	MatchPosition int
}

func SearchAllSessions(sessions []Session, query string) []SearchResult {
	if query == "" {
		return nil
	}

	query = strings.ToLower(query)

	var results []SearchResult

	for sessionIndex := range sessions {
		session := &sessions[sessionIndex]
		matches := searchSession(session, query)
		results = append(results, matches...)
	}

	return results
}

func searchSession(session *Session, query string) []SearchResult {
	file, openError := os.Open(session.FullPath)

	if openError != nil {
		return nil
	}

	defer func() { _ = file.Close() }()

	var results []SearchResult

	scanner := bufio.NewScanner(file)
	scanBuffer := make([]byte, 0, 64*1024)

	scanner.Buffer(scanBuffer, 10*1024*1024)

	messageIndex := 0

	for scanner.Scan() {
		line := scanner.Text()

		if line == "" {
			continue
		}

		var rawMessage RawMessage

		if unmarshalError := json.Unmarshal([]byte(line), &rawMessage); unmarshalError != nil {
			continue
		}

		matches := searchRawMessage(session, &rawMessage, query, messageIndex)
		results = append(results, matches...)

		if rawMessage.Type == "user" || rawMessage.Type == "assistant" {
			messageIndex += 1
		}
	}

	return results
}

func searchRawMessage(session *Session, rawMessage *RawMessage, query string, messageIndex int) []SearchResult {
	var results []SearchResult

	switch rawMessage.Type {
	case "user":
		var userMessage UserMessage

		if unmarshalError := json.Unmarshal(rawMessage.Message, &userMessage); unmarshalError != nil {
			return nil
		}

		contentLowercase := strings.ToLower(userMessage.Content)

		if matchPosition := strings.Index(contentLowercase, query); matchPosition != -1 {
			content := matchContext(userMessage.Content, matchPosition, len(query))
			results = append(results, SearchResult{
				Session:       session,
				MessageIndex:  messageIndex,
				Role:          "user",
				Content:       content,
				MatchPosition: matchPosition,
			})
		}
	case "assistant":
		var assistantMessage AssistantMessage

		if unmarshalError := json.Unmarshal(rawMessage.Message, &assistantMessage); unmarshalError != nil {
			return nil
		}

		for _, contentBlock := range assistantMessage.Content {
			if contentBlock.Type == "text" {
				textLowercase := strings.ToLower(contentBlock.Text)

				if matchPosition := strings.Index(textLowercase, query); matchPosition != -1 {
					content := matchContext(contentBlock.Text, matchPosition, len(query))
					results = append(results, SearchResult{
						Session:       session,
						MessageIndex:  messageIndex,
						Role:          "assistant",
						Content:       content,
						MatchPosition: matchPosition,
					})
				}
			}
		}
	}

	return results
}

func matchContext(text string, matchPosition, matchLength int) string {
	const contextLength = 100

	start := matchPosition - contextLength/2

	if start < 0 {
		start = 0
	}

	end := matchPosition + matchLength + contextLength/2

	if end > len(text) {
		end = len(text)
	}

	result := text[start:end]
	result = strings.ReplaceAll(result, "\n", " ")
	result = strings.Join(strings.Fields(result), " ")

	if start > 0 {
		result = "… " + result
	}

	if end < len(text) {
		result = result + " …"
	}

	return result
}

func SearchPreview(preview *PreviewContent, query string) []int {
	if preview == nil || query == "" {
		return nil
	}

	query = strings.ToLower(query)

	var matches []int

	for messageIndex, previewMessage := range preview.Messages {
		if strings.Contains(strings.ToLower(previewMessage.Content), query) {
			matches = append(matches, messageIndex)
		}
	}

	return matches
}