-
Notifications
You must be signed in to change notification settings - Fork 306
feat(routines): implement azd ai routine commands #8241
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
Draft
huimiu
wants to merge
11
commits into
main
Choose a base branch
from
hui/add-routine-command
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.
Draft
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ec5f1e6
feat(routines): implement azd ai routine commands
huimiu 520ab32
test(routines): add unit tests for endpoint, create, manifest, and mo…
huimiu e092aa5
test(routines): align test patterns with azure.ai.agents extension
huimiu b9122b6
fix(routines): address CI lint and spell-check failures
huimiu 32dcd78
fix(routines): remove unused ptrBool helper
huimiu f7d1284
docs(routines): remove design spec from PR
huimiu 8e0d723
fix(routines): close response bodies per page and preserve filter on …
huimiu b7d375b
fix(routines): flatten command tree to remove duplicate 'routine rout…
huimiu 02fb138
fix(routines): align data-plane client with Foundry Routines TypeSpec
huimiu 4a9cbe3
fix(routines): clarify comment for github_issue fields in RoutineTrigger
huimiu 1ff5487
fix(routines): address PR review feedback
huimiu 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 |
|---|---|---|
| @@ -1,2 +1,6 @@ | ||
| import: ../../.vscode/cspell.yaml | ||
| words: [] | ||
| words: | ||
| - exterrors | ||
| - sess | ||
| - routineName | ||
| - azdProjectSources |
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
226 changes: 226 additions & 0 deletions
226
cli/azd/extensions/azure.ai.routines/internal/cmd/endpoint.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,226 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/url" | ||
| "os" | ||
| "strings" | ||
|
|
||
| "azure.ai.routines/internal/exterrors" | ||
|
|
||
| "github.com/azure/azure-dev/cli/azd/pkg/azdext" | ||
| ) | ||
|
|
||
| // EndpointSource identifies where the resolved project endpoint came from. | ||
| type EndpointSource string | ||
|
|
||
| const ( | ||
| // SourceFlag means the endpoint came from the -p / --project-endpoint flag. | ||
| SourceFlag EndpointSource = "flag" | ||
| // SourceAzdEnv means the endpoint came from the active azd environment's AZURE_AI_PROJECT_ENDPOINT. | ||
| SourceAzdEnv EndpointSource = "azdEnv" | ||
| // SourceGlobalConfig means the endpoint came from ~/.azd/config.json. | ||
| SourceGlobalConfig EndpointSource = "globalConfig" | ||
| // SourceFoundryEnv means the endpoint came from the FOUNDRY_PROJECT_ENDPOINT env var. | ||
| SourceFoundryEnv EndpointSource = "foundryEnv" | ||
| ) | ||
|
|
||
| // foundryHostSuffixes lists the accepted Foundry host suffixes. | ||
| var foundryHostSuffixes = []string{ | ||
| ".services.ai.azure.com", | ||
| } | ||
|
|
||
| // projectContextConfigPath is the global config path for the persisted project context. | ||
| // Matches the azure.ai.agents extension for cross-extension compatibility. | ||
| const projectContextConfigPath = "extensions.ai-agents.project.context" | ||
|
|
||
| // isFoundryHost reports whether the hostname ends with a recognized Foundry suffix. | ||
| func isFoundryHost(hostname string) bool { | ||
| h := strings.ToLower(hostname) | ||
| for _, suffix := range foundryHostSuffixes { | ||
| if strings.HasSuffix(h, suffix) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // validateProjectEndpoint validates and normalizes a Foundry project endpoint URL. | ||
| func validateProjectEndpoint(raw string) (normalized string, err error) { | ||
| raw = strings.TrimSpace(raw) | ||
| if raw == "" { | ||
| return "", exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| "project endpoint must not be empty", | ||
| "provide a Foundry project endpoint URL "+ | ||
| "(e.g. https://<account>.services.ai.azure.com/api/projects/<project>)", | ||
| ) | ||
| } | ||
|
|
||
| u, parseErr := url.Parse(raw) | ||
| if parseErr != nil { | ||
| return "", exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| fmt.Sprintf("invalid project endpoint URL: %v", parseErr), | ||
| "provide a valid https:// Foundry project endpoint URL", | ||
| ) | ||
| } | ||
|
|
||
| if !strings.EqualFold(u.Scheme, "https") { | ||
| return "", exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| "project endpoint must use https", | ||
| "provide an https:// URL", | ||
| ) | ||
| } | ||
|
|
||
| // Reject explicit ports: Foundry hosts are reached on the default HTTPS | ||
| // port (443) and accepting other ports would silently misroute traffic | ||
| // (the normalized URL strips the port). | ||
| if u.Port() != "" { | ||
| return "", exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| "project endpoint must not contain an explicit port", | ||
| "remove the port from the URL (Foundry uses the default HTTPS port 443)", | ||
| ) | ||
| } | ||
|
|
||
| host := u.Hostname() | ||
| if host == "" || !isFoundryHost(host) { | ||
| return "", exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| fmt.Sprintf("project endpoint host %q is not a recognized Foundry host (*%s)", | ||
| host, foundryHostSuffixes[0]), | ||
| "the host must end with "+foundryHostSuffixes[0], | ||
| ) | ||
| } | ||
|
|
||
| // Normalize: lowercase host, strip trailing slash. | ||
| path := strings.TrimRight(u.EscapedPath(), "/") | ||
| normalized = fmt.Sprintf("https://%s%s", strings.ToLower(host), path) | ||
| return normalized, nil | ||
| } | ||
|
|
||
| // resolvedEndpoint holds the result of resolveProjectEndpoint. | ||
| type resolvedEndpoint struct { | ||
| Endpoint string | ||
| Source EndpointSource | ||
| } | ||
|
|
||
| // azdProjectSources holds the values read from azd-managed sources (levels 2 and 3). | ||
| type azdProjectSources struct { | ||
| // EnvValue is the AZURE_AI_PROJECT_ENDPOINT from the active azd env, or "". | ||
| EnvValue string | ||
| // EnvName is the active azd env name. Only meaningful when EnvValue != "". | ||
| EnvName string | ||
| // CfgEndpoint is the endpoint persisted in global config, or "". | ||
| CfgEndpoint string | ||
| // CfgFound is true when the global config path was found and had a non-empty endpoint. | ||
| CfgFound bool | ||
| } | ||
|
|
||
| // readAzdProjectSourcesFunc is a package-level seam so tests can stub the | ||
| // daemon-backed lookup without spinning up a real azd gRPC server. | ||
| var readAzdProjectSourcesFunc = readAzdProjectSources | ||
|
|
||
| // readAzdProjectSources dials the azd daemon (if reachable) and reads the | ||
| // active env's AZURE_AI_PROJECT_ENDPOINT and the global-config project | ||
| // endpoint in a single client lifetime. Errors talking to the daemon are | ||
| // silently ignored (treated as "no daemon"); the caller falls through to | ||
| // the FOUNDRY_PROJECT_ENDPOINT host env var. | ||
| func readAzdProjectSources(ctx context.Context) (azdProjectSources, error) { | ||
| var out azdProjectSources | ||
|
|
||
| azdClient, err := azdext.NewAzdClient() | ||
| if err != nil { | ||
| return out, nil | ||
| } | ||
| defer azdClient.Close() | ||
|
|
||
| // Level 2: active azd env → AZURE_AI_PROJECT_ENDPOINT. | ||
| if envResp, err := azdClient.Environment().GetCurrent(ctx, &azdext.EmptyRequest{}); err == nil { | ||
| if valResp, err := azdClient.Environment().GetValue(ctx, &azdext.GetEnvRequest{ | ||
| EnvName: envResp.Environment.Name, | ||
| Key: "AZURE_AI_PROJECT_ENDPOINT", | ||
| }); err == nil && valResp.Value != "" { | ||
| out.EnvValue = valResp.Value | ||
| out.EnvName = envResp.Environment.Name | ||
| } | ||
| } | ||
|
|
||
| // Level 3: global config → extensions.ai-agents.project.context.endpoint. | ||
| ch, cfgErr := azdext.NewConfigHelper(azdClient) | ||
| if cfgErr == nil { | ||
| var state struct { | ||
| Endpoint string `json:"endpoint"` | ||
| } | ||
| if found, err := ch.GetUserJSON(ctx, projectContextConfigPath, &state); err == nil && found && state.Endpoint != "" { | ||
| out.CfgEndpoint = state.Endpoint | ||
| out.CfgFound = true | ||
| } | ||
| } | ||
|
|
||
| return out, nil | ||
| } | ||
|
|
||
| // resolveProjectEndpoint implements the 5-level cascade: | ||
| // | ||
| // 1. -p / --project-endpoint flag | ||
| // 2. Active azd env → AZURE_AI_PROJECT_ENDPOINT | ||
| // 3. Global config → extensions.ai-agents.project.context.endpoint | ||
| // 4. FOUNDRY_PROJECT_ENDPOINT environment variable | ||
| // 5. Structured dependency error | ||
| func resolveProjectEndpoint(ctx context.Context, flagValue string) (*resolvedEndpoint, error) { | ||
| // Level 1: explicit flag. | ||
| if flagValue != "" { | ||
| normalized, err := validateProjectEndpoint(flagValue) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &resolvedEndpoint{Endpoint: normalized, Source: SourceFlag}, nil | ||
| } | ||
|
|
||
| // Levels 2 & 3: azd daemon sources (replaceable seam for testing). | ||
| sources, err := readAzdProjectSourcesFunc(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if sources.EnvValue != "" { | ||
| normalized, err := validateProjectEndpoint(sources.EnvValue) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &resolvedEndpoint{Endpoint: normalized, Source: SourceAzdEnv}, nil | ||
| } | ||
|
|
||
| if sources.CfgFound && sources.CfgEndpoint != "" { | ||
| normalized, err := validateProjectEndpoint(sources.CfgEndpoint) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &resolvedEndpoint{Endpoint: normalized, Source: SourceGlobalConfig}, nil | ||
| } | ||
|
|
||
| // Level 4: FOUNDRY_PROJECT_ENDPOINT env var. | ||
| if ep := os.Getenv("FOUNDRY_PROJECT_ENDPOINT"); ep != "" { | ||
| normalized, err := validateProjectEndpoint(ep) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &resolvedEndpoint{Endpoint: normalized, Source: SourceFoundryEnv}, nil | ||
| } | ||
|
|
||
| // Level 5: structured error. | ||
| return nil, exterrors.Dependency( | ||
| exterrors.CodeMissingProjectEndpoint, | ||
| "no Foundry project endpoint resolved", | ||
| "pass -p / --project-endpoint, run 'azd ai agent project set <endpoint>', "+ | ||
| "set AZURE_AI_PROJECT_ENDPOINT in the active azd environment, "+ | ||
| "or export FOUNDRY_PROJECT_ENDPOINT in your shell", | ||
| ) | ||
| } | ||
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.