File size: 15,015 Bytes
d8c6e01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# Copyright (c) 2026 CrewHub. All rights reserved.
# Proprietary and confidential. See LICENSE for details.
"""Discovery service -- search, recommend, and explore agents."""

import logging
import time
from uuid import UUID

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

from src.config import settings
from src.core.embeddings import EmbeddingService, MissingAPIKeyError
from src.models.agent import Agent, AgentStatus
from src.models.skill import AgentSkill
from src.models.task import Task
from src.schemas.agent import AgentResponse
from src.schemas.discovery import AgentMatch, DiscoveryResponse, SearchQuery

logger = logging.getLogger(__name__)


class DiscoveryService:
    """Search and discovery of marketplace agents.

    Uses pgvector DB-side cosine similarity on PostgreSQL.
    Falls back to in-memory cosine similarity on SQLite (local dev).
    """

    # Composite ranking weights
    W_RELEVANCE = 0.35
    W_REPUTATION = 0.25
    W_SUCCESS = 0.20
    W_LATENCY = 0.10
    W_COST = 0.10

    def __init__(
        self,
        db: AsyncSession,
        user_llm_keys: dict[str, str] | None = None,
        user_id: str | None = None,
        account_tier: str = "free",
    ):
        self.db = db
        self._search_hint: str | None = None

        # Resolve API key for the configured embedding provider
        provider = settings.embedding_provider.lower()
        api_key = (user_llm_keys or {}).get(provider)

        self.embeddings = EmbeddingService(
            api_key=api_key,
            user_id=user_id,
            account_tier=account_tier,
        )

    # ------------------------------------------------------------------
    # Search
    # ------------------------------------------------------------------

    async def search(self, query: SearchQuery) -> DiscoveryResponse:
        """Route to the appropriate search strategy based on query.mode."""
        start = time.monotonic()

        if query.mode.value == "keyword":
            agents, total = await self._keyword_search(query)
        elif query.mode.value in ("semantic", "intent"):
            agents, total = await self._semantic_search(query)
        elif query.mode.value == "capability":
            agents, total = await self._capability_search(query)
        else:
            agents, total = await self._keyword_search(query)

        # Apply common post-filters and rank
        matches = self._rank_results(agents, query)

        elapsed_ms = (time.monotonic() - start) * 1000
        return DiscoveryResponse(
            matches=matches[: query.limit],
            total_candidates=total,
            query_time_ms=round(elapsed_ms, 2),
            hint=self._search_hint,
        )

    # ------------------------------------------------------------------
    # Keyword search
    # ------------------------------------------------------------------

    async def _keyword_search(
        self, query: SearchQuery
    ) -> tuple[list[Agent], int]:
        """SQL ILIKE search on agent name, description, skill names/descriptions."""
        pattern = f"%{query.query}%"
        stmt = (
            select(Agent)
            .options(selectinload(Agent.skills))
            .outerjoin(AgentSkill, AgentSkill.agent_id == Agent.id)
            .where(Agent.status == AgentStatus.ACTIVE)
            .where(
                (Agent.name.ilike(pattern))
                | (Agent.description.ilike(pattern))
                | (AgentSkill.name.ilike(pattern))
                | (AgentSkill.description.ilike(pattern))
            )
        )

        stmt = self._apply_filters(stmt, query)

        # Count distinct agents (avoid DISTINCT on full rows — JSON columns lack equality operator)
        count_stmt = select(func.count(func.distinct(Agent.id))).select_from(
            Agent.__table__.outerjoin(AgentSkill, AgentSkill.agent_id == Agent.id)
        ).where(Agent.status == AgentStatus.ACTIVE).where(
            (Agent.name.ilike(pattern))
            | (Agent.description.ilike(pattern))
            | (AgentSkill.name.ilike(pattern))
            | (AgentSkill.description.ilike(pattern))
        )
        total = (await self.db.execute(count_stmt)).scalar_one()

        stmt = stmt.limit(query.limit)
        result = await self.db.execute(stmt)
        agents = list(result.scalars().unique().all())

        return agents, total

    # ------------------------------------------------------------------
    # Semantic search (pgvector DB-side on PG, in-memory fallback on SQLite)
    # ------------------------------------------------------------------

    async def _semantic_search(
        self, query: SearchQuery
    ) -> tuple[list[Agent], int]:
        """Generate embedding for query, compute cosine similarity against skill embeddings.

        On PostgreSQL: uses pgvector's <=> operator for DB-side cosine distance.
        On SQLite: falls back to Python-side cosine similarity.
        Gracefully degrades to keyword search if no API key is configured.
        """
        try:
            query_embedding = await self.embeddings.generate(query.query)
        except MissingAPIKeyError:
            logger.info("No embedding API key — falling back to keyword search")
            self._search_hint = (
                "Configure an API key in Settings > LLM Keys for smarter semantic search."
            )
            return await self._keyword_search(query)

        url = str(self.db.get_bind().url)
        if "postgresql" in url or "asyncpg" in url:
            return await self._semantic_search_db(query, query_embedding)
        return await self._semantic_search_python(query, query_embedding)

    async def _semantic_search_db(
        self, query: SearchQuery, query_embedding: list[float]
    ) -> tuple[list[Agent], int]:
        """DB-side semantic search using pgvector cosine distance."""
        # Subquery: best similarity per agent
        best_sim = (
            select(
                AgentSkill.agent_id,
                func.min(AgentSkill.embedding.cosine_distance(query_embedding)).label("min_dist"),
            )
            .where(AgentSkill.embedding.isnot(None))
            .group_by(AgentSkill.agent_id)
            .subquery()
        )

        # Main query: agents ranked by best skill similarity
        stmt = (
            select(Agent)
            .join(best_sim, Agent.id == best_sim.c.agent_id)
            .options(selectinload(Agent.skills))
            .where(Agent.status == AgentStatus.ACTIVE)
            .order_by(best_sim.c.min_dist)
        )
        stmt = self._apply_filters(stmt, query)

        # Count: how many agents have at least one embedded skill
        count_stmt = (
            select(func.count(func.distinct(AgentSkill.agent_id)))
            .where(AgentSkill.embedding.isnot(None))
            .join(Agent, Agent.id == AgentSkill.agent_id)
            .where(Agent.status == AgentStatus.ACTIVE)
        )
        total = (await self.db.execute(count_stmt)).scalar_one()

        stmt = stmt.limit(query.limit)
        result = await self.db.execute(stmt)
        agents = list(result.scalars().unique().all())

        return agents, total

    async def _semantic_search_python(
        self, query: SearchQuery, query_embedding: list[float]
    ) -> tuple[list[Agent], int]:
        """In-memory semantic search fallback for SQLite."""
        stmt = (
            select(Agent)
            .options(selectinload(Agent.skills).undefer(AgentSkill.embedding))
            .where(Agent.status == AgentStatus.ACTIVE)
        )
        stmt = self._apply_filters(stmt, query)

        result = await self.db.execute(stmt)
        agents = list(result.scalars().unique().all())

        if not agents:
            return [], 0

        scored = []
        for agent in agents:
            best_sim = 0.0
            for skill in agent.skills:
                if skill.embedding is not None:
                    sim = self._cosine_similarity(query_embedding, list(skill.embedding))
                    best_sim = max(best_sim, sim)
            scored.append((agent, best_sim))

        scored.sort(key=lambda x: x[1], reverse=True)
        top_agents = [a for a, _ in scored[: query.limit]]
        return top_agents, len(scored)

    @staticmethod
    def _cosine_similarity(a: list[float], b: list[float]) -> float:
        """Compute cosine similarity between two vectors."""
        if len(a) != len(b) or not a:
            return 0.0
        dot = sum(x * y for x, y in zip(a, b))
        norm_a = sum(x * x for x in a) ** 0.5
        norm_b = sum(x * x for x in b) ** 0.5
        if norm_a == 0 or norm_b == 0:
            return 0.0
        return dot / (norm_a * norm_b)

    # ------------------------------------------------------------------
    # Capability search
    # ------------------------------------------------------------------

    async def _capability_search(
        self, query: SearchQuery
    ) -> tuple[list[Agent], int]:
        """Filter by input_modes, output_modes, category, max_latency, max_credits."""
        stmt = (
            select(Agent)
            .options(selectinload(Agent.skills))
            .outerjoin(AgentSkill, AgentSkill.agent_id == Agent.id)
            .where(Agent.status == AgentStatus.ACTIVE)
        )

        stmt = self._apply_filters(stmt, query)

        # Count distinct agents (JSON columns lack equality operator for DISTINCT)
        count_stmt = select(func.count(func.distinct(Agent.id))).select_from(
            Agent.__table__.outerjoin(AgentSkill, AgentSkill.agent_id == Agent.id)
        ).where(Agent.status == AgentStatus.ACTIVE)
        total = (await self.db.execute(count_stmt)).scalar_one()

        stmt = stmt.limit(query.limit)
        result = await self.db.execute(stmt)
        agents = list(result.scalars().unique().all())

        return agents, total

    # ------------------------------------------------------------------
    # Shared filter helper
    # ------------------------------------------------------------------

    @staticmethod
    def _apply_filters(stmt, query: SearchQuery):
        """Apply common filters (category, tags, reputation, latency, credits)."""
        if query.category:
            stmt = stmt.where(Agent.category == query.category)
        if query.tags:
            stmt = stmt.where(Agent.tags.contains(query.tags))
        if query.min_reputation > 0:
            stmt = stmt.where(Agent.reputation_score >= query.min_reputation)
        if query.max_latency_ms is not None:
            stmt = stmt.where(Agent.avg_latency_ms <= query.max_latency_ms)
        return stmt

    # ------------------------------------------------------------------
    # Ranking
    # ------------------------------------------------------------------

    def _rank_results(
        self, agents: list[Agent], query: SearchQuery
    ) -> list[AgentMatch]:
        """Rank agents by composite score."""
        if not agents:
            return []

        max_latency = max((a.avg_latency_ms or 1) for a in agents) or 1

        matches: list[AgentMatch] = []
        for agent in agents:
            relevance = 1.0
            reputation = min(agent.reputation_score / 5.0, 1.0)
            success = agent.success_rate
            latency_factor = 1.0 - ((agent.avg_latency_ms or 0) / max_latency)
            cost_factor = 0.5

            composite = (
                self.W_RELEVANCE * relevance
                + self.W_REPUTATION * reputation
                + self.W_SUCCESS * success
                + self.W_LATENCY * latency_factor
                + self.W_COST * cost_factor
            )

            matches.append(
                AgentMatch(
                    agent=AgentResponse.model_validate(agent),
                    relevance_score=round(composite, 4),
                    match_reason=f"Matched via {query.mode.value} search",
                )
            )

        matches.sort(key=lambda m: m.relevance_score, reverse=True)
        return matches

    # ------------------------------------------------------------------
    # Recommendations
    # ------------------------------------------------------------------

    async def get_recommendations(self, agent_id: UUID) -> list[AgentMatch]:
        """Find agents with complementary skills (different category, high reputation)."""
        source = await self.db.get(Agent, agent_id)
        if not source:
            return []

        stmt = (
            select(Agent)
            .options(selectinload(Agent.skills))
            .where(Agent.status == AgentStatus.ACTIVE)
            .where(Agent.id != agent_id)
            .where(Agent.category != source.category)
            .order_by(Agent.reputation_score.desc())
            .limit(10)
        )
        result = await self.db.execute(stmt)
        agents = list(result.scalars().unique().all())

        return [
            AgentMatch(
                agent=AgentResponse.model_validate(a),
                relevance_score=round(a.reputation_score / 5.0, 4),
                match_reason="Complementary category with high reputation",
            )
            for a in agents
        ]

    # ------------------------------------------------------------------
    # Categories
    # ------------------------------------------------------------------

    async def get_categories(self) -> list[dict]:
        """Return distinct categories with agent counts."""
        stmt = (
            select(Agent.category, func.count(Agent.id).label("count"))
            .where(Agent.status == AgentStatus.ACTIVE)
            .where(Agent.category.isnot(None))
            .group_by(Agent.category)
            .order_by(func.count(Agent.id).desc())
        )
        result = await self.db.execute(stmt)
        rows = result.all()
        return [{"category": row.category, "agent_count": row.count} for row in rows]

    # ------------------------------------------------------------------
    # Trending skills
    # ------------------------------------------------------------------

    async def get_trending_skills(self) -> list[dict]:
        """Return skills ordered by recent task volume."""
        stmt = (
            select(
                AgentSkill.id,
                AgentSkill.name,
                AgentSkill.description,
                func.count(Task.id).label("task_count"),
            )
            .outerjoin(Task, Task.skill_id == AgentSkill.id)
            .group_by(AgentSkill.id, AgentSkill.name, AgentSkill.description)
            .order_by(func.count(Task.id).desc())
            .limit(20)
        )
        result = await self.db.execute(stmt)
        rows = result.all()
        return [
            {
                "skill_id": str(row.id),
                "name": row.name,
                "description": row.description,
                "task_count": row.task_count,
            }
            for row in rows
        ]