Files
deepseek-harness/packages/hooks/hook-protocol/src/events.ts
T

88 lines
3.6 KiB
TypeScript
Raw Normal View History

/**
* Append helpers for the log-only `hook/*` session events — the durable record
* that a hook ran and what it decided. Thin wrappers over `session.append` so a
* bridge does not hand-build the payloads (and so the `turn`-enclosure +
* invoked/result pairing stay consistent across both bridges).
*
* `hook/*` events are log-only (not {@link SurfaceEventType}), so they carry no
* `surfaceOp` and append with no surface intent — but, like every event, they
* must sit inside an OPEN turn (the invariants oracle rejects an un-enclosed
* event). The mid-turn hook points (`PreToolUse`/`PostToolUse`/`UserPromptSubmit`/
* `Stop`) fire inside the loop's open turn by construction; `SessionStart` is the
* exception (its injected `context/message` is the durable evidence instead), so
* a bridge does NOT write `hook/*` for session-start — see the hooks RFC.
*
* @module @deepseek-ai/dsh-hook-protocol/events
*/
import type { Session } from '@deepseek-ai/dsh-session'
import type { HookDialect, HookOutput } from './types.ts'
/** What identifies a hook invocation across its invoked/result pair. */
export interface HookInvocation {
/** The open turn the invocation lives inside. */
turn: number
/** The hook point (`PreToolUse`, `Stop`, …). */
point: string
/** The bridge dialect that ran it. */
dialect: HookDialect
/** A stable id correlating the invoked event with its result. */
handlerId: string
/** The matcher-group pattern that selected it (absent for match-all). */
matcher?: string
}
/** The decided outcome half of the pair. */
export interface HookResultRecord {
turn: number
point: string
handlerId: string
/**
* The decoded outcome the run produced. {@link appendHookResult} derives the
* durable `decision`/`exitCode`/`stderrSummary` fields from it, so the shared
* event's semantics live here, in the lib that declares it, not per-bridge.
*/
output: HookOutput
}
/** How many characters of stderr the `hook/result.stderrSummary` field keeps. */
const STDERR_SUMMARY_MAX = 500
/** Truncate a stderr blob for the `hook/result.stderrSummary` field (`undefined` when empty). */
function summarizeStderr(stderr: string): string | undefined {
const t = stderr.trim()
if (t.length === 0) return undefined
return t.length > STDERR_SUMMARY_MAX ? t.slice(0, STDERR_SUMMARY_MAX) + '…' : t
}
/** Append a `hook/invoked` provenance event to `session`. */
export function appendHookInvoked(session: Session, invocation: HookInvocation): void {
session.append('hook/invoked', {
turn: invocation.turn,
point: invocation.point,
dialect: invocation.dialect,
handlerId: invocation.handlerId,
...invocation.matcher !== undefined ? { matcher: invocation.matcher } : {},
})
}
/**
* Append a `hook/result` outcome event to `session` (pairs with a prior
* `hook/invoked`). Owns the durable event's semantics: `decision` is the hook's
* parsed decision, else `'stop'` when it asked to halt (`continue: false`),
* else `'pass'`; `stderrSummary` is the trimmed stderr truncated to 500
* characters (omitted when empty); `exitCode` is omitted when the hook never ran.
*/
export function appendHookResult(session: Session, record: HookResultRecord): void {
const { output } = record
const stderrSummary = summarizeStderr(output.stderr)
session.append('hook/result', {
turn: record.turn,
point: record.point,
handlerId: record.handlerId,
decision: output.decision ?? (output.continue === false ? 'stop' : 'pass'),
...output.exitCode !== undefined ? { exitCode: output.exitCode } : {},
...stderrSummary !== undefined ? { stderrSummary } : {},
})
}