2026-04-10 16:00:10 +08:00
package tools
import (
"context"
2026-05-31 18:43:48 +08:00
"agent-desk/internal/ai/runtime/graphs"
"agent-desk/internal/ai/runtime/registry"
"agent-desk/internal/models"
"agent-desk/internal/pkg/toolx"
2026-04-10 16:00:10 +08:00
einotool "github.com/cloudwego/eino/components/tool"
"github.com/cloudwego/eino/schema"
einojsonschema "github.com/eino-contrib/jsonschema"
orderedmap "github.com/wk8/go-ordered-map/v2"
)
type HandoffGraphTool struct {
2026-04-17 18:48:45 +08:00
conversation models . Conversation
2026-04-17 17:57:01 +08:00
aiAgent models . AIAgent
2026-04-10 16:00:10 +08:00
}
func NewHandoffGraphTool () * HandoffGraphTool {
return & HandoffGraphTool {}
}
2026-04-14 09:56:37 +08:00
func ( t * HandoffGraphTool ) Spec () toolx . ToolSpec {
return toolx . GraphHandoffConversation
}
2026-04-10 16:00:10 +08:00
func ( t * HandoffGraphTool ) Name () string {
2026-04-14 09:51:25 +08:00
return toolx . GraphHandoffConversation . Name
2026-04-10 16:00:10 +08:00
}
func ( t * HandoffGraphTool ) Code () string {
2026-04-14 09:51:25 +08:00
return toolx . GraphHandoffConversation . Code
2026-04-10 16:00:10 +08:00
}
func ( t * HandoffGraphTool ) Enabled ( ctx registry . Context ) bool {
2026-04-17 17:57:01 +08:00
return true
2026-04-10 16:00:10 +08:00
}
func ( t * HandoffGraphTool ) Build ( ctx registry . Context ) ( einotool . BaseTool , error ) {
if ! t . Enabled ( ctx ) {
return nil , nil
}
return & HandoffGraphTool {
conversation : ctx . Conversation ,
aiAgent : ctx . AIAgent ,
}, nil
}
func ( t * HandoffGraphTool ) Info ( ctx context . Context ) ( * schema . ToolInfo , error ) {
return & schema . ToolInfo {
2026-04-14 09:51:25 +08:00
Name : toolx . GraphHandoffConversation . Name ,
2026-05-31 20:06:44 +08:00
Desc : "Graph Tool. Handles handoff reason preparation, user confirmation, actual human handoff, and result return. Use only when the user explicitly asks for a human agent or you have confirmed that human handling is required. Do not repeat the call when the result has terminal=true and shouldRetry=false." ,
2026-04-10 16:00:10 +08:00
ParamsOneOf : schema . NewParamsOneOfByJSONSchema ( & einojsonschema . Schema {
Version : einojsonschema . Version ,
Type : "object" ,
Properties : orderedmap . New [ string , * einojsonschema . Schema ]( orderedmap . WithInitialData (
orderedmap . Pair [ string , * einojsonschema . Schema ]{
Key : "reason" ,
Value : & einojsonschema . Schema {
Type : "string" ,
2026-05-31 20:06:44 +08:00
Description : "Handoff reason. Briefly explain why a human is needed, such as explicit user request, manual verification, or after-sales handling." ,
2026-04-10 16:00:10 +08:00
},
},
)),
}),
Extra : map [ string ] any {
2026-04-14 09:51:25 +08:00
"toolCode" : toolx . GraphHandoffConversation . Code ,
2026-04-14 18:27:45 +08:00
"sourceType" : toolx . GraphHandoffConversation . SourceType ,
2026-04-10 16:00:10 +08:00
},
}, nil
}
func ( t * HandoffGraphTool ) InvokableRun ( ctx context . Context , argumentsInJSON string , opts ... einotool . Option ) ( string , error ) {
return graphs . NewHandoffGraph ( t . conversation , t . aiAgent ). Run ( ctx , argumentsInJSON )
}