LogSession.js
7.96 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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/**
* LogSession - 会话级日志管理
*
* 为单个会话(任务)提供结构化的日志记录接口
*/
const { EventEmitter } = require('events');
class LogSession extends EventEmitter {
constructor(sessionId, backend, options = {}) {
super();
this.sessionId = sessionId;
this.backend = backend;
this.options = options;
this.sequence = 0;
this.agentContexts = new Map(); // agentName -> context
this.startTime = new Date().toISOString();
}
/**
* 生成下一个序列号
* @private
*/
_nextSeq() {
return ++this.sequence;
}
/**
* 构建基础日志条目
* @private
*/
_buildEntry(category, eventType, level, payload, agentName = null, metadata = {}) {
return {
sessionId: this.sessionId,
sequence: this._nextSeq(),
timestamp: new Date().toISOString(),
level,
category,
eventType,
agentName,
payload,
metadata: {
...this.options.metadata,
...metadata,
},
};
}
/**
* 写入日志
* @private
*/
async _write(entry) {
try {
await this.backend.write(entry);
this.emit('log', entry);
} catch (err) {
console.error('[LogSession] Failed to write log:', err);
}
}
// ============ Agent 生命周期 ============
/**
* 记录 Agent 开始
*/
logAgentStart(agentName, task, metadata = {}) {
// 设置 Agent 上下文
this.agentContexts.set(agentName, {
startTime: Date.now(),
task,
...metadata,
});
const entry = this._buildEntry(
'agent',
'agent_start',
'info',
{ task },
agentName,
metadata
);
return this._write(entry);
}
/**
* 记录 Agent 结束
*/
logAgentEnd(agentName, output, status, metadata = {}) {
const context = this.agentContexts.get(agentName) || {};
const duration = context.startTime ? Date.now() - context.startTime : null;
const entry = this._buildEntry(
'agent',
'agent_end',
status === 'success' ? 'info' : 'warn',
{ output: this._truncate(output), status, duration },
agentName,
metadata
);
this.agentContexts.delete(agentName);
return this._write(entry);
}
/**
* 记录 Agent 错误
*/
logAgentError(agentName, error, metadata = {}) {
const entry = this._buildEntry(
'agent',
'agent_error',
'error',
{
message: error.message,
stack: error.stack,
},
agentName,
metadata
);
return this._write(entry);
}
// ============ 流式输出 ============
/**
* 记录流式输出块
* @param {string} agentName - Agent 名称
* @param {string} chunk - 输出内容
* @param {string} type - 类型: 'stdout'|'stderr'|'thought'|'message'
*/
logStreamChunk(agentName, chunk, type = 'stdout') {
const entry = this._buildEntry(
'agent',
'stream_chunk',
'debug',
{ chunk, chunkType: type },
agentName
);
// 流式输出通常是高频的,使用 debug 级别
return this._write(entry);
}
// ============ 协议事件 ============
/**
* 记录协议事件(ACP/MCP)
* @param {string} agentName - Agent 名称
* @param {string} direction - 方向: 'in'|'out'
* @param {Object} event - 事件对象
*/
logProtocolEvent(agentName, direction, event) {
const entry = this._buildEntry(
'protocol',
'protocol_event',
'debug',
{ direction, event },
agentName
);
return this._write(entry);
}
// ============ 工具调用 ============
/**
* 记录工具调用
*/
logToolCall(agentName, toolName, args, callId) {
const entry = this._buildEntry(
'tool',
'tool_call',
'info',
{ toolName, args, callId },
agentName
);
return this._write(entry);
}
/**
* 记录工具结果
*/
logToolResult(agentName, callId, result, error = null) {
const level = error ? 'error' : 'info';
const payload = error
? { callId, error: { message: error.message, stack: error.stack } }
: { callId, result: this._truncate(result) };
const entry = this._buildEntry('tool', 'tool_result', level, payload, agentName);
return this._write(entry);
}
// ============ 子代理调用 ============
/**
* 记录子代理调用
*/
logSubagentCall(parentAgent, subagentName, task) {
const entry = this._buildEntry(
'agent',
'subagent_call',
'info',
{ subagentName, task: this._truncate(task) },
parentAgent
);
return this._write(entry);
}
/**
* 记录子代理返回
*/
logSubagentReturn(parentAgent, subagentName, result) {
const entry = this._buildEntry(
'agent',
'subagent_return',
'info',
{ subagentName, result: this._truncate(result) },
parentAgent
);
return this._write(entry);
}
// ============ 文件变更 ============
/**
* 记录文件变更
* @param {string} agentName - Agent 名称
* @param {string} operation - 操作: 'create'|'modify'|'delete'|'read'
* @param {string} filePath - 文件路径
* @param {Object} metadata - 额外信息
*/
logFileChange(agentName, operation, filePath, metadata = {}) {
const entry = this._buildEntry(
'file',
'file_change',
'info',
{ operation, filePath },
agentName,
metadata
);
return this._write(entry);
}
// ============ 自定义事件 ============
/**
* 记录自定义事件
* @param {string} agentName - Agent 名称(可选,null 表示系统级)
* @param {string} eventType - 事件类型
* @param {Object} payload - 事件数据
* @param {string} level - 日志级别
*/
logEvent(agentName, eventType, payload, level = 'info') {
const category = agentName ? 'agent' : 'system';
const entry = this._buildEntry(category, eventType, level, payload, agentName);
return this._write(entry);
}
// ============ 系统级快捷方法 ============
logInfo(eventType, payload) {
return this.logEvent(null, eventType, payload, 'info');
}
logWarn(eventType, payload) {
return this.logEvent(null, eventType, payload, 'warn');
}
logError(eventType, payload) {
return this.logEvent(null, eventType, payload, 'error');
}
logDebug(eventType, payload) {
return this.logEvent(null, eventType, payload, 'debug');
}
// ============ 会话管理 ============
/**
* 生成会话报告
*/
async generateReport() {
// 查询所有日志
const entries = await this.backend.query({ sessionId: this.sessionId, limit: 10000 });
// 统计信息
const stats = {
totalEvents: entries.length,
agents: new Set(),
tools: new Set(),
files: new Set(),
errors: 0,
};
for (const entry of entries) {
if (entry.agentName) stats.agents.add(entry.agentName);
if (entry.category === 'tool' && entry.payload?.toolName) {
stats.tools.add(entry.payload.toolName);
}
if (entry.category === 'file' && entry.payload?.filePath) {
stats.files.add(entry.payload.filePath);
}
if (entry.level === 'error' || entry.level === 'fatal') {
stats.errors++;
}
}
return {
sessionId: this.sessionId,
startTime: this.startTime,
endTime: new Date().toISOString(),
stats: {
...stats,
agents: Array.from(stats.agents),
tools: Array.from(stats.tools),
files: Array.from(stats.files),
},
};
}
/**
* 关闭会话
*/
async close() {
this.emit('close');
this.removeAllListeners();
}
// ============ 辅助方法 ============
/**
* 截断长文本
* @private
*/
_truncate(text, maxLength = 10000) {
if (!text) return text;
const str = typeof text === 'string' ? text : JSON.stringify(text);
if (str.length <= maxLength) return str;
return str.substring(0, maxLength) + `... [truncated ${str.length - maxLength} chars]`;
}
}
module.exports = { LogSession };