UNPKG

@autobe/agent

Version:

AI backend server code generator

446 lines 27.1 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.orchestrateTestWrite = orchestrateTestWrite; 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 getTestScenarioArtifacts_1 = require("./compile/getTestScenarioArtifacts"); const transformTestWriteHistories_1 = require("./transformTestWriteHistories"); function orchestrateTestWrite(ctx, scenarios) { return __awaiter(this, void 0, void 0, function* () { const start = new Date(); let complete = 0; const result = yield Promise.all( /** * Generate test code for each scenario. Maps through plans array to create * individual test code implementations. Each scenario is processed to * generate corresponding test code and progress events. */ scenarios.map((scenario) => __awaiter(this, void 0, void 0, function* () { try { const r = yield (0, forceRetry_1.forceRetry)(() => __awaiter(this, void 0, void 0, function* () { var _a, _b; const artifacts = yield (0, getTestScenarioArtifacts_1.getTestScenarioArtifacts)(ctx, scenario); const result = yield process(ctx, scenario, artifacts); const event = Object.assign(Object.assign({ type: "testWrite", created_at: start.toISOString(), location: `test/features/api/${result.domain}/${scenario.functionName}.ts` }, result), { completed: ++complete, total: scenarios.length, step: (_b = (_a = ctx.state().interface) === null || _a === void 0 ? void 0 : _a.step) !== null && _b !== void 0 ? _b : 0 }); ctx.dispatch(event); return { scenario, artifacts, event, }; })); return r; } catch (_a) { return null; } }))); return result.filter((r) => r !== null); }); } /** * Process function that generates test code for each individual scenario. Takes * the AutoBeContext and scenario information as input and uses MicroAgentica to * generate appropriate test code through LLM interaction. * * @param ctx - The AutoBeContext containing model, vendor and configuration * @param scenario - The test scenario information to generate code for * @param artifacts - The artifacts containing the reference files and schemas * @returns Promise resolving to IAutoBeTestWriteApplication.IProps containing * the generated test code */ function process(ctx, scenario, artifacts) { return __awaiter(this, void 0, void 0, function* () { var _a; const pointer = { value: null, }; const agentica = new core_1.MicroAgentica({ model: ctx.model, vendor: ctx.vendor, config: Object.assign({}, ((_a = ctx.config) !== null && _a !== void 0 ? _a : {})), histories: (0, transformTestWriteHistories_1.transformTestWriteHistories)(scenario, artifacts), controllers: [ createApplication({ model: ctx.model, artifacts, build: (next) => { pointer.value = next; }, }), ], }); (0, enforceToolCall_1.enforceToolCall)(agentica); yield agentica.conversate("Create e2e test functions.").finally(() => { const tokenUsage = agentica.getTokenUsage(); ctx.usage().record(tokenUsage, ["test"]); }); if (pointer.value === null) throw new Error("Failed to create test code."); const compiler = yield ctx.compiler(); pointer.value.final = yield compiler.typescript.beautify(pointer.value.final); return pointer.value; }); } function createApplication(props) { (0, assertSchemaModel_1.assertSchemaModel)(props.model); const application = collection[props.model]; return { protocol: "class", name: "Create Test Code", application, execute: { write: (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: "write", parameters: { description: " Complete specification for test generation including scenario,\ndomain, and implementation steps\n\n------------------------------\n\nCurrent Type: {@link IAutoBeTestWriteApplication.IProps}", type: "object", properties: { scenario: { description: "Step 1: Strategic test planning and scenario analysis.\n\nAI analyzes the given test scenario and creates a comprehensive\nimplementation strategy. This planning phase is crucial for generating\nwell-structured, maintainable test code. The AI must define test\nmethodology, data preparation, execution flow, and validation logic\nbefore proceeding to code implementation.\n\nWorkflow: Input scenario \u2192 Strategic analysis \u2192 Detailed test plan", type: "string" }, domain: { description: "Step 2: Functional domain classification for test organization.\n\nAI determines the appropriate domain category based on the scenario\nanalysis. This classification drives file structure, test categorization,\nand logical grouping. The domain must be a single, lowercase word that\nrepresents the primary API resource.\n\nWorkflow: Scenario analysis \u2192 Domain identification \u2192 Test organization\nstructure", type: "string" }, draft: { description: "Step 3: Initial TypeScript E2E test code implementation.\n\nAI generates the first working version of the test code based on the\nstrategic plan. This draft must be compilation-error-free and follow", type: "string" }, review: { description: "Step 4: Code review and quality assessment.\n\nAI performs a thorough review of the draft implementation, examining:\n\n**Compilation & Syntax:**\n\n- TypeScript compilation errors and type mismatches\n- Syntax errors and missing semicolons/brackets\n- Correct function signatures and parameter types\n\n**Framework Compliance:**\n\n-", type: "string" }, final: { description: "Step 5: Final production-ready test code.\n\nAI produces the final, polished version of the test code incorporating\nall review feedback. This code represents the completed test\nimplementation, ready for production deployment. All identified issues\nmust be resolved, and the code must meet the highest quality standards.\n\nWorkflow: Review feedback \u2192 Code refinement \u2192 Production-ready\nimplementation\n\nThis is the ultimate deliverable that will be used in the actual test\nsuite.", type: "string" } }, required: [ "scenario", "domain", "draft", "review", "final" ], additionalProperties: false, $defs: {} }, description: "Main entry point for AI Function Call - generates complete E2E test code.\n\nThe AI executes this function to perform the entire test generation\nworkflow: scenario analysis \u2192 draft implementation \u2192 code review \u2192 final\ncode production. This structured approach ensures high-quality,\ncompilation-error-free test code.", validate: (() => { const _io0 = input => "string" === typeof input.scenario && "string" === typeof input.domain && "string" === typeof input.draft && "string" === typeof input.review && "string" === typeof input.final; const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.scenario || _report(_exceptionable, { path: _path + ".scenario", expected: "string", value: input.scenario }), "string" === typeof input.domain || _report(_exceptionable, { path: _path + ".domain", expected: "string", value: input.domain }), "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: "IAutoBeTestWriteApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeTestWriteApplication.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: "write", parameters: { description: " Complete specification for test generation including scenario,\ndomain, and implementation steps\n\n------------------------------\n\nCurrent Type: {@link IAutoBeTestWriteApplication.IProps}", type: "object", properties: { scenario: { description: "Step 1: Strategic test planning and scenario analysis.\n\nAI analyzes the given test scenario and creates a comprehensive\nimplementation strategy. This planning phase is crucial for generating\nwell-structured, maintainable test code. The AI must define test\nmethodology, data preparation, execution flow, and validation logic\nbefore proceeding to code implementation.\n\nWorkflow: Input scenario \u2192 Strategic analysis \u2192 Detailed test plan", type: "string" }, domain: { description: "Step 2: Functional domain classification for test organization.\n\nAI determines the appropriate domain category based on the scenario\nanalysis. This classification drives file structure, test categorization,\nand logical grouping. The domain must be a single, lowercase word that\nrepresents the primary API resource.\n\nWorkflow: Scenario analysis \u2192 Domain identification \u2192 Test organization\nstructure", type: "string" }, draft: { description: "Step 3: Initial TypeScript E2E test code implementation.\n\nAI generates the first working version of the test code based on the\nstrategic plan. This draft must be compilation-error-free and follow", type: "string" }, review: { description: "Step 4: Code review and quality assessment.\n\nAI performs a thorough review of the draft implementation, examining:\n\n**Compilation & Syntax:**\n\n- TypeScript compilation errors and type mismatches\n- Syntax errors and missing semicolons/brackets\n- Correct function signatures and parameter types\n\n**Framework Compliance:**\n\n-", type: "string" }, final: { description: "Step 5: Final production-ready test code.\n\nAI produces the final, polished version of the test code incorporating\nall review feedback. This code represents the completed test\nimplementation, ready for production deployment. All identified issues\nmust be resolved, and the code must meet the highest quality standards.\n\nWorkflow: Review feedback \u2192 Code refinement \u2192 Production-ready\nimplementation\n\nThis is the ultimate deliverable that will be used in the actual test\nsuite.", type: "string" } }, required: [ "scenario", "domain", "draft", "review", "final" ], additionalProperties: false, $defs: {} }, description: "Main entry point for AI Function Call - generates complete E2E test code.\n\nThe AI executes this function to perform the entire test generation\nworkflow: scenario analysis \u2192 draft implementation \u2192 code review \u2192 final\ncode production. This structured approach ensures high-quality,\ncompilation-error-free test code.", validate: (() => { const _io0 = input => "string" === typeof input.scenario && "string" === typeof input.domain && "string" === typeof input.draft && "string" === typeof input.review && "string" === typeof input.final; const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.scenario || _report(_exceptionable, { path: _path + ".scenario", expected: "string", value: input.scenario }), "string" === typeof input.domain || _report(_exceptionable, { path: _path + ".domain", expected: "string", value: input.domain }), "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: "IAutoBeTestWriteApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeTestWriteApplication.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, "3.0": { model: "3.0", options: { recursive: 3, constraint: true, separate: null }, functions: [ { name: "write", parameters: { type: "object", properties: { scenario: { type: "string", description: "Step 1: Strategic test planning and scenario analysis.\n\nAI analyzes the given test scenario and creates a comprehensive\nimplementation strategy. This planning phase is crucial for generating\nwell-structured, maintainable test code. The AI must define test\nmethodology, data preparation, execution flow, and validation logic\nbefore proceeding to code implementation.\n\nWorkflow: Input scenario \u2192 Strategic analysis \u2192 Detailed test plan" }, domain: { type: "string", description: "Step 2: Functional domain classification for test organization.\n\nAI determines the appropriate domain category based on the scenario\nanalysis. This classification drives file structure, test categorization,\nand logical grouping. The domain must be a single, lowercase word that\nrepresents the primary API resource.\n\nWorkflow: Scenario analysis \u2192 Domain identification \u2192 Test organization\nstructure" }, draft: { type: "string", description: "Step 3: Initial TypeScript E2E test code implementation.\n\nAI generates the first working version of the test code based on the\nstrategic plan. This draft must be compilation-error-free and follow" }, review: { type: "string", description: "Step 4: Code review and quality assessment.\n\nAI performs a thorough review of the draft implementation, examining:\n\n**Compilation & Syntax:**\n\n- TypeScript compilation errors and type mismatches\n- Syntax errors and missing semicolons/brackets\n- Correct function signatures and parameter types\n\n**Framework Compliance:**\n\n-" }, final: { type: "string", description: "Step 5: Final production-ready test code.\n\nAI produces the final, polished version of the test code incorporating\nall review feedback. This code represents the completed test\nimplementation, ready for production deployment. All identified issues\nmust be resolved, and the code must meet the highest quality standards.\n\nWorkflow: Review feedback \u2192 Code refinement \u2192 Production-ready\nimplementation\n\nThis is the ultimate deliverable that will be used in the actual test\nsuite." } }, required: [ "scenario", "domain", "draft", "review", "final" ], description: " Complete specification for test generation including scenario,\ndomain, and implementation steps\n\n------------------------------\n\nCurrent Type: {@link IAutoBeTestWriteApplication.IProps}", additionalProperties: false }, description: "Main entry point for AI Function Call - generates complete E2E test code.\n\nThe AI executes this function to perform the entire test generation\nworkflow: scenario analysis \u2192 draft implementation \u2192 code review \u2192 final\ncode production. This structured approach ensures high-quality,\ncompilation-error-free test code.", validate: (() => { const _io0 = input => "string" === typeof input.scenario && "string" === typeof input.domain && "string" === typeof input.draft && "string" === typeof input.review && "string" === typeof input.final; const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.scenario || _report(_exceptionable, { path: _path + ".scenario", expected: "string", value: input.scenario }), "string" === typeof input.domain || _report(_exceptionable, { path: _path + ".domain", expected: "string", value: input.domain }), "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: "IAutoBeTestWriteApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeTestWriteApplication.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 }; }; })() } ] }, }; //# sourceMappingURL=orchestrateTestWrite.js.map