usegrunt 0.6.2 → 0.6.3
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 +9 -2
- package/dist/local/consent-source.js +27 -0
- package/dist/local/data-sharing.js +100 -0
- package/dist/local/deepseek-response.js +16 -0
- package/dist/local/fallback-setup.js +52 -3
- package/dist/local/free-api.js +137 -0
- package/dist/local/main.js +33 -9
- package/dist/local/managed-router.js +32 -0
- package/dist/local/nvidia.js +116 -0
- package/dist/local/opencode.js +100 -7
- package/dist/local/registry-source.js +26 -0
- package/dist/local/setup.js +269 -70
- package/dist/local/tokenharbor.js +64 -0
- package/dist/local/wizard.js +46 -9
- package/dist/local/zro.js +79 -0
- package/dist/registry/adapters.json +32 -4
- package/dist/registry.js +6 -4
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -16,11 +16,18 @@ usegrunt installs grunt on this machine. It pools free AI quota and serves one l
|
|
|
16
16
|
|
|
17
17
|
https://grunt.run. `milo` and `milo-local` remain aliases.
|
|
18
18
|
|
|
19
|
-
Setup connects Trusty Squire, installs and connects the vetted Freebuff, OpenCode and
|
|
19
|
+
Setup connects Trusty Squire, installs and connects the vetted Freebuff, OpenCode, Kilo and Zro pool, plus opt-in Token Harbor (up to three services at once), tests each service, then connects your coding tools. Browser verification is queued when a provider needs you. Pi is optional.
|
|
20
20
|
|
|
21
21
|
[Reference](https://github.com/bananaman614305/milo/blob/main/docs/reference.md).
|
|
22
22
|
|
|
23
|
-
New installs
|
|
23
|
+
New installs offer Zro's daily free DeepSeek V4.1 Flash and, after one grouped data-sharing approval, Freebuff, OpenCode, Kilo Auto and Token Harbor's weekly free route. `usegrunt add groq`, `usegrunt add cerebras`, `usegrunt add kilo`, `usegrunt add zro` and `usegrunt add tokenharbor` remain available. NVIDIA stays optional while its account verification is unvetted. Existing `freellmapi` routes keep their Nemotron-first Kilo chain; the default `kilo` source selects `kilo-auto/free`. Repeat setup preserves previously connected API sources.
|
|
24
|
+
|
|
25
|
+
Within OpenCode, new conversations rotate across DeepSeek V4.1 Flash Free,
|
|
26
|
+
GLM 5.3 Flash Free and Big Pickle. Follow-ups keep their model, and quota errors
|
|
27
|
+
try another free model. These models may share provider limits; the pool does
|
|
28
|
+
not create extra credit balances or purchase credits.
|
|
29
|
+
|
|
30
|
+
Zro setup confirms the Free plan, no purchased usage balance and no payment method before connecting it. Keep that account free: Zro uses the same model ID on its paid plans, so changing the account outside grunt can change billing. Token Harbor uses its explicit `:free` model ID and requires approval for retained prompts, staff review and upstream-provider training. Setup asks once for Freebuff, OpenCode (including Big Pickle), Kilo and Token Harbor together, showing each provider's disclosure. It remembers the approved policy versions in a private local receipt. Declining or cancelling skips those providers and continues with Zro. Changed or newly added policies require another grouped approval. Background setup never prompts, and both signup and routing require the current receipt.
|
|
24
31
|
|
|
25
32
|
Kilo free models may log prompts or use them for training; avoid sensitive prompts. Their free limits are unknown. Kilo quota errors fall through FreeLLMAPI's fallback chain. Requests allow a long timeout for Nemotron reasoning.
|
|
26
33
|
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { readDataSharingConsent } from './data-sharing.js';
|
|
2
|
+
import { SourceError } from './source.js';
|
|
3
|
+
// Every request rechecks the policy receipt, including requests made with an
|
|
4
|
+
// existing grant. A daemon cannot silently keep sharing after approval changes.
|
|
5
|
+
export class ConsentSource {
|
|
6
|
+
source;
|
|
7
|
+
directory;
|
|
8
|
+
id;
|
|
9
|
+
supportsFunctionTools;
|
|
10
|
+
constructor(source, directory) {
|
|
11
|
+
this.source = source;
|
|
12
|
+
this.directory = directory;
|
|
13
|
+
this.id = source.id;
|
|
14
|
+
if (source.supportsFunctionTools !== undefined)
|
|
15
|
+
this.supportsFunctionTools = source.supportsFunctionTools;
|
|
16
|
+
}
|
|
17
|
+
async approved() { return (await readDataSharingConsent(this.directory, [this.id])).includes(this.id); }
|
|
18
|
+
async available() { return await this.approved() && await this.source.available(); }
|
|
19
|
+
async healthCheck() { return await this.approved() && await (this.source.healthCheck?.() ?? this.source.available()); }
|
|
20
|
+
async complete(request, signal) {
|
|
21
|
+
signal.throwIfAborted();
|
|
22
|
+
if (!await this.approved())
|
|
23
|
+
throw new SourceError('refused', `${this.id} data sharing is not approved; rerun usegrunt setup`);
|
|
24
|
+
return this.source.complete(request, signal);
|
|
25
|
+
}
|
|
26
|
+
async close() { await this.source.close?.(); }
|
|
27
|
+
}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { mkdir, open, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { confirm, isCancel } from '@clack/prompts';
|
|
6
|
+
import { withTerminal } from './terminal.js';
|
|
7
|
+
// Keep these provider-specific. An unknown policy must not inherit consent to
|
|
8
|
+
// another provider merely because both offer free inference.
|
|
9
|
+
export const DATA_SHARING_POLICIES = [{
|
|
10
|
+
provider: 'freebuff', name: 'Freebuff', version: '2026-11-15',
|
|
11
|
+
disclosure: 'Prompt, code, and project data may be retained and reviewed for safety, analyzed for advertising and ad-model training. For features marked for AI training, data may also be shared or sold for training and research.',
|
|
12
|
+
url: 'https://freebuff.com/privacy-policy'
|
|
13
|
+
}, {
|
|
14
|
+
provider: 'opencode', name: 'OpenCode free model pool', version: '2026-11-15',
|
|
15
|
+
disclosure: 'DeepSeek and GLM free routes have zero-retention and no-training policies. The pool also includes Big Pickle, whose collected data may be used for model improvement.',
|
|
16
|
+
url: 'https://opencode.ai/docs/console/models/#privacy'
|
|
17
|
+
}, {
|
|
18
|
+
provider: 'kilo', name: 'Kilo Auto Free', version: '2026-11-15',
|
|
19
|
+
disclosure: 'Auto Free may select providers that log prompts and outputs and use them to improve their services. Do not submit personal or confidential data.',
|
|
20
|
+
url: 'https://kilo.ai/docs/getting-started/using-kilo-for-free'
|
|
21
|
+
}, {
|
|
22
|
+
provider: 'tokenharbor', name: 'Token Harbor', version: '2026-11-15',
|
|
23
|
+
disclosure: 'Prompts and responses may be retained, read by staff, and used by upstream providers for training.',
|
|
24
|
+
url: 'https://team.tokenharbor.ai/faq'
|
|
25
|
+
}];
|
|
26
|
+
const fileName = 'data-sharing-consent.json';
|
|
27
|
+
const emptyReceipt = () => ({ version: 1, approved: {} });
|
|
28
|
+
const fingerprint = (policy) => createHash('sha256').update(JSON.stringify(policy)).digest('hex');
|
|
29
|
+
export function dataSharingPolicies(providers) {
|
|
30
|
+
const selected = new Set(providers);
|
|
31
|
+
return DATA_SHARING_POLICIES.filter(policy => selected.has(policy.provider));
|
|
32
|
+
}
|
|
33
|
+
export const requiresDataSharing = (provider) => DATA_SHARING_POLICIES.some(policy => policy.provider === provider);
|
|
34
|
+
async function receipt(directory) {
|
|
35
|
+
try {
|
|
36
|
+
const file = await open(join(directory, fileName), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
37
|
+
try {
|
|
38
|
+
const stat = await file.stat();
|
|
39
|
+
if (!stat.isFile() || (stat.mode & 0o777) !== 0o600 || stat.uid !== process.getuid?.())
|
|
40
|
+
return emptyReceipt();
|
|
41
|
+
const value = JSON.parse(await file.readFile('utf8'));
|
|
42
|
+
if (value.version !== 1 || !value.approved || typeof value.approved !== 'object' || Array.isArray(value.approved))
|
|
43
|
+
return emptyReceipt();
|
|
44
|
+
const approved = Object.fromEntries(Object.entries(value.approved).filter(([, item]) => item
|
|
45
|
+
&& typeof item.policy === 'string' && typeof item.approvedAt === 'string' && Number.isFinite(Date.parse(item.approvedAt))));
|
|
46
|
+
return { version: 1, approved };
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
await file.close();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
catch {
|
|
53
|
+
return emptyReceipt();
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Background setup only reads this receipt; it never opens an approval prompt.
|
|
57
|
+
export async function readDataSharingConsent(directory, providers) {
|
|
58
|
+
const saved = await receipt(directory);
|
|
59
|
+
return dataSharingPolicies(providers).filter(policy => saved.approved[policy.provider]?.policy === fingerprint(policy)).map(policy => policy.provider);
|
|
60
|
+
}
|
|
61
|
+
// Also used when an operator has already obtained explicit approval outside the
|
|
62
|
+
// CLI. This records the same current provider policies as the grouped prompt.
|
|
63
|
+
export async function recordDataSharingConsent(directory, providers) {
|
|
64
|
+
const policies = dataSharingPolicies(providers);
|
|
65
|
+
if (!policies.length)
|
|
66
|
+
return;
|
|
67
|
+
const saved = await receipt(directory);
|
|
68
|
+
const approvedAt = new Date().toISOString();
|
|
69
|
+
for (const policy of policies)
|
|
70
|
+
saved.approved[policy.provider] = { policy: fingerprint(policy), approvedAt };
|
|
71
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
72
|
+
const temporary = join(directory, `${fileName}.${randomUUID()}.tmp`);
|
|
73
|
+
try {
|
|
74
|
+
await writeFile(temporary, JSON.stringify(saved), { mode: 0o600, flag: 'wx' });
|
|
75
|
+
await rename(temporary, join(directory, fileName));
|
|
76
|
+
}
|
|
77
|
+
finally {
|
|
78
|
+
await rm(temporary, { force: true });
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
export async function approveDataSharing(options) {
|
|
82
|
+
const policies = dataSharingPolicies(options.providers);
|
|
83
|
+
const approved = await readDataSharingConsent(options.directory, options.providers);
|
|
84
|
+
const pending = policies.filter(policy => !approved.includes(policy.provider));
|
|
85
|
+
if (!pending.length)
|
|
86
|
+
return { approved, skipped: [] };
|
|
87
|
+
const interactive = options.interactive ?? Boolean(process.stdin.isTTY && process.stdout.isTTY && process.env.MILO_SETUP_NO_HUMAN !== '1');
|
|
88
|
+
const prompt = options.prompt ?? (async (list) => withTerminal(async () => confirm({
|
|
89
|
+
message: `Allow data sharing for all these free services?\n${list.map(policy => `${policy.name}: ${policy.disclosure} ${policy.url}`).join('\n')}\nDeclining skips these services; other free services continue.`,
|
|
90
|
+
initialValue: false
|
|
91
|
+
})));
|
|
92
|
+
if (!interactive)
|
|
93
|
+
return { approved, skipped: pending.map(policy => policy.provider) };
|
|
94
|
+
const answer = await prompt(pending);
|
|
95
|
+
if (isCancel(answer) || answer !== true)
|
|
96
|
+
return { approved, skipped: pending.map(policy => policy.provider) };
|
|
97
|
+
const newlyApproved = pending.map(policy => policy.provider);
|
|
98
|
+
await recordDataSharingConsent(options.directory, newlyApproved);
|
|
99
|
+
return { approved: [...approved, ...newlyApproved], skipped: [] };
|
|
100
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { isDeepSeekV41Model } from './free-api.js';
|
|
2
|
+
// A provider must acknowledge the pinned model before its completion can be
|
|
3
|
+
// attributed to this route. Error payloads and model mismatches stay private.
|
|
4
|
+
export function verifiedDeepSeekFetch(fetcher) {
|
|
5
|
+
return async (input, init) => {
|
|
6
|
+
const response = await (fetcher ?? fetch)(input, init);
|
|
7
|
+
if (response.ok && String(input).endsWith('/chat/completions')) {
|
|
8
|
+
const result = await response.clone().json();
|
|
9
|
+
if (!isDeepSeekV41Model(result.model)) {
|
|
10
|
+
await response.body?.cancel();
|
|
11
|
+
return new Response(null, { status: 502 });
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return response;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
@@ -7,6 +7,7 @@ import { parallelMap } from './parallel.js';
|
|
|
7
7
|
import { OpenCode } from './opencode.js';
|
|
8
8
|
import { DOCTOR_TIMEOUT_MS } from './freebuff.js';
|
|
9
9
|
import { SourceError, Sources } from './source.js';
|
|
10
|
+
import { DATA_SHARING_POLICIES, readDataSharingConsent, requiresDataSharing } from './data-sharing.js';
|
|
10
11
|
const exec = promisify(execFile);
|
|
11
12
|
class FreebuffCompatibilityError extends Error {
|
|
12
13
|
}
|
|
@@ -62,9 +63,13 @@ export async function verifyOpenCode(source = new OpenCode(), signal = AbortSign
|
|
|
62
63
|
}
|
|
63
64
|
}
|
|
64
65
|
export async function setupFallbacks(options) {
|
|
65
|
-
const squire = options.squire ?? new BrowserSquire();
|
|
66
|
+
const squire = options.squire ?? new BrowserSquire({ dataSharingApproved: options.dataSharingApproved ?? [] });
|
|
66
67
|
const owned = squire instanceof BrowserSquire && !options.squire;
|
|
67
68
|
const unavailable = [];
|
|
69
|
+
const adapters = options.adapters ?? ['freebuff', 'opencode'];
|
|
70
|
+
const approved = [...new Set([...(options.dataSharingApproved ?? []), ...await readDataSharingConsent(options.directory, DATA_SHARING_POLICIES.map(policy => policy.provider))])];
|
|
71
|
+
if (squire instanceof BrowserSquire)
|
|
72
|
+
squire.approveDataSharing(approved);
|
|
68
73
|
// Reuse a grant created by an earlier setup before probing the free model.
|
|
69
74
|
const savedConfig = join(options.directory, 'opencode-free.json');
|
|
70
75
|
try {
|
|
@@ -189,17 +194,61 @@ export async function setupFallbacks(options) {
|
|
|
189
194
|
await connect();
|
|
190
195
|
if (!squire.runs)
|
|
191
196
|
throw new Error('Provider signup requires Squire');
|
|
192
|
-
await (options.setupProvider ?? setup)(options.notify, ['kilo'], { squire: squire, ...(options.signal ? { signal: options.signal } : {}) });
|
|
197
|
+
await (options.setupProvider ?? setup)(options.notify, ['kilo'], { squire: squire, directory: options.directory, dataSharingApproved: approved, ...(options.signal ? { signal: options.signal } : {}) });
|
|
193
198
|
options.notify('Kilo: free API access connected through Squire');
|
|
194
199
|
}
|
|
195
200
|
catch {
|
|
196
201
|
unavailable.push('kilo');
|
|
197
202
|
options.notify('Kilo setup incomplete. Rerun usegrunt setup to resume; other routes remain available.');
|
|
198
203
|
}
|
|
204
|
+
},
|
|
205
|
+
zro: async () => {
|
|
206
|
+
try {
|
|
207
|
+
await connect();
|
|
208
|
+
if (!squire.runs)
|
|
209
|
+
throw new Error('Provider signup requires Squire');
|
|
210
|
+
await (options.setupProvider ?? setup)(options.notify, ['zro'], { squire: squire, directory: options.directory, ...(options.signal ? { signal: options.signal } : {}) });
|
|
211
|
+
options.notify('Zro: daily free DeepSeek V4.1 Flash access connected through Squire');
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
unavailable.push('zro');
|
|
215
|
+
options.notify('Zro setup incomplete. Rerun usegrunt setup to resume; other routes remain available.');
|
|
216
|
+
}
|
|
217
|
+
},
|
|
218
|
+
tokenharbor: async () => {
|
|
219
|
+
try {
|
|
220
|
+
await connect();
|
|
221
|
+
if (!squire.runs)
|
|
222
|
+
throw new Error('Provider signup requires Squire');
|
|
223
|
+
await (options.setupProvider ?? setup)(options.notify, ['tokenharbor'], { squire: squire, directory: options.directory,
|
|
224
|
+
dataSharingApproved: approved, ...(options.signal ? { signal: options.signal } : {}) });
|
|
225
|
+
options.notify('Token Harbor: approved free DeepSeek V4.1 Flash access connected through Squire');
|
|
226
|
+
}
|
|
227
|
+
catch {
|
|
228
|
+
unavailable.push('tokenharbor');
|
|
229
|
+
options.notify('Token Harbor setup incomplete or data sharing not approved. Run usegrunt setup to finish; other routes remain available.');
|
|
230
|
+
}
|
|
231
|
+
},
|
|
232
|
+
nvidia: async () => {
|
|
233
|
+
try {
|
|
234
|
+
await connect();
|
|
235
|
+
if (!squire.runs)
|
|
236
|
+
throw new Error('Provider signup requires Squire');
|
|
237
|
+
await (options.setupProvider ?? setup)(options.notify, ['nvidia'], { squire: squire, ...(options.signal ? { signal: options.signal } : {}) });
|
|
238
|
+
options.notify('NVIDIA: free DeepSeek V4.1 Flash API access connected through Squire');
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
unavailable.push('nvidia');
|
|
242
|
+
options.notify('NVIDIA setup incomplete. Rerun usegrunt setup to resume; other routes remain available.');
|
|
243
|
+
}
|
|
199
244
|
}
|
|
200
245
|
};
|
|
201
|
-
const adapters = options.adapters ?? ['freebuff', 'opencode'];
|
|
202
246
|
await parallelMap(adapters, async (id) => {
|
|
247
|
+
if (requiresDataSharing(id) && !approved.includes(id)) {
|
|
248
|
+
unavailable.push(id);
|
|
249
|
+
options.notify(`${id}: skipped until the grouped data-sharing approval is granted`);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
203
252
|
const task = tasks[id];
|
|
204
253
|
if (!task)
|
|
205
254
|
throw new Error('Unsupported vetted adapter');
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { open } from 'node:fs/promises';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { SourceError } from './source.js';
|
|
6
|
+
// Probe-only providers: these are not enabled by the published adapter registry.
|
|
7
|
+
export const FREE_API_PROVIDERS = {
|
|
8
|
+
zro: { model: 'deepseek-v4.1-flash', prefix: '/v1' },
|
|
9
|
+
amd: { model: 'DeepSeek-V4.1-Flash', prefix: '/radeon/api/v1' },
|
|
10
|
+
tokenharbor: { model: 'deepseek-v4.1-flash:free', prefix: '/v1' }
|
|
11
|
+
};
|
|
12
|
+
// Official API metadata maps deepseek-flash to DeepSeek-V4.1-Flash:
|
|
13
|
+
// https://api-docs.deepseek.com/api/list-models/ . Other aliases below are the
|
|
14
|
+
// explicit V4.1 IDs used by these providers, not loose family-name matches.
|
|
15
|
+
const deepSeekV41Aliases = new Set(['deepseek-flash', 'deepseek-v4.1-flash', 'deepseek-v4.1-flash-1017',
|
|
16
|
+
'deepseek-v4.1-flash-free', 'deepseek-v4.1-flash:free']);
|
|
17
|
+
export function isDeepSeekV41Model(value) {
|
|
18
|
+
return typeof value === 'string' && deepSeekV41Aliases.has(value.toLowerCase().replace(/^deepseek\//, ''));
|
|
19
|
+
}
|
|
20
|
+
const directory = () => process.env.MILO_CONFIG_DIR || join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'milo');
|
|
21
|
+
function providerName(value) {
|
|
22
|
+
if (!Object.hasOwn(FREE_API_PROVIDERS, value))
|
|
23
|
+
throw new Error('Unsupported free API provider');
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
function validateGrant(value) {
|
|
27
|
+
const grant = value;
|
|
28
|
+
if (!grant || typeof grant.base_url !== 'string' || typeof grant.token !== 'string' || !grant.token.trim())
|
|
29
|
+
throw new Error('Invalid free API grant');
|
|
30
|
+
let url;
|
|
31
|
+
try {
|
|
32
|
+
url = new URL(grant.base_url);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
throw new Error('Invalid free API grant endpoint');
|
|
36
|
+
}
|
|
37
|
+
if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash)
|
|
38
|
+
throw new Error('Invalid free API grant endpoint');
|
|
39
|
+
return { base_url: grant.base_url.replace(/\/$/, ''), token: grant.token };
|
|
40
|
+
}
|
|
41
|
+
export async function readFreeApiGrant(provider, location = directory()) {
|
|
42
|
+
const name = providerName(provider);
|
|
43
|
+
const file = await open(join(location, `${name}.json`), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
44
|
+
try {
|
|
45
|
+
const stat = await file.stat();
|
|
46
|
+
if (!stat.isFile() || (stat.mode & 0o777) !== 0o600 || stat.uid !== process.getuid?.())
|
|
47
|
+
throw new Error('Unsafe free API grant file');
|
|
48
|
+
return validateGrant(JSON.parse(await file.readFile('utf8')));
|
|
49
|
+
}
|
|
50
|
+
finally {
|
|
51
|
+
await file.close();
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const tokens = (value) => typeof value === 'number' && Number.isFinite(value) && value >= 0 ? value : 0;
|
|
55
|
+
const validCalls = (value) => Array.isArray(value) && value.every(call => call && typeof call.id === 'string'
|
|
56
|
+
&& call.type === 'function' && typeof call.function?.name === 'string' && typeof call.function?.arguments === 'string');
|
|
57
|
+
export class FreeApi {
|
|
58
|
+
options;
|
|
59
|
+
id;
|
|
60
|
+
supportsFunctionTools = true;
|
|
61
|
+
shutdown = new AbortController();
|
|
62
|
+
constructor(provider, options = {}) {
|
|
63
|
+
this.options = options;
|
|
64
|
+
this.id = providerName(provider);
|
|
65
|
+
}
|
|
66
|
+
get spec() { return FREE_API_PROVIDERS[this.id]; }
|
|
67
|
+
grant() { return (this.options.loadGrant?.() ?? readFreeApiGrant(this.id, this.options.directory)).then(validateGrant); }
|
|
68
|
+
// Availability here means private access is configured. Only the live probe
|
|
69
|
+
// establishes whether the provider will serve the requested free model.
|
|
70
|
+
async available() { try {
|
|
71
|
+
await this.grant();
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return false;
|
|
76
|
+
} }
|
|
77
|
+
async complete(request, caller) {
|
|
78
|
+
caller.throwIfAborted();
|
|
79
|
+
if (![this.id, 'milo'].includes(request.model))
|
|
80
|
+
throw new SourceError('upstream', 'Free API probe requires an explicit provider');
|
|
81
|
+
if (request.tools.some(tool => tool.type !== 'function'))
|
|
82
|
+
throw new SourceError('upstream', 'Free API probe requires function tools');
|
|
83
|
+
let grant;
|
|
84
|
+
try {
|
|
85
|
+
grant = await this.grant();
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
throw new SourceError('logged_out', `${this.id} access is not configured`);
|
|
89
|
+
}
|
|
90
|
+
const signal = AbortSignal.any([caller, this.shutdown.signal, AbortSignal.timeout(this.options.timeout ?? 180000)]);
|
|
91
|
+
let response;
|
|
92
|
+
try {
|
|
93
|
+
response = await (this.options.fetcher ?? fetch)(`${grant.base_url}${this.spec.prefix}/chat/completions`, {
|
|
94
|
+
method: 'POST', redirect: 'error', signal,
|
|
95
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${grant.token}` },
|
|
96
|
+
body: JSON.stringify({ model: this.spec.model, messages: request.messages,
|
|
97
|
+
tools: request.tools.length ? request.tools : undefined, stream: false,
|
|
98
|
+
max_tokens: Math.min(4096, Math.max(1, request.maxTokens ?? 4096)) })
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
caller.throwIfAborted();
|
|
103
|
+
if (this.shutdown.signal.aborted)
|
|
104
|
+
throw new SourceError('cancelled', 'Free API probe closed');
|
|
105
|
+
throw new SourceError(signal.aborted ? 'timeout' : 'upstream', `${this.id} endpoint is unavailable`);
|
|
106
|
+
}
|
|
107
|
+
if (!response.ok) {
|
|
108
|
+
const raw = response.headers.get('retry-after');
|
|
109
|
+
const retry = raw === null ? NaN : /^\d+(?:\.\d+)?$/.test(raw) ? Number(raw) : (Date.parse(raw) - Date.now()) / 1000;
|
|
110
|
+
await response.body?.cancel();
|
|
111
|
+
throw new SourceError(response.status === 401 ? 'logged_out' : [402, 429].includes(response.status) ? 'quota' : response.status === 403 ? 'refused' : 'upstream', `${this.id} returned HTTP ${response.status}`, Number.isFinite(retry) ? Math.max(1, retry) : undefined);
|
|
112
|
+
}
|
|
113
|
+
try {
|
|
114
|
+
const body = await response.json();
|
|
115
|
+
signal.throwIfAborted();
|
|
116
|
+
const message = body.choices?.[0]?.message;
|
|
117
|
+
if (message?.refusal)
|
|
118
|
+
throw new SourceError('refused', `${this.id} refused the request`);
|
|
119
|
+
const calls = message?.tool_calls;
|
|
120
|
+
if (!message || (typeof message.content !== 'string' && !(validCalls(calls) && calls.length)) || (calls !== undefined && !validCalls(calls)))
|
|
121
|
+
throw new SourceError('upstream', `${this.id} returned an invalid completion`);
|
|
122
|
+
return { text: typeof message.content === 'string' ? message.content : '', ...(validCalls(calls) && calls.length ? { calls } : {}),
|
|
123
|
+
inputTokens: tokens(body.usage?.prompt_tokens), outputTokens: tokens(body.usage?.completion_tokens) };
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
caller.throwIfAborted();
|
|
127
|
+
if (this.shutdown.signal.aborted)
|
|
128
|
+
throw new SourceError('cancelled', 'Free API probe closed');
|
|
129
|
+
if (signal.aborted)
|
|
130
|
+
throw new SourceError('timeout', `${this.id} endpoint is unavailable`);
|
|
131
|
+
if (error instanceof SourceError)
|
|
132
|
+
throw error;
|
|
133
|
+
throw new SourceError('upstream', `${this.id} returned an invalid completion`);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
async close() { this.shutdown.abort(); }
|
|
137
|
+
}
|
package/dist/local/main.js
CHANGED
|
@@ -8,8 +8,15 @@ import { createLocalApp } from './api.js';
|
|
|
8
8
|
import { Freebuff, positive } from './freebuff.js';
|
|
9
9
|
import { Sources } from './source.js';
|
|
10
10
|
import { FreeLLMAPI } from './freellmapi.js';
|
|
11
|
+
import { Nvidia } from './nvidia.js';
|
|
12
|
+
import { Zro } from './zro.js';
|
|
13
|
+
import { TokenHarbor } from './tokenharbor.js';
|
|
14
|
+
import { RegistrySource } from './registry-source.js';
|
|
15
|
+
import { ManagedRouter } from './managed-router.js';
|
|
16
|
+
import { ConsentSource } from './consent-source.js';
|
|
17
|
+
import { readDataSharingConsent, requiresDataSharing } from './data-sharing.js';
|
|
11
18
|
import { Codex } from './codex.js';
|
|
12
|
-
import { OpenCode } from './opencode.js';
|
|
19
|
+
import { OpenCode, DEFAULT_OPENCODE_MODELS } from './opencode.js';
|
|
13
20
|
import { Dots } from './dots.js';
|
|
14
21
|
import { bundledRegistry } from '../registry.js';
|
|
15
22
|
import { RegistrySync } from './registry.js';
|
|
@@ -32,11 +39,24 @@ export function configure(env = process.env, registry = () => bundledRegistry) {
|
|
|
32
39
|
};
|
|
33
40
|
const connection = { url, loadKey };
|
|
34
41
|
const dots = new Dots({ env, timeout: positive(env.MILO_DOTS_TIMEOUT_MS, 300000) });
|
|
35
|
-
const
|
|
36
|
-
|
|
42
|
+
const opencodeModels = () => {
|
|
43
|
+
if (env.MILO_OPENCODE_MODEL)
|
|
44
|
+
return [env.MILO_OPENCODE_MODEL];
|
|
45
|
+
const adapter = registry().adapters.find(a => a.id === 'opencode');
|
|
46
|
+
// Older published registries pinned the original default. Upgrade that
|
|
47
|
+
// default while retaining explicit environment and custom-model overrides.
|
|
48
|
+
return adapter?.models ?? (adapter?.model && adapter.model !== 'big-pickle' ? [adapter.model] : DEFAULT_OPENCODE_MODELS);
|
|
49
|
+
};
|
|
50
|
+
const registered = [dots, new Codex(env.MILO_CODEX_MODEL ? { model: env.MILO_CODEX_MODEL } : {}), new ManagedRouter(new FreeLLMAPI(connection), url, configDir(env)), new FreeLLMAPI({ ...connection, id: 'kilo', model: 'kilo-auto/free' }), new Nvidia({ directory: configDir(env) }), new Zro({ directory: configDir(env) }), new TokenHarbor({ directory: configDir(env) }), new OpenCode({ models: opencodeModels, ...(env.MILO_OPENCODE_URL ? { url: env.MILO_OPENCODE_URL } : {}) }), new Freebuff({ toolBridge: env.MILO_FREEBUFF_JSON_BRIDGE === '1' })];
|
|
51
|
+
const ids = env.MILO_SOURCES?.split(',').map(id => id.trim()) ?? registry().adapters.filter(adapter => knownAdapters.has(adapter.id)).map(adapter => adapter.id);
|
|
37
52
|
const routing = ids.every(id => knownAdapters.has(id)) ? { registry } : undefined;
|
|
38
|
-
return new Sources(ids.map(id => {
|
|
39
|
-
|
|
53
|
+
return new Sources(ids.map(id => {
|
|
54
|
+
const source = registered.find(source => source.id === id);
|
|
55
|
+
if (!source)
|
|
56
|
+
throw new Error(`Unknown grunt source: ${id}`);
|
|
57
|
+
const consent = requiresDataSharing(id) && id !== 'tokenharbor' ? new ConsentSource(source, configDir(env)) : source;
|
|
58
|
+
return !routing && knownAdapters.has(id) ? new RegistrySource(consent, registry) : consent;
|
|
59
|
+
}), routing);
|
|
40
60
|
}
|
|
41
61
|
export function start(env = process.env) {
|
|
42
62
|
let registry = structuredClone(bundledRegistry);
|
|
@@ -49,10 +69,14 @@ export function start(env = process.env) {
|
|
|
49
69
|
ensure: async (adapter, signal) => {
|
|
50
70
|
if (!sources.sources.some(source => source.id === adapter.id))
|
|
51
71
|
return;
|
|
72
|
+
const approved = await readDataSharingConsent(directory, [adapter.id]);
|
|
73
|
+
if (requiresDataSharing(adapter.id) && !approved.includes(adapter.id))
|
|
74
|
+
return;
|
|
52
75
|
sources.preparing.add(adapter.id);
|
|
53
|
-
const client = squire ??= new BrowserSquire({ background: true, signal });
|
|
76
|
+
const client = squire ??= new BrowserSquire({ background: true, signal, dataSharingApproved: approved });
|
|
77
|
+
client.approveDataSharing([...new Set([...(await readDataSharingConsent(directory, [...knownAdapters])), ...approved])]);
|
|
54
78
|
try {
|
|
55
|
-
const result = await setupFallbacks({ directory, run: (binary, args) => command(binary, args, false, signal), notify: console.error, adapters: [adapter.id], squire: client, signal,
|
|
79
|
+
const result = await setupFallbacks({ directory, run: (binary, args) => command(binary, args, false, signal), notify: console.error, adapters: [adapter.id], squire: client, signal, dataSharingApproved: approved,
|
|
56
80
|
connect: () => client.connect() });
|
|
57
81
|
if (result.unavailable.length)
|
|
58
82
|
throw new Error('Adapter setup incomplete');
|
|
@@ -70,14 +94,14 @@ export function start(env = process.env) {
|
|
|
70
94
|
sources.preparing.add(adapter.id); });
|
|
71
95
|
void ready.then(() => sync.start()).catch(() => console.error('Registry sync could not start; using the bundled list')).finally(() => sources.preparing.clear());
|
|
72
96
|
// Warm the saved login without delaying the server; failed loads retry lazily.
|
|
73
|
-
void sources.sources.find(source =>
|
|
97
|
+
void sources.sources.find(source => ['freellmapi', 'kilo'].includes(source.id))?.available();
|
|
74
98
|
const port = positive(env.MILO_PORT, 8788);
|
|
75
99
|
if (port > 65535)
|
|
76
100
|
throw new Error('MILO_PORT must be at most 65535');
|
|
77
101
|
const app = createLocalApp(sources, { port, dotsEnv: env, log: cwd => console.error(`grunt working directory: ${cwd}`) });
|
|
78
102
|
const server = serve({ fetch: async (request) => { await ready; return app.fetch(request); }, hostname: HOST, port }, () => console.error(`grunt model API: http://${HOST}:${port}/v1`));
|
|
79
103
|
// A distinct source list prevents FreeLLMAPI -> grunt -> FreeLLMAPI recursion.
|
|
80
|
-
const fallback = env.MILO_FREEBUFF_BRIDGE !== '0' && sources.sources.some(s =>
|
|
104
|
+
const fallback = env.MILO_FREEBUFF_BRIDGE !== '0' && sources.sources.some(s => ['freellmapi', 'kilo'].includes(s.id)) ? new Sources([new ConsentSource(new Freebuff({ toolBridge: env.MILO_FREEBUFF_JSON_BRIDGE === '1' }), directory)]) : undefined;
|
|
81
105
|
const bridge = fallback ? serve({ fetch: createLocalApp(fallback, { port: 8789 }).fetch, hostname: HOST, port: 8789 }) : undefined;
|
|
82
106
|
let closing = false;
|
|
83
107
|
const close = async () => { if (closing)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { guardRouterSharing } from './setup.js';
|
|
2
|
+
import { SourceError } from './source.js';
|
|
3
|
+
// The legacy backend can contain Grunt-created sharing providers alongside the
|
|
4
|
+
// user's custom rows. Enforce current consent without disabling custom sources.
|
|
5
|
+
export class ManagedRouter {
|
|
6
|
+
source;
|
|
7
|
+
url;
|
|
8
|
+
directory;
|
|
9
|
+
id;
|
|
10
|
+
supportsFunctionTools;
|
|
11
|
+
constructor(source, url, directory) {
|
|
12
|
+
this.source = source;
|
|
13
|
+
this.url = url;
|
|
14
|
+
this.directory = directory;
|
|
15
|
+
this.id = source.id;
|
|
16
|
+
if (source.supportsFunctionTools !== undefined)
|
|
17
|
+
this.supportsFunctionTools = source.supportsFunctionTools;
|
|
18
|
+
}
|
|
19
|
+
available() { return this.source.available(); }
|
|
20
|
+
async complete(request, signal) {
|
|
21
|
+
signal.throwIfAborted();
|
|
22
|
+
try {
|
|
23
|
+
await guardRouterSharing(this.url, this.directory);
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
throw new SourceError('refused', 'Cannot enforce saved router data-sharing choices; rerun usegrunt setup');
|
|
27
|
+
}
|
|
28
|
+
signal.throwIfAborted();
|
|
29
|
+
return this.source.complete(request, signal);
|
|
30
|
+
}
|
|
31
|
+
async close() { await this.source.close?.(); }
|
|
32
|
+
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { mkdir, open, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { homedir } from 'node:os';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { randomUUID } from 'node:crypto';
|
|
6
|
+
import { SourceError } from './source.js';
|
|
7
|
+
export const NVIDIA_DEEPSEEK = 'deepseek-ai/deepseek-v4.1-flash';
|
|
8
|
+
const configDirectory = () => process.env.MILO_CONFIG_DIR || join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'milo');
|
|
9
|
+
function validateGrant(value) {
|
|
10
|
+
const grant = value;
|
|
11
|
+
if (!grant || typeof grant.base_url !== 'string' || typeof grant.token !== 'string' || !grant.token.trim())
|
|
12
|
+
throw new Error('Invalid NVIDIA Squire grant');
|
|
13
|
+
const url = new URL(grant.base_url);
|
|
14
|
+
if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash)
|
|
15
|
+
throw new Error('Invalid NVIDIA Squire endpoint');
|
|
16
|
+
return { base_url: grant.base_url.replace(/\/$/, ''), token: grant.token };
|
|
17
|
+
}
|
|
18
|
+
export async function readNvidiaGrant(directory = configDirectory()) {
|
|
19
|
+
const file = await open(join(directory, 'nvidia.json'), constants.O_RDONLY | constants.O_NOFOLLOW);
|
|
20
|
+
try {
|
|
21
|
+
const stat = await file.stat();
|
|
22
|
+
if (!stat.isFile() || (stat.mode & 0o777) !== 0o600 || stat.uid !== process.getuid?.())
|
|
23
|
+
throw new Error('Unsafe NVIDIA grant file');
|
|
24
|
+
return validateGrant(JSON.parse(await file.readFile('utf8')));
|
|
25
|
+
}
|
|
26
|
+
finally {
|
|
27
|
+
await file.close();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// Store only the revocable, host-scoped Squire grant, never the NVIDIA API key.
|
|
31
|
+
export async function writeNvidiaGrant(grant, directory = configDirectory()) {
|
|
32
|
+
const value = validateGrant(grant), temporary = join(directory, `nvidia.${randomUUID()}.tmp`);
|
|
33
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
34
|
+
try {
|
|
35
|
+
await writeFile(temporary, JSON.stringify(value), { mode: 0o600, flag: 'wx' });
|
|
36
|
+
await rename(temporary, join(directory, 'nvidia.json'));
|
|
37
|
+
}
|
|
38
|
+
finally {
|
|
39
|
+
await rm(temporary, { force: true });
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
export class Nvidia {
|
|
43
|
+
options;
|
|
44
|
+
id = 'nvidia';
|
|
45
|
+
supportsFunctionTools = true;
|
|
46
|
+
shutdown = new AbortController();
|
|
47
|
+
constructor(options = {}) {
|
|
48
|
+
this.options = options;
|
|
49
|
+
}
|
|
50
|
+
get fetcher() { return this.options.fetcher ?? fetch; }
|
|
51
|
+
async grant() { return validateGrant(await (this.options.loadGrant?.() ?? readNvidiaGrant(this.options.directory))); }
|
|
52
|
+
async available() {
|
|
53
|
+
try {
|
|
54
|
+
const grant = await this.grant();
|
|
55
|
+
const response = await this.fetcher(`${grant.base_url}/v1/models`, { headers: { Authorization: `Bearer ${grant.token}` }, redirect: 'error', signal: AbortSignal.timeout(5000) });
|
|
56
|
+
await response.body?.cancel();
|
|
57
|
+
return response.ok;
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async healthCheck() { return this.available(); }
|
|
64
|
+
async complete(request, caller) {
|
|
65
|
+
caller.throwIfAborted();
|
|
66
|
+
let grant;
|
|
67
|
+
try {
|
|
68
|
+
grant = await this.grant();
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
throw new SourceError('logged_out', 'NVIDIA access is unavailable; rerun usegrunt setup');
|
|
72
|
+
}
|
|
73
|
+
if (request.tools.some(tool => tool.type !== 'function'))
|
|
74
|
+
throw new SourceError('upstream', 'NVIDIA Chat Completions requires function tools');
|
|
75
|
+
const signal = AbortSignal.any([caller, this.shutdown.signal, AbortSignal.timeout(this.options.timeout ?? 120000)]);
|
|
76
|
+
let response;
|
|
77
|
+
try {
|
|
78
|
+
// This grant is bound to integrate.api.nvidia.com. There is no local
|
|
79
|
+
// aggregator or secondary provider that could answer or spend paid quota.
|
|
80
|
+
response = await this.fetcher(`${grant.base_url}/v1/chat/completions`, {
|
|
81
|
+
method: 'POST', redirect: 'error', signal,
|
|
82
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${grant.token}` },
|
|
83
|
+
body: JSON.stringify({ model: NVIDIA_DEEPSEEK, messages: request.messages, tools: request.tools.length ? request.tools : undefined,
|
|
84
|
+
stream: false, max_tokens: Math.min(4096, Math.max(1, request.maxTokens ?? 4096)) })
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
caller.throwIfAborted();
|
|
89
|
+
if (this.shutdown.signal.aborted)
|
|
90
|
+
throw new SourceError('cancelled', 'NVIDIA request cancelled during shutdown');
|
|
91
|
+
throw new SourceError(signal.aborted ? 'timeout' : 'upstream', 'NVIDIA endpoint is unavailable');
|
|
92
|
+
}
|
|
93
|
+
if (!response.ok) {
|
|
94
|
+
const header = response.headers.get('retry-after');
|
|
95
|
+
const seconds = header === null ? NaN : /^\d+(?:\.\d+)?$/.test(header) ? Number(header) : (Date.parse(header) - Date.now()) / 1000;
|
|
96
|
+
await response.body?.cancel();
|
|
97
|
+
throw new SourceError(response.status === 401 ? 'logged_out' : [402, 429].includes(response.status) ? 'quota' : response.status === 403 ? 'refused' : 'upstream', `NVIDIA returned HTTP ${response.status}`, Number.isFinite(seconds) ? Math.max(1, seconds) : undefined);
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
const result = await response.json();
|
|
101
|
+
const message = result.choices?.[0]?.message;
|
|
102
|
+
if (message?.refusal)
|
|
103
|
+
throw new SourceError('refused', 'NVIDIA refused the request');
|
|
104
|
+
if (!message || (typeof message.content !== 'string' && !message.tool_calls?.length))
|
|
105
|
+
throw new SourceError('upstream', 'NVIDIA returned an invalid completion');
|
|
106
|
+
return { text: message.content ?? '', ...(message.tool_calls ? { calls: message.tool_calls } : {}), inputTokens: result.usage?.prompt_tokens ?? 0, outputTokens: result.usage?.completion_tokens ?? 0 };
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
caller.throwIfAborted();
|
|
110
|
+
if (error instanceof SourceError)
|
|
111
|
+
throw error;
|
|
112
|
+
throw new SourceError('upstream', 'NVIDIA returned an invalid completion');
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
async close() { this.shutdown.abort(); }
|
|
116
|
+
}
|