Skip to content

Conversation

@zastrowm
Copy link
Owner

@zastrowm zastrowm commented Dec 8, 2025

Motivation

The TypeScript SDK currently only supports read-only hooks, preventing hooks from modifying event properties to change agent behavior. The Python SDK allows hooks to write to certain properties (via _can_write() mechanism), enabling use cases like redirecting tool calls, modifying tool parameters, and transforming tool results.

This implementation achieves parity with the Python SDK by making specific hook properties writable.

Resolves #17

Public API Changes

The following hook event properties are now writable (readonly modifiers removed):

BeforeToolCallEvent:

// Before: tool and toolUse were readonly
const hook = {
  registerCallbacks: (registry) => {
    registry.addCallback(BeforeToolCallEvent, (event) => {
      event.tool = differentTool      // Now allowed
      event.toolUse = modifiedToolUse // Now allowed
    })
  }
}

AfterToolCallEvent:

// Before: result was readonly
const hook = {
  registerCallbacks: (registry) => {
    registry.addCallback(AfterToolCallEvent, (event) => {
      event.result = modifiedResult // Now allowed
    })
  }
}

The agent reads potentially modified values after invoking hooks and uses them for tool execution and result handling. This follows the same pattern as the existing AfterModelCallEvent.retryModelCall property.

Use Cases

  • Tool redirection: Change which tool gets executed based on runtime conditions
  • Parameter sanitization: Modify tool inputs before execution (e.g., adding authentication, filtering sensitive data)
  • Result transformation: Post-process tool results before they're sent to the model (e.g., formatting, filtering, error recovery)
  • Error handling: Convert tool errors to success results or vice versa

zastrowm and others added 3 commits December 8, 2025 12:08
Remove readonly modifiers from BeforeToolCallEvent (tool, toolUse) and
AfterToolCallEvent (result) properties. Agent now reads potentially modified
values after yielding hook events, enabling hooks to change tool execution
behavior and modify tool results.

Resolves #17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Enable writable hooks (v5)

3 participants