@llblab/pi-telegram 0.5.1 → 0.6.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.
package/lib/media.ts CHANGED
@@ -6,6 +6,7 @@
6
6
  import { basename, dirname } from "node:path";
7
7
 
8
8
  const TELEGRAM_MEDIA_GROUP_DEBOUNCE_MS = 1200;
9
+ const TELEGRAM_REPLY_CONTEXT_MAX_LENGTH = 1000;
9
10
 
10
11
  export interface TelegramPhotoSize {
11
12
  file_id: string;
@@ -27,6 +28,12 @@ export interface TelegramVoice {
27
28
  mime_type?: string;
28
29
  }
29
30
 
31
+ export interface TelegramReplyToMessage {
32
+ message_id?: number;
33
+ text?: string;
34
+ caption?: string;
35
+ }
36
+
30
37
  export interface TelegramSticker {
31
38
  file_id: string;
32
39
  }
@@ -35,6 +42,7 @@ export interface TelegramMediaMessage {
35
42
  message_id: number;
36
43
  text?: string;
37
44
  caption?: string;
45
+ reply_to_message?: TelegramReplyToMessage;
38
46
  media_group_id?: string;
39
47
  photo?: TelegramPhotoSize[];
40
48
  document?: TelegramDocument;
@@ -167,12 +175,58 @@ export function extractTelegramMessageText(
167
175
  return (message.text || message.caption || "").trim();
168
176
  }
169
177
 
178
+ function truncateTelegramReplyContextText(text: string): string {
179
+ if (text.length <= TELEGRAM_REPLY_CONTEXT_MAX_LENGTH) return text;
180
+ return `${text.slice(0, TELEGRAM_REPLY_CONTEXT_MAX_LENGTH).trimEnd()}…`;
181
+ }
182
+
183
+ export function extractTelegramReplyContextText(
184
+ message: TelegramMediaMessage,
185
+ ): string {
186
+ const quoted = (
187
+ message.reply_to_message?.text ||
188
+ message.reply_to_message?.caption ||
189
+ ""
190
+ ).trim();
191
+ return quoted ? truncateTelegramReplyContextText(quoted) : "";
192
+ }
193
+
194
+ export function appendTelegramReplyContext(
195
+ text: string,
196
+ replyContext: string,
197
+ ): string {
198
+ if (!replyContext) return text;
199
+ const replyBlock = `[reply] ${replyContext}`;
200
+ return text ? `${text}\n\n${replyBlock}` : `_\n\n${replyBlock}`;
201
+ }
202
+
203
+ export function extractTelegramMessagePromptText(
204
+ message: TelegramMediaMessage,
205
+ ): string {
206
+ return appendTelegramReplyContext(
207
+ extractTelegramMessageText(message),
208
+ extractTelegramReplyContextText(message),
209
+ );
210
+ }
211
+
170
212
  export function extractTelegramMessagesText(
171
213
  messages: TelegramMediaMessage[],
172
214
  ): string {
173
215
  return messages.map(extractTelegramMessageText).filter(Boolean).join("\n\n");
174
216
  }
175
217
 
218
+ export function extractTelegramMessagesPromptText(
219
+ messages: TelegramMediaMessage[],
220
+ ): string {
221
+ const text = extractTelegramMessagesText(messages);
222
+ const firstMessage = messages[0];
223
+ if (!firstMessage) return text;
224
+ return appendTelegramReplyContext(
225
+ text,
226
+ extractTelegramReplyContextText(firstMessage),
227
+ );
228
+ }
229
+
176
230
  export function extractFirstTelegramMessageText(
177
231
  messages: TelegramMediaMessage[],
178
232
  ): string {