Spaces:
Running
Running
refactor(api): scope dedup per route to surface JS-only same-name apps
Browse filesMove the name-based dedup out of `fetchAppsFromHF` so each route can
apply its own policy on top of a single raw cache:
- `/api/apps` (showcase site) keeps the original global dedup, so
Python forks of `reachy_mini_conversation_app` etc. still collapse
to a single tile.
- `/api/js-apps` (mobile shell) now dedups within the JS subset only,
so a JS Space like `tfrere/emotions` no longer loses a name fight
against an unrelated Python Space (`RemiFabre/emotions`) and
surfaces correctly in the store.
`getApps()` is renamed to `getRawApps()` to make the cache contract
explicit, and `dedupAppsByName` is shared between `dedupGlobalApps`
and `dedupJsApps` so the policies can diverge later without churn.
Co-authored-by: Cursor <cursoragent@cursor.com>
- server/index.js +91 -50
server/index.js
CHANGED
|
@@ -197,53 +197,86 @@ async function fetchAppsFromHF() {
|
|
| 197 |
};
|
| 198 |
});
|
| 199 |
|
| 200 |
-
|
| 201 |
-
// named "reachy_mini_conversation_app" from different authors).
|
| 202 |
-
// Priority: 1) official, 2) oldest (original), 3) most likes as tiebreaker.
|
| 203 |
-
const deduped = new Map();
|
| 204 |
-
for (const app of allApps) {
|
| 205 |
-
const key = app.name.toLowerCase();
|
| 206 |
-
const existing = deduped.get(key);
|
| 207 |
-
if (!existing) {
|
| 208 |
-
deduped.set(key, app);
|
| 209 |
-
continue;
|
| 210 |
-
}
|
| 211 |
-
// Official always wins
|
| 212 |
-
if (app.isOfficial && !existing.isOfficial) {
|
| 213 |
-
deduped.set(key, app);
|
| 214 |
-
continue;
|
| 215 |
-
}
|
| 216 |
-
if (existing.isOfficial) continue;
|
| 217 |
-
// Oldest wins (the original is created before its forks)
|
| 218 |
-
const appDate = app.extra.createdAt ? new Date(app.extra.createdAt).getTime() : Infinity;
|
| 219 |
-
const existingDate = existing.extra.createdAt ? new Date(existing.extra.createdAt).getTime() : Infinity;
|
| 220 |
-
if (appDate < existingDate) {
|
| 221 |
-
deduped.set(key, app);
|
| 222 |
-
} else if (appDate === existingDate && (app.extra.likes || 0) > (existing.extra.likes || 0)) {
|
| 223 |
-
deduped.set(key, app);
|
| 224 |
-
}
|
| 225 |
-
}
|
| 226 |
-
const uniqueApps = [...deduped.values()];
|
| 227 |
-
|
| 228 |
-
console.log(`[Cache] Deduplicated ${allApps.length} → ${uniqueApps.length} apps (removed ${allApps.length - uniqueApps.length} forks with duplicate names)`);
|
| 229 |
|
| 230 |
-
// Sort: official first, then by likes
|
| 231 |
-
|
|
|
|
| 232 |
if (a.isOfficial !== b.isOfficial) {
|
| 233 |
return a.isOfficial ? -1 : 1;
|
| 234 |
}
|
| 235 |
return (b.extra.likes || 0) - (a.extra.likes || 0);
|
| 236 |
});
|
| 237 |
|
| 238 |
-
return
|
| 239 |
} catch (err) {
|
| 240 |
console.error('[Cache] Error fetching apps:', err);
|
| 241 |
throw err;
|
| 242 |
}
|
| 243 |
}
|
| 244 |
|
| 245 |
-
/
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
const now = Date.now();
|
| 248 |
|
| 249 |
// Return cache if valid
|
|
@@ -265,7 +298,7 @@ async function getApps() {
|
|
| 265 |
const apps = await fetchAppsFromHF();
|
| 266 |
appsCache.data = apps;
|
| 267 |
appsCache.lastFetch = now;
|
| 268 |
-
console.log(`[Cache] Cache updated with ${apps.length}
|
| 269 |
return apps;
|
| 270 |
} catch (err) {
|
| 271 |
// On error, return stale cache if available
|
|
@@ -282,7 +315,8 @@ async function getApps() {
|
|
| 282 |
// API endpoint
|
| 283 |
app.get('/api/apps', async (req, res) => {
|
| 284 |
try {
|
| 285 |
-
const
|
|
|
|
| 286 |
res.json({
|
| 287 |
apps,
|
| 288 |
cached: true,
|
|
@@ -299,9 +333,12 @@ app.get('/api/apps', async (req, res) => {
|
|
| 299 |
// JS apps + LLM-inferred categories
|
| 300 |
// =====================================================================
|
| 301 |
//
|
| 302 |
-
// `/api/js-apps` is a curated view on
|
| 303 |
// 1. Filter on the `reachy_mini_js_app` tag (the mobile-embeddable subset).
|
| 304 |
-
// 2.
|
|
|
|
|
|
|
|
|
|
| 305 |
// sourced from a persistent dataset cache (see categoryCache.js).
|
| 306 |
//
|
| 307 |
// Categories are inferred lazily by an LLM from each Space's
|
|
@@ -310,18 +347,20 @@ app.get('/api/apps', async (req, res) => {
|
|
| 310 |
// flight; subsequent requests pick them up as the cache fills.
|
| 311 |
|
| 312 |
/**
|
| 313 |
-
* Pull the JS-app subset out of the
|
| 314 |
-
* in cached categories. Pure,
|
| 315 |
-
* call is to
|
|
|
|
| 316 |
*/
|
| 317 |
async function getJsApps() {
|
| 318 |
-
const
|
| 319 |
-
const jsApps =
|
| 320 |
const tags = a?.extra?.tags;
|
| 321 |
return Array.isArray(tags) && tags.includes(JS_APP_TAG);
|
| 322 |
});
|
|
|
|
| 323 |
|
| 324 |
-
return
|
| 325 |
const cached = categoryCache.get(app.id);
|
| 326 |
return {
|
| 327 |
...app,
|
|
@@ -540,7 +579,7 @@ app.get('/api/health', (req, res) => {
|
|
| 540 |
app.post('/api/refresh', async (req, res) => {
|
| 541 |
try {
|
| 542 |
appsCache.lastFetch = null; // Invalidate cache
|
| 543 |
-
const apps = await
|
| 544 |
res.json({ success: true, count: apps.length });
|
| 545 |
} catch (err) {
|
| 546 |
res.status(500).json({ error: 'Failed to refresh cache' });
|
|
@@ -562,7 +601,7 @@ app.get('*', (req, res) => {
|
|
| 562 |
async function warmCache() {
|
| 563 |
console.log('[Startup] Pre-warming cache...');
|
| 564 |
try {
|
| 565 |
-
const apps = await
|
| 566 |
console.log('[Startup] Cache warmed successfully');
|
| 567 |
|
| 568 |
// Categorization warm-up: fire the JS-app batch in the
|
|
@@ -573,10 +612,12 @@ async function warmCache() {
|
|
| 573 |
void (async () => {
|
| 574 |
try {
|
| 575 |
await categoryCache.load();
|
| 576 |
-
const jsApps =
|
| 577 |
-
|
| 578 |
-
|
| 579 |
-
|
|
|
|
|
|
|
| 580 |
console.log(
|
| 581 |
`[Startup] Found ${jsApps.length} JS apps; checking categories...`,
|
| 582 |
);
|
|
|
|
| 197 |
};
|
| 198 |
});
|
| 199 |
|
| 200 |
+
console.log(`[Cache] Built ${allApps.length} raw app entries from HF.`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
+
// Sort: official first, then by likes. Dedup is route-specific
|
| 203 |
+
// and applied downstream (see `dedupGlobalApps` and `dedupJsApps`).
|
| 204 |
+
allApps.sort((a, b) => {
|
| 205 |
if (a.isOfficial !== b.isOfficial) {
|
| 206 |
return a.isOfficial ? -1 : 1;
|
| 207 |
}
|
| 208 |
return (b.extra.likes || 0) - (a.extra.likes || 0);
|
| 209 |
});
|
| 210 |
|
| 211 |
+
return allApps;
|
| 212 |
} catch (err) {
|
| 213 |
console.error('[Cache] Error fetching apps:', err);
|
| 214 |
throw err;
|
| 215 |
}
|
| 216 |
}
|
| 217 |
|
| 218 |
+
/**
|
| 219 |
+
* Pick a winner among Spaces sharing the same repo name. Forks
|
| 220 |
+
* keep the upstream name (e.g. several `reachy_mini_conversation_app`
|
| 221 |
+
* from different authors); we surface only one in the store to
|
| 222 |
+
* avoid drowning the original under a dozen near-identical tiles.
|
| 223 |
+
*
|
| 224 |
+
* Priority: 1) official, 2) oldest (likely original), 3) most likes
|
| 225 |
+
* as tiebreaker.
|
| 226 |
+
*/
|
| 227 |
+
function dedupAppsByName(apps) {
|
| 228 |
+
const deduped = new Map();
|
| 229 |
+
for (const app of apps) {
|
| 230 |
+
const key = app.name.toLowerCase();
|
| 231 |
+
const existing = deduped.get(key);
|
| 232 |
+
if (!existing) {
|
| 233 |
+
deduped.set(key, app);
|
| 234 |
+
continue;
|
| 235 |
+
}
|
| 236 |
+
if (app.isOfficial && !existing.isOfficial) {
|
| 237 |
+
deduped.set(key, app);
|
| 238 |
+
continue;
|
| 239 |
+
}
|
| 240 |
+
if (existing.isOfficial) continue;
|
| 241 |
+
const appDate = app.extra?.createdAt ? new Date(app.extra.createdAt).getTime() : Infinity;
|
| 242 |
+
const existingDate = existing.extra?.createdAt ? new Date(existing.extra.createdAt).getTime() : Infinity;
|
| 243 |
+
if (appDate < existingDate) {
|
| 244 |
+
deduped.set(key, app);
|
| 245 |
+
} else if (appDate === existingDate && (app.extra?.likes || 0) > (existing.extra?.likes || 0)) {
|
| 246 |
+
deduped.set(key, app);
|
| 247 |
+
}
|
| 248 |
+
}
|
| 249 |
+
return [...deduped.values()];
|
| 250 |
+
}
|
| 251 |
+
|
| 252 |
+
/**
|
| 253 |
+
* Dedup applied to the full `/api/apps` payload (Python + JS + others
|
| 254 |
+
* mixed). Same-name collisions across SDKs collapse here too, by design:
|
| 255 |
+
* the showcase site favours a clean catalog over completeness, and
|
| 256 |
+
* SDK-aware variants of the same idea live as separate Spaces only
|
| 257 |
+
* by accident in practice.
|
| 258 |
+
*/
|
| 259 |
+
function dedupGlobalApps(apps) {
|
| 260 |
+
return dedupAppsByName(apps);
|
| 261 |
+
}
|
| 262 |
+
|
| 263 |
+
/**
|
| 264 |
+
* Dedup applied to the `/api/js-apps` route only. We restrict the
|
| 265 |
+
* comparison to entries already filtered to the JS subset, so a JS
|
| 266 |
+
* Space (e.g. `tfrere/emotions`) does not lose a name fight against
|
| 267 |
+
* an unrelated Python Space sharing the same repo name (e.g.
|
| 268 |
+
* `RemiFabre/emotions`). The mobile shell only sees JS apps anyway,
|
| 269 |
+
* so confining dedup to that scope is what matches the user model.
|
| 270 |
+
*/
|
| 271 |
+
function dedupJsApps(jsApps) {
|
| 272 |
+
return dedupAppsByName(jsApps);
|
| 273 |
+
}
|
| 274 |
+
|
| 275 |
+
// Get raw apps with caching. Dedup is NOT applied here - each
|
| 276 |
+
// route owns its own dedup policy (see `dedupGlobalApps` and
|
| 277 |
+
// `dedupJsApps`) so they can disagree without paying for two
|
| 278 |
+
// upstream fetches.
|
| 279 |
+
async function getRawApps() {
|
| 280 |
const now = Date.now();
|
| 281 |
|
| 282 |
// Return cache if valid
|
|
|
|
| 298 |
const apps = await fetchAppsFromHF();
|
| 299 |
appsCache.data = apps;
|
| 300 |
appsCache.lastFetch = now;
|
| 301 |
+
console.log(`[Cache] Cache updated with ${apps.length} raw entries`);
|
| 302 |
return apps;
|
| 303 |
} catch (err) {
|
| 304 |
// On error, return stale cache if available
|
|
|
|
| 315 |
// API endpoint
|
| 316 |
app.get('/api/apps', async (req, res) => {
|
| 317 |
try {
|
| 318 |
+
const raw = await getRawApps();
|
| 319 |
+
const apps = dedupGlobalApps(raw);
|
| 320 |
res.json({
|
| 321 |
apps,
|
| 322 |
cached: true,
|
|
|
|
| 333 |
// JS apps + LLM-inferred categories
|
| 334 |
// =====================================================================
|
| 335 |
//
|
| 336 |
+
// `/api/js-apps` is a curated view on the JS-only subset:
|
| 337 |
// 1. Filter on the `reachy_mini_js_app` tag (the mobile-embeddable subset).
|
| 338 |
+
// 2. Dedup name collisions among JS apps only (`dedupJsApps`),
|
| 339 |
+
// so a JS app does not get knocked out by a same-named Python
|
| 340 |
+
// Space surfaced through `/api/apps`.
|
| 341 |
+
// 3. Enrich each entry with `categories` + `categories_source`,
|
| 342 |
// sourced from a persistent dataset cache (see categoryCache.js).
|
| 343 |
//
|
| 344 |
// Categories are inferred lazily by an LLM from each Space's
|
|
|
|
| 347 |
// flight; subsequent requests pick them up as the cache fills.
|
| 348 |
|
| 349 |
/**
|
| 350 |
+
* Pull the JS-app subset out of the raw apps cache, dedup it
|
| 351 |
+
* within the JS scope, and fold in cached categories. Pure,
|
| 352 |
+
* synchronous-ish (the only async call is to `getRawApps()` which
|
| 353 |
+
* has its own cache).
|
| 354 |
*/
|
| 355 |
async function getJsApps() {
|
| 356 |
+
const raw = await getRawApps();
|
| 357 |
+
const jsApps = raw.filter((a) => {
|
| 358 |
const tags = a?.extra?.tags;
|
| 359 |
return Array.isArray(tags) && tags.includes(JS_APP_TAG);
|
| 360 |
});
|
| 361 |
+
const deduped = dedupJsApps(jsApps);
|
| 362 |
|
| 363 |
+
return deduped.map((app) => {
|
| 364 |
const cached = categoryCache.get(app.id);
|
| 365 |
return {
|
| 366 |
...app,
|
|
|
|
| 579 |
app.post('/api/refresh', async (req, res) => {
|
| 580 |
try {
|
| 581 |
appsCache.lastFetch = null; // Invalidate cache
|
| 582 |
+
const apps = await getRawApps();
|
| 583 |
res.json({ success: true, count: apps.length });
|
| 584 |
} catch (err) {
|
| 585 |
res.status(500).json({ error: 'Failed to refresh cache' });
|
|
|
|
| 601 |
async function warmCache() {
|
| 602 |
console.log('[Startup] Pre-warming cache...');
|
| 603 |
try {
|
| 604 |
+
const apps = await getRawApps();
|
| 605 |
console.log('[Startup] Cache warmed successfully');
|
| 606 |
|
| 607 |
// Categorization warm-up: fire the JS-app batch in the
|
|
|
|
| 612 |
void (async () => {
|
| 613 |
try {
|
| 614 |
await categoryCache.load();
|
| 615 |
+
const jsApps = dedupJsApps(
|
| 616 |
+
apps.filter((a) => {
|
| 617 |
+
const tags = a?.extra?.tags;
|
| 618 |
+
return Array.isArray(tags) && tags.includes(JS_APP_TAG);
|
| 619 |
+
}),
|
| 620 |
+
);
|
| 621 |
console.log(
|
| 622 |
`[Startup] Found ${jsApps.length} JS apps; checking categories...`,
|
| 623 |
);
|