not require crypto for ai chat

This commit is contained in:
Ruben Fiszel
2025-11-08 20:14:07 +00:00
parent 2d54dfbf05
commit 41a6f89bdb
4 changed files with 18 additions and 7 deletions

View File

@@ -39,6 +39,7 @@
import { aiChatManager } from './copilot/chat/AIChatManager.svelte'
import { stateSnapshot } from '$lib/svelte5Utils.svelte'
import FlowChatInterface from './flows/conversations/FlowChatInterface.svelte'
import { randomUUID } from './flows/conversations/FlowChatManager.svelte'
interface Props {
previewMode: 'upTo' | 'whole'
@@ -469,7 +470,7 @@
return jobId ?? ''
}}
createConversation={async () => {
const newConversationId = crypto.randomUUID()
const newConversationId = randomUUID()
return newConversationId
}}
/>

View File

@@ -49,6 +49,7 @@
import { AI_AGENT_SCHEMA } from '../flowInfers'
import { nextId } from '../flowModuleNextId'
import ConfirmationModal from '$lib/components/common/confirmationModal/ConfirmationModal.svelte'
import { randomUUID } from '../conversations/FlowChatManager.svelte'
interface Props {
noEditor: boolean
@@ -491,7 +492,7 @@
<FlowChatInterface
onRunFlow={runFlowWithMessage}
createConversation={async () => {
const newConversationId = crypto.randomUUID()
const newConversationId = randomUUID()
return newConversationId
}}
/>

View File

@@ -18,6 +18,15 @@ export interface FlowChatManagerOptions {
path?: string
}
export function randomUUID() {
// Pure JS (RFC4122 v4) UUID implementation (no external dependencies)
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0
const v = c === 'x' ? r : (r & 0x3) | 0x8
return v.toString(16)
})
}
class FlowChatManager {
// State
messages = $state<ChatMessage[]>([])
@@ -320,7 +329,7 @@ class FlowChatManager {
delete this.#conversationsCache[currentConversationId]
const userMessage: ChatMessage = {
id: crypto.randomUUID(),
id: randomUUID(),
content: this.inputMessage.trim(),
created_at: new Date().toISOString(),
message_type: 'user',
@@ -395,7 +404,6 @@ class FlowChatManager {
eventSource.onmessage = async (event) => {
try {
const data = JSON.parse(event.data)
console.log('data', data)
if (data.type === 'update') {
if (data.flow_stream_job_id) {
this.currentJobId = data.flow_stream_job_id
@@ -423,7 +431,7 @@ class FlowChatManager {
this.messages = [
...this.messages,
{
id: 'temp-' + crypto.randomUUID(),
id: 'temp-' + randomUUID(),
content: newContent,
created_at: new Date().toISOString(),
message_type: 'tool',
@@ -445,7 +453,7 @@ class FlowChatManager {
assistantMessageId.length === 0 &&
accumulatedContent.length > 0
) {
assistantMessageId = 'temp-' + crypto.randomUUID()
assistantMessageId = 'temp-' + randomUUID()
this.messages = [
...this.messages,
{

View File

@@ -65,6 +65,7 @@
initFlowGraphAssetsCtx
} from '$lib/components/flows/FlowAssetsHandler.svelte'
import { page } from '$app/state'
import { randomUUID } from '$lib/components/flows/conversations/FlowChatManager.svelte'
let flow: Flow | undefined = $state()
let can_write = false
@@ -401,7 +402,7 @@
let path = $derived(page.params.path ?? '')
async function handleNewConversation({ clearMessages = true }: { clearMessages?: boolean }) {
const newConversationId = crypto.randomUUID()
const newConversationId = randomUUID()
// Add the new conversation to the sidebar (returns id of draft or new conversation)
if (flowConversationsSidebar) {