Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(search): ingest github discussions

Greg Richardsone8a0a908de94
diff --git a/apps/docs/scripts/search/sources/github-discussion.ts b/apps/docs/scripts/search/sources/github-discussion.ts
new file mode 100644
index 00000000000..d60a06c6e5a
--- /dev/null
+++ b/apps/docs/scripts/search/sources/github-discussion.ts
@@ -0,0 +1,111 @@
+import { Octokit } from '@octokit/core'
+import { paginateGraphql } from '@octokit/plugin-paginate-graphql'
+import { createHash } from 'crypto'
+import { BaseSource } from './base'
+
+export const ExtendedOctokit = Octokit.plugin(paginateGraphql)
+export type ExtendedOctokit = InstanceType<typeof ExtendedOctokit>
+
+export type Discussion = {
+ id: string
+ updatedAt: string
+ url: string
+ title: string
+ body: string
+ databaseId: number
+}
+
+export type DiscussionsResponse = {
+ repository: {
+ discussions: {
+ totalCount: number
+ nodes: Discussion[]
+ }
+ }
+}
+
+/**
+ * Fetches GitHub discussions for a repository + category
+ */
+export async function fetchDiscussions(owner: string, repo: string, categoryId: string) {
+ const octokit = new ExtendedOctokit({ auth: process.env.GITHUB_ACCESS_TOKEN })
+
+ const {
+ repository: {
+ discussions: { nodes: discussions },
+ },
+ } = await octokit.graphql.paginate<DiscussionsResponse>(
+ `
+ query troubleshootDiscussions($cursor: String, $owner: String!, $repo: String!, $categoryId: ID!) {
+ repository(owner: $owner, name: $repo) {
+ discussions(first: 100, after: $cursor, categoryId: $categoryId) {
+ totalCount
+ nodes {
+ id
+ updatedAt
+ url
+ title
+ body
+ databaseId
+ }
+ pageInfo {
+ hasNextPage
+ endCursor
+ }
+ }
+ }
+ }
+ `,
+ {
+ owner,
+ repo,
+ categoryId,
+ }
+ )
+
+ return discussions
+}
+
+export class GitHubDiscussionSource extends BaseSource {
+ type = 'github-discussions' as const
+
+ constructor(source: string, public discussion: Discussion) {
+ super(source, discussion.url)
+ }
+
+ async load() {
+ const { id, title, updatedAt, body, databaseId } = this.discussion
+
+ const checksum = createHash('sha256').update(updatedAt).digest('base64')
+
+ const meta = { id, title, updatedAt }
+
+ // Currently the discussion post itself is being considered as the answer
+ // (as opposed to a comment marked as answer)
+ // So we link the slug to the initial discussion post rather than a comment answer
+ const slug = `#discussion-${databaseId}`
+
+ // Format the discussion title + body as markdown for better embeddings + LLM response
+ const content = `# ${title}\n${body}`
+
+ // For now, only a single section is created for GH discussions
+ // Consider adding multiple if we want to include comments/answers
+ const sections = [
+ {
+ heading: title,
+ slug,
+ content,
+ },
+ ]
+
+ this.checksum = checksum
+ this.meta = meta
+ this.sections = sections
+
+ return {
+ checksum,
+ meta,
+ sections,
+ }
+ }
+}
diff --git a/apps/docs/scripts/search/sources/index.ts b/apps/docs/scripts/search/sources/index.ts
index 66ff45b2932..57b8314aafc 100644
--- a/apps/docs/scripts/search/sources/index.ts
+++ b/apps/docs/scripts/search/sources/index.ts
@@ -1,3 +1,4 @@
+import { GitHubDiscussionSource, fetchDiscussions } from './github-discussion'
import { MarkdownSource } from './markdown'
import {
CliReferenceSource,
@@ -13,6 +14,7 @@ export type SearchSource =
| OpenApiReferenceSource
| ClientLibReferenceSource
| CliReferenceSource
+ | GitHubDiscussionSource
/**
* Fetches all the sources we want to index for search
@@ -71,6 +73,14 @@ export async function fetchSources() {
.filter(({ path }) => !ignoredFiles.includes(path))
.map((entry) => new MarkdownSource('guide', entry.path))
+ const githubDiscussionSources = (
+ await fetchDiscussions(
+ 'supabase',
+ 'supabase',
+ 'DIC_kwDODMpXOc4CUvEr' // 'Troubleshooting' category
+ )
+ ).map((discussion) => new GitHubDiscussionSource('supabase/supabase', discussion))
+
const sources: SearchSource[] = [
openApiReferenceSource,
jsLibReferenceSource,
@@ -78,6 +88,7 @@ export async function fetchSources() {
pythonLibReferenceSource,
cSharpLibReferenceSource,
cliReferenceSource,
+ ...githubDiscussionSources,
...guideSources,
]