@autobe/agent
Version:
AI backend server code generator
169 lines • 7.25 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.orchestrateDatabase = void 0;
const utils_1 = require("@autobe/utils");
const utils_2 = require("@typia/utils");
const uuid_1 = require("uuid");
const predicateStateMessage_1 = require("../../utils/predicateStateMessage");
const orchestrateDatabaseAuthorization_1 = require("./orchestrateDatabaseAuthorization");
const orchestrateDatabaseComponent_1 = require("./orchestrateDatabaseComponent");
const orchestrateDatabaseCorrect_1 = require("./orchestrateDatabaseCorrect");
const orchestrateDatabaseGroup_1 = require("./orchestrateDatabaseGroup");
const orchestrateDatabaseSchema_1 = require("./orchestrateDatabaseSchema");
const orchestrateDatabase = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d;
// PREDICATION
const start = new Date();
const predicate = (0, predicateStateMessage_1.predicateStateMessage)(ctx.state(), "database");
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: "databaseStart",
id: (0, uuid_1.v7)(),
created_at: start.toISOString(),
reason: props.instruction,
step: (_b = (_a = ctx.state().analyze) === null || _a === void 0 ? void 0 : _a.step) !== null && _b !== void 0 ? _b : 0,
});
// NORMALIZE PREFIX
const analyze = ctx.state().analyze;
if (analyze === null || analyze === void 0 ? void 0 : analyze.prefix) {
analyze.prefix = utils_2.NamingConvention.snake(analyze.prefix);
}
// GROUPS
const groups = yield orchestrateGroup(ctx, props);
const components = yield orchestrateComponent(ctx, {
groups,
instruction: props.instruction,
});
const application = yield orchestrateSchema(ctx, {
instruction: props.instruction,
components,
});
// VALIDATE
const validation = yield (0, orchestrateDatabaseCorrect_1.orchestrateDatabaseCorrect)(ctx, application);
const files = (0, utils_1.writePrismaApplication)({
dbms: "postgres",
application: validation.data,
});
// PROPAGATE
const compiler = yield ctx.compiler();
return ctx.dispatch({
type: "databaseComplete",
id: (0, uuid_1.v7)(),
result: validation,
schemas: files,
compiled: yield compiler.database.compilePrismaSchemas({
files,
}),
aggregates: ctx.getCurrentAggregates("database"),
step: (_d = (_c = ctx.state().analyze) === null || _c === void 0 ? void 0 : _c.step) !== null && _d !== void 0 ? _d : 0,
elapsed: new Date().getTime() - start.getTime(),
created_at: new Date().toISOString(),
});
});
exports.orchestrateDatabase = orchestrateDatabase;
const orchestrateGroup = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () {
return yield (0, orchestrateDatabaseGroup_1.orchestrateDatabaseGroup)(ctx, props.instruction);
});
const orchestrateAuthorization = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () {
return yield (0, orchestrateDatabaseAuthorization_1.orchestrateDatabaseAuthorization)(ctx, {
instruction: props.instruction,
groups: props.groups,
});
});
const orchestrateComponent = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () {
const authorization = yield orchestrateAuthorization(ctx, {
groups: props.groups,
instruction: props.instruction,
});
const components = yield (0, orchestrateDatabaseComponent_1.orchestrateDatabaseComponent)(ctx, {
instruction: props.instruction,
groups: props.groups,
});
return [...(authorization ? [authorization] : []), ...components];
});
const orchestrateSchema = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () {
//----
// STATES
//----
// clone groups to keep previous events
const components = props.components.map((c) => (Object.assign(Object.assign({}, c), { tables: c.tables.slice() })));
// completion set
const written = new Set();
const failed = new Map();
const complete = () => components
.flatMap((g) => g.tables)
.every((t) => written.has(t.name) === true);
const pairs = [];
//----
// DEFINER
//----
const application = () => ({
files: components.map((comp) => ({
filename: comp.filename,
namespace: comp.namespace,
models: pairs
.filter((p) => p.namespace === comp.namespace)
.map((p) => p.model),
})),
});
const define = (next) => {
// find parent component and matched design
const myComponent = components.find((c) => c.namespace === next.namespace);
const myTable = myComponent.tables.find((t) => t.name === next.definition.model.name);
// mark as done
written.add(myTable.name);
const existing = pairs.findIndex((p) => p.namespace === next.namespace &&
p.model.name === next.definition.model.name);
if (existing !== -1)
pairs[existing] = {
namespace: next.namespace,
model: next.definition.model,
};
else
pairs.push({ namespace: next.namespace, model: next.definition.model });
// prepare new designs
for (const design of next.definition.newDesigns)
if (written.has(design.name) === false &&
myComponent.tables.find((t) => t.name === design.name) === undefined &&
components
.flatMap((c) => c.tables)
.find((t) => t.name === design.name) === undefined)
myComponent.tables.push(design);
};
//----
// THE LOOP
//----
const writeProgress = { total: 0, completed: 0 };
while (complete() === false) {
const events = yield (0, orchestrateDatabaseSchema_1.orchestrateDatabaseSchema)(ctx, {
instruction: props.instruction,
components,
written,
failed,
progress: writeProgress,
});
for (const e of events)
define({
namespace: e.namespace,
definition: e.definition,
});
}
return application();
});
//# sourceMappingURL=orchestrateDatabase.js.map