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

View File

@@ -1 +1 @@
v5.0.0-beta-2505051654
v5.0.0-beta-2505060010

View File

@@ -84,8 +84,7 @@ type IDriver interface {
func NewXTDriver(driver IDriver, opts ...option.AIServiceOption) (*XTDriver, error) {
driverExt := &XTDriver{
IDriver: driver,
screenResults: make([]*ScreenResult, 0),
IDriver: driver,
}
services := option.NewAIServiceOptions(opts...)
@@ -114,7 +113,4 @@ type XTDriver struct {
IDriver
CVService ai.ICVService // OCR/CV
LLMService ai.ILLMService // LLM
// cache screenshot results
screenResults []*ScreenResult
}

View File

@@ -133,8 +133,9 @@ func (dExt *XTDriver) GetScreenResult(opts ...option.ActionOption) (screenResult
}
}
// cache screen result
dExt.screenResults = append(dExt.screenResults, screenResult)
// save screen result to session
session := dExt.GetSession()
session.screenResults = append(session.screenResults, screenResult)
log.Debug().
Str("imagePath", imagePath).
@@ -313,7 +314,7 @@ func MarkUIOperation(driver IDriver, actionType ActionMethod, actionCoordinates
}
// create screenshot save path
timestamp := builtin.GenNameWithTimestamp("%d")
timestamp := builtin.GenNameWithTimestamp("action_%d")
var imagePath string
if actionType == ACTION_TapAbsXY {
@@ -322,18 +323,18 @@ func MarkUIOperation(driver IDriver, actionType ActionMethod, actionCoordinates
}
imagePath = filepath.Join(
config.GetConfig().ScreenShotsPath,
fmt.Sprintf("%s_tap_marked.png", timestamp),
fmt.Sprintf("%s_tap.png", timestamp),
)
x, y := actionCoordinates[0], actionCoordinates[1]
point := image.Point{X: int(x), Y: int(y)}
err = SaveImageWithCircleMarker(compressedBufSource, point, imagePath)
} else if actionType == ACTION_Swipe {
} else if actionType == ACTION_Swipe || actionType == ACTION_Drag {
if len(actionCoordinates) != 4 {
return fmt.Errorf("invalid swipe action coordinates: %v", actionCoordinates)
}
imagePath = filepath.Join(
config.GetConfig().ScreenShotsPath,
fmt.Sprintf("%s_swipe_marked.png", timestamp),
fmt.Sprintf("%s_%s.png", timestamp, actionType),
)
fromX, fromY := actionCoordinates[0], actionCoordinates[1]
toX, toY := actionCoordinates[2], actionCoordinates[3]
@@ -353,6 +354,13 @@ func MarkUIOperation(driver IDriver, actionType ActionMethod, actionCoordinates
Str("imagePath", imagePath).
Int64("duration(ms)", time.Since(start).Milliseconds()).
Msg("mark UI operation success")
// save screenshot to session
session := driver.GetSession()
session.screenResults = append(session.screenResults, &ScreenResult{
bufSource: compressedBufSource,
ImagePath: imagePath,
})
}
return nil

View File

@@ -47,9 +47,9 @@ func NewDriverSession() *DriverSession {
client: &http.Client{
Timeout: timeout,
},
requests: make([]*DriverRequests, 0),
maxRetry: 5,
}
session.Reset()
return session
}
@@ -66,10 +66,14 @@ type DriverSession struct {
// cache driver request and response
requests []*DriverRequests
// cache screenshot results
screenResults []*ScreenResult
}
func (s *DriverSession) Reset() {
s.requests = make([]*DriverRequests, 0)
s.screenResults = make([]*ScreenResult, 0)
}
func (s *DriverSession) SetBaseURL(baseUrl string) {

View File

@@ -117,11 +117,10 @@ func (dExt *XTDriver) GetData(withReset bool) map[string]interface{} {
session := dExt.GetSession()
data := map[string]interface{}{
"requests": session.History(),
"screen_results": dExt.screenResults,
"screen_results": session.screenResults,
}
if withReset {
session.Reset()
dExt.screenResults = make([]*ScreenResult, 0)
}
return data
}