kompass-sdk 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/ranking.ts ADDED
@@ -0,0 +1,336 @@
1
+ /**
2
+ * Kompass Ranking Engine
3
+ *
4
+ * Fair, cross-protocol ranking of agents/services using:
5
+ * 1. Source-specific reputation normalization
6
+ * 2. Bayesian average for cold-start protection
7
+ * 3. Value-for-money scoring (cost:quality ratio)
8
+ * 4. Geometric mean composite for final ranking
9
+ * 5. Wilson score for confidence-adjusted reputation
10
+ *
11
+ * Based on: npm scoring, Reddit Wilson, Twitter log-engagement,
12
+ * Amazon sales velocity, Airbnb market-relative pricing,
13
+ * IMDB Bayesian average, AgentRank (0xIntuition)
14
+ */
15
+
16
+ import type { UnifiedAgent } from "./sources/types.js";
17
+
18
+ // ── Types ────────────────────────────────────────────────
19
+
20
+ export interface NormalizedReputation {
21
+ /** Quality signal 0-100 (success rate, reliability) */
22
+ quality: number;
23
+ /** Volume signal 0-100 (log-normalized job count, installs) */
24
+ volume: number;
25
+ /** Recency signal 0-100 (time since last active) */
26
+ recency: number;
27
+ /** Confidence 0-1 (how much evidence backs this score) */
28
+ confidence: number;
29
+ }
30
+
31
+ export interface RankingScores {
32
+ /** How well agent matches the query (0-100) */
33
+ relevance: number;
34
+ /** Bayesian-adjusted quality (0-100) */
35
+ quality: number;
36
+ /** Log-normalized usage volume (0-100) */
37
+ volume: number;
38
+ /** Time-decay recency (0-100) */
39
+ recency: number;
40
+ /** Value for money — quality relative to price (0-100) */
41
+ value: number;
42
+ /** Final composite score */
43
+ composite: number;
44
+ }
45
+
46
+ export interface RankedAgent {
47
+ agent: UnifiedAgent;
48
+ scores: RankingScores;
49
+ explanation: string;
50
+ }
51
+
52
+ // ── Configuration ────────────────────────────────────────
53
+
54
+ const WEIGHTS = {
55
+ relevance: 0.30,
56
+ quality: 0.25,
57
+ volume: 0.15,
58
+ recency: 0.10,
59
+ value: 0.20,
60
+ };
61
+
62
+ const BAYESIAN_C = 10; // Confidence parameter for Bayesian average
63
+ const GLOBAL_AVG_QUALITY = 50; // Prior assumption: average quality is 50/100
64
+
65
+ // ── Step 1: Source-Specific Normalization ─────────────────
66
+
67
+ export function normalizeReputation(agent: UnifiedAgent): NormalizedReputation {
68
+ const rep = agent.reputation;
69
+ if (!rep) return { quality: GLOBAL_AVG_QUALITY, volume: 0, recency: 50, confidence: 0 };
70
+
71
+ switch (rep.source) {
72
+ case "acp":
73
+ // successRate is 0-100%, count is completed jobs
74
+ return {
75
+ quality: rep.score,
76
+ volume: logNormalize(rep.count, 1000),
77
+ recency: recencyScore(agent.lastSeen),
78
+ confidence: Math.min(1, rep.count / 20), // Full confidence at 20+ jobs
79
+ };
80
+
81
+ case "402index":
82
+ // reliability_score is 0-100, count is uptime percentage (0-100)
83
+ return {
84
+ quality: rep.score,
85
+ volume: rep.count, // Uptime as volume proxy
86
+ recency: recencyScore(agent.lastSeen),
87
+ confidence: 0.7, // Automated monitoring = moderate confidence
88
+ };
89
+
90
+ case "8004scan":
91
+ // total_score is 0-100, count is star_count
92
+ return {
93
+ quality: rep.score,
94
+ volume: logNormalize(rep.count, 100),
95
+ recency: recencyScore(agent.lastSeen),
96
+ confidence: Math.min(1, rep.count / 10),
97
+ };
98
+
99
+ case "skills-sh-installs":
100
+ // Installs tell popularity, NOT quality
101
+ return {
102
+ quality: GLOBAL_AVG_QUALITY, // Unknown quality — use prior
103
+ volume: logNormalize(rep.count, 100000),
104
+ recency: 50,
105
+ confidence: Math.min(0.5, rep.count / 1000), // Installs give weak quality signal, cap at 0.5
106
+ };
107
+
108
+ case "clawhub-relevance":
109
+ // Search relevance, NOT reputation
110
+ return {
111
+ quality: GLOBAL_AVG_QUALITY,
112
+ volume: 0,
113
+ recency: 50,
114
+ confidence: 0, // Relevance says nothing about quality
115
+ };
116
+
117
+ default:
118
+ return {
119
+ quality: rep.score > 0 ? Math.min(rep.score, 100) : GLOBAL_AVG_QUALITY,
120
+ volume: logNormalize(rep.count, 100),
121
+ recency: recencyScore(agent.lastSeen),
122
+ confidence: 0.3,
123
+ };
124
+ }
125
+ }
126
+
127
+ // ── Step 2: Bayesian Average ─────────────────────────────
128
+
129
+ /**
130
+ * Bayesian average prevents new agents with 1 job at 100%
131
+ * from outranking established agents with 95% over 200 jobs.
132
+ *
133
+ * Formula: (count * score + C * prior) / (count + C)
134
+ *
135
+ * Examples:
136
+ * - 2 jobs at 100% → 66.7 (pulls toward prior)
137
+ * - 200 jobs at 95% → 93.3 (mostly own data)
138
+ * - 0 jobs → 50.0 (pure prior)
139
+ */
140
+ function bayesianQuality(normalized: NormalizedReputation): number {
141
+ const effectiveCount = normalized.confidence * 20; // Scale confidence to pseudo-count
142
+ return (effectiveCount * normalized.quality + BAYESIAN_C * GLOBAL_AVG_QUALITY) /
143
+ (effectiveCount + BAYESIAN_C);
144
+ }
145
+
146
+ // ── Step 3: Value Score ──────────────────────────────────
147
+
148
+ /**
149
+ * Value = quality relative to price.
150
+ * A $0.01 agent with 82% success is better value than $0.25 at 76%.
151
+ *
152
+ * Free services get a moderate value score (good value but unknown quality).
153
+ */
154
+ function valueScore(agent: UnifiedAgent, bayesianQ: number, allAgents: UnifiedAgent[]): number {
155
+ const price = parseAgentPrice(agent);
156
+
157
+ if (price === null) return 50; // Unknown price → neutral
158
+ if (price === 0) return Math.min(bayesianQ, 70); // Free → capped (free doesn't mean best)
159
+
160
+ // Find min price among agents with similar capabilities
161
+ const comparablePrices = allAgents
162
+ .map(parseAgentPrice)
163
+ .filter((p): p is number => p !== null && p > 0);
164
+
165
+ if (comparablePrices.length === 0) return 50;
166
+
167
+ const minPrice = Math.min(...comparablePrices);
168
+ const priceRatio = minPrice / price; // 1.0 = cheapest, <1 = more expensive
169
+
170
+ // Value = quality-adjusted price efficiency
171
+ // High quality + low price = high value
172
+ // Low quality + high price = low value
173
+ return Math.min(100, (bayesianQ * 0.6 + priceRatio * 100 * 0.4));
174
+ }
175
+
176
+ // ── Step 4: Geometric Mean Composite ─────────────────────
177
+
178
+ /**
179
+ * Geometric mean ensures ALL factors must be non-zero.
180
+ * An agent that's free but unreliable scores zero.
181
+ * An agent that's perfect quality but irrelevant also scores low.
182
+ */
183
+ function compositeScore(scores: Omit<RankingScores, "composite">): number {
184
+ const eps = 1; // Prevent zero from killing the product
185
+ const r = scores.relevance + eps;
186
+ const q = scores.quality + eps;
187
+ const v = scores.volume + eps;
188
+ const t = scores.recency + eps;
189
+ const p = scores.value + eps;
190
+
191
+ const totalWeight = WEIGHTS.relevance + WEIGHTS.quality + WEIGHTS.volume + WEIGHTS.recency + WEIGHTS.value;
192
+
193
+ return Math.pow(
194
+ Math.pow(r, WEIGHTS.relevance) *
195
+ Math.pow(q, WEIGHTS.quality) *
196
+ Math.pow(v, WEIGHTS.volume) *
197
+ Math.pow(t, WEIGHTS.recency) *
198
+ Math.pow(p, WEIGHTS.value),
199
+ 1 / totalWeight
200
+ );
201
+ }
202
+
203
+ // ── Step 5: Wilson Score (Confidence-Adjusted) ───────────
204
+
205
+ /**
206
+ * Lower bound of Wilson score confidence interval.
207
+ * Penalizes agents with few interactions even if success rate is high.
208
+ *
209
+ * Agent with 5/5 success → Wilson ~0.57
210
+ * Agent with 95/100 success → Wilson ~0.90
211
+ */
212
+ export function wilsonLowerBound(positive: number, total: number, z = 1.96): number {
213
+ if (total === 0) return 0;
214
+ const phat = positive / total;
215
+ const denominator = 1 + z * z / total;
216
+ const center = phat + z * z / (2 * total);
217
+ const spread = z * Math.sqrt((phat * (1 - phat) + z * z / (4 * total)) / total);
218
+ return Math.max(0, (center - spread) / denominator);
219
+ }
220
+
221
+ // ── Main Ranking Function ────────────────────────────────
222
+
223
+ /**
224
+ * Rank agents using the full pipeline:
225
+ * Normalize → Bayesian adjust → Value score → Geometric composite
226
+ */
227
+ export function rankAgentsV2(
228
+ agents: UnifiedAgent[],
229
+ query: string
230
+ ): RankedAgent[] {
231
+ // Calculate relevance for each agent (reuse existing BM25 + structured matching)
232
+ const queryTerms = query.toLowerCase().split(/\s+/).filter((t) => t.length > 2);
233
+
234
+ return agents
235
+ .map((agent) => {
236
+ // Relevance (BM25-lite + structured)
237
+ const relevance = calculateRelevance(agent, queryTerms);
238
+
239
+ // Normalize reputation per source
240
+ const normalized = normalizeReputation(agent);
241
+
242
+ // Bayesian-adjusted quality
243
+ const quality = bayesianQuality(normalized);
244
+
245
+ // Volume
246
+ const volume = normalized.volume;
247
+
248
+ // Recency
249
+ const recency = normalized.recency;
250
+
251
+ // Value for money
252
+ const value = valueScore(agent, quality, agents);
253
+
254
+ const scores: RankingScores = {
255
+ relevance,
256
+ quality,
257
+ volume,
258
+ recency,
259
+ value,
260
+ composite: 0,
261
+ };
262
+
263
+ scores.composite = compositeScore(scores);
264
+
265
+ // Build explanation
266
+ const explanation = buildExplanation(agent, scores, normalized);
267
+
268
+ return { agent, scores, explanation };
269
+ })
270
+ .sort((a, b) => b.scores.composite - a.scores.composite);
271
+ }
272
+
273
+ // ── Helpers ──────────────────────────────────────────────
274
+
275
+ function logNormalize(value: number, maxExpected: number): number {
276
+ if (value <= 0) return 0;
277
+ return Math.min(100, (Math.log(1 + value) / Math.log(1 + maxExpected)) * 100);
278
+ }
279
+
280
+ function recencyScore(lastSeen?: number): number {
281
+ if (!lastSeen) return 50; // Unknown → neutral
282
+ const hoursSince = (Date.now() - lastSeen) / (1000 * 60 * 60);
283
+ if (hoursSince < 1) return 100;
284
+ if (hoursSince < 24) return 80;
285
+ if (hoursSince < 168) return 60; // 1 week
286
+ if (hoursSince < 720) return 40; // 1 month
287
+ return 20;
288
+ }
289
+
290
+ function parseAgentPrice(agent: UnifiedAgent): number | null {
291
+ if (!agent.pricing?.amount) return null;
292
+ const amount = agent.pricing.amount;
293
+ if (typeof amount === "number") return amount;
294
+ const parsed = parseFloat(amount);
295
+ return isNaN(parsed) ? null : parsed;
296
+ }
297
+
298
+ function calculateRelevance(agent: UnifiedAgent, queryTerms: string[]): number {
299
+ const text = `${agent.name} ${agent.description} ${agent.categories.join(" ")} ${agent.capabilities.join(" ")}`.toLowerCase();
300
+ let score = 0;
301
+
302
+ for (const term of queryTerms) {
303
+ if (agent.name.toLowerCase().includes(term)) score += 15;
304
+ if (agent.description.toLowerCase().includes(term)) score += 8;
305
+ if (agent.categories.some((c) => c.includes(term))) score += 12;
306
+ if (agent.capabilities.some((c) => c.toLowerCase().includes(term))) score += 5;
307
+ }
308
+
309
+ return Math.min(100, score);
310
+ }
311
+
312
+ function buildExplanation(agent: UnifiedAgent, scores: RankingScores, normalized: NormalizedReputation): string {
313
+ const parts: string[] = [];
314
+
315
+ if (scores.relevance > 30) parts.push("Strong match");
316
+ else if (scores.relevance > 15) parts.push("Moderate match");
317
+ else parts.push("Weak match");
318
+
319
+ if (normalized.confidence > 0.5) {
320
+ if (scores.quality > 70) parts.push("proven quality");
321
+ else if (scores.quality > 50) parts.push("decent quality");
322
+ else parts.push("unproven quality");
323
+ } else {
324
+ parts.push("limited track record");
325
+ }
326
+
327
+ const price = parseAgentPrice(agent);
328
+ if (price === 0) parts.push("free");
329
+ else if (price !== null && price < 0.05) parts.push("very affordable");
330
+ else if (price !== null && price > 1) parts.push("premium priced");
331
+
332
+ if (scores.value > 70) parts.push("great value");
333
+ else if (scores.value < 30) parts.push("questionable value");
334
+
335
+ return parts.join(". ") + ".";
336
+ }
@@ -1,51 +1,55 @@
1
1
  /**
2
2
  * Agent Discovery Protocol (ADP) Source Adapter
3
- * agentdiscovery.io open protocol for autonomous agent commerce
3
+ * Uses the real ADP v2 API at agentdiscovery.io
4
4
  */
5
5
 
6
6
  import type { SourceAdapter, UnifiedAgent, SourceSearchOptions } from "./types.js";
7
7
 
8
- const ADP_BASE = "https://agentdiscovery.io";
8
+ const ADP_BASE = "https://agentdiscovery.io/api/adp/v2";
9
9
 
10
10
  export const adpAdapter: SourceAdapter = {
11
11
  name: "adp",
12
- displayName: "Agent Discovery Protocol",
12
+ displayName: "Agent Discovery Protocol (ADP v2)",
13
13
 
14
14
  async search(query: string, options?: SourceSearchOptions): Promise<UnifiedAgent[]> {
15
15
  try {
16
- // Try the ADP API
17
- const res = await fetch(`${ADP_BASE}/api/agents?q=${encodeURIComponent(query)}&limit=${options?.limit ?? 20}`, {
16
+ // First try discovery endpoint with intent
17
+ const discoverRes = await fetch(`${ADP_BASE}/discover`, {
18
+ method: "POST",
19
+ headers: { "Content-Type": "application/json" },
20
+ body: JSON.stringify({ intent: query, category: query }),
18
21
  signal: AbortSignal.timeout(options?.timeout ?? 5000),
19
22
  });
20
23
 
21
- if (!res.ok) return [];
24
+ if (discoverRes.ok) {
25
+ const data = await discoverRes.json();
26
+ const agents = data.matches ?? data.agents ?? data.results ?? [];
27
+ if (Array.isArray(agents) && agents.length > 0) {
28
+ return agents.slice(0, options?.limit ?? 20).map(mapAdpAgent);
29
+ }
30
+ }
22
31
 
23
- const data = await res.json();
24
- const agents = Array.isArray(data) ? data : data.agents ?? data.data ?? [];
32
+ // Fallback: list all agents
33
+ const listRes = await fetch(`${ADP_BASE}/agents`, {
34
+ signal: AbortSignal.timeout(options?.timeout ?? 5000),
35
+ });
36
+
37
+ if (!listRes.ok) return [];
25
38
 
26
- return agents.map((agent: any) => ({
27
- id: `adp:${agent.id ?? agent.address}`,
28
- nativeId: String(agent.id ?? agent.address),
29
- name: agent.name ?? "ADP Agent",
30
- description: agent.description ?? "",
31
- categories: agent.categories ?? agent.tags ?? ["general"],
32
- capabilities: agent.capabilities ?? [agent.description ?? ""],
33
- source: "adp" as const,
34
- protocol: (agent.protocol ?? "http") as any,
35
- endpoints: {
36
- http: agent.endpoint ?? agent.url,
37
- a2a: agent.a2aEndpoint,
38
- x402: agent.x402Endpoint,
39
- },
40
- reputation: agent.reputation ? {
41
- score: agent.reputation.score ?? 0,
42
- count: agent.reputation.count ?? 0,
43
- source: "adp",
44
- } : undefined,
45
- pricing: agent.pricing,
46
- verified: agent.verified ?? false,
47
- raw: agent,
48
- }));
39
+ const data = await listRes.json();
40
+ const agents = data.agents ?? [];
41
+ if (!Array.isArray(agents)) return [];
42
+
43
+ // Client-side filter by query
44
+ const lower = query.toLowerCase();
45
+ return agents
46
+ .filter((a: any) => {
47
+ if (!query) return true;
48
+ const text = `${a.name ?? ""} ${a.role ?? ""} ${(a.categories ?? []).join(" ")} ${(a.capabilities ?? []).map((c: any) => c.description ?? c.key ?? "").join(" ")}`.toLowerCase();
49
+ return lower.split(/\s+/).some((t) => text.includes(t));
50
+ })
51
+ .slice(0, options?.limit ?? 20)
52
+ .map(mapAdpAgent);
49
53
  } catch {
50
54
  return [];
51
55
  }
@@ -53,12 +57,30 @@ export const adpAdapter: SourceAdapter = {
53
57
 
54
58
  async ping(): Promise<boolean> {
55
59
  try {
56
- const res = await fetch(`${ADP_BASE}/api/health`, {
57
- signal: AbortSignal.timeout(3000),
58
- });
60
+ const res = await fetch(`${ADP_BASE}/agents`, { signal: AbortSignal.timeout(3000) });
59
61
  return res.ok;
60
62
  } catch {
61
63
  return false;
62
64
  }
63
65
  },
64
66
  };
67
+
68
+ function mapAdpAgent(agent: any): UnifiedAgent {
69
+ return {
70
+ id: `adp:${agent.did ?? agent.id ?? agent.name}`,
71
+ nativeId: agent.did ?? agent.id ?? "",
72
+ name: agent.name ?? "ADP Agent",
73
+ description: (agent.capabilities ?? []).map((c: any) => c.description ?? c.key ?? "").join(". ") || (agent.role ?? ""),
74
+ categories: agent.categories ?? [agent.role ?? "general"],
75
+ capabilities: (agent.capabilities ?? []).map((c: any) => `${c.key}: ${c.description ?? ""}`),
76
+ source: "adp",
77
+ protocol: "a2a",
78
+ endpoints: {
79
+ http: agent.endpoint ?? agent.url,
80
+ a2a: agent.a2a_endpoint,
81
+ },
82
+ pricing: agent.pricing,
83
+ verified: !!agent.did,
84
+ raw: agent,
85
+ };
86
+ }
@@ -1,107 +1,47 @@
1
1
  /**
2
- * ERC-8004 On-Chain Source Adapter
3
- * Indexes agent identities from the ERC-8004 Identity Registry on Base
2
+ * ERC-8004 Source Adapter
3
+ * Searches 8004scan.io API for registered AI agents
4
+ * Real data — agent identity, reputation, supported protocols
4
5
  */
5
6
 
6
- import { createPublicClient, http, type Address, parseAbiItem } from "viem";
7
- import { base, baseSepolia } from "viem/chains";
8
7
  import type { SourceAdapter, UnifiedAgent, SourceSearchOptions } from "./types.js";
9
8
 
10
- // ERC-8004 Identity Registry addresses
11
- const REGISTRY_ADDRESSES: Record<string, Address> = {
12
- "base": "0x8004A818BFB912233c491871b3d84c89A494BD9e" as Address,
13
- "base-sepolia": "0x8004A818BFB912233c491871b3d84c89A494BD9e" as Address,
14
- };
15
-
16
- const TRANSFER_EVENT = parseAbiItem(
17
- "event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)"
18
- );
19
-
20
- const TOKEN_URI_ABI = [
21
- {
22
- type: "function",
23
- name: "tokenURI",
24
- inputs: [{ name: "tokenId", type: "uint256" }],
25
- outputs: [{ name: "", type: "string" }],
26
- stateMutability: "view",
27
- },
28
- ] as const;
29
-
30
- export function createErc8004Adapter(network: "base" | "base-sepolia" = "base-sepolia"): SourceAdapter {
31
- const chain = network === "base" ? base : baseSepolia;
32
- const registryAddress = REGISTRY_ADDRESSES[network];
33
-
34
- const client = createPublicClient({
35
- chain,
36
- transport: http(),
37
- });
9
+ const SCAN_API = "https://www.8004scan.io/api/v1";
38
10
 
11
+ export function createErc8004Adapter(network: "base" | "base-sepolia" = "base"): SourceAdapter {
39
12
  return {
40
13
  name: "erc8004",
41
- displayName: "ERC-8004 Identity Registry",
14
+ displayName: "ERC-8004 Identity Registry (8004scan)",
42
15
 
43
16
  async search(query: string, options?: SourceSearchOptions): Promise<UnifiedAgent[]> {
44
17
  const limit = options?.limit ?? 20;
18
+ const timeout = options?.timeout ?? 10000;
45
19
 
46
20
  try {
47
- // Get recent Transfer events (minting = from 0x0)
48
- const logs = await client.getLogs({
49
- address: registryAddress,
50
- event: TRANSFER_EVENT,
51
- args: { from: "0x0000000000000000000000000000000000000000" as Address },
52
- fromBlock: "earliest",
53
- toBlock: "latest",
54
- });
55
-
56
- const agents: UnifiedAgent[] = [];
57
-
58
- // Process most recent first
59
- const recentLogs = logs.slice(-limit * 2).reverse();
21
+ // Search across all chains on 8004scan
22
+ const url = new URL(`${SCAN_API}/agents`);
23
+ if (query) url.searchParams.set("search", query);
24
+ url.searchParams.set("limit", String(limit));
60
25
 
61
- for (const log of recentLogs) {
62
- if (agents.length >= limit) break;
63
-
64
- const tokenId = log.args.tokenId!;
65
- const owner = log.args.to!;
66
-
67
- try {
68
- // Fetch tokenURI for agent metadata
69
- const uri = await client.readContract({
70
- address: registryAddress,
71
- abi: TOKEN_URI_ABI,
72
- functionName: "tokenURI",
73
- args: [tokenId],
74
- });
75
-
76
- // Resolve URI to JSON
77
- let metadata: any = {};
78
- if (uri.startsWith("http") || uri.startsWith("ipfs")) {
79
- const fetchUrl = uri.startsWith("ipfs://")
80
- ? `https://ipfs.io/ipfs/${uri.slice(7)}`
81
- : uri;
82
- const res = await fetch(fetchUrl, { signal: AbortSignal.timeout(3000) });
83
- if (res.ok) metadata = await res.json();
84
- } else if (uri.startsWith("data:")) {
85
- const json = uri.split(",")[1];
86
- metadata = JSON.parse(atob(json));
87
- }
26
+ const res = await fetch(url.toString(), {
27
+ signal: AbortSignal.timeout(timeout),
28
+ });
88
29
 
89
- const agent = mapErc8004Agent(tokenId, owner, metadata, network);
30
+ if (!res.ok) return [];
90
31
 
91
- // Filter by query
92
- if (query) {
93
- const searchText = `${agent.name} ${agent.description} ${agent.categories.join(" ")}`.toLowerCase();
94
- if (!searchText.includes(query.toLowerCase())) continue;
95
- }
32
+ const data = await res.json();
33
+ const agents = data.items ?? data.agents ?? data.data ?? [];
96
34
 
97
- agents.push(agent);
98
- } catch {
99
- // Skip agents with unresolvable metadata
100
- continue;
101
- }
102
- }
35
+ if (!Array.isArray(agents)) return [];
103
36
 
104
- return agents;
37
+ return agents
38
+ .filter((a: any) => {
39
+ if (!query) return true;
40
+ const text = `${a.name ?? ""} ${a.description ?? ""}`.toLowerCase();
41
+ return query.toLowerCase().split(/\s+/).some((t) => text.includes(t));
42
+ })
43
+ .slice(0, limit)
44
+ .map(mapErc8004Agent);
105
45
  } catch {
106
46
  return [];
107
47
  }
@@ -109,8 +49,10 @@ export function createErc8004Adapter(network: "base" | "base-sepolia" = "base-se
109
49
 
110
50
  async ping(): Promise<boolean> {
111
51
  try {
112
- await client.getBlockNumber();
113
- return true;
52
+ const res = await fetch(`${SCAN_API}/agents?limit=1`, {
53
+ signal: AbortSignal.timeout(5000),
54
+ });
55
+ return res.ok;
114
56
  } catch {
115
57
  return false;
116
58
  }
@@ -118,49 +60,46 @@ export function createErc8004Adapter(network: "base" | "base-sepolia" = "base-se
118
60
  };
119
61
  }
120
62
 
121
- function mapErc8004Agent(
122
- tokenId: bigint,
123
- owner: Address,
124
- metadata: any,
125
- network: string
126
- ): UnifiedAgent {
127
- const services = metadata.services ?? [];
128
- const endpoints: UnifiedAgent["endpoints"] = {};
63
+ function mapErc8004Agent(agent: any): UnifiedAgent {
64
+ const protocols = agent.supported_protocols ?? [];
65
+ const protocol = protocols.includes("A2A") ? "a2a" as const
66
+ : protocols.includes("MCP") ? "mcp" as const
67
+ : protocols.includes("x402") || agent.x402_supported ? "x402" as const
68
+ : "onchain" as const;
129
69
 
130
- for (const svc of services) {
131
- if (svc.protocol === "mcp") endpoints.mcp = svc.endpoint;
132
- if (svc.protocol === "a2a") endpoints.a2a = svc.endpoint;
133
- if (svc.protocol === "x402") endpoints.x402 = svc.endpoint;
134
- if (svc.protocol === "http") endpoints.http = svc.endpoint;
135
- }
70
+ const endpoints: UnifiedAgent["endpoints"] = {};
71
+ if (protocols.includes("MCP")) endpoints.mcp = agent.mcp_endpoint;
72
+ if (protocols.includes("A2A")) endpoints.a2a = agent.a2a_endpoint;
73
+ if (agent.x402_supported) endpoints.x402 = agent.x402_endpoint;
136
74
 
137
75
  return {
138
- id: `erc8004:${network}:${tokenId}`,
139
- nativeId: tokenId.toString(),
140
- name: metadata.name ?? `Agent #${tokenId}`,
141
- description: metadata.description ?? "",
142
- categories: metadata.categories ?? extractFromDescription(metadata.description ?? ""),
143
- capabilities: services.map((s: any) => `${s.protocol}: ${s.endpoint}`),
76
+ id: `erc8004:${agent.agent_id ?? agent.token_id}`,
77
+ nativeId: agent.token_id?.toString() ?? agent.agent_id ?? "",
78
+ name: agent.name ?? `Agent #${agent.token_id}`,
79
+ ensName: agent.owner_ens ?? undefined,
80
+ description: agent.description ?? "",
81
+ categories: extractCategories(agent.name ?? "", agent.description ?? ""),
82
+ capabilities: protocols.map((p: string) => `Supports ${p}`),
144
83
  source: "erc8004",
145
- protocol: endpoints.a2a ? "a2a" : endpoints.mcp ? "mcp" : endpoints.x402 ? "x402" : "onchain",
84
+ protocol,
146
85
  endpoints,
147
86
  reputation: {
148
- score: 0,
149
- count: 0,
150
- source: "erc8004",
87
+ score: agent.total_score ?? 0,
88
+ count: agent.star_count ?? 0,
89
+ source: "8004scan",
151
90
  },
152
- verified: true,
153
- lastSeen: Date.now(),
154
- raw: { tokenId: tokenId.toString(), owner, metadata, network },
91
+ verified: agent.is_verified ?? false,
92
+ raw: agent,
155
93
  };
156
94
  }
157
95
 
158
- function extractFromDescription(text: string): string[] {
159
- const lower = text.toLowerCase();
96
+ function extractCategories(name: string, description: string): string[] {
97
+ const text = `${name} ${description}`.toLowerCase();
160
98
  const cats: string[] = [];
161
- if (lower.match(/defi|yield|swap|pool/)) cats.push("defi");
162
- if (lower.match(/data|analyt/)) cats.push("data");
163
- if (lower.match(/trade|trading/)) cats.push("trading");
164
- if (lower.match(/research/)) cats.push("research");
99
+ if (text.match(/defi|yield|swap|pool|trading/)) cats.push("defi");
100
+ if (text.match(/data|analyt|research/)) cats.push("data");
101
+ if (text.match(/code|develop|build/)) cats.push("development");
102
+ if (text.match(/ai|model|inference/)) cats.push("ai");
103
+ if (text.match(/security|audit/)) cats.push("security");
165
104
  return cats.length > 0 ? cats : ["general"];
166
105
  }