@recapi/client 0.15.1 → 0.16.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.
- package/README.md +6 -1
- package/index.d.ts +12 -3
- package/package.json +1 -1
- package/src/client.js +1 -1
- package/src/history.js +69 -23
package/README.md
CHANGED
|
@@ -35,7 +35,7 @@ are, gzipped, and browsers cache them:
|
|
|
35
35
|
|
|
36
36
|
```html
|
|
37
37
|
<script type="module">
|
|
38
|
-
import { createClient } from 'https://api.example.com/sdk/0.
|
|
38
|
+
import { createClient } from 'https://api.example.com/sdk/0.16.0/index.js';
|
|
39
39
|
|
|
40
40
|
const recapi = createClient({ apiKey: 'rk_pub_…', baseUrl: 'https://api.example.com', consent: false });
|
|
41
41
|
// From your cookie banner, once the visitor agrees: recapi.setConsent(true).
|
|
@@ -1115,6 +1115,11 @@ minifier, to keep zero dev dependencies.
|
|
|
1115
1115
|
|
|
1116
1116
|
## Changes
|
|
1117
1117
|
|
|
1118
|
+
- 2026-11-22: version 0.16.0. Each history entry keeps the strongest level
|
|
1119
|
+
tracked on it since it started (a view, a cart, a purchase), sent as a
|
|
1120
|
+
fourth element of the entry when known, so a row can tell a product opened
|
|
1121
|
+
from one bought. A stored history gains a `maxLevel` list beside `top`; a
|
|
1122
|
+
history stored by an earlier version still loads.
|
|
1118
1123
|
- 2026-11-22: version 0.15.1. Shopify, WooCommerce and Magento: a product
|
|
1119
1124
|
page's second row prints "Often bought together" only when one of the
|
|
1120
1125
|
cards it shows was carted or bought with the product on screen (`why:
|
package/index.d.ts
CHANGED
|
@@ -22,6 +22,11 @@ export interface TopEntry {
|
|
|
22
22
|
s: number;
|
|
23
23
|
/** Unix ms of the last in-order interaction. */
|
|
24
24
|
t: number;
|
|
25
|
+
/**
|
|
26
|
+
* Strongest level applied since the entry started (1–10); 0 or absent when
|
|
27
|
+
* unknown (an entry stored or sent without it).
|
|
28
|
+
*/
|
|
29
|
+
m?: number;
|
|
25
30
|
}
|
|
26
31
|
|
|
27
32
|
export interface RecentEntry {
|
|
@@ -39,8 +44,11 @@ export interface HistoryState {
|
|
|
39
44
|
lastT: number | null;
|
|
40
45
|
}
|
|
41
46
|
|
|
42
|
-
/**
|
|
43
|
-
|
|
47
|
+
/**
|
|
48
|
+
* `[item_id, weight rounded to 4 decimals, age in seconds]`, plus the entry's
|
|
49
|
+
* strongest level (1–10) as a fourth element when it is known.
|
|
50
|
+
*/
|
|
51
|
+
export type WireEntry = [item: string, weight: number, ageSeconds: number, maxLevel?: number];
|
|
44
52
|
|
|
45
53
|
export interface WireHistory {
|
|
46
54
|
history: WireEntry[];
|
|
@@ -51,7 +59,8 @@ export interface WireHistory {
|
|
|
51
59
|
|
|
52
60
|
/** Server-side history snapshot (`history` field of a recommendation response). */
|
|
53
61
|
export interface ServerSnapshot {
|
|
54
|
-
|
|
62
|
+
/** An entry's optional third element is its strongest level (1–10). */
|
|
63
|
+
top: Array<[item: string, weight: number, maxLevel?: number]>;
|
|
55
64
|
recent: string[];
|
|
56
65
|
/** Absent from servers that predate rejections. */
|
|
57
66
|
rejected?: string[];
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -745,7 +745,7 @@ export function createClient(options) {
|
|
|
745
745
|
* The history exactly as the next recommendation request would send it;
|
|
746
746
|
* `rejected` goes into its `exclude`.
|
|
747
747
|
*
|
|
748
|
-
* @returns {{ history:
|
|
748
|
+
* @returns {{ history: import('./history.js').WireEntry[], recent: string[], rejected: string[] }}
|
|
749
749
|
*/
|
|
750
750
|
getHistory() {
|
|
751
751
|
start();
|
package/src/history.js
CHANGED
|
@@ -19,6 +19,8 @@
|
|
|
19
19
|
* @typedef {object} TopEntry
|
|
20
20
|
* @property {number} s Score valid at time `t`.
|
|
21
21
|
* @property {number} t Unix ms of the last in-order interaction.
|
|
22
|
+
* @property {number} [m] The strongest level applied since the entry started,
|
|
23
|
+
* 1–10; 0 (or absent) when unknown: an entry stored or sent without it.
|
|
22
24
|
*/
|
|
23
25
|
|
|
24
26
|
/**
|
|
@@ -40,12 +42,16 @@
|
|
|
40
42
|
*/
|
|
41
43
|
|
|
42
44
|
/**
|
|
43
|
-
*
|
|
45
|
+
* `[item, weight, ageSeconds]`, plus the entry's strongest level as a fourth
|
|
46
|
+
* element when it is known.
|
|
47
|
+
*
|
|
48
|
+
* @typedef {[item: string, weight: number, ageSeconds: number, maxLevel?: number]} WireEntry
|
|
44
49
|
*/
|
|
45
50
|
|
|
46
51
|
/**
|
|
47
52
|
* @typedef {object} ServerSnapshot
|
|
48
|
-
* @property {Array<[string, number]>} top
|
|
53
|
+
* @property {Array<[string, number] | [string, number, number]>} top An
|
|
54
|
+
* entry's optional third element is its strongest level.
|
|
49
55
|
* @property {string[]} recent
|
|
50
56
|
* @property {string[]} [rejected] Absent from servers older than rejections.
|
|
51
57
|
* @property {number} as_of Unix ms at which the `top` weights are valid.
|
|
@@ -103,6 +109,29 @@ export function isEmpty(state) {
|
|
|
103
109
|
return state.top.size === 0 && state.recent.length === 0;
|
|
104
110
|
}
|
|
105
111
|
|
|
112
|
+
/**
|
|
113
|
+
* The strongest level of two sides of one entry: unknown (0) wins, since the
|
|
114
|
+
* unknown side may hold a purchase.
|
|
115
|
+
*
|
|
116
|
+
* @param {number | undefined} a
|
|
117
|
+
* @param {number | undefined} b
|
|
118
|
+
* @returns {number}
|
|
119
|
+
*/
|
|
120
|
+
function strongest(a, b) {
|
|
121
|
+
return a && b ? Math.max(a, b) : 0;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* A strongest level read from storage or a snapshot: an integer 1–10, else 0
|
|
126
|
+
* (unknown).
|
|
127
|
+
*
|
|
128
|
+
* @param {unknown} value
|
|
129
|
+
* @returns {number}
|
|
130
|
+
*/
|
|
131
|
+
function levelOf(value) {
|
|
132
|
+
return Number.isInteger(value) && value >= 1 && value <= 10 ? /** @type {number} */ (value) : 0;
|
|
133
|
+
}
|
|
134
|
+
|
|
106
135
|
function decay(config, dtMs) {
|
|
107
136
|
return Math.exp((-Math.LN2 / config.halfLifeSeconds) * (dtMs / 1000));
|
|
108
137
|
}
|
|
@@ -127,7 +156,7 @@ export function weightAt(entry, T, config) {
|
|
|
127
156
|
* @param {HistoryState} state
|
|
128
157
|
* @param {number} T Unix ms.
|
|
129
158
|
* @param {HistoryConfig} config
|
|
130
|
-
* @returns {Array<{ item: string, weight: number, t: number }>}
|
|
159
|
+
* @returns {Array<{ item: string, weight: number, t: number, m: number }>}
|
|
131
160
|
*/
|
|
132
161
|
export function sortedEntries(state, T, config) {
|
|
133
162
|
return rank(state.top, T, config);
|
|
@@ -141,7 +170,7 @@ export function sortedEntries(state, T, config) {
|
|
|
141
170
|
function rank(top, T, config) {
|
|
142
171
|
const entries = [];
|
|
143
172
|
for (const [item, entry] of top) {
|
|
144
|
-
entries.push({ item, weight: weightAt(entry, T, config), t: entry.t });
|
|
173
|
+
entries.push({ item, weight: weightAt(entry, T, config), t: entry.t, m: entry.m || 0 });
|
|
145
174
|
}
|
|
146
175
|
return entries.sort((a, b) => b.weight - a.weight || compareIds(a.item, b.item));
|
|
147
176
|
}
|
|
@@ -234,11 +263,11 @@ export function applyEvent(state, { item, level, t }, config) {
|
|
|
234
263
|
const top = new Map(state.top);
|
|
235
264
|
const current = top.get(item);
|
|
236
265
|
if (!current) {
|
|
237
|
-
top.set(item, { s: level, t });
|
|
266
|
+
top.set(item, { s: level, t, m: level });
|
|
238
267
|
} else if (t >= current.t) {
|
|
239
|
-
top.set(item, { s: current.s * decay(config, t - current.t) + level, t });
|
|
268
|
+
top.set(item, { s: current.s * decay(config, t - current.t) + level, t, m: strongest(current.m, level) });
|
|
240
269
|
} else {
|
|
241
|
-
top.set(item, { s: current.s + level * decay(config, current.t - t), t: current.t });
|
|
270
|
+
top.set(item, { s: current.s + level * decay(config, current.t - t), t: current.t, m: strongest(current.m, level) });
|
|
242
271
|
}
|
|
243
272
|
|
|
244
273
|
const inOrder = state.lastT === null || t >= state.lastT;
|
|
@@ -260,8 +289,9 @@ export function applyEvent(state, { item, level, t }, config) {
|
|
|
260
289
|
/**
|
|
261
290
|
* Wire format of a recommendation request: weights decayed to `T`, at most
|
|
262
291
|
* what the API accepts, and rounded to 4 decimals, integer age in seconds (clamped at 0 so a skewed
|
|
263
|
-
* clock never yields negative ages)
|
|
264
|
-
*
|
|
292
|
+
* clock never yields negative ages), and a fourth element, the entry's
|
|
293
|
+
* strongest level, only when it is known. `rejected` (oldest mark first) is
|
|
294
|
+
* sent in the request's `exclude`.
|
|
265
295
|
*
|
|
266
296
|
* @param {HistoryState} state
|
|
267
297
|
* @param {number} T Unix ms of the request.
|
|
@@ -271,19 +301,24 @@ export function applyEvent(state, { item, level, t }, config) {
|
|
|
271
301
|
export function toWire(state, T, config) {
|
|
272
302
|
const history = sortedEntries(state, T, config)
|
|
273
303
|
.slice(0, config.topK)
|
|
274
|
-
.map(({ item, weight, t }) =>
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
304
|
+
.map(({ item, weight, t, m }) => {
|
|
305
|
+
const entry = /** @type {WireEntry} */ ([
|
|
306
|
+
item,
|
|
307
|
+
Math.round(Math.min(weight, MAX_REQUEST_WEIGHT) * 1e4) / 1e4,
|
|
308
|
+
Math.max(0, Math.floor((T - t) / 1000))
|
|
309
|
+
]);
|
|
310
|
+
if (m > 0) entry.push(m);
|
|
311
|
+
return entry;
|
|
312
|
+
});
|
|
279
313
|
const recent = state.recent.slice(-config.recentSize).map((entry) => entry.item);
|
|
280
314
|
return { history, recent, rejected: rejectedOrder(state.rejected) };
|
|
281
315
|
}
|
|
282
316
|
|
|
283
317
|
/**
|
|
284
318
|
* Builds a state from the server's fallback snapshot. Each top entry becomes
|
|
285
|
-
* `{ s: weight, t: as_of }` as the spec requires,
|
|
286
|
-
*
|
|
319
|
+
* `{ s: weight, t: as_of }` as the spec requires, with its strongest level `m`
|
|
320
|
+
* (0, unknown, when its third element is absent or not an integer 1–10), each
|
|
321
|
+
* rejected item a mark at `as_of`. `lastT` is left unknown
|
|
287
322
|
* rather than set to `as_of`: `as_of` is the server clock, and a device clock
|
|
288
323
|
* running behind it would otherwise treat every new local event as out of
|
|
289
324
|
* order and freeze the recent list.
|
|
@@ -301,7 +336,7 @@ export function fromSnapshot(snapshot, config) {
|
|
|
301
336
|
const top = new Map();
|
|
302
337
|
for (const entry of Array.isArray(snapshot.top) ? snapshot.top : []) {
|
|
303
338
|
if (Array.isArray(entry) && isId(entry[0]) && isWeight(entry[1])) {
|
|
304
|
-
top.set(entry[0], { s: entry[1], t: asOf });
|
|
339
|
+
top.set(entry[0], { s: entry[1], t: asOf, m: levelOf(entry[2]) });
|
|
305
340
|
}
|
|
306
341
|
}
|
|
307
342
|
const seen = new Set();
|
|
@@ -332,7 +367,9 @@ export function fromSnapshot(snapshot, config) {
|
|
|
332
367
|
* Top: per item, the sum of both decayed scores. The sum is expressed at the
|
|
333
368
|
* later of the two `t`s, which keeps the true last-interaction time (and so
|
|
334
369
|
* the wire `age_seconds`) instead of resetting it to the merge time; the
|
|
335
|
-
* weight at any later time equals the sum of the two weights.
|
|
370
|
+
* weight at any later time equals the sum of the two weights. An entry on one
|
|
371
|
+
* side keeps its strongest level; on both, the stronger one, unless either
|
|
372
|
+
* side's is unknown (then unknown).
|
|
336
373
|
*
|
|
337
374
|
* Recent: union of both lists ordered by interaction time (oldest first); an
|
|
338
375
|
* item in both lists takes its most recent time. Equal times keep `a`'s order
|
|
@@ -357,7 +394,7 @@ export function merge(a, b, T, config) {
|
|
|
357
394
|
continue;
|
|
358
395
|
}
|
|
359
396
|
const t = Math.max(entry.t, other.t);
|
|
360
|
-
top.set(item, { s: weightAt(entry, t, config) + weightAt(other, t, config), t });
|
|
397
|
+
top.set(item, { s: weightAt(entry, t, config) + weightAt(other, t, config), t, m: strongest(entry.m, other.m) });
|
|
361
398
|
}
|
|
362
399
|
|
|
363
400
|
const rejected = new Map(a.rejected);
|
|
@@ -411,17 +448,22 @@ export function mergeSnapshot(local, snapshot, T, config) {
|
|
|
411
448
|
/**
|
|
412
449
|
* Compact JSON-safe form for storage. Kept separate from the wire format
|
|
413
450
|
* because storage needs raw `{s, t}` (not rounded weights) to stay exact.
|
|
451
|
+
* `maxLevel` holds each top entry's strongest level, in `top`'s order (0
|
|
452
|
+
* unknown): a key of its own, not a fourth element of a top entry, so an SDK
|
|
453
|
+
* from before it (which refuses a top entry of another length) still reads
|
|
454
|
+
* the state.
|
|
414
455
|
*
|
|
415
456
|
* @param {HistoryState} state
|
|
416
457
|
* @returns {{ top: Array<[string, number, number]>, recent: Array<[string, number]>,
|
|
417
|
-
* rejected: Array<[string, number]>, lastT: number|null }}
|
|
458
|
+
* rejected: Array<[string, number]>, lastT: number|null, maxLevel: number[] }}
|
|
418
459
|
*/
|
|
419
460
|
export function encodeState(state) {
|
|
420
461
|
return {
|
|
421
462
|
top: [...state.top].map(([item, { s, t }]) => [item, s, t]),
|
|
422
463
|
recent: state.recent.map(({ item, t }) => [item, t]),
|
|
423
464
|
rejected: [...state.rejected],
|
|
424
|
-
lastT: state.lastT
|
|
465
|
+
lastT: state.lastT,
|
|
466
|
+
maxLevel: [...state.top.values()].map(({ m }) => m || 0)
|
|
425
467
|
};
|
|
426
468
|
}
|
|
427
469
|
|
|
@@ -429,6 +471,9 @@ export function encodeState(state) {
|
|
|
429
471
|
* Inverse of {@link encodeState}. Returns `null` on any malformed field so
|
|
430
472
|
* the caller discards corrupt storage instead of sending garbage. A state
|
|
431
473
|
* stored before rejections existed has no `rejected` and decodes as none.
|
|
474
|
+
* `maxLevel` is read leniently and never fails the state: absent, not a list
|
|
475
|
+
* or not as long as `top`, every level is unknown (0); an element that is not
|
|
476
|
+
* an integer 1–10 is unknown.
|
|
432
477
|
*
|
|
433
478
|
* An entry whose id the API refuses is dropped and the rest kept: earlier
|
|
434
479
|
* SDKs counted characters, not bytes, and stored such ids, which made every
|
|
@@ -442,12 +487,13 @@ export function decodeState(raw) {
|
|
|
442
487
|
if (!raw || !Array.isArray(raw.top) || !Array.isArray(raw.recent)) return null;
|
|
443
488
|
if (raw.lastT !== null && !Number.isFinite(raw.lastT)) return null;
|
|
444
489
|
|
|
490
|
+
const levels = Array.isArray(raw.maxLevel) && raw.maxLevel.length === raw.top.length ? raw.maxLevel : null;
|
|
445
491
|
const top = new Map();
|
|
446
|
-
for (const entry of raw.top) {
|
|
492
|
+
for (const [i, entry] of raw.top.entries()) {
|
|
447
493
|
if (!Array.isArray(entry) || typeof entry[0] !== 'string' || !isWeight(entry[1]) || !Number.isFinite(entry[2])) {
|
|
448
494
|
return null;
|
|
449
495
|
}
|
|
450
|
-
if (isId(entry[0])) top.set(entry[0], { s: entry[1], t: entry[2] });
|
|
496
|
+
if (isId(entry[0])) top.set(entry[0], { s: entry[1], t: entry[2], m: levels ? levelOf(levels[i]) : 0 });
|
|
451
497
|
}
|
|
452
498
|
const recent = [];
|
|
453
499
|
for (const entry of raw.recent) {
|