usegrunt 0.0.0-stage → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,107 @@
1
+ #!/usr/bin/env node
2
+ import { realpathSync } from 'node:fs';
3
+ import { pathToFileURL } from 'node:url';
4
+ import { basename } from 'node:path';
5
+ import { serve } from '@hono/node-server';
6
+ import { createLocalApp } from './api.js';
7
+ import { Freebuff, positive } from './freebuff.js';
8
+ import { Sources } from './source.js';
9
+ import { FreeLLMAPI } from './freellmapi.js';
10
+ import { Codex } from './codex.js';
11
+ import { OpenCode } from './opencode.js';
12
+ import { Dots } from './dots.js';
13
+ import { bundledRegistry } from '../registry.js';
14
+ import { RegistrySync } from './registry.js';
15
+ import { configDir, command } from './wizard.js';
16
+ import { setupFallbacks } from './fallback-setup.js';
17
+ import { BrowserSquire } from './setup.js';
18
+ export const HOST = '127.0.0.1';
19
+ export function configure(env = process.env, registry = () => bundledRegistry) {
20
+ const url = env.MILO_FREELLMAPI_URL ?? 'http://127.0.0.1:3001/v1';
21
+ let key = env.MILO_FREELLMAPI_KEY ?? '';
22
+ let pending;
23
+ const loadKey = async () => {
24
+ if (key)
25
+ return key;
26
+ pending ??= import('./setup.js').then(module => module.loadRouterKey(url)).then(value => key = value).catch(() => {
27
+ console.error('grunt router login unavailable or invalid; will retry on the next request. Check the local router and its saved login.');
28
+ return '';
29
+ }).finally(() => { pending = undefined; });
30
+ return pending;
31
+ };
32
+ const connection = { url, loadKey };
33
+ 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()];
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);
39
+ }
40
+ export function start(env = process.env) {
41
+ let registry = structuredClone(bundledRegistry);
42
+ const sources = configure(env, () => registry);
43
+ const directory = configDir(env);
44
+ const sync = new RegistrySync({ directory, ...(env.GRUNT_REGISTRY_URL ? { url: env.GRUNT_REGISTRY_URL } : {}),
45
+ changed: current => { registry = current; }, notify: console.error,
46
+ ensure: async (adapter, signal) => {
47
+ if (!sources.sources.some(source => source.id === adapter.id))
48
+ return;
49
+ const squire = new BrowserSquire({ background: true, signal });
50
+ try {
51
+ const result = await setupFallbacks({ directory, run: (binary, args) => command(binary, args, false, signal), notify: console.error, adapters: [adapter.id], squire, signal,
52
+ connect: () => squire.connect() });
53
+ if (result.unavailable.length)
54
+ throw new Error('Adapter setup incomplete');
55
+ sources.healthy(adapter.id);
56
+ }
57
+ finally {
58
+ await squire.close();
59
+ }
60
+ }
61
+ });
62
+ // Load the private cache before accepting tasks, without waiting for the network.
63
+ const ready = sync.load();
64
+ void ready.then(() => sync.start()).catch(() => console.error('Registry sync could not start; using the bundled list'));
65
+ // Warm the saved login without delaying the server; failed loads retry lazily.
66
+ void sources.sources.find(source => source.id === 'freellmapi' || source.id === 'kilo')?.available();
67
+ const port = positive(env.MILO_PORT, 8788);
68
+ if (port > 65535)
69
+ throw new Error('MILO_PORT must be at most 65535');
70
+ const app = createLocalApp(sources, { port, dotsEnv: env, log: cwd => console.error(`grunt working directory: ${cwd}`) });
71
+ const server = serve({ fetch: async (request) => { await ready; return app.fetch(request); }, hostname: HOST, port }, () => console.error(`grunt model API: http://${HOST}:${port}/v1`));
72
+ // A distinct source list prevents FreeLLMAPI -> grunt -> FreeLLMAPI recursion.
73
+ const fallback = env.MILO_FREEBUFF_BRIDGE !== '0' && sources.sources.some(s => s.id === 'freellmapi' || s.id === 'kilo') ? new Sources([new Freebuff()]) : undefined;
74
+ const bridge = fallback ? serve({ fetch: createLocalApp(fallback, { port: 8789 }).fetch, hostname: HOST, port: 8789 }) : undefined;
75
+ let closing = false;
76
+ const close = async () => { if (closing)
77
+ return; closing = true; await sync.close(); await sources.close(); await fallback?.close(); bridge?.close(); server.close(); };
78
+ process.once('SIGINT', close);
79
+ process.once('SIGTERM', close);
80
+ return { server, close };
81
+ }
82
+ if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
83
+ if (process.argv[2] === 'dots' && process.argv[3] === 'link') {
84
+ const { dotsLink } = await import('./dots-link.js');
85
+ try {
86
+ await dotsLink(process.argv[4]);
87
+ }
88
+ catch {
89
+ console.error('Dots pairing failed. Use the fallback code from the Allow page: usegrunt dots link <code>.');
90
+ process.exitCode = 1;
91
+ }
92
+ }
93
+ else if (['setup', 'add'].includes(process.argv[2] ?? '') || (!process.argv[2] && basename(process.argv[1]) === 'usegrunt' && process.env.GRUNT_DAEMON !== '1')) {
94
+ const { wizard } = await import('./wizard.js');
95
+ try {
96
+ if (process.argv[2] === 'add' && !process.argv[3])
97
+ throw new Error('Usage: usegrunt add <provider>');
98
+ await wizard(process.argv[2] === 'add' ? process.argv[3] : undefined);
99
+ }
100
+ catch (error) {
101
+ console.error(error instanceof Error ? error.message : 'grunt setup failed');
102
+ process.exitCode = 1;
103
+ }
104
+ }
105
+ else
106
+ start();
107
+ }
@@ -0,0 +1,62 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { execFile } from 'node:child_process';
3
+ import { promisify } from 'node:util';
4
+ import { access } from 'node:fs/promises';
5
+ import { constants } from 'node:fs';
6
+ import { delimiter, join } from 'node:path';
7
+ import { SourceError } from './source.js';
8
+ const exec = promisify(execFile);
9
+ export async function executable(name) {
10
+ for (const dir of (process.env.PATH ?? '').split(delimiter)) {
11
+ try {
12
+ const path = join(dir, name);
13
+ await access(path, constants.X_OK);
14
+ return path;
15
+ }
16
+ catch { /* next PATH entry */ }
17
+ }
18
+ throw new SourceError('not_installed', `${name} is not installed or executable on PATH`);
19
+ }
20
+ const quote = (value) => `'${value.replaceAll("'", "'\\''")}'`;
21
+ export class Tmux {
22
+ socket = `milo-local-${process.pid}-${randomUUID()}`;
23
+ closed = false;
24
+ launches = new Set();
25
+ async run(args, input) {
26
+ const command = exec(await executable('tmux'), args, { timeout: 5000, maxBuffer: 65536 });
27
+ if (input !== undefined)
28
+ command.child.stdin?.end(input);
29
+ return command;
30
+ }
31
+ async start(id, cwd, binary, continuation) {
32
+ // Keep the private server alive across empty-session handoffs. Closing the
33
+ // daemon kills this socket explicitly, without touching the user's server.
34
+ if (this.closed)
35
+ throw new SourceError('upstream', 'grunt multiplexer is closed');
36
+ const launch = this.run(['-L', this.socket, 'new-session', '-d', '-s', id, '-c', cwd, '-x', '180', '-y', '50', `${quote(binary)} --cwd ${quote(cwd)}${continuation ? ` --continue ${quote(continuation)}` : ''}`, ';', 'set-option', '-s', 'exit-empty', 'off']);
37
+ this.launches.add(launch);
38
+ try {
39
+ await launch;
40
+ }
41
+ finally {
42
+ this.launches.delete(launch);
43
+ }
44
+ }
45
+ async send(id, prompt) {
46
+ // Bracketed paste preserves multiline transcripts and avoids shell/key interpretation.
47
+ await this.run(['-L', this.socket, 'load-buffer', '-b', id, '-'], prompt);
48
+ try {
49
+ await this.run(['-L', this.socket, 'paste-buffer', '-p', '-b', id, '-t', `${id}:0.0`]);
50
+ }
51
+ finally {
52
+ await this.run(['-L', this.socket, 'delete-buffer', '-b', id]);
53
+ }
54
+ await this.run(['-L', this.socket, 'send-keys', '-t', `${id}:0.0`, 'Enter']);
55
+ }
56
+ async close() {
57
+ this.closed = true;
58
+ await Promise.allSettled([...this.launches]);
59
+ await this.run(['-L', this.socket, 'kill-server']).catch(() => { });
60
+ }
61
+ async stop(id) { await this.run(['-L', this.socket, 'kill-session', '-t', id]).catch(() => { }); }
62
+ }
@@ -0,0 +1,56 @@
1
+ import { access, mkdir, rm, writeFile } from 'node:fs/promises';
2
+ import { dirname, join } from 'node:path';
3
+ import { command, configDir, installBackgroundService } from './wizard.js';
4
+ import { loopback } from './freellmapi.js';
5
+ // Published FreeLLMAPI v0.16.24 source, pinned independently of grunt releases.
6
+ export const FREELLMAPI_REVISION = '4c8c2f834b587b6acc3f00698ad8f6468bea1aa2';
7
+ export async function ensureNativeRouter() {
8
+ if (!['linux', 'darwin'].includes(process.platform))
9
+ throw new Error('grunt setup requires Linux or macOS');
10
+ const directory = join(configDir(), 'freellmapi'), source = join(directory, 'source');
11
+ const entry = join(source, 'server', 'dist', 'index.js');
12
+ await mkdir(directory, { recursive: true, mode: 0o700 });
13
+ let installed = false;
14
+ try {
15
+ await access(join(directory, FREELLMAPI_REVISION));
16
+ await access(entry);
17
+ installed = true;
18
+ }
19
+ catch { /* First install or interrupted build. */ }
20
+ if (!installed) {
21
+ 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) });
23
+ if (!response.ok)
24
+ throw new Error('Cannot download the pinned FreeLLMAPI Node server');
25
+ await writeFile(archive, Buffer.from(await response.arrayBuffer()), { mode: 0o600 });
26
+ await mkdir(source, { recursive: true, mode: 0o700 });
27
+ await command('tar', ['-xzf', archive, '--strip-components=1', '-C', source]);
28
+ 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')]);
32
+ await writeFile(join(directory, FREELLMAPI_REVISION), '', { mode: 0o600 });
33
+ }
34
+ const runner = join(directory, 'runner.mjs');
35
+ // Only the backend reads this login; systemd/launchd files contain no secrets.
36
+ await writeFile(runner, `import { readFile, stat } from 'node:fs/promises';
37
+ import { join } from 'node:path';
38
+ import { pathToFileURL } from 'node:url';
39
+ process.umask(0o077);
40
+ const path = join(process.env.MILO_CONFIG_DIR, 'router-admin.json');
41
+ const metadata = await stat(path);
42
+ if (!metadata.isFile() || (metadata.mode & 0o777) !== 0o600 || metadata.uid !== process.getuid()) throw new Error('Invalid private router login permissions');
43
+ const admin = JSON.parse(await readFile(path, 'utf8'));
44
+ process.env.ENCRYPTION_KEY = admin.encryption;
45
+ process.env.FREEAPI_CONFIG_JSON = JSON.stringify({ admin: { email: 'milo@localhost.local', password: admin.password } });
46
+ await import(pathToFileURL(process.env.MILO_ROUTER_ENTRY).href);
47
+ `, { mode: 0o600 });
48
+ const url = loopback(process.env.MILO_FREELLMAPI_URL || 'http://127.0.0.1:3001/v1');
49
+ await mkdir(dirname(join(directory, 'router.db')), { recursive: true, mode: 0o700 });
50
+ await installBackgroundService('milo-router', runner, {
51
+ MILO_CONFIG_DIR: configDir(), MILO_ROUTER_ENTRY: entry,
52
+ HOST: '127.0.0.1', PORT: url.port || '80', NODE_ENV: 'production',
53
+ FREEAPI_DB_PATH: join(directory, 'router.db'),
54
+ PATH: process.env.PATH || '/usr/local/bin:/usr/bin:/bin'
55
+ });
56
+ }
@@ -0,0 +1,143 @@
1
+ import { spawn } from 'node:child_process';
2
+ import { createHash } from 'node:crypto';
3
+ import { tmpdir } from 'node:os';
4
+ import { realpath } from 'node:fs/promises';
5
+ import { userTurns } from './harness.js';
6
+ import { executable } from './multiplexer.js';
7
+ import { loopback } from './freellmapi.js';
8
+ import { SourceError } from './source.js';
9
+ export class OpenCode {
10
+ options;
11
+ id = 'opencode';
12
+ child;
13
+ starting;
14
+ sessions = new Map();
15
+ busy = new Set();
16
+ shutdown = new AbortController();
17
+ constructor(options = {}) {
18
+ this.options = options;
19
+ if (options.url)
20
+ loopback(options.url);
21
+ }
22
+ get fetcher() { return this.options.fetcher ?? fetch; }
23
+ async available() { if (this.options.url)
24
+ return true; try {
25
+ await executable('opencode');
26
+ return true;
27
+ }
28
+ catch {
29
+ return false;
30
+ } }
31
+ async healthCheck() {
32
+ // Executable discovery cannot prove that the upstream login has recovered.
33
+ try {
34
+ const result = await this.complete({ model: this.id, messages: [{ role: 'user', content: 'Reply OK. Do not use tools or edit files.' }], tools: [], cwd: tmpdir(), stream: false }, AbortSignal.timeout(30000));
35
+ return Boolean(result.text);
36
+ }
37
+ catch {
38
+ return false;
39
+ }
40
+ }
41
+ async server() {
42
+ if (this.options.url)
43
+ return this.options.url.replace(/\/$/, '');
44
+ if (this.starting)
45
+ return this.starting;
46
+ const binary = await executable('opencode');
47
+ this.starting = new Promise((resolve, reject) => {
48
+ // Keep the normal OS home and the user's existing OpenCode login.
49
+ const child = spawn(binary, ['serve', '--hostname', '127.0.0.1', '--port', '0'], { stdio: ['ignore', 'pipe', 'ignore'] });
50
+ this.child = child;
51
+ const timer = setTimeout(() => { child.kill(); reject(new SourceError('timeout', 'OpenCode server startup timed out')); }, 30000);
52
+ let output = '';
53
+ child.stdout.on('data', (chunk) => {
54
+ output = (output + chunk.toString()).slice(-8192);
55
+ const address = output.match(/http:\/\/127\.0\.0\.1:\d+/)?.[0];
56
+ if (address) {
57
+ clearTimeout(timer);
58
+ resolve(address);
59
+ }
60
+ });
61
+ const failed = () => { clearTimeout(timer); this.starting = undefined; this.sessions.clear(); reject(new SourceError('upstream', 'OpenCode server stopped; run opencode serve to check installation')); };
62
+ child.once('error', failed);
63
+ child.once('exit', failed);
64
+ });
65
+ return this.starting;
66
+ }
67
+ async complete(request, caller) {
68
+ const turns = userTurns(request), prompt = request.handoffPrompt ?? turns.at(-1);
69
+ if (!prompt)
70
+ throw new SourceError('upstream', 'No user text in this request');
71
+ const cwd = await realpath(request.cwd);
72
+ const key = (history) => createHash('sha256').update(JSON.stringify([cwd, history])).digest('hex');
73
+ const prior = this.sessions.get(key(turns)) ?? (turns.length > 1 ? this.sessions.get(key(turns.slice(0, -1))) : undefined);
74
+ const continuation = prior && !this.busy.has(prior) ? prior : undefined;
75
+ const signal = AbortSignal.any([caller, this.shutdown.signal, AbortSignal.timeout(this.options.timeout ?? 90000)]);
76
+ let session, base;
77
+ const headers = { 'Content-Type': 'application/json', 'x-opencode-directory': cwd,
78
+ ...(process.env.OPENCODE_SERVER_PASSWORD ? { Authorization: `Basic ${Buffer.from(`${process.env.OPENCODE_SERVER_USERNAME ?? 'opencode'}:${process.env.OPENCODE_SERVER_PASSWORD}`).toString('base64')}` } : {}) };
79
+ const call = async (path, body) => {
80
+ const res = await this.fetcher(`${base}${path}`, { method: 'POST', headers, redirect: 'error', signal, body: JSON.stringify(body) });
81
+ if (!res.ok) {
82
+ const header = res.headers.get('Retry-After');
83
+ const seconds = header === null ? NaN : /^\d+(?:\.\d+)?$/.test(header) ? Number(header) : (Date.parse(header) - Date.now()) / 1000;
84
+ 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
+ }
86
+ return await res.json();
87
+ };
88
+ try {
89
+ base = await this.server();
90
+ signal.throwIfAborted();
91
+ session = continuation ?? (await call('/session', { title: 'grunt' })).id;
92
+ if (!session || !/^[a-zA-Z0-9_-]+$/.test(session))
93
+ throw new SourceError('upstream', 'OpenCode returned an invalid session');
94
+ this.busy.add(session);
95
+ 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 }] });
97
+ const parts = reply.parts ?? [];
98
+ const text = parts.filter(p => p.type === 'text' && typeof p.text === 'string').map(p => p.text).join('\n');
99
+ if (reply.info?.error) {
100
+ const error = reply.info.error;
101
+ if (error.name === 'ProviderAuthError' || error.data?.statusCode === 401)
102
+ throw new SourceError('logged_out', 'OpenCode free model requires sign-in');
103
+ if (error.data?.statusCode === 429)
104
+ throw new SourceError('quota', 'OpenCode free model quota exhausted', undefined, text);
105
+ throw new SourceError('refused', 'OpenCode provider refused the request');
106
+ }
107
+ if (!text)
108
+ throw new SourceError('upstream', 'OpenCode returned no assistant text');
109
+ this.sessions.set(key(turns), session);
110
+ return { text };
111
+ }
112
+ catch (error) {
113
+ 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
+ caller.throwIfAborted();
117
+ if (this.shutdown.signal.aborted)
118
+ throw new SourceError('cancelled', 'OpenCode request cancelled during shutdown');
119
+ throw new SourceError('timeout', 'OpenCode request timed out');
120
+ }
121
+ if (error instanceof SourceError)
122
+ throw error;
123
+ throw new SourceError('upstream', 'OpenCode request failed');
124
+ }
125
+ finally {
126
+ if (session)
127
+ this.busy.delete(session);
128
+ }
129
+ }
130
+ async close() {
131
+ this.shutdown.abort();
132
+ const child = this.child;
133
+ if (!child || child.exitCode !== null || child.signalCode !== null)
134
+ return;
135
+ await new Promise(resolve => {
136
+ // Some CLI versions keep their idle server alive after SIGTERM. Setup
137
+ // must release its probe process before it can exit or try sign-in again.
138
+ const timer = setTimeout(() => child.kill('SIGKILL'), 2000);
139
+ child.once('exit', () => { clearTimeout(timer); resolve(); });
140
+ child.kill();
141
+ });
142
+ }
143
+ }
@@ -0,0 +1,81 @@
1
+ import { mkdir, readFile, writeFile, rename, rm } from 'node:fs/promises';
2
+ import { join } from 'node:path';
3
+ import { randomUUID } from 'node:crypto';
4
+ import { bundledRegistry, knownAdapters, validateRegistry } from '../registry.js';
5
+ export const REGISTRY_INTERVAL = 24 * 60 * 60 * 1000;
6
+ export class RegistrySync {
7
+ options;
8
+ current = structuredClone(bundledRegistry);
9
+ etag;
10
+ timer;
11
+ pending;
12
+ controller = new AbortController();
13
+ constructor(options) {
14
+ this.options = options;
15
+ }
16
+ get enabled() { return this.current.adapters.filter(adapter => knownAdapters.has(adapter.id)); }
17
+ async load() {
18
+ try {
19
+ const saved = JSON.parse(await readFile(join(this.options.directory, 'registry.json'), 'utf8'));
20
+ this.current = validateRegistry(saved.registry);
21
+ this.etag = typeof saved.etag === 'string' ? saved.etag : undefined;
22
+ }
23
+ catch { /* A missing or damaged cache uses the packaged registry. */ }
24
+ this.options.changed?.(this.current);
25
+ }
26
+ sync() {
27
+ this.pending ??= this.refresh().finally(() => { this.pending = undefined; });
28
+ return this.pending;
29
+ }
30
+ async refresh() {
31
+ 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
+ });
36
+ if (response.status === 200) {
37
+ const registry = validateRegistry(await response.json());
38
+ const etag = response.headers.get('ETag') ?? undefined;
39
+ const temporary = join(this.options.directory, `registry.${randomUUID()}.tmp`);
40
+ try {
41
+ await mkdir(this.options.directory, { recursive: true, mode: 0o700 });
42
+ await writeFile(temporary, JSON.stringify({ registry, etag }), { mode: 0o600, flag: 'wx' });
43
+ await rename(temporary, join(this.options.directory, 'registry.json'));
44
+ }
45
+ finally {
46
+ await rm(temporary, { force: true });
47
+ }
48
+ this.current = registry;
49
+ this.etag = etag;
50
+ this.options.changed?.(registry);
51
+ }
52
+ else if (response.status !== 304)
53
+ throw new Error('Registry unavailable');
54
+ }
55
+ catch {
56
+ this.options.notify?.('Registry update unavailable; keeping the last good list');
57
+ }
58
+ if (this.controller.signal.aborted)
59
+ 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
+ }
69
+ }
70
+ }
71
+ async start() {
72
+ await this.load();
73
+ if (this.controller.signal.aborted)
74
+ return;
75
+ this.timer = setInterval(() => { void this.sync(); }, REGISTRY_INTERVAL);
76
+ this.timer.unref();
77
+ await this.sync();
78
+ }
79
+ async close() { if (this.timer)
80
+ clearInterval(this.timer); this.controller.abort(); await this.pending; }
81
+ }
@@ -0,0 +1,138 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { execFile } from 'node:child_process';
3
+ import { promisify } from 'node:util';
4
+ import { realpath } from 'node:fs/promises';
5
+ import { userTurns } from './harness.js';
6
+ import { SourceError } from './source.js';
7
+ import { knownAdapters } from '../registry.js';
8
+ const exec = promisify(execFile);
9
+ export class StickyRouter {
10
+ sources;
11
+ options;
12
+ tasks = new Map();
13
+ bars = new Map();
14
+ constructor(sources, options) {
15
+ this.sources = sources;
16
+ this.options = options;
17
+ }
18
+ now() { return (this.options.now ?? Date.now)(); }
19
+ needsHealthCheck(id) { return this.bars.get(id) === Infinity; }
20
+ healthy(id) { if (this.bars.get(id) === Infinity)
21
+ this.bars.delete(id); }
22
+ listed(id) { return knownAdapters.has(id) && this.options.registry().adapters.some(a => a.id === id); }
23
+ async git(cwd, args) {
24
+ return (await exec('git', ['-C', cwd, ...args], { timeout: 10000, maxBuffer: 1024 * 1024 })).stdout.trim();
25
+ }
26
+ async createTask(request) {
27
+ const task = { failures: 0, original: userTurns(request).at(-1) ?? '', output: '', switched: false, busy: false };
28
+ try {
29
+ task.checkpoint = await this.git(request.cwd, ['stash', 'create']) || await this.git(request.cwd, ['rev-parse', 'HEAD']);
30
+ }
31
+ catch { /* Non-git and unborn directories use prompt/output handoff only. */ }
32
+ return task;
33
+ }
34
+ async handoff(request, task) {
35
+ let work = '';
36
+ if (task.checkpoint) {
37
+ try {
38
+ const status = await this.git(request.cwd, ['status', '--short']);
39
+ const diff = await this.git(request.cwd, ['diff', task.checkpoint, '--']);
40
+ work = `\nWorking tree status:\n${status}\nChanges since task start:\n${diff}\n`;
41
+ }
42
+ catch {
43
+ work = '\nWorking tree snapshot unavailable. Inspect the current files before continuing.\n';
44
+ }
45
+ }
46
+ const prompt = `Original task:\n${task.original}\n${work}\nPrevious adapter output (last 4 KB):\n${Buffer.from(task.output).subarray(-4096).toString('utf8')}\nContinue this task from the current state. Do not restart.`;
47
+ return { ...request, messages: [...request.messages, { role: 'user', content: prompt }], handoffPrompt: prompt };
48
+ }
49
+ exhausted() {
50
+ const times = [...this.bars.values()].filter(Number.isFinite);
51
+ const seconds = times.length ? Math.max(1, Math.ceil((Math.min(...times) - this.now()) / 1000)) : undefined;
52
+ return new SourceError('quota', 'No eligible adapter is available; finish setup or retry after quota resets', seconds);
53
+ }
54
+ async complete(request, signal) {
55
+ signal.throwIfAborted();
56
+ const cwd = await realpath(request.cwd);
57
+ const key = createHash('sha256').update(JSON.stringify([cwd, userTurns(request)])).digest('hex');
58
+ let task = this.tasks.get(key);
59
+ if (!task) {
60
+ // Reserve the task before asynchronous checkpoint creation to avoid two bindings.
61
+ task = { failures: 0, original: '', output: '', switched: false, busy: true };
62
+ this.tasks.set(key, task);
63
+ Object.assign(task, await this.createTask({ ...request, cwd }), { busy: true });
64
+ }
65
+ else {
66
+ if (task.busy)
67
+ throw new SourceError('upstream', 'A request for this task is already running; retry when it finishes');
68
+ task.busy = true;
69
+ }
70
+ try {
71
+ for (;;) {
72
+ signal.throwIfAborted();
73
+ const adapters = this.options.registry().adapters.filter(a => knownAdapters.has(a.id) && a.modalities.includes('text') && (!request.imageInput || a.modalities.includes('image')));
74
+ const eligible = [];
75
+ for (const source of this.sources) {
76
+ const adapter = adapters.find(a => a.id === source.id);
77
+ if (!task.source && this.sources.some(s => s.id === request.model) && source.id !== request.model)
78
+ continue;
79
+ if (!adapter || (this.bars.get(source.id) ?? 0) > this.now())
80
+ continue;
81
+ if (source.id === task.source || await source.available().catch(() => false))
82
+ eligible.push({ source, weight: adapter.weight });
83
+ }
84
+ signal.throwIfAborted();
85
+ let selected = eligible.find(candidate => candidate.source.id === task.source);
86
+ if (!selected) {
87
+ if (!eligible.length)
88
+ throw this.exhausted();
89
+ let draw = (this.options.random ?? Math.random)() * eligible.reduce((total, a) => total + a.weight, 0);
90
+ selected = eligible.find(a => (draw -= a.weight) < 0) ?? eligible.at(-1);
91
+ if (task.source)
92
+ task.switched = true;
93
+ task.source = selected.source.id;
94
+ task.failures = 0;
95
+ }
96
+ try {
97
+ const input = task.switched ? await this.handoff({ ...request, cwd }, task) : { ...request, cwd };
98
+ const result = await selected.source.complete(input, signal);
99
+ task.failures = 0;
100
+ task.output = Buffer.from(result.text).subarray(-4096).toString('utf8');
101
+ task.switched = false;
102
+ return result;
103
+ }
104
+ catch (error) {
105
+ signal.throwIfAborted();
106
+ const failure = error instanceof SourceError ? error : new SourceError('upstream', 'Adapter request failed');
107
+ if (failure.output)
108
+ task.output = failure.output;
109
+ if (failure.code === 'cancelled' || failure.code === 'refused')
110
+ throw failure;
111
+ let until;
112
+ if (failure.code === 'quota')
113
+ until = this.now() + (failure.retryAfter ?? 3600) * 1000;
114
+ else if (failure.code === 'logged_out' || failure.code === 'not_installed')
115
+ until = Infinity;
116
+ else {
117
+ task.failures++;
118
+ if (task.failures < 3)
119
+ throw failure;
120
+ until = this.now() + 15 * 60 * 1000;
121
+ }
122
+ this.bars.set(selected.source.id, until);
123
+ task.switched = true;
124
+ }
125
+ }
126
+ }
127
+ finally {
128
+ task.busy = false;
129
+ if (this.tasks.size > 1024)
130
+ for (const [oldKey, oldTask] of this.tasks) {
131
+ if (oldKey !== key && !oldTask.busy) {
132
+ this.tasks.delete(oldKey);
133
+ break;
134
+ }
135
+ }
136
+ }
137
+ }
138
+ }