|
| 1 | +import { createLogger } from '@sim/logger' |
| 2 | +import { getErrorMessage } from '@sim/utils/errors' |
| 3 | +import { secureFetchWithRetry } from '@/lib/knowledge/documents/secure-fetch.server' |
| 4 | +import { type RetryOptions, VALIDATE_RETRY_OPTIONS } from '@/lib/knowledge/documents/utils' |
| 5 | +import { planeConnectorMeta } from '@/connectors/plane/meta' |
| 6 | +import { |
| 7 | + type PlaneContentConfig, |
| 8 | + parsePlanePage, |
| 9 | + planePageDocument, |
| 10 | + planeWorkItemDocument, |
| 11 | +} from '@/connectors/plane/utils' |
| 12 | +import type { ConnectorConfig, ExternalDocument } from '@/connectors/types' |
| 13 | +import { joinTagArray, listingRequestError, parseTagDate } from '@/connectors/utils' |
| 14 | +import { planeApiUrl, planeHeaders, planeRedirectPolicy } from '@/tools/plane/utils' |
| 15 | +import { safeUrlPathSegment } from '@/tools/url-path' |
| 16 | + |
| 17 | +const logger = createLogger('PlaneConnector') |
| 18 | +const PAGE_SIZE = 100 |
| 19 | +const WORK_ITEM_FIELDS = |
| 20 | + 'id,name,sequence_id,description_html,description_stripped,priority,state,assignees,labels,updated_at' |
| 21 | +const PAGE_FIELDS = 'id,name,updated_at' |
| 22 | + |
| 23 | +interface PlaneSourceConfig extends PlaneContentConfig { |
| 24 | + baseUrl?: string |
| 25 | + contentType: 'work_items' | 'pages' |
| 26 | + maxDocuments: number |
| 27 | +} |
| 28 | + |
| 29 | +function readConfig(source: Record<string, unknown>): PlaneSourceConfig { |
| 30 | + const workspaceSlug = typeof source.workspaceSlug === 'string' ? source.workspaceSlug.trim() : '' |
| 31 | + const projectId = typeof source.projectId === 'string' ? source.projectId.trim() : '' |
| 32 | + if (!workspaceSlug || !projectId) throw new Error('Workspace slug and project ID are required') |
| 33 | + safeUrlPathSegment(workspaceSlug, 'workspaceSlug') |
| 34 | + safeUrlPathSegment(projectId, 'projectId') |
| 35 | + const contentType = source.contentType || 'work_items' |
| 36 | + if (contentType !== 'work_items' && contentType !== 'pages') |
| 37 | + throw new Error('Content must be work items or project pages') |
| 38 | + const maxInput = source.maxDocuments |
| 39 | + if ( |
| 40 | + maxInput !== undefined && |
| 41 | + maxInput !== null && |
| 42 | + typeof maxInput !== 'string' && |
| 43 | + typeof maxInput !== 'number' |
| 44 | + ) |
| 45 | + throw new Error('Max documents must be a positive integer') |
| 46 | + const maxDocuments = |
| 47 | + maxInput === undefined || maxInput === null || maxInput === '' ? 0 : Number(maxInput) |
| 48 | + if ( |
| 49 | + !Number.isSafeInteger(maxDocuments) || |
| 50 | + maxDocuments < 0 || |
| 51 | + (maxDocuments === 0 && maxInput !== undefined && maxInput !== null && maxInput !== '') |
| 52 | + ) |
| 53 | + throw new Error('Max documents must be a positive integer') |
| 54 | + const baseUrl = |
| 55 | + typeof source.baseUrl === 'string' && source.baseUrl.trim() ? source.baseUrl.trim() : undefined |
| 56 | + const apiOrigin = new URL(planeApiUrl(baseUrl, '/')) |
| 57 | + const defaultWebUrl = |
| 58 | + apiOrigin.origin === 'https://api.plane.so' |
| 59 | + ? 'https://app.plane.so' |
| 60 | + : `${apiOrigin.origin}${apiOrigin.pathname.replace(/\/+$/, '')}` |
| 61 | + const webUrl = |
| 62 | + typeof source.webUrl === 'string' && source.webUrl.trim() ? source.webUrl.trim() : defaultWebUrl |
| 63 | + const normalizedWeb = new URL(planeApiUrl(webUrl, '/')) |
| 64 | + return { |
| 65 | + workspaceSlug, |
| 66 | + projectId, |
| 67 | + contentType, |
| 68 | + maxDocuments, |
| 69 | + baseUrl, |
| 70 | + webUrl: `${normalizedWeb.origin}${normalizedWeb.pathname.replace(/\/+$/, '')}`, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +function resourcePath(config: PlaneSourceConfig, id?: string): string { |
| 75 | + const resource = config.contentType === 'pages' ? 'pages' : 'work-items' |
| 76 | + return `/api/v1/workspaces/${safeUrlPathSegment(config.workspaceSlug, 'workspaceSlug')}/projects/${safeUrlPathSegment(config.projectId, 'projectId')}/${resource}/${id ? `${safeUrlPathSegment(id, 'externalId')}/` : ''}` |
| 77 | +} |
| 78 | + |
| 79 | +async function requestPlane( |
| 80 | + token: string, |
| 81 | + config: PlaneSourceConfig, |
| 82 | + path: string, |
| 83 | + query: Record<string, unknown> = {}, |
| 84 | + retryOptions?: RetryOptions |
| 85 | +) { |
| 86 | + return secureFetchWithRetry( |
| 87 | + planeApiUrl(config.baseUrl, path, query), |
| 88 | + { |
| 89 | + method: 'GET', |
| 90 | + headers: planeHeaders(token), |
| 91 | + profile: 'configuredEndpoint', |
| 92 | + redirectPolicy: planeRedirectPolicy(), |
| 93 | + maxResponseBytes: 8 * 1024 * 1024, |
| 94 | + }, |
| 95 | + retryOptions |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +export const planeConnector: ConnectorConfig = { |
| 100 | + ...planeConnectorMeta, |
| 101 | + |
| 102 | + async listDocuments(token, sourceConfig, cursor, syncContext) { |
| 103 | + const config = readConfig(sourceConfig) |
| 104 | + const fetched = |
| 105 | + typeof syncContext?.totalDocsFetched === 'number' ? syncContext.totalDocsFetched : 0 |
| 106 | + const remaining = |
| 107 | + config.maxDocuments > 0 ? Math.max(0, config.maxDocuments - fetched) : PAGE_SIZE |
| 108 | + if (remaining === 0) { |
| 109 | + if (syncContext) syncContext.listingCapped = true |
| 110 | + return { documents: [], hasMore: false, reconciliationSafe: false } |
| 111 | + } |
| 112 | + const response = await requestPlane(token, config, resourcePath(config), { |
| 113 | + cursor, |
| 114 | + per_page: Math.min(PAGE_SIZE, remaining), |
| 115 | + fields: config.contentType === 'pages' ? PAGE_FIELDS : WORK_ITEM_FIELDS, |
| 116 | + ...(config.contentType === 'work_items' |
| 117 | + ? { expand: 'state,assignees,labels', order_by: 'created_at' } |
| 118 | + : {}), |
| 119 | + }) |
| 120 | + if (!response.ok) throw listingRequestError('Plane listing failed', response.status) |
| 121 | + const page = parsePlanePage(await response.json(), cursor) |
| 122 | + const documents = await Promise.all( |
| 123 | + page.results.map((item) => |
| 124 | + config.contentType === 'pages' |
| 125 | + ? planePageDocument(item, config, true) |
| 126 | + : planeWorkItemDocument(item, config) |
| 127 | + ) |
| 128 | + ) |
| 129 | + const total = fetched + documents.length |
| 130 | + if (syncContext) syncContext.totalDocsFetched = total |
| 131 | + const capped = |
| 132 | + config.maxDocuments > 0 && total >= config.maxDocuments && page.nextCursor !== undefined |
| 133 | + if (capped && syncContext) syncContext.listingCapped = true |
| 134 | + logger.info('Listed Plane documents', { |
| 135 | + count: documents.length, |
| 136 | + hasMore: page.nextCursor !== undefined && !capped, |
| 137 | + capped, |
| 138 | + }) |
| 139 | + return { |
| 140 | + documents, |
| 141 | + nextCursor: capped ? undefined : page.nextCursor, |
| 142 | + hasMore: page.nextCursor !== undefined && !capped, |
| 143 | + // Plane v1 cursors encode offsets, so concurrent source changes can shift records between pages. |
| 144 | + reconciliationSafe: false, |
| 145 | + } |
| 146 | + }, |
| 147 | + |
| 148 | + async getDocument(token, sourceConfig, externalId): Promise<ExternalDocument | null> { |
| 149 | + const config = readConfig(sourceConfig) |
| 150 | + const prefix = config.contentType === 'pages' ? 'page:' : 'work_item:' |
| 151 | + if (!externalId.startsWith(prefix) || externalId.length === prefix.length) return null |
| 152 | + const response = await requestPlane( |
| 153 | + token, |
| 154 | + config, |
| 155 | + resourcePath(config, externalId.slice(prefix.length)), |
| 156 | + { |
| 157 | + fields: |
| 158 | + config.contentType === 'pages' |
| 159 | + ? 'id,name,updated_at,description_html,description_stripped,description' |
| 160 | + : WORK_ITEM_FIELDS, |
| 161 | + ...(config.contentType === 'work_items' ? { expand: 'state,assignees,labels' } : {}), |
| 162 | + } |
| 163 | + ) |
| 164 | + if (response.status === 404) return null |
| 165 | + if (!response.ok) throw new Error(`Plane document read failed (HTTP ${response.status})`) |
| 166 | + const payload: unknown = await response.json() |
| 167 | + return config.contentType === 'pages' |
| 168 | + ? planePageDocument(payload, config, false) |
| 169 | + : planeWorkItemDocument(payload, config) |
| 170 | + }, |
| 171 | + |
| 172 | + async validateConfig(token, sourceConfig) { |
| 173 | + try { |
| 174 | + const config = readConfig(sourceConfig) |
| 175 | + const response = await requestPlane( |
| 176 | + token, |
| 177 | + config, |
| 178 | + resourcePath(config), |
| 179 | + { |
| 180 | + per_page: 1, |
| 181 | + fields: config.contentType === 'pages' ? PAGE_FIELDS : 'id,name', |
| 182 | + }, |
| 183 | + VALIDATE_RETRY_OPTIONS |
| 184 | + ) |
| 185 | + if (!response.ok) |
| 186 | + return { |
| 187 | + valid: false, |
| 188 | + error: `Plane access failed (HTTP ${response.status}). Check the token, workspace, project and edition.`, |
| 189 | + } |
| 190 | + parsePlanePage(await response.json()) |
| 191 | + return { valid: true } |
| 192 | + } catch (error) { |
| 193 | + return { |
| 194 | + valid: false, |
| 195 | + error: getErrorMessage(error, 'Failed to validate Plane configuration'), |
| 196 | + } |
| 197 | + } |
| 198 | + }, |
| 199 | + |
| 200 | + mapTags(metadata) { |
| 201 | + return { |
| 202 | + projectId: metadata.projectId, |
| 203 | + state: metadata.state, |
| 204 | + priority: metadata.priority, |
| 205 | + assignees: joinTagArray(metadata.assignees), |
| 206 | + labels: joinTagArray(metadata.labels), |
| 207 | + lastModified: parseTagDate(metadata.lastModified), |
| 208 | + } |
| 209 | + }, |
| 210 | +} |
0 commit comments