import {
  Orchestrator
} from "./chunk-IM3JSE73.js";
import "./chunk-LVYGDT5V.js";
import "./chunk-CK5NTM2S.js";
import "./chunk-FSFEQI74.js";
import "./chunk-Z5AAYHUC.js";
import "./chunk-S75M5ZRK.js";
import "./chunk-TPB3I2AC.js";
import "./chunk-UHGBNIOS.js";
import "./chunk-B2TL6GA2.js";
import "./chunk-ETOW6ACV.js";
import "./chunk-ZTSE2ZJ6.js";
import "./chunk-MDYG7VI7.js";
import "./chunk-JR4ZC3G4.js";
import "./chunk-5IZL4DCV.js";
import "./chunk-X7XN6YU4.js";
import "./chunk-5NPGSAVB.js";
import "./chunk-C7VW7C3F.js";
import "./chunk-K4FLSOR5.js";
import "./chunk-V3RXWQIE.js";
import "./chunk-7DHTMOND.js";
import "./chunk-WPGJYVUH.js";
import "./chunk-ZZTOURJI.js";
import "./chunk-YDBIWGNI.js";
import "./chunk-YCN4BVDK.js";
import "./chunk-D654IBA6.js";
import "./chunk-3GPTTA4J.js";
import "./chunk-IISBCCWR.js";
import "./chunk-ASIQZXYO.js";
import "./chunk-H63EDPFJ.js";
import "./chunk-YAZNBMNF.js";
import "./chunk-GGD5W7TB.js";
import "./chunk-DF3RVK3X.js";
import "./chunk-C4SQJZAF.js";
import "./chunk-N7X62G74.js";
import "./chunk-WW3QQF4H.js";
import "./chunk-54V4BZWP.js";
import "./chunk-NBNN5GOB.js";
import "./chunk-FEMOX5AD.js";
import "./chunk-R2XRID2N.js";
import "./chunk-GZCUW5IC.js";
import "./chunk-VEWZZM3H.js";
import "./chunk-6AUUAZEX.js";
import "./chunk-Y4FHOFJ2.js";
import "./chunk-Z2E7VW55.js";
import "./chunk-Y3WQ4ZWK.js";
import "./chunk-2NMMFZ5T.js";
import "./chunk-VTJVUHRK.js";
import "./chunk-PYXS46O7.js";
import "./chunk-3QKK7QOS.js";
import "./chunk-DHRQHX36.js";
import "./chunk-JRNQ3RNA.js";
import "./chunk-L5IIGA5V.js";
import "./chunk-CULXMQJH.js";
import "./chunk-E6K4NIEU.js";
import "./chunk-EABGC2TL.js";
import "./chunk-LOBRX7VD.js";
import "./chunk-R2L7SUX2.js";
import "./chunk-YRMVARQP.js";
import "./chunk-WSZIHQBK.js";
import {
  resolveRemnicPluginEntry
} from "./chunk-U66YHYC7.js";
import "./chunk-L2IO2QPY.js";
import "./chunk-6TBWYBJ3.js";
import "./chunk-HMDCOMYU.js";
import "./chunk-JXS5PDQ7.js";
import "./chunk-K6WK37A6.js";
import "./chunk-AJU4PJGY.js";
import "./chunk-7SEAZFFB.js";
import {
  parseConfig
} from "./chunk-JWSENLQI.js";
import "./chunk-Z5LAYHGJ.js";
import "./chunk-OZHRDTDX.js";
import "./chunk-C6QPK5GG.js";
import "./chunk-3PG3H5TD.js";
import "./chunk-FYIYMQ5N.js";
import "./chunk-KVE7R4CG.js";
import "./chunk-4WMCPJWX.js";
import "./chunk-JBMSGZEQ.js";
import "./chunk-55FXRRSJ.js";
import "./chunk-EJI5XIBB.js";
import "./chunk-CRU27Q4J.js";
import "./chunk-Q7FJ5ZHM.js";
import "./chunk-ODWDQNRE.js";
import "./chunk-Y27UJK6V.js";
import "./chunk-UZB5KHKX.js";
import "./chunk-6HZ6AO2P.js";
import "./chunk-JWPLJLDU.js";
import "./chunk-N53K2EXC.js";
import "./chunk-XYIK4LF6.js";
import "./chunk-URB2WSKZ.js";
import "./chunk-PVGDJXVK.js";
import "./chunk-NGAVDO7E.js";
import {
  EngramAccessService
} from "./chunk-DR7MCMPS.js";
import "./chunk-3KIS4VGT.js";
import "./chunk-BT7NVCML.js";
import "./chunk-H7XKCNR6.js";
import "./chunk-LW2NMHDW.js";
import "./chunk-SOBJ6NEY.js";
import "./chunk-NN3TS5BM.js";
import "./chunk-EQINRHYR.js";
import "./chunk-USFPPRAF.js";
import "./chunk-LZRYQK6L.js";
import "./chunk-EIR5VLIH.js";
import "./chunk-IXEJRKCZ.js";
import "./chunk-PCUKNJAZ.js";
import "./chunk-3FPTCC3Z.js";
import "./chunk-2LGMW3DJ.js";
import "./chunk-EEQLFRUM.js";
import "./chunk-OR64ZGRZ.js";
import "./chunk-QDW3E4RD.js";
import "./chunk-GDFS42HT.js";
import "./chunk-RK2Y4XOM.js";
import "./chunk-2LSZVONP.js";
import "./chunk-WIICJPET.js";
import "./chunk-G2WADRQ3.js";
import "./chunk-74EMIVE4.js";
import "./chunk-DT5TVLJE.js";
import "./chunk-N5AKDXAI.js";
import "./chunk-TBBDFYXW.js";
import "./chunk-SS253RXF.js";
import "./chunk-DCE6SQLA.js";
import "./chunk-YNJHCGDT.js";
import "./chunk-4KAN3GZ3.js";
import "./chunk-43EKP2UK.js";
import "./chunk-PFV5C235.js";
import "./chunk-RULE4VG5.js";
import "./chunk-SCU65EZI.js";
import "./chunk-BOUYNNYD.js";
import "./chunk-DM2T26WE.js";
import "./chunk-QSVPYQPG.js";
import "./chunk-M62O4P4T.js";
import "./chunk-4DJQYKMN.js";
import "./chunk-G7D6GZ5J.js";
import "./chunk-FAAFWE4G.js";
import {
  readEnvVar,
  resolveHomeDir
} from "./chunk-MARWOCVP.js";
import "./chunk-2ODBA7MQ.js";
import "./chunk-DGXUHMOV.js";
import "./chunk-LPSF4OQH.js";
import "./chunk-TVVEYCNW.js";
import "./chunk-RFYAYKTD.js";
import "./chunk-LBLXEFWK.js";
import "./chunk-XKECPATV.js";
import "./chunk-IYY4MCPG.js";
import "./chunk-457A4P3L.js";
import "./chunk-OA3L7BFR.js";
import "./chunk-KNKUID7G.js";
import "./chunk-BJMBJZ2Y.js";
import "./chunk-UKJAGEXH.js";
import "./chunk-FP2373TW.js";
import "./chunk-A6XUJE5D.js";
import "./chunk-PZ5AY32C.js";

// src/access-cli.ts
import fs from "fs";
import path from "path";
var UsageError = class extends Error {
  constructor(kind, optionName) {
    super("invalid access-cli arguments");
    this.kind = kind;
    this.optionName = optionName;
  }
  kind;
  optionName;
};
function formatUsageError(error) {
  switch (error.kind) {
    case "unsupported-command":
      return "unsupported command";
    case "unexpected-positional":
      return "unexpected positional argument";
    case "missing-option":
      return `missing required option: --${error.optionName ?? "unknown"}`;
    case "missing-content":
      return "missing required option: --content or --content-file";
    case "invalid-integer":
      return `invalid integer for --${error.optionName ?? "unknown"}`;
    case "invalid-number":
      return `invalid number for --${error.optionName ?? "unknown"}`;
  }
}
function writeCliOutput(text = "") {
  process.stdout.write(`${text}
`);
}
function usage() {
  return [
    "Usage:",
    "  engram-access browse [options]",
    "  engram-access store [options]",
    "",
    "Browse options:",
    "  --namespace <name>",
    "  --query <text>",
    "  --category <name>",
    "  --status <name>",
    "  --sort <updated_desc|updated_asc|created_desc|created_asc>",
    "  --limit <n>",
    "  --offset <n>",
    "",
    "Store options:",
    "  --namespace <name>",
    "  --session-key <key>",
    "  --principal <principal>",
    "  --content <text> | --content-file <path>",
    "  --category <name>",
    "  --confidence <0-1>",
    "  --tag <tag> (repeatable)",
    "  --entity-ref <ref>",
    "  --ttl <duration>",
    "  --source-reason <text>",
    "  --idempotency-key <key>",
    "  --dry-run"
  ].join("\n");
}
function parseArgs(argv) {
  const [commandRaw, ...rest] = argv;
  if (commandRaw !== "browse" && commandRaw !== "store") {
    throw new UsageError("unsupported-command");
  }
  const options = {};
  const flags = /* @__PURE__ */ new Set();
  for (let i = 0; i < rest.length; i += 1) {
    const token = rest[i];
    if (!token.startsWith("--")) {
      throw new UsageError("unexpected-positional");
    }
    const key = token.slice(2);
    const next = rest[i + 1];
    if (!next || next.startsWith("--")) {
      flags.add(key);
      continue;
    }
    if (!options[key]) {
      options[key] = [];
    }
    options[key].push(next);
    i += 1;
  }
  return {
    command: commandRaw,
    options,
    flags
  };
}
function getLastOption(args, name) {
  const values = args.options[name];
  if (!values || values.length === 0) return void 0;
  return values[values.length - 1];
}
function getAllOptions(args, name) {
  return args.options[name] ?? [];
}
function requireOption(args, name) {
  const value = getLastOption(args, name);
  if (!value || value.trim().length === 0) {
    throw new UsageError("missing-option", name);
  }
  return value;
}
function parseIntegerOption(args, name) {
  const raw = getLastOption(args, name);
  if (!raw) return void 0;
  const value = parseInt(raw, 10);
  if (!Number.isFinite(value)) {
    throw new UsageError("invalid-integer", name);
  }
  return value;
}
function parseFloatOption(args, name) {
  const raw = getLastOption(args, name);
  if (!raw) return void 0;
  const value = Number.parseFloat(raw);
  if (!Number.isFinite(value)) {
    throw new UsageError("invalid-number", name);
  }
  return value;
}
function loadPluginConfig(preferredId) {
  const configPath = readEnvVar("OPENCLAW_ENGRAM_CONFIG_PATH") || readEnvVar("OPENCLAW_CONFIG_PATH") || path.join(resolveHomeDir(), ".openclaw", "openclaw.json");
  const raw = JSON.parse(fs.readFileSync(configPath, "utf8"));
  const entry = resolveRemnicPluginEntry(raw, preferredId);
  return entry?.["config"] ?? {};
}
function buildRuntime(preferredId) {
  const config = parseConfig(loadPluginConfig(preferredId));
  return {
    config,
    service: new EngramAccessService(new Orchestrator(config))
  };
}
async function runBrowse(args, preferredId) {
  const { service } = buildRuntime(preferredId);
  const result = await service.memoryBrowse({
    namespace: getLastOption(args, "namespace"),
    query: getLastOption(args, "query"),
    category: getLastOption(args, "category"),
    status: getLastOption(args, "status"),
    sort: getLastOption(args, "sort"),
    limit: parseIntegerOption(args, "limit"),
    offset: parseIntegerOption(args, "offset")
  });
  console.log(JSON.stringify(result, null, 2));
}
async function runStore(args, preferredId) {
  const { config, service } = buildRuntime(preferredId);
  const contentFile = getLastOption(args, "content-file");
  const inlineContent = getLastOption(args, "content");
  const content = contentFile ? fs.readFileSync(contentFile, "utf8") : inlineContent;
  if (!content || content.trim().length === 0) {
    throw new UsageError("missing-content");
  }
  const result = await service.memoryStore({
    namespace: getLastOption(args, "namespace"),
    sessionKey: getLastOption(args, "session-key"),
    authenticatedPrincipal: getLastOption(args, "principal") ?? config.agentAccessHttp.principal,
    content,
    category: requireOption(args, "category"),
    confidence: parseFloatOption(args, "confidence"),
    tags: getAllOptions(args, "tag"),
    entityRef: getLastOption(args, "entity-ref"),
    ttl: getLastOption(args, "ttl"),
    sourceReason: getLastOption(args, "source-reason"),
    idempotencyKey: getLastOption(args, "idempotency-key"),
    dryRun: args.flags.has("dry-run")
  });
  console.log(JSON.stringify(result, null, 2));
}
async function main(argv = process.argv.slice(2), options = {}) {
  const args = parseArgs(argv);
  if (args.command === "browse") {
    await runBrowse(args, options.preferredId);
    return;
  }
  await runStore(args, options.preferredId);
}
function printUsage() {
  writeCliOutput(usage());
}
async function runCli(argv = process.argv.slice(2), options = {}) {
  try {
    await main(argv, options);
  } catch (error) {
    if (error instanceof UsageError) {
      writeCliOutput(formatUsageError(error));
      writeCliOutput();
      printUsage();
      process.exit(1);
    }
    console.error("access-cli failed");
    process.exit(1);
  }
}
export {
  main,
  printUsage,
  runCli
};
//# sourceMappingURL=access-cli.js.map