Skip to content

feat(adk): Add SendEvent API for emitting custom events from middleware#791

Merged
hi-pender merged 2 commits intomainfrom
feat/copy_send_event
Feb 14, 2026
Merged

feat(adk): Add SendEvent API for emitting custom events from middleware#791
hi-pender merged 2 commits intomainfrom
feat/copy_send_event

Conversation

@hi-pender
Copy link
Contributor

Summary

This PR introduces the SendEvent API that enables ChatModelAgentMiddleware implementations to emit custom AgentEvents to the event stream during agent execution.

Motivation

When implementing custom middleware for ChatModelAgent, there was previously no way to inject custom events into the agent's event stream. This limitation prevented middleware from communicating progress, status updates, or custom payloads to the caller iterating over the event stream.

Changes

Core Implementation

  • SendEvent(ctx context.Context, event *AgentEvent) error: A new public API that sends a custom event to the agent's event stream. Returns an error if called outside of an agent execution context.

  • chatModelAgentExecCtx: An internal execution context struct that carries the async generator reference, enabling event emission from anywhere within the middleware chain.

  • Context injection: The execution context is now properly injected in both:

    • Chain mode (simple chat model without tools)
    • React mode (chat model with tool calling capabilities)

Test Coverage

Added comprehensive test cases covering:

  • Error handling when called outside execution context
  • Error handling when generator is nil
  • Successful event emission in chain mode (via BeforeChatModel middleware)
  • Successful event emission in react mode (with tools)
  • Successful event emission in streaming mode
  • Context utility functions (withChatModelAgentExecCtx / getChatModelAgentExecCtx)
  • Edge cases for the send method

Usage Example

agent, _ := NewChatModelAgent(ctx, &ChatModelAgentConfig{
    // ... config
    Middlewares: []AgentMiddleware{
        {
            BeforeChatModel: func(ctx context.Context, state *ChatModelAgentState) error {
                // Emit a custom event before the model is called
                return SendEvent(ctx, &AgentEvent{
                    Output: &AgentOutput{
                        MessageOutput: &MessageVariant{
                            Message: schema.AssistantMessage("Processing...", nil),
                        },
                    },
                })
            },
        },
    },
})

Testing

  • All new tests pass
  • All existing tests in adk package pass
  • golangci-lint reports no issues

@codecov
Copy link

codecov bot commented Feb 14, 2026

Codecov Report

❌ Patch coverage is 90.47619% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.41%. Comparing base (65f8955) to head (2427cd6).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
adk/chatmodel.go 90.47% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #791      +/-   ##
==========================================
+ Coverage   80.40%   80.41%   +0.01%     
==========================================
  Files         129      129              
  Lines       13026    13047      +21     
==========================================
+ Hits        10473    10492      +19     
+ Misses       1753     1752       -1     
- Partials      800      803       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@hi-pender hi-pender merged commit e685d86 into main Feb 14, 2026
19 checks passed
@hi-pender hi-pender deleted the feat/copy_send_event branch February 14, 2026 07:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants