Spaces:
Running
on
Inf2
Running
on
Inf2
feat(models): add transferTo property on old models (#1453)
Browse files* feat(models): add transferTo property on old models
used to transfer assistants to new models if specified
* make sure transferTo is an existing model
src/lib/migrations/routines/02-update-assistants-models.ts
CHANGED
|
@@ -7,17 +7,36 @@ const updateAssistantsModels: Migration = {
|
|
| 7 |
name: "Update deprecated models in assistants with the default model",
|
| 8 |
up: async () => {
|
| 9 |
const models = (await import("$lib/server/models")).models;
|
| 10 |
-
|
| 11 |
const { assistants } = collections;
|
| 12 |
|
| 13 |
-
const modelIds = models.map((el) => el.id);
|
| 14 |
const defaultModelId = models[0].id;
|
| 15 |
|
| 16 |
-
// Find all assistants whose modelId is not in modelIds, and update it
|
| 17 |
-
await assistants
|
| 18 |
-
{ modelId: { $nin: modelIds } }
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
return true;
|
| 23 |
},
|
|
|
|
| 7 |
name: "Update deprecated models in assistants with the default model",
|
| 8 |
up: async () => {
|
| 9 |
const models = (await import("$lib/server/models")).models;
|
| 10 |
+
const oldModels = (await import("$lib/server/models")).oldModels;
|
| 11 |
const { assistants } = collections;
|
| 12 |
|
| 13 |
+
const modelIds = models.map((el) => el.id);
|
| 14 |
const defaultModelId = models[0].id;
|
| 15 |
|
| 16 |
+
// Find all assistants whose modelId is not in modelIds, and update it
|
| 17 |
+
const bulkOps = await assistants
|
| 18 |
+
.find({ modelId: { $nin: modelIds } })
|
| 19 |
+
.map((assistant) => {
|
| 20 |
+
// has an old model
|
| 21 |
+
let newModelId = defaultModelId;
|
| 22 |
+
|
| 23 |
+
const oldModel = oldModels.find((m) => m.id === assistant.modelId);
|
| 24 |
+
if (oldModel && oldModel.transferTo && !!models.find((m) => m.id === oldModel.transferTo)) {
|
| 25 |
+
newModelId = oldModel.transferTo;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
return {
|
| 29 |
+
updateOne: {
|
| 30 |
+
filter: { _id: assistant._id },
|
| 31 |
+
update: { $set: { modelId: newModelId } },
|
| 32 |
+
},
|
| 33 |
+
};
|
| 34 |
+
})
|
| 35 |
+
.toArray();
|
| 36 |
+
|
| 37 |
+
if (bulkOps.length > 0) {
|
| 38 |
+
await assistants.bulkWrite(bulkOps);
|
| 39 |
+
}
|
| 40 |
|
| 41 |
return true;
|
| 42 |
},
|
src/lib/server/models.ts
CHANGED
|
@@ -320,6 +320,9 @@ export const models: ProcessedModel[] = await Promise.all(
|
|
| 320 |
modelsRaw.map((e) => processModel(e).then(addEndpoint))
|
| 321 |
);
|
| 322 |
|
|
|
|
|
|
|
|
|
|
| 323 |
export const defaultModel = models[0];
|
| 324 |
|
| 325 |
// Models that have been deprecated
|
|
@@ -330,6 +333,7 @@ export const oldModels = env.OLD_MODELS
|
|
| 330 |
id: z.string().optional(),
|
| 331 |
name: z.string().min(1),
|
| 332 |
displayName: z.string().min(1).optional(),
|
|
|
|
| 333 |
})
|
| 334 |
)
|
| 335 |
.parse(JSON5.parse(env.OLD_MODELS))
|
|
|
|
| 320 |
modelsRaw.map((e) => processModel(e).then(addEndpoint))
|
| 321 |
);
|
| 322 |
|
| 323 |
+
// super ugly but not sure how to make typescript happier
|
| 324 |
+
const validModelIdSchema = z.enum(models.map((m) => m.id) as [string, ...string[]]);
|
| 325 |
+
|
| 326 |
export const defaultModel = models[0];
|
| 327 |
|
| 328 |
// Models that have been deprecated
|
|
|
|
| 333 |
id: z.string().optional(),
|
| 334 |
name: z.string().min(1),
|
| 335 |
displayName: z.string().min(1).optional(),
|
| 336 |
+
transferTo: validModelIdSchema.optional(),
|
| 337 |
})
|
| 338 |
)
|
| 339 |
.parse(JSON5.parse(env.OLD_MODELS))
|