Skip to content

Commit

Permalink
fix: gcp editor agents to comply with application/json
Browse files Browse the repository at this point in the history
  • Loading branch information
frederik-encord committed Dec 19, 2024
1 parent 2118626 commit 6628184
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion encord_agents/gcp/wrappers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import re
from contextlib import ExitStack
from functools import wraps
from typing import Any, Callable
Expand Down Expand Up @@ -26,6 +27,15 @@ def generate_response() -> Response:
return response


ALLOWED_ORIGINS = [
r"^https:\/\/app.encord.com$",
r"^https:\/\/staging.app.encord.com$",
r"^https:\/\/dev.encord.com$",
r"^https:\/\/staging.encord.com$",
r"^https:\/\/cord-ai-development--[\w\d]+-[\w\d]+\.web.app$",
]


def editor_agent(
*,
label_row_metadata_include_args: LabelRowMetadataIncludeArgs | None = None,
Expand All @@ -52,7 +62,34 @@ def context_wrapper_inner(func: AgentFunction) -> Callable[[Request], Response]:

@wraps(func)
def wrapper(request: Request) -> Response:
frame_data = FrameData.model_validate_json(request.data)
# Set CORS headers for the preflight request
if request.method == "OPTIONS":
# Allows GET requests from any origin with the Content-Type
# header and caches preflight response for an 3600s
response = make_response("")

origin_ok = False
for origin in ALLOWED_ORIGINS:
origin_ok |= bool(re.search(origin, request.origin))

if not origin_ok:
response.status_code = 403
return response

headers = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST",
"Access-Control-Allow-Headers": "Content-Type",
"Access-Control-Max-Age": "3600",
}
response.headers.update(headers)
response.status_code = 204
return response

if request.is_json:
frame_data = FrameData.model_validate(request.get_json())
else:
frame_data = FrameData.model_validate_json(request.get_data())
logging.info(f"Request: {frame_data}")

client = get_user_client()
Expand Down

0 comments on commit 6628184

Please sign in to comment.