@llblab/pi-telegram 0.7.2 → 0.8.1

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/index.ts CHANGED
@@ -5,18 +5,19 @@
5
5
  */
6
6
 
7
7
  import * as Api from "./lib/api.ts";
8
- import * as AttachmentHandlers from "./lib/attachment-handlers.ts";
9
- import * as Attachments from "./lib/attachments.ts";
10
- import * as Commands from "./lib/commands.ts";
8
+ import * as OutboundAttachments from "./lib/outbound-attachments.ts";
11
9
  import * as CommandTemplates from "./lib/command-templates.ts";
10
+ import * as Commands from "./lib/commands.ts";
12
11
  import * as Config from "./lib/config.ts";
12
+ import * as InboundHandlers from "./lib/inbound-handlers.ts";
13
13
  import * as Keyboard from "./lib/keyboard.ts";
14
14
  import * as Lifecycle from "./lib/lifecycle.ts";
15
15
  import * as Locks from "./lib/locks.ts";
16
16
  import * as Media from "./lib/media.ts";
17
- import * as Menu from "./lib/menu.ts";
18
17
  import * as MenuQueue from "./lib/menu-queue.ts";
18
+ import * as Menu from "./lib/menu.ts";
19
19
  import * as Model from "./lib/model.ts";
20
+ import * as OutboundHandlers from "./lib/outbound-handlers.ts";
20
21
  import * as Pi from "./lib/pi.ts";
21
22
  import * as Polling from "./lib/polling.ts";
22
23
  import * as Preview from "./lib/preview.ts";
@@ -24,10 +25,9 @@ import * as PromptTemplates from "./lib/prompt-templates.ts";
24
25
  import * as Prompts from "./lib/prompts.ts";
25
26
  import * as Queue from "./lib/queue.ts";
26
27
  import * as Replies from "./lib/replies.ts";
27
- import * as Runtime from "./lib/runtime.ts";
28
28
  import * as Routing from "./lib/routing.ts";
29
+ import * as Runtime from "./lib/runtime.ts";
29
30
  import * as Setup from "./lib/setup.ts";
30
- import * as OutboundHandlers from "./lib/outbound-handlers.ts";
31
31
  import * as Status from "./lib/status.ts";
32
32
  import * as TextGroups from "./lib/text-groups.ts";
33
33
 
@@ -114,15 +114,13 @@ export default function (pi: Pi.ExtensionAPI) {
114
114
  queue.incrementNextPriorityReactionOrder,
115
115
  updateStatus,
116
116
  });
117
- const attachmentHandlerRuntime =
118
- AttachmentHandlers.createTelegramAttachmentHandlerRuntime<Pi.ExtensionContext>(
119
- {
120
- getHandlers: configStore.getAttachmentHandlers,
121
- execCommand: CommandTemplates.execCommandTemplate,
122
- getCwd: Pi.getExtensionContextCwd,
123
- recordRuntimeEvent,
124
- },
125
- );
117
+ const inboundHandlerRuntime =
118
+ InboundHandlers.createTelegramInboundHandlerRuntime<Pi.ExtensionContext>({
119
+ getHandlers: configStore.getInboundHandlers,
120
+ execCommand: CommandTemplates.execCommandTemplate,
121
+ getCwd: Pi.getExtensionContextCwd,
122
+ recordRuntimeEvent,
123
+ });
126
124
 
127
125
  // --- Telegram API ---
128
126
 
@@ -157,19 +155,23 @@ export default function (pi: Pi.ExtensionAPI) {
157
155
 
158
156
  // --- Reply Runtime Wiring ---
159
157
 
160
- const {
161
- replyTransport,
162
- sendTextReply,
163
- sendMarkdownReply,
164
- editInteractiveMessage,
165
- sendInteractiveMessage,
166
- } =
158
+ const replyRuntime =
167
159
  Replies.createTelegramRenderedMessageDeliveryRuntime<Keyboard.TelegramInlineKeyboardMarkup>(
168
160
  {
169
161
  sendMessage,
170
162
  editMessage: editTelegramMessageText,
171
163
  },
172
164
  );
165
+ const { replyTransport, editInteractiveMessage, sendInteractiveMessage } =
166
+ replyRuntime;
167
+ const { sendTextReply, sendMarkdownReply } =
168
+ OutboundHandlers.createTelegramOutboundTextReplyRuntime({
169
+ sendTextReply: replyRuntime.sendTextReply,
170
+ sendMarkdownReply: replyRuntime.sendMarkdownReply,
171
+ execCommand: CommandTemplates.execCommandTemplate,
172
+ getHandlers: configStore.getOutboundHandlers,
173
+ recordRuntimeEvent,
174
+ });
173
175
  const dispatchNextQueuedTelegramTurn =
174
176
  Queue.createTelegramQueueDispatchRuntime<Pi.ExtensionContext>({
175
177
  ...telegramQueueStore,
@@ -198,6 +200,13 @@ export default function (pi: Pi.ExtensionAPI) {
198
200
  editMessageText: editTelegramMessageText,
199
201
  ...replyTransport,
200
202
  });
203
+ const { finalizeMarkdownPreview } =
204
+ OutboundHandlers.createTelegramOutboundTextPreviewRuntime({
205
+ finalizeMarkdownPreview: previewRuntime.finalizeMarkdown,
206
+ execCommand: CommandTemplates.execCommandTemplate,
207
+ getHandlers: configStore.getOutboundHandlers,
208
+ recordRuntimeEvent,
209
+ });
201
210
 
202
211
  // --- Bridge Setup ---
203
212
 
@@ -218,9 +227,8 @@ export default function (pi: Pi.ExtensionAPI) {
218
227
  appendQueuedItem: queueMutationRuntime.append,
219
228
  updateStatus,
220
229
  });
221
- const getQueueItemCount = Queue.createTelegramQueueItemCountGetter(
222
- telegramQueueStore,
223
- );
230
+ const getQueueItemCount =
231
+ Queue.createTelegramQueueItemCountGetter(telegramQueueStore);
224
232
  const getPromptTemplateCommands =
225
233
  PromptTemplates.createTelegramPromptTemplateCommandGetter({
226
234
  getCommands,
@@ -292,7 +300,7 @@ export default function (pi: Pi.ExtensionAPI) {
292
300
  openQueueMenu: queueMenuRuntime.openQueueMenu,
293
301
  queueMenuCallbackHandler: queueMenuRuntime.handleCallbackQuery,
294
302
  buttonActionStore,
295
- attachmentHandlerRuntime,
303
+ inboundHandlerRuntime,
296
304
  updateStatus,
297
305
  dispatchNextQueuedTelegramTurn,
298
306
  answerCallbackQuery,
@@ -367,7 +375,7 @@ export default function (pi: Pi.ExtensionAPI) {
367
375
 
368
376
  // --- Extension API Bindings ---
369
377
 
370
- Attachments.registerTelegramAttachmentTool(pi, {
378
+ OutboundAttachments.registerTelegramOutboundAttachmentTool(pi, {
371
379
  getActiveTurn: activeTurnRuntime.get,
372
380
  recordRuntimeEvent,
373
381
  });
@@ -402,7 +410,7 @@ export default function (pi: Pi.ExtensionAPI) {
402
410
  clearDispatchPending: lifecycle.clearDispatchPending,
403
411
  });
404
412
  const queuedAttachmentSender =
405
- Attachments.createTelegramQueuedAttachmentSender({
413
+ OutboundAttachments.createTelegramQueuedOutboundAttachmentSender({
406
414
  sendMultipart: callMultipart,
407
415
  sendTextReply,
408
416
  recordRuntimeEvent,
@@ -437,14 +445,15 @@ export default function (pi: Pi.ExtensionAPI) {
437
445
  updateStatus,
438
446
  getActiveTurn: activeTurnRuntime.get,
439
447
  extractAssistant: Replies.extractLatestAssistantMessageText,
440
- getPreserveQueuedTurnsAsHistory: lifecycle.shouldPreserveQueuedTurnsAsHistory,
448
+ getPreserveQueuedTurnsAsHistory:
449
+ lifecycle.shouldPreserveQueuedTurnsAsHistory,
441
450
  resetRuntimeState: agentEndResetter,
442
451
  dispatchNextQueuedTelegramTurn,
443
452
  requestDeferredDispatchNextQueuedTelegramTurn:
444
453
  deferredQueueDispatchRuntime.request,
445
454
  clearPreview: previewRuntime.clear,
446
455
  setPreviewPendingText: previewRuntime.setPendingText,
447
- finalizeMarkdownPreview: previewRuntime.finalizeMarkdown,
456
+ finalizeMarkdownPreview,
448
457
  sendMarkdownReply,
449
458
  sendTextReply,
450
459
  sendQueuedAttachments: queuedAttachmentSender,
@@ -456,7 +465,9 @@ export default function (pi: Pi.ExtensionAPI) {
456
465
  });
457
466
  // Wire transport-level reply dedup reset via lifecycle
458
467
  Lifecycle.setResetTransportReplyDedup(Replies.resetTransportReplyDedup);
459
- const agentStartWithDedupReset = Lifecycle.createAgentStartDedupHook(agentLifecycleHooks.onAgentStart);
468
+ const agentStartWithDedupReset = Lifecycle.createAgentStartDedupHook(
469
+ agentLifecycleHooks.onAgentStart,
470
+ );
460
471
  Lifecycle.registerTelegramLifecycleHooks(pi, {
461
472
  ...sessionLifecycleRuntime,
462
473
  ...agentLifecycleHooks,
package/lib/config.ts CHANGED
@@ -9,7 +9,7 @@ import { chmod, mkdir, readFile, rename, writeFile } from "node:fs/promises";
9
9
  import { homedir } from "node:os";
10
10
  import { join, resolve } from "node:path";
11
11
 
12
- import type { TelegramAttachmentHandlerConfig } from "./attachment-handlers.ts";
12
+ import type { TelegramInboundHandlerConfig } from "./inbound-handlers.ts";
13
13
  import type { CommandTemplateObjectConfig } from "./command-templates.ts";
14
14
 
15
15
  function getAgentDir(): string {
@@ -39,7 +39,8 @@ export interface TelegramConfig {
39
39
  botId?: number;
40
40
  allowedUserId?: number;
41
41
  lastUpdateId?: number;
42
- attachmentHandlers?: TelegramAttachmentHandlerConfig[];
42
+ inboundHandlers?: TelegramInboundHandlerConfig[];
43
+ attachmentHandlers?: TelegramInboundHandlerConfig[];
43
44
  outboundHandlers?: TelegramOutboundHandlerConfig[];
44
45
  }
45
46
 
@@ -50,7 +51,8 @@ export interface TelegramConfigStore {
50
51
  getBotToken: () => string | undefined;
51
52
  hasBotToken: () => boolean;
52
53
  getAllowedUserId: () => number | undefined;
53
- getAttachmentHandlers: () => TelegramAttachmentHandlerConfig[] | undefined;
54
+ getInboundHandlers: () => TelegramInboundHandlerConfig[] | undefined;
55
+ getAttachmentHandlers: () => TelegramInboundHandlerConfig[] | undefined;
54
56
  getOutboundHandlers: () => TelegramOutboundHandlerConfig[] | undefined;
55
57
  setAllowedUserId: (userId: number) => void;
56
58
  load: () => Promise<void>;
@@ -104,6 +106,10 @@ export function createTelegramConfigStore(
104
106
  getBotToken: () => config.botToken,
105
107
  hasBotToken: () => !!config.botToken,
106
108
  getAllowedUserId: () => config.allowedUserId,
109
+ getInboundHandlers: () => [
110
+ ...(config.inboundHandlers ?? []),
111
+ ...(config.attachmentHandlers ?? []),
112
+ ],
107
113
  getAttachmentHandlers: () => config.attachmentHandlers,
108
114
  getOutboundHandlers: () => config.outboundHandlers,
109
115
  setAllowedUserId: (userId) => {