@vectoriox/iox-ui 5.111.0 → 5.113.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 +650 -0
- package/assist-core/package.json +3 -0
- package/design-core/index.d.ts +80 -0
- package/fesm2022/vectoriox-iox-ui-assist-core.mjs +1144 -0
- package/fesm2022/vectoriox-iox-ui-assist-core.mjs.map +1 -0
- 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 +103 -43
- package/fesm2022/vectoriox-iox-ui-generation-core.mjs.map +1 -1
- package/generation-core/index.d.ts +20 -47
- package/package.json +5 -1
- package/src/assist-core/assist-locale.ts +73 -0
- package/src/assist-core/assist-turn.ts +240 -0
- package/src/assist-core/brief.ts +98 -0
- package/src/assist-core/budget.ts +34 -0
- package/src/assist-core/content-fields.ts +256 -0
- package/src/assist-core/field-edits.ts +178 -0
- package/src/assist-core/field-values.ts +42 -0
- package/src/assist-core/fields-schema.ts +117 -0
- package/src/assist-core/public-api.ts +24 -0
- package/src/assist-core/rich-text.ts +32 -0
- package/src/assist-core/route-schema.ts +49 -0
- package/src/assist-core/summarize-item.ts +219 -0
- package/src/assist-core/surfaces.ts +235 -0
- package/src/design-core/flavor-spec.generated.ts +94 -0
- package/src/generation-core/ai-run.ts +34 -0
- package/src/generation-core/compose-layout.ts +57 -18
- package/src/generation-core/plan-words.ts +12 -3
- package/src/generation-core/site-spec.generated.ts +1 -22
- package/styles/iox-design.css +39 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
import { fieldHandle } from './brief';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A content type's fields as Assist sees them, read from the REAL stored schema (iox-cms `schemas` row:
|
|
5
|
+
* `{ name, metaData, schemaObject: { properties } }`; each property `{ type, label, controller: { type, options } }` —
|
|
6
|
+
* cms-client `schema-doc/property-def.model.ts`). A multiLang field is `type: 'object'` + `controller.type: 'multiLang'`
|
|
7
|
+
* whose `properties` are its locales (`{ en: {…}, he: {…} }`), each with the inner control.
|
|
8
|
+
*
|
|
9
|
+
* Every field is either WRITABLE (one of the kinds below) or NEVER written, with a reason. Never written:
|
|
10
|
+
* - by what it is: relations, media (ids / URLs), secrets, dates, e-mail, nested structures, a select with no options;
|
|
11
|
+
* - by what it is about — a closed word list over the key and the label (English + Hebrew): money (price, cost, fee,
|
|
12
|
+
* currency, discount, VAT…), stock (quantity, inventory…), product codes (SKU, barcode…), contact facts (phone, e-mail,
|
|
13
|
+
* address, location, hours…), dates and web addresses (slug, URL, link). A field whose controller or type is `money`
|
|
14
|
+
* / `price` / `currency` is money, ready for "money as a field type" (AI plan) the day it exists.
|
|
15
|
+
* The model never sees the value of a never-written field, only whether it is filled; the owner fills it ("needs you").
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export interface ContentControllerDef {
|
|
19
|
+
readonly type?: string;
|
|
20
|
+
readonly options?: unknown;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface ContentPropertyDef {
|
|
24
|
+
readonly type: string;
|
|
25
|
+
readonly label?: string;
|
|
26
|
+
readonly controller?: ContentControllerDef;
|
|
27
|
+
readonly properties?: Readonly<Record<string, ContentPropertyDef>>;
|
|
28
|
+
readonly items?: ContentPropertyDef;
|
|
29
|
+
readonly required?: boolean;
|
|
30
|
+
readonly rel?: unknown;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface ContentTypeSchema {
|
|
34
|
+
readonly name: string;
|
|
35
|
+
readonly metaData?: { readonly label?: string; readonly previewTextField?: string; readonly previewImageField?: string };
|
|
36
|
+
readonly schemaObject: { readonly properties: Readonly<Record<string, ContentPropertyDef>> };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export enum AssistFieldKind {
|
|
40
|
+
Text = 'text',
|
|
41
|
+
Textarea = 'textarea',
|
|
42
|
+
/** Written as plain paragraphs, never HTML (trap T6). */
|
|
43
|
+
RichText = 'richText',
|
|
44
|
+
Number = 'number',
|
|
45
|
+
Boolean = 'boolean',
|
|
46
|
+
/** One of the field's options. */
|
|
47
|
+
Choice = 'choice',
|
|
48
|
+
/** Some of the field's options. */
|
|
49
|
+
Choices = 'choices',
|
|
50
|
+
/** A list of words. */
|
|
51
|
+
Tags = 'tags',
|
|
52
|
+
/** Not writable — `never` says why. */
|
|
53
|
+
Other = 'other',
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export enum NeverWriteReason {
|
|
57
|
+
Money = 'money',
|
|
58
|
+
Stock = 'stock',
|
|
59
|
+
ProductCode = 'productCode',
|
|
60
|
+
Contact = 'contact',
|
|
61
|
+
Date = 'date',
|
|
62
|
+
WebAddress = 'webAddress',
|
|
63
|
+
Relation = 'relation',
|
|
64
|
+
Media = 'media',
|
|
65
|
+
Secret = 'secret',
|
|
66
|
+
Structure = 'structure',
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface AssistField {
|
|
70
|
+
readonly key: string;
|
|
71
|
+
readonly handle: string;
|
|
72
|
+
readonly label: string;
|
|
73
|
+
readonly kind: AssistFieldKind;
|
|
74
|
+
readonly required: boolean;
|
|
75
|
+
/** Choice / Choices: the option values the field accepts. */
|
|
76
|
+
readonly options?: ReadonlyArray<string>;
|
|
77
|
+
/** multiLang: the locales the field holds, in schema order. */
|
|
78
|
+
readonly locales?: ReadonlyArray<string>;
|
|
79
|
+
readonly never?: NeverWriteReason;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** iox-cms `RESERVED_SCHEMA_PROPERTIES` + read-time bookkeeping + shop catalog bookkeeping — never a field. */
|
|
83
|
+
const NOT_FIELDS: ReadonlySet<string> = new Set([
|
|
84
|
+
'isPublished', 'organization', 'info', 'addedBy', 'created', 'lastUpdate', '_id', 'isSample', 'pricingUnit',
|
|
85
|
+
]);
|
|
86
|
+
|
|
87
|
+
enum Control {
|
|
88
|
+
Text = 'text',
|
|
89
|
+
Tag = 'tag',
|
|
90
|
+
Textarea = 'textarea',
|
|
91
|
+
RichText = 'richText',
|
|
92
|
+
Number = 'number',
|
|
93
|
+
Range = 'range',
|
|
94
|
+
Switch = 'switch',
|
|
95
|
+
Checkbox = 'checkbox',
|
|
96
|
+
Select = 'select',
|
|
97
|
+
MultiSelect = 'multiSelect',
|
|
98
|
+
CheckboxGroup = 'checkboxGroup',
|
|
99
|
+
Tags = 'tags',
|
|
100
|
+
List = 'list',
|
|
101
|
+
MultiLang = 'multiLang',
|
|
102
|
+
Email = 'email',
|
|
103
|
+
Password = 'password',
|
|
104
|
+
Encrypt = 'encrypt',
|
|
105
|
+
Date = 'date',
|
|
106
|
+
Time = 'time',
|
|
107
|
+
Image = 'image',
|
|
108
|
+
Media = 'media',
|
|
109
|
+
Gallery = 'gallery',
|
|
110
|
+
RelationCombobox = 'relationCombobox',
|
|
111
|
+
Money = 'money',
|
|
112
|
+
Price = 'price',
|
|
113
|
+
Currency = 'currency',
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
enum DataType {
|
|
117
|
+
String = 'string',
|
|
118
|
+
Array = 'array',
|
|
119
|
+
Object = 'object',
|
|
120
|
+
Rel = 'rel',
|
|
121
|
+
Encrypt = 'encrypt',
|
|
122
|
+
Date = 'date',
|
|
123
|
+
Money = 'money',
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const KIND_OF_CONTROL: Readonly<Partial<Record<string, AssistFieldKind>>> = {
|
|
127
|
+
[Control.Text]: AssistFieldKind.Text,
|
|
128
|
+
[Control.Tag]: AssistFieldKind.Text,
|
|
129
|
+
[Control.Textarea]: AssistFieldKind.Textarea,
|
|
130
|
+
[Control.RichText]: AssistFieldKind.RichText,
|
|
131
|
+
[Control.Number]: AssistFieldKind.Number,
|
|
132
|
+
[Control.Range]: AssistFieldKind.Number,
|
|
133
|
+
[Control.Switch]: AssistFieldKind.Boolean,
|
|
134
|
+
[Control.Checkbox]: AssistFieldKind.Boolean,
|
|
135
|
+
[Control.Select]: AssistFieldKind.Choice,
|
|
136
|
+
[Control.MultiSelect]: AssistFieldKind.Choices,
|
|
137
|
+
[Control.CheckboxGroup]: AssistFieldKind.Choices,
|
|
138
|
+
[Control.Tags]: AssistFieldKind.Tags,
|
|
139
|
+
[Control.List]: AssistFieldKind.Tags,
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const LIST_KINDS: ReadonlySet<AssistFieldKind> = new Set([AssistFieldKind.Choices, AssistFieldKind.Tags]);
|
|
143
|
+
const CHOICE_KINDS: ReadonlySet<AssistFieldKind> = new Set([AssistFieldKind.Choice, AssistFieldKind.Choices]);
|
|
144
|
+
|
|
145
|
+
const NEVER_OF_CONTROL: Readonly<Partial<Record<string, NeverWriteReason>>> = {
|
|
146
|
+
[Control.Email]: NeverWriteReason.Contact,
|
|
147
|
+
[Control.Password]: NeverWriteReason.Secret,
|
|
148
|
+
[Control.Encrypt]: NeverWriteReason.Secret,
|
|
149
|
+
[Control.Date]: NeverWriteReason.Date,
|
|
150
|
+
[Control.Time]: NeverWriteReason.Date,
|
|
151
|
+
[Control.Image]: NeverWriteReason.Media,
|
|
152
|
+
[Control.Media]: NeverWriteReason.Media,
|
|
153
|
+
[Control.Gallery]: NeverWriteReason.Media,
|
|
154
|
+
[Control.RelationCombobox]: NeverWriteReason.Relation,
|
|
155
|
+
[Control.Money]: NeverWriteReason.Money,
|
|
156
|
+
[Control.Price]: NeverWriteReason.Money,
|
|
157
|
+
[Control.Currency]: NeverWriteReason.Money,
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const NEVER_OF_TYPE: Readonly<Partial<Record<string, NeverWriteReason>>> = {
|
|
161
|
+
[DataType.Rel]: NeverWriteReason.Relation,
|
|
162
|
+
[DataType.Encrypt]: NeverWriteReason.Secret,
|
|
163
|
+
[DataType.Date]: NeverWriteReason.Date,
|
|
164
|
+
[DataType.Money]: NeverWriteReason.Money,
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
/** The closed word list: a key or label word that makes a field a fact the owner gives. */
|
|
168
|
+
const NEVER_WORDS: ReadonlyArray<readonly [NeverWriteReason, ReadonlyArray<string>, ReadonlyArray<string>]> = [
|
|
169
|
+
[NeverWriteReason.Money,
|
|
170
|
+
['price', 'prices', 'pricing', 'cost', 'costs', 'amount', 'fee', 'fees', 'currency', 'discount', 'compareat', 'vat', 'tax', 'total', 'salary', 'budget', 'payment'],
|
|
171
|
+
['מחיר', 'עלות', 'תשלום', 'מע"מ', 'הנחה']],
|
|
172
|
+
[NeverWriteReason.Stock, ['stock', 'quantity', 'qty', 'inventory'], ['מלאי', 'כמות']],
|
|
173
|
+
[NeverWriteReason.ProductCode, ['sku', 'barcode', 'ean', 'upc', 'gtin', 'isbn', 'mpn'], ['מק"ט', 'ברקוד']],
|
|
174
|
+
[NeverWriteReason.Contact,
|
|
175
|
+
['phone', 'tel', 'telephone', 'mobile', 'whatsapp', 'email', 'mail', 'fax', 'address', 'street', 'city', 'zip', 'postcode', 'location', 'hours'],
|
|
176
|
+
['טלפון', 'נייד', 'וואטסאפ', 'דוא"ל', 'אימייל', 'מייל', 'כתובת', 'שעות', 'פקס', 'מיקום']],
|
|
177
|
+
[NeverWriteReason.Date, ['date', 'dates', 'time', 'datetime', 'deadline'], ['תאריך']],
|
|
178
|
+
[NeverWriteReason.WebAddress, ['slug', 'url', 'uri', 'href', 'link', 'website', 'route', 'permalink'], ['קישור', 'לינק']],
|
|
179
|
+
];
|
|
180
|
+
|
|
181
|
+
/** `startDate` → start, date · `ticket_link` → ticket, link · `Price (₪)` → price. */
|
|
182
|
+
function words(text: string): string[] {
|
|
183
|
+
return text.replace(/([a-z0-9])([A-Z])/g, '$1 $2').toLowerCase().split(/[^a-z0-9]+/).filter(Boolean);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
function neverByWords(key: string, label: string): NeverWriteReason | undefined {
|
|
187
|
+
const english = new Set([...words(key), ...words(label)]);
|
|
188
|
+
const raw = `${key} ${label}`;
|
|
189
|
+
return NEVER_WORDS.find(([, en, he]) => en.some(w => english.has(w)) || he.some(w => raw.includes(w)))?.[0];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
function optionValues(options: unknown): string[] | undefined {
|
|
193
|
+
if (!Array.isArray(options)) return undefined;
|
|
194
|
+
const values = options.map(o => (o && typeof o === 'object' ? (o as { value?: unknown }).value : o))
|
|
195
|
+
.filter(v => v !== undefined && v !== null && v !== '')
|
|
196
|
+
.map(String);
|
|
197
|
+
return values.length ? [...new Set(values)] : undefined;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
interface Shape {
|
|
201
|
+
kind: AssistFieldKind;
|
|
202
|
+
options?: string[];
|
|
203
|
+
never?: NeverWriteReason;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
const other = (never: NeverWriteReason): Shape => ({ kind: AssistFieldKind.Other, never });
|
|
207
|
+
|
|
208
|
+
/** What one leaf property is, by its type and control alone. */
|
|
209
|
+
function leafShape(def: ContentPropertyDef): Shape {
|
|
210
|
+
const control = def.controller?.type ?? '';
|
|
211
|
+
const never = NEVER_OF_TYPE[def.type] ?? NEVER_OF_CONTROL[control] ?? (def.items ? NEVER_OF_CONTROL[def.items.controller?.type ?? ''] : undefined);
|
|
212
|
+
if (never) return other(never);
|
|
213
|
+
const kind = KIND_OF_CONTROL[control];
|
|
214
|
+
const isList = def.type === DataType.Array;
|
|
215
|
+
if (!kind || isList !== LIST_KINDS.has(kind)) return other(NeverWriteReason.Structure);
|
|
216
|
+
if (isList && def.items && def.items.type !== DataType.String) return other(NeverWriteReason.Structure);
|
|
217
|
+
if (!CHOICE_KINDS.has(kind)) return { kind };
|
|
218
|
+
const options = optionValues(def.controller?.options);
|
|
219
|
+
return options ? { kind, options } : other(NeverWriteReason.Structure);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
const isMultiLang = (def: ContentPropertyDef) => def.controller?.type === Control.MultiLang && !!def.properties;
|
|
223
|
+
|
|
224
|
+
function fieldOf(key: string, def: ContentPropertyDef): AssistField {
|
|
225
|
+
const label = def.label || key;
|
|
226
|
+
const base = { key, handle: fieldHandle(key), label, required: !!def.required };
|
|
227
|
+
if (isMultiLang(def)) {
|
|
228
|
+
const locales = Object.keys(def.properties ?? {});
|
|
229
|
+
const inner = locales.length ? leafShape(def.properties![locales[0]]) : other(NeverWriteReason.Structure);
|
|
230
|
+
const never = inner.never ?? neverByWords(key, label);
|
|
231
|
+
return never ? { ...base, kind: AssistFieldKind.Other, locales, never } : { ...base, ...inner, locales };
|
|
232
|
+
}
|
|
233
|
+
if (def.type === DataType.Object) return { ...base, ...other(NeverWriteReason.Structure) };
|
|
234
|
+
const shape = leafShape(def);
|
|
235
|
+
const never = shape.never ?? neverByWords(key, label);
|
|
236
|
+
return never ? { ...base, kind: AssistFieldKind.Other, never } : { ...base, ...shape };
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** A property the item form renders: not bookkeeping, and (a leaf) carrying a control. */
|
|
240
|
+
const isFormField = (key: string, def: ContentPropertyDef) =>
|
|
241
|
+
!NOT_FIELDS.has(key) && !!def && (def.type === DataType.Object || !!def.controller);
|
|
242
|
+
|
|
243
|
+
/** Every form field of the type, in schema order, writable or with the reason it is never written. */
|
|
244
|
+
export function assistFields(schema: ContentTypeSchema): AssistField[] {
|
|
245
|
+
return Object.entries(schema.schemaObject?.properties ?? {})
|
|
246
|
+
.filter(([key, def]) => isFormField(key, def))
|
|
247
|
+
.map(([key, def]) => fieldOf(key, def));
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
export const isWritable = (field: AssistField): boolean => !field.never;
|
|
251
|
+
|
|
252
|
+
/** The type's title field: its `@title` role (`metaData.previewTextField`), else its first text field. */
|
|
253
|
+
export function titleField(schema: ContentTypeSchema, fields: ReadonlyArray<AssistField> = assistFields(schema)): AssistField | undefined {
|
|
254
|
+
const role = schema.metaData?.previewTextField;
|
|
255
|
+
return fields.find(f => f.key === role) ?? fields.find(f => f.kind === AssistFieldKind.Text);
|
|
256
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { AssistIssue, AssistIssueCode, HandleMap, resolveHandle } from './brief';
|
|
2
|
+
import { AssistField, AssistFieldKind, assistFields, ContentTypeSchema } from './content-fields';
|
|
3
|
+
import { BOOLEAN_FALSE, BOOLEAN_TRUE, FieldEdit } from './fields-schema';
|
|
4
|
+
import { BriefValue, isPlainRecord, readFieldValue } from './field-values';
|
|
5
|
+
import { containsHtml, paragraphsToRichText } from './rich-text';
|
|
6
|
+
import { ContentRecord } from './summarize-item';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Family `fields`, applied (plan §3c rule 6): the SAME pure check runs on the server (on a copy, before the turn is
|
|
10
|
+
* shown) and in the client (the trial in the form). All or none — one refused edit refuses the turn, which is re-asked
|
|
11
|
+
* once with the issues, never repaired in code. Never mutates its input.
|
|
12
|
+
* - a handle maps back through the brief's map; an unknown one is refused (T1);
|
|
13
|
+
* - a never-written field, a wrong type, a choice outside the options, markup in a text: refused;
|
|
14
|
+
* - a multiLang field gets ONE locale key, the others kept (T5);
|
|
15
|
+
* - rich text is stored as escaped `<p>` paragraphs; the change carries the paragraphs (T6);
|
|
16
|
+
* - a field the owner edited since the last turn is KEPT unless the sentence names it (rule 9);
|
|
17
|
+
* - an edit that changes nothing is dropped.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export interface FieldEditContext {
|
|
21
|
+
readonly schema: ContentTypeSchema;
|
|
22
|
+
readonly handles: HandleMap;
|
|
23
|
+
/** The locale a multiLang field is written in. */
|
|
24
|
+
readonly locale: string;
|
|
25
|
+
/** Fields (keys or handles) the owner changed since the last AI turn. */
|
|
26
|
+
readonly ownerEdited?: ReadonlyArray<string>;
|
|
27
|
+
/** Fields (keys or handles) the sentence names. Default: read from `sentence` by `fieldsNamedIn`. */
|
|
28
|
+
readonly named?: ReadonlyArray<string>;
|
|
29
|
+
readonly sentence?: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface FieldChange {
|
|
33
|
+
readonly handle: string;
|
|
34
|
+
readonly key: string;
|
|
35
|
+
readonly label: string;
|
|
36
|
+
readonly kind: AssistFieldKind;
|
|
37
|
+
/** Readable: text, paragraphs, a number, yes/no, options, words. */
|
|
38
|
+
readonly before: BriefValue;
|
|
39
|
+
readonly after: BriefValue;
|
|
40
|
+
readonly locale?: string;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface FieldEditsOutcome {
|
|
44
|
+
readonly ok: boolean;
|
|
45
|
+
/** The record with every change applied (stored form) — a copy of the input when refused. */
|
|
46
|
+
readonly result: ContentRecord;
|
|
47
|
+
readonly changes: ReadonlyArray<FieldChange>;
|
|
48
|
+
/** Proposed, but the owner edited the field since and the sentence did not name it. */
|
|
49
|
+
readonly kept: ReadonlyArray<FieldChange>;
|
|
50
|
+
readonly issues: ReadonlyArray<AssistIssue>;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
type Coerced = { ok: true; stored: unknown; readable: BriefValue } | { ok: false; code: AssistIssueCode; message: string };
|
|
54
|
+
|
|
55
|
+
const refuse = (code: AssistIssueCode, message: string): Coerced => ({ ok: false, code, message });
|
|
56
|
+
const wrongType = (what: string) => refuse(AssistIssueCode.WrongType, `expected ${what}`);
|
|
57
|
+
const fine = (stored: unknown, readable: BriefValue = stored as BriefValue): Coerced => ({ ok: true, stored, readable });
|
|
58
|
+
|
|
59
|
+
function coerceText(value: unknown): Coerced {
|
|
60
|
+
if (typeof value !== 'string' || !value.trim()) return wrongType('a non-empty text');
|
|
61
|
+
if (containsHtml(value)) return refuse(AssistIssueCode.HtmlNotAllowed, 'markup is not written by Assist');
|
|
62
|
+
return fine(value.trim());
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function coerceWords(value: unknown): { words: string[] } | Coerced {
|
|
66
|
+
if (!Array.isArray(value) || value.some(v => typeof v !== 'string')) return wrongType('a list of texts');
|
|
67
|
+
const words = [...new Set((value as string[]).map(v => v.trim()).filter(Boolean))];
|
|
68
|
+
if (!words.length) return wrongType('at least one text');
|
|
69
|
+
if (words.some(containsHtml)) return refuse(AssistIssueCode.HtmlNotAllowed, 'markup is not written by Assist');
|
|
70
|
+
return { words };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function coerceNumber(value: unknown): Coerced {
|
|
74
|
+
const n = typeof value === 'number' ? value : typeof value === 'string' && value.trim() !== '' ? Number(value.trim()) : NaN;
|
|
75
|
+
return Number.isFinite(n) ? fine(n) : wrongType('a number');
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function coerceBoolean(value: unknown): Coerced {
|
|
79
|
+
if (typeof value === 'boolean') return fine(value);
|
|
80
|
+
if (value === BOOLEAN_TRUE || value === BOOLEAN_FALSE) return fine(value === BOOLEAN_TRUE);
|
|
81
|
+
return wrongType('yes or no');
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function coerceChoice(field: AssistField, value: unknown): Coerced {
|
|
85
|
+
if (typeof value !== 'string') return wrongType('one of the options');
|
|
86
|
+
return field.options?.includes(value) ? fine(value) : refuse(AssistIssueCode.NotAnOption, `"${value}" is not an option of ${field.label}`);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function coerceList(field: AssistField, value: unknown): Coerced {
|
|
90
|
+
const read = coerceWords(value);
|
|
91
|
+
if (!('words' in read)) return read;
|
|
92
|
+
if (field.kind === AssistFieldKind.Choices) {
|
|
93
|
+
const outside = read.words.filter(w => !field.options?.includes(w));
|
|
94
|
+
if (outside.length) return refuse(AssistIssueCode.NotAnOption, `${outside.map(w => `"${w}"`).join(', ')} not an option of ${field.label}`);
|
|
95
|
+
}
|
|
96
|
+
return fine(read.words);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function coerceRichText(value: unknown): Coerced {
|
|
100
|
+
const read = coerceWords(value);
|
|
101
|
+
if (!('words' in read)) return read;
|
|
102
|
+
return fine(paragraphsToRichText(read.words), read.words);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function coerce(field: AssistField, value: unknown): Coerced {
|
|
106
|
+
switch (field.kind) {
|
|
107
|
+
case AssistFieldKind.RichText: return coerceRichText(value);
|
|
108
|
+
case AssistFieldKind.Number: return coerceNumber(value);
|
|
109
|
+
case AssistFieldKind.Boolean: return coerceBoolean(value);
|
|
110
|
+
case AssistFieldKind.Choice: return coerceChoice(field, value);
|
|
111
|
+
case AssistFieldKind.Choices:
|
|
112
|
+
case AssistFieldKind.Tags: return coerceList(field, value);
|
|
113
|
+
default: return coerceText(value);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
const NAME_MIN_CHARS = 3;
|
|
118
|
+
|
|
119
|
+
/** The fields a sentence names by label, key or handle (case-insensitive) — rule 9's "unless the sentence names it". */
|
|
120
|
+
export function fieldsNamedIn(sentence: string, fields: ReadonlyArray<AssistField>): string[] {
|
|
121
|
+
const s = sentence.toLowerCase();
|
|
122
|
+
if (!s.trim()) return [];
|
|
123
|
+
return fields.filter(f => [f.label, f.key, f.handle].some(n => n.length >= NAME_MIN_CHARS && s.includes(n.toLowerCase()))).map(f => f.key);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const names = (list: ReadonlyArray<string> | undefined, fields: ReadonlyArray<AssistField>): Set<string> =>
|
|
127
|
+
new Set((list ?? []).map(n => fields.find(f => f.key === n || f.handle === n)?.key ?? n));
|
|
128
|
+
|
|
129
|
+
interface Planned {
|
|
130
|
+
field: AssistField;
|
|
131
|
+
stored: unknown;
|
|
132
|
+
change: FieldChange;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function plan(field: AssistField, handle: string, values: ContentRecord, coerced: { stored: unknown; readable: BriefValue }, locale: string): Planned | null {
|
|
136
|
+
const raw = values[field.key];
|
|
137
|
+
const own = field.locales ? (isPlainRecord(raw) ? raw : {}) : null;
|
|
138
|
+
const before = readFieldValue(field.kind, own ? own[locale] : raw);
|
|
139
|
+
if (JSON.stringify(before) === JSON.stringify(coerced.readable)) return null;
|
|
140
|
+
const stored = own ? { ...own, [locale]: coerced.stored } : coerced.stored;
|
|
141
|
+
const change: FieldChange = {
|
|
142
|
+
handle, key: field.key, label: field.label, kind: field.kind, before, after: coerced.readable, ...(own ? { locale } : {}),
|
|
143
|
+
};
|
|
144
|
+
return { field, stored, change };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export function applyFieldEdits(values: ContentRecord, edits: ReadonlyArray<FieldEdit>, ctx: FieldEditContext): FieldEditsOutcome {
|
|
148
|
+
const fields = assistFields(ctx.schema);
|
|
149
|
+
const issues: AssistIssue[] = [];
|
|
150
|
+
const planned: Planned[] = [];
|
|
151
|
+
const seen = new Set<string>();
|
|
152
|
+
const issue = (code: AssistIssueCode, handle: string, message: string) => issues.push({ code, handle, message });
|
|
153
|
+
|
|
154
|
+
for (const edit of edits) {
|
|
155
|
+
const resolved = resolveHandle(ctx.handles, edit.handle);
|
|
156
|
+
if ('issue' in resolved) { issues.push(resolved.issue); continue; }
|
|
157
|
+
const field = fields.find(f => f.key === resolved.target.key);
|
|
158
|
+
if (!field) { issue(AssistIssueCode.UnknownHandle, edit.handle, `"${edit.handle}" is not a field of ${ctx.schema.name}`); continue; }
|
|
159
|
+
if (seen.has(field.key)) { issue(AssistIssueCode.Duplicate, edit.handle, `${field.label} is written twice`); continue; }
|
|
160
|
+
seen.add(field.key);
|
|
161
|
+
if (field.never) { issue(AssistIssueCode.NeverWrite, edit.handle, `${field.label} is never written by Assist (${field.never})`); continue; }
|
|
162
|
+
if (field.locales && !field.locales.includes(ctx.locale)) { issue(AssistIssueCode.WrongLocale, edit.handle, `${field.label} has no ${ctx.locale}`); continue; }
|
|
163
|
+
const coerced = coerce(field, edit.value);
|
|
164
|
+
if ('code' in coerced) { issue(coerced.code, edit.handle, `${field.label}: ${coerced.message}`); continue; }
|
|
165
|
+
const p = plan(field, edit.handle, values, coerced, ctx.locale);
|
|
166
|
+
if (p) planned.push(p);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (issues.length) return { ok: false, result: { ...values }, changes: [], kept: [], issues };
|
|
170
|
+
|
|
171
|
+
const edited = names(ctx.ownerEdited, fields);
|
|
172
|
+
const named = names(ctx.named ?? fieldsNamedIn(ctx.sentence ?? '', fields), fields);
|
|
173
|
+
const isKept = (p: Planned) => edited.has(p.field.key) && !named.has(p.field.key);
|
|
174
|
+
const applied = planned.filter(p => !isKept(p));
|
|
175
|
+
const result: Record<string, unknown> = { ...values };
|
|
176
|
+
for (const p of applied) result[p.field.key] = p.stored;
|
|
177
|
+
return { ok: true, result, changes: applied.map(p => p.change), kept: planned.filter(isKept).map(p => p.change), issues: [] };
|
|
178
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { AssistFieldKind } from './content-fields';
|
|
2
|
+
import { richTextParagraphs } from './rich-text';
|
|
3
|
+
|
|
4
|
+
/** A field's value as the model reads it: text, paragraphs, a number, yes/no, options, words, or per locale. */
|
|
5
|
+
export type BriefValue = string | number | boolean | ReadonlyArray<string> | Readonly<Record<string, string | ReadonlyArray<string>>> | null;
|
|
6
|
+
|
|
7
|
+
const LIST_KINDS: ReadonlySet<AssistFieldKind> = new Set([AssistFieldKind.Choices, AssistFieldKind.Tags]);
|
|
8
|
+
|
|
9
|
+
export const isPlainRecord = (v: unknown): v is Record<string, unknown> => !!v && typeof v === 'object' && !Array.isArray(v);
|
|
10
|
+
const asStrings = (v: unknown): string[] => (Array.isArray(v) ? v.filter(x => x !== null && x !== undefined && x !== '').map(String) : []);
|
|
11
|
+
|
|
12
|
+
function readNumber(raw: unknown): number | null {
|
|
13
|
+
if (typeof raw === 'number') return Number.isFinite(raw) ? raw : null;
|
|
14
|
+
if (typeof raw !== 'string' || raw.trim() === '') return null;
|
|
15
|
+
const n = Number(raw);
|
|
16
|
+
return Number.isFinite(n) ? n : null;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** One stored value (a plain field, or one locale of a multiLang field) in its kind's readable form. */
|
|
20
|
+
export function readFieldValue(kind: AssistFieldKind, raw: unknown): BriefValue {
|
|
21
|
+
if (raw === undefined || raw === null) return null;
|
|
22
|
+
if (kind === AssistFieldKind.RichText) return richTextParagraphs(raw);
|
|
23
|
+
if (LIST_KINDS.has(kind)) return asStrings(raw);
|
|
24
|
+
if (kind === AssistFieldKind.Number) return readNumber(raw);
|
|
25
|
+
if (kind === AssistFieldKind.Boolean) return !!raw;
|
|
26
|
+
return typeof raw === 'string' ? raw : String(raw);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function isEmptyFieldValue(v: unknown): boolean {
|
|
30
|
+
if (v === null || v === undefined) return true;
|
|
31
|
+
if (typeof v === 'string') return v.trim() === '';
|
|
32
|
+
if (Array.isArray(v)) return v.length === 0;
|
|
33
|
+
if (isPlainRecord(v)) return Object.values(v).every(isEmptyFieldValue);
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** A multiLang value's locales, each read in the field's kind. */
|
|
38
|
+
export function readLocaleValues(kind: AssistFieldKind, locales: ReadonlyArray<string>, raw: unknown): Record<string, BriefValue> {
|
|
39
|
+
const values = isPlainRecord(raw) ? raw : {};
|
|
40
|
+
return Object.fromEntries(locales.map(l => [l, readFieldValue(kind, values[l])]));
|
|
41
|
+
}
|
|
42
|
+
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { jsonClosedObject, jsonListOf, jsonOneOf, JsonSchema, jsonText } from '@vectoriox/iox-ui/generation-core';
|
|
2
|
+
|
|
3
|
+
import { assertAssistBudget, MAX_ASSIST_PROPERTIES } from './budget';
|
|
4
|
+
import { AssistBriefError, AssistIssueCode } from './brief';
|
|
5
|
+
import { AssistField, AssistFieldKind, assistFields, ContentTypeSchema, isWritable } from './content-fields';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* The strict output format of family `fields` for one content type (plan §3b): one property per WRITABLE field, keyed
|
|
9
|
+
* by its handle, every key required, every object closed. "Leave it as it is" is the kind's NONE value — `''` for a
|
|
10
|
+
* text, a number (written in digits), a yes/no or a choice, `[]` for paragraphs and lists — never `null`: the house
|
|
11
|
+
* budget allows 8 unions a request, so nullable fields would cap a part at 8 fields; the house wire (plan-edits) uses
|
|
12
|
+
* the same "every key required, each with its none". So Assist never EMPTIES a field — clearing one is the owner's.
|
|
13
|
+
* Rich text is a list of plain paragraphs (trap T6); a multiLang field is written in ONE locale (trap T5). A type with
|
|
14
|
+
* more writable fields than one request holds is split into parts, each ≤ 40 properties (trap T7 — never raised).
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
export enum FieldsAnswerKey {
|
|
18
|
+
Fields = 'fields',
|
|
19
|
+
Note = 'note',
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The answer object's own keys, beside the fields. */
|
|
23
|
+
const PART_OWN_PROPERTIES = Object.keys(FieldsAnswerKey).length;
|
|
24
|
+
|
|
25
|
+
/** How many fields one request carries. */
|
|
26
|
+
export const FIELDS_PER_PART = MAX_ASSIST_PROPERTIES - PART_OWN_PROPERTIES;
|
|
27
|
+
|
|
28
|
+
export const NONE_TEXT = '';
|
|
29
|
+
export const BOOLEAN_TRUE = 'true';
|
|
30
|
+
export const BOOLEAN_FALSE = 'false';
|
|
31
|
+
|
|
32
|
+
export interface FieldsSchemaOptions {
|
|
33
|
+
/** The locale a multiLang field is written in. */
|
|
34
|
+
readonly locale: string;
|
|
35
|
+
/** Only these fields (keys or handles) — the focus. Default: every writable field. */
|
|
36
|
+
readonly fields?: ReadonlyArray<string>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface FieldsSchemas {
|
|
40
|
+
/** One strict format per request. Empty when nothing is writable. */
|
|
41
|
+
readonly parts: JsonSchema[];
|
|
42
|
+
/** Per part, the handles it carries. */
|
|
43
|
+
readonly handles: string[][];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const LEAVE_TEXT = `'' leaves it as it is`;
|
|
47
|
+
const LEAVE_LIST = '[] leaves it as it is';
|
|
48
|
+
|
|
49
|
+
function kindSchema(field: AssistField): { schema: JsonSchema; says: string } {
|
|
50
|
+
const options = [...(field.options ?? [])];
|
|
51
|
+
switch (field.kind) {
|
|
52
|
+
case AssistFieldKind.RichText: return { schema: jsonListOf(jsonText()), says: `paragraphs of plain text, no markup; ${LEAVE_LIST}` };
|
|
53
|
+
case AssistFieldKind.Number: return { schema: jsonText(), says: `a number in digits; ${LEAVE_TEXT}` };
|
|
54
|
+
case AssistFieldKind.Boolean: return { schema: jsonOneOf([NONE_TEXT, BOOLEAN_TRUE, BOOLEAN_FALSE]), says: `yes or no; ${LEAVE_TEXT}` };
|
|
55
|
+
case AssistFieldKind.Choice: return { schema: jsonOneOf([NONE_TEXT, ...options]), says: `one of its options; ${LEAVE_TEXT}` };
|
|
56
|
+
case AssistFieldKind.Choices: return { schema: jsonListOf(jsonOneOf(options)), says: `some of its options; ${LEAVE_LIST}` };
|
|
57
|
+
case AssistFieldKind.Tags: return { schema: jsonListOf(jsonText()), says: `a list of short words; ${LEAVE_LIST}` };
|
|
58
|
+
case AssistFieldKind.Textarea: return { schema: jsonText(), says: `plain text, may run several sentences; ${LEAVE_TEXT}` };
|
|
59
|
+
default: return { schema: jsonText(), says: `one line of plain text; ${LEAVE_TEXT}` };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function fieldSchema(field: AssistField, locale: string): JsonSchema {
|
|
64
|
+
const { schema, says } = kindSchema(field);
|
|
65
|
+
const inLocale = field.locales ? ` Written in ${locale} only.` : '';
|
|
66
|
+
return { ...schema, description: `${field.label}${field.required ? ' (required)' : ''} — ${says}.${inLocale}` };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function chosen(all: ReadonlyArray<AssistField>, wanted: ReadonlyArray<string> | undefined): AssistField[] {
|
|
70
|
+
if (!wanted) return all.filter(isWritable);
|
|
71
|
+
return wanted.map(w => {
|
|
72
|
+
const f = all.find(x => x.key === w || x.handle === w);
|
|
73
|
+
if (!f) throw new AssistBriefError(AssistIssueCode.UnknownHandle, `"${w}" is not a field of this type`);
|
|
74
|
+
return f;
|
|
75
|
+
}).filter(isWritable);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Splits n fields into the fewest parts that fit, as even as they come. */
|
|
79
|
+
function split<T>(list: ReadonlyArray<T>, max: number): T[][] {
|
|
80
|
+
if (!list.length) return [];
|
|
81
|
+
const count = Math.ceil(list.length / max);
|
|
82
|
+
const size = Math.ceil(list.length / count);
|
|
83
|
+
return Array.from({ length: count }, (_, i) => list.slice(i * size, (i + 1) * size));
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function partSchema(fields: ReadonlyArray<AssistField>, locale: string, part: number, of: number): JsonSchema {
|
|
87
|
+
const properties = Object.fromEntries(fields.map(f => [f.handle, fieldSchema(f, locale)]));
|
|
88
|
+
const which = of > 1 ? ` (part ${part + 1} of ${of})` : '';
|
|
89
|
+
return jsonClosedObject({
|
|
90
|
+
[FieldsAnswerKey.Fields]: jsonClosedObject(properties, fields.map(f => f.handle), `Per field${which}: its new value, or its leave value.`),
|
|
91
|
+
[FieldsAnswerKey.Note]: { ...jsonText(), description: 'One short line for the owner on what changed, or what needs them.' },
|
|
92
|
+
}, Object.values(FieldsAnswerKey));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function fieldsJsonSchema(schema: ContentTypeSchema, options: FieldsSchemaOptions): FieldsSchemas {
|
|
96
|
+
const groups = split(chosen(assistFields(schema), options.fields), FIELDS_PER_PART);
|
|
97
|
+
const parts = groups.map((g, i) => partSchema(g, options.locale, i, groups.length));
|
|
98
|
+
parts.forEach((p, i) => assertAssistBudget(`fields ${schema.name} part ${i + 1}`, [p]));
|
|
99
|
+
return { parts, handles: groups.map(g => g.map(f => f.handle)) };
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** One field's answer as the model wrote it. */
|
|
103
|
+
export interface FieldEdit {
|
|
104
|
+
readonly handle: string;
|
|
105
|
+
readonly value: unknown;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
const isLeave = (v: unknown) => v === null || v === undefined || v === NONE_TEXT || (Array.isArray(v) && v.length === 0);
|
|
109
|
+
|
|
110
|
+
/** The parts' answers → the edits they make (every leave value dropped). */
|
|
111
|
+
export function decodeFieldsAnswers(answers: ReadonlyArray<unknown>): FieldEdit[] {
|
|
112
|
+
return answers.flatMap(answer => {
|
|
113
|
+
const fields = answer && typeof answer === 'object' ? (answer as Record<string, unknown>)[FieldsAnswerKey.Fields] : undefined;
|
|
114
|
+
if (!fields || typeof fields !== 'object' || Array.isArray(fields)) return [];
|
|
115
|
+
return Object.entries(fields as Record<string, unknown>).filter(([, v]) => !isLeave(v)).map(([handle, value]) => ({ handle, value }));
|
|
116
|
+
});
|
|
117
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @vectoriox/iox-ui/assist-core — the shared, FRAMEWORK-FREE vocabulary of AI Assist (iox-ai-guidance
|
|
3
|
+
* `architecture/ai/ai-assist-plan.md` §3b): the surfaces and their specs, the brief and its handle map, the route
|
|
4
|
+
* request, and family `fields` for surface S2 (one content item) — its brief (`summarizeItem`), its strict output format
|
|
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. Pure: no transport, no clock, no randomness.
|
|
7
|
+
* ⚠️ Not re-exported from the main entry. Consumers wire the usual trio — ng-package raw-ts assets, jest
|
|
8
|
+
* moduleNameMapper, tsconfig paths — or `nest start` dies with ERR_REQUIRE_ESM. It imports generation-core (the strict
|
|
9
|
+
* 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
|
+
*/
|
|
13
|
+
export * from './surfaces';
|
|
14
|
+
export * from './budget';
|
|
15
|
+
export * from './brief';
|
|
16
|
+
export * from './content-fields';
|
|
17
|
+
export * from './rich-text';
|
|
18
|
+
export * from './field-values';
|
|
19
|
+
export * from './summarize-item';
|
|
20
|
+
export * from './fields-schema';
|
|
21
|
+
export * from './route-schema';
|
|
22
|
+
export * from './field-edits';
|
|
23
|
+
export * from './assist-turn';
|
|
24
|
+
export * from './assist-locale';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Rich text crosses the model boundary as PLAIN PARAGRAPHS in both directions (trap T6): the brief reads the stored
|
|
3
|
+
* value (the item editor's Quill writes HTML) as paragraphs, and an answer's paragraphs become HTML only here, every
|
|
4
|
+
* character escaped — the model never writes markup that reaches a page.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
const BLOCK_END = /<\/(p|div|h[1-6]|li|blockquote|pre)>|<br\s*\/?>/gi;
|
|
8
|
+
const TAG = /<[^>]*>/g;
|
|
9
|
+
const HTML_TAG = /<\/?[a-z][a-z0-9-]*(\s[^<>]*)?\/?>/i;
|
|
10
|
+
|
|
11
|
+
const ENTITIES: Readonly<Record<string, string>> = { '&': '&', '<': '<', '>': '>', '"': '"', ''': "'", ' ': ' ' };
|
|
12
|
+
const ESCAPES: Readonly<Record<string, string>> = { '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' };
|
|
13
|
+
|
|
14
|
+
const decode = (text: string) => text.replace(/&(amp|lt|gt|quot|#39|nbsp);/g, e => ENTITIES[e] ?? e);
|
|
15
|
+
const escape = (text: string) => text.replace(/[&<>"']/g, c => ESCAPES[c]);
|
|
16
|
+
|
|
17
|
+
/** A stored rich-text value (HTML, or plain text) as its non-empty paragraphs. */
|
|
18
|
+
export function richTextParagraphs(value: unknown): string[] {
|
|
19
|
+
if (typeof value !== 'string') return [];
|
|
20
|
+
return decode(value.replace(BLOCK_END, '\n').replace(TAG, ''))
|
|
21
|
+
.split(/\n+/)
|
|
22
|
+
.map(p => p.replace(/\s+/g, ' ').trim())
|
|
23
|
+
.filter(Boolean);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Paragraphs as the editor's HTML: one `<p>` each, escaped. */
|
|
27
|
+
export function paragraphsToRichText(paragraphs: ReadonlyArray<string>): string {
|
|
28
|
+
return paragraphs.map(p => `<p>${escape(p.trim())}</p>`).join('');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** True when a text written by the model carries markup — refused, never stripped. */
|
|
32
|
+
export const containsHtml = (text: string): boolean => HTML_TAG.test(text);
|