Add import-safe API/MCP contract tests
This commit is contained in:
1 parent
7b6b176b1b
commit
925f8072e9
3 files changed
+124
-23
No files matched your search
+17
-13
@@ -1,4 +1,5 @@
|
||||
import { compile, analyze } from "./compile.js";
|
||||
import { pathToFileURL } from "node:url";
|
||||
const API_VERSION = "0.3.0";
|
||||
const SCHEMA_VERSION = "1.0.0";
|
||||
|
||||
@@ -38,7 +39,7 @@ function decodePayload(value) {
|
||||
throw new Error("payload must be a JSON object or JSON string");
|
||||
}
|
||||
|
||||
function toolListResult() {
|
||||
export function toolListResult() {
|
||||
return {
|
||||
tools: [
|
||||
{
|
||||
@@ -89,7 +90,7 @@ function toolListResult() {
|
||||
};
|
||||
}
|
||||
|
||||
function withEnvelopeMeta(payload) {
|
||||
export function withEnvelopeMeta(payload) {
|
||||
return {
|
||||
api_version: API_VERSION,
|
||||
schema_version: SCHEMA_VERSION,
|
||||
@@ -97,7 +98,7 @@ function withEnvelopeMeta(payload) {
|
||||
};
|
||||
}
|
||||
|
||||
function uiBundleDescriptor() {
|
||||
export function uiBundleDescriptor() {
|
||||
return {
|
||||
name: "schemeta-workspace",
|
||||
version: "0.2.0",
|
||||
@@ -109,7 +110,7 @@ function uiBundleDescriptor() {
|
||||
};
|
||||
}
|
||||
|
||||
function handleToolCall(name, args) {
|
||||
export function handleToolCall(name, args) {
|
||||
if (!args || typeof args !== "object") {
|
||||
args = {};
|
||||
}
|
||||
@@ -146,7 +147,7 @@ function handleToolCall(name, args) {
|
||||
throw new Error(`Unknown tool '${name}'`);
|
||||
}
|
||||
|
||||
function handleRequest(message) {
|
||||
export function handleRequest(message) {
|
||||
const { id, method, params } = message;
|
||||
|
||||
if (method === "initialize") {
|
||||
@@ -226,13 +227,16 @@ function tryParseBuffer() {
|
||||
}
|
||||
}
|
||||
|
||||
process.stdin.on("data", (chunk) => {
|
||||
stdinBuffer = Buffer.concat([stdinBuffer, Buffer.from(chunk)]);
|
||||
tryParseBuffer();
|
||||
});
|
||||
const isMain = process.argv[1] ? import.meta.url === pathToFileURL(process.argv[1]).href : false;
|
||||
if (isMain) {
|
||||
process.stdin.on("data", (chunk) => {
|
||||
stdinBuffer = Buffer.concat([stdinBuffer, Buffer.from(chunk)]);
|
||||
tryParseBuffer();
|
||||
});
|
||||
|
||||
process.stdin.on("error", (err) => {
|
||||
process.stderr.write(`stdin error: ${String(err)}\n`);
|
||||
});
|
||||
process.stdin.on("error", (err) => {
|
||||
process.stderr.write(`stdin error: ${String(err)}\n`);
|
||||
});
|
||||
|
||||
process.stdin.resume();
|
||||
process.stdin.resume();
|
||||
}
|
||||
+22
-10
@@ -1,6 +1,7 @@
|
||||
import { createServer } from "node:http";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { extname, join, normalize } from "node:path";
|
||||
import { pathToFileURL } from "node:url";
|
||||
import { analyze, compile } from "./compile.js";
|
||||
import { applyLayoutToModel } from "./layout.js";
|
||||
import { validateModel } from "./validate.js";
|
||||
@@ -10,8 +11,8 @@ const MAX_BODY_BYTES = Number(process.env.MAX_BODY_BYTES ?? 2 * 1024 * 1024);
|
||||
const CORS_ORIGIN = process.env.CORS_ORIGIN ?? "*";
|
||||
const MAX_REQUESTS_PER_MINUTE = Number(process.env.MAX_REQUESTS_PER_MINUTE ?? 120);
|
||||
const FRONTEND_ROOT = join(process.cwd(), "frontend");
|
||||
const API_VERSION = "0.3.0";
|
||||
const SCHEMA_VERSION = "1.0.0";
|
||||
export const API_VERSION = "0.3.0";
|
||||
export const SCHEMA_VERSION = "1.0.0";
|
||||
const RATE_WINDOW_MS = 60_000;
|
||||
const rateByClient = new Map();
|
||||
|
||||
@@ -36,7 +37,7 @@ function json(res, status, payload) {
|
||||
res.end(JSON.stringify(payload, null, 2));
|
||||
}
|
||||
|
||||
function errorEnvelope(code, message) {
|
||||
export function errorEnvelope(code, message) {
|
||||
return {
|
||||
ok: false,
|
||||
error: {
|
||||
@@ -114,7 +115,7 @@ async function serveStatic(urlPath, res) {
|
||||
}
|
||||
}
|
||||
|
||||
function parsePayloadOptions(body) {
|
||||
export function parsePayloadOptions(body) {
|
||||
if (body && typeof body === "object" && Object.prototype.hasOwnProperty.call(body, "payload")) {
|
||||
return {
|
||||
payload: body.payload,
|
||||
@@ -128,7 +129,7 @@ function parsePayloadOptions(body) {
|
||||
};
|
||||
}
|
||||
|
||||
function withEnvelopeMeta(payload) {
|
||||
export function withEnvelopeMeta(payload) {
|
||||
return {
|
||||
api_version: API_VERSION,
|
||||
schema_version: SCHEMA_VERSION,
|
||||
@@ -172,7 +173,8 @@ function withinRateLimit(req) {
|
||||
return true;
|
||||
}
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
export function createRequestHandler() {
|
||||
return async (req, res) => {
|
||||
if (!req.url || !req.method) {
|
||||
return json(res, 400, errorEnvelope("invalid_request", "Invalid request."));
|
||||
}
|
||||
@@ -312,8 +314,18 @@ const server = createServer(async (req, res) => {
|
||||
}
|
||||
|
||||
return json(res, 404, errorEnvelope("not_found", "Not found."));
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`Schemeta server listening on http://localhost:${PORT}`);
|
||||
});
|
||||
export const server = createServer(createRequestHandler());
|
||||
|
||||
export function startServer(port = PORT) {
|
||||
server.listen(port, () => {
|
||||
console.log(`Schemeta server listening on http://localhost:${port}`);
|
||||
});
|
||||
}
|
||||
|
||||
const isMain = process.argv[1] ? import.meta.url === pathToFileURL(process.argv[1]).href : false;
|
||||
if (isMain) {
|
||||
startServer(PORT);
|
||||
}
|
||||
Reference in new issue
Block a user