-
Notifications
You must be signed in to change notification settings - Fork 0
feat(init): Allow agent init without implicit app selection #244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
djgould
wants to merge
4
commits into
main
Choose a base branch
from
codex/agent-init-keyless
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
13a59bf
Allow agent init without implicit app selection
djgould 0b6092f
Merge branch 'main' into codex/agent-init-keyless
djgould d69f33f
Add changeset for agent init keyless flow
djgould 230a35b
Merge branch 'main' into codex/agent-init-keyless
djgould File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "clerk": patch | ||
| --- | ||
|
|
||
| Allow `clerk init` to run in agent mode without requiring `--app`. For keyless-capable frameworks, agent init now uses keyless setup when no real Clerk app target is provided; explicit `--app` or an existing linked profile still uses the authenticated app-linking flow. Agent init no longer creates, auto-selects, or auto-links a Clerk application when no app target is provided. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,7 +3,7 @@ import { link } from "../link/index.js"; | |||||||||||||
| import { pull } from "../env/pull.js"; | ||||||||||||||
| import { isAgent } from "../../mode.js"; | ||||||||||||||
| import { dim, bold } from "../../lib/color.js"; | ||||||||||||||
| import { throwUserAbort, CliError, errorMessage } from "../../lib/errors.js"; | ||||||||||||||
| import { throwUserAbort, CliError, errorMessage, throwUsageError } from "../../lib/errors.js"; | ||||||||||||||
| import { lookupFramework, type FrameworkInfo } from "../../lib/framework.js"; | ||||||||||||||
| import { resolveProfile } from "../../lib/config.js"; | ||||||||||||||
| import { log } from "../../lib/log.js"; | ||||||||||||||
|
|
@@ -58,6 +58,7 @@ type InitOptions = { | |||||||||||||
|
|
||||||||||||||
| export async function init(options: InitOptions = {}) { | ||||||||||||||
| const cwd = process.cwd(); | ||||||||||||||
| const agent = isAgent(); | ||||||||||||||
|
|
||||||||||||||
| const frameworkOverride = options.framework | ||||||||||||||
| ? (lookupFramework(options.framework) ?? undefined) | ||||||||||||||
|
|
@@ -72,7 +73,7 @@ export async function init(options: InitOptions = {}) { | |||||||||||||
|
|
||||||||||||||
| // In agent mode, implicitly enable --yes to skip all confirmation prompts. | ||||||||||||||
| const overrides: BootstrapOverrides = { | ||||||||||||||
| skipConfirm: options.yes || isAgent(), | ||||||||||||||
| skipConfirm: options.yes || agent, | ||||||||||||||
| pmOverride: options.pm, | ||||||||||||||
| nameOverride: options.name, | ||||||||||||||
| }; | ||||||||||||||
|
|
@@ -94,14 +95,23 @@ export async function init(options: InitOptions = {}) { | |||||||||||||
| await enrichProjectContext(ctx); | ||||||||||||||
|
|
||||||||||||||
| const authed = await isAuthenticated(); | ||||||||||||||
| const keyless = resolveKeylessMode(bootstrap, ctx, authed); | ||||||||||||||
| const linkedProfile = agent && !options.app ? await resolveProfile(ctx.cwd) : undefined; | ||||||||||||||
| const hasRealAppTarget = Boolean(options.app || linkedProfile); | ||||||||||||||
| const keyless = resolveKeylessMode({ | ||||||||||||||
| agent, | ||||||||||||||
| bootstrap, | ||||||||||||||
| ctx, | ||||||||||||||
| authed, | ||||||||||||||
| hasRealAppTarget, | ||||||||||||||
| }); | ||||||||||||||
| ctx.keyless = keyless; | ||||||||||||||
|
|
||||||||||||||
| const skipAuth = !keyless && bootstrap != null && overrides.skipConfirm && !authed; | ||||||||||||||
| const manualSetup = | ||||||||||||||
| !keyless && (agent ? !hasRealAppTarget : bootstrap != null && overrides.skipConfirm && !authed); | ||||||||||||||
|
|
||||||||||||||
| if (!keyless && !skipAuth) { | ||||||||||||||
| if (!keyless && !manualSetup) { | ||||||||||||||
| bar(); | ||||||||||||||
| await authenticateAndLink(ctx.cwd, options.app); | ||||||||||||||
| await authenticateAndLink(ctx.cwd, options.app, { allowInteractiveLogin: !agent }); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // Short-circuit on a fully-clean re-run so env pull / skills prompt don't | ||||||||||||||
|
|
@@ -113,12 +123,15 @@ export async function init(options: InitOptions = {}) { | |||||||||||||
|
|
||||||||||||||
| if (alreadySetUp) { | ||||||||||||||
| log.success("\nClerk is already set up in this project."); | ||||||||||||||
| if (agent && manualSetup) { | ||||||||||||||
| printBootstrapManualSetupInfo(ctx.framework.name); | ||||||||||||||
| } | ||||||||||||||
| outro("Done"); | ||||||||||||||
| return; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| bar(); | ||||||||||||||
| if (skipAuth) { | ||||||||||||||
| if (manualSetup) { | ||||||||||||||
| printBootstrapManualSetupInfo(ctx.framework.name); | ||||||||||||||
| } else if (!keyless) { | ||||||||||||||
| await pull({ file: ctx.envFile, cwd: ctx.cwd }); | ||||||||||||||
|
|
@@ -214,28 +227,35 @@ function printBootstrapNextSteps( | |||||||||||||
| function printBootstrapManualSetupInfo(frameworkName: string): void { | ||||||||||||||
| const lines = [ | ||||||||||||||
| `\n ${frameworkName} requires API keys — set them up manually:`, | ||||||||||||||
| " clerk auth login", | ||||||||||||||
| " clerk link", | ||||||||||||||
| " clerk init --app <app_id>", | ||||||||||||||
| " clerk env pull", | ||||||||||||||
| ]; | ||||||||||||||
| log.info(lines.map(dim).join("\n")); | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| // --- Keyless --- | ||||||||||||||
|
|
||||||||||||||
| function resolveKeylessMode( | ||||||||||||||
| bootstrap: BootstrapResult | null, | ||||||||||||||
| ctx: ProjectContext, | ||||||||||||||
| authed: boolean, | ||||||||||||||
| ): boolean { | ||||||||||||||
| // Auto-keyless is scoped to bootstrap (new-project) flows only. For existing | ||||||||||||||
| // projects, fall through to the authenticated flow so `clerk init` still | ||||||||||||||
| // runs `authenticateAndLink` and pulls real keys — even when signed out the | ||||||||||||||
| // user gets a login prompt rather than being silently dropped into keyless | ||||||||||||||
| // (which would skip `env pull` and could overwrite permissive middleware). | ||||||||||||||
| if (!bootstrap) return false; | ||||||||||||||
|
|
||||||||||||||
| function resolveKeylessMode({ | ||||||||||||||
| agent, | ||||||||||||||
| bootstrap, | ||||||||||||||
| ctx, | ||||||||||||||
| authed, | ||||||||||||||
| hasRealAppTarget, | ||||||||||||||
| }: { | ||||||||||||||
| agent: boolean; | ||||||||||||||
| bootstrap: BootstrapResult | null; | ||||||||||||||
| ctx: ProjectContext; | ||||||||||||||
| authed: boolean; | ||||||||||||||
| hasRealAppTarget: boolean; | ||||||||||||||
| }): boolean { | ||||||||||||||
| if (ctx.framework.supportsKeyless) { | ||||||||||||||
| if (agent) return !hasRealAppTarget; | ||||||||||||||
|
|
||||||||||||||
| // Auto-keyless is scoped to bootstrap (new-project) flows only in human | ||||||||||||||
| // mode. Existing projects keep the authenticated flow so real keys can be | ||||||||||||||
| // pulled unless an agent explicitly chooses keyless by omitting an app. | ||||||||||||||
| if (!bootstrap) return false; | ||||||||||||||
|
|
||||||||||||||
| // Authenticated (OAuth token or CLERK_PLATFORM_API_KEY) — use the | ||||||||||||||
| // authenticated flow so real keys get pulled into .env. Otherwise fall | ||||||||||||||
| // back to keyless: the app runs on auto-generated dev keys and the user | ||||||||||||||
|
|
@@ -253,19 +273,33 @@ function resolveKeylessMode( | |||||||||||||
|
|
||||||||||||||
| // --- Auth --- | ||||||||||||||
|
|
||||||||||||||
| async function resolveAuthLabel(): Promise<string> { | ||||||||||||||
| async function resolveAuthLabel({ | ||||||||||||||
| allowInteractiveLogin, | ||||||||||||||
| }: { | ||||||||||||||
| allowInteractiveLogin: boolean; | ||||||||||||||
| }): Promise<string> { | ||||||||||||||
| const hasApiKey = Boolean(process.env.CLERK_PLATFORM_API_KEY); | ||||||||||||||
| if (hasApiKey) return "Using API key"; | ||||||||||||||
|
|
||||||||||||||
| const email = await getAuthenticatedEmail(); | ||||||||||||||
| if (email) return `Logged in as ${email}`; | ||||||||||||||
|
|
||||||||||||||
| if (!allowInteractiveLogin) { | ||||||||||||||
| throwUsageError( | ||||||||||||||
| "clerk init in agent mode requires CLERK_PLATFORM_API_KEY or a valid stored session when using a real Clerk application. Pass --app only with auth available, or omit --app for keyless-capable frameworks.", | ||||||||||||||
| ); | ||||||||||||||
| } | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This throws
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| await login({ showNextSteps: false }); | ||||||||||||||
| return ""; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| async function authenticateAndLink(cwd: string, app: string | undefined): Promise<void> { | ||||||||||||||
| const label = await resolveAuthLabel(); | ||||||||||||||
| async function authenticateAndLink( | ||||||||||||||
| cwd: string, | ||||||||||||||
| app: string | undefined, | ||||||||||||||
| options: { allowInteractiveLogin: boolean }, | ||||||||||||||
| ): Promise<void> { | ||||||||||||||
| const label = await resolveAuthLabel(options); | ||||||||||||||
| const profile = await resolveProfile(cwd); | ||||||||||||||
|
|
||||||||||||||
| const alreadyOnRequestedApp = profile && (!app || profile.profile.appId === app); | ||||||||||||||
|
|
||||||||||||||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
to be honest i'm not sure this is what we want, but its what we were doing before. i find the fact that
clerk initdoesn't require auth for bootstrapping and does require auth in existing projects a bit odd