@@ -5,6 +5,7 @@ import { usePathname } from 'next/navigation'
55import { executeRunToolOnClient } from '@/lib/copilot/client-sse/run-tool-execution'
66import { MOTHERSHIP_CHAT_API_PATH } from '@/lib/copilot/constants'
77import { isWorkflowToolName } from '@/lib/copilot/workflow-tools'
8+ import { getNextWorkflowColor } from '@/lib/workflows/colors'
89import { knowledgeKeys } from '@/hooks/queries/kb/knowledge'
910import { tableKeys , useTablesList } from '@/hooks/queries/tables'
1011import {
@@ -16,8 +17,10 @@ import {
1617 taskKeys ,
1718 useChatHistory ,
1819} from '@/hooks/queries/tasks'
20+ import { getTopInsertionSortOrder } from '@/hooks/queries/utils/top-insertion-sort-order'
1921import { useWorkflows , workflowKeys } from '@/hooks/queries/workflows'
2022import { useWorkspaceFiles , workspaceFilesKeys } from '@/hooks/queries/workspace-files'
23+ import { useFolderStore } from '@/stores/folders/store'
2124import { useWorkflowRegistry } from '@/stores/workflows/registry/store'
2225import type { FileAttachmentForApi } from '../components/user-input/user-input'
2326import type {
@@ -197,6 +200,34 @@ function getPayloadData(payload: SSEPayload): SSEPayloadData | undefined {
197200 return typeof payload . data === 'object' ? payload . data : undefined
198201}
199202
203+ /** Adds a workflow to the registry with a top-insertion sort order if it doesn't already exist. */
204+ function ensureWorkflowInRegistry ( resourceId : string , title : string , workspaceId : string ) : boolean {
205+ const registry = useWorkflowRegistry . getState ( )
206+ if ( registry . workflows [ resourceId ] ) return false
207+ const sortOrder = getTopInsertionSortOrder (
208+ registry . workflows ,
209+ useFolderStore . getState ( ) . folders ,
210+ workspaceId ,
211+ null
212+ )
213+ useWorkflowRegistry . setState ( ( state ) => ( {
214+ workflows : {
215+ ...state . workflows ,
216+ [ resourceId ] : {
217+ id : resourceId ,
218+ name : title ,
219+ lastModified : new Date ( ) ,
220+ createdAt : new Date ( ) ,
221+ color : getNextWorkflowColor ( ) ,
222+ workspaceId,
223+ folderId : null ,
224+ sortOrder,
225+ } ,
226+ } ,
227+ } ) )
228+ return true
229+ }
230+
200231export function useChat ( workspaceId : string , initialChatId ?: string ) : UseChatReturn {
201232 const pathname = usePathname ( )
202233 const queryClient = useQueryClient ( )
@@ -349,24 +380,7 @@ export function useChat(workspaceId: string, initialChatId?: string): UseChatRet
349380
350381 for ( const resource of restored ) {
351382 if ( resource . type !== 'workflow' ) continue
352- const registry = useWorkflowRegistry . getState ( )
353- if ( ! registry . workflows [ resource . id ] ) {
354- useWorkflowRegistry . setState ( ( state ) => ( {
355- workflows : {
356- ...state . workflows ,
357- [ resource . id ] : {
358- id : resource . id ,
359- name : resource . title ,
360- lastModified : new Date ( ) ,
361- createdAt : new Date ( ) ,
362- color : '#7F2FFF' ,
363- workspaceId,
364- folderId : null ,
365- sortOrder : 0 ,
366- } ,
367- } ,
368- } ) )
369- }
383+ ensureWorkflowInRegistry ( resource . id , resource . title , workspaceId )
370384 }
371385 }
372386 } , [ chatHistory , workspaceId ] )
@@ -649,28 +663,12 @@ export function useChat(workspaceId: string, initialChatId?: string): UseChatRet
649663 resource = extractWorkflowResource ( parsed , lastWorkflowId , storedArgs )
650664 if ( resource ) {
651665 lastWorkflowId = resource . id
652- queryClient . invalidateQueries ( { queryKey : workflowKeys . list ( workspaceId ) } )
653- const registry = useWorkflowRegistry . getState ( )
654- if ( ! registry . workflows [ resource . id ] ) {
655- useWorkflowRegistry . setState ( ( state ) => ( {
656- workflows : {
657- ...state . workflows ,
658- [ resource ! . id ] : {
659- id : resource ! . id ,
660- name : resource ! . title ,
661- lastModified : new Date ( ) ,
662- createdAt : new Date ( ) ,
663- color : '#7F2FFF' ,
664- workspaceId,
665- folderId : null ,
666- sortOrder : 0 ,
667- } ,
668- } ,
669- } ) )
670- registry . setActiveWorkflow ( resource . id )
666+ if ( ensureWorkflowInRegistry ( resource . id , resource . title , workspaceId ) ) {
667+ useWorkflowRegistry . getState ( ) . setActiveWorkflow ( resource . id )
671668 } else {
672- registry . loadWorkflowState ( resource . id )
669+ useWorkflowRegistry . getState ( ) . loadWorkflowState ( resource . id )
673670 }
671+ queryClient . invalidateQueries ( { queryKey : workflowKeys . list ( workspaceId ) } )
674672 }
675673 } else if ( toolName === 'knowledge_base' ) {
676674 resource = extractKnowledgeBaseResource ( parsed , storedArgs )
0 commit comments