claw-subagent-service 0.0.28 → 0.0.29

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claw-subagent-service",
3
- "version": "0.0.28",
3
+ "version": "0.0.29",
4
4
  "description": "虾说静态服务",
5
5
  "main": "cli.js",
6
6
  "bin": {
@@ -14,7 +14,8 @@
14
14
  "dev": "nodemon cli.js --run",
15
15
  "publish:patch": "npm version patch && npm publish",
16
16
  "publish:minor": "npm version minor && npm publish",
17
- "publish:major": "npm version major && npm publish"
17
+ "publish:major": "npm version major && npm publish",
18
+ "sync": "node scripts/sync-local.js"
18
19
  },
19
20
  "files": [
20
21
  "cli.js",
@@ -0,0 +1,248 @@
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * sync-local.js - 本地开发快速同步脚本
5
+ *
6
+ * 运行后将当前代码直接替换到全局安装的 claw-subagent-service 目录,
7
+ * 无需发布到 npm 后再重新下载安装。
8
+ *
9
+ * 用法:
10
+ * npm run sync (推荐)
11
+ * node scripts/sync-local.js
12
+ *
13
+ * 注意: Windows 下若服务正在运行,会自动停止服务、同步后再启动。
14
+ */
15
+
16
+ const { execSync, spawn } = require('child_process');
17
+ const path = require('path');
18
+ const fs = require('fs');
19
+ const os = require('os');
20
+
21
+ const isWindows = process.platform === 'win32';
22
+ const SERVICE_NAME = 'claw-subagent-service';
23
+ const PACKAGE_NAME = 'claw-subagent-service';
24
+ const ROOT = path.join(__dirname, '..');
25
+
26
+ // 需要同步的文件/目录(与 package.json files 保持一致)
27
+ const FILES_TO_SYNC = [
28
+ 'cli.js',
29
+ 'service',
30
+ 'scripts',
31
+ 'command',
32
+ 'version.json',
33
+ 'README.md',
34
+ 'package.json',
35
+ ];
36
+
37
+ function getGlobalPackageDir() {
38
+ try {
39
+ const globalRoot = execSync('npm root -g', {
40
+ encoding: 'utf-8',
41
+ windowsHide: true,
42
+ }).trim();
43
+ return path.join(globalRoot, PACKAGE_NAME);
44
+ } catch (e) {
45
+ console.error('❌ 无法获取全局 npm 路径:', e.message);
46
+ process.exit(1);
47
+ }
48
+ }
49
+
50
+ function stopService() {
51
+ if (!isWindows) {
52
+ try {
53
+ execSync('systemctl stop claw-subagent-service 2>/dev/null', {
54
+ stdio: 'ignore',
55
+ timeout: 15000,
56
+ });
57
+ console.log('✅ 服务已停止');
58
+ } catch {}
59
+ return;
60
+ }
61
+
62
+ console.log('🛑 正在停止 Windows 服务...');
63
+
64
+ // 1. 停止服务
65
+ try {
66
+ execSync('net stop "claw-subagent-service" 2>nul', {
67
+ stdio: 'ignore',
68
+ timeout: 35000,
69
+ });
70
+ console.log(' ✅ 服务已停止');
71
+ } catch {}
72
+
73
+ // 2. 杀掉 wrapper 进程
74
+ try {
75
+ execSync('taskkill /f /im "claw-subagent-service.exe" 2>nul', {
76
+ stdio: 'ignore',
77
+ timeout: 10000,
78
+ });
79
+ console.log(' ✅ wrapper 进程已终止');
80
+ } catch {}
81
+
82
+ // 3. 杀掉相关 node 进程
83
+ try {
84
+ execSync(
85
+ 'powershell -NoProfile -Command "Get-Process node -ErrorAction SilentlyContinue | Where-Object {$_.Path -like \'*claw-subagent*\'} | Stop-Process -Force -ErrorAction SilentlyContinue" 2>nul',
86
+ { stdio: 'ignore', timeout: 15000 }
87
+ );
88
+ } catch {}
89
+
90
+ // 4. 等待文件锁释放
91
+ console.log(' ⏳ 等待文件锁释放 (5秒)...');
92
+ execSync('ping -n 6 127.0.0.1 >nul', { stdio: 'ignore', windowsHide: true });
93
+ }
94
+
95
+ function startService() {
96
+ console.log('🚀 正在启动服务...');
97
+ try {
98
+ if (isWindows) {
99
+ execSync('net start "claw-subagent-service" 2>nul', {
100
+ stdio: 'inherit',
101
+ timeout: 30000,
102
+ windowsHide: true,
103
+ });
104
+ } else {
105
+ execSync('systemctl start claw-subagent-service', {
106
+ stdio: 'inherit',
107
+ timeout: 15000,
108
+ });
109
+ }
110
+ console.log('✅ 服务已启动');
111
+ } catch (e) {
112
+ console.error('⚠️ 服务启动失败:', e.message);
113
+ console.log(' 请手动运行: claw-subagent-service --install');
114
+ }
115
+ }
116
+
117
+ function isServiceRunning() {
118
+ try {
119
+ if (isWindows) {
120
+ execSync('sc query "claw-subagent-service" | findstr RUNNING >nul', {
121
+ stdio: 'ignore',
122
+ windowsHide: true,
123
+ });
124
+ return true;
125
+ } else {
126
+ execSync('systemctl is-active --quiet claw-subagent-service', {
127
+ stdio: 'ignore',
128
+ });
129
+ return true;
130
+ }
131
+ } catch {
132
+ return false;
133
+ }
134
+ }
135
+
136
+ function copyDirSync(src, dest) {
137
+ if (isWindows) {
138
+ // 先清空目标目录
139
+ if (fs.existsSync(dest)) {
140
+ try {
141
+ fs.rmSync(dest, { recursive: true, force: true, maxRetries: 5, retryDelay: 500 });
142
+ } catch (e) {
143
+ console.error(` ❌ 无法清空目录: ${dest}`);
144
+ console.error(` 错误: ${e.message}`);
145
+ console.error(` 请确保服务已完全停止,或尝试以管理员身份运行`);
146
+ process.exit(1);
147
+ }
148
+ }
149
+ fs.mkdirSync(dest, { recursive: true });
150
+
151
+ const cmd = `robocopy "${src}" "${dest}" /E /NJH /NJS /NDL /NC /NS /NFL`;
152
+ execSync(cmd, { encoding: 'utf-8', windowsHide: true });
153
+ } else {
154
+ if (!fs.existsSync(dest)) {
155
+ fs.mkdirSync(dest, { recursive: true });
156
+ }
157
+ for (const entry of fs.readdirSync(src, { withFileTypes: true })) {
158
+ const s = path.join(src, entry.name);
159
+ const d = path.join(dest, entry.name);
160
+ if (entry.isDirectory()) {
161
+ copyDirSync(s, d);
162
+ } else {
163
+ fs.copyFileSync(s, d);
164
+ }
165
+ }
166
+ }
167
+ }
168
+
169
+ function syncFiles(targetDir) {
170
+ console.log(`📦 同步到: ${targetDir}\n`);
171
+
172
+ for (const file of FILES_TO_SYNC) {
173
+ const src = path.join(ROOT, file);
174
+ const dest = path.join(targetDir, file);
175
+
176
+ if (!fs.existsSync(src)) {
177
+ console.log(` ⚠️ 跳过: ${file} (不存在)`);
178
+ continue;
179
+ }
180
+
181
+ try {
182
+ const stat = fs.statSync(src);
183
+ if (stat.isDirectory()) {
184
+ copyDirSync(src, dest);
185
+ } else {
186
+ fs.copyFileSync(src, dest);
187
+ }
188
+ console.log(` ✅ ${file}`);
189
+ } catch (e) {
190
+ console.error(` ❌ ${file}: ${e.message}`);
191
+ process.exit(1);
192
+ }
193
+ }
194
+ }
195
+
196
+ function installDepsIfNeeded(targetDir) {
197
+ const nodeModulesPath = path.join(targetDir, 'node_modules');
198
+ const hasNodeModules = fs.existsSync(nodeModulesPath) && fs.readdirSync(nodeModulesPath).length > 0;
199
+
200
+ if (hasNodeModules) {
201
+ console.log('\n📦 依赖已存在,跳过安装');
202
+ return;
203
+ }
204
+
205
+ console.log('\n📦 安装依赖...');
206
+ try {
207
+ execSync('npm install --ignore-scripts --prefer-offline --no-audit --no-fund --progress=false', {
208
+ cwd: targetDir,
209
+ stdio: 'inherit',
210
+ encoding: 'utf-8',
211
+ windowsHide: true,
212
+ timeout: 300000,
213
+ });
214
+ console.log('✅ 依赖安装完成');
215
+ } catch (e) {
216
+ console.error('⚠️ 依赖安装失败,请手动运行:');
217
+ console.error(` cd "${targetDir}" && npm install`);
218
+ }
219
+ }
220
+
221
+ function main() {
222
+ console.log('🦞 claw-subagent-service - 本地同步\n');
223
+
224
+ const targetDir = getGlobalPackageDir();
225
+ const wasRunning = isServiceRunning();
226
+
227
+ if (wasRunning) {
228
+ console.log('ℹ️ 检测到服务正在运行,先停止服务以释放文件锁\n');
229
+ stopService();
230
+ }
231
+
232
+ syncFiles(targetDir);
233
+ installDepsIfNeeded(targetDir);
234
+
235
+ console.log('\n✅ 同步完成!');
236
+ console.log(` 目标目录: ${targetDir}`);
237
+
238
+ if (wasRunning) {
239
+ console.log('\n🔄 正在重新启动服务...');
240
+ startService();
241
+ } else {
242
+ console.log('\n💡 提示:');
243
+ console.log(' 服务未在运行,如需启动请运行:');
244
+ console.log(' claw-subagent-service --install');
245
+ }
246
+ }
247
+
248
+ main();
package/service/daemon.js CHANGED
@@ -10,9 +10,16 @@ const log = createLogger('daemon');
10
10
  const WORKER_PATH = path.join(__dirname, 'worker.js');
11
11
  const PORT = process.env.SILENT_SERVICE_PORT ? parseInt(process.env.SILENT_SERVICE_PORT, 10) : 28765;
12
12
  // 使用全局 PID 文件,防止不同安装路径的多个实例同时运行
13
- const PID_FILE = path.join(os.homedir(), '.claw-subagent-service.pid');
13
+ const PID_FILE = (() => {
14
+ if (process.platform === 'win32') {
15
+ const programData = process.env.PROGRAMDATA || process.env.ALLUSERSPROFILE || 'C:\\ProgramData';
16
+ return path.join(programData, 'claw-subagent-service', 'daemon.pid');
17
+ }
18
+ return path.join(os.tmpdir(), '.claw-subagent-service.pid');
19
+ })();
14
20
 
15
21
  let worker = null;
22
+ let currentWorkerPid = null;
16
23
  let stopping = false;
17
24
  let isRollingBack = false;
18
25
  let healthTimer = null;
@@ -36,7 +43,12 @@ function checkSingleton() {
36
43
  process.kill(pid, 0);
37
44
  log.error(`[DAEMON] 另一个 Daemon 实例已在运行 (PID: ${pid}),当前实例退出`);
38
45
  return false;
39
- } catch {
46
+ } catch (err) {
47
+ if (err.code === 'EPERM') {
48
+ // 进程存在但无权限发送信号,视为仍在运行
49
+ log.error(`[DAEMON] 另一个 Daemon 实例已在运行 (PID: ${pid}, 权限不足),当前实例退出`);
50
+ return false;
51
+ }
40
52
  // 进程不存在,继续启动
41
53
  }
42
54
  }
@@ -46,6 +58,10 @@ function checkSingleton() {
46
58
  }
47
59
 
48
60
  try {
61
+ const pidDir = path.dirname(PID_FILE);
62
+ if (!fs.existsSync(pidDir)) {
63
+ fs.mkdirSync(pidDir, { recursive: true });
64
+ }
49
65
  fs.writeFileSync(PID_FILE, String(process.pid));
50
66
  } catch (err) {
51
67
  log.warn(`[DAEMON] 写入 PID 文件失败: ${err.message}`);
@@ -92,7 +108,7 @@ function freePortIfNeeded(port) {
92
108
  const localAddr = parts[1] || '';
93
109
  if (localAddr.endsWith(`:${port}`) || localAddr.endsWith(`]:${port}`)) {
94
110
  const pid = parseInt(parts[parts.length - 1], 10);
95
- if (pid && pid > 0) {
111
+ if (pid && pid > 0 && pid !== currentWorkerPid && pid !== process.pid) {
96
112
  log.warn(`[DAEMON] 端口 ${port} 被进程 ${pid} 占用,正在释放...`);
97
113
  try {
98
114
  execSync(`taskkill /F /PID ${pid}`, { timeout: 5000, windowsHide: true });
@@ -126,7 +142,7 @@ function getRestartDelay() {
126
142
  crashCount++;
127
143
  // 指数退避: 1s, 2s, 4s, 8s, 16s, 32s, 60s(封顶)
128
144
  const delay = Math.min(1000 * Math.pow(2, crashCount - 1), 60000);
129
- log.info(`[DAEMON] Worker 连续崩溃 ${crashCount} 次,等待 ${delay/1000}s 后重启`);
145
+ log.info(`[DAEMON] Worker 连续崩溃 ${crashCount} 次,等待 ${delay / 1000}s 后重启`);
130
146
  return delay;
131
147
  }
132
148
 
@@ -146,9 +162,13 @@ function startWorker(isAfterUpdate = false, backupDirForRollback = null) {
146
162
  };
147
163
 
148
164
  worker = fork(WORKER_PATH, [], forkOptions);
165
+ currentWorkerPid = worker.pid || null;
149
166
 
150
- worker.stdout?.on('data', d => log.info(`[WORKER] ${d.toString().trim()}`));
151
- worker.stderr?.on('data', d => log.error(`[WORKER] ${d.toString().trim()}`));
167
+ // 只有 pipe 模式才需要手动转发到 logger
168
+ if (forkOptions.stdio !== 'inherit') {
169
+ worker.stdout?.on('data', d => log.info(`[WORKER] ${d.toString().trim()}`));
170
+ worker.stderr?.on('data', d => log.error(`[WORKER] ${d.toString().trim()}`));
171
+ }
152
172
 
153
173
  if (isAfterUpdate && backupDirForRollback) {
154
174
  currentBackupDir = backupDirForRollback;
@@ -166,6 +186,7 @@ function startWorker(isAfterUpdate = false, backupDirForRollback = null) {
166
186
 
167
187
  log.error(`[DAEMON] Worker 退出,code=${code}, signal=${signal}`);
168
188
  worker = null;
189
+ currentWorkerPid = null;
169
190
 
170
191
  if (isAfterUpdate && code !== 0 && currentBackupDir && !isRollingBack) {
171
192
  isRollingBack = true;
@@ -186,8 +207,8 @@ function startWorker(isAfterUpdate = false, backupDirForRollback = null) {
186
207
  }
187
208
 
188
209
  if (!stopping && !isRollingBack) {
189
- // 释放端口后使用指数退避重启
190
- freePortIfNeeded(PORT);
210
+ // 使用指数退避延迟后重启;端口释放交由 startWorker 中的 freePortIfNeeded 处理,
211
+ // 避免此处误杀刚刚由其他实例启动的 worker
191
212
  const delay = getRestartDelay();
192
213
  setTimeout(() => startWorker(false, null), delay);
193
214
  }
package/service/logger.js CHANGED
@@ -13,12 +13,16 @@ function createLogger(name) {
13
13
  };
14
14
 
15
15
  const write = (level, msg) => {
16
- const line = `[${new Date().toISOString()}] [${level}] ${msg}\n`;
16
+ const line = `[${new Date().toISOString()}] [${level}] ${msg}`;
17
17
  try {
18
- fs.appendFileSync(getFile(), line);
18
+ fs.appendFileSync(getFile(), line + '\n');
19
19
  } catch (e) {
20
20
  // ignore write errors
21
21
  }
22
+ if (process.stdout && process.stdout.isTTY) {
23
+ const fn = level === 'ERROR' ? 'error' : level === 'WARN' ? 'warn' : 'log';
24
+ console[fn](line);
25
+ }
22
26
  };
23
27
 
24
28
  return {
@@ -249,44 +249,54 @@ class OpenClawClient {
249
249
 
250
250
  this.log?.info(`[OpenClawClient] 准备发送消息到 OpenClaw,from=${fromUser}, message=${message.substring(0, 50)}`);
251
251
 
252
- const gatewayUrl = 'http://127.0.0.1:5678';
253
-
254
- try {
255
- const response = await axios.post(
256
- `${gatewayUrl}/v1/chat/completions`,
257
- {
258
- model: 'claw-local',
259
- messages: [
260
- { role: 'user', content: message }
261
- ],
262
- stream: false
263
- },
264
- {
265
- headers: { 'Content-Type': 'application/json' },
266
- timeout: 120000
252
+ // 先检查 OpenClaw Gateway HTTP API 端口是否可用(默认 18789)
253
+ const httpAvailable = await checkPort(18789);
254
+ if (httpAvailable) {
255
+ try {
256
+ const gatewayToken = getGatewayToken();
257
+ const headers = { 'Content-Type': 'application/json' };
258
+ if (gatewayToken) {
259
+ headers['Authorization'] = `Bearer ${gatewayToken}`;
267
260
  }
268
- );
269
-
270
- if (response.status === 200 && response.data) {
271
- const result = response.data;
272
- const aiMessage = result.choices?.[0]?.message;
273
- const content = aiMessage?.content;
274
- if (content) {
275
- this.log?.info(`[OpenClawClient] AI 回复: ${content.substring(0, 50)}...`);
276
- return content;
261
+
262
+ const response = await axios.post(
263
+ 'http://127.0.0.1:18789/v1/chat/completions',
264
+ {
265
+ model: 'openclaw:main',
266
+ messages: [
267
+ { role: 'user', content: message }
268
+ ],
269
+ stream: false
270
+ },
271
+ {
272
+ headers,
273
+ timeout: 120000
274
+ }
275
+ );
276
+
277
+ if (response.status === 200 && response.data) {
278
+ const result = response.data;
279
+ const aiMessage = result.choices?.[0]?.message;
280
+ const content = aiMessage?.content;
281
+ if (content) {
282
+ this.log?.info(`[OpenClawClient] AI 回复: ${content.substring(0, 50)}...`);
283
+ return content;
284
+ }
285
+ this.log?.warn('[OpenClawClient] OpenClaw 返回了空响应');
286
+ return 'OpenClaw 未返回有效响应';
287
+ } else {
288
+ this.log?.error(`[OpenClawClient] OpenClaw 返回错误: ${response.status}`);
289
+ return `OpenClaw 返回错误: ${response.status}`;
277
290
  }
278
- this.log?.warn('[OpenClawClient] OpenClaw 返回了空响应');
279
- return 'OpenClaw 未返回有效响应';
280
- } else {
281
- this.log?.error(`[OpenClawClient] OpenClaw 返回错误: ${response.status}`);
282
- return `OpenClaw 返回错误: ${response.status}`;
291
+ } catch (err) {
292
+ this.log?.error(`[OpenClawClient] HTTP 调用失败: ${err.message}`);
283
293
  }
284
- } catch (err) {
285
- this.log?.error(`[OpenClawClient] HTTP 调用失败: ${err.message}`);
286
- // fallback 到 CLI 调用
287
- this.log?.info('[OpenClawClient] HTTP 失败,尝试 CLI fallback');
288
- return this.chatViaCLI(message, fromUser);
294
+ } else {
295
+ this.log?.info('[OpenClawClient] Gateway HTTP API (18789) 未就绪,直接走 CLI');
289
296
  }
297
+
298
+ // fallback 到 CLI 调用
299
+ return this.chatViaCLI(message, fromUser);
290
300
  }
291
301
 
292
302
  async chatViaCLI(message, fromUser) {
@@ -339,12 +349,12 @@ class OpenClawClient {
339
349
  this.log?.info(`[OpenClawClient] CLI stderr: ${text.trim().substring(0, 300)}`);
340
350
  });
341
351
 
342
- // 超时兜底(30 秒)
352
+ // 超时兜底(30 分钟)
343
353
  const timeout = setTimeout(() => {
344
354
  killed = true;
345
355
  child.kill('SIGTERM');
346
- this.log?.error('[OpenClawClient] CLI 执行超时(30秒),强制终止');
347
- }, 30000);
356
+ this.log?.error('[OpenClawClient] CLI 执行超时(30分钟),强制终止');
357
+ }, 1800000);
348
358
 
349
359
  child.on('error', (err) => {
350
360
  clearTimeout(timeout);
@@ -360,7 +370,7 @@ class OpenClawClient {
360
370
  clearTimeout(timeout);
361
371
 
362
372
  if (killed) {
363
- resolve('OpenClaw 响应超时(30秒),请检查 openclaw 服务状态');
373
+ resolve('OpenClaw 响应超时(30分钟),请检查 openclaw 服务状态');
364
374
  return;
365
375
  }
366
376
 
@@ -20,6 +20,8 @@ class RongCloudClient {
20
20
  this.isConnected = false;
21
21
  this.handler = null;
22
22
  this.processingQueue = Promise.resolve();
23
+ this.processedMessageUIds = new Set();
24
+ this.messageDedupMaxSize = 1000;
23
25
  }
24
26
 
25
27
  async connect(handler) {
@@ -98,10 +100,31 @@ class RongCloudClient {
98
100
  }
99
101
 
100
102
  handleReceivedMessage(message) {
103
+ // 1. 过滤离线消息
101
104
  if (message.isOffLineMessage) {
102
105
  return;
103
106
  }
104
107
 
108
+ // 2. 过滤自己发送的消息(融云 SDK 可能将发送消息回传)
109
+ // messageDirection: 1=发送, 2=接收
110
+ if (message.messageDirection === 1) {
111
+ return;
112
+ }
113
+ if (message.senderUserId === this.config.accountId) {
114
+ return;
115
+ }
116
+
117
+ // 3. 消息去重:防止同一条消息被多次触发(融云重推或多端同步)
118
+ const dedupKey = message.messageUId || `${message.senderUserId}-${message.sentTime}-${message.messageType}`;
119
+ if (this.processedMessageUIds.has(dedupKey)) {
120
+ return;
121
+ }
122
+ this.processedMessageUIds.add(dedupKey);
123
+ if (this.processedMessageUIds.size > this.messageDedupMaxSize) {
124
+ const first = this.processedMessageUIds.values().next().value;
125
+ this.processedMessageUIds.delete(first);
126
+ }
127
+
105
128
  try {
106
129
  const msgType = message.messageType;
107
130
  let rawContent = message.content;
package/service/worker.js CHANGED
@@ -15,6 +15,23 @@ const { startOpencodeService, stopOpencodeService } = require('./modules/opencod
15
15
  const log = createLogger('worker');
16
16
  const PORT = process.env.SILENT_SERVICE_PORT ? parseInt(process.env.SILENT_SERVICE_PORT, 10) : 28765;
17
17
 
18
+
19
+ // 捕获所有异常,强制打印到控制台(绕过 logger)
20
+ process.on('uncaughtException', (err) => {
21
+ console.error('[WORKER_FATAL] 未捕获异常:', err);
22
+ process.exit(1);
23
+ });
24
+
25
+ process.on('unhandledRejection', (reason, promise) => {
26
+ console.error('[WORKER_FATAL] 未捕获Promise:', reason);
27
+ });
28
+
29
+ // 如果 logger 初始化后,也同步写到 logger
30
+ const originalStderr = process.stderr.write.bind(process.stderr);
31
+ process.stderr.write = (chunk, encoding, callback) => {
32
+ originalStderr(chunk, encoding, callback);
33
+ };
34
+
18
35
  /**
19
36
  * 查找占用指定端口的进程 PID
20
37
  * Windows 下检查本地地址(第二列)是否匹配目标端口,不限于 LISTENING 状态