naisys
Version:
NAISYS - Autonomous AI agent runner with built-in context management and cost tracking
97 lines • 2.98 kB
JavaScript
import chalk from "chalk";
export var OutputColor;
(function (OutputColor) {
OutputColor["comment"] = "greenBright";
OutputColor["error"] = "redBright";
OutputColor["llm"] = "magenta";
OutputColor["console"] = "white";
OutputColor["loading"] = "yellow";
OutputColor["notice"] = "cyan";
})(OutputColor || (OutputColor = {}));
export function createOutputService(logService) {
const consoleBuffer = [];
/** Whether the output for this agent should be piped to the console */
let consoleEnabled = false;
function isConsoleEnabled() {
return consoleEnabled;
}
function setConsoleEnabled(enabled) {
const flush = enabled && !consoleEnabled;
consoleEnabled = enabled;
if (flush) {
flushBuffer();
}
}
const BUFFER_MAX_LINES = 10;
// color available on chalk
function write(msg, color = OutputColor.console) {
if (consoleEnabled) {
console.log(chalk[color](msg));
}
else {
consoleBuffer.push(chalk[color](msg));
if (consoleBuffer.length > BUFFER_MAX_LINES) {
consoleBuffer.splice(0, consoleBuffer.length - BUFFER_MAX_LINES);
}
}
}
function flushBuffer() {
if (!consoleEnabled) {
throw new Error("Console is not enabled"); // do nothing
}
if (consoleBuffer.length) {
consoleBuffer.forEach((line) => console.log(line));
consoleBuffer.length = 0;
}
}
/* Important system messages we want the operator to notice in the console */
function notice(msg) {
write(msg, OutputColor.notice);
}
/** Meant for non-content output we show in the console, but is not added to the context */
function comment(msg) {
write(msg, OutputColor.comment);
}
function commentAndLog(msg, filepath) {
comment(msg);
writeDbLog(msg, "comment", filepath);
}
/** Log without echoing to stdout — for input already shown via readline */
function logOnly(msg) {
writeDbLog(msg, "comment");
}
function error(msg) {
write(msg, OutputColor.error);
}
function errorAndLog(msg, err) {
const stack = err instanceof Error && err.stack ? err.stack : undefined;
if (stack) {
error(`${msg} (see error log for details)`);
writeDbLog(`${msg}\n${stack}`, "error");
}
else {
error(msg);
writeDbLog(msg, "error");
}
}
function writeDbLog(msg, type, filepath) {
logService.write({
role: "user",
content: msg,
type,
}, filepath);
}
return {
notice,
write,
comment,
commentAndLog,
logOnly,
error,
errorAndLog,
consoleBuffer,
isConsoleEnabled,
setConsoleEnabled,
};
}
//# sourceMappingURL=output.js.map