@vectoriox/iox-ui 5.112.0 → 5.114.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/assist-core/index.d.ts +234 -7
- package/design-core/index.d.ts +80 -0
- package/fesm2022/vectoriox-iox-ui-assist-core.mjs +480 -75
- package/fesm2022/vectoriox-iox-ui-assist-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-design-core.mjs +94 -0
- package/fesm2022/vectoriox-iox-ui-design-core.mjs.map +1 -1
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs +61 -42
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs.map +1 -1
- package/generation-core/index.d.ts +7 -44
- package/package.json +1 -1
- package/src/assist-core/assist-facts.ts +36 -0
- package/src/assist-core/assist-turn.ts +28 -6
- package/src/assist-core/brief.ts +4 -0
- package/src/assist-core/field-edits.ts +14 -0
- package/src/assist-core/page-words.ts +503 -0
- package/src/assist-core/public-api.ts +5 -3
- package/src/assist-core/rich-text.ts +3 -0
- package/src/assist-core/surfaces.ts +4 -2
- package/src/design-core/flavor-spec.generated.ts +94 -0
- package/src/generation-core/compose-layout.ts +57 -18
- package/src/generation-core/plan-words.ts +8 -3
- package/src/generation-core/site-spec.generated.ts +1 -22
- package/styles/iox-design.css +39 -0
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import { jsonClosedObject, jsonListOf, JsonSchema, jsonText, SerializedNode, walkSerialized } from '@vectoriox/iox-ui/generation-core';
|
|
2
|
+
|
|
3
|
+
import { assistFactIssues, AssistWordContext } from './assist-facts';
|
|
4
|
+
import { namedLocale } from './assist-locale';
|
|
5
|
+
import { assertAssistBudget, MAX_ASSIST_PROPERTIES } from './budget';
|
|
6
|
+
import { AssistIssue, AssistIssueCode, Brief, BriefRules, HandleMap, NeverWrite, resolveHandle } from './brief';
|
|
7
|
+
import { containsHtml, escapeText, paragraphsToRichText, richTextParagraphs } from './rich-text';
|
|
8
|
+
import { clipText } from './summarize-item';
|
|
9
|
+
import { AssistScope, AssistSurface, EditFamily, SURFACES } from './surfaces';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Surface S1 (the builder, this page), family `words` (ai-assist-plan §4, ai-session-panel-plan §3b–§3d): the page as
|
|
13
|
+
* the model sees it — sections in order, each a handle (`s0`), the parts that carry words as handles (`s0.heading1`,
|
|
14
|
+
* `s2.button1`) with their kind and words — never a node id, a style value or the tree. The SAME pure functions run on
|
|
15
|
+
* the server (the brief it trusts, the check on a copy) and in the builder (the trial on the canvas):
|
|
16
|
+
* - `summarizePage` — the brief + the handle map (handle → node id + prop), sized by distance from the selection;
|
|
17
|
+
* - `wordsJsonSchema` — the strict format, one property per writable handle of the focus, ≤ 40 a request;
|
|
18
|
+
* - `applyWordEdits` — all or none on a COPY: unknown or bound handles, markup, an empty text, a word to avoid or a fact
|
|
19
|
+
* nobody gave are refused (re-asked once by the server, never repaired here).
|
|
20
|
+
*/
|
|
21
|
+
|
|
22
|
+
/** What a part is, as the owner and the model call it. */
|
|
23
|
+
export enum PagePartKind {
|
|
24
|
+
Heading = 'heading',
|
|
25
|
+
Text = 'text',
|
|
26
|
+
Button = 'button',
|
|
27
|
+
Link = 'link',
|
|
28
|
+
Image = 'image',
|
|
29
|
+
Field = 'field',
|
|
30
|
+
Slide = 'slide',
|
|
31
|
+
Video = 'video',
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
interface WordSlot {
|
|
35
|
+
readonly prop: string;
|
|
36
|
+
readonly kind: PagePartKind;
|
|
37
|
+
/** A second words prop of the same node (`s0.field1.placeholder`). */
|
|
38
|
+
readonly suffix?: string;
|
|
39
|
+
/** What these words are, for the model. */
|
|
40
|
+
readonly says: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Which saved prop holds a type's words (node baselines). One table — ARIA labels are left to the author. */
|
|
44
|
+
export const PAGE_WORD_SLOTS: Readonly<Record<string, ReadonlyArray<WordSlot>>> = {
|
|
45
|
+
Heading: [{ prop: 'content', kind: PagePartKind.Heading, says: 'a heading' }],
|
|
46
|
+
Text: [{ prop: 'content', kind: PagePartKind.Text, says: 'text' }],
|
|
47
|
+
Button: [{ prop: 'label', kind: PagePartKind.Button, says: 'a button\'s words' }],
|
|
48
|
+
SubmitButton: [{ prop: 'label', kind: PagePartKind.Button, says: 'a form\'s send button' }],
|
|
49
|
+
Link: [{ prop: 'label', kind: PagePartKind.Link, says: 'a link\'s words' }],
|
|
50
|
+
Field: [
|
|
51
|
+
{ prop: 'label', kind: PagePartKind.Field, says: 'a form field\'s label' },
|
|
52
|
+
{ prop: 'placeholder', kind: PagePartKind.Field, suffix: 'placeholder', says: 'the hint inside an empty form field' },
|
|
53
|
+
],
|
|
54
|
+
Image: [{ prop: 'alt', kind: PagePartKind.Image, says: 'a picture\'s description for people who cannot see it' }],
|
|
55
|
+
SliderSlide: [{ prop: 'caption', kind: PagePartKind.Slide, says: 'a slide\'s caption' }],
|
|
56
|
+
Video: [{ prop: 'title', kind: PagePartKind.Video, says: 'a video\'s title' }],
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/** A Text wearing this element style is a ButtonBlock's words — a button to the owner. */
|
|
60
|
+
const BUTTON_LABEL_STYLE = 'button-label';
|
|
61
|
+
|
|
62
|
+
/** Focus words are sent whole up to this; Around words are summaries (`AROUND_TEXT_CHARS`). */
|
|
63
|
+
export const FOCUS_TEXT_CHARS = 1200;
|
|
64
|
+
|
|
65
|
+
const BLOCK_MARKUP = /<(p|div|h[1-6]|li|blockquote|br)\b/i;
|
|
66
|
+
|
|
67
|
+
/** A stored words value written as paragraphs (rich) or one plain line. */
|
|
68
|
+
const isRich = (value: unknown): boolean => typeof value === 'string' && BLOCK_MARKUP.test(value);
|
|
69
|
+
|
|
70
|
+
/** A stored words value as the owner reads it. */
|
|
71
|
+
export function readableWords(value: unknown): string {
|
|
72
|
+
return richTextParagraphs(value).join('\n');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export interface PagePart {
|
|
76
|
+
readonly handle: string;
|
|
77
|
+
readonly kind: PagePartKind;
|
|
78
|
+
/** "Heading 1", "Button 2" — how the change card names it. */
|
|
79
|
+
readonly label: string;
|
|
80
|
+
readonly words: string;
|
|
81
|
+
/** Written as paragraphs (a list) rather than one line. */
|
|
82
|
+
readonly paragraphs?: boolean;
|
|
83
|
+
/** `Projects ▸ title`: bound words change with their item, never here. */
|
|
84
|
+
readonly bound?: string;
|
|
85
|
+
readonly style?: string;
|
|
86
|
+
readonly role?: string;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface PageSectionSummary {
|
|
90
|
+
readonly handle: string;
|
|
91
|
+
/** The section's own name ("Hero", "Our work") or "Section 3". */
|
|
92
|
+
readonly label: string;
|
|
93
|
+
readonly parts: ReadonlyArray<PagePart>;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export interface PageFocus {
|
|
97
|
+
readonly scope: AssistScope;
|
|
98
|
+
readonly page: { readonly name: string; readonly route: string };
|
|
99
|
+
readonly sections: ReadonlyArray<PageSectionSummary>;
|
|
100
|
+
/** The selection's handles (the node and the parts inside it) — "this". Empty on a page focus. */
|
|
101
|
+
readonly selected: ReadonlyArray<string>;
|
|
102
|
+
/** The selection as the owner sees it ("Hero ▸ Heading 1"). */
|
|
103
|
+
readonly selectedLabel?: string;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export interface PageAround {
|
|
107
|
+
/** The other sections, one line each. */
|
|
108
|
+
readonly sections: ReadonlyArray<{ readonly handle: string; readonly label: string; readonly headline: string }>;
|
|
109
|
+
/** The site's other pages, by name. */
|
|
110
|
+
readonly pages: ReadonlyArray<string>;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
export type PageBrief = Brief<PageFocus, PageAround>;
|
|
114
|
+
|
|
115
|
+
export interface PageBriefContext {
|
|
116
|
+
readonly locale: string;
|
|
117
|
+
readonly page: { readonly name: string; readonly route: string };
|
|
118
|
+
/** The selected node, when there is one. */
|
|
119
|
+
readonly selectedId?: string | null;
|
|
120
|
+
/** A rung wider than the selection (the chip's ✕). Default: node when something is selected, else page. */
|
|
121
|
+
readonly scope?: AssistScope;
|
|
122
|
+
readonly pages?: ReadonlyArray<string>;
|
|
123
|
+
readonly owner?: string | null;
|
|
124
|
+
readonly memory?: ReadonlyArray<string>;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export interface SummarizedPage {
|
|
128
|
+
readonly brief: PageBrief;
|
|
129
|
+
/** Every WRITABLE words handle of the page → its node id and prop. Bound parts are not in it. */
|
|
130
|
+
readonly handles: HandleMap;
|
|
131
|
+
/** The writable handles of the focus — what a words request carries. */
|
|
132
|
+
readonly focusHandles: ReadonlyArray<string>;
|
|
133
|
+
/** Every part of the page with its node id (for the client to outline a change) — never sent. */
|
|
134
|
+
readonly nodes: Readonly<Record<string, string>>;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const sectionHandle = (index: number): string => `s${index}`;
|
|
138
|
+
|
|
139
|
+
const capitalise = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
140
|
+
|
|
141
|
+
/** "Hero · ground" → "Hero"; a composed section's id label ("hero-1") reads as words. */
|
|
142
|
+
function sectionLabel(node: SerializedNode, index: number): string {
|
|
143
|
+
const own = (node.label ?? '').split(' · ')[0].trim();
|
|
144
|
+
return own ? capitalise(own.replace(/[-_]+/g, ' ')) : `Section ${index + 1}`;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function boundLabel(node: SerializedNode, prop: string): string | undefined {
|
|
148
|
+
const b = (node.bindings ?? []).find(x => x.prop === prop);
|
|
149
|
+
return b ? [b.source, b.path].filter(Boolean).join(' ▸ ') : undefined;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function slotsOf(node: SerializedNode): ReadonlyArray<WordSlot> {
|
|
153
|
+
const slots = PAGE_WORD_SLOTS[node.type] ?? [];
|
|
154
|
+
if (node.type === 'Text' && node.elementStyle === BUTTON_LABEL_STYLE) return slots.map(s => ({ ...s, kind: PagePartKind.Button, says: 'a button\'s words' }));
|
|
155
|
+
return slots;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
interface Walked {
|
|
159
|
+
readonly part: PagePart;
|
|
160
|
+
readonly section: number;
|
|
161
|
+
readonly nodeId: string;
|
|
162
|
+
readonly prop: string;
|
|
163
|
+
/** Node ids from the section root down to the node. */
|
|
164
|
+
readonly lineage: ReadonlyArray<string>;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** Every words part of the page, in reading order, with its handle. */
|
|
168
|
+
function walkParts(layout: ReadonlyArray<SerializedNode>): Walked[] {
|
|
169
|
+
const out: Walked[] = [];
|
|
170
|
+
layout.forEach((root, index) => {
|
|
171
|
+
const counts = new Map<PagePartKind, number>();
|
|
172
|
+
const visit = (node: SerializedNode, lineage: string[]) => {
|
|
173
|
+
for (const slot of slotsOf(node)) {
|
|
174
|
+
if (!slot.suffix) counts.set(slot.kind, (counts.get(slot.kind) ?? 0) + 1);
|
|
175
|
+
const n = counts.get(slot.kind) ?? 1;
|
|
176
|
+
const base = `${sectionHandle(index)}.${slot.kind}${n}`;
|
|
177
|
+
const value = node.props?.[slot.prop];
|
|
178
|
+
const bound = boundLabel(node, slot.prop);
|
|
179
|
+
const part: PagePart = {
|
|
180
|
+
handle: slot.suffix ? `${base}.${slot.suffix}` : base,
|
|
181
|
+
kind: slot.kind,
|
|
182
|
+
label: `${capitalise(slot.kind)} ${n}${slot.suffix ? ` · ${slot.suffix}` : ''}`,
|
|
183
|
+
words: readableWords(value),
|
|
184
|
+
...(isRich(value) ? { paragraphs: true } : {}),
|
|
185
|
+
...(bound ? { bound } : {}),
|
|
186
|
+
...(node.elementStyle ? { style: node.elementStyle } : {}),
|
|
187
|
+
...(node.role ? { role: node.role } : {}),
|
|
188
|
+
};
|
|
189
|
+
out.push({ part, section: index, nodeId: node.id, prop: slot.prop, lineage: [...lineage, node.id] });
|
|
190
|
+
}
|
|
191
|
+
for (const child of node.children ?? []) visit(child, [...lineage, node.id]);
|
|
192
|
+
};
|
|
193
|
+
visit(root, []);
|
|
194
|
+
});
|
|
195
|
+
return out;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const WIDENS: Readonly<Partial<Record<AssistScope, ReadonlyArray<AssistScope>>>> = {
|
|
199
|
+
[AssistScope.Node]: [AssistScope.Section, AssistScope.Page],
|
|
200
|
+
[AssistScope.Section]: [AssistScope.Page],
|
|
201
|
+
[AssistScope.Page]: [],
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
/** The section a node is in (its index), or -1. */
|
|
205
|
+
function sectionOf(layout: ReadonlyArray<SerializedNode>, id: string): number {
|
|
206
|
+
let found = -1;
|
|
207
|
+
layout.forEach((root, index) => walkSerialized([root], n => { if (n.id === id) found = index; }));
|
|
208
|
+
return found;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* The page as the model sees it. The selection decides the focus: a node → its section in full, its own parts as
|
|
213
|
+
* "this"; a section → that section; nothing (or the page rung) → every section. The other sections are one line each.
|
|
214
|
+
*/
|
|
215
|
+
export function summarizePage(layout: ReadonlyArray<SerializedNode>, ctx: PageBriefContext): SummarizedPage {
|
|
216
|
+
const walked = walkParts(layout);
|
|
217
|
+
const selectedSection = ctx.selectedId ? sectionOf(layout, ctx.selectedId) : -1;
|
|
218
|
+
const isSectionRoot = selectedSection >= 0 && layout[selectedSection].id === ctx.selectedId;
|
|
219
|
+
const asked = ctx.scope ?? (selectedSection < 0 ? AssistScope.Page : isSectionRoot ? AssistScope.Section : AssistScope.Node);
|
|
220
|
+
const scope = selectedSection < 0 ? AssistScope.Page : asked;
|
|
221
|
+
|
|
222
|
+
const inFocus = (w: Walked) => scope === AssistScope.Page || w.section === selectedSection;
|
|
223
|
+
const selectedParts = scope === AssistScope.Node ? walked.filter(w => w.lineage.includes(ctx.selectedId!)) : [];
|
|
224
|
+
|
|
225
|
+
const sections: PageSectionSummary[] = layout
|
|
226
|
+
.map((root, index) => ({ root, index }))
|
|
227
|
+
.filter(({ index }) => scope === AssistScope.Page || index === selectedSection)
|
|
228
|
+
.map(({ root, index }) => ({
|
|
229
|
+
handle: sectionHandle(index),
|
|
230
|
+
label: sectionLabel(root, index),
|
|
231
|
+
parts: walked.filter(w => w.section === index).map(w => ({ ...w.part, words: clipText(w.part.words, FOCUS_TEXT_CHARS) })),
|
|
232
|
+
}));
|
|
233
|
+
const around = layout
|
|
234
|
+
.map((root, index) => ({ root, index }))
|
|
235
|
+
.filter(({ index }) => scope !== AssistScope.Page && index !== selectedSection)
|
|
236
|
+
.map(({ root, index }) => {
|
|
237
|
+
const first = walked.find(w => w.section === index && w.part.words && (w.part.kind === PagePartKind.Heading || w.part.kind === PagePartKind.Text));
|
|
238
|
+
return { handle: sectionHandle(index), label: sectionLabel(root, index), headline: clipText(first?.part.words ?? '') };
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
const handles: Record<string, { key: string; path: string[]; id: string }> = {};
|
|
242
|
+
const nodes: Record<string, string> = {};
|
|
243
|
+
for (const w of walked) {
|
|
244
|
+
nodes[w.part.handle] = w.nodeId;
|
|
245
|
+
if (!w.part.bound) handles[w.part.handle] = { key: w.prop, path: [], id: w.nodeId };
|
|
246
|
+
}
|
|
247
|
+
layout.forEach((root, index) => { nodes[sectionHandle(index)] = root.id; });
|
|
248
|
+
|
|
249
|
+
const focusWalked = scope === AssistScope.Node && selectedParts.length ? selectedParts : walked.filter(inFocus);
|
|
250
|
+
const never: NeverWrite[] = focusWalked.filter(w => w.part.bound)
|
|
251
|
+
.map(w => ({ handle: w.part.handle, label: w.part.label, reason: `its words come from ${w.part.bound} — change them in that item` }));
|
|
252
|
+
const rules: BriefRules = {
|
|
253
|
+
families: SURFACES[AssistSurface.BuilderPage].families,
|
|
254
|
+
locale: ctx.locale,
|
|
255
|
+
never,
|
|
256
|
+
scope,
|
|
257
|
+
widensTo: WIDENS[scope] ?? [],
|
|
258
|
+
};
|
|
259
|
+
const selectedNode = selectedParts[0];
|
|
260
|
+
const focus: PageFocus = {
|
|
261
|
+
scope,
|
|
262
|
+
page: ctx.page,
|
|
263
|
+
sections,
|
|
264
|
+
selected: selectedParts.map(w => w.part.handle),
|
|
265
|
+
...(selectedNode ? { selectedLabel: `${sections[0]?.label ?? ''} ▸ ${selectedNode.part.label}` } : {}),
|
|
266
|
+
};
|
|
267
|
+
return {
|
|
268
|
+
brief: {
|
|
269
|
+
surface: AssistSurface.BuilderPage,
|
|
270
|
+
focus,
|
|
271
|
+
around: { sections: around, pages: [...(ctx.pages ?? [])] },
|
|
272
|
+
owner: ctx.owner ?? null,
|
|
273
|
+
rules,
|
|
274
|
+
memory: [...(ctx.memory ?? [])],
|
|
275
|
+
},
|
|
276
|
+
handles,
|
|
277
|
+
focusHandles: focusWalked.filter(w => !w.part.bound).map(w => w.part.handle),
|
|
278
|
+
nodes,
|
|
279
|
+
};
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export enum WordsAnswerKey {
|
|
283
|
+
Words = 'words',
|
|
284
|
+
Note = 'note',
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** How many words handles one request carries. */
|
|
288
|
+
export const WORDS_PER_PART = MAX_ASSIST_PROPERTIES - Object.keys(WordsAnswerKey).length;
|
|
289
|
+
|
|
290
|
+
export interface WordsSchemas {
|
|
291
|
+
readonly parts: JsonSchema[];
|
|
292
|
+
readonly handles: string[][];
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function split<T>(list: ReadonlyArray<T>, max: number): T[][] {
|
|
296
|
+
if (!list.length) return [];
|
|
297
|
+
const count = Math.ceil(list.length / max);
|
|
298
|
+
const size = Math.ceil(list.length / count);
|
|
299
|
+
return Array.from({ length: count }, (_, i) => list.slice(i * size, (i + 1) * size));
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function partSlot(part: PagePart, locale: string): JsonSchema {
|
|
303
|
+
const where = `${part.label}${part.style ? ` (${part.style})` : ''}`;
|
|
304
|
+
if (part.paragraphs) {
|
|
305
|
+
return { ...jsonListOf(jsonText()), description: `${where} — paragraphs of plain text in ${locale}; [] leaves it as it is.` };
|
|
306
|
+
}
|
|
307
|
+
return { ...jsonText(), description: `${where} — one line of plain text in ${locale}; '' leaves it as it is.` };
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** The strict format of family `words` for the focus: every writable handle answered (new words, or its leave value). */
|
|
311
|
+
export function wordsJsonSchema(summary: SummarizedPage): WordsSchemas {
|
|
312
|
+
const byHandle = new Map(summary.brief.focus.sections.flatMap(s => s.parts).map(p => [p.handle, p]));
|
|
313
|
+
const writable = summary.focusHandles.map(h => byHandle.get(h)).filter((p): p is PagePart => !!p);
|
|
314
|
+
const groups = split(writable, WORDS_PER_PART);
|
|
315
|
+
const parts = groups.map((g, i) => {
|
|
316
|
+
const which = groups.length > 1 ? ` (part ${i + 1} of ${groups.length})` : '';
|
|
317
|
+
return jsonClosedObject({
|
|
318
|
+
[WordsAnswerKey.Words]: jsonClosedObject(
|
|
319
|
+
Object.fromEntries(g.map(p => [p.handle, partSlot(p, summary.brief.rules.locale)])),
|
|
320
|
+
g.map(p => p.handle),
|
|
321
|
+
`Per part${which}: its new words, or its leave value.`,
|
|
322
|
+
),
|
|
323
|
+
[WordsAnswerKey.Note]: { ...jsonText(), description: 'One short line for the owner on what changed, or what needs them.' },
|
|
324
|
+
}, Object.values(WordsAnswerKey));
|
|
325
|
+
});
|
|
326
|
+
parts.forEach((p, i) => assertAssistBudget(`words part ${i + 1}`, [p]));
|
|
327
|
+
return { parts, handles: groups.map(g => g.map(p => p.handle)) };
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/** One part's answer as the model wrote it: a line, or paragraphs. */
|
|
331
|
+
export interface WordEdit {
|
|
332
|
+
readonly handle: string;
|
|
333
|
+
readonly value: string | ReadonlyArray<string>;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const isLeave = (v: unknown) => v === null || v === undefined || v === '' || (Array.isArray(v) && v.length === 0);
|
|
337
|
+
|
|
338
|
+
/** The parts' answers → the edits they make (every leave value dropped). */
|
|
339
|
+
export function decodeWordsAnswers(answers: ReadonlyArray<unknown>): WordEdit[] {
|
|
340
|
+
return answers.flatMap(answer => {
|
|
341
|
+
const words = answer && typeof answer === 'object' ? (answer as Record<string, unknown>)[WordsAnswerKey.Words] : undefined;
|
|
342
|
+
if (!words || typeof words !== 'object' || Array.isArray(words)) return [];
|
|
343
|
+
return Object.entries(words as Record<string, unknown>).filter(([, v]) => !isLeave(v))
|
|
344
|
+
.map(([handle, value]) => ({ handle, value: value as WordEdit['value'] }));
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
export interface WordChange {
|
|
349
|
+
readonly handle: string;
|
|
350
|
+
/** The node the change is on — the builder outlines it; never sent to the model. */
|
|
351
|
+
readonly nodeId: string;
|
|
352
|
+
readonly kind: PagePartKind;
|
|
353
|
+
readonly label: string;
|
|
354
|
+
readonly before: string;
|
|
355
|
+
readonly after: string;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
export interface WordEditContext {
|
|
359
|
+
readonly handles: HandleMap;
|
|
360
|
+
/** Handles the brief says are never written (bound words). */
|
|
361
|
+
readonly never?: ReadonlyArray<NeverWrite>;
|
|
362
|
+
/** The owner's sentence — a fact in it is theirs. */
|
|
363
|
+
readonly sentence?: string;
|
|
364
|
+
/** The owner's words to avoid and own facts (the business reading); none known → none allowed beyond the page's. */
|
|
365
|
+
readonly words?: AssistWordContext;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
export interface WordEditsOutcome {
|
|
369
|
+
readonly ok: boolean;
|
|
370
|
+
/** The layout with every change applied — the input untouched; a copy of it when refused. */
|
|
371
|
+
readonly result: SerializedNode[];
|
|
372
|
+
readonly changes: ReadonlyArray<WordChange>;
|
|
373
|
+
readonly issues: ReadonlyArray<AssistIssue>;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/** Every words value already on the page — its facts are the owner's (R-H2). */
|
|
377
|
+
function pageTexts(layout: ReadonlyArray<SerializedNode>): string[] {
|
|
378
|
+
const out: string[] = [];
|
|
379
|
+
walkSerialized(layout, node => {
|
|
380
|
+
for (const slot of slotsOf(node)) {
|
|
381
|
+
const text = readableWords(node.props?.[slot.prop]);
|
|
382
|
+
if (text) out.push(text);
|
|
383
|
+
}
|
|
384
|
+
});
|
|
385
|
+
return out;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function partLabel(node: SerializedNode, prop: string, handle: string): { kind: PagePartKind; label: string } {
|
|
389
|
+
const slot = slotsOf(node).find(s => s.prop === prop);
|
|
390
|
+
const kind = slot?.kind ?? PagePartKind.Text;
|
|
391
|
+
const tail = handle.split('.').slice(1).join(' ').replace(/^([a-z]+)(\d+)/, (_, k: string, n: string) => `${capitalise(k)} ${n}`);
|
|
392
|
+
return { kind, label: tail || capitalise(kind) };
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Family `words`, applied — the SAME check on the server (before a turn is shown) and in the builder (the trial). All
|
|
397
|
+
* or none; never mutates its input. A part written as paragraphs takes paragraphs (stored as escaped `<p>`), a line
|
|
398
|
+
* takes one line (stored escaped). An edit that changes nothing is dropped.
|
|
399
|
+
*/
|
|
400
|
+
export function applyWordEdits(layout: ReadonlyArray<SerializedNode>, edits: ReadonlyArray<WordEdit>, ctx: WordEditContext): WordEditsOutcome {
|
|
401
|
+
const result = JSON.parse(JSON.stringify(layout)) as SerializedNode[];
|
|
402
|
+
const byId = new Map<string, SerializedNode>();
|
|
403
|
+
walkSerialized(result, n => byId.set(n.id, n));
|
|
404
|
+
const never = new Map((ctx.never ?? []).map(n => [n.handle, n]));
|
|
405
|
+
const issues: AssistIssue[] = [];
|
|
406
|
+
const changes: WordChange[] = [];
|
|
407
|
+
const seen = new Set<string>();
|
|
408
|
+
const written: Array<{ handle: string; text: string }> = [];
|
|
409
|
+
|
|
410
|
+
for (const edit of edits) {
|
|
411
|
+
const refuse = (code: AssistIssueCode, message: string) => issues.push({ code, handle: edit.handle, message });
|
|
412
|
+
if (seen.has(edit.handle)) { refuse(AssistIssueCode.Duplicate, `"${edit.handle}" is answered twice`); continue; }
|
|
413
|
+
seen.add(edit.handle);
|
|
414
|
+
const kept = never.get(edit.handle);
|
|
415
|
+
if (kept) { refuse(AssistIssueCode.NeverWrite, `${kept.label}: ${kept.reason}`); continue; }
|
|
416
|
+
const resolved = resolveHandle(ctx.handles, edit.handle);
|
|
417
|
+
if ('issue' in resolved) { issues.push(resolved.issue); continue; }
|
|
418
|
+
const node = resolved.target.id ? byId.get(resolved.target.id) : undefined;
|
|
419
|
+
if (!node) { refuse(AssistIssueCode.UnknownHandle, `"${edit.handle}" is no longer on the page`); continue; }
|
|
420
|
+
const prop = resolved.target.key;
|
|
421
|
+
const before = node.props?.[prop];
|
|
422
|
+
const rich = isRich(before);
|
|
423
|
+
const lines = typeof edit.value === 'string' ? [edit.value] : Array.isArray(edit.value) ? [...edit.value] : null;
|
|
424
|
+
if (!lines || lines.some(l => typeof l !== 'string')) { refuse(AssistIssueCode.WrongType, rich ? 'expected paragraphs of text' : 'expected one line of text'); continue; }
|
|
425
|
+
const clean = lines.map(l => l.replace(/\s+/g, ' ').trim()).filter(Boolean);
|
|
426
|
+
if (!clean.length) { refuse(AssistIssueCode.WrongType, 'expected words — leave a part as it is with its leave value'); continue; }
|
|
427
|
+
if (clean.some(containsHtml)) { refuse(AssistIssueCode.HtmlNotAllowed, 'markup is not written by Assist'); continue; }
|
|
428
|
+
if (!rich && clean.length > 1) { refuse(AssistIssueCode.WrongType, 'this part is one line'); continue; }
|
|
429
|
+
const stored = rich ? paragraphsToRichText(clean) : escapeText(clean[0]);
|
|
430
|
+
const readable = clean.join('\n');
|
|
431
|
+
if (readable === readableWords(before)) continue;
|
|
432
|
+
written.push({ handle: edit.handle, text: readable });
|
|
433
|
+
node.props = { ...(node.props ?? {}), [prop]: stored };
|
|
434
|
+
changes.push({ handle: edit.handle, nodeId: node.id, ...partLabel(node, prop, edit.handle), before: readableWords(before), after: readable });
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
issues.push(...assistFactIssues(written, { words: ctx.words, sentence: ctx.sentence, existing: pageTexts(layout) }));
|
|
438
|
+
|
|
439
|
+
if (issues.length) return { ok: false, result: JSON.parse(JSON.stringify(layout)) as SerializedNode[], changes: [], issues };
|
|
440
|
+
return { ok: true, result, changes, issues: [] };
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
export enum PageSuggestionKind {
|
|
444
|
+
/** The selection's words, rewritten as the owner asks. */
|
|
445
|
+
Rewrite = 'rewrite',
|
|
446
|
+
Shorten = 'shorten',
|
|
447
|
+
FillEmpty = 'fillEmpty',
|
|
448
|
+
/** Pictures with no description for people who cannot see them. */
|
|
449
|
+
DescribePictures = 'describePictures',
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/** A quick action the Ask box offers, built from the brief (never canned text — the client words it). */
|
|
453
|
+
export interface PageSuggestion {
|
|
454
|
+
readonly kind: PageSuggestionKind;
|
|
455
|
+
readonly handles: ReadonlyArray<string>;
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** A heading this long, or a text this long, is worth a "Shorten". */
|
|
459
|
+
export const SHORTEN_HEADING_FROM_CHARS = 60;
|
|
460
|
+
export const SHORTEN_TEXT_FROM_CHARS = 280;
|
|
461
|
+
|
|
462
|
+
const tooLong = (p: PagePart) => p.words.length >= (p.kind === PagePartKind.Heading ? SHORTEN_HEADING_FROM_CHARS : SHORTEN_TEXT_FROM_CHARS);
|
|
463
|
+
|
|
464
|
+
/** S1's quick actions — each only when it applies to the focus. */
|
|
465
|
+
export function pageSuggestions(summary: SummarizedPage): PageSuggestion[] {
|
|
466
|
+
const writable = new Set(summary.focusHandles);
|
|
467
|
+
const parts = summary.brief.focus.sections.flatMap(s => s.parts).filter(p => writable.has(p.handle));
|
|
468
|
+
const pick = (test: (p: PagePart) => boolean) => parts.filter(test).map(p => p.handle);
|
|
469
|
+
const selected = summary.brief.focus.selected.filter(h => writable.has(h));
|
|
470
|
+
const long = pick(p => p.kind !== PagePartKind.Image && tooLong(p));
|
|
471
|
+
const empty = pick(p => p.kind !== PagePartKind.Image && !p.words);
|
|
472
|
+
const pictures = pick(p => p.kind === PagePartKind.Image && !p.words);
|
|
473
|
+
return [
|
|
474
|
+
...(selected.length ? [{ kind: PageSuggestionKind.Rewrite, handles: selected }] : []),
|
|
475
|
+
...(long.length ? [{ kind: PageSuggestionKind.Shorten, handles: long }] : []),
|
|
476
|
+
...(empty.length ? [{ kind: PageSuggestionKind.FillEmpty, handles: empty }] : []),
|
|
477
|
+
...(pictures.length ? [{ kind: PageSuggestionKind.DescribePictures, handles: pictures }] : []),
|
|
478
|
+
];
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const HEBREW_LETTER = /[-]/g;
|
|
482
|
+
const LATIN_LETTER = /[A-Za-zÀ-ɏ]/g;
|
|
483
|
+
const LOCALE_SHAPE = /^[A-Za-z]{2,3}([-_][A-Za-z0-9]{2,8})?$/;
|
|
484
|
+
const HEBREW = 'he';
|
|
485
|
+
const ENGLISH = 'en';
|
|
486
|
+
|
|
487
|
+
/**
|
|
488
|
+
* The ONE locale a page turn writes in (ai-session-panel-plan §3h) — the server's and the builder's: a language the
|
|
489
|
+
* sentence names ("translate this to English") → the page's own locale → the script its words are written in (a Hebrew
|
|
490
|
+
* page edited in English still gets Hebrew words) → the admin's language.
|
|
491
|
+
*/
|
|
492
|
+
export function pageWriteLocale(layout: ReadonlyArray<SerializedNode>, ctx: { readonly sentence: string; readonly stored?: string | null; readonly uiLang: string }): string {
|
|
493
|
+
const stored = ctx.stored && LOCALE_SHAPE.test(ctx.stored) ? ctx.stored : null;
|
|
494
|
+
const named = namedLocale(ctx.sentence, [...new Set([...(stored ? [stored] : []), ENGLISH, HEBREW])]);
|
|
495
|
+
if (named) return named;
|
|
496
|
+
if (stored) return stored;
|
|
497
|
+
const words = pageTexts(layout).join(' ');
|
|
498
|
+
const hebrew = (words.match(HEBREW_LETTER) ?? []).length;
|
|
499
|
+
const latin = (words.match(LATIN_LETTER) ?? []).length;
|
|
500
|
+
if (hebrew > latin) return HEBREW;
|
|
501
|
+
if (latin > hebrew) return ctx.uiLang === HEBREW ? ENGLISH : ctx.uiLang;
|
|
502
|
+
return ctx.uiLang;
|
|
503
|
+
}
|
|
@@ -3,12 +3,12 @@
|
|
|
3
3
|
* `architecture/ai/ai-assist-plan.md` §3b): the surfaces and their specs, the brief and its handle map, the route
|
|
4
4
|
* request, and family `fields` for surface S2 (one content item) — its brief (`summarizeItem`), its strict output format
|
|
5
5
|
* (`fieldsJsonSchema`) and the ONE check that applies an answer (`applyFieldEdits`), run by iox-cms on a copy and by
|
|
6
|
-
* cms-client as the trial
|
|
6
|
+
* cms-client as the trial — and family `words` for surface S1 (the builder, this page): `summarizePage`, `wordsJsonSchema`,
|
|
7
|
+
* `applyWordEdits`, `pageSuggestions`. Every family's words pass ONE fact gate (`assistFactIssues` over generation-core's
|
|
8
|
+
* `factIssues`, R-H2). Pure: no transport, no clock, no randomness.
|
|
7
9
|
* ⚠️ Not re-exported from the main entry. Consumers wire the usual trio — ng-package raw-ts assets, jest
|
|
8
10
|
* moduleNameMapper, tsconfig paths — or `nest start` dies with ERR_REQUIRE_ESM. It imports generation-core (the strict
|
|
9
11
|
* schema builders and budget), so a consumer maps both.
|
|
10
|
-
* TODO(after the 2026-11-19 demo): extract the fact gate `factIssues` out of generation-core's `planWordIssues` into
|
|
11
|
-
* this entry (plan §3b, R-H2) — `planWordIssues` becomes its caller; deferred because it edits generation-core.
|
|
12
12
|
*/
|
|
13
13
|
export * from './surfaces';
|
|
14
14
|
export * from './budget';
|
|
@@ -19,6 +19,8 @@ export * from './field-values';
|
|
|
19
19
|
export * from './summarize-item';
|
|
20
20
|
export * from './fields-schema';
|
|
21
21
|
export * from './route-schema';
|
|
22
|
+
export * from './assist-facts';
|
|
22
23
|
export * from './field-edits';
|
|
24
|
+
export * from './page-words';
|
|
23
25
|
export * from './assist-turn';
|
|
24
26
|
export * from './assist-locale';
|
|
@@ -23,6 +23,9 @@ export function richTextParagraphs(value: unknown): string[] {
|
|
|
23
23
|
.filter(Boolean);
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
/** One line of plain text as stored HTML: every character escaped (a heading's or a button's words). */
|
|
27
|
+
export const escapeText = (text: string): string => escape(text);
|
|
28
|
+
|
|
26
29
|
/** Paragraphs as the editor's HTML: one `<p>` each, escaped. */
|
|
27
30
|
export function paragraphsToRichText(paragraphs: ReadonlyArray<string>): string {
|
|
28
31
|
return paragraphs.map(p => `<p>${escape(p.trim())}</p>`).join('');
|
|
@@ -116,14 +116,16 @@ const spec = (s: Omit<SurfaceSpec, 'perContentType' | 'readsPhotos'> & Partial<P
|
|
|
116
116
|
});
|
|
117
117
|
|
|
118
118
|
export const SURFACES: Readonly<Record<AssistSurface, SurfaceSpec>> = {
|
|
119
|
+
// S1 offers only the families the server answers (the route can never pick one it cannot): Words first (AI2a);
|
|
120
|
+
// Style, Sections and Look & motion join as each slice ships (ai-assist-plan §6, AI2).
|
|
119
121
|
[AssistSurface.BuilderPage]: spec({
|
|
120
122
|
surface: AssistSurface.BuilderPage,
|
|
121
|
-
families: [EditFamily.Words
|
|
123
|
+
families: [EditFamily.Words],
|
|
122
124
|
commit: CommitKind.BuilderStep,
|
|
123
125
|
steps: [AssistStep.AssistRoute, AssistStep.AssistEdit],
|
|
124
126
|
permissions: [AssistPermission.PageUpdate],
|
|
125
127
|
scopes: [AssistScope.Node, AssistScope.Section, AssistScope.Page],
|
|
126
|
-
status: SurfaceStatus.
|
|
128
|
+
status: SurfaceStatus.Built,
|
|
127
129
|
}),
|
|
128
130
|
[AssistSurface.ContentItem]: spec({
|
|
129
131
|
surface: AssistSurface.ContentItem,
|