@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
252 lines • 11.6 kB
JavaScript
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 { args, flags } from '@adonisjs/core/ace';
import { AgentCommand, dayFlag, opt } from '../src/cli/agent_command.js';
import { governancePendingApprovalsPage, governanceRunDetail, governanceRunsPage, governanceThreadDetail, governanceThreadsPage, governanceToolCallsPage, } from '../src/governance-reads.js';
const usd = (value) => value === null || value === undefined ? '—' : `$${value.toFixed(4)}`;
const ms = (value) => value === null || value === undefined ? '—' : `${value}ms`;
/** The cursor paging flags of a list (`--after`/`--before` + `--first`/`--last`). */
class PagedList extends AgentCommand {
paging() {
return this.pagingQuery({
after: this.after,
before: this.before,
first: this.first,
last: this.last,
});
}
}
__decorate([
flags.string({ description: 'Only this organization (with tenancy on)' })
], PagedList.prototype, "tenant", void 0);
__decorate([
flags.string({ description: 'The page after this cursor' })
], PagedList.prototype, "after", void 0);
__decorate([
flags.string({ description: 'The page before this cursor' })
], PagedList.prototype, "before", void 0);
__decorate([
flags.number({ description: 'Page size (default 50, at most 200)' })
], PagedList.prototype, "first", void 0);
__decorate([
flags.number({ description: 'Page size, counted back from --before' })
], PagedList.prototype, "last", void 0);
/** `agent:runs:list` — the console's Runs list, newest first. */
export class AgentRunsList extends AgentCommand {
static commandName = 'agent:runs:list';
static description = 'List agent runs, newest first';
async execute() {
const console = await this.governance();
const filter = {};
if (opt(this.actor) !== undefined)
filter.actor = this.actor;
if (opt(this.agent) !== undefined)
filter.agent = this.agent;
if (opt(this.status) !== undefined)
filter.status = this.status;
const from = dayFlag('from', this.from);
const to = dayFlag('to', this.to);
if (from !== undefined && to !== undefined)
filter.startedAt = { between: [from, to] };
else if (from !== undefined)
filter.startedAt = { gte: from };
else if (to !== undefined)
filter.startedAt = { lte: to };
const page = await governanceRunsPage(console.queries(opt(this.tenant)), console.actorDirectory, {
filter,
...this.pagingQuery({ after: this.after, first: this.first }),
});
return this.output(page, (value) => {
this.table(['Run', 'Agent', 'Actor', 'Status', 'Started', 'Duration', 'Tokens', 'Cost'], value.data.map((run) => [
run.runId,
run.agentName ?? '—',
run.actorLabel ?? run.actorRef,
run.status,
run.startedAt,
ms(run.durationMs),
run.inputTokens + run.outputTokens,
usd(run.costUsd),
]), 'No runs.');
this.cursorHint(value.meta);
});
}
}
__decorate([
flags.string({ description: 'Only this organization (with tenancy on)' })
], AgentRunsList.prototype, "tenant", void 0);
__decorate([
flags.string({ description: 'Only this actor ref' })
], AgentRunsList.prototype, "actor", void 0);
__decorate([
flags.string({ description: 'Only this agent' })
], AgentRunsList.prototype, "agent", void 0);
__decorate([
flags.string({ description: 'running, completed, failed or cancelled' })
], AgentRunsList.prototype, "status", void 0);
__decorate([
flags.string({ description: 'Started on or after this UTC day (YYYY-MM-DD)' })
], AgentRunsList.prototype, "from", void 0);
__decorate([
flags.string({ description: 'Started on or before this UTC day (YYYY-MM-DD)' })
], AgentRunsList.prototype, "to", void 0);
__decorate([
flags.string({ description: 'The page after this cursor' })
], AgentRunsList.prototype, "after", void 0);
__decorate([
flags.number({ description: 'Page size (default 50, at most 200)' })
], AgentRunsList.prototype, "first", void 0);
/** `agent:runs:show <id>` — one run's trace: run, tool calls, approvals, usage (and messages). */
export class AgentRunsShow extends AgentCommand {
static commandName = 'agent:runs:show';
static description = "Show one run's trace";
async execute() {
const console = await this.governance();
const detail = await governanceRunDetail(console.queries(opt(this.tenant)), console.actorDirectory, this.id);
if (detail === null)
return this.refuse(`No run "${this.id}".`);
return this.output(detail, (value) => {
const { run } = value;
this.pairs([
['Run', run.runId],
['Thread', run.threadId],
['Agent', run.agentName],
['Actor', run.actorLabel ?? run.actorRef],
['Status', run.status],
['Started', run.startedAt],
['Finished', run.finishedAt],
['Duration', ms(run.durationMs)],
['Tokens in/out', `${run.inputTokens}/${run.outputTokens}`],
['Cost', usd(run.costUsd)],
['Error', run.error],
]);
this.table(['Tool call', 'Tool', 'Status', 'Time', 'Error'], value.toolCalls.map((call) => [
call.toolCallId,
call.toolName,
call.status,
ms(call.executionMs),
call.error ?? '',
]), 'No tool calls.');
if (this.messages) {
for (const message of value.messages) {
this.logger.log(`[${message.role}] ${message.content}`);
}
}
else {
this.logger.log(`${value.messages.length} messages (--messages to print them)`);
}
});
}
}
__decorate([
args.string({ description: 'Run id' })
], AgentRunsShow.prototype, "id", void 0);
__decorate([
flags.string({ description: 'Only within this organization (with tenancy on)' })
], AgentRunsShow.prototype, "tenant", void 0);
__decorate([
flags.boolean({ description: 'Also print the messages' })
], AgentRunsShow.prototype, "messages", void 0);
/** `agent:threads:list` — the recent thread activity feed (the console's Threads tab). */
export class AgentThreadsList extends PagedList {
static commandName = 'agent:threads:list';
static description = 'List threads by recent activity, newest first';
async execute() {
const console = await this.governance();
const page = await governanceThreadsPage(console.queries(opt(this.tenant)), console.actorDirectory, this.paging());
return this.output(page, (value) => {
this.table(['Thread', 'Title', 'Actor', 'Messages', 'Tokens', 'Last activity'], value.data.map((thread) => [
thread.threadId,
thread.title,
thread.actorLabel ?? thread.actorRef,
thread.messageCount,
thread.totalTokens,
thread.lastActivityAt,
]), 'No threads.');
this.cursorHint(value.meta);
});
}
}
/** `agent:threads:show <id>` — one thread's drill-down: usage rollup, runs and messages. */
export class AgentThreadsShow extends AgentCommand {
static commandName = 'agent:threads:show';
static description = "Show one thread's usage, runs and messages";
async execute() {
const console = await this.governance();
const detail = await governanceThreadDetail(console.queries(opt(this.tenant)), console.actorDirectory, this.id);
if (detail === undefined) {
return this.refuse('This governance adapter does not support thread detail.');
}
if (detail === null)
return this.refuse(`No thread "${this.id}".`);
return this.output(detail, (value) => {
this.pairs([
['Thread', value.threadId],
['Title', value.title],
['Actor', value.actorLabel ?? value.actorRef],
['Created', value.createdAt],
['Updated', value.updatedAt],
['Deleted', value.deleted ? 'yes' : 'no'],
['Runs', value.usage.runCount],
['Messages', value.usage.messageCount],
['Tokens', value.usage.totalTokens],
['Cost', usd(value.usage.costUsd)],
]);
this.table(['Run', 'Status', 'Started', 'Cost'], value.runs.map((run) => [run.runId, run.status, run.startedAt, usd(run.costUsd)]), 'No runs.');
});
}
}
__decorate([
args.string({ description: 'Thread id' })
], AgentThreadsShow.prototype, "id", void 0);
__decorate([
flags.string({ description: 'Only within this organization (with tenancy on)' })
], AgentThreadsShow.prototype, "tenant", void 0);
/** `agent:tool-calls:list` — the recent tool-call activity feed, newest first. */
export class AgentToolCallsList extends PagedList {
static commandName = 'agent:tool-calls:list';
static description = 'List recent tool calls, newest first';
async execute() {
const page = await governanceToolCallsPage((await this.governance()).queries(opt(this.tenant)), this.paging());
return this.output(page, (value) => {
this.table(['Tool call', 'Tool', 'Type', 'Status', 'Thread', 'At'], value.data.map((call) => [
call.toolCallId,
call.toolName,
call.toolType,
call.status,
call.threadId,
call.createdAt,
]), 'No tool calls.');
this.cursorHint(value.meta);
});
}
}
/** `agent:approvals:list` — the cross-actor HITL inbox, oldest first. */
export class AgentApprovalsList extends PagedList {
static commandName = 'agent:approvals:list';
static description = 'List tool calls waiting for a human approval, oldest first';
async execute() {
const console = await this.governance();
const page = await governancePendingApprovalsPage(console.queries(opt(this.tenant)), console.actorDirectory, {
...(opt(this.actor) !== undefined ? { filter: { actor: this.actor } } : {}),
...this.paging(),
});
return this.output(page, (value) => {
this.table(['Tool call', 'Tool', 'Run', 'Actor', 'Requested'], value.data.map((row) => [
row.toolCallId,
row.toolName,
row.runId ?? '—',
row.actorLabel ?? row.actorRef,
row.requestedAt,
]), 'No pending approvals.');
this.cursorHint(value.meta);
});
}
}
__decorate([
flags.string({ description: 'Only this actor ref' })
], AgentApprovalsList.prototype, "actor", void 0);
//# sourceMappingURL=agent_runs.js.map