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.
- package/README.md +14 -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 +63 -23
- 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 +12 -11
- 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/setup.js +332 -62
- package/dist/local/source.js +7 -2
- package/dist/local/wizard.js +24 -21
- package/dist/registry/adapters.json +57 -33
- package/dist/registry.js +4 -2
- package/package.json +1 -1
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
import { Ajv } from 'ajv';
|
|
2
|
+
import { readDataSharingConsent } from './data-sharing.js';
|
|
3
|
+
import { providerGrantDirectory } from './provider-grant.js';
|
|
4
|
+
import { FreebuffTools } from './freebuff-tools.js';
|
|
5
|
+
import { SourceError } from './source.js';
|
|
6
|
+
export const KILO_PARTICLE = 'particle-ai/particle-1:free';
|
|
7
|
+
export const KILO_ORIGIN = 'https://ai-gateway.kilo.ai/api/v1';
|
|
8
|
+
const object = (value) => value && typeof value === 'object' && !Array.isArray(value) ? value : {};
|
|
9
|
+
async function abortable(promise, signal) {
|
|
10
|
+
signal.throwIfAborted();
|
|
11
|
+
let abort;
|
|
12
|
+
try {
|
|
13
|
+
return await Promise.race([promise, new Promise((_resolve, reject) => {
|
|
14
|
+
abort = () => reject(signal.reason);
|
|
15
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
16
|
+
if (signal.aborted)
|
|
17
|
+
abort();
|
|
18
|
+
})]);
|
|
19
|
+
}
|
|
20
|
+
finally {
|
|
21
|
+
if (abort)
|
|
22
|
+
signal.removeEventListener('abort', abort);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
async function json(response, signal, limit) {
|
|
26
|
+
if (!response.body || Number(response.headers.get('content-length')) > limit) {
|
|
27
|
+
void response.body?.cancel().catch(() => { });
|
|
28
|
+
throw new SourceError('upstream', 'Kilo response exceeded the supported size');
|
|
29
|
+
}
|
|
30
|
+
const reader = response.body.getReader(), chunks = [];
|
|
31
|
+
let bytes = 0;
|
|
32
|
+
try {
|
|
33
|
+
for (;;) {
|
|
34
|
+
const part = await abortable(reader.read(), signal);
|
|
35
|
+
signal.throwIfAborted();
|
|
36
|
+
if (part.done)
|
|
37
|
+
break;
|
|
38
|
+
bytes += part.value.byteLength;
|
|
39
|
+
if (bytes > limit)
|
|
40
|
+
throw new SourceError('upstream', 'Kilo response exceeded the supported size');
|
|
41
|
+
chunks.push(part.value);
|
|
42
|
+
}
|
|
43
|
+
return object(JSON.parse(Buffer.concat(chunks).toString('utf8')));
|
|
44
|
+
}
|
|
45
|
+
finally {
|
|
46
|
+
void reader.cancel().catch(() => { });
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
function status(response) {
|
|
50
|
+
if (response.ok)
|
|
51
|
+
return;
|
|
52
|
+
const raw = response.headers.get('retry-after'), seconds = raw === null ? NaN : /^\d+(?:\.\d+)?$/.test(raw) ? Number(raw) : (Date.parse(raw) - Date.now()) / 1000;
|
|
53
|
+
void response.body?.cancel().catch(() => { });
|
|
54
|
+
throw new SourceError(response.status === 429 ? 'quota' : [401, 402, 403].includes(response.status) ? 'refused' : 'upstream', `Kilo returned HTTP ${response.status}`, response.status === 429 ? Math.max(1, Number.isFinite(seconds) ? Math.ceil(seconds) : 60) : undefined);
|
|
55
|
+
}
|
|
56
|
+
/** Nonbillable public metadata only. No account, API key, or paid fallback is used. */
|
|
57
|
+
export async function verifyKiloAccess(options = {}) {
|
|
58
|
+
const signal = AbortSignal.any([...(options.signal ? [options.signal] : []), AbortSignal.timeout(5000)]);
|
|
59
|
+
try {
|
|
60
|
+
signal.throwIfAborted();
|
|
61
|
+
const response = await abortable((options.fetcher ?? fetch)(`${KILO_ORIGIN}/models`, { redirect: 'error', signal }), signal);
|
|
62
|
+
status(response);
|
|
63
|
+
const body = await json(response, signal, 4 * 1024 * 1024), rows = Array.isArray(body.data) ? body.data.filter(row => object(row).id === KILO_PARTICLE) : [];
|
|
64
|
+
const model = object(rows.length === 1 ? rows[0] : undefined), pricing = object(model.pricing), parameters = model.supported_parameters;
|
|
65
|
+
const zero = (key) => (typeof pricing[key] === 'string' || typeof pricing[key] === 'number') && String(pricing[key]).trim() !== '' && Number(pricing[key]) === 0;
|
|
66
|
+
const costs = ['request', 'image', 'web_search', 'internal_reasoning', 'input_cache_read', 'input_cache_write'];
|
|
67
|
+
if (model.isFree !== true || !zero('prompt') || !zero('completion') || costs.some(key => key in pricing && !zero(key))
|
|
68
|
+
|| !Number.isSafeInteger(model.context_length) || Number(model.context_length) < 200000
|
|
69
|
+
|| !Array.isArray(parameters) || !['tools', 'max_tokens'].every(key => parameters.includes(key))) {
|
|
70
|
+
throw new SourceError('refused', 'Kilo Particle is not currently a verified free model with at least 200K context');
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
catch (error) {
|
|
74
|
+
if (options.signal?.aborted)
|
|
75
|
+
throw new SourceError('cancelled', 'Kilo access check cancelled');
|
|
76
|
+
if (signal.aborted)
|
|
77
|
+
throw new SourceError('timeout', 'Kilo access check timed out');
|
|
78
|
+
if (error instanceof SourceError)
|
|
79
|
+
throw error;
|
|
80
|
+
throw new SourceError('upstream', 'Kilo free model metadata is unavailable');
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function conversation(request) {
|
|
84
|
+
try {
|
|
85
|
+
const messages = [];
|
|
86
|
+
for (const message of request.messages) {
|
|
87
|
+
const previous = messages.at(-1);
|
|
88
|
+
// Responses expresses parallel calls as separate assistant items.
|
|
89
|
+
if (message.role === 'assistant' && message.content === '' && message.tool_calls?.length && !message.tool_call_id
|
|
90
|
+
&& previous?.role === 'assistant' && previous.content === '' && previous.tool_calls?.length && !previous.tool_call_id) {
|
|
91
|
+
previous.tool_calls = [...previous.tool_calls, ...message.tool_calls];
|
|
92
|
+
}
|
|
93
|
+
else
|
|
94
|
+
messages.push({ ...message });
|
|
95
|
+
}
|
|
96
|
+
new FreebuffTools({ ...request, messages }); // Validate roles and actual correlated results; no JSON bridge or tool execution.
|
|
97
|
+
const validators = new Map(), ajv = new Ajv({ strict: true, strictTypes: false, strictTuples: false, addUsedSchema: false });
|
|
98
|
+
for (const tool of request.tools) {
|
|
99
|
+
const fn = object(tool.function);
|
|
100
|
+
validators.set(fn.name, ajv.compile(fn.parameters));
|
|
101
|
+
}
|
|
102
|
+
return { messages, validators };
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
throw new SourceError('refused', 'Kilo rejected invalid function tools or an uncorrelated tool conversation');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
export class Kilo {
|
|
109
|
+
options;
|
|
110
|
+
id = 'kilo';
|
|
111
|
+
supportsFunctionTools = true;
|
|
112
|
+
shutdown = new AbortController();
|
|
113
|
+
pending = new Set();
|
|
114
|
+
constructor(options = {}) {
|
|
115
|
+
this.options = options;
|
|
116
|
+
}
|
|
117
|
+
async approved() { return (await readDataSharingConsent(this.options.directory ?? providerGrantDirectory(), [this.id])).includes(this.id); }
|
|
118
|
+
async track(task) { this.pending.add(task); try {
|
|
119
|
+
return await task;
|
|
120
|
+
}
|
|
121
|
+
finally {
|
|
122
|
+
this.pending.delete(task);
|
|
123
|
+
} }
|
|
124
|
+
async available() {
|
|
125
|
+
return this.track((async () => {
|
|
126
|
+
try {
|
|
127
|
+
if (this.shutdown.signal.aborted || !await this.approved())
|
|
128
|
+
return false;
|
|
129
|
+
await verifyKiloAccess({ ...(this.options.fetcher ? { fetcher: this.options.fetcher } : {}), signal: this.shutdown.signal });
|
|
130
|
+
return !this.shutdown.signal.aborted;
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
})());
|
|
136
|
+
}
|
|
137
|
+
async healthCheck() { return this.available(); }
|
|
138
|
+
complete(request, caller) { return this.track(this.once(request, caller)); }
|
|
139
|
+
async once(request, caller) {
|
|
140
|
+
const timeout = this.options.timeout, signal = AbortSignal.any([caller, this.shutdown.signal, AbortSignal.timeout(typeof timeout === 'number' && Number.isFinite(timeout) && timeout > 0 ? Math.min(180000, Math.ceil(timeout)) : 180000)]);
|
|
141
|
+
try {
|
|
142
|
+
signal.throwIfAborted();
|
|
143
|
+
if (request.imageInput)
|
|
144
|
+
throw new SourceError('refused', 'Kilo Particle supports text inputs through this route');
|
|
145
|
+
if (request.maxTokens !== undefined && (!Number.isSafeInteger(request.maxTokens) || request.maxTokens < 1))
|
|
146
|
+
throw new SourceError('refused', 'Kilo requires a positive integer output-token budget');
|
|
147
|
+
const limit = Math.min(4096, request.maxTokens ?? 4096), { messages, validators } = conversation(request);
|
|
148
|
+
if (!await this.approved())
|
|
149
|
+
throw new SourceError('refused', 'Kilo data sharing is not approved; rerun usegrunt setup');
|
|
150
|
+
signal.throwIfAborted();
|
|
151
|
+
const body = JSON.stringify({ model: KILO_PARTICLE, messages, ...(request.tools.length ? { tools: request.tools } : {}), stream: false, max_tokens: limit });
|
|
152
|
+
if (Buffer.byteLength(body) > 2 * 1024 * 1024)
|
|
153
|
+
throw new SourceError('refused', 'Kilo prompt exceeds the supported input size');
|
|
154
|
+
await verifyKiloAccess({ ...(this.options.fetcher ? { fetcher: this.options.fetcher } : {}), signal });
|
|
155
|
+
signal.throwIfAborted();
|
|
156
|
+
const response = await abortable((this.options.fetcher ?? fetch)(`${KILO_ORIGIN}/chat/completions`, { method: 'POST', redirect: 'error', signal,
|
|
157
|
+
headers: { 'Content-Type': 'application/json' }, body }), signal);
|
|
158
|
+
status(response);
|
|
159
|
+
const result = await json(response, signal, 2 * 1024 * 1024), usage = object(result.usage), details = object(usage.cost_details);
|
|
160
|
+
if (result.model !== KILO_PARTICLE)
|
|
161
|
+
throw new SourceError('refused', 'Kilo returned an unverified Particle model');
|
|
162
|
+
if (usage.cost !== 0 || ['total_cost', 'credits_charged'].some(key => key in usage && usage[key] !== 0)
|
|
163
|
+
|| ['upstream_inference_cost', 'upstream_inference_prompt_cost', 'upstream_inference_completions_cost'].some(key => details[key] !== 0)
|
|
164
|
+
|| ['cost', 'total_cost', 'credits_charged'].some(key => key in result && result[key] !== 0)
|
|
165
|
+
|| !Number.isSafeInteger(usage.prompt_tokens) || Number(usage.prompt_tokens) < 1 || !Number.isSafeInteger(usage.completion_tokens)
|
|
166
|
+
|| Number(usage.completion_tokens) < 1 || Number(usage.completion_tokens) > limit)
|
|
167
|
+
throw new SourceError('refused', 'Kilo returned unverified free usage or exceeded the output budget');
|
|
168
|
+
const choice = object(Array.isArray(result.choices) && result.choices.length === 1 ? result.choices[0] : undefined), message = object(choice.message);
|
|
169
|
+
if (message.refusal)
|
|
170
|
+
throw new SourceError('refused', 'Kilo refused the request');
|
|
171
|
+
if (!(message.content === undefined || message.content === null || typeof message.content === 'string'))
|
|
172
|
+
throw new SourceError('upstream', 'Kilo returned invalid completion content');
|
|
173
|
+
const calls = [], ids = new Set();
|
|
174
|
+
if (message.tool_calls !== undefined) {
|
|
175
|
+
if (!Array.isArray(message.tool_calls) || message.tool_calls.length > 16)
|
|
176
|
+
throw new SourceError('upstream', 'Kilo returned invalid function calls');
|
|
177
|
+
for (const value of message.tool_calls) {
|
|
178
|
+
const call = object(value), fn = object(call.function);
|
|
179
|
+
let args;
|
|
180
|
+
try {
|
|
181
|
+
args = JSON.parse(String(fn.arguments));
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
throw new SourceError('upstream', 'Kilo returned invalid function arguments');
|
|
185
|
+
}
|
|
186
|
+
if (call.type !== 'function' || typeof call.id !== 'string' || !call.id || call.id.length > 256 || ids.has(call.id)
|
|
187
|
+
|| request.messages.some(m => m.tool_calls?.some(old => old.id === call.id)) || typeof fn.name !== 'string' || typeof fn.arguments !== 'string'
|
|
188
|
+
|| !args || typeof args !== 'object' || Array.isArray(args) || !validators.get(fn.name)?.(args))
|
|
189
|
+
throw new SourceError('upstream', 'Kilo returned invalid function calls');
|
|
190
|
+
ids.add(call.id);
|
|
191
|
+
calls.push({ id: call.id, type: 'function', function: { name: fn.name, arguments: fn.arguments } });
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (!message.content && !calls.length)
|
|
195
|
+
throw new SourceError('upstream', 'Kilo returned an empty completion');
|
|
196
|
+
return { text: typeof message.content === 'string' ? message.content : '', ...(calls.length ? { calls } : {}), inputTokens: usage.prompt_tokens, outputTokens: usage.completion_tokens };
|
|
197
|
+
}
|
|
198
|
+
catch (error) {
|
|
199
|
+
if (caller.aborted || this.shutdown.signal.aborted)
|
|
200
|
+
throw new SourceError('cancelled', 'Kilo request cancelled');
|
|
201
|
+
if (signal.aborted)
|
|
202
|
+
throw new SourceError('timeout', 'Kilo request timed out');
|
|
203
|
+
if (error instanceof SourceError)
|
|
204
|
+
throw error;
|
|
205
|
+
throw new SourceError('upstream', 'Kilo request failed');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
async close() { this.shutdown.abort(); await Promise.allSettled([...this.pending]); }
|
|
209
|
+
}
|
package/dist/local/main.js
CHANGED
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import { knownAdapters } from '../registry.js';
|
|
2
|
+
import { disabledAdapters, knownAdapters } from '../registry.js';
|
|
3
3
|
import { realpathSync } from 'node:fs';
|
|
4
4
|
import { pathToFileURL } from 'node:url';
|
|
5
5
|
import { basename } from 'node:path';
|
|
6
6
|
import { serve } from '@hono/node-server';
|
|
7
7
|
import { createLocalApp } from './api.js';
|
|
8
|
-
import {
|
|
8
|
+
import { positive } from './freebuff.js';
|
|
9
9
|
import { Sources } from './source.js';
|
|
10
10
|
import { FreeLLMAPI } from './freellmapi.js';
|
|
11
11
|
import { Nvidia } from './nvidia.js';
|
|
12
12
|
import { Zro } from './zro.js';
|
|
13
13
|
import { TokenHarbor } from './tokenharbor.js';
|
|
14
14
|
import { Runcrate } from './runcrate.js';
|
|
15
|
-
import { Apinex } from './apinex.js';
|
|
16
15
|
import { MistralVibe } from './mistral-vibe.js';
|
|
17
16
|
import { LiveKit } from './livekit.js';
|
|
17
|
+
import { Pollinations } from './pollinations.js';
|
|
18
|
+
import { BeatAPI } from './beatapi.js';
|
|
19
|
+
import { Kilo } from './kilo.js';
|
|
20
|
+
import { Clixad } from './clixad.js';
|
|
21
|
+
import { Perch } from './perch.js';
|
|
18
22
|
import { RegistrySource } from './registry-source.js';
|
|
19
23
|
import { ManagedRouter } from './managed-router.js';
|
|
20
24
|
import { ConsentSource } from './consent-source.js';
|
|
@@ -51,8 +55,8 @@ export function configure(env = process.env, registry = () => bundledRegistry) {
|
|
|
51
55
|
// default while retaining explicit environment and custom-model overrides.
|
|
52
56
|
return adapter?.models ?? (adapter?.model && adapter.model !== 'big-pickle' ? [adapter.model] : DEFAULT_OPENCODE_MODELS);
|
|
53
57
|
};
|
|
54
|
-
const registered = [dots, new Codex(env.MILO_CODEX_MODEL ? { model: env.MILO_CODEX_MODEL } : {}), new ManagedRouter(new FreeLLMAPI(connection), url, configDir(env)), new
|
|
55
|
-
const ids = env.MILO_SOURCES?.split(',').map(id => id.trim()) ?? registry().adapters.filter(adapter => knownAdapters.has(adapter.id)).map(adapter => adapter.id);
|
|
58
|
+
const registered = [dots, new Codex(env.MILO_CODEX_MODEL ? { model: env.MILO_CODEX_MODEL } : {}), new ManagedRouter(new FreeLLMAPI(connection), url, configDir(env)), new Kilo({ directory: configDir(env) }), new Nvidia({ directory: configDir(env) }), new Zro({ directory: configDir(env) }), new TokenHarbor({ directory: configDir(env) }), new Runcrate({ directory: configDir(env) }), new MistralVibe({ directory: configDir(env) }), new LiveKit({ directory: configDir(env) }), new Pollinations({ directory: configDir(env) }), new BeatAPI({ directory: configDir(env) }), new Clixad({ directory: configDir(env) }), new Perch({ directory: configDir(env) }), new OpenCode({ models: opencodeModels, ...(env.MILO_OPENCODE_URL ? { url: env.MILO_OPENCODE_URL } : {}) })];
|
|
59
|
+
const ids = (env.MILO_SOURCES?.split(',').map(id => id.trim()) ?? registry().adapters.filter(adapter => knownAdapters.has(adapter.id)).map(adapter => adapter.id)).filter(id => id && !disabledAdapters.has(id));
|
|
56
60
|
const routing = ids.every(id => knownAdapters.has(id)) ? { registry } : undefined;
|
|
57
61
|
return new Sources(ids.map(id => {
|
|
58
62
|
const source = registered.find(source => source.id === id);
|
|
@@ -60,7 +64,7 @@ export function configure(env = process.env, registry = () => bundledRegistry) {
|
|
|
60
64
|
throw new Error(`Unknown grunt source: ${id}`);
|
|
61
65
|
const consent = requiresDataSharing(id) && id !== 'tokenharbor' ? new ConsentSource(source, configDir(env)) : source;
|
|
62
66
|
return !routing && knownAdapters.has(id) ? new RegistrySource(consent, registry) : consent;
|
|
63
|
-
}), routing);
|
|
67
|
+
}), routing, disabledAdapters);
|
|
64
68
|
}
|
|
65
69
|
export function start(env = process.env) {
|
|
66
70
|
let registry = structuredClone(bundledRegistry);
|
|
@@ -80,7 +84,7 @@ export function start(env = process.env) {
|
|
|
80
84
|
const client = squire ??= new BrowserSquire({ background: true, signal, dataSharingApproved: approved });
|
|
81
85
|
client.approveDataSharing([...new Set([...(await readDataSharingConsent(directory, [...knownAdapters])), ...approved])]);
|
|
82
86
|
try {
|
|
83
|
-
const result = await setupFallbacks({ directory, run: (binary, args) => command(binary, args, false, signal), notify: console.error, adapters: [adapter.id], squire: client, signal, dataSharingApproved: approved,
|
|
87
|
+
const result = await setupFallbacks({ directory, run: (binary, args) => command(binary, args, false, signal), notify: console.error, adapters: [adapter.id], squire: client, signal, background: true, dataSharingApproved: approved,
|
|
84
88
|
connect: () => client.connect() });
|
|
85
89
|
if (result.unavailable.length)
|
|
86
90
|
throw new Error('Adapter setup incomplete');
|
|
@@ -104,12 +108,9 @@ export function start(env = process.env) {
|
|
|
104
108
|
throw new Error('MILO_PORT must be at most 65535');
|
|
105
109
|
const app = createLocalApp(sources, { port, dotsEnv: env, log: cwd => console.error(`grunt working directory: ${cwd}`) });
|
|
106
110
|
const server = serve({ fetch: async (request) => { await ready; return app.fetch(request); }, hostname: HOST, port }, () => console.error(`grunt model API: http://${HOST}:${port}/v1`));
|
|
107
|
-
// A distinct source list prevents FreeLLMAPI -> grunt -> FreeLLMAPI recursion.
|
|
108
|
-
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;
|
|
109
|
-
const bridge = fallback ? serve({ fetch: createLocalApp(fallback, { port: 8789 }).fetch, hostname: HOST, port: 8789 }) : undefined;
|
|
110
111
|
let closing = false;
|
|
111
112
|
const close = async () => { if (closing)
|
|
112
|
-
return; closing = true; await sync.close(); await sources.close();
|
|
113
|
+
return; closing = true; await sync.close(); await sources.close(); server.close(); };
|
|
113
114
|
process.once('SIGINT', close);
|
|
114
115
|
process.once('SIGTERM', close);
|
|
115
116
|
return { server, close };
|
package/dist/local/parallel.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export const SETUP_CONCURRENCY = 6;
|
|
2
|
+
export const BROWSER_CONCURRENCY = 3;
|
|
1
3
|
// Limit browser/build pressure as the vetted catalog grows. Wait for started
|
|
2
4
|
// jobs before rejecting so callers can safely close shared resources.
|
|
3
5
|
export async function parallelMap(items, task, limit = 3, signal) {
|