naisys
Version:
NAISYS - Autonomous AI agent runner with built-in context management and cost tracking
60 lines • 2.02 kB
JavaScript
import { COST_FLUSH_INTERVAL_MS, CostWriteResponseSchema, HubEvents, } from "@naisys/hub-protocol";
/**
* Shared cost 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.
*
* After each flush, per-user budgetLeft values from the hub response
* are dispatched to registered callbacks.
*/
export function createHubCostBuffer(hubClient) {
const buffer = [];
let isFlushing = false;
// Per-user callbacks for budget updates
const budgetCallbacks = new Map();
setInterval(() => void flush(), COST_FLUSH_INTERVAL_MS);
function pushEntry(entry) {
buffer.push(entry);
}
function registerBudgetCallback(userId, callback) {
budgetCallbacks.set(userId, callback);
}
function unregisterBudgetCallback(userId) {
budgetCallbacks.delete(userId);
}
async function flush() {
if (buffer.length === 0)
return;
if (isFlushing)
return;
isFlushing = true;
const entries = buffer.splice(0, buffer.length);
try {
const response = await hubClient.sendRequest(HubEvents.COST_WRITE, {
entries,
});
const parsed = CostWriteResponseSchema.safeParse(response);
if (parsed.success) {
for (const entry of parsed.data.budgets) {
const cb = budgetCallbacks.get(entry.userId);
if (cb && entry.budgetLeft !== null) {
cb(entry.budgetLeft);
}
}
}
}
catch {
// Silently ignore flush errors; costs are best-effort.
}
finally {
isFlushing = false;
}
}
return {
pushEntry,
registerBudgetCallback,
unregisterBudgetCallback,
flushFinal: flush,
};
}
//# sourceMappingURL=hubCostBuffer.js.map