@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
156 lines • 6.01 kB
JavaScript
/**
* Structural reader for the tenancy contract `@adonis-agora/context` publishes on its global slots.
* Never imports that package: with the slot missing, too old, or throwing, tenancy reads as OFF, so
* the agent behaves exactly as it does without tenancy.
*
* The tenant here is the customer a piece of data belongs to (an organization, a clinic…), not a
* process topology.
*/
const ACCESSOR = Symbol.for('@agora/context:accessor');
const TENANCY = Symbol.for('@agora/context:tenancy');
/**
* The tenancy reader: context's versioned `:tenancy` slot (v1) when present, else the `:accessor`
* slot's tenancy members (an earlier context), else nothing.
*/
function reader() {
const tenancy = slot(TENANCY);
if (tenancy?.version === 1 && typeof tenancy.tenantFilter === 'function') {
return tenancy;
}
const accessor = slot(ACCESSOR);
return typeof accessor?.tenantFilter === 'function' ? accessor : undefined;
}
function slot(key) {
const value = globalThis[key];
return value !== null && typeof value === 'object' ? value : undefined;
}
/** Resolve the agent's `tenancy` option. With no (or an old) context, tenancy is off. */
export function resolveAgentTenancy(option) {
const accessor = reader();
const off = { enabled: false, nullRows: 'tenantless' };
if (option === false)
return off;
if (accessor === undefined)
return off;
const own = typeof option === 'object' ? option : {};
let context;
try {
context = accessor.tenancy?.();
}
catch {
context = undefined;
}
const enabled = own.enabled === undefined || own.enabled === 'inherit'
? context?.enabled === true
: own.enabled;
const kinds = own.kinds ?? context?.kinds;
return {
enabled,
...(kinds !== undefined ? { kinds: [...kinds] } : {}),
nullRows: own.nullRows ?? 'tenantless',
};
}
/**
* How the current request (or job) must be scoped. Tenancy off → `system` (nothing filtered). A
* tenant of a kind the agent does not honour reads as `tenantless`.
*/
export function currentTenantFilter(tenancy) {
if (!tenancy.enabled)
return { mode: 'system' };
const accessor = reader();
// A `required: true` context throws here when there is no tenant: the caller refuses.
{
const filter = accessor?.tenantFilter?.(tenancy.kinds !== undefined ? { kinds: tenancy.kinds } : undefined);
if (filter === undefined || filter === null || typeof filter !== 'object') {
return { mode: 'system' };
}
if (filter.mode === 'tenant') {
const kind = filter.tenant?.kind;
if (tenancy.kinds !== undefined && !tenancy.kinds.includes(kind)) {
return { mode: 'tenantless' };
}
}
return filter;
}
}
/** The tenant id to stamp on a write right now: the tenant's id in `tenant` mode, else `null`. */
export function currentTenantRef(tenancy) {
const filter = currentTenantFilter(tenancy);
return filter.mode === 'tenant' ? filter.tenant.id : null;
}
/**
* Whether a row stamped `rowTenant` is visible under `filter` (the matching rule every Agora lib
* applies). Invisible rows are answered as not found.
*/
export function tenantVisible(filter, rowTenant, nullRows = 'tenantless') {
const row = rowTenant ?? null;
switch (filter.mode) {
case 'system':
case 'all':
return true;
case 'tenantless':
return row === null;
case 'tenant':
return row === filter.tenant.id || (row === null && nullRows === 'shared');
}
}
/** The {@link TenantScope} of a filter, or `undefined` for "no filtering" (`system` / `all`). */
export function scopeOfFilter(filter, nullRows = 'tenantless') {
switch (filter.mode) {
case 'system':
case 'all':
return undefined;
case 'tenantless':
return { tenants: [], includeNull: true };
case 'tenant':
return { tenants: [filter.tenant.id], includeNull: nullRows === 'shared' };
}
}
/** Whether `tenantRef` is inside `scope` (`undefined` scope = everything). */
export function inScope(scope, tenantRef) {
if (scope === undefined)
return true;
const ref = tenantRef ?? null;
return ref === null ? scope.includeNull : scope.tenants.includes(ref);
}
/** Display labels for tenant ids, from context's label resolver; ids map to themselves otherwise. */
export async function tenantLabels(ids, kind) {
const labels = Object.fromEntries(ids.map((id) => [id, id]));
const tenancy = slot(TENANCY);
if (tenancy?.version !== 1 || typeof tenancy.tenantLabels !== 'function')
return labels;
try {
const resolved = await tenancy.tenantLabels(ids, kind);
for (const id of ids) {
const label = resolved?.[id];
if (typeof label === 'string' && label !== '')
labels[id] = label;
}
}
catch {
// labels are display only
}
return labels;
}
/**
* `actor` with `tenantRef` filled from the active tenant when tenancy is on and the actor has none.
* A resolver that set `tenantRef` wins.
*/
export function withTenantRef(actor, tenancy) {
if (tenancy === undefined || !tenancy.enabled || actor.tenantRef !== undefined)
return actor;
const tenantRef = currentTenantRef(tenancy);
return tenantRef === null ? actor : { ...actor, tenantRef };
}
/** An actor resolver whose actors carry the active tenant (see {@link withTenantRef}). */
export function tenantFillingResolver(resolver, tenancy) {
if (!tenancy.enabled)
return resolver;
const wrapped = Object.create(resolver);
wrapped.resolve = async (req) => {
const actor = await resolver.resolve(req);
return actor !== null && typeof actor === 'object' ? withTenantRef(actor, tenancy) : actor;
};
return wrapped;
}
//# sourceMappingURL=tenancy.js.map