usegrunt 0.6.5 → 0.6.6

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.
@@ -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
+ }
@@ -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?.();