refactor: enhance event consumption logic and add graph tool completion test
This commit is contained in:
@@ -74,6 +74,8 @@ func consumeAgentEvents(events *adk.AsyncIterator[*adk.AgentEvent], summary *Run
|
|||||||
summary.Status = "interrupted"
|
summary.Status = "interrupted"
|
||||||
case strings.TrimSpace(summary.ReplyText) != "":
|
case strings.TrimSpace(summary.ReplyText) != "":
|
||||||
summary.Status = "completed"
|
summary.Status = "completed"
|
||||||
|
case hasInvokedGraphTool(summary.InvokedToolCodes):
|
||||||
|
summary.Status = "completed"
|
||||||
default:
|
default:
|
||||||
summary.Status = "fallback"
|
summary.Status = "fallback"
|
||||||
}
|
}
|
||||||
@@ -81,6 +83,15 @@ func consumeAgentEvents(events *adk.AsyncIterator[*adk.AgentEvent], summary *Run
|
|||||||
summary.ToolCallCount = len(summary.InvokedToolCodes)
|
summary.ToolCallCount = len(summary.InvokedToolCodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func hasInvokedGraphTool(toolCodes []string) bool {
|
||||||
|
for _, toolCode := range toolCodes {
|
||||||
|
if toolx.ResolveToolSourceType(toolCode) == enums.ToolSourceTypeGraph {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
func buildInterruptSummaries(event *adk.AgentEvent) []InterruptContextSummary {
|
func buildInterruptSummaries(event *adk.AgentEvent) []InterruptContextSummary {
|
||||||
if event == nil || event.Action == nil || event.Action.Interrupted == nil {
|
if event == nil || event.Action == nil || event.Action.Interrupted == nil {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -39,3 +39,34 @@ func TestConsumeAgentEventsUsesGraphToolTextAsReplyFallback(t *testing.T) {
|
|||||||
t.Fatalf("unexpected summary status: %q", summary.Status)
|
t.Fatalf("unexpected summary status: %q", summary.Status)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConsumeAgentEventsCompletesGraphToolWithNoVisibleReply(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.Close()
|
||||||
|
|
||||||
|
consumeAgentEvents(events, summary, nil, map[string]string{
|
||||||
|
toolx.GraphHandoffConversation.Name: toolx.GraphHandoffConversation.Code,
|
||||||
|
})
|
||||||
|
|
||||||
|
if summary.ReplyText != "" {
|
||||||
|
t.Fatalf("expected no reply text, got %q", summary.ReplyText)
|
||||||
|
}
|
||||||
|
if summary.Status != "completed" {
|
||||||
|
t.Fatalf("unexpected summary status: %q", summary.Status)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -79,7 +79,8 @@ func (g *HandoffGraph) Run(ctx context.Context, argumentsInJSON string) (string,
|
|||||||
if err := services.ConversationService.HandoffByAI(g.conversation.ID, g.aiAgent, state.Reason); err != nil {
|
if err := services.ConversationService.HandoffByAI(g.conversation.ID, g.aiAgent, state.Reason); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
return g.buildSuccessReply(), nil
|
// ConversationService sends the customer-visible handoff notice according to the dispatch decision.
|
||||||
|
return "", nil
|
||||||
case ConfirmationDecisionCancel:
|
case ConfirmationDecisionCancel:
|
||||||
return CancelHandoffReply, nil
|
return CancelHandoffReply, nil
|
||||||
default:
|
default:
|
||||||
@@ -109,10 +110,6 @@ func (g *HandoffGraph) buildConfirmationPrompt(reason string) string {
|
|||||||
return fmt.Sprintf("我准备为你转接人工客服。\n原因:%s\n请直接回复“确认”或“取消”。", strings.TrimSpace(reason))
|
return fmt.Sprintf("我准备为你转接人工客服。\n原因:%s\n请直接回复“确认”或“取消”。", strings.TrimSpace(reason))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *HandoffGraph) buildSuccessReply() string {
|
|
||||||
return "已为你转接人工客服,请稍候。,请稍候。"
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseHandoffDecision(value string) ConfirmationDecision {
|
func parseHandoffDecision(value string) ConfirmationDecision {
|
||||||
return ParseConfirmationDecision(value)
|
return ParseConfirmationDecision(value)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user