-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathexplore-item.js
More file actions
174 lines (153 loc) · 5.47 KB
/
Copy pathexplore-item.js
File metadata and controls
174 lines (153 loc) · 5.47 KB
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
/**
* GET /api/explore-item?kind=onchain&chain=<id>&id=<agentId>
* GET /api/explore-item?kind=avatar&id=<avatarId>
* GET /api/explore-item?kind=solana&id=<asset_pubkey>
*
* Returns a single item in the same shape as the /api/explore feed items.
*/
import { sql } from './_lib/db.js';
import { cors, json, method, wrap, error, rateLimited } from './_lib/http.js';
import { limits, clientIp } from './_lib/rate-limit.js';
import { CHAIN_BY_ID, tokenExplorerUrl, addressExplorerUrl } from './_lib/erc8004-chains.js';
import { publicUrl } from './_lib/r2.js';
export default wrap(async (req, res) => {
if (cors(req, res, { methods: 'GET,OPTIONS', origins: '*' })) return;
if (!method(req, res, ['GET'])) return;
const rl = await limits.publicIp(clientIp(req));
if (!rl.success) return rateLimited(res, rl);
const url = new URL(req.url, 'http://x');
const kind = url.searchParams.get('kind');
const id = url.searchParams.get('id');
if (!kind || !id) return error(res, 400, 'validation_error', 'kind and id are required');
if (kind === 'onchain') {
const chainId = parseInt(url.searchParams.get('chain') || '', 10);
if (!Number.isFinite(chainId)) return error(res, 400, 'validation_error', 'chain is required for onchain items');
const rows = await sql`
SELECT chain_id, agent_id, owner, name, description, image, glb_url,
has_3d, x402_support, registered_at, registered_tx,
services, agent_uri
FROM erc8004_agents_index
WHERE active = true
AND chain_id = ${chainId}
AND agent_id = ${parseInt(id, 10)}
LIMIT 1
`;
if (!rows.length) return error(res, 404, 'not_found', 'agent not found');
const r = rows[0];
const chain = CHAIN_BY_ID[r.chain_id];
const item = {
kind: 'onchain',
chainId: r.chain_id,
chainName: chain?.name || `Chain ${r.chain_id}`,
explorerBase: chain?.explorer || null,
agentId: r.agent_id,
owner: r.owner,
ownerShort: shortAddr(r.owner),
name: r.name || `Agent #${r.agent_id}`,
description: r.description || '',
image: r.image || null,
glbUrl: r.glb_url || null,
has3d: r.has_3d,
x402Support: r.x402_support,
registeredAt: r.registered_at,
registeredTx: r.registered_tx || null,
tokenExplorerUrl: tokenExplorerUrl(r.chain_id, r.agent_id),
ownerExplorerUrl: addressExplorerUrl(r.chain_id, r.owner),
viewerUrl: r.glb_url ? `/app#model=${encodeURIComponent(r.glb_url)}` : null,
services: (r.services || []).map((s) => ({
name: s?.name || null,
endpoint: s?.endpoint || null,
version: s?.version || null,
})),
};
return json(res, 200, { item });
}
if (kind === 'avatar') {
const rows = await sql`
SELECT a.id, a.slug, a.name, a.description, a.storage_key, a.thumbnail_key,
a.tags, a.created_at, a.source,
coalesce(a.featured, false) AS featured,
coalesce(a.view_count, 0) AS view_count,
u.username AS owner_username,
u.display_name AS owner_display_name,
u.wallet_address AS owner_wallet
FROM avatars a
LEFT JOIN users u ON u.id = a.owner_id AND u.deleted_at IS NULL
WHERE a.deleted_at IS NULL
AND a.visibility = 'public'
AND a.id = ${id}
LIMIT 1
`;
if (!rows.length) return error(res, 404, 'not_found', 'avatar not found');
const r = rows[0];
const glb = publicUrl(r.storage_key);
const handle = r.owner_username
? `@${r.owner_username}`
: r.owner_wallet
? shortAddr(r.owner_wallet)
: null;
const item = {
kind: 'avatar',
avatarId: r.id,
slug: r.slug,
name: r.name,
description: r.description || '',
image: r.thumbnail_key ? publicUrl(r.thumbnail_key) : null,
glbUrl: glb,
has3d: true,
tags: r.tags || [],
source: r.source || null,
featured: r.featured === true || r.featured === 't',
viewCount: Number(r.view_count) || 0,
createdAt: r.created_at,
viewerUrl: `/app#model=${encodeURIComponent(glb)}`,
author: handle
? {
handle,
displayName: r.owner_display_name || r.owner_username || handle,
profileUrl: r.owner_username ? `/u/${r.owner_username}` : null,
}
: null,
};
return json(res, 200, { item });
}
if (kind === 'solana') {
const rows = await sql`
SELECT ai.id, ai.name, ai.description, ai.wallet_address, ai.skills,
ai.meta, ai.created_at,
a.thumbnail_key AS avatar_thumb
FROM agent_identities ai
LEFT JOIN avatars a ON a.id = ai.avatar_id AND a.deleted_at IS NULL
WHERE ai.deleted_at IS NULL
AND ai.meta->>'chain_type' = 'solana'
AND ai.meta->>'sol_mint_address' = ${id}
LIMIT 1
`;
if (!rows.length) return error(res, 404, 'not_found', 'solana agent not found');
const r = rows[0];
const asset = r.meta?.sol_mint_address;
const network = r.meta?.network || 'mainnet';
const thumb = r.avatar_thumb ? publicUrl(r.avatar_thumb) : null;
const item = {
kind: 'solana',
asset,
name: r.name || 'Solana Agent',
description: r.description || '',
image: thumb,
has3d: !!r.avatar_thumb,
skills: r.skills || [],
owner: r.wallet_address,
ownerShort: shortAddr(r.wallet_address),
createdAt: r.created_at,
network,
explorerUrl: asset ? `https://solscan.io/token/${asset}` : null,
ownerExplorerUrl: r.wallet_address ? `https://solscan.io/account/${r.wallet_address}` : null,
};
return json(res, 200, { item });
}
return error(res, 400, 'validation_error', 'kind must be onchain, avatar, or solana');
});
function shortAddr(a) {
if (!a || a.length < 10) return a || '';
return `${a.slice(0, 6)}…${a.slice(-4)}`;
}