Improve azd ai agent init for non-interactive environments#6823
Improve azd ai agent init for non-interactive environments#6823spboyer wants to merge 3 commits intoAzure:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Improves the azd ai agent init extension initialization flow for non-interactive environments by avoiding prompts for required Azure context when possible.
Changes:
- Default newly created azd environment name to the current directory basename when
--no-promptis active and-eisn’t provided. - In
--no-promptmode, auto-select the Azure subscription when exactly one subscription is available; otherwise return a remediation error.
Comments suppressed due to low confidence (1)
cli/azd/extensions/azure.ai.agents/internal/cmd/init.go:495
- When listing subscriptions in --no-prompt mode, consider passing the existing tenant from the environment (AZURE_TENANT_ID) as
ListSubscriptionsRequest.TenantIdwhen available. Otherwise, users with multiple tenants can hit the "multiple subscriptions" error even if only one subscription exists in the intended tenant.
subsResp, listErr := azdClient.Account().ListSubscriptions(
ctx,
&azdext.ListSubscriptionsRequest{},
)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
4710e8a to
2365d2d
Compare
| envArgs := []string{"env", "new"} | ||
| if flags.env != "" { | ||
| envArgs = append(envArgs, flags.env) | ||
| } else if flags.NoPrompt { |
There was a problem hiding this comment.
Would it be better to just pass --no-prompt to the env new call that we're making, and rely on however core wants to handle that flag, instead of having special logic in the extension? Seems better for consistency
Two fixes for non-TTY environments (CI/CD, IDE terminals, AI assistants): 1. Default environment name to directory basename when --no-prompt is active and no -e flag is provided, consistent with azd init behavior. (Fixes Azure#6817) 2. Auto-select subscription when only one is available in non-interactive mode using azdClient.Account().ListSubscriptions(). Shows clear error with remediation hint when multiple subscriptions exist. (Fixes Azure#6818) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Handle os.Getwd() failure with clear error suggesting '-e <name>' - Sanitize directory basename for env name (replace invalid chars) - Guard against edge cases (root dir, empty/dot names) - Clarify '-e' hint: specify it needs an env with AZURE_SUBSCRIPTION_ID Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…ests - Query UserConfig for defaults.subscription before falling back to ListSubscriptions auto-select logic - Extract sanitizeEnvName helper function for testability - Add 12 test cases covering valid, invalid, and edge-case env names Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2365d2d to
1cdd108
Compare
|
Good points from all three of you. To clarify the intent: This was originally a quick unblock for AI coding assistants (Copilot CLI, etc.) that need to run That said, I agree the principle should live in core:
Proposal: I'll file a core issue to track making
@vhvb1989 @trangevi @kristenwomack what's your preference? Happy to go any direction. |
My preference, in order would be 2, 2 and 1 at the end. - I would swap 1 to the top if there is some customer blocked or some team is planning to demo/present this in the nearest feature. |
Problem
azd ai agent initis unusable in non-interactive (non-TTY) environments because two required inputs (environment name and subscription) have no non-interactive defaults. This blocks CI/CD pipelines, IDE-integrated terminals, AI coding assistants, and scripted automation.Root cause analysis identified 4 sequential blockers when running without a TTY. This PR addresses the first two:
Changes
1. Default environment name to directory basename (#6817)
When
--no-promptis active and no-eflag is provided, the environment name now defaults to the current directory's basename -- consistent with howazd initsuggests-devetc.Before:
no default response for prompt 'Enter a unique environment name:'After: Automatically creates environment named after the working directory
2. Auto-select subscription in non-interactive mode (#6818)
When
--no-promptis active and no subscription is configured, the command now:azdClient.Account().ListSubscriptions()Before:
failed to prompt for subscription: rpc error: ... cannot prompt in non-interactive modeAfter:
Auto-selected subscription: My Sub (guid)multiple subscriptions found but running in non-interactive mode. Set a default with 'azd config set defaults.subscription <id>'Testing
All existing extension tests pass. Changes are in the initialization flow which requires end-to-end testing with Azure resources.
Related Issues
--model-deploymentflag (still blocking for full non-interactive flow)--no-promptbehaviorFixes #6817
Fixes #6818