Skip to content

Commit 30ffe0b

Browse files
committed
fix(fork-chat): fix messageid handling in fork chat
1 parent 2446e82 commit 30ffe0b

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

apps/sim/app/api/mothership/chats/[chatId]/fork/route.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,12 @@ describe('POST /api/mothership/chats/[chatId]/fork', () => {
393393
const goCall = mockFetchGo.mock.calls[0]
394394
expect(goCall[0]).toBe('http://mothership.test/api/chats/fork')
395395
const goBody = JSON.parse(goCall[1].body)
396+
// The copilot service only knows USER message ids, so the clone cut is the
397+
// kept slice's last user message (msg-1), not the clicked assistant (msg-2).
396398
expect(goBody).toEqual({
397399
sourceChatId: 'chat-1',
398400
newChatId: body.id,
399-
upToMessageId: 'msg-2',
401+
upToMessageId: 'msg-1',
400402
userId: 'user-1',
401403
})
402404

apps/sim/app/api/mothership/chats/[chatId]/fork/route.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,16 @@ export const POST = withRouteHandler(
236236

237237
// Clone copilot-service conversation state (messages, active_messages, memory files).
238238
// Best-effort: if the copilot service doesn't have a row for the source chat yet, skip.
239+
// The service stamps MessageID only on USER messages (assistant rows carry
240+
// Sim-local ids it has never seen), so hand it the kept slice's last user
241+
// message — it clones through the end of that turn, matching this route's cut.
242+
let goCutMessageId = upToMessageId
243+
for (let i = forkedMessages.length - 1; i >= 0; i--) {
244+
if (forkedMessages[i].role === 'user') {
245+
goCutMessageId = forkedMessages[i].id
246+
break
247+
}
248+
}
239249
try {
240250
const copilotHeaders: Record<string, string> = { 'Content-Type': 'application/json' }
241251
if (env.COPILOT_API_KEY) {
@@ -249,7 +259,7 @@ export const POST = withRouteHandler(
249259
body: JSON.stringify({
250260
sourceChatId: chatId,
251261
newChatId: newId,
252-
upToMessageId,
262+
upToMessageId: goCutMessageId,
253263
userId,
254264
}),
255265
spanName: 'sim → go /api/chats/fork',

0 commit comments

Comments
 (0)