Skip to content

Commit 13c84e1

Browse files
author
acer-king
committed
cursor support
1 parent 9abc81a commit 13c84e1

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

validators/core/axon.py

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
import os
32
import uuid
43
import copy
@@ -51,7 +50,6 @@ def __init__(self,
5150
self.app.add_middleware(AxonMiddleware, axon=self)
5251

5352

54-
5553
class CortexAxonMiddleware(BaseHTTPMiddleware):
5654
"""
5755
The `AxonMiddleware` class is a key component in the Axon server, responsible for processing all incoming requests.
@@ -85,7 +83,7 @@ def __init__(self, app: "AxonMiddleware", axon: "bittensor.axon"):
8583
self.axon = axon
8684

8785
async def dispatch(
88-
self, request: Request, call_next: RequestResponseEndpoint
86+
self, request: Request, call_next: RequestResponseEndpoint
8987
) -> Response:
9088
"""
9189
Asynchronously processes incoming HTTP requests and returns the corresponding responses. This
@@ -115,6 +113,21 @@ async def dispatch(
115113
# Records the start time of the request processing.
116114
start_time = time.time()
117115

116+
if "v1/chat/completions" in request.url.path:
117+
if request.method == "OPTIONS":
118+
return await call_next(request)
119+
try:
120+
api_key = request.headers.get("Authorization").split(" ")[1]
121+
if not api_key or api_key not in VALID_API_KEYS:
122+
return JSONResponse(
123+
{"detail": "Invalid or missing API Key"}, status_code=401
124+
)
125+
return await call_next(request)
126+
except Exception:
127+
return JSONResponse(
128+
{"detail": "Invalid or missing API Key"}, status_code=401
129+
)
130+
118131
try:
119132
# Set up the synapse from its headers.
120133
synapse: bittensor.Synapse = await self.preprocess(request)
@@ -410,7 +423,7 @@ async def priority(self, synapse: bittensor.Synapse):
410423
priority_fn = self.axon.priority_fns.get(str(synapse.name), None)
411424

412425
async def submit_task(
413-
executor: PriorityThreadPoolExecutor, priority: float
426+
executor: PriorityThreadPoolExecutor, priority: float
414427
) -> Tuple[float, Any]:
415428
"""
416429
Submits the given priority function to the specified executor for asynchronous execution.
@@ -455,10 +468,10 @@ async def submit_task(
455468
raise PriorityException(f"Response timeout after: {synapse.timeout}s")
456469

457470
async def run(
458-
self,
459-
synapse: bittensor.Synapse,
460-
call_next: RequestResponseEndpoint,
461-
request: Request,
471+
self,
472+
synapse: bittensor.Synapse,
473+
call_next: RequestResponseEndpoint,
474+
request: Request,
462475
) -> Response:
463476
"""
464477
Executes the requested function as part of the request processing pipeline. This method calls
@@ -499,7 +512,7 @@ async def run(
499512
return response
500513

501514
async def postprocess(
502-
self, synapse: bittensor.Synapse, response: Response, start_time: float
515+
self, synapse: bittensor.Synapse, response: Response, start_time: float
503516
) -> Response:
504517
"""
505518
Performs the final processing on the response before sending it back to the client. This method

0 commit comments

Comments
 (0)