config.ts 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /**
  2. * Parse a Codex `hooks.json` into the shared {@link MatcherGroup} shape. Codex's
  3. * config format is a SUBSET of Claude Code's: the same event-name → matcher-group
  4. * structure and the same `{ type: 'command', command, timeout?/timeoutSec? }`
  5. * hook shape, but only five events and NO command-string substitution (Codex sets
  6. * no hook env vars and does not expand `${…}`). Non-command hooks (and Codex's
  7. * `async: true` commands) are parsed-and-skipped with a warning.
  8. *
  9. * @module @deepseek-ai/dsh-hooks-codex/config
  10. */
  11. import type { MatcherGroup } from '@deepseek-ai/dsh-hook-protocol'
  12. /** The five hook points Codex's engine supports. */
  13. export const CODEX_EVENTS = ['PreToolUse', 'PostToolUse', 'SessionStart', 'UserPromptSubmit', 'Stop'] as const
  14. /** A parsed Codex config: event name → its matcher groups (command hooks only). */
  15. export type CodexHookConfig = Record<string, MatcherGroup[]>
  16. /** A skipped non-command (or async) hook, surfaced so the bridge can warn. */
  17. export interface SkippedHook {
  18. event: string
  19. reason: string
  20. }
  21. /** The outcome of parsing one Codex config file. */
  22. export interface ParsedCodexConfig {
  23. config: CodexHookConfig
  24. skipped: SkippedHook[]
  25. }
  26. function asObject(value: unknown): Record<string, unknown> | undefined {
  27. return typeof value === 'object' && value !== null && !Array.isArray(value)
  28. ? value as Record<string, unknown>
  29. : undefined
  30. }
  31. /**
  32. * Parse a raw Codex `hooks.json` object into runnable {@link MatcherGroup}s.
  33. * Only the five {@link CODEX_EVENTS} are honored; an unknown event is dropped.
  34. * `type !== 'command'` and `async: true` command hooks are skipped (recorded in
  35. * `skipped`). Malformed entries are ignored rather than thrown — a bad config
  36. * must not crash boot. No command substitution (Codex does none).
  37. * @param raw - the parsed JSON config: a `{ hooks: … }` wrapper or the bare event map.
  38. * @returns the runnable per-event groups plus the skipped hooks with their reasons.
  39. */
  40. export function parseCodexConfig(raw: unknown): ParsedCodexConfig {
  41. const config: CodexHookConfig = {}
  42. const skipped: SkippedHook[] = []
  43. const root = asObject(raw)
  44. const hooksMap = root ? asObject(root.hooks) ?? root : undefined
  45. if (!hooksMap) return { config, skipped }
  46. for (const event of CODEX_EVENTS) {
  47. const rawGroups = hooksMap[event]
  48. // Matcher-group parsing remains dialect-local because the supported hook
  49. // shapes and skip reasons differ from Claude Code's.
  50. /* jscpd:ignore-start */
  51. if (!Array.isArray(rawGroups)) continue
  52. const groups: MatcherGroup[] = []
  53. for (const rawGroup of rawGroups) {
  54. const group = asObject(rawGroup)
  55. if (!group || !Array.isArray(group.hooks)) continue
  56. const commands: MatcherGroup['hooks'] = []
  57. for (const rawHook of group.hooks) {
  58. const hook = asObject(rawHook)
  59. if (!hook) continue
  60. const type = typeof hook.type === 'string' ? hook.type : 'command'
  61. if (type !== 'command') { skipped.push({ event, reason: `unsupported "${type}" hook` }); continue }
  62. /* jscpd:ignore-end */
  63. if (hook.async === true) { skipped.push({ event, reason: 'async hook' }); continue }
  64. if (typeof hook.command !== 'string') continue
  65. // Codex accepts `timeout` or the `timeoutSec` alias.
  66. const timeout = typeof hook.timeout === 'number' ? hook.timeout
  67. : typeof hook.timeoutSec === 'number' ? hook.timeoutSec : undefined
  68. commands.push({ command: hook.command, ...timeout !== undefined ? { timeoutSec: timeout } : {} })
  69. }
  70. if (commands.length === 0) continue
  71. groups.push({ ...typeof group.matcher === 'string' ? { matcher: group.matcher } : {}, hooks: commands })
  72. }
  73. if (groups.length > 0) config[event] = groups
  74. }
  75. return { config, skipped }
  76. }