Skip to content

Commit 5b7e070

Browse files
committed
Fix lint issues in agent and utils
1 parent caed90c commit 5b7e070

5 files changed

Lines changed: 56 additions & 53 deletions

File tree

‎src/agent/index.ts‎

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ import { setDebugOptions } from "@/debug/options";
4949
import { initializeRuntimeContext } from "./shared/runtime-context";
5050
import { performAction } from "./actions/shared/perform-action";
5151

52+
type ActionDebugMetadata = {
53+
elementMetadata?: {
54+
xpath?: string;
55+
};
56+
};
57+
58+
const hasElementMetadata = (debugData: unknown): debugData is ActionDebugMetadata =>
59+
typeof debugData === "object" &&
60+
debugData !== null &&
61+
"elementMetadata" in debugData;
62+
5263
export class HyperAgent<T extends BrowserProviders = "Local"> {
5364
// aiAction configuration constants
5465
private static readonly AIACTION_CONFIG = {
@@ -202,7 +213,7 @@ export class HyperAgent<T extends BrowserProviders = "Local"> {
202213
* @returns
203214
*/
204215
private getActions(
205-
outputSchema?: z.ZodType<any>
216+
outputSchema?: z.ZodType<unknown>
206217
): Array<AgentActionDefinition> {
207218
if (outputSchema) {
208219
return [
@@ -764,7 +775,7 @@ export class HyperAgent<T extends BrowserProviders = "Local"> {
764775
public async executeSingleAction(
765776
instruction: string,
766777
pageOrGetter: Page | (() => Page),
767-
_params?: TaskParams
778+
params?: TaskParams
768779
): Promise<TaskOutput> {
769780
const actionStart = performance.now();
770781
const startTime = new Date().toISOString();
@@ -869,7 +880,7 @@ export class HyperAgent<T extends BrowserProviders = "Local"> {
869880
}
870881
: undefined,
871882
// These are required by ActionContext but not used by performAction
872-
debugDir: undefined,
883+
debugDir: params?.debugDir,
873884
mcpClient: this.mcpClient,
874885
variables: Object.values(this._variables),
875886
invalidateDomCache: () => markDomSnapshotDirty(initialPage),
@@ -887,9 +898,10 @@ export class HyperAgent<T extends BrowserProviders = "Local"> {
887898
if (
888899
actionOutput.debug &&
889900
typeof actionOutput.debug === "object" &&
890-
"requestedAction" in actionOutput.debug
901+
"requestedAction" in actionOutput.debug &&
902+
hasElementMetadata(actionOutput.debug)
891903
) {
892-
actionXPath = (actionOutput.debug as any).elementMetadata?.xpath;
904+
actionXPath = actionOutput.debug.elementMetadata?.xpath;
893905
}
894906

895907
if (!actionOutput.success) {
@@ -1140,11 +1152,15 @@ export class HyperAgent<T extends BrowserProviders = "Local"> {
11401152
}
11411153

11421154
private setupHyperPage(page: Page): HyperPage {
1143-
const hyperPage = page as HyperPage;
1155+
type ScopedHyperPage = HyperPage & {
1156+
_scopeListenerCleanup?: () => void;
1157+
};
1158+
1159+
const hyperPage = page as ScopedHyperPage;
11441160

11451161
// Clean up existing listener if this page was already setup
1146-
if ((hyperPage as any)._scopeListenerCleanup) {
1147-
(hyperPage as any)._scopeListenerCleanup();
1162+
if (hyperPage._scopeListenerCleanup) {
1163+
hyperPage._scopeListenerCleanup();
11481164
}
11491165

11501166
// History Stack: [Root, Tab1, Tab2, ...]
@@ -1187,27 +1203,32 @@ export class HyperAgent<T extends BrowserProviders = "Local"> {
11871203

11881204
// Attach a persistent listener to track page flow for the lifetime of this wrapper
11891205
page.context().on("page", onPage);
1190-
(hyperPage as any)._scopeListenerCleanup = () => {
1206+
hyperPage._scopeListenerCleanup = () => {
11911207
page.context().off("page", onPage);
11921208
};
11931209

1194-
const executeSingleActionWithRetry = async (
1195-
instruction: string,
1196-
params?: TaskParams
1197-
) => {
1198-
const maxRetries = 3;
1199-
for (let i = 0; i < maxRetries; i++) {
1200-
try {
1201-
return await this.executeSingleAction(
1202-
instruction,
1203-
getActivePage,
1204-
params
1205-
);
1206-
} catch (err: any) {
1207-
if (
1208-
err.statusCode === 409 ||
1209-
(err.message && err.message.includes("Page context switched"))
1210-
) {
1210+
const executeSingleActionWithRetry = async (
1211+
instruction: string,
1212+
params?: TaskParams
1213+
) => {
1214+
const maxRetries = 3;
1215+
for (let i = 0; i < maxRetries; i++) {
1216+
try {
1217+
return await this.executeSingleAction(
1218+
instruction,
1219+
getActivePage,
1220+
params
1221+
);
1222+
} catch (err: unknown) {
1223+
const maybeError = err as {
1224+
statusCode?: number;
1225+
message?: string;
1226+
};
1227+
if (
1228+
maybeError?.statusCode === 409 ||
1229+
(maybeError?.message &&
1230+
maybeError.message.includes("Page context switched"))
1231+
) {
12111232
if (this.debug) {
12121233
console.log(
12131234
"[HyperPage] Action aborted due to tab switch, retrying on new page..."

‎src/cdp/element-resolver.ts‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -161,24 +161,6 @@ async function resolveFrameSession(
161161
);
162162
}
163163

164-
async function ensureRootSession(
165-
ctx: ElementResolveContext
166-
): Promise<CDPSession> {
167-
try {
168-
const session = ctx.cdpClient.rootSession;
169-
const cache = getSessionCache(ctx.cdpClient);
170-
if (!cache.has(0)) {
171-
cache.set(0, session);
172-
}
173-
return session;
174-
} catch {
175-
const session = await ctx.cdpClient.acquireSession("dom");
176-
const cache = getSessionCache(ctx.cdpClient);
177-
cache.set(0, session);
178-
return session;
179-
}
180-
}
181-
182164
function getSessionCache(client: CDPClient): Map<number, CDPSession> {
183165
let cache = sessionCache.get(client);
184166
if (!cache) {

‎src/cdp/types.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export interface CDPSession {
2-
send<T = any>(method: string, params?: Record<string, unknown>): Promise<T>;
3-
on(event: string, handler: (...payload: any[]) => void): void;
4-
off?(event: string, handler: (...payload: any[]) => void): void;
2+
send<T = unknown>(method: string, params?: Record<string, unknown>): Promise<T>;
3+
on(event: string, handler: (...payload: unknown[]) => void): void;
4+
off?(event: string, handler: (...payload: unknown[]) => void): void;
55
detach(): Promise<void>;
66
raw?: unknown;
77
id?: string | null;

‎src/utils/debugWriter.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface DebugData {
1616
foundElement?: {
1717
elementId: string;
1818
method: string;
19-
arguments: any[];
19+
arguments: unknown[];
2020
xpath?: string;
2121
};
2222
availableElements?: Array<{

‎src/utils/html-to-markdown.ts‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ turndownService.addRule("removeUnwantedTags", {
1313
});
1414

1515
turndownService.addRule("inlineLink", {
16-
filter: function (node: any, options: any) {
16+
filter: function (node: HTMLElement, options: TurndownService.Options) {
1717
return (
1818
options.linkStyle === "inlined" &&
1919
node.nodeName === "A" &&
2020
node.getAttribute("href")
2121
);
2222
},
23-
replacement: function (content: string, node: any) {
24-
var href = node.getAttribute("href").trim();
25-
var title = node.title ? ' "' + node.title + '"' : "";
23+
replacement: function (content: string, node: HTMLElement) {
24+
const href = (node.getAttribute("href") ?? "").trim();
25+
const title = node.title ? ' "' + node.title + '"' : "";
2626
return "[" + content.trim() + "](" + href + title + ")\n";
2727
},
2828
});
@@ -54,7 +54,7 @@ const processMultiLineLinks = (markdownContent: string): string => {
5454
const removeSkipToContentLinks = (markdownContent: string): string => {
5555
// Remove [Skip to Content](#page) and [Skip to content](#skip)
5656
const newMarkdownContent = markdownContent.replace(
57-
/\[Skip to Content\]\(#[^\)]*\)/gi,
57+
/\[Skip to Content\]\(#[^)]*\)/gi,
5858
""
5959
);
6060
return newMarkdownContent;

0 commit comments

Comments
 (0)