Files
Stirling-PDF/engine/tests/test_stirling_api.py
James Brunton 3e94157137 Add document context for edit agent (#6152)
# Description of Changes
Adds the ability for the Edit agent to request the content of the
document before it decides which parameters it needs. This makes it able
to process requests like `Split the document after the page containing
the "My Section" section`, allowing for document context-based requests
for all[^1] tools.

I had to make a few changes elsewhere to make this work, including:
- Moving the requesting of content out of the Question Agent and into a
common location
- Added specific API docs for the Split param because the generic ones
were not specific enough for the AI to be able to reliably perform the
correct operation
- Fixed an issue in the tool models generator which caused the Redact
params to only be half-generated (causing Pydantic to crash when the AI
tried to run Redact)
- Added missing logging to a bunch of tools and hooked it up properly so
it'll print to stderr
- Made the limits for the max pages/chars to extract from PDFs
configurable via env var

[^1]: Many of the tools can't actually do anything useful with the
context at this stage, but will just need the tool API to be extended
with new features like page-specific operations to be automatically able
to do smart operations without needing to change the Edit agent itself.
2026-04-23 13:19:27 +00:00

172 lines
5.4 KiB
Python

from conftest import build_app_settings
from fastapi.testclient import TestClient
from stirling.api import app
from stirling.api.dependencies import (
get_execution_planning_agent,
get_orchestrator_agent,
get_pdf_edit_agent,
get_pdf_question_agent,
get_user_spec_agent,
)
from stirling.config import load_settings
from stirling.contracts import (
AgentDraft,
AgentDraftRequest,
AgentDraftResponse,
AgentExecutionRequest,
AgentRevisionRequest,
AgentRevisionResponse,
CannotContinueExecutionAction,
EditCannotDoResponse,
NeedContentResponse,
OrchestratorRequest,
PdfEditRequest,
PdfQuestionNotFoundResponse,
PdfQuestionRequest,
SupportedCapability,
)
from stirling.models.tool_models import Angle, RotatePdfParams
class StubOrchestratorAgent:
async def handle(self, request: OrchestratorRequest) -> NeedContentResponse:
return NeedContentResponse(
resume_with=SupportedCapability.PDF_QUESTION,
reason=request.user_message,
files=[],
max_pages=1,
max_characters=1000,
)
class StubPdfEditAgent:
async def handle(self, request: PdfEditRequest) -> EditCannotDoResponse:
return EditCannotDoResponse(reason=request.user_message)
class StubPdfQuestionAgent:
async def handle(self, request: PdfQuestionRequest) -> PdfQuestionNotFoundResponse:
return PdfQuestionNotFoundResponse(reason=request.question)
class StubUserSpecAgent:
async def draft(self, request: AgentDraftRequest) -> AgentDraftResponse:
return AgentDraftResponse(
draft=AgentDraft(
name="Drafted",
description="Route wiring test",
objective=request.user_message,
steps=[],
)
)
async def revise(self, request: AgentRevisionRequest) -> AgentRevisionResponse:
return AgentRevisionResponse(draft=request.current_draft)
class StubExecutionPlanningAgent:
async def next_action(self, request: AgentExecutionRequest) -> CannotContinueExecutionAction:
return CannotContinueExecutionAction(reason=str(request.current_step_index))
app.dependency_overrides[load_settings] = build_app_settings
app.dependency_overrides[get_orchestrator_agent] = lambda: StubOrchestratorAgent()
app.dependency_overrides[get_pdf_edit_agent] = lambda: StubPdfEditAgent()
app.dependency_overrides[get_pdf_question_agent] = lambda: StubPdfQuestionAgent()
app.dependency_overrides[get_user_spec_agent] = lambda: StubUserSpecAgent()
app.dependency_overrides[get_execution_planning_agent] = lambda: StubExecutionPlanningAgent()
client: TestClient = TestClient(app)
def test_health_route() -> None:
response = client.get("/health")
assert response.status_code == 200
assert response.json()["status"] == "ok"
def test_orchestrator_route() -> None:
response = client.post("/api/v1/orchestrator", json={"userMessage": "route this", "fileNames": ["test.pdf"]})
assert response.status_code == 200
assert response.json()["outcome"] == "need_content"
def test_pdf_edit_route() -> None:
response = client.post("/api/v1/pdf/edit", json={"userMessage": "rotate this"})
assert response.status_code == 200
assert response.json()["outcome"] == "cannot_do"
def test_pdf_questions_route() -> None:
response = client.post(
"/api/v1/pdf/questions",
json={
"question": "what is this?",
"fileNames": ["test.pdf"],
"pageText": [{"fileName": "test.pdf", "pages": [{"pageNumber": 1, "text": "Example"}]}],
},
)
assert response.status_code == 200
assert response.json()["outcome"] == "not_found"
def test_agent_draft_route() -> None:
response = client.post("/api/v1/agents/draft", json={"userMessage": "build me an agent"})
assert response.status_code == 200
assert response.json()["outcome"] == "draft"
def test_agent_revise_route() -> None:
response = client.post(
"/api/v1/agents/revise",
json={
"userMessage": "revise it",
"currentDraft": {
"name": "Drafted",
"description": "Route wiring test",
"objective": "build me an agent",
"steps": [
{
"kind": "tool",
"tool": "/api/v1/general/rotate-pdf",
"parameters": RotatePdfParams(angle=Angle(90)).model_dump(by_alias=True),
}
],
},
},
)
assert response.status_code == 200
assert response.json()["outcome"] == "draft"
def test_next_action_route() -> None:
response = client.post(
"/api/v1/agents/next-action",
json={
"agentSpec": {
"name": "Drafted",
"description": "Route wiring test",
"objective": "build me an agent",
"steps": [
{
"kind": "tool",
"tool": "/api/v1/general/rotate-pdf",
"parameters": RotatePdfParams(angle=Angle(90)).model_dump(by_alias=True),
}
],
},
"currentStepIndex": 0,
"executionContext": {"inputFiles": ["input.pdf"], "metadata": {}},
},
)
assert response.status_code == 200
assert response.json()["outcome"] == "cannot_continue"