Skip to content

Commit d8e839e

Browse files
authored
Use uuid for message ID generation instead of crypto method (#882)
Because `crypto.randomUUID()` is not available on non-secure contexts (for example `http` versions of chat-ui), this would break on non-https deployments of chat-ui. This should fix #870 and fix #868
1 parent 00443e1 commit d8e839e

File tree

10 files changed

+38
-9
lines changed

10 files changed

+38
-9
lines changed

package-lock.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"@types/jsdom": "^21.1.1",
2525
"@types/marked": "^4.0.8",
2626
"@types/parquetjs": "^0.10.3",
27+
"@types/uuid": "^9.0.8",
2728
"@typescript-eslint/eslint-plugin": "^6.x",
2829
"@typescript-eslint/parser": "^6.x",
2930
"eslint": "^8.28.0",
@@ -71,6 +72,7 @@
7172
"sharp": "^0.33.2",
7273
"tailwind-scrollbar": "^3.0.0",
7374
"tailwindcss": "^3.4.0",
75+
"uuid": "^9.0.1",
7476
"zod": "^3.22.3"
7577
},
7678
"optionalDependencies": {

src/lib/types/Message.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { MessageUpdate } from "./MessageUpdate";
22
import type { Timestamps } from "./Timestamps";
33
import type { WebSearch } from "./WebSearch";
4+
import type { v4 } from "uuid";
45

56
export type Message = Partial<Timestamps> & {
67
from: "user" | "assistant" | "system";
7-
id: ReturnType<typeof crypto.randomUUID>;
8+
id: ReturnType<typeof v4>;
89
content: string;
910
updates?: MessageUpdate[];
1011
webSearchId?: WebSearch["_id"]; // legacy version

src/lib/utils/tree/addChildren.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Conversation } from "$lib/types/Conversation";
22
import type { Message } from "$lib/types/Message";
3+
import { v4 } from "uuid";
34

45
export function addChildren(
56
conv: Pick<Conversation, "messages" | "rootMessageId">,
@@ -8,7 +9,7 @@ export function addChildren(
89
): Message["id"] {
910
// if this is the first message we just push it
1011
if (conv.messages.length === 0) {
11-
const messageId = crypto.randomUUID();
12+
const messageId = v4();
1213
conv.rootMessageId = messageId;
1314
conv.messages.push({
1415
...message,
@@ -22,7 +23,7 @@ export function addChildren(
2223
throw new Error("You need to specify a parentId if this is not the first message");
2324
}
2425

25-
const messageId = crypto.randomUUID();
26+
const messageId = v4();
2627
if (!conv.rootMessageId) {
2728
// if there is no parentId we just push the message
2829
if (!!parentId && parentId !== conv.messages[conv.messages.length - 1].id) {

src/lib/utils/tree/addSibling.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Conversation } from "$lib/types/Conversation";
22
import type { Message } from "$lib/types/Message";
3+
import { v4 } from "uuid";
34

45
export function addSibling(
56
conv: Pick<Conversation, "messages" | "rootMessageId">,
@@ -23,7 +24,7 @@ export function addSibling(
2324
throw new Error("The sibling message is the root message, therefore we can't add a sibling");
2425
}
2526

26-
const messageId = crypto.randomUUID();
27+
const messageId = v4();
2728

2829
conv.messages.push({
2930
...message,

src/lib/utils/tree/convertLegacyConversation.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Conversation } from "$lib/types/Conversation";
22
import type { Message } from "$lib/types/Message";
3+
import { v4 } from "uuid";
34

45
export function convertLegacyConversation(
56
conv: Pick<Conversation, "messages" | "rootMessageId" | "preprompt">
@@ -12,7 +13,7 @@ export function convertLegacyConversation(
1213
content: conv.preprompt ?? "",
1314
createdAt: new Date(),
1415
updatedAt: new Date(),
15-
id: crypto.randomUUID(),
16+
id: v4(),
1617
} satisfies Message,
1718
...conv.messages,
1819
];

src/lib/utils/tree/isMessageId.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { describe, expect, it } from "vitest";
22
import { isMessageId } from "./isMessageId";
3+
import { v4 } from "uuid";
34

45
describe("isMessageId", () => {
56
it("should return true for a valid message id", () => {
6-
expect(isMessageId(crypto.randomUUID())).toBe(true);
7+
expect(isMessageId(v4())).toBe(true);
78
});
89
it("should return false for an invalid message id", () => {
910
expect(isMessageId("1-2-3-4")).toBe(false);

src/routes/conversation/+server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { z } from "zod";
77
import type { Message } from "$lib/types/Message";
88
import { models, validateModel } from "$lib/server/models";
99
import { defaultEmbeddingModel } from "$lib/server/embeddingModels";
10+
import { v4 } from "uuid";
1011

1112
export const POST: RequestHandler = async ({ locals, request }) => {
1213
const body = await request.text();
@@ -24,7 +25,7 @@ export const POST: RequestHandler = async ({ locals, request }) => {
2425

2526
let messages: Message[] = [
2627
{
27-
id: crypto.randomUUID(),
28+
id: v4(),
2829
from: "system",
2930
content: values.preprompt ?? "",
3031
createdAt: new Date(),

src/routes/conversation/[id]/+page.svelte

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import { addChildren } from "$lib/utils/tree/addChildren";
1919
import { addSibling } from "$lib/utils/tree/addSibling";
2020
import { createConvTreeStore } from "$lib/stores/convTree";
21+
import type { v4 } from "uuid";
2122
2223
export let data;
2324
@@ -72,7 +73,7 @@
7273
isContinue = false,
7374
}: {
7475
prompt?: string;
75-
messageId?: ReturnType<typeof crypto.randomUUID>;
76+
messageId?: ReturnType<typeof v4>;
7677
isRetry?: boolean;
7778
isContinue?: boolean;
7879
}): Promise<void> {

vite.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ export default defineConfig({
2626
loadTTFAsArrayBuffer(),
2727
],
2828
optimizeDeps: {
29-
include: ["browser-image-resizer"],
29+
include: ["browser-image-resizer", "uuid"],
3030
},
3131
});

0 commit comments

Comments
 (0)