Overview
Concrete type-safe wrapper around chrome.debugger.sendCommand with timeout handling and pending command tracking for CDP interactions.
Description
The CdpCommander class provides a Promise-based interface to Chrome DevTools Protocol commands. Each call to `sendCommand` wraps `chrome.debugger.sendCommand` in a Promise with a configurable timeout (default 10 seconds). Pending commands are tracked per tab in a `pendingCommands` map, allowing bulk rejection when a tab's debugger detaches (via `rejectPendingCommands`). The class handles `chrome.runtime.lastError` checks and timeout cleanup.
Usage
- Send CDP command: `const result = await new CdpCommander(tabId).sendCommand("Accessibility.getFullAXTree", {})`
- Reject pending: `rejectPendingCommands(tabId, "Debugger detached")` on debugger detach events.
Code Reference
Source Location
- Repository: AIPex
- File: packages/browser-runtime/src/automation/cdp-commander.ts
- Lines: L25-76 (class), L14-23 (rejectPendingCommands)
Signature
export class CdpCommander {
constructor(readonly tabId: number)
async sendCommand<T = unknown>(
command: string,
params: Record<string, unknown>,
timeout?: number
): Promise<T>
}
export function rejectPendingCommands(tabId: number, reason: string): void
// Default timeout: 10000ms
Import
import { CdpCommander, rejectPendingCommands } from "@aipexstudio/browser-runtime";
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| command |
string |
Yes |
CDP method name (e.g., "Accessibility.getFullAXTree")
|
| params |
Record<string, unknown> |
Yes |
CDP command parameters
|
| timeout |
number |
No |
Timeout in ms (default 10000)
|
Outputs
| Name |
Type |
Description
|
| returns |
T |
CDP response result (typed via generic)
|
Usage Examples
Send CDP Command
import { CdpCommander } from "@aipexstudio/browser-runtime";
const commander = new CdpCommander(tabId);
const axTree = await commander.sendCommand("Accessibility.getFullAXTree", {});
console.log(axTree.nodes.length);
Resolve DOM Node
const commander = new CdpCommander(tabId);
const remoteObject = await commander.sendCommand("DOM.resolveNode", {
backendNodeId: backendDOMNodeId,
});
// remoteObject.object.objectId for further CDP calls
Related Pages
Implements Principle
Requires Environment
Page Connections
Double-click a node to navigate. Hold to expand connections.