index.js
5.23 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
/**
* ClaudeStreamProtocol - Claude CLI stream-json 协议实现
*
* 职责:只关心 Claude stream-json 消息格式和业务语义。
* 不关心传输方式和启动方式 — 那是 Transport 和 Launcher 的事。
*/
const { Protocol } = require('../Protocol');
const { StreamLogger } = require('./logger');
const { handleMessage, registerHandler, registerContentBlockHandler } = require('./handlers');
class ClaudeStreamProtocol extends Protocol {
constructor(config, options = {}) {
super(config, options);
this.workingDir = null;
this.outputChunks = [];
this.transport = null;
// 组件
this.logger = new StreamLogger({
logDir: options.logDir || '.autodev-logs',
prefix: options.logPrefix || 'claude-stream'
});
this._customHandlers = new Map();
}
get name() {
return 'claude-stream';
}
/**
* 注册自定义消息处理器
*/
registerMessageHandler(type, handler) {
registerHandler(type, handler);
return this;
}
/**
* 注册自定义 content block 处理器
*/
registerBlockHandler(type, handler) {
registerContentBlockHandler(type, handler);
return this;
}
/**
* 绑定已连接的 transport
* @param {Transport} transport - 已连接的 Transport 实例
* @param {string} workingDir - 工作目录
*/
async connect(transport, workingDir) {
this.transport = transport;
this.workingDir = workingDir;
const agentName = this.config.id || this.config.name || 'agent';
this.logger.init(workingDir, agentName);
// 监听 Transport 事件
this.transport.on('message', (msg) => this._handleMessage(msg));
this.transport.on('output', (line) => this.emit('output', line));
this.transport.on('stderr', (text) => {
this.logger.logInternal('stderr', { content: text });
this._log(`stderr: ${text}`);
this.emit('stderr', text);
});
this.transport.on('close', (code, signal) => {
this.logger.logInternal('process_close', { code, signal });
this.logger.close();
this.isConnected = false;
this.emit('disconnected', { code, signal });
});
this.isConnected = true;
this._log('连接成功');
}
/**
* 初始化(Claude 不需要显式初始化)
*/
async initialize() {
this._log('initialize (no-op for claude)');
return { sessionId: this.sessionId };
}
/**
* 创建会话(Claude 不支持多会话)
*/
async newSession(options = {}) {
this._log('newSession (no-op for claude)');
return { sessionId: this.sessionId };
}
/**
* 发送提示消息
*/
async sendPrompt(text, files = []) {
if (!this.isConnected) {
throw new Error('Not connected');
}
this.outputChunks = [];
const content = [{ type: 'text', text }];
for (const file of files) {
content.push({ type: 'file', path: file });
}
const msg = {
type: 'user',
message: { role: 'user', content }
};
this._log('发送 user message');
this.logger.logOutgoing(msg);
this.transport.send(msg);
return this._waitForResult();
}
/**
* 等待结果
*/
_waitForResult() {
return new Promise((resolve, reject) => {
const handler = (msg) => {
if (msg.type === 'result') {
this.removeListener('_message', handler);
this.removeListener('disconnected', exitHandler);
resolve({
result: msg.result,
is_error: msg.is_error,
duration_ms: msg.duration_ms
});
}
};
const exitHandler = ({ code, signal } = {}) => {
this.removeListener('_message', handler);
reject(new Error(`Claude process exited unexpectedly (code: ${code}, signal: ${signal})`));
};
this.on('_message', handler);
this.once('disconnected', exitHandler);
});
}
/**
* 响应权限请求
*/
respondPermission(requestId, optionId) {
this._log('respondPermission (not supported in bypass mode)');
}
/**
* 断开连接 — 完整清理协议层资源
*
* 清理 Transport 层的事件监听器,防止已关闭的 transport
* 继续触发回调导致 event loop 无法退出。
*/
disconnect() {
// 清理 Transport 层(解除流上的事件监听器)
if (this.transport) {
try { this.transport.close(); } catch { }
this.transport = null;
}
// 清理 logger
try { this.logger.close(); } catch { }
this.isConnected = false;
this.sessionId = null;
}
/** @private */
_handleMessage(msg) {
this.logger.logIncoming(msg);
this.emit('_message', msg);
const ctx = this._createContext();
handleMessage(msg, ctx);
this.emit('acp_event', {
type: msg.type,
subtype: msg.subtype,
data: msg,
timestamp: Date.now()
});
}
/** @private */
_createContext() {
return {
protocol: this,
emit: (event, data) => this.emit(event, data),
log: (message) => this._log(message),
appendOutput: (text) => this.outputChunks.push(text),
getOutput: () => this.outputChunks.join('')
};
}
/** @private */
_log(message) {
console.log(`[ClaudeStreamProtocol] ${message}`);
}
getLogStats() {
return this.logger.getStats();
}
}
module.exports = { ClaudeStreamProtocol };