UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

85 lines (84 loc) • 3.34 kB
import { t as asNonArrayRecord } from "./record-coerce-CVPuDqcc.mjs"; import { a as extractErrorCode } from "./error-coercion-hDrHOSGy.mjs"; import { t as formatErrorMessage } from "./errors-BKzCeTYb.mjs"; import { g as readRegularFile, n as appendRegularFile } from "./fs-safe-BbWvmdVn.mjs"; import { r as decodeUtf8Input } from "./utf8-input-BHczGT8v.mjs"; import "./string-coerce-runtime-BwAZZDf5.mjs"; import "./security-runtime-C-zE_Zy_.mjs"; import "./runtime-doctor-migrations-YTlcIEpR.mjs"; import "./error-runtime-BM740gJj.mjs"; import { d as markMigrationItemSkipped, t as MIGRATION_REASON_MISSING_SOURCE_OR_TARGET, u as markMigrationItemError } from "./migration-C73lp9ea.mjs"; import path from "node:path"; import fs from "node:fs/promises"; import os from "node:os"; import { parse as parse$1 } from "dotenv"; import { parse as parse$2 } from "yaml"; //#region extensions/migrate-hermes/helpers.ts const HOME_SHORTHAND_RE = /^~(?=$|[\\/])/u; const UNSAFE_NAME_CHARS_RE = /[^a-z0-9._-]+/g; const EDGE_DASHES_RE = /^-+|-+$/g; function resolveHomePath(e) { const t = e.trim(); return t ? path.resolve(t.replace(HOME_SHORTHAND_RE, () => os.homedir())) : t; } async function isDirectory(e) { return (await fs.stat(e).catch((e) => { const t = extractErrorCode(e); if (t === "ENOENT" || t === "ENOTDIR") return; throw e; }))?.isDirectory() === true; } function sanitizeName(e) { return e.trim().toLowerCase().replaceAll(UNSAFE_NAME_CHARS_RE, "-").replaceAll(EDGE_DASHES_RE, ""); } async function readText(e) { return e ? await fs.realpath(e).then((e) => readRegularFile({ filePath: e })).then(({ buffer: e }) => e.toString("utf8")).catch(() => void 0) : void 0; } async function readUtf8Text(e) { if (!e) return; try { const { buffer: t } = await readRegularFile({ filePath: await fs.realpath(e) }); return decodeUtf8Input(t, "Source file"); } catch (t) { throw new Error(`Failed reading text file ${e}: ${formatErrorMessage(t)}. Use an ordinary readable UTF-8 file and retry migration.`, { cause: t }); } } async function readJsonObject(e) { const t = await readUtf8Text(e); try { return t ? asNonArrayRecord(JSON.parse(t)) : {}; } catch { return {}; } } function parseEnv(e) { return e ? parse$1(e) : {}; } function parseHermesConfig(e) { return e ? asNonArrayRecord(parse$2(e)) : {}; } async function appendItem(e) { if (!e.source || !e.target) return markMigrationItemError(e, MIGRATION_REASON_MISSING_SOURCE_OR_TARGET); try { const t = await readUtf8Text(e.source); const n = `\n\n<!-- Imported from Hermes: ${path.basename(e.source)} -->\n\n`; const r = t?.trimEnd(); if (!r) return markMigrationItemSkipped(e, "source file is empty"); const i = `${n}${r}\n`; if ((await readText(e.target) ?? "").includes(i)) return markMigrationItemSkipped(e, "already imported from Hermes"); await fs.mkdir(path.dirname(e.target), { recursive: true }); await appendRegularFile({ filePath: e.target, content: i, rejectSymlinkParents: true }); return { ...e, status: "migrated" }; } catch (t) { return markMigrationItemError(e, t instanceof Error ? t.message : String(t)); } } //#endregion export { readJsonObject as a, resolveHomePath as c, parseHermesConfig as i, sanitizeName as l, isDirectory as n, readText as o, parseEnv as r, readUtf8Text as s, appendItem as t };