mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-05-04 14:40:15 +00:00
update
This commit is contained in:
parent
c8a0a21ef2
commit
3f21c2b2c2
83 changed files with 6355 additions and 0 deletions
52
server/app/controller/chat/history_controller.py
Normal file
52
server/app/controller/chat/history_controller.py
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||
from fastapi_pagination import Page
|
||||
from fastapi_pagination.ext.sqlmodel import paginate
|
||||
from app.model.chat.chat_history import ChatHistoryOut, ChatHistoryIn, ChatHistory, ChatHistoryUpdate
|
||||
from fastapi_babel import _
|
||||
from sqlmodel import Session, select, desc
|
||||
from app.component.auth import Auth, auth_must
|
||||
from app.component.database import session
|
||||
|
||||
router = APIRouter(prefix="/chat", tags=["Chat History"])
|
||||
|
||||
|
||||
@router.post("/history", name="save chat history", response_model=ChatHistoryOut)
|
||||
def create_chat_history(data: ChatHistoryIn, session: Session = Depends(session), auth: Auth = Depends(auth_must)):
|
||||
data.user_id = auth.user.id
|
||||
chat_history = ChatHistory(**data.model_dump())
|
||||
session.add(chat_history)
|
||||
session.commit()
|
||||
session.refresh(chat_history)
|
||||
return chat_history
|
||||
|
||||
|
||||
@router.get("/histories", name="get chat history")
|
||||
def list_chat_history(session: Session = Depends(session), auth: Auth = Depends(auth_must)) -> Page[ChatHistoryOut]:
|
||||
stmt = select(ChatHistory).where(ChatHistory.user_id == auth.user.id).order_by(desc(ChatHistory.created_at))
|
||||
return paginate(session, stmt)
|
||||
|
||||
|
||||
@router.delete("/history/{history_id}", name="delete chat history")
|
||||
def delete_chat_history(history_id: str, session: Session = Depends(session)):
|
||||
history = session.exec(select(ChatHistory).where(ChatHistory.id == history_id)).first()
|
||||
if not history:
|
||||
raise HTTPException(status_code=404, detail="Caht History not found")
|
||||
session.delete(history)
|
||||
session.commit()
|
||||
return Response(status_code=204)
|
||||
|
||||
|
||||
@router.put("/history/{history_id}", name="update chat history", response_model=ChatHistoryOut)
|
||||
def update_chat_history(
|
||||
history_id: int, data: ChatHistoryUpdate, session: Session = Depends(session), auth: Auth = Depends(auth_must)
|
||||
):
|
||||
history = session.exec(select(ChatHistory).where(ChatHistory.id == history_id)).first()
|
||||
if not history:
|
||||
raise HTTPException(status_code=404, detail="Chat History not found")
|
||||
if history.user_id != auth.user.id:
|
||||
raise HTTPException(status_code=403, detail="You are not allowed to update this chat history")
|
||||
update_data = data.model_dump(exclude_unset=True)
|
||||
history.update_fields(update_data)
|
||||
history.save(session)
|
||||
session.refresh(history)
|
||||
return history
|
||||
78
server/app/controller/chat/share_controller.py
Normal file
78
server/app/controller/chat/share_controller.py
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||
from sqlmodel import Session, asc, select
|
||||
from app.component.database import session
|
||||
import json
|
||||
import asyncio
|
||||
from itsdangerous import SignatureExpired, BadTimeSignature
|
||||
from starlette.responses import StreamingResponse
|
||||
from app.model.chat.chat_share import ChatHistoryShareOut, ChatShare, ChatShareIn
|
||||
from app.model.chat.chat_step import ChatStep
|
||||
from app.model.chat.chat_history import ChatHistory
|
||||
|
||||
router = APIRouter(prefix="/chat", tags=["Chat Share"])
|
||||
|
||||
|
||||
@router.get("/share/info/{token}", name="Get shared chat info", response_model=ChatHistoryShareOut)
|
||||
def get_share_info(token: str, session: Session = Depends(session)):
|
||||
"""
|
||||
Get shared chat history info by token, excluding sensitive data.
|
||||
"""
|
||||
try:
|
||||
task_id = ChatShare.verify_token(token, False)
|
||||
except (SignatureExpired, BadTimeSignature):
|
||||
raise HTTPException(status_code=400, detail="Share link is invalid or has expired.")
|
||||
|
||||
stmt = select(ChatHistory).where(ChatHistory.task_id == task_id)
|
||||
history = session.exec(stmt).one_or_none()
|
||||
|
||||
if not history:
|
||||
raise HTTPException(status_code=404, detail="Chat history not found.")
|
||||
|
||||
return history
|
||||
|
||||
|
||||
@router.get("/share/playback/{token}", name="Playback shared chat via SSE")
|
||||
async def share_playback(token: str, session: Session = Depends(session), delay_time: float = 0):
|
||||
"""
|
||||
Playbacks the chat history via a sharing token (SSE).
|
||||
delay_time: control sse interval, max 5 seconds
|
||||
"""
|
||||
if delay_time > 5:
|
||||
delay_time = 5
|
||||
try:
|
||||
task_id = ChatShare.verify_token(token, False)
|
||||
except SignatureExpired:
|
||||
raise HTTPException(status_code=400, detail="Share link has expired.")
|
||||
except BadTimeSignature:
|
||||
raise HTTPException(status_code=400, detail="Share link is invalid.")
|
||||
|
||||
async def event_generator():
|
||||
stmt = select(ChatStep).where(ChatStep.task_id == task_id).order_by(asc(ChatStep.id))
|
||||
steps = session.exec(stmt).all()
|
||||
|
||||
if not steps:
|
||||
yield f"data: {json.dumps({'error': 'No steps found for this task.'})}\n\n"
|
||||
return
|
||||
|
||||
for step in steps:
|
||||
step_data = {
|
||||
"id": step.id,
|
||||
"task_id": step.task_id,
|
||||
"step": step.step,
|
||||
"data": step.data,
|
||||
"created_at": step.created_at.isoformat() if step.created_at else None,
|
||||
}
|
||||
yield f"data: {json.dumps(step_data)}\n\n"
|
||||
if delay_time > 0 and step.step != "create_agent":
|
||||
await asyncio.sleep(delay_time)
|
||||
|
||||
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
||||
|
||||
|
||||
@router.post("/share", name="Generate sharable link for a task(1 day expiration)")
|
||||
def create_share_link(data: ChatShareIn):
|
||||
"""
|
||||
Generates a sharing token with an expiration time for the specified task_id.
|
||||
"""
|
||||
share_token = ChatShare.generate_token(data.task_id)
|
||||
return {"share_token": share_token}
|
||||
81
server/app/controller/chat/snapshot_controller.py
Normal file
81
server/app/controller/chat/snapshot_controller.py
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
from app.model.chat.chat_snpshot import ChatSnapshot, ChatSnapshotIn
|
||||
from typing import List, Optional
|
||||
from fastapi import Depends, HTTPException, Response, APIRouter
|
||||
from sqlmodel import Session, select
|
||||
from app.component.database import session
|
||||
from app.component.auth import Auth, auth_must
|
||||
from fastapi_babel import _
|
||||
|
||||
router = APIRouter(prefix="/chat", tags=["Chat Snapshot Management"])
|
||||
|
||||
|
||||
@router.get("/snapshots", name="list chat snapshots", response_model=List[ChatSnapshot])
|
||||
async def list_chat_snapshots(
|
||||
api_task_id: Optional[str] = None,
|
||||
camel_task_id: Optional[str] = None,
|
||||
browser_url: Optional[str] = None,
|
||||
session: Session = Depends(session),
|
||||
):
|
||||
query = select(ChatSnapshot)
|
||||
if api_task_id is not None:
|
||||
query = query.where(ChatSnapshot.api_task_id == api_task_id)
|
||||
if camel_task_id is not None:
|
||||
query = query.where(ChatSnapshot.camel_task_id == camel_task_id)
|
||||
if browser_url is not None:
|
||||
query = query.where(ChatSnapshot.browser_url == browser_url)
|
||||
snapshots = session.exec(query).all()
|
||||
return snapshots
|
||||
|
||||
|
||||
@router.get("/snapshots/{snapshot_id}", name="get chat snapshot", response_model=ChatSnapshot)
|
||||
async def get_chat_snapshot(snapshot_id: int, session: Session = Depends(session), auth: Auth = Depends(auth_must)):
|
||||
snapshot = session.get(ChatSnapshot, snapshot_id)
|
||||
if not snapshot:
|
||||
raise HTTPException(status_code=404, detail=_("Chat snapshot not found"))
|
||||
return snapshot
|
||||
|
||||
|
||||
@router.post("/snapshots", name="create chat snapshot", response_model=ChatSnapshot)
|
||||
async def create_chat_snapshot(
|
||||
snapshot: ChatSnapshotIn, auth: Auth = Depends(auth_must), session: Session = Depends(session)
|
||||
):
|
||||
image_path = ChatSnapshotIn.save_image(auth.user.id, snapshot.api_task_id, snapshot.image_base64)
|
||||
chat_snapshot = ChatSnapshot(
|
||||
user_id=auth.user.id,
|
||||
api_task_id=snapshot.api_task_id,
|
||||
camel_task_id=snapshot.camel_task_id,
|
||||
browser_url=snapshot.browser_url,
|
||||
image_path=image_path,
|
||||
)
|
||||
session.add(chat_snapshot)
|
||||
session.commit()
|
||||
session.refresh(chat_snapshot)
|
||||
return Response(status_code=200)
|
||||
|
||||
|
||||
@router.put("/snapshots/{snapshot_id}", name="update chat snapshot", response_model=ChatSnapshot)
|
||||
async def update_chat_snapshot(
|
||||
snapshot_id: int,
|
||||
snapshot_update: ChatSnapshot,
|
||||
session: Session = Depends(session),
|
||||
auth: Auth = Depends(auth_must),
|
||||
):
|
||||
db_snapshot = session.get(ChatSnapshot, snapshot_id)
|
||||
if not db_snapshot:
|
||||
raise HTTPException(status_code=404, detail=_("Chat snapshot not found"))
|
||||
for key, value in snapshot_update.dict(exclude_unset=True).items():
|
||||
setattr(db_snapshot, key, value)
|
||||
session.add(db_snapshot)
|
||||
session.commit()
|
||||
session.refresh(db_snapshot)
|
||||
return db_snapshot
|
||||
|
||||
|
||||
@router.delete("/snapshots/{snapshot_id}", name="delete chat snapshot")
|
||||
async def delete_chat_snapshot(snapshot_id: int, session: Session = Depends(session), auth: Auth = Depends(auth_must)):
|
||||
db_snapshot = session.get(ChatSnapshot, snapshot_id)
|
||||
if not db_snapshot:
|
||||
raise HTTPException(status_code=404, detail=_("Chat snapshot not found"))
|
||||
session.delete(db_snapshot)
|
||||
session.commit()
|
||||
return Response(status_code=204)
|
||||
105
server/app/controller/chat/step_controller.py
Normal file
105
server/app/controller/chat/step_controller.py
Normal file
|
|
@ -0,0 +1,105 @@
|
|||
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 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
|
||||
|
||||
router = APIRouter(prefix="/chat", tags=["Chat Step Management"])
|
||||
|
||||
|
||||
@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)
|
||||
):
|
||||
query = select(ChatStep)
|
||||
if task_id is not None:
|
||||
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()
|
||||
return chat_steps
|
||||
|
||||
|
||||
@router.get("/steps/playback/{task_id}", name="Playback Chat Step via SSE")
|
||||
async def share_playback(
|
||||
task_id: str, delay_time: float = 0, session: Session = Depends(session), auth: Auth = Depends(auth_must)
|
||||
):
|
||||
"""
|
||||
Playbacks the chat steps (SSE).
|
||||
"""
|
||||
if delay_time > 5:
|
||||
delay_time = 5
|
||||
|
||||
async def event_generator():
|
||||
stmt = select(ChatStep).where(ChatStep.task_id == task_id).order_by(asc(ChatStep.id))
|
||||
steps = session.exec(stmt).all()
|
||||
|
||||
if not steps:
|
||||
yield f"data: {json.dumps({'error': 'No steps found for this task.'})}\n\n"
|
||||
return
|
||||
|
||||
for step in steps:
|
||||
step_data = {
|
||||
"id": step.id,
|
||||
"task_id": step.task_id,
|
||||
"step": step.step,
|
||||
"data": step.data,
|
||||
"created_at": step.created_at.isoformat() if step.created_at else None,
|
||||
}
|
||||
yield f"data: {json.dumps(step_data)}\n\n"
|
||||
if delay_time > 0:
|
||||
await asyncio.sleep(delay_time)
|
||||
|
||||
return StreamingResponse(event_generator(), media_type="text/event-stream")
|
||||
|
||||
|
||||
@router.get("/steps/{step_id}", name="get chat step", response_model=ChatStepOut)
|
||||
async def get_chat_step(step_id: int, session: Session = Depends(session), auth: Auth = Depends(auth_must)):
|
||||
chat_step = session.get(ChatStep, step_id)
|
||||
if not chat_step:
|
||||
raise HTTPException(status_code=404, detail=_("Chat step not found"))
|
||||
return chat_step
|
||||
|
||||
|
||||
@router.post("/steps", name="create chat step")
|
||||
# TODO Limit request sources
|
||||
async def create_chat_step(step: ChatStepIn, session: Session = Depends(session)):
|
||||
chat_step = ChatStep(
|
||||
task_id=step.task_id,
|
||||
step=step.step,
|
||||
data=step.data,
|
||||
)
|
||||
session.add(chat_step)
|
||||
session.commit()
|
||||
session.refresh(chat_step)
|
||||
return {"code": 200, "msg": "success"}
|
||||
|
||||
|
||||
@router.put("/steps/{step_id}", name="update chat step", response_model=ChatStepOut)
|
||||
async def update_chat_step(
|
||||
step_id: int, chat_step_update: ChatStep, session: Session = Depends(session), auth: Auth = Depends(auth_must)
|
||||
):
|
||||
db_chat_step = session.get(ChatStep, step_id)
|
||||
if not db_chat_step:
|
||||
raise HTTPException(status_code=404, detail=_("Chat step not found"))
|
||||
for key, value in chat_step_update.dict(exclude_unset=True).items():
|
||||
setattr(db_chat_step, key, value)
|
||||
session.add(db_chat_step)
|
||||
session.commit()
|
||||
session.refresh(db_chat_step)
|
||||
return db_chat_step
|
||||
|
||||
|
||||
@router.delete("/steps/{step_id}", name="delete chat step")
|
||||
async def delete_chat_step(step_id: int, session: Session = Depends(session), auth: Auth = Depends(auth_must)):
|
||||
db_chat_step = session.get(ChatStep, step_id)
|
||||
if not db_chat_step:
|
||||
raise HTTPException(status_code=404, detail=_("Chat step not found"))
|
||||
session.delete(db_chat_step)
|
||||
session.commit()
|
||||
return Response(status_code=204)
|
||||
Loading…
Add table
Add a link
Reference in a new issue