@devries-cameron20 / composio Mirror · read-only
Fork 0

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, 04:45 UTC

next
tsx 413 lines 14.1 KB
Raw
1 /* oxlint-disable next/no-img-element -- Satori renders raw <img>, next/image does not apply here */
2 import { ImageResponse } from 'next/og';
3 import { readFile } from 'fs/promises';
4 import { join } from 'path';
5
6 // Social preview cards for every docs page. One shared shell, one slot per
7 // section. Palette and type follow app/global.css: dark DEV surface by
8 // default, light on request, deep electric blue brand accent, square corners.
9 // Geist Sans / Mono are vendored in app/fonts because Satori cannot read the
10 // site's woff2 files.
11
12 export const OG_WIDTH = 1200;
13 export const OG_HEIGHT = 630;
14
15 const THEMES = {
16 dark: {
17 ground: '#0f0f0f',
18 card: '#1a1a1a',
19 ink: '#fafafa',
20 soft: '#d4d4d4',
21 muted: '#a1a1a1',
22 line: '#2a2a2a',
23 brand: '#51a2ff',
24 wordmarkFile: 'Composio Logo Dark.svg',
25 logoTheme: 'dark',
26 backgroundFile: 'og/bg-dark.jpg' as string | null,
27 },
28 light: {
29 ground: '#fbfbfb',
30 card: '#ffffff',
31 ink: '#0f0f0f',
32 soft: '#3d3d3d',
33 muted: '#6b6b6b',
34 line: '#e5e5e5',
35 brand: '#0007cd',
36 wordmarkFile: 'Composio Logo.svg',
37 logoTheme: null,
38 backgroundFile: null as string | null,
39 },
40 } as const;
41 export type OgTheme = keyof typeof THEMES;
42 type Palette = (typeof THEMES)[OgTheme];
43
44 const SECTIONS = ['docs', 'toolkits', 'reference', 'changelog', 'home'] as const;
45 export type OgSection = (typeof SECTIONS)[number];
46
47 const SECTION_LABELS: Record<OgSection, string> = {
48 docs: 'Docs',
49 toolkits: 'Toolkits',
50 reference: 'API Reference',
51 changelog: 'Changelog',
52 home: 'Docs',
53 };
54
55 const LOGO_HOSTS = new Set(['logos.composio.dev', 'assets.composio.dev']);
56
57 const TITLE_MAX = 120;
58 const DESCRIPTION_MAX = 200;
59
60 // --- assets -----------------------------------------------------------------
61
62 // Resolved per call, not at import time, so the app root is whatever cwd is
63 // when the first request lands (matches lib/toolkit-data.ts).
64 const fontsDir = () => join(process.cwd(), 'app/fonts');
65 const publicDir = () => join(process.cwd(), 'public');
66
67 type FontSpec = { name: string; data: ArrayBuffer; weight: 400 | 500; style: 'normal' };
68
69 async function loadFont(file: string, name: string, weight: 400 | 500): Promise<FontSpec | null> {
70 try {
71 const buf = await readFile(join(fontsDir(), file));
72 return { name, data: buf.buffer.slice(buf.byteOffset, buf.byteOffset + buf.byteLength), weight, style: 'normal' };
73 } catch {
74 return null;
75 }
76 }
77
78 let fontsPromise: Promise<FontSpec[]> | null = null;
79 function getFonts(): Promise<FontSpec[]> {
80 if (!fontsPromise) {
81 fontsPromise = Promise.all([
82 loadFont('Geist-Regular.ttf', 'Geist', 400),
83 loadFont('Geist-Medium.ttf', 'Geist', 500),
84 loadFont('GeistMono-Regular.ttf', 'Geist Mono', 400),
85 loadFont('GeistMono-Medium.ttf', 'Geist Mono', 500),
86 ]).then((fonts) => fonts.filter((f): f is FontSpec => f !== null));
87 }
88 return fontsPromise;
89 }
90
91 interface Brand {
92 /** Full wordmark as a data URI. */
93 wordmark: string | null;
94 /** Icon glyph only, as a data URI. */
95 mark: string | null;
96 /** Full-bleed background as a data URI, when the theme has one. */
97 background: string | null;
98 }
99
100 const toDataUri = (svg: string) => `data:image/svg+xml;base64,${Buffer.from(svg).toString('base64')}`;
101
102 const brandPromises: Partial<Record<OgTheme, Promise<Brand>>> = {};
103 function getBrand(theme: OgTheme): Promise<Brand> {
104 let promise = brandPromises[theme];
105 if (!promise) {
106 const { wordmarkFile, backgroundFile } = THEMES[theme];
107 const background = backgroundFile
108 ? readFile(join(publicDir(), backgroundFile))
109 .then((buf) => `data:image/jpeg;base64,${buf.toString('base64')}`)
110 .catch(() => null)
111 : Promise.resolve(null);
112 promise = Promise.all([readFile(join(publicDir(), wordmarkFile), 'utf8').catch(() => null), background])
113 .then(([svg, background]) => {
114 if (!svg) return { wordmark: null, mark: null, background };
115 // The first two <path>s of the wordmark are the icon glyph; the rest are letterforms.
116 const paths = svg.match(/<path[^>]*\/>/g)?.slice(0, 2) ?? [];
117 const mark = paths.length === 2
118 ? toDataUri(`<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 104 121" fill="none">${paths.join('')}</svg>`)
119 : null;
120 return { wordmark: toDataUri(svg), mark, background };
121 });
122 brandPromises[theme] = promise;
123 }
124 return promise;
125 }
126
127 // --- params -----------------------------------------------------------------
128
129 export interface OgParams {
130 section: OgSection;
131 theme: OgTheme;
132 title: string;
133 description: string;
134 logo: string | null;
135 date: string | null;
136 version: string | null;
137 }
138
139 function clip(value: string | null, max: number): string {
140 return Array.from((value ?? '').trim()).slice(0, max).join('');
141 }
142
143 function safeLogo(value: string | null, theme: OgTheme): string | null {
144 if (!value) return null;
145 try {
146 const url = new URL(value);
147 if (url.protocol !== 'https:' || !LOGO_HOSTS.has(url.hostname)) return null;
148 const logoTheme = THEMES[theme].logoTheme;
149 if (logoTheme) url.searchParams.set('theme', logoTheme);
150 return url.toString();
151 } catch {
152 return null;
153 }
154 }
155
156 export function parseOgParams(request: Request): OgParams {
157 const params = new URL(request.url).searchParams;
158 const rawSection = params.get('section') ?? (params.get('variant') === 'home' ? 'home' : 'docs');
159 const section = (SECTIONS as readonly string[]).includes(rawSection) ? (rawSection as OgSection) : 'docs';
160 const theme: OgTheme = params.get('theme') === 'light' ? 'light' : 'dark';
161 const title = clip(params.get('title'), TITLE_MAX) || (section === 'home' ? '' : 'Composio Docs');
162 const description = clip(params.get('description'), DESCRIPTION_MAX);
163 return {
164 section,
165 theme,
166 title,
167 // Some pages fall back to their title as the meta description; never print it twice.
168 description: description.toLowerCase() === title.toLowerCase() ? '' : description,
169 logo: safeLogo(params.get('logo'), theme),
170 date: clip(params.get('date'), 40) || null,
171 version: clip(params.get('version'), 12) || null,
172 };
173 }
174
175 // --- primitives -------------------------------------------------------------
176
177 const sans = 'Geist, ui-sans-serif, system-ui, sans-serif';
178 const mono = '"Geist Mono", ui-monospace, SFMono-Regular, Menlo, monospace';
179
180 interface Ctx {
181 c: Palette;
182 brand: Brand;
183 }
184
185 function Stack({ children, gap = 24 }: { children: React.ReactNode; gap?: number }) {
186 // Satori flattens fragments into the parent's flex row, so every card
187 // slot wraps its pieces in an explicit column.
188 return <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', textAlign: 'center', gap }}>{children}</div>;
189 }
190
191 function Eyebrow({ children, color }: { children: string; color: string }) {
192 return (
193 <div style={{ display: 'flex', fontFamily: mono, fontSize: 20, fontWeight: 500, letterSpacing: 2.4, textTransform: 'uppercase', color }}>
194 {children}
195 </div>
196 );
197 }
198
199 function Chip({ children, c }: { children: string; c: Palette }) {
200 return (
201 <div style={{
202 display: 'flex', alignItems: 'center', padding: '8px 14px',
203 border: `1px solid ${c.line}`, background: c.card,
204 fontFamily: mono, fontSize: 18, letterSpacing: 1.2, textTransform: 'uppercase', color: c.muted,
205 }}>
206 {children}
207 </div>
208 );
209 }
210
211 // Satori balances by shrinking the measured box until the line count would
212 // grow. Text with no break opportunity never wraps, so the box shrinks below
213 // the drawn glyphs and flex centering shifts the text right. Balance only
214 // text that can actually wrap.
215 function balanceIfBreakable(text: string) {
216 return /\s/.test(text) ? ({ textWrap: 'balance' } as const) : {};
217 }
218
219 function Title({ children, size, c }: { children: string; size: number; c: Palette }) {
220 return (
221 <div style={{
222 display: 'flex', fontFamily: sans, fontWeight: 500, fontSize: size, lineHeight: 1.08,
223 letterSpacing: size >= 60 ? -2 : -1.2, color: c.ink, maxWidth: 1000, lineClamp: 3,
224 justifyContent: 'center', textAlign: 'center', ...balanceIfBreakable(children),
225 }}>
226 {children}
227 </div>
228 );
229 }
230
231 function titleSize(title: string, base = 72): number {
232 const n = title.length;
233 if (n > 90) return base - 28;
234 if (n > 60) return base - 16;
235 if (n > 36) return base - 8;
236 return base;
237 }
238
239 function Description({ children, c }: { children: string; c: Palette }) {
240 if (!children) return null;
241 return (
242 <div style={{
243 display: 'flex', fontFamily: sans, fontSize: 30, lineHeight: 1.4, color: c.soft, maxWidth: 900, lineClamp: 2,
244 justifyContent: 'center', textAlign: 'center', ...balanceIfBreakable(children),
245 }}>
246 {children}
247 </div>
248 );
249 }
250
251 function Node({ children, c }: { children: React.ReactNode; c: Palette }) {
252 return (
253 <div style={{
254 display: 'flex', alignItems: 'center', justifyContent: 'center',
255 width: 112, height: 112, background: c.card, border: `1px solid ${c.line}`,
256 }}>
257 {children}
258 </div>
259 );
260 }
261
262 // --- shell ------------------------------------------------------------------
263
264 function Shell({ ctx, label, children }: { ctx: Ctx; label: string; children: React.ReactNode }) {
265 const { c, brand } = ctx;
266 return (
267 <div style={{
268 display: 'flex', flexDirection: 'column', justifyContent: 'space-between',
269 width: '100%', height: '100%', padding: '56px 64px',
270 background: c.ground, color: c.ink, fontFamily: sans, position: 'relative',
271 }}>
272 {brand.background ? (
273 <img src={brand.background} width={OG_WIDTH} height={OG_HEIGHT} alt=""
274 style={{ position: 'absolute', top: 0, left: 0, width: OG_WIDTH, height: OG_HEIGHT, opacity: 0.7 }} />
275 ) : null}
276 <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center' }}>
277 {brand.wordmark ? (
278 <img src={brand.wordmark} width={165} height={30} alt="" />
279 ) : (
280 <div style={{ display: 'flex', fontSize: 28, fontWeight: 500 }}>Composio</div>
281 )}
282 <Eyebrow color={c.brand}>{label}</Eyebrow>
283 </div>
284
285 <div style={{
286 display: 'flex', flexDirection: 'column', gap: 24, flexGrow: 1,
287 justifyContent: 'center', alignItems: 'center', textAlign: 'center', paddingTop: 24,
288 }}>
289 {children}
290 </div>
291 </div>
292 );
293 }
294
295 // --- section slots ----------------------------------------------------------
296
297 type CardProps = { p: OgParams; ctx: Ctx };
298
299 function DocsCard({ p, ctx: { c } }: CardProps) {
300 return (
301 <Stack>
302 <Title size={titleSize(p.title)} c={c}>{p.title}</Title>
303 <Description c={c}>{p.description}</Description>
304 </Stack>
305 );
306 }
307
308 const TOOLKIT_SUFFIX = /\s*-\s*Composio Toolkit\s*$/i;
309
310 /**
311 * Catalog toolkit pages title themselves "<Name> - Composio Toolkit"; the card
312 * shows "<Name> Toolkit". Any other page in the section (the index, MDX
313 * guides like "Premium Tools") keeps its title as written.
314 */
315 export function toolkitCardTitle(title: string): string {
316 return TOOLKIT_SUFFIX.test(title) ? `${title.replace(TOOLKIT_SUFFIX, '')} Toolkit` : title;
317 }
318
319 function ToolkitCard({ p, ctx: { c, brand } }: CardProps) {
320 const name = p.title.replace(TOOLKIT_SUFFIX, '');
321 const title = toolkitCardTitle(p.title);
322 return (
323 <Stack>
324 {p.logo ? (
325 <div style={{ display: 'flex', alignItems: 'center', marginBottom: 16 }}>
326 <Node c={c}>
327 {brand.mark ? <img src={brand.mark} width={56} height={65} alt="" /> : <div style={{ display: 'flex', fontSize: 40, fontWeight: 500 }}>C</div>}
328 </Node>
329 <div style={{ display: 'flex', width: 72, height: 4, background: c.muted }} />
330 <Node c={c}>
331 <img src={p.logo} width={72} height={72} alt="" style={{ objectFit: 'contain' }} />
332 </Node>
333 </div>
334 ) : null}
335 <Title size={titleSize(title, 64)} c={c}>{title}</Title>
336 <Description c={c}>{p.description || `Connect your AI agent to ${name} with Composio.`}</Description>
337 </Stack>
338 );
339 }
340
341 function ReferenceCard({ p, ctx: { c } }: CardProps) {
342 return (
343 <Stack>
344 <div style={{ display: 'flex', justifyContent: 'center', gap: 10 }}>
345 <div style={{
346 display: 'flex', alignItems: 'center', padding: '8px 14px',
347 background: c.brand, color: c.ground,
348 fontFamily: mono, fontSize: 18, fontWeight: 500, letterSpacing: 1.2,
349 }}>
350 REST API
351 </div>
352 {p.version ? <Chip c={c}>{p.version}</Chip> : null}
353 </div>
354 <Title size={titleSize(p.title)} c={c}>{p.title}</Title>
355 <Description c={c}>{p.description}</Description>
356 </Stack>
357 );
358 }
359
360 function ChangelogCard({ p, ctx: { c } }: CardProps) {
361 return (
362 <Stack>
363 {p.date ? <Eyebrow color={c.muted}>{p.date}</Eyebrow> : null}
364 <Title size={titleSize(p.title)} c={c}>{p.title}</Title>
365 {/* The page description is "Updates from <date>", which the eyebrow already says. */}
366 {p.date ? null : <Description c={c}>{p.description}</Description>}
367 </Stack>
368 );
369 }
370
371 function HomeCard({ p, ctx: { c } }: CardProps) {
372 return (
373 <Stack>
374 <div style={{
375 display: 'flex', flexDirection: 'column', alignItems: 'center',
376 fontFamily: sans, fontWeight: 500, fontSize: 84, lineHeight: 1.02, letterSpacing: -3, color: c.ink,
377 }}>
378 <span>Build and operate</span>
379 <span style={{ color: c.brand }}>AI agents.</span>
380 </div>
381 <Description c={c}>{p.description || 'Tools, managed authentication, and secure execution for your agents.'}</Description>
382 </Stack>
383 );
384 }
385
386 const CARDS: Record<OgSection, (props: CardProps) => React.ReactElement> = {
387 docs: DocsCard,
388 toolkits: ToolkitCard,
389 reference: ReferenceCard,
390 changelog: ChangelogCard,
391 home: HomeCard,
392 };
393
394 // --- handler ----------------------------------------------------------------
395
396 export async function GET(request: Request) {
397 const p = parseOgParams(request);
398 const [fonts, brand] = await Promise.all([getFonts(), getBrand(p.theme)]);
399 const ctx: Ctx = { c: THEMES[p.theme], brand };
400 const Card = CARDS[p.section];
401
402 return new ImageResponse(
403 <Shell ctx={ctx} label={SECTION_LABELS[p.section]}>
404 <Card p={p} ctx={ctx} />
405 </Shell>,
406 {
407 width: OG_WIDTH,
408 height: OG_HEIGHT,
409 fonts,
410 headers: { 'Cache-Control': 'public, max-age=86400, s-maxage=86400' },
411 },
412 );
413 }