2026-09-09 21:55:37 +08:00
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, Header
|
|
|
|
|
|
from pydantic import Field
|
|
|
|
|
|
|
|
|
|
|
|
from app.api.dependencies.auth import build_request_context
|
2026-09-11 16:57:47 +08:00
|
|
|
|
from app.api.dependencies.rate_limit import enforce_rate_limit
|
2026-09-09 21:55:37 +08:00
|
|
|
|
from app.api.schemas.admin import StrictPayload
|
2026-09-11 16:57:47 +08:00
|
|
|
|
from app.api.schemas.conversations import HandoverRequest
|
2026-09-09 21:55:37 +08:00
|
|
|
|
from app.core.contracts import RequestContext
|
|
|
|
|
|
from app.service.public_platform_service import PublicPlatformService
|
|
|
|
|
|
|
2026-09-11 16:57:47 +08:00
|
|
|
|
router = APIRouter(prefix="/api/v1", tags=["public-platform"],
|
|
|
|
|
|
dependencies=[Depends(enforce_rate_limit)])
|
2026-09-09 21:55:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@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)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-09-11 16:57:47 +08:00
|
|
|
|
# 转人工申请的唯一入口:必须走 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()
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-09-09 21:55:37 +08:00
|
|
|
|
@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)
|