/ extension / src / protocol.ts
protocol.ts
 1  /**
 2   * opencli browser protocol — shared types between daemon, extension, and CLI.
 3   *
 4   * 5 actions: exec, navigate, tabs, cookies, screenshot.
 5   * Everything else is just JS code sent via 'exec'.
 6   */
 7  
 8  export type Action =
 9    | 'exec'
10    | 'navigate'
11    | 'tabs'
12    | 'cookies'
13    | 'screenshot'
14    | 'close-window'
15    | 'sessions'
16    | 'set-file-input'
17    | 'insert-text'
18    | 'bind-current'
19    | 'network-capture-start'
20    | 'network-capture-read'
21    | 'cdp'
22    | 'frames';
23  
24  export interface Command {
25    /** Unique request ID */
26    id: string;
27    /** Action type */
28    action: Action;
29    /** Target page identity (targetId). Cross-layer contract with the daemon. */
30    page?: string;
31    /** JS code to evaluate in page context (exec action) */
32    code?: string;
33    /** Logical workspace for automation session reuse */
34    workspace?: string;
35    /** URL to navigate to (navigate action) */
36    url?: string;
37    /** Sub-operation for tabs: list, new, close, select */
38    op?: 'list' | 'new' | 'close' | 'select';
39    /** Tab index for tabs select/close */
40    index?: number;
41    /** Cookie domain filter */
42    domain?: string;
43    /** Optional hostname/domain to require for current-tab binding */
44    matchDomain?: string;
45    /** Optional pathname prefix to require for current-tab binding */
46    matchPathPrefix?: string;
47    /** Screenshot format: png (default) or jpeg */
48    format?: 'png' | 'jpeg';
49    /** JPEG quality (0-100), only for jpeg format */
50    quality?: number;
51    /** Whether to capture full page (not just viewport) */
52    fullPage?: boolean;
53    /** Local file paths for set-file-input action */
54    files?: string[];
55    /** CSS selector for file input element (set-file-input action) */
56    selector?: string;
57    /** Raw text payload for insert-text action */
58    text?: string;
59    /** URL substring filter pattern for network capture actions */
60    pattern?: string;
61    /** CDP method name for 'cdp' action (e.g. 'Accessibility.getFullAXTree') */
62    cdpMethod?: string;
63    /** CDP method params for 'cdp' action */
64    cdpParams?: Record<string, unknown>;
65    /** When true, automation windows are created in the foreground (focused) */
66    windowFocused?: boolean;
67    /** Custom idle timeout in seconds for this workspace session. Overrides the default. */
68    idleTimeout?: number;
69    /** Frame index for cross-frame operations (0-based, from 'frames' action) */
70    frameIndex?: number;
71  }
72  
73  export interface Result {
74    /** Matching request ID */
75    id: string;
76    /** Whether the command succeeded */
77    ok: boolean;
78    /** Result data on success */
79    data?: unknown;
80    /** Error message on failure */
81    error?: string;
82    /** Page identity (targetId) — present only on page-scoped command responses */
83    page?: string;
84  }
85  
86  /** Default daemon port */
87  export const DAEMON_PORT = 19825;
88  export const DAEMON_HOST = 'localhost';
89  export const DAEMON_WS_URL = `ws://${DAEMON_HOST}:${DAEMON_PORT}/ext`;
90  /** Lightweight health-check endpoint — probed before each WebSocket attempt. */
91  export const DAEMON_PING_URL = `http://${DAEMON_HOST}:${DAEMON_PORT}/ping`;
92  
93  /** Base reconnect delay for extension WebSocket (ms) */
94  export const WS_RECONNECT_BASE_DELAY = 2000;
95  /** Max reconnect delay (ms) — kept short since daemon is long-lived */
96  export const WS_RECONNECT_MAX_DELAY = 5000;