diff options
| author | amankumarsingh77 <amankumarsingh7702@gmail.com> | 2026-07-28 00:17:13 +0530 |
|---|---|---|
| committer | Aman Kumar <145339995+amankumarsingh77@users.noreply.github.com> | 2026-08-02 19:12:46 +0530 |
| commit | bfba73fa2f1373abd8a5a704c9c833d6fce1b44f (patch) | |
| tree | e684310863ef6d9aaa9a6bd3d4d3100d75ee9069 /src/domains/keyboard.ts | |
| parent | 5cfa7ccc8f4082db111bc0186c5fa3d44b767ad3 (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/keyboard.ts')
| -rw-r--r-- | src/domains/keyboard.ts | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/src/domains/keyboard.ts b/src/domains/keyboard.ts index 473f633..4ee8243 100644 --- a/src/domains/keyboard.ts +++ b/src/domains/keyboard.ts @@ -3,6 +3,7 @@ import { safeJs } from "../util/js-template"; import { virtualKeyCode } from "../util/keycodes"; import { type Result, err, ok } from "../util/result"; import { defineBrowserTool, type ToolErr, type ToolOk } from "../util/tool"; +import { cdpCall } from "./cdp-call"; import { resolveRefToObjectId } from "./ref-resolve"; const TypeArgs = Type.Object({ @@ -47,8 +48,8 @@ export const typeTool = defineBrowserTool({ }); } } - const r = await client.session().call("Input.insertText", { text: args.text }); - if (!r.success) return err({ kind: "cdp_error", message: r.error.message }); + const r = await cdpCall(client, "Input.insertText", { text: args.text }); + if (!r.success) return r; return ok({ text: `Typed: "${args.text}"` }); }, }); @@ -94,18 +95,18 @@ export const pressKeyTool = defineBrowserTool({ ...(code ? { windowsVirtualKeyCode: code, nativeVirtualKeyCode: code } : {}), ...(isChar ? { text: k, unmodifiedText: k } : {}), }; - const down = await client.session().call("Input.dispatchKeyEvent", downParams); - if (!down.success) return err({ kind: "cdp_error", message: down.error.message }); + const down = await cdpCall(client, "Input.dispatchKeyEvent", downParams); + if (!down.success) return down; // For a printable character with no command modifier (Ctrl/Meta/Alt = 1|2|4), // emit a `char` event so the page receives keypress/textInput and the // character is actually inserted. Shift (8) is allowed (capitals/symbols). const hasCommandModifier = (modifiers & (1 | 2 | 4)) !== 0; if (isChar && !hasCommandModifier) { - const charEv = await client.session().call("Input.dispatchKeyEvent", { type: "char", text: k, key: k }); - if (!charEv.success) return err({ kind: "cdp_error", message: charEv.error.message }); + const charEv = await cdpCall(client, "Input.dispatchKeyEvent", { type: "char", text: k, key: k }); + if (!charEv.success) return charEv; } - const up = await client.session().call("Input.dispatchKeyEvent", { type: "keyUp", key: k, code: k, modifiers }); - if (!up.success) return err({ kind: "cdp_error", message: up.error.message }); + const up = await cdpCall(client, "Input.dispatchKeyEvent", { type: "keyUp", key: k, code: k, modifiers }); + if (!up.success) return up; return ok({ text: `Pressed: ${k}${modifiers ? ` (modifiers=${modifiers})` : ""}` }); }, }); @@ -151,13 +152,13 @@ export const dispatchKeyTool = defineBrowserTool({ if (args.ref !== undefined) { const objectId = await resolveRefToObjectId(client, args.ref); if (!objectId.success) return objectId; - const r = await client.session().call("Runtime.callFunctionOn", { + const r = await cdpCall(client, "Runtime.callFunctionOn", { objectId: objectId.data, functionDeclaration: `function (type, key, keyCode) { this.dispatchEvent(new KeyboardEvent(type, { key, keyCode, which: keyCode, bubbles: true, cancelable: true })); return 1; }`, arguments: [{ value: eventType }, { value: args.key }, { value: code }], returnByValue: true, }); - if (!r.success) return err({ kind: "cdp_error", message: r.error.message }); + if (!r.success) return r; return ok({ text: `Dispatched ${eventType}('${args.key}') on ${target}`, details: { matched: 1, ref: args.ref }, |
