-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Python: Surface oauth_consent_request events from Responses API in Foundry clients
#5070
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 all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
425c176
Fix Foundry clients not surfacing oauth_consent_request events (#5054)
cca1aff
Address PR review feedback for #5054 OAuth consent parsing
7d77e4c
Handle response.oauth_consent_requested top-level event (#5054)
a544ca9
Address review feedback for #5054: Python: [Bug]: `FoundryAgent` (Res…
5393ced
Address review feedback: defensive getattr and dedicated helper tests…
866b3e5
Address review feedback for #5054: Python: [Bug]: `FoundryAgent` (Res…
8ed6e9a
Merge branch 'main' into agent/fix-5054-1
giles17 8eeaae2
Fix mypy: match _parse_chunk_from_openai signature with superclass
819cc6e
Merge remote-tracking branch 'upstream/main' into agent/fix-5054-1
moonbox3 acdcb2d
Merge remote-tracking branch 'upstream/main' into agent/fix-5054-1
moonbox3 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
75 changes: 75 additions & 0 deletions
75
python/packages/foundry/agent_framework_foundry/_oauth_helpers.py
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,75 @@ | ||
| # Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
| from typing import Any | ||
| from urllib.parse import urlparse | ||
|
|
||
| from agent_framework import ChatResponseUpdate, Content | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _validate_consent_link(consent_link: str, item_id: str) -> str: | ||
| """Validate a consent link is HTTPS with a valid netloc. | ||
|
|
||
| Returns the link unchanged if valid, or an empty string if not. | ||
| """ | ||
| parsed = urlparse(consent_link) | ||
| if parsed.scheme.lower() != "https" or not parsed.netloc: | ||
| logger.warning( | ||
| "Skipping oauth_consent_request with non-HTTPS consent_link (item id=%s)", | ||
| item_id, | ||
| ) | ||
| return "" | ||
| return consent_link | ||
|
|
||
|
|
||
| def try_parse_oauth_consent_event(event: Any, model: str) -> ChatResponseUpdate | None: | ||
| """Parse an oauth_consent_request from a streaming event, if present. | ||
|
|
||
| Returns a ``ChatResponseUpdate`` when *event* is a | ||
| ``response.output_item.added`` carrying an ``oauth_consent_request`` item | ||
| or a top-level ``response.oauth_consent_requested`` event, | ||
| or ``None`` so the caller can fall through to the base implementation. | ||
| """ | ||
| consent_link: str = "" | ||
| raw_item: Any = None | ||
|
|
||
| event_type = getattr(event, "type", None) | ||
|
|
||
| if event_type == "response.output_item.added" and getattr(event.item, "type", None) == "oauth_consent_request": | ||
| raw_item = event.item | ||
| consent_link = getattr(raw_item, "consent_link", None) or "" | ||
| elif event_type == "response.oauth_consent_requested": | ||
| raw_item = event | ||
| consent_link = getattr(event, "consent_link", None) or "" | ||
| else: | ||
| return None | ||
|
|
||
| item_id = getattr(raw_item, "id", "<unknown>") | ||
|
|
||
| if consent_link: | ||
| consent_link = _validate_consent_link(consent_link, item_id) | ||
|
|
||
| contents: list[Content] = [] | ||
| if consent_link: | ||
| contents.append( | ||
| Content.from_oauth_consent_request( | ||
| consent_link=consent_link, | ||
| raw_representation=raw_item, | ||
| ) | ||
| ) | ||
| else: | ||
| logger.warning( | ||
| "Received oauth_consent_request output without valid consent_link (item id=%s)", | ||
| item_id, | ||
| ) | ||
|
|
||
| return ChatResponseUpdate( | ||
| contents=contents, | ||
| role="assistant", | ||
| model=model, | ||
| raw_representation=event, | ||
| ) |
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
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.