Spaces:
Running
Running
Add optional timestamps to messages (#294)
Browse files* add optional timestamp field to messages
* Add a `hashConv` function that only uses a subset of the message for hashing
src/lib/types/Message.ts
CHANGED
@@ -1,7 +1,9 @@
|
|
1 |
-
|
|
|
|
|
2 |
from: "user" | "assistant";
|
3 |
id: ReturnType<typeof crypto.randomUUID>;
|
4 |
content: string;
|
5 |
webSearchId?: string;
|
6 |
score?: -1 | 0 | 1;
|
7 |
-
}
|
|
|
1 |
+
import type { Timestamps } from "./Timestamps";
|
2 |
+
|
3 |
+
export type Message = Partial<Timestamps> & {
|
4 |
from: "user" | "assistant";
|
5 |
id: ReturnType<typeof crypto.randomUUID>;
|
6 |
content: string;
|
7 |
webSearchId?: string;
|
8 |
score?: -1 | 0 | 1;
|
9 |
+
};
|
src/lib/utils/hashConv.ts
ADDED
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import type { Conversation } from "$lib/types/Conversation";
|
2 |
+
import { sha256 } from "./sha256";
|
3 |
+
|
4 |
+
export async function hashConv(conv: Conversation) {
|
5 |
+
// messages contains the conversation message but only the immutable part
|
6 |
+
const messages = conv.messages.map((message) => {
|
7 |
+
return (({ from, id, content, webSearchId }) => ({ from, id, content, webSearchId }))(message);
|
8 |
+
});
|
9 |
+
|
10 |
+
const hash = await sha256(JSON.stringify(messages));
|
11 |
+
return hash;
|
12 |
+
}
|
src/routes/conversation/[id]/+server.ts
CHANGED
@@ -73,12 +73,18 @@ export async function POST({ request, fetch, locals, params }) {
|
|
73 |
}
|
74 |
return [
|
75 |
...conv.messages.slice(0, retryMessageIdx),
|
76 |
-
{ content: newPrompt, from: "user", id: messageId as Message["id"] },
|
77 |
];
|
78 |
}
|
79 |
return [
|
80 |
...conv.messages,
|
81 |
-
{
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
];
|
83 |
})() satisfies Message[];
|
84 |
|
@@ -130,6 +136,8 @@ export async function POST({ request, fetch, locals, params }) {
|
|
130 |
content: generated_text,
|
131 |
webSearchId: web_search_id,
|
132 |
id: (responseId as Message["id"]) || crypto.randomUUID(),
|
|
|
|
|
133 |
});
|
134 |
|
135 |
await collections.messageEvents.insertOne({
|
|
|
73 |
}
|
74 |
return [
|
75 |
...conv.messages.slice(0, retryMessageIdx),
|
76 |
+
{ content: newPrompt, from: "user", id: messageId as Message["id"], updatedAt: new Date() },
|
77 |
];
|
78 |
}
|
79 |
return [
|
80 |
...conv.messages,
|
81 |
+
{
|
82 |
+
content: newPrompt,
|
83 |
+
from: "user",
|
84 |
+
id: (messageId as Message["id"]) || crypto.randomUUID(),
|
85 |
+
createdAt: new Date(),
|
86 |
+
updatedAt: new Date(),
|
87 |
+
},
|
88 |
];
|
89 |
})() satisfies Message[];
|
90 |
|
|
|
136 |
content: generated_text,
|
137 |
webSearchId: web_search_id,
|
138 |
id: (responseId as Message["id"]) || crypto.randomUUID(),
|
139 |
+
createdAt: new Date(),
|
140 |
+
updatedAt: new Date(),
|
141 |
});
|
142 |
|
143 |
await collections.messageEvents.insertOne({
|
src/routes/conversation/[id]/share/+server.ts
CHANGED
@@ -3,7 +3,7 @@ import { PUBLIC_ORIGIN, PUBLIC_SHARE_PREFIX } from "$env/static/public";
|
|
3 |
import { authCondition } from "$lib/server/auth";
|
4 |
import { collections } from "$lib/server/database";
|
5 |
import type { SharedConversation } from "$lib/types/SharedConversation";
|
6 |
-
import {
|
7 |
import { error } from "@sveltejs/kit";
|
8 |
import { ObjectId } from "mongodb";
|
9 |
import { nanoid } from "nanoid";
|
@@ -18,7 +18,7 @@ export async function POST({ params, url, locals }) {
|
|
18 |
throw error(404, "Conversation not found");
|
19 |
}
|
20 |
|
21 |
-
const hash = await
|
22 |
|
23 |
const existingShare = await collections.sharedConversations.findOne({ hash });
|
24 |
|
|
|
3 |
import { authCondition } from "$lib/server/auth";
|
4 |
import { collections } from "$lib/server/database";
|
5 |
import type { SharedConversation } from "$lib/types/SharedConversation";
|
6 |
+
import { hashConv } from "$lib/utils/hashConv.js";
|
7 |
import { error } from "@sveltejs/kit";
|
8 |
import { ObjectId } from "mongodb";
|
9 |
import { nanoid } from "nanoid";
|
|
|
18 |
throw error(404, "Conversation not found");
|
19 |
}
|
20 |
|
21 |
+
const hash = await hashConv(conversation);
|
22 |
|
23 |
const existingShare = await collections.sharedConversations.findOne({ hash });
|
24 |
|
src/routes/search/[id]/+server.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import { collections } from "$lib/server/database";
|
2 |
-
import {
|
3 |
import { error } from "@sveltejs/kit";
|
4 |
import { ObjectId } from "mongodb";
|
5 |
|
@@ -23,7 +23,7 @@ export async function GET({ params, locals }) {
|
|
23 |
}
|
24 |
|
25 |
// there's no better way to see if a conversation has been shared, so we hash the messages and see if there's a shared conversation with the same hash
|
26 |
-
const hash = await
|
27 |
const sharedConv = await collections.sharedConversations.findOne({
|
28 |
hash: hash,
|
29 |
});
|
|
|
1 |
import { collections } from "$lib/server/database";
|
2 |
+
import { hashConv } from "$lib/utils/hashConv.js";
|
3 |
import { error } from "@sveltejs/kit";
|
4 |
import { ObjectId } from "mongodb";
|
5 |
|
|
|
23 |
}
|
24 |
|
25 |
// there's no better way to see if a conversation has been shared, so we hash the messages and see if there's a shared conversation with the same hash
|
26 |
+
const hash = await hashConv(conv);
|
27 |
const sharedConv = await collections.sharedConversations.findOne({
|
28 |
hash: hash,
|
29 |
});
|