Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: better way to ensure page has been loaded #106

Merged
merged 1 commit into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/helpers/rpc/domActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { scrollScriptString } from "./runtimeFunctionStrings";
import { sleep, waitFor, waitTillStable } from "../utils";

const DEFAULT_INTERVAL = 500;
const DEFAULT_TIMEOUT = 0;
const DEFAULT_TIMEOUT = 10000; // 10 seconds

export class DomActions {
static delayBetweenClicks = 500; // Set this value to control the delay between clicks
Expand Down
10 changes: 10 additions & 0 deletions src/helpers/rpc/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { DomActions } from "./domActions";

export async function waitTillHTMLRendered(
tabId: number,
interval = undefined,
timeout = undefined,
) {
const domActions = new DomActions(tabId);
return await domActions.waitTillHTMLRendered(interval, timeout);
}
26 changes: 18 additions & 8 deletions src/state/currentTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import {
} from "../helpers/vision-agent/parseResponse";
import { callRPCWithTab } from "../helpers/rpc/pageRPC";
import { getSimplifiedDom } from "../helpers/simplifyDom";
import { sleep, truthyFilter } from "../helpers/utils";
import { sleep, truthyFilter, waitFor } from "../helpers/utils";
import {
operateTool,
operateToolWithSimpliedDom,
} from "../helpers/rpc/performAction";
import { waitTillHTMLRendered } from "../helpers/rpc/utils";
import { findActiveTab } from "../helpers/browserUtils";
import { MyStateCreator } from "./store";
import buildAnnotatedScreenshots from "../helpers/buildAnnotatedScreenshots";
Expand Down Expand Up @@ -103,12 +104,24 @@ export const createCurrentTaskSlice: MyStateCreator<CurrentTaskSlice> = (
while (true) {
if (wasStopped()) break;

// get latest tab info, since button clicking might have changed it
const activeTab = await findActiveTab();
// always get latest tab info, since actions such as button clicking might have changed it
let activeTab = await findActiveTab();
const tabId = activeTab?.id || -1;
if (!activeTab || !tabId) {
throw new Error("No active tab found");
}
if (activeTab.status === "loading") {
// wait for tab to be loaded
await waitFor(
async () => {
// findActiveTab give a new reference to activeTab every time
activeTab = await findActiveTab();
return activeTab?.status === "complete";
},
200, // check every 200ms
100, // wait for up to 20 seconds (100*200ms)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just a thought. 20s itself seems to be a long time, maybe better to couple with some UI indication of waiting such as showing "still waiting for the page to load" for every 5 second. This is not necessary though, because it's rare that some pages take ~20s to load.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! added issue #109

);
}

const isVisionModel = hasVisionSupport(get().settings.selectedModel);

Expand Down Expand Up @@ -168,14 +181,13 @@ export const createCurrentTaskSlice: MyStateCreator<CurrentTaskSlice> = (
if (shouldContinue) {
// if navigation was successful, continue the task on the new page
setActionStatus("waiting");
// sleep 2 seconds. This is pretty arbitrary; we should figure out a better way to determine when the page has settled.
await sleep(2000);
continue;
} else {
break;
}
}
await attachDebugger(tabId);
await waitTillHTMLRendered(tabId);

set((state) => {
state.currentTask.tabId = tabId;
Expand Down Expand Up @@ -243,9 +255,7 @@ export const createCurrentTaskSlice: MyStateCreator<CurrentTaskSlice> = (
}

setActionStatus("waiting");
// sleep 2 seconds. This is pretty arbitrary; we should figure out a better way to determine when the page has settled.
await sleep(2000);
}
} // end of while loop
set((state) => {
state.currentTask.status = "success";
});
Expand Down
Loading