UNPKG

@autobe/agent

Version:

AI backend server code generator

117 lines 5.55 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.orchestrateRealize = void 0; const uuid_1 = require("uuid"); const predicateStateMessage_1 = require("../../utils/predicateStateMessage"); const orchestrateRealizeAuthorizationWrite_1 = require("./orchestrateRealizeAuthorizationWrite"); const orchestrateRealizeCollector_1 = require("./orchestrateRealizeCollector"); const orchestrateRealizeOperation_1 = require("./orchestrateRealizeOperation"); const orchestrateRealizeTransformer_1 = require("./orchestrateRealizeTransformer"); const AutoBeRealizeCollectorProgrammer_1 = require("./programmers/AutoBeRealizeCollectorProgrammer"); const AutoBeRealizeOperationProgrammer_1 = require("./programmers/AutoBeRealizeOperationProgrammer"); const AutoBeRealizeTransformerProgrammer_1 = require("./programmers/AutoBeRealizeTransformerProgrammer"); const compileRealizeFiles_1 = require("./programmers/compileRealizeFiles"); const orchestrateRealize = (ctx) => (props) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c; // PREDICATION const document = (_a = ctx.state().interface) === null || _a === void 0 ? void 0 : _a.document; if (document === undefined) throw new Error("Can't do realize agent because operations are nothing."); const start = new Date(); const predicate = (0, predicateStateMessage_1.predicateStateMessage)(ctx.state(), "realize"); if (predicate !== null) return ctx.assistantMessage({ type: "assistantMessage", id: (0, uuid_1.v7)(), created_at: start.toISOString(), text: predicate, completed_at: new Date().toISOString(), }); ctx.dispatch({ type: "realizeStart", id: (0, uuid_1.v7)(), created_at: start.toISOString(), reason: props.instruction, step: (_c = (_b = ctx.state().test) === null || _b === void 0 ? void 0 : _b.step) !== null && _c !== void 0 ? _c : 0, }); // PREPARE ASSETS const planProgress = { completed: 0, total: Object.keys(document.components.schemas).filter(AutoBeRealizeCollectorProgrammer_1.AutoBeRealizeCollectorProgrammer.filter).length + Object.keys(document.components.schemas).filter((key) => AutoBeRealizeTransformerProgrammer_1.AutoBeRealizeTransformerProgrammer.filter({ schemas: document.components.schemas, key, })).length, }; const writeProgress = { completed: 0, total: document.operations.length, }; const validateProgress = { completed: 0, total: 0, }; // RE-USABLE FUNCTIONS const authorizations = yield (0, orchestrateRealizeAuthorizationWrite_1.orchestrateRealizeAuthorizationWrite)(ctx); const collectors = yield (0, orchestrateRealizeCollector_1.orchestrateRealizeCollector)(ctx, { planProgress, writeProgress, validateProgress, }); const transformers = yield (0, orchestrateRealizeTransformer_1.orchestrateRealizeTransformer)(ctx, { planProgress, writeProgress, validateProgress: validateProgress, }); const out = (next) => { var _a, _b; return ctx.dispatch({ type: "realizeComplete", id: (0, uuid_1.v7)(), functions: [...collectors, ...transformers, ...next.operations], authorizations, controllers: next.controllers, compiled: next.event.result, aggregates: ctx.getCurrentAggregates("realize"), step: (_b = (_a = ctx.state().analyze) === null || _a === void 0 ? void 0 : _a.step) !== null && _b !== void 0 ? _b : 0, elapsed: new Date().getTime() - start.getTime(), created_at: new Date().toISOString(), }); }; // FINAL STEP, THE OPERATIONS const operations = yield (0, orchestrateRealizeOperation_1.orchestrateRealizeOperation)(ctx, { authorizations, collectors, transformers, writeProgress, validateProgress: validateProgress, }); return out({ operations, controllers: yield (yield ctx.compiler()).realize.controller({ document: ctx.state().interface.document, functions: operations, authorizations, }), event: yield (0, compileRealizeFiles_1.compileRealizeFiles)(ctx, { functions: [...collectors, ...transformers, ...operations], additional: AutoBeRealizeOperationProgrammer_1.AutoBeRealizeOperationProgrammer.getAdditional({ authorizations, collectors, transformers, }), progress: () => validateProgress, }), }); }); exports.orchestrateRealize = orchestrateRealize; //# sourceMappingURL=orchestrateRealize.js.map