UNPKG

@autobe/agent

Version:

AI backend server code generator

364 lines 26.2 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); 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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.orchestrateTestCorrect = void 0; const __typia_transform__validateReport = __importStar(require("typia/lib/internal/_validateReport.js")); const core_1 = require("@agentica/core"); const typia_1 = __importDefault(require("typia")); const assertSchemaModel_1 = require("../../context/assertSchemaModel"); const enforceToolCall_1 = require("../../utils/enforceToolCall"); const forceRetry_1 = require("../../utils/forceRetry"); const completeTestCode_1 = require("./compile/completeTestCode"); const transformTestCorrectHistories_1 = require("./transformTestCorrectHistories"); const orchestrateTestCorrect = (ctx_1, writeResult_1, ...args_1) => __awaiter(void 0, [ctx_1, writeResult_1, ...args_1], void 0, function* (ctx, writeResult, life = 4) { const result = yield Promise.all(writeResult.map((w) => __awaiter(void 0, void 0, void 0, function* () { try { return yield (0, forceRetry_1.forceRetry)(() => __awaiter(void 0, void 0, void 0, function* () { const event = yield compile(ctx, { artifacts: w.artifacts, scenario: w.scenario, location: w.event.location, script: w.event.final, }); return predicate(ctx, { artifacts: w.artifacts, scenario: w.scenario, location: w.event.location, script: w.event.final, }, event, life); })); } catch (_a) { return null; } }))); return result.filter((r) => r !== null); }); exports.orchestrateTestCorrect = orchestrateTestCorrect; const compile = (ctx, func) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b; const compiler = yield ctx.compiler(); const result = yield compiler.test.compile({ files: Object.assign(Object.assign(Object.assign({}, func.artifacts.dto), func.artifacts.sdk), { [func.location]: func.script }), }); return { type: "testValidate", file: { scenario: func.scenario, location: func.location, content: func.script, }, result, created_at: new Date().toISOString(), step: (_b = (_a = ctx.state().analyze) === null || _a === void 0 ? void 0 : _a.step) !== null && _b !== void 0 ? _b : 0, }; }); const predicate = (ctx, content, event, life) => __awaiter(void 0, void 0, void 0, function* () { if (event.result.type === "failure") ctx.dispatch(event); return event.result.type === "failure" ? correct(ctx, content, event, life - 1) : event; }); const correct = (ctx, content, validate, life) => __awaiter(void 0, void 0, void 0, function* () { var _a, _b, _c; if (validate.result.type !== "failure") return validate; else if (--life <= 0) return validate; const pointer = { value: null, }; const agentica = new core_1.MicroAgentica({ model: ctx.model, vendor: ctx.vendor, config: Object.assign(Object.assign({}, ((_a = ctx.config) !== null && _a !== void 0 ? _a : {})), { executor: { describe: null, }, retry: 4 }), histories: (0, transformTestCorrectHistories_1.transformTestCorrectHistories)(content, validate.result), controllers: [ createApplication({ model: ctx.model, artifacts: content.artifacts, build: (next) => { pointer.value = next; }, }), ], }); (0, enforceToolCall_1.enforceToolCall)(agentica); yield agentica .conversate("Fix the `AutoBeTest.IFunction` data to resolve the compilation error.") .finally(() => { const tokenUsage = agentica.getTokenUsage(); ctx.usage().record(tokenUsage, ["test"]); }); if (pointer.value === null) throw new Error("Failed to modify test code."); const compiler = yield ctx.compiler(); pointer.value.final = yield compiler.typescript.beautify(pointer.value.final); ctx.dispatch(Object.assign({ type: "testCorrect", created_at: new Date().toISOString(), file: validate.file, result: validate.result, step: (_c = (_b = ctx.state().analyze) === null || _b === void 0 ? void 0 : _b.step) !== null && _c !== void 0 ? _c : 0 }, pointer.value)); const newContent = Object.assign(Object.assign({}, content), { script: pointer.value.final }); const newValidate = yield compile(ctx, newContent); return predicate(ctx, newContent, newValidate, life); }); const createApplication = (props) => { (0, assertSchemaModel_1.assertSchemaModel)(props.model); const application = collection[props.model]; return { protocol: "class", name: "Modify Test Code", application, execute: { rewrite: (next) => { next.draft = (0, completeTestCode_1.completeTestCode)(props.artifacts, next.draft); next.final = (0, completeTestCode_1.completeTestCode)(props.artifacts, next.final); props.build(next); }, }, }; }; const claude = { model: "claude", options: { reference: true, separate: null }, functions: [ { name: "rewrite", parameters: { description: " Complete specification for error correction workflow including\nanalysis steps, draft implementation, review process, and final code\ngeneration\n\n------------------------------\n\nCurrent Type: {@link IAutoBeTestCorrectApplication.IProps}", type: "object", properties: { think_without_compile_error: { description: "Step 1: Initial analysis and understanding without compilation error\ncontext.\n\nAI analyzes the original test scenario, business requirements, and\nintended functionality without being influenced by compilation errors.\nThis clean analysis establishes a clear understanding of what the test\nshould accomplish, the expected business workflow, and the correct API\nintegration patterns.\n\nThis step ensures that error correction doesn't lose sight of the\noriginal test purpose and helps maintain the intended business logic\nwhile addressing technical compilation issues. The AI develops a\ncomprehensive understanding of the test requirements before diving into\nerror-specific details.\n\nWorkflow: Scenario understanding \u2192 Business logic analysis \u2192 Intended\nfunctionality mapping", type: "string" }, think_again_with_compile_error: { description: "Step 2: Compilation error analysis and root cause identification.\n\nAI re-analyzes the scenario and implementation with full awareness of\ncompilation errors and diagnostic information. This step involves\nsystematic examination of error messages, identification of error\npatterns, and understanding of how compilation issues relate to the\nintended functionality.\n\nThe AI correlates compilation diagnostics with the original requirements\nto understand where the implementation diverged from correct TypeScript\nusage while maintaining the business logic intent. This analysis forms\nthe foundation for targeted error correction strategies.\n\nWorkflow: Error diagnostic analysis \u2192 Root cause identification \u2192\nCorrection strategy planning", type: "string" }, draft: { description: "Step 3: Draft corrected TypeScript E2E test code implementation.\n\nAI generates the first corrected version of the test code based on error\nanalysis and correction strategies. This draft addresses all identified\ncompilation errors while preserving the original business logic and test\nworkflow. The code must be compilation-error-free and follow all\nestablished conventions.\n\nThe implementation incorporates lessons learned from error analysis to\nproduce properly typed, syntactically correct code that maintains the\nintended test functionality. All type safety requirements and framework\nconventions must be followed in this corrected implementation.\n\nWorkflow: Error correction \u2192 TypeScript implementation \u2192 Functional\npreservation Critical: Must resolve all compilation errors while\nmaintaining original test intent", type: "string" }, review: { description: "Step 4: Code review and correction validation.\n\nAI performs a comprehensive review of the corrected draft implementation,\nvalidating that all compilation errors have been resolved and that the\ncode maintains the original functionality. This review examines both\ntechnical correctness and business logic preservation.\n\nThe review process includes verification of TypeScript compilation\ncompatibility, API integration correctness, test workflow completeness,\nand adherence to all quality standards. Any remaining issues or potential\nimprovements are identified for incorporation into the final\nimplementation.\n\nWorkflow: Draft validation \u2192 Compilation verification \u2192 Functionality\nreview \u2192 Quality assessment", type: "string" }, final: { description: "Step 5: Final production-ready corrected test code.\n\nAI produces the final, polished version of the corrected test code\nincorporating all review feedback and validation results. This code\nrepresents the completed error correction, guaranteed to compile\nsuccessfully while preserving all original test functionality and\nbusiness logic.\n\nThe final implementation resolves all compilation issues, maintains\nstrict type safety, follows all established conventions, and delivers a\nproduction-ready test that accurately validates the intended API\nbehaviors and user workflows.\n\nWorkflow: Review integration \u2192 Final refinement \u2192 Production-ready\nimplementation This is the ultimate deliverable that will replace the\ncompilation-failed code.", type: "string" } }, required: [ "think_without_compile_error", "think_again_with_compile_error", "draft", "review", "final" ], additionalProperties: false, $defs: {} }, description: "Main entry point for AI Function Call - analyzes compilation errors and\ngenerates corrected E2E test code.\n\nThe AI executes this function to perform the complete error correction\nworkflow: error-free analysis \u2192 compilation error analysis \u2192 draft\ncorrection \u2192 code review \u2192 final corrected implementation. This multi-step\nprocess ensures systematic error resolution while preserving original test\nfunctionality and maintaining code quality.\n\nThe corrector first analyzes the scenario without considering compilation\nerrors to understand the intended functionality, then incorporates\ncompilation diagnostics to identify specific issues, and finally produces\ncorrected code through iterative refinement with comprehensive review and\nvalidation.", validate: (() => { const _io0 = input => "string" === typeof input.think_without_compile_error && "string" === typeof input.think_again_with_compile_error && "string" === typeof input.draft && "string" === typeof input.review && "string" === typeof input.final; const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.think_without_compile_error || _report(_exceptionable, { path: _path + ".think_without_compile_error", expected: "string", value: input.think_without_compile_error }), "string" === typeof input.think_again_with_compile_error || _report(_exceptionable, { path: _path + ".think_again_with_compile_error", expected: "string", value: input.think_again_with_compile_error }), "string" === typeof input.draft || _report(_exceptionable, { path: _path + ".draft", expected: "string", value: input.draft }), "string" === typeof input.review || _report(_exceptionable, { path: _path + ".review", expected: "string", value: input.review }), "string" === typeof input.final || _report(_exceptionable, { path: _path + ".final", expected: "string", value: input.final })].every(flag => flag); const __is = input => "object" === typeof input && null !== input && _io0(input); let errors; let _report; return input => { if (false === __is(input)) { errors = []; _report = __typia_transform__validateReport._validateReport(errors); ((input, _path, _exceptionable = true) => ("object" === typeof input && null !== input || _report(true, { path: _path + "", expected: "IAutoBeTestCorrectApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeTestCorrectApplication.IProps", value: input }))(input, "$input", true); const success = 0 === errors.length; return success ? { success, data: input } : { success, errors, data: input }; } return { success: true, data: input }; }; })() } ] }; const collection = { chatgpt: { model: "chatgpt", options: { reference: true, strict: false, separate: null }, functions: [ { name: "rewrite", parameters: { description: " Complete specification for error correction workflow including\nanalysis steps, draft implementation, review process, and final code\ngeneration\n\n------------------------------\n\nCurrent Type: {@link IAutoBeTestCorrectApplication.IProps}", type: "object", properties: { think_without_compile_error: { description: "Step 1: Initial analysis and understanding without compilation error\ncontext.\n\nAI analyzes the original test scenario, business requirements, and\nintended functionality without being influenced by compilation errors.\nThis clean analysis establishes a clear understanding of what the test\nshould accomplish, the expected business workflow, and the correct API\nintegration patterns.\n\nThis step ensures that error correction doesn't lose sight of the\noriginal test purpose and helps maintain the intended business logic\nwhile addressing technical compilation issues. The AI develops a\ncomprehensive understanding of the test requirements before diving into\nerror-specific details.\n\nWorkflow: Scenario understanding \u2192 Business logic analysis \u2192 Intended\nfunctionality mapping", type: "string" }, think_again_with_compile_error: { description: "Step 2: Compilation error analysis and root cause identification.\n\nAI re-analyzes the scenario and implementation with full awareness of\ncompilation errors and diagnostic information. This step involves\nsystematic examination of error messages, identification of error\npatterns, and understanding of how compilation issues relate to the\nintended functionality.\n\nThe AI correlates compilation diagnostics with the original requirements\nto understand where the implementation diverged from correct TypeScript\nusage while maintaining the business logic intent. This analysis forms\nthe foundation for targeted error correction strategies.\n\nWorkflow: Error diagnostic analysis \u2192 Root cause identification \u2192\nCorrection strategy planning", type: "string" }, draft: { description: "Step 3: Draft corrected TypeScript E2E test code implementation.\n\nAI generates the first corrected version of the test code based on error\nanalysis and correction strategies. This draft addresses all identified\ncompilation errors while preserving the original business logic and test\nworkflow. The code must be compilation-error-free and follow all\nestablished conventions.\n\nThe implementation incorporates lessons learned from error analysis to\nproduce properly typed, syntactically correct code that maintains the\nintended test functionality. All type safety requirements and framework\nconventions must be followed in this corrected implementation.\n\nWorkflow: Error correction \u2192 TypeScript implementation \u2192 Functional\npreservation Critical: Must resolve all compilation errors while\nmaintaining original test intent", type: "string" }, review: { description: "Step 4: Code review and correction validation.\n\nAI performs a comprehensive review of the corrected draft implementation,\nvalidating that all compilation errors have been resolved and that the\ncode maintains the original functionality. This review examines both\ntechnical correctness and business logic preservation.\n\nThe review process includes verification of TypeScript compilation\ncompatibility, API integration correctness, test workflow completeness,\nand adherence to all quality standards. Any remaining issues or potential\nimprovements are identified for incorporation into the final\nimplementation.\n\nWorkflow: Draft validation \u2192 Compilation verification \u2192 Functionality\nreview \u2192 Quality assessment", type: "string" }, final: { description: "Step 5: Final production-ready corrected test code.\n\nAI produces the final, polished version of the corrected test code\nincorporating all review feedback and validation results. This code\nrepresents the completed error correction, guaranteed to compile\nsuccessfully while preserving all original test functionality and\nbusiness logic.\n\nThe final implementation resolves all compilation issues, maintains\nstrict type safety, follows all established conventions, and delivers a\nproduction-ready test that accurately validates the intended API\nbehaviors and user workflows.\n\nWorkflow: Review integration \u2192 Final refinement \u2192 Production-ready\nimplementation This is the ultimate deliverable that will replace the\ncompilation-failed code.", type: "string" } }, required: [ "think_without_compile_error", "think_again_with_compile_error", "draft", "review", "final" ], additionalProperties: false, $defs: {} }, description: "Main entry point for AI Function Call - analyzes compilation errors and\ngenerates corrected E2E test code.\n\nThe AI executes this function to perform the complete error correction\nworkflow: error-free analysis \u2192 compilation error analysis \u2192 draft\ncorrection \u2192 code review \u2192 final corrected implementation. This multi-step\nprocess ensures systematic error resolution while preserving original test\nfunctionality and maintaining code quality.\n\nThe corrector first analyzes the scenario without considering compilation\nerrors to understand the intended functionality, then incorporates\ncompilation diagnostics to identify specific issues, and finally produces\ncorrected code through iterative refinement with comprehensive review and\nvalidation.", validate: (() => { const _io0 = input => "string" === typeof input.think_without_compile_error && "string" === typeof input.think_again_with_compile_error && "string" === typeof input.draft && "string" === typeof input.review && "string" === typeof input.final; const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.think_without_compile_error || _report(_exceptionable, { path: _path + ".think_without_compile_error", expected: "string", value: input.think_without_compile_error }), "string" === typeof input.think_again_with_compile_error || _report(_exceptionable, { path: _path + ".think_again_with_compile_error", expected: "string", value: input.think_again_with_compile_error }), "string" === typeof input.draft || _report(_exceptionable, { path: _path + ".draft", expected: "string", value: input.draft }), "string" === typeof input.review || _report(_exceptionable, { path: _path + ".review", expected: "string", value: input.review }), "string" === typeof input.final || _report(_exceptionable, { path: _path + ".final", expected: "string", value: input.final })].every(flag => flag); const __is = input => "object" === typeof input && null !== input && _io0(input); let errors; let _report; return input => { if (false === __is(input)) { errors = []; _report = __typia_transform__validateReport._validateReport(errors); ((input, _path, _exceptionable = true) => ("object" === typeof input && null !== input || _report(true, { path: _path + "", expected: "IAutoBeTestCorrectApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeTestCorrectApplication.IProps", value: input }))(input, "$input", true); const success = 0 === errors.length; return success ? { success, data: input } : { success, errors, data: input }; } return { success: true, data: input }; }; })() } ] }, claude, llama: claude, deepseek: claude, "3.1": claude, }; //# sourceMappingURL=orchestrateTestCorrect.js.map