@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
105 lines • 5.78 kB
JavaScript
import { getAgentDir } from "../../../../config.js";
import { AuthStorage } from "../../../auth-storage.js";
import { toBuiltinProviderContext } from "../provider-context.js";
import { registerCursorCliAccountCommand } from "./account-command.js";
import { refreshCursorCliModelCatalogForLane } from "./catalog-refresh.js";
import { defaultCursorAgentExecutableDeps, resolveCursorAgentExecutable } from "./executable.js";
import { cursorCliForceRefusalPending } from "./guardrails.js";
import { STATIC_CURSOR_CLI_MODELS } from "./models.js";
import { createCursorCliOauthCredentialReader } from "./native-bootstrap.js";
import { CURSOR_CLI_OAUTH_PROVIDER_ID, createCursorCliOauthConfig } from "./oauth-login.js";
import { loadCursorCliOauthProviderSettingsFromDisk, persistCursorCliNoApprovalAcknowledgement, persistCursorCliOauthEnabled, } from "./settings.js";
import { streamCursorCliOauth } from "./stream.js";
export { CURSOR_CLI_OAUTH_PROVIDER_ID } from "./oauth-login.js";
const defaultResolveExecutable = (settings) => resolveCursorAgentExecutable({ ...defaultCursorAgentExecutableDeps(), settings });
/**
* Registers the cursor-cli-oauth provider lane. Registration is unconditional:
* the provider ships with the offline model fallback and reports availability
* through the oauth `check`, so a missing cursor-agent binary never hides the
* lane - it only makes `check` name the install guidance.
*/
export function registerCursorCliOauthExtension(pi, deps = {}) {
const cwd = deps.cwd ?? process.cwd();
const agentDir = deps.agentDir ?? getAgentDir();
const store = deps.store ?? AuthStorage.create();
const readCurrent = deps.readCurrent ??
createCursorCliOauthCredentialReader({
store,
readNativeCredential: deps.readNativeCredential ?? (() => store.read("cursor")),
// Copying the host's native Cursor credential IS the ambient lane, so it
// requires the explicit flag - never merely a logged-in cursor-agent.
canBootstrap: () => {
const settings = loadSettings(cwd);
if (!settings.enabled || settings.explicitlyDisabled)
return false;
try {
resolveExecutable(settings);
return true;
}
catch {
return false;
}
},
});
const loadSettings = deps.loadSettings ?? loadCursorCliOauthProviderSettingsFromDisk;
const resolveExecutable = deps.resolveExecutable ?? defaultResolveExecutable;
// Todo 21: /cursor-account command plus the reload/shutdown teardown for
// this extension generation (minimal additive wiring).
registerCursorCliAccountCommand(pi);
const register = (models) => {
pi.registerProvider(CURSOR_CLI_OAUTH_PROVIDER_ID, {
baseUrl: CURSOR_CLI_OAUTH_PROVIDER_ID,
api: CURSOR_CLI_OAUTH_PROVIDER_ID,
// Like the Anthropic Subscription lane, this lane selects and fails over among its own
// accounts, so the generic credential pool must not choose a second time (senpi#2081).
ownsAccountSelection: true,
// Requests run the cursor-agent CLI as a subprocess, under Cursor logins on this machine.
localOnly: true,
models: models.map((entry) => ({ ...entry })),
// Settings, accounts, and the executable are re-resolved inside every
// turn; nothing captured here outlives a settings or credential change.
streamSimple: (model, context, options) => streamCursorCliOauth(model, toBuiltinProviderContext(context), options, { cwd, agentDir, store }),
// A kill-switched lane, or one whose unacknowledged --force gate
// guarantees a refusal, must not consume an implicit fallback-expansion
// slot it can never serve. Only deterministic settings-level states
// exclude; explicit selection and /login stay available either way.
fallbackEligible: () => {
const current = loadSettings(cwd);
return !current.explicitlyDisabled && !cursorCliForceRefusalPending(current);
},
oauth: createCursorCliOauthConfig({
readCurrent,
readSettings: () => loadSettings(cwd),
resolveExecutable,
persistAcknowledgement: (acknowledgedAt) => persistCursorCliNoApprovalAcknowledgement(cwd, acknowledgedAt),
persistEnabled: (enabled) => persistCursorCliOauthEnabled(cwd, enabled),
}),
});
};
// Register immediately with the offline fallback so a missing, hanging, or
// broken cursor-agent can never delay or block provider registration; the
// probe-backed catalog replaces it once resolved. The probe itself runs only
// when the lane is usable, inside the account HOME (senpi#1722).
register(STATIC_CURSOR_CLI_MODELS);
void refreshCursorCliModelCatalogForLane({
agentDir,
settings: loadSettings(cwd),
readCurrent,
resolveExecutable,
...(deps.runModelsProbe === undefined ? {} : { runProbe: deps.runModelsProbe }),
})
.then((models) => {
if (models === undefined)
return;
const resolved = models.map((entry) => entry.id).join("\n");
if (resolved !== STATIC_CURSOR_CLI_MODELS.map((entry) => entry.id).join("\n"))
register(models);
})
.catch(() => {
// The loader degrades to the static fallback itself; keep the registered fallback.
});
}
export default function cursorCliOauthExtension(pi) {
registerCursorCliOauthExtension(pi);
}
//# sourceMappingURL=index.js.map