usegrunt 0.6.1 → 0.6.3

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
  #!/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';
@@ -7,8 +8,15 @@ import { createLocalApp } from './api.js';
7
8
  import { Freebuff, positive } from './freebuff.js';
8
9
  import { Sources } from './source.js';
9
10
  import { FreeLLMAPI } from './freellmapi.js';
11
+ import { Nvidia } from './nvidia.js';
12
+ import { Zro } from './zro.js';
13
+ import { TokenHarbor } from './tokenharbor.js';
14
+ import { RegistrySource } from './registry-source.js';
15
+ import { ManagedRouter } from './managed-router.js';
16
+ import { ConsentSource } from './consent-source.js';
17
+ import { readDataSharingConsent, requiresDataSharing } from './data-sharing.js';
10
18
  import { Codex } from './codex.js';
11
- import { OpenCode } from './opencode.js';
19
+ import { OpenCode, DEFAULT_OPENCODE_MODELS } from './opencode.js';
12
20
  import { Dots } from './dots.js';
13
21
  import { bundledRegistry } from '../registry.js';
14
22
  import { RegistrySync } from './registry.js';
@@ -31,33 +39,51 @@ export function configure(env = process.env, registry = () => bundledRegistry) {
31
39
  };
32
40
  const connection = { url, loadKey };
33
41
  const dots = new Dots({ env, timeout: positive(env.MILO_DOTS_TIMEOUT_MS, 300000) });
34
- 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()) ?? ['freebuff', 'opencode'];
36
- const routing = ids.every(id => ['freebuff', 'opencode'].includes(id)) ? { registry } : undefined;
37
- return new Sources(ids.map(id => { const source = registered.find(s => s.id === id); if (!source)
38
- throw new Error(`Unknown grunt source: ${id}`); return source; }), routing);
42
+ const opencodeModels = () => {
43
+ if (env.MILO_OPENCODE_MODEL)
44
+ return [env.MILO_OPENCODE_MODEL];
45
+ const adapter = registry().adapters.find(a => a.id === 'opencode');
46
+ // Older published registries pinned the original default. Upgrade that
47
+ // default while retaining explicit environment and custom-model overrides.
48
+ return adapter?.models ?? (adapter?.model && adapter.model !== 'big-pickle' ? [adapter.model] : DEFAULT_OPENCODE_MODELS);
49
+ };
50
+ 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 OpenCode({ models: opencodeModels, ...(env.MILO_OPENCODE_URL ? { url: env.MILO_OPENCODE_URL } : {}) }), new Freebuff({ toolBridge: env.MILO_FREEBUFF_JSON_BRIDGE === '1' })];
51
+ const ids = env.MILO_SOURCES?.split(',').map(id => id.trim()) ?? registry().adapters.filter(adapter => knownAdapters.has(adapter.id)).map(adapter => adapter.id);
52
+ const routing = ids.every(id => knownAdapters.has(id)) ? { registry } : undefined;
53
+ return new Sources(ids.map(id => {
54
+ const source = registered.find(source => source.id === id);
55
+ if (!source)
56
+ throw new Error(`Unknown grunt source: ${id}`);
57
+ const consent = requiresDataSharing(id) && id !== 'tokenharbor' ? new ConsentSource(source, configDir(env)) : source;
58
+ return !routing && knownAdapters.has(id) ? new RegistrySource(consent, registry) : consent;
59
+ }), routing);
39
60
  }
40
61
  export function start(env = process.env) {
41
62
  let registry = structuredClone(bundledRegistry);
42
63
  const sources = configure(env, () => registry);
43
64
  const directory = configDir(env);
65
+ let squire;
44
66
  const sync = new RegistrySync({ directory, ...(env.GRUNT_REGISTRY_URL ? { url: env.GRUNT_REGISTRY_URL } : {}),
67
+ afterEnsure: async () => { await squire?.close(); squire = undefined; },
45
68
  changed: current => { registry = current; }, notify: console.error,
46
69
  ensure: async (adapter, signal) => {
47
70
  if (!sources.sources.some(source => source.id === adapter.id))
48
71
  return;
72
+ const approved = await readDataSharingConsent(directory, [adapter.id]);
73
+ if (requiresDataSharing(adapter.id) && !approved.includes(adapter.id))
74
+ return;
49
75
  sources.preparing.add(adapter.id);
50
- const squire = new BrowserSquire({ background: true, signal });
76
+ const client = squire ??= new BrowserSquire({ background: true, signal, dataSharingApproved: approved });
77
+ client.approveDataSharing([...new Set([...(await readDataSharingConsent(directory, [...knownAdapters])), ...approved])]);
51
78
  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: () => squire.connect() });
79
+ const result = await setupFallbacks({ directory, run: (binary, args) => command(binary, args, false, signal), notify: console.error, adapters: [adapter.id], squire: client, signal, dataSharingApproved: approved,
80
+ connect: () => client.connect() });
54
81
  if (result.unavailable.length)
55
82
  throw new Error('Adapter setup incomplete');
56
83
  sources.healthy(adapter.id);
57
84
  }
58
85
  finally {
59
86
  sources.preparing.delete(adapter.id);
60
- await squire.close();
61
87
  }
62
88
  }
63
89
  });
@@ -68,14 +94,14 @@ export function start(env = process.env) {
68
94
  sources.preparing.add(adapter.id); });
69
95
  void ready.then(() => sync.start()).catch(() => console.error('Registry sync could not start; using the bundled list')).finally(() => sources.preparing.clear());
70
96
  // Warm the saved login without delaying the server; failed loads retry lazily.
71
- void sources.sources.find(source => source.id === 'freellmapi' || source.id === 'kilo')?.available();
97
+ void sources.sources.find(source => ['freellmapi', 'kilo'].includes(source.id))?.available();
72
98
  const port = positive(env.MILO_PORT, 8788);
73
99
  if (port > 65535)
74
100
  throw new Error('MILO_PORT must be at most 65535');
75
101
  const app = createLocalApp(sources, { port, dotsEnv: env, log: cwd => console.error(`grunt working directory: ${cwd}`) });
76
102
  const server = serve({ fetch: async (request) => { await ready; return app.fetch(request); }, hostname: HOST, port }, () => console.error(`grunt model API: http://${HOST}:${port}/v1`));
77
103
  // A distinct source list prevents FreeLLMAPI -> grunt -> FreeLLMAPI recursion.
78
- const fallback = env.MILO_FREEBUFF_BRIDGE !== '0' && sources.sources.some(s => s.id === 'freellmapi' || s.id === 'kilo') ? new Sources([new Freebuff({ toolBridge: env.MILO_FREEBUFF_JSON_BRIDGE === '1' })]) : undefined;
104
+ 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;
79
105
  const bridge = fallback ? serve({ fetch: createLocalApp(fallback, { port: 8789 }).fetch, hostname: HOST, port: 8789 }) : undefined;
80
106
  let closing = false;
81
107
  const close = async () => { if (closing)
@@ -0,0 +1,32 @@
1
+ import { guardRouterSharing } from './setup.js';
2
+ import { SourceError } from './source.js';
3
+ // The legacy backend can contain Grunt-created sharing providers alongside the
4
+ // user's custom rows. Enforce current consent without disabling custom sources.
5
+ export class ManagedRouter {
6
+ source;
7
+ url;
8
+ directory;
9
+ id;
10
+ supportsFunctionTools;
11
+ constructor(source, url, directory) {
12
+ this.source = source;
13
+ this.url = url;
14
+ this.directory = directory;
15
+ this.id = source.id;
16
+ if (source.supportsFunctionTools !== undefined)
17
+ this.supportsFunctionTools = source.supportsFunctionTools;
18
+ }
19
+ available() { return this.source.available(); }
20
+ async complete(request, signal) {
21
+ signal.throwIfAborted();
22
+ try {
23
+ await guardRouterSharing(this.url, this.directory);
24
+ }
25
+ catch {
26
+ throw new SourceError('refused', 'Cannot enforce saved router data-sharing choices; rerun usegrunt setup');
27
+ }
28
+ signal.throwIfAborted();
29
+ return this.source.complete(request, signal);
30
+ }
31
+ async close() { await this.source.close?.(); }
32
+ }
@@ -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 command('tar', ['-xzf', archive, '--strip-components=1', '-C', source]);
29
+ await run('tar', ['-xzf', archive, '--strip-components=1', '-C', source]);
28
30
  await rm(archive);
29
- await command('npm', ['install', '--prefix', source, '--ignore-scripts', '--no-audit', '--no-fund']);
30
- await command('npm', ['rebuild', '--prefix', source, 'better-sqlite3', 'sharp']);
31
- await command(process.execPath, [join(source, 'node_modules', 'typescript', 'bin', 'tsc'), '--project', join(source, 'server', 'tsconfig.json')]);
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');
@@ -0,0 +1,116 @@
1
+ import { constants } from 'node:fs';
2
+ import { mkdir, open, rename, rm, writeFile } from 'node:fs/promises';
3
+ import { homedir } from 'node:os';
4
+ import { join } from 'node:path';
5
+ import { randomUUID } from 'node:crypto';
6
+ import { SourceError } from './source.js';
7
+ export const NVIDIA_DEEPSEEK = 'deepseek-ai/deepseek-v4.1-flash';
8
+ const configDirectory = () => process.env.MILO_CONFIG_DIR || join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'milo');
9
+ function validateGrant(value) {
10
+ const grant = value;
11
+ if (!grant || typeof grant.base_url !== 'string' || typeof grant.token !== 'string' || !grant.token.trim())
12
+ throw new Error('Invalid NVIDIA Squire grant');
13
+ const url = new URL(grant.base_url);
14
+ if (url.protocol !== 'https:' || url.username || url.password || url.search || url.hash)
15
+ throw new Error('Invalid NVIDIA Squire endpoint');
16
+ return { base_url: grant.base_url.replace(/\/$/, ''), token: grant.token };
17
+ }
18
+ export async function readNvidiaGrant(directory = configDirectory()) {
19
+ const file = await open(join(directory, 'nvidia.json'), constants.O_RDONLY | constants.O_NOFOLLOW);
20
+ try {
21
+ const stat = await file.stat();
22
+ if (!stat.isFile() || (stat.mode & 0o777) !== 0o600 || stat.uid !== process.getuid?.())
23
+ throw new Error('Unsafe NVIDIA grant file');
24
+ return validateGrant(JSON.parse(await file.readFile('utf8')));
25
+ }
26
+ finally {
27
+ await file.close();
28
+ }
29
+ }
30
+ // Store only the revocable, host-scoped Squire grant, never the NVIDIA API key.
31
+ export async function writeNvidiaGrant(grant, directory = configDirectory()) {
32
+ const value = validateGrant(grant), temporary = join(directory, `nvidia.${randomUUID()}.tmp`);
33
+ await mkdir(directory, { recursive: true, mode: 0o700 });
34
+ try {
35
+ await writeFile(temporary, JSON.stringify(value), { mode: 0o600, flag: 'wx' });
36
+ await rename(temporary, join(directory, 'nvidia.json'));
37
+ }
38
+ finally {
39
+ await rm(temporary, { force: true });
40
+ }
41
+ }
42
+ export class Nvidia {
43
+ options;
44
+ id = 'nvidia';
45
+ supportsFunctionTools = true;
46
+ shutdown = new AbortController();
47
+ constructor(options = {}) {
48
+ this.options = options;
49
+ }
50
+ get fetcher() { return this.options.fetcher ?? fetch; }
51
+ async grant() { return validateGrant(await (this.options.loadGrant?.() ?? readNvidiaGrant(this.options.directory))); }
52
+ async available() {
53
+ try {
54
+ const grant = await this.grant();
55
+ const response = await this.fetcher(`${grant.base_url}/v1/models`, { headers: { Authorization: `Bearer ${grant.token}` }, redirect: 'error', signal: AbortSignal.timeout(5000) });
56
+ await response.body?.cancel();
57
+ return response.ok;
58
+ }
59
+ catch {
60
+ return false;
61
+ }
62
+ }
63
+ async healthCheck() { return this.available(); }
64
+ async complete(request, caller) {
65
+ caller.throwIfAborted();
66
+ let grant;
67
+ try {
68
+ grant = await this.grant();
69
+ }
70
+ catch {
71
+ throw new SourceError('logged_out', 'NVIDIA access is unavailable; rerun usegrunt setup');
72
+ }
73
+ if (request.tools.some(tool => tool.type !== 'function'))
74
+ throw new SourceError('upstream', 'NVIDIA Chat Completions requires function tools');
75
+ const signal = AbortSignal.any([caller, this.shutdown.signal, AbortSignal.timeout(this.options.timeout ?? 120000)]);
76
+ let response;
77
+ try {
78
+ // This grant is bound to integrate.api.nvidia.com. There is no local
79
+ // aggregator or secondary provider that could answer or spend paid quota.
80
+ response = await this.fetcher(`${grant.base_url}/v1/chat/completions`, {
81
+ method: 'POST', redirect: 'error', signal,
82
+ headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${grant.token}` },
83
+ body: JSON.stringify({ model: NVIDIA_DEEPSEEK, messages: request.messages, tools: request.tools.length ? request.tools : undefined,
84
+ stream: false, max_tokens: Math.min(4096, Math.max(1, request.maxTokens ?? 4096)) })
85
+ });
86
+ }
87
+ catch {
88
+ caller.throwIfAborted();
89
+ if (this.shutdown.signal.aborted)
90
+ throw new SourceError('cancelled', 'NVIDIA request cancelled during shutdown');
91
+ throw new SourceError(signal.aborted ? 'timeout' : 'upstream', 'NVIDIA endpoint is unavailable');
92
+ }
93
+ if (!response.ok) {
94
+ const header = response.headers.get('retry-after');
95
+ const seconds = header === null ? NaN : /^\d+(?:\.\d+)?$/.test(header) ? Number(header) : (Date.parse(header) - Date.now()) / 1000;
96
+ await response.body?.cancel();
97
+ throw new SourceError(response.status === 401 ? 'logged_out' : [402, 429].includes(response.status) ? 'quota' : response.status === 403 ? 'refused' : 'upstream', `NVIDIA returned HTTP ${response.status}`, Number.isFinite(seconds) ? Math.max(1, seconds) : undefined);
98
+ }
99
+ try {
100
+ const result = await response.json();
101
+ const message = result.choices?.[0]?.message;
102
+ if (message?.refusal)
103
+ throw new SourceError('refused', 'NVIDIA refused the request');
104
+ if (!message || (typeof message.content !== 'string' && !message.tool_calls?.length))
105
+ throw new SourceError('upstream', 'NVIDIA returned an invalid completion');
106
+ return { text: message.content ?? '', ...(message.tool_calls ? { calls: message.tool_calls } : {}), inputTokens: result.usage?.prompt_tokens ?? 0, outputTokens: result.usage?.completion_tokens ?? 0 };
107
+ }
108
+ catch (error) {
109
+ caller.throwIfAborted();
110
+ if (error instanceof SourceError)
111
+ throw error;
112
+ throw new SourceError('upstream', 'NVIDIA returned an invalid completion');
113
+ }
114
+ }
115
+ async close() { this.shutdown.abort(); }
116
+ }
@@ -6,12 +6,21 @@ import { userTurns } from './harness.js';
6
6
  import { executable } from './multiplexer.js';
7
7
  import { loopback } from './freellmapi.js';
8
8
  import { SourceError } from './source.js';
9
+ export const DEFAULT_OPENCODE_MODELS = ['deepseek-v4.1-flash-free', 'glm-5.3-flash-free', 'big-pickle'];
10
+ const MODEL_QUOTA_COOLDOWN_MS = 60000;
11
+ class UnavailableModel extends SourceError {
12
+ constructor() { super('upstream', 'OpenCode free model is unavailable'); }
13
+ }
9
14
  export class OpenCode {
10
15
  options;
11
16
  id = 'opencode';
12
17
  child;
13
18
  starting;
14
19
  sessions = new Map();
20
+ sessionModels = new Map();
21
+ sessionParents = new Map();
22
+ modelCooldowns = new Map();
23
+ nextModel = 0;
15
24
  prepared = new Map();
16
25
  busy = new Set();
17
26
  shutdown = new AbortController();
@@ -64,21 +73,70 @@ export class OpenCode {
64
73
  resolve(address);
65
74
  }
66
75
  });
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')); };
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')); };
68
77
  child.once('error', failed);
69
78
  child.once('exit', failed);
70
79
  });
71
80
  return this.starting;
72
81
  }
73
82
  async complete(request, caller) {
83
+ const signal = AbortSignal.any([caller, this.shutdown.signal, ...(this.options.timeout ? [AbortSignal.timeout(this.options.timeout)] : [])]);
84
+ signal.throwIfAborted();
85
+ const scalar = typeof this.options.model === 'function' ? this.options.model() : this.options.model;
86
+ const configured = typeof this.options.models === 'function' ? this.options.models() : this.options.models;
87
+ const models = [...new Set(scalar === undefined ? configured ?? DEFAULT_OPENCODE_MODELS : [scalar])];
88
+ if (!models.length || models.some(model => typeof model !== 'string' || !model.trim()))
89
+ throw new SourceError('upstream', 'OpenCode model pool is empty or invalid');
90
+ const turns = userTurns(request), cwd = await realpath(request.cwd);
91
+ const key = (history) => createHash('sha256').update(JSON.stringify([cwd, history])).digest('hex');
92
+ const prior = this.sessions.get(key(turns)) ?? (turns.length > 1 ? this.sessions.get(key(turns.slice(0, -1))) : undefined);
93
+ const sticky = prior && !this.busy.has(prior) ? this.sessionModels.get(prior) : undefined;
94
+ const start = sticky && models.includes(sticky) ? models.indexOf(sticky) : this.nextModel % models.length;
95
+ const candidates = [...models.slice(start), ...models.slice(0, start)];
96
+ const attempt = { retryable: false };
97
+ let last;
98
+ try {
99
+ for (const model of candidates) {
100
+ if ((this.modelCooldowns.get(model) ?? 0) > Date.now())
101
+ continue;
102
+ signal.throwIfAborted();
103
+ if (model !== sticky)
104
+ this.nextModel = (models.indexOf(model) + 1) % models.length;
105
+ try {
106
+ return await this.completeModel(request, caller, model, signal, attempt);
107
+ }
108
+ catch (error) {
109
+ if (signal.aborted || !(error instanceof SourceError) || (error.code !== 'quota' && !(error instanceof UnavailableModel)))
110
+ throw error;
111
+ last = error;
112
+ this.modelCooldowns.set(model, Date.now() + (error.retryAfter === undefined ? MODEL_QUOTA_COOLDOWN_MS : error.retryAfter * 1000));
113
+ if (!attempt.retryable)
114
+ throw error;
115
+ }
116
+ }
117
+ const retryAfter = Math.max(1, Math.ceil((Math.min(...models.map(model => this.modelCooldowns.get(model) ?? Date.now())) - Date.now()) / 1000));
118
+ throw new SourceError('quota', 'OpenCode free models are temporarily exhausted or unavailable', retryAfter, last?.output);
119
+ }
120
+ finally {
121
+ if (attempt.session)
122
+ this.busy.delete(attempt.session);
123
+ }
124
+ }
125
+ async completeModel(request, caller, modelID, signal, attempt) {
74
126
  const turns = userTurns(request), prompt = request.handoffPrompt ?? turns.at(-1);
75
127
  if (!prompt)
76
128
  throw new SourceError('upstream', 'No user text in this request');
77
129
  const cwd = await realpath(request.cwd);
78
130
  const key = (history) => createHash('sha256').update(JSON.stringify([cwd, history])).digest('hex');
79
131
  const prior = this.sessions.get(key(turns)) ?? (turns.length > 1 ? this.sessions.get(key(turns.slice(0, -1))) : undefined);
80
- const continuation = prior && !this.busy.has(prior) ? prior : undefined;
81
- const signal = AbortSignal.any([caller, this.shutdown.signal, ...(this.options.timeout ? [AbortSignal.timeout(this.options.timeout)] : [])]);
132
+ const continuation = attempt.session ?? (prior && !this.busy.has(prior) ? prior : undefined);
133
+ attempt.retryable = false;
134
+ // Reserve a continuation before any asynchronous server work, and keep the
135
+ // reservation across quota retries of this same user turn.
136
+ if (continuation) {
137
+ attempt.session = continuation;
138
+ this.busy.add(continuation);
139
+ }
82
140
  let session, base;
83
141
  const events = new AbortController();
84
142
  let reader;
@@ -142,6 +200,7 @@ export class OpenCode {
142
200
  session = continuation ?? (await call('/session', { title: 'grunt' })).id;
143
201
  if (!session || !/^[a-zA-Z0-9_-]+$/.test(session))
144
202
  throw new SourceError('upstream', 'OpenCode returned an invalid session');
203
+ attempt.session = session;
145
204
  this.busy.add(session);
146
205
  // OpenCode 1.18.31: subscribe before sending the message so a fast
147
206
  // permission request cannot be missed, including on an existing server.
@@ -181,8 +240,10 @@ export class OpenCode {
181
240
  }
182
241
  }
183
242
  })().catch(error => { guardFailed = true; throw error; });
184
- const modelID = typeof this.options.model === 'function' ? this.options.model() : this.options.model ?? 'big-pickle';
185
- const reply = await Promise.race([watching, call(`/session/${session}/message`, { model: { providerID: 'opencode', modelID }, parts: [{ type: 'text', text: prompt }] })]);
243
+ const reply = await Promise.race([watching, call(`/session/${session}/message`, {
244
+ ...(attempt.messageID ? { messageID: attempt.messageID } : {}),
245
+ model: { providerID: 'opencode', modelID }, parts: [{ ...(attempt.partID ? { id: attempt.partID } : {}), type: 'text', text: prompt }]
246
+ })]);
186
247
  const parts = reply.parts ?? [];
187
248
  const text = parts.filter(p => p.type === 'text' && typeof p.text === 'string').map(p => p.text).join('\n');
188
249
  if (reply.info?.error) {
@@ -191,11 +252,16 @@ export class OpenCode {
191
252
  throw new SourceError('logged_out', 'OpenCode free model requires sign-in');
192
253
  if (error.data?.statusCode === 429)
193
254
  throw new SourceError('quota', 'OpenCode free model quota exhausted', undefined, text);
255
+ if (error.data?.statusCode === 404 || error.data?.code === 'model_not_found' || ['ModelNotFoundError', 'ProviderModelNotFoundError'].includes(error.name))
256
+ throw new UnavailableModel();
194
257
  throw new SourceError('refused', 'OpenCode provider refused the request');
195
258
  }
196
259
  if (!text)
197
260
  throw new SourceError('upstream', 'OpenCode returned no assistant text');
198
261
  this.sessions.set(key(turns), session);
262
+ this.sessionModels.set(session, modelID);
263
+ if (typeof reply.info?.parentID === 'string')
264
+ this.sessionParents.set(session, reply.info.parentID);
199
265
  return { text };
200
266
  }
201
267
  catch (error) {
@@ -207,6 +273,35 @@ export class OpenCode {
207
273
  throw new SourceError('cancelled', 'OpenCode request cancelled during shutdown');
208
274
  throw new SourceError('timeout', 'OpenCode request timed out');
209
275
  }
276
+ if (error instanceof SourceError && (error.code === 'quota' || error instanceof UnavailableModel) && session && base) {
277
+ // A provider error can arrive after OpenCode has persisted the user
278
+ // prompt. Reuse that prompt's IDs rather than appending it again. Never
279
+ // replay a turn that already ran native tools, or whose state is unclear.
280
+ try {
281
+ const response = await this.fetcher(`${base}/session/${session}/message`, { headers, redirect: 'error', signal: turnSignal });
282
+ check(response);
283
+ const messages = await response.json();
284
+ if (Array.isArray(messages)) {
285
+ const userIndex = messages.findLastIndex(message => message.info?.role === 'user');
286
+ const user = messages[userIndex];
287
+ const usedTools = messages.slice(Math.max(0, userIndex)).some(message => message.parts?.some(part => part.type === 'tool'));
288
+ if (!usedTools && (messages.length === 0 || user?.info.id === this.sessionParents.get(session))) {
289
+ attempt.session = session;
290
+ delete attempt.messageID;
291
+ delete attempt.partID;
292
+ attempt.retryable = true;
293
+ }
294
+ else if (!usedTools && user?.parts?.length === 1 && user.parts[0]?.type === 'text' && user.parts[0].text === prompt
295
+ && /^msg[a-zA-Z0-9_-]+$/.test(user.info.id) && /^prt[a-zA-Z0-9_-]+$/.test(user.parts[0].id)) {
296
+ attempt.session = session;
297
+ attempt.messageID = user.info.id;
298
+ attempt.partID = user.parts[0].id;
299
+ attempt.retryable = true;
300
+ }
301
+ }
302
+ }
303
+ catch { /* Unknown persisted state is not safe to replay automatically. */ }
304
+ }
210
305
  if (error instanceof SourceError)
211
306
  throw error;
212
307
  throw new SourceError('upstream', 'OpenCode request failed');
@@ -215,8 +310,6 @@ export class OpenCode {
215
310
  events.abort();
216
311
  await reader?.cancel().catch(() => { });
217
312
  await watching?.catch(() => { });
218
- if (session)
219
- this.busy.delete(session);
220
313
  }
221
314
  }
222
315
  async close() {
@@ -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
+ }
@@ -0,0 +1,26 @@
1
+ import { SourceError } from './source.js';
2
+ // Mixed legacy/custom source lists do not use StickyRouter. Keep the current
3
+ // registry's revocation boundary around every known source in that path too.
4
+ export class RegistrySource {
5
+ source;
6
+ registry;
7
+ id;
8
+ supportsFunctionTools;
9
+ constructor(source, registry) {
10
+ this.source = source;
11
+ this.registry = registry;
12
+ this.id = source.id;
13
+ if (source.supportsFunctionTools !== undefined)
14
+ this.supportsFunctionTools = source.supportsFunctionTools;
15
+ }
16
+ listed() { return this.registry().adapters.some(adapter => adapter.id === this.id); }
17
+ async available() { return this.listed() && await this.source.available(); }
18
+ async healthCheck() { return this.listed() && await (this.source.healthCheck?.() ?? this.source.available()); }
19
+ complete(request, signal) {
20
+ signal.throwIfAborted();
21
+ if (!this.listed())
22
+ throw new SourceError('refused', `${this.id} is not in the current adapter registry`);
23
+ return this.source.complete(request, signal);
24
+ }
25
+ async close() { await this.source.close?.(); }
26
+ }
@@ -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
- const response = await (this.options.fetcher ?? fetch)(this.options.url ?? 'https://api.grunt.run/v1/registry', {
33
- headers: this.etag ? { 'If-None-Match': this.etag } : {}, redirect: 'error',
34
- signal: AbortSignal.any([this.controller.signal, AbortSignal.timeout(10000)])
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
- for (const adapter of this.enabled) {
61
- if (!this.options.ensure)
62
- continue;
63
- try {
64
- await this.options.ensure(adapter, this.controller.signal);
65
- }
66
- catch {
67
- this.options.notify?.(`${adapter.id}: setup needs attention; run usegrunt setup to finish sign-in`);
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() {