@vectoriox/iox-ui 5.117.0 → 5.118.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 +169 -5
- package/fesm2022/vectoriox-iox-ui-assist-core.mjs +309 -14
- package/fesm2022/vectoriox-iox-ui-assist-core.mjs.map +1 -1
- package/package.json +1 -1
- package/src/assist-core/assist-turn.ts +7 -4
- package/src/assist-core/page-edits.ts +39 -0
- package/src/assist-core/page-style.ts +346 -0
- package/src/assist-core/page-words.ts +37 -9
- package/src/assist-core/public-api.ts +5 -2
- package/src/assist-core/surfaces.ts +1 -1
package/package.json
CHANGED
|
@@ -4,6 +4,7 @@ import type { SerializedNode } from '@vectoriox/iox-ui/generation-core';
|
|
|
4
4
|
|
|
5
5
|
import { FieldChange } from './field-edits';
|
|
6
6
|
import { FieldEdit } from './fields-schema';
|
|
7
|
+
import { PageChangeOp, SchemeEdit, StyleChange, StyleEdit } from './page-style';
|
|
7
8
|
import { WordChange, WordEdit } from './page-words';
|
|
8
9
|
import { ContentRecord, clipText } from './summarize-item';
|
|
9
10
|
import { AssistScope, AssistSurface, CommitKind, EditFamily, SURFACES, SurfaceStatus } from './surfaces';
|
|
@@ -63,11 +64,13 @@ export interface AssistTurnRequest {
|
|
|
63
64
|
readonly page?: { readonly name: string; readonly route: string };
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
/** What a turn proposes, per family: a field's value (`fields`)
|
|
67
|
-
export type AssistEdit = FieldEdit | WordEdit;
|
|
67
|
+
/** What a turn proposes, per family: a field's value (`fields`), a part's words (`words`), an element's style or a section's scheme (`style`). */
|
|
68
|
+
export type AssistEdit = FieldEdit | WordEdit | StyleEdit | SchemeEdit;
|
|
68
69
|
|
|
69
70
|
/** What a turn changed, as the card says it. */
|
|
70
|
-
export type AssistChange = FieldChange | WordChange;
|
|
71
|
+
export type AssistChange = FieldChange | WordChange | StyleChange;
|
|
72
|
+
|
|
73
|
+
const isLookChange = (c: AssistChange): c is StyleChange => 'op' in c && (c.op === PageChangeOp.Style || c.op === PageChangeOp.Scheme);
|
|
71
74
|
|
|
72
75
|
/** POST's 202 answer. */
|
|
73
76
|
export interface AssistTurnAccepted {
|
|
@@ -184,7 +187,7 @@ const SENTENCE_IN_MEMORY_CHARS = 80;
|
|
|
184
187
|
|
|
185
188
|
function outcome(turn: AssistTurn): string {
|
|
186
189
|
const named = (list: ReadonlyArray<AssistChange> | undefined) =>
|
|
187
|
-
(list ?? []).map(c => ('locale' in c && c.locale ? `${c.label} (${c.locale})` : c.label)).join(', ');
|
|
190
|
+
(list ?? []).map(c => (isLookChange(c) ? `${c.label} → ${c.after}` : 'locale' in c && c.locale ? `${c.label} (${c.locale})` : c.label)).join(', ');
|
|
188
191
|
switch (turn.status) {
|
|
189
192
|
case AssistTurnStatus.Refused: return `refused: ${turn.refusal?.message ?? ''}`.trim();
|
|
190
193
|
case AssistTurnStatus.Failed: return 'failed';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { SerializedNode } from '@vectoriox/iox-ui/generation-core';
|
|
2
|
+
|
|
3
|
+
import { AssistIssue } from './brief';
|
|
4
|
+
import { applyWordEdits, SummarizedPage, WordChange, WordEdit, WordEditContext } from './page-words';
|
|
5
|
+
import { applyStyleEdits, BaselineStyles, isSchemeEdit, isStyleEdit, SchemeEdit, StyleChange, StyleEdit } from './page-style';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Surface S1's answer as ONE thing (AI2b): the edits of every family a page turn routed to — words (`{ handle, value }`),
|
|
9
|
+
* styles (`{ handle, style }`), schemes (`{ handle, scheme }`) — applied in that order on a copy, all or none. The server
|
|
10
|
+
* checks each family as it is asked; the builder's trial applies the turn's edits through this, so both read one rule.
|
|
11
|
+
*/
|
|
12
|
+
export type PageEdit = WordEdit | StyleEdit | SchemeEdit;
|
|
13
|
+
export type PageChange = WordChange | StyleChange;
|
|
14
|
+
|
|
15
|
+
export interface PageEditContext extends Omit<WordEditContext, 'handles' | 'never'> {
|
|
16
|
+
readonly summary: Pick<SummarizedPage, 'handles' | 'looks'> & { readonly brief: { readonly rules: { readonly never: WordEditContext['never'] } } };
|
|
17
|
+
/** The builder's registry baseline; default the saved shape's (`serializedBaselineStyles`). */
|
|
18
|
+
readonly baseline?: BaselineStyles;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface PageEditsOutcome {
|
|
22
|
+
readonly ok: boolean;
|
|
23
|
+
readonly result: SerializedNode[];
|
|
24
|
+
readonly changes: ReadonlyArray<PageChange>;
|
|
25
|
+
readonly issues: ReadonlyArray<AssistIssue>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const isWordEdit = (e: PageEdit): e is WordEdit => 'value' in e;
|
|
29
|
+
|
|
30
|
+
export function applyPageEdits(layout: ReadonlyArray<SerializedNode>, edits: ReadonlyArray<PageEdit>, ctx: PageEditContext): PageEditsOutcome {
|
|
31
|
+
const words = applyWordEdits(layout, edits.filter(isWordEdit), {
|
|
32
|
+
handles: ctx.summary.handles, never: ctx.summary.brief.rules.never, sentence: ctx.sentence, words: ctx.words,
|
|
33
|
+
});
|
|
34
|
+
const looks = edits.filter((e): e is StyleEdit | SchemeEdit => !isWordEdit(e) && (isStyleEdit(e) || isSchemeEdit(e)));
|
|
35
|
+
const style = applyStyleEdits(words.result, looks, { looks: ctx.summary.looks, baseline: ctx.baseline });
|
|
36
|
+
const issues = [...words.issues, ...style.issues];
|
|
37
|
+
if (issues.length) return { ok: false, result: JSON.parse(JSON.stringify(layout)) as SerializedNode[], changes: [], issues };
|
|
38
|
+
return { ok: true, result: style.result, changes: [...words.changes, ...style.changes], issues: [] };
|
|
39
|
+
}
|
|
@@ -0,0 +1,346 @@
|
|
|
1
|
+
import {
|
|
2
|
+
COLOR_SCHEME_TYPES, COLOR_SCHEMES, ColorScheme, colorSchemeOf, elementStyleById, elementStylesFor, ElementStyleTarget, NEW_NODE_STYLE_DEFAULTS,
|
|
3
|
+
setColorScheme, switchElementStyle,
|
|
4
|
+
} from '@vectoriox/iox-ui/design-core';
|
|
5
|
+
import { jsonClosedObject, jsonListOf, jsonOneOf, JsonSchema, jsonText, NODE_BASELINES, SerializedNode, walkSerialized } from '@vectoriox/iox-ui/generation-core';
|
|
6
|
+
|
|
7
|
+
import { assertAssistBudget } from './budget';
|
|
8
|
+
import { AssistIssue, AssistIssueCode } from './brief';
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Surface S1 (the builder, this page), family `style` (ai-assist-plan §4, AI2b; ai-session-panel-plan `setStyle` /
|
|
12
|
+
* `setScheme`): an element takes ANOTHER of the look's element styles, a section (or a box) another colour scheme — never
|
|
13
|
+
* a CSS value, a colour or a size. What a node may take is a closed list: the other members of the swap set its current
|
|
14
|
+
* style is in that its type may wear ({@link ASK_STYLE_SETS}). The SAME pure functions run on the server (the check on a
|
|
15
|
+
* copy) and in the builder (the trial): `pageLooks` (the targets and their handles), `styleJsonSchema`,
|
|
16
|
+
* `decodeStyleAnswers`, `applyStyleEdits` — design-core's `switchElementStyle` (the author's own changes are kept) and
|
|
17
|
+
* `setColorScheme` (the ONE scheme writer), all or none.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The styles an owner may swap between by asking — each set is ONE kind of thing (a button, a heading, words, a box, a
|
|
22
|
+
* badge). Every other element style (a tab, an accordion part, a cart button, the navigation bar) belongs to the recipe
|
|
23
|
+
* that stamped it and is never offered. A node wearing a style is offered the union of the sets that style is in, kept
|
|
24
|
+
* to the styles its type may wear (design-core `elementStylesFor`).
|
|
25
|
+
*/
|
|
26
|
+
export const ASK_STYLE_SETS: ReadonlyArray<ReadonlyArray<string>> = [
|
|
27
|
+
['button-primary', 'button-secondary', 'button-ghost'],
|
|
28
|
+
['display', 'h1', 'h2', 'h3', 'eyebrow'],
|
|
29
|
+
['lead', 'body', 'small', 'eyebrow'],
|
|
30
|
+
['section', 'box', 'card', 'panel'],
|
|
31
|
+
['badge-neutral', 'badge-success', 'badge-warning', 'badge-accent'],
|
|
32
|
+
];
|
|
33
|
+
|
|
34
|
+
/** A box that only holds things — offered a style only when it is the selection ("make this a card"). */
|
|
35
|
+
const PLAIN_BOX_STYLE = 'box';
|
|
36
|
+
|
|
37
|
+
/** A box wearing one of these paints its own ground — a colour scheme shows on it. */
|
|
38
|
+
const SURFACE_STYLES: ReadonlySet<string> = new Set(['section', 'card', 'panel']);
|
|
39
|
+
|
|
40
|
+
const paintsGround = (style: string | null | undefined): boolean => SURFACE_STYLES.has(style ?? '');
|
|
41
|
+
|
|
42
|
+
/** A Text wearing this is a ButtonBlock's words: the button is the style target. */
|
|
43
|
+
const BUTTON_LABEL_STYLE = 'button-label';
|
|
44
|
+
|
|
45
|
+
/** The styles a node may take by asking — its own among them — or [] when it is not a target. */
|
|
46
|
+
export function askableStyles(node: Pick<SerializedNode, 'type' | 'elementStyle'>): string[] {
|
|
47
|
+
const current = node.elementStyle;
|
|
48
|
+
if (!current) return [];
|
|
49
|
+
const wears = new Set(elementStylesFor(node.type).map(s => s.id));
|
|
50
|
+
const offered = new Set(ASK_STYLE_SETS.filter(set => set.includes(current)).flat().filter(id => wears.has(id)));
|
|
51
|
+
return offered.size > 1 ? [...offered] : [];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** A target of family `style` — what the brief says, and what the check needs (the node id is never sent). */
|
|
55
|
+
export interface PageLook {
|
|
56
|
+
readonly handle: string;
|
|
57
|
+
/** "Hero" (a section root), "Box 2", "Heading 1" — how the change card names it. */
|
|
58
|
+
readonly label: string;
|
|
59
|
+
/** The element style it wears, and the styles it may take (its own among them); absent when only its scheme is offered. */
|
|
60
|
+
readonly style?: string;
|
|
61
|
+
readonly styles?: ReadonlyArray<string>;
|
|
62
|
+
/** Its colour scheme, when it takes one. */
|
|
63
|
+
readonly scheme?: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface WalkedLook {
|
|
67
|
+
readonly look: PageLook;
|
|
68
|
+
readonly section: number;
|
|
69
|
+
readonly nodeId: string;
|
|
70
|
+
readonly type: string;
|
|
71
|
+
readonly lineage: ReadonlyArray<string>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const isRecipePart = (node: SerializedNode) => !!node.role && node.role.includes('.');
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Every style / scheme target of the page in reading order. A node that carries words keeps its words handle
|
|
78
|
+
* (`wordHandles`: node id → `s0.heading1`); a ButtonBlock is named by its words' handle (`s0.button1`); a section root is
|
|
79
|
+
* `s{i}`; any other box is `s{i}.box{n}`. `selectedId` lets a plain box that is the selection be a target.
|
|
80
|
+
*/
|
|
81
|
+
export function pageLooks(
|
|
82
|
+
layout: ReadonlyArray<SerializedNode>, wordHandles: ReadonlyMap<string, string>, selectedId?: string | null,
|
|
83
|
+
): WalkedLook[] {
|
|
84
|
+
const out: WalkedLook[] = [];
|
|
85
|
+
layout.forEach((root, index) => {
|
|
86
|
+
let boxes = 0;
|
|
87
|
+
const visit = (node: SerializedNode, lineage: string[]) => {
|
|
88
|
+
const here = [...lineage, node.id];
|
|
89
|
+
const styles = askableStyles(node);
|
|
90
|
+
const isRoot = node === root;
|
|
91
|
+
const plain = node.elementStyle === PLAIN_BOX_STYLE && !isRoot && node.id !== selectedId;
|
|
92
|
+
const takesStyle = styles.length > 0 && !plain && !(isRecipePart(node) && !wordHandles.has(node.id) && node.type !== 'ButtonBlock');
|
|
93
|
+
const scheme = colorSchemeOf(node as never) ?? ColorScheme.Default;
|
|
94
|
+
// A scheme re-declares tokens: only a node that paints a ground of its own (a section, card or panel style) shows
|
|
95
|
+
// it — on any other the words take the scheme's ink over the page's ground (white on white). A node that may
|
|
96
|
+
// TAKE such a style is offered one too; the check refuses the scheme unless the turn gives it that style.
|
|
97
|
+
const takesScheme = COLOR_SCHEME_TYPES.includes(node.type) && !isRecipePart(node)
|
|
98
|
+
&& (paintsGround(node.elementStyle) || scheme !== ColorScheme.Default || (takesStyle && styles.some(paintsGround)));
|
|
99
|
+
if (takesStyle || takesScheme) {
|
|
100
|
+
const handle = lookHandle(node, index, isRoot, wordHandles, () => ++boxes);
|
|
101
|
+
if (handle) {
|
|
102
|
+
out.push({
|
|
103
|
+
look: {
|
|
104
|
+
handle,
|
|
105
|
+
label: lookLabel(handle, root, index),
|
|
106
|
+
...(takesStyle ? { style: node.elementStyle!, styles } : {}),
|
|
107
|
+
...(takesScheme ? { scheme } : {}),
|
|
108
|
+
},
|
|
109
|
+
section: index, nodeId: node.id, type: node.type, lineage: here,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
for (const child of node.children ?? []) visit(child, here);
|
|
114
|
+
};
|
|
115
|
+
visit(root, []);
|
|
116
|
+
});
|
|
117
|
+
return out;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function lookHandle(node: SerializedNode, section: number, isRoot: boolean, wordHandles: ReadonlyMap<string, string>, nextBox: () => number): string | null {
|
|
121
|
+
if (isRoot) return `s${section}`;
|
|
122
|
+
const own = wordHandles.get(node.id);
|
|
123
|
+
if (own) return own;
|
|
124
|
+
if (node.type === 'ButtonBlock') {
|
|
125
|
+
const label = (node.children ?? []).find(c => c.type === 'Text' && c.elementStyle === BUTTON_LABEL_STYLE);
|
|
126
|
+
return label ? wordHandles.get(label.id) ?? null : null;
|
|
127
|
+
}
|
|
128
|
+
return `s${section}.box${nextBox()}`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const capitalise = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
132
|
+
|
|
133
|
+
/** "Hero · ground" → "Hero"; a composed section's id label ("hero-1") reads as words; else "Section 3". */
|
|
134
|
+
export function sectionLabel(node: SerializedNode, index: number): string {
|
|
135
|
+
const own = (node.label ?? '').split(' · ')[0].trim();
|
|
136
|
+
return own ? capitalise(own.replace(/[-_]+/g, ' ')) : `Section ${index + 1}`;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** `s0` → the section's own name; `s0.box2` → "Box 2"; `s1.heading1` → "Heading 1". */
|
|
140
|
+
function lookLabel(handle: string, root: SerializedNode, index: number): string {
|
|
141
|
+
const tail = handle.split('.').slice(1).join(' ');
|
|
142
|
+
if (!tail) return sectionLabel(root, index);
|
|
143
|
+
return tail.replace(/^([a-z]+)(\d+)/, (_, k: string, n: string) => `${capitalise(k)} ${n}`);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/** The labels of the styles and schemes a brief mentions — the model reads ids, the owner reads these. */
|
|
147
|
+
export function lookNames(looks: ReadonlyArray<PageLook>): { styles: Record<string, string>; schemes: Record<string, string> } {
|
|
148
|
+
const styles: Record<string, string> = {};
|
|
149
|
+
for (const look of looks) for (const id of look.styles ?? []) styles[id] = elementStyleById(id)?.label ?? id;
|
|
150
|
+
const schemes: Record<string, string> = looks.some(l => l.scheme) ? Object.fromEntries(COLOR_SCHEMES.map(s => [s.id, s.label])) : {};
|
|
151
|
+
return { styles, schemes };
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** A focus target the check may write: its node and what it takes. */
|
|
155
|
+
export interface LookTarget {
|
|
156
|
+
readonly id: string;
|
|
157
|
+
readonly type: string;
|
|
158
|
+
readonly label: string;
|
|
159
|
+
readonly styles: ReadonlyArray<string>;
|
|
160
|
+
readonly scheme: boolean;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export type LookMap = Readonly<Record<string, LookTarget>>;
|
|
164
|
+
|
|
165
|
+
export enum StyleAnswerKey {
|
|
166
|
+
Styles = 'styles',
|
|
167
|
+
Schemes = 'schemes',
|
|
168
|
+
Note = 'note',
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export interface StyleSchemas {
|
|
172
|
+
readonly parts: JsonSchema[];
|
|
173
|
+
readonly handles: string[][];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* The strict format of family `style` for the focus — ONE request: a list of `{ handle, style }` and a list of
|
|
178
|
+
* `{ handle, scheme }`, each handle an enum of the focus's targets ([] leaves everything as it is). A list whose enum
|
|
179
|
+
* would be empty is left out. The pairing (does THIS handle take THAT style) is the check's, so the format stays small
|
|
180
|
+
* whatever the page holds.
|
|
181
|
+
*/
|
|
182
|
+
export function styleJsonSchema(looks: LookMap): StyleSchemas {
|
|
183
|
+
const styled = Object.entries(looks).filter(([, t]) => t.styles.length);
|
|
184
|
+
const schemed = Object.entries(looks).filter(([, t]) => t.scheme);
|
|
185
|
+
if (!styled.length && !schemed.length) return { parts: [], handles: [] };
|
|
186
|
+
const properties: Record<string, JsonSchema> = {};
|
|
187
|
+
if (styled.length) {
|
|
188
|
+
const ids = [...new Set(styled.flatMap(([, t]) => t.styles))];
|
|
189
|
+
properties[StyleAnswerKey.Styles] = {
|
|
190
|
+
...jsonListOf(jsonClosedObject({
|
|
191
|
+
handle: jsonOneOf(styled.map(([h]) => h)),
|
|
192
|
+
style: jsonOneOf(ids),
|
|
193
|
+
}, ['handle', 'style'])),
|
|
194
|
+
description: 'An element taking another of the styles the focus lists for it; [] changes no style.',
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
if (schemed.length) {
|
|
198
|
+
properties[StyleAnswerKey.Schemes] = {
|
|
199
|
+
...jsonListOf(jsonClosedObject({
|
|
200
|
+
handle: jsonOneOf(schemed.map(([h]) => h)),
|
|
201
|
+
scheme: jsonOneOf(COLOR_SCHEMES.map(s => s.id)),
|
|
202
|
+
}, ['handle', 'scheme'])),
|
|
203
|
+
description: 'A section or box taking another colour scheme; [] changes no colours.',
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
properties[StyleAnswerKey.Note] = { ...jsonText(), description: 'One short line for the owner on what changed, or what needs them.' };
|
|
207
|
+
const part = jsonClosedObject(properties, Object.keys(properties));
|
|
208
|
+
assertAssistBudget('style', [part]);
|
|
209
|
+
return { parts: [part], handles: [[...new Set([...styled, ...schemed].map(([h]) => h))]] };
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** An element taking another element style. */
|
|
213
|
+
export interface StyleEdit {
|
|
214
|
+
readonly handle: string;
|
|
215
|
+
readonly style: string;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/** A section or box taking another colour scheme. */
|
|
219
|
+
export interface SchemeEdit {
|
|
220
|
+
readonly handle: string;
|
|
221
|
+
readonly scheme: string;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const listOf = (answer: unknown, key: StyleAnswerKey): Array<Record<string, unknown>> => {
|
|
225
|
+
const list = answer && typeof answer === 'object' ? (answer as Record<string, unknown>)[key] : undefined;
|
|
226
|
+
return Array.isArray(list) ? list.filter((x): x is Record<string, unknown> => !!x && typeof x === 'object') : [];
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
/** The answer → the edits it makes. */
|
|
230
|
+
export function decodeStyleAnswers(answers: ReadonlyArray<unknown>): Array<StyleEdit | SchemeEdit> {
|
|
231
|
+
return answers.flatMap(answer => [
|
|
232
|
+
...listOf(answer, StyleAnswerKey.Styles).map(x => ({ handle: String(x['handle'] ?? ''), style: String(x['style'] ?? '') })),
|
|
233
|
+
...listOf(answer, StyleAnswerKey.Schemes).map(x => ({ handle: String(x['handle'] ?? ''), scheme: String(x['scheme'] ?? '') })),
|
|
234
|
+
]);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export const isStyleEdit = (e: object): e is StyleEdit => typeof (e as StyleEdit).style === 'string';
|
|
238
|
+
export const isSchemeEdit = (e: object): e is SchemeEdit => typeof (e as SchemeEdit).scheme === 'string';
|
|
239
|
+
|
|
240
|
+
/** Which kind of change a change card shows. */
|
|
241
|
+
export enum PageChangeOp {
|
|
242
|
+
Words = 'words',
|
|
243
|
+
Style = 'style',
|
|
244
|
+
Scheme = 'scheme',
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export interface StyleChange {
|
|
248
|
+
readonly op: PageChangeOp.Style | PageChangeOp.Scheme;
|
|
249
|
+
readonly handle: string;
|
|
250
|
+
readonly nodeId: string;
|
|
251
|
+
readonly label: string;
|
|
252
|
+
/** The style / scheme ids, and how the owner reads them. */
|
|
253
|
+
readonly from: string;
|
|
254
|
+
readonly to: string;
|
|
255
|
+
readonly before: string;
|
|
256
|
+
readonly after: string;
|
|
257
|
+
/** Values the author had set by hand on it, kept (`switchElementStyle`). */
|
|
258
|
+
readonly keptOwn?: number;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/** A type's unstyled values (what a released trait goes back to) — the builder passes its registry's; this is the saved shape's. */
|
|
262
|
+
export type BaselineStyles = (type: string) => Readonly<Record<string, unknown>>;
|
|
263
|
+
|
|
264
|
+
/** The serialized baseline: the type's constructor values, its text colour released as a new node's is (generation-core `createSerializedNode`). */
|
|
265
|
+
export const serializedBaselineStyles: BaselineStyles = type => {
|
|
266
|
+
const own = { ...(NODE_BASELINES[type]?.styleProps ?? {}) } as Record<string, unknown>;
|
|
267
|
+
for (const [name, value] of Object.entries(NEW_NODE_STYLE_DEFAULTS)) if (name in own) own[name] = value;
|
|
268
|
+
return own;
|
|
269
|
+
};
|
|
270
|
+
|
|
271
|
+
export interface StyleEditContext {
|
|
272
|
+
/** The focus's targets (`summarizePage(…).looks`). */
|
|
273
|
+
readonly looks: LookMap;
|
|
274
|
+
readonly baseline?: BaselineStyles;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export interface StyleEditsOutcome {
|
|
278
|
+
readonly ok: boolean;
|
|
279
|
+
readonly result: SerializedNode[];
|
|
280
|
+
readonly changes: ReadonlyArray<StyleChange>;
|
|
281
|
+
readonly issues: ReadonlyArray<AssistIssue>;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const schemeLabel = (id: string) => COLOR_SCHEMES.find(s => s.id === id)?.label ?? id;
|
|
285
|
+
const styleLabel = (id: string) => elementStyleById(id)?.label ?? id;
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Family `style`, applied — the SAME check on the server and in the builder. All or none; never mutates its input. A
|
|
289
|
+
* handle not in the focus, a style the target may not take, a scheme on a target that takes none are refused (re-asked
|
|
290
|
+
* once by the server, never repaired here); an edit that changes nothing is dropped.
|
|
291
|
+
*/
|
|
292
|
+
export function applyStyleEdits(
|
|
293
|
+
layout: ReadonlyArray<SerializedNode>, edits: ReadonlyArray<StyleEdit | SchemeEdit>, ctx: StyleEditContext,
|
|
294
|
+
): StyleEditsOutcome {
|
|
295
|
+
const result = JSON.parse(JSON.stringify(layout)) as SerializedNode[];
|
|
296
|
+
const byId = new Map<string, SerializedNode>();
|
|
297
|
+
walkSerialized(result, n => byId.set(n.id, n));
|
|
298
|
+
const baseline = ctx.baseline ?? serializedBaselineStyles;
|
|
299
|
+
const issues: AssistIssue[] = [];
|
|
300
|
+
const changes: StyleChange[] = [];
|
|
301
|
+
const seen = new Set<string>();
|
|
302
|
+
|
|
303
|
+
// Styles first: a scheme may need the style the same turn gives its node (a plain box made a panel, then darkened).
|
|
304
|
+
const ordered = [...edits.filter(e => !isSchemeEdit(e)), ...edits.filter(isSchemeEdit)];
|
|
305
|
+
for (const edit of ordered) {
|
|
306
|
+
const scheme = isSchemeEdit(edit);
|
|
307
|
+
const key = `${scheme ? 'scheme' : 'style'}:${edit.handle}`;
|
|
308
|
+
const refuse = (code: AssistIssueCode, message: string) => issues.push({ code, handle: edit.handle, message });
|
|
309
|
+
if (seen.has(key)) { refuse(AssistIssueCode.Duplicate, `"${edit.handle}" is given two ${scheme ? 'schemes' : 'styles'}`); continue; }
|
|
310
|
+
seen.add(key);
|
|
311
|
+
const target = Object.prototype.hasOwnProperty.call(ctx.looks, edit.handle) ? ctx.looks[edit.handle] : undefined;
|
|
312
|
+
const node = target ? byId.get(target.id) : undefined;
|
|
313
|
+
if (!target || !node) { refuse(AssistIssueCode.UnknownHandle, `"${edit.handle}" is not an element of the focus`); continue; }
|
|
314
|
+
|
|
315
|
+
if (scheme) {
|
|
316
|
+
const to = (edit as SchemeEdit).scheme;
|
|
317
|
+
if (!target.scheme) { refuse(AssistIssueCode.NotAnOption, `${target.label} takes no colour scheme`); continue; }
|
|
318
|
+
if (!COLOR_SCHEMES.some(s => s.id === to)) { refuse(AssistIssueCode.NotAnOption, `"${to}" is not a colour scheme`); continue; }
|
|
319
|
+
const from = colorSchemeOf(node as never) ?? ColorScheme.Default;
|
|
320
|
+
if (to !== ColorScheme.Default && !paintsGround(node.elementStyle)) {
|
|
321
|
+
refuse(AssistIssueCode.NotAnOption, `${target.label} paints no ground of its own — give it the section, card or panel style in the same answer, or leave its colours`);
|
|
322
|
+
continue;
|
|
323
|
+
}
|
|
324
|
+
if (!setColorScheme(node, to)) continue;
|
|
325
|
+
changes.push({ op: PageChangeOp.Scheme, handle: edit.handle, nodeId: node.id, label: target.label, from, to, before: schemeLabel(from), after: schemeLabel(to) });
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
const to = (edit as StyleEdit).style;
|
|
330
|
+
if (!target.styles.includes(to)) {
|
|
331
|
+
refuse(AssistIssueCode.NotAnOption, `${target.label} may take ${target.styles.map(s => `"${s}"`).join(', ')} — not "${to}"`);
|
|
332
|
+
continue;
|
|
333
|
+
}
|
|
334
|
+
const from = node.elementStyle ?? '';
|
|
335
|
+
if (from === to) continue;
|
|
336
|
+
const switched = switchElementStyle(node as ElementStyleTarget, from, to, baseline(node.type));
|
|
337
|
+
if (!switched.applied) { refuse(AssistIssueCode.NotAnOption, `"${to}" is not an element style`); continue; }
|
|
338
|
+
changes.push({
|
|
339
|
+
op: PageChangeOp.Style, handle: edit.handle, nodeId: node.id, label: target.label, from, to,
|
|
340
|
+
before: styleLabel(from), after: styleLabel(to), ...(switched.kept.length ? { keptOwn: switched.kept.length } : {}),
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (issues.length) return { ok: false, result: JSON.parse(JSON.stringify(layout)) as SerializedNode[], changes: [], issues };
|
|
345
|
+
return { ok: true, result, changes, issues: [] };
|
|
346
|
+
}
|
|
@@ -4,6 +4,7 @@ import { assistFactIssues, AssistWordContext } from './assist-facts';
|
|
|
4
4
|
import { namedLocale } from './assist-locale';
|
|
5
5
|
import { assertAssistBudget, MAX_ASSIST_PROPERTIES } from './budget';
|
|
6
6
|
import { AssistIssue, AssistIssueCode, Brief, BriefRules, HandleMap, NeverWrite, resolveHandle } from './brief';
|
|
7
|
+
import { LookMap, lookNames, PageChangeOp, PageLook, pageLooks, sectionLabel } from './page-style';
|
|
7
8
|
import { containsHtml, escapeText, paragraphsToRichText, richTextParagraphs } from './rich-text';
|
|
8
9
|
import { clipText } from './summarize-item';
|
|
9
10
|
import { AssistScope, AssistSurface, EditFamily, SURFACES } from './surfaces';
|
|
@@ -91,6 +92,8 @@ export interface PageSectionSummary {
|
|
|
91
92
|
/** The section's own name ("Hero", "Our work") or "Section 3". */
|
|
92
93
|
readonly label: string;
|
|
93
94
|
readonly parts: ReadonlyArray<PagePart>;
|
|
95
|
+
/** Family `style`: the elements of the section that may take another element style or colour scheme. */
|
|
96
|
+
readonly looks?: ReadonlyArray<PageLook>;
|
|
94
97
|
}
|
|
95
98
|
|
|
96
99
|
export interface PageFocus {
|
|
@@ -101,6 +104,9 @@ export interface PageFocus {
|
|
|
101
104
|
readonly selected: ReadonlyArray<string>;
|
|
102
105
|
/** The selection as the owner sees it ("Hero ▸ Heading 1"). */
|
|
103
106
|
readonly selectedLabel?: string;
|
|
107
|
+
/** How the owner reads the style and scheme ids the looks name. */
|
|
108
|
+
readonly styleNames?: Readonly<Record<string, string>>;
|
|
109
|
+
readonly schemeNames?: Readonly<Record<string, string>>;
|
|
104
110
|
}
|
|
105
111
|
|
|
106
112
|
export interface PageAround {
|
|
@@ -132,18 +138,14 @@ export interface SummarizedPage {
|
|
|
132
138
|
readonly focusHandles: ReadonlyArray<string>;
|
|
133
139
|
/** Every part of the page with its node id (for the client to outline a change) — never sent. */
|
|
134
140
|
readonly nodes: Readonly<Record<string, string>>;
|
|
141
|
+
/** Family `style`: the focus's targets — what a style request carries and the check may write. */
|
|
142
|
+
readonly looks: LookMap;
|
|
135
143
|
}
|
|
136
144
|
|
|
137
145
|
const sectionHandle = (index: number): string => `s${index}`;
|
|
138
146
|
|
|
139
147
|
const capitalise = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
140
148
|
|
|
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
149
|
function boundLabel(node: SerializedNode, prop: string): string | undefined {
|
|
148
150
|
const b = (node.bindings ?? []).find(x => x.prop === prop);
|
|
149
151
|
return b ? [b.source, b.path].filter(Boolean).join(' ▸ ') : undefined;
|
|
@@ -222,6 +224,12 @@ export function summarizePage(layout: ReadonlyArray<SerializedNode>, ctx: PageBr
|
|
|
222
224
|
const inFocus = (w: Walked) => scope === AssistScope.Page || w.section === selectedSection;
|
|
223
225
|
const selectedParts = scope === AssistScope.Node ? walked.filter(w => w.lineage.includes(ctx.selectedId!)) : [];
|
|
224
226
|
|
|
227
|
+
const wordHandles = new Map(walked.filter(w => !w.part.handle.split('.')[2]).map(w => [w.nodeId, w.part.handle]));
|
|
228
|
+
const walkedLooks = pageLooks(layout, wordHandles, ctx.selectedId);
|
|
229
|
+
const selectedWords = new Set(selectedParts.map(w => w.part.handle));
|
|
230
|
+
const lookInFocus = (l: (typeof walkedLooks)[number]) => scope === AssistScope.Page || (l.section === selectedSection
|
|
231
|
+
&& (scope === AssistScope.Section || l.lineage.includes(ctx.selectedId!) || selectedWords.has(l.look.handle)));
|
|
232
|
+
|
|
225
233
|
const sections: PageSectionSummary[] = layout
|
|
226
234
|
.map((root, index) => ({ root, index }))
|
|
227
235
|
.filter(({ index }) => scope === AssistScope.Page || index === selectedSection)
|
|
@@ -229,6 +237,10 @@ export function summarizePage(layout: ReadonlyArray<SerializedNode>, ctx: PageBr
|
|
|
229
237
|
handle: sectionHandle(index),
|
|
230
238
|
label: sectionLabel(root, index),
|
|
231
239
|
parts: walked.filter(w => w.section === index).map(w => ({ ...w.part, words: clipText(w.part.words, FOCUS_TEXT_CHARS) })),
|
|
240
|
+
...(() => {
|
|
241
|
+
const looks = walkedLooks.filter(l => l.section === index).map(l => l.look);
|
|
242
|
+
return looks.length ? { looks } : {};
|
|
243
|
+
})(),
|
|
232
244
|
}));
|
|
233
245
|
const around = layout
|
|
234
246
|
.map((root, index) => ({ root, index }))
|
|
@@ -245,6 +257,12 @@ export function summarizePage(layout: ReadonlyArray<SerializedNode>, ctx: PageBr
|
|
|
245
257
|
if (!w.part.bound) handles[w.part.handle] = { key: w.prop, path: [], id: w.nodeId };
|
|
246
258
|
}
|
|
247
259
|
layout.forEach((root, index) => { nodes[sectionHandle(index)] = root.id; });
|
|
260
|
+
const focusLooks = walkedLooks.filter(lookInFocus);
|
|
261
|
+
const looks: LookMap = Object.fromEntries(focusLooks.map(l => [l.look.handle, {
|
|
262
|
+
id: l.nodeId, type: l.type, label: l.look.label, styles: [...(l.look.styles ?? [])], scheme: l.look.scheme !== undefined,
|
|
263
|
+
}]));
|
|
264
|
+
for (const l of walkedLooks) nodes[l.look.handle] ??= l.nodeId;
|
|
265
|
+
const names = lookNames(focusLooks.map(l => l.look));
|
|
248
266
|
|
|
249
267
|
const focusWalked = scope === AssistScope.Node && selectedParts.length ? selectedParts : walked.filter(inFocus);
|
|
250
268
|
const never: NeverWrite[] = focusWalked.filter(w => w.part.bound)
|
|
@@ -257,12 +275,19 @@ export function summarizePage(layout: ReadonlyArray<SerializedNode>, ctx: PageBr
|
|
|
257
275
|
widensTo: WIDENS[scope] ?? [],
|
|
258
276
|
};
|
|
259
277
|
const selectedNode = selectedParts[0];
|
|
278
|
+
const selectedLook = scope === AssistScope.Node ? focusLooks.find(l => l.nodeId === ctx.selectedId) : undefined;
|
|
260
279
|
const focus: PageFocus = {
|
|
261
280
|
scope,
|
|
262
281
|
page: ctx.page,
|
|
263
282
|
sections,
|
|
264
|
-
selected:
|
|
265
|
-
|
|
283
|
+
selected: [...new Set([
|
|
284
|
+
...selectedParts.map(w => w.part.handle),
|
|
285
|
+
...(scope === AssistScope.Node ? focusLooks.map(l => l.look.handle) : []),
|
|
286
|
+
])],
|
|
287
|
+
...(selectedLook ? { selectedLabel: `${sections[0]?.label ?? ''} ▸ ${selectedLook.look.label}` }
|
|
288
|
+
: selectedNode ? { selectedLabel: `${sections[0]?.label ?? ''} ▸ ${selectedNode.part.label}` } : {}),
|
|
289
|
+
...(Object.keys(names.styles).length ? { styleNames: names.styles } : {}),
|
|
290
|
+
...(Object.keys(names.schemes).length ? { schemeNames: names.schemes } : {}),
|
|
266
291
|
};
|
|
267
292
|
return {
|
|
268
293
|
brief: {
|
|
@@ -276,6 +301,7 @@ export function summarizePage(layout: ReadonlyArray<SerializedNode>, ctx: PageBr
|
|
|
276
301
|
handles,
|
|
277
302
|
focusHandles: focusWalked.filter(w => !w.part.bound).map(w => w.part.handle),
|
|
278
303
|
nodes,
|
|
304
|
+
looks,
|
|
279
305
|
};
|
|
280
306
|
}
|
|
281
307
|
|
|
@@ -346,6 +372,8 @@ export function decodeWordsAnswers(answers: ReadonlyArray<unknown>): WordEdit[]
|
|
|
346
372
|
}
|
|
347
373
|
|
|
348
374
|
export interface WordChange {
|
|
375
|
+
/** Absent on a turn stored before AI2b — a words change. */
|
|
376
|
+
readonly op?: PageChangeOp.Words;
|
|
349
377
|
readonly handle: string;
|
|
350
378
|
/** The node the change is on — the builder outlines it; never sent to the model. */
|
|
351
379
|
readonly nodeId: string;
|
|
@@ -431,7 +459,7 @@ export function applyWordEdits(layout: ReadonlyArray<SerializedNode>, edits: Rea
|
|
|
431
459
|
if (readable === readableWords(before)) continue;
|
|
432
460
|
written.push({ handle: edit.handle, text: readable });
|
|
433
461
|
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 });
|
|
462
|
+
changes.push({ op: PageChangeOp.Words, handle: edit.handle, nodeId: node.id, ...partLabel(node, prop, edit.handle), before: readableWords(before), after: readable });
|
|
435
463
|
}
|
|
436
464
|
|
|
437
465
|
issues.push(...assistFactIssues(written, { words: ctx.words, sentence: ctx.sentence, existing: pageTexts(layout) }));
|
|
@@ -4,11 +4,12 @@
|
|
|
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
6
|
* cms-client as the trial — and family `words` for surface S1 (the builder, this page): `summarizePage`, `wordsJsonSchema`,
|
|
7
|
-
* `applyWordEdits`, `pageSuggestions
|
|
7
|
+
* `applyWordEdits`, `pageSuggestions` — and family `style` (`pageLooks`, `styleJsonSchema`, `applyStyleEdits`; both applied as
|
|
8
|
+
* one turn by `applyPageEdits`). Every family's words pass ONE fact gate (`assistFactIssues` over generation-core's
|
|
8
9
|
* `factIssues`, R-H2). Pure: no transport, no clock, no randomness.
|
|
9
10
|
* ⚠️ Not re-exported from the main entry. Consumers wire the usual trio — ng-package raw-ts assets, jest
|
|
10
11
|
* moduleNameMapper, tsconfig paths — or `nest start` dies with ERR_REQUIRE_ESM. It imports generation-core (the strict
|
|
11
|
-
* schema builders and budget), so a consumer maps
|
|
12
|
+
* schema builders and budget) and design-core (element styles, schemes), so a consumer maps all three.
|
|
12
13
|
*/
|
|
13
14
|
export * from './surfaces';
|
|
14
15
|
export * from './budget';
|
|
@@ -22,5 +23,7 @@ export * from './route-schema';
|
|
|
22
23
|
export * from './assist-facts';
|
|
23
24
|
export * from './field-edits';
|
|
24
25
|
export * from './page-words';
|
|
26
|
+
export * from './page-style';
|
|
27
|
+
export * from './page-edits';
|
|
25
28
|
export * from './assist-turn';
|
|
26
29
|
export * from './assist-locale';
|
|
@@ -120,7 +120,7 @@ export const SURFACES: Readonly<Record<AssistSurface, SurfaceSpec>> = {
|
|
|
120
120
|
// Style, Sections and Look & motion join as each slice ships (ai-assist-plan §6, AI2).
|
|
121
121
|
[AssistSurface.BuilderPage]: spec({
|
|
122
122
|
surface: AssistSurface.BuilderPage,
|
|
123
|
-
families: [EditFamily.Words],
|
|
123
|
+
families: [EditFamily.Words, EditFamily.Style],
|
|
124
124
|
commit: CommitKind.BuilderStep,
|
|
125
125
|
steps: [AssistStep.AssistRoute, AssistStep.AssistEdit],
|
|
126
126
|
permissions: [AssistPermission.PageUpdate],
|