import { createRequire as __cr } from 'node:module'; const require = __cr(import.meta.url);
import {
  VaultWriter,
  indexNote,
  supersedeSpore
} from "./chunk-ALBVNGCF.js";
import {
  generateEmbedding
} from "./chunk-RGVBGTD6.js";
import {
  stripFrontmatter
} from "./chunk-RY76WEN3.js";
import {
  DIGEST_TIERS,
  EMBEDDING_INPUT_LIMIT
} from "./chunk-WBLTISAK.js";

// src/mcp/tools/context.ts
import fs from "fs";
import path from "path";
var DEFAULT_CONTEXT_TIER = 3e3;
function tryReadExtract(filePath, tier, fallback) {
  let raw;
  try {
    raw = fs.readFileSync(filePath, "utf-8");
  } catch {
    return null;
  }
  const { body, frontmatter } = stripFrontmatter(raw);
  const generated = frontmatter.generated;
  return {
    content: body,
    tier,
    fallback,
    generated
  };
}
function handleMycoContext(vaultDir, input) {
  const requestedTier = input.tier ?? DEFAULT_CONTEXT_TIER;
  const digestDir = path.join(vaultDir, "digest");
  const exact = tryReadExtract(path.join(digestDir, `extract-${requestedTier}.md`), requestedTier, false);
  if (exact) return exact;
  const candidates = [...DIGEST_TIERS].sort((a, b) => Math.abs(a - requestedTier) - Math.abs(b - requestedTier));
  for (const tier of candidates) {
    const result = tryReadExtract(path.join(digestDir, `extract-${tier}.md`), tier, true);
    if (result) return result;
  }
  return {
    content: "Digest context is not yet available. The first digest cycle has not completed.",
    tier: requestedTier,
    fallback: false
  };
}

// src/vault/consolidation.ts
import { randomBytes } from "crypto";
async function consolidateSpores(input, deps) {
  const { vaultDir, index, vectorIndex, embeddingProvider } = deps;
  const writer = new VaultWriter(vaultDir);
  const wisdomId = `${input.observationType}-wisdom-${randomBytes(4).toString("hex")}`;
  const sourceLinks = input.sourceSporeIds.map((id) => `- [[${id}]]`).join("\n");
  const sourcesSection = input.sourceSporeIds.length > 0 ? `

## Sources

Consolidated from:
${sourceLinks}` : "";
  const fullContent = `${input.consolidatedContent}${sourcesSection}`;
  const wisdomPath = writer.writeSpore({
    id: wisdomId,
    observation_type: input.observationType,
    tags: [...input.tags ?? [], "wisdom", "consolidated"],
    content: fullContent
  });
  writer.updateNoteFrontmatter(wisdomPath, {
    consolidated_from: input.sourceSporeIds
  }, true);
  const sourceNotes = index.queryByIds(input.sourceSporeIds);
  const sourceNoteMap = new Map(sourceNotes.map((n) => [n.id, n]));
  let sourcesArchived = 0;
  for (const sourceId of input.sourceSporeIds) {
    const note = sourceNoteMap.get(sourceId);
    if (!note) continue;
    const superseded = supersedeSpore(sourceId, wisdomId, note.path, {
      index,
      vectorIndex,
      vaultDir
    });
    if (superseded) sourcesArchived++;
  }
  indexNote(index, vaultDir, wisdomPath);
  if (vectorIndex && embeddingProvider) {
    generateEmbedding(embeddingProvider, fullContent.slice(0, EMBEDDING_INPUT_LIMIT)).then(
      (emb) => vectorIndex.upsert(wisdomId, emb.embedding, {
        type: "spore",
        observation_type: input.observationType,
        importance: "high"
      })
    ).catch(() => {
    });
  }
  return {
    wisdom_id: wisdomId,
    wisdom_path: wisdomPath,
    sources_archived: sourcesArchived
  };
}

export {
  consolidateSpores,
  handleMycoContext
};
//# sourceMappingURL=chunk-LDKXXKF6.js.map