149 lines
4.0 KiB
JavaScript
149 lines
4.0 KiB
JavaScript
import path from '#host-path'
|
|
import { statSync } from '#host-fs'
|
|
import { fileURLToPath } from 'url'
|
|
import os from 'bare-os'
|
|
|
|
function cwd() {
|
|
if (typeof globalThis.process?.cwd === 'function') {
|
|
try {
|
|
const p = globalThis.process.cwd()
|
|
if (p) return p
|
|
} catch {
|
|
/* Pear/Bare sometimes exposes cwd() but returns "" */
|
|
}
|
|
}
|
|
return os.cwd()
|
|
}
|
|
|
|
function env(name) {
|
|
return globalThis.process?.env?.[name]
|
|
}
|
|
|
|
/** Host-side state root (Corestore, etc.): `BARE_OS_HOST_DATA` or `~/.bare-os`. */
|
|
export function hostDataRoot() {
|
|
const override = env('BARE_OS_HOST_DATA')
|
|
if (override) return path.resolve(override)
|
|
return path.join(os.homedir(), '.bare-os')
|
|
}
|
|
|
|
/** Host cache for QVAC GGUFs (llama.cpp load path). */
|
|
export function bareOsQvacHostModelsDir() {
|
|
return path.join(hostDataRoot(), 'qvac', 'models')
|
|
}
|
|
|
|
/**
|
|
* Host materialize root for ggml backend .so files (prebuilds-equivalent).
|
|
* Layout: `$root/<platform-arch>/qvac__llm-llamacpp/*.so`
|
|
*/
|
|
export function bareOsQvacHostBackendsRoot() {
|
|
return path.join(hostDataRoot(), 'qvac', 'backends')
|
|
}
|
|
|
|
/** Runtime qvac.config.json written before SDK init (absolute cacheDirectory). */
|
|
export function bareOsQvacHostConfigPath() {
|
|
return path.join(hostDataRoot(), 'qvac', 'qvac.config.json')
|
|
}
|
|
|
|
/** Booter has no `kernel/`; use `package.json` to detect the staged app root. */
|
|
function stagedAppRootHeuristic(root) {
|
|
try {
|
|
return statSync(path.join(root, 'package.json')).isFile()
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
|
|
function dirnameOf(p) {
|
|
return path.dirname(path.resolve(String(p)))
|
|
}
|
|
|
|
function pathBasename(p) {
|
|
const s = String(p || '')
|
|
const i = Math.max(s.lastIndexOf('/'), s.lastIndexOf('\\'))
|
|
return i >= 0 ? s.slice(i + 1) : s
|
|
}
|
|
|
|
/**
|
|
* Directories that may contain the standalone booter binary (and sidecars).
|
|
* Exported for QVAC ggml backend discovery beside the binary.
|
|
* @returns {string[]}
|
|
*/
|
|
export function bundledExecCandidates() {
|
|
const out = []
|
|
const push = (c) => {
|
|
if (!c) return
|
|
const p = path.resolve(String(c))
|
|
if (!out.includes(p)) out.push(p)
|
|
}
|
|
try {
|
|
if (typeof os.execPath === 'function') {
|
|
const ep = os.execPath()
|
|
if (ep) push(dirnameOf(ep))
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
try {
|
|
const pep = globalThis.process?.execPath
|
|
if (pep && pathBasename(pep) !== 'bare' && !String(pep).includes('node')) {
|
|
push(dirnameOf(pep))
|
|
}
|
|
} catch {
|
|
// ignore
|
|
}
|
|
const argv0 = globalThis.Bare?.argv?.[0] || globalThis.process?.argv?.[0]
|
|
if (
|
|
argv0 &&
|
|
pathBasename(argv0) !== 'bare' &&
|
|
!String(argv0).includes('node') &&
|
|
!String(argv0).startsWith('bare:')
|
|
) {
|
|
push(dirnameOf(argv0))
|
|
}
|
|
return out
|
|
}
|
|
|
|
export function packageRootDir(metaUrl) {
|
|
const href = String(metaUrl)
|
|
const seen = new Set()
|
|
const candidates = []
|
|
const push = (c) => {
|
|
if (!c) return
|
|
const p = path.resolve(String(c))
|
|
if (!seen.has(p)) {
|
|
seen.add(p)
|
|
candidates.push(p)
|
|
}
|
|
}
|
|
|
|
for (const c of bundledExecCandidates()) push(c)
|
|
|
|
if (href.startsWith('file:')) {
|
|
// Caller passes index.js import.meta.url → dirname is package root
|
|
push(path.dirname(fileURLToPath(href)))
|
|
}
|
|
|
|
const rti = globalThis.Pear?.constructor?.RTI?.mount
|
|
const swap = globalThis.Pear?.config?.swapDir
|
|
const pearAppDir = globalThis.Pear?.app?.dir ?? globalThis.Pear?.config?.dir
|
|
const rtiDir = globalThis.Pear?.constructor?.RTI?.dir
|
|
for (const c of [rti, swap, pearAppDir, rtiDir, cwd()].filter(Boolean)) push(c)
|
|
|
|
for (const root of candidates) {
|
|
if (stagedAppRootHeuristic(root)) return root
|
|
}
|
|
return candidates[0] ?? cwd()
|
|
}
|
|
|
|
export function defaultBootCorestorePath(pkgRoot, metaUrl) {
|
|
const override = env('BARE_OS_BOOT_STORE')
|
|
if (override) return override
|
|
return path.join(hostDataRoot(), 'corestore', 'booter')
|
|
}
|
|
|
|
export function defaultLocalSeedCorestorePath(pkgRoot, metaUrl) {
|
|
const override = env('BARE_OS_LOCAL_SEED')
|
|
if (override) return override
|
|
return path.join(hostDataRoot(), 'corestore', 'seeder')
|
|
}
|