claw-subagent-service 0.0.143 → 0.0.145

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.143",
3
+ "version": "0.0.145",
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
 
@@ -111,10 +115,11 @@ async function getOrCreateGatewaySession(fallbackSessionId) {
111
115
  async function ensureOpencodeRunning(log) {
112
116
  try {
113
117
  // 快速检查 4096 端口
114
- await axios.get('http://127.0.0.1:4096/global/health', { timeout: 3000 });
118
+ const healthResponse = await axios.get('http://127.0.0.1:4096/global/health', { timeout: 3000 });
119
+ log('DEBUG', `OpenCode health check: ${JSON.stringify(healthResponse.data)}`);
115
120
  return true;
116
- } catch {
117
- log('WARN', 'OpenCode 服务未运行,准备启动...');
121
+ } catch (e) {
122
+ log('WARN', `OpenCode 服务未运行: ${e.message},准备启动...`);
118
123
 
119
124
  const { exec } = require('child_process');
120
125
 
@@ -126,11 +131,12 @@ async function ensureOpencodeRunning(log) {
126
131
 
127
132
  // 再次检查
128
133
  try {
129
- await axios.get('http://127.0.0.1:4096/global/health', { timeout: 3000 });
134
+ const healthResponse = await axios.get('http://127.0.0.1:4096/global/health', { timeout: 3000 });
135
+ log('DEBUG', `OpenCode health check after start: ${JSON.stringify(healthResponse.data)}`);
130
136
  log('INFO', 'OpenCode 服务已启动');
131
137
  return true;
132
- } catch {
133
- log('ERROR', 'OpenCode 服务启动失败');
138
+ } catch (e) {
139
+ log('ERROR', `OpenCode 服务启动失败: ${e.message}`);
134
140
  return false;
135
141
  }
136
142
  }
@@ -167,6 +173,9 @@ async function forwardChatMessage(sessionId, content, onDelta, logFn, timeoutMs
167
173
  log('DEBUG', `请求体包含 system 参数: ${!!requestBody.system}`);
168
174
 
169
175
  try {
176
+ log('DEBUG', `发送请求到: ${url}`);
177
+ log('DEBUG', `请求体: ${JSON.stringify(requestBody).substring(0, 500)}`);
178
+
170
179
  const response = await axios.post(url, requestBody, {
171
180
  headers: { 'Content-Type': 'application/json' },
172
181
  timeout: timeoutMs
@@ -174,15 +183,17 @@ async function forwardChatMessage(sessionId, content, onDelta, logFn, timeoutMs
174
183
 
175
184
  const result = response.data || {};
176
185
  log('DEBUG', `响应状态: ${response.status}`);
186
+ log('DEBUG', `响应数据类型: ${typeof result}`);
177
187
  log('DEBUG', `响应数据 keys: ${Object.keys(result).join(', ')}`);
178
- log('DEBUG', `响应数据: ${JSON.stringify(result).substring(0, 500)}`);
188
+ log('DEBUG', `响应数据: ${JSON.stringify(result).substring(0, 1000)}`);
189
+ log('DEBUG', `响应 headers: ${JSON.stringify(response.headers)}`);
179
190
 
180
191
  const parts = result.parts || [];
181
192
  const info = result.info || {};
182
193
 
183
194
  let fullContent = '';
184
195
 
185
- for (const key of ['text', 'content', 'response', 'output', 'message', 'reply']) {
196
+ for (const key of ['text', 'content', 'response', 'output', 'message', 'reply', 'answer']) {
186
197
  const val = result[key];
187
198
  if (val && typeof val === 'string') {
188
199
  fullContent = val;
@@ -221,8 +232,9 @@ async function forwardChatMessage(sessionId, content, onDelta, logFn, timeoutMs
221
232
  }
222
233
 
223
234
  if (!fullContent) {
224
- log('ERROR', `Gateway 返回空内容, parts 数量: ${parts.length}`);
225
- throw new Error('Gateway 返回空内容');
235
+ // 如果返回空内容,可能是异步处理,返回一个提示信息
236
+ log('WARN', `Gateway 返回空内容, parts 数量: ${parts.length}, 返回默认响应`);
237
+ fullContent = '消息已发送,正在处理中...';
226
238
  }
227
239
 
228
240
  log('DEBUG', `总内容长度: ${fullContent.length}, 开始模拟流式发送`);