@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,86 +0,0 @@
1
- /**
2
- * LLM streaming output for GramIO bots.
3
- *
4
- * Sends a placeholder, then debounces `editMessageText` calls as the
5
- * LLM produces chunks. Markdown is parsed locally with
6
- * `markdownToFormattable` — invalid markup degrades to plain text
7
- * instead of failing (Telegram's `parse_mode` would reject the whole
8
- * message). Splits at 4000 chars by promoting the next chunk to a
9
- * fresh message at a paragraph/line/word boundary.
10
- *
11
- * Peer deps: `gramio`, `@gramio/format`, `marked`.
12
- *
13
- * @example
14
- * import { Bot } from 'gramio'
15
- * import { llmStream } from '@adriangalilea/utils/bot/llm-stream'
16
- * import Anthropic from '@anthropic-ai/sdk'
17
- *
18
- * const claude = new Anthropic()
19
- * const bot = new Bot(process.env.BOT_TOKEN!)
20
- * .extend(llmStream())
21
- * .on('message', async (ctx) => {
22
- * const stream = ctx.startStream()
23
- * const sse = await claude.messages.stream({
24
- * model: 'claude-opus-4-5',
25
- * max_tokens: 1024,
26
- * messages: [{ role: 'user', content: ctx.text ?? '' }],
27
- * })
28
- * for await (const chunk of sse) {
29
- * if (chunk.type === 'content_block_delta' && chunk.delta.type === 'text_delta') {
30
- * await stream.append(chunk.delta.text)
31
- * }
32
- * }
33
- * await stream.end()
34
- * })
35
- *
36
- * bot.start()
37
- */
38
- import { Plugin } from 'gramio';
39
- export type StreamOptions = {
40
- /** Debounce window between edits, in ms. Default 800. */
41
- debounceMs?: number;
42
- /** Initial placeholder shown until the first chunk arrives. Default "…". */
43
- placeholder?: string;
44
- /** Parse buffer as markdown. Default true. Set false for plain text streaming. */
45
- markdown?: boolean;
46
- /** Called on edit/send errors after internal recovery (rate limits, etc.). */
47
- onError?: (err: unknown) => void;
48
- };
49
- export declare class MarkdownStreamer {
50
- private buffer;
51
- private currentMessageId?;
52
- private firstSendPromise?;
53
- private debounceTimer?;
54
- private inFlight;
55
- private dirty;
56
- private ended;
57
- private chatId;
58
- private threadId?;
59
- private bot;
60
- private opts;
61
- constructor(ctx: {
62
- chat: {
63
- id: number;
64
- };
65
- threadId?: number;
66
- bot: MarkdownStreamer['bot'];
67
- }, opts: StreamOptions);
68
- /** Append a chunk. Schedules a debounced edit. */
69
- append(text: string): Promise<void>;
70
- /** Flush any pending edit and close the stream. Idempotent. */
71
- end(): Promise<void>;
72
- private scheduleFlush;
73
- private flushNow;
74
- }
75
- /**
76
- * GramIO plugin. Adds `ctx.startStream(opts?)` on every message context.
77
- *
78
- * Defaults set here apply to every stream; per-call options in
79
- * `ctx.startStream({...})` override them.
80
- */
81
- export declare const llmStream: (defaults?: StreamOptions) => Plugin<{}, import("gramio").DeriveDefinitions & {
82
- message: {
83
- startStream: (opts?: StreamOptions) => MarkdownStreamer;
84
- };
85
- }, {}>;
86
- //# sourceMappingURL=llm-stream.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"llm-stream.d.ts","sourceRoot":"","sources":["../../src/bot/llm-stream.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAM/B,MAAM,MAAM,aAAa,GAAG;IAC1B,yDAAyD;IACzD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,4EAA4E;IAC5E,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,kFAAkF;IAClF,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,8EAA8E;IAC9E,OAAO,CAAC,EAAE,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,CAAA;CACjC,CAAA;AAED,qBAAa,gBAAgB;IAC3B,OAAO,CAAC,MAAM,CAAK;IACnB,OAAO,CAAC,gBAAgB,CAAC,CAAQ;IACjC,OAAO,CAAC,gBAAgB,CAAC,CAAe;IACxC,OAAO,CAAC,aAAa,CAAC,CAA+B;IACrD,OAAO,CAAC,QAAQ,CAAQ;IACxB,OAAO,CAAC,KAAK,CAAQ;IACrB,OAAO,CAAC,KAAK,CAAQ;IAErB,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,QAAQ,CAAC,CAAQ;IAIzB,OAAO,CAAC,GAAG,CAaV;IACD,OAAO,CAAC,IAAI,CAAyB;gBAGnC,GAAG,EAAE;QACH,IAAI,EAAE;YAAE,EAAE,EAAE,MAAM,CAAA;SAAE,CAAA;QACpB,QAAQ,CAAC,EAAE,MAAM,CAAA;QACjB,GAAG,EAAE,gBAAgB,CAAC,KAAK,CAAC,CAAA;KAC7B,EACD,IAAI,EAAE,aAAa;IAgBrB,kDAAkD;IAC5C,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IA+CzC,+DAA+D;IACzD,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;IAW1B,OAAO,CAAC,aAAa;YAaP,QAAQ;CAmCvB;AAED;;;;;GAKG;AACH,eAAO,MAAM,SAAS,GAAI,WAAU,aAAkB;;6BAI9B,aAAa;;MAEhC,CAAA"}
@@ -1,207 +0,0 @@
1
- /**
2
- * LLM streaming output for GramIO bots.
3
- *
4
- * Sends a placeholder, then debounces `editMessageText` calls as the
5
- * LLM produces chunks. Markdown is parsed locally with
6
- * `markdownToFormattable` — invalid markup degrades to plain text
7
- * instead of failing (Telegram's `parse_mode` would reject the whole
8
- * message). Splits at 4000 chars by promoting the next chunk to a
9
- * fresh message at a paragraph/line/word boundary.
10
- *
11
- * Peer deps: `gramio`, `@gramio/format`, `marked`.
12
- *
13
- * @example
14
- * import { Bot } from 'gramio'
15
- * import { llmStream } from '@adriangalilea/utils/bot/llm-stream'
16
- * import Anthropic from '@anthropic-ai/sdk'
17
- *
18
- * const claude = new Anthropic()
19
- * const bot = new Bot(process.env.BOT_TOKEN!)
20
- * .extend(llmStream())
21
- * .on('message', async (ctx) => {
22
- * const stream = ctx.startStream()
23
- * const sse = await claude.messages.stream({
24
- * model: 'claude-opus-4-5',
25
- * max_tokens: 1024,
26
- * messages: [{ role: 'user', content: ctx.text ?? '' }],
27
- * })
28
- * for await (const chunk of sse) {
29
- * if (chunk.type === 'content_block_delta' && chunk.delta.type === 'text_delta') {
30
- * await stream.append(chunk.delta.text)
31
- * }
32
- * }
33
- * await stream.end()
34
- * })
35
- *
36
- * bot.start()
37
- */
38
- import { Plugin } from 'gramio';
39
- import { markdownToFormattable } from '@gramio/format/markdown';
40
- const MAX_LEN = 4000; // Telegram caps at 4096; leave headroom for entity offsets
41
- const DEFAULT_DEBOUNCE_MS = 800;
42
- export class MarkdownStreamer {
43
- buffer = '';
44
- currentMessageId;
45
- firstSendPromise;
46
- debounceTimer;
47
- inFlight = false;
48
- dirty = false;
49
- ended = false;
50
- chatId;
51
- threadId;
52
- // Match gramio's `bot.api` shape structurally. `text` accepts string or any
53
- // `Formattable` (from `@gramio/format`) — both stringify safely. We don't
54
- // import gramio's full Bot type to keep the streamer testable in isolation.
55
- bot;
56
- opts;
57
- constructor(ctx, opts) {
58
- this.chatId = ctx.chat.id;
59
- // Captured so the streamed reply stays in the same thread.
60
- // We call bot.api.sendMessage directly (no ctx.send), so the
61
- // SendMixin's auto-thread doesn't help us here.
62
- this.threadId = ctx.threadId;
63
- this.bot = ctx.bot;
64
- this.opts = {
65
- debounceMs: opts.debounceMs ?? DEFAULT_DEBOUNCE_MS,
66
- placeholder: opts.placeholder ?? '…',
67
- markdown: opts.markdown ?? true,
68
- onError: opts.onError ?? ((e) => console.error('[llm-stream]', e)),
69
- };
70
- }
71
- /** Append a chunk. Schedules a debounced edit. */
72
- async append(text) {
73
- if (this.ended)
74
- throw new Error('stream already ended');
75
- if (!text)
76
- return;
77
- // first chunk: send the placeholder so we have a message_id to edit.
78
- // Serialized via firstSendPromise so concurrent appends don't double-send.
79
- if (this.currentMessageId === undefined && !this.firstSendPromise) {
80
- this.firstSendPromise = (async () => {
81
- const sent = await this.bot.api.sendMessage({
82
- chat_id: this.chatId,
83
- ...(this.threadId !== undefined && { message_thread_id: this.threadId }),
84
- text: this.opts.placeholder,
85
- });
86
- this.currentMessageId = sent.message_id;
87
- })();
88
- }
89
- if (this.firstSendPromise)
90
- await this.firstSendPromise;
91
- // overflow: freeze current message at last good split, start a new one.
92
- const next = this.buffer + text;
93
- if (next.length > MAX_LEN) {
94
- const splitAt = findSplit(next, MAX_LEN);
95
- const head = next.slice(0, splitAt);
96
- const tail = next.slice(splitAt).trimStart();
97
- this.buffer = head;
98
- this.dirty = true;
99
- await this.flushNow(); // commits head into current message
100
- this.buffer = '';
101
- this.currentMessageId = undefined;
102
- this.firstSendPromise = undefined;
103
- this.dirty = false;
104
- if (this.debounceTimer) {
105
- clearTimeout(this.debounceTimer);
106
- this.debounceTimer = undefined;
107
- }
108
- if (tail)
109
- await this.append(tail);
110
- return;
111
- }
112
- this.buffer = next;
113
- this.dirty = true;
114
- this.scheduleFlush();
115
- }
116
- /** Flush any pending edit and close the stream. Idempotent. */
117
- async end() {
118
- if (this.ended)
119
- return;
120
- this.ended = true;
121
- if (this.debounceTimer) {
122
- clearTimeout(this.debounceTimer);
123
- this.debounceTimer = undefined;
124
- }
125
- while (this.inFlight)
126
- await sleep(50);
127
- if (this.dirty)
128
- await this.flushNow();
129
- }
130
- scheduleFlush() {
131
- if (this.debounceTimer)
132
- return;
133
- this.debounceTimer = setTimeout(async () => {
134
- this.debounceTimer = undefined;
135
- if (this.inFlight) {
136
- this.scheduleFlush();
137
- return;
138
- }
139
- await this.flushNow();
140
- if (this.dirty && !this.ended)
141
- this.scheduleFlush();
142
- }, this.opts.debounceMs);
143
- }
144
- async flushNow() {
145
- if (!this.dirty)
146
- return;
147
- if (this.currentMessageId === undefined) {
148
- // No active message yet — likely the first send is still in flight
149
- // or the recovery path cleared it. Caller's next append() will
150
- // re-create one.
151
- return;
152
- }
153
- this.inFlight = true;
154
- this.dirty = false;
155
- const snapshot = this.buffer;
156
- try {
157
- const payload = this.opts.markdown ? markdownToFormattable(snapshot) : snapshot;
158
- await this.bot.api.editMessageText({
159
- chat_id: this.chatId,
160
- message_id: this.currentMessageId,
161
- text: payload,
162
- });
163
- }
164
- catch (e) {
165
- const msg = String(e?.message ?? e);
166
- if (msg.includes('message is not modified')) {
167
- // identical content — fine
168
- }
169
- else if (msg.includes('message to edit not found')) {
170
- // message gone (deleted by user, etc.) — restart with a fresh send
171
- this.currentMessageId = undefined;
172
- this.firstSendPromise = undefined;
173
- this.dirty = true;
174
- }
175
- else {
176
- this.dirty = true;
177
- this.opts.onError(e);
178
- }
179
- }
180
- finally {
181
- this.inFlight = false;
182
- }
183
- }
184
- }
185
- /**
186
- * GramIO plugin. Adds `ctx.startStream(opts?)` on every message context.
187
- *
188
- * Defaults set here apply to every stream; per-call options in
189
- * `ctx.startStream({...})` override them.
190
- */
191
- export const llmStream = (defaults = {}) => new Plugin('@adriangalilea/utils/bot/llm-stream').derive('message', (ctx) => ({
192
- // gramio's `message` scope guarantees `ctx.chat`. `ctx.bot.api` is on
193
- // every Context. Structural compat → no cast needed.
194
- startStream: (opts = {}) => new MarkdownStreamer(ctx, { ...defaults, ...opts }),
195
- }));
196
- function findSplit(text, maxLen) {
197
- // Prefer paragraph break, then line, then space. Reject splits in the
198
- // first half — better to truncate at maxLen than to leave a stub.
199
- for (const sep of ['\n\n', '\n', ' ']) {
200
- const idx = text.lastIndexOf(sep, maxLen);
201
- if (idx > maxLen / 2)
202
- return idx;
203
- }
204
- return maxLen;
205
- }
206
- const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
207
- //# sourceMappingURL=llm-stream.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"llm-stream.js","sourceRoot":"","sources":["../../src/bot/llm-stream.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC/B,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,C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gBACjB,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAA;YACtB,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAA;QACvB,CAAC;IACH,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,CAAC,MAAM,SAAS,GAAG,CAAC,WAA0B,EAAE,EAAE,EAAE,CACxD,IAAI,MAAM,CAAC,qCAAqC,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;IAC5E,sEAAsE;IACtE,qDAAqD;IACrD,WAAW,EAAE,CAAC,OAAsB,EAAE,EAAE,EAAE,CACxC,IAAI,gBAAgB,CAAC,GAAG,EAAE,EAAE,GAAG,QAAQ,EAAE,GAAG,IAAI,EAAE,CAAC;CACtD,CAAC,CAAC,CAAA;AAEL,SAAS,SAAS,CAAC,IAAY,EAAE,MAAc;IAC7C,sEAAsE;IACtE,kEAAkE;IAClE,KAAK,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,CAAC,EAAE,CAAC;QACtC,MAAM,GAAG,GAAG,IAAI,CAAC,WAAW,CAAC,GAAG,EAAE,MAAM,CAAC,CAAA;QACzC,IAAI,GAAG,GAAG,MAAM,GAAG,CAAC;YAAE,OAAO,GAAG,CAAA;IAClC,CAAC;IACD,OAAO,MAAM,CAAA;AACf,CAAC;AAED,MAAM,KAAK,GAAG,CAAC,EAAU,EAAE,EAAE,CAAC,IAAI,OAAO,CAAO,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAA"}
@@ -1,279 +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 { type DeriveDefinitions, Plugin } from 'gramio';
71
- import { session } from '@gramio/session';
72
- export type HistoryEntry = {
73
- /** Telegram message id. */
74
- messageId: number;
75
- /** Unix seconds (Telegram's `message.date`). */
76
- date: number;
77
- /** Message text, or empty string if non-text. */
78
- text: string;
79
- };
80
- /**
81
- * Per-thread shards. Key is `String(ctx.threadId)` or `'general'`
82
- * when no thread. Each shard is an independently capped + pruned
83
- * ring buffer.
84
- */
85
- export type HistoryRecord = {
86
- shards: {
87
- [threadKey: string]: HistoryEntry[];
88
- };
89
- };
90
- /** Loose session shape — this plugin only touches the `history` field. */
91
- type SessionLike = {
92
- history?: HistoryRecord;
93
- };
94
- /** @internal — kept unexported so it doesn't clash with peers' refs. */
95
- type HistorySessionPluginRef = ReturnType<typeof session<SessionLike, 'session'>>;
96
- export type MessageHistoryOptions = {
97
- /**
98
- * Shared session plugin. This plugin extends it for the type flow;
99
- * gramio's runtime dedup ensures it only runs once per update.
100
- */
101
- session: HistorySessionPluginRef;
102
- /** Ring buffer cap **per thread**. Oldest entries dropped when exceeded. */
103
- maxMessages: number;
104
- /** Entries older than this (in days) are dropped on read. */
105
- retentionDays: number;
106
- };
107
- export type MessageHistoryFeature = {
108
- plugin: ReturnType<typeof buildHistoryPlugin>;
109
- };
110
- type HistoryDerives = {
111
- /** Pruned snapshot for the CURRENT thread only. */
112
- history: ReadonlyArray<HistoryEntry>;
113
- /**
114
- * Pruned snapshot of all threads, keyed by thread (`'general'` or
115
- * stringified threadId). Use for cross-thread views (e.g. /export).
116
- */
117
- allHistory: Readonly<{
118
- [threadKey: string]: ReadonlyArray<HistoryEntry>;
119
- }>;
120
- };
121
- export declare const messageHistory: (opts: MessageHistoryOptions) => MessageHistoryFeature;
122
- declare const buildHistoryPlugin: (args: {
123
- sessionPlugin: HistorySessionPluginRef;
124
- maxMessages: number;
125
- retentionDays: number;
126
- }) => Plugin<{}, DeriveDefinitions & {
127
- global: HistoryDerives;
128
- } & {
129
- message: {
130
- session: SessionLike & {
131
- $clear: () => Promise<void>;
132
- };
133
- };
134
- channel_post: {
135
- session: SessionLike & {
136
- $clear: () => Promise<void>;
137
- };
138
- };
139
- inline_query: {
140
- session: SessionLike & {
141
- $clear: () => Promise<void>;
142
- };
143
- };
144
- chosen_inline_result: {
145
- session: SessionLike & {
146
- $clear: () => Promise<void>;
147
- };
148
- };
149
- callback_query: {
150
- session: SessionLike & {
151
- $clear: () => Promise<void>;
152
- };
153
- };
154
- shipping_query: {
155
- session: SessionLike & {
156
- $clear: () => Promise<void>;
157
- };
158
- };
159
- pre_checkout_query: {
160
- session: SessionLike & {
161
- $clear: () => Promise<void>;
162
- };
163
- };
164
- poll_answer: {
165
- session: SessionLike & {
166
- $clear: () => Promise<void>;
167
- };
168
- };
169
- chat_join_request: {
170
- session: SessionLike & {
171
- $clear: () => Promise<void>;
172
- };
173
- };
174
- new_chat_members: {
175
- session: SessionLike & {
176
- $clear: () => Promise<void>;
177
- };
178
- };
179
- new_chat_title: {
180
- session: SessionLike & {
181
- $clear: () => Promise<void>;
182
- };
183
- };
184
- new_chat_photo: {
185
- session: SessionLike & {
186
- $clear: () => Promise<void>;
187
- };
188
- };
189
- delete_chat_photo: {
190
- session: SessionLike & {
191
- $clear: () => Promise<void>;
192
- };
193
- };
194
- group_chat_created: {
195
- session: SessionLike & {
196
- $clear: () => Promise<void>;
197
- };
198
- };
199
- message_auto_delete_timer_changed: {
200
- session: SessionLike & {
201
- $clear: () => Promise<void>;
202
- };
203
- };
204
- migrate_to_chat_id: {
205
- session: SessionLike & {
206
- $clear: () => Promise<void>;
207
- };
208
- };
209
- migrate_from_chat_id: {
210
- session: SessionLike & {
211
- $clear: () => Promise<void>;
212
- };
213
- };
214
- pinned_message: {
215
- session: SessionLike & {
216
- $clear: () => Promise<void>;
217
- };
218
- };
219
- invoice: {
220
- session: SessionLike & {
221
- $clear: () => Promise<void>;
222
- };
223
- };
224
- successful_payment: {
225
- session: SessionLike & {
226
- $clear: () => Promise<void>;
227
- };
228
- };
229
- chat_shared: {
230
- session: SessionLike & {
231
- $clear: () => Promise<void>;
232
- };
233
- };
234
- proximity_alert_triggered: {
235
- session: SessionLike & {
236
- $clear: () => Promise<void>;
237
- };
238
- };
239
- video_chat_scheduled: {
240
- session: SessionLike & {
241
- $clear: () => Promise<void>;
242
- };
243
- };
244
- video_chat_started: {
245
- session: SessionLike & {
246
- $clear: () => Promise<void>;
247
- };
248
- };
249
- video_chat_ended: {
250
- session: SessionLike & {
251
- $clear: () => Promise<void>;
252
- };
253
- };
254
- video_chat_participants_invited: {
255
- session: SessionLike & {
256
- $clear: () => Promise<void>;
257
- };
258
- };
259
- web_app_data: {
260
- session: SessionLike & {
261
- $clear: () => Promise<void>;
262
- };
263
- };
264
- location: {
265
- session: SessionLike & {
266
- $clear: () => Promise<void>;
267
- };
268
- };
269
- passport_data: {
270
- session: SessionLike & {
271
- $clear: () => Promise<void>;
272
- };
273
- };
274
- } & {
275
- message: HistoryDerives;
276
- callback_query: HistoryDerives;
277
- }, {}>;
278
- export {};
279
- //# sourceMappingURL=message-history.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"message-history.d.ts","sourceRoot":"","sources":["../../src/bot/message-history.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoEG;AACH,OAAO,EAAE,KAAK,iBAAiB,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AACvD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAA;AAIzC,MAAM,MAAM,YAAY,GAAG;IACzB,2BAA2B;IAC3B,SAAS,EAAE,MAAM,CAAA;IACjB,gDAAgD;IAChD,IAAI,EAAE,MAAM,CAAA;IACZ,iDAAiD;IACjD,IAAI,EAAE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