Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
refactor(docs): Improve type safety and add validation in LLM markdown
- Extract shared processParams function with proper ParameterSchema typing - Add API response validation before processing - Add warning logs when API calls fail or hit fetch limit - Remove redundant || undefined patterns - Add API_FETCH_LIMIT constant with documentation Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Sushmithamallesh committed
Jan 28, 2026 at 14:24 UTC
5113c3ecf77abd3e29e7604864a777f0d6bd98cc
1 file changed
+50
-38
docs/app/llms.mdx/[[...slug]]/route.ts
+50
-38
@@ -15,6 +15,30 @@ export const revalidate = false;
15
16
const API_BASE = process.env.COMPOSIO_API_BASE || 'https://backend.composio.dev/api/v3';
17
const API_KEY = process.env.COMPOSIO_API_KEY;
18
+const API_FETCH_LIMIT = 1000; // Note: Toolkits with more items will be truncated
19
+
20
+/**
21
+ * Process raw parameter properties into typed ParameterSchema records.
22
+ * Adds required flag based on the requiredList from JSON Schema.
23
+ */
24
+function processParams(
25
+ props: unknown,
26
+ requiredList: string[]
27
+): Record<string, ParameterSchema> | undefined {
28
+ if (!props || typeof props !== 'object') return undefined;
29
+
30
+ const result: Record<string, ParameterSchema> = {};
31
+ for (const [key, value] of Object.entries(props)) {
32
+ if (typeof value === 'object' && value !== null) {
33
+ result[key] = {
34
+ ...(value as ParameterSchema),
35
+ // Explicitly set required based on JSON Schema required array
36
+ required: requiredList.includes(key),
37
+ };
38
+ }
39
+ }
40
+ return Object.keys(result).length > 0 ? result : undefined;
41
+}
42
43
// Fetch detailed tool info from Composio API
44
async function fetchDetailedTools(toolkitSlug: string): Promise<Tool[] | null> {
@@ -24,7 +48,7 @@ async function fetchDetailedTools(toolkitSlug: string): Promise<Tool[] | null> {
48
49
try {
50
const response = await fetch(
27
- `${API_BASE}/tools?toolkit_slug=${toolkitSlug.toUpperCase()}&limit=1000`,
51
+ `${API_BASE}/tools?toolkit_slug=${toolkitSlug.toUpperCase()}&limit=${API_FETCH_LIMIT}`,
52
{
53
headers: {
54
'Content-Type': 'application/json',
@@ -35,13 +59,23 @@ async function fetchDetailedTools(toolkitSlug: string): Promise<Tool[] | null> {
59
);
60
61
if (!response.ok) {
62
+ console.warn(`[LLM Markdown] Failed to fetch tools for ${toolkitSlug}: ${response.status}`);
63
return null;
64
}
65
66
const data = await response.json();
67
+ if (!data || typeof data !== 'object') {
68
+ console.warn(`[LLM Markdown] Invalid API response format for toolkit ${toolkitSlug}`);
69
+ return null;
70
+ }
71
+
72
const rawItems = data.items || data;
73
const items = Array.isArray(rawItems) ? rawItems : [];
74
75
+ if (items.length >= API_FETCH_LIMIT) {
76
+ console.warn(`[LLM Markdown] Toolkit ${toolkitSlug} has ${items.length}+ tools, results may be truncated`);
77
+ }
78
+
79
// eslint-disable-next-line @typescript-eslint/no-explicit-any
80
return items.filter((tool: any) => tool && typeof tool === 'object').map((tool: any) => {
81
const inputSchema = tool.input_parameters || tool.parameters;
@@ -52,30 +86,14 @@ async function fetchDetailedTools(toolkitSlug: string): Promise<Tool[] | null> {
86
const outputProps = outputSchema?.properties || outputSchema;
87
const outputRequired = outputSchema?.required || [];
88
55
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
56
- const processParams = (props: any, requiredList: string[]) => {
57
- if (!props || typeof props !== 'object') return undefined;
58
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
59
- const result: Record<string, any> = {};
60
- for (const [key, value] of Object.entries(props)) {
61
- if (typeof value === 'object' && value !== null) {
62
- result[key] = {
63
- ...(value as object),
64
- required: requiredList.includes(key),
65
- };
66
- }
67
- }
68
- return Object.keys(result).length > 0 ? result : undefined;
69
- };
70
-
89
return {
90
slug: tool.slug || '',
91
name: tool.name || tool.display_name || tool.slug || '',
92
description: tool.description || '',
93
input_parameters: processParams(inputProps, inputRequired),
94
output_parameters: processParams(outputProps, outputRequired),
77
- scopes: tool.scopes || undefined,
78
- tags: tool.tags || undefined,
95
+ scopes: tool.scopes,
96
+ tags: tool.tags,
97
is_deprecated: tool.is_deprecated || false,
98
};
99
});
@@ -92,7 +110,7 @@ async function fetchDetailedTriggers(toolkitSlug: string): Promise<Trigger[] | n
110
111
try {
112
const response = await fetch(
95
- `${API_BASE}/triggers_types?toolkit_slugs=${toolkitSlug.toUpperCase()}&limit=1000`,
113
+ `${API_BASE}/triggers_types?toolkit_slugs=${toolkitSlug.toUpperCase()}&limit=${API_FETCH_LIMIT}`,
114
{
115
headers: {
116
'Content-Type': 'application/json',
@@ -103,13 +121,23 @@ async function fetchDetailedTriggers(toolkitSlug: string): Promise<Trigger[] | n
121
);
122
123
if (!response.ok) {
124
+ console.warn(`[LLM Markdown] Failed to fetch triggers for ${toolkitSlug}: ${response.status}`);
125
return null;
126
}
127
128
const data = await response.json();
129
+ if (!data || typeof data !== 'object') {
130
+ console.warn(`[LLM Markdown] Invalid API response format for triggers ${toolkitSlug}`);
131
+ return null;
132
+ }
133
+
134
const rawItems = data.items || data;
135
const items = Array.isArray(rawItems) ? rawItems : [];
136
137
+ if (items.length >= API_FETCH_LIMIT) {
138
+ console.warn(`[LLM Markdown] Toolkit ${toolkitSlug} has ${items.length}+ triggers, results may be truncated`);
139
+ }
140
+
141
// eslint-disable-next-line @typescript-eslint/no-explicit-any
142
return items.filter((trigger: any) => trigger && typeof trigger === 'object').map((trigger: any) => {
143
const configSchema = trigger.config;
@@ -120,30 +148,14 @@ async function fetchDetailedTriggers(toolkitSlug: string): Promise<Trigger[] | n
148
const payloadProps = payloadSchema?.properties || payloadSchema;
149
const payloadRequired = payloadSchema?.required || [];
150
123
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
124
- const processParams = (props: any, requiredList: string[]) => {
125
- if (!props || typeof props !== 'object') return undefined;
126
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
127
- const result: Record<string, any> = {};
128
- for (const [key, value] of Object.entries(props)) {
129
- if (typeof value === 'object' && value !== null) {
130
- result[key] = {
131
- ...(value as object),
132
- required: requiredList.includes(key),
133
- };
134
- }
135
- }
136
- return Object.keys(result).length > 0 ? result : undefined;
137
- };
138
-
151
return {
152
slug: trigger.slug || '',
153
name: trigger.name || trigger.display_name || trigger.slug || '',
154
description: trigger.description || '',
143
- type: trigger.type || undefined,
155
+ type: trigger.type,
156
config: processParams(configProps, configRequired),
157
payload: processParams(payloadProps, payloadRequired),
146
- instructions: trigger.instructions || undefined,
158
+ instructions: trigger.instructions,
159
};
160
});
161
} catch {