Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion agents/external/src/hyperforge_external/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ async def __call__(
request = client.build_request(
method=self.config.method.value,
url=self.config.url,
json=memory.contexts,
json=[
context.model_dump(mode="json") for context in memory.contexts
],
headers=self.config.headers,
)
else:
Expand Down
55 changes: 53 additions & 2 deletions agents/external/tests/test_external.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import json
import os
from unittest.mock import AsyncMock
from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock

import httpx
import pytest
from hyperforge.engine import main as arag_main
from hyperforge.minimal_fixtures import cassette_nua_key
from hyperforge.models import Context

from hyperforge_external.agent import ExternalCallAgent
from hyperforge_external.config import ExternalCallAgentConfig

NUA_KEY = os.environ.get(
"NUA_KEY",
Expand Down Expand Up @@ -66,7 +72,7 @@ async def test_external(mocker):
mock_client.__aexit__ = AsyncMock(return_value=None)

mocker.patch(
"hyperforge.utils.http.safe_http_client",
"hyperforge_external.agent.safe_http_client",
return_value=mock_client,
)

Expand All @@ -89,3 +95,48 @@ async def test_external(mocker):
or "Peugeot" in question_memory.steps[-2].reason
)
assert mock_client.send.called


@pytest.mark.asyncio
async def test_external_with_context_payload(mocker):
config = ExternalCallAgentConfig(
context=True,
url="https://example.com/aaa",
)
external_agent = ExternalCallAgent(config)
memory = SimpleNamespace(
original_question="Question",
final_answer="Answer",
contexts=[
Context(
original_question_uuid=None,
actual_question_uuid=None,
question="Question",
source="static",
agent="static",
)
],
contexts_minimal=lambda: "Context",
add_step=AsyncMock(),
)

mock_response = httpx.Response(
200,
content=b"oki doki",
request=httpx.Request("POST", config.url),
)
mock_client = MagicMock()
mock_client.build_request.side_effect = httpx.Request
mock_client.send = AsyncMock(return_value=mock_response)
mock_client.__aenter__ = AsyncMock(return_value=mock_client)
mock_client.__aexit__ = AsyncMock(return_value=None)
mocker.patch(
"hyperforge_external.agent.safe_http_client",
return_value=mock_client,
)

await external_agent(memory, manager=None)

request = mock_client.send.call_args.args[0]
assert request.headers["content-type"] == "application/json"
assert json.loads(request.content)[0]["source"] == "static"
Loading