Datasets:
File size: 5,178 Bytes
8d88d9b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
import type { Migration } from ".";
import { collections } from "$lib/server/database";
import { ObjectId, type WithId } from "mongodb";
import type { Conversation } from "$lib/types/Conversation";
import type { WebSearchSource } from "$lib/types/WebSearch";
import {
MessageUpdateStatus,
MessageUpdateType,
MessageWebSearchUpdateType,
type MessageUpdate,
type MessageWebSearchFinishedUpdate,
} from "$lib/types/MessageUpdate";
import type { Message } from "$lib/types/Message";
import { isMessageWebSearchSourcesUpdate } from "$lib/utils/messageUpdates";
// -----------
// Copy of the previous message update types
export type FinalAnswer = {
type: "finalAnswer";
text: string;
};
export type TextStreamUpdate = {
type: "stream";
token: string;
};
type WebSearchUpdate = {
type: "webSearch";
messageType: "update" | "error" | "sources";
message: string;
args?: string[];
sources?: WebSearchSource[];
};
type StatusUpdate = {
type: "status";
status: "started" | "pending" | "finished" | "error" | "title";
message?: string;
};
type ErrorUpdate = {
type: "error";
message: string;
name: string;
};
type FileUpdate = {
type: "file";
sha: string;
};
type OldMessageUpdate =
| FinalAnswer
| TextStreamUpdate
| WebSearchUpdate
| StatusUpdate
| ErrorUpdate
| FileUpdate;
/** Converts the old message update to the new schema */
function convertMessageUpdate(message: Message, update: OldMessageUpdate): MessageUpdate | null {
try {
// Text and files
if (update.type === "finalAnswer") {
return {
type: MessageUpdateType.FinalAnswer,
text: update.text,
interrupted: message.interrupted ?? false,
};
} else if (update.type === "stream") {
return {
type: MessageUpdateType.Stream,
token: update.token,
};
} else if (update.type === "file") {
return {
type: MessageUpdateType.File,
name: "Unknown",
sha: update.sha,
// assume jpeg but could be any image. should be harmless
mime: "image/jpeg",
};
}
// Status
else if (update.type === "status") {
if (update.status === "title") {
return {
type: MessageUpdateType.Title,
title: update.message ?? "New Chat",
};
}
if (update.status === "pending") return null;
const status =
update.status === "started"
? MessageUpdateStatus.Started
: update.status === "finished"
? MessageUpdateStatus.Finished
: MessageUpdateStatus.Error;
return {
type: MessageUpdateType.Status,
status,
message: update.message,
};
} else if (update.type === "error") {
// Treat it as an error status update
return {
type: MessageUpdateType.Status,
status: MessageUpdateStatus.Error,
message: update.message,
};
}
// Web Search
else if (update.type === "webSearch") {
if (update.messageType === "update") {
return {
type: MessageUpdateType.WebSearch,
subtype: MessageWebSearchUpdateType.Update,
message: update.message,
args: update.args,
};
} else if (update.messageType === "error") {
return {
type: MessageUpdateType.WebSearch,
subtype: MessageWebSearchUpdateType.Error,
message: update.message,
args: update.args,
};
} else if (update.messageType === "sources") {
return {
type: MessageUpdateType.WebSearch,
subtype: MessageWebSearchUpdateType.Sources,
message: update.message,
sources: update.sources ?? [],
};
}
}
console.warn("Unknown message update during migration:", update);
return null;
} catch (error) {
console.error("Error converting message update during migration. Skipping it... Error:", error);
return null;
}
}
const updateMessageUpdates: Migration = {
_id: new ObjectId("5f9f7f7f7f7f7f7f7f7f7f7f"),
name: "Convert message updates to the new schema",
up: async () => {
const allConversations = collections.conversations.find({});
let conversation: WithId<Pick<Conversation, "messages">> | null = null;
while ((conversation = await allConversations.tryNext())) {
const messages = conversation.messages.map((message) => {
// Convert all of the existing updates to the new schema
const updates = message.updates
?.map((update) => convertMessageUpdate(message, update as OldMessageUpdate))
.filter((update): update is MessageUpdate => Boolean(update));
// Add the new web search finished update if the sources update exists and webSearch is defined
const webSearchSourcesUpdateIndex = updates?.findIndex(isMessageWebSearchSourcesUpdate);
if (
message.webSearch &&
updates &&
webSearchSourcesUpdateIndex &&
webSearchSourcesUpdateIndex !== -1
) {
const webSearchFinishedUpdate: MessageWebSearchFinishedUpdate = {
type: MessageUpdateType.WebSearch,
subtype: MessageWebSearchUpdateType.Finished,
};
updates.splice(webSearchSourcesUpdateIndex + 1, 0, webSearchFinishedUpdate);
}
return { ...message, updates };
});
// Set the new messages array
await collections.conversations.updateOne({ _id: conversation._id }, { $set: { messages } });
}
return true;
},
runEveryTime: false,
};
export default updateMessageUpdates;
|