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
Expand Up @@ -182,7 +182,7 @@ Create a workflow with a Schedule trigger set to every minute, deploy it, and wa
| Table row TTL cleanup | `/api/cron/cleanup-table-row-ttl` | `*/15 * * * *` | Deletes table rows whose TTL column has expired |
| Stale execution cleanup | `/api/cron/cleanup-stale-executions` | `*/30 * * * *` | Fails runs whose worker died without finishing — workflow executions, background jobs, table jobs and table runs — so they stop showing as running |
| OAuth token cleanup | `/api/cron/cleanup-oauth-tokens` | `0 * * * *` | Deletes access and refresh tokens after the retention tail |
| Activity history cleanup | `/api/cron/cleanup-activity-history` | `45 4 * * *` | Deletes organization adoption history past its retention: daily member activity and Search MCP call metadata after 400 days |
| Activity history cleanup | `/api/cron/cleanup-activity-history` | `45 4 * * *` | Deletes organization adoption history past its retention: daily member activity, daily Search integration counts and Search MCP call metadata after 400 days |
| Workspace source staging cleanup | `/api/cron/cleanup-source-staging` | `30 * * * *` | Deletes binary files staged for a workspace source apply a day after they were last staged |
| SCIM reconciliation | `/api/cron/scim-reconcile` | `17 * * * *` | Re-applies directory group mappings |
| Data drains | `/api/cron/run-data-drains` | `*/15 * * * *` | Enterprise data drains; each drain runs only when its own cadence is due |
Expand Down
1 change: 1 addition & 0 deletions apps/sim/app/api/knowledge/search/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export const POST = defineInternalJsonRoute({
...body,
signal: request.signal,
surface: 'dashboard' as const,
activitySurface: 'search' as const,
}),
useCase: searchLiveKnowledge,
present: (data) => ({ success: true as const, data }),
Expand Down
22 changes: 22 additions & 0 deletions apps/sim/app/api/organizations/[id]/search/opens/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { recordSearchOpenContract } from '@/lib/api/contracts/knowledge/search-activity'
import {
defineInternalJsonRoute,
internalOrchestrationErrorPolicy,
internalRateLimits,
internalSessionAuth,
} from '@/lib/api/server/routes'
import {
recordSearchOpen,
searchActivityOperations,
} from '@/lib/knowledge/application/search-activity'

export const POST = defineInternalJsonRoute({
contract: recordSearchOpenContract,
auth: internalSessionAuth,
operation: searchActivityOperations.recordOpen,
rateLimit: internalRateLimits.user({ bucketName: 'search-opens-write' }),
errorPolicy: internalOrchestrationErrorPolicy,
mapInput: ({ params, body }) => ({ organizationId: params.id, ...body }),
useCase: recordSearchOpen,
staticResponseHeaders: { 'Cache-Control': 'private, no-store' },
})
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import {
} from '@/app/o/[organizationId]/search/search-params'
import { ChatResourcePanel } from '@/app/workspace/[workspaceId]/home/components/chat-resource-panel'
import { ComposerScrollArea } from '@/app/workspace/[workspaceId]/home/components/composer-scroll-area'
import { useSearchHistoryActions } from '@/app/workspace/[workspaceId]/home/components/message-content/components/source-history-context'
import {
SourceOpenSurfaceProvider,
useSearchHistoryActions,
} from '@/app/workspace/[workspaceId]/home/components/message-content/components/source-history-context'
import { SearchIntegrationConnection } from '@/app/workspace/[workspaceId]/home/components/message-content/components/special-tags/search-integration-connection'
import { MothershipChat } from '@/app/workspace/[workspaceId]/home/components/mothership-chat'
import { SourcesResourceContent } from '@/app/workspace/[workspaceId]/home/components/mothership-view/components/resource-content/components/sources-resource-content'
Expand Down Expand Up @@ -120,10 +123,12 @@ export function OrganizationConversation(props: OrganizationConversationProps) {
if (isRestoredChatEntry || !isClient || !session?.user?.id)
return <HomeFallback header={props.header} />
return (
<OrganizationConversationContent
key={`${session.user.id}:${organization.id}:${props.chatId ?? 'new'}:${props.panelOptions ? 'panel' : props.requestMode}`}
{...props}
/>
<SourceOpenSurfaceProvider surface={props.requestMode === 'assistant' ? 'assistant' : 'agent'}>
<OrganizationConversationContent
key={`${session.user.id}:${organization.id}:${props.chatId ?? 'new'}:${props.panelOptions ? 'panel' : props.requestMode}`}
{...props}
/>
</SourceOpenSurfaceProvider>
)
}

Expand Down
76 changes: 42 additions & 34 deletions apps/sim/app/o/[organizationId]/search/search-results-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { OrganizationLanding } from '@/app/o/[organizationId]/components/organiz
import { PAGE_COLUMN_CLASS } from '@/app/o/[organizationId]/components/organization-page'
import { useOrganizationContext } from '@/app/o/[organizationId]/providers/organization-provider'
import { KnowledgeSearchResults } from '@/app/workspace/[workspaceId]/home/components/knowledge-search-results'
import { SourceOpenSurfaceProvider } from '@/app/workspace/[workspaceId]/home/components/message-content/components/source-history-context'
import {
SIDEBAR_DIVIDER_PAD_ABOVE_CLASS,
SIDEBAR_DIVIDER_PAD_BELOW_CLASS,
Expand All @@ -24,7 +25,10 @@ interface SearchResultsViewProps {
onSearchChange?: (search: SearchResource) => void
}

/** Standalone Search uses the original centered field and docked results layout. */
/**
* Standalone Search uses the original centered field and docked results layout. Its results and
* the recently viewed sources under the field both count opens toward the `search` surface.
*/
export function SearchResultsView({
composer,
query,
Expand All @@ -41,40 +45,44 @@ export function SearchResultsView({
enabled: searching,
})
return (
<div className='flex h-full min-h-0 flex-col bg-[var(--bg)]'>
{searching ? (
<>
<div className={cn(PAGE_COLUMN_CLASS, SIDEBAR_DIVIDER_PAD_ABOVE_CLASS, 'shrink-0 pt-8')}>
{composer}
</div>
<div
ref={scrollContainerRef}
className={cn(
SIDEBAR_DIVIDER_PAD_BELOW_CLASS,
SIDEBAR_DIVIDER_PAD_ABOVE_CLASS,
scrollFadeClass,
overflowFadeSizeClass,
'min-h-0 flex-1 overflow-y-auto [scrollbar-gutter:stable_both-edges]'
)}
{...scrollFadeAttributes(scrollEdges)}
>
{/* The rows carry their own `px-2`; this gutter brings each row's mark under the
<SourceOpenSurfaceProvider surface='search'>
<div className='flex h-full min-h-0 flex-col bg-[var(--bg)]'>
{searching ? (
<>
<div
className={cn(PAGE_COLUMN_CLASS, SIDEBAR_DIVIDER_PAD_ABOVE_CLASS, 'shrink-0 pt-8')}
>
{composer}
</div>
<div
ref={scrollContainerRef}
className={cn(
SIDEBAR_DIVIDER_PAD_BELOW_CLASS,
SIDEBAR_DIVIDER_PAD_ABOVE_CLASS,
scrollFadeClass,
overflowFadeSizeClass,
'min-h-0 flex-1 overflow-y-auto [scrollbar-gutter:stable_both-edges]'
)}
{...scrollFadeAttributes(scrollEdges)}
>
{/* The rows carry their own `px-2`; this gutter brings each row's mark under the
field's own search glyph, so results read as a column hanging from the field. */}
<div ref={scrollContentRef} className={cn(PAGE_COLUMN_CLASS, 'px-8')}>
<KnowledgeSearchResults
scope={scope}
query={query}
onSummarize={onSummarize}
onSearchChange={onSearchChange}
/>
<div ref={scrollContentRef} className={cn(PAGE_COLUMN_CLASS, 'px-8')}>
<KnowledgeSearchResults
scope={scope}
query={query}
onSummarize={onSummarize}
onSearchChange={onSearchChange}
/>
</div>
</div>
</div>
</>
) : (
<OrganizationLanding heading={`Search ${organization.name}`}>
{composer}
</OrganizationLanding>
)}
</div>
</>
) : (
<OrganizationLanding heading={`Search ${organization.name}`}>
{composer}
</OrganizationLanding>
)}
</div>
</SourceOpenSurfaceProvider>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,8 @@ import {
sourceLabel,
sourceSiteName,
} from '@/app/workspace/[workspaceId]/home/components/message-content/components/source-chip'
import {
externalLinkHostname,
handleExternalLinkClick,
} from '@/app/workspace/[workspaceId]/home/components/message-content/components/source-link'
import { useSourceNavigation } from '@/app/workspace/[workspaceId]/home/components/message-content/components/source-history-context'
import { externalLinkHostname } from '@/app/workspace/[workspaceId]/home/components/message-content/components/source-link'
import type { SourceTagData } from '@/app/workspace/[workspaceId]/home/components/message-content/components/special-tags'

interface SearchActivityResultsProps {
Expand Down Expand Up @@ -49,39 +47,50 @@ export function SearchActivityResults({ sources, label }: SearchActivityResultsP
{...scrollFadeAttributes(edges)}
>
<ul className='m-0 list-none p-0'>
{sources.map((source) => {
const hostname = externalLinkHostname(source.url)
return (
<li key={source.url}>
<a
href={source.url}
target='_blank'
rel='noopener noreferrer'
onClick={(event) => handleExternalLinkClick(event, source.url)}
className={cn(
'not-prose flex h-8 min-w-0 items-center gap-2 px-2 text-[var(--text-body)] text-small no-underline transition-colors focus-visible:bg-[var(--surface-hover)] focus-visible:outline-none',
chipHoverSurfaceClass,
chipRadiusClass
)}
>
<span className={chipIconSlotClass}>
<SourceIcon source={source} />
</span>
<OverflowText
label={sourceLabel(source)}
focusTarget='nearest-interactive'
className='flex-1'
/>
<OverflowText
label={hostname?.replace(/^www\./, '') ?? sourceSiteName(source)}
className='max-w-[35%] text-[var(--text-muted)] text-caption'
/>
</a>
</li>
)
})}
{sources.map((source) => (
<li key={source.url}>
<SearchActivityResultLink source={source} />
</li>
))}
</ul>
</div>
</div>
)
}

interface SearchActivityResultLinkProps {
source: SourceTagData
}

/** Opens through the shared source-open boundary, so a result opened here is counted like any other. */
function SearchActivityResultLink({ source }: SearchActivityResultLinkProps) {
const navigate = useSourceNavigation(source)
const hostname = externalLinkHostname(source.url)
return (
<a
href={source.url}
target='_blank'
rel='noopener noreferrer'
onClick={navigate}
onAuxClick={navigate}
className={cn(
'not-prose flex h-8 min-w-0 items-center gap-2 px-2 text-[var(--text-body)] text-small no-underline transition-colors focus-visible:bg-[var(--surface-hover)] focus-visible:outline-none',
chipHoverSurfaceClass,
chipRadiusClass
)}
>
<span className={chipIconSlotClass}>
<SourceIcon source={source} />
</span>
<OverflowText
label={sourceLabel(source)}
focusTarget='nearest-interactive'
className='flex-1'
/>
<OverflowText
label={hostname?.replace(/^www\./, '') ?? sourceSiteName(source)}
className='max-w-[35%] text-[var(--text-muted)] text-caption'
/>
</a>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@ import {
} from 'react'
import { noop } from '@sim/utils/helpers'
import { truncateAtCodePoint } from '@sim/utils/string'
import type { SearchOpenSurface } from '@/lib/api/contracts/knowledge/search-activity'
import type { ViewedSearchSource } from '@/lib/api/contracts/knowledge/search-history'
import { isKnowledgeSourceUrl } from '@/lib/knowledge/search/source-url'
import { isLiveSearchProviderId } from '@/lib/sim-search/live/provider-catalog'
import { handleExternalLinkClick } from '@/app/workspace/[workspaceId]/home/components/message-content/components/source-link'
import type { SourceTagData } from '@/app/workspace/[workspaceId]/home/components/message-content/components/special-tags'
import { useRecordSearchOpen } from '@/hooks/queries/search-activity'
import { useRecordSearchHistory } from '@/hooks/queries/search-history'

interface SourceHistoryContextValue {
recordSource: (source: ViewedSearchSource) => void
recordQuery: (query: string) => void
recordOpen: (connectorType: string | undefined, surface: SearchOpenSurface | undefined) => void
}
const SourceHistoryContext = createContext<SourceHistoryContextValue>({
recordSource: noop,
recordQuery: noop,
recordOpen: noop,
})
/** Where the sources below it are opened from; none outside an organization's Search surfaces. */
const SourceOpenSurfaceContext = createContext<SearchOpenSurface | undefined>(undefined)

interface SourceHistoryProviderProps {
organizationId?: string
Expand All @@ -38,6 +45,7 @@ export function SourceHistoryProvider({
children,
}: SourceHistoryProviderProps) {
const { mutate } = useRecordSearchHistory(organizationId, userId)
const { mutate: countOpen } = useRecordSearchOpen()
const recordSource = useCallback(
(source: ViewedSearchSource) => {
if (
Expand All @@ -58,20 +66,46 @@ export function SourceHistoryProvider({
},
[organizationId, userId, mutate]
)
const value = useMemo(() => ({ recordSource, recordQuery }), [recordSource, recordQuery])
const recordOpen = useCallback(
(connectorType: string | undefined, surface: SearchOpenSurface | undefined) => {
if (organizationId && userId && surface && isLiveSearchProviderId(connectorType))
countOpen({ organizationId, connectorType, surface })
},
[organizationId, userId, countOpen]
)
const value = useMemo(
() => ({ recordSource, recordQuery, recordOpen }),
[recordSource, recordQuery, recordOpen]
)
return <SourceHistoryContext.Provider value={value}>{children}</SourceHistoryContext.Provider>
}

interface SourceOpenSurfaceProviderProps {
surface: SearchOpenSurface
children: ReactNode
}

/** Names the Search surface the sources below it are opened from, so each open counts there. */
export function SourceOpenSurfaceProvider({ surface, children }: SourceOpenSurfaceProviderProps) {
return (
<SourceOpenSurfaceContext.Provider value={surface}>
{children}
</SourceOpenSurfaceContext.Provider>
)
}

export function useSearchHistoryActions() {
return useContext(SourceHistoryContext)
}

/** Native, keyboard and middle-click navigation share the same source-open boundary. */
export function useSourceNavigation(source: SourceTagData) {
const { recordSource } = useSearchHistoryActions()
const { recordSource, recordOpen } = useSearchHistoryActions()
const surface = useContext(SourceOpenSurfaceContext)
return useCallback(
(event: MouseEvent<HTMLAnchorElement>) => {
if (event.defaultPrevented || (event.button !== 0 && event.button !== 1)) return
recordOpen(source.connectorType, surface)
Comment thread
mzxchandra marked this conversation as resolved.
Resolved
recordSource({
url: source.url,
title: source.title?.trim() ? truncateAtCodePoint(source.title.trim(), 512, '') : undefined,
Expand All @@ -84,6 +118,14 @@ export function useSourceNavigation(source: SourceTagData) {
})
handleExternalLinkClick(event, source.url)
},
[source.url, source.title, source.siteName, source.connectorType, recordSource]
[
source.url,
source.title,
source.siteName,
source.connectorType,
surface,
recordSource,
recordOpen,
]
)
}
21 changes: 21 additions & 0 deletions apps/sim/hooks/queries/search-activity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useMutation } from '@tanstack/react-query'
import { requestJson } from '@/lib/api/client/request'
import {
type RecordSearchOpenBody,
recordSearchOpenContract,
} from '@/lib/api/contracts/knowledge/search-activity'

interface RecordSearchOpenVariables extends RecordSearchOpenBody {
organizationId: string
}

/**
* Counts an opened Search result toward its integration. No client query reads these counts, so
* there is nothing to invalidate.
*/
export function useRecordSearchOpen() {
return useMutation({
mutationFn: ({ organizationId, ...body }: RecordSearchOpenVariables) =>
requestJson(recordSearchOpenContract, { params: { id: organizationId }, body }),
})
}
Loading
Loading