UNPKG

@autobe/agent

Version:

AI backend server code generator

146 lines 7.21 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 executeCachedBatch_1 = require("../../utils/executeCachedBatch"); const predicateStateMessage_1 = require("../../utils/predicateStateMessage"); const compileRealizeFiles_1 = require("./internal/compileRealizeFiles"); const orchestrateRealizeAuthorizationWrite_1 = require("./orchestrateRealizeAuthorizationWrite"); const orchestrateRealizeCorrect_1 = require("./orchestrateRealizeCorrect"); const orchestrateRealizeCorrectCasting_1 = require("./orchestrateRealizeCorrectCasting"); const orchestrateRealizeWrite_1 = require("./orchestrateRealizeWrite"); const generateRealizeScenario_1 = require("./utils/generateRealizeScenario"); const orchestrateRealize = (ctx) => (props) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c, _d, _e; // 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 compiler = yield ctx.compiler(); const authorizations = yield (0, orchestrateRealizeAuthorizationWrite_1.orchestrateRealizeAuthorizationWrite)(ctx); const writeProgress = { total: document.operations.length, completed: 0, }; const correctProgress = { total: document.operations.length, completed: 0, }; const process = (artifacts) => __awaiter(void 0, void 0, void 0, function* () { const writes = (yield (0, executeCachedBatch_1.executeCachedBatch)(ctx, artifacts.map((art) => (promptCacheKey) => __awaiter(void 0, void 0, void 0, function* () { var _a; const write = () => __awaiter(void 0, void 0, void 0, function* () { var _a; try { return yield (0, orchestrateRealizeWrite_1.orchestrateRealizeWrite)(ctx, { totalAuthorizations: authorizations, authorization: (_a = art.decoratorEvent) !== null && _a !== void 0 ? _a : null, scenario: art, document, progress: writeProgress, promptCacheKey, }); } catch (_b) { return null; } }); return (_a = (yield write())) !== null && _a !== void 0 ? _a : (yield write()); })))).filter((w) => w !== null); const functions = Object.entries(Object.fromEntries(writes.map((w) => [w.location, w.content]))).map(([location, content]) => { const scenario = artifacts.find((el) => el.location === location); return { location, content, endpoint: { method: scenario.operation.method, path: scenario.operation.path, }, name: scenario.functionName, }; }); const corrected = yield (0, orchestrateRealizeCorrectCasting_1.orchestrateRealizeCorrectCasting)(ctx, artifacts, authorizations, functions, correctProgress).then((res) => __awaiter(void 0, void 0, void 0, function* () { return yield (0, orchestrateRealizeCorrect_1.orchestrateRealizeCorrect)(ctx, artifacts, authorizations, res, [], correctProgress); })); const validate = yield (0, compileRealizeFiles_1.compileRealizeFiles)(ctx, { authorizations, functions: corrected, }); return { corrected, validate, }; }); // SCENARIOS const entireScenarios = document.operations.map((operation) => (0, generateRealizeScenario_1.generateRealizeScenario)(operation, authorizations)); let bucket = yield process(entireScenarios); for (let i = 0; i < 2; ++i) { if (bucket.validate.result.type !== "failure") break; const failedScenarios = Array.from(new Set(bucket.validate.result.diagnostics.map((f) => f.file))) .map((location) => bucket.corrected.find((f) => f.location === location)) .filter((f) => f !== undefined) .map((f) => entireScenarios.find((s) => s.operation.path === f.endpoint.path && s.operation.method === f.endpoint.method)) .filter((o) => o !== undefined); if (failedScenarios.length === 0) break; writeProgress.total += failedScenarios.length; correctProgress.total += failedScenarios.length; const newBucket = yield process(failedScenarios); const corrected = new Map([ ...bucket.corrected.map((f) => [f.location, f]), ...newBucket.corrected.map((f) => [f.location, f]), ]); bucket = { corrected: Array.from(corrected.values()), validate: newBucket.validate, }; } const controllers = yield compiler.realize.controller({ document: ctx.state().interface.document, functions: bucket.corrected, authorizations, }); return ctx.dispatch({ type: "realizeComplete", id: (0, uuid_1.v7)(), functions: bucket.corrected, authorizations, controllers, compiled: bucket.validate.result, aggregates: ctx.getCurrentAggregates("realize"), step: (_e = (_d = ctx.state().analyze) === null || _d === void 0 ? void 0 : _d.step) !== null && _e !== void 0 ? _e : 0, elapsed: new Date().getTime() - start.getTime(), created_at: new Date().toISOString(), }); }); exports.orchestrateRealize = orchestrateRealize; //# sourceMappingURL=orchestrateRealize.js.map