@leitwerk-dev/telegram 0.2.0 → 0.3.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.
@@ -14,52 +14,138 @@ import type {
14
14
  UiLauncherSummary,
15
15
  } from "@leitwerk-dev/process-sdk";
16
16
 
17
+ /** @internal */
17
18
  export type LaunchFieldValue = string | number | boolean;
18
19
 
20
+ /** @internal */
19
21
  export type LaunchModelStep =
20
- | { kind: "default"; key: "default"; label: string }
21
- | { kind: "turn"; key: string; turnId: string; label: string };
22
-
22
+ | {
23
+ /** @internal */
24
+ kind: "default";
25
+ /** @internal */
26
+ key: "default";
27
+ /** @internal */
28
+ label: string;
29
+ }
30
+ | {
31
+ /** @internal */
32
+ kind: "turn";
33
+ /** @internal */
34
+ key: string;
35
+ /** @internal */
36
+ turnId: string;
37
+ /** @internal */
38
+ label: string;
39
+ };
40
+
41
+ /** @internal */
23
42
  export interface PendingLaunchModelEdit {
43
+ /** @internal */
24
44
  stepIndex: number;
25
45
  }
26
46
 
47
+ /** @internal */
27
48
  export interface PendingLaunchSession {
49
+ /** @internal */
28
50
  kind: "launch";
51
+ /** @internal */
29
52
  launcherId: string;
53
+ /** @internal */
30
54
  fieldIndex: number;
55
+ /** @internal */
31
56
  fieldOrder: readonly string[];
57
+ /** @internal */
32
58
  values: Record<string, LaunchFieldValue>;
59
+ /** @internal */
33
60
  modelConfig: LaunchModelConfigInputLike;
61
+ /** @internal */
34
62
  modelConfigTouched: boolean;
63
+ /** @internal */
35
64
  modelEdit: PendingLaunchModelEdit | null;
65
+ /** @internal */
36
66
  expiresAt: number;
37
67
  }
38
68
 
69
+ /** @internal */
39
70
  export type LaunchFieldStepResult =
40
- | { ok: true; done: false; field: LauncherFieldDefinition }
41
- | { ok: true; done: true; values: Record<string, LaunchFieldValue> }
42
- | { ok: false; prompt: string };
43
-
71
+ | {
72
+ /** @internal */
73
+ ok: true;
74
+ /** @internal */
75
+ done: false;
76
+ /** @internal */
77
+ field: LauncherFieldDefinition;
78
+ }
79
+ | {
80
+ /** @internal */
81
+ ok: true;
82
+ /** @internal */
83
+ done: true;
84
+ /** @internal */
85
+ values: Record<string, LaunchFieldValue>;
86
+ }
87
+ | {
88
+ /** @internal */
89
+ ok: false;
90
+ /** @internal */
91
+ prompt: string;
92
+ };
93
+
94
+ /** @internal */
44
95
  export type LaunchModelStepResult =
45
- | { ok: true; done: false; step: LaunchModelStep }
46
- | { ok: true; done: true }
47
- | { ok: false; prompt: string };
48
-
96
+ | {
97
+ /** @internal */
98
+ ok: true;
99
+ /** @internal */
100
+ done: false;
101
+ /** @internal */
102
+ step: LaunchModelStep;
103
+ }
104
+ | {
105
+ /** @internal */
106
+ ok: true;
107
+ /** @internal */
108
+ done: true;
109
+ }
110
+ | {
111
+ /** @internal */
112
+ ok: false;
113
+ /** @internal */
114
+ prompt: string;
115
+ };
116
+
117
+ /** @internal */
49
118
  export type LaunchModelStepAction =
50
- | { action: "set"; value: string }
51
- | { action: "inherit" }
52
- | { action: "skip" };
53
-
119
+ | {
120
+ /** @internal */
121
+ action: "set";
122
+ /** @internal */
123
+ value: string;
124
+ }
125
+ | {
126
+ /** @internal */
127
+ action: "inherit";
128
+ }
129
+ | {
130
+ /** @internal */
131
+ action: "skip";
132
+ };
133
+
134
+ /** @internal */
54
135
  export type LaunchFieldOptionsById = Record<
55
136
  string,
56
137
  readonly LauncherFieldOptionDefinition[] | undefined
57
138
  >;
58
139
 
140
+ /** @internal */
59
141
  export function createLaunchSession(input: {
142
+ /** @internal */
60
143
  launcher: UiLauncherSummary;
144
+ /** @internal */
61
145
  defaults?: Record<string, unknown>;
146
+ /** @internal */
62
147
  now?: number;
148
+ /** @internal */
63
149
  ttlMs?: number;
64
150
  }): PendingLaunchSession {
65
151
  const values: Record<string, LaunchFieldValue> = {};
@@ -82,14 +168,17 @@ export function createLaunchSession(input: {
82
168
  };
83
169
  }
84
170
 
171
+ /** @internal */
85
172
  export function buildLaunchInput(session: PendingLaunchSession): PendingLaunchSession["values"] {
86
173
  return { ...session.values };
87
174
  }
88
175
 
176
+ /** @internal */
89
177
  export function buildLaunchModelConfig(session: PendingLaunchSession): LaunchModelConfigInputLike {
90
178
  return normalizeLaunchModelConfigInput(session.modelConfig);
91
179
  }
92
180
 
181
+ /** @internal */
93
182
  export function seedLaunchModelConfigFromPlan(
94
183
  session: PendingLaunchSession,
95
184
  launchPlan: ProcessLaunchPlan,
@@ -105,6 +194,7 @@ export function seedLaunchModelConfigFromPlan(
105
194
  });
106
195
  }
107
196
 
197
+ /** @internal */
108
198
  export function hasLaunchModelControls(
109
199
  schema: LauncherModelConfigSchemaLike | null | undefined,
110
200
  ): schema is LauncherModelConfigSchemaLike {
@@ -123,6 +213,7 @@ function buildLaunchModelStepOrder(schema: LauncherModelConfigSchemaLike): Launc
123
213
  ];
124
214
  }
125
215
 
216
+ /** @internal */
126
217
  export function beginLaunchModelEdit(
127
218
  session: PendingLaunchSession,
128
219
  schema: LauncherModelConfigSchemaLike,
@@ -131,6 +222,7 @@ export function beginLaunchModelEdit(
131
222
  return { ok: true, done: false, step: buildLaunchModelStepOrder(schema)[0] as LaunchModelStep };
132
223
  }
133
224
 
225
+ /** @internal */
134
226
  export function currentLaunchModelStep(
135
227
  session: PendingLaunchSession,
136
228
  schema: LauncherModelConfigSchemaLike,
@@ -139,10 +231,12 @@ export function currentLaunchModelStep(
139
231
  return buildLaunchModelStepOrder(schema)[session.modelEdit.stepIndex] ?? null;
140
232
  }
141
233
 
234
+ /** @internal */
142
235
  export function finishLaunchModelEdit(session: PendingLaunchSession): void {
143
236
  session.modelEdit = null;
144
237
  }
145
238
 
239
+ /** @internal */
146
240
  export function currentLaunchField(
147
241
  session: PendingLaunchSession,
148
242
  launcher: UiLauncherSummary,
@@ -153,10 +247,12 @@ export function currentLaunchField(
153
247
  : null;
154
248
  }
155
249
 
250
+ /** @internal */
156
251
  export function isLaunchSessionComplete(session: PendingLaunchSession): boolean {
157
252
  return session.fieldIndex >= session.fieldOrder.length;
158
253
  }
159
254
 
255
+ /** @internal */
160
256
  export function getLaunchFieldOptions(
161
257
  field: LauncherFieldDefinition,
162
258
  dynamicOptionsById: LaunchFieldOptionsById,
@@ -165,11 +261,17 @@ export function getLaunchFieldOptions(
165
261
  return dynamicOptionsById[field.id] ?? field.options ?? [];
166
262
  }
167
263
 
264
+ /** @internal */
168
265
  export function buildLaunchFieldPrompt(input: {
266
+ /** @internal */
169
267
  field: LauncherFieldDefinition;
268
+ /** @internal */
170
269
  currentValue?: LaunchFieldValue;
270
+ /** @internal */
171
271
  options?: readonly LauncherFieldOptionDefinition[];
272
+ /** @internal */
172
273
  recentValues?: readonly string[];
274
+ /** @internal */
173
275
  maxOptionsInPrompt?: number;
174
276
  }): string {
175
277
  const { field } = input;
@@ -208,9 +310,13 @@ export function buildLaunchFieldPrompt(input: {
208
310
  return lines.join("\n");
209
311
  }
210
312
 
313
+ /** @internal */
211
314
  export function buildLaunchModelSummary(input: {
315
+ /** @internal */
212
316
  schema: LauncherModelConfigSchemaLike;
317
+ /** @internal */
213
318
  session: PendingLaunchSession;
319
+ /** @internal */
214
320
  preview?: LauncherModelConfigPreviewLike | null;
215
321
  }): string {
216
322
  const config = buildLaunchModelConfig(input.session);
@@ -234,11 +340,17 @@ export function buildLaunchModelSummary(input: {
234
340
  return lines.join("\n");
235
341
  }
236
342
 
343
+ /** @internal */
237
344
  export function buildLaunchModelStepPrompt(input: {
345
+ /** @internal */
238
346
  step: LaunchModelStep;
347
+ /** @internal */
239
348
  schema: LauncherModelConfigSchemaLike;
349
+ /** @internal */
240
350
  session: PendingLaunchSession;
351
+ /** @internal */
241
352
  preview?: LauncherModelConfigPreviewLike | null;
353
+ /** @internal */
242
354
  maxOptionsInPrompt?: number;
243
355
  }): string {
244
356
  const currentOverride = getLaunchModelStepOverride(input.session, input.step);
@@ -262,6 +374,7 @@ export function buildLaunchModelStepPrompt(input: {
262
374
  return lines.join("\n");
263
375
  }
264
376
 
377
+ /** @internal */
265
378
  export function applyLaunchModelStepText(
266
379
  session: PendingLaunchSession,
267
380
  schema: LauncherModelConfigSchemaLike,
@@ -287,6 +400,7 @@ export function applyLaunchModelStepText(
287
400
  : launchModelStepError(step, schema, session);
288
401
  }
289
402
 
403
+ /** @internal */
290
404
  export function applyLaunchModelStepAction(
291
405
  session: PendingLaunchSession,
292
406
  schema: LauncherModelConfigSchemaLike,
@@ -316,6 +430,7 @@ function advanceLaunchField(
316
430
  : { ok: true, done: true, values: buildLaunchInput(session) };
317
431
  }
318
432
 
433
+ /** @internal */
319
434
  export function applyLaunchFieldText(
320
435
  session: PendingLaunchSession,
321
436
  launcher: UiLauncherSummary,
@@ -348,6 +463,7 @@ export function applyLaunchFieldText(
348
463
  return advanceLaunchField(session, launcher);
349
464
  }
350
465
 
466
+ /** @internal */
351
467
  export function applyLaunchFieldValue(
352
468
  session: PendingLaunchSession,
353
469
  launcher: UiLauncherSummary,
@@ -374,6 +490,7 @@ export function applyLaunchFieldValue(
374
490
  return advanceLaunchField(session, launcher);
375
491
  }
376
492
 
493
+ /** @internal */
377
494
  export function moveLaunchSessionToValidationField(
378
495
  session: PendingLaunchSession,
379
496
  launcher: UiLauncherSummary,
@@ -397,8 +514,11 @@ function buildFieldOrder(launcher: UiLauncherSummary): string[] {
397
514
  ];
398
515
  }
399
516
 
517
+ /** @internal */
400
518
  export function buildLaunchReview(input: {
519
+ /** @internal */
401
520
  launcher: UiLauncherSummary;
521
+ /** @internal */
402
522
  session: PendingLaunchSession;
403
523
  }): string {
404
524
  const lines = [`Ready to start ${input.launcher.label}.`, "", "Launcher config:"];
@@ -50,11 +50,32 @@ const markdown = new MarkdownIt("commonmark", {
50
50
  linkify: false,
51
51
  });
52
52
 
53
+ /** @internal */
53
54
  export type TelegramResultVisual =
54
- | { kind: "image"; imageId: string; alt: string }
55
- | { kind: "mermaid"; source: string };
56
-
57
- export type TelegramResultPart = { kind: "html"; html: string } | TelegramResultVisual;
55
+ | {
56
+ /** @internal */
57
+ kind: "image";
58
+ /** @internal */
59
+ imageId: string;
60
+ /** @internal */
61
+ alt: string;
62
+ }
63
+ | {
64
+ /** @internal */
65
+ kind: "mermaid";
66
+ /** @internal */
67
+ source: string;
68
+ };
69
+
70
+ /** @internal */
71
+ export type TelegramResultPart =
72
+ | {
73
+ /** @internal */
74
+ kind: "html";
75
+ /** @internal */
76
+ html: string;
77
+ }
78
+ | TelegramResultVisual;
58
79
 
59
80
  markdown.renderer.rules.image = (tokens, index) => escapeTelegramHtml(tokens[index]?.content ?? "");
60
81
 
@@ -62,10 +83,12 @@ type Parse5Node = DefaultTreeAdapterMap["node"];
62
83
  type Parse5Element = DefaultTreeAdapterMap["element"];
63
84
  type Parse5TextNode = DefaultTreeAdapterMap["textNode"];
64
85
 
86
+ /** @internal */
65
87
  export function escapeTelegramHtml(value: string): string {
66
88
  return value.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
67
89
  }
68
90
 
91
+ /** @internal */
69
92
  export function escapeTelegramAttribute(value: string): string {
70
93
  return escapeTelegramHtml(value).replaceAll('"', "&quot;");
71
94
  }
@@ -150,6 +173,7 @@ function normalizeTelegramHtml(html: string): string {
150
173
  return parseFragment(html).childNodes.map(renderNode).join("").trim();
151
174
  }
152
175
 
176
+ /** @internal */
153
177
  export function telegramHtmlToPlainText(html: string): string {
154
178
  return parseFragment(normalizeTelegramHtml(html)).childNodes.map(collectText).join("").trim();
155
179
  }
@@ -202,6 +226,7 @@ function splitEncodedText(
202
226
  return chunks;
203
227
  }
204
228
 
229
+ /** @internal */
205
230
  export function splitPlainTelegramText(
206
231
  value: string,
207
232
  maxChars = DEFAULT_TELEGRAM_SOFT_LIMIT,
@@ -209,6 +234,7 @@ export function splitPlainTelegramText(
209
234
  return splitEncodedText(value, maxChars, (char) => char);
210
235
  }
211
236
 
237
+ /** @internal */
212
238
  export function splitTelegramHtml(value: string, maxChars = DEFAULT_TELEGRAM_SOFT_LIMIT): string[] {
213
239
  const normalized = normalizeLineEndings(value);
214
240
  if (!normalized) return [];
@@ -221,6 +247,7 @@ export function splitTelegramHtml(value: string, maxChars = DEFAULT_TELEGRAM_SOF
221
247
  : splitEncodedText(telegramHtmlToPlainText(safeHtml), max, escapeTelegramHtml);
222
248
  }
223
249
 
250
+ /** @internal */
224
251
  export function renderMarkdownToTelegramHtml(markdownText: string): string {
225
252
  const normalized = normalizeLineEndings(markdownText);
226
253
  return normalized ? normalizeTelegramHtml(markdown.render(normalized)) : "";
@@ -249,9 +276,13 @@ function resultVisual(
249
276
  * ordinary rendered Markdown so media delivery never has to reconstruct open
250
277
  * formatting containers around Telegram uploads.
251
278
  */
279
+ /** @internal */
252
280
  export function renderResultMarkdownForTelegram(input: {
281
+ /** @internal */
253
282
  markdown: string;
283
+ /** @internal */
254
284
  instanceId: string;
285
+ /** @internal */
255
286
  turnRecordId: string;
256
287
  }): TelegramResultPart[] {
257
288
  const normalized = normalizeLineEndings(input.markdown);
@@ -2,6 +2,7 @@ import type { ProcessEvent, ProcessInstance } from "@leitwerk-dev/domain";
2
2
  import type { CoreServerSetupDeps } from "@leitwerk-dev/process-sdk";
3
3
  import type { TelegramProcessThread } from "./types.js";
4
4
 
5
+ /** @internal */
5
6
  export const TELEGRAM_THREAD_LINKED_EVENT = "telegram.thread_linked";
6
7
 
7
8
  function readThreadFromEvent(event: ProcessEvent): TelegramProcessThread | null {
@@ -32,16 +33,20 @@ type ProcessEventRepoWithTypedLookup = CoreServerSetupDeps["events"] & {
32
33
  ) => ProcessEvent[];
33
34
  };
34
35
 
36
+ /** @internal */
35
37
  export function threadKey(chatId: string, messageThreadId: number | undefined): string {
36
38
  return `${chatId}:${messageThreadId ?? 0}`;
37
39
  }
38
40
 
41
+ /** @internal */
39
42
  export class ProcessThreadStore {
40
43
  private readonly byInstanceId = new Map<string, TelegramProcessThread>();
41
44
  private readonly byThreadKey = new Map<string, string>();
42
45
 
46
+ /** @internal */
43
47
  constructor(private readonly deps: Pick<CoreServerSetupDeps, "events" | "processes">) {}
44
48
 
49
+ /** @internal */
45
50
  rebuildFromEvents(): readonly string[] {
46
51
  this.byInstanceId.clear();
47
52
  this.byThreadKey.clear();
@@ -58,14 +63,17 @@ export class ProcessThreadStore {
58
63
  return mappedInstanceIds;
59
64
  }
60
65
 
66
+ /** @internal */
61
67
  getByInstanceId(instanceId: string): TelegramProcessThread | null {
62
68
  return this.byInstanceId.get(instanceId) ?? null;
63
69
  }
64
70
 
71
+ /** @internal */
65
72
  getInstanceIdForThread(chatId: string, messageThreadId: number | undefined): string | null {
66
73
  return this.byThreadKey.get(threadKey(chatId, messageThreadId)) ?? null;
67
74
  }
68
75
 
76
+ /** @internal */
69
77
  record(process: Pick<ProcessInstance, "id">, thread: TelegramProcessThread): void {
70
78
  this.deps.events.create({
71
79
  instanceId: process.id,
@@ -14,6 +14,7 @@ function truncate(value: string, max: number): string {
14
14
  return `${value.slice(0, Math.max(1, max - 1)).trimEnd()}…`;
15
15
  }
16
16
 
17
+ /** @internal */
17
18
  export function getProcessTopicBaseTitle(process: ProcessInstance): string {
18
19
  return (
19
20
  flatten(process.title ?? "") ||
@@ -23,7 +24,13 @@ export function getProcessTopicBaseTitle(process: ProcessInstance): string {
23
24
  );
24
25
  }
25
26
 
26
- export function buildTopicTitle(input: { process: ProcessInstance; template: string }): string {
27
+ /** @internal */
28
+ export function buildTopicTitle(input: {
29
+ /** @internal */
30
+ process: ProcessInstance;
31
+ /** @internal */
32
+ template: string;
33
+ }): string {
27
34
  const shortId = input.process.id.slice(-8);
28
35
  const title = getProcessTopicBaseTitle(input.process);
29
36
  const rendered = input.template