@leitwerk-dev/telegram 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +145 -0
- package/dist/LICENSE +13 -0
- package/dist/actions.d.ts +9 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +32 -0
- package/dist/actions.js.map +1 -0
- package/dist/bridge.d.ts +132 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +2032 -0
- package/dist/bridge.js.map +1 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +70 -0
- package/dist/config.js.map +1 -0
- package/dist/fake-telegram-client.d.ts +82 -0
- package/dist/fake-telegram-client.d.ts.map +1 -0
- package/dist/fake-telegram-client.js +107 -0
- package/dist/fake-telegram-client.js.map +1 -0
- package/dist/form-session.d.ts +143 -0
- package/dist/form-session.d.ts.map +1 -0
- package/dist/form-session.js +218 -0
- package/dist/form-session.js.map +1 -0
- package/dist/grammy-telegram-client.d.ts +131 -0
- package/dist/grammy-telegram-client.d.ts.map +1 -0
- package/dist/grammy-telegram-client.js +151 -0
- package/dist/grammy-telegram-client.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/launch-session.d.ts +103 -0
- package/dist/launch-session.d.ts.map +1 -0
- package/dist/launch-session.js +435 -0
- package/dist/launch-session.js.map +1 -0
- package/dist/markdown-html.d.ts +29 -0
- package/dist/markdown-html.d.ts.map +1 -0
- package/dist/markdown-html.js +259 -0
- package/dist/markdown-html.js.map +1 -0
- package/dist/mermaid-image.d.ts +2 -0
- package/dist/mermaid-image.d.ts.map +1 -0
- package/dist/mermaid-image.js +99 -0
- package/dist/mermaid-image.js.map +1 -0
- package/dist/process-feed.d.ts +58 -0
- package/dist/process-feed.d.ts.map +1 -0
- package/dist/process-feed.js +165 -0
- package/dist/process-feed.js.map +1 -0
- package/dist/process-thread-store.d.ts +18 -0
- package/dist/process-thread-store.d.ts.map +1 -0
- package/dist/process-thread-store.js +74 -0
- package/dist/process-thread-store.js.map +1 -0
- package/dist/topic-title.d.ts +7 -0
- package/dist/topic-title.d.ts.map +1 -0
- package/dist/topic-title.js +29 -0
- package/dist/topic-title.js.map +1 -0
- package/dist/types.d.ts +118 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/package.json +57 -0
- package/src/actions.ts +41 -0
- package/src/bridge.ts +2870 -0
- package/src/config.ts +108 -0
- package/src/fake-telegram-client.ts +167 -0
- package/src/form-session.ts +359 -0
- package/src/grammy-telegram-client.ts +292 -0
- package/src/index.ts +68 -0
- package/src/launch-session.ts +620 -0
- package/src/markdown-html.ts +297 -0
- package/src/mermaid-image.ts +99 -0
- package/src/process-feed.ts +237 -0
- package/src/process-thread-store.ts +98 -0
- package/src/topic-title.ts +35 -0
- package/src/types.ts +138 -0
|
@@ -0,0 +1,620 @@
|
|
|
1
|
+
import {
|
|
2
|
+
normalizeLaunchModelConfigInput,
|
|
3
|
+
parseStrictInstanceTurnConfigsJson,
|
|
4
|
+
trimToNull,
|
|
5
|
+
} from "@leitwerk-dev/domain";
|
|
6
|
+
import type {
|
|
7
|
+
LauncherFieldDefinition,
|
|
8
|
+
LauncherFieldOptionDefinition,
|
|
9
|
+
LauncherModelConfigPreviewLike,
|
|
10
|
+
LauncherModelConfigSchemaLike,
|
|
11
|
+
LauncherValidationError,
|
|
12
|
+
LaunchModelConfigInputLike,
|
|
13
|
+
ProcessLaunchPlan,
|
|
14
|
+
UiLauncherSummary,
|
|
15
|
+
} from "@leitwerk-dev/process-sdk";
|
|
16
|
+
|
|
17
|
+
export type LaunchFieldValue = string | number | boolean;
|
|
18
|
+
|
|
19
|
+
export type LaunchModelStep =
|
|
20
|
+
| { kind: "default"; key: "default"; label: string }
|
|
21
|
+
| { kind: "turn"; key: string; turnId: string; label: string };
|
|
22
|
+
|
|
23
|
+
export interface PendingLaunchModelEdit {
|
|
24
|
+
stepIndex: number;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export interface PendingLaunchSession {
|
|
28
|
+
kind: "launch";
|
|
29
|
+
launcherId: string;
|
|
30
|
+
fieldIndex: number;
|
|
31
|
+
fieldOrder: readonly string[];
|
|
32
|
+
values: Record<string, LaunchFieldValue>;
|
|
33
|
+
modelConfig: LaunchModelConfigInputLike;
|
|
34
|
+
modelConfigTouched: boolean;
|
|
35
|
+
modelEdit: PendingLaunchModelEdit | null;
|
|
36
|
+
expiresAt: number;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
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
|
+
|
|
44
|
+
export type LaunchModelStepResult =
|
|
45
|
+
| { ok: true; done: false; step: LaunchModelStep }
|
|
46
|
+
| { ok: true; done: true }
|
|
47
|
+
| { ok: false; prompt: string };
|
|
48
|
+
|
|
49
|
+
export type LaunchModelStepAction =
|
|
50
|
+
| { action: "set"; value: string }
|
|
51
|
+
| { action: "inherit" }
|
|
52
|
+
| { action: "skip" };
|
|
53
|
+
|
|
54
|
+
export type LaunchFieldOptionsById = Record<
|
|
55
|
+
string,
|
|
56
|
+
readonly LauncherFieldOptionDefinition[] | undefined
|
|
57
|
+
>;
|
|
58
|
+
|
|
59
|
+
export function createLaunchSession(input: {
|
|
60
|
+
launcher: UiLauncherSummary;
|
|
61
|
+
defaults?: Record<string, unknown>;
|
|
62
|
+
now?: number;
|
|
63
|
+
ttlMs?: number;
|
|
64
|
+
}): PendingLaunchSession {
|
|
65
|
+
const values: Record<string, LaunchFieldValue> = {};
|
|
66
|
+
for (const field of input.launcher.launchConfigSchema.fields) {
|
|
67
|
+
const normalized = normalizeFieldValue(field, input.defaults?.[field.id]);
|
|
68
|
+
if (normalized.ok) {
|
|
69
|
+
values[field.id] = normalized.value;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
kind: "launch",
|
|
74
|
+
launcherId: input.launcher.id,
|
|
75
|
+
fieldIndex: 0,
|
|
76
|
+
fieldOrder: buildFieldOrder(input.launcher),
|
|
77
|
+
values,
|
|
78
|
+
modelConfig: { defaultModelProfileId: null, turnConfigs: {} },
|
|
79
|
+
modelConfigTouched: false,
|
|
80
|
+
modelEdit: null,
|
|
81
|
+
expiresAt: (input.now ?? Date.now()) + (input.ttlMs ?? 20 * 60 * 1000),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
export function buildLaunchInput(session: PendingLaunchSession): Record<string, unknown> {
|
|
86
|
+
return { ...session.values };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function buildLaunchModelConfig(session: PendingLaunchSession): LaunchModelConfigInputLike {
|
|
90
|
+
return normalizeLaunchModelConfigInput(session.modelConfig);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function seedLaunchModelConfigFromPlan(
|
|
94
|
+
session: PendingLaunchSession,
|
|
95
|
+
launchPlan: ProcessLaunchPlan,
|
|
96
|
+
): void {
|
|
97
|
+
if (session.modelConfigTouched) return;
|
|
98
|
+
const parsedTurnConfigs = parseStrictInstanceTurnConfigsJson(
|
|
99
|
+
launchPlan.processId,
|
|
100
|
+
launchPlan.processInput.turnConfigsJson,
|
|
101
|
+
);
|
|
102
|
+
session.modelConfig = normalizeLaunchModelConfigInput({
|
|
103
|
+
defaultModelProfileId: launchPlan.processInput.defaultModelProfileId ?? null,
|
|
104
|
+
turnConfigs: parsedTurnConfigs.ok ? parsedTurnConfigs.value : {},
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export function hasLaunchModelControls(
|
|
109
|
+
schema: LauncherModelConfigSchemaLike | null | undefined,
|
|
110
|
+
): schema is LauncherModelConfigSchemaLike {
|
|
111
|
+
return Boolean(schema && schema.availableProfiles.length > 0 && schema.llmTurns.length > 0);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
function buildLaunchModelStepOrder(schema: LauncherModelConfigSchemaLike): LaunchModelStep[] {
|
|
115
|
+
return [
|
|
116
|
+
{ kind: "default", key: "default", label: "Default model" },
|
|
117
|
+
...schema.llmTurns.map((turn) => ({
|
|
118
|
+
kind: "turn" as const,
|
|
119
|
+
key: `turn:${turn.turnId}`,
|
|
120
|
+
turnId: turn.turnId,
|
|
121
|
+
label: turn.description || turn.turnId,
|
|
122
|
+
})),
|
|
123
|
+
];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
export function beginLaunchModelEdit(
|
|
127
|
+
session: PendingLaunchSession,
|
|
128
|
+
schema: LauncherModelConfigSchemaLike,
|
|
129
|
+
): LaunchModelStepResult {
|
|
130
|
+
session.modelEdit = { stepIndex: 0 };
|
|
131
|
+
return { ok: true, done: false, step: buildLaunchModelStepOrder(schema)[0] as LaunchModelStep };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function currentLaunchModelStep(
|
|
135
|
+
session: PendingLaunchSession,
|
|
136
|
+
schema: LauncherModelConfigSchemaLike,
|
|
137
|
+
): LaunchModelStep | null {
|
|
138
|
+
if (!session.modelEdit) return null;
|
|
139
|
+
return buildLaunchModelStepOrder(schema)[session.modelEdit.stepIndex] ?? null;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function finishLaunchModelEdit(session: PendingLaunchSession): void {
|
|
143
|
+
session.modelEdit = null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export function currentLaunchField(
|
|
147
|
+
session: PendingLaunchSession,
|
|
148
|
+
launcher: UiLauncherSummary,
|
|
149
|
+
): LauncherFieldDefinition | null {
|
|
150
|
+
const fieldId = session.fieldOrder[session.fieldIndex];
|
|
151
|
+
return fieldId
|
|
152
|
+
? (launcher.launchConfigSchema.fields.find((field) => field.id === fieldId) ?? null)
|
|
153
|
+
: null;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
export function isLaunchSessionComplete(session: PendingLaunchSession): boolean {
|
|
157
|
+
return session.fieldIndex >= session.fieldOrder.length;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
export function getLaunchFieldOptions(
|
|
161
|
+
field: LauncherFieldDefinition,
|
|
162
|
+
dynamicOptionsById: LaunchFieldOptionsById,
|
|
163
|
+
): readonly LauncherFieldOptionDefinition[] {
|
|
164
|
+
if (field.kind !== "select") return [];
|
|
165
|
+
return dynamicOptionsById[field.id] ?? field.options ?? [];
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function buildLaunchFieldPrompt(input: {
|
|
169
|
+
field: LauncherFieldDefinition;
|
|
170
|
+
currentValue?: LaunchFieldValue;
|
|
171
|
+
options?: readonly LauncherFieldOptionDefinition[];
|
|
172
|
+
recentValues?: readonly string[];
|
|
173
|
+
maxOptionsInPrompt?: number;
|
|
174
|
+
}): string {
|
|
175
|
+
const { field } = input;
|
|
176
|
+
const lines = [`${field.required ? "Required" : "Optional"} field: ${field.label}`];
|
|
177
|
+
if (field.description?.trim()) lines.push(field.description.trim());
|
|
178
|
+
const current = formatLaunchFieldValue(input.currentValue);
|
|
179
|
+
if (current) lines.push(`Current/default: ${current}`);
|
|
180
|
+
if (field.kind === "boolean") {
|
|
181
|
+
lines.push("Reply yes or no, or use the buttons.");
|
|
182
|
+
} else if (field.kind === "number") {
|
|
183
|
+
lines.push("Reply with a number.");
|
|
184
|
+
} else if (field.kind === "select") {
|
|
185
|
+
const options = input.options ?? [];
|
|
186
|
+
if (options.length > 0) {
|
|
187
|
+
lines.push("Choose one option by button, number, value, or label:");
|
|
188
|
+
const max = input.maxOptionsInPrompt ?? 12;
|
|
189
|
+
for (const [index, option] of options.slice(0, max).entries()) {
|
|
190
|
+
lines.push(
|
|
191
|
+
`${index + 1}. ${option.label}${option.description ? ` — ${option.description}` : ""}`,
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
if (options.length > max) {
|
|
195
|
+
lines.push(`…and ${options.length - max} more. Use a button or type the exact value.`);
|
|
196
|
+
}
|
|
197
|
+
} else {
|
|
198
|
+
lines.push("Reply with a value for this field.");
|
|
199
|
+
}
|
|
200
|
+
} else if (input.recentValues?.length) {
|
|
201
|
+
lines.push("Choose a recent value or reply with a different value.");
|
|
202
|
+
} else {
|
|
203
|
+
lines.push("Reply with the value for this field.");
|
|
204
|
+
}
|
|
205
|
+
if (!field.required || hasPresentValue(input.currentValue)) {
|
|
206
|
+
lines.push("Send /skip to keep the current/default value or leave it blank.");
|
|
207
|
+
}
|
|
208
|
+
return lines.join("\n");
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export function buildLaunchModelSummary(input: {
|
|
212
|
+
schema: LauncherModelConfigSchemaLike;
|
|
213
|
+
session: PendingLaunchSession;
|
|
214
|
+
preview?: LauncherModelConfigPreviewLike | null;
|
|
215
|
+
}): string {
|
|
216
|
+
const config = buildLaunchModelConfig(input.session);
|
|
217
|
+
const lines = ["Model setup:"];
|
|
218
|
+
const defaultProfileId = trimToNull(config.defaultModelProfileId);
|
|
219
|
+
const defaultPreview = input.preview?.defaultModel.profile;
|
|
220
|
+
lines.push(
|
|
221
|
+
`- Default: ${defaultProfileId ? formatLaunchModelProfile(input.schema, defaultProfileId) : (defaultPreview?.label ?? "inherited/recommended")}${
|
|
222
|
+
defaultProfileId ? " (override)" : ""
|
|
223
|
+
}`,
|
|
224
|
+
);
|
|
225
|
+
for (const turn of input.schema.llmTurns) {
|
|
226
|
+
const override = trimToNull(config.turnConfigs?.[turn.turnId]?.modelProfileId);
|
|
227
|
+
const preview = input.preview?.turns.find((candidate) => candidate.turnId === turn.turnId);
|
|
228
|
+
lines.push(
|
|
229
|
+
`- ${turn.description || turn.turnId}: ${override ? formatLaunchModelProfile(input.schema, override) : (preview?.effective.profile?.label ?? "inherited/recommended")}${
|
|
230
|
+
override ? " (override)" : ""
|
|
231
|
+
}`,
|
|
232
|
+
);
|
|
233
|
+
}
|
|
234
|
+
return lines.join("\n");
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export function buildLaunchModelStepPrompt(input: {
|
|
238
|
+
step: LaunchModelStep;
|
|
239
|
+
schema: LauncherModelConfigSchemaLike;
|
|
240
|
+
session: PendingLaunchSession;
|
|
241
|
+
preview?: LauncherModelConfigPreviewLike | null;
|
|
242
|
+
maxOptionsInPrompt?: number;
|
|
243
|
+
}): string {
|
|
244
|
+
const currentOverride = getLaunchModelStepOverride(input.session, input.step);
|
|
245
|
+
const effective = getLaunchModelStepPreview(input.preview, input.step);
|
|
246
|
+
const lines = [`Model setup: ${input.step.label}`];
|
|
247
|
+
lines.push(
|
|
248
|
+
`Current override: ${currentOverride ? formatLaunchModelProfile(input.schema, currentOverride) : "inherited/recommended"}`,
|
|
249
|
+
);
|
|
250
|
+
if (effective) lines.push(`Effective now: ${effective}`);
|
|
251
|
+
lines.push("Choose a model by button, number, or profile id.");
|
|
252
|
+
const max = input.maxOptionsInPrompt ?? 12;
|
|
253
|
+
for (const [index, profile] of input.schema.availableProfiles.slice(0, max).entries()) {
|
|
254
|
+
lines.push(`${index + 1}. ${profile.label}`);
|
|
255
|
+
}
|
|
256
|
+
if (input.schema.availableProfiles.length > max) {
|
|
257
|
+
lines.push(
|
|
258
|
+
`…and ${input.schema.availableProfiles.length - max} more. Use a button or type the exact profile id.`,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
lines.push("Send /skip to keep this value, or use Inherit to clear an override.");
|
|
262
|
+
return lines.join("\n");
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
export function applyLaunchModelStepText(
|
|
266
|
+
session: PendingLaunchSession,
|
|
267
|
+
schema: LauncherModelConfigSchemaLike,
|
|
268
|
+
text: string,
|
|
269
|
+
): LaunchModelStepResult {
|
|
270
|
+
const step = currentLaunchModelStep(session, schema);
|
|
271
|
+
if (!step) return { ok: true, done: true };
|
|
272
|
+
const trimmed = text.trim();
|
|
273
|
+
const normalized = trimmed.toLowerCase();
|
|
274
|
+
if (/^\/skip(?:@\w+)?$/i.test(trimmed) || normalized === "skip" || !trimmed) {
|
|
275
|
+
return applyLaunchModelStepAction(session, schema, { action: "skip" });
|
|
276
|
+
}
|
|
277
|
+
if (["inherit", "inherited", "clear", "none"].includes(normalized)) {
|
|
278
|
+
return applyLaunchModelStepAction(session, schema, { action: "inherit" });
|
|
279
|
+
}
|
|
280
|
+
const matched = matchModelProfile(trimmed, schema);
|
|
281
|
+
return matched
|
|
282
|
+
? applyLaunchModelStepAction(session, schema, { action: "set", value: matched.id })
|
|
283
|
+
: launchModelStepError(step, schema, session);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
export function applyLaunchModelStepAction(
|
|
287
|
+
session: PendingLaunchSession,
|
|
288
|
+
schema: LauncherModelConfigSchemaLike,
|
|
289
|
+
action: LaunchModelStepAction,
|
|
290
|
+
): LaunchModelStepResult {
|
|
291
|
+
const step = currentLaunchModelStep(session, schema);
|
|
292
|
+
if (!step) return { ok: true, done: true };
|
|
293
|
+
if (action.action === "skip") return advanceLaunchModelStep(session, schema);
|
|
294
|
+
if (action.action === "inherit") {
|
|
295
|
+
clearLaunchModelStepOverride(session, step);
|
|
296
|
+
return advanceLaunchModelStep(session, schema);
|
|
297
|
+
}
|
|
298
|
+
const matched = schema.availableProfiles.find((profile) => profile.id === action.value);
|
|
299
|
+
if (!matched) return launchModelStepError(step, schema, session);
|
|
300
|
+
setLaunchModelStepOverride(session, step, matched.id);
|
|
301
|
+
return advanceLaunchModelStep(session, schema);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
export function applyLaunchFieldText(
|
|
305
|
+
session: PendingLaunchSession,
|
|
306
|
+
launcher: UiLauncherSummary,
|
|
307
|
+
text: string,
|
|
308
|
+
dynamicOptionsById: LaunchFieldOptionsById = {},
|
|
309
|
+
): LaunchFieldStepResult {
|
|
310
|
+
const field = currentLaunchField(session, launcher);
|
|
311
|
+
if (!field) {
|
|
312
|
+
return {
|
|
313
|
+
ok: true,
|
|
314
|
+
done: true,
|
|
315
|
+
values: buildLaunchInput(session) as Record<string, LaunchFieldValue>,
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
const options = getLaunchFieldOptions(field, dynamicOptionsById);
|
|
319
|
+
const trimmed = text.trim();
|
|
320
|
+
const wantsSkip = /^\/skip(?:@\w+)?$/i.test(trimmed) || trimmed.toLowerCase() === "skip";
|
|
321
|
+
const parsed = wantsSkip
|
|
322
|
+
? skipField(field, session.values[field.id])
|
|
323
|
+
: parseFieldText(field, text, options);
|
|
324
|
+
if (!parsed.ok) {
|
|
325
|
+
return {
|
|
326
|
+
ok: false,
|
|
327
|
+
prompt: `${parsed.error}\n${buildLaunchFieldPrompt({ field, currentValue: session.values[field.id], options })}`,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
if (parsed.setValue) {
|
|
331
|
+
session.values[field.id] = parsed.value;
|
|
332
|
+
}
|
|
333
|
+
session.fieldIndex += 1;
|
|
334
|
+
const next = currentLaunchField(session, launcher);
|
|
335
|
+
return next
|
|
336
|
+
? { ok: true, done: false, field: next }
|
|
337
|
+
: {
|
|
338
|
+
ok: true,
|
|
339
|
+
done: true,
|
|
340
|
+
values: buildLaunchInput(session) as Record<string, LaunchFieldValue>,
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
export function applyLaunchFieldValue(
|
|
345
|
+
session: PendingLaunchSession,
|
|
346
|
+
launcher: UiLauncherSummary,
|
|
347
|
+
value: unknown,
|
|
348
|
+
dynamicOptionsById: LaunchFieldOptionsById = {},
|
|
349
|
+
): LaunchFieldStepResult {
|
|
350
|
+
const field = currentLaunchField(session, launcher);
|
|
351
|
+
if (!field) {
|
|
352
|
+
return {
|
|
353
|
+
ok: true,
|
|
354
|
+
done: true,
|
|
355
|
+
values: buildLaunchInput(session) as Record<string, LaunchFieldValue>,
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
const parsed = normalizeFieldValue(field, value);
|
|
359
|
+
if (!parsed.ok || (field.required && !hasPresentValue(parsed.value))) {
|
|
360
|
+
const options = getLaunchFieldOptions(field, dynamicOptionsById);
|
|
361
|
+
return {
|
|
362
|
+
ok: false,
|
|
363
|
+
prompt: `${parsed.ok ? `${field.label} is required.` : parsed.error}\n${buildLaunchFieldPrompt({ field, currentValue: session.values[field.id], options })}`,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
session.values[field.id] = parsed.value;
|
|
367
|
+
session.fieldIndex += 1;
|
|
368
|
+
const next = currentLaunchField(session, launcher);
|
|
369
|
+
return next
|
|
370
|
+
? { ok: true, done: false, field: next }
|
|
371
|
+
: {
|
|
372
|
+
ok: true,
|
|
373
|
+
done: true,
|
|
374
|
+
values: buildLaunchInput(session) as Record<string, LaunchFieldValue>,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export function moveLaunchSessionToValidationField(
|
|
379
|
+
session: PendingLaunchSession,
|
|
380
|
+
launcher: UiLauncherSummary,
|
|
381
|
+
errors: readonly LauncherValidationError[],
|
|
382
|
+
): LauncherFieldDefinition | null {
|
|
383
|
+
const fieldId = errors.find((error) => error.fieldId)?.fieldId;
|
|
384
|
+
if (!fieldId) return null;
|
|
385
|
+
const field = launcher.launchConfigSchema.fields.find((candidate) => candidate.id === fieldId);
|
|
386
|
+
if (!field) return null;
|
|
387
|
+
const index = session.fieldOrder.indexOf(fieldId);
|
|
388
|
+
if (index < 0) return null;
|
|
389
|
+
session.fieldIndex = index;
|
|
390
|
+
return field;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
function buildFieldOrder(launcher: UiLauncherSummary): string[] {
|
|
394
|
+
const fields = launcher.launchConfigSchema.fields;
|
|
395
|
+
return [
|
|
396
|
+
...fields.filter((field) => field.required).map((field) => field.id),
|
|
397
|
+
...fields.filter((field) => !field.required).map((field) => field.id),
|
|
398
|
+
];
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
export function buildLaunchReview(input: {
|
|
402
|
+
launcher: UiLauncherSummary;
|
|
403
|
+
session: PendingLaunchSession;
|
|
404
|
+
}): string {
|
|
405
|
+
const lines = [`Ready to start ${input.launcher.label}.`, "", "Launcher config:"];
|
|
406
|
+
for (const field of input.launcher.launchConfigSchema.fields) {
|
|
407
|
+
lines.push(
|
|
408
|
+
`- ${field.label}: ${formatLaunchFieldValue(input.session.values[field.id]) || "(empty)"}`,
|
|
409
|
+
);
|
|
410
|
+
}
|
|
411
|
+
lines.push("", "Use Start process to launch, or Cancel to discard this draft.");
|
|
412
|
+
return lines.join("\n");
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
function formatLaunchModelProfile(
|
|
416
|
+
schema: LauncherModelConfigSchemaLike,
|
|
417
|
+
modelProfileId: string,
|
|
418
|
+
): string {
|
|
419
|
+
return (
|
|
420
|
+
schema.availableProfiles.find((profile) => profile.id === modelProfileId)?.label ??
|
|
421
|
+
modelProfileId
|
|
422
|
+
);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function getLaunchModelStepOverride(
|
|
426
|
+
session: PendingLaunchSession,
|
|
427
|
+
step: LaunchModelStep,
|
|
428
|
+
): string | null {
|
|
429
|
+
const config = buildLaunchModelConfig(session);
|
|
430
|
+
return step.kind === "default"
|
|
431
|
+
? (trimToNull(config.defaultModelProfileId) ?? null)
|
|
432
|
+
: (trimToNull(config.turnConfigs?.[step.turnId]?.modelProfileId) ?? null);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
function getLaunchModelStepPreview(
|
|
436
|
+
preview: LauncherModelConfigPreviewLike | null | undefined,
|
|
437
|
+
step: LaunchModelStep,
|
|
438
|
+
): string | null {
|
|
439
|
+
if (!preview) return null;
|
|
440
|
+
if (step.kind === "default") return preview.defaultModel.profile?.label ?? null;
|
|
441
|
+
return (
|
|
442
|
+
preview.turns.find((turn) => turn.turnId === step.turnId)?.effective.profile?.label ?? null
|
|
443
|
+
);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
function launchModelStepError(
|
|
447
|
+
step: LaunchModelStep,
|
|
448
|
+
schema: LauncherModelConfigSchemaLike,
|
|
449
|
+
session: PendingLaunchSession,
|
|
450
|
+
): LaunchModelStepResult {
|
|
451
|
+
return {
|
|
452
|
+
ok: false,
|
|
453
|
+
prompt: `${step.label} must match an available model profile.\n${buildLaunchModelStepPrompt({ step, schema, session })}`,
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
function matchModelProfile(
|
|
458
|
+
text: string,
|
|
459
|
+
schema: LauncherModelConfigSchemaLike,
|
|
460
|
+
): LauncherModelConfigSchemaLike["availableProfiles"][number] | null {
|
|
461
|
+
return matchByNumberValueOrLabel(text, schema.availableProfiles, (profile) => profile.id);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
function setLaunchModelStepOverride(
|
|
465
|
+
session: PendingLaunchSession,
|
|
466
|
+
step: LaunchModelStep,
|
|
467
|
+
modelProfileId: string,
|
|
468
|
+
): void {
|
|
469
|
+
const current = buildLaunchModelConfig(session);
|
|
470
|
+
if (step.kind === "default") {
|
|
471
|
+
session.modelConfig = {
|
|
472
|
+
...current,
|
|
473
|
+
defaultModelProfileId: modelProfileId,
|
|
474
|
+
turnConfigs: current.turnConfigs ?? {},
|
|
475
|
+
};
|
|
476
|
+
} else {
|
|
477
|
+
session.modelConfig = {
|
|
478
|
+
...current,
|
|
479
|
+
turnConfigs: {
|
|
480
|
+
...(current.turnConfigs ?? {}),
|
|
481
|
+
[step.turnId]: { modelProfileId },
|
|
482
|
+
},
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
session.modelConfigTouched = true;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
function clearLaunchModelStepOverride(session: PendingLaunchSession, step: LaunchModelStep): void {
|
|
489
|
+
const current = buildLaunchModelConfig(session);
|
|
490
|
+
if (step.kind === "default") {
|
|
491
|
+
session.modelConfig = {
|
|
492
|
+
...current,
|
|
493
|
+
defaultModelProfileId: null,
|
|
494
|
+
turnConfigs: current.turnConfigs ?? {},
|
|
495
|
+
};
|
|
496
|
+
} else {
|
|
497
|
+
const turnConfigs = { ...(current.turnConfigs ?? {}) };
|
|
498
|
+
delete turnConfigs[step.turnId];
|
|
499
|
+
session.modelConfig = { ...current, turnConfigs };
|
|
500
|
+
}
|
|
501
|
+
session.modelConfigTouched = true;
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
function advanceLaunchModelStep(
|
|
505
|
+
session: PendingLaunchSession,
|
|
506
|
+
schema: LauncherModelConfigSchemaLike,
|
|
507
|
+
): LaunchModelStepResult {
|
|
508
|
+
if (!session.modelEdit) return { ok: true, done: true };
|
|
509
|
+
session.modelEdit = { stepIndex: session.modelEdit.stepIndex + 1 };
|
|
510
|
+
const next = currentLaunchModelStep(session, schema);
|
|
511
|
+
if (next) return { ok: true, done: false, step: next };
|
|
512
|
+
finishLaunchModelEdit(session);
|
|
513
|
+
return { ok: true, done: true };
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function parseBoolean(value: string): boolean | null {
|
|
517
|
+
const normalized = value.trim().toLowerCase();
|
|
518
|
+
if (["yes", "y", "true", "1", "on"].includes(normalized)) return true;
|
|
519
|
+
if (["no", "n", "false", "0", "off"].includes(normalized)) return false;
|
|
520
|
+
return null;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
function hasPresentValue(value: unknown): boolean {
|
|
524
|
+
if (typeof value === "string") return value.trim() !== "";
|
|
525
|
+
if (typeof value === "number") return Number.isFinite(value);
|
|
526
|
+
if (typeof value === "boolean") return true;
|
|
527
|
+
return false;
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
function formatLaunchFieldValue(value: unknown): string {
|
|
531
|
+
if (typeof value === "string") return value.trim();
|
|
532
|
+
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
533
|
+
if (typeof value === "boolean") return value ? "yes" : "no";
|
|
534
|
+
return "";
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
function normalizeFieldValue(
|
|
538
|
+
field: LauncherFieldDefinition,
|
|
539
|
+
value: unknown,
|
|
540
|
+
): { ok: true; value: LaunchFieldValue } | { ok: false; error: string } {
|
|
541
|
+
if (value === undefined || value === null)
|
|
542
|
+
return { ok: false, error: `${field.label} is required.` };
|
|
543
|
+
if (field.kind === "boolean") {
|
|
544
|
+
if (typeof value === "boolean") return { ok: true, value };
|
|
545
|
+
if (typeof value === "string") {
|
|
546
|
+
const parsed = parseBoolean(value);
|
|
547
|
+
if (parsed !== null) return { ok: true, value: parsed };
|
|
548
|
+
}
|
|
549
|
+
return { ok: false, error: `${field.label} must be yes or no.` };
|
|
550
|
+
}
|
|
551
|
+
if (field.kind === "number") {
|
|
552
|
+
const parsed = typeof value === "number" ? value : Number(String(value).trim());
|
|
553
|
+
return Number.isFinite(parsed)
|
|
554
|
+
? { ok: true, value: parsed }
|
|
555
|
+
: { ok: false, error: `${field.label} must be a number.` };
|
|
556
|
+
}
|
|
557
|
+
return { ok: true, value: String(value) };
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
function skipField(
|
|
561
|
+
field: LauncherFieldDefinition,
|
|
562
|
+
currentValue: LaunchFieldValue | undefined,
|
|
563
|
+
): { ok: true; setValue: boolean; value: LaunchFieldValue } | { ok: false; error: string } {
|
|
564
|
+
if (hasPresentValue(currentValue)) {
|
|
565
|
+
return { ok: true, setValue: false, value: currentValue as LaunchFieldValue };
|
|
566
|
+
}
|
|
567
|
+
if (field.required) {
|
|
568
|
+
return { ok: false, error: `${field.label} is required.` };
|
|
569
|
+
}
|
|
570
|
+
return { ok: true, setValue: true, value: field.kind === "boolean" ? false : "" };
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function parseFieldText(
|
|
574
|
+
field: LauncherFieldDefinition,
|
|
575
|
+
text: string,
|
|
576
|
+
options: readonly LauncherFieldOptionDefinition[],
|
|
577
|
+
): { ok: true; setValue: boolean; value: LaunchFieldValue } | { ok: false; error: string } {
|
|
578
|
+
const trimmed = text.trim();
|
|
579
|
+
if (!trimmed) {
|
|
580
|
+
return skipField(field, undefined);
|
|
581
|
+
}
|
|
582
|
+
if (field.kind === "select") {
|
|
583
|
+
const matched = matchSelectOption(trimmed, options);
|
|
584
|
+
if (matched) return { ok: true, setValue: true, value: matched.value };
|
|
585
|
+
if (options.length > 0) {
|
|
586
|
+
return { ok: false, error: `${field.label} must match one of the available options.` };
|
|
587
|
+
}
|
|
588
|
+
return { ok: true, setValue: true, value: trimmed };
|
|
589
|
+
}
|
|
590
|
+
const normalized = normalizeFieldValue(field, text);
|
|
591
|
+
if (!normalized.ok) return normalized;
|
|
592
|
+
if (field.required && !hasPresentValue(normalized.value)) {
|
|
593
|
+
return { ok: false, error: `${field.label} is required.` };
|
|
594
|
+
}
|
|
595
|
+
return { ok: true, setValue: true, value: normalized.value };
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
function matchSelectOption(
|
|
599
|
+
text: string,
|
|
600
|
+
options: readonly LauncherFieldOptionDefinition[],
|
|
601
|
+
): LauncherFieldOptionDefinition | null {
|
|
602
|
+
return matchByNumberValueOrLabel(text, options, (option) => option.value);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
function matchByNumberValueOrLabel<T extends { label: string }>(
|
|
606
|
+
text: string,
|
|
607
|
+
options: readonly T[],
|
|
608
|
+
getValue: (item: T) => string,
|
|
609
|
+
): T | null {
|
|
610
|
+
const numeric = Number(text);
|
|
611
|
+
if (Number.isInteger(numeric) && numeric >= 1 && numeric <= options.length) {
|
|
612
|
+
return options[numeric - 1] ?? null;
|
|
613
|
+
}
|
|
614
|
+
const lower = text.toLowerCase();
|
|
615
|
+
return (
|
|
616
|
+
options.find((option) => getValue(option) === text) ??
|
|
617
|
+
options.find((option) => option.label.toLowerCase() === lower) ??
|
|
618
|
+
null
|
|
619
|
+
);
|
|
620
|
+
}
|