nsarrazin commited on
Commit
919c290
·
1 Parent(s): 612f16b

refactor: optimize main load function to use Promise.all for concurrent API calls

Browse files
Files changed (1) hide show
  1. 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 settings = await client.user.settings.get().then(throwOnError);
13
- const models = await client.models.get().then(throwOnError);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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
- }
34
 
35
- // remove invalid unicode and trim whitespaces
36
- conv.title = conv.title.replace(/\uFFFD/gu, "").trimStart();
37
 
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
- }),
55
  }
56
- : {}),
57
- } satisfies ConvSidebar;
58
- }),
59
- };
60
- });
61
 
62
  return {
63
  nConversations,
64
  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),
 
 
 
 
 
 
 
72
  settings: {
73
  ...settings,
74
  ethicsModalAcceptedAt: settings.ethicsModalAcceptedAt
75
  ? new Date(settings.ethicsModalAcceptedAt)
76
  : null,
77
  },
78
- publicConfig: getConfigManager(await client["public-config"].get().then(throwOnError)),
79
- ...(await client["feature-flags"].get().then(throwOnError)),
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
  };