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.
- package/README.md +463 -2
- package/dist/agent.js +292 -0
- package/dist/chat.html +39 -0
- package/dist/config.js +29 -0
- package/dist/dots-events.js +141 -0
- package/dist/dots-oauth.js +179 -0
- package/dist/dots-relay.js +143 -0
- package/dist/dots-store.js +70 -0
- package/dist/index.js +64 -0
- package/dist/local/api.js +196 -0
- package/dist/local/approval-proxy.js +71 -0
- package/dist/local/codex.js +115 -0
- package/dist/local/dots-link.js +110 -0
- package/dist/local/dots.js +87 -0
- package/dist/local/fallback-setup.js +191 -0
- package/dist/local/fixtures/ownership-mux.js +42 -0
- package/dist/local/freebuff.js +300 -0
- package/dist/local/freellmapi.js +77 -0
- package/dist/local/harness.js +25 -0
- package/dist/local/main.js +107 -0
- package/dist/local/multiplexer.js +62 -0
- package/dist/local/native-router.js +56 -0
- package/dist/local/opencode.js +143 -0
- package/dist/local/registry.js +81 -0
- package/dist/local/routing.js +138 -0
- package/dist/local/setup.js +523 -0
- package/dist/local/source.js +105 -0
- package/dist/local/wizard.js +258 -0
- package/dist/openrouter.js +15 -0
- package/dist/registry/adapters.json +10 -0
- package/dist/registry-routes.js +25 -0
- package/dist/registry.js +54 -0
- package/dist/router.js +37 -0
- package/dist/server.js +30 -0
- package/dist/store.js +56 -0
- package/dist/types.js +1 -0
- package/package.json +50 -4
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { spawn, execFile } from 'node:child_process';
|
|
2
|
+
import { promisify } from 'node:util';
|
|
3
|
+
import { mkdir, writeFile, access } from 'node:fs/promises';
|
|
4
|
+
import { join } from 'node:path';
|
|
5
|
+
import { BrowserSquire } from './setup.js';
|
|
6
|
+
import { OpenCode } from './opencode.js';
|
|
7
|
+
import { SourceError, Sources } from './source.js';
|
|
8
|
+
const exec = promisify(execFile);
|
|
9
|
+
class FreebuffCompatibilityError extends Error {
|
|
10
|
+
}
|
|
11
|
+
// The CLI keeps the local login exchange alive while Squire completes its URL.
|
|
12
|
+
export async function loginFreebuff(squire, signal) {
|
|
13
|
+
const child = spawn('freebuff', ['login'], { stdio: ['ignore', 'pipe', 'pipe'], ...(signal ? { signal } : {}) });
|
|
14
|
+
let output = '', started = false;
|
|
15
|
+
let browser;
|
|
16
|
+
let rejectLogin = () => { };
|
|
17
|
+
const exited = new Promise((resolve, reject) => {
|
|
18
|
+
rejectLogin = reject;
|
|
19
|
+
child.once('error', () => reject(new Error('Freebuff login could not start')));
|
|
20
|
+
child.once('exit', code => code === 0 ? resolve() : reject(new Error('Freebuff login did not complete')));
|
|
21
|
+
});
|
|
22
|
+
const capture = (chunk) => {
|
|
23
|
+
output = (output + chunk.toString()).slice(-16384);
|
|
24
|
+
const url = output.match(/https:\/\/freebuff\.com\/login\?[^\s\x1b]+/)?.[0];
|
|
25
|
+
if (started || !url)
|
|
26
|
+
return;
|
|
27
|
+
started = true;
|
|
28
|
+
browser = squire.signup('freebuff', url, "Complete this Freebuff CLI login using the user's existing account, or sign up for the free plan if none exists. This URL pairs the local CLI; finish that pairing. No paid plan, payment method, extra account or account switching. Stop on refusal; report captcha, passkey or email verification for human handoff. Do not bypass verification.");
|
|
29
|
+
void browser.catch(() => rejectLogin(new Error('Freebuff browser signup did not complete; rerun setup to resume')));
|
|
30
|
+
};
|
|
31
|
+
child.stdout.on('data', capture);
|
|
32
|
+
child.stderr.on('data', capture);
|
|
33
|
+
const timer = setTimeout(() => rejectLogin(new Error('Freebuff login timed out; rerun setup to resume')), 600000);
|
|
34
|
+
try {
|
|
35
|
+
await exited;
|
|
36
|
+
if (!started)
|
|
37
|
+
throw new Error('Freebuff did not provide a browser login URL');
|
|
38
|
+
await browser;
|
|
39
|
+
}
|
|
40
|
+
finally {
|
|
41
|
+
clearTimeout(timer);
|
|
42
|
+
child.kill();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
export async function freebuffSignedIn() {
|
|
46
|
+
const { stdout } = await exec('freebuff', ['doctor'], { timeout: 10000, maxBuffer: 65536 });
|
|
47
|
+
if (/not signed in|logged.?out|login required/i.test(stdout))
|
|
48
|
+
return false;
|
|
49
|
+
if (!/Freebuff doctor/i.test(stdout))
|
|
50
|
+
throw new Error('Unrecognized Freebuff diagnostics; run freebuff doctor');
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
export async function verifyOpenCode(source = new OpenCode(), signal = AbortSignal.timeout(120000)) {
|
|
54
|
+
const sources = new Sources([source]);
|
|
55
|
+
try {
|
|
56
|
+
await sources.complete({ model: 'opencode', cwd: process.cwd(), messages: [{ role: 'user', content: 'Reply only: grunt is ready. Do not use tools or change files.' }], tools: [], stream: false }, signal);
|
|
57
|
+
}
|
|
58
|
+
finally {
|
|
59
|
+
await sources.close();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
export async function setupFallbacks(options) {
|
|
63
|
+
const squire = options.squire ?? new BrowserSquire();
|
|
64
|
+
const owned = squire instanceof BrowserSquire && !options.squire;
|
|
65
|
+
const unavailable = [];
|
|
66
|
+
// Reuse a grant created by an earlier setup before probing the free model.
|
|
67
|
+
const savedConfig = join(options.directory, 'opencode-free.json');
|
|
68
|
+
try {
|
|
69
|
+
await access(savedConfig);
|
|
70
|
+
process.env.OPENCODE_CONFIG = savedConfig;
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (error.code !== 'ENOENT')
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
let connected = false;
|
|
77
|
+
const connect = async () => { if (!connected) {
|
|
78
|
+
if (options.connect)
|
|
79
|
+
await options.connect();
|
|
80
|
+
else if (owned)
|
|
81
|
+
await squire.connect();
|
|
82
|
+
connected = true;
|
|
83
|
+
} };
|
|
84
|
+
const install = async (binary, pkg) => {
|
|
85
|
+
try {
|
|
86
|
+
await options.run(binary, ['--version']);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
await options.run('npm', ['install', '--global', pkg], true);
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
options.notify('Free routes run their own tools with your filesystem permissions in the working directory. Free model providers may retain prompts.');
|
|
93
|
+
try {
|
|
94
|
+
if (!options.adapters || options.adapters.includes('freebuff'))
|
|
95
|
+
try {
|
|
96
|
+
await install('freebuff', 'freebuff');
|
|
97
|
+
// Older CLIs accept --version but cannot run the login diagnostic.
|
|
98
|
+
try {
|
|
99
|
+
await options.run('freebuff', ['doctor']);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
options.notify('Freebuff: repairing the installed CLI before checking sign-in');
|
|
103
|
+
try {
|
|
104
|
+
await options.run('npm', ['install', '--global', 'freebuff@latest'], true);
|
|
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
|
+
}
|
|
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. */ }
|
|
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);
|
|
131
|
+
}
|
|
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');
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
unavailable.push('freebuff');
|
|
147
|
+
if (error instanceof FreebuffCompatibilityError)
|
|
148
|
+
options.notify(error.message);
|
|
149
|
+
else
|
|
150
|
+
options.notify('Freebuff setup incomplete. Run freebuff doctor, install tmux if missing, then rerun usegrunt setup. Continuing with OpenCode.');
|
|
151
|
+
}
|
|
152
|
+
if (!options.adapters || options.adapters.includes('opencode'))
|
|
153
|
+
try {
|
|
154
|
+
await install('opencode', 'opencode-ai');
|
|
155
|
+
const verify = options.verify ?? (() => verifyOpenCode(new OpenCode(), options.signal ? AbortSignal.any([options.signal, AbortSignal.timeout(120000)]) : AbortSignal.timeout(120000)));
|
|
156
|
+
try {
|
|
157
|
+
await verify();
|
|
158
|
+
}
|
|
159
|
+
catch (error) {
|
|
160
|
+
if (!(error instanceof SourceError) || error.code !== 'logged_out')
|
|
161
|
+
throw error;
|
|
162
|
+
await connect();
|
|
163
|
+
const findCredential = async () => (await squire.credentials.list()).find(c => c.service?.trim().toLowerCase() === 'opencode'
|
|
164
|
+
&& c.auth_strategy !== 'username_password' && c.field_names.some(field => /^(?:api_?key|token|key)$/i.test(field)));
|
|
165
|
+
let credential = await findCredential();
|
|
166
|
+
if (!credential) {
|
|
167
|
+
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.");
|
|
168
|
+
credential = await findCredential();
|
|
169
|
+
}
|
|
170
|
+
if (!credential)
|
|
171
|
+
throw new Error('No OpenCode access was created');
|
|
172
|
+
const grant = await squire.credentials.grant(credential.reference);
|
|
173
|
+
const path = join(options.directory, 'opencode-free.json');
|
|
174
|
+
await mkdir(options.directory, { recursive: true, mode: 0o700 });
|
|
175
|
+
await writeFile(path, JSON.stringify({ provider: { opencode: { options: { baseURL: grant.base_url.replace(/\/$/, '') + '/zen/v1', apiKey: grant.token } } } }), { mode: 0o600 });
|
|
176
|
+
process.env.OPENCODE_CONFIG = path;
|
|
177
|
+
await verify();
|
|
178
|
+
}
|
|
179
|
+
options.notify('OpenCode: free model answered');
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
unavailable.push('opencode');
|
|
183
|
+
options.notify('OpenCode free model check failed or signup was refused. Rerun usegrunt setup after repairing OpenCode; other routes remain available.');
|
|
184
|
+
}
|
|
185
|
+
return { unavailable };
|
|
186
|
+
}
|
|
187
|
+
finally {
|
|
188
|
+
if (owned && connected)
|
|
189
|
+
await squire.close();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { mkdir, readFile, writeFile } from 'node:fs/promises';
|
|
2
|
+
import { basename, dirname, join } from 'node:path';
|
|
3
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
4
|
+
// Persist both processes before allowing either send to return. A returns later,
|
|
5
|
+
// so matching by prompt alone deterministically swaps the two replies.
|
|
6
|
+
export class OwnershipMux {
|
|
7
|
+
home;
|
|
8
|
+
sessions = new Map();
|
|
9
|
+
sends = 0;
|
|
10
|
+
release;
|
|
11
|
+
both = new Promise(resolve => { this.release = resolve; });
|
|
12
|
+
constructor(home) {
|
|
13
|
+
this.home = home;
|
|
14
|
+
}
|
|
15
|
+
async start(id, cwd, _binary, continuation) {
|
|
16
|
+
this.sessions.set(id, { cwd, chat: continuation ?? `${basename(dirname(cwd))}-chat` });
|
|
17
|
+
}
|
|
18
|
+
async send(id, prompt) {
|
|
19
|
+
const session = this.sessions.get(id);
|
|
20
|
+
const folder = join(this.home, '.config/manicode/projects', basename(session.cwd), 'chats', session.chat);
|
|
21
|
+
await mkdir(folder, { recursive: true });
|
|
22
|
+
if (prompt === 'quota please') {
|
|
23
|
+
await writeFile(join(folder, 'log.jsonl'), JSON.stringify({ code: 'quota', message: 'Free hours exhausted' }));
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
const file = join(folder, 'chat-messages.json');
|
|
27
|
+
let messages = [];
|
|
28
|
+
try {
|
|
29
|
+
messages = JSON.parse(await readFile(file, 'utf8'));
|
|
30
|
+
}
|
|
31
|
+
catch { /* fresh chat */ }
|
|
32
|
+
messages.push({ variant: 'user', content: prompt }, { variant: 'ai', isComplete: true, blocks: [{ type: 'text', content: JSON.stringify(session) }] });
|
|
33
|
+
await writeFile(file, JSON.stringify(messages));
|
|
34
|
+
}
|
|
35
|
+
if (++this.sends >= 2)
|
|
36
|
+
this.release();
|
|
37
|
+
await this.both;
|
|
38
|
+
if (basename(dirname(session.cwd)) === 'A')
|
|
39
|
+
await delay(100);
|
|
40
|
+
}
|
|
41
|
+
async stop() { }
|
|
42
|
+
}
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { promisify } from 'node:util';
|
|
3
|
+
import { readdir, readFile, realpath, stat } from 'node:fs/promises';
|
|
4
|
+
import { basename, dirname, join } from 'node:path';
|
|
5
|
+
import { homedir } from 'node:os';
|
|
6
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
7
|
+
import { setTimeout as delay } from 'node:timers/promises';
|
|
8
|
+
import { executable, Tmux } from './multiplexer.js';
|
|
9
|
+
import { userTurns } from './harness.js';
|
|
10
|
+
import { SourceError } from './source.js';
|
|
11
|
+
export function positive(value, fallback) {
|
|
12
|
+
const number = value === undefined ? fallback : Number(value);
|
|
13
|
+
if (!Number.isSafeInteger(number) || number < 1)
|
|
14
|
+
throw new Error('Pool size and timeout must be positive integers');
|
|
15
|
+
return number;
|
|
16
|
+
}
|
|
17
|
+
const exec = promisify(execFile);
|
|
18
|
+
function logError(log) {
|
|
19
|
+
const line = log.split('\n').find(line => /prompt_policy_refused|logged.?out|unauthorized|login required|not signed in|"code"\s*:\s*"quota"|quota[ _-](?:exceeded|exhausted|reached)|free[ _-]?hours.*(?:exhaust|exceed|empty)|(?:out of|no) free hours/i.test(line));
|
|
20
|
+
if (!line)
|
|
21
|
+
return;
|
|
22
|
+
return new SourceError(/prompt_policy_refused/.test(line) ? 'refused' : /quota|hours/i.test(line) ? 'quota' : 'logged_out', `Freebuff: ${line}`);
|
|
23
|
+
}
|
|
24
|
+
// A request starts a fresh process; slots bound parallel sessions for each canonical cwd.
|
|
25
|
+
class Pool {
|
|
26
|
+
size;
|
|
27
|
+
active = 0;
|
|
28
|
+
queue = [];
|
|
29
|
+
constructor(size) {
|
|
30
|
+
this.size = size;
|
|
31
|
+
}
|
|
32
|
+
async acquire(signal) {
|
|
33
|
+
signal.throwIfAborted();
|
|
34
|
+
if (this.active < this.size) {
|
|
35
|
+
this.active++;
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
await new Promise((resolve, reject) => {
|
|
39
|
+
const waiter = { resolve: () => { signal.removeEventListener('abort', abort); resolve(); }, reject };
|
|
40
|
+
const abort = () => { this.queue = this.queue.filter(w => w !== waiter); reject(new SourceError('timeout', 'Freebuff request cancelled while queued')); };
|
|
41
|
+
this.queue.push(waiter);
|
|
42
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
release() { const waiter = this.queue.shift(); if (waiter)
|
|
46
|
+
waiter.resolve();
|
|
47
|
+
else
|
|
48
|
+
this.active--; }
|
|
49
|
+
}
|
|
50
|
+
function matchesUser(m, prompt) {
|
|
51
|
+
const normalize = (text) => text.replace(/\r\n?/g, '\n').trim();
|
|
52
|
+
return m.variant === 'user' && normalize(m.content || m.textAttachments?.map(a => a.content ?? '').join('\n') || '') === normalize(prompt);
|
|
53
|
+
}
|
|
54
|
+
export function parseChat(value, prompt) {
|
|
55
|
+
if (!Array.isArray(value) || value.some(m => !m || typeof m !== 'object' || typeof m.variant !== 'string'))
|
|
56
|
+
throw new SourceError('upstream', 'Freebuff chat format changed: expected a message array');
|
|
57
|
+
const messages = value;
|
|
58
|
+
const userIndex = messages.findLastIndex(m => matchesUser(m, prompt));
|
|
59
|
+
if (userIndex < 0)
|
|
60
|
+
return;
|
|
61
|
+
const replies = messages.slice(userIndex + 1).filter(m => m.variant === 'ai');
|
|
62
|
+
const last = replies.at(-1);
|
|
63
|
+
if (!last || last.isComplete !== true)
|
|
64
|
+
return;
|
|
65
|
+
if (!Array.isArray(last.blocks))
|
|
66
|
+
throw new SourceError('upstream', 'Freebuff chat format changed: missing reply blocks');
|
|
67
|
+
const blocks = last.blocks.filter(b => b.type === 'text');
|
|
68
|
+
if (!blocks.length)
|
|
69
|
+
return;
|
|
70
|
+
if (blocks.some(b => typeof b.content !== 'string'))
|
|
71
|
+
throw new SourceError('upstream', 'Freebuff chat format changed: missing text blocks');
|
|
72
|
+
return blocks.map(b => b.content).join('\n');
|
|
73
|
+
}
|
|
74
|
+
export class Freebuff {
|
|
75
|
+
options;
|
|
76
|
+
id = 'freebuff';
|
|
77
|
+
pools = new Map();
|
|
78
|
+
sessions = new Set();
|
|
79
|
+
conversations = new Map();
|
|
80
|
+
busy = new Set();
|
|
81
|
+
creationLocks = new Map();
|
|
82
|
+
shutdown = new AbortController();
|
|
83
|
+
constructor(options = {}) {
|
|
84
|
+
this.options = options;
|
|
85
|
+
}
|
|
86
|
+
get mux() { return this.options.mux ?? (this.options.mux = new Tmux()); }
|
|
87
|
+
async available() {
|
|
88
|
+
try {
|
|
89
|
+
const binary = await executable('freebuff');
|
|
90
|
+
await executable('tmux');
|
|
91
|
+
const { stdout } = await exec(binary, ['doctor'], { timeout: 5000, maxBuffer: 65536 });
|
|
92
|
+
return /Freebuff doctor/i.test(stdout) && logError(stdout)?.code !== 'logged_out';
|
|
93
|
+
}
|
|
94
|
+
catch {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async healthCheck() { return this.available(); }
|
|
99
|
+
async complete(request, caller) {
|
|
100
|
+
const binary = await executable('freebuff');
|
|
101
|
+
await executable('tmux');
|
|
102
|
+
let diagnostics;
|
|
103
|
+
try {
|
|
104
|
+
diagnostics = (await exec(binary, ['doctor'], { timeout: 5000, maxBuffer: 65536, signal: caller })).stdout;
|
|
105
|
+
}
|
|
106
|
+
catch {
|
|
107
|
+
throw new SourceError('upstream', 'Freebuff startup check failed; run freebuff doctor');
|
|
108
|
+
}
|
|
109
|
+
const diagnosticError = logError(diagnostics);
|
|
110
|
+
if (diagnosticError)
|
|
111
|
+
throw diagnosticError;
|
|
112
|
+
const cwd = await realpath(request.cwd);
|
|
113
|
+
if (!(await stat(cwd)).isDirectory())
|
|
114
|
+
throw new SourceError('upstream', 'Freebuff cwd must be a directory');
|
|
115
|
+
const timeout = this.options.timeout ?? positive(process.env.MILO_APP_TIMEOUT_MS, 600000);
|
|
116
|
+
const signal = AbortSignal.any([caller, this.shutdown.signal, AbortSignal.timeout(timeout)]);
|
|
117
|
+
let pool = this.pools.get(cwd);
|
|
118
|
+
if (!pool) {
|
|
119
|
+
pool = new Pool(this.options.pool ?? positive(process.env.MILO_APP_POOL, 2));
|
|
120
|
+
this.pools.set(cwd, pool);
|
|
121
|
+
}
|
|
122
|
+
let acquired = false;
|
|
123
|
+
const id = `milo-${randomUUID()}`;
|
|
124
|
+
const directory = join(this.options.home ?? homedir(), '.config/manicode/projects', basename(cwd), 'chats');
|
|
125
|
+
const turns = userTurns(request);
|
|
126
|
+
const prompt = request.handoffPrompt ?? turns.at(-1);
|
|
127
|
+
if (!prompt)
|
|
128
|
+
throw new SourceError('upstream', 'No user text in this request');
|
|
129
|
+
const key = cwd + ':' + createHash('sha256').update(turns[0]).digest('hex');
|
|
130
|
+
// Simultaneous requests must never send into the same pane/chat.
|
|
131
|
+
let continuation = this.busy.has(key) ? undefined : this.conversations.get(key);
|
|
132
|
+
const ownsConversation = !this.busy.has(key);
|
|
133
|
+
if (ownsConversation)
|
|
134
|
+
this.busy.add(key);
|
|
135
|
+
let creation;
|
|
136
|
+
let creating = false;
|
|
137
|
+
let partial = '';
|
|
138
|
+
const releaseCreation = () => { if (creating) {
|
|
139
|
+
creation.release();
|
|
140
|
+
creating = false;
|
|
141
|
+
} };
|
|
142
|
+
try {
|
|
143
|
+
await pool.acquire(signal);
|
|
144
|
+
acquired = true;
|
|
145
|
+
let previousMessages = 0;
|
|
146
|
+
if (continuation) {
|
|
147
|
+
try {
|
|
148
|
+
previousMessages = JSON.parse(await readFile(continuation, 'utf8')).length;
|
|
149
|
+
}
|
|
150
|
+
catch {
|
|
151
|
+
this.conversations.delete(key);
|
|
152
|
+
continuation = undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (!continuation) {
|
|
156
|
+
// Freebuff groups different working folders with the same basename together.
|
|
157
|
+
// Start only one unassociated process per on-disk directory at a time.
|
|
158
|
+
creation = this.creationLocks.get(directory);
|
|
159
|
+
if (!creation) {
|
|
160
|
+
creation = new Pool(1);
|
|
161
|
+
this.creationLocks.set(directory, creation);
|
|
162
|
+
}
|
|
163
|
+
await creation.acquire(signal);
|
|
164
|
+
creating = true;
|
|
165
|
+
}
|
|
166
|
+
signal.throwIfAborted();
|
|
167
|
+
const before = new Set(await this.chatFiles(directory));
|
|
168
|
+
const logOffsets = new Map();
|
|
169
|
+
if (continuation) {
|
|
170
|
+
try {
|
|
171
|
+
logOffsets.set(continuation, (await readFile(join(dirname(continuation), 'log.jsonl'), 'utf8')).length);
|
|
172
|
+
}
|
|
173
|
+
catch (e) {
|
|
174
|
+
if (e.code !== 'ENOENT')
|
|
175
|
+
throw e;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
this.sessions.add(id);
|
|
179
|
+
await this.mux.start(id, cwd, binary, continuation ? basename(dirname(continuation)) : undefined);
|
|
180
|
+
await delay(this.options.startup ?? 2500, undefined, { signal });
|
|
181
|
+
// Persisted files can identify this process before send finishes returning.
|
|
182
|
+
// Observe them concurrently, while still holding the creation lock.
|
|
183
|
+
let sendComplete = false;
|
|
184
|
+
let sendFailure;
|
|
185
|
+
void this.mux.send(id, prompt).then(() => { sendComplete = true; }, error => { sendFailure = error; sendComplete = true; });
|
|
186
|
+
let malformedSince;
|
|
187
|
+
let selected = continuation;
|
|
188
|
+
let sawUser = false;
|
|
189
|
+
const sentAt = Date.now();
|
|
190
|
+
while (true) {
|
|
191
|
+
signal.throwIfAborted();
|
|
192
|
+
if (sendFailure)
|
|
193
|
+
throw sendFailure;
|
|
194
|
+
for (const file of await this.chatFiles(directory)) {
|
|
195
|
+
if (selected ? file !== selected : before.has(file))
|
|
196
|
+
continue;
|
|
197
|
+
let log = '';
|
|
198
|
+
try {
|
|
199
|
+
log = await readFile(join(dirname(file), 'log.jsonl'), 'utf8');
|
|
200
|
+
}
|
|
201
|
+
catch (e) {
|
|
202
|
+
if (e.code !== 'ENOENT')
|
|
203
|
+
throw e;
|
|
204
|
+
}
|
|
205
|
+
const startupError = logError(log.slice(logOffsets.get(file) ?? 0));
|
|
206
|
+
let raw;
|
|
207
|
+
try {
|
|
208
|
+
raw = await readFile(file, 'utf8');
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
if (error.code === 'ENOENT') {
|
|
212
|
+
if (startupError)
|
|
213
|
+
throw startupError;
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
let data;
|
|
219
|
+
try {
|
|
220
|
+
data = JSON.parse(raw);
|
|
221
|
+
}
|
|
222
|
+
catch {
|
|
223
|
+
// Writes may briefly be partial; a persistently invalid file is an actionable format error.
|
|
224
|
+
malformedSince ??= Date.now();
|
|
225
|
+
if (Date.now() - malformedSince > 2000)
|
|
226
|
+
throw new SourceError('upstream', 'Freebuff chat file is persistently invalid JSON');
|
|
227
|
+
continue;
|
|
228
|
+
}
|
|
229
|
+
if (Array.isArray(data)) {
|
|
230
|
+
const latest = data.filter(m => m?.variant === 'ai').at(-1);
|
|
231
|
+
if (Array.isArray(latest?.blocks))
|
|
232
|
+
partial = latest.blocks.filter((b) => b.type === 'text' && typeof b.content === 'string').map((b) => b.content).join('\n').slice(-4096);
|
|
233
|
+
}
|
|
234
|
+
if (startupError) {
|
|
235
|
+
selected = file;
|
|
236
|
+
releaseCreation();
|
|
237
|
+
throw startupError;
|
|
238
|
+
}
|
|
239
|
+
if (continuation && Array.isArray(data))
|
|
240
|
+
data = data.slice(previousMessages);
|
|
241
|
+
const matched = Array.isArray(data) && data.some(m => matchesUser(m, prompt));
|
|
242
|
+
if (!matched)
|
|
243
|
+
continue;
|
|
244
|
+
// No other new process sharing this storage directory started between
|
|
245
|
+
// our snapshot and this association. Later polling reads only this chat.
|
|
246
|
+
selected = file;
|
|
247
|
+
releaseCreation();
|
|
248
|
+
sawUser = true;
|
|
249
|
+
const text = parseChat(data, prompt);
|
|
250
|
+
if (text !== undefined && sendComplete) {
|
|
251
|
+
if (ownsConversation)
|
|
252
|
+
this.conversations.set(key, file);
|
|
253
|
+
return { text };
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
if (!sawUser && Date.now() - sentAt > 10000)
|
|
257
|
+
throw new SourceError('upstream', 'Freebuff did not persist a user turn within 10 seconds; run freebuff doctor and check login or startup prompts');
|
|
258
|
+
await delay(this.options.poll ?? 200, undefined, { signal });
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
catch (error) {
|
|
262
|
+
caller.throwIfAborted();
|
|
263
|
+
if (this.shutdown.signal.aborted)
|
|
264
|
+
throw new SourceError('cancelled', 'Freebuff request cancelled during shutdown');
|
|
265
|
+
if (signal.aborted)
|
|
266
|
+
throw new SourceError('timeout', `Freebuff timed out after ${timeout}ms; check freebuff login and your free-hours balance`);
|
|
267
|
+
if (error instanceof SourceError) {
|
|
268
|
+
if (partial)
|
|
269
|
+
error.output = partial;
|
|
270
|
+
throw error;
|
|
271
|
+
}
|
|
272
|
+
throw new SourceError('upstream', `Freebuff failed: ${error instanceof Error ? error.message : 'unknown error'}`);
|
|
273
|
+
}
|
|
274
|
+
finally {
|
|
275
|
+
try {
|
|
276
|
+
await this.mux.stop(id);
|
|
277
|
+
}
|
|
278
|
+
finally {
|
|
279
|
+
// An unassociated process must be stopped before another one can start.
|
|
280
|
+
releaseCreation();
|
|
281
|
+
this.sessions.delete(id);
|
|
282
|
+
if (acquired)
|
|
283
|
+
pool.release();
|
|
284
|
+
if (ownsConversation)
|
|
285
|
+
this.busy.delete(key);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
async chatFiles(dir) {
|
|
290
|
+
try {
|
|
291
|
+
return (await readdir(dir, { withFileTypes: true })).filter(d => d.isDirectory()).map(d => join(dir, d.name, 'chat-messages.json'));
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
if (error.code === 'ENOENT')
|
|
295
|
+
return [];
|
|
296
|
+
throw error;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
async close() { this.shutdown.abort(); await Promise.all([...this.sessions].map(id => this.mux.stop(id))); await this.mux.close?.(); }
|
|
300
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { SourceError } from './source.js';
|
|
2
|
+
// Numeric loopback addresses avoid DNS rebinding and accidental remote routing.
|
|
3
|
+
export function loopback(value) {
|
|
4
|
+
const url = new URL(value);
|
|
5
|
+
if (url.protocol !== 'http:' || !['127.0.0.1', '[::1]'].includes(url.hostname) || url.username || url.password || url.search || url.hash)
|
|
6
|
+
throw new Error('A numeric HTTP loopback URL is required');
|
|
7
|
+
return url;
|
|
8
|
+
}
|
|
9
|
+
export const KILO_URL = 'https://ai-gateway.kilo.ai/api/v1';
|
|
10
|
+
export const FREEBUFF_URL = 'http://127.0.0.1:8789/v1';
|
|
11
|
+
export function freeConfig() {
|
|
12
|
+
return {
|
|
13
|
+
customProviders: [
|
|
14
|
+
{ baseUrl: KILO_URL, apiKey: 'no-key', label: 'Kilo Free', models: [{ model: 'kilo-auto/free', supportsTools: true }] },
|
|
15
|
+
{ baseUrl: FREEBUFF_URL, apiKey: 'no-key', label: 'Freebuff', models: [{ model: 'freebuff', supportsTools: true }] }
|
|
16
|
+
],
|
|
17
|
+
// Existing keyed provider rows retain their catalog priorities. Never enable
|
|
18
|
+
// OpenRouter or unvetted aggregators by provisioning keys for them here.
|
|
19
|
+
fallback: [
|
|
20
|
+
{ platform: 'custom', modelId: 'kilo-auto/free', endpoint: KILO_URL, priority: 999999 },
|
|
21
|
+
{ platform: 'custom', modelId: 'freebuff', endpoint: FREEBUFF_URL, priority: 1000000 }
|
|
22
|
+
],
|
|
23
|
+
routing: { strategy: 'priority' }
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export class FreeLLMAPI {
|
|
27
|
+
options;
|
|
28
|
+
id;
|
|
29
|
+
url;
|
|
30
|
+
fetcher;
|
|
31
|
+
constructor(options = {}) {
|
|
32
|
+
this.options = options;
|
|
33
|
+
this.url = loopback(options.url ?? 'http://127.0.0.1:3001/v1').href.replace(/\/$/, '');
|
|
34
|
+
this.fetcher = options.fetcher ?? fetch;
|
|
35
|
+
this.id = options.id ?? 'freellmapi';
|
|
36
|
+
}
|
|
37
|
+
async available() {
|
|
38
|
+
try {
|
|
39
|
+
const key = this.options.key || await this.options.loadKey?.();
|
|
40
|
+
if (!key)
|
|
41
|
+
return false;
|
|
42
|
+
return (await this.fetcher(`${this.url}/models`, { headers: { Authorization: `Bearer ${key}` }, redirect: 'error', signal: AbortSignal.timeout(2000) })).ok;
|
|
43
|
+
}
|
|
44
|
+
catch {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async complete(request, caller) {
|
|
49
|
+
const key = this.options.key || await this.options.loadKey?.();
|
|
50
|
+
caller.throwIfAborted();
|
|
51
|
+
if (!key)
|
|
52
|
+
throw new SourceError('logged_out', 'FreeLLMAPI login unavailable; check the local router and saved login');
|
|
53
|
+
if (request.tools.some(t => t.type !== 'function'))
|
|
54
|
+
throw new SourceError('upstream', 'FreeLLMAPI Chat Completions requires function tools');
|
|
55
|
+
const signal = AbortSignal.any([caller, AbortSignal.timeout(600000)]);
|
|
56
|
+
let response;
|
|
57
|
+
try {
|
|
58
|
+
response = await this.fetcher(`${this.url}/chat/completions`, {
|
|
59
|
+
method: 'POST', redirect: 'error', signal,
|
|
60
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${key}` },
|
|
61
|
+
body: JSON.stringify({ model: this.options.model ?? 'auto', messages: request.messages, tools: request.tools.length ? request.tools : undefined, stream: false, max_tokens: Math.min(4096, Math.max(1, request.maxTokens ?? 4096)) })
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
catch {
|
|
65
|
+
throw new SourceError(signal.aborted ? 'timeout' : 'upstream', 'Local FreeLLMAPI is unavailable');
|
|
66
|
+
}
|
|
67
|
+
if (!response.ok)
|
|
68
|
+
throw new SourceError(response.status === 403 ? 'refused' : response.status === 401 ? 'logged_out' : response.status === 429 ? 'quota' : 'upstream', `FreeLLMAPI returned HTTP ${response.status}`);
|
|
69
|
+
const result = await response.json();
|
|
70
|
+
const message = result.choices?.[0]?.message;
|
|
71
|
+
if (message?.refusal)
|
|
72
|
+
throw new SourceError('refused', 'FreeLLMAPI provider refused the request');
|
|
73
|
+
if (!message || (typeof message.content !== 'string' && !message.tool_calls?.length))
|
|
74
|
+
throw new SourceError('upstream', 'FreeLLMAPI returned an invalid completion');
|
|
75
|
+
return { text: message.content ?? '', ...(message.tool_calls ? { calls: message.tool_calls } : {}), inputTokens: result.usage?.prompt_tokens ?? 0, outputTokens: result.usage?.completion_tokens ?? 0 };
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// Remove known harness context envelopes only; never rewrite the person's text.
|
|
2
|
+
export function userText(value) {
|
|
3
|
+
return value
|
|
4
|
+
.replace(/<system-reminder>[\s\S]*?<\/system-reminder>\r?\n?/g, '')
|
|
5
|
+
.replace(/<environment_context>[\s\S]*?<\/environment_context>\r?\n?/g, '')
|
|
6
|
+
.replace(/<user_instructions>[\s\S]*?<\/user_instructions>\r?\n?/g, '')
|
|
7
|
+
.replace(/^# AGENTS\.md instructions for [^\n]*\r?\n(?:[\s\S]*?<\/INSTRUCTIONS>\r?\n?)/g, '')
|
|
8
|
+
.replace(/^# Environment\r?\n(?:[ \t]*- [^\n]*(?:\r?\n|$))+/g, '');
|
|
9
|
+
}
|
|
10
|
+
export function userTurns(request) {
|
|
11
|
+
if (request.userMessages)
|
|
12
|
+
return request.userMessages;
|
|
13
|
+
return request.messages.filter(m => m.role === 'user').map(m => userText(m.content)).filter(s => s.length > 0);
|
|
14
|
+
}
|
|
15
|
+
export function housekeeping(request) {
|
|
16
|
+
const instructions = request.messages.filter(m => m.role === 'system' || m.role === 'developer').map(m => m.content).join('\n').replace(/^x-anthropic-billing-header:[^\n]*\n?/gm, '').replace(/^You are Claude Code, Anthropic's official CLI for Claude\.\n?/gm, '').trimStart();
|
|
17
|
+
const side = /^(?:generate|create|write|provide|return)[\s\S]{0,100}(?:session title|conversation title|chat title|short title|concise title|topic|summary)|^(?:quota|warm[- ]?up) (?:probe|check)|^title for (?:this|the) (?:session|conversation)/i;
|
|
18
|
+
const claudeTitle = /^You are naming a coding session so the user can pick it out of a long list of sessions\./.test(instructions);
|
|
19
|
+
if (!claudeTitle && !side.test(instructions))
|
|
20
|
+
return;
|
|
21
|
+
const first = userTurns(request)[0];
|
|
22
|
+
const text = claudeTitle ? first?.match(/<session>\n([\s\S]*?)\n<\/session>/)?.[1] : first;
|
|
23
|
+
const reply = text?.replace(/\s+/g, ' ').slice(0, 80) || 'Ready';
|
|
24
|
+
return claudeTitle ? JSON.stringify({ title: reply }) : reply;
|
|
25
|
+
}
|