usegrunt 0.6.0 → 0.6.2
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 +16 -511
- package/dist/local/fallback-setup.js +109 -86
- package/dist/local/fixtures/opencode-events.js +4 -0
- package/dist/local/freellmapi.js +6 -0
- package/dist/local/harnesses.js +1 -1
- package/dist/local/main.js +8 -6
- package/dist/local/native-router.js +9 -6
- package/dist/local/opencode.js +100 -8
- package/dist/local/parallel.js +20 -0
- package/dist/local/registry.js +35 -13
- package/dist/local/routing.js +5 -10
- package/dist/local/setup.js +100 -35
- package/dist/local/terminal.js +8 -0
- package/dist/local/verification-queue.js +31 -0
- package/dist/local/wizard.js +102 -74
- package/dist/registry/adapters.json +35 -5
- package/dist/registry.js +4 -3
- package/package.json +1 -1
|
@@ -2,7 +2,8 @@ import { spawn, execFile } from 'node:child_process';
|
|
|
2
2
|
import { promisify } from 'node:util';
|
|
3
3
|
import { mkdir, writeFile, access } from 'node:fs/promises';
|
|
4
4
|
import { join } from 'node:path';
|
|
5
|
-
import { BrowserSquire } from './setup.js';
|
|
5
|
+
import { BrowserSquire, setup } from './setup.js';
|
|
6
|
+
import { parallelMap } from './parallel.js';
|
|
6
7
|
import { OpenCode } from './opencode.js';
|
|
7
8
|
import { DOCTOR_TIMEOUT_MS } from './freebuff.js';
|
|
8
9
|
import { SourceError, Sources } from './source.js';
|
|
@@ -74,119 +75,141 @@ export async function setupFallbacks(options) {
|
|
|
74
75
|
if (error.code !== 'ENOENT')
|
|
75
76
|
throw error;
|
|
76
77
|
}
|
|
77
|
-
let
|
|
78
|
-
const connect = async () => { if (
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
await squire.connect();
|
|
83
|
-
connected = true;
|
|
84
|
-
} };
|
|
78
|
+
let connection;
|
|
79
|
+
const connect = () => connection ??= (async () => { if (options.connect)
|
|
80
|
+
await options.connect();
|
|
81
|
+
else if (owned)
|
|
82
|
+
await squire.connect(); })();
|
|
85
83
|
const install = async (binary, pkg) => {
|
|
86
84
|
try {
|
|
87
85
|
await options.run(binary, ['--version']);
|
|
88
86
|
}
|
|
89
87
|
catch {
|
|
90
|
-
await options.run('npm', ['install', '--global', pkg]
|
|
88
|
+
await options.run('npm', ['install', '--global', pkg]);
|
|
91
89
|
}
|
|
92
90
|
};
|
|
93
91
|
options.notify('Free routes run their own tools with your filesystem permissions in the working directory. Free model providers may retain prompts.');
|
|
94
92
|
try {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
await install('freebuff', 'freebuff');
|
|
98
|
-
// Older CLIs accept --version but cannot run the login diagnostic.
|
|
93
|
+
const tasks = {
|
|
94
|
+
freebuff: async () => {
|
|
99
95
|
try {
|
|
100
|
-
await
|
|
101
|
-
|
|
102
|
-
catch {
|
|
103
|
-
options.notify('Freebuff: repairing the installed CLI before checking sign-in');
|
|
96
|
+
await install('freebuff', 'freebuff');
|
|
97
|
+
// Older CLIs accept --version but cannot run the login diagnostic.
|
|
104
98
|
try {
|
|
105
|
-
await options.run('npm', ['install', '--global', 'freebuff@latest'], true);
|
|
106
99
|
await options.run('freebuff', ['doctor']);
|
|
107
100
|
}
|
|
108
101
|
catch {
|
|
109
|
-
|
|
102
|
+
options.notify('Freebuff: repairing the installed CLI before checking sign-in');
|
|
103
|
+
try {
|
|
104
|
+
await options.run('npm', ['install', '--global', 'freebuff@latest']);
|
|
105
|
+
await options.run('freebuff', ['doctor']);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
throw new FreebuffCompatibilityError('Freebuff CLI repair incomplete. The active freebuff on PATH must support doctor. Run npm install --global freebuff@latest, remove older Freebuff binaries earlier on PATH, then rerun usegrunt setup. Continuing with OpenCode.');
|
|
109
|
+
}
|
|
110
110
|
}
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
111
|
+
try {
|
|
112
|
+
await options.run('tmux', ['-V']);
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
if (process.platform === 'darwin')
|
|
116
|
+
await options.run('brew', ['install', 'tmux'], true);
|
|
117
|
+
else {
|
|
118
|
+
let manager;
|
|
119
|
+
for (const candidate of ['apt-get', 'dnf']) {
|
|
120
|
+
try {
|
|
121
|
+
await options.run(candidate, ['--version']);
|
|
122
|
+
manager = candidate;
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
catch { /* Try the next supported OS package manager. */ }
|
|
125
126
|
}
|
|
126
|
-
|
|
127
|
+
if (!manager)
|
|
128
|
+
throw new Error('Install tmux with your OS package manager');
|
|
129
|
+
const args = ['install', '-y', 'tmux'];
|
|
130
|
+
await options.run(process.getuid?.() === 0 ? manager : 'sudo', process.getuid?.() === 0 ? args : [manager, ...args], true);
|
|
127
131
|
}
|
|
128
|
-
if (!manager)
|
|
129
|
-
throw new Error('Install tmux with your OS package manager');
|
|
130
|
-
const args = ['install', '-y', 'tmux'];
|
|
131
|
-
await options.run(process.getuid?.() === 0 ? manager : 'sudo', process.getuid?.() === 0 ? args : [manager, ...args], true);
|
|
132
132
|
}
|
|
133
|
+
const signedIn = options.signedIn ?? freebuffSignedIn;
|
|
134
|
+
if (!await signedIn()) {
|
|
135
|
+
await connect();
|
|
136
|
+
if (options.login)
|
|
137
|
+
await options.login(squire);
|
|
138
|
+
else
|
|
139
|
+
await loginFreebuff(squire, options.signal);
|
|
140
|
+
}
|
|
141
|
+
if (!await signedIn())
|
|
142
|
+
throw new Error('Freebuff is still signed out; rerun setup to resume');
|
|
143
|
+
options.notify('Freebuff: signed in; grunt will start its loopback bridge with the background service');
|
|
133
144
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
await options.login(squire);
|
|
145
|
+
catch (error) {
|
|
146
|
+
unavailable.push('freebuff');
|
|
147
|
+
if (error instanceof FreebuffCompatibilityError)
|
|
148
|
+
options.notify(error.message);
|
|
139
149
|
else
|
|
140
|
-
|
|
150
|
+
options.notify('Freebuff setup incomplete. Run freebuff doctor, install tmux if missing, then rerun usegrunt setup. Continuing with OpenCode.');
|
|
141
151
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
options.notify('Freebuff: signed in; grunt will start its loopback bridge with the background service');
|
|
145
|
-
}
|
|
146
|
-
catch (error) {
|
|
147
|
-
unavailable.push('freebuff');
|
|
148
|
-
if (error instanceof FreebuffCompatibilityError)
|
|
149
|
-
options.notify(error.message);
|
|
150
|
-
else
|
|
151
|
-
options.notify('Freebuff setup incomplete. Run freebuff doctor, install tmux if missing, then rerun usegrunt setup. Continuing with OpenCode.');
|
|
152
|
-
}
|
|
153
|
-
if (!options.adapters || options.adapters.includes('opencode'))
|
|
154
|
-
try {
|
|
155
|
-
await install('opencode', 'opencode-ai');
|
|
156
|
-
const verify = options.verify ?? (() => verifyOpenCode(new OpenCode(), options.signal ? AbortSignal.any([options.signal, AbortSignal.timeout(120000)]) : AbortSignal.timeout(120000)));
|
|
152
|
+
},
|
|
153
|
+
opencode: async () => {
|
|
157
154
|
try {
|
|
158
|
-
await
|
|
155
|
+
await install('opencode', 'opencode-ai');
|
|
156
|
+
const verify = options.verify ?? (() => verifyOpenCode(new OpenCode(), options.signal ? AbortSignal.any([options.signal, AbortSignal.timeout(120000)]) : AbortSignal.timeout(120000)));
|
|
157
|
+
try {
|
|
158
|
+
await verify();
|
|
159
|
+
}
|
|
160
|
+
catch (error) {
|
|
161
|
+
if (!(error instanceof SourceError) || error.code !== 'logged_out')
|
|
162
|
+
throw error;
|
|
163
|
+
await connect();
|
|
164
|
+
const findCredential = async () => (await squire.credentials.list()).find(c => c.service?.trim().toLowerCase() === 'opencode'
|
|
165
|
+
&& c.auth_strategy !== 'username_password' && c.field_names.some(field => /^(?:api_?key|token|key)$/i.test(field)));
|
|
166
|
+
let credential = await findCredential();
|
|
167
|
+
if (!credential) {
|
|
168
|
+
await squire.signup('opencode', 'https://opencode.ai/auth', "Use the user's existing OpenCode account or sign up for free access. Use operate_extract to store an OpenCode Zen API key directly in this user's Squire vault under service opencode. Never include its value in your answer. No payment, paid plans, extra accounts or account switching. Stop if free access requires payment. Report human verification for browser handoff; do not bypass it.");
|
|
169
|
+
credential = await findCredential();
|
|
170
|
+
}
|
|
171
|
+
if (!credential)
|
|
172
|
+
throw new Error('No OpenCode access was created');
|
|
173
|
+
const grant = await squire.credentials.grant(credential.reference);
|
|
174
|
+
const path = join(options.directory, 'opencode-free.json');
|
|
175
|
+
await mkdir(options.directory, { recursive: true, mode: 0o700 });
|
|
176
|
+
await writeFile(path, JSON.stringify({ provider: { opencode: { options: { baseURL: grant.base_url.replace(/\/$/, '') + '/zen/v1', apiKey: grant.token } } } }), { mode: 0o600 });
|
|
177
|
+
process.env.OPENCODE_CONFIG = path;
|
|
178
|
+
await verify();
|
|
179
|
+
}
|
|
180
|
+
options.notify('OpenCode: free model answered');
|
|
159
181
|
}
|
|
160
|
-
catch
|
|
161
|
-
|
|
162
|
-
|
|
182
|
+
catch {
|
|
183
|
+
unavailable.push('opencode');
|
|
184
|
+
options.notify('OpenCode free model check failed or signup was refused. Rerun usegrunt setup after repairing OpenCode; other routes remain available.');
|
|
185
|
+
}
|
|
186
|
+
},
|
|
187
|
+
kilo: async () => {
|
|
188
|
+
try {
|
|
163
189
|
await connect();
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
throw new Error('No OpenCode access was created');
|
|
173
|
-
const grant = await squire.credentials.grant(credential.reference);
|
|
174
|
-
const path = join(options.directory, 'opencode-free.json');
|
|
175
|
-
await mkdir(options.directory, { recursive: true, mode: 0o700 });
|
|
176
|
-
await writeFile(path, JSON.stringify({ provider: { opencode: { options: { baseURL: grant.base_url.replace(/\/$/, '') + '/zen/v1', apiKey: grant.token } } } }), { mode: 0o600 });
|
|
177
|
-
process.env.OPENCODE_CONFIG = path;
|
|
178
|
-
await verify();
|
|
190
|
+
if (!squire.runs)
|
|
191
|
+
throw new Error('Provider signup requires Squire');
|
|
192
|
+
await (options.setupProvider ?? setup)(options.notify, ['kilo'], { squire: squire, ...(options.signal ? { signal: options.signal } : {}) });
|
|
193
|
+
options.notify('Kilo: free API access connected through Squire');
|
|
194
|
+
}
|
|
195
|
+
catch {
|
|
196
|
+
unavailable.push('kilo');
|
|
197
|
+
options.notify('Kilo setup incomplete. Rerun usegrunt setup to resume; other routes remain available.');
|
|
179
198
|
}
|
|
180
|
-
options.notify('OpenCode: free model answered');
|
|
181
|
-
}
|
|
182
|
-
catch {
|
|
183
|
-
unavailable.push('opencode');
|
|
184
|
-
options.notify('OpenCode free model check failed or signup was refused. Rerun usegrunt setup after repairing OpenCode; other routes remain available.');
|
|
185
199
|
}
|
|
186
|
-
|
|
200
|
+
};
|
|
201
|
+
const adapters = options.adapters ?? ['freebuff', 'opencode'];
|
|
202
|
+
await parallelMap(adapters, async (id) => {
|
|
203
|
+
const task = tasks[id];
|
|
204
|
+
if (!task)
|
|
205
|
+
throw new Error('Unsupported vetted adapter');
|
|
206
|
+
await task();
|
|
207
|
+
}, 3, options.signal);
|
|
208
|
+
options.signal?.throwIfAborted();
|
|
209
|
+
return { unavailable: adapters.filter(id => unavailable.includes(id)) };
|
|
187
210
|
}
|
|
188
211
|
finally {
|
|
189
|
-
if (owned &&
|
|
212
|
+
if (owned && connection)
|
|
190
213
|
await squire.close();
|
|
191
214
|
}
|
|
192
215
|
}
|
package/dist/local/freellmapi.js
CHANGED
|
@@ -67,6 +67,12 @@ export class FreeLLMAPI {
|
|
|
67
67
|
}
|
|
68
68
|
if (!response.ok)
|
|
69
69
|
throw new SourceError(response.status === 403 ? 'refused' : response.status === 401 ? 'logged_out' : response.status === 429 ? 'quota' : 'upstream', `FreeLLMAPI returned HTTP ${response.status}`);
|
|
70
|
+
// FreeLLMAPI treats a pinned model as a preference and can fall back to
|
|
71
|
+
// another provider. Never attribute that provider's answer to Kilo.
|
|
72
|
+
if (this.id === 'kilo' && response.headers.get('x-routed-via') !== `custom/${this.options.model}`) {
|
|
73
|
+
await response.body?.cancel();
|
|
74
|
+
throw new SourceError('upstream', 'Kilo did not answer; the local backend selected another route');
|
|
75
|
+
}
|
|
70
76
|
const result = await response.json();
|
|
71
77
|
const message = result.choices?.[0]?.message;
|
|
72
78
|
if (message?.refusal)
|
package/dist/local/harnesses.js
CHANGED
|
@@ -82,7 +82,7 @@ export async function configureOpenCode(port = 8788) {
|
|
|
82
82
|
catch {
|
|
83
83
|
try {
|
|
84
84
|
await access(join(directory, 'opencode.jsonc'));
|
|
85
|
-
throw new Error(`OpenCode uses ${join(directory, 'opencode.jsonc')}, which setup does not edit. Add the grunt provider by hand: see
|
|
85
|
+
throw new Error(`OpenCode uses ${join(directory, 'opencode.jsonc')}, which setup does not edit. Add the grunt provider by hand: see docs/reference.md`);
|
|
86
86
|
}
|
|
87
87
|
catch (error) {
|
|
88
88
|
if (error.code !== 'ENOENT')
|
package/dist/local/main.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import { knownAdapters } from '../registry.js';
|
|
2
3
|
import { realpathSync } from 'node:fs';
|
|
3
4
|
import { pathToFileURL } from 'node:url';
|
|
4
5
|
import { basename } from 'node:path';
|
|
@@ -32,8 +33,8 @@ export function configure(env = process.env, registry = () => bundledRegistry) {
|
|
|
32
33
|
const connection = { url, loadKey };
|
|
33
34
|
const dots = new Dots({ env, timeout: positive(env.MILO_DOTS_TIMEOUT_MS, 300000) });
|
|
34
35
|
const registered = [dots, new Codex(env.MILO_CODEX_MODEL ? { model: env.MILO_CODEX_MODEL } : {}), new FreeLLMAPI(connection), new FreeLLMAPI({ ...connection, id: 'kilo', model: 'kilo-auto/free' }), new OpenCode({ model: () => env.MILO_OPENCODE_MODEL ?? registry().adapters.find(a => a.id === 'opencode')?.model ?? 'big-pickle', ...(env.MILO_OPENCODE_URL ? { url: env.MILO_OPENCODE_URL } : {}) }), new Freebuff({ toolBridge: env.MILO_FREEBUFF_JSON_BRIDGE === '1' })];
|
|
35
|
-
const ids = env.MILO_SOURCES?.split(',').map(id => id.trim()) ?? [
|
|
36
|
-
const routing = ids.every(id =>
|
|
36
|
+
const ids = env.MILO_SOURCES?.split(',').map(id => id.trim()) ?? [...knownAdapters];
|
|
37
|
+
const routing = ids.every(id => knownAdapters.has(id)) ? { registry } : undefined;
|
|
37
38
|
return new Sources(ids.map(id => { const source = registered.find(s => s.id === id); if (!source)
|
|
38
39
|
throw new Error(`Unknown grunt source: ${id}`); return source; }), routing);
|
|
39
40
|
}
|
|
@@ -41,23 +42,24 @@ export function start(env = process.env) {
|
|
|
41
42
|
let registry = structuredClone(bundledRegistry);
|
|
42
43
|
const sources = configure(env, () => registry);
|
|
43
44
|
const directory = configDir(env);
|
|
45
|
+
let squire;
|
|
44
46
|
const sync = new RegistrySync({ directory, ...(env.GRUNT_REGISTRY_URL ? { url: env.GRUNT_REGISTRY_URL } : {}),
|
|
47
|
+
afterEnsure: async () => { await squire?.close(); squire = undefined; },
|
|
45
48
|
changed: current => { registry = current; }, notify: console.error,
|
|
46
49
|
ensure: async (adapter, signal) => {
|
|
47
50
|
if (!sources.sources.some(source => source.id === adapter.id))
|
|
48
51
|
return;
|
|
49
52
|
sources.preparing.add(adapter.id);
|
|
50
|
-
const
|
|
53
|
+
const client = squire ??= new BrowserSquire({ background: true, signal });
|
|
51
54
|
try {
|
|
52
|
-
const result = await setupFallbacks({ directory, run: (binary, args) => command(binary, args, false, signal), notify: console.error, adapters: [adapter.id], squire, signal,
|
|
53
|
-
connect: () =>
|
|
55
|
+
const result = await setupFallbacks({ directory, run: (binary, args) => command(binary, args, false, signal), notify: console.error, adapters: [adapter.id], squire: client, signal,
|
|
56
|
+
connect: () => client.connect() });
|
|
54
57
|
if (result.unavailable.length)
|
|
55
58
|
throw new Error('Adapter setup incomplete');
|
|
56
59
|
sources.healthy(adapter.id);
|
|
57
60
|
}
|
|
58
61
|
finally {
|
|
59
62
|
sources.preparing.delete(adapter.id);
|
|
60
|
-
await squire.close();
|
|
61
63
|
}
|
|
62
64
|
}
|
|
63
65
|
});
|
|
@@ -4,7 +4,9 @@ import { command, configDir, installBackgroundService } from './wizard.js';
|
|
|
4
4
|
import { loopback } from './freellmapi.js';
|
|
5
5
|
// Published FreeLLMAPI v0.16.24 source, pinned independently of grunt releases.
|
|
6
6
|
export const FREELLMAPI_REVISION = '4c8c2f834b587b6acc3f00698ad8f6468bea1aa2';
|
|
7
|
-
export async function ensureNativeRouter() {
|
|
7
|
+
export async function ensureNativeRouter(signal) {
|
|
8
|
+
signal?.throwIfAborted();
|
|
9
|
+
const run = (binary, args) => command(binary, args, false, signal);
|
|
8
10
|
if (!['linux', 'darwin'].includes(process.platform))
|
|
9
11
|
throw new Error('grunt setup requires Linux or macOS');
|
|
10
12
|
const directory = join(configDir(), 'freellmapi'), source = join(directory, 'source');
|
|
@@ -19,16 +21,16 @@ export async function ensureNativeRouter() {
|
|
|
19
21
|
catch { /* First install or interrupted build. */ }
|
|
20
22
|
if (!installed) {
|
|
21
23
|
const archive = join(directory, 'source.tar.gz');
|
|
22
|
-
const response = await fetch(`https://codeload.github.com/tashfeenahmed/freellmapi/tar.gz/${FREELLMAPI_REVISION}`, { signal: AbortSignal.timeout(120000) });
|
|
24
|
+
const response = await fetch(`https://codeload.github.com/tashfeenahmed/freellmapi/tar.gz/${FREELLMAPI_REVISION}`, { signal: AbortSignal.any([...(signal ? [signal] : []), AbortSignal.timeout(120000)]) });
|
|
23
25
|
if (!response.ok)
|
|
24
26
|
throw new Error('Cannot download the pinned FreeLLMAPI Node server');
|
|
25
27
|
await writeFile(archive, Buffer.from(await response.arrayBuffer()), { mode: 0o600 });
|
|
26
28
|
await mkdir(source, { recursive: true, mode: 0o700 });
|
|
27
|
-
await
|
|
29
|
+
await run('tar', ['-xzf', archive, '--strip-components=1', '-C', source]);
|
|
28
30
|
await rm(archive);
|
|
29
|
-
await
|
|
30
|
-
await
|
|
31
|
-
await
|
|
31
|
+
await run('npm', ['install', '--prefix', source, '--ignore-scripts', '--no-audit', '--no-fund']);
|
|
32
|
+
await run('npm', ['rebuild', '--prefix', source, 'better-sqlite3', 'sharp']);
|
|
33
|
+
await run(process.execPath, [join(source, 'node_modules', 'typescript', 'bin', 'tsc'), '--project', join(source, 'server', 'tsconfig.json')]);
|
|
32
34
|
await writeFile(join(directory, FREELLMAPI_REVISION), '', { mode: 0o600 });
|
|
33
35
|
}
|
|
34
36
|
const runner = join(directory, 'runner.mjs');
|
|
@@ -51,6 +53,7 @@ await import(pathToFileURL(process.env.MILO_ROUTER_ENTRY).href);
|
|
|
51
53
|
MILO_CONFIG_DIR: configDir(), MILO_ROUTER_ENTRY: entry,
|
|
52
54
|
HOST: '127.0.0.1', PORT: url.port || '80', NODE_ENV: 'production',
|
|
53
55
|
FREEAPI_DB_PATH: join(directory, 'router.db'),
|
|
56
|
+
PROVIDER_TIMEOUT_CUSTOM: '600000',
|
|
54
57
|
PATH: process.env.PATH || '/usr/local/bin:/usr/bin:/bin'
|
|
55
58
|
});
|
|
56
59
|
}
|
package/dist/local/opencode.js
CHANGED
|
@@ -12,6 +12,7 @@ export class OpenCode {
|
|
|
12
12
|
child;
|
|
13
13
|
starting;
|
|
14
14
|
sessions = new Map();
|
|
15
|
+
prepared = new Map();
|
|
15
16
|
busy = new Set();
|
|
16
17
|
shutdown = new AbortController();
|
|
17
18
|
constructor(options = {}) {
|
|
@@ -46,7 +47,12 @@ export class OpenCode {
|
|
|
46
47
|
const binary = await executable('opencode');
|
|
47
48
|
this.starting = new Promise((resolve, reject) => {
|
|
48
49
|
// Keep the normal OS home and the user's existing OpenCode login.
|
|
49
|
-
const
|
|
50
|
+
const config = JSON.parse(process.env.OPENCODE_CONFIG_CONTENT || '{}');
|
|
51
|
+
const permission = typeof config.permission === 'string' ? { '*': config.permission } : config.permission;
|
|
52
|
+
const env = { ...process.env, OPENCODE_CONFIG_CONTENT: JSON.stringify({
|
|
53
|
+
...config, permission: { ...permission, external_directory: 'deny' }
|
|
54
|
+
}) };
|
|
55
|
+
const child = spawn(binary, ['serve', '--hostname', '127.0.0.1', '--port', '0'], { stdio: ['ignore', 'pipe', 'ignore'], env });
|
|
50
56
|
this.child = child;
|
|
51
57
|
const timer = setTimeout(() => { child.kill(); reject(new SourceError('timeout', 'OpenCode server startup timed out')); }, 30000);
|
|
52
58
|
let output = '';
|
|
@@ -58,7 +64,7 @@ export class OpenCode {
|
|
|
58
64
|
resolve(address);
|
|
59
65
|
}
|
|
60
66
|
});
|
|
61
|
-
const failed = () => { clearTimeout(timer); this.starting = undefined; this.sessions.clear(); reject(new SourceError('upstream', 'OpenCode server stopped; run opencode serve to check installation')); };
|
|
67
|
+
const failed = () => { clearTimeout(timer); this.starting = undefined; this.sessions.clear(); this.prepared.clear(); reject(new SourceError('upstream', 'OpenCode server stopped; run opencode serve to check installation')); };
|
|
62
68
|
child.once('error', failed);
|
|
63
69
|
child.once('exit', failed);
|
|
64
70
|
});
|
|
@@ -72,28 +78,111 @@ export class OpenCode {
|
|
|
72
78
|
const key = (history) => createHash('sha256').update(JSON.stringify([cwd, history])).digest('hex');
|
|
73
79
|
const prior = this.sessions.get(key(turns)) ?? (turns.length > 1 ? this.sessions.get(key(turns.slice(0, -1))) : undefined);
|
|
74
80
|
const continuation = prior && !this.busy.has(prior) ? prior : undefined;
|
|
75
|
-
const signal = AbortSignal.any([caller, this.shutdown.signal, AbortSignal.timeout(this.options.timeout
|
|
81
|
+
const signal = AbortSignal.any([caller, this.shutdown.signal, ...(this.options.timeout ? [AbortSignal.timeout(this.options.timeout)] : [])]);
|
|
76
82
|
let session, base;
|
|
83
|
+
const events = new AbortController();
|
|
84
|
+
let reader;
|
|
85
|
+
let watching;
|
|
86
|
+
let guardFailed = false;
|
|
87
|
+
const turnSignal = AbortSignal.any([signal, events.signal]);
|
|
77
88
|
const headers = { 'Content-Type': 'application/json', 'x-opencode-directory': cwd,
|
|
78
89
|
...(process.env.OPENCODE_SERVER_PASSWORD ? { Authorization: `Basic ${Buffer.from(`${process.env.OPENCODE_SERVER_USERNAME ?? 'opencode'}:${process.env.OPENCODE_SERVER_PASSWORD}`).toString('base64')}` } : {}) };
|
|
79
|
-
const
|
|
80
|
-
const res = await this.fetcher(`${base}${path}`, { method: 'POST', headers, redirect: 'error', signal, body: JSON.stringify(body) });
|
|
90
|
+
const check = (res) => {
|
|
81
91
|
if (!res.ok) {
|
|
82
92
|
const header = res.headers.get('Retry-After');
|
|
83
93
|
const seconds = header === null ? NaN : /^\d+(?:\.\d+)?$/.test(header) ? Number(header) : (Date.parse(header) - Date.now()) / 1000;
|
|
84
94
|
throw new SourceError(res.status === 403 ? 'refused' : res.status === 401 ? 'logged_out' : res.status === 429 ? 'quota' : 'upstream', `OpenCode returned HTTP ${res.status}`, Number.isFinite(seconds) ? Math.max(1, seconds) : undefined);
|
|
85
95
|
}
|
|
96
|
+
};
|
|
97
|
+
const call = async (path, body) => {
|
|
98
|
+
const res = await this.fetcher(`${base}${path}`, { method: 'POST', headers, redirect: 'error', signal: turnSignal, body: JSON.stringify(body) });
|
|
99
|
+
check(res);
|
|
86
100
|
return await res.json();
|
|
87
101
|
};
|
|
88
102
|
try {
|
|
89
103
|
base = await this.server();
|
|
90
104
|
signal.throwIfAborted();
|
|
105
|
+
if (!this.options.url) {
|
|
106
|
+
// Personal MCP catalogs can fill the model context before a task starts.
|
|
107
|
+
// Disconnect them only in our own worker instance, once per directory.
|
|
108
|
+
// OpenCode's coding tools, saved login and config files stay available.
|
|
109
|
+
let prepared = this.prepared.get(cwd);
|
|
110
|
+
if (!prepared) {
|
|
111
|
+
prepared = (async () => {
|
|
112
|
+
const preparationSignal = AbortSignal.any([this.shutdown.signal, AbortSignal.timeout(30000)]);
|
|
113
|
+
const prepare = async (path, method = 'GET') => {
|
|
114
|
+
const response = await this.fetcher(`${base}${path}`, { method, headers, redirect: 'error', signal: preparationSignal });
|
|
115
|
+
check(response);
|
|
116
|
+
return await response.json();
|
|
117
|
+
};
|
|
118
|
+
const peers = await prepare('/mcp');
|
|
119
|
+
if (!peers || typeof peers !== 'object' || Array.isArray(peers))
|
|
120
|
+
throw new SourceError('upstream', 'OpenCode returned invalid MCP status');
|
|
121
|
+
await Promise.all(Object.entries(peers).filter(([, peer]) => peer?.status === 'connected').map(async ([name]) => {
|
|
122
|
+
if (await prepare(`/mcp/${encodeURIComponent(name)}/disconnect`, 'POST') !== true)
|
|
123
|
+
throw new SourceError('upstream', 'OpenCode could not prepare its worker tools');
|
|
124
|
+
}));
|
|
125
|
+
})();
|
|
126
|
+
this.prepared.set(cwd, prepared);
|
|
127
|
+
const pending = prepared;
|
|
128
|
+
void pending.catch(() => { if (this.prepared.get(cwd) === pending)
|
|
129
|
+
this.prepared.delete(cwd); });
|
|
130
|
+
}
|
|
131
|
+
let cancel;
|
|
132
|
+
const cancellation = new Promise((_, reject) => { cancel = () => reject(turnSignal.reason); });
|
|
133
|
+
turnSignal.addEventListener('abort', cancel, { once: true });
|
|
134
|
+
try {
|
|
135
|
+
turnSignal.throwIfAborted();
|
|
136
|
+
await Promise.race([prepared, cancellation]);
|
|
137
|
+
}
|
|
138
|
+
finally {
|
|
139
|
+
turnSignal.removeEventListener('abort', cancel);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
91
142
|
session = continuation ?? (await call('/session', { title: 'grunt' })).id;
|
|
92
143
|
if (!session || !/^[a-zA-Z0-9_-]+$/.test(session))
|
|
93
144
|
throw new SourceError('upstream', 'OpenCode returned an invalid session');
|
|
94
145
|
this.busy.add(session);
|
|
146
|
+
// OpenCode 1.18.31: subscribe before sending the message so a fast
|
|
147
|
+
// permission request cannot be missed, including on an existing server.
|
|
148
|
+
const stream = await this.fetcher(`${base}/event`, { headers, redirect: 'error', signal: turnSignal });
|
|
149
|
+
check(stream);
|
|
150
|
+
if (!stream.headers.get('content-type')?.includes('text/event-stream') || !stream.body) {
|
|
151
|
+
throw new SourceError('upstream', 'OpenCode permission event stream unavailable');
|
|
152
|
+
}
|
|
153
|
+
reader = stream.body.getReader();
|
|
154
|
+
watching = (async () => {
|
|
155
|
+
const decoder = new TextDecoder();
|
|
156
|
+
let buffer = '';
|
|
157
|
+
while (true) {
|
|
158
|
+
const chunk = await reader.read();
|
|
159
|
+
if (chunk.done)
|
|
160
|
+
throw new SourceError('upstream', 'OpenCode permission event stream closed');
|
|
161
|
+
buffer += decoder.decode(chunk.value, { stream: true });
|
|
162
|
+
let boundary;
|
|
163
|
+
while ((boundary = /\r?\n\r?\n/.exec(buffer))) {
|
|
164
|
+
const frame = buffer.slice(0, boundary.index);
|
|
165
|
+
buffer = buffer.slice(boundary.index + boundary[0].length);
|
|
166
|
+
const data = frame.split(/\r?\n/).filter(line => line.startsWith('data:')).map(line => line.slice(5).trimStart()).join('\n');
|
|
167
|
+
if (!data)
|
|
168
|
+
continue;
|
|
169
|
+
const event = JSON.parse(data);
|
|
170
|
+
if (event.type !== 'permission.asked')
|
|
171
|
+
continue;
|
|
172
|
+
const permission = event.properties;
|
|
173
|
+
if (typeof permission?.sessionID !== 'string')
|
|
174
|
+
throw new SourceError('upstream', 'OpenCode returned an invalid permission request');
|
|
175
|
+
if (permission.sessionID !== session)
|
|
176
|
+
continue;
|
|
177
|
+
if (typeof permission.id !== 'string' || !/^[a-zA-Z0-9_-]+$/.test(permission.id)) {
|
|
178
|
+
throw new SourceError('upstream', 'OpenCode returned an invalid permission request');
|
|
179
|
+
}
|
|
180
|
+
await call(`/permission/${permission.id}/reply`, { reply: 'reject' });
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
})().catch(error => { guardFailed = true; throw error; });
|
|
95
184
|
const modelID = typeof this.options.model === 'function' ? this.options.model() : this.options.model ?? 'big-pickle';
|
|
96
|
-
const reply = await call(`/session/${session}/message`, { model: { providerID: 'opencode', modelID }, parts: [{ type: 'text', text: prompt }] });
|
|
185
|
+
const reply = await Promise.race([watching, call(`/session/${session}/message`, { model: { providerID: 'opencode', modelID }, parts: [{ type: 'text', text: prompt }] })]);
|
|
97
186
|
const parts = reply.parts ?? [];
|
|
98
187
|
const text = parts.filter(p => p.type === 'text' && typeof p.text === 'string').map(p => p.text).join('\n');
|
|
99
188
|
if (reply.info?.error) {
|
|
@@ -110,9 +199,9 @@ export class OpenCode {
|
|
|
110
199
|
return { text };
|
|
111
200
|
}
|
|
112
201
|
catch (error) {
|
|
202
|
+
if ((signal.aborted || guardFailed) && session && base)
|
|
203
|
+
await this.fetcher(`${base}/session/${session}/abort`, { method: 'POST', headers, redirect: 'error', signal: AbortSignal.timeout(2000) }).catch(() => { });
|
|
113
204
|
if (signal.aborted) {
|
|
114
|
-
if (session && base)
|
|
115
|
-
await this.fetcher(`${base}/session/${session}/abort`, { method: 'POST', headers, redirect: 'error', signal: AbortSignal.timeout(2000) }).catch(() => { });
|
|
116
205
|
caller.throwIfAborted();
|
|
117
206
|
if (this.shutdown.signal.aborted)
|
|
118
207
|
throw new SourceError('cancelled', 'OpenCode request cancelled during shutdown');
|
|
@@ -123,6 +212,9 @@ export class OpenCode {
|
|
|
123
212
|
throw new SourceError('upstream', 'OpenCode request failed');
|
|
124
213
|
}
|
|
125
214
|
finally {
|
|
215
|
+
events.abort();
|
|
216
|
+
await reader?.cancel().catch(() => { });
|
|
217
|
+
await watching?.catch(() => { });
|
|
126
218
|
if (session)
|
|
127
219
|
this.busy.delete(session);
|
|
128
220
|
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Limit browser/build pressure as the vetted catalog grows. Wait for started
|
|
2
|
+
// jobs before rejecting so callers can safely close shared resources.
|
|
3
|
+
export async function parallelMap(items, task, limit = 3, signal) {
|
|
4
|
+
if (!Number.isInteger(limit) || limit < 1)
|
|
5
|
+
throw new Error('Invalid concurrency limit');
|
|
6
|
+
const results = new Array(items.length);
|
|
7
|
+
let next = 0;
|
|
8
|
+
const workers = Array.from({ length: Math.min(limit, items.length) }, async () => {
|
|
9
|
+
while (next < items.length) {
|
|
10
|
+
signal?.throwIfAborted();
|
|
11
|
+
const index = next++;
|
|
12
|
+
results[index] = await task(items[index]);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
const settled = await Promise.allSettled(workers);
|
|
16
|
+
const failed = settled.find(result => result.status === 'rejected');
|
|
17
|
+
if (failed?.status === 'rejected')
|
|
18
|
+
throw failed.reason;
|
|
19
|
+
return results;
|
|
20
|
+
}
|
package/dist/local/registry.js
CHANGED
|
@@ -2,7 +2,9 @@ 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
4
|
import { bundledRegistry, knownAdapters, validateRegistry } from '../registry.js';
|
|
5
|
+
import { parallelMap } from './parallel.js';
|
|
5
6
|
export const REGISTRY_INTERVAL = 24 * 60 * 60 * 1000;
|
|
7
|
+
const REGISTRY_URLS = ['https://api.grunt.run/v1/registry', 'https://milo-gateway.fly.dev/v1/registry'];
|
|
6
8
|
export class RegistrySync {
|
|
7
9
|
options;
|
|
8
10
|
current = structuredClone(bundledRegistry);
|
|
@@ -29,10 +31,25 @@ export class RegistrySync {
|
|
|
29
31
|
}
|
|
30
32
|
async refresh() {
|
|
31
33
|
try {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
let response;
|
|
35
|
+
for (const url of this.options.url ? [this.options.url] : REGISTRY_URLS) {
|
|
36
|
+
try {
|
|
37
|
+
response = await (this.options.fetcher ?? fetch)(url, {
|
|
38
|
+
headers: this.etag ? { 'If-None-Match': this.etag } : {}, redirect: 'error',
|
|
39
|
+
signal: AbortSignal.any([this.controller.signal, AbortSignal.timeout(10000)])
|
|
40
|
+
});
|
|
41
|
+
if (response.status === 200 || response.status === 304)
|
|
42
|
+
break;
|
|
43
|
+
await response.body?.cancel();
|
|
44
|
+
response = undefined;
|
|
45
|
+
}
|
|
46
|
+
catch {
|
|
47
|
+
if (this.controller.signal.aborted)
|
|
48
|
+
throw new Error('Registry sync cancelled');
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (!response)
|
|
52
|
+
throw new Error('Registry unavailable');
|
|
36
53
|
if (response.status === 200) {
|
|
37
54
|
const registry = validateRegistry(await response.json());
|
|
38
55
|
const etag = response.headers.get('ETag') ?? undefined;
|
|
@@ -57,15 +74,20 @@ export class RegistrySync {
|
|
|
57
74
|
}
|
|
58
75
|
if (this.controller.signal.aborted)
|
|
59
76
|
return;
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
77
|
+
try {
|
|
78
|
+
await parallelMap(this.enabled, async (adapter) => {
|
|
79
|
+
if (!this.options.ensure || this.controller.signal.aborted)
|
|
80
|
+
return;
|
|
81
|
+
try {
|
|
82
|
+
await this.options.ensure(adapter, this.controller.signal);
|
|
83
|
+
}
|
|
84
|
+
catch {
|
|
85
|
+
this.options.notify?.(`${adapter.id}: setup needs attention; run usegrunt setup to finish sign-in`);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
finally {
|
|
90
|
+
await this.options.afterEnsure?.();
|
|
69
91
|
}
|
|
70
92
|
}
|
|
71
93
|
async start() {
|