@behavioral-agentic-design-system/core 0.7.0 → 0.8.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/package.json +7 -2
- package/src/api/layers.ts +34 -6
- package/src/api/onboarding-extract.ts +435 -0
- package/src/api/onboarding.ts +82 -0
- package/src/app/shell.tsx +87 -25
- package/src/components/Foundations/Colors.stories.tsx +10 -1
- package/src/components/customize-tab.tsx +3 -0
- package/src/components/home-tab.tsx +1 -1
- package/src/components/layers-tab.tsx +172 -7
- package/src/components/onboarding-wizard.tsx +1421 -0
- package/src/components/ui/dialog.tsx +4 -1
- package/src/components/ui/dropdown-menu.tsx +4 -0
- package/src/components/ui/popover.tsx +6 -0
- package/src/components/ui/scroll-area.tsx +5 -0
- package/src/components/ui/select.tsx +4 -0
- package/src/components/ui/sheet.tsx +7 -0
- package/src/components/ui/slider.tsx +22 -7
- package/src/components/ui/tooltip.tsx +4 -0
- package/src/hooks/use-portal-theme-class.tsx +90 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@behavioral-agentic-design-system/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.1",
|
|
4
4
|
"description": "BADS core — tokens, shadcn/ui components, and the BADS template shell (app routes, customize tab, theme hooks). Forks consume this and re-export, keeping client repos to ~30 lines of glue.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Matthew Stephens <work@matthewstephens.com> (https://github.com/matthewlarn)",
|
|
@@ -59,6 +59,7 @@
|
|
|
59
59
|
"./components/bads-update-banner": "./src/components/bads-update-banner.tsx",
|
|
60
60
|
"./components/brand-mark": "./src/components/brand-mark.tsx",
|
|
61
61
|
"./components/dev-identity-badge": "./src/components/dev-identity-badge.tsx",
|
|
62
|
+
"./components/onboarding-wizard": "./src/components/onboarding-wizard.tsx",
|
|
62
63
|
"./components/customize/*": "./src/components/customize/*.tsx",
|
|
63
64
|
"./components/Foundations/*": "./src/components/Foundations/*.tsx",
|
|
64
65
|
"./app/page": "./src/app/page.tsx",
|
|
@@ -81,13 +82,17 @@
|
|
|
81
82
|
"./api/save-tokens": "./src/api/save-tokens.ts",
|
|
82
83
|
"./api/brand-colors": "./src/api/brand-colors.ts",
|
|
83
84
|
"./api/brand-fonts": "./src/api/brand-fonts.ts",
|
|
84
|
-
"./api/brand-mark": "./src/api/brand-mark.ts"
|
|
85
|
+
"./api/brand-mark": "./src/api/brand-mark.ts",
|
|
86
|
+
"./hooks/use-portal-theme-class": "./src/hooks/use-portal-theme-class.tsx",
|
|
87
|
+
"./api/onboarding": "./src/api/onboarding.ts",
|
|
88
|
+
"./api/onboarding-extract": "./src/api/onboarding-extract.ts"
|
|
85
89
|
},
|
|
86
90
|
"publishConfig": {
|
|
87
91
|
"access": "public"
|
|
88
92
|
},
|
|
89
93
|
"dependencies": {
|
|
90
94
|
"@xmldom/xmldom": "^0.9.10",
|
|
95
|
+
"cheerio": "^1.0.0",
|
|
91
96
|
"class-variance-authority": "^0.7.0",
|
|
92
97
|
"clsx": "^2.1.0",
|
|
93
98
|
"react-markdown": "^10.1.0",
|
package/src/api/layers.ts
CHANGED
|
@@ -18,6 +18,15 @@ function listLayers() {
|
|
|
18
18
|
function listFiles(dir: string): string[] {
|
|
19
19
|
const out: string[] = [];
|
|
20
20
|
for (const entry of fs.readdirSync(dir)) {
|
|
21
|
+
// Skip folders prefixed with `_` (e.g. `_template/`). They hold
|
|
22
|
+
// per-fork scaffolding — placeholder shapes the agent copies in
|
|
23
|
+
// during onboarding — not browse-worthy content. Surfacing them
|
|
24
|
+
// doubles up every research file in the sidebar (a stub at
|
|
25
|
+
// `research/<name>.md` next to a template at
|
|
26
|
+
// `research/_template/<name>.md`) and confuses users into thinking
|
|
27
|
+
// there are duplicates. This matches the same convention used by
|
|
28
|
+
// `scripts/check-agent-rules.mjs` for orphan detection.
|
|
29
|
+
if (entry.startsWith("_")) continue;
|
|
21
30
|
const full = path.join(dir, entry);
|
|
22
31
|
const stat = fs.statSync(full);
|
|
23
32
|
if (stat.isDirectory()) {
|
|
@@ -30,14 +39,33 @@ function listFiles(dir: string): string[] {
|
|
|
30
39
|
}
|
|
31
40
|
|
|
32
41
|
function sortLayerFiles(slug: string, files: string[]): string[] {
|
|
33
|
-
//
|
|
42
|
+
// Pin the layer-root README.md to the top of every layer. Without this,
|
|
43
|
+
// sub-discipline READMEs (e.g. layer-1-foundation/research/README.md) sort
|
|
44
|
+
// adjacent to the layer's own README and the obvious "start here" entry
|
|
45
|
+
// gets buried. Layer-1 keeps its intro/framework second-place preference.
|
|
46
|
+
const layerReadme = `${slug}/README.md`;
|
|
47
|
+
|
|
34
48
|
if (slug === "layer-1-foundation") {
|
|
35
|
-
const intro = files.find((f) => f.endsWith("intro.md"));
|
|
36
|
-
const framework = files.find((f) => f.endsWith("framework.md"));
|
|
37
|
-
const rest = files
|
|
38
|
-
|
|
49
|
+
const intro = files.find((f) => f.endsWith("/intro.md"));
|
|
50
|
+
const framework = files.find((f) => f.endsWith("/framework.md"));
|
|
51
|
+
const rest = files
|
|
52
|
+
.filter(
|
|
53
|
+
(f) =>
|
|
54
|
+
f !== layerReadme &&
|
|
55
|
+
!f.endsWith("/intro.md") &&
|
|
56
|
+
!f.endsWith("/framework.md"),
|
|
57
|
+
)
|
|
58
|
+
.sort();
|
|
59
|
+
return [
|
|
60
|
+
files.includes(layerReadme) ? layerReadme : undefined,
|
|
61
|
+
intro,
|
|
62
|
+
framework,
|
|
63
|
+
...rest,
|
|
64
|
+
].filter((f): f is string => f !== undefined);
|
|
39
65
|
}
|
|
40
|
-
|
|
66
|
+
|
|
67
|
+
const rest = files.filter((f) => f !== layerReadme).sort();
|
|
68
|
+
return files.includes(layerReadme) ? [layerReadme, ...rest] : rest;
|
|
41
69
|
}
|
|
42
70
|
|
|
43
71
|
function prettifyLayerName(slug: string) {
|
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
import { NextResponse } from "next/server";
|
|
2
|
+
import { load, type CheerioAPI } from "cheerio";
|
|
3
|
+
|
|
4
|
+
const FETCH_TIMEOUT_MS = 12_000;
|
|
5
|
+
const USER_AGENT =
|
|
6
|
+
"Mozilla/5.0 (compatible; BADS-Onboarding/1.0; +https://github.com/dazzag24/behavioral-agentic-design-system)";
|
|
7
|
+
|
|
8
|
+
export type ExtractedColor = {
|
|
9
|
+
value: string;
|
|
10
|
+
count: number;
|
|
11
|
+
source: "css" | "inline" | "meta";
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type ExtractedFont = {
|
|
15
|
+
family: string;
|
|
16
|
+
source: "google-fonts" | "css" | "inline";
|
|
17
|
+
url?: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type ExtractedLogo = {
|
|
21
|
+
kind: "svg" | "image";
|
|
22
|
+
url?: string;
|
|
23
|
+
svg?: string;
|
|
24
|
+
width?: number;
|
|
25
|
+
height?: number;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export type ExtractResult = {
|
|
29
|
+
url: string;
|
|
30
|
+
finalUrl: string;
|
|
31
|
+
title: string | null;
|
|
32
|
+
siteName: string | null;
|
|
33
|
+
description: string | null;
|
|
34
|
+
headline: string | null;
|
|
35
|
+
subhead: string | null;
|
|
36
|
+
ctas: string[];
|
|
37
|
+
colors: ExtractedColor[];
|
|
38
|
+
fonts: ExtractedFont[];
|
|
39
|
+
logo: ExtractedLogo | null;
|
|
40
|
+
themeColor: string | null;
|
|
41
|
+
warnings: string[];
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function normalizeUrl(input: string): string | null {
|
|
45
|
+
const trimmed = input.trim();
|
|
46
|
+
if (!trimmed) return null;
|
|
47
|
+
const withProtocol = /^https?:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`;
|
|
48
|
+
try {
|
|
49
|
+
const u = new URL(withProtocol);
|
|
50
|
+
if (u.protocol !== "https:" && u.protocol !== "http:") return null;
|
|
51
|
+
return u.toString();
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function fetchHtml(url: string): Promise<{ html: string; finalUrl: string }> {
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
60
|
+
try {
|
|
61
|
+
const res = await fetch(url, {
|
|
62
|
+
headers: {
|
|
63
|
+
"User-Agent": USER_AGENT,
|
|
64
|
+
Accept: "text/html,application/xhtml+xml",
|
|
65
|
+
"Accept-Language": "en-US,en;q=0.9",
|
|
66
|
+
},
|
|
67
|
+
redirect: "follow",
|
|
68
|
+
signal: controller.signal,
|
|
69
|
+
});
|
|
70
|
+
if (!res.ok) {
|
|
71
|
+
throw new Error(`Fetch failed: ${res.status} ${res.statusText}`);
|
|
72
|
+
}
|
|
73
|
+
const html = await res.text();
|
|
74
|
+
return { html, finalUrl: res.url || url };
|
|
75
|
+
} finally {
|
|
76
|
+
clearTimeout(timer);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const MAX_STYLESHEETS = 6;
|
|
81
|
+
const MAX_STYLESHEET_BYTES = 800_000;
|
|
82
|
+
|
|
83
|
+
async function fetchStylesheet(url: string): Promise<string | null> {
|
|
84
|
+
try {
|
|
85
|
+
const controller = new AbortController();
|
|
86
|
+
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
87
|
+
const res = await fetch(url, {
|
|
88
|
+
headers: { "User-Agent": USER_AGENT, Accept: "text/css,*/*;q=0.1" },
|
|
89
|
+
signal: controller.signal,
|
|
90
|
+
});
|
|
91
|
+
clearTimeout(timer);
|
|
92
|
+
if (!res.ok) return null;
|
|
93
|
+
const ct = res.headers.get("content-type") ?? "";
|
|
94
|
+
// Many CDNs serve CSS with text/plain or no header; trust the fetch worked.
|
|
95
|
+
if (ct && !ct.includes("css") && !ct.includes("text")) return null;
|
|
96
|
+
const text = await res.text();
|
|
97
|
+
if (text.length > MAX_STYLESHEET_BYTES) return text.slice(0, MAX_STYLESHEET_BYTES);
|
|
98
|
+
return text;
|
|
99
|
+
} catch {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async function collectExternalCss($: CheerioAPI, baseUrl: string): Promise<string> {
|
|
105
|
+
const urls: string[] = [];
|
|
106
|
+
$('link[rel="stylesheet"][href], link[rel*="stylesheet"][href]').each((_, el) => {
|
|
107
|
+
const href = $(el).attr("href");
|
|
108
|
+
if (!href) return;
|
|
109
|
+
try {
|
|
110
|
+
urls.push(new URL(href, baseUrl).toString());
|
|
111
|
+
} catch {}
|
|
112
|
+
});
|
|
113
|
+
if (!urls.length) return "";
|
|
114
|
+
const limited = urls.slice(0, MAX_STYLESHEETS);
|
|
115
|
+
const results = await Promise.all(limited.map((u) => fetchStylesheet(u)));
|
|
116
|
+
return results.filter((r): r is string => Boolean(r)).join("\n");
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
const COLOR_RE =
|
|
120
|
+
/#[0-9a-f]{3,8}\b|rgba?\([^)]+\)|hsla?\([^)]+\)|oklch\([^)]+\)|oklab\([^)]+\)/gi;
|
|
121
|
+
|
|
122
|
+
const NEUTRAL_GRAYS = new Set([
|
|
123
|
+
"#000",
|
|
124
|
+
"#000000",
|
|
125
|
+
"#fff",
|
|
126
|
+
"#ffffff",
|
|
127
|
+
"#fafafa",
|
|
128
|
+
"#f5f5f5",
|
|
129
|
+
"#eaeaea",
|
|
130
|
+
"#e5e5e5",
|
|
131
|
+
"#d4d4d4",
|
|
132
|
+
"#a3a3a3",
|
|
133
|
+
"#737373",
|
|
134
|
+
"#525252",
|
|
135
|
+
"#404040",
|
|
136
|
+
"#262626",
|
|
137
|
+
"#171717",
|
|
138
|
+
"#0a0a0a",
|
|
139
|
+
]);
|
|
140
|
+
|
|
141
|
+
function normalizeColor(value: string): string {
|
|
142
|
+
const v = value.trim().toLowerCase();
|
|
143
|
+
// Expand short hex (#abc → #aabbcc) for consistent counting
|
|
144
|
+
if (/^#[0-9a-f]{3}$/.test(v)) {
|
|
145
|
+
return `#${v[1]}${v[1]}${v[2]}${v[2]}${v[3]}${v[3]}`;
|
|
146
|
+
}
|
|
147
|
+
return v;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
function isInterestingColor(value: string): boolean {
|
|
151
|
+
const v = normalizeColor(value);
|
|
152
|
+
if (NEUTRAL_GRAYS.has(v)) return false;
|
|
153
|
+
// Pure transparent
|
|
154
|
+
if (/^rgba?\([^)]*,\s*0(\.0+)?\s*\)$/.test(v)) return false;
|
|
155
|
+
// Hex8 with alpha < 0x40 (shadow-tier transparency)
|
|
156
|
+
if (/^#[0-9a-f]{6}([0-3][0-9a-f])$/.test(v)) return false;
|
|
157
|
+
// Modern hex shorthand #abcd → 4-digit form with low alpha (#xxx0–#xxx5)
|
|
158
|
+
if (/^#[0-9a-f]{3}[0-5]$/.test(v)) return false;
|
|
159
|
+
// rgba/hsla with alpha < 0.3 — usually shadows or hairline borders
|
|
160
|
+
const alphaMatch = /^(rgba|hsla)\([^)]*,\s*(0?\.\d+|0)\s*\)$/i.exec(v);
|
|
161
|
+
if (alphaMatch) {
|
|
162
|
+
const a = parseFloat(alphaMatch[2]);
|
|
163
|
+
if (a < 0.3) return false;
|
|
164
|
+
}
|
|
165
|
+
return true;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function extractColors($: CheerioAPI, externalCss: string): ExtractedColor[] {
|
|
169
|
+
const counts = new Map<string, ExtractedColor>();
|
|
170
|
+
const bump = (raw: string, source: ExtractedColor["source"]) => {
|
|
171
|
+
if (!isInterestingColor(raw)) return;
|
|
172
|
+
const key = normalizeColor(raw);
|
|
173
|
+
const existing = counts.get(key);
|
|
174
|
+
if (existing) {
|
|
175
|
+
existing.count += 1;
|
|
176
|
+
} else {
|
|
177
|
+
counts.set(key, { value: key, count: 1, source });
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
$("style").each((_, el) => {
|
|
182
|
+
const css = $(el).text();
|
|
183
|
+
css.match(COLOR_RE)?.forEach((m) => bump(m, "css"));
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
if (externalCss) {
|
|
187
|
+
externalCss.match(COLOR_RE)?.forEach((m) => bump(m, "css"));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
$("[style]").each((_, el) => {
|
|
191
|
+
const style = $(el).attr("style") ?? "";
|
|
192
|
+
style.match(COLOR_RE)?.forEach((m) => bump(m, "inline"));
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
const themeColor = $('meta[name="theme-color"]').attr("content");
|
|
196
|
+
if (themeColor) bump(themeColor, "meta");
|
|
197
|
+
|
|
198
|
+
return Array.from(counts.values()).sort((a, b) => b.count - a.count);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const FONT_FAMILY_RE = /font-family\s*:\s*([^;}"]+)/gi;
|
|
202
|
+
const GOOGLE_FONTS_FAMILY_RE = /family=([^&"]+)/gi;
|
|
203
|
+
|
|
204
|
+
function cleanFontStack(stack: string): string {
|
|
205
|
+
return stack
|
|
206
|
+
.replace(/!important/i, "")
|
|
207
|
+
.split(",")[0]
|
|
208
|
+
.trim()
|
|
209
|
+
.replace(/^["']|["']$/g, "")
|
|
210
|
+
.replace(/\+/g, " ");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function extractFonts($: CheerioAPI, externalCss: string): ExtractedFont[] {
|
|
214
|
+
const seen = new Map<string, ExtractedFont>();
|
|
215
|
+
const add = (family: string, source: ExtractedFont["source"], url?: string) => {
|
|
216
|
+
const cleaned = cleanFontStack(family);
|
|
217
|
+
if (!cleaned || cleaned.length > 80) return;
|
|
218
|
+
if (/^(serif|sans-serif|monospace|system-ui|inherit|initial|unset|cursive|fantasy|ui-sans-serif|ui-serif|ui-monospace|ui-rounded)$/i.test(cleaned)) return;
|
|
219
|
+
// Skip CSS custom-property references — they're not real font names
|
|
220
|
+
if (/^var\(/.test(cleaned)) return;
|
|
221
|
+
// Skip vendor-prefixed system fonts (-apple-system, -webkit-system, etc.)
|
|
222
|
+
if (/^-[a-z]+-/.test(cleaned)) return;
|
|
223
|
+
const key = cleaned.toLowerCase();
|
|
224
|
+
if (!seen.has(key)) seen.set(key, { family: cleaned, source, url });
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// Google Fonts links
|
|
228
|
+
$('link[rel="stylesheet"][href*="fonts.googleapis.com"], link[href*="fonts.googleapis.com"]').each(
|
|
229
|
+
(_, el) => {
|
|
230
|
+
const href = $(el).attr("href") ?? "";
|
|
231
|
+
const families = href.matchAll(GOOGLE_FONTS_FAMILY_RE);
|
|
232
|
+
for (const match of families) {
|
|
233
|
+
const family = match[1].split(":")[0];
|
|
234
|
+
add(family, "google-fonts", href);
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
);
|
|
238
|
+
|
|
239
|
+
// CSS font-family declarations (inline <style>)
|
|
240
|
+
$("style").each((_, el) => {
|
|
241
|
+
const css = $(el).text();
|
|
242
|
+
const matches = css.matchAll(FONT_FAMILY_RE);
|
|
243
|
+
for (const match of matches) {
|
|
244
|
+
add(match[1], "css");
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// External stylesheets
|
|
249
|
+
if (externalCss) {
|
|
250
|
+
const matches = externalCss.matchAll(FONT_FAMILY_RE);
|
|
251
|
+
for (const match of matches) {
|
|
252
|
+
add(match[1], "css");
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// Inline font-family
|
|
257
|
+
$("[style*='font-family']").each((_, el) => {
|
|
258
|
+
const style = $(el).attr("style") ?? "";
|
|
259
|
+
const matches = style.matchAll(FONT_FAMILY_RE);
|
|
260
|
+
for (const match of matches) {
|
|
261
|
+
add(match[1], "inline");
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
return Array.from(seen.values());
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async function fetchSvg(url: string): Promise<string | null> {
|
|
269
|
+
try {
|
|
270
|
+
const controller = new AbortController();
|
|
271
|
+
const timer = setTimeout(() => controller.abort(), FETCH_TIMEOUT_MS);
|
|
272
|
+
const res = await fetch(url, {
|
|
273
|
+
headers: { "User-Agent": USER_AGENT },
|
|
274
|
+
signal: controller.signal,
|
|
275
|
+
});
|
|
276
|
+
clearTimeout(timer);
|
|
277
|
+
if (!res.ok) return null;
|
|
278
|
+
const ct = res.headers.get("content-type") ?? "";
|
|
279
|
+
if (!ct.includes("svg")) return null;
|
|
280
|
+
const text = await res.text();
|
|
281
|
+
if (text.length > 64 * 1024) return null;
|
|
282
|
+
return text;
|
|
283
|
+
} catch {
|
|
284
|
+
return null;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
async function extractLogo($: CheerioAPI, baseUrl: string): Promise<ExtractedLogo | null> {
|
|
289
|
+
// Inline header SVG — scan the first <header> for a small SVG that looks like a logo
|
|
290
|
+
const headerSvg = $("header svg, nav svg").first();
|
|
291
|
+
if (headerSvg.length) {
|
|
292
|
+
const html = $.html(headerSvg);
|
|
293
|
+
if (html && html.length < 16 * 1024) {
|
|
294
|
+
return { kind: "svg", svg: html };
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
// Header <img> — first <img> inside <header> or <nav>
|
|
299
|
+
const headerImg = $("header img, nav img").first();
|
|
300
|
+
if (headerImg.length) {
|
|
301
|
+
const src = headerImg.attr("src");
|
|
302
|
+
if (src) {
|
|
303
|
+
try {
|
|
304
|
+
const abs = new URL(src, baseUrl).toString();
|
|
305
|
+
if (abs.endsWith(".svg")) {
|
|
306
|
+
const svg = await fetchSvg(abs);
|
|
307
|
+
if (svg) return { kind: "svg", svg };
|
|
308
|
+
}
|
|
309
|
+
return { kind: "image", url: abs };
|
|
310
|
+
} catch {}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Apple touch icon (usually higher quality than favicon)
|
|
315
|
+
const apple = $('link[rel="apple-touch-icon"]').attr("href");
|
|
316
|
+
if (apple) {
|
|
317
|
+
try {
|
|
318
|
+
return { kind: "image", url: new URL(apple, baseUrl).toString() };
|
|
319
|
+
} catch {}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// og:image
|
|
323
|
+
const og = $('meta[property="og:image"]').attr("content");
|
|
324
|
+
if (og) {
|
|
325
|
+
try {
|
|
326
|
+
return { kind: "image", url: new URL(og, baseUrl).toString() };
|
|
327
|
+
} catch {}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// Favicon
|
|
331
|
+
const icon = $('link[rel~="icon"]').attr("href");
|
|
332
|
+
if (icon) {
|
|
333
|
+
try {
|
|
334
|
+
return { kind: "image", url: new URL(icon, baseUrl).toString() };
|
|
335
|
+
} catch {}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return null;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
function extractCopy($: CheerioAPI): {
|
|
342
|
+
title: string | null;
|
|
343
|
+
siteName: string | null;
|
|
344
|
+
description: string | null;
|
|
345
|
+
headline: string | null;
|
|
346
|
+
subhead: string | null;
|
|
347
|
+
ctas: string[];
|
|
348
|
+
} {
|
|
349
|
+
const title = $("title").first().text().trim() || null;
|
|
350
|
+
const siteName = $('meta[property="og:site_name"]').attr("content")?.trim() || null;
|
|
351
|
+
const description =
|
|
352
|
+
$('meta[name="description"]').attr("content")?.trim() ||
|
|
353
|
+
$('meta[property="og:description"]').attr("content")?.trim() ||
|
|
354
|
+
null;
|
|
355
|
+
const headline = $("h1").first().text().trim().replace(/\s+/g, " ") || null;
|
|
356
|
+
const subhead = $("h2").first().text().trim().replace(/\s+/g, " ") || null;
|
|
357
|
+
|
|
358
|
+
const ctas: string[] = [];
|
|
359
|
+
const ctaSeen = new Set<string>();
|
|
360
|
+
$("a[role='button'], button, a.btn, a[class*='button'], a[class*='cta']").each((_, el) => {
|
|
361
|
+
if (ctas.length >= 8) return;
|
|
362
|
+
const text = $(el).text().trim().replace(/\s+/g, " ");
|
|
363
|
+
if (text && text.length < 40 && !ctaSeen.has(text.toLowerCase())) {
|
|
364
|
+
ctaSeen.add(text.toLowerCase());
|
|
365
|
+
ctas.push(text);
|
|
366
|
+
}
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
return { title, siteName, description, headline, subhead, ctas };
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
export async function POST(request: Request) {
|
|
373
|
+
let payload: { url?: string };
|
|
374
|
+
try {
|
|
375
|
+
payload = (await request.json()) as { url?: string };
|
|
376
|
+
} catch {
|
|
377
|
+
return NextResponse.json({ ok: false, error: "Invalid JSON" }, { status: 400 });
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
const url = normalizeUrl(payload.url ?? "");
|
|
381
|
+
if (!url) {
|
|
382
|
+
return NextResponse.json(
|
|
383
|
+
{ ok: false, error: "Provide a valid http(s) URL." },
|
|
384
|
+
{ status: 400 },
|
|
385
|
+
);
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
let html: string;
|
|
389
|
+
let finalUrl: string;
|
|
390
|
+
try {
|
|
391
|
+
const fetched = await fetchHtml(url);
|
|
392
|
+
html = fetched.html;
|
|
393
|
+
finalUrl = fetched.finalUrl;
|
|
394
|
+
} catch (error) {
|
|
395
|
+
const message = error instanceof Error ? error.message : "Failed to fetch the URL";
|
|
396
|
+
return NextResponse.json({ ok: false, error: message }, { status: 502 });
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
const $ = load(html);
|
|
400
|
+
const warnings: string[] = [];
|
|
401
|
+
|
|
402
|
+
// Fetch external stylesheets in parallel with the logo lookup so we can
|
|
403
|
+
// extract colors/fonts even when the page doesn't inline its CSS.
|
|
404
|
+
const [externalCss, logo] = await Promise.all([
|
|
405
|
+
collectExternalCss($, finalUrl).catch(() => ""),
|
|
406
|
+
extractLogo($, finalUrl).catch(() => null),
|
|
407
|
+
]);
|
|
408
|
+
|
|
409
|
+
const copy = extractCopy($);
|
|
410
|
+
const colors = extractColors($, externalCss).slice(0, 12);
|
|
411
|
+
const fonts = extractFonts($, externalCss).slice(0, 8);
|
|
412
|
+
const themeColor = $('meta[name="theme-color"]').attr("content")?.trim() || null;
|
|
413
|
+
|
|
414
|
+
if (!colors.length) warnings.push("No colors detected — the site may use external stylesheets.");
|
|
415
|
+
if (!fonts.length) warnings.push("No fonts detected — the site may use external font hosts.");
|
|
416
|
+
if (!logo) warnings.push("No logo detected — fall back to manual upload.");
|
|
417
|
+
|
|
418
|
+
const result: ExtractResult = {
|
|
419
|
+
url,
|
|
420
|
+
finalUrl,
|
|
421
|
+
title: copy.title,
|
|
422
|
+
siteName: copy.siteName,
|
|
423
|
+
description: copy.description,
|
|
424
|
+
headline: copy.headline,
|
|
425
|
+
subhead: copy.subhead,
|
|
426
|
+
ctas: copy.ctas,
|
|
427
|
+
colors,
|
|
428
|
+
fonts,
|
|
429
|
+
logo,
|
|
430
|
+
themeColor,
|
|
431
|
+
warnings,
|
|
432
|
+
};
|
|
433
|
+
|
|
434
|
+
return NextResponse.json({ ok: true, result });
|
|
435
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import fs from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { NextResponse } from "next/server";
|
|
4
|
+
|
|
5
|
+
const ONBOARDING_PATH = path.join(process.cwd(), "onboarding-brief.json");
|
|
6
|
+
|
|
7
|
+
type SavePayload = {
|
|
8
|
+
draft?: unknown;
|
|
9
|
+
brief?: string;
|
|
10
|
+
agentPrompt?: string;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function isReasonableText(value: unknown) {
|
|
14
|
+
return typeof value === "string" && value.length <= 200_000;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function GET() {
|
|
18
|
+
if (process.env.NODE_ENV !== "development") {
|
|
19
|
+
return NextResponse.json({ ok: false, error: "dev-only" }, { status: 403 });
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const raw = await fs.readFile(ONBOARDING_PATH, "utf8");
|
|
24
|
+
return NextResponse.json({ ok: true, file: JSON.parse(raw) });
|
|
25
|
+
} catch (error) {
|
|
26
|
+
const code = (error as NodeJS.ErrnoException).code;
|
|
27
|
+
if (code === "ENOENT") {
|
|
28
|
+
return NextResponse.json({ ok: true, file: null });
|
|
29
|
+
}
|
|
30
|
+
return NextResponse.json(
|
|
31
|
+
{ ok: false, error: (error as Error).message },
|
|
32
|
+
{ status: 500 },
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function POST(request: Request) {
|
|
38
|
+
if (process.env.NODE_ENV !== "development") {
|
|
39
|
+
return NextResponse.json({ ok: false, error: "dev-only" }, { status: 403 });
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let payload: SavePayload;
|
|
43
|
+
try {
|
|
44
|
+
payload = (await request.json()) as SavePayload;
|
|
45
|
+
} catch {
|
|
46
|
+
return NextResponse.json({ ok: false, error: "Invalid JSON" }, { status: 400 });
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
if (!isReasonableText(payload.brief)) {
|
|
50
|
+
return NextResponse.json(
|
|
51
|
+
{ ok: false, error: "Missing or oversized onboarding brief" },
|
|
52
|
+
{ status: 400 },
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (payload.agentPrompt !== undefined && !isReasonableText(payload.agentPrompt)) {
|
|
57
|
+
return NextResponse.json(
|
|
58
|
+
{ ok: false, error: "Oversized agent prompt" },
|
|
59
|
+
{ status: 400 },
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const file = {
|
|
64
|
+
$comment:
|
|
65
|
+
"Generated by the BADS in-app onboarding wizard. Review before using as setup input.",
|
|
66
|
+
savedAt: new Date().toISOString(),
|
|
67
|
+
draft: payload.draft ?? null,
|
|
68
|
+
brief: payload.brief,
|
|
69
|
+
agentPrompt: payload.agentPrompt ?? "",
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
try {
|
|
73
|
+
await fs.writeFile(ONBOARDING_PATH, JSON.stringify(file, null, 2) + "\n", "utf8");
|
|
74
|
+
} catch (error) {
|
|
75
|
+
return NextResponse.json(
|
|
76
|
+
{ ok: false, error: (error as Error).message },
|
|
77
|
+
{ status: 500 },
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return NextResponse.json({ ok: true, path: "onboarding-brief.json" });
|
|
82
|
+
}
|