-
Notifications
You must be signed in to change notification settings - Fork 306
feat: add azd ai agent project commands for Foundry endpoint management #8162
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
29 changes: 29 additions & 0 deletions
29
cli/azd/extensions/azure.ai.agents/internal/cmd/project.go
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,29 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "github.com/azure/azure-dev/cli/azd/pkg/azdext" | ||
| "github.com/spf13/cobra" | ||
| ) | ||
|
|
||
| func newProjectCommand(extCtx *azdext.ExtensionContext) *cobra.Command { | ||
| extCtx = ensureExtensionContext(extCtx) | ||
|
|
||
| cmd := &cobra.Command{ | ||
| Use: "project <command> [options]", | ||
| Short: "Manage the default Microsoft Foundry project endpoint.", | ||
| Long: `Manage the default Microsoft Foundry project endpoint. | ||
|
|
||
| These commands persist a workspace-level project endpoint in the azd global | ||
| config (~/.azd/config.json) so that other agent commands can resolve it | ||
| without requiring an azd environment or explicit flags.`, | ||
| } | ||
|
|
||
| cmd.AddCommand(newProjectSetCommand(extCtx)) | ||
| cmd.AddCommand(newProjectUnsetCommand(extCtx)) | ||
| cmd.AddCommand(newProjectShowCommand(extCtx)) | ||
|
|
||
| return cmd | ||
| } |
101 changes: 101 additions & 0 deletions
101
cli/azd/extensions/azure.ai.agents/internal/cmd/project_context_store.go
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,101 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/pkg/azdext" | ||
| ) | ||
|
|
||
| // projectContextConfigPath is the UserConfig path for the persisted project context. | ||
| const projectContextConfigPath = configPathPrefix + ".context" | ||
|
huimiu marked this conversation as resolved.
Outdated
|
||
|
|
||
| // projectContextState is the JSON shape stored at extensions.ai-agents.context | ||
| // in ~/.azd/config.json. | ||
| type projectContextState struct { | ||
| Endpoint string `json:"endpoint"` | ||
| SetAt string `json:"setAt"` | ||
| } | ||
|
|
||
| // getProjectContext reads the persisted project context from global config. | ||
| // Returns (state, true, nil) when present, (zero, false, nil) when absent. | ||
| func getProjectContext( | ||
| ctx context.Context, | ||
| azdClient *azdext.AzdClient, | ||
| ) (projectContextState, bool, error) { | ||
| ch, err := azdext.NewConfigHelper(azdClient) | ||
| if err != nil { | ||
| return projectContextState{}, false, fmt.Errorf("getProjectContext: %w", err) | ||
| } | ||
|
|
||
| var state projectContextState | ||
| found, err := ch.GetUserJSON(ctx, projectContextConfigPath, &state) | ||
| if err != nil { | ||
| return projectContextState{}, false, | ||
| fmt.Errorf("getProjectContext: failed to read config: %w", err) | ||
| } | ||
|
|
||
| if !found || state.Endpoint == "" { | ||
| return projectContextState{}, false, nil | ||
| } | ||
|
|
||
| return state, true, nil | ||
| } | ||
|
|
||
| // setProjectContext persists a validated project endpoint to global config. | ||
| // The caller is responsible for validating the endpoint before calling this function. | ||
| // Returns the setAt timestamp that was written to config. | ||
| func setProjectContext( | ||
| ctx context.Context, | ||
| azdClient *azdext.AzdClient, | ||
| endpoint string, | ||
| ) (setAt string, err error) { | ||
| ch, chErr := azdext.NewConfigHelper(azdClient) | ||
| if chErr != nil { | ||
| return "", fmt.Errorf("setProjectContext: %w", chErr) | ||
| } | ||
|
|
||
| state := projectContextState{ | ||
| Endpoint: endpoint, | ||
| SetAt: time.Now().UTC().Format(time.RFC3339), | ||
| } | ||
|
|
||
| if err := ch.SetUserJSON(ctx, projectContextConfigPath, state); err != nil { | ||
| return "", fmt.Errorf("setProjectContext: failed to write config: %w", err) | ||
| } | ||
|
|
||
| return state.SetAt, nil | ||
| } | ||
|
|
||
| // clearProjectContext removes the context subtree from global config. | ||
| // Returns the previously stored endpoint (empty if none was set). | ||
| // The operation is idempotent — calling it when no context is set is not an error. | ||
| func clearProjectContext( | ||
| ctx context.Context, | ||
| azdClient *azdext.AzdClient, | ||
| ) (previousEndpoint string, err error) { | ||
| // Read existing state first so we can return the previous endpoint. | ||
| state, found, err := getProjectContext(ctx, azdClient) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| if found { | ||
| previousEndpoint = state.Endpoint | ||
| } | ||
|
|
||
| ch, chErr := azdext.NewConfigHelper(azdClient) | ||
| if chErr != nil { | ||
| return "", fmt.Errorf("clearProjectContext: %w", chErr) | ||
| } | ||
|
|
||
| if err := ch.UnsetUser(ctx, projectContextConfigPath); err != nil { | ||
| return "", fmt.Errorf("clearProjectContext: failed to clear config: %w", err) | ||
| } | ||
|
|
||
| return previousEndpoint, nil | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.