usegrunt 0.0.1 → 0.6.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 +522 -0
- package/dist/agent.js +292 -0
- package/dist/chat.html +39 -0
- package/dist/config.js +29 -0
- package/dist/dots-events.js +141 -0
- package/dist/dots-oauth.js +179 -0
- package/dist/dots-relay.js +143 -0
- package/dist/dots-store.js +70 -0
- package/dist/index.js +64 -0
- package/dist/local/api.js +210 -0
- package/dist/local/approval-proxy.js +71 -0
- package/dist/local/banner.js +136 -0
- package/dist/local/codex.js +115 -0
- package/dist/local/dots-link.js +110 -0
- package/dist/local/dots.js +87 -0
- package/dist/local/fallback-setup.js +192 -0
- package/dist/local/fixtures/ownership-mux.js +42 -0
- package/dist/local/freebuff-tools.js +112 -0
- package/dist/local/freebuff.js +332 -0
- package/dist/local/freellmapi.js +78 -0
- package/dist/local/harness.js +25 -0
- package/dist/local/harnesses.js +164 -0
- package/dist/local/main.js +112 -0
- package/dist/local/multiplexer.js +62 -0
- package/dist/local/native-router.js +56 -0
- package/dist/local/opencode.js +143 -0
- package/dist/local/registry.js +81 -0
- package/dist/local/routing.js +139 -0
- package/dist/local/setup.js +523 -0
- package/dist/local/source.js +111 -0
- package/dist/local/wizard.js +443 -0
- package/dist/openrouter.js +15 -0
- package/dist/registry/adapters.json +10 -0
- package/dist/registry-routes.js +25 -0
- package/dist/registry.js +54 -0
- package/dist/router.js +37 -0
- package/dist/server.js +30 -0
- package/dist/store.js +56 -0
- package/dist/types.js +1 -0
- package/package.json +48 -7
|
@@ -0,0 +1,443 @@
|
|
|
1
|
+
import { promisify } from 'node:util';
|
|
2
|
+
import { spawn, execFile } from 'node:child_process';
|
|
3
|
+
import { mkdir, readFile, writeFile, cp, mkdtemp, rm, access } from 'node:fs/promises';
|
|
4
|
+
import { homedir, tmpdir } from 'node:os';
|
|
5
|
+
import { dirname, join } from 'node:path';
|
|
6
|
+
import { fileURLToPath, pathToFileURL } from 'node:url';
|
|
7
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
8
|
+
import { intro, outro, spinner, log, note, multiselect, isCancel } from '@clack/prompts';
|
|
9
|
+
import chalk from 'chalk';
|
|
10
|
+
import { setupFallbacks } from './fallback-setup.js';
|
|
11
|
+
import { DOCTOR_TIMEOUT_MS } from './freebuff.js';
|
|
12
|
+
import { BrowserSquire, PROVIDERS, setup, squireEnvironment } from './setup.js';
|
|
13
|
+
import { RegistrySync } from './registry.js';
|
|
14
|
+
import { bundledRegistry, knownAdapters } from '../registry.js';
|
|
15
|
+
import { banner } from './banner.js';
|
|
16
|
+
import { HARNESSES, configurePi, connectHarness, detectHarnesses } from './harnesses.js';
|
|
17
|
+
export const configDir = (env = process.env) => env.MILO_CONFIG_DIR || join(env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'milo');
|
|
18
|
+
export async function command(binary, args, interactive = false, signal) {
|
|
19
|
+
await new Promise((resolve, reject) => {
|
|
20
|
+
const child = spawn(binary, args, { stdio: interactive ? 'inherit' : 'ignore', ...(signal ? { signal } : {}) });
|
|
21
|
+
child.once('error', () => reject(new Error(`Could not run ${binary}`)));
|
|
22
|
+
child.once('exit', code => code === 0 ? resolve() : reject(new Error(`${binary} failed; inspect its service status before retrying`)));
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
async function step(label, task, done = () => label) {
|
|
26
|
+
const progress = spinner();
|
|
27
|
+
progress.start(label);
|
|
28
|
+
try {
|
|
29
|
+
const result = await task();
|
|
30
|
+
progress.stop(done(result));
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
33
|
+
catch (error) {
|
|
34
|
+
progress.error(`${label} failed`);
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
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');`;
|
|
66
|
+
const result = await promisify(execFile)(process.execPath, ['--input-type=module', '-e', program], { env: squireEnvironment(), timeout: 10000 });
|
|
67
|
+
return result.stdout === 'present';
|
|
68
|
+
}
|
|
69
|
+
export async function connectSquire(options = {}) {
|
|
70
|
+
const probe = async () => { const squire = new BrowserSquire(); try {
|
|
71
|
+
await squire.connect();
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
await squire.close();
|
|
75
|
+
} };
|
|
76
|
+
try {
|
|
77
|
+
await probe();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
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
|
+
}
|
|
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();
|
|
91
|
+
}
|
|
92
|
+
export async function preflight(run = command) {
|
|
93
|
+
const major = Number(process.versions.node.split('.')[0]);
|
|
94
|
+
if (major < 22 || major >= 25)
|
|
95
|
+
throw new Error('Install Node.js 22 or 24 LTS, then run npx usegrunt setup.');
|
|
96
|
+
if (!['linux', 'darwin'].includes(process.platform))
|
|
97
|
+
throw new Error('Run setup on Linux with systemd or macOS with launchd.');
|
|
98
|
+
for (const [binary, args, fix] of [
|
|
99
|
+
['npm', ['--version'], 'Install Node.js 22 or 24 LTS including npm.'],
|
|
100
|
+
['pi', ['--version'], 'Run npm install --global @mariozechner/pi-coding-agent.'],
|
|
101
|
+
['tar', ['--version'], 'Install tar using your system package manager.']
|
|
102
|
+
]) {
|
|
103
|
+
try {
|
|
104
|
+
await run(binary, [...args]);
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
throw new Error(fix + ' Then run npx usegrunt setup.');
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
if (process.env.MILO_ROUTER_RUNTIME === 'docker') {
|
|
111
|
+
try {
|
|
112
|
+
await run('docker', ['info']);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
throw new Error('Install and start Docker Desktop, then run npx usegrunt setup.');
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
if (process.platform === 'linux')
|
|
120
|
+
await run('systemctl', ['--user', 'show-environment']);
|
|
121
|
+
else
|
|
122
|
+
await run('launchctl', ['print', `gui/${process.getuid()}`]);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
throw new Error('Start a normal user login with its systemd/launchd service manager, then run npx usegrunt setup.');
|
|
126
|
+
}
|
|
127
|
+
}
|
|
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 installService() {
|
|
131
|
+
if (!['linux', 'darwin'].includes(process.platform))
|
|
132
|
+
throw new Error('grunt background service requires systemd or launchd');
|
|
133
|
+
const directory = configDir(), runtime = join(directory, 'runtime');
|
|
134
|
+
// npx cache entries can disappear. Keep a backend runtime outside that cache.
|
|
135
|
+
const root = fileURLToPath(new URL('../../', import.meta.url));
|
|
136
|
+
await mkdir(runtime, { recursive: true, mode: 0o700 });
|
|
137
|
+
await cp(join(root, 'dist'), join(runtime, 'dist'), { recursive: true });
|
|
138
|
+
const pkg = JSON.parse(await readFile(join(root, 'package.json'), 'utf8'));
|
|
139
|
+
await writeFile(join(runtime, 'package.json'), JSON.stringify({ name: 'milo-runtime', private: true, type: 'module', dependencies: pkg.dependencies }), { mode: 0o600 });
|
|
140
|
+
await command('npm', ['install', '--prefix', runtime, '--omit=dev', '--ignore-scripts', '--no-audit', '--no-fund']);
|
|
141
|
+
const entry = join(runtime, 'dist', 'local', 'main.js');
|
|
142
|
+
const port = process.env.MILO_PORT || '8788';
|
|
143
|
+
if (!/^\d+$/.test(port) || Number(port) < 1 || Number(port) > 65535)
|
|
144
|
+
throw new Error('Invalid grunt service port');
|
|
145
|
+
const env = { GRUNT_DAEMON: '1', MILO_CONFIG_DIR: directory, MILO_PORT: port, MILO_SOURCES: 'freebuff,opencode', PATH: process.env.PATH || '/usr/local/bin:/usr/bin:/bin' };
|
|
146
|
+
const opencodeConfig = join(directory, 'opencode-free.json');
|
|
147
|
+
try {
|
|
148
|
+
await readFile(opencodeConfig);
|
|
149
|
+
env.OPENCODE_CONFIG = opencodeConfig;
|
|
150
|
+
}
|
|
151
|
+
catch (error) {
|
|
152
|
+
if (error.code !== 'ENOENT')
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
await installBackgroundService('milo', entry, env);
|
|
156
|
+
}
|
|
157
|
+
export async function installBackgroundService(name, entry, env) {
|
|
158
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name))
|
|
159
|
+
throw new Error('Invalid service name');
|
|
160
|
+
if (process.platform === 'linux') {
|
|
161
|
+
const units = join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'systemd', 'user');
|
|
162
|
+
await mkdir(units, { recursive: true });
|
|
163
|
+
await writeFile(join(units, `${name}.service`), `[Unit]\nDescription=grunt model service\nAfter=network-online.target\n\n[Service]\nExecStart=${systemdQuote(process.execPath)} ${systemdQuote(entry)}\n${Object.entries(env).map(([key, value]) => `Environment=${systemdQuote(`${key}=${value}`)}`).join('\n')}\nRestart=on-failure\nUMask=0077\n\n[Install]\nWantedBy=default.target\n`, { mode: 0o600 });
|
|
164
|
+
await command('systemctl', ['--user', 'daemon-reload']);
|
|
165
|
+
await command('systemctl', ['--user', 'enable', `${name}.service`]);
|
|
166
|
+
await command('systemctl', ['--user', 'restart', `${name}.service`]);
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
const agents = join(homedir(), 'Library', 'LaunchAgents');
|
|
170
|
+
await mkdir(agents, { recursive: true });
|
|
171
|
+
const plist = join(agents, `ai.${name}.service.plist`), domain = `gui/${process.getuid()}`;
|
|
172
|
+
await writeFile(plist, `<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><dict><key>Label</key><string>ai.${name}.service</string><key>ProgramArguments</key><array><string>${xml(process.execPath)}</string><string>${xml(entry)}</string></array><key>EnvironmentVariables</key><dict>${Object.entries(env).map(([key, value]) => `<key>${xml(key)}</key><string>${xml(value)}</string>`).join('')}</dict><key>RunAtLoad</key><true/><key>KeepAlive</key><true/><key>Umask</key><integer>63</integer></dict></plist>`, { mode: 0o600 });
|
|
173
|
+
await command('launchctl', ['bootout', `${domain}/ai.${name}.service`]).catch(() => { });
|
|
174
|
+
await command('launchctl', ['bootstrap', domain, plist]);
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
// /v1/models asks every source whether it is available, and Freebuff runs `freebuff doctor` to answer.
|
|
178
|
+
// Give each probe longer than that check, and stop after totalMs so a hung service still fails.
|
|
179
|
+
export async function checkService(port = 8788, totalMs = 90000) {
|
|
180
|
+
const deadline = Date.now() + totalMs;
|
|
181
|
+
while (Date.now() < deadline) {
|
|
182
|
+
const timeout = Math.max(1, Math.min(DOCTOR_TIMEOUT_MS + 5000, deadline - Date.now()));
|
|
183
|
+
try {
|
|
184
|
+
const response = await fetch(`http://127.0.0.1:${port}/v1/models`, { signal: AbortSignal.timeout(timeout) });
|
|
185
|
+
if (response.ok)
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
catch { /* Wait for the service manager. */ }
|
|
189
|
+
await delay(Math.max(0, Math.min(1000, deadline - Date.now())));
|
|
190
|
+
}
|
|
191
|
+
throw new Error('grunt service did not start; inspect its service status');
|
|
192
|
+
}
|
|
193
|
+
const systemctl = async (args) => (await promisify(execFile)('systemctl', ['--user', ...args], { timeout: 10000 })).stdout;
|
|
194
|
+
// Read MILO_SOURCES as systemd applies it, including drop-ins, and name the file that set it.
|
|
195
|
+
export async function serviceSources(run = systemctl, name = 'milo') {
|
|
196
|
+
if (run === systemctl && process.platform !== 'linux')
|
|
197
|
+
return;
|
|
198
|
+
let environment, dropIns;
|
|
199
|
+
try {
|
|
200
|
+
[environment, dropIns] = await Promise.all(['Environment', 'DropInPaths'].map(property => run(['show', `${name}.service`, '-p', property, '--value'])));
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
const value = environment.match(/(?:^|\s)"?MILO_SOURCES=([^\s"]*)/)?.[1];
|
|
206
|
+
if (value === undefined)
|
|
207
|
+
return;
|
|
208
|
+
let origin = `${name}.service`;
|
|
209
|
+
for (const path of dropIns.trim().split(/\s+/).filter(Boolean)) {
|
|
210
|
+
try {
|
|
211
|
+
if (/^\s*Environment=.*MILO_SOURCES=/m.test(await readFile(path, 'utf8')))
|
|
212
|
+
origin = path;
|
|
213
|
+
}
|
|
214
|
+
catch { /* An unreadable drop-in cannot be named. */ }
|
|
215
|
+
}
|
|
216
|
+
return { sources: value.split(',').map(id => id.trim()), origin };
|
|
217
|
+
}
|
|
218
|
+
export async function checkServiceSources(unavailable, notify = console.log, adapters = ['freebuff', 'opencode'], read = serviceSources) {
|
|
219
|
+
const unit = await read();
|
|
220
|
+
if (!unit)
|
|
221
|
+
return;
|
|
222
|
+
for (const id of adapters) {
|
|
223
|
+
if (unit.sources.includes(id))
|
|
224
|
+
continue;
|
|
225
|
+
if (!unavailable.includes(id))
|
|
226
|
+
unavailable.push(id);
|
|
227
|
+
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.`);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
export async function checkFallbackServices(unavailable, port = 8788, notify = console.log, adapters = ['freebuff', 'opencode'], wait = { limit: 180000, interval: 2000 }) {
|
|
231
|
+
const directory = await mkdtemp(join(tmpdir(), 'milo-free-check-'));
|
|
232
|
+
try {
|
|
233
|
+
for (const source of adapters) {
|
|
234
|
+
if (unavailable.includes(source)) {
|
|
235
|
+
notify(`${source}: FAIL — setup incomplete`);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
const deadline = Date.now() + wait.limit;
|
|
239
|
+
let failure, waiting = false;
|
|
240
|
+
for (let attempt = 1;; attempt++) {
|
|
241
|
+
let preparing = false;
|
|
242
|
+
try {
|
|
243
|
+
const response = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
|
|
244
|
+
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
245
|
+
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 }),
|
|
246
|
+
signal: AbortSignal.timeout(120000)
|
|
247
|
+
});
|
|
248
|
+
preparing = response.headers.get('x-grunt-preparing')?.split(',').includes(source) ?? false;
|
|
249
|
+
const answered = response.headers.get('x-grunt-source');
|
|
250
|
+
const result = await response.json().catch(() => ({}));
|
|
251
|
+
if (!response.ok)
|
|
252
|
+
failure = `grunt returned HTTP ${response.status}`;
|
|
253
|
+
else if (answered !== source)
|
|
254
|
+
failure = `${answered ?? 'an unnamed source'} answered instead of ${source}`;
|
|
255
|
+
else if (!result.choices?.[0]?.message?.content?.trim())
|
|
256
|
+
failure = 'no text returned';
|
|
257
|
+
else
|
|
258
|
+
failure = undefined;
|
|
259
|
+
}
|
|
260
|
+
catch {
|
|
261
|
+
failure = 'grunt did not answer';
|
|
262
|
+
}
|
|
263
|
+
// Retry only while grunt still runs this adapter's background setup. Each attempt
|
|
264
|
+
// is a new prompt, so the router does not keep a fallback source from an earlier try.
|
|
265
|
+
if (!failure || !preparing || Date.now() + wait.interval > deadline)
|
|
266
|
+
break;
|
|
267
|
+
if (!waiting) {
|
|
268
|
+
waiting = true;
|
|
269
|
+
notify(`${source}: grunt is still preparing this adapter; waiting`);
|
|
270
|
+
}
|
|
271
|
+
await delay(wait.interval);
|
|
272
|
+
}
|
|
273
|
+
if (failure) {
|
|
274
|
+
unavailable.push(source);
|
|
275
|
+
notify(`${source}: FAIL — ${failure}`);
|
|
276
|
+
}
|
|
277
|
+
else
|
|
278
|
+
notify(`${source}: PASS — ${source} answered through the grunt service`);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
finally {
|
|
282
|
+
await rm(directory, { recursive: true, force: true });
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
export { configurePi };
|
|
286
|
+
// Same URL and validation as the daemon's RegistrySync; says where the list came from.
|
|
287
|
+
export async function loadRegistry(options = {}) {
|
|
288
|
+
let offline = false, cached = false;
|
|
289
|
+
const registry = new RegistrySync({ directory: configDir(), ...options, notify: () => { offline = true; } });
|
|
290
|
+
try {
|
|
291
|
+
await registry.load();
|
|
292
|
+
cached = registry.current.published !== bundledRegistry.published || JSON.stringify(registry.current) !== JSON.stringify(bundledRegistry);
|
|
293
|
+
await registry.sync();
|
|
294
|
+
return { registry: registry.current, origin: (offline ? cached ? 'cache' : 'bundled' : 'remote') };
|
|
295
|
+
}
|
|
296
|
+
finally {
|
|
297
|
+
await registry.close();
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const PREAMBLE = [
|
|
301
|
+
'usegrunt installs grunt, a small daemon that runs on this machine.',
|
|
302
|
+
'It pools the free AI inference quota offered by many services and serves',
|
|
303
|
+
'it as one agent at a local address. Your coding tools talk to grunt; grunt',
|
|
304
|
+
'sends each request to a free service that can answer it.',
|
|
305
|
+
'',
|
|
306
|
+
'Setup will:',
|
|
307
|
+
' 1. Connect Trusty Squire (one Google sign-in)',
|
|
308
|
+
' 2. Pull the current list of free services from the grunt registry',
|
|
309
|
+
' 3. Sign up for each service, connect it to grunt and test it',
|
|
310
|
+
' 4. Connect grunt to the coding tools installed here'
|
|
311
|
+
].join('\n');
|
|
312
|
+
const SQUIRE_NOTICE = [
|
|
313
|
+
'Some free services only work with an account. Trusty Squire is a local',
|
|
314
|
+
'browser helper that signs up for them for you using Google sign-in (OAuth).',
|
|
315
|
+
'',
|
|
316
|
+
'Next, a browser may open and ask you to sign in to Google once. If Trusty',
|
|
317
|
+
'Squire is already connected on this machine, setup reuses it. A captcha or',
|
|
318
|
+
'email check pauses setup until you finish it in the browser.'
|
|
319
|
+
].join('\n');
|
|
320
|
+
const mark = (status) => status === 'pass' ? chalk.green('✔') : status === 'fail' ? chalk.red('✘') : chalk.dim('–');
|
|
321
|
+
const describe = (adapter) => [adapter.kind === 'agent' ? 'coding agent' : 'model API', adapter.model ? `free model ${adapter.model}` : undefined,
|
|
322
|
+
adapter.signup ? `account at ${new URL(adapter.signup).host}` : 'no account needed until it asks'].filter(Boolean).join(' · ');
|
|
323
|
+
// Service lines name their result, so show PASS and FAIL as success and error.
|
|
324
|
+
const report = (shown = new Set()) => (line) => {
|
|
325
|
+
if (shown.has(line))
|
|
326
|
+
return;
|
|
327
|
+
shown.add(line);
|
|
328
|
+
if (/: PASS\b/.test(line))
|
|
329
|
+
log.success(line);
|
|
330
|
+
else if (/: FAIL\b/.test(line))
|
|
331
|
+
log.error(line);
|
|
332
|
+
else
|
|
333
|
+
log.info(line);
|
|
334
|
+
};
|
|
335
|
+
export async function connectHarnesses(port, options = {}) {
|
|
336
|
+
log.step('Connect grunt to your coding tools');
|
|
337
|
+
const found = await detectHarnesses(options.harnesses);
|
|
338
|
+
if (!found.length) {
|
|
339
|
+
log.warn(`No supported coding tool found on PATH (${HARNESSES.map(h => h.binary).join(', ')}). Install one, then rerun usegrunt setup.`);
|
|
340
|
+
return [];
|
|
341
|
+
}
|
|
342
|
+
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.`);
|
|
343
|
+
const interactive = process.stdin.isTTY && process.stdout.isTTY && process.env.MILO_SETUP_NO_HUMAN !== '1';
|
|
344
|
+
const choose = options.choose ?? (async (list) => {
|
|
345
|
+
if (!interactive)
|
|
346
|
+
return list.filter(h => h.id === 'pi');
|
|
347
|
+
const picked = await multiselect({ message: 'Connect grunt to which tools? (space toggles, enter confirms)', required: false,
|
|
348
|
+
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) });
|
|
349
|
+
return isCancel(picked) ? [] : list.filter(h => picked.includes(h.id));
|
|
350
|
+
});
|
|
351
|
+
const connected = [];
|
|
352
|
+
for (const harness of await choose(found)) {
|
|
353
|
+
const progress = spinner();
|
|
354
|
+
progress.start(`${harness.name}: adding grunt and sending a test request through it`);
|
|
355
|
+
try {
|
|
356
|
+
const { change } = await connectHarness(harness, port, options.run);
|
|
357
|
+
progress.stop(`${harness.name}: PASS — answered through grunt`);
|
|
358
|
+
if (change?.backup)
|
|
359
|
+
log.info(`${harness.name}: previous config saved as ${change.backup}`);
|
|
360
|
+
connected.push(harness);
|
|
361
|
+
}
|
|
362
|
+
catch (error) {
|
|
363
|
+
progress.error(`${harness.name}: FAIL — ${error instanceof Error ? error.message : String(error)}`);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
if (connected.length)
|
|
367
|
+
note(connected.map(h => `${chalk.bold(h.name)}\n ${h.launch(port)}`).join('\n'), 'Start coding with grunt');
|
|
368
|
+
return connected;
|
|
369
|
+
}
|
|
370
|
+
const wizardActions = { banner, preflight, connectSquire, setup, setupFallbacks, installService, configurePi, checkService, checkFallbackServices, loadRegistry, checkServiceSources, connectHarnesses };
|
|
371
|
+
export async function wizard(add, actions = wizardActions) {
|
|
372
|
+
if (add && !Object.hasOwn(PROVIDERS, add))
|
|
373
|
+
throw new Error(`Choose a supported provider: ${Object.keys(PROVIDERS).join(', ')}`);
|
|
374
|
+
await actions.banner?.();
|
|
375
|
+
intro(chalk.hex('#f4b41a').bold('grunt') + chalk.dim(' — npx usegrunt setup'));
|
|
376
|
+
note(PREAMBLE, 'What grunt does');
|
|
377
|
+
await step('Checking prerequisites', () => actions.preflight());
|
|
378
|
+
note(SQUIRE_NOTICE, 'Google sign-in for Trusty Squire');
|
|
379
|
+
await step('Connecting Trusty Squire', () => actions.connectSquire());
|
|
380
|
+
if (add) {
|
|
381
|
+
await step('FreeLLMAPI and provider grants', () => actions.setup(line => log.info(line), [add]));
|
|
382
|
+
outro(`${add} connected`);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
const pulled = actions.loadRegistry ? await step('Pulling the free service list from the registry', () => actions.loadRegistry()) : { registry: bundledRegistry, origin: 'bundled' };
|
|
386
|
+
const { registry } = pulled;
|
|
387
|
+
const published = registry.published.slice(0, 10);
|
|
388
|
+
if (pulled.origin === 'remote')
|
|
389
|
+
log.info(`Registry: ${registry.adapters.length} free services, published ${published}`);
|
|
390
|
+
else
|
|
391
|
+
log.warn(`Registry unreachable; using the ${pulled.origin === 'cache' ? 'list saved by an earlier setup' : 'list bundled with this version'} (published ${published})`);
|
|
392
|
+
const adapters = registry.adapters.filter(adapter => knownAdapters.has(adapter.id));
|
|
393
|
+
log.message(registry.adapters.map(adapter => knownAdapters.has(adapter.id)
|
|
394
|
+
? `${chalk.bold(adapter.id.padEnd(10))} ${chalk.dim(describe(adapter))}`
|
|
395
|
+
: `${chalk.dim(adapter.id.padEnd(10))} ${chalk.dim('skipped: needs a newer usegrunt')}`).join('\n'));
|
|
396
|
+
const ids = adapters.map(adapter => adapter.id);
|
|
397
|
+
const results = new Map(ids.map(id => [id, {}]));
|
|
398
|
+
const unavailable = [];
|
|
399
|
+
const notify = report();
|
|
400
|
+
log.step(`Signing up for ${ids.length} free services`);
|
|
401
|
+
for (const id of ids) {
|
|
402
|
+
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`);
|
|
403
|
+
const failed = outcome.unavailable.includes(id);
|
|
404
|
+
if (failed)
|
|
405
|
+
unavailable.push(id);
|
|
406
|
+
results.get(id).signup = failed ? 'fail' : 'pass';
|
|
407
|
+
}
|
|
408
|
+
log.step('Connecting the services to the grunt daemon');
|
|
409
|
+
await step('Starting the grunt background service', actions.installService);
|
|
410
|
+
const port = Number(process.env.MILO_PORT || '8788');
|
|
411
|
+
await step(`Waiting for grunt to answer at http://127.0.0.1:${port}`, () => actions.checkService(port));
|
|
412
|
+
const beforeSources = new Set(unavailable);
|
|
413
|
+
if (actions.checkServiceSources)
|
|
414
|
+
await step('Checking which services the daemon runs', () => actions.checkServiceSources(unavailable, notify, ids));
|
|
415
|
+
for (const id of ids)
|
|
416
|
+
results.get(id).daemon = beforeSources.has(id) ? 'skip' : unavailable.includes(id) ? 'fail' : 'pass';
|
|
417
|
+
log.step('Testing each service with a real request through grunt');
|
|
418
|
+
for (const id of ids) {
|
|
419
|
+
const before = unavailable.includes(id);
|
|
420
|
+
// Hold the result line until the spinner is gone so the two do not share a row.
|
|
421
|
+
const lines = [], progress = spinner();
|
|
422
|
+
progress.start(`${id}: sending a test request through grunt`);
|
|
423
|
+
try {
|
|
424
|
+
await actions.checkFallbackServices(unavailable, port, line => lines.push(line), [id]);
|
|
425
|
+
}
|
|
426
|
+
finally {
|
|
427
|
+
progress.clear();
|
|
428
|
+
lines.forEach(notify);
|
|
429
|
+
}
|
|
430
|
+
results.get(id).check = before ? 'skip' : unavailable.includes(id) ? 'fail' : 'pass';
|
|
431
|
+
}
|
|
432
|
+
note([`${'service'.padEnd(10)} sign-in daemon test`, ...ids.map(id => {
|
|
433
|
+
const r = results.get(id);
|
|
434
|
+
return `${id.padEnd(10)} ${mark(r.signup)} ${mark(r.daemon)} ${mark(r.check)}`;
|
|
435
|
+
})].join('\n'), 'Free services');
|
|
436
|
+
if (actions.connectHarnesses)
|
|
437
|
+
await actions.connectHarnesses(port);
|
|
438
|
+
else if (actions.configurePi)
|
|
439
|
+
await step('Pi provider', () => actions.configurePi(port));
|
|
440
|
+
outro(unavailable.length ? `grunt is ready; incomplete fallbacks: ${unavailable.join(', ')}. Rerun setup to finish them.` : 'grunt is ready');
|
|
441
|
+
if (unavailable.length)
|
|
442
|
+
throw new Error('Some adapters failed verification. Run usegrunt setup to resume.');
|
|
443
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// One attempt per call. Milo owns model selection, retry and cancellation.
|
|
2
|
+
export class OpenRouterClient {
|
|
3
|
+
fetchImpl;
|
|
4
|
+
baseUrl;
|
|
5
|
+
constructor(options = {}) {
|
|
6
|
+
this.fetchImpl = options.fetch ?? fetch;
|
|
7
|
+
this.baseUrl = options.baseUrl ?? "https://openrouter.ai/api/v1";
|
|
8
|
+
}
|
|
9
|
+
chatCompletions(options) {
|
|
10
|
+
return this.fetchImpl(`${this.baseUrl}/chat/completions`, {
|
|
11
|
+
method: "POST", headers: { authorization: options.headers.get("authorization") ?? "", "content-type": "application/json", "x-title": "Milo" },
|
|
12
|
+
body: JSON.stringify(options.body), signal: options.signal,
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 1,
|
|
3
|
+
"published": "2026-11-11T00:00:00Z",
|
|
4
|
+
"adapters": [
|
|
5
|
+
{ "id": "freebuff", "kind": "agent", "tier": "stable", "weight": 0.4,
|
|
6
|
+
"modalities": ["text"], "install": "npm:freebuff", "signup": "https://freebuff.com/login" },
|
|
7
|
+
{ "id": "opencode", "kind": "agent", "tier": "stable", "weight": 0.4,
|
|
8
|
+
"modalities": ["text"], "install": "npm:opencode-ai", "model": "big-pickle" }
|
|
9
|
+
]
|
|
10
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { readFile } from 'node:fs/promises';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
3
|
+
import { Hono } from 'hono';
|
|
4
|
+
import { validateRegistry } from './registry.js';
|
|
5
|
+
// Ordinary deployments never publish registry edits. Only the dispatch workflow writes this file.
|
|
6
|
+
export function registryRoutes(path = process.env.GRUNT_REGISTRY_FILE ?? '/data/registry.json') {
|
|
7
|
+
const app = new Hono();
|
|
8
|
+
app.get('/v1/registry', async (c) => {
|
|
9
|
+
try {
|
|
10
|
+
const registry = validateRegistry(JSON.parse(await readFile(path, 'utf8')));
|
|
11
|
+
const body = JSON.stringify(registry);
|
|
12
|
+
const etag = '"' + createHash('sha256').update(body).digest('hex') + '"';
|
|
13
|
+
c.header('ETag', etag);
|
|
14
|
+
c.header('Cache-Control', 'public, max-age=0, must-revalidate');
|
|
15
|
+
if (c.req.header('If-None-Match')?.split(',').some(value => value.trim().replace(/^W\//, '') === etag || value.trim() === '*'))
|
|
16
|
+
return c.body(null, 304);
|
|
17
|
+
c.header('Content-Type', 'application/json');
|
|
18
|
+
return c.body(body);
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return c.json({ error: 'No valid registry has been published' }, 503);
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
return app;
|
|
25
|
+
}
|
package/dist/registry.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { existsSync, readFileSync } from 'node:fs';
|
|
2
|
+
import { pathToFileURL } from 'node:url';
|
|
3
|
+
export const knownAdapters = new Set(['freebuff', 'opencode']);
|
|
4
|
+
export function validateRegistry(value) {
|
|
5
|
+
const fail = () => { throw new Error('Invalid adapter registry'); };
|
|
6
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
7
|
+
return fail();
|
|
8
|
+
const registry = value;
|
|
9
|
+
if (registry.version !== 1 || typeof registry.published !== 'string' || !/^\d{4}-\d\d-\d\dT\d\d:\d\d:\d\d(?:\.\d+)?Z$/.test(registry.published) || !Number.isFinite(Date.parse(registry.published))
|
|
10
|
+
|| !Array.isArray(registry.adapters) || !registry.adapters.length || registry.adapters.length > 100)
|
|
11
|
+
return fail();
|
|
12
|
+
const ids = new Set();
|
|
13
|
+
const weights = { stable: 0, experimental: 0 };
|
|
14
|
+
for (const item of registry.adapters) {
|
|
15
|
+
if (!item || typeof item !== 'object' || Array.isArray(item))
|
|
16
|
+
return fail();
|
|
17
|
+
const a = item;
|
|
18
|
+
if (typeof a.id !== 'string' || !/^[a-z][a-z0-9-]{0,63}$/.test(a.id) || ids.has(a.id)
|
|
19
|
+
|| !['agent', 'model'].includes(String(a.kind)) || !['stable', 'experimental'].includes(String(a.tier))
|
|
20
|
+
|| typeof a.weight !== 'number' || !Number.isFinite(a.weight) || a.weight <= 0 || a.weight > 1
|
|
21
|
+
|| !Array.isArray(a.modalities) || !a.modalities.length || a.modalities.some(m => !['text', 'image'].includes(m))
|
|
22
|
+
|| new Set(a.modalities).size !== a.modalities.length || typeof a.install !== 'string' || !/^npm:[a-z0-9@/._-]+$/.test(a.install)
|
|
23
|
+
|| (a.model !== undefined && (typeof a.model !== 'string' || !a.model.trim())))
|
|
24
|
+
return fail();
|
|
25
|
+
if (a.signup !== undefined) {
|
|
26
|
+
if (typeof a.signup !== 'string')
|
|
27
|
+
return fail();
|
|
28
|
+
let url;
|
|
29
|
+
try {
|
|
30
|
+
url = new URL(a.signup);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
return fail();
|
|
34
|
+
}
|
|
35
|
+
if (url.protocol !== 'https:' || url.username || url.password)
|
|
36
|
+
return fail();
|
|
37
|
+
}
|
|
38
|
+
// Known adapters have fixed installers and capabilities; registry data never supplies code.
|
|
39
|
+
if (knownAdapters.has(a.id) && (a.kind !== 'agent' || a.install !== (a.id === 'freebuff' ? 'npm:freebuff' : 'npm:opencode-ai')
|
|
40
|
+
|| a.modalities.some(m => m !== 'text')))
|
|
41
|
+
return fail();
|
|
42
|
+
ids.add(a.id);
|
|
43
|
+
weights[a.tier] += a.weight;
|
|
44
|
+
}
|
|
45
|
+
if (weights.stable && weights.experimental && (Math.abs(weights.stable - 0.8) > 1e-9 || Math.abs(weights.experimental - 0.2) > 1e-9))
|
|
46
|
+
return fail();
|
|
47
|
+
return structuredClone(value);
|
|
48
|
+
}
|
|
49
|
+
const packaged = new URL('./registry/adapters.json', import.meta.url);
|
|
50
|
+
export const bundledRegistry = validateRegistry(JSON.parse(readFileSync(existsSync(packaged) ? packaged : new URL('../registry/adapters.json', import.meta.url), 'utf8')));
|
|
51
|
+
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
|
52
|
+
validateRegistry(JSON.parse(readFileSync(process.argv[2] ?? 'registry/adapters.json', 'utf8')));
|
|
53
|
+
console.log('Adapter registry is valid');
|
|
54
|
+
}
|
package/dist/router.js
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const prior = (model, quality, input, output, latencyMs) => ({
|
|
2
|
+
model, quality: { chat: quality, code: quality, research: quality, writing: quality },
|
|
3
|
+
inputUsdPerM: input, outputUsdPerM: output, contextLength: 128_000, reliability: 0.98, latencyMs,
|
|
4
|
+
});
|
|
5
|
+
// Initial estimates, replaced by measured call averages as traffic arrives.
|
|
6
|
+
export const DEFAULT_WHITELIST = [
|
|
7
|
+
prior("deepseek/deepseek-v4.1-flash", 0.85, 0.14, 0.28, 1500),
|
|
8
|
+
prior("z-ai/glm-5.3-flash", 0.84, 0.1, 0.4, 1700),
|
|
9
|
+
prior("z-ai/glm-5.3", 0.91, 0.6, 2, 3500),
|
|
10
|
+
prior("openai/gpt-5.6-luna", 0.95, 2.5, 15, 4500),
|
|
11
|
+
prior("anthropic/claude-opus-5", 0.96, 5, 25, 5000),
|
|
12
|
+
];
|
|
13
|
+
const weights = {
|
|
14
|
+
chat: { latency: 0.0001, cost: 2 }, code: { latency: 0.000005, cost: 0.5 },
|
|
15
|
+
research: { latency: 0.000005, cost: 0.5 }, writing: { latency: 0.000005, cost: 0.5 },
|
|
16
|
+
};
|
|
17
|
+
export function whitelist(config = {}) {
|
|
18
|
+
return (config.whitelist ?? DEFAULT_WHITELIST).filter(m => !m.model.trim().toLowerCase().startsWith("google/"));
|
|
19
|
+
}
|
|
20
|
+
export function pick(task, calls = [], config = {}, excluded = [], now = Date.now()) {
|
|
21
|
+
const w = weights[task.kind];
|
|
22
|
+
const candidates = whitelist(config).filter(m => !excluded.includes(m.model) && m.contextLength >= task.inputTokens + task.outputTokens &&
|
|
23
|
+
!calls.some(c => c.model === m.model && c.outcome === "429" && now - c.ts < 60_000)).map(m => {
|
|
24
|
+
const history = calls.filter(c => c.model === m.model);
|
|
25
|
+
const reliability = (history.filter(c => c.outcome === "ok").length + (m.reliability ?? 0.98) * 5) / (history.length + 5);
|
|
26
|
+
const latency = (history.reduce((n, c) => n + c.latency_ms, 0) + (m.latencyMs ?? 2000) * 5) / (history.length + 5);
|
|
27
|
+
const cost = (task.inputTokens * m.inputUsdPerM + task.outputTokens * m.outputUsdPerM) / 1e6;
|
|
28
|
+
const quality = m.quality[task.kind];
|
|
29
|
+
const score = reliability * quality - w.latency * latency - w.cost * cost;
|
|
30
|
+
return { model: m.model, score, reliability, quality, latency, cost };
|
|
31
|
+
}).sort((a, b) => b.score - a.score);
|
|
32
|
+
const best = candidates[0];
|
|
33
|
+
if (!best)
|
|
34
|
+
throw new Error("No whitelisted model fits this task or all models are cooling down.");
|
|
35
|
+
return { model: best.model, score: best.score,
|
|
36
|
+
reason: `Utility ${best.score.toFixed(3)}: reliability ${best.reliability.toFixed(2)}, quality ${best.quality.toFixed(2)}, expected latency ${Math.round(best.latency)}ms, expected cost $${best.cost.toFixed(5)}.`, candidates };
|
|
37
|
+
}
|
package/dist/server.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { serve } from "@hono/node-server";
|
|
2
|
+
import { MiloAgent } from "./agent.js";
|
|
3
|
+
import { loadConfig } from "./config.js";
|
|
4
|
+
import { agentRoutes } from "./index.js";
|
|
5
|
+
import { DotsStore } from "./dots-store.js";
|
|
6
|
+
import { dotsRelay } from "./dots-relay.js";
|
|
7
|
+
import { registryRoutes } from './registry-routes.js';
|
|
8
|
+
const port = Number(process.env.PORT ?? 8787);
|
|
9
|
+
if (!Number.isInteger(port) || port < 1 || port > 65535)
|
|
10
|
+
throw new Error("PORT must be an integer between 1 and 65535.");
|
|
11
|
+
const agent = await MiloAgent.open({ ...loadConfig(process.env.MILO_CONFIG), dbUrl: process.env.MILO_DB ?? "file:/data/milo.db" });
|
|
12
|
+
const dotsStore = new DotsStore(agent.store.db);
|
|
13
|
+
await dotsStore.init();
|
|
14
|
+
const relay = dotsRelay(dotsStore, { origin: process.env.MILO_PUBLIC_URL ?? 'https://api.grunt.run' });
|
|
15
|
+
const stopRelay = relay.start();
|
|
16
|
+
const app = relay.app.route('/', registryRoutes()).route('/', agentRoutes(agent));
|
|
17
|
+
const server = serve({ fetch: app.fetch, port, hostname: "0.0.0.0" }, () => console.log(`grunt is listening on port ${port}. Open /chat.`));
|
|
18
|
+
let stopping = false;
|
|
19
|
+
async function stop() {
|
|
20
|
+
if (stopping)
|
|
21
|
+
return;
|
|
22
|
+
stopping = true;
|
|
23
|
+
if ("closeAllConnections" in server)
|
|
24
|
+
server.closeAllConnections();
|
|
25
|
+
server.close();
|
|
26
|
+
stopRelay();
|
|
27
|
+
await agent.close();
|
|
28
|
+
}
|
|
29
|
+
process.on("SIGTERM", () => { void stop(); });
|
|
30
|
+
process.on("SIGINT", () => { void stop(); });
|