@vite-plugin-opencode-assistant/shared 1.0.5
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/constants.d.ts +74 -0
- package/es/constants.js +65 -0
- package/es/index.d.ts +3 -0
- package/es/index.js +3 -0
- package/es/logger.d.ts +64 -0
- package/es/logger.js +353 -0
- package/es/types.d.ts +126 -0
- package/es/types.js +0 -0
- package/lib/constants.d.ts +74 -0
- package/lib/constants.js +113 -0
- package/lib/index.d.ts +3 -0
- package/lib/index.js +25 -0
- package/lib/logger.d.ts +64 -0
- package/lib/logger.js +384 -0
- package/lib/types.d.ts +126 -0
- package/lib/types.js +15 -0
- package/package.json +27 -0
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview OpenCode 插件常量定义
|
|
3
|
+
*/
|
|
4
|
+
/** ==================== 网络相关 ==================== */
|
|
5
|
+
/** 默认主机名 */
|
|
6
|
+
export declare const DEFAULT_HOSTNAME = "127.0.0.1";
|
|
7
|
+
/** 默认 Web 服务端口 */
|
|
8
|
+
export declare const DEFAULT_WEB_PORT = 4097;
|
|
9
|
+
/** 服务器启动超时时间(毫秒) */
|
|
10
|
+
export declare const SERVER_START_TIMEOUT = 300000;
|
|
11
|
+
/** 服务器检查间隔(毫秒) */
|
|
12
|
+
export declare const SERVER_CHECK_INTERVAL = 100;
|
|
13
|
+
/** ==================== 重试相关 ==================== */
|
|
14
|
+
/** 默认重试次数 */
|
|
15
|
+
export declare const DEFAULT_RETRIES = 5;
|
|
16
|
+
/** 重试延迟(毫秒) */
|
|
17
|
+
export declare const RETRY_DELAY = 500;
|
|
18
|
+
/** ==================== 端口查找 ==================== */
|
|
19
|
+
/** 最大端口尝试次数 */
|
|
20
|
+
export declare const MAX_PORT_TRIES = 10;
|
|
21
|
+
/** ==================== 日志相关 ==================== */
|
|
22
|
+
/** 插件日志前缀 */
|
|
23
|
+
export declare const LOG_PREFIX = "[vite-plugin-opencode]";
|
|
24
|
+
/** OpenCode Web 日志前缀 */
|
|
25
|
+
export declare const WEB_LOG_PREFIX = "[OpenCode Web]";
|
|
26
|
+
/** OpenCode Web 错误日志前缀 */
|
|
27
|
+
export declare const WEB_ERROR_PREFIX = "[OpenCode Web Error]";
|
|
28
|
+
/** ==================== 挂件相关 ==================== */
|
|
29
|
+
/** 挂件脚本路径 */
|
|
30
|
+
export declare const WIDGET_SCRIPT_PATH = "/__opencode_widget__.js";
|
|
31
|
+
/** 配置数据属性名 */
|
|
32
|
+
export declare const CONFIG_DATA_ATTR = "data-opencode-config";
|
|
33
|
+
/** 上下文 API 路径 */
|
|
34
|
+
export declare const CONTEXT_API_PATH = "/__opencode_context__";
|
|
35
|
+
/** 启动 API 路径 */
|
|
36
|
+
export declare const START_API_PATH = "/__opencode_start__";
|
|
37
|
+
/** 会话列表 API 路径 */
|
|
38
|
+
export declare const SESSIONS_API_PATH = "/__opencode_sessions__";
|
|
39
|
+
/** SSE 事件流路径 */
|
|
40
|
+
export declare const SSE_EVENTS_PATH = "/__opencode_events__";
|
|
41
|
+
/** 上下文更新间隔(毫秒) */
|
|
42
|
+
export declare const CONTEXT_UPDATE_INTERVAL = 500;
|
|
43
|
+
/** 服务器同步间隔(毫秒) */
|
|
44
|
+
export declare const SERVER_SYNC_INTERVAL = 2000;
|
|
45
|
+
/** Vue Inspector 检查间隔(毫秒) */
|
|
46
|
+
export declare const INSPECTOR_CHECK_INTERVAL = 500;
|
|
47
|
+
/** 自动打开延迟(毫秒) */
|
|
48
|
+
export declare const AUTO_OPEN_DELAY = 1000;
|
|
49
|
+
/** 通知显示时间(毫秒) */
|
|
50
|
+
export declare const NOTIFICATION_DURATION = 3000;
|
|
51
|
+
/** ==================== 存储相关 ==================== */
|
|
52
|
+
/** 初始化标记 */
|
|
53
|
+
export declare const INIT_MARKER = "__OPENCODE_INITIALIZED__";
|
|
54
|
+
/** 选中元素存储键 */
|
|
55
|
+
export declare const SELECTED_ELEMENTS_KEY = "__opencode_selected_elements__";
|
|
56
|
+
/** ==================== 文本处理 ==================== */
|
|
57
|
+
/** 元素文本最大显示长度 */
|
|
58
|
+
export declare const MAX_TEXT_LENGTH = 100;
|
|
59
|
+
/** 元素上下文标记 */
|
|
60
|
+
export declare const CONTEXT_MARKER = "[\u5143\u7D20\u4E0A\u4E0B\u6587]";
|
|
61
|
+
/** ==================== 默认配置 ==================== */
|
|
62
|
+
/** 默认插件配置 */
|
|
63
|
+
export declare const DEFAULT_CONFIG: {
|
|
64
|
+
enabled: boolean;
|
|
65
|
+
webPort: number;
|
|
66
|
+
hostname: string;
|
|
67
|
+
position: "bottom-right";
|
|
68
|
+
theme: "auto";
|
|
69
|
+
open: boolean;
|
|
70
|
+
autoReload: boolean;
|
|
71
|
+
verbose: boolean;
|
|
72
|
+
hotkey: string;
|
|
73
|
+
warmupChromeMcp: boolean;
|
|
74
|
+
};
|
package/lib/constants.js
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var constants_exports = {};
|
|
19
|
+
__export(constants_exports, {
|
|
20
|
+
AUTO_OPEN_DELAY: () => AUTO_OPEN_DELAY,
|
|
21
|
+
CONFIG_DATA_ATTR: () => CONFIG_DATA_ATTR,
|
|
22
|
+
CONTEXT_API_PATH: () => CONTEXT_API_PATH,
|
|
23
|
+
CONTEXT_MARKER: () => CONTEXT_MARKER,
|
|
24
|
+
CONTEXT_UPDATE_INTERVAL: () => CONTEXT_UPDATE_INTERVAL,
|
|
25
|
+
DEFAULT_CONFIG: () => DEFAULT_CONFIG,
|
|
26
|
+
DEFAULT_HOSTNAME: () => DEFAULT_HOSTNAME,
|
|
27
|
+
DEFAULT_RETRIES: () => DEFAULT_RETRIES,
|
|
28
|
+
DEFAULT_WEB_PORT: () => DEFAULT_WEB_PORT,
|
|
29
|
+
INIT_MARKER: () => INIT_MARKER,
|
|
30
|
+
INSPECTOR_CHECK_INTERVAL: () => INSPECTOR_CHECK_INTERVAL,
|
|
31
|
+
LOG_PREFIX: () => LOG_PREFIX,
|
|
32
|
+
MAX_PORT_TRIES: () => MAX_PORT_TRIES,
|
|
33
|
+
MAX_TEXT_LENGTH: () => MAX_TEXT_LENGTH,
|
|
34
|
+
NOTIFICATION_DURATION: () => NOTIFICATION_DURATION,
|
|
35
|
+
RETRY_DELAY: () => RETRY_DELAY,
|
|
36
|
+
SELECTED_ELEMENTS_KEY: () => SELECTED_ELEMENTS_KEY,
|
|
37
|
+
SERVER_CHECK_INTERVAL: () => SERVER_CHECK_INTERVAL,
|
|
38
|
+
SERVER_START_TIMEOUT: () => SERVER_START_TIMEOUT,
|
|
39
|
+
SERVER_SYNC_INTERVAL: () => SERVER_SYNC_INTERVAL,
|
|
40
|
+
SESSIONS_API_PATH: () => SESSIONS_API_PATH,
|
|
41
|
+
SSE_EVENTS_PATH: () => SSE_EVENTS_PATH,
|
|
42
|
+
START_API_PATH: () => START_API_PATH,
|
|
43
|
+
WEB_ERROR_PREFIX: () => WEB_ERROR_PREFIX,
|
|
44
|
+
WEB_LOG_PREFIX: () => WEB_LOG_PREFIX,
|
|
45
|
+
WIDGET_SCRIPT_PATH: () => WIDGET_SCRIPT_PATH
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(constants_exports);
|
|
48
|
+
const DEFAULT_HOSTNAME = "127.0.0.1";
|
|
49
|
+
const DEFAULT_WEB_PORT = 4097;
|
|
50
|
+
const SERVER_START_TIMEOUT = 3e5;
|
|
51
|
+
const SERVER_CHECK_INTERVAL = 100;
|
|
52
|
+
const DEFAULT_RETRIES = 5;
|
|
53
|
+
const RETRY_DELAY = 500;
|
|
54
|
+
const MAX_PORT_TRIES = 10;
|
|
55
|
+
const LOG_PREFIX = "[vite-plugin-opencode]";
|
|
56
|
+
const WEB_LOG_PREFIX = "[OpenCode Web]";
|
|
57
|
+
const WEB_ERROR_PREFIX = "[OpenCode Web Error]";
|
|
58
|
+
const WIDGET_SCRIPT_PATH = "/__opencode_widget__.js";
|
|
59
|
+
const CONFIG_DATA_ATTR = "data-opencode-config";
|
|
60
|
+
const CONTEXT_API_PATH = "/__opencode_context__";
|
|
61
|
+
const START_API_PATH = "/__opencode_start__";
|
|
62
|
+
const SESSIONS_API_PATH = "/__opencode_sessions__";
|
|
63
|
+
const SSE_EVENTS_PATH = "/__opencode_events__";
|
|
64
|
+
const CONTEXT_UPDATE_INTERVAL = 500;
|
|
65
|
+
const SERVER_SYNC_INTERVAL = 2e3;
|
|
66
|
+
const INSPECTOR_CHECK_INTERVAL = 500;
|
|
67
|
+
const AUTO_OPEN_DELAY = 1e3;
|
|
68
|
+
const NOTIFICATION_DURATION = 3e3;
|
|
69
|
+
const INIT_MARKER = "__OPENCODE_INITIALIZED__";
|
|
70
|
+
const SELECTED_ELEMENTS_KEY = "__opencode_selected_elements__";
|
|
71
|
+
const MAX_TEXT_LENGTH = 100;
|
|
72
|
+
const CONTEXT_MARKER = "[\u5143\u7D20\u4E0A\u4E0B\u6587]";
|
|
73
|
+
const DEFAULT_CONFIG = {
|
|
74
|
+
enabled: true,
|
|
75
|
+
webPort: DEFAULT_WEB_PORT,
|
|
76
|
+
hostname: DEFAULT_HOSTNAME,
|
|
77
|
+
position: "bottom-right",
|
|
78
|
+
theme: "auto",
|
|
79
|
+
open: false,
|
|
80
|
+
autoReload: true,
|
|
81
|
+
verbose: false,
|
|
82
|
+
hotkey: "ctrl+k",
|
|
83
|
+
warmupChromeMcp: true
|
|
84
|
+
};
|
|
85
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
86
|
+
0 && (module.exports = {
|
|
87
|
+
AUTO_OPEN_DELAY,
|
|
88
|
+
CONFIG_DATA_ATTR,
|
|
89
|
+
CONTEXT_API_PATH,
|
|
90
|
+
CONTEXT_MARKER,
|
|
91
|
+
CONTEXT_UPDATE_INTERVAL,
|
|
92
|
+
DEFAULT_CONFIG,
|
|
93
|
+
DEFAULT_HOSTNAME,
|
|
94
|
+
DEFAULT_RETRIES,
|
|
95
|
+
DEFAULT_WEB_PORT,
|
|
96
|
+
INIT_MARKER,
|
|
97
|
+
INSPECTOR_CHECK_INTERVAL,
|
|
98
|
+
LOG_PREFIX,
|
|
99
|
+
MAX_PORT_TRIES,
|
|
100
|
+
MAX_TEXT_LENGTH,
|
|
101
|
+
NOTIFICATION_DURATION,
|
|
102
|
+
RETRY_DELAY,
|
|
103
|
+
SELECTED_ELEMENTS_KEY,
|
|
104
|
+
SERVER_CHECK_INTERVAL,
|
|
105
|
+
SERVER_START_TIMEOUT,
|
|
106
|
+
SERVER_SYNC_INTERVAL,
|
|
107
|
+
SESSIONS_API_PATH,
|
|
108
|
+
SSE_EVENTS_PATH,
|
|
109
|
+
START_API_PATH,
|
|
110
|
+
WEB_ERROR_PREFIX,
|
|
111
|
+
WEB_LOG_PREFIX,
|
|
112
|
+
WIDGET_SCRIPT_PATH
|
|
113
|
+
});
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
14
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
+
var lib_exports = {};
|
|
16
|
+
module.exports = __toCommonJS(lib_exports);
|
|
17
|
+
__reExport(lib_exports, require("./constants.js"), module.exports);
|
|
18
|
+
__reExport(lib_exports, require("./logger.js"), module.exports);
|
|
19
|
+
__reExport(lib_exports, require("./types.js"), module.exports);
|
|
20
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
21
|
+
0 && (module.exports = {
|
|
22
|
+
...require("./constants.js"),
|
|
23
|
+
...require("./logger.js"),
|
|
24
|
+
...require("./types.js")
|
|
25
|
+
});
|
package/lib/logger.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export declare enum LogLevel {
|
|
2
|
+
DEBUG = 0,
|
|
3
|
+
INFO = 1,
|
|
4
|
+
WARN = 2,
|
|
5
|
+
ERROR = 3,
|
|
6
|
+
NONE = 4
|
|
7
|
+
}
|
|
8
|
+
export interface LogContext {
|
|
9
|
+
module?: string;
|
|
10
|
+
operation?: string;
|
|
11
|
+
traceId?: string;
|
|
12
|
+
duration?: number;
|
|
13
|
+
error?: Error | unknown;
|
|
14
|
+
[key: string]: unknown;
|
|
15
|
+
}
|
|
16
|
+
interface LoggerConfig {
|
|
17
|
+
verbose: boolean;
|
|
18
|
+
level: LogLevel;
|
|
19
|
+
showTimestamp: boolean;
|
|
20
|
+
showCaller: boolean;
|
|
21
|
+
showTrace: boolean;
|
|
22
|
+
indent: string;
|
|
23
|
+
}
|
|
24
|
+
export declare function configureLogger(options: Partial<LoggerConfig>): void;
|
|
25
|
+
export declare function setVerbose(verbose: boolean): void;
|
|
26
|
+
export declare const logger: {
|
|
27
|
+
debug(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
28
|
+
info(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
29
|
+
warn(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
30
|
+
error(message: string, context?: LogContext, ...args: unknown[]): void;
|
|
31
|
+
group(label: string, context?: LogContext): void;
|
|
32
|
+
groupEnd(): void;
|
|
33
|
+
};
|
|
34
|
+
export declare function generateTraceId(): string;
|
|
35
|
+
export declare class PerformanceTimer {
|
|
36
|
+
private startTime;
|
|
37
|
+
private context;
|
|
38
|
+
private operation;
|
|
39
|
+
constructor(operation: string, context?: LogContext);
|
|
40
|
+
end(message?: string): number;
|
|
41
|
+
checkpoint(label: string): number;
|
|
42
|
+
}
|
|
43
|
+
export declare class RequestContext {
|
|
44
|
+
traceId: string;
|
|
45
|
+
method: string;
|
|
46
|
+
path: string;
|
|
47
|
+
startTime: number;
|
|
48
|
+
private checkpoints;
|
|
49
|
+
constructor(method: string, path: string);
|
|
50
|
+
checkpoint(label: string): void;
|
|
51
|
+
end(statusCode: number): void;
|
|
52
|
+
error(error: Error | unknown): void;
|
|
53
|
+
}
|
|
54
|
+
export declare function createLogger(module: string): {
|
|
55
|
+
debug(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
56
|
+
info(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
57
|
+
warn(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
58
|
+
error(message: string, context?: Omit<LogContext, "module">, ...args: unknown[]): void;
|
|
59
|
+
timer(operation: string, context?: Omit<LogContext, "module">): PerformanceTimer;
|
|
60
|
+
};
|
|
61
|
+
export declare function logMethod(target: unknown, propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor;
|
|
62
|
+
export declare function formatBytes(bytes: number): string;
|
|
63
|
+
export declare function formatDuration(ms: number): string;
|
|
64
|
+
export {};
|
package/lib/logger.js
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __spreadValues = (a, b) => {
|
|
11
|
+
for (var prop in b || (b = {}))
|
|
12
|
+
if (__hasOwnProp.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
if (__getOwnPropSymbols)
|
|
15
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
+
if (__propIsEnum.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
};
|
|
21
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
22
|
+
var __export = (target, all) => {
|
|
23
|
+
for (var name in all)
|
|
24
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
25
|
+
};
|
|
26
|
+
var __copyProps = (to, from, except, desc) => {
|
|
27
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
28
|
+
for (let key of __getOwnPropNames(from))
|
|
29
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
30
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
31
|
+
}
|
|
32
|
+
return to;
|
|
33
|
+
};
|
|
34
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
35
|
+
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
36
|
+
var __async = (__this, __arguments, generator) => {
|
|
37
|
+
return new Promise((resolve, reject) => {
|
|
38
|
+
var fulfilled = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.next(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var rejected = (value) => {
|
|
46
|
+
try {
|
|
47
|
+
step(generator.throw(value));
|
|
48
|
+
} catch (e) {
|
|
49
|
+
reject(e);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
53
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
54
|
+
});
|
|
55
|
+
};
|
|
56
|
+
var logger_exports = {};
|
|
57
|
+
__export(logger_exports, {
|
|
58
|
+
LogLevel: () => LogLevel,
|
|
59
|
+
PerformanceTimer: () => PerformanceTimer,
|
|
60
|
+
RequestContext: () => RequestContext,
|
|
61
|
+
configureLogger: () => configureLogger,
|
|
62
|
+
createLogger: () => createLogger,
|
|
63
|
+
formatBytes: () => formatBytes,
|
|
64
|
+
formatDuration: () => formatDuration,
|
|
65
|
+
generateTraceId: () => generateTraceId,
|
|
66
|
+
logMethod: () => logMethod,
|
|
67
|
+
logger: () => logger,
|
|
68
|
+
setVerbose: () => setVerbose
|
|
69
|
+
});
|
|
70
|
+
module.exports = __toCommonJS(logger_exports);
|
|
71
|
+
var import_constants = require("./constants.js");
|
|
72
|
+
var LogLevel = /* @__PURE__ */ ((LogLevel2) => {
|
|
73
|
+
LogLevel2[LogLevel2["DEBUG"] = 0] = "DEBUG";
|
|
74
|
+
LogLevel2[LogLevel2["INFO"] = 1] = "INFO";
|
|
75
|
+
LogLevel2[LogLevel2["WARN"] = 2] = "WARN";
|
|
76
|
+
LogLevel2[LogLevel2["ERROR"] = 3] = "ERROR";
|
|
77
|
+
LogLevel2[LogLevel2["NONE"] = 4] = "NONE";
|
|
78
|
+
return LogLevel2;
|
|
79
|
+
})(LogLevel || {});
|
|
80
|
+
const COLORS = {
|
|
81
|
+
reset: "\x1B[0m",
|
|
82
|
+
dim: "\x1B[2m",
|
|
83
|
+
bright: "\x1B[1m",
|
|
84
|
+
red: "\x1B[31m",
|
|
85
|
+
green: "\x1B[32m",
|
|
86
|
+
yellow: "\x1B[33m",
|
|
87
|
+
blue: "\x1B[34m",
|
|
88
|
+
magenta: "\x1B[35m",
|
|
89
|
+
cyan: "\x1B[36m",
|
|
90
|
+
white: "\x1B[37m"
|
|
91
|
+
};
|
|
92
|
+
const LEVEL_COLORS = {
|
|
93
|
+
[0 /* DEBUG */]: COLORS.cyan,
|
|
94
|
+
[1 /* INFO */]: COLORS.green,
|
|
95
|
+
[2 /* WARN */]: COLORS.yellow,
|
|
96
|
+
[3 /* ERROR */]: COLORS.red,
|
|
97
|
+
[4 /* NONE */]: COLORS.reset
|
|
98
|
+
};
|
|
99
|
+
const LEVEL_NAMES = {
|
|
100
|
+
[0 /* DEBUG */]: "DEBUG",
|
|
101
|
+
[1 /* INFO */]: "INFO",
|
|
102
|
+
[2 /* WARN */]: "WARN",
|
|
103
|
+
[3 /* ERROR */]: "ERROR",
|
|
104
|
+
[4 /* NONE */]: "NONE"
|
|
105
|
+
};
|
|
106
|
+
let globalConfig = {
|
|
107
|
+
verbose: false,
|
|
108
|
+
level: 1 /* INFO */,
|
|
109
|
+
showTimestamp: true,
|
|
110
|
+
showCaller: true,
|
|
111
|
+
showTrace: false,
|
|
112
|
+
indent: " "
|
|
113
|
+
};
|
|
114
|
+
let traceCounter = 0;
|
|
115
|
+
function configureLogger(options) {
|
|
116
|
+
globalConfig = __spreadValues(__spreadValues({}, globalConfig), options);
|
|
117
|
+
}
|
|
118
|
+
function setVerbose(verbose) {
|
|
119
|
+
globalConfig.verbose = verbose;
|
|
120
|
+
globalConfig.level = verbose ? 0 /* DEBUG */ : 1 /* INFO */;
|
|
121
|
+
}
|
|
122
|
+
function getTimestamp() {
|
|
123
|
+
const now = /* @__PURE__ */ new Date();
|
|
124
|
+
const hours = String(now.getHours()).padStart(2, "0");
|
|
125
|
+
const minutes = String(now.getMinutes()).padStart(2, "0");
|
|
126
|
+
const seconds = String(now.getSeconds()).padStart(2, "0");
|
|
127
|
+
const ms = String(now.getMilliseconds()).padStart(3, "0");
|
|
128
|
+
return `${hours}:${minutes}:${seconds}.${ms}`;
|
|
129
|
+
}
|
|
130
|
+
function getCallerInfo(depth = 3) {
|
|
131
|
+
const stack = new Error().stack;
|
|
132
|
+
if (!stack) return "";
|
|
133
|
+
const lines = stack.split("\n");
|
|
134
|
+
const targetLine = lines[depth];
|
|
135
|
+
if (!targetLine) return "";
|
|
136
|
+
const match = targetLine.match(/at\s+(?:(.+?)\s+\()?(.+?):(\d+):(\d+)\)?/);
|
|
137
|
+
if (!match) return "";
|
|
138
|
+
const [, funcName, filePath, line] = match;
|
|
139
|
+
const fileName = filePath.split("/").pop() || filePath;
|
|
140
|
+
const func = funcName || "<anonymous>";
|
|
141
|
+
return `${fileName}:${line} ${func}`;
|
|
142
|
+
}
|
|
143
|
+
function formatValue(value, depth = 0) {
|
|
144
|
+
if (depth > 3) return "...";
|
|
145
|
+
if (value === null) return "null";
|
|
146
|
+
if (value === void 0) return "undefined";
|
|
147
|
+
if (typeof value === "string") return depth > 0 ? `"${value}"` : value;
|
|
148
|
+
if (typeof value === "number" || typeof value === "boolean") return String(value);
|
|
149
|
+
if (value instanceof Error) {
|
|
150
|
+
return `${value.name}: ${value.message}${value.stack ? `
|
|
151
|
+
${value.stack}` : ""}`;
|
|
152
|
+
}
|
|
153
|
+
if (Array.isArray(value)) {
|
|
154
|
+
if (value.length === 0) return "[]";
|
|
155
|
+
if (value.length > 5) {
|
|
156
|
+
const items2 = value.slice(0, 3).map((v) => formatValue(v, depth + 1));
|
|
157
|
+
return `[${items2.join(", ")}, ... ${value.length - 3} more items]`;
|
|
158
|
+
}
|
|
159
|
+
const items = value.map((v) => formatValue(v, depth + 1));
|
|
160
|
+
return `[${items.join(", ")}]`;
|
|
161
|
+
}
|
|
162
|
+
if (typeof value === "object") {
|
|
163
|
+
const entries = Object.entries(value);
|
|
164
|
+
if (entries.length === 0) return "{}";
|
|
165
|
+
if (entries.length > 5) {
|
|
166
|
+
const shown = entries.slice(0, 3).map(([k, v]) => `${k}: ${formatValue(v, depth + 1)}`);
|
|
167
|
+
return `{${shown.join(", ")}, ... ${entries.length - 3} more keys}`;
|
|
168
|
+
}
|
|
169
|
+
const formatted = entries.map(([k, v]) => `${k}: ${formatValue(v, depth + 1)}`);
|
|
170
|
+
return `{${formatted.join(", ")}}`;
|
|
171
|
+
}
|
|
172
|
+
return String(value);
|
|
173
|
+
}
|
|
174
|
+
function formatContext(context) {
|
|
175
|
+
if (!context || Object.keys(context).length === 0) return "";
|
|
176
|
+
const parts = [];
|
|
177
|
+
if (context.module) parts.push(`[${context.module}]`);
|
|
178
|
+
if (context.operation) parts.push(`(${context.operation})`);
|
|
179
|
+
if (context.traceId) parts.push(`trace:${context.traceId}`);
|
|
180
|
+
if (context.duration !== void 0) parts.push(`${context.duration}ms`);
|
|
181
|
+
const extraKeys = Object.keys(context).filter(
|
|
182
|
+
(k) => !["module", "operation", "traceId", "duration", "error"].includes(k)
|
|
183
|
+
);
|
|
184
|
+
if (extraKeys.length > 0) {
|
|
185
|
+
const extra = {};
|
|
186
|
+
extraKeys.forEach((k) => extra[k] = context[k]);
|
|
187
|
+
parts.push(formatValue(extra));
|
|
188
|
+
}
|
|
189
|
+
return parts.join(" ");
|
|
190
|
+
}
|
|
191
|
+
function log(level, message, context, ...args) {
|
|
192
|
+
if (level < globalConfig.level) return;
|
|
193
|
+
const parts = [];
|
|
194
|
+
parts.push(`${COLORS.dim}[${process.pid}]${COLORS.reset}`);
|
|
195
|
+
if (globalConfig.showTimestamp) {
|
|
196
|
+
parts.push(`${COLORS.dim}${getTimestamp()}${COLORS.reset}`);
|
|
197
|
+
}
|
|
198
|
+
const levelColor = LEVEL_COLORS[level];
|
|
199
|
+
const levelName = LEVEL_NAMES[level].padEnd(5);
|
|
200
|
+
parts.push(`${levelColor}${levelName}${COLORS.reset}`);
|
|
201
|
+
parts.push(`${COLORS.bright}${import_constants.LOG_PREFIX}${COLORS.reset}`);
|
|
202
|
+
const contextStr = formatContext(context);
|
|
203
|
+
if (contextStr) {
|
|
204
|
+
parts.push(`${COLORS.magenta}${contextStr}${COLORS.reset}`);
|
|
205
|
+
}
|
|
206
|
+
parts.push(message);
|
|
207
|
+
if (globalConfig.showCaller && level >= 2 /* WARN */) {
|
|
208
|
+
const caller = getCallerInfo(4);
|
|
209
|
+
if (caller) {
|
|
210
|
+
parts.push(`${COLORS.dim}(${caller})${COLORS.reset}`);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
const formattedArgs = args.map((a) => formatValue(a)).join(" ");
|
|
214
|
+
if (formattedArgs) {
|
|
215
|
+
parts.push(formattedArgs);
|
|
216
|
+
}
|
|
217
|
+
const output = parts.join(" ");
|
|
218
|
+
if (level >= 3 /* ERROR */) {
|
|
219
|
+
console.error(output);
|
|
220
|
+
} else if (level === 2 /* WARN */) {
|
|
221
|
+
console.warn(output);
|
|
222
|
+
} else {
|
|
223
|
+
console.log(output);
|
|
224
|
+
}
|
|
225
|
+
if ((context == null ? void 0 : context.error) && level >= 3 /* ERROR */ && globalConfig.showTrace) {
|
|
226
|
+
const err = context.error;
|
|
227
|
+
if (err instanceof Error && err.stack) {
|
|
228
|
+
console.error(`${COLORS.dim}${err.stack}${COLORS.reset}`);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
const logger = {
|
|
233
|
+
debug(message, context, ...args) {
|
|
234
|
+
log(0 /* DEBUG */, message, context, ...args);
|
|
235
|
+
},
|
|
236
|
+
info(message, context, ...args) {
|
|
237
|
+
log(1 /* INFO */, message, context, ...args);
|
|
238
|
+
},
|
|
239
|
+
warn(message, context, ...args) {
|
|
240
|
+
log(2 /* WARN */, message, context, ...args);
|
|
241
|
+
},
|
|
242
|
+
error(message, context, ...args) {
|
|
243
|
+
log(3 /* ERROR */, message, context, ...args);
|
|
244
|
+
},
|
|
245
|
+
group(label, context) {
|
|
246
|
+
if (!globalConfig.verbose) return;
|
|
247
|
+
const contextStr = formatContext(context);
|
|
248
|
+
console.log(
|
|
249
|
+
`${COLORS.dim}[${process.pid}]${COLORS.reset} ${COLORS.bright}${import_constants.LOG_PREFIX}${COLORS.reset} ${COLORS.blue}\u25BC${COLORS.reset} ${label}${contextStr ? ` ${contextStr}` : ""}`
|
|
250
|
+
);
|
|
251
|
+
},
|
|
252
|
+
groupEnd() {
|
|
253
|
+
if (!globalConfig.verbose) return;
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
function generateTraceId() {
|
|
257
|
+
traceCounter++;
|
|
258
|
+
const timestamp = Date.now().toString(36);
|
|
259
|
+
const counter = traceCounter.toString(36).padStart(4, "0");
|
|
260
|
+
return `${timestamp}-${counter}`;
|
|
261
|
+
}
|
|
262
|
+
class PerformanceTimer {
|
|
263
|
+
constructor(operation, context) {
|
|
264
|
+
__publicField(this, "startTime");
|
|
265
|
+
__publicField(this, "context");
|
|
266
|
+
__publicField(this, "operation");
|
|
267
|
+
this.operation = operation;
|
|
268
|
+
this.context = context || {};
|
|
269
|
+
this.startTime = performance.now();
|
|
270
|
+
logger.debug(`\u23F1\uFE0F Starting: ${operation}`, this.context);
|
|
271
|
+
}
|
|
272
|
+
end(message) {
|
|
273
|
+
const duration = Math.round(performance.now() - this.startTime);
|
|
274
|
+
const msg = message || `\u2713 Completed: ${this.operation}`;
|
|
275
|
+
logger.debug(msg, __spreadProps(__spreadValues({}, this.context), { duration }));
|
|
276
|
+
return duration;
|
|
277
|
+
}
|
|
278
|
+
checkpoint(label) {
|
|
279
|
+
const elapsed = Math.round(performance.now() - this.startTime);
|
|
280
|
+
logger.debug(` \u21B3 ${label}`, __spreadProps(__spreadValues({}, this.context), { duration: elapsed }));
|
|
281
|
+
return elapsed;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
class RequestContext {
|
|
285
|
+
constructor(method, path) {
|
|
286
|
+
__publicField(this, "traceId");
|
|
287
|
+
__publicField(this, "method");
|
|
288
|
+
__publicField(this, "path");
|
|
289
|
+
__publicField(this, "startTime");
|
|
290
|
+
__publicField(this, "checkpoints", []);
|
|
291
|
+
this.traceId = generateTraceId();
|
|
292
|
+
this.method = method;
|
|
293
|
+
this.path = path;
|
|
294
|
+
this.startTime = performance.now();
|
|
295
|
+
logger.debug(`\u2192 ${method} ${path}`, { traceId: this.traceId, module: "HTTP" });
|
|
296
|
+
}
|
|
297
|
+
checkpoint(label) {
|
|
298
|
+
const elapsed = Math.round(performance.now() - this.startTime);
|
|
299
|
+
this.checkpoints.push({ time: elapsed, label });
|
|
300
|
+
logger.debug(` \u2192 ${label}`, { traceId: this.traceId, duration: elapsed });
|
|
301
|
+
}
|
|
302
|
+
end(statusCode) {
|
|
303
|
+
const duration = Math.round(performance.now() - this.startTime);
|
|
304
|
+
const statusColor = statusCode < 400 ? COLORS.green : COLORS.red;
|
|
305
|
+
logger.debug(`\u2190 ${this.method} ${this.path} ${statusColor}${statusCode}${COLORS.reset}`, {
|
|
306
|
+
traceId: this.traceId,
|
|
307
|
+
duration,
|
|
308
|
+
checkpoints: this.checkpoints.length
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
error(error) {
|
|
312
|
+
const duration = Math.round(performance.now() - this.startTime);
|
|
313
|
+
logger.error(`\u2717 ${this.method} ${this.path}`, {
|
|
314
|
+
traceId: this.traceId,
|
|
315
|
+
duration,
|
|
316
|
+
error
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
function createLogger(module2) {
|
|
321
|
+
return {
|
|
322
|
+
debug(message, context, ...args) {
|
|
323
|
+
logger.debug(message, __spreadProps(__spreadValues({}, context), { module: module2 }), ...args);
|
|
324
|
+
},
|
|
325
|
+
info(message, context, ...args) {
|
|
326
|
+
logger.info(message, __spreadProps(__spreadValues({}, context), { module: module2 }), ...args);
|
|
327
|
+
},
|
|
328
|
+
warn(message, context, ...args) {
|
|
329
|
+
logger.warn(message, __spreadProps(__spreadValues({}, context), { module: module2 }), ...args);
|
|
330
|
+
},
|
|
331
|
+
error(message, context, ...args) {
|
|
332
|
+
logger.error(message, __spreadProps(__spreadValues({}, context), { module: module2 }), ...args);
|
|
333
|
+
},
|
|
334
|
+
timer(operation, context) {
|
|
335
|
+
return new PerformanceTimer(operation, __spreadProps(__spreadValues({}, context), { module: module2 }));
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
function logMethod(target, propertyKey, descriptor) {
|
|
340
|
+
const originalMethod = descriptor.value;
|
|
341
|
+
const className = target.constructor.name;
|
|
342
|
+
descriptor.value = function(...args) {
|
|
343
|
+
return __async(this, null, function* () {
|
|
344
|
+
const timer = new PerformanceTimer(`${className}.${propertyKey}`);
|
|
345
|
+
try {
|
|
346
|
+
const result = yield originalMethod.apply(this, args);
|
|
347
|
+
timer.end();
|
|
348
|
+
return result;
|
|
349
|
+
} catch (error) {
|
|
350
|
+
timer.end("\u274C Failed");
|
|
351
|
+
throw error;
|
|
352
|
+
}
|
|
353
|
+
});
|
|
354
|
+
};
|
|
355
|
+
return descriptor;
|
|
356
|
+
}
|
|
357
|
+
function formatBytes(bytes) {
|
|
358
|
+
if (bytes === 0) return "0B";
|
|
359
|
+
const k = 1024;
|
|
360
|
+
const sizes = ["B", "KB", "MB", "GB"];
|
|
361
|
+
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
362
|
+
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))}${sizes[i]}`;
|
|
363
|
+
}
|
|
364
|
+
function formatDuration(ms) {
|
|
365
|
+
if (ms < 1e3) return `${ms}ms`;
|
|
366
|
+
if (ms < 6e4) return `${(ms / 1e3).toFixed(2)}s`;
|
|
367
|
+
const minutes = Math.floor(ms / 6e4);
|
|
368
|
+
const seconds = Math.round(ms % 6e4 / 1e3);
|
|
369
|
+
return `${minutes}m ${seconds}s`;
|
|
370
|
+
}
|
|
371
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
372
|
+
0 && (module.exports = {
|
|
373
|
+
LogLevel,
|
|
374
|
+
PerformanceTimer,
|
|
375
|
+
RequestContext,
|
|
376
|
+
configureLogger,
|
|
377
|
+
createLogger,
|
|
378
|
+
formatBytes,
|
|
379
|
+
formatDuration,
|
|
380
|
+
generateTraceId,
|
|
381
|
+
logMethod,
|
|
382
|
+
logger,
|
|
383
|
+
setVerbose
|
|
384
|
+
});
|