Refactor "created_at" column type for consistency and fix cancel middleware (#2316)

* chore: update linting workflows to include dev branch in merge_group

* Update README.md

Add 1.0 banner

* Update README.md

* chore: update package versions in pyproject.toml files

* refactor: update "created_at" column type to use the "sa" module for consistency

* Update README.md

Add 1.0 banner

* chore: Remove unused import in ToolCallingAgent.py

* fix: adapt RequestCancelledMiddleware to handle cancelled requests

* chore: Remove unused import in test_helper_components.py

* refactor: Declare queue variable with explicit type in RequestCancelledMiddleware

---------

Co-authored-by: Rodrigo Nader <rodrigosilvanader@gmail.com>
This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-06-24 12:39:49 -07:00 committed by GitHub
commit 77e23d9442
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 25 additions and 30 deletions

View file

@ -34,7 +34,7 @@ def upgrade() -> None:
sa.Column("provider", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
sa.Column("user_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
sa.Column("created_at", sqlmodel.sql.sqltypes.DateTime(), nullable=False),
sa.Column("created_at", sa.DateTime(), nullable=False),
sa.Column("updated_at", sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint("id"),
)

View file

@ -2,7 +2,6 @@ from typing import Dict, List, cast
from langchain.agents import AgentExecutor, BaseSingleActionAgent
from langchain.agents.tool_calling_agent.base import create_tool_calling_agent
from langchain_core.messages import BaseMessage
from langchain_core.prompts import ChatPromptTemplate
from langflow.custom import Component

View file

@ -6,7 +6,7 @@ from typing import Optional
from urllib.parse import urlencode
import nest_asyncio # type: ignore
from fastapi import FastAPI, Request
from fastapi import FastAPI, Request, Response
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from fastapi.staticfiles import StaticFiles
@ -32,22 +32,17 @@ from langflow.utils.logger import configure
warnings.filterwarnings("ignore", category=PydanticDeprecatedSince20)
class RequestCancelledMiddleware:
class RequestCancelledMiddleware(BaseHTTPMiddleware):
def __init__(self, app):
self.app = app
super().__init__(app)
async def __call__(self, scope, receive, send):
if scope["type"] != "http":
await self.app(scope, receive, send)
return
async def dispatch(self, request: Request, call_next):
queue: asyncio.Queue = asyncio.Queue()
# Let's make a shared queue for the request messages
queue = asyncio.Queue()
async def message_poller(sentinel, handler_task):
async def message_poller(sentinel, handler_task, request):
nonlocal queue
while True:
message = await receive()
message = await request.receive
if message["type"] == "http.disconnect":
handler_task.cancel()
return sentinel # Break the loop
@ -56,13 +51,14 @@ class RequestCancelledMiddleware:
await queue.put(message)
sentinel = object()
handler_task = asyncio.create_task(self.app(scope, queue.get, send))
asyncio.create_task(message_poller(sentinel, handler_task))
handler_task = asyncio.create_task(call_next(request))
asyncio.create_task(message_poller(sentinel, handler_task, request))
try:
return await handler_task
response = await handler_task
return response
except asyncio.CancelledError:
pass
return Response("Request was cancelled", status_code=499)
class JavaScriptMIMETypeMiddleware(BaseHTTPMiddleware):