opencode-azure-devops-pr-review 0.4.2 → 0.4.3
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/CHANGELOG.md +92 -0
- package/README.md +121 -40
- package/package.json +2 -2
- package/src/azure.mjs +12 -2
- package/src/config.mjs +3 -0
- package/src/runtime.mjs +33 -11
- package/src/session.mjs +8 -5
- package/src/setup.mjs +904 -85
- package/src/tui-setup.mjs +344 -0
- package/src/tui-view.jsx +394 -154
- package/src/tui.js +446 -173
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
// AZPR's setup in OpenCode's terminal UI: what is asked, in which order, and what is done with the answers.
|
|
2
|
+
//
|
|
3
|
+
// The server part (src/setup.mjs) knows every setting: it describes each as a row (what it is called, its
|
|
4
|
+
// value, how it is asked), checks an answer, asks Azure DevOps whether a token works, and is the only one to
|
|
5
|
+
// write the settings file and the token file. This part only asks, with the host's own dialogs, and hands
|
|
6
|
+
// the answers to the server part over the plugin's RPC. It reads no file, writes none and makes no request
|
|
7
|
+
// to Azure DevOps. A token it was given is passed on once and kept nowhere: not in a toast, a dialog text,
|
|
8
|
+
// a log or a store.
|
|
9
|
+
//
|
|
10
|
+
// This module imports nothing, so that the order of its questions is tested with answers a test gives
|
|
11
|
+
// (tests/tui-setup.test.mjs); src/tui-view.jsx supplies the dialogs, the masked token field, the host's
|
|
12
|
+
// model list and the RPC.
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* The RPC between the two parts. The schemas are deliberately loose: the server part checks every value
|
|
16
|
+
* itself and answers in its own words, so a refused input is never quoted by the host's schema error.
|
|
17
|
+
*/
|
|
18
|
+
const anyObject = Object.freeze({ type: 'object' });
|
|
19
|
+
export const SETUP_RPC = Object.freeze({
|
|
20
|
+
id: 'azpr.setup',
|
|
21
|
+
methods: Object.freeze({
|
|
22
|
+
/** What is set, as rows, and whether AZPR can run (`state`: required, ready or restart). */
|
|
23
|
+
status: Object.freeze({ input: anyObject, output: anyObject }),
|
|
24
|
+
/** Whether answers can be used, and their saved form, without saving anything. */
|
|
25
|
+
validate: Object.freeze({ input: anyObject, output: anyObject }),
|
|
26
|
+
/** Whether Azure DevOps accepts a token (the one given, else the saved one) for an organization. */
|
|
27
|
+
check: Object.freeze({ input: anyObject, output: anyObject }),
|
|
28
|
+
/** Save answers; the server part writes the files. */
|
|
29
|
+
save: Object.freeze({ input: anyObject, output: anyObject }),
|
|
30
|
+
}),
|
|
31
|
+
events: Object.freeze({
|
|
32
|
+
/**
|
|
33
|
+
* A first setup was loaded: the review commands are the real ones now. OpenCode 2.3.0 announces a change
|
|
34
|
+
* of commands that a plugin makes after it was loaded without saying where, and its clients then keep
|
|
35
|
+
* the list they have; the terminal part reads the commands again when it hears this.
|
|
36
|
+
*/
|
|
37
|
+
loaded: Object.freeze({ schema: anyObject }),
|
|
38
|
+
}),
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
/** Whether a notice is the server part's answer to /pr-setup without options: the terminal part then asks. */
|
|
42
|
+
export const isSetupNotice = metadata => metadata !== null && typeof metadata === 'object' && metadata.azpr !== null &&
|
|
43
|
+
typeof metadata.azpr === 'object' && metadata.azpr.setup === true;
|
|
44
|
+
|
|
45
|
+
const NOT_SET_UP = 'AZPR is not set up yet. Type /pr-setup to set it up.';
|
|
46
|
+
const CANCELLED = 'AZPR setup: left before the end; nothing was saved.';
|
|
47
|
+
const STOPPED = 'AZPR setup stopped: the OpenCode server did not answer as expected. What was saved before stays; run /pr-setup again.';
|
|
48
|
+
const UNAVAILABLE = 'AZPR setup is not available: this OpenCode server did not answer for AZPR. Restart the OpenCode service and try again; /pr-setup --help shows the options.';
|
|
49
|
+
const text = value => typeof value === 'string' ? value : '';
|
|
50
|
+
const rowOf = (status, id) => (Array.isArray(status?.rows) ? status.rows : []).find(row => row?.id === id);
|
|
51
|
+
const withProblem = (problem, description) => problem ? `${problem}\n\n${description}` : description;
|
|
52
|
+
// A model as settings name it: provider/model, optionally with #variant.
|
|
53
|
+
const baseModel = value => text(value).split('#')[0];
|
|
54
|
+
const variantOf = value => text(value).includes('#') ? text(value).slice(text(value).indexOf('#') + 1) : '';
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @param {object} host
|
|
58
|
+
* @param {(method: string, input?: object) => Promise<any>} host.call the server part's RPC
|
|
59
|
+
* @param {{prompt: Function, select: Function, confirm: Function, alert: Function}} host.dialog the host's dialogs
|
|
60
|
+
* @param {(options: {title: string, description: string, problem?: string, optional?: boolean}) => Promise<string | undefined>} host.secret a field that shows nothing of what is typed
|
|
61
|
+
* @param {(options: {title?: string, message: string, variant?: string, duration?: number}) => void} host.toast
|
|
62
|
+
* @param {() => Promise<Array<{id: string, name: string, provider: string, variants: string[]}>>} host.models models the host offers that can call tools
|
|
63
|
+
* @param {() => void} [host.loaded] told when a first setup was loaded, so that the commands are read again
|
|
64
|
+
* @param {() => (() => void)} [host.pin] called when a setup opens: from then until the function it returns
|
|
65
|
+
* is called, `call`, `models` and `loaded` must reach the same server part, whatever is on screen meanwhile
|
|
66
|
+
*/
|
|
67
|
+
export function createSetup(host) {
|
|
68
|
+
let running = false, hinted = false, stopped = false;
|
|
69
|
+
// After stop() nothing more is asked of the server part or of the person: a setup that is under way ends
|
|
70
|
+
// at its next step, whatever answer arrives for the step it was waiting on, and saves nothing more.
|
|
71
|
+
const live = run => (...inputs) => {
|
|
72
|
+
if (stopped) throw new Error('AZPR setup was stopped.');
|
|
73
|
+
return run(...inputs);
|
|
74
|
+
};
|
|
75
|
+
const call = live((method, input) => host.call(method, input));
|
|
76
|
+
const dialog = { prompt: live(options => host.dialog.prompt(options)), select: live(options => host.dialog.select(options)),
|
|
77
|
+
confirm: live(options => host.dialog.confirm(options)), alert: live(options => host.dialog.alert(options)) };
|
|
78
|
+
const secret = live(options => host.secret(options));
|
|
79
|
+
const models = live(() => host.models());
|
|
80
|
+
const toast = options => { if (!stopped) host.toast(options); };
|
|
81
|
+
const loaded = () => { if (!stopped) host.loaded?.(); };
|
|
82
|
+
const pin = () => host.pin?.();
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* A text answer the server part accepts, or undefined when the person left: `value` is its saved form and
|
|
86
|
+
* `shown` how it may be displayed (a proxy without its user name and password).
|
|
87
|
+
*/
|
|
88
|
+
async function askText(row, start = row.current ?? '') {
|
|
89
|
+
let problem = '', value = start;
|
|
90
|
+
for (;;) {
|
|
91
|
+
const answer = await dialog.prompt({ title: row.title, description: withProblem(problem, row.description), placeholder: row.placeholder, value });
|
|
92
|
+
if (answer === undefined) return undefined;
|
|
93
|
+
const result = await call('validate', { changes: { [row.id]: answer } });
|
|
94
|
+
// The saved form of the answer, where the server part sends one back: it sends none for a proxy with a
|
|
95
|
+
// user name and password, and what was typed is then what is checked and saved.
|
|
96
|
+
if (result?.ok) return { value: Object.hasOwn(result.values ?? {}, row.id) ? text(result.values[row.id]) : answer, shown: text(result.shown?.[row.id]) };
|
|
97
|
+
problem = text(result?.problem) || 'That value cannot be used.';
|
|
98
|
+
value = answer;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** One of a row's choices, or undefined; a choice that needs it is confirmed, with the safe answer first. */
|
|
103
|
+
async function askChoice(row) {
|
|
104
|
+
const choices = Array.isArray(row.choices) ? row.choices : [];
|
|
105
|
+
const listed = choices.some(choice => choice.value === row.current);
|
|
106
|
+
const OTHER = '\u0000other';
|
|
107
|
+
const picked = await dialog.select({ title: row.title, current: listed ? row.current : row.other ? OTHER : undefined,
|
|
108
|
+
options: [...choices.map(choice => ({ title: choice.label, value: choice.value, description: choice.description })),
|
|
109
|
+
...(row.other ? [{ title: row.other.label, value: OTHER, description: listed ? undefined : text(row.current) }] : [])] });
|
|
110
|
+
if (picked === undefined) return undefined;
|
|
111
|
+
if (picked === OTHER) return (await askText({ ...row, ...row.other }))?.value;
|
|
112
|
+
const confirm = choices.find(choice => choice.value === picked)?.confirm;
|
|
113
|
+
if (confirm && picked !== row.current) {
|
|
114
|
+
const answer = await dialog.select({ title: confirm.title, options: [
|
|
115
|
+
{ title: confirm.keep, value: 'keep', description: confirm.message },
|
|
116
|
+
{ title: confirm.accept, value: 'accept' }] });
|
|
117
|
+
if (answer !== 'accept') return undefined;
|
|
118
|
+
}
|
|
119
|
+
return picked;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** A model of the host's list, with its variant when it has some; '' when the row may be off; undefined when left. */
|
|
123
|
+
async function askModel(row) {
|
|
124
|
+
const list = await models();
|
|
125
|
+
// A row that may be off can still be switched off when OpenCode offers no model; a row that needs one cannot be answered.
|
|
126
|
+
if (!list.length && !row.optional) {
|
|
127
|
+
await dialog.alert({ title: 'No model available', message: 'OpenCode offers no model that can call tools. Connect a model provider first (/connect), then run /pr-setup again.' });
|
|
128
|
+
return undefined;
|
|
129
|
+
}
|
|
130
|
+
const current = baseModel(row.current);
|
|
131
|
+
const picked = await dialog.select({ title: row.title, placeholder: 'Search models', current: row.optional && !current ? '' : current || undefined,
|
|
132
|
+
options: [...(row.optional ? [{ title: row.optional.label, value: '', description: row.optional.description, category: 'Off' }] : []),
|
|
133
|
+
...list.map(model => ({ title: model.name, value: model.id, description: model.id, category: model.provider }))] });
|
|
134
|
+
if (picked === undefined || picked === '') return picked;
|
|
135
|
+
const variants = list.find(model => model.id === picked)?.variants ?? [];
|
|
136
|
+
if (!variants.length) return picked;
|
|
137
|
+
const variant = await dialog.select({ title: `Variant of ${picked}`, current: picked === current ? variantOf(row.current) : '',
|
|
138
|
+
options: [{ title: 'Default', value: '', description: 'The model\'s own default' }, ...variants.map(name => ({ title: name, value: name }))] });
|
|
139
|
+
return variant === undefined ? undefined : variant ? `${picked}#${variant}` : picked;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Ask for the token and have Azure DevOps check it. Returns `{ token }` (undefined: the saved one was
|
|
144
|
+
* checked and stays), `{ organization: problem }` when Azure DevOps knows no such organization, or
|
|
145
|
+
* undefined when the person left.
|
|
146
|
+
* `network()` gives the proxy and CA file to check with (none: the saved ones); `repair(fix)` lets the
|
|
147
|
+
* person change the proxy or the CA file after a failure and says whether to try again; `problem` is
|
|
148
|
+
* what the dialog says first.
|
|
149
|
+
*/
|
|
150
|
+
async function askToken(row, { organization, network, repair, problem = '' }) {
|
|
151
|
+
for (;;) {
|
|
152
|
+
const answer = await secret({ title: row.title, problem, optional: row.saved === true,
|
|
153
|
+
description: text(row.description).replaceAll('{organization}', organization) + (row.saved === true ? ' Leave it empty to keep the saved token.' : '') });
|
|
154
|
+
if (answer === undefined) return undefined;
|
|
155
|
+
const token = answer.trim() || undefined;
|
|
156
|
+
if (token === undefined && row.saved !== true) { problem = 'Paste the token first.'; continue; }
|
|
157
|
+
// The same token is checked again after the network settings were changed.
|
|
158
|
+
for (;;) {
|
|
159
|
+
toast({ message: 'AZPR: asking Azure DevOps whether the token works…', variant: 'info', duration: 2000 });
|
|
160
|
+
const result = await call('check', { organization, ...(token === undefined ? {} : { token }), ...network() });
|
|
161
|
+
if (result?.ok) {
|
|
162
|
+
toast({ message: `AZPR: Azure DevOps accepted the token for ${organization}.`, variant: 'success' });
|
|
163
|
+
return { token };
|
|
164
|
+
}
|
|
165
|
+
const why = text(result?.problem) || 'The token could not be checked.';
|
|
166
|
+
if (result?.fix === 'organization') return { organization: why };
|
|
167
|
+
if (result?.fix === 'token') { problem = why; break; }
|
|
168
|
+
await dialog.alert({ title: 'The token could not be checked', message: why });
|
|
169
|
+
const fixable = result?.fix === 'proxy' || result?.fix === 'caFile';
|
|
170
|
+
const next = await dialog.select({ title: 'What now?', options: [
|
|
171
|
+
...(fixable ? [{ title: result.fix === 'proxy' ? 'Set a proxy and try again' : 'Set the CA certificate file and try again', value: 'repair' }] : []),
|
|
172
|
+
{ title: 'Try again', value: 'again' },
|
|
173
|
+
{ title: token === undefined ? 'Keep the saved token without checking it' : 'Save the token without checking it', value: 'unchecked', description: '/pr-check tells later whether it works' }] });
|
|
174
|
+
if (next === undefined) return undefined;
|
|
175
|
+
// Unchecked: the token that was entered is saved as it is, and where none was entered the saved one stays.
|
|
176
|
+
if (next === 'unchecked') return { token, unchecked: true };
|
|
177
|
+
if (next === 'repair' && !(await repair(result.fix))) return undefined;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const failed = result => toast({ title: 'AZPR setup', message: text(result?.problem) || 'The settings could not be saved.', variant: 'error', duration: 10000 });
|
|
183
|
+
const restartNote = status => status?.state === 'restart'
|
|
184
|
+
? 'Saved. It takes effect after the OpenCode service restarts: opencode service restart'
|
|
185
|
+
: 'Saved.';
|
|
186
|
+
|
|
187
|
+
/** The first setup: what a review cannot run without, and the network settings that could make it fail. */
|
|
188
|
+
async function firstSetup(status) {
|
|
189
|
+
const rows = id => rowOf(status, id);
|
|
190
|
+
const answers = {}, shown = {};
|
|
191
|
+
let problem = '';
|
|
192
|
+
const ask = async (id, row = rows(id)) => {
|
|
193
|
+
const answer = await askText({ ...row, description: withProblem(problem, row.description) }, answers[id] ?? row.current ?? '');
|
|
194
|
+
problem = '';
|
|
195
|
+
if (answer === undefined) return false;
|
|
196
|
+
answers[id] = answer.value;
|
|
197
|
+
shown[id] = answer.shown;
|
|
198
|
+
return true;
|
|
199
|
+
};
|
|
200
|
+
if (!(await ask('organization'))) return false;
|
|
201
|
+
// Asked before anything is sent to Azure DevOps: a network that needs them makes every later step fail.
|
|
202
|
+
if (!(await ask('proxy'))) return false;
|
|
203
|
+
if (answers.proxy !== '' && !(await ask('caFile'))) return false;
|
|
204
|
+
let token;
|
|
205
|
+
for (;;) {
|
|
206
|
+
const answer = await askToken(rows('token'), { organization: answers.organization,
|
|
207
|
+
network: () => ({ proxy: answers.proxy, ...(answers.caFile === undefined ? {} : { caFile: answers.caFile }) }),
|
|
208
|
+
repair: fix => ask(fix) });
|
|
209
|
+
if (answer === undefined) return false;
|
|
210
|
+
if (answer.organization === undefined) { token = answer.token; break; }
|
|
211
|
+
// Azure DevOps knows no such organization: ask for it again, then for the token again.
|
|
212
|
+
problem = answer.organization;
|
|
213
|
+
if (!(await ask('organization'))) return false;
|
|
214
|
+
}
|
|
215
|
+
const model = await askModel(rows('reviewModel'));
|
|
216
|
+
if (model === undefined) return false;
|
|
217
|
+
const yes = await dialog.confirm({ title: 'Save AZPR\'s settings?', label: { confirm: 'Save', cancel: 'Leave' },
|
|
218
|
+
message: [`Organization: ${answers.organization}`, `Proxy: ${shown.proxy || 'none'}`, ...(answers.caFile ? [`CA certificate file: ${answers.caFile}`] : []),
|
|
219
|
+
`Token: ${token === undefined ? 'the saved one' : 'the one just entered, in a file only you can read'}`,
|
|
220
|
+
`Model: ${model} (reviewers, verifier and /pr-quick)`, '', 'Other settings keep their values (the defaults in a new file); /pr-setup changes them later.'].join('\n') });
|
|
221
|
+
if (!yes) return false;
|
|
222
|
+
const saved = await call('save', { changes: { organization: answers.organization, proxy: answers.proxy, ...(answers.caFile === undefined ? {} : { caFile: answers.caFile }), model },
|
|
223
|
+
...(token === undefined ? {} : { token }) });
|
|
224
|
+
if (!saved?.ok) { failed(saved); return true; }
|
|
225
|
+
if (saved.status?.state === 'ready') loaded();
|
|
226
|
+
await dialog.alert({ title: 'AZPR is set up', message: (saved.status?.state === 'ready'
|
|
227
|
+
? 'Check access to a pull request, then review it:\n\n/pr-check <PR URL>\n/pr-review <PR URL>\n\n/pr-setup shows and changes these settings.'
|
|
228
|
+
: `The settings are saved${text(saved.status?.problem) ? `, but AZPR could not load them: ${saved.status.problem.replace(/\.$/, '')}` : ''}.\n\nRestart the OpenCode service (opencode service restart), then run /pr-check <PR URL>.`) +
|
|
229
|
+
// What the server part adds: where the token of an earlier organization still is.
|
|
230
|
+
(text(saved.note) ? `\n\n${saved.note}` : '') });
|
|
231
|
+
return true;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** The settings as one list; choosing a row changes it, and the list comes back with the new value. */
|
|
235
|
+
async function settingsList(first) {
|
|
236
|
+
let status = first, last;
|
|
237
|
+
/** Change one text row at once (the proxy or the CA file, from the token's check); whether it was changed. */
|
|
238
|
+
const change = async row => {
|
|
239
|
+
const answer = await askText(row);
|
|
240
|
+
if (answer === undefined) return false;
|
|
241
|
+
const saved = await call('save', { changes: { [row.id]: answer.value } });
|
|
242
|
+
if (!saved?.ok) { failed(saved); return false; }
|
|
243
|
+
status = saved.status;
|
|
244
|
+
return true;
|
|
245
|
+
};
|
|
246
|
+
for (;;) {
|
|
247
|
+
const rows = (Array.isArray(status?.rows) ? status.rows : []).filter(row => row && typeof row.id === 'string');
|
|
248
|
+
// The list comes back on the row that was changed, which may be far from its top: the title says what is pending.
|
|
249
|
+
const choice = await dialog.select({ title: status.state === 'restart' ? 'AZPR settings · restart needed' : 'AZPR settings', placeholder: 'Search settings', current: last, options: [
|
|
250
|
+
...(status.state === 'restart' ? [{ title: 'Restart needed', value: '@restart', footer: 'opencode service restart', category: 'Status' }] : []),
|
|
251
|
+
...rows.map(row => ({ title: row.label, value: row.id, footer: text(row.value), category: row.group })),
|
|
252
|
+
{ title: 'Settings file', value: '@file', footer: text(status.settingsFileShown) || text(status.settingsFile), category: 'More' }] });
|
|
253
|
+
if (choice === undefined) return;
|
|
254
|
+
last = choice;
|
|
255
|
+
if (choice === '@restart') {
|
|
256
|
+
await dialog.alert({ title: 'Restart needed', message: 'Settings are read when OpenCode loads AZPR. What was changed takes effect after the OpenCode service restarts:\n\nopencode service restart\n\nUntil then AZPR refuses new reviews.' });
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
if (choice === '@file') {
|
|
260
|
+
await dialog.alert({ title: 'Settings file', message: `${text(status.settingsFile)}\n\nSettings that are not in this list are changed in this file; the README lists them all.` });
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
const row = rows.find(item => item.id === choice);
|
|
264
|
+
const tokenRow = rows.find(item => item.kind === 'secret');
|
|
265
|
+
const repair = fix => change(rows.find(item => item.id === fix));
|
|
266
|
+
let input;
|
|
267
|
+
if (row.kind === 'secret') {
|
|
268
|
+
const organization = text(status.organization);
|
|
269
|
+
if (!organization) { await dialog.alert({ title: row.title, message: 'Set the organization first: a token is checked for it.' }); continue; }
|
|
270
|
+
const answer = await askToken(row, { organization, network: () => ({}), repair });
|
|
271
|
+
if (answer === undefined) continue;
|
|
272
|
+
if (answer.organization !== undefined) { await dialog.alert({ title: row.title, message: answer.organization }); continue; }
|
|
273
|
+
if (answer.token === undefined) continue; // The saved token was checked, or left unchecked: nothing to save.
|
|
274
|
+
// Saved for the organization it was checked for: the server part refuses it when the settings name another by now.
|
|
275
|
+
input = { token: answer.token, organization };
|
|
276
|
+
} else if (row.id === 'organization' && (tokenRow?.bound ?? tokenRow?.saved) === true) {
|
|
277
|
+
const answer = await askText(row);
|
|
278
|
+
if (answer === undefined) continue;
|
|
279
|
+
const previous = text(status.organization);
|
|
280
|
+
if (answer.value.toLowerCase() === previous.toLowerCase()) input = { changes: { organization: answer.value } };
|
|
281
|
+
else {
|
|
282
|
+
// The token these settings name was saved for the organization that is being replaced, whether it can
|
|
283
|
+
// be used at the moment or not: the new one gets its own, checked for it, and both are saved together
|
|
284
|
+
// or not at all.
|
|
285
|
+
const token = await askToken({ ...tokenRow, saved: false }, { organization: answer.value, network: () => ({}), repair,
|
|
286
|
+
problem: previous ? `The saved token was saved for ${previous}. Enter a token of ${answer.value}; without one the organization stays ${previous}.`
|
|
287
|
+
: `The token these settings name was saved without an organization. Enter a token of ${answer.value}; without one the organization is not saved.` });
|
|
288
|
+
if (token === undefined) continue;
|
|
289
|
+
if (token.organization !== undefined) { await dialog.alert({ title: row.title, message: token.organization }); continue; }
|
|
290
|
+
input = { changes: { organization: answer.value }, token: token.token };
|
|
291
|
+
}
|
|
292
|
+
} else {
|
|
293
|
+
const value = row.kind === 'model' ? await askModel(row) : row.kind === 'choice' ? await askChoice(row) : (await askText(row))?.value;
|
|
294
|
+
if (value === undefined) continue;
|
|
295
|
+
input = { changes: { [row.id]: value } };
|
|
296
|
+
}
|
|
297
|
+
const saved = await call('save', input);
|
|
298
|
+
if (!saved?.ok) { failed(saved); continue; }
|
|
299
|
+
status = saved.status;
|
|
300
|
+
toast({ message: `AZPR: ${restartNote(status)}`, variant: 'success', duration: status?.state === 'restart' ? 8000 : 2500 });
|
|
301
|
+
// What the server part adds: where the token of an earlier organization still is. It stays until it was read.
|
|
302
|
+
if (text(saved.note)) await dialog.alert({ title: 'An earlier token is still on disk', message: saved.note });
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
return {
|
|
307
|
+
/** Ask what is missing, or show the settings. One at a time: a second call while one is open does nothing. */
|
|
308
|
+
async open() {
|
|
309
|
+
if (running || stopped) return;
|
|
310
|
+
running = true;
|
|
311
|
+
// One setup talks to one server part, from its first question to its save.
|
|
312
|
+
const release = pin();
|
|
313
|
+
try {
|
|
314
|
+
let status;
|
|
315
|
+
try { status = await call('status', {}); } catch { toast({ title: 'AZPR setup', message: UNAVAILABLE, variant: 'error', duration: 10000 }); return; }
|
|
316
|
+
if (!status || !Array.isArray(status.rows)) { toast({ title: 'AZPR setup', message: text(status?.problem) || UNAVAILABLE, variant: 'error', duration: 10000 }); return; }
|
|
317
|
+
if (status.editable === false) {
|
|
318
|
+
await dialog.alert({ title: 'AZPR\'s settings file needs repair', message: `${text(status.fileProblem) || 'The settings file cannot be changed here.'}\n\n${text(status.settingsFile)}\n\nRepair it by hand, then run /pr-setup again.` });
|
|
319
|
+
return;
|
|
320
|
+
}
|
|
321
|
+
if (status.state === 'required') {
|
|
322
|
+
if (!(await firstSetup(status))) toast({ message: CANCELLED, variant: 'info' });
|
|
323
|
+
return;
|
|
324
|
+
}
|
|
325
|
+
await settingsList(status);
|
|
326
|
+
} catch {
|
|
327
|
+
// What the error says is not shown: it is the host's text, and this part cannot know that it holds no answer.
|
|
328
|
+
toast({ title: 'AZPR setup', message: STOPPED, variant: 'error', duration: 10000 });
|
|
329
|
+
} finally { running = false; release?.(); }
|
|
330
|
+
},
|
|
331
|
+
/** Say once, when AZPR has no usable settings, how to set it up. */
|
|
332
|
+
async hint() {
|
|
333
|
+
if (hinted || stopped) return;
|
|
334
|
+
let status;
|
|
335
|
+
try { status = await call('status', {}); } catch { return; }
|
|
336
|
+
if (hinted || status?.state !== 'required') return;
|
|
337
|
+
hinted = true;
|
|
338
|
+
toast({ message: NOT_SET_UP, variant: 'info', duration: 8000 });
|
|
339
|
+
},
|
|
340
|
+
get running() { return running; },
|
|
341
|
+
/** The terminal part is being unloaded: a setup that is open ends at its next step, and none opens after it. */
|
|
342
|
+
stop() { stopped = true; },
|
|
343
|
+
};
|
|
344
|
+
}
|