@mearl/cloud-connector 3.0.0-beta.5 → 3.0.0-beta.8
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 +6 -34
- package/dist/chunks/chunk-QOO7GAHM.js +379 -0
- package/dist/cli.js +5 -5
- package/dist/contracts.d.ts +2 -0
- package/dist/contracts.js +0 -0
- package/dist/endpoint.d.ts +6 -14
- package/dist/index.d.ts +3 -3
- package/dist/index.js +1 -15
- package/dist/plugins.d.ts +17 -0
- package/package.json +12 -9
- package/dist/chunks/chunk-Z4WXDOPB.js +0 -1000
- package/dist/qoder-auth.d.ts +0 -12
- package/dist/qoder.d.ts +0 -108
package/README.md
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# @mearl/cloud-connector
|
|
2
2
|
|
|
3
|
-
本地连接器,用于把云端 Agent 的操作转发到本地 native-host
|
|
4
|
-
Qoder Cloud Agent 使用官方 Session API、SSE 事件流和 Client-Side 自定义工具。
|
|
3
|
+
本地连接器,用于把云端 Agent 的操作转发到本地 native-host。内置传输为 WebSocket;其他协议通过已安装的能力包加载。Qoder 的凭据、配对和 SSE 实现在可选的 [`@mearl/plugin-qoder`](../../plugin-qoder/README.md) 中。
|
|
5
4
|
|
|
6
5
|
## 安装
|
|
7
6
|
|
|
@@ -27,9 +26,7 @@ mearl-cloud-connector <command> [server-url|pid] [options] # 管理后台守护
|
|
|
27
26
|
|
|
28
27
|
**参数:**
|
|
29
28
|
|
|
30
|
-
- `server-url` ——
|
|
31
|
-
`qoder://pair/<code>?created=<timestamp>`。`qoder://` 是不含凭证的配对标识,不是网络
|
|
32
|
-
地址,也不会被转换成 WebSocket URL。
|
|
29
|
+
- `server-url` —— `ws://` / `wss://`,或已安装能力声明的 URI。核心不解释插件 URI 的配对参数。
|
|
33
30
|
|
|
34
31
|
**子命令:**
|
|
35
32
|
|
|
@@ -59,7 +56,7 @@ mearl-cloud-connector <command> [server-url|pid] [options] # 管理后台守护
|
|
|
59
56
|
# 后台启动一个自管理连接器,拥有独立日志文件(推荐)
|
|
60
57
|
mearl-cloud-connector start "wss://cloud.example.com/ws?token=xxx" --name work-mac
|
|
61
58
|
|
|
62
|
-
#
|
|
59
|
+
# 可选协议示例;须先安装 @mearl/plugin-qoder,按其 README 生成地址
|
|
63
60
|
mearl-cloud-connector start "qoder://pair/pairing-code-1234?created=1786723200000"
|
|
64
61
|
|
|
65
62
|
# 前台运行,仅用于排障或交给 systemd / PM2 等外部进程管理器
|
|
@@ -84,7 +81,7 @@ mearl-cloud-connector start "ws://localhost:8080/ws?token=xxx" --heartbeat 60 --
|
|
|
84
81
|
旧版本创建、尚未包含连接状态字段的运行记录显示为 `unknown`,重启该连接器后即可补齐。
|
|
85
82
|
输出中的 token 会被隐藏。
|
|
86
83
|
|
|
87
|
-
`start`
|
|
84
|
+
`start` 会在连接成功后返回,后台进程继续运行。可选配对协议请按对应插件指引提交输出的
|
|
88
85
|
`pair` 工具调用;如果配对工具调用已经结束、取消或过期,需要重新生成配对 URL,旧 URL
|
|
89
86
|
不能重复使用。
|
|
90
87
|
|
|
@@ -130,19 +127,7 @@ native-host 与守护进程 CLI 都通过这套共享路径(由 [`@mearl/daemo
|
|
|
130
127
|
|
|
131
128
|
> 前台运行(含 native-host 托管)时日志输出到 stdout/stderr,不写独立日志文件;只有守护进程模式才落盘到 `<hash>.log`。
|
|
132
129
|
|
|
133
|
-
|
|
134
|
-
`MEARL_ENV_FILE`、`~/.mearl/.env`;macOS 还会读取当前用户钥匙串中 service 为
|
|
135
|
-
`ai.mearl.qoder.pat` 的通用密码。PAT 只用于访问 Qoder Cloud Session API,连接器不会把它
|
|
136
|
-
写入 `qoder://` URL、进程参数、注册表或日志。配对完成后,连接器
|
|
137
|
-
保持 SSE 连接,收到 `agent.custom_tool_use` 后调用现有本地 Mearl action,并以
|
|
138
|
-
`user.custom_tool_result` 回传。截图会优先作为 base64 图片内容块回传;若 Cloud API 拒绝
|
|
139
|
-
图片块,则退回完整 JSON 文本,避免丢失数据。
|
|
140
|
-
|
|
141
|
-
macOS 可在本地终端交互式写入钥匙串,PAT 不会进入 shell 历史:
|
|
142
|
-
|
|
143
|
-
```bash
|
|
144
|
-
security add-generic-password -U -a "$USER" -s ai.mearl.qoder.pat -w
|
|
145
|
-
```
|
|
130
|
+
可选协议的认证与配对方式见对应能力包文档;核心不读取其凭据。
|
|
146
131
|
|
|
147
132
|
## 前置条件
|
|
148
133
|
|
|
@@ -180,20 +165,7 @@ security add-generic-password -U -a "$USER" -s ai.mearl.qoder.pat -w
|
|
|
180
165
|
Chrome Extension / CDP
|
|
181
166
|
```
|
|
182
167
|
|
|
183
|
-
|
|
184
|
-
cloud-server 服务:
|
|
185
|
-
|
|
186
|
-
```
|
|
187
|
-
Qoder Cloud Agent(mearl Client-Side 自定义工具)
|
|
188
|
-
↓ agent.custom_tool_use / user.custom_tool_result
|
|
189
|
-
Qoder Cloud Session API + SSE
|
|
190
|
-
↓
|
|
191
|
-
@mearl/cloud-connector(本进程)
|
|
192
|
-
↓ (Unix Socket)
|
|
193
|
-
@mearl/native-host
|
|
194
|
-
↓
|
|
195
|
-
Chrome Extension / CDP
|
|
196
|
-
```
|
|
168
|
+
已安装的连接协议使用同一生命周期和状态回调;具体拓扑由插件说明。
|
|
197
169
|
|
|
198
170
|
## 注意事项
|
|
199
171
|
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import WebSocket from "ws";
|
|
3
|
+
import {
|
|
4
|
+
isHeartbeatMessage,
|
|
5
|
+
MAX_BUFFER_SIZE,
|
|
6
|
+
DEFAULT_HEARTBEAT_INTERVAL
|
|
7
|
+
} from "@mearl/cloud-types";
|
|
8
|
+
import { resolveActionTimeoutSec } from "@mearl/client/action-timeouts";
|
|
9
|
+
|
|
10
|
+
// src/identity.ts
|
|
11
|
+
import { hostname } from "node:os";
|
|
12
|
+
import { randomUUID } from "node:crypto";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import { DAEMON_HOME, readRecord, writeRecord } from "@mearl/daemon-core";
|
|
15
|
+
var IDENTITY_FILE = join(DAEMON_HOME, "cloud-connector-identity.json");
|
|
16
|
+
function resolveConnectorIdentity(options) {
|
|
17
|
+
const explicitId = options.connectorId?.trim() || process.env.MEARL_CONNECTOR_ID?.trim();
|
|
18
|
+
const explicitName = options.connectorName?.trim() || process.env.MEARL_CONNECTOR_NAME?.trim();
|
|
19
|
+
const stored = readRecord(IDENTITY_FILE);
|
|
20
|
+
const connectorId = explicitId || stored?.connectorId || randomUUID();
|
|
21
|
+
if (!explicitId && !stored?.connectorId) {
|
|
22
|
+
try {
|
|
23
|
+
writeRecord(IDENTITY_FILE, { pid: process.pid, connectorId });
|
|
24
|
+
} catch {
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
const name = explicitName || hostname() || `connector-${connectorId.slice(0, 8)}`;
|
|
28
|
+
if (!/^[A-Za-z0-9._:-]{1,128}$/.test(connectorId)) {
|
|
29
|
+
throw new Error('connectorId must contain only letters, numbers, ".", "_", ":", or "-"');
|
|
30
|
+
}
|
|
31
|
+
if (name.length > 128 || new RegExp("\\p{Cc}", "u").test(name)) {
|
|
32
|
+
throw new Error("connectorName must be 1-128 characters without control characters");
|
|
33
|
+
}
|
|
34
|
+
return { connectorId, name };
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/endpoint.ts
|
|
38
|
+
function parseConnectorEndpoint(value) {
|
|
39
|
+
if (!value) return null;
|
|
40
|
+
const serverUrl = value.trim();
|
|
41
|
+
try {
|
|
42
|
+
const parsed = new URL(serverUrl);
|
|
43
|
+
if (!parsed.hostname || parsed.hash || parsed.username || parsed.password) return null;
|
|
44
|
+
const scheme = parsed.protocol.slice(0, -1);
|
|
45
|
+
if (!/^[a-z][a-z0-9+-]{0,31}$/.test(scheme) || ["http", "https", "file", "data", "javascript", "chrome"].includes(scheme)) return null;
|
|
46
|
+
return { serverUrl, transportUrl: serverUrl, scheme };
|
|
47
|
+
} catch {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function isConnectorEndpoint(value) {
|
|
52
|
+
return parseConnectorEndpoint(value) !== null;
|
|
53
|
+
}
|
|
54
|
+
function requireConnectorEndpoint(value) {
|
|
55
|
+
const endpoint = parseConnectorEndpoint(value);
|
|
56
|
+
if (!endpoint) throw new Error("serverUrl must be a valid connector URL without credentials or a fragment");
|
|
57
|
+
return endpoint;
|
|
58
|
+
}
|
|
59
|
+
function isWebSocketEndpoint(endpoint) {
|
|
60
|
+
return endpoint.scheme === "ws" || endpoint.scheme === "wss";
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// src/plugins.ts
|
|
64
|
+
import { capabilityDirectory } from "@mearl/daemon-core/page-runtimes";
|
|
65
|
+
import { NodeCapabilityRegistry } from "@mearl/daemon-core/capabilities";
|
|
66
|
+
async function loadConnectorPlugin(scheme, options) {
|
|
67
|
+
const hook = `cloud.connector.${scheme}`;
|
|
68
|
+
const registry = new NodeCapabilityRegistry({}, capabilityDirectory(), { hooks: [hook] });
|
|
69
|
+
await registry.refresh();
|
|
70
|
+
const create = registry.require(hook);
|
|
71
|
+
const plugin = await create({ ...options, invokeAction: async (action, payload, requestOptions) => {
|
|
72
|
+
const client = await import("@mearl/client/local");
|
|
73
|
+
return client.invoke(action, payload, { ...requestOptions, controlSource: "remote" });
|
|
74
|
+
} });
|
|
75
|
+
if (!plugin || typeof plugin.connect !== "function" || typeof plugin.disconnect !== "function") throw new Error("Invalid connector capability");
|
|
76
|
+
return plugin;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// src/index.ts
|
|
80
|
+
var CLOUD_CONNECTOR_VERSION = true ? "3.0.0-beta.8" : "unknown";
|
|
81
|
+
var DEFAULT_RECONNECT_INTERVAL = 5e3;
|
|
82
|
+
var BACKOFF_BASE_MS = 1e3;
|
|
83
|
+
var BACKOFF_MAX_MS = 3e4;
|
|
84
|
+
var SLOW_PROBE_INTERVAL_MS = 6e4;
|
|
85
|
+
var FAST_RECONNECT_ATTEMPTS = 5;
|
|
86
|
+
var CONNECTOR_REPLACED_REASON = "Connector replaced by a newer connection";
|
|
87
|
+
var CloudConnector = class {
|
|
88
|
+
options;
|
|
89
|
+
endpoint;
|
|
90
|
+
identity;
|
|
91
|
+
plugin = null;
|
|
92
|
+
pluginLoad;
|
|
93
|
+
ws = null;
|
|
94
|
+
heartbeatTimer = null;
|
|
95
|
+
reconnectTimer = null;
|
|
96
|
+
reconnectAttempts = 0;
|
|
97
|
+
messageBuffer = "";
|
|
98
|
+
isManualClose = false;
|
|
99
|
+
shouldTryWss = false;
|
|
100
|
+
wssRetryAttempts = 0;
|
|
101
|
+
maxWssRetryAttempts = 1;
|
|
102
|
+
hasConnected = false;
|
|
103
|
+
lastConnectionError = null;
|
|
104
|
+
constructor(options) {
|
|
105
|
+
this.endpoint = requireConnectorEndpoint(options.serverUrl);
|
|
106
|
+
this.options = {
|
|
107
|
+
serverUrl: this.endpoint.serverUrl,
|
|
108
|
+
heartbeatInterval: options.heartbeatInterval ?? DEFAULT_HEARTBEAT_INTERVAL,
|
|
109
|
+
reconnectInterval: options.reconnectInterval ?? DEFAULT_RECONNECT_INTERVAL,
|
|
110
|
+
maxReconnectAttempts: options.maxReconnectAttempts ?? -1,
|
|
111
|
+
failFast: options.failFast ?? false,
|
|
112
|
+
onInitialConnectionFailed: options.onInitialConnectionFailed,
|
|
113
|
+
onConnected: options.onConnected,
|
|
114
|
+
onStateChange: options.onStateChange,
|
|
115
|
+
onSuperseded: options.onSuperseded
|
|
116
|
+
};
|
|
117
|
+
this.identity = resolveConnectorIdentity(options);
|
|
118
|
+
}
|
|
119
|
+
connect() {
|
|
120
|
+
if (!isWebSocketEndpoint(this.endpoint)) {
|
|
121
|
+
if (this.plugin) {
|
|
122
|
+
this.plugin.connect();
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
this.isManualClose = false;
|
|
126
|
+
if (this.pluginLoad) return;
|
|
127
|
+
this.pluginLoad = loadConnectorPlugin(this.endpoint.scheme, {
|
|
128
|
+
serverUrl: this.endpoint.serverUrl,
|
|
129
|
+
identity: this.identity,
|
|
130
|
+
reconnectInterval: this.options.reconnectInterval,
|
|
131
|
+
maxReconnectAttempts: this.options.maxReconnectAttempts,
|
|
132
|
+
failFast: this.options.failFast,
|
|
133
|
+
onConnected: () => {
|
|
134
|
+
this.hasConnected = true;
|
|
135
|
+
this.options.onConnected?.(this.options.serverUrl, this.identity);
|
|
136
|
+
},
|
|
137
|
+
onStateChange: (change) => this.emitState(change),
|
|
138
|
+
onInitialConnectionFailed: this.options.onInitialConnectionFailed
|
|
139
|
+
}).then((plugin) => {
|
|
140
|
+
if (this.isManualClose) {
|
|
141
|
+
plugin.disconnect();
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
this.plugin = plugin;
|
|
145
|
+
plugin.connect();
|
|
146
|
+
}).catch((error) => {
|
|
147
|
+
if (this.isManualClose) return;
|
|
148
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
149
|
+
this.emitState({ state: "failed", reason: message });
|
|
150
|
+
this.options.onInitialConnectionFailed?.(message);
|
|
151
|
+
}).finally(() => {
|
|
152
|
+
this.pluginLoad = void 0;
|
|
153
|
+
});
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (this.ws && (this.ws.readyState === 0 || this.ws.readyState === 1)) {
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
if (!this.hasConnected && this.reconnectAttempts === 0) {
|
|
160
|
+
this.emitState({ state: "connecting" });
|
|
161
|
+
}
|
|
162
|
+
let serverUrl = this.endpoint.transportUrl;
|
|
163
|
+
if (this.shouldTryWss && serverUrl.startsWith("ws://")) {
|
|
164
|
+
serverUrl = serverUrl.replace("ws://", "wss://");
|
|
165
|
+
console.log(`[CloudConnector] Retrying with wss://: ${serverUrl}`);
|
|
166
|
+
}
|
|
167
|
+
this.ws = new WebSocket(serverUrl);
|
|
168
|
+
this.ws.on("open", () => {
|
|
169
|
+
this.hasConnected = true;
|
|
170
|
+
this.lastConnectionError = null;
|
|
171
|
+
this.reconnectAttempts = 0;
|
|
172
|
+
this.shouldTryWss = false;
|
|
173
|
+
this.wssRetryAttempts = 0;
|
|
174
|
+
this.sendConnectorHello();
|
|
175
|
+
this.startHeartbeat();
|
|
176
|
+
this.emitState({ state: "connected" });
|
|
177
|
+
this.options.onConnected?.(this.options.serverUrl, this.identity);
|
|
178
|
+
console.log(`[CloudConnector] Connected to ${serverUrl}`);
|
|
179
|
+
});
|
|
180
|
+
this.ws.on("message", (data) => {
|
|
181
|
+
this.handleMessage(data);
|
|
182
|
+
});
|
|
183
|
+
this.ws.on("close", (code, reason) => {
|
|
184
|
+
this.handleClose(code, reason.toString(), serverUrl);
|
|
185
|
+
});
|
|
186
|
+
this.ws.on("error", (error) => {
|
|
187
|
+
console.error(`[CloudConnector] WebSocket error:`, error.message);
|
|
188
|
+
this.lastConnectionError = error.message;
|
|
189
|
+
if (error.message.includes("301") && !this.shouldTryWss && serverUrl.startsWith("ws://") && this.wssRetryAttempts < this.maxWssRetryAttempts) {
|
|
190
|
+
this.shouldTryWss = true;
|
|
191
|
+
this.wssRetryAttempts++;
|
|
192
|
+
this.ws = null;
|
|
193
|
+
console.log(
|
|
194
|
+
`[CloudConnector] Server requires wss://, retrying (${this.wssRetryAttempts}/${this.maxWssRetryAttempts})...`
|
|
195
|
+
);
|
|
196
|
+
setTimeout(() => this.connect(), 1e3);
|
|
197
|
+
}
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
disconnect() {
|
|
201
|
+
this.isManualClose = true;
|
|
202
|
+
this.plugin?.disconnect();
|
|
203
|
+
this.plugin = null;
|
|
204
|
+
this.stopHeartbeat();
|
|
205
|
+
if (this.reconnectTimer) {
|
|
206
|
+
clearTimeout(this.reconnectTimer);
|
|
207
|
+
this.reconnectTimer = null;
|
|
208
|
+
}
|
|
209
|
+
if (this.ws) {
|
|
210
|
+
this.ws.close();
|
|
211
|
+
this.ws = null;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
handleMessage(data) {
|
|
215
|
+
this.messageBuffer += data.toString("utf-8");
|
|
216
|
+
if (this.messageBuffer.length > MAX_BUFFER_SIZE) {
|
|
217
|
+
console.error("[CloudConnector] Message buffer exceeds limit, closing connection");
|
|
218
|
+
this.disconnect();
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
let newlineIndex;
|
|
222
|
+
while ((newlineIndex = this.messageBuffer.indexOf("\n")) !== -1) {
|
|
223
|
+
const line = this.messageBuffer.slice(0, newlineIndex);
|
|
224
|
+
this.messageBuffer = this.messageBuffer.slice(newlineIndex + 1);
|
|
225
|
+
if (!line.trim()) continue;
|
|
226
|
+
try {
|
|
227
|
+
const message = JSON.parse(line);
|
|
228
|
+
this.handleCloudMessage(message);
|
|
229
|
+
} catch (error) {
|
|
230
|
+
console.error(`[CloudConnector] Failed to parse message:`, line, error);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
handleClose(code, reasonText, serverUrl) {
|
|
235
|
+
console.log(`[CloudConnector] Disconnected (${code}: ${reasonText})`);
|
|
236
|
+
this.stopHeartbeat();
|
|
237
|
+
this.messageBuffer = "";
|
|
238
|
+
if (this.shouldTryWss && serverUrl.startsWith("ws://")) return;
|
|
239
|
+
if (code === 1e3 && reasonText === CONNECTOR_REPLACED_REASON) {
|
|
240
|
+
this.isManualClose = true;
|
|
241
|
+
console.error("[CloudConnector] Superseded by a newer connector; exiting");
|
|
242
|
+
this.options.onSuperseded?.();
|
|
243
|
+
return;
|
|
244
|
+
}
|
|
245
|
+
if (this.options.failFast && !this.hasConnected) {
|
|
246
|
+
const message = this.lastConnectionError || reasonText || `Connection closed (${code})`;
|
|
247
|
+
this.emitState({ state: "failed", reason: message });
|
|
248
|
+
this.options.onInitialConnectionFailed?.(message);
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
this.tryReconnect(this.lastConnectionError || reasonText || `Connection closed (${code})`);
|
|
252
|
+
}
|
|
253
|
+
async handleCloudMessage(message) {
|
|
254
|
+
if (isHeartbeatMessage(message)) {
|
|
255
|
+
this.emitState({ state: "connected", heartbeat: true });
|
|
256
|
+
if (message.type === "ping") {
|
|
257
|
+
this.sendHeartbeatResponse(message.timestamp);
|
|
258
|
+
}
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
const request = message;
|
|
262
|
+
const { id, action, data, browser, timeoutSec } = request;
|
|
263
|
+
try {
|
|
264
|
+
const client = await import("@mearl/client/local");
|
|
265
|
+
const result = await client.invoke(action, data, {
|
|
266
|
+
browser,
|
|
267
|
+
timeoutSec: timeoutSec ?? resolveActionTimeoutSec(action, data, 15),
|
|
268
|
+
controlSource: "remote",
|
|
269
|
+
...request.controlSessionId ? { controlSessionId: request.controlSessionId } : {}
|
|
270
|
+
});
|
|
271
|
+
this.sendResponse({ id, success: true, data: result });
|
|
272
|
+
} catch (error) {
|
|
273
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
274
|
+
this.sendResponse({ id, success: false, error: errorMessage });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
sendResponse(response) {
|
|
278
|
+
if (this.ws && this.ws.readyState === 1) {
|
|
279
|
+
const message = JSON.stringify(response) + "\n";
|
|
280
|
+
const bytes = Buffer.byteLength(message, "utf-8");
|
|
281
|
+
if (bytes > MAX_BUFFER_SIZE) {
|
|
282
|
+
console.error(
|
|
283
|
+
`[CloudConnector] Response ${response.id} exceeds message limit (${bytes} > ${MAX_BUFFER_SIZE} bytes)`
|
|
284
|
+
);
|
|
285
|
+
this.ws.send(
|
|
286
|
+
JSON.stringify({
|
|
287
|
+
id: response.id,
|
|
288
|
+
success: false,
|
|
289
|
+
error: `Cloud response exceeds ${MAX_BUFFER_SIZE} byte message limit (${bytes} bytes). Reduce the result size; for screenshots use JPEG quality or a selector.`
|
|
290
|
+
}) + "\n"
|
|
291
|
+
);
|
|
292
|
+
return;
|
|
293
|
+
}
|
|
294
|
+
this.ws.send(message);
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
sendConnectorHello() {
|
|
298
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
299
|
+
const message = {
|
|
300
|
+
type: "connector_hello",
|
|
301
|
+
connectorId: this.identity.connectorId,
|
|
302
|
+
name: this.identity.name,
|
|
303
|
+
...CLOUD_CONNECTOR_VERSION === "unknown" ? {} : { version: CLOUD_CONNECTOR_VERSION }
|
|
304
|
+
};
|
|
305
|
+
this.ws.send(JSON.stringify(message) + "\n");
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
sendHeartbeatResponse(_timestamp) {
|
|
309
|
+
if (this.ws && this.ws.readyState === 1) {
|
|
310
|
+
this.ws.send(
|
|
311
|
+
JSON.stringify({ type: "pong", timestamp: Date.now() }) + "\n"
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
startHeartbeat() {
|
|
316
|
+
this.stopHeartbeat();
|
|
317
|
+
this.heartbeatTimer = setInterval(() => {
|
|
318
|
+
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
|
319
|
+
this.ws.send(
|
|
320
|
+
JSON.stringify({ type: "ping", timestamp: Date.now() }) + "\n"
|
|
321
|
+
);
|
|
322
|
+
}
|
|
323
|
+
}, this.options.heartbeatInterval * 1e3);
|
|
324
|
+
}
|
|
325
|
+
stopHeartbeat() {
|
|
326
|
+
if (this.heartbeatTimer) {
|
|
327
|
+
clearInterval(this.heartbeatTimer);
|
|
328
|
+
this.heartbeatTimer = null;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
tryReconnect(reason) {
|
|
332
|
+
if (this.isManualClose) return;
|
|
333
|
+
if (this.reconnectTimer) return;
|
|
334
|
+
this.reconnectAttempts++;
|
|
335
|
+
if (this.options.maxReconnectAttempts !== -1 && this.reconnectAttempts > this.options.maxReconnectAttempts) {
|
|
336
|
+
console.log(
|
|
337
|
+
`[CloudConnector] Max reconnect attempts (${this.options.maxReconnectAttempts}) reached`
|
|
338
|
+
);
|
|
339
|
+
this.emitState({
|
|
340
|
+
state: "failed",
|
|
341
|
+
...reason ? { reason } : {},
|
|
342
|
+
reconnectAttempt: this.reconnectAttempts
|
|
343
|
+
});
|
|
344
|
+
return;
|
|
345
|
+
}
|
|
346
|
+
const isSlowProbe = this.reconnectAttempts > FAST_RECONNECT_ATTEMPTS;
|
|
347
|
+
const delay = isSlowProbe ? SLOW_PROBE_INTERVAL_MS : Math.min(BACKOFF_BASE_MS * Math.pow(2, this.reconnectAttempts - 1), BACKOFF_MAX_MS);
|
|
348
|
+
if (isSlowProbe) {
|
|
349
|
+
console.log(
|
|
350
|
+
`[CloudConnector] Slow probe reconnect in ${delay / 1e3}s (attempt ${this.reconnectAttempts})`
|
|
351
|
+
);
|
|
352
|
+
} else {
|
|
353
|
+
console.log(
|
|
354
|
+
`[CloudConnector] Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts}/${FAST_RECONNECT_ATTEMPTS})`
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
this.emitState({
|
|
358
|
+
state: "reconnecting",
|
|
359
|
+
...reason ? { reason } : {},
|
|
360
|
+
reconnectAttempt: this.reconnectAttempts,
|
|
361
|
+
retryInMs: delay
|
|
362
|
+
});
|
|
363
|
+
this.reconnectTimer = setTimeout(() => {
|
|
364
|
+
this.reconnectTimer = null;
|
|
365
|
+
this.connect();
|
|
366
|
+
}, delay);
|
|
367
|
+
}
|
|
368
|
+
emitState(change) {
|
|
369
|
+
this.options.onStateChange?.(change);
|
|
370
|
+
}
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
export {
|
|
374
|
+
parseConnectorEndpoint,
|
|
375
|
+
isConnectorEndpoint,
|
|
376
|
+
requireConnectorEndpoint,
|
|
377
|
+
resolveConnectorIdentity,
|
|
378
|
+
CloudConnector
|
|
379
|
+
};
|
package/dist/cli.js
CHANGED
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
CloudConnector,
|
|
4
4
|
isConnectorEndpoint,
|
|
5
5
|
resolveConnectorIdentity
|
|
6
|
-
} from "./chunks/chunk-
|
|
6
|
+
} from "./chunks/chunk-QOO7GAHM.js";
|
|
7
7
|
|
|
8
8
|
// src/cli.ts
|
|
9
9
|
import { Command, CommanderError, InvalidArgumentError } from "commander";
|
|
@@ -55,7 +55,7 @@ function resolveConnectorHealth(record, now = Date.now()) {
|
|
|
55
55
|
case "failed":
|
|
56
56
|
return "failed";
|
|
57
57
|
case "connected": {
|
|
58
|
-
if (record.serverUrl
|
|
58
|
+
if (!/^wss?:/i.test(record.serverUrl)) return "healthy";
|
|
59
59
|
const freshness = record.lastHeartbeatAt ?? record.connectedAt;
|
|
60
60
|
if (!freshness) return "degraded";
|
|
61
61
|
const timestamp = Date.parse(freshness);
|
|
@@ -83,7 +83,7 @@ function formatConnectorStatus(record, now = Date.now()) {
|
|
|
83
83
|
lines.push(` retry: attempt ${record.reconnectAttempt}`);
|
|
84
84
|
}
|
|
85
85
|
if (record.nextRetryAt) lines.push(` next retry: ${record.nextRetryAt}`);
|
|
86
|
-
if (
|
|
86
|
+
if (/^wss?:/i.test(record.serverUrl) && (health === "reconnecting" || health === "failed" || health === "degraded")) {
|
|
87
87
|
lines.push(
|
|
88
88
|
" next step: on the Agent machine, run mearl-cloud-server status; verify the current connection URL/token and that its port is reachable from this machine."
|
|
89
89
|
);
|
|
@@ -311,7 +311,7 @@ function runForeground(options) {
|
|
|
311
311
|
}
|
|
312
312
|
|
|
313
313
|
// src/cli.ts
|
|
314
|
-
var CLOUD_CONNECTOR_VERSION = true ? "3.0.0-beta.
|
|
314
|
+
var CLOUD_CONNECTOR_VERSION = true ? "3.0.0-beta.8" : "unknown";
|
|
315
315
|
var SUBCOMMANDS = ["start", "stop", "restart", "status", "list", "logs"];
|
|
316
316
|
var CONNECTION_OPTION_NAMES = /* @__PURE__ */ new Set([
|
|
317
317
|
"foreground",
|
|
@@ -441,7 +441,7 @@ function parseCloudConnectorArgs(argv) {
|
|
|
441
441
|
const serverUrl = target && isConnectorEndpoint(target) ? target : void 0;
|
|
442
442
|
if (target !== void 0 && !serverUrl && stopPid === void 0) {
|
|
443
443
|
throw new Error(
|
|
444
|
-
command === "stop" ? "stop target must be a
|
|
444
|
+
command === "stop" ? "stop target must be a connector server-url, or a positive pid" : "server-url must be a valid connector URL"
|
|
445
445
|
);
|
|
446
446
|
}
|
|
447
447
|
assertAllowedOptions(program, command);
|
|
File without changes
|
package/dist/endpoint.d.ts
CHANGED
|
@@ -1,20 +1,12 @@
|
|
|
1
|
-
export type ConnectorEndpointScheme =
|
|
2
|
-
export interface
|
|
3
|
-
/** User-facing URL retained for daemon identity and lifecycle commands. */
|
|
1
|
+
export type ConnectorEndpointScheme = string;
|
|
2
|
+
export interface ConnectorEndpoint {
|
|
4
3
|
serverUrl: string;
|
|
5
|
-
/** WebSocket URL used for the network connection. */
|
|
6
4
|
transportUrl: string;
|
|
7
|
-
scheme:
|
|
5
|
+
scheme: string;
|
|
8
6
|
}
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
serverUrl: string;
|
|
12
|
-
scheme: 'qoder';
|
|
13
|
-
pairingCode: string;
|
|
14
|
-
createdAt?: number;
|
|
15
|
-
}
|
|
16
|
-
export type ConnectorEndpoint = WebSocketConnectorEndpoint | QoderConnectorEndpoint;
|
|
17
|
-
/** Parse a connector endpoint without changing its user-facing daemon identity. */
|
|
7
|
+
export type WebSocketConnectorEndpoint = ConnectorEndpoint;
|
|
8
|
+
/** WebSocket is built in; other schemes require a locally installed capability. */
|
|
18
9
|
export declare function parseConnectorEndpoint(value: string | undefined): ConnectorEndpoint | null;
|
|
19
10
|
export declare function isConnectorEndpoint(value: string | undefined): value is string;
|
|
20
11
|
export declare function requireConnectorEndpoint(value: string): ConnectorEndpoint;
|
|
12
|
+
export declare function isWebSocketEndpoint(endpoint: ConnectorEndpoint): boolean;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { type ConnectorIdentity } from './identity.js';
|
|
2
2
|
import type { CloudConnectorStateChange } from './state.js';
|
|
3
|
-
export { isConnectorEndpoint, parseConnectorEndpoint, requireConnectorEndpoint, type ConnectorEndpoint, type ConnectorEndpointScheme, type
|
|
4
|
-
export { DEFAULT_QODER_API_BASE_URL, QODER_TOOL_NAME, QoderApiClient, QoderApiError, QoderConnector, formatQoderToolResult, parseQoderSse, type QoderContentBlock, type QoderPairingRequest, type QoderPublicEvent, type QoderSseFrame, } from './qoder.js';
|
|
3
|
+
export { isConnectorEndpoint, parseConnectorEndpoint, requireConnectorEndpoint, type ConnectorEndpoint, type ConnectorEndpointScheme, type WebSocketConnectorEndpoint, } from './endpoint.js';
|
|
5
4
|
export type { CloudConnectorRuntimeState, CloudConnectorStateChange } from './state.js';
|
|
6
5
|
export interface CloudConnectorOptions {
|
|
7
6
|
serverUrl: string;
|
|
@@ -23,7 +22,8 @@ export declare class CloudConnector {
|
|
|
23
22
|
private options;
|
|
24
23
|
private endpoint;
|
|
25
24
|
private identity;
|
|
26
|
-
private
|
|
25
|
+
private plugin;
|
|
26
|
+
private pluginLoad;
|
|
27
27
|
private ws;
|
|
28
28
|
private heartbeatTimer;
|
|
29
29
|
private reconnectTimer;
|
package/dist/index.js
CHANGED
|
@@ -1,26 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
CloudConnector,
|
|
3
|
-
DEFAULT_QODER_API_BASE_URL,
|
|
4
|
-
QODER_TOOL_NAME,
|
|
5
|
-
QoderApiClient,
|
|
6
|
-
QoderApiError,
|
|
7
|
-
QoderConnector,
|
|
8
|
-
formatQoderToolResult,
|
|
9
3
|
isConnectorEndpoint,
|
|
10
4
|
parseConnectorEndpoint,
|
|
11
|
-
parseQoderSse,
|
|
12
5
|
requireConnectorEndpoint
|
|
13
|
-
} from "./chunks/chunk-
|
|
6
|
+
} from "./chunks/chunk-QOO7GAHM.js";
|
|
14
7
|
export {
|
|
15
8
|
CloudConnector,
|
|
16
|
-
DEFAULT_QODER_API_BASE_URL,
|
|
17
|
-
QODER_TOOL_NAME,
|
|
18
|
-
QoderApiClient,
|
|
19
|
-
QoderApiError,
|
|
20
|
-
QoderConnector,
|
|
21
|
-
formatQoderToolResult,
|
|
22
9
|
isConnectorEndpoint,
|
|
23
10
|
parseConnectorEndpoint,
|
|
24
|
-
parseQoderSse,
|
|
25
11
|
requireConnectorEndpoint
|
|
26
12
|
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ConnectorIdentity } from './identity.js';
|
|
2
|
+
import type { CloudConnectorStateChange } from './state.js';
|
|
3
|
+
export interface ConnectorPlugin {
|
|
4
|
+
connect(): void;
|
|
5
|
+
disconnect(): void;
|
|
6
|
+
}
|
|
7
|
+
export interface ConnectorPluginOptions {
|
|
8
|
+
serverUrl: string;
|
|
9
|
+
identity: ConnectorIdentity;
|
|
10
|
+
reconnectInterval: number;
|
|
11
|
+
maxReconnectAttempts: number;
|
|
12
|
+
failFast: boolean;
|
|
13
|
+
onConnected(): void;
|
|
14
|
+
onStateChange(change: CloudConnectorStateChange): void;
|
|
15
|
+
onInitialConnectionFailed?: (message: string) => void;
|
|
16
|
+
}
|
|
17
|
+
export declare function loadConnectorPlugin(scheme: string, options: ConnectorPluginOptions): Promise<ConnectorPlugin>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mearl/cloud-connector",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.8",
|
|
4
4
|
"description": "Cloud connector for Mearl — bridges cloud agents to a local native-host",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -9,6 +9,10 @@
|
|
|
9
9
|
".": {
|
|
10
10
|
"types": "./dist/index.d.ts",
|
|
11
11
|
"import": "./dist/index.js"
|
|
12
|
+
},
|
|
13
|
+
"./contracts": {
|
|
14
|
+
"types": "./dist/contracts.d.ts",
|
|
15
|
+
"import": "./dist/contracts.js"
|
|
12
16
|
}
|
|
13
17
|
},
|
|
14
18
|
"bin": {
|
|
@@ -23,22 +27,21 @@
|
|
|
23
27
|
"keywords": [
|
|
24
28
|
"mearl",
|
|
25
29
|
"cloud-connector",
|
|
26
|
-
"websocket"
|
|
27
|
-
"qoder"
|
|
30
|
+
"websocket"
|
|
28
31
|
],
|
|
29
32
|
"license": "Apache-2.0",
|
|
30
33
|
"engines": {
|
|
31
|
-
"node": ">=
|
|
34
|
+
"node": ">=20.18.0"
|
|
32
35
|
},
|
|
33
36
|
"publishConfig": {
|
|
34
37
|
"registry": "https://registry.npmjs.org"
|
|
35
38
|
},
|
|
36
39
|
"dependencies": {
|
|
37
|
-
"commander": "^
|
|
40
|
+
"commander": "^14.0.3",
|
|
38
41
|
"ws": "^8.18.0",
|
|
39
|
-
"@mearl/
|
|
40
|
-
"@mearl/
|
|
41
|
-
"@mearl/
|
|
42
|
+
"@mearl/client": "3.0.0-beta.8",
|
|
43
|
+
"@mearl/cloud-types": "3.0.0-beta.8",
|
|
44
|
+
"@mearl/daemon-core": "3.0.0-beta.8"
|
|
42
45
|
},
|
|
43
46
|
"devDependencies": {
|
|
44
47
|
"@types/node": "^24.9.1",
|
|
@@ -48,7 +51,7 @@
|
|
|
48
51
|
},
|
|
49
52
|
"repository": {
|
|
50
53
|
"type": "git",
|
|
51
|
-
"url": "git+https://github.com/
|
|
54
|
+
"url": "git+https://github.com/mearlhq/mearl.git",
|
|
52
55
|
"directory": "packages/cloud/cloud-connector"
|
|
53
56
|
},
|
|
54
57
|
"scripts": {
|