UNPKG

@adonis-agora/agent

Version:

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

143 lines • 5.45 kB
import { countOtherConsoles, readAgoraConsoles, registerAgoraConsole, } from '../console-switcher.js'; import { getTelescopeGovernanceQueries } from '../telescope/governance-registry.js'; import { consoleShellIdentity } from './console_shell.js'; import { evaluateDashboardGate } from './gate.js'; /** The agent console's id in the shared `'agora.consoles'` registry. */ export const AGENT_CONSOLE_ID = 'agent'; /** * Register the agent governance console into the shared `'agora.consoles'` registry, so every other * Agora console's switcher can link to it and its global search can find runs and threads here. * Call from the provider that mounts the console shell, after the routes are registered. */ export async function registerAgentConsole(app, options, logger = console) { const governance = options.governance ?? getTelescopeGovernanceQueries; const entry = { id: AGENT_CONSOLE_ID, title: 'Agents', package: '@adonis-agora/agent', icon: 'agent', path: options.mount, order: 30, authorize: (ctx) => agentConsoleDecision(ctx, app, options), search: (_ctx, query, limit) => { const queries = governance(); return queries === undefined ? Promise.resolve([]) : searchAgentConsole(queries, options.mount, query, limit); }, }; return registerAgoraConsole(app, entry, logger); } /** The agent console's own gate, as a switcher decision. Never throws. */ export async function agentConsoleDecision(ctx, app, options) { try { const verdict = await evaluateDashboardGate(ctx, { actorResolver: options.actorResolver, authorize: options.authorize, inProduction: app.inProduction, }); if (verdict.ok) return { allowed: true }; return { allowed: false, reason: verdict.error, ...(verdict.redirectTo !== undefined ? { redirectTo: verdict.redirectTo } : {}), }; } catch { return { allowed: false, reason: 'forbidden' }; } } /** * The agent console's search provider: a run id or a thread id opens it directly; otherwise the * query is read as an actor ref or an agent name and their latest runs are listed. */ export async function searchAgentConsole(governance, mount, query, limit) { const q = query.trim(); if (q === '') return []; const hits = []; const seen = new Set(); const push = (hit) => { if (seen.has(hit.href) || hits.length >= limit) return; seen.add(hit.href); hits.push(hit); }; const runHref = (id) => `${mount}#runs/${encodeURIComponent(id)}`; const quiet = async (fn) => { try { return await fn(); } catch { return undefined; } }; const [run, thread, byActor, byAgent] = await Promise.all([ quiet(() => governance.runDetail(q)), governance.threadDetail !== undefined ? quiet(() => governance.threadDetail(q)) : Promise.resolve(undefined), quiet(() => governance.listRuns({ actor: q, first: limit })), quiet(() => governance.listRuns({ agent: q, first: limit })), ]); if (run) { push({ title: `Run ${q}`, subtitle: [run.run.agentName, run.run.status, run.run.actorRef].filter(Boolean).join(' · '), href: runHref(q), }); } if (thread) { push({ title: `Thread ${q}`, href: `${mount}#threads/${encodeURIComponent(q)}` }); } for (const list of [byActor, byAgent]) { for (const row of list?.items ?? []) { push({ title: `${row.agentName ?? 'run'} · ${row.status}`, subtitle: `${row.actorRef} · ${row.startedAt}`, href: runHref(row.runId), }); } } return hits; } /** The `switcher` and `search` fields of the shell's data block, or `{}` when nothing to show. */ export async function switcherShellConfig(app, apiBase) { let registry; try { registry = await readAgoraConsoles(app); } catch { return {}; } if (registry === null) return {}; const base = apiBase === '/' ? '' : apiBase; const others = countOtherConsoles(registry, AGENT_CONSOLE_ID); const searchable = registry.entries.some((entry) => typeof entry.search === 'function'); return { ...(others > 0 ? { switcher: { endpoint: `${base}/governance/consoles`, others } } : {}), ...(searchable ? { search: { endpoint: `${base}/governance/consoles/search` } } : {}), }; } /** * Every console-shell field of the data block for this request: `switcher` / `search` (from the * registry) plus `viewer` / `signOut` (from the `dashboard.viewer` / `dashboard.signOut` options, * else `ctx.auth.user` and the sign-out authkit announced on the registry). Never throws. */ export async function consoleShellConfig(app, ctx, apiBase, options) { let registry = null; try { registry = await readAgoraConsoles(app); } catch { registry = null; } const [switcher, identity] = await Promise.all([ switcherShellConfig(app, apiBase), consoleShellIdentity(ctx, options, registry).catch(() => ({})), ]); return { ...switcher, ...identity }; } //# sourceMappingURL=switcher.js.map