@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
49 lines • 1.89 kB
JavaScript
import { GrepEngineError } from "./engine.js";
let singleton;
export function resolveGrepEngine(options = {}) {
const useSingleton = Object.keys(options).length === 0;
if (useSingleton && singleton)
return singleton;
const promise = selectEngine(options);
if (useSingleton)
singleton = promise;
return promise;
}
async function selectEngine(options) {
const env = options.env ?? process.env;
const requested = env.SENPI_GREP_ENGINE ?? "auto";
if (requested !== "auto" && requested !== "rg" && requested !== "native") {
throw new GrepEngineError("ENGINE_UNAVAILABLE", `Unknown SENPI_GREP_ENGINE value: ${requested}`);
}
if (requested === "rg")
return loadRgEngine();
if (requested === "native")
return loadNativeEngine(options, env);
try {
return await loadNativeEngine(options, env);
}
catch (error) {
// ABI mismatch is fatal, not a missing optional addon.
if (!(error instanceof GrepEngineError) || error.code !== "ENGINE_UNAVAILABLE")
throw error;
console.warn(`[grep] native engine unavailable; falling back to rg: ${error.message}`);
return loadRgEngine();
}
}
async function loadRgEngine() {
const module = await import("./rg-engine.js");
return module.createRgEngine();
}
async function loadNativeEngine(options, env) {
const { loadNativeGrep } = await import("./native-loader.js");
const loaded = loadNativeGrep({ ...options, env });
if (!loaded.native) {
throw new GrepEngineError("ENGINE_UNAVAILABLE", `${loaded.diagnostic.message} ${loaded.diagnostic.cause}`);
}
const { createNativeEngine } = await import("./native-engine.js");
return createNativeEngine(loaded.native);
}
export function resetGrepEngineForTests() {
singleton = undefined;
}
//# sourceMappingURL=select-engine.js.map