feat: add skill and assistant knowledge bases
This commit is contained in:
1 parent
dd0ee282df
commit
fbd8dec48f
45 files changed
+5262
-21
No files matched your search
+15
@@ -0,0 +1,15 @@
|
||||
from deerflow.persistence.skill_knowledge.model import ( # noqa: F401
|
||||
SkillKnowledgeArtifactRow,
|
||||
SkillKnowledgeBindingRow,
|
||||
SkillKnowledgeEntityContributionRow,
|
||||
SkillKnowledgeEntityRow,
|
||||
SkillKnowledgeRelationContributionRow,
|
||||
SkillKnowledgeRelationRow,
|
||||
SkillKnowledgeReviewRow,
|
||||
SkillKnowledgeSnapshotRow,
|
||||
SkillKnowledgeSyncItemRow,
|
||||
SkillKnowledgeSyncJobRow,
|
||||
)
|
||||
from deerflow.persistence.skill_knowledge.store import SkillKnowledgeRepository, make_skill_knowledge_store # noqa: F401
|
||||
|
||||
__all__ = [name for name in globals() if name.startswith("SkillKnowledge") or name == "make_skill_knowledge_store"]
|
||||
+203
@@ -0,0 +1,203 @@
|
||||
"""ORM rows for skill-to-knowledge distillation and review."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime
|
||||
|
||||
from sqlalchemy import Boolean, Float, Integer, String, UniqueConstraint, false, true
|
||||
from sqlalchemy.orm import Mapped, mapped_column
|
||||
|
||||
from deerflow.persistence.base import Base
|
||||
from deerflow.persistence.types import BeijingDateTime, PortableJSON, PortableLongText
|
||||
|
||||
|
||||
def _now() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
class SkillKnowledgeBindingRow(Base):
|
||||
__tablename__ = "skill_knowledge_bindings"
|
||||
__table_args__ = (UniqueConstraint("skill_name", "target_type", "target_id", name="uq_skill_knowledge_binding"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
skill_name: Mapped[str] = mapped_column(String(255), index=True)
|
||||
target_type: Mapped[str] = mapped_column(String(32), index=True)
|
||||
target_mode: Mapped[str] = mapped_column(String(32), default="wiki")
|
||||
target_id: Mapped[str] = mapped_column(String(128), index=True)
|
||||
target_name: Mapped[str] = mapped_column(String(255), default="")
|
||||
remote_business_key: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
||||
remote_refs_json: Mapped[list | None] = mapped_column(PortableJSON(), default=list, nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(48), default="pending", index=True)
|
||||
active: Mapped[bool] = mapped_column(Boolean, default=True, server_default=true(), index=True)
|
||||
auto_sync_enabled: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false(), index=True)
|
||||
current_snapshot_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
|
||||
source_digest: Mapped[str | None] = mapped_column(String(80), nullable=True)
|
||||
synced_digest: Mapped[str | None] = mapped_column(String(80), nullable=True)
|
||||
last_job_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
|
||||
last_item_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
|
||||
last_synced_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
||||
last_error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
last_error_message: Mapped[str | None] = mapped_column(String(2048), nullable=True)
|
||||
created_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now, onupdate=_now)
|
||||
|
||||
|
||||
class SkillKnowledgeSnapshotRow(Base):
|
||||
__tablename__ = "skill_knowledge_snapshots"
|
||||
__table_args__ = (UniqueConstraint("skill_name", "source_digest", "extractor_version", "prompt_hash", "model_name", name="uq_skill_knowledge_snapshot"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
skill_name: Mapped[str] = mapped_column(String(255), index=True)
|
||||
source_digest: Mapped[str] = mapped_column(String(80), index=True)
|
||||
extractor_version: Mapped[str] = mapped_column(String(64))
|
||||
prompt_hash: Mapped[str] = mapped_column(String(80))
|
||||
model_name: Mapped[str] = mapped_column(String(128))
|
||||
manifest_json: Mapped[list | None] = mapped_column(PortableJSON(), default=list, nullable=True)
|
||||
ir_json: Mapped[dict | None] = mapped_column(PortableJSON(), default=dict, nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(24), default="ready")
|
||||
counts_json: Mapped[dict | None] = mapped_column(PortableJSON(), default=dict, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now)
|
||||
|
||||
|
||||
class SkillKnowledgeSyncJobRow(Base):
|
||||
__tablename__ = "skill_knowledge_sync_jobs"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
trigger: Mapped[str] = mapped_column(String(32), default="manual")
|
||||
target_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
targets_json: Mapped[list | None] = mapped_column(PortableJSON(), default=list, nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(32), default="queued", index=True)
|
||||
total_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
success_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
partial_failed_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
failed_count: Mapped[int] = mapped_column(Integer, default=0)
|
||||
cancel_requested: Mapped[bool] = mapped_column(Boolean, default=False, server_default=false())
|
||||
review_mode: Mapped[str] = mapped_column(String(32), default="auto_high_confidence")
|
||||
created_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now)
|
||||
started_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
||||
completed_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
||||
|
||||
|
||||
class SkillKnowledgeSyncItemRow(Base):
|
||||
__tablename__ = "skill_knowledge_sync_items"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
job_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
binding_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
target_type: Mapped[str] = mapped_column(String(32))
|
||||
target_mode: Mapped[str] = mapped_column(String(32))
|
||||
target_id: Mapped[str] = mapped_column(String(128))
|
||||
skill_name: Mapped[str] = mapped_column(String(255), index=True)
|
||||
snapshot_id: Mapped[str | None] = mapped_column(String(36), nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(32), default="queued", index=True)
|
||||
phase: Mapped[str] = mapped_column(String(32), default="scanning")
|
||||
progress: Mapped[int] = mapped_column(Integer, default=0)
|
||||
attempts: Mapped[int] = mapped_column(Integer, default=0)
|
||||
active_dedupe_key: Mapped[str | None] = mapped_column(String(36), nullable=True, unique=True)
|
||||
lease_owner: Mapped[str | None] = mapped_column(String(128), nullable=True, index=True)
|
||||
lease_until: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True, index=True)
|
||||
last_error_code: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
last_error_message: Mapped[str | None] = mapped_column(String(2048), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now, onupdate=_now)
|
||||
started_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
||||
completed_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
||||
|
||||
|
||||
class SkillKnowledgeArtifactRow(Base):
|
||||
__tablename__ = "skill_knowledge_artifacts"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
binding_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
snapshot_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
kind: Mapped[str] = mapped_column(String(48), index=True)
|
||||
slug: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
||||
content_text: Mapped[str | None] = mapped_column(PortableLongText(), nullable=True)
|
||||
remote_id: Mapped[str | None] = mapped_column(String(255), nullable=True)
|
||||
business_key: Mapped[str | None] = mapped_column(String(512), nullable=True)
|
||||
remote_refs_json: Mapped[list | None] = mapped_column(PortableJSON(), default=list, nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(32), default="staged", index=True)
|
||||
metadata_json: Mapped[dict | None] = mapped_column(PortableJSON(), default=dict, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now, onupdate=_now)
|
||||
|
||||
|
||||
class SkillKnowledgeReviewRow(Base):
|
||||
__tablename__ = "skill_knowledge_reviews"
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
job_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
item_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
binding_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
snapshot_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
object_type: Mapped[str] = mapped_column(String(32), index=True)
|
||||
object_key: Mapped[str] = mapped_column(String(128))
|
||||
original_json: Mapped[dict | None] = mapped_column(PortableJSON(), default=dict, nullable=True)
|
||||
edited_json: Mapped[dict | None] = mapped_column(PortableJSON(), nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(24), default="pending", index=True)
|
||||
reason: Mapped[str | None] = mapped_column(String(1024), nullable=True)
|
||||
reviewed_by: Mapped[str | None] = mapped_column(String(64), nullable=True)
|
||||
reviewed_at: Mapped[datetime | None] = mapped_column(BeijingDateTime(), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now)
|
||||
|
||||
|
||||
class SkillKnowledgeEntityRow(Base):
|
||||
__tablename__ = "skill_knowledge_entities"
|
||||
__table_args__ = (UniqueConstraint("target_type", "target_id", "entity_type", "normalized_name", name="uq_skill_knowledge_entity"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
target_type: Mapped[str] = mapped_column(String(32), index=True)
|
||||
target_id: Mapped[str] = mapped_column(String(128), index=True)
|
||||
entity_type: Mapped[str] = mapped_column(String(64), index=True)
|
||||
normalized_name: Mapped[str] = mapped_column(String(512))
|
||||
display_name: Mapped[str] = mapped_column(String(512))
|
||||
aliases_json: Mapped[list | None] = mapped_column(PortableJSON(), default=list, nullable=True)
|
||||
attributes_json: Mapped[dict | None] = mapped_column(PortableJSON(), default=dict, nullable=True)
|
||||
status: Mapped[str] = mapped_column(String(24), default="active")
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now, onupdate=_now)
|
||||
|
||||
|
||||
class SkillKnowledgeEntityContributionRow(Base):
|
||||
__tablename__ = "skill_knowledge_entity_contributions"
|
||||
__table_args__ = (UniqueConstraint("entity_id", "binding_id", "snapshot_id", "local_entity_id", name="uq_skill_entity_contribution"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
entity_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
binding_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
snapshot_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
local_entity_id: Mapped[str] = mapped_column(String(128))
|
||||
evidence_json: Mapped[list | None] = mapped_column(PortableJSON(), default=list, nullable=True)
|
||||
attributes_json: Mapped[dict | None] = mapped_column(PortableJSON(), default=dict, nullable=True)
|
||||
confidence: Mapped[float] = mapped_column(Float, default=1.0)
|
||||
|
||||
|
||||
class SkillKnowledgeRelationRow(Base):
|
||||
__tablename__ = "skill_knowledge_relations"
|
||||
__table_args__ = (UniqueConstraint("target_type", "target_id", "source_entity_id", "predicate", "target_entity_id", name="uq_skill_knowledge_relation"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
target_type: Mapped[str] = mapped_column(String(32), index=True)
|
||||
target_id: Mapped[str] = mapped_column(String(128), index=True)
|
||||
source_entity_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
predicate: Mapped[str] = mapped_column(String(128), index=True)
|
||||
target_entity_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
description: Mapped[str] = mapped_column(String(2048), default="")
|
||||
status: Mapped[str] = mapped_column(String(24), default="active")
|
||||
created_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now)
|
||||
updated_at: Mapped[datetime] = mapped_column(BeijingDateTime(), default=_now, onupdate=_now)
|
||||
|
||||
|
||||
class SkillKnowledgeRelationContributionRow(Base):
|
||||
__tablename__ = "skill_knowledge_relation_contributions"
|
||||
__table_args__ = (UniqueConstraint("relation_id", "binding_id", "snapshot_id", "local_relation_id", name="uq_skill_relation_contribution"),)
|
||||
|
||||
id: Mapped[str] = mapped_column(String(36), primary_key=True)
|
||||
relation_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
binding_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
snapshot_id: Mapped[str] = mapped_column(String(36), index=True)
|
||||
local_relation_id: Mapped[str] = mapped_column(String(128))
|
||||
evidence_json: Mapped[list | None] = mapped_column(PortableJSON(), default=list, nullable=True)
|
||||
confidence: Mapped[float] = mapped_column(Float, default=1.0)
|
||||
+758
@@ -0,0 +1,758 @@
|
||||
"""SQL repository for skill knowledge jobs, snapshots and contributions."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import UTC, datetime, timedelta
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from sqlalchemy import delete, func, or_, select, update
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
|
||||
from deerflow.persistence.skill_knowledge.model import (
|
||||
SkillKnowledgeArtifactRow,
|
||||
SkillKnowledgeBindingRow,
|
||||
SkillKnowledgeEntityContributionRow,
|
||||
SkillKnowledgeEntityRow,
|
||||
SkillKnowledgeRelationContributionRow,
|
||||
SkillKnowledgeRelationRow,
|
||||
SkillKnowledgeReviewRow,
|
||||
SkillKnowledgeSnapshotRow,
|
||||
SkillKnowledgeSyncItemRow,
|
||||
SkillKnowledgeSyncJobRow,
|
||||
)
|
||||
from deerflow.skill_knowledge.merge import normalize_name, normalize_predicate
|
||||
|
||||
|
||||
def _now() -> datetime:
|
||||
return datetime.now(UTC)
|
||||
|
||||
|
||||
def _dict(row: Any) -> dict[str, Any]:
|
||||
list_json_fields = {
|
||||
"aliases_json",
|
||||
"evidence_json",
|
||||
"manifest_json",
|
||||
"refs_json",
|
||||
"targets_json",
|
||||
}
|
||||
data = row.to_dict()
|
||||
for key, value in tuple(data.items()):
|
||||
if isinstance(value, datetime):
|
||||
data[key] = value.isoformat()
|
||||
elif key.endswith("_json"):
|
||||
data[key[:-5]] = data.pop(key) or ([] if key in list_json_fields else {})
|
||||
return data
|
||||
|
||||
|
||||
class SkillKnowledgeRepository:
|
||||
def __init__(self, session_factory: async_sessionmaker[AsyncSession]) -> None:
|
||||
self._sf = session_factory
|
||||
|
||||
async def _retire_target_orphans(self, session: AsyncSession, *, target_type: str, target_id: str) -> None:
|
||||
entities = (
|
||||
await session.execute(
|
||||
select(SkillKnowledgeEntityRow).where(
|
||||
SkillKnowledgeEntityRow.target_type == target_type,
|
||||
SkillKnowledgeEntityRow.target_id == target_id,
|
||||
)
|
||||
)
|
||||
).scalars()
|
||||
for entity in entities:
|
||||
contribution = (await session.execute(select(SkillKnowledgeEntityContributionRow.id).where(SkillKnowledgeEntityContributionRow.entity_id == entity.id).limit(1))).scalar_one_or_none()
|
||||
entity.status = "active" if contribution else "deleted"
|
||||
relations = (
|
||||
await session.execute(
|
||||
select(SkillKnowledgeRelationRow).where(
|
||||
SkillKnowledgeRelationRow.target_type == target_type,
|
||||
SkillKnowledgeRelationRow.target_id == target_id,
|
||||
)
|
||||
)
|
||||
).scalars()
|
||||
for relation in relations:
|
||||
contribution = (await session.execute(select(SkillKnowledgeRelationContributionRow.id).where(SkillKnowledgeRelationContributionRow.relation_id == relation.id).limit(1))).scalar_one_or_none()
|
||||
relation.status = "active" if contribution else "deleted"
|
||||
|
||||
async def create_job(
|
||||
self,
|
||||
*,
|
||||
skill_names: list[str],
|
||||
targets: list[dict[str, Any]],
|
||||
created_by: str | None,
|
||||
force: bool = False,
|
||||
trigger: str = "manual",
|
||||
review_mode: str = "auto_high_confidence",
|
||||
) -> dict[str, Any]:
|
||||
names = list(dict.fromkeys(name.strip() for name in skill_names if name.strip()))
|
||||
normalized_targets: list[dict[str, Any]] = []
|
||||
seen_targets: set[tuple[str, str]] = set()
|
||||
for target in targets:
|
||||
target_type = str(target.get("target_type") or "").strip()
|
||||
target_id = str(target.get("target_id") or "").strip()
|
||||
if not target_type or not target_id or (target_type, target_id) in seen_targets:
|
||||
continue
|
||||
seen_targets.add((target_type, target_id))
|
||||
normalized_targets.append(
|
||||
{
|
||||
"target_type": target_type,
|
||||
"target_mode": str(target.get("target_mode") or ("wiki" if target_type == "weknora" else "assistant")),
|
||||
"target_id": target_id,
|
||||
"target_name": str(target.get("target_name") or target_id),
|
||||
"remote_business_key": target.get("remote_business_key"),
|
||||
"readonly": bool(target.get("readonly", False)),
|
||||
}
|
||||
)
|
||||
job_id = str(uuid4())
|
||||
now = _now()
|
||||
async with self._sf() as session:
|
||||
job = SkillKnowledgeSyncJobRow(
|
||||
id=job_id,
|
||||
trigger=trigger,
|
||||
target_count=len(normalized_targets),
|
||||
targets_json=normalized_targets,
|
||||
status="queued",
|
||||
total_count=0,
|
||||
review_mode=review_mode,
|
||||
created_by=created_by,
|
||||
created_at=now,
|
||||
)
|
||||
session.add(job)
|
||||
for skill_name in names:
|
||||
for target in normalized_targets:
|
||||
stmt = select(SkillKnowledgeBindingRow).where(
|
||||
SkillKnowledgeBindingRow.skill_name == skill_name,
|
||||
SkillKnowledgeBindingRow.target_type == target["target_type"],
|
||||
SkillKnowledgeBindingRow.target_id == target["target_id"],
|
||||
)
|
||||
binding = (await session.execute(stmt)).scalar_one_or_none()
|
||||
if binding is None:
|
||||
binding = SkillKnowledgeBindingRow(
|
||||
id=str(uuid4()),
|
||||
skill_name=skill_name,
|
||||
target_type=target["target_type"],
|
||||
target_mode=target["target_mode"],
|
||||
target_id=target["target_id"],
|
||||
target_name=target["target_name"],
|
||||
remote_business_key=target.get("remote_business_key"),
|
||||
status="queued",
|
||||
created_by=created_by,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
session.add(binding)
|
||||
else:
|
||||
active_item = (
|
||||
await session.execute(
|
||||
select(SkillKnowledgeSyncItemRow.id)
|
||||
.where(
|
||||
SkillKnowledgeSyncItemRow.binding_id == binding.id,
|
||||
SkillKnowledgeSyncItemRow.status.in_(["queued", "running", "awaiting_review"]),
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
if active_item is not None:
|
||||
continue
|
||||
binding.target_mode = target["target_mode"]
|
||||
binding.target_name = target["target_name"]
|
||||
binding.remote_business_key = target.get("remote_business_key")
|
||||
binding.status = "queued"
|
||||
binding.active = True
|
||||
binding.updated_at = now
|
||||
item = SkillKnowledgeSyncItemRow(
|
||||
id=str(uuid4()),
|
||||
job_id=job_id,
|
||||
binding_id=binding.id,
|
||||
target_type=target["target_type"],
|
||||
target_mode=target["target_mode"],
|
||||
target_id=target["target_id"],
|
||||
skill_name=skill_name,
|
||||
status="queued",
|
||||
phase="scanning",
|
||||
progress=0,
|
||||
active_dedupe_key=binding.id,
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
session.add(item)
|
||||
binding.last_job_id = job_id
|
||||
binding.last_item_id = item.id
|
||||
job.total_count += 1
|
||||
if job.total_count == 0:
|
||||
job.status = "completed"
|
||||
job.completed_at = now
|
||||
await session.flush()
|
||||
result = _dict(job)
|
||||
result["force"] = force
|
||||
await session.commit()
|
||||
return result
|
||||
|
||||
async def list_bindings(self, *, skill_names: list[str] | None = None, target_id: str | None = None, include_detached: bool = False) -> list[dict[str, Any]]:
|
||||
stmt = select(SkillKnowledgeBindingRow)
|
||||
if skill_names:
|
||||
stmt = stmt.where(SkillKnowledgeBindingRow.skill_name.in_(skill_names))
|
||||
if target_id:
|
||||
stmt = stmt.where(SkillKnowledgeBindingRow.target_id == target_id)
|
||||
if not include_detached:
|
||||
stmt = stmt.where(SkillKnowledgeBindingRow.active.is_(True))
|
||||
stmt = stmt.order_by(SkillKnowledgeBindingRow.updated_at.desc())
|
||||
async with self._sf() as session:
|
||||
return [_dict(row) for row in (await session.execute(stmt)).scalars()]
|
||||
|
||||
async def get_binding(self, binding_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(SkillKnowledgeBindingRow, binding_id)
|
||||
return _dict(row) if row else None
|
||||
|
||||
async def list_artifacts(self, binding_id: str, *, active_only: bool = True) -> list[dict[str, Any]]:
|
||||
stmt = select(SkillKnowledgeArtifactRow).where(SkillKnowledgeArtifactRow.binding_id == binding_id)
|
||||
if active_only:
|
||||
stmt = stmt.where(SkillKnowledgeArtifactRow.status == "active")
|
||||
async with self._sf() as session:
|
||||
return [_dict(row) for row in (await session.execute(stmt)).scalars()]
|
||||
|
||||
async def has_active_item(self, binding_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
value = (
|
||||
await session.execute(
|
||||
select(SkillKnowledgeSyncItemRow.id)
|
||||
.where(
|
||||
SkillKnowledgeSyncItemRow.binding_id == binding_id,
|
||||
SkillKnowledgeSyncItemRow.status.in_(["queued", "running", "awaiting_review"]),
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
return value is not None
|
||||
|
||||
async def set_binding_auto_sync(self, binding_id: str, *, enabled: bool) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(SkillKnowledgeBindingRow, binding_id)
|
||||
if row is None or not row.active:
|
||||
return None
|
||||
row.auto_sync_enabled = enabled
|
||||
row.updated_at = _now()
|
||||
await session.flush()
|
||||
data = _dict(row)
|
||||
await session.commit()
|
||||
return data
|
||||
|
||||
async def get_job(self, job_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
job = await session.get(SkillKnowledgeSyncJobRow, job_id)
|
||||
if job is None:
|
||||
return None
|
||||
items = (await session.execute(select(SkillKnowledgeSyncItemRow).where(SkillKnowledgeSyncItemRow.job_id == job_id).order_by(SkillKnowledgeSyncItemRow.created_at))).scalars()
|
||||
result = _dict(job)
|
||||
result["items"] = [_dict(item) for item in items]
|
||||
return result
|
||||
|
||||
async def list_jobs(self, *, limit: int = 50) -> list[dict[str, Any]]:
|
||||
stmt = select(SkillKnowledgeSyncJobRow).order_by(SkillKnowledgeSyncJobRow.created_at.desc()).limit(limit)
|
||||
async with self._sf() as session:
|
||||
return [_dict(row) for row in (await session.execute(stmt)).scalars()]
|
||||
|
||||
async def list_queued_items(self, *, limit: int = 20) -> list[dict[str, Any]]:
|
||||
now = _now()
|
||||
stmt = (
|
||||
select(SkillKnowledgeSyncItemRow)
|
||||
.where(
|
||||
or_(
|
||||
SkillKnowledgeSyncItemRow.status == "queued",
|
||||
((SkillKnowledgeSyncItemRow.status == "running") & (SkillKnowledgeSyncItemRow.lease_until < now)),
|
||||
)
|
||||
)
|
||||
.order_by(SkillKnowledgeSyncItemRow.created_at)
|
||||
.limit(limit)
|
||||
)
|
||||
async with self._sf() as session:
|
||||
return [_dict(row) for row in (await session.execute(stmt)).scalars()]
|
||||
|
||||
async def claim_item(
|
||||
self,
|
||||
item_id: str,
|
||||
*,
|
||||
owner: str,
|
||||
lease_seconds: int = 900,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Atomically claim a queued item, or reclaim one whose worker lease expired."""
|
||||
now = _now()
|
||||
stmt = (
|
||||
update(SkillKnowledgeSyncItemRow)
|
||||
.where(
|
||||
SkillKnowledgeSyncItemRow.id == item_id,
|
||||
or_(
|
||||
SkillKnowledgeSyncItemRow.status == "queued",
|
||||
((SkillKnowledgeSyncItemRow.status == "running") & (SkillKnowledgeSyncItemRow.lease_until < now)),
|
||||
),
|
||||
)
|
||||
.values(
|
||||
status="running",
|
||||
started_at=func.coalesce(SkillKnowledgeSyncItemRow.started_at, now),
|
||||
attempts=SkillKnowledgeSyncItemRow.attempts + 1,
|
||||
lease_owner=owner,
|
||||
lease_until=now + timedelta(seconds=lease_seconds),
|
||||
updated_at=now,
|
||||
)
|
||||
)
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(stmt)
|
||||
if result.rowcount != 1:
|
||||
await session.rollback()
|
||||
return None
|
||||
row = await session.get(SkillKnowledgeSyncItemRow, item_id)
|
||||
data = _dict(row)
|
||||
await session.commit()
|
||||
await self.refresh_job(data["job_id"])
|
||||
return data
|
||||
|
||||
async def renew_lease(self, item_id: str, *, owner: str, lease_seconds: int = 900) -> bool:
|
||||
now = _now()
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
update(SkillKnowledgeSyncItemRow)
|
||||
.where(
|
||||
SkillKnowledgeSyncItemRow.id == item_id,
|
||||
SkillKnowledgeSyncItemRow.status == "running",
|
||||
SkillKnowledgeSyncItemRow.lease_owner == owner,
|
||||
)
|
||||
.values(
|
||||
lease_until=now + timedelta(seconds=lease_seconds),
|
||||
updated_at=now,
|
||||
)
|
||||
)
|
||||
await session.commit()
|
||||
return result.rowcount == 1
|
||||
|
||||
async def update_item(
|
||||
self,
|
||||
item_id: str,
|
||||
*,
|
||||
status: str | None = None,
|
||||
phase: str | None = None,
|
||||
progress: int | None = None,
|
||||
snapshot_id: str | None = None,
|
||||
error_code: str | None = None,
|
||||
error_message: str | None = None,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(SkillKnowledgeSyncItemRow, item_id)
|
||||
if row is None:
|
||||
return None
|
||||
if status is not None:
|
||||
row.status = status
|
||||
if status == "running" and row.started_at is None:
|
||||
row.started_at = _now()
|
||||
row.attempts += 1
|
||||
if status in {"completed", "partial_failed", "failed", "canceled"}:
|
||||
row.completed_at = _now()
|
||||
row.active_dedupe_key = None
|
||||
elif status == "queued":
|
||||
row.completed_at = None
|
||||
if status != "running":
|
||||
row.lease_owner = None
|
||||
row.lease_until = None
|
||||
if phase is not None:
|
||||
row.phase = phase
|
||||
if progress is not None:
|
||||
row.progress = max(0, min(100, progress))
|
||||
if snapshot_id is not None:
|
||||
row.snapshot_id = snapshot_id
|
||||
row.last_error_code = error_code
|
||||
row.last_error_message = error_message
|
||||
row.updated_at = _now()
|
||||
await session.flush()
|
||||
data = _dict(row)
|
||||
await session.commit()
|
||||
await self.refresh_job(data["job_id"])
|
||||
return data
|
||||
|
||||
async def refresh_job(self, job_id: str) -> None:
|
||||
async with self._sf() as session:
|
||||
job = await session.get(SkillKnowledgeSyncJobRow, job_id)
|
||||
if job is None:
|
||||
return
|
||||
rows = (await session.execute(select(SkillKnowledgeSyncItemRow.status, func.count()).where(SkillKnowledgeSyncItemRow.job_id == job_id).group_by(SkillKnowledgeSyncItemRow.status))).all()
|
||||
counts = {str(status): int(count) for status, count in rows}
|
||||
job.success_count = counts.get("completed", 0)
|
||||
job.partial_failed_count = counts.get("partial_failed", 0)
|
||||
job.failed_count = counts.get("failed", 0)
|
||||
active = sum(counts.get(key, 0) for key in ("queued", "running", "awaiting_review"))
|
||||
if counts.get("running", 0) or counts.get("awaiting_review", 0):
|
||||
job.status = "running"
|
||||
job.started_at = job.started_at or _now()
|
||||
elif active == 0 and sum(counts.values()) >= job.total_count:
|
||||
if job.failed_count and job.success_count == 0 and job.partial_failed_count == 0:
|
||||
job.status = "failed"
|
||||
elif job.failed_count or job.partial_failed_count:
|
||||
job.status = "partial_failed"
|
||||
else:
|
||||
job.status = "completed"
|
||||
job.completed_at = _now()
|
||||
await session.commit()
|
||||
|
||||
async def cancel_job(self, job_id: str) -> bool:
|
||||
async with self._sf() as session:
|
||||
job = await session.get(SkillKnowledgeSyncJobRow, job_id)
|
||||
if job is None:
|
||||
return False
|
||||
job.cancel_requested = True
|
||||
await session.execute(
|
||||
SkillKnowledgeSyncItemRow.__table__.update()
|
||||
.where(
|
||||
SkillKnowledgeSyncItemRow.job_id == job_id,
|
||||
SkillKnowledgeSyncItemRow.status.in_(["queued", "awaiting_review"]),
|
||||
)
|
||||
.values(
|
||||
status="canceled",
|
||||
phase="completed",
|
||||
completed_at=_now(),
|
||||
progress=100,
|
||||
active_dedupe_key=None,
|
||||
lease_owner=None,
|
||||
lease_until=None,
|
||||
)
|
||||
)
|
||||
job.status = "canceled"
|
||||
job.completed_at = _now()
|
||||
await session.commit()
|
||||
return True
|
||||
|
||||
async def requeue_item(self, item_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(SkillKnowledgeSyncItemRow, item_id)
|
||||
if row is None:
|
||||
return None
|
||||
active_item = (
|
||||
await session.execute(
|
||||
select(SkillKnowledgeSyncItemRow.id)
|
||||
.where(
|
||||
SkillKnowledgeSyncItemRow.binding_id == row.binding_id,
|
||||
SkillKnowledgeSyncItemRow.id != row.id,
|
||||
SkillKnowledgeSyncItemRow.status.in_(["queued", "running", "awaiting_review"]),
|
||||
)
|
||||
.limit(1)
|
||||
)
|
||||
).scalar_one_or_none()
|
||||
if active_item is not None:
|
||||
return None
|
||||
row.status = "queued"
|
||||
row.phase = "scanning"
|
||||
row.progress = 0
|
||||
row.completed_at = None
|
||||
row.active_dedupe_key = row.binding_id
|
||||
row.updated_at = _now()
|
||||
await session.flush()
|
||||
data = _dict(row)
|
||||
await session.commit()
|
||||
await self.refresh_job(data["job_id"])
|
||||
return data
|
||||
|
||||
async def get_or_create_snapshot(self, *, skill_name: str, source_digest: str, extractor_version: str, prompt_hash: str, model_name: str, manifest: list, ir: dict, counts: dict) -> dict[str, Any]:
|
||||
stmt = select(SkillKnowledgeSnapshotRow).where(
|
||||
SkillKnowledgeSnapshotRow.skill_name == skill_name,
|
||||
SkillKnowledgeSnapshotRow.source_digest == source_digest,
|
||||
SkillKnowledgeSnapshotRow.extractor_version == extractor_version,
|
||||
SkillKnowledgeSnapshotRow.prompt_hash == prompt_hash,
|
||||
SkillKnowledgeSnapshotRow.model_name == model_name,
|
||||
)
|
||||
async with self._sf() as session:
|
||||
row = (await session.execute(stmt)).scalar_one_or_none()
|
||||
if row is None:
|
||||
row = SkillKnowledgeSnapshotRow(
|
||||
id=str(uuid4()),
|
||||
skill_name=skill_name,
|
||||
source_digest=source_digest,
|
||||
extractor_version=extractor_version,
|
||||
prompt_hash=prompt_hash,
|
||||
model_name=model_name,
|
||||
manifest_json=manifest,
|
||||
ir_json=ir,
|
||||
counts_json=counts,
|
||||
status="ready",
|
||||
created_at=_now(),
|
||||
)
|
||||
session.add(row)
|
||||
await session.flush()
|
||||
data = _dict(row)
|
||||
await session.commit()
|
||||
return data
|
||||
|
||||
async def get_snapshot(self, snapshot_id: str) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
row = await session.get(SkillKnowledgeSnapshotRow, snapshot_id)
|
||||
return _dict(row) if row else None
|
||||
|
||||
async def create_reviews(self, *, job_id: str, item_id: str, binding_id: str, snapshot_id: str, objects: list[tuple[str, dict]]) -> int:
|
||||
rows = [
|
||||
SkillKnowledgeReviewRow(
|
||||
id=str(uuid4()),
|
||||
job_id=job_id,
|
||||
item_id=item_id,
|
||||
binding_id=binding_id,
|
||||
snapshot_id=snapshot_id,
|
||||
object_type=kind,
|
||||
object_key=str(obj.get("local_id") or obj.get("slug") or uuid4()),
|
||||
original_json=obj,
|
||||
status="pending",
|
||||
created_at=_now(),
|
||||
)
|
||||
for kind, obj in objects
|
||||
]
|
||||
async with self._sf() as session:
|
||||
session.add_all(rows)
|
||||
await session.commit()
|
||||
return len(rows)
|
||||
|
||||
async def list_reviews(self, *, job_id: str | None = None, item_id: str | None = None, status: str | None = "pending") -> list[dict[str, Any]]:
|
||||
stmt = select(SkillKnowledgeReviewRow)
|
||||
if job_id:
|
||||
stmt = stmt.where(SkillKnowledgeReviewRow.job_id == job_id)
|
||||
if item_id:
|
||||
stmt = stmt.where(SkillKnowledgeReviewRow.item_id == item_id)
|
||||
if status:
|
||||
stmt = stmt.where(SkillKnowledgeReviewRow.status == status)
|
||||
stmt = stmt.order_by(SkillKnowledgeReviewRow.created_at)
|
||||
async with self._sf() as session:
|
||||
return [_dict(row) for row in (await session.execute(stmt)).scalars()]
|
||||
|
||||
async def review(self, review_ids: list[str], *, action: str, actor: str | None, edits: dict[str, dict] | None = None, reason: str | None = None) -> list[str]:
|
||||
final_status = "approved" if action == "approve" else "rejected"
|
||||
affected_items: set[str] = set()
|
||||
async with self._sf() as session:
|
||||
rows = (await session.execute(select(SkillKnowledgeReviewRow).where(SkillKnowledgeReviewRow.id.in_(review_ids)))).scalars()
|
||||
for row in rows:
|
||||
row.status = final_status
|
||||
row.edited_json = (edits or {}).get(row.id)
|
||||
row.reason = reason
|
||||
row.reviewed_by = actor
|
||||
row.reviewed_at = _now()
|
||||
affected_items.add(row.item_id)
|
||||
await session.commit()
|
||||
return sorted(affected_items)
|
||||
|
||||
async def pending_review_count(self, item_id: str) -> int:
|
||||
stmt = select(func.count()).select_from(SkillKnowledgeReviewRow).where(SkillKnowledgeReviewRow.item_id == item_id, SkillKnowledgeReviewRow.status == "pending")
|
||||
async with self._sf() as session:
|
||||
return int((await session.execute(stmt)).scalar_one())
|
||||
|
||||
async def effective_ir(self, snapshot_id: str, item_id: str) -> dict[str, Any] | None:
|
||||
snapshot = await self.get_snapshot(snapshot_id)
|
||||
if snapshot is None:
|
||||
return None
|
||||
ir = dict(snapshot.get("ir") or {})
|
||||
reviews = await self.list_reviews(item_id=item_id, status=None)
|
||||
if not reviews:
|
||||
return ir
|
||||
by_key = {(row["object_type"], row["object_key"]): row for row in reviews}
|
||||
for key in ("entities", "relations"):
|
||||
kind = key[:-1]
|
||||
accepted = []
|
||||
for obj in ir.get(key, []):
|
||||
review = by_key.get((kind, str(obj.get("local_id"))))
|
||||
if review and review.get("status") == "rejected":
|
||||
continue
|
||||
accepted.append((review or {}).get("edited") or obj)
|
||||
ir[key] = accepted
|
||||
return ir
|
||||
|
||||
async def stage_and_activate(self, *, item_id: str, snapshot_id: str, ir: dict[str, Any], artifacts: list[dict[str, Any]], status: str = "synced") -> dict[str, Any]:
|
||||
now = _now()
|
||||
async with self._sf() as session:
|
||||
item = await session.get(SkillKnowledgeSyncItemRow, item_id)
|
||||
if item is None:
|
||||
raise KeyError(item_id)
|
||||
binding = await session.get(SkillKnowledgeBindingRow, item.binding_id)
|
||||
if binding is None:
|
||||
raise KeyError(item.binding_id)
|
||||
if not binding.active:
|
||||
raise RuntimeError("Binding was detached before staged activation")
|
||||
await session.execute(
|
||||
SkillKnowledgeArtifactRow.__table__.update()
|
||||
.where(
|
||||
SkillKnowledgeArtifactRow.binding_id == binding.id,
|
||||
SkillKnowledgeArtifactRow.status == "active",
|
||||
)
|
||||
.values(status="retired", updated_at=now)
|
||||
)
|
||||
for artifact in artifacts:
|
||||
session.add(
|
||||
SkillKnowledgeArtifactRow(
|
||||
id=str(uuid4()),
|
||||
binding_id=binding.id,
|
||||
snapshot_id=snapshot_id,
|
||||
kind=str(artifact.get("kind") or "wiki_page"),
|
||||
slug=artifact.get("slug"),
|
||||
content_text=artifact.get("content"),
|
||||
remote_id=artifact.get("remote_id"),
|
||||
business_key=artifact.get("business_key"),
|
||||
remote_refs_json=artifact.get("remote_refs") or [],
|
||||
status="active",
|
||||
metadata_json=artifact.get("metadata") or {},
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
)
|
||||
# Retire contributions from the old active version only after all staged artifacts are ready.
|
||||
await session.execute(delete(SkillKnowledgeEntityContributionRow).where(SkillKnowledgeEntityContributionRow.binding_id == binding.id))
|
||||
await session.execute(delete(SkillKnowledgeRelationContributionRow).where(SkillKnowledgeRelationContributionRow.binding_id == binding.id))
|
||||
entity_ids: dict[str, str] = {}
|
||||
for entity in ir.get("entities", []):
|
||||
normalized = normalize_name(str(entity.get("name") or ""))
|
||||
if not normalized:
|
||||
continue
|
||||
entity_type = str(entity.get("type") or "concept")
|
||||
stmt = select(SkillKnowledgeEntityRow).where(
|
||||
SkillKnowledgeEntityRow.target_type == binding.target_type,
|
||||
SkillKnowledgeEntityRow.target_id == binding.target_id,
|
||||
SkillKnowledgeEntityRow.entity_type == entity_type,
|
||||
SkillKnowledgeEntityRow.normalized_name == normalized,
|
||||
)
|
||||
canonical = (await session.execute(stmt)).scalar_one_or_none()
|
||||
if canonical is None:
|
||||
canonical = SkillKnowledgeEntityRow(
|
||||
id=str(uuid4()),
|
||||
target_type=binding.target_type,
|
||||
target_id=binding.target_id,
|
||||
entity_type=entity_type,
|
||||
normalized_name=normalized,
|
||||
display_name=str(entity.get("name") or normalized),
|
||||
aliases_json=entity.get("aliases") or [],
|
||||
attributes_json=entity.get("attributes") or {},
|
||||
status="active",
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
session.add(canonical)
|
||||
await session.flush()
|
||||
else:
|
||||
canonical.status = "active"
|
||||
entity_ids[str(entity.get("local_id"))] = canonical.id
|
||||
session.add(
|
||||
SkillKnowledgeEntityContributionRow(
|
||||
id=str(uuid4()),
|
||||
entity_id=canonical.id,
|
||||
binding_id=binding.id,
|
||||
snapshot_id=snapshot_id,
|
||||
local_entity_id=str(entity.get("local_id")),
|
||||
evidence_json=entity.get("evidence") or [],
|
||||
attributes_json=entity.get("attributes") or {},
|
||||
confidence=float(entity.get("confidence") or 0),
|
||||
)
|
||||
)
|
||||
for relation in ir.get("relations", []):
|
||||
source_id = entity_ids.get(str(relation.get("source_local_id")))
|
||||
target_id = entity_ids.get(str(relation.get("target_local_id")))
|
||||
confidence = float(relation.get("confidence") or 0)
|
||||
if not source_id or not target_id or confidence < 0.75 or not relation.get("evidence"):
|
||||
continue
|
||||
predicate = normalize_predicate(str(relation.get("predicate") or "related_to"))
|
||||
stmt = select(SkillKnowledgeRelationRow).where(
|
||||
SkillKnowledgeRelationRow.target_type == binding.target_type,
|
||||
SkillKnowledgeRelationRow.target_id == binding.target_id,
|
||||
SkillKnowledgeRelationRow.source_entity_id == source_id,
|
||||
SkillKnowledgeRelationRow.predicate == predicate,
|
||||
SkillKnowledgeRelationRow.target_entity_id == target_id,
|
||||
)
|
||||
canonical = (await session.execute(stmt)).scalar_one_or_none()
|
||||
if canonical is None:
|
||||
canonical = SkillKnowledgeRelationRow(
|
||||
id=str(uuid4()),
|
||||
target_type=binding.target_type,
|
||||
target_id=binding.target_id,
|
||||
source_entity_id=source_id,
|
||||
predicate=predicate,
|
||||
target_entity_id=target_id,
|
||||
description=str(relation.get("description") or ""),
|
||||
status="active",
|
||||
created_at=now,
|
||||
updated_at=now,
|
||||
)
|
||||
session.add(canonical)
|
||||
await session.flush()
|
||||
else:
|
||||
canonical.status = "active"
|
||||
session.add(
|
||||
SkillKnowledgeRelationContributionRow(
|
||||
id=str(uuid4()),
|
||||
relation_id=canonical.id,
|
||||
binding_id=binding.id,
|
||||
snapshot_id=snapshot_id,
|
||||
local_relation_id=str(relation.get("local_id")),
|
||||
evidence_json=relation.get("evidence") or [],
|
||||
confidence=confidence,
|
||||
)
|
||||
)
|
||||
await self._retire_target_orphans(
|
||||
session,
|
||||
target_type=binding.target_type,
|
||||
target_id=binding.target_id,
|
||||
)
|
||||
binding.current_snapshot_id = snapshot_id
|
||||
binding.source_digest = str(ir.get("skill", {}).get("source_digest") or "")
|
||||
binding.synced_digest = binding.source_digest
|
||||
binding.status = status
|
||||
binding.last_synced_at = now
|
||||
binding.last_error_code = None
|
||||
binding.last_error_message = None
|
||||
binding.updated_at = now
|
||||
item.snapshot_id = snapshot_id
|
||||
item.status = "completed" if status in {"synced", "pending_remote_write"} else status
|
||||
item.phase = "completed"
|
||||
item.progress = 100
|
||||
item.completed_at = now
|
||||
item.active_dedupe_key = None
|
||||
item.lease_owner = None
|
||||
item.lease_until = None
|
||||
item.updated_at = now
|
||||
await session.flush()
|
||||
result = _dict(binding)
|
||||
await session.commit()
|
||||
await self.refresh_job(item.job_id)
|
||||
return result
|
||||
|
||||
async def detach_binding(
|
||||
self,
|
||||
binding_id: str,
|
||||
*,
|
||||
delete_contributions: bool,
|
||||
cleanup_failed: bool = False,
|
||||
) -> dict[str, Any] | None:
|
||||
async with self._sf() as session:
|
||||
binding = await session.get(SkillKnowledgeBindingRow, binding_id)
|
||||
if binding is None:
|
||||
return None
|
||||
binding.active = False
|
||||
binding.status = "detached_delete_pending_cleanup" if delete_contributions and cleanup_failed else "detached_deleted" if delete_contributions else "detached_keep"
|
||||
binding.updated_at = _now()
|
||||
if delete_contributions:
|
||||
await session.execute(delete(SkillKnowledgeEntityContributionRow).where(SkillKnowledgeEntityContributionRow.binding_id == binding_id))
|
||||
await session.execute(delete(SkillKnowledgeRelationContributionRow).where(SkillKnowledgeRelationContributionRow.binding_id == binding_id))
|
||||
await session.execute(SkillKnowledgeArtifactRow.__table__.update().where(SkillKnowledgeArtifactRow.binding_id == binding_id).values(status="deleted", updated_at=_now()))
|
||||
await self._retire_target_orphans(
|
||||
session,
|
||||
target_type=binding.target_type,
|
||||
target_id=binding.target_id,
|
||||
)
|
||||
await session.flush()
|
||||
data = _dict(binding)
|
||||
await session.commit()
|
||||
return data
|
||||
|
||||
async def mark_skill_stale(self, skill_name: str) -> int:
|
||||
async with self._sf() as session:
|
||||
result = await session.execute(
|
||||
SkillKnowledgeBindingRow.__table__.update()
|
||||
.where(
|
||||
SkillKnowledgeBindingRow.skill_name == skill_name,
|
||||
SkillKnowledgeBindingRow.active.is_(True),
|
||||
SkillKnowledgeBindingRow.status.in_(["synced", "partial_failed", "pending_remote_write"]),
|
||||
)
|
||||
.values(status="stale", updated_at=_now())
|
||||
)
|
||||
await session.commit()
|
||||
return int(result.rowcount or 0)
|
||||
|
||||
|
||||
def make_skill_knowledge_store(session_factory):
|
||||
return SkillKnowledgeRepository(session_factory) if session_factory is not None else None
|
||||
Reference in new issue
Block a user