package PBS references and expose paginated MCP reads

This commit is contained in:
2026-09-29 08:48:33 +01:00
parent 58f11a8123
commit feb57c5298
23 changed files with 738 additions and 21 deletions
@@ -11,6 +11,8 @@ import java.util.Optional;
public final class PBSFrontendProvider implements FrontendProvider {
private final FrontendPhaseService compiler;
private final PBSFrontendLanguageService languageService;
private final p.studio.compiler.services.FrontendReferenceProvider documentation =
new p.studio.compiler.pbs.documentation.PbsDocumentation();
public PBSFrontendProvider() {
this(new PBSFrontendPhaseService(), new PBSFrontendLanguageService());
@@ -41,4 +43,9 @@ public final class PBSFrontendProvider implements FrontendProvider {
public Optional<FrontendLanguageService> languageService() {
return Optional.of(languageService);
}
@Override
public Optional<p.studio.compiler.services.FrontendReferenceProvider> documentation() {
return Optional.of(documentation);
}
}
@@ -0,0 +1,78 @@
package p.studio.compiler.pbs.documentation;
import com.fasterxml.jackson.databind.ObjectMapper;
import p.studio.AppMode;
import p.studio.compiler.services.FrontendReferenceProvider;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.*;
import java.io.InputStream;
import java.util.function.Function;
/** Loads only the build-generated PBS index and its packaged resources, never user-supplied paths. */
public final class PbsDocumentation implements FrontendReferenceProvider {
private record Index(int schemaVersion, String owner, List<Entry> references) {}
private record Entry(String id, String title, Kind kind, String resource, String sha256,
Integer sdkVersion, String target) {}
private record Installed(Entry entry, Reference reference) {}
private final Function<String, InputStream> resources;
private volatile List<Installed> installed;
public PbsDocumentation() { this(PbsDocumentation.class.getClassLoader()::getResourceAsStream); }
PbsDocumentation(Function<String, InputStream> resources) { this.resources = Objects.requireNonNull(resources); }
@Override public List<Reference> references(int stdlibVersion, AppMode target) {
Objects.requireNonNull(target);
return installed().stream()
.filter(item -> item.entry().sdkVersion() == null || item.entry().sdkVersion() == stdlibVersion)
.filter(item -> item.entry().target() == null || item.entry().target().equals(target.name()))
.map(Installed::reference).toList();
}
private List<Installed> installed() {
if (installed != null) return installed;
synchronized (this) {
if (installed != null) return installed;
try {
var index = new ObjectMapper().readValue(resource("pbs-knowledge/index.json"), Index.class);
if (index.schemaVersion() != 1 || !"pbs".equals(index.owner()) || index.references() == null)
throw new IOException("Invalid PBS reference index");
var entries = new ArrayList<Installed>();
var ids = new HashSet<String>();
for (var entry : index.references()) {
if (entry.id() == null || !entry.id().startsWith("pbs:") || !ids.add(entry.id())
|| entry.sdkVersion() != null && entry.sdkVersion() < 1)
throw new IOException("Invalid or duplicate PBS reference identity");
byte[] bytes = resource(entry.resource());
if (!HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes)).equals(entry.sha256()))
throw new IOException("PBS reference does not match its installed index");
String content = StandardCharsets.UTF_8.newDecoder().onMalformedInput(CodingErrorAction.REPORT)
.onUnmappableCharacter(CodingErrorAction.REPORT).decode(ByteBuffer.wrap(bytes)).toString();
entries.add(new Installed(entry, new Reference(entry.id(), entry.title(), entry.kind(),
entry.sdkVersion() == null ? null : entry.sdkVersion().toString(),
entry.sdkVersion() == null ? "unknown" : "installed PBS SDK major; target=" + entry.target(), content)));
}
installed = List.copyOf(entries);
return installed;
} catch (Exception failure) {
throw new IllegalStateException("Installed PBS documentation is unavailable", failure);
}
}
}
private byte[] resource(String path) throws IOException {
if (path == null || path.contains("..") || path.contains(":") || path.contains("\\")
|| !(path.startsWith("pbs-knowledge/") || path.startsWith("game/stdlib/")))
throw new IOException("Invalid PBS reference resource");
try (var stream = resources.apply(path)) {
if (stream == null) throw new IOException("Missing PBS reference resource");
byte[] bytes = stream.readNBytes(4 * 1024 * 1024 + 1);
if (bytes.length > 4 * 1024 * 1024) throw new IOException("PBS reference resource is too large");
return bytes;
}
}
}
@@ -0,0 +1,8 @@
{
"name": "pbs-reference-example",
"version": "1.0.0",
"language": "pbs",
"stdlib": "1",
"target": "Game",
"dependencies": []
}
@@ -0,0 +1,19 @@
import { Color } from @core:color;
import { Gfx } from @sdk:gfx;
declare global x: int = 0;
[Init]
fn init() -> void {
x = 0;
}
[Frame]
fn frame() -> void {
Gfx.clear(Color.rgb(16, 20, 28));
Gfx.fill_rect(x, 32, 16, 16, Color.rgb(240, 160, 48));
x = x + 1;
if (x > 120) {
x = 0;
}
}
@@ -0,0 +1,2 @@
pub fn init() -> void;
pub fn frame() -> void;