package net.intrepid.aiengineering.discussion; import net.intrepid.aiengineering.filesystem.*; import net.intrepid.aiengineering.work.*; import java.nio.file.*; import java.time.Instant; import java.util.*; public final class TopicService { private final WorkService works = new WorkService(); public record View(String id, String title, String description, List sources, boolean implicit) { public View { sources = List.copyOf(sources); } } private Path path(Path root, String work, String id) { Work.identifier(id, "TOP"); return Filespace.resolve(works.folder(root, work), "topics/" + id + ".md"); } private Topic read(Path path) { return Documents.readMarkdownDocument(path, Topic.class, "description"); } private void identity(Topic t, String work, String id) { if (!t.workId().equals(work) || !t.id().equals(id)) throw ProtocolException.validation("Topic identity mismatch: " + id); } public List list(Path root, String work) { var w = works.load(root, work); return w.topics().stream().map(id -> { var t = read(path(root, work, id)); identity(t, work, id); chain(root, t); if (!w.discussions().containsAll(t.sources())) throw ProtocolException.validation("Missing topic sources: " + id); return t; }).toList(); } public List views(Path root, String work) { var views = new LinkedHashMap(); for (var t : list(root, work)) views.put(t.id(), new View(t.id(), t.title(), t.description(), t.sources(), false)); var threads = new LinkedHashMap>(); for (var e : new DiscussionService().list(root, work)) threads.computeIfAbsent(e.discussionId(), ignored -> new ArrayList<>()).add(e.id()); threads.forEach((id, sources) -> { if (id.startsWith("TOP-") && !views.containsKey(id)) throw ProtocolException.validation("Missing formal topic: " + id); if (!views.containsKey(id)) views.put(id, new View(id, id.equals("main") ? "Discussion" : id, "Legacy discussion thread", sources, true)); }); if (views.isEmpty()) views.put("main", new View("main", "Discussion", "Default topic", List.of(), true)); return List.copyOf(views.values()); } public void require(Path root, String work, String id) { if (views(root, work).stream().noneMatch(t -> t.id().equals(id))) throw ProtocolException.missing("Topic not found in Work: " + id); } public Topic get(Path root, String work, String id) { Work.identifier(id, "TOP"); return list(root, work).stream().filter(t -> t.id().equals(id)).findFirst().orElseThrow(() -> ProtocolException.missing("Topic not found: " + id)); } private List chain(Path root, Topic current) { var all = new ArrayList(); all.add(current); var next = current; for (int revision = current.revision() - 1; revision >= 1; revision--) { var p = Revisions.history(works.folder(root, current.workId()), "topics", current.id(), revision); String raw = Filespace.read(p); Revisions.chain(next.revision(), next.previousHash(), revision + 1, raw); var previous = read(p); identity(previous, current.workId(), current.id()); if (previous.revision() != revision) throw ProtocolException.validation("Invalid topic history revision"); all.add(previous); next = previous; } Collections.reverse(all); return List.copyOf(all); } public Topic create(Path root, String work, String title, String description, List sources, String author) { return Filespace.locked(root, () -> { var w = works.editable(root, work); if (!w.discussions().containsAll(sources)) throw ProtocolException.invalid("Unknown topic source"); String id = works.nextReference(w.topics(), "TOP"), now = Instant.now().toString(); var topic = new Topic(1, id, work, title, description, sources, author, now, 1, null, now, author, "Topic created"); var p = path(root, work, id); Filespace.directory(p.getParent()); Documents.writeMarkdownDocument(p, topic, "description", false); works.addReference(root, work, "topics", id); return topic; }); } public Topic update(Path root, String work, String id, String title, String description, String by, String reason) { if (title == null && description == null) throw ProtocolException.invalid("Specify a topic field to update"); return Filespace.locked(root, () -> { works.editable(root, work); var old = get(root, work, id); String raw = Filespace.read(path(root, work, id)); String nextTitle = title == null ? old.title() : title, nextDescription = description == null ? old.description() : description; if (nextTitle.equals(old.title()) && nextDescription.equals(old.description())) return old; var changed = new Topic(1, id, work, nextTitle, nextDescription, old.sources(), old.author(), old.createdAt(), old.revision() + 1, Revisions.hash(raw), Instant.now().toString(), by, reason); Revisions.replace(works.folder(root, work), "topics", id, old.revision(), raw, Documents.markdownDocument(changed, "description")); works.touched(root, work, changed.updatedAt()); return changed; }); } }