sanity-plugin-seo-copilot 0.1.0
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 +50 -0
- package/dist/index.d.mts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +2706 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +2671 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +56 -0
- package/sanity.json +3 -0
- package/src/components/ChatPanel.tsx +291 -0
- package/src/components/ConfigContext.tsx +22 -0
- package/src/components/FeedbackButton.tsx +208 -0
- package/src/components/KeywordBadge.tsx +50 -0
- package/src/components/KeywordPanel.tsx +1165 -0
- package/src/components/PlacementIndicators.tsx +76 -0
- package/src/components/SEOCopilotPanel.tsx +550 -0
- package/src/components/SeedKeywordInput.tsx +125 -0
- package/src/components/styles.ts +77 -0
- package/src/hooks/useDocumentContent.ts +99 -0
- package/src/hooks/useKeywordAnalysis.ts +225 -0
- package/src/hooks/useSEOChat.ts +169 -0
- package/src/index.ts +16 -0
- package/src/inspector.ts +51 -0
- package/src/lib/api.ts +175 -0
- package/src/lib/types.ts +108 -0
- package/v2-incompatible.js +10 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { definePlugin } from "sanity";
|
|
2
|
+
import { seoCopilotInspector } from "./inspector";
|
|
3
|
+
|
|
4
|
+
export interface SEOCopilotConfig {
|
|
5
|
+
apiUrl: string;
|
|
6
|
+
publication?: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export const seoCopilot = definePlugin<SEOCopilotConfig>((config) => ({
|
|
10
|
+
name: "seo-copilot",
|
|
11
|
+
document: {
|
|
12
|
+
inspectors: (prev) => {
|
|
13
|
+
return [...prev, seoCopilotInspector(config)];
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
}));
|
package/src/inspector.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { type DocumentInspector, type DocumentInspectorProps } from "sanity";
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import type { SEOCopilotConfig } from "./index";
|
|
4
|
+
import SEOCopilotPanel from "./components/SEOCopilotPanel";
|
|
5
|
+
|
|
6
|
+
function SEOIcon() {
|
|
7
|
+
return createElement("svg", { viewBox: "0 0 25 25", width: "1em", height: "1em", fill: "none" },
|
|
8
|
+
createElement("circle", { cx: "12.5", cy: "12.5", r: "11.5", fill: "#E6C01B" }),
|
|
9
|
+
createElement("text", {
|
|
10
|
+
x: "12.5", y: "7.5",
|
|
11
|
+
textAnchor: "middle",
|
|
12
|
+
dominantBaseline: "central",
|
|
13
|
+
fill: "white",
|
|
14
|
+
fontSize: "7",
|
|
15
|
+
fontWeight: "900",
|
|
16
|
+
fontFamily: "Arial Black, Impact, sans-serif",
|
|
17
|
+
}, "S"),
|
|
18
|
+
createElement("text", {
|
|
19
|
+
x: "12.5", y: "12.8",
|
|
20
|
+
textAnchor: "middle",
|
|
21
|
+
dominantBaseline: "central",
|
|
22
|
+
fill: "white",
|
|
23
|
+
fontSize: "7",
|
|
24
|
+
fontWeight: "900",
|
|
25
|
+
fontFamily: "Arial Black, Impact, sans-serif",
|
|
26
|
+
}, "E"),
|
|
27
|
+
createElement("text", {
|
|
28
|
+
x: "12.5", y: "18",
|
|
29
|
+
textAnchor: "middle",
|
|
30
|
+
dominantBaseline: "central",
|
|
31
|
+
fill: "white",
|
|
32
|
+
fontSize: "7",
|
|
33
|
+
fontWeight: "900",
|
|
34
|
+
fontFamily: "Arial Black, Impact, sans-serif",
|
|
35
|
+
}, "O")
|
|
36
|
+
);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function seoCopilotInspector(config: SEOCopilotConfig): DocumentInspector {
|
|
40
|
+
return {
|
|
41
|
+
name: "seo-copilot",
|
|
42
|
+
useMenuItem: () => ({
|
|
43
|
+
title: "SEO Copilot",
|
|
44
|
+
icon: SEOIcon,
|
|
45
|
+
showAsAction: true,
|
|
46
|
+
}),
|
|
47
|
+
component: function SEOCopilotInspector(props: DocumentInspectorProps) {
|
|
48
|
+
return createElement(SEOCopilotPanel, { ...props, config });
|
|
49
|
+
},
|
|
50
|
+
};
|
|
51
|
+
}
|
package/src/lib/api.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import type { SEOAnalysis, DeepAnalysis, ChatMessage, DocumentFields } from "./types";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Safely parse JSON from a response that may have been truncated by
|
|
5
|
+
* Vercel's function timeout (HTML error appended after valid JSON).
|
|
6
|
+
*/
|
|
7
|
+
function safeParseJson<T>(text: string): T {
|
|
8
|
+
try {
|
|
9
|
+
return JSON.parse(text) as T;
|
|
10
|
+
} catch {
|
|
11
|
+
// Find the last } or ] and try parsing up to that point
|
|
12
|
+
for (let i = text.length - 1; i >= 0; i--) {
|
|
13
|
+
if (text[i] === "}" || text[i] === "]") {
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(text.substring(0, i + 1)) as T;
|
|
16
|
+
} catch {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
throw new Error("Invalid JSON response from server");
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function fetchAnalysis(
|
|
26
|
+
apiUrl: string,
|
|
27
|
+
content: string,
|
|
28
|
+
mode: "realtime" | "deep",
|
|
29
|
+
publication?: string,
|
|
30
|
+
seedKeywords?: string[],
|
|
31
|
+
documentFields?: DocumentFields,
|
|
32
|
+
focusKeyword?: string
|
|
33
|
+
): Promise<SEOAnalysis | DeepAnalysis> {
|
|
34
|
+
const response = await fetch(`${apiUrl}/api/seo-copilot/analyze`, {
|
|
35
|
+
method: "POST",
|
|
36
|
+
headers: { "Content-Type": "application/json" },
|
|
37
|
+
body: JSON.stringify({ content, mode, publication, seedKeywords, documentFields, focusKeyword }),
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const text = await response.text();
|
|
41
|
+
|
|
42
|
+
if (!response.ok) {
|
|
43
|
+
const err = safeParseJson<{ error?: string }>(text);
|
|
44
|
+
throw new Error(err.error || `HTTP ${response.status}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return safeParseJson<SEOAnalysis | DeepAnalysis>(text);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function fetchChatStream(
|
|
51
|
+
apiUrl: string,
|
|
52
|
+
messages: ChatMessage[],
|
|
53
|
+
documentContext: string,
|
|
54
|
+
publication?: string,
|
|
55
|
+
keywordPanelData?: {
|
|
56
|
+
primaryKeyword?: { term: string; volume: number | null };
|
|
57
|
+
supportingKeywords?: { term: string; volume: number | null }[];
|
|
58
|
+
missingKeywords?: { term: string; volume: number | null }[];
|
|
59
|
+
aeoData?: {
|
|
60
|
+
questionHeadings: { suggestedHeading: string; rationale: string }[];
|
|
61
|
+
faqSuggestions: { question: string; answer: string }[];
|
|
62
|
+
};
|
|
63
|
+
}
|
|
64
|
+
): Promise<ReadableStream<Uint8Array> | null> {
|
|
65
|
+
const response = await fetch(`${apiUrl}/api/seo-copilot/chat`, {
|
|
66
|
+
method: "POST",
|
|
67
|
+
headers: { "Content-Type": "application/json" },
|
|
68
|
+
body: JSON.stringify({ messages, documentContext, publication, keywordPanelData }),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
if (!response.ok) {
|
|
72
|
+
const err = await response.json().catch(() => ({ error: "Request failed" }));
|
|
73
|
+
throw new Error(err.error || `HTTP ${response.status}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return response.body;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export async function fetchQuestions(
|
|
80
|
+
apiUrl: string,
|
|
81
|
+
keyword: string
|
|
82
|
+
): Promise<{ questions: { question: string; volume: number; cpc: number; competition: number }[]; apiUnitsUsed: number }> {
|
|
83
|
+
const response = await fetch(`${apiUrl}/api/seo-copilot/questions`, {
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers: { "Content-Type": "application/json" },
|
|
86
|
+
body: JSON.stringify({ keyword }),
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
if (!response.ok) {
|
|
90
|
+
const err = await response.json().catch(() => ({ error: "Request failed" }));
|
|
91
|
+
throw new Error(err.error || `HTTP ${response.status}`);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return response.json();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function submitFeedback(
|
|
98
|
+
apiUrl: string,
|
|
99
|
+
data: { suggestion_type: string; suggestion_text: string; vote: "up" | "down"; document_id?: string; publication?: string; user_id?: string }
|
|
100
|
+
): Promise<void> {
|
|
101
|
+
await fetch(`${apiUrl}/api/seo-copilot/feedback`, {
|
|
102
|
+
method: "POST",
|
|
103
|
+
headers: { "Content-Type": "application/json" },
|
|
104
|
+
body: JSON.stringify(data),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export async function lookupKeywords(
|
|
109
|
+
apiUrl: string,
|
|
110
|
+
keywords: string[]
|
|
111
|
+
): Promise<{ results: { keyword: string; volume: number; cpc: number; competition: number }[]; apiUnitsUsed: number }> {
|
|
112
|
+
const response = await fetch(`${apiUrl}/api/seo-copilot/keywords`, {
|
|
113
|
+
method: "POST",
|
|
114
|
+
headers: { "Content-Type": "application/json" },
|
|
115
|
+
body: JSON.stringify({ keywords }),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (!response.ok) {
|
|
119
|
+
const err = await response.json().catch(() => ({ error: "Request failed" }));
|
|
120
|
+
throw new Error(err.error || `HTTP ${response.status}`);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return response.json();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export async function loadChatHistory(
|
|
127
|
+
apiUrl: string,
|
|
128
|
+
userId: string,
|
|
129
|
+
documentId: string
|
|
130
|
+
): Promise<ChatMessage[]> {
|
|
131
|
+
try {
|
|
132
|
+
const res = await fetch(
|
|
133
|
+
`${apiUrl}/api/seo-copilot/chat-history?userId=${encodeURIComponent(userId)}&documentId=${encodeURIComponent(documentId)}`
|
|
134
|
+
);
|
|
135
|
+
if (!res.ok) return [];
|
|
136
|
+
const data = await res.json();
|
|
137
|
+
return (data.messages as ChatMessage[]) || [];
|
|
138
|
+
} catch {
|
|
139
|
+
return [];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
export async function fetchMoreFAQs(
|
|
144
|
+
apiUrl: string,
|
|
145
|
+
content: string,
|
|
146
|
+
existingQuestions: string[],
|
|
147
|
+
publication?: string
|
|
148
|
+
): Promise<{ question: string; answer: string }[]> {
|
|
149
|
+
const res = await fetch(`${apiUrl}/api/seo-copilot/more-faqs`, {
|
|
150
|
+
method: "POST",
|
|
151
|
+
headers: { "Content-Type": "application/json" },
|
|
152
|
+
body: JSON.stringify({ content, existingQuestions, publication }),
|
|
153
|
+
});
|
|
154
|
+
if (!res.ok) return [];
|
|
155
|
+
const data = await res.json();
|
|
156
|
+
return data.faqSuggestions || [];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export async function saveChatHistory(
|
|
160
|
+
apiUrl: string,
|
|
161
|
+
userId: string,
|
|
162
|
+
documentId: string,
|
|
163
|
+
messages: ChatMessage[],
|
|
164
|
+
publication?: string
|
|
165
|
+
): Promise<void> {
|
|
166
|
+
try {
|
|
167
|
+
await fetch(`${apiUrl}/api/seo-copilot/chat-history`, {
|
|
168
|
+
method: "POST",
|
|
169
|
+
headers: { "Content-Type": "application/json" },
|
|
170
|
+
body: JSON.stringify({ userId, documentId, messages, publication }),
|
|
171
|
+
});
|
|
172
|
+
} catch {
|
|
173
|
+
// Silent - history save should never break chat
|
|
174
|
+
}
|
|
175
|
+
}
|
package/src/lib/types.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// SEO Copilot V2 - Plugin Types
|
|
2
|
+
|
|
3
|
+
export interface KeywordPlacement {
|
|
4
|
+
inBody: boolean;
|
|
5
|
+
inTitle: boolean;
|
|
6
|
+
inMetaDescription: boolean;
|
|
7
|
+
inImages: boolean;
|
|
8
|
+
inUrl: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface PrimaryKeywordCandidate {
|
|
12
|
+
term: string;
|
|
13
|
+
volume: number | null;
|
|
14
|
+
cpc: number | null;
|
|
15
|
+
competition: number | null;
|
|
16
|
+
confidence: number;
|
|
17
|
+
placement?: KeywordPlacement;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SEOAnalysis {
|
|
21
|
+
primaryKeywordCandidates: PrimaryKeywordCandidate[];
|
|
22
|
+
primaryKeyword: PrimaryKeywordCandidate; // legacy compat
|
|
23
|
+
supportingKeywords: {
|
|
24
|
+
term: string;
|
|
25
|
+
volume: number | null;
|
|
26
|
+
relevance: number;
|
|
27
|
+
inContent: boolean;
|
|
28
|
+
placement?: KeywordPlacement;
|
|
29
|
+
}[];
|
|
30
|
+
missingKeywords: {
|
|
31
|
+
term: string;
|
|
32
|
+
volume: number | null;
|
|
33
|
+
reason: string;
|
|
34
|
+
placement?: KeywordPlacement;
|
|
35
|
+
}[];
|
|
36
|
+
keywordDensity?: {
|
|
37
|
+
term: string;
|
|
38
|
+
count: number;
|
|
39
|
+
density: number;
|
|
40
|
+
}[];
|
|
41
|
+
suggestions?: string[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface AEOQuestion {
|
|
45
|
+
question: string;
|
|
46
|
+
volume: number;
|
|
47
|
+
cpc: number;
|
|
48
|
+
competition: number;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface AEORecommendations {
|
|
52
|
+
questions: AEOQuestion[];
|
|
53
|
+
questionHeadings: {
|
|
54
|
+
originalHeading?: string;
|
|
55
|
+
suggestedHeading: string;
|
|
56
|
+
rationale: string;
|
|
57
|
+
}[];
|
|
58
|
+
faqSuggestions: {
|
|
59
|
+
question: string;
|
|
60
|
+
answer: string;
|
|
61
|
+
volume?: number | null;
|
|
62
|
+
}[];
|
|
63
|
+
// Legacy fields for backward compat
|
|
64
|
+
faqItems?: {
|
|
65
|
+
question: string;
|
|
66
|
+
answer: string;
|
|
67
|
+
volume: number | null;
|
|
68
|
+
highlightedKeywords?: { term: string; volume: number }[];
|
|
69
|
+
}[];
|
|
70
|
+
highVolumeQuestions?: {
|
|
71
|
+
question: string;
|
|
72
|
+
answer: string;
|
|
73
|
+
volume: number | null;
|
|
74
|
+
highlightedKeywords?: { term: string; volume: number }[];
|
|
75
|
+
}[];
|
|
76
|
+
headingSuggestions?: {
|
|
77
|
+
heading: string;
|
|
78
|
+
answer: string;
|
|
79
|
+
rationale: string;
|
|
80
|
+
}[];
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export interface DeepAnalysis extends SEOAnalysis {
|
|
84
|
+
keywordDensity: {
|
|
85
|
+
term: string;
|
|
86
|
+
count: number;
|
|
87
|
+
density: number;
|
|
88
|
+
}[];
|
|
89
|
+
readabilityNotes: string[];
|
|
90
|
+
competitiveGaps: string[];
|
|
91
|
+
structureRecommendations: string[];
|
|
92
|
+
aeo?: AEORecommendations;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface ChatMessage {
|
|
96
|
+
role: "user" | "assistant";
|
|
97
|
+
content: string;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface DocumentFields {
|
|
101
|
+
title?: string;
|
|
102
|
+
metaDescription?: string;
|
|
103
|
+
slug?: string;
|
|
104
|
+
imageNames?: string[];
|
|
105
|
+
headings?: string[];
|
|
106
|
+
contentCategory?: string;
|
|
107
|
+
contentPlacement?: string[];
|
|
108
|
+
}
|