@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
52 lines • 2.21 kB
JavaScript
class PresenterAuthorityRegistrationError extends Error {
constructor(code) {
super(code);
this.name = "PresenterAuthorityRegistrationError";
this.code = code;
}
}
const SLOT_KEY = Symbol.for("senpi.presenterAccountAuthority.v1");
function createSlot(sealedEmpty = false) {
let authority;
let sealed = sealedEmpty;
return Object.freeze({
register(next) {
if (sealed)
throw new PresenterAuthorityRegistrationError("presenter_authority_sealed");
if (authority)
throw new PresenterAuthorityRegistrationError("presenter_authority_already_registered");
authority = next;
},
take() {
sealed = true;
return authority;
},
});
}
const isSlot = (value) => typeof value === "object" && value !== null && "register" in value && "take" in value;
const pin = (value) => {
Object.defineProperty(globalThis, SLOT_KEY, { value, writable: false, configurable: false, enumerable: false });
return value;
};
function slot() {
const existing = Reflect.getOwnPropertyDescriptor(globalThis, SLOT_KEY);
if (existing === undefined)
return pin(createSlot());
// Only a slot pinned the way pin() pins it is ours. Anything else was planted before this module ran: it reads as
// empty and sealed, and is replaced by such a slot when it can be, so a later swap of the value changes nothing.
if (existing.writable === false && existing.configurable === false && isSlot(existing.value))
return existing.value;
return existing.configurable ? pin(createSlot(true)) : createSlot(true);
}
/**
* @public package entry `@code-yeongyu/senpi/presenter-authority`, called by an embedding distribution before it loads
* the CLI. Throws once a registration exists or main() has started.
*/
export function registerPresenterAccountAuthority(authority) {
slot().register(authority);
}
/** Seals the slot and returns its authority; later calls return the same value. Called first thing by `main()`. */
export function takePresenterAccountAuthority() {
return slot().take();
}
//# sourceMappingURL=presenter-authority-slot.js.map