mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-05-05 15:30:48 +00:00
chore: fix pre commit format and pipeline issue (#1144)
This commit is contained in:
parent
2256497dff
commit
893f51fc82
970 changed files with 11235 additions and 9533 deletions
|
|
@ -14,25 +14,26 @@
|
|||
|
||||
import asyncio
|
||||
import json
|
||||
from typing import List, Optional
|
||||
from fastapi import Depends, HTTPException, Query, Response, APIRouter
|
||||
from fastapi.responses import StreamingResponse
|
||||
from sqlmodel import Session, asc, select
|
||||
from sqlalchemy.sql.expression import case
|
||||
from app.component.database import session
|
||||
from app.component.auth import Auth, auth_must
|
||||
from fastapi_babel import _
|
||||
from app.model.chat.chat_step import ChatStep, ChatStepOut, ChatStepIn
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||
from fastapi.responses import StreamingResponse
|
||||
from fastapi_babel import _
|
||||
from sqlalchemy.sql.expression import case
|
||||
from sqlmodel import Session, asc, select
|
||||
|
||||
from app.component.auth import Auth, auth_must
|
||||
from app.component.database import session
|
||||
from app.model.chat.chat_step import ChatStep, ChatStepIn, ChatStepOut
|
||||
|
||||
logger = logging.getLogger("server_chat_step")
|
||||
|
||||
router = APIRouter(prefix="/chat", tags=["Chat Step Management"])
|
||||
|
||||
|
||||
@router.get("/steps", name="list chat steps", response_model=List[ChatStepOut])
|
||||
@router.get("/steps", name="list chat steps", response_model=list[ChatStepOut])
|
||||
async def list_chat_steps(
|
||||
task_id: str, step: Optional[str] = None, session: Session = Depends(session), auth: Auth = Depends(auth_must)
|
||||
task_id: str, step: str | None = None, session: Session = Depends(session), auth: Auth = Depends(auth_must)
|
||||
):
|
||||
"""List chat steps for a task with optional step type filtering."""
|
||||
user_id = auth.user.id
|
||||
|
|
@ -41,9 +42,11 @@ async def list_chat_steps(
|
|||
query = query.where(ChatStep.task_id == task_id)
|
||||
if step is not None:
|
||||
query = query.where(ChatStep.step == step)
|
||||
|
||||
|
||||
chat_steps = session.exec(query).all()
|
||||
logger.debug("Chat steps listed", extra={"user_id": user_id, "task_id": task_id, "step_type": step, "count": len(chat_steps)})
|
||||
logger.debug(
|
||||
"Chat steps listed", extra={"user_id": user_id, "task_id": task_id, "step_type": step, "count": len(chat_steps)}
|
||||
)
|
||||
return chat_steps
|
||||
|
||||
|
||||
|
|
@ -54,15 +57,19 @@ async def share_playback(
|
|||
"""Playback chat steps via SSE stream."""
|
||||
user_id = auth.user.id
|
||||
if delay_time > 5:
|
||||
logger.debug("Delay time capped", extra={"user_id": user_id, "task_id": task_id, "requested": delay_time, "capped": 5})
|
||||
logger.debug(
|
||||
"Delay time capped", extra={"user_id": user_id, "task_id": task_id, "requested": delay_time, "capped": 5}
|
||||
)
|
||||
delay_time = 5
|
||||
|
||||
async def event_generator():
|
||||
try:
|
||||
stmt = select(ChatStep).where(ChatStep.task_id == task_id).order_by(
|
||||
asc(case((ChatStep.timestamp.is_(None), 1), else_=0)),
|
||||
asc(ChatStep.timestamp),
|
||||
asc(ChatStep.id)
|
||||
stmt = (
|
||||
select(ChatStep)
|
||||
.where(ChatStep.task_id == task_id)
|
||||
.order_by(
|
||||
asc(case((ChatStep.timestamp.is_(None), 1), else_=0)), asc(ChatStep.timestamp), asc(ChatStep.id)
|
||||
)
|
||||
)
|
||||
steps = session.exec(stmt).all()
|
||||
|
||||
|
|
@ -71,8 +78,11 @@ async def share_playback(
|
|||
yield f"data: {json.dumps({'error': 'No steps found for this task.'})}\n\n"
|
||||
return
|
||||
|
||||
logger.info("Chat step playback started", extra={"user_id": user_id, "task_id": task_id, "step_count": len(steps), "delay_time": delay_time})
|
||||
|
||||
logger.info(
|
||||
"Chat step playback started",
|
||||
extra={"user_id": user_id, "task_id": task_id, "step_count": len(steps), "delay_time": delay_time},
|
||||
)
|
||||
|
||||
for step in steps:
|
||||
step_data = {
|
||||
"id": step.id,
|
||||
|
|
@ -85,10 +95,15 @@ async def share_playback(
|
|||
if delay_time > 0:
|
||||
await asyncio.sleep(delay_time)
|
||||
|
||||
|
||||
logger.info("Chat step playback completed", extra={"user_id": user_id, "task_id": task_id, "step_count": len(steps)})
|
||||
logger.info(
|
||||
"Chat step playback completed", extra={"user_id": user_id, "task_id": task_id, "step_count": len(steps)}
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("Chat step playback error", extra={"user_id": user_id, "task_id": task_id, "error": str(e)}, exc_info=True)
|
||||
logger.error(
|
||||
"Chat step playback error",
|
||||
extra={"user_id": user_id, "task_id": task_id, "error": str(e)},
|
||||
exc_info=True,
|
||||
)
|
||||
yield f"data: {json.dumps({'error': 'Playback error occurred.'})}\n\n"
|
||||
|
||||
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
||||
|
|
@ -99,11 +114,11 @@ async def get_chat_step(step_id: int, session: Session = Depends(session), auth:
|
|||
"""Get specific chat step."""
|
||||
user_id = auth.user.id
|
||||
chat_step = session.get(ChatStep, step_id)
|
||||
|
||||
|
||||
if not chat_step:
|
||||
logger.warning("Chat step not found", extra={"user_id": user_id, "step_id": step_id})
|
||||
raise HTTPException(status_code=404, detail=_("Chat step not found"))
|
||||
|
||||
|
||||
logger.debug("Chat step retrieved", extra={"user_id": user_id, "step_id": step_id, "task_id": chat_step.task_id})
|
||||
return chat_step
|
||||
|
||||
|
|
@ -112,20 +127,21 @@ async def get_chat_step(step_id: int, session: Session = Depends(session), auth:
|
|||
async def create_chat_step(step: ChatStepIn, session: Session = Depends(session)):
|
||||
"""Create new chat step. TODO: Implement request source validation."""
|
||||
try:
|
||||
chat_step = ChatStep(
|
||||
task_id=step.task_id,
|
||||
step=step.step,
|
||||
data=step.data,
|
||||
timestamp=step.timestamp
|
||||
)
|
||||
chat_step = ChatStep(task_id=step.task_id, step=step.step, data=step.data, timestamp=step.timestamp)
|
||||
session.add(chat_step)
|
||||
session.commit()
|
||||
session.refresh(chat_step)
|
||||
logger.info("Chat step created", extra={"step_id": chat_step.id, "task_id": step.task_id, "step_type": step.step})
|
||||
logger.info(
|
||||
"Chat step created", extra={"step_id": chat_step.id, "task_id": step.task_id, "step_type": step.step}
|
||||
)
|
||||
return {"code": 200, "msg": "success"}
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
logger.error("Chat step creation failed", extra={"task_id": step.task_id, "step_type": step.step, "error": str(e)}, exc_info=True)
|
||||
logger.error(
|
||||
"Chat step creation failed",
|
||||
extra={"task_id": step.task_id, "step_type": step.step, "error": str(e)},
|
||||
exc_info=True,
|
||||
)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
|
|
@ -136,7 +152,7 @@ async def update_chat_step(
|
|||
"""Update chat step."""
|
||||
user_id = auth.user.id
|
||||
db_chat_step = session.get(ChatStep, step_id)
|
||||
|
||||
|
||||
if not db_chat_step:
|
||||
logger.warning("Chat step not found for update", extra={"user_id": user_id, "step_id": step_id})
|
||||
raise HTTPException(status_code=404, detail=_("Chat step not found"))
|
||||
|
|
@ -148,11 +164,21 @@ async def update_chat_step(
|
|||
session.add(db_chat_step)
|
||||
session.commit()
|
||||
session.refresh(db_chat_step)
|
||||
logger.info("Chat step updated", extra={"user_id": user_id, "step_id": step_id, "task_id": db_chat_step.task_id, "fields_updated": list(update_data.keys())})
|
||||
logger.info(
|
||||
"Chat step updated",
|
||||
extra={
|
||||
"user_id": user_id,
|
||||
"step_id": step_id,
|
||||
"task_id": db_chat_step.task_id,
|
||||
"fields_updated": list(update_data.keys()),
|
||||
},
|
||||
)
|
||||
return db_chat_step
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
logger.error("Chat step update failed", extra={"user_id": user_id, "step_id": step_id, "error": str(e)}, exc_info=True)
|
||||
logger.error(
|
||||
"Chat step update failed", extra={"user_id": user_id, "step_id": step_id, "error": str(e)}, exc_info=True
|
||||
)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
||||
|
||||
|
|
@ -161,7 +187,7 @@ async def delete_chat_step(step_id: int, session: Session = Depends(session), au
|
|||
"""Delete chat step."""
|
||||
user_id = auth.user.id
|
||||
db_chat_step = session.get(ChatStep, step_id)
|
||||
|
||||
|
||||
if not db_chat_step:
|
||||
logger.warning("Chat step not found for deletion", extra={"user_id": user_id, "step_id": step_id})
|
||||
raise HTTPException(status_code=404, detail=_("Chat step not found"))
|
||||
|
|
@ -169,9 +195,13 @@ async def delete_chat_step(step_id: int, session: Session = Depends(session), au
|
|||
try:
|
||||
session.delete(db_chat_step)
|
||||
session.commit()
|
||||
logger.info("Chat step deleted", extra={"user_id": user_id, "step_id": step_id, "task_id": db_chat_step.task_id})
|
||||
logger.info(
|
||||
"Chat step deleted", extra={"user_id": user_id, "step_id": step_id, "task_id": db_chat_step.task_id}
|
||||
)
|
||||
return Response(status_code=204)
|
||||
except Exception as e:
|
||||
session.rollback()
|
||||
logger.error("Chat step deletion failed", extra={"user_id": user_id, "step_id": step_id, "error": str(e)}, exc_info=True)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
logger.error(
|
||||
"Chat step deletion failed", extra={"user_id": user_id, "step_id": step_id, "error": str(e)}, exc_info=True
|
||||
)
|
||||
raise HTTPException(status_code=500, detail="Internal server error")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue