Skip to content

Commit e347486

Browse files
Sg312emir-karabeg
andauthored
fix(copilot): fix copilot chat loading (#2769)
* Fix loading * Fix Lint * Scroll stickiness * Scroll stickiness * improvement: diff controls and notifications positioning * feat(copilot): editable input component --------- Co-authored-by: Emir Karabeg <[email protected]>
1 parent e21cc11 commit e347486

File tree

8 files changed

+316
-156
lines changed

8 files changed

+316
-156
lines changed

apps/sim/app/api/copilot/chat/route.ts

Lines changed: 17 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -802,49 +802,29 @@ export async function POST(req: NextRequest) {
802802
toolNames: toolCalls.map((tc) => tc?.name).filter(Boolean),
803803
})
804804

805-
// Save messages to database after streaming completes (including aborted messages)
805+
// NOTE: Messages are saved by the client via update-messages endpoint with full contentBlocks.
806+
// Server only updates conversationId here to avoid overwriting client's richer save.
806807
if (currentChat) {
807-
const updatedMessages = [...conversationHistory, userMessage]
808-
809-
// Save assistant message if there's any content or tool calls (even partial from abort)
810-
if (assistantContent.trim() || toolCalls.length > 0) {
811-
const assistantMessage = {
812-
id: crypto.randomUUID(),
813-
role: 'assistant',
814-
content: assistantContent,
815-
timestamp: new Date().toISOString(),
816-
...(toolCalls.length > 0 && { toolCalls }),
817-
}
818-
updatedMessages.push(assistantMessage)
819-
logger.info(
820-
`[${tracker.requestId}] Saving assistant message with content (${assistantContent.length} chars) and ${toolCalls.length} tool calls`
821-
)
822-
} else {
823-
logger.info(
824-
`[${tracker.requestId}] No assistant content or tool calls to save (aborted before response)`
825-
)
826-
}
827-
828808
// Persist only a safe conversationId to avoid continuing from a state that expects tool outputs
829809
const previousConversationId = currentChat?.conversationId as string | undefined
830810
const responseId = lastSafeDoneResponseId || previousConversationId || undefined
831811

832-
// Update chat in database immediately (without title)
833-
await db
834-
.update(copilotChats)
835-
.set({
836-
messages: updatedMessages,
837-
updatedAt: new Date(),
838-
...(responseId ? { conversationId: responseId } : {}),
839-
})
840-
.where(eq(copilotChats.id, actualChatId!))
812+
if (responseId) {
813+
await db
814+
.update(copilotChats)
815+
.set({
816+
updatedAt: new Date(),
817+
conversationId: responseId,
818+
})
819+
.where(eq(copilotChats.id, actualChatId!))
841820

842-
logger.info(`[${tracker.requestId}] Updated chat ${actualChatId} with new messages`, {
843-
messageCount: updatedMessages.length,
844-
savedUserMessage: true,
845-
savedAssistantMessage: assistantContent.trim().length > 0,
846-
updatedConversationId: responseId || null,
847-
})
821+
logger.info(
822+
`[${tracker.requestId}] Updated conversationId for chat ${actualChatId}`,
823+
{
824+
updatedConversationId: responseId,
825+
}
826+
)
827+
}
848828
}
849829
} catch (error) {
850830
logger.error(`[${tracker.requestId}] Error processing stream:`, error)

apps/sim/app/api/copilot/chat/update-messages/route.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ export async function POST(req: NextRequest) {
7777

7878
const { chatId, messages, planArtifact, config } = UpdateMessagesSchema.parse(body)
7979

80+
// Debug: Log what we're about to save
81+
const lastMsgParsed = messages[messages.length - 1]
82+
if (lastMsgParsed?.role === 'assistant') {
83+
logger.info(`[${tracker.requestId}] Parsed messages to save`, {
84+
messageCount: messages.length,
85+
lastMsgId: lastMsgParsed.id,
86+
lastMsgContentLength: lastMsgParsed.content?.length || 0,
87+
lastMsgContentBlockCount: lastMsgParsed.contentBlocks?.length || 0,
88+
lastMsgContentBlockTypes: lastMsgParsed.contentBlocks?.map((b: any) => b?.type) || [],
89+
})
90+
}
91+
8092
// Verify that the chat belongs to the user
8193
const [chat] = await db
8294
.select()

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/diff-controls/diff-controls.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,8 +303,8 @@ export const DiffControls = memo(function DiffControls() {
303303
!isResizing && 'transition-[bottom,right] duration-100 ease-out'
304304
)}
305305
style={{
306-
bottom: 'calc(var(--terminal-height) + 8px)',
307-
right: 'calc(var(--panel-width) + 8px)',
306+
bottom: 'calc(var(--terminal-height) + 16px)',
307+
right: 'calc(var(--panel-width) + 16px)',
308308
}}
309309
>
310310
<div

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/components/panel/components/copilot/components/copilot-message/copilot-message.tsx

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,17 +470,8 @@ const CopilotMessage: FC<CopilotMessageProps> = memo(
470470
{/* Content blocks in chronological order */}
471471
{memoizedContentBlocks}
472472

473-
{/* Show streaming indicator if streaming but no text content yet after tool calls */}
474-
{isStreaming &&
475-
!message.content &&
476-
message.contentBlocks?.every((block) => block.type === 'tool_call') && (
477-
<StreamingIndicator />
478-
)}
479-
480-
{/* Streaming indicator when no content yet */}
481-
{!cleanTextContent && !message.contentBlocks?.length && isStreaming && (
482-
<StreamingIndicator />
483-
)}
473+
{/* Always show streaming indicator at the end while streaming */}
474+
{isStreaming && <StreamingIndicator />}
484475

485476
{message.errorType === 'usage_limit' && (
486477
<div className='flex gap-1.5'>

0 commit comments

Comments
 (0)