usegrunt 0.6.0 → 0.6.2
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 -511
- package/dist/local/fallback-setup.js +109 -86
- package/dist/local/fixtures/opencode-events.js +4 -0
- package/dist/local/freellmapi.js +6 -0
- package/dist/local/harnesses.js +1 -1
- package/dist/local/main.js +8 -6
- package/dist/local/native-router.js +9 -6
- package/dist/local/opencode.js +100 -8
- package/dist/local/parallel.js +20 -0
- package/dist/local/registry.js +35 -13
- package/dist/local/routing.js +5 -10
- package/dist/local/setup.js +100 -35
- package/dist/local/terminal.js +8 -0
- package/dist/local/verification-queue.js +31 -0
- package/dist/local/wizard.js +102 -74
- package/dist/registry/adapters.json +35 -5
- package/dist/registry.js +4 -3
- package/package.json +1 -1
package/dist/local/routing.js
CHANGED
|
@@ -24,7 +24,7 @@ export class StickyRouter {
|
|
|
24
24
|
return (await exec('git', ['-C', cwd, ...args], { timeout: 10000, maxBuffer: 1024 * 1024 })).stdout.trim();
|
|
25
25
|
}
|
|
26
26
|
async createTask(request) {
|
|
27
|
-
const task = {
|
|
27
|
+
const task = { original: userTurns(request).at(-1) ?? '', output: '', switched: false, busy: false };
|
|
28
28
|
try {
|
|
29
29
|
task.checkpoint = await this.git(request.cwd, ['stash', 'create']) || await this.git(request.cwd, ['rev-parse', 'HEAD']);
|
|
30
30
|
}
|
|
@@ -58,7 +58,7 @@ export class StickyRouter {
|
|
|
58
58
|
let task = this.tasks.get(key);
|
|
59
59
|
if (!task) {
|
|
60
60
|
// Reserve the task before asynchronous checkpoint creation to avoid two bindings.
|
|
61
|
-
task = {
|
|
61
|
+
task = { original: '', output: '', switched: false, busy: true };
|
|
62
62
|
this.tasks.set(key, task);
|
|
63
63
|
Object.assign(task, await this.createTask({ ...request, cwd }), { busy: true });
|
|
64
64
|
}
|
|
@@ -91,12 +91,10 @@ export class StickyRouter {
|
|
|
91
91
|
if (task.source)
|
|
92
92
|
task.switched = true;
|
|
93
93
|
task.source = selected.source.id;
|
|
94
|
-
task.failures = 0;
|
|
95
94
|
}
|
|
96
95
|
try {
|
|
97
96
|
const input = task.switched ? await this.handoff({ ...request, cwd }, task) : { ...request, cwd };
|
|
98
97
|
const result = await selected.source.complete(input, signal);
|
|
99
|
-
task.failures = 0;
|
|
100
98
|
task.output = Buffer.from(result.text).subarray(-4096).toString('utf8');
|
|
101
99
|
task.switched = false;
|
|
102
100
|
answeredBy.set(result, selected.source.id);
|
|
@@ -107,19 +105,16 @@ export class StickyRouter {
|
|
|
107
105
|
const failure = error instanceof SourceError ? error : new SourceError('upstream', 'Adapter request failed');
|
|
108
106
|
if (failure.output)
|
|
109
107
|
task.output = failure.output;
|
|
110
|
-
if (failure.code === 'cancelled'
|
|
108
|
+
if (failure.code === 'cancelled')
|
|
111
109
|
throw failure;
|
|
112
110
|
let until;
|
|
113
111
|
if (failure.code === 'quota')
|
|
114
112
|
until = this.now() + (failure.retryAfter ?? 3600) * 1000;
|
|
115
113
|
else if (failure.code === 'logged_out' || failure.code === 'not_installed')
|
|
116
114
|
until = Infinity;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
if (task.failures < 3)
|
|
120
|
-
throw failure;
|
|
115
|
+
// Any other failure moves this task to the next source now, so the request still gets an answer.
|
|
116
|
+
else
|
|
121
117
|
until = this.now() + 15 * 60 * 1000;
|
|
122
|
-
}
|
|
123
118
|
this.bars.set(selected.source.id, until);
|
|
124
119
|
task.switched = true;
|
|
125
120
|
}
|
package/dist/local/setup.js
CHANGED
|
@@ -13,9 +13,14 @@ import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
|
|
|
13
13
|
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
|
|
14
14
|
import { freeConfig, loopback } from './freellmapi.js';
|
|
15
15
|
import { proxyApproval } from './approval-proxy.js';
|
|
16
|
+
import { VerificationQueue } from './verification-queue.js';
|
|
17
|
+
import { withTerminal } from './terminal.js';
|
|
16
18
|
const exec = promisify(execFile);
|
|
17
19
|
export const FREELLMAPI_IMAGE = 'ghcr.io/tashfeenahmed/freellmapi@sha256:845a4b568a20fffd5112e1b9c0258e7782d9eabbba2430612d2f7c811eee267e';
|
|
18
|
-
export const PROVIDERS = { groq: 'https://console.groq.com/keys', cerebras: 'https://cloud.cerebras.ai/platform/api-keys' };
|
|
20
|
+
export const PROVIDERS = { groq: 'https://console.groq.com/keys', cerebras: 'https://cloud.cerebras.ai/platform/api-keys', kilo: 'https://app.kilo.ai/profile' };
|
|
21
|
+
export const KILO_NEMOTRON = 'nvidia/nemotron-3-ultra-550b-a55b:free';
|
|
22
|
+
const savedProviders = ['kilo'];
|
|
23
|
+
const providerModels = { kilo: [KILO_NEMOTRON, 'kilo-auto/free'] };
|
|
19
24
|
const sameService = (credential, service) => credential.service?.trim().toLowerCase() === service.trim().toLowerCase();
|
|
20
25
|
function apiKeyField(credential, provider) {
|
|
21
26
|
return credential.field_names.find(field => {
|
|
@@ -104,6 +109,22 @@ export async function setupProviders(squire, router, options) {
|
|
|
104
109
|
await router.addGrant(provider, grant);
|
|
105
110
|
options.notify(`${provider}: connected through Squire grant`);
|
|
106
111
|
}
|
|
112
|
+
// These sources reuse existing vault access only; missing keys never start signup.
|
|
113
|
+
for (const provider of savedProviders) {
|
|
114
|
+
if (options.providers?.includes(provider))
|
|
115
|
+
continue;
|
|
116
|
+
if (await router.hasKey?.(provider)) {
|
|
117
|
+
options.notify(`${provider}: connected`);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const credential = (await squire.credentials.list()).find(c => sameService(c, provider) && c.auth_strategy !== 'username_password' && apiKeyField(c, provider));
|
|
121
|
+
if (!credential) {
|
|
122
|
+
options.notify(`${provider}: skipped; no saved API access`);
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
await router.addGrant(provider, await squire.credentials.grant(credential.reference));
|
|
126
|
+
options.notify(`${provider}: connected through Squire grant`);
|
|
127
|
+
}
|
|
107
128
|
return { pending, dropped };
|
|
108
129
|
}
|
|
109
130
|
const object = (value) => value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
@@ -123,7 +144,23 @@ export class BrowserSquire {
|
|
|
123
144
|
client = new Client({ name: 'grunt setup', version: '0.1.0' });
|
|
124
145
|
sessions = new Map();
|
|
125
146
|
handoffs = [];
|
|
126
|
-
|
|
147
|
+
connection;
|
|
148
|
+
metadata;
|
|
149
|
+
grants = new Map();
|
|
150
|
+
verification = new VerificationQueue(items => withTerminal(async () => {
|
|
151
|
+
this.options.signal?.throwIfAborted();
|
|
152
|
+
for (const item of items)
|
|
153
|
+
console.log(`Complete ${item.service} verification in this browser: ${item.url}`);
|
|
154
|
+
const { confirm, isCancel } = await import('@clack/prompts');
|
|
155
|
+
const done = await confirm({ message: `Finished verification for ${items.map(item => item.service).join(', ')}? Resume setup.`, initialValue: true });
|
|
156
|
+
if (isCancel(done) || !done)
|
|
157
|
+
throw new Error('Provider verification cancelled');
|
|
158
|
+
this.options.signal?.throwIfAborted();
|
|
159
|
+
}));
|
|
160
|
+
connect() {
|
|
161
|
+
return this.connection ??= this.connectOnce().catch(error => { this.connection = undefined; throw error; });
|
|
162
|
+
}
|
|
163
|
+
async connectOnce() {
|
|
127
164
|
const bin = join(dirname(fileURLToPath(import.meta.resolve('@trusty-squire/mcp'))), 'bin.js');
|
|
128
165
|
const env = squireEnvironment();
|
|
129
166
|
// Squire owns its account binding and vault. grunt never reads its files.
|
|
@@ -144,6 +181,7 @@ export class BrowserSquire {
|
|
|
144
181
|
const result = await this.call('list_credentials', { fields: 'summary' }, true);
|
|
145
182
|
if (!Array.isArray(result.credentials))
|
|
146
183
|
throw new Error('Squire did not return vault metadata');
|
|
184
|
+
this.metadata = Promise.resolve(result.credentials);
|
|
147
185
|
}
|
|
148
186
|
catch (error) {
|
|
149
187
|
await this.client.close().catch(() => { });
|
|
@@ -187,21 +225,30 @@ export class BrowserSquire {
|
|
|
187
225
|
}
|
|
188
226
|
credentials = {
|
|
189
227
|
list: async () => {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
228
|
+
return structuredClone(await (this.metadata ??= this.call('list_credentials', { fields: 'summary' }).then(result => {
|
|
229
|
+
if (!Array.isArray(result.credentials))
|
|
230
|
+
throw new Error('Squire did not return vault metadata');
|
|
231
|
+
return result.credentials;
|
|
232
|
+
}).catch(error => { this.metadata = undefined; throw error; })));
|
|
194
233
|
},
|
|
195
234
|
grant: async (reference) => {
|
|
196
|
-
|
|
197
|
-
if (
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
return { base_url: result.base_url, token: result.token };
|
|
235
|
+
let grant = this.grants.get(reference);
|
|
236
|
+
if (!grant) {
|
|
237
|
+
grant = this.grant(reference).catch(error => { this.grants.delete(reference); throw error; });
|
|
238
|
+
this.grants.set(reference, grant);
|
|
239
|
+
}
|
|
240
|
+
return grant;
|
|
203
241
|
}
|
|
204
242
|
};
|
|
243
|
+
async grant(reference) {
|
|
244
|
+
const result = await this.call('grant_app_access', { reference });
|
|
245
|
+
if (typeof result.base_url !== 'string' || typeof result.token !== 'string' || !result.token)
|
|
246
|
+
throw new Error('Squire returned no app grant');
|
|
247
|
+
const url = new URL(result.base_url);
|
|
248
|
+
if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash)
|
|
249
|
+
throw new Error('Squire returned an invalid grant endpoint');
|
|
250
|
+
return { base_url: result.base_url, token: result.token };
|
|
251
|
+
}
|
|
205
252
|
async signup(service, url, goal) {
|
|
206
253
|
const deadline = Date.now() + 600000;
|
|
207
254
|
while (Date.now() < deadline) {
|
|
@@ -218,7 +265,7 @@ export class BrowserSquire {
|
|
|
218
265
|
const session = this.sessions.get(provider);
|
|
219
266
|
const result = await this.call('operate_drive', {
|
|
220
267
|
...(session ? { session_id: session } : { url }),
|
|
221
|
-
goal: goal ?? `Use the user's existing ${provider} account or sign up for the free plan. Create one API key for grunt and store it in this user's Squire vault with service ${provider}. Use operate_extract to capture the key directly into the vault; never put its value in your answer. No paid plan, payment method, extra account or account switching. If captcha, passkey or email confirmation needs a human, stop and report the required step so grunt can share this browser. Do not bypass verification. Stop on provider refusal.`,
|
|
268
|
+
goal: goal ?? `Use the user's existing ${provider} account or sign up for the free plan. Create one API key for grunt and store it in this user's Squire vault with service ${provider}. Use operate_extract to capture the key directly into the vault; never put its value in your answer.${provider === 'kilo' ? ' Reuse the existing profile API key without resetting it. Capture with api_hosts ["api.kilo.ai", "ai-gateway.kilo.ai"] in that order and auth_shape "bearer"; grunt uses /api/gateway beneath the grant.' : ''} No paid plan, payment method, extra account or account switching. If captcha, passkey or email confirmation needs a human, stop and report the required step so grunt can share this browser. Do not bypass verification. Stop on provider refusal.`,
|
|
222
269
|
facts: { plan: 'free', project_name: 'grunt' }, max_seconds: 45, max_steps: 60
|
|
223
270
|
});
|
|
224
271
|
if (typeof result.session_id === 'string')
|
|
@@ -229,6 +276,7 @@ export class BrowserSquire {
|
|
|
229
276
|
if (refusal)
|
|
230
277
|
throw setupStopped(`Provider refused ${provider} signup; setup stopped`, reason);
|
|
231
278
|
if (result.status === 'complete') {
|
|
279
|
+
this.metadata = undefined; // A browser capture may have added a credential.
|
|
232
280
|
const id = this.sessions.get(provider);
|
|
233
281
|
if (id) {
|
|
234
282
|
await this.call('operate_finish', { session_id: id, outcome: 'result', summary: 'grunt provider key stored in user vault' });
|
|
@@ -243,15 +291,12 @@ export class BrowserSquire {
|
|
|
243
291
|
throw new Error('Provider signup needs human verification; stopped before handoff');
|
|
244
292
|
if (humanStep && this.sessions.has(provider)) {
|
|
245
293
|
const url = await this.humanHandoff(this.sessions.get(provider));
|
|
246
|
-
console.log(`Complete ${provider} verification in this browser: ${url}`);
|
|
247
294
|
if (this.options.background) {
|
|
295
|
+
console.log(`Complete ${provider} verification in this browser: ${url}`);
|
|
248
296
|
await delay(5000, undefined, this.options.signal ? { signal: this.options.signal } : {});
|
|
249
297
|
return { run: { id: provider, state: 'running' }, decision: 'silent' };
|
|
250
298
|
}
|
|
251
|
-
|
|
252
|
-
const done = await confirm({ message: 'Verification complete? Resume signup.', initialValue: true });
|
|
253
|
-
if (isCancel(done) || !done)
|
|
254
|
-
throw new Error('Provider verification cancelled');
|
|
299
|
+
await this.verification.wait({ service: provider, url });
|
|
255
300
|
return { run: { id: provider, state: 'running' }, decision: 'silent' };
|
|
256
301
|
}
|
|
257
302
|
this.sessions.delete(provider);
|
|
@@ -272,7 +317,7 @@ export class BrowserSquire {
|
|
|
272
317
|
async close() { for (const handoff of this.handoffs)
|
|
273
318
|
handoff.close(); await this.client.close(); }
|
|
274
319
|
}
|
|
275
|
-
const providerPath = (provider) =>
|
|
320
|
+
const providerPath = (provider) => ({ groq: '/openai/v1', kilo: '/api/gateway' })[provider] ?? '/v1';
|
|
276
321
|
export class FreeLLMAdmin {
|
|
277
322
|
token = '';
|
|
278
323
|
url;
|
|
@@ -290,7 +335,13 @@ export class FreeLLMAdmin {
|
|
|
290
335
|
if (!key || !key.models?.length)
|
|
291
336
|
return false;
|
|
292
337
|
const discovery = await this.call('/keys/custom/discover-models', { keyId: key.id });
|
|
293
|
-
|
|
338
|
+
const available = new Set(discovery.models.map(model => model.id));
|
|
339
|
+
const wanted = providerModels[platform]?.filter(id => available.has(id));
|
|
340
|
+
if (wanted) {
|
|
341
|
+
const required = platform === 'kilo' ? wanted : wanted.slice(0, 1);
|
|
342
|
+
return required.length > 0 && required.every(id => key.models.some(model => model.modelId === id));
|
|
343
|
+
}
|
|
344
|
+
return key.models.some(model => available.has(model.modelId));
|
|
294
345
|
}
|
|
295
346
|
async addGrant(platform, grant) {
|
|
296
347
|
// Custom endpoints bind the grant URL and token to their own model rows.
|
|
@@ -298,12 +349,13 @@ export class FreeLLMAdmin {
|
|
|
298
349
|
const label = `Milo ${platform} (Squire)`;
|
|
299
350
|
const discovery = await this.call('/keys/custom/discover-models', { baseUrl, apiKey: grant.token });
|
|
300
351
|
const available = new Set(discovery.models.map(model => model.id));
|
|
301
|
-
const
|
|
302
|
-
|
|
352
|
+
const candidates = providerModels[platform] ?? ['openai/gpt-oss-120b', 'gpt-oss-120b', 'llama-3.3-70b-versatile', 'openai/gpt-oss-20b'];
|
|
353
|
+
const models = platform === 'kilo' ? candidates.filter(id => available.has(id)) : candidates.filter(id => available.has(id)).slice(0, 1);
|
|
354
|
+
if (!models.length)
|
|
303
355
|
throw new Error(`No supported chat model is currently available from ${platform}`);
|
|
304
356
|
const previous = await this.call('/keys', undefined, 'GET');
|
|
305
357
|
await this.call('/keys/custom', { baseUrl, apiKey: grant.token,
|
|
306
|
-
label, models:
|
|
358
|
+
label, models: models.map(model => ({ model, supportsTools: true })) });
|
|
307
359
|
for (const key of previous) {
|
|
308
360
|
if (Number.isInteger(key.id) && key.platform === 'custom' && key.label === label && key.baseUrl !== baseUrl)
|
|
309
361
|
await this.call(`/keys/${key.id}`, undefined, 'DELETE');
|
|
@@ -320,11 +372,12 @@ export class FreeLLMAdmin {
|
|
|
320
372
|
async configureChain(excluded = []) {
|
|
321
373
|
const chain = await this.call('/fallback', undefined, 'GET');
|
|
322
374
|
const rows = chain.map(row => {
|
|
323
|
-
const provider = Object.keys(PROVIDERS).find(provider => row.keyLabel === `Milo ${provider} (Squire)`);
|
|
324
|
-
const fallback = row.modelId === 'freebuff' || row.modelId === 'kilo-auto/free';
|
|
375
|
+
const provider = [...Object.keys(PROVIDERS), ...savedProviders].find(provider => row.keyLabel === `Milo ${provider} (Squire)`);
|
|
376
|
+
const fallback = !provider && (row.modelId === 'freebuff' || row.modelId === 'kilo-auto/free');
|
|
377
|
+
const vetted = !provider || !providerModels[provider] || providerModels[provider].includes(row.modelId);
|
|
325
378
|
return { modelDbId: row.modelDbId,
|
|
326
|
-
enabled: fallback || (provider ? row.platform === 'custom' && !excluded.includes(provider) : row.enabled === true),
|
|
327
|
-
priority: row.modelId === 'freebuff' ? 1000000 : row.modelId === 'kilo-auto/free' ? 999999 : row.priority };
|
|
379
|
+
enabled: fallback || (provider ? row.platform === 'custom' && vetted && !excluded.includes(provider) : row.enabled === true),
|
|
380
|
+
priority: provider === 'kilo' ? (row.modelId === KILO_NEMOTRON ? 999996 : 999997) : row.modelId === 'freebuff' ? 1000000 : row.modelId === 'kilo-auto/free' ? 999999 : row.priority };
|
|
328
381
|
});
|
|
329
382
|
await this.call('/fallback', rows, 'PUT');
|
|
330
383
|
await this.call('/fallback/routing', { strategy: 'priority' }, 'PUT');
|
|
@@ -392,7 +445,7 @@ export async function ensureRouter(password, encryption, run = argsWithEnv, recr
|
|
|
392
445
|
await run(['volume', 'rm', 'milo-freellmapi-data']);
|
|
393
446
|
}
|
|
394
447
|
// Docker copies named variables from env, keeping secrets out of argv.
|
|
395
|
-
await run(['run', '-d', '--name', 'milo-freellmapi', '--label', 'io.milo.router=1', ...(process.platform === 'linux' ? ['--network', 'host'] : ['-p', '127.0.0.1:3001:3001']), '--restart', 'unless-stopped', '-v', 'milo-freellmapi-data:/app/server/data', '-e', process.platform === 'linux' ? 'HOST=127.0.0.1' : 'HOST=0.0.0.0', '-e', 'PORT=3001', '-e', 'ENCRYPTION_KEY', '-e', 'FREEAPI_CONFIG_JSON', FREELLMAPI_IMAGE], {
|
|
448
|
+
await run(['run', '-d', '--name', 'milo-freellmapi', '--label', 'io.milo.router=1', ...(process.platform === 'linux' ? ['--network', 'host'] : ['-p', '127.0.0.1:3001:3001']), '--restart', 'unless-stopped', '-v', 'milo-freellmapi-data:/app/server/data', '-e', process.platform === 'linux' ? 'HOST=127.0.0.1' : 'HOST=0.0.0.0', '-e', 'PORT=3001', '-e', 'PROVIDER_TIMEOUT_CUSTOM=600000', '-e', 'ENCRYPTION_KEY', '-e', 'FREEAPI_CONFIG_JSON', FREELLMAPI_IMAGE], {
|
|
396
449
|
...process.env, ENCRYPTION_KEY: encryption, FREEAPI_CONFIG_JSON: JSON.stringify({ admin: { email: 'milo@localhost.local', password }, ...freeConfig() })
|
|
397
450
|
});
|
|
398
451
|
}
|
|
@@ -455,11 +508,13 @@ export async function loadRouterKey(url) {
|
|
|
455
508
|
throw new Error('Local router returned an invalid API key');
|
|
456
509
|
return key;
|
|
457
510
|
}
|
|
458
|
-
export async function setup(notify = console.log, providers = ['groq']) {
|
|
459
|
-
const
|
|
511
|
+
export async function setup(notify = console.log, providers = ['groq'], options = {}) {
|
|
512
|
+
const owned = options.squire ? undefined : new BrowserSquire(options.signal ? { signal: options.signal } : {});
|
|
513
|
+
const squire = options.squire ?? owned;
|
|
460
514
|
const router = new FreeLLMAdmin();
|
|
461
515
|
try {
|
|
462
|
-
await
|
|
516
|
+
await owned?.connect();
|
|
517
|
+
options.signal?.throwIfAborted();
|
|
463
518
|
const saved = await readLocalAdmin();
|
|
464
519
|
if (!saved && process.env.MILO_ROUTER_RUNTIME !== 'docker') {
|
|
465
520
|
const database = join(dirname(localAdminPath()), 'freellmapi', 'router.db');
|
|
@@ -485,17 +540,27 @@ export async function setup(notify = console.log, providers = ['groq']) {
|
|
|
485
540
|
if (!saved)
|
|
486
541
|
await saveLocalAdmin(admin);
|
|
487
542
|
const { ensureNativeRouter } = await import('./native-router.js');
|
|
488
|
-
|
|
543
|
+
let running = false;
|
|
544
|
+
if (saved) {
|
|
545
|
+
try {
|
|
546
|
+
await router.login(admin.password);
|
|
547
|
+
running = true;
|
|
548
|
+
}
|
|
549
|
+
catch { /* Repair an unavailable backend. */ }
|
|
550
|
+
}
|
|
551
|
+
if (!running)
|
|
552
|
+
await ensureNativeRouter(options.signal);
|
|
489
553
|
}
|
|
490
554
|
let ready = false;
|
|
491
555
|
for (let attempt = 0; attempt < 60; attempt++) {
|
|
556
|
+
options.signal?.throwIfAborted();
|
|
492
557
|
try {
|
|
493
558
|
await router.login(admin.password);
|
|
494
559
|
ready = true;
|
|
495
560
|
break;
|
|
496
561
|
}
|
|
497
562
|
catch {
|
|
498
|
-
await delay(1000);
|
|
563
|
+
await delay(1000, undefined, options.signal ? { signal: options.signal } : {});
|
|
499
564
|
}
|
|
500
565
|
}
|
|
501
566
|
if (!ready)
|
|
@@ -518,6 +583,6 @@ export async function setup(notify = console.log, providers = ['groq']) {
|
|
|
518
583
|
throw new Error('FreeLLMAPI installation failed; check Node and npm availability and run npx usegrunt setup again');
|
|
519
584
|
}
|
|
520
585
|
finally {
|
|
521
|
-
await
|
|
586
|
+
await owned?.close();
|
|
522
587
|
}
|
|
523
588
|
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// Interactive child processes (for example sudo) and browser confirmations
|
|
2
|
+
// must never read stdin at the same time. Noninteractive jobs stay concurrent.
|
|
3
|
+
let tail = Promise.resolve();
|
|
4
|
+
export function withTerminal(task) {
|
|
5
|
+
const result = tail.then(task);
|
|
6
|
+
tail = result.catch(() => { });
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// Group handoffs arriving together; only one terminal prompt may own stdin.
|
|
2
|
+
// Other installation/browser jobs keep running while the user verifies.
|
|
3
|
+
export class VerificationQueue {
|
|
4
|
+
confirm;
|
|
5
|
+
pending = [];
|
|
6
|
+
running = false;
|
|
7
|
+
constructor(confirm) {
|
|
8
|
+
this.confirm = confirm;
|
|
9
|
+
}
|
|
10
|
+
wait(item) {
|
|
11
|
+
const result = new Promise((resolve, reject) => this.pending.push({ item, resolve, reject }));
|
|
12
|
+
if (!this.running) {
|
|
13
|
+
this.running = true;
|
|
14
|
+
setImmediate(() => { void this.drain(); });
|
|
15
|
+
}
|
|
16
|
+
return result;
|
|
17
|
+
}
|
|
18
|
+
async drain() {
|
|
19
|
+
while (this.pending.length) {
|
|
20
|
+
const batch = this.pending.splice(0);
|
|
21
|
+
try {
|
|
22
|
+
await this.confirm(batch.map(entry => entry.item));
|
|
23
|
+
batch.forEach(entry => entry.resolve());
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
[...batch, ...this.pending.splice(0)].forEach(entry => entry.reject(error));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
this.running = false;
|
|
30
|
+
}
|
|
31
|
+
}
|
package/dist/local/wizard.js
CHANGED
|
@@ -8,6 +8,8 @@ import { setTimeout as delay } from 'node:timers/promises';
|
|
|
8
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 { parallelMap } from './parallel.js';
|
|
12
|
+
import { withTerminal } from './terminal.js';
|
|
11
13
|
import { DOCTOR_TIMEOUT_MS } from './freebuff.js';
|
|
12
14
|
import { BrowserSquire, PROVIDERS, setup, squireEnvironment } from './setup.js';
|
|
13
15
|
import { RegistrySync } from './registry.js';
|
|
@@ -16,11 +18,12 @@ import { banner } from './banner.js';
|
|
|
16
18
|
import { HARNESSES, configurePi, connectHarness, detectHarnesses } from './harnesses.js';
|
|
17
19
|
export const configDir = (env = process.env) => env.MILO_CONFIG_DIR || join(env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'milo');
|
|
18
20
|
export async function command(binary, args, interactive = false, signal) {
|
|
19
|
-
|
|
21
|
+
const run = () => new Promise((resolve, reject) => {
|
|
20
22
|
const child = spawn(binary, args, { stdio: interactive ? 'inherit' : 'ignore', ...(signal ? { signal } : {}) });
|
|
21
23
|
child.once('error', () => reject(new Error(`Could not run ${binary}`)));
|
|
22
24
|
child.once('exit', code => code === 0 ? resolve() : reject(new Error(`${binary} failed; inspect its service status before retrying`)));
|
|
23
25
|
});
|
|
26
|
+
await (interactive ? withTerminal(run) : run());
|
|
24
27
|
}
|
|
25
28
|
async function step(label, task, done = () => label) {
|
|
26
29
|
const progress = spinner();
|
|
@@ -67,11 +70,12 @@ export async function squireBrokerExists() {
|
|
|
67
70
|
return result.stdout === 'present';
|
|
68
71
|
}
|
|
69
72
|
export async function connectSquire(options = {}) {
|
|
70
|
-
const probe = async () => { const squire = new BrowserSquire(); try {
|
|
73
|
+
const probe = async () => { const squire = options.squire ?? new BrowserSquire(); try {
|
|
71
74
|
await squire.connect();
|
|
72
75
|
}
|
|
73
76
|
finally {
|
|
74
|
-
|
|
77
|
+
if (!options.squire)
|
|
78
|
+
await squire.close();
|
|
75
79
|
} };
|
|
76
80
|
try {
|
|
77
81
|
await probe();
|
|
@@ -127,7 +131,28 @@ export async function preflight(run = command) {
|
|
|
127
131
|
}
|
|
128
132
|
const systemdQuote = (value) => '"' + value.replaceAll('\\', '\\\\').replaceAll('"', '\\"').replaceAll('%', '%%').replaceAll('$', '$$').replaceAll('\n', '\\n') + '"';
|
|
129
133
|
const xml = (value) => value.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"');
|
|
130
|
-
export async function
|
|
134
|
+
export async function installedSources(directory = configDir()) {
|
|
135
|
+
try {
|
|
136
|
+
const sources = JSON.parse(await readFile(join(directory, 'installed-sources.json'), 'utf8'));
|
|
137
|
+
if (!Array.isArray(sources) || !sources.length || sources.some(id => typeof id !== 'string' || ![...knownAdapters, 'freellmapi'].includes(id)))
|
|
138
|
+
throw new Error('Invalid saved grunt sources');
|
|
139
|
+
return [...new Set(sources)].join(',');
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
if (error.code !== 'ENOENT')
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
await access(join(directory, 'router-admin.json'));
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
if (error.code !== 'ENOENT')
|
|
150
|
+
throw error;
|
|
151
|
+
return 'freebuff,opencode,kilo';
|
|
152
|
+
}
|
|
153
|
+
return 'freellmapi,kilo,freebuff,opencode';
|
|
154
|
+
}
|
|
155
|
+
export async function installService(adapters) {
|
|
131
156
|
if (!['linux', 'darwin'].includes(process.platform))
|
|
132
157
|
throw new Error('grunt background service requires systemd or launchd');
|
|
133
158
|
const directory = configDir(), runtime = join(directory, 'runtime');
|
|
@@ -142,7 +167,9 @@ export async function installService() {
|
|
|
142
167
|
const port = process.env.MILO_PORT || '8788';
|
|
143
168
|
if (!/^\d+$/.test(port) || Number(port) < 1 || Number(port) > 65535)
|
|
144
169
|
throw new Error('Invalid grunt service port');
|
|
145
|
-
const env = { GRUNT_DAEMON: '1', MILO_CONFIG_DIR: directory, MILO_PORT: port, MILO_SOURCES: '
|
|
170
|
+
const env = { GRUNT_DAEMON: '1', MILO_CONFIG_DIR: directory, MILO_PORT: port, MILO_SOURCES: adapters?.join(',') ?? await installedSources(directory), PATH: process.env.PATH || '/usr/local/bin:/usr/bin:/bin' };
|
|
171
|
+
if (process.env.MILO_FREELLMAPI_URL)
|
|
172
|
+
env.MILO_FREELLMAPI_URL = process.env.MILO_FREELLMAPI_URL;
|
|
146
173
|
const opencodeConfig = join(directory, 'opencode-free.json');
|
|
147
174
|
try {
|
|
148
175
|
await readFile(opencodeConfig);
|
|
@@ -153,6 +180,7 @@ export async function installService() {
|
|
|
153
180
|
throw error;
|
|
154
181
|
}
|
|
155
182
|
await installBackgroundService('milo', entry, env);
|
|
183
|
+
await writeFile(join(directory, 'installed-sources.json'), JSON.stringify(env.MILO_SOURCES.split(',')), { mode: 0o600 });
|
|
156
184
|
}
|
|
157
185
|
export async function installBackgroundService(name, entry, env) {
|
|
158
186
|
if (!/^[a-z][a-z0-9-]*$/.test(name))
|
|
@@ -367,77 +395,77 @@ export async function connectHarnesses(port, options = {}) {
|
|
|
367
395
|
note(connected.map(h => `${chalk.bold(h.name)}\n ${h.launch(port)}`).join('\n'), 'Start coding with grunt');
|
|
368
396
|
return connected;
|
|
369
397
|
}
|
|
370
|
-
const wizardActions = { banner, preflight, connectSquire, setup, setupFallbacks, installService, configurePi, checkService, checkFallbackServices, loadRegistry, checkServiceSources, connectHarnesses };
|
|
398
|
+
const wizardActions = { installedSources, createSquire: () => new BrowserSquire(), banner, preflight, connectSquire, setup, setupFallbacks, installService, configurePi, checkService, checkFallbackServices, loadRegistry, checkServiceSources, connectHarnesses };
|
|
371
399
|
export async function wizard(add, actions = wizardActions) {
|
|
372
400
|
if (add && !Object.hasOwn(PROVIDERS, add))
|
|
373
401
|
throw new Error(`Choose a supported provider: ${Object.keys(PROVIDERS).join(', ')}`);
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
await step('
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
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);
|
|
402
|
+
const squire = actions.createSquire?.();
|
|
403
|
+
try {
|
|
404
|
+
await actions.banner?.();
|
|
405
|
+
intro(chalk.hex('#f4b41a').bold('grunt') + chalk.dim(' — npx usegrunt setup'));
|
|
406
|
+
note(PREAMBLE, 'What grunt does');
|
|
407
|
+
await step('Checking prerequisites', () => actions.preflight());
|
|
408
|
+
note(SQUIRE_NOTICE, 'Google sign-in for Trusty Squire');
|
|
409
|
+
await step('Connecting Trusty Squire', () => actions.connectSquire(squire ? { squire } : {}));
|
|
410
|
+
// Capture legacy API routing before default Kilo setup creates its admin file.
|
|
411
|
+
const previous = (await actions.installedSources?.())?.split(',') ?? [];
|
|
412
|
+
if (add) {
|
|
413
|
+
await step('FreeLLMAPI and provider grants', () => actions.setup(line => log.info(line), [add], squire ? { squire } : {}));
|
|
414
|
+
await step('Starting the grunt background service', () => actions.installService(add !== 'kilo' ? [...new Set(['freellmapi', ...previous, ...knownAdapters])] : undefined));
|
|
415
|
+
await step('Waiting for grunt to answer', () => actions.checkService(Number(process.env.MILO_PORT || '8788')));
|
|
416
|
+
outro(`${add} connected`);
|
|
417
|
+
return;
|
|
429
418
|
}
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
419
|
+
const pulled = actions.loadRegistry ? await step('Pulling the free service list from the registry', () => actions.loadRegistry()) : { registry: bundledRegistry, origin: 'bundled' };
|
|
420
|
+
const { registry } = pulled;
|
|
421
|
+
const published = registry.published.slice(0, 10);
|
|
422
|
+
if (pulled.origin === 'remote')
|
|
423
|
+
log.info(`Registry: ${registry.adapters.length} free services, published ${published}`);
|
|
424
|
+
else
|
|
425
|
+
log.warn(`Registry unreachable; using the ${pulled.origin === 'cache' ? 'list saved by an earlier setup' : 'list bundled with this version'} (published ${published})`);
|
|
426
|
+
const adapters = registry.adapters.filter(adapter => knownAdapters.has(adapter.id));
|
|
427
|
+
log.message(registry.adapters.map(adapter => knownAdapters.has(adapter.id)
|
|
428
|
+
? `${chalk.bold(adapter.id.padEnd(10))} ${chalk.dim(describe(adapter))}`
|
|
429
|
+
: `${chalk.dim(adapter.id.padEnd(10))} ${chalk.dim('skipped: needs a newer usegrunt')}`).join('\n'));
|
|
430
|
+
const ids = adapters.map(adapter => adapter.id);
|
|
431
|
+
const results = new Map(ids.map(id => [id, {}]));
|
|
432
|
+
const unavailable = [];
|
|
433
|
+
const notify = report();
|
|
434
|
+
log.step(`Installing and connecting ${ids.length} vetted free services (up to 3 at once)`);
|
|
435
|
+
log.info('Trusty Squire reuses existing accounts and grants. Only free access is requested. Provider verification may still need your browser; verification prompts are queued.');
|
|
436
|
+
const outcome = await actions.setupFallbacks({ directory: configDir(), run: command, adapters: ids, notify,
|
|
437
|
+
...(squire ? { squire, connect: () => squire.connect() } : {}) });
|
|
438
|
+
unavailable.push(...outcome.unavailable);
|
|
439
|
+
for (const id of ids)
|
|
440
|
+
results.get(id).signup = unavailable.includes(id) ? 'fail' : 'pass';
|
|
441
|
+
log.step('Connecting the services to the grunt daemon');
|
|
442
|
+
await step('Starting the grunt background service', () => actions.installService([...new Set([...previous.filter(id => id === 'freellmapi'), ...ids])]));
|
|
443
|
+
const port = Number(process.env.MILO_PORT || '8788');
|
|
444
|
+
await step(`Waiting for grunt to answer at http://127.0.0.1:${port}`, () => actions.checkService(port));
|
|
445
|
+
const beforeSources = new Set(unavailable);
|
|
446
|
+
if (actions.checkServiceSources)
|
|
447
|
+
await step('Checking which services the daemon runs', () => actions.checkServiceSources(unavailable, notify, ids));
|
|
448
|
+
for (const id of ids)
|
|
449
|
+
results.get(id).daemon = beforeSources.has(id) ? 'skip' : unavailable.includes(id) ? 'fail' : 'pass';
|
|
450
|
+
log.step('Testing each service with a real request through grunt');
|
|
451
|
+
await parallelMap(ids, async (id) => {
|
|
452
|
+
const before = unavailable.includes(id);
|
|
453
|
+
await actions.checkFallbackServices(unavailable, port, notify, [id]);
|
|
454
|
+
results.get(id).check = before ? 'skip' : unavailable.includes(id) ? 'fail' : 'pass';
|
|
455
|
+
});
|
|
456
|
+
note([`${'service'.padEnd(10)} sign-in daemon test`, ...ids.map(id => {
|
|
457
|
+
const r = results.get(id);
|
|
458
|
+
return `${id.padEnd(10)} ${mark(r.signup)} ${mark(r.daemon)} ${mark(r.check)}`;
|
|
459
|
+
})].join('\n'), 'Free services');
|
|
460
|
+
if (actions.connectHarnesses)
|
|
461
|
+
await actions.connectHarnesses(port);
|
|
462
|
+
else if (actions.configurePi)
|
|
463
|
+
await step('Pi provider', () => actions.configurePi(port));
|
|
464
|
+
outro(unavailable.length ? `grunt is ready; incomplete fallbacks: ${unavailable.join(', ')}. Rerun setup to finish them.` : 'grunt is ready');
|
|
465
|
+
if (unavailable.length)
|
|
466
|
+
throw new Error('Some adapters failed verification. Run usegrunt setup to resume.');
|
|
467
|
+
}
|
|
468
|
+
finally {
|
|
469
|
+
await squire?.close();
|
|
470
|
+
}
|
|
443
471
|
}
|