@ai-setting/roy-agent-core 1.5.14-test → 1.5.15-test

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.
Files changed (141) hide show
  1. package/package.json +1 -1
  2. package/dist/config/index.d.ts +0 -1250
  3. package/dist/config/index.js +0 -32
  4. package/dist/env/agent/index.d.ts +0 -2279
  5. package/dist/env/agent/index.js +0 -24
  6. package/dist/env/commands/index.d.ts +0 -1131
  7. package/dist/env/commands/index.js +0 -14
  8. package/dist/env/debug/formatters/index.d.ts +0 -236
  9. package/dist/env/debug/formatters/index.js +0 -11
  10. package/dist/env/debug/index.d.ts +0 -1652
  11. package/dist/env/debug/index.js +0 -26
  12. package/dist/env/hook/index.d.ts +0 -279
  13. package/dist/env/hook/index.js +0 -29
  14. package/dist/env/index.d.ts +0 -3481
  15. package/dist/env/index.js +0 -82
  16. package/dist/env/llm/index.d.ts +0 -1760
  17. package/dist/env/llm/index.js +0 -40
  18. package/dist/env/log-trace/index.d.ts +0 -1574
  19. package/dist/env/log-trace/index.js +0 -83
  20. package/dist/env/mcp/index.d.ts +0 -1331
  21. package/dist/env/mcp/index.js +0 -39
  22. package/dist/env/mcp/tool/index.d.ts +0 -183
  23. package/dist/env/mcp/tool/index.js +0 -14
  24. package/dist/env/memory/built-in/index.d.ts +0 -232
  25. package/dist/env/memory/built-in/index.js +0 -11
  26. package/dist/env/memory/index.d.ts +0 -1799
  27. package/dist/env/memory/index.js +0 -56
  28. package/dist/env/memory/plugin/index.d.ts +0 -747
  29. package/dist/env/memory/plugin/index.js +0 -36
  30. package/dist/env/prompt/index.d.ts +0 -1164
  31. package/dist/env/prompt/index.js +0 -20
  32. package/dist/env/session/index.d.ts +0 -1908
  33. package/dist/env/session/index.js +0 -25
  34. package/dist/env/session/storage/index.d.ts +0 -564
  35. package/dist/env/session/storage/index.js +0 -18
  36. package/dist/env/skill/index.d.ts +0 -1266
  37. package/dist/env/skill/index.js +0 -34
  38. package/dist/env/skill/tool/index.d.ts +0 -193
  39. package/dist/env/skill/tool/index.js +0 -9
  40. package/dist/env/task/delegate/index.d.ts +0 -1612
  41. package/dist/env/task/delegate/index.js +0 -18
  42. package/dist/env/task/events/index.d.ts +0 -171
  43. package/dist/env/task/events/index.js +0 -7
  44. package/dist/env/task/hooks/index.d.ts +0 -624
  45. package/dist/env/task/hooks/index.js +0 -7
  46. package/dist/env/task/index.d.ts +0 -1553
  47. package/dist/env/task/index.js +0 -34
  48. package/dist/env/task/plugins/index.d.ts +0 -466
  49. package/dist/env/task/plugins/index.js +0 -23
  50. package/dist/env/task/storage/index.d.ts +0 -241
  51. package/dist/env/task/storage/index.js +0 -14
  52. package/dist/env/task/tools/index.d.ts +0 -1485
  53. package/dist/env/task/tools/index.js +0 -17
  54. package/dist/env/task/tools/operation/index.d.ts +0 -1484
  55. package/dist/env/task/tools/operation/index.js +0 -15
  56. package/dist/env/tool/built-in/index.d.ts +0 -218
  57. package/dist/env/tool/built-in/index.js +0 -25
  58. package/dist/env/tool/index.d.ts +0 -1396
  59. package/dist/env/tool/index.js +0 -39
  60. package/dist/env/workflow/decorators/index.d.ts +0 -2161
  61. package/dist/env/workflow/decorators/index.js +0 -27
  62. package/dist/env/workflow/engine/index.d.ts +0 -3453
  63. package/dist/env/workflow/engine/index.js +0 -28
  64. package/dist/env/workflow/index.d.ts +0 -3546
  65. package/dist/env/workflow/index.js +0 -136
  66. package/dist/env/workflow/nodes/index.d.ts +0 -2092
  67. package/dist/env/workflow/nodes/index.js +0 -19
  68. package/dist/env/workflow/service/index.d.ts +0 -227
  69. package/dist/env/workflow/service/index.js +0 -13
  70. package/dist/env/workflow/storage/index.d.ts +0 -165
  71. package/dist/env/workflow/storage/index.js +0 -27
  72. package/dist/env/workflow/tools/index.d.ts +0 -416
  73. package/dist/env/workflow/tools/index.js +0 -159
  74. package/dist/env/workflow/types/index.d.ts +0 -2255
  75. package/dist/env/workflow/types/index.js +0 -98
  76. package/dist/env/workflow/utils/index.d.ts +0 -2031
  77. package/dist/env/workflow/utils/index.js +0 -637
  78. package/dist/index.d.ts +0 -7858
  79. package/dist/index.js +0 -399
  80. package/dist/shared/@ai-setting/roy-agent-core-0rtxwr28.js +0 -258
  81. package/dist/shared/@ai-setting/roy-agent-core-0vbdz0x7.js +0 -36
  82. package/dist/shared/@ai-setting/roy-agent-core-1akcqxj9.js +0 -349
  83. package/dist/shared/@ai-setting/roy-agent-core-1ce3fqrk.js +0 -117
  84. package/dist/shared/@ai-setting/roy-agent-core-2dhd60aw.js +0 -11
  85. package/dist/shared/@ai-setting/roy-agent-core-3jywqmdd.js +0 -393
  86. package/dist/shared/@ai-setting/roy-agent-core-3rr5k71j.js +0 -200
  87. package/dist/shared/@ai-setting/roy-agent-core-44hnfb02.js +0 -299
  88. package/dist/shared/@ai-setting/roy-agent-core-4t40mkpv.js +0 -206
  89. package/dist/shared/@ai-setting/roy-agent-core-4txzpsbt.js +0 -393
  90. package/dist/shared/@ai-setting/roy-agent-core-5x94xmt6.js +0 -350
  91. package/dist/shared/@ai-setting/roy-agent-core-69jskqjg.js +0 -180
  92. package/dist/shared/@ai-setting/roy-agent-core-6kvtahqv.js +0 -408
  93. package/dist/shared/@ai-setting/roy-agent-core-7fgf85wc.js +0 -284
  94. package/dist/shared/@ai-setting/roy-agent-core-81w1963m.js +0 -762
  95. package/dist/shared/@ai-setting/roy-agent-core-8gxth0eh.js +0 -10
  96. package/dist/shared/@ai-setting/roy-agent-core-92z6t4he.js +0 -14
  97. package/dist/shared/@ai-setting/roy-agent-core-93zfb3r1.js +0 -922
  98. package/dist/shared/@ai-setting/roy-agent-core-9yxb3ty9.js +0 -15
  99. package/dist/shared/@ai-setting/roy-agent-core-b0x5dda6.js +0 -1130
  100. package/dist/shared/@ai-setting/roy-agent-core-bcbqy27c.js +0 -14
  101. package/dist/shared/@ai-setting/roy-agent-core-bvr1761x.js +0 -653
  102. package/dist/shared/@ai-setting/roy-agent-core-ctdhjv68.js +0 -93
  103. package/dist/shared/@ai-setting/roy-agent-core-d7cyjkf7.js +0 -872
  104. package/dist/shared/@ai-setting/roy-agent-core-dh9d7a3m.js +0 -11
  105. package/dist/shared/@ai-setting/roy-agent-core-e25xkv53.js +0 -64
  106. package/dist/shared/@ai-setting/roy-agent-core-eajcvp4e.js +0 -378
  107. package/dist/shared/@ai-setting/roy-agent-core-f7q2x5z6.js +0 -492
  108. package/dist/shared/@ai-setting/roy-agent-core-fs0mn2jk.js +0 -52
  109. package/dist/shared/@ai-setting/roy-agent-core-g1s2h0e5.js +0 -171
  110. package/dist/shared/@ai-setting/roy-agent-core-g99pxzn5.js +0 -862
  111. package/dist/shared/@ai-setting/roy-agent-core-gbqcyegm.js +0 -1387
  112. package/dist/shared/@ai-setting/roy-agent-core-gjq1yk68.js +0 -208
  113. package/dist/shared/@ai-setting/roy-agent-core-gq20wsgv.js +0 -139
  114. package/dist/shared/@ai-setting/roy-agent-core-gwc4h96n.js +0 -534
  115. package/dist/shared/@ai-setting/roy-agent-core-jfh9q2qh.js +0 -204
  116. package/dist/shared/@ai-setting/roy-agent-core-jvatggbb.js +0 -603
  117. package/dist/shared/@ai-setting/roy-agent-core-kkbwepqb.js +0 -97
  118. package/dist/shared/@ai-setting/roy-agent-core-pjr12nnd.js +0 -587
  119. package/dist/shared/@ai-setting/roy-agent-core-psv4v63c.js +0 -176
  120. package/dist/shared/@ai-setting/roy-agent-core-psvxt4c9.js +0 -60
  121. package/dist/shared/@ai-setting/roy-agent-core-qqceba6k.js +0 -442
  122. package/dist/shared/@ai-setting/roy-agent-core-qxhq8ven.js +0 -57
  123. package/dist/shared/@ai-setting/roy-agent-core-qxnbvgwe.js +0 -66
  124. package/dist/shared/@ai-setting/roy-agent-core-r9ezzemr.js +0 -10
  125. package/dist/shared/@ai-setting/roy-agent-core-rhmtwnw1.js +0 -267
  126. package/dist/shared/@ai-setting/roy-agent-core-rvv6ydff.js +0 -584
  127. package/dist/shared/@ai-setting/roy-agent-core-rvxg1wps.js +0 -102
  128. package/dist/shared/@ai-setting/roy-agent-core-satmq6sh.js +0 -549
  129. package/dist/shared/@ai-setting/roy-agent-core-sx7wsvnn.js +0 -15
  130. package/dist/shared/@ai-setting/roy-agent-core-t94ktchq.js +0 -213
  131. package/dist/shared/@ai-setting/roy-agent-core-vf215qfv.js +0 -812
  132. package/dist/shared/@ai-setting/roy-agent-core-vkz81f7v.js +0 -1316
  133. package/dist/shared/@ai-setting/roy-agent-core-vn2bc59q.js +0 -1205
  134. package/dist/shared/@ai-setting/roy-agent-core-wa1kzqky.js +0 -328
  135. package/dist/shared/@ai-setting/roy-agent-core-wft9ra24.js +0 -20
  136. package/dist/shared/@ai-setting/roy-agent-core-wrcy0h6z.js +0 -2098
  137. package/dist/shared/@ai-setting/roy-agent-core-xq8hhqb8.js +0 -419
  138. package/dist/shared/@ai-setting/roy-agent-core-xs5rsgat.js +0 -368
  139. package/dist/shared/@ai-setting/roy-agent-core-zbkpc41z.js +0 -377
  140. package/dist/shared/@ai-setting/roy-agent-core-zgypchmt.js +0 -172
  141. package/dist/shared/@ai-setting/roy-agent-core-zpn0bqa8.js +0 -103
@@ -1,862 +0,0 @@
1
- import {
2
- TraceFormatter,
3
- TreeFormatter
4
- } from "./roy-agent-core-rvv6ydff.js";
5
- import {
6
- BaseComponent
7
- } from "./roy-agent-core-kkbwepqb.js";
8
- import {
9
- __require
10
- } from "./roy-agent-core-fs0mn2jk.js";
11
-
12
- // src/env/debug/reader/log-reader.ts
13
- import * as fs from "fs";
14
- import * as readline from "readline";
15
- import * as path from "path";
16
- import { glob } from "glob";
17
- function getXDGDataHome() {
18
- return process.env.XDG_DATA_HOME || path.join(process.env.HOME || "/home/" + (process.env.USER || "user"), ".local", "share");
19
- }
20
-
21
- class LogReader {
22
- logDir;
23
- constructor(options) {
24
- this.logDir = options?.logDir || path.join(getXDGDataHome(), "roy-agent", "logs");
25
- }
26
- getLogPath(filename) {
27
- if (!filename) {
28
- return path.join(this.logDir, "app.log");
29
- }
30
- if (path.isAbsolute(filename)) {
31
- return filename;
32
- }
33
- return path.join(this.logDir, filename);
34
- }
35
- async findLogFiles(pattern = "*.log") {
36
- try {
37
- const normalizedDir = this.logDir.replace(/\\/g, "/");
38
- const fullPattern = `${normalizedDir}/${pattern}`;
39
- const files = await glob(fullPattern);
40
- return files.sort();
41
- } catch {
42
- return [];
43
- }
44
- }
45
- getDefaultLogDir() {
46
- return this.logDir;
47
- }
48
- async* readLines(filepath, options) {
49
- const sinceTime = options?.since ? this.parseTime(options.since) : null;
50
- const untilTime = options?.until ? this.parseTime(options.until) : null;
51
- if (!fs.existsSync(filepath)) {
52
- return;
53
- }
54
- const fileStream = fs.createReadStream(filepath, { encoding: "utf-8" });
55
- const rl = readline.createInterface({
56
- input: fileStream,
57
- crlfDelay: Infinity
58
- });
59
- try {
60
- for await (const line of rl) {
61
- if (!line.trim())
62
- continue;
63
- if (sinceTime || untilTime) {
64
- const lineTime = this.extractTimestamp(line);
65
- if (lineTime) {
66
- if (sinceTime && lineTime < sinceTime)
67
- continue;
68
- if (untilTime && lineTime > untilTime)
69
- continue;
70
- }
71
- }
72
- yield line;
73
- }
74
- } finally {
75
- rl.close();
76
- fileStream.destroy();
77
- }
78
- }
79
- extractTimestamp(line) {
80
- const match = line.match(/^(\d{4}-\d{2}-\d{2})[T ](\d{2}:\d{2}:\d{2})/);
81
- if (!match)
82
- return null;
83
- const datePart = match[1];
84
- const timePart = match[2];
85
- const isoString = `${datePart}T${timePart}`;
86
- const timestamp = new Date(isoString).getTime();
87
- return isNaN(timestamp) ? null : timestamp;
88
- }
89
- parseTime(timeStr) {
90
- const now = Date.now();
91
- const normalizedStr = timeStr.replace("T", " ");
92
- const absoluteTime = new Date(normalizedStr).getTime();
93
- if (!isNaN(absoluteTime) && absoluteTime > 0) {
94
- return absoluteTime;
95
- }
96
- const originalTime = new Date(timeStr).getTime();
97
- if (!isNaN(originalTime) && originalTime > 0) {
98
- return originalTime;
99
- }
100
- const relativeMatch = timeStr.match(/^(\d+)([hms])$/);
101
- if (relativeMatch) {
102
- const value = parseInt(relativeMatch[1], 10);
103
- const unit = relativeMatch[2];
104
- switch (unit) {
105
- case "h":
106
- return now - value * 60 * 60 * 1000;
107
- case "m":
108
- return now - value * 60 * 1000;
109
- case "s":
110
- return now - value * 1000;
111
- }
112
- }
113
- return now;
114
- }
115
- async readFile(filepath) {
116
- return fs.promises.readFile(filepath, "utf-8");
117
- }
118
- exists(filepath) {
119
- return fs.existsSync(filepath);
120
- }
121
- }
122
-
123
- // src/env/debug/reader/span-db-reader.ts
124
- class SpanDbReader {
125
- storage = null;
126
- dbPath = null;
127
- sqliteDb = null;
128
- constructor(options) {
129
- if (options?.storage) {
130
- this.storage = options.storage;
131
- } else if (options?.dbPath) {
132
- this.dbPath = options.dbPath;
133
- }
134
- }
135
- async initialize() {
136
- if (this.storage) {
137
- await this.storage.initialize();
138
- } else if (this.dbPath) {
139
- await this.initSqliteStorage();
140
- }
141
- }
142
- async initSqliteStorage() {
143
- try {
144
- const { SQLiteSpanStorage } = await import("./roy-agent-core-r9ezzemr.js");
145
- this.storage = new SQLiteSpanStorage(this.dbPath);
146
- await this.storage.initialize();
147
- } catch (error) {
148
- throw new Error(`Failed to initialize SQLite storage: ${error}`);
149
- }
150
- }
151
- async getTraceEntries(traceId) {
152
- if (!this.storage) {
153
- throw new Error("Storage not initialized. Call initialize() first.");
154
- }
155
- const spans = this.storage.findByTraceId(traceId);
156
- if (spans.length === 0) {
157
- return [];
158
- }
159
- return this.spansToEntries(spans);
160
- }
161
- async listTraceIds(limit = 20) {
162
- if (!this.storage) {
163
- throw new Error("Storage not initialized. Call initialize() first.");
164
- }
165
- const traceInfos = this.storage.listTraces(limit);
166
- const traceIds = traceInfos.map((info) => ({
167
- traceId: info.traceId,
168
- firstTime: new Date(info.startTime).toISOString(),
169
- lastTime: info.endTime ? new Date(info.endTime).toISOString() : undefined,
170
- count: info.spanCount
171
- }));
172
- return {
173
- traceIds,
174
- count: traceInfos.length
175
- };
176
- }
177
- spansToEntries(spans) {
178
- const entries = [];
179
- for (const span of spans) {
180
- entries.push({
181
- traceId: span.traceId,
182
- timestamp: new Date(span.startTime).toISOString(),
183
- function: span.name,
184
- action: "enter",
185
- params: this.attributesToParams(span.attributes),
186
- spanId: span.spanId,
187
- parentSpanId: span.parentSpanId
188
- });
189
- if (span.endTime) {
190
- entries.push({
191
- traceId: span.traceId,
192
- timestamp: new Date(span.endTime).toISOString(),
193
- function: span.name,
194
- action: "quit",
195
- result: span.result,
196
- error: span.error,
197
- durationMs: span.endTime - span.startTime,
198
- spanId: span.spanId,
199
- parentSpanId: span.parentSpanId
200
- });
201
- }
202
- }
203
- entries.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
204
- return entries;
205
- }
206
- attributesToParams(attributes) {
207
- if (!attributes)
208
- return [];
209
- return Object.entries(attributes).map(([key, value]) => ({
210
- key,
211
- value
212
- }));
213
- }
214
- getStorage() {
215
- return this.storage;
216
- }
217
- close() {
218
- if (this.storage) {
219
- this.storage.close();
220
- this.storage = null;
221
- }
222
- if (this.sqliteDb) {
223
- this.sqliteDb.close();
224
- this.sqliteDb = null;
225
- }
226
- }
227
- }
228
-
229
- // src/env/debug/parser/regex-parser.ts
230
- var TRACE_LINE_REGEX = /^(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2}\.\d{3})\s+\[(\w+)\]\s+\[([^\]]*)\]\[([^\]]*)\]\s+\[TRACE\]\s+(>>>|<<<|!!!)\s+(.+?)(?:\s+\[requestId=([^\]]+)\])?\s+(enter|quit|error):\s+(.*)$/;
231
- var REQUEST_ID_IN_CATEGORY_REGEX = /\[requestId=([^\]]+)\]/;
232
-
233
- class RegexParser {
234
- parseLogLine(line) {
235
- const trimmedLine = line.trim();
236
- if (!trimmedLine) {
237
- return null;
238
- }
239
- const match = trimmedLine.match(TRACE_LINE_REGEX);
240
- if (!match) {
241
- return null;
242
- }
243
- const [, timestamp, level, source, category, action, method, requestId, actionType, rawArgs] = match;
244
- return {
245
- raw: trimmedLine,
246
- timestamp: timestamp.replace(" ", "T"),
247
- level,
248
- source,
249
- category,
250
- action,
251
- method,
252
- actionType,
253
- rawArgs,
254
- requestId: requestId || null
255
- };
256
- }
257
- parseArgs(raw) {
258
- if (!raw || !raw.trim()) {
259
- return null;
260
- }
261
- const trimmed = raw.trim();
262
- try {
263
- const parsed = JSON.parse(trimmed);
264
- if (Array.isArray(parsed)) {
265
- return parsed;
266
- }
267
- return parsed;
268
- } catch {
269
- return trimmed;
270
- }
271
- }
272
- isTraceLine(line) {
273
- return line.includes("[TRACE]") && (line.includes(">>>") || line.includes("<<<") || line.includes("!!!"));
274
- }
275
- filterByLevel(lines, level) {
276
- if (!level) {
277
- return lines;
278
- }
279
- const levelPattern = `[${level.toUpperCase()}]`;
280
- return lines.filter((line) => {
281
- if (!this.isTraceLine(line)) {
282
- return false;
283
- }
284
- return line.includes(levelPattern);
285
- });
286
- }
287
- filterByFunction(lines, functionName) {
288
- if (!functionName) {
289
- return lines;
290
- }
291
- return lines.filter((line) => {
292
- return line.includes(functionName);
293
- });
294
- }
295
- extractTraceId(line) {
296
- const categoryMatch = line.match(REQUEST_ID_IN_CATEGORY_REGEX);
297
- if (categoryMatch) {
298
- return categoryMatch[1];
299
- }
300
- const match = line.match(TRACE_LINE_REGEX);
301
- if (match && match[7]) {
302
- return match[7];
303
- }
304
- return null;
305
- }
306
- extractTraceIds(lines) {
307
- const traceIdMap = new Map;
308
- for (const line of lines) {
309
- if (!this.isTraceLine(line))
310
- continue;
311
- const traceId = this.extractTraceId(line);
312
- const parsed = this.parseLogLine(line);
313
- if (traceId && parsed) {
314
- const existing = traceIdMap.get(traceId) || [];
315
- existing.push(line);
316
- traceIdMap.set(traceId, existing);
317
- }
318
- }
319
- return traceIdMap;
320
- }
321
- parseLines(lines) {
322
- const results = [];
323
- for (const line of lines) {
324
- const parsed = this.parseLogLine(line);
325
- if (parsed) {
326
- results.push(parsed);
327
- }
328
- }
329
- return results;
330
- }
331
- }
332
-
333
- // src/env/debug/parser/span-builder.ts
334
- class SpanBuilder {
335
- parser;
336
- constructor() {
337
- this.parser = new RegexParser;
338
- }
339
- buildTraceEntries(lines) {
340
- const entries = [];
341
- for (const line of lines) {
342
- const entry = this.buildTraceEntry(line);
343
- if (entry) {
344
- entries.push(entry);
345
- }
346
- }
347
- entries.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
348
- return entries;
349
- }
350
- buildTraceEntry(line) {
351
- const traceId = line.requestId || this.extractTraceId(line.raw);
352
- const entry = {
353
- traceId: traceId || this.generateVirtualTraceId(line.method, line.timestamp),
354
- timestamp: line.timestamp,
355
- function: line.method,
356
- action: line.actionType
357
- };
358
- switch (line.actionType) {
359
- case "enter":
360
- entry.params = this.parser.parseArgs(line.rawArgs);
361
- break;
362
- case "quit":
363
- entry.result = this.parser.parseArgs(line.rawArgs);
364
- break;
365
- case "error":
366
- entry.error = line.rawArgs;
367
- break;
368
- }
369
- return entry;
370
- }
371
- generateVirtualTraceId(functionName, timestamp) {
372
- const base = `${functionName}:${timestamp}`;
373
- let hash = 0;
374
- for (let i = 0;i < base.length; i++) {
375
- const char = base.charCodeAt(i);
376
- hash = (hash << 5) - hash + char;
377
- hash = hash & hash;
378
- }
379
- return `vtrace_${Math.abs(hash).toString(36)}`;
380
- }
381
- extractTraceId(line) {
382
- return this.parser.extractTraceId(line);
383
- }
384
- calculateDuration(enter, quit) {
385
- try {
386
- const enterTime = new Date(enter.timestamp).getTime();
387
- const quitTime = new Date(quit.timestamp).getTime();
388
- if (isNaN(enterTime) || isNaN(quitTime)) {
389
- return 0;
390
- }
391
- return quitTime - enterTime;
392
- } catch {
393
- return 0;
394
- }
395
- }
396
- pairEnterQuit(entries) {
397
- const paired = [];
398
- const enterStack = [];
399
- for (const entry of entries) {
400
- if (entry.action === "enter") {
401
- enterStack.push(entry);
402
- } else if (entry.action === "quit" && enterStack.length > 0) {
403
- const enter = enterStack.pop();
404
- const duration = this.calculateDuration(enter, entry);
405
- paired.push({
406
- enter,
407
- quit: entry,
408
- durationMs: duration
409
- });
410
- }
411
- }
412
- return paired;
413
- }
414
- buildCallTree(entries) {
415
- if (entries.length === 0) {
416
- return null;
417
- }
418
- const hasSpanHierarchy = entries.some((e) => e.spanId !== undefined || e.parentSpanId !== undefined);
419
- if (hasSpanHierarchy) {
420
- return this.buildCallTreeFromHierarchy(entries);
421
- }
422
- return this.buildCallTreeFromPairs(entries);
423
- }
424
- buildCallTreeFromHierarchy(entries) {
425
- const nodeMap = new Map;
426
- const rootNodes = [];
427
- const addedToRoots = new Set;
428
- const enterEntries = entries.filter((e) => e.action === "enter");
429
- for (const entry of enterEntries) {
430
- const spanId = entry.spanId;
431
- const parentSpanId = entry.parentSpanId;
432
- if (spanId && nodeMap.has(spanId)) {
433
- continue;
434
- }
435
- const node = {
436
- traceId: entry.traceId,
437
- function: entry.function,
438
- startTime: entry.timestamp,
439
- endTime: undefined,
440
- durationMs: entry.durationMs,
441
- children: [],
442
- entry,
443
- spanId,
444
- parentSpanId
445
- };
446
- if (spanId) {
447
- nodeMap.set(spanId, node);
448
- }
449
- if (!parentSpanId || !nodeMap.has(parentSpanId)) {
450
- rootNodes.push(node);
451
- if (spanId)
452
- addedToRoots.add(spanId);
453
- }
454
- }
455
- const quitEntries = entries.filter((e) => e.action === "quit");
456
- for (const quit of quitEntries) {
457
- if (quit.spanId) {
458
- const node = nodeMap.get(quit.spanId);
459
- if (node) {
460
- node.endTime = quit.timestamp;
461
- node.durationMs = quit.durationMs || (node.endTime && node.startTime ? new Date(node.endTime).getTime() - new Date(node.startTime).getTime() : undefined);
462
- }
463
- }
464
- }
465
- for (const node of nodeMap.values()) {
466
- if (node.parentSpanId && nodeMap.has(node.parentSpanId)) {
467
- const parent = nodeMap.get(node.parentSpanId);
468
- if (parent.traceId === node.traceId) {
469
- parent.children.push(node);
470
- rootNodes.splice(rootNodes.indexOf(node), 1);
471
- }
472
- }
473
- }
474
- if (rootNodes.length === 0) {
475
- for (const entry of enterEntries) {
476
- if (!entry.parentSpanId) {
477
- const spanId = entry.spanId;
478
- if (spanId && nodeMap.has(spanId) && !addedToRoots.has(spanId)) {
479
- rootNodes.push(nodeMap.get(spanId));
480
- }
481
- }
482
- }
483
- }
484
- rootNodes.sort((a, b) => a.startTime.localeCompare(b.startTime));
485
- if (rootNodes.length === 0) {
486
- return null;
487
- }
488
- if (rootNodes.length === 1) {
489
- return rootNodes[0];
490
- }
491
- const allStartTimes = rootNodes.map((n) => new Date(n.startTime).getTime());
492
- const allEndTimes = rootNodes.map((n) => n.endTime ? new Date(n.endTime).getTime() : 0).filter((t) => t > 0);
493
- return {
494
- traceId: entries[0]?.traceId || "merged",
495
- function: "(root)",
496
- children: rootNodes,
497
- startTime: new Date(Math.min(...allStartTimes)).toISOString(),
498
- endTime: allEndTimes.length > 0 ? new Date(Math.max(...allEndTimes)).toISOString() : undefined,
499
- durationMs: allEndTimes.length > 0 && allStartTimes.length > 0 ? Math.max(...allEndTimes) - Math.min(...allStartTimes) : undefined
500
- };
501
- }
502
- buildCallTreeFromPairs(entries) {
503
- const sortedEntries = [...entries].sort((a, b) => a.timestamp.localeCompare(b.timestamp));
504
- const paired = this.pairEnterQuit(sortedEntries);
505
- if (paired.length === 0) {
506
- return null;
507
- }
508
- const stack = [];
509
- let root = null;
510
- for (const entry of sortedEntries) {
511
- if (entry.action === "enter") {
512
- const span = paired.find((p) => p.enter === entry);
513
- if (!span)
514
- continue;
515
- const node = this.buildNodeFromPaired(span);
516
- if (stack.length === 0) {
517
- root = node;
518
- } else {
519
- stack[stack.length - 1].node.children.push(node);
520
- }
521
- stack.push({ node, paired: span });
522
- } else if (entry.action === "quit" && stack.length > 0) {
523
- stack.pop();
524
- }
525
- }
526
- return root;
527
- }
528
- buildNodeFromPaired(span) {
529
- return {
530
- traceId: span.enter.traceId,
531
- function: span.enter.function,
532
- startTime: span.enter.timestamp,
533
- endTime: span.quit.timestamp,
534
- durationMs: span.durationMs,
535
- children: [],
536
- entry: span.enter
537
- };
538
- }
539
- buildMergedTree(entriesByFunction) {
540
- const allEntries = [];
541
- for (const entries of entriesByFunction.values()) {
542
- allEntries.push(...entries);
543
- }
544
- allEntries.sort((a, b) => a.timestamp.localeCompare(b.timestamp));
545
- return this.buildCallTree(allEntries);
546
- }
547
- getDurationSummary(entries) {
548
- const summary = new Map;
549
- const paired = this.pairEnterQuit(entries);
550
- for (const span of paired) {
551
- const func = span.enter.function;
552
- const existing = summary.get(func) || { count: 0, totalMs: 0, avgMs: 0 };
553
- existing.count++;
554
- existing.totalMs += span.durationMs;
555
- existing.avgMs = Math.round(existing.totalMs / existing.count);
556
- summary.set(func, existing);
557
- }
558
- return summary;
559
- }
560
- }
561
-
562
- // src/env/debug/debug-component.ts
563
- class DebugComponent extends BaseComponent {
564
- name = "debug";
565
- version = "1.0.0";
566
- reader;
567
- spanDbReader = null;
568
- parser;
569
- spanBuilder;
570
- traceFormatter;
571
- treeFormatter;
572
- options;
573
- constructor(options) {
574
- super();
575
- this.options = options || {};
576
- this.reader = new LogReader({ logDir: this.options.logDir });
577
- this.parser = new RegexParser;
578
- this.spanBuilder = new SpanBuilder;
579
- this.traceFormatter = new TraceFormatter;
580
- this.treeFormatter = new TreeFormatter;
581
- }
582
- async initSpanDbReader(dbPath) {
583
- if (!this.spanDbReader) {
584
- this.spanDbReader = new SpanDbReader({ dbPath });
585
- await this.spanDbReader.initialize();
586
- }
587
- }
588
- getSpanDbReader() {
589
- return this.spanDbReader;
590
- }
591
- async getLiveTrace(traceId) {
592
- try {
593
- const { getTracerProvider } = await import("./roy-agent-core-sx7wsvnn.js");
594
- const provider = getTracerProvider();
595
- if (!provider.isInitialized()) {
596
- await provider.initialize();
597
- }
598
- const storage = provider.getStorage();
599
- const spans = storage.findByTraceId(traceId);
600
- if (spans.length === 0) {
601
- return null;
602
- }
603
- const flattenSpans = (spanList) => {
604
- const result = [];
605
- for (const span of spanList) {
606
- result.push({
607
- traceId: span.traceId,
608
- spanId: span.spanId,
609
- parentSpanId: span.parentSpanId,
610
- function: span.name,
611
- action: "enter",
612
- timestamp: new Date(span.startTime).toISOString(),
613
- params: Object.entries(span.attributes || {}).map(([k, v]) => ({ key: k, value: v }))
614
- });
615
- if (span.children && span.children.length > 0) {
616
- result.push(...flattenSpans(span.children));
617
- }
618
- }
619
- return result;
620
- };
621
- const flatSpans = flattenSpans(spans);
622
- const tree = this.spanBuilder.buildCallTree(flatSpans);
623
- const rootSpans = spans.filter((s) => !s.parentSpanId);
624
- return {
625
- traceId,
626
- spans,
627
- callTree: tree,
628
- spanCount: flatSpans.length,
629
- rootSpans
630
- };
631
- } catch (error) {
632
- console.error("Failed to get trace from live storage:", error);
633
- return null;
634
- }
635
- }
636
- async listLiveTraces(limit = 20) {
637
- try {
638
- const { getTracerProvider } = await import("./roy-agent-core-sx7wsvnn.js");
639
- const provider = getTracerProvider();
640
- if (!provider.isInitialized()) {
641
- await provider.initialize();
642
- }
643
- const storage = provider.getStorage();
644
- const traces = storage.listTraces(limit);
645
- return {
646
- traces,
647
- count: traces.length
648
- };
649
- } catch (error) {
650
- console.error("Failed to list live traces:", error);
651
- return { traces: [], count: 0 };
652
- }
653
- }
654
- async formatLiveTrace(traceId) {
655
- try {
656
- const { getTracerProvider } = await import("./roy-agent-core-sx7wsvnn.js");
657
- const provider = getTracerProvider();
658
- if (!provider.isInitialized()) {
659
- await provider.initialize();
660
- }
661
- const storage = provider.getStorage();
662
- const spans = storage.findByTraceId(traceId);
663
- if (spans.length === 0) {
664
- return `No trace found for traceId: ${traceId}`;
665
- }
666
- const flattenSpans = (spanList) => {
667
- const result = [];
668
- for (const span of spanList) {
669
- result.push({
670
- traceId: span.traceId,
671
- spanId: span.spanId,
672
- parentSpanId: span.parentSpanId,
673
- function: span.name,
674
- action: "enter",
675
- timestamp: new Date(span.startTime).toISOString(),
676
- params: Object.entries(span.attributes || {}).map(([k, v]) => ({ key: k, value: v }))
677
- });
678
- if (span.children && span.children.length > 0) {
679
- result.push(...flattenSpans(span.children));
680
- }
681
- }
682
- return result;
683
- };
684
- const flatSpans = flattenSpans(spans);
685
- const tree = this.spanBuilder.buildCallTree(flatSpans);
686
- if (!tree) {
687
- return "No trace structure found";
688
- }
689
- return this.treeFormatter.formatTree(tree);
690
- } catch (error) {
691
- return `Failed to format trace: ${error}`;
692
- }
693
- }
694
- async init(config) {
695
- await super.init(config);
696
- this.setStatus("running");
697
- }
698
- async stop() {
699
- this.setStatus("stopping");
700
- this.setStatus("stopped");
701
- }
702
- async getTraces(options) {
703
- if (options?.source === "sqlite") {
704
- return this.getTracesFromSqlite(options);
705
- }
706
- const logFile = options?.logFile || this.reader.getLogPath();
707
- const lines = [];
708
- for await (const line of this.reader.readLines(logFile, options)) {
709
- if (options?.function && !line.includes(options.function)) {
710
- continue;
711
- }
712
- if (options?.traceId && !line.includes(`requestId=${options.traceId}`)) {
713
- continue;
714
- }
715
- if (this.parser.isTraceLine(line)) {
716
- lines.push(line);
717
- }
718
- }
719
- const parsedLines = this.parser.parseLines(lines);
720
- const entries = this.spanBuilder.buildTraceEntries(parsedLines);
721
- let filteredEntries = entries;
722
- if (options?.function) {
723
- filteredEntries = entries.filter((e) => e.function.includes(options.function));
724
- }
725
- const paired = this.spanBuilder.pairEnterQuit(filteredEntries);
726
- const result = [];
727
- for (const span of paired) {
728
- result.push({
729
- ...span.enter,
730
- durationMs: span.durationMs
731
- });
732
- if (span.quit) {
733
- result.push({
734
- ...span.quit,
735
- durationMs: span.durationMs
736
- });
737
- }
738
- }
739
- return result;
740
- }
741
- async getTracesFromSqlite(options) {
742
- if (!options?.traceId) {
743
- return [];
744
- }
745
- await this.initSpanDbReader(options.dbPath);
746
- const reader = this.getSpanDbReader();
747
- if (!reader) {
748
- throw new Error("Failed to initialize span database reader");
749
- }
750
- const entries = await reader.getTraceEntries(options.traceId);
751
- if (options?.function) {
752
- return entries.filter((e) => e.function.includes(options.function));
753
- }
754
- return entries;
755
- }
756
- async listTraceIds(options) {
757
- if (options?.source === "sqlite") {
758
- return this.listTraceIdsFromSqlite(options);
759
- }
760
- const logFile = options?.logFile || this.reader.getLogPath();
761
- const limit = options?.limit || 20;
762
- const traceMap = new Map;
763
- for await (const line of this.reader.readLines(logFile, options)) {
764
- if (!this.parser.isTraceLine(line))
765
- continue;
766
- const parsed = this.parser.parseLogLine(line);
767
- if (!parsed)
768
- continue;
769
- const traceId = this.parser.extractTraceId(line) || this.spanBuilder.generateVirtualTraceId(parsed.method, parsed.timestamp);
770
- const existing = traceMap.get(traceId);
771
- if (existing) {
772
- existing.lastTime = parsed.timestamp;
773
- existing.count++;
774
- } else {
775
- traceMap.set(traceId, {
776
- firstTime: parsed.timestamp,
777
- lastTime: parsed.timestamp,
778
- count: 1
779
- });
780
- }
781
- }
782
- const traceIds = [];
783
- for (const [traceId, info] of traceMap.entries()) {
784
- traceIds.push({
785
- traceId,
786
- firstTime: info.firstTime,
787
- lastTime: info.lastTime,
788
- count: info.count
789
- });
790
- }
791
- traceIds.sort((a, b) => b.firstTime.localeCompare(a.firstTime));
792
- return {
793
- traceIds: traceIds.slice(0, limit),
794
- count: traceIds.length
795
- };
796
- }
797
- async listTraceIdsFromSqlite(options) {
798
- const limit = options?.limit || 20;
799
- await this.initSpanDbReader(options?.dbPath);
800
- const reader = this.getSpanDbReader();
801
- if (!reader) {
802
- throw new Error("Failed to initialize span database reader");
803
- }
804
- return reader.listTraceIds(limit);
805
- }
806
- async getCallTree(options) {
807
- if (!options?.traceId) {
808
- return null;
809
- }
810
- const traces = await this.getTraces({
811
- ...options,
812
- traceId: options.traceId
813
- });
814
- if (traces.length === 0) {
815
- return null;
816
- }
817
- return this.spanBuilder.buildCallTree(traces);
818
- }
819
- getReader() {
820
- return this.reader;
821
- }
822
- getParser() {
823
- return this.parser;
824
- }
825
- getSpanBuilder() {
826
- return this.spanBuilder;
827
- }
828
- getTraceFormatter() {
829
- return this.traceFormatter;
830
- }
831
- getTreeFormatter() {
832
- return this.treeFormatter;
833
- }
834
- formatTraces(entries, options) {
835
- return this.traceFormatter.formatEntries(entries, options);
836
- }
837
- formatTraceEntry(entry, options) {
838
- return this.traceFormatter.formatEntry(entry, options);
839
- }
840
- tracesToJSON(entries) {
841
- return this.traceFormatter.toJSON(entries);
842
- }
843
- tracesToSummary(entries) {
844
- return this.traceFormatter.toSummary(entries);
845
- }
846
- formatTree(node) {
847
- return this.treeFormatter.formatTree(node);
848
- }
849
- formatTrees(trees) {
850
- return this.treeFormatter.formatTrees(trees);
851
- }
852
- treesToJSON(trees) {
853
- return this.treeFormatter.toJSON(trees);
854
- }
855
- formatTreeSummary(node) {
856
- return this.treeFormatter.formatSummary(node);
857
- }
858
- formatTreeTimeline(node) {
859
- return this.treeFormatter.formatTimeline(node);
860
- }
861
- }
862
- export { LogReader, SpanDbReader, RegexParser, SpanBuilder, DebugComponent };