@@ -7,6 +7,7 @@ import { type NextRequest, NextResponse } from 'next/server'
77import { importWorkflowAsSuperuserContract } from '@/lib/api/contracts/workflows'
88import { parseRequest } from '@/lib/api/server'
99import { getSession } from '@/lib/auth'
10+ import { appendCopilotChatMessages } from '@/lib/copilot/chat/messages-dual-write'
1011import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
1112import { verifyEffectiveSuperUser } from '@/lib/templates/permissions'
1213import { parseWorkflowJson } from '@/lib/workflows/operations/import-export'
@@ -172,19 +173,25 @@ export const POST = withRouteHandler(async (request: NextRequest) => {
172173 let copilotChatsImported = 0
173174
174175 for ( const chat of sourceCopilotChats ) {
175- await db . insert ( copilotChats ) . values ( {
176- userId : session . user . id ,
177- workflowId : newWorkflowId ,
178- title : chat . title ? `[Import] ${ chat . title } ` : null ,
179- messages : chat . messages ,
180- model : chat . model ,
181- conversationId : null , // Don't copy conversation ID
182- previewYaml : chat . previewYaml ,
183- planArtifact : chat . planArtifact ,
184- config : chat . config ,
185- createdAt : new Date ( ) ,
186- updatedAt : new Date ( ) ,
187- } )
176+ const [ imported ] = await db
177+ . insert ( copilotChats )
178+ . values ( {
179+ userId : session . user . id ,
180+ workflowId : newWorkflowId ,
181+ title : chat . title ? `[Import] ${ chat . title } ` : null ,
182+ messages : chat . messages ,
183+ model : chat . model ,
184+ conversationId : null , // Don't copy conversation ID
185+ previewYaml : chat . previewYaml ,
186+ planArtifact : chat . planArtifact ,
187+ config : chat . config ,
188+ createdAt : new Date ( ) ,
189+ updatedAt : new Date ( ) ,
190+ } )
191+ . returning ( { id : copilotChats . id } )
192+ if ( imported && Array . isArray ( chat . messages ) && chat . messages . length > 0 ) {
193+ await appendCopilotChatMessages ( imported . id , chat . messages , { chatModel : chat . model } )
194+ }
188195 copilotChatsImported ++
189196 }
190197
0 commit comments