-
-
Notifications
You must be signed in to change notification settings - Fork 65
Expand file tree
/
Copy pathon-idle.ts
More file actions
317 lines (272 loc) · 10.1 KB
/
on-idle.ts
File metadata and controls
317 lines (272 loc) · 10.1 KB
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import { z } from "zod"
import type { SessionState, WithParts, ToolParameterEntry } from "../state"
import type { Logger } from "../logger"
import type { PluginConfig } from "../config"
import { buildAnalysisPrompt } from "../prompt"
import { selectModel, ModelInfo } from "../model-selector"
import { saveSessionState } from "../state/persistence"
import { sendUnifiedNotification } from "../ui/notification"
import { calculateTokensSaved, getCurrentParams } from "./utils"
export interface OnIdleResult {
prunedCount: number
tokensSaved: number
prunedIds: string[]
}
/**
* Parse messages to extract tool information.
*/
function parseMessages(
messages: WithParts[],
toolParametersCache: Map<string, ToolParameterEntry>
): {
toolCallIds: string[]
toolMetadata: Map<string, ToolParameterEntry>
} {
const toolCallIds: string[] = []
const toolMetadata = new Map<string, ToolParameterEntry>()
for (const msg of messages) {
if (msg.parts) {
for (const part of msg.parts) {
if (part.type === "tool" && part.callID) {
toolCallIds.push(part.callID)
const cachedData = toolParametersCache.get(part.callID)
const parameters = cachedData?.parameters ?? part.state?.input ?? {}
toolMetadata.set(part.callID, {
tool: part.tool,
parameters: parameters,
status: part.state?.status,
error: part.state?.status === "error" ? part.state.error : undefined
})
}
}
}
}
return { toolCallIds, toolMetadata }
}
/**
* Replace pruned tool outputs in messages for LLM analysis.
*/
function replacePrunedToolOutputs(messages: WithParts[], prunedIds: string[]): WithParts[] {
if (prunedIds.length === 0) return messages
const prunedIdsSet = new Set(prunedIds)
return messages.map(msg => {
if (!msg.parts) return msg
return {
...msg,
parts: msg.parts.map((part: any) => {
if (part.type === 'tool' &&
part.callID &&
prunedIdsSet.has(part.callID) &&
part.state?.output) {
return {
...part,
state: {
...part.state,
output: '[Output removed to save context - information superseded or no longer needed]'
}
}
}
return part
})
}
}) as WithParts[]
}
/**
* Run LLM analysis to determine which tool calls can be pruned.
*/
async function runLlmAnalysis(
client: any,
state: SessionState,
logger: Logger,
config: PluginConfig,
messages: WithParts[],
unprunedToolCallIds: string[],
alreadyPrunedIds: string[],
toolMetadata: Map<string, ToolParameterEntry>,
workingDirectory?: string
): Promise<string[]> {
const protectedToolCallIds: string[] = []
const prunableToolCallIds = unprunedToolCallIds.filter(id => {
const metadata = toolMetadata.get(id)
if (metadata && config.strategies.onIdle.protectedTools.includes(metadata.tool)) {
protectedToolCallIds.push(id)
return false
}
return true
})
if (prunableToolCallIds.length === 0) {
return []
}
// Get model info from messages
let validModelInfo: ModelInfo | undefined = undefined
if (messages.length > 0) {
const lastMessage = messages[messages.length - 1]
const model = (lastMessage.info as any)?.model
if (model?.providerID && model?.modelID) {
validModelInfo = {
providerID: model.providerID,
modelID: model.modelID
}
}
}
const modelSelection = await selectModel(
validModelInfo,
logger,
config.strategies.onIdle.model,
workingDirectory
)
logger.info(`OnIdle Model: ${modelSelection.modelInfo.providerID}/${modelSelection.modelInfo.modelID}`, {
source: modelSelection.source
})
if (modelSelection.failedModel && config.strategies.onIdle.showModelErrorToasts) {
const skipAi = modelSelection.source === 'fallback' && config.strategies.onIdle.strictModelSelection
try {
await client.tui.showToast({
body: {
title: skipAi ? "DCP: AI analysis skipped" : "DCP: Model fallback",
message: skipAi
? `${modelSelection.failedModel.providerID}/${modelSelection.failedModel.modelID} failed\nAI analysis skipped (strictModelSelection enabled)`
: `${modelSelection.failedModel.providerID}/${modelSelection.failedModel.modelID} failed\nUsing ${modelSelection.modelInfo.providerID}/${modelSelection.modelInfo.modelID}`,
variant: "info",
duration: 5000
}
})
} catch {
// Ignore toast errors
}
}
if (modelSelection.source === 'fallback' && config.strategies.onIdle.strictModelSelection) {
logger.info("Skipping AI analysis (fallback model, strictModelSelection enabled)")
return []
}
const { generateObject } = await import('ai')
const sanitizedMessages = replacePrunedToolOutputs(messages, alreadyPrunedIds)
const analysisPrompt = buildAnalysisPrompt(
prunableToolCallIds,
sanitizedMessages,
alreadyPrunedIds,
protectedToolCallIds
)
const result = await generateObject({
model: modelSelection.model,
schema: z.object({
pruned_tool_call_ids: z.array(z.string()),
reasoning: z.string(),
}),
prompt: analysisPrompt
})
const rawLlmPrunedIds = result.object.pruned_tool_call_ids
const llmPrunedIds = rawLlmPrunedIds.filter(id =>
prunableToolCallIds.includes(id)
)
// Always log LLM output as debug
const reasoning = result.object.reasoning.replace(/\n+/g, ' ').replace(/\s+/g, ' ').trim()
logger.debug(`OnIdle LLM output`, {
pruned_tool_call_ids: rawLlmPrunedIds,
reasoning: reasoning
})
return llmPrunedIds
}
/**
* Run the onIdle pruning strategy.
* This is called when the session transitions to idle state.
*/
export async function runOnIdle(
client: any,
state: SessionState,
logger: Logger,
config: PluginConfig,
workingDirectory?: string
): Promise<void | null> {
try {
if (!state.sessionId) {
return null
}
const sessionId = state.sessionId
// Fetch session info and messages
const [sessionInfoResponse, messagesResponse] = await Promise.all([
client.session.get({ path: { id: sessionId } }),
client.session.messages({ path: { id: sessionId }})
])
const sessionInfo = sessionInfoResponse.data
const messages: WithParts[] = messagesResponse.data || messagesResponse
if (!messages || messages.length < 3) {
return null
}
const currentParams = getCurrentParams(messages, logger)
const { toolCallIds, toolMetadata } = parseMessages(messages, state.toolParameters)
const alreadyPrunedIds = state.prune.toolIds
const unprunedToolCallIds = toolCallIds.filter(id => !alreadyPrunedIds.includes(id))
if (unprunedToolCallIds.length === 0) {
return null
}
// Count prunable tools (excluding protected)
const candidateCount = unprunedToolCallIds.filter(id => {
const metadata = toolMetadata.get(id)
return !metadata || !config.strategies.onIdle.protectedTools.includes(metadata.tool)
}).length
if (candidateCount === 0) {
return null
}
// Run LLM analysis
const llmPrunedIds = await runLlmAnalysis(
client,
state,
logger,
config,
messages,
unprunedToolCallIds,
alreadyPrunedIds,
toolMetadata,
workingDirectory
)
const newlyPrunedIds = llmPrunedIds.filter(id => !alreadyPrunedIds.includes(id))
if (newlyPrunedIds.length === 0) {
return null
}
// Log the tool IDs being pruned with their tool names
for (const id of newlyPrunedIds) {
const metadata = toolMetadata.get(id)
const toolName = metadata?.tool || 'unknown'
logger.info(`OnIdle pruning tool: ${toolName}`, { callID: id })
}
// Update state
const allPrunedIds = [...new Set([...alreadyPrunedIds, ...newlyPrunedIds])]
state.prune.toolIds = allPrunedIds
state.stats.pruneTokenCounter += calculateTokensSaved(messages, newlyPrunedIds)
// Build tool metadata map for notification
const prunedToolMetadata = new Map<string, ToolParameterEntry>()
for (const id of newlyPrunedIds) {
const metadata = toolMetadata.get(id)
if (metadata) {
prunedToolMetadata.set(id, metadata)
}
}
// Send notification
await sendUnifiedNotification(
client,
logger,
config,
state,
sessionId,
newlyPrunedIds,
prunedToolMetadata,
undefined, // reason
currentParams,
workingDirectory || ""
)
state.stats.totalPruneTokens += state.stats.pruneTokenCounter
state.stats.pruneTokenCounter = 0
state.nudgeCounter = 0
state.lastToolPrune = true
// Persist state
const sessionName = sessionInfo?.title
saveSessionState(state, logger, sessionName).catch(err => {
logger.error("Failed to persist state", { error: err.message })
})
logger.info(`OnIdle: Pruned ${newlyPrunedIds.length}/${candidateCount} tools`)
} catch (error: any) {
logger.error("OnIdle analysis failed", { error: error.message })
return null
}
}