usegrunt 0.6.5 → 0.6.7
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 +20 -7
- package/dist/local/beatapi-setup.js +83 -0
- package/dist/local/beatapi.js +298 -0
- package/dist/local/clixad-preload.js +177 -0
- package/dist/local/clixad-setup.js +345 -0
- package/dist/local/clixad.js +398 -0
- package/dist/local/data-sharing.js +18 -2
- package/dist/local/fallback-setup.js +112 -33
- package/dist/local/freebuff.js +5 -1
- package/dist/local/freellmapi.js +3 -5
- package/dist/local/kilo.js +209 -0
- package/dist/local/main.js +62 -20
- package/dist/local/managed-router.js +4 -2
- package/dist/local/opencode.js +38 -18
- package/dist/local/parallel.js +2 -0
- package/dist/local/perch-process.js +377 -0
- package/dist/local/perch-setup.js +297 -0
- package/dist/local/perch.js +480 -0
- package/dist/local/pollinations-setup.js +109 -0
- package/dist/local/pollinations.js +324 -0
- package/dist/local/registry.js +4 -4
- package/dist/local/routing.js +5 -1
- package/dist/local/setup-session.js +232 -0
- package/dist/local/setup.js +343 -62
- package/dist/local/source.js +34 -4
- package/dist/local/wizard.js +283 -109
- package/dist/registry/adapters.json +57 -33
- package/dist/registry.js +4 -2
- package/package.json +1 -1
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { mkdir, open, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { homedir } from 'node:os';
|
|
5
|
+
import { join } from 'node:path';
|
|
6
|
+
import { Ajv } from 'ajv';
|
|
7
|
+
import { FreebuffTools } from './freebuff-tools.js';
|
|
8
|
+
import { readDataSharingConsent } from './data-sharing.js';
|
|
9
|
+
import { SourceError } from './source.js';
|
|
10
|
+
export const POLLINATIONS_MODEL = 'community/morriszdweck/glm-5.3-flash-free';
|
|
11
|
+
// An explicit one-item candidate list replaces provider-configured fallbacks.
|
|
12
|
+
// The trailing separator is intentional: its empty second item is discarded.
|
|
13
|
+
export const POLLINATIONS_WIRE_MODEL = `${POLLINATIONS_MODEL},`;
|
|
14
|
+
export const POLLINATIONS_RETURNED_MODEL = 'poe/GLM-5.3-Flash-T';
|
|
15
|
+
export const POLLINATIONS_ORIGIN = 'https://gen.pollinations.ai';
|
|
16
|
+
export const POLLINATIONS_GRANT_ORIGIN = 'https://trusty-squire-api.fly.dev';
|
|
17
|
+
export const POLLINATIONS_RECEIPT_MAX_AGE_MS = 30 * 86400000;
|
|
18
|
+
const directory = () => process.env.MILO_CONFIG_DIR || join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'milo');
|
|
19
|
+
export const pollinationsGrantPath = (location = directory()) => join(location, 'pollinations.json');
|
|
20
|
+
const object = (value) => value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
21
|
+
const rejected = () => new SourceError('logged_out', 'Pollinations private free-access verification is missing or expired; rerun usegrunt setup');
|
|
22
|
+
function accessGrant(value) {
|
|
23
|
+
const grant = object(value);
|
|
24
|
+
if (typeof grant.base_url !== 'string' || typeof grant.token !== 'string' || !/^[\x21-\x7e]{1,16384}$/.test(grant.token))
|
|
25
|
+
throw rejected();
|
|
26
|
+
let url;
|
|
27
|
+
try {
|
|
28
|
+
url = new URL(grant.base_url);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
throw rejected();
|
|
32
|
+
}
|
|
33
|
+
if (url.origin !== POLLINATIONS_GRANT_ORIGIN || url.username || url.password || url.search || url.hash || /[?#]/.test(grant.base_url)
|
|
34
|
+
|| !/^\/v1\/egress\/g_[A-Za-z0-9_-]{16,64}\/?$/.test(url.pathname))
|
|
35
|
+
throw rejected();
|
|
36
|
+
return { base_url: url.href.replace(/\/$/, ''), token: grant.token };
|
|
37
|
+
}
|
|
38
|
+
function validateGrant(value) {
|
|
39
|
+
const grant = object(value), access = accessGrant(value);
|
|
40
|
+
const verified = typeof grant.verified_at === 'string' ? Date.parse(grant.verified_at) : NaN;
|
|
41
|
+
if (grant.version !== 1 || grant.upstream_origin !== POLLINATIONS_ORIGIN || grant.model !== POLLINATIONS_MODEL
|
|
42
|
+
|| grant.free_account_verified !== true || grant.key_zero_budget_verified !== true || grant.quest_pollen_only !== true
|
|
43
|
+
|| !Number.isFinite(verified) || verified > Date.now() + 60000 || Date.now() - verified > POLLINATIONS_RECEIPT_MAX_AGE_MS)
|
|
44
|
+
throw rejected();
|
|
45
|
+
return { ...access, version: 1, upstream_origin: POLLINATIONS_ORIGIN, model: POLLINATIONS_MODEL, verified_at: grant.verified_at,
|
|
46
|
+
free_account_verified: true, key_zero_budget_verified: true, quest_pollen_only: true };
|
|
47
|
+
}
|
|
48
|
+
export async function readPollinationsGrant(location = directory()) {
|
|
49
|
+
let file;
|
|
50
|
+
try {
|
|
51
|
+
file = await open(pollinationsGrantPath(location), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
52
|
+
const stat = await file.stat();
|
|
53
|
+
if (!stat.isFile() || stat.uid !== process.getuid?.() || (stat.mode & 0o777) !== 0o600 || stat.size > 32768)
|
|
54
|
+
throw rejected();
|
|
55
|
+
return validateGrant(JSON.parse(await file.readFile('utf8')));
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
throw rejected();
|
|
59
|
+
}
|
|
60
|
+
finally {
|
|
61
|
+
await file?.close();
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// Setup supplies these facts only after ordinary browser verification and a
|
|
65
|
+
// successful read-only access check. No raw provider key is persisted by Grunt.
|
|
66
|
+
export async function writePollinationsGrant(grant, location = directory()) {
|
|
67
|
+
const value = validateGrant(grant), temporary = join(location, `.pollinations-${randomUUID()}.tmp`);
|
|
68
|
+
await mkdir(location, { recursive: true, mode: 0o700 });
|
|
69
|
+
try {
|
|
70
|
+
await writeFile(temporary, JSON.stringify(value), { flag: 'wx', mode: 0o600 });
|
|
71
|
+
await rename(temporary, pollinationsGrantPath(location));
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
await rm(temporary, { force: true });
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async function readJSON(response, signal, limit) {
|
|
78
|
+
if (signal.aborted) {
|
|
79
|
+
void response.body?.cancel().catch(() => { });
|
|
80
|
+
signal.throwIfAborted();
|
|
81
|
+
}
|
|
82
|
+
if (Number(response.headers.get('content-length')) > limit || !response.body) {
|
|
83
|
+
void response.body?.cancel().catch(() => { });
|
|
84
|
+
throw new SourceError('upstream', 'Pollinations returned an unsupported response');
|
|
85
|
+
}
|
|
86
|
+
const reader = response.body.getReader(), chunks = [];
|
|
87
|
+
let size = 0;
|
|
88
|
+
try {
|
|
89
|
+
for (;;) {
|
|
90
|
+
signal.throwIfAborted();
|
|
91
|
+
let abort;
|
|
92
|
+
const cancellation = new Promise((_resolve, reject) => {
|
|
93
|
+
abort = () => { void reader.cancel().catch(() => { }); reject(signal.reason); };
|
|
94
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
95
|
+
if (signal.aborted)
|
|
96
|
+
abort();
|
|
97
|
+
});
|
|
98
|
+
let part;
|
|
99
|
+
try {
|
|
100
|
+
part = await Promise.race([reader.read(), cancellation]);
|
|
101
|
+
}
|
|
102
|
+
finally {
|
|
103
|
+
if (abort)
|
|
104
|
+
signal.removeEventListener('abort', abort);
|
|
105
|
+
}
|
|
106
|
+
if (part.done)
|
|
107
|
+
break;
|
|
108
|
+
size += part.value.byteLength;
|
|
109
|
+
if (size > limit)
|
|
110
|
+
throw new SourceError('upstream', 'Pollinations response exceeded the supported size');
|
|
111
|
+
chunks.push(part.value);
|
|
112
|
+
}
|
|
113
|
+
signal.throwIfAborted();
|
|
114
|
+
return JSON.parse(Buffer.concat(chunks).toString('utf8'));
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
void reader.cancel().catch(() => { });
|
|
118
|
+
try {
|
|
119
|
+
reader.releaseLock();
|
|
120
|
+
}
|
|
121
|
+
catch { /* An aborted reader may still be closing. */ }
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function status(response) {
|
|
125
|
+
if (response.ok)
|
|
126
|
+
return;
|
|
127
|
+
const raw = response.headers.get('retry-after');
|
|
128
|
+
const seconds = raw === null ? NaN : /^\d+(?:\.\d+)?$/.test(raw) ? Number(raw) : (Date.parse(raw) - Date.now()) / 1000;
|
|
129
|
+
void response.body?.cancel().catch(() => { });
|
|
130
|
+
throw new SourceError(response.status === 401 ? 'logged_out' : [402, 429].includes(response.status) ? 'quota' : response.status === 403 ? 'refused' : 'upstream', `Pollinations returned HTTP ${response.status}`, Number.isFinite(seconds) ? Math.max(1, seconds) : undefined);
|
|
131
|
+
}
|
|
132
|
+
// Nonbillable metadata only. The authoritative server's zero-budget rule
|
|
133
|
+
// rejects every non-free model even when account funds exist. Quest-only keys
|
|
134
|
+
// cannot consume purchased Pollen. Recheck before each actual inference.
|
|
135
|
+
export async function verifyPollinationsAccess(grant, options = {}) {
|
|
136
|
+
const access = accessGrant(grant), deadline = AbortSignal.timeout(5000);
|
|
137
|
+
const signal = options.signal ? AbortSignal.any([options.signal, deadline]) : deadline;
|
|
138
|
+
const controller = new AbortController(), combined = AbortSignal.any([signal, controller.signal]);
|
|
139
|
+
try {
|
|
140
|
+
const get = async (path) => {
|
|
141
|
+
combined.throwIfAborted();
|
|
142
|
+
const response = await (options.fetcher ?? fetch)(`${access.base_url}${path}`, {
|
|
143
|
+
headers: { Authorization: `Bearer ${access.token}` }, redirect: 'error', signal: combined
|
|
144
|
+
});
|
|
145
|
+
status(response);
|
|
146
|
+
return object(await readJSON(response, combined, 131072));
|
|
147
|
+
};
|
|
148
|
+
const pending = [get('/account/key'), get('/account/balance'), get(`/v1/models/${POLLINATIONS_MODEL}`)];
|
|
149
|
+
let key, balance, catalog;
|
|
150
|
+
try {
|
|
151
|
+
[key, balance, catalog] = await Promise.all(pending);
|
|
152
|
+
}
|
|
153
|
+
catch (error) {
|
|
154
|
+
controller.abort();
|
|
155
|
+
await Promise.allSettled(pending);
|
|
156
|
+
throw error;
|
|
157
|
+
}
|
|
158
|
+
combined.throwIfAborted();
|
|
159
|
+
const permissions = object(key.permissions), models = permissions.models, account = permissions.account;
|
|
160
|
+
const expires = key.expiresAt;
|
|
161
|
+
if (key.valid !== true || key.type !== 'secret' || key.pollenBudget !== 0 || key.questPollenOnly !== true
|
|
162
|
+
|| !(models === null || (Array.isArray(models) && models.length === 1 && models[0] === POLLINATIONS_MODEL))
|
|
163
|
+
|| !Array.isArray(account) || account.length !== 1 || account[0] !== 'usage'
|
|
164
|
+
|| !(expires === null || (typeof expires === 'string' && Number.isFinite(Date.parse(expires)) && Date.parse(expires) > Date.now()))) {
|
|
165
|
+
throw new SourceError('refused', 'Pollinations requires a valid zero-budget, Quest-only, minimally scoped free key');
|
|
166
|
+
}
|
|
167
|
+
const funds = object(balance.accountBalance);
|
|
168
|
+
if (balance.balance !== 0 || funds.paid !== 0 || typeof funds.tier !== 'number' || !Number.isFinite(funds.tier) || funds.tier <= 0) {
|
|
169
|
+
throw new SourceError('quota', 'Pollinations free Quest allowance is unavailable');
|
|
170
|
+
}
|
|
171
|
+
const pricing = object(catalog.pricing);
|
|
172
|
+
// This vetted registry exports currency alone for a zero-rate model. New
|
|
173
|
+
// price or billing fields must be reviewed rather than assumed harmless.
|
|
174
|
+
if (catalog.id !== POLLINATIONS_MODEL || catalog.object !== 'model' || catalog.owned_by !== 'OSAII'
|
|
175
|
+
|| catalog.category !== 'text' || catalog.community !== true || catalog.paid_only !== false
|
|
176
|
+
|| pricing.currency !== 'pollen' || Object.keys(pricing).length !== 1
|
|
177
|
+
|| ['billing', 'billing_rules', 'billingRules', 'costVariants', 'rates', 'rateSheets'].some(field => Object.hasOwn(catalog, field))
|
|
178
|
+
|| !Array.isArray(catalog.supported_endpoints) || !catalog.supported_endpoints.includes('/v1/chat/completions')) {
|
|
179
|
+
throw new SourceError('refused', 'Pollinations vetted community route is no longer verified as zero priced');
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
catch (error) {
|
|
183
|
+
options.signal?.throwIfAborted();
|
|
184
|
+
if (signal.aborted)
|
|
185
|
+
throw new SourceError('timeout', 'Pollinations free-access check timed out');
|
|
186
|
+
if (error instanceof SourceError)
|
|
187
|
+
throw error;
|
|
188
|
+
throw new SourceError('upstream', 'Pollinations free-access check failed');
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
function requestBridge(request) {
|
|
192
|
+
try {
|
|
193
|
+
const ajv = new Ajv({ strict: true, strictTypes: false, strictTuples: false, validateFormats: true, addUsedSchema: false });
|
|
194
|
+
for (const tool of request.tools) {
|
|
195
|
+
if (tool.type !== 'function' || !object(tool.function).parameters)
|
|
196
|
+
throw new Error('Unsupported tool');
|
|
197
|
+
ajv.compile(object(tool.function).parameters);
|
|
198
|
+
}
|
|
199
|
+
// Responses represents simultaneous calls as adjacent assistant items.
|
|
200
|
+
// Group those call-only items before enforcing complete result correlation.
|
|
201
|
+
const messages = [];
|
|
202
|
+
for (const message of request.messages) {
|
|
203
|
+
const previous = messages.at(-1);
|
|
204
|
+
if (message.role === 'assistant' && message.content === '' && message.tool_calls?.length
|
|
205
|
+
&& previous?.role === 'assistant' && previous.content === '' && previous.tool_calls?.length) {
|
|
206
|
+
previous.tool_calls = [...previous.tool_calls, ...message.tool_calls];
|
|
207
|
+
}
|
|
208
|
+
else
|
|
209
|
+
messages.push({ ...message });
|
|
210
|
+
}
|
|
211
|
+
return new FreebuffTools({ ...request, messages });
|
|
212
|
+
}
|
|
213
|
+
catch {
|
|
214
|
+
throw new SourceError('refused', 'Pollinations rejected unsupported tool definitions or an invalid tool conversation');
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
export function stripPollinationsThinking(text) {
|
|
218
|
+
return text.replace(/^\s*<think>[\s\S]*?<\/think>\s*/, '');
|
|
219
|
+
}
|
|
220
|
+
export class Pollinations {
|
|
221
|
+
options;
|
|
222
|
+
id = 'pollinations';
|
|
223
|
+
supportsFunctionTools = true;
|
|
224
|
+
shutdown = new AbortController();
|
|
225
|
+
pending = new Set();
|
|
226
|
+
constructor(options = {}) {
|
|
227
|
+
this.options = options;
|
|
228
|
+
}
|
|
229
|
+
grant() { return (this.options.loadGrant?.() ?? readPollinationsGrant(this.options.directory)).then(validateGrant); }
|
|
230
|
+
async approved() { return (await readDataSharingConsent(this.options.directory ?? directory(), [this.id])).includes(this.id); }
|
|
231
|
+
async track(task) { this.pending.add(task); try {
|
|
232
|
+
return await task;
|
|
233
|
+
}
|
|
234
|
+
finally {
|
|
235
|
+
this.pending.delete(task);
|
|
236
|
+
} }
|
|
237
|
+
async available() {
|
|
238
|
+
return this.track((async () => {
|
|
239
|
+
try {
|
|
240
|
+
if (this.shutdown.signal.aborted || !await this.approved())
|
|
241
|
+
return false;
|
|
242
|
+
await verifyPollinationsAccess(await this.grant(), { fetcher: this.options.fetcher ?? fetch, signal: this.shutdown.signal });
|
|
243
|
+
return !this.shutdown.signal.aborted;
|
|
244
|
+
}
|
|
245
|
+
catch {
|
|
246
|
+
return false;
|
|
247
|
+
}
|
|
248
|
+
})());
|
|
249
|
+
}
|
|
250
|
+
async healthCheck() { return this.available(); }
|
|
251
|
+
async complete(request, caller) {
|
|
252
|
+
caller.throwIfAborted();
|
|
253
|
+
return this.track(this.completeOnce(request, caller));
|
|
254
|
+
}
|
|
255
|
+
async completeOnce(request, caller) {
|
|
256
|
+
const signal = AbortSignal.any([caller, this.shutdown.signal, AbortSignal.timeout(this.options.timeout ?? 180000)]);
|
|
257
|
+
try {
|
|
258
|
+
signal.throwIfAborted();
|
|
259
|
+
if (request.imageInput)
|
|
260
|
+
throw new SourceError('refused', 'Pollinations supports text inputs through this route');
|
|
261
|
+
const bridge = requestBridge(request);
|
|
262
|
+
if (!await this.approved())
|
|
263
|
+
throw new SourceError('refused', 'Pollinations data sharing is not approved; rerun usegrunt setup');
|
|
264
|
+
let inputTokens = 0, outputTokens = 0;
|
|
265
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
266
|
+
signal.throwIfAborted();
|
|
267
|
+
const grant = await this.grant();
|
|
268
|
+
await verifyPollinationsAccess(grant, { fetcher: this.options.fetcher ?? fetch, signal });
|
|
269
|
+
const response = await (this.options.fetcher ?? fetch)(`${grant.base_url}/v1/chat/completions`, {
|
|
270
|
+
method: 'POST', redirect: 'error', signal,
|
|
271
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${grant.token}` },
|
|
272
|
+
body: JSON.stringify({ model: POLLINATIONS_WIRE_MODEL, messages: [{ role: 'user', content: bridge.prompt(attempt > 0) }],
|
|
273
|
+
tools: [], stream: false, max_tokens: Math.min(4096, Math.max(1, Number.isFinite(request.maxTokens) ? Math.floor(request.maxTokens) : 4096)) })
|
|
274
|
+
});
|
|
275
|
+
status(response);
|
|
276
|
+
if (response.headers.get('x-model-requested') !== POLLINATIONS_MODEL || response.headers.get('x-model-used') !== POLLINATIONS_MODEL) {
|
|
277
|
+
void response.body?.cancel().catch(() => { });
|
|
278
|
+
throw new SourceError('upstream', 'Pollinations did not acknowledge the pinned community route');
|
|
279
|
+
}
|
|
280
|
+
const body = object(await readJSON(response, signal, 2 * 1024 * 1024));
|
|
281
|
+
signal.throwIfAborted();
|
|
282
|
+
if (body.model !== POLLINATIONS_RETURNED_MODEL)
|
|
283
|
+
throw new SourceError('upstream', 'Pollinations returned an unverified model identity');
|
|
284
|
+
const choices = body.choices, message = object(Array.isArray(choices) && choices.length === 1 ? object(choices[0]).message : undefined);
|
|
285
|
+
if (message.refusal)
|
|
286
|
+
throw new SourceError('refused', 'Pollinations refused the request');
|
|
287
|
+
if (message.function_call !== undefined || (message.tool_calls !== undefined && (!Array.isArray(message.tool_calls) || message.tool_calls.length))) {
|
|
288
|
+
throw new SourceError('refused', 'Pollinations attempted unsupported native API tools');
|
|
289
|
+
}
|
|
290
|
+
const usage = object(body.usage);
|
|
291
|
+
if (typeof message.content !== 'string' || !Number.isSafeInteger(usage.prompt_tokens) || Number(usage.prompt_tokens) < 0
|
|
292
|
+
|| !Number.isSafeInteger(usage.completion_tokens) || Number(usage.completion_tokens) < 0) {
|
|
293
|
+
throw new SourceError('upstream', 'Pollinations returned an invalid completion or usage');
|
|
294
|
+
}
|
|
295
|
+
inputTokens += usage.prompt_tokens;
|
|
296
|
+
outputTokens += usage.completion_tokens;
|
|
297
|
+
if (!Number.isSafeInteger(inputTokens) || !Number.isSafeInteger(outputTokens))
|
|
298
|
+
throw new SourceError('upstream', 'Pollinations returned invalid usage');
|
|
299
|
+
try {
|
|
300
|
+
const result = bridge.parse(stripPollinationsThinking(message.content));
|
|
301
|
+
if (!result.text.trim() && !result.calls?.length)
|
|
302
|
+
throw new Error('Empty answer');
|
|
303
|
+
return { ...result, inputTokens, outputTokens };
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
if (attempt === 1)
|
|
307
|
+
throw new SourceError('upstream', 'Pollinations tool response failed validation after one repair attempt');
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
throw new SourceError('upstream', 'Pollinations tool protocol failed');
|
|
311
|
+
}
|
|
312
|
+
catch (error) {
|
|
313
|
+
caller.throwIfAborted();
|
|
314
|
+
if (this.shutdown.signal.aborted)
|
|
315
|
+
throw new SourceError('cancelled', 'Pollinations request cancelled during shutdown');
|
|
316
|
+
if (signal.aborted)
|
|
317
|
+
throw new SourceError('timeout', 'Pollinations request timed out');
|
|
318
|
+
if (error instanceof SourceError)
|
|
319
|
+
throw error;
|
|
320
|
+
throw new SourceError('upstream', 'Pollinations request failed');
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
async close() { this.shutdown.abort(); await Promise.allSettled([...this.pending]); }
|
|
324
|
+
}
|
package/dist/local/registry.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { mkdir, readFile, writeFile, rename, rm } from 'node:fs/promises';
|
|
2
2
|
import { join } from 'node:path';
|
|
3
3
|
import { randomUUID } from 'node:crypto';
|
|
4
|
-
import { bundledRegistry, knownAdapters, validateRegistry } from '../registry.js';
|
|
5
|
-
import { parallelMap } from './parallel.js';
|
|
4
|
+
import { bundledRegistry, disabledAdapters, knownAdapters, validateRegistry } from '../registry.js';
|
|
5
|
+
import { parallelMap, SETUP_CONCURRENCY } from './parallel.js';
|
|
6
6
|
export const REGISTRY_INTERVAL = 24 * 60 * 60 * 1000;
|
|
7
7
|
const REGISTRY_URLS = ['https://api.grunt.run/v1/registry', 'https://milo-gateway.fly.dev/v1/registry'];
|
|
8
8
|
export class RegistrySync {
|
|
@@ -15,7 +15,7 @@ export class RegistrySync {
|
|
|
15
15
|
constructor(options) {
|
|
16
16
|
this.options = options;
|
|
17
17
|
}
|
|
18
|
-
get enabled() { return this.current.adapters.filter(adapter => knownAdapters.has(adapter.id)); }
|
|
18
|
+
get enabled() { return this.current.adapters.filter(adapter => knownAdapters.has(adapter.id) && !disabledAdapters.has(adapter.id)); }
|
|
19
19
|
async load() {
|
|
20
20
|
try {
|
|
21
21
|
const saved = JSON.parse(await readFile(join(this.options.directory, 'registry.json'), 'utf8'));
|
|
@@ -84,7 +84,7 @@ export class RegistrySync {
|
|
|
84
84
|
catch {
|
|
85
85
|
this.options.notify?.(`${adapter.id}: setup needs attention; run usegrunt setup to finish sign-in`);
|
|
86
86
|
}
|
|
87
|
-
});
|
|
87
|
+
}, SETUP_CONCURRENCY);
|
|
88
88
|
}
|
|
89
89
|
finally {
|
|
90
90
|
await this.options.afterEnsure?.();
|
package/dist/local/routing.js
CHANGED
|
@@ -19,7 +19,11 @@ export class StickyRouter {
|
|
|
19
19
|
needsHealthCheck(id) { return this.bars.get(id) === Infinity; }
|
|
20
20
|
healthy(id) { if (this.bars.get(id) === Infinity)
|
|
21
21
|
this.bars.delete(id); }
|
|
22
|
+
verified(id) { this.bars.delete(id); }
|
|
22
23
|
listed(id) { return knownAdapters.has(id) && this.options.registry().adapters.some(a => a.id === id); }
|
|
24
|
+
supportsInput(id, imageInput = false) {
|
|
25
|
+
return knownAdapters.has(id) && this.options.registry().adapters.some(adapter => adapter.id === id && adapter.modalities.includes('text') && (!imageInput || adapter.modalities.includes('image')));
|
|
26
|
+
}
|
|
23
27
|
async git(cwd, args) {
|
|
24
28
|
return (await exec('git', ['-C', cwd, ...args], { timeout: 10000, maxBuffer: 1024 * 1024 })).stdout.trim();
|
|
25
29
|
}
|
|
@@ -76,7 +80,7 @@ export class StickyRouter {
|
|
|
76
80
|
const adapter = adapters.find(a => a.id === source.id);
|
|
77
81
|
if (!task.source && this.sources.some(s => s.id === request.model) && source.id !== request.model)
|
|
78
82
|
continue;
|
|
79
|
-
if (!adapter || (this.bars.get(source.id) ?? 0) > this.now())
|
|
83
|
+
if (!adapter || this.options.pending?.has(source.id) || (this.bars.get(source.id) ?? 0) > this.now())
|
|
80
84
|
continue;
|
|
81
85
|
if (source.id === task.source || await source.available().catch(() => false))
|
|
82
86
|
eligible.push({ source, weight: adapter.weight });
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { constants } from 'node:fs';
|
|
2
|
+
import { mkdir, lstat, readdir, open, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { randomUUID } from 'node:crypto';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { knownAdapters } from '../registry.js';
|
|
6
|
+
const prefix = '.setup-session-';
|
|
7
|
+
const ttl = 90000;
|
|
8
|
+
const heartbeat = 15000;
|
|
9
|
+
const claimName = /^\.setup-session-(\d+)-([a-f0-9-]{36})$/;
|
|
10
|
+
const sourceId = /^[a-z][a-z0-9-]{0,63}$/;
|
|
11
|
+
const unavailable = () => new Error('Cannot coordinate provider setup safely; finish the existing setup or rerun usegrunt setup');
|
|
12
|
+
function alive(pid) { try {
|
|
13
|
+
process.kill(pid, 0);
|
|
14
|
+
return true;
|
|
15
|
+
}
|
|
16
|
+
catch (error) {
|
|
17
|
+
return error.code === 'EPERM';
|
|
18
|
+
} }
|
|
19
|
+
async function privateDirectory(path) {
|
|
20
|
+
const stat = await lstat(path);
|
|
21
|
+
return stat.isDirectory() && !stat.isSymbolicLink() && stat.uid === process.getuid?.() && (stat.mode & 0o777) === 0o700;
|
|
22
|
+
}
|
|
23
|
+
async function claims(directory) {
|
|
24
|
+
if (!await privateDirectory(directory))
|
|
25
|
+
throw unavailable();
|
|
26
|
+
const result = [];
|
|
27
|
+
for (const name of await readdir(directory)) {
|
|
28
|
+
const match = claimName.exec(name);
|
|
29
|
+
if (!match)
|
|
30
|
+
continue;
|
|
31
|
+
const pid = Number(match[1]), path = join(directory, name);
|
|
32
|
+
if (!Number.isSafeInteger(pid) || pid < 1 || !alive(pid))
|
|
33
|
+
continue;
|
|
34
|
+
try {
|
|
35
|
+
if (await privateDirectory(path))
|
|
36
|
+
result.push({ path, id: match[2], pid });
|
|
37
|
+
}
|
|
38
|
+
catch { /* A completed setup may remove its own directory concurrently. */ }
|
|
39
|
+
}
|
|
40
|
+
return result;
|
|
41
|
+
}
|
|
42
|
+
async function readState(claim, allowExpired = false) {
|
|
43
|
+
let file;
|
|
44
|
+
try {
|
|
45
|
+
file = await open(join(claim.path, 'state.json'), constants.O_RDONLY | constants.O_NOFOLLOW | constants.O_NONBLOCK);
|
|
46
|
+
const stat = await file.stat();
|
|
47
|
+
if (!stat.isFile() || stat.uid !== process.getuid?.() || (stat.mode & 0o777) !== 0o600 || stat.size > 32768)
|
|
48
|
+
return;
|
|
49
|
+
const state = JSON.parse(await file.readFile('utf8'));
|
|
50
|
+
if (state.version !== 1 || state.id !== claim.id || state.pid !== claim.pid || typeof state.expires !== 'number'
|
|
51
|
+
|| !Number.isFinite(state.expires) || (!allowExpired && state.expires <= Date.now()) || state.expires > Date.now() + ttl + 60000
|
|
52
|
+
|| !Array.isArray(state.owned) || state.owned.length > 100 || state.owned.some(id => typeof id !== 'string' || !sourceId.test(id))
|
|
53
|
+
|| new Set(state.owned).size !== state.owned.length || !Array.isArray(state.pending)
|
|
54
|
+
|| state.pending.some(id => typeof id !== 'string' || !state.owned.includes(id)) || new Set(state.pending).size !== state.pending.length
|
|
55
|
+
|| !Array.isArray(state.verifying) || state.verifying.some(id => typeof id !== 'string' || !state.pending.includes(id))
|
|
56
|
+
|| new Set(state.verifying).size !== state.verifying.length)
|
|
57
|
+
return;
|
|
58
|
+
return state;
|
|
59
|
+
}
|
|
60
|
+
catch {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
finally {
|
|
64
|
+
await file?.close();
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
async function blockingClaim(claim) {
|
|
68
|
+
const state = await readState(claim, true);
|
|
69
|
+
if (state)
|
|
70
|
+
return state.expires > Date.now();
|
|
71
|
+
// Protect the atomic mkdir -> initial snapshot handshake. Old partial claims
|
|
72
|
+
// cannot block a new installer indefinitely, including after PID reuse.
|
|
73
|
+
try {
|
|
74
|
+
return Date.now() - (await lstat(claim.path)).mtimeMs < ttl;
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** No credentials are stored here. Dead or expired owners never block routing. */
|
|
81
|
+
export async function readSetupSession(directory) {
|
|
82
|
+
try {
|
|
83
|
+
const active = await claims(directory);
|
|
84
|
+
const states = await Promise.all(active.map(claim => readState(claim, true)));
|
|
85
|
+
// An active initializing/corrupt claim has no trustworthy provider subset.
|
|
86
|
+
// Hold provisioning and dispatch conservatively until its owner repairs or
|
|
87
|
+
// aborts, rather than treating unsafe IO as permission to duplicate signup.
|
|
88
|
+
for (let i = 0; i < active.length; i++)
|
|
89
|
+
if (!states[i] && await blockingClaim(active[i])) {
|
|
90
|
+
const all = new Set([...knownAdapters, 'freellmapi']);
|
|
91
|
+
return { owned: all, pending: all, verifying: new Set() };
|
|
92
|
+
}
|
|
93
|
+
const valid = states.filter((state) => state !== undefined && state.expires > Date.now());
|
|
94
|
+
if (!valid.length)
|
|
95
|
+
return;
|
|
96
|
+
return {
|
|
97
|
+
owned: new Set(valid.flatMap(state => state.owned)), pending: new Set(valid.flatMap(state => state.pending)),
|
|
98
|
+
verifying: new Set(valid.flatMap(state => state.verifying))
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
/** Claim before starting the daemon. Keep this owner alive while later approvals run. */
|
|
106
|
+
export async function beginSetupSession(directory, ids) {
|
|
107
|
+
if (ids.length > 100 || ids.some(id => !sourceId.test(id)) || new Set(ids).size !== ids.length)
|
|
108
|
+
throw unavailable();
|
|
109
|
+
await mkdir(directory, { recursive: true, mode: 0o700 });
|
|
110
|
+
if (!await privateDirectory(directory))
|
|
111
|
+
throw unavailable();
|
|
112
|
+
const id = randomUUID(), pid = process.pid, path = join(directory, `${prefix}${pid}-${id}`), claim = { id, pid, path };
|
|
113
|
+
await mkdir(path, { mode: 0o700 });
|
|
114
|
+
// Every contender creates its unique claim before scanning. At most one can
|
|
115
|
+
// observe no other live claim; rejected contenders remove only their own.
|
|
116
|
+
try {
|
|
117
|
+
for (const peer of await claims(directory))
|
|
118
|
+
if (peer.id !== id && await blockingClaim(peer))
|
|
119
|
+
throw unavailable();
|
|
120
|
+
}
|
|
121
|
+
catch (error) {
|
|
122
|
+
await rm(path, { recursive: true, force: true });
|
|
123
|
+
throw error;
|
|
124
|
+
}
|
|
125
|
+
const pending = new Set(ids), verifying = new Set(), controller = new AbortController();
|
|
126
|
+
let closed = false, initialized = false, failure, tail = Promise.resolve();
|
|
127
|
+
let expires = Date.now() + ttl;
|
|
128
|
+
let timer, expiryTimer;
|
|
129
|
+
const fail = () => {
|
|
130
|
+
failure ??= unavailable();
|
|
131
|
+
controller.abort(failure);
|
|
132
|
+
if (timer)
|
|
133
|
+
clearInterval(timer);
|
|
134
|
+
timer = undefined;
|
|
135
|
+
if (expiryTimer)
|
|
136
|
+
clearTimeout(expiryTimer);
|
|
137
|
+
};
|
|
138
|
+
const armExpiry = () => {
|
|
139
|
+
if (expiryTimer)
|
|
140
|
+
clearTimeout(expiryTimer);
|
|
141
|
+
expiryTimer = setTimeout(fail, Math.max(0, expires - Date.now()));
|
|
142
|
+
expiryTimer.unref();
|
|
143
|
+
};
|
|
144
|
+
armExpiry();
|
|
145
|
+
const assertOwner = async () => {
|
|
146
|
+
if (closed || controller.signal.aborted || expires <= Date.now() || !await privateDirectory(path))
|
|
147
|
+
throw unavailable();
|
|
148
|
+
if (initialized) {
|
|
149
|
+
const state = await readState(claim, true);
|
|
150
|
+
if (!state || state.expires !== expires)
|
|
151
|
+
throw unavailable();
|
|
152
|
+
}
|
|
153
|
+
// A committed newer owner means this one has lost authority. Initializing
|
|
154
|
+
// peers will reject our current valid claim, so they cannot evict us.
|
|
155
|
+
for (const peer of await claims(directory))
|
|
156
|
+
if (peer.id !== id && await readState(peer))
|
|
157
|
+
throw unavailable();
|
|
158
|
+
if (controller.signal.aborted || expires <= Date.now())
|
|
159
|
+
throw unavailable();
|
|
160
|
+
};
|
|
161
|
+
const persist = () => {
|
|
162
|
+
const work = tail.then(async () => {
|
|
163
|
+
if (closed)
|
|
164
|
+
return;
|
|
165
|
+
await assertOwner();
|
|
166
|
+
const temporary = join(path, `state.${randomUUID()}.tmp`), nextExpiry = Date.now() + ttl;
|
|
167
|
+
try {
|
|
168
|
+
const state = { version: 1, id, pid, expires: nextExpiry, owned: [...ids], pending: [...pending], verifying: [...verifying] };
|
|
169
|
+
await writeFile(temporary, JSON.stringify(state), { flag: 'wx', mode: 0o600 });
|
|
170
|
+
// Slow IO must not resurrect a lease after its TTL or watchdog abort.
|
|
171
|
+
await assertOwner();
|
|
172
|
+
await rename(temporary, join(path, 'state.json'));
|
|
173
|
+
if (controller.signal.aborted || expires <= Date.now())
|
|
174
|
+
throw unavailable();
|
|
175
|
+
expires = nextExpiry;
|
|
176
|
+
initialized = true;
|
|
177
|
+
armExpiry();
|
|
178
|
+
}
|
|
179
|
+
finally {
|
|
180
|
+
await rm(temporary, { force: true });
|
|
181
|
+
}
|
|
182
|
+
}).catch(() => { fail(); throw failure; });
|
|
183
|
+
tail = work.catch(() => { });
|
|
184
|
+
return work;
|
|
185
|
+
};
|
|
186
|
+
try {
|
|
187
|
+
await persist();
|
|
188
|
+
}
|
|
189
|
+
catch (error) {
|
|
190
|
+
closed = true;
|
|
191
|
+
fail();
|
|
192
|
+
await rm(path, { recursive: true, force: true });
|
|
193
|
+
throw error;
|
|
194
|
+
}
|
|
195
|
+
timer = setInterval(() => { void persist().catch(() => { }); }, heartbeat);
|
|
196
|
+
timer.unref();
|
|
197
|
+
const update = async (source, state) => {
|
|
198
|
+
if (closed || controller.signal.aborted || !ids.includes(source))
|
|
199
|
+
throw unavailable();
|
|
200
|
+
if (state === 'release') {
|
|
201
|
+
pending.delete(source);
|
|
202
|
+
verifying.delete(source);
|
|
203
|
+
}
|
|
204
|
+
else {
|
|
205
|
+
pending.add(source);
|
|
206
|
+
if (state === 'verify')
|
|
207
|
+
verifying.add(source);
|
|
208
|
+
else
|
|
209
|
+
verifying.delete(source);
|
|
210
|
+
}
|
|
211
|
+
await persist();
|
|
212
|
+
};
|
|
213
|
+
return {
|
|
214
|
+
signal: controller.signal,
|
|
215
|
+
verify: source => update(source, 'verify'),
|
|
216
|
+
hold: source => update(source, 'hold'),
|
|
217
|
+
release: source => update(source, 'release'),
|
|
218
|
+
async close() {
|
|
219
|
+
if (closed)
|
|
220
|
+
return;
|
|
221
|
+
closed = true;
|
|
222
|
+
if (timer)
|
|
223
|
+
clearInterval(timer);
|
|
224
|
+
timer = undefined;
|
|
225
|
+
if (expiryTimer)
|
|
226
|
+
clearTimeout(expiryTimer);
|
|
227
|
+
controller.abort(new Error('Provider setup session ended'));
|
|
228
|
+
await tail;
|
|
229
|
+
await rm(path, { recursive: true, force: true });
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
}
|