@aglyn/plugins-ai 1.27.0 → 1.28.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 (43) hide show
  1. package/package.json +17 -17
  2. package/src/lib/components/ai-credits-prompt.component.js +4 -1
  3. package/src/lib/components/ai-credits-prompt.component.js.map +1 -1
  4. package/src/lib/components/ai-site-import-job.component.js +5 -2
  5. package/src/lib/components/ai-site-import-job.component.js.map +1 -1
  6. package/src/lib/components/ai-site-start-card.component.js +6 -3
  7. package/src/lib/components/ai-site-start-card.component.js.map +1 -1
  8. package/src/lib/components/assist-build-card.component.js +4 -1
  9. package/src/lib/components/assist-build-card.component.js.map +1 -1
  10. package/src/lib/components/assist-edit-canvas.d.ts +12 -0
  11. package/src/lib/components/assist-edit-canvas.js +38 -7
  12. package/src/lib/components/assist-edit-canvas.js.map +1 -1
  13. package/src/lib/components/assist-jobs-drawer.component.js +4 -1
  14. package/src/lib/components/assist-jobs-drawer.component.js.map +1 -1
  15. package/src/lib/components/assist-panel.component.js +5 -2
  16. package/src/lib/components/assist-panel.component.js.map +1 -1
  17. package/src/lib/declarations.server.js +17 -0
  18. package/src/lib/declarations.server.js.map +1 -1
  19. package/src/lib/jobs/ai-job-drafts.d.ts +5 -11
  20. package/src/lib/jobs/ai-job-drafts.js +8 -25
  21. package/src/lib/jobs/ai-job-drafts.js.map +1 -1
  22. package/src/lib/jobs/ai-job-edit-step.d.ts +95 -5
  23. package/src/lib/jobs/ai-job-edit-step.js +88 -17
  24. package/src/lib/jobs/ai-job-edit-step.js.map +1 -1
  25. package/src/lib/model/ai-edit-job.d.ts +27 -0
  26. package/src/lib/model/ai-edit-job.js +45 -23
  27. package/src/lib/model/ai-edit-job.js.map +1 -1
  28. package/src/lib/model/assist-edit.d.ts +2 -0
  29. package/src/lib/model/assist-edit.js.map +1 -1
  30. package/src/lib/runtime/ai-node-tree.d.ts +25 -1
  31. package/src/lib/runtime/ai-node-tree.js +39 -14
  32. package/src/lib/runtime/ai-node-tree.js.map +1 -1
  33. package/src/lib/server/ai-page-content.d.ts +83 -0
  34. package/src/lib/server/ai-page-content.js +765 -0
  35. package/src/lib/server/ai-page-content.js.map +1 -0
  36. package/src/lib/server/ai-usage.js +7 -1
  37. package/src/lib/server/ai-usage.js.map +1 -1
  38. package/src/lib/server/assist-edit.d.ts +136 -1
  39. package/src/lib/server/assist-edit.js +124 -47
  40. package/src/lib/server/assist-edit.js.map +1 -1
  41. package/src/lib/usage/ai-usage-wire.d.ts +15 -0
  42. package/src/lib/usage/ai-usage-wire.js +21 -0
  43. package/src/lib/usage/ai-usage-wire.js.map +1 -1
@@ -18,6 +18,7 @@ import { SCREEN_SEO_TEXT_FIELDS } from '@aglyn/aglyn/app-utils/screen-seo-fields
18
18
  import { CanvasManager } from '@aglyn/aglyn/canvas-manager/canvas-manager';
19
19
  import type { NodesMap } from '@aglyn/aglyn/types/nodes';
20
20
  import { resolveOrgIdForHost } from '@aglyn/tenant-data-admin/server/organizations';
21
+ import { type AssistEditInserted } from '../components/assist-edit-canvas';
21
22
  import { type AiEditJobInputs, type AiEditPlacement, type AiEditTargetKind } from '../model/ai-edit-job';
22
23
  import type { AiJob, AiJobOutput } from '../model/ai-jobs.types';
23
24
  import { type AssistEditCanvasContext, type AssistEditOp, type AssistEditProposal, type AssistEditTarget } from '../model/assist-edit';
@@ -119,6 +120,7 @@ export declare function aiEditCanvas(nodes: NodesMap): CanvasManager;
119
120
  export declare function aiApplyEditToNodes(nodes: NodesMap, proposal: AssistEditProposal, seo?: Partial<Record<(typeof SCREEN_SEO_TEXT_FIELDS)[number], string>>): {
120
121
  ok: true;
121
122
  nodes: NodesMap;
123
+ inserted: AssistEditInserted[];
122
124
  } | {
123
125
  ok: false;
124
126
  reason: string;
@@ -158,6 +160,48 @@ export interface AiEditTargetRefusal {
158
160
  status: 400 | 404;
159
161
  error: string;
160
162
  }
163
+ /**
164
+ * A version's own tree as the Besigner opens it (AGL-4235): the stored
165
+ * `nodes` with the version's layout style overrides parked on the root by the
166
+ * editor's own `injectLayoutStyleOverrides`. A draft the editor saves carries
167
+ * them there, so a draft built on this tree does too, and publishing it — from
168
+ * the editor or over the API — keeps them rather than clearing them. Null
169
+ * without a tree.
170
+ */
171
+ export declare function aiEditSavedTree(version: FirebaseFirestore.DocumentSnapshot): NodesMap | null;
172
+ /** The tree a change is built on, as it is stored now (AGL-4235). */
173
+ export interface AiEditBase {
174
+ /**
175
+ * `draft` for the version's working draft, when the Besigner would still
176
+ * offer it; `saved` for the version's own tree otherwise.
177
+ */
178
+ from: 'draft' | 'saved';
179
+ /**
180
+ * The tree, as the Besigner would hold it: a draft's as its author left it,
181
+ * the saved version's with its layout style overrides on the root.
182
+ */
183
+ nodes: NodesMap;
184
+ /**
185
+ * The `nodes` field as stored, exactly as read: what a revision of the tree
186
+ * is read off, so parking the overrides on the root never moves it.
187
+ */
188
+ stored: unknown;
189
+ /** What stamped the draft, when a write did (`aiEditJobId`); null for a saved tree. */
190
+ stampedBy: string | null;
191
+ }
192
+ /**
193
+ * The tree a change to a version is built on — the one rule, for the edit job,
194
+ * its write and a reader that shows a page as the next change will find it:
195
+ * the version's working draft when the Besigner would still offer it, so an
196
+ * author's work in progress is never thrown away; otherwise the version's own
197
+ * tree. `useDraft: false` is a change that lands somewhere other than the
198
+ * draft, built on the version alone. Null when neither has a tree.
199
+ */
200
+ export declare function aiEditBase(draft: FirebaseFirestore.DocumentSnapshot, version: FirebaseFirestore.DocumentSnapshot, options?: {
201
+ useDraft?: boolean;
202
+ }): AiEditBase | null;
203
+ /** A version's working draft document: the Besigner's, at the core's one address. */
204
+ export declare function draftRef(versions: FirebaseFirestore.CollectionReference, versionId: string): FirebaseFirestore.DocumentReference;
161
205
  /**
162
206
  * The document an edit job changes, on the job's own site: the page or
163
207
  * layout named, not deleted, not an email design, and the version it starts
@@ -193,33 +237,79 @@ export declare function aiEditJobPlacement(input: {
193
237
  sourceIsPointer: boolean;
194
238
  sourceScheduled: boolean;
195
239
  }): AiEditPlacement;
196
- export interface AiEditWriteInput {
240
+ /** What a proposal built from the stored tree sees of the document, besides the tree (AGL-4235). */
241
+ export interface AiEditWriteBase extends AiEditBase {
242
+ /** The document's current version, as the transaction reads it. */
243
+ pointerVersionId: string | null;
244
+ }
245
+ export interface AiEditWriteInput<R = never> {
197
246
  hostId: string;
198
247
  kind: AiEditTargetKind;
199
248
  id: string;
200
249
  sourceVersionId: string;
201
- /** The new version's id, which is the job's; also the stamp a draft it writes carries. */
250
+ /**
251
+ * A fresh id for this write: the new version's id, which is the job's, and
252
+ * the stamp a draft it writes carries, which is how a write asked again
253
+ * finds what it wrote. An API edit hands its own (AGL-4235).
254
+ */
202
255
  jobVersionId: string;
203
256
  versioning: boolean;
204
257
  /** The request asked for a new version (see {@link aiEditJobPlacement}). */
205
258
  newVersion: boolean;
206
- proposal: AssistEditProposal;
259
+ /**
260
+ * The change. A model's proposal is checked before the write against the
261
+ * tree the model was shown, and the canvas's guards refuse it whole if an
262
+ * element changed since. A caller that holds no tree yet (AGL-4235) hands a
263
+ * function instead, given the tree exactly as this transaction reads it,
264
+ * and validates and builds the change on that, so what it checked and what
265
+ * is written are one tree. It refuses with `{ refused }`, and nothing is
266
+ * written.
267
+ */
268
+ proposal: AssistEditProposal | ((base: AiEditWriteBase) => AssistEditProposal | {
269
+ refused: R;
270
+ });
207
271
  /** Whether the page's search fields may be written: a page no visitor reaches. */
208
272
  writeSeo: boolean;
209
273
  versionName: string;
274
+ /** Who the write is for: a member's uid, or a stable id for a writer that is no person. */
210
275
  uid: string;
276
+ /**
277
+ * Who the write is for in words, written on a draft as `updatedByLabel`
278
+ * when given: an API key's or a connected app's name (AGL-4235). The job
279
+ * writes none.
280
+ */
281
+ updatedByLabel?: string;
282
+ /**
283
+ * Refuse a tree larger than this many bytes, measured as the save measures
284
+ * it (`measureNodeMap`), and write nothing. Absent: not measured.
285
+ */
286
+ maxNodeBytes?: number;
211
287
  /** The job the member started, stamped as `aiJobId` on a new version. */
212
288
  aiJobId: string;
213
289
  now: Date;
214
290
  }
215
- export type AiEditWrite = {
291
+ export type AiEditWrite<R = never> = {
216
292
  ok: true;
217
293
  versionId: string;
218
294
  placement: AiEditPlacement;
219
295
  seoWritten: boolean;
296
+ /**
297
+ * What this call wrote, when it wrote (not a run asked again): the
298
+ * stored tree's bytes, and the ids the canvas gave each insert's elements.
299
+ */
300
+ written?: {
301
+ stored: Uint8Array;
302
+ inserted: AssistEditInserted[];
303
+ };
220
304
  } | {
221
305
  ok: false;
222
306
  error: string;
307
+ /** The caller's own refusal, from a proposal built on the stored tree. */
308
+ refused?: R;
309
+ /** The tree would pass `maxNodeBytes`: its size. */
310
+ tooLarge?: {
311
+ bytes: number;
312
+ };
223
313
  };
224
314
  /**
225
315
  * The change written, in one transaction that reads the document as it is
@@ -228,7 +318,7 @@ export type AiEditWrite = {
228
318
  * proposal to the stored nodes through the canvas's guards. An element that
229
319
  * changed since the model saw it refuses the whole change, as the editor does.
230
320
  */
231
- export declare function writeAiEdit(firestore: Firestore, input: AiEditWriteInput): Promise<AiEditWrite>;
321
+ export declare function writeAiEdit<R = never>(firestore: Firestore, input: AiEditWriteInput<R>): Promise<AiEditWrite<R>>;
232
322
  export interface AiJobEditStepDeps {
233
323
  readTarget?: typeof readAiEditTarget;
234
324
  writeEdit?: typeof writeAiEdit;
@@ -18,14 +18,16 @@ import { _ as _extends } from "@swc/helpers/_/_extends";
18
18
  import { SCREEN_KIND_TEMPLATE } from "@aglyn/aglyn/app-utils/screen-route";
19
19
  import { screenSeoStageKey, SCREEN_SEO_TEXT_FIELDS } from "@aglyn/aglyn/app-utils/screen-seo-fields";
20
20
  import { hasConcurrentWrite, versionStamp } from "@aglyn/aglyn/app-utils/guarded-version-save";
21
+ import { extractLayoutStyleOverrides, injectLayoutStyleOverrides } from "@aglyn/aglyn/app-utils/layout-style-overrides";
21
22
  import { decodeStoredNodes, encodeStoredNodes } from "@aglyn/aglyn/app-utils/stored-nodes";
22
23
  import { coeditMirrorRoomPath, VERSION_DRAFT_COLLECTION, VERSION_DRAFT_DOC_ID } from "@aglyn/aglyn/app-utils/version-draft";
23
24
  import { CanvasManager } from "@aglyn/aglyn/canvas-manager/canvas-manager";
24
25
  import { FEATURE_FLAG } from "@aglyn/aglyn/foundation/constants/shared";
25
26
  import { firebaseAdmin } from "@aglyn/tenant-data-admin/server/firebase-admin";
26
27
  import { resolveOrgIdForHost } from "@aglyn/tenant-data-admin/server/organizations";
28
+ import { measureNodeMap } from "@aglyn/aglyn/app-utils/measure-node-map";
27
29
  import { applyAssistEdit } from "../components/assist-edit-canvas.js";
28
- import { AI_EDIT_COMPONENT_LEFT_OUT, AI_EDIT_EMAIL_COPY, AI_EDIT_GONE_COPY, AI_EDIT_NO_NODES_COPY, AI_EDIT_NO_SITE_COPY, AI_EDIT_NOTHING_COPY, AI_EDIT_REFUSED_COPY, AI_EDIT_SEO_LEFT_OUT, AI_EDIT_STALE_COPY, AI_EDIT_VERSION_GONE_COPY, aiEditJobOutline, aiEditOutputNote, aiEditVersionName, parseAiEditJobInputs } from "../model/ai-edit-job.js";
30
+ import { AI_EDIT_COMPONENT_LEFT_OUT, AI_EDIT_EMAIL_COPY, AI_EDIT_GONE_COPY, AI_EDIT_NO_NODES_COPY, AI_EDIT_NO_SITE_COPY, AI_EDIT_NOTHING_COPY, AI_EDIT_REFUSED_COPY, AI_EDIT_SEO_LEFT_OUT, AI_EDIT_STALE_COPY, AI_EDIT_TOO_LARGE_COPY, AI_EDIT_VERSION_GONE_COPY, aiEditJobOutline, aiEditOutputNote, aiEditVersionName, parseAiEditJobInputs } from "../model/ai-edit-job.js";
29
31
  import { ASSIST_EDIT_TOOL_NAME } from "../model/assist-edit.js";
30
32
  import { AI_STEP_TIERS } from "../providers/catalog.js";
31
33
  import { AI_ROUTING_TABLE, aiModelForStep } from "../providers/routing.js";
@@ -220,6 +222,7 @@ export function aiJobEditModel() {
220
222
  * the version being written, which is never one a visitor sees; a page's
221
223
  * search fields are captured into `seo` only where `seo` is given.
222
224
  */ export function aiApplyEditToNodes(nodes, proposal, seo) {
225
+ var _applied_inserted;
223
226
  const canvas = aiEditCanvas(nodes);
224
227
  const fields = {};
225
228
  if (seo) {
@@ -240,7 +243,8 @@ export function aiJobEditModel() {
240
243
  };
241
244
  return {
242
245
  ok: true,
243
- nodes: canvas.toJSON().nodes
246
+ nodes: canvas.toJSON().nodes,
247
+ inserted: (_applied_inserted = applied.inserted) != null ? _applied_inserted : []
244
248
  };
245
249
  }
246
250
  const COLLECTIONS = {
@@ -274,7 +278,43 @@ function pendingScheduleVersion(doc) {
274
278
  aiEditJobId: text(draft.get('aiEditJobId')) || null
275
279
  };
276
280
  }
277
- /** A version's working draft document: the Besigner's, at the core's one address. */ function draftRef(versions, versionId) {
281
+ /**
282
+ * A version's own tree as the Besigner opens it (AGL-4235): the stored
283
+ * `nodes` with the version's layout style overrides parked on the root by the
284
+ * editor's own `injectLayoutStyleOverrides`. A draft the editor saves carries
285
+ * them there, so a draft built on this tree does too, and publishing it — from
286
+ * the editor or over the API — keeps them rather than clearing them. Null
287
+ * without a tree.
288
+ */ export function aiEditSavedTree(version) {
289
+ const nodes = decodeStoredNodes(version.get('nodes'));
290
+ return nodes ? injectLayoutStyleOverrides(nodes, version.get('layoutStyleOverrides')) : null;
291
+ }
292
+ /**
293
+ * The tree a change to a version is built on — the one rule, for the edit job,
294
+ * its write and a reader that shows a page as the next change will find it:
295
+ * the version's working draft when the Besigner would still offer it, so an
296
+ * author's work in progress is never thrown away; otherwise the version's own
297
+ * tree. `useDraft: false` is a change that lands somewhere other than the
298
+ * draft, built on the version alone. Null when neither has a tree.
299
+ */ export function aiEditBase(draft, version, options = {}) {
300
+ const usable = options.useDraft === false ? null : usableDraft(draft, version);
301
+ if (usable) return {
302
+ from: 'draft',
303
+ nodes: usable.nodes,
304
+ stored: draft.get('nodes'),
305
+ stampedBy: usable.aiEditJobId
306
+ };
307
+ // A usable draft's root says what its author meant, overrides or none, so
308
+ // only the saved tree gets them, as the editor gives them when it opens it.
309
+ const nodes = aiEditSavedTree(version);
310
+ return nodes ? {
311
+ from: 'saved',
312
+ nodes,
313
+ stored: version.get('nodes'),
314
+ stampedBy: null
315
+ } : null;
316
+ }
317
+ /** A version's working draft document: the Besigner's, at the core's one address. */ export function draftRef(versions, versionId) {
278
318
  return versions.doc(versionId).collection(VERSION_DRAFT_COLLECTION).doc(VERSION_DRAFT_DOC_ID);
279
319
  }
280
320
  /**
@@ -370,7 +410,9 @@ function pendingScheduleVersion(doc) {
370
410
  }
371
411
  });
372
412
  }
373
- const nodes = decodeStoredNodes(source.get('nodes'));
413
+ // The saved tree as the Besigner opens it, the page's layout style
414
+ // overrides on its root (see `aiEditSavedTree`).
415
+ const nodes = aiEditSavedTree(source);
374
416
  if (!nodes || !Object.keys(nodes).length) return {
375
417
  status: 400,
376
418
  error: AI_EDIT_NO_NODES_COPY
@@ -421,7 +463,6 @@ function pendingScheduleVersion(doc) {
421
463
  const newRef = versions.doc(input.jobVersionId);
422
464
  const sourceDraftRef = draftRef(versions, input.sourceVersionId);
423
465
  return firestore.runTransaction(async (tx)=>{
424
- var _ref;
425
466
  const [host, doc, source, already, draftDoc] = await Promise.all([
426
467
  tx.get(hostRef),
427
468
  tx.get(docRef),
@@ -456,8 +497,10 @@ function pendingScheduleVersion(doc) {
456
497
  });
457
498
  // Read again here, as the version is: an author who saved their draft,
458
499
  // or published it, since the read is what the change is built on now.
459
- const draft = placement === 'draft' ? usableDraft(draftDoc, source) : null;
460
- if ((draft == null ? void 0 : draft.aiEditJobId) === input.jobVersionId) {
500
+ const base = aiEditBase(draftDoc, source, {
501
+ useDraft: placement === 'draft'
502
+ });
503
+ if ((base == null ? void 0 : base.from) === 'draft' && base.stampedBy === input.jobVersionId) {
461
504
  return {
462
505
  ok: true,
463
506
  versionId: input.sourceVersionId,
@@ -465,17 +508,25 @@ function pendingScheduleVersion(doc) {
465
508
  seoWritten: false
466
509
  };
467
510
  }
468
- const nodes = (_ref = draft == null ? void 0 : draft.nodes) != null ? _ref : decodeStoredNodes(source.get('nodes'));
469
- if (!nodes) return {
511
+ if (!base) return {
470
512
  ok: false,
471
513
  error: AI_EDIT_NO_NODES_COPY
472
514
  };
515
+ const nodes = base.nodes;
516
+ const built = typeof input.proposal === 'function' ? input.proposal(_extends({}, base, {
517
+ pointerVersionId: pointer
518
+ })) : input.proposal;
519
+ if ('refused' in built) return {
520
+ ok: false,
521
+ error: AI_EDIT_REFUSED_COPY,
522
+ refused: built.refused
523
+ };
473
524
  const versionId = placement === 'draft' ? input.sourceVersionId : input.jobVersionId;
474
525
  const seo = {};
475
526
  const writeSeo = input.writeSeo && input.kind === 'screen' && !served;
476
- const proposal = _extends({}, input.proposal, {
477
- ops: writeSeo ? input.proposal.ops : input.proposal.ops.filter((op)=>op.op !== 'setSeo'),
478
- target: _extends({}, input.proposal.target, {
527
+ const proposal = _extends({}, built, {
528
+ ops: writeSeo ? built.ops : built.ops.filter((op)=>op.op !== 'setSeo'),
529
+ target: _extends({}, built.target, {
479
530
  versionId
480
531
  })
481
532
  });
@@ -484,7 +535,20 @@ function pendingScheduleVersion(doc) {
484
535
  ok: false,
485
536
  error: applied.reason === 'stale' ? AI_EDIT_STALE_COPY : AI_EDIT_REFUSED_COPY
486
537
  };
487
- const packed = encodeStoredNodes(applied.nodes);
538
+ if (input.maxNodeBytes !== undefined) {
539
+ const { bytes } = measureNodeMap(applied.nodes);
540
+ if (bytes > input.maxNodeBytes) return {
541
+ ok: false,
542
+ error: AI_EDIT_TOO_LARGE_COPY,
543
+ tooLarge: {
544
+ bytes
545
+ }
546
+ };
547
+ }
548
+ // A draft keeps the overrides on its root, as the editor's own draft
549
+ // does; a version stores them beside its tree, never in it, as the
550
+ // editor's save lifts them out (the copy below keeps the field).
551
+ const packed = encodeStoredNodes(placement === 'draft' ? applied.nodes : extractLayoutStyleOverrides(applied.nodes).nodes);
488
552
  if (!packed) return {
489
553
  ok: false,
490
554
  error: AI_EDIT_NO_NODES_COPY
@@ -494,14 +558,17 @@ function pendingScheduleVersion(doc) {
494
558
  if (placement === 'draft') {
495
559
  // The whole working tree, as the Besigner writes a draft, taken against
496
560
  // the version as stored so the Besigner offers it with Open draft.
497
- tx.set(sourceDraftRef, {
561
+ tx.set(sourceDraftRef, _extends({
498
562
  nodes: stored,
499
563
  baseStamp: versionStamp(source.get('updatedAt')),
500
564
  updatedByUid: input.uid,
501
- updatedByEmail: null,
565
+ updatedByEmail: null
566
+ }, input.updatedByLabel ? {
567
+ updatedByLabel: input.updatedByLabel
568
+ } : {}, {
502
569
  updatedAt: input.now,
503
570
  aiEditJobId: input.jobVersionId
504
- });
571
+ }));
505
572
  } else {
506
573
  // A copy of the version it started from — its layout binding, its
507
574
  // declared properties, everything the editor stored — with the changed
@@ -528,7 +595,11 @@ function pendingScheduleVersion(doc) {
528
595
  ok: true,
529
596
  versionId,
530
597
  placement,
531
- seoWritten: Object.keys(seo).length > 0
598
+ seoWritten: Object.keys(seo).length > 0,
599
+ written: {
600
+ stored,
601
+ inserted: applied.inserted
602
+ }
532
603
  };
533
604
  });
534
605
  }