Skip to content

Commit 7f05fb7

Browse files
author
Theodore Li
committed
Fix persisted writing file tab
1 parent e35f0ec commit 7f05fb7

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ export async function POST(req: NextRequest) {
5454
const body = await req.json()
5555
const { chatId, resource } = AddResourceSchema.parse(body)
5656

57+
// Ephemeral UI tab (client does not POST this; guard for old clients / bugs).
58+
if (resource.id === 'streaming-file') {
59+
return NextResponse.json({ success: true })
60+
}
61+
5762
if (!VALID_RESOURCE_TYPES.has(resource.type)) {
5863
return createBadRequestResponse(`Invalid resource type: ${resource.type}`)
5964
}

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,11 @@ export function useChat(
351351
})
352352
setActiveResourceId(resource.id)
353353

354+
// Ephemeral UI tab — must not be stored; it would reappear as "Writing file..." after refresh.
355+
if (resource.id === 'streaming-file') {
356+
return true
357+
}
358+
354359
const persistChatId = chatIdRef.current ?? selectedChatIdRef.current
355360
if (persistChatId) {
356361
fetch('/api/copilot/chat/resources', {
@@ -442,14 +447,30 @@ export function useChat(
442447
const mappedMessages = chatHistory.messages.map(mapStoredMessage)
443448
setMessages(mappedMessages)
444449

445-
if (chatHistory.resources.length > 0) {
446-
setResources(chatHistory.resources)
447-
setActiveResourceId(chatHistory.resources[chatHistory.resources.length - 1].id)
450+
if (chatHistory.resources.some((r) => r.id === 'streaming-file')) {
451+
fetch('/api/copilot/chat/resources', {
452+
method: 'DELETE',
453+
headers: { 'Content-Type': 'application/json' },
454+
body: JSON.stringify({
455+
chatId: chatHistory.id,
456+
resourceType: 'file',
457+
resourceId: 'streaming-file',
458+
}),
459+
}).catch(() => {})
460+
}
461+
462+
const persistedResources = chatHistory.resources.filter((r) => r.id !== 'streaming-file')
463+
if (persistedResources.length > 0) {
464+
setResources(persistedResources)
465+
setActiveResourceId(persistedResources[persistedResources.length - 1].id)
448466

449-
for (const resource of chatHistory.resources) {
467+
for (const resource of persistedResources) {
450468
if (resource.type !== 'workflow') continue
451469
ensureWorkflowInRegistry(resource.id, resource.title, workspaceId)
452470
}
471+
} else if (chatHistory.resources.some((r) => r.id === 'streaming-file')) {
472+
setResources([])
473+
setActiveResourceId(null)
453474
}
454475

455476
if (activeStreamId && !sendingRef.current) {

apps/sim/lib/copilot/resources.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ export async function persistChatResources(
2727
chatId: string,
2828
newResources: ChatResource[]
2929
): Promise<void> {
30-
if (newResources.length === 0) return
30+
const toMerge = newResources.filter((r) => r.id !== 'streaming-file')
31+
if (toMerge.length === 0) return
3132

3233
try {
3334
const [chat] = await db
@@ -46,7 +47,7 @@ export async function persistChatResources(
4647
map.set(`${r.type}:${r.id}`, r)
4748
}
4849

49-
for (const r of newResources) {
50+
for (const r of toMerge) {
5051
const key = `${r.type}:${r.id}`
5152
const prev = map.get(key)
5253
if (!prev || (GENERIC.has(prev.title) && !GENERIC.has(r.title))) {

0 commit comments

Comments
 (0)