@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
74 lines • 3 kB
JavaScript
import { projectSlot } from "@earendil-works/pi-ai/auth/pool/slots";
function asExtensionOAuthRegistry(store) {
const candidate = store;
return typeof candidate.registerOAuthProvider === "function" &&
typeof candidate.unregisterOAuthProvider === "function"
? candidate
: undefined;
}
/** Async credential store overlay for non-persistent runtime API keys. */
export class RuntimeCredentials {
constructor(store) {
this.overrides = new Map();
this.store = store;
this.withRefreshLock = store.withRefreshLock?.bind(store);
}
/** The store extension OAuth providers register into; a session's ModelRegistry reads it (senpi#3434). */
get backingStore() {
return this.store;
}
registerOAuthProvider(providerId, oauth) {
asExtensionOAuthRegistry(this.store)?.registerOAuthProvider(providerId, oauth);
}
unregisterOAuthProvider(providerId) {
asExtensionOAuthRegistry(this.store)?.unregisterOAuthProvider(providerId);
}
setRuntimeApiKey(providerId, apiKey) {
this.overrides.set(providerId, apiKey);
}
removeRuntimeApiKey(providerId) {
this.overrides.delete(providerId);
}
hasRuntimeApiKey(providerId) {
return this.overrides.has(providerId);
}
/**
* Reads that found the backing store locked and answered from its cached snapshot.
* Stores that never report contention count as 0.
*/
busyReadCount() {
const store = this.store;
return typeof store.getBusyReadCount === "function" ? store.getBusyReadCount() : 0;
}
async read(providerId, options) {
options?.signal?.throwIfAborted();
const override = this.overrides.get(providerId);
return override ? { type: "api_key", key: override } : this.store.read(providerId, options);
}
async readSlot(providerId, slotName, options) {
options?.signal?.throwIfAborted();
const override = this.overrides.get(providerId);
if (override)
return projectSlot({ type: "api_key", key: override }, slotName);
if (this.store.readSlot)
return this.store.readSlot(providerId, slotName, options);
return projectSlot(await this.store.read(providerId, options), slotName);
}
async list(options) {
const entries = new Map((await this.store.list(options)).map((entry) => [entry.providerId, entry]));
options?.signal?.throwIfAborted();
for (const providerId of this.overrides.keys()) {
entries.set(providerId, { providerId, type: "api_key" });
}
return [...entries.values()];
}
modify(providerId, fn, options) {
return this.store.modify(providerId, fn, options);
}
async delete(providerId, options) {
options?.signal?.throwIfAborted();
await this.store.delete(providerId, options);
this.overrides.delete(providerId);
}
}
//# sourceMappingURL=runtime-credentials.js.map