* Internal helper to add a hook to session state
( setAppState: (updater: (prev: AppState) => AppState) => void, sessionId: string, event: HookEvent, matcher: string, hook: HookCommand | FunctionHook, onHookSuccess?: OnHookSuccess, skillRoot?: string, )
| 165 | * Internal helper to add a hook to session state |
| 166 | */ |
| 167 | function addHookToSession( |
| 168 | setAppState: (updater: (prev: AppState) => AppState) => void, |
| 169 | sessionId: string, |
| 170 | event: HookEvent, |
| 171 | matcher: string, |
| 172 | hook: HookCommand | FunctionHook, |
| 173 | onHookSuccess?: OnHookSuccess, |
| 174 | skillRoot?: string, |
| 175 | ): void { |
| 176 | setAppState(prev => { |
| 177 | const store = prev.sessionHooks.get(sessionId) ?? { hooks: {} } |
| 178 | const eventMatchers = store.hooks[event] || [] |
| 179 | |
| 180 | // Find existing matcher or create new one |
| 181 | const existingMatcherIndex = eventMatchers.findIndex( |
| 182 | m => m.matcher === matcher && m.skillRoot === skillRoot, |
| 183 | ) |
| 184 | |
| 185 | let updatedMatchers: SessionHookMatcher[] |
| 186 | if (existingMatcherIndex >= 0) { |
| 187 | // Add to existing matcher |
| 188 | updatedMatchers = [...eventMatchers] |
| 189 | const existingMatcher = updatedMatchers[existingMatcherIndex]! |
| 190 | updatedMatchers[existingMatcherIndex] = { |
| 191 | matcher: existingMatcher.matcher, |
| 192 | skillRoot: existingMatcher.skillRoot, |
| 193 | hooks: [...existingMatcher.hooks, { hook, onHookSuccess }], |
| 194 | } |
| 195 | } else { |
| 196 | // Create new matcher |
| 197 | updatedMatchers = [ |
| 198 | ...eventMatchers, |
| 199 | { |
| 200 | matcher, |
| 201 | skillRoot, |
| 202 | hooks: [{ hook, onHookSuccess }], |
| 203 | }, |
| 204 | ] |
| 205 | } |
| 206 | |
| 207 | const newHooks = { ...store.hooks, [event]: updatedMatchers } |
| 208 | |
| 209 | prev.sessionHooks.set(sessionId, { hooks: newHooks }) |
| 210 | return prev |
| 211 | }) |
| 212 | |
| 213 | logForDebugging( |
| 214 | `Added session hook for event ${event} in session ${sessionId}`, |
| 215 | ) |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * Remove a specific hook from the session |
no test coverage detected