import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);
import {
  AgentRegistry
} from "./chunk-RNWALAFP.js";

// src/native-deps.ts
import { execFileSync } from "child_process";
import { createRequire } from "module";
import path from "path";
import fs from "fs";
var NATIVE_PACKAGES = ["better-sqlite3", "sqlite-vec"];
function findPluginRoot() {
  const fromRegistry = new AgentRegistry().resolvePluginRoot();
  if (fromRegistry) return fromRegistry;
  let dir = path.dirname(new URL(import.meta.url).pathname);
  for (let i = 0; i < 5; i++) {
    if (fs.existsSync(path.join(dir, "package.json"))) return dir;
    dir = path.dirname(dir);
  }
  return process.cwd();
}
function ensureNativeDeps() {
  const pluginRoot = findPluginRoot();
  const require2 = createRequire(path.join(pluginRoot, "node_modules", ".package.json"));
  const missing = [];
  for (const pkg of NATIVE_PACKAGES) {
    try {
      require2.resolve(pkg);
    } catch {
      missing.push(pkg);
    }
  }
  if (missing.length === 0) return;
  const nodeModulesDir = path.join(pluginRoot, "node_modules");
  if (!fs.existsSync(nodeModulesDir)) {
    fs.mkdirSync(nodeModulesDir, { recursive: true });
  }
  try {
    execFileSync("npm", ["install", "--no-save", "--no-package-lock", ...missing], {
      cwd: pluginRoot,
      stdio: "pipe",
      timeout: 12e4
    });
  } catch (error) {
    const msg = error.message;
    process.stderr.write(`[myco] Failed to install native dependencies: ${msg}
`);
    process.stderr.write(`[myco] You can install them manually: cd ${pluginRoot} && npm install ${missing.join(" ")}
`);
  }
}

export {
  ensureNativeDeps
};
//# sourceMappingURL=chunk-RXJHB7W4.js.map