aboutsummaryrefslogtreecommitdiff
path: root/src/domains/target.ts
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmail.com>2026-07-28 00:17:13 +0530
committerAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-08-02 19:12:46 +0530
commitbfba73fa2f1373abd8a5a704c9c833d6fce1b44f (patch)
treee684310863ef6d9aaa9a6bd3d4d3100d75ee9069 /src/domains/target.ts
parent5cfa7ccc8f4082db111bc0186c5fa3d44b767ad3 (diff)
refactor(domains): share target resolution and CDP error mapping
The ref-vs-x/y branch turned out to exist in one tool, not four; it is now one helper with tests covering the half-specified-coordinate and stale-ref cases. cdpCall folds the repeated cdp_error mapping at 24 active-session call sites; target-scoped callOnTarget/callBrowser sites are left alone. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'src/domains/target.ts')
-rw-r--r--src/domains/target.ts35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/domains/target.ts b/src/domains/target.ts
new file mode 100644
index 0000000..8a9ee07
--- /dev/null
+++ b/src/domains/target.ts
@@ -0,0 +1,35 @@
+import type { BrowserClient } from "../client";
+import { type Result, err, ok } from "../util/result";
+import type { ToolErr } from "../util/tool";
+import { resolveRefToBox } from "./ref-resolve";
+
+export type PointArgs = {
+ readonly ref?: string | undefined;
+ readonly x?: number | undefined;
+ readonly y?: number | undefined;
+};
+
+export type ResolvedTarget = {
+ readonly x: number;
+ readonly y: number;
+ readonly label: string;
+};
+
+export const resolveTarget = async (
+ client: BrowserClient,
+ args: PointArgs,
+): Promise<Result<ResolvedTarget, ToolErr>> => {
+ if (args.ref !== undefined) {
+ const box = await resolveRefToBox(client, args.ref);
+ if (!box.success) return box;
+ return ok({
+ x: box.data.cx,
+ y: box.data.cy,
+ label: `[${args.ref}] (${box.data.cx}, ${box.data.cy})`,
+ });
+ }
+ if (args.x !== undefined && args.y !== undefined) {
+ return ok({ x: args.x, y: args.y, label: `(${args.x}, ${args.y})` });
+ }
+ return err({ kind: "invalid_state", message: "Provide either `ref` or both `x` and `y`." });
+};