-
-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathconfig.ts
More file actions
253 lines (230 loc) · 9.27 KB
/
config.ts
File metadata and controls
253 lines (230 loc) · 9.27 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
import { readFileSync, writeFileSync, existsSync, mkdirSync, statSync, copyFileSync } from 'fs'
import { join, dirname } from 'path'
import { homedir } from 'os'
import { parse } from 'jsonc-parser'
import { Logger } from './logger'
import type { PluginInput } from '@opencode-ai/plugin'
export type PruningStrategy = "deduplication" | "ai-analysis"
export interface PluginConfig {
enabled: boolean
debug: boolean
protectedTools: string[]
model?: string
showModelErrorToasts?: boolean
showUpdateToasts?: boolean
strictModelSelection?: boolean
pruning_summary: "off" | "minimal" | "detailed"
nudge_freq: number
strategies: {
onIdle: PruningStrategy[]
onTool: PruningStrategy[]
}
}
export interface ConfigResult {
config: PluginConfig
migrations: string[]
}
const defaultConfig: PluginConfig = {
enabled: true,
debug: false,
protectedTools: ['task', 'todowrite', 'todoread', 'prune', 'batch', 'write', 'edit'],
showModelErrorToasts: true,
showUpdateToasts: true,
strictModelSelection: false,
pruning_summary: 'detailed',
nudge_freq: 10,
strategies: {
onIdle: ['ai-analysis'],
onTool: ['ai-analysis']
}
}
const VALID_CONFIG_KEYS = new Set([
'enabled',
'debug',
'protectedTools',
'model',
'showModelErrorToasts',
'showUpdateToasts',
'strictModelSelection',
'pruning_summary',
'nudge_freq',
'strategies'
])
const GLOBAL_CONFIG_DIR = join(homedir(), '.config', 'opencode')
const GLOBAL_CONFIG_PATH_JSONC = join(GLOBAL_CONFIG_DIR, 'dcp.jsonc')
const GLOBAL_CONFIG_PATH_JSON = join(GLOBAL_CONFIG_DIR, 'dcp.json')
function findOpencodeDir(startDir: string): string | null {
let current = startDir
while (current !== '/') {
const candidate = join(current, '.opencode')
if (existsSync(candidate) && statSync(candidate).isDirectory()) {
return candidate
}
const parent = dirname(current)
if (parent === current) break
current = parent
}
return null
}
function getConfigPaths(ctx?: PluginInput): { global: string | null, project: string | null } {
let globalPath: string | null = null
if (existsSync(GLOBAL_CONFIG_PATH_JSONC)) {
globalPath = GLOBAL_CONFIG_PATH_JSONC
} else if (existsSync(GLOBAL_CONFIG_PATH_JSON)) {
globalPath = GLOBAL_CONFIG_PATH_JSON
}
let projectPath: string | null = null
if (ctx?.directory) {
const opencodeDir = findOpencodeDir(ctx.directory)
if (opencodeDir) {
const projectJsonc = join(opencodeDir, 'dcp.jsonc')
const projectJson = join(opencodeDir, 'dcp.json')
if (existsSync(projectJsonc)) {
projectPath = projectJsonc
} else if (existsSync(projectJson)) {
projectPath = projectJson
}
}
}
return { global: globalPath, project: projectPath }
}
function createDefaultConfig(): void {
if (!existsSync(GLOBAL_CONFIG_DIR)) {
mkdirSync(GLOBAL_CONFIG_DIR, { recursive: true })
}
const configContent = `{
// Enable or disable the plugin
"enabled": true,
// Enable debug logging to ~/.config/opencode/logs/dcp/
"debug": false,
// Override model for analysis (format: "provider/model", e.g. "anthropic/claude-haiku-4-5")
// "model": "anthropic/claude-haiku-4-5",
// Show toast notifications when model selection fails
"showModelErrorToasts": true,
// Show toast notifications when a new version is available
"showUpdateToasts": true,
// Only run AI analysis with session model or configured model (disables fallback models)
"strictModelSelection": false,
// AI analysis strategies (deduplication runs automatically on every request)
"strategies": {
// Strategies to run when session goes idle
"onIdle": ["ai-analysis"],
// Strategies to run when AI calls prune tool
"onTool": ["ai-analysis"]
},
// Summary display: "off", "minimal", or "detailed"
"pruning_summary": "detailed",
// How often to nudge the AI to prune (every N tool results, 0 = disabled)
"nudge_freq": 10
// Additional tools to protect from pruning
// "protectedTools": ["bash"]
}
`
writeFileSync(GLOBAL_CONFIG_PATH_JSONC, configContent, 'utf-8')
}
function loadConfigFile(configPath: string): Record<string, any> | null {
try {
const fileContent = readFileSync(configPath, 'utf-8')
return parse(fileContent)
} catch (error: any) {
return null
}
}
function getInvalidKeys(config: Record<string, any>): string[] {
const invalidKeys: string[] = []
for (const key of Object.keys(config)) {
if (!VALID_CONFIG_KEYS.has(key)) {
invalidKeys.push(key)
}
}
return invalidKeys
}
function backupAndResetConfig(configPath: string, logger: Logger): string | null {
try {
const backupPath = configPath + '.bak'
copyFileSync(configPath, backupPath)
logger.info('config', 'Created config backup', { backup: backupPath })
createDefaultConfig()
logger.info('config', 'Created fresh default config', { path: GLOBAL_CONFIG_PATH_JSONC })
return backupPath
} catch (error: any) {
logger.error('config', 'Failed to backup/reset config', { error: error.message })
return null
}
}
function mergeStrategies(
base: PluginConfig['strategies'],
override?: Partial<PluginConfig['strategies']>
): PluginConfig['strategies'] {
if (!override) return base
return {
onIdle: override.onIdle ?? base.onIdle,
onTool: override.onTool ?? base.onTool
}
}
export function getConfig(ctx?: PluginInput): ConfigResult {
let config = { ...defaultConfig, protectedTools: [...defaultConfig.protectedTools] }
const configPaths = getConfigPaths(ctx)
const logger = new Logger(true)
const migrations: string[] = []
if (configPaths.global) {
const globalConfig = loadConfigFile(configPaths.global)
if (globalConfig) {
const invalidKeys = getInvalidKeys(globalConfig)
if (invalidKeys.length > 0) {
logger.info('config', 'Found invalid config keys', { keys: invalidKeys })
const backupPath = backupAndResetConfig(configPaths.global, logger)
if (backupPath) {
migrations.push(`Old config backed up to ${backupPath}`)
}
} else {
config = {
enabled: globalConfig.enabled ?? config.enabled,
debug: globalConfig.debug ?? config.debug,
protectedTools: [...new Set([...config.protectedTools, ...(globalConfig.protectedTools ?? [])])],
model: globalConfig.model ?? config.model,
showModelErrorToasts: globalConfig.showModelErrorToasts ?? config.showModelErrorToasts,
showUpdateToasts: globalConfig.showUpdateToasts ?? config.showUpdateToasts,
strictModelSelection: globalConfig.strictModelSelection ?? config.strictModelSelection,
strategies: mergeStrategies(config.strategies, globalConfig.strategies as any),
pruning_summary: globalConfig.pruning_summary ?? config.pruning_summary,
nudge_freq: globalConfig.nudge_freq ?? config.nudge_freq
}
logger.info('config', 'Loaded global config', { path: configPaths.global })
}
}
} else {
createDefaultConfig()
logger.info('config', 'Created default global config', { path: GLOBAL_CONFIG_PATH_JSONC })
}
if (configPaths.project) {
const projectConfig = loadConfigFile(configPaths.project)
if (projectConfig) {
const invalidKeys = getInvalidKeys(projectConfig)
if (invalidKeys.length > 0) {
logger.warn('config', 'Project config has invalid keys (ignored)', {
path: configPaths.project,
keys: invalidKeys
})
migrations.push(`Project config has invalid keys: ${invalidKeys.join(', ')}`)
} else {
config = {
enabled: projectConfig.enabled ?? config.enabled,
debug: projectConfig.debug ?? config.debug,
protectedTools: [...new Set([...config.protectedTools, ...(projectConfig.protectedTools ?? [])])],
model: projectConfig.model ?? config.model,
showModelErrorToasts: projectConfig.showModelErrorToasts ?? config.showModelErrorToasts,
showUpdateToasts: projectConfig.showUpdateToasts ?? config.showUpdateToasts,
strictModelSelection: projectConfig.strictModelSelection ?? config.strictModelSelection,
strategies: mergeStrategies(config.strategies, projectConfig.strategies as any),
pruning_summary: projectConfig.pruning_summary ?? config.pruning_summary,
nudge_freq: projectConfig.nudge_freq ?? config.nudge_freq
}
logger.info('config', 'Loaded project config (overrides global)', { path: configPaths.project })
}
}
} else if (ctx?.directory) {
logger.debug('config', 'No project config found', { searchedFrom: ctx.directory })
}
return { config, migrations }
}