@curless/hotel-storefront 0.69.3 → 0.70.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 (92) hide show
  1. package/README.md +11 -0
  2. package/dist/account/index.d.ts +21 -0
  3. package/dist/account/index.d.ts.map +1 -1
  4. package/dist/account/index.js +63 -4
  5. package/dist/account/index.js.map +1 -1
  6. package/dist/config.d.ts +12 -0
  7. package/dist/config.d.ts.map +1 -1
  8. package/dist/config.js.map +1 -1
  9. package/dist/huazhu/client.d.ts +3 -1
  10. package/dist/huazhu/client.d.ts.map +1 -1
  11. package/dist/huazhu/client.js +26 -0
  12. package/dist/huazhu/client.js.map +1 -1
  13. package/dist/huazhu/english.d.ts.map +1 -1
  14. package/dist/huazhu/english.js +11 -0
  15. package/dist/huazhu/english.js.map +1 -1
  16. package/dist/otaless/backend.d.ts.map +1 -1
  17. package/dist/otaless/backend.js +17 -6
  18. package/dist/otaless/backend.js.map +1 -1
  19. package/dist/otaless/booking.d.ts +11 -4
  20. package/dist/otaless/booking.d.ts.map +1 -1
  21. package/dist/otaless/booking.js +95 -11
  22. package/dist/otaless/booking.js.map +1 -1
  23. package/dist/otaless/fake.d.ts +7 -2
  24. package/dist/otaless/fake.d.ts.map +1 -1
  25. package/dist/otaless/fake.js +4 -2
  26. package/dist/otaless/fake.js.map +1 -1
  27. package/dist/otaless/places.d.ts.map +1 -1
  28. package/dist/otaless/places.js +16 -2
  29. package/dist/otaless/places.js.map +1 -1
  30. package/dist/regions.d.ts.map +1 -1
  31. package/dist/regions.js +25 -2
  32. package/dist/regions.js.map +1 -1
  33. package/dist/server.d.ts +4 -1
  34. package/dist/server.d.ts.map +1 -1
  35. package/dist/server.js +280 -13
  36. package/dist/server.js.map +1 -1
  37. package/dist/source-allowlist.d.ts +9 -0
  38. package/dist/source-allowlist.d.ts.map +1 -1
  39. package/dist/source-allowlist.js +23 -1
  40. package/dist/source-allowlist.js.map +1 -1
  41. package/dist/state.d.ts +25 -2
  42. package/dist/state.d.ts.map +1 -1
  43. package/dist/state.js +88 -15
  44. package/dist/state.js.map +1 -1
  45. package/dist/tools.d.ts.map +1 -1
  46. package/dist/tools.js +61 -2
  47. package/dist/tools.js.map +1 -1
  48. package/dist/usage/boot.d.ts +32 -0
  49. package/dist/usage/boot.d.ts.map +1 -0
  50. package/dist/usage/boot.js +140 -0
  51. package/dist/usage/boot.js.map +1 -0
  52. package/dist/usage/geo.d.ts +48 -0
  53. package/dist/usage/geo.d.ts.map +1 -0
  54. package/dist/usage/geo.js +347 -0
  55. package/dist/usage/geo.js.map +1 -0
  56. package/dist/usage/maintain.d.ts +26 -0
  57. package/dist/usage/maintain.d.ts.map +1 -0
  58. package/dist/usage/maintain.js +177 -0
  59. package/dist/usage/maintain.js.map +1 -0
  60. package/dist/usage/platform.d.ts +69 -0
  61. package/dist/usage/platform.d.ts.map +1 -0
  62. package/dist/usage/platform.js +124 -0
  63. package/dist/usage/platform.js.map +1 -0
  64. package/dist/usage/pseudonym.d.ts +28 -0
  65. package/dist/usage/pseudonym.d.ts.map +1 -0
  66. package/dist/usage/pseudonym.js +73 -0
  67. package/dist/usage/pseudonym.js.map +1 -0
  68. package/dist/usage/range.d.ts +24 -0
  69. package/dist/usage/range.d.ts.map +1 -0
  70. package/dist/usage/range.js +102 -0
  71. package/dist/usage/range.js.map +1 -0
  72. package/dist/usage/recorder.d.ts +115 -0
  73. package/dist/usage/recorder.d.ts.map +1 -0
  74. package/dist/usage/recorder.js +227 -0
  75. package/dist/usage/recorder.js.map +1 -0
  76. package/dist/usage/report.d.ts +207 -0
  77. package/dist/usage/report.d.ts.map +1 -0
  78. package/dist/usage/report.js +730 -0
  79. package/dist/usage/report.js.map +1 -0
  80. package/dist/usage/shadow-import.d.ts +28 -0
  81. package/dist/usage/shadow-import.d.ts.map +1 -0
  82. package/dist/usage/shadow-import.js +107 -0
  83. package/dist/usage/shadow-import.js.map +1 -0
  84. package/dist/usage/store.d.ts +215 -0
  85. package/dist/usage/store.d.ts.map +1 -0
  86. package/dist/usage/store.js +430 -0
  87. package/dist/usage/store.js.map +1 -0
  88. package/dist/usage-cli.d.ts +3 -0
  89. package/dist/usage-cli.d.ts.map +1 -0
  90. package/dist/usage-cli.js +152 -0
  91. package/dist/usage-cli.js.map +1 -0
  92. package/package.json +3 -2
package/dist/server.js CHANGED
@@ -17,13 +17,14 @@ import { MemberStore } from './members.js';
17
17
  import { paidAnswer } from './paid.js';
18
18
  import { PICTURES_PATH, PictureStore, picturesAs } from './pictures.js';
19
19
  import { Sessions, cardSupport } from './sessions.js';
20
- import { REFUSED_SAID, checkSourceAllowlist, makeSourceAllowlist, signatureOf, sourceAddress, } from './source-allowlist.js';
20
+ import { REFUSED_SAID, SOURCE_FILE_MAX_BYTES, checkSourceAllowlist, makeSourceAllowlist, signatureOf, sourceAddress, } from './source-allowlist.js';
21
21
  import { StateDir } from './state.js';
22
22
  import { makeSupplier } from './supplier.js';
23
23
  import { buildInstructions, listTools, tripToolNames } from './surface.js';
24
24
  import { TERMS_META_KEY, agreeToTerms, bookingRefusal, buildOutputSchemas, buildTools, commonFields, contextFor, handleTool, homeListing, identityFor, outputSchemasFor, tripGone, withheldTools, } from './tools.js';
25
25
  import { ID_TTL_MS, SAME_NETWORK_MS, TAKE_OVER_MS, TripIds, networkOf } from './trip-ids.js';
26
26
  import { TripStore, tripKey } from './trip.js';
27
+ import { startUsage } from './usage/boot.js';
27
28
  import { checkDemoData, resolveBrand } from './vocab.js';
28
29
  // The connector itself: one config in, one HTTP app out.
29
30
  //
@@ -53,7 +54,7 @@ export const INITIALIZE_PER_MINUTE = 300;
53
54
  * number. Found by reading the boot banner of a container that had just been
54
55
  * deployed (2026-09-22): seventeen tools listed, and "0.7.0" above them.
55
56
  */
56
- export const VERSION = '0.69.3';
57
+ export const VERSION = '0.70.0';
57
58
  /**
58
59
  * The card's resource URI.
59
60
  *
@@ -325,14 +326,28 @@ export const buildServer = (opts) => {
325
326
  console.error(`[source] fault ${doing} (${e instanceof Error ? e.name : typeof e}) - ${outcome}; ${sourceFaults} so far`);
326
327
  };
327
328
  // Before anything else is built: an allowlist written wrong stops the start.
329
+ const SOURCE_FILE = 'source-shadow.json';
328
330
  const sources = makeSourceAllowlist(config.sourceAllowlist, opts.env ?? process.env, {
329
331
  now,
330
332
  ...(opts.hostOf ? { hostOf: opts.hostOf } : {}),
331
333
  onFault: (doing, e) => sourceFault(doing, e, 'its line not written'),
332
334
  // The shadow's samples survive a deploy (总线程 11-12). A write that fails
333
335
  // is the state directory's trouble, on /healthz/detail - never the request's.
334
- ...(config.sourceAllowlist ? { keep: state.file('source-shadow.json') } : {}),
336
+ // Read at most `SOURCE_FILE_MAX_BYTES`, never through a link, and deleted - never
337
+ // set aside - when it cannot be used (0.69.4): a copy kept aside would outlive the
338
+ // 30 days the privacy page promises.
339
+ ...(config.sourceAllowlist
340
+ ? {
341
+ keep: state.file(SOURCE_FILE, { maxBytes: SOURCE_FILE_MAX_BYTES, corrupt: 'delete' }),
342
+ }
343
+ : {}),
335
344
  });
345
+ // ⚠️ OFF IS GONE (0.69.4, 总线程's review of 0.69.3): turned off - by the config or
346
+ // HOTEL_SOURCE_ALLOWLIST - nothing writes the file or prunes it any more, so it
347
+ // is deleted at start, with any copy an older version set aside. On, only those copies.
348
+ if (state.remove(SOURCE_FILE, true, sources !== undefined)) {
349
+ console.log(`[source] ${sources ? 'copies of the kept shadow counts set aside by an older version' : 'the kept shadow counts (the allowlist is off)'} deleted`);
350
+ }
336
351
  const publicUrl = (opts.publicUrl ?? config.urls.publicUrl ?? config.urls.appUrl).replace(/\/$/, '');
337
352
  /**
338
353
  * The `data:` pictures the backend sent, served from our own address - see pictures.ts.
@@ -515,6 +530,21 @@ export const buildServer = (opts) => {
515
530
  if (url)
516
531
  groupLogos.set(g.code, url);
517
532
  }
533
+ // The usage statistics (usage/, docs/usage-events.md): one line per connection,
534
+ // tool call, stay put in the trip, booking outcome and account step - the
535
+ // platform, pseudonyms, a country, a group. Only where the config turns them on
536
+ // (robin 11-13: Huazhu's production and Otaless); never in a request's way, and
537
+ // nothing a tool, a description, the card or the privacy page says changes with it.
538
+ const usageRoles = new Map();
539
+ const usage = startUsage({
540
+ deployment: config.usage?.deployment ?? config.id,
541
+ enabled: config.usage?.enabled === true,
542
+ env: opts.env ?? process.env,
543
+ stateDir: state.dir,
544
+ roleOf: (tool) => usageRoles.get(tool) ?? 'other',
545
+ now,
546
+ quiet: opts.background === false,
547
+ }) ?? undefined;
518
548
  // The deployment's own account, where it has one - see account/index.ts.
519
549
  const account = config.account
520
550
  ? makeAccount({
@@ -536,6 +566,19 @@ export const buildServer = (opts) => {
536
566
  // A hotel group's logo for its membership row and link page (0.61.0): the
537
567
  // address the hotel list uses, served below - none, its initials.
538
568
  groupLogo: (code) => groupLogos.get(code),
569
+ // Sign-ins, accounts made, memberships linked - by category only, against
570
+ // the conversation that began them (usage/recorder.ts bindKey).
571
+ ...(usage
572
+ ? {
573
+ onUsage: (e) => {
574
+ const f = usage.recorder.factsOf(e.key);
575
+ if (e.kind === 'member')
576
+ usage.recorder.member(f, e);
577
+ else
578
+ usage.recorder.account(f, e);
579
+ },
580
+ }
581
+ : {}),
539
582
  ...(config.legal.operator ? { operator: config.legal.operator } : {}),
540
583
  // The pages in the deployment's own colour, as its card is (0.64.0 暖纸).
541
584
  ...(primaryOf(config.server.primary)?.color
@@ -654,6 +697,61 @@ export const buildServer = (opts) => {
654
697
  ]);
655
698
  const WITHHELD = withheldTools(brand.tools, caps);
656
699
  deps.offered = OFFERED;
700
+ // What each tool is for, in the usage format's ten roles (docs/usage-events.md §4).
701
+ if (usage) {
702
+ const t = brand.tools;
703
+ const role = (r, names) => {
704
+ for (const n of names)
705
+ if (n)
706
+ usageRoles.set(n, r);
707
+ };
708
+ role('browse', [t.listHotels, t.lookUp, t.compare, t.itinerary]);
709
+ role('product', [t.getHotel]);
710
+ role('cart', [t.addToTrip, t.viewTrip, t.clearTrip]);
711
+ role('checkout', [
712
+ t.requestBooking,
713
+ t.bookPicked,
714
+ t.pickRoom,
715
+ t.acceptTerms,
716
+ t.payWithUnionPay,
717
+ ]);
718
+ role('order', [
719
+ t.listBookings,
720
+ t.previewCancellation,
721
+ t.cancelBooking,
722
+ t.refundBooking,
723
+ t.previewChange,
724
+ t.changeBooking,
725
+ t.listBookingAddons,
726
+ t.addBookingAddon,
727
+ ]);
728
+ role('account', [
729
+ t.signIn,
730
+ t.signOut,
731
+ t.memberCard,
732
+ ...(account ? Object.values(account.names) : []),
733
+ ]);
734
+ role('policy', [t.policies, t.listExtras]);
735
+ }
736
+ /** The groups a market's events may name (`shop`): Otaless's, as its config lists them. */
737
+ const usageGroups = new Set(config.data.source === 'otaless' ? (config.data.otaless?.groups ?? []).map((g) => g.code) : []);
738
+ const knownGroup = (g) => typeof g === 'string' && usageGroups.has(g) ? g : undefined;
739
+ /** A hotel's group - only one this market lists, never the caller's prefix as typed. */
740
+ const groupOfHotel = (hotelId) => usageGroups.size && typeof hotelId === 'string' && hotelId
741
+ ? knownGroup(backend.supplierOf?.(hotelId))
742
+ : undefined;
743
+ /** The one group every booking in an answer is with - or none. */
744
+ const onlyGroup = (s) => {
745
+ const list = (Array.isArray(s.bookings) ? s.bookings : s.booking ? [s.booking] : []);
746
+ const groups = new Set(list.map((b) => knownGroup(b?.supplier)));
747
+ return groups.size === 1 ? [...groups][0] : undefined;
748
+ };
749
+ /** The tools whose answer is a booking's outcome. Not UnionPay's: it pays a booking already made. */
750
+ const BOOKING_RESULTS = new Set([
751
+ brand.tools.requestBooking,
752
+ brand.tools.bookPicked,
753
+ brand.tools.acceptTerms,
754
+ ]);
657
755
  const OUTPUT_SCHEMAS = outputSchemasFor(buildOutputSchemas(brand.tools), caps, deps.lang, {
658
756
  logoDark: !!logoDarkUrl,
659
757
  accent: !!deps.accent,
@@ -704,6 +802,21 @@ export const buildServer = (opts) => {
704
802
  // a press on the card, never the model's - no tool it can see agrees.
705
803
  brand.tools.acceptTerms,
706
804
  ]);
805
+ /**
806
+ * An answer's `toolNames` without the card's own tools - for a session that said it draws
807
+ * no card (0.69.4, 总线程 11-12): its `tools/list` already leaves them out (`toolList`) and
808
+ * a call to one is refused, but the map in `structuredContent` still named them, for the
809
+ * model to call. A card session's map is not touched.
810
+ */
811
+ const withoutAppOnly = (s) => {
812
+ const names = s.toolNames;
813
+ if (!names || typeof names !== 'object')
814
+ return s;
815
+ return {
816
+ ...s,
817
+ toolNames: Object.fromEntries(Object.entries(names).filter(([, n]) => !APP_ONLY.has(String(n)))),
818
+ };
819
+ };
707
820
  /** App-only tools with a refusal of their own for a client that draws no card (see tools/call). */
708
821
  const OWN_REFUSALS = new Set([
709
822
  ...(account ? [account.names.card] : []),
@@ -961,6 +1074,7 @@ export const buildServer = (opts) => {
961
1074
  ...made.health(),
962
1075
  ...(account?.health() ?? {}),
963
1076
  ...(memberLogin?.health() ?? {}),
1077
+ ...(usage ? { usage: usage.health() } : {}),
964
1078
  // New sessions by platform, and who came from elsewhere (by network only).
965
1079
  ...(sources
966
1080
  ? {
@@ -1044,13 +1158,143 @@ export const buildServer = (opts) => {
1044
1158
  app.delete('/mcp', (c) => c.body(null, 405, { Allow: 'GET, POST' }));
1045
1159
  app.get('/mcp', (c) => c.json({ name: config.server.name, transport: 'streamable-http', version: VERSION }));
1046
1160
  // JSON-RPC, both halves. `id ?? null` because a malformed request has no id
1047
- // and the spec still wants the field present.
1048
- const ok = (c, id, result) => c.json({ jsonrpc: '2.0', id: id ?? null, result });
1161
+ // and the spec still wants the field present. What a request was answered with
1162
+ // is kept beside it for the usage statistics to read after (`noteUsage`).
1163
+ const answered = new WeakMap();
1164
+ const ok = (c, id, result) => {
1165
+ if (usage)
1166
+ answered.set(c, result);
1167
+ return c.json({ jsonrpc: '2.0', id: id ?? null, result });
1168
+ };
1049
1169
  const fail = (c, id, code, message) => c.json({ jsonrpc: '2.0', id: id ?? null, error: { code, message } });
1050
1170
  app.post('/mcp', bodyLimit({
1051
1171
  maxSize: MAX_BODY_BYTES,
1052
1172
  onError: (c) => c.json({ jsonrpc: '2.0', id: null, error: { code: -32600, message: 'request too large' } }, 413),
1053
1173
  }), async (c) => {
1174
+ const startedAt = performance.now();
1175
+ const res = await answerMcp(c);
1176
+ if (usage)
1177
+ noteUsage(c, res, performance.now() - startedAt);
1178
+ return res;
1179
+ });
1180
+ /** What a request asked, for `noteUsage` - its method and parameters, nothing else kept. */
1181
+ const requestOf = new WeakMap();
1182
+ /** Who and where a tool call was, once its conversation is known (tools/call, below). */
1183
+ const usageNotes = new WeakMap();
1184
+ const usageFacts = (c, connection, conversation, client) => usage?.recorder.facts({ get: (n) => c.req.header(n) }, {
1185
+ peer: sourceOf(c),
1186
+ client: client ?? (connection ? sessions.clientOf(connection) : undefined),
1187
+ connection,
1188
+ conversation,
1189
+ });
1190
+ /**
1191
+ * The usage lines for a request just answered (usage/recorder.ts): a new
1192
+ * connection, or a tool call with what it came to - a stay put in the trip, a
1193
+ * booking's outcome, the guest's own bookings read. ⚠️ BEHIND THE ANSWER AND
1194
+ * NEVER IN ITS WAY: everything here is caught, and nothing is awaited.
1195
+ */
1196
+ const noteUsage = (c, res, ms) => {
1197
+ if (!usage)
1198
+ return;
1199
+ try {
1200
+ const r = usage.recorder;
1201
+ const { method, params } = requestOf.get(c) ?? {};
1202
+ if (method === 'initialize') {
1203
+ const session = res.headers.get('mcp-session-id');
1204
+ if (res.status !== 200 || !session)
1205
+ return;
1206
+ const said = params?.clientInfo?.name;
1207
+ const client = typeof said === 'string' ? said : undefined;
1208
+ // No conversation yet (recorder.ts `facts`): ChatGPT's own session id only.
1209
+ const f = usageFacts(c, session, undefined, client);
1210
+ if (f)
1211
+ r.init(f, { client, cards: cardSupport(params) });
1212
+ return;
1213
+ }
1214
+ if (method !== 'tools/call')
1215
+ return;
1216
+ const presented = c.req.header('mcp-session-id');
1217
+ if (!presented || !sessions.known(presented))
1218
+ return;
1219
+ // Never a name the caller made up: only this deployment's tools are counted.
1220
+ const name = String(params?.name ?? '');
1221
+ if (!OFFERED.has(name) && !WITHHELD.has(name))
1222
+ return;
1223
+ const args = (params?.arguments ?? {});
1224
+ const cardsSaid = sessions.cardsOf(presented);
1225
+ const fromCard = (cardsSaid === 'extension' || cardsSaid === 'host') && typeof args.trip === 'string';
1226
+ // The card's own polls are not a person's doing (docs/usage-events.md §3): a
1227
+ // booking waiting for its payment, asked after every few seconds, and the
1228
+ // account or membership panel watching a sign-in.
1229
+ const poll = (name === brand.tools.listBookings && fromCard && args.booking !== undefined) ||
1230
+ ((name === brand.tools.memberCard || name === account?.names.card) &&
1231
+ (args.action === undefined || args.action === 'view'));
1232
+ if (poll)
1233
+ return;
1234
+ const note = usageNotes.get(c);
1235
+ const key = note?.key ??
1236
+ (cardsSaid === 'none' ? sessions.boundOf(presented) : undefined) ??
1237
+ tripKey(presented, config.brand);
1238
+ const f = note?.facts ?? usageFacts(c, presented, key);
1239
+ if (!f)
1240
+ return;
1241
+ const result = answered.get(c);
1242
+ const s = result?.structuredContent ?? {};
1243
+ // Failed: said so the protocol's way, or an error view without it (a start
1244
+ // time it does not take, a room it does not have) - the guest got no answer.
1245
+ const failed = !result || result.isError === true || s.view === 'error';
1246
+ const why = failed
1247
+ ? String(s.code ?? s.error ?? (result ? 'error' : 'rpc-error'))
1248
+ : undefined;
1249
+ const shop = groupOfHotel(args.hotel ?? args.hotelId) ?? knownGroup(args.group) ?? onlyGroup(s);
1250
+ r.call(f, { tool: name, ok: !failed, ms, why, ...(shop ? { shop } : {}) });
1251
+ if (name === brand.tools.addToTrip && !failed && note?.tripBefore !== undefined) {
1252
+ const added = trips.read(key).length - note.tripBefore;
1253
+ if (added > 0)
1254
+ r.cartAdd(f, { tool: name, added, ...(shop ? { shop } : {}) });
1255
+ }
1256
+ if (BOOKING_RESULTS.has(name)) {
1257
+ const kind = name === brand.tools.requestBooking ? 'cart' : 'item';
1258
+ const via = name === brand.tools.requestBooking && !fromCard ? 'model' : 'card';
1259
+ const signedIn = account ? account.whoOf(key) !== undefined : undefined;
1260
+ const common = { kind, via, signedIn };
1261
+ if (failed)
1262
+ r.booking(f, { res: 'fail', ...common, why, shop });
1263
+ else {
1264
+ if (s.view === 'booking') {
1265
+ const made = (Array.isArray(s.bookings) ? s.bookings : s.booking ? [s.booking] : []);
1266
+ for (const b of made) {
1267
+ r.booking(f, {
1268
+ res: b?.pending === true ? 'checking' : 'ok',
1269
+ id: typeof b?.id === 'string' ? b.id : undefined,
1270
+ ...common,
1271
+ shop: knownGroup(b?.supplier) ?? groupOfHotel(b?.hotelId),
1272
+ });
1273
+ }
1274
+ }
1275
+ for (const x of (Array.isArray(s.failed) ? s.failed : [])) {
1276
+ r.booking(f, {
1277
+ res: 'fail',
1278
+ ...common,
1279
+ why: typeof x?.code === 'string' ? x.code : 'not-booked',
1280
+ shop: groupOfHotel(x?.hotelId),
1281
+ });
1282
+ }
1283
+ }
1284
+ }
1285
+ // Their own bookings read, signed in to the account: by the account, pseudonymised.
1286
+ if (name === brand.tools.listBookings && !failed && account) {
1287
+ const member = account.memberOf(key);
1288
+ if (member)
1289
+ r.account(f, { step: 'orders', member });
1290
+ }
1291
+ }
1292
+ catch (err) {
1293
+ usage.store.fault('note', err);
1294
+ }
1295
+ };
1296
+ /** The /mcp answer itself - `noteUsage` reads what it was, after. */
1297
+ async function answerMcp(c) {
1054
1298
  let body;
1055
1299
  try {
1056
1300
  body = await c.req.json();
@@ -1058,6 +1302,8 @@ export const buildServer = (opts) => {
1058
1302
  catch {
1059
1303
  return fail(c, null, -32700, 'parse error');
1060
1304
  }
1305
+ if (usage)
1306
+ requestOf.set(c, { method: body?.method, params: body?.params });
1061
1307
  const { id, method, params } = body;
1062
1308
  const ip = clientIp(c.req.header('x-forwarded-for'));
1063
1309
  if (method === 'initialize') {
@@ -1085,8 +1331,7 @@ export const buildServer = (opts) => {
1085
1331
  }, 429);
1086
1332
  }
1087
1333
  initializes.add(from);
1088
- const client = params?.clientInfo
1089
- ?.name;
1334
+ const client = params?.clientInfo?.name;
1090
1335
  const cards = cardSupport(params);
1091
1336
  const sessionId = sessions.issue({
1092
1337
  ...(ip ? { ip } : {}),
@@ -1435,6 +1680,19 @@ export const buildServer = (opts) => {
1435
1680
  }
1436
1681
  }
1437
1682
  const key = named ?? own;
1683
+ // Who and where, bound to the conversation before the tool runs: an account
1684
+ // step finished on our own page is counted against it (usage/recorder.ts).
1685
+ if (usage) {
1686
+ const facts = usageFacts(c, presented, key);
1687
+ if (facts) {
1688
+ usage.recorder.bindKey(key, facts);
1689
+ usageNotes.set(c, {
1690
+ key,
1691
+ facts,
1692
+ ...(name === brand.tools.addToTrip ? { tripBefore: trips.read(key).length } : {}),
1693
+ });
1694
+ }
1695
+ }
1438
1696
  // Its own conversation, by naming none or its own: this session is
1439
1697
  // the conversation's, not a card's.
1440
1698
  const ours = asked === undefined || named === own;
@@ -1574,8 +1832,9 @@ export const buildServer = (opts) => {
1574
1832
  const told = said(answer.text, {
1575
1833
  // The deployment's name, logo and tools beside it, as every answer
1576
1834
  // has them: this may be the first card the conversation draws.
1577
- ...commonFields(deps, key),
1578
- ...answer.structured,
1835
+ ...(words
1836
+ ? withoutAppOnly({ ...commonFields(deps, key), ...answer.structured })
1837
+ : { ...commonFields(deps, key), ...answer.structured }),
1579
1838
  });
1580
1839
  return ok(c, id, {
1581
1840
  content: [{ type: 'text', text: told.text }],
@@ -1710,7 +1969,7 @@ export const buildServer = (opts) => {
1710
1969
  const { shown: unnamed, who } = memberApart(shown);
1711
1970
  // And the city field's menu - see `CITIES_META_KEY`.
1712
1971
  const { allCities, ...drawn } = unnamed;
1713
- const told = said(answer.text, drawn);
1972
+ const told = said(answer.text, words ? withoutAppOnly(drawn) : drawn);
1714
1973
  // ⚠️ AND NO PICTURE SENT AS ITSELF, FOR ANY HOST (0.67.0, 总线程 11-12): the
1715
1974
  // card's CSP would have to name `data:` in `resourceDomains`, which hosts put
1716
1975
  // in script-src too - and ChatGPT's widgetCSP would change. One the backend
@@ -1753,7 +2012,7 @@ export const buildServer = (opts) => {
1753
2012
  }
1754
2013
  }
1755
2014
  return fail(c, id, -32601, `unknown method: ${method}`);
1756
- });
2015
+ }
1757
2016
  return {
1758
2017
  app,
1759
2018
  ready: made.ready,
@@ -1764,8 +2023,16 @@ export const buildServer = (opts) => {
1764
2023
  tripIds,
1765
2024
  sessions,
1766
2025
  ...(sources ? { sources } : {}),
1767
- flush: () => state.flush(),
1768
- drain: () => made.drain?.() ?? Promise.resolve(),
2026
+ ...(usage ? { usage } : {}),
2027
+ flush: () => {
2028
+ state.flush();
2029
+ void usage?.flush();
2030
+ },
2031
+ // The bookings in flight first, then the usage lines still queued.
2032
+ drain: async () => {
2033
+ await (made.drain?.() ?? Promise.resolve());
2034
+ await usage?.flush();
2035
+ },
1769
2036
  };
1770
2037
  };
1771
2038
  /**