Skip to content

Commit

Permalink
FS-133 Make scratchpad per request
Browse files Browse the repository at this point in the history
  • Loading branch information
mic-smith committed Dec 12, 2024
1 parent 27a853f commit 72629cf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions backend/src/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from fastapi import FastAPI, HTTPException, Response, WebSocket, UploadFile
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
from src.utils.scratchpad import ScratchPadMiddleware
from src.chat_storage_service import get_chat_message
from src.directors.report_director import report_on_file_upload
from src.session.file_uploads import clear_session_file_uploads
Expand Down Expand Up @@ -45,6 +46,7 @@ async def lifespan(app: FastAPI):
)

app.add_middleware(RedisSessionMiddleware)
app.add_middleware(ScratchPadMiddleware)

health_prefix = "InferESG healthcheck: "
further_guidance = "Please check the README files for further guidance."
Expand Down
16 changes: 11 additions & 5 deletions backend/src/utils/scratchpad.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import TypedDict
from starlette.middleware.base import BaseHTTPMiddleware
from starlette.requests import Request
import contextvars
import logging

logger = logging.getLogger(__name__)
Expand All @@ -10,20 +13,23 @@ class Answer(TypedDict):
result: str | None
error: str | None

scratchpad_context = contextvars.ContextVar("scratchpad", default=[])

Scratchpad = list[Answer]

scratchpad: Scratchpad = []

class ScratchPadMiddleware(BaseHTTPMiddleware):
async def dispatch(self, request: Request, call_next):
scratchpad_context.set([])
return await call_next(request)

def get_scratchpad() -> Scratchpad:
return scratchpad
return scratchpad_context.get()


def update_scratchpad(agent_name=None, question=None, result=None, error=None):
scratchpad.append({"agent_name": agent_name, "question": question, "result": result, "error": error})
get_scratchpad().append({"agent_name": agent_name, "question": question, "result": result, "error": error})


def clear_scratchpad():
logger.debug("Scratchpad cleared")
scratchpad.clear()
get_scratchpad().clear()

0 comments on commit 72629cf

Please sign in to comment.