package PBS references and expose paginated MCP reads
This commit is contained in:
@@ -20,6 +20,8 @@ import java.util.*;
|
||||
|
||||
/** Read-only manifest/provider discovery. Never selects a default frontend or starts a build. */
|
||||
public final class CompilerProjectKnowledge implements ProjectKnowledge {
|
||||
private record OwnedReference(String owner, p.studio.compiler.services.FrontendReferenceProvider.Reference reference) {}
|
||||
private record Discovery(Context context, Map<String, OwnedReference> references) {}
|
||||
private static final int MAX_PROJECTS = 256;
|
||||
private final ProjectAccessRegistry openProjects;
|
||||
private final ProjectCatalog projects;
|
||||
@@ -33,6 +35,10 @@ public final class CompilerProjectKnowledge implements ProjectKnowledge {
|
||||
}
|
||||
|
||||
@Override public Context context(Path openProjectRoot) {
|
||||
return discover(openProjectRoot).context();
|
||||
}
|
||||
|
||||
private Discovery discover(Path openProjectRoot) {
|
||||
Path root = openProjects.requireOpenRoot(openProjectRoot);
|
||||
var limitations = new ArrayList<String>();
|
||||
var discovered = new LinkedHashMap<Path, ProjectReference>();
|
||||
@@ -128,12 +134,15 @@ public final class CompilerProjectKnowledge implements ProjectKnowledge {
|
||||
}
|
||||
}
|
||||
libraries.addAll(unresolved);
|
||||
var references = new LinkedHashMap<String, OwnedReference>();
|
||||
discovered.values().forEach(project -> collectReferences(project, references, limitations));
|
||||
var languages = new ArrayList<Language>();
|
||||
String primary = discovered.get(root).languageId();
|
||||
if (byLanguage.size() > 1) limitations.add("The installed build pipeline requires the same language across the dependency graph");
|
||||
for (var entry : byLanguage.entrySet()) {
|
||||
var language = language(entry.getKey(), entry.getValue(), primary.equals(entry.getKey()),
|
||||
dependenciesComplete && byLanguage.size() == 1, limitations);
|
||||
dependenciesComplete && byLanguage.size() == 1, limitations,
|
||||
references.entrySet().stream().filter(ref -> ref.getValue().owner().equals(entry.getKey())).map(Map.Entry::getKey).toList());
|
||||
languages.add(language);
|
||||
for (Path source : language.sourceRoots()) if (!source.startsWith(root)) readOnly.add(source);
|
||||
for (var project : entry.getValue()) {
|
||||
@@ -144,15 +153,19 @@ public final class CompilerProjectKnowledge implements ProjectKnowledge {
|
||||
}
|
||||
}
|
||||
limitations.add("SDK resources are provider-owned, read-only resources, not editable project files");
|
||||
limitations.add("DOCUMENTATION_UNAVAILABLE: installed language/SDK reference index is not yet available");
|
||||
// Closing a project during discovery must not publish a successful context for that closed project.
|
||||
openProjects.requireOpenRoot(root);
|
||||
return new Context(root, discovered.get(root).name(), languages, libraries,
|
||||
List.of(root), List.copyOf(readOnly), List.of(), limitations);
|
||||
var descriptors = references.values().stream().map(owned -> {
|
||||
var ref = owned.reference();
|
||||
return new Reference(ref.id(), ref.title(), owned.owner(), ReferenceKind.valueOf(ref.kind().name()),
|
||||
new Version(ref.version(), ref.versionSource()));
|
||||
}).toList();
|
||||
return new Discovery(new Context(root, discovered.get(root).name(), languages, libraries,
|
||||
List.of(root), List.copyOf(readOnly), descriptors, limitations.stream().distinct().toList()), Map.copyOf(references));
|
||||
}
|
||||
|
||||
private Language language(String id, List<ProjectReference> projects, boolean primary,
|
||||
boolean graphBuildable, List<String> limitations) {
|
||||
boolean graphBuildable, List<String> limitations, List<String> referenceIds) {
|
||||
var sdkVersions = projects.stream().map(ProjectReference::stdlibVersion).distinct().toList();
|
||||
Version sdk = sdkVersions.size() == 1
|
||||
? new Version(sdkVersions.getFirst().toString(), "prometeu.json#stdlib (requested)") : Version.unknown();
|
||||
@@ -161,7 +174,7 @@ public final class CompilerProjectKnowledge implements ProjectKnowledge {
|
||||
if (provider.isEmpty()) {
|
||||
limitations.add("UNSUPPORTED_LANGUAGE: no installed frontend for " + id);
|
||||
return new Language(id, primary, Version.unknown(), Version.unknown(), sdk, List.of(), List.of(),
|
||||
List.of(capability("build", false), capability("lsp", false)), List.of());
|
||||
List.of(capability("build", false), capability("lsp", false)), referenceIds);
|
||||
}
|
||||
var spec = provider.get().specification();
|
||||
var roots = new LinkedHashSet<Path>();
|
||||
@@ -176,7 +189,35 @@ public final class CompilerProjectKnowledge implements ProjectKnowledge {
|
||||
String compilerVersion = provider.get().getClass().getPackage().getImplementationVersion();
|
||||
return new Language(id, primary, Version.unknown(), compilerVersion == null || compilerVersion.isBlank()
|
||||
? Version.unknown() : new Version(compilerVersion, "installed frontend package Implementation-Version"), sdk,
|
||||
spec.getAllowedExtensions().stream().sorted().toList(), List.copyOf(roots), capabilities, List.of());
|
||||
spec.getAllowedExtensions().stream().sorted().toList(), List.copyOf(roots), capabilities, referenceIds);
|
||||
}
|
||||
|
||||
private void collectReferences(ProjectReference project, Map<String, OwnedReference> references, List<String> limitations) {
|
||||
String owner = project.languageId();
|
||||
String selection = owner + " / " + project.target() + " / SDK " + project.stdlibVersion();
|
||||
try {
|
||||
var documentation = FrontendRegistryService.getProvider(owner).flatMap(p.studio.compiler.services.FrontendProvider::documentation);
|
||||
if (documentation.isEmpty()) {
|
||||
limitations.add("DOCUMENTATION_UNAVAILABLE: no installed reference provider for " + owner);
|
||||
return;
|
||||
}
|
||||
var selected = documentation.get().references(project.stdlibVersion(), p.studio.AppMode.valueOf(project.target().name()));
|
||||
var additions = new LinkedHashMap<String, OwnedReference>();
|
||||
for (var reference : selected) {
|
||||
var owned = new OwnedReference(owner, reference);
|
||||
if (!reference.id().startsWith(owner + ":")
|
||||
|| references.containsKey(reference.id()) && !references.get(reference.id()).equals(owned)
|
||||
|| additions.putIfAbsent(reference.id(), owned) != null)
|
||||
throw new IllegalStateException("Ambiguous or invalid reference ID");
|
||||
}
|
||||
references.putAll(additions);
|
||||
if (selected.isEmpty()) limitations.add("DOCUMENTATION_UNAVAILABLE: no installed references for " + selection);
|
||||
if (selected.stream().noneMatch(ref -> ref.kind() == p.studio.compiler.services.FrontendReferenceProvider.Kind.SDK
|
||||
&& Integer.toString(project.stdlibVersion()).equals(ref.version())))
|
||||
limitations.add("DOCUMENTATION_UNAVAILABLE: no installed SDK reference for " + selection);
|
||||
} catch (RuntimeException unavailable) {
|
||||
limitations.add("DOCUMENTATION_UNAVAILABLE: cannot load installed references for " + selection);
|
||||
}
|
||||
}
|
||||
|
||||
private void addLspCapabilities(String id, ProjectReference project, List<Capability> capabilities, List<String> limitations) {
|
||||
@@ -224,6 +265,21 @@ public final class CompilerProjectKnowledge implements ProjectKnowledge {
|
||||
|
||||
@Override public ReferencePage readReference(Path openProjectRoot, String referenceId, int offset, int limit) {
|
||||
openProjects.requireOpenRoot(openProjectRoot);
|
||||
throw new AutomationException(AutomationFailure.Code.DOCUMENTATION_UNAVAILABLE, "No installed reference index is available");
|
||||
if (referenceId == null || referenceId.isBlank() || offset < 0 || limit < 1 || limit > 16000)
|
||||
throw new IllegalArgumentException("Invalid reference ID or pagination");
|
||||
var discovery = discover(openProjectRoot);
|
||||
var reference = discovery.references().get(referenceId);
|
||||
if (reference == null) {
|
||||
var primary = discovery.context().languages().stream().filter(Language::primary).findFirst().orElseThrow();
|
||||
var code = FrontendRegistryService.getProvider(primary.id()).isEmpty() ? AutomationFailure.Code.UNSUPPORTED_LANGUAGE
|
||||
: discovery.references().isEmpty() ? AutomationFailure.Code.DOCUMENTATION_UNAVAILABLE : AutomationFailure.Code.REFERENCE_NOT_FOUND;
|
||||
throw new AutomationException(code, "Reference is not available in this project's installed index");
|
||||
}
|
||||
String text = reference.reference().text();
|
||||
int total = text.codePointCount(0, text.length());
|
||||
if (offset > total) throw new IllegalArgumentException("Reference offset exceeds its length");
|
||||
int next = offset + Math.min(limit, total - offset);
|
||||
String page = text.substring(text.offsetByCodePoints(0, offset), text.offsetByCodePoints(0, next));
|
||||
return new ReferencePage(referenceId, offset, total, next == total ? null : next, page);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user