Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { isProductAreaSlug } from './taxonomy'; |
| 2 | import type { |
| 3 | KnowledgeClassificationInput, |
| 4 | KnowledgeIntent, |
| 5 | KnowledgeMetadata, |
| 6 | ProductAreaSlug, |
| 7 | } from './types'; |
| 8 | |
| 9 | const KB_TOPIC_AREAS: Record<string, ProductAreaSlug[]> = { |
| 10 | authentication: ['authentication-and-connected-accounts'], |
| 11 | 'connected-accounts': ['authentication-and-connected-accounts'], |
| 12 | 'auth-config': ['authentication-and-connected-accounts'], |
| 13 | 'sessions-and-execution': ['tools-actions-and-execution'], |
| 14 | 'triggers-and-webhooks': ['triggers-and-workflows'], |
| 15 | workflows: ['triggers-and-workflows'], |
| 16 | 'tool-router-and-mcp': ['sdk-api-and-mcp'], |
| 17 | 'sdk-and-api': ['sdk-api-and-mcp'], |
| 18 | 'dashboard-and-projects': ['account-billing-and-security'], |
| 19 | 'billing-and-plans': ['account-billing-and-security'], |
| 20 | 'account-and-billing': ['account-billing-and-security'], |
| 21 | 'security-and-trust': ['account-billing-and-security'], |
| 22 | }; |
| 23 | |
| 24 | const DOC_PATH_AREAS: Array<[prefix: string, area: ProductAreaSlug]> = [ |
| 25 | ['/docs/auth-configuration', 'authentication-and-connected-accounts'], |
| 26 | ['/docs/providers', 'sdk-api-and-mcp'], |
| 27 | ['/docs/tools-direct', 'tools-actions-and-execution'], |
| 28 | ['/docs/setting-up-triggers', 'triggers-and-workflows'], |
| 29 | ['/docs/extending-sessions', 'tools-actions-and-execution'], |
| 30 | ['/docs/migration-guide', 'sdk-api-and-mcp'], |
| 31 | ['/docs/sandbox', 'tools-actions-and-execution'], |
| 32 | ['/docs/sessions-via-mcp', 'sdk-api-and-mcp'], |
| 33 | ['/docs/single-toolkit-mcp', 'sdk-api-and-mcp'], |
| 34 | ['/docs/composio-connect', 'sdk-api-and-mcp'], |
| 35 | ['/docs/claude-code-plugin', 'sdk-api-and-mcp'], |
| 36 | ['/docs/cli', 'sdk-api-and-mcp'], |
| 37 | ['/docs/configuring-sessions', 'tools-actions-and-execution'], |
| 38 | ['/docs/how-composio-works', 'tools-actions-and-execution'], |
| 39 | ['/docs/quickstart', 'tools-actions-and-execution'], |
| 40 | ['/docs/triggers', 'triggers-and-workflows'], |
| 41 | ]; |
| 42 | |
| 43 | function unique<T>(values: T[]): T[] { |
| 44 | return Array.from(new Set(values)); |
| 45 | } |
| 46 | |
| 47 | function inferredProductAreas(input: KnowledgeClassificationInput): ProductAreaSlug[] { |
| 48 | if (input.sourceType === 'kb') { |
| 49 | return unique((input.topics ?? []).flatMap((topic) => KB_TOPIC_AREAS[topic] ?? [])); |
| 50 | } |
| 51 | if (input.sourceType === 'docs') { |
| 52 | return DOC_PATH_AREAS |
| 53 | .filter(([prefix]) => input.canonicalUrl === prefix || input.canonicalUrl.startsWith(`${prefix}/`)) |
| 54 | .map(([, area]) => area); |
| 55 | } |
| 56 | if (input.sourceType === 'oauth-guide') return ['authentication-and-connected-accounts']; |
| 57 | if (input.sourceType === 'example') return ['tools-actions-and-execution']; |
| 58 | if (input.sourceType === 'reference' || input.sourceType === 'legacy') return ['sdk-api-and-mcp']; |
| 59 | return []; |
| 60 | } |
| 61 | |
| 62 | function inferredIntents(input: KnowledgeClassificationInput): KnowledgeIntent[] { |
| 63 | const intents: KnowledgeIntent[] = []; |
| 64 | if (input.sourceType === 'oauth-guide') intents.push('setup'); |
| 65 | if (input.sourceType === 'docs' || input.sourceType === 'kb' || input.sourceType === 'example') { |
| 66 | intents.push('how-to'); |
| 67 | } |
| 68 | if (input.sourceType === 'toolkit' || input.sourceType === 'reference' || input.sourceType === 'legacy') { |
| 69 | intents.push('reference'); |
| 70 | } |
| 71 | if (input.topics?.includes('incidents-and-known-issues')) intents.push('known-issue'); |
| 72 | if (input.topics?.includes('errors-and-troubleshooting')) intents.push('troubleshooting'); |
| 73 | if (input.topics?.includes('getting-started')) intents.push('setup'); |
| 74 | return unique(intents); |
| 75 | } |
| 76 | |
| 77 | export function normalizeToolkitSlug(value: string): string { |
| 78 | return value.trim().toLowerCase().replace(/\s+/g, '-'); |
| 79 | } |
| 80 | |
| 81 | export function normalizeKnowledgeKeywords(values: string[]): string[] { |
| 82 | return unique(values.map((value) => value.trim()).filter( |
| 83 | (value) => value.length > 0 && !/\brube\b/i.test(value), |
| 84 | )); |
| 85 | } |
| 86 | |
| 87 | export function classifyKnowledgeRecord( |
| 88 | input: KnowledgeClassificationInput, |
| 89 | ): KnowledgeMetadata { |
| 90 | if (!input.canonicalUrl.trim()) throw new Error('Knowledge records require a canonical URL'); |
| 91 | |
| 92 | const explicitAreas = (input.productAreas ?? []).filter(isProductAreaSlug); |
| 93 | const productAreas = unique([...explicitAreas, ...inferredProductAreas(input)]); |
| 94 | const toolkitSlugs = unique( |
| 95 | (input.toolkitSlugs ?? []).map(normalizeToolkitSlug).filter(Boolean), |
| 96 | ); |
| 97 | |
| 98 | return { |
| 99 | source_type: input.sourceType, |
| 100 | canonical_url: input.canonicalUrl, |
| 101 | product_areas: productAreas, |
| 102 | toolkit_slugs: toolkitSlugs, |
| 103 | intents: unique(input.intents?.length ? input.intents : inferredIntents(input)), |
| 104 | last_verified_at: input.lastVerifiedAt?.trim() || null, |
| 105 | }; |
| 106 | } |