@genesislcap/ai-assistant 15.43.0 → 15.45.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 (56) hide show
  1. package/dist/ai-assistant.api.json +34 -19
  2. package/dist/ai-assistant.d.ts +36 -2
  3. package/dist/chat-driver.cjs +89 -10
  4. package/dist/chat-driver.cjs.map +2 -2
  5. package/dist/chat-driver.mjs +89 -10
  6. package/dist/chat-driver.mjs.map +2 -2
  7. package/dist/custom-elements.json +466 -285
  8. package/dist/dts/components/chat-driver/chat-driver.d.ts +15 -2
  9. package/dist/dts/components/chat-driver/chat-driver.d.ts.map +1 -1
  10. package/dist/dts/components/chat-driver/interaction-controller.d.ts +26 -3
  11. package/dist/dts/components/chat-driver/interaction-controller.d.ts.map +1 -1
  12. package/dist/dts/components/chat-interaction-wrapper/chat-interaction-wrapper.d.ts +6 -0
  13. package/dist/dts/components/chat-interaction-wrapper/chat-interaction-wrapper.d.ts.map +1 -1
  14. package/dist/dts/genesis/config.d.ts +28 -1
  15. package/dist/dts/genesis/config.d.ts.map +1 -1
  16. package/dist/dts/genesis/consumes.d.ts.map +1 -1
  17. package/dist/dts/genesis/index.d.ts +2 -1
  18. package/dist/dts/genesis/index.d.ts.map +1 -1
  19. package/dist/dts/genesis/register-genesis-assistant.d.ts.map +1 -1
  20. package/dist/dts/genesis/review-model.d.ts +72 -1
  21. package/dist/dts/genesis/review-model.d.ts.map +1 -1
  22. package/dist/dts/genesis/types.d.ts +32 -0
  23. package/dist/dts/genesis/types.d.ts.map +1 -1
  24. package/dist/dts/genesis/write-review/write-review.d.ts +22 -1
  25. package/dist/dts/genesis/write-review/write-review.d.ts.map +1 -1
  26. package/dist/dts/genesis/write-review/write-review.styles.d.ts.map +1 -1
  27. package/dist/dts/genesis/write-review/write-review.template.d.ts.map +1 -1
  28. package/dist/dts/genesis/write-tools.d.ts +6 -1
  29. package/dist/dts/genesis/write-tools.d.ts.map +1 -1
  30. package/dist/dts/main/main.template.d.ts +8 -0
  31. package/dist/dts/main/main.template.d.ts.map +1 -1
  32. package/dist/dts/state/debug-event-log.d.ts +1 -1
  33. package/dist/dts/state/debug-event-log.d.ts.map +1 -1
  34. package/dist/dts/types/ai-chat-widget.d.ts +7 -0
  35. package/dist/dts/types/ai-chat-widget.d.ts.map +1 -1
  36. package/dist/esm/components/chat-driver/chat-driver.js +39 -6
  37. package/dist/esm/components/chat-driver/interaction-controller.js +50 -4
  38. package/dist/esm/components/chat-interaction-wrapper/chat-interaction-wrapper.js +17 -6
  39. package/dist/esm/genesis/config.js +108 -7
  40. package/dist/esm/genesis/consumes.js +8 -1
  41. package/dist/esm/genesis/index.js +1 -0
  42. package/dist/esm/genesis/register-genesis-assistant.js +7 -0
  43. package/dist/esm/genesis/review-model.js +115 -7
  44. package/dist/esm/genesis/write-review/write-review.js +80 -19
  45. package/dist/esm/genesis/write-review/write-review.styles.js +5 -2
  46. package/dist/esm/genesis/write-review/write-review.template.js +19 -2
  47. package/dist/esm/genesis/write-tools.js +49 -13
  48. package/dist/esm/main/main.template.js +19 -10
  49. package/dist/esm/state/debug-event-log.js +4 -0
  50. package/dist/genesis-consumes.cjs +8 -1
  51. package/dist/genesis-consumes.cjs.map +2 -2
  52. package/dist/genesis-consumes.mjs +8 -1
  53. package/dist/genesis-consumes.mjs.map +2 -2
  54. package/docs/genesis.md +60 -2
  55. package/package.json +18 -18
  56. package/scripts/smoke-genesis-consumes-node.mjs +1 -1
@@ -1,6 +1,6 @@
1
1
  import { __decorate } from "tslib";
2
2
  import { customElement, GenesisElement, observable } from '@genesislcap/web-core';
3
- import { canWrite, initialSelection, restoredSelection, reviewDecisionOf, } from '../review-model';
3
+ import { canWrite, customCodeLine, initialSelection, restoredSelection, reviewDecisionOf, reviewOutcomeFrom, stoppedSummary, } from '../review-model';
4
4
  import { MAX_FEEDBACK_CHARS, } from '../review-payload';
5
5
  import { isBlankText } from '../write-payload';
6
6
  import { WriteReviewStyles as styles } from './write-review.styles';
@@ -31,8 +31,8 @@ const literalMarkdown = (text) => text
31
31
  .map((line) => line.replace(ASCII_PUNCTUATION, '\\$&'))
32
32
  .join('\\\n');
33
33
  /**
34
- * Reviews answered on this page, by interaction id, until a review showing them resolved takes
35
- * the entry.
34
+ * Reviews answered on this page, by interaction id, until a review has said all there is to say
35
+ * about the answer.
36
36
  *
37
37
  * @remarks
38
38
  * The review that answers is not the one that shows the answer. The host rebuilds its trailing
@@ -40,8 +40,13 @@ const literalMarkdown = (text) => text
40
40
  * review arrives as a FRESH element, already holding its outcome. A live region that starts
41
41
  * populated announces nothing, and the button that was pressed goes with the old element, which
42
42
  * drops focus to the page. This is how the fresh one knows it is showing an answer given just now
43
- * rather than one read back from history, which must neither announce nor take focus. Taken once:
44
- * the next re-mount of the same review is history.
43
+ * rather than one read back from history, which must neither announce nor take focus.
44
+ *
45
+ * An approval has more to say: the write tool records what came of its rows AFTER they are
46
+ * written, and that arrives as one more rebuild. So its entry is kept until a review shows that
47
+ * outcome, and then taken; any other answer's is taken by the first review that shows it. The next
48
+ * re-mount after that is history. An approval whose outcome is never recorded keeps its entry, one
49
+ * per such review for the life of the page.
45
50
  */
46
51
  const answeredHere = new Map();
47
52
  /**
@@ -75,7 +80,7 @@ const PARKED = {
75
80
  */
76
81
  let GenesisWriteReview = class GenesisWriteReview extends GenesisElement {
77
82
  constructor() {
78
- super(...arguments);
83
+ super();
79
84
  /** Set by the interaction wrapper on every (re)mount. */
80
85
  this.interactionId = '';
81
86
  /** Chosen rows, by index. Starts empty — see {@link canWrite} and the model's own reasoning. */
@@ -91,6 +96,21 @@ let GenesisWriteReview = class GenesisWriteReview extends GenesisElement {
91
96
  * until it did, a second click sent a second answer — a Deny straight after a Write included.
92
97
  */
93
98
  this.answered = false;
99
+ this.addEventListener('focusin', () => {
100
+ if (this.heldAnswer)
101
+ this.heldAnswer.focused = true;
102
+ });
103
+ this.addEventListener('focusout', () => {
104
+ const held = this.heldAnswer;
105
+ // Judged once focus has landed, and only while this element is on the page: one removed
106
+ // with focus in it still held it.
107
+ if (held) {
108
+ queueMicrotask(() => {
109
+ if (this.isConnected)
110
+ held.focused = this.hasFocus();
111
+ });
112
+ }
113
+ });
94
114
  }
95
115
  /**
96
116
  * The rows, whether the review is still open or already resolved: a user who has approved a
@@ -105,6 +125,16 @@ let GenesisWriteReview = class GenesisWriteReview extends GenesisElement {
105
125
  var _a;
106
126
  return ((_a = this.interaction) === null || _a === void 0 ? void 0 : _a.status) === 'resolved';
107
127
  }
128
+ /**
129
+ * What the review says about custom code the event runs, or `''` when it runs none. Read from
130
+ * the model, which history keeps, so it is there whether the review is open, resolved or
131
+ * re-mounted. Not repeated in the closed summary: the resolved review still shows it, and the
132
+ * summary's live region would announce it as though code had run.
133
+ */
134
+ get customCodeLine() {
135
+ const model = this.model;
136
+ return (model === null || model === void 0 ? void 0 : model.customCode) ? customCodeLine(model.op, model.customCode) : '';
137
+ }
108
138
  /**
109
139
  * Whether the review is past taking input: it has resolved, or it has sent its answer. Every
110
140
  * action checks this rather than `resolved`, which is still false in between.
@@ -129,6 +159,21 @@ let GenesisWriteReview = class GenesisWriteReview extends GenesisElement {
129
159
  ? read.rows.filter((index) => selectable.includes(index))
130
160
  : [];
131
161
  }
162
+ /**
163
+ * What came of the approved rows, once the write tool has recorded it: see `ReviewOutcome`.
164
+ * Only for an approval — nothing else writes — and only in the shape the review can read.
165
+ */
166
+ get outcome() {
167
+ const interaction = this.interaction;
168
+ if ((interaction === null || interaction === void 0 ? void 0 : interaction.status) !== 'resolved' || !this.approved.length)
169
+ return undefined;
170
+ return reviewOutcomeFrom(interaction.outcome);
171
+ }
172
+ /** An approved row's recorded fate, when there is one. */
173
+ fateOf(index) {
174
+ var _a;
175
+ return (_a = this.outcome) === null || _a === void 0 ? void 0 : _a.rows.find((row) => row.index === index);
176
+ }
132
177
  /**
133
178
  * What a CLOSED review can truthfully say about its outcome.
134
179
  *
@@ -145,11 +190,17 @@ let GenesisWriteReview = class GenesisWriteReview extends GenesisElement {
145
190
  return '';
146
191
  const read = reviewDecisionOf(interaction.result, this.model);
147
192
  switch (read.decision) {
148
- case 'approved':
149
- // The reply is where the written rows are named, and a stopped turn has none: said, so the
150
- // user is not left waiting on it.
193
+ case 'approved': {
194
+ // A stopped turn gets no reply to say which rows were written, so the review says it,
195
+ // from what the write tool recorded (and marks each row). With no such record, the reply
196
+ // is where the written rows are named, and a stopped turn has none: said, so the user is
197
+ // not left waiting on it.
198
+ const outcome = this.outcome;
199
+ if (outcome === null || outcome === void 0 ? void 0 : outcome.stopped)
200
+ return stoppedSummary(outcome);
151
201
  return (`Approved ${this.approved.length} row(s). The reply says which were written; if the ` +
152
202
  'turn was stopped, some may not be.');
203
+ }
153
204
  case 'timed_out':
154
205
  return 'Closed without a decision. Nothing was written.';
155
206
  case 'changes_requested':
@@ -210,18 +261,28 @@ let GenesisWriteReview = class GenesisWriteReview extends GenesisElement {
210
261
  var _a;
211
262
  const answered = (_a = this.answeredNow) !== null && _a !== void 0 ? _a : answeredHere.get(this.interactionId);
212
263
  this.answeredNow = undefined;
213
- answeredHere.delete(this.interactionId);
214
- if (!answered || answered.focused) {
215
- this.announcement = this.closedSummary;
216
- if (answered)
264
+ const summary = this.closedSummary;
265
+ // An approval's outcome is still to come: see answeredHere.
266
+ const awaiting = !!answered && this.approved.length > 0 && !this.outcome;
267
+ this.heldAnswer = awaiting ? answered : undefined;
268
+ if (awaiting && this.interactionId)
269
+ answeredHere.set(this.interactionId, answered);
270
+ else
271
+ answeredHere.delete(this.interactionId);
272
+ if (!answered || answered.focused || answered.said === summary) {
273
+ this.announcement = summary;
274
+ if (answered === null || answered === void 0 ? void 0 : answered.focused)
217
275
  this.focusSoon('.closed');
218
- return;
219
276
  }
220
- this.announcement = '';
221
- setTimeout(() => {
222
- if (this.isConnected)
223
- this.announcement = this.closedSummary;
224
- }, ANNOUNCE_DELAY_MS);
277
+ else {
278
+ this.announcement = '';
279
+ setTimeout(() => {
280
+ if (this.isConnected)
281
+ this.announcement = this.closedSummary;
282
+ }, ANNOUNCE_DELAY_MS);
283
+ }
284
+ if (answered)
285
+ answered.said = summary;
225
286
  }
226
287
  /** Whether focus is on something inside this review, however deep in shadow roots. */
227
288
  hasFocus() {
@@ -154,9 +154,12 @@ export const WriteReviewStyles = css `
154
154
 
155
155
  /*
156
156
  * A closed review's row that was not approved says so in words — without them, a denied modify
157
- * read in greyscale or by a screen reader exactly like an approved one.
157
+ * read in greyscale or by a screen reader exactly like an approved one. An approved row says what
158
+ * came of it the same way, once the write tool has recorded that (written, not written
159
+ * because the user stopped, or its own reason).
158
160
  */
159
- .not-written {
161
+ .not-written,
162
+ .fate {
160
163
  margin-inline-start: calc(var(--design-unit, 4) * 1.5px);
161
164
  font-size: var(--type-ramp-minus-1-font-size, 0.875em);
162
165
  color: var(--neutral-foreground-hint);
@@ -1,6 +1,9 @@
1
1
  import { html, repeat, when } from '@genesislcap/web-core';
2
+ import { rowFate } from '../review-model';
2
3
  import { MAX_FEEDBACK_CHARS } from '../review-payload';
3
4
  import { isBlankText } from '../write-payload';
5
+ /** The custom-code line's id: the table is described by it, when there is one. */
6
+ const CUSTOM_CODE_LINE_ID = 'custom-code';
4
7
  /**
5
8
  * What a checkbox says NOW, read from the event rather than inferred from the state.
6
9
  *
@@ -40,6 +43,11 @@ const rowClass = (row, review) => {
40
43
  // saying it would happen after it had been decided it would not.
41
44
  if (review.resolved && !review.approved.includes(row.index))
42
45
  return 'unwritten';
46
+ // An approved row the write tool has recorded as anything but written: it did not happen, or
47
+ // may not have, so it is not shown as happening either.
48
+ const fate = review.fateOf(row.index);
49
+ if (fate && fate.status !== 'written')
50
+ return 'unwritten';
43
51
  if (((_b = review.model) === null || _b === void 0 ? void 0 : _b.op) === 'insert')
44
52
  return 'added';
45
53
  if (((_c = review.model) === null || _c === void 0 ? void 0 : _c.op) === 'delete')
@@ -108,9 +116,12 @@ const row = html `
108
116
  ${when((x, c) => rowClass(x, c.parent) === 'removed', html `
109
117
  ${mark('−')}
110
118
  `)}
111
- ${when((x, c) => rowClass(x, c.parent) === 'unwritten', html `
119
+ ${when((x, c) => rowClass(x, c.parent) === 'unwritten' && !c.parent.fateOf(x.index), html `
112
120
  <span class="not-written">not written</span>
113
121
  `)}
122
+ ${when((x, c) => !!c.parent.fateOf(x.index), html `
123
+ <span class="fate">${(x, c) => rowFate(c.parent.fateOf(x.index))}</span>
124
+ `)}
114
125
  </td>
115
126
  ${repeat((x) => x.cells, cell)}
116
127
  </tr>
@@ -129,8 +140,14 @@ export const WriteReviewTemplate = html `
129
140
  ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.op; }} ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.rows.length; }} row(s) in ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.resource; }}.
130
141
  Nothing is written until you choose rows and select Write.
131
142
  </p>
143
+ ${when((x) => !!x.customCodeLine, html `
144
+ <p class="custom-code" id="${CUSTOM_CODE_LINE_ID}">${(x) => x.customCodeLine}</p>
145
+ `)}
132
146
  <div class="scroller">
133
- <table aria-label="Rows to be written to ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.resource; }}">
147
+ <table
148
+ aria-label="Rows to be written to ${(x) => { var _a; return (_a = x.model) === null || _a === void 0 ? void 0 : _a.resource; }}"
149
+ aria-describedby="${(x) => (x.customCodeLine ? CUSTOM_CODE_LINE_ID : undefined)}"
150
+ >
134
151
  <thead>
135
152
  <tr>
136
153
  <th>
@@ -1,7 +1,7 @@
1
1
  import { __awaiter } from "tslib";
2
2
  import { getConnect } from '@genesislcap/foundation-comms';
3
3
  import { logger } from '../utils/logger';
4
- import { capIsReadable, opFromName, toolNameOf } from './config';
4
+ import { capIsReadable, customCodeOf, opFromName, toolNameOf } from './config';
5
5
  import { fieldKindOf as criteriaKindOf } from './criteria';
6
6
  import { loadEventFields } from './event-fields';
7
7
  import { GENESIS_DATA_SOURCE } from './resource-tools';
@@ -9,7 +9,7 @@ import { buildReviewModel, reviewDecisionOf, selectedRows, } from './review-mode
9
9
  import { MAX_FEEDBACK_CHARS } from './review-payload';
10
10
  import { createRequestRowSource } from './row-source';
11
11
  import { genesisTypeName } from './type-name';
12
- import { GENESIS_EVENT_OPS } from './types';
12
+ import { GENESIS_EVENT_OPS, } from './types';
13
13
  import { commitRows } from './write-commit';
14
14
  import { isCarryOnly, isWholeNumberType, PLATFORM_MAINTAINED, sendableFields, } from './write-payload';
15
15
  import { MAX_WRITE_ROWS, planWrite, stillCurrent, } from './write-plan';
@@ -302,6 +302,35 @@ const decideOp = (resource) => {
302
302
  }
303
303
  return { op };
304
304
  };
305
+ /** The Connect the tools were given, or the app's own; none when neither can be had yet. */
306
+ const connectOf = (options) => {
307
+ var _a;
308
+ try {
309
+ return (_a = options.connect) !== null && _a !== void 0 ? _a : getConnect();
310
+ }
311
+ catch (_b) {
312
+ return undefined;
313
+ }
314
+ };
315
+ /**
316
+ * Each event's customCode as its reviews show it, read ONCE, when the tools are built.
317
+ *
318
+ * @remarks
319
+ * The factory is exported and never validates, so a direct call gets the validator's rule, and a
320
+ * malformed value is said once rather than on every review. A validated config's copies are
321
+ * already normal, so they say nothing twice.
322
+ */
323
+ const customCodesOf = (events) => {
324
+ const customCodes = new Map();
325
+ for (const resource of events) {
326
+ const customCode = customCodeOf(resource);
327
+ if (customCode.warning)
328
+ logger.warn(`Genesis assistant: ${customCode.warning}.`);
329
+ if (customCode.value)
330
+ customCodes.set(resource.name, customCode.value);
331
+ }
332
+ return customCodes;
333
+ };
305
334
  /** `EVENT_TRADE_MODIFY` reads its rows from `REQ_TRADE`, when the app exposes one. */
306
335
  const readResourceFor = (eventName) => {
307
336
  const match = /^EVENT_(.+)_(?:INSERT|MODIFY|DELETE)$/.exec(eventName);
@@ -514,15 +543,7 @@ function configRules(resources, options) {
514
543
  export function createGenesisWriteTools(resources, options = {}) {
515
544
  var _a;
516
545
  const { events, decide, whyNoSource, readsFrom, writable } = configRules(resources, options);
517
- const resolveConnect = () => {
518
- var _a;
519
- try {
520
- return (_a = options.connect) !== null && _a !== void 0 ? _a : getConnect();
521
- }
522
- catch (_b) {
523
- return undefined;
524
- }
525
- };
546
+ const resolveConnect = () => connectOf(options);
526
547
  const sources = new Map();
527
548
  const lockOf = locksOf(resources, decide, readsFrom);
528
549
  /**
@@ -686,6 +707,7 @@ export function createGenesisWriteTools(resources, options = {}) {
686
707
  // One record per turn and event, shared by every handler below: see write-turn.ts.
687
708
  const turns = createWriteTurns();
688
709
  const feedbackKey = (_a = options.feedbackKey) !== null && _a !== void 0 ? _a : mintFeedbackKey();
710
+ const customCodes = customCodesOf(events);
689
711
  const toolHandlers = {};
690
712
  for (const resource of events) {
691
713
  toolHandlers[toolNameOf(resource.name)] = (args, context) => __awaiter(this, void 0, void 0, function* () {
@@ -700,6 +722,7 @@ export function createGenesisWriteTools(resources, options = {}) {
700
722
  turns,
701
723
  feedbackKey,
702
724
  lockOf,
725
+ customCode: customCodes.get(resource.name),
703
726
  });
704
727
  });
705
728
  }
@@ -758,7 +781,7 @@ const summarise = (rows) => {
758
781
  };
759
782
  function runWrite(resource, args, context, deps) {
760
783
  return __awaiter(this, void 0, void 0, function* () {
761
- var _a, _b, _c, _d, _e, _f;
784
+ var _a, _b, _c, _d, _e, _f, _g, _h;
762
785
  const fail = (error, message) => ({
763
786
  source: GENESIS_DATA_SOURCE,
764
787
  untrusted: true,
@@ -849,6 +872,7 @@ function runWrite(resource, args, context, deps) {
849
872
  var _a;
850
873
  return (Object.assign(Object.assign({ index: row.index, details: (_a = row.details) !== null && _a !== void 0 ? _a : {} }, (op === 'delete' && row.preImage ? { display: row.preImage } : {})), { preImage: op === 'delete' ? undefined : row.preImage, problems: row.problems }));
851
874
  }),
875
+ customCode: deps.customCode,
852
876
  });
853
877
  // What the staleness check compares: every column the review RENDERED, because what the user
854
878
  // saw is evidence for what was checked only if the two are the same set. For a modify that is
@@ -974,9 +998,21 @@ function runWrite(resource, args, context, deps) {
974
998
  message: 'No outcome was recorded for this row.',
975
999
  };
976
1000
  });
1001
+ // The review says what came of its rows itself: when the user stopped the turn, no reply will.
1002
+ (_g = context.recordInteractionOutcome) === null || _g === void 0 ? void 0 : _g.call(context, reviewOutcomeOf(rows, approved, ((_h = context.turnSignal) === null || _h === void 0 ? void 0 : _h.aborted) === true));
977
1003
  return result(resource.name, true, rows, summarise(rows), 'approved');
978
1004
  });
979
1005
  }
1006
+ /** What an approved review records about its rows: see `ReviewOutcome`. */
1007
+ const reviewOutcomeOf = (rows, approved, stopped) => {
1008
+ const written = new Set(approved.map((row) => row.index));
1009
+ return {
1010
+ stopped,
1011
+ rows: rows
1012
+ .filter((row) => written.has(row.index))
1013
+ .map(({ index, status, message }) => ({ index, status, message })),
1014
+ };
1015
+ };
980
1016
  /**
981
1017
  * Re-checks and commits the approved rows ONE AT A TIME, in order, recording each outcome in
982
1018
  * `lines`.
@@ -999,7 +1035,7 @@ function commitApproved(approved, lines, run) {
999
1035
  var _a;
1000
1036
  if (!((_a = run.turnSignal) === null || _a === void 0 ? void 0 : _a.aborted))
1001
1037
  return false;
1002
- lines.set(index, { index, status: 'failed', committed: false, message: STOPPED_ROW });
1038
+ lines.set(index, { index, status: 'stopped', committed: false, message: STOPPED_ROW });
1003
1039
  return true;
1004
1040
  };
1005
1041
  for (const row of approved) {
@@ -352,6 +352,24 @@ export const blockedBannerClasses = (x) => [
352
352
  ]
353
353
  .filter(Boolean)
354
354
  .join(' ');
355
+ /**
356
+ * A message's interaction widget, in the wrapper that mounts it. Everything the widget is shown
357
+ * comes from the history message through here, the outcome a tool recorded after the answer
358
+ * included, so a remount, a pop-out and a reload all show the same thing.
359
+ *
360
+ * @internal
361
+ */
362
+ export const interactionWrapperTemplate = html `
363
+ <ai-chat-interaction-wrapper
364
+ :componentName=${(m) => m.interaction.componentName}
365
+ :data=${(m) => m.interaction.data}
366
+ :interactionId=${(m) => m.interaction.interactionId}
367
+ :resolved=${(m) => m.interaction.resolved}
368
+ :outcome=${(m) => m.interaction.outcome}
369
+ @interaction-completed=${(m, c) => c.parent.handleInteractionCompleted(c.event)}
370
+ @interaction-cost=${(m, c) => c.parent.handleInteractionCost(c.event)}
371
+ ></ai-chat-interaction-wrapper>
372
+ `;
355
373
  /**
356
374
  * Id of the blocked banner, referenced by the composer controls'
357
375
  * `aria-describedby`. Shadow-DOM-scoped, so a fixed string cannot collide with
@@ -608,16 +626,7 @@ ${(tc) => { var _a; return (((_a = tc.foldPath) === null || _a === void 0 ? void
608
626
  ${when((m) => m.toolCalls, html `
609
627
  ${repeat((m) => { var _a; return (_a = m.toolCalls) !== null && _a !== void 0 ? _a : []; }, toolCallItemTemplate)}
610
628
  `)}
611
- ${when((m) => m.interaction, html `
612
- <ai-chat-interaction-wrapper
613
- :componentName=${(m) => m.interaction.componentName}
614
- :data=${(m) => m.interaction.data}
615
- :interactionId=${(m) => m.interaction.interactionId}
616
- :resolved=${(m) => m.interaction.resolved}
617
- @interaction-completed=${(m, c) => c.parent.handleInteractionCompleted(c.event)}
618
- @interaction-cost=${(m, c) => c.parent.handleInteractionCost(c.event)}
619
- ></ai-chat-interaction-wrapper>
620
- `)}
629
+ ${when((m) => m.interaction, interactionWrapperTemplate)}
621
630
  </div>
622
631
  </div>
623
632
  </div>
@@ -72,6 +72,10 @@ export const META_EVENT_IMPORTANCE = {
72
72
  // the value was not real spend). 'high': it is the one way the session total goes quietly
73
73
  // wrong, so it must stand out when reconciling.
74
74
  'interaction.external-cost-dropped': 'high',
75
+ // What came of an answer, recorded by the tool that asked (see recordInteractionOutcome).
76
+ 'interaction.outcome': 'normal',
77
+ // An outcome that could not be recorded: the widget goes on showing only the answer.
78
+ 'interaction.outcome-dropped': 'high',
75
79
  'external-diagnostics.folded': 'normal',
76
80
  'session.cleared': 'normal',
77
81
  'session.switched': 'low',
@@ -24,7 +24,14 @@ __export(consumes_exports, {
24
24
  module.exports = __toCommonJS(consumes_exports);
25
25
  var GENESIS_AI_CONSUMES = Object.freeze({
26
26
  kinds: Object.freeze(["request", "event"]),
27
- resourceFields: Object.freeze(["name", "kind", "op", "context", "maxRows"]),
27
+ resourceFields: Object.freeze([
28
+ "name",
29
+ "kind",
30
+ "op",
31
+ "context",
32
+ "maxRows",
33
+ "customCode"
34
+ ]),
28
35
  vendors: Object.freeze(["anthropic", "gemini"]),
29
36
  tiers: Object.freeze(["low", "high", "reasoning"])
30
37
  });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/genesis/consumes.ts"],
4
- "sourcesContent": ["/**\n * What this version of the assistant reads from a generated AI config\n * (`@genesislcap/ai-assistant/genesis/consumes`).\n *\n * @remarks\n * Its own entry point, loadable in bare Node, because the seed's CI reads it in a Node step and\n * `./genesis` cannot be: that entry is bundler-target ESM, and its closure registers custom\n * elements, which need a DOM. So this module imports nothing at run time. A type from `./types`\n * is all it may take, and a test holds it to that. The Node build bundles it to\n * `dist/genesis-consumes.mjs` and `.cjs`, and a smoke test loads both without a DOM.\n *\n * @packageDocumentation\n * @beta\n */\nimport type { GenesisAiConsumes } from './types';\n\nexport type { GenesisAiConsumes } from './types';\n\n/**\n * What this version of the assistant reads from a generated AI config: the resource kinds it\n * accepts, the resource fields its validator or a tool reads, and the `vendor` and `tier` values\n * it accepts.\n *\n * @remarks\n * The generator must not emit what the installed assistant does not read, or a new kind is left out\n * and a new field ignored \u2014 and an older assistant used to block entirely on a kind it did not know.\n * Nothing in the generator can say which version will run its output. So the app's seed declares\n * what it expects, and the seed's CI checks that declaration against this constant, exported from\n * the version it installs. That makes this the single statement of what an assistant consumes, and\n * it carries no version number because it needs none.\n *\n * A test holds it to the code: its kinds are the ones validation accepts, and its fields are the\n * ones read. A change that teaches the validator a kind, or a tool a field, updates it in the same\n * commit.\n *\n * A resource this version does not read is only left out, but an unknown `vendor` or `tier` still\n * blocks the whole assistant, so those are here too. They are written out rather than imported\n * from foundation-ai, which this entry must not load; a test holds them equal to its\n * `AI_TIER_VENDORS` and `AI_TIER_IDS`, which the validator checks against.\n *\n * @beta\n */\nexport const GENESIS_AI_CONSUMES: GenesisAiConsumes = Object.freeze({\n kinds: Object.freeze(['request', 'event'] as const),\n resourceFields: Object.freeze(['name', 'kind', 'op', 'context', 'maxRows'] as const),\n vendors: Object.freeze(['anthropic', 'gemini'] as const),\n tiers: Object.freeze(['low', 'high', 'reasoning'] as const),\n});\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA0CO,IAAM,sBAAyC,OAAO,OAAO;AAAA,EAClE,OAAO,OAAO,OAAO,CAAC,WAAW,OAAO,CAAU;AAAA,EAClD,gBAAgB,OAAO,OAAO,CAAC,QAAQ,QAAQ,MAAM,WAAW,SAAS,CAAU;AAAA,EACnF,SAAS,OAAO,OAAO,CAAC,aAAa,QAAQ,CAAU;AAAA,EACvD,OAAO,OAAO,OAAO,CAAC,OAAO,QAAQ,WAAW,CAAU;AAC5D,CAAC;",
4
+ "sourcesContent": ["/**\n * What this version of the assistant reads from a generated AI config\n * (`@genesislcap/ai-assistant/genesis/consumes`).\n *\n * @remarks\n * Its own entry point, loadable in bare Node, because the seed's CI reads it in a Node step and\n * `./genesis` cannot be: that entry is bundler-target ESM, and its closure registers custom\n * elements, which need a DOM. So this module imports nothing at run time. A type from `./types`\n * is all it may take, and a test holds it to that. The Node build bundles it to\n * `dist/genesis-consumes.mjs` and `.cjs`, and a smoke test loads both without a DOM.\n *\n * @packageDocumentation\n * @beta\n */\nimport type { GenesisAiConsumes } from './types';\n\nexport type { GenesisAiConsumes } from './types';\n\n/**\n * What this version of the assistant reads from a generated AI config: the resource kinds it\n * accepts, the resource fields its validator or a tool reads, and the `vendor` and `tier` values\n * it accepts.\n *\n * @remarks\n * The generator must not emit what the installed assistant does not read, or a new kind is left out\n * and a new field ignored \u2014 and an older assistant used to block entirely on a kind it did not know.\n * Nothing in the generator can say which version will run its output. So the app's seed declares\n * what it expects, and the seed's CI checks that declaration against this constant, exported from\n * the version it installs. That makes this the single statement of what an assistant consumes, and\n * it carries no version number because it needs none.\n *\n * A test holds it to the code: its kinds are the ones validation accepts, and its fields are the\n * ones read. A change that teaches the validator a kind, or a tool a field, updates it in the same\n * commit.\n *\n * A resource this version does not read is only left out, but an unknown `vendor` or `tier` still\n * blocks the whole assistant, so those are here too. They are written out rather than imported\n * from foundation-ai, which this entry must not load; a test holds them equal to its\n * `AI_TIER_VENDORS` and `AI_TIER_IDS`, which the validator checks against.\n *\n * @beta\n */\nexport const GENESIS_AI_CONSUMES: GenesisAiConsumes = Object.freeze({\n kinds: Object.freeze(['request', 'event'] as const),\n resourceFields: Object.freeze([\n 'name',\n 'kind',\n 'op',\n 'context',\n 'maxRows',\n 'customCode',\n ] as const),\n vendors: Object.freeze(['anthropic', 'gemini'] as const),\n tiers: Object.freeze(['low', 'high', 'reasoning'] as const),\n});\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AA0CO,IAAM,sBAAyC,OAAO,OAAO;AAAA,EAClE,OAAO,OAAO,OAAO,CAAC,WAAW,OAAO,CAAU;AAAA,EAClD,gBAAgB,OAAO,OAAO;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAU;AAAA,EACV,SAAS,OAAO,OAAO,CAAC,aAAa,QAAQ,CAAU;AAAA,EACvD,OAAO,OAAO,OAAO,CAAC,OAAO,QAAQ,WAAW,CAAU;AAC5D,CAAC;",
6
6
  "names": []
7
7
  }
@@ -1,7 +1,14 @@
1
1
  // src/genesis/consumes.ts
2
2
  var GENESIS_AI_CONSUMES = Object.freeze({
3
3
  kinds: Object.freeze(["request", "event"]),
4
- resourceFields: Object.freeze(["name", "kind", "op", "context", "maxRows"]),
4
+ resourceFields: Object.freeze([
5
+ "name",
6
+ "kind",
7
+ "op",
8
+ "context",
9
+ "maxRows",
10
+ "customCode"
11
+ ]),
5
12
  vendors: Object.freeze(["anthropic", "gemini"]),
6
13
  tiers: Object.freeze(["low", "high", "reasoning"])
7
14
  });
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../src/genesis/consumes.ts"],
4
- "sourcesContent": ["/**\n * What this version of the assistant reads from a generated AI config\n * (`@genesislcap/ai-assistant/genesis/consumes`).\n *\n * @remarks\n * Its own entry point, loadable in bare Node, because the seed's CI reads it in a Node step and\n * `./genesis` cannot be: that entry is bundler-target ESM, and its closure registers custom\n * elements, which need a DOM. So this module imports nothing at run time. A type from `./types`\n * is all it may take, and a test holds it to that. The Node build bundles it to\n * `dist/genesis-consumes.mjs` and `.cjs`, and a smoke test loads both without a DOM.\n *\n * @packageDocumentation\n * @beta\n */\nimport type { GenesisAiConsumes } from './types';\n\nexport type { GenesisAiConsumes } from './types';\n\n/**\n * What this version of the assistant reads from a generated AI config: the resource kinds it\n * accepts, the resource fields its validator or a tool reads, and the `vendor` and `tier` values\n * it accepts.\n *\n * @remarks\n * The generator must not emit what the installed assistant does not read, or a new kind is left out\n * and a new field ignored \u2014 and an older assistant used to block entirely on a kind it did not know.\n * Nothing in the generator can say which version will run its output. So the app's seed declares\n * what it expects, and the seed's CI checks that declaration against this constant, exported from\n * the version it installs. That makes this the single statement of what an assistant consumes, and\n * it carries no version number because it needs none.\n *\n * A test holds it to the code: its kinds are the ones validation accepts, and its fields are the\n * ones read. A change that teaches the validator a kind, or a tool a field, updates it in the same\n * commit.\n *\n * A resource this version does not read is only left out, but an unknown `vendor` or `tier` still\n * blocks the whole assistant, so those are here too. They are written out rather than imported\n * from foundation-ai, which this entry must not load; a test holds them equal to its\n * `AI_TIER_VENDORS` and `AI_TIER_IDS`, which the validator checks against.\n *\n * @beta\n */\nexport const GENESIS_AI_CONSUMES: GenesisAiConsumes = Object.freeze({\n kinds: Object.freeze(['request', 'event'] as const),\n resourceFields: Object.freeze(['name', 'kind', 'op', 'context', 'maxRows'] as const),\n vendors: Object.freeze(['anthropic', 'gemini'] as const),\n tiers: Object.freeze(['low', 'high', 'reasoning'] as const),\n});\n"],
5
- "mappings": ";AA0CO,IAAM,sBAAyC,OAAO,OAAO;AAAA,EAClE,OAAO,OAAO,OAAO,CAAC,WAAW,OAAO,CAAU;AAAA,EAClD,gBAAgB,OAAO,OAAO,CAAC,QAAQ,QAAQ,MAAM,WAAW,SAAS,CAAU;AAAA,EACnF,SAAS,OAAO,OAAO,CAAC,aAAa,QAAQ,CAAU;AAAA,EACvD,OAAO,OAAO,OAAO,CAAC,OAAO,QAAQ,WAAW,CAAU;AAC5D,CAAC;",
4
+ "sourcesContent": ["/**\n * What this version of the assistant reads from a generated AI config\n * (`@genesislcap/ai-assistant/genesis/consumes`).\n *\n * @remarks\n * Its own entry point, loadable in bare Node, because the seed's CI reads it in a Node step and\n * `./genesis` cannot be: that entry is bundler-target ESM, and its closure registers custom\n * elements, which need a DOM. So this module imports nothing at run time. A type from `./types`\n * is all it may take, and a test holds it to that. The Node build bundles it to\n * `dist/genesis-consumes.mjs` and `.cjs`, and a smoke test loads both without a DOM.\n *\n * @packageDocumentation\n * @beta\n */\nimport type { GenesisAiConsumes } from './types';\n\nexport type { GenesisAiConsumes } from './types';\n\n/**\n * What this version of the assistant reads from a generated AI config: the resource kinds it\n * accepts, the resource fields its validator or a tool reads, and the `vendor` and `tier` values\n * it accepts.\n *\n * @remarks\n * The generator must not emit what the installed assistant does not read, or a new kind is left out\n * and a new field ignored \u2014 and an older assistant used to block entirely on a kind it did not know.\n * Nothing in the generator can say which version will run its output. So the app's seed declares\n * what it expects, and the seed's CI checks that declaration against this constant, exported from\n * the version it installs. That makes this the single statement of what an assistant consumes, and\n * it carries no version number because it needs none.\n *\n * A test holds it to the code: its kinds are the ones validation accepts, and its fields are the\n * ones read. A change that teaches the validator a kind, or a tool a field, updates it in the same\n * commit.\n *\n * A resource this version does not read is only left out, but an unknown `vendor` or `tier` still\n * blocks the whole assistant, so those are here too. They are written out rather than imported\n * from foundation-ai, which this entry must not load; a test holds them equal to its\n * `AI_TIER_VENDORS` and `AI_TIER_IDS`, which the validator checks against.\n *\n * @beta\n */\nexport const GENESIS_AI_CONSUMES: GenesisAiConsumes = Object.freeze({\n kinds: Object.freeze(['request', 'event'] as const),\n resourceFields: Object.freeze([\n 'name',\n 'kind',\n 'op',\n 'context',\n 'maxRows',\n 'customCode',\n ] as const),\n vendors: Object.freeze(['anthropic', 'gemini'] as const),\n tiers: Object.freeze(['low', 'high', 'reasoning'] as const),\n});\n"],
5
+ "mappings": ";AA0CO,IAAM,sBAAyC,OAAO,OAAO;AAAA,EAClE,OAAO,OAAO,OAAO,CAAC,WAAW,OAAO,CAAU;AAAA,EAClD,gBAAgB,OAAO,OAAO;AAAA,IAC5B;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,CAAU;AAAA,EACV,SAAS,OAAO,OAAO,CAAC,aAAa,QAAQ,CAAU;AAAA,EACvD,OAAO,OAAO,OAAO,CAAC,OAAO,QAAQ,WAAW,CAAU;AAC5D,CAAC;",
6
6
  "names": []
7
7
  }
package/docs/genesis.md CHANGED
@@ -104,6 +104,7 @@ that table, unless asked), then the extensions' prompt.
104
104
  | `context` | Required. Becomes the tool description, so write it for the model. |
105
105
  | `op` | Events only: `insert`, `modify`, `delete` or `custom`. |
106
106
  | `maxRows` | Requests only: a whole number from 1 to 1000. Defaults to 50. |
107
+ | `customCode` | Insert, modify and delete events only: the event's handler runs custom code. See [Custom code](#custom-code). |
107
108
 
108
109
  A rule about the config **as a whole** — `enabled`, `vendor`, `tier`, `systemPrompt`, and
109
110
  `resources` being a list — blocks the assistant. A rule about **one resource** leaves that resource
@@ -129,7 +130,7 @@ import { GENESIS_AI_CONSUMES } from '@genesislcap/ai-assistant/genesis/consumes'
129
130
  GENESIS_AI_CONSUMES;
130
131
  // {
131
132
  // kinds: ['request', 'event'],
132
- // resourceFields: ['name', 'kind', 'op', 'context', 'maxRows'],
133
+ // resourceFields: ['name', 'kind', 'op', 'context', 'maxRows', 'customCode'],
133
134
  // vendors: ['anthropic', 'gemini'],
134
135
  // tiers: ['low', 'high', 'reasoning'],
135
136
  // }
@@ -286,7 +287,16 @@ The review offers three answers:
286
287
  open when they stop closes unanswered, like one that timed out; and nothing is committed once the
287
288
  turn is stopped, even if an Approve lands first. A Stop pressed while approved rows are being
288
289
  written stops the rest: rows already committed are reported as written, and every row not yet
289
- sent is reported `failed` with "Not written: the user stopped the turn."
290
+ sent is reported `stopped` with "Not written: the user stopped the turn." No reply follows a
291
+ stopped turn, so the review says it itself: "Stopped while writing: 6 written, 5 not written.", and
292
+ each approved row is marked written, "not written (stopped)", or why else it was not. The marks
293
+ are for the user: "not written (changed since the review)", "not written (row changed or no
294
+ longer exists)", and so on. Only the app's or the database's own refusal is shown as it came. A
295
+ row that was sent and got no answer (`unknown`) may well have been written, so it is never
296
+ counted as not written. The summary adds ", K may have been written" ("Stopped while writing: 6
297
+ written, 4 not written, 1 may have been written."), and the row is marked "may have been
298
+ written". The tool records that on the review through `recordInteractionOutcome`, so it is still
299
+ there after a remount, a pop-out or a reload. It is never sent to the model.
290
300
 
291
301
  The text is capped at 1000 characters and never reaches the event's `REASON` or `DETAILS`. The
292
302
  system prompt makes `userFeedback` and `next` the one exception to "tool results are data", and
@@ -330,6 +340,53 @@ every chat view, and all three rules with it: a denial holds until the tools are
330
340
  change requests count across every message (so the fourth ends revisions for good), and a resend
331
341
  is compared with the last proposal asked about in any message. The first such call logs a warning.
332
342
 
343
+ ### Custom code
344
+
345
+ A generated CRUD event writes the row the review shows. An event whose handler runs **custom code**
346
+ can do more: the row it saves is built in its code, and the code can write to other tables too.
347
+ The generator marks such an event with `customCode`:
348
+
349
+ ```json
350
+ {
351
+ "name": "EVENT_ORDER_INSERT",
352
+ "kind": "event",
353
+ "op": "insert",
354
+ "context": "Place an order. Custom code matches it against resting orders.",
355
+ "customCode": { "alsoWrites": ["TRADE", "ORDER_FILL", "AXE"], "listComplete": false }
356
+ }
357
+ ```
358
+
359
+ `alsoWrites` names the other tables the generator found the code writes to, in the order the code
360
+ writes them. `listComplete` is `true` only when the generator could prove those are all of them.
361
+
362
+ Every review of such an event shows one line directly under its header, above the table, and the
363
+ table is described by it (`aria-describedby`). An insert or modify says "save the values shown", a
364
+ delete says "remove the row shown":
365
+
366
+ | `alsoWrites` | `listComplete` | The line |
367
+ | ------------ | -------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
368
+ | empty | `true` | This action runs custom code, so it can do more than save the values shown. |
369
+ | empty | `false` | This action runs custom code, so it can do more than save the values shown, and it can also write to other records. |
370
+ | named | `true` | This action runs custom code, so it can do more than save the values shown. When this app was generated, that code could also write to TRADE and AXE. |
371
+ | named | `false` | This action runs custom code, so it can do more than save the values shown. When this app was generated, that code could also write to TRADE, AXE and other records. |
372
+
373
+ The named tables are what the code wrote to **when the app was generated**. The app's code can
374
+ change after that, and the config does not change with it, so regenerating the app refreshes the
375
+ list. The line stays on a review once it is answered and when the chat is reloaded from history.
376
+ It is not repeated in the answer's summary.
377
+
378
+ `customCode` means something only on an insert, modify or delete event. Anywhere else the field
379
+ is ignored, the resource is kept, and the console says so. A malformed value keeps the valid table names,
380
+ in order and once each, and is read as **not complete**, so the review says the code can also
381
+ write to other records. The resource is kept and the console says what was wrong. A value that
382
+ cannot be read never shows the form that says the code writes nothing else. Neither warning is
383
+ logged for a resource an app extension replaced: it gets no review, and its own warning says it
384
+ was left out.
385
+
386
+ `customCodeOf`, exported from `@genesislcap/ai-assistant/genesis`, is the one reader of the field.
387
+ Registration uses it, and so does `createGenesisWriteTools` when it is called directly, since
388
+ that factory does not validate its resources.
389
+
333
390
  ### A modify needs somewhere to read its rows from
334
391
 
335
392
  A generated CRUD modify **replaces** the record, so a payload carrying only the fields the model
@@ -466,6 +523,7 @@ one — that is how a row gets written twice.
466
523
  | `database_error` | The database refused it for another reason. |
467
524
  | `unavailable` | The event is not mapped to a service (a 404 `MSG_NACK`). |
468
525
  | `failed` | Nothing was sent: the app was not connected, or the row could not be checked first. |
526
+ | `stopped` | Approved, and not sent: the user stopped the turn first. |
469
527
  | `unknown` | Sent, and no answer this can read came back. It may have been written. Never retry. |
470
528
 
471
529
  `createGenesisWriteTools(resources, { connect })` builds these without registering anything, for