@hotmeshio/hotmesh
Version:
Permanent-Memory Workflows & AI Agents
113 lines (112 loc) • 4.46 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.CompilerService = void 0;
const fs = __importStar(require("fs/promises"));
const path = __importStar(require("path"));
const json_schema_ref_parser_1 = __importDefault(require("@apidevtools/json-schema-ref-parser"));
const js_yaml_1 = __importDefault(require("js-yaml"));
const deployer_1 = require("./deployer");
const validator_1 = require("./validator");
/**
* The compiler service converts a graph into a executable program.
*/
class CompilerService {
constructor(store, stream, logger) {
this.store = store;
this.stream = stream;
this.logger = logger;
}
/**
* verifies and plans the deployment of an app to the DB; the app is not deployed yet
* @param path
*/
async plan(mySchemaOrPath) {
try {
let schema;
if (this.isPath(mySchemaOrPath)) {
schema = (await json_schema_ref_parser_1.default.dereference(mySchemaOrPath));
}
else {
schema = js_yaml_1.default.load(mySchemaOrPath);
}
// 1) validate the manifest file
const validator = new validator_1.Validator(schema);
validator.validate(this.store);
// 2) todo: add a PlannerService module that will plan the deployment (what might break, drift, etc)
return schema;
}
catch (err) {
this.logger.error('compiler-plan-error', err);
}
}
isPath(input) {
return !input.trim().startsWith('app:');
}
/**
* deploys an app to the DB but does NOT activate it.
*/
async deploy(mySchemaOrPath) {
try {
let schema;
if (this.isPath(mySchemaOrPath)) {
schema = (await json_schema_ref_parser_1.default.dereference(mySchemaOrPath));
await this.saveAsJSON(mySchemaOrPath, schema);
}
else {
schema = js_yaml_1.default.load(mySchemaOrPath);
}
// 2) validate the manifest file (synchronous operation...no callbacks)
const validator = new validator_1.Validator(schema);
validator.validate(this.store);
// 3) deploy the schema (segment, optimize, etc; save to the DB)
const deployer = new deployer_1.Deployer(schema);
await deployer.deploy(this.store, this.stream);
// 4) save the app version to the DB (so it can be activated later)
await this.store.setApp(schema.app.id, schema.app.version);
return schema;
}
catch (err) {
this.logger.error('compiler-deploy-error', err);
}
}
/**
* activates a deployed version of an app;
* @param appId
* @param appVersion
*/
async activate(appId, appVersion) {
return await this.store.activateAppVersion(appId, appVersion);
}
async saveAsJSON(originalPath, schema) {
const json = JSON.stringify(schema, null, 2);
const newPath = path.join(path.dirname(originalPath), `.hotmesh.${schema.app.id}.${schema.app.version}.json`);
await fs.writeFile(newPath, json, 'utf8');
}
}
exports.CompilerService = CompilerService;