Skip to content

Commit 778f4ec

Browse files
shadowfax92felarof99github-actions[bot]
authored
PocAgent + refactor (#77)
* Screenshot tool fixed * ReactAgent loop ReactAgent loop v0.2 * Trim to max tokens implemented correctly trim max tokens * JSON Parse fix Fixed json.parse * Minor fix -- add system message always at position 0 * minor fix * Added support for passing screenshot size to captureScreenshot backup * Make react agent use screenshot tool * Refactor backend and execution (#75) * wip: new exection class and manager * wip: new pubsub channels * wip: new background handlers * new execution logic * removed execution status * handle workflow status for processing in sidepanel * mcp server fix * sending pause message * better portName parsing, sidepanel sends tabId, storing tabId too * 49.0.0.26 release * docs: OmkarBansod02 signed the CLA in browseros-ai/BrowserOS-agent#$pullRequestNo * Refactor backend and execution (#75) * wip: new exection class and manager * wip: new pubsub channels * wip: new background handlers * new execution logic * removed execution status * handle workflow status for processing in sidepanel * mcp server fix * sending pause message * better portName parsing, sidepanel sends tabId, storing tabId too * Moved react agent into POCAgent * Revert changes of ReacStrategy from BrowserAgent * Minor fix * fix: execution class abort issue * clean-up: removed un-used MessageTypes * clean-up: execution-manager simplified * better abort handling --------- Co-authored-by: Felarof <nithin.sonti@gmail.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 5c18a16 commit 778f4ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+4378
-1981
lines changed

manifest.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"manifest_version": 3,
33
"name": "Agent",
4-
"version": "49.0.0.26",
4+
"version": "49.0.0.27",
55
"description": "Agent",
66
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs1zULZz5eE0U8SEjr/R++dlx6WKFj7GbpnBiE1n17gaylMWDlw6uuBJNjcRrSGwOt53Z3PKf2T3g5DtNES8q6rQc11P/y8J8GKhKuqGrtRJyk5iXzcKJk4CHz6leFSMt8CsZY0r0b7wCZ5QuhomTHGQpNWNS0c13xfVqWt4dncfIRj7fMzfTkicq7Mqqx+JcdprLkiVfETvdkMwwEWmSNwQ6nCDzLtTbyyMiGUEBSJs+WlP1fO7LIX0sHesFVxfPhCZ2K4F1biwenbRL+YYD60ogpVppop2ee/W3D211IN1zYxgnhycFv3m8TrzG+MD/IZgcu13u0bHRn3V7IGW1iwIDAQAB",
77
"permissions": [
@@ -51,4 +51,4 @@
5151
"48": "assets/icon48.png",
5252
"128": "assets/icon128.png"
5353
}
54-
}
54+
}
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
import { MessageType, ExecuteQueryMessage, CancelTaskMessage, ResetConversationMessage } from '@/lib/types/messaging'
2+
import { PortMessage } from '@/lib/runtime/PortMessaging'
3+
import { ExecutionManager } from '@/lib/execution/ExecutionManager'
4+
import { Logging } from '@/lib/utils/Logging'
5+
import { PubSub } from '@/lib/pubsub'
6+
7+
/**
8+
* Handles execution-related messages:
9+
* - EXECUTE_QUERY: Start a new query execution
10+
* - CANCEL_TASK: Cancel running execution
11+
* - RESET_CONVERSATION: Reset execution state
12+
*/
13+
export class ExecutionHandler {
14+
private executionManager: ExecutionManager
15+
16+
constructor() {
17+
this.executionManager = ExecutionManager.getInstance()
18+
}
19+
20+
/**
21+
* Handle EXECUTE_QUERY message
22+
*/
23+
async handleExecuteQuery(
24+
message: PortMessage,
25+
port: chrome.runtime.Port,
26+
executionId?: string
27+
): Promise<void> {
28+
const payload = message.payload as ExecuteQueryMessage['payload']
29+
const { query, tabIds, source, chatMode, metadata } = payload
30+
31+
// Use executionId from port or generate default
32+
const execId = executionId || 'default'
33+
34+
Logging.log('ExecutionHandler',
35+
`Starting execution ${execId}: "${query}" (mode: ${chatMode ? 'chat' : 'browse'})`)
36+
37+
// Log metrics
38+
Logging.logMetric('query_initiated', {
39+
query,
40+
source: source || metadata?.source || 'unknown',
41+
mode: chatMode ? 'chat' : 'browse',
42+
executionMode: metadata?.executionMode || 'dynamic',
43+
})
44+
45+
try {
46+
// Get or create execution
47+
let execution = this.executionManager.get(execId)
48+
49+
if (!execution) {
50+
// Create new execution
51+
execution = this.executionManager.create(execId, {
52+
mode: chatMode ? 'chat' : 'browse',
53+
tabIds,
54+
metadata,
55+
debug: false
56+
})
57+
} else {
58+
// If execution exists and is running, cancel it
59+
// No need to wait - the new run() will handle it
60+
if (execution.isRunning()) {
61+
Logging.log('ExecutionHandler', `Cancelling previous task for execution ${execId}`)
62+
execution.cancel()
63+
}
64+
}
65+
66+
// Run the query
67+
await execution.run(query, metadata)
68+
69+
// Send success response
70+
port.postMessage({
71+
type: MessageType.WORKFLOW_STATUS,
72+
payload: {
73+
status: 'success',
74+
executionId: execId
75+
},
76+
id: message.id
77+
})
78+
79+
} catch (error) {
80+
const errorMessage = error instanceof Error ? error.message : String(error)
81+
Logging.log('ExecutionHandler', `Error executing query: ${errorMessage}`, 'error')
82+
83+
// Send error response
84+
port.postMessage({
85+
type: MessageType.WORKFLOW_STATUS,
86+
payload: {
87+
status: 'error',
88+
error: errorMessage,
89+
executionId: execId
90+
},
91+
id: message.id
92+
})
93+
}
94+
}
95+
96+
/**
97+
* Handle CANCEL_TASK message
98+
*/
99+
handleCancelTask(
100+
message: PortMessage,
101+
port: chrome.runtime.Port,
102+
executionId?: string
103+
): void {
104+
const execId = executionId || 'default'
105+
106+
Logging.log('ExecutionHandler', `Cancelling execution ${execId}`)
107+
108+
try {
109+
this.executionManager.cancel(execId)
110+
Logging.logMetric('task_cancelled', { executionId: execId })
111+
112+
// Send success response
113+
port.postMessage({
114+
type: MessageType.WORKFLOW_STATUS,
115+
payload: {
116+
status: 'success',
117+
message: 'Task cancelled',
118+
executionId: execId
119+
},
120+
id: message.id
121+
})
122+
123+
} catch (error) {
124+
const errorMessage = error instanceof Error ? error.message : String(error)
125+
Logging.log('ExecutionHandler', `Error cancelling task: ${errorMessage}`, 'error')
126+
127+
// Send error response
128+
port.postMessage({
129+
type: MessageType.WORKFLOW_STATUS,
130+
payload: {
131+
status: 'error',
132+
error: errorMessage,
133+
executionId: execId
134+
},
135+
id: message.id
136+
})
137+
}
138+
}
139+
140+
/**
141+
* Handle RESET_CONVERSATION message
142+
*/
143+
handleResetConversation(
144+
message: PortMessage,
145+
port: chrome.runtime.Port,
146+
executionId?: string
147+
): void {
148+
const execId = executionId || 'default'
149+
150+
Logging.log('ExecutionHandler', `Resetting execution ${execId}`)
151+
152+
try {
153+
this.executionManager.reset(execId)
154+
Logging.logMetric('conversation_reset', { executionId: execId })
155+
156+
// Send success response
157+
port.postMessage({
158+
type: MessageType.WORKFLOW_STATUS,
159+
payload: {
160+
status: 'success',
161+
message: 'Conversation reset',
162+
executionId: execId
163+
},
164+
id: message.id
165+
})
166+
167+
} catch (error) {
168+
const errorMessage = error instanceof Error ? error.message : String(error)
169+
Logging.log('ExecutionHandler', `Error resetting conversation: ${errorMessage}`, 'error')
170+
171+
// Send error response
172+
port.postMessage({
173+
type: MessageType.WORKFLOW_STATUS,
174+
payload: {
175+
status: 'error',
176+
error: errorMessage,
177+
executionId: execId
178+
},
179+
id: message.id
180+
})
181+
}
182+
}
183+
184+
/**
185+
* Handle HUMAN_INPUT_RESPONSE message
186+
*/
187+
handleHumanInputResponse(
188+
message: PortMessage,
189+
port: chrome.runtime.Port,
190+
executionId?: string
191+
): void {
192+
const execId = executionId || 'default'
193+
const payload = message.payload as any
194+
195+
// Get the execution and forward the response
196+
const execution = this.executionManager.get(execId)
197+
if (execution) {
198+
// Get the execution's PubSub channel
199+
const pubsub = PubSub.getChannel(execId)
200+
pubsub.publishHumanInputResponse(payload)
201+
202+
Logging.log('ExecutionHandler',
203+
`Forwarded human input response for execution ${execId}`)
204+
} else {
205+
Logging.log('ExecutionHandler',
206+
`No execution found for human input response: ${execId}`, 'warning')
207+
}
208+
}
209+
210+
/**
211+
* Get execution statistics
212+
*/
213+
getStats(): any {
214+
return this.executionManager.getStats()
215+
}
216+
}

0 commit comments

Comments
 (0)