@@ -46,86 +46,92 @@ export function createCompressMessageTool(ctx: ToolContext): ReturnType<typeof t
4646 description : runtimePrompts . compressMessage + MESSAGE_FORMAT_EXTENSION ,
4747 args : buildSchema ( ) ,
4848 async execute ( args , toolCtx ) {
49- const input = args as CompressMessageToolArgs
50- validateArgs ( input )
51- const { rawMessages, searchContext } = await prepareSession (
52- ctx ,
53- toolCtx ,
54- `Compress Message: ${ input . topic } ` ,
55- )
56- const { plans, skippedIssues } = resolveMessages (
57- input ,
58- searchContext ,
59- ctx . state ,
60- ctx . config ,
61- )
49+ try {
50+ const input = args as CompressMessageToolArgs
51+ validateArgs ( input )
52+ const { rawMessages, searchContext } = await prepareSession (
53+ ctx ,
54+ toolCtx ,
55+ `Compress Message: ${ input . topic } ` ,
56+ )
57+ const { plans, skippedIssues } = resolveMessages (
58+ input ,
59+ searchContext ,
60+ ctx . state ,
61+ ctx . config ,
62+ )
6263
63- if ( plans . length === 0 && skippedIssues . length > 0 ) {
64- throw new Error ( formatIssues ( skippedIssues ) )
65- }
64+ if ( plans . length === 0 && skippedIssues . length > 0 ) {
65+ throw new Error ( formatIssues ( skippedIssues ) )
66+ }
6667
67- const notifications : NotificationEntry [ ] = [ ]
68+ const notifications : NotificationEntry [ ] = [ ]
6869
69- const preparedPlans : Array < {
70- plan : ( typeof plans ) [ number ]
71- summaryWithTools : string
72- } > = [ ]
70+ const preparedPlans : Array < {
71+ plan : ( typeof plans ) [ number ]
72+ summaryWithTools : string
73+ } > = [ ]
7374
74- for ( const plan of plans ) {
75- const summaryWithTools = await appendProtectedTools (
76- ctx . client ,
77- ctx . state ,
78- ctx . config . experimental . allowSubAgents ,
79- plan . entry . summary ,
80- plan . selection ,
81- searchContext ,
82- ctx . config . compress . protectedTools ,
83- ctx . config . protectedFilePatterns ,
84- )
75+ for ( const plan of plans ) {
76+ const summaryWithTools = await appendProtectedTools (
77+ ctx . client ,
78+ ctx . state ,
79+ ctx . config . experimental . allowSubAgents ,
80+ plan . entry . summary ,
81+ plan . selection ,
82+ searchContext ,
83+ ctx . config . compress . protectedTools ,
84+ ctx . config . protectedFilePatterns ,
85+ )
8586
86- preparedPlans . push ( {
87- plan,
88- summaryWithTools,
89- } )
90- }
87+ preparedPlans . push ( {
88+ plan,
89+ summaryWithTools,
90+ } )
91+ }
9192
92- const runId = allocateRunId ( ctx . state )
93+ const runId = allocateRunId ( ctx . state )
94+ const durationMs = ctx . state . compressionDurations . get ( toolCtx . messageID ) || 0
9395
94- for ( const { plan, summaryWithTools } of preparedPlans ) {
95- const blockId = allocateBlockId ( ctx . state )
96- const storedSummary = wrapCompressedSummary ( blockId , summaryWithTools )
97- const summaryTokens = countTokens ( storedSummary )
96+ for ( const { plan, summaryWithTools } of preparedPlans ) {
97+ const blockId = allocateBlockId ( ctx . state )
98+ const storedSummary = wrapCompressedSummary ( blockId , summaryWithTools )
99+ const summaryTokens = countTokens ( storedSummary )
98100
99- applyCompressionState (
100- ctx . state ,
101- {
102- topic : plan . entry . topic ,
103- batchTopic : input . topic ,
104- startId : plan . entry . messageId ,
105- endId : plan . entry . messageId ,
106- mode : "message" ,
101+ applyCompressionState (
102+ ctx . state ,
103+ {
104+ topic : plan . entry . topic ,
105+ batchTopic : input . topic ,
106+ startId : plan . entry . messageId ,
107+ endId : plan . entry . messageId ,
108+ mode : "message" ,
109+ runId,
110+ compressMessageId : toolCtx . messageID ,
111+ summaryTokens,
112+ durationMs,
113+ } ,
114+ plan . selection ,
115+ plan . anchorMessageId ,
116+ blockId ,
117+ storedSummary ,
118+ [ ] ,
119+ )
120+
121+ notifications . push ( {
122+ blockId,
107123 runId,
108- compressMessageId : toolCtx . messageID ,
124+ summary : summaryWithTools ,
109125 summaryTokens,
110- } ,
111- plan . selection ,
112- plan . anchorMessageId ,
113- blockId ,
114- storedSummary ,
115- [ ] ,
116- )
117-
118- notifications . push ( {
119- blockId,
120- runId,
121- summary : summaryWithTools ,
122- summaryTokens,
123- } )
124- }
126+ } )
127+ }
125128
126- await finalizeSession ( ctx , toolCtx , rawMessages , notifications , input . topic )
129+ await finalizeSession ( ctx , toolCtx , rawMessages , notifications , input . topic )
127130
128- return formatResult ( plans . length , skippedIssues )
131+ return formatResult ( plans . length , skippedIssues )
132+ } finally {
133+ ctx . state . compressionDurations . delete ( toolCtx . messageID )
134+ }
129135 } ,
130136 } )
131137}
0 commit comments