Skip to content

Commit cd63043

Browse files
committed
Make registering views for individual agents easier
1 parent a1b27a3 commit cd63043

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

docs/modules/agents/index.md

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class MyAppAppConfig(AppConfig):
5757

5858
## Configuring Agent URLs
5959

60-
To make your agents accessible at automatically generated URLs, add `agent_urls()` to your `urlpatterns`:
60+
Agents can be made accessible at automatically generated URLs by adding `agent_urls()` to your `urlpatterns`:
6161

6262
```python
6363
from django.urls import path, include
@@ -71,6 +71,18 @@ urlpatterns = [
7171

7272
This will generate URLs under `ai/` for all your registered agents based on their `slug` values. The `SimplePromptAgent` from the above example will be accessible at `ai/prompt/`.
7373

74+
Alternatively, you can register views for individual agents using the `as_view` classmethod on Agent classes:
75+
76+
````python
77+
78+
from django.urls import path, include
79+
from .agents import SimplePromptAgent
80+
81+
urlpatterns = [
82+
...
83+
path("ai/simple/", SimplePromptAgent.as_view()),
84+
]
85+
7486
## Using Agents
7587

7688
Agents can either be invoked directly:
@@ -79,7 +91,7 @@ Agents can either be invoked directly:
7991
from .agents import SimplePromptAgent
8092

8193
SimplePromptAgent().execute(prompt="Foo")
82-
```
94+
````
8395

8496
or via their URL:
8597

@@ -92,3 +104,25 @@ POST https://www.example.com/ai/prompt/
92104
}
93105
}
94106
```
107+
108+
## Permissions
109+
110+
You can control who can execute your agents using permission. See the [Permissions](./permissions) documentation for detailed information on how to secure your agents.
111+
112+
Quick example:
113+
114+
```python
115+
116+
from django_ai_core.contrib.agents import Agent
117+
from django_ai_core.contrib.agents.permissions import IsAuthenticated
118+
119+
@registry.register()
120+
class SecureAgent(Agent):
121+
slug = "secure"
122+
description = "Agent requiring authentication"
123+
permission = IsAuthenticated()
124+
parameters = []
125+
126+
def execute(self):
127+
return "Authenticated access only!"
128+
```

src/django_ai_core/contrib/agents/base.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.core.exceptions import ValidationError
88

99
from .permissions import BasePermission
10+
from .views import AgentExecutionView
1011

1112

1213
@dataclass
@@ -31,6 +32,10 @@ class Agent(ABC):
3132
description: str
3233
parameters: list[AgentParameter] | None
3334

35+
@classmethod
36+
def as_view(cls):
37+
return AgentExecutionView.as_view(agent_slug=cls.slug)
38+
3439
@classmethod
3540
def _derive_parameters_from_signature(cls) -> list[AgentParameter]:
3641
"""Derive parameters from `execute` type signature"""

src/django_ai_core/contrib/agents/urls.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from django.urls import path
22

33
from . import registry
4-
from .views import AgentExecutionView
54

65

76
def agent_urls() -> list:
@@ -22,12 +21,12 @@ def agent_urls() -> list:
2221
"""
2322
urlpatterns = []
2423

25-
for agent_slug in registry.list().keys():
24+
for agent in registry.list().values():
2625
urlpatterns.append(
2726
path(
28-
f"{agent_slug}/",
29-
AgentExecutionView.as_view(agent_slug=agent_slug),
30-
name=f"agent_{agent_slug}",
27+
f"{agent.slug}/",
28+
agent.as_view(),
29+
name=f"agent_{agent.slug}",
3130
)
3231
)
3332

src/django_ai_core/contrib/agents/views.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
from typing import Any
1+
from typing import Any, TYPE_CHECKING
22
import json
33

44
from django.http import JsonResponse
55
from django.views import View
66
from django.views.decorators.csrf import csrf_exempt
77
from django.utils.decorators import method_decorator
88

9-
from . import registry, Agent
109
from .permissions import AllowAny
1110

11+
if TYPE_CHECKING:
12+
from . import Agent
13+
1214

1315
class AgentExecutionException(Exception):
1416
pass
@@ -90,11 +92,13 @@ def post(self, request):
9092

9193
return JsonResponse({"status": "completed", "data": result})
9294

93-
def _get_agent(self) -> Agent:
95+
def _get_agent(self) -> "Agent":
96+
from . import registry
97+
9498
try:
9599
return registry.get(self.agent_slug)()
96100
except KeyError:
97101
raise AgentNotFound
98102

99-
def _execute_agent(self, agent: Agent, arguments: dict[str, Any]) -> Any:
103+
def _execute_agent(self, agent: "Agent", arguments: dict[str, Any]) -> Any:
100104
return agent.execute(**arguments)

0 commit comments

Comments
 (0)