-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathgithub.ts
More file actions
425 lines (373 loc) · 12.6 KB
/
github.ts
File metadata and controls
425 lines (373 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
import { createLogger } from '@sim/logger'
import { GithubIcon } from '@/components/icons'
import { fetchWithRetry, VALIDATE_RETRY_OPTIONS } from '@/lib/knowledge/documents/utils'
import type { ConnectorConfig, ExternalDocument, ExternalDocumentList } from '@/connectors/types'
import { computeContentHash, parseTagDate } from '@/connectors/utils'
const logger = createLogger('GitHubConnector')
const GITHUB_API_URL = 'https://api.github.com'
const BATCH_SIZE = 30
/**
* Parses the repository string into owner and repo.
*/
function parseRepo(repository: string): { owner: string; repo: string } {
const cleaned = repository.replace(/^https?:\/\/github\.com\//, '').replace(/\.git$/, '')
const parts = cleaned.split('/')
if (parts.length < 2 || !parts[0] || !parts[1]) {
throw new Error(`Invalid repository format: "${repository}". Use "owner/repo".`)
}
return { owner: parts[0], repo: parts[1] }
}
/**
* File extension filter set from user config. Returns null if no filter (accept all).
*/
function parseExtensions(extensions: string): Set<string> | null {
const trimmed = extensions.trim()
if (!trimmed) return null
const exts = trimmed
.split(',')
.map((e) => e.trim().toLowerCase())
.filter(Boolean)
.map((e) => (e.startsWith('.') ? e : `.${e}`))
return exts.length > 0 ? new Set(exts) : null
}
/**
* Checks whether a file path matches the extension filter.
*/
function matchesExtension(filePath: string, extSet: Set<string> | null): boolean {
if (!extSet) return true
const lastDot = filePath.lastIndexOf('.')
if (lastDot === -1) return false
return extSet.has(filePath.slice(lastDot).toLowerCase())
}
interface TreeItem {
path: string
mode: string
type: string
sha: string
size?: number
}
/**
* Fetches the full recursive tree for a branch.
*/
async function fetchTree(
accessToken: string,
owner: string,
repo: string,
branch: string
): Promise<TreeItem[]> {
const url = `${GITHUB_API_URL}/repos/${owner}/${repo}/git/trees/${encodeURIComponent(branch)}?recursive=1`
const response = await fetchWithRetry(url, {
method: 'GET',
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${accessToken}`,
'X-GitHub-Api-Version': '2022-11-28',
},
})
if (!response.ok) {
const errorText = await response.text()
logger.error('Failed to fetch GitHub tree', { status: response.status, error: errorText })
throw new Error(`Failed to fetch repository tree: ${response.status}`)
}
const data = await response.json()
if (data.truncated) {
logger.warn('GitHub tree was truncated — some files may be missing', { owner, repo, branch })
}
return (data.tree || []).filter((item: TreeItem) => item.type === 'blob')
}
/**
* Fetches file content via the Blobs API and decodes base64.
*/
async function fetchBlobContent(
accessToken: string,
owner: string,
repo: string,
sha: string
): Promise<string> {
const url = `${GITHUB_API_URL}/repos/${owner}/${repo}/git/blobs/${sha}`
const response = await fetchWithRetry(url, {
method: 'GET',
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${accessToken}`,
'X-GitHub-Api-Version': '2022-11-28',
},
})
if (!response.ok) {
throw new Error(`Failed to fetch blob ${sha}: ${response.status}`)
}
const data = await response.json()
if (data.encoding === 'base64') {
return Buffer.from(data.content, 'base64').toString('utf-8')
}
return data.content || ''
}
/**
* Converts a tree item to an ExternalDocument by fetching its content.
*/
async function treeItemToDocument(
accessToken: string,
owner: string,
repo: string,
branch: string,
item: TreeItem
): Promise<ExternalDocument> {
const content = await fetchBlobContent(accessToken, owner, repo, item.sha)
const contentHash = await computeContentHash(content)
return {
externalId: item.path,
title: item.path.split('/').pop() || item.path,
content,
mimeType: 'text/plain',
sourceUrl: `https://github.com/${owner}/${repo}/blob/${encodeURIComponent(branch)}/${item.path.split('/').map(encodeURIComponent).join('/')}`,
contentHash,
metadata: {
path: item.path,
sha: item.sha,
size: item.size,
branch,
repository: `${owner}/${repo}`,
},
}
}
export const githubConnector: ConnectorConfig = {
id: 'github',
name: 'GitHub',
description: 'Sync files from a GitHub repository into your knowledge base',
version: '1.0.0',
icon: GithubIcon,
auth: {
mode: 'apiKey',
label: 'Personal Access Token',
placeholder: 'ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
},
configFields: [
{
id: 'repository',
title: 'Repository',
type: 'short-input',
placeholder: 'owner/repo',
required: true,
},
{
id: 'branch',
title: 'Branch',
type: 'short-input',
placeholder: 'main (default)',
required: false,
},
{
id: 'pathPrefix',
title: 'Path Filter',
type: 'short-input',
placeholder: 'e.g. docs/, src/components/',
required: false,
},
{
id: 'extensions',
title: 'File Extensions',
type: 'short-input',
placeholder: 'e.g. .md, .txt, .mdx',
required: false,
},
{
id: 'maxFiles',
title: 'Max Files',
type: 'short-input',
required: false,
placeholder: 'e.g. 500 (default: unlimited)',
},
],
listDocuments: async (
accessToken: string,
sourceConfig: Record<string, unknown>,
cursor?: string,
syncContext?: Record<string, unknown>
): Promise<ExternalDocumentList> => {
const { owner, repo } = parseRepo(sourceConfig.repository as string)
const branch = ((sourceConfig.branch as string) || 'main').trim()
const pathPrefix = ((sourceConfig.pathPrefix as string) || '').trim()
const extSet = parseExtensions((sourceConfig.extensions as string) || '')
const maxFiles = sourceConfig.maxFiles ? Number(sourceConfig.maxFiles) : 0
let capped: TreeItem[]
if (syncContext?.filteredTree) {
capped = syncContext.filteredTree as TreeItem[]
} else {
const tree = await fetchTree(accessToken, owner, repo, branch)
// Filter by path prefix and extensions
const filtered = tree.filter((item) => {
if (pathPrefix && !item.path.startsWith(pathPrefix)) return false
if (!matchesExtension(item.path, extSet)) return false
return true
})
// Apply max files limit
capped = maxFiles > 0 ? filtered.slice(0, maxFiles) : filtered
if (syncContext) syncContext.filteredTree = capped
}
// Paginate using offset cursor
const offset = cursor ? Number(cursor) : 0
const batch = capped.slice(offset, offset + BATCH_SIZE)
logger.info('Listing GitHub files', {
owner,
repo,
branch,
totalFiltered: capped.length,
offset,
batchSize: batch.length,
})
const BLOB_CONCURRENCY = 5
const documents: ExternalDocument[] = []
for (let i = 0; i < batch.length; i += BLOB_CONCURRENCY) {
const chunk = batch.slice(i, i + BLOB_CONCURRENCY)
const results = await Promise.all(
chunk.map(async (item) => {
try {
return await treeItemToDocument(accessToken, owner, repo, branch, item)
} catch (error) {
logger.warn(`Failed to fetch content for ${item.path}`, {
error: error instanceof Error ? error.message : String(error),
})
return null
}
})
)
documents.push(...(results.filter(Boolean) as ExternalDocument[]))
}
const nextOffset = offset + BATCH_SIZE
const hasMore = nextOffset < capped.length
return {
documents,
nextCursor: hasMore ? String(nextOffset) : undefined,
hasMore,
}
},
getDocument: async (
accessToken: string,
sourceConfig: Record<string, unknown>,
externalId: string
): Promise<ExternalDocument | null> => {
const { owner, repo } = parseRepo(sourceConfig.repository as string)
const branch = ((sourceConfig.branch as string) || 'main').trim()
// externalId is the file path
const path = externalId
try {
const encodedPath = path.split('/').map(encodeURIComponent).join('/')
const url = `${GITHUB_API_URL}/repos/${owner}/${repo}/contents/${encodedPath}?ref=${encodeURIComponent(branch)}`
const response = await fetchWithRetry(url, {
method: 'GET',
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${accessToken}`,
'X-GitHub-Api-Version': '2022-11-28',
},
})
if (!response.ok) {
if (response.status === 404) return null
throw new Error(`Failed to fetch file ${path}: ${response.status}`)
}
const lastModifiedHeader = response.headers.get('last-modified') || undefined
const data = await response.json()
const content =
data.encoding === 'base64'
? Buffer.from(data.content as string, 'base64').toString('utf-8')
: (data.content as string) || ''
const contentHash = await computeContentHash(content)
return {
externalId,
title: path.split('/').pop() || path,
content,
mimeType: 'text/plain',
sourceUrl: `https://github.com/${owner}/${repo}/blob/${encodeURIComponent(branch)}/${path.split('/').map(encodeURIComponent).join('/')}`,
contentHash,
metadata: {
path,
sha: data.sha as string,
size: data.size as number,
branch,
repository: `${owner}/${repo}`,
lastModified: lastModifiedHeader,
},
}
} catch (error) {
logger.warn(`Failed to fetch GitHub document ${externalId}`, {
error: error instanceof Error ? error.message : String(error),
})
return null
}
},
validateConfig: async (
accessToken: string,
sourceConfig: Record<string, unknown>
): Promise<{ valid: boolean; error?: string }> => {
const repository = (sourceConfig.repository as string)?.trim()
if (!repository) {
return { valid: false, error: 'Repository is required' }
}
let owner: string
let repo: string
try {
const parsed = parseRepo(repository)
owner = parsed.owner
repo = parsed.repo
} catch (error) {
return {
valid: false,
error: error instanceof Error ? error.message : 'Invalid repository format',
}
}
const maxFiles = sourceConfig.maxFiles as string | undefined
if (maxFiles && (Number.isNaN(Number(maxFiles)) || Number(maxFiles) <= 0)) {
return { valid: false, error: 'Max files must be a positive number' }
}
const branch = ((sourceConfig.branch as string) || 'main').trim()
try {
// Verify repo and branch are accessible
const url = `${GITHUB_API_URL}/repos/${owner}/${repo}/branches/${encodeURIComponent(branch)}`
const response = await fetchWithRetry(
url,
{
method: 'GET',
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${accessToken}`,
'X-GitHub-Api-Version': '2022-11-28',
},
},
VALIDATE_RETRY_OPTIONS
)
if (response.status === 404) {
return {
valid: false,
error: `Repository "${owner}/${repo}" or branch "${branch}" not found`,
}
}
if (!response.ok) {
return { valid: false, error: `Cannot access repository: ${response.status}` }
}
return { valid: true }
} catch (error) {
const message = error instanceof Error ? error.message : 'Failed to validate configuration'
return { valid: false, error: message }
}
},
tagDefinitions: [
{ id: 'path', displayName: 'File Path', fieldType: 'text' },
{ id: 'repository', displayName: 'Repository', fieldType: 'text' },
{ id: 'branch', displayName: 'Branch', fieldType: 'text' },
{ id: 'size', displayName: 'File Size', fieldType: 'number' },
{ id: 'lastModified', displayName: 'Last Modified', fieldType: 'date' },
],
mapTags: (metadata: Record<string, unknown>): Record<string, unknown> => {
const result: Record<string, unknown> = {}
if (typeof metadata.path === 'string') result.path = metadata.path
if (typeof metadata.repository === 'string') result.repository = metadata.repository
if (typeof metadata.branch === 'string') result.branch = metadata.branch
if (metadata.size != null) {
const num = Number(metadata.size)
if (!Number.isNaN(num)) result.size = num
}
const lastModified = parseTagDate(metadata.lastModified)
if (lastModified) result.lastModified = lastModified
return result
},
}