Skip to content

Commit 3d8f620

Browse files
refactor(adk): genericize callback types and unify duplicate helpers
- Introduce TypedAgentCallbackInput[M] and TypedAgentCallbackOutput[M] as generic replacements for the removed AgenticCallbackInput/Output - Add ConvTypedCallbackInput[M] and ConvTypedCallbackOutput[M] generic converter functions - Unify copyEventIterator/copyAgenticEventIterator into single generic copyTypedEventIterator[M] - Unify copyAgenticCallbackOutput into generic copyTypedCallbackOutput[M] - Remove copyAgentEvent/copyAgenticEvent wrappers, use copyTypedAgentEvent directly at all call sites - Rename ComponentOfAgentic to ComponentOfAgenticAgent for naming consistency Change-Id: If56fcf3005921c374cb3b5ed3f1a7fbf1fbe6fbe
1 parent a8a6ffc commit 3d8f620

11 files changed

Lines changed: 111 additions & 156 deletions

adk/agent_tool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func (at *typedAgentTool[M]) InvokableRun(ctx context.Context, argumentsInJSON s
231231
event.RunPath = rp
232232
}
233233
if msgEvent, ok := any(event).(*AgentEvent); ok {
234-
tmp := copyAgentEvent(msgEvent)
234+
tmp := copyTypedAgentEvent(msgEvent)
235235
gen.Send(msgEvent)
236236
event = any(tmp).(*TypedAgentEvent[M])
237237
} else {

adk/agentic_callback_integration_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type agenticCallbackRecorder struct {
3434
onStartCalled bool
3535
onEndCalled bool
3636
runInfo *callbacks.RunInfo
37-
inputReceived *AgenticCallbackInput
37+
inputReceived *TypedAgentCallbackInput[*schema.AgenticMessage]
3838
eventsReceived []*TypedAgentEvent[*schema.AgenticMessage]
3939
eventsDone chan struct{}
4040
closeOnce sync.Once
@@ -64,28 +64,28 @@ func newAgenticRecordingHandler(recorder *agenticCallbackRecorder) callbacks.Han
6464
recorder.eventsDone = make(chan struct{})
6565
return callbacks.NewHandlerBuilder().
6666
OnStartFn(func(ctx context.Context, info *callbacks.RunInfo, input callbacks.CallbackInput) context.Context {
67-
if info.Component != ComponentOfAgentic {
67+
if info.Component != ComponentOfAgenticAgent {
6868
return ctx
6969
}
7070
recorder.mu.Lock()
7171
defer recorder.mu.Unlock()
7272
recorder.onStartCalled = true
7373
recorder.runInfo = info
74-
if agentInput := ConvAgenticCallbackInput(input); agentInput != nil {
74+
if agentInput := ConvTypedCallbackInput[*schema.AgenticMessage](input); agentInput != nil {
7575
recorder.inputReceived = agentInput
7676
}
7777
return ctx
7878
}).
7979
OnEndFn(func(ctx context.Context, info *callbacks.RunInfo, output callbacks.CallbackOutput) context.Context {
80-
if info.Component != ComponentOfAgentic {
80+
if info.Component != ComponentOfAgenticAgent {
8181
return ctx
8282
}
8383
recorder.mu.Lock()
8484
recorder.onEndCalled = true
8585
recorder.runInfo = info
8686
recorder.mu.Unlock()
8787

88-
if agentOutput := ConvAgenticCallbackOutput(output); agentOutput != nil {
88+
if agentOutput := ConvTypedCallbackOutput[*schema.AgenticMessage](output); agentOutput != nil {
8989
if agentOutput.Events != nil {
9090
go func() {
9191
defer recorder.closeOnce.Do(func() { close(recorder.eventsDone) })
@@ -158,7 +158,7 @@ func TestAgenticCallback(t *testing.T) {
158158
t.Run("RunInfo_Fields", func(t *testing.T) {
159159
require.NotNil(t, recorder.runInfo)
160160
assert.Equal(t, "TestChatAgent", recorder.runInfo.Name)
161-
assert.Equal(t, ComponentOfAgentic, recorder.runInfo.Component)
161+
assert.Equal(t, ComponentOfAgenticAgent, recorder.runInfo.Component)
162162
})
163163

164164
t.Run("Events_MatchAgentOutput", func(t *testing.T) {
@@ -226,7 +226,7 @@ func TestCoverage_WrapAgenticIterWithOnEnd(t *testing.T) {
226226
return ctx
227227
}).
228228
OnEndFn(func(ctx context.Context, info *callbacks.RunInfo, output callbacks.CallbackOutput) context.Context {
229-
if info.Component == ComponentOfAgentic {
229+
if info.Component == ComponentOfAgenticAgent {
230230
onEndCalled = true
231231
}
232232
return ctx
@@ -236,7 +236,7 @@ func TestCoverage_WrapAgenticIterWithOnEnd(t *testing.T) {
236236
ctx = initAgenticCallbacks(ctx, "test-agent", "ChatModel",
237237
WithCallbacks(handler))
238238

239-
cbInput := &AgenticCallbackInput{
239+
cbInput := &TypedAgentCallbackInput[*schema.AgenticMessage]{
240240
Input: &TypedAgentInput[*schema.AgenticMessage]{
241241
Messages: []*schema.AgenticMessage{schema.UserAgenticMessage("Hi")},
242242
},

adk/agentic_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ func TestCoverage_CopyAgenticEvent(t *testing.T) {
12121212
},
12131213
}
12141214

1215-
copied := copyAgenticEvent(original)
1215+
copied := copyTypedAgentEvent(original)
12161216
assert.Equal(t, original.AgentName, copied.AgentName)
12171217
assert.Equal(t, len(original.RunPath), len(copied.RunPath))
12181218
assert.Equal(t, original.Action, copied.Action)

adk/callback.go

Lines changed: 28 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/cloudwego/eino/callbacks"
2323
"github.com/cloudwego/eino/components"
2424
icb "github.com/cloudwego/eino/internal/callbacks"
25-
"github.com/cloudwego/eino/schema"
2625
)
2726

2827
// AgentCallbackInput represents the input passed to agent callbacks during OnStart.
@@ -44,18 +43,18 @@ type AgentCallbackOutput struct {
4443
Events *AsyncIterator[*AgentEvent]
4544
}
4645

47-
func copyEventIterator(iter *AsyncIterator[*AgentEvent], n int) []*AsyncIterator[*AgentEvent] {
46+
func copyTypedEventIterator[M messageType](iter *AsyncIterator[*TypedAgentEvent[M]], n int) []*AsyncIterator[*TypedAgentEvent[M]] {
4847
if n <= 0 {
4948
return nil
5049
}
5150
if n == 1 {
52-
return []*AsyncIterator[*AgentEvent]{iter}
51+
return []*AsyncIterator[*TypedAgentEvent[M]]{iter}
5352
}
5453

55-
iterators := make([]*AsyncIterator[*AgentEvent], n)
56-
generators := make([]*AsyncGenerator[*AgentEvent], n)
54+
iterators := make([]*AsyncIterator[*TypedAgentEvent[M]], n)
55+
generators := make([]*AsyncGenerator[*TypedAgentEvent[M]], n)
5756
for i := 0; i < n; i++ {
58-
iterators[i], generators[i] = NewAsyncIteratorPair[*AgentEvent]()
57+
iterators[i], generators[i] = NewAsyncIteratorPair[*TypedAgentEvent[M]]()
5958
}
6059

6160
go func() {
@@ -71,7 +70,7 @@ func copyEventIterator(iter *AsyncIterator[*AgentEvent], n int) []*AsyncIterator
7170
break
7271
}
7372
for i := 0; i < n-1; i++ {
74-
generators[i].Send(copyAgentEvent(event))
73+
generators[i].Send(copyTypedAgentEvent(event))
7574
}
7675
generators[n-1].Send(event)
7776
}
@@ -88,7 +87,7 @@ func copyAgentCallbackOutput(out *AgentCallbackOutput, n int) []*AgentCallbackOu
8887
}
8988
return result
9089
}
91-
iters := copyEventIterator(out.Events, n)
90+
iters := copyTypedEventIterator(out.Events, n)
9291
result := make([]*AgentCallbackOutput, n)
9392
for i, iter := range iters {
9493
result[i] = &AgentCallbackOutput{Events: iter}
@@ -135,91 +134,55 @@ func getAgentType(agent Agent) string {
135134
return ""
136135
}
137136

138-
// AgenticCallbackInput represents the input passed to agentic agent callbacks during OnStart.
139-
// Use ConvAgenticCallbackInput to safely convert from callbacks.CallbackInput.
140-
type AgenticCallbackInput struct {
137+
// TypedAgentCallbackInput represents the input passed to typed agent callbacks during OnStart.
138+
// Use ConvTypedCallbackInput to safely convert from callbacks.CallbackInput.
139+
type TypedAgentCallbackInput[M messageType] struct {
141140
// Input contains the agent input for a new run. Nil when resuming.
142-
Input *TypedAgentInput[*schema.AgenticMessage]
141+
Input *TypedAgentInput[M]
143142
// ResumeInfo contains resume information when resuming from an interrupt. Nil for new runs.
144143
ResumeInfo *ResumeInfo
145144
}
146145

147-
// AgenticCallbackOutput represents the output passed to agentic agent callbacks during OnEnd.
148-
// Use ConvAgenticCallbackOutput to safely convert from callbacks.CallbackOutput.
146+
// TypedAgentCallbackOutput represents the output passed to typed agent callbacks during OnEnd.
147+
// Use ConvTypedCallbackOutput to safely convert from callbacks.CallbackOutput.
149148
//
150149
// Important: The Events iterator should be consumed asynchronously to avoid blocking
151150
// the agent execution. Each callback handler receives an independent copy of the iterator.
152-
type AgenticCallbackOutput struct {
153-
// Events provides a stream of agentic agent events. Each handler receives its own copy.
154-
Events *AsyncIterator[*TypedAgentEvent[*schema.AgenticMessage]]
151+
type TypedAgentCallbackOutput[M messageType] struct {
152+
// Events provides a stream of agent events. Each handler receives its own copy.
153+
Events *AsyncIterator[*TypedAgentEvent[M]]
155154
}
156155

157-
// ConvAgenticCallbackInput converts a callbacks.CallbackInput to *AgenticCallbackInput.
156+
// ConvTypedCallbackInput converts a callbacks.CallbackInput to *TypedAgentCallbackInput[M].
158157
// Returns nil if the input is not of the expected type.
159-
func ConvAgenticCallbackInput(input callbacks.CallbackInput) *AgenticCallbackInput {
160-
if v, ok := input.(*AgenticCallbackInput); ok {
158+
func ConvTypedCallbackInput[M messageType](input callbacks.CallbackInput) *TypedAgentCallbackInput[M] {
159+
if v, ok := input.(*TypedAgentCallbackInput[M]); ok {
161160
return v
162161
}
163162
return nil
164163
}
165164

166-
// ConvAgenticCallbackOutput converts a callbacks.CallbackOutput to *AgenticCallbackOutput.
165+
// ConvTypedCallbackOutput converts a callbacks.CallbackOutput to *TypedAgentCallbackOutput[M].
167166
// Returns nil if the output is not of the expected type.
168-
func ConvAgenticCallbackOutput(output callbacks.CallbackOutput) *AgenticCallbackOutput {
169-
if v, ok := output.(*AgenticCallbackOutput); ok {
167+
func ConvTypedCallbackOutput[M messageType](output callbacks.CallbackOutput) *TypedAgentCallbackOutput[M] {
168+
if v, ok := output.(*TypedAgentCallbackOutput[M]); ok {
170169
return v
171170
}
172171
return nil
173172
}
174173

175-
func copyAgenticEventIterator(iter *AsyncIterator[*TypedAgentEvent[*schema.AgenticMessage]], n int) []*AsyncIterator[*TypedAgentEvent[*schema.AgenticMessage]] {
176-
if n <= 0 {
177-
return nil
178-
}
179-
if n == 1 {
180-
return []*AsyncIterator[*TypedAgentEvent[*schema.AgenticMessage]]{iter}
181-
}
182-
183-
iterators := make([]*AsyncIterator[*TypedAgentEvent[*schema.AgenticMessage]], n)
184-
generators := make([]*AsyncGenerator[*TypedAgentEvent[*schema.AgenticMessage]], n)
185-
for i := 0; i < n; i++ {
186-
iterators[i], generators[i] = NewAsyncIteratorPair[*TypedAgentEvent[*schema.AgenticMessage]]()
187-
}
188-
189-
go func() {
190-
defer func() {
191-
for _, g := range generators {
192-
g.Close()
193-
}
194-
}()
195-
196-
for {
197-
event, ok := iter.Next()
198-
if !ok {
199-
break
200-
}
201-
for i := 0; i < n-1; i++ {
202-
generators[i].Send(copyAgenticEvent(event))
203-
}
204-
generators[n-1].Send(event)
205-
}
206-
}()
207-
208-
return iterators
209-
}
210-
211-
func copyAgenticCallbackOutput(out *AgenticCallbackOutput, n int) []*AgenticCallbackOutput {
174+
func copyTypedCallbackOutput[M messageType](out *TypedAgentCallbackOutput[M], n int) []*TypedAgentCallbackOutput[M] {
212175
if out == nil || out.Events == nil {
213-
result := make([]*AgenticCallbackOutput, n)
176+
result := make([]*TypedAgentCallbackOutput[M], n)
214177
for i := 0; i < n; i++ {
215178
result[i] = out
216179
}
217180
return result
218181
}
219-
iters := copyAgenticEventIterator(out.Events, n)
220-
result := make([]*AgenticCallbackOutput, n)
182+
iters := copyTypedEventIterator(out.Events, n)
183+
result := make([]*TypedAgentCallbackOutput[M], n)
221184
for i, iter := range iters {
222-
result[i] = &AgenticCallbackOutput{Events: iter}
185+
result[i] = &TypedAgentCallbackOutput[M]{Events: iter}
223186
}
224187
return result
225188
}
@@ -228,7 +191,7 @@ func initAgenticCallbacks(ctx context.Context, agentName, agentType string, opts
228191
ri := &callbacks.RunInfo{
229192
Name: agentName,
230193
Type: agentType,
231-
Component: ComponentOfAgentic,
194+
Component: ComponentOfAgenticAgent,
232195
}
233196

234197
o := getCommonOptions(nil, opts...)

0 commit comments

Comments
 (0)