@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
116 lines • 5.21 kB
JavaScript
/** The fixed view behind each catalog {@link staticModelCatalog} built — what boot validation reads. */
const staticViews = new WeakMap();
/** A catalog that answers the same view to everyone — for a fixed list of models. */
export function staticModelCatalog(view) {
const catalog = { list: () => view };
staticViews.set(catalog, view);
return catalog;
}
/**
* The view a catalog answers everyone, when it is a fixed one ({@link staticModelCatalog}, a
* `ModelCatalogView` in `config/agent.ts`, `aiSdkModels().catalog`); `undefined` for a catalog that
* decides per caller, which can only be checked turn by turn.
*/
export function staticCatalogView(catalog) {
return catalog === undefined ? undefined : staticViews.get(catalog);
}
/** Every model id `view` lists, available or not. */
export function catalogModelIds(view) {
return view.providers.flatMap((group) => group.models.map((model) => model.id));
}
/** Accept a catalog or a fixed view (the `config/agent.ts` shorthand). */
export function toModelCatalog(value) {
return typeof value.list === 'function'
? value
: staticModelCatalog(value);
}
/** The entry `id` names in `view`, or `undefined`. */
export function findCatalogModel(view, id) {
for (const group of view.providers) {
const found = group.models.find((model) => model.id === id);
if (found !== undefined)
return found;
}
return undefined;
}
/**
* `model` when `view` offers it as available, else a {@link ModelNotAllowedError} naming why.
* `subject` names who chose it in the message (`model "x"` for a caller's pick, `agent "a" runs on
* model "x"` for a configured one).
*/
export function assertCatalogModel(view, model, subject = `model "${model}"`) {
const entry = findCatalogModel(view, model);
if (entry === undefined) {
throw new ModelNotAllowedError(`${subject} is not offered`);
}
if (!entry.available) {
throw new ModelNotAllowedError(`${subject} is not available${entry.unavailableReason ? `: ${entry.unavailableReason}` : ''}`);
}
return entry.id;
}
/** The words a refusal of a configured model uses for who configured it. */
function configuredSubject(configured) {
return configured.persona !== undefined
? `persona "${configured.persona}" of agent "${configured.agent}" runs on model "${configured.model}", which`
: `agent "${configured.agent}" runs on model "${configured.model}", which`;
}
/**
* The model a turn runs on: the send's own, else the thread's pinned one, else the persona's
* configured `model`, else the agent's, else none (the provider's default — the catalog's
* `default`). A catalog `locked` model wins over all of them, and a send naming another model is
* refused.
*
* Every choice passes the catalog gate for this actor and agent ({@link ModelNotAllowedError},
* answered `400 model_not_allowed`) — a configured model included: it is refused, not swapped for
* another one, so a turn never quietly runs on a model nobody chose. Without a catalog a caller's
* pick is refused, while a configured model goes to the provider as written (no catalog, nothing to
* check it against).
*/
export async function resolveTurnModel(args) {
const { catalog, actor, agent, persona, requested, configured } = args;
const view = catalog === undefined
? undefined
: await catalog.list({ actor, agent, ...(persona !== undefined ? { persona } : {}) });
const locked = view?.locked;
if (locked !== undefined) {
if (requested !== undefined && requested !== locked.model) {
throw new ModelNotAllowedError(`model "${requested}" cannot be selected: ${locked.reason ?? `this agent always uses "${locked.model}"`}`);
}
return locked.model;
}
const picked = requested ?? (await args.pinned?.()) ?? undefined;
if (picked !== undefined) {
if (view === undefined) {
throw new ModelNotAllowedError(`model "${picked}" cannot be selected: no model catalog is configured (models in config/agent.ts)`);
}
return assertCatalogModel(view, picked);
}
if (configured === undefined) {
return undefined;
}
return view === undefined
? configured.model
: assertCatalogModel(view, configured.model, configuredSubject(configured));
}
/**
* A provider that runs every turn on `model` — what the loop wraps the bound provider in when a
* turn has a selected model, so every call it makes (the answer, a structured-output pass, the
* follow-up suggestions) goes to the same one. A provider that does not read `args.model` runs its
* own default, unchanged.
*/
export function withSelectedModel(provider, model) {
return {
runTurn: (args) => provider.runTurn({ ...args, model }),
};
}
/**
* A request named a model the catalog does not offer to this caller right now — or there is no
* catalog to offer one. The routes answer `400` with its message.
*/
export class ModelNotAllowedError extends Error {
constructor(message) {
super(message);
this.name = 'ModelNotAllowedError';
}
}
//# sourceMappingURL=model-catalog.js.map