@recapi/client 0.6.6 → 0.6.8
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 +8 -1
- package/package.json +1 -1
- package/src/client.js +6 -6
- package/src/history.js +14 -6
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.6.
|
|
38
|
+
import { createClient } from 'https://api.example.com/sdk/0.6.8/index.js';
|
|
39
39
|
|
|
40
40
|
const recapi = createClient({ apiKey: 'rk_pub_…', baseUrl: 'https://api.example.com' });
|
|
41
41
|
recapi.track('item-42', 'view'); // and the calls of the quick start below
|
|
@@ -876,6 +876,13 @@ minifier, to keep zero dev dependencies.
|
|
|
876
876
|
|
|
877
877
|
## Changes
|
|
878
878
|
|
|
879
|
+
- 2026-11-07: version 0.6.8. A saved time ahead of the clock reads as now and a
|
|
880
|
+
damaged weight is sent as the API's limit (1e6). A device whose clock ran months
|
|
881
|
+
ahead and was then corrected used to get a 400 on every list until the entry had
|
|
882
|
+
decayed back under that limit.
|
|
883
|
+
- 2026-11-07: version 0.6.7. One damaged shown entry in the browser's storage
|
|
884
|
+
costs that item's click credit; it used to lose the visitor's id and history.
|
|
885
|
+
An entry whose time isn't a whole number is now damaged too.
|
|
879
886
|
- 2026-11-07: version 0.6.6. With a public key, `recommend()` and the event
|
|
880
887
|
batches go out with `?key=` and `Content-Type: text/plain`, no `Authorization`,
|
|
881
888
|
so a browser skips the permission request before them: a visit's first list
|
package/package.json
CHANGED
package/src/client.js
CHANGED
|
@@ -785,13 +785,13 @@ function decodePersisted(raw) {
|
|
|
785
785
|
if (raw.userId !== null && typeof raw.userId !== 'string') return null;
|
|
786
786
|
const shown = [];
|
|
787
787
|
for (const entry of raw.shown) {
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
788
|
+
// A bad entry costs that item's click credit, not the visitor's id and
|
|
789
|
+
// history. Entries without a click time are also the format of SDKs before
|
|
790
|
+
// the click window, so their storage keeps working.
|
|
791
|
+
if (Array.isArray(entry) && entry.length >= 3 && entry.length <= 4 && isId(entry[0]) && typeof entry[1] === 'string' &&
|
|
792
|
+
Number.isSafeInteger(entry[2]) && (entry.length === 3 || Number.isSafeInteger(entry[3]))) {
|
|
793
|
+
shown.push(/** @type {[string, [string, number, number?]]} */ ([entry[0], entry.slice(1)]));
|
|
791
794
|
}
|
|
792
|
-
// Entries without a click time are also the format of SDKs before the
|
|
793
|
-
// click window, so their storage keeps working.
|
|
794
|
-
if (isId(entry[0])) shown.push(/** @type {[string, [string, number, number?]]} */ ([entry[0], entry.slice(1, 4)]));
|
|
795
795
|
}
|
|
796
796
|
// Like the history's entries, a user id earlier SDKs let through but the
|
|
797
797
|
// API refuses is dropped, so requests work again (anonymously).
|
package/src/history.js
CHANGED
|
@@ -64,6 +64,9 @@ export const DEFAULT_CONFIG = Object.freeze({
|
|
|
64
64
|
*/
|
|
65
65
|
export const REJECTED_SIZE = 200;
|
|
66
66
|
|
|
67
|
+
/** The API answers 400 above this, which fails the list: a damaged score is sent as this. */
|
|
68
|
+
const MAX_REQUEST_WEIGHT = 1e6;
|
|
69
|
+
|
|
67
70
|
/**
|
|
68
71
|
* Fills missing fields with spec defaults so callers can override a single
|
|
69
72
|
* parameter (e.g. a shorter half-life for news sites).
|
|
@@ -105,7 +108,8 @@ function decay(config, dtMs) {
|
|
|
105
108
|
}
|
|
106
109
|
|
|
107
110
|
/**
|
|
108
|
-
* Weight of an entry decayed to time `T`.
|
|
111
|
+
* Weight of an entry decayed to time `T`. A time ahead of `T` reads as `T`: a
|
|
112
|
+
* clock that ran ahead must not grow the weight past what the API takes.
|
|
109
113
|
*
|
|
110
114
|
* @param {TopEntry} entry
|
|
111
115
|
* @param {number} T Unix ms.
|
|
@@ -113,7 +117,7 @@ function decay(config, dtMs) {
|
|
|
113
117
|
* @returns {number}
|
|
114
118
|
*/
|
|
115
119
|
export function weightAt(entry, T, config) {
|
|
116
|
-
return entry.s * decay(config, T - entry.t);
|
|
120
|
+
return entry.s * decay(config, Math.max(0, T - entry.t));
|
|
117
121
|
}
|
|
118
122
|
|
|
119
123
|
/**
|
|
@@ -154,7 +158,11 @@ function compareIds(a, b) {
|
|
|
154
158
|
*/
|
|
155
159
|
function prune(top, T, config) {
|
|
156
160
|
if (top.size <= config.topK) return top;
|
|
157
|
-
|
|
161
|
+
// Read at the newest entry when an event arrives out of order: at the old
|
|
162
|
+
// event's time rank() would flatten the newer entries to their score.
|
|
163
|
+
let at = T;
|
|
164
|
+
for (const entry of top.values()) at = Math.max(at, entry.t);
|
|
165
|
+
const kept = rank(top, at, config).slice(0, config.topK);
|
|
158
166
|
return new Map(kept.map(({ item }) => [item, top.get(item)]));
|
|
159
167
|
}
|
|
160
168
|
|
|
@@ -250,8 +258,8 @@ export function applyEvent(state, { item, level, t }, config) {
|
|
|
250
258
|
}
|
|
251
259
|
|
|
252
260
|
/**
|
|
253
|
-
* Wire format of a recommendation request: weights decayed to `T
|
|
254
|
-
* rounded to 4 decimals, integer age in seconds (clamped at 0 so a skewed
|
|
261
|
+
* Wire format of a recommendation request: weights decayed to `T`, at most
|
|
262
|
+
* what the API accepts, and rounded to 4 decimals, integer age in seconds (clamped at 0 so a skewed
|
|
255
263
|
* clock never yields negative ages). `rejected` (oldest mark first) is sent
|
|
256
264
|
* in the request's `exclude`.
|
|
257
265
|
*
|
|
@@ -265,7 +273,7 @@ export function toWire(state, T, config) {
|
|
|
265
273
|
.slice(0, config.topK)
|
|
266
274
|
.map(({ item, weight, t }) => /** @type {WireEntry} */ ([
|
|
267
275
|
item,
|
|
268
|
-
Math.round(weight * 1e4) / 1e4,
|
|
276
|
+
Math.round(Math.min(weight, MAX_REQUEST_WEIGHT) * 1e4) / 1e4,
|
|
269
277
|
Math.max(0, Math.floor((T - t) / 1000))
|
|
270
278
|
]));
|
|
271
279
|
const recent = state.recent.slice(-config.recentSize).map((entry) => entry.item);
|