Spaces:
Running
Running
Reduce message update size returned by websearch (#1179)
Browse files* Reduce message update size returned by websearch
* lint
* Update src/lib/migrations/routines/06-trim-message-updates.ts
Co-authored-by: Liam Dyer <[email protected]>
* clean up types
* clean up migrations
---------
Co-authored-by: Liam Dyer <[email protected]>
- src/lib/migrations/routines/04-update-message-updates.ts +0 -1
- src/lib/migrations/routines/06-trim-message-updates.ts +68 -0
- src/lib/migrations/routines/index.ts +2 -0
- src/lib/server/websearch/runWebSearch.ts +2 -2
- src/lib/server/websearch/update.ts +3 -4
- src/lib/types/MessageUpdate.ts +4 -6
- src/routes/conversation/[id]/+server.ts +0 -9
src/lib/migrations/routines/04-update-message-updates.ts
CHANGED
@@ -171,7 +171,6 @@ const updateMessageUpdates: Migration = {
|
|
171 |
const webSearchFinishedUpdate: MessageWebSearchFinishedUpdate = {
|
172 |
type: MessageUpdateType.WebSearch,
|
173 |
subtype: MessageWebSearchUpdateType.Finished,
|
174 |
-
webSearch: message.webSearch,
|
175 |
};
|
176 |
updates.splice(webSearchSourcesUpdateIndex + 1, 0, webSearchFinishedUpdate);
|
177 |
}
|
|
|
171 |
const webSearchFinishedUpdate: MessageWebSearchFinishedUpdate = {
|
172 |
type: MessageUpdateType.WebSearch,
|
173 |
subtype: MessageWebSearchUpdateType.Finished,
|
|
|
174 |
};
|
175 |
updates.splice(webSearchSourcesUpdateIndex + 1, 0, webSearchFinishedUpdate);
|
176 |
}
|
src/lib/migrations/routines/06-trim-message-updates.ts
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import type { Migration } from ".";
|
2 |
+
import { collections } from "$lib/server/database";
|
3 |
+
import { ObjectId, type WithId } from "mongodb";
|
4 |
+
import type { Conversation } from "$lib/types/Conversation";
|
5 |
+
import {
|
6 |
+
MessageUpdateType,
|
7 |
+
MessageWebSearchUpdateType,
|
8 |
+
type MessageUpdate,
|
9 |
+
} from "$lib/types/MessageUpdate";
|
10 |
+
import type { Message } from "$lib/types/Message";
|
11 |
+
|
12 |
+
// -----------
|
13 |
+
|
14 |
+
/** Converts the old message update to the new schema */
|
15 |
+
function convertMessageUpdate(message: Message, update: MessageUpdate): MessageUpdate | null {
|
16 |
+
try {
|
17 |
+
// trim final websearch update, and sources update
|
18 |
+
|
19 |
+
if (update.type === "webSearch") {
|
20 |
+
if (update.subtype === MessageWebSearchUpdateType.Sources) {
|
21 |
+
return {
|
22 |
+
type: MessageUpdateType.WebSearch,
|
23 |
+
subtype: MessageWebSearchUpdateType.Sources,
|
24 |
+
message: update.message,
|
25 |
+
sources: update.sources.map(({ link, title }) => ({ link, title })),
|
26 |
+
};
|
27 |
+
} else if (update.subtype === MessageWebSearchUpdateType.Finished) {
|
28 |
+
return {
|
29 |
+
type: MessageUpdateType.WebSearch,
|
30 |
+
subtype: MessageWebSearchUpdateType.Finished,
|
31 |
+
};
|
32 |
+
}
|
33 |
+
}
|
34 |
+
|
35 |
+
return update;
|
36 |
+
} catch (error) {
|
37 |
+
console.error("Error converting message update during migration. Skipping it... Error:", error);
|
38 |
+
return null;
|
39 |
+
}
|
40 |
+
}
|
41 |
+
|
42 |
+
const trimMessageUpdates: Migration = {
|
43 |
+
_id: new ObjectId(6),
|
44 |
+
name: "Trim message updates to reduce stored size",
|
45 |
+
up: async () => {
|
46 |
+
const allConversations = collections.conversations.find({});
|
47 |
+
|
48 |
+
let conversation: WithId<Pick<Conversation, "messages">> | null = null;
|
49 |
+
while ((conversation = await allConversations.tryNext())) {
|
50 |
+
const messages = conversation.messages.map((message) => {
|
51 |
+
// Convert all of the existing updates to the new schema
|
52 |
+
const updates = message.updates
|
53 |
+
?.map((update) => convertMessageUpdate(message, update))
|
54 |
+
.filter((update): update is MessageUpdate => Boolean(update));
|
55 |
+
|
56 |
+
return { ...message, updates };
|
57 |
+
});
|
58 |
+
|
59 |
+
// Set the new messages array
|
60 |
+
await collections.conversations.updateOne({ _id: conversation._id }, { $set: { messages } });
|
61 |
+
}
|
62 |
+
|
63 |
+
return true;
|
64 |
+
},
|
65 |
+
runEveryTime: false,
|
66 |
+
};
|
67 |
+
|
68 |
+
export default trimMessageUpdates;
|
src/lib/migrations/routines/index.ts
CHANGED
@@ -6,6 +6,7 @@ import type { Database } from "$lib/server/database";
|
|
6 |
import addToolsToSettings from "./03-add-tools-in-settings";
|
7 |
import updateMessageUpdates from "./04-update-message-updates";
|
8 |
import updateMessageFiles from "./05-update-message-files";
|
|
|
9 |
|
10 |
export interface Migration {
|
11 |
_id: ObjectId;
|
@@ -23,4 +24,5 @@ export const migrations: Migration[] = [
|
|
23 |
addToolsToSettings,
|
24 |
updateMessageUpdates,
|
25 |
updateMessageFiles,
|
|
|
26 |
];
|
|
|
6 |
import addToolsToSettings from "./03-add-tools-in-settings";
|
7 |
import updateMessageUpdates from "./04-update-message-updates";
|
8 |
import updateMessageFiles from "./05-update-message-files";
|
9 |
+
import trimMessageUpdates from "./06-trim-message-updates";
|
10 |
|
11 |
export interface Migration {
|
12 |
_id: ObjectId;
|
|
|
24 |
addToolsToSettings,
|
25 |
updateMessageUpdates,
|
26 |
updateMessageFiles,
|
27 |
+
trimMessageUpdates,
|
28 |
];
|
src/lib/server/websearch/runWebSearch.ts
CHANGED
@@ -82,7 +82,7 @@ export async function* runWebSearch(
|
|
82 |
createdAt,
|
83 |
updatedAt,
|
84 |
};
|
85 |
-
yield makeFinalAnswerUpdate(
|
86 |
return webSearch;
|
87 |
} catch (searchError) {
|
88 |
const message = searchError instanceof Error ? searchError.message : String(searchError);
|
@@ -97,7 +97,7 @@ export async function* runWebSearch(
|
|
97 |
createdAt,
|
98 |
updatedAt,
|
99 |
};
|
100 |
-
yield makeFinalAnswerUpdate(
|
101 |
return webSearch;
|
102 |
}
|
103 |
}
|
|
|
82 |
createdAt,
|
83 |
updatedAt,
|
84 |
};
|
85 |
+
yield makeFinalAnswerUpdate();
|
86 |
return webSearch;
|
87 |
} catch (searchError) {
|
88 |
const message = searchError instanceof Error ? searchError.message : String(searchError);
|
|
|
97 |
createdAt,
|
98 |
updatedAt,
|
99 |
};
|
100 |
+
yield makeFinalAnswerUpdate();
|
101 |
return webSearch;
|
102 |
}
|
103 |
}
|
src/lib/server/websearch/update.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import type {
|
2 |
import {
|
3 |
MessageUpdateType,
|
4 |
MessageWebSearchUpdateType,
|
@@ -33,14 +33,13 @@ export function makeSourcesUpdate(sources: WebSearchSource[]): MessageWebSearchS
|
|
33 |
type: MessageUpdateType.WebSearch,
|
34 |
subtype: MessageWebSearchUpdateType.Sources,
|
35 |
message: "sources",
|
36 |
-
sources,
|
37 |
};
|
38 |
}
|
39 |
|
40 |
-
export function makeFinalAnswerUpdate(
|
41 |
return {
|
42 |
type: MessageUpdateType.WebSearch,
|
43 |
subtype: MessageWebSearchUpdateType.Finished,
|
44 |
-
webSearch,
|
45 |
};
|
46 |
}
|
|
|
1 |
+
import type { WebSearchSource } from "$lib/types/WebSearch";
|
2 |
import {
|
3 |
MessageUpdateType,
|
4 |
MessageWebSearchUpdateType,
|
|
|
33 |
type: MessageUpdateType.WebSearch,
|
34 |
subtype: MessageWebSearchUpdateType.Sources,
|
35 |
message: "sources",
|
36 |
+
sources: sources.map(({ link, title }) => ({ link, title })),
|
37 |
};
|
38 |
}
|
39 |
|
40 |
+
export function makeFinalAnswerUpdate(): MessageWebSearchFinishedUpdate {
|
41 |
return {
|
42 |
type: MessageUpdateType.WebSearch,
|
43 |
subtype: MessageWebSearchUpdateType.Finished,
|
|
|
44 |
};
|
45 |
}
|
src/lib/types/MessageUpdate.ts
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
import type {
|
2 |
import type { ToolCall, ToolResult } from "$lib/types/Tool";
|
3 |
|
4 |
export type MessageUpdate =
|
@@ -39,7 +39,7 @@ export enum MessageWebSearchUpdateType {
|
|
39 |
Sources = "sources",
|
40 |
Finished = "finished",
|
41 |
}
|
42 |
-
interface BaseMessageWebSearchUpdate<TSubType extends MessageWebSearchUpdateType> {
|
43 |
type: MessageUpdateType.WebSearch;
|
44 |
subtype: TSubType;
|
45 |
}
|
@@ -58,10 +58,8 @@ export interface MessageWebSearchSourcesUpdate
|
|
58 |
message: string;
|
59 |
sources: WebSearchSource[];
|
60 |
}
|
61 |
-
export
|
62 |
-
|
63 |
-
webSearch: WebSearch;
|
64 |
-
}
|
65 |
export type MessageWebSearchUpdate =
|
66 |
| MessageWebSearchErrorUpdate
|
67 |
| MessageWebSearchGeneralUpdate
|
|
|
1 |
+
import type { WebSearchSource } from "$lib/types/WebSearch";
|
2 |
import type { ToolCall, ToolResult } from "$lib/types/Tool";
|
3 |
|
4 |
export type MessageUpdate =
|
|
|
39 |
Sources = "sources",
|
40 |
Finished = "finished",
|
41 |
}
|
42 |
+
export interface BaseMessageWebSearchUpdate<TSubType extends MessageWebSearchUpdateType> {
|
43 |
type: MessageUpdateType.WebSearch;
|
44 |
subtype: TSubType;
|
45 |
}
|
|
|
58 |
message: string;
|
59 |
sources: WebSearchSource[];
|
60 |
}
|
61 |
+
export type MessageWebSearchFinishedUpdate =
|
62 |
+
BaseMessageWebSearchUpdate<MessageWebSearchUpdateType.Finished>;
|
|
|
|
|
63 |
export type MessageWebSearchUpdate =
|
64 |
| MessageWebSearchErrorUpdate
|
65 |
| MessageWebSearchGeneralUpdate
|
src/routes/conversation/[id]/+server.ts
CHANGED
@@ -11,7 +11,6 @@ import { z } from "zod";
|
|
11 |
import {
|
12 |
MessageUpdateStatus,
|
13 |
MessageUpdateType,
|
14 |
-
MessageWebSearchUpdateType,
|
15 |
type MessageUpdate,
|
16 |
} from "$lib/types/MessageUpdate";
|
17 |
import { uploadFile } from "$lib/server/files/uploadFile";
|
@@ -359,14 +358,6 @@ export async function POST({ request, locals, params, getClientAddress }) {
|
|
359 |
];
|
360 |
}
|
361 |
|
362 |
-
// Set web search
|
363 |
-
else if (
|
364 |
-
event.type === MessageUpdateType.WebSearch &&
|
365 |
-
event.subtype === MessageWebSearchUpdateType.Finished
|
366 |
-
) {
|
367 |
-
messageToWriteTo.webSearch = event.webSearch;
|
368 |
-
}
|
369 |
-
|
370 |
// Append to the persistent message updates if it's not a stream update
|
371 |
if (event.type !== "stream") {
|
372 |
messageToWriteTo?.updates?.push(event);
|
|
|
11 |
import {
|
12 |
MessageUpdateStatus,
|
13 |
MessageUpdateType,
|
|
|
14 |
type MessageUpdate,
|
15 |
} from "$lib/types/MessageUpdate";
|
16 |
import { uploadFile } from "$lib/server/files/uploadFile";
|
|
|
358 |
];
|
359 |
}
|
360 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
361 |
// Append to the persistent message updates if it's not a stream update
|
362 |
if (event.type !== "stream") {
|
363 |
messageToWriteTo?.updates?.push(event);
|