UNPKG

@adonis-agora/agent

Version:

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

148 lines • 5.83 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; }; /** * The shared base of the `agent:*` operator commands: boots the app (never its workers or server — * the provider's `start` is a no-op under ace), reads the instances the console routes use from the * container, and prints a table or, with `--json`, one JSON document. A refusal prints to stderr and * exits `1`. */ import { userInfo } from 'node:os'; import { BaseCommand, flags } from '@adonisjs/core/ace'; import { AgentService } from '../agent-service.js'; import { AgentGovernanceConsole, GovernanceUnavailableError } from '../governance-console.js'; import { InvalidColumnFilterError } from '../list-api.js'; const DAY = /^\d{4}-\d{2}-\d{2}$/; /** A refusal the command prints as is (no stack) before exiting `1`. */ export class CliRefusal extends Error { constructor(message) { super(message); this.name = 'CliRefusal'; } } /** Who a command acts as: `cli:<os user>` (or `cli` when the OS user cannot be read). */ export function cliActorRef() { try { const name = userInfo().username; return name ? `cli:${name}` : 'cli'; } catch { return 'cli'; } } /** `undefined` for an absent or empty flag, else the string. */ export function opt(value) { return value === undefined || value === '' ? undefined : value; } /** A UTC day flag, checked (`YYYY-MM-DD`). */ export function dayFlag(name, value) { const day = opt(value); if (day !== undefined && !DAY.test(day)) { throw new CliRefusal(`--${name} takes a UTC day (YYYY-MM-DD); got "${day}".`); } return day; } export const today = () => new Date().toISOString().slice(0, 10); export class AgentCommand extends BaseCommand { static options = { startApp: true }; /** Write the command's result: JSON with `--json`, else `human()`. */ output(value, human) { if (this.json) { this.logger.log(JSON.stringify(value, null, 2)); return; } human(value); } /** A table of `rows`; `empty` when there are none. */ table(headers, rows, empty = 'Nothing.') { if (rows.length === 0) { this.logger.info(empty); return; } const table = this.ui.table(); table.head(headers); for (const row of rows) table.row(row.map(String)); table.render(); } /** `key: value` lines. */ pairs(pairs) { for (const [key, value] of pairs) { this.logger.log(`${key}: ${value === null || value === undefined ? '—' : value}`); } } async governance() { if (!this.app.container.hasBinding(AgentGovernanceConsole)) { throw new CliRefusal('The agent is not booted: is `@adonis-agora/agent/agent_provider` in adonisrc.ts providers?'); } return this.app.container.make(AgentGovernanceConsole); } async agentService() { if (!this.app.container.hasBinding(AgentService)) { throw new CliRefusal('The agent is not booted: is `@adonis-agora/agent/agent_provider` in adonisrc.ts providers?'); } return this.app.container.make(AgentService); } /** The actor a command acts as. */ cliActor() { return { id: cliActorRef() }; } /** * Confirm an irreversible action: `--force` skips the question; without a terminal to ask on, the * action is refused. `true` to go ahead. */ async confirmed(force, question) { if (force) return true; if (!process.stdin.isTTY && this.prompt.traps.prompts.size === 0) { throw new CliRefusal(`${question} Re-run with --force to confirm without a prompt.`); } return this.prompt.confirm(question); } /** The cursor-paging part of a list query, from the paging flags. */ pagingQuery(paging) { const qs = {}; if (opt(paging.after) !== undefined) qs.after = paging.after; if (opt(paging.before) !== undefined) qs.before = paging.before; if (paging.first !== undefined) qs.first = String(paging.first); if (paging.last !== undefined) qs.last = String(paging.last); return qs; } /** Print the next/previous cursors of a cursor page under its table. */ cursorHint(meta) { if (meta.nextCursor !== null) this.logger.log(`next page: --after ${meta.nextCursor}`); if (meta.prevCursor !== null) this.logger.log(`previous page: --before ${meta.prevCursor}`); } async run() { try { await this.execute(); } catch (error) { if (error instanceof CliRefusal || error instanceof GovernanceUnavailableError || error instanceof InvalidColumnFilterError) { this.logger.error(error.message); this.exitCode = 1; return; } throw error; } } /** Refuse with `message` (exit `1`). */ refuse(message) { throw new CliRefusal(message); } } __decorate([ flags.boolean({ description: 'Print one JSON document to stdout' }) ], AgentCommand.prototype, "json", void 0); //# sourceMappingURL=agent_command.js.map