import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);
import {
  parseStringFlag
} from "./chunk-SAKJMNSR.js";
import {
  MycoConfigSchema,
  require_dist
} from "./chunk-JSK7L46L.js";
import {
  __toESM
} from "./chunk-PZUWP5VK.js";

// src/cli/setup-digest.ts
var import_yaml = __toESM(require_dist(), 1);
import fs from "fs";
import path from "path";
var CONFIG_FILENAME = "myco.yaml";
var DAEMON_STATE_FILENAME = "daemon.json";
var USAGE = `Usage: myco setup-digest [options]

Configure digest (continuous reasoning) settings.

Options:
  --enabled <true|false>        Enable/disable digest (default: true)
  --tiers <1500,3000,...>       Comma-separated tier list
  --inject-tier <number|null>   Tier to auto-inject at session start
  --provider <name>             LLM provider for digest (null = inherit)
  --model <name>                Model for digest (null = inherit)
  --base-url <url>              Provider base URL (null = inherit)
  --context-window <number>     Context window for digest operations
  --keep-alive <duration>       Keep model loaded (Ollama, e.g. "30m")
  --gpu-kv-cache <true|false>   Offload KV cache to GPU (LM Studio)
  --active-interval <seconds>   Metabolism active interval
  --dormancy-threshold <seconds> Time before dormancy
  --max-notes <number>          Max substrate notes per cycle
  --extraction-tokens <number>  Max tokens for spore extraction
  --summary-tokens <number>     Max tokens for session summaries
  --title-tokens <number>       Max tokens for session titles
  --classification-tokens <number> Max tokens for artifact classification
  --show                        Show current settings and exit
`;
async function run(args, vaultDir) {
  const configPath = path.join(vaultDir, CONFIG_FILENAME);
  const raw = fs.readFileSync(configPath, "utf-8");
  const doc = import_yaml.default.parse(raw);
  if (args.includes("--show")) {
    const config = MycoConfigSchema.parse(doc);
    console.log(JSON.stringify({
      digest: config.digest,
      capture: {
        extraction_max_tokens: config.capture.extraction_max_tokens,
        summary_max_tokens: config.capture.summary_max_tokens,
        title_max_tokens: config.capture.title_max_tokens,
        classification_max_tokens: config.capture.classification_max_tokens
      }
    }, null, 2));
    return;
  }
  if (args.length === 0) {
    console.log(USAGE);
    return;
  }
  if (!doc.digest || typeof doc.digest !== "object") {
    doc.digest = {};
  }
  const digest = doc.digest;
  if (!digest.intelligence || typeof digest.intelligence !== "object") {
    digest.intelligence = {};
  }
  if (!digest.metabolism || typeof digest.metabolism !== "object") {
    digest.metabolism = {};
  }
  if (!digest.substrate || typeof digest.substrate !== "object") {
    digest.substrate = {};
  }
  if (!doc.capture || typeof doc.capture !== "object") {
    doc.capture = {};
  }
  const intelligence = digest.intelligence;
  const metabolism = digest.metabolism;
  const substrate = digest.substrate;
  const capture = doc.capture;
  const enabled = parseStringFlag(args, "--enabled");
  if (enabled !== void 0) digest.enabled = enabled === "true";
  const tiers = parseStringFlag(args, "--tiers");
  if (tiers !== void 0) {
    digest.tiers = tiers.split(",").map((t) => parseInt(t.trim(), 10));
  }
  const injectTier = parseStringFlag(args, "--inject-tier");
  if (injectTier !== void 0) {
    digest.inject_tier = injectTier === "null" ? null : parseInt(injectTier, 10);
  }
  const provider = parseStringFlag(args, "--provider");
  if (provider !== void 0) intelligence.provider = provider === "null" ? null : provider;
  const model = parseStringFlag(args, "--model");
  if (model !== void 0) intelligence.model = model === "null" ? null : model;
  const baseUrl = parseStringFlag(args, "--base-url");
  if (baseUrl !== void 0) intelligence.base_url = baseUrl === "null" ? null : baseUrl;
  const contextWindow = parseStringFlag(args, "--context-window");
  if (contextWindow !== void 0) intelligence.context_window = parseInt(contextWindow, 10);
  const keepAlive = parseStringFlag(args, "--keep-alive");
  if (keepAlive !== void 0) intelligence.keep_alive = keepAlive === "null" ? null : keepAlive;
  const gpuKvCache = parseStringFlag(args, "--gpu-kv-cache");
  if (gpuKvCache !== void 0) intelligence.gpu_kv_cache = gpuKvCache === "true";
  const activeInterval = parseStringFlag(args, "--active-interval");
  if (activeInterval !== void 0) metabolism.active_interval = parseInt(activeInterval, 10);
  const dormancyThreshold = parseStringFlag(args, "--dormancy-threshold");
  if (dormancyThreshold !== void 0) metabolism.dormancy_threshold = parseInt(dormancyThreshold, 10);
  const maxNotes = parseStringFlag(args, "--max-notes");
  if (maxNotes !== void 0) substrate.max_notes_per_cycle = parseInt(maxNotes, 10);
  const extractionTokens = parseStringFlag(args, "--extraction-tokens");
  if (extractionTokens !== void 0) capture.extraction_max_tokens = parseInt(extractionTokens, 10);
  const summaryTokens = parseStringFlag(args, "--summary-tokens");
  if (summaryTokens !== void 0) capture.summary_max_tokens = parseInt(summaryTokens, 10);
  const titleTokens = parseStringFlag(args, "--title-tokens");
  if (titleTokens !== void 0) capture.title_max_tokens = parseInt(titleTokens, 10);
  const classificationTokens = parseStringFlag(args, "--classification-tokens");
  if (classificationTokens !== void 0) capture.classification_max_tokens = parseInt(classificationTokens, 10);
  const result = MycoConfigSchema.safeParse(doc);
  if (!result.success) {
    console.error("Validation error:");
    for (const issue of result.error.issues) {
      console.error(`  ${issue.path.join(".")}: ${issue.message}`);
    }
    process.exit(1);
  }
  fs.writeFileSync(configPath, import_yaml.default.stringify(doc), "utf-8");
  console.log("Digest configuration updated.");
  const updated = MycoConfigSchema.parse(doc);
  console.log(JSON.stringify({
    digest: updated.digest,
    capture: {
      extraction_max_tokens: updated.capture.extraction_max_tokens,
      summary_max_tokens: updated.capture.summary_max_tokens,
      title_max_tokens: updated.capture.title_max_tokens,
      classification_max_tokens: updated.capture.classification_max_tokens
    }
  }, null, 2));
  if (fs.existsSync(path.join(vaultDir, DAEMON_STATE_FILENAME))) {
    console.log("\nNote: restart the daemon for changes to take effect (myco restart)");
  }
}

export {
  run
};
//# sourceMappingURL=chunk-XNAM6Z4O.js.map