UNPKG

@autobe/agent

Version:

AI backend server code generator

247 lines 10.5 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.AutoBeMockAgent = void 0; const tstl_1 = require("tstl"); const uuid_1 = require("uuid"); const AutoBeAgentBase_1 = require("./AutoBeAgentBase"); const AutoBeTokenUsage_1 = require("./context/AutoBeTokenUsage"); const createAutoBeMessageContent_1 = require("./factory/createAutoBeMessageContent"); const createAutoBeState_1 = require("./factory/createAutoBeState"); const getAutoBeGenerated_1 = require("./factory/getAutoBeGenerated"); /** @internal */ class AutoBeMockAgent extends AutoBeAgentBase_1.AutoBeAgentBase { constructor(props) { super(); this.props_ = props; this.histories_ = []; this.compiler_ = new tstl_1.Singleton(() => __awaiter(this, void 0, void 0, function* () { return props.compiler({ realize: { test: { onOperation: () => __awaiter(this, void 0, void 0, function* () { }), onReset: () => __awaiter(this, void 0, void 0, function* () { }), }, }, }); })); this.token_usage_ = new AutoBeTokenUsage_1.AutoBeTokenUsage(); } conversate(content) { return __awaiter(this, void 0, void 0, function* () { const contents = typeof content === "string" ? [ { type: "text", text: content, }, ] : Array.isArray(content) ? content : [content]; // ALREADY REALIZED CASE const state = (0, createAutoBeState_1.createAutoBeState)(this.histories_); if (state.realize !== null) { const userMessage = { id: (0, uuid_1.v7)(), type: "userMessage", contents: contents.map((c) => (0, createAutoBeMessageContent_1.createAutoBeUserMessageContent)({ content: c })), created_at: new Date().toISOString(), }; void this.dispatch(userMessage).catch(() => { }); yield (0, tstl_1.sleep_for)(2000); const assistantMessage = { id: (0, uuid_1.v7)(), type: "assistantMessage", text: [ "AutoBE has successfully realized the application.", "", "Thanks for using AutoBE!", ].join("\n"), created_at: new Date().toISOString(), completed_at: new Date().toISOString(), }; void this.dispatch(assistantMessage).catch(() => { }); this.histories_.push(userMessage, assistantMessage); return this.histories_; } const take = (type) => __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d; const snapshots = this.getEventSnapshots(type); if (snapshots === null) { this.histories_.push({ id: (0, uuid_1.v7)(), type: "userMessage", contents: contents.map((c) => (0, createAutoBeMessageContent_1.createAutoBeUserMessageContent)({ content: c })), created_at: new Date().toISOString(), }); this.histories_.push({ id: (0, uuid_1.v7)(), type: "assistantMessage", text: [ "The histories are prepared until current state.", "", "Thanks for using AutoBE!", ].join("\n"), created_at: new Date().toISOString(), completed_at: new Date().toISOString(), }); return; } // Find the nearest user message that led into this phase for history // tracking. There may be assistant messages between the user request // and the resulting phase history, so we search backwards. const phaseIndex = this.props_.replay.histories.findIndex((h) => h.type === type); const originalUserMessage = phaseIndex > 0 ? this.props_.replay.histories .slice(0, phaseIndex) .reverse() .find((h) => h.type === "userMessage") : undefined; for (const s of snapshots) { const time = (_d = (_c = (_b = (_a = this.props_).delay) === null || _b === void 0 ? void 0 : _b.call(_a, s.event.type)) !== null && _c !== void 0 ? _c : sleepMap[s.event.type]) !== null && _d !== void 0 ? _d : 500; yield (0, tstl_1.sleep_for)((0, tstl_1.randint)(time * 0.2, time * 1.8)); void this.dispatch(s.event).catch(() => { }); this.token_usage_ = new AutoBeTokenUsage_1.AutoBeTokenUsage(s.tokenUsage); } this.histories_.push((originalUserMessage === null || originalUserMessage === void 0 ? void 0 : originalUserMessage.type) === "userMessage" ? originalUserMessage : { id: (0, uuid_1.v7)(), type: "userMessage", contents: contents.map((c) => (0, createAutoBeMessageContent_1.createAutoBeUserMessageContent)({ content: c })), created_at: new Date().toISOString(), }); this.histories_.push(this.props_.replay.histories.find((h) => h.type === type)); }); if (state.analyze === null) yield take("analyze"); else if (state.database === null) yield take("database"); else if (state.interface === null) yield take("interface"); else if (state.test === null) yield take("test"); else if (state.realize === null) yield take("realize"); return this.histories_; }); } getHistories() { return this.histories_; } getTokenUsage() { return this.token_usage_; } getFiles(options) { return __awaiter(this, void 0, void 0, function* () { return yield (0, getAutoBeGenerated_1.getAutoBeGenerated)({ compiler: yield this.compiler_.get(), state: (0, createAutoBeState_1.createAutoBeState)(this.histories_), histories: this.getHistories(), tokenUsage: this.getTokenUsage(), options, }); }); } getPhase() { var _a, _b, _c, _d; const state = (0, createAutoBeState_1.createAutoBeState)(this.histories_); if (state.analyze === null) return null; else if (((_a = state.realize) === null || _a === void 0 ? void 0 : _a.step) === state.analyze.step) return "realize"; else if (((_b = state.test) === null || _b === void 0 ? void 0 : _b.step) === state.analyze.step) return "test"; else if (((_c = state.interface) === null || _c === void 0 ? void 0 : _c.step) === state.analyze.step) return "interface"; else if (((_d = state.database) === null || _d === void 0 ? void 0 : _d.step) === state.analyze.step) return "database"; return "analyze"; } getEventSnapshots(state) { var _a; return (_a = this.props_.replay[state]) !== null && _a !== void 0 ? _a : null; } } exports.AutoBeMockAgent = AutoBeMockAgent; const sleepMap = { userMessage: 1000, assistantMessage: 1000, vendorRequest: 0, vendorResponse: 0, vendorTimeout: 0, jsonParseError: 0, jsonValidateError: 0, consentFunctionCall: 0, preliminaryAcquire: 0, preliminaryRewrite: 0, // DESCRIBE imageDescribeStart: 1000, imageDescribeDraft: 300, imageDescribeComplete: 1000, // ANALYZE analyzeStart: 1000, analyzeScenario: 1000, analyzeWriteModule: 500, analyzeWriteUnit: 250, analyzeWriteSection: 200, analyzeSectionReview: 300, analyzeComplete: 1000, // PRISMA databaseStart: 1000, databaseGroup: 1000, databaseAuthorization: 1000, databaseComponent: 1000, databaseSchema: 500, databaseValidate: 2000, databaseCorrect: 500, databaseComplete: 1000, // INTERFACE interfaceStart: 1000, interfaceGroup: 1000, interfaceEndpoint: 500, interfaceOperation: 400, interfaceAuthorization: 400, interfaceSchema: 400, interfaceSchemaCasting: 400, interfaceSchemaRefine: 400, interfaceSchemaReview: 200, interfaceSchemaRename: 200, interfaceSchemaComplement: 400, interfaceSchemaDecouple: 400, interfaceComplete: 1000, interfacePrerequisite: 400, // TEST testStart: 1000, testScenario: 40, testWrite: 40, testValidate: 100, testCorrect: 100, testComplete: 1000, // REALIZE realizeStart: 1000, realizeComplete: 1000, realizePlan: 80, realizeWrite: 80, realizeCorrect: 80, realizeValidate: 200, realizeAuthorizationStart: 1000, realizeAuthorizationWrite: 200, realizeAuthorizationValidate: 200, realizeAuthorizationCorrect: 200, realizeAuthorizationComplete: 1000, realizeTestStart: 1000, realizeTestReset: 2500, realizeTestOperation: 400, realizeTestComplete: 1000, }; //# sourceMappingURL=AutoBeMockAgent.js.map