@tuanhung303/opencode-dcp 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (159) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +249 -0
  3. package/dist/index.d.ts +4 -0
  4. package/dist/index.d.ts.map +1 -0
  5. package/dist/index.js +71 -0
  6. package/dist/index.js.map +1 -0
  7. package/dist/lib/commands/context.d.ts +49 -0
  8. package/dist/lib/commands/context.d.ts.map +1 -0
  9. package/dist/lib/commands/context.js +191 -0
  10. package/dist/lib/commands/context.js.map +1 -0
  11. package/dist/lib/commands/help.d.ts +15 -0
  12. package/dist/lib/commands/help.d.ts.map +1 -0
  13. package/dist/lib/commands/help.js +26 -0
  14. package/dist/lib/commands/help.js.map +1 -0
  15. package/dist/lib/commands/stats.d.ts +15 -0
  16. package/dist/lib/commands/stats.d.ts.map +1 -0
  17. package/dist/lib/commands/stats.js +44 -0
  18. package/dist/lib/commands/stats.js.map +1 -0
  19. package/dist/lib/commands/sweep.d.ts +23 -0
  20. package/dist/lib/commands/sweep.d.ts.map +1 -0
  21. package/dist/lib/commands/sweep.js +191 -0
  22. package/dist/lib/commands/sweep.js.map +1 -0
  23. package/dist/lib/config.d.ts +69 -0
  24. package/dist/lib/config.d.ts.map +1 -0
  25. package/dist/lib/config.js +754 -0
  26. package/dist/lib/config.js.map +1 -0
  27. package/dist/lib/hooks.d.ts +17 -0
  28. package/dist/lib/hooks.d.ts.map +1 -0
  29. package/dist/lib/hooks.js +121 -0
  30. package/dist/lib/hooks.js.map +1 -0
  31. package/dist/lib/logger.d.ts +31 -0
  32. package/dist/lib/logger.d.ts.map +1 -0
  33. package/dist/lib/logger.js +189 -0
  34. package/dist/lib/logger.js.map +1 -0
  35. package/dist/lib/messages/index.d.ts +3 -0
  36. package/dist/lib/messages/index.d.ts.map +1 -0
  37. package/dist/lib/messages/index.js +3 -0
  38. package/dist/lib/messages/index.js.map +1 -0
  39. package/dist/lib/messages/inject.d.ts +5 -0
  40. package/dist/lib/messages/inject.d.ts.map +1 -0
  41. package/dist/lib/messages/inject.js +127 -0
  42. package/dist/lib/messages/inject.js.map +1 -0
  43. package/dist/lib/messages/prune.d.ts +5 -0
  44. package/dist/lib/messages/prune.d.ts.map +1 -0
  45. package/dist/lib/messages/prune.js +86 -0
  46. package/dist/lib/messages/prune.js.map +1 -0
  47. package/dist/lib/messages/utils.d.ts +31 -0
  48. package/dist/lib/messages/utils.d.ts.map +1 -0
  49. package/dist/lib/messages/utils.js +228 -0
  50. package/dist/lib/messages/utils.js.map +1 -0
  51. package/dist/lib/prompts/discard-tool-spec.d.ts +2 -0
  52. package/dist/lib/prompts/discard-tool-spec.d.ts.map +1 -0
  53. package/dist/lib/prompts/discard-tool-spec.js +41 -0
  54. package/dist/lib/prompts/discard-tool-spec.js.map +1 -0
  55. package/dist/lib/prompts/extract-tool-spec.d.ts +2 -0
  56. package/dist/lib/prompts/extract-tool-spec.d.ts.map +1 -0
  57. package/dist/lib/prompts/extract-tool-spec.js +48 -0
  58. package/dist/lib/prompts/extract-tool-spec.js.map +1 -0
  59. package/dist/lib/prompts/index.d.ts +2 -0
  60. package/dist/lib/prompts/index.d.ts.map +1 -0
  61. package/dist/lib/prompts/index.js +34 -0
  62. package/dist/lib/prompts/index.js.map +1 -0
  63. package/dist/lib/prompts/nudge/both.d.ts +2 -0
  64. package/dist/lib/prompts/nudge/both.d.ts.map +1 -0
  65. package/dist/lib/prompts/nudge/both.js +11 -0
  66. package/dist/lib/prompts/nudge/both.js.map +1 -0
  67. package/dist/lib/prompts/nudge/discard.d.ts +2 -0
  68. package/dist/lib/prompts/nudge/discard.d.ts.map +1 -0
  69. package/dist/lib/prompts/nudge/discard.js +10 -0
  70. package/dist/lib/prompts/nudge/discard.js.map +1 -0
  71. package/dist/lib/prompts/nudge/extract.d.ts +2 -0
  72. package/dist/lib/prompts/nudge/extract.d.ts.map +1 -0
  73. package/dist/lib/prompts/nudge/extract.js +10 -0
  74. package/dist/lib/prompts/nudge/extract.js.map +1 -0
  75. package/dist/lib/prompts/system/both.d.ts +2 -0
  76. package/dist/lib/prompts/system/both.d.ts.map +1 -0
  77. package/dist/lib/prompts/system/both.js +61 -0
  78. package/dist/lib/prompts/system/both.js.map +1 -0
  79. package/dist/lib/prompts/system/discard.d.ts +2 -0
  80. package/dist/lib/prompts/system/discard.d.ts.map +1 -0
  81. package/dist/lib/prompts/system/discard.js +52 -0
  82. package/dist/lib/prompts/system/discard.js.map +1 -0
  83. package/dist/lib/prompts/system/extract.d.ts +2 -0
  84. package/dist/lib/prompts/system/extract.d.ts.map +1 -0
  85. package/dist/lib/prompts/system/extract.js +52 -0
  86. package/dist/lib/prompts/system/extract.js.map +1 -0
  87. package/dist/lib/protected-file-patterns.d.ts +12 -0
  88. package/dist/lib/protected-file-patterns.d.ts.map +1 -0
  89. package/dist/lib/protected-file-patterns.js +69 -0
  90. package/dist/lib/protected-file-patterns.js.map +1 -0
  91. package/dist/lib/shared-utils.d.ts +4 -0
  92. package/dist/lib/shared-utils.d.ts.map +1 -0
  93. package/dist/lib/shared-utils.js +14 -0
  94. package/dist/lib/shared-utils.js.map +1 -0
  95. package/dist/lib/state/index.d.ts +4 -0
  96. package/dist/lib/state/index.d.ts.map +1 -0
  97. package/dist/lib/state/index.js +4 -0
  98. package/dist/lib/state/index.js.map +1 -0
  99. package/dist/lib/state/persistence.d.ts +22 -0
  100. package/dist/lib/state/persistence.d.ts.map +1 -0
  101. package/dist/lib/state/persistence.js +107 -0
  102. package/dist/lib/state/persistence.js.map +1 -0
  103. package/dist/lib/state/state.d.ts +8 -0
  104. package/dist/lib/state/state.d.ts.map +1 -0
  105. package/dist/lib/state/state.js +115 -0
  106. package/dist/lib/state/state.js.map +1 -0
  107. package/dist/lib/state/tool-cache.d.ts +13 -0
  108. package/dist/lib/state/tool-cache.d.ts.map +1 -0
  109. package/dist/lib/state/tool-cache.js +77 -0
  110. package/dist/lib/state/tool-cache.js.map +1 -0
  111. package/dist/lib/state/types.d.ts +33 -0
  112. package/dist/lib/state/types.d.ts.map +1 -0
  113. package/dist/lib/state/types.js +2 -0
  114. package/dist/lib/state/types.js.map +1 -0
  115. package/dist/lib/state/utils.d.ts +2 -0
  116. package/dist/lib/state/utils.d.ts.map +1 -0
  117. package/dist/lib/state/utils.js +10 -0
  118. package/dist/lib/state/utils.js.map +1 -0
  119. package/dist/lib/strategies/deduplication.d.ts +10 -0
  120. package/dist/lib/strategies/deduplication.d.ts.map +1 -0
  121. package/dist/lib/strategies/deduplication.js +94 -0
  122. package/dist/lib/strategies/deduplication.js.map +1 -0
  123. package/dist/lib/strategies/index.d.ts +7 -0
  124. package/dist/lib/strategies/index.d.ts.map +1 -0
  125. package/dist/lib/strategies/index.js +7 -0
  126. package/dist/lib/strategies/index.js.map +1 -0
  127. package/dist/lib/strategies/placeholder-compression.d.ts +5 -0
  128. package/dist/lib/strategies/placeholder-compression.d.ts.map +1 -0
  129. package/dist/lib/strategies/placeholder-compression.js +148 -0
  130. package/dist/lib/strategies/placeholder-compression.js.map +1 -0
  131. package/dist/lib/strategies/prune-thinking.d.ts +15 -0
  132. package/dist/lib/strategies/prune-thinking.d.ts.map +1 -0
  133. package/dist/lib/strategies/prune-thinking.js +79 -0
  134. package/dist/lib/strategies/prune-thinking.js.map +1 -0
  135. package/dist/lib/strategies/purge-errors.d.ts +13 -0
  136. package/dist/lib/strategies/purge-errors.d.ts.map +1 -0
  137. package/dist/lib/strategies/purge-errors.js +59 -0
  138. package/dist/lib/strategies/purge-errors.js.map +1 -0
  139. package/dist/lib/strategies/supersede-writes.d.ts +13 -0
  140. package/dist/lib/strategies/supersede-writes.d.ts.map +1 -0
  141. package/dist/lib/strategies/supersede-writes.js +84 -0
  142. package/dist/lib/strategies/supersede-writes.js.map +1 -0
  143. package/dist/lib/strategies/tools.d.ts +14 -0
  144. package/dist/lib/strategies/tools.d.ts.map +1 -0
  145. package/dist/lib/strategies/tools.js +135 -0
  146. package/dist/lib/strategies/tools.js.map +1 -0
  147. package/dist/lib/strategies/utils.d.ts +11 -0
  148. package/dist/lib/strategies/utils.d.ts.map +1 -0
  149. package/dist/lib/strategies/utils.js +75 -0
  150. package/dist/lib/strategies/utils.js.map +1 -0
  151. package/dist/lib/ui/notification.d.ts +9 -0
  152. package/dist/lib/ui/notification.d.ts.map +1 -0
  153. package/dist/lib/ui/notification.js +77 -0
  154. package/dist/lib/ui/notification.js.map +1 -0
  155. package/dist/lib/ui/utils.d.ts +10 -0
  156. package/dist/lib/ui/utils.d.ts.map +1 -0
  157. package/dist/lib/ui/utils.js +87 -0
  158. package/dist/lib/ui/utils.js.map +1 -0
  159. package/package.json +63 -0
@@ -0,0 +1,754 @@
1
+ import { readFileSync, writeFileSync, existsSync, mkdirSync, statSync } from "fs";
2
+ import { join, dirname } from "path";
3
+ import { homedir } from "os";
4
+ import { parse } from "jsonc-parser";
5
+ const DEFAULT_PROTECTED_TOOLS = [
6
+ "task",
7
+ "todowrite",
8
+ "todoread",
9
+ "discard",
10
+ "extract",
11
+ "batch",
12
+ "write",
13
+ "edit",
14
+ "plan_enter",
15
+ "plan_exit",
16
+ ];
17
+ // Valid config keys for validation against user config
18
+ export const VALID_CONFIG_KEYS = new Set([
19
+ // Top-level keys
20
+ "$schema",
21
+ "enabled",
22
+ "debug",
23
+ "showUpdateToasts", // Deprecated but kept for backwards compatibility
24
+ "pruneNotification",
25
+ "turnProtection",
26
+ "turnProtection.enabled",
27
+ "turnProtection.turns",
28
+ "protectedFilePatterns",
29
+ "commands",
30
+ "commands.enabled",
31
+ "commands.protectedTools",
32
+ "tools",
33
+ "tools.settings",
34
+ "tools.settings.nudgeEnabled",
35
+ "tools.settings.nudgeFrequency",
36
+ "tools.settings.protectedTools",
37
+ "tools.discard",
38
+ "tools.discard.enabled",
39
+ "tools.extract",
40
+ "tools.extract.enabled",
41
+ "tools.extract.showDistillation",
42
+ "strategies",
43
+ // strategies.deduplication
44
+ "strategies.deduplication",
45
+ "strategies.deduplication.enabled",
46
+ "strategies.deduplication.protectedTools",
47
+ // strategies.supersedeWrites
48
+ "strategies.supersedeWrites",
49
+ "strategies.supersedeWrites.enabled",
50
+ // strategies.purgeErrors
51
+ "strategies.purgeErrors",
52
+ "strategies.purgeErrors.enabled",
53
+ "strategies.purgeErrors.turns",
54
+ "strategies.purgeErrors.protectedTools",
55
+ // strategies.pruneThinking
56
+ "strategies.pruneThinking",
57
+ "strategies.pruneThinking.enabled",
58
+ "strategies.pruneThinking.delayTurns",
59
+ // strategies.placeholderCompression
60
+ "strategies.placeholderCompression",
61
+ "strategies.placeholderCompression.enabled",
62
+ "strategies.placeholderCompression.delayTurns",
63
+ "strategies.placeholderCompression.minOutputTokens",
64
+ "strategies.placeholderCompression.protectedTools",
65
+ ]);
66
+ // Extract all key paths from a config object for validation
67
+ function getConfigKeyPaths(obj, prefix = "") {
68
+ const keys = [];
69
+ for (const key of Object.keys(obj)) {
70
+ const fullKey = prefix ? `${prefix}.${key}` : key;
71
+ keys.push(fullKey);
72
+ if (obj[key] && typeof obj[key] === "object" && !Array.isArray(obj[key])) {
73
+ keys.push(...getConfigKeyPaths(obj[key], fullKey));
74
+ }
75
+ }
76
+ return keys;
77
+ }
78
+ // Returns invalid keys found in user config
79
+ export function getInvalidConfigKeys(userConfig) {
80
+ const userKeys = getConfigKeyPaths(userConfig);
81
+ return userKeys.filter((key) => !VALID_CONFIG_KEYS.has(key));
82
+ }
83
+ function validateConfigTypes(config) {
84
+ const errors = [];
85
+ // Top-level validators
86
+ if (config.enabled !== undefined && typeof config.enabled !== "boolean") {
87
+ errors.push({ key: "enabled", expected: "boolean", actual: typeof config.enabled });
88
+ }
89
+ if (config.debug !== undefined && typeof config.debug !== "boolean") {
90
+ errors.push({ key: "debug", expected: "boolean", actual: typeof config.debug });
91
+ }
92
+ if (config.pruneNotification !== undefined) {
93
+ const validValues = ["off", "minimal", "detailed"];
94
+ if (!validValues.includes(config.pruneNotification)) {
95
+ errors.push({
96
+ key: "pruneNotification",
97
+ expected: '"off" | "minimal" | "detailed"',
98
+ actual: JSON.stringify(config.pruneNotification),
99
+ });
100
+ }
101
+ }
102
+ if (config.protectedFilePatterns !== undefined) {
103
+ if (!Array.isArray(config.protectedFilePatterns)) {
104
+ errors.push({
105
+ key: "protectedFilePatterns",
106
+ expected: "string[]",
107
+ actual: typeof config.protectedFilePatterns,
108
+ });
109
+ }
110
+ else if (!config.protectedFilePatterns.every((v) => typeof v === "string")) {
111
+ errors.push({
112
+ key: "protectedFilePatterns",
113
+ expected: "string[]",
114
+ actual: "non-string entries",
115
+ });
116
+ }
117
+ }
118
+ // Top-level turnProtection validator
119
+ if (config.turnProtection) {
120
+ if (config.turnProtection.enabled !== undefined &&
121
+ typeof config.turnProtection.enabled !== "boolean") {
122
+ errors.push({
123
+ key: "turnProtection.enabled",
124
+ expected: "boolean",
125
+ actual: typeof config.turnProtection.enabled,
126
+ });
127
+ }
128
+ if (config.turnProtection.turns !== undefined &&
129
+ typeof config.turnProtection.turns !== "number") {
130
+ errors.push({
131
+ key: "turnProtection.turns",
132
+ expected: "number",
133
+ actual: typeof config.turnProtection.turns,
134
+ });
135
+ }
136
+ }
137
+ // Commands validator
138
+ const commands = config.commands;
139
+ if (commands !== undefined) {
140
+ if (typeof commands === "object") {
141
+ if (commands.enabled !== undefined && typeof commands.enabled !== "boolean") {
142
+ errors.push({
143
+ key: "commands.enabled",
144
+ expected: "boolean",
145
+ actual: typeof commands.enabled,
146
+ });
147
+ }
148
+ if (commands.protectedTools !== undefined && !Array.isArray(commands.protectedTools)) {
149
+ errors.push({
150
+ key: "commands.protectedTools",
151
+ expected: "string[]",
152
+ actual: typeof commands.protectedTools,
153
+ });
154
+ }
155
+ }
156
+ else {
157
+ errors.push({
158
+ key: "commands",
159
+ expected: "{ enabled: boolean, protectedTools: string[] }",
160
+ actual: typeof commands,
161
+ });
162
+ }
163
+ }
164
+ // Tools validators
165
+ const tools = config.tools;
166
+ if (tools) {
167
+ if (tools.settings) {
168
+ if (tools.settings.nudgeEnabled !== undefined &&
169
+ typeof tools.settings.nudgeEnabled !== "boolean") {
170
+ errors.push({
171
+ key: "tools.settings.nudgeEnabled",
172
+ expected: "boolean",
173
+ actual: typeof tools.settings.nudgeEnabled,
174
+ });
175
+ }
176
+ if (tools.settings.nudgeFrequency !== undefined &&
177
+ typeof tools.settings.nudgeFrequency !== "number") {
178
+ errors.push({
179
+ key: "tools.settings.nudgeFrequency",
180
+ expected: "number",
181
+ actual: typeof tools.settings.nudgeFrequency,
182
+ });
183
+ }
184
+ if (tools.settings.protectedTools !== undefined &&
185
+ !Array.isArray(tools.settings.protectedTools)) {
186
+ errors.push({
187
+ key: "tools.settings.protectedTools",
188
+ expected: "string[]",
189
+ actual: typeof tools.settings.protectedTools,
190
+ });
191
+ }
192
+ }
193
+ if (tools.discard) {
194
+ if (tools.discard.enabled !== undefined && typeof tools.discard.enabled !== "boolean") {
195
+ errors.push({
196
+ key: "tools.discard.enabled",
197
+ expected: "boolean",
198
+ actual: typeof tools.discard.enabled,
199
+ });
200
+ }
201
+ }
202
+ if (tools.extract) {
203
+ if (tools.extract.enabled !== undefined && typeof tools.extract.enabled !== "boolean") {
204
+ errors.push({
205
+ key: "tools.extract.enabled",
206
+ expected: "boolean",
207
+ actual: typeof tools.extract.enabled,
208
+ });
209
+ }
210
+ if (tools.extract.showDistillation !== undefined &&
211
+ typeof tools.extract.showDistillation !== "boolean") {
212
+ errors.push({
213
+ key: "tools.extract.showDistillation",
214
+ expected: "boolean",
215
+ actual: typeof tools.extract.showDistillation,
216
+ });
217
+ }
218
+ }
219
+ }
220
+ // Strategies validators
221
+ const strategies = config.strategies;
222
+ if (strategies) {
223
+ // deduplication
224
+ if (strategies.deduplication?.enabled !== undefined &&
225
+ typeof strategies.deduplication.enabled !== "boolean") {
226
+ errors.push({
227
+ key: "strategies.deduplication.enabled",
228
+ expected: "boolean",
229
+ actual: typeof strategies.deduplication.enabled,
230
+ });
231
+ }
232
+ if (strategies.deduplication?.protectedTools !== undefined &&
233
+ !Array.isArray(strategies.deduplication.protectedTools)) {
234
+ errors.push({
235
+ key: "strategies.deduplication.protectedTools",
236
+ expected: "string[]",
237
+ actual: typeof strategies.deduplication.protectedTools,
238
+ });
239
+ }
240
+ // supersedeWrites
241
+ if (strategies.supersedeWrites) {
242
+ if (strategies.supersedeWrites.enabled !== undefined &&
243
+ typeof strategies.supersedeWrites.enabled !== "boolean") {
244
+ errors.push({
245
+ key: "strategies.supersedeWrites.enabled",
246
+ expected: "boolean",
247
+ actual: typeof strategies.supersedeWrites.enabled,
248
+ });
249
+ }
250
+ }
251
+ // purgeErrors
252
+ if (strategies.purgeErrors) {
253
+ if (strategies.purgeErrors.enabled !== undefined &&
254
+ typeof strategies.purgeErrors.enabled !== "boolean") {
255
+ errors.push({
256
+ key: "strategies.purgeErrors.enabled",
257
+ expected: "boolean",
258
+ actual: typeof strategies.purgeErrors.enabled,
259
+ });
260
+ }
261
+ if (strategies.purgeErrors.turns !== undefined &&
262
+ typeof strategies.purgeErrors.turns !== "number") {
263
+ errors.push({
264
+ key: "strategies.purgeErrors.turns",
265
+ expected: "number",
266
+ actual: typeof strategies.purgeErrors.turns,
267
+ });
268
+ }
269
+ if (strategies.purgeErrors.protectedTools !== undefined &&
270
+ !Array.isArray(strategies.purgeErrors.protectedTools)) {
271
+ errors.push({
272
+ key: "strategies.purgeErrors.protectedTools",
273
+ expected: "string[]",
274
+ actual: typeof strategies.purgeErrors.protectedTools,
275
+ });
276
+ }
277
+ }
278
+ // pruneThinking
279
+ if (strategies.pruneThinking) {
280
+ if (strategies.pruneThinking.enabled !== undefined &&
281
+ typeof strategies.pruneThinking.enabled !== "boolean") {
282
+ errors.push({
283
+ key: "strategies.pruneThinking.enabled",
284
+ expected: "boolean",
285
+ actual: typeof strategies.pruneThinking.enabled,
286
+ });
287
+ }
288
+ if (strategies.pruneThinking.delayTurns !== undefined &&
289
+ typeof strategies.pruneThinking.delayTurns !== "number") {
290
+ errors.push({
291
+ key: "strategies.pruneThinking.delayTurns",
292
+ expected: "number",
293
+ actual: typeof strategies.pruneThinking.delayTurns,
294
+ });
295
+ }
296
+ }
297
+ // placeholderCompression
298
+ if (strategies.placeholderCompression) {
299
+ if (strategies.placeholderCompression.enabled !== undefined &&
300
+ typeof strategies.placeholderCompression.enabled !== "boolean") {
301
+ errors.push({
302
+ key: "strategies.placeholderCompression.enabled",
303
+ expected: "boolean",
304
+ actual: typeof strategies.placeholderCompression.enabled,
305
+ });
306
+ }
307
+ if (strategies.placeholderCompression.delayTurns !== undefined &&
308
+ typeof strategies.placeholderCompression.delayTurns !== "number") {
309
+ errors.push({
310
+ key: "strategies.placeholderCompression.delayTurns",
311
+ expected: "number",
312
+ actual: typeof strategies.placeholderCompression.delayTurns,
313
+ });
314
+ }
315
+ if (strategies.placeholderCompression.minOutputTokens !== undefined &&
316
+ typeof strategies.placeholderCompression.minOutputTokens !== "number") {
317
+ errors.push({
318
+ key: "strategies.placeholderCompression.minOutputTokens",
319
+ expected: "number",
320
+ actual: typeof strategies.placeholderCompression.minOutputTokens,
321
+ });
322
+ }
323
+ if (strategies.placeholderCompression.protectedTools !== undefined &&
324
+ !Array.isArray(strategies.placeholderCompression.protectedTools)) {
325
+ errors.push({
326
+ key: "strategies.placeholderCompression.protectedTools",
327
+ expected: "string[]",
328
+ actual: typeof strategies.placeholderCompression.protectedTools,
329
+ });
330
+ }
331
+ }
332
+ }
333
+ return errors;
334
+ }
335
+ // Show validation warnings for a config file
336
+ function showConfigValidationWarnings(ctx, configPath, configData, isProject) {
337
+ const invalidKeys = getInvalidConfigKeys(configData);
338
+ const typeErrors = validateConfigTypes(configData);
339
+ if (invalidKeys.length === 0 && typeErrors.length === 0) {
340
+ return;
341
+ }
342
+ const configType = isProject ? "project config" : "config";
343
+ const messages = [];
344
+ if (invalidKeys.length > 0) {
345
+ const keyList = invalidKeys.slice(0, 3).join(", ");
346
+ const suffix = invalidKeys.length > 3 ? ` (+${invalidKeys.length - 3} more)` : "";
347
+ messages.push(`Unknown keys: ${keyList}${suffix}`);
348
+ }
349
+ if (typeErrors.length > 0) {
350
+ for (const err of typeErrors.slice(0, 2)) {
351
+ messages.push(`${err.key}: expected ${err.expected}, got ${err.actual}`);
352
+ }
353
+ if (typeErrors.length > 2) {
354
+ messages.push(`(+${typeErrors.length - 2} more type errors)`);
355
+ }
356
+ }
357
+ setTimeout(() => {
358
+ try {
359
+ ctx.client.tui.showToast({
360
+ body: {
361
+ title: `DCP: Invalid ${configType}`,
362
+ message: `${configPath}\n${messages.join("\n")}`,
363
+ variant: "warning",
364
+ duration: 7000,
365
+ },
366
+ });
367
+ }
368
+ catch { }
369
+ }, 7000);
370
+ }
371
+ export const DEFAULT_CONFIG = {
372
+ enabled: true,
373
+ debug: false,
374
+ pruneNotification: "detailed",
375
+ commands: {
376
+ enabled: true,
377
+ protectedTools: [...DEFAULT_PROTECTED_TOOLS],
378
+ },
379
+ turnProtection: {
380
+ enabled: false,
381
+ turns: 4,
382
+ },
383
+ protectedFilePatterns: [],
384
+ tools: {
385
+ settings: {
386
+ nudgeEnabled: true,
387
+ nudgeFrequency: 5,
388
+ protectedTools: [...DEFAULT_PROTECTED_TOOLS],
389
+ },
390
+ discard: {
391
+ enabled: true,
392
+ },
393
+ extract: {
394
+ enabled: true,
395
+ showDistillation: false,
396
+ },
397
+ },
398
+ strategies: {
399
+ deduplication: {
400
+ enabled: true,
401
+ protectedTools: [],
402
+ },
403
+ supersedeWrites: {
404
+ enabled: true,
405
+ },
406
+ purgeErrors: {
407
+ enabled: true,
408
+ turns: 2,
409
+ protectedTools: [],
410
+ },
411
+ pruneThinking: {
412
+ enabled: true,
413
+ delayTurns: 1,
414
+ },
415
+ placeholderCompression: {
416
+ enabled: true,
417
+ delayTurns: 2,
418
+ minOutputTokens: 100,
419
+ protectedTools: [],
420
+ },
421
+ },
422
+ };
423
+ const GLOBAL_CONFIG_DIR = join(homedir(), ".config", "opencode");
424
+ const GLOBAL_CONFIG_PATH_JSONC = join(GLOBAL_CONFIG_DIR, "dcp.jsonc");
425
+ const GLOBAL_CONFIG_PATH_JSON = join(GLOBAL_CONFIG_DIR, "dcp.json");
426
+ function findOpencodeDir(startDir) {
427
+ let current = startDir;
428
+ while (current !== "/") {
429
+ const candidate = join(current, ".opencode");
430
+ if (existsSync(candidate) && statSync(candidate).isDirectory()) {
431
+ return candidate;
432
+ }
433
+ const parent = dirname(current);
434
+ if (parent === current)
435
+ break;
436
+ current = parent;
437
+ }
438
+ return null;
439
+ }
440
+ function getConfigPaths(ctx) {
441
+ // Global: ~/.config/opencode/dcp.jsonc|json
442
+ let globalPath = null;
443
+ if (existsSync(GLOBAL_CONFIG_PATH_JSONC)) {
444
+ globalPath = GLOBAL_CONFIG_PATH_JSONC;
445
+ }
446
+ else if (existsSync(GLOBAL_CONFIG_PATH_JSON)) {
447
+ globalPath = GLOBAL_CONFIG_PATH_JSON;
448
+ }
449
+ // Custom config directory: $OPENCODE_CONFIG_DIR/dcp.jsonc|json
450
+ let configDirPath = null;
451
+ const opencodeConfigDir = process.env.OPENCODE_CONFIG_DIR;
452
+ if (opencodeConfigDir) {
453
+ const configJsonc = join(opencodeConfigDir, "dcp.jsonc");
454
+ const configJson = join(opencodeConfigDir, "dcp.json");
455
+ if (existsSync(configJsonc)) {
456
+ configDirPath = configJsonc;
457
+ }
458
+ else if (existsSync(configJson)) {
459
+ configDirPath = configJson;
460
+ }
461
+ }
462
+ // Project: <project>/.opencode/dcp.jsonc|json
463
+ let projectPath = null;
464
+ if (ctx?.directory) {
465
+ const opencodeDir = findOpencodeDir(ctx.directory);
466
+ if (opencodeDir) {
467
+ const projectJsonc = join(opencodeDir, "dcp.jsonc");
468
+ const projectJson = join(opencodeDir, "dcp.json");
469
+ if (existsSync(projectJsonc)) {
470
+ projectPath = projectJsonc;
471
+ }
472
+ else if (existsSync(projectJson)) {
473
+ projectPath = projectJson;
474
+ }
475
+ }
476
+ }
477
+ return { global: globalPath, configDir: configDirPath, project: projectPath };
478
+ }
479
+ function createDefaultConfig() {
480
+ if (!existsSync(GLOBAL_CONFIG_DIR)) {
481
+ mkdirSync(GLOBAL_CONFIG_DIR, { recursive: true });
482
+ }
483
+ const configContent = `{
484
+ "$schema": "https://raw.githubusercontent.com/Opencode-DCP/opencode-dynamic-context-pruning/master/dcp.schema.json"
485
+ }
486
+ `;
487
+ writeFileSync(GLOBAL_CONFIG_PATH_JSONC, configContent, "utf-8");
488
+ }
489
+ function loadConfigFile(configPath) {
490
+ let fileContent;
491
+ try {
492
+ fileContent = readFileSync(configPath, "utf-8");
493
+ }
494
+ catch {
495
+ // File doesn't exist or can't be read - not a parse error
496
+ return { data: null };
497
+ }
498
+ try {
499
+ const parsed = parse(fileContent);
500
+ if (parsed === undefined || parsed === null) {
501
+ return { data: null, parseError: "Config file is empty or invalid" };
502
+ }
503
+ return { data: parsed };
504
+ }
505
+ catch (error) {
506
+ return { data: null, parseError: error.message || "Failed to parse config" };
507
+ }
508
+ }
509
+ function mergeStrategies(base, override) {
510
+ if (!override)
511
+ return base;
512
+ return {
513
+ deduplication: {
514
+ enabled: override.deduplication?.enabled ?? base.deduplication.enabled,
515
+ protectedTools: [
516
+ ...new Set([
517
+ ...base.deduplication.protectedTools,
518
+ ...(override.deduplication?.protectedTools ?? []),
519
+ ]),
520
+ ],
521
+ },
522
+ supersedeWrites: {
523
+ enabled: override.supersedeWrites?.enabled ?? base.supersedeWrites.enabled,
524
+ },
525
+ purgeErrors: {
526
+ enabled: override.purgeErrors?.enabled ?? base.purgeErrors.enabled,
527
+ turns: override.purgeErrors?.turns ?? base.purgeErrors.turns,
528
+ protectedTools: [
529
+ ...new Set([
530
+ ...base.purgeErrors.protectedTools,
531
+ ...(override.purgeErrors?.protectedTools ?? []),
532
+ ]),
533
+ ],
534
+ },
535
+ pruneThinking: {
536
+ enabled: override.pruneThinking?.enabled ?? base.pruneThinking.enabled,
537
+ delayTurns: override.pruneThinking?.delayTurns ?? base.pruneThinking.delayTurns,
538
+ },
539
+ placeholderCompression: {
540
+ enabled: override.placeholderCompression?.enabled ?? base.placeholderCompression.enabled,
541
+ delayTurns: override.placeholderCompression?.delayTurns ?? base.placeholderCompression.delayTurns,
542
+ minOutputTokens: override.placeholderCompression?.minOutputTokens ?? base.placeholderCompression.minOutputTokens,
543
+ protectedTools: [
544
+ ...new Set([
545
+ ...base.placeholderCompression.protectedTools,
546
+ ...(override.placeholderCompression?.protectedTools ?? []),
547
+ ]),
548
+ ],
549
+ },
550
+ };
551
+ }
552
+ function mergeTools(base, override) {
553
+ if (!override)
554
+ return base;
555
+ return {
556
+ settings: {
557
+ nudgeEnabled: override.settings?.nudgeEnabled ?? base.settings.nudgeEnabled,
558
+ nudgeFrequency: override.settings?.nudgeFrequency ?? base.settings.nudgeFrequency,
559
+ protectedTools: [
560
+ ...new Set([
561
+ ...base.settings.protectedTools,
562
+ ...(override.settings?.protectedTools ?? []),
563
+ ]),
564
+ ],
565
+ },
566
+ discard: {
567
+ enabled: override.discard?.enabled ?? base.discard.enabled,
568
+ },
569
+ extract: {
570
+ enabled: override.extract?.enabled ?? base.extract.enabled,
571
+ showDistillation: override.extract?.showDistillation ?? base.extract.showDistillation,
572
+ },
573
+ };
574
+ }
575
+ function mergeCommands(base, override) {
576
+ if (override === undefined)
577
+ return base;
578
+ return {
579
+ enabled: override.enabled ?? base.enabled,
580
+ protectedTools: [...new Set([...base.protectedTools, ...(override.protectedTools ?? [])])],
581
+ };
582
+ }
583
+ function deepCloneConfig(config) {
584
+ return {
585
+ ...config,
586
+ commands: {
587
+ enabled: config.commands.enabled,
588
+ protectedTools: [...config.commands.protectedTools],
589
+ },
590
+ turnProtection: { ...config.turnProtection },
591
+ protectedFilePatterns: [...config.protectedFilePatterns],
592
+ tools: {
593
+ settings: {
594
+ ...config.tools.settings,
595
+ protectedTools: [...config.tools.settings.protectedTools],
596
+ },
597
+ discard: { ...config.tools.discard },
598
+ extract: { ...config.tools.extract },
599
+ },
600
+ strategies: {
601
+ deduplication: {
602
+ ...config.strategies.deduplication,
603
+ protectedTools: [...config.strategies.deduplication.protectedTools],
604
+ },
605
+ supersedeWrites: {
606
+ ...config.strategies.supersedeWrites,
607
+ },
608
+ purgeErrors: {
609
+ ...config.strategies.purgeErrors,
610
+ protectedTools: [...config.strategies.purgeErrors.protectedTools],
611
+ },
612
+ pruneThinking: {
613
+ ...config.strategies.pruneThinking,
614
+ },
615
+ placeholderCompression: {
616
+ ...config.strategies.placeholderCompression,
617
+ protectedTools: [...config.strategies.placeholderCompression.protectedTools],
618
+ },
619
+ },
620
+ };
621
+ }
622
+ export function getConfig(ctx) {
623
+ let config = deepCloneConfig(DEFAULT_CONFIG);
624
+ const configPaths = getConfigPaths(ctx);
625
+ // Load and merge global config
626
+ if (configPaths.global) {
627
+ const result = loadConfigFile(configPaths.global);
628
+ if (result.parseError) {
629
+ setTimeout(async () => {
630
+ try {
631
+ ctx.client.tui.showToast({
632
+ body: {
633
+ title: "DCP: Invalid config",
634
+ message: `${configPaths.global}\n${result.parseError}\nUsing default values`,
635
+ variant: "warning",
636
+ duration: 7000,
637
+ },
638
+ });
639
+ }
640
+ catch { }
641
+ }, 7000);
642
+ }
643
+ else if (result.data) {
644
+ // Validate config keys and types
645
+ showConfigValidationWarnings(ctx, configPaths.global, result.data, false);
646
+ config = {
647
+ enabled: result.data.enabled ?? config.enabled,
648
+ debug: result.data.debug ?? config.debug,
649
+ pruneNotification: result.data.pruneNotification ?? config.pruneNotification,
650
+ commands: mergeCommands(config.commands, result.data.commands),
651
+ turnProtection: {
652
+ enabled: result.data.turnProtection?.enabled ?? config.turnProtection.enabled,
653
+ turns: result.data.turnProtection?.turns ?? config.turnProtection.turns,
654
+ },
655
+ protectedFilePatterns: [
656
+ ...new Set([
657
+ ...config.protectedFilePatterns,
658
+ ...(result.data.protectedFilePatterns ?? []),
659
+ ]),
660
+ ],
661
+ tools: mergeTools(config.tools, result.data.tools),
662
+ strategies: mergeStrategies(config.strategies, result.data.strategies),
663
+ };
664
+ }
665
+ }
666
+ else {
667
+ // No config exists, create default
668
+ createDefaultConfig();
669
+ }
670
+ // Load and merge $OPENCODE_CONFIG_DIR/dcp.jsonc|json (overrides global)
671
+ if (configPaths.configDir) {
672
+ const result = loadConfigFile(configPaths.configDir);
673
+ if (result.parseError) {
674
+ setTimeout(async () => {
675
+ try {
676
+ ctx.client.tui.showToast({
677
+ body: {
678
+ title: "DCP: Invalid configDir config",
679
+ message: `${configPaths.configDir}\n${result.parseError}\nUsing global/default values`,
680
+ variant: "warning",
681
+ duration: 7000,
682
+ },
683
+ });
684
+ }
685
+ catch { }
686
+ }, 7000);
687
+ }
688
+ else if (result.data) {
689
+ // Validate config keys and types
690
+ showConfigValidationWarnings(ctx, configPaths.configDir, result.data, true);
691
+ config = {
692
+ enabled: result.data.enabled ?? config.enabled,
693
+ debug: result.data.debug ?? config.debug,
694
+ pruneNotification: result.data.pruneNotification ?? config.pruneNotification,
695
+ commands: mergeCommands(config.commands, result.data.commands),
696
+ turnProtection: {
697
+ enabled: result.data.turnProtection?.enabled ?? config.turnProtection.enabled,
698
+ turns: result.data.turnProtection?.turns ?? config.turnProtection.turns,
699
+ },
700
+ protectedFilePatterns: [
701
+ ...new Set([
702
+ ...config.protectedFilePatterns,
703
+ ...(result.data.protectedFilePatterns ?? []),
704
+ ]),
705
+ ],
706
+ tools: mergeTools(config.tools, result.data.tools),
707
+ strategies: mergeStrategies(config.strategies, result.data.strategies),
708
+ };
709
+ }
710
+ }
711
+ // Load and merge project config (overrides global)
712
+ if (configPaths.project) {
713
+ const result = loadConfigFile(configPaths.project);
714
+ if (result.parseError) {
715
+ setTimeout(async () => {
716
+ try {
717
+ ctx.client.tui.showToast({
718
+ body: {
719
+ title: "DCP: Invalid project config",
720
+ message: `${configPaths.project}\n${result.parseError}\nUsing global/default values`,
721
+ variant: "warning",
722
+ duration: 7000,
723
+ },
724
+ });
725
+ }
726
+ catch { }
727
+ }, 7000);
728
+ }
729
+ else if (result.data) {
730
+ // Validate config keys and types
731
+ showConfigValidationWarnings(ctx, configPaths.project, result.data, true);
732
+ config = {
733
+ enabled: result.data.enabled ?? config.enabled,
734
+ debug: result.data.debug ?? config.debug,
735
+ pruneNotification: result.data.pruneNotification ?? config.pruneNotification,
736
+ commands: mergeCommands(config.commands, result.data.commands),
737
+ turnProtection: {
738
+ enabled: result.data.turnProtection?.enabled ?? config.turnProtection.enabled,
739
+ turns: result.data.turnProtection?.turns ?? config.turnProtection.turns,
740
+ },
741
+ protectedFilePatterns: [
742
+ ...new Set([
743
+ ...config.protectedFilePatterns,
744
+ ...(result.data.protectedFilePatterns ?? []),
745
+ ]),
746
+ ],
747
+ tools: mergeTools(config.tools, result.data.tools),
748
+ strategies: mergeStrategies(config.strategies, result.data.strategies),
749
+ };
750
+ }
751
+ }
752
+ return config;
753
+ }
754
+ //# sourceMappingURL=config.js.map