|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | import logging |
6 | | -from typing import Any |
| 6 | +from collections.abc import AsyncIterable, Awaitable |
| 7 | +from typing import Any, Literal, overload |
7 | 8 | from unittest.mock import MagicMock |
8 | 9 |
|
9 | 10 | import pytest |
10 | | - |
11 | | -from agent_framework_foundry._oauth_helpers import _validate_consent_link, try_parse_oauth_consent_event |
| 11 | +from agent_framework import ( |
| 12 | + AgentExecutor, |
| 13 | + AgentResponse, |
| 14 | + AgentResponseUpdate, |
| 15 | + AgentRunInputs, |
| 16 | + AgentSession, |
| 17 | + BaseAgent, |
| 18 | + Content, |
| 19 | + Message, |
| 20 | + ResponseStream, |
| 21 | + WorkflowBuilder, |
| 22 | +) |
| 23 | + |
| 24 | +from agent_framework_foundry._oauth_helpers import ( |
| 25 | + _validate_consent_link, |
| 26 | + parse_oauth_consent_output_items, |
| 27 | + try_parse_oauth_consent_event, |
| 28 | +) |
12 | 29 |
|
13 | 30 | # region _validate_consent_link tests |
14 | 31 |
|
@@ -162,3 +179,121 @@ def test_empty_contents_for_https_empty_netloc(caplog: pytest.LogCaptureFixture) |
162 | 179 |
|
163 | 180 |
|
164 | 181 | # endregion |
| 182 | + |
| 183 | + |
| 184 | +# region consent request id |
| 185 | + |
| 186 | + |
| 187 | +def _make_output_item(*, item_id: Any = "oauth-item-1", consent_link: str = "https://consent.example.com/auth") -> Any: |
| 188 | + """Create a mock non-streaming ``oauth_consent_request`` output item.""" |
| 189 | + item = MagicMock() |
| 190 | + item.type = "oauth_consent_request" |
| 191 | + item.consent_link = consent_link |
| 192 | + item.id = item_id |
| 193 | + return item |
| 194 | + |
| 195 | + |
| 196 | +def test_output_item_added_keeps_provider_item_id() -> None: |
| 197 | + """The consent content carries the provider item id, as user-input requests need one.""" |
| 198 | + update = try_parse_oauth_consent_event(_make_output_item_event(item_id="oauth-item-42"), "test-model") |
| 199 | + |
| 200 | + assert update is not None |
| 201 | + assert [c.id for c in update.contents if c.type == "oauth_consent_request"] == ["oauth-item-42"] |
| 202 | + |
| 203 | + |
| 204 | +def test_top_level_consent_requested_event_keeps_event_id() -> None: |
| 205 | + """A top-level ``response.oauth_consent_requested`` event keeps its id on the content.""" |
| 206 | + update = try_parse_oauth_consent_event(_make_top_level_event(event_id="consent-event-7"), "test-model") |
| 207 | + |
| 208 | + assert update is not None |
| 209 | + assert [c.id for c in update.contents if c.type == "oauth_consent_request"] == ["consent-event-7"] |
| 210 | + |
| 211 | + |
| 212 | +def test_non_streaming_output_item_keeps_provider_item_id() -> None: |
| 213 | + """Non-streaming output items keep the provider item id on the consent content.""" |
| 214 | + contents = parse_oauth_consent_output_items([_make_output_item(item_id="oauth-item-9")]) |
| 215 | + |
| 216 | + assert len(contents) == 1 |
| 217 | + assert contents[0].type == "oauth_consent_request" |
| 218 | + assert contents[0].user_input_request is True |
| 219 | + assert contents[0].id == "oauth-item-9" |
| 220 | + |
| 221 | + |
| 222 | +def test_consent_content_without_string_id_has_no_id() -> None: |
| 223 | + """A provider item without a usable id does not get a made-up one.""" |
| 224 | + contents = parse_oauth_consent_output_items([_make_output_item(item_id=None)]) |
| 225 | + |
| 226 | + assert len(contents) == 1 |
| 227 | + assert contents[0].id is None |
| 228 | + |
| 229 | + |
| 230 | +class _ConsentEmittingAgent(BaseAgent): |
| 231 | + """Agent that returns the consent content parsed from a Foundry output item.""" |
| 232 | + |
| 233 | + def __init__(self, *, item_id: str, **kwargs: Any) -> None: |
| 234 | + super().__init__(**kwargs) |
| 235 | + self._item_id = item_id |
| 236 | + |
| 237 | + def _consent_contents(self) -> list[Content]: |
| 238 | + return parse_oauth_consent_output_items([_make_output_item(item_id=self._item_id)]) |
| 239 | + |
| 240 | + @overload |
| 241 | + def run( |
| 242 | + self, |
| 243 | + messages: AgentRunInputs | None = ..., |
| 244 | + *, |
| 245 | + stream: Literal[False] = ..., |
| 246 | + session: AgentSession | None = ..., |
| 247 | + **kwargs: Any, |
| 248 | + ) -> Awaitable[AgentResponse[Any]]: ... |
| 249 | + |
| 250 | + @overload |
| 251 | + def run( |
| 252 | + self, |
| 253 | + messages: AgentRunInputs | None = ..., |
| 254 | + *, |
| 255 | + stream: Literal[True], |
| 256 | + session: AgentSession | None = ..., |
| 257 | + **kwargs: Any, |
| 258 | + ) -> ResponseStream[AgentResponseUpdate, AgentResponse[Any]]: ... |
| 259 | + |
| 260 | + def run( |
| 261 | + self, |
| 262 | + messages: AgentRunInputs | None = None, |
| 263 | + *, |
| 264 | + stream: bool = False, |
| 265 | + session: AgentSession | None = None, |
| 266 | + **kwargs: Any, |
| 267 | + ) -> Awaitable[AgentResponse[Any]] | ResponseStream[AgentResponseUpdate, AgentResponse[Any]]: |
| 268 | + contents = self._consent_contents() |
| 269 | + |
| 270 | + if stream: |
| 271 | + |
| 272 | + async def _stream() -> AsyncIterable[AgentResponseUpdate]: |
| 273 | + yield AgentResponseUpdate(contents=contents, role="assistant") |
| 274 | + |
| 275 | + return ResponseStream(_stream(), finalizer=AgentResponse.from_updates) |
| 276 | + |
| 277 | + async def _run() -> AgentResponse: |
| 278 | + return AgentResponse(messages=[Message("assistant", contents)]) |
| 279 | + |
| 280 | + return _run() |
| 281 | + |
| 282 | + |
| 283 | +@pytest.mark.parametrize("stream", [False, True]) |
| 284 | +async def test_workflow_pauses_for_oauth_consent_request(stream: bool) -> None: |
| 285 | + """In a workflow, the consent request becomes a ``request_info`` event keyed by the provider item id.""" |
| 286 | + agent = _ConsentEmittingAgent(item_id="oauth-item-wf", id="consent_agent", name="ConsentAgent") |
| 287 | + workflow = WorkflowBuilder(start_executor=AgentExecutor(agent, id="consent_exec")).build() |
| 288 | + |
| 289 | + if stream: |
| 290 | + events = [event async for event in workflow.run("connect my calendar", stream=True)] |
| 291 | + else: |
| 292 | + events = list(await workflow.run("connect my calendar")) |
| 293 | + |
| 294 | + request_info_events = [event for event in events if event.type == "request_info"] |
| 295 | + assert len(request_info_events) == 1 |
| 296 | + assert request_info_events[0].request_id == "oauth-item-wf" |
| 297 | + |
| 298 | + |
| 299 | +# endregion |
0 commit comments