Use source images as article references
This commit is contained in:
+18
-6
@@ -1,7 +1,8 @@
|
||||
import { lookup } from "node:dns/promises";
|
||||
import { isIP } from "node:net";
|
||||
|
||||
const cache = new Map<string, { expiresAt: number; value: string }>();
|
||||
type WebsiteResearch = { prompt: string; imageUrl: string | null };
|
||||
const cache = new Map<string, { expiresAt: number; value: WebsiteResearch }>();
|
||||
const MAX_BYTES = 1_500_000;
|
||||
const MAX_TEXT = 6_000;
|
||||
const MAX_REDIRECTS = 3;
|
||||
@@ -38,11 +39,11 @@ function metaContent(html: string, name: string): string {
|
||||
return decodeEntities(tag?.match(/content=["']([^"']*)["']/i)?.[1] ?? "").trim();
|
||||
}
|
||||
|
||||
/** Reads a shop's public website into short plain text (title, description, visible copy). Empty string if it cannot be read. */
|
||||
export async function websitePrompt(website: string): Promise<string> {
|
||||
/** Reads public page text and its declared social-preview image with SSRF protection. */
|
||||
export async function websiteResearch(website: string): Promise<WebsiteResearch> {
|
||||
const cached = cache.get(website);
|
||||
if (cached && cached.expiresAt > Date.now()) return cached.value;
|
||||
let value = "";
|
||||
let value: WebsiteResearch = { prompt: "", imageUrl: null };
|
||||
try {
|
||||
let url = new URL(website);
|
||||
let response: Response | null = null;
|
||||
@@ -77,6 +78,12 @@ export async function websitePrompt(website: string): Promise<string> {
|
||||
const html = Buffer.concat(chunks).toString("utf8");
|
||||
const title = decodeEntities(html.match(/<title[^>]*>([\s\S]*?)<\/title>/i)?.[1] ?? "").replace(/\s+/g, " ").trim();
|
||||
const description = metaContent(html, "description") || metaContent(html, "og:description");
|
||||
const rawImage = metaContent(html, "og:image") || metaContent(html, "twitter:image");
|
||||
let imageUrl: string | null = null;
|
||||
try {
|
||||
const image = new URL(rawImage, url);
|
||||
if (image.protocol === "https:" || image.protocol === "http:") imageUrl = image.toString();
|
||||
} catch { /* A page image is optional. */ }
|
||||
const body = decodeEntities(
|
||||
html
|
||||
.replace(/<(script|style|noscript|svg|template|iframe)[\s\S]*?<\/\1>/gi, " ")
|
||||
@@ -90,12 +97,17 @@ export async function websitePrompt(website: string): Promise<string> {
|
||||
.join("\n")
|
||||
.slice(0, MAX_TEXT);
|
||||
if (title || description || body) {
|
||||
value = `<shop_website url="${website}">\nTitle: ${title}\nDescription: ${description}\n\n${body}\n</shop_website>`;
|
||||
value = { prompt: `<shop_website url="${website}">\nTitle: ${title}\nDescription: ${description}\n\n${body}\n</shop_website>`, imageUrl };
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
value = "";
|
||||
value = { prompt: "", imageUrl: null };
|
||||
}
|
||||
cache.set(website, { expiresAt: Date.now() + 60 * 60_000, value });
|
||||
return value;
|
||||
}
|
||||
|
||||
/** Backward-compatible text-only helper for shop profile research. */
|
||||
export async function websitePrompt(website: string): Promise<string> {
|
||||
return (await websiteResearch(website)).prompt;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user