Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

173 lines
5.6 KiB
TypeScript

import { db } from '@sim/db'
import { knowledgeBase, knowledgeConnector } from '@sim/db/schema'
import { createLogger } from '@sim/logger'
import { toError } from '@sim/utils/errors'
import { generateId } from '@sim/utils/id'
import { isRecordLike } from '@sim/utils/object'
import { tasks } from '@trigger.dev/sdk'
import { eq } from 'drizzle-orm'
import {
assertBillingAttributionSnapshot,
type BillingAttributionSnapshot,
} from '@/lib/billing/core/billing-attribution'
import { resolveTriggerRegion } from '@/lib/core/async-jobs/region'
import { executeSync } from '@/lib/knowledge/connectors/sync-engine'
import { isTriggerAvailable } from '@/lib/knowledge/documents/service'
const logger = createLogger('ConnectorSyncQueue')
export interface ConnectorSyncPayload {
connectorId: string
fullSync?: boolean
/**
* Force re-hydration + re-indexing of already-synced documents for connectors
* whose rendered content can drift without a hash change (see
* `ConnectorMeta.rehydrateOnFullSync`). Forces a full (non-incremental) listing
* so every document is re-hydrated, but — unlike `fullSync` — keeps every
* deletion-reconciliation safety guard armed.
*/
rehydrate?: boolean
requestId: string
billingAttribution: BillingAttributionSnapshot
}
export interface DispatchSyncOptions {
billingAttribution: BillingAttributionSnapshot
fullSync?: boolean
rehydrate?: boolean
requestId?: string
}
function isNonEmptyString(value: unknown): value is string {
return typeof value === 'string' && value.trim().length > 0
}
/**
* Restores and validates connector work crossing the asynchronous boundary.
*/
export function assertConnectorSyncPayload(value: unknown): ConnectorSyncPayload {
if (!isRecordLike(value)) {
throw new Error('Connector sync payload must be an object')
}
if (!isNonEmptyString(value.connectorId) || !isNonEmptyString(value.requestId)) {
throw new Error('Connector sync payload requires connectorId and requestId')
}
if (value.fullSync !== undefined && typeof value.fullSync !== 'boolean') {
throw new Error('Connector sync payload fullSync must be a boolean when provided')
}
if (value.rehydrate !== undefined && typeof value.rehydrate !== 'boolean') {
throw new Error('Connector sync payload rehydrate must be a boolean when provided')
}
if (value.billingAttribution === undefined) {
throw new Error('Connector sync payload requires billing attribution')
}
return {
connectorId: value.connectorId,
fullSync: value.fullSync as boolean | undefined,
rehydrate: value.rehydrate as boolean | undefined,
requestId: value.requestId,
billingAttribution: assertBillingAttributionSnapshot(value.billingAttribution),
}
}
/**
* Dispatches a connector sync with billing attribution already fixed by the
* authenticated or scheduled entry point.
*/
export async function dispatchSync(
connectorId: string,
options: DispatchSyncOptions
): Promise<void> {
if (!isNonEmptyString(connectorId)) {
throw new Error('Connector sync dispatch requires a connector ID')
}
const requestId = options?.requestId ?? generateId()
const payload = assertConnectorSyncPayload({
connectorId,
fullSync: options?.fullSync,
rehydrate: options?.rehydrate,
requestId,
billingAttribution: options?.billingAttribution,
})
const connectorRows = await db
.select({
knowledgeBaseId: knowledgeConnector.knowledgeBaseId,
connectorArchivedAt: knowledgeConnector.archivedAt,
connectorDeletedAt: knowledgeConnector.deletedAt,
workspaceId: knowledgeBase.workspaceId,
kbDeletedAt: knowledgeBase.deletedAt,
})
.from(knowledgeConnector)
.innerJoin(knowledgeBase, eq(knowledgeBase.id, knowledgeConnector.knowledgeBaseId))
.where(eq(knowledgeConnector.id, connectorId))
.limit(1)
const row = connectorRows[0]
if (!row) {
logger.warn('Skipping sync dispatch: connector not found', { connectorId, requestId })
return
}
if (row.kbDeletedAt) {
logger.warn('Skipping sync dispatch: knowledge base is deleted', {
connectorId,
knowledgeBaseId: row.knowledgeBaseId,
requestId,
})
await db
.update(knowledgeConnector)
.set({
status: 'error',
nextSyncAt: null,
lastSyncError: 'Knowledge base deleted',
updatedAt: new Date(),
})
.where(eq(knowledgeConnector.id, connectorId))
return
}
if (row.connectorArchivedAt || row.connectorDeletedAt) {
logger.warn('Skipping sync dispatch: connector is archived or deleted', {
connectorId,
requestId,
})
return
}
if (!row.workspaceId) {
throw new Error(`Connector ${connectorId} is missing workspace billing context`)
}
if (payload.billingAttribution.workspaceId !== row.workspaceId) {
throw new Error(
`Connector sync billing attribution does not match connector workspace ${row.workspaceId}`
)
}
const tags = [
`connectorId:${connectorId}`,
`knowledgeBaseId:${row.knowledgeBaseId}`,
`workspaceId:${row.workspaceId}`,
`userId:${payload.billingAttribution.actorUserId}`,
]
if (isTriggerAvailable()) {
await tasks.trigger('knowledge-connector-sync', payload, {
tags,
region: await resolveTriggerRegion(),
})
logger.info('Dispatched connector sync to Trigger.dev', { connectorId, requestId })
return
}
executeSync(connectorId, {
fullSync: payload.fullSync,
rehydrate: payload.rehydrate,
billingAttribution: payload.billingAttribution,
}).catch((error) => {
logger.error(`Sync failed for connector ${connectorId}`, {
error: toError(error).message,
requestId,
})
})
}