File size: 14,280 Bytes
042f4ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Agent registry service -- register, update, list, and manage agents."""

from uuid import UUID

from sqlalchemy import func, select
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import selectinload

from src.core.embeddings import EmbeddingService
from src.core.encryption import decrypt_value
from src.core.exceptions import ForbiddenError, NotFoundError
from src.models.agent import Agent, AgentStatus
from src.models.skill import AgentSkill
from src.models.user import User
from src.schemas.agent import AgentCreate, AgentUpdate


class RegistryService:
    """Handles agent registration, updates, listing, and agent-card generation."""

    def __init__(self, db: AsyncSession):
        self.db = db

    async def _resolve_embeddings(
        self, owner_id: UUID, agent_embedding_config: dict | None = None
    ) -> EmbeddingService:
        """Resolve embedding service: agent config → user keys → platform config."""
        # Get owner's LLM keys
        owner_keys: dict[str, str] = {}
        user = await self.db.get(User, owner_id)
        if user and user.llm_api_keys:
            for provider, encrypted in user.llm_api_keys.items():
                decrypted = decrypt_value(encrypted)
                if decrypted:
                    owner_keys[provider] = decrypted

        return EmbeddingService.for_agent(
            agent_embedding_config=agent_embedding_config,
            owner_llm_keys=owner_keys,
        )

    # ------------------------------------------------------------------
    # Register
    # ------------------------------------------------------------------

    async def register_agent(self, owner_id: UUID, data: AgentCreate) -> Agent:
        """Create a new agent record together with its skills.

        Steps:
            1. Check for duplicate endpoint.
            2. Persist the Agent row.
            3. Persist each AgentSkill row.
            4. Generate embeddings for every skill and store them.
            5. Return the agent with skills loaded.
        """
        # Reject duplicate endpoints (check active/inactive agents)
        if data.endpoint:
            existing = await self.db.execute(
                select(Agent.id).where(
                    Agent.endpoint == data.endpoint,
                    Agent.status.in_([AgentStatus.ACTIVE, AgentStatus.INACTIVE]),
                )
            )
            if existing.scalar_one_or_none():
                from fastapi import HTTPException
                raise HTTPException(
                    status_code=409,
                    detail=f"An agent with endpoint '{data.endpoint}' is already registered.",
                )

        embedding_cfg = data.embedding_config.model_dump() if data.embedding_config else {}

        # Generate DID keypair for the agent
        from src.core.did import encrypt_private_key, generate_did_keypair
        public_key, private_key = generate_did_keypair()

        agent = Agent(
            owner_id=owner_id,
            name=data.name,
            description=data.description,
            version=data.version,
            endpoint=data.endpoint,
            capabilities=data.capabilities,
            security_schemes=data.security_schemes,
            category=data.category,
            tags=data.tags,
            pricing=data.pricing.model_dump(),
            license_type=data.pricing.license_type.value,
            sla=data.sla.model_dump() if data.sla else {},
            embedding_config=embedding_cfg,
            accepted_payment_methods=data.accepted_payment_methods,
            mcp_server_url=getattr(data, "mcp_server_url", None),
            avatar_url=getattr(data, "avatar_url", None),
            conversation_starters=getattr(data, "conversation_starters", []),
            test_cases=getattr(data, "test_cases", []),
            did_public_key=public_key,
            did_private_key_encrypted=encrypt_private_key(private_key),
            metadata_={},
            status=AgentStatus.ACTIVE,
        )
        self.db.add(agent)
        await self.db.flush()

        # Create skills and collect texts for batch embedding
        skills: list[AgentSkill] = []
        embedding_texts: list[str] = []
        for skill_data in data.skills:
            skill = AgentSkill(
                agent_id=agent.id,
                skill_key=skill_data.skill_key,
                name=skill_data.name,
                description=skill_data.description,
                input_modes=skill_data.input_modes,
                output_modes=skill_data.output_modes,
                examples=[ex.model_dump() for ex in skill_data.examples],
                avg_credits=skill_data.avg_credits,
                avg_latency_ms=skill_data.avg_latency_ms,
            )
            self.db.add(skill)
            skills.append(skill)
            embedding_texts.append(f"{skill_data.name}: {skill_data.description}")

        await self.db.flush()

        # Generate and store embeddings (agent → user → platform resolution)
        if embedding_texts:
            embed_svc = await self._resolve_embeddings(owner_id, embedding_cfg or None)
            embeddings = await embed_svc.generate_batch(embedding_texts)
            for skill, emb in zip(skills, embeddings):
                skill.embedding = emb
            await self.db.flush()

        await self.db.commit()
        # Re-fetch with skills eagerly loaded.
        # populate_existing=True forces SQLAlchemy to overwrite the identity-map
        # cache so skills are actually loaded even with expire_on_commit=False.
        result = await self.db.execute(
            select(Agent)
            .options(selectinload(Agent.skills))
            .where(Agent.id == agent.id)
            .execution_options(populate_existing=True)
        )
        return result.scalar_one()

    # ------------------------------------------------------------------
    # List
    # ------------------------------------------------------------------

    async def list_agents(
        self,
        page: int = 1,
        per_page: int = 20,
        category: str | None = None,
        status: str = "active",
        owner_id: str | None = None,
    ) -> tuple[list[Agent], int]:
        """Return a paginated list of agents with optional filters."""
        query = select(Agent).options(selectinload(Agent.skills))

        if status:
            # Support "active" returning both active and unavailable (public-facing)
            if status == "active":
                query = query.where(
                    Agent.status.in_([AgentStatus.ACTIVE, AgentStatus.UNAVAILABLE])
                )
            else:
                query = query.where(Agent.status == status)
        if category:
            query = query.where(Agent.category == category)
        if owner_id:
            query = query.where(Agent.owner_id == owner_id)

        # Total count
        count_query = select(func.count()).select_from(query.subquery())
        total = (await self.db.execute(count_query)).scalar_one()

        # Paginate
        offset = (page - 1) * per_page
        query = query.offset(offset).limit(per_page).order_by(Agent.created_at.desc())
        result = await self.db.execute(query)
        agents = list(result.scalars().unique().all())

        return agents, total

    # ------------------------------------------------------------------
    # Get
    # ------------------------------------------------------------------

    async def get_agent(self, agent_id: UUID) -> Agent:
        """Get a single agent by ID with skills eagerly loaded."""
        query = (
            select(Agent)
            .options(selectinload(Agent.skills))
            .where(Agent.id == agent_id)
        )
        result = await self.db.execute(query)
        agent = result.scalars().first()
        if not agent:
            raise NotFoundError(detail=f"Agent {agent_id} not found")
        return agent

    # ------------------------------------------------------------------
    # Update
    # ------------------------------------------------------------------

    async def update_agent(
        self, agent_id: UUID, owner_id: UUID, data: AgentUpdate
    ) -> Agent:
        """Update agent fields after verifying ownership."""
        agent = await self.get_agent(agent_id)
        if agent.owner_id != owner_id:
            raise ForbiddenError(detail="You do not own this agent")

        update_data = data.model_dump(exclude_unset=True)

        # Handle nested pydantic models
        if "pricing" in update_data and update_data["pricing"] is not None:
            update_data["pricing"] = data.pricing.model_dump()
        if "sla" in update_data and update_data["sla"] is not None:
            update_data["sla"] = data.sla.model_dump()
        if "embedding_config" in update_data and update_data["embedding_config"] is not None:
            update_data["embedding_config"] = data.embedding_config.model_dump()

        # Handle skills separately -- rebuild skill rows and embeddings
        new_skills_data = update_data.pop("skills", None)
        for field, value in update_data.items():
            setattr(agent, field, value)

        if new_skills_data is not None:
            # Remove existing skills
            for existing in list(agent.skills):
                await self.db.delete(existing)
            await self.db.flush()

            skills: list[AgentSkill] = []
            embedding_texts: list[str] = []
            for skill_data in data.skills:
                skill = AgentSkill(
                    agent_id=agent.id,
                    skill_key=skill_data.skill_key,
                    name=skill_data.name,
                    description=skill_data.description,
                    input_modes=skill_data.input_modes,
                    output_modes=skill_data.output_modes,
                    examples=[ex.model_dump() for ex in skill_data.examples],
                    avg_credits=skill_data.avg_credits,
                    avg_latency_ms=skill_data.avg_latency_ms,
                )
                self.db.add(skill)
                skills.append(skill)
                embedding_texts.append(f"{skill_data.name}: {skill_data.description}")

            await self.db.flush()

            if embedding_texts:
                embed_svc = await self._resolve_embeddings(
                    owner_id, agent.embedding_config or None
                )
                embeddings = await embed_svc.generate_batch(embedding_texts)
                for skill, emb in zip(skills, embeddings):
                    skill.embedding = emb
                await self.db.flush()

        await self.db.commit()
        result = await self.db.execute(
            select(Agent)
            .options(selectinload(Agent.skills))
            .where(Agent.id == agent.id)
            .execution_options(populate_existing=True)
        )
        return result.scalar_one()

    # ------------------------------------------------------------------
    # Deactivate
    # ------------------------------------------------------------------

    async def deactivate_agent(self, agent_id: UUID, owner_id: UUID) -> None:
        """Set agent status to inactive after verifying ownership."""
        agent = await self.get_agent(agent_id)
        if agent.owner_id != owner_id:
            raise ForbiddenError(detail="You do not own this agent")

        agent.status = AgentStatus.INACTIVE
        await self.db.commit()

    async def hard_delete_agent(self, agent_id: UUID, owner_id: UUID) -> None:
        """Permanently delete an agent and its skills from the database."""
        agent = await self.get_agent(agent_id)
        if agent.owner_id != owner_id:
            raise ForbiddenError(detail="You do not own this agent")

        await self.db.delete(agent)
        await self.db.commit()

    # ------------------------------------------------------------------
    # Agent Card
    # ------------------------------------------------------------------

    async def get_agent_card(self, agent_id: UUID) -> dict:
        """Generate an A2A-spec compliant agent card JSON."""
        agent = await self.get_agent(agent_id)

        # Collect default input/output modes from all skills
        all_input_modes: set[str] = set()
        all_output_modes: set[str] = set()
        skill_cards: list[dict] = []
        for skill in agent.skills:
            all_input_modes.update(skill.input_modes or [])
            all_output_modes.update(skill.output_modes or [])
            skill_cards.append(
                {
                    "id": skill.skill_key,
                    "name": skill.name,
                    "description": skill.description,
                    "inputModes": skill.input_modes or [],
                    "outputModes": skill.output_modes or [],
                    "examples": skill.examples or [],
                }
            )

        return {
            "name": agent.name,
            "description": agent.description or "",
            "url": agent.endpoint,
            "version": agent.version,
            "capabilities": agent.capabilities or {},
            "skills": skill_cards,
            "securitySchemes": agent.security_schemes if isinstance(agent.security_schemes, list) else [],
            "defaultInputModes": sorted(all_input_modes),
            "defaultOutputModes": sorted(all_output_modes),
        }

    # ------------------------------------------------------------------
    # Verification
    # ------------------------------------------------------------------

    async def request_verification(self, agent_id: UUID, owner_id: UUID) -> dict:
        """Placeholder -- submit a verification request for the agent."""
        agent = await self.get_agent(agent_id)
        if agent.owner_id != owner_id:
            raise ForbiddenError(detail="You do not own this agent")

        return {
            "agent_id": str(agent.id),
            "current_level": agent.verification_level,
            "status": "pending",
            "message": "Verification request submitted. Review typically takes 24-48 hours.",
        }