Merge pull request #34 from tybradle/fix/crypto-uuid-fallback
Fix: Add crypto.randomUUID fallback for browser compatibility
This commit is contained in:
@@ -47,6 +47,19 @@ export interface AgentSource {
|
|||||||
name: string
|
name: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Generate UUID with fallback for browsers without crypto.randomUUID
|
||||||
|
function generateUUID(): string {
|
||||||
|
if (typeof crypto !== "undefined" && crypto.randomUUID) {
|
||||||
|
return crypto.randomUUID()
|
||||||
|
}
|
||||||
|
// Fallback: generate a simple UUID v4
|
||||||
|
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
|
||||||
|
const r = (Math.random() * 16) | 0
|
||||||
|
const v = c === "x" ? r : (r & 0x3) | 0x8
|
||||||
|
return v.toString(16)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
export function createFileAttachment(
|
export function createFileAttachment(
|
||||||
path: string,
|
path: string,
|
||||||
filename: string,
|
filename: string,
|
||||||
@@ -63,7 +76,7 @@ export function createFileAttachment(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: crypto.randomUUID(),
|
id: generateUUID(),
|
||||||
type: "file",
|
type: "file",
|
||||||
display: `@${filename}`,
|
display: `@${filename}`,
|
||||||
url: fileUrl,
|
url: fileUrl,
|
||||||
@@ -81,7 +94,7 @@ export function createFileAttachment(
|
|||||||
export function createTextAttachment(value: string, display: string, filename: string): Attachment {
|
export function createTextAttachment(value: string, display: string, filename: string): Attachment {
|
||||||
const base64 = encodeTextAsBase64(value)
|
const base64 = encodeTextAsBase64(value)
|
||||||
return {
|
return {
|
||||||
id: crypto.randomUUID(),
|
id: generateUUID(),
|
||||||
type: "text",
|
type: "text",
|
||||||
display,
|
display,
|
||||||
url: `data:text/plain;base64,${base64}`,
|
url: `data:text/plain;base64,${base64}`,
|
||||||
@@ -114,7 +127,7 @@ function encodeTextAsBase64(value: string): string {
|
|||||||
|
|
||||||
export function createAgentAttachment(agentName: string): Attachment {
|
export function createAgentAttachment(agentName: string): Attachment {
|
||||||
return {
|
return {
|
||||||
id: crypto.randomUUID(),
|
id: generateUUID(),
|
||||||
type: "agent",
|
type: "agent",
|
||||||
display: `@${agentName}`,
|
display: `@${agentName}`,
|
||||||
url: "",
|
url: "",
|
||||||
|
|||||||
Reference in New Issue
Block a user