@leitwerk-dev/telegram 0.2.0 → 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 +8 -0
- package/dist/actor.d.ts +1 -1
- package/dist/actor.d.ts.map +1 -1
- package/dist/bridge.d.ts +10 -0
- 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/index.d.ts +1 -16
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +20 -162
- package/dist/index.js.map +1 -1
- package/dist/launch-session.d.ts +83 -0
- 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 +94 -0
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/actor.ts +1 -1
- package/src/bridge.ts +10 -0
- package/src/config.ts +35 -5
- package/src/fake-telegram-client.ts +92 -9
- package/src/form-session.ts +143 -30
- package/src/index.ts +7 -12
- package/src/launch-session.ts +135 -15
- 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 +109 -3
package/src/config.ts
CHANGED
|
@@ -1,19 +1,48 @@
|
|
|
1
1
|
import { isUnknownRecord as isRecord } from "@leitwerk-dev/domain";
|
|
2
2
|
|
|
3
|
+
/** @internal */
|
|
3
4
|
export interface TelegramExtensionConfig {
|
|
5
|
+
/** @internal */
|
|
4
6
|
enabled: boolean;
|
|
7
|
+
/** @internal */
|
|
5
8
|
botToken: string;
|
|
9
|
+
/** @internal */
|
|
6
10
|
allowUserIds: readonly number[];
|
|
7
|
-
|
|
8
|
-
|
|
11
|
+
/** @internal */
|
|
12
|
+
delivery: {
|
|
13
|
+
/** @internal */
|
|
14
|
+
forumChatId: string;
|
|
15
|
+
};
|
|
16
|
+
/** @internal */
|
|
17
|
+
markdown: {
|
|
18
|
+
/** @internal */
|
|
19
|
+
maxChars: number;
|
|
20
|
+
};
|
|
21
|
+
/** @internal */
|
|
9
22
|
topicTitleTemplate: string;
|
|
10
|
-
|
|
23
|
+
/** @internal */
|
|
24
|
+
actionModelSelection: {
|
|
25
|
+
/** @internal */
|
|
26
|
+
enabled: boolean;
|
|
27
|
+
};
|
|
28
|
+
/** @internal */
|
|
11
29
|
allowedModelProfileIds: readonly string[];
|
|
12
30
|
}
|
|
13
31
|
|
|
32
|
+
/** @internal */
|
|
14
33
|
export type TelegramConfigResult =
|
|
15
|
-
| {
|
|
16
|
-
|
|
34
|
+
| {
|
|
35
|
+
/** @internal */
|
|
36
|
+
ok: true;
|
|
37
|
+
/** @internal */
|
|
38
|
+
config: TelegramExtensionConfig;
|
|
39
|
+
}
|
|
40
|
+
| {
|
|
41
|
+
/** @internal */
|
|
42
|
+
ok: false;
|
|
43
|
+
/** @internal */
|
|
44
|
+
errors: readonly string[];
|
|
45
|
+
};
|
|
17
46
|
|
|
18
47
|
interface RawTelegramConfig {
|
|
19
48
|
enabled?: unknown;
|
|
@@ -61,6 +90,7 @@ function normalizeAllowedModelProfileIds(value: unknown): string[] {
|
|
|
61
90
|
];
|
|
62
91
|
}
|
|
63
92
|
|
|
93
|
+
/** @internal */
|
|
64
94
|
export function normalizeTelegramConfig(rawValue: unknown): TelegramConfigResult {
|
|
65
95
|
const raw = (isRecord(rawValue) ? rawValue : {}) as RawTelegramConfig;
|
|
66
96
|
const delivery = isRecord(raw.delivery) ? raw.delivery : {};
|
|
@@ -10,46 +10,106 @@ import {
|
|
|
10
10
|
type TelegramTextUpdate,
|
|
11
11
|
} from "./types.js";
|
|
12
12
|
|
|
13
|
+
/** @internal */
|
|
13
14
|
export interface FakeTelegramTopic {
|
|
15
|
+
/** @internal */
|
|
14
16
|
chatId: string;
|
|
17
|
+
/** @internal */
|
|
15
18
|
name: string;
|
|
19
|
+
/** @internal */
|
|
16
20
|
messageThreadId: number;
|
|
17
21
|
}
|
|
18
22
|
|
|
23
|
+
/** @internal */
|
|
19
24
|
export interface FakeTelegramTopicEdit {
|
|
25
|
+
/** @internal */
|
|
20
26
|
chatId: string;
|
|
27
|
+
/** @internal */
|
|
21
28
|
messageThreadId: number;
|
|
29
|
+
/** @internal */
|
|
22
30
|
name: string;
|
|
23
31
|
}
|
|
24
32
|
|
|
33
|
+
/** @internal */
|
|
25
34
|
export interface FakeTelegramTopicClose {
|
|
35
|
+
/** @internal */
|
|
26
36
|
chatId: string;
|
|
37
|
+
/** @internal */
|
|
27
38
|
messageThreadId: number;
|
|
28
39
|
}
|
|
29
40
|
|
|
41
|
+
/** @internal */
|
|
30
42
|
export interface FakeTelegramAnswerCallback {
|
|
43
|
+
/** @internal */
|
|
31
44
|
callbackQueryId: string;
|
|
45
|
+
/** @internal */
|
|
32
46
|
text?: string;
|
|
47
|
+
/** @internal */
|
|
33
48
|
showAlert?: boolean;
|
|
34
49
|
}
|
|
35
50
|
|
|
51
|
+
/** @internal */
|
|
36
52
|
export type FakeTelegramOperation =
|
|
37
|
-
| {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
| {
|
|
44
|
-
|
|
53
|
+
| {
|
|
54
|
+
/** @internal */
|
|
55
|
+
kind: "createForumTopic";
|
|
56
|
+
/** @internal */
|
|
57
|
+
input: FakeTelegramTopic;
|
|
58
|
+
}
|
|
59
|
+
| {
|
|
60
|
+
/** @internal */
|
|
61
|
+
kind: "editForumTopic";
|
|
62
|
+
/** @internal */
|
|
63
|
+
input: FakeTelegramTopicEdit;
|
|
64
|
+
}
|
|
65
|
+
| {
|
|
66
|
+
/** @internal */
|
|
67
|
+
kind: "closeForumTopic";
|
|
68
|
+
/** @internal */
|
|
69
|
+
input: FakeTelegramTopicClose;
|
|
70
|
+
}
|
|
71
|
+
| {
|
|
72
|
+
/** @internal */
|
|
73
|
+
kind: "sendMessage";
|
|
74
|
+
/** @internal */
|
|
75
|
+
input: TelegramSendMessageInput;
|
|
76
|
+
}
|
|
77
|
+
| {
|
|
78
|
+
/** @internal */
|
|
79
|
+
kind: "sendPhoto";
|
|
80
|
+
/** @internal */
|
|
81
|
+
input: TelegramSendFileInput;
|
|
82
|
+
}
|
|
83
|
+
| {
|
|
84
|
+
/** @internal */
|
|
85
|
+
kind: "sendDocument";
|
|
86
|
+
/** @internal */
|
|
87
|
+
input: TelegramSendFileInput;
|
|
88
|
+
}
|
|
89
|
+
| {
|
|
90
|
+
/** @internal */
|
|
91
|
+
kind: "answerCallbackQuery";
|
|
92
|
+
/** @internal */
|
|
93
|
+
input: FakeTelegramAnswerCallback;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/** @internal */
|
|
45
97
|
export class FakeTelegramClient extends BaseTelegramClientEventRegistrar implements TelegramClient {
|
|
98
|
+
/** @internal */
|
|
46
99
|
readonly operations: FakeTelegramOperation[] = [];
|
|
100
|
+
/** @internal */
|
|
47
101
|
readonly createdTopics: FakeTelegramTopic[] = [];
|
|
102
|
+
/** @internal */
|
|
48
103
|
readonly editedTopics: FakeTelegramTopicEdit[] = [];
|
|
104
|
+
/** @internal */
|
|
49
105
|
readonly closedTopics: FakeTelegramTopicClose[] = [];
|
|
106
|
+
/** @internal */
|
|
50
107
|
readonly sentMessages: TelegramSendMessageInput[] = [];
|
|
108
|
+
/** @internal */
|
|
51
109
|
readonly sentPhotos: TelegramSendFileInput[] = [];
|
|
110
|
+
/** @internal */
|
|
52
111
|
readonly sentDocuments: TelegramSendFileInput[] = [];
|
|
112
|
+
/** @internal */
|
|
53
113
|
readonly answeredCallbacks: FakeTelegramAnswerCallback[] = [];
|
|
54
114
|
private nextThreadId = 100;
|
|
55
115
|
private nextMessageId = 1;
|
|
@@ -57,20 +117,25 @@ export class FakeTelegramClient extends BaseTelegramClientEventRegistrar impleme
|
|
|
57
117
|
private sendPhotoFailures: Error[] = [];
|
|
58
118
|
private sendDocumentFailures: Error[] = [];
|
|
59
119
|
private sendMessageFailure: { remainingSuccessfulSends: number; error: Error } | null = null;
|
|
120
|
+
/** @internal */
|
|
60
121
|
started = false;
|
|
61
122
|
|
|
123
|
+
/** @internal */
|
|
62
124
|
async start(): Promise<void> {
|
|
63
125
|
this.started = true;
|
|
64
126
|
}
|
|
65
127
|
|
|
128
|
+
/** @internal */
|
|
66
129
|
async stop(): Promise<void> {
|
|
67
130
|
this.started = false;
|
|
68
131
|
}
|
|
69
132
|
|
|
133
|
+
/** @internal */
|
|
70
134
|
failNextCreateForumTopic(error = new Error("createForumTopic failed")): void {
|
|
71
135
|
this.createForumTopicFailures.push(error);
|
|
72
136
|
}
|
|
73
137
|
|
|
138
|
+
/** @internal */
|
|
74
139
|
failSendMessageAfter(successfulSends: number, error = new Error("sendMessage failed")): void {
|
|
75
140
|
this.sendMessageFailure = {
|
|
76
141
|
remainingSuccessfulSends: Math.max(0, successfulSends),
|
|
@@ -78,18 +143,26 @@ export class FakeTelegramClient extends BaseTelegramClientEventRegistrar impleme
|
|
|
78
143
|
};
|
|
79
144
|
}
|
|
80
145
|
|
|
146
|
+
/** @internal */
|
|
81
147
|
failNextSendPhoto(error = new Error("sendPhoto failed")): void {
|
|
82
148
|
this.sendPhotoFailures.push(error);
|
|
83
149
|
}
|
|
84
150
|
|
|
151
|
+
/** @internal */
|
|
85
152
|
failNextSendDocument(error = new Error("sendDocument failed")): void {
|
|
86
153
|
this.sendDocumentFailures.push(error);
|
|
87
154
|
}
|
|
88
155
|
|
|
156
|
+
/** @internal */
|
|
89
157
|
async createForumTopic(input: {
|
|
158
|
+
/** @internal */
|
|
90
159
|
chatId: string;
|
|
160
|
+
/** @internal */
|
|
91
161
|
name: string;
|
|
92
|
-
}): Promise<{
|
|
162
|
+
}): Promise<{
|
|
163
|
+
/** @internal */
|
|
164
|
+
messageThreadId: number;
|
|
165
|
+
}> {
|
|
93
166
|
const failure = this.createForumTopicFailures.shift();
|
|
94
167
|
if (failure) {
|
|
95
168
|
throw failure;
|
|
@@ -104,16 +177,19 @@ export class FakeTelegramClient extends BaseTelegramClientEventRegistrar impleme
|
|
|
104
177
|
return { messageThreadId: topic.messageThreadId };
|
|
105
178
|
}
|
|
106
179
|
|
|
180
|
+
/** @internal */
|
|
107
181
|
async editForumTopic(input: FakeTelegramTopicEdit): Promise<void> {
|
|
108
182
|
this.operations.push({ kind: "editForumTopic", input });
|
|
109
183
|
this.editedTopics.push(input);
|
|
110
184
|
}
|
|
111
185
|
|
|
186
|
+
/** @internal */
|
|
112
187
|
async closeForumTopic(input: FakeTelegramTopicClose): Promise<void> {
|
|
113
188
|
this.operations.push({ kind: "closeForumTopic", input });
|
|
114
189
|
this.closedTopics.push(input);
|
|
115
190
|
}
|
|
116
191
|
|
|
192
|
+
/** @internal */
|
|
117
193
|
async sendMessage(input: TelegramSendMessageInput): Promise<TelegramSentMessage> {
|
|
118
194
|
const failure = this.sendMessageFailure;
|
|
119
195
|
if (failure) {
|
|
@@ -128,6 +204,7 @@ export class FakeTelegramClient extends BaseTelegramClientEventRegistrar impleme
|
|
|
128
204
|
return { messageId: this.nextMessageId++ };
|
|
129
205
|
}
|
|
130
206
|
|
|
207
|
+
/** @internal */
|
|
131
208
|
async sendPhoto(input: TelegramSendFileInput): Promise<TelegramSentMessage> {
|
|
132
209
|
const failure = this.sendPhotoFailures.shift();
|
|
133
210
|
if (failure) throw failure;
|
|
@@ -136,6 +213,7 @@ export class FakeTelegramClient extends BaseTelegramClientEventRegistrar impleme
|
|
|
136
213
|
return { messageId: this.nextMessageId++ };
|
|
137
214
|
}
|
|
138
215
|
|
|
216
|
+
/** @internal */
|
|
139
217
|
async sendDocument(input: TelegramSendFileInput): Promise<TelegramSentMessage> {
|
|
140
218
|
const failure = this.sendDocumentFailures.shift();
|
|
141
219
|
if (failure) throw failure;
|
|
@@ -144,23 +222,28 @@ export class FakeTelegramClient extends BaseTelegramClientEventRegistrar impleme
|
|
|
144
222
|
return { messageId: this.nextMessageId++ };
|
|
145
223
|
}
|
|
146
224
|
|
|
225
|
+
/** @internal */
|
|
147
226
|
async answerCallbackQuery(input: FakeTelegramAnswerCallback): Promise<void> {
|
|
148
227
|
this.operations.push({ kind: "answerCallbackQuery", input });
|
|
149
228
|
this.answeredCallbacks.push(input);
|
|
150
229
|
}
|
|
151
230
|
|
|
231
|
+
/** @internal */
|
|
152
232
|
async simulateText(update: TelegramTextUpdate): Promise<void> {
|
|
153
233
|
await Promise.all([...this.textHandlers].map((handler) => handler(update)));
|
|
154
234
|
}
|
|
155
235
|
|
|
236
|
+
/** @internal */
|
|
156
237
|
async simulateCallback(update: TelegramCallbackUpdate): Promise<void> {
|
|
157
238
|
await Promise.all([...this.callbackHandlers].map((handler) => handler(update)));
|
|
158
239
|
}
|
|
159
240
|
|
|
241
|
+
/** @internal */
|
|
160
242
|
async simulateForumTopicCreated(update: TelegramForumTopicCreatedUpdate): Promise<void> {
|
|
161
243
|
await Promise.all([...this.forumTopicCreatedHandlers].map((handler) => handler(update)));
|
|
162
244
|
}
|
|
163
245
|
|
|
246
|
+
/** @internal */
|
|
164
247
|
async simulateForumTopicClosed(update: TelegramForumTopicClosedUpdate): Promise<void> {
|
|
165
248
|
await Promise.all([...this.forumTopicClosedHandlers].map((handler) => handler(update)));
|
|
166
249
|
}
|
package/src/form-session.ts
CHANGED
|
@@ -11,80 +11,138 @@ import type {
|
|
|
11
11
|
} from "@leitwerk-dev/process-sdk";
|
|
12
12
|
import { resolvePromptCacheSwitch } from "@leitwerk-dev/protocol/http-contracts";
|
|
13
13
|
|
|
14
|
+
/** @internal */
|
|
14
15
|
export interface ActionPreviewLike {
|
|
16
|
+
/** @internal */
|
|
15
17
|
kind?: string;
|
|
18
|
+
/** @internal */
|
|
16
19
|
turnId?: string;
|
|
20
|
+
/** @internal */
|
|
17
21
|
lifecycleStatus?: string;
|
|
18
22
|
}
|
|
19
23
|
|
|
20
|
-
|
|
21
|
-
|
|
24
|
+
/** @internal */
|
|
25
|
+
interface PendingAction {
|
|
26
|
+
/** @internal */
|
|
27
|
+
actionId: string;
|
|
28
|
+
/** @internal */
|
|
29
|
+
actionLabel: string;
|
|
30
|
+
/** @internal */
|
|
31
|
+
actionPreview?: ActionPreviewLike | null;
|
|
32
|
+
/** @internal */
|
|
33
|
+
sessionId: string;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @internal */
|
|
37
|
+
export type PendingTelegramSession = {
|
|
38
|
+
/** @internal */
|
|
39
|
+
instanceId: string;
|
|
40
|
+
/** @internal */
|
|
41
|
+
expiresAt: number;
|
|
42
|
+
} & (
|
|
43
|
+
| (PendingAction & {
|
|
44
|
+
/** @internal */
|
|
22
45
|
kind: "action_form";
|
|
23
|
-
|
|
24
|
-
actionId: string;
|
|
25
|
-
actionLabel: string;
|
|
26
|
-
actionPreview?: ActionPreviewLike | null;
|
|
27
|
-
sessionId: string;
|
|
46
|
+
/** @internal */
|
|
28
47
|
form: FormDefinition;
|
|
48
|
+
/** @internal */
|
|
29
49
|
fieldIndex: number;
|
|
50
|
+
/** @internal */
|
|
30
51
|
values: Record<string, unknown>;
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
52
|
+
})
|
|
53
|
+
| (PendingAction & {
|
|
54
|
+
/** @internal */
|
|
34
55
|
kind: "action_model";
|
|
35
|
-
|
|
36
|
-
actionId: string;
|
|
37
|
-
actionLabel: string;
|
|
38
|
-
actionPreview?: ActionPreviewLike | null;
|
|
39
|
-
sessionId: string;
|
|
56
|
+
/** @internal */
|
|
40
57
|
formValues: Record<string, unknown>;
|
|
58
|
+
/** @internal */
|
|
41
59
|
preview: ProcessActionModelPreviewLike;
|
|
60
|
+
/** @internal */
|
|
42
61
|
profiles: readonly ModelProfileOptionSummaryLike[];
|
|
43
|
-
|
|
44
|
-
}
|
|
62
|
+
})
|
|
45
63
|
| {
|
|
64
|
+
/** @internal */
|
|
46
65
|
kind: "recovery_model";
|
|
47
|
-
|
|
66
|
+
/** @internal */
|
|
48
67
|
recoveryKind: "retry" | "continue";
|
|
68
|
+
/** @internal */
|
|
49
69
|
turnRecordId?: string;
|
|
70
|
+
/** @internal */
|
|
50
71
|
sessionId: string;
|
|
72
|
+
/** @internal */
|
|
51
73
|
profiles: readonly ModelProfileOptionSummaryLike[];
|
|
74
|
+
/** @internal */
|
|
52
75
|
selectedModelProfileId?: string | null;
|
|
53
|
-
expiresAt: number;
|
|
54
76
|
}
|
|
55
77
|
| {
|
|
78
|
+
/** @internal */
|
|
56
79
|
kind: "continue";
|
|
57
|
-
|
|
80
|
+
/** @internal */
|
|
58
81
|
turnRecordId: string;
|
|
82
|
+
/** @internal */
|
|
59
83
|
nextTurnModelProfileId?: string | null;
|
|
60
|
-
expiresAt: number;
|
|
61
84
|
}
|
|
62
85
|
| {
|
|
86
|
+
/** @internal */
|
|
63
87
|
kind: "question";
|
|
64
|
-
|
|
88
|
+
/** @internal */
|
|
65
89
|
request: ProcessQuestionRequest;
|
|
90
|
+
/** @internal */
|
|
66
91
|
questionIndex: number;
|
|
92
|
+
/** @internal */
|
|
67
93
|
draft: QuestionAnswerDraft[];
|
|
68
|
-
|
|
69
|
-
|
|
94
|
+
}
|
|
95
|
+
);
|
|
70
96
|
|
|
97
|
+
/** @internal */
|
|
71
98
|
export type PendingActionFormSession = Extract<PendingTelegramSession, { kind: "action_form" }>;
|
|
99
|
+
/** @internal */
|
|
72
100
|
export type PendingActionModelSession = Extract<PendingTelegramSession, { kind: "action_model" }>;
|
|
101
|
+
/** @internal */
|
|
73
102
|
export type PendingQuestionSession = Extract<PendingTelegramSession, { kind: "question" }>;
|
|
74
103
|
|
|
104
|
+
/** @internal */
|
|
75
105
|
export type FormStepResult =
|
|
76
|
-
| {
|
|
77
|
-
|
|
78
|
-
|
|
106
|
+
| {
|
|
107
|
+
/** @internal */
|
|
108
|
+
ok: true;
|
|
109
|
+
/** @internal */
|
|
110
|
+
done: false;
|
|
111
|
+
/** @internal */
|
|
112
|
+
prompt: string;
|
|
113
|
+
}
|
|
114
|
+
| {
|
|
115
|
+
/** @internal */
|
|
116
|
+
ok: true;
|
|
117
|
+
/** @internal */
|
|
118
|
+
done: true;
|
|
119
|
+
/** @internal */
|
|
120
|
+
values: Record<string, unknown>;
|
|
121
|
+
}
|
|
122
|
+
| {
|
|
123
|
+
/** @internal */
|
|
124
|
+
ok: false;
|
|
125
|
+
/** @internal */
|
|
126
|
+
prompt: string;
|
|
127
|
+
};
|
|
79
128
|
|
|
129
|
+
/** @internal */
|
|
80
130
|
export function buildActionFormSession(input: {
|
|
131
|
+
/** @internal */
|
|
81
132
|
instanceId: string;
|
|
133
|
+
/** @internal */
|
|
82
134
|
actionId: string;
|
|
135
|
+
/** @internal */
|
|
83
136
|
actionLabel?: string;
|
|
137
|
+
/** @internal */
|
|
84
138
|
actionPreview?: ActionPreviewLike | null;
|
|
139
|
+
/** @internal */
|
|
85
140
|
sessionId: string;
|
|
141
|
+
/** @internal */
|
|
86
142
|
form: FormDefinition;
|
|
143
|
+
/** @internal */
|
|
87
144
|
now?: number;
|
|
145
|
+
/** @internal */
|
|
88
146
|
ttlMs?: number;
|
|
89
147
|
}): PendingActionFormSession {
|
|
90
148
|
return {
|
|
@@ -101,8 +159,11 @@ export function buildActionFormSession(input: {
|
|
|
101
159
|
};
|
|
102
160
|
}
|
|
103
161
|
|
|
162
|
+
/** @internal */
|
|
104
163
|
export function buildQuestionSession(input: {
|
|
164
|
+
/** @internal */
|
|
105
165
|
instanceId: string;
|
|
166
|
+
/** @internal */
|
|
106
167
|
request: ProcessQuestionRequest;
|
|
107
168
|
}): PendingQuestionSession {
|
|
108
169
|
return {
|
|
@@ -115,6 +176,7 @@ export function buildQuestionSession(input: {
|
|
|
115
176
|
};
|
|
116
177
|
}
|
|
117
178
|
|
|
179
|
+
/** @internal */
|
|
118
180
|
export function buildQuestionPrompt(session: PendingQuestionSession): string {
|
|
119
181
|
const question = session.request.questions[session.questionIndex];
|
|
120
182
|
if (!question) return "Send your answer.";
|
|
@@ -129,10 +191,18 @@ export function buildQuestionPrompt(session: PendingQuestionSession): string {
|
|
|
129
191
|
].join("\n");
|
|
130
192
|
}
|
|
131
193
|
|
|
194
|
+
/** @internal */
|
|
132
195
|
export function applyQuestionText(
|
|
133
196
|
session: PendingQuestionSession,
|
|
134
197
|
text: string,
|
|
135
|
-
): {
|
|
198
|
+
): {
|
|
199
|
+
/** @internal */
|
|
200
|
+
done: boolean;
|
|
201
|
+
/** @internal */
|
|
202
|
+
draft: QuestionAnswerDraft[];
|
|
203
|
+
/** @internal */
|
|
204
|
+
prompt?: string;
|
|
205
|
+
} {
|
|
136
206
|
const answer = session.draft[session.questionIndex];
|
|
137
207
|
if (!answer || text.trim() === "") {
|
|
138
208
|
return {
|
|
@@ -148,11 +218,17 @@ export function applyQuestionText(
|
|
|
148
218
|
: { done: true, draft: session.draft };
|
|
149
219
|
}
|
|
150
220
|
|
|
221
|
+
/** @internal */
|
|
151
222
|
export function buildContinueSession(input: {
|
|
223
|
+
/** @internal */
|
|
152
224
|
instanceId: string;
|
|
225
|
+
/** @internal */
|
|
153
226
|
turnRecordId: string;
|
|
227
|
+
/** @internal */
|
|
154
228
|
nextTurnModelProfileId?: string | null;
|
|
229
|
+
/** @internal */
|
|
155
230
|
now?: number;
|
|
231
|
+
/** @internal */
|
|
156
232
|
ttlMs?: number;
|
|
157
233
|
}): Extract<PendingTelegramSession, { kind: "continue" }> {
|
|
158
234
|
return {
|
|
@@ -164,10 +240,12 @@ export function buildContinueSession(input: {
|
|
|
164
240
|
};
|
|
165
241
|
}
|
|
166
242
|
|
|
243
|
+
/** @internal */
|
|
167
244
|
export function currentField(session: PendingActionFormSession): FormFieldDefinition | null {
|
|
168
245
|
return session.form.fields[session.fieldIndex] ?? null;
|
|
169
246
|
}
|
|
170
247
|
|
|
248
|
+
/** @internal */
|
|
171
249
|
export function buildFieldPrompt(field: FormFieldDefinition): string {
|
|
172
250
|
const required = field.required ? " required" : "";
|
|
173
251
|
const description = field.description ? `\n${field.description}` : "";
|
|
@@ -175,6 +253,7 @@ export function buildFieldPrompt(field: FormFieldDefinition): string {
|
|
|
175
253
|
return `Enter ${field.label}${required}.${description}${hint}`;
|
|
176
254
|
}
|
|
177
255
|
|
|
256
|
+
/** @internal */
|
|
178
257
|
export function canSkipActionFormField(field: FormFieldDefinition | null | undefined): boolean {
|
|
179
258
|
return Boolean(field && !field.required);
|
|
180
259
|
}
|
|
@@ -187,6 +266,7 @@ function advanceFormField(session: PendingActionFormSession): FormStepResult {
|
|
|
187
266
|
: { ok: true, done: true, values: session.values };
|
|
188
267
|
}
|
|
189
268
|
|
|
269
|
+
/** @internal */
|
|
190
270
|
export function applyFormSkip(session: PendingActionFormSession): FormStepResult {
|
|
191
271
|
const field = currentField(session);
|
|
192
272
|
if (!field) {
|
|
@@ -231,6 +311,7 @@ function parseFieldValue(
|
|
|
231
311
|
return { ok: true, value: text };
|
|
232
312
|
}
|
|
233
313
|
|
|
314
|
+
/** @internal */
|
|
234
315
|
export function applyFormText(session: PendingActionFormSession, text: string): FormStepResult {
|
|
235
316
|
const field = currentField(session);
|
|
236
317
|
if (!field) {
|
|
@@ -244,16 +325,27 @@ export function applyFormText(session: PendingActionFormSession, text: string):
|
|
|
244
325
|
return advanceFormField(session);
|
|
245
326
|
}
|
|
246
327
|
|
|
328
|
+
/** @internal */
|
|
247
329
|
export function buildActionModelSession(input: {
|
|
330
|
+
/** @internal */
|
|
248
331
|
instanceId: string;
|
|
332
|
+
/** @internal */
|
|
249
333
|
actionId: string;
|
|
334
|
+
/** @internal */
|
|
250
335
|
actionLabel: string;
|
|
336
|
+
/** @internal */
|
|
251
337
|
actionPreview?: ActionPreviewLike | null;
|
|
338
|
+
/** @internal */
|
|
252
339
|
sessionId: string;
|
|
340
|
+
/** @internal */
|
|
253
341
|
formValues: Record<string, unknown>;
|
|
342
|
+
/** @internal */
|
|
254
343
|
preview: ProcessActionModelPreviewLike;
|
|
344
|
+
/** @internal */
|
|
255
345
|
profiles: readonly ModelProfileOptionSummaryLike[];
|
|
346
|
+
/** @internal */
|
|
256
347
|
now?: number;
|
|
348
|
+
/** @internal */
|
|
257
349
|
ttlMs?: number;
|
|
258
350
|
}): PendingActionModelSession {
|
|
259
351
|
return {
|
|
@@ -270,9 +362,13 @@ export function buildActionModelSession(input: {
|
|
|
270
362
|
};
|
|
271
363
|
}
|
|
272
364
|
|
|
365
|
+
/** @internal */
|
|
273
366
|
export function buildActionModelSwitchWarning(input: {
|
|
367
|
+
/** @internal */
|
|
274
368
|
session: PendingActionModelSession;
|
|
369
|
+
/** @internal */
|
|
275
370
|
effectiveModelProfileId: string;
|
|
371
|
+
/** @internal */
|
|
276
372
|
now?: number;
|
|
277
373
|
}): string | null {
|
|
278
374
|
const resolution = resolvePromptCacheSwitch({
|
|
@@ -291,7 +387,11 @@ export function buildActionModelSwitchWarning(input: {
|
|
|
291
387
|
return `⚠ Switching models may lose prompt-cache reuse and increase costs. To keep using the previous model, choose ${recommendation}.`;
|
|
292
388
|
}
|
|
293
389
|
|
|
294
|
-
|
|
390
|
+
/** @internal */
|
|
391
|
+
export function buildActionModelPrompt(input: {
|
|
392
|
+
/** @internal */
|
|
393
|
+
session: PendingActionModelSession;
|
|
394
|
+
}): string {
|
|
295
395
|
const { session } = input;
|
|
296
396
|
const lines = [`Next-turn model for: ${session.actionLabel}`];
|
|
297
397
|
const preview = session.preview;
|
|
@@ -317,18 +417,27 @@ export function buildActionModelPrompt(input: { session: PendingActionModelSessi
|
|
|
317
417
|
return lines.join("\n");
|
|
318
418
|
}
|
|
319
419
|
|
|
420
|
+
/** @internal */
|
|
320
421
|
export type PendingRecoveryModelSession = Extract<
|
|
321
422
|
PendingTelegramSession,
|
|
322
423
|
{ kind: "recovery_model" }
|
|
323
424
|
>;
|
|
324
425
|
|
|
426
|
+
/** @internal */
|
|
325
427
|
export function buildRecoveryModelSession(input: {
|
|
428
|
+
/** @internal */
|
|
326
429
|
instanceId: string;
|
|
430
|
+
/** @internal */
|
|
327
431
|
recoveryKind: "retry" | "continue";
|
|
432
|
+
/** @internal */
|
|
328
433
|
turnRecordId?: string;
|
|
434
|
+
/** @internal */
|
|
329
435
|
sessionId: string;
|
|
436
|
+
/** @internal */
|
|
330
437
|
profiles: readonly ModelProfileOptionSummaryLike[];
|
|
438
|
+
/** @internal */
|
|
331
439
|
now?: number;
|
|
440
|
+
/** @internal */
|
|
332
441
|
ttlMs?: number;
|
|
333
442
|
}): PendingRecoveryModelSession {
|
|
334
443
|
return {
|
|
@@ -342,7 +451,11 @@ export function buildRecoveryModelSession(input: {
|
|
|
342
451
|
};
|
|
343
452
|
}
|
|
344
453
|
|
|
345
|
-
|
|
454
|
+
/** @internal */
|
|
455
|
+
export function buildRecoveryModelPrompt(input: {
|
|
456
|
+
/** @internal */
|
|
457
|
+
session: PendingRecoveryModelSession;
|
|
458
|
+
}): string {
|
|
346
459
|
const { session } = input;
|
|
347
460
|
const label = session.recoveryKind === "retry" ? "Retry" : "Continue";
|
|
348
461
|
const lines = [`Next-turn model for: ${label}`];
|
package/src/index.ts
CHANGED
|
@@ -8,13 +8,18 @@ import { normalizeTelegramConfig } from "./config.js";
|
|
|
8
8
|
import { GrammyTelegramClient } from "./grammy-telegram-client.js";
|
|
9
9
|
import type { TelegramClient } from "./types.js";
|
|
10
10
|
|
|
11
|
-
|
|
11
|
+
/** @internal */
|
|
12
|
+
const telegramClientToken = createCapabilityToken<TelegramClient>("telegram:client");
|
|
12
13
|
|
|
13
|
-
|
|
14
|
+
/** @internal */
|
|
15
|
+
const manifest = {
|
|
16
|
+
/** @internal */
|
|
14
17
|
id: "telegram",
|
|
18
|
+
/** @internal */
|
|
15
19
|
version: "0.1.0",
|
|
16
20
|
} as const;
|
|
17
21
|
|
|
22
|
+
/** @public */
|
|
18
23
|
const telegramExtension: LeitwerkExtensionModule = {
|
|
19
24
|
manifest,
|
|
20
25
|
setupServer(api, rawConfig) {
|
|
@@ -57,13 +62,3 @@ const telegramExtension: LeitwerkExtensionModule = {
|
|
|
57
62
|
};
|
|
58
63
|
|
|
59
64
|
export default telegramExtension;
|
|
60
|
-
export { TELEGRAM_ACTOR } from "./actor.js";
|
|
61
|
-
export * from "./bridge.js";
|
|
62
|
-
export * from "./config.js";
|
|
63
|
-
export * from "./fake-telegram-client.js";
|
|
64
|
-
export * from "./form-session.js";
|
|
65
|
-
export * from "./launch-session.js";
|
|
66
|
-
export * from "./markdown-html.js";
|
|
67
|
-
export * from "./process-thread-store.js";
|
|
68
|
-
export * from "./topic-title.js";
|
|
69
|
-
export type * from "./types.js";
|