@ywldan/kiro 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +429 -2
- package/dist/agent/compact.js +133 -0
- package/dist/agent/compact.js.map +1 -0
- package/dist/agent/loop.js +253 -0
- package/dist/agent/loop.js.map +1 -0
- package/dist/agent/prompt.js +53 -0
- package/dist/agent/prompt.js.map +1 -0
- package/dist/agent/tokens.js +36 -0
- package/dist/agent/tokens.js.map +1 -0
- package/dist/config.js +382 -0
- package/dist/config.js.map +1 -0
- package/dist/env.js +203 -0
- package/dist/env.js.map +1 -0
- package/dist/index.js +272 -0
- package/dist/index.js.map +1 -0
- package/dist/model/cli-ui.js +458 -0
- package/dist/model/cli-ui.js.map +1 -0
- package/dist/model/cli.js +382 -0
- package/dist/model/cli.js.map +1 -0
- package/dist/model/contexts.js +61 -0
- package/dist/model/contexts.js.map +1 -0
- package/dist/model/discovery.js +225 -0
- package/dist/model/discovery.js.map +1 -0
- package/dist/model/presets.js +60 -0
- package/dist/model/presets.js.map +1 -0
- package/dist/model/wizard.js +663 -0
- package/dist/model/wizard.js.map +1 -0
- package/dist/providers/index.js +10 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/providers/openai.js +422 -0
- package/dist/providers/openai.js.map +1 -0
- package/dist/providers/types.js +19 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/secrets.js +63 -0
- package/dist/secrets.js.map +1 -0
- package/dist/sessions.js +248 -0
- package/dist/sessions.js.map +1 -0
- package/dist/tools/diff.js +32 -0
- package/dist/tools/diff.js.map +1 -0
- package/dist/tools/fs.js +524 -0
- package/dist/tools/fs.js.map +1 -0
- package/dist/tools/glob.js +105 -0
- package/dist/tools/glob.js.map +1 -0
- package/dist/tools/ignore.js +63 -0
- package/dist/tools/ignore.js.map +1 -0
- package/dist/tools/index.js +65 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/patch.js +191 -0
- package/dist/tools/patch.js.map +1 -0
- package/dist/tools/paths.js +85 -0
- package/dist/tools/paths.js.map +1 -0
- package/dist/tools/search.js +553 -0
- package/dist/tools/search.js.map +1 -0
- package/dist/tools/shell.js +189 -0
- package/dist/tools/shell.js.map +1 -0
- package/dist/tools/types.js +16 -0
- package/dist/tools/types.js.map +1 -0
- package/dist/ui/App.js +1074 -0
- package/dist/ui/App.js.map +1 -0
- package/dist/ui/ApprovalPrompt.js +48 -0
- package/dist/ui/ApprovalPrompt.js.map +1 -0
- package/dist/ui/Input.js +213 -0
- package/dist/ui/Input.js.map +1 -0
- package/dist/ui/MessageList.js +214 -0
- package/dist/ui/MessageList.js.map +1 -0
- package/dist/ui/ModelPicker.js +100 -0
- package/dist/ui/ModelPicker.js.map +1 -0
- package/dist/ui/StatusBar.js +63 -0
- package/dist/ui/StatusBar.js.map +1 -0
- package/dist/ui/approval-policy.js +22 -0
- package/dist/ui/approval-policy.js.map +1 -0
- package/dist/ui/commands.js +30 -0
- package/dist/ui/commands.js.map +1 -0
- package/dist/ui/export.js +65 -0
- package/dist/ui/export.js.map +1 -0
- package/dist/ui/history.js +86 -0
- package/dist/ui/history.js.map +1 -0
- package/dist/ui/markdown.js +285 -0
- package/dist/ui/markdown.js.map +1 -0
- package/dist/ui/model-picker.js +59 -0
- package/dist/ui/model-picker.js.map +1 -0
- package/dist/ui/mouse.js +36 -0
- package/dist/ui/mouse.js.map +1 -0
- package/dist/ui/profile-list.js +40 -0
- package/dist/ui/profile-list.js.map +1 -0
- package/dist/ui/text.js +328 -0
- package/dist/ui/text.js.map +1 -0
- package/dist/ui/types.js +3 -0
- package/dist/ui/types.js.map +1 -0
- package/dist/version.js +2 -0
- package/dist/version.js.map +1 -0
- package/package.json +58 -4
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
import { DEFAULT_CONTEXT_WINDOW, normalizeBaseUrl, suggestProfileName, validateProfileName, } from '../config.js';
|
|
2
|
+
import { defaultKeyEnv, isValidEnvName, keyPreview, writeEnvValue, } from '../env.js';
|
|
3
|
+
import { discoverModels, modelsEndpoint, testConnection, } from './discovery.js';
|
|
4
|
+
import { allPresets } from './presets.js';
|
|
5
|
+
const DEFAULT_TEMPERATURE = 0.2;
|
|
6
|
+
const DEFAULT_MAX_TOKENS = 4096;
|
|
7
|
+
function cancel(ctx) {
|
|
8
|
+
ctx.ui.note('muted', 'Cancelled; nothing was saved.');
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
async function askText(ctx, options) {
|
|
12
|
+
for (;;) {
|
|
13
|
+
const answer = await ctx.ui.text(options);
|
|
14
|
+
if (answer === null)
|
|
15
|
+
return null;
|
|
16
|
+
const value = answer.trim() ? answer.trim() : (options.defaultValue ?? '').trim();
|
|
17
|
+
if (!value && !options.allowEmpty) {
|
|
18
|
+
ctx.ui.note('error', 'A value is required.');
|
|
19
|
+
continue;
|
|
20
|
+
}
|
|
21
|
+
if (options.validate) {
|
|
22
|
+
const message = options.validate(value);
|
|
23
|
+
if (message) {
|
|
24
|
+
ctx.ui.note('error', message);
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
async function chooseMenu(ctx, title, items) {
|
|
32
|
+
const ui = ctx.ui;
|
|
33
|
+
ui.note('info', [title, ...items.map((item, index) => ` ${index + 1}. ${item.label}`), '', 'Enter a number.'].join('\n'));
|
|
34
|
+
for (;;) {
|
|
35
|
+
const answer = await ui.text({ label: 'Choose', allowEmpty: false });
|
|
36
|
+
if (answer === null)
|
|
37
|
+
return null;
|
|
38
|
+
const value = answer.trim();
|
|
39
|
+
const numeric = /^\d+$/.test(value) ? items[Number(value) - 1] : undefined;
|
|
40
|
+
if (numeric)
|
|
41
|
+
return numeric.key;
|
|
42
|
+
const named = items.find((item) => item.key === value);
|
|
43
|
+
if (named)
|
|
44
|
+
return named.key;
|
|
45
|
+
ui.note('error', `Choose a number from 1 to ${items.length}.`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async function chooseFromList(ctx, title, items) {
|
|
49
|
+
const ui = ctx.ui;
|
|
50
|
+
ui.note('info', [
|
|
51
|
+
title,
|
|
52
|
+
...items.map((item, index) => ` ${index + 1}. ${item}`),
|
|
53
|
+
'',
|
|
54
|
+
'Enter a number, part of an id to filter, or 0 to type it manually.',
|
|
55
|
+
].join('\n'));
|
|
56
|
+
for (;;) {
|
|
57
|
+
const answer = await ui.text({ label: 'Model', allowEmpty: false });
|
|
58
|
+
if (answer === null)
|
|
59
|
+
return null;
|
|
60
|
+
const value = answer.trim();
|
|
61
|
+
if (value === '0')
|
|
62
|
+
return { kind: 'manual' };
|
|
63
|
+
if (/^\d+$/.test(value)) {
|
|
64
|
+
const item = items[Number(value) - 1];
|
|
65
|
+
if (item)
|
|
66
|
+
return { kind: 'item', value: item };
|
|
67
|
+
ui.note('error', `There is no model number ${value}.`);
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const exact = items.find((item) => item === value);
|
|
71
|
+
if (exact)
|
|
72
|
+
return { kind: 'item', value: exact };
|
|
73
|
+
const matches = items.filter((item) => item.toLowerCase().includes(value.toLowerCase()));
|
|
74
|
+
if (matches.length === 1)
|
|
75
|
+
return { kind: 'item', value: matches[0] };
|
|
76
|
+
if (matches.length === 0) {
|
|
77
|
+
ui.note('error', `No model id contains "${value}".`);
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
ui.note('muted', `Matches: ${matches.join(', ')}`);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
async function pickProfile(ctx, title) {
|
|
84
|
+
const names = Object.keys(ctx.config.profiles);
|
|
85
|
+
if (names.length === 0) {
|
|
86
|
+
ctx.ui.note('error', 'There are no profiles configured.');
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
return chooseMenu(ctx, title, names.map((name) => {
|
|
90
|
+
const profile = ctx.config.profiles[name];
|
|
91
|
+
const active = name === ctx.config.activeProfile ? ' [active]' : '';
|
|
92
|
+
return { key: name, label: `${name}${active} — ${profile.model} @ ${profile.baseUrl}` };
|
|
93
|
+
}));
|
|
94
|
+
}
|
|
95
|
+
function hostLabel(baseUrl) {
|
|
96
|
+
try {
|
|
97
|
+
return new URL(baseUrl).hostname.replace(/^www\./, '');
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
100
|
+
return 'custom';
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
function parseTemperature(value) {
|
|
104
|
+
const number = Number(value);
|
|
105
|
+
if (!Number.isFinite(number) || number < 0 || number > 2) {
|
|
106
|
+
return 'Temperature must be a number between 0 and 2.';
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
function parseMaxTokens(value) {
|
|
111
|
+
const number = Number(value);
|
|
112
|
+
if (!Number.isInteger(number) || number <= 0) {
|
|
113
|
+
return 'Max tokens must be a positive whole number.';
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
function parseContextWindow(value) {
|
|
118
|
+
const number = Number(value);
|
|
119
|
+
if (!Number.isInteger(number) || number < 1_000) {
|
|
120
|
+
return 'Context window must be a whole number of at least 1000 tokens.';
|
|
121
|
+
}
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
function profileNameValidator(ctx, originalName) {
|
|
125
|
+
return (value) => {
|
|
126
|
+
try {
|
|
127
|
+
const name = validateProfileName(value);
|
|
128
|
+
if (name !== originalName && ctx.config.profiles[name]) {
|
|
129
|
+
return `A profile named "${name}" already exists.`;
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
catch (error) {
|
|
134
|
+
return error.message;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function keyFor(profile, pendingKey) {
|
|
139
|
+
if (pendingKey)
|
|
140
|
+
return pendingKey;
|
|
141
|
+
return profile.apiKeyEnv ? process.env[profile.apiKeyEnv] : undefined;
|
|
142
|
+
}
|
|
143
|
+
async function runTest(ctx, profile, pendingKey) {
|
|
144
|
+
const key = keyFor(profile, pendingKey);
|
|
145
|
+
if (profile.apiKeyEnv && !key) {
|
|
146
|
+
ctx.ui.note('error', `API key is not set: $${profile.apiKeyEnv} is empty.`);
|
|
147
|
+
}
|
|
148
|
+
ctx.ui.note('muted', `Testing ${profile.baseUrl} with model "${profile.model}"…`);
|
|
149
|
+
const test = ctx.deps?.testConnection ?? testConnection;
|
|
150
|
+
const result = await test(profile, key, { signal: ctx.signal });
|
|
151
|
+
ctx.ui.note(result.ok ? 'info' : 'error', result.message);
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* Returns 'pass' when the test succeeded, 'save' when the user wants to save
|
|
156
|
+
* despite a failure, 'edit' when fields should be edited, or 'cancel'.
|
|
157
|
+
*/
|
|
158
|
+
async function confirmConnection(ctx, profile, pendingKey) {
|
|
159
|
+
for (;;) {
|
|
160
|
+
const result = await runTest(ctx, profile, pendingKey);
|
|
161
|
+
if (ctx.cancelled?.())
|
|
162
|
+
return 'cancel';
|
|
163
|
+
if (result.ok)
|
|
164
|
+
return 'pass';
|
|
165
|
+
const action = await chooseMenu(ctx, 'Connection test failed', [
|
|
166
|
+
{ key: 'retry', label: 'Retry the test' },
|
|
167
|
+
{ key: 'edit', label: 'Edit connection fields' },
|
|
168
|
+
{ key: 'save', label: 'Save anyway' },
|
|
169
|
+
{ key: 'cancel', label: 'Cancel without saving' },
|
|
170
|
+
]);
|
|
171
|
+
if (action === null || action === 'cancel')
|
|
172
|
+
return 'cancel';
|
|
173
|
+
if (action === 'retry')
|
|
174
|
+
continue;
|
|
175
|
+
if (action === 'save')
|
|
176
|
+
return 'save';
|
|
177
|
+
return 'edit';
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
async function editFields(ctx, draft, originalName) {
|
|
181
|
+
const ui = ctx.ui;
|
|
182
|
+
for (;;) {
|
|
183
|
+
const current = draft.profile;
|
|
184
|
+
ui.note('muted', `Profile "${draft.name}": ${current.model} @ ${current.baseUrl}; key ${keyPreview(current)}`);
|
|
185
|
+
const field = await chooseMenu(ctx, 'Edit fields', [
|
|
186
|
+
{ key: 'name', label: `Profile name (${draft.name})` },
|
|
187
|
+
{ key: 'baseUrl', label: `Base URL (${current.baseUrl})` },
|
|
188
|
+
{ key: 'model', label: `Model (${current.model})` },
|
|
189
|
+
{ key: 'env', label: `API key environment variable (${current.apiKeyEnv ?? 'none'})` },
|
|
190
|
+
{ key: 'key', label: 'API key (masked)' },
|
|
191
|
+
{ key: 'temperature', label: `Temperature (${current.temperature ?? DEFAULT_TEMPERATURE})` },
|
|
192
|
+
{ key: 'maxTokens', label: `Max tokens (${current.maxTokens ?? DEFAULT_MAX_TOKENS})` },
|
|
193
|
+
{
|
|
194
|
+
key: 'contextWindow',
|
|
195
|
+
label: `Context window (${current.contextWindow ?? DEFAULT_CONTEXT_WINDOW})`,
|
|
196
|
+
},
|
|
197
|
+
{ key: 'done', label: 'Done editing; test and save' },
|
|
198
|
+
]);
|
|
199
|
+
if (field === null)
|
|
200
|
+
return null;
|
|
201
|
+
if (field === 'done')
|
|
202
|
+
return draft;
|
|
203
|
+
if (field === 'name') {
|
|
204
|
+
const answer = await askText(ctx, {
|
|
205
|
+
label: 'Profile name',
|
|
206
|
+
defaultValue: draft.name,
|
|
207
|
+
allowEmpty: false,
|
|
208
|
+
validate: profileNameValidator(ctx, originalName ?? draft.name),
|
|
209
|
+
});
|
|
210
|
+
if (answer === null)
|
|
211
|
+
return null;
|
|
212
|
+
draft.name = answer;
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
if (field === 'baseUrl') {
|
|
216
|
+
const answer = await askText(ctx, {
|
|
217
|
+
label: 'Base URL',
|
|
218
|
+
defaultValue: current.baseUrl,
|
|
219
|
+
allowEmpty: false,
|
|
220
|
+
validate: (value) => {
|
|
221
|
+
try {
|
|
222
|
+
normalizeBaseUrl(value);
|
|
223
|
+
return null;
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
return error.message;
|
|
227
|
+
}
|
|
228
|
+
},
|
|
229
|
+
});
|
|
230
|
+
if (answer === null)
|
|
231
|
+
return null;
|
|
232
|
+
draft.profile = { ...draft.profile, baseUrl: normalizeBaseUrl(answer) };
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (field === 'model') {
|
|
236
|
+
const answer = await askText(ctx, {
|
|
237
|
+
label: 'Model id',
|
|
238
|
+
defaultValue: current.model,
|
|
239
|
+
allowEmpty: false,
|
|
240
|
+
});
|
|
241
|
+
if (answer === null)
|
|
242
|
+
return null;
|
|
243
|
+
draft.profile = { ...draft.profile, model: answer };
|
|
244
|
+
continue;
|
|
245
|
+
}
|
|
246
|
+
if (field === 'env') {
|
|
247
|
+
const answer = await askText(ctx, {
|
|
248
|
+
label: 'Environment variable name (type "-" for none)',
|
|
249
|
+
defaultValue: current.apiKeyEnv ?? '',
|
|
250
|
+
allowEmpty: true,
|
|
251
|
+
validate: (value) => value && value !== '-' && !isValidEnvName(value)
|
|
252
|
+
? 'Use letters, digits, and underscores; start with a letter or underscore.'
|
|
253
|
+
: null,
|
|
254
|
+
});
|
|
255
|
+
if (answer === null)
|
|
256
|
+
return null;
|
|
257
|
+
const envName = answer === '-' ? undefined : answer || undefined;
|
|
258
|
+
draft.profile = { ...draft.profile, apiKeyEnv: envName };
|
|
259
|
+
if (envName && process.env[envName]) {
|
|
260
|
+
ui.note('muted', `$${envName} is set in the environment.`);
|
|
261
|
+
}
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
if (field === 'key') {
|
|
265
|
+
const answer = await ui.text({
|
|
266
|
+
label: 'New API key (masked; Enter keeps the current key, "-" removes it)',
|
|
267
|
+
mask: true,
|
|
268
|
+
allowEmpty: true,
|
|
269
|
+
});
|
|
270
|
+
if (answer === null)
|
|
271
|
+
return null;
|
|
272
|
+
const value = answer.trim();
|
|
273
|
+
if (value === '-') {
|
|
274
|
+
draft.pendingKey = undefined;
|
|
275
|
+
draft.profile = { ...draft.profile, apiKeyEnv: undefined };
|
|
276
|
+
ui.note('muted', 'The profile will no longer reference an API key. The value stays in ~/.kiro/.env.');
|
|
277
|
+
}
|
|
278
|
+
else if (value) {
|
|
279
|
+
draft.pendingKey = value;
|
|
280
|
+
ui.note('muted', 'The new key will be stored in ~/.kiro/.env when you save.');
|
|
281
|
+
}
|
|
282
|
+
continue;
|
|
283
|
+
}
|
|
284
|
+
if (field === 'temperature') {
|
|
285
|
+
const answer = await askText(ctx, {
|
|
286
|
+
label: 'Temperature',
|
|
287
|
+
defaultValue: String(current.temperature ?? DEFAULT_TEMPERATURE),
|
|
288
|
+
allowEmpty: false,
|
|
289
|
+
validate: parseTemperature,
|
|
290
|
+
});
|
|
291
|
+
if (answer === null)
|
|
292
|
+
return null;
|
|
293
|
+
draft.profile = { ...draft.profile, temperature: Number(answer) };
|
|
294
|
+
continue;
|
|
295
|
+
}
|
|
296
|
+
if (field === 'maxTokens') {
|
|
297
|
+
const answer = await askText(ctx, {
|
|
298
|
+
label: 'Max tokens',
|
|
299
|
+
defaultValue: String(current.maxTokens ?? DEFAULT_MAX_TOKENS),
|
|
300
|
+
allowEmpty: false,
|
|
301
|
+
validate: parseMaxTokens,
|
|
302
|
+
});
|
|
303
|
+
if (answer === null)
|
|
304
|
+
return null;
|
|
305
|
+
draft.profile = { ...draft.profile, maxTokens: Number(answer) };
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
if (field === 'contextWindow') {
|
|
309
|
+
const answer = await askText(ctx, {
|
|
310
|
+
label: `Context window in tokens (Enter keeps ${DEFAULT_CONTEXT_WINDOW})`,
|
|
311
|
+
defaultValue: String(current.contextWindow ?? DEFAULT_CONTEXT_WINDOW),
|
|
312
|
+
allowEmpty: false,
|
|
313
|
+
validate: parseContextWindow,
|
|
314
|
+
});
|
|
315
|
+
if (answer === null)
|
|
316
|
+
return null;
|
|
317
|
+
draft.profile = { ...draft.profile, contextWindow: Number(answer) };
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
async function storePendingKey(draft) {
|
|
322
|
+
if (!draft.pendingKey)
|
|
323
|
+
return { profile: draft.profile };
|
|
324
|
+
const envName = draft.profile.apiKeyEnv ?? defaultKeyEnv(draft.name);
|
|
325
|
+
await writeEnvValue(envName, draft.pendingKey);
|
|
326
|
+
return {
|
|
327
|
+
profile: { ...draft.profile, apiKeyEnv: envName },
|
|
328
|
+
storedAs: envName,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
function summarizeSaved(ctx, name, storedAs) {
|
|
332
|
+
const profile = ctx.config.profiles[name];
|
|
333
|
+
const key = profile?.apiKeyEnv
|
|
334
|
+
? process.env[profile.apiKeyEnv]
|
|
335
|
+
? `key via $${profile.apiKeyEnv}`
|
|
336
|
+
: `$${profile.apiKeyEnv} is not set yet`
|
|
337
|
+
: 'no API key';
|
|
338
|
+
ctx.ui.note('info', `Saved profile "${name}" (${profile?.model ?? '?'} @ ${profile?.baseUrl ?? '?'}; ${key}).`);
|
|
339
|
+
if (storedAs) {
|
|
340
|
+
ctx.ui.note('muted', `API key stored in ~/.kiro/.env as $${storedAs} (file mode 0600).`);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
export async function runSelectFlow(ctx, requestedName) {
|
|
344
|
+
const name = requestedName ?? (await pickProfile(ctx, 'Select active profile'));
|
|
345
|
+
if (name === null)
|
|
346
|
+
return cancel(ctx);
|
|
347
|
+
const profile = ctx.config.profiles[name];
|
|
348
|
+
if (!profile) {
|
|
349
|
+
ctx.ui.note('error', `Unknown profile "${name}".`);
|
|
350
|
+
return null;
|
|
351
|
+
}
|
|
352
|
+
if (ctx.config.activeProfile === name) {
|
|
353
|
+
ctx.ui.note('info', `Profile "${name}" is already active.`);
|
|
354
|
+
return name;
|
|
355
|
+
}
|
|
356
|
+
const next = { ...ctx.config, activeProfile: name };
|
|
357
|
+
await ctx.save(next);
|
|
358
|
+
ctx.config = next;
|
|
359
|
+
ctx.ui.note('info', `Active profile is now "${name}" (${profile.model}).`);
|
|
360
|
+
return name;
|
|
361
|
+
}
|
|
362
|
+
export async function runAddFlow(ctx) {
|
|
363
|
+
const ui = ctx.ui;
|
|
364
|
+
const presets = allPresets();
|
|
365
|
+
const presetId = await chooseMenu(ctx, 'Choose a provider', presets.map((preset) => ({
|
|
366
|
+
key: preset.id,
|
|
367
|
+
label: preset.local
|
|
368
|
+
? `${preset.label} — ${preset.baseUrl} (no key)`
|
|
369
|
+
: preset.apiKeyEnv
|
|
370
|
+
? `${preset.label} — ${preset.baseUrl} ($${preset.apiKeyEnv})`
|
|
371
|
+
: `${preset.label} — enter a base URL`,
|
|
372
|
+
})));
|
|
373
|
+
if (presetId === null)
|
|
374
|
+
return cancel(ctx);
|
|
375
|
+
const preset = presets.find((entry) => entry.id === presetId);
|
|
376
|
+
if (!preset)
|
|
377
|
+
return cancel(ctx);
|
|
378
|
+
const baseAnswer = await askText(ctx, {
|
|
379
|
+
label: preset.baseUrl ? 'Base URL (Enter keeps the default)' : 'Base URL (for example https://api.example.com/v1)',
|
|
380
|
+
defaultValue: preset.baseUrl || undefined,
|
|
381
|
+
allowEmpty: Boolean(preset.baseUrl),
|
|
382
|
+
validate: (value) => {
|
|
383
|
+
try {
|
|
384
|
+
normalizeBaseUrl(value);
|
|
385
|
+
return null;
|
|
386
|
+
}
|
|
387
|
+
catch (error) {
|
|
388
|
+
return error.message;
|
|
389
|
+
}
|
|
390
|
+
},
|
|
391
|
+
});
|
|
392
|
+
if (baseAnswer === null)
|
|
393
|
+
return cancel(ctx);
|
|
394
|
+
const baseUrl = normalizeBaseUrl(baseAnswer);
|
|
395
|
+
let envName;
|
|
396
|
+
let pendingKey;
|
|
397
|
+
if (!preset.local) {
|
|
398
|
+
const envAnswer = await askText(ctx, {
|
|
399
|
+
label: 'Environment variable for the API key (Enter to generate one from the profile name)',
|
|
400
|
+
defaultValue: preset.apiKeyEnv,
|
|
401
|
+
allowEmpty: true,
|
|
402
|
+
validate: (value) => (value && !isValidEnvName(value) ? 'Use letters, digits, and underscores.' : null),
|
|
403
|
+
});
|
|
404
|
+
if (envAnswer === null)
|
|
405
|
+
return cancel(ctx);
|
|
406
|
+
envName = envAnswer || undefined;
|
|
407
|
+
const existing = envName ? process.env[envName] : undefined;
|
|
408
|
+
const keyAnswer = await ui.text({
|
|
409
|
+
label: existing
|
|
410
|
+
? `API key (masked; $${envName} is already set, Enter keeps it)`
|
|
411
|
+
: 'API key (masked; Enter to skip)',
|
|
412
|
+
mask: true,
|
|
413
|
+
allowEmpty: true,
|
|
414
|
+
});
|
|
415
|
+
if (keyAnswer === null)
|
|
416
|
+
return cancel(ctx);
|
|
417
|
+
pendingKey = keyAnswer.trim() || undefined;
|
|
418
|
+
if (envName && !process.env[envName] && !pendingKey) {
|
|
419
|
+
ui.note('muted', `$${envName} is not set yet; you can add it later or save a key when editing.`);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
ui.note('info', `Fetching models from ${modelsEndpoint(baseUrl)}…`);
|
|
423
|
+
const discover = ctx.deps?.discoverModels ?? discoverModels;
|
|
424
|
+
const discovery = await discover({
|
|
425
|
+
baseUrl,
|
|
426
|
+
apiKey: pendingKey ?? (envName ? process.env[envName] : undefined),
|
|
427
|
+
signal: ctx.signal,
|
|
428
|
+
});
|
|
429
|
+
if (ctx.cancelled?.())
|
|
430
|
+
return cancel(ctx);
|
|
431
|
+
let model;
|
|
432
|
+
let discoveredModels = [];
|
|
433
|
+
if (discovery.ok && discovery.models.length > 0) {
|
|
434
|
+
discoveredModels = discovery.models;
|
|
435
|
+
const choice = await chooseFromList(ctx, 'Available models:', discovery.models);
|
|
436
|
+
if (choice === null)
|
|
437
|
+
return cancel(ctx);
|
|
438
|
+
if (choice.kind === 'item') {
|
|
439
|
+
model = choice.value;
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
const manual = await askText(ctx, { label: 'Model id', allowEmpty: false });
|
|
443
|
+
if (manual === null)
|
|
444
|
+
return cancel(ctx);
|
|
445
|
+
model = manual;
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
ui.note(discovery.ok ? 'muted' : 'error', discovery.ok ? 'The endpoint returned an empty model list.' : discovery.message);
|
|
450
|
+
ui.note('info', 'Enter the model id manually.');
|
|
451
|
+
const manual = await askText(ctx, { label: 'Model id', allowEmpty: false });
|
|
452
|
+
if (manual === null)
|
|
453
|
+
return cancel(ctx);
|
|
454
|
+
model = manual;
|
|
455
|
+
}
|
|
456
|
+
const name = await askText(ctx, {
|
|
457
|
+
label: 'Profile name',
|
|
458
|
+
defaultValue: suggestProfileName(preset.id === 'custom' ? hostLabel(baseUrl) : preset.id, model, Object.keys(ctx.config.profiles)),
|
|
459
|
+
allowEmpty: false,
|
|
460
|
+
validate: profileNameValidator(ctx),
|
|
461
|
+
});
|
|
462
|
+
if (name === null)
|
|
463
|
+
return cancel(ctx);
|
|
464
|
+
const advanced = await ui.confirm('Configure advanced settings (temperature, maxTokens, context window)?', false);
|
|
465
|
+
if (advanced === null)
|
|
466
|
+
return cancel(ctx);
|
|
467
|
+
let temperature = DEFAULT_TEMPERATURE;
|
|
468
|
+
let maxTokens = DEFAULT_MAX_TOKENS;
|
|
469
|
+
let contextWindow;
|
|
470
|
+
if (advanced) {
|
|
471
|
+
const tempAnswer = await askText(ctx, {
|
|
472
|
+
label: 'Temperature',
|
|
473
|
+
defaultValue: String(DEFAULT_TEMPERATURE),
|
|
474
|
+
allowEmpty: false,
|
|
475
|
+
validate: parseTemperature,
|
|
476
|
+
});
|
|
477
|
+
if (tempAnswer === null)
|
|
478
|
+
return cancel(ctx);
|
|
479
|
+
const tokensAnswer = await askText(ctx, {
|
|
480
|
+
label: 'Max tokens',
|
|
481
|
+
defaultValue: String(DEFAULT_MAX_TOKENS),
|
|
482
|
+
allowEmpty: false,
|
|
483
|
+
validate: parseMaxTokens,
|
|
484
|
+
});
|
|
485
|
+
if (tokensAnswer === null)
|
|
486
|
+
return cancel(ctx);
|
|
487
|
+
const windowAnswer = await askText(ctx, {
|
|
488
|
+
label: `Context window in tokens (Enter keeps ${DEFAULT_CONTEXT_WINDOW})`,
|
|
489
|
+
defaultValue: String(DEFAULT_CONTEXT_WINDOW),
|
|
490
|
+
allowEmpty: false,
|
|
491
|
+
validate: parseContextWindow,
|
|
492
|
+
});
|
|
493
|
+
if (windowAnswer === null)
|
|
494
|
+
return cancel(ctx);
|
|
495
|
+
temperature = Number(tempAnswer);
|
|
496
|
+
maxTokens = Number(tokensAnswer);
|
|
497
|
+
contextWindow = Number(windowAnswer);
|
|
498
|
+
}
|
|
499
|
+
const draft = {
|
|
500
|
+
name,
|
|
501
|
+
profile: {
|
|
502
|
+
type: 'openai',
|
|
503
|
+
baseUrl,
|
|
504
|
+
apiKeyEnv: preset.local ? undefined : envName,
|
|
505
|
+
model,
|
|
506
|
+
models: discoveredModels.length > 0 ? discoveredModels : undefined,
|
|
507
|
+
temperature,
|
|
508
|
+
maxTokens,
|
|
509
|
+
contextWindow,
|
|
510
|
+
},
|
|
511
|
+
pendingKey,
|
|
512
|
+
};
|
|
513
|
+
let verdict = await confirmConnection(ctx, draft.profile, draft.pendingKey);
|
|
514
|
+
while (verdict === 'edit') {
|
|
515
|
+
const edited = await editFields(ctx, draft);
|
|
516
|
+
if (!edited)
|
|
517
|
+
return cancel(ctx);
|
|
518
|
+
draft.profile = edited.profile;
|
|
519
|
+
draft.name = edited.name;
|
|
520
|
+
draft.pendingKey = edited.pendingKey;
|
|
521
|
+
verdict = await confirmConnection(ctx, draft.profile, draft.pendingKey);
|
|
522
|
+
}
|
|
523
|
+
if (verdict === 'cancel')
|
|
524
|
+
return cancel(ctx);
|
|
525
|
+
let stored;
|
|
526
|
+
try {
|
|
527
|
+
stored = await storePendingKey(draft);
|
|
528
|
+
}
|
|
529
|
+
catch (error) {
|
|
530
|
+
ui.note('error', error.message);
|
|
531
|
+
return null;
|
|
532
|
+
}
|
|
533
|
+
const profiles = { ...ctx.config.profiles, [draft.name]: stored.profile };
|
|
534
|
+
let next = { ...ctx.config, profiles };
|
|
535
|
+
await ctx.save(next);
|
|
536
|
+
ctx.config = next;
|
|
537
|
+
summarizeSaved(ctx, draft.name, stored.storedAs);
|
|
538
|
+
const activate = await ui.confirm(`Set "${draft.name}" as the active profile?`, true);
|
|
539
|
+
if (activate === null)
|
|
540
|
+
return draft.name;
|
|
541
|
+
if (activate) {
|
|
542
|
+
next = { ...ctx.config, activeProfile: draft.name };
|
|
543
|
+
await ctx.save(next);
|
|
544
|
+
ctx.config = next;
|
|
545
|
+
ui.note('info', `Active profile is now "${draft.name}".`);
|
|
546
|
+
}
|
|
547
|
+
return draft.name;
|
|
548
|
+
}
|
|
549
|
+
export async function runEditFlow(ctx, requestedName) {
|
|
550
|
+
const name = requestedName ?? (await pickProfile(ctx, 'Choose a profile to edit'));
|
|
551
|
+
if (name === null)
|
|
552
|
+
return cancel(ctx);
|
|
553
|
+
const existing = ctx.config.profiles[name];
|
|
554
|
+
if (!existing) {
|
|
555
|
+
ctx.ui.note('error', `Unknown profile "${name}".`);
|
|
556
|
+
return null;
|
|
557
|
+
}
|
|
558
|
+
const draft = {
|
|
559
|
+
name,
|
|
560
|
+
profile: { ...existing },
|
|
561
|
+
pendingKey: undefined,
|
|
562
|
+
};
|
|
563
|
+
const edited = await editFields(ctx, draft, name);
|
|
564
|
+
if (!edited)
|
|
565
|
+
return cancel(ctx);
|
|
566
|
+
draft.profile = edited.profile;
|
|
567
|
+
draft.name = edited.name;
|
|
568
|
+
draft.pendingKey = edited.pendingKey;
|
|
569
|
+
let verdict = await confirmConnection(ctx, draft.profile, draft.pendingKey);
|
|
570
|
+
while (verdict === 'edit') {
|
|
571
|
+
const again = await editFields(ctx, draft, name);
|
|
572
|
+
if (!again)
|
|
573
|
+
return cancel(ctx);
|
|
574
|
+
draft.profile = again.profile;
|
|
575
|
+
draft.name = again.name;
|
|
576
|
+
draft.pendingKey = again.pendingKey;
|
|
577
|
+
verdict = await confirmConnection(ctx, draft.profile, draft.pendingKey);
|
|
578
|
+
}
|
|
579
|
+
if (verdict === 'cancel')
|
|
580
|
+
return cancel(ctx);
|
|
581
|
+
let stored;
|
|
582
|
+
try {
|
|
583
|
+
stored = await storePendingKey(draft);
|
|
584
|
+
}
|
|
585
|
+
catch (error) {
|
|
586
|
+
ctx.ui.note('error', error.message);
|
|
587
|
+
return null;
|
|
588
|
+
}
|
|
589
|
+
const profiles = { ...ctx.config.profiles };
|
|
590
|
+
if (draft.name !== name)
|
|
591
|
+
delete profiles[name];
|
|
592
|
+
profiles[draft.name] = stored.profile;
|
|
593
|
+
let activeProfile = ctx.config.activeProfile;
|
|
594
|
+
if (activeProfile === name)
|
|
595
|
+
activeProfile = draft.name;
|
|
596
|
+
const next = { activeProfile, profiles };
|
|
597
|
+
await ctx.save(next);
|
|
598
|
+
ctx.config = next;
|
|
599
|
+
summarizeSaved(ctx, draft.name, stored.storedAs);
|
|
600
|
+
return draft.name;
|
|
601
|
+
}
|
|
602
|
+
export async function runRemoveFlow(ctx, requestedName) {
|
|
603
|
+
const ui = ctx.ui;
|
|
604
|
+
const name = requestedName ?? (await pickProfile(ctx, 'Choose a profile to remove'));
|
|
605
|
+
if (name === null)
|
|
606
|
+
return cancel(ctx);
|
|
607
|
+
const profile = ctx.config.profiles[name];
|
|
608
|
+
if (!profile) {
|
|
609
|
+
ui.note('error', `Unknown profile "${name}".`);
|
|
610
|
+
return null;
|
|
611
|
+
}
|
|
612
|
+
if (Object.keys(ctx.config.profiles).length <= 1) {
|
|
613
|
+
ui.note('error', `Cannot remove "${name}": it is the only profile. Add another model first.`);
|
|
614
|
+
return null;
|
|
615
|
+
}
|
|
616
|
+
const confirmed = await ui.confirm(`Remove profile "${name}" (${profile.model} @ ${profile.baseUrl})?`, false);
|
|
617
|
+
if (confirmed === null)
|
|
618
|
+
return cancel(ctx);
|
|
619
|
+
if (!confirmed) {
|
|
620
|
+
ui.note('muted', 'Nothing was removed.');
|
|
621
|
+
return null;
|
|
622
|
+
}
|
|
623
|
+
const profiles = { ...ctx.config.profiles };
|
|
624
|
+
delete profiles[name];
|
|
625
|
+
let activeProfile = ctx.config.activeProfile;
|
|
626
|
+
if (activeProfile === name) {
|
|
627
|
+
const remaining = Object.keys(profiles);
|
|
628
|
+
activeProfile = remaining[0];
|
|
629
|
+
ui.note('info', `Profile "${name}" was active; switched to "${activeProfile}".`);
|
|
630
|
+
}
|
|
631
|
+
const next = { activeProfile, profiles };
|
|
632
|
+
await ctx.save(next);
|
|
633
|
+
ctx.config = next;
|
|
634
|
+
ui.note('info', `Removed profile "${name}". Its key (if any) stays in ~/.kiro/.env so another profile can reuse it.`);
|
|
635
|
+
return name;
|
|
636
|
+
}
|
|
637
|
+
export async function runModelMenu(ctx) {
|
|
638
|
+
for (;;) {
|
|
639
|
+
const action = await chooseMenu(ctx, 'Model workflow', [
|
|
640
|
+
{ key: 'select', label: 'Select the active model' },
|
|
641
|
+
{ key: 'add', label: 'Add a new model' },
|
|
642
|
+
{ key: 'edit', label: 'Edit a model' },
|
|
643
|
+
{ key: 'remove', label: 'Remove a model' },
|
|
644
|
+
{ key: 'quit', label: 'Quit' },
|
|
645
|
+
]);
|
|
646
|
+
if (action === null || action === 'quit')
|
|
647
|
+
return;
|
|
648
|
+
try {
|
|
649
|
+
if (action === 'select')
|
|
650
|
+
await runSelectFlow(ctx);
|
|
651
|
+
else if (action === 'add')
|
|
652
|
+
await runAddFlow(ctx);
|
|
653
|
+
else if (action === 'edit')
|
|
654
|
+
await runEditFlow(ctx);
|
|
655
|
+
else if (action === 'remove')
|
|
656
|
+
await runRemoveFlow(ctx);
|
|
657
|
+
}
|
|
658
|
+
catch (error) {
|
|
659
|
+
ctx.ui.note('error', error instanceof Error ? error.message : String(error));
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
}
|
|
663
|
+
//# sourceMappingURL=wizard.js.map
|