@ontosdk/next 1.3.0 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/cli.ts CHANGED
@@ -4,6 +4,24 @@ import fs from 'fs';
4
4
  import path from 'path';
5
5
  import pc from 'picocolors';
6
6
  import { extractContent } from './extractor';
7
+ import { generateLlmsTxt, OntoConfig } from './config';
8
+
9
+ async function loadOntoConfig(): Promise<OntoConfig | null> {
10
+ try {
11
+ const configPath = path.join(process.cwd(), 'onto.config');
12
+ // Node.js dynamic import
13
+ const config = await import('file://' + configPath.replace(/\\/g, '/') + '.ts');
14
+ return config.default || config;
15
+ } catch (error) {
16
+ try {
17
+ const configPath = path.join(process.cwd(), 'onto.config');
18
+ const config = await import('file://' + configPath.replace(/\\/g, '/') + '.js');
19
+ return config.default || config;
20
+ } catch (e) {
21
+ return null;
22
+ }
23
+ }
24
+ }
7
25
 
8
26
  // Simple helper to load .env.local from the current working directory
9
27
  function loadEnv() {
@@ -139,6 +157,22 @@ async function main() {
139
157
  }
140
158
  }
141
159
 
160
+ // --- Generate llms.txt manifest ---
161
+ const config = await loadOntoConfig();
162
+ if (config) {
163
+ const llmsTxtContent = generateLlmsTxt(config);
164
+ const llmsTxtPath = path.join(cwd, 'public/llms.txt');
165
+
166
+ // Ensure public dir exists
167
+ const publicDir = path.join(cwd, 'public');
168
+ if (!fs.existsSync(publicDir)) {
169
+ fs.mkdirSync(publicDir, { recursive: true });
170
+ }
171
+
172
+ fs.writeFileSync(llmsTxtPath, llmsTxtContent, 'utf8');
173
+ console.log(pc.green('✓ Generated') + pc.dim(' /llms.txt'));
174
+ }
175
+
142
176
  console.log(pc.dim(`Edge payloads are ready at /public/.onto/*\n`));
143
177
  }
144
178
 
package/src/config.ts CHANGED
@@ -78,22 +78,6 @@ export interface OntoConfig {
78
78
  };
79
79
  }
80
80
 
81
- /**
82
- * Load the onto.config.ts file from the user's project
83
- * This is used by the middleware to dynamically generate llms.txt
84
- */
85
- export async function loadOntoConfig(): Promise<OntoConfig | null> {
86
- try {
87
- // Try to dynamically import the config file from the user's project root
88
- // This runs in the middleware context, so we look in the project root
89
- const config = await import(process.cwd() + '/onto.config');
90
- return config.default || config;
91
- } catch (error) {
92
- // Config file doesn't exist or failed to load
93
- return null;
94
- }
95
- }
96
-
97
81
  /**
98
82
  * Generate llms.txt content from OntoConfig
99
83
  * Follows the llms.txt specification:
package/src/middleware.ts CHANGED
@@ -1,18 +1,19 @@
1
1
  import { NextRequest, NextResponse } from 'next/server';
2
- import { AI_BOT_USER_AGENTS, matchBot } from './bots';
3
- import { loadOntoConfig, generateLlmsTxt } from './config';
2
+ import { matchBot } from './bots';
3
+ import { generateLlmsTxt, OntoConfig } from './config';
4
4
 
5
- export async function ontoMiddleware(request: NextRequest) {
5
+ export async function ontoMiddleware(request: NextRequest, config?: OntoConfig) {
6
6
  const userAgent = request.headers.get('user-agent');
7
+ const url = request.nextUrl.clone();
8
+ const matched = matchBot(userAgent);
9
+
7
10
  const accept = request.headers.get('accept') || '';
8
11
 
9
- const matched = matchBot(userAgent);
10
12
  const isAiBot = !!matched;
11
13
  const isMarkdownRequested = accept.includes('text/markdown');
12
14
 
13
15
  // If traffic is identified as an AI Bot or markdown is requested
14
16
  if (isAiBot || isMarkdownRequested) {
15
- const url = request.nextUrl.clone();
16
17
 
17
18
  // Ignore internal next.js requests & static assets (but not llms.txt)
18
19
  if (url.pathname.startsWith('/_next')) {
@@ -20,11 +21,9 @@ export async function ontoMiddleware(request: NextRequest) {
20
21
  }
21
22
 
22
23
  // --- llms.txt Auto-Discovery ---
23
- // Dynamically generate llms.txt from onto.config.ts
24
+ // Serve the llms.txt manifest to AI agents
24
25
  if (url.pathname === '/llms.txt') {
25
26
  try {
26
- const config = await loadOntoConfig();
27
-
28
27
  if (config) {
29
28
  // Generate llms.txt dynamically from config
30
29
  const llmsTxtContent = generateLlmsTxt(config);
@@ -1,27 +0,0 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
-
3
- /**
4
- * OntoHead — Auto-Discovery component for AI agents.
5
- *
6
- * Injects `<link rel="alternate">` tags into the page `<head>` so AI crawlers
7
- * can discover the optimized markdown endpoint for the current route.
8
- *
9
- * Usage in a Next.js App Router layout:
10
- * ```tsx
11
- * import { OntoHead } from '@ontosdk/next/components';
12
- *
13
- * export default function RootLayout({ children }) {
14
- * return (
15
- * <html>
16
- * <head>
17
- * <OntoHead />
18
- * </head>
19
- * <body>{children}</body>
20
- * </html>
21
- * );
22
- * }
23
- * ```
24
- */
25
- declare function OntoHead(): react_jsx_runtime.JSX.Element;
26
-
27
- export { OntoHead };
@@ -1,27 +0,0 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
-
3
- /**
4
- * OntoHead — Auto-Discovery component for AI agents.
5
- *
6
- * Injects `<link rel="alternate">` tags into the page `<head>` so AI crawlers
7
- * can discover the optimized markdown endpoint for the current route.
8
- *
9
- * Usage in a Next.js App Router layout:
10
- * ```tsx
11
- * import { OntoHead } from '@ontosdk/next/components';
12
- *
13
- * export default function RootLayout({ children }) {
14
- * return (
15
- * <html>
16
- * <head>
17
- * <OntoHead />
18
- * </head>
19
- * <body>{children}</body>
20
- * </html>
21
- * );
22
- * }
23
- * ```
24
- */
25
- declare function OntoHead(): react_jsx_runtime.JSX.Element;
26
-
27
- export { OntoHead };
@@ -1,52 +0,0 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import { ReactNode } from 'react';
3
- import { OntoConfig } from './config.mjs';
4
-
5
- interface OntoProviderProps {
6
- /**
7
- * The base URL of your site (e.g., 'https://example.com')
8
- * Used to construct the full href for the AI discovery link tag.
9
- */
10
- baseUrl: string;
11
- /**
12
- * Child components to render
13
- */
14
- children: ReactNode;
15
- /**
16
- * Optional: Onto configuration for automatic JSON-LD schema injection
17
- * If provided, the provider will automatically inject JSON-LD schemas
18
- * based on the page type configuration
19
- */
20
- config?: OntoConfig;
21
- }
22
- /**
23
- * OntoProvider — Automatic AI Discovery Provider
24
- *
25
- * Wraps your application and automatically injects:
26
- * 1. `<link rel="alternate">` tags for AI discovery
27
- * 2. JSON-LD structured data schemas based on page type
28
- *
29
- * With config, automatically generates JSON-LD schemas:
30
- * - 'scoring' pages get Methodology schema with AIO weights (40/35/25)
31
- * - 'about' pages get Organization/AboutPage schema
32
- *
33
- * Usage in a Next.js App Router layout:
34
- * ```tsx
35
- * import { OntoProvider } from '@ontosdk/next/provider';
36
- * import config from '../onto.config';
37
- *
38
- * export default function RootLayout({ children }) {
39
- * return (
40
- * <OntoProvider baseUrl="https://example.com" config={config}>
41
- * <html>
42
- * <head />
43
- * <body>{children}</body>
44
- * </html>
45
- * </OntoProvider>
46
- * );
47
- * }
48
- * ```
49
- */
50
- declare function OntoProvider({ baseUrl, children, config }: OntoProviderProps): react_jsx_runtime.JSX.Element;
51
-
52
- export { OntoProvider, type OntoProviderProps };
@@ -1,52 +0,0 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
- import { ReactNode } from 'react';
3
- import { OntoConfig } from './config.js';
4
-
5
- interface OntoProviderProps {
6
- /**
7
- * The base URL of your site (e.g., 'https://example.com')
8
- * Used to construct the full href for the AI discovery link tag.
9
- */
10
- baseUrl: string;
11
- /**
12
- * Child components to render
13
- */
14
- children: ReactNode;
15
- /**
16
- * Optional: Onto configuration for automatic JSON-LD schema injection
17
- * If provided, the provider will automatically inject JSON-LD schemas
18
- * based on the page type configuration
19
- */
20
- config?: OntoConfig;
21
- }
22
- /**
23
- * OntoProvider — Automatic AI Discovery Provider
24
- *
25
- * Wraps your application and automatically injects:
26
- * 1. `<link rel="alternate">` tags for AI discovery
27
- * 2. JSON-LD structured data schemas based on page type
28
- *
29
- * With config, automatically generates JSON-LD schemas:
30
- * - 'scoring' pages get Methodology schema with AIO weights (40/35/25)
31
- * - 'about' pages get Organization/AboutPage schema
32
- *
33
- * Usage in a Next.js App Router layout:
34
- * ```tsx
35
- * import { OntoProvider } from '@ontosdk/next/provider';
36
- * import config from '../onto.config';
37
- *
38
- * export default function RootLayout({ children }) {
39
- * return (
40
- * <OntoProvider baseUrl="https://example.com" config={config}>
41
- * <html>
42
- * <head />
43
- * <body>{children}</body>
44
- * </html>
45
- * </OntoProvider>
46
- * );
47
- * }
48
- * ```
49
- */
50
- declare function OntoProvider({ baseUrl, children, config }: OntoProviderProps): react_jsx_runtime.JSX.Element;
51
-
52
- export { OntoProvider, type OntoProviderProps };
package/dist/cli.d.mts DELETED
@@ -1 +0,0 @@
1
- #!/usr/bin/env node
package/dist/cli.d.ts DELETED
@@ -1 +0,0 @@
1
- #!/usr/bin/env node
package/dist/config.d.mts DELETED
@@ -1,85 +0,0 @@
1
- /**
2
- * Configuration schema for onto.config.ts
3
- * Used to dynamically generate llms.txt and other AI discovery files
4
- */
5
- type PageType = 'scoring' | 'about' | 'default';
6
- interface OntoRoute {
7
- /**
8
- * The URL path (e.g., '/docs', '/api/reference')
9
- */
10
- path: string;
11
- /**
12
- * Description of what this route contains
13
- */
14
- description: string;
15
- /**
16
- * Optional: Page type for automatic JSON-LD schema injection
17
- * - 'scoring': Injects Methodology schema with AIO scoring weights (40/35/25)
18
- * - 'about': Injects Organization/AboutPage schema
19
- * - 'default': No automatic schema injection
20
- */
21
- pageType?: PageType;
22
- }
23
- interface OntoConfig {
24
- /**
25
- * The name of your project or site (required)
26
- * Used as the H1 heading in llms.txt
27
- */
28
- name: string;
29
- /**
30
- * A short summary of your project (required)
31
- * Displayed as a blockquote in llms.txt
32
- * Should contain key information necessary for understanding the rest of the file
33
- */
34
- summary: string;
35
- /**
36
- * The base URL of your site (e.g., 'https://example.com')
37
- */
38
- baseUrl: string;
39
- /**
40
- * Optional: Additional sections to include in llms.txt
41
- * Each section can contain any markdown content
42
- */
43
- sections?: {
44
- heading: string;
45
- content: string;
46
- }[];
47
- /**
48
- * Key routes that AI agents should know about
49
- * These will be formatted as a markdown list in llms.txt
50
- */
51
- routes?: OntoRoute[];
52
- /**
53
- * Optional: Links to external resources (documentation, API references, etc.)
54
- */
55
- externalLinks?: {
56
- title: string;
57
- url: string;
58
- description?: string;
59
- }[];
60
- /**
61
- * Optional: Organization information for JSON-LD schemas
62
- */
63
- organization?: {
64
- name: string;
65
- description?: string;
66
- url?: string;
67
- logo?: string;
68
- foundingDate?: string;
69
- };
70
- }
71
- /**
72
- * Load the onto.config.ts file from the user's project
73
- * This is used by the middleware to dynamically generate llms.txt
74
- */
75
- declare function loadOntoConfig(): Promise<OntoConfig | null>;
76
- /**
77
- * Generate llms.txt content from OntoConfig
78
- * Follows the llms.txt specification:
79
- * - H1 with project name
80
- * - Blockquote with summary
81
- * - Additional markdown sections
82
- */
83
- declare function generateLlmsTxt(config: OntoConfig): string;
84
-
85
- export { type OntoConfig, type OntoRoute, type PageType, generateLlmsTxt, loadOntoConfig };
package/dist/config.d.ts DELETED
@@ -1,85 +0,0 @@
1
- /**
2
- * Configuration schema for onto.config.ts
3
- * Used to dynamically generate llms.txt and other AI discovery files
4
- */
5
- type PageType = 'scoring' | 'about' | 'default';
6
- interface OntoRoute {
7
- /**
8
- * The URL path (e.g., '/docs', '/api/reference')
9
- */
10
- path: string;
11
- /**
12
- * Description of what this route contains
13
- */
14
- description: string;
15
- /**
16
- * Optional: Page type for automatic JSON-LD schema injection
17
- * - 'scoring': Injects Methodology schema with AIO scoring weights (40/35/25)
18
- * - 'about': Injects Organization/AboutPage schema
19
- * - 'default': No automatic schema injection
20
- */
21
- pageType?: PageType;
22
- }
23
- interface OntoConfig {
24
- /**
25
- * The name of your project or site (required)
26
- * Used as the H1 heading in llms.txt
27
- */
28
- name: string;
29
- /**
30
- * A short summary of your project (required)
31
- * Displayed as a blockquote in llms.txt
32
- * Should contain key information necessary for understanding the rest of the file
33
- */
34
- summary: string;
35
- /**
36
- * The base URL of your site (e.g., 'https://example.com')
37
- */
38
- baseUrl: string;
39
- /**
40
- * Optional: Additional sections to include in llms.txt
41
- * Each section can contain any markdown content
42
- */
43
- sections?: {
44
- heading: string;
45
- content: string;
46
- }[];
47
- /**
48
- * Key routes that AI agents should know about
49
- * These will be formatted as a markdown list in llms.txt
50
- */
51
- routes?: OntoRoute[];
52
- /**
53
- * Optional: Links to external resources (documentation, API references, etc.)
54
- */
55
- externalLinks?: {
56
- title: string;
57
- url: string;
58
- description?: string;
59
- }[];
60
- /**
61
- * Optional: Organization information for JSON-LD schemas
62
- */
63
- organization?: {
64
- name: string;
65
- description?: string;
66
- url?: string;
67
- logo?: string;
68
- foundingDate?: string;
69
- };
70
- }
71
- /**
72
- * Load the onto.config.ts file from the user's project
73
- * This is used by the middleware to dynamically generate llms.txt
74
- */
75
- declare function loadOntoConfig(): Promise<OntoConfig | null>;
76
- /**
77
- * Generate llms.txt content from OntoConfig
78
- * Follows the llms.txt specification:
79
- * - H1 with project name
80
- * - Blockquote with summary
81
- * - Additional markdown sections
82
- */
83
- declare function generateLlmsTxt(config: OntoConfig): string;
84
-
85
- export { type OntoConfig, type OntoRoute, type PageType, generateLlmsTxt, loadOntoConfig };
package/dist/index.d.mts DELETED
@@ -1,25 +0,0 @@
1
- export { OntoConfig, OntoConfig as OntoConfigType, OntoRoute, OntoRoute as OntoRouteType, PageType, generateLlmsTxt, loadOntoConfig } from './config.mjs';
2
- export { AIOMethodologySchema, AboutPageSchema, OrganizationSchema, generateAIOMethodologySchema, generateAboutPageSchema, generateOrganizationSchema, generateSchemaForPageType, serializeSchema } from './schemas.mjs';
3
-
4
- interface ExtractionResult {
5
- markdown: string;
6
- metadata: {
7
- title: string;
8
- description: string;
9
- jsonLd: any[];
10
- };
11
- stats: {
12
- originalHtmlSize: number;
13
- markdownSize: number;
14
- tokenReductionRatio: number;
15
- };
16
- }
17
- /**
18
- * Extracts pure semantic markdown and metadata from rendered Next.js HTML strings.
19
- * @param html The raw HTML string.
20
- * @param sourceUrl (Optional) the URL this was generated from, to attach as metadata.
21
- * @returns {ExtractionResult} The extracted payload.
22
- */
23
- declare function extractContent(html: string, sourceUrl?: string): ExtractionResult;
24
-
25
- export { extractContent };
package/dist/index.d.ts DELETED
@@ -1,25 +0,0 @@
1
- export { OntoConfig, OntoConfig as OntoConfigType, OntoRoute, OntoRoute as OntoRouteType, PageType, generateLlmsTxt, loadOntoConfig } from './config.js';
2
- export { AIOMethodologySchema, AboutPageSchema, OrganizationSchema, generateAIOMethodologySchema, generateAboutPageSchema, generateOrganizationSchema, generateSchemaForPageType, serializeSchema } from './schemas.js';
3
-
4
- interface ExtractionResult {
5
- markdown: string;
6
- metadata: {
7
- title: string;
8
- description: string;
9
- jsonLd: any[];
10
- };
11
- stats: {
12
- originalHtmlSize: number;
13
- markdownSize: number;
14
- tokenReductionRatio: number;
15
- };
16
- }
17
- /**
18
- * Extracts pure semantic markdown and metadata from rendered Next.js HTML strings.
19
- * @param html The raw HTML string.
20
- * @param sourceUrl (Optional) the URL this was generated from, to attach as metadata.
21
- * @returns {ExtractionResult} The extracted payload.
22
- */
23
- declare function extractContent(html: string, sourceUrl?: string): ExtractionResult;
24
-
25
- export { extractContent };
@@ -1,25 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
-
3
- /**
4
- * Comprehensive registry of AI bot user-agent strings.
5
- * The middleware uses this list to detect AI crawlers and serve optimized markdown.
6
- */
7
- interface AiBot {
8
- /** The user-agent substring to match against */
9
- name: string;
10
- /** The company operating this bot */
11
- company: string;
12
- }
13
- /**
14
- * Flat list of user-agent substrings for fast matching in the middleware.
15
- */
16
- declare const AI_BOT_USER_AGENTS: string[];
17
- /**
18
- * Given a raw user-agent string, returns the matched AiBot entry or undefined.
19
- * Comparison is case-insensitive to handle inconsistent agent casing.
20
- */
21
- declare function matchBot(userAgent: string | null): AiBot | undefined;
22
-
23
- declare function ontoMiddleware(request: NextRequest): Promise<NextResponse<unknown>>;
24
-
25
- export { AI_BOT_USER_AGENTS, type AiBot, matchBot, ontoMiddleware };
@@ -1,25 +0,0 @@
1
- import { NextRequest, NextResponse } from 'next/server';
2
-
3
- /**
4
- * Comprehensive registry of AI bot user-agent strings.
5
- * The middleware uses this list to detect AI crawlers and serve optimized markdown.
6
- */
7
- interface AiBot {
8
- /** The user-agent substring to match against */
9
- name: string;
10
- /** The company operating this bot */
11
- company: string;
12
- }
13
- /**
14
- * Flat list of user-agent substrings for fast matching in the middleware.
15
- */
16
- declare const AI_BOT_USER_AGENTS: string[];
17
- /**
18
- * Given a raw user-agent string, returns the matched AiBot entry or undefined.
19
- * Comparison is case-insensitive to handle inconsistent agent casing.
20
- */
21
- declare function matchBot(userAgent: string | null): AiBot | undefined;
22
-
23
- declare function ontoMiddleware(request: NextRequest): Promise<NextResponse<unknown>>;
24
-
25
- export { AI_BOT_USER_AGENTS, type AiBot, matchBot, ontoMiddleware };
@@ -1,73 +0,0 @@
1
- import { OntoConfig } from './config.mjs';
2
-
3
- /**
4
- * JSON-LD Schema generators for automatic structured data injection
5
- * Follows Schema.org standards for AI-friendly metadata
6
- */
7
-
8
- /**
9
- * Standard AIO (AI Optimization) scoring methodology
10
- * Based on the Onto scoring algorithm:
11
- * - Content Negotiation: 40% (30 points)
12
- * - React Tax / Token Efficiency: 35% (30 points)
13
- * - Structured Data: 25% (25 points)
14
- * - Semantic HTML: Bonus (15 points)
15
- */
16
- interface AIOMethodologySchema {
17
- '@context': 'https://schema.org';
18
- '@type': 'HowTo';
19
- name: string;
20
- description: string;
21
- step: Array<{
22
- '@type': 'HowToStep';
23
- name: string;
24
- text: string;
25
- position: number;
26
- }>;
27
- }
28
- /**
29
- * Generate AIO Scoring Methodology JSON-LD schema
30
- * This explains to AI agents how the scoring system works
31
- */
32
- declare function generateAIOMethodologySchema(config: OntoConfig, pageUrl: string): AIOMethodologySchema;
33
- /**
34
- * Organization schema for About pages
35
- */
36
- interface OrganizationSchema {
37
- '@context': 'https://schema.org';
38
- '@type': 'Organization';
39
- name: string;
40
- url?: string;
41
- description?: string;
42
- logo?: string;
43
- foundingDate?: string;
44
- }
45
- /**
46
- * Generate Organization JSON-LD schema for About pages
47
- */
48
- declare function generateOrganizationSchema(config: OntoConfig, pageUrl: string): OrganizationSchema | null;
49
- /**
50
- * AboutPage schema combining Organization and WebPage
51
- */
52
- interface AboutPageSchema {
53
- '@context': 'https://schema.org';
54
- '@type': 'AboutPage';
55
- name: string;
56
- url: string;
57
- description?: string;
58
- mainEntity?: OrganizationSchema;
59
- }
60
- /**
61
- * Generate AboutPage JSON-LD schema
62
- */
63
- declare function generateAboutPageSchema(config: OntoConfig, pageUrl: string): AboutPageSchema;
64
- /**
65
- * Determine which schema to generate based on page type
66
- */
67
- declare function generateSchemaForPageType(pageType: 'scoring' | 'about' | 'default', config: OntoConfig, pageUrl: string): any | null;
68
- /**
69
- * Serialize schema to JSON-LD script tag content
70
- */
71
- declare function serializeSchema(schema: any | null): string | null;
72
-
73
- export { type AIOMethodologySchema, type AboutPageSchema, type OrganizationSchema, generateAIOMethodologySchema, generateAboutPageSchema, generateOrganizationSchema, generateSchemaForPageType, serializeSchema };