251 lines
7.6 KiB
Python
251 lines
7.6 KiB
Python
from fastapi import APIRouter, Depends, HTTPException
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Optional
|
|
import uuid
|
|
from datetime import datetime
|
|
|
|
from ..db import get_db
|
|
from ..models import Assistant, Voice, Workflow
|
|
from ..schemas import (
|
|
AssistantCreate, AssistantUpdate, AssistantOut,
|
|
VoiceCreate, VoiceUpdate, VoiceOut,
|
|
WorkflowCreate, WorkflowUpdate, WorkflowOut
|
|
)
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# ============ Voices ============
|
|
@router.get("/voices")
|
|
def list_voices(
|
|
vendor: Optional[str] = None,
|
|
language: Optional[str] = None,
|
|
gender: Optional[str] = None,
|
|
page: int = 1,
|
|
limit: int = 50,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取声音库列表"""
|
|
query = db.query(Voice)
|
|
if vendor:
|
|
query = query.filter(Voice.vendor == vendor)
|
|
if language:
|
|
query = query.filter(Voice.language == language)
|
|
if gender:
|
|
query = query.filter(Voice.gender == gender)
|
|
|
|
total = query.count()
|
|
voices = query.order_by(Voice.created_at.desc()) \
|
|
.offset((page-1)*limit).limit(limit).all()
|
|
return {"total": total, "page": page, "limit": limit, "list": voices}
|
|
|
|
|
|
@router.post("/voices", response_model=VoiceOut)
|
|
def create_voice(data: VoiceCreate, db: Session = Depends(get_db)):
|
|
"""创建声音"""
|
|
voice = Voice(
|
|
id=data.id or str(uuid.uuid4())[:8],
|
|
user_id=1,
|
|
name=data.name,
|
|
vendor=data.vendor,
|
|
gender=data.gender,
|
|
language=data.language,
|
|
description=data.description,
|
|
model=data.model,
|
|
voice_key=data.voice_key,
|
|
speed=data.speed,
|
|
gain=data.gain,
|
|
pitch=data.pitch,
|
|
enabled=data.enabled,
|
|
)
|
|
db.add(voice)
|
|
db.commit()
|
|
db.refresh(voice)
|
|
return voice
|
|
|
|
|
|
@router.get("/voices/{id}", response_model=VoiceOut)
|
|
def get_voice(id: str, db: Session = Depends(get_db)):
|
|
"""获取单个声音详情"""
|
|
voice = db.query(Voice).filter(Voice.id == id).first()
|
|
if not voice:
|
|
raise HTTPException(status_code=404, detail="Voice not found")
|
|
return voice
|
|
|
|
|
|
@router.put("/voices/{id}", response_model=VoiceOut)
|
|
def update_voice(id: str, data: VoiceUpdate, db: Session = Depends(get_db)):
|
|
"""更新声音"""
|
|
voice = db.query(Voice).filter(Voice.id == id).first()
|
|
if not voice:
|
|
raise HTTPException(status_code=404, detail="Voice not found")
|
|
|
|
update_data = data.model_dump(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(voice, field, value)
|
|
|
|
db.commit()
|
|
db.refresh(voice)
|
|
return voice
|
|
|
|
|
|
@router.delete("/voices/{id}")
|
|
def delete_voice(id: str, db: Session = Depends(get_db)):
|
|
"""删除声音"""
|
|
voice = db.query(Voice).filter(Voice.id == id).first()
|
|
if not voice:
|
|
raise HTTPException(status_code=404, detail="Voice not found")
|
|
db.delete(voice)
|
|
db.commit()
|
|
return {"message": "Deleted successfully"}
|
|
|
|
|
|
# ============ Assistants ============
|
|
@router.get("/assistants")
|
|
def list_assistants(
|
|
page: int = 1,
|
|
limit: int = 50,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取助手列表"""
|
|
query = db.query(Assistant)
|
|
total = query.count()
|
|
assistants = query.order_by(Assistant.created_at.desc()) \
|
|
.offset((page-1)*limit).limit(limit).all()
|
|
return {"total": total, "page": page, "limit": limit, "list": assistants}
|
|
|
|
|
|
@router.get("/assistants/{id}", response_model=AssistantOut)
|
|
def get_assistant(id: str, db: Session = Depends(get_db)):
|
|
"""获取单个助手详情"""
|
|
assistant = db.query(Assistant).filter(Assistant.id == id).first()
|
|
if not assistant:
|
|
raise HTTPException(status_code=404, detail="Assistant not found")
|
|
return assistant
|
|
|
|
|
|
@router.post("/assistants", response_model=AssistantOut)
|
|
def create_assistant(data: AssistantCreate, db: Session = Depends(get_db)):
|
|
"""创建新助手"""
|
|
assistant = Assistant(
|
|
id=str(uuid.uuid4())[:8],
|
|
user_id=1, # 默认用户,后续添加认证
|
|
name=data.name,
|
|
opener=data.opener,
|
|
prompt=data.prompt,
|
|
knowledge_base_id=data.knowledgeBaseId,
|
|
language=data.language,
|
|
voice=data.voice,
|
|
speed=data.speed,
|
|
hotwords=data.hotwords,
|
|
tools=data.tools,
|
|
interruption_sensitivity=data.interruptionSensitivity,
|
|
config_mode=data.configMode,
|
|
api_url=data.apiUrl,
|
|
api_key=data.apiKey,
|
|
)
|
|
db.add(assistant)
|
|
db.commit()
|
|
db.refresh(assistant)
|
|
return assistant
|
|
|
|
|
|
@router.put("/assistants/{id}")
|
|
def update_assistant(id: str, data: AssistantUpdate, db: Session = Depends(get_db)):
|
|
"""更新助手"""
|
|
assistant = db.query(Assistant).filter(Assistant.id == id).first()
|
|
if not assistant:
|
|
raise HTTPException(status_code=404, detail="Assistant not found")
|
|
|
|
update_data = data.model_dump(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(assistant, field, value)
|
|
|
|
assistant.updated_at = datetime.utcnow()
|
|
db.commit()
|
|
db.refresh(assistant)
|
|
return assistant
|
|
|
|
|
|
@router.delete("/assistants/{id}")
|
|
def delete_assistant(id: str, db: Session = Depends(get_db)):
|
|
"""删除助手"""
|
|
assistant = db.query(Assistant).filter(Assistant.id == id).first()
|
|
if not assistant:
|
|
raise HTTPException(status_code=404, detail="Assistant not found")
|
|
db.delete(assistant)
|
|
db.commit()
|
|
return {"message": "Deleted successfully"}
|
|
|
|
|
|
# ============ Workflows ============
|
|
@router.get("/workflows", response_model=List[WorkflowOut])
|
|
def list_workflows(
|
|
page: int = 1,
|
|
limit: int = 50,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
"""获取工作流列表"""
|
|
query = db.query(Workflow)
|
|
total = query.count()
|
|
workflows = query.order_by(Workflow.created_at.desc()) \
|
|
.offset((page-1)*limit).limit(limit).all()
|
|
return {"total": total, "page": page, "limit": limit, "list": workflows}
|
|
|
|
|
|
@router.post("/workflows", response_model=WorkflowOut)
|
|
def create_workflow(data: WorkflowCreate, db: Session = Depends(get_db)):
|
|
"""创建工作流"""
|
|
workflow = Workflow(
|
|
id=str(uuid.uuid4())[:8],
|
|
user_id=1,
|
|
name=data.name,
|
|
node_count=data.nodeCount,
|
|
created_at=data.createdAt or datetime.utcnow().isoformat(),
|
|
updated_at=data.updatedAt or "",
|
|
global_prompt=data.globalPrompt,
|
|
nodes=data.nodes,
|
|
edges=data.edges,
|
|
)
|
|
db.add(workflow)
|
|
db.commit()
|
|
db.refresh(workflow)
|
|
return workflow
|
|
|
|
|
|
@router.get("/workflows/{id}", response_model=WorkflowOut)
|
|
def get_workflow(id: str, db: Session = Depends(get_db)):
|
|
"""获取单个工作流"""
|
|
workflow = db.query(Workflow).filter(Workflow.id == id).first()
|
|
if not workflow:
|
|
raise HTTPException(status_code=404, detail="Workflow not found")
|
|
return workflow
|
|
|
|
|
|
@router.put("/workflows/{id}", response_model=WorkflowOut)
|
|
def update_workflow(id: str, data: WorkflowUpdate, db: Session = Depends(get_db)):
|
|
"""更新工作流"""
|
|
workflow = db.query(Workflow).filter(Workflow.id == id).first()
|
|
if not workflow:
|
|
raise HTTPException(status_code=404, detail="Workflow not found")
|
|
|
|
update_data = data.model_dump(exclude_unset=True)
|
|
for field, value in update_data.items():
|
|
setattr(workflow, field, value)
|
|
|
|
workflow.updated_at = datetime.utcnow().isoformat()
|
|
db.commit()
|
|
db.refresh(workflow)
|
|
return workflow
|
|
|
|
|
|
@router.delete("/workflows/{id}")
|
|
def delete_workflow(id: str, db: Session = Depends(get_db)):
|
|
"""删除工作流"""
|
|
workflow = db.query(Workflow).filter(Workflow.id == id).first()
|
|
if not workflow:
|
|
raise HTTPException(status_code=404, detail="Workflow not found")
|
|
db.delete(workflow)
|
|
db.commit()
|
|
return {"message": "Deleted successfully"}
|