feat(cursor): support browser login for the agent SDK #12948
MergedYash-Singh1(GitHub profile, opens in a new tab)merged 5 commits into
t3code/codex-turn-mapping fromrefs/pull/12948/headmerged by Yash-Singh1(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/provider/Drivers/CursorDriver.ts lines 49 to 52OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -44,3 +44,9 @@4444 type ProviderSnapshotSettings,4545 } from "../providerUpdateSettings.ts";4646 import { probeCursorSkills } from "./CursorSkills.ts";47+import { makeCursorAuth } from "../CursorAuth.ts";48+import { FileCredentialStore } from "../cursorSdk.ts";49+import {50+ CursorAgentSdkRunner,51+ CursorAgentSdkRunnerError,52+} from "../../orchestration-v2/Adapters/CursorAgentSdk.ts";apps/server/src/provider/CursorAuth.ts lines 69 to 70OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +50,21 @@50+/** Each instance owns its browser credential; explicit API keys always take precedence. */51+export const makeCursorAuth = Effect.fn("makeCursorAuth")(function* (options: CursorAuthOptions) {52+ const scope = yield* Scope.Scope;53+ const crypto = yield* Crypto.Crypto;54+ const lock = yield* Semaphore.make(1);55+ const configuredKey = options.apiKey?.trim() || undefined;56+ const emptyState: ProviderAuthState = {57+ instanceId: options.instanceId,58+ phase: "idle",59+ flowId: null,60+ authorizationUrl: null,61+ expiresAt: null,62+ message: null,63+ };64+ const snapshot = yield* SubscriptionRef.make({ owner: null as string | null, state: emptyState });65+ let active: AuthFlow | undefined;66+ let operation: "idle" | "login" | "stopping" | "closed" = "idle";67+ const sessions = new Set<Scope.Closeable>();68+69+ const failure = (operation: string, detail: string) =>70+ new ProviderSetupError({ instanceId: options.instanceId, operation, detail });
- apps/server/src/provider/CursorAuth.ts lines 245 to 245Resolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -0,0 +208,21 @@208+ const credentials = await pendingStore.load();209+ if (!credentials) throw new Error("Cursor login did not return credentials");210+ await options.store.save(credentials);211+ },212+ catch: () =>213+ new ProviderSetupError({214+ instanceId: options.instanceId,215+ operation: "start",216+ detail: "Could not save the Cursor sign-in. Try again.",217+ }),218+ });219+ yield* options.onChanged(true);220+ active = undefined;221+ operation = "idle";222+ yield* publish(flow, {223+ phase: "succeeded",224+ authorizationUrl: null,225+ expiresAt: null,226+ message: "Signed in with Cursor.",227+ });228+ }).pipe(Effect.uninterruptible),apps/server/src/provider/CursorAuth.ts lines 219 to 219OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -0,0 +199,21 @@199+ Effect.gen(function* () {200+ if (active !== flow) return;201+ yield* publish(flow, {202+ phase: "verifying",203+ authorizationUrl: null,204+ message: "Checking Cursor sign-in.",205+ });206+ yield* Effect.tryPromise({207+ try: async () => {208+ const credentials = await pendingStore.load();209+ if (!credentials) throw new Error("Cursor login did not return credentials");210+ await options.store.save(credentials);211+ },212+ catch: () =>213+ new ProviderSetupError({214+ instanceId: options.instanceId,215+ operation: "start",216+ detail: "Could not save the Cursor sign-in. Try again.",217+ }),218+ });219+ yield* options.onChanged(true);
- apps/server/src/provider/CursorAuth.ts lines 191 to 197OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +177,21 @@177+ Effect.forever,178+ Effect.forkScoped,179+ );180+ yield* Effect.tryPromise({181+ try: (signal) =>182+ (options.login ?? Cursor.auth.login)({183+ openBrowser: false,184+ store: pendingStore,185+ signal,186+ apiKeyName: `T3 Code - ${options.displayName}`,187+ onLoginUrl: (authorizationUrl) => {188+ if (active === flow) Queue.offerUnsafe(urls, authorizationUrl);189+ },190+ }),191+ catch: () =>192+ new ProviderSetupError({193+ instanceId: options.instanceId,194+ operation: "start",195+ detail: "Cursor sign-in failed. Start sign-in again.",196+ }),197+ });apps/server/src/provider/CursorAuth.ts lines 318 to 327Resolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -0,0 +302,21 @@302+ operation: "start",303+ detail:304+ "Remove CURSOR_API_KEY from this provider's environment before using browser sign-in.",305+ });306+ if (active?.owner === owner && operation === "login") return snapshot.value.state;307+ if (operation !== "idle")308+ return yield* new ProviderSetupError({309+ instanceId: options.instanceId,310+ operation: "start",311+ detail: "Cursor setup is already in progress.",312+ });313+ const id = yield* crypto.randomUUIDv4.pipe(314+ Effect.mapError(315+ () =>316+ new ProviderSetupError({317+ instanceId: options.instanceId,318+ operation: "start",319+ detail: "Could not start Cursor sign-in. Try again.",320+ }),321+ ),322+ );apps/server/src/provider/CursorAuth.ts lines 404 to 410OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -0,0 +390,21 @@390+ operation: "logout",391+ detail: "Cursor setup is already stopping.",392+ });393+ operation = "stopping";394+ const flow = active;395+ active = undefined;396+ return flow;397+ }),398+ );399+ const result = yield* Effect.gen(function* () {400+ if (flow?.fiber) yield* Fiber.interrupt(flow.fiber);401+ yield* stopSessions.pipe(Effect.ensuring(stopSessionsWithCredentials));402+ yield* Effect.tryPromise({403+ try: () => options.store.clear(),404+ catch: () =>405+ new ProviderSetupError({406+ instanceId: options.instanceId,407+ operation: "logout",408+ detail: "Could not clear the Cursor sign-in. Try again.",409+ }),410+ });apps/server/src/provider/CursorAuth.ts lines 213 to 219OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +199,21 @@199+ yield* lock.withPermits(1)(200+ Effect.gen(function* () {201+ if (active !== flow) return;202+ yield* publish(flow, {203+ phase: "verifying",204+ authorizationUrl: null,205+ message: "Checking Cursor sign-in.",206+ });207+ yield* Effect.tryPromise({208+ try: async () => {209+ const credentials = await pendingStore.load();210+ if (!credentials) throw new Error("Cursor login did not return credentials");211+ await options.store.save(credentials);212+ },213+ catch: () =>214+ new ProviderSetupError({215+ instanceId: options.instanceId,216+ operation: "start",217+ detail: "Could not save the Cursor sign-in. Try again.",218+ }),219+ });apps/server/src/provider/CursorAuth.ts lines 79 to 85OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +65,21 @@65+ let active: AuthFlow | undefined;66+ let operation: "idle" | "login" | "stopping" | "closed" = "idle";67+ const sessions = new Set<Scope.Closeable>();68+69+ const publish = (flow: AuthFlow, patch: Partial<ProviderAuthState>) =>70+ SubscriptionRef.update(snapshot, (current) => ({71+ owner: flow.owner,72+ state: { ...current.state, ...patch },73+ }));74+75+ const readApiKey = Effect.gen(function* () {76+ if (configuredKey) return configuredKey;77+ const credentials = yield* Effect.tryPromise({78+ try: () => options.store.load(),79+ catch: () =>80+ new ProviderSetupError({81+ instanceId: options.instanceId,82+ operation: "credentials",83+ detail: "Could not read the Cursor sign-in. Try signing in again.",84+ }),85+ });apps/server/src/provider/CursorAuth.ts lines 224 to 230OutdatedResolvedby Yash-Singh1(GitHub profile, opens in a new tab)@@ -0,0 +210,21 @@210+ if (!credentials) throw new Error("Cursor login did not return credentials");211+ await options.store.save(credentials);212+ },213+ catch: () =>214+ new ProviderSetupError({215+ instanceId: options.instanceId,216+ operation: "start",217+ detail: "Could not save the Cursor sign-in. Try again.",218+ }),219+ });220+ const verified = yield* options.onChanged(true).pipe(Effect.exit);221+ if (Exit.isFailure(verified)) {222+ yield* Effect.tryPromise({223+ try: () => options.store.clear(),224+ catch: () =>225+ new ProviderSetupError({226+ instanceId: options.instanceId,227+ operation: "start",228+ detail: "Could not clear the rejected Cursor sign-in. Try signing out.",229+ }),230+ });
Yash-Singh1(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed