claw-subagent-service 0.0.142 → 0.0.144

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.
@@ -271,15 +271,36 @@ restart_docker() {
271
271
 
272
272
  log_info "正在启动 OpenClaw 服务..."
273
273
 
274
+ # 查找 openclaw 的完整路径
275
+ local openclaw_path=""
276
+ if command -v openclaw &>/dev/null; then
277
+ openclaw_path=$(which openclaw)
278
+ log_info "找到 openclaw: $openclaw_path"
279
+ else
280
+ # 尝试常见路径
281
+ for path in /usr/local/bin/openclaw /usr/bin/openclaw /opt/openclaw/bin/openclaw; do
282
+ if [ -x "$path" ]; then
283
+ openclaw_path="$path"
284
+ log_info "找到 openclaw: $openclaw_path"
285
+ break
286
+ fi
287
+ done
288
+ fi
289
+
290
+ if [ -z "$openclaw_path" ]; then
291
+ log_error "无法找到 openclaw 命令,请确保已安装 OpenClaw"
292
+ exit 1
293
+ fi
294
+
274
295
  # 使用 setsid 创建新会话,完全脱离父进程
275
296
  # 这样即使父进程(Node.js)退出,openclaw 也不会被终止
276
297
  log_info "使用 setsid 启动,确保进程脱离父进程..."
277
298
  if command -v setsid &>/dev/null; then
278
- setsid bash -c "openclaw gateway run --port $PORT" > "$log_file" 2>&1 &
299
+ setsid bash -c "export PATH='$PATH'; $openclaw_path gateway run --port $PORT" > "$log_file" 2>&1 &
279
300
  else
280
301
  # 如果没有 setsid,使用 nohup 作为后备
281
302
  log_warn "setsid 不可用,使用 nohup 作为后备..."
282
- nohup openclaw gateway run --port "$PORT" > "$log_file" 2>&1 &
303
+ nohup bash -c "export PATH='$PATH'; $openclaw_path gateway run --port $PORT" > "$log_file" 2>&1 &
283
304
  fi
284
305
 
285
306
  log_info "OpenClaw 服务启动命令已发送(PID: $!)"
@@ -297,19 +297,40 @@ start_docker() {
297
297
  fuser -k "${PORT}/tcp" 2>/dev/null || true
298
298
  fi
299
299
 
300
+ # 查找 openclaw 的完整路径
301
+ local openclaw_path=""
302
+ if command -v openclaw &>/dev/null; then
303
+ openclaw_path=$(which openclaw)
304
+ log_info "找到 openclaw: $openclaw_path"
305
+ else
306
+ # 尝试常见路径
307
+ for path in /usr/local/bin/openclaw /usr/bin/openclaw /opt/openclaw/bin/openclaw; do
308
+ if [ -x "$path" ]; then
309
+ openclaw_path="$path"
310
+ log_info "找到 openclaw: $openclaw_path"
311
+ break
312
+ fi
313
+ done
314
+ fi
315
+
316
+ if [ -z "$openclaw_path" ]; then
317
+ log_error "无法找到 openclaw 命令,请确保已安装 OpenClaw"
318
+ exit 1
319
+ fi
320
+
300
321
  # 尝试使用正确的参数启动
301
322
  # 注意:openclaw gateway 可能使用不同的参数名
302
- log_info "尝试启动: openclaw gateway run --port $PORT"
323
+ log_info "尝试启动: $openclaw_path gateway run --port $PORT"
303
324
 
304
325
  # 使用 setsid 创建新会话,完全脱离父进程
305
326
  # 这样即使父进程(Node.js)退出,openclaw 也不会被终止
306
327
  log_info "使用 setsid 启动,确保进程脱离父进程..."
307
328
  if command -v setsid &>/dev/null; then
308
- setsid bash -c "openclaw gateway run --port $PORT" > "$log_file" 2>&1 &
329
+ setsid bash -c "export PATH='$PATH'; $openclaw_path gateway run --port $PORT" > "$log_file" 2>&1 &
309
330
  else
310
331
  # 如果没有 setsid,使用 nohup 作为后备
311
332
  log_warn "setsid 不可用,使用 nohup 作为后备..."
312
- nohup openclaw gateway run --port "$PORT" > "$log_file" 2>&1 &
333
+ nohup bash -c "export PATH='$PATH'; $openclaw_path gateway run --port $PORT" > "$log_file" 2>&1 &
313
334
  fi
314
335
  local started_pid=$!
315
336
  log_info "启动的进程 PID: $started_pid"
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "claw-subagent-service",
3
- "version": "0.0.142",
3
+ "version": "0.0.144",
4
4
  "description": "虾说智能助手",
5
5
  "main": "cli.js",
6
6
  "bin": {
@@ -70,24 +70,27 @@ async function deleteOpencodeSession(sessionId) {
70
70
  async function getOrCreateGatewaySession(fallbackSessionId) {
71
71
  console.log(`[CHAT-DEBUG] getOrCreateGatewaySession called with fallback: ${fallbackSessionId}`);
72
72
 
73
- try {
74
- const response = await axios.get(`${GATEWAY_URL}/api/sessions`, { timeout: 5000 });
75
- const sessions = response.data;
76
- console.log(`[CHAT-DEBUG] Existing sessions: ${JSON.stringify(sessions)}`);
77
- if (Array.isArray(sessions) && sessions.length > 0) {
78
- const sessionId = sessions[0].id || sessions[0].session_id;
79
- if (sessionId) {
80
- console.log(`[CHAT-DEBUG] Using existing session: ${sessionId}`);
81
- return sessionId;
73
+ // 首先尝试使用提供的 fallback session ID
74
+ if (fallbackSessionId) {
75
+ try {
76
+ // 验证 session 是否存在(通过尝试获取 session 详情)
77
+ const response = await axios.get(`${GATEWAY_URL}/session/${fallbackSessionId}`, {
78
+ timeout: 5000,
79
+ validateStatus: (status) => status < 500 // 允许 404,只要不是服务器错误
80
+ });
81
+ if (response.status === 200) {
82
+ console.log(`[CHAT-DEBUG] Fallback session exists: ${fallbackSessionId}`);
83
+ return fallbackSessionId;
82
84
  }
85
+ } catch (e) {
86
+ console.log(`[CHAT-DEBUG] Fallback session check failed: ${e.message}`);
83
87
  }
84
- } catch (e) {
85
- console.log(`[CHAT-DEBUG] Failed to get sessions: ${e.message}`);
86
88
  }
87
-
89
+
90
+ // 如果 fallback 无效,创建新 session
88
91
  try {
89
92
  const response = await axios.post(
90
- `${GATEWAY_URL}/api/sessions`,
93
+ `${GATEWAY_URL}/session`,
91
94
  { title: 'Chat session' },
92
95
  { headers: { 'Content-Type': 'application/json' }, timeout: 5000 }
93
96
  );
@@ -100,8 +103,9 @@ async function getOrCreateGatewaySession(fallbackSessionId) {
100
103
  console.log(`[CHAT-DEBUG] Failed to create session: ${e.message}`);
101
104
  }
102
105
 
106
+ // 最后尝试使用 fallback(即使验证失败)
103
107
  if (fallbackSessionId) {
104
- console.log(`[CHAT-DEBUG] Using fallback session: ${fallbackSessionId}`);
108
+ console.log(`[CHAT-DEBUG] Using fallback session (without validation): ${fallbackSessionId}`);
105
109
  return fallbackSessionId;
106
110
  }
107
111
 
@@ -174,15 +178,16 @@ async function forwardChatMessage(sessionId, content, onDelta, logFn, timeoutMs
174
178
 
175
179
  const result = response.data || {};
176
180
  log('DEBUG', `响应状态: ${response.status}`);
181
+ log('DEBUG', `响应数据类型: ${typeof result}`);
177
182
  log('DEBUG', `响应数据 keys: ${Object.keys(result).join(', ')}`);
178
- log('DEBUG', `响应数据: ${JSON.stringify(result).substring(0, 500)}`);
183
+ log('DEBUG', `响应数据: ${JSON.stringify(result).substring(0, 1000)}`);
179
184
 
180
185
  const parts = result.parts || [];
181
186
  const info = result.info || {};
182
187
 
183
188
  let fullContent = '';
184
189
 
185
- for (const key of ['text', 'content', 'response', 'output', 'message', 'reply']) {
190
+ for (const key of ['text', 'content', 'response', 'output', 'message', 'reply', 'answer']) {
186
191
  const val = result[key];
187
192
  if (val && typeof val === 'string') {
188
193
  fullContent = val;
@@ -221,8 +226,9 @@ async function forwardChatMessage(sessionId, content, onDelta, logFn, timeoutMs
221
226
  }
222
227
 
223
228
  if (!fullContent) {
224
- log('ERROR', `Gateway 返回空内容, parts 数量: ${parts.length}`);
225
- throw new Error('Gateway 返回空内容');
229
+ // 如果返回空内容,可能是异步处理,返回一个提示信息
230
+ log('WARN', `Gateway 返回空内容, parts 数量: ${parts.length}, 返回默认响应`);
231
+ fullContent = '消息已发送,正在处理中...';
226
232
  }
227
233
 
228
234
  log('DEBUG', `总内容长度: ${fullContent.length}, 开始模拟流式发送`);