Skip to content

Commit f48fa3a

Browse files
committed
fix(mship-resources): defer reorder when ADDs are in-flight on existing chat
reorderResources previously only checked pendingPersistResourceKeysRef. When a chatId exists, addResource fires the POST immediately and only tracks the promise in inFlightResourceAddsRef — so a reorder before those ADDs settle shipped a PATCH the server rejected, and the silent catch lost the reorder. Now treat in-flight ADDs like pending ones: defer the PATCH and replay it after Promise.allSettled on the in-flight map.
1 parent 4988c78 commit f48fa3a

1 file changed

Lines changed: 29 additions & 6 deletions

File tree

  • apps/sim/app/workspace/[workspaceId]/home/hooks

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

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,15 +2116,38 @@ export function useChat(
21162116

21172117
const reorderResources = useCallback((newOrder: MothershipResource[]) => {
21182118
setResources(newOrder)
2119+
const persistChatId = chatIdRef.current ?? selectedChatIdRef.current
2120+
if (!persistChatId) return
21192121
const pendingKeys = pendingPersistResourceKeysRef.current
2120-
if (pendingKeys.size > 0) {
2122+
const inFlightAdds = inFlightResourceAddsRef.current
2123+
const hasUnsyncedAdds = newOrder.some((r) => {
2124+
const key = `${r.type}:${r.id}`
2125+
return pendingKeys.has(key) || inFlightAdds.has(key)
2126+
})
2127+
if (hasUnsyncedAdds) {
21212128
reorderNeededAfterFlushRef.current = true
2129+
if (pendingKeys.size === 0 && inFlightAdds.size > 0) {
2130+
Promise.allSettled(Array.from(inFlightAdds.values())).then(() => {
2131+
if (!reorderNeededAfterFlushRef.current) return
2132+
reorderNeededAfterFlushRef.current = false
2133+
const chatId = chatIdRef.current ?? selectedChatIdRef.current
2134+
if (!chatId) return
2135+
const order = resourcesRef.current.filter(
2136+
(r) =>
2137+
r.id !== 'streaming-file' &&
2138+
!pendingPersistResourceKeysRef.current.has(`${r.type}:${r.id}`)
2139+
)
2140+
if (order.length === 0) return
2141+
requestJson(reorderMothershipChatResourcesContract, {
2142+
body: { chatId, resources: order },
2143+
}).catch((err) => {
2144+
logger.warn('Failed to sync resource order after in-flight ADDs', err)
2145+
})
2146+
})
2147+
}
2148+
return
21222149
}
2123-
const persistChatId = chatIdRef.current ?? selectedChatIdRef.current
2124-
if (!persistChatId) return
2125-
const persistableResources = newOrder.filter(
2126-
(r) => r.id !== 'streaming-file' && !pendingKeys.has(`${r.type}:${r.id}`)
2127-
)
2150+
const persistableResources = newOrder.filter((r) => r.id !== 'streaming-file')
21282151
if (persistableResources.length === 0) return
21292152
requestJson(reorderMothershipChatResourcesContract, {
21302153
body: { chatId: persistChatId, resources: persistableResources },

0 commit comments

Comments
 (0)