@mearl/native-host 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +99 -0
  2. package/dist/agent/codex-provider.d.ts +45 -0
  3. package/dist/agent/event-transport.d.ts +3 -0
  4. package/dist/agent/instructions.d.ts +1 -0
  5. package/dist/agent/manager.d.ts +44 -0
  6. package/dist/agent/process-env.d.ts +6 -0
  7. package/dist/agent/provider.d.ts +53 -0
  8. package/dist/agent/qoder-provider.d.ts +49 -0
  9. package/dist/agent/skill-manager.d.ts +28 -0
  10. package/dist/agent/workspace-directory.d.ts +4 -0
  11. package/dist/analytics.d.ts +1 -0
  12. package/dist/browser-inventory.d.ts +33 -0
  13. package/dist/browser-registry.d.ts +78 -0
  14. package/dist/cdp-backend/action-registry.d.ts +35 -0
  15. package/dist/cdp-backend/browser-handlers.d.ts +7 -0
  16. package/dist/cdp-backend/buffers.d.ts +46 -0
  17. package/dist/cdp-backend/cdp-session.d.ts +60 -0
  18. package/dist/cdp-backend/cdp-utils.d.ts +66 -0
  19. package/dist/cdp-backend/client.d.ts +47 -0
  20. package/dist/cdp-backend/composite-handlers.d.ts +19 -0
  21. package/dist/cdp-backend/controlled-indicator.d.ts +17 -0
  22. package/dist/cdp-backend/cookie-transfer.d.ts +25 -0
  23. package/dist/cdp-backend/discover.d.ts +28 -0
  24. package/dist/cdp-backend/domain-manager.d.ts +4 -0
  25. package/dist/cdp-backend/handlers.d.ts +29 -0
  26. package/dist/cdp-backend/http-handlers.d.ts +7 -0
  27. package/dist/cdp-backend/interaction-handlers.d.ts +16 -0
  28. package/dist/cdp-backend/mock-handlers.d.ts +6 -0
  29. package/dist/cdp-backend/navigation-observer.d.ts +7 -0
  30. package/dist/cdp-backend/page-observer.d.ts +5 -0
  31. package/dist/cdp-backend/recording-handlers.d.ts +9 -0
  32. package/dist/cdp-backend/request-handlers.d.ts +7 -0
  33. package/dist/cdp-backend/snapshot-handlers.d.ts +6 -0
  34. package/dist/cdp-backend/tab-handlers.d.ts +7 -0
  35. package/dist/cdp-backend/tdbank-handlers.d.ts +13 -0
  36. package/dist/cdp-backend/timezone-handlers.d.ts +3 -0
  37. package/dist/cloud-connector.d.ts +56 -0
  38. package/dist/index.d.ts +2 -0
  39. package/dist/index.js +26945 -0
  40. package/dist/install.d.ts +36 -0
  41. package/dist/install.js +220 -0
  42. package/dist/managed-browser.d.ts +85 -0
  43. package/package.json +44 -0
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Shared utilities for CDP backend handlers.
3
+ *
4
+ * Contains: HTTP helper (nodeFetch), cookie utilities, page-load waiters,
5
+ * viewport helpers, and serialized function sources for page injection.
6
+ */
7
+ import type { PageSession } from './cdp-session.js';
8
+ import { type CdpTarget } from './discover.js';
9
+ export declare const TYPE_INTO_ELEMENT_SRC: string;
10
+ export declare const CLICK_ELEMENT_VIA_JS_SRC: string;
11
+ export declare const GET_CLICK_COORDS_FROM_NODE_SRC: string;
12
+ export interface CdpViewportMetrics {
13
+ cssLayoutViewport?: {
14
+ clientWidth?: number;
15
+ clientHeight?: number;
16
+ };
17
+ cssVisualViewport?: {
18
+ clientWidth?: number;
19
+ clientHeight?: number;
20
+ scale?: number;
21
+ };
22
+ layoutViewport?: {
23
+ clientWidth?: number;
24
+ clientHeight?: number;
25
+ };
26
+ visualViewport?: {
27
+ clientWidth?: number;
28
+ clientHeight?: number;
29
+ scale?: number;
30
+ };
31
+ }
32
+ export declare function nodeFetch(urlStr: string, options?: {
33
+ method?: string;
34
+ headers?: Record<string, string>;
35
+ body?: string;
36
+ timeoutMs?: number;
37
+ }): Promise<{
38
+ status: number;
39
+ statusText: string;
40
+ headers: Record<string, string>;
41
+ body: string;
42
+ }>;
43
+ export declare function getCookiesForUrl(session: PageSession, targetUrl: string): Promise<Map<string, string>>;
44
+ export declare function decodeTracknick(raw: string): string;
45
+ export declare function buildMultipartFormBody(fields: Record<string, string>): {
46
+ contentType: string;
47
+ body: string;
48
+ };
49
+ export declare function waitForPageLoad(session: PageSession, timeoutMs: number): Promise<void>;
50
+ export declare function waitForTargetLoad(session: {
51
+ port: number;
52
+ }, targetId: string, timeoutMs: number): Promise<CdpTarget | null>;
53
+ export declare function getPageViewportSize(session: PageSession): Promise<{
54
+ width: number;
55
+ height: number;
56
+ } | null>;
57
+ export declare function getCdpInputViewportSize(session: PageSession): Promise<{
58
+ width: number;
59
+ height: number;
60
+ } | null>;
61
+ export declare function toCdpInputPoint(session: PageSession, x: number, y: number): Promise<{
62
+ x: number;
63
+ y: number;
64
+ }>;
65
+ export declare function dispatchCdpClick(session: PageSession, x: number, y: number): Promise<void>;
66
+ export declare function resolveBackendNodeObjectId(session: PageSession, backendNodeId: number): Promise<string>;
@@ -0,0 +1,47 @@
1
+ /**
2
+ * Minimal WebSocket-based CDP client for native-host (no external deps).
3
+ * Uses node:http upgrade event + raw frame handling via node:net Socket.
4
+ */
5
+ type EventCallback = (params: any) => void;
6
+ /**
7
+ * A virtual client that routes CDP messages through a specific sessionId
8
+ * on a shared browser-level WebSocket connection.
9
+ *
10
+ * This is used for page-level commands when Chrome does not allow direct
11
+ * connections to /devtools/page/... (e.g., when enabled via chrome://inspect).
12
+ */
13
+ export declare class CdpChildSession {
14
+ private readonly parent;
15
+ readonly sessionId: string;
16
+ constructor(parent: CdpClient, sessionId: string);
17
+ sendCommand(method: string, params?: Record<string, any>, timeoutMs?: number): Promise<any>;
18
+ on(method: string, callback: EventCallback): () => void;
19
+ get closed(): boolean;
20
+ close(): void;
21
+ }
22
+ export declare class CdpClient {
23
+ private socket;
24
+ private buffer;
25
+ private nextId;
26
+ private pending;
27
+ private eventHandlers;
28
+ /** sessionId → method → callbacks (for multiplexed page sessions) */
29
+ private sessionEventHandlers;
30
+ private _closed;
31
+ private constructor();
32
+ /** Create a child session client that routes through this connection via sessionId. */
33
+ createChildSession(sessionId: string): CdpChildSession;
34
+ /** Send a command within a specific CDP session (multiplexed on this connection). */
35
+ sendCommandInSession(sessionId: string, method: string, params?: Record<string, any>, timeoutMs?: number): Promise<any>;
36
+ /** Subscribe to events from a specific CDP session. */
37
+ onSession(sessionId: string, method: string, callback: EventCallback): () => void;
38
+ static connect(wsUrl: string): Promise<CdpClient>;
39
+ private consumeFrames;
40
+ private handleMessage;
41
+ sendCommand(method: string, params?: Record<string, any>, timeoutMs?: number): Promise<any>;
42
+ on(method: string, callback: EventCallback): () => void;
43
+ private sendText;
44
+ close(): void;
45
+ get closed(): boolean;
46
+ }
47
+ export {};
@@ -0,0 +1,19 @@
1
+ /**
2
+ * CDP composite action handlers.
3
+ *
4
+ * Provides `run_actions` — sequential batch execution of multiple actions
5
+ * within a single request, sharing the same CDP session — and the observed
6
+ * page-action wrapper that gives page interaction actions their default
7
+ * built-in observation.
8
+ */
9
+ import type { CdpSession } from './cdp-session.js';
10
+ import { type PageActAction } from '@mearl/browser-core';
11
+ export declare function cdpRunActions(session: CdpSession, data: any): Promise<any>;
12
+ /**
13
+ * Runs a page interaction action with built-in observation: executes the raw
14
+ * handler inside `runPageAct` and reports resulting page changes / navigation
15
+ * in the same round trip. Callers opt out with `observe: false`, which
16
+ * dispatches the raw handler directly. `run_actions` steps never go through
17
+ * this wrapper — they use dispatchCdpAction and stay lightweight.
18
+ */
19
+ export declare function cdpObservedPageAction(session: CdpSession, action: PageActAction, data: any): Promise<any>;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * CDP 模式的受控页面视觉标识。
3
+ *
4
+ * 与扩展模式的 favicon 替换保持一致:agent 操作过的页面把 favicon 换成 Mearl
5
+ * 图标。CDP 模式拿不到扩展资源,图标以 data URL 内联在注入脚本里,不依赖任何
6
+ * 网络请求。
7
+ *
8
+ * 注入通过 Page.addScriptToEvaluateOnNewDocument + runImmediately 完成:每个
9
+ * PageSession 只注册一次,注册的脚本在 session 存续期内跨导航自动生效;
10
+ * session 断开重建后由下一次动作重新注册。
11
+ */
12
+ import type { PageSession } from './cdp-session.js';
13
+ /**
14
+ * 确保受控标识已注入到该 page session。幂等且 best-effort:
15
+ * 失败时撤销标记,下一次动作重试;任何异常都不阻断 agent 动作本身。
16
+ */
17
+ export declare function ensureControlledIndicator(session: PageSession): Promise<void>;
@@ -0,0 +1,25 @@
1
+ /** Cookie export/import helpers for copying selected domains between CDP browsers. */
2
+ import type { CdpSession } from './cdp-session.js';
3
+ export interface TransferableCookie {
4
+ name: string;
5
+ value: string;
6
+ domain: string;
7
+ path: string;
8
+ secure?: boolean;
9
+ httpOnly?: boolean;
10
+ sameSite?: 'Strict' | 'Lax' | 'None';
11
+ expires?: number;
12
+ priority?: 'Low' | 'Medium' | 'High';
13
+ sameParty?: boolean;
14
+ sourceScheme?: 'Unset' | 'NonSecure' | 'Secure';
15
+ sourcePort?: number;
16
+ partitionKey?: {
17
+ topLevelSite: string;
18
+ hasCrossSiteAncestor?: boolean;
19
+ };
20
+ }
21
+ export declare function normalizeCookieDomain(input: string): string;
22
+ export declare function normalizeCookieDomains(inputs: unknown): string[];
23
+ export declare function cookieMatchesDomain(cookieDomain: string, requestedDomain: string): boolean;
24
+ export declare function cdpExportCookies(session: CdpSession, domains: string[]): Promise<TransferableCookie[]>;
25
+ export declare function cdpImportCookies(session: CdpSession, cookies: TransferableCookie[]): Promise<number>;
@@ -0,0 +1,28 @@
1
+ /**
2
+ * CDP target discovery.
3
+ *
4
+ * Strategy (in order):
5
+ * 1. Read Chrome's DevToolsActivePort file — written by Chrome when remote
6
+ * debugging is enabled (including via chrome://inspect/#remote-debugging).
7
+ * Contains the actual port and browser WebSocket path.
8
+ * 2. Fall back to HTTP /json REST API on the given port (requires Chrome
9
+ * launched with --remote-debugging-port).
10
+ */
11
+ export interface CdpTarget {
12
+ id: string;
13
+ title: string;
14
+ type: string;
15
+ url: string;
16
+ webSocketDebuggerUrl: string;
17
+ faviconUrl?: string;
18
+ }
19
+ /**
20
+ * Read Chrome's DevToolsActivePort file.
21
+ * Returns { port, browserWsPath } or null if unavailable.
22
+ */
23
+ export declare function readDevToolsActivePort(userDataDir?: string): {
24
+ port: number;
25
+ browserWsPath: string;
26
+ } | null;
27
+ export declare function discoverTargets(port?: number): Promise<CdpTarget[]>;
28
+ export declare function isCdpAvailable(port?: number, userDataDir?: string): Promise<boolean>;
@@ -0,0 +1,4 @@
1
+ import type { CdpChildSession, CdpClient } from './client.js';
2
+ type DomainClient = CdpClient | CdpChildSession;
3
+ export declare function ensureCdpDomain(client: DomainClient, domain: string, params?: Record<string, unknown>): Promise<void>;
4
+ export {};
@@ -0,0 +1,29 @@
1
+ /**
2
+ * CDP backend action handlers — re-export hub.
3
+ *
4
+ * All handler implementations live in dedicated sub-modules.
5
+ * Importing this file triggers side-effect registrations in the action registry.
6
+ */
7
+ import './request-handlers.js';
8
+ import './mock-handlers.js';
9
+ import './http-handlers.js';
10
+ import './browser-handlers.js';
11
+ import './tdbank-handlers.js';
12
+ import './tab-handlers.js';
13
+ import './interaction-handlers.js';
14
+ import './timezone-handlers.js';
15
+ import './snapshot-handlers.js';
16
+ import './recording-handlers.js';
17
+ import './composite-handlers.js';
18
+ export { dispatchCdpAction, getRegisteredActionNames } from './action-registry.js';
19
+ export { cdpGetRequests, cdpGetLogs, cdpGetEvents } from './request-handlers.js';
20
+ export { cdpSetMock, cdpGetMocks } from './mock-handlers.js';
21
+ export { cdpSendRequest, cdpSendMtopRequest, cdpGetApiSchema } from './http-handlers.js';
22
+ export { cdpPageScreenshot, cdpPageSelectedElement, cdpGetUserInfo } from './browser-handlers.js';
23
+ export { cdpTdbankAccount } from './tdbank-handlers.js';
24
+ export { cdpTabOpen, cdpTabClose, cdpTabList } from './tab-handlers.js';
25
+ export { cdpPageClick, cdpPageType, cdpPageScroll, cdpPageEval, cdpPagePress, cdpPageWait, cdpPageNavigate, cdpPageUpload, cdpSetDeviceEmulation, } from './interaction-handlers.js';
26
+ export { cdpPageSnapshot, cdpPageFrames } from './snapshot-handlers.js';
27
+ export { cdpRecordStart, cdpRecordStop } from './recording-handlers.js';
28
+ export { cdpRunActions, cdpObservedPageAction } from './composite-handlers.js';
29
+ export { cdpSetTimezone } from './timezone-handlers.js';
@@ -0,0 +1,7 @@
1
+ /**
2
+ * CDP HTTP proxy / MTOP request / API schema handlers.
3
+ */
4
+ import type { PageSession } from './cdp-session.js';
5
+ export declare function cdpSendRequest(session: PageSession, data: any): Promise<any>;
6
+ export declare function cdpSendMtopRequest(session: PageSession, data: any): Promise<any>;
7
+ export declare function cdpGetApiSchema(session: PageSession, data: any): Promise<any>;
@@ -0,0 +1,16 @@
1
+ /**
2
+ * CDP page interaction handlers.
3
+ *
4
+ * Covers: click, type, scroll, eval, press, wait, navigate, upload, device emulation.
5
+ */
6
+ import type { PageSession } from './cdp-session.js';
7
+ export declare function cdpPageClick(session: PageSession, data: any): Promise<any>;
8
+ export declare function cdpPageType(session: PageSession, data: any): Promise<any>;
9
+ export declare function cdpPageScroll(session: PageSession, data: any): Promise<any>;
10
+ export declare function cdpPageEval(session: PageSession, data: any): Promise<any>;
11
+ export declare function cdpPagePress(session: PageSession, data: any): Promise<any>;
12
+ export declare function cdpPageWait(session: PageSession, data: any): Promise<any>;
13
+ export declare function cdpPageNavigate(session: PageSession, data: any): Promise<any>;
14
+ export declare function cdpPageUpload(session: PageSession, data: any): Promise<any>;
15
+ export declare function cdpSetDeviceEmulation(session: PageSession, data: any): Promise<any>;
16
+ export declare function cdpPageHover(session: PageSession, data: any): Promise<any>;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * CDP mock interception handlers.
3
+ */
4
+ import type { PageSession } from './cdp-session.js';
5
+ export declare function cdpSetMock(session: PageSession, data: any): Promise<any>;
6
+ export declare function cdpGetMocks(_session: PageSession, _data: any): any;
@@ -0,0 +1,7 @@
1
+ import { type PageActNavigationObserver } from '@mearl/browser-core';
2
+ import type { PageSession } from './cdp-session.js';
3
+ export declare function startCdpNavigationObserver(session: PageSession, urlBefore: string, options?: {
4
+ trackNetwork?: boolean;
5
+ networkIdleMs?: number;
6
+ contentReadyAfterMs?: number;
7
+ }): Promise<PageActNavigationObserver>;
@@ -0,0 +1,5 @@
1
+ import { type PageChangeSet, type PageObservationCheckpoint, type PageObservationWaitOptions } from '@mearl/browser-core';
2
+ import type { PageSession } from './cdp-session.js';
3
+ export declare function startPageObservation(session: PageSession): Promise<PageObservationCheckpoint>;
4
+ export declare function finishPageObservation(session: PageSession, checkpoint: PageObservationCheckpoint, options?: PageObservationWaitOptions): Promise<PageChangeSet>;
5
+ export declare function disposePageObservation(session: PageSession): Promise<void>;
@@ -0,0 +1,9 @@
1
+ /**
2
+ * CDP recording session handlers.
3
+ *
4
+ * Manages browser-side action recording via Runtime.addBinding +
5
+ * Page.addScriptToEvaluateOnNewDocument.
6
+ */
7
+ import type { PageSession } from './cdp-session.js';
8
+ export declare function cdpRecordStart(session: PageSession, _data: any): Promise<any>;
9
+ export declare function cdpRecordStop(session: PageSession, _data: any): Promise<any>;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * CDP request/log/event buffer query handlers.
3
+ */
4
+ import type { PageSession } from './cdp-session.js';
5
+ export declare function cdpGetRequests(session: PageSession, data: any): Promise<any>;
6
+ export declare function cdpGetLogs(session: PageSession, data: any): Promise<any>;
7
+ export declare function cdpGetEvents(session: PageSession, data: any): Promise<any>;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * CDP accessibility tree snapshot and frame handlers.
3
+ */
4
+ import type { PageSession } from './cdp-session.js';
5
+ export declare function cdpPageSnapshot(session: PageSession, data: any): Promise<any>;
6
+ export declare function cdpPageFrames(session: PageSession, _data: any): Promise<any>;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * CDP tab management handlers.
3
+ */
4
+ import type { CdpSession } from './cdp-session.js';
5
+ export declare function cdpTabOpen(session: CdpSession, data: any): Promise<any>;
6
+ export declare function cdpTabClose(session: CdpSession, data: any): Promise<any>;
7
+ export declare function cdpTabList(session: CdpSession, _data: any): Promise<any>;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * CDP TDBank account handlers.
3
+ *
4
+ * Manages TDBank account listing, switching, borrowing via CDP cookie-based auth.
5
+ */
6
+ import type { CdpSession } from './cdp-session.js';
7
+ /** Prepare a TDBank SSO ticket without changing the controller browser account. */
8
+ export declare function cdpPrepareTdbankSso(cdpSession: CdpSession, data: any): Promise<any>;
9
+ /** Consume a TDBank SSO ticket inside an isolated managed browser. */
10
+ export declare function cdpConsumeTdbankSso(cdpSession: CdpSession, ssoUrl: string, expectedLoginId?: string): Promise<{
11
+ nickname: string;
12
+ }>;
13
+ export declare function cdpTdbankAccount(cdpSession: CdpSession, data: any): Promise<any>;
@@ -0,0 +1,3 @@
1
+ /** CDP timezone override handler. */
2
+ import type { PageSession } from './cdp-session.js';
3
+ export declare function cdpSetTimezone(session: PageSession, data: any): Promise<any>;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Cloud connector process management — start/stop/list cloud-connector
3
+ * child processes that bridge the local native-host to remote cloud servers.
4
+ *
5
+ * Lifecycle design
6
+ * ----------------
7
+ * cloud-connector processes are spawned `detached` and intentionally OUTLIVE
8
+ * the native-host that started them. The native-host is an ephemeral broker:
9
+ * Chrome kills it whenever the extension reloads / updates (stdin EOF). If a
10
+ * connector were a normal child it would die with that broker, tearing down the
11
+ * cloud bridge on every extension reload.
12
+ *
13
+ * Shared registry & ownership
14
+ * ---------------------------
15
+ * Connectors are also self-manageable from the terminal (`cloud-connector start
16
+ * <url>`), so the host-managed and self-managed modes must not fight over the
17
+ * same connector. They interoperate through a single shared registry under
18
+ * ~/.mearl/connectors/ (one JSON record per serverUrl), with paths
19
+ * derived via @mearl/daemon-core so both sides agree on locations.
20
+ *
21
+ * The CONNECTOR PROCESS is the sole writer of its own record (it writes on
22
+ * connect, removes on graceful exit). This native-host only READS the registry
23
+ * to adopt/list connectors, and best-effort removes a record when it stops or
24
+ * reaps one. A freshly started native-host reads the registry, verifies the
25
+ * recorded pids are still alive, and ADOPTS them instead of spawning duplicates.
26
+ * Starting a connector is therefore idempotent: if a healthy one already exists
27
+ * for the serverUrl (started by us, a previous host, or the daemon CLI) we reuse
28
+ * it. Connectors die only when explicitly stopped, when they exit on their own,
29
+ * or when the machine reboots — never merely because the broker restarted.
30
+ */
31
+ export interface CloudConnectorListResult {
32
+ connectors: Array<{
33
+ serverUrl: string;
34
+ pid: number;
35
+ }>;
36
+ }
37
+ /**
38
+ * Initialise module-level dependencies. Must be called before any other export.
39
+ */
40
+ export declare function initCloudConnector(deps: {
41
+ log: (...args: any[]) => void;
42
+ isDaemonMode: boolean;
43
+ sendToExtension: (message: Record<string, any>) => void;
44
+ version: string;
45
+ }): void;
46
+ export declare function startCloudConnector(serverUrl: string): Promise<{
47
+ success: boolean;
48
+ pid?: number;
49
+ reused?: boolean;
50
+ error?: string;
51
+ }>;
52
+ export declare function stopCloudConnector(serverUrl: string): {
53
+ success: boolean;
54
+ error?: string;
55
+ };
56
+ export declare function listCloudConnectors(): CloudConnectorListResult;
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};