@aglyn/plugins-ai 1.2.0 → 1.4.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.
Files changed (83) hide show
  1. package/package.json +17 -17
  2. package/src/lib/components/ai-insight-dialog.component.js +7 -2
  3. package/src/lib/components/ai-insight-dialog.component.js.map +1 -1
  4. package/src/lib/components/ai-job-request.component.d.ts +28 -0
  5. package/src/lib/components/ai-job-request.component.js +103 -0
  6. package/src/lib/components/ai-job-request.component.js.map +1 -0
  7. package/src/lib/components/ai-jobs-list-page.component.d.ts +2 -0
  8. package/src/lib/components/ai-jobs-list-page.component.js +2 -2
  9. package/src/lib/components/ai-jobs-list-page.component.js.map +1 -1
  10. package/src/lib/components/ai-site-build-page.component.js +36 -6
  11. package/src/lib/components/ai-site-build-page.component.js.map +1 -1
  12. package/src/lib/components/ai-site-import-job.component.js +2 -2
  13. package/src/lib/components/ai-site-import-job.component.js.map +1 -1
  14. package/src/lib/insights/ai-insight-digest.d.ts +34 -5
  15. package/src/lib/insights/ai-insight-digest.js +171 -10
  16. package/src/lib/insights/ai-insight-digest.js.map +1 -1
  17. package/src/lib/jobs/ai-job-import-step.d.ts +35 -2
  18. package/src/lib/jobs/ai-job-import-step.js +124 -12
  19. package/src/lib/jobs/ai-job-import-step.js.map +1 -1
  20. package/src/lib/jobs/ai-job-insight-step.d.ts +1 -1
  21. package/src/lib/jobs/ai-job-insight-step.js +5 -1
  22. package/src/lib/jobs/ai-job-insight-step.js.map +1 -1
  23. package/src/lib/jobs/ai-jobs.d.ts +6 -0
  24. package/src/lib/jobs/ai-jobs.js +43 -6
  25. package/src/lib/jobs/ai-jobs.js.map +1 -1
  26. package/src/lib/jobs/ai-layout-stock-photos.d.ts +13 -6
  27. package/src/lib/jobs/ai-layout-stock-photos.js +104 -32
  28. package/src/lib/jobs/ai-layout-stock-photos.js.map +1 -1
  29. package/src/lib/jobs/ai-site-import-compile.js +110 -23
  30. package/src/lib/jobs/ai-site-import-compile.js.map +1 -1
  31. package/src/lib/jobs/ai-site-import-document.d.ts +5 -0
  32. package/src/lib/jobs/ai-site-import-document.js.map +1 -1
  33. package/src/lib/jobs/ai-site-import-forms.d.ts +83 -0
  34. package/src/lib/jobs/ai-site-import-forms.js +376 -0
  35. package/src/lib/jobs/ai-site-import-forms.js.map +1 -0
  36. package/src/lib/jobs/fixtures/ai-stock-photo-agl4092-1101-recordings.d.ts +21 -0
  37. package/src/lib/jobs/fixtures/ai-stock-photo-agl4092-1101-recordings.js +77 -0
  38. package/src/lib/jobs/fixtures/ai-stock-photo-agl4092-1101-recordings.js.map +1 -0
  39. package/src/lib/layout-language/ai-layout-compiler.d.ts +2 -0
  40. package/src/lib/layout-language/ai-layout-compiler.js +215 -30
  41. package/src/lib/layout-language/ai-layout-compiler.js.map +1 -1
  42. package/src/lib/layout-language/ai-layout-language.d.ts +12 -0
  43. package/src/lib/layout-language/ai-layout-language.js.map +1 -1
  44. package/src/lib/model/ai-credit-estimate.d.ts +25 -22
  45. package/src/lib/model/ai-credit-estimate.js +24 -22
  46. package/src/lib/model/ai-credit-estimate.js.map +1 -1
  47. package/src/lib/model/ai-insight.d.ts +4 -0
  48. package/src/lib/model/ai-insight.js +1 -0
  49. package/src/lib/model/ai-insight.js.map +1 -1
  50. package/src/lib/model/ai-site-build-progress.d.ts +8 -0
  51. package/src/lib/model/ai-site-build-progress.js +62 -1
  52. package/src/lib/model/ai-site-build-progress.js.map +1 -1
  53. package/src/lib/model/ai-site-import-fidelity.d.ts +2 -0
  54. package/src/lib/model/ai-site-import-fidelity.js +41 -5
  55. package/src/lib/model/ai-site-import-fidelity.js.map +1 -1
  56. package/src/lib/model/ai-site-import-report.d.ts +60 -0
  57. package/src/lib/model/ai-site-import-report.js +116 -0
  58. package/src/lib/model/ai-site-import-report.js.map +1 -0
  59. package/src/lib/model/ai-site-import-view.d.ts +7 -1
  60. package/src/lib/model/ai-site-import-view.js +14 -2
  61. package/src/lib/model/ai-site-import-view.js.map +1 -1
  62. package/src/lib/model/ai-site-import.d.ts +49 -2
  63. package/src/lib/model/ai-site-import.js +81 -23
  64. package/src/lib/model/ai-site-import.js.map +1 -1
  65. package/src/lib/plugin.js +1 -1
  66. package/src/lib/plugin.js.map +1 -1
  67. package/src/lib/runtime/ai-insight-check.js +22 -6
  68. package/src/lib/runtime/ai-insight-check.js.map +1 -1
  69. package/src/lib/server/ai-insight-digest-route.js +3 -1
  70. package/src/lib/server/ai-insight-digest-route.js.map +1 -1
  71. package/src/lib/site-import/fixtures/site-layout-recordings.d.ts +4 -0
  72. package/src/lib/site-import/fixtures/site-layout-recordings.js +9 -4
  73. package/src/lib/site-import/fixtures/site-layout-recordings.js.map +1 -1
  74. package/src/lib/site-import/site-import-css.d.ts +10 -1
  75. package/src/lib/site-import/site-import-css.js +19 -12
  76. package/src/lib/site-import/site-import-css.js.map +1 -1
  77. package/src/lib/site-import/site-import-html.d.ts +17 -4
  78. package/src/lib/site-import/site-import-html.js +146 -17
  79. package/src/lib/site-import/site-import-html.js.map +1 -1
  80. package/src/lib/site-import/site-import-layout.js +194 -32
  81. package/src/lib/site-import/site-import-layout.js.map +1 -1
  82. package/src/lib/tools/ai-insight-tool.js +11 -3
  83. package/src/lib/tools/ai-insight-tool.js.map +1 -1
@@ -39,7 +39,8 @@ import { _ as _extends } from "@swc/helpers/_/_extends";
39
39
  * own header and footer words as the footer's facts, and the site's
40
40
  * menu order.
41
41
  * 5. EACH PAGE: its photos into the media library (no model, deduped by
42
- * source key), then the page step's own passes, NEVER REWRITTEN
42
+ * source key), its forms rebuilt as the site's own (no model, AGL-4083,
43
+ * `ai-site-import-forms.ts`), then the page step's own passes, NEVER REWRITTEN
43
44
  * (AGL-4084, `ai-site-import-document.ts`): a collection's page or a
44
45
  * long page is copied as it is, every word in order, with no model; a
45
46
  * main page is arranged, the model placing the source's numbered lines
@@ -61,7 +62,7 @@ import { listDomainOwnershipClaims, normalizeOwnershipDomain } from "@aglyn/tena
61
62
  import { getOrgForUser, memberHasOrgPermission } from "@aglyn/tenant-data-admin/server/organizations";
62
63
  import { consumeRateLimit } from "@aglyn/tenant-data-admin/server/rate-limit-store";
63
64
  import { isServerReleaseFlagOnForOrg } from "@aglyn/tenant-data-admin/server/release-flags";
64
- import { AI_SITE_IMPORT_DOMAIN_INPUT, AI_SITE_IMPORT_FRAME_SLOT, AI_SITE_IMPORT_LABEL_MAX_CHARS, AI_SITE_IMPORT_LEFT_OUT_COPY, AI_SITE_IMPORT_MAX_PAGES, AI_SITE_IMPORT_MIN_WORDS, AI_SITE_IMPORT_WAVE_PAGES, aiSiteImportFrameIsRead, aiSiteImportInventory, aiSiteImportItemOpen, aiSiteImportLedger, aiSiteImportNextWork, aiSiteImportPageBuild, aiSiteImportPageSettled, aiSiteImportPagesRead, aiSiteImportPhotosIn, aiSiteImportReadStops, aiSiteImportReadVerdict, aiSiteImportReadVerdictMessage, aiSiteImportReadVerdictReason, aiSiteImportWaveClosed, aiSiteImportWaveMessage, aiSiteImportWaveStarted } from "../model/ai-site-import.js";
65
+ import { AI_SITE_IMPORT_DOMAIN_INPUT, AI_SITE_IMPORT_FRAME_SLOT, AI_SITE_IMPORT_LABEL_MAX_CHARS, AI_SITE_IMPORT_LEFT_OUT_COPY, AI_SITE_IMPORT_MAX_PAGES, AI_SITE_IMPORT_MIN_WORDS, AI_SITE_IMPORT_WAVE_PAGES, aiSiteImportFormsIn, aiSiteImportFrameIsRead, aiSiteImportInventory, aiSiteImportItemOpen, aiSiteImportLedger, aiSiteImportNextWork, aiSiteImportPageBuild, aiSiteImportPageSettled, aiSiteImportPagesRead, aiSiteImportPhotosIn, aiSiteImportReadStops, aiSiteImportReadVerdict, aiSiteImportReadVerdictMessage, aiSiteImportReadVerdictReason, aiSiteImportWaveClosed, aiSiteImportWaveMessage, aiSiteImportWaveStarted } from "../model/ai-site-import.js";
65
66
  import { aiModelForStep } from "../providers/routing.js";
66
67
  import { SITE_IMPORT_RELEASE_FLAG } from "../site-import/site-import-flag.js";
67
68
  import { crawlSiteForImport, siteImportOrigins } from "../site-import/site-import-crawl.js";
@@ -72,7 +73,8 @@ import { AI_LAYOUT_SITE_BRAND_INPUT, AI_LAYOUT_SITE_FACTS_INPUT, AI_LAYOUT_SITE_
72
73
  import { aiLayoutTelHref } from "../layout-language/ai-layout-frame-source.js";
73
74
  import { aiHexOklch } from "../model/ai-site-look.js";
74
75
  import { AI_LAYOUT_IMPORTED_SECTIONS_INPUT } from "./ai-site-import-document.js";
75
- import { aiSiteImportSectionNames } from "./ai-site-import-compile.js";
76
+ import { aiImportedPageListingOwed, aiSiteImportSectionNames } from "./ai-site-import-compile.js";
77
+ import { aiRebuildImportedForms } from "./ai-site-import-forms.js";
76
78
  import { aiRunSiteImportLook } from "./ai-site-import-look.js";
77
79
  import { aiPublishImportedWave } from "./ai-site-import-publish.js";
78
80
  import { aiUnitErrorRetryable, aiUnitFailure, aiUnitSpend } from "./ai-build-unit-outcome.js";
@@ -87,7 +89,7 @@ import { AI_LAYOUT_IMPORTED_PAGE_INPUT, AI_LAYOUT_LANGUAGE_INPUT } from "./ai-jo
87
89
  import { aiSitePageWritten } from "./ai-job-site-step.js";
88
90
  import { AI_STOCK_PHOTO_PAGES_INPUT } from "./ai-layout-stock-photos.js";
89
91
  import { AI_JOB_BRIEF_MAX_CHARS } from "./ai-job-text-step.js";
90
- import { aiApplyJobItemRecord, aiJobStepRunMinimumMs, aiJobStepRunnerFor, registerAiJobStep, registerAiJobStepPasses } from "./ai-jobs.js";
92
+ import { aiApplyJobItemRecord, aiJobStepRunMinimumMs, aiJobStepRunnerFor, registerAiJobStep, registerAiJobStepPasses, registerAiJobStopReview } from "./ai-jobs.js";
91
93
  /** Waves (the read included) one workspace may start in a day. */ export const SITE_IMPORT_WAVES_PER_ORG_PER_DAY = 12;
92
94
  const DAY_MS = 24 * 60 * 60 * 1000;
93
95
  /** The rate-limit key a workspace's imports share. */ export function siteImportRateKey(orgId) {
@@ -106,8 +108,17 @@ const DAY_MS = 24 * 60 * 60 * 1000;
106
108
  * The passes one import may take: a read, then for every page its photos,
107
109
  * its answer and its listing, every wave's start and the header and footer,
108
110
  * with room for re-asks.
109
- */ export const AI_SITE_IMPORT_MAX_PASSES = AI_SITE_IMPORT_MAX_PAGES * 4 + 60;
111
+ */ export const AI_SITE_IMPORT_MAX_PASSES = AI_SITE_IMPORT_MAX_PAGES * 8 + 60;
112
+ /**
113
+ * The most the job's document may weigh with a wave's reads in it (AGL-4083),
114
+ * as JSON, which runs about a tenth over what Firestore stores, so this leaves
115
+ * room under its 1 MiB for what the pass writes beside them. Pages past it are
116
+ * read once the pages before them are in. On 11/1 a 400-page import's start of
117
+ * its 36th wave asked for about 1.1 MB, and the write failed until its attempts
118
+ * ran out.
119
+ */ export const AI_SITE_IMPORT_JOB_MAX_BYTES = 980000;
110
120
  /** Customer-safe sentences. */ export const AI_SITE_IMPORT_COPY = {
121
+ stopped: (pages, wave)=>`Your import stopped before it finished, so ${pages.toLocaleString('en-US')} ${pages === 1 ? 'page is' : 'pages are'} not in yet. Everything brought in so far is safe. Start ${wave} to bring in the rest.`,
111
122
  noSite: 'Open the site to bring your website into.',
112
123
  badDomain: 'This website’s address cannot be brought in.',
113
124
  ownership: (domain)=>`Verify that you own ${domain} before bringing it in. A proof is good for 30 days; verify again, then start this wave again.`,
@@ -425,7 +436,9 @@ const delivered = (items, slot)=>items.some((row)=>row.slot === slot && (row.sta
425
436
  `https://${state.domain}`,
426
437
  `https://www.${state.domain.replace(/^www\./, '')}`
427
438
  ]
428
- }, ((_page_read1 = page.read) == null ? void 0 : _page_read1.layout) ? {
439
+ }, page.forms ? {
440
+ forms: page.forms
441
+ } : {}, ((_page_read1 = page.read) == null ? void 0 : _page_read1.layout) ? {
429
442
  layout: page.read.layout
430
443
  } : {});
431
444
  }
@@ -552,7 +565,22 @@ const delivered = (items, slot)=>items.some((row)=>row.slot === slot && (row.sta
552
565
  look: page.look
553
566
  } : {});
554
567
  }
555
- /** The least time the import's next pass needs: a read's, a photo pass's, or the time of the step its unit is handed to. */ export function aiSiteImportRunMinimumMs(job, runnerFor = aiJobStepRunnerFor) {
568
+ /** Whether a page asks no model at all: copied as it is, its listing the source's own. */ function aiSiteImportPageAsksNoModel(page) {
569
+ var _ref;
570
+ var _page_read_meta, _page_read, _page_read_meta1, _page_read1;
571
+ if (page.build !== 'copied') return false;
572
+ return !aiImportedPageListingOwed({
573
+ title: ((_page_read = page.read) == null ? void 0 : (_page_read_meta = _page_read.meta) == null ? void 0 : _page_read_meta.title) || page.title,
574
+ description: (_ref = (_page_read1 = page.read) == null ? void 0 : (_page_read_meta1 = _page_read1.meta) == null ? void 0 : _page_read_meta1.description) != null ? _ref : ''
575
+ });
576
+ }
577
+ /**
578
+ * The least time the import's next pass needs: a read's, a photo pass's, or
579
+ * the time of the step its unit is handed to. A pass that fetches nothing and
580
+ * asks no model needs none of that (AGL-4083): held to a model's whole answer,
581
+ * a copied page could only start in the first seconds of a beat, and the 11/1
582
+ * import took 6 to 7 minutes for each ten pages that used no AI.
583
+ */ export function aiSiteImportRunMinimumMs(job, runnerFor = aiJobStepRunnerFor) {
556
584
  var _job_siteImport, _job_items;
557
585
  const state = (_job_siteImport = job.siteImport) != null ? _job_siteImport : null;
558
586
  const items = (_job_items = job.items) != null ? _job_items : [];
@@ -564,10 +592,12 @@ const delivered = (items, slot)=>items.some((row)=>row.slot === slot && (row.sta
564
592
  case 'read':
565
593
  return AI_SITE_IMPORT_PASS_MINIMUM_MS.wave;
566
594
  case 'images':
567
- return AI_SITE_IMPORT_PASS_MINIMUM_MS.photos;
595
+ var _work_page_read;
596
+ return ((_work_page_read = work.page.read) == null ? void 0 : _work_page_read.images.length) ? AI_SITE_IMPORT_PASS_MINIMUM_MS.photos : 0;
568
597
  case 'frame':
569
598
  return runnerFor('layout') && state ? aiJobStepRunMinimumMs(aiSiteImportFrameJob(job, state, work.wave, items)) : 0;
570
599
  case 'page':
600
+ if (aiSiteImportPageAsksNoModel(work.page)) return 0;
571
601
  return runnerFor('page') && state ? aiJobStepRunMinimumMs(aiSiteImportPageJob(job, state, work.page, items)) : 0;
572
602
  default:
573
603
  return 0;
@@ -580,6 +610,58 @@ const delivered = (items, slot)=>items.some((row)=>row.slot === slot && (row.sta
580
610
  message,
581
611
  findings: []
582
612
  });
613
+ const jobBytes = (value)=>{
614
+ var _JSON_stringify;
615
+ return Buffer.byteLength((_JSON_stringify = JSON.stringify(value)) != null ? _JSON_stringify : '');
616
+ };
617
+ /**
618
+ * The reads a job's document can take (AGL-4083): the pages in order, as many
619
+ * as fit `AI_SITE_IMPORT_JOB_MAX_BYTES`, and every page the read left out. A
620
+ * page past that is read later, once the ones before it are in. With nothing
621
+ * else held, the first page goes whatever it weighs, or no page would ever go.
622
+ */ export function aiSiteImportReadsWithin(job, state, reads) {
623
+ let room = AI_SITE_IMPORT_JOB_MAX_BYTES - jobBytes(_extends({}, job, {
624
+ siteImport: state
625
+ }));
626
+ let first = !state.pages.some((page)=>{
627
+ var _page_read_blocks, _page_read;
628
+ return (_page_read = page.read) == null ? void 0 : (_page_read_blocks = _page_read.blocks) == null ? void 0 : _page_read_blocks.length;
629
+ });
630
+ let full = false;
631
+ const kept = new Map();
632
+ for (const [slot, read] of reads){
633
+ if (typeof read === 'string') {
634
+ kept.set(slot, read);
635
+ continue;
636
+ }
637
+ const weight = jobBytes(read);
638
+ full || (full = weight > room && !first);
639
+ if (full) continue;
640
+ kept.set(slot, read);
641
+ room -= weight;
642
+ first = false;
643
+ }
644
+ return kept;
645
+ }
646
+ /**
647
+ * What an import waits on when it stopped with work left (AGL-4083). Its run
648
+ * can take hours, so a read or a write that keeps failing, or the bound on its
649
+ * passes, ends a stretch of it and never the import: the person who started it
650
+ * is told, and the waves it did not reach stay theirs to start.
651
+ */ export function aiSiteImportStopReview(job) {
652
+ var _job_siteImport, _job_items;
653
+ const state = (_job_siteImport = job.siteImport) != null ? _job_siteImport : null;
654
+ if (!state) return null;
655
+ const items = (_job_items = job.items) != null ? _job_items : [];
656
+ const work = aiSiteImportNextWork(state, items);
657
+ if (work.kind === 'done' || work.kind === 'inventory') return null;
658
+ const left = state.pages.filter((page)=>!page.refused && aiSiteImportItemOpen(items, page.slot)).length;
659
+ return {
660
+ reason: 'wave',
661
+ message: AI_SITE_IMPORT_COPY.stopped(left, work.wave.label),
662
+ findings: []
663
+ };
664
+ }
583
665
  /** The rows a wave's read left out, settled as not brought in, with the reason. */ function withLeftOutRows(items, reads) {
584
666
  return items.map((row)=>{
585
667
  const read = reads.get(row.slot);
@@ -901,7 +983,9 @@ export function createAiJobImportStep(deps = {}) {
901
983
  note: 'Not built: no page of this wave could be read.'
902
984
  }) : row);
903
985
  }
904
- const started = aiSiteImportWithBuilds(aiSiteImportWaveStarted(state, wave.index, nowIso, reads));
986
+ const started = aiSiteImportWithBuilds(aiSiteImportWaveStarted(state, wave.index, nowIso, aiSiteImportReadsWithin(_extends({}, job, {
987
+ items: ledger
988
+ }), state, reads)));
905
989
  const running = started.waves.find((entry)=>entry.index === wave.index);
906
990
  return closing(started, ledger, running, unspent({
907
991
  items: ledger
@@ -925,7 +1009,9 @@ export function createAiJobImportStep(deps = {}) {
925
1009
  cutShort: crawled.stoppedBy === 'time'
926
1010
  });
927
1011
  const ledger = withLeftOutRows(items, reads);
928
- return closing(aiSiteImportWithBuilds(aiSiteImportPagesRead(state, reads)), ledger, wave, unspent({
1012
+ return closing(aiSiteImportWithBuilds(aiSiteImportPagesRead(state, aiSiteImportReadsWithin(_extends({}, job, {
1013
+ items: ledger
1014
+ }), state, reads))), ledger, wave, unspent({
929
1015
  items: ledger
930
1016
  }));
931
1017
  }
@@ -1005,6 +1091,29 @@ export function createAiJobImportStep(deps = {}) {
1005
1091
  continue: true
1006
1092
  });
1007
1093
  }
1094
+ // ── 4a'. A page's forms, as the site's own: no model, no credits (AGL-4083) ──
1095
+ if (work.kind === 'forms') {
1096
+ var _context_org2;
1097
+ const rebuilt = await aiRebuildImportedForms(firestore, {
1098
+ job,
1099
+ hostId,
1100
+ state,
1101
+ page: work.page,
1102
+ org: (_context_org2 = context.org) != null ? _context_org2 : null,
1103
+ now
1104
+ }, deps.forms);
1105
+ const fresh = rebuilt.rows.filter((row)=>!items.some((held)=>held.slot === row.slot));
1106
+ return unspent(_extends({}, fresh.length ? {
1107
+ items: [
1108
+ ...items,
1109
+ ...fresh
1110
+ ]
1111
+ } : {}, {
1112
+ outputs: rebuilt.outputs,
1113
+ siteImport: aiSiteImportFormsIn(state, work.page.slot, rebuilt.forms, rebuilt.note),
1114
+ continue: true
1115
+ }));
1116
+ }
1008
1117
  // ── 4b. The page ──
1009
1118
  if (work.kind === 'page') {
1010
1119
  const runner = runnerFor('page');
@@ -1076,11 +1185,13 @@ export function createAiJobImportStep(deps = {}) {
1076
1185
  draftId: page.draftId,
1077
1186
  layoutLanguage: true
1078
1187
  });
1079
- item = written ? {
1188
+ item = written ? _extends({
1080
1189
  slot,
1081
1190
  status: 'succeeded',
1082
1191
  outputs: spent.outputs.map((output)=>output.id)
1083
- } : {
1192
+ }, page.formNote ? {
1193
+ note: page.formNote
1194
+ } : {}) : {
1084
1195
  slot,
1085
1196
  status: 'failed',
1086
1197
  failure: {
@@ -1265,6 +1376,7 @@ function siteImportReadOrEmpty(page, at) {
1265
1376
  minimumMsFor: (job)=>aiSiteImportRunMinimumMs(job, runnerFor)
1266
1377
  });
1267
1378
  registerAiJobStepPasses('import', AI_SITE_IMPORT_MAX_PASSES);
1379
+ registerAiJobStopReview('import', aiSiteImportStopReview);
1268
1380
  registerAiJobAdmission('import', createAiSiteImportAdmission());
1269
1381
  }
1270
1382