@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/AGENTS.md +5 -5
- package/CHANGELOG.md +25 -1
- package/README.md +40 -10
- package/docs/README.md +2 -2
- package/docs/architecture.md +6 -6
- package/docs/command-templates.md +21 -5
- package/docs/inbound-handlers.md +93 -0
- package/docs/locks.md +13 -11
- package/docs/outbound-handlers.md +41 -5
- package/index.ts +42 -31
- package/lib/config.ts +9 -3
- package/lib/inbound-handlers.ts +588 -0
- package/lib/menu-queue.ts +161 -20
- package/lib/menu-status.ts +1 -1
- package/lib/{attachments.ts → outbound-attachments.ts} +34 -34
- package/lib/outbound-handlers.ts +247 -2
- package/lib/prompts.ts +1 -1
- package/lib/queue.ts +18 -4
- package/lib/routing.ts +3 -3
- package/lib/updates.ts +34 -21
- package/package.json +1 -1
- package/docs/attachment-handlers.md +0 -50
- package/lib/attachment-handlers.ts +0 -423
package/index.ts
CHANGED
|
@@ -5,18 +5,19 @@
|
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
7
|
import * as Api from "./lib/api.ts";
|
|
8
|
-
import * as
|
|
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
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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:
|
|
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
|
|
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(
|
|
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 {
|
|
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
|
-
|
|
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
|
-
|
|
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) => {
|