Spaces:
Paused
Paused
File size: 2,538 Bytes
786115c 7d34920 786115c a4c3fca db943f9 10dbbd6 08e8583 25a7ba5 786115c 69684cd 786115c 25a7ba5 db943f9 10dbbd6 a4c3fca db943f9 08e8583 db943f9 08e8583 786115c 84d0edd 7d34920 25a7ba5 881070b 7d34920 10dbbd6 7d34920 84d0edd 25a7ba5 786115c 25a7ba5 a4c3fca 25a7ba5 786115c 25a7ba5 2128ce0 25a7ba5 10dbbd6 a4c3fca 25a7ba5 786115c |
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 |
import { base } from "$app/paths";
import { ENABLE_ASSISTANTS, REQUIRE_FEATURED_ASSISTANTS } from "$env/static/private";
import { collections } from "$lib/server/database.js";
import { SortKey, type Assistant } from "$lib/types/Assistant";
import type { User } from "$lib/types/User";
import { generateQueryTokens } from "$lib/utils/searchTokens.js";
import { error, redirect } from "@sveltejs/kit";
import type { Filter } from "mongodb";
const NUM_PER_PAGE = 24;
export const load = async ({ url, locals }) => {
if (!ENABLE_ASSISTANTS) {
throw redirect(302, `${base}/`);
}
const modelId = url.searchParams.get("modelId");
const pageIndex = parseInt(url.searchParams.get("p") ?? "0");
const username = url.searchParams.get("user");
const query = url.searchParams.get("q")?.trim() ?? null;
const sort = url.searchParams.get("sort")?.trim() ?? SortKey.POPULAR;
const createdByCurrentUser = locals.user?.username && locals.user.username === username;
let user: Pick<User, "_id"> | null = null;
if (username) {
user = await collections.users.findOne<Pick<User, "_id">>(
{ username },
{ projection: { _id: 1 } }
);
if (!user) {
throw error(404, `User "${username}" doesn't exist`);
}
}
// if there is no user, we show community assistants, so only show featured assistants
const shouldBeFeatured =
REQUIRE_FEATURED_ASSISTANTS === "true" && !user ? { featured: true } : {};
// if the user queried is not the current user, only show "public" assistants that have been shared before
const shouldHaveBeenShared =
REQUIRE_FEATURED_ASSISTANTS === "true" && !createdByCurrentUser
? { userCount: { $gt: 1 } }
: {};
// fetch the top assistants sorted by user count from biggest to smallest. filter by model too if modelId is provided or query if query is provided
const filter: Filter<Assistant> = {
...(modelId && { modelId }),
...(user && { createdById: user._id }),
...(query && { searchTokens: { $all: generateQueryTokens(query) } }),
...shouldBeFeatured,
...shouldHaveBeenShared,
};
const assistants = await collections.assistants
.find(filter)
.skip(NUM_PER_PAGE * pageIndex)
.sort({
...(sort === SortKey.TRENDING && { last24HoursCount: -1 }),
userCount: -1,
})
.limit(NUM_PER_PAGE)
.toArray();
const numTotalItems = await collections.assistants.countDocuments(filter);
return {
assistants: JSON.parse(JSON.stringify(assistants)) as Array<Assistant>,
selectedModel: modelId ?? "",
numTotalItems,
numItemsPerPage: NUM_PER_PAGE,
query,
sort,
};
};
|