-
-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathindex.ts
More file actions
273 lines (235 loc) · 11.4 KB
/
index.ts
File metadata and controls
273 lines (235 loc) · 11.4 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
// index.ts - Main plugin entry point for Dynamic Context Pruning
import type { Plugin } from "@opencode-ai/plugin"
import { tool } from "@opencode-ai/plugin"
import { getConfig } from "./lib/config"
import { Logger } from "./lib/logger"
import { Janitor, type SessionStats } from "./lib/janitor"
import { checkForUpdates } from "./lib/version-checker"
/**
* Checks if a session is a subagent (child session)
* Subagent sessions should skip pruning operations
*/
async function isSubagentSession(client: any, sessionID: string): Promise<boolean> {
try {
const result = await client.session.get({ path: { id: sessionID } })
return !!result.data?.parentID
} catch (error: any) {
// On error, assume it's not a subagent and continue (fail open)
return false
}
}
const plugin: Plugin = (async (ctx) => {
const { config, migrations } = getConfig(ctx)
// Exit early if plugin is disabled
if (!config.enabled) {
return {}
}
// Suppress AI SDK warnings about responseFormat (harmless for our use case)
if (typeof globalThis !== 'undefined') {
(globalThis as any).AI_SDK_LOG_WARNINGS = false
}
// Logger uses ~/.config/opencode/logs/dcp/ for consistent log location
const logger = new Logger(config.debug)
const prunedIdsState = new Map<string, string[]>()
const statsState = new Map<string, SessionStats>()
const toolParametersCache = new Map<string, any>() // callID -> parameters
const modelCache = new Map<string, { providerID: string; modelID: string }>() // sessionID -> model info
const janitor = new Janitor(ctx.client, prunedIdsState, statsState, logger, toolParametersCache, config.protectedTools, modelCache, config.model, config.showModelErrorToasts, config.pruning_summary, ctx.directory)
const cacheToolParameters = (messages: any[]) => {
for (const message of messages) {
if (message.role !== 'assistant' || !Array.isArray(message.tool_calls)) {
continue
}
for (const toolCall of message.tool_calls) {
if (!toolCall.id || !toolCall.function) {
continue
}
try {
const params = typeof toolCall.function.arguments === 'string'
? JSON.parse(toolCall.function.arguments)
: toolCall.function.arguments
toolParametersCache.set(toolCall.id, {
tool: toolCall.function.name,
parameters: params
})
} catch (error) {
// Ignore JSON parse errors for individual tool calls
}
}
}
}
// Global fetch wrapper that both caches tool parameters AND performs pruning
// This works because all providers ultimately call globalThis.fetch
const originalGlobalFetch = globalThis.fetch
globalThis.fetch = async (input: any, init?: any) => {
if (init?.body && typeof init.body === 'string') {
try {
const body = JSON.parse(init.body)
if (body.messages && Array.isArray(body.messages)) {
// Cache tool parameters for janitor metadata
cacheToolParameters(body.messages)
// Check for tool messages that might need pruning
const toolMessages = body.messages.filter((m: any) => m.role === 'tool')
// Collect all pruned IDs across all sessions (excluding subagents)
// This is safe because tool_call_ids are globally unique
const allSessions = await ctx.client.session.list()
const allPrunedIds = new Set<string>()
if (allSessions.data) {
for (const session of allSessions.data) {
if (session.parentID) continue // Skip subagent sessions
const prunedIds = prunedIdsState.get(session.id) ?? []
prunedIds.forEach((id: string) => allPrunedIds.add(id))
}
}
// Only process tool message replacement if there are tool messages and pruned IDs
if (toolMessages.length > 0 && allPrunedIds.size > 0) {
let replacedCount = 0
body.messages = body.messages.map((m: any) => {
// Normalize ID to lowercase for case-insensitive matching
if (m.role === 'tool' && allPrunedIds.has(m.tool_call_id?.toLowerCase())) {
replacedCount++
return {
...m,
content: '[Output removed to save context - information superseded or no longer needed]'
}
}
return m
})
if (replacedCount > 0) {
logger.info("fetch", "Replaced pruned tool outputs", {
replaced: replacedCount,
total: toolMessages.length
})
// Save wrapped context to file if debug is enabled
if (logger.enabled) {
await logger.saveWrappedContext(
"global",
body.messages,
{
url: typeof input === 'string' ? input : 'URL object',
replacedCount,
totalMessages: body.messages.length
}
)
}
// Update the request body with modified messages
init.body = JSON.stringify(body)
}
}
}
} catch (e) {
// Ignore parse errors and fall through to original fetch
}
}
return originalGlobalFetch(input, init)
}
logger.info("plugin", "DCP initialized", {
strategies: config.strategies,
model: config.model || "auto"
})
// Check for updates on launch (fire and forget)
checkForUpdates(ctx.client, logger).catch(() => {})
// Show migration toast if config was migrated (delayed to not overlap with version toast)
if (migrations.length > 0) {
setTimeout(async () => {
try {
await ctx.client.tui.showToast({
body: {
title: "DCP: Config upgraded",
message: migrations.join('\n'),
variant: "info",
duration: 8000
}
})
} catch {
// Silently fail - toast is non-critical
}
}, 7000) // 7s delay to show after version toast (6s) completes
}
return {
/**
* Event Hook: Triggers janitor analysis when session becomes idle
*/
event: async ({ event }) => {
if (event.type === "session.status" && event.properties.status.type === "idle") {
// Skip pruning for subagent sessions
if (await isSubagentSession(ctx.client, event.properties.sessionID)) return
// Skip if no idle strategies configured
if (config.strategies.onIdle.length === 0) return
// Fire and forget the janitor - don't block the event handler
janitor.runOnIdle(event.properties.sessionID, config.strategies.onIdle).catch(err => {
logger.error("janitor", "Failed", { error: err.message })
})
}
},
/**
* Chat Params Hook: Caches model info for janitor
*/
"chat.params": async (input, _output) => {
const sessionId = input.sessionID
// Cache model information for this session so janitor can access it
// The provider.id is actually nested at provider.info.id (not in SDK types)
let providerID = (input.provider as any)?.info?.id || input.provider?.id
const modelID = input.model?.id
// If provider.id is not available, try to get it from the message
if (!providerID && input.message?.model?.providerID) {
providerID = input.message.model.providerID
}
if (providerID && modelID) {
modelCache.set(sessionId, {
providerID: providerID,
modelID: modelID
})
}
},
/**
* Tool Hook: Exposes context_pruning tool to AI (if configured)
*/
tool: config.strategies.onTool.length > 0 ? {
context_pruning: tool({
description: `Performs semantic pruning on session tool outputs that are no longer relevant to the current task. Use this to declutter the conversation context and filter signal from noise when you notice the context is getting cluttered with outdated information.
## When to Use This Tool
- After completing a debugging session or fixing a bug
- When switching focus to a new task or feature
- After exploring multiple files that didn't lead to changes
- When you've been iterating on a difficult problem and some approaches didn't pan out
- When old file reads, greps, or bash outputs are no longer relevant
## Examples
<example>
Working through a list of bugs to fix:
User: Please fix these 5 type errors in the codebase.
Assistant: I'll work through each error. [Fixes first error]
First error fixed. Let me prune the debugging context before moving to the next one.
[Uses context_pruning with reason: "first bug fixed, moving to next task"]
</example>
<example>
After exploring the codebase to understand it:
Assistant: I've reviewed the relevant files. Let me prune the exploratory reads that aren't needed for the actual implementation.
[Uses context_pruning with reason: "exploration complete, pruning unrelated file reads"]
</example>
<example>
After trying multiple approaches that didn't work:
Assistant: I've been trying several approaches to fix this issue. Let me prune the failed attempts to keep focus on the working solution.
[Uses context_pruning with reason: "pruning failed iteration attempts, keeping working solution context"]
</example>`,
args: {
reason: tool.schema.string().optional().describe(
"Brief reason for triggering pruning (e.g., 'task complete', 'switching focus')"
),
},
async execute(args, ctx) {
const result = await janitor.runForTool(
ctx.sessionID,
config.strategies.onTool,
args.reason
)
if (!result || result.prunedCount === 0) {
return "No prunable tool outputs found. Context is already optimized."
}
return janitor.formatPruningResultForTool(result)
},
}),
} : undefined,
}
}) satisfies Plugin
export default plugin