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.
@@ -1,4 +1,5 @@
1
1
  import { SourceError } from './source.js';
2
+ import { KILO_PARTICLE } from './kilo.js';
2
3
  // Numeric loopback addresses avoid DNS rebinding and accidental remote routing.
3
4
  export function loopback(value) {
4
5
  const url = new URL(value);
@@ -7,18 +8,15 @@ export function loopback(value) {
7
8
  return url;
8
9
  }
9
10
  export const KILO_URL = 'https://ai-gateway.kilo.ai/api/v1';
10
- export const FREEBUFF_URL = 'http://127.0.0.1:8789/v1';
11
11
  export function freeConfig() {
12
12
  return {
13
13
  customProviders: [
14
- { baseUrl: KILO_URL, apiKey: 'no-key', label: 'Kilo Free', models: [{ model: 'kilo-auto/free', supportsTools: true }] },
15
- { baseUrl: FREEBUFF_URL, apiKey: 'no-key', label: 'Freebuff', models: [{ model: 'freebuff', supportsTools: false }] }
14
+ { baseUrl: KILO_URL, apiKey: 'no-key', label: 'Kilo Free', models: [{ model: KILO_PARTICLE, supportsTools: true }] }
16
15
  ],
17
16
  // Existing keyed provider rows retain their catalog priorities. Never enable
18
17
  // OpenRouter or unvetted aggregators by provisioning keys for them here.
19
18
  fallback: [
20
- { platform: 'custom', modelId: 'kilo-auto/free', endpoint: KILO_URL, priority: 999999 },
21
- { platform: 'custom', modelId: 'freebuff', endpoint: FREEBUFF_URL, priority: 1000000 }
19
+ { platform: 'custom', modelId: KILO_PARTICLE, endpoint: KILO_URL, priority: 999999 }
22
20
  ],
23
21
  routing: { strategy: 'priority' }
24
22
  };
@@ -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
+ }
@@ -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
- import { basename } from 'node:path';
5
+ import { basename, join } from 'node:path';
6
6
  import { serve } from '@hono/node-server';
7
7
  import { createLocalApp } from './api.js';
8
- import { Freebuff, positive } from './freebuff.js';
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';
@@ -27,6 +31,7 @@ import { RegistrySync } from './registry.js';
27
31
  import { configDir, command } from './wizard.js';
28
32
  import { setupFallbacks } from './fallback-setup.js';
29
33
  import { BrowserSquire } from './setup.js';
34
+ import { readSetupSession } from './setup-session.js';
30
35
  export const HOST = '127.0.0.1';
31
36
  export function configure(env = process.env, registry = () => bundledRegistry) {
32
37
  const url = env.MILO_FREELLMAPI_URL ?? 'http://127.0.0.1:3001/v1';
@@ -51,8 +56,8 @@ export function configure(env = process.env, registry = () => bundledRegistry) {
51
56
  // default while retaining explicit environment and custom-model overrides.
52
57
  return adapter?.models ?? (adapter?.model && adapter.model !== 'big-pickle' ? [adapter.model] : DEFAULT_OPENCODE_MODELS);
53
58
  };
54
- 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 Runcrate({ directory: configDir(env) }), new Apinex({ directory: configDir(env) }), new MistralVibe({ directory: configDir(env) }), new LiveKit({ 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' })];
55
- const ids = env.MILO_SOURCES?.split(',').map(id => id.trim()) ?? registry().adapters.filter(adapter => knownAdapters.has(adapter.id)).map(adapter => adapter.id);
59
+ 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, configFile: join(configDir(env), 'opencode-free.json'), ...(env.MILO_OPENCODE_URL ? { url: env.MILO_OPENCODE_URL } : {}) })];
60
+ 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
61
  const routing = ids.every(id => knownAdapters.has(id)) ? { registry } : undefined;
57
62
  return new Sources(ids.map(id => {
58
63
  const source = registered.find(source => source.id === id);
@@ -60,56 +65,93 @@ export function configure(env = process.env, registry = () => bundledRegistry) {
60
65
  throw new Error(`Unknown grunt source: ${id}`);
61
66
  const consent = requiresDataSharing(id) && id !== 'tokenharbor' ? new ConsentSource(source, configDir(env)) : source;
62
67
  return !routing && knownAdapters.has(id) ? new RegistrySource(consent, registry) : consent;
63
- }), routing);
68
+ }), routing, disabledAdapters);
64
69
  }
65
70
  export function start(env = process.env) {
66
71
  let registry = structuredClone(bundledRegistry);
67
72
  const sources = configure(env, () => registry);
68
73
  const directory = configDir(env);
69
74
  let squire;
75
+ const backgroundPreparing = new Set();
76
+ const reconcile = () => {
77
+ sources.preparing.clear();
78
+ for (const id of backgroundPreparing)
79
+ sources.preparing.add(id);
80
+ for (const id of sources.pending)
81
+ sources.preparing.add(id);
82
+ };
83
+ // Serialize snapshots so an older filesystem read cannot overwrite a newer
84
+ // provider release while requests and registry checks run concurrently.
85
+ let sessionRead = Promise.resolve();
86
+ const refreshSession = () => {
87
+ const result = sessionRead.then(async () => {
88
+ const session = await readSetupSession(directory);
89
+ sources.pending.clear();
90
+ for (const id of session?.pending ?? [])
91
+ if (sources.sources.some(source => source.id === id))
92
+ sources.pending.add(id);
93
+ sources.verifying.clear();
94
+ for (const id of session?.verifying ?? [])
95
+ if (sources.pending.has(id))
96
+ sources.verifying.add(id);
97
+ reconcile();
98
+ return session;
99
+ });
100
+ sessionRead = result.catch(() => { });
101
+ return result;
102
+ };
70
103
  const sync = new RegistrySync({ directory, ...(env.GRUNT_REGISTRY_URL ? { url: env.GRUNT_REGISTRY_URL } : {}),
71
104
  afterEnsure: async () => { await squire?.close(); squire = undefined; },
72
105
  changed: current => { registry = current; }, notify: console.error,
73
106
  ensure: async (adapter, signal) => {
74
107
  if (!sources.sources.some(source => source.id === adapter.id))
75
108
  return;
109
+ const session = await refreshSession();
110
+ if (session?.owned.has(adapter.id)) {
111
+ backgroundPreparing.delete(adapter.id);
112
+ reconcile();
113
+ return;
114
+ }
76
115
  const approved = await readDataSharingConsent(directory, [adapter.id]);
77
116
  if (requiresDataSharing(adapter.id) && !approved.includes(adapter.id))
78
117
  return;
79
- sources.preparing.add(adapter.id);
118
+ backgroundPreparing.add(adapter.id);
119
+ reconcile();
80
120
  const client = squire ??= new BrowserSquire({ background: true, signal, dataSharingApproved: approved });
81
121
  client.approveDataSharing([...new Set([...(await readDataSharingConsent(directory, [...knownAdapters])), ...approved])]);
82
122
  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,
123
+ 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
124
  connect: () => client.connect() });
85
125
  if (result.unavailable.length)
86
126
  throw new Error('Adapter setup incomplete');
87
127
  sources.healthy(adapter.id);
88
128
  }
89
129
  finally {
90
- sources.preparing.delete(adapter.id);
130
+ backgroundPreparing.delete(adapter.id);
131
+ reconcile();
91
132
  }
92
133
  }
93
134
  });
94
135
  // Load the private cache before accepting tasks, without waiting for the network.
95
136
  // Mark adapters as preparing until their first background setup finishes.
96
- const ready = sync.load().then(() => { for (const adapter of sync.enabled)
97
- if (sources.sources.some(source => source.id === adapter.id))
98
- sources.preparing.add(adapter.id); });
99
- void ready.then(() => sync.start()).catch(() => console.error('Registry sync could not start; using the bundled list')).finally(() => sources.preparing.clear());
137
+ const ready = sync.load().then(async () => {
138
+ const session = await refreshSession();
139
+ for (const adapter of sync.enabled)
140
+ if (!session?.owned.has(adapter.id) && sources.sources.some(source => source.id === adapter.id))
141
+ backgroundPreparing.add(adapter.id);
142
+ reconcile();
143
+ });
144
+ void ready.then(() => sync.start()).catch(() => console.error('Registry sync could not start; using the bundled list')).finally(() => { backgroundPreparing.clear(); reconcile(); });
100
145
  // Warm the saved login without delaying the server; failed loads retry lazily.
101
- void sources.sources.find(source => ['freellmapi', 'kilo'].includes(source.id))?.available();
146
+ void ready.then(() => sources.sources.find(source => !sources.pending.has(source.id) && ['freellmapi', 'kilo'].includes(source.id))?.available()).catch(() => { });
102
147
  const port = positive(env.MILO_PORT, 8788);
103
148
  if (port > 65535)
104
149
  throw new Error('MILO_PORT must be at most 65535');
105
150
  const app = createLocalApp(sources, { port, dotsEnv: env, log: cwd => console.error(`grunt working directory: ${cwd}`) });
106
- 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;
151
+ const server = serve({ fetch: async (request) => { await ready; await refreshSession(); return app.fetch(request); }, hostname: HOST, port }, () => console.error(`grunt model API: http://${HOST}:${port}/v1`));
110
152
  let closing = false;
111
153
  const close = async () => { if (closing)
112
- return; closing = true; await sync.close(); await sources.close(); await fallback?.close(); bridge?.close(); server.close(); };
154
+ return; closing = true; await sync.close(); await sources.close(); server.close(); };
113
155
  process.once('SIGINT', close);
114
156
  process.once('SIGTERM', close);
115
157
  return { server, close };
@@ -1,4 +1,4 @@
1
- import { guardRouterSharing } from './setup.js';
1
+ import { guardRouterSharing, RouterSetupPending } from './setup.js';
2
2
  import { SourceError } from './source.js';
3
3
  // The legacy backend can contain Grunt-created sharing providers alongside the
4
4
  // user's custom rows. Enforce current consent without disabling custom sources.
@@ -22,7 +22,9 @@ export class ManagedRouter {
22
22
  try {
23
23
  await guardRouterSharing(this.url, this.directory);
24
24
  }
25
- catch {
25
+ catch (error) {
26
+ if (error instanceof RouterSetupPending)
27
+ throw new SourceError('not_installed', 'A managed provider is still being verified; other ready sources remain available');
26
28
  throw new SourceError('refused', 'Cannot enforce saved router data-sharing choices; rerun usegrunt setup');
27
29
  }
28
30
  signal.throwIfAborted();
@@ -1,7 +1,7 @@
1
1
  import { spawn } from 'node:child_process';
2
2
  import { createHash } from 'node:crypto';
3
3
  import { tmpdir } from 'node:os';
4
- import { realpath } from 'node:fs/promises';
4
+ import { lstat, realpath } from 'node:fs/promises';
5
5
  import { userTurns } from './harness.js';
6
6
  import { executable } from './multiplexer.js';
7
7
  import { loopback } from './freellmapi.js';
@@ -53,31 +53,51 @@ export class OpenCode {
53
53
  return this.options.url.replace(/\/$/, '');
54
54
  if (this.starting)
55
55
  return this.starting;
56
- const binary = await executable('opencode');
57
- this.starting = new Promise((resolve, reject) => {
56
+ const starting = (async () => {
57
+ const binary = await executable('opencode');
58
58
  // Keep the normal OS home and the user's existing OpenCode login.
59
59
  const config = JSON.parse(process.env.OPENCODE_CONFIG_CONTENT || '{}');
60
60
  const permission = typeof config.permission === 'string' ? { '*': config.permission } : config.permission;
61
61
  const env = { ...process.env, OPENCODE_CONFIG_CONTENT: JSON.stringify({
62
62
  ...config, permission: { ...permission, external_directory: 'deny' }
63
63
  }) };
64
- const child = spawn(binary, ['serve', '--hostname', '127.0.0.1', '--port', '0'], { stdio: ['ignore', 'pipe', 'ignore'], env });
65
- this.child = child;
66
- const timer = setTimeout(() => { child.kill(); reject(new SourceError('timeout', 'OpenCode server startup timed out')); }, 30000);
67
- let output = '';
68
- child.stdout.on('data', (chunk) => {
69
- output = (output + chunk.toString()).slice(-8192);
70
- const address = output.match(/http:\/\/127\.0\.0\.1:\d+/)?.[0];
71
- if (address) {
72
- clearTimeout(timer);
73
- resolve(address);
64
+ if (this.options.configFile) {
65
+ try {
66
+ const stat = await lstat(this.options.configFile);
67
+ if (!stat.isFile() || stat.isSymbolicLink() || stat.uid !== process.getuid?.() || (stat.mode & 0o777) !== 0o600)
68
+ throw new Error('Invalid private config');
69
+ // Signup may create this private grant after the daemon was started.
70
+ // Let the CLI read it; provider values never enter this process.
71
+ env.OPENCODE_CONFIG = this.options.configFile;
72
+ }
73
+ catch (error) {
74
+ if (error.code !== 'ENOENT')
75
+ throw new SourceError('logged_out', 'OpenCode private configuration is unavailable; rerun usegrunt setup');
74
76
  }
77
+ }
78
+ this.shutdown.signal.throwIfAborted();
79
+ return new Promise((resolve, reject) => {
80
+ const child = spawn(binary, ['serve', '--hostname', '127.0.0.1', '--port', '0'], { stdio: ['ignore', 'pipe', 'ignore'], env });
81
+ this.child = child;
82
+ const timer = setTimeout(() => { child.kill(); reject(new SourceError('timeout', 'OpenCode server startup timed out')); }, 30000);
83
+ let output = '';
84
+ child.stdout.on('data', (chunk) => {
85
+ output = (output + chunk.toString()).slice(-8192);
86
+ const address = output.match(/http:\/\/127\.0\.0\.1:\d+/)?.[0];
87
+ if (address) {
88
+ clearTimeout(timer);
89
+ resolve(address);
90
+ }
91
+ });
92
+ const failed = () => { clearTimeout(timer); this.starting = undefined; this.sessions.clear(); this.sessionModels.clear(); this.sessionParents.clear(); this.prepared.clear(); reject(new SourceError('upstream', 'OpenCode server stopped; run opencode serve to check installation')); };
93
+ child.once('error', failed);
94
+ child.once('exit', failed);
75
95
  });
76
- const failed = () => { clearTimeout(timer); this.starting = undefined; this.sessions.clear(); this.sessionModels.clear(); this.sessionParents.clear(); this.prepared.clear(); reject(new SourceError('upstream', 'OpenCode server stopped; run opencode serve to check installation')); };
77
- child.once('error', failed);
78
- child.once('exit', failed);
79
- });
80
- return this.starting;
96
+ })();
97
+ this.starting = starting;
98
+ void starting.catch(() => { if (this.starting === starting)
99
+ this.starting = undefined; });
100
+ return starting;
81
101
  }
82
102
  async complete(request, caller) {
83
103
  const signal = AbortSignal.any([caller, this.shutdown.signal, ...(this.options.timeout ? [AbortSignal.timeout(this.options.timeout)] : [])]);
@@ -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) {