ConsoleBackend.js
3.62 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
/**
* ConsoleBackend - 控制台输出后端
*
* 用于开发和调试,将日志输出到控制台
*/
const { BaseBackend } = require('./BaseBackend');
class ConsoleBackend extends BaseBackend {
constructor(options = {}) {
super({ name: 'console', ...options });
this.colorize = options.colorize !== false;
this.pretty = options.pretty !== false;
}
async init() {
// 无需初始化
}
async write(entry) {
if (!this._shouldLog(entry)) return;
const formatted = this._formatEntry(entry);
const output = this._formatForConsole(formatted);
if (entry.level === 'error' || entry.level === 'fatal') {
console.error(output);
} else if (entry.level === 'warn') {
console.warn(output);
} else {
console.log(output);
}
}
async query(filters = {}) {
// 控制台不支持查询
return [];
}
/**
* 格式化为控制台输出
* @private
*/
_formatForConsole(entry) {
const { timestamp, level, category, agentName, eventType, payload, sequence } = entry;
const time = new Date(timestamp).toLocaleTimeString();
const agent = agentName ? `[${agentName}]` : '';
const seq = sequence !== undefined ? `#${sequence}` : '';
let output = '';
if (this.pretty) {
// 彩色格式化
const colors = {
reset: '\x1b[0m',
dim: '\x1b[2m',
bright: '\x1b[1m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
};
const levelColors = {
debug: colors.dim,
info: colors.blue,
warn: colors.yellow,
error: colors.red,
fatal: colors.red + colors.bright,
};
const categoryColors = {
agent: colors.cyan,
protocol: colors.magenta,
tool: colors.green,
file: colors.yellow,
system: colors.dim,
};
const lc = this.colorize ? levelColors[level] || colors.reset : '';
const cc = this.colorize ? categoryColors[category] || colors.reset : '';
const rc = this.colorize ? colors.reset : '';
output = `${colors.dim}${time}${rc} ${lc}[${level.toUpperCase()}]${rc} ${cc}[${category}]${rc}${agent} ${eventType} ${seq}`;
// 添加 payload 摘要
if (payload) {
const payloadStr = this._summarizePayload(payload);
if (payloadStr) {
output += ` ${colors.dim}|${rc} ${payloadStr}`;
}
}
} else {
// 简单格式
output = `[${time}] [${level}] [${category}]${agent} ${eventType} ${seq}`;
if (payload) {
const payloadStr = this._summarizePayload(payload);
if (payloadStr) {
output += ` | ${payloadStr}`;
}
}
}
return output;
}
/**
* 摘要化 payload
* @private
*/
_summarizePayload(payload) {
if (!payload) return '';
// 流式输出摘要
if (payload.chunk) {
const text = payload.chunk.substring(0, 50).replace(/\n/g, '\\n');
return `chunk: "${text}${payload.chunk.length > 50 ? '...' : ''}"`;
}
// 状态变更
if (payload.status) {
return `status: ${payload.status}`;
}
// 工具调用
if (payload.toolName) {
return `tool: ${payload.toolName}`;
}
// 文件操作
if (payload.operation && payload.filePath) {
return `${payload.operation}: ${payload.filePath}`;
}
// 默认:返回 JSON 摘要
try {
const str = JSON.stringify(payload);
return str.length > 100 ? str.substring(0, 100) + '...' : str;
} catch {
return '[Object]';
}
}
}
module.exports = { ConsoleBackend };