diff --git a/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-content/resource-content.tsx b/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-content/resource-content.tsx
index e793991957..1adfb0f344 100644
--- a/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-content/resource-content.tsx
+++ b/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-content/resource-content.tsx
@@ -32,6 +32,7 @@ import {
RESOURCE_TAB_ICON_BUTTON_CLASS,
RESOURCE_TAB_ICON_CLASS,
} from '@/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-tabs/resource-tab-controls'
+import { hasRenderableFilePreviewContent } from '@/app/workspace/[workspaceId]/home/hooks/use-file-preview-sessions'
import type {
GenericResourceData,
MothershipResource,
@@ -116,13 +117,19 @@ export const ResourceContent = memo(function ResourceContent({
const disableStreamingAutoScroll = previewSession?.operation === 'patch'
const rawPreviewText = previewSession?.previewText
const streamingPreviewText =
- typeof rawPreviewText === 'string' && rawPreviewText.length > 0 ? rawPreviewText : undefined
+ previewSession &&
+ typeof rawPreviewText === 'string' &&
+ hasRenderableFilePreviewContent(previewSession)
+ ? rawPreviewText
+ : undefined
const pendingOrStreamingFilePreviewText =
- previewSession?.fileId === resource.id && typeof rawPreviewText === 'string'
+ previewSession?.fileId === resource.id &&
+ typeof rawPreviewText === 'string' &&
+ hasRenderableFilePreviewContent(previewSession)
? rawPreviewText
: undefined
- if (previewSession && resource.id === 'streaming-file') {
+ if (resource.id === 'streaming-file') {
return (
{streamingPreviewText !== undefined ? (
diff --git a/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/mothership-view.tsx b/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/mothership-view.tsx
index 4eb7227c85..be06ee8481 100644
--- a/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/mothership-view.tsx
+++ b/apps/sim/app/workspace/[workspaceId]/home/components/mothership-view/mothership-view.tsx
@@ -6,6 +6,7 @@ import { cn } from '@/lib/core/utils/cn'
import { getFileExtension } from '@/lib/uploads/utils/file-utils'
import type { PreviewMode } from '@/app/workspace/[workspaceId]/files/components/file-viewer'
import { RICH_PREVIEWABLE_EXTENSIONS } from '@/app/workspace/[workspaceId]/files/components/file-viewer'
+import { hasRenderableFilePreviewContent } from '@/app/workspace/[workspaceId]/home/hooks/use-file-preview-sessions'
import type {
GenericResourceData,
MothershipResource,
@@ -23,7 +24,7 @@ const PREVIEW_CYCLE: Record
= {
/**
* Whether the active resource should show the in-progress file stream.
* The synthetic `streaming-file` tab always shows it; a real file tab only shows it
- * when the streamed fileId matches that exact resource.
+ * after a preview content event has arrived for that exact resource.
*/
function shouldShowStreamingFilePanel(
previewSession: FilePreviewSession | null | undefined,
@@ -32,7 +33,9 @@ function shouldShowStreamingFilePanel(
if (!previewSession || previewSession.status === 'complete' || !active) return false
if (active.id === 'streaming-file') return true
if (active.type !== 'file') return false
- if (active.id && previewSession.fileId === active.id) return true
+ if (active.id && previewSession.fileId === active.id) {
+ return hasRenderableFilePreviewContent(previewSession)
+ }
return false
}
diff --git a/apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts b/apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts
index 0321517211..8e6d5bc49d 100644
--- a/apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts
+++ b/apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts
@@ -104,8 +104,10 @@ import { invalidateResourceQueries } from '@/app/workspace/[workspaceId]/home/co
import {
buildCompletedPreviewSessions,
type FilePreviewSessionsState,
+ hasRenderableFilePreviewContent,
INITIAL_FILE_PREVIEW_SESSIONS_STATE,
reduceFilePreviewSessions,
+ shouldReplaceSession,
useFilePreviewSessions,
} from '@/app/workspace/[workspaceId]/home/hooks/use-file-preview-sessions'
import { deploymentKeys } from '@/hooks/queries/deployments'
@@ -1385,6 +1387,52 @@ export function useChat(
const activeResourceIdRef = useRef(effectiveActiveResourceId)
activeResourceIdRef.current = effectiveActiveResourceId
+ const previewActivationOwnerRef = useRef