UNPKG

@adonis-agora/agent

Version:

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

219 lines • 10.6 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; import { flags } from '@adonisjs/core/ace'; import { withActorLabels } from '../src/actor-labels.js'; import { AgentCommand, dayFlag, opt, today } from '../src/cli/agent_command.js'; const usd = (value) => value === null || value === undefined ? '—' : `$${value.toFixed(4)}`; /** The `--from`/`--to` day range every rollup takes (default: today). */ class RangeCommand extends AgentCommand { range() { const fromDay = dayFlag('from', this.from) ?? today(); const toDay = dayFlag('to', this.to) ?? dayFlag('from', this.from) ?? today(); return { fromDay, toDay }; } } __decorate([ flags.string({ description: 'First UTC day, YYYY-MM-DD (default today)' }) ], RangeCommand.prototype, "from", void 0); __decorate([ flags.string({ description: 'Last UTC day, YYYY-MM-DD (default --from)' }) ], RangeCommand.prototype, "to", void 0); __decorate([ flags.string({ description: 'Only this organization (with tenancy on)' }) ], RangeCommand.prototype, "tenant", void 0); /** `agent:spend:show` — the console overview's token + cost rollup, by model, actor or feature. */ export class AgentSpendShow extends RangeCommand { static commandName = 'agent:spend:show'; static description = 'Token and cost rollup over UTC days, by model, actor or feature'; async execute() { const console = await this.governance(); const gov = console.queries(opt(this.tenant)); const range = this.range(); const by = opt(this.by) ?? 'model'; if (by === 'actor') { const rows = await withActorLabels(await gov.spendByActor(range), console.actorDirectory); return this.output(rows, (items) => this.table(['Actor', 'Requests', 'Tokens', 'Cost'], items.map((row) => [ row.actorLabel ?? row.actorRef, row.requests, row.totalTokens, usd(row.costUsd), ]), 'No spend in this range.')); } if (by === 'feature') { if (gov.spendByFeature === undefined) { return this.refuse('This governance adapter does not report spend by feature.'); } const rows = await gov.spendByFeature(range); return this.output(rows, (items) => this.table(['Feature', 'Source', 'Requests', 'Input', 'Output', 'Cost'], items.map((row) => [ row.feature, row.source, row.requests, row.inputTokens, row.outputTokens, usd(row.costUsd), ]), 'No spend in this range.')); } if (by !== 'model') return this.refuse(`--by is model, actor or feature; got "${by}".`); const rows = await gov.spendByModel(range); return this.output(rows, (items) => this.table(['Model', 'Requests', 'Input', 'Output', 'Cost'], items.map((row) => [ row.modelId, row.requests, row.inputTokens, row.outputTokens, usd(row.costUsd), ]), 'No spend in this range.')); } } __decorate([ flags.string({ description: 'Group by model (default), actor or feature' }) ], AgentSpendShow.prototype, "by", void 0); /** `agent:usage:trend` — the daily token + cost trend (the overview chart). */ export class AgentUsageTrend extends RangeCommand { static commandName = 'agent:usage:trend'; static description = 'Daily token and cost trend over UTC days'; async execute() { const gov = (await this.governance()).queries(opt(this.tenant)); const rows = await gov.usageTrend(this.range()); return this.output(rows, (items) => this.table(['Day', 'Tokens', 'Cost'], items.map((row) => [row.day, row.totalTokens, usd(row.costUsd)]), 'No usage in this range.')); } } /** `agent:usage:breakdown` — spend per actor × feature ("cost by actor and by feature"). */ export class AgentUsageBreakdown extends RangeCommand { static commandName = 'agent:usage:breakdown'; static description = 'Spend per actor and feature over UTC days or an instant window'; async execute() { const console = await this.governance(); const gov = console.queries(opt(this.tenant)); if (gov.usageBreakdown === undefined) { return this.refuse('This governance adapter does not report a usage breakdown.'); } const since = opt(this.since); const until = opt(this.until); if ((since === undefined) !== (until === undefined)) { return this.refuse('--since and --until go together: pass both, or neither (--from/--to).'); } if (since !== undefined && until !== undefined && (Number.isNaN(Date.parse(since)) || Number.isNaN(Date.parse(until)))) { return this.refuse('--since and --until must be ISO instants.'); } const window = since !== undefined && until !== undefined ? { from: since, to: until } : this.range(); const rows = await withActorLabels(await gov.usageBreakdown(window, { ...(opt(this.actor) !== undefined ? { actor: this.actor } : {}), ...(opt(this.feature) !== undefined ? { feature: this.feature } : {}), ...(this.reprice === false ? { reprice: false } : {}), }), console.actorDirectory); return this.output(rows, (items) => this.table(['Actor', 'Feature', 'Source', 'Requests', 'Input', 'Output', 'Cost', 'Estimated'], items.map((row) => [ row.actorLabel ?? row.actorRef, row.feature, row.source, row.requests, row.inputTokens, row.outputTokens, usd(row.costUsd), usd(row.estimatedCostUsd), ]), 'No usage in this range.')); } } __decorate([ flags.string({ description: 'Window start, an ISO instant (with --until, replaces --from/--to)', }) ], AgentUsageBreakdown.prototype, "since", void 0); __decorate([ flags.string({ description: 'Window end (exclusive), an ISO instant' }) ], AgentUsageBreakdown.prototype, "until", void 0); __decorate([ flags.string({ description: 'Only this actor ref' }) ], AgentUsageBreakdown.prototype, "actor", void 0); __decorate([ flags.string({ description: 'Only this feature' }) ], AgentUsageBreakdown.prototype, "feature", void 0); __decorate([ flags.boolean({ description: 'Reprice unpriced rows against current prices (--no-reprice: recorded cost only)', default: true, showNegatedVariantInHelp: true, }) ], AgentUsageBreakdown.prototype, "reprice", void 0); /** `agent:reliability:show` — run success/failure/cancel rates and the mean settled duration. */ export class AgentReliabilityShow extends AgentCommand { static commandName = 'agent:reliability:show'; static description = 'Run success, failure and cancel rates'; async execute() { const gov = (await this.governance()).queries(opt(this.tenant)); const from = dayFlag('from', this.from); const to = dayFlag('to', this.to); const stats = await gov.runReliability({ ...(from !== undefined ? { from } : {}), ...(to !== undefined ? { to } : {}), }); const pct = (rate) => `${(rate * 100).toFixed(1)}%`; return this.output(stats, (value) => { this.pairs([ ['Runs', value.runs], ['Completed', value.completed], ['Failed', value.failed], ['Cancelled', value.cancelled], ['Running', value.running], ['Success rate', pct(value.successRate)], ['Failure rate', pct(value.failureRate)], ['Cancel rate', pct(value.cancelRate)], ['Mean duration (ms)', value.avgDurationMs], ]); if (value.byAgent !== undefined && value.byAgent.length > 0) { this.table(['Agent', 'Runs', 'Failed', 'Success'], value.byAgent.map((row) => [ row.agentName ?? '—', row.runs, row.failed, pct(row.successRate), ])); } }); } } __decorate([ flags.string({ description: 'First UTC day, YYYY-MM-DD (default: all time)' }) ], AgentReliabilityShow.prototype, "from", void 0); __decorate([ flags.string({ description: 'Last UTC day, YYYY-MM-DD' }) ], AgentReliabilityShow.prototype, "to", void 0); __decorate([ flags.string({ description: 'Only this organization (with tenancy on)' }) ], AgentReliabilityShow.prototype, "tenant", void 0); /** `agent:genui:show` — generative-UI telemetry (first-try success, errors, cost per turn). */ export class AgentGenuiShow extends RangeCommand { static commandName = 'agent:genui:show'; static description = 'Generative UI telemetry over UTC days'; async execute() { const gov = (await this.governance()).queries(opt(this.tenant)); if (gov.genuiStats === undefined) { return this.refuse('This governance adapter does not record generative UI telemetry.'); } const stats = await gov.genuiStats(this.range()); return this.output(stats, (value) => { const { totals, costPerTurn } = value; this.pairs([ ['UI calls', totals.calls], ['Turns', totals.turns], [ 'First-try rate', totals.firstTryRate === null ? null : `${(totals.firstTryRate * 100).toFixed(1)}%`, ], ['Retried', totals.retried], ['Failed', totals.failed], ['Cost', usd(totals.costUsd)], ['Avg cost per turn', usd(costPerTurn.avgUsd)], ]); if (value.errors.length > 0) { this.table(['Tool', 'Category', 'Error', 'Count'], value.errors.map((row) => [row.toolName, row.category, row.message, row.count])); } }); } } //# sourceMappingURL=agent_usage.js.map