refactor: move cached screenResults from XTDriver to DriverSession

This commit is contained in:
lilong.129
2025-05-06 00:10:54 +08:00
parent ce93df1f23
commit 379fea9008
5 changed files with 22 additions and 15 deletions
+1 -1
View File
@@ -1 +1 @@
v5.0.0-beta-2505051654 v5.0.0-beta-2505060010
-4
View File
@@ -85,7 +85,6 @@ type IDriver interface {
func NewXTDriver(driver IDriver, opts ...option.AIServiceOption) (*XTDriver, error) { func NewXTDriver(driver IDriver, opts ...option.AIServiceOption) (*XTDriver, error) {
driverExt := &XTDriver{ driverExt := &XTDriver{
IDriver: driver, IDriver: driver,
screenResults: make([]*ScreenResult, 0),
} }
services := option.NewAIServiceOptions(opts...) services := option.NewAIServiceOptions(opts...)
@@ -114,7 +113,4 @@ type XTDriver struct {
IDriver IDriver
CVService ai.ICVService // OCR/CV CVService ai.ICVService // OCR/CV
LLMService ai.ILLMService // LLM LLMService ai.ILLMService // LLM
// cache screenshot results
screenResults []*ScreenResult
} }
+14 -6
View File
@@ -133,8 +133,9 @@ func (dExt *XTDriver) GetScreenResult(opts ...option.ActionOption) (screenResult
} }
} }
// cache screen result // save screen result to session
dExt.screenResults = append(dExt.screenResults, screenResult) session := dExt.GetSession()
session.screenResults = append(session.screenResults, screenResult)
log.Debug(). log.Debug().
Str("imagePath", imagePath). Str("imagePath", imagePath).
@@ -313,7 +314,7 @@ func MarkUIOperation(driver IDriver, actionType ActionMethod, actionCoordinates
} }
// create screenshot save path // create screenshot save path
timestamp := builtin.GenNameWithTimestamp("%d") timestamp := builtin.GenNameWithTimestamp("action_%d")
var imagePath string var imagePath string
if actionType == ACTION_TapAbsXY { if actionType == ACTION_TapAbsXY {
@@ -322,18 +323,18 @@ func MarkUIOperation(driver IDriver, actionType ActionMethod, actionCoordinates
} }
imagePath = filepath.Join( imagePath = filepath.Join(
config.GetConfig().ScreenShotsPath, config.GetConfig().ScreenShotsPath,
fmt.Sprintf("%s_tap_marked.png", timestamp), fmt.Sprintf("%s_tap.png", timestamp),
) )
x, y := actionCoordinates[0], actionCoordinates[1] x, y := actionCoordinates[0], actionCoordinates[1]
point := image.Point{X: int(x), Y: int(y)} point := image.Point{X: int(x), Y: int(y)}
err = SaveImageWithCircleMarker(compressedBufSource, point, imagePath) err = SaveImageWithCircleMarker(compressedBufSource, point, imagePath)
} else if actionType == ACTION_Swipe { } else if actionType == ACTION_Swipe || actionType == ACTION_Drag {
if len(actionCoordinates) != 4 { if len(actionCoordinates) != 4 {
return fmt.Errorf("invalid swipe action coordinates: %v", actionCoordinates) return fmt.Errorf("invalid swipe action coordinates: %v", actionCoordinates)
} }
imagePath = filepath.Join( imagePath = filepath.Join(
config.GetConfig().ScreenShotsPath, config.GetConfig().ScreenShotsPath,
fmt.Sprintf("%s_swipe_marked.png", timestamp), fmt.Sprintf("%s_%s.png", timestamp, actionType),
) )
fromX, fromY := actionCoordinates[0], actionCoordinates[1] fromX, fromY := actionCoordinates[0], actionCoordinates[1]
toX, toY := actionCoordinates[2], actionCoordinates[3] toX, toY := actionCoordinates[2], actionCoordinates[3]
@@ -353,6 +354,13 @@ func MarkUIOperation(driver IDriver, actionType ActionMethod, actionCoordinates
Str("imagePath", imagePath). Str("imagePath", imagePath).
Int64("duration(ms)", time.Since(start).Milliseconds()). Int64("duration(ms)", time.Since(start).Milliseconds()).
Msg("mark UI operation success") Msg("mark UI operation success")
// save screenshot to session
session := driver.GetSession()
session.screenResults = append(session.screenResults, &ScreenResult{
bufSource: compressedBufSource,
ImagePath: imagePath,
})
} }
return nil return nil
+5 -1
View File
@@ -47,9 +47,9 @@ func NewDriverSession() *DriverSession {
client: &http.Client{ client: &http.Client{
Timeout: timeout, Timeout: timeout,
}, },
requests: make([]*DriverRequests, 0),
maxRetry: 5, maxRetry: 5,
} }
session.Reset()
return session return session
} }
@@ -66,10 +66,14 @@ type DriverSession struct {
// cache driver request and response // cache driver request and response
requests []*DriverRequests requests []*DriverRequests
// cache screenshot results
screenResults []*ScreenResult
} }
func (s *DriverSession) Reset() { func (s *DriverSession) Reset() {
s.requests = make([]*DriverRequests, 0) s.requests = make([]*DriverRequests, 0)
s.screenResults = make([]*ScreenResult, 0)
} }
func (s *DriverSession) SetBaseURL(baseUrl string) { func (s *DriverSession) SetBaseURL(baseUrl string) {
+1 -2
View File
@@ -117,11 +117,10 @@ func (dExt *XTDriver) GetData(withReset bool) map[string]interface{} {
session := dExt.GetSession() session := dExt.GetSession()
data := map[string]interface{}{ data := map[string]interface{}{
"requests": session.History(), "requests": session.History(),
"screen_results": dExt.screenResults, "screen_results": session.screenResults,
} }
if withReset { if withReset {
session.Reset() session.Reset()
dExt.screenResults = make([]*ScreenResult, 0)
} }
return data return data
} }