@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.
Files changed (71) hide show
  1. package/README.md +9 -1
  2. package/dist/actions.d.ts +1 -0
  3. package/dist/actions.d.ts.map +1 -1
  4. package/dist/actor.d.ts +1 -1
  5. package/dist/actor.d.ts.map +1 -1
  6. package/dist/bridge.d.ts +13 -10
  7. package/dist/bridge.d.ts.map +1 -1
  8. package/dist/config.d.ts +18 -0
  9. package/dist/config.d.ts.map +1 -1
  10. package/dist/fake-telegram-client.d.ts +60 -0
  11. package/dist/fake-telegram-client.d.ts.map +1 -1
  12. package/dist/form-session.d.ts +112 -19
  13. package/dist/form-session.d.ts.map +1 -1
  14. package/dist/grammy-telegram-client.d.ts +17 -41
  15. package/dist/grammy-telegram-client.d.ts.map +1 -1
  16. package/dist/index.d.ts +1 -16
  17. package/dist/index.d.ts.map +1 -1
  18. package/dist/index.js +3580 -52
  19. package/dist/index.js.map +1 -1
  20. package/dist/launch-session.d.ts +84 -1
  21. package/dist/launch-session.d.ts.map +1 -1
  22. package/dist/markdown-html.d.ts +19 -0
  23. package/dist/markdown-html.d.ts.map +1 -1
  24. package/dist/process-thread-store.d.ts +8 -0
  25. package/dist/process-thread-store.d.ts.map +1 -1
  26. package/dist/topic-title.d.ts +4 -0
  27. package/dist/topic-title.d.ts.map +1 -1
  28. package/dist/types.d.ts +99 -6
  29. package/dist/types.d.ts.map +1 -1
  30. package/package.json +7 -7
  31. package/src/actions.ts +14 -5
  32. package/src/actor.ts +1 -1
  33. package/src/bridge.ts +123 -303
  34. package/src/config.ts +37 -9
  35. package/src/fake-telegram-client.ts +92 -9
  36. package/src/form-session.ts +153 -42
  37. package/src/grammy-telegram-client.ts +23 -43
  38. package/src/index.ts +7 -12
  39. package/src/launch-session.ts +165 -70
  40. package/src/markdown-html.ts +35 -4
  41. package/src/process-thread-store.ts +8 -0
  42. package/src/topic-title.ts +8 -1
  43. package/src/types.ts +113 -10
  44. package/dist/actions.js +0 -32
  45. package/dist/actions.js.map +0 -1
  46. package/dist/actor.js +0 -7
  47. package/dist/actor.js.map +0 -1
  48. package/dist/bridge.js +0 -2032
  49. package/dist/bridge.js.map +0 -1
  50. package/dist/config.js +0 -70
  51. package/dist/config.js.map +0 -1
  52. package/dist/fake-telegram-client.js +0 -107
  53. package/dist/fake-telegram-client.js.map +0 -1
  54. package/dist/form-session.js +0 -218
  55. package/dist/form-session.js.map +0 -1
  56. package/dist/grammy-telegram-client.js +0 -195
  57. package/dist/grammy-telegram-client.js.map +0 -1
  58. package/dist/launch-session.js +0 -435
  59. package/dist/launch-session.js.map +0 -1
  60. package/dist/markdown-html.js +0 -259
  61. package/dist/markdown-html.js.map +0 -1
  62. package/dist/mermaid-image.js +0 -99
  63. package/dist/mermaid-image.js.map +0 -1
  64. package/dist/process-feed.js +0 -165
  65. package/dist/process-feed.js.map +0 -1
  66. package/dist/process-thread-store.js +0 -74
  67. package/dist/process-thread-store.js.map +0 -1
  68. package/dist/topic-title.js +0 -29
  69. package/dist/topic-title.js.map +0 -1
  70. package/dist/types.js +0 -19
  71. package/dist/types.js.map +0 -1
package/src/config.ts CHANGED
@@ -1,17 +1,48 @@
1
+ import { isUnknownRecord as isRecord } from "@leitwerk-dev/domain";
2
+
3
+ /** @internal */
1
4
  export interface TelegramExtensionConfig {
5
+ /** @internal */
2
6
  enabled: boolean;
7
+ /** @internal */
3
8
  botToken: string;
9
+ /** @internal */
4
10
  allowUserIds: readonly number[];
5
- delivery: { forumChatId: string };
6
- markdown: { maxChars: number };
11
+ /** @internal */
12
+ delivery: {
13
+ /** @internal */
14
+ forumChatId: string;
15
+ };
16
+ /** @internal */
17
+ markdown: {
18
+ /** @internal */
19
+ maxChars: number;
20
+ };
21
+ /** @internal */
7
22
  topicTitleTemplate: string;
8
- actionModelSelection: { enabled: boolean };
23
+ /** @internal */
24
+ actionModelSelection: {
25
+ /** @internal */
26
+ enabled: boolean;
27
+ };
28
+ /** @internal */
9
29
  allowedModelProfileIds: readonly string[];
10
30
  }
11
31
 
32
+ /** @internal */
12
33
  export type TelegramConfigResult =
13
- | { ok: true; config: TelegramExtensionConfig }
14
- | { ok: false; errors: readonly string[] };
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
+ };
15
46
 
16
47
  interface RawTelegramConfig {
17
48
  enabled?: unknown;
@@ -25,10 +56,6 @@ interface RawTelegramConfig {
25
56
  allowed_model_profile_ids?: unknown;
26
57
  }
27
58
 
28
- function isRecord(value: unknown): value is Record<string, unknown> {
29
- return typeof value === "object" && value !== null && !Array.isArray(value);
30
- }
31
-
32
59
  function normalizeUserIds(value: unknown): number[] {
33
60
  if (!Array.isArray(value)) return [];
34
61
  return [
@@ -63,6 +90,7 @@ function normalizeAllowedModelProfileIds(value: unknown): string[] {
63
90
  ];
64
91
  }
65
92
 
93
+ /** @internal */
66
94
  export function normalizeTelegramConfig(rawValue: unknown): TelegramConfigResult {
67
95
  const raw = (isRecord(rawValue) ? rawValue : {}) as RawTelegramConfig;
68
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
- | { kind: "createForumTopic"; input: FakeTelegramTopic }
38
- | { kind: "editForumTopic"; input: FakeTelegramTopicEdit }
39
- | { kind: "closeForumTopic"; input: FakeTelegramTopicClose }
40
- | { kind: "sendMessage"; input: TelegramSendMessageInput }
41
- | { kind: "sendPhoto"; input: TelegramSendFileInput }
42
- | { kind: "sendDocument"; input: TelegramSendFileInput }
43
- | { kind: "answerCallbackQuery"; input: FakeTelegramAnswerCallback };
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<{ messageThreadId: number }> {
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
  }
@@ -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
- export type PendingTelegramSession =
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
- instanceId: string;
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
- expiresAt: number;
32
- }
33
- | {
52
+ })
53
+ | (PendingAction & {
54
+ /** @internal */
34
55
  kind: "action_model";
35
- instanceId: string;
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
- expiresAt: number;
44
- }
62
+ })
45
63
  | {
64
+ /** @internal */
46
65
  kind: "recovery_model";
47
- instanceId: string;
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
- instanceId: string;
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
- instanceId: string;
88
+ /** @internal */
65
89
  request: ProcessQuestionRequest;
90
+ /** @internal */
66
91
  questionIndex: number;
92
+ /** @internal */
67
93
  draft: QuestionAnswerDraft[];
68
- expiresAt: number;
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
- | { ok: true; done: false; prompt: string }
77
- | { ok: true; done: true; values: Record<string, unknown> }
78
- | { ok: false; prompt: string };
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
- ): { done: boolean; draft: QuestionAnswerDraft[]; prompt?: string } {
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,10 +253,20 @@ 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
  }
181
260
 
261
+ function advanceFormField(session: PendingActionFormSession): FormStepResult {
262
+ session.fieldIndex += 1;
263
+ const field = currentField(session);
264
+ return field
265
+ ? { ok: true, done: false, prompt: buildFieldPrompt(field) }
266
+ : { ok: true, done: true, values: session.values };
267
+ }
268
+
269
+ /** @internal */
182
270
  export function applyFormSkip(session: PendingActionFormSession): FormStepResult {
183
271
  const field = currentField(session);
184
272
  if (!field) {
@@ -187,12 +275,7 @@ export function applyFormSkip(session: PendingActionFormSession): FormStepResult
187
275
  if (!canSkipActionFormField(field)) {
188
276
  return { ok: false, prompt: `${field.label} is required.\n${buildFieldPrompt(field)}` };
189
277
  }
190
- session.fieldIndex += 1;
191
- const next = currentField(session);
192
- if (!next) {
193
- return { ok: true, done: true, values: session.values };
194
- }
195
- return { ok: true, done: false, prompt: buildFieldPrompt(next) };
278
+ return advanceFormField(session);
196
279
  }
197
280
 
198
281
  function parseBoolean(value: string): boolean | null {
@@ -228,6 +311,7 @@ function parseFieldValue(
228
311
  return { ok: true, value: text };
229
312
  }
230
313
 
314
+ /** @internal */
231
315
  export function applyFormText(session: PendingActionFormSession, text: string): FormStepResult {
232
316
  const field = currentField(session);
233
317
  if (!field) {
@@ -238,24 +322,30 @@ export function applyFormText(session: PendingActionFormSession, text: string):
238
322
  return { ok: false, prompt: `${parsed.error}\n${buildFieldPrompt(field)}` };
239
323
  }
240
324
  session.values[field.id] = parsed.value;
241
- session.fieldIndex += 1;
242
- const next = currentField(session);
243
- if (!next) {
244
- return { ok: true, done: true, values: session.values };
245
- }
246
- return { ok: true, done: false, prompt: buildFieldPrompt(next) };
325
+ return advanceFormField(session);
247
326
  }
248
327
 
328
+ /** @internal */
249
329
  export function buildActionModelSession(input: {
330
+ /** @internal */
250
331
  instanceId: string;
332
+ /** @internal */
251
333
  actionId: string;
334
+ /** @internal */
252
335
  actionLabel: string;
336
+ /** @internal */
253
337
  actionPreview?: ActionPreviewLike | null;
338
+ /** @internal */
254
339
  sessionId: string;
340
+ /** @internal */
255
341
  formValues: Record<string, unknown>;
342
+ /** @internal */
256
343
  preview: ProcessActionModelPreviewLike;
344
+ /** @internal */
257
345
  profiles: readonly ModelProfileOptionSummaryLike[];
346
+ /** @internal */
258
347
  now?: number;
348
+ /** @internal */
259
349
  ttlMs?: number;
260
350
  }): PendingActionModelSession {
261
351
  return {
@@ -272,9 +362,13 @@ export function buildActionModelSession(input: {
272
362
  };
273
363
  }
274
364
 
365
+ /** @internal */
275
366
  export function buildActionModelSwitchWarning(input: {
367
+ /** @internal */
276
368
  session: PendingActionModelSession;
369
+ /** @internal */
277
370
  effectiveModelProfileId: string;
371
+ /** @internal */
278
372
  now?: number;
279
373
  }): string | null {
280
374
  const resolution = resolvePromptCacheSwitch({
@@ -293,7 +387,11 @@ export function buildActionModelSwitchWarning(input: {
293
387
  return `⚠ Switching models may lose prompt-cache reuse and increase costs. To keep using the previous model, choose ${recommendation}.`;
294
388
  }
295
389
 
296
- export function buildActionModelPrompt(input: { session: PendingActionModelSession }): string {
390
+ /** @internal */
391
+ export function buildActionModelPrompt(input: {
392
+ /** @internal */
393
+ session: PendingActionModelSession;
394
+ }): string {
297
395
  const { session } = input;
298
396
  const lines = [`Next-turn model for: ${session.actionLabel}`];
299
397
  const preview = session.preview;
@@ -319,18 +417,27 @@ export function buildActionModelPrompt(input: { session: PendingActionModelSessi
319
417
  return lines.join("\n");
320
418
  }
321
419
 
420
+ /** @internal */
322
421
  export type PendingRecoveryModelSession = Extract<
323
422
  PendingTelegramSession,
324
423
  { kind: "recovery_model" }
325
424
  >;
326
425
 
426
+ /** @internal */
327
427
  export function buildRecoveryModelSession(input: {
428
+ /** @internal */
328
429
  instanceId: string;
430
+ /** @internal */
329
431
  recoveryKind: "retry" | "continue";
432
+ /** @internal */
330
433
  turnRecordId?: string;
434
+ /** @internal */
331
435
  sessionId: string;
436
+ /** @internal */
332
437
  profiles: readonly ModelProfileOptionSummaryLike[];
438
+ /** @internal */
333
439
  now?: number;
440
+ /** @internal */
334
441
  ttlMs?: number;
335
442
  }): PendingRecoveryModelSession {
336
443
  return {
@@ -344,7 +451,11 @@ export function buildRecoveryModelSession(input: {
344
451
  };
345
452
  }
346
453
 
347
- export function buildRecoveryModelPrompt(input: { session: PendingRecoveryModelSession }): string {
454
+ /** @internal */
455
+ export function buildRecoveryModelPrompt(input: {
456
+ /** @internal */
457
+ session: PendingRecoveryModelSession;
458
+ }): string {
348
459
  const { session } = input;
349
460
  const label = session.recoveryKind === "retry" ? "Retry" : "Continue";
350
461
  const lines = [`Next-turn model for: ${label}`];