Skip to content

Conversation

@github-actions
Copy link

@github-actions github-actions bot commented Dec 6, 2025

Motivation

The Python SDK allows hook callbacks to modify event properties to change agent behavior (e.g., switching tools, modifying parameters, transforming results), but the TypeScript SDK marked these properties as readonly. This prevented users from implementing similar behavior modification patterns and created an API inconsistency between the two SDKs.

Resolves: #12

Public API Changes

The following hook event properties are now writable, allowing hook callbacks to modify agent behavior:

BeforeToolCallEvent:

// Before: Cannot modify
event.tool = differentTool        // TypeScript error
event.toolUse = modifiedParams    // TypeScript error

// After: Can modify to change behavior
event.tool = differentTool        // ✓ Agent uses different tool
event.toolUse = {
  ...event.toolUse,
  input: modifiedInput            // ✓ Agent uses modified parameters
}

AfterToolCallEvent:

// Before: Cannot modify
event.result = modifiedResult     // TypeScript error

// After: Can modify to transform results
event.result = new ToolResultBlock({
  toolUseId: event.result.toolUseId,
  status: 'success',
  content: [new TextBlock('Modified result')]  // ✓ Agent returns modified result
})

The changes are backward compatible. Existing hooks that only read properties continue to work without modification.

Use Cases

  • Tool routing: Intercept tool calls and redirect to alternative implementations (e.g., sandbox, mock, or different version)
  • Parameter validation/transformation: Modify tool inputs before execution (e.g., sanitization, schema migration, default injection)
  • Result transformation: Post-process tool results (e.g., filtering sensitive data, format conversion, error recovery)
  • Monitoring: Change tool execution based on runtime conditions without modifying tool implementations

Implementation Notes

The agent now reads potentially modified values after yielding hook events:

  1. Yield BeforeToolCallEvent and capture modified tool and toolUse
  2. Execute tool using potentially modified values
  3. Yield AfterToolCallEvent and capture modified result
  4. Return potentially modified result to agent loop

This follows the same pattern as the existing AfterModelCallEvent.retryModelCall property.

zastrowm and others added 13 commits December 4, 2025 20:47
Added a checklist for PR descriptions to ensure clarity and relevance.
Added a checklist for testing best practices.
Added a checklist item to ensure the PR description includes an overview of the feature and key implementation decisions.
Clarified guidelines for pull request creation and checklist inclusion.
Allow hook callbacks to modify event properties to change agent behavior,
achieving parity with the Python SDK.

Hook callbacks can now modify:
- BeforeToolCallEvent.tool - Change which tool gets executed
- BeforeToolCallEvent.toolUse - Modify tool parameters before execution
- AfterToolCallEvent.result - Transform tool results before sending to model

This enables use cases like tool routing, parameter validation, result
transformation, and conditional behavior changes without modifying tool
implementations.

Resolves #12
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 (v3)

3 participants