Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client'

import { lazy, memo, Suspense, useEffect, useMemo } from 'react'
import { lazy, memo, Suspense, useEffect, useMemo, useRef } from 'react'
import { createLogger } from '@sim/logger'
import { Square } from 'lucide-react'
import { useRouter } from 'next/navigation'
Expand All @@ -13,6 +13,7 @@ import {
SquareArrowUpRight,
WorkflowX,
} from '@/components/emcn/icons'
import { isApiClientError } from '@/lib/api/client/errors'
import type { FilePreviewSession } from '@/lib/copilot/request/session'
import {
cancelRunToolExecution,
Expand Down Expand Up @@ -70,6 +71,7 @@ interface ResourceContentProps {
previewSession?: FilePreviewSession | null
genericResourceData?: GenericResourceData
previewContextKey?: string
onNotFound?: (resourceId: string) => void
}

/**
Expand All @@ -86,6 +88,7 @@ export const ResourceContent = memo(function ResourceContent({
previewSession,
genericResourceData,
previewContextKey,
onNotFound,
}: ResourceContentProps) {
const streamFileName = previewSession?.fileName || 'file.md'
const syntheticFile = useMemo(() => {
Expand Down Expand Up @@ -179,7 +182,13 @@ export const ResourceContent = memo(function ResourceContent({
return <EmbeddedFolder key={resource.id} workspaceId={workspaceId} folderId={resource.id} />

case 'log':
return <EmbeddedLog key={resource.id} logId={resource.id} />
return (
<EmbeddedLog
key={resource.id}
logId={resource.id}
onNotFound={onNotFound ? () => onNotFound(resource.id) : undefined}
/>
)

case 'generic':
return (
Expand Down Expand Up @@ -618,10 +627,20 @@ function EmbeddedFolder({ workspaceId, folderId }: EmbeddedFolderProps) {

interface EmbeddedLogProps {
logId: string
onNotFound?: () => void
}

function EmbeddedLog({ logId }: EmbeddedLogProps) {
const { data: log, isLoading } = useLogDetail(logId)
function EmbeddedLog({ logId, onNotFound }: EmbeddedLogProps) {
const { data: log, isLoading, error } = useLogDetail(logId)
Comment thread
waleedlatif1 marked this conversation as resolved.

const onNotFoundRef = useRef(onNotFound)
onNotFoundRef.current = onNotFound

useEffect(() => {
if (isApiClientError(error) && error.status === 404) {
onNotFoundRef.current?.()
}
}, [error])

if (isLoading) return LOADING_SKELETON

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export const MothershipView = memo(
previewSession={previewForActive}
genericResourceData={active.type === 'generic' ? genericResourceData : undefined}
previewContextKey={chatId}
onNotFound={(resourceId) => onRemoveResource('log', resourceId)}
/>
) : (
<div className='flex h-full items-center justify-center text-[var(--text-muted)] text-sm'>
Expand Down
12 changes: 12 additions & 0 deletions apps/sim/app/workspace/[workspaceId]/home/hooks/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1420,6 +1420,18 @@ export function useChat(
const removeResource = useCallback((resourceType: MothershipResourceType, resourceId: string) => {
setResources((prev) => prev.filter((r) => !(r.type === resourceType && r.id === resourceId)))
setActiveResourceId((prev) => (prev === resourceId ? null : prev))

const persistChatId = chatIdRef.current ?? selectedChatIdRef.current
if (persistChatId) {
// boundary-raw-fetch: fire-and-forget side-effect; intentionally avoids requestJson's response parsing/throw semantics so a transient failure cannot interrupt the caller
fetch('/api/mothership/chat/resources', {
method: 'DELETE',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ chatId: persistChatId, resourceType, resourceId }),
}).catch((err) => {
logger.warn('Failed to persist resource removal', err)
})
}
}, [])

const reorderResources = useCallback((newOrder: MothershipResource[]) => {
Expand Down
3 changes: 3 additions & 0 deletions apps/sim/hooks/queries/logs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
useQuery,
useQueryClient,
} from '@tanstack/react-query'
import { isApiClientError } from '@/lib/api/client/errors'
import { requestJson } from '@/lib/api/client/request'
import {
cancelWorkflowExecutionContract,
Expand Down Expand Up @@ -194,6 +195,8 @@ export function useLogDetail(logId: string | undefined, options?: UseLogDetailOp
enabled: Boolean(logId) && (options?.enabled ?? true),
refetchInterval: options?.refetchInterval ?? false,
staleTime: 30 * 1000,
retry: (failureCount, err) =>
!(isApiClientError(err) && err.status === 404) && failureCount < 3,
})
}

Expand Down
15 changes: 0 additions & 15 deletions apps/sim/lib/copilot/resources/extraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
FunctionExecute,
GenerateImage,
GenerateVisualization,
GetWorkflowLogs,
Knowledge,
KnowledgeBase,
UserTable,
Expand All @@ -30,7 +29,6 @@ const RESOURCE_TOOL_NAMES: Set<string> = new Set([
Knowledge.id,
GenerateVisualization.id,
GenerateImage.id,
GetWorkflowLogs.id,
])

export function isResourceToolName(toolName: string): boolean {
Expand Down Expand Up @@ -214,19 +212,6 @@ export function extractResourcesFromToolResult(
return resources
}

case GetWorkflowLogs.id: {
const entries = Array.isArray(output) ? output : Array.isArray(result.data) ? result.data : []
const resources: ChatResource[] = []
for (const entry of entries) {
const rec = asRecord(entry)
const logId = rec.id as string | undefined
if (logId) {
resources.push({ type: 'log', id: logId, title: 'Log' })
}
}
return resources
}

default:
return []
}
Expand Down
Loading