Skip to content

Hooks

Hooks react to events in the chat and tool lifecycle.

  • Service hooks: observe and control the send loop lifecycle via service.hook().beforeSendLoop().do(cb)
  • Stream hooks: observe streaming output (content, reasoning, tool-call deltas, usage, finish) via service.stream().hook().chunks().do(cb)
  • Chat hooks: subscribe to messages by role/regex
  • Tool hooks: before, after, and error hooks on tool execution
  • JSON hooks: declare hooks as .json files in a configurable directory, auto-loaded on init/send
  • HookMessageWriter: how hooks queue messages with origin: Hook

Framework types

Two base types underpin all hook builders:

  • HookBuilderBase<TCallback>: abstract class that every hook builder extends. Provides .do(cb) which registers the hook and returns a Hook for disposal.
  • HasHooks<THookBuilder>: interface for any class that exposes a .hook() method returning a builder. Implemented by ChatInterface, ChunkStreamInterface, ToolSuiteInterface, and ChatService.

These are rarely used directly; they are exported for type annotations when wrapping or composing hook builders.