import { getRecord } from "../goat"; import type { ExtractedContent } from "./types"; // Direct record extractors const EXTRACTORS: Record string | null> = { // Textpad "computer.aetheros.textpad.text": (r) => { const parts = [r.title, r.body].filter(Boolean); return parts.length > 0 ? parts.join("\n") : null; }, // Knowledge "computer.aetheros.knowledge.bank": (r) => { const parts = [r.name, r.description].filter(Boolean); return parts.length > 0 ? parts.join("\n") : null; }, "computer.aetheros.knowledge.page": (r) => { const parts = [r.title]; if (r.layout) { parts.push(extractKnowledgeLayoutText(r.layout)); } return parts.filter(Boolean).join("\n") || null; }, "computer.aetheros.knowledge.view": (r) => { return r.title || null; }, // Tasks "computer.aetheros.tasks.task": (r) => { const parts = [r.title, r.description].filter(Boolean); return parts.length > 0 ? parts.join("\n") : null; }, // Bluesky posts "app.bsky.feed.post": (r) => r.text || null, "app.bsky.actor.profile": (r) => { const parts = [r.displayName, r.description].filter(Boolean); return parts.length > 0 ? parts.join("\n") : null; }, // Skills "computer.aetheros.skill": (r) => { const parts = [r.name, r.description, r.content].filter(Boolean); return parts.length > 0 ? parts.join("\n") : null; }, // Conduit Editions "computer.aetheros.conduit.edition": (r) => { const parts = [r.name]; if (r.dataJson && r.dataType?.includes("json")) { try { const data = JSON.parse(r.dataJson); parts.push(extractStringsFromObject(data, 3)); } catch { /* skip malformed JSON */ } } return parts.filter(Boolean).join("\n") || null; }, // Drafts "computer.aetheros.drafts.draft": (r) => r.content || null, // Beacon (location) "computer.aetheros.beacon": (r) => { const parts = [r.title, r.description]; if (r.address) { parts.push( [r.address.city, r.address.region, r.address.country] .filter(Boolean) .join(", "), ); } return parts.filter(Boolean).join("\n") || null; }, // Standard.site documents "site.standard.document": (r) => { const parts = [r.title, r.description, r.textContent].filter(Boolean); return parts.length > 0 ? parts.join("\n") : null; }, "site.standard.publication": (r) => { const parts = [r.name, r.description].filter(Boolean); return parts.length > 0 ? parts.join("\n") : null; }, // People — profile resolved via RELATIONAL_EXTRACTORS // Docs / Spreadsheets — title only (cell data not meaningful for semantic search) "computer.aetheros.docs.spreadsheet": (r) => r.title || null, // Plyr tracks "fm.plyr.track": (r) => { const parts = [r.title, r.artist].filter(Boolean); return parts.length > 0 ? parts.join(" - ") : null; }, // Settings / system records — not embeddable "computer.aetheros.settings": () => null, "computer.aetheros.clipboard.item": () => null, "computer.aetheros.system.folder": () => null, "computer.aetheros.textpad.folder": () => null, "computer.aetheros.app.bsky.settings": () => null, }; // Relational extractors resolve subject records before embedding. const RELATIONAL_EXTRACTORS: Record< string, (record: any) => Promise > = { "app.bsky.feed.like": async (r) => { const subjectUri = r.subject?.uri; if (!subjectUri) return null; const subject = await resolveSubjectText(subjectUri); return subject ? `liked: ${subject}` : null; }, "app.bsky.feed.repost": async (r) => { const subjectUri = r.subject?.uri; if (!subjectUri) return null; const subject = await resolveSubjectText(subjectUri); return subject ? `reposted: ${subject}` : null; }, "app.bsky.graph.follow": async (r) => { const subjectDid = r.subject; if (!subjectDid) return null; const profileUri = `at://${subjectDid}/app.bsky.actor.profile/self`; try { const profile = await getRecord(profileUri); const value = profile?.value || profile; const parts = [value?.displayName, value?.description].filter(Boolean); return parts.length > 0 ? `follows: ${parts.join(" — ")}` : null; } catch { return null; } }, "computer.aetheros.people.contact": async (r) => { const parts: string[] = []; if (r.did) { try { const profileUri = `at://${r.did}/app.bsky.actor.profile/self`; const profile = await getRecord(profileUri); const value = profile?.value || profile; if (value?.displayName) parts.push(value.displayName); if (value?.description) parts.push(value.description); } catch { // Profile resolution failed — fall through to note } } if (r.note) parts.push(r.note); return parts.length > 0 ? parts.join("\n") : null; }, }; // Resolve a subject URI into text content. async function resolveSubjectText(uri: string): Promise { try { const record = await getRecord(uri); if (!record) return null; const value = record.value || record; // Prefer known extractors for subject records. const collection = uri.replace("at://", "").split("/")[1]; const extractor = EXTRACTORS[collection]; if (extractor) return extractor(value); // Fallback to generic string extraction. const text = extractStringsFromObject(value, 3); return text || null; } catch { return null; } } // Extract embeddable text from a record. export async function extractContent( collection: string, record: any, ): Promise { let text: string | null = null; // Relational records need async subject resolution. const relational = RELATIONAL_EXTRACTORS[collection]; if (relational) { text = await relational(record); } else { const extractor = EXTRACTORS[collection]; if (extractor) { text = extractor(record); } else { // Generic fallback text = extractStringsFromObject(record, 5); } } if (!text || text.trim().length < 10) return null; // Keep embedding input bounded. const truncated = text.length > 4000 ? text.slice(0, 4000) : text; return { text: truncated, title: record.title || record.name || undefined, collection, }; } // Extract text from Knowledge page layout blocks. function extractKnowledgeLayoutText(layout: any): string { const parts: string[] = []; function walkSections(sections: any[]) { if (!Array.isArray(sections)) return; for (const section of sections) { if (section.type === "rich-text" && Array.isArray(section.blocks)) { walkBlocks(section.blocks); } else if (section.type === "callout") { if (section.title) parts.push(section.title); if (section.body) parts.push(section.body); } else if (section.type === "toggle") { if (section.title) parts.push(section.title); if (Array.isArray(section.blocks)) walkBlocks(section.blocks); } } } function walkBlocks(blocks: any[]) { if (!Array.isArray(blocks)) return; for (const block of blocks) { if (Array.isArray(block.content)) { for (const item of block.content) { if (typeof item === "string") parts.push(item); else if (item?.text) parts.push(item.text); } } if (Array.isArray(block.children)) { walkBlocks(block.children); } } } // Layout can be flow or column-based. if (layout.type === "flow" && Array.isArray(layout.sections)) { for (const section of layout.sections) { walkSections(section.items || []); } } else if (layout.type === "columns" && Array.isArray(layout.columns)) { for (const col of layout.columns) { walkSections(col.items || []); } } return parts.join("\n"); } // Generic fallback for unknown record shapes. function extractStringsFromObject(obj: any, maxDepth: number): string { const parts: string[] = []; function walk(value: any, depth: number) { if (depth > maxDepth || parts.length > 50) return; if (typeof value === "string" && value.length > 3 && value.length < 2000) { // Skip IDs, dates, and other low-signal strings. if ( !value.startsWith("at://") && !value.startsWith("did:") && !value.match(/^\d{4}-\d{2}-\d{2}/) ) { parts.push(value); } } else if (Array.isArray(value)) { for (const item of value) walk(item, depth + 1); } else if (value && typeof value === "object") { for (const v of Object.values(value)) walk(v, depth + 1); } } walk(obj, 0); return parts.join("\n"); }