@recapi/client 0.6.7 → 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 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.7/index.js';
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,10 @@ 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.
879
883
  - 2026-11-07: version 0.6.7. One damaged shown entry in the browser's storage
880
884
  costs that item's click credit; it used to lose the visitor's id and history.
881
885
  An entry whose time isn't a whole number is now damaged too.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@recapi/client",
3
- "version": "0.6.7",
3
+ "version": "0.6.8",
4
4
  "description": "Browser and Node client for the recapi recommendation API: local decayed history, event batching, attribution.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
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
- const kept = rank(top, T, config).slice(0, config.topK);
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` and
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);