Backend passed in codex
This commit is contained in:
@@ -5,99 +5,55 @@ import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from ..db import get_db
|
||||
from ..models import Assistant, Voice, Workflow
|
||||
from ..models import Assistant, 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}
|
||||
def assistant_to_dict(assistant: Assistant) -> dict:
|
||||
return {
|
||||
"id": assistant.id,
|
||||
"name": assistant.name,
|
||||
"callCount": assistant.call_count,
|
||||
"opener": assistant.opener or "",
|
||||
"prompt": assistant.prompt or "",
|
||||
"knowledgeBaseId": assistant.knowledge_base_id,
|
||||
"language": assistant.language,
|
||||
"voice": assistant.voice,
|
||||
"speed": assistant.speed,
|
||||
"hotwords": assistant.hotwords or [],
|
||||
"tools": assistant.tools or [],
|
||||
"interruptionSensitivity": assistant.interruption_sensitivity,
|
||||
"configMode": assistant.config_mode,
|
||||
"apiUrl": assistant.api_url,
|
||||
"apiKey": assistant.api_key,
|
||||
"llmModelId": assistant.llm_model_id,
|
||||
"asrModelId": assistant.asr_model_id,
|
||||
"embeddingModelId": assistant.embedding_model_id,
|
||||
"rerankModelId": assistant.rerank_model_id,
|
||||
"created_at": assistant.created_at,
|
||||
"updated_at": assistant.updated_at,
|
||||
}
|
||||
|
||||
|
||||
@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)
|
||||
def _apply_assistant_update(assistant: Assistant, update_data: dict) -> None:
|
||||
field_map = {
|
||||
"knowledgeBaseId": "knowledge_base_id",
|
||||
"interruptionSensitivity": "interruption_sensitivity",
|
||||
"configMode": "config_mode",
|
||||
"apiUrl": "api_url",
|
||||
"apiKey": "api_key",
|
||||
"llmModelId": "llm_model_id",
|
||||
"asrModelId": "asr_model_id",
|
||||
"embeddingModelId": "embedding_model_id",
|
||||
"rerankModelId": "rerank_model_id",
|
||||
}
|
||||
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"}
|
||||
setattr(assistant, field_map.get(field, field), value)
|
||||
|
||||
|
||||
# ============ Assistants ============
|
||||
@@ -112,7 +68,12 @@ def list_assistants(
|
||||
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}
|
||||
return {
|
||||
"total": total,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"list": [assistant_to_dict(a) for a in assistants]
|
||||
}
|
||||
|
||||
|
||||
@router.get("/assistants/{id}", response_model=AssistantOut)
|
||||
@@ -121,7 +82,7 @@ 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
|
||||
return assistant_to_dict(assistant)
|
||||
|
||||
|
||||
@router.post("/assistants", response_model=AssistantOut)
|
||||
@@ -143,11 +104,15 @@ def create_assistant(data: AssistantCreate, db: Session = Depends(get_db)):
|
||||
config_mode=data.configMode,
|
||||
api_url=data.apiUrl,
|
||||
api_key=data.apiKey,
|
||||
llm_model_id=data.llmModelId,
|
||||
asr_model_id=data.asrModelId,
|
||||
embedding_model_id=data.embeddingModelId,
|
||||
rerank_model_id=data.rerankModelId,
|
||||
)
|
||||
db.add(assistant)
|
||||
db.commit()
|
||||
db.refresh(assistant)
|
||||
return assistant
|
||||
return assistant_to_dict(assistant)
|
||||
|
||||
|
||||
@router.put("/assistants/{id}")
|
||||
@@ -158,13 +123,12 @@ def update_assistant(id: str, data: AssistantUpdate, db: Session = Depends(get_d
|
||||
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)
|
||||
_apply_assistant_update(assistant, update_data)
|
||||
|
||||
assistant.updated_at = datetime.utcnow()
|
||||
db.commit()
|
||||
db.refresh(assistant)
|
||||
return assistant
|
||||
return assistant_to_dict(assistant)
|
||||
|
||||
|
||||
@router.delete("/assistants/{id}")
|
||||
|
||||
Reference in New Issue
Block a user