Spaces:
Running
Running
add api assistants endpoints (#951)
Browse files* add api assistants endpoints
* lint
---------
Co-authored-by: Kevin CATHALY <[email protected]>
Co-authored-by: Nathan Sarrazin <[email protected]>
src/routes/api/assistants/+server.ts
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { collections } from "$lib/server/database.js";
|
2 |
+
import type { Assistant } from "$lib/types/Assistant";
|
3 |
+
import type { User } from "$lib/types/User";
|
4 |
+
import { generateQueryTokens } from "$lib/utils/searchTokens.js";
|
5 |
+
import type { Filter } from "mongodb";
|
6 |
+
|
7 |
+
const NUM_PER_PAGE = 24;
|
8 |
+
|
9 |
+
export async function GET({ url, locals }) {
|
10 |
+
const modelId = url.searchParams.get("modelId");
|
11 |
+
const pageIndex = parseInt(url.searchParams.get("p") ?? "0");
|
12 |
+
const username = url.searchParams.get("user");
|
13 |
+
const query = url.searchParams.get("q")?.trim() ?? null;
|
14 |
+
const createdByCurrentUser = locals.user?.username && locals.user.username === username;
|
15 |
+
|
16 |
+
let user: Pick<User, "_id"> | null = null;
|
17 |
+
if (username) {
|
18 |
+
user = await collections.users.findOne<Pick<User, "_id">>(
|
19 |
+
{ username },
|
20 |
+
{ projection: { _id: 1 } }
|
21 |
+
);
|
22 |
+
if (!user) {
|
23 |
+
return Response.json({ message: `User "${username}" doesn't exist` }, { status: 404 });
|
24 |
+
}
|
25 |
+
}
|
26 |
+
|
27 |
+
// fetch the top assistants sorted by user count from biggest to smallest, filter out all assistants with only 1 users. filter by model too if modelId is provided
|
28 |
+
const filter: Filter<Assistant> = {
|
29 |
+
...(modelId && { modelId }),
|
30 |
+
...(!createdByCurrentUser && { userCount: { $gt: 1 } }),
|
31 |
+
...(user ? { createdById: user._id } : { featured: true }),
|
32 |
+
...(query && { searchTokens: { $all: generateQueryTokens(query) } }),
|
33 |
+
};
|
34 |
+
const assistants = await collections.assistants
|
35 |
+
.find(filter)
|
36 |
+
.skip(NUM_PER_PAGE * pageIndex)
|
37 |
+
.sort({ userCount: -1 })
|
38 |
+
.limit(NUM_PER_PAGE)
|
39 |
+
.toArray();
|
40 |
+
|
41 |
+
const numTotalItems = await collections.assistants.countDocuments(filter);
|
42 |
+
|
43 |
+
return Response.json({
|
44 |
+
assistants,
|
45 |
+
selectedModel: modelId ?? "",
|
46 |
+
numTotalItems,
|
47 |
+
numItemsPerPage: NUM_PER_PAGE,
|
48 |
+
query,
|
49 |
+
});
|
50 |
+
}
|
src/routes/api/user/assistants/+server.ts
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { authCondition } from "$lib/server/auth";
|
2 |
+
import type { Conversation } from "$lib/types/Conversation";
|
3 |
+
import { collections } from "$lib/server/database";
|
4 |
+
import { ObjectId } from "mongodb";
|
5 |
+
|
6 |
+
export async function GET({ locals }) {
|
7 |
+
if (locals.user?._id || locals.sessionId) {
|
8 |
+
const settings = await collections.settings.findOne(authCondition(locals));
|
9 |
+
|
10 |
+
const conversations = await collections.conversations
|
11 |
+
.find(authCondition(locals))
|
12 |
+
.sort({ updatedAt: -1 })
|
13 |
+
.project<Pick<Conversation, "assistantId">>({
|
14 |
+
assistantId: 1,
|
15 |
+
})
|
16 |
+
.limit(300)
|
17 |
+
.toArray();
|
18 |
+
|
19 |
+
const userAssistants = settings?.assistants?.map((assistantId) => assistantId.toString()) ?? [];
|
20 |
+
const userAssistantsSet = new Set(userAssistants);
|
21 |
+
|
22 |
+
const assistantIds = [
|
23 |
+
...userAssistants.map((el) => new ObjectId(el)),
|
24 |
+
...(conversations.map((conv) => conv.assistantId).filter((el) => !!el) as ObjectId[]),
|
25 |
+
];
|
26 |
+
|
27 |
+
const assistants = await collections.assistants.find({ _id: { $in: assistantIds } }).toArray();
|
28 |
+
|
29 |
+
const res = assistants
|
30 |
+
.filter((el) => userAssistantsSet.has(el._id.toString()))
|
31 |
+
.map((el) => ({
|
32 |
+
...el,
|
33 |
+
_id: el._id.toString(),
|
34 |
+
createdById: undefined,
|
35 |
+
createdByMe:
|
36 |
+
el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
|
37 |
+
}));
|
38 |
+
|
39 |
+
return Response.json(res);
|
40 |
+
} else {
|
41 |
+
return Response.json({ message: "Must have session cookie" }, { status: 401 });
|
42 |
+
}
|
43 |
+
}
|