mirror of
https://github.com/httprunner/httprunner.git
synced 2026-06-26 01:51:29 +08:00
refactor ToolSwipe to delegate to existing tools instead of duplicating logic
- Modified ToolSwipe.ConvertActionToCallToolRequest to delegate to ToolSwipeDirection and ToolSwipeCoordinate - Removed duplicate parameter handling logic in favor of reusing existing implementations - Fixed linter error by removing unused variable - Maintained backward compatibility while reducing code duplication - All tests pass, confirming the refactoring is successful
This commit is contained in:
@@ -1 +1 @@
|
||||
v5.0.0-beta-2505262239
|
||||
v5.0.0-beta-2505262242
|
||||
|
||||
@@ -910,142 +910,44 @@ func (t *ToolSwipe) Options() []mcp.ToolOption {
|
||||
|
||||
func (t *ToolSwipe) Implement() server.ToolHandlerFunc {
|
||||
return func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||
driverExt, err := setupXTDriver(ctx, request.Params.Arguments)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("setup driver failed: %w", err)
|
||||
}
|
||||
|
||||
// Check if it's direction-based swipe (has "direction" parameter)
|
||||
if direction, exists := request.Params.Arguments["direction"]; exists {
|
||||
// Direction-based swipe
|
||||
directionStr, ok := direction.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("direction parameter must be a string")
|
||||
}
|
||||
|
||||
// Validate direction
|
||||
validDirections := []string{"up", "down", "left", "right"}
|
||||
isValid := false
|
||||
for _, validDir := range validDirections {
|
||||
if directionStr == validDir {
|
||||
isValid = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isValid {
|
||||
return nil, fmt.Errorf("invalid swipe direction: %s, expected one of: %v", directionStr, validDirections)
|
||||
}
|
||||
|
||||
log.Info().Str("direction", directionStr).Msg("performing direction-based swipe")
|
||||
|
||||
// Extract duration and press duration
|
||||
var duration, pressDuration float64
|
||||
if d, exists := request.Params.Arguments["duration"]; exists {
|
||||
if dFloat, ok := d.(float64); ok {
|
||||
duration = dFloat
|
||||
}
|
||||
}
|
||||
if pd, exists := request.Params.Arguments["pressDuration"]; exists {
|
||||
if pdFloat, ok := pd.(float64); ok {
|
||||
pressDuration = pdFloat
|
||||
}
|
||||
}
|
||||
|
||||
opts := []option.ActionOption{
|
||||
option.WithPreMarkOperation(true),
|
||||
}
|
||||
if duration > 0 {
|
||||
opts = append(opts, option.WithDuration(duration))
|
||||
}
|
||||
if pressDuration > 0 {
|
||||
opts = append(opts, option.WithPressDuration(pressDuration))
|
||||
}
|
||||
|
||||
// Convert direction to coordinates and perform swipe
|
||||
switch directionStr {
|
||||
case "up":
|
||||
err = driverExt.Swipe(0.5, 0.5, 0.5, 0.1, opts...)
|
||||
case "down":
|
||||
err = driverExt.Swipe(0.5, 0.5, 0.5, 0.9, opts...)
|
||||
case "left":
|
||||
err = driverExt.Swipe(0.5, 0.5, 0.1, 0.5, opts...)
|
||||
case "right":
|
||||
err = driverExt.Swipe(0.5, 0.5, 0.9, 0.5, opts...)
|
||||
default:
|
||||
return mcp.NewToolResultError(fmt.Sprintf("Unexpected swipe direction: %s", directionStr)), nil
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return mcp.NewToolResultError(fmt.Sprintf("Direction swipe failed: %s", err.Error())), nil
|
||||
}
|
||||
|
||||
return mcp.NewToolResultText(fmt.Sprintf("Successfully swiped %s", directionStr)), nil
|
||||
|
||||
if _, exists := request.Params.Arguments["direction"]; exists {
|
||||
// Delegate to ToolSwipeDirection
|
||||
directionTool := &ToolSwipeDirection{}
|
||||
return directionTool.Implement()(ctx, request)
|
||||
} else {
|
||||
// Coordinate-based swipe
|
||||
var swipeAdvReq option.SwipeAdvancedRequest
|
||||
if err := mapToStruct(request.Params.Arguments, &swipeAdvReq); err != nil {
|
||||
return nil, fmt.Errorf("parse parameters error: %w", err)
|
||||
}
|
||||
|
||||
log.Info().
|
||||
Float64("fromX", swipeAdvReq.FromX).Float64("fromY", swipeAdvReq.FromY).
|
||||
Float64("toX", swipeAdvReq.ToX).Float64("toY", swipeAdvReq.ToY).
|
||||
Msg("performing coordinate-based swipe")
|
||||
|
||||
params := []float64{swipeAdvReq.FromX, swipeAdvReq.FromY, swipeAdvReq.ToX, swipeAdvReq.ToY}
|
||||
opts := []option.ActionOption{}
|
||||
if swipeAdvReq.Duration > 0 {
|
||||
opts = append(opts, option.WithDuration(swipeAdvReq.Duration))
|
||||
}
|
||||
if swipeAdvReq.PressDuration > 0 {
|
||||
opts = append(opts, option.WithPressDuration(swipeAdvReq.PressDuration))
|
||||
}
|
||||
|
||||
swipeAction := prepareSwipeAction(driverExt, params, opts...)
|
||||
err = swipeAction(driverExt)
|
||||
if err != nil {
|
||||
return mcp.NewToolResultError(fmt.Sprintf("Coordinate swipe failed: %s", err.Error())), nil
|
||||
}
|
||||
|
||||
return mcp.NewToolResultText(fmt.Sprintf("Successfully performed coordinate swipe from (%.2f, %.2f) to (%.2f, %.2f)",
|
||||
swipeAdvReq.FromX, swipeAdvReq.FromY, swipeAdvReq.ToX, swipeAdvReq.ToY)), nil
|
||||
// Delegate to ToolSwipeCoordinate
|
||||
coordinateTool := &ToolSwipeCoordinate{}
|
||||
return coordinateTool.Implement()(ctx, request)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *ToolSwipe) ConvertActionToCallToolRequest(action MobileAction) (mcp.CallToolRequest, error) {
|
||||
// Check if params is a string (direction-based swipe)
|
||||
if direction, ok := action.Params.(string); ok {
|
||||
arguments := map[string]any{
|
||||
"direction": direction,
|
||||
if _, ok := action.Params.(string); ok {
|
||||
// Delegate to ToolSwipeDirection but use our tool name
|
||||
directionTool := &ToolSwipeDirection{}
|
||||
request, err := directionTool.ConvertActionToCallToolRequest(action)
|
||||
if err != nil {
|
||||
return mcp.CallToolRequest{}, err
|
||||
}
|
||||
// Add duration and press duration from options
|
||||
if duration := action.ActionOptions.Duration; duration > 0 {
|
||||
arguments["duration"] = duration
|
||||
}
|
||||
if pressDuration := action.ActionOptions.PressDuration; pressDuration > 0 {
|
||||
arguments["pressDuration"] = pressDuration
|
||||
}
|
||||
return buildMCPCallToolRequest(t.Name(), arguments), nil
|
||||
// Change the tool name to use generic swipe
|
||||
request.Params.Name = string(t.Name())
|
||||
return request, nil
|
||||
}
|
||||
|
||||
// Check if params is a coordinate array (coordinate-based swipe)
|
||||
if paramSlice, err := builtin.ConvertToFloat64Slice(action.Params); err == nil && len(paramSlice) == 4 {
|
||||
arguments := map[string]any{
|
||||
"fromX": paramSlice[0],
|
||||
"fromY": paramSlice[1],
|
||||
"toX": paramSlice[2],
|
||||
"toY": paramSlice[3],
|
||||
// Delegate to ToolSwipeCoordinate but use our tool name
|
||||
coordinateTool := &ToolSwipeCoordinate{}
|
||||
request, err := coordinateTool.ConvertActionToCallToolRequest(action)
|
||||
if err != nil {
|
||||
return mcp.CallToolRequest{}, err
|
||||
}
|
||||
// Add duration and press duration from options
|
||||
if duration := action.ActionOptions.Duration; duration > 0 {
|
||||
arguments["duration"] = duration
|
||||
}
|
||||
if pressDuration := action.ActionOptions.PressDuration; pressDuration > 0 {
|
||||
arguments["pressDuration"] = pressDuration
|
||||
}
|
||||
return buildMCPCallToolRequest(t.Name(), arguments), nil
|
||||
// Change the tool name to use generic swipe
|
||||
request.Params.Name = string(t.Name())
|
||||
return request, nil
|
||||
}
|
||||
|
||||
return mcp.CallToolRequest{}, fmt.Errorf("invalid swipe params: %v, expected string direction or [fromX, fromY, toX, toY] coordinates", action.Params)
|
||||
|
||||
Reference in New Issue
Block a user