@aglyn/plugins-ai 1.6.0 → 1.8.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 +2 -2
- package/package.json +17 -17
- package/src/lib/components/ai-insight-dialog.component.js +14 -0
- package/src/lib/components/ai-insight-dialog.component.js.map +1 -1
- package/src/lib/insights/ai-figure-readers.d.ts +13 -0
- package/src/lib/insights/ai-figure-readers.js +158 -11
- package/src/lib/insights/ai-figure-readers.js.map +1 -1
- package/src/lib/insights/ai-figure-summaries.js +4 -2
- package/src/lib/insights/ai-figure-summaries.js.map +1 -1
- package/src/lib/insights/ai-insight-digest.d.ts +27 -17
- package/src/lib/insights/ai-insight-digest.js +96 -107
- package/src/lib/insights/ai-insight-digest.js.map +1 -1
- package/src/lib/jobs/ai-job-import-step.d.ts +28 -2
- package/src/lib/jobs/ai-job-import-step.js +170 -14
- package/src/lib/jobs/ai-job-import-step.js.map +1 -1
- package/src/lib/jobs/ai-job-insight-budget.d.ts +19 -3
- package/src/lib/jobs/ai-job-insight-budget.js +18 -4
- package/src/lib/jobs/ai-job-insight-budget.js.map +1 -1
- package/src/lib/jobs/ai-job-insight-step.d.ts +2 -2
- package/src/lib/jobs/ai-job-insight-step.js +17 -7
- package/src/lib/jobs/ai-job-insight-step.js.map +1 -1
- package/src/lib/jobs/ai-job-layout-language.js +2 -0
- package/src/lib/jobs/ai-job-layout-language.js.map +1 -1
- package/src/lib/jobs/ai-job-page-language.js +6 -2
- package/src/lib/jobs/ai-job-page-language.js.map +1 -1
- package/src/lib/jobs/ai-site-import-compile.d.ts +13 -3
- package/src/lib/jobs/ai-site-import-compile.js +336 -27
- package/src/lib/jobs/ai-site-import-compile.js.map +1 -1
- package/src/lib/jobs/ai-site-import-document.d.ts +3 -1
- package/src/lib/jobs/ai-site-import-document.js +36 -0
- package/src/lib/jobs/ai-site-import-document.js.map +1 -1
- package/src/lib/jobs/ai-site-import-look.d.ts +9 -0
- package/src/lib/jobs/ai-site-import-look.js +89 -6
- package/src/lib/jobs/ai-site-import-look.js.map +1 -1
- package/src/lib/layout-language/ai-layout-compiler.d.ts +16 -0
- package/src/lib/layout-language/ai-layout-compiler.js +351 -54
- package/src/lib/layout-language/ai-layout-compiler.js.map +1 -1
- package/src/lib/layout-language/ai-layout-frame-source.d.ts +88 -2
- package/src/lib/layout-language/ai-layout-frame-source.js +591 -30
- package/src/lib/layout-language/ai-layout-frame-source.js.map +1 -1
- package/src/lib/layout-language/ai-layout-frame.js +30 -18
- package/src/lib/layout-language/ai-layout-frame.js.map +1 -1
- package/src/lib/layout-language/ai-layout-language.d.ts +72 -3
- package/src/lib/layout-language/ai-layout-language.js +5 -3
- package/src/lib/layout-language/ai-layout-language.js.map +1 -1
- package/src/lib/layout-language/ai-layout-store.d.ts +8 -0
- package/src/lib/layout-language/ai-layout-store.js +32 -6
- package/src/lib/layout-language/ai-layout-store.js.map +1 -1
- package/src/lib/model/ai-insight.d.ts +23 -3
- package/src/lib/model/ai-insight.js +17 -3
- package/src/lib/model/ai-insight.js.map +1 -1
- package/src/lib/model/ai-site-import-fidelity.d.ts +31 -1
- package/src/lib/model/ai-site-import-fidelity.js +347 -25
- package/src/lib/model/ai-site-import-fidelity.js.map +1 -1
- package/src/lib/model/ai-site-import.d.ts +203 -6
- package/src/lib/model/ai-site-import.js +161 -5
- package/src/lib/model/ai-site-import.js.map +1 -1
- package/src/lib/model/ai-site-look.d.ts +8 -1
- package/src/lib/model/ai-site-look.js +8 -5
- package/src/lib/model/ai-site-look.js.map +1 -1
- package/src/lib/providers/routing.js +3 -1
- package/src/lib/providers/routing.js.map +1 -1
- package/src/lib/runtime/ai-doctrine-validators.d.ts +7 -1
- package/src/lib/runtime/ai-doctrine-validators.js +25 -2
- package/src/lib/runtime/ai-doctrine-validators.js.map +1 -1
- package/src/lib/runtime/ai-insight-check.d.ts +7 -0
- package/src/lib/runtime/ai-insight-check.js +111 -27
- package/src/lib/runtime/ai-insight-check.js.map +1 -1
- package/src/lib/runtime/ai-node-tree.js +19 -2
- package/src/lib/runtime/ai-node-tree.js.map +1 -1
- package/src/lib/runtime/ai-page-links.d.ts +15 -0
- package/src/lib/runtime/ai-page-links.js +48 -0
- package/src/lib/runtime/ai-page-links.js.map +1 -1
- package/src/lib/site-import/fixtures/site-import-fidelity-replay.js +30 -5
- package/src/lib/site-import/fixtures/site-import-fidelity-replay.js.map +1 -1
- package/src/lib/site-import/fixtures/site-layout-recordings.d.ts +9 -0
- package/src/lib/site-import/fixtures/site-layout-recordings.js +24 -5
- package/src/lib/site-import/fixtures/site-layout-recordings.js.map +1 -1
- package/src/lib/site-import/site-import-crawl.d.ts +3 -1
- package/src/lib/site-import/site-import-crawl.js +8 -3
- package/src/lib/site-import/site-import-crawl.js.map +1 -1
- package/src/lib/site-import/site-import-css.d.ts +20 -0
- package/src/lib/site-import/site-import-css.js +106 -0
- package/src/lib/site-import/site-import-css.js.map +1 -1
- package/src/lib/site-import/site-import-html.d.ts +39 -0
- package/src/lib/site-import/site-import-html.js +515 -5
- package/src/lib/site-import/site-import-html.js.map +1 -1
- package/src/lib/site-import/site-import-layout.d.ts +16 -6
- package/src/lib/site-import/site-import-layout.js +524 -52
- package/src/lib/site-import/site-import-layout.js.map +1 -1
- package/src/lib/site-import/site-import-nav.js +286 -24
- package/src/lib/site-import/site-import-nav.js.map +1 -1
- package/src/lib/site-import/site-import-page.js +3 -0
- package/src/lib/site-import/site-import-page.js.map +1 -1
- package/src/lib/tools/ai-insight-tool.js +45 -2
- package/src/lib/tools/ai-insight-tool.js.map +1 -1
|
@@ -140,6 +140,7 @@ const wordsIn = (text)=>{
|
|
|
140
140
|
* read's budget, and how the source lays them out (AGL-4106) over the blocks
|
|
141
141
|
* kept, its icons' drawings dropped first when it runs past its own budget.
|
|
142
142
|
*/ export function siteImportPageRead(page, at) {
|
|
143
|
+
var _page_videos;
|
|
143
144
|
const main = page.blocks.filter((block)=>block.region === 'main');
|
|
144
145
|
const capped = aiSiteImportCapBlocks(main, AI_SITE_IMPORT_READ_MAX_CHARS);
|
|
145
146
|
let layout = page.layout ? siteImportLayoutWithin(page.layout, capped.blocks.length) : null;
|
|
@@ -162,6 +163,8 @@ const wordsIn = (text)=>{
|
|
|
162
163
|
forms: page.forms.kept
|
|
163
164
|
}, layout ? {
|
|
164
165
|
layout
|
|
166
|
+
} : {}, ((_page_videos = page.videos) == null ? void 0 : _page_videos.length) ? {
|
|
167
|
+
videos: page.videos
|
|
165
168
|
} : {}, {
|
|
166
169
|
// The source's own title and description, for the page's listing (AGL-4107).
|
|
167
170
|
meta: {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../libs/plugins/ai/src/lib/site-import/site-import-page.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/*==========================================\n * ONE CRAWLED PAGE, AS AN IMPORT KEEPS IT (AGL-3976).\n *\n * The crawler's page (`SiteImportPage`) holds everything it extracted; an\n * import keeps much less, and keeps it in two shapes:\n *\n * - **Listed**, for the inventory: its address, title, how it was found and\n * how many words and photos it has. No text: an inventory of a whole site\n * rides the job document.\n * - **Read**, for a wave: its text in order, its sections from its headings,\n * and its photos' addresses. The text is kept as the page says it and is\n * fenced as untrusted data only where a prompt shows it; the section names\n * and the title travel outside the fence, so they are neutralized (no\n * fence marker, no hidden character) and cut short. Every block of its\n * main region rides along too (AGL-4083), so a page can be brought in word\n * for word.\n *\n * And from the home page, what the site's header and footer are made of\n * (AGL-4083): its brand as the source draws it (`siteImportHeaderBrand`) and\n * its own facts (`siteImportSiteFacts`), for the header and footer the import\n * builds.\n *=========================================*/\n\nimport { neutralizeUntrustedText } from '@aglyn/shared-util-tools/untrusted-text'\nimport {\n AI_SITE_IMPORT_BLOCKED_STATUSES,\n AI_SITE_IMPORT_IMAGES_PER_PAGE,\n AI_SITE_IMPORT_LABEL_MAX_CHARS,\n AI_SITE_IMPORT_MAX_SECTIONS,\n AI_SITE_IMPORT_PAGE_TEXT_MAX_CHARS,\n AI_SITE_IMPORT_READ_MAX_CHARS,\n aiSiteImportCapBlocks,\n AI_SITE_IMPORT_LAYOUT_MAX_CHARS,\n siteImportLayoutChars,\n siteImportLayoutWithin,\n siteImportLayoutWithoutDrawings,\n type AiSiteImportLeftOutReason,\n type AiSiteImportListedPage,\n type AiSiteImportPageRead,\n} from '../model/ai-site-import'\nimport type { SiteImportPage, SiteImportSkipReason } from './site-import-crawl'\nimport type { SiteImportBlock, SiteImportOrganization } from './site-import-html'\nimport { siteImportLogoCandidates, type SiteImportLogoCandidate } from './site-import-images'\n\n/** A line of untrusted text safe to show outside a fence: neutralized, one line, cut short. */\nexport function siteImportLabel(text: string, max = AI_SITE_IMPORT_LABEL_MAX_CHARS): string {\n const line = neutralizeUntrustedText(text).replace(/\\s+/g, ' ').trim()\n return line.length > max ? `${line.slice(0, max - 1).trimEnd()}…` : line\n}\n\nconst wordsIn = (text: string) => (text.match(/[\\p{L}\\p{N}]+/gu) ?? []).length\n\n/** How many words a page says, its title and headings included; a link inside a sentence is counted once. */\nexport function siteImportPageWords(page: Pick<SiteImportPage, 'title' | 'blocks'>): number {\n return page.blocks.reduce(\n (total, block) => total + (block.kind === 'image' || (block.kind === 'link' && block.inline) ? 0 : wordsIn(block.text)),\n 0,\n )\n}\n\n/** Between a page's own name and its site's in a browser title: \"About | Rosie's Bakery\". */\nconst TITLE_SEPARATOR = /\\s+[|\\u2013\\u2014\\u00b7:-]\\s+/\n\n/**\n * A page's name on the new site: \"Home\" for the home page, else its browser\n * title without the old site's name after a separator (\"About | Rosie's\n * Bakery\" is \"About\"), else its first heading.\n */\nexport function siteImportPageTitle(page: Pick<SiteImportPage, 'path' | 'title' | 'blocks'>): string {\n if ((page.path.replace(/\\/+$/, '') || '/') === '/') return 'Home'\n const own = siteImportLabel((page.title ?? '').split(TITLE_SEPARATOR)[0] ?? '')\n if (own.length >= 2) return own\n return siteImportLabel(page.blocks.find((block) => block.kind === 'heading')?.text ?? '')\n}\n\n/** The page as the inventory lists it. */\nexport function siteImportListedPage(page: SiteImportPage): AiSiteImportListedPage {\n return {\n url: page.url,\n path: page.path,\n title: siteImportPageTitle(page),\n foundBy: page.foundBy,\n words: siteImportPageWords(page),\n images: page.images.length,\n }\n}\n\n/** The page's photos, the ones in its body first, each once. */\nexport function siteImportPageImages(page: Pick<SiteImportPage, 'blocks' | 'images'>): Array<{ url: string; alt: string }> {\n const seen = new Set<string>()\n const images: Array<{ url: string; alt: string }> = []\n const add = (url: string, alt: string) => {\n if (!url || seen.has(url) || images.length >= AI_SITE_IMPORT_IMAGES_PER_PAGE) return\n seen.add(url)\n images.push({ url, alt: siteImportLabel(alt, 250) })\n }\n for (const block of page.blocks) if (block.kind === 'image' && block.region === 'main') add(block.url, block.alt)\n for (const image of page.images) add(image.url, image.alt)\n return images\n}\n\n/**\n * The page's sections, from its headings: the first is its opening (its\n * title or first heading), then one for each second-level heading of its\n * body, at most `AI_SITE_IMPORT_MAX_SECTIONS`. A page with no headings below\n * its title but more to say than an opening gets a second section for it.\n */\nexport function siteImportPageSections(page: Pick<SiteImportPage, 'title' | 'blocks'>): string[] {\n const main = page.blocks.filter((block) => block.region === 'main')\n const headings = main.filter((block): block is Extract<typeof block, { kind: 'heading' }> => block.kind === 'heading')\n const opening = siteImportLabel(headings.find((block) => block.level === 1)?.text || page.title || '') || 'Introduction'\n const sections = [opening]\n const seen = new Set([opening.toLowerCase()])\n for (const heading of headings) {\n if (heading.level !== 2) continue\n const name = siteImportLabel(heading.text)\n if (!name || seen.has(name.toLowerCase())) continue\n seen.add(name.toLowerCase())\n sections.push(name)\n if (sections.length >= AI_SITE_IMPORT_MAX_SECTIONS) break\n }\n if (sections.length === 1 && siteImportPageWords({ title: '', blocks: main }) > 80) sections.push('Details')\n return sections\n}\n\n/** The page's text in order, as a prompt shows it inside its fence. */\nexport function siteImportPageText(page: Pick<SiteImportPage, 'title' | 'description' | 'blocks'>): string {\n const lines: string[] = []\n if (page.title) lines.push(`Title: ${page.title}`)\n if (page.description) lines.push(`Description: ${page.description}`)\n for (const block of page.blocks) {\n const where = block.region === 'main' ? '' : `(${block.region}) `\n if (block.kind === 'heading') lines.push(`${'#'.repeat(block.level)} ${block.text}`)\n else if (block.kind === 'image') lines.push(`${where}[photo: ${block.alt || 'no description'}]`)\n else if (block.kind === 'list-item') lines.push(`${where}- ${block.text}`)\n else if (block.kind === 'quote') lines.push(`${where}> ${block.text}`)\n else if (block.kind === 'link') {\n // A link inside a sentence is in its block's words already.\n if (!block.inline) lines.push(`${where}${block.text}`)\n } else lines.push(`${where}${block.text}`)\n }\n const text = lines.join('\\n')\n return text.length > AI_SITE_IMPORT_PAGE_TEXT_MAX_CHARS ? text.slice(0, AI_SITE_IMPORT_PAGE_TEXT_MAX_CHARS) : text\n}\n\n/**\n * The page as a wave reads it: its main region's blocks whole, within the\n * read's budget, and how the source lays them out (AGL-4106) over the blocks\n * kept, its icons' drawings dropped first when it runs past its own budget.\n */\nexport function siteImportPageRead(page: SiteImportPage, at: string): AiSiteImportPageRead {\n const main = page.blocks.filter((block) => block.region === 'main')\n const capped = aiSiteImportCapBlocks(main, AI_SITE_IMPORT_READ_MAX_CHARS)\n let layout = page.layout ? siteImportLayoutWithin(page.layout, capped.blocks.length) : null\n if (layout && siteImportLayoutChars(layout) > AI_SITE_IMPORT_LAYOUT_MAX_CHARS) layout = siteImportLayoutWithoutDrawings(layout)\n if (layout && siteImportLayoutChars(layout) > AI_SITE_IMPORT_LAYOUT_MAX_CHARS) layout = null\n return {\n at,\n title: siteImportPageTitle(page),\n text: siteImportPageText(page),\n sections: siteImportPageSections(page),\n images: siteImportPageImages(page),\n blocks: capped.blocks,\n ...(capped.cut ? { cut: true } : {}),\n words: siteImportPageWords({ title: '', blocks: main }),\n forms: page.forms.kept,\n ...(layout ? { layout } : {}),\n // The source's own title and description, for the page's listing (AGL-4107).\n meta: { title: siteImportMetaText(page.title, SITE_IMPORT_META_TITLE_MAX_CHARS), description: siteImportMetaText(page.description, SITE_IMPORT_META_DESCRIPTION_MAX_CHARS) },\n }\n}\n\n/** The most characters of a source's title and description a read keeps: far past what any listing shows. */\nexport const SITE_IMPORT_META_TITLE_MAX_CHARS = 300\nexport const SITE_IMPORT_META_DESCRIPTION_MAX_CHARS = 1_000\n\n/** A title or a description as the source writes it, one line, neutralized; a longer one is kept to its ceiling. */\nfunction siteImportMetaText(text: string | null | undefined, max: number): string {\n return neutralizeUntrustedText(String(text ?? '')).replace(/\\s+/g, ' ').trim().slice(0, max)\n}\n\n/*------------------------------------------\n * The home page's header and footer (AGL-4083)\n *-----------------------------------------*/\n\n/**\n * How the source's header draws its brand: its logo alone (aglyn.com), its\n * logo with its name beside it, or its name alone. `text` is the name as the\n * header prints it, or the site's own name when it prints nothing at all.\n */\nexport interface SiteImportHeaderBrand {\n kind: 'logo-only' | 'logo-and-name' | 'name-only'\n logo: SiteImportLogoCandidate | null\n /** Whether the header prints the name as words. */\n name: boolean\n text: string\n}\n\n/** The site's own name: its head's, its structured data's, else its home page's title before a separator. */\nexport function siteImportSiteName(home: Pick<SiteImportPage, 'siteName' | 'organization' | 'title'>): string {\n return siteImportLabel(home.siteName || home.organization?.name || (home.title ?? '').split(TITLE_SEPARATOR).pop() || '')\n}\n\n/** Words short enough to be a name, not a sentence. */\nconst looksLikeName = (text: string) => text.length >= 2 && text.length <= AI_SITE_IMPORT_LABEL_MAX_CHARS && text.split(/\\s+/).length <= 8\n\n/**\n * The home page's header brand (AGL-4083), as the source shows it: the link\n * home in its header, the picture inside it, and whether words sit beside\n * the picture, inside the link or next to it. A header with no picture link\n * home is the name alone.\n */\nexport function siteImportHeaderBrand(\n home: Pick<SiteImportPage, 'brand' | 'images' | 'icons' | 'siteName' | 'organization' | 'title' | 'blocks'>,\n): SiteImportHeaderBrand {\n const logo = siteImportLogoCandidates(home).find((candidate) => candidate.source === 'header') ?? null\n const siteName = siteImportSiteName(home)\n const linked = siteImportLabel(home.brand?.text ?? '')\n const beside = home.blocks.find(\n (block) =>\n block.region === 'header' &&\n (block.kind === 'paragraph' || block.kind === 'heading' || block.kind === 'link') &&\n !!siteName &&\n block.text.trim().toLowerCase() === siteName.toLowerCase(),\n )\n const printed = looksLikeName(linked) ? linked : beside && beside.kind !== 'image' ? siteImportLabel(beside.text) : ''\n if (logo && printed) return { kind: 'logo-and-name', logo, name: true, text: printed }\n if (logo) return { kind: 'logo-only', logo, name: false, text: '' }\n return { kind: 'name-only', logo: null, name: true, text: printed || siteName }\n}\n\n/** The facts a site's header and footer carry, as the source writes them. */\nexport interface SiteImportSiteFacts {\n header: SiteImportBlock[]\n footer: SiteImportBlock[]\n organization: SiteImportOrganization | null\n /** Email addresses, from its structured data and its `mailto:` links. */\n emails: string[]\n /** Phone numbers as written, from its structured data and its `tel:` links. */\n phones: string[]\n /** Its profiles on other sites: its structured data's `sameAs` and its links to them. */\n social: string[]\n}\n\nconst SOCIAL_HOSTS =\n /(?:^|\\.)(?:twitter\\.com|x\\.com|facebook\\.com|instagram\\.com|linkedin\\.com|youtube\\.com|tiktok\\.com|github\\.com|pinterest\\.com|threads\\.net|yelp\\.com|tripadvisor\\.com|nextdoor\\.com|vimeo\\.com|bsky\\.app|mastodon\\.social)$/i\n\n/**\n * The home page's own facts (AGL-4083), word for word: its header and footer\n * blocks, its Organization structured data, and the email addresses, phone\n * numbers and profiles they hold. Nothing here is invented or reworded; a\n * fact the page does not state is not here.\n */\nexport function siteImportSiteFacts(home: Pick<SiteImportPage, 'blocks' | 'organization'>): SiteImportSiteFacts {\n const header = home.blocks.filter((block) => block.region === 'header')\n const footer = home.blocks.filter((block) => block.region === 'footer')\n const organization = home.organization ?? null\n const emails: string[] = []\n const phones: string[] = []\n const social: string[] = []\n const add = (list: string[], value: string) => {\n const clean = value.trim()\n if (clean && !list.some((entry) => entry.toLowerCase() === clean.toLowerCase())) list.push(clean)\n }\n if (organization?.email) add(emails, organization.email.toLowerCase())\n if (organization?.telephone) add(phones, organization.telephone)\n for (const url of organization?.sameAs ?? []) add(social, url)\n for (const block of [...header, ...footer]) {\n if (block.kind !== 'link') continue\n if (block.href.startsWith('mailto:')) add(emails, block.href.slice('mailto:'.length))\n else if (block.href.startsWith('tel:')) add(phones, block.text)\n else {\n try {\n if (SOCIAL_HOSTS.test(new URL(block.href).hostname)) add(social, block.href)\n } catch {\n // A link that is not an address is no profile.\n }\n }\n }\n return { header, footer, organization, emails, phones, social }\n}\n\n/** Why the crawler could not read a page, as an import leaves it out; `status` is the answer an `http-error` got. */\nexport function siteImportLeftOutReason(reason: SiteImportSkipReason, status?: number): AiSiteImportLeftOutReason {\n switch (reason) {\n case 'login-form':\n return 'login-form'\n case 'robots':\n return 'robots'\n case 'too-large':\n return 'too-large'\n case 'off-origin-redirect':\n return 'off-site'\n case 'http-error':\n return status !== undefined && AI_SITE_IMPORT_BLOCKED_STATUSES.has(status) ? 'blocked' : 'unreadable'\n case 'not-reached':\n return 'not-reached'\n case 'not-html':\n return 'file'\n default:\n return 'unreadable'\n }\n}\n"],"names":["neutralizeUntrustedText","AI_SITE_IMPORT_BLOCKED_STATUSES","AI_SITE_IMPORT_IMAGES_PER_PAGE","AI_SITE_IMPORT_LABEL_MAX_CHARS","AI_SITE_IMPORT_MAX_SECTIONS","AI_SITE_IMPORT_PAGE_TEXT_MAX_CHARS","AI_SITE_IMPORT_READ_MAX_CHARS","aiSiteImportCapBlocks","AI_SITE_IMPORT_LAYOUT_MAX_CHARS","siteImportLayoutChars","siteImportLayoutWithin","siteImportLayoutWithoutDrawings","siteImportLogoCandidates","siteImportLabel","text","max","line","replace","trim","length","slice","trimEnd","wordsIn","match","siteImportPageWords","page","blocks","reduce","total","block","kind","inline","TITLE_SEPARATOR","siteImportPageTitle","path","own","title","split","find","siteImportListedPage","url","foundBy","words","images","siteImportPageImages","seen","Set","add","alt","has","push","region","image","siteImportPageSections","headings","main","filter","opening","level","sections","toLowerCase","heading","name","siteImportPageText","lines","description","where","repeat","join","siteImportPageRead","at","capped","layout","cut","forms","kept","meta","siteImportMetaText","SITE_IMPORT_META_TITLE_MAX_CHARS","SITE_IMPORT_META_DESCRIPTION_MAX_CHARS","String","siteImportSiteName","home","siteName","organization","pop","looksLikeName","siteImportHeaderBrand","logo","candidate","source","linked","brand","beside","printed","SOCIAL_HOSTS","siteImportSiteFacts","header","footer","emails","phones","social","list","value","clean","some","entry","email","telephone","sameAs","href","startsWith","test","URL","hostname","siteImportLeftOutReason","reason","status","undefined"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;2CAqB2C,GAE3C,SAASA,uBAAuB,QAAQ,0CAAyC;AACjF,SACEC,+BAA+B,EAC/BC,8BAA8B,EAC9BC,8BAA8B,EAC9BC,2BAA2B,EAC3BC,kCAAkC,EAClCC,6BAA6B,EAC7BC,qBAAqB,EACrBC,+BAA+B,EAC/BC,qBAAqB,EACrBC,sBAAsB,EACtBC,+BAA+B,QAI1B,6BAAyB;AAGhC,SAASC,wBAAwB,QAAsC,0BAAsB;AAE7F,6FAA6F,GAC7F,OAAO,SAASC,gBAAgBC,IAAY,EAAEC,MAAMZ,8BAA8B;IAChF,MAAMa,OAAOhB,wBAAwBc,MAAMG,OAAO,CAAC,QAAQ,KAAKC,IAAI;IACpE,OAAOF,KAAKG,MAAM,GAAGJ,MAAM,GAAGC,KAAKI,KAAK,CAAC,GAAGL,MAAM,GAAGM,OAAO,GAAG,CAAC,CAAC,GAAGL;AACtE;AAEA,MAAMM,UAAU,CAACR;QAAkBA;WAAD,EAACA,cAAAA,KAAKS,KAAK,CAAC,ynaAAXT,cAAiC,EAAE,EAAEK,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YhE,IAAIoD,QAAQtE,MAAMiF,IAAI,CAAC1F,KAAK,CAAC,UAAUD,MAAM;aAC9E,IAAIU,MAAMiF,IAAI,CAACC,UAAU,CAAC,SAAShE,IAAIqD,QAAQvE,MAAMf,IAAI;aACzD;YACH,IAAI;gBACF,IAAIiF,aAAaiB,IAAI,CAAC,IAAIC,IAAIpF,MAAMiF,IAAI,EAAEI,QAAQ,GAAGnE,IAAIsD,QAAQxE,MAAMiF,IAAI;YAC7E,EAAE,eAAM;YACN,+CAA+C;YACjD;QACF;IACF;IACA,OAAO;QAAEb;QAAQC;QAAQd;QAAce;QAAQC;QAAQC;IAAO;AAChE;AAEA,mHAAmH,GACnH,OAAO,SAASc,wBAAwBC,MAA4B,EAAEC,MAAe;IACnF,OAAQD;QACN,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAOC,WAAWC,aAAarH,gCAAgCgD,GAAG,CAACoE,UAAU,YAAY;QAC3F,KAAK;YACH,OAAO;QACT,KAAK;YACH,OAAO;QACT;YACE,OAAO;IACX;AACF"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/plugins/ai/src/lib/site-import/site-import-page.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/*==========================================\n * ONE CRAWLED PAGE, AS AN IMPORT KEEPS IT (AGL-3976).\n *\n * The crawler's page (`SiteImportPage`) holds everything it extracted; an\n * import keeps much less, and keeps it in two shapes:\n *\n * - **Listed**, for the inventory: its address, title, how it was found and\n * how many words and photos it has. No text: an inventory of a whole site\n * rides the job document.\n * - **Read**, for a wave: its text in order, its sections from its headings,\n * and its photos' addresses. The text is kept as the page says it and is\n * fenced as untrusted data only where a prompt shows it; the section names\n * and the title travel outside the fence, so they are neutralized (no\n * fence marker, no hidden character) and cut short. Every block of its\n * main region rides along too (AGL-4083), so a page can be brought in word\n * for word.\n *\n * And from the home page, what the site's header and footer are made of\n * (AGL-4083): its brand as the source draws it (`siteImportHeaderBrand`) and\n * its own facts (`siteImportSiteFacts`), for the header and footer the import\n * builds.\n *=========================================*/\n\nimport { neutralizeUntrustedText } from '@aglyn/shared-util-tools/untrusted-text'\nimport {\n AI_SITE_IMPORT_BLOCKED_STATUSES,\n AI_SITE_IMPORT_IMAGES_PER_PAGE,\n AI_SITE_IMPORT_LABEL_MAX_CHARS,\n AI_SITE_IMPORT_MAX_SECTIONS,\n AI_SITE_IMPORT_PAGE_TEXT_MAX_CHARS,\n AI_SITE_IMPORT_READ_MAX_CHARS,\n aiSiteImportCapBlocks,\n AI_SITE_IMPORT_LAYOUT_MAX_CHARS,\n siteImportLayoutChars,\n siteImportLayoutWithin,\n siteImportLayoutWithoutDrawings,\n type AiSiteImportLeftOutReason,\n type AiSiteImportListedPage,\n type AiSiteImportPageRead,\n} from '../model/ai-site-import'\nimport type { SiteImportPage, SiteImportSkipReason } from './site-import-crawl'\nimport type { SiteImportBlock, SiteImportOrganization } from './site-import-html'\nimport { siteImportLogoCandidates, type SiteImportLogoCandidate } from './site-import-images'\n\n/** A line of untrusted text safe to show outside a fence: neutralized, one line, cut short. */\nexport function siteImportLabel(text: string, max = AI_SITE_IMPORT_LABEL_MAX_CHARS): string {\n const line = neutralizeUntrustedText(text).replace(/\\s+/g, ' ').trim()\n return line.length > max ? `${line.slice(0, max - 1).trimEnd()}…` : line\n}\n\nconst wordsIn = (text: string) => (text.match(/[\\p{L}\\p{N}]+/gu) ?? []).length\n\n/** How many words a page says, its title and headings included; a link inside a sentence is counted once. */\nexport function siteImportPageWords(page: Pick<SiteImportPage, 'title' | 'blocks'>): number {\n return page.blocks.reduce(\n (total, block) => total + (block.kind === 'image' || (block.kind === 'link' && block.inline) ? 0 : wordsIn(block.text)),\n 0,\n )\n}\n\n/** Between a page's own name and its site's in a browser title: \"About | Rosie's Bakery\". */\nconst TITLE_SEPARATOR = /\\s+[|\\u2013\\u2014\\u00b7:-]\\s+/\n\n/**\n * A page's name on the new site: \"Home\" for the home page, else its browser\n * title without the old site's name after a separator (\"About | Rosie's\n * Bakery\" is \"About\"), else its first heading.\n */\nexport function siteImportPageTitle(page: Pick<SiteImportPage, 'path' | 'title' | 'blocks'>): string {\n if ((page.path.replace(/\\/+$/, '') || '/') === '/') return 'Home'\n const own = siteImportLabel((page.title ?? '').split(TITLE_SEPARATOR)[0] ?? '')\n if (own.length >= 2) return own\n return siteImportLabel(page.blocks.find((block) => block.kind === 'heading')?.text ?? '')\n}\n\n/** The page as the inventory lists it. */\nexport function siteImportListedPage(page: SiteImportPage): AiSiteImportListedPage {\n return {\n url: page.url,\n path: page.path,\n title: siteImportPageTitle(page),\n foundBy: page.foundBy,\n words: siteImportPageWords(page),\n images: page.images.length,\n }\n}\n\n/** The page's photos, the ones in its body first, each once. */\nexport function siteImportPageImages(page: Pick<SiteImportPage, 'blocks' | 'images'>): Array<{ url: string; alt: string }> {\n const seen = new Set<string>()\n const images: Array<{ url: string; alt: string }> = []\n const add = (url: string, alt: string) => {\n if (!url || seen.has(url) || images.length >= AI_SITE_IMPORT_IMAGES_PER_PAGE) return\n seen.add(url)\n images.push({ url, alt: siteImportLabel(alt, 250) })\n }\n for (const block of page.blocks) if (block.kind === 'image' && block.region === 'main') add(block.url, block.alt)\n for (const image of page.images) add(image.url, image.alt)\n return images\n}\n\n/**\n * The page's sections, from its headings: the first is its opening (its\n * title or first heading), then one for each second-level heading of its\n * body, at most `AI_SITE_IMPORT_MAX_SECTIONS`. A page with no headings below\n * its title but more to say than an opening gets a second section for it.\n */\nexport function siteImportPageSections(page: Pick<SiteImportPage, 'title' | 'blocks'>): string[] {\n const main = page.blocks.filter((block) => block.region === 'main')\n const headings = main.filter((block): block is Extract<typeof block, { kind: 'heading' }> => block.kind === 'heading')\n const opening = siteImportLabel(headings.find((block) => block.level === 1)?.text || page.title || '') || 'Introduction'\n const sections = [opening]\n const seen = new Set([opening.toLowerCase()])\n for (const heading of headings) {\n if (heading.level !== 2) continue\n const name = siteImportLabel(heading.text)\n if (!name || seen.has(name.toLowerCase())) continue\n seen.add(name.toLowerCase())\n sections.push(name)\n if (sections.length >= AI_SITE_IMPORT_MAX_SECTIONS) break\n }\n if (sections.length === 1 && siteImportPageWords({ title: '', blocks: main }) > 80) sections.push('Details')\n return sections\n}\n\n/** The page's text in order, as a prompt shows it inside its fence. */\nexport function siteImportPageText(page: Pick<SiteImportPage, 'title' | 'description' | 'blocks'>): string {\n const lines: string[] = []\n if (page.title) lines.push(`Title: ${page.title}`)\n if (page.description) lines.push(`Description: ${page.description}`)\n for (const block of page.blocks) {\n const where = block.region === 'main' ? '' : `(${block.region}) `\n if (block.kind === 'heading') lines.push(`${'#'.repeat(block.level)} ${block.text}`)\n else if (block.kind === 'image') lines.push(`${where}[photo: ${block.alt || 'no description'}]`)\n else if (block.kind === 'list-item') lines.push(`${where}- ${block.text}`)\n else if (block.kind === 'quote') lines.push(`${where}> ${block.text}`)\n else if (block.kind === 'link') {\n // A link inside a sentence is in its block's words already.\n if (!block.inline) lines.push(`${where}${block.text}`)\n } else lines.push(`${where}${block.text}`)\n }\n const text = lines.join('\\n')\n return text.length > AI_SITE_IMPORT_PAGE_TEXT_MAX_CHARS ? text.slice(0, AI_SITE_IMPORT_PAGE_TEXT_MAX_CHARS) : text\n}\n\n/**\n * The page as a wave reads it: its main region's blocks whole, within the\n * read's budget, and how the source lays them out (AGL-4106) over the blocks\n * kept, its icons' drawings dropped first when it runs past its own budget.\n */\nexport function siteImportPageRead(page: SiteImportPage, at: string): AiSiteImportPageRead {\n const main = page.blocks.filter((block) => block.region === 'main')\n const capped = aiSiteImportCapBlocks(main, AI_SITE_IMPORT_READ_MAX_CHARS)\n let layout = page.layout ? siteImportLayoutWithin(page.layout, capped.blocks.length) : null\n if (layout && siteImportLayoutChars(layout) > AI_SITE_IMPORT_LAYOUT_MAX_CHARS) layout = siteImportLayoutWithoutDrawings(layout)\n if (layout && siteImportLayoutChars(layout) > AI_SITE_IMPORT_LAYOUT_MAX_CHARS) layout = null\n return {\n at,\n title: siteImportPageTitle(page),\n text: siteImportPageText(page),\n sections: siteImportPageSections(page),\n images: siteImportPageImages(page),\n blocks: capped.blocks,\n ...(capped.cut ? { cut: true } : {}),\n words: siteImportPageWords({ title: '', blocks: main }),\n forms: page.forms.kept,\n ...(layout ? { layout } : {}),\n // Its videos (AGL-4140), which its layout places by index.\n ...(page.videos?.length ? { videos: page.videos } : {}),\n // The source's own title and description, for the page's listing (AGL-4107).\n meta: { title: siteImportMetaText(page.title, SITE_IMPORT_META_TITLE_MAX_CHARS), description: siteImportMetaText(page.description, SITE_IMPORT_META_DESCRIPTION_MAX_CHARS) },\n }\n}\n\n/** The most characters of a source's title and description a read keeps: far past what any listing shows. */\nexport const SITE_IMPORT_META_TITLE_MAX_CHARS = 300\nexport const SITE_IMPORT_META_DESCRIPTION_MAX_CHARS = 1_000\n\n/** A title or a description as the source writes it, one line, neutralized; a longer one is kept to its ceiling. */\nfunction siteImportMetaText(text: string | null | undefined, max: number): string {\n return neutralizeUntrustedText(String(text ?? '')).replace(/\\s+/g, ' ').trim().slice(0, max)\n}\n\n/*------------------------------------------\n * The home page's header and footer (AGL-4083)\n *-----------------------------------------*/\n\n/**\n * How the source's header draws its brand: its logo alone (aglyn.com), its\n * logo with its name beside it, or its name alone. `text` is the name as the\n * header prints it, or the site's own name when it prints nothing at all.\n */\nexport interface SiteImportHeaderBrand {\n kind: 'logo-only' | 'logo-and-name' | 'name-only'\n logo: SiteImportLogoCandidate | null\n /** Whether the header prints the name as words. */\n name: boolean\n text: string\n}\n\n/** The site's own name: its head's, its structured data's, else its home page's title before a separator. */\nexport function siteImportSiteName(home: Pick<SiteImportPage, 'siteName' | 'organization' | 'title'>): string {\n return siteImportLabel(home.siteName || home.organization?.name || (home.title ?? '').split(TITLE_SEPARATOR).pop() || '')\n}\n\n/** Words short enough to be a name, not a sentence. */\nconst looksLikeName = (text: string) => text.length >= 2 && text.length <= AI_SITE_IMPORT_LABEL_MAX_CHARS && text.split(/\\s+/).length <= 8\n\n/**\n * The home page's header brand (AGL-4083), as the source shows it: the link\n * home in its header, the picture inside it, and whether words sit beside\n * the picture, inside the link or next to it. A header with no picture link\n * home is the name alone.\n */\nexport function siteImportHeaderBrand(\n home: Pick<SiteImportPage, 'brand' | 'images' | 'icons' | 'siteName' | 'organization' | 'title' | 'blocks'>,\n): SiteImportHeaderBrand {\n const logo = siteImportLogoCandidates(home).find((candidate) => candidate.source === 'header') ?? null\n const siteName = siteImportSiteName(home)\n const linked = siteImportLabel(home.brand?.text ?? '')\n const beside = home.blocks.find(\n (block) =>\n block.region === 'header' &&\n (block.kind === 'paragraph' || block.kind === 'heading' || block.kind === 'link') &&\n !!siteName &&\n block.text.trim().toLowerCase() === siteName.toLowerCase(),\n )\n const printed = looksLikeName(linked) ? linked : beside && beside.kind !== 'image' ? siteImportLabel(beside.text) : ''\n if (logo && printed) return { kind: 'logo-and-name', logo, name: true, text: printed }\n if (logo) return { kind: 'logo-only', logo, name: false, text: '' }\n return { kind: 'name-only', logo: null, name: true, text: printed || siteName }\n}\n\n/** The facts a site's header and footer carry, as the source writes them. */\nexport interface SiteImportSiteFacts {\n header: SiteImportBlock[]\n footer: SiteImportBlock[]\n organization: SiteImportOrganization | null\n /** Email addresses, from its structured data and its `mailto:` links. */\n emails: string[]\n /** Phone numbers as written, from its structured data and its `tel:` links. */\n phones: string[]\n /** Its profiles on other sites: its structured data's `sameAs` and its links to them. */\n social: string[]\n}\n\nconst SOCIAL_HOSTS =\n /(?:^|\\.)(?:twitter\\.com|x\\.com|facebook\\.com|instagram\\.com|linkedin\\.com|youtube\\.com|tiktok\\.com|github\\.com|pinterest\\.com|threads\\.net|yelp\\.com|tripadvisor\\.com|nextdoor\\.com|vimeo\\.com|bsky\\.app|mastodon\\.social)$/i\n\n/**\n * The home page's own facts (AGL-4083), word for word: its header and footer\n * blocks, its Organization structured data, and the email addresses, phone\n * numbers and profiles they hold. Nothing here is invented or reworded; a\n * fact the page does not state is not here.\n */\nexport function siteImportSiteFacts(home: Pick<SiteImportPage, 'blocks' | 'organization'>): SiteImportSiteFacts {\n const header = home.blocks.filter((block) => block.region === 'header')\n const footer = home.blocks.filter((block) => block.region === 'footer')\n const organization = home.organization ?? null\n const emails: string[] = []\n const phones: string[] = []\n const social: string[] = []\n const add = (list: string[], value: string) => {\n const clean = value.trim()\n if (clean && !list.some((entry) => entry.toLowerCase() === clean.toLowerCase())) list.push(clean)\n }\n if (organization?.email) add(emails, organization.email.toLowerCase())\n if (organization?.telephone) add(phones, organization.telephone)\n for (const url of organization?.sameAs ?? []) add(social, url)\n for (const block of [...header, ...footer]) {\n if (block.kind !== 'link') continue\n if (block.href.startsWith('mailto:')) add(emails, block.href.slice('mailto:'.length))\n else if (block.href.startsWith('tel:')) add(phones, block.text)\n else {\n try {\n if (SOCIAL_HOSTS.test(new URL(block.href).hostname)) add(social, block.href)\n } catch {\n // A link that is not an address is no profile.\n }\n }\n }\n return { header, footer, organization, emails, phones, social }\n}\n\n/** Why the crawler could not read a page, as an import leaves it out; `status` is the answer an `http-error` got. */\nexport function siteImportLeftOutReason(reason: SiteImportSkipReason, status?: number): AiSiteImportLeftOutReason {\n switch (reason) {\n case 'login-form':\n return 'login-form'\n case 'robots':\n return 'robots'\n case 'too-large':\n return 'too-large'\n case 'off-origin-redirect':\n return 'off-site'\n case 'http-error':\n return status !== undefined && AI_SITE_IMPORT_BLOCKED_STATUSES.has(status) ? 'blocked' : 'unreadable'\n case 'not-reached':\n return 'not-reached'\n case 'not-html':\n return 'file'\n default:\n return 'unreadable'\n }\n}\n"],"names":["neutralizeUntrustedText","AI_SITE_IMPORT_BLOCKED_STATUSES","AI_SITE_IMPORT_IMAGES_PER_PAGE","AI_SITE_IMPORT_LABEL_MAX_CHARS","AI_SITE_IMPORT_MAX_SECTIONS","AI_SITE_IMPORT_PAGE_TEXT_MAX_CHARS","AI_SITE_IMPORT_READ_MAX_CHARS","aiSiteImportCapBlocks","AI_SITE_IMPORT_LAYOUT_MAX_CHARS","siteImportLayoutChars","siteImportLayoutWithin","siteImportLayoutWithoutDrawings","siteImportLogoCandidates","siteImportLabel","text","max","line","replace","trim","length","slice","trimEnd","wordsIn","match","siteImportPageWords","page","blocks","reduce","total","block","kind","inline","TITLE_SEPARATOR","siteImportPageTitle","path","own","title","split","find","siteImportListedPage","url","foundBy","words","images","siteImportPageImages","seen","Set","add","alt","has","push","region","image","siteImportPageSections","headings","main","filter","opening","level","sections","toLowerCase","heading","name","siteImportPageText","lines","description","where","repeat","join","siteImportPageRead","at","capped","layout","cut","forms","kept","videos","meta","siteImportMetaText","SITE_IMPORT_META_TITLE_MAX_CHARS","SITE_IMPORT_META_DESCRIPTION_MAX_CHARS","String","siteImportSiteName","home","siteName","organization","pop","looksLikeName","siteImportHeaderBrand","logo","candidate","source","linked","brand","beside","printed","SOCIAL_HOSTS","siteImportSiteFacts","header","footer","emails","phones","social","list","value","clean","some","entry","email","telephone","sameAs","href","startsWith","test","URL","hostname","siteImportLeftOutReason","reason","status","undefined"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED;;;;;;;;;;;;;;;;;;;;;2CAqB2C,GAE3C,SAASA,uBAAuB,QAAQ,0CAAyC;AACjF,SACEC,+BAA+B,EAC/BC,8BAA8B,EAC9BC,8BAA8B,EAC9BC,2BAA2B,EAC3BC,kCAAkC,EAClCC,6BAA6B,EAC7BC,qBAAqB,EACrBC,+BAA+B,EAC/BC,qBAAqB,EACrBC,sBAAsB,EACtBC,+BAA+B,QAI1B,6BAAyB;AAGhC,SAASC,wBAAwB,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
@@ -14,7 +14,7 @@ import { _ as _extends } from "@swc/helpers/_/_extends";
|
|
|
14
14
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
15
|
* See the License for the specific language governing permissions and
|
|
16
16
|
* limitations under the License.
|
|
17
|
-
*/ import { AI_INSIGHT_GAP_MAX_CHARS, AI_INSIGHT_HEADLINE_MAX_CHARS, AI_INSIGHT_MAX_CHARS, AI_INSIGHT_MAX_CITED_ROWS, AI_INSIGHT_MAX_CITES, AI_INSIGHT_MAX_INSIGHTS, AI_INSIGHT_MAX_READS } from "../model/ai-insight.js";
|
|
17
|
+
*/ import { AI_INSIGHT_GAP_MAX_CHARS, AI_INSIGHT_HEADLINE_MAX_CHARS, AI_INSIGHT_NEXT_MAX_CHARS, AI_INSIGHT_OVERVIEW_MAX_CHARS, AI_INSIGHT_WHY_MAX_CHARS, AI_INSIGHT_MAX_CHARS, AI_INSIGHT_MAX_CITED_ROWS, AI_INSIGHT_MAX_CITES, AI_INSIGHT_MAX_INSIGHTS, AI_INSIGHT_MAX_READS } from "../model/ai-insight.js";
|
|
18
18
|
/**
|
|
19
19
|
* The two strict tools an insight job answers through (AGL-2915).
|
|
20
20
|
*
|
|
@@ -93,6 +93,17 @@ export function aiInsightAnswerTool() {
|
|
|
93
93
|
inputSchema: {
|
|
94
94
|
type: 'object',
|
|
95
95
|
properties: {
|
|
96
|
+
overview: {
|
|
97
|
+
anyOf: [
|
|
98
|
+
{
|
|
99
|
+
type: 'string'
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
type: 'null'
|
|
103
|
+
}
|
|
104
|
+
],
|
|
105
|
+
description: `A weekly digest only: what the week meant, in two or three sentences of your own; at most ${AI_INSIGHT_OVERVIEW_MAX_CHARS} characters. null on a question.`
|
|
106
|
+
},
|
|
96
107
|
insights: {
|
|
97
108
|
type: 'array',
|
|
98
109
|
description: `Most useful first; at most ${AI_INSIGHT_MAX_INSIGHTS}.`,
|
|
@@ -107,6 +118,28 @@ export function aiInsightAnswerTool() {
|
|
|
107
118
|
type: 'string',
|
|
108
119
|
description: `Why it matters, in one or two plain sentences that do not repeat the headline; at most ${AI_INSIGHT_MAX_CHARS} characters.`
|
|
109
120
|
},
|
|
121
|
+
why: {
|
|
122
|
+
anyOf: [
|
|
123
|
+
{
|
|
124
|
+
type: 'string'
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
type: 'null'
|
|
128
|
+
}
|
|
129
|
+
],
|
|
130
|
+
description: `What most likely drove it, resting on a row you cite from the changes, sources or campaigns table; at most ${AI_INSIGHT_WHY_MAX_CHARS} characters. null when nothing there explains it.`
|
|
131
|
+
},
|
|
132
|
+
next: {
|
|
133
|
+
anyOf: [
|
|
134
|
+
{
|
|
135
|
+
type: 'string'
|
|
136
|
+
},
|
|
137
|
+
{
|
|
138
|
+
type: 'null'
|
|
139
|
+
}
|
|
140
|
+
],
|
|
141
|
+
description: `One concrete step the owner can take; at most ${AI_INSIGHT_NEXT_MAX_CHARS} characters. null when none fits.`
|
|
142
|
+
},
|
|
110
143
|
cites: {
|
|
111
144
|
type: 'array',
|
|
112
145
|
description: `The tables and rows its figures come from; at most ${AI_INSIGHT_MAX_CITES} tables.`,
|
|
@@ -136,6 +169,8 @@ export function aiInsightAnswerTool() {
|
|
|
136
169
|
required: [
|
|
137
170
|
'headline',
|
|
138
171
|
'text',
|
|
172
|
+
'why',
|
|
173
|
+
'next',
|
|
139
174
|
'cites'
|
|
140
175
|
],
|
|
141
176
|
additionalProperties: false
|
|
@@ -154,6 +189,7 @@ export function aiInsightAnswerTool() {
|
|
|
154
189
|
}
|
|
155
190
|
},
|
|
156
191
|
required: [
|
|
192
|
+
'overview',
|
|
157
193
|
'insights',
|
|
158
194
|
'gap'
|
|
159
195
|
],
|
|
@@ -192,14 +228,21 @@ const isRecord = (value)=>typeof value === 'object' && value !== null && !Array.
|
|
|
192
228
|
const insights = input['insights'].filter(isRecord).map((raw)=>_extends({}, typeof raw['headline'] === 'string' ? {
|
|
193
229
|
headline: raw['headline']
|
|
194
230
|
} : {}, {
|
|
195
|
-
text: typeof raw['text'] === 'string' ? raw['text'] : ''
|
|
231
|
+
text: typeof raw['text'] === 'string' ? raw['text'] : ''
|
|
232
|
+
}, typeof raw['why'] === 'string' ? {
|
|
233
|
+
why: raw['why']
|
|
234
|
+
} : {}, typeof raw['next'] === 'string' ? {
|
|
235
|
+
next: raw['next']
|
|
236
|
+
} : {}, {
|
|
196
237
|
cites: (Array.isArray(raw['cites']) ? raw['cites'] : []).filter(isRecord).map((cite)=>({
|
|
197
238
|
table: typeof cite['table'] === 'string' ? cite['table'] : '',
|
|
198
239
|
rows: Array.isArray(cite['rows']) ? cite['rows'] : []
|
|
199
240
|
}))
|
|
200
241
|
}));
|
|
201
242
|
const gap = typeof input['gap'] === 'string' ? input['gap'] : null;
|
|
243
|
+
const overview = typeof input['overview'] === 'string' ? input['overview'] : null;
|
|
202
244
|
return {
|
|
245
|
+
overview,
|
|
203
246
|
insights,
|
|
204
247
|
gap
|
|
205
248
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../../../../libs/plugins/ai/src/lib/tools/ai-insight-tool.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AI_INSIGHT_GAP_MAX_CHARS,\n AI_INSIGHT_HEADLINE_MAX_CHARS,\n AI_INSIGHT_MAX_CHARS,\n AI_INSIGHT_MAX_CITED_ROWS,\n AI_INSIGHT_MAX_CITES,\n AI_INSIGHT_MAX_INSIGHTS,\n AI_INSIGHT_MAX_READS,\n type AiInsightAnswer,\n} from '../model/ai-insight'\nimport type { AiTool } from '../providers/contract'\n\n/**\n * The two strict tools an insight job answers through (AGL-2915).\n *\n * `read_figures` names readers — an id, a window and the reader's own\n * parameters — and nothing else: the model never writes a query, a field\n * path or a filter of its own, only a choice among the readers the request\n * lists. `submit_insights` is the answer, each insight citing the table rows\n * its figures come from. Lengths and counts are stated in the descriptions,\n * as the platform's other strict tools state them; the step holds every one.\n */\n\nexport const AI_INSIGHT_READ_TOOL_NAME = 'read_figures'\nexport const AI_INSIGHT_ANSWER_TOOL_NAME = 'submit_insights'\n\nexport function aiInsightReadTool(): AiTool {\n return {\n name: AI_INSIGHT_READ_TOOL_NAME,\n description:\n `Choose the readers whose tables answer the question: at most ${AI_INSIGHT_MAX_READS}, ` +\n 'each from the list the request gives. Every field is required.',\n strict: true,\n inputSchema: {\n type: 'object',\n properties: {\n reads: {\n type: 'array',\n description: `The readers to read, most useful first; at most ${AI_INSIGHT_MAX_READS}.`,\n items: {\n type: 'object',\n properties: {\n reader: { type: 'string', description: 'A reader id from the list, exactly as written.' },\n days: {\n type: 'integer',\n description: 'One of the windows that reader lists, in days; 0 for a reader that lists none.',\n },\n params: {\n type: 'array',\n description: 'The parameters that reader lists, by name; empty when it lists none.',\n items: {\n type: 'object',\n properties: {\n name: { type: 'string' },\n value: { type: 'string' },\n },\n required: ['name', 'value'],\n additionalProperties: false,\n },\n },\n },\n required: ['reader', 'days', 'params'],\n additionalProperties: false,\n },\n },\n },\n required: ['reads'],\n additionalProperties: false,\n },\n }\n}\n\nexport function aiInsightAnswerTool(): AiTool {\n return {\n name: AI_INSIGHT_ANSWER_TOOL_NAME,\n description:\n `Answer with at most ${AI_INSIGHT_MAX_INSIGHTS} insights, each citing the table rows its figures come from. ` +\n 'Every field is required.',\n strict: true,\n inputSchema: {\n type: 'object',\n properties: {\n insights: {\n type: 'array',\n description: `Most useful first; at most ${AI_INSIGHT_MAX_INSIGHTS}.`,\n items: {\n type: 'object',\n properties: {\n headline: {\n type: 'string',\n description: `What happened, in a few words, with no full stop; at most ${AI_INSIGHT_HEADLINE_MAX_CHARS} characters.`,\n },\n text: {\n type: 'string',\n description: `Why it matters, in one or two plain sentences that do not repeat the headline; at most ${AI_INSIGHT_MAX_CHARS} characters.`,\n },\n cites: {\n type: 'array',\n description: `The tables and rows its figures come from; at most ${AI_INSIGHT_MAX_CITES} tables.`,\n items: {\n type: 'object',\n properties: {\n table: { type: 'string', description: 'The table handle: t1, t2, …' },\n rows: {\n type: 'array',\n description: `Row numbers as the table numbers them, from 0; at most ${AI_INSIGHT_MAX_CITED_ROWS}.`,\n items: { type: 'integer' },\n },\n },\n required: ['table', 'rows'],\n additionalProperties: false,\n },\n },\n },\n required: ['headline', 'text', 'cites'],\n additionalProperties: false,\n },\n },\n gap: {\n anyOf: [{ type: 'string' }, { type: 'null' }],\n description: `What the tables cannot answer, in one sentence without numbers; at most ${AI_INSIGHT_GAP_MAX_CHARS} characters. null when nothing.`,\n },\n },\n required: ['insights', 'gap'],\n additionalProperties: false,\n },\n }\n}\n\n/** One reader the model asked for, as it asked. */\nexport interface AiInsightReadRequest {\n reader: string\n days: number\n params: Record<string, string>\n}\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value)\n\n/** The reads a `read_figures` call names, shape-checked and bounded; the step decides which are real. */\nexport function parseAiInsightReads(input: unknown): AiInsightReadRequest[] {\n const reads = isRecord(input) && Array.isArray(input['reads']) ? input['reads'] : []\n const parsed: AiInsightReadRequest[] = []\n for (const raw of reads) {\n if (parsed.length === AI_INSIGHT_MAX_READS) break\n if (!isRecord(raw) || typeof raw['reader'] !== 'string') continue\n const params: Record<string, string> = {}\n for (const pair of Array.isArray(raw['params']) ? raw['params'] : []) {\n if (isRecord(pair) && typeof pair['name'] === 'string' && typeof pair['value'] === 'string') {\n params[pair['name']] = pair['value']\n }\n }\n const days = Number(raw['days'])\n parsed.push({\n reader: raw['reader'].trim(),\n days: Number.isInteger(days) && days >= 0 ? days : 0,\n params,\n })\n }\n return parsed\n}\n\n/**\n * A `submit_insights` call as the trace reads it, or `null` when the call is\n * not an answer at all. Values keep the types the model wrote, so the trace\n * — not this reader — decides what an unreadable citation costs.\n */\nexport function parseAiInsightAnswer(input: unknown): AiInsightAnswer | null {\n if (!isRecord(input) || !Array.isArray(input['insights'])) return null\n const insights = input['insights']\n .filter(isRecord)\n .map((raw) => ({\n ...(typeof raw['headline'] === 'string' ? { headline: raw['headline'] } : {}),\n text: typeof raw['text'] === 'string' ? raw['text'] : '',\n cites: (Array.isArray(raw['cites']) ? raw['cites'] : []).filter(isRecord).map((cite) => ({\n table: typeof cite['table'] === 'string' ? cite['table'] : '',\n rows: (Array.isArray(cite['rows']) ? cite['rows'] : []) as number[],\n })),\n }))\n const gap = typeof input['gap'] === 'string' ? input['gap'] : null\n return { insights, gap }\n}\n"],"names":["AI_INSIGHT_GAP_MAX_CHARS","AI_INSIGHT_HEADLINE_MAX_CHARS","AI_INSIGHT_MAX_CHARS","AI_INSIGHT_MAX_CITED_ROWS","AI_INSIGHT_MAX_CITES","AI_INSIGHT_MAX_INSIGHTS","AI_INSIGHT_MAX_READS","AI_INSIGHT_READ_TOOL_NAME","AI_INSIGHT_ANSWER_TOOL_NAME","aiInsightReadTool","name","description","strict","inputSchema","type","properties","reads","items","reader","days","params","value","required","additionalProperties","aiInsightAnswerTool","insights","headline","text","cites","table","rows","gap","anyOf","isRecord","Array","isArray","parseAiInsightReads","input","parsed","raw","length","pair","Number","push","trim","isInteger","parseAiInsightAnswer","filter","map","cite"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SACEA,wBAAwB,EACxBC,6BAA6B,EAC7BC,oBAAoB,EACpBC,yBAAyB,EACzBC,oBAAoB,EACpBC,uBAAuB,EACvBC,oBAAoB,QAEf,yBAAqB;AAG5B;;;;;;;;;CASC,GAED,OAAO,MAAMC,4BAA4B,eAAc;AACvD,OAAO,MAAMC,8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A,KAAK,CAAC,MAAM,GAAG;IAC9D,OAAO;QAAEZ;QAAUM;IAAI;AACzB"}
|
|
1
|
+
{"version":3,"sources":["../../../../../../../libs/plugins/ai/src/lib/tools/ai-insight-tool.ts"],"sourcesContent":["/**\n * @license\n * Copyright 2026 Aglyn LLC\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n AI_INSIGHT_GAP_MAX_CHARS,\n AI_INSIGHT_HEADLINE_MAX_CHARS,\n AI_INSIGHT_NEXT_MAX_CHARS,\n AI_INSIGHT_OVERVIEW_MAX_CHARS,\n AI_INSIGHT_WHY_MAX_CHARS,\n AI_INSIGHT_MAX_CHARS,\n AI_INSIGHT_MAX_CITED_ROWS,\n AI_INSIGHT_MAX_CITES,\n AI_INSIGHT_MAX_INSIGHTS,\n AI_INSIGHT_MAX_READS,\n type AiInsightAnswer,\n} from '../model/ai-insight'\nimport type { AiTool } from '../providers/contract'\n\n/**\n * The two strict tools an insight job answers through (AGL-2915).\n *\n * `read_figures` names readers — an id, a window and the reader's own\n * parameters — and nothing else: the model never writes a query, a field\n * path or a filter of its own, only a choice among the readers the request\n * lists. `submit_insights` is the answer, each insight citing the table rows\n * its figures come from. Lengths and counts are stated in the descriptions,\n * as the platform's other strict tools state them; the step holds every one.\n */\n\nexport const AI_INSIGHT_READ_TOOL_NAME = 'read_figures'\nexport const AI_INSIGHT_ANSWER_TOOL_NAME = 'submit_insights'\n\nexport function aiInsightReadTool(): AiTool {\n return {\n name: AI_INSIGHT_READ_TOOL_NAME,\n description:\n `Choose the readers whose tables answer the question: at most ${AI_INSIGHT_MAX_READS}, ` +\n 'each from the list the request gives. Every field is required.',\n strict: true,\n inputSchema: {\n type: 'object',\n properties: {\n reads: {\n type: 'array',\n description: `The readers to read, most useful first; at most ${AI_INSIGHT_MAX_READS}.`,\n items: {\n type: 'object',\n properties: {\n reader: { type: 'string', description: 'A reader id from the list, exactly as written.' },\n days: {\n type: 'integer',\n description: 'One of the windows that reader lists, in days; 0 for a reader that lists none.',\n },\n params: {\n type: 'array',\n description: 'The parameters that reader lists, by name; empty when it lists none.',\n items: {\n type: 'object',\n properties: {\n name: { type: 'string' },\n value: { type: 'string' },\n },\n required: ['name', 'value'],\n additionalProperties: false,\n },\n },\n },\n required: ['reader', 'days', 'params'],\n additionalProperties: false,\n },\n },\n },\n required: ['reads'],\n additionalProperties: false,\n },\n }\n}\n\nexport function aiInsightAnswerTool(): AiTool {\n return {\n name: AI_INSIGHT_ANSWER_TOOL_NAME,\n description:\n `Answer with at most ${AI_INSIGHT_MAX_INSIGHTS} insights, each citing the table rows its figures come from. ` +\n 'Every field is required.',\n strict: true,\n inputSchema: {\n type: 'object',\n properties: {\n overview: {\n anyOf: [{ type: 'string' }, { type: 'null' }],\n description: `A weekly digest only: what the week meant, in two or three sentences of your own; at most ${AI_INSIGHT_OVERVIEW_MAX_CHARS} characters. null on a question.`,\n },\n insights: {\n type: 'array',\n description: `Most useful first; at most ${AI_INSIGHT_MAX_INSIGHTS}.`,\n items: {\n type: 'object',\n properties: {\n headline: {\n type: 'string',\n description: `What happened, in a few words, with no full stop; at most ${AI_INSIGHT_HEADLINE_MAX_CHARS} characters.`,\n },\n text: {\n type: 'string',\n description: `Why it matters, in one or two plain sentences that do not repeat the headline; at most ${AI_INSIGHT_MAX_CHARS} characters.`,\n },\n why: {\n anyOf: [{ type: 'string' }, { type: 'null' }],\n description: `What most likely drove it, resting on a row you cite from the changes, sources or campaigns table; at most ${AI_INSIGHT_WHY_MAX_CHARS} characters. null when nothing there explains it.`,\n },\n next: {\n anyOf: [{ type: 'string' }, { type: 'null' }],\n description: `One concrete step the owner can take; at most ${AI_INSIGHT_NEXT_MAX_CHARS} characters. null when none fits.`,\n },\n cites: {\n type: 'array',\n description: `The tables and rows its figures come from; at most ${AI_INSIGHT_MAX_CITES} tables.`,\n items: {\n type: 'object',\n properties: {\n table: { type: 'string', description: 'The table handle: t1, t2, …' },\n rows: {\n type: 'array',\n description: `Row numbers as the table numbers them, from 0; at most ${AI_INSIGHT_MAX_CITED_ROWS}.`,\n items: { type: 'integer' },\n },\n },\n required: ['table', 'rows'],\n additionalProperties: false,\n },\n },\n },\n required: ['headline', 'text', 'why', 'next', 'cites'],\n additionalProperties: false,\n },\n },\n gap: {\n anyOf: [{ type: 'string' }, { type: 'null' }],\n description: `What the tables cannot answer, in one sentence without numbers; at most ${AI_INSIGHT_GAP_MAX_CHARS} characters. null when nothing.`,\n },\n },\n required: ['overview', 'insights', 'gap'],\n additionalProperties: false,\n },\n }\n}\n\n/** One reader the model asked for, as it asked. */\nexport interface AiInsightReadRequest {\n reader: string\n days: number\n params: Record<string, string>\n}\n\nconst isRecord = (value: unknown): value is Record<string, unknown> =>\n typeof value === 'object' && value !== null && !Array.isArray(value)\n\n/** The reads a `read_figures` call names, shape-checked and bounded; the step decides which are real. */\nexport function parseAiInsightReads(input: unknown): AiInsightReadRequest[] {\n const reads = isRecord(input) && Array.isArray(input['reads']) ? input['reads'] : []\n const parsed: AiInsightReadRequest[] = []\n for (const raw of reads) {\n if (parsed.length === AI_INSIGHT_MAX_READS) break\n if (!isRecord(raw) || typeof raw['reader'] !== 'string') continue\n const params: Record<string, string> = {}\n for (const pair of Array.isArray(raw['params']) ? raw['params'] : []) {\n if (isRecord(pair) && typeof pair['name'] === 'string' && typeof pair['value'] === 'string') {\n params[pair['name']] = pair['value']\n }\n }\n const days = Number(raw['days'])\n parsed.push({\n reader: raw['reader'].trim(),\n days: Number.isInteger(days) && days >= 0 ? days : 0,\n params,\n })\n }\n return parsed\n}\n\n/**\n * A `submit_insights` call as the trace reads it, or `null` when the call is\n * not an answer at all. Values keep the types the model wrote, so the trace\n * — not this reader — decides what an unreadable citation costs.\n */\nexport function parseAiInsightAnswer(input: unknown): AiInsightAnswer | null {\n if (!isRecord(input) || !Array.isArray(input['insights'])) return null\n const insights = input['insights']\n .filter(isRecord)\n .map((raw) => ({\n ...(typeof raw['headline'] === 'string' ? { headline: raw['headline'] } : {}),\n text: typeof raw['text'] === 'string' ? raw['text'] : '',\n ...(typeof raw['why'] === 'string' ? { why: raw['why'] } : {}),\n ...(typeof raw['next'] === 'string' ? { next: raw['next'] } : {}),\n cites: (Array.isArray(raw['cites']) ? raw['cites'] : []).filter(isRecord).map((cite) => ({\n table: typeof cite['table'] === 'string' ? cite['table'] : '',\n rows: (Array.isArray(cite['rows']) ? cite['rows'] : []) as number[],\n })),\n }))\n const gap = typeof input['gap'] === 'string' ? input['gap'] : null\n const overview = typeof input['overview'] === 'string' ? input['overview'] : null\n return { overview, insights, gap }\n}\n"],"names":["AI_INSIGHT_GAP_MAX_CHARS","AI_INSIGHT_HEADLINE_MAX_CHARS","AI_INSIGHT_NEXT_MAX_CHARS","AI_INSIGHT_OVERVIEW_MAX_CHARS","AI_INSIGHT_WHY_MAX_CHARS","AI_INSIGHT_MAX_CHARS","AI_INSIGHT_MAX_CITED_ROWS","AI_INSIGHT_MAX_CITES","AI_INSIGHT_MAX_INSIGHTS","AI_INSIGHT_MAX_READS","AI_INSIGHT_READ_TOOL_NAME","AI_INSIGHT_ANSWER_TOOL_NAME","aiInsightReadTool","name","description","strict","inputSchema","type","properties","reads","items","reader","days","params","value","required","additionalProperties","aiInsightAnswerTool","overview","anyOf","insights","headline","text","why","next","cites","table","rows","gap","isRecord","Array","isArray","parseAiInsightReads","input","parsed","raw","length","pair","Number","push","trim","isInteger","parseAiInsightAnswer","filter","map","cite"],"mappings":";AAAA;;;;;;;;;;;;;;;CAeC,GAED,SACEA,wBAAwB,EACxBC,6BAA6B,EAC7BC,yBAAyB,EACzBC,6BAA6B,EAC7BC,wBAAwB,EACxBC,oBAAoB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