Skip to content

Commit 670196d

Browse files
committed
Optimize AI prompt by minimizing message structure
- Add minimizeMessages() function to strip unnecessary data from messages - Remove step markers that add no value for analysis - Keep only essential tool call information (callID, tool, output, minimal input) - Summarize batch operations instead of including full arrays - Reduces token usage significantly while preserving analysis accuracy
1 parent 508cc2e commit 670196d

File tree

1 file changed

+87
-7
lines changed

1 file changed

+87
-7
lines changed

lib/prompt.ts

Lines changed: 87 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,88 @@
1+
/**
2+
* Minimize message structure for AI analysis - keep only what's needed
3+
* to determine if tool calls are obsolete
4+
*/
5+
function minimizeMessages(messages: any[]): any[] {
6+
return messages.map(msg => {
7+
const minimized: any = {
8+
role: msg.info?.role
9+
}
10+
11+
// Keep essential parts only
12+
if (msg.parts) {
13+
minimized.parts = msg.parts
14+
.filter((part: any) => {
15+
// Completely remove step markers - they add no value for janitor
16+
if (part.type === 'step-start' || part.type === 'step-finish') {
17+
return false
18+
}
19+
return true
20+
})
21+
.map((part: any) => {
22+
// For text parts, keep the text content (needed for user intent & retention requests)
23+
if (part.type === 'text') {
24+
return {
25+
type: 'text',
26+
text: part.text
27+
}
28+
}
29+
30+
// For tool parts, keep what's needed for pruning decisions
31+
if (part.type === 'tool') {
32+
const toolPart: any = {
33+
type: 'tool',
34+
callID: part.callID,
35+
tool: part.tool
36+
}
37+
38+
// Keep the actual output - janitor needs to see what was returned
39+
if (part.state?.output) {
40+
toolPart.output = part.state.output
41+
}
42+
43+
// Include minimal input for deduplication context
44+
// Only keep resource identifiers, not full nested structures
45+
if (part.state?.input) {
46+
const input = part.state.input
47+
48+
// For file operations, just keep the file path
49+
if (input.filePath) {
50+
toolPart.input = { filePath: input.filePath }
51+
}
52+
// For batch operations, summarize instead of full array
53+
else if (input.tool_calls && Array.isArray(input.tool_calls)) {
54+
toolPart.input = {
55+
batch_summary: `${input.tool_calls.length} tool calls`,
56+
tools: input.tool_calls.map((tc: any) => tc.tool)
57+
}
58+
}
59+
// For other operations, keep minimal input
60+
else {
61+
toolPart.input = input
62+
}
63+
}
64+
65+
return toolPart
66+
}
67+
68+
// Skip all other part types (they're not relevant to pruning)
69+
return null
70+
})
71+
.filter(Boolean) // Remove nulls
72+
}
73+
74+
return minimized
75+
})
76+
}
77+
178
export function buildAnalysisPrompt(unprunedToolCallIds: string[], messages: any[], protectedTools: string[]): string {
2-
const protectedToolsText = protectedTools.length > 0
3-
? `- NEVER prune the following protected tools: ${protectedTools.join(", ")}\n`
79+
const protectedToolsText = protectedTools.length > 0
80+
? `- NEVER prune tool calls from these protected tools: ${protectedTools.join(", ")}\n`
481
: '';
5-
82+
83+
// Minimize messages to reduce token usage
84+
const minimizedMessages = minimizeMessages(messages)
85+
686
return `You are a conversation analyzer that identifies obsolete tool outputs in a coding session.
787
888
Your task: Analyze the session history and identify tool call IDs whose outputs are NO LONGER RELEVANT to the current conversation context.
@@ -15,19 +95,19 @@ Guidelines for identifying obsolete tool calls:
1595
5. Tool calls whose information has been replaced by more recent operations
1696
1797
DO NOT prune:
18-
${protectedToolsText}- Recent tool calls
98+
${protectedToolsText}
1999
- Tool calls that modified state (edits, writes, etc.)
20100
- Tool calls whose outputs are actively being discussed
21101
- Tool calls that produced errors still being debugged
22102
- Tool calls where the user explicitly indicated they want to retain the information (e.g., "save this", "remember this", "keep this for later", "don't output anything else but save this")
23103
- Tool calls that are the MOST RECENT activity in the conversation (these may be intended for future use)
24104
25-
IMPORTANT: Available tool call IDs for analysis (duplicates already removed): ${unprunedToolCallIds.join(", ")}
105+
IMPORTANT: Available tool call IDs for analysis: ${unprunedToolCallIds.join(", ")}
26106
27-
You may see additional tool call IDs in the session history below, but those have already been handled by automatic deduplication. ONLY return IDs from the available list above.
107+
You may see additional tool call IDs in the session history below, but those have already been pruned (either by automatic deduplication or previous analysis runs) and their outputs replaced with placeholders. ONLY return IDs from the available list above.
28108
29109
Session history:
30-
${JSON.stringify(messages, null, 2)}
110+
${JSON.stringify(minimizedMessages, null, 2)}
31111
32112
You MUST respond with valid JSON matching this exact schema:
33113
{

0 commit comments

Comments
 (0)