@autobe/agent
Version:
AI backend server code generator
167 lines • 7.77 kB
JavaScript
;
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.orchestrateInterfaceSchema = void 0;
const utils_1 = require("@autobe/utils");
const orchestrateInterfaceSchemaCasting_1 = require("./orchestrateInterfaceSchemaCasting");
const orchestrateInterfaceSchemaComplement_1 = require("./orchestrateInterfaceSchemaComplement");
const orchestrateInterfaceSchemaDecouple_1 = require("./orchestrateInterfaceSchemaDecouple");
const orchestrateInterfaceSchemaRefine_1 = require("./orchestrateInterfaceSchemaRefine");
const orchestrateInterfaceSchemaRename_1 = require("./orchestrateInterfaceSchemaRename");
const orchestrateInterfaceSchemaReview_1 = require("./orchestrateInterfaceSchemaReview");
const orchestrateInterfaceSchemaWrite_1 = require("./orchestrateInterfaceSchemaWrite");
const AutoBeInterfaceSchemaReviewProgrammer_1 = require("./programmers/AutoBeInterfaceSchemaReviewProgrammer");
const AutoBeJsonSchemaCollection_1 = require("./utils/AutoBeJsonSchemaCollection");
const AutoBeJsonSchemaFactory_1 = require("./utils/AutoBeJsonSchemaFactory");
const AutoBeJsonSchemaNamingConvention_1 = require("./utils/AutoBeJsonSchemaNamingConvention");
const orchestrateInterfaceSchema = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
//----
// PREPARATIONS
//----
// MOCK DOCUMENT
const document = {
operations: props.operations,
components: {
authorizations: (_b = (_a = ctx.state().analyze) === null || _a === void 0 ? void 0 : _a.actors) !== null && _b !== void 0 ? _b : [],
schemas: {},
},
};
// RENAME REQUEST/RESPONSE BODY TYPE NAMES
const renameProgress = {
completed: 0,
total: 0,
};
AutoBeJsonSchemaNamingConvention_1.AutoBeJsonSchemaNamingConvention.normalize({
operations: document.operations,
collection: new AutoBeJsonSchemaCollection_1.AutoBeJsonSchemaCollection({}, {}),
});
yield (0, orchestrateInterfaceSchemaRename_1.orchestrateInterfaceSchemaRename)(ctx, {
operations: document.operations,
progress: renameProgress,
collection: new AutoBeJsonSchemaCollection_1.AutoBeJsonSchemaCollection({}, {}),
});
// PREPARE ITERATOR
const castingProgress = {
completed: 0,
total: 0,
};
const refineProgress = {
completed: 0,
total: 0,
};
const reviewProgress = {
completed: 0,
total: 0,
};
//----
// LOGIC FUNCTION
//----
const iterate = (initialize) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const schemas = {};
const overwrite = (next) => __awaiter(void 0, void 0, void 0, function* () {
for (const [k, v] of Object.entries(next))
if (v === undefined)
delete next[k];
if (Object.keys(next).length === 0)
return;
// assign schemas
const collection = new AutoBeJsonSchemaCollection_1.AutoBeJsonSchemaCollection(document.components.schemas, schemas);
collection.assign(next);
collection.assign(AutoBeJsonSchemaFactory_1.AutoBeJsonSchemaFactory.presets(new Set(Object.keys(schemas))));
// naming convention
AutoBeJsonSchemaNamingConvention_1.AutoBeJsonSchemaNamingConvention.normalize({
operations: document.operations,
collection,
});
yield (0, orchestrateInterfaceSchemaRename_1.orchestrateInterfaceSchemaRename)(ctx, {
operations: document.operations,
progress: renameProgress,
collection,
});
// special logics
AutoBeJsonSchemaFactory_1.AutoBeJsonSchemaFactory.fixPaginationSchemas(document.components.schemas);
AutoBeJsonSchemaFactory_1.AutoBeJsonSchemaFactory.fixAuthorizationSchemas(document.components.schemas);
AutoBeJsonSchemaFactory_1.AutoBeJsonSchemaFactory.finalize({
application: ctx.state().database.result.data,
operations: document.operations,
collection,
});
});
// initialize schemas
yield overwrite(yield initialize());
// type casting
yield overwrite(yield (0, orchestrateInterfaceSchemaCasting_1.orchestrateInterfaceSchemaCasting)(ctx, {
instruction: props.instruction,
document: {
operations: document.operations,
components: {
authorizations: (_b = (_a = ctx.state().analyze) === null || _a === void 0 ? void 0 : _a.actors) !== null && _b !== void 0 ? _b : [],
schemas: document.components.schemas,
},
},
schemas,
progress: castingProgress,
}));
// refine schemas
yield overwrite(yield (0, orchestrateInterfaceSchemaRefine_1.orchestrateInterfaceSchemaRefine)(ctx, {
instruction: props.instruction,
document,
schemas,
progress: refineProgress,
}));
// review schemas
const REVIEW_ITERATIONS = 1;
reviewProgress.total +=
Object.entries(schemas).filter(([k, v]) => AutoBeInterfaceSchemaReviewProgrammer_1.AutoBeInterfaceSchemaReviewProgrammer.filter(k, v)).length * REVIEW_ITERATIONS;
for (let i = 0; i < REVIEW_ITERATIONS; i++)
yield overwrite(yield (0, orchestrateInterfaceSchemaReview_1.orchestrateInterfaceSchemaReview)(ctx, {
instruction: props.instruction,
document,
schemas,
progress: reviewProgress,
}));
});
//----
// SCHEMA GENERATION LOOP
//----
// INITIAL SCHEMAS
yield iterate(() => (0, orchestrateInterfaceSchemaWrite_1.orchestrateInterfaceSchemaWrite)(ctx, {
instruction: props.instruction,
operations: document.operations,
}));
// COMPLEMENTATION
const complementProgress = {
completed: 0,
total: 0,
};
const failures = new Map();
while ((0, utils_1.missedOpenApiSchemas)(document).length !== 0)
yield iterate(() => (0, orchestrateInterfaceSchemaComplement_1.orchestrateInterfaceSchemaComplement)(ctx, {
instruction: props.instruction,
progress: complementProgress,
failures,
document,
}));
// DECOUPLE - break cross-type circular references (A->B->C->A)
yield (0, orchestrateInterfaceSchemaDecouple_1.orchestrateInterfaceSchemaDecouple)(ctx, {
schemas: document.components.schemas,
operations: document.operations,
});
AutoBeJsonSchemaFactory_1.AutoBeJsonSchemaFactory.removeUnused({
operations: document.operations,
schemas: document.components.schemas,
});
return document.components.schemas;
});
exports.orchestrateInterfaceSchema = orchestrateInterfaceSchema;
//# sourceMappingURL=orchestrateInterfaceSchema.js.map