@kb-labs/shared-cli-ui 1.0.0
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/README.md +342 -0
- package/dist/debug.cjs +765 -0
- package/dist/debug.cjs.map +1 -0
- package/dist/debug.d.cts +151 -0
- package/dist/debug.d.ts +151 -0
- package/dist/debug.js +735 -0
- package/dist/debug.js.map +1 -0
- package/dist/index.cjs +2629 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +1037 -0
- package/dist/index.d.ts +1037 -0
- package/dist/index.js +2516 -0
- package/dist/index.js.map +1 -0
- package/package.json +71 -0
package/dist/debug.cjs
ADDED
|
@@ -0,0 +1,765 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/debug/formatters/ai.ts
|
|
4
|
+
function formatDebugEntryAI(entry) {
|
|
5
|
+
return JSON.stringify(entry, null, 2);
|
|
6
|
+
}
|
|
7
|
+
function formatDebugEntriesAI(entries) {
|
|
8
|
+
return JSON.stringify(entries, null, 2);
|
|
9
|
+
}
|
|
10
|
+
function shouldUseAIFormat(format, jsonMode) {
|
|
11
|
+
if (jsonMode) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
return format === "ai";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// src/colors.ts
|
|
18
|
+
var CSI = "\x1B[";
|
|
19
|
+
var RESET = "\x1B[0m";
|
|
20
|
+
var createColor = (...codes) => (text) => `${CSI}${codes.join(";")}m${text}${RESET}`;
|
|
21
|
+
var accentBlue = "38;5;39";
|
|
22
|
+
var accentViolet = "38;5;99";
|
|
23
|
+
var accentTeal = "38;5;51";
|
|
24
|
+
var accentIndigo = "38;5;63";
|
|
25
|
+
var neutral = 37;
|
|
26
|
+
var neutralMuted = 90;
|
|
27
|
+
var colors = {
|
|
28
|
+
// Semantic colors
|
|
29
|
+
success: createColor(32),
|
|
30
|
+
error: createColor(31),
|
|
31
|
+
warning: createColor(33),
|
|
32
|
+
info: createColor(36),
|
|
33
|
+
// Accent palette (reused across CLI)
|
|
34
|
+
primary: createColor(accentBlue),
|
|
35
|
+
accent: createColor(accentViolet),
|
|
36
|
+
highlight: createColor(accentTeal),
|
|
37
|
+
secondary: createColor(accentIndigo),
|
|
38
|
+
emphasis: createColor("38;5;117"),
|
|
39
|
+
muted: createColor(neutralMuted),
|
|
40
|
+
foreground: createColor(neutral),
|
|
41
|
+
// Formatting helpers
|
|
42
|
+
dim: createColor(2),
|
|
43
|
+
bold: createColor(1),
|
|
44
|
+
underline: createColor(4),
|
|
45
|
+
inverse: createColor(7)
|
|
46
|
+
};
|
|
47
|
+
var symbolCharacters = {
|
|
48
|
+
success: "OK",
|
|
49
|
+
error: "ERR",
|
|
50
|
+
warning: "WARN",
|
|
51
|
+
info: "\u2139",
|
|
52
|
+
bullet: "\u2022",
|
|
53
|
+
clock: "TIME",
|
|
54
|
+
folder: "DIR",
|
|
55
|
+
package: "\u203A",
|
|
56
|
+
pointer: "\u203A",
|
|
57
|
+
section: "\u2502"
|
|
58
|
+
};
|
|
59
|
+
var symbols = {
|
|
60
|
+
success: colors.success(symbolCharacters.success),
|
|
61
|
+
error: colors.error(symbolCharacters.error),
|
|
62
|
+
warning: colors.warning(symbolCharacters.warning),
|
|
63
|
+
info: colors.info(symbolCharacters.info),
|
|
64
|
+
bullet: colors.muted(symbolCharacters.bullet),
|
|
65
|
+
clock: colors.info(symbolCharacters.clock),
|
|
66
|
+
folder: colors.primary(symbolCharacters.folder),
|
|
67
|
+
package: colors.accent(symbolCharacters.package),
|
|
68
|
+
pointer: colors.primary(symbolCharacters.pointer),
|
|
69
|
+
section: colors.primary(symbolCharacters.section)
|
|
70
|
+
};
|
|
71
|
+
var isTruthyEnv = (value) => {
|
|
72
|
+
if (!value) {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
const normalized = value.trim().toLowerCase();
|
|
76
|
+
return normalized !== "" && normalized !== "0" && normalized !== "false" && normalized !== "off";
|
|
77
|
+
};
|
|
78
|
+
var supportsColor = (() => {
|
|
79
|
+
if (typeof process === "undefined") {
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
const forceColor = process.env.FORCE_COLOR;
|
|
83
|
+
if (isTruthyEnv(process.env.NO_COLOR)) {
|
|
84
|
+
return false;
|
|
85
|
+
}
|
|
86
|
+
if (isTruthyEnv(forceColor)) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
if (!process.stdout) {
|
|
90
|
+
return false;
|
|
91
|
+
}
|
|
92
|
+
if (process.stdout.isTTY === false) {
|
|
93
|
+
return false;
|
|
94
|
+
}
|
|
95
|
+
return true;
|
|
96
|
+
})();
|
|
97
|
+
var passthrough = (fn) => (text) => supportsColor ? fn(text) : text;
|
|
98
|
+
var safeColors = {
|
|
99
|
+
success: passthrough(colors.success),
|
|
100
|
+
error: passthrough(colors.error),
|
|
101
|
+
warning: passthrough(colors.warning),
|
|
102
|
+
info: passthrough(colors.info),
|
|
103
|
+
accent: passthrough(colors.accent),
|
|
104
|
+
primary: passthrough(colors.primary),
|
|
105
|
+
highlight: passthrough(colors.highlight),
|
|
106
|
+
secondary: passthrough(colors.secondary),
|
|
107
|
+
emphasis: passthrough(colors.emphasis),
|
|
108
|
+
foreground: passthrough(colors.foreground),
|
|
109
|
+
muted: passthrough(colors.muted),
|
|
110
|
+
dim: passthrough(colors.dim),
|
|
111
|
+
bold: passthrough(colors.bold),
|
|
112
|
+
underline: passthrough(colors.underline),
|
|
113
|
+
inverse: passthrough(colors.inverse)
|
|
114
|
+
};
|
|
115
|
+
var safeSymbols = {
|
|
116
|
+
error: supportsColor ? symbols.error : "\u2717",
|
|
117
|
+
warning: supportsColor ? symbols.warning : "\u26A0"};
|
|
118
|
+
|
|
119
|
+
// src/debug/formatters/human.ts
|
|
120
|
+
function formatTimestamp(timestamp) {
|
|
121
|
+
const date = new Date(timestamp);
|
|
122
|
+
return date.toISOString();
|
|
123
|
+
}
|
|
124
|
+
function formatDuration(duration) {
|
|
125
|
+
if (duration < 1e3) {
|
|
126
|
+
return `${duration}ms`;
|
|
127
|
+
}
|
|
128
|
+
return `${(duration / 1e3).toFixed(2)}s`;
|
|
129
|
+
}
|
|
130
|
+
function formatDebugEntryHuman(entry, options = {}) {
|
|
131
|
+
const parts = [];
|
|
132
|
+
if (options.showTimestamp) {
|
|
133
|
+
parts.push(safeColors.dim(formatTimestamp(entry.timestamp)));
|
|
134
|
+
}
|
|
135
|
+
parts.push(safeColors.bold(`[${entry.namespace}]`));
|
|
136
|
+
parts.push(entry.message);
|
|
137
|
+
if (options.showDuration && typeof entry.duration === "number") {
|
|
138
|
+
parts.push(safeColors.dim(`(${formatDuration(entry.duration)})`));
|
|
139
|
+
}
|
|
140
|
+
if (entry.level === "error") {
|
|
141
|
+
parts.push(safeColors.error(safeSymbols.error));
|
|
142
|
+
} else if (entry.level === "warn") {
|
|
143
|
+
parts.push(safeColors.warning(safeSymbols.warning));
|
|
144
|
+
}
|
|
145
|
+
return parts.join(" ");
|
|
146
|
+
}
|
|
147
|
+
function formatDebugEntriesHuman(entries, options = {}) {
|
|
148
|
+
if (entries.length === 0) {
|
|
149
|
+
return "";
|
|
150
|
+
}
|
|
151
|
+
const sorted = [...entries].sort((a, b) => a.timestamp - b.timestamp);
|
|
152
|
+
const lines = [];
|
|
153
|
+
if (options.groupBy === "namespace") {
|
|
154
|
+
const namespaces = /* @__PURE__ */ new Map();
|
|
155
|
+
for (const entry of sorted) {
|
|
156
|
+
if (!namespaces.has(entry.namespace)) {
|
|
157
|
+
namespaces.set(entry.namespace, []);
|
|
158
|
+
}
|
|
159
|
+
namespaces.get(entry.namespace).push(entry);
|
|
160
|
+
}
|
|
161
|
+
for (const [namespace, namespaceEntries] of namespaces) {
|
|
162
|
+
lines.push(safeColors.bold(namespace));
|
|
163
|
+
for (const entry of namespaceEntries) {
|
|
164
|
+
lines.push(` ${formatDebugEntryHuman(entry, options)}`);
|
|
165
|
+
}
|
|
166
|
+
lines.push("");
|
|
167
|
+
}
|
|
168
|
+
return lines.join("\n").trimEnd();
|
|
169
|
+
}
|
|
170
|
+
if (options.groupBy === "group") {
|
|
171
|
+
const groups = /* @__PURE__ */ new Map();
|
|
172
|
+
for (const entry of sorted) {
|
|
173
|
+
const groupName = entry.group ?? "default";
|
|
174
|
+
if (!groups.has(groupName)) {
|
|
175
|
+
groups.set(groupName, []);
|
|
176
|
+
}
|
|
177
|
+
groups.get(groupName).push(entry);
|
|
178
|
+
}
|
|
179
|
+
for (const [groupName, groupEntries] of groups) {
|
|
180
|
+
lines.push(safeColors.bold(groupName));
|
|
181
|
+
for (const entry of groupEntries) {
|
|
182
|
+
lines.push(` ${formatDebugEntryHuman(entry, options)}`);
|
|
183
|
+
}
|
|
184
|
+
lines.push("");
|
|
185
|
+
}
|
|
186
|
+
return lines.join("\n").trimEnd();
|
|
187
|
+
}
|
|
188
|
+
for (const entry of sorted) {
|
|
189
|
+
lines.push(formatDebugEntryHuman(entry, options));
|
|
190
|
+
}
|
|
191
|
+
return lines.join("\n");
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// src/debug/utilities.ts
|
|
195
|
+
function toRegex(pattern) {
|
|
196
|
+
if (!pattern.includes("*")) {
|
|
197
|
+
return new RegExp(`^${pattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`, "i");
|
|
198
|
+
}
|
|
199
|
+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&").replace(/\*/g, ".*");
|
|
200
|
+
return new RegExp(`^${escaped}$`, "i");
|
|
201
|
+
}
|
|
202
|
+
function filterByNamespace(entries, pattern) {
|
|
203
|
+
const matcher = typeof pattern === "string" ? toRegex(pattern) : pattern;
|
|
204
|
+
return entries.filter((entry) => matcher.test(entry.namespace));
|
|
205
|
+
}
|
|
206
|
+
function filterByLevel(entries, level) {
|
|
207
|
+
const allowed = Array.isArray(level) ? new Set(level) : /* @__PURE__ */ new Set([level]);
|
|
208
|
+
return entries.filter((entry) => allowed.has(entry.level));
|
|
209
|
+
}
|
|
210
|
+
function filterByTimeRange(entries, from, to) {
|
|
211
|
+
return entries.filter((entry) => {
|
|
212
|
+
if (from !== void 0 && entry.timestamp < from) {
|
|
213
|
+
return false;
|
|
214
|
+
}
|
|
215
|
+
if (to !== void 0 && entry.timestamp > to) {
|
|
216
|
+
return false;
|
|
217
|
+
}
|
|
218
|
+
return true;
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
function searchInLogs(entries, query) {
|
|
222
|
+
const lowered = query.toLowerCase();
|
|
223
|
+
return entries.filter((entry) => {
|
|
224
|
+
if (entry.message.toLowerCase().includes(lowered)) {
|
|
225
|
+
return true;
|
|
226
|
+
}
|
|
227
|
+
if (entry.namespace.toLowerCase().includes(lowered)) {
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
if (entry.meta) {
|
|
231
|
+
const metaString = JSON.stringify(entry.meta).toLowerCase();
|
|
232
|
+
if (metaString.includes(lowered)) {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
return false;
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
function filterDebugEntries(entries, options) {
|
|
240
|
+
let filtered = [...entries];
|
|
241
|
+
if (options.namespace) {
|
|
242
|
+
filtered = filterByNamespace(filtered, options.namespace);
|
|
243
|
+
}
|
|
244
|
+
if (options.level) {
|
|
245
|
+
filtered = filterByLevel(filtered, options.level);
|
|
246
|
+
}
|
|
247
|
+
if (options.timeRange) {
|
|
248
|
+
filtered = filterByTimeRange(filtered, options.timeRange.from, options.timeRange.to);
|
|
249
|
+
}
|
|
250
|
+
if (options.search) {
|
|
251
|
+
filtered = searchInLogs(filtered, options.search);
|
|
252
|
+
}
|
|
253
|
+
if (options.traceId) {
|
|
254
|
+
filtered = filtered.filter((entry) => entry.traceId === options.traceId);
|
|
255
|
+
}
|
|
256
|
+
return filtered;
|
|
257
|
+
}
|
|
258
|
+
function groupByNamespace(entries) {
|
|
259
|
+
const map = /* @__PURE__ */ new Map();
|
|
260
|
+
for (const entry of entries) {
|
|
261
|
+
if (!map.has(entry.namespace)) {
|
|
262
|
+
map.set(entry.namespace, []);
|
|
263
|
+
}
|
|
264
|
+
map.get(entry.namespace).push(entry);
|
|
265
|
+
}
|
|
266
|
+
return map;
|
|
267
|
+
}
|
|
268
|
+
function groupByGroup(entries) {
|
|
269
|
+
const map = /* @__PURE__ */ new Map();
|
|
270
|
+
for (const entry of entries) {
|
|
271
|
+
const group = entry.group ?? "default";
|
|
272
|
+
if (!map.has(group)) {
|
|
273
|
+
map.set(group, []);
|
|
274
|
+
}
|
|
275
|
+
map.get(group).push(entry);
|
|
276
|
+
}
|
|
277
|
+
return map;
|
|
278
|
+
}
|
|
279
|
+
function createDebugTree(entries) {
|
|
280
|
+
if (entries.length === 0) {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
const sorted = [...entries].sort((a, b) => a.timestamp - b.timestamp);
|
|
284
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
285
|
+
let root = null;
|
|
286
|
+
function ensureNode(entry) {
|
|
287
|
+
if (entry.spanId && nodes.has(entry.spanId)) {
|
|
288
|
+
return nodes.get(entry.spanId);
|
|
289
|
+
}
|
|
290
|
+
const node = { entry, children: [], depth: 0 };
|
|
291
|
+
if (entry.spanId) {
|
|
292
|
+
nodes.set(entry.spanId, node);
|
|
293
|
+
}
|
|
294
|
+
return node;
|
|
295
|
+
}
|
|
296
|
+
for (const entry of sorted) {
|
|
297
|
+
const node = ensureNode(entry);
|
|
298
|
+
if (entry.parentSpanId && nodes.has(entry.parentSpanId)) {
|
|
299
|
+
const parent = nodes.get(entry.parentSpanId);
|
|
300
|
+
node.depth = parent.depth + 1;
|
|
301
|
+
parent.children.push(node);
|
|
302
|
+
} else if (root === null) {
|
|
303
|
+
root = node;
|
|
304
|
+
} else {
|
|
305
|
+
node.depth = root.depth + 1;
|
|
306
|
+
root.children.push(node);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
return root;
|
|
310
|
+
}
|
|
311
|
+
function exportToJSON(entries, options = {}) {
|
|
312
|
+
const filtered = options.filter ? filterDebugEntries(entries, options.filter) : entries;
|
|
313
|
+
if (options.includeMeta === false) {
|
|
314
|
+
return JSON.stringify(
|
|
315
|
+
filtered.map(({ meta, ...rest }) => rest),
|
|
316
|
+
null,
|
|
317
|
+
2
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
return JSON.stringify(filtered, null, 2);
|
|
321
|
+
}
|
|
322
|
+
function exportToChromeFormat(entries, options = {}) {
|
|
323
|
+
const filtered = options.filter ? filterDebugEntries(entries, options.filter) : entries;
|
|
324
|
+
const traceEvents = filtered.map((entry) => ({
|
|
325
|
+
name: entry.message,
|
|
326
|
+
cat: entry.namespace,
|
|
327
|
+
ph: entry.level === "error" ? "E" : entry.level === "warn" ? "W" : "X",
|
|
328
|
+
ts: entry.timestamp * 1e3,
|
|
329
|
+
dur: (entry.duration ?? 0) * 1e3,
|
|
330
|
+
pid: 1,
|
|
331
|
+
tid: entry.spanId ?? entry.namespace,
|
|
332
|
+
args: entry.meta ?? {}
|
|
333
|
+
}));
|
|
334
|
+
return JSON.stringify({ traceEvents }, null, 2);
|
|
335
|
+
}
|
|
336
|
+
function exportToPlainText(entries, options = {}) {
|
|
337
|
+
const filtered = options.filter ? filterDebugEntries(entries, options.filter) : entries;
|
|
338
|
+
return formatDebugEntriesHuman(filtered, { groupBy: "namespace", showTimestamp: true, showDuration: true });
|
|
339
|
+
}
|
|
340
|
+
function exportDebugEntries(entries, options) {
|
|
341
|
+
const format = options.format ?? "json";
|
|
342
|
+
switch (format) {
|
|
343
|
+
case "chrome":
|
|
344
|
+
return exportToChromeFormat(entries, options);
|
|
345
|
+
case "text":
|
|
346
|
+
return exportToPlainText(entries, options);
|
|
347
|
+
case "json":
|
|
348
|
+
default:
|
|
349
|
+
return exportToJSON(entries, options);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
function describeEntriesHuman(entries, options = {}) {
|
|
353
|
+
const filtered = filterDebugEntries(entries, options);
|
|
354
|
+
return formatDebugEntriesHuman(filtered, { showTimestamp: true, showDuration: true, groupBy: "namespace" });
|
|
355
|
+
}
|
|
356
|
+
function describeEntriesAI(entries, options = {}) {
|
|
357
|
+
const filtered = filterDebugEntries(entries, options);
|
|
358
|
+
return formatDebugEntriesAI(filtered);
|
|
359
|
+
}
|
|
360
|
+
function describeEntriesTimeline(entries, options = {}) {
|
|
361
|
+
const filtered = filterDebugEntries(entries, options);
|
|
362
|
+
return formatTimelineWithSummary(filtered);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
// src/debug/formatters/timeline.ts
|
|
366
|
+
function formatDuration2(duration) {
|
|
367
|
+
if (duration < 1e3) {
|
|
368
|
+
return `${duration}ms`;
|
|
369
|
+
}
|
|
370
|
+
return `${(duration / 1e3).toFixed(2)}s`;
|
|
371
|
+
}
|
|
372
|
+
function formatTimelineNode(node, isLast, prefix = "", maxDepth) {
|
|
373
|
+
const lines = [];
|
|
374
|
+
const connector = isLast ? "\u2514\u2500" : "\u251C\u2500";
|
|
375
|
+
const parts = [];
|
|
376
|
+
parts.push(safeColors.bold(`[${node.entry.namespace}]`));
|
|
377
|
+
parts.push(node.entry.message);
|
|
378
|
+
if (typeof node.entry.duration === "number") {
|
|
379
|
+
parts.push(safeColors.dim(`(${formatDuration2(node.entry.duration)})`));
|
|
380
|
+
}
|
|
381
|
+
if (node.entry.level === "error") {
|
|
382
|
+
parts.push(safeColors.error(safeSymbols.error));
|
|
383
|
+
} else if (node.entry.level === "warn") {
|
|
384
|
+
parts.push(safeColors.warning(safeSymbols.warning));
|
|
385
|
+
}
|
|
386
|
+
lines.push(`${prefix}${connector} ${parts.join(" ")}`.trimEnd());
|
|
387
|
+
if (maxDepth !== void 0 && node.depth + 1 >= maxDepth) {
|
|
388
|
+
return lines;
|
|
389
|
+
}
|
|
390
|
+
const childPrefix = prefix + (isLast ? " " : "\u2502 ");
|
|
391
|
+
for (let index = 0; index < node.children.length; index++) {
|
|
392
|
+
const child = node.children[index];
|
|
393
|
+
if (!child) {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
const childIsLast = index === node.children.length - 1;
|
|
397
|
+
lines.push(...formatTimelineNode(child, childIsLast, childPrefix, maxDepth));
|
|
398
|
+
}
|
|
399
|
+
return lines;
|
|
400
|
+
}
|
|
401
|
+
function formatTimeline(entries) {
|
|
402
|
+
if (entries.length === 0) {
|
|
403
|
+
return "";
|
|
404
|
+
}
|
|
405
|
+
const tree = createDebugTree(entries);
|
|
406
|
+
if (!tree) {
|
|
407
|
+
return "";
|
|
408
|
+
}
|
|
409
|
+
return formatTimelineNode(tree, true).join("\n");
|
|
410
|
+
}
|
|
411
|
+
function formatTimelineWithSummary(entries) {
|
|
412
|
+
const timeline = formatTimeline(entries);
|
|
413
|
+
if (timeline.length === 0) {
|
|
414
|
+
return "";
|
|
415
|
+
}
|
|
416
|
+
const totalDuration = entries.reduce((acc, entry) => acc + (entry.duration ?? 0), 0);
|
|
417
|
+
const errorCount = entries.filter((entry) => entry.level === "error").length;
|
|
418
|
+
const warnCount = entries.filter((entry) => entry.level === "warn").length;
|
|
419
|
+
const summaryParts = [];
|
|
420
|
+
if (totalDuration > 0) {
|
|
421
|
+
summaryParts.push(`Total: ${formatDuration2(totalDuration)}`);
|
|
422
|
+
}
|
|
423
|
+
if (errorCount > 0) {
|
|
424
|
+
summaryParts.push(safeColors.error(`${errorCount} errors`));
|
|
425
|
+
}
|
|
426
|
+
if (warnCount > 0) {
|
|
427
|
+
summaryParts.push(safeColors.warning(`${warnCount} warnings`));
|
|
428
|
+
}
|
|
429
|
+
if (summaryParts.length === 0) {
|
|
430
|
+
return timeline;
|
|
431
|
+
}
|
|
432
|
+
return `${timeline}
|
|
433
|
+
|
|
434
|
+
${safeColors.bold("Summary:")} ${summaryParts.join(", ")}`;
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// src/format.ts
|
|
438
|
+
var ANSI_PATTERN = /\u001B\[[0-9;]*m/g;
|
|
439
|
+
function stripAnsi(input) {
|
|
440
|
+
return input.replace(ANSI_PATTERN, "");
|
|
441
|
+
}
|
|
442
|
+
function visibleLength(input) {
|
|
443
|
+
return stripAnsi(input).length;
|
|
444
|
+
}
|
|
445
|
+
function box(title, content = [], maxWidth) {
|
|
446
|
+
const lines = content.length > 0 ? content : [""];
|
|
447
|
+
const titleWidth = visibleLength(title);
|
|
448
|
+
const terminalWidth = typeof process !== "undefined" && process.stdout?.columns ? process.stdout.columns : 80;
|
|
449
|
+
const effectiveMaxWidth = Math.min(terminalWidth - 4, 120);
|
|
450
|
+
const bodyWidth = Math.min(
|
|
451
|
+
effectiveMaxWidth,
|
|
452
|
+
Math.max(titleWidth, ...lines.map((line) => visibleLength(line)))
|
|
453
|
+
);
|
|
454
|
+
const wrappedLines = [];
|
|
455
|
+
for (const line of lines) {
|
|
456
|
+
const lineLength = visibleLength(line);
|
|
457
|
+
if (lineLength <= bodyWidth) {
|
|
458
|
+
wrappedLines.push(line);
|
|
459
|
+
} else {
|
|
460
|
+
const words = line.split(/(\s+)/);
|
|
461
|
+
let currentLine = "";
|
|
462
|
+
for (const word of words) {
|
|
463
|
+
const testLine = currentLine + word;
|
|
464
|
+
if (visibleLength(testLine) <= bodyWidth) {
|
|
465
|
+
currentLine = testLine;
|
|
466
|
+
} else {
|
|
467
|
+
if (currentLine) {
|
|
468
|
+
wrappedLines.push(currentLine.trimEnd());
|
|
469
|
+
}
|
|
470
|
+
if (visibleLength(word) > bodyWidth) {
|
|
471
|
+
wrappedLines.push(truncate(word, bodyWidth));
|
|
472
|
+
currentLine = "";
|
|
473
|
+
} else {
|
|
474
|
+
currentLine = word;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
if (currentLine) {
|
|
479
|
+
wrappedLines.push(currentLine.trimEnd());
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
const topBorder = `\u250C${"\u2500".repeat(bodyWidth + 2)}\u2510`;
|
|
484
|
+
const titleLine = `\u2502 ${safeColors.bold(title)}${" ".repeat(Math.max(0, bodyWidth - titleWidth))} \u2502`;
|
|
485
|
+
const bodyLines = wrappedLines.map((line) => {
|
|
486
|
+
const padding = Math.max(0, bodyWidth - visibleLength(line));
|
|
487
|
+
return `\u2502 ${line}${" ".repeat(padding)} \u2502`;
|
|
488
|
+
});
|
|
489
|
+
const bottomBorder = `\u2514${"\u2500".repeat(bodyWidth + 2)}\u2518`;
|
|
490
|
+
return [topBorder, titleLine, ...bodyLines, bottomBorder].join("\n");
|
|
491
|
+
}
|
|
492
|
+
function truncate(text, maxLength) {
|
|
493
|
+
if (text.length <= maxLength) {
|
|
494
|
+
return text;
|
|
495
|
+
}
|
|
496
|
+
return text.substring(0, maxLength - 3) + "...";
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
// src/debug/components/output.ts
|
|
500
|
+
function formatDebugOutput(entry, options = {}) {
|
|
501
|
+
if (shouldUseAIFormat(options.format)) {
|
|
502
|
+
return formatDebugEntryAI(entry);
|
|
503
|
+
}
|
|
504
|
+
return formatDebugEntryHuman(entry, options);
|
|
505
|
+
}
|
|
506
|
+
function formatDebugOutputs(entries, options = {}) {
|
|
507
|
+
if (shouldUseAIFormat(options.format)) {
|
|
508
|
+
return formatDebugEntriesAI(entries);
|
|
509
|
+
}
|
|
510
|
+
if (options.useTimeline) {
|
|
511
|
+
return formatTimelineWithSummary(entries);
|
|
512
|
+
}
|
|
513
|
+
return formatDebugEntriesHuman(entries, options);
|
|
514
|
+
}
|
|
515
|
+
var DebugSection = class {
|
|
516
|
+
entries = [];
|
|
517
|
+
title;
|
|
518
|
+
options;
|
|
519
|
+
constructor(title, options = {}) {
|
|
520
|
+
this.title = title;
|
|
521
|
+
this.options = options;
|
|
522
|
+
}
|
|
523
|
+
add(entry) {
|
|
524
|
+
this.entries.push(entry);
|
|
525
|
+
}
|
|
526
|
+
addAll(entries) {
|
|
527
|
+
this.entries.push(...entries);
|
|
528
|
+
}
|
|
529
|
+
clear() {
|
|
530
|
+
this.entries.length = 0;
|
|
531
|
+
}
|
|
532
|
+
format() {
|
|
533
|
+
if (this.entries.length === 0) {
|
|
534
|
+
return "";
|
|
535
|
+
}
|
|
536
|
+
const content = formatDebugOutputs(this.entries, this.options);
|
|
537
|
+
if (!content) {
|
|
538
|
+
return "";
|
|
539
|
+
}
|
|
540
|
+
if (shouldUseAIFormat(this.options.format)) {
|
|
541
|
+
return content;
|
|
542
|
+
}
|
|
543
|
+
return box(this.title, content.split("\n"));
|
|
544
|
+
}
|
|
545
|
+
get count() {
|
|
546
|
+
return this.entries.length;
|
|
547
|
+
}
|
|
548
|
+
};
|
|
549
|
+
var DebugOutput = class {
|
|
550
|
+
sections = /* @__PURE__ */ new Map();
|
|
551
|
+
globalEntries = [];
|
|
552
|
+
options;
|
|
553
|
+
constructor(options = {}) {
|
|
554
|
+
this.options = options;
|
|
555
|
+
}
|
|
556
|
+
add(entry) {
|
|
557
|
+
this.globalEntries.push(entry);
|
|
558
|
+
}
|
|
559
|
+
addToSection(sectionName, entry) {
|
|
560
|
+
if (!this.sections.has(sectionName)) {
|
|
561
|
+
this.sections.set(sectionName, new DebugSection(sectionName, this.options));
|
|
562
|
+
}
|
|
563
|
+
this.sections.get(sectionName).add(entry);
|
|
564
|
+
}
|
|
565
|
+
getSection(sectionName) {
|
|
566
|
+
if (!this.sections.has(sectionName)) {
|
|
567
|
+
this.sections.set(sectionName, new DebugSection(sectionName, this.options));
|
|
568
|
+
}
|
|
569
|
+
return this.sections.get(sectionName);
|
|
570
|
+
}
|
|
571
|
+
format() {
|
|
572
|
+
const parts = [];
|
|
573
|
+
if (this.globalEntries.length > 0) {
|
|
574
|
+
const formatted = formatDebugOutputs(this.globalEntries, this.options);
|
|
575
|
+
if (formatted) {
|
|
576
|
+
parts.push(formatted);
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
for (const [, section] of this.sections) {
|
|
580
|
+
const formatted = section.format();
|
|
581
|
+
if (formatted) {
|
|
582
|
+
parts.push(formatted);
|
|
583
|
+
}
|
|
584
|
+
}
|
|
585
|
+
return parts.join("\n\n");
|
|
586
|
+
}
|
|
587
|
+
clear() {
|
|
588
|
+
this.globalEntries.length = 0;
|
|
589
|
+
this.sections.clear();
|
|
590
|
+
}
|
|
591
|
+
get totalCount() {
|
|
592
|
+
let count = this.globalEntries.length;
|
|
593
|
+
for (const section of this.sections.values()) {
|
|
594
|
+
count += section.count;
|
|
595
|
+
}
|
|
596
|
+
return count;
|
|
597
|
+
}
|
|
598
|
+
};
|
|
599
|
+
|
|
600
|
+
// src/debug/components/tree.ts
|
|
601
|
+
var DebugTree = class {
|
|
602
|
+
tree = null;
|
|
603
|
+
options;
|
|
604
|
+
constructor(entries, options = {}) {
|
|
605
|
+
this.options = options;
|
|
606
|
+
this.tree = createDebugTree(entries);
|
|
607
|
+
}
|
|
608
|
+
update(entries) {
|
|
609
|
+
this.tree = createDebugTree(entries);
|
|
610
|
+
}
|
|
611
|
+
format() {
|
|
612
|
+
if (!this.tree) {
|
|
613
|
+
return "";
|
|
614
|
+
}
|
|
615
|
+
const lines = formatTimelineNode(this.tree, true, "", this.options.maxDepth);
|
|
616
|
+
if (this.options.showSummary) {
|
|
617
|
+
const summary = this.getSummary();
|
|
618
|
+
if (summary) {
|
|
619
|
+
lines.push("", summary);
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
return lines.join("\n");
|
|
623
|
+
}
|
|
624
|
+
getSummary() {
|
|
625
|
+
if (!this.tree) {
|
|
626
|
+
return "";
|
|
627
|
+
}
|
|
628
|
+
const stats = this.collectStats(this.tree);
|
|
629
|
+
const parts = [];
|
|
630
|
+
if (stats.totalDuration > 0) {
|
|
631
|
+
parts.push(`total ${stats.totalDuration}ms`);
|
|
632
|
+
}
|
|
633
|
+
if (stats.errorCount > 0) {
|
|
634
|
+
parts.push(`${stats.errorCount} errors`);
|
|
635
|
+
}
|
|
636
|
+
if (stats.warnCount > 0) {
|
|
637
|
+
parts.push(`${stats.warnCount} warnings`);
|
|
638
|
+
}
|
|
639
|
+
parts.push(`${stats.nodeCount} nodes`);
|
|
640
|
+
return `Summary: ${parts.join(", ")}`;
|
|
641
|
+
}
|
|
642
|
+
collectStats(node) {
|
|
643
|
+
let totalDuration = node.entry.duration ?? 0;
|
|
644
|
+
let errorCount = node.entry.level === "error" ? 1 : 0;
|
|
645
|
+
let warnCount = node.entry.level === "warn" ? 1 : 0;
|
|
646
|
+
let nodeCount = 1;
|
|
647
|
+
for (const child of node.children) {
|
|
648
|
+
const childStats = this.collectStats(child);
|
|
649
|
+
totalDuration += childStats.totalDuration;
|
|
650
|
+
errorCount += childStats.errorCount;
|
|
651
|
+
warnCount += childStats.warnCount;
|
|
652
|
+
nodeCount += childStats.nodeCount;
|
|
653
|
+
}
|
|
654
|
+
return { totalDuration, errorCount, warnCount, nodeCount };
|
|
655
|
+
}
|
|
656
|
+
get root() {
|
|
657
|
+
return this.tree;
|
|
658
|
+
}
|
|
659
|
+
};
|
|
660
|
+
|
|
661
|
+
// src/debug/components/trace.ts
|
|
662
|
+
var DebugTrace = class {
|
|
663
|
+
entries = [];
|
|
664
|
+
options;
|
|
665
|
+
constructor(entries = [], options = {}) {
|
|
666
|
+
this.entries.push(...entries);
|
|
667
|
+
this.options = options;
|
|
668
|
+
}
|
|
669
|
+
add(entry) {
|
|
670
|
+
this.entries.push(entry);
|
|
671
|
+
}
|
|
672
|
+
addAll(entries) {
|
|
673
|
+
this.entries.push(...entries);
|
|
674
|
+
}
|
|
675
|
+
clear() {
|
|
676
|
+
this.entries.length = 0;
|
|
677
|
+
}
|
|
678
|
+
format() {
|
|
679
|
+
if (this.entries.length === 0) {
|
|
680
|
+
return "";
|
|
681
|
+
}
|
|
682
|
+
const sorted = [...this.entries].sort((a, b) => a.timestamp - b.timestamp);
|
|
683
|
+
if (this.options.groupByPlugin) {
|
|
684
|
+
return this.formatGrouped(sorted);
|
|
685
|
+
}
|
|
686
|
+
return formatTimelineWithSummary(sorted);
|
|
687
|
+
}
|
|
688
|
+
formatGrouped(entries) {
|
|
689
|
+
const groups = /* @__PURE__ */ new Map();
|
|
690
|
+
for (const entry of entries) {
|
|
691
|
+
const plugin = entry.namespace.split(":")[0] ?? entry.namespace;
|
|
692
|
+
if (!groups.has(plugin)) {
|
|
693
|
+
groups.set(plugin, []);
|
|
694
|
+
}
|
|
695
|
+
groups.get(plugin).push(entry);
|
|
696
|
+
}
|
|
697
|
+
const parts = [];
|
|
698
|
+
for (const [plugin, pluginEntries] of groups) {
|
|
699
|
+
const tree = new DebugTree(pluginEntries, {
|
|
700
|
+
showSummary: this.options.showSummary,
|
|
701
|
+
maxDepth: this.options.maxDepth
|
|
702
|
+
});
|
|
703
|
+
const formatted = tree.format();
|
|
704
|
+
if (formatted) {
|
|
705
|
+
parts.push(`[${plugin}]`, formatted);
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
return parts.join("\n\n");
|
|
709
|
+
}
|
|
710
|
+
getStats() {
|
|
711
|
+
const plugins = /* @__PURE__ */ new Set();
|
|
712
|
+
let totalDuration = 0;
|
|
713
|
+
let errorCount = 0;
|
|
714
|
+
let warnCount = 0;
|
|
715
|
+
for (const entry of this.entries) {
|
|
716
|
+
const plugin = entry.namespace.split(":")[0] ?? entry.namespace;
|
|
717
|
+
plugins.add(plugin);
|
|
718
|
+
totalDuration += entry.duration ?? 0;
|
|
719
|
+
if (entry.level === "error") {
|
|
720
|
+
errorCount += 1;
|
|
721
|
+
} else if (entry.level === "warn") {
|
|
722
|
+
warnCount += 1;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
return {
|
|
726
|
+
totalEntries: this.entries.length,
|
|
727
|
+
plugins: Array.from(plugins),
|
|
728
|
+
totalDuration,
|
|
729
|
+
errorCount,
|
|
730
|
+
warnCount
|
|
731
|
+
};
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
|
|
735
|
+
exports.DebugOutput = DebugOutput;
|
|
736
|
+
exports.DebugSection = DebugSection;
|
|
737
|
+
exports.DebugTrace = DebugTrace;
|
|
738
|
+
exports.DebugTree = DebugTree;
|
|
739
|
+
exports.createDebugTree = createDebugTree;
|
|
740
|
+
exports.describeEntriesAI = describeEntriesAI;
|
|
741
|
+
exports.describeEntriesHuman = describeEntriesHuman;
|
|
742
|
+
exports.describeEntriesTimeline = describeEntriesTimeline;
|
|
743
|
+
exports.exportDebugEntries = exportDebugEntries;
|
|
744
|
+
exports.exportToChromeFormat = exportToChromeFormat;
|
|
745
|
+
exports.exportToJSON = exportToJSON;
|
|
746
|
+
exports.exportToPlainText = exportToPlainText;
|
|
747
|
+
exports.filterByLevel = filterByLevel;
|
|
748
|
+
exports.filterByNamespace = filterByNamespace;
|
|
749
|
+
exports.filterByTimeRange = filterByTimeRange;
|
|
750
|
+
exports.filterDebugEntries = filterDebugEntries;
|
|
751
|
+
exports.formatDebugEntriesAI = formatDebugEntriesAI;
|
|
752
|
+
exports.formatDebugEntriesHuman = formatDebugEntriesHuman;
|
|
753
|
+
exports.formatDebugEntryAI = formatDebugEntryAI;
|
|
754
|
+
exports.formatDebugEntryHuman = formatDebugEntryHuman;
|
|
755
|
+
exports.formatDebugOutput = formatDebugOutput;
|
|
756
|
+
exports.formatDebugOutputs = formatDebugOutputs;
|
|
757
|
+
exports.formatTimeline = formatTimeline;
|
|
758
|
+
exports.formatTimelineNode = formatTimelineNode;
|
|
759
|
+
exports.formatTimelineWithSummary = formatTimelineWithSummary;
|
|
760
|
+
exports.groupByGroup = groupByGroup;
|
|
761
|
+
exports.groupByNamespace = groupByNamespace;
|
|
762
|
+
exports.searchInLogs = searchInLogs;
|
|
763
|
+
exports.shouldUseAIFormat = shouldUseAIFormat;
|
|
764
|
+
//# sourceMappingURL=debug.cjs.map
|
|
765
|
+
//# sourceMappingURL=debug.cjs.map
|