wave-agent-sdk 0.0.8 → 0.0.11

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 (236) hide show
  1. package/dist/agent.d.ts +92 -23
  2. package/dist/agent.d.ts.map +1 -1
  3. package/dist/agent.js +351 -137
  4. package/dist/index.d.ts +3 -0
  5. package/dist/index.d.ts.map +1 -1
  6. package/dist/index.js +3 -0
  7. package/dist/managers/aiManager.d.ts +14 -36
  8. package/dist/managers/aiManager.d.ts.map +1 -1
  9. package/dist/managers/aiManager.js +74 -77
  10. package/dist/managers/backgroundBashManager.d.ts.map +1 -1
  11. package/dist/managers/backgroundBashManager.js +4 -3
  12. package/dist/managers/hookManager.d.ts +3 -8
  13. package/dist/managers/hookManager.d.ts.map +1 -1
  14. package/dist/managers/hookManager.js +39 -29
  15. package/dist/managers/liveConfigManager.d.ts +55 -18
  16. package/dist/managers/liveConfigManager.d.ts.map +1 -1
  17. package/dist/managers/liveConfigManager.js +372 -90
  18. package/dist/managers/lspManager.d.ts +43 -0
  19. package/dist/managers/lspManager.d.ts.map +1 -0
  20. package/dist/managers/lspManager.js +326 -0
  21. package/dist/managers/messageManager.d.ts +8 -16
  22. package/dist/managers/messageManager.d.ts.map +1 -1
  23. package/dist/managers/messageManager.js +52 -74
  24. package/dist/managers/permissionManager.d.ts +75 -0
  25. package/dist/managers/permissionManager.d.ts.map +1 -0
  26. package/dist/managers/permissionManager.js +368 -0
  27. package/dist/managers/skillManager.d.ts +1 -0
  28. package/dist/managers/skillManager.d.ts.map +1 -1
  29. package/dist/managers/skillManager.js +2 -1
  30. package/dist/managers/slashCommandManager.d.ts.map +1 -1
  31. package/dist/managers/slashCommandManager.js +0 -1
  32. package/dist/managers/subagentManager.d.ts +8 -23
  33. package/dist/managers/subagentManager.d.ts.map +1 -1
  34. package/dist/managers/subagentManager.js +97 -117
  35. package/dist/managers/toolManager.d.ts +38 -1
  36. package/dist/managers/toolManager.d.ts.map +1 -1
  37. package/dist/managers/toolManager.js +66 -2
  38. package/dist/services/aiService.d.ts +3 -1
  39. package/dist/services/aiService.d.ts.map +1 -1
  40. package/dist/services/aiService.js +123 -30
  41. package/dist/services/configurationService.d.ts +116 -0
  42. package/dist/services/configurationService.d.ts.map +1 -0
  43. package/dist/services/configurationService.js +585 -0
  44. package/dist/services/fileWatcher.d.ts.map +1 -1
  45. package/dist/services/fileWatcher.js +5 -6
  46. package/dist/services/hook.d.ts +7 -124
  47. package/dist/services/hook.d.ts.map +1 -1
  48. package/dist/services/hook.js +46 -458
  49. package/dist/services/jsonlHandler.d.ts +24 -15
  50. package/dist/services/jsonlHandler.d.ts.map +1 -1
  51. package/dist/services/jsonlHandler.js +67 -88
  52. package/dist/services/memory.d.ts +0 -9
  53. package/dist/services/memory.d.ts.map +1 -1
  54. package/dist/services/memory.js +2 -49
  55. package/dist/services/session.d.ts +82 -33
  56. package/dist/services/session.d.ts.map +1 -1
  57. package/dist/services/session.js +275 -181
  58. package/dist/tools/bashTool.d.ts.map +1 -1
  59. package/dist/tools/bashTool.js +109 -11
  60. package/dist/tools/deleteFileTool.d.ts.map +1 -1
  61. package/dist/tools/deleteFileTool.js +25 -0
  62. package/dist/tools/editTool.d.ts.map +1 -1
  63. package/dist/tools/editTool.js +30 -6
  64. package/dist/tools/lspTool.d.ts +6 -0
  65. package/dist/tools/lspTool.d.ts.map +1 -0
  66. package/dist/tools/lspTool.js +589 -0
  67. package/dist/tools/multiEditTool.d.ts.map +1 -1
  68. package/dist/tools/multiEditTool.js +26 -7
  69. package/dist/tools/readTool.d.ts.map +1 -1
  70. package/dist/tools/readTool.js +111 -2
  71. package/dist/tools/skillTool.js +2 -2
  72. package/dist/tools/todoWriteTool.d.ts.map +1 -1
  73. package/dist/tools/todoWriteTool.js +23 -0
  74. package/dist/tools/types.d.ts +11 -8
  75. package/dist/tools/types.d.ts.map +1 -1
  76. package/dist/tools/writeTool.d.ts.map +1 -1
  77. package/dist/tools/writeTool.js +25 -9
  78. package/dist/types/commands.d.ts +0 -1
  79. package/dist/types/commands.d.ts.map +1 -1
  80. package/dist/types/config.d.ts +4 -0
  81. package/dist/types/config.d.ts.map +1 -1
  82. package/dist/types/configuration.d.ts +69 -0
  83. package/dist/types/configuration.d.ts.map +1 -0
  84. package/dist/types/configuration.js +8 -0
  85. package/dist/types/core.d.ts +10 -0
  86. package/dist/types/core.d.ts.map +1 -1
  87. package/dist/types/environment.d.ts +41 -0
  88. package/dist/types/environment.d.ts.map +1 -1
  89. package/dist/types/fileSearch.d.ts +5 -0
  90. package/dist/types/fileSearch.d.ts.map +1 -0
  91. package/dist/types/fileSearch.js +1 -0
  92. package/dist/types/hooks.d.ts +11 -2
  93. package/dist/types/hooks.d.ts.map +1 -1
  94. package/dist/types/hooks.js +1 -7
  95. package/dist/types/index.d.ts +5 -0
  96. package/dist/types/index.d.ts.map +1 -1
  97. package/dist/types/index.js +5 -0
  98. package/dist/types/lsp.d.ts +90 -0
  99. package/dist/types/lsp.d.ts.map +1 -0
  100. package/dist/types/lsp.js +4 -0
  101. package/dist/types/messaging.d.ts +6 -11
  102. package/dist/types/messaging.d.ts.map +1 -1
  103. package/dist/types/permissions.d.ts +39 -0
  104. package/dist/types/permissions.d.ts.map +1 -0
  105. package/dist/types/permissions.js +12 -0
  106. package/dist/types/session.d.ts +1 -6
  107. package/dist/types/session.d.ts.map +1 -1
  108. package/dist/types/skills.d.ts +1 -0
  109. package/dist/types/skills.d.ts.map +1 -1
  110. package/dist/types/tools.d.ts +35 -0
  111. package/dist/types/tools.d.ts.map +1 -0
  112. package/dist/types/tools.js +4 -0
  113. package/dist/utils/abortUtils.d.ts +34 -0
  114. package/dist/utils/abortUtils.d.ts.map +1 -0
  115. package/dist/utils/abortUtils.js +92 -0
  116. package/dist/utils/bashHistory.d.ts +4 -0
  117. package/dist/utils/bashHistory.d.ts.map +1 -1
  118. package/dist/utils/bashHistory.js +21 -4
  119. package/dist/utils/bashParser.d.ts +24 -0
  120. package/dist/utils/bashParser.d.ts.map +1 -0
  121. package/dist/utils/bashParser.js +413 -0
  122. package/dist/utils/builtinSubagents.d.ts +7 -0
  123. package/dist/utils/builtinSubagents.d.ts.map +1 -0
  124. package/dist/utils/builtinSubagents.js +65 -0
  125. package/dist/utils/cacheControlUtils.d.ts +8 -33
  126. package/dist/utils/cacheControlUtils.d.ts.map +1 -1
  127. package/dist/utils/cacheControlUtils.js +83 -126
  128. package/dist/utils/constants.d.ts +0 -12
  129. package/dist/utils/constants.d.ts.map +1 -1
  130. package/dist/utils/constants.js +1 -13
  131. package/dist/utils/convertMessagesForAPI.d.ts +2 -1
  132. package/dist/utils/convertMessagesForAPI.d.ts.map +1 -1
  133. package/dist/utils/convertMessagesForAPI.js +33 -14
  134. package/dist/utils/fileSearch.d.ts +14 -0
  135. package/dist/utils/fileSearch.d.ts.map +1 -0
  136. package/dist/utils/fileSearch.js +88 -0
  137. package/dist/utils/fileUtils.d.ts +14 -2
  138. package/dist/utils/fileUtils.d.ts.map +1 -1
  139. package/dist/utils/fileUtils.js +101 -17
  140. package/dist/utils/globalLogger.d.ts +0 -14
  141. package/dist/utils/globalLogger.d.ts.map +1 -1
  142. package/dist/utils/globalLogger.js +0 -16
  143. package/dist/utils/markdownParser.d.ts.map +1 -1
  144. package/dist/utils/markdownParser.js +1 -17
  145. package/dist/utils/messageOperations.d.ts +1 -11
  146. package/dist/utils/messageOperations.d.ts.map +1 -1
  147. package/dist/utils/messageOperations.js +7 -24
  148. package/dist/utils/pathEncoder.d.ts +4 -0
  149. package/dist/utils/pathEncoder.d.ts.map +1 -1
  150. package/dist/utils/pathEncoder.js +16 -9
  151. package/dist/utils/pathSafety.d.ts +10 -0
  152. package/dist/utils/pathSafety.d.ts.map +1 -0
  153. package/dist/utils/pathSafety.js +23 -0
  154. package/dist/utils/subagentParser.d.ts +2 -2
  155. package/dist/utils/subagentParser.d.ts.map +1 -1
  156. package/dist/utils/subagentParser.js +10 -7
  157. package/package.json +9 -9
  158. package/src/agent.ts +475 -216
  159. package/src/index.ts +3 -0
  160. package/src/managers/aiManager.ts +107 -111
  161. package/src/managers/backgroundBashManager.ts +4 -3
  162. package/src/managers/hookManager.ts +44 -39
  163. package/src/managers/liveConfigManager.ts +524 -138
  164. package/src/managers/lspManager.ts +434 -0
  165. package/src/managers/messageManager.ts +73 -103
  166. package/src/managers/permissionManager.ts +480 -0
  167. package/src/managers/skillManager.ts +3 -1
  168. package/src/managers/slashCommandManager.ts +1 -2
  169. package/src/managers/subagentManager.ts +116 -159
  170. package/src/managers/toolManager.ts +95 -3
  171. package/src/services/aiService.ts +207 -26
  172. package/src/services/configurationService.ts +762 -0
  173. package/src/services/fileWatcher.ts +5 -6
  174. package/src/services/hook.ts +50 -631
  175. package/src/services/jsonlHandler.ts +84 -100
  176. package/src/services/memory.ts +2 -59
  177. package/src/services/session.ts +338 -213
  178. package/src/tools/bashTool.ts +126 -13
  179. package/src/tools/deleteFileTool.ts +36 -0
  180. package/src/tools/editTool.ts +41 -7
  181. package/src/tools/lspTool.ts +760 -0
  182. package/src/tools/multiEditTool.ts +37 -8
  183. package/src/tools/readTool.ts +125 -2
  184. package/src/tools/skillTool.ts +2 -2
  185. package/src/tools/todoWriteTool.ts +33 -1
  186. package/src/tools/types.ts +15 -9
  187. package/src/tools/writeTool.ts +36 -10
  188. package/src/types/commands.ts +0 -1
  189. package/src/types/config.ts +5 -0
  190. package/src/types/configuration.ts +73 -0
  191. package/src/types/core.ts +11 -0
  192. package/src/types/environment.ts +44 -0
  193. package/src/types/fileSearch.ts +4 -0
  194. package/src/types/hooks.ts +14 -11
  195. package/src/types/index.ts +5 -0
  196. package/src/types/lsp.ts +96 -0
  197. package/src/types/messaging.ts +8 -13
  198. package/src/types/permissions.ts +52 -0
  199. package/src/types/session.ts +3 -8
  200. package/src/types/skills.ts +1 -0
  201. package/src/types/tools.ts +38 -0
  202. package/src/utils/abortUtils.ts +118 -0
  203. package/src/utils/bashHistory.ts +28 -4
  204. package/src/utils/bashParser.ts +444 -0
  205. package/src/utils/builtinSubagents.ts +71 -0
  206. package/src/utils/cacheControlUtils.ts +106 -171
  207. package/src/utils/constants.ts +1 -16
  208. package/src/utils/convertMessagesForAPI.ts +38 -14
  209. package/src/utils/fileSearch.ts +107 -0
  210. package/src/utils/fileUtils.ts +114 -19
  211. package/src/utils/globalLogger.ts +0 -17
  212. package/src/utils/markdownParser.ts +1 -19
  213. package/src/utils/messageOperations.ts +7 -35
  214. package/src/utils/pathEncoder.ts +24 -9
  215. package/src/utils/pathSafety.ts +26 -0
  216. package/src/utils/subagentParser.ts +11 -8
  217. package/dist/constants/events.d.ts +0 -28
  218. package/dist/constants/events.d.ts.map +0 -1
  219. package/dist/constants/events.js +0 -27
  220. package/dist/services/configurationWatcher.d.ts +0 -120
  221. package/dist/services/configurationWatcher.d.ts.map +0 -1
  222. package/dist/services/configurationWatcher.js +0 -439
  223. package/dist/services/memoryStore.d.ts +0 -81
  224. package/dist/services/memoryStore.d.ts.map +0 -1
  225. package/dist/services/memoryStore.js +0 -200
  226. package/dist/types/memoryStore.d.ts +0 -82
  227. package/dist/types/memoryStore.d.ts.map +0 -1
  228. package/dist/types/memoryStore.js +0 -7
  229. package/dist/utils/configResolver.d.ts +0 -65
  230. package/dist/utils/configResolver.d.ts.map +0 -1
  231. package/dist/utils/configResolver.js +0 -210
  232. package/src/constants/events.ts +0 -38
  233. package/src/services/configurationWatcher.ts +0 -622
  234. package/src/services/memoryStore.ts +0 -279
  235. package/src/types/memoryStore.ts +0 -94
  236. package/src/utils/configResolver.ts +0 -302
@@ -3,52 +3,89 @@
3
3
  *
4
4
  * Orchestrates live configuration reload functionality including:
5
5
  * - Hook configuration watching and reloading
6
- * - Memory store management for AGENTS.md files
6
+ * - Configuration file watching for settings.json files
7
7
  * - Coordination between file watchers and configuration updates
8
8
  */
9
9
  import type { Logger } from "../types/index.js";
10
+ import type { HookManager } from "./hookManager.js";
11
+ import type { PermissionManager } from "./permissionManager.js";
12
+ import type { WaveConfiguration } from "../types/hooks.js";
13
+ import { ConfigurationService } from "../services/configurationService.js";
10
14
  export interface LiveConfigManagerOptions {
11
15
  workdir: string;
12
16
  logger?: Logger;
13
- onConfigurationChanged?: () => void;
14
- onMemoryStoreFileChanged?: (filePath: string, changeType: "add" | "change" | "unlink") => Promise<void>;
17
+ hookManager?: HookManager;
18
+ permissionManager?: PermissionManager;
19
+ configurationService?: ConfigurationService;
15
20
  }
16
21
  export declare class LiveConfigManager {
17
22
  private readonly workdir;
18
23
  private readonly logger?;
19
- private readonly onConfigurationChanged?;
20
- private readonly onMemoryStoreFileChanged?;
21
- private configurationWatcher?;
24
+ private readonly hookManager?;
25
+ private readonly permissionManager?;
22
26
  private isInitialized;
27
+ private readonly configurationService;
28
+ private currentConfiguration;
29
+ private lastValidConfiguration;
30
+ private fileWatcher;
31
+ private userConfigPaths?;
32
+ private projectConfigPaths?;
33
+ private isWatching;
34
+ private reloadInProgress;
23
35
  constructor(options: LiveConfigManagerOptions);
24
36
  /**
25
- * Initialize live configuration management
37
+ * Initialize configuration watching
38
+ * Maps to FR-004: System MUST watch settings.json files
39
+ * Supports watching multiple file paths (e.g., settings.local.json and settings.json)
40
+ */
41
+ private initializeWatching;
42
+ /**
43
+ * Get current configuration
44
+ */
45
+ getCurrentConfiguration(): WaveConfiguration | null;
46
+ /**
47
+ * Initialize configuration management with file watching
26
48
  */
27
49
  initialize(): Promise<void>;
28
50
  /**
29
- * Shutdown live configuration management
51
+ * Shutdown configuration management and cleanup resources
30
52
  */
31
53
  shutdown(): Promise<void>;
32
54
  /**
33
- * Initialize configuration watcher for hook settings
55
+ * Reload configuration from files
56
+ * Maps to FR-008: Continue with previous valid configuration on errors
34
57
  */
35
- private initializeConfigurationWatcher;
58
+ private reloadConfiguration;
36
59
  /**
37
- * Initialize memory store watching for AGENTS.md files
60
+ * Reload configuration from files (public method)
38
61
  */
39
- private initializeMemoryStoreWatching;
62
+ reload(): Promise<WaveConfiguration>;
40
63
  /**
41
- * Handle configuration change events
64
+ * Check if watching is active
42
65
  */
43
- private handleConfigurationChange;
66
+ isWatchingActive(): boolean;
44
67
  /**
45
- * Handle AGENTS.md file change events
68
+ * Get watcher status for monitoring
46
69
  */
47
- private handleMemoryStoreFileChange;
70
+ getWatcherStatus(): {
71
+ isActive: boolean;
72
+ configurationLoaded: boolean;
73
+ hasValidConfiguration: boolean;
74
+ reloadInProgress: boolean;
75
+ watchedFiles: {
76
+ path: string;
77
+ isActive: boolean;
78
+ method: "native" | "polling" | "failed";
79
+ errorCount: number;
80
+ }[];
81
+ };
82
+ private setupFileWatcherEvents;
83
+ private handleFileChange;
48
84
  /**
49
- * Get initialization status
85
+ * Validate configuration structure and content
50
86
  */
51
- get initialized(): boolean;
87
+ private validateConfiguration;
88
+ private detectChanges;
52
89
  /**
53
90
  * Get configuration file paths for user and project settings
54
91
  * Returns paths in priority order (local.json first, then .json)
@@ -1 +1 @@
1
- {"version":3,"file":"liveConfigManager.d.ts","sourceRoot":"","sources":["../../src/managers/liveConfigManager.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAehD,MAAM,WAAW,wBAAwB;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,sBAAsB,CAAC,EAAE,MAAM,IAAI,CAAC;IACpC,wBAAwB,CAAC,EAAE,CACzB,QAAQ,EAAE,MAAM,EAChB,UAAU,EAAE,KAAK,GAAG,QAAQ,GAAG,QAAQ,KACpC,OAAO,CAAC,IAAI,CAAC,CAAC;CACpB;AAED,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,sBAAsB,CAAC,CAAa;IACrD,OAAO,CAAC,QAAQ,CAAC,wBAAwB,CAAC,CAGvB;IACnB,OAAO,CAAC,oBAAoB,CAAC,CAAuB;IACpD,OAAO,CAAC,aAAa,CAAkB;gBAE3B,OAAO,EAAE,wBAAwB;IAO7C;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IA2BjC;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAuB/B;;OAEG;YACW,8BAA8B;IAoB5C;;OAEG;YACW,6BAA6B;IA4B3C;;OAEG;IACH,OAAO,CAAC,yBAAyB;IA8BjC;;OAEG;YACW,2BAA2B;IA8BzC;;OAEG;IACH,IAAI,WAAW,IAAI,OAAO,CAEzB;IAED;;;OAGG;IACH,OAAO,CAAC,qBAAqB;CAQ9B"}
1
+ {"version":3,"file":"liveConfigManager.d.ts","sourceRoot":"","sources":["../../src/managers/liveConfigManager.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAGH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAKhD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACpD,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAKhE,OAAO,KAAK,EAAE,iBAAiB,EAAoB,MAAM,mBAAmB,CAAC;AAE7E,OAAO,EAAE,oBAAoB,EAAE,MAAM,qCAAqC,CAAC;AAK3E,MAAM,WAAW,wBAAwB;IACvC,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,iBAAiB,CAAC,EAAE,iBAAiB,CAAC;IACtC,oBAAoB,CAAC,EAAE,oBAAoB,CAAC;CAC7C;AAED,qBAAa,iBAAiB;IAC5B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAS;IACjC,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAc;IAC3C,OAAO,CAAC,QAAQ,CAAC,iBAAiB,CAAC,CAAoB;IACvD,OAAO,CAAC,aAAa,CAAkB;IACvC,OAAO,CAAC,QAAQ,CAAC,oBAAoB,CAAuB;IAG5D,OAAO,CAAC,oBAAoB,CAAkC;IAC9D,OAAO,CAAC,sBAAsB,CAAkC;IAGhE,OAAO,CAAC,WAAW,CAAqB;IACxC,OAAO,CAAC,eAAe,CAAC,CAAW;IACnC,OAAO,CAAC,kBAAkB,CAAC,CAAW;IACtC,OAAO,CAAC,UAAU,CAAkB;IACpC,OAAO,CAAC,gBAAgB,CAAkB;gBAE9B,OAAO,EAAE,wBAAwB;IAW7C;;;;OAIG;YACW,kBAAkB;IA6DhC;;OAEG;IACH,uBAAuB,IAAI,iBAAiB,GAAG,IAAI;IAInD;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAuBjC;;OAEG;IACG,QAAQ,IAAI,OAAO,CAAC,IAAI,CAAC;IAyB/B;;;OAGG;YACW,mBAAmB;IA8MjC;;OAEG;IACG,MAAM,IAAI,OAAO,CAAC,iBAAiB,CAAC;IAK1C;;OAEG;IACH,gBAAgB,IAAI,OAAO;IAI3B;;OAEG;IACH,gBAAgB;;;;;;;;;;;;IAgBhB,OAAO,CAAC,sBAAsB;YAMhB,gBAAgB;IA8C9B;;OAEG;IACH,OAAO,CAAC,qBAAqB;IA0E7B,OAAO,CAAC,aAAa;IAuDrB;;;OAGG;IACH,OAAO,CAAC,qBAAqB;CAQ9B"}
@@ -3,150 +3,432 @@
3
3
  *
4
4
  * Orchestrates live configuration reload functionality including:
5
5
  * - Hook configuration watching and reloading
6
- * - Memory store management for AGENTS.md files
6
+ * - Configuration file watching for settings.json files
7
7
  * - Coordination between file watchers and configuration updates
8
8
  */
9
- import { ConfigurationWatcher, } from "../services/configurationWatcher.js";
10
- import { configResolver } from "../utils/configResolver.js";
11
- import { join } from "path";
12
- import { getUserConfigPaths, getProjectConfigPaths, } from "../utils/configPaths.js";
13
- import { CONFIGURATION_EVENTS } from "../constants/events.js";
9
+ import { existsSync } from "fs";
10
+ import { FileWatcherService, } from "../services/fileWatcher.js";
11
+ import { getProjectConfigPaths, getUserConfigPaths, } from "../utils/configPaths.js";
12
+ import { isValidHookEvent, isValidHookEventConfig } from "../types/hooks.js";
13
+ import { ConfigurationService } from "../services/configurationService.js";
14
+ import { ensureGlobalGitIgnore } from "../utils/fileUtils.js";
14
15
  export class LiveConfigManager {
15
16
  constructor(options) {
16
17
  this.isInitialized = false;
18
+ // Configuration state
19
+ this.currentConfiguration = null;
20
+ this.lastValidConfiguration = null;
21
+ this.isWatching = false;
22
+ this.reloadInProgress = false;
17
23
  this.workdir = options.workdir;
18
24
  this.logger = options.logger;
19
- this.onConfigurationChanged = options.onConfigurationChanged;
20
- this.onMemoryStoreFileChanged = options.onMemoryStoreFileChanged;
25
+ this.hookManager = options.hookManager;
26
+ this.permissionManager = options.permissionManager;
27
+ this.configurationService =
28
+ options.configurationService || new ConfigurationService();
29
+ this.fileWatcher = new FileWatcherService(this.logger);
30
+ this.setupFileWatcherEvents();
21
31
  }
22
32
  /**
23
- * Initialize live configuration management
33
+ * Initialize configuration watching
34
+ * Maps to FR-004: System MUST watch settings.json files
35
+ * Supports watching multiple file paths (e.g., settings.local.json and settings.json)
36
+ */
37
+ async initializeWatching(userPaths, projectPaths) {
38
+ try {
39
+ this.logger?.info("Live Config: Initializing configuration watching...");
40
+ this.userConfigPaths = userPaths;
41
+ this.projectConfigPaths = projectPaths;
42
+ // Load initial configuration
43
+ await this.reloadConfiguration();
44
+ // Start watching user configs that exist
45
+ for (const userPath of userPaths) {
46
+ if (existsSync(userPath)) {
47
+ this.logger?.debug(`Live Config: Starting to watch user config: ${userPath}`);
48
+ await this.fileWatcher.watchFile(userPath, (event) => this.handleFileChange(event, "user"));
49
+ }
50
+ else {
51
+ this.logger?.debug(`Live Config: User config file does not exist: ${userPath}`);
52
+ }
53
+ }
54
+ // Start watching project configs that exist
55
+ if (projectPaths) {
56
+ for (const projectPath of projectPaths) {
57
+ if (existsSync(projectPath)) {
58
+ if (projectPath.endsWith("settings.local.json")) {
59
+ await ensureGlobalGitIgnore("**/.wave/settings.local.json");
60
+ }
61
+ this.logger?.debug(`Live Config: Starting to watch project config: ${projectPath}`);
62
+ await this.fileWatcher.watchFile(projectPath, (event) => this.handleFileChange(event, "project"));
63
+ }
64
+ else {
65
+ this.logger?.debug(`Live Config: Project config file does not exist: ${projectPath}`);
66
+ }
67
+ }
68
+ }
69
+ this.isWatching = true;
70
+ this.logger?.info("Live Config: Configuration watching initialized successfully");
71
+ }
72
+ catch (error) {
73
+ const errorMessage = `Failed to initialize configuration watching: ${error.message}`;
74
+ this.logger?.error(`Live Config: ${errorMessage}`);
75
+ throw new Error(errorMessage);
76
+ }
77
+ }
78
+ /**
79
+ * Get current configuration
80
+ */
81
+ getCurrentConfiguration() {
82
+ return this.currentConfiguration ? { ...this.currentConfiguration } : null;
83
+ }
84
+ /**
85
+ * Initialize configuration management with file watching
24
86
  */
25
87
  async initialize() {
26
88
  if (this.isInitialized) {
27
- this.logger?.debug("[LiveConfigManager] Already initialized");
89
+ this.logger?.debug("Already initialized");
28
90
  return;
29
91
  }
30
92
  try {
31
- // Initialize configuration watcher for hook settings
32
- await this.initializeConfigurationWatcher();
33
- // Initialize memory store watching for AGENTS.md if callback is available
34
- if (this.onMemoryStoreFileChanged) {
35
- await this.initializeMemoryStoreWatching();
36
- }
93
+ // Get configuration file paths
94
+ const { userPaths, projectPaths } = this.getConfigurationPaths();
95
+ // Initialize configuration watching
96
+ await this.initializeWatching(userPaths, projectPaths);
37
97
  this.isInitialized = true;
38
- this.logger?.info("Live Config: Live configuration management initialized successfully");
98
+ this.logger?.info("Live configuration management initialized with file watching");
39
99
  }
40
100
  catch (error) {
41
- this.logger?.error(`Live Config: Failed to initialize: ${error.message}`);
101
+ this.logger?.error(`Failed to initialize: ${error.message}`);
42
102
  throw error;
43
103
  }
44
104
  }
45
105
  /**
46
- * Shutdown live configuration management
106
+ * Shutdown configuration management and cleanup resources
47
107
  */
48
108
  async shutdown() {
49
109
  if (!this.isInitialized) {
50
110
  return;
51
111
  }
52
112
  try {
53
- if (this.configurationWatcher) {
54
- await this.configurationWatcher.shutdown();
55
- this.configurationWatcher = undefined;
56
- }
113
+ this.logger?.info("Live Config: Shutting down configuration manager...");
114
+ this.isWatching = false;
115
+ // Cleanup file watcher
116
+ await this.fileWatcher.cleanup();
117
+ // Clean up state
118
+ this.currentConfiguration = null;
119
+ this.lastValidConfiguration = null;
57
120
  this.isInitialized = false;
58
- this.logger?.info("Live Config: Live configuration management shutdown completed");
121
+ this.logger?.info("Live configuration management shutdown completed");
59
122
  }
60
123
  catch (error) {
61
- this.logger?.error(`Live Config: Error during shutdown: ${error.message}`);
124
+ this.logger?.error(`Error during shutdown: ${error.message}`);
62
125
  throw error;
63
126
  }
64
127
  }
65
128
  /**
66
- * Initialize configuration watcher for hook settings
67
- */
68
- async initializeConfigurationWatcher() {
69
- this.configurationWatcher = new ConfigurationWatcher(this.workdir, this.logger);
70
- // Set up configuration change handler using EventEmitter pattern
71
- this.configurationWatcher.on(CONFIGURATION_EVENTS.CONFIGURATION_CHANGE, (event) => {
72
- this.handleConfigurationChange(event);
73
- });
74
- // Initialize watching for user and project settings
75
- const { userPaths, projectPaths } = this.getConfigurationPaths();
76
- await this.configurationWatcher.initializeWatching(userPaths, projectPaths);
77
- this.logger?.info("Live Config: Configuration watching initialized");
78
- }
79
- /**
80
- * Initialize memory store watching for AGENTS.md files
129
+ * Reload configuration from files
130
+ * Maps to FR-008: Continue with previous valid configuration on errors
81
131
  */
82
- async initializeMemoryStoreWatching() {
83
- if (!this.onMemoryStoreFileChanged || !this.configurationWatcher) {
84
- this.logger?.debug("Live Config: Memory store callback or configuration watcher not available, skipping AGENTS.md watching");
85
- return;
132
+ async reloadConfiguration() {
133
+ if (this.reloadInProgress) {
134
+ this.logger?.debug("Live Config: Reload already in progress, skipping");
135
+ return this.currentConfiguration || {};
86
136
  }
137
+ this.reloadInProgress = true;
87
138
  try {
88
- const agentsFilePath = join(this.workdir, "AGENTS.md");
89
- // Add AGENTS.md to file watcher
90
- await this.configurationWatcher.watchAdditionalFile(agentsFilePath, async (event) => {
91
- await this.handleMemoryStoreFileChange(event);
92
- });
93
- this.logger?.info("Live Config: AGENTS.md file watching initialized");
139
+ this.logger?.debug("Live Config: Reloading configuration from files...");
140
+ // Load merged configuration using ConfigurationService
141
+ const loadResult = await this.configurationService.loadMergedConfiguration(this.workdir);
142
+ const newConfig = loadResult.configuration;
143
+ // Check for errors during loading
144
+ if (!loadResult.success) {
145
+ const errorMessage = loadResult.error || "Configuration loading failed with unknown error";
146
+ this.logger?.error(`Live Config: Configuration loading failed: ${errorMessage}`);
147
+ // Log warnings if any
148
+ if (loadResult.warnings && loadResult.warnings.length > 0) {
149
+ this.logger?.warn(`Live Config: Configuration warnings: ${loadResult.warnings.join("; ")}`);
150
+ }
151
+ // Use fallback configuration if available
152
+ if (this.lastValidConfiguration) {
153
+ this.logger?.info("Live Config: Using previous valid configuration due to loading errors");
154
+ this.currentConfiguration = this.lastValidConfiguration;
155
+ // Apply environment variables to configuration service if configured
156
+ if (this.lastValidConfiguration.env) {
157
+ this.configurationService.setEnvironmentVars(this.lastValidConfiguration.env);
158
+ }
159
+ // Update hook manager if available
160
+ if (this.hookManager) {
161
+ this.hookManager.loadConfigurationFromWaveConfig(this.lastValidConfiguration);
162
+ }
163
+ return this.currentConfiguration;
164
+ }
165
+ else {
166
+ this.logger?.warn("Live Config: No previous valid configuration available, using empty config");
167
+ this.currentConfiguration = {};
168
+ return this.currentConfiguration;
169
+ }
170
+ }
171
+ // Log success with detailed information
172
+ if (newConfig) {
173
+ this.logger?.info(`Live Config: Configuration loaded successfully from ${loadResult.sourcePath || "merged sources"}`);
174
+ // Log detailed configuration info
175
+ const hookCount = Object.keys(newConfig.hooks || {}).length;
176
+ const envCount = Object.keys(newConfig.env || {}).length;
177
+ this.logger?.debug(`Live Config: Loaded ${hookCount} hook events and ${envCount} environment variables`);
178
+ }
179
+ else {
180
+ this.logger?.info("Live Config: No configuration found (using empty configuration)");
181
+ }
182
+ // Log warnings from successful loading
183
+ if (loadResult.warnings && loadResult.warnings.length > 0) {
184
+ this.logger?.warn(`Live Config: Configuration warnings: ${loadResult.warnings.join("; ")}`);
185
+ }
186
+ // Validate new configuration if it exists
187
+ if (newConfig) {
188
+ const validation = this.validateConfiguration(newConfig);
189
+ if (!validation.valid) {
190
+ const errorMessage = `Configuration validation failed: ${validation.errors.join(", ")}`;
191
+ this.logger?.error(`Live Config: ${errorMessage}`);
192
+ // Use previous valid configuration for error recovery
193
+ if (this.lastValidConfiguration) {
194
+ this.logger?.info("Live Config: Using previous valid configuration due to validation errors");
195
+ this.currentConfiguration = this.lastValidConfiguration;
196
+ // Apply environment variables to configuration service if configured
197
+ if (this.lastValidConfiguration.env) {
198
+ this.configurationService.setEnvironmentVars(this.lastValidConfiguration.env);
199
+ }
200
+ // Update hook manager if available
201
+ if (this.hookManager) {
202
+ this.hookManager.loadConfigurationFromWaveConfig(this.lastValidConfiguration);
203
+ }
204
+ return this.currentConfiguration;
205
+ }
206
+ else {
207
+ this.logger?.warn("Live Config: No previous valid configuration available, using empty config");
208
+ this.currentConfiguration = {};
209
+ return this.currentConfiguration;
210
+ }
211
+ }
212
+ }
213
+ // Detect changes between old and new configuration
214
+ this.detectChanges(this.currentConfiguration, newConfig);
215
+ // Update current configuration
216
+ this.currentConfiguration = newConfig || {};
217
+ // Save as last valid configuration if it's valid and not empty
218
+ if (newConfig && (newConfig.hooks || newConfig.env)) {
219
+ this.lastValidConfiguration = { ...newConfig };
220
+ this.logger?.debug("Live Config: Saved current configuration as last valid backup");
221
+ }
222
+ // Note: Environment variables are already applied by loadMergedConfiguration()
223
+ // No need to set them again here as currentConfiguration === newConfig
224
+ // Update hook manager if available
225
+ if (this.hookManager) {
226
+ this.hookManager.loadConfigurationFromWaveConfig(this.currentConfiguration);
227
+ }
228
+ // Update permission manager if available
229
+ if (this.permissionManager) {
230
+ if (this.currentConfiguration.defaultMode) {
231
+ this.permissionManager.updateConfiguredDefaultMode(this.currentConfiguration.defaultMode);
232
+ }
233
+ if (this.currentConfiguration.permissions?.allow) {
234
+ this.permissionManager.updateAllowedRules(this.currentConfiguration.permissions.allow);
235
+ }
236
+ }
237
+ this.logger?.info(`Live Config: Configuration reload completed successfully with ${Object.keys(newConfig?.hooks || {}).length} event types and ${Object.keys(newConfig?.env || {}).length} environment variables`);
238
+ return this.currentConfiguration;
94
239
  }
95
240
  catch (error) {
96
- this.logger?.warn(`Live Config: Failed to initialize AGENTS.md watching: ${error.message}`);
97
- // Don't throw - memory optimization is not critical for core functionality
241
+ const errorMessage = `Configuration reload failed with exception: ${error.message}`;
242
+ this.logger?.error(`Live Config: ${errorMessage}`);
243
+ // Use previous valid configuration for error recovery
244
+ if (this.lastValidConfiguration) {
245
+ this.logger?.info("Live Config: Using previous valid configuration due to reload exception");
246
+ this.currentConfiguration = this.lastValidConfiguration;
247
+ // Apply environment variables to configuration service if configured
248
+ if (this.lastValidConfiguration.env) {
249
+ this.configurationService.setEnvironmentVars(this.lastValidConfiguration.env);
250
+ }
251
+ // Update hook manager if available
252
+ if (this.hookManager) {
253
+ this.hookManager.loadConfigurationFromWaveConfig(this.lastValidConfiguration);
254
+ }
255
+ }
256
+ else {
257
+ this.logger?.warn("Live Config: No previous valid configuration available, using empty config");
258
+ this.currentConfiguration = {};
259
+ }
260
+ return this.currentConfiguration;
261
+ }
262
+ finally {
263
+ this.reloadInProgress = false;
98
264
  }
99
265
  }
100
266
  /**
101
- * Handle configuration change events
267
+ * Reload configuration from files (public method)
102
268
  */
103
- handleConfigurationChange(event) {
104
- this.logger?.info(`Live Config: Configuration change detected: ${event.type} at ${event.path}`);
105
- // Invalidate and refresh configuration cache for live environment variable updates
106
- configResolver.invalidateCache(this.workdir);
107
- configResolver.refreshCache(this.workdir);
108
- // Trigger Agent configuration update callback if provided
109
- if (this.onConfigurationChanged) {
110
- try {
111
- this.logger?.info("Live Config: Triggering Agent configuration update");
112
- this.onConfigurationChanged();
113
- }
114
- catch (error) {
115
- this.logger?.error(`Live Config: Error in configuration change callback: ${error.message}`);
116
- }
117
- }
118
- // Log cache status after refresh
119
- const cacheStatus = configResolver.getCacheStatus();
120
- if (cacheStatus) {
121
- this.logger?.info(`Live Config: Configuration cache refreshed - ${cacheStatus.envVarCount} environment variables loaded`);
122
- }
269
+ async reload() {
270
+ this.logger?.info("Manually reloading configuration...");
271
+ return await this.reloadConfiguration();
123
272
  }
124
273
  /**
125
- * Handle AGENTS.md file change events
274
+ * Check if watching is active
126
275
  */
127
- async handleMemoryStoreFileChange(event) {
128
- if (!this.onMemoryStoreFileChanged) {
129
- return;
130
- }
276
+ isWatchingActive() {
277
+ return this.isWatching;
278
+ }
279
+ /**
280
+ * Get watcher status for monitoring
281
+ */
282
+ getWatcherStatus() {
283
+ const statuses = this.fileWatcher.getAllWatcherStatuses();
284
+ return {
285
+ isActive: this.isWatching,
286
+ configurationLoaded: this.currentConfiguration !== null,
287
+ hasValidConfiguration: this.lastValidConfiguration !== null,
288
+ reloadInProgress: this.reloadInProgress,
289
+ watchedFiles: statuses.map((s) => ({
290
+ path: s.path,
291
+ isActive: s.isActive,
292
+ method: s.method,
293
+ errorCount: s.errorCount,
294
+ })),
295
+ };
296
+ }
297
+ setupFileWatcherEvents() {
298
+ this.fileWatcher.on("watcherError", (error) => {
299
+ this.logger?.error(`Live Config: File watcher error: ${error.message}`);
300
+ });
301
+ }
302
+ async handleFileChange(event, source) {
303
+ this.logger?.debug(`Live Config: File ${event.type} detected for ${source} config: ${event.path}`);
131
304
  try {
132
- this.logger?.info(`Live Config: AGENTS.md ${event.type} detected: ${event.path}`);
133
- const changeType = event.type === "delete"
134
- ? "unlink"
135
- : event.type === "create"
136
- ? "add"
137
- : "change";
138
- await this.onMemoryStoreFileChanged(event.path, changeType);
139
- this.logger?.info(`Live Config: Memory store updated for AGENTS.md ${event.type}`);
305
+ // Handle file deletion
306
+ if (event.type === "delete") {
307
+ this.logger?.info(`Live Config: ${source} config file deleted: ${event.path}`);
308
+ // Reload configuration without the deleted file
309
+ await this.reloadConfiguration();
310
+ return;
311
+ }
312
+ // Handle file creation or modification
313
+ if (event.type === "change" || event.type === "create") {
314
+ this.logger?.info(`Live Config: ${source} config file ${event.type}: ${event.path}`);
315
+ if (source === "project" &&
316
+ event.path.endsWith("settings.local.json") &&
317
+ event.type === "create") {
318
+ await ensureGlobalGitIgnore("**/.wave/settings.local.json");
319
+ }
320
+ // Add small delay to ensure file write is complete
321
+ await new Promise((resolve) => setTimeout(resolve, 50));
322
+ // Reload configuration
323
+ await this.reloadConfiguration();
324
+ }
140
325
  }
141
326
  catch (error) {
142
- this.logger?.error(`Live Config: Failed to handle AGENTS.md file change: ${error.message}`);
327
+ this.logger?.error(`Live Config: Error handling file change for ${source} config: ${error.message}`);
143
328
  }
144
329
  }
145
330
  /**
146
- * Get initialization status
331
+ * Validate configuration structure and content
147
332
  */
148
- get initialized() {
149
- return this.isInitialized;
333
+ validateConfiguration(config) {
334
+ const errors = [];
335
+ if (!config || typeof config !== "object") {
336
+ return { valid: false, errors: ["Configuration must be an object"] };
337
+ }
338
+ // Validate defaultMode if present
339
+ if (config.defaultMode !== undefined) {
340
+ if (config.defaultMode !== "default" &&
341
+ config.defaultMode !== "bypassPermissions" &&
342
+ config.defaultMode !== "acceptEdits") {
343
+ errors.push(`Invalid defaultMode: "${config.defaultMode}". Must be "default", "bypassPermissions" or "acceptEdits"`);
344
+ }
345
+ }
346
+ // Validate hooks if present
347
+ if (config.hooks) {
348
+ if (typeof config.hooks !== "object") {
349
+ errors.push("hooks property must be an object");
350
+ }
351
+ else {
352
+ // Validate each hook event
353
+ for (const [eventName, eventConfigs] of Object.entries(config.hooks)) {
354
+ // Validate event name
355
+ if (!isValidHookEvent(eventName)) {
356
+ errors.push(`Invalid hook event: ${eventName}`);
357
+ continue;
358
+ }
359
+ // Validate event configurations
360
+ if (!Array.isArray(eventConfigs)) {
361
+ errors.push(`Hook event ${eventName} must be an array of configurations`);
362
+ continue;
363
+ }
364
+ eventConfigs.forEach((eventConfig, index) => {
365
+ if (!isValidHookEventConfig(eventConfig)) {
366
+ errors.push(`Invalid hook event configuration at ${eventName}[${index}]`);
367
+ }
368
+ });
369
+ }
370
+ }
371
+ }
372
+ // Validate environment variables if present
373
+ if (config.env) {
374
+ if (typeof config.env !== "object" || Array.isArray(config.env)) {
375
+ errors.push("env property must be an object");
376
+ }
377
+ else {
378
+ for (const [key, value] of Object.entries(config.env)) {
379
+ if (typeof key !== "string" || key.trim() === "") {
380
+ errors.push(`Invalid environment variable key: ${key}`);
381
+ }
382
+ if (typeof value !== "string") {
383
+ errors.push(`Environment variable ${key} must have a string value`);
384
+ }
385
+ }
386
+ }
387
+ }
388
+ return {
389
+ valid: errors.length === 0,
390
+ errors,
391
+ };
392
+ }
393
+ detectChanges(oldConfig, newConfig) {
394
+ const added = [];
395
+ const modified = [];
396
+ const removed = [];
397
+ // Handle environment variables changes
398
+ const oldEnv = oldConfig?.env || {};
399
+ const newEnv = newConfig?.env || {};
400
+ for (const key of Object.keys(newEnv)) {
401
+ if (!(key in oldEnv)) {
402
+ added.push(`env.${key}`);
403
+ }
404
+ else if (oldEnv[key] !== newEnv[key]) {
405
+ modified.push(`env.${key}`);
406
+ }
407
+ }
408
+ for (const key of Object.keys(oldEnv)) {
409
+ if (!(key in newEnv)) {
410
+ removed.push(`env.${key}`);
411
+ }
412
+ }
413
+ // Handle hooks changes (simplified)
414
+ const oldHooks = oldConfig?.hooks || {};
415
+ const newHooks = newConfig?.hooks || {};
416
+ for (const event of Object.keys(newHooks)) {
417
+ if (isValidHookEvent(event)) {
418
+ if (!(event in oldHooks)) {
419
+ added.push(`hooks.${event}`);
420
+ }
421
+ else if (JSON.stringify(oldHooks[event]) !== JSON.stringify(newHooks[event])) {
422
+ modified.push(`hooks.${event}`);
423
+ }
424
+ }
425
+ }
426
+ for (const event of Object.keys(oldHooks)) {
427
+ if (isValidHookEvent(event) && !(event in newHooks)) {
428
+ removed.push(`hooks.${event}`);
429
+ }
430
+ }
431
+ return { added, modified, removed };
150
432
  }
151
433
  /**
152
434
  * Get configuration file paths for user and project settings