config.ts 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Parse Codex's five-event hook subset into shared {@link MatcherGroup}s. Only synchronous command
  3. * hooks run; other types and `async: true` commands are recorded as skipped. Codex performs no
  4. * command substitution.
  5. * @module @deepseek-ai/dsh-hooks-codex/config
  6. */
  7. import { matcherDiagnostic, type MatcherGroup } from '@deepseek-ai/dsh-hook-protocol'
  8. /** The five Codex hook points this bridge supports. */
  9. export const CODEX_EVENTS = ['PreToolUse', 'PostToolUse', 'SessionStart', 'UserPromptSubmit', 'Stop'] as const
  10. /** A parsed Codex config: event name → its matcher groups (command hooks only). */
  11. export type CodexHookConfig = Record<string, MatcherGroup[]>
  12. /** A skipped non-command (or async) hook, surfaced so the bridge can warn. */
  13. export interface SkippedHook {
  14. event: string
  15. reason: string
  16. }
  17. /** The outcome of parsing one Codex config file. */
  18. export interface ParsedCodexConfig {
  19. config: CodexHookConfig
  20. skipped: SkippedHook[]
  21. }
  22. function asObject(value: unknown): Record<string, unknown> | undefined {
  23. return typeof value === 'object' && value !== null && !Array.isArray(value)
  24. ? value as Record<string, unknown>
  25. : undefined
  26. }
  27. /**
  28. * Parse a wrapped or bare Codex event map. Unknown events and malformed entries are ignored rather
  29. * than failing boot; unsupported or asynchronous hooks are returned in `skipped`. Matcher fields on
  30. * UserPromptSubmit and Stop are discarded because those events have no matcher subject. A
  31. * matcher-bearing runnable group with an invalid regex throws a `SyntaxError`, allowing the bridge
  32. * to reject the complete config before listener registration.
  33. * @param raw - the parsed JSON config: a `{ hooks: … }` wrapper or the bare event map.
  34. * @returns the runnable per-event groups plus the skipped hooks with their reasons.
  35. */
  36. export function parseCodexConfig(raw: unknown): ParsedCodexConfig {
  37. const config: CodexHookConfig = {}
  38. const skipped: SkippedHook[] = []
  39. const root = asObject(raw)
  40. const hooksMap = root ? asObject(root.hooks) ?? root : undefined
  41. if (!hooksMap) return { config, skipped }
  42. for (const event of CODEX_EVENTS) {
  43. const rawGroups = hooksMap[event]
  44. // Matcher-group parsing remains dialect-local because the supported hook
  45. // shapes and skip reasons differ from Claude Code's.
  46. /* jscpd:ignore-start */
  47. if (!Array.isArray(rawGroups)) continue
  48. const groups: MatcherGroup[] = []
  49. for (const rawGroup of rawGroups) {
  50. const group = asObject(rawGroup)
  51. if (!group || !Array.isArray(group.hooks)) continue
  52. const commands: MatcherGroup['hooks'] = []
  53. for (const rawHook of group.hooks) {
  54. const hook = asObject(rawHook)
  55. if (!hook) continue
  56. const type = typeof hook.type === 'string' ? hook.type : 'command'
  57. if (type !== 'command') { skipped.push({ event, reason: `unsupported "${type}" hook` }); continue }
  58. /* jscpd:ignore-end */
  59. if (hook.async === true) { skipped.push({ event, reason: 'async hook' }); continue }
  60. if (typeof hook.command !== 'string') continue
  61. // Codex accepts `timeout` or the `timeoutSec` alias.
  62. const timeout = typeof hook.timeout === 'number' ? hook.timeout
  63. : typeof hook.timeoutSec === 'number' ? hook.timeoutSec : undefined
  64. commands.push({ command: hook.command, ...timeout !== undefined ? { timeoutSec: timeout } : {} })
  65. }
  66. if (commands.length === 0) continue
  67. const matcher = event === 'UserPromptSubmit' || event === 'Stop'
  68. ? undefined
  69. : typeof group.matcher === 'string' ? group.matcher : undefined
  70. const diagnostic = matcherDiagnostic(matcher, 'codex')
  71. if (diagnostic !== undefined) throw new SyntaxError(`${diagnostic} on event ${JSON.stringify(event)}`)
  72. groups.push({ ...matcher !== undefined ? { matcher } : {}, hooks: commands })
  73. }
  74. if (groups.length > 0) config[event] = groups
  75. }
  76. return { config, skipped }
  77. }