存档
This commit is contained in:
@@ -48,6 +48,9 @@ class BaseIndividualModel(BaseDataModel):
|
||||
nullable=True,
|
||||
index=True,
|
||||
)
|
||||
plugin_owned: Mapped[Optional[str]] = mapped_column(
|
||||
String(64), nullable=True, index=True
|
||||
)
|
||||
|
||||
__mapper_args__ = {"polymorphic_on": "agent_type", "polymorphic_identity": "base"}
|
||||
|
||||
|
||||
@@ -18,9 +18,10 @@ from kilostar.core.postgres_database.model.individual import (
|
||||
OrdinaryIndividualModel,
|
||||
SpecialIndividualModel,
|
||||
)
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy import select, and_
|
||||
from typing import List, Optional
|
||||
from kilostar.core.postgres_database.database_exception import database_exception
|
||||
from kilostar.utils.error import BusinessError
|
||||
|
||||
from ulid import ULID
|
||||
|
||||
@@ -95,7 +96,11 @@ class IndividualDatabase:
|
||||
|
||||
@database_exception
|
||||
async def delete_worker_individual(self, agent_id: str) -> bool:
|
||||
"""删除 Individual;不存在返回 False,删除成功返回 True。"""
|
||||
"""删除 Individual;不存在返回 False,删除成功返回 True。
|
||||
|
||||
``plugin_owned`` 不为空时拒绝删除(插件 agent 由插件生命周期管理,
|
||||
要清理该插件目录后由启动期兜底逻辑收回)。
|
||||
"""
|
||||
async with self.async_session_maker() as session:
|
||||
statement = select(BaseIndividualModel).where(
|
||||
BaseIndividualModel.agent_id == agent_id
|
||||
@@ -104,6 +109,11 @@ class IndividualDatabase:
|
||||
individual = results.scalar_one_or_none()
|
||||
if not individual:
|
||||
return False
|
||||
if individual.plugin_owned:
|
||||
raise BusinessError(
|
||||
f"agent {agent_id} 由插件 {individual.plugin_owned} 拥有,"
|
||||
f"不可删除(请改卸载插件)"
|
||||
)
|
||||
await session.delete(individual)
|
||||
await session.commit()
|
||||
return True
|
||||
@@ -115,3 +125,85 @@ class IndividualDatabase:
|
||||
statement = select(BaseIndividualModel)
|
||||
results = await session.execute(statement)
|
||||
return list(results.scalars().all())
|
||||
|
||||
# ─── plugin_owned slot 专用 ─────────────────────────────────────
|
||||
|
||||
@database_exception
|
||||
async def find_plugin_slot(self, plugin_name: str, slot_name: str):
|
||||
"""按 ``(plugin_owned, agent_name)`` 查 slot;返回 ORM 对象或 None。"""
|
||||
async with self.async_session_maker() as session:
|
||||
stmt = select(BaseIndividualModel).where(
|
||||
and_(
|
||||
BaseIndividualModel.plugin_owned == plugin_name,
|
||||
BaseIndividualModel.agent_name == slot_name,
|
||||
)
|
||||
)
|
||||
return (await session.execute(stmt)).scalar_one_or_none()
|
||||
|
||||
@database_exception
|
||||
async def upsert_plugin_slot(
|
||||
self,
|
||||
plugin_name: str,
|
||||
slot_name: str,
|
||||
description: str,
|
||||
owner_id: str = "system",
|
||||
node_affinity: str = "cpu",
|
||||
):
|
||||
"""插件安装期登记一个 agent slot。
|
||||
|
||||
slot 的 provider/model 留空,等用户在前端 Agent 页面装配。已存在则
|
||||
只刷新 description/node_affinity(用户自己装配的 provider+model 不被覆盖)。
|
||||
"""
|
||||
async with self.async_session_maker() as session:
|
||||
stmt = select(BaseIndividualModel).where(
|
||||
and_(
|
||||
BaseIndividualModel.plugin_owned == plugin_name,
|
||||
BaseIndividualModel.agent_name == slot_name,
|
||||
)
|
||||
)
|
||||
existing = (await session.execute(stmt)).scalar_one_or_none()
|
||||
if existing is not None:
|
||||
existing.description = description
|
||||
existing.node_affinity = node_affinity
|
||||
session.add(existing)
|
||||
await session.commit()
|
||||
await session.refresh(existing)
|
||||
return existing
|
||||
|
||||
row = BaseIndividualModel(
|
||||
agent_id=str(ULID()),
|
||||
agent_name=slot_name,
|
||||
description=description,
|
||||
provider_title="",
|
||||
model_id="",
|
||||
owner_id=owner_id,
|
||||
agent_type="base",
|
||||
node_affinity=node_affinity,
|
||||
plugin_owned=plugin_name,
|
||||
)
|
||||
session.add(row)
|
||||
await session.commit()
|
||||
await session.refresh(row)
|
||||
return row
|
||||
|
||||
@database_exception
|
||||
async def list_plugin_owned_names(self) -> List[str]:
|
||||
"""返回当前 DB 中所有出现过的 ``plugin_owned`` 值(去重)。"""
|
||||
async with self.async_session_maker() as session:
|
||||
stmt = select(BaseIndividualModel.plugin_owned).where(
|
||||
BaseIndividualModel.plugin_owned.is_not(None)
|
||||
).distinct()
|
||||
return [r for (r,) in (await session.execute(stmt)).all() if r]
|
||||
|
||||
@database_exception
|
||||
async def delete_plugin_slots(self, plugin_name: str) -> int:
|
||||
"""删掉 ``plugin_owned == plugin_name`` 的所有 slot;返回被删条数。"""
|
||||
async with self.async_session_maker() as session:
|
||||
stmt = select(BaseIndividualModel).where(
|
||||
BaseIndividualModel.plugin_owned == plugin_name
|
||||
)
|
||||
rows = list((await session.execute(stmt)).scalars().all())
|
||||
for row in rows:
|
||||
await session.delete(row)
|
||||
await session.commit()
|
||||
return len(rows)
|
||||
|
||||
@@ -246,6 +246,36 @@ class PostgresDatabase:
|
||||
await self.ready_event.wait()
|
||||
return await self._individual_database.get_all_worker_individual()
|
||||
|
||||
# plugin_owned slot 专用门面方法
|
||||
async def find_plugin_slot(self, plugin_name: str, slot_name: str):
|
||||
"""按 (plugin, slot) 查找已登记的插件 agent slot。"""
|
||||
await self.ready_event.wait()
|
||||
return await self._individual_database.find_plugin_slot(plugin_name, slot_name)
|
||||
|
||||
async def upsert_plugin_slot(
|
||||
self,
|
||||
plugin_name: str,
|
||||
slot_name: str,
|
||||
description: str,
|
||||
owner_id: str = "system",
|
||||
node_affinity: str = "cpu",
|
||||
):
|
||||
"""插件安装期登记 agent slot;用户自配 provider/model 不被覆盖。"""
|
||||
await self.ready_event.wait()
|
||||
return await self._individual_database.upsert_plugin_slot(
|
||||
plugin_name, slot_name, description, owner_id, node_affinity
|
||||
)
|
||||
|
||||
async def list_plugin_owned_names(self):
|
||||
"""枚举当前数据库中所有 plugin_owned 值(去重)。"""
|
||||
await self.ready_event.wait()
|
||||
return await self._individual_database.list_plugin_owned_names()
|
||||
|
||||
async def delete_plugin_slots(self, plugin_name: str) -> int:
|
||||
"""删掉某插件登记的全部 slot,返回被删条数。"""
|
||||
await self.ready_event.wait()
|
||||
return await self._individual_database.delete_plugin_slots(plugin_name)
|
||||
|
||||
# Workflow Database Methods
|
||||
async def create_workflow(
|
||||
self, trace_id: str, user_id: str, title: str, command: str
|
||||
|
||||
Reference in New Issue
Block a user