events.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * Append helpers for durable, log-only hook events. They carry no surface
  3. * intent and must remain turn-enclosed and invoked/result paired. Mid-turn hook
  4. * points satisfy that boundary; SessionStart records injected context instead
  5. * and does not append `hook/*` outside a turn.
  6. * @module @deepseek-ai/dsh-hook-protocol/events
  7. */
  8. import type { Session } from '@deepseek-ai/dsh-session'
  9. import type { HookDialect, HookOutput } from './types.ts'
  10. /** What identifies a hook invocation across its invoked/result pair. */
  11. export interface HookInvocation {
  12. /** The open turn the invocation lives inside. */
  13. turn: number
  14. /** The hook point (`PreToolUse`, `Stop`, …). */
  15. point: string
  16. /** The bridge dialect that ran it. */
  17. dialect: HookDialect
  18. /** A stable id correlating the invoked event with its result. */
  19. handlerId: string
  20. /** The matcher-group pattern that selected it (absent for match-all). */
  21. matcher?: string
  22. }
  23. /** The decided outcome half of the pair. */
  24. export interface HookResultRecord {
  25. turn: number
  26. point: string
  27. handlerId: string
  28. /**
  29. * The decoded outcome the run produced. {@link appendHookResult} derives the
  30. * durable `decision`/`exitCode`/`stderrSummary` fields from it, so the shared
  31. * event's semantics live here, in the lib that declares it, not per-bridge.
  32. */
  33. output: HookOutput
  34. /**
  35. * Character cap for the derived `stderrSummary`. The bound is the bridge's
  36. * to own (its `stderrSummaryMaxChars` config) and is passed in explicitly —
  37. * {@link DEFAULT_STDERR_SUMMARY_MAX_CHARS} is the reference default.
  38. */
  39. stderrSummaryMaxChars: number
  40. /** Wall-clock duration of the run (from `runHook`) — durable audit timing. */
  41. durationMs: number
  42. }
  43. /**
  44. * The reference default for {@link HookResultRecord.stderrSummaryMaxChars}
  45. * (both bridges' config default). It lives here, once, next to the truncation
  46. * rule it bounds, so the bridges cannot drift apart on the shared event's
  47. * default cap.
  48. */
  49. export const DEFAULT_STDERR_SUMMARY_MAX_CHARS = 500
  50. /**
  51. * Truncate a hook's stderr for {@link HookResultRecord.stderrSummary}: trimmed,
  52. * `undefined` when empty, cut at `maxChars` with an ellipsis when over. The
  53. * bound is a parameter — like `runHook`'s `defaultTimeoutMs`, each bridge owns
  54. * the config default and passes it in.
  55. * @param stderr - the hook's raw captured stderr.
  56. * @param maxChars - the character cap for the summary (the bridge's config value).
  57. * @returns the trimmed, capped summary, or `undefined` when stderr is blank.
  58. */
  59. export function summarizeStderr(stderr: string, maxChars: number): string | undefined {
  60. const t = stderr.trim()
  61. if (t.length === 0) return undefined
  62. return t.length > maxChars ? t.slice(0, maxChars) + '…' : t
  63. }
  64. /**
  65. * Append a `hook/invoked` event naming the handler and hook point to `session`.
  66. * @param session - the session whose open turn records the event.
  67. * @param invocation - the invocation identity; an absent `matcher` is omitted from the payload.
  68. */
  69. export function appendHookInvoked(session: Session, invocation: HookInvocation): void {
  70. session.append('hook/invoked', {
  71. turn: invocation.turn,
  72. point: invocation.point,
  73. dialect: invocation.dialect,
  74. handlerId: invocation.handlerId,
  75. ...invocation.matcher !== undefined ? { matcher: invocation.matcher } : {},
  76. })
  77. }
  78. /**
  79. * Append the durable result paired with `hook/invoked`. The recorded decision
  80. * is the parsed decision, then `stop` for `continue:false`, else `pass`; stderr
  81. * is trimmed and capped, and an absent process exit stays omitted.
  82. * @param session - the session whose open turn records the event.
  83. * @param record - the outcome to record: the decoded output plus the summary cap and duration.
  84. */
  85. export function appendHookResult(session: Session, record: HookResultRecord): void {
  86. const { output } = record
  87. const stderrSummary = summarizeStderr(output.stderr, record.stderrSummaryMaxChars)
  88. session.append('hook/result', {
  89. turn: record.turn,
  90. point: record.point,
  91. handlerId: record.handlerId,
  92. decision: output.decision ?? (output.continue === false ? 'stop' : 'pass'),
  93. ...output.exitCode !== undefined ? { exitCode: output.exitCode } : {},
  94. ...stderrSummary !== undefined ? { stderrSummary } : {},
  95. durationMs: record.durationMs,
  96. })
  97. }