Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

next
ts 232 lines 7.08 KB
Raw
1 import {
2 source,
3 examplesSource,
4 referenceSource,
5 toolkitsSource,
6 knowledgeBaseSource,
7 } from '@/lib/source';
8 import { detectReferenceApiVersion } from '@/lib/api-version';
9 import { TOOLKIT_COUNT_LABEL } from '@/lib/toolkit-count';
10 import {
11 formatKnowledgeDiscoveryLinks,
12 getLocalKnowledgeDiscoveryPaths,
13 } from '@/lib/knowledge/discovery';
14 import type { ReactNode } from 'react';
15
16 export const revalidate = false;
17
18 // Fumadocs page tree node types
19 interface PageNode {
20 type: 'page';
21 name: ReactNode;
22 url: string;
23 external?: boolean;
24 }
25
26 interface SeparatorNode {
27 type: 'separator';
28 name?: ReactNode;
29 }
30
31 interface FolderNode {
32 type: 'folder';
33 name: ReactNode;
34 index?: PageNode;
35 children: TreeNode[];
36 }
37
38 type TreeNode = PageNode | SeparatorNode | FolderNode;
39
40 /** Extract plain text from a ReactNode (handles strings, numbers, skips elements). */
41 function nodeText(name: ReactNode): string | null {
42 if (typeof name === 'string') return name;
43 if (typeof name === 'number') return String(name);
44 return null;
45 }
46
47 /**
48 * A section is legacy/deprecated when its separator heading says so (e.g.
49 * "Direct Tool Execution Guides (Legacy)"). We omit those sections from the
50 * default LLM index so code generators reach for the current session-based
51 * APIs, not deprecated ones.
52 */
53 function isLegacySeparator(name: ReactNode): boolean {
54 const text = nodeText(name);
55 return text != null && /legacy|deprecated/i.test(text);
56 }
57
58 /**
59 * Walk the fumadocs page tree and generate a markdown index.
60 * Separators become ## headings, pages become URL entries, folders recurse.
61 * Legacy/deprecated sections (and everything under them) are skipped.
62 *
63 * A folder emits a heading one level deeper, and markdown headings do not
64 * close — so a plain page emitted after a folder reads as belonging to that
65 * folder. `meta.json` order is the human sidebar's order and cannot absorb
66 * this, so each section's direct pages are buffered and flushed ahead of its
67 * folders. Within pages, and within folders, `meta.json` order is preserved.
68 */
69 function walkPageTree(nodes: TreeNode[], depth = 2): string {
70 const lines: string[] = [];
71 let skippingSection = false;
72 let sectionPages: string[] = [];
73 let sectionFolders: string[] = [];
74
75 function flushSection() {
76 lines.push(...sectionPages, ...sectionFolders);
77 sectionPages = [];
78 sectionFolders = [];
79 }
80
81 for (const node of nodes) {
82 if (node.type === 'separator') {
83 // A separator starts a new section; skip it and its pages when legacy.
84 flushSection();
85 skippingSection = isLegacySeparator(node.name);
86 if (skippingSection) continue;
87 const text = nodeText(node.name);
88 if (text) {
89 lines.push('', `${'#'.repeat(depth)} ${text}`, '');
90 }
91 continue;
92 }
93
94 if (skippingSection) continue;
95
96 switch (node.type) {
97 case 'page':
98 if (!node.external) {
99 sectionPages.push(formatPage({ url: node.url, data: source.getPageByHref(node.url)?.page.data }));
100 }
101 break;
102
103 case 'folder': {
104 // Folders are sub-sections within separator sections, so one level deeper
105 const text = nodeText(node.name);
106 if (text) {
107 sectionFolders.push('', `${'#'.repeat(depth + 1)} ${text}`, '');
108 }
109 // If folder has an index page, include it
110 if (node.index && !node.index.external) {
111 sectionFolders.push(formatPage({ url: node.index.url, data: source.getPageByHref(node.index.url)?.page.data }));
112 }
113 // Recurse into children
114 if (node.children.length > 0) {
115 sectionFolders.push(walkPageTree(node.children, depth + 1));
116 }
117 break;
118 }
119 }
120 }
121
122 flushSection();
123
124 return lines
125 .join('\n')
126 .replace(/\n{3,}/g, '\n\n')
127 .trim();
128 }
129
130 function formatPage(page: { url: string; data?: { title?: string; description?: string } }) {
131 const title = (page.data?.title ?? page.url).replace(/[\[\]\r\n]/g, ' ');
132 const description = page.data?.description?.replace(/[\r\n]/g, ' ');
133 return `- [${title}](https://docs.composio.dev${page.url}.md)${description ? `: ${description}` : ''}`;
134 }
135
136 /**
137 * Splits reference pages by REST version.
138 *
139 * This list is built with a flat `.map(formatPage)` that bypasses the
140 * `isLegacySeparator` filter the guides tree gets, so the two trees used to
141 * arrive interleaved under one unlabelled heading — and `/reference/v3/` is
142 * the only version-shaped path in the corpus, so it is the highest-precision
143 * match for the query most readers intend as "current Composio".
144 *
145 * Partitioned with `detectApiVersion`, not a literal path test, so moving the
146 * legacy tree's URL stays a one-line change in `lib/api-version.ts`.
147 */
148 function partitionByApiVersion<T extends { url: string }>(pages: T[]) {
149 const current: T[] = [];
150 const legacy: T[] = [];
151 const versionIndependent: T[] = [];
152 for (const page of pages) {
153 const version = detectReferenceApiVersion(page.url);
154 if (version === '3.0') legacy.push(page);
155 else if (version === '3.1') current.push(page);
156 else versionIndependent.push(page);
157 }
158 return { current, legacy, versionIndependent };
159 }
160
161 export async function GET() {
162 try {
163 const docsTree = walkPageTree(source.pageTree.children as TreeNode[]);
164
165 const examplesPages = examplesSource.getPages();
166 const toolkitsPages = toolkitsSource.getPages();
167 const {
168 current: currentReferencePages,
169 legacy: legacyReferencePages,
170 versionIndependent: versionIndependentReferencePages,
171 } = partitionByApiVersion(referenceSource.getPages());
172
173 const legacyReferenceSection =
174 legacyReferencePages.length > 0
175 ? `
176 ## API Reference (v3.0, legacy — supported, not for new code)
177
178 ${legacyReferencePages.map(formatPage).join('\n')}
179 `
180 : '';
181 const knowledgeBasePages = knowledgeBaseSource.getPages();
182 const knowledgeDiscoveryLinks = formatKnowledgeDiscoveryLinks(
183 (await getLocalKnowledgeDiscoveryPaths()).filter(
184 (path) => !path.startsWith('/kb/guide/'),
185 ),
186 );
187
188 const index = `# Composio Documentation
189
190 > Composio powers ${TOOLKIT_COUNT_LABEL} apps, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
191
192 For a shorter routing map, start at [llms.txt](https://docs.composio.dev/llms.txt).
193
194 ${docsTree}
195
196 ## Examples
197
198 ${examplesPages.map(formatPage).join('\n')}
199
200 ## Knowledge Base
201
202 ${knowledgeDiscoveryLinks}
203
204 ${knowledgeBasePages.map(formatPage).join('\n')}
205
206 ## API Reference (v3.1, current)
207
208 ${currentReferencePages.map(formatPage).join('\n')}
209 ${legacyReferenceSection}
210 ## SDK and product reference (version-independent)
211
212 ${versionIndependentReferencePages.map(formatPage).join('\n')}
213
214 ## Toolkits
215
216 ${toolkitsPages.map(formatPage).join('\n')}
217
218 ## Full Documentation
219
220 - [Full documentation](https://docs.composio.dev/llms-full.txt): All current page bodies.
221 `;
222
223 return new Response(index, {
224 headers: {
225 'Content-Type': 'text/plain; charset=utf-8',
226 },
227 });
228 } catch (error) {
229 console.error('Error generating llms.txt:', error);
230 return new Response('Error generating documentation index', { status: 500 });
231 }
232 }