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:
@@ -97,7 +97,7 @@ Parameters:
|
||||
|
||||
Returns: `Promise<ExecutionResult>`
|
||||
|
||||
#### `PAGE_AGENT_EXT.dispose()`
|
||||
#### `PAGE_AGENT_EXT.stop()`
|
||||
|
||||
Stop the current task.
|
||||
|
||||
@@ -124,7 +124,6 @@ export interface ExecuteConfig {
|
||||
onStatusChange?: (status: AgentStatus) => void
|
||||
onActivity?: (activity: AgentActivity) => void
|
||||
onHistoryUpdate?: (history: HistoricalEvent[]) => void
|
||||
onDispose?: () => void
|
||||
}
|
||||
|
||||
export type Execute = (task: string, config: ExecuteConfig) => Promise<ExecutionResult>
|
||||
@@ -189,7 +188,7 @@ const result = await window.PAGE_AGENT_EXT!.execute(
|
||||
### Stop the Current Task
|
||||
|
||||
```typescript
|
||||
window.PAGE_AGENT_EXT!.dispose()
|
||||
window.PAGE_AGENT_EXT!.stop()
|
||||
```
|
||||
|
||||
## Window Type Declaration
|
||||
@@ -212,7 +211,6 @@ interface ExecuteConfig {
|
||||
onStatusChange?: (status: AgentStatus) => void
|
||||
onActivity?: (activity: AgentActivity) => void
|
||||
onHistoryUpdate?: (history: HistoricalEvent[]) => void
|
||||
onDispose?: () => void
|
||||
}
|
||||
|
||||
declare global {
|
||||
@@ -221,7 +219,7 @@ declare global {
|
||||
PAGE_AGENT_EXT?: {
|
||||
version: string
|
||||
execute: Execute
|
||||
dispose: () => void
|
||||
stop: () => void
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ token 匹配后,插件会在 `window` 上注入 API。
|
||||
|
||||
返回:`Promise<ExecutionResult>`
|
||||
|
||||
#### `PAGE_AGENT_EXT.dispose()`
|
||||
#### `PAGE_AGENT_EXT.stop()`
|
||||
|
||||
停止当前任务。
|
||||
|
||||
@@ -124,7 +124,6 @@ export interface ExecuteConfig {
|
||||
onStatusChange?: (status: AgentStatus) => void
|
||||
onActivity?: (activity: AgentActivity) => void
|
||||
onHistoryUpdate?: (history: HistoricalEvent[]) => void
|
||||
onDispose?: () => void
|
||||
}
|
||||
|
||||
export type Execute = (task: string, config: ExecuteConfig) => Promise<ExecutionResult>
|
||||
@@ -189,7 +188,7 @@ const result = await window.PAGE_AGENT_EXT!.execute(
|
||||
### 停止当前任务
|
||||
|
||||
```typescript
|
||||
window.PAGE_AGENT_EXT!.dispose()
|
||||
window.PAGE_AGENT_EXT!.stop()
|
||||
```
|
||||
|
||||
## Window 类型声明
|
||||
@@ -212,7 +211,6 @@ interface ExecuteConfig {
|
||||
onStatusChange?: (status: AgentStatus) => void
|
||||
onActivity?: (activity: AgentActivity) => void
|
||||
onHistoryUpdate?: (history: HistoricalEvent[]) => void
|
||||
onDispose?: () => void
|
||||
}
|
||||
|
||||
declare global {
|
||||
@@ -221,7 +219,7 @@ declare global {
|
||||
PAGE_AGENT_EXT?: {
|
||||
version: string
|
||||
execute: Execute
|
||||
dispose: () => void
|
||||
stop: () => void
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -84,7 +84,7 @@ export function useAgent(): UseAgentResult {
|
||||
}, [])
|
||||
|
||||
const stop = useCallback(() => {
|
||||
agentRef.current?.dispose()
|
||||
agentRef.current?.stop()
|
||||
}, [])
|
||||
|
||||
const configure = useCallback(async (newConfig: LLMConfig) => {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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,
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user