Skip to content

Conversation

tkaufmann
Copy link

Problem

The current Response class requires method chaining to build responses, but the send() method is protected. This creates a gap when you need to immediately block or approve a tool and send the response in one operation.

Currently, to block a tool and send the response, you must:

$hook->response()->block('reason'); // Returns self, but cannot call send()
// No way to send without extending the class

Solution

This PR adds two methods that combine decision-making with sending:

  • blockAndSend(string $reason): never - Blocks tool execution and sends response
  • approveAndSend(string $reason = ''): never - Approves tool execution and sends response

Use Case

When building middleware hooks that intercept tool calls (e.g., routing WebSearch to a different API), you need to:

  1. Block the original tool execution
  2. Provide replacement content
  3. Send the response immediately

Without these methods, developers must create extended hook classes with custom logic.

Implementation

public function blockAndSend(string $reason): never
{
    $this->data['decision'] = 'block';
    $this->data['reason'] = $reason;
    $this->send();
}

public function approveAndSend(string $reason = ''): never
{
    $this->data['decision'] = 'approve';
    if ($reason) {
        $this->data['reason'] = $reason;
    }
    $this->send();
}

Testing

Added tests in tests/Hooks/ExtendedResponseTest.php to verify:

  • Correct JSON output structure
  • Proper decision values
  • Execution termination behavior

Breaking Changes

None. These are new methods that don't affect existing functionality.

Alternative Considered

Making send() public was considered but rejected to maintain encapsulation and prevent misuse of the response flow.

…e handling

These methods combine decision-making with sending, eliminating the need for
extended classes when replacing tool output. This is particularly useful for
middleware hooks that intercept and replace tool functionality.

- blockAndSend(): Block tool execution and immediately send response
- approveAndSend(): Approve tool execution and immediately send response
- Added tests for both new methods

Use case: When building bridges (e.g., routing Claude's WebSearch to Perplexity),
these methods allow clean interception without needing custom hook classes.
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.

1 participant