agenta 0.27.3__py3-none-any.whl → 0.27.4a0__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.
Potentially problematic release.
This version of agenta might be problematic. Click here for more details.
- agenta/sdk/decorators/routing.py +27 -3
- agenta/sdk/middleware/__init__.py +0 -0
- agenta/sdk/middleware/auth.py +136 -0
- agenta/sdk/middleware/cache.py +43 -0
- agenta/sdk/tracing/exporters.py +1 -1
- agenta/sdk/tracing/processors.py +1 -1
- {agenta-0.27.3.dist-info → agenta-0.27.4a0.dist-info}/METADATA +1 -1
- {agenta-0.27.3.dist-info → agenta-0.27.4a0.dist-info}/RECORD +10 -7
- {agenta-0.27.3.dist-info → agenta-0.27.4a0.dist-info}/WHEEL +0 -0
- {agenta-0.27.3.dist-info → agenta-0.27.4a0.dist-info}/entry_points.txt +0 -0
agenta/sdk/decorators/routing.py
CHANGED
|
@@ -14,9 +14,10 @@ from os import environ
|
|
|
14
14
|
from fastapi.middleware.cors import CORSMiddleware
|
|
15
15
|
from fastapi import Body, FastAPI, UploadFile, HTTPException
|
|
16
16
|
|
|
17
|
+
from agenta.sdk.middleware.auth import AuthorizationMiddleware
|
|
17
18
|
from agenta.sdk.context.routing import routing_context_manager, routing_context
|
|
18
19
|
from agenta.sdk.context.tracing import tracing_context
|
|
19
|
-
from agenta.sdk.router import router
|
|
20
|
+
from agenta.sdk.router import router
|
|
20
21
|
from agenta.sdk.utils.exceptions import suppress
|
|
21
22
|
from agenta.sdk.utils.logging import log
|
|
22
23
|
from agenta.sdk.types import (
|
|
@@ -50,6 +51,9 @@ app.add_middleware(
|
|
|
50
51
|
allow_headers=["*"],
|
|
51
52
|
)
|
|
52
53
|
|
|
54
|
+
_MIDDLEWARES = True
|
|
55
|
+
|
|
56
|
+
|
|
53
57
|
app.include_router(router, prefix="")
|
|
54
58
|
|
|
55
59
|
|
|
@@ -121,6 +125,26 @@ class entrypoint:
|
|
|
121
125
|
route_path="",
|
|
122
126
|
config_schema: Optional[BaseModel] = None,
|
|
123
127
|
):
|
|
128
|
+
### --- Update Middleware --- #
|
|
129
|
+
try:
|
|
130
|
+
global _MIDDLEWARES # pylint: disable=global-statement
|
|
131
|
+
|
|
132
|
+
if _MIDDLEWARES:
|
|
133
|
+
app.add_middleware(
|
|
134
|
+
AuthorizationMiddleware,
|
|
135
|
+
host=ag.DEFAULT_AGENTA_SINGLETON_INSTANCE.host,
|
|
136
|
+
resource_id=ag.DEFAULT_AGENTA_SINGLETON_INSTANCE.app_id,
|
|
137
|
+
resource_type="application",
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
_MIDDLEWARES = False
|
|
141
|
+
|
|
142
|
+
except: # pylint: disable=bare-except
|
|
143
|
+
log.error("------------------------------------")
|
|
144
|
+
log.error("Agenta SDK - failed to secure route: %s", route_path)
|
|
145
|
+
log.error("------------------------------------")
|
|
146
|
+
### --- Update Middleware --- #
|
|
147
|
+
|
|
124
148
|
DEFAULT_PATH = "generate"
|
|
125
149
|
PLAYGROUND_PATH = "/playground"
|
|
126
150
|
RUN_PATH = "/run"
|
|
@@ -330,9 +354,9 @@ class entrypoint:
|
|
|
330
354
|
*args,
|
|
331
355
|
**func_params,
|
|
332
356
|
):
|
|
333
|
-
log.info(
|
|
357
|
+
log.info("---------------------------")
|
|
334
358
|
log.info(f"Agenta SDK - running route: {repr(self.route_path or '/')}")
|
|
335
|
-
log.info(
|
|
359
|
+
log.info("---------------------------")
|
|
336
360
|
|
|
337
361
|
tracing_context.set(routing_context.get())
|
|
338
362
|
|
|
File without changes
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
from typing import Callable, Optional
|
|
2
|
+
from os import environ
|
|
3
|
+
from uuid import UUID
|
|
4
|
+
from json import dumps
|
|
5
|
+
from traceback import format_exc
|
|
6
|
+
|
|
7
|
+
import httpx
|
|
8
|
+
from starlette.middleware.base import BaseHTTPMiddleware
|
|
9
|
+
from fastapi import FastAPI, Request, Response
|
|
10
|
+
|
|
11
|
+
from agenta.sdk.utils.logging import log
|
|
12
|
+
from agenta.sdk.middleware.cache import TTLLRUCache
|
|
13
|
+
|
|
14
|
+
AGENTA_SDK_AUTH_CACHE_CAPACITY = environ.get(
|
|
15
|
+
"AGENTA_SDK_AUTH_CACHE_CAPACITY",
|
|
16
|
+
512,
|
|
17
|
+
)
|
|
18
|
+
|
|
19
|
+
AGENTA_SDK_AUTH_CACHE_TTL = environ.get(
|
|
20
|
+
"AGENTA_SDK_AUTH_CACHE_TTL",
|
|
21
|
+
15 * 60, # 15 minutes
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
AGENTA_UNAUTHORIZED_EXECUTION_ALLOWED = str(
|
|
25
|
+
environ.get("AGENTA_UNAUTHORIZED_EXECUTION_ALLOWED", False)
|
|
26
|
+
).lower() in ("true", "1", "t")
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class Deny(Response):
|
|
30
|
+
def __init__(self) -> None:
|
|
31
|
+
super().__init__(status_code=401, content="Unauthorized")
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
cache = TTLLRUCache(
|
|
35
|
+
capacity=AGENTA_SDK_AUTH_CACHE_CAPACITY,
|
|
36
|
+
ttl=AGENTA_SDK_AUTH_CACHE_TTL,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class AuthorizationMiddleware(BaseHTTPMiddleware):
|
|
41
|
+
def __init__(
|
|
42
|
+
self,
|
|
43
|
+
app: FastAPI,
|
|
44
|
+
host: str,
|
|
45
|
+
resource_id: UUID,
|
|
46
|
+
resource_type: str,
|
|
47
|
+
):
|
|
48
|
+
super().__init__(app)
|
|
49
|
+
|
|
50
|
+
self.host = host
|
|
51
|
+
self.resource_id = resource_id
|
|
52
|
+
self.resource_type = resource_type
|
|
53
|
+
|
|
54
|
+
async def dispatch(
|
|
55
|
+
self,
|
|
56
|
+
request: Request,
|
|
57
|
+
call_next: Callable,
|
|
58
|
+
project_id: Optional[UUID] = None,
|
|
59
|
+
):
|
|
60
|
+
if AGENTA_UNAUTHORIZED_EXECUTION_ALLOWED:
|
|
61
|
+
return await call_next(request)
|
|
62
|
+
|
|
63
|
+
try:
|
|
64
|
+
authorization = (
|
|
65
|
+
request.headers.get("Authorization")
|
|
66
|
+
or request.headers.get("authorization")
|
|
67
|
+
or None
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
headers = {"Authorization": authorization} if authorization else None
|
|
71
|
+
|
|
72
|
+
cookies = {"sAccessToken": request.cookies.get("sAccessToken")}
|
|
73
|
+
|
|
74
|
+
params = {
|
|
75
|
+
"action": "run_service",
|
|
76
|
+
"resource_type": self.resource_type,
|
|
77
|
+
"resource_id": self.resource_id,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
if project_id:
|
|
81
|
+
params["project_id"] = project_id
|
|
82
|
+
|
|
83
|
+
_hash = dumps(
|
|
84
|
+
{
|
|
85
|
+
"headers": headers,
|
|
86
|
+
"cookies": cookies,
|
|
87
|
+
"params": params,
|
|
88
|
+
},
|
|
89
|
+
sort_keys=True,
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
cached_policy = cache.get(_hash)
|
|
93
|
+
|
|
94
|
+
if not cached_policy:
|
|
95
|
+
async with httpx.AsyncClient() as client:
|
|
96
|
+
response = await client.get(
|
|
97
|
+
f"{self.host}/api/permissions/verify",
|
|
98
|
+
headers=headers,
|
|
99
|
+
cookies=cookies,
|
|
100
|
+
params=params,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
if response.status_code != 200:
|
|
104
|
+
cache.put(_hash, {"effect": "deny"})
|
|
105
|
+
return Deny()
|
|
106
|
+
|
|
107
|
+
auth = response.json()
|
|
108
|
+
|
|
109
|
+
if auth.get("effect") != "allow":
|
|
110
|
+
cache.put(_hash, {"effect": "deny"})
|
|
111
|
+
return Deny()
|
|
112
|
+
|
|
113
|
+
cached_policy = {
|
|
114
|
+
"effect": "allow",
|
|
115
|
+
"credentials": auth.get("credentials"),
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
cache.put(_hash, cached_policy)
|
|
119
|
+
|
|
120
|
+
if cached_policy.get("effect") == "deny":
|
|
121
|
+
return Deny()
|
|
122
|
+
|
|
123
|
+
request.state.credentials = cached_policy.get("credentials")
|
|
124
|
+
|
|
125
|
+
print(f"credentials: {request.state.credentials}")
|
|
126
|
+
|
|
127
|
+
return await call_next(request)
|
|
128
|
+
|
|
129
|
+
except: # pylint: disable=bare-except
|
|
130
|
+
log.error("------------------------------------------------------")
|
|
131
|
+
log.error("Agenta SDK - handling auth middleware exception below:")
|
|
132
|
+
log.error("------------------------------------------------------")
|
|
133
|
+
log.error(format_exc().strip("\n"))
|
|
134
|
+
log.error("------------------------------------------------------")
|
|
135
|
+
|
|
136
|
+
return Deny()
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from time import time
|
|
2
|
+
from collections import OrderedDict
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class TTLLRUCache:
|
|
6
|
+
def __init__(self, capacity: int, ttl: int):
|
|
7
|
+
self.cache = OrderedDict()
|
|
8
|
+
self.capacity = capacity
|
|
9
|
+
self.ttl = ttl
|
|
10
|
+
|
|
11
|
+
def get(self, key):
|
|
12
|
+
# CACHE
|
|
13
|
+
if key not in self.cache:
|
|
14
|
+
return None
|
|
15
|
+
|
|
16
|
+
value, expiry = self.cache[key]
|
|
17
|
+
# -----
|
|
18
|
+
|
|
19
|
+
# TTL
|
|
20
|
+
if time() > expiry:
|
|
21
|
+
del self.cache[key]
|
|
22
|
+
|
|
23
|
+
return None
|
|
24
|
+
# ---
|
|
25
|
+
|
|
26
|
+
# LRU
|
|
27
|
+
self.cache.move_to_end(key)
|
|
28
|
+
# ---
|
|
29
|
+
|
|
30
|
+
return value
|
|
31
|
+
|
|
32
|
+
def put(self, key, value):
|
|
33
|
+
# CACHE
|
|
34
|
+
if key in self.cache:
|
|
35
|
+
del self.cache[key]
|
|
36
|
+
# CACHE & LRU
|
|
37
|
+
elif len(self.cache) >= self.capacity:
|
|
38
|
+
self.cache.popitem(last=False)
|
|
39
|
+
# -----------
|
|
40
|
+
|
|
41
|
+
# TTL
|
|
42
|
+
self.cache[key] = (value, time() + self.ttl)
|
|
43
|
+
# ---
|
agenta/sdk/tracing/exporters.py
CHANGED
|
@@ -58,7 +58,7 @@ class InlineTraceExporter(SpanExporter):
|
|
|
58
58
|
return trace
|
|
59
59
|
|
|
60
60
|
|
|
61
|
-
OTLPSpanExporter._MAX_RETRY_TIMEOUT = 2
|
|
61
|
+
OTLPSpanExporter._MAX_RETRY_TIMEOUT = 2 # pylint: disable=protected-access
|
|
62
62
|
|
|
63
63
|
ConsoleExporter = ConsoleSpanExporter
|
|
64
64
|
InlineExporter = InlineTraceExporter
|
agenta/sdk/tracing/processors.py
CHANGED
|
@@ -147,7 +147,7 @@ agenta/sdk/context/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuF
|
|
|
147
147
|
agenta/sdk/context/routing.py,sha256=ycUgmJZyWhL4bHjKtUSAsTlt_0Fujr_6OpoaEH1lAN0,683
|
|
148
148
|
agenta/sdk/context/tracing.py,sha256=UmmW15UFFsvxS0myS6aD9wBk5iNepNlQi4tEQ_ejfYM,96
|
|
149
149
|
agenta/sdk/decorators/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
150
|
-
agenta/sdk/decorators/routing.py,sha256=
|
|
150
|
+
agenta/sdk/decorators/routing.py,sha256=biG9W7zgZ6V8z9Ca6Oq4NC7m89vO97mnRwmy2vOYook,36987
|
|
151
151
|
agenta/sdk/decorators/tracing.py,sha256=rwxbxsDb6B0VaJf4qLPgyTMYNkJMFLsCiX2ZQ6W-zOw,7713
|
|
152
152
|
agenta/sdk/litellm/__init__.py,sha256=Bpz1gfHQc0MN1yolWcjifLWznv6GjHggvRGQSpxpihM,37
|
|
153
153
|
agenta/sdk/litellm/litellm.py,sha256=J9NefQ2yvfWQy9e0zmPZiRQDz5GM2ThWAw5XM8gCaqw,8461
|
|
@@ -156,14 +156,17 @@ agenta/sdk/managers/config.py,sha256=AuFfHYOkmilDdcAJt2nlw_WlA3ho4Htjf29FKTZGElM
|
|
|
156
156
|
agenta/sdk/managers/deployment.py,sha256=SEokjZeh6n7HRKZ92Y0WncdG49hIFx-Z3B3HAl2kmUg,1174
|
|
157
157
|
agenta/sdk/managers/shared.py,sha256=e53jckQq5PIMpjdxADOonUj7o8aGfzmSvdeH5f43rGs,21497
|
|
158
158
|
agenta/sdk/managers/variant.py,sha256=A5ga3mq3b0weUTXa9HO72MGaspthGcu1uK9K5OnP738,4172
|
|
159
|
+
agenta/sdk/middleware/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
160
|
+
agenta/sdk/middleware/auth.py,sha256=olVQ9Mm5E3dYDtjRkMXWGzJe_WkMTkYw7Yf5v9uKC0U,3999
|
|
161
|
+
agenta/sdk/middleware/cache.py,sha256=C_LEzFbulbCBIKtcut2T4qJzh90q4369WCsApDg3Vm0,902
|
|
159
162
|
agenta/sdk/router.py,sha256=mOguvtOwl2wmyAgOuWTsf98pQwpNiUILKIo67W_hR3A,119
|
|
160
163
|
agenta/sdk/tracing/__init__.py,sha256=rQNe5-zT5Kt7_CDhq-lnUIi1EYTBVzVf_MbfcIxVD98,41
|
|
161
164
|
agenta/sdk/tracing/attributes.py,sha256=zh8JQZSeYCLBeIRSopKJx6QQ-WEgw08Cr64DS_WOcT8,3833
|
|
162
165
|
agenta/sdk/tracing/context.py,sha256=PSJdhcaOXSMAuGUBySpLKPKyx8duF3TJzhUEk2ufqPc,777
|
|
163
166
|
agenta/sdk/tracing/conventions.py,sha256=JBtznBXZ3aRkGKkLl7cPwdMNh3w1G-H2Ta2YrAxbr38,950
|
|
164
|
-
agenta/sdk/tracing/exporters.py,sha256=
|
|
167
|
+
agenta/sdk/tracing/exporters.py,sha256=YvTke0RaxeOLqWOuhC5EFzYwFY39kcoBtDLfcyla3j4,1604
|
|
165
168
|
agenta/sdk/tracing/inline.py,sha256=jVIlmDIFSzXr-ofMbGlO8VFnNeiUL4C4yA8soED4AHI,34611
|
|
166
|
-
agenta/sdk/tracing/processors.py,sha256=
|
|
169
|
+
agenta/sdk/tracing/processors.py,sha256=tjoz_uXm_2yD1Ozvz_CnYnsG82R6vmnYpjMhx2ztALs,3117
|
|
167
170
|
agenta/sdk/tracing/spans.py,sha256=nqUOjjirBxB8Eacv8Qj4Ra_6rknGi3lbJdNyKmk5ODQ,3707
|
|
168
171
|
agenta/sdk/tracing/tracing.py,sha256=50669Lr6XwRoIEWDqEFj-K4HRcYSaL9i5vqwARvP7bk,6778
|
|
169
172
|
agenta/sdk/types.py,sha256=oEeSUQn4tMzV7dkSuucBC2YlAaxS-7qgkdlT763YfLM,7076
|
|
@@ -190,7 +193,7 @@ agenta/templates/simple_prompt/app.py,sha256=kODgF6lhzsaJPdgL5b21bUki6jkvqjWZzWR
|
|
|
190
193
|
agenta/templates/simple_prompt/env.example,sha256=g9AE5bYcGPpxawXMJ96gh8oenEPCHTabsiOnfQo3c5k,70
|
|
191
194
|
agenta/templates/simple_prompt/requirements.txt,sha256=ywRglRy7pPkw8bljmMEJJ4aOOQKrt9FGKULZ-DGkoBU,23
|
|
192
195
|
agenta/templates/simple_prompt/template.toml,sha256=DQBtRrF4GU8LBEXOZ-GGuINXMQDKGTEG5y37tnvIUIE,60
|
|
193
|
-
agenta-0.27.
|
|
194
|
-
agenta-0.27.
|
|
195
|
-
agenta-0.27.
|
|
196
|
-
agenta-0.27.
|
|
196
|
+
agenta-0.27.4a0.dist-info/METADATA,sha256=-GdsWNYbNU0cr7NXSuY_PvlX99YhILCnih7yjbXHPak,31738
|
|
197
|
+
agenta-0.27.4a0.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
|
198
|
+
agenta-0.27.4a0.dist-info/entry_points.txt,sha256=PDiu8_8AsL7ibU9v4iNoOKR1S7F2rdxjlEprjM9QOgo,46
|
|
199
|
+
agenta-0.27.4a0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|