aboutsummaryrefslogtreecommitdiff
path: root/autogpts/forge/forge/sdk/middlewares.py
diff options
context:
space:
mode:
Diffstat (limited to 'autogpts/forge/forge/sdk/middlewares.py')
-rw-r--r--autogpts/forge/forge/sdk/middlewares.py34
1 files changed, 34 insertions, 0 deletions
diff --git a/autogpts/forge/forge/sdk/middlewares.py b/autogpts/forge/forge/sdk/middlewares.py
new file mode 100644
index 000000000..92945a95c
--- /dev/null
+++ b/autogpts/forge/forge/sdk/middlewares.py
@@ -0,0 +1,34 @@
+from fastapi import FastAPI
+
+
+class AgentMiddleware:
+ """
+ Middleware that injects the agent instance into the request scope.
+ """
+
+ def __init__(self, app: FastAPI, agent: "Agent"):
+ """
+
+ Args:
+ app: The FastAPI app - automatically injected by FastAPI.
+ agent: The agent instance to inject into the request scope.
+
+ Examples:
+ >>> from fastapi import FastAPI, Request
+ >>> from agent_protocol.agent import Agent
+ >>> from agent_protocol.middlewares import AgentMiddleware
+ >>> app = FastAPI()
+ >>> @app.get("/")
+ >>> async def root(request: Request):
+ >>> agent = request["agent"]
+ >>> task = agent.db.create_task("Do something.")
+ >>> return {"task_id": a.task_id}
+ >>> agent = Agent()
+ >>> app.add_middleware(AgentMiddleware, agent=agent)
+ """
+ self.app = app
+ self.agent = agent
+
+ async def __call__(self, scope, receive, send):
+ scope["agent"] = self.agent
+ await self.app(scope, receive, send)