@adriangalilea/utils 0.11.0 → 0.13.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.
@@ -1,142 +0,0 @@
1
- /**
2
- * Per-user rolling message history for GramIO bots — opt-in, with
3
- * retention, **sharded by thread**.
4
- *
5
- * Each user has one session record. Inside that record, `history` keeps
6
- * a separate ring buffer per thread:
7
- *
8
- * storage[userId].history.shards = {
9
- * 'general': [ ...entries from no-thread messages ],
10
- * '12345': [ ...entries from message_thread_id=12345 ],
11
- * '98765': [ ...entries from message_thread_id=98765 ],
12
- * }
13
- *
14
- * This makes the plugin compatible with both forum-supergroup topics
15
- * and BotFather's Threaded Mode for private chats (which is the whole
16
- * point of those features: parallel conversations with separate
17
- * context). `ctx.history` returns ONLY the slice for the current
18
- * thread; `ctx.allHistory` returns the full sharded map when you need
19
- * a cross-thread view (e.g. for `/export`).
20
- *
21
- * Thread key: `String(ctx.threadId)`, or `'general'` when no thread.
22
- *
23
- * ## What this plugin owns
24
- *
25
- * - Appends each incoming user message to the shard for its thread
26
- * - Prunes per-shard by `retentionDays` and `maxMessages`
27
- * - Exposes `ctx.history` (current thread's pruned slice) and
28
- * `ctx.allHistory` (full map) to handlers
29
- *
30
- * ## GDPR caveat — retention IS personal data
31
- *
32
- * Unlike `bot/language` (preference data, covered by Telegram's standard
33
- * privacy policy), retaining user **message content** is the one thing
34
- * the [standard policy](https://telegram.org/privacy-tpa) does NOT cover
35
- * by default. If you extend `messageHistory`, you should:
36
- *
37
- * 1. Set a custom `privacy` URL on your `botMenu` describing what you
38
- * retain and for how long.
39
- * 2. Pass `personalData: { storage }` to your `botMenu` so 🗑 Forget
40
- * and 📥 Export buttons appear — letting users see and delete
41
- * the data you keep about them.
42
- *
43
- * These are not enforced by this plugin (would couple it to menu); they
44
- * are documented as the bot author's legal responsibility.
45
- *
46
- * Peer deps: `gramio`, `@gramio/session`.
47
- *
48
- * @example
49
- * import { Bot } from 'gramio'
50
- * import { session } from '@gramio/session'
51
- * import { redisStorage } from '@gramio/storage-redis'
52
- * import { messageHistory } from '@adriangalilea/utils/bot/message-history'
53
- *
54
- * const userSession = session({ storage: redisStorage(), key: 'session', initial: () => ({}) })
55
- *
56
- * const history = messageHistory({
57
- * session: userSession,
58
- * maxMessages: 100, // cap per-thread
59
- * retentionDays: 7,
60
- * })
61
- *
62
- * const bot = new Bot(process.env.BOT_TOKEN!)
63
- * .extend(userSession)
64
- * .extend(history.plugin)
65
- * .command('replay', (ctx) => {
66
- * const last = ctx.history.slice(-3).map((e) => e.text).join('\n---\n')
67
- * return ctx.send(last || '(no history in this thread)')
68
- * })
69
- */
70
- import { Plugin } from 'gramio';
71
- const GENERAL_THREAD = 'general';
72
- const threadKey = (ctx) => {
73
- // Message events: ctx.threadId. Callback events: ctx.message.threadId.
74
- const tid = ctx.threadId ?? ctx.message?.threadId;
75
- return tid !== undefined ? String(tid) : GENERAL_THREAD;
76
- };
77
- // ─── helpers ───────────────────────────────────────────────────────
78
- const prune = (items, maxMessages, retentionDays) => {
79
- const cutoffSec = Math.floor(Date.now() / 1000) - retentionDays * 86400;
80
- const fresh = items.filter((e) => e.date >= cutoffSec);
81
- return fresh.slice(-maxMessages);
82
- };
83
- const pruneAll = (shards, maxMessages, retentionDays) => {
84
- const out = {};
85
- for (const k of Object.keys(shards)) {
86
- const pruned = prune(shards[k], maxMessages, retentionDays);
87
- if (pruned.length > 0)
88
- out[k] = pruned;
89
- }
90
- return out;
91
- };
92
- // ─── feature factory ───────────────────────────────────────────────
93
- export const messageHistory = (opts) => {
94
- if (opts.maxMessages <= 0)
95
- throw new Error('messageHistory: maxMessages must be > 0');
96
- if (opts.retentionDays <= 0)
97
- throw new Error('messageHistory: retentionDays must be > 0');
98
- const plugin = buildHistoryPlugin({
99
- sessionPlugin: opts.session,
100
- maxMessages: opts.maxMessages,
101
- retentionDays: opts.retentionDays,
102
- });
103
- return { plugin };
104
- };
105
- // ─── plugin builder ────────────────────────────────────────────────
106
- const buildHistoryPlugin = (args) => {
107
- const { sessionPlugin, maxMessages, retentionDays } = args;
108
- return new Plugin('@adriangalilea/utils/bot/message-history')
109
- .extend(sessionPlugin)
110
- // Record incoming messages with text into their thread's shard.
111
- // Service messages, edits, and callback queries don't append —
112
- // only direct user input.
113
- .on('message', async (ctx, next) => {
114
- if (ctx.text !== undefined) {
115
- const entry = {
116
- messageId: ctx.id,
117
- date: ctx.payload.date,
118
- text: ctx.text,
119
- };
120
- const key = threadKey(ctx);
121
- const shards = { ...(ctx.session.history?.shards ?? {}) };
122
- const cur = shards[key] ?? [];
123
- shards[key] = [
124
- ...prune(cur, maxMessages, retentionDays),
125
- entry,
126
- ].slice(-maxMessages);
127
- ctx.session.history = { shards };
128
- }
129
- return next();
130
- })
131
- // Read-only views for handlers downstream.
132
- .derive(['message', 'callback_query'], (ctx) => {
133
- const shards = ctx.session.history?.shards ?? {};
134
- const allPruned = pruneAll(shards, maxMessages, retentionDays);
135
- const key = threadKey(ctx);
136
- return {
137
- history: (allPruned[key] ?? []),
138
- allHistory: allPruned,
139
- };
140
- });
141
- };
142
- //# sourceMappingURL=message-history.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"message-history.js","sourceRoot":"","sources":["../../src/bot/message-history.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoEG;AACH,OAAO,EAA0B,MAAM,EAAE,MAAM,QAAQ,CAAA;AAuBvD,MAAM,cAAc,GAAG,SAAS,CAAA;AAEhC,MAAM,SAAS,GAAG,CAAC,GAGlB,EAAU,EAAE;IACX,uEAAuE;IACvE,MAAM,GAAG,GAAG,GAAG,CAAC,QAAQ,IAAI,GAAG,CAAC,OAAO,EAAE,QAAQ,CAAA;IACjD,OAAO,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,cAAc,CAAA;AACz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