@autobe/agent
Version:
AI backend server code generator
125 lines • 5.13 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.orchestrateAnalyze = void 0;
const uuid_1 = require("uuid");
const orchestrateAnalyzeComposer_1 = require("./orchestrateAnalyzeComposer");
const writeDocumentUntilReviewPassed_1 = require("./writeDocumentUntilReviewPassed");
/** @todo Kakasoo */
const orchestrateAnalyze = (ctx) => (props) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b, _c, _d;
const step = (_b = (_a = ctx.state().analyze) === null || _a === void 0 ? void 0 : _a.step) !== null && _b !== void 0 ? _b : 0;
const created_at = new Date().toISOString();
ctx.dispatch({
type: "analyzeStart",
reason: props.reason,
step,
created_at,
});
const composeInputPointer = { value: null };
const agentica = (0, orchestrateAnalyzeComposer_1.orchestrateAnalyzeComposer)(ctx, (v) => {
composeInputPointer.value = v;
});
const determined = yield agentica
.conversate([
`Design a complete list of documents and user roles for this project.`,
`Define user roles that can authenticate via API and create appropriate documentation files.`,
`You must respect the number of documents specified by the user.`,
].join("\n"))
.finally(() => {
const tokenUsage = agentica.getTokenUsage();
ctx.usage().record(tokenUsage, ["analyze"]);
});
const composeInput = composeInputPointer.value;
if (composeInput === null) {
return {
id: (0, uuid_1.v4)(),
text: "Failed to analyze your request. please request again.",
type: "assistantMessage",
completed_at: new Date().toISOString(),
created_at: new Date().toISOString(),
};
}
const { files: tableOfContents, prefix, roles } = composeInput;
if (tableOfContents.length === 0) {
const history = {
id: (0, uuid_1.v4)(),
type: "assistantMessage",
text: "The current requirements are insufficient, so file generation will be suspended. It would be better to continue the conversation.",
created_at,
completed_at: new Date().toISOString(),
};
ctx.dispatch({
type: "assistantMessage",
text: "The current requirements are insufficient, so file generation will be suspended. It would be better to continue the conversation.",
created_at,
});
return history;
}
const retryCount = 3;
const progress = {
total: tableOfContents.length * retryCount,
completed: 0,
};
const pointers = yield Promise.all(tableOfContents.map((_a) => __awaiter(void 0, [_a], void 0, function* ({ filename }) {
return yield (0, writeDocumentUntilReviewPassed_1.writeDocumentUntilReviewPassed)(ctx, {
totalFiles: tableOfContents,
filename,
roles,
progress,
retry: retryCount,
});
})));
const files = pointers
.map((pointer) => {
var _a, _b;
return (_b = (_a = pointer.value) === null || _a === void 0 ? void 0 : _a.files) !== null && _b !== void 0 ? _b : {};
})
.reduce((acc, cur) => Object.assign(acc, cur));
if (Object.keys(files).length) {
const history = {
id: (0, uuid_1.v4)(),
type: "analyze",
reason: props.reason,
prefix,
roles: roles,
files: files,
step,
created_at,
completed_at: new Date().toISOString(),
};
ctx.state().analyze = history;
ctx.histories().push(history);
ctx.dispatch({
type: "analyzeComplete",
prefix,
files,
step,
created_at,
});
return history;
}
const history = {
id: (0, uuid_1.v4)(),
type: "assistantMessage",
text: (_d = (_c = determined.find((el) => el.type === "assistantMessage")) === null || _c === void 0 ? void 0 : _c.text) !== null && _d !== void 0 ? _d : "",
created_at,
completed_at: new Date().toISOString(),
};
ctx.dispatch({
type: history.type,
text: history.text,
created_at: history.created_at,
});
return history;
});
exports.orchestrateAnalyze = orchestrateAnalyze;
//# sourceMappingURL=orchestrateAnalyze.js.map