数据库模型字段添加;数据模型添加;utils_message传参控制

This commit is contained in:
UnCLAS-Prommer
2026-03-06 22:45:47 +08:00
parent baae2df154
commit c2b75a03d7
4 changed files with 68 additions and 1 deletions

View File

@@ -15,7 +15,7 @@ class BaseDataModel:
return copy.deepcopy(self)
class BaseDatabaseDataModel(ABC, Generic[T]):
class BaseDatabaseDataModel(ABC, Generic[T], BaseDataModel):
@classmethod
@abstractmethod
def from_db_instance(cls, db_record: T) -> Self:

View File

@@ -0,0 +1,64 @@
from typing import Optional
from src.common.database.database_model import Jargon
from . import BaseDatabaseDataModel
class MaiJargon(BaseDatabaseDataModel[Jargon]):
"""Jargon 数据模型,与数据库模型 Jargon 互转。"""
def __init__(
self,
content: str,
meaning: str,
raw_content: Optional[str] = None,
session_id: Optional[str] = None,
count: int = 0,
is_jargon: Optional[bool] = True,
is_global: bool = False,
is_complete: bool = False,
inference_with_context: Optional[str] = None,
inference_with_content_only: Optional[str] = None,
):
self.content = content
self.raw_content = raw_content
self.meaning = meaning
self.session_id = session_id
self.count = count
self.is_jargon = is_jargon
self.is_global = is_global
self.is_complete = is_complete
self.inference_with_context = inference_with_context
self.inference_with_content_only = inference_with_content_only
@classmethod
def from_db_instance(cls, db_record: Jargon) -> "MaiJargon":
"""从数据库模型创建 MaiJargon 实例。"""
return cls(
content=db_record.content,
meaning=db_record.meaning,
raw_content=db_record.raw_content,
session_id=db_record.session_id,
count=db_record.count,
is_jargon=db_record.is_jargon,
is_global=db_record.is_global,
is_complete=db_record.is_complete,
inference_with_context=db_record.inference_with_context,
inference_with_content_only=db_record.inference_with_content_only,
)
def to_db_instance(self) -> Jargon:
"""将 MaiJargon 转换为数据库模型 Jargon。"""
return Jargon(
content=self.content,
raw_content=self.raw_content,
meaning=self.meaning,
session_id=self.session_id,
count=self.count,
is_jargon=self.is_jargon,
is_global=self.is_global,
is_complete=self.is_complete,
inference_with_context=self.inference_with_context,
inference_with_content_only=self.inference_with_content_only,
)

View File

@@ -208,6 +208,7 @@ class Jargon(SQLModel, table=True):
count: int = Field(default=0) # 使用次数
is_jargon: Optional[bool] = Field(default=True) # 是否为黑话False表示为白话
is_global: bool = Field(default=False) # 是否为全局黑话
is_complete: bool = Field(default=False) # 是否为已经完成全部推断count > 100后不再推断
inference_with_context: Optional[str] = Field(default=None, nullable=True) # 带上下文的推断结果JSON格式
inference_with_content_only: Optional[str] = Field(default=None, nullable=True) # 只基于词条的推断结果JSON格式

View File

@@ -145,6 +145,7 @@ class MessageUtils:
@staticmethod
async def build_readable_message(
messages: List["SessionMessage"],
*,
anonymize: bool = False,
show_lineno: bool = False,
extract_pictures: bool = False,
@@ -437,6 +438,7 @@ class MessageUtils:
]
return component
# TODO: 这个函数的实现非常临时后续需要替换为更完善的实现比如直接从配置文件中读取机器人自己的ID或者通过API获取机器人自己的信息等
def is_bot_self(user_id: str) -> bool:
"""