Files
seal-use/src/core/runtime.ts
T

89 lines
2.4 KiB
TypeScript

import { existsSync, symlinkSync } from "node:fs";
import { homedir } from "node:os";
import { basename, dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
export function isCompiledBinary(execPath = process.execPath, metaUrl = import.meta.url): boolean {
if (metaUrl.includes("/$bunfs/") || metaUrl.includes("\\$bunfs\\")) {
return true;
}
const lower = execPath.toLowerCase().replace(/\\/g, "/");
return !lower.endsWith("/bun") && !lower.endsWith("/bun.exe") && !lower.endsWith("/node") && !lower.endsWith("/node.exe");
}
export function resolveAppRoot(entryFile?: string, isCompiled = isCompiledBinary()): string {
if (isCompiled) {
return process.cwd();
}
const file = entryFile || fileURLToPath(import.meta.url);
return dirname(dirname(file));
}
export type LauncherConfig = {
command: string;
args?: string[];
};
export function resolveDefaultLauncher(
entryFile: string,
isCompiled = isCompiledBinary(),
execPath = process.execPath
): LauncherConfig {
if (isCompiled) {
return {
command: execPath,
args: []
};
}
return {
command: execPath,
args: [entryFile]
};
}
export async function performRuntimeSelfHealing(): Promise<{
binaryHealed?: boolean;
skillHealed?: boolean;
}> {
const result: { binaryHealed?: boolean; skillHealed?: boolean } = {};
// 1. Binary executable alias self-healing
try {
if (isCompiledBinary()) {
const execPath = process.execPath;
const base = basename(execPath);
const isWin = process.platform === "win32";
const legacyName = isWin ? "seal-home.exe" : "seal-home";
const primaryName = isWin ? "seal-use.exe" : "seal-use";
if (base === legacyName) {
const primaryPath = join(dirname(execPath), primaryName);
if (!existsSync(primaryPath)) {
try {
symlinkSync(execPath, primaryPath);
result.binaryHealed = true;
} catch {
// ignore if permissions do not allow
}
}
}
}
} catch {
// ignore
}
// 2. Legacy skill cutover self-healing
try {
const legacySkillDir = join(homedir(), ".agents", "skills", "seal-home");
if (existsSync(legacySkillDir)) {
// Ordinary discovery must not initialize the embedded resource graph.
(await import("./embedded-skill.js")).syncGlobalSkillIfPresent();
result.skillHealed = true;
}
} catch {
// ignore
}
return result;
}