Skip to content

Commit 4cb5e34

Browse files
fix(mothership): minor followups (#3709)
* fix(mothership): abort fix * diff engine fix
1 parent 59307e2 commit 4cb5e34

File tree

3 files changed

+34
-9
lines changed

3 files changed

+34
-9
lines changed

apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,6 @@ export function useChat(
420420
chatIdRef.current = undefined
421421
setResolvedChatId(undefined)
422422
appliedChatIdRef.current = undefined
423-
abortControllerRef.current?.abort()
424423
abortControllerRef.current = null
425424
sendingRef.current = false
426425
setMessages([])
@@ -445,7 +444,26 @@ export function useChat(
445444

446445
appliedChatIdRef.current = chatHistory.id
447446
const mappedMessages = chatHistory.messages.map(mapStoredMessage)
448-
setMessages(mappedMessages)
447+
const shouldPreserveActiveStreamingMessage =
448+
sendingRef.current && Boolean(activeStreamId) && activeStreamId === streamIdRef.current
449+
450+
if (shouldPreserveActiveStreamingMessage) {
451+
setMessages((prev) => {
452+
const localStreamingAssistant = prev[prev.length - 1]
453+
if (localStreamingAssistant?.role !== 'assistant') {
454+
return mappedMessages
455+
}
456+
457+
const nextMessages =
458+
mappedMessages[mappedMessages.length - 1]?.role === 'assistant'
459+
? mappedMessages.slice(0, -1)
460+
: mappedMessages
461+
462+
return [...nextMessages, localStreamingAssistant]
463+
})
464+
} else {
465+
setMessages(mappedMessages)
466+
}
449467

450468
if (chatHistory.resources.some((r) => r.id === 'streaming-file')) {
451469
fetch('/api/copilot/chat/resources', {
@@ -1467,7 +1485,6 @@ export function useChat(
14671485
return () => {
14681486
streamReaderRef.current?.cancel().catch(() => {})
14691487
streamReaderRef.current = null
1470-
abortControllerRef.current?.abort()
14711488
abortControllerRef.current = null
14721489
streamGenRef.current++
14731490
sendingRef.current = false

apps/sim/lib/copilot/chat-streaming.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,6 @@ export function createSSEStream(params: StreamingOrchestrationParams): ReadableS
353353
},
354354
cancel() {
355355
clientDisconnected = true
356-
abortController.abort()
357356
if (eventWriter) {
358357
eventWriter.flush().catch(() => {})
359358
}

apps/sim/lib/workflows/diff/diff-engine.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -505,12 +505,21 @@ export class WorkflowDiffEngine {
505505
try {
506506
const baselineBlockIds = new Set(Object.keys(mergedBaseline.blocks))
507507

508-
// Identify blocks that need positioning: genuinely new blocks AND
509-
// blocks inserted into subflows (position reset to 0,0). Extracted
510-
// blocks are excluded — the server computes valid absolute positions
511-
// from the container offset, so they don't need repositioning.
508+
// Identify blocks that need positioning: genuinely new blocks that
509+
// don't have valid positions yet. Blocks already positioned by a
510+
// previous server-side layout (non-origin position) are skipped to
511+
// avoid redundant client-side re-layout that can shift blocks when
512+
// block metrics change between edits (e.g. condition handle offsets).
512513
const blocksNeedingLayout = Object.keys(finalBlocks).filter((id) => {
513-
if (!baselineBlockIds.has(id)) return true
514+
if (!baselineBlockIds.has(id)) {
515+
const pos = finalBlocks[id]?.position
516+
const hasValidPosition =
517+
pos &&
518+
Number.isFinite(pos.x) &&
519+
Number.isFinite(pos.y) &&
520+
!(pos.x === 0 && pos.y === 0)
521+
return !hasValidPosition
522+
}
514523
const baselineParent = mergedBaseline.blocks[id]?.data?.parentId ?? null
515524
const proposedParent = finalBlocks[id]?.data?.parentId ?? null
516525
if (baselineParent === proposedParent) return false

0 commit comments

Comments
 (0)