mcp: сервер memo_search/memo_status/memo_reindex

This commit is contained in:
2026-10-02 01:44:45 +03:00
parent f1d497d336
commit f2956e3cef
6 changed files with 586 additions and 75 deletions
+9 -32
View File
@@ -7,6 +7,8 @@ import memo.core.Indexer
import memo.core.RefreshHook
import memo.core.SearchMode
import memo.core.Searcher
import memo.core.findCollections
import memo.core.resolveCollection
import java.io.File
sealed interface Cmd
@@ -125,9 +127,8 @@ private fun modelPaths(): Pair<String, String> {
}
private fun runIndex(cmd: IndexCmd) {
val raw = File(cmd.path)
val base = if (raw.name == ".memo") raw.parentFile ?: raw else raw
val collections = discoverCollections(base)
val base = resolveCollection(File(cmd.path))
val collections = findCollections(base)
if (collections.isEmpty()) {
System.err.println("коллекции не найдены в ${base.absolutePath}")
return
@@ -159,30 +160,6 @@ private fun runIndex(cmd: IndexCmd) {
println("итого: $totalUpdated обновлено в ${collections.size} коллекциях")
}
private fun discoverCollections(base: File): List<File> {
if (!base.isDirectory) return emptyList()
val candidates = LinkedHashSet<File>()
candidates.add(base)
val q = ArrayDeque<Pair<File, Int>>()
q.addLast(base to 0)
while (q.isNotEmpty()) {
val (d, depth) = q.removeFirst()
if (depth >= 2) continue
val children = d.listFiles() ?: continue
for (c in children) {
if (c.isDirectory && !c.name.startsWith(".")) {
candidates.add(c)
q.addLast(c to depth + 1)
}
}
}
return candidates.filter { d ->
d.walkTopDown()
.maxDepth(8)
.any { it.isFile && it.extension == "md" }
}
}
private fun runSearch(cmd: SearchCmd) {
val targets = resolveSearchTargets(File(cmd.path))
if (targets.isEmpty()) {
@@ -250,11 +227,11 @@ private fun resolveSearchTargets(path: File): List<File> {
}
private fun runStatus(cmd: StatusCmd) {
val base = File(cmd.path)
val base = resolveCollection(File(cmd.path))
val collections = if (File(base, ".memo/index.db").exists()) {
listOf(base)
} else {
discoverCollections(base)
findCollections(base)
}
if (collections.isEmpty()) {
println("коллекции не найдены")
@@ -383,11 +360,11 @@ private fun performSearch(path: String, query: String, k: Int, mode: SearchMode)
}
private fun formatStatusInner(path: String): String {
val base = File(path)
val base = resolveCollection(File(path))
val collections = if (File(base, ".memo/index.db").exists()) {
listOf(base)
} else {
discoverCollections(base)
findCollections(base)
}
if (collections.isEmpty()) return "коллекции не найдены"
val sb = StringBuilder()
@@ -572,4 +549,4 @@ private fun unescapeJson(s: String): String {
}
}
return sb.toString()
}
}