sanity-plugin-seo-copilot 0.2.15 → 0.2.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.
- package/README.md +63 -50
- package/dist/index.js +841 -782
- package/dist/index.mjs +763 -704
- package/package.json +62 -64
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/sanity.json +0 -3
- package/src/components/ChatPanel.tsx +0 -311
- package/src/components/ConfigContext.tsx +0 -22
- package/src/components/FeedbackButton.tsx +0 -218
- package/src/components/KeywordBadge.tsx +0 -50
- package/src/components/KeywordPanel.tsx +0 -1234
- package/src/components/OnboardingPopup.tsx +0 -108
- package/src/components/PlacementIndicators.tsx +0 -80
- package/src/components/ReportProblemButton.tsx +0 -212
- package/src/components/SEOCopilotPanel.tsx +0 -1287
- package/src/components/ScoreDonut.tsx +0 -158
- package/src/components/SeedKeywordInput.tsx +0 -128
- package/src/components/ShimmerLoader.tsx +0 -123
- package/src/components/TabBar.tsx +0 -373
- package/src/components/styles.ts +0 -158
- package/src/components/tabs/AEOTab.tsx +0 -585
- package/src/components/tabs/LinkingTab.tsx +0 -446
- package/src/components/tabs/MetaTab.tsx +0 -431
- package/src/components/tabs/SummaryTab.tsx +0 -405
- package/src/components/tabs/TechnicalTab.tsx +0 -933
- package/src/hooks/useDocumentContent.ts +0 -48
- package/src/hooks/useKeywordAnalysis.ts +0 -292
- package/src/hooks/useSEOChat.ts +0 -254
- package/src/index.ts +0 -18
- package/src/inspector.ts +0 -58
- package/src/lib/api.ts +0 -262
- package/src/lib/apiKey.ts +0 -30
- package/src/lib/documentExtract.ts +0 -422
- package/src/lib/geoAnalysis.ts +0 -122
- package/src/lib/persistence.ts +0 -69
- package/src/lib/scoring.ts +0 -197
- package/src/lib/summary.ts +0 -645
- package/src/lib/types.ts +0 -212
- package/src/lib/utils.ts +0 -35
- package/v2-incompatible.js +0 -10
package/src/lib/api.ts
DELETED
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
import type { SEOAnalysis, DeepAnalysis, ChatMessage, DocumentFields, ContentSuggestions, LinkingSuggestion, LinkCheckResult } from "./types";
|
|
2
|
-
import { authHeaders, jsonHeaders } from "./apiKey";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* Safely parse JSON from a response that may have been truncated by
|
|
6
|
-
* Vercel's function timeout (HTML error appended after valid JSON).
|
|
7
|
-
*/
|
|
8
|
-
function safeParseJson<T>(text: string): T {
|
|
9
|
-
try {
|
|
10
|
-
return JSON.parse(text) as T;
|
|
11
|
-
} catch {
|
|
12
|
-
// Find the last } or ] and try parsing up to that point
|
|
13
|
-
for (let i = text.length - 1; i >= 0; i--) {
|
|
14
|
-
if (text[i] === "}" || text[i] === "]") {
|
|
15
|
-
try {
|
|
16
|
-
return JSON.parse(text.substring(0, i + 1)) as T;
|
|
17
|
-
} catch {
|
|
18
|
-
continue;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
throw new Error("Invalid JSON response from server");
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export async function fetchAnalysis(
|
|
27
|
-
apiUrl: string,
|
|
28
|
-
content: string,
|
|
29
|
-
mode: "realtime" | "deep",
|
|
30
|
-
publication?: string,
|
|
31
|
-
seedKeywords?: string[],
|
|
32
|
-
documentFields?: DocumentFields,
|
|
33
|
-
focusKeyword?: string,
|
|
34
|
-
secondaryKeyword?: string,
|
|
35
|
-
signal?: AbortSignal
|
|
36
|
-
): Promise<SEOAnalysis | DeepAnalysis> {
|
|
37
|
-
const response = await fetch(`${apiUrl}/api/seo-copilot/analyze`, {
|
|
38
|
-
method: "POST",
|
|
39
|
-
headers: jsonHeaders(),
|
|
40
|
-
body: JSON.stringify({ content, mode, publication, seedKeywords, documentFields, focusKeyword, secondaryKeyword }),
|
|
41
|
-
signal,
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
const text = await response.text();
|
|
45
|
-
|
|
46
|
-
if (!response.ok) {
|
|
47
|
-
if (response.status === 429) throw new Error("Analysis service is busy. Please try again.");
|
|
48
|
-
if (response.status >= 500) throw new Error("Could not reach the SEO service.");
|
|
49
|
-
throw new Error("Something went wrong analyzing this document.");
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return safeParseJson<SEOAnalysis | DeepAnalysis>(text);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export async function fetchChatStream(
|
|
56
|
-
apiUrl: string,
|
|
57
|
-
messages: ChatMessage[],
|
|
58
|
-
documentContext: string,
|
|
59
|
-
publication?: string,
|
|
60
|
-
keywordPanelData?: {
|
|
61
|
-
primaryKeyword?: { term: string; volume: number | null };
|
|
62
|
-
supportingKeywords?: { term: string; volume: number | null }[];
|
|
63
|
-
missingKeywords?: { term: string; volume: number | null }[];
|
|
64
|
-
aeoData?: {
|
|
65
|
-
questionHeadings: { suggestedHeading: string; rationale: string }[];
|
|
66
|
-
};
|
|
67
|
-
},
|
|
68
|
-
signal?: AbortSignal
|
|
69
|
-
): Promise<ReadableStream<Uint8Array> | null> {
|
|
70
|
-
const response = await fetch(`${apiUrl}/api/seo-copilot/chat`, {
|
|
71
|
-
method: "POST",
|
|
72
|
-
headers: jsonHeaders(),
|
|
73
|
-
body: JSON.stringify({ messages, documentContext, publication, keywordPanelData }),
|
|
74
|
-
signal,
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
if (!response.ok) {
|
|
78
|
-
if (response.status === 429) throw new Error("Analysis service is busy. Please try again.");
|
|
79
|
-
if (response.status >= 500) throw new Error("Could not reach the SEO service.");
|
|
80
|
-
throw new Error("Something went wrong. Please try again.");
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
return response.body;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export async function fetchQuestions(
|
|
87
|
-
apiUrl: string,
|
|
88
|
-
keyword: string,
|
|
89
|
-
signal?: AbortSignal
|
|
90
|
-
): Promise<{ questions: { question: string; volume: number; cpc: number; competition: number }[]; apiUnitsUsed: number }> {
|
|
91
|
-
const response = await fetch(`${apiUrl}/api/seo-copilot/questions`, {
|
|
92
|
-
method: "POST",
|
|
93
|
-
headers: jsonHeaders(),
|
|
94
|
-
body: JSON.stringify({ keyword }),
|
|
95
|
-
signal,
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
if (!response.ok) {
|
|
99
|
-
if (response.status === 429) throw new Error("Analysis service is busy. Please try again.");
|
|
100
|
-
if (response.status >= 500) throw new Error("Could not reach the SEO service.");
|
|
101
|
-
throw new Error("Something went wrong. Please try again.");
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
return response.json();
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Telemetry attached to a "Report a problem" submission - which surface/tab the editor
|
|
108
|
-
// was on, etc. Keys must match the backend's strict schema in feedback/route.ts.
|
|
109
|
-
export interface FeedbackContext {
|
|
110
|
-
surface?: string;
|
|
111
|
-
activeTab?: string;
|
|
112
|
-
primaryKeyword?: string;
|
|
113
|
-
documentType?: string;
|
|
114
|
-
url?: string;
|
|
115
|
-
userAgent?: string;
|
|
116
|
-
appVersion?: string;
|
|
117
|
-
timestamp?: string;
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
export async function submitFeedback(
|
|
121
|
-
apiUrl: string,
|
|
122
|
-
data: { suggestion_type: string; suggestion_text: string; vote: "up" | "down"; document_id?: string; publication?: string; user_id?: string; context?: FeedbackContext }
|
|
123
|
-
): Promise<void> {
|
|
124
|
-
const res = await fetch(`${apiUrl}/api/seo-copilot/feedback`, {
|
|
125
|
-
method: "POST",
|
|
126
|
-
headers: jsonHeaders(),
|
|
127
|
-
body: JSON.stringify(data),
|
|
128
|
-
});
|
|
129
|
-
// Surface HTTP failures (500, 429, ...) so the caller can show a real error instead of a
|
|
130
|
-
// false "Thanks" - fetch() only rejects on network errors, not on a bad status.
|
|
131
|
-
if (!res.ok) throw new Error(`Feedback submit failed: HTTP ${res.status}`);
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
export async function lookupKeywords(
|
|
135
|
-
apiUrl: string,
|
|
136
|
-
keywords: string[],
|
|
137
|
-
signal?: AbortSignal
|
|
138
|
-
): Promise<{ results: { keyword: string; volume: number; cpc: number; competition: number }[]; apiUnitsUsed: number }> {
|
|
139
|
-
const response = await fetch(`${apiUrl}/api/seo-copilot/keywords`, {
|
|
140
|
-
method: "POST",
|
|
141
|
-
headers: jsonHeaders(),
|
|
142
|
-
body: JSON.stringify({ keywords }),
|
|
143
|
-
signal,
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
if (!response.ok) {
|
|
147
|
-
if (response.status === 429) throw new Error("Analysis service is busy. Please try again.");
|
|
148
|
-
if (response.status >= 500) throw new Error("Could not reach the SEO service.");
|
|
149
|
-
throw new Error("Something went wrong. Please try again.");
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
return response.json();
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export async function loadChatHistory(
|
|
156
|
-
apiUrl: string,
|
|
157
|
-
userId: string,
|
|
158
|
-
documentId: string
|
|
159
|
-
): Promise<ChatMessage[]> {
|
|
160
|
-
try {
|
|
161
|
-
const res = await fetch(
|
|
162
|
-
`${apiUrl}/api/seo-copilot/chat-history?userId=${encodeURIComponent(userId)}&documentId=${encodeURIComponent(documentId)}`,
|
|
163
|
-
{ headers: authHeaders() }
|
|
164
|
-
);
|
|
165
|
-
if (!res.ok) return [];
|
|
166
|
-
const data = await res.json();
|
|
167
|
-
return (data.messages as ChatMessage[]) || [];
|
|
168
|
-
} catch {
|
|
169
|
-
return [];
|
|
170
|
-
}
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
export async function fetchContentSuggestions(
|
|
174
|
-
apiUrl: string,
|
|
175
|
-
content: string,
|
|
176
|
-
primaryKeyword: string,
|
|
177
|
-
publication?: string,
|
|
178
|
-
signal?: AbortSignal
|
|
179
|
-
): Promise<ContentSuggestions> {
|
|
180
|
-
const empty: ContentSuggestions = { keyTakeaways: [], infographicOpportunities: [], bulletListItems: [] };
|
|
181
|
-
try {
|
|
182
|
-
const res = await fetch(`${apiUrl}/api/seo-copilot/content-suggestions`, {
|
|
183
|
-
method: "POST",
|
|
184
|
-
headers: jsonHeaders(),
|
|
185
|
-
body: JSON.stringify({ content, primaryKeyword, publication }),
|
|
186
|
-
signal,
|
|
187
|
-
});
|
|
188
|
-
if (!res.ok) return empty;
|
|
189
|
-
const data = await res.json().catch(() => null);
|
|
190
|
-
if (!data || typeof data !== "object") return empty;
|
|
191
|
-
return {
|
|
192
|
-
keyTakeaways: Array.isArray(data.keyTakeaways) ? data.keyTakeaways : [],
|
|
193
|
-
infographicOpportunities: Array.isArray(data.infographicOpportunities) ? data.infographicOpportunities : [],
|
|
194
|
-
bulletListItems: Array.isArray(data.bulletListItems) ? data.bulletListItems : [],
|
|
195
|
-
};
|
|
196
|
-
} catch {
|
|
197
|
-
return empty;
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
export async function fetchLinkingSuggestions(
|
|
202
|
-
apiUrl: string,
|
|
203
|
-
content: string,
|
|
204
|
-
primaryKeyword: string,
|
|
205
|
-
supportingKeywords: string[],
|
|
206
|
-
currentDocumentId?: string,
|
|
207
|
-
publication?: string,
|
|
208
|
-
signal?: AbortSignal
|
|
209
|
-
): Promise<LinkingSuggestion[]> {
|
|
210
|
-
if (!content || content.trim().length < 50) return [];
|
|
211
|
-
try {
|
|
212
|
-
const res = await fetch(`${apiUrl}/api/seo-copilot/linking-suggestions`, {
|
|
213
|
-
method: "POST",
|
|
214
|
-
headers: jsonHeaders(),
|
|
215
|
-
body: JSON.stringify({ content, primaryKeyword, supportingKeywords, currentDocumentId, publication }),
|
|
216
|
-
signal,
|
|
217
|
-
});
|
|
218
|
-
if (!res.ok) return [];
|
|
219
|
-
const data = await res.json().catch(() => null);
|
|
220
|
-
if (!data || !Array.isArray(data.suggestions)) return [];
|
|
221
|
-
return data.suggestions as LinkingSuggestion[];
|
|
222
|
-
} catch {
|
|
223
|
-
return [];
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
// Calls the backend broken-link scanner. Returns one result per unique URL,
|
|
228
|
-
// or an empty array on failure (so callers don't have to handle errors).
|
|
229
|
-
export async function checkLinks(apiUrl: string, urls: string[]): Promise<LinkCheckResult[]> {
|
|
230
|
-
if (!urls || urls.length === 0) return [];
|
|
231
|
-
try {
|
|
232
|
-
const res = await fetch(`${apiUrl}/api/seo-copilot/check-links`, {
|
|
233
|
-
method: "POST",
|
|
234
|
-
headers: jsonHeaders(),
|
|
235
|
-
body: JSON.stringify({ urls }),
|
|
236
|
-
});
|
|
237
|
-
if (!res.ok) return [];
|
|
238
|
-
const data = await res.json().catch(() => null);
|
|
239
|
-
if (!data || !Array.isArray(data.results)) return [];
|
|
240
|
-
return data.results as LinkCheckResult[];
|
|
241
|
-
} catch {
|
|
242
|
-
return [];
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
export async function saveChatHistory(
|
|
247
|
-
apiUrl: string,
|
|
248
|
-
userId: string,
|
|
249
|
-
documentId: string,
|
|
250
|
-
messages: ChatMessage[],
|
|
251
|
-
publication?: string
|
|
252
|
-
): Promise<void> {
|
|
253
|
-
try {
|
|
254
|
-
await fetch(`${apiUrl}/api/seo-copilot/chat-history`, {
|
|
255
|
-
method: "POST",
|
|
256
|
-
headers: jsonHeaders(),
|
|
257
|
-
body: JSON.stringify({ userId, documentId, messages, publication }),
|
|
258
|
-
});
|
|
259
|
-
} catch {
|
|
260
|
-
// Silent - history save should never break chat
|
|
261
|
-
}
|
|
262
|
-
}
|
package/src/lib/apiKey.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Shared API key for the SEO Copilot backend.
|
|
3
|
-
*
|
|
4
|
-
* Every /api/seo-copilot/* route requires a matching `x-api-key` header
|
|
5
|
-
* (see PR #33). It is injected into every request from here so call sites do
|
|
6
|
-
* not have to thread it through their signatures.
|
|
7
|
-
*
|
|
8
|
-
* This is NOT a secret and there is deliberately no way to configure it. The
|
|
9
|
-
* plugin runs in the browser and ships as a public npm package, so any value
|
|
10
|
-
* placed here is readable by anyone. An earlier revision exposed a
|
|
11
|
-
* `seoCopilot({ apiKey })` option documented as "supply the value of
|
|
12
|
-
* SEO_COPILOT_API_KEY from the backend deployment", which invited operators to
|
|
13
|
-
* paste the genuine server credential into a public bundle. The option was
|
|
14
|
-
* removed rather than documented around: the only key a browser client can
|
|
15
|
-
* hold is one that is already public.
|
|
16
|
-
*
|
|
17
|
-
* Must stay in lockstep with SHARED_CLIENT_KEY in the backend's
|
|
18
|
-
* src/lib/seo-copilot/sharedKey.ts. The backend test pins this exact literal.
|
|
19
|
-
*/
|
|
20
|
-
export const SHARED_CLIENT_KEY = "seo-copilot-public-client-key-v1";
|
|
21
|
-
|
|
22
|
-
/** Auth headers for a backend call. */
|
|
23
|
-
export function authHeaders(): Record<string, string> {
|
|
24
|
-
return { "x-api-key": SHARED_CLIENT_KEY };
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** Standard JSON request headers plus auth. */
|
|
28
|
-
export function jsonHeaders(): Record<string, string> {
|
|
29
|
-
return { "Content-Type": "application/json", ...authHeaders() };
|
|
30
|
-
}
|