claw-subagent-service 0.0.25 → 0.0.26
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
|
@@ -3,6 +3,7 @@ const net = require('net');
|
|
|
3
3
|
const os = require('os');
|
|
4
4
|
const fs = require('fs');
|
|
5
5
|
const path = require('path');
|
|
6
|
+
const axios = require('axios');
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* 获取实际用户主目录(SYSTEM 账户下 os.homedir() 返回 systemprofile)
|
|
@@ -159,6 +160,47 @@ class OpenClawClient {
|
|
|
159
160
|
|
|
160
161
|
this.log?.info(`[OpenClawClient] 准备发送消息到 OpenClaw,from=${fromUser}, message=${message.substring(0, 50)}`);
|
|
161
162
|
|
|
163
|
+
const gatewayUrl = 'http://127.0.0.1:5678';
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
const response = await axios.post(
|
|
167
|
+
`${gatewayUrl}/v1/chat/completions`,
|
|
168
|
+
{
|
|
169
|
+
model: 'claw-local',
|
|
170
|
+
messages: [
|
|
171
|
+
{ role: 'user', content: message }
|
|
172
|
+
],
|
|
173
|
+
stream: false
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
headers: { 'Content-Type': 'application/json' },
|
|
177
|
+
timeout: 120000
|
|
178
|
+
}
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
if (response.status === 200 && response.data) {
|
|
182
|
+
const result = response.data;
|
|
183
|
+
const aiMessage = result.choices?.[0]?.message;
|
|
184
|
+
const content = aiMessage?.content;
|
|
185
|
+
if (content) {
|
|
186
|
+
this.log?.info(`[OpenClawClient] AI 回复: ${content.substring(0, 50)}...`);
|
|
187
|
+
return content;
|
|
188
|
+
}
|
|
189
|
+
this.log?.warn('[OpenClawClient] OpenClaw 返回了空响应');
|
|
190
|
+
return 'OpenClaw 未返回有效响应';
|
|
191
|
+
} else {
|
|
192
|
+
this.log?.error(`[OpenClawClient] OpenClaw 返回错误: ${response.status}`);
|
|
193
|
+
return `OpenClaw 返回错误: ${response.status}`;
|
|
194
|
+
}
|
|
195
|
+
} catch (err) {
|
|
196
|
+
this.log?.error(`[OpenClawClient] HTTP 调用失败: ${err.message}`);
|
|
197
|
+
// fallback 到 CLI 调用
|
|
198
|
+
this.log?.info('[OpenClawClient] HTTP 失败,尝试 CLI fallback');
|
|
199
|
+
return this.chatViaCLI(message, fromUser);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async chatViaCLI(message, fromUser) {
|
|
162
204
|
const sessionId = `clawmessenger-${fromUser}`;
|
|
163
205
|
const escapedMessage = message
|
|
164
206
|
.replace(/\\/g, '\\\\')
|
|
@@ -190,7 +232,6 @@ class OpenClawClient {
|
|
|
190
232
|
},
|
|
191
233
|
});
|
|
192
234
|
|
|
193
|
-
// 流式收集输出,无 maxBuffer 限制
|
|
194
235
|
child.stdout.on('data', (chunk) => {
|
|
195
236
|
stdout += chunk.toString();
|
|
196
237
|
});
|
|
@@ -198,20 +239,19 @@ class OpenClawClient {
|
|
|
198
239
|
stderr += chunk.toString();
|
|
199
240
|
});
|
|
200
241
|
|
|
201
|
-
// 超时兜底(20 分钟)
|
|
202
242
|
const timeout = setTimeout(() => {
|
|
203
243
|
killed = true;
|
|
204
244
|
child.kill('SIGTERM');
|
|
205
|
-
this.log?.error('[OpenClawClient] 执行超时,强制终止');
|
|
245
|
+
this.log?.error('[OpenClawClient] CLI 执行超时,强制终止');
|
|
206
246
|
}, 1200000);
|
|
207
247
|
|
|
208
248
|
child.on('error', (err) => {
|
|
209
249
|
clearTimeout(timeout);
|
|
210
|
-
this.log?.error(`[OpenClawClient] 子进程错误: ${err.message}`);
|
|
250
|
+
this.log?.error(`[OpenClawClient] CLI 子进程错误: ${err.message}`);
|
|
211
251
|
if (err.code === 'ENOENT') {
|
|
212
252
|
resolve('找不到 openclaw 命令');
|
|
213
253
|
} else {
|
|
214
|
-
resolve(`OpenClaw 调用失败: ${err.message}`);
|
|
254
|
+
resolve(`OpenClaw CLI 调用失败: ${err.message}`);
|
|
215
255
|
}
|
|
216
256
|
});
|
|
217
257
|
|
|
@@ -219,17 +259,17 @@ class OpenClawClient {
|
|
|
219
259
|
clearTimeout(timeout);
|
|
220
260
|
|
|
221
261
|
if (killed) {
|
|
222
|
-
resolve('OpenClaw 响应超时');
|
|
262
|
+
resolve('OpenClaw CLI 响应超时');
|
|
223
263
|
return;
|
|
224
264
|
}
|
|
225
265
|
|
|
226
|
-
this.log?.info(`[OpenClawClient] 进程退出 code=${code}`);
|
|
266
|
+
this.log?.info(`[OpenClawClient] CLI 进程退出 code=${code}`);
|
|
227
267
|
this.log?.info(`[OpenClawClient] stdout 长度: ${stdout.length}, stderr 长度: ${stderr.length}`);
|
|
228
268
|
|
|
229
269
|
if (code !== 0) {
|
|
230
270
|
const errOutput = stderr || stdout || '';
|
|
231
|
-
this.log?.error(`[OpenClawClient] 错误输出: ${errOutput.substring(0, 500)}`);
|
|
232
|
-
resolve(`OpenClaw 调用失败: ${errOutput.substring(0, 200)}`);
|
|
271
|
+
this.log?.error(`[OpenClawClient] CLI 错误输出: ${errOutput.substring(0, 500)}`);
|
|
272
|
+
resolve(`OpenClaw CLI 调用失败: ${errOutput.substring(0, 200)}`);
|
|
233
273
|
return;
|
|
234
274
|
}
|
|
235
275
|
|