@adonis-agora/agent
Version:
Governed, durable-backed AI agent (chat + tool-calling + governance) for AdonisJS — part of the Agora ecosystem.
46 lines • 1.89 kB
JavaScript
/**
* Structural, dependency-free reader for the Agora runtime context — mirrors
* `@adonis-agora/authz`'s `agora/context.ts`. The Agora context library publishes a READ accessor
* on the well-known symbol slot. We never import that package; when the slot is absent (context not
* installed) the reader degrades to `undefined` and the caller fails closed.
*/
/** The symbol slot the Agora context library writes its read accessor into. */
export const AGORA_CONTEXT_ACCESSOR = Symbol.for('@agora/context:accessor');
/** Read the active Agora context accessor from the global slot, if present. */
export function readContextAccessor() {
const slot = globalThis[AGORA_CONTEXT_ACCESSOR];
if (slot == null || typeof slot !== 'object')
return undefined;
return slot;
}
/**
* Call an accessor method safely: tolerates the method being absent (a partial/mocked accessor,
* or a slot that isn't the real `@adonis-agora/context` implementation) and tolerates it throwing.
* Degrades to `undefined` either way — this package never fabricates identity from a broken
* context, it just leaves the field empty and lets the caller fail closed.
*/
function readMethod(fn) {
if (typeof fn !== 'function')
return undefined;
try {
return fn();
}
catch {
return undefined;
}
}
/**
* The active caller's `userRef` from the Agora context, or `undefined` outside a context / when
* the accessor slot is absent.
*/
export function userRefFromContext(accessor = readContextAccessor()) {
return readMethod(accessor?.userRef);
}
/**
* The active tenant id from the Agora context, or `undefined` outside a context / when unset /
* when the accessor slot is absent.
*/
export function tenantIdFromContext(accessor = readContextAccessor()) {
return readMethod(accessor?.tenantId);
}
//# sourceMappingURL=agora-context.js.map