@phren/cli
Version:
Knowledge layer for AI agents - CLI, MCP server, and data layer
86 lines (85 loc) • 2.98 kB
JavaScript
import { execFileSync } from "child_process";
import { runLink } from "../link/link.js";
import { runPhrenUpdate } from "../update.js";
import { resolveEntryScript } from "../init/shared.js";
import { EXEC_TIMEOUT_MS, } from "../shared.js";
export function resultMsg(r) {
if (!r.ok)
return r.error;
if (typeof r.data === "string")
return r.data;
if (r.data && typeof r.data === "object" && "message" in r.data && typeof r.data.message === "string") {
return r.data.message;
}
return JSON.stringify(r.data);
}
export function editDistance(a, b) {
const m = a.length;
const n = b.length;
const dp = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0));
for (let i = 0; i <= m; i++)
dp[i][0] = i;
for (let j = 0; j <= n; j++)
dp[0][j] = j;
for (let i = 1; i <= m; i++) {
for (let j = 1; j <= n; j++) {
dp[i][j] = a[i - 1] === b[j - 1]
? dp[i - 1][j - 1]
: 1 + Math.min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]);
}
}
return dp[m][n];
}
/** Splits a typed palette command into words on whitespace, honoring single and double quotes. */
export function splitCommandLine(input) {
const out = [];
let current = "";
let quote = null;
for (let i = 0; i < input.length; i++) {
const ch = input[i];
if ((ch === '"' || ch === "'") && (!quote || quote === ch)) {
quote = quote ? null : ch;
continue;
}
if (!quote && /\s/.test(ch)) {
if (current) {
out.push(current);
current = "";
}
continue;
}
current += ch;
}
if (current)
out.push(current);
return out;
}
export function queueByFilter(items, filter) {
const needle = filter.toLowerCase().trim();
if (!needle)
return items;
return items.filter((item) => `${item.id} ${item.section} ${item.text}`.toLowerCase().includes(needle));
}
// ── Infrastructure ───────────────────────────────────────────────────────────
export async function defaultRunHooks(phrenPath) {
const entry = resolveEntryScript();
execFileSync(process.execPath, [entry, "hook-session-start"], {
cwd: phrenPath,
stdio: "ignore",
timeout: EXEC_TIMEOUT_MS,
});
execFileSync(process.execPath, [entry, "hook-stop"], {
cwd: phrenPath,
stdio: "ignore",
timeout: EXEC_TIMEOUT_MS,
});
return "Lifecycle hooks rerun (session-start + stop).";
}
export async function defaultRunUpdate() {
const result = await runPhrenUpdate();
return result.message;
}
export async function defaultRunRelink(phrenPath) {
await runLink(phrenPath, { register: false, allTools: true });
return "Relink completed for detected tools.";
}