# Copyright 2026 zhaoxi826 # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. from kilostar.core.postgres_database.model.individual import WorkerIndividual from sqlmodel import select from typing import List, Optional from kilostar.core.postgres_database.database_exception import database_exception from ulid import ULID class IndividualDatabase: """IndividualDatabase 核心组件类。 这是一个数据库操作层 (DAO/Repository) 封装类,专注于处理实体模型与关系型数据库表之间的映射。它将复杂的 SQL 查询、跨表 Join 和事务回滚逻辑进行了高级抽象,向上层服务暴露简洁的数据读写接口。""" def __init__(self, async_session_maker): self.async_session_maker = async_session_maker @database_exception async def add_worker_individual(self, **kwargs) -> WorkerIndividual: """创建并持久化新的 worker individual 实体。 接收构建参数,执行必要的数据校验与默认值填充后,将新记录安全地写入底层存储或系统注册表中。 Returns: (WorkerIndividual): 经由当前业务模型加工处理后所输出的具体数据实例或领域模型对象。""" async with self.async_session_maker() as session: agent_id = str(ULID()) individual = WorkerIndividual(agent_id=agent_id, **kwargs) session.add(individual) await session.commit() await session.refresh(individual) return individual @database_exception async def get_worker_individual(self, agent_id: str) -> Optional[WorkerIndividual]: """检索并获取特定的 worker individual 数据集合或实例对象。 根据提供的查询条件或上下文凭证,从数据库、缓存或第三方服务中读取对应的资源状态。 Args: agent_id (str): 目标对象的唯一全局标识符 (UUID/ULID),用于在数据库表或缓存结构中精准匹配该 agent 实例。 Returns: (Optional[WorkerIndividual]): 经由当前业务模型加工处理后所输出的具体数据实例或领域模型对象。""" async with self.async_session_maker() as session: statement = select(WorkerIndividual).where( WorkerIndividual.agent_id == agent_id ) results = await session.execute(statement) return results.scalar_one_or_none() @database_exception async def get_worker_individual_list(self, owner_id: str) -> List[WorkerIndividual]: """检索并获取特定的 worker individual list 数据集合或实例对象。 根据提供的查询条件或上下文凭证,从数据库、缓存或第三方服务中读取对应的资源状态。 Args: owner_id (str): 目标对象的唯一全局标识符 (UUID/ULID),用于在数据库表或缓存结构中精准匹配该 owner 实例。 Returns: (List[WorkerIndividual]): 经过筛选、排序或分页处理后的实体对象列表集合。""" async with self.async_session_maker() as session: statement = select(WorkerIndividual).where( WorkerIndividual.owner_id == owner_id ) results = await session.execute(statement) return list(results.scalars().all()) @database_exception async def update_worker_individual( self, agent_id: str, **kwargs ) -> Optional[WorkerIndividual]: """对现有的 worker individual 进行状态更新或属性覆盖。 基于增量变更原则,合并最新的配置或数据,并触发相关依赖组件的缓存刷新或事件通知。 Args: agent_id (str): 目标对象的唯一全局标识符 (UUID/ULID),用于在数据库表或缓存结构中精准匹配该 agent 实例。 Returns: (Optional[WorkerIndividual]): 经由当前业务模型加工处理后所输出的具体数据实例或领域模型对象。""" async with self.async_session_maker() as session: statement = select(WorkerIndividual).where( WorkerIndividual.agent_id == agent_id ) results = await session.execute(statement) individual = results.scalar_one_or_none() if not individual: return None for key, value in kwargs.items(): if value is not None: setattr(individual, key, value) session.add(individual) await session.commit() await session.refresh(individual) return individual @database_exception async def delete_worker_individual(self, agent_id: str) -> bool: """安全地移除或注销 worker individual。 执行物理删除或逻辑删除操作,并妥善清理相关的关联数据及占用资源。 Args: agent_id (str): 目标对象的唯一全局标识符 (UUID/ULID),用于在数据库表或缓存结构中精准匹配该 agent 实例。 Returns: (bool): 一个布尔型结果标志,明确返回 True 表示该操作成功应用或条件达成,False 则表示失败或被拒绝。""" async with self.async_session_maker() as session: statement = select(WorkerIndividual).where( WorkerIndividual.agent_id == agent_id ) results = await session.execute(statement) individual = results.scalar_one_or_none() if not individual: return False session.delete(individual) await session.commit() return True @database_exception async def get_all_worker_individual(self) -> List[WorkerIndividual]: """检索并获取特定的 all worker individual 数据集合或实例对象。 根据提供的查询条件或上下文凭证,从数据库、缓存或第三方服务中读取对应的资源状态。 Returns: (List[WorkerIndividual]): 经过筛选、排序或分页处理后的实体对象列表集合。""" async with self.async_session_maker() as session: statement = select(WorkerIndividual) results = await session.execute(statement) return list(results.scalars().all())