usegrunt 0.5.0 → 0.6.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/README.md +16 -453
- package/dist/local/api.js +24 -10
- package/dist/local/banner.js +136 -0
- package/dist/local/fallback-setup.js +2 -1
- package/dist/local/fixtures/opencode-events.js +4 -0
- package/dist/local/freebuff-tools.js +112 -0
- package/dist/local/freebuff.js +41 -9
- package/dist/local/freellmapi.js +2 -1
- package/dist/local/harnesses.js +164 -0
- package/dist/local/main.js +9 -4
- package/dist/local/native-router.js +1 -0
- package/dist/local/opencode.js +100 -8
- package/dist/local/routing.js +7 -11
- package/dist/local/setup.js +39 -12
- package/dist/local/source.js +6 -0
- package/dist/local/wizard.js +286 -86
- package/package.json +2 -1
package/dist/local/wizard.js
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
1
|
import { promisify } from 'node:util';
|
|
2
2
|
import { spawn, execFile } from 'node:child_process';
|
|
3
|
-
import { mkdir, readFile, writeFile,
|
|
3
|
+
import { mkdir, readFile, writeFile, cp, mkdtemp, rm, access } from 'node:fs/promises';
|
|
4
4
|
import { homedir, tmpdir } from 'node:os';
|
|
5
5
|
import { dirname, join } from 'node:path';
|
|
6
6
|
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
7
7
|
import { setTimeout as delay } from 'node:timers/promises';
|
|
8
|
-
import { intro, outro, spinner, log } from '@clack/prompts';
|
|
8
|
+
import { intro, outro, spinner, log, note, multiselect, isCancel } from '@clack/prompts';
|
|
9
9
|
import chalk from 'chalk';
|
|
10
10
|
import { setupFallbacks } from './fallback-setup.js';
|
|
11
|
+
import { DOCTOR_TIMEOUT_MS } from './freebuff.js';
|
|
11
12
|
import { BrowserSquire, PROVIDERS, setup, squireEnvironment } from './setup.js';
|
|
12
13
|
import { RegistrySync } from './registry.js';
|
|
13
14
|
import { bundledRegistry, knownAdapters } from '../registry.js';
|
|
15
|
+
import { banner } from './banner.js';
|
|
16
|
+
import { HARNESSES, configurePi, connectHarness, detectHarnesses } from './harnesses.js';
|
|
14
17
|
export const configDir = (env = process.env) => env.MILO_CONFIG_DIR || join(env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'milo');
|
|
15
18
|
export async function command(binary, args, interactive = false, signal) {
|
|
16
19
|
await new Promise((resolve, reject) => {
|
|
@@ -19,37 +22,51 @@ export async function command(binary, args, interactive = false, signal) {
|
|
|
19
22
|
child.once('exit', code => code === 0 ? resolve() : reject(new Error(`${binary} failed; inspect its service status before retrying`)));
|
|
20
23
|
});
|
|
21
24
|
}
|
|
22
|
-
async function step(label, task) {
|
|
25
|
+
async function step(label, task, done = () => label) {
|
|
23
26
|
const progress = spinner();
|
|
24
27
|
progress.start(label);
|
|
25
28
|
try {
|
|
26
29
|
const result = await task();
|
|
27
|
-
progress.stop(
|
|
30
|
+
progress.stop(done(result));
|
|
28
31
|
return result;
|
|
29
32
|
}
|
|
30
33
|
catch (error) {
|
|
31
|
-
progress.
|
|
34
|
+
progress.error(`${label} failed`);
|
|
32
35
|
throw error;
|
|
33
36
|
}
|
|
34
37
|
}
|
|
35
|
-
export async function
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
+
export async function squireBrokerExists() {
|
|
39
|
+
const service = process.platform === 'darwin'
|
|
40
|
+
? ['launchctl', ['print', `gui/${process.getuid()}/ai.trustysquire.trusty-squire-broker`]]
|
|
41
|
+
: ['systemctl', ['--user', 'is-active', 'trusty-squire-broker']];
|
|
42
|
+
try {
|
|
43
|
+
await promisify(execFile)(service[0], [...service[1]], { timeout: 10000 });
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
catch { /* An inactive service can still own the broker installation. */ }
|
|
47
|
+
if (process.platform === 'linux') {
|
|
48
|
+
try {
|
|
49
|
+
const { stdout } = await promisify(execFile)('systemctl', ['--user', 'list-unit-files', 'trusty-squire-broker*.service', '--no-legend', '--no-pager'], { timeout: 10000 });
|
|
50
|
+
if (/^trusty-squire-broker\S*\.service\s/m.test(stdout))
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
catch { /* Socket discovery remains available without a service manager. */ }
|
|
54
|
+
}
|
|
55
|
+
const home = homedir();
|
|
56
|
+
const serviceFile = process.platform === 'darwin'
|
|
57
|
+
? join(home, 'Library', 'LaunchAgents', 'ai.trustysquire.trusty-squire-broker.plist')
|
|
58
|
+
: join(process.env.XDG_CONFIG_HOME || join(home, '.config'), 'systemd', 'user', 'trusty-squire-broker.service');
|
|
59
|
+
try {
|
|
60
|
+
await access(serviceFile);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
catch { /* Check socket discovery too. */ }
|
|
64
|
+
const discovery = join(dirname(fileURLToPath(import.meta.resolve('@trusty-squire/mcp'))), 'bot/broker/discovery.js');
|
|
65
|
+
const program = `const {brokerSocketPath} = await import(${JSON.stringify(pathToFileURL(discovery).href)}); const {existsSync} = await import('node:fs'); process.stdout.write(existsSync(brokerSocketPath()) ? 'present' : 'missing');`;
|
|
38
66
|
const result = await promisify(execFile)(process.execPath, ['--input-type=module', '-e', program], { env: squireEnvironment(), timeout: 10000 });
|
|
39
67
|
return result.stdout === 'present';
|
|
40
68
|
}
|
|
41
69
|
export async function connectSquire(options = {}) {
|
|
42
|
-
const run = options.run || command;
|
|
43
|
-
const install = async () => {
|
|
44
|
-
if (process.env.MILO_SETUP_NO_HUMAN === '1')
|
|
45
|
-
throw new Error('Squire login needs human verification; stopped before onboarding');
|
|
46
|
-
// The bundled installer owns account creation and broker service installation.
|
|
47
|
-
const bin = options.installer || join(dirname(fileURLToPath(import.meta.resolve('@trusty-squire/mcp'))), 'bin.js');
|
|
48
|
-
await run(process.execPath, [bin, 'connect', '--target=opencode', '--json'], true);
|
|
49
|
-
};
|
|
50
|
-
const existing = await (options.hasSession || squireSessionExists)();
|
|
51
|
-
if (!existing)
|
|
52
|
-
await install();
|
|
53
70
|
const probe = async () => { const squire = new BrowserSquire(); try {
|
|
54
71
|
await squire.connect();
|
|
55
72
|
}
|
|
@@ -58,13 +75,19 @@ export async function connectSquire(options = {}) {
|
|
|
58
75
|
} };
|
|
59
76
|
try {
|
|
60
77
|
await probe();
|
|
78
|
+
return;
|
|
61
79
|
}
|
|
62
80
|
catch (error) {
|
|
63
|
-
if (
|
|
64
|
-
throw error;
|
|
65
|
-
|
|
66
|
-
await probe();
|
|
81
|
+
if (await (options.hasBroker || squireBrokerExists)()) {
|
|
82
|
+
throw new Error(`Existing trusty-squire-broker failed its connection probe: ${error instanceof Error ? error.message : String(error)}. Repair the broker, then rerun usegrunt setup.`, { cause: error });
|
|
83
|
+
}
|
|
67
84
|
}
|
|
85
|
+
if (process.env.MILO_SETUP_NO_HUMAN === '1')
|
|
86
|
+
throw new Error('Squire login needs human verification; stopped before onboarding');
|
|
87
|
+
// Only a machine without a broker may enter the installer.
|
|
88
|
+
const bin = options.installer || join(dirname(fileURLToPath(import.meta.resolve('@trusty-squire/mcp'))), 'bin.js');
|
|
89
|
+
await (options.run || command)(process.execPath, [bin, 'connect', '--target=opencode', '--json'], true);
|
|
90
|
+
await probe();
|
|
68
91
|
}
|
|
69
92
|
export async function preflight(run = command) {
|
|
70
93
|
const major = Number(process.versions.node.split('.')[0]);
|
|
@@ -102,29 +125,19 @@ export async function preflight(run = command) {
|
|
|
102
125
|
throw new Error('Start a normal user login with its systemd/launchd service manager, then run npx usegrunt setup.');
|
|
103
126
|
}
|
|
104
127
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
let saved = {};
|
|
128
|
+
const systemdQuote = (value) => '"' + value.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('%', '%%').replaceAll('$', '$$').replaceAll('\n', '\\n') + '"';
|
|
129
|
+
const xml = (value) => value.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"');
|
|
130
|
+
export async function installedSources(directory = configDir()) {
|
|
109
131
|
try {
|
|
110
|
-
|
|
132
|
+
await access(join(directory, 'router-admin.json'));
|
|
111
133
|
}
|
|
112
134
|
catch (error) {
|
|
113
135
|
if (error.code !== 'ENOENT')
|
|
114
|
-
throw
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
const providers = (saved.providers ?? {});
|
|
119
|
-
providers.grunt = { baseUrl: `http://127.0.0.1:${port}/v1`, api: 'openai-completions', apiKey: 'local',
|
|
120
|
-
models: [{ id: 'grunt', name: 'grunt', reasoning: false, input: ['text'], cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: 128000, maxTokens: 4096 }] };
|
|
121
|
-
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
122
|
-
const temporary = `${path}.${process.pid}.tmp`;
|
|
123
|
-
await writeFile(temporary, JSON.stringify({ ...saved, providers }, null, 2) + '\n', { mode: 0o600, flag: 'wx' });
|
|
124
|
-
await rename(temporary, path);
|
|
136
|
+
throw error;
|
|
137
|
+
return 'freebuff,opencode';
|
|
138
|
+
}
|
|
139
|
+
return 'freellmapi,kilo,freebuff,opencode';
|
|
125
140
|
}
|
|
126
|
-
const systemdQuote = (value) => '"' + value.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('%', '%%').replaceAll('$', '$$').replaceAll('\n', '\\n') + '"';
|
|
127
|
-
const xml = (value) => value.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"');
|
|
128
141
|
export async function installService() {
|
|
129
142
|
if (!['linux', 'darwin'].includes(process.platform))
|
|
130
143
|
throw new Error('grunt background service requires systemd or launchd');
|
|
@@ -140,7 +153,9 @@ export async function installService() {
|
|
|
140
153
|
const port = process.env.MILO_PORT || '8788';
|
|
141
154
|
if (!/^\d+$/.test(port) || Number(port) < 1 || Number(port) > 65535)
|
|
142
155
|
throw new Error('Invalid grunt service port');
|
|
143
|
-
const env = { GRUNT_DAEMON: '1', MILO_CONFIG_DIR: directory, MILO_PORT: port, MILO_SOURCES:
|
|
156
|
+
const env = { GRUNT_DAEMON: '1', MILO_CONFIG_DIR: directory, MILO_PORT: port, MILO_SOURCES: await installedSources(directory), PATH: process.env.PATH || '/usr/local/bin:/usr/bin:/bin' };
|
|
157
|
+
if (process.env.MILO_FREELLMAPI_URL)
|
|
158
|
+
env.MILO_FREELLMAPI_URL = process.env.MILO_FREELLMAPI_URL;
|
|
144
159
|
const opencodeConfig = join(directory, 'opencode-free.json');
|
|
145
160
|
try {
|
|
146
161
|
await readFile(opencodeConfig);
|
|
@@ -172,23 +187,60 @@ export async function installBackgroundService(name, entry, env) {
|
|
|
172
187
|
await command('launchctl', ['bootstrap', domain, plist]);
|
|
173
188
|
}
|
|
174
189
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
190
|
+
// /v1/models asks every source whether it is available, and Freebuff runs `freebuff doctor` to answer.
|
|
191
|
+
// Give each probe longer than that check, and stop after totalMs so a hung service still fails.
|
|
192
|
+
export async function checkService(port = 8788, totalMs = 90000) {
|
|
193
|
+
const deadline = Date.now() + totalMs;
|
|
194
|
+
while (Date.now() < deadline) {
|
|
195
|
+
const timeout = Math.max(1, Math.min(DOCTOR_TIMEOUT_MS + 5000, deadline - Date.now()));
|
|
178
196
|
try {
|
|
179
|
-
const response = await fetch(`http://127.0.0.1:${port}/v1/models`, { signal: AbortSignal.timeout(
|
|
180
|
-
if (response.ok)
|
|
181
|
-
|
|
182
|
-
break;
|
|
183
|
-
}
|
|
197
|
+
const response = await fetch(`http://127.0.0.1:${port}/v1/models`, { signal: AbortSignal.timeout(timeout) });
|
|
198
|
+
if (response.ok)
|
|
199
|
+
return;
|
|
184
200
|
}
|
|
185
201
|
catch { /* Wait for the service manager. */ }
|
|
186
|
-
await delay(1000);
|
|
202
|
+
await delay(Math.max(0, Math.min(1000, deadline - Date.now())));
|
|
203
|
+
}
|
|
204
|
+
throw new Error('grunt service did not start; inspect its service status');
|
|
205
|
+
}
|
|
206
|
+
const systemctl = async (args) => (await promisify(execFile)('systemctl', ['--user', ...args], { timeout: 10000 })).stdout;
|
|
207
|
+
// Read MILO_SOURCES as systemd applies it, including drop-ins, and name the file that set it.
|
|
208
|
+
export async function serviceSources(run = systemctl, name = 'milo') {
|
|
209
|
+
if (run === systemctl && process.platform !== 'linux')
|
|
210
|
+
return;
|
|
211
|
+
let environment, dropIns;
|
|
212
|
+
try {
|
|
213
|
+
[environment, dropIns] = await Promise.all(['Environment', 'DropInPaths'].map(property => run(['show', `${name}.service`, '-p', property, '--value'])));
|
|
214
|
+
}
|
|
215
|
+
catch {
|
|
216
|
+
return;
|
|
217
|
+
}
|
|
218
|
+
const value = environment.match(/(?:^|\s)"?MILO_SOURCES=([^\s"]*)/)?.[1];
|
|
219
|
+
if (value === undefined)
|
|
220
|
+
return;
|
|
221
|
+
let origin = `${name}.service`;
|
|
222
|
+
for (const path of dropIns.trim().split(/\s+/).filter(Boolean)) {
|
|
223
|
+
try {
|
|
224
|
+
if (/^\s*Environment=.*MILO_SOURCES=/m.test(await readFile(path, 'utf8')))
|
|
225
|
+
origin = path;
|
|
226
|
+
}
|
|
227
|
+
catch { /* An unreadable drop-in cannot be named. */ }
|
|
228
|
+
}
|
|
229
|
+
return { sources: value.split(',').map(id => id.trim()), origin };
|
|
230
|
+
}
|
|
231
|
+
export async function checkServiceSources(unavailable, notify = console.log, adapters = ['freebuff', 'opencode'], read = serviceSources) {
|
|
232
|
+
const unit = await read();
|
|
233
|
+
if (!unit)
|
|
234
|
+
return;
|
|
235
|
+
for (const id of adapters) {
|
|
236
|
+
if (unit.sources.includes(id))
|
|
237
|
+
continue;
|
|
238
|
+
if (!unavailable.includes(id))
|
|
239
|
+
unavailable.push(id);
|
|
240
|
+
notify(`${id}: FAIL — ${unit.origin} sets MILO_SOURCES=${unit.sources.join(',')}, which lacks ${id}. Setup does not edit it; add ${id} there or remove that setting.`);
|
|
187
241
|
}
|
|
188
|
-
if (!ready)
|
|
189
|
-
throw new Error('grunt service did not start; inspect its service status');
|
|
190
242
|
}
|
|
191
|
-
export async function checkFallbackServices(unavailable, port = 8788, notify = console.log, adapters = ['freebuff', 'opencode']) {
|
|
243
|
+
export async function checkFallbackServices(unavailable, port = 8788, notify = console.log, adapters = ['freebuff', 'opencode'], wait = { limit: 180000, interval: 2000 }) {
|
|
192
244
|
const directory = await mkdtemp(join(tmpdir(), 'milo-free-check-'));
|
|
193
245
|
try {
|
|
194
246
|
for (const source of adapters) {
|
|
@@ -196,63 +248,211 @@ export async function checkFallbackServices(unavailable, port = 8788, notify = c
|
|
|
196
248
|
notify(`${source}: FAIL — setup incomplete`);
|
|
197
249
|
continue;
|
|
198
250
|
}
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
251
|
+
const deadline = Date.now() + wait.limit;
|
|
252
|
+
let failure, waiting = false;
|
|
253
|
+
for (let attempt = 1;; attempt++) {
|
|
254
|
+
let preparing = false;
|
|
255
|
+
try {
|
|
256
|
+
const response = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
|
|
257
|
+
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
258
|
+
body: JSON.stringify({ model: source, cwd: directory, messages: [{ role: 'user', content: `Reply only: grunt is ready (${source}). Do not use tools or change files. Check ${attempt}.` }], stream: false, max_tokens: 256 }),
|
|
259
|
+
signal: AbortSignal.timeout(120000)
|
|
260
|
+
});
|
|
261
|
+
preparing = response.headers.get('x-grunt-preparing')?.split(',').includes(source) ?? false;
|
|
262
|
+
const answered = response.headers.get('x-grunt-source');
|
|
263
|
+
const result = await response.json().catch(() => ({}));
|
|
264
|
+
if (!response.ok)
|
|
265
|
+
failure = `grunt returned HTTP ${response.status}`;
|
|
266
|
+
else if (answered !== source)
|
|
267
|
+
failure = `${answered ?? 'an unnamed source'} answered instead of ${source}`;
|
|
268
|
+
else if (!result.choices?.[0]?.message?.content?.trim())
|
|
269
|
+
failure = 'no text returned';
|
|
270
|
+
else
|
|
271
|
+
failure = undefined;
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
failure = 'grunt did not answer';
|
|
275
|
+
}
|
|
276
|
+
// Retry only while grunt still runs this adapter's background setup. Each attempt
|
|
277
|
+
// is a new prompt, so the router does not keep a fallback source from an earlier try.
|
|
278
|
+
if (!failure || !preparing || Date.now() + wait.interval > deadline)
|
|
279
|
+
break;
|
|
280
|
+
if (!waiting) {
|
|
281
|
+
waiting = true;
|
|
282
|
+
notify(`${source}: grunt is still preparing this adapter; waiting`);
|
|
283
|
+
}
|
|
284
|
+
await delay(wait.interval);
|
|
211
285
|
}
|
|
212
|
-
|
|
286
|
+
if (failure) {
|
|
213
287
|
unavailable.push(source);
|
|
214
|
-
notify(`${source}:
|
|
288
|
+
notify(`${source}: FAIL — ${failure}`);
|
|
215
289
|
}
|
|
290
|
+
else
|
|
291
|
+
notify(`${source}: PASS — ${source} answered through the grunt service`);
|
|
216
292
|
}
|
|
217
293
|
}
|
|
218
294
|
finally {
|
|
219
295
|
await rm(directory, { recursive: true, force: true });
|
|
220
296
|
}
|
|
221
297
|
}
|
|
222
|
-
export
|
|
223
|
-
|
|
298
|
+
export { configurePi };
|
|
299
|
+
// Same URL and validation as the daemon's RegistrySync; says where the list came from.
|
|
300
|
+
export async function loadRegistry(options = {}) {
|
|
301
|
+
let offline = false, cached = false;
|
|
302
|
+
const registry = new RegistrySync({ directory: configDir(), ...options, notify: () => { offline = true; } });
|
|
224
303
|
try {
|
|
225
304
|
await registry.load();
|
|
305
|
+
cached = registry.current.published !== bundledRegistry.published || JSON.stringify(registry.current) !== JSON.stringify(bundledRegistry);
|
|
226
306
|
await registry.sync();
|
|
227
|
-
return registry.current;
|
|
307
|
+
return { registry: registry.current, origin: (offline ? cached ? 'cache' : 'bundled' : 'remote') };
|
|
228
308
|
}
|
|
229
309
|
finally {
|
|
230
310
|
await registry.close();
|
|
231
311
|
}
|
|
232
312
|
}
|
|
233
|
-
const
|
|
313
|
+
const PREAMBLE = [
|
|
314
|
+
'usegrunt installs grunt, a small daemon that runs on this machine.',
|
|
315
|
+
'It pools the free AI inference quota offered by many services and serves',
|
|
316
|
+
'it as one agent at a local address. Your coding tools talk to grunt; grunt',
|
|
317
|
+
'sends each request to a free service that can answer it.',
|
|
318
|
+
'',
|
|
319
|
+
'Setup will:',
|
|
320
|
+
' 1. Connect Trusty Squire (one Google sign-in)',
|
|
321
|
+
' 2. Pull the current list of free services from the grunt registry',
|
|
322
|
+
' 3. Sign up for each service, connect it to grunt and test it',
|
|
323
|
+
' 4. Connect grunt to the coding tools installed here'
|
|
324
|
+
].join('\n');
|
|
325
|
+
const SQUIRE_NOTICE = [
|
|
326
|
+
'Some free services only work with an account. Trusty Squire is a local',
|
|
327
|
+
'browser helper that signs up for them for you using Google sign-in (OAuth).',
|
|
328
|
+
'',
|
|
329
|
+
'Next, a browser may open and ask you to sign in to Google once. If Trusty',
|
|
330
|
+
'Squire is already connected on this machine, setup reuses it. A captcha or',
|
|
331
|
+
'email check pauses setup until you finish it in the browser.'
|
|
332
|
+
].join('\n');
|
|
333
|
+
const mark = (status) => status === 'pass' ? chalk.green('✔') : status === 'fail' ? chalk.red('✘') : chalk.dim('–');
|
|
334
|
+
const describe = (adapter) => [adapter.kind === 'agent' ? 'coding agent' : 'model API', adapter.model ? `free model ${adapter.model}` : undefined,
|
|
335
|
+
adapter.signup ? `account at ${new URL(adapter.signup).host}` : 'no account needed until it asks'].filter(Boolean).join(' · ');
|
|
336
|
+
// Service lines name their result, so show PASS and FAIL as success and error.
|
|
337
|
+
const report = (shown = new Set()) => (line) => {
|
|
338
|
+
if (shown.has(line))
|
|
339
|
+
return;
|
|
340
|
+
shown.add(line);
|
|
341
|
+
if (/: PASS\b/.test(line))
|
|
342
|
+
log.success(line);
|
|
343
|
+
else if (/: FAIL\b/.test(line))
|
|
344
|
+
log.error(line);
|
|
345
|
+
else
|
|
346
|
+
log.info(line);
|
|
347
|
+
};
|
|
348
|
+
export async function connectHarnesses(port, options = {}) {
|
|
349
|
+
log.step('Connect grunt to your coding tools');
|
|
350
|
+
const found = await detectHarnesses(options.harnesses);
|
|
351
|
+
if (!found.length) {
|
|
352
|
+
log.warn(`No supported coding tool found on PATH (${HARNESSES.map(h => h.binary).join(', ')}). Install one, then rerun usegrunt setup.`);
|
|
353
|
+
return [];
|
|
354
|
+
}
|
|
355
|
+
log.info(`Found: ${found.map(h => h.name).join(', ')}. grunt is added as an extra, named option; your default model and provider stay as they are.`);
|
|
356
|
+
const interactive = process.stdin.isTTY && process.stdout.isTTY && process.env.MILO_SETUP_NO_HUMAN !== '1';
|
|
357
|
+
const choose = options.choose ?? (async (list) => {
|
|
358
|
+
if (!interactive)
|
|
359
|
+
return list.filter(h => h.id === 'pi');
|
|
360
|
+
const picked = await multiselect({ message: 'Connect grunt to which tools? (space toggles, enter confirms)', required: false,
|
|
361
|
+
options: list.map(h => ({ value: h.id, label: h.name, hint: h.launch(port) })), initialValues: list.filter(h => h.id === 'pi').map(h => h.id) });
|
|
362
|
+
return isCancel(picked) ? [] : list.filter(h => picked.includes(h.id));
|
|
363
|
+
});
|
|
364
|
+
const connected = [];
|
|
365
|
+
for (const harness of await choose(found)) {
|
|
366
|
+
const progress = spinner();
|
|
367
|
+
progress.start(`${harness.name}: adding grunt and sending a test request through it`);
|
|
368
|
+
try {
|
|
369
|
+
const { change } = await connectHarness(harness, port, options.run);
|
|
370
|
+
progress.stop(`${harness.name}: PASS — answered through grunt`);
|
|
371
|
+
if (change?.backup)
|
|
372
|
+
log.info(`${harness.name}: previous config saved as ${change.backup}`);
|
|
373
|
+
connected.push(harness);
|
|
374
|
+
}
|
|
375
|
+
catch (error) {
|
|
376
|
+
progress.error(`${harness.name}: FAIL — ${error instanceof Error ? error.message : String(error)}`);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
if (connected.length)
|
|
380
|
+
note(connected.map(h => `${chalk.bold(h.name)}\n ${h.launch(port)}`).join('\n'), 'Start coding with grunt');
|
|
381
|
+
return connected;
|
|
382
|
+
}
|
|
383
|
+
const wizardActions = { banner, preflight, connectSquire, setup, setupFallbacks, installService, configurePi, checkService, checkFallbackServices, loadRegistry, checkServiceSources, connectHarnesses };
|
|
234
384
|
export async function wizard(add, actions = wizardActions) {
|
|
235
385
|
if (add && !Object.hasOwn(PROVIDERS, add))
|
|
236
386
|
throw new Error(`Choose a supported provider: ${Object.keys(PROVIDERS).join(', ')}`);
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
await step('
|
|
387
|
+
await actions.banner?.();
|
|
388
|
+
intro(chalk.hex('#f4b41a').bold('grunt') + chalk.dim(' — npx usegrunt setup'));
|
|
389
|
+
note(PREAMBLE, 'What grunt does');
|
|
390
|
+
await step('Checking prerequisites', () => actions.preflight());
|
|
391
|
+
note(SQUIRE_NOTICE, 'Google sign-in for Trusty Squire');
|
|
392
|
+
await step('Connecting Trusty Squire', () => actions.connectSquire());
|
|
241
393
|
if (add) {
|
|
242
394
|
await step('FreeLLMAPI and provider grants', () => actions.setup(line => log.info(line), [add]));
|
|
395
|
+
await step('Starting the grunt background service', actions.installService);
|
|
396
|
+
await step('Waiting for grunt to answer', () => actions.checkService(Number(process.env.MILO_PORT || '8788')));
|
|
243
397
|
outro(`${add} connected`);
|
|
244
398
|
return;
|
|
245
399
|
}
|
|
246
|
-
const
|
|
247
|
-
const
|
|
248
|
-
const
|
|
249
|
-
|
|
400
|
+
const pulled = actions.loadRegistry ? await step('Pulling the free service list from the registry', () => actions.loadRegistry()) : { registry: bundledRegistry, origin: 'bundled' };
|
|
401
|
+
const { registry } = pulled;
|
|
402
|
+
const published = registry.published.slice(0, 10);
|
|
403
|
+
if (pulled.origin === 'remote')
|
|
404
|
+
log.info(`Registry: ${registry.adapters.length} free services, published ${published}`);
|
|
405
|
+
else
|
|
406
|
+
log.warn(`Registry unreachable; using the ${pulled.origin === 'cache' ? 'list saved by an earlier setup' : 'list bundled with this version'} (published ${published})`);
|
|
407
|
+
const adapters = registry.adapters.filter(adapter => knownAdapters.has(adapter.id));
|
|
408
|
+
log.message(registry.adapters.map(adapter => knownAdapters.has(adapter.id)
|
|
409
|
+
? `${chalk.bold(adapter.id.padEnd(10))} ${chalk.dim(describe(adapter))}`
|
|
410
|
+
: `${chalk.dim(adapter.id.padEnd(10))} ${chalk.dim('skipped: needs a newer usegrunt')}`).join('\n'));
|
|
411
|
+
const ids = adapters.map(adapter => adapter.id);
|
|
412
|
+
const results = new Map(ids.map(id => [id, {}]));
|
|
413
|
+
const unavailable = [];
|
|
414
|
+
const notify = report();
|
|
415
|
+
log.step(`Signing up for ${ids.length} free services`);
|
|
416
|
+
for (const id of ids) {
|
|
417
|
+
const outcome = await step(`${id}: installing and signing in`, () => actions.setupFallbacks({ directory: configDir(), run: command, adapters: [id], notify }), result => result.unavailable.includes(id) ? `${id}: sign-in incomplete` : `${id}: signed in`);
|
|
418
|
+
const failed = outcome.unavailable.includes(id);
|
|
419
|
+
if (failed)
|
|
420
|
+
unavailable.push(id);
|
|
421
|
+
results.get(id).signup = failed ? 'fail' : 'pass';
|
|
422
|
+
}
|
|
423
|
+
log.step('Connecting the services to the grunt daemon');
|
|
424
|
+
await step('Starting the grunt background service', actions.installService);
|
|
250
425
|
const port = Number(process.env.MILO_PORT || '8788');
|
|
251
|
-
await step(
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
426
|
+
await step(`Waiting for grunt to answer at http://127.0.0.1:${port}`, () => actions.checkService(port));
|
|
427
|
+
const beforeSources = new Set(unavailable);
|
|
428
|
+
if (actions.checkServiceSources)
|
|
429
|
+
await step('Checking which services the daemon runs', () => actions.checkServiceSources(unavailable, notify, ids));
|
|
430
|
+
for (const id of ids)
|
|
431
|
+
results.get(id).daemon = beforeSources.has(id) ? 'skip' : unavailable.includes(id) ? 'fail' : 'pass';
|
|
432
|
+
log.step('Testing each service with a real request through grunt');
|
|
433
|
+
for (const id of ids) {
|
|
434
|
+
const before = unavailable.includes(id);
|
|
435
|
+
// Hold the result line until the spinner is gone so the two do not share a row.
|
|
436
|
+
const lines = [], progress = spinner();
|
|
437
|
+
progress.start(`${id}: sending a test request through grunt`);
|
|
438
|
+
try {
|
|
439
|
+
await actions.checkFallbackServices(unavailable, port, line => lines.push(line), [id]);
|
|
440
|
+
}
|
|
441
|
+
finally {
|
|
442
|
+
progress.clear();
|
|
443
|
+
lines.forEach(notify);
|
|
444
|
+
}
|
|
445
|
+
results.get(id).check = before ? 'skip' : unavailable.includes(id) ? 'fail' : 'pass';
|
|
446
|
+
}
|
|
447
|
+
note([`${'service'.padEnd(10)} sign-in daemon test`, ...ids.map(id => {
|
|
448
|
+
const r = results.get(id);
|
|
449
|
+
return `${id.padEnd(10)} ${mark(r.signup)} ${mark(r.daemon)} ${mark(r.check)}`;
|
|
450
|
+
})].join('\n'), 'Free services');
|
|
451
|
+
if (actions.connectHarnesses)
|
|
452
|
+
await actions.connectHarnesses(port);
|
|
453
|
+
else if (actions.configurePi)
|
|
454
|
+
await step('Pi provider', () => actions.configurePi(port));
|
|
455
|
+
outro(unavailable.length ? `grunt is ready; incomplete fallbacks: ${unavailable.join(', ')}. Rerun setup to finish them.` : 'grunt is ready');
|
|
456
|
+
if (unavailable.length)
|
|
257
457
|
throw new Error('Some adapters failed verification. Run usegrunt setup to resume.');
|
|
258
458
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "usegrunt",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22"
|
|
@@ -26,6 +26,7 @@
|
|
|
26
26
|
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
27
27
|
"@trusty-squire/mcp": "1.1.39",
|
|
28
28
|
"@trusty-squire/sdk": "0.1.0",
|
|
29
|
+
"ajv": "^8.20.0",
|
|
29
30
|
"chalk": "^6.0.1",
|
|
30
31
|
"hono": "^4.13.8",
|
|
31
32
|
"ipaddr.js": "^2.5.0",
|