Files
page-agent/packages/extension/src/agent/useAgent.ts
T

106 lines
2.8 KiB
TypeScript
Raw Normal View History

2026-01-24 19:29:27 +08:00
/**
* React hook for using AgentController
*/
import type { AgentActivity, AgentStatus, HistoricalEvent } from '@page-agent/core'
2026-01-28 13:15:34 +08:00
import type { LLMConfig } from '@page-agent/llms'
2026-01-24 19:29:27 +08:00
import { useCallback, useEffect, useRef, useState } from 'react'
import { MultiPageAgent } from './MultiPageAgent'
import { DEMO_CONFIG } from './constants'
2026-01-24 19:29:27 +08:00
export interface UseAgentResult {
status: AgentStatus
history: HistoricalEvent[]
activity: AgentActivity | null
currentTask: string
config: LLMConfig | null
2026-01-24 19:29:27 +08:00
execute: (task: string) => Promise<void>
stop: () => void
configure: (config: LLMConfig) => Promise<void>
}
export function useAgent(): UseAgentResult {
const agentRef = useRef<MultiPageAgent | null>(null)
2026-01-24 19:29:27 +08:00
const [status, setStatus] = useState<AgentStatus>('idle')
const [history, setHistory] = useState<HistoricalEvent[]>([])
const [activity, setActivity] = useState<AgentActivity | null>(null)
const [currentTask, setCurrentTask] = useState('')
const [config, setConfig] = useState<LLMConfig | null>(null)
2026-01-24 19:29:27 +08:00
useEffect(() => {
chrome.storage.local.get('llmConfig').then((result) => {
if (result.llmConfig) {
setConfig(result.llmConfig as LLMConfig)
} else {
chrome.storage.local.set({ llmConfig: DEMO_CONFIG })
setConfig(DEMO_CONFIG)
}
2026-01-24 19:29:27 +08:00
})
}, [])
useEffect(() => {
if (!config) return
const agent = new MultiPageAgent(config)
agentRef.current = agent
2026-01-24 19:29:27 +08:00
const handleStatusChange = (e: Event) => {
const newStatus = agent.status as AgentStatus
2026-01-24 19:29:27 +08:00
setStatus(newStatus)
if (newStatus === 'idle' || newStatus === 'completed' || newStatus === 'error') {
setActivity(null)
}
}
const handleHistoryChange = (e: Event) => {
setHistory([...agent.history])
2026-01-24 19:29:27 +08:00
}
const handleActivity = (e: Event) => {
const newActivity = (e as CustomEvent).detail as AgentActivity
setActivity(newActivity)
}
agent.addEventListener('statuschange', handleStatusChange)
agent.addEventListener('historychange', handleHistoryChange)
agent.addEventListener('activity', handleActivity)
2026-01-24 19:29:27 +08:00
return () => {
agent.removeEventListener('statuschange', handleStatusChange)
agent.removeEventListener('historychange', handleHistoryChange)
agent.removeEventListener('activity', handleActivity)
agent.dispose()
2026-01-24 19:29:27 +08:00
}
}, [config])
2026-01-24 19:29:27 +08:00
const execute = useCallback(async (task: string) => {
const agent = agentRef.current
2026-02-11 19:27:14 +08:00
console.log('🚀 [useAgent] start executing task:', task)
2026-01-28 14:11:44 +08:00
if (!agent) throw new Error('Agent not initialized')
2026-01-24 19:29:27 +08:00
setCurrentTask(task)
setHistory([])
await agent.execute(task)
2026-01-24 19:29:27 +08:00
}, [])
const stop = useCallback(() => {
agentRef.current?.dispose()
2026-01-24 19:29:27 +08:00
}, [])
const configure = useCallback(async (newConfig: LLMConfig) => {
2026-02-02 18:48:16 +08:00
await chrome.storage.local.set({ llmConfig: newConfig })
2026-01-24 19:29:27 +08:00
setConfig(newConfig)
}, [])
return {
status,
history,
activity,
currentTask,
config,
execute,
stop,
configure,
}
}