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 { z } from 'zod'; |
| 2 | import authGuideRegistry from '@/kb/external-sources/auth-guides.json'; |
| 3 | import type { AlgoliaDocsRecord } from '@/lib/search-index'; |
| 4 | import { |
| 5 | classifyKnowledgeRecord, |
| 6 | normalizeKnowledgeKeywords, |
| 7 | } from './metadata'; |
| 8 | |
| 9 | export interface AuthGuideRegistryEntry { |
| 10 | slug: string; |
| 11 | toolkitSlug: string; |
| 12 | canonicalUrl: `/toolkits/${string}`; |
| 13 | title: string; |
| 14 | description: string; |
| 15 | } |
| 16 | |
| 17 | function parseRegistry(value: unknown): AuthGuideRegistryEntry[] { |
| 18 | // The imported snapshot retains landing URLs. Resolve destinations here so |
| 19 | // refreshing that snapshot cannot restore retired routes to the search index. |
| 20 | const entries = z.array(z.object({ |
| 21 | slug: z.string().trim().regex(/^[a-z0-9-]+$/), |
| 22 | toolkitSlug: z.string().trim().regex(/^[a-z0-9_-]+$/), |
| 23 | title: z.string().trim().min(1), |
| 24 | description: z.string().trim().min(1), |
| 25 | })).parse(value).map((entry): AuthGuideRegistryEntry => { |
| 26 | const toolkitSlug = entry.toolkitSlug === 'googleapps' ? 'googlesuper' : entry.toolkitSlug; |
| 27 | return { |
| 28 | ...entry, |
| 29 | toolkitSlug, |
| 30 | canonicalUrl: `/toolkits/${toolkitSlug}`, |
| 31 | }; |
| 32 | }); |
| 33 | |
| 34 | if (new Set(entries.map((entry) => entry.slug)).size !== entries.length) { |
| 35 | throw new Error('OAuth guide registry contains duplicate slugs'); |
| 36 | } |
| 37 | if (new Set(entries.map((entry) => entry.canonicalUrl)).size !== entries.length) { |
| 38 | throw new Error('OAuth guide registry contains duplicate canonical URLs'); |
| 39 | } |
| 40 | return entries; |
| 41 | } |
| 42 | |
| 43 | const parsedRegistry = parseRegistry(authGuideRegistry); |
| 44 | |
| 45 | export function getAuthGuideRegistry(): AuthGuideRegistryEntry[] { |
| 46 | return parsedRegistry.map((entry) => ({ ...entry })); |
| 47 | } |
| 48 | |
| 49 | export function getAuthGuideSearchRecords(): AlgoliaDocsRecord[] { |
| 50 | return parsedRegistry.map((entry) => { |
| 51 | const metadata = classifyKnowledgeRecord({ |
| 52 | sourceType: 'oauth-guide', |
| 53 | canonicalUrl: entry.canonicalUrl, |
| 54 | toolkitSlugs: [entry.toolkitSlug], |
| 55 | intents: ['setup'], |
| 56 | }); |
| 57 | |
| 58 | return { |
| 59 | objectID: `oauth-guide:${entry.slug}`, |
| 60 | title: entry.title, |
| 61 | description: entry.description, |
| 62 | breadcrumbs: ['OAuth'], |
| 63 | url: entry.canonicalUrl, |
| 64 | page_id: entry.canonicalUrl, |
| 65 | content: entry.description, |
| 66 | keywords: normalizeKnowledgeKeywords([ |
| 67 | entry.slug, |
| 68 | entry.toolkitSlug, |
| 69 | 'oauth', |
| 70 | 'authentication', |
| 71 | 'credentials', |
| 72 | ]), |
| 73 | slug: entry.slug, |
| 74 | headings: [], |
| 75 | type: 'oauth-guide', |
| 76 | lang: 'en', |
| 77 | page_rank: 1_700, |
| 78 | toolkit_popularity: 0, |
| 79 | section_rank: 120, |
| 80 | position: 0, |
| 81 | depth: 0, |
| 82 | ...metadata, |
| 83 | } satisfies AlgoliaDocsRecord; |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | export async function validateAuthGuideUrls( |
| 88 | entries: AuthGuideRegistryEntry[], |
| 89 | fetchImpl: typeof fetch = fetch, |
| 90 | ): Promise<void> { |
| 91 | await Promise.all(entries.map(async (entry) => { |
| 92 | const expected = new URL(entry.canonicalUrl, 'https://docs.composio.dev'); |
| 93 | let response: Response; |
| 94 | try { |
| 95 | response = await fetchImpl(expected.href, { |
| 96 | method: 'GET', |
| 97 | redirect: 'follow', |
| 98 | headers: { accept: 'text/html' }, |
| 99 | }); |
| 100 | } catch (error) { |
| 101 | throw new Error(`Failed to validate OAuth guide ${expected.href}`, { cause: error }); |
| 102 | } |
| 103 | if (!response.ok) { |
| 104 | throw new Error( |
| 105 | `Failed to validate OAuth guide ${expected.href}: HTTP ${response.status}`, |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | let finalUrl: string; |
| 110 | try { |
| 111 | const resolved = new URL(response.url); |
| 112 | finalUrl = `${resolved.origin}${resolved.pathname.replace(/\/+$/, '')}`; |
| 113 | } catch { |
| 114 | throw new Error( |
| 115 | `Failed to validate OAuth guide ${expected.href}: missing final response URL`, |
| 116 | ); |
| 117 | } |
| 118 | const expectedUrl = `${expected.origin}${expected.pathname.replace(/\/+$/, '')}`; |
| 119 | if (finalUrl !== expectedUrl) { |
| 120 | throw new Error( |
| 121 | `Failed to validate OAuth guide ${expected.href}: redirected to ${response.url}`, |
| 122 | ); |
| 123 | } |
| 124 | })); |
| 125 | } |