refactor: implement off-hours handoff logic and enhance related tests
This commit is contained in:
@@ -18,6 +18,7 @@ func consumeAgentEvents(events *adk.AsyncIterator[*adk.AgentEvent], summary *Run
|
||||
if collector == nil {
|
||||
collector = callbacks.NewRuntimeTraceCollector()
|
||||
}
|
||||
suppressAssistantReply := false
|
||||
for {
|
||||
event, ok := events.Next()
|
||||
if !ok {
|
||||
@@ -44,6 +45,9 @@ func consumeAgentEvents(events *adk.AsyncIterator[*adk.AgentEvent], summary *Run
|
||||
messageOutput := event.Output.MessageOutput
|
||||
switch messageOutput.Role {
|
||||
case schema.Assistant:
|
||||
if suppressAssistantReply {
|
||||
continue
|
||||
}
|
||||
replyText := strings.TrimSpace(messageOutput.Message.Content)
|
||||
if replyText != "" {
|
||||
summary.ReplyText = replyText
|
||||
@@ -62,6 +66,8 @@ func consumeAgentEvents(events *adk.AsyncIterator[*adk.AgentEvent], summary *Run
|
||||
toolReplyText := strings.TrimSpace(messageOutput.Message.Content)
|
||||
if toolReplyText != "" {
|
||||
summary.ReplyText = toolReplyText
|
||||
} else if toolCode == toolx.GraphHandoffConversation.Code {
|
||||
suppressAssistantReply = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -70,3 +70,44 @@ func TestConsumeAgentEventsCompletesGraphToolWithNoVisibleReply(t *testing.T) {
|
||||
t.Fatalf("unexpected summary status: %q", summary.Status)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConsumeAgentEventsSuppressesAssistantReplyAfterSilentHandoffTool(t *testing.T) {
|
||||
summary := &RunResult{
|
||||
Status: "started",
|
||||
InvokedToolCodes: make([]string, 0),
|
||||
}
|
||||
events, gen := adk.NewAsyncIteratorPair[*adk.AgentEvent]()
|
||||
gen.Send(&adk.AgentEvent{
|
||||
Output: &adk.AgentOutput{
|
||||
MessageOutput: &adk.MessageVariant{
|
||||
Role: schema.Tool,
|
||||
ToolName: toolx.GraphHandoffConversation.Name,
|
||||
Message: &schema.Message{
|
||||
Content: "",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
gen.Send(&adk.AgentEvent{
|
||||
Output: &adk.AgentOutput{
|
||||
MessageOutput: &adk.MessageVariant{
|
||||
Role: schema.Assistant,
|
||||
Message: &schema.Message{
|
||||
Content: "好的,已为您发起转接人工客服的请求。系统正在为您确认,请稍候。",
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
gen.Close()
|
||||
|
||||
consumeAgentEvents(events, summary, nil, map[string]string{
|
||||
toolx.GraphHandoffConversation.Name: toolx.GraphHandoffConversation.Code,
|
||||
})
|
||||
|
||||
if summary.ReplyText != "" {
|
||||
t.Fatalf("expected assistant reply after silent handoff to be suppressed, got %q", summary.ReplyText)
|
||||
}
|
||||
if summary.Status != "completed" {
|
||||
t.Fatalf("unexpected summary status: %q", summary.Status)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user