Skip to content

Commit b21ab82

Browse files
committed
fix(mothership): persist new-chat tabs in attachment order
Attachments resolve concurrently, so tabs were persisted in completion order. Each authorized tab now keeps its attachment index.
1 parent 7dd15b1 commit b21ab82

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

‎apps/sim/lib/mothership/chat/resource-attachments.ts‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,15 @@ export async function resolveResourceAttachmentContexts(params: {
7878
requestId,
7979
} = params
8080
const agentContexts: ChatContextItem[] = []
81-
const authorizedResources: z.infer<typeof mothershipResourceSchema>[] = []
81+
/** Indexed by attachment so concurrent resolution persists tabs in the order the user has them. */
82+
const authorizedResources: (z.infer<typeof mothershipResourceSchema> | undefined)[] = []
8283
if (
8384
Array.isArray(resourceAttachments) &&
8485
resourceAttachments.length > 0 &&
8586
(workspaceId || organizationId)
8687
) {
8788
const results = await Promise.allSettled(
88-
resourceAttachments.map(async (resource) => {
89+
resourceAttachments.map(async (resource, index) => {
8990
if (resource.type === 'sources') return null
9091
// The live browser panel resolves from the attachment itself: its
9192
// page state is client-held (the desktop app's embedded browser),
@@ -110,9 +111,10 @@ export async function resolveResourceAttachmentContexts(params: {
110111
)
111112
return null
112113
if (persistResources)
113-
authorizedResources.push(
114-
mothershipResourceSchema.parse({ ...resource, title: 'Search results' })
115-
)
114+
authorizedResources[index] = mothershipResourceSchema.parse({
115+
...resource,
116+
title: 'Search results',
117+
})
116118
return {
117119
type: 'active_resource',
118120
tag: resource.active ? '@active_tab' : '@open_tab',
@@ -127,7 +129,7 @@ export async function resolveResourceAttachmentContexts(params: {
127129
chatId,
128130
requestMode,
129131
})
130-
if (persistResources && tab) authorizedResources.push(tab)
132+
if (persistResources && tab) authorizedResources[index] = tab
131133
return context
132134
}
133135
const target =
@@ -161,15 +163,13 @@ export async function resolveResourceAttachmentContexts(params: {
161163
)
162164
if (!ctx) return null
163165
if (persistResources && isPersistableAttachment(resource))
164-
authorizedResources.push(
165-
mothershipResourceSchema.parse({
166-
...resource,
167-
title: resource.title ?? GENERIC_RESOURCE_TITLE[resource.type],
168-
...(organizationId
169-
? { workspaceId: target.workspaceId }
170-
: { workspaceId: undefined, workspaceName: undefined }),
171-
})
172-
)
166+
authorizedResources[index] = mothershipResourceSchema.parse({
167+
...resource,
168+
title: resource.title ?? GENERIC_RESOURCE_TITLE[resource.type],
169+
...(organizationId
170+
? { workspaceId: target.workspaceId }
171+
: { workspaceId: undefined, workspaceName: undefined }),
172+
})
173173
return {
174174
...ctx,
175175
...(organizationId
@@ -203,7 +203,8 @@ export async function resolveResourceAttachmentContexts(params: {
203203
})
204204
}
205205

206-
if (chatId && authorizedResources.length)
207-
await persistChatResources(chatId, sanitizeChatResources(authorizedResources))
206+
const orderedResources = authorizedResources.filter((resource) => resource !== undefined)
207+
if (chatId && orderedResources.length)
208+
await persistChatResources(chatId, sanitizeChatResources(orderedResources))
208209
return agentContexts
209210
}

0 commit comments

Comments
 (0)