agent-inspect 0.1.2 → 1.0.1
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/CHANGELOG.md +99 -0
- package/README.md +254 -44
- package/SECURITY.md +77 -0
- package/docs/ADAPTERS.md +15 -0
- package/docs/API.md +152 -0
- package/docs/ARCHITECTURE.md +13 -0
- package/docs/CLI.md +212 -0
- package/docs/COMPARE.md +69 -0
- package/docs/DIFF.md +11 -0
- package/docs/EXPORTS.md +12 -0
- package/docs/GETTING-STARTED.md +128 -0
- package/docs/KNOWN-ISSUES.md +34 -0
- package/docs/LIMITATIONS.md +32 -0
- package/docs/LOG-TO-TREE-QUICKSTART.md +54 -0
- package/docs/LOGS.md +13 -0
- package/docs/MIGRATION.md +109 -0
- package/docs/SCHEMA.md +199 -0
- package/docs/SCREENSHOTS.md +14 -0
- package/package.json +41 -15
- package/packages/cli/dist/index.cjs +3620 -118
- package/packages/cli/dist/index.cjs.map +1 -1
- package/packages/cli/dist/index.mjs +3620 -119
- package/packages/cli/dist/index.mjs.map +1 -1
- package/packages/core/dist/index.cjs +2732 -30
- package/packages/core/dist/index.cjs.map +1 -1
- package/packages/core/dist/index.d.cts +461 -6
- package/packages/core/dist/index.d.ts +461 -6
- package/packages/core/dist/index.mjs +2687 -30
- package/packages/core/dist/index.mjs.map +1 -1
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { mkdir, writeFile,
|
|
1
|
+
import { readFile, mkdir, writeFile, readdir, stat, appendFile } from 'fs/promises';
|
|
2
|
+
import crypto from 'crypto';
|
|
3
|
+
import { nanoid } from 'nanoid';
|
|
2
4
|
import os from 'os';
|
|
3
5
|
import path from 'path';
|
|
4
|
-
import { nanoid } from 'nanoid';
|
|
5
6
|
import { AsyncLocalStorage } from 'async_hooks';
|
|
6
|
-
import
|
|
7
|
+
import { createReadStream } from 'fs';
|
|
8
|
+
import { createInterface } from 'readline';
|
|
9
|
+
import chalk2 from 'chalk';
|
|
7
10
|
|
|
8
11
|
// packages/core/src/types.ts
|
|
9
12
|
var STEP_TYPES = [
|
|
@@ -51,6 +54,1002 @@ function isTraceEvent(value) {
|
|
|
51
54
|
return false;
|
|
52
55
|
}
|
|
53
56
|
}
|
|
57
|
+
function isRecord2(v) {
|
|
58
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
59
|
+
}
|
|
60
|
+
function isNonEmptyStringArray(v) {
|
|
61
|
+
return Array.isArray(v) && v.length > 0 && v.every((x) => typeof x === "string" && x.trim() !== "");
|
|
62
|
+
}
|
|
63
|
+
function validateRedact(redact) {
|
|
64
|
+
if (!Array.isArray(redact)) {
|
|
65
|
+
throw new Error("Invalid config: redact must be an array");
|
|
66
|
+
}
|
|
67
|
+
for (const r of redact) {
|
|
68
|
+
if (typeof r === "string") continue;
|
|
69
|
+
if (!isRecord2(r)) {
|
|
70
|
+
throw new Error("Invalid config: redact entries must be strings or objects");
|
|
71
|
+
}
|
|
72
|
+
if (typeof r.key !== "string" || r.key.trim() === "") {
|
|
73
|
+
throw new Error("Invalid config: redact.key must be a non-empty string");
|
|
74
|
+
}
|
|
75
|
+
if (r.strategy !== "full" && r.strategy !== "prefix" && r.strategy !== "hash") {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`Invalid config: redact.strategy must be one of full, prefix, hash (got ${String(
|
|
78
|
+
r.strategy
|
|
79
|
+
)})`
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (r.keep !== void 0 && (typeof r.keep !== "number" || !Number.isFinite(r.keep) || r.keep < 0)) {
|
|
83
|
+
throw new Error("Invalid config: redact.keep must be a non-negative number when provided");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
function validateMappings(mappings) {
|
|
88
|
+
if (!isRecord2(mappings)) {
|
|
89
|
+
throw new Error("Invalid config: mappings must be an object");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
var DEFAULT_LOG_INGEST_CONFIG = {
|
|
93
|
+
runIdKeys: ["runId", "traceId", "requestId", "decisionId", "jobId"],
|
|
94
|
+
eventKey: "event",
|
|
95
|
+
timestampKey: "timestamp",
|
|
96
|
+
messageKey: "message",
|
|
97
|
+
levelKey: "level",
|
|
98
|
+
heuristicWindowMs: 2e3,
|
|
99
|
+
mappings: {
|
|
100
|
+
"*.error": { kind: "ERROR", status: "error" },
|
|
101
|
+
"*.failed": { kind: "ERROR", status: "error" },
|
|
102
|
+
"*.llm.*": { kind: "LLM" },
|
|
103
|
+
"*.tool.*": { kind: "TOOL" },
|
|
104
|
+
"*.agent.*": { kind: "AGENT" },
|
|
105
|
+
"*.retriever.*": { kind: "RETRIEVER" },
|
|
106
|
+
"*.result.*": { kind: "RESULT" }
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
function mergeLogIngestConfig(base, override) {
|
|
110
|
+
const merged = {
|
|
111
|
+
...base,
|
|
112
|
+
...override,
|
|
113
|
+
mappings: {
|
|
114
|
+
...base.mappings ?? {},
|
|
115
|
+
...override.mappings ?? {}
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
return merged;
|
|
119
|
+
}
|
|
120
|
+
async function loadLogIngestConfig(configPath) {
|
|
121
|
+
if (configPath === void 0 || configPath.trim() === "") {
|
|
122
|
+
return DEFAULT_LOG_INGEST_CONFIG;
|
|
123
|
+
}
|
|
124
|
+
let rawText;
|
|
125
|
+
try {
|
|
126
|
+
rawText = await readFile(configPath, "utf-8");
|
|
127
|
+
} catch (e) {
|
|
128
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
129
|
+
throw new Error(`Failed to read config file: ${configPath} (${msg})`);
|
|
130
|
+
}
|
|
131
|
+
let parsed;
|
|
132
|
+
try {
|
|
133
|
+
parsed = JSON.parse(rawText);
|
|
134
|
+
} catch (e) {
|
|
135
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
136
|
+
throw new Error(`Invalid JSON in config file: ${configPath} (${msg})`);
|
|
137
|
+
}
|
|
138
|
+
if (!isRecord2(parsed)) {
|
|
139
|
+
throw new Error("Invalid config: expected a JSON object at top-level");
|
|
140
|
+
}
|
|
141
|
+
const user = parsed;
|
|
142
|
+
if (user.runIdKeys !== void 0 && !isNonEmptyStringArray(user.runIdKeys)) {
|
|
143
|
+
throw new Error("Invalid config: runIdKeys must be a non-empty array of strings");
|
|
144
|
+
}
|
|
145
|
+
if (user.eventKey !== void 0 && (typeof user.eventKey !== "string" || user.eventKey.trim() === "")) {
|
|
146
|
+
throw new Error("Invalid config: eventKey must be a non-empty string");
|
|
147
|
+
}
|
|
148
|
+
for (const k of [
|
|
149
|
+
"timestampKey",
|
|
150
|
+
"messageKey",
|
|
151
|
+
"levelKey",
|
|
152
|
+
"parentIdKey",
|
|
153
|
+
"durationKey",
|
|
154
|
+
"statusKey"
|
|
155
|
+
]) {
|
|
156
|
+
const v = user[k];
|
|
157
|
+
if (v !== void 0 && (typeof v !== "string" || v.trim() === "")) {
|
|
158
|
+
throw new Error(`Invalid config: ${k} must be a non-empty string when provided`);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
if (user.mappings !== void 0) {
|
|
162
|
+
validateMappings(user.mappings);
|
|
163
|
+
}
|
|
164
|
+
if (user.redact !== void 0) {
|
|
165
|
+
validateRedact(user.redact);
|
|
166
|
+
}
|
|
167
|
+
if (user.heuristicWindowMs !== void 0 && (typeof user.heuristicWindowMs !== "number" || !Number.isFinite(user.heuristicWindowMs) || user.heuristicWindowMs < 0)) {
|
|
168
|
+
throw new Error("Invalid config: heuristicWindowMs must be a non-negative number when provided");
|
|
169
|
+
}
|
|
170
|
+
if (user.redact && JSON.stringify(user.redact).includes("=>")) {
|
|
171
|
+
throw new Error("Invalid config: function strings are not supported in redact rules");
|
|
172
|
+
}
|
|
173
|
+
return mergeLogIngestConfig(DEFAULT_LOG_INGEST_CONFIG, user);
|
|
174
|
+
}
|
|
175
|
+
function isRecord3(v) {
|
|
176
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
177
|
+
}
|
|
178
|
+
var JsonLogParser = class {
|
|
179
|
+
parseLines(lines, filePath) {
|
|
180
|
+
const records = [];
|
|
181
|
+
const warnings = [];
|
|
182
|
+
for (let i = 0; i < lines.length; i++) {
|
|
183
|
+
const lineNumber = i + 1;
|
|
184
|
+
const raw = lines[i] ?? "";
|
|
185
|
+
const trimmed = raw.trim();
|
|
186
|
+
if (trimmed === "") continue;
|
|
187
|
+
let parsed;
|
|
188
|
+
try {
|
|
189
|
+
parsed = JSON.parse(trimmed);
|
|
190
|
+
} catch {
|
|
191
|
+
warnings.push({
|
|
192
|
+
code: "MALFORMED_JSON",
|
|
193
|
+
message: "Malformed JSON log line",
|
|
194
|
+
file: filePath,
|
|
195
|
+
line: lineNumber,
|
|
196
|
+
raw: trimmed.slice(0, 500)
|
|
197
|
+
});
|
|
198
|
+
continue;
|
|
199
|
+
}
|
|
200
|
+
if (!isRecord3(parsed)) {
|
|
201
|
+
warnings.push({
|
|
202
|
+
code: "MALFORMED_JSON",
|
|
203
|
+
message: "JSON log line must be an object",
|
|
204
|
+
file: filePath,
|
|
205
|
+
line: lineNumber,
|
|
206
|
+
raw: trimmed.slice(0, 500)
|
|
207
|
+
});
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
records.push({
|
|
211
|
+
raw: parsed,
|
|
212
|
+
file: filePath,
|
|
213
|
+
line: lineNumber,
|
|
214
|
+
sourceType: "json-log"
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
return { records, warnings };
|
|
218
|
+
}
|
|
219
|
+
async parseFile(filePath) {
|
|
220
|
+
let text;
|
|
221
|
+
try {
|
|
222
|
+
text = await readFile(filePath, "utf-8");
|
|
223
|
+
} catch (e) {
|
|
224
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
225
|
+
throw new Error(`Failed to read log file: ${filePath} (${msg})`);
|
|
226
|
+
}
|
|
227
|
+
const lines = text.split(/\r?\n/);
|
|
228
|
+
return this.parseLines(lines, filePath);
|
|
229
|
+
}
|
|
230
|
+
};
|
|
231
|
+
function isRecord4(v) {
|
|
232
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
233
|
+
}
|
|
234
|
+
function findLastJsonObjectSubstring(line) {
|
|
235
|
+
let last;
|
|
236
|
+
for (let i = 0; i < line.length; i++) {
|
|
237
|
+
if (line[i] !== "{") continue;
|
|
238
|
+
let depth = 0;
|
|
239
|
+
let inString = false;
|
|
240
|
+
let escape = false;
|
|
241
|
+
for (let j = i; j < line.length; j++) {
|
|
242
|
+
const ch = line[j];
|
|
243
|
+
if (inString) {
|
|
244
|
+
if (escape) {
|
|
245
|
+
escape = false;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
if (ch === "\\") {
|
|
249
|
+
escape = true;
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
if (ch === '"') {
|
|
253
|
+
inString = false;
|
|
254
|
+
}
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if (ch === '"') {
|
|
258
|
+
inString = true;
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (ch === "{") depth += 1;
|
|
262
|
+
if (ch === "}") depth -= 1;
|
|
263
|
+
if (depth === 0) {
|
|
264
|
+
last = { start: i, end: j + 1 };
|
|
265
|
+
i = j;
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
if (depth < 0) break;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (!last) return void 0;
|
|
272
|
+
return line.slice(last.start, last.end);
|
|
273
|
+
}
|
|
274
|
+
var Log4jsParser = class {
|
|
275
|
+
parseLines(lines, filePath) {
|
|
276
|
+
const records = [];
|
|
277
|
+
const warnings = [];
|
|
278
|
+
for (let i = 0; i < lines.length; i++) {
|
|
279
|
+
const lineNumber = i + 1;
|
|
280
|
+
const rawLine = lines[i] ?? "";
|
|
281
|
+
const trimmed = rawLine.trim();
|
|
282
|
+
if (trimmed === "") continue;
|
|
283
|
+
const jsonText = findLastJsonObjectSubstring(trimmed);
|
|
284
|
+
if (!jsonText) {
|
|
285
|
+
warnings.push({
|
|
286
|
+
code: "UNSUPPORTED_LOG4JS_PAYLOAD",
|
|
287
|
+
message: "No embedded JSON object found in log4js line",
|
|
288
|
+
file: filePath,
|
|
289
|
+
line: lineNumber,
|
|
290
|
+
raw: trimmed.slice(0, 500)
|
|
291
|
+
});
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
let parsed;
|
|
295
|
+
try {
|
|
296
|
+
parsed = JSON.parse(jsonText);
|
|
297
|
+
} catch {
|
|
298
|
+
warnings.push({
|
|
299
|
+
code: "MALFORMED_JSON",
|
|
300
|
+
message: "Malformed embedded JSON object in log4js line",
|
|
301
|
+
file: filePath,
|
|
302
|
+
line: lineNumber,
|
|
303
|
+
raw: jsonText.slice(0, 500)
|
|
304
|
+
});
|
|
305
|
+
continue;
|
|
306
|
+
}
|
|
307
|
+
if (!isRecord4(parsed)) {
|
|
308
|
+
warnings.push({
|
|
309
|
+
code: "UNSUPPORTED_LOG4JS_PAYLOAD",
|
|
310
|
+
message: "Embedded JSON payload must be an object",
|
|
311
|
+
file: filePath,
|
|
312
|
+
line: lineNumber,
|
|
313
|
+
raw: jsonText.slice(0, 500)
|
|
314
|
+
});
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
records.push({
|
|
318
|
+
raw: parsed,
|
|
319
|
+
file: filePath,
|
|
320
|
+
line: lineNumber,
|
|
321
|
+
sourceType: "log4js"
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
return { records, warnings };
|
|
325
|
+
}
|
|
326
|
+
async parseFile(filePath) {
|
|
327
|
+
let text;
|
|
328
|
+
try {
|
|
329
|
+
text = await readFile(filePath, "utf-8");
|
|
330
|
+
} catch (e) {
|
|
331
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
332
|
+
throw new Error(`Failed to read log file: ${filePath} (${msg})`);
|
|
333
|
+
}
|
|
334
|
+
const lines = text.split(/\r?\n/);
|
|
335
|
+
return this.parseLines(lines, filePath);
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
// packages/core/src/logs/mapping.ts
|
|
340
|
+
function wildcardMatch(pattern, value) {
|
|
341
|
+
if (pattern === value) return true;
|
|
342
|
+
if (!pattern.includes("*")) return false;
|
|
343
|
+
const parts = pattern.split("*");
|
|
344
|
+
let idx = 0;
|
|
345
|
+
for (let i = 0; i < parts.length; i++) {
|
|
346
|
+
const part = parts[i];
|
|
347
|
+
if (part === "") continue;
|
|
348
|
+
const found = value.indexOf(part, idx);
|
|
349
|
+
if (found === -1) return false;
|
|
350
|
+
if (i === 0 && !pattern.startsWith("*") && found !== 0) return false;
|
|
351
|
+
idx = found + part.length;
|
|
352
|
+
}
|
|
353
|
+
if (!pattern.endsWith("*")) {
|
|
354
|
+
const last = parts[parts.length - 1];
|
|
355
|
+
if (last !== "" && !value.endsWith(last)) return false;
|
|
356
|
+
if (last === "" && !value.endsWith(parts[parts.length - 2] ?? "")) return false;
|
|
357
|
+
}
|
|
358
|
+
return true;
|
|
359
|
+
}
|
|
360
|
+
function matchMapping(eventName, mappings) {
|
|
361
|
+
if (!mappings) return void 0;
|
|
362
|
+
if (mappings[eventName]) return mappings[eventName];
|
|
363
|
+
let bestKey;
|
|
364
|
+
let bestScore = -1;
|
|
365
|
+
for (const key of Object.keys(mappings)) {
|
|
366
|
+
if (!key.includes("*")) continue;
|
|
367
|
+
if (!wildcardMatch(key, eventName)) continue;
|
|
368
|
+
const score = key.replaceAll("*", "").length;
|
|
369
|
+
if (score > bestScore) {
|
|
370
|
+
bestScore = score;
|
|
371
|
+
bestKey = key;
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
return bestKey ? mappings[bestKey] : void 0;
|
|
375
|
+
}
|
|
376
|
+
var DEFAULT_REDACT_KEYS = [
|
|
377
|
+
"authorization",
|
|
378
|
+
"cookie",
|
|
379
|
+
"token",
|
|
380
|
+
"apiKey",
|
|
381
|
+
"password",
|
|
382
|
+
"secret",
|
|
383
|
+
"email"
|
|
384
|
+
];
|
|
385
|
+
function isRecord5(v) {
|
|
386
|
+
return typeof v === "object" && v !== null && !Array.isArray(v);
|
|
387
|
+
}
|
|
388
|
+
function toKey(s) {
|
|
389
|
+
return s.toLowerCase();
|
|
390
|
+
}
|
|
391
|
+
function stableHash(value) {
|
|
392
|
+
const h = crypto.createHash("sha256").update(value, "utf8").digest("hex");
|
|
393
|
+
return h.slice(0, 8);
|
|
394
|
+
}
|
|
395
|
+
function compileRules(rules) {
|
|
396
|
+
const out = /* @__PURE__ */ new Map();
|
|
397
|
+
const set = (r) => {
|
|
398
|
+
const k = toKey(r.key);
|
|
399
|
+
out.set(k, { ...r, key: k });
|
|
400
|
+
};
|
|
401
|
+
for (const k of DEFAULT_REDACT_KEYS) {
|
|
402
|
+
set({ key: k, strategy: "full" });
|
|
403
|
+
}
|
|
404
|
+
for (const r of rules ?? []) {
|
|
405
|
+
if (typeof r === "string") {
|
|
406
|
+
set({ key: r, strategy: "full" });
|
|
407
|
+
continue;
|
|
408
|
+
}
|
|
409
|
+
const key = r.key;
|
|
410
|
+
if (r.strategy === "full") set({ key, strategy: "full" });
|
|
411
|
+
if (r.strategy === "hash") set({ key, strategy: "hash" });
|
|
412
|
+
if (r.strategy === "prefix") {
|
|
413
|
+
set({ key, strategy: "prefix", keep: typeof r.keep === "number" ? r.keep : 8 });
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
return [...out.values()];
|
|
417
|
+
}
|
|
418
|
+
var Redactor = class {
|
|
419
|
+
#rules;
|
|
420
|
+
constructor(options) {
|
|
421
|
+
this.#rules = compileRules(options?.rules);
|
|
422
|
+
}
|
|
423
|
+
redactValue(key, value) {
|
|
424
|
+
const k = toKey(key);
|
|
425
|
+
const rule = this.#rules.find((r) => r.key === k);
|
|
426
|
+
if (!rule) {
|
|
427
|
+
return this.#redactNested(value);
|
|
428
|
+
}
|
|
429
|
+
if (rule.strategy === "full") return "[REDACTED]";
|
|
430
|
+
const asString = typeof value === "string" ? value : typeof value === "number" || typeof value === "boolean" || typeof value === "bigint" ? String(value) : void 0;
|
|
431
|
+
if (rule.strategy === "prefix") {
|
|
432
|
+
if (asString === void 0) return "[REDACTED]";
|
|
433
|
+
const keep = Math.max(0, Math.floor(rule.keep));
|
|
434
|
+
return asString.length <= keep ? `${asString}\u2026` : `${asString.slice(0, keep)}\u2026`;
|
|
435
|
+
}
|
|
436
|
+
if (rule.strategy === "hash") {
|
|
437
|
+
if (asString === void 0) return "[HASH:unknown]";
|
|
438
|
+
return `[HASH:${stableHash(asString)}]`;
|
|
439
|
+
}
|
|
440
|
+
return this.#redactNested(value);
|
|
441
|
+
}
|
|
442
|
+
redactRecord(record) {
|
|
443
|
+
const out = {};
|
|
444
|
+
for (const [k, v] of Object.entries(record)) {
|
|
445
|
+
out[k] = this.redactValue(k, v);
|
|
446
|
+
}
|
|
447
|
+
return out;
|
|
448
|
+
}
|
|
449
|
+
#redactNested(value) {
|
|
450
|
+
if (Array.isArray(value)) {
|
|
451
|
+
return value.map((v) => this.#redactNested(v));
|
|
452
|
+
}
|
|
453
|
+
if (isRecord5(value)) {
|
|
454
|
+
const out = {};
|
|
455
|
+
for (const [k, v] of Object.entries(value)) {
|
|
456
|
+
out[k] = this.redactValue(k, v);
|
|
457
|
+
}
|
|
458
|
+
return out;
|
|
459
|
+
}
|
|
460
|
+
return value;
|
|
461
|
+
}
|
|
462
|
+
};
|
|
463
|
+
function isFiniteNumber(v) {
|
|
464
|
+
return typeof v === "number" && Number.isFinite(v);
|
|
465
|
+
}
|
|
466
|
+
function safeString(v) {
|
|
467
|
+
if (typeof v !== "string") return void 0;
|
|
468
|
+
const t = v.trim();
|
|
469
|
+
return t === "" ? void 0 : t;
|
|
470
|
+
}
|
|
471
|
+
function parseTimestamp(v) {
|
|
472
|
+
if (isFiniteNumber(v)) return v;
|
|
473
|
+
if (typeof v === "string") {
|
|
474
|
+
const t = Date.parse(v);
|
|
475
|
+
if (Number.isFinite(t)) return t;
|
|
476
|
+
}
|
|
477
|
+
return void 0;
|
|
478
|
+
}
|
|
479
|
+
function hasToken(hay, token) {
|
|
480
|
+
return hay.includes(token);
|
|
481
|
+
}
|
|
482
|
+
function inferKind(eventName) {
|
|
483
|
+
if (hasToken(eventName, ".llm.")) return "LLM";
|
|
484
|
+
if (hasToken(eventName, ".tool.")) return "TOOL";
|
|
485
|
+
if (hasToken(eventName, ".agent.")) return "AGENT";
|
|
486
|
+
if (hasToken(eventName, ".retriever.")) return "RETRIEVER";
|
|
487
|
+
if (hasToken(eventName, ".result.")) return "RESULT";
|
|
488
|
+
if (eventName.endsWith(".error") || eventName.endsWith(".failed") || eventName.includes(".error") || eventName.includes(".failed")) {
|
|
489
|
+
return "ERROR";
|
|
490
|
+
}
|
|
491
|
+
return "LOG";
|
|
492
|
+
}
|
|
493
|
+
function deriveName(eventName, kind) {
|
|
494
|
+
const parts = eventName.split(".");
|
|
495
|
+
const last = parts[parts.length - 1] ?? eventName;
|
|
496
|
+
if (kind === "LLM") return `llm:${last}`;
|
|
497
|
+
if (kind === "TOOL") return `tool:${last}`;
|
|
498
|
+
if (kind === "AGENT") return `agent:${last}`;
|
|
499
|
+
if (kind === "RESULT") return `result:${last}`;
|
|
500
|
+
if (kind === "RUN") return `run:${last}`;
|
|
501
|
+
if (kind === "RETRIEVER") return `retriever:${last}`;
|
|
502
|
+
if (kind === "ERROR") return `error:${last}`;
|
|
503
|
+
return eventName;
|
|
504
|
+
}
|
|
505
|
+
var EventNormalizer = class {
|
|
506
|
+
#config;
|
|
507
|
+
constructor(options) {
|
|
508
|
+
this.#config = options.config;
|
|
509
|
+
}
|
|
510
|
+
#normalizeInternal(record) {
|
|
511
|
+
const raw = record.raw;
|
|
512
|
+
const cfg = this.#config;
|
|
513
|
+
let runId;
|
|
514
|
+
for (const k of cfg.runIdKeys) {
|
|
515
|
+
const v = raw[k];
|
|
516
|
+
const s = safeString(v);
|
|
517
|
+
if (s) {
|
|
518
|
+
runId = s;
|
|
519
|
+
break;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
if (!runId) {
|
|
523
|
+
return {
|
|
524
|
+
warning: {
|
|
525
|
+
code: "MISSING_RUN_ID",
|
|
526
|
+
message: "Missing run id (none of runIdKeys present)",
|
|
527
|
+
file: record.file,
|
|
528
|
+
line: record.line
|
|
529
|
+
}
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
const eventName = safeString(raw[cfg.eventKey]);
|
|
533
|
+
if (!eventName) {
|
|
534
|
+
return {
|
|
535
|
+
warning: {
|
|
536
|
+
code: "MISSING_EVENT",
|
|
537
|
+
message: `Missing event name (key: ${cfg.eventKey})`,
|
|
538
|
+
file: record.file,
|
|
539
|
+
line: record.line
|
|
540
|
+
}
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
const mapping = matchMapping(eventName, cfg.mappings);
|
|
544
|
+
const tsKey = cfg.timestampKey ?? "timestamp";
|
|
545
|
+
const tsRaw = raw[tsKey];
|
|
546
|
+
const parsedTs = parseTimestamp(tsRaw);
|
|
547
|
+
const timestamp = parsedTs ?? Date.now();
|
|
548
|
+
const timestampMissing = parsedTs === void 0;
|
|
549
|
+
const parentIdKey = cfg.parentIdKey;
|
|
550
|
+
const parentId = parentIdKey ? safeString(raw[parentIdKey]) : void 0;
|
|
551
|
+
let durationMs;
|
|
552
|
+
const durationKey = cfg.durationKey;
|
|
553
|
+
if (durationKey) {
|
|
554
|
+
const v = raw[durationKey];
|
|
555
|
+
if (isFiniteNumber(v)) durationMs = v;
|
|
556
|
+
} else if (isFiniteNumber(raw.durationMs)) {
|
|
557
|
+
durationMs = raw.durationMs;
|
|
558
|
+
}
|
|
559
|
+
let status;
|
|
560
|
+
const statusKey = cfg.statusKey;
|
|
561
|
+
const statusRaw = statusKey ? safeString(raw[statusKey]) : void 0;
|
|
562
|
+
if (statusRaw === "running" || statusRaw === "ok" || statusRaw === "error") {
|
|
563
|
+
status = statusRaw;
|
|
564
|
+
} else if (mapping?.status) {
|
|
565
|
+
status = mapping.status;
|
|
566
|
+
} else if (mapping?.kind === "ERROR") {
|
|
567
|
+
status = "error";
|
|
568
|
+
} else if (eventName.includes(".failed") || eventName.includes(".error")) {
|
|
569
|
+
status = "error";
|
|
570
|
+
} else if (mapping?.startsRun || mapping?.startsStep) {
|
|
571
|
+
status = "running";
|
|
572
|
+
} else if (mapping?.endsRun || mapping?.endsStep) {
|
|
573
|
+
status = "ok";
|
|
574
|
+
}
|
|
575
|
+
const kind = mapping?.kind ?? inferKind(eventName);
|
|
576
|
+
const name = mapping?.name ?? deriveName(eventName, kind);
|
|
577
|
+
let confidence = "correlated";
|
|
578
|
+
if (parentId || mapping?.startsRun) confidence = "explicit";
|
|
579
|
+
if (timestampMissing) confidence = "unknown";
|
|
580
|
+
const omit = new Set([
|
|
581
|
+
...cfg.runIdKeys,
|
|
582
|
+
cfg.eventKey,
|
|
583
|
+
tsKey,
|
|
584
|
+
cfg.messageKey ?? "message",
|
|
585
|
+
cfg.levelKey ?? "level",
|
|
586
|
+
cfg.parentIdKey ?? "",
|
|
587
|
+
cfg.durationKey ?? "",
|
|
588
|
+
cfg.statusKey ?? "",
|
|
589
|
+
"durationMs"
|
|
590
|
+
].filter((k) => k !== ""));
|
|
591
|
+
const attributes = {};
|
|
592
|
+
for (const [k, v] of Object.entries(raw)) {
|
|
593
|
+
if (omit.has(k)) continue;
|
|
594
|
+
attributes[k] = v;
|
|
595
|
+
}
|
|
596
|
+
const event = {
|
|
597
|
+
eventId: nanoid(10),
|
|
598
|
+
runId,
|
|
599
|
+
...parentId ? { parentId } : {},
|
|
600
|
+
name,
|
|
601
|
+
kind,
|
|
602
|
+
timestamp,
|
|
603
|
+
...status ? { status } : {},
|
|
604
|
+
...durationMs !== void 0 ? { durationMs } : {},
|
|
605
|
+
...Object.keys(attributes).length > 0 ? { attributes } : {},
|
|
606
|
+
confidence,
|
|
607
|
+
source: {
|
|
608
|
+
type: record.sourceType,
|
|
609
|
+
file: record.file,
|
|
610
|
+
line: record.line
|
|
611
|
+
}
|
|
612
|
+
};
|
|
613
|
+
if (timestampMissing) {
|
|
614
|
+
return {
|
|
615
|
+
event,
|
|
616
|
+
warning: {
|
|
617
|
+
code: "MISSING_TIMESTAMP",
|
|
618
|
+
message: `Missing or invalid timestamp (key: ${tsKey})`,
|
|
619
|
+
file: record.file,
|
|
620
|
+
line: record.line,
|
|
621
|
+
raw: JSON.stringify(raw).slice(0, 500)
|
|
622
|
+
}
|
|
623
|
+
};
|
|
624
|
+
}
|
|
625
|
+
return { event };
|
|
626
|
+
}
|
|
627
|
+
normalize(record) {
|
|
628
|
+
const r = this.#normalizeInternal(record);
|
|
629
|
+
return r.event ?? r.warning;
|
|
630
|
+
}
|
|
631
|
+
normalizeAll(records) {
|
|
632
|
+
const out = [];
|
|
633
|
+
const warnings = [];
|
|
634
|
+
for (const r of records) {
|
|
635
|
+
const normalized = this.#normalizeInternal(r);
|
|
636
|
+
if (normalized.event) out.push(normalized.event);
|
|
637
|
+
if (normalized.warning) warnings.push(normalized.warning);
|
|
638
|
+
}
|
|
639
|
+
return { records: out, warnings };
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
|
|
643
|
+
// packages/core/src/logs/tree-builder.ts
|
|
644
|
+
function inc(map, key) {
|
|
645
|
+
map[key] = (map[key] ?? 0) + 1;
|
|
646
|
+
}
|
|
647
|
+
function computeRunStatus(events) {
|
|
648
|
+
let hasRunning = false;
|
|
649
|
+
for (const e of events) {
|
|
650
|
+
if (e.status === "error") return "error";
|
|
651
|
+
if (e.status === "running") hasRunning = true;
|
|
652
|
+
}
|
|
653
|
+
if (hasRunning) return "running";
|
|
654
|
+
return "ok";
|
|
655
|
+
}
|
|
656
|
+
var TreeBuilder = class {
|
|
657
|
+
constructor(options) {
|
|
658
|
+
void options?.config;
|
|
659
|
+
}
|
|
660
|
+
build(events) {
|
|
661
|
+
const byRun = /* @__PURE__ */ new Map();
|
|
662
|
+
for (const e of events) {
|
|
663
|
+
if (!byRun.has(e.runId)) byRun.set(e.runId, []);
|
|
664
|
+
byRun.get(e.runId).push(e);
|
|
665
|
+
}
|
|
666
|
+
const out = [];
|
|
667
|
+
for (const [runId, runEvents] of byRun.entries()) {
|
|
668
|
+
const sorted = [...runEvents].sort((a, b) => a.timestamp - b.timestamp);
|
|
669
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
670
|
+
for (const e of sorted) {
|
|
671
|
+
nodes.set(e.eventId, { event: e, children: [], depth: 0 });
|
|
672
|
+
}
|
|
673
|
+
const roots = [];
|
|
674
|
+
for (const node of nodes.values()) {
|
|
675
|
+
const parentId = node.event.parentId;
|
|
676
|
+
if (parentId && nodes.has(parentId)) {
|
|
677
|
+
nodes.get(parentId).children.push(node);
|
|
678
|
+
} else {
|
|
679
|
+
roots.push(node);
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
const assignDepth = (n, depth) => {
|
|
683
|
+
n.depth = depth;
|
|
684
|
+
for (const c of n.children) assignDepth(c, depth + 1);
|
|
685
|
+
};
|
|
686
|
+
for (const r of roots) assignDepth(r, 0);
|
|
687
|
+
const confidenceBreakdown = {
|
|
688
|
+
explicit: 0,
|
|
689
|
+
correlated: 0,
|
|
690
|
+
heuristic: 0,
|
|
691
|
+
unknown: 0
|
|
692
|
+
};
|
|
693
|
+
const kinds = {};
|
|
694
|
+
for (const e of sorted) {
|
|
695
|
+
inc(confidenceBreakdown, e.confidence);
|
|
696
|
+
kinds[e.kind] = (kinds[e.kind] ?? 0) + 1;
|
|
697
|
+
}
|
|
698
|
+
const startedAt = sorted.length > 0 ? sorted[0].timestamp : void 0;
|
|
699
|
+
const endedAt = sorted.length > 0 ? sorted[sorted.length - 1].timestamp : void 0;
|
|
700
|
+
const status = computeRunStatus(sorted);
|
|
701
|
+
const durationMs = startedAt !== void 0 && endedAt !== void 0 && Number.isFinite(startedAt) && Number.isFinite(endedAt) && endedAt >= startedAt && status !== "running" ? endedAt - startedAt : void 0;
|
|
702
|
+
const name = sorted.find((e) => e.kind === "RUN")?.name;
|
|
703
|
+
out.push({
|
|
704
|
+
runId,
|
|
705
|
+
name,
|
|
706
|
+
status,
|
|
707
|
+
startedAt,
|
|
708
|
+
endedAt: status === "running" ? void 0 : endedAt,
|
|
709
|
+
durationMs,
|
|
710
|
+
children: roots,
|
|
711
|
+
metadata: {
|
|
712
|
+
totalEvents: sorted.length,
|
|
713
|
+
confidenceBreakdown,
|
|
714
|
+
kinds
|
|
715
|
+
}
|
|
716
|
+
});
|
|
717
|
+
}
|
|
718
|
+
out.sort((a, b) => (b.startedAt ?? 0) - (a.startedAt ?? 0));
|
|
719
|
+
return out;
|
|
720
|
+
}
|
|
721
|
+
};
|
|
722
|
+
|
|
723
|
+
// packages/core/src/logs/tree-renderer.ts
|
|
724
|
+
function truncate(v, max) {
|
|
725
|
+
if (v.length <= max) return v;
|
|
726
|
+
return v.slice(0, Math.max(0, max - 1)) + "\u2026";
|
|
727
|
+
}
|
|
728
|
+
function fmtAttrValue(value, maxLen) {
|
|
729
|
+
if (value === null || value === void 0) return void 0;
|
|
730
|
+
if (typeof value === "string") return truncate(value, maxLen);
|
|
731
|
+
if (typeof value === "number" || typeof value === "boolean" || typeof value === "bigint") {
|
|
732
|
+
return String(value);
|
|
733
|
+
}
|
|
734
|
+
if (typeof value === "object") {
|
|
735
|
+
try {
|
|
736
|
+
return truncate(JSON.stringify(value), maxLen);
|
|
737
|
+
} catch {
|
|
738
|
+
return "[object]";
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
return String(value);
|
|
742
|
+
}
|
|
743
|
+
function compactAttrs(attrs, maxLen) {
|
|
744
|
+
if (!attrs) return "";
|
|
745
|
+
const entries = Object.entries(attrs);
|
|
746
|
+
if (entries.length === 0) return "";
|
|
747
|
+
const picks = /* @__PURE__ */ new Map();
|
|
748
|
+
const set = (k, v) => {
|
|
749
|
+
const s = fmtAttrValue(v, maxLen);
|
|
750
|
+
if (s !== void 0) picks.set(k, s);
|
|
751
|
+
};
|
|
752
|
+
set("job", attrs.jobId ?? attrs.job ?? attrs.jobUuid);
|
|
753
|
+
set("user", attrs.userUuid ?? attrs.userId ?? attrs.user);
|
|
754
|
+
set("trip", attrs.tripUuid ?? attrs.tripId ?? attrs.trip);
|
|
755
|
+
set("msgs", attrs.messageCount ?? attrs.msgs);
|
|
756
|
+
set("trips", attrs.trips);
|
|
757
|
+
set("model", attrs.model);
|
|
758
|
+
const tokens = attrs.tokens;
|
|
759
|
+
if (tokens && typeof tokens === "object" && tokens !== null) {
|
|
760
|
+
const input = tokens.input;
|
|
761
|
+
const output = tokens.output;
|
|
762
|
+
if (typeof input === "number" || typeof output === "number") {
|
|
763
|
+
picks.set("tokens", `${input ?? "?"}/${output ?? "?"}`);
|
|
764
|
+
}
|
|
765
|
+
}
|
|
766
|
+
for (const k of ["shouldNotify", "variant"]) {
|
|
767
|
+
if (k in attrs) set(k, attrs[k]);
|
|
768
|
+
}
|
|
769
|
+
const rendered = [...picks.entries()].filter(([, v]) => v !== "").map(([k, v]) => `${k}=${v}`);
|
|
770
|
+
return rendered.length > 0 ? " " + rendered.join(" ") : "";
|
|
771
|
+
}
|
|
772
|
+
function statusMark(node) {
|
|
773
|
+
if (node.event.status === "error") return " \u2716";
|
|
774
|
+
if (node.event.status === "ok") return " \u2714";
|
|
775
|
+
return "";
|
|
776
|
+
}
|
|
777
|
+
function fmtDuration(ms) {
|
|
778
|
+
if (!Number.isFinite(ms) || ms < 0) return "";
|
|
779
|
+
if (ms < 1e3) return `${Math.round(ms)}ms`;
|
|
780
|
+
return `${(ms / 1e3).toFixed(2)}s`;
|
|
781
|
+
}
|
|
782
|
+
function renderNodeLines(node, prefix, isLast, options) {
|
|
783
|
+
const branch = prefix + (isLast ? "\u2514\u2500 " : "\u251C\u2500 ");
|
|
784
|
+
const nextPrefix = prefix + (isLast ? " " : "\u2502 ");
|
|
785
|
+
const attrs = compactAttrs(node.event.attributes, options.maxAttributeLength);
|
|
786
|
+
const dur = node.event.durationMs !== void 0 ? ` ${fmtDuration(node.event.durationMs)}` : "";
|
|
787
|
+
const line = `${branch}${node.event.name}${attrs}${statusMark(node)}${dur}`;
|
|
788
|
+
const lines = [line];
|
|
789
|
+
const showConf = options.showConfidence === "always" || options.showConfidence === "non-explicit" && node.event.confidence !== "explicit";
|
|
790
|
+
if (showConf) {
|
|
791
|
+
lines.push(`${nextPrefix}confidence: ${node.event.confidence}`);
|
|
792
|
+
}
|
|
793
|
+
const children = node.children;
|
|
794
|
+
for (let i = 0; i < children.length; i++) {
|
|
795
|
+
lines.push(...renderNodeLines(children[i], nextPrefix, i === children.length - 1, options));
|
|
796
|
+
}
|
|
797
|
+
return lines;
|
|
798
|
+
}
|
|
799
|
+
function renderRunTree(tree, options) {
|
|
800
|
+
const opts = {
|
|
801
|
+
verbose: options?.verbose ?? false,
|
|
802
|
+
showConfidence: options?.showConfidence ?? "always",
|
|
803
|
+
showMetadata: options?.showMetadata ?? false,
|
|
804
|
+
color: options?.color ?? false,
|
|
805
|
+
maxAttributeLength: options?.maxAttributeLength ?? 40,
|
|
806
|
+
summary: options?.summary ?? true
|
|
807
|
+
};
|
|
808
|
+
const header = `Run ${tree.runId}`;
|
|
809
|
+
const lines = [header];
|
|
810
|
+
const children = tree.children;
|
|
811
|
+
for (let i = 0; i < children.length; i++) {
|
|
812
|
+
lines.push(...renderNodeLines(children[i], "", i === children.length - 1, opts));
|
|
813
|
+
}
|
|
814
|
+
if (opts.summary) {
|
|
815
|
+
const cb = tree.metadata.confidenceBreakdown;
|
|
816
|
+
const tools = tree.metadata.kinds.TOOL ?? 0;
|
|
817
|
+
const llms = tree.metadata.kinds.LLM ?? 0;
|
|
818
|
+
lines.push("");
|
|
819
|
+
lines.push("Summary:");
|
|
820
|
+
lines.push(` Events: ${tree.metadata.totalEvents}`);
|
|
821
|
+
lines.push(` Tools: ${tools}`);
|
|
822
|
+
lines.push(` LLMs: ${llms}`);
|
|
823
|
+
lines.push(
|
|
824
|
+
` Confidence: ${cb.explicit} explicit, ${cb.correlated} correlated, ${cb.heuristic} heuristic, ${cb.unknown} unknown`
|
|
825
|
+
);
|
|
826
|
+
lines.push("");
|
|
827
|
+
lines.push("Note:");
|
|
828
|
+
lines.push(" Flat timeline by default. Nesting only with explicit parentId.");
|
|
829
|
+
}
|
|
830
|
+
return lines.join("\n");
|
|
831
|
+
}
|
|
832
|
+
function renderRunTrees(trees, options) {
|
|
833
|
+
return trees.map((t) => renderRunTree(t, options)).join("\n\n");
|
|
834
|
+
}
|
|
835
|
+
|
|
836
|
+
// packages/core/src/logs/line-parser.ts
|
|
837
|
+
function shiftLineNumbers(res, options) {
|
|
838
|
+
const targetLine = typeof options.line === "number" && Number.isFinite(options.line) && options.line > 0 ? Math.floor(options.line) : void 0;
|
|
839
|
+
const file = typeof options.file === "string" && options.file.trim() !== "" ? options.file : void 0;
|
|
840
|
+
if (targetLine === void 0 && file === void 0) return res;
|
|
841
|
+
const mapRecord = (x) => ({
|
|
842
|
+
...x,
|
|
843
|
+
...targetLine !== void 0 ? { line: targetLine } : {},
|
|
844
|
+
...file !== void 0 ? { file } : {}
|
|
845
|
+
});
|
|
846
|
+
const mapWarning = (x) => ({
|
|
847
|
+
...x,
|
|
848
|
+
...targetLine !== void 0 ? { line: targetLine } : {},
|
|
849
|
+
...file !== void 0 ? { file } : {}
|
|
850
|
+
});
|
|
851
|
+
return {
|
|
852
|
+
records: res.records.map(mapRecord),
|
|
853
|
+
warnings: res.warnings.map(mapWarning)
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
function normalizeFormat(line, format) {
|
|
857
|
+
if (format && format !== "auto") return format;
|
|
858
|
+
const trimmed = line.trim();
|
|
859
|
+
if (trimmed.startsWith("{")) return "json";
|
|
860
|
+
return "log4js";
|
|
861
|
+
}
|
|
862
|
+
function parseLogLine(line, options = {}) {
|
|
863
|
+
const raw = typeof line === "string" ? line : "";
|
|
864
|
+
const trimmed = raw.trim();
|
|
865
|
+
if (trimmed === "") return { records: [], warnings: [] };
|
|
866
|
+
const format = normalizeFormat(raw, options.format);
|
|
867
|
+
const base = format === "json" ? new JsonLogParser().parseLines([raw], options.file) : new Log4jsParser().parseLines([raw], options.file);
|
|
868
|
+
return shiftLineNumbers(base, options);
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
// packages/core/src/logs/live-tree.ts
|
|
872
|
+
var LiveLogAccumulator = class {
|
|
873
|
+
#config;
|
|
874
|
+
#format;
|
|
875
|
+
#file;
|
|
876
|
+
#normalizer;
|
|
877
|
+
#redactor;
|
|
878
|
+
#treeBuilder;
|
|
879
|
+
#events = [];
|
|
880
|
+
#warnings = [];
|
|
881
|
+
#trees = [];
|
|
882
|
+
constructor(options) {
|
|
883
|
+
this.#config = mergeLogIngestConfig(options.config, {});
|
|
884
|
+
this.#format = options.format ?? "auto";
|
|
885
|
+
this.#file = options.file;
|
|
886
|
+
this.#normalizer = new EventNormalizer({ config: this.#config });
|
|
887
|
+
this.#redactor = new Redactor({ rules: this.#config.redact });
|
|
888
|
+
this.#treeBuilder = new TreeBuilder({ config: this.#config });
|
|
889
|
+
}
|
|
890
|
+
pushLine(line, lineNumber) {
|
|
891
|
+
try {
|
|
892
|
+
const parsed = parseLogLine(line, {
|
|
893
|
+
format: this.#format,
|
|
894
|
+
file: this.#file,
|
|
895
|
+
line: lineNumber
|
|
896
|
+
});
|
|
897
|
+
const normalized = this.#normalizer.normalizeAll(parsed.records);
|
|
898
|
+
const redactedEvents = normalized.records.map((e) => ({
|
|
899
|
+
...e,
|
|
900
|
+
attributes: e.attributes ? this.#redactor.redactRecord(e.attributes) : void 0
|
|
901
|
+
}));
|
|
902
|
+
this.#events = [...this.#events, ...redactedEvents];
|
|
903
|
+
this.#warnings = [...this.#warnings, ...parsed.warnings, ...normalized.warnings];
|
|
904
|
+
this.#trees = this.#treeBuilder.build(this.#events);
|
|
905
|
+
return {
|
|
906
|
+
events: this.#events,
|
|
907
|
+
trees: this.#trees,
|
|
908
|
+
warnings: this.#warnings
|
|
909
|
+
};
|
|
910
|
+
} catch (e) {
|
|
911
|
+
const msg = e instanceof Error ? e.message : String(e);
|
|
912
|
+
const warning = {
|
|
913
|
+
code: "UNKNOWN",
|
|
914
|
+
message: `LiveLogAccumulator failed to process line (${msg})`,
|
|
915
|
+
file: this.#file,
|
|
916
|
+
line: lineNumber,
|
|
917
|
+
raw: typeof line === "string" ? line.slice(0, 500) : void 0
|
|
918
|
+
};
|
|
919
|
+
this.#warnings = [...this.#warnings, warning];
|
|
920
|
+
return { events: this.#events, trees: this.#trees, warnings: this.#warnings };
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
getEvents() {
|
|
924
|
+
return this.#events;
|
|
925
|
+
}
|
|
926
|
+
getTrees() {
|
|
927
|
+
return this.#trees;
|
|
928
|
+
}
|
|
929
|
+
getWarnings() {
|
|
930
|
+
return this.#warnings;
|
|
931
|
+
}
|
|
932
|
+
reset() {
|
|
933
|
+
this.#events = [];
|
|
934
|
+
this.#trees = [];
|
|
935
|
+
this.#warnings = [];
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
|
|
939
|
+
// packages/core/src/logs/index.ts
|
|
940
|
+
function firstNonEmptyLine(text) {
|
|
941
|
+
for (const line of text.split(/\r?\n/)) {
|
|
942
|
+
const t = line.trim();
|
|
943
|
+
if (t !== "") return t;
|
|
944
|
+
}
|
|
945
|
+
return void 0;
|
|
946
|
+
}
|
|
947
|
+
async function detectFormat(filePath) {
|
|
948
|
+
const text = await readFile(filePath, "utf-8");
|
|
949
|
+
const first = firstNonEmptyLine(text);
|
|
950
|
+
if (!first) return "log4js";
|
|
951
|
+
if (!first.startsWith("{")) return "log4js";
|
|
952
|
+
try {
|
|
953
|
+
const parsed = JSON.parse(first);
|
|
954
|
+
if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) return "json";
|
|
955
|
+
} catch {
|
|
956
|
+
}
|
|
957
|
+
return "log4js";
|
|
958
|
+
}
|
|
959
|
+
function applyOverrides(cfg, options) {
|
|
960
|
+
const override = {};
|
|
961
|
+
for (const k of [
|
|
962
|
+
"runIdKeys",
|
|
963
|
+
"eventKey",
|
|
964
|
+
"timestampKey",
|
|
965
|
+
"messageKey",
|
|
966
|
+
"levelKey",
|
|
967
|
+
"parentIdKey",
|
|
968
|
+
"durationKey",
|
|
969
|
+
"statusKey"
|
|
970
|
+
]) {
|
|
971
|
+
const v = options[k];
|
|
972
|
+
if (v !== void 0) override[k] = v;
|
|
973
|
+
}
|
|
974
|
+
return mergeLogIngestConfig(cfg, override);
|
|
975
|
+
}
|
|
976
|
+
async function parseLogsToTrees(filePath, options = {}) {
|
|
977
|
+
const base = options.config ?? await loadLogIngestConfig(options.configPath);
|
|
978
|
+
const config = applyOverrides(base, options);
|
|
979
|
+
const format = options.format === "auto" || options.format === void 0 ? await detectFormat(filePath) : options.format;
|
|
980
|
+
let parsed;
|
|
981
|
+
if (format === "json") {
|
|
982
|
+
parsed = await new JsonLogParser().parseFile(filePath);
|
|
983
|
+
} else {
|
|
984
|
+
parsed = await new Log4jsParser().parseFile(filePath);
|
|
985
|
+
}
|
|
986
|
+
const normalizer = new EventNormalizer({ config });
|
|
987
|
+
const normalized = normalizer.normalizeAll(parsed.records);
|
|
988
|
+
const redactor = new Redactor({ rules: config.redact });
|
|
989
|
+
const events = normalized.records.map((e) => ({
|
|
990
|
+
...e,
|
|
991
|
+
attributes: e.attributes ? redactor.redactRecord(e.attributes) : void 0
|
|
992
|
+
}));
|
|
993
|
+
const trees = new TreeBuilder({ config }).build(events);
|
|
994
|
+
return {
|
|
995
|
+
events,
|
|
996
|
+
trees,
|
|
997
|
+
warnings: [...parsed.warnings, ...normalized.warnings]
|
|
998
|
+
};
|
|
999
|
+
}
|
|
1000
|
+
|
|
1001
|
+
// packages/core/src/utils/duration.ts
|
|
1002
|
+
function parseDuration(duration) {
|
|
1003
|
+
const raw = typeof duration === "string" ? duration.trim() : "";
|
|
1004
|
+
const match = raw.match(/^(\d+)(ms|[smhd])$/);
|
|
1005
|
+
if (!match) {
|
|
1006
|
+
throw new Error(
|
|
1007
|
+
`Invalid duration format: ${duration}. Use a positive integer followed by ms, s, m, h, or d (e.g. 500ms, 30s, 5m, 2h, 7d).`
|
|
1008
|
+
);
|
|
1009
|
+
}
|
|
1010
|
+
const amount = Number.parseInt(match[1], 10);
|
|
1011
|
+
const unit = match[2];
|
|
1012
|
+
if (!Number.isFinite(amount) || amount <= 0) {
|
|
1013
|
+
throw new Error(
|
|
1014
|
+
`Invalid duration amount: ${duration}. Amount must be a positive integer.`
|
|
1015
|
+
);
|
|
1016
|
+
}
|
|
1017
|
+
switch (unit) {
|
|
1018
|
+
case "ms":
|
|
1019
|
+
return amount;
|
|
1020
|
+
case "s":
|
|
1021
|
+
return amount * 1e3;
|
|
1022
|
+
case "m":
|
|
1023
|
+
return amount * 60 * 1e3;
|
|
1024
|
+
case "h":
|
|
1025
|
+
return amount * 60 * 60 * 1e3;
|
|
1026
|
+
case "d":
|
|
1027
|
+
return amount * 24 * 60 * 60 * 1e3;
|
|
1028
|
+
default: {
|
|
1029
|
+
throw new Error(`Unknown duration unit: ${unit}`);
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
function formatDuration(ms) {
|
|
1034
|
+
if (!Number.isFinite(ms)) {
|
|
1035
|
+
return "0ms";
|
|
1036
|
+
}
|
|
1037
|
+
if (ms < 0) {
|
|
1038
|
+
throw new Error(`formatDuration: ms must be non-negative (got ${ms})`);
|
|
1039
|
+
}
|
|
1040
|
+
if (ms < 1e3) {
|
|
1041
|
+
return `${Math.floor(ms)}ms`;
|
|
1042
|
+
}
|
|
1043
|
+
if (ms < 6e4) {
|
|
1044
|
+
return `${(ms / 1e3).toFixed(2)}s`;
|
|
1045
|
+
}
|
|
1046
|
+
if (ms < 36e5) {
|
|
1047
|
+
return `${(ms / 6e4).toFixed(1)}m`;
|
|
1048
|
+
}
|
|
1049
|
+
return `${(ms / 36e5).toFixed(1)}h`;
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
// packages/core/src/utils.ts
|
|
54
1053
|
var DEFAULT_TRACE_DIR_NAME = ".agent-inspect";
|
|
55
1054
|
var RUNS_DIR_NAME = "runs";
|
|
56
1055
|
var FALLBACK_TRACE_DIR = path.join(
|
|
@@ -65,15 +1064,8 @@ function createRunId() {
|
|
|
65
1064
|
function createStepId() {
|
|
66
1065
|
return `step_${nanoid(10)}`;
|
|
67
1066
|
}
|
|
68
|
-
function
|
|
69
|
-
|
|
70
|
-
return "0ms";
|
|
71
|
-
}
|
|
72
|
-
if (ms < 1e3) {
|
|
73
|
-
return `${Math.floor(ms)}ms`;
|
|
74
|
-
}
|
|
75
|
-
const seconds = ms / 1e3;
|
|
76
|
-
return `${(Math.round(seconds * 10) / 10).toFixed(1)}s`;
|
|
1067
|
+
function formatDuration2(ms) {
|
|
1068
|
+
return formatDuration(ms);
|
|
77
1069
|
}
|
|
78
1070
|
function formatTimestamp(timestamp) {
|
|
79
1071
|
if (!Number.isFinite(timestamp)) {
|
|
@@ -92,6 +1084,10 @@ function formatTimestamp(timestamp) {
|
|
|
92
1084
|
return `${y}-${mo}-${day} ${h}:${min}:${s}`;
|
|
93
1085
|
}
|
|
94
1086
|
function getDefaultTraceDir() {
|
|
1087
|
+
const envDir = process.env.AGENT_INSPECT_TRACE_DIR;
|
|
1088
|
+
if (typeof envDir === "string" && envDir.trim() !== "") {
|
|
1089
|
+
return envDir.trim();
|
|
1090
|
+
}
|
|
95
1091
|
try {
|
|
96
1092
|
const home = os.homedir();
|
|
97
1093
|
if (typeof home !== "string" || home.trim() === "") {
|
|
@@ -307,7 +1303,7 @@ function runWithStepContext(stepId, fn) {
|
|
|
307
1303
|
});
|
|
308
1304
|
});
|
|
309
1305
|
}
|
|
310
|
-
function
|
|
1306
|
+
function isRecord6(value) {
|
|
311
1307
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
312
1308
|
}
|
|
313
1309
|
function nonEmptyString(value) {
|
|
@@ -318,7 +1314,7 @@ function finiteNumber(value) {
|
|
|
318
1314
|
}
|
|
319
1315
|
function optionalErrorInfo(value) {
|
|
320
1316
|
if (value === void 0) return true;
|
|
321
|
-
if (!
|
|
1317
|
+
if (!isRecord6(value)) return false;
|
|
322
1318
|
if (typeof value.message !== "string") return false;
|
|
323
1319
|
if ("stack" in value && value.stack !== void 0) {
|
|
324
1320
|
if (typeof value.stack !== "string") return false;
|
|
@@ -326,7 +1322,7 @@ function optionalErrorInfo(value) {
|
|
|
326
1322
|
return true;
|
|
327
1323
|
}
|
|
328
1324
|
function validateEvent(event) {
|
|
329
|
-
if (!
|
|
1325
|
+
if (!isRecord6(event)) return false;
|
|
330
1326
|
if (event.schemaVersion !== "0.1") return false;
|
|
331
1327
|
if (!finiteNumber(event.timestamp)) return false;
|
|
332
1328
|
if (typeof event.event !== "string") return false;
|
|
@@ -335,7 +1331,7 @@ function validateEvent(event) {
|
|
|
335
1331
|
if (!nonEmptyString(event.runId) || !nonEmptyString(event.name) || !finiteNumber(event.startTime)) {
|
|
336
1332
|
return false;
|
|
337
1333
|
}
|
|
338
|
-
if (event.metadata !== void 0 && !
|
|
1334
|
+
if (event.metadata !== void 0 && !isRecord6(event.metadata)) {
|
|
339
1335
|
return false;
|
|
340
1336
|
}
|
|
341
1337
|
return true;
|
|
@@ -350,7 +1346,7 @@ function validateEvent(event) {
|
|
|
350
1346
|
if (event.parentId !== void 0 && typeof event.parentId !== "string") {
|
|
351
1347
|
return false;
|
|
352
1348
|
}
|
|
353
|
-
if (event.metadata !== void 0 && !
|
|
1349
|
+
if (event.metadata !== void 0 && !isRecord6(event.metadata)) {
|
|
354
1350
|
return false;
|
|
355
1351
|
}
|
|
356
1352
|
return true;
|
|
@@ -494,6 +1490,898 @@ function getRunIdFromTraceFileName(fileName) {
|
|
|
494
1490
|
return void 0;
|
|
495
1491
|
}
|
|
496
1492
|
}
|
|
1493
|
+
function resolveTraceDir(options = {}) {
|
|
1494
|
+
if (typeof options.dir === "string" && options.dir.trim() !== "") {
|
|
1495
|
+
return options.dir.trim();
|
|
1496
|
+
}
|
|
1497
|
+
const envDir = process.env.AGENT_INSPECT_TRACE_DIR;
|
|
1498
|
+
if (typeof envDir === "string" && envDir.trim() !== "") {
|
|
1499
|
+
return envDir.trim();
|
|
1500
|
+
}
|
|
1501
|
+
return getDefaultTraceDir();
|
|
1502
|
+
}
|
|
1503
|
+
var TraceDirectory = class {
|
|
1504
|
+
#dir;
|
|
1505
|
+
constructor(options = {}) {
|
|
1506
|
+
this.#dir = resolveTraceDir(options);
|
|
1507
|
+
}
|
|
1508
|
+
getPath(filename) {
|
|
1509
|
+
return filename ? path.join(this.#dir, filename) : this.#dir;
|
|
1510
|
+
}
|
|
1511
|
+
async list() {
|
|
1512
|
+
try {
|
|
1513
|
+
const files = await readdir(this.#dir);
|
|
1514
|
+
return files.filter((f) => f.endsWith(".jsonl"));
|
|
1515
|
+
} catch (e) {
|
|
1516
|
+
if (e && typeof e === "object" && "code" in e && e.code === "ENOENT") {
|
|
1517
|
+
return [];
|
|
1518
|
+
}
|
|
1519
|
+
throw e;
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
async getFileStats(filename) {
|
|
1523
|
+
return await stat(this.getPath(filename));
|
|
1524
|
+
}
|
|
1525
|
+
};
|
|
1526
|
+
function isFiniteNumber2(v) {
|
|
1527
|
+
return typeof v === "number" && Number.isFinite(v);
|
|
1528
|
+
}
|
|
1529
|
+
function safeParseJson(line) {
|
|
1530
|
+
try {
|
|
1531
|
+
return JSON.parse(line);
|
|
1532
|
+
} catch {
|
|
1533
|
+
return void 0;
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
async function extractMetadata(filePath, _quickScan) {
|
|
1537
|
+
const stats = await stat(filePath);
|
|
1538
|
+
let runIdFromFile = path.basename(filePath);
|
|
1539
|
+
if (runIdFromFile.endsWith(".jsonl")) {
|
|
1540
|
+
runIdFromFile = runIdFromFile.slice(0, -".jsonl".length);
|
|
1541
|
+
}
|
|
1542
|
+
const raw = await readFile(filePath, "utf-8");
|
|
1543
|
+
const lines = raw.split(/\r?\n/);
|
|
1544
|
+
let eventCount = 0;
|
|
1545
|
+
let runId;
|
|
1546
|
+
let name;
|
|
1547
|
+
let startedAt;
|
|
1548
|
+
let endedAt;
|
|
1549
|
+
let explicitDurationMs;
|
|
1550
|
+
let hasRunStarted = false;
|
|
1551
|
+
let hasRunCompleted = false;
|
|
1552
|
+
let runCompletedStatus;
|
|
1553
|
+
let anyStepError = false;
|
|
1554
|
+
let anyKnownEvent = false;
|
|
1555
|
+
for (const line of lines) {
|
|
1556
|
+
const trimmed = line.trim();
|
|
1557
|
+
if (trimmed === "") continue;
|
|
1558
|
+
const parsed = safeParseJson(trimmed);
|
|
1559
|
+
if (!parsed) continue;
|
|
1560
|
+
if (!isTraceEvent(parsed)) continue;
|
|
1561
|
+
const e = parsed;
|
|
1562
|
+
anyKnownEvent = true;
|
|
1563
|
+
eventCount += 1;
|
|
1564
|
+
if (runId === void 0 && typeof e.runId === "string") {
|
|
1565
|
+
runId = e.runId;
|
|
1566
|
+
}
|
|
1567
|
+
if (e.event === "run_started") {
|
|
1568
|
+
hasRunStarted = true;
|
|
1569
|
+
const rs = e;
|
|
1570
|
+
if (typeof rs.name === "string" && rs.name.trim() !== "") {
|
|
1571
|
+
name = rs.name;
|
|
1572
|
+
}
|
|
1573
|
+
if (isFiniteNumber2(rs.startTime)) {
|
|
1574
|
+
startedAt = rs.startTime;
|
|
1575
|
+
} else if (isFiniteNumber2(rs.timestamp)) {
|
|
1576
|
+
startedAt = rs.timestamp;
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
if (e.event === "run_completed") {
|
|
1580
|
+
hasRunCompleted = true;
|
|
1581
|
+
const rc = e;
|
|
1582
|
+
runCompletedStatus = rc.status;
|
|
1583
|
+
if (isFiniteNumber2(rc.endTime)) endedAt = rc.endTime;
|
|
1584
|
+
else if (isFiniteNumber2(rc.timestamp)) endedAt = rc.timestamp;
|
|
1585
|
+
if (isFiniteNumber2(rc.durationMs)) explicitDurationMs = rc.durationMs;
|
|
1586
|
+
}
|
|
1587
|
+
if (e.event === "step_completed") {
|
|
1588
|
+
const sc = e;
|
|
1589
|
+
if (sc.status === "error") {
|
|
1590
|
+
anyStepError = true;
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
}
|
|
1594
|
+
const resolvedRunId = runId ?? runIdFromFile;
|
|
1595
|
+
let status = "unknown";
|
|
1596
|
+
if (hasRunCompleted && (runCompletedStatus === "success" || runCompletedStatus === "error")) {
|
|
1597
|
+
status = runCompletedStatus;
|
|
1598
|
+
} else if (anyStepError) {
|
|
1599
|
+
status = "error";
|
|
1600
|
+
} else if (hasRunStarted && !hasRunCompleted) {
|
|
1601
|
+
status = "running";
|
|
1602
|
+
} else if (anyKnownEvent) {
|
|
1603
|
+
status = "unknown";
|
|
1604
|
+
} else {
|
|
1605
|
+
status = "unknown";
|
|
1606
|
+
}
|
|
1607
|
+
const durationMs = explicitDurationMs ?? (startedAt !== void 0 && endedAt !== void 0 && Number.isFinite(startedAt) && Number.isFinite(endedAt) && endedAt >= startedAt ? endedAt - startedAt : void 0);
|
|
1608
|
+
return {
|
|
1609
|
+
runId: resolvedRunId,
|
|
1610
|
+
name,
|
|
1611
|
+
status,
|
|
1612
|
+
startedAt,
|
|
1613
|
+
endedAt,
|
|
1614
|
+
durationMs,
|
|
1615
|
+
eventCount,
|
|
1616
|
+
filePath,
|
|
1617
|
+
fileSize: stats.size,
|
|
1618
|
+
createdAt: stats.birthtime
|
|
1619
|
+
};
|
|
1620
|
+
}
|
|
1621
|
+
function buildRunSummary(events) {
|
|
1622
|
+
const started = events.find(
|
|
1623
|
+
(e) => e.event === "run_started"
|
|
1624
|
+
);
|
|
1625
|
+
const completed = events.filter(
|
|
1626
|
+
(e) => e.event === "run_completed"
|
|
1627
|
+
);
|
|
1628
|
+
const lastCompleted = completed[completed.length - 1];
|
|
1629
|
+
const runId = started?.runId ?? events.find((e) => typeof e.runId === "string")?.runId ?? "unknown-run";
|
|
1630
|
+
const name = typeof started?.name === "string" && started.name.trim() !== "" ? started.name : void 0;
|
|
1631
|
+
const status = lastCompleted ? lastCompleted.status : started ? "running" : "unknown";
|
|
1632
|
+
const durationMs = lastCompleted && isFiniteNumber2(lastCompleted.durationMs) ? lastCompleted.durationMs : void 0;
|
|
1633
|
+
started && isFiniteNumber2(started.startTime) ? started.startTime : void 0;
|
|
1634
|
+
const steps = /* @__PURE__ */ new Map();
|
|
1635
|
+
for (const e of events) {
|
|
1636
|
+
if (e.event === "step_started") {
|
|
1637
|
+
const s = e;
|
|
1638
|
+
steps.set(s.stepId, {
|
|
1639
|
+
type: s.type,
|
|
1640
|
+
name: s.name,
|
|
1641
|
+
status: "running",
|
|
1642
|
+
parentId: s.parentId,
|
|
1643
|
+
tokensInput: typeof s.metadata?.tokens?.input === "number" ? s.metadata.tokens.input : void 0,
|
|
1644
|
+
tokensOutput: typeof s.metadata?.tokens?.output === "number" ? s.metadata.tokens.output : void 0
|
|
1645
|
+
});
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
for (const e of events) {
|
|
1649
|
+
if (e.event === "step_completed") {
|
|
1650
|
+
const c = e;
|
|
1651
|
+
const existing = steps.get(c.stepId);
|
|
1652
|
+
if (!existing) continue;
|
|
1653
|
+
existing.status = c.status;
|
|
1654
|
+
existing.durationMs = c.durationMs;
|
|
1655
|
+
}
|
|
1656
|
+
}
|
|
1657
|
+
let totalSteps = 0;
|
|
1658
|
+
let llmSteps = 0;
|
|
1659
|
+
let toolSteps = 0;
|
|
1660
|
+
let logicSteps = 0;
|
|
1661
|
+
let errorSteps = 0;
|
|
1662
|
+
let maxDepth = 0;
|
|
1663
|
+
let longestStep;
|
|
1664
|
+
let totalTokensInput = 0;
|
|
1665
|
+
let totalTokensOutput = 0;
|
|
1666
|
+
let hasAnyTokens = false;
|
|
1667
|
+
const depthCache = /* @__PURE__ */ new Map();
|
|
1668
|
+
const computeDepth = (stepId) => {
|
|
1669
|
+
const cached = depthCache.get(stepId);
|
|
1670
|
+
if (cached !== void 0) return cached;
|
|
1671
|
+
const node = steps.get(stepId);
|
|
1672
|
+
if (!node) return 0;
|
|
1673
|
+
const parent = node.parentId;
|
|
1674
|
+
if (typeof parent !== "string" || parent.trim() === "" || !steps.has(parent)) {
|
|
1675
|
+
depthCache.set(stepId, 0);
|
|
1676
|
+
return 0;
|
|
1677
|
+
}
|
|
1678
|
+
const d = Math.min(1e3, computeDepth(parent) + 1);
|
|
1679
|
+
depthCache.set(stepId, d);
|
|
1680
|
+
return d;
|
|
1681
|
+
};
|
|
1682
|
+
for (const [id, s] of steps.entries()) {
|
|
1683
|
+
totalSteps += 1;
|
|
1684
|
+
if (s.type === "llm") llmSteps += 1;
|
|
1685
|
+
else if (s.type === "tool") toolSteps += 1;
|
|
1686
|
+
else logicSteps += 1;
|
|
1687
|
+
if (s.status === "error") errorSteps += 1;
|
|
1688
|
+
const depth = computeDepth(id);
|
|
1689
|
+
if (depth > maxDepth) maxDepth = depth;
|
|
1690
|
+
if (typeof s.durationMs === "number" && Number.isFinite(s.durationMs)) {
|
|
1691
|
+
if (!longestStep || s.durationMs > longestStep.durationMs) {
|
|
1692
|
+
longestStep = { name: s.name, durationMs: s.durationMs, type: s.type };
|
|
1693
|
+
}
|
|
1694
|
+
}
|
|
1695
|
+
if (typeof s.tokensInput === "number" || typeof s.tokensOutput === "number") {
|
|
1696
|
+
hasAnyTokens = true;
|
|
1697
|
+
if (typeof s.tokensInput === "number") totalTokensInput += s.tokensInput;
|
|
1698
|
+
if (typeof s.tokensOutput === "number") totalTokensOutput += s.tokensOutput;
|
|
1699
|
+
}
|
|
1700
|
+
}
|
|
1701
|
+
const summary = {
|
|
1702
|
+
runId,
|
|
1703
|
+
name,
|
|
1704
|
+
status,
|
|
1705
|
+
durationMs,
|
|
1706
|
+
totalSteps,
|
|
1707
|
+
llmSteps,
|
|
1708
|
+
toolSteps,
|
|
1709
|
+
logicSteps,
|
|
1710
|
+
errorSteps,
|
|
1711
|
+
maxDepth,
|
|
1712
|
+
...longestStep ? { longestStep } : {},
|
|
1713
|
+
...hasAnyTokens ? { totalTokens: { input: totalTokensInput, output: totalTokensOutput } } : {}
|
|
1714
|
+
};
|
|
1715
|
+
return summary;
|
|
1716
|
+
}
|
|
1717
|
+
|
|
1718
|
+
// packages/core/src/trace-filter.ts
|
|
1719
|
+
function toLower(s) {
|
|
1720
|
+
return typeof s === "string" ? s.toLowerCase() : "";
|
|
1721
|
+
}
|
|
1722
|
+
function filterTraces(traces, options) {
|
|
1723
|
+
const input = [...traces];
|
|
1724
|
+
let out = input.filter((t) => {
|
|
1725
|
+
if (options.status && t.status !== options.status) return false;
|
|
1726
|
+
if (options.name) {
|
|
1727
|
+
const q = options.name.toLowerCase();
|
|
1728
|
+
const hay = `${toLower(t.name)} ${toLower(t.runId)}`;
|
|
1729
|
+
if (!hay.includes(q)) return false;
|
|
1730
|
+
}
|
|
1731
|
+
if (options.since) {
|
|
1732
|
+
const windowMs = parseDuration(options.since);
|
|
1733
|
+
const cutoff = Date.now() - windowMs;
|
|
1734
|
+
const started = typeof t.startedAt === "number" ? t.startedAt : void 0;
|
|
1735
|
+
const basis = started ?? t.createdAt.getTime();
|
|
1736
|
+
if (!Number.isFinite(basis) || basis < cutoff) return false;
|
|
1737
|
+
}
|
|
1738
|
+
return true;
|
|
1739
|
+
});
|
|
1740
|
+
out.sort((a, b) => {
|
|
1741
|
+
const aTime = (typeof a.startedAt === "number" ? a.startedAt : void 0) ?? a.createdAt.getTime();
|
|
1742
|
+
const bTime = (typeof b.startedAt === "number" ? b.startedAt : void 0) ?? b.createdAt.getTime();
|
|
1743
|
+
return bTime - aTime;
|
|
1744
|
+
});
|
|
1745
|
+
if (typeof options.limit === "number" && Number.isFinite(options.limit)) {
|
|
1746
|
+
const n = Math.max(0, Math.floor(options.limit));
|
|
1747
|
+
out = out.slice(0, n);
|
|
1748
|
+
}
|
|
1749
|
+
return out;
|
|
1750
|
+
}
|
|
1751
|
+
var KNOWN_EVENTS = /* @__PURE__ */ new Set([
|
|
1752
|
+
"run_started",
|
|
1753
|
+
"run_completed",
|
|
1754
|
+
"step_started",
|
|
1755
|
+
"step_completed"
|
|
1756
|
+
]);
|
|
1757
|
+
function isRecord7(value) {
|
|
1758
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1759
|
+
}
|
|
1760
|
+
function safeParse(line) {
|
|
1761
|
+
try {
|
|
1762
|
+
return JSON.parse(line);
|
|
1763
|
+
} catch {
|
|
1764
|
+
return void 0;
|
|
1765
|
+
}
|
|
1766
|
+
}
|
|
1767
|
+
async function isAgentInspectTrace(filePath) {
|
|
1768
|
+
try {
|
|
1769
|
+
const rl = createInterface({
|
|
1770
|
+
input: createReadStream(filePath, { encoding: "utf8" }),
|
|
1771
|
+
crlfDelay: Infinity
|
|
1772
|
+
});
|
|
1773
|
+
let checked = 0;
|
|
1774
|
+
for await (const line of rl) {
|
|
1775
|
+
const trimmed = line.trim();
|
|
1776
|
+
if (trimmed === "") continue;
|
|
1777
|
+
const parsed = safeParse(trimmed);
|
|
1778
|
+
if (!parsed) continue;
|
|
1779
|
+
if (!isRecord7(parsed)) continue;
|
|
1780
|
+
checked += 1;
|
|
1781
|
+
if (isTraceEvent(parsed)) return true;
|
|
1782
|
+
const ev = parsed.event;
|
|
1783
|
+
const runId = parsed.runId;
|
|
1784
|
+
if (typeof ev === "string" && KNOWN_EVENTS.has(ev) && typeof runId === "string") {
|
|
1785
|
+
return true;
|
|
1786
|
+
}
|
|
1787
|
+
if (checked >= 20) break;
|
|
1788
|
+
}
|
|
1789
|
+
return false;
|
|
1790
|
+
} catch {
|
|
1791
|
+
return false;
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
|
|
1795
|
+
// packages/core/src/diff/comparable.ts
|
|
1796
|
+
function extractOutputPreview(meta) {
|
|
1797
|
+
if (meta === void 0) return void 0;
|
|
1798
|
+
if ("outputPreview" in meta) return meta.outputPreview;
|
|
1799
|
+
if ("resultPreview" in meta) return meta.resultPreview;
|
|
1800
|
+
return void 0;
|
|
1801
|
+
}
|
|
1802
|
+
function mapStepStatus(s) {
|
|
1803
|
+
if (s === void 0) return "running";
|
|
1804
|
+
return s;
|
|
1805
|
+
}
|
|
1806
|
+
function manualTraceEventsToComparableRun(events) {
|
|
1807
|
+
const started = events.find((e) => e.event === "run_started");
|
|
1808
|
+
if (!started || started.event !== "run_started") {
|
|
1809
|
+
throw new Error("Invalid trace: missing run_started");
|
|
1810
|
+
}
|
|
1811
|
+
const rs = started;
|
|
1812
|
+
const runId = rs.runId;
|
|
1813
|
+
const completedAll = events.filter((e) => e.event === "run_completed");
|
|
1814
|
+
const lastCompleted = completedAll[completedAll.length - 1];
|
|
1815
|
+
let runStatus;
|
|
1816
|
+
if (lastCompleted === void 0) runStatus = "running";
|
|
1817
|
+
else runStatus = lastCompleted.status;
|
|
1818
|
+
const durationMs = lastCompleted !== void 0 && Number.isFinite(lastCompleted.durationMs) ? lastCompleted.durationMs : void 0;
|
|
1819
|
+
const steps = /* @__PURE__ */ new Map();
|
|
1820
|
+
let order = 0;
|
|
1821
|
+
for (const e of events) {
|
|
1822
|
+
if (e.event !== "step_started") continue;
|
|
1823
|
+
const s = e;
|
|
1824
|
+
const meta = s.metadata ? { ...s.metadata } : void 0;
|
|
1825
|
+
steps.set(s.stepId, {
|
|
1826
|
+
id: s.stepId,
|
|
1827
|
+
parentId: s.parentId,
|
|
1828
|
+
name: s.name,
|
|
1829
|
+
type: s.type,
|
|
1830
|
+
order: order++,
|
|
1831
|
+
timestamp: s.timestamp,
|
|
1832
|
+
metadata: meta
|
|
1833
|
+
});
|
|
1834
|
+
}
|
|
1835
|
+
for (const e of events) {
|
|
1836
|
+
if (e.event !== "step_completed") continue;
|
|
1837
|
+
const acc = steps.get(e.stepId);
|
|
1838
|
+
if (!acc) continue;
|
|
1839
|
+
acc.status = e.status;
|
|
1840
|
+
acc.durationMs = e.durationMs;
|
|
1841
|
+
if (e.error?.message) acc.errorMsg = e.error.message;
|
|
1842
|
+
const extra = e;
|
|
1843
|
+
if (extra.metadata !== void 0 && typeof extra.metadata === "object") {
|
|
1844
|
+
acc.metadata = { ...acc.metadata ?? {}, ...extra.metadata };
|
|
1845
|
+
}
|
|
1846
|
+
}
|
|
1847
|
+
const nodes = /* @__PURE__ */ new Map();
|
|
1848
|
+
for (const acc of steps.values()) {
|
|
1849
|
+
let meta = acc.metadata ? { ...acc.metadata } : void 0;
|
|
1850
|
+
if (acc.parentId !== void 0 && !steps.has(acc.parentId)) {
|
|
1851
|
+
meta = { ...meta ?? {}, agent_inspect_diff_parent_missing: true };
|
|
1852
|
+
}
|
|
1853
|
+
const outputPreview = extractOutputPreview(meta);
|
|
1854
|
+
const sc = {
|
|
1855
|
+
id: acc.id,
|
|
1856
|
+
name: acc.name,
|
|
1857
|
+
type: acc.type,
|
|
1858
|
+
status: mapStepStatus(acc.status),
|
|
1859
|
+
durationMs: acc.durationMs,
|
|
1860
|
+
error: acc.errorMsg,
|
|
1861
|
+
metadata: meta && Object.keys(meta).length > 0 ? meta : void 0,
|
|
1862
|
+
outputPreview,
|
|
1863
|
+
children: []
|
|
1864
|
+
};
|
|
1865
|
+
nodes.set(acc.id, sc);
|
|
1866
|
+
}
|
|
1867
|
+
const roots = [];
|
|
1868
|
+
const sortByOrder = (a, b) => {
|
|
1869
|
+
const oa = steps.get(a.id)?.order ?? 0;
|
|
1870
|
+
const ob = steps.get(b.id)?.order ?? 0;
|
|
1871
|
+
return oa - ob;
|
|
1872
|
+
};
|
|
1873
|
+
for (const acc of steps.values()) {
|
|
1874
|
+
const node = nodes.get(acc.id);
|
|
1875
|
+
if (acc.parentId !== void 0 && nodes.has(acc.parentId)) {
|
|
1876
|
+
nodes.get(acc.parentId).children.push(node);
|
|
1877
|
+
} else {
|
|
1878
|
+
roots.push(node);
|
|
1879
|
+
}
|
|
1880
|
+
}
|
|
1881
|
+
roots.sort(sortByOrder);
|
|
1882
|
+
for (const n of nodes.values()) {
|
|
1883
|
+
n.children.sort(sortByOrder);
|
|
1884
|
+
}
|
|
1885
|
+
return {
|
|
1886
|
+
runId,
|
|
1887
|
+
name: rs.name,
|
|
1888
|
+
status: runStatus,
|
|
1889
|
+
durationMs,
|
|
1890
|
+
steps: roots
|
|
1891
|
+
};
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
// packages/core/src/exporters/helpers.ts
|
|
1895
|
+
var REDACT_SUBSTRINGS = [
|
|
1896
|
+
"authorization",
|
|
1897
|
+
"cookie",
|
|
1898
|
+
"token",
|
|
1899
|
+
"apikey",
|
|
1900
|
+
"password",
|
|
1901
|
+
"secret",
|
|
1902
|
+
"email"
|
|
1903
|
+
];
|
|
1904
|
+
function shouldRedactKey(key) {
|
|
1905
|
+
const k = key.toLowerCase();
|
|
1906
|
+
for (const s of REDACT_SUBSTRINGS) {
|
|
1907
|
+
if (k.includes(s)) return true;
|
|
1908
|
+
}
|
|
1909
|
+
return false;
|
|
1910
|
+
}
|
|
1911
|
+
function safeString2(value, maxLength) {
|
|
1912
|
+
if (value === null || value === void 0) return "";
|
|
1913
|
+
let s;
|
|
1914
|
+
if (typeof value === "string") s = value;
|
|
1915
|
+
else if (typeof value === "number" || typeof value === "boolean") s = String(value);
|
|
1916
|
+
else s = stableJson(value, false);
|
|
1917
|
+
if (maxLength !== void 0 && maxLength >= 0 && s.length > maxLength) {
|
|
1918
|
+
return `${s.slice(0, maxLength)}\u2026`;
|
|
1919
|
+
}
|
|
1920
|
+
return s;
|
|
1921
|
+
}
|
|
1922
|
+
function escapeMarkdown(value) {
|
|
1923
|
+
return value.replace(/\|/g, "\\|").replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\n/g, " ");
|
|
1924
|
+
}
|
|
1925
|
+
function escapeHtml(value) {
|
|
1926
|
+
return value.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
1927
|
+
}
|
|
1928
|
+
function sortKeysDeep(input) {
|
|
1929
|
+
if (input === null || typeof input !== "object") return input;
|
|
1930
|
+
if (Array.isArray(input)) return input.map(sortKeysDeep);
|
|
1931
|
+
const o = input;
|
|
1932
|
+
const out = {};
|
|
1933
|
+
for (const k of Object.keys(o).sort()) {
|
|
1934
|
+
out[k] = sortKeysDeep(o[k]);
|
|
1935
|
+
}
|
|
1936
|
+
return out;
|
|
1937
|
+
}
|
|
1938
|
+
function stableJson(value, pretty) {
|
|
1939
|
+
const sorted = sortKeysDeep(value);
|
|
1940
|
+
return pretty === true ? JSON.stringify(sorted, null, 2) : JSON.stringify(sorted);
|
|
1941
|
+
}
|
|
1942
|
+
function compactAttributes(attrs, options) {
|
|
1943
|
+
if (attrs === void 0) return {};
|
|
1944
|
+
const maxLen = options?.maxLength ?? 500;
|
|
1945
|
+
const redacted = options?.redacted ?? true;
|
|
1946
|
+
const out = {};
|
|
1947
|
+
for (const key of Object.keys(attrs).sort()) {
|
|
1948
|
+
if (redacted && shouldRedactKey(key)) {
|
|
1949
|
+
out[key] = "[REDACTED]";
|
|
1950
|
+
continue;
|
|
1951
|
+
}
|
|
1952
|
+
const v = attrs[key];
|
|
1953
|
+
out[key] = compactValue(v, maxLen, redacted);
|
|
1954
|
+
}
|
|
1955
|
+
return out;
|
|
1956
|
+
}
|
|
1957
|
+
function compactValue(value, maxLen, redacted) {
|
|
1958
|
+
if (value === null || typeof value !== "object") {
|
|
1959
|
+
return typeof value === "string" ? safeString2(value, maxLen) : value;
|
|
1960
|
+
}
|
|
1961
|
+
if (Array.isArray(value)) {
|
|
1962
|
+
const arr = value.slice(0, 20).map((x) => compactValue(x, maxLen, redacted));
|
|
1963
|
+
if (value.length > 20) arr.push(`\u2026(+${value.length - 20} more)`);
|
|
1964
|
+
return arr;
|
|
1965
|
+
}
|
|
1966
|
+
const o = value;
|
|
1967
|
+
const inner = {};
|
|
1968
|
+
for (const k of Object.keys(o)) {
|
|
1969
|
+
if (redacted && shouldRedactKey(k)) inner[k] = "[REDACTED]";
|
|
1970
|
+
else inner[k] = compactValue(o[k], maxLen, redacted);
|
|
1971
|
+
}
|
|
1972
|
+
return inner;
|
|
1973
|
+
}
|
|
1974
|
+
function summarizeTree(tree) {
|
|
1975
|
+
const flat = flattenTree(tree);
|
|
1976
|
+
const errorNodes = flat.filter((n) => n.event.status === "error").length;
|
|
1977
|
+
return {
|
|
1978
|
+
runId: tree.runId,
|
|
1979
|
+
name: tree.name,
|
|
1980
|
+
status: tree.status,
|
|
1981
|
+
startedAt: tree.startedAt,
|
|
1982
|
+
endedAt: tree.endedAt,
|
|
1983
|
+
durationMs: tree.durationMs,
|
|
1984
|
+
stepCount: flat.length,
|
|
1985
|
+
errorStepCount: errorNodes,
|
|
1986
|
+
totalEvents: tree.metadata.totalEvents,
|
|
1987
|
+
confidenceBreakdown: { ...tree.metadata.confidenceBreakdown },
|
|
1988
|
+
kinds: { ...tree.metadata.kinds }
|
|
1989
|
+
};
|
|
1990
|
+
}
|
|
1991
|
+
function flattenTree(tree) {
|
|
1992
|
+
const out = [];
|
|
1993
|
+
function walk(nodes) {
|
|
1994
|
+
for (const n of nodes) {
|
|
1995
|
+
out.push(n);
|
|
1996
|
+
if (n.children.length > 0) walk(n.children);
|
|
1997
|
+
}
|
|
1998
|
+
}
|
|
1999
|
+
walk(tree.children);
|
|
2000
|
+
return out;
|
|
2001
|
+
}
|
|
2002
|
+
function zeroKinds() {
|
|
2003
|
+
return {
|
|
2004
|
+
RUN: 0,
|
|
2005
|
+
AGENT: 0,
|
|
2006
|
+
LLM: 0,
|
|
2007
|
+
TOOL: 0,
|
|
2008
|
+
CHAIN: 0,
|
|
2009
|
+
RETRIEVER: 0,
|
|
2010
|
+
DECISION: 0,
|
|
2011
|
+
RESULT: 0,
|
|
2012
|
+
ERROR: 0,
|
|
2013
|
+
LOGIC: 0,
|
|
2014
|
+
LOG: 0
|
|
2015
|
+
};
|
|
2016
|
+
}
|
|
2017
|
+
|
|
2018
|
+
// packages/core/src/diff/engine.ts
|
|
2019
|
+
var DEFAULT_THRESHOLD_MS = 0;
|
|
2020
|
+
function pathSeg(step2, index) {
|
|
2021
|
+
return { index, name: step2.name, stepId: step2.id };
|
|
2022
|
+
}
|
|
2023
|
+
function buildPath(segments) {
|
|
2024
|
+
return { path: [...segments] };
|
|
2025
|
+
}
|
|
2026
|
+
function pairSteps(left, right) {
|
|
2027
|
+
const usedRight = /* @__PURE__ */ new Set();
|
|
2028
|
+
const pairs = [];
|
|
2029
|
+
for (let i = 0; i < left.length; i++) {
|
|
2030
|
+
const L = left[i];
|
|
2031
|
+
let R = right.find((r) => !usedRight.has(r.id) && r.id === L.id);
|
|
2032
|
+
if (R === void 0 && i < right.length && !usedRight.has(right[i].id)) {
|
|
2033
|
+
const cand = right[i];
|
|
2034
|
+
if (cand.name === L.name && (cand.type ?? "") === (L.type ?? "")) {
|
|
2035
|
+
R = cand;
|
|
2036
|
+
}
|
|
2037
|
+
}
|
|
2038
|
+
if (R === void 0) {
|
|
2039
|
+
R = right.find(
|
|
2040
|
+
(r) => !usedRight.has(r.id) && r.name === L.name && (r.type ?? "") === (L.type ?? "")
|
|
2041
|
+
);
|
|
2042
|
+
}
|
|
2043
|
+
if (R !== void 0) {
|
|
2044
|
+
usedRight.add(R.id);
|
|
2045
|
+
pairs.push([L, R]);
|
|
2046
|
+
} else {
|
|
2047
|
+
pairs.push([L, void 0]);
|
|
2048
|
+
}
|
|
2049
|
+
}
|
|
2050
|
+
for (const R of right) {
|
|
2051
|
+
if (!usedRight.has(R.id)) {
|
|
2052
|
+
pairs.push([void 0, R]);
|
|
2053
|
+
}
|
|
2054
|
+
}
|
|
2055
|
+
return pairs;
|
|
2056
|
+
}
|
|
2057
|
+
function compareLeafSteps(L, R, segments, opts, out) {
|
|
2058
|
+
const path5 = buildPath(segments);
|
|
2059
|
+
if (L.name !== R.name) {
|
|
2060
|
+
out.push({
|
|
2061
|
+
kind: "structure",
|
|
2062
|
+
severity: "warning",
|
|
2063
|
+
message: "Step name differs",
|
|
2064
|
+
path: path5,
|
|
2065
|
+
left: L.name,
|
|
2066
|
+
right: R.name
|
|
2067
|
+
});
|
|
2068
|
+
}
|
|
2069
|
+
if ((L.type ?? "") !== (R.type ?? "")) {
|
|
2070
|
+
out.push({
|
|
2071
|
+
kind: "step-type",
|
|
2072
|
+
severity: "warning",
|
|
2073
|
+
message: "Step type differs",
|
|
2074
|
+
path: path5,
|
|
2075
|
+
left: L.type,
|
|
2076
|
+
right: R.type
|
|
2077
|
+
});
|
|
2078
|
+
}
|
|
2079
|
+
if ((L.status ?? "") !== (R.status ?? "")) {
|
|
2080
|
+
out.push({
|
|
2081
|
+
kind: "step-status",
|
|
2082
|
+
severity: "warning",
|
|
2083
|
+
message: "Step status differs",
|
|
2084
|
+
path: path5,
|
|
2085
|
+
left: L.status,
|
|
2086
|
+
right: R.status
|
|
2087
|
+
});
|
|
2088
|
+
}
|
|
2089
|
+
const le = L.error ?? "";
|
|
2090
|
+
const re = R.error ?? "";
|
|
2091
|
+
if (le !== re) {
|
|
2092
|
+
out.push({
|
|
2093
|
+
kind: "error",
|
|
2094
|
+
severity: "error",
|
|
2095
|
+
message: "Step error message differs",
|
|
2096
|
+
path: path5,
|
|
2097
|
+
left: le || void 0,
|
|
2098
|
+
right: re || void 0
|
|
2099
|
+
});
|
|
2100
|
+
}
|
|
2101
|
+
if (!opts.ignoreDuration) {
|
|
2102
|
+
const ld = L.durationMs;
|
|
2103
|
+
const rd = R.durationMs;
|
|
2104
|
+
const th = opts.durationThresholdMs;
|
|
2105
|
+
let differs = false;
|
|
2106
|
+
if (ld === void 0 && rd === void 0) differs = false;
|
|
2107
|
+
else if (ld === void 0 || rd === void 0) differs = true;
|
|
2108
|
+
else differs = Math.abs(ld - rd) > th;
|
|
2109
|
+
if (differs) {
|
|
2110
|
+
out.push({
|
|
2111
|
+
kind: "duration",
|
|
2112
|
+
severity: "info",
|
|
2113
|
+
message: "Step duration differs",
|
|
2114
|
+
path: path5,
|
|
2115
|
+
left: ld,
|
|
2116
|
+
right: rd
|
|
2117
|
+
});
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
const lm = stableJson(L.metadata ?? {});
|
|
2121
|
+
const rm = stableJson(R.metadata ?? {});
|
|
2122
|
+
if (lm !== rm) {
|
|
2123
|
+
out.push({
|
|
2124
|
+
kind: "metadata",
|
|
2125
|
+
severity: "info",
|
|
2126
|
+
message: "Step metadata differs",
|
|
2127
|
+
path: path5,
|
|
2128
|
+
left: L.metadata,
|
|
2129
|
+
right: R.metadata
|
|
2130
|
+
});
|
|
2131
|
+
}
|
|
2132
|
+
const lo = stableJson(L.outputPreview ?? null);
|
|
2133
|
+
const ro = stableJson(R.outputPreview ?? null);
|
|
2134
|
+
if (lo !== ro) {
|
|
2135
|
+
out.push({
|
|
2136
|
+
kind: "output",
|
|
2137
|
+
severity: "info",
|
|
2138
|
+
message: "Output preview differs",
|
|
2139
|
+
path: path5,
|
|
2140
|
+
left: L.outputPreview,
|
|
2141
|
+
right: R.outputPreview
|
|
2142
|
+
});
|
|
2143
|
+
}
|
|
2144
|
+
}
|
|
2145
|
+
function compareRecursive(L, R, segments, opts, out) {
|
|
2146
|
+
compareLeafSteps(L, R, segments, opts, out);
|
|
2147
|
+
const pairs = pairSteps(L.children, R.children);
|
|
2148
|
+
let ci = 0;
|
|
2149
|
+
for (const [lch, rch] of pairs) {
|
|
2150
|
+
if (lch !== void 0 && rch !== void 0) {
|
|
2151
|
+
compareRecursive(lch, rch, [...segments, pathSeg(lch, ci)], opts, out);
|
|
2152
|
+
} else if (lch !== void 0) {
|
|
2153
|
+
out.push({
|
|
2154
|
+
kind: "step-removed",
|
|
2155
|
+
severity: "warning",
|
|
2156
|
+
message: `Step only in left run: ${lch.name}`,
|
|
2157
|
+
path: buildPath([...segments, pathSeg(lch, ci)]),
|
|
2158
|
+
left: lch.id,
|
|
2159
|
+
right: void 0
|
|
2160
|
+
});
|
|
2161
|
+
} else if (rch !== void 0) {
|
|
2162
|
+
out.push({
|
|
2163
|
+
kind: "step-added",
|
|
2164
|
+
severity: "warning",
|
|
2165
|
+
message: `Step only in right run: ${rch.name}`,
|
|
2166
|
+
path: buildPath([...segments, pathSeg(rch, ci)]),
|
|
2167
|
+
left: void 0,
|
|
2168
|
+
right: rch.id
|
|
2169
|
+
});
|
|
2170
|
+
}
|
|
2171
|
+
ci += 1;
|
|
2172
|
+
}
|
|
2173
|
+
}
|
|
2174
|
+
function mergeDiffDefaults(options) {
|
|
2175
|
+
return {
|
|
2176
|
+
ignoreDuration: options?.ignoreDuration ?? false,
|
|
2177
|
+
durationThresholdMs: options?.durationThresholdMs !== void 0 ? options.durationThresholdMs : DEFAULT_THRESHOLD_MS,
|
|
2178
|
+
focus: options?.focus ?? "all",
|
|
2179
|
+
check: options?.check ?? "all"
|
|
2180
|
+
};
|
|
2181
|
+
}
|
|
2182
|
+
function kindMatchesFilter(kind, merged) {
|
|
2183
|
+
const { focus, check } = merged;
|
|
2184
|
+
if (check !== "all") {
|
|
2185
|
+
if (check === "structure") {
|
|
2186
|
+
if (!["step-added", "step-removed", "structure", "step-type"].includes(kind)) return false;
|
|
2187
|
+
} else if (check === "outputs") {
|
|
2188
|
+
if (!["metadata", "output"].includes(kind)) return false;
|
|
2189
|
+
} else if (check === "errors") {
|
|
2190
|
+
if (!["run-status", "step-status", "error"].includes(kind)) return false;
|
|
2191
|
+
} else if (check === "timing") {
|
|
2192
|
+
if (kind !== "duration") return false;
|
|
2193
|
+
}
|
|
2194
|
+
}
|
|
2195
|
+
if (focus !== "all") {
|
|
2196
|
+
if (focus === "errors") {
|
|
2197
|
+
if (!["run-status", "step-status", "error"].includes(kind)) return false;
|
|
2198
|
+
} else if (focus === "structure") {
|
|
2199
|
+
if (!["step-added", "step-removed", "structure", "step-type"].includes(kind)) return false;
|
|
2200
|
+
} else if (focus === "outputs") {
|
|
2201
|
+
if (!["metadata", "output"].includes(kind)) return false;
|
|
2202
|
+
}
|
|
2203
|
+
}
|
|
2204
|
+
return true;
|
|
2205
|
+
}
|
|
2206
|
+
function diffRuns(left, right, options) {
|
|
2207
|
+
const merged = mergeDiffDefaults(options);
|
|
2208
|
+
const opts = {
|
|
2209
|
+
ignoreDuration: merged.ignoreDuration,
|
|
2210
|
+
durationThresholdMs: merged.durationThresholdMs
|
|
2211
|
+
};
|
|
2212
|
+
const raw = [];
|
|
2213
|
+
if ((left.status ?? "") !== (right.status ?? "")) {
|
|
2214
|
+
raw.push({
|
|
2215
|
+
kind: "run-status",
|
|
2216
|
+
severity: "warning",
|
|
2217
|
+
message: "Run completion status differs",
|
|
2218
|
+
left: left.status,
|
|
2219
|
+
right: right.status
|
|
2220
|
+
});
|
|
2221
|
+
}
|
|
2222
|
+
if (!merged.ignoreDuration) {
|
|
2223
|
+
const ld = left.durationMs;
|
|
2224
|
+
const rd = right.durationMs;
|
|
2225
|
+
const th = merged.durationThresholdMs;
|
|
2226
|
+
let differs = false;
|
|
2227
|
+
if (ld === void 0 && rd === void 0) differs = false;
|
|
2228
|
+
else if (ld === void 0 || rd === void 0) differs = true;
|
|
2229
|
+
else differs = Math.abs(ld - rd) > th;
|
|
2230
|
+
if (differs) {
|
|
2231
|
+
raw.push({
|
|
2232
|
+
kind: "duration",
|
|
2233
|
+
severity: "info",
|
|
2234
|
+
message: "Run duration differs",
|
|
2235
|
+
left: ld,
|
|
2236
|
+
right: rd
|
|
2237
|
+
});
|
|
2238
|
+
}
|
|
2239
|
+
}
|
|
2240
|
+
const pairs = pairSteps(left.steps, right.steps);
|
|
2241
|
+
let idx = 0;
|
|
2242
|
+
for (const [ls, rs] of pairs) {
|
|
2243
|
+
if (ls !== void 0 && rs !== void 0) {
|
|
2244
|
+
compareRecursive(ls, rs, [pathSeg(ls, idx)], opts, raw);
|
|
2245
|
+
idx += 1;
|
|
2246
|
+
} else if (ls !== void 0) {
|
|
2247
|
+
raw.push({
|
|
2248
|
+
kind: "step-removed",
|
|
2249
|
+
severity: "warning",
|
|
2250
|
+
message: `Step only in left run: ${ls.name}`,
|
|
2251
|
+
path: buildPath([pathSeg(ls, idx)]),
|
|
2252
|
+
left: ls.id,
|
|
2253
|
+
right: void 0
|
|
2254
|
+
});
|
|
2255
|
+
idx += 1;
|
|
2256
|
+
} else if (rs !== void 0) {
|
|
2257
|
+
raw.push({
|
|
2258
|
+
kind: "step-added",
|
|
2259
|
+
severity: "warning",
|
|
2260
|
+
message: `Step only in right run: ${rs.name}`,
|
|
2261
|
+
path: buildPath([pathSeg(rs, idx)]),
|
|
2262
|
+
left: void 0,
|
|
2263
|
+
right: rs.id
|
|
2264
|
+
});
|
|
2265
|
+
idx += 1;
|
|
2266
|
+
}
|
|
2267
|
+
}
|
|
2268
|
+
const differences = raw.filter((d) => kindMatchesFilter(d.kind, merged));
|
|
2269
|
+
let errors = 0;
|
|
2270
|
+
let warnings = 0;
|
|
2271
|
+
let info = 0;
|
|
2272
|
+
for (const d of differences) {
|
|
2273
|
+
if (d.severity === "error") errors += 1;
|
|
2274
|
+
else if (d.severity === "warning") warnings += 1;
|
|
2275
|
+
else info += 1;
|
|
2276
|
+
}
|
|
2277
|
+
const firstVisible = differences[0];
|
|
2278
|
+
const firstDivergence = firstVisible !== void 0 ? {
|
|
2279
|
+
kind: "first-divergence",
|
|
2280
|
+
severity: firstVisible.severity,
|
|
2281
|
+
message: `First divergence: ${firstVisible.message}`,
|
|
2282
|
+
path: firstVisible.path,
|
|
2283
|
+
left: firstVisible.left,
|
|
2284
|
+
right: firstVisible.right
|
|
2285
|
+
} : void 0;
|
|
2286
|
+
const summary = {
|
|
2287
|
+
leftRunId: left.runId,
|
|
2288
|
+
rightRunId: right.runId,
|
|
2289
|
+
totalDifferences: differences.length,
|
|
2290
|
+
errors,
|
|
2291
|
+
warnings,
|
|
2292
|
+
info,
|
|
2293
|
+
firstDivergence
|
|
2294
|
+
};
|
|
2295
|
+
return { summary, differences };
|
|
2296
|
+
}
|
|
2297
|
+
function formatPath(path5) {
|
|
2298
|
+
if (path5 === void 0 || path5.path.length === 0) {
|
|
2299
|
+
return "(run)";
|
|
2300
|
+
}
|
|
2301
|
+
return path5.path.map((s) => s.name).join(" > ");
|
|
2302
|
+
}
|
|
2303
|
+
function formatValue(v, verbose) {
|
|
2304
|
+
if (v === void 0) return "(undefined)";
|
|
2305
|
+
if (typeof v === "string") return v;
|
|
2306
|
+
if (typeof v === "number" || typeof v === "boolean") return String(v);
|
|
2307
|
+
const s = JSON.stringify(v);
|
|
2308
|
+
if (verbose || s.length <= 120) return s;
|
|
2309
|
+
return `${s.slice(0, 117)}...`;
|
|
2310
|
+
}
|
|
2311
|
+
function renderRunDiff(result, options) {
|
|
2312
|
+
const json = options?.json === true;
|
|
2313
|
+
const verbose = options?.verbose === true;
|
|
2314
|
+
const color = options?.color === true;
|
|
2315
|
+
if (json) {
|
|
2316
|
+
return JSON.stringify(result, null, 2);
|
|
2317
|
+
}
|
|
2318
|
+
const sev = (s, level) => {
|
|
2319
|
+
if (!color) return s;
|
|
2320
|
+
if (level === "error") return chalk2.red(s);
|
|
2321
|
+
if (level === "warning") return chalk2.yellow(s);
|
|
2322
|
+
return chalk2.gray(s);
|
|
2323
|
+
};
|
|
2324
|
+
const lines = [];
|
|
2325
|
+
const { summary } = result;
|
|
2326
|
+
lines.push("Run diff");
|
|
2327
|
+
lines.push(`Left: ${summary.leftRunId}`);
|
|
2328
|
+
lines.push(`Right: ${summary.rightRunId}`);
|
|
2329
|
+
lines.push("");
|
|
2330
|
+
lines.push("Summary:");
|
|
2331
|
+
lines.push(` Differences: ${summary.totalDifferences}`);
|
|
2332
|
+
lines.push(` Errors: ${summary.errors}`);
|
|
2333
|
+
lines.push(` Warnings: ${summary.warnings}`);
|
|
2334
|
+
lines.push(` Info: ${summary.info}`);
|
|
2335
|
+
lines.push("");
|
|
2336
|
+
const fd = summary.firstDivergence;
|
|
2337
|
+
const firstKind = result.differences[0]?.kind;
|
|
2338
|
+
if (fd !== void 0) {
|
|
2339
|
+
lines.push("First divergence:");
|
|
2340
|
+
const where = formatPath(fd.path);
|
|
2341
|
+
const displayKind = firstKind ?? fd.kind;
|
|
2342
|
+
lines.push(` ${displayKind} at ${where}`);
|
|
2343
|
+
if (fd.left !== void 0 || fd.right !== void 0) {
|
|
2344
|
+
lines.push(` left: ${formatValue(fd.left, verbose)}`);
|
|
2345
|
+
lines.push(` right: ${formatValue(fd.right, verbose)}`);
|
|
2346
|
+
}
|
|
2347
|
+
lines.push("");
|
|
2348
|
+
}
|
|
2349
|
+
lines.push("Differences:");
|
|
2350
|
+
if (result.differences.length === 0) {
|
|
2351
|
+
lines.push(" (none)");
|
|
2352
|
+
return lines.join("\n");
|
|
2353
|
+
}
|
|
2354
|
+
const showSides = (kind) => verbose || [
|
|
2355
|
+
"run-status",
|
|
2356
|
+
"step-status",
|
|
2357
|
+
"error",
|
|
2358
|
+
"duration",
|
|
2359
|
+
"step-type",
|
|
2360
|
+
"structure",
|
|
2361
|
+
"step-added",
|
|
2362
|
+
"step-removed"
|
|
2363
|
+
].includes(kind);
|
|
2364
|
+
for (const d of result.differences) {
|
|
2365
|
+
const tag = sev(`[${d.severity}]`, d.severity);
|
|
2366
|
+
const pathStr = d.path !== void 0 ? ` ${formatPath(d.path)}` : "";
|
|
2367
|
+
lines.push(` ${tag} ${d.kind}${pathStr}`);
|
|
2368
|
+
lines.push(` ${d.message}`);
|
|
2369
|
+
if (d.left !== void 0 || d.right !== void 0) {
|
|
2370
|
+
if (showSides(d.kind)) {
|
|
2371
|
+
lines.push(` left: ${formatValue(d.left, verbose)}`);
|
|
2372
|
+
lines.push(` right: ${formatValue(d.right, verbose)}`);
|
|
2373
|
+
}
|
|
2374
|
+
}
|
|
2375
|
+
}
|
|
2376
|
+
return lines.join("\n");
|
|
2377
|
+
}
|
|
2378
|
+
|
|
2379
|
+
// packages/core/src/diff/index.ts
|
|
2380
|
+
function diffTraceEvents(leftEvents, rightEvents, options) {
|
|
2381
|
+
const left = manualTraceEventsToComparableRun(leftEvents);
|
|
2382
|
+
const right = manualTraceEventsToComparableRun(rightEvents);
|
|
2383
|
+
return diffRuns(left, right, options);
|
|
2384
|
+
}
|
|
497
2385
|
var TERMINAL_INDENT = " ";
|
|
498
2386
|
var MAX_TERMINAL_NAME_LENGTH = 80;
|
|
499
2387
|
var MAX_TERMINAL_DEPTH = 10;
|
|
@@ -519,24 +2407,24 @@ function formatTerminalName(name) {
|
|
|
519
2407
|
return truncateName(name, MAX_TERMINAL_NAME_LENGTH);
|
|
520
2408
|
}
|
|
521
2409
|
function getStatusIcon(status) {
|
|
522
|
-
if (status === "success") return
|
|
523
|
-
if (status === "error") return
|
|
524
|
-
return
|
|
2410
|
+
if (status === "success") return chalk2.green("\u2714");
|
|
2411
|
+
if (status === "error") return chalk2.red("\u2716");
|
|
2412
|
+
return chalk2.yellow("\u23F3");
|
|
525
2413
|
}
|
|
526
2414
|
function renderStepLine(name, durationMs, status, depth) {
|
|
527
2415
|
try {
|
|
528
2416
|
const nm = formatTerminalName(name);
|
|
529
2417
|
const ind = getIndent(depth ?? 0);
|
|
530
2418
|
if (status === "running" && durationMs === void 0) {
|
|
531
|
-
return `${ind}${
|
|
2419
|
+
return `${ind}${chalk2.yellow("\u23F3")} ${nm}`;
|
|
532
2420
|
}
|
|
533
2421
|
const hasDur = durationMs !== void 0 && Number.isFinite(durationMs);
|
|
534
|
-
const dur = hasDur ?
|
|
2422
|
+
const dur = hasDur ? formatDuration2(durationMs) : void 0;
|
|
535
2423
|
if (status === "running") {
|
|
536
|
-
return dur !== void 0 ? `${ind}${
|
|
2424
|
+
return dur !== void 0 ? `${ind}${chalk2.yellow("\u23F3")} ${nm} (${dur})` : `${ind}${chalk2.yellow("\u23F3")} ${nm}`;
|
|
537
2425
|
}
|
|
538
2426
|
if (!hasDur || dur === void 0) {
|
|
539
|
-
return `${ind}${
|
|
2427
|
+
return `${ind}${chalk2.yellow("\u23F3")} ${nm}`;
|
|
540
2428
|
}
|
|
541
2429
|
if (status === "success") {
|
|
542
2430
|
return `${ind}${getStatusIcon("success")} ${nm} (${dur})`;
|
|
@@ -557,7 +2445,7 @@ function renderErrorLine(error, depth) {
|
|
|
557
2445
|
}
|
|
558
2446
|
function renderRunSummary(durationMs, status, traceFilePath) {
|
|
559
2447
|
try {
|
|
560
|
-
const dur = Number.isFinite(durationMs) ?
|
|
2448
|
+
const dur = Number.isFinite(durationMs) ? formatDuration2(durationMs) : formatDuration2(0);
|
|
561
2449
|
const head = status === "error" ? `Failed in ${dur}` : `Completed in ${dur}`;
|
|
562
2450
|
const lines = [head];
|
|
563
2451
|
if (traceFilePath !== void 0 && traceFilePath.trim() !== "") {
|
|
@@ -572,7 +2460,7 @@ function printRunStart(runId, name) {
|
|
|
572
2460
|
if (isSilentContext()) return;
|
|
573
2461
|
try {
|
|
574
2462
|
safePrint("");
|
|
575
|
-
const header = `${
|
|
2463
|
+
const header = `${chalk2.cyan.bold("\u{1F50D} AgentInspect:")} ${formatTerminalName(name)} ${chalk2.dim(`(${runId})`)}`;
|
|
576
2464
|
safePrint(header);
|
|
577
2465
|
} catch {
|
|
578
2466
|
}
|
|
@@ -605,10 +2493,10 @@ function printRunComplete(_name, _runId, durationMs, status, traceFilePath) {
|
|
|
605
2493
|
for (let i = 0; i < lines.length; i++) {
|
|
606
2494
|
const line = lines[i];
|
|
607
2495
|
if (i === 0) {
|
|
608
|
-
const color = status === "error" ?
|
|
2496
|
+
const color = status === "error" ? chalk2.red : status === "running" ? chalk2.yellow : chalk2.green;
|
|
609
2497
|
safePrint(color(line));
|
|
610
2498
|
} else {
|
|
611
|
-
safePrint(
|
|
2499
|
+
safePrint(chalk2.dim(line));
|
|
612
2500
|
}
|
|
613
2501
|
}
|
|
614
2502
|
} catch {
|
|
@@ -642,7 +2530,7 @@ async function inspectRun(name, fn, options) {
|
|
|
642
2530
|
}
|
|
643
2531
|
const runName = normalizeRunName(name);
|
|
644
2532
|
const runId = createRunId();
|
|
645
|
-
const traceDir =
|
|
2533
|
+
const traceDir = resolveTraceDir({ dir: options?.traceDir });
|
|
646
2534
|
const context = {
|
|
647
2535
|
runId,
|
|
648
2536
|
runName,
|
|
@@ -914,6 +2802,775 @@ function observe(agent, options) {
|
|
|
914
2802
|
}
|
|
915
2803
|
}
|
|
916
2804
|
|
|
917
|
-
|
|
2805
|
+
// packages/core/src/exporters/types.ts
|
|
2806
|
+
var EXPORT_PAYLOAD_VERSION = "0.1.2";
|
|
2807
|
+
|
|
2808
|
+
// packages/core/src/exporters/html-exporter.ts
|
|
2809
|
+
function renderTreeHtml(nodes, ulClass = "tree") {
|
|
2810
|
+
if (nodes.length === 0) return "";
|
|
2811
|
+
const parts = [`<ul class="${ulClass}">`];
|
|
2812
|
+
for (const n of nodes) {
|
|
2813
|
+
const ev = n.event;
|
|
2814
|
+
const status = ev.status ?? "?";
|
|
2815
|
+
const dur = ev.durationMs !== void 0 && Number.isFinite(ev.durationMs) ? `${ev.durationMs}ms` : "-";
|
|
2816
|
+
parts.push("<li>");
|
|
2817
|
+
parts.push(
|
|
2818
|
+
`<span class="nm">${escapeHtml(ev.name)}</span> <span class="meta">[${escapeHtml(ev.kind)}] ${escapeHtml(status)} (${escapeHtml(dur)})</span>`
|
|
2819
|
+
);
|
|
2820
|
+
if (n.children.length > 0) {
|
|
2821
|
+
parts.push(renderTreeHtml(n.children, "tree nested"));
|
|
2822
|
+
}
|
|
2823
|
+
parts.push("</li>");
|
|
2824
|
+
}
|
|
2825
|
+
parts.push("</ul>");
|
|
2826
|
+
return parts.join("");
|
|
2827
|
+
}
|
|
2828
|
+
function exportHtml(tree, options) {
|
|
2829
|
+
const warnings = [];
|
|
2830
|
+
const includeMetadata = options?.includeMetadata ?? true;
|
|
2831
|
+
const includeAttributes = options?.includeAttributes ?? false;
|
|
2832
|
+
const includeErrors = options?.includeErrors ?? true;
|
|
2833
|
+
const maxLen = options?.maxAttributeLength ?? 500;
|
|
2834
|
+
const redacted = options?.redacted ?? true;
|
|
2835
|
+
const titleName = escapeHtml(tree.name ?? tree.runId);
|
|
2836
|
+
const summaryRows = [];
|
|
2837
|
+
summaryRows.push(
|
|
2838
|
+
`<tr><th scope="row">runId</th><td><code>${escapeHtml(tree.runId)}</code></td></tr>`
|
|
2839
|
+
);
|
|
2840
|
+
if (tree.name !== void 0) {
|
|
2841
|
+
summaryRows.push(`<tr><th scope="row">name</th><td>${escapeHtml(tree.name)}</td></tr>`);
|
|
2842
|
+
}
|
|
2843
|
+
summaryRows.push(
|
|
2844
|
+
`<tr><th scope="row">status</th><td>${escapeHtml(String(tree.status ?? "unknown"))}</td></tr>`
|
|
2845
|
+
);
|
|
2846
|
+
summaryRows.push(
|
|
2847
|
+
`<tr><th scope="row">durationMs</th><td>${tree.durationMs !== void 0 ? escapeHtml(String(tree.durationMs)) : "\u2014"}</td></tr>`
|
|
2848
|
+
);
|
|
2849
|
+
summaryRows.push(
|
|
2850
|
+
`<tr><th scope="row">startedAt</th><td>${tree.startedAt !== void 0 ? escapeHtml(String(tree.startedAt)) : "\u2014"}</td></tr>`
|
|
2851
|
+
);
|
|
2852
|
+
summaryRows.push(
|
|
2853
|
+
`<tr><th scope="row">endedAt</th><td>${tree.endedAt !== void 0 ? escapeHtml(String(tree.endedAt)) : "\u2014"}</td></tr>`
|
|
2854
|
+
);
|
|
2855
|
+
summaryRows.push(
|
|
2856
|
+
`<tr><th scope="row">totalEvents</th><td>${escapeHtml(String(tree.metadata.totalEvents))}</td></tr>`
|
|
2857
|
+
);
|
|
2858
|
+
let confidenceHtml = "";
|
|
2859
|
+
if (includeMetadata) {
|
|
2860
|
+
const cb = tree.metadata.confidenceBreakdown;
|
|
2861
|
+
confidenceHtml += "<h3>Confidence breakdown</h3><table><thead><tr><th>bucket</th><th>count</th></tr></thead><tbody>";
|
|
2862
|
+
for (const k of Object.keys(cb).sort()) {
|
|
2863
|
+
const key = k;
|
|
2864
|
+
confidenceHtml += `<tr><td>${escapeHtml(key)}</td><td>${cb[key]}</td></tr>`;
|
|
2865
|
+
}
|
|
2866
|
+
confidenceHtml += "</tbody></table>";
|
|
2867
|
+
confidenceHtml += "<h3>Kind breakdown</h3><table><thead><tr><th>kind</th><th>count</th></tr></thead><tbody>";
|
|
2868
|
+
for (const k of Object.keys(tree.metadata.kinds).sort()) {
|
|
2869
|
+
const key = k;
|
|
2870
|
+
const c = tree.metadata.kinds[key];
|
|
2871
|
+
if (c > 0) confidenceHtml += `<tr><td>${escapeHtml(key)}</td><td>${c}</td></tr>`;
|
|
2872
|
+
}
|
|
2873
|
+
confidenceHtml += "</tbody></table>";
|
|
2874
|
+
}
|
|
2875
|
+
const flat = flattenTree(tree);
|
|
2876
|
+
const errors = flat.filter((n) => n.event.status === "error");
|
|
2877
|
+
let errorsHtml = "";
|
|
2878
|
+
if (includeErrors && errors.length > 0) {
|
|
2879
|
+
errorsHtml += "<h2>Errors</h2><ul>";
|
|
2880
|
+
for (const n of errors) {
|
|
2881
|
+
const msg = n.event.attributes && typeof n.event.attributes.error === "object" ? safeString2(
|
|
2882
|
+
n.event.attributes.error.message,
|
|
2883
|
+
maxLen
|
|
2884
|
+
) : "";
|
|
2885
|
+
errorsHtml += `<li><strong>${escapeHtml(n.event.name)}</strong> (${escapeHtml(n.event.eventId)}): ${escapeHtml(msg || "error")}</li>`;
|
|
2886
|
+
}
|
|
2887
|
+
errorsHtml += "</ul>";
|
|
2888
|
+
}
|
|
2889
|
+
let attrsHtml = "";
|
|
2890
|
+
if (includeAttributes) {
|
|
2891
|
+
attrsHtml += "<h2>Attributes (bounded)</h2>";
|
|
2892
|
+
for (const n of flat) {
|
|
2893
|
+
if (!n.event.attributes || Object.keys(n.event.attributes).length === 0) continue;
|
|
2894
|
+
const compact = compactAttributes(n.event.attributes, {
|
|
2895
|
+
maxLength: maxLen,
|
|
2896
|
+
redacted
|
|
2897
|
+
});
|
|
2898
|
+
attrsHtml += `<h3>${escapeHtml(n.event.name)}</h3><pre class="json">${escapeHtml(stableJson(compact, true))}</pre>`;
|
|
2899
|
+
}
|
|
2900
|
+
warnings.push(
|
|
2901
|
+
"Attributes may still contain sensitive data; review exports before sharing."
|
|
2902
|
+
);
|
|
2903
|
+
}
|
|
2904
|
+
const css = `
|
|
2905
|
+
body{font-family:system-ui,sans-serif;line-height:1.5;margin:1.5rem;max-width:960px;color:#111}
|
|
2906
|
+
h1{font-size:1.35rem}
|
|
2907
|
+
h2{font-size:1.1rem;margin-top:1.5rem}
|
|
2908
|
+
table{border-collapse:collapse;margin:0.75rem 0}
|
|
2909
|
+
th,td{border:1px solid #ccc;padding:0.35rem 0.6rem;text-align:left}
|
|
2910
|
+
th{background:#f5f5f5}
|
|
2911
|
+
pre.json{background:#f8f8f8;padding:0.75rem;overflow:auto;font-size:0.85rem}
|
|
2912
|
+
ul.tree{list-style:none;padding-left:1rem}
|
|
2913
|
+
ul.tree.nested{padding-left:1.25rem;border-left:1px solid #ddd;margin:0.25rem 0}
|
|
2914
|
+
.nm{font-weight:600}
|
|
2915
|
+
.meta{color:#555;font-size:0.9rem}
|
|
2916
|
+
footer{margin-top:2rem;font-size:0.85rem;color:#555}
|
|
2917
|
+
`.trim();
|
|
2918
|
+
const html = `<!doctype html>
|
|
2919
|
+
<html lang="en">
|
|
2920
|
+
<head>
|
|
2921
|
+
<meta charset="utf-8"/>
|
|
2922
|
+
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
|
2923
|
+
<title>${titleName}</title>
|
|
2924
|
+
<style>${css}</style>
|
|
2925
|
+
</head>
|
|
2926
|
+
<body>
|
|
2927
|
+
<header><h1>AgentInspect Run: ${titleName}</h1></header>
|
|
2928
|
+
<p class="note">Generated locally by AgentInspect.</p>
|
|
2929
|
+
${includeMetadata ? `<section class="summary"><h2>Summary</h2><table>${summaryRows.join("")}</table>${confidenceHtml}</section>` : ""}
|
|
2930
|
+
<section class="tree"><h2>Execution tree</h2>${tree.children.length > 0 ? renderTreeHtml(tree.children) : "<p>No steps recorded.</p>"}</section>
|
|
2931
|
+
${errorsHtml}
|
|
2932
|
+
${attrsHtml}
|
|
2933
|
+
<footer>Generated locally by AgentInspect. Review for sensitive data before sharing.</footer>
|
|
2934
|
+
</body>
|
|
2935
|
+
</html>`;
|
|
2936
|
+
return {
|
|
2937
|
+
format: "html",
|
|
2938
|
+
content: html,
|
|
2939
|
+
contentType: "text/html",
|
|
2940
|
+
fileExtension: ".html",
|
|
2941
|
+
warnings
|
|
2942
|
+
};
|
|
2943
|
+
}
|
|
2944
|
+
|
|
2945
|
+
// packages/core/src/exporters/markdown-exporter.ts
|
|
2946
|
+
function renderTreeAscii(nodes, indent = "") {
|
|
2947
|
+
const lines = [];
|
|
2948
|
+
for (let i = 0; i < nodes.length; i++) {
|
|
2949
|
+
const n = nodes[i];
|
|
2950
|
+
const last = i === nodes.length - 1;
|
|
2951
|
+
const branch = last ? "\u2514\u2500 " : "\u251C\u2500 ";
|
|
2952
|
+
const ev = n.event;
|
|
2953
|
+
const status = ev.status ?? "?";
|
|
2954
|
+
const dur = ev.durationMs !== void 0 && Number.isFinite(ev.durationMs) ? `${ev.durationMs}ms` : "-";
|
|
2955
|
+
lines.push(`${indent}${branch}${escapeMarkdown(ev.name)} [${ev.kind}] ${status} (${dur})`);
|
|
2956
|
+
const nextIndent = indent + (last ? " " : "\u2502 ");
|
|
2957
|
+
if (n.children.length > 0) {
|
|
2958
|
+
const childStr = renderTreeAscii(n.children, nextIndent);
|
|
2959
|
+
if (childStr.length > 0) lines.push(childStr);
|
|
2960
|
+
}
|
|
2961
|
+
}
|
|
2962
|
+
return lines.join("\n");
|
|
2963
|
+
}
|
|
2964
|
+
function exportMarkdown(tree, options) {
|
|
2965
|
+
const warnings = [];
|
|
2966
|
+
const includeMetadata = options?.includeMetadata ?? true;
|
|
2967
|
+
const includeAttributes = options?.includeAttributes ?? false;
|
|
2968
|
+
const includeErrors = options?.includeErrors ?? true;
|
|
2969
|
+
const maxLen = options?.maxAttributeLength ?? 500;
|
|
2970
|
+
const redacted = options?.redacted ?? true;
|
|
2971
|
+
const titleName = tree.name ?? tree.runId;
|
|
2972
|
+
const lines = [];
|
|
2973
|
+
lines.push(`# AgentInspect Run: ${escapeMarkdown(titleName)}`);
|
|
2974
|
+
lines.push("");
|
|
2975
|
+
lines.push("Generated locally by AgentInspect. Review for sensitive data before sharing.");
|
|
2976
|
+
lines.push("");
|
|
2977
|
+
if (includeMetadata) {
|
|
2978
|
+
lines.push("## Summary");
|
|
2979
|
+
lines.push("");
|
|
2980
|
+
lines.push(`- **runId**: ${escapeMarkdown(tree.runId)}`);
|
|
2981
|
+
if (tree.name !== void 0) lines.push(`- **name**: ${escapeMarkdown(tree.name)}`);
|
|
2982
|
+
lines.push(`- **status**: ${escapeMarkdown(String(tree.status ?? "unknown"))}`);
|
|
2983
|
+
lines.push(
|
|
2984
|
+
`- **durationMs**: ${tree.durationMs !== void 0 ? escapeMarkdown(String(tree.durationMs)) : "-"}`
|
|
2985
|
+
);
|
|
2986
|
+
lines.push(
|
|
2987
|
+
`- **startedAt**: ${tree.startedAt !== void 0 ? escapeMarkdown(String(tree.startedAt)) : "-"}`
|
|
2988
|
+
);
|
|
2989
|
+
lines.push(
|
|
2990
|
+
`- **endedAt**: ${tree.endedAt !== void 0 ? escapeMarkdown(String(tree.endedAt)) : "-"}`
|
|
2991
|
+
);
|
|
2992
|
+
lines.push(`- **totalEvents**: ${tree.metadata.totalEvents}`);
|
|
2993
|
+
lines.push("");
|
|
2994
|
+
lines.push("### Confidence breakdown");
|
|
2995
|
+
lines.push("");
|
|
2996
|
+
lines.push("| bucket | count |");
|
|
2997
|
+
lines.push("| --- | --- |");
|
|
2998
|
+
for (const k of Object.keys(tree.metadata.confidenceBreakdown).sort()) {
|
|
2999
|
+
const key = k;
|
|
3000
|
+
lines.push(
|
|
3001
|
+
`| ${escapeMarkdown(key)} | ${tree.metadata.confidenceBreakdown[key]} |`
|
|
3002
|
+
);
|
|
3003
|
+
}
|
|
3004
|
+
lines.push("");
|
|
3005
|
+
lines.push("### Kind breakdown");
|
|
3006
|
+
lines.push("");
|
|
3007
|
+
lines.push("| kind | count |");
|
|
3008
|
+
lines.push("| --- | --- |");
|
|
3009
|
+
for (const k of Object.keys(tree.metadata.kinds).sort()) {
|
|
3010
|
+
const key = k;
|
|
3011
|
+
const c = tree.metadata.kinds[key];
|
|
3012
|
+
if (c > 0) lines.push(`| ${escapeMarkdown(key)} | ${c} |`);
|
|
3013
|
+
}
|
|
3014
|
+
lines.push("");
|
|
3015
|
+
}
|
|
3016
|
+
lines.push("## Execution tree");
|
|
3017
|
+
lines.push("");
|
|
3018
|
+
lines.push("```text");
|
|
3019
|
+
lines.push(
|
|
3020
|
+
tree.children.length > 0 ? renderTreeAscii(tree.children) : "(no steps)"
|
|
3021
|
+
);
|
|
3022
|
+
lines.push("```");
|
|
3023
|
+
lines.push("");
|
|
3024
|
+
const flat = flattenTree(tree);
|
|
3025
|
+
const errors = flat.filter((n) => n.event.status === "error");
|
|
3026
|
+
if (includeErrors && errors.length > 0) {
|
|
3027
|
+
lines.push("## Errors");
|
|
3028
|
+
lines.push("");
|
|
3029
|
+
for (const n of errors) {
|
|
3030
|
+
const msg = n.event.attributes && typeof n.event.attributes.error === "object" ? safeString2(
|
|
3031
|
+
n.event.attributes.error.message,
|
|
3032
|
+
maxLen
|
|
3033
|
+
) : "";
|
|
3034
|
+
lines.push(
|
|
3035
|
+
`- **${escapeMarkdown(n.event.name)}** (${escapeMarkdown(n.event.eventId)}): ${escapeMarkdown(msg || "error")}`
|
|
3036
|
+
);
|
|
3037
|
+
}
|
|
3038
|
+
lines.push("");
|
|
3039
|
+
}
|
|
3040
|
+
if (includeAttributes) {
|
|
3041
|
+
lines.push("## Attributes (bounded)");
|
|
3042
|
+
lines.push("");
|
|
3043
|
+
for (const n of flat) {
|
|
3044
|
+
if (!n.event.attributes || Object.keys(n.event.attributes).length === 0) continue;
|
|
3045
|
+
const compact = compactAttributes(n.event.attributes, {
|
|
3046
|
+
maxLength: maxLen,
|
|
3047
|
+
redacted
|
|
3048
|
+
});
|
|
3049
|
+
lines.push(`### ${escapeMarkdown(n.event.name)}`);
|
|
3050
|
+
lines.push("");
|
|
3051
|
+
lines.push("```json");
|
|
3052
|
+
lines.push(stableJson(compact, true));
|
|
3053
|
+
lines.push("```");
|
|
3054
|
+
lines.push("");
|
|
3055
|
+
}
|
|
3056
|
+
warnings.push(
|
|
3057
|
+
"Attributes may still contain sensitive data; review exports before sharing."
|
|
3058
|
+
);
|
|
3059
|
+
}
|
|
3060
|
+
return {
|
|
3061
|
+
format: "markdown",
|
|
3062
|
+
content: lines.join("\n"),
|
|
3063
|
+
contentType: "text/markdown",
|
|
3064
|
+
fileExtension: ".md",
|
|
3065
|
+
warnings
|
|
3066
|
+
};
|
|
3067
|
+
}
|
|
3068
|
+
function hexFrom(seed, byteLen) {
|
|
3069
|
+
return crypto.createHash("sha256").update(seed, "utf8").digest("hex").slice(0, byteLen * 2);
|
|
3070
|
+
}
|
|
3071
|
+
function mapInspectKindToOI(kind, warnings) {
|
|
3072
|
+
switch (kind) {
|
|
3073
|
+
case "LLM":
|
|
3074
|
+
return { openInferenceKind: "LLM" };
|
|
3075
|
+
case "TOOL":
|
|
3076
|
+
return { openInferenceKind: "TOOL" };
|
|
3077
|
+
case "CHAIN":
|
|
3078
|
+
return { openInferenceKind: "CHAIN" };
|
|
3079
|
+
case "RETRIEVER":
|
|
3080
|
+
return { openInferenceKind: "RETRIEVER" };
|
|
3081
|
+
case "AGENT":
|
|
3082
|
+
return { openInferenceKind: "AGENT" };
|
|
3083
|
+
case "DECISION":
|
|
3084
|
+
warnings.push(
|
|
3085
|
+
`Ambiguous kind DECISION mapped to CHAIN for span compatibility (${EXPORT_PAYLOAD_VERSION}).`
|
|
3086
|
+
);
|
|
3087
|
+
return { openInferenceKind: "CHAIN" };
|
|
3088
|
+
case "RESULT":
|
|
3089
|
+
warnings.push(
|
|
3090
|
+
`Ambiguous kind RESULT mapped to UNKNOWN for span compatibility (${EXPORT_PAYLOAD_VERSION}).`
|
|
3091
|
+
);
|
|
3092
|
+
return { openInferenceKind: "UNKNOWN" };
|
|
3093
|
+
case "ERROR":
|
|
3094
|
+
warnings.push(`ERROR kind mapped to CHAIN for span compatibility.`);
|
|
3095
|
+
return { openInferenceKind: "CHAIN" };
|
|
3096
|
+
case "LOG":
|
|
3097
|
+
case "LOGIC":
|
|
3098
|
+
case "RUN":
|
|
3099
|
+
warnings.push(`${kind} mapped to CHAIN for span compatibility.`);
|
|
3100
|
+
return { openInferenceKind: "CHAIN" };
|
|
3101
|
+
default:
|
|
3102
|
+
warnings.push(`Unhandled InspectKind ${kind} mapped to UNKNOWN.`);
|
|
3103
|
+
return { openInferenceKind: "UNKNOWN" };
|
|
3104
|
+
}
|
|
3105
|
+
}
|
|
3106
|
+
function exportOpenInference(tree, options) {
|
|
3107
|
+
const warnings = [
|
|
3108
|
+
"OpenInference-compatible JSON export is experimental until verified against specific backends.",
|
|
3109
|
+
"This file was generated locally and not sent anywhere."
|
|
3110
|
+
];
|
|
3111
|
+
const traceId = hexFrom(`trace:${tree.runId}`, 16);
|
|
3112
|
+
const includeAttributes = options?.includeAttributes ?? false;
|
|
3113
|
+
const maxLen = options?.maxAttributeLength ?? 500;
|
|
3114
|
+
const pretty = options?.pretty ?? true;
|
|
3115
|
+
const spans = [];
|
|
3116
|
+
for (const n of flattenTree(tree)) {
|
|
3117
|
+
const ev = n.event;
|
|
3118
|
+
const spanId = hexFrom(`${tree.runId}:${ev.eventId}`, 8);
|
|
3119
|
+
const parentSpanHex = ev.parentId ? hexFrom(`${tree.runId}:${ev.parentId}`, 8) : void 0;
|
|
3120
|
+
const startNs = Math.round(ev.timestamp * 1e6);
|
|
3121
|
+
let endNs;
|
|
3122
|
+
if (ev.durationMs !== void 0 && Number.isFinite(ev.durationMs)) {
|
|
3123
|
+
endNs = startNs + Math.round(ev.durationMs * 1e6);
|
|
3124
|
+
}
|
|
3125
|
+
const { openInferenceKind } = mapInspectKindToOI(ev.kind, warnings);
|
|
3126
|
+
const attrs = {
|
|
3127
|
+
"openinference.span.kind": openInferenceKind,
|
|
3128
|
+
"agent_inspect.kind": ev.kind,
|
|
3129
|
+
"agent_inspect.confidence": ev.confidence,
|
|
3130
|
+
"agent_inspect.source.type": ev.source.type,
|
|
3131
|
+
"agent_inspect.run_id": tree.runId,
|
|
3132
|
+
"agent_inspect.event_id": ev.eventId,
|
|
3133
|
+
"agent_inspect.status": ev.status ?? "unset"
|
|
3134
|
+
};
|
|
3135
|
+
if (ev.durationMs !== void 0) {
|
|
3136
|
+
attrs["agent_inspect.duration_ms"] = ev.durationMs;
|
|
3137
|
+
}
|
|
3138
|
+
const meta = ev.attributes;
|
|
3139
|
+
if (meta?.model !== void 0 && typeof meta.model === "string") {
|
|
3140
|
+
attrs["llm.model_name"] = meta.model;
|
|
3141
|
+
}
|
|
3142
|
+
const tokens = meta?.tokens;
|
|
3143
|
+
if (tokens && typeof tokens === "object" && tokens !== null) {
|
|
3144
|
+
const inp = tokens.input;
|
|
3145
|
+
const outp = tokens.output;
|
|
3146
|
+
if (typeof inp === "number") attrs["llm.token_count.prompt"] = inp;
|
|
3147
|
+
if (typeof outp === "number") attrs["llm.token_count.completion"] = outp;
|
|
3148
|
+
}
|
|
3149
|
+
if (includeAttributes && meta && typeof meta === "object") {
|
|
3150
|
+
for (const [k, v] of Object.entries(meta)) {
|
|
3151
|
+
if (k === "tokens" || k === "model") continue;
|
|
3152
|
+
if (v !== void 0 && v !== null && typeof v !== "object") {
|
|
3153
|
+
attrs[`agent_inspect.preview.${k}`] = typeof v === "string" ? v.slice(0, maxLen) : v;
|
|
3154
|
+
}
|
|
3155
|
+
}
|
|
3156
|
+
}
|
|
3157
|
+
let status;
|
|
3158
|
+
if (ev.status === "error") {
|
|
3159
|
+
const msg = meta && typeof meta.error === "object" && meta.error !== null ? String(meta.error.message ?? "error") : "error";
|
|
3160
|
+
status = { code: "ERROR", message: msg.slice(0, maxLen) };
|
|
3161
|
+
} else if (ev.status === "ok") {
|
|
3162
|
+
status = { code: "OK" };
|
|
3163
|
+
} else {
|
|
3164
|
+
status = { code: "UNSET" };
|
|
3165
|
+
}
|
|
3166
|
+
spans.push({
|
|
3167
|
+
trace_id: traceId,
|
|
3168
|
+
span_id: spanId,
|
|
3169
|
+
parent_span_id: parentSpanHex,
|
|
3170
|
+
name: ev.name,
|
|
3171
|
+
start_time_unix_nano: startNs,
|
|
3172
|
+
end_time_unix_nano: endNs,
|
|
3173
|
+
attributes: attrs,
|
|
3174
|
+
status
|
|
3175
|
+
});
|
|
3176
|
+
}
|
|
3177
|
+
const payload = {
|
|
3178
|
+
exporter: "agent-inspect",
|
|
3179
|
+
format: "openinference",
|
|
3180
|
+
compatibility: "openinference-compatible",
|
|
3181
|
+
version: EXPORT_PAYLOAD_VERSION,
|
|
3182
|
+
trace_id: traceId,
|
|
3183
|
+
spans,
|
|
3184
|
+
warnings
|
|
3185
|
+
};
|
|
3186
|
+
return {
|
|
3187
|
+
format: "openinference",
|
|
3188
|
+
content: JSON.stringify(payload, null, pretty ? 2 : void 0),
|
|
3189
|
+
contentType: "application/json",
|
|
3190
|
+
fileExtension: ".openinference.json",
|
|
3191
|
+
warnings
|
|
3192
|
+
};
|
|
3193
|
+
}
|
|
3194
|
+
function hexFrom2(seed, byteLen) {
|
|
3195
|
+
return crypto.createHash("sha256").update(seed, "utf8").digest("hex").slice(0, byteLen * 2);
|
|
3196
|
+
}
|
|
3197
|
+
function stringAttr(key, value) {
|
|
3198
|
+
return { key, value: { stringValue: value } };
|
|
3199
|
+
}
|
|
3200
|
+
function intAttr(key, value) {
|
|
3201
|
+
return { key, value: { intValue: String(value) } };
|
|
3202
|
+
}
|
|
3203
|
+
function genAiOperationName(kind) {
|
|
3204
|
+
switch (kind) {
|
|
3205
|
+
case "LLM":
|
|
3206
|
+
return "generate_content";
|
|
3207
|
+
case "TOOL":
|
|
3208
|
+
return "execute_tool";
|
|
3209
|
+
case "AGENT":
|
|
3210
|
+
return "invoke_agent";
|
|
3211
|
+
default:
|
|
3212
|
+
return void 0;
|
|
3213
|
+
}
|
|
3214
|
+
}
|
|
3215
|
+
function exportOtlpJson(tree, options) {
|
|
3216
|
+
const warnings = [
|
|
3217
|
+
"OTLP JSON export uses OTel GenAI-aligned attributes where applicable; experimental until verified against specific collectors.",
|
|
3218
|
+
"Not OTLP gRPC/protobuf \u2014 JSON mapping only. Generated locally; no network upload."
|
|
3219
|
+
];
|
|
3220
|
+
const traceId = hexFrom2(`trace:${tree.runId}`, 16);
|
|
3221
|
+
const includeAttributes = options?.includeAttributes ?? false;
|
|
3222
|
+
const maxLen = options?.maxAttributeLength ?? 500;
|
|
3223
|
+
const pretty = options?.pretty ?? true;
|
|
3224
|
+
const flat = flattenTree(tree);
|
|
3225
|
+
const spans = [];
|
|
3226
|
+
for (const n of flat) {
|
|
3227
|
+
const ev = n.event;
|
|
3228
|
+
const spanId = hexFrom2(`${tree.runId}:${ev.eventId}`, 8);
|
|
3229
|
+
const parentSpanId = ev.parentId ? hexFrom2(`${tree.runId}:${ev.parentId}`, 8) : void 0;
|
|
3230
|
+
const startNs = String(Math.round(ev.timestamp * 1e6));
|
|
3231
|
+
let endNs;
|
|
3232
|
+
if (ev.durationMs !== void 0 && Number.isFinite(ev.durationMs)) {
|
|
3233
|
+
endNs = String(Math.round(ev.timestamp * 1e6 + ev.durationMs * 1e6));
|
|
3234
|
+
}
|
|
3235
|
+
const attrs = [
|
|
3236
|
+
stringAttr("agent_inspect.kind", ev.kind),
|
|
3237
|
+
stringAttr("agent_inspect.confidence", ev.confidence),
|
|
3238
|
+
stringAttr("agent_inspect.source.type", ev.source.type),
|
|
3239
|
+
stringAttr("agent_inspect.run_id", tree.runId),
|
|
3240
|
+
stringAttr("agent_inspect.event_id", ev.eventId),
|
|
3241
|
+
stringAttr("agent_inspect.status", ev.status ?? "unset")
|
|
3242
|
+
];
|
|
3243
|
+
if (ev.durationMs !== void 0) {
|
|
3244
|
+
attrs.push(intAttr("agent_inspect.duration_ms", ev.durationMs));
|
|
3245
|
+
}
|
|
3246
|
+
const op = genAiOperationName(ev.kind);
|
|
3247
|
+
if (op !== void 0) {
|
|
3248
|
+
attrs.push(stringAttr("gen_ai.operation.name", op));
|
|
3249
|
+
}
|
|
3250
|
+
const meta = ev.attributes;
|
|
3251
|
+
if (meta?.model !== void 0 && typeof meta.model === "string") {
|
|
3252
|
+
attrs.push(stringAttr("gen_ai.request.model", meta.model.slice(0, maxLen)));
|
|
3253
|
+
}
|
|
3254
|
+
const tokens = meta?.tokens;
|
|
3255
|
+
if (tokens && typeof tokens === "object" && tokens !== null) {
|
|
3256
|
+
const inp = tokens.input;
|
|
3257
|
+
const outp = tokens.output;
|
|
3258
|
+
if (typeof inp === "number") attrs.push(intAttr("gen_ai.usage.input_tokens", inp));
|
|
3259
|
+
if (typeof outp === "number") attrs.push(intAttr("gen_ai.usage.output_tokens", outp));
|
|
3260
|
+
}
|
|
3261
|
+
if (includeAttributes && meta && typeof meta === "object") {
|
|
3262
|
+
for (const [k, v] of Object.entries(meta)) {
|
|
3263
|
+
if (k === "tokens" || k === "model") continue;
|
|
3264
|
+
if (typeof v === "string" || typeof v === "number" || typeof v === "boolean") {
|
|
3265
|
+
attrs.push(
|
|
3266
|
+
stringAttr(
|
|
3267
|
+
`agent_inspect.preview.${k}`,
|
|
3268
|
+
typeof v === "string" ? v.slice(0, maxLen) : String(v)
|
|
3269
|
+
)
|
|
3270
|
+
);
|
|
3271
|
+
}
|
|
3272
|
+
}
|
|
3273
|
+
}
|
|
3274
|
+
let statusCode = "STATUS_CODE_UNSET";
|
|
3275
|
+
let statusMessage;
|
|
3276
|
+
if (ev.status === "error") {
|
|
3277
|
+
statusCode = "STATUS_CODE_ERROR";
|
|
3278
|
+
statusMessage = meta && typeof meta.error === "object" && meta.error !== null ? String(meta.error.message ?? "error").slice(0, maxLen) : "error";
|
|
3279
|
+
} else if (ev.status === "ok") {
|
|
3280
|
+
statusCode = "STATUS_CODE_OK";
|
|
3281
|
+
}
|
|
3282
|
+
const spanJson = {
|
|
3283
|
+
traceId,
|
|
3284
|
+
spanId,
|
|
3285
|
+
name: ev.name,
|
|
3286
|
+
kind: "SPAN_KIND_INTERNAL",
|
|
3287
|
+
startTimeUnixNano: startNs,
|
|
3288
|
+
attributes: attrs,
|
|
3289
|
+
status: {
|
|
3290
|
+
code: statusCode,
|
|
3291
|
+
...statusMessage !== void 0 ? { message: statusMessage } : {}
|
|
3292
|
+
}
|
|
3293
|
+
};
|
|
3294
|
+
if (parentSpanId !== void 0) {
|
|
3295
|
+
spanJson.parentSpanId = parentSpanId;
|
|
3296
|
+
}
|
|
3297
|
+
if (endNs !== void 0) {
|
|
3298
|
+
spanJson.endTimeUnixNano = endNs;
|
|
3299
|
+
}
|
|
3300
|
+
spans.push(spanJson);
|
|
3301
|
+
}
|
|
3302
|
+
const payload = {
|
|
3303
|
+
resourceSpans: [
|
|
3304
|
+
{
|
|
3305
|
+
resource: {
|
|
3306
|
+
attributes: [stringAttr("service.name", "agent-inspect")]
|
|
3307
|
+
},
|
|
3308
|
+
scopeSpans: [
|
|
3309
|
+
{
|
|
3310
|
+
scope: { name: "agent-inspect" },
|
|
3311
|
+
spans
|
|
3312
|
+
}
|
|
3313
|
+
]
|
|
3314
|
+
}
|
|
3315
|
+
]
|
|
3316
|
+
};
|
|
3317
|
+
return {
|
|
3318
|
+
format: "otlp-json",
|
|
3319
|
+
content: JSON.stringify(payload, null, pretty ? 2 : void 0),
|
|
3320
|
+
contentType: "application/json",
|
|
3321
|
+
fileExtension: ".otlp.json",
|
|
3322
|
+
warnings
|
|
3323
|
+
};
|
|
3324
|
+
}
|
|
3325
|
+
|
|
3326
|
+
// packages/core/src/exporters/validation.ts
|
|
3327
|
+
var EXPERIMENTAL = "Experimental compatibility export \u2014 verify against your target tooling before relying on it.";
|
|
3328
|
+
function validateExportContent(format, content) {
|
|
3329
|
+
const errors = [];
|
|
3330
|
+
const warnings = [EXPERIMENTAL];
|
|
3331
|
+
if (format === "markdown") {
|
|
3332
|
+
if (!content.startsWith("# AgentInspect Run")) {
|
|
3333
|
+
errors.push('Markdown export must start with "# AgentInspect Run"');
|
|
3334
|
+
}
|
|
3335
|
+
return { ok: errors.length === 0, format, errors, warnings };
|
|
3336
|
+
}
|
|
3337
|
+
if (format === "html") {
|
|
3338
|
+
const lower = content.toLowerCase();
|
|
3339
|
+
if (!lower.includes("<!doctype html")) {
|
|
3340
|
+
errors.push("HTML export must include <!doctype html>");
|
|
3341
|
+
}
|
|
3342
|
+
if (/<\s*script\b/i.test(content)) {
|
|
3343
|
+
errors.push("HTML export must not contain script tags");
|
|
3344
|
+
}
|
|
3345
|
+
if (/<\s*link\b[^>]*href\s*=/i.test(content)) {
|
|
3346
|
+
warnings.push("HTML export contains link tags \u2014 ensure no external stylesheets.");
|
|
3347
|
+
}
|
|
3348
|
+
return { ok: errors.length === 0, format, errors, warnings };
|
|
3349
|
+
}
|
|
3350
|
+
if (format === "openinference") {
|
|
3351
|
+
let parsed;
|
|
3352
|
+
try {
|
|
3353
|
+
parsed = JSON.parse(content);
|
|
3354
|
+
} catch {
|
|
3355
|
+
errors.push("OpenInference export is not valid JSON");
|
|
3356
|
+
return { ok: false, format, errors, warnings };
|
|
3357
|
+
}
|
|
3358
|
+
if (!parsed || typeof parsed !== "object") {
|
|
3359
|
+
errors.push("OpenInference export JSON must be an object");
|
|
3360
|
+
return { ok: false, format, errors, warnings };
|
|
3361
|
+
}
|
|
3362
|
+
const o = parsed;
|
|
3363
|
+
if (o.format !== "openinference") {
|
|
3364
|
+
errors.push('OpenInference export must include format: "openinference"');
|
|
3365
|
+
}
|
|
3366
|
+
if (!Array.isArray(o.spans)) {
|
|
3367
|
+
errors.push("OpenInference export must include a spans array");
|
|
3368
|
+
}
|
|
3369
|
+
warnings.push("OpenInference-compatible JSON is not guaranteed for every backend.");
|
|
3370
|
+
return { ok: errors.length === 0, format, errors, warnings };
|
|
3371
|
+
}
|
|
3372
|
+
if (format === "otlp-json") {
|
|
3373
|
+
let parsed;
|
|
3374
|
+
try {
|
|
3375
|
+
parsed = JSON.parse(content);
|
|
3376
|
+
} catch {
|
|
3377
|
+
errors.push("OTLP JSON export is not valid JSON");
|
|
3378
|
+
return { ok: false, format, errors, warnings };
|
|
3379
|
+
}
|
|
3380
|
+
if (!parsed || typeof parsed !== "object") {
|
|
3381
|
+
errors.push("OTLP JSON export must be an object");
|
|
3382
|
+
return { ok: false, format, errors, warnings };
|
|
3383
|
+
}
|
|
3384
|
+
const o = parsed;
|
|
3385
|
+
if (!Array.isArray(o.resourceSpans)) {
|
|
3386
|
+
errors.push("OTLP JSON export must include resourceSpans array");
|
|
3387
|
+
}
|
|
3388
|
+
warnings.push(
|
|
3389
|
+
"OTLP JSON mapping uses OTel GenAI-aligned attributes where applicable; collectors may require transformation."
|
|
3390
|
+
);
|
|
3391
|
+
return { ok: errors.length === 0, format, errors, warnings };
|
|
3392
|
+
}
|
|
3393
|
+
errors.push(`Unsupported export format`);
|
|
3394
|
+
return { ok: false, format, errors, warnings };
|
|
3395
|
+
}
|
|
3396
|
+
|
|
3397
|
+
// packages/core/src/exporters/manual-trace-adapter.ts
|
|
3398
|
+
function stepTypeToInspectKind(t) {
|
|
3399
|
+
switch (t) {
|
|
3400
|
+
case "llm":
|
|
3401
|
+
return "LLM";
|
|
3402
|
+
case "tool":
|
|
3403
|
+
return "TOOL";
|
|
3404
|
+
case "decision":
|
|
3405
|
+
return "DECISION";
|
|
3406
|
+
case "run":
|
|
3407
|
+
return "CHAIN";
|
|
3408
|
+
default:
|
|
3409
|
+
return "LOGIC";
|
|
3410
|
+
}
|
|
3411
|
+
}
|
|
3412
|
+
function mapStepStatus2(s) {
|
|
3413
|
+
if (s === void 0) return "running";
|
|
3414
|
+
if (s === "success") return "ok";
|
|
3415
|
+
return "error";
|
|
3416
|
+
}
|
|
3417
|
+
function manualTraceEventsToRunTree(events) {
|
|
3418
|
+
const started = events.find((e) => e.event === "run_started");
|
|
3419
|
+
if (!started || started.event !== "run_started") {
|
|
3420
|
+
throw new Error("Invalid trace: missing run_started");
|
|
3421
|
+
}
|
|
3422
|
+
const runId = started.runId;
|
|
3423
|
+
const runName = started.name;
|
|
3424
|
+
const completedAll = events.filter((e) => e.event === "run_completed");
|
|
3425
|
+
const lastCompleted = completedAll[completedAll.length - 1];
|
|
3426
|
+
let runStatus;
|
|
3427
|
+
if (lastCompleted === void 0) {
|
|
3428
|
+
runStatus = "running";
|
|
3429
|
+
} else if (lastCompleted.status === "success") {
|
|
3430
|
+
runStatus = "ok";
|
|
3431
|
+
} else {
|
|
3432
|
+
runStatus = "error";
|
|
3433
|
+
}
|
|
3434
|
+
const startedAt = started.startTime;
|
|
3435
|
+
const endedAt = lastCompleted !== void 0 && runStatus !== "running" ? lastCompleted.endTime : void 0;
|
|
3436
|
+
const durationMs = lastCompleted !== void 0 && Number.isFinite(lastCompleted.durationMs) ? lastCompleted.durationMs : void 0;
|
|
3437
|
+
const steps = /* @__PURE__ */ new Map();
|
|
3438
|
+
for (const e of events) {
|
|
3439
|
+
if (e.event !== "step_started") continue;
|
|
3440
|
+
const s = e;
|
|
3441
|
+
steps.set(s.stepId, {
|
|
3442
|
+
id: s.stepId,
|
|
3443
|
+
parentId: s.parentId,
|
|
3444
|
+
name: s.name,
|
|
3445
|
+
type: s.type,
|
|
3446
|
+
startTime: s.startTime,
|
|
3447
|
+
timestamp: s.timestamp,
|
|
3448
|
+
metadata: s.metadata
|
|
3449
|
+
});
|
|
3450
|
+
}
|
|
3451
|
+
for (const e of events) {
|
|
3452
|
+
if (e.event !== "step_completed") continue;
|
|
3453
|
+
const acc = steps.get(e.stepId);
|
|
3454
|
+
if (!acc) continue;
|
|
3455
|
+
acc.status = e.status;
|
|
3456
|
+
acc.endTime = e.endTime;
|
|
3457
|
+
acc.durationMs = e.durationMs;
|
|
3458
|
+
if (e.error?.message) {
|
|
3459
|
+
acc.error = e.error;
|
|
3460
|
+
}
|
|
3461
|
+
}
|
|
3462
|
+
const inspectNodes = /* @__PURE__ */ new Map();
|
|
3463
|
+
for (const acc of steps.values()) {
|
|
3464
|
+
const kind = stepTypeToInspectKind(acc.type);
|
|
3465
|
+
const status = mapStepStatus2(acc.status);
|
|
3466
|
+
const attrs = { ...acc.metadata ?? {} };
|
|
3467
|
+
if (acc.error?.message) {
|
|
3468
|
+
attrs.error = acc.error;
|
|
3469
|
+
}
|
|
3470
|
+
const evt = {
|
|
3471
|
+
eventId: acc.id,
|
|
3472
|
+
runId,
|
|
3473
|
+
parentId: acc.parentId,
|
|
3474
|
+
name: acc.name,
|
|
3475
|
+
kind,
|
|
3476
|
+
timestamp: acc.timestamp,
|
|
3477
|
+
status,
|
|
3478
|
+
durationMs: acc.durationMs,
|
|
3479
|
+
attributes: Object.keys(attrs).length > 0 ? attrs : void 0,
|
|
3480
|
+
confidence: "explicit",
|
|
3481
|
+
source: { type: "manual" }
|
|
3482
|
+
};
|
|
3483
|
+
inspectNodes.set(acc.id, { event: evt, children: [], depth: 0 });
|
|
3484
|
+
}
|
|
3485
|
+
const roots = [];
|
|
3486
|
+
const sortByStart = (a, b) => a.event.timestamp - b.event.timestamp;
|
|
3487
|
+
for (const node of inspectNodes.values()) {
|
|
3488
|
+
const pid = node.event.parentId;
|
|
3489
|
+
if (pid !== void 0 && inspectNodes.has(pid)) {
|
|
3490
|
+
inspectNodes.get(pid).children.push(node);
|
|
3491
|
+
} else {
|
|
3492
|
+
roots.push(node);
|
|
3493
|
+
}
|
|
3494
|
+
}
|
|
3495
|
+
roots.sort(sortByStart);
|
|
3496
|
+
for (const n of inspectNodes.values()) {
|
|
3497
|
+
n.children.sort(sortByStart);
|
|
3498
|
+
}
|
|
3499
|
+
const assignDepth = (n, depth) => {
|
|
3500
|
+
n.depth = depth;
|
|
3501
|
+
for (const c of n.children) assignDepth(c, depth + 1);
|
|
3502
|
+
};
|
|
3503
|
+
for (const r of roots) assignDepth(r, 0);
|
|
3504
|
+
const confidenceBreakdown = {
|
|
3505
|
+
explicit: 0,
|
|
3506
|
+
correlated: 0,
|
|
3507
|
+
heuristic: 0,
|
|
3508
|
+
unknown: 0
|
|
3509
|
+
};
|
|
3510
|
+
const kinds = zeroKinds();
|
|
3511
|
+
function countWalk(nodes) {
|
|
3512
|
+
for (const n of nodes) {
|
|
3513
|
+
confidenceBreakdown[n.event.confidence] += 1;
|
|
3514
|
+
kinds[n.event.kind] += 1;
|
|
3515
|
+
if (n.children.length > 0) countWalk(n.children);
|
|
3516
|
+
}
|
|
3517
|
+
}
|
|
3518
|
+
countWalk(roots);
|
|
3519
|
+
return {
|
|
3520
|
+
runId,
|
|
3521
|
+
name: runName,
|
|
3522
|
+
status: runStatus,
|
|
3523
|
+
startedAt,
|
|
3524
|
+
endedAt,
|
|
3525
|
+
durationMs,
|
|
3526
|
+
children: roots,
|
|
3527
|
+
metadata: {
|
|
3528
|
+
totalEvents: inspectNodes.size,
|
|
3529
|
+
confidenceBreakdown,
|
|
3530
|
+
kinds
|
|
3531
|
+
}
|
|
3532
|
+
};
|
|
3533
|
+
}
|
|
3534
|
+
|
|
3535
|
+
// packages/core/src/exporters/index.ts
|
|
3536
|
+
function mergeExportDefaults(options) {
|
|
3537
|
+
return {
|
|
3538
|
+
format: options.format,
|
|
3539
|
+
includeMetadata: options.includeMetadata ?? true,
|
|
3540
|
+
includeAttributes: options.includeAttributes ?? false,
|
|
3541
|
+
includeErrors: options.includeErrors ?? true,
|
|
3542
|
+
pretty: options.pretty ?? true,
|
|
3543
|
+
redacted: options.redacted ?? true,
|
|
3544
|
+
maxAttributeLength: options.maxAttributeLength ?? 500
|
|
3545
|
+
};
|
|
3546
|
+
}
|
|
3547
|
+
function exportRunTree(tree, options) {
|
|
3548
|
+
const opts = mergeExportDefaults(options);
|
|
3549
|
+
switch (opts.format) {
|
|
3550
|
+
case "markdown":
|
|
3551
|
+
return exportMarkdown(tree, opts);
|
|
3552
|
+
case "html":
|
|
3553
|
+
return exportHtml(tree, opts);
|
|
3554
|
+
case "openinference":
|
|
3555
|
+
return exportOpenInference(tree, opts);
|
|
3556
|
+
case "otlp-json":
|
|
3557
|
+
return exportOtlpJson(tree, opts);
|
|
3558
|
+
default: {
|
|
3559
|
+
const _x = opts.format;
|
|
3560
|
+
throw new Error(`Unsupported export format: ${String(_x)}`);
|
|
3561
|
+
}
|
|
3562
|
+
}
|
|
3563
|
+
}
|
|
3564
|
+
function validateExport(result) {
|
|
3565
|
+
const base = validateExportContent(result.format, result.content);
|
|
3566
|
+
return {
|
|
3567
|
+
ok: base.ok,
|
|
3568
|
+
format: base.format,
|
|
3569
|
+
errors: base.errors,
|
|
3570
|
+
warnings: [...result.warnings, ...base.warnings]
|
|
3571
|
+
};
|
|
3572
|
+
}
|
|
3573
|
+
|
|
3574
|
+
export { DEFAULT_LOG_INGEST_CONFIG, DEFAULT_REDACT_KEYS, DEFAULT_TRACE_DIR_NAME, EXPORT_PAYLOAD_VERSION, EventNormalizer, FALLBACK_TRACE_DIR, JsonLogParser, LiveLogAccumulator, Log4jsParser, MAX_NAME_LENGTH, MAX_TERMINAL_DEPTH, MAX_TERMINAL_NAME_LENGTH, RUNS_DIR_NAME, Redactor, TERMINAL_INDENT, TraceDirectory, TreeBuilder, buildRunSummary, compactAttributes, createRunId, createStepId, diffRuns, diffTraceEvents, ensureTraceDir, escapeHtml, escapeMarkdown, exportHtml, exportMarkdown, exportOpenInference, exportOtlpJson, exportRunTree, extractMetadata, filterTraces, flattenTree, formatDuration2 as formatDuration, formatError, formatTerminalName, formatTimestamp, getCurrentContext, getCurrentDepth, getCurrentRunId, getCurrentRunName, getCurrentStepId, getDefaultTraceDir, getIndent, getParentStepId, getRunIdFromTraceFileName, getTraceDirFromContext, getTraceFilePath, hasActiveContext, initializeTraceFile, inspectRun, isAgentInspectTrace, isSilentContext, isStepStatus, isStepType, isTraceEvent, listTraceFiles, loadLogIngestConfig, manualTraceEventsToComparableRun, manualTraceEventsToRunTree, matchMapping, mergeExportDefaults, mergeLogIngestConfig, observe, parseDuration, parseLogLine, parseLogsToTrees, printError, printFailedAt, printRunComplete, printRunStart, printStepComplete, printStepStart, readTraceEvents, readTraceFile, renderErrorLine, renderRunDiff, renderRunSummary, renderRunTree, renderRunTrees, renderStepLine, resolveTraceDir, runWithContext, runWithStepContext, safeString2 as safeString, serializeEvent, stableJson, step, summarizeTree, truncateName, validateEvent, validateExport, validateExportContent, warn, wildcardMatch, writeTraceEvent };
|
|
918
3575
|
//# sourceMappingURL=index.mjs.map
|
|
919
3576
|
//# sourceMappingURL=index.mjs.map
|