Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | import toolkitsData from '@/public/data/toolkits-list.json'; |
| 2 | import type { ToolkitSummary } from '@/types/toolkit'; |
| 3 | import { getAlgoliaSearchDocuments, type AlgoliaDocsRecord } from '@/lib/search-index'; |
| 4 | import { normalizeToolkitSlug } from './metadata'; |
| 5 | import { KNOWLEDGE_SOURCE_LABELS, type KnowledgeSourceType, type ProductAreaSlug } from './types'; |
| 6 | |
| 7 | export interface KnowledgeLink { |
| 8 | title: string; |
| 9 | description: string; |
| 10 | href: string; |
| 11 | sourceType: KnowledgeSourceType; |
| 12 | sourceLabel: string; |
| 13 | productAreas: ProductAreaSlug[]; |
| 14 | toolkitSlugs: string[]; |
| 15 | lastVerifiedAt: string | null; |
| 16 | } |
| 17 | |
| 18 | export interface ToolkitKnowledgeSummary { |
| 19 | slug: string; |
| 20 | name: string; |
| 21 | logo: string | null; |
| 22 | category: string | null; |
| 23 | knowledgeCount: number; |
| 24 | } |
| 25 | |
| 26 | const toolkits = toolkitsData as ToolkitSummary[]; |
| 27 | const SOURCE_ORDER: Record<KnowledgeSourceType, number> = { |
| 28 | docs: 0, |
| 29 | kb: 1, |
| 30 | 'oauth-guide': 2, |
| 31 | toolkit: 3, |
| 32 | example: 4, |
| 33 | reference: 5, |
| 34 | changelog: 6, |
| 35 | legacy: 7, |
| 36 | }; |
| 37 | |
| 38 | const FEATURED_CANONICAL_URLS = [ |
| 39 | '/kb/guide/platform-pagination', |
| 40 | '/kb/guide/platform-triggers', |
| 41 | '/kb/guide/platform-custom-connection-data-fields', |
| 42 | '/toolkits/github', |
| 43 | ] as const; |
| 44 | |
| 45 | let catalogPromise: Promise<KnowledgeLink[]> | null = null; |
| 46 | |
| 47 | function recordToLink(record: AlgoliaDocsRecord): KnowledgeLink { |
| 48 | return { |
| 49 | title: record.title, |
| 50 | description: record.description || record.content || record.title, |
| 51 | href: record.canonical_url, |
| 52 | sourceType: record.source_type, |
| 53 | sourceLabel: KNOWLEDGE_SOURCE_LABELS[record.source_type], |
| 54 | productAreas: record.product_areas, |
| 55 | toolkitSlugs: record.toolkit_slugs, |
| 56 | lastVerifiedAt: record.last_verified_at, |
| 57 | }; |
| 58 | } |
| 59 | |
| 60 | function sortLinks(links: KnowledgeLink[]): KnowledgeLink[] { |
| 61 | return [...links].sort((left, right) => |
| 62 | SOURCE_ORDER[left.sourceType] - SOURCE_ORDER[right.sourceType] || |
| 63 | left.title.localeCompare(right.title), |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | async function getKnowledgeCatalog(): Promise<KnowledgeLink[]> { |
| 68 | catalogPromise ??= getAlgoliaSearchDocuments().then((records) => { |
| 69 | const bestByCanonicalUrl = new Map<string, AlgoliaDocsRecord>(); |
| 70 | for (const record of records) { |
| 71 | const current = bestByCanonicalUrl.get(record.canonical_url); |
| 72 | if ( |
| 73 | !current || |
| 74 | record.page_rank > current.page_rank || |
| 75 | (record.page_rank === current.page_rank && record.section_rank > current.section_rank) |
| 76 | ) { |
| 77 | bestByCanonicalUrl.set(record.canonical_url, record); |
| 78 | } |
| 79 | } |
| 80 | return sortLinks([...bestByCanonicalUrl.values()].map(recordToLink)); |
| 81 | }); |
| 82 | return catalogPromise; |
| 83 | } |
| 84 | |
| 85 | export async function getFeaturedKnowledgeLinks(): Promise<KnowledgeLink[]> { |
| 86 | const links = await getKnowledgeCatalog(); |
| 87 | const byUrl = new Map(links.map((link) => [link.href, link])); |
| 88 | return FEATURED_CANONICAL_URLS.map((url) => { |
| 89 | const link = byUrl.get(url); |
| 90 | if (!link) throw new Error(`Featured knowledge URL is missing from the corpus: ${url}`); |
| 91 | return link; |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | export async function getKnowledgeByProductArea( |
| 96 | slug: ProductAreaSlug, |
| 97 | ): Promise<KnowledgeLink[]> { |
| 98 | return (await getKnowledgeCatalog()).filter((link) => link.productAreas.includes(slug)); |
| 99 | } |
| 100 | |
| 101 | export async function getKnowledgeByToolkit(slug: string): Promise<KnowledgeLink[]> { |
| 102 | const normalized = normalizeToolkitSlug(slug); |
| 103 | return (await getKnowledgeCatalog()).filter((link) => link.toolkitSlugs.includes(normalized)); |
| 104 | } |
| 105 | |
| 106 | export async function getKnowledgeToolkitSummaries(): Promise<ToolkitKnowledgeSummary[]> { |
| 107 | const links = await getKnowledgeCatalog(); |
| 108 | const counts = new Map<string, number>(); |
| 109 | for (const link of links) { |
| 110 | for (const slug of link.toolkitSlugs) counts.set(slug, (counts.get(slug) ?? 0) + 1); |
| 111 | } |
| 112 | |
| 113 | return toolkits |
| 114 | .map((toolkit) => ({ |
| 115 | slug: toolkit.slug, |
| 116 | name: toolkit.name.trim(), |
| 117 | logo: toolkit.logo, |
| 118 | category: toolkit.category, |
| 119 | knowledgeCount: counts.get(toolkit.slug) ?? 0, |
| 120 | })) |
| 121 | .filter((toolkit) => toolkit.knowledgeCount > 0); |
| 122 | } |