@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.
Files changed (73) hide show
  1. package/dist/custom-elements.json +638 -0
  2. package/dist/dts/genesis/calendar.d.ts +25 -0
  3. package/dist/dts/genesis/calendar.d.ts.map +1 -0
  4. package/dist/dts/genesis/config.d.ts +16 -0
  5. package/dist/dts/genesis/config.d.ts.map +1 -1
  6. package/dist/dts/genesis/criteria.d.ts +24 -2
  7. package/dist/dts/genesis/criteria.d.ts.map +1 -1
  8. package/dist/dts/genesis/event-fields.d.ts +84 -0
  9. package/dist/dts/genesis/event-fields.d.ts.map +1 -0
  10. package/dist/dts/genesis/filter-fields.d.ts +69 -5
  11. package/dist/dts/genesis/filter-fields.d.ts.map +1 -1
  12. package/dist/dts/genesis/index.d.ts +3 -0
  13. package/dist/dts/genesis/index.d.ts.map +1 -1
  14. package/dist/dts/genesis/register-genesis-assistant.d.ts +13 -0
  15. package/dist/dts/genesis/register-genesis-assistant.d.ts.map +1 -1
  16. package/dist/dts/genesis/resource-tools.d.ts.map +1 -1
  17. package/dist/dts/genesis/review-model.d.ts +202 -0
  18. package/dist/dts/genesis/review-model.d.ts.map +1 -0
  19. package/dist/dts/genesis/row-source.d.ts +102 -0
  20. package/dist/dts/genesis/row-source.d.ts.map +1 -0
  21. package/dist/dts/genesis/serialisable.d.ts +25 -0
  22. package/dist/dts/genesis/serialisable.d.ts.map +1 -0
  23. package/dist/dts/genesis/server-text.d.ts +63 -0
  24. package/dist/dts/genesis/server-text.d.ts.map +1 -0
  25. package/dist/dts/genesis/type-name.d.ts +18 -0
  26. package/dist/dts/genesis/type-name.d.ts.map +1 -0
  27. package/dist/dts/genesis/types.d.ts +7 -0
  28. package/dist/dts/genesis/types.d.ts.map +1 -1
  29. package/dist/dts/genesis/value-compare.d.ts +19 -0
  30. package/dist/dts/genesis/value-compare.d.ts.map +1 -0
  31. package/dist/dts/genesis/write-commit.d.ts +53 -0
  32. package/dist/dts/genesis/write-commit.d.ts.map +1 -0
  33. package/dist/dts/genesis/write-outcome.d.ts +133 -0
  34. package/dist/dts/genesis/write-outcome.d.ts.map +1 -0
  35. package/dist/dts/genesis/write-payload.d.ts +129 -0
  36. package/dist/dts/genesis/write-payload.d.ts.map +1 -0
  37. package/dist/dts/genesis/write-plan.d.ts +109 -0
  38. package/dist/dts/genesis/write-plan.d.ts.map +1 -0
  39. package/dist/dts/genesis/write-review/write-review.d.ts +127 -0
  40. package/dist/dts/genesis/write-review/write-review.d.ts.map +1 -0
  41. package/dist/dts/genesis/write-review/write-review.styles.d.ts +2 -0
  42. package/dist/dts/genesis/write-review/write-review.styles.d.ts.map +1 -0
  43. package/dist/dts/genesis/write-review/write-review.template.d.ts +4 -0
  44. package/dist/dts/genesis/write-review/write-review.template.d.ts.map +1 -0
  45. package/dist/dts/genesis/write-tools.d.ts +99 -0
  46. package/dist/dts/genesis/write-tools.d.ts.map +1 -0
  47. package/dist/dts/react.d.ts +10 -0
  48. package/dist/esm/genesis/calendar.js +38 -0
  49. package/dist/esm/genesis/config.js +36 -1
  50. package/dist/esm/genesis/criteria.js +59 -55
  51. package/dist/esm/genesis/event-fields.js +158 -0
  52. package/dist/esm/genesis/filter-fields.js +111 -20
  53. package/dist/esm/genesis/index.js +2 -0
  54. package/dist/esm/genesis/register-genesis-assistant.js +76 -4
  55. package/dist/esm/genesis/resource-tools.js +21 -103
  56. package/dist/esm/genesis/review-model.js +248 -0
  57. package/dist/esm/genesis/row-source.js +127 -0
  58. package/dist/esm/genesis/serialisable.js +38 -0
  59. package/dist/esm/genesis/server-text.js +186 -0
  60. package/dist/esm/genesis/type-name.js +20 -0
  61. package/dist/esm/genesis/value-compare.js +87 -0
  62. package/dist/esm/genesis/write-commit.js +78 -0
  63. package/dist/esm/genesis/write-outcome.js +299 -0
  64. package/dist/esm/genesis/write-payload.js +632 -0
  65. package/dist/esm/genesis/write-plan.js +275 -0
  66. package/dist/esm/genesis/write-review/write-review.js +252 -0
  67. package/dist/esm/genesis/write-review/write-review.styles.js +214 -0
  68. package/dist/esm/genesis/write-review/write-review.template.js +182 -0
  69. package/dist/esm/genesis/write-tools.js +660 -0
  70. package/dist/react.cjs +7 -0
  71. package/dist/react.mjs +6 -0
  72. package/docs/genesis.md +152 -10
  73. package/package.json +18 -18
@@ -0,0 +1,299 @@
1
+ import { logger } from '../utils/logger';
2
+ import { serialisable } from './serialisable';
3
+ import { plainFieldName, plainServerText, readsLikeFieldName, readsLikeKeyValue, } from './server-text';
4
+ /**
5
+ * Whether a MSG_NACK error says the event is not mapped to a service — the one MSG_NACK that
6
+ * means nothing ran. Live 8.15: `{"CODE": 404, "TEXT": "Resource EVENT_X is not mapped to a
7
+ * service", "STATUS_CODE": "404 Not Found"}`.
8
+ */
9
+ const NOT_MAPPED = 404;
10
+ const isNotMapped = (error) => { var _a; return error.CODE === NOT_MAPPED || String((_a = error.STATUS_CODE) !== null && _a !== void 0 ? _a : '').startsWith(String(NOT_MAPPED)); };
11
+ /**
12
+ * A server error's own words, when they can be shown.
13
+ *
14
+ * @remarks
15
+ * Every message this module builds funnels through here, so the write path gets the treatment
16
+ * the read path already gives a refusal: an app's own "Counterparty is required" is the most
17
+ * useful thing in the reply and travels unchanged, while a stack trace or a SQL code is
18
+ * replaced and logged. The reply reaches a model vendor and a person's screen alike, and a
19
+ * write is no reason for it to travel further than a read.
20
+ */
21
+ const textOf = (error) => plainServerText(error === null || error === void 0 ? void 0 : error.TEXT, 'a write was refused', 'no reason that can be shown here');
22
+ /**
23
+ * The field a server error names, when it can be shown.
24
+ *
25
+ * @remarks
26
+ * `FIELD` is not a field name in the sense the word suggests: on a TableStandardError it is an
27
+ * index name and a value (`TRADE_BY_ID -> T-001`), and it is another place the platform can put
28
+ * whatever it likes — a DUPLICATE_KEY has carried a class name, an inner class and a file and
29
+ * line. Sanitising only TEXT left this half open through the same mapper.
30
+ */
31
+ const fieldOf = (error) => plainFieldName(error === null || error === void 0 ? void 0 : error.FIELD, 'a write named a field');
32
+ const listOf = (value) => Array.isArray(value) ? value.filter(Boolean) : [];
33
+ const warningsOf = (reply) => {
34
+ const all = listOf(reply.WARNING);
35
+ const warnings = all.slice(0, MAX_REASONS).map(textOf);
36
+ if (all.length > MAX_REASONS)
37
+ warnings.push(`and ${all.length - MAX_REASONS} more`);
38
+ return warnings.length ? warnings : undefined;
39
+ };
40
+ /**
41
+ * How many reasons one message carries.
42
+ *
43
+ * @remarks
44
+ * A wide table's schema NACK names every column, which composed a message of tens of kilobytes —
45
+ * and it lands in the tool result, is stringified into the transcript, and is billed again as
46
+ * input on every later turn. The same amplification the filter-value cap exists for, one module
47
+ * along. Ten is more than a person reads before fixing the first one.
48
+ */
49
+ const MAX_REASONS = 10;
50
+ /**
51
+ * The keys a write GENERATED, as far as they can be shown, and how many could not be.
52
+ *
53
+ * @remarks
54
+ * `GENERATED` is server output like any other, and it went to the model raw: a 300-character
55
+ * value composed a 318-character message, a class name for a key came out verbatim, and nothing
56
+ * bounded how many there were. Worse, it skipped {@link serialisable}: comms reads an integer
57
+ * beyond 2^53 as a BigInt, and the chat driver stringifies a tool result INSIDE the handler's
58
+ * try — so one such key would have turned a committed write, and every other row of its batch,
59
+ * into "Tool error… you may retry it". That retry is the double write `unknown` exists to stop.
60
+ *
61
+ * So each entry is kept only when its key is a Genesis identifier and its value is a string or
62
+ * a number that reads as a key value (the rule a duplicate-key `FIELD` is held to), up to
63
+ * {@link MAX_REASONS} of them. What is carried in `generated` is exactly what the message shows:
64
+ * cleaning only the message would leave the raw value one field along. Anything dropped is
65
+ * logged, and the message says so rather than implying there was nothing.
66
+ */
67
+ const generatedOf = (raw) => {
68
+ const generated = [];
69
+ let kept = 0;
70
+ let hidden = 0;
71
+ const unshowable = (what) => {
72
+ hidden += 1;
73
+ logger.error(`Genesis assistant: a write generated a value it cannot show: ${what}`);
74
+ };
75
+ for (const row of Array.isArray(raw) ? serialisable(raw) : []) {
76
+ if (!row || typeof row !== 'object' || Array.isArray(row)) {
77
+ unshowable(JSON.stringify(row));
78
+ continue;
79
+ }
80
+ const clean = {};
81
+ for (const [key, value] of Object.entries(row)) {
82
+ const plain = (typeof value === 'string' || typeof value === 'number') &&
83
+ readsLikeFieldName(key) &&
84
+ readsLikeKeyValue(String(value));
85
+ if (plain && kept < MAX_REASONS) {
86
+ clean[key] = value;
87
+ kept += 1;
88
+ }
89
+ else {
90
+ unshowable(`${key} = ${JSON.stringify(value)}`);
91
+ }
92
+ }
93
+ if (Object.keys(clean).length)
94
+ generated.push(clean);
95
+ }
96
+ return { generated, hidden };
97
+ };
98
+ /** Joins several reasons into one line without losing any of them. */
99
+ const joined = (errors) => errors
100
+ .slice(0, MAX_REASONS)
101
+ .map((error) => {
102
+ var _a;
103
+ const named = fieldOf(error);
104
+ if (!named)
105
+ return textOf(error);
106
+ // Same distinction as above: `COUNTERPARTY_ID: required` reads as a field, while
107
+ // `TRADE_BY_ID -> T-001` is an index and belongs after the reason, not in front of it.
108
+ return String((_a = error['@type']) !== null && _a !== void 0 ? _a : '') === 'FieldError'
109
+ ? `${named}: ${textOf(error)}`
110
+ : `${textOf(error)} (${named})`;
111
+ })
112
+ .join('; ')
113
+ .concat(errors.length > MAX_REASONS ? `; and ${errors.length - MAX_REASONS} more` : '');
114
+ /**
115
+ * Reads one commit or `VALIDATE` reply into an outcome.
116
+ *
117
+ * @remarks
118
+ * Never throws, and never guesses: a reply it does not recognise is `unknown`, naming the
119
+ * reply's message type, rather than an optimistic "written".
120
+ *
121
+ * @internal
122
+ */
123
+ export function mapWriteReply(reply, { mode, op }) {
124
+ var _a;
125
+ // This function only ever sees a reply to a row that was SENT. So a reply it cannot read is
126
+ // not a failure and not an unavailable service — it is the one case where "we do not know
127
+ // whether this was written" is simply the truth, and `unknown` is the status that says so.
128
+ //
129
+ // The distinction is not cosmetic. `unknown` is the one outcome the system prompt tells the
130
+ // model never to retry, because a retry can write the row twice. `failed` and `unavailable`
131
+ // both carry `committed: false` with nothing to warn against trying again — which is the
132
+ // double write the `unknown` status was introduced to prevent, reached through the one path
133
+ // that had not been routed through it.
134
+ if (!reply || typeof reply !== 'object') {
135
+ return writeOutcomeUnknown('The app answered with nothing, so whether this was written is unknown.');
136
+ }
137
+ const message = reply;
138
+ // MESSAGE_TYPE is a platform identifier — EVENT_ACK, EVENT_NACK, MSG_NACK — so it is read as
139
+ // one. It is compared below AND printed at the end, so it must not go to the model raw and
140
+ // unbounded, the rule every server string here follows (GENERATED included: see generatedOf).
141
+ const type = String((_a = message.MESSAGE_TYPE) !== null && _a !== void 0 ? _a : '');
142
+ const namedType = plainFieldName(type, 'a reply named a message type');
143
+ const errors = listOf(message.ERROR);
144
+ const warnings = warningsOf(message);
145
+ if (type === 'EVENT_ACK') {
146
+ // The mode is the whole difference. A dry run ACKs without writing anything, and on a
147
+ // generated app it ACKs whatever it is given, so calling that "written" would be the lie
148
+ // the review table is built to avoid.
149
+ if (mode === 'validate') {
150
+ return Object.assign({ status: 'checked', committed: false, message: 'Checked.' }, (warnings ? { warnings } : {}));
151
+ }
152
+ const { generated, hidden } = generatedOf(message.GENERATED);
153
+ const keys = generated.flatMap((row) => Object.entries(row));
154
+ // The generated key is worth carrying: "written, TRADE_ID …" tells the model what it made.
155
+ const said = [
156
+ ...(keys.length ? [keys.map(([key, value]) => `${key} ${String(value)}`).join(', ')] : []),
157
+ ...(hidden ? [`${hidden} generated value(s) cannot be shown here`] : []),
158
+ ];
159
+ return Object.assign(Object.assign({ status: 'written', committed: true, message: said.length ? `Written. ${said.join('; ')}` : 'Written.' }, (generated.length ? { generated } : {})), (warnings ? { warnings } : {}));
160
+ }
161
+ if (!errors.length) {
162
+ // A NACK carrying only warnings is a shape the platform DECLARES (every NACK has a WARNING
163
+ // list) though the showcase's handlers never produced one. It is perfectly readable: the
164
+ // app declined and said why, so say that rather than "this is not a result this can read".
165
+ if (type === 'EVENT_NACK' && (warnings === null || warnings === void 0 ? void 0 : warnings.length)) {
166
+ return {
167
+ status: 'refused_by_app',
168
+ committed: false,
169
+ message: `The app did not accept it: ${warnings.join('; ')}`,
170
+ warnings,
171
+ };
172
+ }
173
+ // Unreadable, not refused: an ACK in a shape this module has not seen is still an ACK, and
174
+ // a future platform version adding one would otherwise have its successful writes reported
175
+ // as not written — and re-proposed.
176
+ return Object.assign(Object.assign({}, writeOutcomeUnknown(`The app answered with ${namedType !== null && namedType !== void 0 ? namedType : 'no message type'}, which this cannot read, so ` +
177
+ 'whether this was written is unknown.')), (warnings ? { warnings } : {}));
178
+ }
179
+ // Errors are read as a refusal only inside an envelope that CAN be one. EVENT_NACK is a
180
+ // handler's no; MSG_NACK is the router's, and only its 404 is a definite one (below). An ERROR
181
+ // list under any other type, or under none, is not known to mean "not written". A future ACK that reports a partial problem, or some other
182
+ // layer's error shape, was classified by its contents as refused or invalid, both of which
183
+ // say `committed: false` with nothing warning against a retry. That retry is how a row gets
184
+ // written twice, the case `unknown` exists for.
185
+ if (type !== 'EVENT_NACK' && type !== 'MSG_NACK') {
186
+ return Object.assign(Object.assign({}, writeOutcomeUnknown(`The app answered with ${namedType !== null && namedType !== void 0 ? namedType : 'no message type'} and an error list, which this ` +
187
+ 'cannot read as a refusal, so whether this was written is unknown.')), (warnings ? { warnings } : {}));
188
+ }
189
+ // `FIELD` is a COLUMN on a FieldError, but on a TableStandardError it is an index name and
190
+ // the offending value ("TRADE_BY_ID -> T-001"). Carrying it as a field either way would
191
+ // highlight a cell called TRADE_BY_ID on every duplicate-key case, which is not a column the
192
+ // user ever typed into. Only a FieldError names a field; the rest goes into the text.
193
+ //
194
+ // Capped like the message and the warnings, with the same tail: `fields` is copied into the
195
+ // tool result beside the message, so a 40-error NACK carried all 40 texts there after the
196
+ // message had been cut to ten. Only the OUTPUT is sliced. What decides the status below reads
197
+ // every error, or a DUPLICATE_KEY at position eleven would stop being one.
198
+ const fields = errors
199
+ .slice(0, MAX_REASONS)
200
+ .map((error) => {
201
+ var _a;
202
+ const isFieldError = String((_a = error['@type']) !== null && _a !== void 0 ? _a : '') === 'FieldError';
203
+ if (isFieldError) {
204
+ const named = fieldOf(error);
205
+ return Object.assign(Object.assign({}, (named ? { field: named } : {})), { text: textOf(error) });
206
+ }
207
+ const named = fieldOf(error);
208
+ return { text: named ? `${textOf(error)} (${named})` : textOf(error) };
209
+ });
210
+ if (errors.length > MAX_REASONS)
211
+ fields.push({ text: `and ${errors.length - MAX_REASONS} more` });
212
+ const withFields = Object.assign({ committed: false, fields }, (warnings ? { warnings } : {}));
213
+ const codes = new Set(errors.map((error) => { var _a; return String((_a = error.CODE) !== null && _a !== void 0 ? _a : ''); }));
214
+ const types = new Set(errors.map((error) => { var _a; return String((_a = error['@type']) !== null && _a !== void 0 ? _a : ''); }));
215
+ // The MSG_NACK envelope: numeric CODE, no @type. Only a 404 says nothing ran — the event is not
216
+ // mapped to a service, a KNOWN "not written". Every MSG_NACK was read that way, and a 500, 503
217
+ // or 504 can arrive AFTER the handler committed, so "not written" invited the retry that writes
218
+ // a row twice. That became reachable for every socket write once a rejected MSG_NACK was read
219
+ // as a reply (020b3687d3); before it, a socket MSG_NACK was `unknown`. Only the code is named:
220
+ // it is a number, where the text is the server's own.
221
+ //
222
+ // Keyed on the envelope, not on STATUS_CODE: an EVENT_NACK's errors carry one too — `400 Bad
223
+ // Request` on a FieldError, `500` on a DUPLICATE_KEY — and are the app's own answer.
224
+ //
225
+ // Checked BEFORE any code or type below, which all describe a handler's answer. Placed after
226
+ // them, a MSG_NACK carrying a string DUPLICATE_KEY code, a TableStandardError @type or, on a
227
+ // modify, a RECORD_NOT_FOUND was read as a definite refusal and never reached `unknown` — and
228
+ // after an insert that did commit, the retry that invites writes a second record. No captured
229
+ // MSG_NACK has that shape, which is what made it latent rather than safe.
230
+ if (type === 'MSG_NACK') {
231
+ if (errors.some(isNotMapped)) {
232
+ return Object.assign(Object.assign({ status: 'unavailable' }, withFields), { message: `Not available: ${joined(errors)}` });
233
+ }
234
+ const numeric = [
235
+ ...new Set(errors.map((error) => error.CODE).filter((code) => typeof code === 'number')),
236
+ ];
237
+ return Object.assign(Object.assign({}, writeOutcomeUnknown(`The app answered ${namedType !== null && namedType !== void 0 ? namedType : 'with a failure'}${numeric.length ? ` with code ${numeric.join(', ')}` : ''}, which does not say whether this was written, so whether this was written is unknown.`)), (warnings ? { warnings } : {}));
238
+ }
239
+ // Everything from here is an EVENT_NACK: the HANDLER's own answer. It is read as "not written"
240
+ // whatever STATUS_CODE its errors carry, 5xx included — do not "fix" that to `unknown` by
241
+ // analogy with the router's MSG_NACK above, but know exactly what it rests on.
242
+ //
243
+ // genesis-server v8.15.14, TransactionalEventHandlerContainer.transactionalEventFlow
244
+ // (genesis-parent/genesis-pal-eventhandler/.../eventhandler/pal/parse/
245
+ // TransactionalEventHandlerContainer.kt) runs a `transactional = true` handler inside one
246
+ // `writeTransaction`, and there are three cases:
247
+ // - onValidate returns a NACK: it returns before onCommit runs. Nothing is written.
248
+ // - onCommit THROWS (DUPLICATE_KEY, RECORD_NOT_FOUND, anything the entity DB throws): the
249
+ // exception escapes the transaction, which rolls back. Nothing is written.
250
+ // - onCommit writes rows and then RETURNS nack(): the block completes normally, nothing rolls
251
+ // back, and those writes COMMIT, under a NACK.
252
+ // So "an EVENT_NACK means not written" holds for the handlers this path accepts: Create-
253
+ // generated CRUD handlers, which write and ack(), or throw and roll back. It does not hold in
254
+ // general — a hand-written handler that writes and then returns nack() commits its writes —
255
+ // and that is one more reason `custom` events get no write tool.
256
+ //
257
+ // Only a modify or a delete has an existing row to lose. On an insert RECORD_NOT_FOUND is a
258
+ // reference the handler looked up ("Counterparty 'CP999' does not exist"), and "that row changed
259
+ // or was removed" was said about a row it was creating, with the app's reason dropped. An insert
260
+ // falls through to the ordinary classification: the live shape, a TableStandardError, is a
261
+ // database_error; anything else, the app's refusal. Both are not written, and both carry the
262
+ // server's own text.
263
+ if ((op === 'modify' || op === 'delete') && codes.has('RECORD_NOT_FOUND')) {
264
+ return Object.assign(Object.assign({ status: 'gone' }, withFields), { message: `That row changed or was removed, so nothing was written: ${joined(errors)}` });
265
+ }
266
+ if (codes.has('DUPLICATE_KEY')) {
267
+ return Object.assign(Object.assign({ status: 'duplicate' }, withFields), { message: `Already exists: ${joined(errors)}` });
268
+ }
269
+ if (types.has('TableStandardError')) {
270
+ return Object.assign(Object.assign({ status: 'database_error' }, withFields), { message: `The database refused it: ${joined(errors)}` });
271
+ }
272
+ // The FieldError split: PATH set means the payload itself was wrong.
273
+ if (errors.some((error) => typeof error.PATH === 'string' && error.PATH.length)) {
274
+ return Object.assign(Object.assign({ status: 'invalid_payload' }, withFields), { message: `The details sent were not valid: ${joined(errors)}` });
275
+ }
276
+ return Object.assign(Object.assign({ status: 'refused_by_app' }, withFields), { message: `The app refused it: ${joined(errors)}` });
277
+ }
278
+ /**
279
+ * The outcome for a row that never reached the server — a thrown request, a timeout, or a row
280
+ * the bridge itself refused before sending.
281
+ *
282
+ * @internal
283
+ */
284
+ export const writeNotAttempted = (status, message, fields) => (Object.assign({ status, committed: false, message }, ((fields === null || fields === void 0 ? void 0 : fields.length) ? { fields } : {})));
285
+ /**
286
+ * The outcome for a row that WAS sent and whose fate no answer settles.
287
+ *
288
+ * @remarks
289
+ * Its own status precisely because `committed: false` is not a fact here: the server may have
290
+ * written the row before the connection went. A consumer counting unwritten rows, or offering
291
+ * to try again, must be able to tell this apart from a row that never left.
292
+ *
293
+ * @internal
294
+ */
295
+ export const writeOutcomeUnknown = (message) => ({
296
+ status: 'unknown',
297
+ committed: false,
298
+ message,
299
+ });