index.js
13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/**
* Hermes Chat Server
*
* Express + SSE server that wraps Claude CLI (via ClaudeCliProtocol from aipcrm)
* and exposes Agent Phone tools to Hermes.
*
* POST /api/hermes/chat — SSE stream, same event schema as aipcrm master-chat
* GET /api/hermes/status — session list + device reachability
* DEL /api/hermes/sessions/:id
*/
const express = require('express');
const cors = require('cors');
const path = require('path');
const fs = require('fs');
const { randomUUID } = require('crypto');
const { EventEmitter } = require('events');
const app = express();
app.use(cors());
app.use(express.json({ limit: '4mb' }));
app.use(express.static(path.join(__dirname, '../public')));
// ─── Paths ───────────────────────────────────────────────────────────────────
// Reuse aipcrm's battle-tested ClaudeCliProtocol directly
const AIPCRM = path.resolve('/Users/yibo/Documents/aipcrm');
const { ClaudeCliProtocol } = require(path.join(AIPCRM, 'packages/acp-core/protocol/ClaudeCliProtocol'));
const { createPhoneTools } = require('./tools/phone-tools');
const PORT = process.env.PORT || 3020;
const WORK_DIR = path.resolve(__dirname, '..');
// ─── Tool Registry (self-contained copy) ─────────────────────────────────────
class ToolRegistry {
constructor() { this.tools = new Map(); }
register(tool) { this.tools.set(tool.name, tool); }
registerAll(tools) { tools.forEach(t => this.register(t)); }
async execute(name, params) {
const tool = this.tools.get(name);
if (!tool) return { error: `Unknown tool: ${name}`, status: 'error' };
try {
const result = await tool.execute(params);
return { result, status: 'success' };
} catch (err) {
return { error: err.message, status: 'error' };
}
}
generateToolPrompt() {
const schemas = Array.from(this.tools.values()).map(t => ({
name: t.name, description: t.description, parameters: t.parameters || {},
}));
if (!schemas.length) return '';
const descs = schemas.map(s => {
const props = Object.entries(s.parameters.properties || {}).map(([k, v]) => {
const req = (s.parameters.required || []).includes(k);
return ` - ${k} (${v.type}${req ? ', 必需' : ', 可选'}): ${v.description || ''}`;
}).join('\n');
return `### ${s.name}\n${s.description}\n\n**参数:**\n${props || ' (无参数)'}`;
}).join('\n\n---\n\n');
return `## 可用工具\n\n通过以下格式调用工具:\n\n\`\`\`\n[TOOL_CALL: tool_name]\n{\n "param1": "value1"\n}\n[/TOOL_CALL]\n\`\`\`\n\n${descs}`;
}
static parseToolCalls(output) {
const regex = /\[TOOL_CALL:\s*([^\]]+)\]\s*([\s\S]*?)\[\/TOOL_CALL\]/g;
const calls = [];
let m;
while ((m = regex.exec(output)) !== null) {
const toolName = m[1].trim();
let params = {};
try { params = JSON.parse(m[2].trim()); } catch { params = { task: m[2].trim() }; }
calls.push({ tool: toolName, params });
}
return calls;
}
}
// ─── Hermes Session ───────────────────────────────────────────────────────────
const SKILL_MD = fs.existsSync(path.join(__dirname, 'skill.md'))
? fs.readFileSync(path.join(__dirname, 'skill.md'), 'utf-8') : '';
class HermesSession extends EventEmitter {
constructor() {
super();
this.protocol = null;
this.toolRegistry = new ToolRegistry();
this._initialized = false;
this._busy = false;
this._queue = [];
this._messageCount = 0;
}
async init() {
if (this._initialized) return;
this.toolRegistry.registerAll(createPhoneTools());
this.protocol = new ClaudeCliProtocol(
{ name: 'hermes' },
{ systemPrompt: SKILL_MD, effort: process.env.CLAUDE_EFFORT || 'medium' }
);
await this.protocol.connect(null, WORK_DIR);
await this.protocol.newSession();
this._initialized = true;
console.log('[HermesSession] 初始化完成');
}
async handleMessage(message, options = {}) {
if (this._busy) {
return new Promise(resolve => this._queue.push({ message, options, resolve }));
}
return this._execute(message, options);
}
async _execute(message, options = {}) {
if (!this._initialized) await this.init();
this._busy = true;
const { onData, onToolCall, maxToolRounds = 8 } = options;
try {
let prompt;
if (this._messageCount === 0) {
const toolPrompt = this.toolRegistry.generateToolPrompt();
prompt = `${toolPrompt}\n\n---\n\n## 用户消息\n\n${message}\n\n---\n\n**行为约束:** 需要调用工具时直接输出 [TOOL_CALL:] 块,不要先解释。`;
} else {
prompt = `## 用户后续消息\n\n${message}`;
}
this._messageCount++;
let allOutput = '';
let toolCallHistory = [];
for (let round = 0; round < maxToolRounds; round++) {
// Collect streaming output for this round
let roundOutput = '';
const textChunks = [];
await new Promise((resolve, reject) => {
const onUpdate = ({ update }) => {
if (update?.sessionUpdate === 'agent_message_chunk' && update.content?.text) {
const chunk = update.content.text;
textChunks.push(chunk);
roundOutput += chunk;
if (onData) onData(chunk);
}
};
const onEvent = ({ method, params }) => {
if (method === 'session/result') {
this.protocol.off('session_update', onUpdate);
this.protocol.off('acp_event', onEvent);
resolve(params);
}
};
const onErr = (err) => {
this.protocol.off('session_update', onUpdate);
this.protocol.off('acp_event', onEvent);
reject(err);
};
this.protocol.once('error', onErr);
this.protocol.on('session_update', onUpdate);
this.protocol.on('acp_event', onEvent);
this.protocol.sendPrompt(prompt).catch(onErr);
});
allOutput += roundOutput + '\n';
const toolCalls = ToolRegistry.parseToolCalls(roundOutput);
if (!toolCalls.length) {
return { type: 'reply', output: roundOutput, toolCalls: toolCallHistory, rounds: round + 1 };
}
// Execute tools and build result prompt
const toolResults = [];
for (const tc of toolCalls) {
if (onToolCall) onToolCall(tc);
const result = await this.toolRegistry.execute(tc.tool, tc.params);
toolResults.push({ ...tc, result });
toolCallHistory.push({ ...tc, result });
}
const resultBlocks = toolResults.map(tr => {
const r = typeof tr.result === 'object' ? JSON.stringify(tr.result, null, 2) : String(tr.result);
return `### 工具 ${tr.tool} 结果\n\`\`\`json\n${r}\n\`\`\``;
}).join('\n\n');
prompt = `## 工具执行结果\n\n${resultBlocks}\n\n---\n\n请根据结果决定下一步:还有后续步骤则继续调用工具;全部完成则给用户最终回复。`;
}
return { type: 'max_rounds', output: allOutput, toolCalls: toolCallHistory, rounds: maxToolRounds };
} finally {
this._busy = false;
this._drainQueue();
}
}
_drainQueue() {
if (!this._queue.length || this._busy) return;
const next = this._queue.shift();
this._execute(next.message, next.options).then(next.resolve).catch(e => {
next.resolve({ type: 'error', output: e.message, toolCalls: [], rounds: 0 });
});
}
async shutdown() {
this._queue = [];
if (this.protocol) this.protocol.disconnect();
}
}
// ─── Session pool ─────────────────────────────────────────────────────────────
const sessions = new Map();
const SESSION_IDLE_MS = 15 * 60 * 1000;
function getOrCreateSession(sessionId) {
let entry = sessions.get(sessionId);
if (entry) {
entry.lastActive = Date.now();
clearTimeout(entry.timer);
entry.timer = null;
return entry.session;
}
const session = new HermesSession();
entry = { session, lastActive: Date.now(), timer: null };
sessions.set(sessionId, entry);
console.log(`[HermesChat] 新建 session: ${sessionId}`);
return session;
}
function resetTimer(sessionId) {
const entry = sessions.get(sessionId);
if (!entry) return;
clearTimeout(entry.timer);
entry.timer = setTimeout(async () => {
console.log(`[HermesChat] Session 超时: ${sessionId}`);
await entry.session.shutdown().catch(() => {});
sessions.delete(sessionId);
}, SESSION_IDLE_MS);
}
// ─── SSE helper ───────────────────────────────────────────────────────────────
function setupSSE(res) {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
});
return (event, data) => res.write(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`);
}
// ─── Routes ───────────────────────────────────────────────────────────────────
app.post('/api/hermes/chat', async (req, res) => {
const { message, sessionId: reqId } = req.body;
if (!message) return res.status(400).json({ error: 'message is required' });
const sessionId = reqId || `hermes-${Date.now()}-${randomUUID().slice(0, 6)}`;
const sendSSE = setupSSE(res);
const session = getOrCreateSession(sessionId);
sendSSE('session', { sessionId });
try {
sendSSE('progress', { step: 'initializing', message: '🤖 Hermes 启动中…' });
const result = await session.handleMessage(message, {
onData: (chunk) => {
if (chunk?.trim()) sendSSE('agent_output', { agent: 'hermes', message: chunk, streamType: 'agent_output' });
},
onToolCall: (tc) => {
sendSSE('tool_call', { tool: tc.tool, params: tc.params, step: tc.tool, message: `调用: ${tc.tool}` });
},
});
sendSSE('result', {
sessionId,
type: result.type,
output: result.output,
toolCalls: (result.toolCalls || []).map(tc => ({ tool: tc.tool, params: tc.params, status: tc.result?.status })),
rounds: result.rounds,
});
} catch (err) {
console.error('[HermesChat] 错误:', err);
sendSSE('error', { message: err.message });
} finally {
resetTimer(sessionId);
res.end();
}
});
app.get('/api/hermes/sessions', (req, res) => {
const list = [];
for (const [id, e] of sessions) {
list.push({ sessionId: id, lastActive: new Date(e.lastActive).toISOString(), age: Date.now() - e.lastActive });
}
res.json(list);
});
app.delete('/api/hermes/sessions/:id', async (req, res) => {
const entry = sessions.get(req.params.id);
if (!entry) return res.status(404).json({ error: 'Session not found' });
clearTimeout(entry.timer);
await entry.session.shutdown().catch(() => {});
sessions.delete(req.params.id);
res.json({ success: true });
});
app.get('/api/hermes/status', (req, res) => {
res.json({
sessions: sessions.size,
phoneUrl: process.env.PHONE_MCP_URL || 'http://localhost:8766',
eventUrl: process.env.PHONE_EVENT_URL || 'http://localhost:8765',
});
});
// ─── Start ────────────────────────────────────────────────────────────────────
app.listen(PORT, () => {
console.log(`\n🤖 Hermes Chat 已启动`);
console.log(` UI: http://localhost:${PORT}`);
console.log(` API: http://localhost:${PORT}/api/hermes/chat`);
console.log(` Phone: ${process.env.PHONE_MCP_URL || 'http://localhost:8766 (adb forward)'}`);
console.log(` Events: ${process.env.PHONE_EVENT_URL || 'http://localhost:8765 (adb forward)'}\n`);
});