mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-07 08:27:35 +08:00
fix: uixt tool take_screenshot
This commit is contained in:
@@ -1 +1 @@
|
|||||||
v5.0.0-beta-2505211747
|
v5.0.0-beta-2505211805
|
||||||
|
|||||||
+1
-1
@@ -112,7 +112,7 @@ func (c *Chat) runPrompt(ctx context.Context, prompt string) error {
|
|||||||
|
|
||||||
// Create user message
|
// Create user message
|
||||||
planningOpts := &ai.PlanningOptions{
|
planningOpts := &ai.PlanningOptions{
|
||||||
UserInstruction: "chat with MCP tools",
|
UserInstruction: prompt,
|
||||||
Message: &schema.Message{
|
Message: &schema.Message{
|
||||||
Role: schema.User,
|
Role: schema.User,
|
||||||
Content: prompt,
|
Content: prompt,
|
||||||
|
|||||||
@@ -394,30 +394,3 @@ func handleToolError(result *mcp.CallToolResult) error {
|
|||||||
}
|
}
|
||||||
return fmt.Errorf("tool error: unknown error")
|
return fmt.Errorf("tool error: unknown error")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScreenshotBase64 get screenshot base64 for the given platform and serial
|
|
||||||
func (h *MCPHost) ScreenshotBase64(ctx context.Context, platform, serial string) (string, error) {
|
|
||||||
driver, err := h.GetOrCreateDriver(platform, serial)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return uixt.GetScreenShotBufferBase64(driver)
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetOrCreateDriver get or create a driver for the given platform and serial
|
|
||||||
func (h *MCPHost) GetOrCreateDriver(platform, serial string) (*uixt.XTDriver, error) {
|
|
||||||
h.mu.Lock()
|
|
||||||
defer h.mu.Unlock()
|
|
||||||
cacheKey := fmt.Sprintf("%s_%s", platform, serial)
|
|
||||||
if driver, ok := h.drivers[cacheKey]; ok {
|
|
||||||
return driver, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
driverExt, err := initDriverExt(platform, serial)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
// store driver in cache
|
|
||||||
h.drivers[cacheKey] = driverExt
|
|
||||||
return driverExt, nil
|
|
||||||
}
|
|
||||||
|
|||||||
+10
-12
@@ -2,7 +2,6 @@ package mcphost
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/base64"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
@@ -178,9 +177,11 @@ func (ums *MCPServer4XTDriver) addTools() {
|
|||||||
log.Info().Str("name", swipeTool.Name).Msg("Register tool")
|
log.Info().Str("name", swipeTool.Name).Msg("Register tool")
|
||||||
|
|
||||||
// ScreenShot Tool
|
// ScreenShot Tool
|
||||||
screenShotTool := mcp.NewTool("screenshot",
|
takeScreenShotParams := append(
|
||||||
mcp.WithDescription("Takes a screenshot of the device screen and returns it as a base64 encoded string."),
|
[]mcp.ToolOption{mcp.WithDescription("Take a screenshot of the mobile device. Use this to understand what's on screen. Do not cache this result.")},
|
||||||
|
commonToolOptions...,
|
||||||
)
|
)
|
||||||
|
screenShotTool := mcp.NewTool("take_screenshot", takeScreenShotParams...)
|
||||||
ums.mcpServer.AddTool(screenShotTool, ums.handleScreenShot)
|
ums.mcpServer.AddTool(screenShotTool, ums.handleScreenShot)
|
||||||
ums.tools = append(ums.tools, screenShotTool)
|
ums.tools = append(ums.tools, screenShotTool)
|
||||||
ums.handlerMap[screenShotTool.Name] = ums.handleScreenShot
|
ums.handlerMap[screenShotTool.Name] = ums.handleScreenShot
|
||||||
@@ -407,23 +408,20 @@ func (ums *MCPServer4XTDriver) handleDrag(ctx context.Context, request mcp.CallT
|
|||||||
|
|
||||||
// handleScreenShot handles the screenshot tool call.
|
// handleScreenShot handles the screenshot tool call.
|
||||||
func (ums *MCPServer4XTDriver) handleScreenShot(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
func (ums *MCPServer4XTDriver) handleScreenShot(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
||||||
log.Info().Msg("Executing ScreenShot")
|
log.Info().Msg("take screenshot")
|
||||||
driverExt, err := ums.setupXTDriver(ctx, request.Params.Arguments)
|
driverExt, err := ums.setupXTDriver(ctx, request.Params.Arguments)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
buffer, err := driverExt.ScreenShot()
|
|
||||||
|
bufferBase64, err := uixt.GetScreenShotBufferBase64(driverExt.IDriver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("ScreenShot failed")
|
log.Error().Err(err).Msg("ScreenShot failed")
|
||||||
return mcp.NewToolResultError(fmt.Sprintf("Failed to take screenshot: %v", err)), nil
|
return mcp.NewToolResultError(fmt.Sprintf("Failed to take screenshot: %v", err)), nil
|
||||||
}
|
}
|
||||||
if buffer == nil || buffer.Len() == 0 {
|
log.Debug().Int("imageBytes", len(bufferBase64)).Msg("take screenshot success")
|
||||||
log.Error().Msg("Screenshot buffer is nil or empty")
|
|
||||||
return mcp.NewToolResultError("Screenshot returned empty buffer"), nil
|
return mcp.NewToolResultImage("screenshot", bufferBase64, "image/jpeg"), nil
|
||||||
}
|
|
||||||
encodedString := base64.StdEncoding.EncodeToString(buffer.Bytes())
|
|
||||||
log.Info().Int("image_size_bytes", len(buffer.Bytes())).Int("base64_len", len(encodedString)).Msg("Screenshot successful")
|
|
||||||
return mcp.NewToolResultText(encodedString), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// setupXTDriver initializes an XTDriver based on the platform and serial.
|
// setupXTDriver initializes an XTDriver based on the platform and serial.
|
||||||
|
|||||||
Reference in New Issue
Block a user