flyspace-cli 0.1.59 → 0.1.60
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/dist/{chunk-44WHW7NK.js → chunk-PXZUNZ4S.js} +54 -2
- package/dist/{chunk-7W5FEG63.js → chunk-SIR6PTDT.js} +370 -918
- package/dist/{chunk-XACRG7BO.js → chunk-XYS7SVWC.js} +594 -13
- package/dist/cli.js +247 -222
- package/dist/index.js +2 -2
- package/dist/{threads-I52LSA5X.js → threads-QQRWT3OM.js} +23 -2
- package/package.json +1 -1
- package/system.capabilities.md +2 -0
|
@@ -211,11 +211,10 @@ function sweepScratchDir(now = Date.now()) {
|
|
|
211
211
|
return removed;
|
|
212
212
|
}
|
|
213
213
|
|
|
214
|
-
// ../api/src/
|
|
215
|
-
import
|
|
216
|
-
import { existsSync as existsSync2
|
|
217
|
-
import {
|
|
218
|
-
import { isAbsolute, relative, resolve as resolve2, sep } from "path";
|
|
214
|
+
// ../api/src/config.ts
|
|
215
|
+
import dotenv from "dotenv";
|
|
216
|
+
import { existsSync as existsSync2 } from "fs";
|
|
217
|
+
import { resolve as resolve2 } from "path";
|
|
219
218
|
|
|
220
219
|
// ../api/src/effort.ts
|
|
221
220
|
var EFFORT_LEVELS = ["low", "medium", "high", "max"];
|
|
@@ -241,7 +240,90 @@ function resolveCodexEffort(effort) {
|
|
|
241
240
|
return CODEX_EFFORT[effort];
|
|
242
241
|
}
|
|
243
242
|
|
|
243
|
+
// ../api/src/config.ts
|
|
244
|
+
var DEFAULT_CODEX_MODEL = "";
|
|
245
|
+
function mcpStrictConfigEnabled() {
|
|
246
|
+
return process.env["MCP_STRICT_CONFIG"]?.trim() === "true";
|
|
247
|
+
}
|
|
248
|
+
function loadEnvFiles() {
|
|
249
|
+
if (existsSync2(paths.CONFIG_FILE)) {
|
|
250
|
+
dotenv.config({ path: paths.CONFIG_FILE, override: true, quiet: true });
|
|
251
|
+
}
|
|
252
|
+
const candidates = [resolve2(paths.PACKAGE_ROOT, ".env"), resolve2(process.cwd(), ".env")];
|
|
253
|
+
for (const path of new Set(candidates)) {
|
|
254
|
+
if (existsSync2(path)) {
|
|
255
|
+
dotenv.config({ path, override: false, quiet: true });
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
function parseBudget(raw) {
|
|
260
|
+
if (raw === void 0) return 10;
|
|
261
|
+
const n = Number.parseFloat(raw);
|
|
262
|
+
if (!Number.isFinite(n) || n <= 0) return void 0;
|
|
263
|
+
return n;
|
|
264
|
+
}
|
|
265
|
+
function parseEffort(raw) {
|
|
266
|
+
const trimmed = raw?.trim().toLowerCase();
|
|
267
|
+
return trimmed && isEffort(trimmed) ? trimmed : void 0;
|
|
268
|
+
}
|
|
269
|
+
var SLACK_ENV = ["SLACK_BOT_TOKEN", "SLACK_APP_TOKEN", "SLACK_SIGNING_SECRET"];
|
|
270
|
+
function slackConfigured(env = process.env) {
|
|
271
|
+
return SLACK_ENV.some((name) => env[name]?.trim());
|
|
272
|
+
}
|
|
273
|
+
function requireSlackBotToken(config) {
|
|
274
|
+
if (!config.slackBotToken) {
|
|
275
|
+
throw new Error("Slack is not configured: set SLACK_BOT_TOKEN to use Slack commands");
|
|
276
|
+
}
|
|
277
|
+
return config.slackBotToken;
|
|
278
|
+
}
|
|
279
|
+
function requireEnv(name) {
|
|
280
|
+
const value = process.env[name];
|
|
281
|
+
if (!value) {
|
|
282
|
+
throw new Error(`Missing required environment variable: ${name}`);
|
|
283
|
+
}
|
|
284
|
+
return value;
|
|
285
|
+
}
|
|
286
|
+
function loadConfig() {
|
|
287
|
+
const ownerUserId = process.env["OWNER_USER_ID"] || void 0;
|
|
288
|
+
const allowedUserIds = new Set(
|
|
289
|
+
(process.env["ALLOWED_USER_IDS"] ?? "").split(",").map((s) => s.trim()).filter(Boolean)
|
|
290
|
+
);
|
|
291
|
+
if (ownerUserId && allowedUserIds.size > 0) {
|
|
292
|
+
allowedUserIds.add(ownerUserId);
|
|
293
|
+
}
|
|
294
|
+
const autoRespondChannelIds = new Set(
|
|
295
|
+
(process.env["AUTO_RESPOND_CHANNEL_IDS"] ?? "").split(",").map((s) => s.trim()).filter(Boolean)
|
|
296
|
+
);
|
|
297
|
+
const slackEnglishOnlyExemptChannelIds = new Set(
|
|
298
|
+
(process.env["SLACK_ENGLISH_ONLY_EXEMPT_CHANNEL_IDS"] ?? "").split(",").map((s) => s.trim()).filter(Boolean)
|
|
299
|
+
);
|
|
300
|
+
const slack = slackConfigured();
|
|
301
|
+
return {
|
|
302
|
+
slackBotToken: slack ? requireEnv("SLACK_BOT_TOKEN") : void 0,
|
|
303
|
+
slackAppToken: slack ? requireEnv("SLACK_APP_TOKEN") : void 0,
|
|
304
|
+
slackSigningSecret: slack ? requireEnv("SLACK_SIGNING_SECRET") : void 0,
|
|
305
|
+
claudeCwd: process.env["CLAUDE_CWD"] ?? process.cwd(),
|
|
306
|
+
claudeConfigDir: process.env["CLAUDE_CONFIG_DIR"] || void 0,
|
|
307
|
+
claudeCodeOauthToken: process.env["CLAUDE_CODE_OAUTH_TOKEN"] || void 0,
|
|
308
|
+
botName: process.env["BOT_NAME"] ?? "Bot",
|
|
309
|
+
allowedUserIds,
|
|
310
|
+
maxTurns: parseInt(process.env["MAX_TURNS"] ?? "10", 10),
|
|
311
|
+
model: process.env["CUSTIE_MODEL"]?.trim() || "sonnet",
|
|
312
|
+
effort: parseEffort(process.env["CUSTIE_EFFORT"]),
|
|
313
|
+
maxBudgetUsd: parseBudget(process.env["MAX_BUDGET_USD"]),
|
|
314
|
+
ownerUserId,
|
|
315
|
+
autoRespondChannelIds,
|
|
316
|
+
codexModel: process.env["CUSTIE_CODEX_MODEL"]?.trim() || DEFAULT_CODEX_MODEL,
|
|
317
|
+
slackEnglishOnly: process.env["SLACK_ENGLISH_ONLY"] === "true",
|
|
318
|
+
slackEnglishOnlyExemptChannelIds
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
|
|
244
322
|
// ../api/src/channels.ts
|
|
323
|
+
import { createHash } from "crypto";
|
|
324
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync2, realpathSync, statSync as statSync2 } from "fs";
|
|
325
|
+
import { homedir as homedir2 } from "os";
|
|
326
|
+
import { isAbsolute, relative, resolve as resolve3, sep } from "path";
|
|
245
327
|
function isPermissionMode(value) {
|
|
246
328
|
return value === "bypass" || value === "ask";
|
|
247
329
|
}
|
|
@@ -249,7 +331,7 @@ function isLogicalCwd(p) {
|
|
|
249
331
|
return /^[a-z0-9][a-z0-9-]*\/[a-z0-9][a-z0-9-]*$/.test(p);
|
|
250
332
|
}
|
|
251
333
|
function logicalRoot() {
|
|
252
|
-
return process.env["FLYSPACE_ROOT"] ||
|
|
334
|
+
return process.env["FLYSPACE_ROOT"] || resolve3(homedir2(), "Flyspace");
|
|
253
335
|
}
|
|
254
336
|
var workspaceRoots = /* @__PURE__ */ new Map();
|
|
255
337
|
var workspacePermissionModes = /* @__PURE__ */ new Map();
|
|
@@ -295,7 +377,7 @@ async function refreshWorkspaceRoots() {
|
|
|
295
377
|
function resolveLogicalCwd(p) {
|
|
296
378
|
const [slug, name] = p.split("/");
|
|
297
379
|
const root = workspaceRoots.get(slug);
|
|
298
|
-
return root ?
|
|
380
|
+
return root ? resolve3(root, name) : resolve3(logicalRoot(), p);
|
|
299
381
|
}
|
|
300
382
|
function isUnderLogicalRoot(cwd) {
|
|
301
383
|
return [logicalRoot(), ...workspaceRoots.values()].some((r) => cwd.startsWith(r + sep));
|
|
@@ -310,7 +392,7 @@ function toLogicalCwd(cwd) {
|
|
|
310
392
|
return cwd;
|
|
311
393
|
}
|
|
312
394
|
function ensureLogicalCwdDir(cwd) {
|
|
313
|
-
if (!isUnderLogicalRoot(cwd) ||
|
|
395
|
+
if (!isUnderLogicalRoot(cwd) || existsSync3(cwd)) return;
|
|
314
396
|
mkdirSync2(cwd, { recursive: true });
|
|
315
397
|
}
|
|
316
398
|
function expandPath(p) {
|
|
@@ -318,9 +400,9 @@ function expandPath(p) {
|
|
|
318
400
|
if (expanded === "~") {
|
|
319
401
|
expanded = homedir2();
|
|
320
402
|
} else if (expanded.startsWith("~/")) {
|
|
321
|
-
expanded =
|
|
403
|
+
expanded = resolve3(homedir2(), expanded.slice(2));
|
|
322
404
|
}
|
|
323
|
-
return isAbsolute(expanded) ? expanded :
|
|
405
|
+
return isAbsolute(expanded) ? expanded : resolve3(expanded);
|
|
324
406
|
}
|
|
325
407
|
function normaliseMemberEmails(raw) {
|
|
326
408
|
if (!Array.isArray(raw)) return [];
|
|
@@ -485,7 +567,7 @@ function stopChannelRegistryPolling() {
|
|
|
485
567
|
}
|
|
486
568
|
function isCwdUsable(cwd) {
|
|
487
569
|
try {
|
|
488
|
-
return
|
|
570
|
+
return existsSync3(cwd) && statSync2(cwd).isDirectory();
|
|
489
571
|
} catch {
|
|
490
572
|
return false;
|
|
491
573
|
}
|
|
@@ -619,7 +701,7 @@ async function assertCwdInWorkspace(profile, cwd) {
|
|
|
619
701
|
const workspaces = data ?? [];
|
|
620
702
|
const ownRoot = workspaces.find((w) => w.slug === profile)?.root_path;
|
|
621
703
|
const home2 = ownRoot?.match(/^(\/Users\/[^/]+|\/home\/[^/]+|\/root)(?=\/|$)/)?.[1];
|
|
622
|
-
const absolute = home2 && (cwd === "~" || cwd.startsWith("~/")) ?
|
|
704
|
+
const absolute = home2 && (cwd === "~" || cwd.startsWith("~/")) ? resolve3(home2, cwd.slice(2)) : expandPath(cwd);
|
|
623
705
|
const path = realpathOrSelf(absolute);
|
|
624
706
|
const roots = workspaces.filter((w) => w.root_path).map((w) => ({ slug: w.slug, root: realpathOrSelf(expandPath(w.root_path)) }));
|
|
625
707
|
const owner = roots.filter(({ root }) => path === root || path.startsWith(root + sep)).sort((a, b) => b.root.length - a.root.length)[0];
|
|
@@ -1186,13 +1268,471 @@ var SessionStore = class {
|
|
|
1186
1268
|
}
|
|
1187
1269
|
};
|
|
1188
1270
|
|
|
1271
|
+
// src/auth/device-auth.ts
|
|
1272
|
+
import { createPrivateKey, createPublicKey, generateKeyPairSync, sign } from "crypto";
|
|
1273
|
+
import { existsSync as existsSync4, mkdirSync as mkdirSync3, readFileSync, rmSync as rmSync2, writeFileSync } from "fs";
|
|
1274
|
+
import { dirname, join as join2 } from "path";
|
|
1275
|
+
var DEFAULT_API_URL = "https://app.flyspace.ai";
|
|
1276
|
+
var EXPIRY_SKEW_MS = 6e4;
|
|
1277
|
+
var AuthApiError = class extends Error {
|
|
1278
|
+
constructor(message, status) {
|
|
1279
|
+
super(message);
|
|
1280
|
+
this.status = status;
|
|
1281
|
+
this.name = "AuthApiError";
|
|
1282
|
+
}
|
|
1283
|
+
};
|
|
1284
|
+
function apiUrl() {
|
|
1285
|
+
return (process.env["FLYSPACE_API_URL"] || DEFAULT_API_URL).replace(/\/+$/, "");
|
|
1286
|
+
}
|
|
1287
|
+
function deviceKeyPath() {
|
|
1288
|
+
return join2(paths.BASE_CONFIG_DIR, "device.key");
|
|
1289
|
+
}
|
|
1290
|
+
function credentialsPath() {
|
|
1291
|
+
return join2(paths.BASE_CONFIG_DIR, "credentials.json");
|
|
1292
|
+
}
|
|
1293
|
+
function ensureDeviceKey(path = deviceKeyPath()) {
|
|
1294
|
+
let created = false;
|
|
1295
|
+
if (!existsSync4(path)) {
|
|
1296
|
+
const { privateKey: privateKey2 } = generateKeyPairSync("ed25519");
|
|
1297
|
+
mkdirSync3(dirname(path), { recursive: true });
|
|
1298
|
+
writeFileSync(path, privateKey2.export({ type: "pkcs8", format: "pem" }), { mode: 384 });
|
|
1299
|
+
created = true;
|
|
1300
|
+
}
|
|
1301
|
+
const privateKey = createPrivateKey(readFileSync(path, "utf8"));
|
|
1302
|
+
const publicKeyBase64 = createPublicKey(privateKey).export({ type: "spki", format: "der" }).toString(
|
|
1303
|
+
"base64"
|
|
1304
|
+
);
|
|
1305
|
+
return { privateKey, publicKeyBase64, created };
|
|
1306
|
+
}
|
|
1307
|
+
function loadCredentials(path = credentialsPath()) {
|
|
1308
|
+
if (!existsSync4(path)) return null;
|
|
1309
|
+
try {
|
|
1310
|
+
return JSON.parse(readFileSync(path, "utf8"));
|
|
1311
|
+
} catch {
|
|
1312
|
+
return null;
|
|
1313
|
+
}
|
|
1314
|
+
}
|
|
1315
|
+
function saveCredentials(creds, path = credentialsPath()) {
|
|
1316
|
+
mkdirSync3(dirname(path), { recursive: true });
|
|
1317
|
+
writeFileSync(path, `${JSON.stringify(creds, null, 2)}
|
|
1318
|
+
`, { mode: 384 });
|
|
1319
|
+
}
|
|
1320
|
+
function clearLocalAuth() {
|
|
1321
|
+
rmSync2(credentialsPath(), { force: true });
|
|
1322
|
+
rmSync2(deviceKeyPath(), { force: true });
|
|
1323
|
+
}
|
|
1324
|
+
async function post(base, path, body, token) {
|
|
1325
|
+
return fetch(`${base}${path}`, {
|
|
1326
|
+
method: "POST",
|
|
1327
|
+
headers: {
|
|
1328
|
+
"content-type": "application/json",
|
|
1329
|
+
...token ? { authorization: `Bearer ${token}` } : {}
|
|
1330
|
+
},
|
|
1331
|
+
body: JSON.stringify(body)
|
|
1332
|
+
});
|
|
1333
|
+
}
|
|
1334
|
+
async function readJson(res) {
|
|
1335
|
+
try {
|
|
1336
|
+
return await res.json();
|
|
1337
|
+
} catch {
|
|
1338
|
+
return {};
|
|
1339
|
+
}
|
|
1340
|
+
}
|
|
1341
|
+
async function expectOk(res, what) {
|
|
1342
|
+
const json = await readJson(res);
|
|
1343
|
+
if (!res.ok) {
|
|
1344
|
+
const reason = typeof json["error"] === "string" ? json["error"] : res.statusText;
|
|
1345
|
+
throw new AuthApiError(`${what} failed (${res.status}): ${reason}`, res.status);
|
|
1346
|
+
}
|
|
1347
|
+
return json;
|
|
1348
|
+
}
|
|
1349
|
+
function toSlugs(value) {
|
|
1350
|
+
return Array.isArray(value) ? value.filter((v) => typeof v === "string") : [];
|
|
1351
|
+
}
|
|
1352
|
+
function toGrant(json) {
|
|
1353
|
+
const user = json["user"] ?? {};
|
|
1354
|
+
return {
|
|
1355
|
+
accessToken: String(json["access_token"]),
|
|
1356
|
+
expiresIn: Number(json["expires_in"]),
|
|
1357
|
+
deviceId: String(json["device_id"]),
|
|
1358
|
+
userEmail: String(user["email"] ?? ""),
|
|
1359
|
+
workspaces: toSlugs(json["workspaces"])
|
|
1360
|
+
};
|
|
1361
|
+
}
|
|
1362
|
+
async function requestDeviceCode(base, input) {
|
|
1363
|
+
const res = await post(base, "/api/auth/device/code", {
|
|
1364
|
+
public_key: input.publicKey,
|
|
1365
|
+
name: input.name,
|
|
1366
|
+
platform: input.platform
|
|
1367
|
+
});
|
|
1368
|
+
const json = await expectOk(res, "Requesting a device code");
|
|
1369
|
+
return {
|
|
1370
|
+
deviceCode: String(json["device_code"]),
|
|
1371
|
+
userCode: String(json["user_code"]),
|
|
1372
|
+
verificationUri: String(json["verification_uri"]),
|
|
1373
|
+
expiresIn: Number(json["expires_in"]),
|
|
1374
|
+
interval: Number(json["interval"] ?? 5)
|
|
1375
|
+
};
|
|
1376
|
+
}
|
|
1377
|
+
async function pollDeviceToken(base, deviceCode) {
|
|
1378
|
+
const res = await post(base, "/api/auth/device/token", { device_code: deviceCode });
|
|
1379
|
+
const json = await readJson(res);
|
|
1380
|
+
if (res.ok) return { status: "approved", grant: toGrant(json) };
|
|
1381
|
+
switch (json["error"]) {
|
|
1382
|
+
case "authorization_pending":
|
|
1383
|
+
return { status: "pending" };
|
|
1384
|
+
case "slow_down":
|
|
1385
|
+
return { status: "slow_down" };
|
|
1386
|
+
case "access_denied":
|
|
1387
|
+
return { status: "denied" };
|
|
1388
|
+
case "expired_token":
|
|
1389
|
+
return { status: "expired" };
|
|
1390
|
+
default:
|
|
1391
|
+
throw new AuthApiError(`Polling for approval failed (${res.status}): ${String(json["error"] ?? res.statusText)}`, res.status);
|
|
1392
|
+
}
|
|
1393
|
+
}
|
|
1394
|
+
async function refreshAccessToken(creds, privateKey) {
|
|
1395
|
+
const challenge = await expectOk(
|
|
1396
|
+
await post(creds.apiUrl, "/api/auth/device/challenge", { device_id: creds.deviceId }),
|
|
1397
|
+
"Requesting a refresh challenge"
|
|
1398
|
+
);
|
|
1399
|
+
const nonce = String(challenge["nonce"]);
|
|
1400
|
+
const signature = sign(null, Buffer.from(nonce), privateKey).toString("base64");
|
|
1401
|
+
const json = await expectOk(
|
|
1402
|
+
await post(creds.apiUrl, "/api/auth/device/refresh", { device_id: creds.deviceId, nonce, signature }),
|
|
1403
|
+
"Refreshing the access token"
|
|
1404
|
+
);
|
|
1405
|
+
return {
|
|
1406
|
+
...creds,
|
|
1407
|
+
accessToken: String(json["access_token"]),
|
|
1408
|
+
accessTokenExpiresAt: Date.now() + Number(json["expires_in"]) * 1e3
|
|
1409
|
+
};
|
|
1410
|
+
}
|
|
1411
|
+
async function fetchWhoAmI(base, token) {
|
|
1412
|
+
const res = await fetch(`${base}/api/auth/device/me`, { headers: { authorization: `Bearer ${token}` } });
|
|
1413
|
+
const json = await expectOk(res, "Checking the session");
|
|
1414
|
+
const user = json["user"] ?? {};
|
|
1415
|
+
const device = json["device"] ?? {};
|
|
1416
|
+
return {
|
|
1417
|
+
userEmail: String(user["email"] ?? ""),
|
|
1418
|
+
deviceId: String(device["id"] ?? ""),
|
|
1419
|
+
deviceName: String(device["name"] ?? ""),
|
|
1420
|
+
workspaces: toSlugs(json["workspaces"])
|
|
1421
|
+
};
|
|
1422
|
+
}
|
|
1423
|
+
async function boundWorkspaces() {
|
|
1424
|
+
const creds = loadCredentials();
|
|
1425
|
+
if (!creds) return null;
|
|
1426
|
+
try {
|
|
1427
|
+
const me = await fetchWhoAmI(creds.apiUrl, await getAccessToken());
|
|
1428
|
+
saveCredentials({ ...loadCredentials() ?? creds, workspaces: me.workspaces });
|
|
1429
|
+
return { workspaces: me.workspaces, fresh: true };
|
|
1430
|
+
} catch {
|
|
1431
|
+
return { workspaces: creds.workspaces ?? [], fresh: false };
|
|
1432
|
+
}
|
|
1433
|
+
}
|
|
1434
|
+
async function revokeDevice(base, token) {
|
|
1435
|
+
await expectOk(await post(base, "/api/auth/device/revoke", {}, token), "Revoking this device");
|
|
1436
|
+
}
|
|
1437
|
+
async function getAccessToken() {
|
|
1438
|
+
const fromEnv = process.env["FLYSPACE_TOKEN"];
|
|
1439
|
+
if (fromEnv) return fromEnv;
|
|
1440
|
+
const creds = loadCredentials();
|
|
1441
|
+
if (!creds) return null;
|
|
1442
|
+
if (creds.accessTokenExpiresAt - EXPIRY_SKEW_MS > Date.now()) return creds.accessToken;
|
|
1443
|
+
const { privateKey } = ensureDeviceKey();
|
|
1444
|
+
const refreshed = await refreshAccessToken(creds, privateKey);
|
|
1445
|
+
saveCredentials(refreshed);
|
|
1446
|
+
return refreshed.accessToken;
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
// src/dashboard/host-lock.ts
|
|
1450
|
+
import { execFileSync } from "child_process";
|
|
1451
|
+
import { randomBytes } from "crypto";
|
|
1452
|
+
import { mkdirSync as mkdirSync4, readFileSync as readFileSync2, rmSync as rmSync3, writeFileSync as writeFileSync2 } from "fs";
|
|
1453
|
+
import { join as join3 } from "path";
|
|
1454
|
+
function hostLockPath() {
|
|
1455
|
+
return join3(paths.BASE_DATA_DIR, "host.lock");
|
|
1456
|
+
}
|
|
1457
|
+
function isLiveFlyspace(pid) {
|
|
1458
|
+
try {
|
|
1459
|
+
process.kill(pid, 0);
|
|
1460
|
+
const command = execFileSync("ps", ["-p", String(pid), "-o", "command="], {
|
|
1461
|
+
encoding: "utf8"
|
|
1462
|
+
});
|
|
1463
|
+
return command.includes("flyspace") || command.includes("host-server");
|
|
1464
|
+
} catch {
|
|
1465
|
+
return false;
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
function readLockPid(path) {
|
|
1469
|
+
try {
|
|
1470
|
+
const pid = Number(JSON.parse(readFileSync2(path, "utf8")).pid);
|
|
1471
|
+
return Number.isInteger(pid) && pid > 0 ? pid : null;
|
|
1472
|
+
} catch {
|
|
1473
|
+
return null;
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
function acquireHostLock() {
|
|
1477
|
+
const path = hostLockPath();
|
|
1478
|
+
mkdirSync4(paths.BASE_DATA_DIR, { recursive: true });
|
|
1479
|
+
const body = JSON.stringify({
|
|
1480
|
+
pid: process.pid,
|
|
1481
|
+
startedAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1482
|
+
execPath: process.argv[1] ?? ""
|
|
1483
|
+
});
|
|
1484
|
+
for (let attempt = 0; attempt < 2; attempt++) {
|
|
1485
|
+
try {
|
|
1486
|
+
writeFileSync2(path, body, { flag: "wx", mode: 384 });
|
|
1487
|
+
const release = () => {
|
|
1488
|
+
if (readLockPid(path) === process.pid) rmSync3(path, { force: true });
|
|
1489
|
+
};
|
|
1490
|
+
process.once("exit", release);
|
|
1491
|
+
return release;
|
|
1492
|
+
} catch (err) {
|
|
1493
|
+
if (err.code !== "EEXIST") throw err;
|
|
1494
|
+
const holder = readLockPid(path);
|
|
1495
|
+
if (holder === process.pid) return () => {
|
|
1496
|
+
};
|
|
1497
|
+
if (holder && isLiveFlyspace(holder)) {
|
|
1498
|
+
throw new Error(
|
|
1499
|
+
`Another Flyspace Host is already running on this machine (pid ${holder}). Stop it first, or check for a second CLI install (\`which -a flyspace\`). Two Hosts overwrite each other's skill mirror.`
|
|
1500
|
+
);
|
|
1501
|
+
}
|
|
1502
|
+
rmSync3(path, { force: true });
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
throw new Error(`Could not take the Host lock at ${path}`);
|
|
1506
|
+
}
|
|
1507
|
+
function publishHostEndpoint(port) {
|
|
1508
|
+
const path = hostLockPath();
|
|
1509
|
+
if (readLockPid(path) !== process.pid) return null;
|
|
1510
|
+
const token = randomBytes(32).toString("hex");
|
|
1511
|
+
const lock = JSON.parse(readFileSync2(path, "utf8"));
|
|
1512
|
+
writeFileSync2(path, JSON.stringify({ ...lock, port, token }), { mode: 384 });
|
|
1513
|
+
return token;
|
|
1514
|
+
}
|
|
1515
|
+
function readHostEndpoint() {
|
|
1516
|
+
try {
|
|
1517
|
+
const { pid, port, token } = JSON.parse(readFileSync2(hostLockPath(), "utf8"));
|
|
1518
|
+
if (pid === process.pid || !Number.isInteger(port) || typeof token !== "string") return null;
|
|
1519
|
+
return { port, token };
|
|
1520
|
+
} catch {
|
|
1521
|
+
return null;
|
|
1522
|
+
}
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
// src/dashboard/host-db-client.ts
|
|
1526
|
+
import { createClient } from "@supabase/supabase-js";
|
|
1527
|
+
import { randomUUID as randomUUID2 } from "crypto";
|
|
1528
|
+
var REQUEST_TIMEOUT_MS = 3e4;
|
|
1529
|
+
var pending = /* @__PURE__ */ new Map();
|
|
1530
|
+
var sender = null;
|
|
1531
|
+
var onSender = [];
|
|
1532
|
+
function setHostDbSender(fn) {
|
|
1533
|
+
sender = fn;
|
|
1534
|
+
if (!fn) return;
|
|
1535
|
+
const waiting = onSender;
|
|
1536
|
+
onSender = [];
|
|
1537
|
+
for (const wake of waiting) wake();
|
|
1538
|
+
}
|
|
1539
|
+
function sendAgentFailure(report) {
|
|
1540
|
+
sender?.({ type: "agent-failure", ...report });
|
|
1541
|
+
}
|
|
1542
|
+
function handleHostDbResult(id, res) {
|
|
1543
|
+
const p = pending.get(id);
|
|
1544
|
+
if (!p) return;
|
|
1545
|
+
pending.delete(id);
|
|
1546
|
+
clearTimeout(p.timer);
|
|
1547
|
+
p.resolve(res);
|
|
1548
|
+
}
|
|
1549
|
+
function whenConnected() {
|
|
1550
|
+
if (sender) return Promise.resolve(sender);
|
|
1551
|
+
return new Promise((resolve4, reject) => {
|
|
1552
|
+
const timer = setTimeout(() => {
|
|
1553
|
+
onSender = onSender.filter((w) => w !== wake);
|
|
1554
|
+
reject(new Error("Host is not connected to the Data API"));
|
|
1555
|
+
}, REQUEST_TIMEOUT_MS);
|
|
1556
|
+
const wake = () => {
|
|
1557
|
+
clearTimeout(timer);
|
|
1558
|
+
resolve4(sender);
|
|
1559
|
+
};
|
|
1560
|
+
onSender.push(wake);
|
|
1561
|
+
});
|
|
1562
|
+
}
|
|
1563
|
+
async function requestHostDb(req) {
|
|
1564
|
+
const send = await whenConnected();
|
|
1565
|
+
const id = randomUUID2();
|
|
1566
|
+
return new Promise((resolve4, reject) => {
|
|
1567
|
+
const timer = setTimeout(() => {
|
|
1568
|
+
pending.delete(id);
|
|
1569
|
+
reject(new Error("Data API did not answer in time"));
|
|
1570
|
+
}, REQUEST_TIMEOUT_MS);
|
|
1571
|
+
pending.set(id, { resolve: resolve4, reject, timer });
|
|
1572
|
+
send({ type: "db", id, req });
|
|
1573
|
+
});
|
|
1574
|
+
}
|
|
1575
|
+
var pendingUrls = /* @__PURE__ */ new Map();
|
|
1576
|
+
function handleAttachmentUrlResult(id, url) {
|
|
1577
|
+
pendingUrls.get(id)?.(url);
|
|
1578
|
+
pendingUrls.delete(id);
|
|
1579
|
+
}
|
|
1580
|
+
async function requestAttachmentUrl(key) {
|
|
1581
|
+
const send = await whenConnected();
|
|
1582
|
+
const id = randomUUID2();
|
|
1583
|
+
return new Promise((resolve4, reject) => {
|
|
1584
|
+
const timer = setTimeout(() => {
|
|
1585
|
+
pendingUrls.delete(id);
|
|
1586
|
+
reject(new Error("Data API did not answer in time"));
|
|
1587
|
+
}, REQUEST_TIMEOUT_MS);
|
|
1588
|
+
pendingUrls.set(id, (url) => {
|
|
1589
|
+
clearTimeout(timer);
|
|
1590
|
+
resolve4(url);
|
|
1591
|
+
});
|
|
1592
|
+
send({ type: "attachment-url", id, key });
|
|
1593
|
+
});
|
|
1594
|
+
}
|
|
1595
|
+
var pendingSvgs = /* @__PURE__ */ new Map();
|
|
1596
|
+
function handlePluginSvgResult(id, url) {
|
|
1597
|
+
pendingSvgs.get(id)?.(url);
|
|
1598
|
+
pendingSvgs.delete(id);
|
|
1599
|
+
}
|
|
1600
|
+
async function requestPluginSvg(workspace, svg) {
|
|
1601
|
+
const send = await whenConnected();
|
|
1602
|
+
const id = randomUUID2();
|
|
1603
|
+
return new Promise((resolve4, reject) => {
|
|
1604
|
+
const timer = setTimeout(() => {
|
|
1605
|
+
pendingSvgs.delete(id);
|
|
1606
|
+
reject(new Error("Data API did not answer in time"));
|
|
1607
|
+
}, REQUEST_TIMEOUT_MS);
|
|
1608
|
+
pendingSvgs.set(id, (url) => {
|
|
1609
|
+
clearTimeout(timer);
|
|
1610
|
+
resolve4(url);
|
|
1611
|
+
});
|
|
1612
|
+
send({ type: "plugin-svg", id, workspace, svg });
|
|
1613
|
+
});
|
|
1614
|
+
}
|
|
1615
|
+
var pendingShares = /* @__PURE__ */ new Map();
|
|
1616
|
+
function handleDocShareResult(id, res) {
|
|
1617
|
+
pendingShares.get(id)?.(res);
|
|
1618
|
+
pendingShares.delete(id);
|
|
1619
|
+
}
|
|
1620
|
+
async function requestDocShare(channelId, path, slug) {
|
|
1621
|
+
const send = await whenConnected();
|
|
1622
|
+
const id = randomUUID2();
|
|
1623
|
+
return new Promise((resolve4) => {
|
|
1624
|
+
const timer = setTimeout(() => {
|
|
1625
|
+
pendingShares.delete(id);
|
|
1626
|
+
resolve4({ error: "Data API did not answer in time" });
|
|
1627
|
+
}, 15e4);
|
|
1628
|
+
pendingShares.set(id, (res) => {
|
|
1629
|
+
clearTimeout(timer);
|
|
1630
|
+
resolve4(res);
|
|
1631
|
+
});
|
|
1632
|
+
send({ type: "doc-share", id, channelId, path, ...slug ? { slug } : {} });
|
|
1633
|
+
});
|
|
1634
|
+
}
|
|
1635
|
+
var LOCAL_DOC_SHARE_PATH = "/internal/doc-share";
|
|
1636
|
+
var pendingRespond = /* @__PURE__ */ new Map();
|
|
1637
|
+
function handleShouldRespondResult(id, respond) {
|
|
1638
|
+
pendingRespond.get(id)?.(respond);
|
|
1639
|
+
pendingRespond.delete(id);
|
|
1640
|
+
}
|
|
1641
|
+
async function requestShouldRespond(transcript) {
|
|
1642
|
+
const send = await whenConnected().catch(() => null);
|
|
1643
|
+
if (!send) return false;
|
|
1644
|
+
const id = randomUUID2();
|
|
1645
|
+
return new Promise((resolve4) => {
|
|
1646
|
+
const timer = setTimeout(() => {
|
|
1647
|
+
pendingRespond.delete(id);
|
|
1648
|
+
resolve4(false);
|
|
1649
|
+
}, REQUEST_TIMEOUT_MS);
|
|
1650
|
+
pendingRespond.set(id, (respond) => {
|
|
1651
|
+
clearTimeout(timer);
|
|
1652
|
+
resolve4(respond);
|
|
1653
|
+
});
|
|
1654
|
+
send({ type: "should-respond", id, transcript });
|
|
1655
|
+
});
|
|
1656
|
+
}
|
|
1657
|
+
var BODYLESS_STATUS = /* @__PURE__ */ new Set([101, 204, 205, 304]);
|
|
1658
|
+
function dbFetch(send) {
|
|
1659
|
+
return async (input, init) => {
|
|
1660
|
+
const req = new Request(input, init);
|
|
1661
|
+
const url = new URL(req.url);
|
|
1662
|
+
const method = req.method.toUpperCase();
|
|
1663
|
+
const res = await send({
|
|
1664
|
+
method,
|
|
1665
|
+
path: `${url.pathname}${url.search}`,
|
|
1666
|
+
headers: Object.fromEntries(req.headers.entries()),
|
|
1667
|
+
body: method === "GET" || method === "HEAD" ? void 0 : await req.text()
|
|
1668
|
+
});
|
|
1669
|
+
const noBody = method === "HEAD" || BODYLESS_STATUS.has(res.status);
|
|
1670
|
+
return new Response(noBody ? null : res.body, { status: res.status, headers: res.headers });
|
|
1671
|
+
};
|
|
1672
|
+
}
|
|
1673
|
+
var hostDbFetch = dbFetch(requestHostDb);
|
|
1674
|
+
var LOCAL_DB_PATH = "/internal/db";
|
|
1675
|
+
async function requestViaLocalHost(req) {
|
|
1676
|
+
const deadline = Date.now() + REQUEST_TIMEOUT_MS;
|
|
1677
|
+
let endpoint = readHostEndpoint();
|
|
1678
|
+
while (!endpoint && Date.now() < deadline) {
|
|
1679
|
+
await new Promise((r) => setTimeout(r, 1e3));
|
|
1680
|
+
endpoint = readHostEndpoint();
|
|
1681
|
+
}
|
|
1682
|
+
if (!endpoint) throw new Error("No local Host to reach the Data API through");
|
|
1683
|
+
const res = await fetch(`http://127.0.0.1:${endpoint.port}${LOCAL_DB_PATH}`, {
|
|
1684
|
+
method: "POST",
|
|
1685
|
+
headers: { authorization: `Bearer ${endpoint.token}`, "content-type": "application/json" },
|
|
1686
|
+
body: JSON.stringify(req),
|
|
1687
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS + 5e3)
|
|
1688
|
+
});
|
|
1689
|
+
if (!res.ok) throw new Error(`Local Host DB relay answered ${res.status}`);
|
|
1690
|
+
return await res.json();
|
|
1691
|
+
}
|
|
1692
|
+
function relayChannel(topic) {
|
|
1693
|
+
const channel = {
|
|
1694
|
+
on: () => channel,
|
|
1695
|
+
subscribe: (callback) => {
|
|
1696
|
+
queueMicrotask(() => callback?.("SUBSCRIBED"));
|
|
1697
|
+
return channel;
|
|
1698
|
+
},
|
|
1699
|
+
send: async (msg) => {
|
|
1700
|
+
if (msg.type !== "broadcast" || !sender) return "error";
|
|
1701
|
+
sender({ type: "db-broadcast", topic, event: msg.event, payload: msg.payload ?? {} });
|
|
1702
|
+
return "ok";
|
|
1703
|
+
},
|
|
1704
|
+
unsubscribe: async () => "ok"
|
|
1705
|
+
};
|
|
1706
|
+
return channel;
|
|
1707
|
+
}
|
|
1708
|
+
function createHostDbClient(via = "socket") {
|
|
1709
|
+
const client3 = createClient("https://host-db.invalid", "host-device", {
|
|
1710
|
+
auth: { persistSession: false, autoRefreshToken: false },
|
|
1711
|
+
global: { fetch: via === "socket" ? hostDbFetch : dbFetch(requestViaLocalHost) }
|
|
1712
|
+
});
|
|
1713
|
+
return new Proxy(client3, {
|
|
1714
|
+
get(target, prop) {
|
|
1715
|
+
if (prop === "channel") return relayChannel;
|
|
1716
|
+
if (prop === "removeChannel") return async () => "ok";
|
|
1717
|
+
const value = Reflect.get(target, prop, target);
|
|
1718
|
+
return typeof value === "function" ? value.bind(target) : value;
|
|
1719
|
+
}
|
|
1720
|
+
});
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
// src/commands/supabase-via-host.ts
|
|
1724
|
+
function useHostDbWithoutCredentials() {
|
|
1725
|
+
const viaHost = !!loadCredentials() && (!hasSupabaseCredentials() || !!readHostEndpoint());
|
|
1726
|
+
if (viaHost) setSupabaseClient(createHostDbClient("local-host"));
|
|
1727
|
+
return viaHost;
|
|
1728
|
+
}
|
|
1729
|
+
|
|
1189
1730
|
export {
|
|
1190
1731
|
__require,
|
|
1191
1732
|
__commonJS,
|
|
1192
1733
|
__toESM,
|
|
1193
1734
|
setSupabaseClient,
|
|
1194
1735
|
setSupabaseClientFactory,
|
|
1195
|
-
hasSupabaseCredentials,
|
|
1196
1736
|
databaseReachable,
|
|
1197
1737
|
getSupabaseClient,
|
|
1198
1738
|
DEFAULT_PROFILE,
|
|
@@ -1204,6 +1744,13 @@ export {
|
|
|
1204
1744
|
isEffort,
|
|
1205
1745
|
resolveClaudeEffort,
|
|
1206
1746
|
resolveCodexEffort,
|
|
1747
|
+
DEFAULT_CODEX_MODEL,
|
|
1748
|
+
mcpStrictConfigEnabled,
|
|
1749
|
+
loadEnvFiles,
|
|
1750
|
+
SLACK_ENV,
|
|
1751
|
+
slackConfigured,
|
|
1752
|
+
requireSlackBotToken,
|
|
1753
|
+
loadConfig,
|
|
1207
1754
|
isPermissionMode,
|
|
1208
1755
|
isLogicalCwd,
|
|
1209
1756
|
DEFAULT_AUTO_CONTINUE_LIMIT,
|
|
@@ -1253,5 +1800,39 @@ export {
|
|
|
1253
1800
|
syncPostToDashboard,
|
|
1254
1801
|
resolveDashboardChannelId,
|
|
1255
1802
|
postToDashboardChannel,
|
|
1803
|
+
DEFAULT_API_URL,
|
|
1804
|
+
AuthApiError,
|
|
1805
|
+
apiUrl,
|
|
1806
|
+
deviceKeyPath,
|
|
1807
|
+
credentialsPath,
|
|
1808
|
+
ensureDeviceKey,
|
|
1809
|
+
loadCredentials,
|
|
1810
|
+
saveCredentials,
|
|
1811
|
+
clearLocalAuth,
|
|
1812
|
+
requestDeviceCode,
|
|
1813
|
+
pollDeviceToken,
|
|
1814
|
+
fetchWhoAmI,
|
|
1815
|
+
boundWorkspaces,
|
|
1816
|
+
revokeDevice,
|
|
1817
|
+
getAccessToken,
|
|
1818
|
+
acquireHostLock,
|
|
1819
|
+
publishHostEndpoint,
|
|
1820
|
+
readHostEndpoint,
|
|
1821
|
+
setHostDbSender,
|
|
1822
|
+
sendAgentFailure,
|
|
1823
|
+
handleHostDbResult,
|
|
1824
|
+
requestHostDb,
|
|
1825
|
+
handleAttachmentUrlResult,
|
|
1826
|
+
requestAttachmentUrl,
|
|
1827
|
+
handlePluginSvgResult,
|
|
1828
|
+
requestPluginSvg,
|
|
1829
|
+
handleDocShareResult,
|
|
1830
|
+
requestDocShare,
|
|
1831
|
+
LOCAL_DOC_SHARE_PATH,
|
|
1832
|
+
handleShouldRespondResult,
|
|
1833
|
+
requestShouldRespond,
|
|
1834
|
+
LOCAL_DB_PATH,
|
|
1835
|
+
createHostDbClient,
|
|
1836
|
+
useHostDbWithoutCredentials,
|
|
1256
1837
|
SessionStore
|
|
1257
1838
|
};
|