refactor: add AIAction

This commit is contained in:
lilong.129
2025-03-22 12:08:42 +08:00
parent f46fcfb456
commit 5ebfca7f62
4 changed files with 28 additions and 36 deletions
+1 -1
View File
@@ -1 +1 @@
v5.0.0-beta-2503220119 v5.0.0-beta-2503221208
+13 -5
View File
@@ -21,11 +21,21 @@ func (dExt *XTDriver) StartToGoal(text string, opts ...option.ActionOption) erro
for { for {
attempt++ attempt++
log.Info().Int("attempt", attempt).Msg("planning attempt") log.Info().Int("attempt", attempt).Msg("planning attempt")
if err := dExt.AIAction(text, opts...); err != nil {
return err
}
if options.MaxRetryTimes > 1 && attempt >= options.MaxRetryTimes {
return errors.New("reached max retry times")
}
}
}
func (dExt *XTDriver) AIAction(text string, opts ...option.ActionOption) error {
// plan next action // plan next action
result, err := dExt.PlanNextAction(text, opts...) result, err := dExt.PlanNextAction(text, opts...)
if err != nil { if err != nil {
return errors.Wrap(err, "failed to get next action from planner") return err
} }
// do actions // do actions
@@ -37,14 +47,12 @@ func (dExt *XTDriver) StartToGoal(text string, opts ...option.ActionOption) erro
return err return err
} }
case ai.ActionTypeFinished: case ai.ActionTypeFinished:
log.Info().Msg("ai action done")
return nil return nil
} }
} }
if options.MaxRetryTimes > 1 && attempt >= options.MaxRetryTimes { return nil
return errors.New("reached max retry times")
}
}
} }
func (dExt *XTDriver) PlanNextAction(text string, opts ...option.ActionOption) (*ai.PlanningResult, error) { func (dExt *XTDriver) PlanNextAction(text string, opts ...option.ActionOption) (*ai.PlanningResult, error) {
-16
View File
@@ -8,22 +8,6 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
func (dExt *XTDriver) TapByLLM(text string, opts ...option.ActionOption) error {
text = "[click] " + text
result, err := dExt.PlanNextAction(text, opts...)
if err != nil {
return err
}
action := result.NextActions[0]
if action.ActionType != ai.ActionTypeClick {
return fmt.Errorf("expected click action, got: %s", action.ActionType)
}
point := action.ActionInputs["startBox"].([]float64)
return dExt.TapAbsXY(point[0], point[1], opts...)
}
func (dExt *XTDriver) TapByOCR(text string, opts ...option.ActionOption) error { func (dExt *XTDriver) TapByOCR(text string, opts ...option.ActionOption) error {
actionOptions := option.NewActionOptions(opts...) actionOptions := option.NewActionOptions(opts...)
if actionOptions.ScreenShotFileName == "" { if actionOptions.ScreenShotFileName == "" {
+1 -1
View File
@@ -125,7 +125,7 @@ func TestDriverExt_TapByOCR(t *testing.T) {
func TestDriverExt_TapByLLM(t *testing.T) { func TestDriverExt_TapByLLM(t *testing.T) {
driver := setupDriverExt(t) driver := setupDriverExt(t)
err := driver.TapByLLM("点击第一个帖子的作者头像") err := driver.AIAction("点击第一个帖子的作者头像")
assert.Nil(t, err) assert.Nil(t, err)
} }