package ar.com.companeros.runtime; import ar.com.companeros.memory.AgentNotebook; import java.util.*; import java.util.concurrent.*; import net.minecraftforge.fml.loading.FMLPaths; /** Disco fuera del tick de Minecraft, con una sola cola acotada para todos los cuadernos. */ public final class AgentNotebooks { private static final ThreadPoolExecutor IO = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(32), task -> { Thread thread = new Thread(task, "raps-notebooks"); thread.setDaemon(true); return thread; }); private AgentNotebooks() { } private static AgentNotebook store() { return new AgentNotebook(FMLPaths.CONFIGDIR.get().resolve("raps/Notebooks")); } public static CompletableFuture> run(UUID actor, String name, String action, String text, String oldText, String query, int offset, String context) { return run(store(), actor, name, action, text, oldText, query, offset, context); } public static CompletableFuture> runKnowledge(UUID recipient, String recipientName, String author, String action, String text, String oldText, String query, int offset, String context) { AgentNotebook notes = new AgentNotebook(FMLPaths.CONFIGDIR.get().resolve("raps/Knowledge"), "KNOWLEDGE.md"); return run(notes, recipient, recipientName, action, text, oldText, query, offset, "author=" + author + " " + context) .thenApply(rows -> rows.stream().map(row -> row.startsWith("notebook_") ? row.replace("README.md", "KNOWLEDGE.md") : row).toList()); } public static CompletableFuture> runPrivateBase(UUID world, UUID actor, String author, String action, String text, String oldText, String query, int offset, String context) { AgentNotebook notes = new AgentNotebook(FMLPaths.CONFIGDIR.get().resolve("raps/Bases").resolve(world.toString()).resolve("private"), "BASE.md"); return run(notes, actor, author, action, text, oldText, query, offset, "scope=own " + context) .thenApply(rows -> rows.stream().map(row -> row.replace("README.md", "BASE.md")).toList()); } public static UUID factionId(UUID world, String team) { return UUID.nameUUIDFromBytes((world + ":" + team).getBytes(java.nio.charset.StandardCharsets.UTF_8)); } public static CompletableFuture> runFaction(UUID world, String team, String author, String action, String text, String oldText, String query, int offset, String context) { AgentNotebook notes = new AgentNotebook(FMLPaths.CONFIGDIR.get().resolve("raps/Factions").resolve(world.toString()), "BASE.md"); return run(notes, factionId(world, team), "Faction", action, text, oldText, query, offset, "team=" + team + " author=" + author + " " + context).thenApply(rows -> rows.stream() .map(row -> row.replace("README.md", "BASE.md")).toList()); } private static CompletableFuture> run(AgentNotebook notes, UUID actor, String name, String action, String text, String oldText, String query, int offset, String context) { try { return CompletableFuture.supplyAsync(() -> { try { return switch (action) { case "init" -> { notes.ensure(actor, name); yield List.of("notebook_ready README.md"); } case "append" -> { notes.append(actor, name, "### " + java.time.Instant.now() + "\n" + context + "\n" + text); yield List.of("notebook_saved README.md"); } case "replace" -> { notes.replace(actor, name, oldText, text); yield List.of("notebook_edited backup=README.md.bak"); } case "read" -> { var page = notes.read(actor, name, offset); List rows = new ArrayList<>(); rows.add("notebook_page offset=" + page.offset() + " nextOffset=" + page.nextOffset() + " more=" + page.more()); int middle = page.text().length() / 2; if (middle > 0 && Character.isHighSurrogate(page.text().charAt(middle - 1))) middle--; if (middle > 0) rows.add("note=" + page.text().substring(0, middle)); if (middle < page.text().length()) rows.add("note=" + page.text().substring(middle)); yield List.copyOf(rows); } case "search" -> { var search = notes.search(actor, name, query, offset); List rows = new ArrayList<>(); rows.add("notebook_search nextOffset=" + search.nextOffset() + " more=" + search.more() + " matches=" + search.matches().size()); search.matches().forEach(hit -> rows.add("note_match offset=" + hit.offset() + " text=" + hit.text())); yield List.copyOf(rows); } default -> throw new IllegalArgumentException("Acción desconocida"); }; } catch (java.io.IOException | IllegalArgumentException failure) { throw new CompletionException(failure); } }, IO); } catch (RejectedExecutionException busy) { return CompletableFuture.failedFuture(busy); } } }