usegrunt 0.6.5 → 0.6.6
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 +14 -7
- package/dist/local/beatapi-setup.js +83 -0
- package/dist/local/beatapi.js +298 -0
- package/dist/local/clixad-preload.js +177 -0
- package/dist/local/clixad-setup.js +345 -0
- package/dist/local/clixad.js +398 -0
- package/dist/local/data-sharing.js +18 -2
- package/dist/local/fallback-setup.js +63 -23
- package/dist/local/freebuff.js +5 -1
- package/dist/local/freellmapi.js +3 -5
- package/dist/local/kilo.js +209 -0
- package/dist/local/main.js +12 -11
- package/dist/local/parallel.js +2 -0
- package/dist/local/perch-process.js +377 -0
- package/dist/local/perch-setup.js +297 -0
- package/dist/local/perch.js +480 -0
- package/dist/local/pollinations-setup.js +109 -0
- package/dist/local/pollinations.js +324 -0
- package/dist/local/registry.js +4 -4
- package/dist/local/setup.js +332 -62
- package/dist/local/source.js +7 -2
- package/dist/local/wizard.js +24 -21
- package/dist/registry/adapters.json +57 -33
- package/dist/registry.js +4 -2
- package/package.json +1 -1
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
import { createHash, randomUUID } from 'node:crypto';
|
|
2
|
+
import { chmod, lstat, mkdir, mkdtemp, rename, rm, writeFile } from 'node:fs/promises';
|
|
3
|
+
import { join } from 'node:path';
|
|
4
|
+
import * as Perch from './perch.js';
|
|
5
|
+
import { runPerchCommand, verifyPerchCoreFiles } from './perch-process.js';
|
|
6
|
+
import { DATA_SHARING_POLICIES, readDataSharingConsent } from './data-sharing.js';
|
|
7
|
+
const setupFailure = () => new Error('Perch active Starter verification or native login did not complete; rerun usegrunt setup');
|
|
8
|
+
async function bounded(operation, signal) {
|
|
9
|
+
let abort;
|
|
10
|
+
try {
|
|
11
|
+
return await Promise.race([Promise.resolve().then(() => {
|
|
12
|
+
if (signal.aborted)
|
|
13
|
+
throw setupFailure();
|
|
14
|
+
return operation();
|
|
15
|
+
}), new Promise((_resolve, reject) => {
|
|
16
|
+
abort = () => reject(setupFailure());
|
|
17
|
+
signal.addEventListener('abort', abort, { once: true });
|
|
18
|
+
if (signal.aborted)
|
|
19
|
+
abort();
|
|
20
|
+
})]);
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
throw setupFailure();
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
if (abort)
|
|
27
|
+
signal.removeEventListener('abort', abort);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
async function archiveBytes(response, signal) {
|
|
31
|
+
const limit = 32 * 1024 * 1024;
|
|
32
|
+
if (!response.ok || !response.body || Number(response.headers.get('content-length')) > limit)
|
|
33
|
+
throw new Error('Perch official package download failed');
|
|
34
|
+
const reader = response.body.getReader(), chunks = [];
|
|
35
|
+
let size = 0;
|
|
36
|
+
try {
|
|
37
|
+
for (;;) {
|
|
38
|
+
const chunk = await bounded(() => reader.read(), signal);
|
|
39
|
+
if (chunk.done)
|
|
40
|
+
break;
|
|
41
|
+
size += chunk.value.byteLength;
|
|
42
|
+
if (size > limit)
|
|
43
|
+
throw new Error('Perch official package exceeded its supported size');
|
|
44
|
+
chunks.push(chunk.value);
|
|
45
|
+
}
|
|
46
|
+
return Buffer.concat(chunks);
|
|
47
|
+
}
|
|
48
|
+
finally {
|
|
49
|
+
void reader.cancel().catch(() => { });
|
|
50
|
+
try {
|
|
51
|
+
reader.releaseLock();
|
|
52
|
+
}
|
|
53
|
+
catch { /* A cancelled body can still be closing. */ }
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Install the verified official package under Grunt, without lifecycle scripts or global PATH changes. */
|
|
57
|
+
export async function installPerch(directory, options = {}) {
|
|
58
|
+
if (options.signal?.aborted)
|
|
59
|
+
throw setupFailure();
|
|
60
|
+
const paths = await Perch.preparePerchDirectories(directory);
|
|
61
|
+
try {
|
|
62
|
+
await Perch.verifyPerchInstallation(directory, options.signal);
|
|
63
|
+
return paths;
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
if (options.signal?.aborted)
|
|
67
|
+
throw setupFailure();
|
|
68
|
+
}
|
|
69
|
+
const temporary = await mkdtemp(join(paths.home, '.install-'));
|
|
70
|
+
const run = options.command ?? runPerchCommand;
|
|
71
|
+
const signal = options.signal ?? new AbortController().signal;
|
|
72
|
+
try {
|
|
73
|
+
const download = AbortSignal.any([signal, AbortSignal.timeout(120000)]);
|
|
74
|
+
const response = await bounded(() => (options.fetcher ?? fetch)(Perch.PERCH_NPM_TARBALL_URL, { signal: download, redirect: 'error' }), download);
|
|
75
|
+
const bytes = await archiveBytes(response, download);
|
|
76
|
+
if (`sha512-${createHash('sha512').update(bytes).digest('base64')}` !== Perch.PERCH_NPM_INTEGRITY
|
|
77
|
+
|| createHash('sha256').update(bytes).digest('hex') !== Perch.PERCH_TARBALL_SHA256)
|
|
78
|
+
throw new Error('Perch official package integrity verification failed');
|
|
79
|
+
const archive = join(temporary, 'package.tgz');
|
|
80
|
+
await writeFile(archive, bytes, { mode: 0o600, flag: 'wx' });
|
|
81
|
+
const env = Perch.perchEnvironment(directory);
|
|
82
|
+
const extracted = await run('tar', ['-xzf', archive, '-C', temporary], { cwd: temporary, env, signal, timeoutMs: 30000, maxOutputBytes: 65536 });
|
|
83
|
+
if (extracted.code !== 0)
|
|
84
|
+
throw new Error('Perch official package extraction failed');
|
|
85
|
+
const packagePath = join(temporary, 'package');
|
|
86
|
+
const metadata = await lstat(packagePath);
|
|
87
|
+
if (!metadata.isDirectory() || metadata.isSymbolicLink() || metadata.uid !== process.getuid?.())
|
|
88
|
+
throw new Error('Perch package directory is unsafe');
|
|
89
|
+
await verifyPerchCoreFiles(packagePath, Perch.PERCH_CORE_SHA256, signal);
|
|
90
|
+
const userNpmrc = join(temporary, 'user.npmrc'), globalNpmrc = join(temporary, 'global.npmrc');
|
|
91
|
+
await writeFile(userNpmrc, '', { mode: 0o600, flag: 'wx' });
|
|
92
|
+
await writeFile(globalNpmrc, '', { mode: 0o600, flag: 'wx' });
|
|
93
|
+
const npmEnv = Object.fromEntries(Object.entries(env).filter(([key]) => !/^npm_config_/i.test(key) && !/^node_(?:options|path)$/i.test(key)));
|
|
94
|
+
const dependencies = await run('npm', ['install', '--prefix', packagePath, '--omit=dev', '--ignore-scripts', '--no-audit', '--no-fund', '--package-lock=false',
|
|
95
|
+
'--registry=https://registry.npmjs.org', '--userconfig', userNpmrc, '--globalconfig', globalNpmrc], {
|
|
96
|
+
cwd: temporary, env: npmEnv, signal, timeoutMs: 300000, maxOutputBytes: 65536
|
|
97
|
+
});
|
|
98
|
+
if (dependencies.code !== 0)
|
|
99
|
+
throw new Error('Perch private dependency installation failed');
|
|
100
|
+
await verifyPerchCoreFiles(packagePath, Perch.PERCH_CORE_SHA256, signal);
|
|
101
|
+
await chmod(packagePath, 0o700);
|
|
102
|
+
await chmod(join(packagePath, 'bin', 'perch'), 0o700);
|
|
103
|
+
if (signal.aborted)
|
|
104
|
+
throw setupFailure();
|
|
105
|
+
// Replace only this managed, fixed-version package. Native auth and all
|
|
106
|
+
// other Perch profiles remain outside the installation directory.
|
|
107
|
+
const previous = join(paths.versionHome, `.previous-${randomUUID()}`);
|
|
108
|
+
let moved = false, installed = false;
|
|
109
|
+
try {
|
|
110
|
+
try {
|
|
111
|
+
await rename(paths.package, previous);
|
|
112
|
+
moved = true;
|
|
113
|
+
}
|
|
114
|
+
catch (error) {
|
|
115
|
+
if (error.code !== 'ENOENT')
|
|
116
|
+
throw error;
|
|
117
|
+
}
|
|
118
|
+
await rename(packagePath, paths.package);
|
|
119
|
+
installed = true;
|
|
120
|
+
await Perch.writePerchInstallReceipt(directory);
|
|
121
|
+
await Perch.verifyPerchInstallation(directory, signal);
|
|
122
|
+
if (signal.aborted)
|
|
123
|
+
throw setupFailure();
|
|
124
|
+
}
|
|
125
|
+
catch (error) {
|
|
126
|
+
if (installed)
|
|
127
|
+
await rm(paths.package, { recursive: true, force: true });
|
|
128
|
+
if (moved) {
|
|
129
|
+
await rename(previous, paths.package);
|
|
130
|
+
moved = false;
|
|
131
|
+
}
|
|
132
|
+
await rm(paths.installReceipt, { force: true });
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
if (moved)
|
|
137
|
+
await rm(previous, { recursive: true, force: true });
|
|
138
|
+
}
|
|
139
|
+
return paths;
|
|
140
|
+
}
|
|
141
|
+
finally {
|
|
142
|
+
await rm(temporary, { recursive: true, force: true });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const sameEmail = (a, b) => a.trim().toLowerCase() === b.trim().toLowerCase();
|
|
146
|
+
function email(value) {
|
|
147
|
+
if (typeof value !== 'string' || value.length > 254 || !/^[^\s@"<>]+@[^\s@"<>]+\.[^\s@"<>]+$/.test(value))
|
|
148
|
+
throw setupFailure();
|
|
149
|
+
return value.trim().toLowerCase();
|
|
150
|
+
}
|
|
151
|
+
const freeGoal = (identity) => `Use only the existing authorized personal account${identity ? ` whose native CLI email is ${JSON.stringify(identity)}` : ' using the personal Google identity already connected to Squire'}. Open the visible account settings in this Perch web app using the top-right avatar button labelled Open account settings; use Account and Billing to verify the plan and allowance, and Privacy to verify the Starter Data Program. Verify this exact account has ACTIVE Starter ($0), with a vetted recurring allowance of at least 20,000 Perch Tokens per month; 20,000 is the conservative supported monthly floor, not a claim about a larger displayed offer. Exclude one-time bonuses when checking this recurring floor. A Starter placeholder before activation is insufficient: if no active plan exists, use the ordinary See plans / Start Free action to activate Starter only. Never activate a paid trial, Pro, Ultra, a subscription, a pass, purchases, top-ups or overage. Do not add a card or payment method. If paid billing, paid credits, a paid plan or overage is visible, stop without changing it. Do not assert hidden saved-card inventory. Verify the optional Starter Data Program is OFF; if it is on, turn off only that optional program through normal settings and verify the saved OFF state. Leave optional marketing off. Do not enable optional memory or claim it is disabled unless the UI actually verifies that state. Do not switch identities, downgrade, remove billing, create a duplicate account or enable BYOK. Finish only after confirming active Starter and Data Program OFF for this identity. Report required human verification for browser handoff; stop on refusal and do not bypass verification.`;
|
|
152
|
+
async function browser(options, url, goal, signal) {
|
|
153
|
+
await bounded(() => options.squire.signup('perch', url, goal), signal);
|
|
154
|
+
}
|
|
155
|
+
async function nativeLogin(options, signal, expectedEmail) {
|
|
156
|
+
const paths = Perch.perchPaths(options.directory), run = options.processes?.command ?? runPerchCommand;
|
|
157
|
+
const temporary = await mkdtemp(join(paths.home, '.login-'));
|
|
158
|
+
const lifetime = new AbortController(), combined = AbortSignal.any([signal, lifetime.signal]);
|
|
159
|
+
let pairing, pairingURL;
|
|
160
|
+
try {
|
|
161
|
+
await Perch.verifyPerchStartupIsolation(temporary);
|
|
162
|
+
for (const name of ['settings', 'state', 'grants', 'telemetry', 'nativeHome', 'consent', 'splash', 'background'])
|
|
163
|
+
await mkdir(join(temporary, name), { mode: 0o700 });
|
|
164
|
+
// The unmodified CLI executes platform opener commands. Squire owns the
|
|
165
|
+
// actual trusted HTTPS pairing page; suppress only the unmanaged launch.
|
|
166
|
+
for (const name of ['open', 'xdg-open', 'x-www-browser', 'www-browser'])
|
|
167
|
+
await writeFile(join(temporary, name), '#!/bin/sh\nexit 0\n', { mode: 0o700, flag: 'wx' });
|
|
168
|
+
const result = await run(process.execPath, [paths.binary, 'login', 'google'], {
|
|
169
|
+
cwd: temporary, env: { ...Perch.perchEnvironment(options.directory, temporary), PATH: `${temporary}:${process.env.PATH ?? '/usr/bin:/bin'}` },
|
|
170
|
+
signal: combined, timeoutMs: 600000, maxOutputBytes: 65536,
|
|
171
|
+
onLine(line) {
|
|
172
|
+
const match = line.replace(/\x1b\[[0-?]*[ -/]*[@-~]/g, '').match(/https:\/\/[^\s"'<>`]+/);
|
|
173
|
+
if (!match)
|
|
174
|
+
return;
|
|
175
|
+
// The official login command first prints its application homepage as
|
|
176
|
+
// prose, before emitting the actual scoped native authorization URL.
|
|
177
|
+
if (['https://app.perchai.app', 'https://app.perchai.app/', 'https://app.perchai.app.', 'https://app.perchai.app/.'].includes(match[0]))
|
|
178
|
+
return;
|
|
179
|
+
const url = new URL(match[0]);
|
|
180
|
+
if (url.origin !== 'https://app.perchai.app' || url.pathname !== '/api/perch-terminal/cli-auth/start' || url.username || url.password || url.hash)
|
|
181
|
+
throw setupFailure();
|
|
182
|
+
if (pairingURL) {
|
|
183
|
+
if (url.href !== pairingURL)
|
|
184
|
+
throw setupFailure();
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
pairingURL = url.href;
|
|
188
|
+
pairing = browser(options, url.href, `Complete only the Perch native CLI Google sign-in started by this Grunt process, using the same personal Starter account just verified${expectedEmail ? ` with email ${JSON.stringify(expectedEmail)}` : ''}. Authorize this native pairing only. Do not create/copy/reveal an API key or native auth token; the official CLI owns its callback and credential storage. Do not switch accounts, create a duplicate, buy a plan, enable a trial, add a card, pay for credits, enable overage, or opt in to the optional Starter Data Program. Stop on refusal; report CAPTCHA, passkey, phone or email verification for human handoff without bypassing it.`, combined);
|
|
189
|
+
void pairing.catch(() => lifetime.abort());
|
|
190
|
+
}
|
|
191
|
+
});
|
|
192
|
+
if (result.code !== 0 || !pairing)
|
|
193
|
+
throw setupFailure();
|
|
194
|
+
await pairing;
|
|
195
|
+
if (signal.aborted)
|
|
196
|
+
throw setupFailure();
|
|
197
|
+
}
|
|
198
|
+
finally {
|
|
199
|
+
lifetime.abort();
|
|
200
|
+
await rm(temporary, { recursive: true, force: true });
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
/** Foreground verifies normal Starter activation; background only reuses verified native state. */
|
|
204
|
+
export async function setupPerch(options) {
|
|
205
|
+
const approved = [...new Set([...(options.dataSharingApproved ?? []), ...await readDataSharingConsent(options.directory, DATA_SHARING_POLICIES.map(policy => policy.provider))])];
|
|
206
|
+
if (!approved.includes('perch'))
|
|
207
|
+
throw new Error('Perch data sharing requires the grouped setup approval');
|
|
208
|
+
const deadline = new AbortController(), signal = options.signal ? AbortSignal.any([options.signal, deadline.signal]) : deadline.signal;
|
|
209
|
+
const timer = setTimeout(() => deadline.abort(), 600000);
|
|
210
|
+
const paths = Perch.perchPaths(options.directory);
|
|
211
|
+
const identify = options.identity ?? Perch.verifyPerchIdentity;
|
|
212
|
+
const readMemory = options.verifyMemory ?? Perch.verifyPerchMemory;
|
|
213
|
+
const configureMemory = options.configureMemory ?? Perch.configurePerchMemory;
|
|
214
|
+
let completed = false;
|
|
215
|
+
try {
|
|
216
|
+
if (signal.aborted)
|
|
217
|
+
throw setupFailure();
|
|
218
|
+
if (options.background)
|
|
219
|
+
await Perch.verifyPerchInstallation(options.directory, signal);
|
|
220
|
+
else {
|
|
221
|
+
options.squire.approveDataSharing?.(approved);
|
|
222
|
+
await (options.install ?? installPerch)(options.directory, { signal, ...(options.fetcher ? { fetcher: options.fetcher } : {}), ...(options.processes ? { command: options.processes.command } : {}) });
|
|
223
|
+
}
|
|
224
|
+
await Perch.verifyPerchStartupIsolation(paths.work);
|
|
225
|
+
let saved;
|
|
226
|
+
try {
|
|
227
|
+
saved = await Perch.readPerchFreePlanReceipt(options.directory);
|
|
228
|
+
}
|
|
229
|
+
catch { /* A fresh foreground check is required. */ }
|
|
230
|
+
if (options.background && !saved)
|
|
231
|
+
throw setupFailure();
|
|
232
|
+
let identity = await bounded(() => identify(options.directory, signal, options.processes), signal);
|
|
233
|
+
if (identity)
|
|
234
|
+
identity = { email: email(identity.email) };
|
|
235
|
+
if (saved && identity && sameEmail(saved.email, identity.email)) {
|
|
236
|
+
let memory;
|
|
237
|
+
try {
|
|
238
|
+
memory = await bounded(() => readMemory(options.directory, signal, options.processes), signal);
|
|
239
|
+
}
|
|
240
|
+
catch {
|
|
241
|
+
if (signal.aborted || options.background)
|
|
242
|
+
throw setupFailure();
|
|
243
|
+
}
|
|
244
|
+
if (memory) {
|
|
245
|
+
if (!sameEmail(identity.email, email(memory.email)))
|
|
246
|
+
throw setupFailure();
|
|
247
|
+
completed = true;
|
|
248
|
+
options.notify?.('Perch: existing Starter native login and memory-off settings verified');
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (options.background)
|
|
253
|
+
throw new Error('Perch requires foreground Starter setup');
|
|
254
|
+
await rm(paths.receipt, { force: true });
|
|
255
|
+
if (saved && identity && !sameEmail(saved.email, identity.email))
|
|
256
|
+
throw setupFailure();
|
|
257
|
+
const expected = identity?.email ?? saved?.email;
|
|
258
|
+
await browser(options, 'https://chat.perchai.app/', freeGoal(expected), signal);
|
|
259
|
+
if (!identity) {
|
|
260
|
+
await nativeLogin(options, signal, expected);
|
|
261
|
+
await Perch.assertPerchPrivateAuth(options.directory);
|
|
262
|
+
identity = await bounded(() => identify(options.directory, signal, options.processes), signal);
|
|
263
|
+
if (!identity || (expected && !sameEmail(expected, identity.email)))
|
|
264
|
+
throw setupFailure();
|
|
265
|
+
identity = { email: email(identity.email) };
|
|
266
|
+
await browser(options, 'https://chat.perchai.app/', freeGoal(identity.email), signal);
|
|
267
|
+
}
|
|
268
|
+
if (signal.aborted)
|
|
269
|
+
throw setupFailure();
|
|
270
|
+
const configured = await bounded(() => configureMemory(options.directory, signal, options.processes), signal);
|
|
271
|
+
if (!sameEmail(identity.email, email(configured.email)))
|
|
272
|
+
throw setupFailure();
|
|
273
|
+
const memory = await bounded(() => readMemory(options.directory, signal, options.processes), signal);
|
|
274
|
+
if (!sameEmail(identity.email, email(memory.email)))
|
|
275
|
+
throw setupFailure();
|
|
276
|
+
if (signal.aborted)
|
|
277
|
+
throw setupFailure();
|
|
278
|
+
await Perch.writePerchFreePlanReceipt(options.directory, { version: 1, plan: 'Starter', email: identity.email, monthlyAllowancePT: 20000, dataProgramEnabled: false,
|
|
279
|
+
memoryUseEnabled: false, memoryLearningEnabled: false, checkedAt: new Date().toISOString() });
|
|
280
|
+
if (signal.aborted)
|
|
281
|
+
throw setupFailure();
|
|
282
|
+
completed = true;
|
|
283
|
+
options.notify?.('Perch: active Starter verified with the optional Data Program and both native memory switches off');
|
|
284
|
+
}
|
|
285
|
+
catch {
|
|
286
|
+
if (!options.background)
|
|
287
|
+
await rm(paths.receipt, { force: true }).catch(() => { });
|
|
288
|
+
throw setupFailure();
|
|
289
|
+
}
|
|
290
|
+
finally {
|
|
291
|
+
clearTimeout(timer);
|
|
292
|
+
if (!completed && !options.background) {
|
|
293
|
+
deadline.abort();
|
|
294
|
+
await bounded(() => options.squire.cancelSignup?.('perch') ?? Promise.resolve(), AbortSignal.timeout(10000)).catch(() => { });
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|