xonecas.io
6119
My

mysis / files

mysis / internal / store / compression.go

4.8 KB
1package store
2
3import (
4	"encoding/json"
5	"strings"
6
7	"github.com/xonecas/mysis/internal/provider"
8)
9
10// ToolCategory represents the category of a tool for compression purposes.
11type ToolCategory int
12
13const (
14	// ToolCategoryAuth - Authentication tools (never compress)
15	ToolCategoryAuth ToolCategory = iota
16	// ToolCategoryState - State query tools (compress old results, keep latest)
17	ToolCategoryState
18	// ToolCategoryAction - Action tools (keep full history)
19	ToolCategoryAction
20)
21
22// compressedToolResult is a marker for compressed content
23const compressedToolResult = "[compressed - old state data]"
24
25// isStateQueryTool returns true if the tool is a state query that can be compressed.
26func isStateQueryTool(toolName string) bool {
27	stateTools := []string{
28		"get_status",
29		"get_ship",
30		"get_system",
31		"get_sector",
32		"get_galaxy",
33		"get_map",
34		"get_players",
35		"get_leaderboard",
36		"get_market",
37		"get_cargo",
38		"captains_log_list",
39	}
40
41	toolName = strings.ToLower(toolName)
42	for _, st := range stateTools {
43		if toolName == st {
44			return true
45		}
46	}
47	return false
48}
49
50// isAuthTool returns true if the tool is authentication-related (never compress).
51func isAuthTool(toolName string) bool {
52	authTools := []string{
53		"login",
54		"register",
55		"logout",
56	}
57
58	toolName = strings.ToLower(toolName)
59	for _, at := range authTools {
60		if toolName == at {
61			return true
62		}
63	}
64	return false
65}
66
67// CompressHistory compresses old tool results while preserving recent context.
68func CompressHistory(messages []provider.Message, keepFullTurns int) []provider.Message {
69	if len(messages) == 0 {
70		return messages
71	}
72
73	// Count turns (user messages)
74	turnCount := 0
75	for i := len(messages) - 1; i >= 0; i-- {
76		if messages[i].Role == "user" {
77			turnCount++
78		}
79	}
80
81	// If we have fewer turns than the threshold, no compression needed
82	if turnCount <= keepFullTurns {
83		return messages
84	}
85
86	// Find the cutoff point (first message of the turn that should be kept)
87	// If keepFullTurns=2, we want to keep the last 2 turns and compress everything before
88	currentTurn := 0
89	cutoffIndex := -1
90	for i := len(messages) - 1; i >= 0; i-- {
91		if messages[i].Role == "user" {
92			currentTurn++
93			if currentTurn == keepFullTurns {
94				// This is the first user message of the Nth-from-last turn
95				// The cutoff is this message (we keep from here onwards)
96				cutoffIndex = i
97				break
98			}
99		}
100	}
101
102	if cutoffIndex == -1 || cutoffIndex == 0 {
103		return messages
104	}
105
106	// Build compressed history
107	compressed := make([]provider.Message, 0, len(messages))
108
109	// Second pass: compress old messages
110	for i := 0; i < cutoffIndex; i++ {
111		msg := messages[i]
112
113		// Keep user messages and assistant messages (they're small)
114		if msg.Role == "user" || msg.Role == "assistant" {
115			compressed = append(compressed, msg)
116			continue
117		}
118
119		// Handle tool results
120		if msg.Role == "tool" {
121			// Find the tool name from the assistant message
122			toolName := findToolNameForResult(messages, i)
123
124			// Never compress auth tools
125			if isAuthTool(toolName) {
126				compressed = append(compressed, msg)
127				continue
128			}
129
130			// For state queries in old section, always compress
131			if isStateQueryTool(toolName) {
132				compressedMsg := msg
133				compressedMsg.Content = compressedToolResult
134				compressed = append(compressed, compressedMsg)
135				continue
136			}
137
138			// For action tools, compress if result is too long
139			if len(msg.Content) > 500 {
140				compressedMsg := msg
141				compressedMsg.Content = msg.Content[:200] + "... [truncated]"
142				compressed = append(compressed, compressedMsg)
143			} else {
144				compressed = append(compressed, msg)
145			}
146		}
147	}
148
149	// Add all recent messages (after cutoff) unchanged
150	compressed = append(compressed, messages[cutoffIndex:]...)
151
152	return compressed
153}
154
155// findToolNameForResult finds the tool name for a tool result message
156// by looking back for the assistant message with the matching tool call.
157func findToolNameForResult(messages []provider.Message, resultIndex int) string {
158	if resultIndex >= len(messages) || messages[resultIndex].Role != "tool" {
159		return ""
160	}
161
162	toolCallID := messages[resultIndex].ToolCallID
163
164	// Search backwards for the assistant message with this tool call
165	for i := resultIndex - 1; i >= 0; i-- {
166		msg := messages[i]
167		if msg.Role == "assistant" && len(msg.ToolCalls) > 0 {
168			for _, tc := range msg.ToolCalls {
169				if tc.ID == toolCallID {
170					return tc.Name
171				}
172			}
173		}
174	}
175
176	return ""
177}
178
179// EstimateTokenCount provides a rough estimate of token count for history.
180// This is used for logging and debugging, not exact.
181func EstimateTokenCount(messages []provider.Message) int {
182	total := 0
183	for _, msg := range messages {
184		// Rough estimate: ~4 characters per token
185		total += len(msg.Content) / 4
186
187		// Add tool calls
188		if len(msg.ToolCalls) > 0 {
189			data, _ := json.Marshal(msg.ToolCalls)
190			total += len(data) / 4
191		}
192
193		// Add role overhead
194		total += 4
195	}
196	return total
197}