@vite-plugin-opencode-assistant/shared 1.0.30 → 1.0.32
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/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/es/process-logger.d.ts +97 -0
- package/es/process-logger.js +195 -0
- package/es/types.d.ts +2 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +2 -0
- package/lib/process-logger.d.ts +97 -0
- package/lib/process-logger.js +219 -0
- package/lib/types.d.ts +2 -0
- package/package.json +1 -1
package/es/index.d.ts
CHANGED
package/es/index.js
CHANGED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview 进程日志捕获器
|
|
3
|
+
* @description 拦截 console 方法并存储日志到内存缓冲区,供 agent 通过工具获取
|
|
4
|
+
*/
|
|
5
|
+
export interface ProcessLogEntry {
|
|
6
|
+
/** 日志级别 */
|
|
7
|
+
level: "log" | "info" | "warn" | "error" | "debug";
|
|
8
|
+
/** 日志内容(已序列化为字符串) */
|
|
9
|
+
message: string;
|
|
10
|
+
/** 时间戳(ISO 格式) */
|
|
11
|
+
timestamp: string;
|
|
12
|
+
/** 来源标识 */
|
|
13
|
+
source?: "console" | "opencode-stdout" | "opencode-stderr" | "vite";
|
|
14
|
+
}
|
|
15
|
+
export interface ProcessLogBufferOptions {
|
|
16
|
+
/** 最大日志条数,默认 500 */
|
|
17
|
+
maxSize?: number;
|
|
18
|
+
/** 是否启用捕获,默认 true */
|
|
19
|
+
enabled?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 进程日志缓冲区
|
|
23
|
+
*/
|
|
24
|
+
declare class ProcessLogBuffer {
|
|
25
|
+
private buffer;
|
|
26
|
+
private maxSize;
|
|
27
|
+
private enabled;
|
|
28
|
+
private originalConsole;
|
|
29
|
+
constructor(options?: ProcessLogBufferOptions);
|
|
30
|
+
/**
|
|
31
|
+
* 启动 console 拦截
|
|
32
|
+
*/
|
|
33
|
+
intercept(): void;
|
|
34
|
+
/**
|
|
35
|
+
* 停止拦截,恢复原始 console
|
|
36
|
+
*/
|
|
37
|
+
restore(): void;
|
|
38
|
+
/**
|
|
39
|
+
* 创建拦截器函数
|
|
40
|
+
*/
|
|
41
|
+
private createInterceptor;
|
|
42
|
+
/**
|
|
43
|
+
* 序列化参数为字符串
|
|
44
|
+
*/
|
|
45
|
+
private serializeArgs;
|
|
46
|
+
/**
|
|
47
|
+
* 添加日志条目
|
|
48
|
+
*/
|
|
49
|
+
addEntry(entry: ProcessLogEntry): void;
|
|
50
|
+
/**
|
|
51
|
+
* 添加 OpenCode stdout 日志
|
|
52
|
+
*/
|
|
53
|
+
addOpenCodeStdout(message: string): void;
|
|
54
|
+
/**
|
|
55
|
+
* 添加 OpenCode stderr 日志
|
|
56
|
+
*/
|
|
57
|
+
addOpenCodeStderr(message: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* 获取日志
|
|
60
|
+
* @param options 过滤选项
|
|
61
|
+
*/
|
|
62
|
+
getLogs(options?: {
|
|
63
|
+
level?: ProcessLogEntry["level"] | ProcessLogEntry["level"][];
|
|
64
|
+
limit?: number;
|
|
65
|
+
source?: ProcessLogEntry["source"];
|
|
66
|
+
since?: string;
|
|
67
|
+
}): ProcessLogEntry[];
|
|
68
|
+
/**
|
|
69
|
+
* 清空缓冲区
|
|
70
|
+
*/
|
|
71
|
+
clear(): void;
|
|
72
|
+
/**
|
|
73
|
+
* 获取缓冲区大小
|
|
74
|
+
*/
|
|
75
|
+
size(): number;
|
|
76
|
+
/**
|
|
77
|
+
* 启用/禁用捕获
|
|
78
|
+
*/
|
|
79
|
+
setEnabled(enabled: boolean): void;
|
|
80
|
+
/**
|
|
81
|
+
* 获取是否启用
|
|
82
|
+
*/
|
|
83
|
+
isEnabled(): boolean;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 获取全局日志缓冲区
|
|
87
|
+
*/
|
|
88
|
+
export declare function getProcessLogBuffer(options?: ProcessLogBufferOptions): ProcessLogBuffer;
|
|
89
|
+
/**
|
|
90
|
+
* 初始化进程日志捕获
|
|
91
|
+
*/
|
|
92
|
+
export declare function initProcessLogCapture(options?: ProcessLogBufferOptions): ProcessLogBuffer;
|
|
93
|
+
/**
|
|
94
|
+
* 停止进程日志捕获
|
|
95
|
+
*/
|
|
96
|
+
export declare function stopProcessLogCapture(): void;
|
|
97
|
+
export {};
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
4
|
+
const DEFAULT_MAX_SIZE = 500;
|
|
5
|
+
class ProcessLogBuffer {
|
|
6
|
+
constructor(options = {}) {
|
|
7
|
+
__publicField(this, "buffer", []);
|
|
8
|
+
__publicField(this, "maxSize");
|
|
9
|
+
__publicField(this, "enabled", true);
|
|
10
|
+
__publicField(this, "originalConsole", null);
|
|
11
|
+
var _a, _b;
|
|
12
|
+
this.maxSize = (_a = options.maxSize) != null ? _a : DEFAULT_MAX_SIZE;
|
|
13
|
+
this.enabled = (_b = options.enabled) != null ? _b : true;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* 启动 console 拦截
|
|
17
|
+
*/
|
|
18
|
+
intercept() {
|
|
19
|
+
if (this.originalConsole) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
this.originalConsole = {
|
|
23
|
+
log: console.log,
|
|
24
|
+
info: console.info,
|
|
25
|
+
warn: console.warn,
|
|
26
|
+
error: console.error,
|
|
27
|
+
debug: console.debug
|
|
28
|
+
};
|
|
29
|
+
console.log = this.createInterceptor("log", this.originalConsole.log);
|
|
30
|
+
console.info = this.createInterceptor("info", this.originalConsole.info);
|
|
31
|
+
console.warn = this.createInterceptor("warn", this.originalConsole.warn);
|
|
32
|
+
console.error = this.createInterceptor("error", this.originalConsole.error);
|
|
33
|
+
console.debug = this.createInterceptor("debug", this.originalConsole.debug);
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 停止拦截,恢复原始 console
|
|
37
|
+
*/
|
|
38
|
+
restore() {
|
|
39
|
+
if (!this.originalConsole) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
console.log = this.originalConsole.log;
|
|
43
|
+
console.info = this.originalConsole.info;
|
|
44
|
+
console.warn = this.originalConsole.warn;
|
|
45
|
+
console.error = this.originalConsole.error;
|
|
46
|
+
console.debug = this.originalConsole.debug;
|
|
47
|
+
this.originalConsole = null;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* 创建拦截器函数
|
|
51
|
+
*/
|
|
52
|
+
createInterceptor(level, original) {
|
|
53
|
+
const self = this;
|
|
54
|
+
return function(...args) {
|
|
55
|
+
original.apply(console, args);
|
|
56
|
+
if (self.enabled) {
|
|
57
|
+
self.addEntry({
|
|
58
|
+
level,
|
|
59
|
+
message: self.serializeArgs(args),
|
|
60
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
61
|
+
source: "console"
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 序列化参数为字符串
|
|
68
|
+
*/
|
|
69
|
+
serializeArgs(args) {
|
|
70
|
+
return args.map((arg) => {
|
|
71
|
+
if (typeof arg === "string") {
|
|
72
|
+
return arg;
|
|
73
|
+
}
|
|
74
|
+
if (typeof arg === "number" || typeof arg === "boolean") {
|
|
75
|
+
return String(arg);
|
|
76
|
+
}
|
|
77
|
+
if (arg === null) {
|
|
78
|
+
return "null";
|
|
79
|
+
}
|
|
80
|
+
if (arg === void 0) {
|
|
81
|
+
return "undefined";
|
|
82
|
+
}
|
|
83
|
+
if (typeof arg === "object") {
|
|
84
|
+
try {
|
|
85
|
+
return JSON.stringify(arg, null, 2);
|
|
86
|
+
} catch (e) {
|
|
87
|
+
return String(arg);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return String(arg);
|
|
91
|
+
}).join(" ");
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 添加日志条目
|
|
95
|
+
*/
|
|
96
|
+
addEntry(entry) {
|
|
97
|
+
if (!this.enabled) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (this.buffer.length >= this.maxSize) {
|
|
101
|
+
this.buffer.shift();
|
|
102
|
+
}
|
|
103
|
+
this.buffer.push(entry);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* 添加 OpenCode stdout 日志
|
|
107
|
+
*/
|
|
108
|
+
addOpenCodeStdout(message) {
|
|
109
|
+
this.addEntry({
|
|
110
|
+
level: "info",
|
|
111
|
+
message,
|
|
112
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
113
|
+
source: "opencode-stdout"
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 添加 OpenCode stderr 日志
|
|
118
|
+
*/
|
|
119
|
+
addOpenCodeStderr(message) {
|
|
120
|
+
this.addEntry({
|
|
121
|
+
level: "error",
|
|
122
|
+
message,
|
|
123
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
124
|
+
source: "opencode-stderr"
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* 获取日志
|
|
129
|
+
* @param options 过滤选项
|
|
130
|
+
*/
|
|
131
|
+
getLogs(options = {}) {
|
|
132
|
+
let logs = [...this.buffer];
|
|
133
|
+
if (options.level) {
|
|
134
|
+
const levels = Array.isArray(options.level) ? options.level : [options.level];
|
|
135
|
+
logs = logs.filter((log) => levels.includes(log.level));
|
|
136
|
+
}
|
|
137
|
+
if (options.source) {
|
|
138
|
+
logs = logs.filter((log) => log.source === options.source);
|
|
139
|
+
}
|
|
140
|
+
if (options.since) {
|
|
141
|
+
const sinceDate = new Date(options.since);
|
|
142
|
+
logs = logs.filter((log) => new Date(log.timestamp) >= sinceDate);
|
|
143
|
+
}
|
|
144
|
+
if (options.limit && options.limit > 0) {
|
|
145
|
+
logs = logs.slice(-options.limit);
|
|
146
|
+
}
|
|
147
|
+
return logs;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* 清空缓冲区
|
|
151
|
+
*/
|
|
152
|
+
clear() {
|
|
153
|
+
this.buffer = [];
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* 获取缓冲区大小
|
|
157
|
+
*/
|
|
158
|
+
size() {
|
|
159
|
+
return this.buffer.length;
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* 启用/禁用捕获
|
|
163
|
+
*/
|
|
164
|
+
setEnabled(enabled) {
|
|
165
|
+
this.enabled = enabled;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* 获取是否启用
|
|
169
|
+
*/
|
|
170
|
+
isEnabled() {
|
|
171
|
+
return this.enabled;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
let globalBuffer = null;
|
|
175
|
+
function getProcessLogBuffer(options) {
|
|
176
|
+
if (!globalBuffer) {
|
|
177
|
+
globalBuffer = new ProcessLogBuffer(options);
|
|
178
|
+
}
|
|
179
|
+
return globalBuffer;
|
|
180
|
+
}
|
|
181
|
+
function initProcessLogCapture(options) {
|
|
182
|
+
const buffer = getProcessLogBuffer(options);
|
|
183
|
+
buffer.intercept();
|
|
184
|
+
return buffer;
|
|
185
|
+
}
|
|
186
|
+
function stopProcessLogCapture() {
|
|
187
|
+
if (globalBuffer) {
|
|
188
|
+
globalBuffer.restore();
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
export {
|
|
192
|
+
getProcessLogBuffer,
|
|
193
|
+
initProcessLogCapture,
|
|
194
|
+
stopProcessLogCapture
|
|
195
|
+
};
|
package/es/types.d.ts
CHANGED
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -16,12 +16,14 @@ var lib_exports = {};
|
|
|
16
16
|
module.exports = __toCommonJS(lib_exports);
|
|
17
17
|
__reExport(lib_exports, require("./constants.js"), module.exports);
|
|
18
18
|
__reExport(lib_exports, require("./logger.js"), module.exports);
|
|
19
|
+
__reExport(lib_exports, require("./process-logger.js"), module.exports);
|
|
19
20
|
__reExport(lib_exports, require("./types.js"), module.exports);
|
|
20
21
|
__reExport(lib_exports, require("./utils.js"), module.exports);
|
|
21
22
|
// Annotate the CommonJS export names for ESM import in node:
|
|
22
23
|
0 && (module.exports = {
|
|
23
24
|
...require("./constants.js"),
|
|
24
25
|
...require("./logger.js"),
|
|
26
|
+
...require("./process-logger.js"),
|
|
25
27
|
...require("./types.js"),
|
|
26
28
|
...require("./utils.js")
|
|
27
29
|
});
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview 进程日志捕获器
|
|
3
|
+
* @description 拦截 console 方法并存储日志到内存缓冲区,供 agent 通过工具获取
|
|
4
|
+
*/
|
|
5
|
+
export interface ProcessLogEntry {
|
|
6
|
+
/** 日志级别 */
|
|
7
|
+
level: "log" | "info" | "warn" | "error" | "debug";
|
|
8
|
+
/** 日志内容(已序列化为字符串) */
|
|
9
|
+
message: string;
|
|
10
|
+
/** 时间戳(ISO 格式) */
|
|
11
|
+
timestamp: string;
|
|
12
|
+
/** 来源标识 */
|
|
13
|
+
source?: "console" | "opencode-stdout" | "opencode-stderr" | "vite";
|
|
14
|
+
}
|
|
15
|
+
export interface ProcessLogBufferOptions {
|
|
16
|
+
/** 最大日志条数,默认 500 */
|
|
17
|
+
maxSize?: number;
|
|
18
|
+
/** 是否启用捕获,默认 true */
|
|
19
|
+
enabled?: boolean;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* 进程日志缓冲区
|
|
23
|
+
*/
|
|
24
|
+
declare class ProcessLogBuffer {
|
|
25
|
+
private buffer;
|
|
26
|
+
private maxSize;
|
|
27
|
+
private enabled;
|
|
28
|
+
private originalConsole;
|
|
29
|
+
constructor(options?: ProcessLogBufferOptions);
|
|
30
|
+
/**
|
|
31
|
+
* 启动 console 拦截
|
|
32
|
+
*/
|
|
33
|
+
intercept(): void;
|
|
34
|
+
/**
|
|
35
|
+
* 停止拦截,恢复原始 console
|
|
36
|
+
*/
|
|
37
|
+
restore(): void;
|
|
38
|
+
/**
|
|
39
|
+
* 创建拦截器函数
|
|
40
|
+
*/
|
|
41
|
+
private createInterceptor;
|
|
42
|
+
/**
|
|
43
|
+
* 序列化参数为字符串
|
|
44
|
+
*/
|
|
45
|
+
private serializeArgs;
|
|
46
|
+
/**
|
|
47
|
+
* 添加日志条目
|
|
48
|
+
*/
|
|
49
|
+
addEntry(entry: ProcessLogEntry): void;
|
|
50
|
+
/**
|
|
51
|
+
* 添加 OpenCode stdout 日志
|
|
52
|
+
*/
|
|
53
|
+
addOpenCodeStdout(message: string): void;
|
|
54
|
+
/**
|
|
55
|
+
* 添加 OpenCode stderr 日志
|
|
56
|
+
*/
|
|
57
|
+
addOpenCodeStderr(message: string): void;
|
|
58
|
+
/**
|
|
59
|
+
* 获取日志
|
|
60
|
+
* @param options 过滤选项
|
|
61
|
+
*/
|
|
62
|
+
getLogs(options?: {
|
|
63
|
+
level?: ProcessLogEntry["level"] | ProcessLogEntry["level"][];
|
|
64
|
+
limit?: number;
|
|
65
|
+
source?: ProcessLogEntry["source"];
|
|
66
|
+
since?: string;
|
|
67
|
+
}): ProcessLogEntry[];
|
|
68
|
+
/**
|
|
69
|
+
* 清空缓冲区
|
|
70
|
+
*/
|
|
71
|
+
clear(): void;
|
|
72
|
+
/**
|
|
73
|
+
* 获取缓冲区大小
|
|
74
|
+
*/
|
|
75
|
+
size(): number;
|
|
76
|
+
/**
|
|
77
|
+
* 启用/禁用捕获
|
|
78
|
+
*/
|
|
79
|
+
setEnabled(enabled: boolean): void;
|
|
80
|
+
/**
|
|
81
|
+
* 获取是否启用
|
|
82
|
+
*/
|
|
83
|
+
isEnabled(): boolean;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* 获取全局日志缓冲区
|
|
87
|
+
*/
|
|
88
|
+
export declare function getProcessLogBuffer(options?: ProcessLogBufferOptions): ProcessLogBuffer;
|
|
89
|
+
/**
|
|
90
|
+
* 初始化进程日志捕获
|
|
91
|
+
*/
|
|
92
|
+
export declare function initProcessLogCapture(options?: ProcessLogBufferOptions): ProcessLogBuffer;
|
|
93
|
+
/**
|
|
94
|
+
* 停止进程日志捕获
|
|
95
|
+
*/
|
|
96
|
+
export declare function stopProcessLogCapture(): void;
|
|
97
|
+
export {};
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
20
|
+
var process_logger_exports = {};
|
|
21
|
+
__export(process_logger_exports, {
|
|
22
|
+
getProcessLogBuffer: () => getProcessLogBuffer,
|
|
23
|
+
initProcessLogCapture: () => initProcessLogCapture,
|
|
24
|
+
stopProcessLogCapture: () => stopProcessLogCapture
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(process_logger_exports);
|
|
27
|
+
const DEFAULT_MAX_SIZE = 500;
|
|
28
|
+
class ProcessLogBuffer {
|
|
29
|
+
constructor(options = {}) {
|
|
30
|
+
__publicField(this, "buffer", []);
|
|
31
|
+
__publicField(this, "maxSize");
|
|
32
|
+
__publicField(this, "enabled", true);
|
|
33
|
+
__publicField(this, "originalConsole", null);
|
|
34
|
+
var _a, _b;
|
|
35
|
+
this.maxSize = (_a = options.maxSize) != null ? _a : DEFAULT_MAX_SIZE;
|
|
36
|
+
this.enabled = (_b = options.enabled) != null ? _b : true;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* 启动 console 拦截
|
|
40
|
+
*/
|
|
41
|
+
intercept() {
|
|
42
|
+
if (this.originalConsole) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
this.originalConsole = {
|
|
46
|
+
log: console.log,
|
|
47
|
+
info: console.info,
|
|
48
|
+
warn: console.warn,
|
|
49
|
+
error: console.error,
|
|
50
|
+
debug: console.debug
|
|
51
|
+
};
|
|
52
|
+
console.log = this.createInterceptor("log", this.originalConsole.log);
|
|
53
|
+
console.info = this.createInterceptor("info", this.originalConsole.info);
|
|
54
|
+
console.warn = this.createInterceptor("warn", this.originalConsole.warn);
|
|
55
|
+
console.error = this.createInterceptor("error", this.originalConsole.error);
|
|
56
|
+
console.debug = this.createInterceptor("debug", this.originalConsole.debug);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* 停止拦截,恢复原始 console
|
|
60
|
+
*/
|
|
61
|
+
restore() {
|
|
62
|
+
if (!this.originalConsole) {
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
console.log = this.originalConsole.log;
|
|
66
|
+
console.info = this.originalConsole.info;
|
|
67
|
+
console.warn = this.originalConsole.warn;
|
|
68
|
+
console.error = this.originalConsole.error;
|
|
69
|
+
console.debug = this.originalConsole.debug;
|
|
70
|
+
this.originalConsole = null;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* 创建拦截器函数
|
|
74
|
+
*/
|
|
75
|
+
createInterceptor(level, original) {
|
|
76
|
+
const self = this;
|
|
77
|
+
return function(...args) {
|
|
78
|
+
original.apply(console, args);
|
|
79
|
+
if (self.enabled) {
|
|
80
|
+
self.addEntry({
|
|
81
|
+
level,
|
|
82
|
+
message: self.serializeArgs(args),
|
|
83
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
84
|
+
source: "console"
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* 序列化参数为字符串
|
|
91
|
+
*/
|
|
92
|
+
serializeArgs(args) {
|
|
93
|
+
return args.map((arg) => {
|
|
94
|
+
if (typeof arg === "string") {
|
|
95
|
+
return arg;
|
|
96
|
+
}
|
|
97
|
+
if (typeof arg === "number" || typeof arg === "boolean") {
|
|
98
|
+
return String(arg);
|
|
99
|
+
}
|
|
100
|
+
if (arg === null) {
|
|
101
|
+
return "null";
|
|
102
|
+
}
|
|
103
|
+
if (arg === void 0) {
|
|
104
|
+
return "undefined";
|
|
105
|
+
}
|
|
106
|
+
if (typeof arg === "object") {
|
|
107
|
+
try {
|
|
108
|
+
return JSON.stringify(arg, null, 2);
|
|
109
|
+
} catch (e) {
|
|
110
|
+
return String(arg);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return String(arg);
|
|
114
|
+
}).join(" ");
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 添加日志条目
|
|
118
|
+
*/
|
|
119
|
+
addEntry(entry) {
|
|
120
|
+
if (!this.enabled) {
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
if (this.buffer.length >= this.maxSize) {
|
|
124
|
+
this.buffer.shift();
|
|
125
|
+
}
|
|
126
|
+
this.buffer.push(entry);
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* 添加 OpenCode stdout 日志
|
|
130
|
+
*/
|
|
131
|
+
addOpenCodeStdout(message) {
|
|
132
|
+
this.addEntry({
|
|
133
|
+
level: "info",
|
|
134
|
+
message,
|
|
135
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
136
|
+
source: "opencode-stdout"
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* 添加 OpenCode stderr 日志
|
|
141
|
+
*/
|
|
142
|
+
addOpenCodeStderr(message) {
|
|
143
|
+
this.addEntry({
|
|
144
|
+
level: "error",
|
|
145
|
+
message,
|
|
146
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
147
|
+
source: "opencode-stderr"
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* 获取日志
|
|
152
|
+
* @param options 过滤选项
|
|
153
|
+
*/
|
|
154
|
+
getLogs(options = {}) {
|
|
155
|
+
let logs = [...this.buffer];
|
|
156
|
+
if (options.level) {
|
|
157
|
+
const levels = Array.isArray(options.level) ? options.level : [options.level];
|
|
158
|
+
logs = logs.filter((log) => levels.includes(log.level));
|
|
159
|
+
}
|
|
160
|
+
if (options.source) {
|
|
161
|
+
logs = logs.filter((log) => log.source === options.source);
|
|
162
|
+
}
|
|
163
|
+
if (options.since) {
|
|
164
|
+
const sinceDate = new Date(options.since);
|
|
165
|
+
logs = logs.filter((log) => new Date(log.timestamp) >= sinceDate);
|
|
166
|
+
}
|
|
167
|
+
if (options.limit && options.limit > 0) {
|
|
168
|
+
logs = logs.slice(-options.limit);
|
|
169
|
+
}
|
|
170
|
+
return logs;
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* 清空缓冲区
|
|
174
|
+
*/
|
|
175
|
+
clear() {
|
|
176
|
+
this.buffer = [];
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* 获取缓冲区大小
|
|
180
|
+
*/
|
|
181
|
+
size() {
|
|
182
|
+
return this.buffer.length;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* 启用/禁用捕获
|
|
186
|
+
*/
|
|
187
|
+
setEnabled(enabled) {
|
|
188
|
+
this.enabled = enabled;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* 获取是否启用
|
|
192
|
+
*/
|
|
193
|
+
isEnabled() {
|
|
194
|
+
return this.enabled;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
let globalBuffer = null;
|
|
198
|
+
function getProcessLogBuffer(options) {
|
|
199
|
+
if (!globalBuffer) {
|
|
200
|
+
globalBuffer = new ProcessLogBuffer(options);
|
|
201
|
+
}
|
|
202
|
+
return globalBuffer;
|
|
203
|
+
}
|
|
204
|
+
function initProcessLogCapture(options) {
|
|
205
|
+
const buffer = getProcessLogBuffer(options);
|
|
206
|
+
buffer.intercept();
|
|
207
|
+
return buffer;
|
|
208
|
+
}
|
|
209
|
+
function stopProcessLogCapture() {
|
|
210
|
+
if (globalBuffer) {
|
|
211
|
+
globalBuffer.restore();
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
215
|
+
0 && (module.exports = {
|
|
216
|
+
getProcessLogBuffer,
|
|
217
|
+
initProcessLogCapture,
|
|
218
|
+
stopProcessLogCapture
|
|
219
|
+
});
|
package/lib/types.d.ts
CHANGED