Backend passed in codex
This commit is contained in:
@@ -7,14 +7,32 @@ from datetime import datetime
|
||||
from ..db import get_db
|
||||
from ..models import CallRecord, CallTranscript, CallAudioSegment
|
||||
from ..storage import get_audio_url
|
||||
from ..schemas import CallRecordCreate, CallRecordUpdate, TranscriptCreate
|
||||
|
||||
router = APIRouter(prefix="/history", tags=["history"])
|
||||
|
||||
|
||||
def record_to_dict(record: CallRecord) -> dict:
|
||||
return {
|
||||
"id": record.id,
|
||||
"user_id": record.user_id,
|
||||
"assistant_id": record.assistant_id,
|
||||
"source": record.source,
|
||||
"status": record.status,
|
||||
"started_at": record.started_at,
|
||||
"ended_at": record.ended_at,
|
||||
"duration_seconds": record.duration_seconds,
|
||||
"summary": record.summary,
|
||||
"cost": record.cost,
|
||||
"created_at": record.created_at,
|
||||
}
|
||||
|
||||
|
||||
@router.get("")
|
||||
def list_history(
|
||||
assistant_id: Optional[str] = None,
|
||||
status: Optional[str] = None,
|
||||
source: Optional[str] = None,
|
||||
page: int = 1,
|
||||
limit: int = 20,
|
||||
db: Session = Depends(get_db)
|
||||
@@ -26,12 +44,19 @@ def list_history(
|
||||
query = query.filter(CallRecord.assistant_id == assistant_id)
|
||||
if status:
|
||||
query = query.filter(CallRecord.status == status)
|
||||
if source:
|
||||
query = query.filter(CallRecord.source == source)
|
||||
|
||||
total = query.count()
|
||||
records = query.order_by(CallRecord.started_at.desc()) \
|
||||
.offset((page-1)*limit).limit(limit).all()
|
||||
|
||||
return {"total": total, "page": page, "limit": limit, "list": records}
|
||||
return {
|
||||
"total": total,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"list": [record_to_dict(r) for r in records]
|
||||
}
|
||||
|
||||
|
||||
@router.get("/{call_id}")
|
||||
@@ -46,10 +71,12 @@ def get_history_detail(call_id: str, db: Session = Depends(get_db)):
|
||||
.filter(CallTranscript.call_id == call_id) \
|
||||
.order_by(CallTranscript.turn_index).all()
|
||||
|
||||
# 补充音频 URL
|
||||
audio_segments = db.query(CallAudioSegment).filter(CallAudioSegment.call_id == call_id).all()
|
||||
audio_by_turn = {seg.turn_index: seg.audio_url for seg in audio_segments if seg.turn_index is not None}
|
||||
|
||||
transcript_list = []
|
||||
for t in transcripts:
|
||||
audio_url = t.audio_url or get_audio_url(call_id, t.turn_index)
|
||||
audio_url = audio_by_turn.get(t.turn_index) or get_audio_url(call_id, t.turn_index)
|
||||
transcript_list.append({
|
||||
"turnIndex": t.turn_index,
|
||||
"speaker": t.speaker,
|
||||
@@ -77,32 +104,29 @@ def get_history_detail(call_id: str, db: Session = Depends(get_db)):
|
||||
|
||||
@router.post("")
|
||||
def create_call_record(
|
||||
user_id: int,
|
||||
assistant_id: Optional[str] = None,
|
||||
source: str = "debug",
|
||||
data: CallRecordCreate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""创建通话记录(引擎回调使用)"""
|
||||
record = CallRecord(
|
||||
id=str(uuid.uuid4())[:8],
|
||||
user_id=user_id,
|
||||
assistant_id=assistant_id,
|
||||
source=source,
|
||||
status="connected",
|
||||
user_id=data.user_id,
|
||||
assistant_id=data.assistant_id,
|
||||
source=data.source,
|
||||
status=data.status or "connected",
|
||||
started_at=datetime.utcnow().isoformat(),
|
||||
cost=data.cost or 0.0,
|
||||
)
|
||||
db.add(record)
|
||||
db.commit()
|
||||
db.refresh(record)
|
||||
return record
|
||||
return record_to_dict(record)
|
||||
|
||||
|
||||
@router.put("/{call_id}")
|
||||
def update_call_record(
|
||||
call_id: str,
|
||||
status: Optional[str] = None,
|
||||
summary: Optional[str] = None,
|
||||
duration_seconds: Optional[int] = None,
|
||||
data: CallRecordUpdate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""更新通话记录"""
|
||||
@@ -110,59 +134,64 @@ def update_call_record(
|
||||
if not record:
|
||||
raise HTTPException(status_code=404, detail="Call record not found")
|
||||
|
||||
if status:
|
||||
record.status = status
|
||||
if summary:
|
||||
record.summary = summary
|
||||
if duration_seconds:
|
||||
record.duration_seconds = duration_seconds
|
||||
if data.status is not None:
|
||||
record.status = data.status
|
||||
if data.summary is not None:
|
||||
record.summary = data.summary
|
||||
if data.duration_seconds is not None:
|
||||
record.duration_seconds = data.duration_seconds
|
||||
record.ended_at = datetime.utcnow().isoformat()
|
||||
if data.ended_at is not None:
|
||||
record.ended_at = data.ended_at
|
||||
if data.cost is not None:
|
||||
record.cost = data.cost
|
||||
if data.metadata is not None:
|
||||
record.call_metadata = data.metadata
|
||||
|
||||
db.commit()
|
||||
return {"message": "Updated successfully"}
|
||||
db.refresh(record)
|
||||
return record_to_dict(record)
|
||||
|
||||
|
||||
@router.post("/{call_id}/transcripts")
|
||||
def add_transcript(
|
||||
call_id: str,
|
||||
turn_index: int,
|
||||
speaker: str,
|
||||
content: str,
|
||||
start_ms: int,
|
||||
end_ms: int,
|
||||
confidence: Optional[float] = None,
|
||||
duration_ms: Optional[int] = None,
|
||||
emotion: Optional[str] = None,
|
||||
data: TranscriptCreate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""添加转写片段"""
|
||||
record = db.query(CallRecord).filter(CallRecord.id == call_id).first()
|
||||
if not record:
|
||||
raise HTTPException(status_code=404, detail="Call record not found")
|
||||
|
||||
transcript = CallTranscript(
|
||||
call_id=call_id,
|
||||
turn_index=turn_index,
|
||||
speaker=speaker,
|
||||
content=content,
|
||||
confidence=confidence,
|
||||
start_ms=start_ms,
|
||||
end_ms=end_ms,
|
||||
duration_ms=duration_ms,
|
||||
emotion=emotion,
|
||||
turn_index=data.turn_index,
|
||||
speaker=data.speaker,
|
||||
content=data.content,
|
||||
confidence=data.confidence,
|
||||
start_ms=data.start_ms,
|
||||
end_ms=data.end_ms,
|
||||
duration_ms=data.duration_ms,
|
||||
emotion=data.emotion,
|
||||
)
|
||||
db.add(transcript)
|
||||
db.commit()
|
||||
db.refresh(transcript)
|
||||
|
||||
# 补充音频 URL
|
||||
audio_url = get_audio_url(call_id, turn_index)
|
||||
audio_url = get_audio_url(call_id, data.turn_index)
|
||||
|
||||
return {
|
||||
"id": transcript.id,
|
||||
"turn_index": turn_index,
|
||||
"speaker": speaker,
|
||||
"content": content,
|
||||
"confidence": confidence,
|
||||
"start_ms": start_ms,
|
||||
"end_ms": end_ms,
|
||||
"duration_ms": duration_ms,
|
||||
"turn_index": data.turn_index,
|
||||
"speaker": data.speaker,
|
||||
"content": data.content,
|
||||
"confidence": data.confidence,
|
||||
"start_ms": data.start_ms,
|
||||
"end_ms": data.end_ms,
|
||||
"duration_ms": data.duration_ms,
|
||||
"emotion": data.emotion,
|
||||
"audio_url": audio_url,
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user