usegrunt 0.0.1 → 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 +464 -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 +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 +47 -7
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
import { randomBytes } from 'node:crypto';
|
|
2
|
+
import { execFile } from 'node:child_process';
|
|
3
|
+
import { promisify } from 'node:util';
|
|
4
|
+
import { Readable } from 'node:stream';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import { dirname, join } from 'node:path';
|
|
7
|
+
import { homedir } from 'node:os';
|
|
8
|
+
import { constants } from 'node:fs';
|
|
9
|
+
import { lstat, mkdir, open, rename, rm, writeFile } from 'node:fs/promises';
|
|
10
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
11
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js';
|
|
12
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
13
|
+
import { ErrorCode, McpError } from '@modelcontextprotocol/sdk/types.js';
|
|
14
|
+
import { freeConfig, loopback } from './freellmapi.js';
|
|
15
|
+
import { proxyApproval } from './approval-proxy.js';
|
|
16
|
+
const exec = promisify(execFile);
|
|
17
|
+
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' };
|
|
19
|
+
const sameService = (credential, service) => credential.service?.trim().toLowerCase() === service.trim().toLowerCase();
|
|
20
|
+
function apiKeyField(credential, provider) {
|
|
21
|
+
return credential.field_names.find(field => {
|
|
22
|
+
const normalized = field.toLowerCase().replace(/[^a-z0-9]/g, '');
|
|
23
|
+
const name = normalized.startsWith(provider) ? normalized.slice(provider.length) : normalized;
|
|
24
|
+
return ['apikey', 'key', 'token', 'value', 'secret', 'apitoken', 'accesstoken'].includes(name);
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
// Select only diagnostic text, never serialize the browser result or vault data.
|
|
28
|
+
function setupReason(...values) {
|
|
29
|
+
return [...new Set(values.filter((value) => typeof value === 'string')
|
|
30
|
+
// Free-form secret descriptions cannot be redacted reliably. Drop the whole
|
|
31
|
+
// field, including ambiguous prose, while retaining separate safe details.
|
|
32
|
+
.filter(value => !/(?:password|passcode|token|secret|key|credential|authorization)/i.test(value.normalize('NFKC'))).map(value => value
|
|
33
|
+
.replace(/\p{Cc}|\p{Cf}/gu, ' ')
|
|
34
|
+
.replace(/https?:\/\/\S+/gi, '[link omitted]')
|
|
35
|
+
.replace(/\b(?:Bearer|Basic)\s+\S+/gi, '[redacted authorization]')
|
|
36
|
+
.replace(/\b(?:gsk_|nvapi-|sk-|sk_|freellmapi-|ghp_)[A-Za-z0-9_-]+/g, '[redacted]')
|
|
37
|
+
.replace(/[A-Za-z0-9_+/=-]{24,}/g, '[redacted]')
|
|
38
|
+
.replace(/\s+/g, ' ').trim()).filter(Boolean))].join(' ').slice(0, 512);
|
|
39
|
+
}
|
|
40
|
+
function setupStopped(message, ...values) {
|
|
41
|
+
const reason = setupReason(...values);
|
|
42
|
+
return new Error(reason ? `${message}: ${reason}` : message);
|
|
43
|
+
}
|
|
44
|
+
// Connection diagnostics are untrusted child output. Redact before truncating.
|
|
45
|
+
function connectionReason(value) {
|
|
46
|
+
return value
|
|
47
|
+
.replace(/\bBearer\s+\S+/gi, '[redacted]')
|
|
48
|
+
.replace(/(?:sk-|gsk_|ghp_|eyJ)[A-Za-z0-9_+/.=-]*/g, '[redacted]')
|
|
49
|
+
.replace(/[A-Za-z0-9_+/-]{32,}={0,2}/g, '[redacted]')
|
|
50
|
+
.replace(/\p{Cc}|\p{Cf}/gu, ' ').replace(/\s+/g, ' ').trim();
|
|
51
|
+
}
|
|
52
|
+
export class SetupPending extends Error {
|
|
53
|
+
}
|
|
54
|
+
export async function setupProviders(squire, router, options) {
|
|
55
|
+
const pending = [];
|
|
56
|
+
const dropped = [];
|
|
57
|
+
const drop = (provider, reason) => {
|
|
58
|
+
const safe = setupReason(reason);
|
|
59
|
+
options.notify(`${provider}: dropped; Squire cannot complete signup automatically${safe ? `: ${safe}` : ''}`);
|
|
60
|
+
dropped.push(provider);
|
|
61
|
+
};
|
|
62
|
+
for (const provider of options.providers ?? ['groq']) {
|
|
63
|
+
if (await router.hasKey?.(provider)) {
|
|
64
|
+
options.notify(`${provider}: connected`);
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
const find = async () => (await squire.credentials.list()).find(c => sameService(c, provider) && c.auth_strategy !== 'username_password' && apiKeyField(c, provider));
|
|
68
|
+
let credential = await find();
|
|
69
|
+
if (!credential) {
|
|
70
|
+
const result = await squire.runs.start({ service: provider, plan: 'free', project_name: 'grunt', category: 'ai', cost_cents: 0, recurrence: 'none', idempotency_key: `milo-free-${provider}` });
|
|
71
|
+
if (result.decision === 'reject')
|
|
72
|
+
throw setupStopped(`Squire refused ${provider} signup; setup stopped`, result.reason);
|
|
73
|
+
if (result.decision === 'needs_approval' || result.decision === 'drop') {
|
|
74
|
+
drop(provider, result.reason);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
let state = result.run.state;
|
|
78
|
+
let reason = result.reason;
|
|
79
|
+
const deadline = Date.now() + 120000;
|
|
80
|
+
while (!['completed', 'failed', 'rejected', 'dropped'].includes(state) && Date.now() < deadline) {
|
|
81
|
+
const run = await squire.runs.get(result.run.id);
|
|
82
|
+
state = run.state;
|
|
83
|
+
reason = run.reason ?? reason;
|
|
84
|
+
if (run.approval_url || state === 'awaiting_approval') {
|
|
85
|
+
state = 'dropped';
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
if (!['completed', 'dropped', 'failed', 'rejected'].includes(state))
|
|
89
|
+
await delay(1000);
|
|
90
|
+
}
|
|
91
|
+
if (state === 'failed' || state === 'rejected')
|
|
92
|
+
throw setupStopped(`Squire could not provision ${provider}; setup stopped`, reason);
|
|
93
|
+
if (state !== 'completed') {
|
|
94
|
+
drop(provider, reason);
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
credential = await find();
|
|
98
|
+
if (!credential) {
|
|
99
|
+
drop(provider, 'No provider access was created');
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
const grant = await squire.credentials.grant(credential.reference);
|
|
104
|
+
await router.addGrant(provider, grant);
|
|
105
|
+
options.notify(`${provider}: connected through Squire grant`);
|
|
106
|
+
}
|
|
107
|
+
return { pending, dropped };
|
|
108
|
+
}
|
|
109
|
+
const object = (value) => value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
110
|
+
export function squireEnvironment() {
|
|
111
|
+
return {
|
|
112
|
+
...Object.fromEntries(Object.entries(process.env).filter((entry) => entry[1] !== undefined)),
|
|
113
|
+
...(process.env.MILO_SQUIRE_HOME ? { HOME: process.env.MILO_SQUIRE_HOME, XDG_CONFIG_HOME: join(process.env.MILO_SQUIRE_HOME, '.config') } : {}),
|
|
114
|
+
...(process.env.MILO_SQUIRE_XDG_CONFIG_HOME ? { XDG_CONFIG_HOME: process.env.MILO_SQUIRE_XDG_CONFIG_HOME } : {}),
|
|
115
|
+
TRUSTY_SQUIRE_AGENT_IDENTITY: 'grunt setup'
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
export class BrowserSquire {
|
|
119
|
+
options;
|
|
120
|
+
constructor(options = {}) {
|
|
121
|
+
this.options = options;
|
|
122
|
+
}
|
|
123
|
+
client = new Client({ name: 'grunt setup', version: '0.1.0' });
|
|
124
|
+
sessions = new Map();
|
|
125
|
+
handoffs = [];
|
|
126
|
+
async connect() {
|
|
127
|
+
const bin = join(dirname(fileURLToPath(import.meta.resolve('@trusty-squire/mcp'))), 'bin.js');
|
|
128
|
+
const env = squireEnvironment();
|
|
129
|
+
// Squire owns its account binding and vault. grunt never reads its files.
|
|
130
|
+
const transport = new StdioClientTransport({ command: process.execPath, args: [bin, 'server'], stderr: 'pipe', env });
|
|
131
|
+
let lastLine = '', partialLine = '';
|
|
132
|
+
const stderr = transport.stderr instanceof Readable ? transport.stderr : null;
|
|
133
|
+
stderr?.setEncoding('utf8');
|
|
134
|
+
const capture = (chunk) => {
|
|
135
|
+
const lines = (partialLine + chunk).split(/\r\n|[\r\n]/);
|
|
136
|
+
partialLine = lines.pop().slice(0, 8192);
|
|
137
|
+
for (const line of lines)
|
|
138
|
+
if (line.trim())
|
|
139
|
+
lastLine = line.slice(0, 8192);
|
|
140
|
+
};
|
|
141
|
+
stderr?.on('data', capture);
|
|
142
|
+
try {
|
|
143
|
+
await this.client.connect(transport);
|
|
144
|
+
const result = await this.call('list_credentials', { fields: 'summary' }, true);
|
|
145
|
+
if (!Array.isArray(result.credentials))
|
|
146
|
+
throw new Error('Squire did not return vault metadata');
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
await this.client.close().catch(() => { });
|
|
150
|
+
const stderrReason = partialLine.trim() || lastLine;
|
|
151
|
+
const mcpReason = error instanceof Error ? error.message : '';
|
|
152
|
+
// Prefer a broker error over a startup banner; use stderr for a bare exit.
|
|
153
|
+
const closed = error instanceof McpError && error.code === ErrorCode.ConnectionClosed
|
|
154
|
+
&& mcpReason === `MCP error ${ErrorCode.ConnectionClosed}: Connection closed`;
|
|
155
|
+
const reason = connectionReason(closed ? stderrReason || mcpReason : mcpReason || stderrReason);
|
|
156
|
+
const instruction = 'Connect Trusty Squire separately, then rerun usegrunt setup';
|
|
157
|
+
throw new Error((reason ? `${instruction}: ${reason}` : instruction).slice(0, 300));
|
|
158
|
+
}
|
|
159
|
+
finally {
|
|
160
|
+
stderr?.off('data', capture);
|
|
161
|
+
// Keep consuming stderr without retaining diagnostics after connecting.
|
|
162
|
+
stderr?.resume();
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
async call(name, args, connectionCheck = false) {
|
|
166
|
+
const result = await this.client.callTool({ name, arguments: args }, undefined, { timeout: 60000, ...(this.options.signal ? { signal: this.options.signal } : {}) }).catch(error => {
|
|
167
|
+
if (connectionCheck)
|
|
168
|
+
throw error;
|
|
169
|
+
throw new Error(`Squire ${name} is unavailable; resume setup after reconnecting Squire`);
|
|
170
|
+
});
|
|
171
|
+
if (result.isError) {
|
|
172
|
+
const reason = connectionCheck ? result.content
|
|
173
|
+
.filter(part => part.type === 'text').map(part => part.text ?? '').join(' ') : '';
|
|
174
|
+
throw new Error(reason || `Squire ${name} failed; inspect Squire for the required action`);
|
|
175
|
+
}
|
|
176
|
+
if (result.structuredContent)
|
|
177
|
+
return object(result.structuredContent);
|
|
178
|
+
for (const part of result.content) {
|
|
179
|
+
if (part.type === 'text' && part.text) {
|
|
180
|
+
try {
|
|
181
|
+
return object(JSON.parse(part.text));
|
|
182
|
+
}
|
|
183
|
+
catch { /* Do not echo upstream output. */ }
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
throw new Error(`Squire ${name} returned an unsupported result`);
|
|
187
|
+
}
|
|
188
|
+
credentials = {
|
|
189
|
+
list: async () => {
|
|
190
|
+
const result = await this.call('list_credentials', { fields: 'summary' });
|
|
191
|
+
if (!Array.isArray(result.credentials))
|
|
192
|
+
throw new Error('Squire did not return vault metadata');
|
|
193
|
+
return result.credentials;
|
|
194
|
+
},
|
|
195
|
+
grant: async (reference) => {
|
|
196
|
+
const result = await this.call('grant_app_access', { reference });
|
|
197
|
+
if (typeof result.base_url !== 'string' || typeof result.token !== 'string' || !result.token)
|
|
198
|
+
throw new Error('Squire returned no app grant');
|
|
199
|
+
const url = new URL(result.base_url);
|
|
200
|
+
if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash)
|
|
201
|
+
throw new Error('Squire returned an invalid grant endpoint');
|
|
202
|
+
return { base_url: result.base_url, token: result.token };
|
|
203
|
+
}
|
|
204
|
+
};
|
|
205
|
+
async signup(service, url, goal) {
|
|
206
|
+
const deadline = Date.now() + 600000;
|
|
207
|
+
while (Date.now() < deadline) {
|
|
208
|
+
this.options.signal?.throwIfAborted();
|
|
209
|
+
const result = await this.drive(service, url, goal);
|
|
210
|
+
if (result.run.state === 'completed')
|
|
211
|
+
return;
|
|
212
|
+
if (result.run.state !== 'running')
|
|
213
|
+
throw new Error(`${service}: signup could not be completed`);
|
|
214
|
+
}
|
|
215
|
+
throw new Error(`${service}: signup timed out; rerun setup to resume`);
|
|
216
|
+
}
|
|
217
|
+
async drive(provider, url = PROVIDERS[provider], goal) {
|
|
218
|
+
const session = this.sessions.get(provider);
|
|
219
|
+
const result = await this.call('operate_drive', {
|
|
220
|
+
...(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.`,
|
|
222
|
+
facts: { plan: 'free', project_name: 'grunt' }, max_seconds: 45, max_steps: 60
|
|
223
|
+
});
|
|
224
|
+
if (typeof result.session_id === 'string')
|
|
225
|
+
this.sessions.set(provider, result.session_id);
|
|
226
|
+
const reason = setupReason(result.reason, result.remaining);
|
|
227
|
+
const classification = [result.reason, result.remaining].filter(value => typeof value === 'string').join(' ').replace(/[_-]/g, ' ');
|
|
228
|
+
const refusal = ['refused', 'rejected', 'denied', 'forbidden'].includes(String(result.status)) || /\b(?:refus(?:al|ed|es|ing)?|denied|prohibit(?:ed|s)?|not allowed|not permitted|forbidden|disallowed)\b/i.test(classification);
|
|
229
|
+
if (refusal)
|
|
230
|
+
throw setupStopped(`Provider refused ${provider} signup; setup stopped`, reason);
|
|
231
|
+
if (result.status === 'complete') {
|
|
232
|
+
const id = this.sessions.get(provider);
|
|
233
|
+
if (id) {
|
|
234
|
+
await this.call('operate_finish', { session_id: id, outcome: 'result', summary: 'grunt provider key stored in user vault' });
|
|
235
|
+
this.sessions.delete(provider);
|
|
236
|
+
}
|
|
237
|
+
return { run: { id: provider, state: 'completed' }, decision: 'silent' };
|
|
238
|
+
}
|
|
239
|
+
const humanStep = /\b(?:sms|phone|captcha|passkey|email|e mail|verification|confirm(?:ation)?|human(?: only| input| assistance)?)\b/i.test(`${String(result.field)} ${classification}`);
|
|
240
|
+
if (!humanStep && (result.status === 'budget' || result.status === 'evaluate_timeout'))
|
|
241
|
+
return { run: { id: provider, state: 'running' }, decision: 'silent' };
|
|
242
|
+
if (humanStep && process.env.MILO_SETUP_NO_HUMAN === '1')
|
|
243
|
+
throw new Error('Provider signup needs human verification; stopped before handoff');
|
|
244
|
+
if (humanStep && this.sessions.has(provider)) {
|
|
245
|
+
const url = await this.humanHandoff(this.sessions.get(provider));
|
|
246
|
+
console.log(`Complete ${provider} verification in this browser: ${url}`);
|
|
247
|
+
if (this.options.background) {
|
|
248
|
+
await delay(5000, undefined, this.options.signal ? { signal: this.options.signal } : {});
|
|
249
|
+
return { run: { id: provider, state: 'running' }, decision: 'silent' };
|
|
250
|
+
}
|
|
251
|
+
const { confirm, isCancel } = await import('@clack/prompts');
|
|
252
|
+
const done = await confirm({ message: 'Verification complete? Resume signup.', initialValue: true });
|
|
253
|
+
if (isCancel(done) || !done)
|
|
254
|
+
throw new Error('Provider verification cancelled');
|
|
255
|
+
return { run: { id: provider, state: 'running' }, decision: 'silent' };
|
|
256
|
+
}
|
|
257
|
+
this.sessions.delete(provider);
|
|
258
|
+
return { run: { id: provider, state: 'dropped' }, decision: 'drop', reason: reason || 'Signup could not be completed' };
|
|
259
|
+
}
|
|
260
|
+
async humanHandoff(session) {
|
|
261
|
+
const share = await this.call('operate_share', { session_id: session });
|
|
262
|
+
if (typeof share.url !== 'string')
|
|
263
|
+
throw new Error('Squire returned no browser handoff link');
|
|
264
|
+
const proxy = await proxyApproval(share.url);
|
|
265
|
+
this.handoffs.push(proxy);
|
|
266
|
+
return proxy.url;
|
|
267
|
+
}
|
|
268
|
+
runs = {
|
|
269
|
+
start: async (input) => this.drive(input.service),
|
|
270
|
+
get: async (id) => { const result = await this.drive(id); return { state: result.run.state, ...(result.approval_url ? { approval_url: result.approval_url } : {}), ...(result.reason ? { reason: result.reason } : {}) }; }
|
|
271
|
+
};
|
|
272
|
+
async close() { for (const handoff of this.handoffs)
|
|
273
|
+
handoff.close(); await this.client.close(); }
|
|
274
|
+
}
|
|
275
|
+
const providerPath = (provider) => provider === 'groq' ? '/openai/v1' : '/v1';
|
|
276
|
+
export class FreeLLMAdmin {
|
|
277
|
+
token = '';
|
|
278
|
+
url;
|
|
279
|
+
constructor(url = process.env.MILO_FREELLMAPI_URL || 'http://127.0.0.1:3001') { this.url = loopback(url).origin; }
|
|
280
|
+
async call(path, body, method = 'POST') {
|
|
281
|
+
const response = await fetch(`${this.url}/api${path}`, { method, headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.token}` }, redirect: 'error', signal: AbortSignal.timeout(10000), ...(body === undefined ? {} : { body: JSON.stringify(body) }) });
|
|
282
|
+
if (!response.ok)
|
|
283
|
+
throw new Error(`Local FreeLLMAPI setup returned HTTP ${response.status}`);
|
|
284
|
+
return await response.json();
|
|
285
|
+
}
|
|
286
|
+
async login(password) { this.token = (await this.call('/auth/login', { email: 'milo@localhost.local', password })).token; }
|
|
287
|
+
async hasKey(platform) {
|
|
288
|
+
const keys = await this.call('/keys', undefined, 'GET');
|
|
289
|
+
const key = keys.find(key => key.platform === 'custom' && key.label === `Milo ${platform} (Squire)` && key.baseUrl?.endsWith(providerPath(platform)));
|
|
290
|
+
if (!key || !key.models?.length)
|
|
291
|
+
return false;
|
|
292
|
+
const discovery = await this.call('/keys/custom/discover-models', { keyId: key.id });
|
|
293
|
+
return key.models.some(model => discovery.models.some(available => available.id === model.modelId));
|
|
294
|
+
}
|
|
295
|
+
async addGrant(platform, grant) {
|
|
296
|
+
// Custom endpoints bind the grant URL and token to their own model rows.
|
|
297
|
+
const baseUrl = grant.base_url.replace(/\/$/, '') + providerPath(platform);
|
|
298
|
+
const label = `Milo ${platform} (Squire)`;
|
|
299
|
+
const discovery = await this.call('/keys/custom/discover-models', { baseUrl, apiKey: grant.token });
|
|
300
|
+
const available = new Set(discovery.models.map(model => model.id));
|
|
301
|
+
const model = ['openai/gpt-oss-120b', 'gpt-oss-120b', 'llama-3.3-70b-versatile', 'openai/gpt-oss-20b'].find(id => available.has(id));
|
|
302
|
+
if (!model)
|
|
303
|
+
throw new Error(`No supported chat model is currently available from ${platform}`);
|
|
304
|
+
const previous = await this.call('/keys', undefined, 'GET');
|
|
305
|
+
await this.call('/keys/custom', { baseUrl, apiKey: grant.token,
|
|
306
|
+
label, models: [{ model, supportsTools: true }] });
|
|
307
|
+
for (const key of previous) {
|
|
308
|
+
if (Number.isInteger(key.id) && key.platform === 'custom' && key.label === label && key.baseUrl !== baseUrl)
|
|
309
|
+
await this.call(`/keys/${key.id}`, undefined, 'DELETE');
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
async ensureFreeFallbacks() {
|
|
313
|
+
const keys = await this.call('/keys', undefined, 'GET');
|
|
314
|
+
for (const provider of freeConfig().customProviders) {
|
|
315
|
+
if (!keys.some(key => key.platform === 'custom' && key.baseUrl === provider.baseUrl)) {
|
|
316
|
+
await this.call('/keys/custom', provider);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
async configureChain(excluded = []) {
|
|
321
|
+
const chain = await this.call('/fallback', undefined, 'GET');
|
|
322
|
+
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';
|
|
325
|
+
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 };
|
|
328
|
+
});
|
|
329
|
+
await this.call('/fallback', rows, 'PUT');
|
|
330
|
+
await this.call('/fallback/routing', { strategy: 'priority' }, 'PUT');
|
|
331
|
+
}
|
|
332
|
+
async key() { return (await this.call('/settings/api-key', undefined, 'GET')).apiKey; }
|
|
333
|
+
}
|
|
334
|
+
// Read only key metadata from the legacy volume. Never load or print key values.
|
|
335
|
+
const inspectRouterKeys = `
|
|
336
|
+
const fs = require('node:fs');
|
|
337
|
+
const path = '/data/freeapi.db';
|
|
338
|
+
if (!fs.existsSync(path)) {
|
|
339
|
+
if (fs.readdirSync('/data').length) throw new Error('Unrecognized router data');
|
|
340
|
+
console.log('empty');
|
|
341
|
+
} else {
|
|
342
|
+
const Database = require('better-sqlite3');
|
|
343
|
+
const db = new Database(path, { readonly: true, fileMustExist: true });
|
|
344
|
+
const keys = db.prepare("SELECT EXISTS(SELECT 1 FROM api_keys WHERE label = 'Milo' OR platform != 'custom' OR label NOT IN ('Kilo Free', 'Freebuff')) AS present").get();
|
|
345
|
+
console.log(keys.present ? 'keys' : 'empty');
|
|
346
|
+
db.close();
|
|
347
|
+
}`;
|
|
348
|
+
export async function ensureRouter(password, encryption, run = argsWithEnv, recreate = false) {
|
|
349
|
+
if (!['linux', 'darwin'].includes(process.platform))
|
|
350
|
+
throw new Error('grunt FreeLLMAPI requires Docker on Linux or macOS');
|
|
351
|
+
let owner;
|
|
352
|
+
try {
|
|
353
|
+
owner = (await run(['inspect', '--format', '{{ index .Config.Labels "io.milo.router" }}', 'milo-freellmapi'])).stdout.trim();
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
if (!/No such (object|container)/i.test(String(error.stderr)))
|
|
357
|
+
throw new Error('Docker is unavailable. Install and start Docker Desktop, then run npx usegrunt setup again');
|
|
358
|
+
}
|
|
359
|
+
if (owner !== undefined) {
|
|
360
|
+
if (owner !== '1')
|
|
361
|
+
throw new Error('The milo-freellmapi container belongs to another installation; setup stopped');
|
|
362
|
+
if (!recreate) {
|
|
363
|
+
await run(['start', 'milo-freellmapi']);
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
await run(['pull', FREELLMAPI_IMAGE]);
|
|
368
|
+
if (recreate) {
|
|
369
|
+
let volume = false;
|
|
370
|
+
try {
|
|
371
|
+
await run(['volume', 'inspect', 'milo-freellmapi-data']);
|
|
372
|
+
volume = true;
|
|
373
|
+
}
|
|
374
|
+
catch (error) {
|
|
375
|
+
if (!/No such volume/i.test(String(error.stderr)))
|
|
376
|
+
throw new Error('Cannot inspect the old router volume; its data was preserved');
|
|
377
|
+
}
|
|
378
|
+
if (volume) {
|
|
379
|
+
let state;
|
|
380
|
+
try {
|
|
381
|
+
state = (await run(['run', '--rm', '--network', 'none', '--mount', 'type=volume,src=milo-freellmapi-data,dst=/data,readonly', '--entrypoint', 'node', FREELLMAPI_IMAGE, '-e', inspectRouterKeys])).stdout.trim();
|
|
382
|
+
}
|
|
383
|
+
catch {
|
|
384
|
+
throw new Error('Cannot check the old router for provider keys; its data was preserved');
|
|
385
|
+
}
|
|
386
|
+
if (state !== 'empty')
|
|
387
|
+
throw new Error('Local router admin credentials are missing and the old router may contain provider keys; its data was preserved. Restore the local admin file before rerunning setup');
|
|
388
|
+
}
|
|
389
|
+
if (owner !== undefined)
|
|
390
|
+
await run(['rm', '-f', 'milo-freellmapi']);
|
|
391
|
+
if (volume)
|
|
392
|
+
await run(['volume', 'rm', 'milo-freellmapi-data']);
|
|
393
|
+
}
|
|
394
|
+
// 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], {
|
|
396
|
+
...process.env, ENCRYPTION_KEY: encryption, FREEAPI_CONFIG_JSON: JSON.stringify({ admin: { email: 'milo@localhost.local', password }, ...freeConfig() })
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
async function argsWithEnv(args, env) {
|
|
400
|
+
return await exec('docker', args, { timeout: 300000, maxBuffer: 1048576, ...(env ? { env } : {}) });
|
|
401
|
+
}
|
|
402
|
+
function localAdminPath() {
|
|
403
|
+
return join(process.env.MILO_CONFIG_DIR || join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'milo'), 'router-admin.json');
|
|
404
|
+
}
|
|
405
|
+
async function readLocalAdmin() {
|
|
406
|
+
let handle;
|
|
407
|
+
try {
|
|
408
|
+
handle = await open(localAdminPath(), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
409
|
+
}
|
|
410
|
+
catch (error) {
|
|
411
|
+
if (error.code === 'ENOENT')
|
|
412
|
+
return;
|
|
413
|
+
throw new Error('Cannot read the local router admin file; setup stopped');
|
|
414
|
+
}
|
|
415
|
+
try {
|
|
416
|
+
const stat = await handle.stat();
|
|
417
|
+
if (!stat.isFile() || (stat.mode & 0o777) !== 0o600 || stat.uid !== process.getuid?.())
|
|
418
|
+
throw new Error('permissions');
|
|
419
|
+
const saved = object(JSON.parse(await handle.readFile('utf8')));
|
|
420
|
+
if (typeof saved.password !== 'string' || !/^[a-f0-9]{64}$/.test(saved.password)
|
|
421
|
+
|| typeof saved.encryption !== 'string' || !/^[a-f0-9]{64}$/.test(saved.encryption))
|
|
422
|
+
throw new Error('format');
|
|
423
|
+
return { password: saved.password, encryption: saved.encryption };
|
|
424
|
+
}
|
|
425
|
+
catch {
|
|
426
|
+
throw new Error('Invalid local router admin file; it must belong to this user and have mode 0600. Restore it before rerunning setup');
|
|
427
|
+
}
|
|
428
|
+
finally {
|
|
429
|
+
await handle.close();
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
async function saveLocalAdmin(admin) {
|
|
433
|
+
const path = localAdminPath(), temporary = `${path}.${randomBytes(8).toString('hex')}.tmp`;
|
|
434
|
+
try {
|
|
435
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
436
|
+
await writeFile(temporary, JSON.stringify(admin), { mode: 0o600, flag: 'wx' });
|
|
437
|
+
await rename(temporary, path);
|
|
438
|
+
}
|
|
439
|
+
catch {
|
|
440
|
+
throw new Error('Cannot save the local router admin file; setup stopped');
|
|
441
|
+
}
|
|
442
|
+
finally {
|
|
443
|
+
await rm(temporary, { force: true }).catch(() => { });
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
// Read the login already created by setup; never provision or change router state.
|
|
447
|
+
export async function loadRouterKey(url) {
|
|
448
|
+
const admin = await readLocalAdmin();
|
|
449
|
+
if (!admin)
|
|
450
|
+
throw new Error('Local router login is missing');
|
|
451
|
+
const router = new FreeLLMAdmin(url);
|
|
452
|
+
await router.login(admin.password);
|
|
453
|
+
const key = await router.key();
|
|
454
|
+
if (typeof key !== 'string' || !key.trim())
|
|
455
|
+
throw new Error('Local router returned an invalid API key');
|
|
456
|
+
return key;
|
|
457
|
+
}
|
|
458
|
+
export async function setup(notify = console.log, providers = ['groq']) {
|
|
459
|
+
const squire = new BrowserSquire();
|
|
460
|
+
const router = new FreeLLMAdmin();
|
|
461
|
+
try {
|
|
462
|
+
await squire.connect();
|
|
463
|
+
const saved = await readLocalAdmin();
|
|
464
|
+
if (!saved && process.env.MILO_ROUTER_RUNTIME !== 'docker') {
|
|
465
|
+
const database = join(dirname(localAdminPath()), 'freellmapi', 'router.db');
|
|
466
|
+
let exists = false;
|
|
467
|
+
try {
|
|
468
|
+
await lstat(database);
|
|
469
|
+
exists = true;
|
|
470
|
+
}
|
|
471
|
+
catch (error) {
|
|
472
|
+
if (error.code !== 'ENOENT')
|
|
473
|
+
throw new Error('Cannot inspect the existing router database; setup stopped');
|
|
474
|
+
}
|
|
475
|
+
if (exists)
|
|
476
|
+
throw new Error('Existing router database has no local encryption key. Restore its original private router login before rerunning setup');
|
|
477
|
+
}
|
|
478
|
+
const admin = saved ?? { password: randomBytes(32).toString('hex'), encryption: randomBytes(32).toString('hex') };
|
|
479
|
+
if (process.env.MILO_ROUTER_RUNTIME === 'docker') {
|
|
480
|
+
await ensureRouter(admin.password, admin.encryption, argsWithEnv, !saved);
|
|
481
|
+
if (!saved)
|
|
482
|
+
await saveLocalAdmin(admin);
|
|
483
|
+
}
|
|
484
|
+
else {
|
|
485
|
+
if (!saved)
|
|
486
|
+
await saveLocalAdmin(admin);
|
|
487
|
+
const { ensureNativeRouter } = await import('./native-router.js');
|
|
488
|
+
await ensureNativeRouter();
|
|
489
|
+
}
|
|
490
|
+
let ready = false;
|
|
491
|
+
for (let attempt = 0; attempt < 60; attempt++) {
|
|
492
|
+
try {
|
|
493
|
+
await router.login(admin.password);
|
|
494
|
+
ready = true;
|
|
495
|
+
break;
|
|
496
|
+
}
|
|
497
|
+
catch {
|
|
498
|
+
await delay(1000);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
if (!ready)
|
|
502
|
+
throw new Error('Local FreeLLMAPI did not start; inspect its health before resuming setup');
|
|
503
|
+
await router.ensureFreeFallbacks();
|
|
504
|
+
await router.configureChain();
|
|
505
|
+
const result = await setupProviders(squire, router, { providers, notify });
|
|
506
|
+
const dropped = new Set(result.dropped);
|
|
507
|
+
if (dropped.size)
|
|
508
|
+
throw new Error(`Provider setup incomplete: ${[...dropped].join(', ')}`);
|
|
509
|
+
await router.configureChain([...dropped]);
|
|
510
|
+
return { key: await router.key() };
|
|
511
|
+
}
|
|
512
|
+
catch (error) {
|
|
513
|
+
// Child-process and SDK errors may contain request data. Keep those local.
|
|
514
|
+
if (error instanceof SetupPending)
|
|
515
|
+
throw new Error('Squire approval remains pending; resume usegrunt setup after approval');
|
|
516
|
+
if (error instanceof Error && !('cmd' in error))
|
|
517
|
+
throw error;
|
|
518
|
+
throw new Error('FreeLLMAPI installation failed; check Node and npm availability and run npx usegrunt setup again');
|
|
519
|
+
}
|
|
520
|
+
finally {
|
|
521
|
+
await squire.close();
|
|
522
|
+
}
|
|
523
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
export class SourceError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
retryAfter;
|
|
4
|
+
output;
|
|
5
|
+
constructor(code, message, retryAfter, output) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.code = code;
|
|
8
|
+
this.retryAfter = retryAfter;
|
|
9
|
+
this.output = output;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export class Sources {
|
|
13
|
+
sources;
|
|
14
|
+
router;
|
|
15
|
+
constructor(sources, routing) {
|
|
16
|
+
this.sources = sources;
|
|
17
|
+
if (!sources.length || new Set(sources.map(s => s.id)).size !== sources.length)
|
|
18
|
+
throw new Error('Configure distinct grunt sources');
|
|
19
|
+
if (routing)
|
|
20
|
+
this.router = new StickyRouter(sources, routing);
|
|
21
|
+
}
|
|
22
|
+
healthy(id) { this.router?.healthy(id); }
|
|
23
|
+
async models() {
|
|
24
|
+
const available = await Promise.all(this.sources.map(async (s) => {
|
|
25
|
+
if (this.router && !this.router.listed(s.id) || !await s.available())
|
|
26
|
+
return null;
|
|
27
|
+
if (this.router?.needsHealthCheck(s.id) && await s.healthCheck?.().catch(() => false))
|
|
28
|
+
this.healthy(s.id);
|
|
29
|
+
return s.id;
|
|
30
|
+
}));
|
|
31
|
+
return [...(this.router ? ['grunt'] : []), 'milo', ...available.filter((id) => id !== null)];
|
|
32
|
+
}
|
|
33
|
+
async complete(request, signal) {
|
|
34
|
+
if (this.router)
|
|
35
|
+
return this.router.complete(request, signal);
|
|
36
|
+
const known = this.sources.some(s => s.id === request.model);
|
|
37
|
+
if (request.model !== 'milo' && !known)
|
|
38
|
+
console.error(`grunt model mapping: ${request.model} -> milo`);
|
|
39
|
+
const candidates = request.model === 'milo' || !known ? this.sources : this.sources.filter(s => s.id === request.model);
|
|
40
|
+
let last = new SourceError('not_installed', `No source for model ${request.model}`);
|
|
41
|
+
for (const source of candidates) {
|
|
42
|
+
signal.throwIfAborted();
|
|
43
|
+
let completion;
|
|
44
|
+
try {
|
|
45
|
+
completion = await source.complete(request, signal);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (signal.aborted || (error instanceof SourceError && error.code === 'cancelled')) {
|
|
49
|
+
console.error(`grunt source: ${source.id} cancelled for model ${request.model}`);
|
|
50
|
+
signal.throwIfAborted();
|
|
51
|
+
throw error;
|
|
52
|
+
}
|
|
53
|
+
last = error instanceof SourceError ? error : new SourceError('upstream', `Source ${source.id} failed`);
|
|
54
|
+
console.error(`grunt source: ${source.id} failed (${last.code}) for model ${request.model}`);
|
|
55
|
+
// Only dots and OpenCode deadlines fall through; caller cancellation stops above.
|
|
56
|
+
if (last.code === 'timeout' && source.id !== 'dots' && source.id !== 'opencode')
|
|
57
|
+
throw last;
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
console.error(`grunt source: ${source.id} answered model ${request.model}`);
|
|
61
|
+
return completion;
|
|
62
|
+
}
|
|
63
|
+
throw last;
|
|
64
|
+
}
|
|
65
|
+
async close() { await Promise.allSettled(this.sources.map(s => s.close?.())); }
|
|
66
|
+
}
|
|
67
|
+
export class Gateway {
|
|
68
|
+
key;
|
|
69
|
+
fetcher;
|
|
70
|
+
id = 'gateway';
|
|
71
|
+
url;
|
|
72
|
+
constructor(url, key, fetcher = fetch) {
|
|
73
|
+
this.key = key;
|
|
74
|
+
this.fetcher = fetcher;
|
|
75
|
+
const parsed = new URL(url);
|
|
76
|
+
if (!['https:', 'http:'].includes(parsed.protocol) || parsed.username || parsed.password || parsed.search || parsed.hash)
|
|
77
|
+
throw new Error('Invalid MILO_GATEWAY_URL');
|
|
78
|
+
this.url = `${url.replace(/\/$/, '')}/chat/completions`;
|
|
79
|
+
}
|
|
80
|
+
async available() { return true; }
|
|
81
|
+
async complete(request, signal) {
|
|
82
|
+
if (request.tools.some(t => t.type !== 'function'))
|
|
83
|
+
throw new SourceError('upstream', 'Gateway Chat Completions cannot forward native Responses tools; use the local app source');
|
|
84
|
+
let response;
|
|
85
|
+
try {
|
|
86
|
+
response = await this.fetcher(this.url, {
|
|
87
|
+
method: 'POST', redirect: 'error', signal,
|
|
88
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${this.key}` },
|
|
89
|
+
body: JSON.stringify({ model: 'milo', messages: request.messages, tools: request.tools.length ? request.tools : undefined, stream: false, max_tokens: request.maxTokens ?? 4096 })
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
catch {
|
|
93
|
+
signal.throwIfAborted();
|
|
94
|
+
throw new SourceError('upstream', 'grunt gateway is unavailable');
|
|
95
|
+
}
|
|
96
|
+
if (!response.ok)
|
|
97
|
+
throw new SourceError(response.status === 401 || response.status === 403 ? 'logged_out' : response.status === 429 ? 'quota' : 'upstream', `grunt gateway returned HTTP ${response.status}`);
|
|
98
|
+
const body = await response.json();
|
|
99
|
+
const message = body.choices?.[0]?.message;
|
|
100
|
+
if (!message || (typeof message.content !== 'string' && !message.tool_calls?.length))
|
|
101
|
+
throw new SourceError('upstream', 'grunt gateway returned an invalid completion');
|
|
102
|
+
return { text: message.content ?? '', ...(message.tool_calls ? { calls: message.tool_calls } : {}), inputTokens: body.usage?.prompt_tokens ?? 0, outputTokens: body.usage?.completion_tokens ?? 0 };
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
import { StickyRouter } from './routing.js';
|