agentforge-chat-http 0.2.3__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agentforge_chat_http/__init__.py +19 -0
- agentforge_chat_http/auth.py +30 -0
- agentforge_chat_http/manifest.yaml +23 -0
- agentforge_chat_http/py.typed +0 -0
- agentforge_chat_http/server.py +325 -0
- agentforge_chat_http-0.2.3.dist-info/METADATA +63 -0
- agentforge_chat_http-0.2.3.dist-info/RECORD +10 -0
- agentforge_chat_http-0.2.3.dist-info/WHEEL +4 -0
- agentforge_chat_http-0.2.3.dist-info/entry_points.txt +2 -0
- agentforge_chat_http-0.2.3.dist-info/licenses/LICENSE +202 -0
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"""`agentforge-chat-http` — FastAPI server for AgentForge chat (feat-020)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from agentforge_chat_http.auth import BearerAuthPolicy, EnvBearerAuth, Principal
|
|
6
|
+
from agentforge_chat_http.server import (
|
|
7
|
+
ChatServer,
|
|
8
|
+
CreateSessionRequest,
|
|
9
|
+
SendMessageRequest,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"BearerAuthPolicy",
|
|
14
|
+
"ChatServer",
|
|
15
|
+
"CreateSessionRequest",
|
|
16
|
+
"EnvBearerAuth",
|
|
17
|
+
"Principal",
|
|
18
|
+
"SendMessageRequest",
|
|
19
|
+
]
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""Bearer auth for `agentforge-chat-http`.
|
|
2
|
+
|
|
3
|
+
feat-020 v0.2 shipped its own `BearerAuthPolicy` ABC + `Principal`
|
|
4
|
+
+ `EnvBearerAuth` here. feat-014 lifted those into canonical
|
|
5
|
+
contracts in `agentforge-core` and a concrete implementation in
|
|
6
|
+
`agentforge`. This module now re-exports the canonical symbols
|
|
7
|
+
for backward compatibility:
|
|
8
|
+
|
|
9
|
+
BearerAuthPolicy — alias for `agentforge_core.contracts.auth.AuthPolicy`
|
|
10
|
+
Principal — alias for `agentforge_core.values.auth.Principal`
|
|
11
|
+
EnvBearerAuth — re-export from `agentforge.auth`
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from agentforge.auth import EnvBearerAuth
|
|
17
|
+
from agentforge_core.contracts.auth import AuthPolicy
|
|
18
|
+
from agentforge_core.values.auth import Principal
|
|
19
|
+
|
|
20
|
+
BearerAuthPolicy = AuthPolicy
|
|
21
|
+
"""Backward-compatible alias for v0.2 consumers. New code should
|
|
22
|
+
import `AuthPolicy` from `agentforge_core.contracts.auth`."""
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
__all__ = [
|
|
26
|
+
"AuthPolicy",
|
|
27
|
+
"BearerAuthPolicy",
|
|
28
|
+
"EnvBearerAuth",
|
|
29
|
+
"Principal",
|
|
30
|
+
]
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Module manifest for `agentforge add module chat-http` (feat-010).
|
|
2
|
+
name: chat-http
|
|
3
|
+
description: |
|
|
4
|
+
FastAPI HTTP + WebSocket + SSE server for AgentForge chat sessions
|
|
5
|
+
(feat-020 v0.2). Wraps `agentforge_chat.ChatSession`.
|
|
6
|
+
|
|
7
|
+
distribution:
|
|
8
|
+
pip_name: agentforge-chat-http
|
|
9
|
+
|
|
10
|
+
config_block:
|
|
11
|
+
modules.chat_http:
|
|
12
|
+
host: "0.0.0.0"
|
|
13
|
+
port: 8080
|
|
14
|
+
cors_origins: []
|
|
15
|
+
auth:
|
|
16
|
+
type: bearer
|
|
17
|
+
tokens_env: API_TOKENS
|
|
18
|
+
rate_limit:
|
|
19
|
+
per_session_per_minute: 60
|
|
20
|
+
|
|
21
|
+
entry_points:
|
|
22
|
+
agentforge.protocols:
|
|
23
|
+
chat_http: agentforge_chat_http.server:ChatServer
|
|
File without changes
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
"""`ChatServer` — FastAPI app exposing `ChatSession` over HTTP.
|
|
2
|
+
|
|
3
|
+
REST API (per feat-020 §4.1):
|
|
4
|
+
|
|
5
|
+
POST /sessions -> 200 + {id}
|
|
6
|
+
GET /sessions -> 200 + [SessionInfo]
|
|
7
|
+
DELETE /sessions/{id} -> 204
|
|
8
|
+
POST /sessions/{id}/messages -> 200 + ChatResponse (JSON)
|
|
9
|
+
or SSE when Accept matches
|
|
10
|
+
GET /sessions/{id}/messages -> 200 + [ChatTurn]
|
|
11
|
+
WS /sessions/{id}/ws -> bidirectional streaming
|
|
12
|
+
GET /healthz -> 200
|
|
13
|
+
|
|
14
|
+
v0.2 ships an in-process bearer-auth policy + token-bucket
|
|
15
|
+
rate limiting. Postgres / Redis history drivers are deferred to
|
|
16
|
+
v0.3 follow-up PRs; today the server runs against any
|
|
17
|
+
`ChatHistoryStore` instance (in-memory or sqlite).
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
from __future__ import annotations
|
|
21
|
+
|
|
22
|
+
import asyncio
|
|
23
|
+
import contextlib
|
|
24
|
+
import json
|
|
25
|
+
import time
|
|
26
|
+
import uuid
|
|
27
|
+
from collections.abc import Callable
|
|
28
|
+
from typing import Any
|
|
29
|
+
|
|
30
|
+
import uvicorn
|
|
31
|
+
from agentforge.agent import Agent
|
|
32
|
+
from agentforge_chat import ChatSession
|
|
33
|
+
from agentforge_core.contracts.chat import ChatHistoryStore, HistoryTruncationStrategy
|
|
34
|
+
from agentforge_core.values.chat import ChatChunk, ChatResponse, ChatTurn
|
|
35
|
+
from fastapi import (
|
|
36
|
+
Depends,
|
|
37
|
+
FastAPI,
|
|
38
|
+
HTTPException,
|
|
39
|
+
Request,
|
|
40
|
+
WebSocket,
|
|
41
|
+
WebSocketDisconnect,
|
|
42
|
+
status,
|
|
43
|
+
)
|
|
44
|
+
from fastapi.middleware.cors import CORSMiddleware
|
|
45
|
+
from fastapi.responses import StreamingResponse
|
|
46
|
+
from fastapi.security import HTTPAuthorizationCredentials, HTTPBearer
|
|
47
|
+
from pydantic import BaseModel, ConfigDict
|
|
48
|
+
|
|
49
|
+
from agentforge_chat_http.auth import BearerAuthPolicy, Principal
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class SendMessageRequest(BaseModel):
|
|
53
|
+
"""Body of POST /sessions/{id}/messages."""
|
|
54
|
+
|
|
55
|
+
model_config = ConfigDict(extra="forbid")
|
|
56
|
+
|
|
57
|
+
content: str
|
|
58
|
+
idempotency_key: str | None = None
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class CreateSessionRequest(BaseModel):
|
|
62
|
+
"""Body of POST /sessions."""
|
|
63
|
+
|
|
64
|
+
model_config = ConfigDict(extra="forbid")
|
|
65
|
+
|
|
66
|
+
session_id: str | None = None
|
|
67
|
+
system_prompt: str | None = None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
class _RateLimiter:
|
|
71
|
+
"""Naive in-process token bucket per (owner, session)."""
|
|
72
|
+
|
|
73
|
+
def __init__(self, *, per_minute: int) -> None:
|
|
74
|
+
self._per_minute = max(1, per_minute)
|
|
75
|
+
self._counts: dict[tuple[str, str], list[float]] = {}
|
|
76
|
+
|
|
77
|
+
def check(self, owner: str, session_id: str) -> bool:
|
|
78
|
+
key = (owner, session_id)
|
|
79
|
+
now = time.monotonic()
|
|
80
|
+
window = self._counts.setdefault(key, [])
|
|
81
|
+
# purge entries older than 60s
|
|
82
|
+
cutoff = now - 60.0
|
|
83
|
+
while window and window[0] < cutoff:
|
|
84
|
+
window.pop(0)
|
|
85
|
+
if len(window) >= self._per_minute:
|
|
86
|
+
return False
|
|
87
|
+
window.append(now)
|
|
88
|
+
return True
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
class ChatServer:
|
|
92
|
+
"""FastAPI app wrapper holding session state."""
|
|
93
|
+
|
|
94
|
+
def __init__(
|
|
95
|
+
self,
|
|
96
|
+
*,
|
|
97
|
+
agent_factory: Callable[[], Agent],
|
|
98
|
+
history_store: ChatHistoryStore,
|
|
99
|
+
auth: BearerAuthPolicy,
|
|
100
|
+
host: str = "0.0.0.0", # noqa: S104 # nosec B104 — server defaults to all interfaces; caller binds explicitly in prod
|
|
101
|
+
port: int = 8080,
|
|
102
|
+
cors_origins: list[str] | None = None,
|
|
103
|
+
rate_limit_per_session_per_minute: int = 60,
|
|
104
|
+
truncation: HistoryTruncationStrategy | None = None,
|
|
105
|
+
) -> None:
|
|
106
|
+
self._agent_factory = agent_factory
|
|
107
|
+
self._history = history_store
|
|
108
|
+
self._auth = auth
|
|
109
|
+
self._host = host
|
|
110
|
+
self._port = port
|
|
111
|
+
self._cors_origins = list(cors_origins or [])
|
|
112
|
+
self._truncation = truncation
|
|
113
|
+
self._sessions: dict[str, ChatSession] = {}
|
|
114
|
+
self._session_owners: dict[str, str] = {}
|
|
115
|
+
self._sessions_lock = asyncio.Lock()
|
|
116
|
+
self._rate_limit = _RateLimiter(per_minute=rate_limit_per_session_per_minute)
|
|
117
|
+
self.app = self._build_app()
|
|
118
|
+
|
|
119
|
+
@property
|
|
120
|
+
def http_app(self) -> FastAPI:
|
|
121
|
+
return self.app
|
|
122
|
+
|
|
123
|
+
def _build_app(self) -> FastAPI:
|
|
124
|
+
app = FastAPI(title="agentforge-chat-http")
|
|
125
|
+
if self._cors_origins:
|
|
126
|
+
app.add_middleware(
|
|
127
|
+
CORSMiddleware,
|
|
128
|
+
allow_origins=self._cors_origins,
|
|
129
|
+
allow_credentials=True,
|
|
130
|
+
allow_methods=["*"],
|
|
131
|
+
allow_headers=["*"],
|
|
132
|
+
)
|
|
133
|
+
bearer = HTTPBearer(auto_error=False)
|
|
134
|
+
|
|
135
|
+
async def require_principal(
|
|
136
|
+
credentials: HTTPAuthorizationCredentials | None = Depends(bearer), # noqa: B008 — FastAPI Depends idiom
|
|
137
|
+
) -> Principal:
|
|
138
|
+
token = credentials.credentials if credentials is not None else None
|
|
139
|
+
principal = await self._auth.authenticate(token)
|
|
140
|
+
if principal is None:
|
|
141
|
+
raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED)
|
|
142
|
+
return principal
|
|
143
|
+
|
|
144
|
+
@app.get("/healthz")
|
|
145
|
+
async def healthz() -> dict[str, str]:
|
|
146
|
+
return {"status": "ok"}
|
|
147
|
+
|
|
148
|
+
@app.post("/sessions")
|
|
149
|
+
async def create_session(
|
|
150
|
+
body: CreateSessionRequest,
|
|
151
|
+
principal: Principal = Depends(require_principal), # noqa: B008
|
|
152
|
+
) -> dict[str, str]:
|
|
153
|
+
session = await self._create_session(body, principal)
|
|
154
|
+
return {"id": session.session_id}
|
|
155
|
+
|
|
156
|
+
@app.get("/sessions")
|
|
157
|
+
async def list_sessions(
|
|
158
|
+
principal: Principal = Depends(require_principal), # noqa: B008
|
|
159
|
+
) -> list[dict[str, Any]]:
|
|
160
|
+
infos = await self._history.list_sessions(owner=principal.id)
|
|
161
|
+
return [json.loads(i.model_dump_json()) for i in infos]
|
|
162
|
+
|
|
163
|
+
@app.delete("/sessions/{session_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
164
|
+
async def delete_session(
|
|
165
|
+
session_id: str,
|
|
166
|
+
principal: Principal = Depends(require_principal), # noqa: B008
|
|
167
|
+
) -> None:
|
|
168
|
+
self._assert_owner(session_id, principal)
|
|
169
|
+
async with self._sessions_lock:
|
|
170
|
+
session = self._sessions.pop(session_id, None)
|
|
171
|
+
self._session_owners.pop(session_id, None)
|
|
172
|
+
if session is not None:
|
|
173
|
+
await session.close()
|
|
174
|
+
await self._history.delete_session(session_id)
|
|
175
|
+
|
|
176
|
+
@app.post("/sessions/{session_id}/messages")
|
|
177
|
+
async def post_message(
|
|
178
|
+
session_id: str,
|
|
179
|
+
body: SendMessageRequest,
|
|
180
|
+
request: Request,
|
|
181
|
+
principal: Principal = Depends(require_principal), # noqa: B008
|
|
182
|
+
) -> Any:
|
|
183
|
+
self._assert_owner(session_id, principal)
|
|
184
|
+
if not self._rate_limit.check(principal.id, session_id):
|
|
185
|
+
raise HTTPException(status_code=status.HTTP_429_TOO_MANY_REQUESTS)
|
|
186
|
+
session = await self._get_session(session_id, principal)
|
|
187
|
+
accept = request.headers.get("accept", "")
|
|
188
|
+
if "text/event-stream" in accept:
|
|
189
|
+
return StreamingResponse(
|
|
190
|
+
self._sse_stream(session, body),
|
|
191
|
+
media_type="text/event-stream",
|
|
192
|
+
)
|
|
193
|
+
response = await session.send(body.content, idempotency_key=body.idempotency_key)
|
|
194
|
+
return json.loads(response.model_dump_json())
|
|
195
|
+
|
|
196
|
+
@app.get("/sessions/{session_id}/messages")
|
|
197
|
+
async def get_history(
|
|
198
|
+
session_id: str,
|
|
199
|
+
principal: Principal = Depends(require_principal), # noqa: B008
|
|
200
|
+
limit: int | None = None,
|
|
201
|
+
) -> list[dict[str, Any]]:
|
|
202
|
+
self._assert_owner(session_id, principal)
|
|
203
|
+
turns = await self._history.load(session_id, limit=limit)
|
|
204
|
+
return [json.loads(t.model_dump_json()) for t in turns]
|
|
205
|
+
|
|
206
|
+
@app.websocket("/sessions/{session_id}/ws")
|
|
207
|
+
async def ws_endpoint(ws: WebSocket, session_id: str) -> None:
|
|
208
|
+
await self._ws_endpoint(ws, session_id)
|
|
209
|
+
|
|
210
|
+
return app
|
|
211
|
+
|
|
212
|
+
async def _ws_endpoint(self, ws: WebSocket, session_id: str) -> None:
|
|
213
|
+
await ws.accept()
|
|
214
|
+
token = ws.headers.get("authorization", "").removeprefix("Bearer ").strip()
|
|
215
|
+
principal = await self._auth.authenticate(token or None)
|
|
216
|
+
if principal is None:
|
|
217
|
+
await ws.close(code=4401)
|
|
218
|
+
return
|
|
219
|
+
try:
|
|
220
|
+
self._assert_owner(session_id, principal)
|
|
221
|
+
except HTTPException:
|
|
222
|
+
await ws.close(code=4403)
|
|
223
|
+
return
|
|
224
|
+
session = await self._get_session(session_id, principal)
|
|
225
|
+
try:
|
|
226
|
+
while True:
|
|
227
|
+
raw = await ws.receive_text()
|
|
228
|
+
payload = json.loads(raw)
|
|
229
|
+
message = str(payload["content"])
|
|
230
|
+
cancellation = asyncio.Event()
|
|
231
|
+
consumer = asyncio.create_task(
|
|
232
|
+
self._consume_stream(ws, session, message, cancellation)
|
|
233
|
+
)
|
|
234
|
+
receiver = asyncio.create_task(ws.receive_text())
|
|
235
|
+
_, pending = await asyncio.wait(
|
|
236
|
+
{consumer, receiver},
|
|
237
|
+
return_when=asyncio.FIRST_COMPLETED,
|
|
238
|
+
)
|
|
239
|
+
if not consumer.done():
|
|
240
|
+
cancellation.set()
|
|
241
|
+
for task in pending:
|
|
242
|
+
task.cancel()
|
|
243
|
+
with contextlib.suppress(asyncio.CancelledError):
|
|
244
|
+
await task
|
|
245
|
+
except WebSocketDisconnect:
|
|
246
|
+
return
|
|
247
|
+
|
|
248
|
+
async def _consume_stream(
|
|
249
|
+
self,
|
|
250
|
+
ws: WebSocket,
|
|
251
|
+
session: ChatSession,
|
|
252
|
+
message: str,
|
|
253
|
+
cancellation: asyncio.Event,
|
|
254
|
+
) -> None:
|
|
255
|
+
async for chunk in await session.stream(message, cancellation=cancellation):
|
|
256
|
+
await ws.send_text(chunk.model_dump_json())
|
|
257
|
+
|
|
258
|
+
async def _sse_stream(
|
|
259
|
+
self,
|
|
260
|
+
session: ChatSession,
|
|
261
|
+
body: SendMessageRequest,
|
|
262
|
+
) -> Any:
|
|
263
|
+
async for chunk in await session.stream(body.content, idempotency_key=body.idempotency_key):
|
|
264
|
+
yield self._sse_format(chunk)
|
|
265
|
+
|
|
266
|
+
def _sse_format(self, chunk: ChatChunk) -> bytes:
|
|
267
|
+
return f"data: {chunk.model_dump_json()}\n\n".encode()
|
|
268
|
+
|
|
269
|
+
async def _create_session(
|
|
270
|
+
self, body: CreateSessionRequest, principal: Principal
|
|
271
|
+
) -> ChatSession:
|
|
272
|
+
agent = self._agent_factory()
|
|
273
|
+
session = ChatSession(
|
|
274
|
+
agent=agent,
|
|
275
|
+
session_id=body.session_id or uuid.uuid4().hex,
|
|
276
|
+
history_store=self._history,
|
|
277
|
+
system_prompt=body.system_prompt,
|
|
278
|
+
owner=principal.id,
|
|
279
|
+
truncation=self._truncation,
|
|
280
|
+
)
|
|
281
|
+
async with self._sessions_lock:
|
|
282
|
+
self._sessions[session.session_id] = session
|
|
283
|
+
self._session_owners[session.session_id] = principal.id
|
|
284
|
+
# Persist an initial owner record on the store so list_sessions
|
|
285
|
+
# filtering works before the first turn.
|
|
286
|
+
await self._history.update_session_metadata(session.session_id, {"owner": principal.id})
|
|
287
|
+
return session
|
|
288
|
+
|
|
289
|
+
async def _get_session(self, session_id: str, principal: Principal) -> ChatSession:
|
|
290
|
+
async with self._sessions_lock:
|
|
291
|
+
cached = self._sessions.get(session_id)
|
|
292
|
+
if cached is not None:
|
|
293
|
+
return cached
|
|
294
|
+
# Cold path: rehydrate a session object from existing history.
|
|
295
|
+
agent = self._agent_factory()
|
|
296
|
+
session = ChatSession(
|
|
297
|
+
agent=agent,
|
|
298
|
+
session_id=session_id,
|
|
299
|
+
history_store=self._history,
|
|
300
|
+
owner=principal.id,
|
|
301
|
+
truncation=self._truncation,
|
|
302
|
+
)
|
|
303
|
+
async with self._sessions_lock:
|
|
304
|
+
self._sessions[session_id] = session
|
|
305
|
+
self._session_owners[session_id] = principal.id
|
|
306
|
+
return session
|
|
307
|
+
|
|
308
|
+
def _assert_owner(self, session_id: str, principal: Principal) -> None:
|
|
309
|
+
recorded = self._session_owners.get(session_id)
|
|
310
|
+
if recorded is not None and recorded != principal.id:
|
|
311
|
+
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN)
|
|
312
|
+
|
|
313
|
+
async def serve(self) -> None:
|
|
314
|
+
config = uvicorn.Config(self.app, host=self._host, port=self._port, log_level="info")
|
|
315
|
+
server = uvicorn.Server(config)
|
|
316
|
+
await server.serve()
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
__all__ = [
|
|
320
|
+
"ChatResponse",
|
|
321
|
+
"ChatServer",
|
|
322
|
+
"ChatTurn",
|
|
323
|
+
"CreateSessionRequest",
|
|
324
|
+
"SendMessageRequest",
|
|
325
|
+
]
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: agentforge-chat-http
|
|
3
|
+
Version: 0.2.3
|
|
4
|
+
Summary: FastAPI HTTP + WebSocket + SSE server for AgentForge chat sessions
|
|
5
|
+
Project-URL: Homepage, https://github.com/Scaffoldic/agentforge-py
|
|
6
|
+
Project-URL: Repository, https://github.com/Scaffoldic/agentforge-py
|
|
7
|
+
Project-URL: Changelog, https://github.com/Scaffoldic/agentforge-py/blob/main/CHANGELOG.md
|
|
8
|
+
Author: The AgentForge Authors
|
|
9
|
+
License-Expression: Apache-2.0
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: agent,ai,chat,fastapi,sse,websocket
|
|
12
|
+
Classifier: Development Status :: 2 - Pre-Alpha
|
|
13
|
+
Classifier: Framework :: FastAPI
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
19
|
+
Classifier: Typing :: Typed
|
|
20
|
+
Requires-Python: >=3.13
|
|
21
|
+
Requires-Dist: agentforge-chat~=0.2.3
|
|
22
|
+
Requires-Dist: agentforge-core~=0.2.3
|
|
23
|
+
Requires-Dist: agentforge-py~=0.2.3
|
|
24
|
+
Requires-Dist: fastapi>=0.115
|
|
25
|
+
Requires-Dist: httpx>=0.27
|
|
26
|
+
Requires-Dist: uvicorn>=0.32
|
|
27
|
+
Description-Content-Type: text/markdown
|
|
28
|
+
|
|
29
|
+
# agentforge-chat-http
|
|
30
|
+
|
|
31
|
+
FastAPI server for `agentforge-chat`: REST + WebSocket + SSE,
|
|
32
|
+
bearer auth, in-process rate limiting, multi-tenant session
|
|
33
|
+
isolation.
|
|
34
|
+
|
|
35
|
+
See [`docs/features/feat-020-chat-agents.md`](https://github.com/Scaffoldic/agentforge-py/blob/main/docs/features/feat-020-chat-agents.md)
|
|
36
|
+
§4.1 for the HTTP wire format.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install agentforge-chat-http
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Run a chat server
|
|
45
|
+
|
|
46
|
+
```python
|
|
47
|
+
import asyncio
|
|
48
|
+
from agentforge import Agent
|
|
49
|
+
from agentforge_chat import InMemoryChatHistory
|
|
50
|
+
from agentforge_chat_http import ChatServer, EnvBearerAuth
|
|
51
|
+
|
|
52
|
+
async def main() -> None:
|
|
53
|
+
server = ChatServer(
|
|
54
|
+
agent_factory=lambda: Agent(model="anthropic:claude-sonnet-4-6", strategy="react"),
|
|
55
|
+
history_store=InMemoryChatHistory(),
|
|
56
|
+
auth=EnvBearerAuth("API_TOKENS"),
|
|
57
|
+
host="0.0.0.0",
|
|
58
|
+
port=8080,
|
|
59
|
+
)
|
|
60
|
+
await server.serve()
|
|
61
|
+
|
|
62
|
+
asyncio.run(main())
|
|
63
|
+
```
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
agentforge_chat_http/__init__.py,sha256=5i1F4KUdeh3An-IkKgkTwA17kiM-ryjR8lSaHOr0cH0,457
|
|
2
|
+
agentforge_chat_http/auth.py,sha256=xJ5XBUoW2fboysJTI7yimQDOT0OcmfZSYWhxJFz1izs,984
|
|
3
|
+
agentforge_chat_http/manifest.yaml,sha256=vMhWYTxlnbfBg2kJyr1TOaoRWtYsObQAXfuQgLyQQzY,564
|
|
4
|
+
agentforge_chat_http/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
agentforge_chat_http/server.py,sha256=burKy6QErOOmsDy08NpyadmIW22913WtormlpHlmopY,12038
|
|
6
|
+
agentforge_chat_http-0.2.3.dist-info/METADATA,sha256=TPtLUKctPT_CsdHrzNpvMS-TWeMODwXSwaCxHbPw42I,2049
|
|
7
|
+
agentforge_chat_http-0.2.3.dist-info/WHEEL,sha256=QccIxa26bgl1E6uMy58deGWi-0aeIkkangHcxk2kWfw,87
|
|
8
|
+
agentforge_chat_http-0.2.3.dist-info/entry_points.txt,sha256=wP9p2XBsFq4Y-EiDxM1TLxiYSKVcj5psEiuqAfhiAog,74
|
|
9
|
+
agentforge_chat_http-0.2.3.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
10
|
+
agentforge_chat_http-0.2.3.dist-info/RECORD,,
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|