Coverage for app / services / agent_context / inputs.py: 98%
59 statements
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
« prev ^ index » next coverage.py v7.13.5, created at 2026-10-31 11:21 +0000
1"""What the eight kinds read about one agent, for one viewer in one
2workspace, loaded once and only when a kind asks.
4Every figure is one the agent's own page or the engine already reads: its
5grants (and their kinds), its skillsets, the rules the engine would apply to
6it here, svc-ai's tool catalogue, and what it remembers of the viewer — the
7viewer's own memories, never another member's.
8"""
10from __future__ import annotations
12from dataclasses import dataclass
13from functools import cached_property
14from typing import Any
15from uuid import UUID
17from sqlalchemy import func
18from sqlalchemy.orm import Session
20from app.db.postgres.enums import RuleEnum
21from app.db.postgres.models import (
22 Agent,
23 AgentRepository,
24 AgentSkillset,
25 KnowledgeRepository,
26 RepositoryKnowledge,
27 Rule,
28 Skillset,
29 User,
30)
31from app.util import knowledge_categories
32from app.util.knowledge_categories import KnowledgeCategory
35@dataclass(frozen=True)
36class Granted:
37 """A repository granted to the agent, as one of its kinds."""
39 id: UUID
40 name: str
41 category: KnowledgeCategory
42 sources: int
45@dataclass
46class AgentContextInputs:
47 db: Session
48 agent: Agent
49 ws: Any
50 viewer: User
52 @cached_property
53 def repositories(self) -> list[Granted]:
54 """Every repository granted to the agent, built-ins included, by
55 name."""
56 sources = (
57 self.db.query(
58 RepositoryKnowledge.repository_id,
59 func.count(RepositoryKnowledge.knowledge_id).label("n"),
60 )
61 .group_by(RepositoryKnowledge.repository_id)
62 .subquery()
63 )
64 rows = (
65 self.db.query(
66 KnowledgeRepository.id,
67 KnowledgeRepository.name,
68 KnowledgeRepository.category,
69 func.coalesce(sources.c.n, 0),
70 )
71 .join(
72 AgentRepository,
73 AgentRepository.repository_id == KnowledgeRepository.id,
74 )
75 .outerjoin(sources, sources.c.repository_id == KnowledgeRepository.id)
76 .filter(AgentRepository.agent_id == self.agent.id)
77 .order_by(KnowledgeRepository.name)
78 .all()
79 )
80 return [
81 Granted(
82 id=rid,
83 name=name,
84 category=knowledge_categories.of(category),
85 sources=int(count or 0),
86 )
87 for rid, name, category, count in rows
88 ]
90 def of_kind(self, category: KnowledgeCategory) -> list[Granted]:
91 return [r for r in self.repositories if r.category is category]
93 @cached_property
94 def skillsets(self) -> list[str]:
95 """The names of its skillsets."""
96 return [
97 name
98 for (name,) in self.db.query(Skillset.name)
99 .join(AgentSkillset, AgentSkillset.skillset_id == Skillset.id)
100 .filter(AgentSkillset.agent_id == self.agent.id)
101 .order_by(Skillset.name)
102 .all()
103 ]
105 @cached_property
106 def skills(self) -> list[Any]:
107 """Its skills, as a turn reads them: oldest first, the first the one
108 it answers through (``transformers._load_agent_skills``)."""
109 from app.router.app.agents import transformers
111 return transformers._load_agent_skills(self.db, self.agent)
113 @cached_property
114 def rules(self) -> list[Rule]:
115 """The active rules the engine applies to the agent's turns in this
116 workspace, every layer, in flow order (``rule_engine``)."""
117 from app.services import rule_engine
118 from app.services.rules import scopes
120 links = scopes.invocation_chain(
121 agent=self.agent, model_id=getattr(self.agent, "ai_model_id", None)
122 )
123 return rule_engine._get_applicable_rules(
124 self.db,
125 list(RuleEnum.Layer),
126 str(self.viewer.id),
127 links,
128 workspace=self.ws,
129 )
131 @cached_property
132 def tools(self) -> list[str] | None:
133 """svc-ai's built-in tools, bound to every agent; None when svc-ai
134 cannot say."""
135 from app.services import ai_client
137 try:
138 return [str(t.get("name")) for t in ai_client.list_tools() if t.get("name")]
139 except ai_client.AIServiceError:
140 return None
142 @cached_property
143 def memories(self) -> int | None:
144 """How many things it remembers of the viewer; None when svc-ai
145 cannot say."""
146 from app.services.network import knowledge as network
148 summaries = network.memory_summaries(
149 [str(self.agent.id)], user_id=str(self.viewer.id)
150 )
151 if summaries is None:
152 return None
153 summary = summaries.get(str(self.agent.id)) or {}
154 return int(summary.get("entries") or 0)
157__all__ = ["AgentContextInputs", "Granted"]