Spaces:
Running
on
Inf2
Running
on
Inf2
File size: 3,896 Bytes
564e576 7bf1507 564e576 7bf1507 564e576 a1a6daf 564e576 7bf1507 564e576 0f633f3 564e576 0f633f3 564e576 |
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 |
import type { Migration } from ".";
import { collections } from "$lib/server/database";
import { ObjectId, type WithId } from "mongodb";
import type { Conversation } from "$lib/types/Conversation";
import {
MessageUpdateStatus,
MessageUpdateType,
type MessageUpdate,
} from "$lib/types/MessageUpdate";
import type { Message } from "$lib/types/Message";
// isMessageWebSearchSourcesUpdate removed from utils; use inline predicate
// -----------
// 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?: { title?: string; link: string }[];
};
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") {
return null; // Web search updates are no longer supported
}
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));
return { ...message, updates };
});
// Set the new messages array
await collections.conversations.updateOne({ _id: conversation._id }, { $set: { messages } });
}
return true;
},
runEveryTime: false,
};
export default updateMessageUpdates;
|