Skip to content

Commit 8d185d5

Browse files
committed
refactor: optimize main load function to use Promise.all for concurrent API calls
1 parent e91cac7 commit 8d185d5

1 file changed

Lines changed: 67 additions & 51 deletions

File tree

src/routes/+layout.ts

Lines changed: 67 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -9,73 +9,89 @@ export const load = async ({ depends, fetch }) => {
99

1010
const client = useAPIClient({ fetch });
1111

12-
const settings = await client.user.settings.get().then(throwOnError);
13-
const models = await client.models.get().then(throwOnError);
12+
const [
13+
settings,
14+
models,
15+
assistants,
16+
oldModels,
17+
tools,
18+
communityToolCount,
19+
user,
20+
publicConfig,
21+
featureFlags,
22+
conversationsData,
23+
] = await Promise.all([
24+
client.user.settings.get().then(throwOnError),
25+
client.models.get().then(throwOnError),
26+
client.user.assistants.get().then(throwOnError),
27+
client.models.old.get().then(throwOnError),
28+
client.tools.active.get().then(throwOnError),
29+
client.tools.count.get().then(throwOnError),
30+
client.user.get().then(throwOnErrorNullable),
31+
client["public-config"].get().then(throwOnError),
32+
client["feature-flags"].get().then(throwOnError),
33+
client.conversations.get({ query: { p: 0 } }).then(throwOnError),
34+
]);
35+
1436
const defaultModel = models[0];
1537

16-
// if the active model is not in the list of models, its probably an assistant
17-
// so we fetch it
1838
const assistantActive = !models.map(({ id }) => id).includes(settings?.activeModel ?? "");
1939

20-
const assistant = assistantActive
21-
? await client.assistants({ id: settings?.activeModel }).get().then(throwOnErrorNullable)
22-
: null;
23-
24-
const { conversations, nConversations } = await client.conversations
25-
.get({ query: { p: 0 } })
26-
.then(throwOnError)
27-
.then(({ conversations, nConversations }) => {
28-
return {
29-
nConversations,
30-
conversations: conversations.map((conv) => {
31-
if (settings?.hideEmojiOnSidebar) {
32-
conv.title = conv.title.replace(/\p{Emoji}/gu, "");
33-
}
40+
const { conversations: rawConversations, nConversations } = conversationsData;
41+
const conversations = rawConversations.map((conv) => {
42+
if (settings?.hideEmojiOnSidebar) {
43+
conv.title = conv.title.replace(/\p{Emoji}/gu, "");
44+
}
3445

35-
// remove invalid unicode and trim whitespaces
36-
conv.title = conv.title.replace(/\uFFFD/gu, "").trimStart();
46+
// remove invalid unicode and trim whitespaces
47+
conv.title = conv.title.replace(/\uFFFD/gu, "").trimStart();
3748

38-
return {
39-
id: conv._id.toString(),
40-
title: conv.title,
41-
model: conv.model ?? defaultModel,
42-
updatedAt: new Date(conv.updatedAt),
43-
...(conv.assistantId
44-
? {
45-
assistantId: conv.assistantId.toString(),
46-
avatarUrl: client
47-
.assistants({ id: conv.assistantId.toString() })
48-
.get()
49-
.then(throwOnErrorNullable)
50-
.then((assistant) => {
51-
if (!assistant.avatar) {
52-
return undefined;
53-
}
54-
}),
49+
return {
50+
id: conv._id.toString(),
51+
title: conv.title,
52+
model: conv.model ?? defaultModel,
53+
updatedAt: new Date(conv.updatedAt),
54+
...(conv.assistantId
55+
? {
56+
assistantId: conv.assistantId.toString(),
57+
avatarUrl: client
58+
.assistants({ id: conv.assistantId.toString() })
59+
.get()
60+
.then(throwOnErrorNullable)
61+
.then((assistant) => {
62+
if (!assistant.avatar) {
63+
return undefined;
5564
}
56-
: {}),
57-
} satisfies ConvSidebar;
58-
}),
59-
};
60-
});
65+
}),
66+
}
67+
: {}),
68+
} satisfies ConvSidebar;
69+
});
6170

6271
return {
6372
nConversations,
6473
conversations,
65-
assistant: assistant ? jsonSerialize(assistant) : undefined,
66-
assistants: await client.user.assistants.get().then(throwOnError),
67-
models: await client.models.get().then(throwOnError),
68-
oldModels: await client.models.old.get().then(throwOnError),
69-
tools: await client.tools.active.get().then(throwOnError),
70-
communityToolCount: await client.tools.count.get().then(throwOnError),
71-
user: await client.user.get().then(throwOnErrorNullable),
74+
assistant: assistantActive
75+
? await client
76+
.assistants({ id: settings?.activeModel })
77+
.get()
78+
.then(throwOnErrorNullable)
79+
.then(jsonSerialize)
80+
.catch(() => undefined)
81+
: null,
82+
assistants,
83+
models,
84+
oldModels,
85+
tools,
86+
communityToolCount,
87+
user,
7288
settings: {
7389
...settings,
7490
ethicsModalAcceptedAt: settings.ethicsModalAcceptedAt
7591
? new Date(settings.ethicsModalAcceptedAt)
7692
: null,
7793
},
78-
publicConfig: getConfigManager(await client["public-config"].get().then(throwOnError)),
79-
...(await client["feature-flags"].get().then(throwOnError)),
94+
publicConfig: getConfigManager(publicConfig),
95+
...featureFlags,
8096
};
8197
};

0 commit comments

Comments
 (0)