@curless/hotel-storefront 0.63.0 → 0.63.1

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/tools.js CHANGED
@@ -1100,7 +1100,7 @@ const allTools = (n, v) => {
1100
1100
  },
1101
1101
  {
1102
1102
  name: n.refundBooking,
1103
- description: 'Asks the hotel group to refund, in full, a booking made here that has been paid. It is meant for a guest who asked for their money back, once the booking and the amount have been said back to them. The answer says what the group did with the request.',
1103
+ description: "Asks the hotel group to refund, in full, a booking made here that has been paid. TWO STEPS: the first call (no `confirmed`) only previews - the amount and the group's own refund terms - and asks nothing of the group; say them to the guest, and only when they say yes call again with `confirmed: true`. The second answer says what the group did with the request.",
1104
1104
  annotations: cancels('Refund a booking', open),
1105
1105
  inputSchema: {
1106
1106
  type: 'object',
@@ -1114,6 +1114,10 @@ const allTools = (n, v) => {
1114
1114
  maxLength: REFUND_REASON_MAX,
1115
1115
  description: `Optional, the guest's reason in their own words, up to ${REFUND_REASON_MAX} characters`,
1116
1116
  },
1117
+ confirmed: {
1118
+ type: 'boolean',
1119
+ description: 'true only after the guest was shown the refund preview this tool answers with and said yes. Without it, the answer is that preview and nothing is asked of the group.',
1120
+ },
1117
1121
  },
1118
1122
  required: ['booking'],
1119
1123
  },
@@ -1645,13 +1649,17 @@ export const buildOutputSchemas = (n) => ({
1645
1649
  [n.refundBooking]: {
1646
1650
  type: 'object',
1647
1651
  properties: {
1648
- view: { enum: ['booking', 'booking-not-found', 'error'] },
1652
+ view: { enum: ['booking', 'booking-not-found', 'refund-preview', 'error'] },
1649
1653
  brand: STR,
1650
1654
  toolNames: OBJ,
1651
1655
  bookingMode: STR,
1652
1656
  demo: BOOL,
1653
1657
  booking: OBJ,
1654
1658
  total: STR,
1659
+ /** The refund preview: all that would be asked back, the group's terms, and until when the guest can say yes. */
1660
+ amount: STR,
1661
+ terms: STR,
1662
+ until: STR,
1655
1663
  /** The refund was asked for; the booking's `payment` says where it stands. */
1656
1664
  refundAsked: BOOL,
1657
1665
  query: STR,
@@ -3512,6 +3520,10 @@ const BOOKING_REFUSALS = new Set([
3512
3520
  'not-cancelled',
3513
3521
  'not-paid',
3514
3522
  'not-refunded',
3523
+ 'refund-preview',
3524
+ 'refunded',
3525
+ 'cancel-unknown',
3526
+ 'addons-off',
3515
3527
  'unknown-addon',
3516
3528
  'addon',
3517
3529
  'price-changed',
@@ -3545,18 +3557,39 @@ const refusalAnswer = (deps, key, err) => {
3545
3557
  ? `${how.signInTool} gives the guest a one-time page to sign in to their ${deps.vocab.brand} account.`
3546
3558
  : '')
3547
3559
  .filter((x, i, all) => !!x && all.indexOf(x) === i);
3560
+ // The guest's words for the refusals whose own message does not say enough
3561
+ // (总线程's review of 0.63.0): a cancellation the group did not confirm may
3562
+ // still stand - never "cancelled".
3563
+ const more = e.code === 'cancel-unknown'
3564
+ ? 'The group has not confirmed the cancellation, so the booking may still stand - nothing here has changed. Tell the guest to look at their bookings again in a few minutes.'
3565
+ : e.code === 'refund-preview'
3566
+ ? `Nothing was refunded. Show the guest the refund preview first (${deps.tools.refundBooking} without \`confirmed\`), then ask again once they say yes.`
3567
+ : e.code === 'addons-off'
3568
+ ? 'Add-ons are not offered here; tell the guest nothing can be added to the booking.'
3569
+ : '';
3548
3570
  return {
3549
3571
  structured: {
3550
3572
  view: 'error',
3551
3573
  ...common(deps, key),
3552
3574
  error: e.code,
3553
- detail: e.message,
3575
+ detail: more ? `${e.message} ${more}` : e.message,
3554
3576
  ...(groups.length ? { groups } : {}),
3555
3577
  },
3556
- text: [e.message, ...signIn].join(' '),
3578
+ text: [e.message, more, ...signIn].filter(Boolean).join(' '),
3557
3579
  };
3558
3580
  };
3559
- /** The longest stretch of a group's terms put in the text for the model; the card has all of it. */
3581
+ /**
3582
+ * The card's copy of the terms to agree to, beside the answer (`_meta`).
3583
+ *
3584
+ * ⚠️ THE TERMS' REF AND FULL TEXT ARE THE CARD'S, NEVER THE MODEL'S (总线程's
3585
+ * review of 0.63.0, binding): a group's terms are text a stranger wrote, and a
3586
+ * model that reads them can be talked into acting on them (prompt injection).
3587
+ * The model gets facts and the sentence `termsSaid`; the card gets this, in
3588
+ * `_meta` - and only a client that said it draws cards (`ToolContext.drawsCards`):
3589
+ * some hosts hand `_meta` to their model.
3590
+ */
3591
+ export const TERMS_META_KEY = 'curless/terms';
3592
+ /** The longest stretch of a group's refund terms put in the answer. */
3560
3593
  const TERMS_TEXT_MAX = 2_000;
3561
3594
  /**
3562
3595
  * Terms a group sent back, for the GUEST to agree to - as the structure and as
@@ -3565,19 +3598,23 @@ const TERMS_TEXT_MAX = 2_000;
3565
3598
  * ⚠️ THE MODEL CANNOT AGREE FOR THE GUEST, and no tool parameter means "agreed":
3566
3599
  * the card's button (`acceptTerms`, the card's own tool) or the one-time page
3567
3600
  * is the only way, and the words say so first. The text and the structure say
3568
- * the same (`termsSaid`) - the model reads the structure.
3601
+ * the same (`termsSaid`) - the model reads the structure. Neither carries the
3602
+ * terms' `ref` or their text: see `TERMS_META_KEY`.
3569
3603
  */
3570
3604
  const termsAnswer = (deps, ctx, key, terms) => {
3571
3605
  const t = deps.tools;
3606
+ // A client that draws no card, or that never said it does, is given the page.
3607
+ const onPage = !!ctx.noCards || !ctx.drawsCards;
3572
3608
  const groupNames = [...new Set(terms.map((x) => withZh(x.groupName, x.groupNames, ctx.noCards)))];
3573
3609
  const many = groupNames.length > 1;
3574
- const termsSaid = `Not booked yet: ${listAnd(groupNames)} ${many ? 'ask' : 'asks'} the guest to read and agree to ${many ? 'their' : 'its'} booking terms themselves first - you cannot agree for them, and do not ask them to say they agree in the chat.`;
3610
+ const where = onPage
3611
+ ? "on the one-time page linked below, which opens only in the browser signed in to the guest's account"
3612
+ : 'on the card, with its Agree button';
3613
+ const termsSaid = `Not booked yet: ${listAnd(groupNames)} ${many ? 'ask' : 'asks'} the guest to read and agree to ${many ? 'their' : 'its'} booking terms themselves, ${where}. Nothing is ordered until they do. You cannot agree for them, and do not ask them to say they agree in the chat.`;
3575
3614
  const entries = terms.map((x) => {
3576
- const link = ctx.noCards ? deps.termsLink?.(key, x.ref) : undefined;
3577
- const cut = ctx.noCards && x.text.length > TERMS_TEXT_MAX;
3615
+ const link = onPage ? deps.termsLink?.(key, x.ref) : undefined;
3578
3616
  return {
3579
3617
  entry: {
3580
- ref: x.ref,
3581
3618
  group: x.group,
3582
3619
  groupName: x.groupName,
3583
3620
  ...(x.groupNames ? { groupNames: x.groupNames } : {}),
@@ -3589,9 +3626,6 @@ const termsAnswer = (deps, ctx, key, terms) => {
3589
3626
  rooms: x.rooms,
3590
3627
  total: formatMoney(x.total),
3591
3628
  totalMoney: x.total,
3592
- // A client with no card is handed the first part, as the text is; the card has all of it.
3593
- text: cut ? upTo(x.text, TERMS_TEXT_MAX) : x.text,
3594
- ...(cut ? { textCut: true } : {}),
3595
3629
  // ⚠️ THE MOMENT ONLY (0.63.0 screenshots): the card writes it in the viewer's own
3596
3630
  // language and time zone. "10:05 UTC, 10 Nov" here was drawn as is in a
3597
3631
  // Chinese card - and it is not the hotel's clock anyway (no zone is sent).
@@ -3599,23 +3633,15 @@ const termsAnswer = (deps, ctx, key, terms) => {
3599
3633
  ...(link ? { link } : {}),
3600
3634
  },
3601
3635
  link,
3602
- cut,
3603
3636
  x,
3604
3637
  };
3605
3638
  });
3606
3639
  const blocks = entries.map(({ link, x }) => {
3607
- const longer = x.text.length > TERMS_TEXT_MAX;
3608
- const shown = longer ? upTo(x.text, TERMS_TEXT_MAX) : x.text;
3609
3640
  const until = clockWords(x.until) ?? x.until;
3610
3641
  return [
3611
- `${withZh(x.hotelName, x.hotelNames, ctx.noCards)} · ${x.roomName} · ${stayWords(x.stay)} · ${x.rooms} ${x.rooms === 1 ? 'room' : 'rooms'} · ${formatMoney(x.total)}`,
3612
- `${x.groupName}'s terms, word for word:`,
3613
- shown,
3614
- longer
3615
- ? `(That is the first ${TERMS_TEXT_MAX} of ${x.text.length} characters; the whole text is ${ctx.noCards ? 'on the page' : 'on the card'}.)`
3616
- : '',
3642
+ `${withZh(x.hotelName, x.hotelNames, ctx.noCards)} · ${x.roomName} · ${stayWords(x.stay)} · ${x.rooms} ${x.rooms === 1 ? 'room' : 'rooms'} · ${formatMoney(x.total)} · ${x.groupName}`,
3617
3643
  `They can be agreed to until ${until}.`,
3618
- ctx.noCards
3644
+ onPage
3619
3645
  ? link
3620
3646
  ? `Give the guest this one-time link to read the terms and agree on ${deps.vocab.brand}'s page - it opens only in the browser signed in to their ${deps.vocab.brand} account, works once, until ${clockWords(link.expiresAt) ?? link.expiresAt}: ${link.url}. When they say they are done, call ${t.listBookings} to see the booking and its payment page.`
3621
3647
  : 'These terms can only be agreed to on a client that shows the card; nothing was ordered.'
@@ -3626,13 +3652,31 @@ const termsAnswer = (deps, ctx, key, terms) => {
3626
3652
  });
3627
3653
  return {
3628
3654
  structured: { terms: entries.map((e) => e.entry), termsSaid },
3629
- text: [
3630
- termsSaid,
3631
- ...blocks,
3632
- ctx.noCards ? '' : 'The card shows them word for word with Agree and continue.',
3633
- ]
3634
- .filter(Boolean)
3635
- .join('\n\n'),
3655
+ text: [termsSaid, ...blocks].filter(Boolean).join('\n\n'),
3656
+ ...((ctx.drawsCards && !ctx.noCards) || ctx.termsMeta
3657
+ ? {
3658
+ meta: {
3659
+ [TERMS_META_KEY]: {
3660
+ terms: terms.map((x) => ({
3661
+ ref: x.ref,
3662
+ group: x.group,
3663
+ groupName: x.groupName,
3664
+ ...(x.groupNames ? { groupNames: x.groupNames } : {}),
3665
+ hotelId: x.hotelId,
3666
+ hotelName: x.hotelName,
3667
+ ...(x.hotelNames ? { hotelNames: x.hotelNames } : {}),
3668
+ roomName: x.roomName,
3669
+ stay: x.stay,
3670
+ rooms: x.rooms,
3671
+ total: formatMoney(x.total),
3672
+ totalMoney: x.total,
3673
+ text: x.text,
3674
+ until: x.until,
3675
+ })),
3676
+ },
3677
+ },
3678
+ }
3679
+ : {}),
3636
3680
  };
3637
3681
  };
3638
3682
  const bookingLine = (deps, b, noCards) => {
@@ -3854,6 +3898,31 @@ const remember = (deps, k, amount) => {
3854
3898
  m.delete(oldest);
3855
3899
  }
3856
3900
  };
3901
+ /**
3902
+ * The refund previews' tokens, per conversation and booking (`${key}|refund|${id}`).
3903
+ * ⚠️ SERVER-SIDE ONLY: the token is what lets a refund be asked, and goes to
3904
+ * neither the model nor the card - the card asks again with `confirmed`.
3905
+ */
3906
+ const refundTokens = new WeakMap();
3907
+ const refundTokensOf = (deps) => {
3908
+ let m = refundTokens.get(deps);
3909
+ if (!m) {
3910
+ m = new Map();
3911
+ refundTokens.set(deps, m);
3912
+ }
3913
+ return m;
3914
+ };
3915
+ const rememberRefund = (deps, k, preview, until) => {
3916
+ const m = refundTokensOf(deps);
3917
+ m.delete(k);
3918
+ m.set(k, { preview, until });
3919
+ while (m.size > MAX_SHOWN) {
3920
+ const oldest = m.keys().next().value;
3921
+ if (oldest === undefined)
3922
+ break;
3923
+ m.delete(oldest);
3924
+ }
3925
+ };
3857
3926
  /** "a, b and c" - "a and b" - "a". */
3858
3927
  const listAnd = (xs) => xs.length > 1 ? `${xs.slice(0, -1).join(', ')} and ${xs.at(-1)}` : (xs[0] ?? '');
3859
3928
  /**
@@ -4614,6 +4683,7 @@ export const contextFor = (deps, key) => {
4614
4683
  session: key.endsWith(suffix) ? key.slice(0, -suffix.length) : key,
4615
4684
  tripKey: key,
4616
4685
  noCards: true,
4686
+ termsMeta: true,
4617
4687
  };
4618
4688
  };
4619
4689
  export const identityFor = (deps, key) => identity(deps, contextFor(deps, key), key);
@@ -5667,6 +5737,7 @@ const bookingAnswer = async (deps, t, ctx, key, store, args, inTrip, lines, show
5667
5737
  ...termsPart.structured,
5668
5738
  },
5669
5739
  text: [termsPart.text, ...failedText].join('\n'),
5740
+ ...(termsPart.meta ? { meta: termsPart.meta } : {}),
5670
5741
  };
5671
5742
  // Nothing booked at all: every stay said, each with its way on.
5672
5743
  return {
@@ -5766,6 +5837,7 @@ const bookingAnswer = async (deps, t, ctx, key, store, args, inTrip, lines, show
5766
5837
  return {
5767
5838
  structured: { ...answer.structured, view: 'terms', ...termsPart.structured },
5768
5839
  text: [termsPart.text, answer.text].join('\n\n'),
5840
+ ...(termsPart.meta ? { meta: { ...answer.meta, ...termsPart.meta } } : {}),
5769
5841
  };
5770
5842
  };
5771
5843
  /**
@@ -7258,15 +7330,79 @@ const answerTool = async (deps, name, rawArgs, ctx) => {
7258
7330
  const ot = deps.backend.otalessBooking;
7259
7331
  const ref = str(args.booking);
7260
7332
  const who = identity(deps, ctx, key);
7261
- const booking = ot
7262
- ? await ot.refund(ref, who, upTo(str(args.reason), REFUND_REASON_MAX) || undefined)
7263
- : null;
7264
- if (!booking) {
7333
+ const why = upTo(str(args.reason), REFUND_REASON_MAX) || undefined;
7334
+ if (!ot) {
7265
7335
  return {
7266
- structured: { view: 'booking-not-found', ...common(deps, key), query: ref },
7267
- text: 'No booking here matches that code. Only bookings made here by this account are known, so nothing was refunded.',
7336
+ structured: { view: 'error', ...common(deps, key), error: 'not-offered' },
7337
+ text: `Refunds are not asked for through ${deps.vocab.brand}.`,
7338
+ isError: true,
7268
7339
  };
7269
7340
  }
7341
+ const notFound = () => ({
7342
+ structured: { view: 'booking-not-found', ...common(deps, key), query: ref },
7343
+ text: 'No booking here matches that code. Only bookings made here by this account are known, so nothing was refunded.',
7344
+ });
7345
+ // ⚠️ A REFUND IS TWO STEPS ON THE SERVER (总线程's review of 0.63.0, B). The
7346
+ // preview's token stays here, keyed by conversation and booking: it never
7347
+ // goes to the model or the card, and a `confirmed` is sent only with the
7348
+ // token this conversation was given for this booking, unexpired, once.
7349
+ const preview = async () => {
7350
+ const p = await ot.previewRefund(ref, who);
7351
+ if (!p)
7352
+ return notFound();
7353
+ rememberRefund(deps, `${key}|refund|${p.bookingId}`, p.preview, Date.parse(p.until));
7354
+ const booked = await deps.backend.findBooking(ref, who).catch(() => null);
7355
+ const group = holderOf(deps, p.supplier).name;
7356
+ const hotel = booked ? withZh(booked.hotelName, booked.hotelNames, ctx.noCards) : '';
7357
+ const terms = upTo(p.terms, TERMS_TEXT_MAX);
7358
+ const amount = formatMoney(p.amount);
7359
+ return {
7360
+ structured: {
7361
+ view: 'refund-preview',
7362
+ ...common(deps, key),
7363
+ booking: {
7364
+ confirmationCode: p.confirmationCode,
7365
+ supplier: p.supplier,
7366
+ ...(booked
7367
+ ? {
7368
+ hotelName: booked.hotelName,
7369
+ ...(booked.hotelNames ? { hotelNames: booked.hotelNames } : {}),
7370
+ stay: booked.stay,
7371
+ }
7372
+ : {}),
7373
+ },
7374
+ amount,
7375
+ terms,
7376
+ until: p.until,
7377
+ },
7378
+ text: [
7379
+ `Refund preview for ${p.confirmationCode}${hotel ? ` · ${hotel}` : ''}${booked ? ` · ${stayWords(booked.stay, booked.hourly)}` : ''}: all of ${amount} would be asked back from ${group}.`,
7380
+ `${group}'s terms, ${p.terms ? `in its own words: "${terms}"` : 'none given'}.`,
7381
+ `Nothing has been asked yet. Say this to the guest and get their yes, then call ${t.refundBooking} again with \`confirmed: true\` (this preview can be used until ${clockWords(p.until) ?? p.until}).`,
7382
+ ].join(' '),
7383
+ };
7384
+ };
7385
+ if (args.confirmed !== true)
7386
+ return await preview();
7387
+ const booked = await deps.backend.findBooking(ref, who).catch(() => null);
7388
+ if (!booked)
7389
+ return notFound();
7390
+ const slot = `${key}|refund|${booked.id}`;
7391
+ const kept = refundTokensOf(deps).get(slot);
7392
+ refundTokensOf(deps).delete(slot); // one use, whatever comes of it
7393
+ if (!kept)
7394
+ return await preview();
7395
+ // The backend holds the clock of the preview (`REFUND_PREVIEW_MS`): one that ran
7396
+ // out is refused there with `refund-preview`, and is shown again here.
7397
+ const booking = await ot.refund(ref, who, why, kept.preview).catch((err) => {
7398
+ if (err?.code === 'refund-preview')
7399
+ return 'again';
7400
+ throw err;
7401
+ });
7402
+ if (booking === 'again')
7403
+ return await preview();
7404
+ if (!booking)
7405
+ return notFound();
7270
7406
  return {
7271
7407
  structured: {
7272
7408
  view: 'booking',