1+ import { v4 as uuidv4 } from 'uuid'
12import { create } from 'zustand'
23import { devtools , persist } from 'zustand/middleware'
34import type { ChatMessage , ChatStore } from './types'
@@ -11,6 +12,7 @@ export const useChatStore = create<ChatStore>()(
1112 ( set , get ) => ( {
1213 messages : [ ] ,
1314 selectedWorkflowOutputs : { } ,
15+ conversationIds : { } ,
1416
1517 addMessage : ( message ) => {
1618 set ( ( state ) => {
@@ -29,11 +31,28 @@ export const useChatStore = create<ChatStore>()(
2931 } ,
3032
3133 clearChat : ( workflowId : string | null ) => {
32- set ( ( state ) => ( {
33- messages : state . messages . filter (
34- ( message ) => ! workflowId || message . workflowId !== workflowId
35- ) ,
36- } ) )
34+ set ( ( state ) => {
35+ const newState = {
36+ messages : state . messages . filter (
37+ ( message ) => ! workflowId || message . workflowId !== workflowId
38+ ) ,
39+ }
40+
41+ // Generate a new conversationId when clearing chat for a specific workflow
42+ if ( workflowId ) {
43+ const newConversationIds = { ...state . conversationIds }
44+ newConversationIds [ workflowId ] = uuidv4 ( )
45+ return {
46+ ...newState ,
47+ conversationIds : newConversationIds ,
48+ }
49+ }
50+ // When clearing all chats (workflowId is null), also clear all conversationIds
51+ return {
52+ ...newState ,
53+ conversationIds : { } ,
54+ }
55+ } )
3756 } ,
3857
3958 getWorkflowMessages : ( workflowId ) => {
@@ -62,6 +81,25 @@ export const useChatStore = create<ChatStore>()(
6281 return get ( ) . selectedWorkflowOutputs [ workflowId ] || [ ]
6382 } ,
6483
84+ getConversationId : ( workflowId ) => {
85+ const state = get ( )
86+ if ( ! state . conversationIds [ workflowId ] ) {
87+ // Generate a new conversation ID if one doesn't exist
88+ return get ( ) . generateNewConversationId ( workflowId )
89+ }
90+ return state . conversationIds [ workflowId ]
91+ } ,
92+
93+ generateNewConversationId : ( workflowId ) => {
94+ const newId = uuidv4 ( )
95+ set ( ( state ) => {
96+ const newConversationIds = { ...state . conversationIds }
97+ newConversationIds [ workflowId ] = newId
98+ return { conversationIds : newConversationIds }
99+ } )
100+ return newId
101+ } ,
102+
65103 appendMessageContent : ( messageId , content ) => {
66104 set ( ( state ) => {
67105 const newMessages = state . messages . map ( ( message ) => {
0 commit comments