henergyqueai 0.9.6 → 0.9.7

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/src/warm.js ADDED
@@ -0,0 +1,47 @@
1
+ // Le cache du fournisseur expire après environ cinq minutes : un message
2
+ // envoyé après une pause plus longue repaie tout l'historique au prix fort.
3
+ // Après chaque réponse, et tant que la session reste ouverte, la demande
4
+ // suivante est renvoyée toutes les trois minutes et demie, pendant trente
5
+ // minutes au plus. Rien ne s'affiche et rien n'est décompté.
6
+ import { warmCache } from "./client.js";
7
+
8
+ export function createWarmer(session, getMessages, options = {}) {
9
+ const interval = options.interval ?? 210000;
10
+ const window = options.window ?? 30 * 60000;
11
+ const send = options.send ?? warmCache;
12
+ let timer = null;
13
+ let until = 0;
14
+ const stop = () => {
15
+ clearTimeout(timer);
16
+ timer = null;
17
+ };
18
+ const schedule = () => {
19
+ timer = setTimeout(tick, interval);
20
+ timer.unref?.();
21
+ };
22
+ function tick() {
23
+ timer = null;
24
+ if (Date.now() > until || !session.key) return;
25
+ // Seul Aelyr coûte assez cher pour que cela compte.
26
+ if (session.modelId && !String(session.modelId).startsWith("aelyr")) return;
27
+ const messages = getMessages();
28
+ if (!messages?.length) return;
29
+ send({
30
+ base: session.base,
31
+ key: session.key,
32
+ messages,
33
+ tools: session.tools,
34
+ modelId: session.modelId,
35
+ surface: session.surface,
36
+ }).catch(() => {});
37
+ schedule();
38
+ }
39
+ return {
40
+ arm() {
41
+ stop();
42
+ until = Date.now() + window;
43
+ schedule();
44
+ },
45
+ stop,
46
+ };
47
+ }