121 lines
4.7 KiB
Python
121 lines
4.7 KiB
Python
from typing import Any, Literal
|
||
|
||
from fastapi import APIRouter, Depends, Header
|
||
from pydantic import Field
|
||
|
||
from app.api.dependencies.auth import build_request_context
|
||
from app.api.dependencies.rate_limit import enforce_rate_limit
|
||
from app.api.schemas.admin import StrictPayload
|
||
from app.api.schemas.conversations import HandoverRequest
|
||
from app.core.contracts import RequestContext
|
||
from app.service.public_platform_service import PublicPlatformService
|
||
|
||
router = APIRouter(prefix="/api/v1", tags=["public-platform"],
|
||
dependencies=[Depends(enforce_rate_limit)])
|
||
|
||
|
||
class SessionCreate(StrictPayload):
|
||
agent_type: str = Field(pattern=r"^[a-z][a-z0-9_]{1,31}$")
|
||
|
||
|
||
class Cancellation(StrictPayload):
|
||
reason: str = Field(default="user_cancelled", max_length=128)
|
||
|
||
|
||
class CandidateDecisionPayload(StrictPayload):
|
||
decision: Literal["confirmed", "rejected"]
|
||
|
||
|
||
@router.post("/conversations", status_code=201)
|
||
async def create_session(
|
||
payload: SessionCreate,
|
||
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write("create", "", context, key, payload.model_dump())
|
||
|
||
|
||
@router.get("/conversations/{session_id}")
|
||
async def get_session(
|
||
session_id: str, context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().session(session_id, context)
|
||
|
||
|
||
@router.post("/conversations/{session_id}/closures")
|
||
async def close_session(
|
||
session_id: str, key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write("close", session_id, context, key, {})
|
||
|
||
|
||
@router.post("/agent-runs/{run_id}/cancellations", status_code=202)
|
||
async def cancel_run(
|
||
run_id: str, payload: Cancellation,
|
||
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write("cancel", run_id, context, key, payload.model_dump())
|
||
|
||
|
||
@router.get("/handover-requests/{handover_id}")
|
||
async def get_handover(
|
||
handover_id: str, context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().handover(handover_id, context)
|
||
|
||
|
||
# 转人工申请的唯一入口:必须走 PublicPlatformService.write("handover"),
|
||
# 它在同一事务内写工单 + Outbox 事件(conversation.transfer_requested)+ 审计。
|
||
# 历史上 conversations.py 另有一份只写审计的实现,因注册顺序覆盖了本入口,
|
||
# 导致转人工的异步链路(Outbox → Worker)永不触发,已合并删除。
|
||
@router.post("/conversations/{session_id}/handover-requests", status_code=202)
|
||
async def request_handover(
|
||
session_id: str,
|
||
payload: HandoverRequest,
|
||
key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().write(
|
||
"handover", session_id, context, key, payload.model_dump()
|
||
)
|
||
|
||
|
||
@router.get("/users/me/memory-profile")
|
||
async def my_memory(
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().memory(int(context.user_id), context)
|
||
|
||
|
||
@router.get("/customers/{customer_id}/memory-profile")
|
||
async def customer_memory(
|
||
customer_id: int, context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
return await PublicPlatformService().memory(customer_id, context)
|
||
|
||
|
||
@router.get("/users/me/memory-candidates")
|
||
async def my_memory_candidates(
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
"""返回当前用户可确认的画像候选,不返回证据原文。"""
|
||
from app.service.customer_profile_candidate_service import CustomerProfileCandidateService
|
||
|
||
return await CustomerProfileCandidateService().list_for_customer(context)
|
||
|
||
|
||
@router.post("/users/me/memory-candidates/{candidate_id}/decisions")
|
||
async def decide_memory_candidate(
|
||
candidate_id: int,
|
||
payload: CandidateDecisionPayload,
|
||
context: RequestContext = Depends(build_request_context), # noqa: B008
|
||
) -> dict[str, Any]:
|
||
"""用户确认或拒绝自己的候选;确认后仍需管理员审核才能激活。"""
|
||
from app.service.customer_profile_candidate_service import CustomerProfileCandidateService
|
||
|
||
return await CustomerProfileCandidateService().decide_by_customer(
|
||
candidate_id, payload.decision, context
|
||
)
|