Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
54 changes: 54 additions & 0 deletions db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1238,3 +1238,57 @@ export const getUniqueTopicCount = async (
if (!count) throw new Error("Failed to get unique topic count")
return count
}

/**
* Data structure for topics to be indexed in AI Search
*/
export interface TopicForAISearch {
id: number
name: string
slug: string
excerpt: string
markdown: string
}

/**
* Get topics (tags with associated topic pages) for AI Search indexing.
* Returns tags that have a matching published topic page (TopicPage or LinearTopicPage).
*/
export async function getTopicsForAISearch(
knex: KnexReadonlyTransaction
): Promise<TopicForAISearch[]> {
const rows = await knexRaw<{
id: number
name: string
slug: string
excerpt: string | null
markdown: string | null
}>(
knex,
`-- sql
SELECT
t.id,
t.name,
t.slug,
JSON_UNQUOTE(JSON_EXTRACT(p.content, '$.excerpt')) AS excerpt,
p.markdown
FROM tags t
JOIN posts_gdocs p ON t.slug = p.slug
WHERE
t.slug IS NOT NULL
AND p.published = 1
AND p.type IN (:types)
ORDER BY t.name ASC`,
{
types: [OwidGdocType.TopicPage, OwidGdocType.LinearTopicPage],
}
)

return rows.map((row) => ({
id: row.id,
name: row.name,
slug: row.slug,
excerpt: row.excerpt ?? "",
markdown: row.markdown ?? "",
}))
}
3 changes: 3 additions & 0 deletions functions/_common/env.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export interface Env {
ASSETS: Fetcher
AI: Ai
VECTORIZE_TOPICS: Vectorize
VECTORIZE_VOCABULARY: Vectorize
url: URL
GRAPHER_CONFIG_R2_BUCKET_URL: string
GRAPHER_CONFIG_R2_BUCKET_FALLBACK_URL: string
Expand Down
Loading
Loading