@llblab/pi-telegram 0.11.2 → 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.
- package/AGENTS.md +20 -15
- package/BACKLOG.md +1 -11
- package/CHANGELOG.md +41 -1
- package/README.md +15 -41
- package/api/inbound.ts +14 -0
- package/api/keyboard.ts +10 -0
- package/api/outbound.ts +11 -0
- package/api/sections.ts +17 -0
- package/api/updates.ts +11 -0
- package/api/voice.ts +24 -0
- package/docs/README.md +7 -5
- package/docs/architecture.md +162 -226
- package/docs/callback-namespaces.md +3 -3
- package/docs/command-templates.md +18 -16
- package/docs/{inbound-handlers.md → inbound.md} +14 -11
- package/docs/locks.md +3 -3
- package/docs/{outbound-handlers.md → outbound.md} +14 -11
- package/docs/public-api.md +420 -0
- package/docs/{extension-sections.md → sections.md} +34 -30
- package/docs/ui-style.md +165 -0
- package/docs/{external-handlers.md → updates.md} +33 -31
- package/docs/voice.md +27 -19
- package/index.ts +88 -242
- package/lib/bindings.ts +299 -0
- package/lib/command-templates.ts +249 -60
- package/lib/commands.ts +114 -1
- package/lib/config.ts +44 -4
- package/lib/{inbound-handlers.ts → inbound.ts} +31 -21
- package/lib/lifecycle.ts +41 -6
- package/lib/locks.ts +4 -1
- package/lib/menu-model.ts +3 -3
- package/lib/menu-queue.ts +1 -1
- package/lib/menu-settings.ts +21 -10
- package/lib/menu-status.ts +1 -1
- package/lib/menu.ts +1 -1
- package/lib/outbound-buttons.ts +226 -0
- package/lib/outbound-markup.ts +357 -0
- package/lib/outbound-voice.ts +263 -0
- package/lib/outbound.ts +908 -0
- package/lib/polling.ts +4 -3
- package/lib/preview.ts +2 -2
- package/lib/queue.ts +3 -0
- package/lib/replies.ts +4 -1
- package/lib/routing.ts +44 -3
- package/lib/{extension-sections.ts → sections.ts} +37 -8
- package/lib/status.ts +13 -0
- package/lib/{api.ts → telegram-api.ts} +4 -4
- package/lib/text-groups.ts +3 -2
- package/lib/updates.ts +121 -1
- package/lib/voice.ts +67 -21
- package/package.json +13 -3
- package/lib/external-handlers.ts +0 -166
- package/lib/outbound-handlers.ts +0 -1663
package/index.ts
CHANGED
|
@@ -4,18 +4,11 @@
|
|
|
4
4
|
* Keeps the runtime wiring in one place while delegating reusable domain logic to /lib modules
|
|
5
5
|
*/
|
|
6
6
|
|
|
7
|
-
import * as
|
|
7
|
+
import * as Bindings from "./lib/bindings.ts";
|
|
8
8
|
import * as CommandTemplates from "./lib/command-templates.ts";
|
|
9
9
|
import * as Commands from "./lib/commands.ts";
|
|
10
10
|
import * as Config from "./lib/config.ts";
|
|
11
|
-
import
|
|
12
|
-
createTelegramExtensionSectionRegistry,
|
|
13
|
-
setGlobalTelegramSectionRegistry,
|
|
14
|
-
type TelegramSectionRegistry,
|
|
15
|
-
} from "./lib/extension-sections.ts";
|
|
16
|
-
import { createTelegramExternalHandleUpdate } from "./lib/external-handlers.ts";
|
|
17
|
-
import * as InboundHandlers from "./lib/inbound-handlers.ts";
|
|
18
|
-
import * as Keyboard from "./lib/keyboard.ts";
|
|
11
|
+
import * as Inbound from "./lib/inbound.ts";
|
|
19
12
|
import * as Lifecycle from "./lib/lifecycle.ts";
|
|
20
13
|
import * as Locks from "./lib/locks.ts";
|
|
21
14
|
import * as Media from "./lib/media.ts";
|
|
@@ -23,27 +16,24 @@ import * as MenuQueue from "./lib/menu-queue.ts";
|
|
|
23
16
|
import * as MenuSettings from "./lib/menu-settings.ts";
|
|
24
17
|
import * as Menu from "./lib/menu.ts";
|
|
25
18
|
import * as Model from "./lib/model.ts";
|
|
26
|
-
import * as
|
|
27
|
-
import * as OutboundHandlers from "./lib/outbound-handlers.ts";
|
|
19
|
+
import * as Outbound from "./lib/outbound.ts";
|
|
28
20
|
import * as Pi from "./lib/pi.ts";
|
|
29
21
|
import * as Polling from "./lib/polling.ts";
|
|
30
22
|
import * as Preview from "./lib/preview.ts";
|
|
31
23
|
import * as PromptTemplates from "./lib/prompt-templates.ts";
|
|
32
|
-
import * as Prompts from "./lib/prompts.ts";
|
|
33
24
|
import * as Queue from "./lib/queue.ts";
|
|
34
25
|
import * as Replies from "./lib/replies.ts";
|
|
35
26
|
import * as Routing from "./lib/routing.ts";
|
|
36
27
|
import * as Runtime from "./lib/runtime.ts";
|
|
37
|
-
import * as
|
|
28
|
+
import * as Sections from "./lib/sections.ts";
|
|
38
29
|
import * as Status from "./lib/status.ts";
|
|
30
|
+
import * as TelegramApi from "./lib/telegram-api.ts";
|
|
39
31
|
import * as TextGroups from "./lib/text-groups.ts";
|
|
40
32
|
import * as TimeInjection from "./lib/time-injection.ts";
|
|
33
|
+
import * as Updates from "./lib/updates.ts";
|
|
41
34
|
import * as Voice from "./lib/voice.ts";
|
|
42
35
|
|
|
43
|
-
const VOICE_EVENT_RECORDER_KEY = "__piTelegramVoiceEventRecorder__";
|
|
44
|
-
|
|
45
36
|
type ActivePiModel = NonNullable<Pi.ExtensionContext["model"]>;
|
|
46
|
-
type RuntimeTelegramQueueItem = Queue.TelegramQueueItem<Pi.ExtensionContext>;
|
|
47
37
|
|
|
48
38
|
// --- Extension Runtime ---
|
|
49
39
|
|
|
@@ -59,31 +49,8 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
59
49
|
const bridgeRuntime = Runtime.createTelegramBridgeRuntime();
|
|
60
50
|
const { abort, lifecycle, queue, setup, typing } = bridgeRuntime;
|
|
61
51
|
const configStore = Config.createTelegramConfigStore();
|
|
62
|
-
Config.
|
|
63
|
-
|
|
64
|
-
const current = configStore.get();
|
|
65
|
-
const next = {
|
|
66
|
-
...current,
|
|
67
|
-
voice: { ...(current.voice ?? {}), ...voice },
|
|
68
|
-
};
|
|
69
|
-
configStore.set(next);
|
|
70
|
-
void configStore.persist(next);
|
|
71
|
-
},
|
|
72
|
-
});
|
|
73
|
-
const isProactivePushEnabled =
|
|
74
|
-
Config.createTelegramProactivePushChecker(configStore);
|
|
75
|
-
const setProactivePushEnabled =
|
|
76
|
-
Config.createTelegramProactivePushSetter(configStore);
|
|
77
|
-
const getVoiceReplyMode =
|
|
78
|
-
Config.createTelegramVoiceReplyModeGetter(configStore);
|
|
79
|
-
const isVoiceReplyModeConfigured =
|
|
80
|
-
Config.createTelegramVoiceReplyModeConfiguredChecker(configStore);
|
|
81
|
-
const setVoiceReplyMode =
|
|
82
|
-
Config.createTelegramVoiceReplyModeSetter(configStore);
|
|
83
|
-
const getTimeInjectionMode =
|
|
84
|
-
Config.createTelegramTimeInjectionModeGetter(configStore);
|
|
85
|
-
const setTimeInjectionMode =
|
|
86
|
-
Config.createTelegramTimeInjectionModeSetter(configStore);
|
|
52
|
+
Config.bindGlobalTelegramConfigRuntime(configStore);
|
|
53
|
+
const configControls = Config.createTelegramConfigControls(configStore);
|
|
87
54
|
const lockRuntime = Locks.createTelegramLockRuntime<Pi.ExtensionContext>();
|
|
88
55
|
const lockOwnershipGuard =
|
|
89
56
|
Locks.createTelegramLockOwnershipGuard(lockRuntime);
|
|
@@ -93,15 +60,13 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
93
60
|
getActiveTurnChatId: activeTurnRuntime.getChatId,
|
|
94
61
|
getAllowedUserId: configStore.getAllowedUserId,
|
|
95
62
|
});
|
|
96
|
-
const buttonActionStore =
|
|
63
|
+
const buttonActionStore = Outbound.createTelegramButtonActionStore();
|
|
97
64
|
const pendingModelSwitchStore =
|
|
98
65
|
Model.createPendingModelSwitchStore<
|
|
99
66
|
Model.ScopedTelegramModel<ActivePiModel>
|
|
100
67
|
>();
|
|
101
68
|
const modelMenuRuntime = Menu.createTelegramModelMenuRuntime<ActivePiModel>();
|
|
102
|
-
const sectionRegistry
|
|
103
|
-
createTelegramExtensionSectionRegistry();
|
|
104
|
-
setGlobalTelegramSectionRegistry(sectionRegistry);
|
|
69
|
+
const sectionRegistry = Sections.createAndBindTelegramSectionRegistry();
|
|
105
70
|
|
|
106
71
|
const runtimeEvents = Status.createTelegramRuntimeEventRecorder({
|
|
107
72
|
getBotToken: configStore.getBotToken,
|
|
@@ -111,18 +76,17 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
111
76
|
getConfig: Config.createTelegramTimeConfigGetter(configStore),
|
|
112
77
|
recordRuntimeEvent,
|
|
113
78
|
});
|
|
114
|
-
(
|
|
115
|
-
recordRuntimeEvent;
|
|
79
|
+
Outbound.bindTelegramRuntimeEventRecorder(recordRuntimeEvent);
|
|
116
80
|
const getContextModel = Pi.getExtensionContextModel;
|
|
117
81
|
const isIdle = Pi.isExtensionContextIdle;
|
|
118
82
|
const hasPendingMessages = Pi.hasExtensionContextPendingMessages;
|
|
119
83
|
const compact = Pi.compactExtensionContext;
|
|
120
84
|
const mediaGroupRuntime = Media.createTelegramMediaGroupController<
|
|
121
|
-
|
|
85
|
+
TelegramApi.TelegramMessage,
|
|
122
86
|
Pi.ExtensionContext
|
|
123
87
|
>();
|
|
124
88
|
const textGroupRuntime = TextGroups.createTelegramTextGroupController<
|
|
125
|
-
|
|
89
|
+
TelegramApi.TelegramMessage,
|
|
126
90
|
Pi.ExtensionContext
|
|
127
91
|
>();
|
|
128
92
|
const telegramQueueStore =
|
|
@@ -136,7 +100,7 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
136
100
|
const { getStatusLines, updateStatus } =
|
|
137
101
|
Status.createTelegramBridgeStatusRuntime<
|
|
138
102
|
Pi.ExtensionContext,
|
|
139
|
-
|
|
103
|
+
Queue.TelegramQueueItem<Pi.ExtensionContext>
|
|
140
104
|
>({
|
|
141
105
|
getConfig: configStore.get,
|
|
142
106
|
isPollingActive: Polling.createTelegramPollingActivityReader(
|
|
@@ -153,28 +117,23 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
153
117
|
getRecentRuntimeEvents: runtimeEvents.getEvents,
|
|
154
118
|
getRuntimeLockState: lockRuntime.getStatusLabel,
|
|
155
119
|
});
|
|
156
|
-
const currentModelRuntime = Model.createCurrentModelRuntime
|
|
157
|
-
Pi.ExtensionContext,
|
|
158
|
-
ActivePiModel
|
|
159
|
-
>({
|
|
120
|
+
const currentModelRuntime = Model.createCurrentModelRuntime({
|
|
160
121
|
getContextModel,
|
|
161
122
|
updateStatus,
|
|
162
123
|
});
|
|
163
|
-
const queueMutationRuntime =
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
incrementNextPriorityReactionOrder
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
recordRuntimeEvent,
|
|
177
|
-
});
|
|
124
|
+
const queueMutationRuntime = Queue.createTelegramQueueMutationController({
|
|
125
|
+
...telegramQueueStore,
|
|
126
|
+
getNextPriorityReactionOrder: queue.getNextPriorityReactionOrder,
|
|
127
|
+
incrementNextPriorityReactionOrder:
|
|
128
|
+
queue.incrementNextPriorityReactionOrder,
|
|
129
|
+
updateStatus,
|
|
130
|
+
});
|
|
131
|
+
const inboundHandlerRuntime = Inbound.createTelegramInboundHandlerRuntime({
|
|
132
|
+
getHandlers: configStore.getInboundHandlers,
|
|
133
|
+
execCommand: CommandTemplates.execCommandTemplate,
|
|
134
|
+
getCwd: Pi.getExtensionContextCwd,
|
|
135
|
+
recordRuntimeEvent,
|
|
136
|
+
});
|
|
178
137
|
|
|
179
138
|
// --- Telegram API ---
|
|
180
139
|
|
|
@@ -194,41 +153,37 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
194
153
|
answerGuestQuery,
|
|
195
154
|
deleteMessage: deleteTelegramMessage,
|
|
196
155
|
prepareTempDir,
|
|
197
|
-
} =
|
|
156
|
+
} = TelegramApi.createDefaultTelegramBridgeApiRuntime({
|
|
198
157
|
getBotToken: configStore.getBotToken,
|
|
199
158
|
recordRuntimeEvent,
|
|
200
159
|
});
|
|
201
160
|
|
|
202
|
-
// --- Message Delivery
|
|
161
|
+
// --- Message Delivery ---
|
|
203
162
|
|
|
204
163
|
const sendGuestReply = Replies.createGuestMarkdownReplySender({
|
|
205
164
|
renderTelegramMessage: Replies.renderTelegramMessage,
|
|
206
165
|
answerGuestQuery,
|
|
207
166
|
});
|
|
208
167
|
|
|
209
|
-
const promptDispatchRuntime =
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
});
|
|
168
|
+
const promptDispatchRuntime = Runtime.createTelegramPromptDispatchRuntime({
|
|
169
|
+
lifecycle,
|
|
170
|
+
typing,
|
|
171
|
+
getDefaultChatId: proactivePushChatIdGetter,
|
|
172
|
+
sendTypingAction,
|
|
173
|
+
updateStatus,
|
|
174
|
+
recordRuntimeEvent,
|
|
175
|
+
});
|
|
218
176
|
|
|
219
|
-
// --- Reply Runtime
|
|
177
|
+
// --- Reply Runtime & Preview ---
|
|
220
178
|
|
|
221
|
-
const replyRuntime =
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
editMessage: editTelegramMessageText,
|
|
226
|
-
},
|
|
227
|
-
);
|
|
179
|
+
const replyRuntime = Replies.createTelegramRenderedMessageDeliveryRuntime({
|
|
180
|
+
sendMessage,
|
|
181
|
+
editMessage: editTelegramMessageText,
|
|
182
|
+
});
|
|
228
183
|
const { replyTransport, editInteractiveMessage, sendInteractiveMessage } =
|
|
229
184
|
replyRuntime;
|
|
230
185
|
const { sendTextReply, sendMarkdownReply } =
|
|
231
|
-
|
|
186
|
+
Outbound.createTelegramOutboundTextReplyRuntime({
|
|
232
187
|
sendTextReply: replyRuntime.sendTextReply,
|
|
233
188
|
sendMarkdownReply: replyRuntime.sendMarkdownReply,
|
|
234
189
|
execCommand: CommandTemplates.execCommandTemplate,
|
|
@@ -236,7 +191,7 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
236
191
|
recordRuntimeEvent,
|
|
237
192
|
});
|
|
238
193
|
const dispatchNextQueuedTelegramTurn =
|
|
239
|
-
Queue.createTelegramQueueDispatchRuntime
|
|
194
|
+
Queue.createTelegramQueueDispatchRuntime({
|
|
240
195
|
...telegramQueueStore,
|
|
241
196
|
isCompactionInProgress: lifecycle.isCompactionInProgress,
|
|
242
197
|
hasActiveTurn: activeTurnRuntime.has,
|
|
@@ -250,10 +205,7 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
250
205
|
...promptDispatchRuntime,
|
|
251
206
|
sendUserMessage,
|
|
252
207
|
}).dispatchNext;
|
|
253
|
-
const previewRuntime = Preview.createTelegramAssistantPreviewRuntime
|
|
254
|
-
unknown,
|
|
255
|
-
Keyboard.TelegramInlineKeyboardMarkup
|
|
256
|
-
>({
|
|
208
|
+
const previewRuntime = Preview.createTelegramAssistantPreviewRuntime({
|
|
257
209
|
getActiveTurn: activeTurnRuntime.get,
|
|
258
210
|
isAssistantMessage: Replies.isAssistantAgentMessage,
|
|
259
211
|
getMessageText: Replies.getAgentMessageText,
|
|
@@ -266,20 +218,17 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
266
218
|
...replyTransport,
|
|
267
219
|
});
|
|
268
220
|
const { finalizeMarkdownPreview } =
|
|
269
|
-
|
|
221
|
+
Outbound.createTelegramOutboundTextPreviewRuntime({
|
|
270
222
|
finalizeMarkdownPreview: previewRuntime.finalizeMarkdown,
|
|
271
223
|
execCommand: CommandTemplates.execCommandTemplate,
|
|
272
224
|
getHandlers: configStore.getOutboundHandlers,
|
|
273
225
|
recordRuntimeEvent,
|
|
274
226
|
});
|
|
275
227
|
|
|
276
|
-
// ---
|
|
228
|
+
// --- Model And Menu Setup ---
|
|
277
229
|
|
|
278
230
|
const modelSwitchController =
|
|
279
|
-
Model.createTelegramModelSwitchControllerRuntime
|
|
280
|
-
Pi.ExtensionContext,
|
|
281
|
-
Model.ScopedTelegramModel<ActivePiModel>
|
|
282
|
-
>({
|
|
231
|
+
Model.createTelegramModelSwitchControllerRuntime({
|
|
283
232
|
isIdle,
|
|
284
233
|
getPendingModelSwitch: pendingModelSwitchStore.get,
|
|
285
234
|
setPendingModelSwitch: pendingModelSwitchStore.set,
|
|
@@ -299,10 +248,7 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
299
248
|
getCommands,
|
|
300
249
|
reservedCommandNames: Commands.TELEGRAM_RESERVED_COMMAND_NAMES,
|
|
301
250
|
});
|
|
302
|
-
const menuActions = Menu.createTelegramMenuActionRuntimeWithStateBuilder
|
|
303
|
-
ActivePiModel,
|
|
304
|
-
Pi.ExtensionContext
|
|
305
|
-
>({
|
|
251
|
+
const menuActions = Menu.createTelegramMenuActionRuntimeWithStateBuilder({
|
|
306
252
|
runtime: modelMenuRuntime,
|
|
307
253
|
createSettingsManager: Pi.createSettingsManager,
|
|
308
254
|
getActiveModel: currentModelRuntime.get,
|
|
@@ -323,14 +269,14 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
323
269
|
sendInteractiveMessage,
|
|
324
270
|
sectionRegistry,
|
|
325
271
|
|
|
326
|
-
//
|
|
272
|
+
// Menu/status UI uses this to reflect whether the active Telegram turn expects voice delivery.
|
|
327
273
|
isVoiceReplyActive: function () {
|
|
328
274
|
const turn = activeTurnRuntime.get();
|
|
329
275
|
return Voice.isVoiceTurn(turn);
|
|
330
276
|
},
|
|
331
277
|
});
|
|
332
278
|
|
|
333
|
-
// --- Queue
|
|
279
|
+
// --- Queue And Settings Menus ---
|
|
334
280
|
|
|
335
281
|
const getQueueMenuState = Menu.createTelegramModelMenuStateBuilder({
|
|
336
282
|
runtime: modelMenuRuntime,
|
|
@@ -357,26 +303,14 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
357
303
|
editInteractiveMessage,
|
|
358
304
|
sendInteractiveMessage,
|
|
359
305
|
answerCallbackQuery,
|
|
360
|
-
|
|
361
|
-
getVoiceReplyMode,
|
|
362
|
-
isVoiceReplyModeConfigured,
|
|
363
|
-
getTimeInjectionMode,
|
|
364
|
-
setProactivePushEnabled,
|
|
365
|
-
setVoiceReplyMode,
|
|
366
|
-
setTimeInjectionMode,
|
|
306
|
+
...configControls,
|
|
367
307
|
},
|
|
368
308
|
sectionRegistry,
|
|
369
309
|
);
|
|
370
310
|
|
|
371
311
|
// --- Polling ---
|
|
372
312
|
|
|
373
|
-
const inboundRouteRuntime = Routing.createTelegramInboundRouteRuntime
|
|
374
|
-
Api.TelegramUpdate,
|
|
375
|
-
Api.TelegramMessage,
|
|
376
|
-
Api.TelegramCallbackQuery,
|
|
377
|
-
Pi.ExtensionContext,
|
|
378
|
-
ActivePiModel
|
|
379
|
-
>({
|
|
313
|
+
const inboundRouteRuntime = Routing.createTelegramInboundRouteRuntime({
|
|
380
314
|
configStore,
|
|
381
315
|
bridgeRuntime,
|
|
382
316
|
activeTurnRuntime,
|
|
@@ -425,17 +359,14 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
425
359
|
compact,
|
|
426
360
|
recordRuntimeEvent,
|
|
427
361
|
});
|
|
428
|
-
const pollingRuntime = Polling.createTelegramPollingControllerRuntime
|
|
429
|
-
Api.TelegramUpdate,
|
|
430
|
-
Pi.ExtensionContext
|
|
431
|
-
>({
|
|
362
|
+
const pollingRuntime = Polling.createTelegramPollingControllerRuntime({
|
|
432
363
|
state: pollingControllerState,
|
|
433
364
|
getConfig: configStore.get,
|
|
434
365
|
hasBotToken: configStore.hasBotToken,
|
|
435
366
|
deleteWebhook,
|
|
436
367
|
getUpdates,
|
|
437
368
|
persistConfig: configStore.persist,
|
|
438
|
-
handleUpdate:
|
|
369
|
+
handleUpdate: Updates.createTelegramUpdateHandle({
|
|
439
370
|
defaultHandle: inboundRouteRuntime.handleUpdate,
|
|
440
371
|
}),
|
|
441
372
|
stopTypingLoop: typing.stop,
|
|
@@ -450,11 +381,7 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
450
381
|
updateStatus,
|
|
451
382
|
recordRuntimeEvent,
|
|
452
383
|
});
|
|
453
|
-
const queueSessionLifecycle = Queue.createTelegramSessionLifecycleRuntime
|
|
454
|
-
Pi.ExtensionContext,
|
|
455
|
-
RuntimeTelegramQueueItem,
|
|
456
|
-
ActivePiModel
|
|
457
|
-
>({
|
|
384
|
+
const queueSessionLifecycle = Queue.createTelegramSessionLifecycleRuntime({
|
|
458
385
|
getCurrentModel: getContextModel,
|
|
459
386
|
loadConfig: configStore.load,
|
|
460
387
|
setQueuedItems: telegramQueueStore.setQueuedItems,
|
|
@@ -487,131 +414,50 @@ export default function (pi: Pi.ExtensionAPI) {
|
|
|
487
414
|
|
|
488
415
|
// --- Extension API Bindings ---
|
|
489
416
|
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
promptForConfig: Setup.createTelegramSetupPromptRuntime({
|
|
497
|
-
getConfig: configStore.get,
|
|
498
|
-
setConfig: configStore.set,
|
|
499
|
-
setupGuard: setup,
|
|
500
|
-
getMe: Api.fetchTelegramBotIdentity,
|
|
501
|
-
persistConfig: configStore.persist,
|
|
502
|
-
startPolling: lockedPollingRuntime.start,
|
|
503
|
-
updateStatus,
|
|
504
|
-
recordRuntimeEvent,
|
|
505
|
-
}),
|
|
417
|
+
Bindings.registerTelegramCommandsAndTools({
|
|
418
|
+
pi,
|
|
419
|
+
configStore,
|
|
420
|
+
setup,
|
|
421
|
+
activeTurnRuntime,
|
|
422
|
+
lockedPollingRuntime,
|
|
506
423
|
getStatusLines,
|
|
507
|
-
reloadConfig: configStore.load,
|
|
508
|
-
hasBotToken: configStore.hasBotToken,
|
|
509
|
-
startPolling: lockedPollingRuntime.start,
|
|
510
|
-
stopPolling: lockedPollingRuntime.stop,
|
|
511
424
|
updateStatus,
|
|
425
|
+
recordRuntimeEvent,
|
|
512
426
|
});
|
|
513
427
|
|
|
514
428
|
// --- Lifecycle Hooks ---
|
|
515
429
|
|
|
516
|
-
|
|
430
|
+
Bindings.registerTelegramLifecycleRuntimeHooks({
|
|
431
|
+
pi,
|
|
432
|
+
sessionLifecycleRuntime: {
|
|
433
|
+
...sessionLifecycleRuntime,
|
|
434
|
+
onModelSelect: currentModelRuntime.onModelSelect,
|
|
435
|
+
},
|
|
436
|
+
queueSessionLifecycle,
|
|
437
|
+
configStore,
|
|
517
438
|
abort,
|
|
518
439
|
typing,
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
OutboundHandlers.createTelegramOutboundReplyPlanner(buttonActionStore);
|
|
532
|
-
const outboundReplyArtifactSender =
|
|
533
|
-
OutboundHandlers.createTelegramOutboundReplyArtifactSender({
|
|
534
|
-
execCommand: CommandTemplates.execCommandTemplate,
|
|
535
|
-
sendMultipart: callMultipart,
|
|
536
|
-
sendTextReply,
|
|
537
|
-
sendChatAction,
|
|
538
|
-
sendRecordVoiceAction,
|
|
539
|
-
getHandlers: configStore.getOutboundHandlers,
|
|
540
|
-
recordRuntimeEvent,
|
|
541
|
-
});
|
|
542
|
-
const agentLifecycleHooks = Queue.createTelegramAgentLifecycleHooks<
|
|
543
|
-
Queue.PendingTelegramTurn,
|
|
544
|
-
Pi.ExtensionContext,
|
|
545
|
-
unknown,
|
|
546
|
-
Keyboard.TelegramInlineKeyboardMarkup
|
|
547
|
-
>({
|
|
548
|
-
setAbortHandler: Runtime.createTelegramContextAbortHandlerSetter(abort),
|
|
549
|
-
getQueuedItems: telegramQueueStore.getQueuedItems,
|
|
550
|
-
hasPendingDispatch: lifecycle.hasDispatchPending,
|
|
551
|
-
hasActiveTurn: activeTurnRuntime.has,
|
|
552
|
-
resetToolExecutions: lifecycle.resetActiveToolExecutions,
|
|
553
|
-
resetPendingModelSwitch: modelSwitchController.clearPendingSwitch,
|
|
554
|
-
setQueuedItems: telegramQueueStore.setQueuedItems,
|
|
555
|
-
clearDispatchPending: lifecycle.clearDispatchPending,
|
|
556
|
-
setActiveTurn: activeTurnRuntime.set,
|
|
557
|
-
createPreviewState: previewRuntime.resetState,
|
|
558
|
-
startTypingLoop: promptDispatchRuntime.startTypingLoop,
|
|
559
|
-
updateStatus,
|
|
560
|
-
getActiveTurn: activeTurnRuntime.get,
|
|
561
|
-
extractAssistant: Replies.extractLatestAssistantMessageText,
|
|
562
|
-
getPreserveQueuedTurnsAsHistory:
|
|
563
|
-
lifecycle.shouldPreserveQueuedTurnsAsHistory,
|
|
564
|
-
resetRuntimeState: agentEndResetter,
|
|
565
|
-
dispatchNextQueuedTelegramTurn,
|
|
566
|
-
requestDeferredDispatchNextQueuedTelegramTurn:
|
|
567
|
-
deferredQueueDispatchRuntime.request,
|
|
568
|
-
clearPreview: previewRuntime.clear,
|
|
569
|
-
setPreviewPendingText: previewRuntime.setPendingText,
|
|
570
|
-
finalizeMarkdownPreview,
|
|
440
|
+
lifecycle,
|
|
441
|
+
activeTurnRuntime,
|
|
442
|
+
telegramQueueStore,
|
|
443
|
+
modelSwitchController,
|
|
444
|
+
previewRuntime,
|
|
445
|
+
promptDispatchRuntime,
|
|
446
|
+
deferredQueueDispatchRuntime,
|
|
447
|
+
lockOwnershipGuard,
|
|
448
|
+
buttonActionStore,
|
|
449
|
+
callMultipart,
|
|
450
|
+
sendChatAction,
|
|
451
|
+
sendRecordVoiceAction,
|
|
571
452
|
sendMarkdownReply,
|
|
572
453
|
sendTextReply,
|
|
573
|
-
|
|
454
|
+
dispatchNextQueuedTelegramTurn,
|
|
574
455
|
answerGuestQuery,
|
|
575
456
|
sendGuestReply,
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
getDefaultChatId: proactivePushChatIdGetter,
|
|
580
|
-
isProactivePushEnabled,
|
|
581
|
-
recordRuntimeEvent,
|
|
582
|
-
getActiveToolExecutions: lifecycle.getActiveToolExecutions,
|
|
583
|
-
setActiveToolExecutions: lifecycle.setActiveToolExecutions,
|
|
584
|
-
triggerPendingModelSwitchAbort: modelSwitchController.triggerPendingAbort,
|
|
585
|
-
});
|
|
586
|
-
// Wire transport-level reply dedup reset via lifecycle
|
|
587
|
-
Lifecycle.setResetTransportReplyDedup(Replies.resetTransportReplyDedup);
|
|
588
|
-
const agentStartWithDedupReset = Lifecycle.createAgentStartDedupHook(
|
|
589
|
-
agentLifecycleHooks.onAgentStart,
|
|
590
|
-
);
|
|
591
|
-
const compactionObserver = Lifecycle.createTelegramCompactionObserverRuntime({
|
|
592
|
-
setCompactionInProgress: lifecycle.setCompactionInProgress,
|
|
457
|
+
finalizeMarkdownPreview,
|
|
458
|
+
proactivePushChatIdGetter,
|
|
459
|
+
isProactivePushEnabled: configControls.isProactivePushEnabled,
|
|
593
460
|
updateStatus,
|
|
594
|
-
requestDeferredDispatchNextQueuedTelegramTurn:
|
|
595
|
-
deferredQueueDispatchRuntime.request,
|
|
596
|
-
dispatchNextQueuedTelegramTurn,
|
|
597
461
|
recordRuntimeEvent,
|
|
598
462
|
});
|
|
599
|
-
Lifecycle.registerTelegramLifecycleHooks(pi, {
|
|
600
|
-
...sessionLifecycleRuntime,
|
|
601
|
-
...agentLifecycleHooks,
|
|
602
|
-
async onSessionShutdown(event, ctx) {
|
|
603
|
-
compactionObserver.onSessionShutdown();
|
|
604
|
-
await sessionLifecycleRuntime.onSessionShutdown(event, ctx);
|
|
605
|
-
},
|
|
606
|
-
onSessionBeforeCompact: compactionObserver.onSessionBeforeCompact,
|
|
607
|
-
onSessionCompact: compactionObserver.onSessionCompact,
|
|
608
|
-
onAgentStart: agentStartWithDedupReset,
|
|
609
|
-
onBeforeAgentStart: Prompts.createTelegramProactiveBeforeAgentStartHook({
|
|
610
|
-
isProactivePushEnabled,
|
|
611
|
-
isCurrentOwner: lockOwnershipGuard.ownsContext,
|
|
612
|
-
}),
|
|
613
|
-
onModelSelect: currentModelRuntime.onModelSelect,
|
|
614
|
-
onMessageStart: previewRuntime.onMessageStart,
|
|
615
|
-
onMessageUpdate: previewRuntime.onMessageUpdate,
|
|
616
|
-
});
|
|
617
463
|
}
|