@mearl/provider 2.21.0 → 2.22.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.
package/README.md CHANGED
@@ -4,6 +4,8 @@
4
4
  Provider 使用相同的 `BrowserProviderDefinition`;区别只在于运行时位于 Native Host 进程内,
5
5
  还是通过本地 Provider Socket 接入。
6
6
 
7
+ action 清单位于 `src/generated/provider-actions.ts`,由根目录 `pnpm run generate:provider-actions` 从 browser-core 协议生成。
8
+
7
9
  ## 核心模型
8
10
 
9
11
  控制会话由 Mearl 管理。Provider 继续接收目标实例、页面与普通 action,无需处理控制会话 ID、资源归属或人工交接状态。带 `--session` 的释放只结束 Mearl 任务控制;实例关闭、退出接管与 Cookie 同步仍使用 Provider 的生命周期方法。上传与下载分别按 `capabilities.actions` 中的 `page_upload`、`page_download` 声明使用。
@@ -40,7 +42,7 @@ pnpm add @mearl/provider
40
42
  "provider": {
41
43
  "providerId": "example-device",
42
44
  "name": "Example device provider",
43
- "protocolVersion": 2,
45
+ "protocolVersion": 3,
44
46
  "entry": "./dist/runtime.js",
45
47
  "browserType": {
46
48
  "name": "Device",
@@ -147,7 +149,7 @@ Provider 需要查询设备池、已有会话或动态候选项时,声明 `bro
147
149
  初次查询携带静态表单默认值;查询只传入可解析的字段,最终启动仍严格校验全部输入。
148
150
  用户在等待期间输入的内容会保留,切换模式或环境会使旧请求失效。
149
151
  管理器通过内部
150
- `browser_provider_launch_form` 请求获取 `ProviderLaunchForm`,不识别 Provider ID 或业务字段:
152
+ `browser_providers` 的 `launch_form` operation 获取 `ProviderLaunchForm`,不识别 Provider ID 或业务字段:
151
153
 
152
154
  - `inputSchema` 描述当前模式的用户输入,`required` 决定必填标识与提交校验。
153
155
  - `variants` 的 `id/title/description` 描述连接方式,`variant` 指定当前方式。
@@ -185,3 +187,17 @@ npx @mearl/setup provider install <package>
185
187
  npx @mearl/setup provider update <provider-id>
186
188
  npx @mearl/setup provider uninstall <provider-id>
187
189
  ```
190
+
191
+ ## Socket 协议
192
+
193
+ 当前 Provider 协议版本为 **3**,请求 envelope 携带 `protocolVersion`。安装元数据、
194
+ 运行时 descriptor、运行中进程与调用方必须使用相同协议;不匹配时更新相关包并重启进程。
195
+
196
+ - `browser_providers`:`operation: "configure"` 携带 `options`,`operation: "launch_form"` 携带 `variant?` 和 `values`。
197
+ - `browser_instances`:`operation: "start" | "detach" | "sync_cookies"`;同步携带 `scope`,目标实例由 envelope 的 `browserId` 选择。
198
+ - `browser_launch`、`browser_close`、`get_browser_access_url` 保留各自职责。
199
+ - `browser_sessions` 由 Mearl 控制进程处理,不转交外部 Provider;会话释放与实例 `detach` 分开执行。
200
+
201
+ SDK 负责将 Socket 请求分发到 `configure/getLaunchForm/start/detach/syncCookies` 等方法。
202
+ `browser_release` 普通 action 不承担实例解绑;公开 CLI 的无会话释放由 Mearl 转换为 `detach`。
203
+ 打包 SDK 的 Provider 必须更新 SDK 并重新构建,修改安装元数据不能替换运行中的旧代码。
@@ -3,6 +3,7 @@ export interface ProviderControlHostRecord extends DaemonRecord {
3
3
  socketPath: string;
4
4
  token: string;
5
5
  ready: boolean;
6
+ protocolVersion: number;
6
7
  }
7
8
  export declare function providerControlHostFile(): string;
8
9
  export declare function providerControlSocketPath(pid: number): string;
@@ -2,6 +2,7 @@ import { spawn } from 'node:child_process';
2
2
  import { randomUUID } from 'node:crypto';
3
3
  import { tmpdir } from 'node:os';
4
4
  import { join } from 'node:path';
5
+ import { assertProviderProtocolVersion, MEARL_PROVIDER_PROTOCOL_VERSION } from './protocol.js';
5
6
  import { isAlive, keyedConfigFile, claimRecord, readRecord, removeInvalidRecord, removeRecordIfPid, writeRecordIfPid, } from '@mearl/daemon-core';
6
7
  export function providerControlHostFile() {
7
8
  return keyedConfigFile('control', 'providers');
@@ -13,9 +14,10 @@ export function providerControlSocketPath(pid) {
13
14
  }
14
15
  export function readProviderControlHost() {
15
16
  const record = readRecord(providerControlHostFile());
16
- return record?.ready && typeof record.socketPath === 'string' && isAlive(record.pid)
17
- ? record
18
- : null;
17
+ if (!record?.ready || typeof record.socketPath !== 'string' || !isAlive(record.pid))
18
+ return null;
19
+ assertProviderProtocolVersion(record.protocolVersion, 'Provider control host');
20
+ return record;
19
21
  }
20
22
  let startup;
21
23
  /** A single host owns Provider sessions across independent CLI and connector processes. */
@@ -36,7 +38,13 @@ export function ensureProviderControlHost(timeoutMs = 10_000) {
36
38
  else
37
39
  removeInvalidRecord(file);
38
40
  const token = randomUUID();
39
- if (claimRecord(file, { pid: process.pid, socketPath: '', token, ready: false })) {
41
+ if (claimRecord(file, {
42
+ pid: process.pid,
43
+ socketPath: '',
44
+ token,
45
+ ready: false,
46
+ protocolVersion: MEARL_PROVIDER_PROTOCOL_VERSION,
47
+ })) {
40
48
  try {
41
49
  const entry = process.env.MEARL_NATIVE_HOST_ENTRY;
42
50
  const child = spawn(entry ? process.execPath : 'mearl-native-host', [...(entry ? [entry] : []), '--provider-control', token], { detached: true, stdio: 'ignore', windowsHide: true });
@@ -52,6 +60,7 @@ export function ensureProviderControlHost(timeoutMs = 10_000) {
52
60
  socketPath: providerControlSocketPath(child.pid),
53
61
  token,
54
62
  ready: false,
63
+ protocolVersion: MEARL_PROVIDER_PROTOCOL_VERSION,
55
64
  });
56
65
  }
57
66
  catch (error) {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './types.js';
2
- export { PROVIDER_ACTIONS, isProviderAction } from './generated-provider-actions.js';
2
+ export { assertProviderProtocolVersion } from './protocol.js';
3
+ export { PROVIDER_ACTIONS, isProviderAction } from './generated/provider-actions.js';
3
4
  export { providerDataDir } from './registry.js';
4
5
  export { createProviderRuntime, validateProviderBrowser, validateProviderInput, validateProviderLaunchForm, } from './provider-runtime.js';
5
6
  export { createProviderServer } from './server.js';
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './types.js';
2
- export { PROVIDER_ACTIONS, isProviderAction } from './generated-provider-actions.js';
2
+ export { assertProviderProtocolVersion } from './protocol.js';
3
+ export { PROVIDER_ACTIONS, isProviderAction } from './generated/provider-actions.js';
3
4
  export { providerDataDir } from './registry.js';
4
5
  export { createProviderRuntime, validateProviderBrowser, validateProviderInput, validateProviderLaunchForm, } from './provider-runtime.js';
5
6
  export { createProviderServer } from './server.js';
package/dist/launcher.js CHANGED
@@ -4,13 +4,18 @@ import { closeSync, existsSync, mkdirSync, openSync } from 'node:fs';
4
4
  import { join } from 'node:path';
5
5
  import { hashKey, keyedDir, rotateLogIfNeeded } from '@mearl/daemon-core';
6
6
  import { PROVIDER_START_TOKEN_ENV, claimProviderStart, listInstalledProviders, listLiveProviders, readLiveProvider, readProviderStart, releaseProviderStart, transferProviderStart, validateInstalledProviderManifest, } from './registry.js';
7
+ import { assertProviderProtocolVersion } from './protocol.js';
7
8
  export const PROVIDER_LOGS_SUBDIR = 'providers/logs';
8
9
  function providerLogFile(providerId) {
9
10
  const hash = hashKey(providerId);
10
11
  return join(keyedDir(PROVIDER_LOGS_SUBDIR), `${hash}.log`);
11
12
  }
12
- function liveProviderIds() {
13
- return new Set(listLiveProviders().map(record => record.providerId));
13
+ function hasCompatibleLiveProvider(providerId) {
14
+ const live = readLiveProvider(providerId);
15
+ if (!live)
16
+ return false;
17
+ assertProviderProtocolVersion(live.providerProtocolVersion, providerId);
18
+ return true;
14
19
  }
15
20
  function delay(ms) {
16
21
  return new Promise(resolve => setTimeout(resolve, ms));
@@ -52,38 +57,39 @@ export async function ensureInstalledProvidersRunning(providerIds) {
52
57
  const warnings = [];
53
58
  const selected = providerIds ? new Set(providerIds) : undefined;
54
59
  const installed = listInstalledProviders().filter(provider => !selected || selected.has(provider.providerId));
55
- const live = liveProviderIds();
56
60
  const starting = new Map();
57
61
  for (const manifest of installed) {
58
- if (live.has(manifest.providerId))
59
- continue;
60
62
  const invalid = validateInstalledProviderManifest(manifest);
61
63
  if (invalid) {
62
64
  warnings.push(`provider ${manifest.providerId || '(unknown)'}: ${invalid}`);
63
65
  continue;
64
66
  }
65
- const startToken = randomUUID();
66
- if (!claimProviderStart(manifest.providerId, startToken)) {
67
- if (readLiveProvider(manifest.providerId))
68
- continue;
69
- if (readProviderStart(manifest.providerId))
70
- starting.set(manifest.providerId, null);
71
- else
72
- warnings.push(`provider ${manifest.providerId}: failed to acquire startup ownership`);
73
- continue;
74
- }
75
- if (readLiveProvider(manifest.providerId)) {
76
- releaseProviderStart(manifest.providerId, startToken);
77
- continue;
78
- }
79
- const logFile = providerLogFile(manifest.providerId);
67
+ let startToken;
80
68
  try {
69
+ if (hasCompatibleLiveProvider(manifest.providerId))
70
+ continue;
71
+ startToken = randomUUID();
72
+ if (!claimProviderStart(manifest.providerId, startToken)) {
73
+ if (hasCompatibleLiveProvider(manifest.providerId))
74
+ continue;
75
+ if (readProviderStart(manifest.providerId))
76
+ starting.set(manifest.providerId, null);
77
+ else
78
+ warnings.push(`provider ${manifest.providerId}: failed to acquire startup ownership`);
79
+ continue;
80
+ }
81
+ if (hasCompatibleLiveProvider(manifest.providerId)) {
82
+ releaseProviderStart(manifest.providerId, startToken);
83
+ continue;
84
+ }
85
+ const logFile = providerLogFile(manifest.providerId);
81
86
  const childPid = await spawnProvider(manifest, logFile, startToken);
82
87
  transferProviderStart(manifest.providerId, startToken, childPid);
83
88
  starting.set(manifest.providerId, startToken);
84
89
  }
85
90
  catch (error) {
86
- releaseProviderStart(manifest.providerId, startToken);
91
+ if (startToken)
92
+ releaseProviderStart(manifest.providerId, startToken);
87
93
  warnings.push(`provider ${manifest.providerId}: ${error instanceof Error ? error.message : String(error)}`);
88
94
  }
89
95
  }
@@ -93,12 +99,20 @@ export async function ensureInstalledProvidersRunning(providerIds) {
93
99
  let remaining = [...starting.keys()];
94
100
  while (remaining.length > 0 && Date.now() < deadline) {
95
101
  await delay(100);
96
- const current = liveProviderIds();
102
+ const current = new Set(listLiveProviders().map(record => record.providerId));
97
103
  remaining = remaining.filter(providerId => !current.has(providerId));
98
104
  }
99
105
  for (const [providerId, token] of starting) {
100
106
  if (!remaining.includes(providerId) && token)
101
107
  releaseProviderStart(providerId, token);
108
+ if (!remaining.includes(providerId)) {
109
+ try {
110
+ hasCompatibleLiveProvider(providerId);
111
+ }
112
+ catch (error) {
113
+ warnings.push(`provider ${providerId}: ${error instanceof Error ? error.message : String(error)}`);
114
+ }
115
+ }
102
116
  }
103
117
  for (const providerId of remaining) {
104
118
  const logFile = providerLogFile(providerId);
@@ -0,0 +1,2 @@
1
+ export declare const MEARL_PROVIDER_PROTOCOL_VERSION = 3;
2
+ export declare function assertProviderProtocolVersion(actual: unknown, peer?: string): void;
@@ -0,0 +1,6 @@
1
+ export const MEARL_PROVIDER_PROTOCOL_VERSION = 3;
2
+ export function assertProviderProtocolVersion(actual, peer = 'Provider') {
3
+ if (actual !== MEARL_PROVIDER_PROTOCOL_VERSION) {
4
+ throw new Error(`PROVIDER_PROTOCOL_MISMATCH: ${peer} uses protocol ${String(actual ?? 'unknown')}; expected ${MEARL_PROVIDER_PROTOCOL_VERSION}. Update Mearl and the Provider package, then restart the services.`);
5
+ }
6
+ }
@@ -1,5 +1,5 @@
1
1
  import { MEARL_PROVIDER_PROTOCOL_VERSION } from './types.js';
2
- import { isProviderAction } from './generated-provider-actions.js';
2
+ import { isProviderAction } from './generated/provider-actions.js';
3
3
  const LIFECYCLE_OPERATIONS = new Set([
4
4
  'start',
5
5
  'close',
package/dist/registry.js CHANGED
@@ -4,6 +4,7 @@ import { isAbsolute, join, relative, resolve, sep } from 'node:path';
4
4
  import { claimRecord, ensureDir, hashKey, isAlive, keyedConfigFile, keyedDir, listRecordFiles, readLiveRecords, readRecord, removeInvalidRecord, removeRecordIfPid, writeRecord, writeRecordIfPid, } from '@mearl/daemon-core';
5
5
  import { MEARL_PROVIDER_PROTOCOL_VERSION, } from './types.js';
6
6
  import { validateProviderLaunchForm, isProviderInputSchema } from './provider-runtime.js';
7
+ import { assertProviderProtocolVersion } from './protocol.js';
7
8
  export const INSTALLED_PROVIDERS_SUBDIR = 'providers/installed';
8
9
  export const LIVE_PROVIDERS_SUBDIR = 'providers/live';
9
10
  export const STARTING_PROVIDERS_SUBDIR = 'providers/starting';
@@ -93,8 +94,11 @@ export function validateInstalledProviderManifest(manifest) {
93
94
  !manifest.version.trim()) {
94
95
  return 'manifest is incomplete';
95
96
  }
96
- if (manifest.protocolVersion !== MEARL_PROVIDER_PROTOCOL_VERSION) {
97
- return `protocol ${manifest.protocolVersion} is unsupported`;
97
+ try {
98
+ assertProviderProtocolVersion(manifest.protocolVersion, `Installed Provider ${manifest.providerId}`);
99
+ }
100
+ catch (error) {
101
+ return error.message;
98
102
  }
99
103
  if (RESERVED_PROVIDER_IDS.has(manifest.providerId)) {
100
104
  return `providerId ${manifest.providerId} is reserved for a built-in Provider`;
package/dist/server.js CHANGED
@@ -3,10 +3,11 @@ import { randomUUID } from 'node:crypto';
3
3
  import { chmodSync, rmSync } from 'node:fs';
4
4
  import { dirname } from 'node:path';
5
5
  import { ensureDir } from '@mearl/daemon-core';
6
- import { isProviderAction } from './generated-provider-actions.js';
6
+ import { isProviderAction } from './generated/provider-actions.js';
7
7
  import { createProviderRuntime } from './provider-runtime.js';
8
8
  import { PROVIDER_START_TOKEN_ENV, claimProviderStart, providerBrowserKey, providerSocketPath, publishLiveProvider, publishProviderBrowsers, readLiveProvider, releaseProviderStart, removeStaleProviderSocket, touchProviderBrowser, unpublishLiveProvider, unpublishProviderBrowsers, } from './registry.js';
9
9
  import { MEARL_PROVIDER_PROTOCOL_VERSION } from './types.js';
10
+ import { assertProviderProtocolVersion } from './protocol.js';
10
11
  const MAX_REQUEST_BYTES = 16 * 1024 * 1024;
11
12
  const activeProviderIds = new Set();
12
13
  function errorMessage(error) {
@@ -29,6 +30,7 @@ function isProviderActionRequest(value) {
29
30
  request.id.length > 0 &&
30
31
  typeof request.action === 'string' &&
31
32
  request.action.length > 0 &&
33
+ typeof request.protocolVersion === 'number' &&
32
34
  (request.data === undefined ||
33
35
  (request.data !== null &&
34
36
  typeof request.data === 'object' &&
@@ -84,32 +86,39 @@ export function createProviderServer(options) {
84
86
  if (request.controlSessionId)
85
87
  throw new Error('CONTROL_SESSION_UNSUPPORTED: this Provider does not implement control sessions');
86
88
  const context = providerContext(request);
87
- if (request.action === 'browser_provider_launch_form') {
89
+ if (request.action === 'browser_providers') {
88
90
  if (request.browserId)
89
- throw new Error('Provider launch form does not accept browserId');
90
- return runtime.getLaunchForm({
91
- variant: request.data?.variant,
92
- values: request.data?.values,
93
- }, context);
91
+ throw new Error('Provider management does not accept browserId');
92
+ switch (request.data?.operation) {
93
+ case 'configure':
94
+ return runtime.configure(request.data.options, context);
95
+ case 'launch_form':
96
+ return runtime.getLaunchForm({
97
+ variant: request.data.variant,
98
+ values: request.data.values,
99
+ }, context);
100
+ default:
101
+ throw new Error('Invalid browser provider operation');
102
+ }
94
103
  }
95
104
  if (request.action === 'browser_launch') {
96
105
  if (request.browserId)
97
106
  throw new Error('browser_launch does not accept browserId');
98
107
  return runtime.launch(request.data ?? {}, context);
99
108
  }
100
- if (request.action === 'browser_provider_configure') {
101
- if (request.browserId)
102
- throw new Error('Provider configuration does not accept browserId');
103
- return runtime.configure(request.data ?? {}, context);
104
- }
105
- if (request.action === 'browser_manager_start') {
106
- return runtime.start({ browserId: await providerBrowserId(request.browserId) }, context);
107
- }
108
- if (request.action === 'browser_manager_sync_cookies') {
109
- return runtime.syncCookies({
110
- browserId: await providerBrowserId(request.browserId),
111
- scope: request.data?.scope,
112
- }, context);
109
+ if (request.action === 'browser_instances') {
110
+ if (!['start', 'detach', 'sync_cookies'].includes(String(request.data?.operation))) {
111
+ throw new Error('Invalid browser instance operation');
112
+ }
113
+ const browserId = await providerBrowserId(request.browserId);
114
+ switch (request.data.operation) {
115
+ case 'start':
116
+ return runtime.start({ browserId }, context);
117
+ case 'detach':
118
+ return runtime.detach({ browserId }, context);
119
+ case 'sync_cookies':
120
+ return runtime.syncCookies({ browserId, scope: request.data.scope }, context);
121
+ }
113
122
  }
114
123
  if (request.action !== 'get_versions' &&
115
124
  request.action !== 'browser_close' &&
@@ -141,12 +150,6 @@ export function createProviderServer(options) {
141
150
  capabilities: found.control.kind === 'provider' ? found.control.capabilities : undefined,
142
151
  };
143
152
  }
144
- if (request.action === 'browser_release') {
145
- const found = (await runtime.listBrowsers()).find(browser => browser.browserId === browserId);
146
- if (found?.operations.includes('detach')) {
147
- return runtime.detach({ browserId }, context);
148
- }
149
- }
150
153
  touchProviderBrowser(providerBrowserKey(descriptor.providerId, browserId));
151
154
  return runtime.invokeAction({
152
155
  browserId,
@@ -179,12 +182,19 @@ export function createProviderServer(options) {
179
182
  socket.end(`${JSON.stringify({ id: '', success: false, error: 'Invalid JSON request' })}\n`);
180
183
  return;
181
184
  }
185
+ const id = parsed && typeof parsed === 'object' && typeof parsed.id === 'string'
186
+ ? parsed.id
187
+ : '';
188
+ if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
189
+ try {
190
+ assertProviderProtocolVersion(parsed.protocolVersion);
191
+ }
192
+ catch (error) {
193
+ socket.end(`${JSON.stringify({ id, success: false, error: errorMessage(error) })}\n`);
194
+ return;
195
+ }
196
+ }
182
197
  if (!isProviderActionRequest(parsed)) {
183
- const id = parsed &&
184
- typeof parsed === 'object' &&
185
- typeof parsed.id === 'string'
186
- ? parsed.id
187
- : '';
188
198
  socket.end(`${JSON.stringify({ id, success: false, error: 'Invalid request envelope' })}\n`);
189
199
  return;
190
200
  }
package/dist/types.d.ts CHANGED
@@ -1,6 +1,6 @@
1
- import type { ProviderAction } from './generated-provider-actions.js';
2
- export declare const MEARL_PROVIDER_PROTOCOL_VERSION = 2;
3
- export type { ProviderAction } from './generated-provider-actions.js';
1
+ import type { ProviderAction } from './generated/provider-actions.js';
2
+ export { MEARL_PROVIDER_PROTOCOL_VERSION } from './protocol.js';
3
+ export type { ProviderAction } from './generated/provider-actions.js';
4
4
  export type JsonPrimitive = string | number | boolean | null;
5
5
  export type JsonValue = JsonPrimitive | JsonValue[] | JsonObject;
6
6
  export interface JsonObject {
@@ -197,6 +197,7 @@ export interface ProviderActionRequest {
197
197
  /** Provider-local browser id selected by the Mearl client. */
198
198
  browserId?: string;
199
199
  version?: string;
200
+ protocolVersion: number;
200
201
  controlSource?: string;
201
202
  host?: ProviderHostContextData;
202
203
  }
package/dist/types.js CHANGED
@@ -1 +1 @@
1
- export const MEARL_PROVIDER_PROTOCOL_VERSION = 2;
1
+ export { MEARL_PROVIDER_PROTOCOL_VERSION } from './protocol.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mearl/provider",
3
- "version": "2.21.0",
3
+ "version": "2.22.0",
4
4
  "description": "Unified contracts and runtime utilities for built-in and external Mearl browser providers",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -35,7 +35,7 @@
35
35
  "registry": "https://registry.npmjs.org"
36
36
  },
37
37
  "dependencies": {
38
- "@mearl/daemon-core": "2.21.0"
38
+ "@mearl/daemon-core": "2.22.0"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/node": "^24.9.1",