-
Notifications
You must be signed in to change notification settings - Fork 305
feat(ai-agents): add --agent-endpoint flag to invoke command #8028
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
trangevi
merged 11 commits into
Azure:main
from
antriksh30:antriksh30/invoke-agent-endpoint-url-fix
May 6, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3e086fd
ai agents: --agent-endpoint flag for ephemeral remote invokes
f3009f7
ai agents: address review feedback on --agent-endpoint
c22c183
ai agents: address multi-model code review findings
81652a0
ai agents: enable global-config persistence for --agent-endpoint
084cbf7
ai agents: replace ephemeral hint fallback with help-pointer tip
8f9456c
azure.ai.agents: protocol-aware reset hint for invoke
42af9d1
azure.ai.agents: address PR review findings on --agent-endpoint
3d560e0
ai agents: replace buildEphemeralAgentKey with buildAgentKey
2b95ecc
ai agents: fix --agent-endpoint help-text example to use responses URL
a360825
ai agents: remove warnIneffectiveResetFlags (dead in normal use)
20d23e4
ai agents: address wbreza PR review nits + add resolveRemoteContext test
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
186 changes: 186 additions & 0 deletions
186
cli/azd/extensions/azure.ai.agents/internal/cmd/agent_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,186 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/url" | ||
| "regexp" | ||
| "strings" | ||
|
|
||
| "azureaiagent/internal/exterrors" | ||
| "azureaiagent/internal/pkg/agents/agent_api" | ||
| "azureaiagent/internal/pkg/agents/agent_yaml" | ||
| ) | ||
|
|
||
| // agentEndpointHostSuffix is the required Foundry host suffix for endpoint URLs. | ||
| const agentEndpointHostSuffix = ".services.ai.azure.com" | ||
|
|
||
| // agentEndpointHint is the suggestion appended to most --agent-endpoint validation errors. | ||
| // `azd ai agent show` persistently prints the agent endpoint URL, so it's the right | ||
| // thing to point users at any time after a deploy. | ||
| const agentEndpointHint = "run `azd ai agent show` to see the agent endpoint URL" | ||
|
|
||
| // agentEndpointPathRegex matches the full Foundry agent-endpoint path. Captures: | ||
| // | ||
| // [1] project name (URL-escaped), | ||
| // [2] agent name (URL-escaped), | ||
| // [3] protocol tail ("invocations" or "openai/responses"). | ||
| var agentEndpointPathRegex = regexp.MustCompile( | ||
| `^/api/projects/([^/]+)/agents/([^/]+)/endpoint/protocols/(invocations|openai/responses)/?$`, | ||
| ) | ||
|
|
||
| // parsedAgentEndpoint describes a deployed agent invocation endpoint. | ||
| type parsedAgentEndpoint struct { | ||
| // ProjectEndpoint is the Foundry project root: https://<acct>.services.ai.azure.com/api/projects/<proj>. | ||
| ProjectEndpoint string | ||
| AgentName string | ||
| Protocol agent_api.AgentProtocol | ||
| // APIVersion is the api-version query parameter from the URL, or empty if absent. | ||
| APIVersion string | ||
| } | ||
|
|
||
| // parseAgentEndpoint parses the full agent invocation URL printed by `azd ai agent show`. | ||
| // | ||
| // Accepted shapes: | ||
| // | ||
| // https://<acct>.services.ai.azure.com/api/projects/<proj>/agents/<name>/endpoint/protocols/invocations[?api-version=…] | ||
| // https://<acct>.services.ai.azure.com/api/projects/<proj>/agents/<name>/endpoint/protocols/openai/responses[?api-version=…] | ||
| // | ||
| // The host must be a `*.services.ai.azure.com` Foundry host. The path must include the | ||
| // protocol-specific suffix; the protocol is derived from the URL. | ||
| func parseAgentEndpoint(rawURL string) (*parsedAgentEndpoint, error) { | ||
| if strings.TrimSpace(rawURL) == "" { | ||
| return nil, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| "--agent-endpoint requires a non-empty URL", | ||
| agentEndpointHint, | ||
| ) | ||
| } | ||
|
|
||
| u, err := url.Parse(rawURL) | ||
| if err != nil { | ||
| return nil, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| fmt.Sprintf("invalid --agent-endpoint URL: %v", err), | ||
| agentEndpointHint, | ||
| ) | ||
| } | ||
|
|
||
| if !strings.EqualFold(u.Scheme, "https") { | ||
| return nil, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| "--agent-endpoint must use https", | ||
| agentEndpointHint, | ||
| ) | ||
| } | ||
|
|
||
| host := strings.ToLower(u.Hostname()) | ||
| if host == "" || !strings.HasSuffix(host, agentEndpointHostSuffix) { | ||
| return nil, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| fmt.Sprintf("--agent-endpoint host %q is not a Foundry host (*%s)", u.Hostname(), agentEndpointHostSuffix), | ||
| agentEndpointHint, | ||
| ) | ||
| } | ||
|
|
||
| // Reject explicit ports — Foundry endpoints always use the default HTTPS port, | ||
| // and silently dropping a non-default port would route requests to a different origin. | ||
| if u.Port() != "" { | ||
| return nil, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| fmt.Sprintf("--agent-endpoint host %q must not include a port", u.Host), | ||
| agentEndpointHint+" (no explicit port)", | ||
| ) | ||
| } | ||
|
|
||
| // Match the full path against the canonical Foundry agent-endpoint shape and pull | ||
| // the project name, agent name, and protocol tail out in one pass. | ||
| matches := agentEndpointPathRegex.FindStringSubmatch(u.EscapedPath()) | ||
| if matches == nil { | ||
| return nil, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| "--agent-endpoint path must match /api/projects/<project>/agents/<name>/endpoint/protocols/<protocol>", | ||
| agentEndpointHint, | ||
| ) | ||
| } | ||
| projectSegment, agentSegment, protocolTail := matches[1], matches[2], matches[3] | ||
|
|
||
| projectName, err := url.PathUnescape(projectSegment) | ||
| if err != nil || projectName == "" || strings.ContainsAny(projectName, "/\\") { | ||
| return nil, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| "--agent-endpoint project segment is invalid", | ||
| agentEndpointHint, | ||
| ) | ||
| } | ||
|
|
||
| agentName, err := url.PathUnescape(agentSegment) | ||
| if err != nil || agent_yaml.ValidateAgentName(agentName) != nil { | ||
| return nil, exterrors.Validation( | ||
| exterrors.CodeInvalidAgentName, | ||
| fmt.Sprintf("--agent-endpoint agent name %q is invalid", agentSegment), | ||
| "agent names must start and end with an alphanumeric character, "+ | ||
| "may contain hyphens in the middle, and be 1-63 characters long", | ||
| ) | ||
|
antriksh30 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| var protocol agent_api.AgentProtocol | ||
| switch protocolTail { | ||
| case "invocations": | ||
| protocol = agent_api.AgentProtocolInvocations | ||
| case "openai/responses": | ||
| protocol = agent_api.AgentProtocolResponses | ||
| } | ||
|
|
||
| // Reject an explicit but empty api-version query parameter; the default fallback would | ||
| // otherwise silently invoke a different version than the user pasted. | ||
| apiVersion := "" | ||
| query := u.Query() | ||
| if values, present := query["api-version"]; present { | ||
| if len(values) == 0 || values[0] == "" { | ||
| return nil, exterrors.Validation( | ||
| exterrors.CodeInvalidParameter, | ||
| "--agent-endpoint api-version query parameter is empty", | ||
| "include a non-empty api-version value or omit the parameter to use the default", | ||
| ) | ||
| } | ||
| apiVersion = values[0] | ||
| } | ||
|
|
||
| projectEndpoint := fmt.Sprintf("https://%s/api/projects/%s", host, projectSegment) | ||
|
|
||
| return &parsedAgentEndpoint{ | ||
| ProjectEndpoint: projectEndpoint, | ||
| AgentName: agentName, | ||
| Protocol: protocol, | ||
| APIVersion: apiVersion, | ||
| }, nil | ||
| } | ||
|
|
||
| // buildResponsesURL builds the Foundry "openai/responses" protocol URL for an agent. | ||
| // apiVersion is URL-encoded so unusual characters cannot break out of the query value. | ||
| func buildResponsesURL(projectEndpoint, agentName, apiVersion string) string { | ||
| return fmt.Sprintf( | ||
| "%s/agents/%s/endpoint/protocols/openai/responses?api-version=%s", | ||
| projectEndpoint, agentName, url.QueryEscape(apiVersion), | ||
| ) | ||
| } | ||
|
|
||
| // buildInvocationsURL builds the Foundry "invocations" protocol URL for an agent. | ||
| // When sid is non-empty, an agent_session_id query parameter is appended (URL-encoded). | ||
| func buildInvocationsURL(projectEndpoint, agentName, apiVersion, sid string) string { | ||
| invURL := fmt.Sprintf( | ||
| "%s/agents/%s/endpoint/protocols/invocations?api-version=%s", | ||
| projectEndpoint, agentName, url.QueryEscape(apiVersion), | ||
| ) | ||
| if sid != "" { | ||
| invURL += "&agent_session_id=" + url.QueryEscape(sid) | ||
| } | ||
| return invURL | ||
| } | ||
|
|
||
| // (isValidAgentNameSegment was removed — agent name validation now delegates | ||
| // to agent_yaml.ValidateAgentName so --agent-endpoint enforces the same | ||
| // deployable-name format as the rest of the extension.) | ||
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.