@wendongfly/myhi 1.0.76 → 1.0.78
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/dist/attach.js +109 -26
- package/dist/chat.html +3 -2
- package/package.json +1 -1
package/dist/attach.js
CHANGED
|
@@ -28,40 +28,122 @@ function attach(socket, sessionId) {
|
|
|
28
28
|
socket.emit('join', sessionId);
|
|
29
29
|
|
|
30
30
|
socket.on('joined', (session) => {
|
|
31
|
-
|
|
31
|
+
const isAgent = session.mode === 'agent';
|
|
32
|
+
process.stderr.write(`\r\n[myhi] 已附加到 "${session.title}" (${sessionId}) 模式=${isAgent ? 'agent' : 'pty'}\r\n`);
|
|
32
33
|
process.stderr.write('[myhi] 按 Ctrl+] 分离\r\n\r\n');
|
|
33
34
|
|
|
34
35
|
// 本地终端自动获取控制权
|
|
35
36
|
socket.emit('take-control', { sessionId });
|
|
36
37
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
if (isAgent) {
|
|
39
|
+
// ── Agent 模式:显示格式化输出 ──
|
|
40
|
+
let _streamText = '';
|
|
40
41
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
42
|
+
socket.on('agent:message', (msg) => {
|
|
43
|
+
if (!msg) return;
|
|
44
|
+
switch (msg.type) {
|
|
45
|
+
case 'system':
|
|
46
|
+
if (msg.subtype === 'init') process.stderr.write('\x1b[90m[会话已连接]\x1b[0m\r\n');
|
|
47
|
+
else if (msg.subtype === 'interrupted') process.stderr.write('\r\n\x1b[33m[已中断]\x1b[0m\r\n');
|
|
48
|
+
break;
|
|
49
|
+
case 'assistant':
|
|
50
|
+
if (msg.message?.content) {
|
|
51
|
+
for (const block of msg.message.content) {
|
|
52
|
+
if (block.type === 'text') process.stdout.write(block.text + '\r\n');
|
|
53
|
+
else if (block.type === 'tool_use') {
|
|
54
|
+
process.stdout.write(`\r\n\x1b[36m[工具] ${block.name}\x1b[0m\r\n`);
|
|
55
|
+
if (block.input) {
|
|
56
|
+
const s = typeof block.input === 'string' ? block.input : JSON.stringify(block.input, null, 2);
|
|
57
|
+
process.stdout.write('\x1b[90m' + s.slice(0, 500) + '\x1b[0m\r\n');
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
case 'content_block_delta':
|
|
64
|
+
if (msg.delta?.text) { process.stdout.write(msg.delta.text); _streamText += msg.delta.text; }
|
|
65
|
+
break;
|
|
66
|
+
case 'content_block_stop':
|
|
67
|
+
if (_streamText) { process.stdout.write('\r\n'); _streamText = ''; }
|
|
68
|
+
break;
|
|
69
|
+
case 'tool_use':
|
|
70
|
+
process.stdout.write(`\r\n\x1b[36m[工具] ${msg.tool_name || msg.name || '?'}\x1b[0m\r\n`);
|
|
71
|
+
break;
|
|
72
|
+
case 'tool_result':
|
|
73
|
+
if (msg.content) {
|
|
74
|
+
const text = typeof msg.content === 'string' ? msg.content
|
|
75
|
+
: Array.isArray(msg.content) ? msg.content.map(b => b.text || '').join('') : '';
|
|
76
|
+
if (text) process.stdout.write('\x1b[90m' + text.slice(0, 1000) + '\x1b[0m\r\n');
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
case 'result':
|
|
80
|
+
process.stdout.write(`\r\n\x1b[32m[完成]\x1b[0m ${msg.duration_ms ? (msg.duration_ms/1000).toFixed(1)+'s' : ''} ${msg.total_cost_usd ? '$'+msg.total_cost_usd.toFixed(4) : ''}\r\n\r\n`);
|
|
81
|
+
break;
|
|
82
|
+
case 'control_request':
|
|
83
|
+
process.stdout.write(`\r\n\x1b[33m[权限请求] ${msg.request?.tool_name || '?'}\x1b[0m\r\n`);
|
|
84
|
+
break;
|
|
85
|
+
}
|
|
86
|
+
});
|
|
51
87
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
88
|
+
socket.on('agent:history', (history) => {
|
|
89
|
+
for (const msg of history) {
|
|
90
|
+
if (msg.type === 'user') process.stdout.write(`\x1b[34m> ${msg.content}\x1b[0m\r\n`);
|
|
91
|
+
else if (msg.type === 'result') process.stdout.write(`\x1b[32m[完成]\x1b[0m\r\n`);
|
|
92
|
+
}
|
|
93
|
+
process.stdout.write('\r\n');
|
|
94
|
+
});
|
|
56
95
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
process.stdout.on('resize', () => {
|
|
60
|
-
socket.emit('resize', {
|
|
61
|
-
cols: process.stdout.columns || 80,
|
|
62
|
-
rows: process.stdout.rows || 24,
|
|
96
|
+
socket.on('agent:busy', (busy) => {
|
|
97
|
+
if (busy) process.stderr.write('\x1b[90m[思考中...]\x1b[0m\r\n');
|
|
63
98
|
});
|
|
64
|
-
|
|
99
|
+
|
|
100
|
+
socket.on('agent:error', (err) => {
|
|
101
|
+
process.stderr.write(`\x1b[31m[错误] ${err.message}\x1b[0m\r\n`);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// Agent 模式用行编辑输入
|
|
105
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout, prompt: '\x1b[34m> \x1b[0m' });
|
|
106
|
+
rl.prompt();
|
|
107
|
+
rl.on('line', (line) => {
|
|
108
|
+
const text = line.trim();
|
|
109
|
+
if (!text) { rl.prompt(); return; }
|
|
110
|
+
if (text === '/quit' || text === '/exit') {
|
|
111
|
+
process.stderr.write('[myhi] 已分离\r\n');
|
|
112
|
+
cleanup(socket);
|
|
113
|
+
process.exit(0);
|
|
114
|
+
}
|
|
115
|
+
socket.emit('agent:query', { prompt: text });
|
|
116
|
+
// result 消息到达后再显示 prompt
|
|
117
|
+
socket.once('agent:busy', (busy) => { if (!busy) rl.prompt(); });
|
|
118
|
+
});
|
|
119
|
+
rl.on('close', () => { cleanup(socket); process.exit(0); });
|
|
120
|
+
|
|
121
|
+
} else {
|
|
122
|
+
// ── PTY 模式:原始终端 ──
|
|
123
|
+
try { process.stdin.setRawMode(true); } catch {}
|
|
124
|
+
process.stdin.resume();
|
|
125
|
+
process.stdin.setEncoding('binary');
|
|
126
|
+
|
|
127
|
+
process.stdin.on('data', (data) => {
|
|
128
|
+
if (data === '\x1d') {
|
|
129
|
+
process.stderr.write('\r\n[myhi] 已分离\r\n');
|
|
130
|
+
cleanup(socket);
|
|
131
|
+
process.exit(0);
|
|
132
|
+
}
|
|
133
|
+
socket.emit('input', data);
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
socket.on('output', (data) => {
|
|
137
|
+
process.stdout.write(data, 'binary');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
process.stdout.on('resize', () => {
|
|
141
|
+
socket.emit('resize', {
|
|
142
|
+
cols: process.stdout.columns || 80,
|
|
143
|
+
rows: process.stdout.rows || 24,
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
}
|
|
65
147
|
});
|
|
66
148
|
|
|
67
149
|
// 控制权被拒绝
|
|
@@ -111,7 +193,8 @@ async function pickSession(socket) {
|
|
|
111
193
|
process.stdout.write('\n活跃会话:\n');
|
|
112
194
|
alive.forEach((s, i) => {
|
|
113
195
|
const viewers = s.viewers > 0 ? ` (${s.viewers} 人在线)` : '';
|
|
114
|
-
|
|
196
|
+
const mode = s.mode === 'agent' ? ' [Agent]' : ' [PTY]';
|
|
197
|
+
process.stdout.write(` [${i + 1}] ${s.title}${mode}${viewers} — ${s.id}\n`);
|
|
115
198
|
});
|
|
116
199
|
process.stdout.write('\n');
|
|
117
200
|
|
package/dist/chat.html
CHANGED
|
@@ -976,8 +976,9 @@
|
|
|
976
976
|
}
|
|
977
977
|
}
|
|
978
978
|
|
|
979
|
-
|
|
980
|
-
socket.on('
|
|
979
|
+
let _reconnectFails = 0;
|
|
980
|
+
socket.on('connect', () => { _reconnectFails = 0; showOverlay('正在加入会话...'); socket.emit('join', SESSION_ID); });
|
|
981
|
+
socket.on('connect_error', () => { _reconnectFails++; showOverlay(_reconnectFails >= 5 ? '连接失败,请检查网络' : `正在重连... (${_reconnectFails})`, _reconnectFails >= 10); });
|
|
981
982
|
socket.on('disconnect', () => { showOverlay('已断开连接,正在重连...'); });
|
|
982
983
|
|
|
983
984
|
socket.on('joined', (session) => {
|