Skip to content

Commit 7bc6170

Browse files
authored
improvement(panel): added unique conversationId to chat panel (#458)
* added unqiue conversationId to chat panel * ack race condition * add uploads directory to gitignore
1 parent c6907c2 commit 7bc6170

File tree

5 files changed

+64
-9
lines changed

5 files changed

+64
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ sim-standalone.tar.gz
2929
# misc
3030
.DS_Store
3131
*.pem
32+
uploads/
3233

3334
# env files
3435
.env

apps/sim/app/w/[id]/components/panel/components/chat/chat.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function Chat({ panelWidth, chatMessage, setChatMessage }: ChatProps) {
3131
setSelectedWorkflowOutput,
3232
appendMessageContent,
3333
finalizeMessageStream,
34+
getConversationId,
3435
} = useChatStore()
3536
const { entries } = useConsoleStore()
3637
const messagesEndRef = useRef<HTMLDivElement>(null)
@@ -91,6 +92,9 @@ export function Chat({ panelWidth, chatMessage, setChatMessage }: ChatProps) {
9192
// Store the message being sent for reference
9293
const sentMessage = chatMessage.trim()
9394

95+
// Get the conversationId for this workflow before adding the message
96+
const conversationId = getConversationId(activeWorkflowId)
97+
9498
// Add user message
9599
addMessage({
96100
content: sentMessage,
@@ -101,8 +105,11 @@ export function Chat({ panelWidth, chatMessage, setChatMessage }: ChatProps) {
101105
// Clear input
102106
setChatMessage('')
103107

104-
// Execute the workflow to generate a response, passing the chat message as input
105-
const result = await handleRunWorkflow({ input: sentMessage })
108+
// Execute the workflow to generate a response, passing the chat message and conversationId as input
109+
const result = await handleRunWorkflow({
110+
input: sentMessage,
111+
conversationId: conversationId,
112+
})
106113

107114
// Check if we got a streaming response
108115
if (result && 'stream' in result && result.stream instanceof ReadableStream) {

apps/sim/app/w/[id]/components/panel/components/chat/components/chat-modal/chat-modal.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function ChatModal({ open, onOpenChange, chatMessage, setChatMessage }: C
7676
const inputRef = useRef<HTMLInputElement>(null)
7777

7878
const { activeWorkflowId } = useWorkflowRegistry()
79-
const { messages, addMessage } = useChatStore()
79+
const { messages, addMessage, getConversationId } = useChatStore()
8080

8181
// Use the execution store state to track if a workflow is executing
8282
const { isExecuting } = useExecutionStore()
@@ -113,6 +113,9 @@ export function ChatModal({ open, onOpenChange, chatMessage, setChatMessage }: C
113113
// Store the message being sent for reference
114114
const sentMessage = chatMessage.trim()
115115

116+
// Get the conversationId for this workflow before adding the message
117+
const conversationId = getConversationId(activeWorkflowId)
118+
116119
// Add user message
117120
addMessage({
118121
content: sentMessage,
@@ -129,7 +132,10 @@ export function ChatModal({ open, onOpenChange, chatMessage, setChatMessage }: C
129132
}
130133

131134
// Execute the workflow to generate a response
132-
await handleRunWorkflow({ input: sentMessage })
135+
await handleRunWorkflow({
136+
input: sentMessage,
137+
conversationId: conversationId,
138+
})
133139

134140
// Ensure input stays focused even after response
135141
if (inputRef.current) {

apps/sim/stores/panel/chat/store.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { v4 as uuidv4 } from 'uuid'
12
import { create } from 'zustand'
23
import { devtools, persist } from 'zustand/middleware'
34
import 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) => {

apps/sim/stores/panel/chat/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@ export interface OutputConfig {
1616
export interface ChatStore {
1717
messages: ChatMessage[]
1818
selectedWorkflowOutputs: Record<string, string[]>
19+
conversationIds: Record<string, string>
1920
addMessage: (message: Omit<ChatMessage, 'id' | 'timestamp'>) => void
2021
clearChat: (workflowId: string | null) => void
2122
getWorkflowMessages: (workflowId: string) => ChatMessage[]
2223
setSelectedWorkflowOutput: (workflowId: string, outputIds: string[]) => void
2324
getSelectedWorkflowOutput: (workflowId: string) => string[]
2425
appendMessageContent: (messageId: string, content: string) => void
2526
finalizeMessageStream: (messageId: string) => void
27+
getConversationId: (workflowId: string) => string
28+
generateNewConversationId: (workflowId: string) => string
2629
}

0 commit comments

Comments
 (0)