@rankcli/agent-runtime 0.0.14 → 0.0.16

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.
Files changed (199) hide show
  1. package/dist/index.d.mts +77 -393
  2. package/dist/index.d.ts +77 -393
  3. package/dist/index.js +417 -6443
  4. package/dist/index.mjs +296 -6313
  5. package/package.json +6 -1
  6. package/scripts/build-deno.ts +0 -134
  7. package/src/analyzers/core-web-vitals-analyzer.test.ts +0 -236
  8. package/src/analyzers/core-web-vitals-analyzer.ts +0 -557
  9. package/src/analyzers/geo-analyzer.test.ts +0 -310
  10. package/src/analyzers/geo-analyzer.ts +0 -814
  11. package/src/analyzers/image-optimization-analyzer.test.ts +0 -145
  12. package/src/analyzers/image-optimization-analyzer.ts +0 -348
  13. package/src/analyzers/index.ts +0 -233
  14. package/src/analyzers/internal-linking-analyzer.test.ts +0 -141
  15. package/src/analyzers/internal-linking-analyzer.ts +0 -419
  16. package/src/analyzers/mobile-seo-analyzer.test.ts +0 -140
  17. package/src/analyzers/mobile-seo-analyzer.ts +0 -455
  18. package/src/analyzers/security-headers-analyzer.test.ts +0 -115
  19. package/src/analyzers/security-headers-analyzer.ts +0 -318
  20. package/src/analyzers/structured-data-analyzer.test.ts +0 -210
  21. package/src/analyzers/structured-data-analyzer.ts +0 -590
  22. package/src/audit/ai/analyzer.ts +0 -347
  23. package/src/audit/ai/index.ts +0 -29
  24. package/src/audit/ai/prompts/content-analysis.ts +0 -271
  25. package/src/audit/ai/types.ts +0 -179
  26. package/src/audit/checks/additional-checks.ts +0 -439
  27. package/src/audit/checks/ai-citation-worthiness.ts +0 -399
  28. package/src/audit/checks/ai-content-structure.ts +0 -325
  29. package/src/audit/checks/ai-readiness.ts +0 -339
  30. package/src/audit/checks/anchor-text.ts +0 -179
  31. package/src/audit/checks/answer-conciseness.ts +0 -322
  32. package/src/audit/checks/asset-minification.ts +0 -270
  33. package/src/audit/checks/bing-optimization.ts +0 -206
  34. package/src/audit/checks/brand-mention-optimization.ts +0 -349
  35. package/src/audit/checks/caching-headers.ts +0 -305
  36. package/src/audit/checks/canonical-advanced.ts +0 -150
  37. package/src/audit/checks/canonical-domain.ts +0 -196
  38. package/src/audit/checks/citation-quality.ts +0 -358
  39. package/src/audit/checks/client-rendering.ts +0 -542
  40. package/src/audit/checks/color-contrast.ts +0 -342
  41. package/src/audit/checks/content-freshness.ts +0 -170
  42. package/src/audit/checks/content-science.ts +0 -589
  43. package/src/audit/checks/conversion-elements.ts +0 -526
  44. package/src/audit/checks/crawlability.ts +0 -220
  45. package/src/audit/checks/directory-listing.ts +0 -172
  46. package/src/audit/checks/dom-analysis.ts +0 -191
  47. package/src/audit/checks/dom-size.ts +0 -246
  48. package/src/audit/checks/duplicate-content.ts +0 -194
  49. package/src/audit/checks/eeat-signals.ts +0 -990
  50. package/src/audit/checks/entity-seo.ts +0 -396
  51. package/src/audit/checks/featured-snippet.ts +0 -473
  52. package/src/audit/checks/freshness-signals.ts +0 -443
  53. package/src/audit/checks/funnel-intent.ts +0 -463
  54. package/src/audit/checks/hreflang.ts +0 -174
  55. package/src/audit/checks/html-compliance.ts +0 -302
  56. package/src/audit/checks/image-dimensions.ts +0 -167
  57. package/src/audit/checks/images.ts +0 -160
  58. package/src/audit/checks/indexnow.ts +0 -275
  59. package/src/audit/checks/interactive-tools.ts +0 -475
  60. package/src/audit/checks/internal-link-graph.ts +0 -436
  61. package/src/audit/checks/keyword-analysis.ts +0 -239
  62. package/src/audit/checks/keyword-cannibalization.ts +0 -385
  63. package/src/audit/checks/keyword-placement.ts +0 -471
  64. package/src/audit/checks/links.ts +0 -273
  65. package/src/audit/checks/llms-txt.ts +0 -224
  66. package/src/audit/checks/local-seo.ts +0 -296
  67. package/src/audit/checks/mobile.ts +0 -167
  68. package/src/audit/checks/modern-images.ts +0 -226
  69. package/src/audit/checks/navboost-signals.ts +0 -395
  70. package/src/audit/checks/on-page.ts +0 -209
  71. package/src/audit/checks/page-resources.ts +0 -285
  72. package/src/audit/checks/pagination.ts +0 -180
  73. package/src/audit/checks/performance.ts +0 -153
  74. package/src/audit/checks/platform-presence.ts +0 -580
  75. package/src/audit/checks/redirect-analysis.ts +0 -153
  76. package/src/audit/checks/redirect-chain.ts +0 -389
  77. package/src/audit/checks/resource-hints.ts +0 -420
  78. package/src/audit/checks/responsive-css.ts +0 -247
  79. package/src/audit/checks/responsive-images.ts +0 -396
  80. package/src/audit/checks/review-ecosystem.ts +0 -415
  81. package/src/audit/checks/robots-validation.ts +0 -373
  82. package/src/audit/checks/security-headers.ts +0 -188
  83. package/src/audit/checks/security.ts +0 -144
  84. package/src/audit/checks/serp-preview.ts +0 -251
  85. package/src/audit/checks/site-maturity.ts +0 -453
  86. package/src/audit/checks/social-meta.test.ts +0 -275
  87. package/src/audit/checks/social-meta.ts +0 -134
  88. package/src/audit/checks/soft-404.ts +0 -151
  89. package/src/audit/checks/structured-data.ts +0 -238
  90. package/src/audit/checks/tech-detection.ts +0 -496
  91. package/src/audit/checks/topical-clusters.ts +0 -435
  92. package/src/audit/checks/tracker-bloat.ts +0 -462
  93. package/src/audit/checks/tracking-verification.test.ts +0 -371
  94. package/src/audit/checks/tracking-verification.ts +0 -636
  95. package/src/audit/checks/url-safety.ts +0 -682
  96. package/src/audit/deno-entry.ts +0 -66
  97. package/src/audit/discovery/index.ts +0 -15
  98. package/src/audit/discovery/link-crawler.ts +0 -233
  99. package/src/audit/discovery/repo-routes.ts +0 -347
  100. package/src/audit/engine.ts +0 -627
  101. package/src/audit/fixes/index.ts +0 -209
  102. package/src/audit/fixes/social-meta-fixes.test.ts +0 -329
  103. package/src/audit/fixes/social-meta-fixes.ts +0 -463
  104. package/src/audit/index.ts +0 -74
  105. package/src/audit/runner.test.ts +0 -299
  106. package/src/audit/runner.ts +0 -130
  107. package/src/audit/types.ts +0 -1963
  108. package/src/content/featured-snippet.ts +0 -367
  109. package/src/content/generator.test.ts +0 -534
  110. package/src/content/generator.ts +0 -501
  111. package/src/content/headline.ts +0 -317
  112. package/src/content/index.ts +0 -62
  113. package/src/content/intent.ts +0 -258
  114. package/src/content/keyword-density.ts +0 -349
  115. package/src/content/readability.ts +0 -262
  116. package/src/executor.ts +0 -336
  117. package/src/fixer/framework-fixes.test.ts +0 -489
  118. package/src/fixer/framework-fixes.ts +0 -5287
  119. package/src/fixer/index.ts +0 -6
  120. package/src/fixer/schemas.ts +0 -971
  121. package/src/fixer.ts +0 -642
  122. package/src/frameworks/detector.test.ts +0 -248
  123. package/src/frameworks/detector.ts +0 -899
  124. package/src/frameworks/index.ts +0 -68
  125. package/src/frameworks/recipes/angular.yaml +0 -171
  126. package/src/frameworks/recipes/astro.yaml +0 -206
  127. package/src/frameworks/recipes/django.yaml +0 -180
  128. package/src/frameworks/recipes/laravel.yaml +0 -137
  129. package/src/frameworks/recipes/nextjs.yaml +0 -268
  130. package/src/frameworks/recipes/nuxt.yaml +0 -175
  131. package/src/frameworks/recipes/rails.yaml +0 -188
  132. package/src/frameworks/recipes/react.yaml +0 -202
  133. package/src/frameworks/recipes/sveltekit.yaml +0 -154
  134. package/src/frameworks/recipes/vue.yaml +0 -137
  135. package/src/frameworks/recipes/wordpress.yaml +0 -209
  136. package/src/frameworks/suggestion-engine.ts +0 -357
  137. package/src/geo/geo-content.test.ts +0 -305
  138. package/src/geo/geo-content.ts +0 -266
  139. package/src/geo/geo-history.test.ts +0 -473
  140. package/src/geo/geo-history.ts +0 -433
  141. package/src/geo/geo-tracker.test.ts +0 -359
  142. package/src/geo/geo-tracker.ts +0 -411
  143. package/src/geo/index.ts +0 -11
  144. package/src/geo/llm-citation-checker.ts +0 -188
  145. package/src/git/commit-helper.test.ts +0 -261
  146. package/src/git/commit-helper.ts +0 -329
  147. package/src/git/index.ts +0 -12
  148. package/src/git/pr-helper.test.ts +0 -284
  149. package/src/git/pr-helper.ts +0 -307
  150. package/src/index.ts +0 -76
  151. package/src/keywords/ai-keyword-engine.ts +0 -1062
  152. package/src/keywords/ai-summarizer.ts +0 -387
  153. package/src/keywords/ci-mode.ts +0 -555
  154. package/src/keywords/engine.ts +0 -359
  155. package/src/keywords/index.ts +0 -151
  156. package/src/keywords/llm-judge.ts +0 -357
  157. package/src/keywords/nlp-analysis.ts +0 -706
  158. package/src/keywords/prioritizer.ts +0 -295
  159. package/src/keywords/site-crawler.ts +0 -342
  160. package/src/keywords/sources/autocomplete.ts +0 -139
  161. package/src/keywords/sources/competitive-search.ts +0 -450
  162. package/src/keywords/sources/competitor-analysis.ts +0 -374
  163. package/src/keywords/sources/dataforseo.ts +0 -206
  164. package/src/keywords/sources/free-sources.ts +0 -294
  165. package/src/keywords/sources/gsc.ts +0 -123
  166. package/src/keywords/topic-grouping.ts +0 -327
  167. package/src/keywords/types.ts +0 -144
  168. package/src/keywords/wizard.ts +0 -457
  169. package/src/loader.ts +0 -40
  170. package/src/ranking/index.ts +0 -5
  171. package/src/ranking/serp-client.ts +0 -348
  172. package/src/ranking/tracker.ts +0 -380
  173. package/src/ranking/types.ts +0 -123
  174. package/src/reports/index.ts +0 -7
  175. package/src/reports/report-generator.test.ts +0 -293
  176. package/src/reports/report-generator.ts +0 -713
  177. package/src/scheduler/alerts.test.ts +0 -458
  178. package/src/scheduler/alerts.ts +0 -328
  179. package/src/scheduler/index.ts +0 -8
  180. package/src/scheduler/scheduled-audit.test.ts +0 -377
  181. package/src/scheduler/scheduled-audit.ts +0 -149
  182. package/src/test/integration-test.ts +0 -325
  183. package/src/tools/analyzer.ts +0 -373
  184. package/src/tools/crawl.ts +0 -293
  185. package/src/tools/files.ts +0 -301
  186. package/src/tools/h1-fixer.ts +0 -249
  187. package/src/tools/index.ts +0 -67
  188. package/src/tracking/github-action.ts +0 -326
  189. package/src/tracking/google-analytics.ts +0 -265
  190. package/src/tracking/index.ts +0 -45
  191. package/src/tracking/report-generator.ts +0 -386
  192. package/src/tracking/search-console.ts +0 -335
  193. package/src/types.ts +0 -148
  194. package/src/utils/http.ts +0 -302
  195. package/src/wasm-adapter.ts +0 -297
  196. package/src/wasm-entry.ts +0 -14
  197. package/tsconfig.json +0 -17
  198. package/tsup.wasm.config.ts +0 -26
  199. package/vitest.config.ts +0 -15
@@ -1,295 +0,0 @@
1
- // Keyword Prioritization Algorithm
2
-
3
- import type {
4
- SiteProfile,
5
- KeywordData,
6
- KeywordOpportunity,
7
- KeywordAction,
8
- KeywordResearchResult,
9
- } from './types.js';
10
- import { getMaxKdThreshold, PRIORITY_WEIGHTS } from './types.js';
11
-
12
- export function prioritizeKeywords(
13
- keywords: KeywordData[],
14
- siteProfile: SiteProfile,
15
- existingMeta?: { title?: string; description?: string; h1?: string }
16
- ): KeywordResearchResult {
17
- const maxKd = getMaxKdThreshold(siteProfile);
18
-
19
- // Score and categorize each keyword
20
- const opportunities: KeywordOpportunity[] = keywords.map((kw) => {
21
- const priorityScore = calculatePriorityScore(kw, siteProfile, maxKd);
22
- const category = categorizeKeyword(kw.keywordDifficulty, maxKd);
23
- const suggestedAction = suggestAction(kw, existingMeta, category);
24
-
25
- return {
26
- ...kw,
27
- priorityScore,
28
- category,
29
- suggestedAction,
30
- };
31
- });
32
-
33
- // Sort by priority score
34
- opportunities.sort((a, b) => b.priorityScore - a.priorityScore);
35
-
36
- // Categorize into buckets
37
- const quickWins = opportunities.filter((kw) => kw.category === 'quick-win');
38
- const mediumTerm = opportunities.filter((kw) => kw.category === 'medium-term');
39
- const longTerm = opportunities.filter((kw) => kw.category === 'long-term');
40
-
41
- // Generate recommendations
42
- const recommendations = generateRecommendations(siteProfile, quickWins, mediumTerm);
43
-
44
- return {
45
- siteProfile,
46
- keywords: opportunities,
47
- quickWins,
48
- mediumTerm,
49
- longTerm,
50
- recommendations,
51
- maxKdThreshold: maxKd,
52
- };
53
- }
54
-
55
- function calculatePriorityScore(
56
- keyword: KeywordData,
57
- profile: SiteProfile,
58
- maxKd: number
59
- ): number {
60
- // Business value score (0-100)
61
- const businessScore = calculateBusinessValue(keyword, profile.businessGoal);
62
-
63
- // Difficulty score (inverse - lower KD = higher score)
64
- let difficultyScore = 0;
65
- if (keyword.keywordDifficulty <= maxKd) {
66
- difficultyScore = 100 - (keyword.keywordDifficulty / maxKd) * 100;
67
- }
68
-
69
- // Traffic score (normalized, capped at 100)
70
- const trafficScore = Math.min((keyword.searchVolume / 1000) * 100, 100);
71
-
72
- // Weighted sum
73
- const score =
74
- businessScore * PRIORITY_WEIGHTS.businessValue +
75
- difficultyScore * PRIORITY_WEIGHTS.difficulty +
76
- trafficScore * PRIORITY_WEIGHTS.trafficPotential;
77
-
78
- return Math.round(score);
79
- }
80
-
81
- function calculateBusinessValue(keyword: KeywordData, goal: SiteProfile['businessGoal']): number {
82
- const intent = keyword.intent || inferIntent(keyword.keyword);
83
-
84
- // Intent weights based on business goal
85
- const intentWeights: Record<SiteProfile['businessGoal'], Record<string, number>> = {
86
- signups: {
87
- transactional: 100,
88
- commercial: 80,
89
- informational: 40,
90
- navigational: 20,
91
- },
92
- purchases: {
93
- transactional: 100,
94
- commercial: 90,
95
- informational: 30,
96
- navigational: 20,
97
- },
98
- leads: {
99
- commercial: 100,
100
- transactional: 80,
101
- informational: 60,
102
- navigational: 20,
103
- },
104
- awareness: {
105
- informational: 100,
106
- commercial: 60,
107
- navigational: 40,
108
- transactional: 30,
109
- },
110
- };
111
-
112
- return intentWeights[goal][intent] || 50;
113
- }
114
-
115
- function inferIntent(keyword: string): 'informational' | 'commercial' | 'transactional' | 'navigational' {
116
- const kw = keyword.toLowerCase();
117
-
118
- // Transactional indicators
119
- if (/\b(buy|purchase|order|subscribe|download|get|try|free trial)\b/.test(kw)) {
120
- return 'transactional';
121
- }
122
-
123
- // Commercial investigation
124
- if (/\b(best|top|review|compare|vs|versus|alternative|pricing|cost)\b/.test(kw)) {
125
- return 'commercial';
126
- }
127
-
128
- // Navigational
129
- if (/\b(login|sign in|website|official|app)\b/.test(kw)) {
130
- return 'navigational';
131
- }
132
-
133
- // Default to informational
134
- return 'informational';
135
- }
136
-
137
- function categorizeKeyword(kd: number, maxKd: number): 'quick-win' | 'medium-term' | 'long-term' {
138
- const quickWinThreshold = Math.min(maxKd * 0.5, 15);
139
- const mediumThreshold = maxKd;
140
-
141
- if (kd <= quickWinThreshold) return 'quick-win';
142
- if (kd <= mediumThreshold) return 'medium-term';
143
- return 'long-term';
144
- }
145
-
146
- function suggestAction(
147
- keyword: KeywordData,
148
- existingMeta: { title?: string; description?: string; h1?: string } | undefined,
149
- category: 'quick-win' | 'medium-term' | 'long-term'
150
- ): KeywordAction {
151
- const kw = keyword.keyword;
152
-
153
- // Check if keyword is already in existing meta
154
- const inTitle = existingMeta?.title?.toLowerCase().includes(kw.toLowerCase());
155
- const inDescription = existingMeta?.description?.toLowerCase().includes(kw.toLowerCase());
156
- const inH1 = existingMeta?.h1?.toLowerCase().includes(kw.toLowerCase());
157
-
158
- // Quick wins: optimize existing elements
159
- if (category === 'quick-win') {
160
- if (!inTitle && existingMeta?.title) {
161
- return {
162
- type: 'add-to-title',
163
- description: `Add "${kw}" to your title tag`,
164
- targetElement: 'title',
165
- currentValue: existingMeta.title,
166
- suggestedValue: generateTitleWithKeyword(existingMeta.title, kw),
167
- };
168
- }
169
-
170
- if (!inH1) {
171
- return {
172
- type: 'add-to-h1',
173
- description: `Include "${kw}" in your H1 heading`,
174
- targetElement: 'h1',
175
- currentValue: existingMeta?.h1,
176
- suggestedValue: generateH1WithKeyword(existingMeta?.h1, kw),
177
- };
178
- }
179
-
180
- if (!inDescription && existingMeta?.description) {
181
- return {
182
- type: 'add-to-meta',
183
- description: `Add "${kw}" to your meta description`,
184
- targetElement: 'meta[name="description"]',
185
- currentValue: existingMeta.description,
186
- suggestedValue: generateDescriptionWithKeyword(existingMeta.description, kw),
187
- };
188
- }
189
-
190
- return {
191
- type: 'optimize-existing',
192
- description: `Optimize existing content for "${kw}"`,
193
- };
194
- }
195
-
196
- // Medium-term: may need content creation
197
- if (category === 'medium-term') {
198
- return {
199
- type: 'create-content',
200
- description: `Create dedicated content targeting "${kw}"`,
201
- };
202
- }
203
-
204
- // Long-term: need authority building first
205
- return {
206
- type: 'create-content',
207
- description: `Build authority first, then target "${kw}" (high competition)`,
208
- };
209
- }
210
-
211
- function generateTitleWithKeyword(currentTitle: string, keyword: string): string {
212
- // If title is short, prepend keyword
213
- if (currentTitle.length < 30) {
214
- return `${capitalizeFirst(keyword)} - ${currentTitle}`;
215
- }
216
-
217
- // Otherwise, try to incorporate naturally
218
- const parts = currentTitle.split(' - ');
219
- if (parts.length >= 2) {
220
- return `${capitalizeFirst(keyword)} | ${parts[parts.length - 1]}`;
221
- }
222
-
223
- return `${capitalizeFirst(keyword)} | ${currentTitle}`.substring(0, 60);
224
- }
225
-
226
- function generateH1WithKeyword(currentH1: string | undefined, keyword: string): string {
227
- if (!currentH1) {
228
- return capitalizeFirst(keyword);
229
- }
230
-
231
- // Try to incorporate keyword naturally
232
- return `${capitalizeFirst(keyword)} - ${currentH1}`;
233
- }
234
-
235
- function generateDescriptionWithKeyword(currentDesc: string, keyword: string): string {
236
- // If description doesn't start with keyword concept, prepend it
237
- if (!currentDesc.toLowerCase().includes(keyword.toLowerCase().split(' ')[0])) {
238
- const newDesc = `${capitalizeFirst(keyword)}: ${currentDesc}`;
239
- return newDesc.substring(0, 160);
240
- }
241
-
242
- return currentDesc;
243
- }
244
-
245
- function capitalizeFirst(str: string): string {
246
- return str.charAt(0).toUpperCase() + str.slice(1);
247
- }
248
-
249
- function generateRecommendations(
250
- profile: SiteProfile,
251
- quickWins: KeywordOpportunity[],
252
- mediumTerm: KeywordOpportunity[]
253
- ): string[] {
254
- const recommendations: string[] = [];
255
-
256
- // Domain age specific recommendations
257
- if (profile.domainAge === 'new') {
258
- recommendations.push(
259
- 'Focus on quick-win keywords (KD < 15) first to build initial traffic and authority.'
260
- );
261
- recommendations.push(
262
- 'Consider creating long-form, comprehensive content to establish topical authority.'
263
- );
264
- }
265
-
266
- // Backlink recommendations
267
- if (profile.backlinkCount === 'none' || profile.backlinkCount === 'few') {
268
- recommendations.push(
269
- 'Prioritize building quality backlinks to increase your ranking potential for competitive keywords.'
270
- );
271
- }
272
-
273
- // Content recommendations based on capacity
274
- if (profile.contentCapacity === 'low' && quickWins.length > 2) {
275
- recommendations.push(
276
- `Focus on your top ${Math.min(2, quickWins.length)} quick-win keywords first, then expand.`
277
- );
278
- }
279
-
280
- // Quick wins available
281
- if (quickWins.length > 0) {
282
- recommendations.push(
283
- `You have ${quickWins.length} quick-win keyword opportunities that you can rank for relatively quickly.`
284
- );
285
- }
286
-
287
- // Medium-term opportunities
288
- if (mediumTerm.length > 0 && profile.domainAge !== 'new') {
289
- recommendations.push(
290
- `${mediumTerm.length} medium-difficulty keywords are within reach with focused content and some link building.`
291
- );
292
- }
293
-
294
- return recommendations;
295
- }
@@ -1,342 +0,0 @@
1
- /**
2
- * Site Crawler for Keyword Research
3
- *
4
- * Crawls an entire site to collect text content for AI analysis.
5
- * Used to understand what the site does and generate relevant keywords.
6
- */
7
-
8
- import * as cheerio from 'cheerio';
9
- import { httpGet } from '../utils/http.js';
10
-
11
- export interface CrawledPage {
12
- url: string;
13
- title: string;
14
- description: string;
15
- h1: string;
16
- h2s: string[];
17
- mainContent: string;
18
- wordCount: number;
19
- internalLinks: string[];
20
- isProductPage: boolean;
21
- isPricingPage: boolean;
22
- isBlogPost: boolean;
23
- isFeaturePage: boolean;
24
- }
25
-
26
- export interface SiteCrawlResult {
27
- domain: string;
28
- pages: CrawledPage[];
29
- aggregatedContent: string;
30
- totalWordCount: number;
31
- uniqueHeadings: string[];
32
- detectedPageTypes: {
33
- product: number;
34
- pricing: number;
35
- blog: number;
36
- feature: number;
37
- other: number;
38
- };
39
- crawlStats: {
40
- attempted: number;
41
- succeeded: number;
42
- failed: number;
43
- duration: number;
44
- };
45
- }
46
-
47
- const EXCLUDED_PATHS = [
48
- '/cdn-cgi/',
49
- '/wp-admin/',
50
- '/wp-includes/',
51
- '/wp-json/',
52
- '/api/',
53
- '/admin/',
54
- '/_next/',
55
- '/static/',
56
- '/assets/',
57
- '.xml',
58
- '.json',
59
- '.pdf',
60
- '.zip',
61
- '.png',
62
- '.jpg',
63
- '.jpeg',
64
- '.gif',
65
- '.svg',
66
- '.ico',
67
- '.css',
68
- '.js',
69
- ];
70
-
71
- const PAGE_TYPE_PATTERNS = {
72
- product: ['/product', '/features', '/solutions', '/platform', '/tour'],
73
- pricing: ['/pricing', '/plans', '/subscription'],
74
- blog: ['/blog', '/news', '/articles', '/posts', '/journal'],
75
- feature: ['/feature', '/capability', '/integration'],
76
- };
77
-
78
- /**
79
- * Crawl an entire site to collect text content
80
- */
81
- export async function crawlSite(
82
- startUrl: string,
83
- options: {
84
- maxPages?: number;
85
- maxDepth?: number;
86
- timeout?: number;
87
- } = {}
88
- ): Promise<SiteCrawlResult> {
89
- const { maxPages = 30, maxDepth = 3, timeout = 10000 } = options;
90
-
91
- const startTime = Date.now();
92
- const parsedUrl = new URL(startUrl);
93
- const domain = parsedUrl.hostname;
94
- const baseUrl = `${parsedUrl.protocol}//${domain}`;
95
-
96
- const visited = new Set<string>();
97
- const toVisit: Array<{ url: string; depth: number }> = [{ url: startUrl, depth: 0 }];
98
- const pages: CrawledPage[] = [];
99
- let failed = 0;
100
-
101
- console.log(`🕷️ Crawling ${domain} (max ${maxPages} pages)...`);
102
-
103
- while (toVisit.length > 0 && pages.length < maxPages) {
104
- const { url, depth } = toVisit.shift()!;
105
-
106
- // Normalize URL
107
- const normalizedUrl = normalizeUrl(url, baseUrl);
108
- if (!normalizedUrl || visited.has(normalizedUrl)) continue;
109
-
110
- // Skip excluded paths
111
- if (EXCLUDED_PATHS.some((p) => normalizedUrl.includes(p))) continue;
112
-
113
- // Only crawl same domain
114
- try {
115
- const urlObj = new URL(normalizedUrl);
116
- if (urlObj.hostname !== domain) continue;
117
- } catch {
118
- continue;
119
- }
120
-
121
- visited.add(normalizedUrl);
122
-
123
- try {
124
- const page = await crawlPage(normalizedUrl, timeout);
125
- pages.push(page);
126
-
127
- // Add internal links to queue (if not at max depth)
128
- if (depth < maxDepth) {
129
- for (const link of page.internalLinks) {
130
- const normalizedLink = normalizeUrl(link, baseUrl);
131
- if (normalizedLink && !visited.has(normalizedLink)) {
132
- toVisit.push({ url: normalizedLink, depth: depth + 1 });
133
- }
134
- }
135
- }
136
-
137
- // Progress indicator
138
- if (pages.length % 5 === 0) {
139
- console.log(` Crawled ${pages.length} pages...`);
140
- }
141
- } catch (error) {
142
- failed++;
143
- }
144
- }
145
-
146
- const duration = Date.now() - startTime;
147
-
148
- // Aggregate content
149
- const aggregatedContent = pages
150
- .map((p) => `# ${p.title}\n${p.description}\n${p.mainContent}`)
151
- .join('\n\n---\n\n');
152
-
153
- const totalWordCount = pages.reduce((sum, p) => sum + p.wordCount, 0);
154
-
155
- // Collect unique headings
156
- const allHeadings = new Set<string>();
157
- pages.forEach((p) => {
158
- if (p.h1) allHeadings.add(p.h1);
159
- p.h2s.forEach((h2) => allHeadings.add(h2));
160
- });
161
-
162
- // Count page types
163
- const detectedPageTypes = {
164
- product: pages.filter((p) => p.isProductPage).length,
165
- pricing: pages.filter((p) => p.isPricingPage).length,
166
- blog: pages.filter((p) => p.isBlogPost).length,
167
- feature: pages.filter((p) => p.isFeaturePage).length,
168
- other: pages.filter(
169
- (p) => !p.isProductPage && !p.isPricingPage && !p.isBlogPost && !p.isFeaturePage
170
- ).length,
171
- };
172
-
173
- console.log(`✅ Crawled ${pages.length} pages in ${(duration / 1000).toFixed(1)}s`);
174
-
175
- return {
176
- domain,
177
- pages,
178
- aggregatedContent,
179
- totalWordCount,
180
- uniqueHeadings: Array.from(allHeadings),
181
- detectedPageTypes,
182
- crawlStats: {
183
- attempted: visited.size,
184
- succeeded: pages.length,
185
- failed,
186
- duration,
187
- },
188
- };
189
- }
190
-
191
- /**
192
- * Crawl a single page
193
- */
194
- async function crawlPage(url: string, timeout: number): Promise<CrawledPage> {
195
- const response = await httpGet<string>(url, {
196
- timeout,
197
- validateStatus: (status) => status === 200,
198
- });
199
-
200
- const html = response.data;
201
- const $ = cheerio.load(html);
202
-
203
- // Remove non-content elements
204
- $('script, style, noscript, iframe, nav, footer, header, aside, [role="navigation"]').remove();
205
-
206
- // Extract content
207
- const title = $('title').text().trim();
208
- const description = $('meta[name="description"]').attr('content')?.trim() || '';
209
- const h1 = $('h1').first().text().trim();
210
- const h2s = $('h2')
211
- .map((_, el) => $(el).text().trim())
212
- .get()
213
- .filter((h) => h.length > 0);
214
-
215
- // Get main content
216
- const mainSelectors = ['main', 'article', '[role="main"]', '.content', '#content', '.post-content'];
217
- let mainContent = '';
218
-
219
- for (const selector of mainSelectors) {
220
- const el = $(selector);
221
- if (el.length > 0) {
222
- mainContent = el.text().trim();
223
- break;
224
- }
225
- }
226
-
227
- // Fallback to body
228
- if (!mainContent) {
229
- mainContent = $('body').text().trim();
230
- }
231
-
232
- // Clean up whitespace
233
- mainContent = mainContent
234
- .replace(/\s+/g, ' ')
235
- .replace(/\n+/g, '\n')
236
- .trim();
237
-
238
- // Limit content length for AI processing
239
- if (mainContent.length > 5000) {
240
- mainContent = mainContent.substring(0, 5000) + '...';
241
- }
242
-
243
- const wordCount = mainContent.split(/\s+/).length;
244
-
245
- // Extract internal links
246
- const internalLinks: string[] = [];
247
- $('a[href]').each((_, el) => {
248
- const href = $(el).attr('href');
249
- if (href && !href.startsWith('mailto:') && !href.startsWith('tel:') && !href.startsWith('#')) {
250
- internalLinks.push(href);
251
- }
252
- });
253
-
254
- // Detect page type
255
- const urlLower = url.toLowerCase();
256
- const isProductPage = PAGE_TYPE_PATTERNS.product.some((p) => urlLower.includes(p));
257
- const isPricingPage = PAGE_TYPE_PATTERNS.pricing.some((p) => urlLower.includes(p));
258
- const isBlogPost = PAGE_TYPE_PATTERNS.blog.some((p) => urlLower.includes(p));
259
- const isFeaturePage = PAGE_TYPE_PATTERNS.feature.some((p) => urlLower.includes(p));
260
-
261
- return {
262
- url,
263
- title,
264
- description,
265
- h1,
266
- h2s,
267
- mainContent,
268
- wordCount,
269
- internalLinks,
270
- isProductPage,
271
- isPricingPage,
272
- isBlogPost,
273
- isFeaturePage,
274
- };
275
- }
276
-
277
- /**
278
- * Normalize a URL
279
- */
280
- function normalizeUrl(url: string, baseUrl: string): string | null {
281
- try {
282
- // Handle relative URLs
283
- const absoluteUrl = url.startsWith('http') ? url : new URL(url, baseUrl).href;
284
-
285
- // Remove hash and trailing slash
286
- const urlObj = new URL(absoluteUrl);
287
- urlObj.hash = '';
288
- let normalized = urlObj.href;
289
-
290
- // Remove trailing slash (except for root)
291
- if (normalized.endsWith('/') && normalized !== `${urlObj.origin}/`) {
292
- normalized = normalized.slice(0, -1);
293
- }
294
-
295
- return normalized;
296
- } catch {
297
- return null;
298
- }
299
- }
300
-
301
- /**
302
- * Extract key phrases from crawled content
303
- */
304
- export function extractKeyPhrases(crawlResult: SiteCrawlResult): string[] {
305
- const phrases = new Set<string>();
306
-
307
- // From titles
308
- crawlResult.pages.forEach((page) => {
309
- if (page.title) {
310
- // Split on common separators
311
- const parts = page.title.split(/[|\-–—:]/).map((p) => p.trim());
312
- parts.forEach((part) => {
313
- if (part.length > 3 && part.length < 50) {
314
- phrases.add(part.toLowerCase());
315
- }
316
- });
317
- }
318
- });
319
-
320
- // From headings
321
- crawlResult.uniqueHeadings.forEach((heading) => {
322
- if (heading.length > 3 && heading.length < 50) {
323
- phrases.add(heading.toLowerCase());
324
- }
325
- });
326
-
327
- // From descriptions
328
- crawlResult.pages.forEach((page) => {
329
- if (page.description) {
330
- // Extract noun phrases (simplified - just multi-word sequences)
331
- const words = page.description.toLowerCase().split(/\s+/);
332
- for (let i = 0; i < words.length - 1; i++) {
333
- const bigram = `${words[i]} ${words[i + 1]}`;
334
- if (bigram.length > 5 && bigram.length < 40) {
335
- phrases.add(bigram);
336
- }
337
- }
338
- }
339
- });
340
-
341
- return Array.from(phrases).slice(0, 100);
342
- }