@saar-ai/web-widget 0.23.0 → 0.25.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.
@@ -20,6 +20,8 @@ export interface CaptureCardSpec {
20
20
  readonly channel: CaptureCardConsentChannel;
21
21
  readonly text: string;
22
22
  };
23
+ /** The other details asked for in the same card (SAA-512), each answered on its own. */
24
+ readonly fields?: readonly Omit<CaptureCardSpec, 'consent' | 'fields'>[];
23
25
  }
24
26
  export type CaptureCardAnswer = {
25
27
  readonly field: string;
@@ -1 +1 @@
1
- {"version":3,"file":"capture-card-dom.d.ts","sourceRoot":"","sources":["../../src/capture-card/capture-card-dom.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,OAAO,EAGL,KAAK,yBAAyB,EAC9B,KAAK,qBAAqB,EAC3B,MAAM,uCAAuC,CAAC;AAE/C,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,OAAO,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACrC,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,wEAAwE;IACxE,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;IACzB,wDAAwD;IACxD,QAAQ,CAAC,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,OAAO,EAAE,yBAAyB,CAAC;QAAC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;CAC3F;AAED,MAAM,MAAM,iBAAiB,GACzB;IAAE,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,OAAO,CAAC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,CAAC,UAAU,EAAE,CAAC,OAAO,EAAE,OAAO,GAAG,UAAU,KAAK,IAAI,CAAC;IAC7D,yFAAyF;IACzF,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CACrC;AAED,KAAK,OAAO,GACR,eAAe,GACf,eAAe,GACf,YAAY,GACZ,MAAM,GACN,QAAQ,GACR,QAAQ,GACR,OAAO,GACP,UAAU,GACV,SAAS,GACT,QAAQ,GACR,QAAQ,GACR,qBAAqB,CAAC;AAE1B,yFAAyF;AACzF,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CACtC,MAAM,CAAC,IAAI,GAAG,IAAI,GAAG,IAAI,EAAE,QAAQ,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,CAuE9D,CAAC;AAuCF;;;;GAIG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,kBAAkB,GAAG,MAAM,IAAI,CAgS3F"}
@@ -153,6 +153,8 @@ function mountCaptureCard(root, options) {
153
153
  const id = `wgt-capture-${++nextId}`;
154
154
  let live = true;
155
155
  let busy = false;
156
+ /** Whether any send of this card had a value taken (SAA-512). */
157
+ let saved = false;
156
158
  const form = el("form");
157
159
  form.noValidate = true;
158
160
  form.dir = RTL.test(options.locale) ? "rtl" : "ltr";
@@ -177,71 +179,81 @@ function mountCaptureCard(root, options) {
177
179
  });
178
180
  form.append(intro);
179
181
  }
180
- const label = el("label", card.label);
181
- label.htmlFor = `${id}-input`;
182
- label.style.fontWeight = "600";
183
- form.append(label);
184
- const describedBy = [];
185
- if (card.purpose !== void 0) {
186
- const purpose = el("p", card.purpose);
187
- purpose.id = `${id}-purpose`;
188
- Object.assign(purpose.style, {
189
- margin: "0",
190
- color: "var(--widget-foreground-muted)"
191
- });
192
- form.append(purpose);
193
- describedBy.push(purpose.id);
194
- }
195
- const choices = card.input === "choice" && card.choices && card.choices.length > 0 ? card.choices : null;
196
- const listed = choices !== null ? choices.map((choice) => [choice, choice]) : card.input === "country" ? countryChoices(options.locale) : null;
197
- let field;
198
- if (listed !== null) {
199
- const select = el("select");
200
- const placeholder = el("option", copy.choose);
201
- placeholder.value = "";
202
- select.append(placeholder);
203
- for (const [value, text] of listed) {
204
- const option = el("option", text);
205
- option.value = value;
206
- select.append(option);
182
+ const rows = [card, ...card.fields ?? []].map((spec, index) => {
183
+ const rowId = `${id}-${index}`;
184
+ const label = el("label", spec.label);
185
+ label.htmlFor = `${rowId}-input`;
186
+ label.style.fontWeight = "600";
187
+ form.append(label);
188
+ const describedBy = [];
189
+ if (spec.purpose !== void 0) {
190
+ const purpose = el("p", spec.purpose);
191
+ purpose.id = `${rowId}-purpose`;
192
+ Object.assign(purpose.style, {
193
+ margin: "0",
194
+ color: "var(--widget-foreground-muted)"
195
+ });
196
+ form.append(purpose);
197
+ describedBy.push(purpose.id);
207
198
  }
208
- field = select;
209
- } else {
210
- const input = el("input");
211
- const kind = INPUT_KINDS[card.input];
212
- input.type = kind?.[0] ?? "text";
213
- if (kind) {
214
- input.dir = "ltr";
215
- if (kind[1]) input.inputMode = kind[1];
216
- if (kind[2]) input.autocomplete = kind[2];
199
+ const choices = spec.input === "choice" && spec.choices && spec.choices.length > 0 ? spec.choices : null;
200
+ const listed = choices !== null ? choices.map((choice) => [choice, choice]) : spec.input === "country" ? countryChoices(options.locale) : null;
201
+ let field;
202
+ if (listed !== null) {
203
+ const select = el("select");
204
+ const placeholder = el("option", copy.choose);
205
+ placeholder.value = "";
206
+ select.append(placeholder);
207
+ for (const [value, text] of listed) {
208
+ const option = el("option", text);
209
+ option.value = value;
210
+ select.append(option);
211
+ }
212
+ field = select;
213
+ } else {
214
+ const input = el("input");
215
+ const kind = INPUT_KINDS[spec.input];
216
+ input.type = kind?.[0] ?? "text";
217
+ if (kind) {
218
+ input.dir = "ltr";
219
+ if (kind[1]) input.inputMode = kind[1];
220
+ if (kind[2]) input.autocomplete = kind[2];
221
+ }
222
+ if (spec.maxLength !== void 0) input.maxLength = spec.maxLength;
223
+ field = input;
217
224
  }
218
- if (card.maxLength !== void 0) input.maxLength = card.maxLength;
219
- field = input;
220
- }
221
- field.id = `${id}-input`;
222
- field.name = card.field;
223
- Object.assign(field.style, {
224
- width: "100%",
225
- boxSizing: "border-box",
226
- padding: "8px 10px",
227
- borderRadius: "8px",
228
- border: "1px solid var(--widget-foreground-muted)",
229
- background: "transparent",
230
- color: "inherit",
231
- font: "inherit"
232
- });
233
- form.append(field);
234
- const error = el("p");
235
- error.id = `${id}-error`;
236
- error.setAttribute("aria-live", "polite");
237
- Object.assign(error.style, {
238
- margin: "0",
239
- minHeight: "0",
240
- color: "var(--widget-foreground)"
225
+ field.id = `${rowId}-input`;
226
+ field.name = spec.field;
227
+ Object.assign(field.style, {
228
+ width: "100%",
229
+ boxSizing: "border-box",
230
+ padding: "8px 10px",
231
+ borderRadius: "8px",
232
+ border: "1px solid var(--widget-foreground-muted)",
233
+ background: "transparent",
234
+ color: "inherit",
235
+ font: "inherit"
236
+ });
237
+ form.append(field);
238
+ const error = el("p");
239
+ error.id = `${rowId}-error`;
240
+ error.setAttribute("aria-live", "polite");
241
+ Object.assign(error.style, {
242
+ margin: "0",
243
+ minHeight: "0",
244
+ color: "var(--widget-foreground)"
245
+ });
246
+ form.append(error);
247
+ describedBy.push(error.id);
248
+ field.setAttribute("aria-describedby", describedBy.join(" "));
249
+ return {
250
+ spec,
251
+ field,
252
+ error,
253
+ choices,
254
+ done: false
255
+ };
241
256
  });
242
- form.append(error);
243
- describedBy.push(error.id);
244
- field.setAttribute("aria-describedby", describedBy.join(" "));
245
257
  let consent = null;
246
258
  if (card.consent) {
247
259
  const row = el("div");
@@ -299,10 +311,10 @@ function mountCaptureCard(root, options) {
299
311
  });
300
312
  actions.append(send, refuse);
301
313
  form.append(actions);
302
- const showError = (key) => {
303
- error.textContent = key === null ? "" : copy[key === "phone" && card.region ? "phoneLocal" : key];
304
- if (key === null) field.removeAttribute("aria-invalid");
305
- else field.setAttribute("aria-invalid", "true");
314
+ const showError = (row, key) => {
315
+ row.error.textContent = key === null ? "" : copy[key === "phone" && row.spec.region ? "phoneLocal" : key];
316
+ if (key === null) row.field.removeAttribute("aria-invalid");
317
+ else row.field.setAttribute("aria-invalid", "true");
306
318
  };
307
319
  const finish = (outcome) => {
308
320
  const done = el("p", outcome === "declined" ? copy.declined : options.checkout === "last" ? copy.checkoutReady : copy.saved);
@@ -315,63 +327,90 @@ function mountCaptureCard(root, options) {
315
327
  form.replaceWith(done);
316
328
  options.onAnswered(outcome);
317
329
  };
318
- const answer = async (body, outcome) => {
330
+ /**
331
+ * Sends one answer per detail still open, in order, and stops at the first
332
+ * that is not taken. In a card of several details, one that closed
333
+ * meanwhile (saved from the conversation) counts as answered. The card
334
+ * finishes once every detail sent is answered, as saved when any value was
335
+ * taken by any send of this card.
336
+ */
337
+ const answer = async (bodies) => {
319
338
  if (busy) return;
320
339
  busy = true;
321
340
  send.disabled = true;
322
341
  refuse.disabled = true;
323
- let status;
324
- try {
325
- status = await options.submit(body);
326
- } catch {
327
- status = null;
342
+ let failed = null;
343
+ for (const [row, body] of bodies) {
344
+ let status;
345
+ try {
346
+ status = await options.submit(body);
347
+ } catch {
348
+ status = null;
349
+ }
350
+ if (!live) return;
351
+ const next = captureCardOutcomeOf(status);
352
+ if (next === "done" || next === "closed" && rows[1]) {
353
+ row.done = true;
354
+ if (next === "done" && "value" in body) saved = true;
355
+ } else {
356
+ failed = [row, next];
357
+ break;
358
+ }
328
359
  }
329
- if (!live) return;
330
360
  busy = false;
331
361
  send.disabled = false;
332
362
  refuse.disabled = false;
333
- const next = captureCardOutcomeOf(status);
334
- if (next === "done") {
335
- finish(outcome);
363
+ if (failed === null) {
364
+ finish(saved ? "saved" : "declined");
336
365
  return;
337
366
  }
338
- showError(next);
367
+ const [row, next] = failed;
368
+ showError(row, next);
339
369
  if (next === "closed") {
340
370
  send.disabled = true;
341
371
  refuse.disabled = true;
342
- } else field.focus();
372
+ } else row.field.focus();
343
373
  };
344
374
  const onSubmit = (event) => {
345
375
  event.preventDefault();
346
- const checked = checkCaptureCardValue({
347
- input: choices !== null ? "choice" : card.input,
348
- ...card.maxLength !== void 0 ? { maxLength: card.maxLength } : {},
349
- ...choices !== null ? { choices } : {},
350
- ...card.region ? { region: card.region } : {}
351
- }, field.value);
352
- if (!checked.ok) {
353
- showError(checked.error);
354
- field.focus();
355
- return;
376
+ const bodies = [];
377
+ for (const row of rows) {
378
+ if (row.done) continue;
379
+ const { spec, field, choices } = row;
380
+ if (row !== rows[0] && field.value.trim() === "") {
381
+ showError(row, null);
382
+ continue;
383
+ }
384
+ const checked = checkCaptureCardValue({
385
+ input: choices !== null ? "choice" : spec.input,
386
+ ...spec.maxLength !== void 0 ? { maxLength: spec.maxLength } : {},
387
+ ...choices !== null ? { choices } : {},
388
+ ...spec.region ? { region: spec.region } : {}
389
+ }, field.value);
390
+ if (!checked.ok) {
391
+ showError(row, checked.error);
392
+ field.focus();
393
+ return;
394
+ }
395
+ showError(row, null);
396
+ bodies.push([row, {
397
+ field: spec.field,
398
+ value: checked.value,
399
+ ...consent !== null && row === rows[0] ? { consent: consent.checked } : {}
400
+ }]);
356
401
  }
357
- showError(null);
358
- answer({
359
- field: card.field,
360
- value: checked.value,
361
- ...consent !== null ? { consent: consent.checked } : {}
362
- }, "saved");
402
+ answer(bodies);
363
403
  };
364
404
  const onRefuse = () => {
365
- showError(null);
366
- answer({
367
- field: card.field,
405
+ answer(rows.filter((row) => !row.done).map((row) => [row, {
406
+ field: row.spec.field,
368
407
  refused: true
369
- }, "declined");
408
+ }]));
370
409
  };
371
410
  form.addEventListener("submit", onSubmit);
372
411
  refuse.addEventListener("click", onRefuse);
373
412
  root.replaceChildren(form);
374
- if (options.autoFocus) field.focus();
413
+ if (options.autoFocus) rows[0].field.focus();
375
414
  return () => {
376
415
  live = false;
377
416
  form.removeEventListener("submit", onSubmit);
@@ -216,9 +216,8 @@ function cardText(max) {
216
216
  return string().trim().min(1).max(max).refine((text) => !hasUnsafeCardText(text), "must not contain control or format characters");
217
217
  }
218
218
  var FieldKeySchema = string().regex(CAPTURE_CARD_FIELD_PATTERN);
219
- /** The concierge asking the widget to show a card for one contact detail. */
220
- var WidgetChatCaptureCardEventSchema = object({
221
- type: literal(CAPTURE_CARD_EVENT_TYPE),
219
+ /** One detail a card asks for: its key, how it is typed in, and the tenant's wording. */
220
+ var CaptureCardFieldSchema = object({
222
221
  field: FieldKeySchema,
223
222
  input: _enum(CAPTURE_CARD_INPUTS).catch("text"),
224
223
  label: cardText(120),
@@ -226,7 +225,19 @@ var WidgetChatCaptureCardEventSchema = object({
226
225
  choices: array(cardText(120)).min(1).max(100).optional(),
227
226
  maxLength: number().int().min(1).max(CAPTURE_CARD_VALUE_MAX).optional(),
228
227
  /** A phone card only: the region a number without its country code is read in. */
229
- region: string().regex(CAPTURE_CARD_REGION_PATTERN).optional().catch(void 0),
228
+ region: string().regex(CAPTURE_CARD_REGION_PATTERN).optional().catch(void 0)
229
+ });
230
+ /**
231
+ * The concierge asking the widget to show a card for a contact detail, and,
232
+ * in `fields`, for the other details the same step still needs (SAA-512),
233
+ * answered together with one Send. Each is answered as its own
234
+ * `POST /widget/capture`. A widget built before `fields` shows the first
235
+ * detail alone, as before, and the concierge asks for the rest afterwards.
236
+ */
237
+ var WidgetChatCaptureCardEventSchema = CaptureCardFieldSchema.extend({
238
+ type: literal(CAPTURE_CARD_EVENT_TYPE),
239
+ /** More details in the same card, after the first; never the first again. */
240
+ fields: array(CaptureCardFieldSchema).min(1).max(3).optional().catch(void 0),
230
241
  /**
231
242
  * The tenant's consent wording for one outreach channel, shown beside an
232
243
  * unticked checkbox. The server keeps which wording version this is; the
@@ -741,7 +752,14 @@ var WidgetChatResumedEventSchema = object({ type: literal("resumed") });
741
752
  var WidgetChatReadEventSchema = object({ type: literal("read") });
742
753
  var WidgetChatDoneEventSchema = object({
743
754
  type: literal("done"),
744
- turnId: string().min(1)
755
+ turnId: string().min(1),
756
+ /**
757
+ * The turn failed (SAA-512): what streamed before this was the failed-turn
758
+ * line (or the budget line), not a reply. For the server's own consumers
759
+ * (a text channel, a hand-back, the test console); the widget draws the line
760
+ * like any reply and ignores this.
761
+ */
762
+ failed: literal(true).optional()
745
763
  });
746
764
  var WidgetChatErrorEventSchema = object({
747
765
  type: literal("error"),