naisys
Version:
NAISYS - Autonomous AI agent runner with built-in context management and cost tracking
45 lines • 1.52 kB
JavaScript
import { HubEvents, LOG_FLUSH_INTERVAL_MS, } from "@naisys/hub-protocol";
/**
* Shared log write buffer for all agent runtimes on this NAISYS host.
* Flushes buffered entries to the hub on a single timer, capping the
* update rate regardless of how many agents are running.
*/
export function createHubLogBuffer(hubClient) {
const buffer = [];
let isFlushing = false;
setInterval(() => void flush(), LOG_FLUSH_INTERVAL_MS);
function pushEntry(entry, resolveAttachment) {
buffer.push({ entry, resolveAttachment });
}
async function flush() {
if (buffer.length === 0)
return;
if (isFlushing)
return;
isFlushing = true;
const items = buffer.splice(0, buffer.length);
try {
// Resolve any pending attachment uploads.
for (const item of items) {
if (item.resolveAttachment) {
try {
item.entry.attachmentId = await item.resolveAttachment();
}
catch {
// Upload failed; log entry will be sent without attachment.
}
}
}
const entries = items.map((item) => item.entry);
hubClient.sendMessage(HubEvents.LOG_WRITE, { entries });
}
finally {
isFlushing = false;
}
}
return {
pushEntry,
flushFinal: flush,
};
}
//# sourceMappingURL=hubLogBuffer.js.map