@leitwerk-dev/telegram 0.1.9 → 0.2.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/README.md +9 -1
- package/dist/actions.d.ts +1 -0
- package/dist/actions.d.ts.map +1 -1
- package/dist/actor.d.ts +1 -1
- package/dist/actor.d.ts.map +1 -1
- package/dist/bridge.d.ts +13 -10
- package/dist/bridge.d.ts.map +1 -1
- package/dist/config.d.ts +18 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/fake-telegram-client.d.ts +60 -0
- package/dist/fake-telegram-client.d.ts.map +1 -1
- package/dist/form-session.d.ts +112 -19
- package/dist/form-session.d.ts.map +1 -1
- package/dist/grammy-telegram-client.d.ts +17 -41
- package/dist/grammy-telegram-client.d.ts.map +1 -1
- package/dist/index.d.ts +1 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3580 -52
- package/dist/index.js.map +1 -1
- package/dist/launch-session.d.ts +84 -1
- package/dist/launch-session.d.ts.map +1 -1
- package/dist/markdown-html.d.ts +19 -0
- package/dist/markdown-html.d.ts.map +1 -1
- package/dist/process-thread-store.d.ts +8 -0
- package/dist/process-thread-store.d.ts.map +1 -1
- package/dist/topic-title.d.ts +4 -0
- package/dist/topic-title.d.ts.map +1 -1
- package/dist/types.d.ts +99 -6
- package/dist/types.d.ts.map +1 -1
- package/package.json +7 -7
- package/src/actions.ts +14 -5
- package/src/actor.ts +1 -1
- package/src/bridge.ts +123 -303
- package/src/config.ts +37 -9
- package/src/fake-telegram-client.ts +92 -9
- package/src/form-session.ts +153 -42
- package/src/grammy-telegram-client.ts +23 -43
- package/src/index.ts +7 -12
- package/src/launch-session.ts +165 -70
- package/src/markdown-html.ts +35 -4
- package/src/process-thread-store.ts +8 -0
- package/src/topic-title.ts +8 -1
- package/src/types.ts +113 -10
- package/dist/actions.js +0 -32
- package/dist/actions.js.map +0 -1
- package/dist/actor.js +0 -7
- package/dist/actor.js.map +0 -1
- package/dist/bridge.js +0 -2032
- package/dist/bridge.js.map +0 -1
- package/dist/config.js +0 -70
- package/dist/config.js.map +0 -1
- package/dist/fake-telegram-client.js +0 -107
- package/dist/fake-telegram-client.js.map +0 -1
- package/dist/form-session.js +0 -218
- package/dist/form-session.js.map +0 -1
- package/dist/grammy-telegram-client.js +0 -195
- package/dist/grammy-telegram-client.js.map +0 -1
- package/dist/launch-session.js +0 -435
- package/dist/launch-session.js.map +0 -1
- package/dist/markdown-html.js +0 -259
- package/dist/markdown-html.js.map +0 -1
- package/dist/mermaid-image.js +0 -99
- package/dist/mermaid-image.js.map +0 -1
- package/dist/process-feed.js +0 -165
- package/dist/process-feed.js.map +0 -1
- package/dist/process-thread-store.js +0 -74
- package/dist/process-thread-store.js.map +0 -1
- package/dist/topic-title.js +0 -29
- package/dist/topic-title.js.map +0 -1
- package/dist/types.js +0 -19
- package/dist/types.js.map +0 -1
package/src/bridge.ts
CHANGED
|
@@ -5,6 +5,7 @@ import type {
|
|
|
5
5
|
ProcessTurnRecord,
|
|
6
6
|
ProcessTurnRecordPathType,
|
|
7
7
|
} from "@leitwerk-dev/domain";
|
|
8
|
+
import { isUnknownRecord as isRecord } from "@leitwerk-dev/domain";
|
|
8
9
|
import type {
|
|
9
10
|
CoreServerSetupDeps,
|
|
10
11
|
FormDefinition,
|
|
@@ -19,7 +20,7 @@ import type {
|
|
|
19
20
|
ServerExtensionLogger,
|
|
20
21
|
UiLauncherSummary,
|
|
21
22
|
} from "@leitwerk-dev/process-sdk";
|
|
22
|
-
import { buildActionKeyboard, continuableFailedTurnRecordId } from "./actions.js";
|
|
23
|
+
import { buildActionKeyboard, continuableFailedTurnRecordId, rows } from "./actions.js";
|
|
23
24
|
import { TELEGRAM_ACTOR } from "./actor.js";
|
|
24
25
|
import type { TelegramExtensionConfig } from "./config.js";
|
|
25
26
|
import {
|
|
@@ -159,12 +160,6 @@ function commandArgs(text: string): string {
|
|
|
159
160
|
.trim();
|
|
160
161
|
}
|
|
161
162
|
|
|
162
|
-
function rows<T>(items: readonly T[], size: number): T[][] {
|
|
163
|
-
return Array.from({ length: Math.ceil(items.length / size) }, (_, index) => [
|
|
164
|
-
...items.slice(index * size, index * size + size),
|
|
165
|
-
]);
|
|
166
|
-
}
|
|
167
|
-
|
|
168
163
|
function messageTarget(chatId: string, messageThreadId: number | undefined) {
|
|
169
164
|
return { chatId, ...(messageThreadId !== undefined ? { messageThreadId } : {}) };
|
|
170
165
|
}
|
|
@@ -179,89 +174,39 @@ function launchSessionKey(input: {
|
|
|
179
174
|
: null;
|
|
180
175
|
}
|
|
181
176
|
|
|
182
|
-
function splitScopedCallbackPayload(payload: string): { instanceId: string; value: string } | null {
|
|
183
|
-
const separator = payload.indexOf(":");
|
|
184
|
-
if (separator <= 0 || separator === payload.length - 1) return null;
|
|
185
|
-
return {
|
|
186
|
-
instanceId: payload.slice(0, separator),
|
|
187
|
-
value: payload.slice(separator + 1),
|
|
188
|
-
};
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
function parseTripleCallbackPayload(
|
|
192
|
-
payload: string,
|
|
193
|
-
): { instanceId: string; sessionId: string; value: string } | null {
|
|
194
|
-
const parts = payload.split(":");
|
|
195
|
-
const [instanceId, sessionId, ...rest] = parts;
|
|
196
|
-
if (!instanceId || !sessionId) return null;
|
|
197
|
-
return { instanceId, sessionId, value: rest.join(":") };
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
function parseActionFormSkipCallbackPayload(
|
|
201
|
-
payload: string,
|
|
202
|
-
): Extract<TelegramCallbackCommand, { kind: "action_form_skip" }> | null {
|
|
203
|
-
const parsed = parseTripleCallbackPayload(payload);
|
|
204
|
-
if (!parsed) return null;
|
|
205
|
-
return {
|
|
206
|
-
kind: "action_form_skip",
|
|
207
|
-
instanceId: parsed.instanceId,
|
|
208
|
-
sessionId: parsed.sessionId,
|
|
209
|
-
fieldId: parsed.value,
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
|
|
213
177
|
function parseCallbackCommand(data: string): TelegramCallbackCommand | null {
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
return
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
if (data.startsWith("r:")) {
|
|
230
|
-
const instanceId = data.slice("r:".length);
|
|
231
|
-
return instanceId ? { kind: "retry", instanceId } : null;
|
|
178
|
+
const separator = data.indexOf(":");
|
|
179
|
+
if (separator < 0) return null;
|
|
180
|
+
const prefix = data.slice(0, separator);
|
|
181
|
+
const payload = data.slice(separator + 1);
|
|
182
|
+
if (prefix === "r" || prefix === "c") {
|
|
183
|
+
return payload ? { kind: prefix === "r" ? "retry" : "continue", instanceId: payload } : null;
|
|
184
|
+
}
|
|
185
|
+
if (prefix === "a") {
|
|
186
|
+
const separator = payload.indexOf(":");
|
|
187
|
+
if (separator <= 0 || separator === payload.length - 1) return null;
|
|
188
|
+
return {
|
|
189
|
+
kind: "process_action",
|
|
190
|
+
instanceId: payload.slice(0, separator),
|
|
191
|
+
actionId: payload.slice(separator + 1),
|
|
192
|
+
};
|
|
232
193
|
}
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
194
|
+
const [instanceId, sessionId, ...rest] = payload.split(":");
|
|
195
|
+
if (!instanceId || !sessionId) return null;
|
|
196
|
+
switch (prefix) {
|
|
197
|
+
case "fs":
|
|
198
|
+
return { kind: "action_form_skip", instanceId, sessionId, fieldId: rest.join(":") };
|
|
199
|
+
case "amk":
|
|
200
|
+
return { kind: "action_model_skip", instanceId, sessionId };
|
|
201
|
+
case "ams": {
|
|
202
|
+
const profileIndex = Number(rest.join(":"));
|
|
203
|
+
return Number.isInteger(profileIndex) && profileIndex >= 1
|
|
204
|
+
? { kind: "action_model_select", instanceId, sessionId, profileIndex }
|
|
205
|
+
: null;
|
|
206
|
+
}
|
|
207
|
+
default:
|
|
208
|
+
return null;
|
|
236
209
|
}
|
|
237
|
-
return null;
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
function parseActionModelSelectCallbackPayload(
|
|
241
|
-
payload: string,
|
|
242
|
-
): Extract<TelegramCallbackCommand, { kind: "action_model_select" }> | null {
|
|
243
|
-
const parsed = parseTripleCallbackPayload(payload);
|
|
244
|
-
if (!parsed) return null;
|
|
245
|
-
const profileIndex = Number(parsed.value);
|
|
246
|
-
if (!Number.isInteger(profileIndex) || profileIndex < 1) return null;
|
|
247
|
-
return {
|
|
248
|
-
kind: "action_model_select",
|
|
249
|
-
instanceId: parsed.instanceId,
|
|
250
|
-
sessionId: parsed.sessionId,
|
|
251
|
-
profileIndex,
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
function parseActionModelSkipCallbackPayload(
|
|
256
|
-
payload: string,
|
|
257
|
-
): Extract<TelegramCallbackCommand, { kind: "action_model_skip" }> | null {
|
|
258
|
-
const parsed = parseTripleCallbackPayload(payload);
|
|
259
|
-
if (!parsed) return null;
|
|
260
|
-
return {
|
|
261
|
-
kind: "action_model_skip",
|
|
262
|
-
instanceId: parsed.instanceId,
|
|
263
|
-
sessionId: parsed.sessionId,
|
|
264
|
-
};
|
|
265
210
|
}
|
|
266
211
|
|
|
267
212
|
function resolveProfileByTextOrIndex(
|
|
@@ -283,14 +228,9 @@ function resolveModelProfileFromSession(
|
|
|
283
228
|
session: PendingTelegramSession | undefined,
|
|
284
229
|
profileIndex: number,
|
|
285
230
|
): ModelProfileOptionSummaryLike | null {
|
|
286
|
-
|
|
287
|
-
session?.
|
|
288
|
-
|
|
289
|
-
: session?.kind === "recovery_model"
|
|
290
|
-
? session.profiles
|
|
291
|
-
: null;
|
|
292
|
-
if (!profiles) return null;
|
|
293
|
-
return profiles[profileIndex - 1] ?? null;
|
|
231
|
+
return session?.kind === "action_model" || session?.kind === "recovery_model"
|
|
232
|
+
? (session.profiles?.[profileIndex - 1] ?? null)
|
|
233
|
+
: null;
|
|
294
234
|
}
|
|
295
235
|
|
|
296
236
|
function processCanAcceptFreeText(process: ProcessInstance): boolean {
|
|
@@ -336,10 +276,6 @@ function shouldDeferCompletedLifecycleUntilTurnOutcome(input: {
|
|
|
336
276
|
);
|
|
337
277
|
}
|
|
338
278
|
|
|
339
|
-
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
340
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
341
|
-
}
|
|
342
|
-
|
|
343
279
|
function readClaimedLaunchThread(
|
|
344
280
|
process: ProcessInstance,
|
|
345
281
|
expectedChatId: string,
|
|
@@ -357,27 +293,7 @@ function createActionFormSessionId(): string {
|
|
|
357
293
|
return randomBytes(4).toString("hex");
|
|
358
294
|
}
|
|
359
295
|
|
|
360
|
-
|
|
361
|
-
launchPlan: ProcessLaunchPlan,
|
|
362
|
-
thread: Pick<TelegramProcessThread, "mode" | "chatId" | "messageThreadId">,
|
|
363
|
-
): ProcessLaunchPlan {
|
|
364
|
-
const metadata = launchPlan.processInput.metadata ?? {};
|
|
365
|
-
const telegram = isRecord(metadata.telegram) ? metadata.telegram : {};
|
|
366
|
-
return {
|
|
367
|
-
...launchPlan,
|
|
368
|
-
processInput: {
|
|
369
|
-
...launchPlan.processInput,
|
|
370
|
-
metadata: {
|
|
371
|
-
...metadata,
|
|
372
|
-
telegram: {
|
|
373
|
-
...telegram,
|
|
374
|
-
launchThread: { ...thread },
|
|
375
|
-
},
|
|
376
|
-
},
|
|
377
|
-
},
|
|
378
|
-
};
|
|
379
|
-
}
|
|
380
|
-
|
|
296
|
+
/** @internal */
|
|
381
297
|
export class TelegramBridge {
|
|
382
298
|
private readonly store: ProcessThreadStore;
|
|
383
299
|
private readonly sessions = new Map<string, PendingTelegramSession>();
|
|
@@ -396,11 +312,16 @@ export class TelegramBridge {
|
|
|
396
312
|
{ pathType: ProcessTurnRecordPathType; turnId: string }
|
|
397
313
|
>();
|
|
398
314
|
|
|
315
|
+
/** @internal */
|
|
399
316
|
constructor(
|
|
400
317
|
private readonly input: {
|
|
318
|
+
/** @internal */
|
|
401
319
|
config: TelegramExtensionConfig;
|
|
320
|
+
/** @internal */
|
|
402
321
|
deps: CoreServerSetupDeps;
|
|
322
|
+
/** @internal */
|
|
403
323
|
client: TelegramClient;
|
|
324
|
+
/** @internal */
|
|
404
325
|
logger?: ServerExtensionLogger;
|
|
405
326
|
},
|
|
406
327
|
) {
|
|
@@ -412,6 +333,7 @@ export class TelegramBridge {
|
|
|
412
333
|
input.client.onForumTopicClosed((update) => this.handleForumTopicClosed(update));
|
|
413
334
|
}
|
|
414
335
|
|
|
336
|
+
/** @internal */
|
|
415
337
|
async start(): Promise<void> {
|
|
416
338
|
for (const instanceId of this.store.rebuildFromEvents()) {
|
|
417
339
|
this.threadEligibleInstanceIds.add(instanceId);
|
|
@@ -446,11 +368,14 @@ export class TelegramBridge {
|
|
|
446
368
|
}
|
|
447
369
|
}
|
|
448
370
|
|
|
371
|
+
/** @internal */
|
|
449
372
|
async stop(): Promise<void> {
|
|
450
373
|
await this.input.client.stop();
|
|
451
374
|
}
|
|
452
375
|
|
|
376
|
+
/** @internal */
|
|
453
377
|
register(events: {
|
|
378
|
+
/** @internal */
|
|
454
379
|
on<K extends keyof ServerExtensionEventMap & string>(
|
|
455
380
|
event: K,
|
|
456
381
|
handler: (payload: ServerExtensionEventMap[K]) => void | Promise<void>,
|
|
@@ -1740,20 +1665,26 @@ export class TelegramBridge {
|
|
|
1740
1665
|
);
|
|
1741
1666
|
return;
|
|
1742
1667
|
}
|
|
1743
|
-
|
|
1744
|
-
mode: "forum_topic",
|
|
1745
|
-
chatId,
|
|
1746
|
-
messageThreadId,
|
|
1747
|
-
});
|
|
1748
|
-
let result: Awaited<
|
|
1749
|
-
ReturnType<CoreServerSetupDeps["processLaunches"]["createProcessFromLaunchPlan"]>
|
|
1750
|
-
>;
|
|
1668
|
+
let result: Awaited<ReturnType<CoreServerSetupDeps["launchRuns"]["startProgrammatic"]>>;
|
|
1751
1669
|
try {
|
|
1752
|
-
result = await this.input.deps.
|
|
1753
|
-
|
|
1754
|
-
|
|
1670
|
+
result = await this.input.deps.launchRuns.startProgrammatic(
|
|
1671
|
+
{
|
|
1672
|
+
launcherId: launcher.id,
|
|
1673
|
+
launcherInput: buildLaunchInput(session),
|
|
1674
|
+
...(session.modelConfigTouched ? { modelConfig: buildLaunchModelConfig(session) } : {}),
|
|
1675
|
+
processMetadata: {
|
|
1676
|
+
telegram: {
|
|
1677
|
+
launchThread: { mode: "forum_topic", chatId, messageThreadId },
|
|
1678
|
+
},
|
|
1679
|
+
},
|
|
1680
|
+
},
|
|
1681
|
+
{
|
|
1682
|
+
actor: TELEGRAM_ACTOR,
|
|
1683
|
+
idempotencyKey: `telegram:${sessionKey}:${session.expiresAt}`,
|
|
1684
|
+
},
|
|
1685
|
+
);
|
|
1755
1686
|
} catch (error) {
|
|
1756
|
-
this.logError(error, "Failed to
|
|
1687
|
+
this.logError(error, "Failed to admit Telegram-launched process");
|
|
1757
1688
|
await this.sendHtmlToChat(
|
|
1758
1689
|
chatId,
|
|
1759
1690
|
messageThreadId,
|
|
@@ -1761,31 +1692,22 @@ export class TelegramBridge {
|
|
|
1761
1692
|
);
|
|
1762
1693
|
return;
|
|
1763
1694
|
}
|
|
1764
|
-
if (result.
|
|
1695
|
+
if (result.process) {
|
|
1765
1696
|
this.recordLaunchRecentValuesBestEffort(session);
|
|
1766
1697
|
this.launchSessions.delete(sessionKey);
|
|
1767
1698
|
await this.sendHtmlToChat(
|
|
1768
1699
|
chatId,
|
|
1769
1700
|
messageThreadId,
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
}
|
|
1774
|
-
const process = "process" in result ? result.process : null;
|
|
1775
|
-
if (process) {
|
|
1776
|
-
this.recordLaunchRecentValuesBestEffort(session);
|
|
1777
|
-
this.launchSessions.delete(sessionKey);
|
|
1778
|
-
await this.sendHtmlToChat(
|
|
1779
|
-
chatId,
|
|
1780
|
-
messageThreadId,
|
|
1781
|
-
`⚠️ Process was created but launch had a follow-up problem. Web UI: ${escapeHtml(`${this.input.deps.serverBaseUrl.replace(/\/$/, "")}/processes/${process.id}`)}`,
|
|
1701
|
+
result.error
|
|
1702
|
+
? `⚠️ Process was created but launch had a follow-up problem. Web UI: ${escapeHtml(`${this.input.deps.serverBaseUrl.replace(/\/$/, "")}/processes/${result.process.id}`)}`
|
|
1703
|
+
: `✅ Process launched. Web UI: ${escapeHtml(`${this.input.deps.serverBaseUrl.replace(/\/$/, "")}/processes/${result.process.id}`)}`,
|
|
1782
1704
|
);
|
|
1783
1705
|
return;
|
|
1784
1706
|
}
|
|
1785
1707
|
await this.sendHtmlToChat(
|
|
1786
1708
|
chatId,
|
|
1787
1709
|
messageThreadId,
|
|
1788
|
-
`⚠️ ${escapeHtml(
|
|
1710
|
+
`⚠️ ${escapeHtml(result.error ?? "Launch failed")}`,
|
|
1789
1711
|
);
|
|
1790
1712
|
}
|
|
1791
1713
|
|
|
@@ -2107,13 +2029,9 @@ export class TelegramBridge {
|
|
|
2107
2029
|
return;
|
|
2108
2030
|
}
|
|
2109
2031
|
if (modelSession?.kind === "recovery_model") {
|
|
2110
|
-
await this.
|
|
2111
|
-
|
|
2112
|
-
|
|
2113
|
-
modelProfile.id,
|
|
2114
|
-
modelSession,
|
|
2115
|
-
thread,
|
|
2116
|
-
);
|
|
2032
|
+
await this.executeRecoveryModelSession(key, modelSession, thread, {
|
|
2033
|
+
nextTurnModelProfileId: modelProfile.id,
|
|
2034
|
+
});
|
|
2117
2035
|
return;
|
|
2118
2036
|
}
|
|
2119
2037
|
await this.handleActionModelSelect(key, command.sessionId, modelProfile.id, thread);
|
|
@@ -2122,7 +2040,7 @@ export class TelegramBridge {
|
|
|
2122
2040
|
case "action_model_skip": {
|
|
2123
2041
|
const modelSession = this.sessions.get(key);
|
|
2124
2042
|
if (modelSession?.kind === "recovery_model") {
|
|
2125
|
-
await this.
|
|
2043
|
+
await this.executeRecoveryModelSession(key, modelSession, thread);
|
|
2126
2044
|
return;
|
|
2127
2045
|
}
|
|
2128
2046
|
await this.handleActionModelSkip(key, command.sessionId, thread);
|
|
@@ -2185,21 +2103,13 @@ export class TelegramBridge {
|
|
|
2185
2103
|
await this.sendHtml(thread, escapeHtml(buildQuestionPrompt(session)));
|
|
2186
2104
|
return true;
|
|
2187
2105
|
}
|
|
2188
|
-
if (session.kind === "action_model") {
|
|
2189
|
-
await this.
|
|
2190
|
-
return true;
|
|
2191
|
-
}
|
|
2192
|
-
if (session.kind === "recovery_model") {
|
|
2193
|
-
await this.sendRecoveryModelPrompt(thread, session);
|
|
2106
|
+
if (session.kind === "action_model" || session.kind === "recovery_model") {
|
|
2107
|
+
await this.sendModelPrompt(thread, session);
|
|
2194
2108
|
return true;
|
|
2195
2109
|
}
|
|
2196
2110
|
const field = currentField(session);
|
|
2197
2111
|
if (!field) return false;
|
|
2198
|
-
|
|
2199
|
-
await this.sendActionFormFieldPrompt(thread, session, field);
|
|
2200
|
-
return true;
|
|
2201
|
-
}
|
|
2202
|
-
await this.sendHtml(thread, escapeHtml(buildFieldPrompt(field)));
|
|
2112
|
+
await this.sendActionFormFieldPrompt(thread, session, field);
|
|
2203
2113
|
return true;
|
|
2204
2114
|
}
|
|
2205
2115
|
|
|
@@ -2251,7 +2161,7 @@ export class TelegramBridge {
|
|
|
2251
2161
|
profiles,
|
|
2252
2162
|
});
|
|
2253
2163
|
this.sessions.set(input.sessionKey, session);
|
|
2254
|
-
await this.
|
|
2164
|
+
await this.sendModelPrompt(input.thread, session);
|
|
2255
2165
|
} catch (error) {
|
|
2256
2166
|
this.logError(error, "Action model preview failed; falling back to direct execution");
|
|
2257
2167
|
return fallback();
|
|
@@ -2331,43 +2241,51 @@ export class TelegramBridge {
|
|
|
2331
2241
|
await this.executeActionModelSession(session, thread);
|
|
2332
2242
|
}
|
|
2333
2243
|
|
|
2334
|
-
private async
|
|
2244
|
+
private async sendModelPrompt(
|
|
2335
2245
|
thread: TelegramProcessThread,
|
|
2336
|
-
session: PendingActionModelSession,
|
|
2246
|
+
session: PendingActionModelSession | PendingRecoveryModelSession,
|
|
2247
|
+
error = "",
|
|
2337
2248
|
): Promise<void> {
|
|
2249
|
+
const prompt =
|
|
2250
|
+
session.kind === "action_model"
|
|
2251
|
+
? buildActionModelPrompt({ session })
|
|
2252
|
+
: buildRecoveryModelPrompt({ session });
|
|
2338
2253
|
await this.sendHtml(
|
|
2339
2254
|
thread,
|
|
2340
|
-
escapeHtml(
|
|
2341
|
-
this.
|
|
2255
|
+
escapeHtml(`${error}${prompt}`),
|
|
2256
|
+
this.buildModelReplyMarkup(session),
|
|
2342
2257
|
);
|
|
2343
2258
|
}
|
|
2344
2259
|
|
|
2345
|
-
private
|
|
2346
|
-
session: PendingActionModelSession,
|
|
2347
|
-
): TelegramInlineKeyboard
|
|
2260
|
+
private buildModelReplyMarkup(
|
|
2261
|
+
session: PendingActionModelSession | PendingRecoveryModelSession,
|
|
2262
|
+
): TelegramInlineKeyboard {
|
|
2348
2263
|
const buttons: Array<{ text: string; callbackData: string }> = [];
|
|
2349
2264
|
let index = 0;
|
|
2350
2265
|
for (const profile of session.profiles.slice(0, 20)) {
|
|
2351
2266
|
index += 1;
|
|
2352
|
-
const switchesModel =
|
|
2353
|
-
session
|
|
2354
|
-
|
|
2355
|
-
|
|
2267
|
+
const switchesModel =
|
|
2268
|
+
session.kind === "action_model" &&
|
|
2269
|
+
buildActionModelSwitchWarning({
|
|
2270
|
+
session,
|
|
2271
|
+
effectiveModelProfileId: profile.id,
|
|
2272
|
+
});
|
|
2356
2273
|
buttons.push({
|
|
2357
2274
|
text: `${switchesModel ? "⚠ " : ""}${profile.id}`,
|
|
2358
2275
|
callbackData: `ams:${session.instanceId}:${session.sessionId}:${index}`,
|
|
2359
2276
|
});
|
|
2360
2277
|
}
|
|
2361
|
-
const resolved = session.preview.resolvedModel;
|
|
2278
|
+
const resolved = session.kind === "action_model" ? session.preview.resolvedModel : null;
|
|
2362
2279
|
const resolvedProfileId = resolved?.status === "resolved" ? resolved.modelProfileId : null;
|
|
2363
|
-
const keepingCurrentSwitchesModel =
|
|
2364
|
-
|
|
2365
|
-
|
|
2280
|
+
const keepingCurrentSwitchesModel =
|
|
2281
|
+
session.kind === "action_model" && resolvedProfileId
|
|
2282
|
+
? buildActionModelSwitchWarning({ session, effectiveModelProfileId: resolvedProfileId })
|
|
2283
|
+
: null;
|
|
2366
2284
|
buttons.push({
|
|
2367
2285
|
text: `${keepingCurrentSwitchesModel ? "⚠ " : ""}Keep current`,
|
|
2368
2286
|
callbackData: `amk:${session.instanceId}:${session.sessionId}`,
|
|
2369
2287
|
});
|
|
2370
|
-
return
|
|
2288
|
+
return { inlineKeyboard: rows(buttons, 1) };
|
|
2371
2289
|
}
|
|
2372
2290
|
|
|
2373
2291
|
private async applyActionFormStep(
|
|
@@ -2400,23 +2318,6 @@ export class TelegramBridge {
|
|
|
2400
2318
|
});
|
|
2401
2319
|
}
|
|
2402
2320
|
|
|
2403
|
-
private async applyActionFormText(
|
|
2404
|
-
key: string,
|
|
2405
|
-
session: PendingActionFormSession,
|
|
2406
|
-
text: string,
|
|
2407
|
-
thread: TelegramProcessThread,
|
|
2408
|
-
): Promise<void> {
|
|
2409
|
-
await this.applyActionFormStep(key, session, applyFormText(session, text), thread);
|
|
2410
|
-
}
|
|
2411
|
-
|
|
2412
|
-
private async applyActionFormSkip(
|
|
2413
|
-
key: string,
|
|
2414
|
-
session: PendingActionFormSession,
|
|
2415
|
-
thread: TelegramProcessThread,
|
|
2416
|
-
): Promise<void> {
|
|
2417
|
-
await this.applyActionFormStep(key, session, applyFormSkip(session), thread);
|
|
2418
|
-
}
|
|
2419
|
-
|
|
2420
2321
|
private async skipCurrentActionFormField(
|
|
2421
2322
|
key: string,
|
|
2422
2323
|
sessionId: string,
|
|
@@ -2450,7 +2351,7 @@ export class TelegramBridge {
|
|
|
2450
2351
|
);
|
|
2451
2352
|
return;
|
|
2452
2353
|
}
|
|
2453
|
-
await this.
|
|
2354
|
+
await this.applyActionFormStep(key, session, applyFormSkip(session), thread);
|
|
2454
2355
|
}
|
|
2455
2356
|
|
|
2456
2357
|
private async handleSessionText(
|
|
@@ -2509,22 +2410,13 @@ export class TelegramBridge {
|
|
|
2509
2410
|
);
|
|
2510
2411
|
return;
|
|
2511
2412
|
}
|
|
2512
|
-
if (session.kind === "action_model") {
|
|
2413
|
+
if (session.kind === "action_model" || session.kind === "recovery_model") {
|
|
2513
2414
|
if (name === "skip") {
|
|
2514
|
-
|
|
2515
|
-
|
|
2516
|
-
|
|
2517
|
-
|
|
2518
|
-
|
|
2519
|
-
await this.sendHtml(thread, "Cancelled.");
|
|
2520
|
-
return;
|
|
2521
|
-
}
|
|
2522
|
-
await this.handleActionModelText(key, session, text, thread);
|
|
2523
|
-
return;
|
|
2524
|
-
}
|
|
2525
|
-
if (session.kind === "recovery_model") {
|
|
2526
|
-
if (name === "skip") {
|
|
2527
|
-
await this.handleRecoveryModelSkip(key, session.sessionId, session, thread);
|
|
2415
|
+
if (session.kind === "action_model") {
|
|
2416
|
+
await this.handleActionModelSkip(key, session.sessionId, thread);
|
|
2417
|
+
} else {
|
|
2418
|
+
await this.executeRecoveryModelSession(key, session, thread);
|
|
2419
|
+
}
|
|
2528
2420
|
return;
|
|
2529
2421
|
}
|
|
2530
2422
|
if (name === "cancel") {
|
|
@@ -2532,7 +2424,7 @@ export class TelegramBridge {
|
|
|
2532
2424
|
await this.sendHtml(thread, "Cancelled.");
|
|
2533
2425
|
return;
|
|
2534
2426
|
}
|
|
2535
|
-
await this.
|
|
2427
|
+
await this.handleModelText(key, session, text, thread);
|
|
2536
2428
|
return;
|
|
2537
2429
|
}
|
|
2538
2430
|
if (name === "skip") {
|
|
@@ -2548,31 +2440,33 @@ export class TelegramBridge {
|
|
|
2548
2440
|
}
|
|
2549
2441
|
return;
|
|
2550
2442
|
}
|
|
2551
|
-
await this.
|
|
2443
|
+
await this.applyActionFormStep(key, session, applyFormSkip(session), thread);
|
|
2552
2444
|
return;
|
|
2553
2445
|
}
|
|
2554
|
-
await this.
|
|
2446
|
+
await this.applyActionFormStep(key, session, applyFormText(session, text), thread);
|
|
2555
2447
|
}
|
|
2556
2448
|
|
|
2557
|
-
private async
|
|
2449
|
+
private async handleModelText(
|
|
2558
2450
|
sessionKey: string,
|
|
2559
|
-
session: PendingActionModelSession,
|
|
2451
|
+
session: PendingActionModelSession | PendingRecoveryModelSession,
|
|
2560
2452
|
text: string,
|
|
2561
2453
|
thread: TelegramProcessThread,
|
|
2562
2454
|
): Promise<void> {
|
|
2563
2455
|
const profile = resolveProfileByTextOrIndex(text.trim(), session.profiles);
|
|
2564
|
-
if (profile) {
|
|
2456
|
+
if (!profile) {
|
|
2457
|
+
await this.sendModelPrompt(thread, session, "Model must match an available profile.\n");
|
|
2458
|
+
return;
|
|
2459
|
+
}
|
|
2460
|
+
if (session.kind === "action_model") {
|
|
2565
2461
|
this.sessions.delete(sessionKey);
|
|
2566
2462
|
await this.sendActionModelSwitchWarning(session, profile.id, thread);
|
|
2567
2463
|
return this.executeActionModelSession(session, thread, {
|
|
2568
2464
|
nextTurnModelProfileId: profile.id,
|
|
2569
2465
|
});
|
|
2570
2466
|
}
|
|
2571
|
-
await this.
|
|
2572
|
-
|
|
2573
|
-
|
|
2574
|
-
this.buildActionModelReplyMarkup(session),
|
|
2575
|
-
);
|
|
2467
|
+
await this.executeRecoveryModelSession(sessionKey, session, thread, {
|
|
2468
|
+
nextTurnModelProfileId: profile.id,
|
|
2469
|
+
});
|
|
2576
2470
|
}
|
|
2577
2471
|
|
|
2578
2472
|
private async maybeSelectRecoveryModel(input: {
|
|
@@ -2595,64 +2489,20 @@ export class TelegramBridge {
|
|
|
2595
2489
|
profiles,
|
|
2596
2490
|
});
|
|
2597
2491
|
this.sessions.set(input.sessionKey, session);
|
|
2598
|
-
await this.
|
|
2492
|
+
await this.sendModelPrompt(input.thread, session);
|
|
2599
2493
|
} catch (error) {
|
|
2600
2494
|
this.logError(error, "Recovery model selection failed; falling back to direct execution");
|
|
2601
2495
|
return input.execute();
|
|
2602
2496
|
}
|
|
2603
2497
|
}
|
|
2604
2498
|
|
|
2605
|
-
private async handleRecoveryModelSelect(
|
|
2606
|
-
sessionKey: string,
|
|
2607
|
-
_sessionId: string,
|
|
2608
|
-
modelProfileId: string,
|
|
2609
|
-
session: PendingRecoveryModelSession,
|
|
2610
|
-
thread: TelegramProcessThread,
|
|
2611
|
-
): Promise<void> {
|
|
2612
|
-
this.sessions.delete(sessionKey);
|
|
2613
|
-
await this.executeRecoveryModelSession(session, thread, {
|
|
2614
|
-
nextTurnModelProfileId: modelProfileId,
|
|
2615
|
-
});
|
|
2616
|
-
}
|
|
2617
|
-
|
|
2618
|
-
private async handleRecoveryModelSkip(
|
|
2619
|
-
sessionKey: string,
|
|
2620
|
-
_sessionId: string,
|
|
2621
|
-
session: PendingRecoveryModelSession,
|
|
2622
|
-
thread: TelegramProcessThread,
|
|
2623
|
-
): Promise<void> {
|
|
2624
|
-
this.sessions.delete(sessionKey);
|
|
2625
|
-
await this.executeRecoveryModelSession(session, thread);
|
|
2626
|
-
}
|
|
2627
|
-
|
|
2628
|
-
private async handleRecoveryModelText(
|
|
2629
|
-
sessionKey: string,
|
|
2630
|
-
session: PendingRecoveryModelSession,
|
|
2631
|
-
text: string,
|
|
2632
|
-
thread: TelegramProcessThread,
|
|
2633
|
-
): Promise<void> {
|
|
2634
|
-
const profile = resolveProfileByTextOrIndex(text.trim(), session.profiles);
|
|
2635
|
-
if (profile) {
|
|
2636
|
-
this.sessions.delete(sessionKey);
|
|
2637
|
-
await this.executeRecoveryModelSession(session, thread, {
|
|
2638
|
-
nextTurnModelProfileId: profile.id,
|
|
2639
|
-
});
|
|
2640
|
-
return;
|
|
2641
|
-
}
|
|
2642
|
-
await this.sendHtml(
|
|
2643
|
-
thread,
|
|
2644
|
-
escapeHtml(
|
|
2645
|
-
`Model must match an available profile.\n${buildRecoveryModelPrompt({ session })}`,
|
|
2646
|
-
),
|
|
2647
|
-
this.buildRecoveryModelReplyMarkup(session),
|
|
2648
|
-
);
|
|
2649
|
-
}
|
|
2650
|
-
|
|
2651
2499
|
private async executeRecoveryModelSession(
|
|
2500
|
+
sessionKey: string,
|
|
2652
2501
|
session: PendingRecoveryModelSession,
|
|
2653
2502
|
thread: TelegramProcessThread,
|
|
2654
2503
|
opts?: { nextTurnModelProfileId?: string | null },
|
|
2655
2504
|
): Promise<void> {
|
|
2505
|
+
this.sessions.delete(sessionKey);
|
|
2656
2506
|
if (session.recoveryKind === "retry") {
|
|
2657
2507
|
const retryOpts = {
|
|
2658
2508
|
...(opts?.nextTurnModelProfileId !== undefined ? opts : {}),
|
|
@@ -2673,36 +2523,6 @@ export class TelegramBridge {
|
|
|
2673
2523
|
}
|
|
2674
2524
|
}
|
|
2675
2525
|
|
|
2676
|
-
private async sendRecoveryModelPrompt(
|
|
2677
|
-
thread: TelegramProcessThread,
|
|
2678
|
-
session: PendingRecoveryModelSession,
|
|
2679
|
-
): Promise<void> {
|
|
2680
|
-
await this.sendHtml(
|
|
2681
|
-
thread,
|
|
2682
|
-
escapeHtml(buildRecoveryModelPrompt({ session })),
|
|
2683
|
-
this.buildRecoveryModelReplyMarkup(session),
|
|
2684
|
-
);
|
|
2685
|
-
}
|
|
2686
|
-
|
|
2687
|
-
private buildRecoveryModelReplyMarkup(
|
|
2688
|
-
session: PendingRecoveryModelSession,
|
|
2689
|
-
): TelegramInlineKeyboard | undefined {
|
|
2690
|
-
const buttons: Array<{ text: string; callbackData: string }> = [];
|
|
2691
|
-
let index = 0;
|
|
2692
|
-
for (const profile of session.profiles.slice(0, 20)) {
|
|
2693
|
-
index += 1;
|
|
2694
|
-
buttons.push({
|
|
2695
|
-
text: profile.id,
|
|
2696
|
-
callbackData: `ams:${session.instanceId}:${session.sessionId}:${index}`,
|
|
2697
|
-
});
|
|
2698
|
-
}
|
|
2699
|
-
buttons.push({
|
|
2700
|
-
text: "Keep current",
|
|
2701
|
-
callbackData: `amk:${session.instanceId}:${session.sessionId}`,
|
|
2702
|
-
});
|
|
2703
|
-
return buttons.length ? { inlineKeyboard: rows(buttons, 1) } : undefined;
|
|
2704
|
-
}
|
|
2705
|
-
|
|
2706
2526
|
private async executeProcessAction(
|
|
2707
2527
|
instanceId: string,
|
|
2708
2528
|
action: TelegramActionExecutionSummary,
|