@osovv/vv-opencode 2.2.0 → 2.3.0-rc.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/CHANGELOG.md +16 -0
- package/README.md +47 -4
- package/dist/index.d.ts +1 -0
- package/dist/index.js +6 -3
- package/dist/index.js.map +1 -1
- package/dist/lib/plugin-toggle-config.d.ts +1 -1
- package/dist/lib/plugin-toggle-config.js +3 -1
- package/dist/lib/plugin-toggle-config.js.map +1 -1
- package/dist/lib/vvoc-config.d.ts +90 -5
- package/dist/lib/vvoc-config.js +123 -9
- package/dist/lib/vvoc-config.js.map +1 -1
- package/dist/plugins/guardian/index.d.ts +3 -6
- package/dist/plugins/guardian/index.js +7 -26
- package/dist/plugins/guardian/index.js.map +1 -1
- package/dist/plugins/telegram/bot-api.d.ts +162 -0
- package/dist/plugins/telegram/bot-api.js +273 -0
- package/dist/plugins/telegram/bot-api.js.map +1 -0
- package/dist/plugins/telegram/commands.d.ts +167 -0
- package/dist/plugins/telegram/commands.js +709 -0
- package/dist/plugins/telegram/commands.js.map +1 -0
- package/dist/plugins/telegram/config.d.ts +38 -0
- package/dist/plugins/telegram/config.js +86 -0
- package/dist/plugins/telegram/config.js.map +1 -0
- package/dist/plugins/telegram/delivery.d.ts +121 -0
- package/dist/plugins/telegram/delivery.js +395 -0
- package/dist/plugins/telegram/delivery.js.map +1 -0
- package/dist/plugins/telegram/gateway.d.ts +61 -0
- package/dist/plugins/telegram/gateway.js +230 -0
- package/dist/plugins/telegram/gateway.js.map +1 -0
- package/dist/plugins/telegram/index.d.ts +110 -0
- package/dist/plugins/telegram/index.js +390 -0
- package/dist/plugins/telegram/index.js.map +1 -0
- package/dist/plugins/telegram/sessions.d.ts +147 -0
- package/dist/plugins/telegram/sessions.js +322 -0
- package/dist/plugins/telegram/sessions.js.map +1 -0
- package/dist/plugins/telegram/topology.d.ts +116 -0
- package/dist/plugins/telegram/topology.js +339 -0
- package/dist/plugins/telegram/topology.js.map +1 -0
- package/dist/plugins/web-tools/schemas.d.ts +19 -19
- package/dist/plugins/workflow/results.d.ts +39 -39
- package/package.json +6 -1
- package/schemas/vvoc/v3.json +89 -13
- package/templates/skills/vv-execute/references/tool-contracts.md +1 -1
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
// FILE: src/plugins/telegram/delivery.ts
|
|
2
|
+
// VERSION: 1.0.0
|
|
3
|
+
// START_MODULE_CONTRACT
|
|
4
|
+
// PURPOSE: Own message delivery for the Telegram bridge: the durable mirror map, durable pending finals with boot-drain exactly-once semantics, draft streaming with a send-then-edit fallback, the adaptive stream throttle, the owner input merge window, persisted runtime settings, and final rendering including code-as-file extraction.
|
|
5
|
+
// SCOPE: Injectable transport, store, clock, and notice boundaries; versioned storage keys for mirrors, the bounded outbox, and settings; pure splitting, throttle-schedule, merge-window, and user-quote helpers; per-message failure containment with one bounded notice per pending record.
|
|
6
|
+
// DEPENDS: [src/plugins/telegram/bot-api.ts, src/plugins/telegram/config.ts, src/plugins/telegram/topology.ts]
|
|
7
|
+
// LINKS: [M-TELEGRAM-DELIVERY, M-TELEGRAM-BOT-API, M-TELEGRAM-TOPICS, V-M-TELEGRAM-DELIVERY]
|
|
8
|
+
// ROLE: RUNTIME
|
|
9
|
+
// MAP_MODE: EXPORTS
|
|
10
|
+
// END_MODULE_CONTRACT
|
|
11
|
+
//
|
|
12
|
+
// START_MODULE_MAP
|
|
13
|
+
// MIRROR_PREFIX - Storage key prefix for message mirror rows.
|
|
14
|
+
// OUTBOX_KEY - Storage key holding the bounded pending-finals queue.
|
|
15
|
+
// SETTINGS_KEY - Storage key holding persisted runtime settings.
|
|
16
|
+
// MAX_PENDING_FINALS - Hard cap on queued finals; the oldest entry drops beyond it.
|
|
17
|
+
// STREAM_DELAY_STEPS - Adaptive throttle schedule: elapsed-time brackets to edit intervals.
|
|
18
|
+
// DeliverySettings - Persisted runtime settings shape.
|
|
19
|
+
// PendingFinal - Durable final record with attempt counting.
|
|
20
|
+
// MirrorRow - Persisted native-to-Telegram message mirror row.
|
|
21
|
+
// SplitFinal - Pure rendering result: remaining text plus extracted code files.
|
|
22
|
+
// splitFinal - Extract oversized fenced code blocks into bounded file documents.
|
|
23
|
+
// nextStreamDelayMs - Pure adaptive throttle interval for streamed edits.
|
|
24
|
+
// formatUserQuote - Pure owner-quote projection for echoed prompts.
|
|
25
|
+
// TelegramInputMerger - Fixed-window owner-text coalescer with due/take semantics.
|
|
26
|
+
// TelegramDelivery - Delivery bound to injectable transport, store, and clock.
|
|
27
|
+
// END_MODULE_MAP
|
|
28
|
+
//
|
|
29
|
+
// START_CHANGE_SUMMARY
|
|
30
|
+
// LAST_CHANGE: [C-TELEGRAM-BRIDGE-PLUGIN T-004 - Created the mirror-based delivery layer with draft streaming, edit fallback, durable exactly-once finals with boot-drain, adaptive throttle, merge window, persisted settings, and code-as-file rendering.]
|
|
31
|
+
// END_CHANGE_SUMMARY
|
|
32
|
+
import { TelegramUnsupportedError } from "./bot-api.js";
|
|
33
|
+
/** Storage key prefix for message mirror rows. */
|
|
34
|
+
export const MIRROR_PREFIX = "telegram/v1/mirror/";
|
|
35
|
+
/** Storage key holding the bounded pending-finals queue. */
|
|
36
|
+
export const OUTBOX_KEY = "telegram/v1/outbox";
|
|
37
|
+
/** Storage key holding persisted runtime settings. */
|
|
38
|
+
export const SETTINGS_KEY = "telegram/v1/settings";
|
|
39
|
+
/** Hard cap on queued finals; the oldest entry drops beyond it. */
|
|
40
|
+
export const MAX_PENDING_FINALS = 500;
|
|
41
|
+
/** Adaptive throttle schedule: elapsed-time brackets to streamed-edit intervals. */
|
|
42
|
+
export const STREAM_DELAY_STEPS = [
|
|
43
|
+
{ withinMs: 60_000, delayMs: 1_000 },
|
|
44
|
+
{ withinMs: 180_000, delayMs: 2_000 },
|
|
45
|
+
{ withinMs: 600_000, delayMs: 5_000 },
|
|
46
|
+
];
|
|
47
|
+
// START_BLOCK_PURE_HELPERS
|
|
48
|
+
/** Extract fenced code blocks larger than the cap into bounded file documents. */
|
|
49
|
+
export function splitFinal(text, codeFileMaxKb) {
|
|
50
|
+
const maxChars = codeFileMaxKb * 1024;
|
|
51
|
+
const files = [];
|
|
52
|
+
let fileIndex = 0;
|
|
53
|
+
const parts = [];
|
|
54
|
+
const fence = /```([^\n`]*)\n([\s\S]*?)```/g;
|
|
55
|
+
let cursor = 0;
|
|
56
|
+
let match;
|
|
57
|
+
while ((match = fence.exec(text)) !== null) {
|
|
58
|
+
parts.push(text.slice(cursor, match.index));
|
|
59
|
+
const info = match[1]?.trim() ?? "";
|
|
60
|
+
const code = match[2] ?? "";
|
|
61
|
+
if (code.length > maxChars) {
|
|
62
|
+
fileIndex += 1;
|
|
63
|
+
const ext = info.length > 0 && /^[\w.+-]{1,10}$/.test(info) ? `.${info}` : ".txt";
|
|
64
|
+
files.push({ filename: `code-${fileIndex}${ext}`, content: code });
|
|
65
|
+
parts.push(`[code attached as code-${fileIndex}${ext}]`);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
parts.push(match[0]);
|
|
69
|
+
}
|
|
70
|
+
cursor = match.index + match[0].length;
|
|
71
|
+
}
|
|
72
|
+
parts.push(text.slice(cursor));
|
|
73
|
+
return { text: parts.join("").trim(), files };
|
|
74
|
+
}
|
|
75
|
+
/** Pure adaptive throttle interval for streamed edits over a turn's lifetime. */
|
|
76
|
+
export function nextStreamDelayMs(elapsedMs) {
|
|
77
|
+
for (const step of STREAM_DELAY_STEPS) {
|
|
78
|
+
if (elapsedMs < step.withinMs)
|
|
79
|
+
return step.delayMs;
|
|
80
|
+
}
|
|
81
|
+
return 10_000;
|
|
82
|
+
}
|
|
83
|
+
/** Pure owner-quote projection for echoed prompts. */
|
|
84
|
+
export function formatUserQuote(text, voice) {
|
|
85
|
+
const marker = voice ? "🎙" : "👤";
|
|
86
|
+
const body = text.trim().length > 0 ? text.trim() : "(empty)";
|
|
87
|
+
return `${marker} ${body}`;
|
|
88
|
+
}
|
|
89
|
+
// END_BLOCK_PURE_HELPERS
|
|
90
|
+
/** Fixed-window owner-text coalescer: the window opens on the first text and take() joins everything collected. */
|
|
91
|
+
export class TelegramInputMerger {
|
|
92
|
+
#windowMs;
|
|
93
|
+
#openedAtMs = null;
|
|
94
|
+
#buffer = [];
|
|
95
|
+
constructor(windowMs) {
|
|
96
|
+
this.#windowMs = windowMs;
|
|
97
|
+
}
|
|
98
|
+
add(text, nowMs) {
|
|
99
|
+
if (this.#openedAtMs === null)
|
|
100
|
+
this.#openedAtMs = nowMs;
|
|
101
|
+
this.#buffer.push(text);
|
|
102
|
+
}
|
|
103
|
+
due(nowMs) {
|
|
104
|
+
if (this.#openedAtMs === null)
|
|
105
|
+
return false;
|
|
106
|
+
return nowMs - this.#openedAtMs >= this.#windowMs;
|
|
107
|
+
}
|
|
108
|
+
/** Join and clear the buffer when due; otherwise return null. */
|
|
109
|
+
take(nowMs) {
|
|
110
|
+
if (!this.due(nowMs))
|
|
111
|
+
return null;
|
|
112
|
+
const joined = this.#buffer.join("\n");
|
|
113
|
+
this.#buffer = [];
|
|
114
|
+
this.#openedAtMs = null;
|
|
115
|
+
return joined;
|
|
116
|
+
}
|
|
117
|
+
/** Force-join and clear regardless of the window. */
|
|
118
|
+
flush() {
|
|
119
|
+
if (this.#buffer.length === 0)
|
|
120
|
+
return null;
|
|
121
|
+
const joined = this.#buffer.join("\n");
|
|
122
|
+
this.#buffer = [];
|
|
123
|
+
this.#openedAtMs = null;
|
|
124
|
+
return joined;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function isPlainObject(value) {
|
|
128
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
129
|
+
}
|
|
130
|
+
function decodeMirror(value) {
|
|
131
|
+
if (!isPlainObject(value))
|
|
132
|
+
return undefined;
|
|
133
|
+
const threadId = value.threadId;
|
|
134
|
+
const tgMessageId = value.tgMessageId;
|
|
135
|
+
const role = value.role;
|
|
136
|
+
if (typeof threadId !== "number" || typeof tgMessageId !== "number")
|
|
137
|
+
return undefined;
|
|
138
|
+
if (role !== "assistant" && role !== "user")
|
|
139
|
+
return undefined;
|
|
140
|
+
return { threadId, tgMessageId, role };
|
|
141
|
+
}
|
|
142
|
+
function decodePending(value) {
|
|
143
|
+
if (!isPlainObject(value))
|
|
144
|
+
return undefined;
|
|
145
|
+
const id = value.id;
|
|
146
|
+
const threadId = value.threadId;
|
|
147
|
+
const nativeMessageId = value.nativeMessageId;
|
|
148
|
+
const text = value.text;
|
|
149
|
+
const role = value.role;
|
|
150
|
+
const attempts = value.attempts;
|
|
151
|
+
if (typeof id !== "string" ||
|
|
152
|
+
typeof threadId !== "number" ||
|
|
153
|
+
typeof nativeMessageId !== "string" ||
|
|
154
|
+
typeof text !== "string") {
|
|
155
|
+
return undefined;
|
|
156
|
+
}
|
|
157
|
+
return {
|
|
158
|
+
id,
|
|
159
|
+
threadId,
|
|
160
|
+
nativeMessageId,
|
|
161
|
+
text,
|
|
162
|
+
role: role === "user" ? "user" : "assistant",
|
|
163
|
+
attempts: typeof attempts === "number" ? attempts : 0,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
function decodeSettings(value, fallback) {
|
|
167
|
+
if (!isPlainObject(value))
|
|
168
|
+
return fallback;
|
|
169
|
+
return {
|
|
170
|
+
showReasoning: value.showReasoning === true,
|
|
171
|
+
showToolCalls: value.showToolCalls === true,
|
|
172
|
+
formatMode: value.formatMode === "raw" ? "raw" : "markdown",
|
|
173
|
+
codeFileMaxKb: typeof value.codeFileMaxKb === "number" && value.codeFileMaxKb > 0
|
|
174
|
+
? value.codeFileMaxKb
|
|
175
|
+
: fallback.codeFileMaxKb,
|
|
176
|
+
mergeWindowMs: typeof value.mergeWindowMs === "number" && value.mergeWindowMs >= 0
|
|
177
|
+
? value.mergeWindowMs
|
|
178
|
+
: fallback.mergeWindowMs,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Delivery bound to injectable transport, store, and clock. Finals are durable:
|
|
183
|
+
* a record is enqueued before the send, the mirror is written after it, and the
|
|
184
|
+
* boot drain delivers records without mirrors exactly once per restart attempt.
|
|
185
|
+
* Streaming uses drafts with a stable per-turn id and falls back to a
|
|
186
|
+
* send-placeholder plus throttled edits once the transport reports drafts
|
|
187
|
+
* unsupported. Every failure is contained per message with one bounded notice
|
|
188
|
+
* per pending record.
|
|
189
|
+
*/
|
|
190
|
+
export class TelegramDelivery {
|
|
191
|
+
#transport;
|
|
192
|
+
#store;
|
|
193
|
+
#clock;
|
|
194
|
+
#onNotice;
|
|
195
|
+
#defaults;
|
|
196
|
+
#mirrors = new Map();
|
|
197
|
+
#streams = new Map();
|
|
198
|
+
#settings;
|
|
199
|
+
#seq = 0;
|
|
200
|
+
constructor(deps) {
|
|
201
|
+
this.#transport = deps.transport;
|
|
202
|
+
this.#store = deps.store;
|
|
203
|
+
this.#clock = deps.clock;
|
|
204
|
+
this.#defaults = deps.defaults;
|
|
205
|
+
this.#onNotice = deps.onNotice ?? (() => undefined);
|
|
206
|
+
this.#settings = deps.defaults;
|
|
207
|
+
}
|
|
208
|
+
/** Load persisted settings and mirrors; seeds settings on first run. */
|
|
209
|
+
async initialize() {
|
|
210
|
+
const stored = await this.#store.get(SETTINGS_KEY);
|
|
211
|
+
this.#settings = decodeSettings(stored, this.#defaults);
|
|
212
|
+
await this.#store.set(SETTINGS_KEY, this.#settings);
|
|
213
|
+
for (const entry of await this.#store.scan(MIRROR_PREFIX)) {
|
|
214
|
+
const row = decodeMirror(entry.value);
|
|
215
|
+
const nativeMessageId = entry.key.slice(MIRROR_PREFIX.length);
|
|
216
|
+
if (row !== undefined && nativeMessageId.length > 0) {
|
|
217
|
+
this.#mirrors.set(nativeMessageId, row);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
get settings() {
|
|
222
|
+
return this.#settings;
|
|
223
|
+
}
|
|
224
|
+
/** Merge and persist a settings patch. */
|
|
225
|
+
async updateSettings(patch) {
|
|
226
|
+
this.#settings = { ...this.#settings, ...patch };
|
|
227
|
+
await this.#store.set(SETTINGS_KEY, this.#settings);
|
|
228
|
+
return this.#settings;
|
|
229
|
+
}
|
|
230
|
+
/** Mirror row for a native message id, if delivered before. */
|
|
231
|
+
mirrorOf(nativeMessageId) {
|
|
232
|
+
return this.#mirrors.get(nativeMessageId);
|
|
233
|
+
}
|
|
234
|
+
async #readOutbox() {
|
|
235
|
+
const stored = await this.#store.get(OUTBOX_KEY);
|
|
236
|
+
if (!Array.isArray(stored))
|
|
237
|
+
return [];
|
|
238
|
+
return stored.map(decodePending).filter((entry) => entry !== undefined);
|
|
239
|
+
}
|
|
240
|
+
async #writeOutbox(records) {
|
|
241
|
+
const bounded = records.length > MAX_PENDING_FINALS
|
|
242
|
+
? records.slice(records.length - MAX_PENDING_FINALS)
|
|
243
|
+
: records;
|
|
244
|
+
await this.#store.set(OUTBOX_KEY, bounded);
|
|
245
|
+
}
|
|
246
|
+
/** Deliver a final message exactly once per native message id. */
|
|
247
|
+
async deliverFinal(input) {
|
|
248
|
+
const existing = this.#mirrors.get(input.nativeMessageId);
|
|
249
|
+
if (existing !== undefined)
|
|
250
|
+
return { delivered: false, tgMessageId: existing.tgMessageId };
|
|
251
|
+
const records = await this.#readOutbox();
|
|
252
|
+
if (records.some((record) => record.nativeMessageId === input.nativeMessageId)) {
|
|
253
|
+
return { delivered: false, tgMessageId: undefined };
|
|
254
|
+
}
|
|
255
|
+
this.#seq += 1;
|
|
256
|
+
const record = {
|
|
257
|
+
id: `${this.#clock.now()}-${this.#seq}`,
|
|
258
|
+
threadId: input.threadId,
|
|
259
|
+
nativeMessageId: input.nativeMessageId,
|
|
260
|
+
text: input.text,
|
|
261
|
+
role: input.role ?? "assistant",
|
|
262
|
+
attempts: 0,
|
|
263
|
+
};
|
|
264
|
+
await this.#writeOutbox([...records, record]);
|
|
265
|
+
return this.#sendPending(record);
|
|
266
|
+
}
|
|
267
|
+
async #sendPending(record) {
|
|
268
|
+
try {
|
|
269
|
+
const rendered = splitFinal(record.text, this.#settings.codeFileMaxKb);
|
|
270
|
+
let lastTgMessageId;
|
|
271
|
+
if (rendered.text.length > 0) {
|
|
272
|
+
lastTgMessageId = (await this.#transport.sendMessage({ threadId: record.threadId, text: rendered.text })).messageId;
|
|
273
|
+
}
|
|
274
|
+
for (const file of rendered.files) {
|
|
275
|
+
const sent = await this.#transport.sendDocument({
|
|
276
|
+
threadId: record.threadId,
|
|
277
|
+
filename: file.filename,
|
|
278
|
+
content: new TextEncoder().encode(file.content),
|
|
279
|
+
});
|
|
280
|
+
lastTgMessageId ??= sent.messageId;
|
|
281
|
+
}
|
|
282
|
+
if (lastTgMessageId === undefined) {
|
|
283
|
+
throw new Error("final carried no renderable content");
|
|
284
|
+
}
|
|
285
|
+
const row = {
|
|
286
|
+
threadId: record.threadId,
|
|
287
|
+
tgMessageId: lastTgMessageId,
|
|
288
|
+
role: record.role,
|
|
289
|
+
};
|
|
290
|
+
this.#mirrors.set(record.nativeMessageId, row);
|
|
291
|
+
await this.#store.set(MIRROR_PREFIX + record.nativeMessageId, row);
|
|
292
|
+
const remaining = (await this.#readOutbox()).filter((entry) => entry.id !== record.id);
|
|
293
|
+
await this.#writeOutbox(remaining);
|
|
294
|
+
return { delivered: true, tgMessageId: lastTgMessageId };
|
|
295
|
+
}
|
|
296
|
+
catch {
|
|
297
|
+
record.attempts += 1;
|
|
298
|
+
if (record.attempts === 1) {
|
|
299
|
+
this.#onNotice(record.threadId, "⚠️ delivery failed; the reply is queued and will retry");
|
|
300
|
+
}
|
|
301
|
+
const records = await this.#readOutbox();
|
|
302
|
+
const patched = records.map((entry) => (entry.id === record.id ? record : entry));
|
|
303
|
+
await this.#writeOutbox(patched);
|
|
304
|
+
return { delivered: false, tgMessageId: undefined };
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/** Boot drain: per-record FIFO delivery of finals still missing mirrors. */
|
|
308
|
+
async drainPendingFinals() {
|
|
309
|
+
const records = await this.#readOutbox();
|
|
310
|
+
let delivered = 0;
|
|
311
|
+
let skipped = 0;
|
|
312
|
+
for (const record of records) {
|
|
313
|
+
if (this.#mirrors.has(record.nativeMessageId)) {
|
|
314
|
+
skipped += 1;
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
const result = await this.#sendPending(record);
|
|
318
|
+
if (result.delivered)
|
|
319
|
+
delivered += 1;
|
|
320
|
+
else
|
|
321
|
+
skipped += 1;
|
|
322
|
+
}
|
|
323
|
+
return { delivered, skipped };
|
|
324
|
+
}
|
|
325
|
+
/** Open a streaming turn with a stable draft id. */
|
|
326
|
+
beginTurn(input) {
|
|
327
|
+
this.#streams.set(input.threadId, {
|
|
328
|
+
draftId: input.draftId,
|
|
329
|
+
turnStartMs: this.#clock.now(),
|
|
330
|
+
placeholderMessageId: undefined,
|
|
331
|
+
draftsDisabled: false,
|
|
332
|
+
lastEditMs: 0,
|
|
333
|
+
});
|
|
334
|
+
}
|
|
335
|
+
/** Stream one throttled text update; returns true when an update went out. */
|
|
336
|
+
async streamText(threadId, text) {
|
|
337
|
+
const state = this.#streams.get(threadId);
|
|
338
|
+
if (state === undefined)
|
|
339
|
+
return false;
|
|
340
|
+
const nowMs = this.#clock.now();
|
|
341
|
+
const delay = nextStreamDelayMs(nowMs - state.turnStartMs);
|
|
342
|
+
if (state.lastEditMs !== 0 && nowMs - state.lastEditMs < delay)
|
|
343
|
+
return false;
|
|
344
|
+
if (text.trim().length === 0)
|
|
345
|
+
return false;
|
|
346
|
+
if (!state.draftsDisabled) {
|
|
347
|
+
try {
|
|
348
|
+
await this.#transport.sendDraft({ threadId, draftId: state.draftId, text });
|
|
349
|
+
state.lastEditMs = nowMs;
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
catch (error) {
|
|
353
|
+
if (!(error instanceof TelegramUnsupportedError)) {
|
|
354
|
+
return false;
|
|
355
|
+
}
|
|
356
|
+
state.draftsDisabled = true;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
if (state.placeholderMessageId === undefined) {
|
|
360
|
+
const sent = await this.#transport.sendMessage({ threadId, text });
|
|
361
|
+
state.placeholderMessageId = sent.messageId;
|
|
362
|
+
state.lastEditMs = nowMs;
|
|
363
|
+
return true;
|
|
364
|
+
}
|
|
365
|
+
await this.#transport.editMessageText({ messageId: state.placeholderMessageId, text });
|
|
366
|
+
state.lastEditMs = nowMs;
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
/** Send a transient progress line or card without a mirror or outbox record; failures are contained. */
|
|
370
|
+
async sendTransient(input) {
|
|
371
|
+
try {
|
|
372
|
+
return (await this.#transport.sendMessage({ threadId: input.threadId, text: input.text }))
|
|
373
|
+
.messageId;
|
|
374
|
+
}
|
|
375
|
+
catch {
|
|
376
|
+
return undefined;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
/** Close a streaming turn; the final send dismisses the draft, the fallback placeholder is removed best-effort. */
|
|
380
|
+
async endTurn(threadId) {
|
|
381
|
+
const state = this.#streams.get(threadId);
|
|
382
|
+
if (state === undefined)
|
|
383
|
+
return;
|
|
384
|
+
if (state.placeholderMessageId !== undefined) {
|
|
385
|
+
try {
|
|
386
|
+
await this.#transport.deleteMessage(state.placeholderMessageId);
|
|
387
|
+
}
|
|
388
|
+
catch {
|
|
389
|
+
// Best-effort removal only.
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
this.#streams.delete(threadId);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
//# sourceMappingURL=delivery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"delivery.js","sourceRoot":"","sources":["../../../src/plugins/telegram/delivery.ts"],"names":[],"mappings":"AAAA,yCAAyC;AACzC,iBAAiB;AACjB,wBAAwB;AACxB,iVAAiV;AACjV,iSAAiS;AACjS,iHAAiH;AACjH,+FAA+F;AAC/F,kBAAkB;AAClB,sBAAsB;AACtB,sBAAsB;AACtB,EAAE;AACF,mBAAmB;AACnB,gEAAgE;AA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,KAAK,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC1C,IAAI,KAAK,KAAK,SAAS;YAAE,OAAO;QAChC,IAAI,KAAK,CAAC,oBAAoB,KAAK,SAAS,EAAE,CAAC;YAC7C,IAAI,CAAC;gBACH,MAAM,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAC;YAClE,CAAC;YAAC,MAAM,CAAC;gBACP,4BAA4B;YAC9B,CAAC;QACH,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACjC,CAAC;CACF"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { TelegramMessage, TelegramTransport, TelegramUpdate } from "./bot-api.js";
|
|
2
|
+
import type { TelegramStore } from "./topology.js";
|
|
3
|
+
/** Storage key holding the last acknowledged update id. */
|
|
4
|
+
export declare const OFFSET_KEY = "telegram/v1/offset";
|
|
5
|
+
/** Long-poll timeout forwarded to getUpdates. */
|
|
6
|
+
export declare const POLLING_TIMEOUT_SEC = 25;
|
|
7
|
+
/** Bounded error backoff schedule; the last entry is the cap. */
|
|
8
|
+
export declare const GATEWAY_BACKOFF_MS: readonly [1000, 2000, 5000, 15000, 30000];
|
|
9
|
+
/** Updates older than this grace at dispatch time are dropped as stale. */
|
|
10
|
+
export declare const STALE_UPDATE_GRACE_SEC = 60;
|
|
11
|
+
/** Dispatch input for one routed owner message or album. */
|
|
12
|
+
export interface TelegramOwnerMessage {
|
|
13
|
+
readonly threadId: number;
|
|
14
|
+
readonly text: string | undefined;
|
|
15
|
+
readonly attachments: readonly {
|
|
16
|
+
readonly fileId: string;
|
|
17
|
+
readonly filename: string | undefined;
|
|
18
|
+
readonly mimeType: string | undefined;
|
|
19
|
+
}[];
|
|
20
|
+
}
|
|
21
|
+
/** Pure staleness predicate over the message date and now. */
|
|
22
|
+
export declare function isStaleUpdate(message: TelegramMessage, nowMs: number): boolean;
|
|
23
|
+
interface BatchEntry {
|
|
24
|
+
readonly updateId: number;
|
|
25
|
+
readonly kind: "message" | "callback" | "album" | "ignored";
|
|
26
|
+
readonly message?: TelegramMessage;
|
|
27
|
+
readonly album?: readonly TelegramMessage[];
|
|
28
|
+
readonly update?: TelegramUpdate;
|
|
29
|
+
}
|
|
30
|
+
/** Pure batch grouper: consecutive photos sharing a media_group id form one dispatch entry. */
|
|
31
|
+
export declare function groupAlbums(updates: readonly TelegramUpdate[]): readonly BatchEntry[];
|
|
32
|
+
/**
|
|
33
|
+
* Long-polling gateway. The offset advances only after an update's dispatch
|
|
34
|
+
* resolves, stale messages queued while the bot was offline are dropped, and
|
|
35
|
+
* every dispatch failure is contained so the loop continues with bounded
|
|
36
|
+
* backoff. Exactly one gateway per bot token may run.
|
|
37
|
+
*/
|
|
38
|
+
export declare class TelegramGateway {
|
|
39
|
+
#private;
|
|
40
|
+
constructor(deps: {
|
|
41
|
+
transport: TelegramTransport;
|
|
42
|
+
store: TelegramStore;
|
|
43
|
+
ownerIds: readonly number[];
|
|
44
|
+
dispatch: {
|
|
45
|
+
handleMessage(input: TelegramOwnerMessage): Promise<void>;
|
|
46
|
+
handleCallback(update: TelegramUpdate): Promise<void>;
|
|
47
|
+
};
|
|
48
|
+
clock: {
|
|
49
|
+
now(): number;
|
|
50
|
+
};
|
|
51
|
+
log?: (level: "warn", message: string) => void;
|
|
52
|
+
sleep?: (ms: number, signal: AbortSignal) => Promise<void>;
|
|
53
|
+
});
|
|
54
|
+
/** Load the persisted offset and start polling; resolves once the loop exits. */
|
|
55
|
+
start(): Promise<void>;
|
|
56
|
+
/** Abort the in-flight poll and settle the loop. */
|
|
57
|
+
stop(): Promise<void>;
|
|
58
|
+
/** Exposed for tests: dispatch one raw update entry without polling. */
|
|
59
|
+
dispatchUpdate(update: TelegramUpdate): Promise<void>;
|
|
60
|
+
}
|
|
61
|
+
export {};
|