@rankcli/agent-runtime 0.0.1
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/README.md +242 -0
- package/dist/analyzer-2CSWIQGD.mjs +6 -0
- package/dist/chunk-YNZYHEYM.mjs +774 -0
- package/dist/index.d.mts +4012 -0
- package/dist/index.d.ts +4012 -0
- package/dist/index.js +29672 -0
- package/dist/index.mjs +28602 -0
- package/package.json +53 -0
- package/scripts/build-deno.ts +134 -0
- package/src/audit/ai/analyzer.ts +347 -0
- package/src/audit/ai/index.ts +29 -0
- package/src/audit/ai/prompts/content-analysis.ts +271 -0
- package/src/audit/ai/types.ts +179 -0
- package/src/audit/checks/additional-checks.ts +439 -0
- package/src/audit/checks/ai-citation-worthiness.ts +399 -0
- package/src/audit/checks/ai-content-structure.ts +325 -0
- package/src/audit/checks/ai-readiness.ts +339 -0
- package/src/audit/checks/anchor-text.ts +179 -0
- package/src/audit/checks/answer-conciseness.ts +322 -0
- package/src/audit/checks/asset-minification.ts +270 -0
- package/src/audit/checks/bing-optimization.ts +206 -0
- package/src/audit/checks/brand-mention-optimization.ts +349 -0
- package/src/audit/checks/caching-headers.ts +305 -0
- package/src/audit/checks/canonical-advanced.ts +150 -0
- package/src/audit/checks/canonical-domain.ts +196 -0
- package/src/audit/checks/citation-quality.ts +358 -0
- package/src/audit/checks/client-rendering.ts +542 -0
- package/src/audit/checks/color-contrast.ts +342 -0
- package/src/audit/checks/content-freshness.ts +170 -0
- package/src/audit/checks/content-science.ts +589 -0
- package/src/audit/checks/conversion-elements.ts +526 -0
- package/src/audit/checks/crawlability.ts +220 -0
- package/src/audit/checks/directory-listing.ts +172 -0
- package/src/audit/checks/dom-analysis.ts +191 -0
- package/src/audit/checks/dom-size.ts +246 -0
- package/src/audit/checks/duplicate-content.ts +194 -0
- package/src/audit/checks/eeat-signals.ts +990 -0
- package/src/audit/checks/entity-seo.ts +396 -0
- package/src/audit/checks/featured-snippet.ts +473 -0
- package/src/audit/checks/freshness-signals.ts +443 -0
- package/src/audit/checks/funnel-intent.ts +463 -0
- package/src/audit/checks/hreflang.ts +174 -0
- package/src/audit/checks/html-compliance.ts +302 -0
- package/src/audit/checks/image-dimensions.ts +167 -0
- package/src/audit/checks/images.ts +160 -0
- package/src/audit/checks/indexnow.ts +275 -0
- package/src/audit/checks/interactive-tools.ts +475 -0
- package/src/audit/checks/internal-link-graph.ts +436 -0
- package/src/audit/checks/keyword-analysis.ts +239 -0
- package/src/audit/checks/keyword-cannibalization.ts +385 -0
- package/src/audit/checks/keyword-placement.ts +471 -0
- package/src/audit/checks/links.ts +203 -0
- package/src/audit/checks/llms-txt.ts +224 -0
- package/src/audit/checks/local-seo.ts +296 -0
- package/src/audit/checks/mobile.ts +167 -0
- package/src/audit/checks/modern-images.ts +226 -0
- package/src/audit/checks/navboost-signals.ts +395 -0
- package/src/audit/checks/on-page.ts +209 -0
- package/src/audit/checks/page-resources.ts +285 -0
- package/src/audit/checks/pagination.ts +180 -0
- package/src/audit/checks/performance.ts +153 -0
- package/src/audit/checks/platform-presence.ts +580 -0
- package/src/audit/checks/redirect-analysis.ts +153 -0
- package/src/audit/checks/redirect-chain.ts +389 -0
- package/src/audit/checks/resource-hints.ts +420 -0
- package/src/audit/checks/responsive-css.ts +247 -0
- package/src/audit/checks/responsive-images.ts +396 -0
- package/src/audit/checks/review-ecosystem.ts +415 -0
- package/src/audit/checks/robots-validation.ts +373 -0
- package/src/audit/checks/security-headers.ts +172 -0
- package/src/audit/checks/security.ts +144 -0
- package/src/audit/checks/serp-preview.ts +251 -0
- package/src/audit/checks/site-maturity.ts +444 -0
- package/src/audit/checks/social-meta.test.ts +275 -0
- package/src/audit/checks/social-meta.ts +134 -0
- package/src/audit/checks/soft-404.ts +151 -0
- package/src/audit/checks/structured-data.ts +238 -0
- package/src/audit/checks/tech-detection.ts +496 -0
- package/src/audit/checks/topical-clusters.ts +435 -0
- package/src/audit/checks/tracker-bloat.ts +462 -0
- package/src/audit/checks/tracking-verification.test.ts +371 -0
- package/src/audit/checks/tracking-verification.ts +636 -0
- package/src/audit/checks/url-safety.ts +682 -0
- package/src/audit/deno-entry.ts +66 -0
- package/src/audit/discovery/index.ts +15 -0
- package/src/audit/discovery/link-crawler.ts +232 -0
- package/src/audit/discovery/repo-routes.ts +347 -0
- package/src/audit/engine.ts +620 -0
- package/src/audit/fixes/index.ts +209 -0
- package/src/audit/fixes/social-meta-fixes.test.ts +329 -0
- package/src/audit/fixes/social-meta-fixes.ts +463 -0
- package/src/audit/index.ts +74 -0
- package/src/audit/runner.test.ts +299 -0
- package/src/audit/runner.ts +130 -0
- package/src/audit/types.ts +1953 -0
- package/src/content/featured-snippet.ts +367 -0
- package/src/content/generator.test.ts +534 -0
- package/src/content/generator.ts +501 -0
- package/src/content/headline.ts +317 -0
- package/src/content/index.ts +62 -0
- package/src/content/intent.ts +258 -0
- package/src/content/keyword-density.ts +349 -0
- package/src/content/readability.ts +262 -0
- package/src/executor.ts +336 -0
- package/src/fixer.ts +416 -0
- package/src/frameworks/detector.test.ts +248 -0
- package/src/frameworks/detector.ts +371 -0
- package/src/frameworks/index.ts +68 -0
- package/src/frameworks/recipes/angular.yaml +171 -0
- package/src/frameworks/recipes/astro.yaml +206 -0
- package/src/frameworks/recipes/django.yaml +180 -0
- package/src/frameworks/recipes/laravel.yaml +137 -0
- package/src/frameworks/recipes/nextjs.yaml +268 -0
- package/src/frameworks/recipes/nuxt.yaml +175 -0
- package/src/frameworks/recipes/rails.yaml +188 -0
- package/src/frameworks/recipes/react.yaml +202 -0
- package/src/frameworks/recipes/sveltekit.yaml +154 -0
- package/src/frameworks/recipes/vue.yaml +137 -0
- package/src/frameworks/recipes/wordpress.yaml +209 -0
- package/src/frameworks/suggestion-engine.ts +320 -0
- package/src/geo/geo-content.test.ts +305 -0
- package/src/geo/geo-content.ts +266 -0
- package/src/geo/geo-history.test.ts +473 -0
- package/src/geo/geo-history.ts +433 -0
- package/src/geo/geo-tracker.test.ts +359 -0
- package/src/geo/geo-tracker.ts +411 -0
- package/src/geo/index.ts +10 -0
- package/src/git/commit-helper.test.ts +261 -0
- package/src/git/commit-helper.ts +329 -0
- package/src/git/index.ts +12 -0
- package/src/git/pr-helper.test.ts +284 -0
- package/src/git/pr-helper.ts +307 -0
- package/src/index.ts +66 -0
- package/src/keywords/ai-keyword-engine.ts +1062 -0
- package/src/keywords/ai-summarizer.ts +387 -0
- package/src/keywords/ci-mode.ts +555 -0
- package/src/keywords/engine.ts +359 -0
- package/src/keywords/index.ts +151 -0
- package/src/keywords/llm-judge.ts +357 -0
- package/src/keywords/nlp-analysis.ts +706 -0
- package/src/keywords/prioritizer.ts +295 -0
- package/src/keywords/site-crawler.ts +342 -0
- package/src/keywords/sources/autocomplete.ts +139 -0
- package/src/keywords/sources/competitive-search.ts +450 -0
- package/src/keywords/sources/competitor-analysis.ts +374 -0
- package/src/keywords/sources/dataforseo.ts +206 -0
- package/src/keywords/sources/free-sources.ts +294 -0
- package/src/keywords/sources/gsc.ts +123 -0
- package/src/keywords/topic-grouping.ts +327 -0
- package/src/keywords/types.ts +144 -0
- package/src/keywords/wizard.ts +457 -0
- package/src/loader.ts +40 -0
- package/src/reports/index.ts +7 -0
- package/src/reports/report-generator.test.ts +293 -0
- package/src/reports/report-generator.ts +713 -0
- package/src/scheduler/alerts.test.ts +458 -0
- package/src/scheduler/alerts.ts +328 -0
- package/src/scheduler/index.ts +8 -0
- package/src/scheduler/scheduled-audit.test.ts +377 -0
- package/src/scheduler/scheduled-audit.ts +149 -0
- package/src/test/integration-test.ts +325 -0
- package/src/tools/analyzer.ts +373 -0
- package/src/tools/crawl.ts +293 -0
- package/src/tools/files.ts +301 -0
- package/src/tools/h1-fixer.ts +249 -0
- package/src/tools/index.ts +67 -0
- package/src/tracking/github-action.ts +326 -0
- package/src/tracking/google-analytics.ts +265 -0
- package/src/tracking/index.ts +45 -0
- package/src/tracking/report-generator.ts +386 -0
- package/src/tracking/search-console.ts +335 -0
- package/src/types.ts +134 -0
- package/src/utils/http.ts +302 -0
- package/src/wasm-adapter.ts +297 -0
- package/src/wasm-entry.ts +14 -0
- package/tsconfig.json +17 -0
- package/tsup.wasm.config.ts +26 -0
- package/vitest.config.ts +15 -0
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GEO (Generative Engine Optimization) Tracker
|
|
3
|
+
*
|
|
4
|
+
* Tracks brand visibility in LLM responses to understand how AI models
|
|
5
|
+
* recommend or mention your brand for relevant queries.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export type MentionType = 'brand' | 'domain' | 'alternative';
|
|
9
|
+
export type LLMProvider = 'openai' | 'anthropic' | 'google' | 'perplexity';
|
|
10
|
+
export type Sentiment = 'positive' | 'negative' | 'neutral';
|
|
11
|
+
|
|
12
|
+
export interface BrandConfig {
|
|
13
|
+
brandName: string;
|
|
14
|
+
domains: string[];
|
|
15
|
+
alternativeNames?: string[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Mention {
|
|
19
|
+
type: MentionType;
|
|
20
|
+
text: string;
|
|
21
|
+
position: number;
|
|
22
|
+
context: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface GEOQuery {
|
|
26
|
+
keyword: string;
|
|
27
|
+
brand: BrandConfig;
|
|
28
|
+
providers: LLMProvider[];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface GEOResult {
|
|
32
|
+
provider: LLMProvider;
|
|
33
|
+
keyword: string;
|
|
34
|
+
mentioned: boolean;
|
|
35
|
+
position: number | null;
|
|
36
|
+
sentiment: Sentiment | null;
|
|
37
|
+
score: number;
|
|
38
|
+
timestamp: string;
|
|
39
|
+
contextSnippet?: string;
|
|
40
|
+
error?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface ParsedResponse {
|
|
44
|
+
mentioned: boolean;
|
|
45
|
+
position: number | null;
|
|
46
|
+
sentiment: Sentiment | null;
|
|
47
|
+
contextSnippet: string | null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface TrackingOptions {
|
|
51
|
+
fetch?: typeof fetch;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Detects brand mentions in text
|
|
56
|
+
*/
|
|
57
|
+
export function detectMentions(text: string, config: BrandConfig): Mention[] {
|
|
58
|
+
const mentions: Mention[] = [];
|
|
59
|
+
const lowerText = text.toLowerCase();
|
|
60
|
+
|
|
61
|
+
// Track found positions to avoid duplicates
|
|
62
|
+
const foundPositions = new Set<number>();
|
|
63
|
+
|
|
64
|
+
// Check for exact brand name (case-insensitive)
|
|
65
|
+
const brandLower = config.brandName.toLowerCase();
|
|
66
|
+
let searchIndex = 0;
|
|
67
|
+
while (true) {
|
|
68
|
+
const idx = lowerText.indexOf(brandLower, searchIndex);
|
|
69
|
+
if (idx === -1) break;
|
|
70
|
+
|
|
71
|
+
if (!foundPositions.has(idx)) {
|
|
72
|
+
foundPositions.add(idx);
|
|
73
|
+
const position = calculatePosition(text, idx);
|
|
74
|
+
mentions.push({
|
|
75
|
+
type: 'brand',
|
|
76
|
+
text: text.substring(idx, idx + config.brandName.length),
|
|
77
|
+
position,
|
|
78
|
+
context: extractContext(text, idx),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
searchIndex = idx + 1;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Check for domain mentions
|
|
85
|
+
for (const domain of config.domains) {
|
|
86
|
+
const domainLower = domain.toLowerCase();
|
|
87
|
+
searchIndex = 0;
|
|
88
|
+
while (true) {
|
|
89
|
+
const idx = lowerText.indexOf(domainLower, searchIndex);
|
|
90
|
+
if (idx === -1) break;
|
|
91
|
+
|
|
92
|
+
if (!foundPositions.has(idx)) {
|
|
93
|
+
foundPositions.add(idx);
|
|
94
|
+
const position = calculatePosition(text, idx);
|
|
95
|
+
mentions.push({
|
|
96
|
+
type: 'domain',
|
|
97
|
+
text: text.substring(idx, idx + domain.length),
|
|
98
|
+
position,
|
|
99
|
+
context: extractContext(text, idx),
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
searchIndex = idx + 1;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Check for alternative names
|
|
107
|
+
if (config.alternativeNames) {
|
|
108
|
+
for (const altName of config.alternativeNames) {
|
|
109
|
+
const altLower = altName.toLowerCase();
|
|
110
|
+
searchIndex = 0;
|
|
111
|
+
while (true) {
|
|
112
|
+
const idx = lowerText.indexOf(altLower, searchIndex);
|
|
113
|
+
if (idx === -1) break;
|
|
114
|
+
|
|
115
|
+
if (!foundPositions.has(idx)) {
|
|
116
|
+
foundPositions.add(idx);
|
|
117
|
+
const position = calculatePosition(text, idx);
|
|
118
|
+
mentions.push({
|
|
119
|
+
type: 'alternative',
|
|
120
|
+
text: text.substring(idx, idx + altName.length),
|
|
121
|
+
position,
|
|
122
|
+
context: extractContext(text, idx),
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
searchIndex = idx + 1;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Sort by position in text
|
|
131
|
+
mentions.sort((a, b) => a.position - b.position);
|
|
132
|
+
|
|
133
|
+
return mentions;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Calculate position (1-indexed) based on list context or sentence order
|
|
138
|
+
*/
|
|
139
|
+
function calculatePosition(text: string, charIndex: number): number {
|
|
140
|
+
// Check if we're in a numbered list
|
|
141
|
+
const beforeText = text.substring(0, charIndex);
|
|
142
|
+
const lines = beforeText.split('\n');
|
|
143
|
+
const currentLineStart = beforeText.lastIndexOf('\n') + 1;
|
|
144
|
+
const currentLine = text.substring(currentLineStart).split('\n')[0];
|
|
145
|
+
|
|
146
|
+
// Check for numbered list pattern (e.g., "1.", "2.", etc.)
|
|
147
|
+
const listMatch = currentLine.match(/^(\d+)\./);
|
|
148
|
+
if (listMatch) {
|
|
149
|
+
return parseInt(listMatch[1], 10);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// Count sentences/items before this mention
|
|
153
|
+
const sentences = beforeText.split(/[.!?]\s+/);
|
|
154
|
+
return Math.max(1, sentences.length);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Extract surrounding context for a mention
|
|
159
|
+
*/
|
|
160
|
+
function extractContext(text: string, charIndex: number): string {
|
|
161
|
+
const contextRadius = 100;
|
|
162
|
+
const start = Math.max(0, charIndex - contextRadius);
|
|
163
|
+
const end = Math.min(text.length, charIndex + contextRadius);
|
|
164
|
+
return text.substring(start, end).trim();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Score a mention based on position, type, and context
|
|
169
|
+
*/
|
|
170
|
+
export function scoreMention(mention: Mention | null): number {
|
|
171
|
+
if (!mention) return 0;
|
|
172
|
+
|
|
173
|
+
let score = 0;
|
|
174
|
+
|
|
175
|
+
// Position scoring (first position = highest score)
|
|
176
|
+
// Position 1 = 50 points, position 2 = 40, position 3 = 30, etc.
|
|
177
|
+
score += Math.max(0, 60 - mention.position * 10);
|
|
178
|
+
|
|
179
|
+
// Type scoring
|
|
180
|
+
if (mention.type === 'brand') {
|
|
181
|
+
score += 30;
|
|
182
|
+
} else if (mention.type === 'alternative') {
|
|
183
|
+
score += 20;
|
|
184
|
+
} else if (mention.type === 'domain') {
|
|
185
|
+
score += 15;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// Context scoring - boost for recommendation language
|
|
189
|
+
const recommendWords = ['recommend', 'best', 'excellent', 'great', 'suggest', 'top choice', 'top pick'];
|
|
190
|
+
const contextLower = mention.context.toLowerCase();
|
|
191
|
+
for (const word of recommendWords) {
|
|
192
|
+
// Use word boundary matching to avoid false positives (e.g., "top" in "Autopilot")
|
|
193
|
+
const wordRegex = new RegExp(`\\b${word}\\b`, 'i');
|
|
194
|
+
if (wordRegex.test(contextLower)) {
|
|
195
|
+
score += 10;
|
|
196
|
+
break;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return score;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Parse an LLM response to extract mention details
|
|
205
|
+
*/
|
|
206
|
+
export function parseGEOResponse(
|
|
207
|
+
response: string,
|
|
208
|
+
config: BrandConfig
|
|
209
|
+
): ParsedResponse {
|
|
210
|
+
const mentions = detectMentions(response, config);
|
|
211
|
+
|
|
212
|
+
if (mentions.length === 0) {
|
|
213
|
+
return {
|
|
214
|
+
mentioned: false,
|
|
215
|
+
position: null,
|
|
216
|
+
sentiment: null,
|
|
217
|
+
contextSnippet: null,
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// Get the first (most prominent) mention
|
|
222
|
+
const primaryMention = mentions[0];
|
|
223
|
+
|
|
224
|
+
// Determine sentiment from context
|
|
225
|
+
const sentiment = analyzeSentiment(primaryMention.context);
|
|
226
|
+
|
|
227
|
+
// Extract context snippet (max 200 chars)
|
|
228
|
+
let contextSnippet = primaryMention.context;
|
|
229
|
+
if (contextSnippet.length > 200) {
|
|
230
|
+
contextSnippet = contextSnippet.substring(0, 197) + '...';
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return {
|
|
234
|
+
mentioned: true,
|
|
235
|
+
position: primaryMention.position,
|
|
236
|
+
sentiment,
|
|
237
|
+
contextSnippet,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Simple sentiment analysis based on keyword matching
|
|
243
|
+
*/
|
|
244
|
+
function analyzeSentiment(text: string): Sentiment {
|
|
245
|
+
const lowerText = text.toLowerCase();
|
|
246
|
+
|
|
247
|
+
const positiveWords = [
|
|
248
|
+
'excellent',
|
|
249
|
+
'great',
|
|
250
|
+
'best',
|
|
251
|
+
'recommend',
|
|
252
|
+
'top',
|
|
253
|
+
'amazing',
|
|
254
|
+
'fantastic',
|
|
255
|
+
'highly',
|
|
256
|
+
'love',
|
|
257
|
+
'perfect',
|
|
258
|
+
'outstanding',
|
|
259
|
+
];
|
|
260
|
+
|
|
261
|
+
const negativeWords = [
|
|
262
|
+
'limitation',
|
|
263
|
+
'limitations',
|
|
264
|
+
'limited',
|
|
265
|
+
'not suitable',
|
|
266
|
+
'not be suitable',
|
|
267
|
+
'may not',
|
|
268
|
+
'poor',
|
|
269
|
+
'bad',
|
|
270
|
+
'avoid',
|
|
271
|
+
'issue',
|
|
272
|
+
'problem',
|
|
273
|
+
'difficult',
|
|
274
|
+
'complicated',
|
|
275
|
+
'expensive',
|
|
276
|
+
'lack',
|
|
277
|
+
'lacking',
|
|
278
|
+
'drawback',
|
|
279
|
+
'downside',
|
|
280
|
+
];
|
|
281
|
+
|
|
282
|
+
let positiveScore = 0;
|
|
283
|
+
let negativeScore = 0;
|
|
284
|
+
|
|
285
|
+
for (const word of positiveWords) {
|
|
286
|
+
if (lowerText.includes(word)) positiveScore++;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
for (const word of negativeWords) {
|
|
290
|
+
if (lowerText.includes(word)) negativeScore++;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
if (positiveScore > negativeScore) return 'positive';
|
|
294
|
+
if (negativeScore > positiveScore) return 'negative';
|
|
295
|
+
return 'neutral';
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Get the API endpoint for a provider
|
|
300
|
+
*/
|
|
301
|
+
function getProviderEndpoint(provider: LLMProvider): string {
|
|
302
|
+
switch (provider) {
|
|
303
|
+
case 'openai':
|
|
304
|
+
return 'https://api.openai.com/v1/chat/completions';
|
|
305
|
+
case 'anthropic':
|
|
306
|
+
return 'https://api.anthropic.com/v1/messages';
|
|
307
|
+
case 'google':
|
|
308
|
+
return 'https://generativelanguage.googleapis.com/v1/models/gemini-pro:generateContent';
|
|
309
|
+
case 'perplexity':
|
|
310
|
+
return 'https://api.perplexity.ai/chat/completions';
|
|
311
|
+
default:
|
|
312
|
+
throw new Error(`Unknown provider: ${provider}`);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Query a single LLM provider
|
|
318
|
+
*/
|
|
319
|
+
async function queryProvider(
|
|
320
|
+
provider: LLMProvider,
|
|
321
|
+
keyword: string,
|
|
322
|
+
fetchFn: typeof fetch
|
|
323
|
+
): Promise<string> {
|
|
324
|
+
const endpoint = getProviderEndpoint(provider);
|
|
325
|
+
|
|
326
|
+
const response = await fetchFn(endpoint, {
|
|
327
|
+
method: 'POST',
|
|
328
|
+
headers: {
|
|
329
|
+
'Content-Type': 'application/json',
|
|
330
|
+
},
|
|
331
|
+
body: JSON.stringify({
|
|
332
|
+
model: provider === 'openai' ? 'gpt-4' : undefined,
|
|
333
|
+
messages: [
|
|
334
|
+
{
|
|
335
|
+
role: 'user',
|
|
336
|
+
content: keyword,
|
|
337
|
+
},
|
|
338
|
+
],
|
|
339
|
+
}),
|
|
340
|
+
});
|
|
341
|
+
|
|
342
|
+
if (!response.ok) {
|
|
343
|
+
throw new Error(`API error: ${response.status}`);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
const data = await response.json();
|
|
347
|
+
|
|
348
|
+
// Handle OpenAI/Perplexity response format
|
|
349
|
+
if (data.choices && data.choices[0]?.message?.content) {
|
|
350
|
+
return data.choices[0].message.content;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// Handle Anthropic response format
|
|
354
|
+
if (data.content && data.content[0]?.text) {
|
|
355
|
+
return data.content[0].text;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
// Handle Google response format
|
|
359
|
+
if (data.candidates && data.candidates[0]?.content?.parts?.[0]?.text) {
|
|
360
|
+
return data.candidates[0].content.parts[0].text;
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
throw new Error('Unexpected response format');
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Track LLM visibility for a brand across multiple providers
|
|
368
|
+
*/
|
|
369
|
+
export async function trackLLMVisibility(
|
|
370
|
+
query: GEOQuery,
|
|
371
|
+
options: TrackingOptions = {}
|
|
372
|
+
): Promise<GEOResult[]> {
|
|
373
|
+
const fetchFn = options.fetch || fetch;
|
|
374
|
+
const timestamp = new Date().toISOString();
|
|
375
|
+
|
|
376
|
+
// Query all providers in parallel
|
|
377
|
+
const results = await Promise.all(
|
|
378
|
+
query.providers.map(async (provider): Promise<GEOResult> => {
|
|
379
|
+
try {
|
|
380
|
+
const response = await queryProvider(provider, query.keyword, fetchFn);
|
|
381
|
+
const parsed = parseGEOResponse(response, query.brand);
|
|
382
|
+
const mentions = detectMentions(response, query.brand);
|
|
383
|
+
const score = mentions.length > 0 ? scoreMention(mentions[0]) : 0;
|
|
384
|
+
|
|
385
|
+
return {
|
|
386
|
+
provider,
|
|
387
|
+
keyword: query.keyword,
|
|
388
|
+
mentioned: parsed.mentioned,
|
|
389
|
+
position: parsed.position,
|
|
390
|
+
sentiment: parsed.sentiment,
|
|
391
|
+
score,
|
|
392
|
+
timestamp,
|
|
393
|
+
contextSnippet: parsed.contextSnippet || undefined,
|
|
394
|
+
};
|
|
395
|
+
} catch (error) {
|
|
396
|
+
return {
|
|
397
|
+
provider,
|
|
398
|
+
keyword: query.keyword,
|
|
399
|
+
mentioned: false,
|
|
400
|
+
position: null,
|
|
401
|
+
sentiment: null,
|
|
402
|
+
score: 0,
|
|
403
|
+
timestamp,
|
|
404
|
+
error: error instanceof Error ? error.message : 'Unknown error',
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
})
|
|
408
|
+
);
|
|
409
|
+
|
|
410
|
+
return results;
|
|
411
|
+
}
|
package/src/geo/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GEO (Generative Engine Optimization) Module
|
|
3
|
+
*
|
|
4
|
+
* Track brand visibility in LLM responses across providers like
|
|
5
|
+
* OpenAI, Anthropic, Google, and Perplexity.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
export * from './geo-tracker.js';
|
|
9
|
+
export * from './geo-history.js';
|
|
10
|
+
export * from './geo-content.js';
|
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
formatConventionalCommit,
|
|
4
|
+
formatSEOCommitMessage,
|
|
5
|
+
isGitRepo,
|
|
6
|
+
detectGitHubPages,
|
|
7
|
+
generateCommitSummary,
|
|
8
|
+
COMMIT_TYPES,
|
|
9
|
+
SEO_SCOPES,
|
|
10
|
+
type ConventionalCommit,
|
|
11
|
+
type SEOFixCommit,
|
|
12
|
+
type CommitConfig,
|
|
13
|
+
} from './commit-helper.js';
|
|
14
|
+
|
|
15
|
+
describe('commit-helper', () => {
|
|
16
|
+
describe('formatConventionalCommit', () => {
|
|
17
|
+
it('formats a basic commit message', () => {
|
|
18
|
+
const commit: ConventionalCommit = {
|
|
19
|
+
type: 'fix',
|
|
20
|
+
description: 'resolve meta tag issue',
|
|
21
|
+
};
|
|
22
|
+
expect(formatConventionalCommit(commit)).toBe('fix: resolve meta tag issue');
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it('includes scope when provided', () => {
|
|
26
|
+
const commit: ConventionalCommit = {
|
|
27
|
+
type: 'fix',
|
|
28
|
+
scope: 'seo',
|
|
29
|
+
description: 'add missing title tag',
|
|
30
|
+
};
|
|
31
|
+
expect(formatConventionalCommit(commit)).toBe('fix(seo): add missing title tag');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('adds breaking change indicator', () => {
|
|
35
|
+
const commit: ConventionalCommit = {
|
|
36
|
+
type: 'feat',
|
|
37
|
+
scope: 'schema',
|
|
38
|
+
description: 'change JSON-LD format',
|
|
39
|
+
breaking: true,
|
|
40
|
+
};
|
|
41
|
+
expect(formatConventionalCommit(commit)).toBe('feat(schema)!: change JSON-LD format');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('includes body when provided', () => {
|
|
45
|
+
const commit: ConventionalCommit = {
|
|
46
|
+
type: 'fix',
|
|
47
|
+
scope: 'meta',
|
|
48
|
+
description: 'add meta description',
|
|
49
|
+
body: 'Added meta descriptions to all pages.\nThis improves SEO score.',
|
|
50
|
+
};
|
|
51
|
+
const result = formatConventionalCommit(commit);
|
|
52
|
+
expect(result).toContain('fix(meta): add meta description');
|
|
53
|
+
expect(result).toContain('Added meta descriptions to all pages.');
|
|
54
|
+
expect(result).toContain('This improves SEO score.');
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('includes issue codes when provided', () => {
|
|
58
|
+
const commit: ConventionalCommit = {
|
|
59
|
+
type: 'fix',
|
|
60
|
+
scope: 'seo',
|
|
61
|
+
description: 'fix crawlability issues',
|
|
62
|
+
issues: ['ROBOTS_TXT_MISSING', 'SITEMAP_MISSING'],
|
|
63
|
+
};
|
|
64
|
+
const result = formatConventionalCommit(commit);
|
|
65
|
+
expect(result).toContain('Issue codes: ROBOTS_TXT_MISSING, SITEMAP_MISSING');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('formatSEOCommitMessage', () => {
|
|
70
|
+
it('formats SEO fix with co-author', () => {
|
|
71
|
+
const fix: SEOFixCommit = {
|
|
72
|
+
category: 'on-page',
|
|
73
|
+
issues: ['TITLE_MISSING', 'META_DESC_MISSING'],
|
|
74
|
+
filesChanged: ['index.html'],
|
|
75
|
+
description: 'add on page improvements',
|
|
76
|
+
};
|
|
77
|
+
const config: CommitConfig = {
|
|
78
|
+
systemName: 'SEO Autopilot',
|
|
79
|
+
systemEmail: 'bot@seo-autopilot.dev',
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const result = formatSEOCommitMessage(fix, config);
|
|
83
|
+
|
|
84
|
+
expect(result).toContain('fix(meta): add on page improvements');
|
|
85
|
+
expect(result).toContain('Files changed:');
|
|
86
|
+
expect(result).toContain('- index.html');
|
|
87
|
+
expect(result).toContain('Issue codes: TITLE_MISSING, META_DESC_MISSING');
|
|
88
|
+
expect(result).toContain('Co-Authored-By: SEO Autopilot <bot@seo-autopilot.dev>');
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('maps categories to correct commit types', () => {
|
|
92
|
+
const testCases = [
|
|
93
|
+
{ category: 'crawlability', expectedType: 'fix', expectedScope: 'seo' },
|
|
94
|
+
{ category: 'on-page', expectedType: 'fix', expectedScope: 'meta' },
|
|
95
|
+
{ category: 'structured-data', expectedType: 'feat', expectedScope: 'schema' },
|
|
96
|
+
{ category: 'performance', expectedType: 'perf', expectedScope: 'web' },
|
|
97
|
+
{ category: 'social-meta', expectedType: 'feat', expectedScope: 'og' },
|
|
98
|
+
{ category: 'images', expectedType: 'fix', expectedScope: 'images' },
|
|
99
|
+
{ category: 'links', expectedType: 'fix', expectedScope: 'links' },
|
|
100
|
+
{ category: 'security', expectedType: 'fix', expectedScope: 'security' },
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
testCases.forEach(({ category, expectedType, expectedScope }) => {
|
|
104
|
+
const fix: SEOFixCommit = {
|
|
105
|
+
category,
|
|
106
|
+
issues: ['TEST_ISSUE'],
|
|
107
|
+
filesChanged: ['test.html'],
|
|
108
|
+
description: 'test fix',
|
|
109
|
+
};
|
|
110
|
+
const result = formatSEOCommitMessage(fix);
|
|
111
|
+
expect(result).toContain(`${expectedType}(${expectedScope}): test fix`);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('uses default scope for unknown categories', () => {
|
|
116
|
+
const fix: SEOFixCommit = {
|
|
117
|
+
category: 'unknown-category',
|
|
118
|
+
issues: ['TEST_ISSUE'],
|
|
119
|
+
filesChanged: ['test.html'],
|
|
120
|
+
description: 'test fix',
|
|
121
|
+
};
|
|
122
|
+
const result = formatSEOCommitMessage(fix);
|
|
123
|
+
expect(result).toContain('fix(seo): test fix');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('handles multiple files', () => {
|
|
127
|
+
const fix: SEOFixCommit = {
|
|
128
|
+
category: 'on-page',
|
|
129
|
+
issues: ['TEST'],
|
|
130
|
+
filesChanged: ['index.html', 'about.html', 'contact.html'],
|
|
131
|
+
description: 'fix meta tags',
|
|
132
|
+
};
|
|
133
|
+
const result = formatSEOCommitMessage(fix);
|
|
134
|
+
expect(result).toContain('- index.html');
|
|
135
|
+
expect(result).toContain('- about.html');
|
|
136
|
+
expect(result).toContain('- contact.html');
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('uses custom system name and email', () => {
|
|
140
|
+
const fix: SEOFixCommit = {
|
|
141
|
+
category: 'on-page',
|
|
142
|
+
issues: ['TEST'],
|
|
143
|
+
filesChanged: ['index.html'],
|
|
144
|
+
description: 'test',
|
|
145
|
+
};
|
|
146
|
+
const config: CommitConfig = {
|
|
147
|
+
systemName: 'Custom Bot',
|
|
148
|
+
systemEmail: 'custom@example.com',
|
|
149
|
+
};
|
|
150
|
+
const result = formatSEOCommitMessage(fix, config);
|
|
151
|
+
expect(result).toContain('Co-Authored-By: Custom Bot <custom@example.com>');
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
describe('generateCommitSummary', () => {
|
|
156
|
+
it('generates summary for successful commits', () => {
|
|
157
|
+
const commits = [
|
|
158
|
+
{
|
|
159
|
+
fix: {
|
|
160
|
+
category: 'on-page',
|
|
161
|
+
issues: ['TITLE_MISSING'],
|
|
162
|
+
filesChanged: ['index.html'],
|
|
163
|
+
description: 'add title tags',
|
|
164
|
+
},
|
|
165
|
+
result: { success: true, hash: 'abc1234' },
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
fix: {
|
|
169
|
+
category: 'social-meta',
|
|
170
|
+
issues: ['OG_TITLE_MISSING'],
|
|
171
|
+
filesChanged: ['index.html'],
|
|
172
|
+
description: 'add og tags',
|
|
173
|
+
},
|
|
174
|
+
result: { success: true, hash: 'def5678' },
|
|
175
|
+
},
|
|
176
|
+
];
|
|
177
|
+
|
|
178
|
+
const summary = generateCommitSummary(commits);
|
|
179
|
+
|
|
180
|
+
expect(summary).toContain('## SEO Fix Commits Summary');
|
|
181
|
+
expect(summary).toContain('2 commits created');
|
|
182
|
+
expect(summary).toContain('abc1234');
|
|
183
|
+
expect(summary).toContain('def5678');
|
|
184
|
+
expect(summary).toContain('add title tags');
|
|
185
|
+
expect(summary).toContain('add og tags');
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('includes failed commits in summary', () => {
|
|
189
|
+
const commits = [
|
|
190
|
+
{
|
|
191
|
+
fix: {
|
|
192
|
+
category: 'on-page',
|
|
193
|
+
issues: ['TITLE_MISSING'],
|
|
194
|
+
filesChanged: ['index.html'],
|
|
195
|
+
description: 'add title tags',
|
|
196
|
+
},
|
|
197
|
+
result: { success: true, hash: 'abc1234' },
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
fix: {
|
|
201
|
+
category: 'crawlability',
|
|
202
|
+
issues: ['ROBOTS_TXT_MISSING'],
|
|
203
|
+
filesChanged: ['robots.txt'],
|
|
204
|
+
description: 'add robots.txt',
|
|
205
|
+
},
|
|
206
|
+
result: { success: false, error: 'Permission denied' },
|
|
207
|
+
},
|
|
208
|
+
];
|
|
209
|
+
|
|
210
|
+
const summary = generateCommitSummary(commits);
|
|
211
|
+
|
|
212
|
+
expect(summary).toContain('1 commits created');
|
|
213
|
+
expect(summary).toContain('1 commits failed');
|
|
214
|
+
expect(summary).toContain('### Failed Commits');
|
|
215
|
+
expect(summary).toContain('Permission denied');
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
describe('COMMIT_TYPES and SEO_SCOPES', () => {
|
|
220
|
+
it('has all expected commit types', () => {
|
|
221
|
+
const types = COMMIT_TYPES.map((t) => t.type);
|
|
222
|
+
expect(types).toContain('fix');
|
|
223
|
+
expect(types).toContain('feat');
|
|
224
|
+
expect(types).toContain('docs');
|
|
225
|
+
expect(types).toContain('perf');
|
|
226
|
+
expect(types).toContain('chore');
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('has all expected SEO scopes', () => {
|
|
230
|
+
expect(SEO_SCOPES).toContain('seo');
|
|
231
|
+
expect(SEO_SCOPES).toContain('meta');
|
|
232
|
+
expect(SEO_SCOPES).toContain('og');
|
|
233
|
+
expect(SEO_SCOPES).toContain('schema');
|
|
234
|
+
expect(SEO_SCOPES).toContain('images');
|
|
235
|
+
expect(SEO_SCOPES).toContain('links');
|
|
236
|
+
expect(SEO_SCOPES).toContain('security');
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
describe('detectGitHubPages', () => {
|
|
242
|
+
// These tests would need filesystem mocking
|
|
243
|
+
// For now, we test the function exists and returns expected shape
|
|
244
|
+
it('returns expected structure', () => {
|
|
245
|
+
// Test with a non-existent path returns false
|
|
246
|
+
const result = detectGitHubPages('/non/existent/path');
|
|
247
|
+
expect(result).toHaveProperty('isGitHubPages');
|
|
248
|
+
expect(typeof result.isGitHubPages).toBe('boolean');
|
|
249
|
+
});
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
describe('isGitRepo', () => {
|
|
253
|
+
it('returns false for non-git directory', () => {
|
|
254
|
+
expect(isGitRepo('/tmp')).toBe(false);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('returns boolean', () => {
|
|
258
|
+
const result = isGitRepo();
|
|
259
|
+
expect(typeof result).toBe('boolean');
|
|
260
|
+
});
|
|
261
|
+
});
|