agentcrumbs
Version:
Debug mode for any agent.
122 lines • 4.28 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.tail = tail;
const node_fs_1 = __importDefault(require("node:fs"));
const format_js_1 = require("../format.js");
const args_js_1 = require("../args.js");
const app_store_js_1 = require("../app-store.js");
async function tail(args) {
const ns = (0, args_js_1.getFlag)(args, "--ns");
const tag = (0, args_js_1.getFlag)(args, "--tag");
const match = (0, args_js_1.getFlag)(args, "--match");
const session = (0, args_js_1.getFlag)(args, "--session");
const json = (0, args_js_1.hasFlag)(args, "--json");
const appCtx = (0, app_store_js_1.parseAppFlags)(args);
const showApp = appCtx.allApps;
const filePath = (0, app_store_js_1.getStoreFilePath)(appCtx);
const dir = filePath.replace(/\/[^/]+$/, "");
if (!node_fs_1.default.existsSync(filePath)) {
if (!node_fs_1.default.existsSync(dir)) {
node_fs_1.default.mkdirSync(dir, { recursive: true });
}
node_fs_1.default.writeFileSync(filePath, "");
process.stderr.write("Waiting for crumbs... (start the collector: agentcrumbs collect)\n");
}
// Print existing lines from the end (last 50)
const existing = readLastLines(filePath, 50);
for (const crumb of existing) {
if (matchesCrumb(crumb, { ns, tag, match, session })) {
printCrumb(crumb, json, showApp);
}
}
// Watch for new lines
let fileSize = node_fs_1.default.statSync(filePath).size;
const watcher = node_fs_1.default.watch(filePath, () => {
try {
const newSize = node_fs_1.default.statSync(filePath).size;
if (newSize <= fileSize) {
fileSize = newSize;
return;
}
const buffer = Buffer.alloc(newSize - fileSize);
const fd = node_fs_1.default.openSync(filePath, "r");
node_fs_1.default.readSync(fd, buffer, 0, buffer.length, fileSize);
node_fs_1.default.closeSync(fd);
fileSize = newSize;
const lines = buffer.toString("utf-8").split("\n").filter(Boolean);
for (const line of lines) {
try {
const crumb = JSON.parse(line);
if (matchesCrumb(crumb, { ns, tag, match, session })) {
printCrumb(crumb, json, showApp);
}
}
catch {
// skip invalid lines
}
}
}
catch {
// ignore read errors
}
});
const shutdown = () => {
watcher.close();
process.exit(0);
};
process.on("SIGINT", shutdown);
process.on("SIGTERM", shutdown);
}
function matchesCrumb(crumb, filters) {
if (filters.ns) {
const pattern = filters.ns.replace(/\*/g, ".*");
if (!new RegExp(`^${pattern}$`).test(crumb.ns))
return false;
}
if (filters.tag) {
if (!crumb.tags?.includes(filters.tag))
return false;
}
if (filters.session) {
if (crumb.sid !== filters.session)
return false;
}
if (filters.match) {
const str = JSON.stringify(crumb);
if (!str.includes(filters.match))
return false;
}
return true;
}
function printCrumb(crumb, json, showApp) {
if (json) {
process.stdout.write((0, format_js_1.formatCrumbJson)(crumb) + "\n");
}
else {
process.stdout.write((0, format_js_1.formatCrumbPretty)(crumb, { showApp }) + "\n");
}
}
function readLastLines(filePath, count) {
try {
const content = node_fs_1.default.readFileSync(filePath, "utf-8");
const lines = content.split("\n").filter(Boolean);
const lastLines = lines.slice(-count);
return lastLines
.map((line) => {
try {
return JSON.parse(line);
}
catch {
return null;
}
})
.filter((c) => c !== null);
}
catch {
return [];
}
}
//# sourceMappingURL=tail.js.map