-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcheck_syntax.js
More file actions
484 lines (421 loc) · 19 KB
/
check_syntax.js
File metadata and controls
484 lines (421 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
import { state, startRunState, finishRunState, appendLog, setSessionTitle } from './state.js';
import { getEls, setRunning, appendBlock, streamText, showAnswer, collapseLogs, setTask, setLiveTask, removeLiveTask, logCompletedTask, updatePlanItem, toContinuous } from './ui.js';
import { generatePlan, callGemini } from './planner.js';
import { execTool, normalizeCall, sendToAgent, ensureActiveAndFocused, waitReady } from './tools.js';
import { log, hostFromUrl, extractTargetDomain } from './utils.js';
import { Logger } from './logger.js';
function callsEqual(a, b) {
try { return JSON.stringify(a) === JSON.stringify(b); } catch { return false; }
}
function normalizeArgsForHistory(args) {
const a = args || {};
const r = {};
if (a.id !== undefined) r.id = a.id;
if (a.selector) r.selector = a.selector;
if (a.xpath) r.xpath = a.xpath;
if (a.text) r.text = String(a.text).slice(0, 60);
if (a.key) r.key = a.key;
if (a.url) r.url = a.url;
if (a.to) r.to = a.to;
if (a.value !== undefined && typeof a.value !== "object") r.value = String(a.value).slice(0, 120);
if (a.href) r.href = a.href;
if (a.signature && a.signature.label) r.label = String(a.signature.label).slice(0, 80);
return r;
}
function normalizeResultForHistory(result) {
if (!result) return undefined;
const r = {};
// if (result.text) r.text = String(result.text).slice(0, 2000); // Capture up to 2000 chars of text content
if (result.newTabId) r.newTabId = result.newTabId;
if (result.url) r.url = result.url;
if (result.error) r.error = result.error;
if (result.searchUrl) r.searchUrl = result.searchUrl;
return r;
}
function recordAction(action, result, preState, postState) {
const h = {
t: action.tool || action.action,
a: normalizeArgsForHistory(action.args || {}),
ok: !!(result && result.ok),
r: normalizeResultForHistory(result),
u: postState && postState.state && postState.state.url ? postState.state.url : (preState && preState.state && preState.state.url ? preState.state.url : ""),
ts: Date.now(),
v: postState && postState.state && postState.state.lastInteraction ? postState.state.lastInteraction : null
};
state.currentRunActions.push(h);
if (state.currentRunActions.length > 50) state.currentRunActions = state.currentRunActions.slice(-50);
if (!h.ok) {
try { chrome.runtime.sendMessage({ t: "TASK_RECORD_ERROR", action: action, error: result && result.error ? result.error : "action_failed" }); } catch { }
}
try { chrome.runtime.sendMessage({ t: "TASK_MEMORY_RECORD_ACTION", action, result, preState, postState }); } catch { }
}
function buildHistoryForPrompt(maxItems = 20) {
const rawItems = state.currentRunActions.slice(-maxItems);
// Deep clone to avoid mutating state
const items = JSON.parse(JSON.stringify(rawItems));
// Truncate text in all but the last item to save context window
// The agent only needs the full text immediately after reading it
for (let i = 0; i < items.length - 1; i++) {
if (items[i].r && items[i].r.text && items[i].r.text.length > 50) {
items[i].r.text = `<text_content_hidden_from_history>`;
}
}
return JSON.stringify(items);
}
async function getInjectableTabId() {
const tabs = await chrome.tabs.query({ active: true, currentWindow: true });
let tab = tabs && tabs[0];
if (tab && (/^(https?|file):/i.test(tab.url))) return tab.id;
const all = await chrome.tabs.query({ currentWindow: true });
for (const t of all) {
if (/^(https?|file):/i.test(t.url)) return t.id;
}
return undefined;
}
async function ensureExistingTab(tabId) {
try {
if (tabId && typeof tabId === 'number') {
const t = await chrome.tabs.get(tabId);
if (t && (/^(https?|file):/i.test(t.url))) return tabId;
}
} catch { }
let newId = await getInjectableTabId();
if (!newId) {
try {
const nt = await chrome.tabs.create({ url: "about:blank", active: true });
newId = nt?.id;
} catch { }
}
return newId;
}
async function simplify(tabId, annotate) {
const r = await sendToAgent(tabId, { t: "simplify", annotate });
return (r && Array.isArray(r.elements)) ? r.elements : [];
}
async function mapCompact(tabId) {
const r = await sendToAgent(tabId, { t: "mapCompact" });
return (r && Array.isArray(r.elements)) ? r.elements : [];
}
async function ensurePlanForInstruction(instruction, context = {}) {
const key = state.geminiKey.trim();
const model = state.geminiModel.trim();
let plan = [];
streamText("Preparing to assist you...\n");
if (key && model) {
plan = await generatePlan(instruction, key, model, context).catch(() => []);
}
if (!plan || !plan.length) {
const text = String(instruction || "").toLowerCase();
const domain = extractTargetDomain(text);
const steps = [];
if (domain) {
steps.push({ title: `Navigate to ${domain}` });
steps.push({ title: "Find relevant element(s)" });
steps.push({ title: "Interact and verify result" });
} else {
steps.push({ title: "Open site or perform web search" });
steps.push({ title: "Click a relevant result" });
steps.push({ title: "Verify destination page" });
}
plan = steps.map(st => ({ title: String(st.title), status: "pending" }));
}
if (Array.isArray(plan) && plan.length) {
try { await chrome.runtime.sendMessage({ t: "UPDATE_TASK_CONTEXT", patch: { subtasks: plan } }); } catch { }
streamText("I have created a plan. Let's get started.\n");
// Restore initial plan dump
appendBlock(plan, "plan");
return plan;
}
return [];
}
function pickNextCall(action) {
try {
if (!action) return null;
if (action.call && (action.call.tool || action.call.action)) {
return normalizeCall(action.call);
}
if (action.calls && Array.isArray(action.calls) && action.calls.length) {
const first = action.calls.find(c => (c.tool || c.action) && String(c.tool || c.action).toLowerCase() !== "done");
return first ? normalizeCall(first) : null;
}
if (action.tool || action.action) {
return normalizeCall(action);
}
return null;
} catch {
return null;
}
}
function describeCall(call) {
const t = call.tool;
const a = call.args || {};
if (t === "navigate") return `Navigating to ${hostFromUrl(a.url) || a.url || "page"}`;
if (t === "tap") return `Clicking ${a.text || a.selector || "element"}`;
if (t === "type") return `Typing "${a.text || a.value}"`;
if (t === "search") return `Searching for "${a.query}"`;
if (t === "wait") return `Waiting for ${a.ms ? a.ms + "ms" : "condition"}`;
return `Executing ${t}`;
}
export async function startAutoRun() {
const els = getEls();
state.autoStop = false;
await setRunning(true);
let tabId = await getInjectableTabId();
const key = state.geminiKey.trim();
const model = state.geminiModel.trim();
const instruction = els.instruction.value.trim();
if (!instruction) {
await setRunning(false);
return;
}
setTask(instruction, true);
setSessionTitle(instruction);
appendBlock(instruction, "user");
els.instruction.value = "";
els.instruction.style.height = "auto";
if (!key) {
streamText("Please add your Gemini API Key in Settings to continue.");
await setRunning(false);
return;
}
let currentPlan = [];
// Initial context build for planning
let initialTabId = await getInjectableTabId();
let initialContext = {};
if (initialTabId) {
try {
const pageState = await sendToAgent(initialTabId, { t: "getPageState" });
if (pageState && pageState.state) {
initialContext.page = pageState.state;
}
} catch { }
}
currentPlan = await ensurePlanForInstruction(instruction, initialContext);
// Initialize Live Task immediately
if (currentPlan.length > 0) {
setLiveTask(toContinuous(currentPlan[0].title));
}
let step = 0;
if (tabId) {
try {
const t = await chrome.tabs.get(tabId);
if (t && (/^(https?|file):/i.test(t.url))) {
await ensureActiveAndFocused(tabId);
await sendToAgent(tabId, { t: "setMode", experimental: state.experimentalMode });
await sendToAgent(tabId, { t: "working", on: true });
await waitReady(tabId);
}
try { chrome.runtime.sendMessage({ t: "TASK_ADD_RELEVANT_TAB", tabId }); } catch { }
} catch { }
}
startRunState("auto", instruction);
const recentActions = [];
while (!state.autoStop && step < 50) {
if (!tabId) {
tabId = await getInjectableTabId();
if (tabId) {
await sendToAgent(tabId, { t: "setMode", experimental: state.experimentalMode });
await sendToAgent(tabId, { t: "working", on: true });
}
}
let elements = [];
if (tabId) {
try {
elements = state.experimentalMode ? await mapCompact(tabId) : await simplify(tabId, state.showIDs);
// Fallback: If mapCompact returns 0 elements (likely due to message size limit), try simplify
if (state.experimentalMode && elements.length === 0) {
elements = await simplify(tabId, state.showIDs);
}
} catch (e) {
try {
await chrome.scripting.executeScript({ target: { tabId, allFrames: true }, files: ["content.js"] });
await new Promise(resolve => setTimeout(resolve, 500));
elements = state.experimentalMode ? await mapCompact(tabId) : await simplify(tabId, state.showIDs);
if (state.experimentalMode && elements.length === 0) {
elements = await simplify(tabId, state.showIDs);
}
} catch { }
}
}
// Update plan status from context
try {
const r = await chrome.runtime.sendMessage({ t: "GET_TASK_CONTEXT" });
const subtasks = r?.taskContext?.subtasks || [];
if (subtasks.length) currentPlan = subtasks;
} catch { }
if (state.autoStop) break;
const response = await callGemini(elements, instruction, key, model, tabId, buildHistoryForPrompt());
if (state.autoStop) break;
if (!response) {
streamText("Planner failed to respond. Retrying...\n");
await new Promise(res => setTimeout(res, 2000));
continue;
}
const thought = response?.thought || "";
const action = response?.call || response; // Fallback if no thought structure
const subtaskUpdates = response?.subtask_updates || [];
if (thought) {
streamText(thought + "\n");
}
// Apply LLM-driven plan updates logic MOVED to after tool execution
// to implement Zero Point Failure logic.
// Update Live Task UI
const activeTaskIdx = currentPlan.findIndex(t => t.status === "active" || t.status === "pending");
if (activeTaskIdx >= 0) {
setLiveTask(toContinuous(currentPlan[activeTaskIdx].title));
updatePlanItem(activeTaskIdx, "active");
} else if (currentPlan.length > 0 && currentPlan.every(t => t.status === "completed")) {
setLiveTask("Finishing up...");
} else {
setLiveTask("Thinking...");
}
const nc = pickNextCall(action);
// Loop detection
if (nc) {
const actionKey = JSON.stringify(nc);
recentActions.push(actionKey);
if (recentActions.length > 5) recentActions.shift();
// Check if we've done this exact action 3 times in the last 5 steps
const repeats = recentActions.filter(k => k === actionKey).length;
if (repeats >= 3) {
streamText("I seem to be stuck in a loop. I will pause to re-evaluate.\n");
// Force a wait or a different strategy? For now, just break to avoid infinite costs
// But better: try to scroll or wait
if (nc.tool !== "wait" && nc.tool !== "scroll") {
streamText("Trying to scroll to break the loop...\n");
await execTool(tabId, { tool: "scroll", args: { amount: 300 } });
continue;
}
}
}
tabId = await ensureExistingTab(tabId);
const preState = tabId ? await sendToAgent(tabId, { t: "getPageState" }) : null;
if (!nc) break;
if (nc.tool === "done") {
if (!thought) streamText("I have completed the task.\n");
// Mark all as done and log them
currentPlan.forEach((t, idx) => {
if (t.status !== "completed") {
t.status = "completed";
logCompletedTask(t.title);
updatePlanItem(idx, "completed");
}
});
// Removed final plan dump
// appendBlock(currentPlan, "plan");
// Show answer if provided
if (nc.args && nc.args.text) {
showAnswer(nc.args.text);
}
// Collapse logs on completion
collapseLogs();
removeLiveTask(); // Clean up live task
break;
}
// Dedup logic
const now = Date.now();
const isWait = nc.tool === "wait";
const isScroll = nc.tool === "scroll";
// Allow faster repeats for wait/scroll, but enforce longer delay for others if successful
const dedupTime = (isWait || isScroll) ? 1000 : 5000;
if (state.lastCall && callsEqual(state.lastCall, nc)) {
// If last call failed, we might want to retry immediately, so don't block unless it's very fast spam
if (state.lastCallResult && !state.lastCallResult.ok) {
if (now - state.lastCallAt < 500) continue;
} else {
// If last call succeeded, enforce the dedup time to prevent accidental double-clicks due to slow state updates
if (now - state.lastCallAt < dedupTime) {
continue;
}
}
}
// Narrative output - only if no thought was provided
if (!thought) {
const desc = describeCall(nc);
streamText(`I will now ${desc.toLowerCase()}.\n`);
}
// Show action block - REMOVED for cleaner UI as per user request
// const desc = describeCall(nc);
// appendBlock(`> ${desc}`, "action");
if (state.autoStop) break;
await ensureActiveAndFocused(tabId);
// ZERO POINT FAILURE: Inject element details for robust retry
if ((nc.tool === "tap" || nc.tool === "type") && nc.args && nc.args.id && !nc.args.element && !nc.args.signature) {
const targetId = Number(nc.args.id);
const foundEl = elements.find(e => e.i === targetId || e.id === targetId);
if (foundEl) {
// Reconstruct the element object expected by tools.js
nc.args.element = {
i: foundEl.i || foundEl.id,
t: foundEl.t || foundEl.tag || foundEl.type,
x: foundEl.x || foundEl.text,
r: foundEl.r || foundEl.role,
l: foundEl.l || foundEl.label,
h: foundEl.h || foundEl.href,
p: foundEl.p || foundEl.placeholder
};
Logger.info({ type: "injected_element_details", id: targetId, element: nc.args.element });
}
}
const r = await execTool(tabId, nc);
if (r && r.ok) {
// Update plan status based on progress
const pendingIdx = currentPlan.findIndex(t => t.status === "pending");
if (pendingIdx >= 0) {
// We trust the tool execution success now that verification is improved
if (nc.tool !== "wait" && nc.tool !== "scroll") {
// Only mark as active, NEVER auto-complete the previous task here.
// We rely strictly on the planner's subtask_updates to mark tasks as completed.
if (currentPlan[pendingIdx].status === "pending") {
currentPlan[pendingIdx].status = "active";
updatePlanItem(pendingIdx, "active");
// Persist state immediately
try { await chrome.runtime.sendMessage({ t: "UPDATE_TASK_CONTEXT", patch: { subtasks: currentPlan } }); } catch { }
}
}
}
// Apply LLM-driven plan updates ONLY if tool execution succeeded (Zero Point Failure)
if (Array.isArray(subtaskUpdates) && subtaskUpdates.length > 0) {
let planChanged = false;
for (const update of subtaskUpdates) {
if (update.index !== undefined && update.index >= 0 && update.index < currentPlan.length) {
if (currentPlan[update.index].status !== update.status) {
currentPlan[update.index].status = update.status;
planChanged = true;
// Trigger UI updates immediately
if (update.status === "completed") {
logCompletedTask(currentPlan[update.index].title);
updatePlanItem(update.index, "completed");
} else if (update.status === "active") {
updatePlanItem(update.index, "active");
}
}
}
}
if (planChanged) {
try { await chrome.runtime.sendMessage({ t: "UPDATE_TASK_CONTEXT", patch: { subtasks: currentPlan } }); } catch { }
}
}
} else {
streamText(`I encountered an issue: ${r?.error || "Unknown error"}. Retrying...\n`);
}
if (r && (r.newTabId || r.activeTabId || r.newActiveTabId)) {
tabId = r.newTabId || r.activeTabId || r.newActiveTabId;
await ensureActiveAndFocused(tabId);
}
await ensureActiveAndFocused(tabId);
let postState = null;
try {
postState = tabId ? await sendToAgent(tabId, { t: "getPageState" }) : null;
} catch { }
recordAction(nc, r, preState, postState);
state.lastCall = nc;
state.lastCallAt = now;
state.lastCallResult = r;
await new Promise(res => setTimeout(res, 1000));
step++;
}
if (tabId) await sendToAgent(tabId, { t: "working", on: false });
await finishRunState();
await setRunning(false);
}