Add independent semantic fields plugin
This commit is contained in:
parent
ae7e2fa2a4
commit
e562055bfe
12 changed files with 378 additions and 11 deletions
62
plugins/eve-plugin-fields/src/cultnet-sidecar.mjs
Normal file
62
plugins/eve-plugin-fields/src/cultnet-sidecar.mjs
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import { Buffer } from "node:buffer";
|
||||
import { decode, encode } from "@msgpack/msgpack";
|
||||
import { startCultNetOperationServer } from "cultnet-ts";
|
||||
import { dispatch, pluginId } from "./plugin.mjs";
|
||||
|
||||
const host = process.env.EVE_PLUGIN_HOST || "127.0.0.1";
|
||||
const port = Number(process.env.EVE_PLUGIN_PORT || 0);
|
||||
const runtimeId = "eve-plugin-fields";
|
||||
const server = await startCultNetOperationServer({
|
||||
runtimeId,
|
||||
host,
|
||||
port,
|
||||
handler: request => {
|
||||
if (request.serviceId !== pluginId) throw new Error(`Expected service ${pluginId}.`);
|
||||
if (request.payloadSchema !== "gamecult.eve.plugin_abi.request.v1") throw new Error("Unsupported plugin payload schema.");
|
||||
const abiRequest = decode(Buffer.from(request.payload, "base64"));
|
||||
let abiResponse;
|
||||
try {
|
||||
abiResponse = {
|
||||
schema: "gamecult.eve.plugin_abi.response.v1",
|
||||
pluginId,
|
||||
operation: abiRequest.operation,
|
||||
requestId: abiRequest.requestId,
|
||||
status: "accepted",
|
||||
output: dispatch(abiRequest),
|
||||
};
|
||||
} catch (error) {
|
||||
abiResponse = {
|
||||
schema: "gamecult.eve.plugin_abi.response.v1",
|
||||
pluginId,
|
||||
operation: abiRequest?.operation || request.operation,
|
||||
requestId: abiRequest?.requestId || request.messageId,
|
||||
status: "rejected",
|
||||
output: {},
|
||||
diagnostics: [{ message: error instanceof Error ? error.message : String(error) }],
|
||||
};
|
||||
}
|
||||
return {
|
||||
schemaVersion: "cultnet.operation_response.v0",
|
||||
messageId: request.messageId,
|
||||
serviceId: pluginId,
|
||||
operation: request.operation,
|
||||
status: abiResponse.status,
|
||||
payloadSchema: abiResponse.schema,
|
||||
payloadEncoding: "messagepack-base64",
|
||||
payload: Buffer.from(encode(abiResponse)).toString("base64"),
|
||||
diagnostics: (abiResponse.diagnostics || []).map(item => item.message),
|
||||
sourceRuntimeId: runtimeId,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
process.stdout.write(`${JSON.stringify({ schema: "gamecult.eve.plugin_endpoint.v1", pluginId, endpoint: server.endpoint })}\n`);
|
||||
const stop = async () => {
|
||||
await server.close();
|
||||
process.exit(0);
|
||||
};
|
||||
process.once("SIGINT", stop);
|
||||
process.once("SIGTERM", stop);
|
||||
await new Promise(() => {});
|
||||
102
plugins/eve-plugin-fields/src/plugin.mjs
Normal file
102
plugins/eve-plugin-fields/src/plugin.mjs
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
export const pluginId = "fields.surface";
|
||||
export const operations = ["describe", "validate", "project", "measure"];
|
||||
export const componentKinds = ["field.surface2d", "gravity.surface"];
|
||||
|
||||
export function dispatch(request) {
|
||||
if (request?.schema !== "gamecult.eve.plugin_abi.request.v1") throw new Error("Unsupported request schema.");
|
||||
if (request.pluginId !== pluginId) throw new Error(`Expected plugin ${pluginId}.`);
|
||||
if (!operations.includes(request.operation)) throw new Error(`Unsupported operation ${request.operation}.`);
|
||||
switch (request.operation) {
|
||||
case "describe": return describe();
|
||||
case "validate": return validate(request.input || {});
|
||||
case "project": return project(request.input || {});
|
||||
case "measure": return measure(request.input || {});
|
||||
}
|
||||
}
|
||||
|
||||
export function describe() {
|
||||
return {
|
||||
pluginId,
|
||||
componentKinds,
|
||||
commands: [],
|
||||
capabilities: ["field.surface2d", "gravity.surface", "field.scalar", "field.vector", "field.objects"],
|
||||
operations,
|
||||
stateAuthority: "provider-owns-field-documents",
|
||||
};
|
||||
}
|
||||
|
||||
export function validate(input) {
|
||||
const requested = Array.isArray(input.componentKinds) ? input.componentKinds : [];
|
||||
const document = input.document || {};
|
||||
const diagnostics = [];
|
||||
if (document.schema && document.schema !== "gamecult.fields.surface2d.v1") diagnostics.push(`Unsupported field schema ${document.schema}.`);
|
||||
if (document.bounds && !validBounds(document.bounds)) diagnostics.push("Field bounds must have increasing finite axes.");
|
||||
return {
|
||||
acceptedComponentKinds: requested.filter(kind => componentKinds.includes(kind)),
|
||||
rejectedComponentKinds: requested.filter(kind => !componentKinds.includes(kind)),
|
||||
validDocument: diagnostics.length === 0,
|
||||
diagnostics,
|
||||
requiredCapabilities: ["field.surface2d"],
|
||||
};
|
||||
}
|
||||
|
||||
export function project(input) {
|
||||
const document = requiredObject(input.document, "document");
|
||||
if (document.schema !== "gamecult.fields.surface2d.v1") throw new Error("document must use gamecult.fields.surface2d.v1.");
|
||||
if (!validBounds(document.bounds)) throw new Error("document bounds are invalid.");
|
||||
const channels = Array.isArray(document.channels) ? document.channels.map(normalizeChannel) : [];
|
||||
return {
|
||||
schema: "gamecult.fields.projection.v1",
|
||||
projectionKind: "semantic-field-surface",
|
||||
ownedComponentKinds: componentKinds,
|
||||
fieldId: requiredString(document.fieldId, "fieldId"),
|
||||
bounds: { ...document.bounds },
|
||||
channels,
|
||||
objects: document.objects ? normalizeDocumentRef(document.objects, "objects") : null,
|
||||
nativeProjectionAuthority: "runtime-adapter",
|
||||
providerStateMutated: false,
|
||||
};
|
||||
}
|
||||
|
||||
export function measure(input) {
|
||||
const projection = project(input);
|
||||
return {
|
||||
measurementKind: "field-domain-metrics",
|
||||
width: projection.bounds.maxX - projection.bounds.minX,
|
||||
height: projection.bounds.maxY - projection.bounds.minY,
|
||||
channelCount: projection.channels.length,
|
||||
preservesProviderAuthority: true,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeChannel(channel, index) {
|
||||
const value = requiredObject(channel, `channels[${index}]`);
|
||||
const kind = requiredString(value.kind, `channels[${index}].kind`);
|
||||
if (!["scalar", "vector", "color", "mask"].includes(kind)) throw new Error(`Unsupported field channel kind ${kind}.`);
|
||||
return {
|
||||
channelId: requiredString(value.channelId, `channels[${index}].channelId`),
|
||||
kind,
|
||||
...normalizeDocumentRef(value, `channels[${index}]`),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeDocumentRef(value, field) {
|
||||
return {
|
||||
documentId: requiredString(value.documentId, `${field}.documentId`),
|
||||
schemaId: requiredString(value.schemaId, `${field}.schemaId`),
|
||||
};
|
||||
}
|
||||
|
||||
function validBounds(value) {
|
||||
return value && [value.minX, value.minY, value.maxX, value.maxY].every(Number.isFinite) && value.minX < value.maxX && value.minY < value.maxY;
|
||||
}
|
||||
|
||||
function requiredObject(value, field) {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error(`${field} is required.`);
|
||||
return value;
|
||||
}
|
||||
|
||||
function requiredString(value, field) {
|
||||
if (typeof value !== "string" || !value.trim()) throw new Error(`${field} is required.`);
|
||||
return value;
|
||||
}
|
||||
27
plugins/eve-plugin-fields/src/sidecar.mjs
Normal file
27
plugins/eve-plugin-fields/src/sidecar.mjs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
import readline from "node:readline";
|
||||
import { dispatch, pluginId } from "./plugin.mjs";
|
||||
|
||||
readline.createInterface({ input: process.stdin }).on("line", line => {
|
||||
if (!line.trim()) return;
|
||||
let request;
|
||||
try {
|
||||
request = JSON.parse(line.replace(/^\uFEFF/, ""));
|
||||
respond(request, dispatch(request));
|
||||
} catch (error) {
|
||||
respond(request, {}, "rejected", error instanceof Error ? error.message : String(error));
|
||||
}
|
||||
});
|
||||
|
||||
function respond(request, output, status = "accepted", message) {
|
||||
process.stdout.write(`${JSON.stringify({
|
||||
schema: "gamecult.eve.plugin_abi.response.v1",
|
||||
pluginId,
|
||||
operation: request?.operation || "unknown",
|
||||
requestId: request?.requestId || "unknown",
|
||||
status,
|
||||
output,
|
||||
...(message ? { diagnostics: [{ message }] } : {}),
|
||||
})}\n`);
|
||||
}
|
||||
Reference in a new issue