@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
106 lines • 5.48 kB
JavaScript
/**
* The governance console's list and detail reads, as the `/agent/governance/*` routes and the
* `agent:*` ace commands both serve them: the same read-model call, the same `filter`/sort/paging
* rules (`@adonis-agora/filter` wire, refused with `InvalidColumnFilterError`) and the same actor
* labels. `qs` is the decoded query string — the CLI builds the same object from its flags.
*
* Server-only: the provider and the commands import it, the browser entry points never do.
*/
import { withActorLabel, withActorLabels } from './actor-labels.js';
import { ApprovalsFilter, applyListFilter, assertFixedSort, cursorPage, feedPaging, NoFieldsFilter, parseForwardParams, parseListWindow, RunsFilter, windowFetchSize, windowPage, } from './list-api.js';
import { decodeKeyset } from './stores/keyset.js';
/** Page sizes of the governance lists. */
export const GOVERNANCE_LIST_SIZES = { defaultSize: 50, maxSize: 200 };
/** Check a list request that takes no filter and has one order; throws on anything else. */
export async function plainList(qs, order) {
await applyListFilter({}, NoFieldsFilter, qs);
assertFixedSort(qs.sort, order);
}
/**
* Runs, newest first: `filter[actor|agent|status]`, `filter[startedAt][gte|lte|between]` (UTC
* days); sort fixed to `-startedAt`; forward cursor paging (`after` + `first`).
*/
export async function governanceRunsPage(gov, directory, qs) {
const filter = await applyListFilter({}, RunsFilter, qs);
assertFixedSort(qs.sort, '-startedAt');
const { after, size } = parseForwardParams(qs, GOVERNANCE_LIST_SIZES);
const page = await gov.listRuns({
...filter,
first: size,
...(after !== undefined ? { after } : {}),
});
return cursorPage({ ...page, items: await withActorLabels(page.items, directory) });
}
/** One run's full trace (run + messages + tool calls + approvals + usage), or `null`. */
export async function governanceRunDetail(gov, directory, runId) {
const detail = await gov.runDetail(runId);
if (detail === null)
return null;
return { ...detail, run: (await withActorLabel(detail.run, directory)) ?? detail.run };
}
/** The cross-actor HITL inbox, oldest first: `filter[actor]`; sort fixed to `requestedAt`. */
export async function governancePendingApprovalsPage(gov, directory, qs) {
const filter = await applyListFilter({}, ApprovalsFilter, qs);
assertFixedSort(qs.sort, 'requestedAt');
const window = parseListWindow(qs, GOVERNANCE_LIST_SIZES);
const rows = await gov.pendingApprovals({ ...filter, limit: windowFetchSize(window) });
const page = windowPage(rows, window);
return { ...page, data: await withActorLabels(page.data, directory) };
}
/**
* The recent tool-call activity feed, newest first; no filter; sort fixed to `-createdAt`. Paged by
* keyset when the read-model offers `toolCallsPage` (a deep page costs what the first does); window
* cursors from older servers, and `before`, keep the capped-read path.
*/
export async function governanceToolCallsPage(gov, qs) {
await plainList(qs, '-createdAt');
if (gov.toolCallsPage !== undefined) {
const paging = feedPaging(qs, GOVERNANCE_LIST_SIZES, isKeysetCursor);
if (paging.kind === 'keyset') {
return cursorPage(await gov.toolCallsPage({
first: paging.size,
...(paging.after !== undefined ? { after: paging.after } : {}),
}));
}
}
const window = parseListWindow(qs, GOVERNANCE_LIST_SIZES);
return windowPage(await gov.recentToolCalls(windowFetchSize(window)), window);
}
/** The recent thread activity feed, newest first; no filter; sort fixed to `-lastActivityAt`. */
export async function governanceThreadsPage(gov, directory, qs) {
await plainList(qs, '-lastActivityAt');
if (gov.threadsPage !== undefined) {
const paging = feedPaging(qs, GOVERNANCE_LIST_SIZES, isKeysetCursor);
if (paging.kind === 'keyset') {
const page = await gov.threadsPage({
first: paging.size,
...(paging.after !== undefined ? { after: paging.after } : {}),
});
return cursorPage({
...page,
items: await withActorLabels(page.items.map(threadTimestamps), directory),
});
}
}
const window = parseListWindow(qs, GOVERNANCE_LIST_SIZES);
const page = windowPage(await gov.recentThreads(windowFetchSize(window)), window);
return { ...page, data: await withActorLabels(page.data.map(threadTimestamps), directory) };
}
/** A thread row always carries `createdAt` / `updatedAt` on the wire: `null` when its read-model has none. */
function threadTimestamps(row) {
return { ...row, createdAt: row.createdAt ?? null, updatedAt: row.updatedAt ?? null };
}
const isKeysetCursor = (cursor) => decodeKeyset(cursor) !== null;
/**
* One thread's drill-down (metadata + lifetime usage + recent runs/messages), `null` when unknown,
* `undefined` when the read-model does not support it (an optional SPI method → `501`).
*/
export async function governanceThreadDetail(gov, directory, threadId) {
if (gov.threadDetail === undefined)
return undefined;
const detail = await withActorLabel(await gov.threadDetail(threadId), directory);
if (detail === null)
return null;
return { ...detail, runs: await withActorLabels(detail.runs, directory) };
}
//# sourceMappingURL=governance-reads.js.map