UNPKG

@adonis-agora/agent

Version:

Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.

46 lines • 1.91 kB
/** Nearest-rank percentile of an ascending-sorted list; `null` when it is empty. */ export function percentile(sorted, p) { if (sorted.length === 0) return null; return sorted[Math.min(sorted.length - 1, Math.floor(p * sorted.length))] ?? null; } /** {@link Latency} of a list of durations, in any order. */ export function summarizeLatency(values) { const sorted = [...values].sort((a, b) => a - b); return { samples: sorted.length, p50Ms: percentile(sorted, 0.5), p95Ms: percentile(sorted, 0.95), }; } /** * Turn latency and failures by code over a set of runs. Latency counts completed turns only, and * only a person's turn: a delegation's child run is part of its parent's turn, and counting it again * would skew both the median and the tail toward short sub-tasks. Every failed run counts toward its * code, the same runs {@link import('./spi/governance-queries.js').RunReliability.failed} counts. */ export function summarizeRunMetrics(runs) { const ttft = []; const duration = []; const codes = new Map(); for (const run of runs) { if (run.status === 'failed') { const code = run.errorCode ?? 'unknown'; codes.set(code, (codes.get(code) ?? 0) + 1); continue; } if (run.status !== 'completed' || run.parentRunId !== null) continue; if (run.finishedAt !== null) duration.push(run.finishedAt - run.startedAt); if (run.ttftMs !== null) ttft.push(run.ttftMs); } return { latency: { ttft: summarizeLatency(ttft), duration: summarizeLatency(duration) }, failuresByCode: [...codes.entries()] .map(([code, count]) => ({ code, count })) .sort((left, right) => right.count - left.count || left.code.localeCompare(right.code)), }; } //# sourceMappingURL=run-metrics.js.map