Enhance credential management and testing functionality
- Introduce new fields for voice, speed, and language in the AssistantConfig and ProviderCredential models to support TTS and ASR configurations. - Update the database schema and seeding script to accommodate the new fields, ensuring backward compatibility. - Implement credential testing endpoints and logic to validate OpenAI-compatible credentials, enhancing user experience and reliability. - Modify frontend components to include new fields in the credential forms and improve connection testing feedback. - Refactor related services and API interactions to support the new credential testing feature.
This commit is contained in:
@@ -9,7 +9,13 @@ import uuid
|
||||
from db.models import ProviderCredential
|
||||
from db.session import get_session
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from schemas import CredentialOut, CredentialUpsert
|
||||
from schemas import (
|
||||
CredentialOut,
|
||||
CredentialTestRequest,
|
||||
CredentialTestResult,
|
||||
CredentialUpsert,
|
||||
)
|
||||
from services.credential_tester import test_openai_credential
|
||||
from services.masking import mask, resolve_incoming_key
|
||||
from sqlalchemy import select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
@@ -26,6 +32,9 @@ def _to_out(c: ProviderCredential) -> CredentialOut:
|
||||
interface_type=c.interface_type,
|
||||
api_url=c.api_url,
|
||||
api_key=mask(c.api_key), # 永远打码
|
||||
voice=c.voice,
|
||||
speed=c.speed,
|
||||
language=c.language,
|
||||
is_default=c.is_default,
|
||||
)
|
||||
|
||||
@@ -60,6 +69,9 @@ async def create_credential(
|
||||
interface_type=body.interface_type,
|
||||
api_url=body.api_url,
|
||||
api_key=resolve_incoming_key(body.api_key, ""),
|
||||
voice=body.voice,
|
||||
speed=body.speed,
|
||||
language=body.language,
|
||||
is_default=body.is_default,
|
||||
)
|
||||
session.add(c)
|
||||
@@ -70,6 +82,44 @@ async def create_credential(
|
||||
return _to_out(c)
|
||||
|
||||
|
||||
@router.post("/test", response_model=CredentialTestResult)
|
||||
async def test_new_credential(body: CredentialTestRequest):
|
||||
if body.interface_type != "openai":
|
||||
return CredentialTestResult(
|
||||
ok=False,
|
||||
message="暂不支持该接口类型",
|
||||
detail="当前仅支持 OpenAI 兼容接口测试",
|
||||
)
|
||||
if not body.api_key:
|
||||
return CredentialTestResult(
|
||||
ok=False,
|
||||
message="缺少 API Key",
|
||||
detail="测试新配置时需要输入 API Key",
|
||||
)
|
||||
return await test_openai_credential(body)
|
||||
|
||||
|
||||
@router.post("/{cred_id}/test", response_model=CredentialTestResult)
|
||||
async def test_saved_credential(
|
||||
cred_id: str,
|
||||
body: CredentialTestRequest,
|
||||
session: AsyncSession = Depends(get_session),
|
||||
):
|
||||
c = await session.get(ProviderCredential, cred_id)
|
||||
if not c:
|
||||
raise HTTPException(404, "凭证不存在")
|
||||
config = body.model_copy(
|
||||
update={"api_key": resolve_incoming_key(body.api_key, c.api_key)}
|
||||
)
|
||||
if config.interface_type != "openai":
|
||||
return CredentialTestResult(
|
||||
ok=False,
|
||||
message="暂不支持该接口类型",
|
||||
detail="当前仅支持 OpenAI 兼容接口测试",
|
||||
)
|
||||
return await test_openai_credential(config)
|
||||
|
||||
|
||||
@router.post("/{cred_id}/duplicate", response_model=CredentialOut)
|
||||
async def duplicate_credential(
|
||||
cred_id: str, session: AsyncSession = Depends(get_session)
|
||||
@@ -86,6 +136,9 @@ async def duplicate_credential(
|
||||
interface_type=src.interface_type,
|
||||
api_url=src.api_url,
|
||||
api_key=src.api_key, # 真 key,DB→DB
|
||||
voice=src.voice,
|
||||
speed=src.speed,
|
||||
language=src.language,
|
||||
is_default=False, # 副本不继承默认,避免抢走源的默认标记
|
||||
)
|
||||
session.add(c)
|
||||
@@ -108,6 +161,9 @@ async def update_credential(
|
||||
c.type = body.type
|
||||
c.interface_type = body.interface_type
|
||||
c.api_url = body.api_url
|
||||
c.voice = body.voice
|
||||
c.speed = body.speed
|
||||
c.language = body.language
|
||||
c.is_default = body.is_default
|
||||
# 写时哨兵:打码占位符 → 保留旧 key
|
||||
c.api_key = resolve_incoming_key(body.api_key, c.api_key)
|
||||
|
||||
Reference in New Issue
Block a user