Skip to content

Commit 0b431c0

Browse files
committed
fix(copilot): wire dual-write into superuser import + preserve model on conflict
1 parent 069f8bd commit 0b431c0

2 files changed

Lines changed: 22 additions & 14 deletions

File tree

apps/sim/app/api/superuser/import-workflow/route.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { type NextRequest, NextResponse } from 'next/server'
77
import { importWorkflowAsSuperuserContract } from '@/lib/api/contracts/workflows'
88
import { parseRequest } from '@/lib/api/server'
99
import { getSession } from '@/lib/auth'
10+
import { appendCopilotChatMessages } from '@/lib/copilot/chat/messages-dual-write'
1011
import { withRouteHandler } from '@/lib/core/utils/with-route-handler'
1112
import { verifyEffectiveSuperUser } from '@/lib/templates/permissions'
1213
import { 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

apps/sim/lib/copilot/chat/messages-dual-write.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ export async function appendCopilotChatMessages(
6161
set: {
6262
content: sql`excluded.content`,
6363
role: sql`excluded.role`,
64-
model: sql`excluded.model`,
64+
// Preserve existing non-null model if the incoming row lacks one.
65+
model: sql`COALESCE(excluded.model, ${copilotChatMessages.model})`,
6566
updatedAt: sql`now()`,
6667
},
6768
})

0 commit comments

Comments
 (0)