Mishig commited on
Commit
264c8d0
1 Parent(s): 105d8aa

[Assistants] Fix `remove` behaviour (#869)

Browse files

Follow up to https://github.com/huggingface/chat-ui/pull/859,
specifically to [this
comment](https://github.com/huggingface/chat-ui/pull/859#discussion_r1497978234)

Bug: when a user removes an assistant, the assistant was NOT being
removed from the settings list.
[This PR fixes this
problem](https://github.com/huggingface/chat-ui/pull/869#discussion_r1499470412)

Files changed (1) hide show
  1. src/routes/+layout.server.ts +14 -8
src/routes/+layout.server.ts CHANGED
@@ -76,8 +76,11 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
76
  .limit(300)
77
  .toArray();
78
 
 
 
 
79
  const assistantIds = [
80
- ...(settings?.assistants?.map((assistantId) => assistantId) ?? []),
81
  ...(conversations.map((conv) => conv.assistantId).filter((el) => !!el) as ObjectId[]),
82
  ];
83
 
@@ -147,7 +150,7 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
147
  settings?.shareConversationsWithModelAuthors ??
148
  DEFAULT_SETTINGS.shareConversationsWithModelAuthors,
149
  customPrompts: settings?.customPrompts ?? {},
150
- assistants: settings?.assistants?.map((el) => el.toString()) ?? [],
151
  },
152
  models: models.map((model) => ({
153
  id: model.id,
@@ -166,12 +169,15 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
166
  unlisted: model.unlisted,
167
  })),
168
  oldModels,
169
- assistants: assistants.map((el) => ({
170
- ...el,
171
- _id: el._id.toString(),
172
- createdById: undefined,
173
- createdByMe: el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
174
- })),
 
 
 
175
  user: locals.user && {
176
  id: locals.user._id.toString(),
177
  username: locals.user.username,
 
76
  .limit(300)
77
  .toArray();
78
 
79
+ const userAssistants = settings?.assistants?.map((assistantId) => assistantId.toString()) ?? [];
80
+ const userAssistantsSet = new Set(userAssistants);
81
+
82
  const assistantIds = [
83
+ ...userAssistants.map((el) => new ObjectId(el)),
84
  ...(conversations.map((conv) => conv.assistantId).filter((el) => !!el) as ObjectId[]),
85
  ];
86
 
 
150
  settings?.shareConversationsWithModelAuthors ??
151
  DEFAULT_SETTINGS.shareConversationsWithModelAuthors,
152
  customPrompts: settings?.customPrompts ?? {},
153
+ assistants: userAssistants,
154
  },
155
  models: models.map((model) => ({
156
  id: model.id,
 
169
  unlisted: model.unlisted,
170
  })),
171
  oldModels,
172
+ assistants: assistants
173
+ .filter((el) => userAssistantsSet.has(el._id.toString()))
174
+ .map((el) => ({
175
+ ...el,
176
+ _id: el._id.toString(),
177
+ createdById: undefined,
178
+ createdByMe:
179
+ el.createdById.toString() === (locals.user?._id ?? locals.sessionId).toString(),
180
+ })),
181
  user: locals.user && {
182
  id: locals.user._id.toString(),
183
  username: locals.user.username,