Spaces:
Sleeping
Sleeping
refactor: optimize main load function to use Promise.all for concurrent API calls
Browse files- src/routes/+layout.ts +67 -51
src/routes/+layout.ts
CHANGED
|
@@ -9,73 +9,89 @@ export const load = async ({ depends, fetch }) => {
|
|
| 9 |
|
| 10 |
const client = useAPIClient({ fetch });
|
| 11 |
|
| 12 |
-
const
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
const defaultModel = models[0];
|
| 15 |
|
| 16 |
-
// if the active model is not in the list of models, its probably an assistant
|
| 17 |
-
// so we fetch it
|
| 18 |
const assistantActive = !models.map(({ id }) => id).includes(settings?.activeModel ?? "");
|
| 19 |
|
| 20 |
-
const
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 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 |
-
}
|
| 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 |
return {
|
| 63 |
nConversations,
|
| 64 |
conversations,
|
| 65 |
-
assistant:
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
settings: {
|
| 73 |
...settings,
|
| 74 |
ethicsModalAcceptedAt: settings.ethicsModalAcceptedAt
|
| 75 |
? new Date(settings.ethicsModalAcceptedAt)
|
| 76 |
: null,
|
| 77 |
},
|
| 78 |
-
publicConfig: getConfigManager(
|
| 79 |
-
...
|
| 80 |
};
|
| 81 |
};
|
|
|
|
| 9 |
|
| 10 |
const client = useAPIClient({ fetch });
|
| 11 |
|
| 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 |
+
|
| 36 |
const defaultModel = models[0];
|
| 37 |
|
|
|
|
|
|
|
| 38 |
const assistantActive = !models.map(({ id }) => id).includes(settings?.activeModel ?? "");
|
| 39 |
|
| 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 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
// remove invalid unicode and trim whitespaces
|
| 47 |
+
conv.title = conv.title.replace(/\uFFFD/gu, "").trimStart();
|
| 48 |
|
| 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;
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
+
}),
|
| 66 |
+
}
|
| 67 |
+
: {}),
|
| 68 |
+
} satisfies ConvSidebar;
|
| 69 |
+
});
|
| 70 |
|
| 71 |
return {
|
| 72 |
nConversations,
|
| 73 |
conversations,
|
| 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,
|
| 88 |
settings: {
|
| 89 |
...settings,
|
| 90 |
ethicsModalAcceptedAt: settings.ethicsModalAcceptedAt
|
| 91 |
? new Date(settings.ethicsModalAcceptedAt)
|
| 92 |
: null,
|
| 93 |
},
|
| 94 |
+
publicConfig: getConfigManager(publicConfig),
|
| 95 |
+
...featureFlags,
|
| 96 |
};
|
| 97 |
};
|