@autobe/agent
Version:
AI backend server code generator
185 lines • 9.33 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.getAutoBeGenerated = getAutoBeGenerated;
const utils_1 = require("@autobe/utils");
const getAutoBeRealizeGenerated_1 = require("./getAutoBeRealizeGenerated");
function getAutoBeGenerated(props) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
const options = {
phase: ((_b = (_a = props.options) === null || _a === void 0 ? void 0 : _a.phase) !== null && _b !== void 0 ? _b : props.state.realize)
? "realize"
: props.state.test
? "test"
: props.state.interface
? "interface"
: "analyze",
dbms: (_d = (_c = props.options) === null || _c === void 0 ? void 0 : _c.dbms) !== null && _d !== void 0 ? _d : "postgres",
};
const ret = yield props.compiler.getTemplate(options);
ret["README.md"] = writeReadMe(props.state, ret["README.md"]);
if (props.state.analyze === null)
return ret;
// ANALYZE
Object.assign(ret, Object.fromEntries(props.state.analyze.files.map((file) => [
`docs/analysis/${file.filename.split("/").at(-1)}`,
file.content,
])));
if (((_e = props.options) === null || _e === void 0 ? void 0 : _e.phase) === "analyze")
return ret;
// PRISMA
if (((_f = props.state.prisma) === null || _f === void 0 ? void 0 : _f.step) === props.state.analyze.step) {
const schemaFiles = ((_g = options === null || options === void 0 ? void 0 : options.dbms) !== null && _g !== void 0 ? _g : "postgres") === "postgres"
? props.state.prisma.schemas
: yield props.compiler.prisma.write(props.state.prisma.result.data, options.dbms);
Object.assign(ret, Object.fromEntries(Object.entries(schemaFiles).map(([key, value]) => [
`prisma/schema/${key.split("/").at(-1)}`,
value,
])), {
"autobe/prisma.json": JSON.stringify(props.state.prisma.result.data),
});
if (props.state.prisma.compiled.type === "success")
ret["docs/ERD.md"] = props.state.prisma.compiled.document;
else if (props.state.prisma.compiled.type === "failure")
ret["prisma/compile-error-reason.log"] =
props.state.prisma.compiled.reason;
}
if (((_h = props.options) === null || _h === void 0 ? void 0 : _h.phase) === "prisma")
return ret;
// INTERFACE
if (((_j = props.state.interface) === null || _j === void 0 ? void 0 : _j.step) === props.state.analyze.step) {
const files = yield props.compiler.interface.write(props.state.interface.document, Object.keys(ret));
Object.assign(ret, ((_k = props.state.test) === null || _k === void 0 ? void 0 : _k.step) === props.state.interface.step
? Object.fromEntries(Object.entries(files).filter(([key]) => key.startsWith("test/features/") === false))
: files, {
"autobe/document.json": JSON.stringify(props.state.interface.document),
});
}
if (((_l = props.options) === null || _l === void 0 ? void 0 : _l.phase) === "interface")
return ret;
// TEST
if (((_m = props.state.test) === null || _m === void 0 ? void 0 : _m.step) === props.state.analyze.step)
Object.assign(ret, Object.fromEntries(props.state.test.files.map((f) => [f.location, f.content])));
// REALIZE
if (((_o = props.state.realize) === null || _o === void 0 ? void 0 : _o.step) === props.state.analyze.step)
Object.assign(ret, yield (0, getAutoBeRealizeGenerated_1.getAutoBeRealizeGenerated)({
compiler: props.compiler,
document: props.state.interface.document,
authorizations: props.state.realize.authorizations,
functions: props.state.realize.functions,
options: {
dbms: (_p = options === null || options === void 0 ? void 0 : options.dbms) !== null && _p !== void 0 ? _p : "postgres",
},
}));
if (((_q = props.options) === null || _q === void 0 ? void 0 : _q.phase) === "test")
return ret;
// LOGGING
Object.assign(ret, {
"autobe/histories.json": JSON.stringify(props.histories),
"autobe/tokenUsage.json": JSON.stringify(props.tokenUsage),
});
return ret;
});
}
function writeReadMe(state, readme) {
const emoji = (history) => (history ? (success(history) ? "✅" : "❌") : "⬜");
return readme
.replaceAll("{{ANALYSIS_EMOJI}}", emoji(state.analyze))
.replaceAll("{{PRISMA_EMOJI}}", emoji(state.prisma))
.replaceAll("{{INTERFACE_EMOJI}}", emoji(state.interface))
.replaceAll("{{TEST_EMOJI}}", emoji(state.test))
.replaceAll("{{REALIZE_EMOJI}}", emoji(state.realize))
.replaceAll("{{BENCHMARK_AGGREGATE}}", writeBenchmarkAggregate(state))
.replaceAll("{{BENCHMARK_FUNCTION_CALLING}}", writeBenchmarkFunctionCalling(state));
}
function writeBenchmarkAggregate(state) {
return ["analyze", "prisma", "interface", "test", "realize"]
.map((key) => {
const h = state[key];
if (h === null)
return `⬜ ${key} | | | | `;
return [
`${success(h) ? "✅" : "❌"} ${h.type}`,
Object.entries(label(h))
.map(([k, v]) => `${k}: ${v.toLocaleString()}`)
.join(", "),
((h.aggregates.total.metric.success /
h.aggregates.total.metric.attempt) *
100).toFixed(2) + " %",
h.aggregates.total.tokenUsage.total.toLocaleString(),
Math.round((new Date(h.completed_at).getTime() -
new Date(h.created_at).getTime()) /
1000) + " sec",
].join(" | ");
})
.join("\n");
}
function writeBenchmarkFunctionCalling(state) {
const aggregates = utils_1.AutoBeProcessAggregateFactory.reduce([state.analyze, state.prisma, state.interface, state.test, state.realize]
.filter((h) => h !== null)
.map((h) => h.aggregates));
return Object.entries(aggregates)
.map(([key, value]) => [
key,
value.metric.attempt.toLocaleString(),
value.metric.validationFailure.toLocaleString(),
value.metric.invalidJson.toLocaleString(),
value.metric.success.toLocaleString(),
((value.metric.success / value.metric.attempt) * 100).toFixed(2) + " %",
].join(" | "))
.join("\n");
}
function success(history) {
if (history.type === "analyze")
return true;
else if (history.type === "interface")
return history.missed.length === 0;
else if (history.type === "prisma" ||
history.type === "test" ||
history.type === "realize")
return history.compiled.type === "success";
history;
throw new Error("Unknown history type encountered.");
}
function label(history) {
if (history.type === "analyze")
return {
actors: history.actors.length,
documents: history.files.length,
};
else if (history.type === "prisma")
return {
namespaces: history.result.data.files.length,
models: history.result.data.files.map((f) => f.models).flat().length,
};
else if (history.type === "interface")
return {
operations: history.document.operations.length,
schemas: Object.keys(history.document.components.schemas).length,
};
else if (history.type === "test") {
return Object.assign({ functions: history.files.length }, (history.compiled.type === "failure"
? {
errors: new Set(history.compiled.diagnostics.map((d) => { var _a; return (_a = d.file) !== null && _a !== void 0 ? _a : ""; })).size,
}
: {}));
}
else if (history.type === "realize")
return Object.assign({ functions: history.functions.length }, (history.compiled.type === "failure"
? {
errors: new Set(history.compiled.diagnostics.map((d) => { var _a; return (_a = d.file) !== null && _a !== void 0 ? _a : ""; })).size,
}
: {}));
history;
throw new Error("Unknown history type encountered.");
}
//# sourceMappingURL=getAutoBeGenerated.js.map