@genesislcap/ai-assistant 15.37.0 → 15.38.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/dist/custom-elements.json +638 -0
- package/dist/dts/genesis/calendar.d.ts +25 -0
- package/dist/dts/genesis/calendar.d.ts.map +1 -0
- package/dist/dts/genesis/config.d.ts +16 -0
- package/dist/dts/genesis/config.d.ts.map +1 -1
- package/dist/dts/genesis/criteria.d.ts +24 -2
- package/dist/dts/genesis/criteria.d.ts.map +1 -1
- package/dist/dts/genesis/event-fields.d.ts +84 -0
- package/dist/dts/genesis/event-fields.d.ts.map +1 -0
- package/dist/dts/genesis/filter-fields.d.ts +69 -5
- package/dist/dts/genesis/filter-fields.d.ts.map +1 -1
- package/dist/dts/genesis/index.d.ts +3 -0
- package/dist/dts/genesis/index.d.ts.map +1 -1
- package/dist/dts/genesis/register-genesis-assistant.d.ts +13 -0
- package/dist/dts/genesis/register-genesis-assistant.d.ts.map +1 -1
- package/dist/dts/genesis/resource-tools.d.ts.map +1 -1
- package/dist/dts/genesis/review-model.d.ts +202 -0
- package/dist/dts/genesis/review-model.d.ts.map +1 -0
- package/dist/dts/genesis/row-source.d.ts +102 -0
- package/dist/dts/genesis/row-source.d.ts.map +1 -0
- package/dist/dts/genesis/serialisable.d.ts +25 -0
- package/dist/dts/genesis/serialisable.d.ts.map +1 -0
- package/dist/dts/genesis/server-text.d.ts +63 -0
- package/dist/dts/genesis/server-text.d.ts.map +1 -0
- package/dist/dts/genesis/type-name.d.ts +18 -0
- package/dist/dts/genesis/type-name.d.ts.map +1 -0
- package/dist/dts/genesis/types.d.ts +7 -0
- package/dist/dts/genesis/types.d.ts.map +1 -1
- package/dist/dts/genesis/value-compare.d.ts +19 -0
- package/dist/dts/genesis/value-compare.d.ts.map +1 -0
- package/dist/dts/genesis/write-commit.d.ts +53 -0
- package/dist/dts/genesis/write-commit.d.ts.map +1 -0
- package/dist/dts/genesis/write-outcome.d.ts +133 -0
- package/dist/dts/genesis/write-outcome.d.ts.map +1 -0
- package/dist/dts/genesis/write-payload.d.ts +129 -0
- package/dist/dts/genesis/write-payload.d.ts.map +1 -0
- package/dist/dts/genesis/write-plan.d.ts +109 -0
- package/dist/dts/genesis/write-plan.d.ts.map +1 -0
- package/dist/dts/genesis/write-review/write-review.d.ts +127 -0
- package/dist/dts/genesis/write-review/write-review.d.ts.map +1 -0
- package/dist/dts/genesis/write-review/write-review.styles.d.ts +2 -0
- package/dist/dts/genesis/write-review/write-review.styles.d.ts.map +1 -0
- package/dist/dts/genesis/write-review/write-review.template.d.ts +4 -0
- package/dist/dts/genesis/write-review/write-review.template.d.ts.map +1 -0
- package/dist/dts/genesis/write-tools.d.ts +99 -0
- package/dist/dts/genesis/write-tools.d.ts.map +1 -0
- package/dist/dts/react.d.ts +10 -0
- package/dist/esm/genesis/calendar.js +38 -0
- package/dist/esm/genesis/config.js +36 -1
- package/dist/esm/genesis/criteria.js +59 -55
- package/dist/esm/genesis/event-fields.js +158 -0
- package/dist/esm/genesis/filter-fields.js +111 -20
- package/dist/esm/genesis/index.js +2 -0
- package/dist/esm/genesis/register-genesis-assistant.js +76 -4
- package/dist/esm/genesis/resource-tools.js +21 -103
- package/dist/esm/genesis/review-model.js +248 -0
- package/dist/esm/genesis/row-source.js +127 -0
- package/dist/esm/genesis/serialisable.js +38 -0
- package/dist/esm/genesis/server-text.js +186 -0
- package/dist/esm/genesis/type-name.js +20 -0
- package/dist/esm/genesis/value-compare.js +87 -0
- package/dist/esm/genesis/write-commit.js +78 -0
- package/dist/esm/genesis/write-outcome.js +299 -0
- package/dist/esm/genesis/write-payload.js +632 -0
- package/dist/esm/genesis/write-plan.js +275 -0
- package/dist/esm/genesis/write-review/write-review.js +252 -0
- package/dist/esm/genesis/write-review/write-review.styles.js +214 -0
- package/dist/esm/genesis/write-review/write-review.template.js +182 -0
- package/dist/esm/genesis/write-tools.js +660 -0
- package/dist/react.cjs +7 -0
- package/dist/react.mjs +6 -0
- package/docs/genesis.md +152 -10
- package/package.json +18 -18
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import { utcMillisOf } from './calendar';
|
|
2
|
+
import { genesisTypeName } from './type-name';
|
|
3
|
+
import { sameValue } from './value-compare';
|
|
4
|
+
/**
|
|
5
|
+
* Epoch millis, the floor below which a "date" is not one.
|
|
6
|
+
*
|
|
7
|
+
* @remarks
|
|
8
|
+
* `20260921` as a NUMBER is 1970-01-01, not September 2026 — and a model writing a bare digit
|
|
9
|
+
* sequence unquoted is the likely form of that mistake, not an exotic one. Nothing downstream
|
|
10
|
+
* catches it: the `VALIDATE` pass checks the JSON type, not the range, so a 1970 date ACKs. So
|
|
11
|
+
* the number branch gets the same floor as the string one, and anything below it is refused.
|
|
12
|
+
*/
|
|
13
|
+
const EPOCH_MILLIS_FLOOR = 1e10;
|
|
14
|
+
/**
|
|
15
|
+
* Types a model may not SET: a value is only ever carried through, unchanged, from the row.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* `NANO_TIMESTAMP`'s unit has not been captured from a live app. It is written as an integer,
|
|
19
|
+
* and if that integer is nanoseconds, a date the model writes as millis lands in 1970, while one
|
|
20
|
+
* beyond 2^53 cannot travel as a JSON number exactly at all. It used to go through the date
|
|
21
|
+
* branch, so a modify of any OTHER field carried it from the row and wrote it back rounded —
|
|
22
|
+
* `…123` as `…000` — while the review showed nothing changing in it. Until the unit is known,
|
|
23
|
+
* the only safe write is the row's own value, exactly, and only when a number can hold it.
|
|
24
|
+
*
|
|
25
|
+
* The platform's own `TIMESTAMP` column never gets here: it is {@link PLATFORM_MAINTAINED}.
|
|
26
|
+
*
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
export const isCarryOnly = (field) => genesisTypeName(field.type) === 'NANO_TIMESTAMP';
|
|
30
|
+
/** What {@link toEpochMillis} returns for a date in the right shape that does not exist. */
|
|
31
|
+
const NO_SUCH_DATE = 'no-such-date';
|
|
32
|
+
/**
|
|
33
|
+
* ISO-ish date or datetime to epoch millis, UTC. Anchored: trailing text is a mistake.
|
|
34
|
+
*
|
|
35
|
+
* @remarks
|
|
36
|
+
* A date in the right shape that names no real instant is {@link NO_SUCH_DATE}, apart from a
|
|
37
|
+
* shape it does not read at all, because the two need different answers. "Must be YYYY-MM-DD"
|
|
38
|
+
* told about `2026-02-30`, which already is, sends the model round a loop.
|
|
39
|
+
*/
|
|
40
|
+
function toEpochMillis(value) {
|
|
41
|
+
var _a;
|
|
42
|
+
// Safe integers only, on both paths. A digit string beyond 2^53 goes through `Number` rounded,
|
|
43
|
+
// and the rounded instant is written as though it were the one given — the rounding the
|
|
44
|
+
// integer branch below was written to refuse, reached through the date branch instead.
|
|
45
|
+
if (typeof value === 'number') {
|
|
46
|
+
return Number.isSafeInteger(Math.trunc(value)) && Math.abs(value) >= EPOCH_MILLIS_FLOOR
|
|
47
|
+
? Math.trunc(value)
|
|
48
|
+
: undefined;
|
|
49
|
+
}
|
|
50
|
+
const text = String(value).trim();
|
|
51
|
+
// Epoch millis as digits, which is what a model echoing a previous read will send back.
|
|
52
|
+
if (/^\d+$/.test(text)) {
|
|
53
|
+
return Number.isSafeInteger(Number(text)) && Number(text) >= EPOCH_MILLIS_FLOOR
|
|
54
|
+
? Number(text)
|
|
55
|
+
: undefined;
|
|
56
|
+
}
|
|
57
|
+
const match = /^(\d{4})-(\d{2})-(\d{2})(?:[T ](\d{2}):(\d{2})(?::(\d{2}))?)?Z?$/.exec(text);
|
|
58
|
+
if (!match)
|
|
59
|
+
return undefined;
|
|
60
|
+
const [, year, month, day, hour, minute, second] = match;
|
|
61
|
+
// Not `Date.UTC` alone: it rolls a part that is out of range into the next one, so this used
|
|
62
|
+
// to write `2026-13-01` as 2027-01-01 and report it `written`. See utcMillisOf.
|
|
63
|
+
return (_a = utcMillisOf(year, month, day, hour, minute, second)) !== null && _a !== void 0 ? _a : NO_SUCH_DATE;
|
|
64
|
+
}
|
|
65
|
+
/** Genesis types that hold a whole number, whatever JSON type their metadata declares. */
|
|
66
|
+
const WHOLE_NUMBER_TYPES = new Set(['INT', 'INTEGER', 'LONG', 'SHORT', 'NANO_TIMESTAMP']);
|
|
67
|
+
/**
|
|
68
|
+
* Whether a field holds a whole number, by its GENESIS type — the one rule the payload builder
|
|
69
|
+
* and the tool schema both follow, so the schema never offers what the builder then refuses.
|
|
70
|
+
*
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
73
|
+
export const isWholeNumberType = (type) => WHOLE_NUMBER_TYPES.has(genesisTypeName(type));
|
|
74
|
+
/**
|
|
75
|
+
* The most significant digits a JavaScript number is guaranteed to hold exactly. A decimal with
|
|
76
|
+
* more cannot have come through `JSON.parse` as it was written.
|
|
77
|
+
*/
|
|
78
|
+
const EXACT_DIGITS = 15;
|
|
79
|
+
/**
|
|
80
|
+
* Whether a JSON NUMBER, for a field that travels as text, may already have been rounded.
|
|
81
|
+
*
|
|
82
|
+
* @remarks
|
|
83
|
+
* The model's arguments are JSON, and `JSON.parse` turns every number into a double BEFORE this
|
|
84
|
+
* module sees it: `9007199254740993` arrives as `9007199254740992`, and carrying its digits
|
|
85
|
+
* "verbatim" as text then writes the rounded value, exactly and confidently. What can be seen is
|
|
86
|
+
* refused — an integer part past 2^53, or more significant digits than a double holds. What
|
|
87
|
+
* cannot be seen is a long number that happened to round to a short one; the digits that would
|
|
88
|
+
* show it are gone before anything here runs, which is why the answer is always "send it as text".
|
|
89
|
+
*/
|
|
90
|
+
const mayHaveBeenRounded = (value) => {
|
|
91
|
+
if (!Number.isSafeInteger(Math.trunc(value)))
|
|
92
|
+
return true;
|
|
93
|
+
const significant = String(Math.abs(value)).split(/e/i)[0].replace('.', '').replace(/^0+/, '');
|
|
94
|
+
return significant.length > EXACT_DIGITS;
|
|
95
|
+
};
|
|
96
|
+
const sendAsText = (label) => `${label} came as a JSON number, which is rounded before it can be read exactly. Send it as text, e.g. "12345678901234567890"`;
|
|
97
|
+
/** Digits only, carried verbatim: a decimal must never be rounded through `Number`. */
|
|
98
|
+
const DECIMAL = /^[-+]?\d+(?:\.\d+)?$/;
|
|
99
|
+
/** Exponent form, which a JSON number may legally take and only a DOUBLE may accept. */
|
|
100
|
+
const EXPONENT = /^[-+]?\d+(?:\.\d+)?[eE][-+]?\d+$/;
|
|
101
|
+
/**
|
|
102
|
+
* How much of a value a refusal may quote back.
|
|
103
|
+
*
|
|
104
|
+
* @remarks
|
|
105
|
+
* These problems reach the MODEL, in the tool result and from there into the transcript, where
|
|
106
|
+
* they are billed again as input on every later turn — the same amplification the criteria
|
|
107
|
+
* value cap exists for, in the module that did not have one.
|
|
108
|
+
*
|
|
109
|
+
* And the value is not always the model's own. On a modify the merge carries the ROW, written by
|
|
110
|
+
* another user of this app, so an unbounded echo launders their text into a sentence of ours —
|
|
111
|
+
* out from under the `untrusted: true` stamp the read path puts on the same data. Truncating is
|
|
112
|
+
* right here where refusing is right in a criteria: this is a diagnostic echo, not the thing
|
|
113
|
+
* being acted on, and a bounded prefix still tells the model which value it got wrong.
|
|
114
|
+
*/
|
|
115
|
+
const MAX_QUOTED = 64;
|
|
116
|
+
const MAX_LISTED_VALUES = 20;
|
|
117
|
+
const quoted = (raw) => {
|
|
118
|
+
// The single quote goes, because our own sentences delimit with one: untrusted text carrying
|
|
119
|
+
// an apostrophe closes the quote early and the rest reads as ours. Bounding the length without
|
|
120
|
+
// neutralising the delimiter leaves the smaller half of the same problem.
|
|
121
|
+
const text = String(raw).replace(/'/g, '');
|
|
122
|
+
return text.length > MAX_QUOTED ? `${text.slice(0, MAX_QUOTED)}…` : text;
|
|
123
|
+
};
|
|
124
|
+
function valueFor(field, raw) {
|
|
125
|
+
var _a;
|
|
126
|
+
// Normalised ONCE, and the reason is the sharpest instance of a class this bridge has hit five
|
|
127
|
+
// times: a parameterised type. `DATETIME(3)` missed the date branch below and fell through to
|
|
128
|
+
// the integer case, where a bare `20260921` is all digits and a safe integer — so it was
|
|
129
|
+
// written as epoch millis, which is 1970-08-23. That is exactly what EPOCH_MILLIS_FLOOR exists
|
|
130
|
+
// to stop, reached around it. `BIGDECIMAL(20,4)` missed the decimal branch the same way and
|
|
131
|
+
// went out as raw text with no numeric check, while `value-compare` treats that same field as
|
|
132
|
+
// numeric — so the review's changed-mark and the write's validation disagreed about what kind
|
|
133
|
+
// of field it was.
|
|
134
|
+
const type = genesisTypeName(field.type);
|
|
135
|
+
const kind = field.jsonType || (type === 'BOOLEAN' ? 'boolean' : 'string');
|
|
136
|
+
const label = field.name;
|
|
137
|
+
// An enum is checked for membership and sent as text, which is what the platform's own
|
|
138
|
+
// metadata shows (SIDE: TYPE ENUM, JSON_TYPE string). A field carrying VALID_VALUES with a
|
|
139
|
+
// non-string JSON type has not been observed; if one turns up, this needs to fall through to
|
|
140
|
+
// the conversion below rather than send a number as a string.
|
|
141
|
+
if ((_a = field.validValues) === null || _a === void 0 ? void 0 : _a.length) {
|
|
142
|
+
const text = String(raw);
|
|
143
|
+
if (!field.validValues.includes(text)) {
|
|
144
|
+
return {
|
|
145
|
+
problem: `${label} must be one of ${field.validValues.slice(0, MAX_LISTED_VALUES).join(', ')}` +
|
|
146
|
+
`${field.validValues.length > MAX_LISTED_VALUES ? ', …' : ''} (got '${quoted(raw)}')`,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return { value: text };
|
|
150
|
+
}
|
|
151
|
+
// A DATE or DATETIME travels as an integer of epoch millis whatever its JSON type says.
|
|
152
|
+
//
|
|
153
|
+
// NANO_TIMESTAMP is NOT one of them any more (see isCarryOnly). Only a row's value reaches
|
|
154
|
+
// here for it — a model's is refused by the caller, which knows where a value came from — so
|
|
155
|
+
// there is nothing to convert, and it falls to the integer branch below: carried exactly when
|
|
156
|
+
// a number can hold it, refused when it cannot. Through this branch it was re-read as a date
|
|
157
|
+
// and rounded.
|
|
158
|
+
if (/^(DATE|DATETIME|LOCALDATE|LOCALDATETIME|TIMESTAMP)$/.test(type)) {
|
|
159
|
+
const millis = toEpochMillis(raw);
|
|
160
|
+
if (millis === NO_SUCH_DATE) {
|
|
161
|
+
return {
|
|
162
|
+
problem: `${label} is not a date that exists (got '${quoted(raw)}'): a month runs 01–12, a day ` +
|
|
163
|
+
"to the month's last, an hour 00–23, and a year starts at 0100",
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
if (millis === undefined) {
|
|
167
|
+
return {
|
|
168
|
+
problem: `${label} is a date and must be YYYY-MM-DD, YYYY-MM-DDTHH:MM or epoch millis (got '${quoted(raw)}')`,
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
return { value: millis };
|
|
172
|
+
}
|
|
173
|
+
// A whole-number GENESIS type is checked as one whatever JSON type the metadata declares for
|
|
174
|
+
// it. Branching on the JSON type alone meant a LONG — or a NANO_TIMESTAMP carried from the row —
|
|
175
|
+
// declared as a JSON `number` went through the DOUBLE branch's `Number` and was rounded past
|
|
176
|
+
// 2^53, the rounding the integer branch exists to refuse. Declared as text it travels as the
|
|
177
|
+
// digits it is, exactly; declared as any kind of number it must be a safe integer.
|
|
178
|
+
if (WHOLE_NUMBER_TYPES.has(type)) {
|
|
179
|
+
if (typeof raw === 'number' && !Number.isSafeInteger(raw))
|
|
180
|
+
return { problem: sendAsText(label) };
|
|
181
|
+
const text = String(raw).trim();
|
|
182
|
+
if (!/^[-+]?\d+$/.test(text)) {
|
|
183
|
+
return { problem: `${label} is a whole number (got '${quoted(raw)}')` };
|
|
184
|
+
}
|
|
185
|
+
if (kind === 'string')
|
|
186
|
+
return { value: text };
|
|
187
|
+
if (!Number.isSafeInteger(Number(text))) {
|
|
188
|
+
return {
|
|
189
|
+
problem: `${label} is ${quoted(text)}, which is too large to write exactly. Whole numbers must be within ${Number.MAX_SAFE_INTEGER}`,
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
return { value: Number(text) };
|
|
193
|
+
}
|
|
194
|
+
switch (kind) {
|
|
195
|
+
case 'integer': {
|
|
196
|
+
const text = String(raw).trim();
|
|
197
|
+
if (!/^[-+]?\d+$/.test(text)) {
|
|
198
|
+
return { problem: `${label} is a whole number (got '${quoted(raw)}')` };
|
|
199
|
+
}
|
|
200
|
+
// The mirror of the BigInt the READ path normalises: a Genesis LONG beyond 2^53 — an id,
|
|
201
|
+
// a sequence number, a nano timestamp, exactly the values a model echoes back from a
|
|
202
|
+
// previous read — would be rounded by Number, and a rounded id writes against the wrong
|
|
203
|
+
// record. A read loses nothing (it arrives as a BigInt and is stamped as text); a write
|
|
204
|
+
// would lose it silently, so it is refused instead of guessed at.
|
|
205
|
+
if (!Number.isSafeInteger(Number(text))) {
|
|
206
|
+
return {
|
|
207
|
+
problem: `${label} is ${quoted(text)}, which is too large to write exactly. Whole numbers must be within ${Number.MAX_SAFE_INTEGER}`,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
return { value: Number(text) };
|
|
211
|
+
}
|
|
212
|
+
case 'number': {
|
|
213
|
+
// A DOUBLE goes through `Number` anyway, so exponent form is no less exact here than it is
|
|
214
|
+
// in the JSON the model was shown — and the schema declares `type: 'number'`, which admits
|
|
215
|
+
// it. Refusing it was the schema promising something the handler would not take.
|
|
216
|
+
const text = String(raw).trim();
|
|
217
|
+
if (!DECIMAL.test(text) && !EXPONENT.test(text)) {
|
|
218
|
+
return { problem: `${label} is a number (got '${quoted(raw)}')` };
|
|
219
|
+
}
|
|
220
|
+
// Finite, or refused. `1e309` passes both patterns and is Infinity to `Number`, which
|
|
221
|
+
// JSON.stringify sends as `null`: the review showed "Infinity" and the write set the
|
|
222
|
+
// column NULL. The integer branch cannot reach this — a safe integer is finite by
|
|
223
|
+
// definition — so this is the one numeric branch that needed saying.
|
|
224
|
+
const value = Number(text);
|
|
225
|
+
if (!Number.isFinite(value)) {
|
|
226
|
+
return { problem: `${label} is ${quoted(text)}, which is too large to be a number` };
|
|
227
|
+
}
|
|
228
|
+
return { value };
|
|
229
|
+
}
|
|
230
|
+
case 'boolean': {
|
|
231
|
+
const text = String(raw).toLowerCase();
|
|
232
|
+
if (text !== 'true' && text !== 'false') {
|
|
233
|
+
return { problem: `${label} is true or false (got '${quoted(raw)}')` };
|
|
234
|
+
}
|
|
235
|
+
return { value: text === 'true' };
|
|
236
|
+
}
|
|
237
|
+
default: {
|
|
238
|
+
// A BIGDECIMAL lands here: JSON_TYPE 'string', and the digits go through untouched so
|
|
239
|
+
// nothing is rounded on the way.
|
|
240
|
+
if (type === 'BIGDECIMAL' || type === 'DECIMAL') {
|
|
241
|
+
if (typeof raw === 'number' && mayHaveBeenRounded(raw))
|
|
242
|
+
return { problem: sendAsText(label) };
|
|
243
|
+
const text = String(raw).trim();
|
|
244
|
+
if (!DECIMAL.test(text))
|
|
245
|
+
return { problem: `${label} is a number (got '${quoted(raw)}')` };
|
|
246
|
+
return { value: text };
|
|
247
|
+
}
|
|
248
|
+
return { value: String(raw) };
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Builds one event payload from a row the model supplied.
|
|
254
|
+
*
|
|
255
|
+
* @internal
|
|
256
|
+
*/
|
|
257
|
+
/**
|
|
258
|
+
* Columns the platform adds to every generated table and nobody models. Never sent, whatever
|
|
259
|
+
* the op: they are not a business key, and a model has no business setting either.
|
|
260
|
+
*
|
|
261
|
+
* Also left out of the approve-time staleness check: TIMESTAMP moves on every write, so
|
|
262
|
+
* comparing it would call a row stale because somebody else's unrelated write touched it.
|
|
263
|
+
*
|
|
264
|
+
* @internal
|
|
265
|
+
*/
|
|
266
|
+
export const PLATFORM_MAINTAINED = new Set(['RECORD_ID', 'TIMESTAMP']);
|
|
267
|
+
/**
|
|
268
|
+
* Whether a read-only field may be written for this op.
|
|
269
|
+
*
|
|
270
|
+
* @remarks
|
|
271
|
+
* An insert refuses them: a model-supplied key is how a write comes back `DUPLICATE_KEY`
|
|
272
|
+
* instead of inserting. A modify or delete must send the row's key, and the key is plausibly
|
|
273
|
+
* marked read-only — the insert metadata marks `TRADE_ID` so, and a modify takes the same
|
|
274
|
+
* payload type. The asymmetry decides it: refusing a field the server REQUIRES makes modify
|
|
275
|
+
* impossible and blames the model for our rule, while accepting one the server rejects produces
|
|
276
|
+
* a NACK the user can read. The loosening is also loose in front of a human — every field sent
|
|
277
|
+
* is rendered in the review and the row has to be selected — rather than behind one.
|
|
278
|
+
*
|
|
279
|
+
* What the loosening lets travel is the ROW's value, never the model's. On a modify a read-only
|
|
280
|
+
* field is carried from the row (see `sourceOf`), and a model value that differs is refused by
|
|
281
|
+
* name: keyed on another field, `changes: { TRADE_ID: 'SOMEONE_ELSE' }` used to go on the wire.
|
|
282
|
+
*
|
|
283
|
+
* A `custom` event stays strict: its semantics are the app's, so there is nothing to reason
|
|
284
|
+
* from, and its NACK will say what it wanted.
|
|
285
|
+
*/
|
|
286
|
+
const readOnlyAllowedFor = (op) => op === 'modify' || op === 'delete';
|
|
287
|
+
/**
|
|
288
|
+
* The fields this op may put on the wire: what the event declares, minus the platform's own
|
|
289
|
+
* columns, minus anything the op may not send.
|
|
290
|
+
*
|
|
291
|
+
* @remarks
|
|
292
|
+
* One rule, three readers — the pre-image merge, the "not a field of this event" message, and
|
|
293
|
+
* the review's columns — because a second copy of it is a second thing to get wrong, and the
|
|
294
|
+
* three disagreeing means a user is shown a set that is not the set being written.
|
|
295
|
+
*
|
|
296
|
+
* @internal
|
|
297
|
+
*/
|
|
298
|
+
export const sendableFields = (fields, op) => fields.filter((field) => !PLATFORM_MAINTAINED.has(field.name) && (!field.readOnly || readOnlyAllowedFor(op)));
|
|
299
|
+
/**
|
|
300
|
+
* What a delete sends: every field the delete event DECLARES, valued from the row — and which of
|
|
301
|
+
* its required fields the row has no value for.
|
|
302
|
+
*
|
|
303
|
+
* @remarks
|
|
304
|
+
* Taken from the ROW rather than from the model's search key, so that what is removed is the
|
|
305
|
+
* record the review showed — see the call site for what the difference costs.
|
|
306
|
+
*
|
|
307
|
+
* It used to keep only the fields the event marks READ-ONLY, on the reasoning that a key is
|
|
308
|
+
* read-only. On an INSERT it is (`TRADE_ID` is generated), and the fixtures modelled the delete
|
|
309
|
+
* on that capture. But a delete declared over an index — the showcase's
|
|
310
|
+
* `eventHandler<Trade.ById>("TRADE_DELETE")`, and a generated delete's shape — takes that index's
|
|
311
|
+
* fields as ordinary required inputs, not read-only ones. So every delete found no key, was
|
|
312
|
+
* refused with "this event declares no key field", and could not be ticked. Found by hand
|
|
313
|
+
* against the showcase backend.
|
|
314
|
+
*
|
|
315
|
+
* Every declared field instead: for an index event that is exactly the key, whatever its
|
|
316
|
+
* marking; and if a delete were ever declared over the whole entity, sending the whole row is
|
|
317
|
+
* still right, because every value is the row's own, never the model's.
|
|
318
|
+
*/
|
|
319
|
+
function deletePayloadOf(fields, preImage) {
|
|
320
|
+
const details = {};
|
|
321
|
+
const missing = [];
|
|
322
|
+
for (const field of sendableFields(fields, 'delete')) {
|
|
323
|
+
const value = preImage === null || preImage === void 0 ? void 0 : preImage[field.name];
|
|
324
|
+
if (value !== undefined && value !== null)
|
|
325
|
+
details[field.name] = value;
|
|
326
|
+
else if (!field.optional)
|
|
327
|
+
missing.push(field.name);
|
|
328
|
+
}
|
|
329
|
+
return { details, missing };
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* The part of a pre-image an event can carry. A value the event does not declare is not ours to
|
|
333
|
+
* echo back.
|
|
334
|
+
*/
|
|
335
|
+
function preImageFor(fields, preImage, op) {
|
|
336
|
+
const carried = {};
|
|
337
|
+
for (const field of sendableFields(fields, op)) {
|
|
338
|
+
const value = preImage[field.name];
|
|
339
|
+
if (value !== undefined && value !== null)
|
|
340
|
+
carried[field.name] = value;
|
|
341
|
+
}
|
|
342
|
+
return carried;
|
|
343
|
+
}
|
|
344
|
+
/**
|
|
345
|
+
* What a required field with no value is told. For a carry-only field "needs a value" would send
|
|
346
|
+
* the model to supply one, which is then refused — a loop it cannot leave — so it is said as the
|
|
347
|
+
* end of the road instead.
|
|
348
|
+
*/
|
|
349
|
+
const unsupplied = (field) => isCarryOnly(field)
|
|
350
|
+
? `${field.name} is required, and it is a ${genesisTypeName(field.type)}, which this ` +
|
|
351
|
+
'assistant does not set — so this row cannot be written here'
|
|
352
|
+
: `${field.name} needs a value`;
|
|
353
|
+
/**
|
|
354
|
+
* Text made only of characters that show nothing: whitespace (`\p{White_Space}`), format
|
|
355
|
+
* characters (`\p{Cf}` — the zero-width space U+200B, the word joiner U+2060, the soft hyphen
|
|
356
|
+
* U+00AD, the BOM), and the few letters that are drawn blank (the Hangul fillers U+115F, U+1160,
|
|
357
|
+
* U+3164 and U+FFA0, the Braille blank U+2800).
|
|
358
|
+
*
|
|
359
|
+
* @remarks
|
|
360
|
+
* `trim()` knows only whitespace, so a model told this tool cannot clear a column could write a
|
|
361
|
+
* zero-width space and do it anyway, while the review showed `+` with nothing after it. Built from
|
|
362
|
+
* a string because the package targets ES2015, and Unicode property escapes are ES2018 syntax;
|
|
363
|
+
* every browser the platform supports runs them.
|
|
364
|
+
*/
|
|
365
|
+
const BLANK_TEXT = new RegExp('^[\\p{White_Space}\\p{Cf}\\u115F\\u1160\\u2800\\u3164\\uFFA0]*$', 'u');
|
|
366
|
+
/**
|
|
367
|
+
* Whether text shows nothing at all. One test for the payload builder and the review, so what the
|
|
368
|
+
* review calls empty is exactly what the builder treats as a blank.
|
|
369
|
+
*
|
|
370
|
+
* @internal
|
|
371
|
+
*/
|
|
372
|
+
export const isBlankText = (text) => BLANK_TEXT.test(text);
|
|
373
|
+
/**
|
|
374
|
+
* Whether a value the model sent means "I am not setting this": an empty string, a null, or text
|
|
375
|
+
* that shows nothing (see {@link isBlankText}). On a modify the field keeps the row's value, so
|
|
376
|
+
* none of them can clear a column; on an insert the field is left out. See `sourceOf`.
|
|
377
|
+
*
|
|
378
|
+
* @internal
|
|
379
|
+
*/
|
|
380
|
+
export const isBlankEntry = (value) => value === null || (typeof value === 'string' && isBlankText(value));
|
|
381
|
+
/**
|
|
382
|
+
* Where one field's value comes from — the model, or the row being changed — and whether the
|
|
383
|
+
* model was allowed to supply it.
|
|
384
|
+
*
|
|
385
|
+
* @remarks
|
|
386
|
+
* Whether a value came from the ROW or from the model decides two things: what an empty string
|
|
387
|
+
* means, and who a problem with the value is addressed to. Both were wrong before.
|
|
388
|
+
*/
|
|
389
|
+
function sourceOf(field, given, changes, rowValue, merge, op) {
|
|
390
|
+
const raw = given[field.name];
|
|
391
|
+
// A delete is built from the row's own key (keyOfRow), so every value it sends is the ROW's —
|
|
392
|
+
// whether or not the model happened to name that field in the key it searched with. Asking
|
|
393
|
+
// "did the model send this name" blamed the model for the row's own value: a delete found by
|
|
394
|
+
// CLIENT_REF, of a row whose TRADE_ID a JSON number cannot hold, told the model its TRADE_ID
|
|
395
|
+
// was wrong, a field it never sent.
|
|
396
|
+
if (op === 'delete')
|
|
397
|
+
return { raw, fromRow: true };
|
|
398
|
+
const modelSent = field.name in changes;
|
|
399
|
+
// Only a merge has a row to fall back to, which is the single place that condition lives:
|
|
400
|
+
// the caller passes no rowValue otherwise.
|
|
401
|
+
const rowHas = rowValue !== undefined && rowValue !== null;
|
|
402
|
+
// A read-only field on a modify is the ROW's. readOnlyAllowedFor loosens the rule so the row's
|
|
403
|
+
// own key can travel, not so the model can set it: keyed on CLIENT_REF, a model's
|
|
404
|
+
// `TRADE_ID: 'SOMEONE_ELSE'` went on the wire with no problem raised, and on a replacing
|
|
405
|
+
// handler that is a write to a record nobody reviewed. write-plan's key-override check only
|
|
406
|
+
// catches it when the field is also the key. So the row's value is what is sent; an echo of it
|
|
407
|
+
// or a blank is the model not changing it; a different value is refused by name, rather than
|
|
408
|
+
// dropped under a `written` the model would then report as done. A required one the row has
|
|
409
|
+
// no value for is the row's failure, not the model's: the schema never offered it the field.
|
|
410
|
+
if (field.readOnly && merge) {
|
|
411
|
+
if (!rowHas && !field.optional) {
|
|
412
|
+
return {
|
|
413
|
+
raw: rowValue,
|
|
414
|
+
fromRow: true,
|
|
415
|
+
problem: `${field.name} has no value in the existing row — so this row cannot be written here`,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
// Only the model's value can differ: one it did not send IS the row's.
|
|
419
|
+
if (!isBlankEntry(raw) && !sameValue(raw, rowValue, field.type)) {
|
|
420
|
+
return {
|
|
421
|
+
raw,
|
|
422
|
+
fromRow: false,
|
|
423
|
+
problem: `${field.name} is set by the server and cannot be changed. Leave it out.`,
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
return { raw: rowValue, fromRow: true };
|
|
427
|
+
}
|
|
428
|
+
// A model echoing back the record it just read is the NORMAL case, not an exotic one, and an
|
|
429
|
+
// empty string — or a null — in that echo means "unchanged", not "clear this column". Taking
|
|
430
|
+
// it literally dropped the field, and under replace semantics the write then nulled it, with
|
|
431
|
+
// the review showing blank on both sides and nothing marked changed. So the row's value wins,
|
|
432
|
+
// which is what the merge is for.
|
|
433
|
+
//
|
|
434
|
+
// `null` is the same token as `''` here, deliberately. It used to fall to the omission rule,
|
|
435
|
+
// which dropped the field from the payload — so beside any other change, a null DID clear the
|
|
436
|
+
// column, while this comment said nothing could. Refusing it by name would catch the innocent
|
|
437
|
+
// echo of a column the row really holds as null. The tool description says both tokens leave a
|
|
438
|
+
// field as it is.
|
|
439
|
+
//
|
|
440
|
+
// The cost, stated rather than hidden: there is NO way for a model to clear a column. One
|
|
441
|
+
// token cannot mean both "leave this alone" and "empty it", and of the two readings only one
|
|
442
|
+
// is safe to guess at. An explicit token would be the way to add it back.
|
|
443
|
+
//
|
|
444
|
+
// A value of only WHITESPACE is a blank too. It used to be sent as it was: on a modify, a
|
|
445
|
+
// "change" whose two sides render identically, or a `' '` that in effect cleared the column
|
|
446
|
+
// this tool says it cannot clear; on an insert, spaces written into a column the review showed
|
|
447
|
+
// as empty. With no row value to keep, a blank is simply not set.
|
|
448
|
+
if (modelSent && isBlankEntry(raw)) {
|
|
449
|
+
return rowHas ? { raw: rowValue, fromRow: true } : { raw: '', fromRow: false };
|
|
450
|
+
}
|
|
451
|
+
// A carry-only field the MODEL set is refused, unless it is the row's own value echoed back
|
|
452
|
+
// exactly: a model restating the record it read is the normal case, and refusing that would
|
|
453
|
+
// cost a turn over a value nobody is changing. An empty string or nothing at all is the model
|
|
454
|
+
// not setting it, which is left to the omission rules.
|
|
455
|
+
if (isCarryOnly(field) && modelSent && raw !== undefined && raw !== null && raw !== '') {
|
|
456
|
+
if (rowHas && String(raw).trim() === String(rowValue))
|
|
457
|
+
return { raw: rowValue, fromRow: true };
|
|
458
|
+
return {
|
|
459
|
+
raw,
|
|
460
|
+
fromRow: false,
|
|
461
|
+
problem: `${field.name} is a ${genesisTypeName(field.type)}, which this assistant does not set: ` +
|
|
462
|
+
'its unit has not been confirmed, so a value written here could land decades away. ' +
|
|
463
|
+
'Leave it out.',
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
return { raw, fromRow: merge && !modelSent };
|
|
467
|
+
}
|
|
468
|
+
export function buildEventPayload(fields, row, { op, preImage } = {}) {
|
|
469
|
+
if (!row || typeof row !== 'object' || Array.isArray(row)) {
|
|
470
|
+
return {
|
|
471
|
+
status: 'rejected',
|
|
472
|
+
problems: ['each row must be an object of field names to values'],
|
|
473
|
+
};
|
|
474
|
+
}
|
|
475
|
+
// Merged BEFORE anything is validated or frozen, so what is checked, shown and committed is
|
|
476
|
+
// one object: re-deriving a payload after the review is the gap the review exists to close.
|
|
477
|
+
const changes = row;
|
|
478
|
+
// A modify or delete without the row it is changing cannot be built at all: under replace
|
|
479
|
+
// semantics the unnamed fields would be written as null. Saying so beats the required-field
|
|
480
|
+
// loop's "SYMBOL needs a value", which blames the model for a value it cannot supply — the
|
|
481
|
+
// row was never read.
|
|
482
|
+
if ((op === 'modify' || op === 'delete') && !preImage) {
|
|
483
|
+
return {
|
|
484
|
+
status: 'rejected',
|
|
485
|
+
problems: [`a ${op} must be based on a row that was read first`],
|
|
486
|
+
};
|
|
487
|
+
}
|
|
488
|
+
// Merged for a MODIFY only.
|
|
489
|
+
//
|
|
490
|
+
// The merge exists because a generated modify replaces the record, so a payload carrying only
|
|
491
|
+
// the named fields writes null over the rest. A delete has no such semantics: the showcase's
|
|
492
|
+
// own working delete sends `DETAILS: { TRADE_ID }` and nothing else, and a generated handler
|
|
493
|
+
// takes the key. Merging there made a delete fail over a column it never sends — a BIGDECIMAL
|
|
494
|
+
// the row holds empty, a LONG beyond 2^53 — and made the row source's coverage check demand
|
|
495
|
+
// fields a delete has no use for.
|
|
496
|
+
//
|
|
497
|
+
// An insert never inherits either, even if a caller hands it a pre-image: a new record must
|
|
498
|
+
// contain what the user saw proposed and nothing else.
|
|
499
|
+
const merge = !!preImage && op === 'modify';
|
|
500
|
+
// A delete is its key: `changes` is meaningless on one, and the rest of the record is not
|
|
501
|
+
// something it sends.
|
|
502
|
+
// A DELETE is built from the ROW'S OWN key, never from the key the model searched with.
|
|
503
|
+
//
|
|
504
|
+
// Those are not the same thing and the difference destroys records. A table keyed by
|
|
505
|
+
// (ACCOUNT_ID, INSTRUMENT_ID), a model sending `{key: {ACCOUNT_ID: 'A1'}}`, an account holding
|
|
506
|
+
// exactly one position: the read resolves one row, the staleness check agrees, the review shows
|
|
507
|
+
// that position, the user approves removing it — and `DETAILS: {ACCOUNT_ID: 'A1'}` goes on the
|
|
508
|
+
// wire. A handler that resolves a delete from the fields it is given removes every position on
|
|
509
|
+
// the account. The user approved one row and a different object was sent.
|
|
510
|
+
//
|
|
511
|
+
// What is sent is every field the delete event declares, read out of the row (see
|
|
512
|
+
// deletePayloadOf). A required one the row has no value for is refused by name, rather than
|
|
513
|
+
// sending a partial key — the failure worth having.
|
|
514
|
+
const removal = op === 'delete' ? deletePayloadOf(fields, preImage) : undefined;
|
|
515
|
+
if (removal === null || removal === void 0 ? void 0 : removal.missing.length) {
|
|
516
|
+
return {
|
|
517
|
+
status: 'rejected',
|
|
518
|
+
problems: [
|
|
519
|
+
`${removal.missing.join(', ')} names the row being removed, and the row read has no ` +
|
|
520
|
+
'value for it — so this row cannot be removed here',
|
|
521
|
+
],
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
const given = removal
|
|
525
|
+
? removal.details
|
|
526
|
+
: merge
|
|
527
|
+
? Object.assign(Object.assign({}, preImageFor(fields, preImage, op)), changes) : changes;
|
|
528
|
+
if (op === 'delete' && !Object.keys(given).length) {
|
|
529
|
+
return {
|
|
530
|
+
status: 'rejected',
|
|
531
|
+
problems: [
|
|
532
|
+
'the row holds no value for any field this delete takes, so there is no way to name ' +
|
|
533
|
+
'the row being removed',
|
|
534
|
+
],
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
const byName = new Map(fields.map((field) => [field.name, field]));
|
|
538
|
+
const problems = [];
|
|
539
|
+
const details = {};
|
|
540
|
+
for (const name of Object.keys(given)) {
|
|
541
|
+
const field = byName.get(name);
|
|
542
|
+
if (!field) {
|
|
543
|
+
problems.push(`${name} is not a field of this event. Its fields are: ${sendableFields(fields, op)
|
|
544
|
+
.map((f) => f.name)
|
|
545
|
+
.join(', ')}`);
|
|
546
|
+
continue;
|
|
547
|
+
}
|
|
548
|
+
if (PLATFORM_MAINTAINED.has(name)) {
|
|
549
|
+
problems.push(`${name} is maintained by the platform and is not sent`);
|
|
550
|
+
continue;
|
|
551
|
+
}
|
|
552
|
+
if (field.readOnly && !readOnlyAllowedFor(op)) {
|
|
553
|
+
problems.push(`${name} is set by the server and cannot be written`);
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
const source = sourceOf(field, given, changes, merge ? preImage === null || preImage === void 0 ? void 0 : preImage[name] : undefined, merge, op);
|
|
557
|
+
if (source.problem) {
|
|
558
|
+
problems.push(source.problem);
|
|
559
|
+
continue;
|
|
560
|
+
}
|
|
561
|
+
const { raw, fromRow } = source;
|
|
562
|
+
// An empty string the ROW holds is a VALUE, not an omission. Dropping it wrote null over it
|
|
563
|
+
// under replace semantics — the blanking the merge exists to prevent, reached through the
|
|
564
|
+
// one value the row source's coverage check cannot see, and invisible in the review because
|
|
565
|
+
// both sides rendered blank and nothing was marked changed. A model-supplied empty stays an
|
|
566
|
+
// omission: there it means "I am not setting this".
|
|
567
|
+
if (raw === undefined || raw === null || (raw === '' && !fromRow)) {
|
|
568
|
+
// Addressed to the model, and only ever the model: a value the ROW failed to supply never
|
|
569
|
+
// reaches here, because the merge does not carry undefined or null and an empty string
|
|
570
|
+
// from the row is now kept. A field the row is missing altogether is caught by the
|
|
571
|
+
// required-field loop below, which attributes it there.
|
|
572
|
+
if (!field.optional)
|
|
573
|
+
problems.push(unsupplied(field));
|
|
574
|
+
continue;
|
|
575
|
+
}
|
|
576
|
+
const { value, problem } = valueFor(field, raw);
|
|
577
|
+
if (problem) {
|
|
578
|
+
// Attribute it correctly. A value carried from the pre-image is the SERVER's own — most
|
|
579
|
+
// often a LONG beyond 2^53 that the read stamped as text and a JSON integer cannot carry
|
|
580
|
+
// — so blaming the model for a field it never supplied sends it round a loop it cannot
|
|
581
|
+
// win. The row simply cannot be written through this tool.
|
|
582
|
+
//
|
|
583
|
+
// By where the value CAME FROM, not by whether the model named the field: an echo the
|
|
584
|
+
// model sent back is the row's own value once it is taken as one, and blaming the model
|
|
585
|
+
// for it sends it to fix what it cannot.
|
|
586
|
+
problems.push(fromRow
|
|
587
|
+
? `${problem.replace(/^(\S+) /, '$1 in the existing row ')} — so this row cannot be written here`
|
|
588
|
+
: problem);
|
|
589
|
+
}
|
|
590
|
+
else {
|
|
591
|
+
details[field.name] = value;
|
|
592
|
+
}
|
|
593
|
+
}
|
|
594
|
+
// Skipped for a delete: "every mandatory field needs a value" is a rule about a record being
|
|
595
|
+
// written, and a delete writes none — it names one.
|
|
596
|
+
//
|
|
597
|
+
// A read-only field counts on a merge: it is carried from the row (see `sourceOf`), so a
|
|
598
|
+
// required one the row has no value for went out missing, with nothing said, whenever the model
|
|
599
|
+
// did not happen to name it. On an insert the server sets it, so it is not asked for.
|
|
600
|
+
for (const field of op === 'delete' ? [] : fields) {
|
|
601
|
+
if (!field.optional &&
|
|
602
|
+
(!field.readOnly || merge) &&
|
|
603
|
+
!PLATFORM_MAINTAINED.has(field.name) &&
|
|
604
|
+
!(field.name in details)) {
|
|
605
|
+
if (!problems.some((problem) => problem.startsWith(`${field.name} `))) {
|
|
606
|
+
// Attributed the same way as above: on a modify the field is one the ROW failed to
|
|
607
|
+
// supply, and "SYMBOL needs a value" would send the model looking for a value it never
|
|
608
|
+
// had and cannot get.
|
|
609
|
+
problems.push(merge
|
|
610
|
+
? `${field.name} has no value in the existing row — so this row cannot be written here`
|
|
611
|
+
: unsupplied(field));
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
// An empty payload is not a row. On an all-optional event the required-field loop finds
|
|
616
|
+
// nothing to complain about, so `{}` passed: the review rendered a blank row, a user approved
|
|
617
|
+
// a row that said nothing, and `DETAILS: {}` went to the server. Every guarantee in this
|
|
618
|
+
// module is about WHICH values travel, and none of them notices when none do.
|
|
619
|
+
if (!problems.length && !Object.keys(details).length) {
|
|
620
|
+
problems.push('this row has no values, so there is nothing to write');
|
|
621
|
+
}
|
|
622
|
+
// Frozen: the review shows this object and the commit sends this object, so "the same one"
|
|
623
|
+
// should also mean "an unchanged one". Anything trying to adjust a payload between the two
|
|
624
|
+
// throws here instead of quietly committing something the user never read.
|
|
625
|
+
//
|
|
626
|
+
// The freeze is SHALLOW, which is sufficient only because every value `valueFor` returns is a
|
|
627
|
+
// scalar — a string, a number or a boolean. If a field type ever produces a nested value, the
|
|
628
|
+
// freeze has to go deep or the guarantee quietly weakens to the top level.
|
|
629
|
+
return problems.length
|
|
630
|
+
? { status: 'rejected', problems }
|
|
631
|
+
: { status: 'ok', details: Object.freeze(details) };
|
|
632
|
+
}
|