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] **Custom actions and HITL**
|
||||
- [ ] **Hooks and Events**
|
||||
- [x] **Step lifecycle**
|
||||
- [ ] **Hijacking `page_open` event**
|
||||
- [ ] **Custom knowledge base and instructions**
|
||||
- [ ] **Black/white-list safeguard**
|
||||
|
||||
@@ -156,6 +156,8 @@ export class PageAgent extends EventTarget {
|
||||
let step = 0
|
||||
|
||||
while (true) {
|
||||
if (this.config.onBeforeStep) await this.config.onBeforeStep.call(this, step)
|
||||
|
||||
console.group(`step: ${step + 1}`)
|
||||
|
||||
// abort
|
||||
@@ -209,6 +211,8 @@ export class PageAgent extends EventTarget {
|
||||
console.log(chalk.green('Step finished:'), actionName)
|
||||
console.groupEnd()
|
||||
|
||||
if (this.config.onAfterStep) await this.config.onAfterStep.call(this, step, this.history)
|
||||
|
||||
step++
|
||||
if (step > MAX_STEPS) {
|
||||
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 { SupportedLanguage } from '@/i18n'
|
||||
import type { PageAgentTool } from '@/tools'
|
||||
@@ -47,6 +48,11 @@ export interface UIConfig {
|
||||
* }
|
||||
*/
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user