From 8c312f891b0f9b115e7ec2a94c11ab98e0fbe2c7 Mon Sep 17 00:00:00 2001 From: wy <18672293959@126.com> Date: Sat, 8 Jun 2024 21:11:20 +0800 Subject: [PATCH 1/5] =?UTF-8?q?update:1=E3=80=81add=20response=20header=20?= =?UTF-8?q?Content-Type:application/json,charest=3DUTF-8,=20solve=20cannot?= =?UTF-8?q?=20display=20chinese=20word=202=E3=80=81update=20the=20openai?= =?UTF-8?q?=20api=20key=20pattern,=20like=20sk-wa-xxx=203=E3=80=81add=20Op?= =?UTF-8?q?tional=20for=20additional=5Finput,=20in=20TaskRequestBody=20and?= =?UTF-8?q?=20StepRequestBody,=20to=20avoid=20422=20status=20in=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- autogpt/autogpt/app/agent_protocol_server.py | 16 +++++++++++++--- forge/forge/agent_protocol/models/task.py | 4 ++-- forge/forge/config/config.py | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/autogpt/autogpt/app/agent_protocol_server.py b/autogpt/autogpt/app/agent_protocol_server.py index 7544d817b2b1..c1771ed03305 100644 --- a/autogpt/autogpt/app/agent_protocol_server.py +++ b/autogpt/autogpt/app/agent_protocol_server.py @@ -6,7 +6,8 @@ from uuid import uuid4 import orjson -from fastapi import APIRouter, FastAPI, UploadFile +from typing import Callable +from fastapi import APIRouter, FastAPI, UploadFile, FastAPI, Request, Response from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import RedirectResponse, StreamingResponse from fastapi.staticfiles import StaticFiles @@ -40,6 +41,15 @@ logger = logging.getLogger(__name__) +def create_middleware(app: FastAPI): + async def add_custom_header(request: Request, call_next: Callable) -> Response: + response = await call_next(request) + response.headers["Content-Type"] = "application/json ; charset=UTF-8" + return response + + app.middleware("http")(add_custom_header) + + class AgentProtocolServer: _task_budgets: dict[str, ModelProviderBudget] @@ -76,7 +86,7 @@ async def start(self, port: int = 8000, router: APIRouter = base_router): "Modified version of The Agent Protocol.", version="v0.4", ) - + create_middleware(app) # Configure CORS middleware default_origins = [f"http://localhost:{port}"] # Default only local access configured_origins = [ @@ -88,7 +98,7 @@ async def start(self, port: int = 8000, router: APIRouter = base_router): app.add_middleware( CORSMiddleware, - allow_origins=origins, + allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], diff --git a/forge/forge/agent_protocol/models/task.py b/forge/forge/agent_protocol/models/task.py index c7e30cbdbb0d..a4ef0b3f2034 100644 --- a/forge/forge/agent_protocol/models/task.py +++ b/forge/forge/agent_protocol/models/task.py @@ -17,7 +17,7 @@ class TaskRequestBody(BaseModel): description="Input prompt for the task.", example="Write the words you receive to the file 'output.txt'.", ) - additional_input: dict[str, Any] = Field(default_factory=dict) + additional_input: Optional[dict[str, Any]] = Field(default_factory=dict) class Task(TaskRequestBody): @@ -55,7 +55,7 @@ class StepRequestBody(BaseModel): input: str = Field( ..., description="Input prompt for the step.", example="Washington" ) - additional_input: dict[str, Any] = Field(default_factory=dict) + additional_input: Optional[dict[str, Any]] = Field(default_factory=dict) class StepStatus(Enum): diff --git a/forge/forge/config/config.py b/forge/forge/config/config.py index 27b57cb5a2a6..6eef0706a05b 100644 --- a/forge/forge/config/config.py +++ b/forge/forge/config/config.py @@ -210,7 +210,7 @@ def build_config_from_env(cls, project_root: Path = PROJECT_ROOT) -> Config: def assert_config_has_openai_api_key(config: Config) -> None: """Check if the OpenAI API key is set in config.py or as an environment variable.""" - key_pattern = r"^sk-(proj-)?\w{48}" + key_pattern = r"^sk-(?:proj-|wa-)?\w{48}" openai_api_key = ( config.openai_credentials.api_key.get_secret_value() if config.openai_credentials From c734867365e31737baf70bdedb1abc2772e4a9f1 Mon Sep 17 00:00:00 2001 From: Nicholas Tindle Date: Mon, 10 Jun 2024 15:11:17 -0500 Subject: [PATCH 2/5] feat(autogpt): fix the origins being repalced --- autogpt/autogpt/app/agent_protocol_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogpt/autogpt/app/agent_protocol_server.py b/autogpt/autogpt/app/agent_protocol_server.py index c1771ed03305..0927be3b64d9 100644 --- a/autogpt/autogpt/app/agent_protocol_server.py +++ b/autogpt/autogpt/app/agent_protocol_server.py @@ -98,7 +98,7 @@ async def start(self, port: int = 8000, router: APIRouter = base_router): app.add_middleware( CORSMiddleware, - allow_origins=["*"], + allow_origins=[origins], allow_credentials=True, allow_methods=["*"], allow_headers=["*"], From bfa81760749221aa5026c541c8f9e402985b97da Mon Sep 17 00:00:00 2001 From: Nicholas Tindle Date: Mon, 10 Jun 2024 15:12:04 -0500 Subject: [PATCH 3/5] fix: sent origins as an array of array instead of array --- autogpt/autogpt/app/agent_protocol_server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autogpt/autogpt/app/agent_protocol_server.py b/autogpt/autogpt/app/agent_protocol_server.py index 0927be3b64d9..6df820b27c1d 100644 --- a/autogpt/autogpt/app/agent_protocol_server.py +++ b/autogpt/autogpt/app/agent_protocol_server.py @@ -98,7 +98,7 @@ async def start(self, port: int = 8000, router: APIRouter = base_router): app.add_middleware( CORSMiddleware, - allow_origins=[origins], + allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], From b6a667c98b03805bce15ba596529c4bb139ed8e8 Mon Sep 17 00:00:00 2001 From: Nicholas Tindle Date: Mon, 10 Jun 2024 15:13:19 -0500 Subject: [PATCH 4/5] fix: review changes requested --- forge/forge/agent_protocol/models/task.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/forge/forge/agent_protocol/models/task.py b/forge/forge/agent_protocol/models/task.py index a4ef0b3f2034..c7e30cbdbb0d 100644 --- a/forge/forge/agent_protocol/models/task.py +++ b/forge/forge/agent_protocol/models/task.py @@ -17,7 +17,7 @@ class TaskRequestBody(BaseModel): description="Input prompt for the task.", example="Write the words you receive to the file 'output.txt'.", ) - additional_input: Optional[dict[str, Any]] = Field(default_factory=dict) + additional_input: dict[str, Any] = Field(default_factory=dict) class Task(TaskRequestBody): @@ -55,7 +55,7 @@ class StepRequestBody(BaseModel): input: str = Field( ..., description="Input prompt for the step.", example="Washington" ) - additional_input: Optional[dict[str, Any]] = Field(default_factory=dict) + additional_input: dict[str, Any] = Field(default_factory=dict) class StepStatus(Enum): From 20f3d9927d1fa8b55990953b948401826c2e1e40 Mon Sep 17 00:00:00 2001 From: Nicholas Tindle Date: Thu, 13 Jun 2024 16:27:16 -0500 Subject: [PATCH 5/5] fix(forge): revert changes here. Addressed in #7214 --- forge/forge/config/config.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/forge/forge/config/config.py b/forge/forge/config/config.py index 6eef0706a05b..27b57cb5a2a6 100644 --- a/forge/forge/config/config.py +++ b/forge/forge/config/config.py @@ -210,7 +210,7 @@ def build_config_from_env(cls, project_root: Path = PROJECT_ROOT) -> Config: def assert_config_has_openai_api_key(config: Config) -> None: """Check if the OpenAI API key is set in config.py or as an environment variable.""" - key_pattern = r"^sk-(?:proj-|wa-)?\w{48}" + key_pattern = r"^sk-(proj-)?\w{48}" openai_api_key = ( config.openai_credentials.api_key.get_secret_value() if config.openai_credentials