@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
60 lines • 2.48 kB
JavaScript
import { stampLogin } from "@earendil-works/pi-ai/auth/pool/login-id";
import { emptyCredential, listAccounts, SENTINEL_OAUTH_FIELDS } from "./accounts.js";
import { CURSOR_CLI_OAUTH_PROVIDER_ID, importNativeCursorCredential } from "./oauth-login.js";
function asManagedCredential(credential) {
if (credential?.type !== "oauth")
return undefined;
const managed = credential;
return Array.isArray(managed.accounts) &&
managed.access === SENTINEL_OAUTH_FIELDS.access &&
managed.refresh === SENTINEL_OAUTH_FIELDS.refresh &&
managed.expires === SENTINEL_OAUTH_FIELDS.expires
? managed
: undefined;
}
async function bootstrapNativeCredential(deps) {
let current;
try {
current = await deps.store.read(CURSOR_CLI_OAUTH_PROVIDER_ID);
const managed = asManagedCredential(current);
if (managed && listAccounts(managed).length > 0)
return current;
if (current !== undefined && !managed)
return current;
if (deps.canBootstrap !== undefined && !(await deps.canBootstrap()))
return current;
const native = await deps.readNativeCredential();
if (native === undefined)
return current;
return await deps.store.modify(CURSOR_CLI_OAUTH_PROVIDER_ID, async (latest) => {
const latestManaged = asManagedCredential(latest);
if (latestManaged && listAccounts(latestManaged).length > 0)
return latest;
if (latest !== undefined && !latestManaged)
return latest;
// The first-run copy adds a signed-in account, so it stamps a login id like a login (senpi#4099).
return stampLogin(await importNativeCursorCredential(latestManaged ?? emptyCredential(), async () => native));
});
}
catch {
return current;
}
}
/**
* Read the managed credential afresh and bootstrap it once from the native
* Cursor OAuth credential when empty. Concurrent reads share only the
* in-flight operation; later turns re-read storage so login/import changes are
* immediately visible.
*/
export function createCursorCliOauthCredentialReader(deps) {
let inFlight;
return () => {
if (inFlight)
return inFlight;
inFlight = bootstrapNativeCredential(deps).finally(() => {
inFlight = undefined;
});
return inFlight;
};
}
//# sourceMappingURL=native-bootstrap.js.map