@unified-llm/core
Version:
Unified LLM interface (in-memory).
50 lines • 1.57 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NOOP_LOGGER = void 0;
exports.toModelSafeError = toModelSafeError;
exports.logTimed = logTimed;
function toModelSafeError(err) {
if (err instanceof Error) {
return { name: err.name, message: err.message };
}
return { name: "Error", message: String(err) };
}
exports.NOOP_LOGGER = {
debug: () => { },
info: () => { },
warn: () => { },
error: () => { },
child: () => exports.NOOP_LOGGER,
};
/**
* Runs an async task while measuring duration and logging success or failure.
* Logs include duration_ms and, on failure, a normalized error object.
*/
async function logTimed(logger, clock, event, meta, fn, level = "info") {
const start = clock.nowMs();
try {
const result = await fn();
const end = clock.nowMs();
const durationMs = Math.max(0, end - start);
logger[level](event, {
...meta,
ok: true,
duration_ms: durationMs,
...(clock.nowEpochMs ? { timestamp_epoch_ms: clock.nowEpochMs() } : {}),
});
return result;
}
catch (err) {
const end = clock.nowMs();
const durationMs = Math.max(0, end - start);
logger.error(event, {
...meta,
ok: false,
duration_ms: durationMs,
error: toModelSafeError(err),
...(clock.nowEpochMs ? { timestamp_epoch_ms: clock.nowEpochMs() } : {}),
});
throw err;
}
}
//# sourceMappingURL=logging.js.map