usegrunt 0.0.0-stage → 0.5.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 +463 -2
- 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 +196 -0
- package/dist/local/approval-proxy.js +71 -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 +191 -0
- package/dist/local/fixtures/ownership-mux.js +42 -0
- package/dist/local/freebuff.js +300 -0
- package/dist/local/freellmapi.js +77 -0
- package/dist/local/harness.js +25 -0
- package/dist/local/main.js +107 -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 +138 -0
- package/dist/local/setup.js +523 -0
- package/dist/local/source.js +105 -0
- package/dist/local/wizard.js +258 -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 +50 -4
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
import { promisify } from 'node:util';
|
|
2
|
+
import { spawn, execFile } from 'node:child_process';
|
|
3
|
+
import { mkdir, readFile, writeFile, rename, cp, mkdtemp, rm } 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 } from '@clack/prompts';
|
|
9
|
+
import chalk from 'chalk';
|
|
10
|
+
import { setupFallbacks } from './fallback-setup.js';
|
|
11
|
+
import { BrowserSquire, PROVIDERS, setup, squireEnvironment } from './setup.js';
|
|
12
|
+
import { RegistrySync } from './registry.js';
|
|
13
|
+
import { bundledRegistry, knownAdapters } from '../registry.js';
|
|
14
|
+
export const configDir = (env = process.env) => env.MILO_CONFIG_DIR || join(env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'milo');
|
|
15
|
+
export async function command(binary, args, interactive = false, signal) {
|
|
16
|
+
await new Promise((resolve, reject) => {
|
|
17
|
+
const child = spawn(binary, args, { stdio: interactive ? 'inherit' : 'ignore', ...(signal ? { signal } : {}) });
|
|
18
|
+
child.once('error', () => reject(new Error(`Could not run ${binary}`)));
|
|
19
|
+
child.once('exit', code => code === 0 ? resolve() : reject(new Error(`${binary} failed; inspect its service status before retrying`)));
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
async function step(label, task) {
|
|
23
|
+
const progress = spinner();
|
|
24
|
+
progress.start(label);
|
|
25
|
+
try {
|
|
26
|
+
const result = await task();
|
|
27
|
+
progress.stop(label);
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
progress.stop(`${label} failed`);
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
export async function squireSessionExists() {
|
|
36
|
+
const sessionModule = join(dirname(fileURLToPath(import.meta.resolve('@trusty-squire/mcp'))), 'session.js');
|
|
37
|
+
const program = `const {openSessionStorage} = await import(${JSON.stringify(pathToFileURL(sessionModule).href)}); process.stdout.write((await (await openSessionStorage()).read()) ? 'present' : 'missing');`;
|
|
38
|
+
const result = await promisify(execFile)(process.execPath, ['--input-type=module', '-e', program], { env: squireEnvironment(), timeout: 10000 });
|
|
39
|
+
return result.stdout === 'present';
|
|
40
|
+
}
|
|
41
|
+
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
|
+
const probe = async () => { const squire = new BrowserSquire(); try {
|
|
54
|
+
await squire.connect();
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
await squire.close();
|
|
58
|
+
} };
|
|
59
|
+
try {
|
|
60
|
+
await probe();
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
if (!existing)
|
|
64
|
+
throw error;
|
|
65
|
+
await install();
|
|
66
|
+
await probe();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export async function preflight(run = command) {
|
|
70
|
+
const major = Number(process.versions.node.split('.')[0]);
|
|
71
|
+
if (major < 22 || major >= 25)
|
|
72
|
+
throw new Error('Install Node.js 22 or 24 LTS, then run npx usegrunt setup.');
|
|
73
|
+
if (!['linux', 'darwin'].includes(process.platform))
|
|
74
|
+
throw new Error('Run setup on Linux with systemd or macOS with launchd.');
|
|
75
|
+
for (const [binary, args, fix] of [
|
|
76
|
+
['npm', ['--version'], 'Install Node.js 22 or 24 LTS including npm.'],
|
|
77
|
+
['pi', ['--version'], 'Run npm install --global @mariozechner/pi-coding-agent.'],
|
|
78
|
+
['tar', ['--version'], 'Install tar using your system package manager.']
|
|
79
|
+
]) {
|
|
80
|
+
try {
|
|
81
|
+
await run(binary, [...args]);
|
|
82
|
+
}
|
|
83
|
+
catch {
|
|
84
|
+
throw new Error(fix + ' Then run npx usegrunt setup.');
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (process.env.MILO_ROUTER_RUNTIME === 'docker') {
|
|
88
|
+
try {
|
|
89
|
+
await run('docker', ['info']);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
throw new Error('Install and start Docker Desktop, then run npx usegrunt setup.');
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
if (process.platform === 'linux')
|
|
97
|
+
await run('systemctl', ['--user', 'show-environment']);
|
|
98
|
+
else
|
|
99
|
+
await run('launchctl', ['print', `gui/${process.getuid()}`]);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
throw new Error('Start a normal user login with its systemd/launchd service manager, then run npx usegrunt setup.');
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
export async function configurePi(port = 8788) {
|
|
106
|
+
const directory = process.env.PI_CODING_AGENT_DIR || join(homedir(), '.pi', 'agent');
|
|
107
|
+
const path = join(directory, 'models.json');
|
|
108
|
+
let saved = {};
|
|
109
|
+
try {
|
|
110
|
+
saved = JSON.parse(await readFile(path, 'utf8'));
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
if (error.code !== 'ENOENT')
|
|
114
|
+
throw new Error('Cannot read Pi models config; repair it before retrying setup');
|
|
115
|
+
}
|
|
116
|
+
if (!saved || typeof saved !== 'object' || Array.isArray(saved) || (saved.providers !== undefined && (!saved.providers || typeof saved.providers !== 'object' || Array.isArray(saved.providers))))
|
|
117
|
+
throw new Error('Invalid Pi models config');
|
|
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);
|
|
125
|
+
}
|
|
126
|
+
const systemdQuote = (value) => '"' + value.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('%', '%%').replaceAll('$', '$$').replaceAll('\n', '\\n') + '"';
|
|
127
|
+
const xml = (value) => value.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"');
|
|
128
|
+
export async function installService() {
|
|
129
|
+
if (!['linux', 'darwin'].includes(process.platform))
|
|
130
|
+
throw new Error('grunt background service requires systemd or launchd');
|
|
131
|
+
const directory = configDir(), runtime = join(directory, 'runtime');
|
|
132
|
+
// npx cache entries can disappear. Keep a backend runtime outside that cache.
|
|
133
|
+
const root = fileURLToPath(new URL('../../', import.meta.url));
|
|
134
|
+
await mkdir(runtime, { recursive: true, mode: 0o700 });
|
|
135
|
+
await cp(join(root, 'dist'), join(runtime, 'dist'), { recursive: true });
|
|
136
|
+
const pkg = JSON.parse(await readFile(join(root, 'package.json'), 'utf8'));
|
|
137
|
+
await writeFile(join(runtime, 'package.json'), JSON.stringify({ name: 'milo-runtime', private: true, type: 'module', dependencies: pkg.dependencies }), { mode: 0o600 });
|
|
138
|
+
await command('npm', ['install', '--prefix', runtime, '--omit=dev', '--ignore-scripts', '--no-audit', '--no-fund']);
|
|
139
|
+
const entry = join(runtime, 'dist', 'local', 'main.js');
|
|
140
|
+
const port = process.env.MILO_PORT || '8788';
|
|
141
|
+
if (!/^\d+$/.test(port) || Number(port) < 1 || Number(port) > 65535)
|
|
142
|
+
throw new Error('Invalid grunt service port');
|
|
143
|
+
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' };
|
|
144
|
+
const opencodeConfig = join(directory, 'opencode-free.json');
|
|
145
|
+
try {
|
|
146
|
+
await readFile(opencodeConfig);
|
|
147
|
+
env.OPENCODE_CONFIG = opencodeConfig;
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
if (error.code !== 'ENOENT')
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
await installBackgroundService('milo', entry, env);
|
|
154
|
+
}
|
|
155
|
+
export async function installBackgroundService(name, entry, env) {
|
|
156
|
+
if (!/^[a-z][a-z0-9-]*$/.test(name))
|
|
157
|
+
throw new Error('Invalid service name');
|
|
158
|
+
if (process.platform === 'linux') {
|
|
159
|
+
const units = join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'systemd', 'user');
|
|
160
|
+
await mkdir(units, { recursive: true });
|
|
161
|
+
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 });
|
|
162
|
+
await command('systemctl', ['--user', 'daemon-reload']);
|
|
163
|
+
await command('systemctl', ['--user', 'enable', `${name}.service`]);
|
|
164
|
+
await command('systemctl', ['--user', 'restart', `${name}.service`]);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
const agents = join(homedir(), 'Library', 'LaunchAgents');
|
|
168
|
+
await mkdir(agents, { recursive: true });
|
|
169
|
+
const plist = join(agents, `ai.${name}.service.plist`), domain = `gui/${process.getuid()}`;
|
|
170
|
+
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 });
|
|
171
|
+
await command('launchctl', ['bootout', `${domain}/ai.${name}.service`]).catch(() => { });
|
|
172
|
+
await command('launchctl', ['bootstrap', domain, plist]);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
export async function checkService(port = 8788) {
|
|
176
|
+
let ready = false;
|
|
177
|
+
for (let attempt = 0; attempt < 30; attempt++) {
|
|
178
|
+
try {
|
|
179
|
+
const response = await fetch(`http://127.0.0.1:${port}/v1/models`, { signal: AbortSignal.timeout(1000) });
|
|
180
|
+
if (response.ok) {
|
|
181
|
+
ready = true;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
catch { /* Wait for the service manager. */ }
|
|
186
|
+
await delay(1000);
|
|
187
|
+
}
|
|
188
|
+
if (!ready)
|
|
189
|
+
throw new Error('grunt service did not start; inspect its service status');
|
|
190
|
+
}
|
|
191
|
+
export async function checkFallbackServices(unavailable, port = 8788, notify = console.log, adapters = ['freebuff', 'opencode']) {
|
|
192
|
+
const directory = await mkdtemp(join(tmpdir(), 'milo-free-check-'));
|
|
193
|
+
try {
|
|
194
|
+
for (const source of adapters) {
|
|
195
|
+
if (unavailable.includes(source)) {
|
|
196
|
+
notify(`${source}: FAIL — setup incomplete`);
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
try {
|
|
200
|
+
const response = await fetch(`http://127.0.0.1:${port}/v1/chat/completions`, {
|
|
201
|
+
method: 'POST', headers: { 'Content-Type': 'application/json' },
|
|
202
|
+
body: JSON.stringify({ model: source, cwd: directory, messages: [{ role: 'user', content: `Reply only: grunt is ready (${source}). Do not use tools or change files.` }], stream: false, max_tokens: 256 }),
|
|
203
|
+
signal: AbortSignal.timeout(120000)
|
|
204
|
+
});
|
|
205
|
+
if (!response.ok)
|
|
206
|
+
throw new Error('Free fallback did not answer');
|
|
207
|
+
const result = await response.json();
|
|
208
|
+
if (!result.choices?.[0]?.message?.content?.trim())
|
|
209
|
+
throw new Error('Free fallback returned no text');
|
|
210
|
+
notify(`${source}: PASS — answered through the grunt service`);
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
unavailable.push(source);
|
|
214
|
+
notify(`${source}: service check failed; rerun setup to finish this fallback`);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
finally {
|
|
219
|
+
await rm(directory, { recursive: true, force: true });
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
export async function loadRegistry() {
|
|
223
|
+
const registry = new RegistrySync({ directory: configDir(), notify: line => log.info(line) });
|
|
224
|
+
try {
|
|
225
|
+
await registry.load();
|
|
226
|
+
await registry.sync();
|
|
227
|
+
return registry.current;
|
|
228
|
+
}
|
|
229
|
+
finally {
|
|
230
|
+
await registry.close();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
const wizardActions = { preflight, connectSquire, setup, setupFallbacks, installService, configurePi, checkService, checkFallbackServices, loadRegistry };
|
|
234
|
+
export async function wizard(add, actions = wizardActions) {
|
|
235
|
+
if (add && !Object.hasOwn(PROVIDERS, add))
|
|
236
|
+
throw new Error(`Choose a supported provider: ${Object.keys(PROVIDERS).join(', ')}`);
|
|
237
|
+
intro(chalk.hex('#cf3a52').bold('grunt') + chalk.dim(' — npx usegrunt setup'));
|
|
238
|
+
log.info('Sign in to Google once. Your local Trusty Squire uses that browser session to sign up for free services for you.');
|
|
239
|
+
await step('Prerequisites', () => actions.preflight());
|
|
240
|
+
await step('Trusty Squire', () => actions.connectSquire());
|
|
241
|
+
if (add) {
|
|
242
|
+
await step('FreeLLMAPI and provider grants', () => actions.setup(line => log.info(line), [add]));
|
|
243
|
+
outro(`${add} connected`);
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
const registry = actions.loadRegistry ? await step('Adapter registry', actions.loadRegistry) : bundledRegistry;
|
|
247
|
+
const adapters = registry.adapters.filter(adapter => knownAdapters.has(adapter.id)).map(adapter => adapter.id);
|
|
248
|
+
const fallbacks = await step('Freebuff and OpenCode', () => actions.setupFallbacks({ directory: configDir(), run: command, adapters, notify: line => log.info(line) }));
|
|
249
|
+
await step('grunt background service', actions.installService);
|
|
250
|
+
const port = Number(process.env.MILO_PORT || '8788');
|
|
251
|
+
await step('Pi provider', () => actions.configurePi(port));
|
|
252
|
+
await step('Live request through grunt service', () => actions.checkService(port));
|
|
253
|
+
await step('Adapter verification through grunt', () => actions.checkFallbackServices(fallbacks.unavailable, port, line => log.info(line), adapters));
|
|
254
|
+
outro(fallbacks.unavailable.length ? `grunt is ready; incomplete fallbacks: ${fallbacks.unavailable.join(', ')}. Rerun setup to finish them.` : 'grunt is ready');
|
|
255
|
+
log.info('Run pi, open /model, and choose grunt.');
|
|
256
|
+
if (fallbacks.unavailable.length)
|
|
257
|
+
throw new Error('Some adapters failed verification. Run usegrunt setup to resume.');
|
|
258
|
+
}
|
|
@@ -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(); });
|
package/dist/store.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createClient } from "@libsql/client";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
import { mkdirSync } from "node:fs";
|
|
4
|
+
import { dirname } from "node:path";
|
|
5
|
+
export class AgentStore {
|
|
6
|
+
db;
|
|
7
|
+
constructor(db) {
|
|
8
|
+
this.db = db;
|
|
9
|
+
}
|
|
10
|
+
static async open(url) {
|
|
11
|
+
if (url.startsWith("file:") && url !== "file::memory:")
|
|
12
|
+
mkdirSync(dirname(url.slice(5)), { recursive: true });
|
|
13
|
+
const db = createClient({ url: url === ":memory:" ? "file::memory:" : url });
|
|
14
|
+
await db.executeMultiple(`
|
|
15
|
+
PRAGMA journal_mode=WAL;
|
|
16
|
+
CREATE TABLE IF NOT EXISTS messages (id TEXT PRIMARY KEY, person TEXT NOT NULL, role TEXT NOT NULL, text TEXT NOT NULL, ts INTEGER NOT NULL);
|
|
17
|
+
CREATE INDEX IF NOT EXISTS messages_person ON messages(person, ts);
|
|
18
|
+
CREATE TABLE IF NOT EXISTS tasks (id TEXT PRIMARY KEY, person TEXT NOT NULL, title TEXT NOT NULL, brief TEXT NOT NULL, kind TEXT NOT NULL, status TEXT NOT NULL, model TEXT, route_reason TEXT, result TEXT, error TEXT, created_at INTEGER NOT NULL, updated_at INTEGER NOT NULL);
|
|
19
|
+
CREATE INDEX IF NOT EXISTS tasks_person ON tasks(person, created_at);
|
|
20
|
+
CREATE TABLE IF NOT EXISTS notes (person TEXT NOT NULL, text TEXT NOT NULL, ts INTEGER NOT NULL, UNIQUE(person, text));
|
|
21
|
+
CREATE TABLE IF NOT EXISTS calls (model TEXT NOT NULL, kind TEXT NOT NULL, tokens_in INTEGER NOT NULL, tokens_out INTEGER NOT NULL, latency_ms INTEGER NOT NULL, outcome TEXT NOT NULL, cost REAL NOT NULL, ts INTEGER NOT NULL);
|
|
22
|
+
UPDATE tasks SET status='failed', error='Gateway restarted before the task finished.', updated_at=${Date.now()} WHERE status IN ('queued','running');
|
|
23
|
+
`);
|
|
24
|
+
return new AgentStore(db);
|
|
25
|
+
}
|
|
26
|
+
async rows(sql, args = []) {
|
|
27
|
+
return (await this.db.execute({ sql, args })).rows;
|
|
28
|
+
}
|
|
29
|
+
async messages(person) { return (await this.rows("SELECT * FROM messages WHERE person=? ORDER BY rowid DESC LIMIT 20", [person])).reverse(); }
|
|
30
|
+
async tasks(person) { return this.rows("SELECT * FROM tasks WHERE person=? AND status IN ('queued','running') UNION ALL SELECT * FROM (SELECT * FROM tasks WHERE person=? AND status NOT IN ('queued','running') ORDER BY created_at DESC LIMIT 30)", [person, person]); }
|
|
31
|
+
async task(person, id) { return (await this.rows("SELECT * FROM tasks WHERE person=? AND id=?", [person, id]))[0]; }
|
|
32
|
+
async notes(person) { return this.rows("SELECT text,ts FROM notes WHERE person=? ORDER BY rowid DESC LIMIT 64", [person]); }
|
|
33
|
+
async calls() { return this.rows("SELECT * FROM calls ORDER BY rowid DESC LIMIT 500"); }
|
|
34
|
+
async message(person, role, text) {
|
|
35
|
+
const m = { id: randomUUID(), person, role, text, ts: Date.now() };
|
|
36
|
+
await this.db.execute({ sql: "INSERT INTO messages VALUES (?,?,?,?,?)", args: [m.id, person, role, text, m.ts] });
|
|
37
|
+
return m;
|
|
38
|
+
}
|
|
39
|
+
async remember(person, text) { await this.db.execute({ sql: "INSERT OR IGNORE INTO notes VALUES (?,?,?)", args: [person, text, Date.now()] }); }
|
|
40
|
+
async dispatch(person, title, brief, kind) {
|
|
41
|
+
const id = randomUUID(), now = Date.now();
|
|
42
|
+
await this.db.execute({ sql: "INSERT INTO tasks (id,person,title,brief,kind,status,created_at,updated_at) VALUES (?,?,?,?,?,'queued',?,?)", args: [id, person, title, brief, kind, now, now] });
|
|
43
|
+
return (await this.task(person, id));
|
|
44
|
+
}
|
|
45
|
+
async update(person, id, patch, from = ["queued", "running"]) {
|
|
46
|
+
const entries = Object.entries({ ...patch, updated_at: Date.now() });
|
|
47
|
+
await this.db.execute({ sql: `UPDATE tasks SET ${entries.map(([k]) => `${k}=?`).join(",")} WHERE person=? AND id=? AND status IN (${from.map(() => "?").join(",")})`, args: [...entries.map(([, v]) => v), person, id, ...from] });
|
|
48
|
+
return this.task(person, id);
|
|
49
|
+
}
|
|
50
|
+
async record(c) { await this.db.execute({ sql: "INSERT INTO calls VALUES (?,?,?,?,?,?,?,?)", args: [c.model, c.kind, c.tokens_in, c.tokens_out, c.latency_ms, c.outcome, c.cost, c.ts] }); }
|
|
51
|
+
async state(person) {
|
|
52
|
+
const [messages, tasks, notes] = await Promise.all([this.messages(person), this.tasks(person), this.notes(person)]);
|
|
53
|
+
return { messages, tasks, notes };
|
|
54
|
+
}
|
|
55
|
+
close() { this.db.close(); }
|
|
56
|
+
}
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "usegrunt",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=22"
|
|
7
|
+
},
|
|
8
|
+
"packageManager": "pnpm@10.34.5",
|
|
9
|
+
"pnpm": {
|
|
10
|
+
"onlyBuiltDependencies": [
|
|
11
|
+
"better-sqlite3@12.11.1"
|
|
12
|
+
]
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"dev": "tsx src/server.ts",
|
|
16
|
+
"build": "tsc && cp src/chat.html dist/chat.html && mkdir -p dist/registry && cp registry/adapters.json dist/registry/adapters.json",
|
|
17
|
+
"start": "node dist/server.js",
|
|
18
|
+
"test": "vitest run",
|
|
19
|
+
"lint": "eslint src && tsc --noEmit",
|
|
20
|
+
"local": "tsx src/local/main.ts"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@clack/prompts": "^1.9.0",
|
|
24
|
+
"@hono/node-server": "^1.19.17",
|
|
25
|
+
"@libsql/client": "^0.18.0",
|
|
26
|
+
"@modelcontextprotocol/sdk": "^1.32.2",
|
|
27
|
+
"@trusty-squire/mcp": "1.1.39",
|
|
28
|
+
"@trusty-squire/sdk": "0.1.0",
|
|
29
|
+
"chalk": "^6.0.1",
|
|
30
|
+
"hono": "^4.13.8",
|
|
31
|
+
"ipaddr.js": "^2.5.0",
|
|
32
|
+
"standardwebhooks": "^1.1.1"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^22.20.3",
|
|
36
|
+
"eslint": "^9.39.5",
|
|
37
|
+
"tsx": "^4.19.2",
|
|
38
|
+
"typescript": "^5.9.3",
|
|
39
|
+
"typescript-eslint": "^8.70.0",
|
|
40
|
+
"vitest": "^3.2.7"
|
|
41
|
+
},
|
|
42
|
+
"bin": {
|
|
43
|
+
"usegrunt": "dist/local/main.js",
|
|
44
|
+
"milo": "dist/local/main.js",
|
|
45
|
+
"milo-local": "dist/local/main.js"
|
|
46
|
+
},
|
|
47
|
+
"description": "One-command setup for grunt",
|
|
48
|
+
"files": [
|
|
49
|
+
"dist",
|
|
50
|
+
"README.md"
|
|
51
|
+
]
|
|
52
|
+
}
|