usegrunt 0.0.1 → 0.6.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 +522 -0
- 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 +210 -0
- package/dist/local/approval-proxy.js +71 -0
- package/dist/local/banner.js +136 -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 +192 -0
- package/dist/local/fixtures/ownership-mux.js +42 -0
- package/dist/local/freebuff-tools.js +112 -0
- package/dist/local/freebuff.js +332 -0
- package/dist/local/freellmapi.js +78 -0
- package/dist/local/harness.js +25 -0
- package/dist/local/harnesses.js +164 -0
- package/dist/local/main.js +112 -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 +139 -0
- package/dist/local/setup.js +523 -0
- package/dist/local/source.js +111 -0
- package/dist/local/wizard.js +443 -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 +48 -7
|
@@ -0,0 +1,332 @@
|
|
|
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
|
+
import { FreebuffTools } from './freebuff-tools.js';
|
|
12
|
+
export function positive(value, fallback) {
|
|
13
|
+
const number = value === undefined ? fallback : Number(value);
|
|
14
|
+
if (!Number.isSafeInteger(number) || number < 1)
|
|
15
|
+
throw new Error('Pool size and timeout must be positive integers');
|
|
16
|
+
return number;
|
|
17
|
+
}
|
|
18
|
+
const exec = promisify(execFile);
|
|
19
|
+
// freebuff doctor can take several seconds on a healthy machine (8 s measured).
|
|
20
|
+
export const DOCTOR_TIMEOUT_MS = 30000;
|
|
21
|
+
function logError(log) {
|
|
22
|
+
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));
|
|
23
|
+
if (!line)
|
|
24
|
+
return;
|
|
25
|
+
return new SourceError(/prompt_policy_refused/.test(line) ? 'refused' : /quota|hours/i.test(line) ? 'quota' : 'logged_out', `Freebuff: ${line}`);
|
|
26
|
+
}
|
|
27
|
+
// A request starts a fresh process; slots bound parallel sessions for each canonical cwd.
|
|
28
|
+
class Pool {
|
|
29
|
+
size;
|
|
30
|
+
active = 0;
|
|
31
|
+
queue = [];
|
|
32
|
+
constructor(size) {
|
|
33
|
+
this.size = size;
|
|
34
|
+
}
|
|
35
|
+
async acquire(signal) {
|
|
36
|
+
signal.throwIfAborted();
|
|
37
|
+
if (this.active < this.size) {
|
|
38
|
+
this.active++;
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
await new Promise((resolve, reject) => {
|
|
42
|
+
const waiter = { resolve: () => { signal.removeEventListener('abort', abort); resolve(); }, reject };
|
|
43
|
+
const abort = () => { this.queue = this.queue.filter(w => w !== waiter); reject(new SourceError('timeout', 'Freebuff request cancelled while queued')); };
|
|
44
|
+
this.queue.push(waiter);
|
|
45
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
release() { const waiter = this.queue.shift(); if (waiter)
|
|
49
|
+
waiter.resolve();
|
|
50
|
+
else
|
|
51
|
+
this.active--; }
|
|
52
|
+
}
|
|
53
|
+
function matchesUser(m, prompt) {
|
|
54
|
+
const normalize = (text) => text.replace(/\r\n?/g, '\n').trim();
|
|
55
|
+
return m.variant === 'user' && normalize(m.content || m.textAttachments?.map(a => a.content ?? '').join('\n') || '') === normalize(prompt);
|
|
56
|
+
}
|
|
57
|
+
export function parseChat(value, prompt, forbidNative = false) {
|
|
58
|
+
if (!Array.isArray(value) || value.some(m => !m || typeof m !== 'object' || typeof m.variant !== 'string'))
|
|
59
|
+
throw new SourceError('upstream', 'Freebuff chat format changed: expected a message array');
|
|
60
|
+
const messages = value;
|
|
61
|
+
const userIndex = messages.findLastIndex(m => matchesUser(m, prompt));
|
|
62
|
+
if (userIndex < 0)
|
|
63
|
+
return;
|
|
64
|
+
const replies = messages.slice(userIndex + 1).filter(m => m.variant === 'ai');
|
|
65
|
+
if (forbidNative && replies.some(m => m.blocks?.some(b => b.type !== 'text')))
|
|
66
|
+
throw new SourceError('refused', 'Freebuff used native tools during a Pi JSON bridge request');
|
|
67
|
+
const last = replies.at(-1);
|
|
68
|
+
if (!last || last.isComplete !== true)
|
|
69
|
+
return;
|
|
70
|
+
if (!Array.isArray(last.blocks))
|
|
71
|
+
throw new SourceError('upstream', 'Freebuff chat format changed: missing reply blocks');
|
|
72
|
+
let end = last.blocks.length;
|
|
73
|
+
while (end > 0 && last.blocks[end - 1].type === 'tool' && last.blocks[end - 1].name === 'suggest_followups')
|
|
74
|
+
end--;
|
|
75
|
+
const answer = last.blocks.slice(0, end);
|
|
76
|
+
const blocks = answer.slice(answer.findLastIndex(b => b.type !== 'text') + 1).filter(b => b.textType !== 'reasoning');
|
|
77
|
+
if (!blocks.length)
|
|
78
|
+
return;
|
|
79
|
+
if (blocks.some(b => typeof b.content !== 'string'))
|
|
80
|
+
throw new SourceError('upstream', 'Freebuff chat format changed: missing text blocks');
|
|
81
|
+
return blocks.map(b => b.content).join('\n');
|
|
82
|
+
}
|
|
83
|
+
export class Freebuff {
|
|
84
|
+
options;
|
|
85
|
+
id = 'freebuff';
|
|
86
|
+
pools = new Map();
|
|
87
|
+
sessions = new Set();
|
|
88
|
+
conversations = new Map();
|
|
89
|
+
busy = new Set();
|
|
90
|
+
creationLocks = new Map();
|
|
91
|
+
shutdown = new AbortController();
|
|
92
|
+
constructor(options = {}) {
|
|
93
|
+
this.options = options;
|
|
94
|
+
}
|
|
95
|
+
get supportsFunctionTools() { return this.options.toolBridge === true; }
|
|
96
|
+
get mux() { return this.options.mux ?? (this.options.mux = new Tmux()); }
|
|
97
|
+
async available() {
|
|
98
|
+
try {
|
|
99
|
+
const binary = await executable('freebuff');
|
|
100
|
+
await executable('tmux');
|
|
101
|
+
const { stdout } = await exec(binary, ['doctor'], { timeout: DOCTOR_TIMEOUT_MS, maxBuffer: 65536 });
|
|
102
|
+
return /Freebuff doctor/i.test(stdout) && logError(stdout)?.code !== 'logged_out';
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async healthCheck() { return this.available(); }
|
|
109
|
+
async complete(request, caller) {
|
|
110
|
+
if (!this.options.toolBridge)
|
|
111
|
+
return this.completeText(request, caller);
|
|
112
|
+
caller.throwIfAborted();
|
|
113
|
+
const bridge = new FreebuffTools(request);
|
|
114
|
+
const signal = AbortSignal.any([caller, AbortSignal.timeout(this.options.timeout ?? positive(process.env.MILO_APP_TIMEOUT_MS, 600000))]);
|
|
115
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
116
|
+
signal.throwIfAborted();
|
|
117
|
+
const result = await this.completeText({ ...request, handoffPrompt: bridge.prompt(attempt > 0) }, signal, true);
|
|
118
|
+
signal.throwIfAborted();
|
|
119
|
+
if (this.shutdown.signal.aborted)
|
|
120
|
+
throw new SourceError('cancelled', 'Freebuff request cancelled during shutdown');
|
|
121
|
+
try {
|
|
122
|
+
return bridge.parse(result.text);
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
if (attempt === 1)
|
|
126
|
+
throw new SourceError('upstream', 'Freebuff JSON bridge failed validation after one repair attempt');
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
throw new SourceError('upstream', 'Freebuff JSON bridge failed');
|
|
130
|
+
}
|
|
131
|
+
async completeText(request, caller, isolated = false) {
|
|
132
|
+
const binary = await executable('freebuff');
|
|
133
|
+
await executable('tmux');
|
|
134
|
+
let diagnostics;
|
|
135
|
+
try {
|
|
136
|
+
diagnostics = (await exec(binary, ['doctor'], { timeout: DOCTOR_TIMEOUT_MS, maxBuffer: 65536, signal: caller })).stdout;
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
throw new SourceError('upstream', 'Freebuff startup check failed; run freebuff doctor');
|
|
140
|
+
}
|
|
141
|
+
const diagnosticError = logError(diagnostics);
|
|
142
|
+
if (diagnosticError)
|
|
143
|
+
throw diagnosticError;
|
|
144
|
+
const cwd = await realpath(request.cwd);
|
|
145
|
+
if (!(await stat(cwd)).isDirectory())
|
|
146
|
+
throw new SourceError('upstream', 'Freebuff cwd must be a directory');
|
|
147
|
+
const timeout = this.options.timeout ?? positive(process.env.MILO_APP_TIMEOUT_MS, 600000);
|
|
148
|
+
const signal = AbortSignal.any([caller, this.shutdown.signal, AbortSignal.timeout(timeout)]);
|
|
149
|
+
let pool = this.pools.get(cwd);
|
|
150
|
+
if (!pool) {
|
|
151
|
+
pool = new Pool(this.options.pool ?? positive(process.env.MILO_APP_POOL, 2));
|
|
152
|
+
this.pools.set(cwd, pool);
|
|
153
|
+
}
|
|
154
|
+
let acquired = false;
|
|
155
|
+
const id = `milo-${randomUUID()}`;
|
|
156
|
+
const directory = join(this.options.home ?? homedir(), '.config/manicode/projects', basename(cwd), 'chats');
|
|
157
|
+
const turns = userTurns(request);
|
|
158
|
+
const prompt = request.handoffPrompt ?? turns.at(-1);
|
|
159
|
+
if (!prompt)
|
|
160
|
+
throw new SourceError('upstream', 'No user text in this request');
|
|
161
|
+
const key = cwd + ':' + createHash('sha256').update(turns[0]).digest('hex');
|
|
162
|
+
// Simultaneous requests must never send into the same pane/chat.
|
|
163
|
+
let continuation = isolated || this.busy.has(key) ? undefined : this.conversations.get(key);
|
|
164
|
+
const ownsConversation = !isolated && !this.busy.has(key);
|
|
165
|
+
if (ownsConversation)
|
|
166
|
+
this.busy.add(key);
|
|
167
|
+
let creation;
|
|
168
|
+
let creating = false;
|
|
169
|
+
let partial = '';
|
|
170
|
+
const releaseCreation = () => { if (creating) {
|
|
171
|
+
creation.release();
|
|
172
|
+
creating = false;
|
|
173
|
+
} };
|
|
174
|
+
try {
|
|
175
|
+
await pool.acquire(signal);
|
|
176
|
+
acquired = true;
|
|
177
|
+
let previousMessages = 0;
|
|
178
|
+
if (continuation) {
|
|
179
|
+
try {
|
|
180
|
+
previousMessages = JSON.parse(await readFile(continuation, 'utf8')).length;
|
|
181
|
+
}
|
|
182
|
+
catch {
|
|
183
|
+
this.conversations.delete(key);
|
|
184
|
+
continuation = undefined;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (!continuation) {
|
|
188
|
+
// Freebuff groups different working folders with the same basename together.
|
|
189
|
+
// Start only one unassociated process per on-disk directory at a time.
|
|
190
|
+
creation = this.creationLocks.get(directory);
|
|
191
|
+
if (!creation) {
|
|
192
|
+
creation = new Pool(1);
|
|
193
|
+
this.creationLocks.set(directory, creation);
|
|
194
|
+
}
|
|
195
|
+
await creation.acquire(signal);
|
|
196
|
+
creating = true;
|
|
197
|
+
}
|
|
198
|
+
signal.throwIfAborted();
|
|
199
|
+
const before = new Set(await this.chatFiles(directory));
|
|
200
|
+
const logOffsets = new Map();
|
|
201
|
+
if (continuation) {
|
|
202
|
+
try {
|
|
203
|
+
logOffsets.set(continuation, (await readFile(join(dirname(continuation), 'log.jsonl'), 'utf8')).length);
|
|
204
|
+
}
|
|
205
|
+
catch (e) {
|
|
206
|
+
if (e.code !== 'ENOENT')
|
|
207
|
+
throw e;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
this.sessions.add(id);
|
|
211
|
+
await this.mux.start(id, cwd, binary, continuation ? basename(dirname(continuation)) : undefined);
|
|
212
|
+
await delay(this.options.startup ?? 2500, undefined, { signal });
|
|
213
|
+
// Persisted files can identify this process before send finishes returning.
|
|
214
|
+
// Observe them concurrently, while still holding the creation lock.
|
|
215
|
+
let sendComplete = false;
|
|
216
|
+
let sendFailure;
|
|
217
|
+
void this.mux.send(id, prompt).then(() => { sendComplete = true; }, error => { sendFailure = error; sendComplete = true; });
|
|
218
|
+
let malformedSince;
|
|
219
|
+
let selected = continuation;
|
|
220
|
+
let sawUser = false;
|
|
221
|
+
const sentAt = Date.now();
|
|
222
|
+
while (true) {
|
|
223
|
+
signal.throwIfAborted();
|
|
224
|
+
if (sendFailure)
|
|
225
|
+
throw sendFailure;
|
|
226
|
+
for (const file of await this.chatFiles(directory)) {
|
|
227
|
+
if (selected ? file !== selected : before.has(file))
|
|
228
|
+
continue;
|
|
229
|
+
let log = '';
|
|
230
|
+
try {
|
|
231
|
+
log = await readFile(join(dirname(file), 'log.jsonl'), 'utf8');
|
|
232
|
+
}
|
|
233
|
+
catch (e) {
|
|
234
|
+
if (e.code !== 'ENOENT')
|
|
235
|
+
throw e;
|
|
236
|
+
}
|
|
237
|
+
const startupError = logError(log.slice(logOffsets.get(file) ?? 0));
|
|
238
|
+
let raw;
|
|
239
|
+
try {
|
|
240
|
+
raw = await readFile(file, 'utf8');
|
|
241
|
+
}
|
|
242
|
+
catch (error) {
|
|
243
|
+
if (error.code === 'ENOENT') {
|
|
244
|
+
if (startupError)
|
|
245
|
+
throw startupError;
|
|
246
|
+
continue;
|
|
247
|
+
}
|
|
248
|
+
throw error;
|
|
249
|
+
}
|
|
250
|
+
let data;
|
|
251
|
+
try {
|
|
252
|
+
data = JSON.parse(raw);
|
|
253
|
+
}
|
|
254
|
+
catch {
|
|
255
|
+
// Writes may briefly be partial; a persistently invalid file is an actionable format error.
|
|
256
|
+
malformedSince ??= Date.now();
|
|
257
|
+
if (Date.now() - malformedSince > 2000)
|
|
258
|
+
throw new SourceError('upstream', 'Freebuff chat file is persistently invalid JSON');
|
|
259
|
+
continue;
|
|
260
|
+
}
|
|
261
|
+
if (Array.isArray(data)) {
|
|
262
|
+
const latest = data.filter(m => m?.variant === 'ai').at(-1);
|
|
263
|
+
if (Array.isArray(latest?.blocks))
|
|
264
|
+
partial = latest.blocks.filter((b) => b.type === 'text' && b.textType !== 'reasoning' && typeof b.content === 'string').map((b) => b.content).join('\n').slice(-4096);
|
|
265
|
+
}
|
|
266
|
+
if (startupError) {
|
|
267
|
+
selected = file;
|
|
268
|
+
releaseCreation();
|
|
269
|
+
throw startupError;
|
|
270
|
+
}
|
|
271
|
+
if (continuation && Array.isArray(data))
|
|
272
|
+
data = data.slice(previousMessages);
|
|
273
|
+
const matched = Array.isArray(data) && data.some(m => matchesUser(m, prompt));
|
|
274
|
+
if (!matched)
|
|
275
|
+
continue;
|
|
276
|
+
// No other new process sharing this storage directory started between
|
|
277
|
+
// our snapshot and this association. Later polling reads only this chat.
|
|
278
|
+
selected = file;
|
|
279
|
+
releaseCreation();
|
|
280
|
+
sawUser = true;
|
|
281
|
+
const text = parseChat(data, prompt, isolated);
|
|
282
|
+
if (text !== undefined && sendComplete) {
|
|
283
|
+
if (ownsConversation)
|
|
284
|
+
this.conversations.set(key, file);
|
|
285
|
+
return { text };
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
if (!sawUser && Date.now() - sentAt > 10000)
|
|
289
|
+
throw new SourceError('upstream', 'Freebuff did not persist a user turn within 10 seconds; run freebuff doctor and check login or startup prompts');
|
|
290
|
+
await delay(this.options.poll ?? 200, undefined, { signal });
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
catch (error) {
|
|
294
|
+
caller.throwIfAborted();
|
|
295
|
+
if (this.shutdown.signal.aborted)
|
|
296
|
+
throw new SourceError('cancelled', 'Freebuff request cancelled during shutdown');
|
|
297
|
+
if (signal.aborted)
|
|
298
|
+
throw new SourceError('timeout', `Freebuff timed out after ${timeout}ms; check freebuff login and your free-hours balance`);
|
|
299
|
+
if (error instanceof SourceError) {
|
|
300
|
+
if (partial && !isolated)
|
|
301
|
+
error.output = partial;
|
|
302
|
+
throw error;
|
|
303
|
+
}
|
|
304
|
+
throw new SourceError('upstream', `Freebuff failed: ${error instanceof Error ? error.message : 'unknown error'}`);
|
|
305
|
+
}
|
|
306
|
+
finally {
|
|
307
|
+
try {
|
|
308
|
+
await this.mux.stop(id);
|
|
309
|
+
}
|
|
310
|
+
finally {
|
|
311
|
+
// An unassociated process must be stopped before another one can start.
|
|
312
|
+
releaseCreation();
|
|
313
|
+
this.sessions.delete(id);
|
|
314
|
+
if (acquired)
|
|
315
|
+
pool.release();
|
|
316
|
+
if (ownsConversation)
|
|
317
|
+
this.busy.delete(key);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
async chatFiles(dir) {
|
|
322
|
+
try {
|
|
323
|
+
return (await readdir(dir, { withFileTypes: true })).filter(d => d.isDirectory()).map(d => join(dir, d.name, 'chat-messages.json'));
|
|
324
|
+
}
|
|
325
|
+
catch (error) {
|
|
326
|
+
if (error.code === 'ENOENT')
|
|
327
|
+
return [];
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
async close() { this.shutdown.abort(); await Promise.all([...this.sessions].map(id => this.mux.stop(id))); await this.mux.close?.(); }
|
|
332
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
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: false }] }
|
|
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
|
+
supportsFunctionTools = true;
|
|
30
|
+
url;
|
|
31
|
+
fetcher;
|
|
32
|
+
constructor(options = {}) {
|
|
33
|
+
this.options = options;
|
|
34
|
+
this.url = loopback(options.url ?? 'http://127.0.0.1:3001/v1').href.replace(/\/$/, '');
|
|
35
|
+
this.fetcher = options.fetcher ?? fetch;
|
|
36
|
+
this.id = options.id ?? 'freellmapi';
|
|
37
|
+
}
|
|
38
|
+
async available() {
|
|
39
|
+
try {
|
|
40
|
+
const key = this.options.key || await this.options.loadKey?.();
|
|
41
|
+
if (!key)
|
|
42
|
+
return false;
|
|
43
|
+
return (await this.fetcher(`${this.url}/models`, { headers: { Authorization: `Bearer ${key}` }, redirect: 'error', signal: AbortSignal.timeout(2000) })).ok;
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async complete(request, caller) {
|
|
50
|
+
const key = this.options.key || await this.options.loadKey?.();
|
|
51
|
+
caller.throwIfAborted();
|
|
52
|
+
if (!key)
|
|
53
|
+
throw new SourceError('logged_out', 'FreeLLMAPI login unavailable; check the local router and saved login');
|
|
54
|
+
if (request.tools.some(t => t.type !== 'function'))
|
|
55
|
+
throw new SourceError('upstream', 'FreeLLMAPI Chat Completions requires function tools');
|
|
56
|
+
const signal = AbortSignal.any([caller, AbortSignal.timeout(600000)]);
|
|
57
|
+
let response;
|
|
58
|
+
try {
|
|
59
|
+
response = await this.fetcher(`${this.url}/chat/completions`, {
|
|
60
|
+
method: 'POST', redirect: 'error', signal,
|
|
61
|
+
headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${key}` },
|
|
62
|
+
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)) })
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
throw new SourceError(signal.aborted ? 'timeout' : 'upstream', 'Local FreeLLMAPI is unavailable');
|
|
67
|
+
}
|
|
68
|
+
if (!response.ok)
|
|
69
|
+
throw new SourceError(response.status === 403 ? 'refused' : response.status === 401 ? 'logged_out' : response.status === 429 ? 'quota' : 'upstream', `FreeLLMAPI returned HTTP ${response.status}`);
|
|
70
|
+
const result = await response.json();
|
|
71
|
+
const message = result.choices?.[0]?.message;
|
|
72
|
+
if (message?.refusal)
|
|
73
|
+
throw new SourceError('refused', 'FreeLLMAPI provider refused the request');
|
|
74
|
+
if (!message || (typeof message.content !== 'string' && !message.tool_calls?.length))
|
|
75
|
+
throw new SourceError('upstream', 'FreeLLMAPI returned an invalid completion');
|
|
76
|
+
return { text: message.content ?? '', ...(message.tool_calls ? { calls: message.tool_calls } : {}), inputTokens: result.usage?.prompt_tokens ?? 0, outputTokens: result.usage?.completion_tokens ?? 0 };
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -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
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
import { spawn } from 'node:child_process';
|
|
2
|
+
import { randomInt } from 'node:crypto';
|
|
3
|
+
import { constants } from 'node:fs';
|
|
4
|
+
import { access, copyFile, mkdir, mkdtemp, readFile, rename, rm, writeFile } from 'node:fs/promises';
|
|
5
|
+
import { homedir, tmpdir } from 'node:os';
|
|
6
|
+
import { delimiter, dirname, join } from 'node:path';
|
|
7
|
+
// Writes a whole config file only when its text changes, after copying the old
|
|
8
|
+
// file aside. Repeated setup runs therefore leave files and backups untouched.
|
|
9
|
+
export async function writeConfig(path, text, mode = 0o600) {
|
|
10
|
+
let old;
|
|
11
|
+
try {
|
|
12
|
+
old = await readFile(path, 'utf8');
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
if (error.code !== 'ENOENT')
|
|
16
|
+
throw error;
|
|
17
|
+
}
|
|
18
|
+
if (old === text)
|
|
19
|
+
return { changed: false };
|
|
20
|
+
await mkdir(dirname(path), { recursive: true, mode: 0o700 });
|
|
21
|
+
let backup;
|
|
22
|
+
if (old !== undefined) {
|
|
23
|
+
backup = `${path}.grunt-backup-${new Date().toISOString().replace(/[:.]/g, '-')}`;
|
|
24
|
+
await copyFile(path, backup, constants.COPYFILE_EXCL);
|
|
25
|
+
}
|
|
26
|
+
const temporary = `${path}.${process.pid}.tmp`;
|
|
27
|
+
try {
|
|
28
|
+
await writeFile(temporary, text, { mode, flag: 'wx' });
|
|
29
|
+
await rename(temporary, path);
|
|
30
|
+
}
|
|
31
|
+
finally {
|
|
32
|
+
await rm(temporary, { force: true });
|
|
33
|
+
}
|
|
34
|
+
return { changed: true, backup };
|
|
35
|
+
}
|
|
36
|
+
const objectOrEmpty = (value, what) => {
|
|
37
|
+
if (value === undefined)
|
|
38
|
+
return {};
|
|
39
|
+
if (!value || typeof value !== 'object' || Array.isArray(value))
|
|
40
|
+
throw new Error(`Invalid ${what}`);
|
|
41
|
+
return value;
|
|
42
|
+
};
|
|
43
|
+
async function readJson(path, what) {
|
|
44
|
+
try {
|
|
45
|
+
return objectOrEmpty(JSON.parse(await readFile(path, 'utf8')), what);
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (error.code === 'ENOENT')
|
|
49
|
+
return {};
|
|
50
|
+
throw new Error(`Cannot read ${what}; repair it before retrying setup`, { cause: error });
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
export async function configurePi(port = 8788) {
|
|
54
|
+
const path = join(process.env.PI_CODING_AGENT_DIR || join(homedir(), '.pi', 'agent'), 'models.json');
|
|
55
|
+
const saved = await readJson(path, 'Pi models config');
|
|
56
|
+
const providers = objectOrEmpty(saved.providers, 'Pi models config');
|
|
57
|
+
providers.grunt = { baseUrl: `http://127.0.0.1:${port}/v1`, api: 'openai-completions', apiKey: 'local',
|
|
58
|
+
models: [{ id: 'grunt', name: 'grunt', reasoning: false, input: ['text'], cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }, contextWindow: 128000, maxTokens: 4096 }] };
|
|
59
|
+
return writeConfig(path, JSON.stringify({ ...saved, providers }, null, 2) + '\n');
|
|
60
|
+
}
|
|
61
|
+
// Codex layers $CODEX_HOME/<name>.config.toml over config.toml for `codex --profile <name>`,
|
|
62
|
+
// so grunt gets its own file and the user's config.toml is never edited.
|
|
63
|
+
export const codexProfilePath = () => join(process.env.CODEX_HOME || join(homedir(), '.codex'), 'grunt.config.toml');
|
|
64
|
+
export async function configureCodex(port = 8788) {
|
|
65
|
+
return writeConfig(codexProfilePath(), `# Written by usegrunt setup for \`codex --profile grunt\`. Your config.toml is unchanged.
|
|
66
|
+
model = "grunt"
|
|
67
|
+
model_provider = "grunt"
|
|
68
|
+
|
|
69
|
+
[model_providers.grunt]
|
|
70
|
+
name = "grunt"
|
|
71
|
+
base_url = "http://127.0.0.1:${port}/v1"
|
|
72
|
+
wire_api = "responses"
|
|
73
|
+
`);
|
|
74
|
+
}
|
|
75
|
+
export const opencodeConfigDir = () => join(process.env.XDG_CONFIG_HOME || join(homedir(), '.config'), 'opencode');
|
|
76
|
+
// Adds a grunt provider and leaves OpenCode's default model alone; pick it with /models.
|
|
77
|
+
export async function configureOpenCode(port = 8788) {
|
|
78
|
+
const directory = opencodeConfigDir(), path = join(directory, 'opencode.json');
|
|
79
|
+
try {
|
|
80
|
+
await access(path);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
try {
|
|
84
|
+
await access(join(directory, 'opencode.jsonc'));
|
|
85
|
+
throw new Error(`OpenCode uses ${join(directory, 'opencode.jsonc')}, which setup does not edit. Add the grunt provider by hand: see the README`);
|
|
86
|
+
}
|
|
87
|
+
catch (error) {
|
|
88
|
+
if (error.code !== 'ENOENT')
|
|
89
|
+
throw error;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
const saved = await readJson(path, 'OpenCode config');
|
|
93
|
+
const provider = objectOrEmpty(saved.provider, 'OpenCode config');
|
|
94
|
+
provider.grunt = { name: 'grunt', npm: '@ai-sdk/openai-compatible', options: { baseURL: `http://127.0.0.1:${port}/v1`, apiKey: 'local' }, models: { grunt: { name: 'grunt' } } };
|
|
95
|
+
return writeConfig(path, JSON.stringify({ $schema: 'https://opencode.ai/config.json', ...saved, provider }, null, 2) + '\n');
|
|
96
|
+
}
|
|
97
|
+
// Free services refuse a message repeated word for word as automated use, so each check differs.
|
|
98
|
+
const prompt = (tool) => `Reply only: grunt is ready for ${tool} (check ${randomInt(1000, 10000)}). Do not use tools or change files.`;
|
|
99
|
+
export const HARNESSES = [
|
|
100
|
+
{ id: 'pi', name: 'Pi', binary: 'pi', configure: configurePi,
|
|
101
|
+
check: () => ({ args: ['-p', '--provider', 'grunt', '--model', 'grunt', '--no-session', prompt('Pi')] }),
|
|
102
|
+
launch: () => 'pi --provider grunt --model grunt (or open /model in pi and choose grunt)' },
|
|
103
|
+
{ id: 'codex', name: 'Codex', binary: 'codex', configure: configureCodex,
|
|
104
|
+
check: () => ({ args: ['exec', '--profile', 'grunt', '--skip-git-repo-check', '--ephemeral', '--sandbox', 'read-only', prompt('Codex')] }),
|
|
105
|
+
launch: () => 'codex --profile grunt' },
|
|
106
|
+
{ id: 'opencode', name: 'OpenCode', binary: 'opencode', configure: configureOpenCode,
|
|
107
|
+
// grunt's own OpenCode source sets OPENCODE_CONFIG; check the user's config without it.
|
|
108
|
+
check: () => ({ args: ['run', '-m', 'grunt/grunt', prompt('OpenCode')], env: { OPENCODE_CONFIG: undefined } }),
|
|
109
|
+
launch: () => 'opencode -m grunt/grunt (or pick grunt with /models)' },
|
|
110
|
+
{ id: 'claude', name: 'Claude Code', binary: 'claude', configure: async () => undefined,
|
|
111
|
+
check: port => ({ args: ['-p', '--model', 'grunt', prompt('Claude Code')], env: { ANTHROPIC_BASE_URL: `http://127.0.0.1:${port}`, ANTHROPIC_AUTH_TOKEN: 'local' } }),
|
|
112
|
+
launch: port => `ANTHROPIC_BASE_URL=http://127.0.0.1:${port} ANTHROPIC_AUTH_TOKEN=local claude --model grunt` }
|
|
113
|
+
];
|
|
114
|
+
export async function onPath(binary, path = process.env.PATH ?? '') {
|
|
115
|
+
for (const directory of path.split(delimiter).filter(Boolean)) {
|
|
116
|
+
try {
|
|
117
|
+
await access(join(directory, binary), constants.X_OK);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
catch { /* Try the next PATH entry. */ }
|
|
121
|
+
}
|
|
122
|
+
return false;
|
|
123
|
+
}
|
|
124
|
+
export async function detectHarnesses(harnesses = HARNESSES) {
|
|
125
|
+
const found = await Promise.all(harnesses.map(harness => onPath(harness.binary)));
|
|
126
|
+
return harnesses.filter((_, index) => found[index]);
|
|
127
|
+
}
|
|
128
|
+
export async function runHarness(binary, args, env = {}, timeoutMs = 240000) {
|
|
129
|
+
const cwd = await mkdtemp(join(tmpdir(), 'grunt-harness-check-'));
|
|
130
|
+
const merged = { ...process.env };
|
|
131
|
+
for (const [key, value] of Object.entries(env)) {
|
|
132
|
+
if (value === undefined)
|
|
133
|
+
delete merged[key];
|
|
134
|
+
else
|
|
135
|
+
merged[key] = value;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
return await new Promise((resolve, reject) => {
|
|
139
|
+
const child = spawn(binary, args, { cwd, env: merged, stdio: ['ignore', 'pipe', 'pipe'], signal: AbortSignal.timeout(timeoutMs) });
|
|
140
|
+
let stdout = '', stderr = '';
|
|
141
|
+
child.stdout.on('data', (chunk) => { stdout = (stdout + chunk.toString()).slice(-65536); });
|
|
142
|
+
child.stderr.on('data', (chunk) => { stderr = (stderr + chunk.toString()).slice(-65536); });
|
|
143
|
+
child.once('error', error => reject(new Error(error.name === 'AbortError' ? `${binary} did not answer within ${Math.round(timeoutMs / 1000)} seconds` : `Could not run ${binary}`)));
|
|
144
|
+
child.once('close', code => {
|
|
145
|
+
const lastLine = (text) => text.trim().split('\n').at(-1)?.slice(0, 200);
|
|
146
|
+
if (code !== 0)
|
|
147
|
+
reject(new Error(`${binary} exited with code ${code}${lastLine(stderr) ? `: ${lastLine(stderr)}` : ''}`));
|
|
148
|
+
else if (!stdout.trim())
|
|
149
|
+
reject(new Error(`${binary} printed no answer`));
|
|
150
|
+
else
|
|
151
|
+
resolve(stdout.trim());
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
await rm(cwd, { recursive: true, force: true });
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
export async function connectHarness(harness, port, run = runHarness) {
|
|
160
|
+
const change = await harness.configure(port);
|
|
161
|
+
const check = harness.check(port);
|
|
162
|
+
const answer = await run(harness.binary, check.args, check.env);
|
|
163
|
+
return { change, answer };
|
|
164
|
+
}
|