Merge pull request #63 from xpack-ai/feature/mcp
Bug fixes and improved prompts
This commit is contained in:
20 files changed
+185
-98
No files matched your search
@@ -1,11 +1,11 @@
|
||||
{
|
||||
"version": "v1.1",
|
||||
"releaseDate": "2025-10-17",
|
||||
"releaseDate": "2025-12-07",
|
||||
"description": "This is the latest release of the application.",
|
||||
"downloads": {
|
||||
"program": {
|
||||
"xpack_mcp_market": {
|
||||
"version": "v1.1.1",
|
||||
"version": "v1.1.5",
|
||||
"docker": "xpackai/xpack-mcp-market",
|
||||
"tar": "xpack-mcp-market.tar.gz",
|
||||
"url": "https://public.eolinker.com/xpack/images/xpack-mcp-market.${VERSION}.${ARCH}.tar.gz"
|
||||
|
||||
@@ -58,6 +58,19 @@ def update_mcp_service_info(request: Request, body: dict = Body(...), mcp_manage
|
||||
# Validate ID is required
|
||||
if not body.get("id"):
|
||||
return ResponseUtils.error(error_msg=error_msg.MISSING_PARAMETER)
|
||||
# Validate price is a positive number if charge_type is per_token
|
||||
if body.get("charge_type") == ChargeType.PER_CALL:
|
||||
price = body.get("price")
|
||||
if price is None or price < 0:
|
||||
return ResponseUtils.error(error_msg=error_msg.INVALID_PRICE)
|
||||
if body.get("charge_type") == ChargeType.PER_TOKEN:
|
||||
input_token = body.get("input_token")
|
||||
output_token = body.get("output_token")
|
||||
|
||||
if input_token is None or input_token < 0:
|
||||
return ResponseUtils.error(error_msg=error_msg.INVALID_INPUT_TOKEN)
|
||||
if output_token is None or output_token < 0:
|
||||
return ResponseUtils.error(error_msg=error_msg.INVALID_OUTPUT_TOKEN)
|
||||
|
||||
try:
|
||||
mcp_manager_service.update(body)
|
||||
@@ -106,7 +119,8 @@ async def openapi_parse(
|
||||
|
||||
return ResponseUtils.success(data=result)
|
||||
except HTTPException as e:
|
||||
return ResponseUtils.error(message=f"Request failed: {e.detail}", code=e.status_code)
|
||||
logger.error(f"Request failed: {e.detail}")
|
||||
return ResponseUtils.error(message=f"Request failed, please contact the administrator.", code=e.status_code)
|
||||
except Exception as e:
|
||||
return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR)
|
||||
|
||||
@@ -147,7 +161,8 @@ async def openapi_parse_update(
|
||||
except ValueError as e:
|
||||
return ResponseUtils.error(message=str(e))
|
||||
except HTTPException as e:
|
||||
return ResponseUtils.error(message=f"Request failed: {e.detail}", code=e.status_code)
|
||||
logger.error(f"Request failed: {e.detail}")
|
||||
return ResponseUtils.error(message=f"Request failed, please contact the administrator.", code=e.status_code)
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to update service from OpenAPI: {str(e)}")
|
||||
return ResponseUtils.error(error_msg=error_msg.INTERNAL_ERROR)
|
||||
|
||||
@@ -35,7 +35,7 @@ async def upload(
|
||||
filename = os.path.basename(filename)
|
||||
|
||||
# enforce content type and size limits
|
||||
allowed_types = {"image/jpeg", "image/png", "image/webp"}
|
||||
allowed_types = {"image/jpeg", "image/png", "image/webp", "image/gif", "image/svg+xml"}
|
||||
if img.content_type not in allowed_types:
|
||||
return ResponseUtils.error(message="Unsupported file type", code=400)
|
||||
|
||||
|
||||
@@ -42,6 +42,7 @@ class UserWalletHistoryRepository:
|
||||
amount=amount,
|
||||
balance_after=0.00,
|
||||
type=TransactionType.DEPOSIT,
|
||||
transaction_id=transaction_id,
|
||||
status=status,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
@@ -201,7 +202,7 @@ class UserWalletHistoryRepository:
|
||||
return False
|
||||
|
||||
# Update wallet balance and order status
|
||||
wallet.balance = float(wallet.balance) + float(obj.amount)
|
||||
# wallet.balance = float(wallet.balance) + float(obj.amount)
|
||||
obj.status = 1 # 1=completed
|
||||
obj.balance_after = float(wallet.balance)
|
||||
obj.transaction_id = transaction_id
|
||||
|
||||
@@ -134,10 +134,17 @@ class AuthService:
|
||||
|
||||
def account_login(self, account: str, password: str) -> Optional[str]:
|
||||
user = self.user_repository.get_by_account(account)
|
||||
if user and user.password == password:
|
||||
token = self.create_user_token(user.id)
|
||||
if token:
|
||||
return token
|
||||
if user is None:
|
||||
logger.warning(f"not found user, account: {account}")
|
||||
return None
|
||||
if user.password != password:
|
||||
logger.warning(f"password not match, account: {account}, password: {password}, user password: {user.password}")
|
||||
return None
|
||||
|
||||
token = self.create_user_token(user.id)
|
||||
if token:
|
||||
return token
|
||||
logger.warning(f"Failed to create user token for user_id: {user.id}")
|
||||
return None
|
||||
|
||||
def logout(self, token: str) -> bool:
|
||||
|
||||
@@ -2,7 +2,6 @@ import logging
|
||||
import uuid
|
||||
import json
|
||||
import re
|
||||
from datetime import datetime, timezone
|
||||
from sqlalchemy.orm import Session
|
||||
from typing import Optional, Tuple, List
|
||||
from services.admin_service.repositories.mcp_service_repository import McpServiceRepository
|
||||
@@ -143,10 +142,7 @@ class McpManagerService:
|
||||
existing_service.short_description = temp_service.short_description
|
||||
existing_service.long_description = temp_service.long_description
|
||||
existing_service.headers = temp_service.headers
|
||||
# existing_service.auth_method = AuthMethod(temp_service.auth_method.value)
|
||||
existing_service.base_url = temp_service.base_url
|
||||
# existing_service.auth_header = temp_service.auth_header
|
||||
# existing_service.auth_token = temp_service.auth_token
|
||||
existing_service.charge_type = ChargeType(temp_service.charge_type.value)
|
||||
existing_service.price = temp_service.price
|
||||
existing_service.input_token_price = temp_service.input_token_price
|
||||
@@ -239,7 +235,8 @@ class McpManagerService:
|
||||
else:
|
||||
# If provided as string, store directly
|
||||
existing_service.tags = body["tags"]
|
||||
|
||||
if not existing_service.service_type:
|
||||
existing_service.service_type = "openapi"
|
||||
# Commit changes
|
||||
self.db.commit()
|
||||
self.db.refresh(existing_service)
|
||||
@@ -369,6 +366,7 @@ class McpManagerService:
|
||||
mcp_service.charge_type = ChargeType.FREE # Default to free
|
||||
mcp_service.price = 0.0
|
||||
mcp_service.enabled = 0 # Default disabled, requires manual activation by user
|
||||
mcp_service.service_type = "openapi"
|
||||
|
||||
# Save service
|
||||
self.mcp_service_repository.create(mcp_service)
|
||||
@@ -433,15 +431,13 @@ class McpManagerService:
|
||||
temp_service.slug_name = existing_service.slug_name # Keep original slug_name
|
||||
temp_service.short_description = openapi_data.description or existing_service.short_description
|
||||
temp_service.long_description = openapi_data.description or existing_service.long_description
|
||||
# temp_service.auth_method = TempAuthMethod(existing_service.auth_method.value) # Keep original auth method
|
||||
temp_service.base_url = existing_service.base_url # Keep original base_url
|
||||
# temp_service.auth_header = existing_service.auth_header
|
||||
# temp_service.auth_token = existing_service.auth_token
|
||||
temp_service.headers = existing_service.headers # Keep original headers
|
||||
temp_service.charge_type = TempChargeType(existing_service.charge_type.value) # Keep original charge type
|
||||
temp_service.price = existing_service.price # Keep original price
|
||||
temp_service.enabled = existing_service.enabled # Keep original enabled status
|
||||
temp_service.tags = existing_service.tags # Keep original tags
|
||||
# temp_service.service_type = existing_service.service_type # Keep original service type
|
||||
|
||||
# Save temporary service record
|
||||
self.temp_mcp_service_repository.create(temp_service)
|
||||
@@ -488,18 +484,17 @@ class McpManagerService:
|
||||
"headers": json.loads(temp_service.headers) if temp_service.headers else [],
|
||||
"charge_type": temp_service.charge_type.value if temp_service.charge_type else None,
|
||||
"price": str(float(temp_service.price)) if temp_service.price and temp_service.charge_type == ChargeType.PER_CALL else "0.00",
|
||||
"input_token_price": str(float(temp_service.input_token_price)) if temp_service.input_token_price and temp_service.charge_type == ChargeType.PER_TOKEN else "0.00",
|
||||
"input_token_price": str(float(temp_service.input_token_price)) if temp_service.input_token_price and temp_service.charge_type.value == ChargeType.PER_TOKEN else "0.00",
|
||||
"output_token_price": str(float(temp_service.output_token_price)) if temp_service.output_token_price and temp_service.charge_type == ChargeType.PER_TOKEN else "0.00",
|
||||
"enabled": temp_service.enabled,
|
||||
"tags": parse_tags_to_array(temp_service.tags),
|
||||
"apis": apis_list,
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"Failed to update service from OpenAPI: {str(e)}")
|
||||
raise ValueError(f"Failed to update service from OpenAPI: {str(e)}")
|
||||
raise ValueError(f"Failed to update service from OpenAPI.")
|
||||
|
||||
def get_public_services_paginated(self, keyword: str, page: int = 1, page_size: int = 10) -> Tuple[List[dict], int]:
|
||||
"""Get public services list with pagination, returns formatted data with API info"""
|
||||
|
||||
@@ -313,6 +313,7 @@ class PaymentService:
|
||||
new_balance=new_balance,
|
||||
expected_balance=current_wallet.balance
|
||||
)
|
||||
logger.info(f"Attempt {attempt + 1}: Balance update successful for user_id {user_id}, new balance: {new_balance},success:{success}")
|
||||
|
||||
if success:
|
||||
self._delete_wallet_cache(user_id)
|
||||
|
||||
@@ -5,6 +5,8 @@ from services.common.models.user_apikey import UserApiKey
|
||||
from services.common.database import SessionLocal
|
||||
from datetime import datetime
|
||||
from services.admin_service.repositories.user_apikey_repository import UserApiKeyRepository
|
||||
from services.common.utils.cache_utils import CacheUtils
|
||||
from services.common.redis_keys import RedisKeys
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -31,6 +33,10 @@ class UserApiKeyService:
|
||||
if not user_apikey or user_apikey.user_id != user_id:
|
||||
return None
|
||||
self.user_apikey_repository.delete(id)
|
||||
try:
|
||||
CacheUtils.delete_cache(RedisKeys.user_apikey_key(user_apikey.apikey))
|
||||
except Exception:
|
||||
pass
|
||||
return user_apikey
|
||||
|
||||
def get_by_user_id(self, user_id: str) -> List[UserApiKey]:
|
||||
|
||||
@@ -61,6 +61,7 @@ class UserStatsService:
|
||||
UserApiKey.id == apikey_id,
|
||||
func.date(McpCallLog.call_start_time) >= start_date,
|
||||
func.date(McpCallLog.call_start_time) <= end_date,
|
||||
McpCallLog.call_success == True,
|
||||
)
|
||||
)
|
||||
.group_by(func.date(McpCallLog.call_start_time))
|
||||
|
||||
@@ -16,7 +16,7 @@ class McpServiceRepository:
|
||||
|
||||
def get_by_id(self, service_id: str, force_update: bool = False) -> Optional[McpService]:
|
||||
"""
|
||||
Get single MCP service by service ID
|
||||
Get single MCP service by service ID
|
||||
"""
|
||||
cache_key = f"xpack:mcp_service:id:{service_id}"
|
||||
if not force_update:
|
||||
|
||||
@@ -207,11 +207,17 @@ class McpServerFactory:
|
||||
|
||||
# Execute tool
|
||||
result,response_data = await self.tool_service.execute_tool(tool_config, arguments, call_params)
|
||||
call_success = True
|
||||
validation_ok, validation_msg = self._validate_output_schema(tool_config, response_data)
|
||||
if not validation_ok:
|
||||
call_success = False
|
||||
error_text = f"Response validation failed: {validation_msg}"
|
||||
result = [types.TextContent(type="text", text=error_text)]
|
||||
response_data = {}
|
||||
else:
|
||||
call_success = True
|
||||
logger.info(f"Tool call successful - User ID: {user_id}, Tool: {name}")
|
||||
|
||||
# Calculate output token amount after tool execution
|
||||
if pre_deduct_result.charge_type == "per_token" and result:
|
||||
if pre_deduct_result.charge_type == "per_token" and result and call_success:
|
||||
output_text = ""
|
||||
for content in result:
|
||||
if isinstance(content, types.TextContent):
|
||||
@@ -257,6 +263,57 @@ class McpServerFactory:
|
||||
# Ensure return type is correct
|
||||
return result,response_data
|
||||
|
||||
def _validate_output_schema(self, tool_config, response_data: dict) -> tuple[bool, str]:
|
||||
try:
|
||||
schema_str = getattr(tool_config, "response_schema", None)
|
||||
if not schema_str:
|
||||
return True, ""
|
||||
try:
|
||||
schema = json.loads(schema_str)
|
||||
except Exception:
|
||||
return False, "Invalid response_schema JSON"
|
||||
|
||||
def check(schema_obj, data_obj) -> tuple[bool, str]:
|
||||
t = schema_obj.get("type")
|
||||
if t == "object":
|
||||
if not isinstance(data_obj, dict):
|
||||
return False, "Expected object"
|
||||
req = schema_obj.get("required", [])
|
||||
for k in req:
|
||||
if k not in data_obj:
|
||||
return False, f"Missing required field: {k}"
|
||||
props = schema_obj.get("properties", {})
|
||||
for k, sub in props.items():
|
||||
if k in data_obj:
|
||||
ok, msg = check(sub, data_obj[k])
|
||||
if not ok:
|
||||
return False, f"Field {k}: {msg}"
|
||||
return True, ""
|
||||
if t == "array":
|
||||
if not isinstance(data_obj, list):
|
||||
return False, "Expected array"
|
||||
items = schema_obj.get("items")
|
||||
if items:
|
||||
for i, it in enumerate(data_obj):
|
||||
ok, msg = check(items, it)
|
||||
if not ok:
|
||||
return False, f"Item {i}: {msg}"
|
||||
return True, ""
|
||||
if t == "string":
|
||||
return (isinstance(data_obj, str), "Expected string")
|
||||
if t == "integer":
|
||||
return (isinstance(data_obj, int), "Expected integer")
|
||||
if t == "number":
|
||||
return ((isinstance(data_obj, (int, float))), "Expected number")
|
||||
if t == "boolean":
|
||||
return (isinstance(data_obj, bool), "Expected boolean")
|
||||
return True, ""
|
||||
|
||||
ok, msg = check(schema, response_data)
|
||||
return ok, msg
|
||||
except Exception as e:
|
||||
return False, str(e)
|
||||
|
||||
def _create_mcp_service(self, db) -> McpService:
|
||||
"""
|
||||
Create MCP service instance
|
||||
|
||||
@@ -67,7 +67,7 @@ class McpToolService:
|
||||
|
||||
logger.info(f"Sending HTTP request: {method} {url}")
|
||||
logger.debug(f"Query parameters: {query_params}")
|
||||
logger.debug(f"Request body: {request_body}")
|
||||
# logger.debug(f"Request body: {request_body}")
|
||||
|
||||
async with create_mcp_http_client(headers=headers) as client:
|
||||
if method == "GET":
|
||||
|
||||
@@ -307,7 +307,7 @@ class HttpRequestBuilder:
|
||||
request_body[param_name] = arguments[param_name]
|
||||
|
||||
if request_body:
|
||||
logger.debug(f"Built request body from parameters: {request_body}")
|
||||
# logger.debug(f"Built request body from parameters: {request_body}")
|
||||
return request_body
|
||||
else:
|
||||
# If not a parameters list, try to parse as JSON schema object
|
||||
@@ -322,7 +322,7 @@ class HttpRequestBuilder:
|
||||
request_body[prop_name] = arguments[prop_name]
|
||||
|
||||
if request_body:
|
||||
logger.debug(f"Built request body from schema: {request_body}")
|
||||
# logger.debug(f"Built request body from schema: {request_body}")
|
||||
return request_body
|
||||
except json.JSONDecodeError:
|
||||
logger.warning(f"Failed to parse request body schema as JSON")
|
||||
|
||||
@@ -4,6 +4,9 @@
|
||||
# 4xx Client Errors
|
||||
INVALID_REQUEST = {"code": 400, "message": "Invalid request parameters"}
|
||||
MISSING_PARAMETER = {"code": 400, "message": "Required parameter is missing"}
|
||||
INVALID_PRICE = {"code": 400, "message": "Price must be a non-negative number"}
|
||||
INVALID_INPUT_TOKEN = {"code": 400, "message": "Input token must be a non-negative number"}
|
||||
INVALID_OUTPUT_TOKEN = {"code": 400, "message": "Output token must be a non-negative number"}
|
||||
INVALID_URL = {"code": 400, "message": "Invalid URL provided"}
|
||||
INVALID_FORMAT = {"code": 400, "message": "Invalid data format"}
|
||||
|
||||
|
||||
@@ -1,4 +1,17 @@
|
||||
from sqlalchemy.orm import DeclarativeBase
|
||||
from enum import Enum as PyEnum
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
pass
|
||||
|
||||
|
||||
class AuthMethod(PyEnum):
|
||||
FREE = "free"
|
||||
APIKEY = "apikey"
|
||||
TOKEN = "token"
|
||||
|
||||
|
||||
class ChargeType(PyEnum):
|
||||
FREE = "free"
|
||||
PER_CALL = "per_call"
|
||||
PER_TOKEN = "per_token"
|
||||
@@ -1,21 +1,8 @@
|
||||
from sqlalchemy import String, Enum, Numeric, Integer, DateTime
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from enum import Enum as PyEnum
|
||||
from datetime import datetime
|
||||
from services.common.models.base import Base
|
||||
|
||||
|
||||
class AuthMethod(PyEnum):
|
||||
FREE = "free"
|
||||
APIKEY = "apikey"
|
||||
TOKEN = "token"
|
||||
|
||||
|
||||
class ChargeType(PyEnum):
|
||||
FREE = "free"
|
||||
PER_CALL = "per_call"
|
||||
PER_TOKEN = "per_token"
|
||||
from services.common.models.base import Base,ChargeType
|
||||
|
||||
|
||||
class McpService(Base):
|
||||
@@ -31,14 +18,7 @@ class McpService(Base):
|
||||
slug_name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, comment="Unique slug name for the service")
|
||||
short_description: Mapped[str] = mapped_column(String, nullable=False, comment="Short description of the service")
|
||||
long_description: Mapped[str] = mapped_column(String, nullable=True, comment="Detailed description of the service (Markdown format)")
|
||||
# auth_method: Mapped[AuthMethod] = mapped_column(
|
||||
# Enum(AuthMethod, values_callable=lambda obj: [e.value for e in obj]),
|
||||
# nullable=False,
|
||||
# comment="Authentication method: free, apikey, token",
|
||||
# )
|
||||
base_url: Mapped[str] = mapped_column(String(512), nullable=True, comment="api url")
|
||||
# auth_header: Mapped[str] = mapped_column(String(255), nullable=True, comment="Authentication header name")
|
||||
# auth_token: Mapped[str] = mapped_column(String(255), nullable=True, comment="Authentication token value")
|
||||
headers: Mapped[str] = mapped_column(String, nullable=True, comment="Additional headers for requests (JSON Array format)")
|
||||
charge_type: Mapped[ChargeType] = mapped_column(
|
||||
Enum(ChargeType, values_callable=lambda obj: [e.value for e in obj]),
|
||||
@@ -54,6 +34,7 @@ class McpService(Base):
|
||||
)
|
||||
enabled: Mapped[int] = mapped_column(Integer, nullable=True, comment="Service status: 0=disabled, 1=enabled")
|
||||
tags: Mapped[str] = mapped_column(String, nullable=True, comment="Tags")
|
||||
service_type: Mapped[str] = mapped_column(String(255), nullable=True, comment="Service type",default="openapi")
|
||||
created_at: Mapped[datetime] = mapped_column(
|
||||
DateTime,
|
||||
nullable=True,
|
||||
|
||||
@@ -1,23 +1,10 @@
|
||||
from sqlalchemy import String, Enum, Numeric, Integer, DateTime
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
from enum import Enum as PyEnum
|
||||
from services.common.models.base import Base
|
||||
from services.common.models.base import Base,ChargeType
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class AuthMethod(PyEnum):
|
||||
FREE = "free"
|
||||
APIKEY = "apikey"
|
||||
TOKEN = "token"
|
||||
|
||||
|
||||
class ChargeType(PyEnum):
|
||||
FREE = "free"
|
||||
PER_CALL = "per_call"
|
||||
PER_TOKEN = "per_token"
|
||||
|
||||
|
||||
class TempMcpService(Base):
|
||||
__tablename__ = "temp_mcp_service"
|
||||
|
||||
@@ -31,15 +18,8 @@ class TempMcpService(Base):
|
||||
slug_name: Mapped[str] = mapped_column(String(255), unique=True, nullable=False, comment="Unique slug name for the service")
|
||||
short_description: Mapped[str] = mapped_column(String, nullable=False, comment="Short description of the service")
|
||||
long_description: Mapped[str] = mapped_column(String, nullable=True, comment="Detailed description of the service (Markdown format)")
|
||||
# auth_method: Mapped[AuthMethod] = mapped_column(
|
||||
# Enum(AuthMethod, values_callable=lambda obj: [e.value for e in obj]),
|
||||
# nullable=False,
|
||||
# comment="Authentication method: free, apikey, token",
|
||||
# )
|
||||
base_url: Mapped[str] = mapped_column(String(512), nullable=True, comment="api url")
|
||||
headers: Mapped[str] = mapped_column(String, nullable=True, comment="Additional headers for requests (JSON Array format)")
|
||||
# auth_header: Mapped[str] = mapped_column(String(255), nullable=True, comment="Authentication header name")
|
||||
# auth_token: Mapped[str] = mapped_column(String(255), nullable=True, comment="Authentication token value")
|
||||
charge_type: Mapped[ChargeType] = mapped_column(
|
||||
Enum(ChargeType, values_callable=lambda obj: [e.value for e in obj]),
|
||||
nullable=False,
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import datetime
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, field_serializer
|
||||
from typing import Optional
|
||||
from decimal import Decimal
|
||||
|
||||
class UserManagerResponse(BaseModel):
|
||||
user_id: Optional[str] = None
|
||||
email: Optional[str] = None
|
||||
balance: Optional[float] = None
|
||||
balance: Optional[Decimal] = None
|
||||
created_at: Optional[datetime.datetime] = None
|
||||
|
||||
@field_serializer('balance', when_used='json')
|
||||
def serialize_balance(self, v: Optional[Decimal]) -> Optional[float]:
|
||||
return float(v) if v is not None else None
|
||||
@@ -1,6 +1,11 @@
|
||||
from pydantic import BaseModel
|
||||
from pydantic import BaseModel, field_serializer
|
||||
from typing import Optional
|
||||
from decimal import Decimal
|
||||
|
||||
|
||||
class UserWalletResponse(BaseModel):
|
||||
balance: Optional[float] = None
|
||||
balance: Optional[Decimal] = None
|
||||
|
||||
@field_serializer('balance', when_used='json')
|
||||
def serialize_balance(self, v: Optional[Decimal]) -> Optional[float]:
|
||||
return float(v) if v is not None else None
|
||||
@@ -1,5 +1,8 @@
|
||||
from typing import get_origin, get_args
|
||||
from enum import Enum as PyEnum
|
||||
from decimal import Decimal
|
||||
from datetime import datetime
|
||||
from sqlalchemy import DateTime, Numeric
|
||||
|
||||
|
||||
class SqlalchemyUtils:
|
||||
@@ -7,10 +10,13 @@ class SqlalchemyUtils:
|
||||
@staticmethod
|
||||
def model_to_dict(model):
|
||||
result = {column.name: getattr(model, column.name) for column in model.__table__.columns}
|
||||
# Normalize Enum values to primitive for safe serialization
|
||||
for key, value in list(result.items()):
|
||||
if isinstance(value, PyEnum):
|
||||
result[key] = value.value
|
||||
elif isinstance(value, Decimal):
|
||||
result[key] = str(value)
|
||||
elif isinstance(value, datetime):
|
||||
result[key] = value.isoformat()
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
@@ -27,31 +33,42 @@ class SqlalchemyUtils:
|
||||
"""
|
||||
instance = model_class()
|
||||
|
||||
# Get all column names for the model
|
||||
column_names = {column.name for column in model_class.__table__.columns}
|
||||
# Map column definitions by name for type-aware restoration
|
||||
columns_by_name = {column.name: column for column in model_class.__table__.columns}
|
||||
|
||||
# Set attributes that exist in both the dictionary and the model
|
||||
for key, value in data_dict.items():
|
||||
if key in column_names and hasattr(instance, key):
|
||||
# Attempt to restore Enum types from strings using type annotations
|
||||
annotations = getattr(model_class, "__annotations__", {})
|
||||
ann = annotations.get(key)
|
||||
target_type = None
|
||||
if ann is not None:
|
||||
origin = get_origin(ann)
|
||||
if origin is None:
|
||||
target_type = ann
|
||||
else:
|
||||
args = get_args(ann)
|
||||
if args:
|
||||
target_type = args[0]
|
||||
column = columns_by_name.get(key)
|
||||
if column is None:
|
||||
continue
|
||||
|
||||
if isinstance(target_type, type) and issubclass(target_type, PyEnum) and isinstance(value, str):
|
||||
try:
|
||||
value = target_type(value)
|
||||
except Exception:
|
||||
pass
|
||||
# Restore Enum from string using annotations if available
|
||||
annotations = getattr(model_class, "__annotations__", {})
|
||||
ann = annotations.get(key)
|
||||
target_type = None
|
||||
if ann is not None:
|
||||
origin = get_origin(ann)
|
||||
if origin is None:
|
||||
target_type = ann
|
||||
else:
|
||||
args = get_args(ann)
|
||||
if args:
|
||||
target_type = args[0]
|
||||
if isinstance(target_type, type) and issubclass(target_type, PyEnum) and isinstance(value, str):
|
||||
try:
|
||||
value = target_type(value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
setattr(instance, key, value)
|
||||
# Restore Numeric and DateTime types from JSON-friendly forms
|
||||
try:
|
||||
if isinstance(column.type, Numeric) and value is not None:
|
||||
if not isinstance(value, Decimal):
|
||||
value = Decimal(str(value))
|
||||
elif isinstance(column.type, DateTime) and isinstance(value, str):
|
||||
value = datetime.fromisoformat(value)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
setattr(instance, key, value)
|
||||
|
||||
return instance
|
||||
Reference in new issue
Block a user