细节优化
This commit is contained in:
@@ -9,6 +9,7 @@ from app.schemas.knowledge_base import (
|
||||
KbCreateRequest,
|
||||
KbListResponse,
|
||||
KbResponse,
|
||||
KbSetExpiryRequest,
|
||||
KbTokenResponse,
|
||||
KbUpdateRequest,
|
||||
)
|
||||
@@ -86,6 +87,8 @@ def regenerate_token(
|
||||
) -> KbTokenResponse:
|
||||
svc = KnowledgeBaseService(db)
|
||||
kb, token = svc.regenerate_token(kb_id, user)
|
||||
# 重新生成 = 全新链接,有效期重置为长期
|
||||
svc.set_expiry(kb_id, user, None)
|
||||
return KbTokenResponse(token=token, ai_url=f"/k/{token}", token_hint=kb.token_hint)
|
||||
|
||||
|
||||
@@ -117,13 +120,42 @@ def get_knowledge_base_link(
|
||||
user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
) -> KbTokenResponse:
|
||||
"""获取完整 AI 链接(解密 token)。"""
|
||||
"""获取完整 AI 链接(解密 token)+ 有效期信息。"""
|
||||
from app.services.kb_public_service import KbPublicService
|
||||
|
||||
svc = KnowledgeBaseService(db)
|
||||
kb = svc.get_or_404(kb_id, user)
|
||||
token = svc.get_full_token(kb)
|
||||
if token is None:
|
||||
return KbTokenResponse(token="", ai_url="", token_hint=kb.token_hint or "")
|
||||
return KbTokenResponse(token=token, ai_url=f"/k/{token}", token_hint=kb.token_hint or "")
|
||||
return KbTokenResponse(
|
||||
token="", ai_url="", token_hint=kb.token_hint or "",
|
||||
expires_at=None, is_expired=False,
|
||||
)
|
||||
return KbTokenResponse(
|
||||
token=token,
|
||||
ai_url=f"/k/{token}",
|
||||
token_hint=kb.token_hint or "",
|
||||
expires_at=kb.token_expires_at,
|
||||
is_expired=KbPublicService.is_expired(kb),
|
||||
)
|
||||
|
||||
|
||||
@router.post("/{kb_id}/set-expiry", response_model=KbResponse)
|
||||
def set_link_expiry(
|
||||
kb_id: str,
|
||||
body: KbSetExpiryRequest,
|
||||
user: User = Depends(get_current_user),
|
||||
db: Session = Depends(get_db),
|
||||
) -> KbResponse:
|
||||
"""设置 AI 链接有效期。expires_in_minutes 为 null 表示长期有效。"""
|
||||
svc = KnowledgeBaseService(db)
|
||||
kb = svc.set_expiry(kb_id, user, body.expires_in_minutes)
|
||||
doc_count = svc._kb_repo.count_documents(kb_id)
|
||||
return KbResponse(
|
||||
id=kb.id, name=kb.name, description=kb.description, enabled=kb.enabled,
|
||||
token_hint=kb.token_hint, document_count=doc_count,
|
||||
created_at=kb.created_at, updated_at=kb.updated_at,
|
||||
)
|
||||
|
||||
|
||||
def _to_response(kb, doc_count: int = 0, ai_url: str | None = None) -> KbResponse:
|
||||
|
||||
Reference in New Issue
Block a user