feat: add hooks for step lifecycle
This commit is contained in:
@@ -17,6 +17,7 @@ The development progress and future plans for PageAgent.
|
|||||||
- [x] **Free evaluation plan?**
|
- [x] **Free evaluation plan?**
|
||||||
- [x] **Custom actions and HITL**
|
- [x] **Custom actions and HITL**
|
||||||
- [ ] **Hooks and Events**
|
- [ ] **Hooks and Events**
|
||||||
|
- [x] **Step lifecycle**
|
||||||
- [ ] **Hijacking `page_open` event**
|
- [ ] **Hijacking `page_open` event**
|
||||||
- [ ] **Custom knowledge base and instructions**
|
- [ ] **Custom knowledge base and instructions**
|
||||||
- [ ] **Black/white-list safeguard**
|
- [ ] **Black/white-list safeguard**
|
||||||
|
|||||||
@@ -156,6 +156,8 @@ export class PageAgent extends EventTarget {
|
|||||||
let step = 0
|
let step = 0
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
|
if (this.config.onBeforeStep) await this.config.onBeforeStep.call(this, step)
|
||||||
|
|
||||||
console.group(`step: ${step + 1}`)
|
console.group(`step: ${step + 1}`)
|
||||||
|
|
||||||
// abort
|
// abort
|
||||||
@@ -209,6 +211,8 @@ export class PageAgent extends EventTarget {
|
|||||||
console.log(chalk.green('Step finished:'), actionName)
|
console.log(chalk.green('Step finished:'), actionName)
|
||||||
console.groupEnd()
|
console.groupEnd()
|
||||||
|
|
||||||
|
if (this.config.onAfterStep) await this.config.onAfterStep.call(this, step, this.history)
|
||||||
|
|
||||||
step++
|
step++
|
||||||
if (step > MAX_STEPS) {
|
if (step > MAX_STEPS) {
|
||||||
this.#onDone('Step count exceeded maximum limit', false)
|
this.#onDone('Step count exceeded maximum limit', false)
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import type { AgentHistory, PageAgent } from '@/PageAgent'
|
||||||
import type { DomConfig } from '@/dom'
|
import type { DomConfig } from '@/dom'
|
||||||
import type { SupportedLanguage } from '@/i18n'
|
import type { SupportedLanguage } from '@/i18n'
|
||||||
import type { PageAgentTool } from '@/tools'
|
import type { PageAgentTool } from '@/tools'
|
||||||
@@ -47,6 +48,11 @@ export interface UIConfig {
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
customTools?: Record<string, PageAgentTool | null>
|
customTools?: Record<string, PageAgentTool | null>
|
||||||
|
|
||||||
|
// hooks
|
||||||
|
|
||||||
|
onBeforeStep?: (this: PageAgent, stepCnt: number) => Promise<void> | void
|
||||||
|
onAfterStep?: (this: PageAgent, stepCnt: number, history: AgentHistory[]) => Promise<void> | void
|
||||||
}
|
}
|
||||||
|
|
||||||
export type PageAgentConfig = LLMConfig & DomConfig & UIConfig
|
export type PageAgentConfig = LLMConfig & DomConfig & UIConfig
|
||||||
|
|||||||
Reference in New Issue
Block a user