feat!: Refine lifecycle hooks; fix abortSignal

- add `stop` method. agent can be reused after stopped
- agent can not be reused after disposed
- extension DO NOT exposes `dispose` anymore. only `stop`.
- update panel for new `stop` method
- fix MultiPageAgent dispose event
- better handling abortSignal
This commit is contained in:
Simon
2026-02-13 17:57:12 +08:00
parent 4dc332a32c
commit dffcb53db9
13 changed files with 80 additions and 68 deletions

View File

@@ -89,8 +89,7 @@ export class MultiPageAgent extends PageAgentCore {
isAgentRunning: false,
})
// no need to dispose tabsController and pageController
// as they do not keep references
tabsController.dispose()
},
})
}

View File

@@ -84,7 +84,7 @@ export function useAgent(): UseAgentResult {
}, [])
const stop = useCallback(() => {
agentRef.current?.dispose()
agentRef.current?.stop()
}, [])
const configure = useCallback(async (newConfig: LLMConfig) => {

View File

@@ -71,7 +71,8 @@ async function exposeAgentToPage() {
try {
const { task, config } = payload
// create when used
// Dispose old instance before creating new one
multiPageAgent?.dispose()
multiPageAgent = new MultiPageAgent(config)
@@ -116,17 +117,6 @@ async function exposeAgentToPage() {
)
})
multiPageAgent.addEventListener('dispose', () => {
window.postMessage(
{
channel: 'PAGE_AGENT_EXT_RESPONSE',
id,
action: 'dispose_event',
},
'*'
)
})
// result
const result = await multiPageAgent.execute(task)
@@ -155,9 +145,8 @@ async function exposeAgentToPage() {
break
}
case 'dispose': {
// @note stop ongoing processes but can still be re-used later
multiPageAgent?.dispose()
case 'stop': {
multiPageAgent?.stop()
break
}

View File

@@ -16,7 +16,6 @@ export interface ExecuteConfig {
onStatusChange?: (status: AgentStatus) => void
onActivity?: (activity: AgentActivity) => void
onHistoryUpdate?: (history: HistoricalEvent[]) => void
onDispose?: () => void
}
export default defineUnlistedScript(() => {
@@ -60,12 +59,6 @@ export default defineUnlistedScript(() => {
return
}
if (data.action === 'dispose_event' && config.onDispose) {
config.onDispose()
window.removeEventListener('message', handleMessage)
return
}
if (data.action !== 'execute_result') return
// execute_result
@@ -104,14 +97,14 @@ export default defineUnlistedScript(() => {
return promise
}
const dispose = () => {
const stop = () => {
const id = getId()
window.postMessage(
{
channel: 'PAGE_AGENT_EXT_REQUEST',
id,
action: 'dispose',
action: 'stop',
},
'*'
)
@@ -121,6 +114,6 @@ export default defineUnlistedScript(() => {
;(window as any).PAGE_AGENT_EXT = {
version: __EXT_VERSION__,
execute,
dispose,
stop,
}
})