Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions apps/sim/app/api/knowledge/[id]/connectors/[connectorId]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ export async function PATCH(request: NextRequest, { params }: RouteParams) {
}
if (parsed.data.status !== undefined) {
updates.status = parsed.data.status
if (parsed.data.status === 'active') {
updates.consecutiveFailures = 0
updates.lastSyncError = null
if (!updates.nextSyncAt) {
const interval = parsed.data.syncIntervalMinutes
if (interval && interval > 0) {
updates.nextSyncAt = new Date(Date.now() + interval * 60 * 1000)
Comment thread
waleedlatif1 marked this conversation as resolved.
Outdated
} else {
updates.nextSyncAt = new Date()
}
Comment thread
waleedlatif1 marked this conversation as resolved.
Outdated
}
}
Comment thread
waleedlatif1 marked this conversation as resolved.
Outdated
}

await db
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { createLogger } from '@sim/logger'
import { format, formatDistanceToNow, isPast } from 'date-fns'
import {
AlertCircle,
AlertTriangle,
CheckCircle2,
ChevronDown,
Loader2,
Expand Down Expand Up @@ -66,6 +67,7 @@ const STATUS_CONFIG = {
syncing: { label: 'Syncing', variant: 'amber' as const },
error: { label: 'Error', variant: 'red' as const },
paused: { label: 'Paused', variant: 'gray' as const },
disabled: { label: 'Disabled', variant: 'amber' as const },
} as const

export function ConnectorsSection({
Expand Down Expand Up @@ -159,7 +161,10 @@ export function ConnectorsSection({
knowledgeBaseId,
connectorId: connector.id,
updates: {
status: connector.status === 'paused' ? 'active' : 'paused',
status:
connector.status === 'paused' || connector.status === 'disabled'
? 'active'
: 'paused',
},
},
{
Expand Down Expand Up @@ -352,7 +357,12 @@ function ConnectorCard({
<div className='rounded-lg border border-[var(--border-1)]'>
<div className='flex items-center justify-between px-3 py-2.5'>
<div className='flex items-center gap-2.5'>
{Icon && <Icon className='h-5 w-5 flex-shrink-0' />}
<div className='relative flex-shrink-0'>
{Icon && <Icon className='h-5 w-5' />}
{connector.status === 'disabled' && (
<AlertTriangle className='-right-1 -top-1 absolute h-3 w-3 text-amber-500' />
)}
</div>
<div className='flex flex-col gap-0.5'>
<div className='flex items-center gap-2'>
<span className='flex items-center gap-1.5 font-medium text-[var(--text-primary)] text-small'>
Expand Down Expand Up @@ -407,7 +417,12 @@ function ConnectorCard({
variant='ghost'
className='h-7 w-7 p-0'
onClick={onSync}
disabled={connector.status === 'syncing' || isSyncPending || syncCooldown}
disabled={
connector.status === 'syncing' ||
connector.status === 'disabled' ||
isSyncPending ||
syncCooldown
}
>
<RefreshCw
className={cn(
Expand Down Expand Up @@ -441,15 +456,17 @@ function ConnectorCard({
>
{isUpdating ? (
<Loader2 className='h-3.5 w-3.5 animate-spin' />
) : connector.status === 'paused' ? (
) : connector.status === 'paused' || connector.status === 'disabled' ? (
<Play className='h-3.5 w-3.5' />
) : (
<Pause className='h-3.5 w-3.5' />
)}
</Button>
</Tooltip.Trigger>
<Tooltip.Content>
{connector.status === 'paused' ? 'Resume' : 'Pause'}
{connector.status === 'paused' || connector.status === 'disabled'
? 'Resume'
: 'Pause'}
</Tooltip.Content>
</Tooltip.Root>

Expand Down Expand Up @@ -481,7 +498,44 @@ function ConnectorCard({
</div>
</div>

{missingScopes.length > 0 && (
{connector.status === 'disabled' && (
<div className='border-[var(--border-1)] border-t px-3 py-2'>
<div className='flex flex-col gap-1 rounded-sm border border-amber-200 bg-amber-50 px-2 py-1.5 dark:border-amber-900 dark:bg-amber-950'>
<div className='flex items-center gap-1.5 font-medium text-amber-800 text-caption dark:text-amber-200'>
<AlertTriangle className='h-3 w-3 flex-shrink-0' />
Connector disabled after repeated sync failures
</div>
<p className='text-amber-700 text-micro dark:text-amber-300'>
Syncing has been paused due to {connector.consecutiveFailures} consecutive failures.
Reconnect your account to resume syncing.
</p>
{canEdit && (
<Button
variant='active'
onClick={() => {
if (connector.credentialId) {
writeOAuthReturnContext({
origin: 'kb-connectors',
knowledgeBaseId,
displayName: connectorDef?.name ?? connector.connectorType,
providerId: providerId!,
preCount: credentials?.length ?? 0,
workspaceId,
requestedAt: Date.now(),
})
}
setShowOAuthModal(true)
}}
className='w-full px-2 py-1 font-medium text-caption'
>
Reconnect
</Button>
)}
</div>
</div>
)}
Comment thread
waleedlatif1 marked this conversation as resolved.
Comment thread
waleedlatif1 marked this conversation as resolved.

{missingScopes.length > 0 && connector.status !== 'disabled' && (
<div className='border-[var(--border-1)] border-t px-3 py-2'>
<div className='flex flex-col gap-1 rounded-sm border bg-[var(--surface-2)] px-2 py-1.5'>
<div className='flex items-center font-medium text-caption'>
Expand Down
2 changes: 1 addition & 1 deletion apps/sim/hooks/queries/kb/connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ConnectorData {
sourceConfig: Record<string, unknown>
syncMode: string
syncIntervalMinutes: number
status: 'active' | 'paused' | 'syncing' | 'error'
status: 'active' | 'paused' | 'syncing' | 'error' | 'disabled'
lastSyncAt: string | null
lastSyncError: string | null
lastSyncDocCount: number | null
Expand Down
44 changes: 31 additions & 13 deletions apps/sim/lib/knowledge/connectors/sync-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const MAX_PAGES = 500
const MAX_SAFE_TITLE_LENGTH = 200
const STALE_PROCESSING_MINUTES = 45
const RETRY_WINDOW_DAYS = 7
const MAX_CONSECUTIVE_FAILURES = 10

/** Sanitizes a document title for use in S3 storage keys. */
function sanitizeStorageTitle(title: string): string {
Expand Down Expand Up @@ -230,7 +231,7 @@ async function resolveAccessToken(
connector: { credentialId: string | null; encryptedApiKey: string | null },
connectorConfig: { auth: ConnectorAuthConfig },
userId: string
): Promise<string | null> {
): Promise<string> {
if (connectorConfig.auth.mode === 'apiKey') {
if (!connector.encryptedApiKey) {
throw new Error('API key connector is missing encrypted API key')
Expand All @@ -243,11 +244,22 @@ async function resolveAccessToken(
throw new Error('OAuth connector is missing credential ID')
}

return refreshAccessTokenIfNeeded(
connector.credentialId,
userId,
`sync-${connector.credentialId}`
)
const requestId = `sync-${connector.credentialId}`
const token = await refreshAccessTokenIfNeeded(connector.credentialId, userId, requestId)

if (!token) {
logger.error(`[${requestId}] refreshAccessTokenIfNeeded returned null`, {
credentialId: connector.credentialId,
userId,
authMode: connectorConfig.auth.mode,
authProvider: connectorConfig.auth.provider,
})
throw new Error(
`Failed to obtain access token for credential ${connector.credentialId} (provider: ${connectorConfig.auth.provider})`
)
}

return token
}

/**
Expand Down Expand Up @@ -307,10 +319,6 @@ export async function executeSync(

let accessToken = await resolveAccessToken(connector, connectorConfig, userId)

if (!accessToken) {
throw new Error('Failed to obtain access token')
}

Comment thread
waleedlatif1 marked this conversation as resolved.
const lockResult = await db
.update(knowledgeConnector)
.set({ status: 'syncing', updatedAt: new Date() })
Expand Down Expand Up @@ -789,15 +797,25 @@ export async function executeSync(

const now = new Date()
const failures = (connector.consecutiveFailures ?? 0) + 1
const disabled = failures >= MAX_CONSECUTIVE_FAILURES
const backoffMinutes = Math.min(failures * 30, 1440)
const nextSync = new Date(now.getTime() + backoffMinutes * 60 * 1000)
const nextSync = disabled ? null : new Date(now.getTime() + backoffMinutes * 60 * 1000)

if (disabled) {
logger.warn('Connector disabled after repeated failures', {
connectorId,
consecutiveFailures: failures,
})
}

await db
.update(knowledgeConnector)
.set({
status: 'error',
status: disabled ? 'disabled' : 'error',
lastSyncAt: now,
lastSyncError: errorMessage,
lastSyncError: disabled
? 'Connector disabled after repeated sync failures. Please reconnect.'
: errorMessage,
nextSyncAt: nextSync,
consecutiveFailures: failures,
updatedAt: now,
Expand Down
Loading