refactor(api): type Tenant custom config with TypedDict and tighten MCP headers type (#34670)

This commit is contained in:
YBoy 2026-04-07 15:18:19 +02:00 committed by GitHub
parent f09be969bb
commit 485fc2c416
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 7 deletions

View File

@ -28,7 +28,7 @@ from enums.cloud_plan import CloudPlan
from extensions.ext_database import db from extensions.ext_database import db
from libs.helper import TimestampField from libs.helper import TimestampField
from libs.login import current_account_with_tenant, login_required from libs.login import current_account_with_tenant, login_required
from models.account import Tenant, TenantStatus from models.account import Tenant, TenantCustomConfigDict, TenantStatus
from services.account_service import TenantService from services.account_service import TenantService
from services.billing_service import BillingService, SubscriptionPlan from services.billing_service import BillingService, SubscriptionPlan
from services.enterprise.enterprise_service import EnterpriseService from services.enterprise.enterprise_service import EnterpriseService
@ -240,8 +240,10 @@ class CustomConfigWorkspaceApi(Resource):
args = WorkspaceCustomConfigPayload.model_validate(payload) args = WorkspaceCustomConfigPayload.model_validate(payload)
tenant = db.get_or_404(Tenant, current_tenant_id) tenant = db.get_or_404(Tenant, current_tenant_id)
custom_config_dict = { custom_config_dict: TenantCustomConfigDict = {
"remove_webapp_brand": args.remove_webapp_brand, "remove_webapp_brand": args.remove_webapp_brand
if args.remove_webapp_brand is not None
else tenant.custom_config_dict.get("remove_webapp_brand", False),
"replace_webapp_logo": args.replace_webapp_logo "replace_webapp_logo": args.replace_webapp_logo
if args.replace_webapp_logo is not None if args.replace_webapp_logo is not None
else tenant.custom_config_dict.get("replace_webapp_logo"), else tenant.custom_config_dict.get("replace_webapp_logo"),

View File

@ -2,7 +2,7 @@ import enum
import json import json
from dataclasses import field from dataclasses import field
from datetime import datetime from datetime import datetime
from typing import Any, Optional from typing import Optional, TypedDict
from uuid import uuid4 from uuid import uuid4
import sqlalchemy as sa import sqlalchemy as sa
@ -232,6 +232,11 @@ class TenantStatus(enum.StrEnum):
ARCHIVE = "archive" ARCHIVE = "archive"
class TenantCustomConfigDict(TypedDict, total=False):
remove_webapp_brand: bool
replace_webapp_logo: str | None
class Tenant(TypeBase): class Tenant(TypeBase):
__tablename__ = "tenants" __tablename__ = "tenants"
__table_args__ = (sa.PrimaryKeyConstraint("id", name="tenant_pkey"),) __table_args__ = (sa.PrimaryKeyConstraint("id", name="tenant_pkey"),)
@ -263,11 +268,11 @@ class Tenant(TypeBase):
) )
@property @property
def custom_config_dict(self) -> dict[str, Any]: def custom_config_dict(self) -> TenantCustomConfigDict:
return json.loads(self.custom_config) if self.custom_config else {} return json.loads(self.custom_config) if self.custom_config else {}
@custom_config_dict.setter @custom_config_dict.setter
def custom_config_dict(self, value: dict[str, Any]) -> None: def custom_config_dict(self, value: TenantCustomConfigDict) -> None:
self.custom_config = json.dumps(value) self.custom_config = json.dumps(value)

View File

@ -356,7 +356,7 @@ class MCPToolProvider(TypeBase):
return {} return {}
@property @property
def headers(self) -> dict[str, Any]: def headers(self) -> dict[str, str]:
if self.encrypted_headers is None: if self.encrypted_headers is None:
return {} return {}
try: try: