config.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 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`.
  30. * @param raw - the parsed JSON config: a `{ hooks: … }` wrapper or the bare event map.
  31. * @returns the runnable per-event groups plus the skipped hooks with their reasons.
  32. */
  33. export function parseCodexConfig(raw: unknown): ParsedCodexConfig {
  34. const config: CodexHookConfig = {}
  35. const skipped: SkippedHook[] = []
  36. const root = asObject(raw)
  37. const hooksMap = root ? asObject(root.hooks) ?? root : undefined
  38. if (!hooksMap) return { config, skipped }
  39. for (const event of CODEX_EVENTS) {
  40. const rawGroups = hooksMap[event]
  41. // Matcher-group parsing remains dialect-local because the supported hook
  42. // shapes and skip reasons differ from Claude Code's.
  43. /* jscpd:ignore-start */
  44. if (!Array.isArray(rawGroups)) continue
  45. const groups: MatcherGroup[] = []
  46. for (const rawGroup of rawGroups) {
  47. const group = asObject(rawGroup)
  48. if (!group || !Array.isArray(group.hooks)) continue
  49. const commands: MatcherGroup['hooks'] = []
  50. for (const rawHook of group.hooks) {
  51. const hook = asObject(rawHook)
  52. if (!hook) continue
  53. const type = typeof hook.type === 'string' ? hook.type : 'command'
  54. if (type !== 'command') { skipped.push({ event, reason: `unsupported "${type}" hook` }); continue }
  55. /* jscpd:ignore-end */
  56. if (hook.async === true) { skipped.push({ event, reason: 'async hook' }); continue }
  57. if (typeof hook.command !== 'string') continue
  58. // Codex accepts `timeout` or the `timeoutSec` alias.
  59. const timeout = typeof hook.timeout === 'number' ? hook.timeout
  60. : typeof hook.timeoutSec === 'number' ? hook.timeoutSec : undefined
  61. commands.push({ command: hook.command, ...timeout !== undefined ? { timeoutSec: timeout } : {} })
  62. }
  63. if (commands.length === 0) continue
  64. groups.push({ ...typeof group.matcher === 'string' ? { matcher: group.matcher } : {}, hooks: commands })
  65. }
  66. if (groups.length > 0) config[event] = groups
  67. }
  68. return { config, skipped }
  69. }