Skip to content

Commit 91ab2d5

Browse files
authored
Merge pull request #34 from tybradle/fix/crypto-uuid-fallback
Fix: Add crypto.randomUUID fallback for browser compatibility
2 parents 2f58e8a + 7277354 commit 91ab2d5

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

packages/ui/src/types/attachment.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,19 @@ export interface AgentSource {
4747
name: string
4848
}
4949

50+
// Generate UUID with fallback for browsers without crypto.randomUUID
51+
function generateUUID(): string {
52+
if (typeof crypto !== "undefined" && crypto.randomUUID) {
53+
return crypto.randomUUID()
54+
}
55+
// Fallback: generate a simple UUID v4
56+
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
57+
const r = (Math.random() * 16) | 0
58+
const v = c === "x" ? r : (r & 0x3) | 0x8
59+
return v.toString(16)
60+
})
61+
}
62+
5063
export function createFileAttachment(
5164
path: string,
5265
filename: string,
@@ -63,7 +76,7 @@ export function createFileAttachment(
6376
}
6477

6578
return {
66-
id: crypto.randomUUID(),
79+
id: generateUUID(),
6780
type: "file",
6881
display: `@${filename}`,
6982
url: fileUrl,
@@ -81,7 +94,7 @@ export function createFileAttachment(
8194
export function createTextAttachment(value: string, display: string, filename: string): Attachment {
8295
const base64 = encodeTextAsBase64(value)
8396
return {
84-
id: crypto.randomUUID(),
97+
id: generateUUID(),
8598
type: "text",
8699
display,
87100
url: `data:text/plain;base64,${base64}`,
@@ -114,7 +127,7 @@ function encodeTextAsBase64(value: string): string {
114127

115128
export function createAgentAttachment(agentName: string): Attachment {
116129
return {
117-
id: crypto.randomUUID(),
130+
id: generateUUID(),
118131
type: "agent",
119132
display: `@${agentName}`,
120133
url: "",

0 commit comments

Comments
 (0)