UNPKG

@autobe/agent

Version:

AI backend server code generator

361 lines 21 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.orchestrateTestGenerateWrite = void 0; const __typia_transform__validateReport = __importStar(require("typia/lib/internal/_validateReport")); const __typia_transform__llmApplicationFinalize = __importStar(require("typia/lib/internal/_llmApplicationFinalize")); const utils_1 = require("@autobe/utils"); const tstl_1 = require("tstl"); const typia_1 = __importDefault(require("typia")); const uuid_1 = require("uuid"); const executeCachedBatch_1 = require("../../utils/executeCachedBatch"); const forceRetry_1 = require("../../utils/forceRetry"); const validateEmptyCode_1 = require("../../utils/validateEmptyCode"); const getTestArtifacts_1 = require("./compile/getTestArtifacts"); const transformTestGenerationWriteHistory_1 = require("./histories/transformTestGenerationWriteHistory"); const AutoBeTestGenerateProgrammer_1 = require("./programmers/AutoBeTestGenerateProgrammer"); const orchestrateTestGenerateWrite = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () { const result = yield (0, executeCachedBatch_1.executeCachedBatch)(ctx, props.document.operations.map((operation) => (promptCacheKey) => __awaiter(void 0, void 0, void 0, function* () { if (operation.requestBody === null) return null; else if (operation.requestBody.typeName.endsWith(".ICreate") === false) return null; else if (props.document.components.schemas[operation.requestBody.typeName] === undefined) return null; else if (utils_1.AutoBeOpenApiTypeChecker.isObject(props.document.components.schemas[operation.requestBody.typeName]) === false) return null; const prepareFunction = props.prepares.find((pf) => { var _a; return pf.typeName === ((_a = operation.requestBody) === null || _a === void 0 ? void 0 : _a.typeName); }); if (prepareFunction === undefined) return null; const artifacts = yield (0, getTestArtifacts_1.getTestArtifacts)(ctx, { endpoint: { path: operation.path, method: operation.method, }, }); const counter = new tstl_1.Singleton(() => ++props.progress.completed); try { return yield (0, forceRetry_1.forceRetry)(() => __awaiter(void 0, void 0, void 0, function* () { const event = yield process(ctx, { prepare: prepareFunction, artifacts, operation, progress: props.progress, counter, promptCacheKey, instruction: props.instruction, }); if (event.function.type !== "generate") return null; ctx.dispatch(event); return { type: "generate", prepare: prepareFunction, artifacts, function: event.function, operation, }; })); } catch (_a) { counter.get(); return null; } }))); return result.filter((r) => r !== null); }); exports.orchestrateTestGenerateWrite = orchestrateTestGenerateWrite; function process(ctx, props) { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c; const functionName = AutoBeTestGenerateProgrammer_1.AutoBeTestGenerateProgrammer.getFunctionName(props.operation); const pointer = { value: null, }; const { metric, tokenUsage } = yield ctx.conversate(Object.assign({ source: "testWrite", controller: createController({ functionName, build: (next) => { pointer.value = next; }, }), enforceFunctionCall: true, promptCacheKey: props.promptCacheKey }, (yield (0, transformTestGenerationWriteHistory_1.transformTestGenerateWriteHistory)(ctx, { instruction: props.instruction, prepare: props.prepare, operation: props.operation, artifacts: props.artifacts, })))); if (pointer.value === null) { props.counter.get(); throw new Error("Failed to create generation function."); } const location = `test/generate/${functionName}.ts`; return { type: "testWrite", id: (0, uuid_1.v7)(), created_at: new Date().toISOString(), function: { type: "generate", endpoint: { method: props.operation.method, path: props.operation.path, }, actor: props.operation.authorizationActor, location, name: functionName, content: yield AutoBeTestGenerateProgrammer_1.AutoBeTestGenerateProgrammer.replaceImportStatements({ compiler: yield ctx.compiler(), artifacts: props.artifacts, prepare: props.prepare, location, content: (_a = pointer.value.revise.final) !== null && _a !== void 0 ? _a : pointer.value.draft, }), }, metric, tokenUsage, completed: props.counter.get(), total: props.progress.total, step: (_c = (_b = ctx.state().test) === null || _b === void 0 ? void 0 : _b.step) !== null && _c !== void 0 ? _c : 0, }; }); } function createController(props) { const validate = (input) => { const result = (() => { const _io0 = input => "string" === typeof input.think && "string" === typeof input.draft && ("object" === typeof input.revise && null !== input.revise && _io1(input.revise)); const _io1 = input => "string" === typeof input.review && (null === input.final || "string" === typeof input.final); const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.think || _report(_exceptionable, { path: _path + ".think", expected: "string", value: input.think }), "string" === typeof input.draft || _report(_exceptionable, { path: _path + ".draft", expected: "string", value: input.draft }), ("object" === typeof input.revise && null !== input.revise || _report(_exceptionable, { path: _path + ".revise", expected: "IAutoBeTestGenerationWriteApplication.IReviseProps", value: input.revise })) && _vo1(input.revise, _path + ".revise", true && _exceptionable) || _report(_exceptionable, { path: _path + ".revise", expected: "IAutoBeTestGenerationWriteApplication.IReviseProps", value: input.revise })].every(flag => flag); const _vo1 = (input, _path, _exceptionable = true) => ["string" === typeof input.review || _report(_exceptionable, { path: _path + ".review", expected: "string", value: input.review }), null === input.final || "string" === typeof input.final || _report(_exceptionable, { path: _path + ".final", expected: "(null | 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: "IAutoBeTestGenerationWriteApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeTestGenerationWriteApplication.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 }; }; })()(input); if (result.success === false) return result; const errors = (0, validateEmptyCode_1.validateEmptyCode)({ name: props.functionName, draft: result.data.draft, revise: result.data.revise, path: "$input", asynchronous: true, }); return errors.length ? { success: false, errors, data: result.data, } : result; }; const application = __typia_transform__llmApplicationFinalize._llmApplicationFinalize({ functions: [ { name: "generate", parameters: { description: "Current Type: {@link IAutoBeTestGenerationWriteApplication.IProps}", type: "object", properties: { think: { description: "Step 1: Strategic analysis and planning.\n\nAI analyzes the prepare function and corresponding API operation to\nunderstand what resource needs to be generated and how. This analysis\nincludes understanding the prepare function's purpose, the API endpoint's\nrequirements, and the relationship between input data and output\nresources.\n\nThe analysis should identify:\n\n- What resource type is being created\n- What SDK function to use for creation\n- What input parameters the prepare function accepts\n- How to use the prepare function output for API call\n- Function naming strategy based on prepare function\n\nWorkflow: Prepare function + Operation \u2192 Strategic analysis \u2192\nImplementation plan", type: "string" }, draft: { description: "Step 2: Initial TypeScript generation function implementation.\n\nAI generates the first working version of the generation function that:\n\n1. Accepts connection, optional input parameters (using the same DeepPartial\n type as prepare function), and optional URL parameters\n2. Calls the prepare function to create test data\n3. Uses the SDK to create the actual resource via API\n4. Returns the created resource\n\nThe function must handle:\n\n- Proper typing with response type from operation.responseBody.typeName\n- Correct import statements for types and functions\n- Import path must use operation.responseBody.typeName exactly\n- Passing input parameters to prepare function\n- Handling URL parameters when required by the operation\n- Error handling and async/await patterns\n\nCritical: Include ALL import statements required for the function\n\nWorkflow: Strategic plan \u2192 TypeScript implementation \u2192 Functional\ngeneration code", type: "string" }, revise: { description: "Steps 3-4: Code review and final refinement process.\n\nContains the iterative improvement workflow that transforms the initial\ndraft into production-ready generation function code. The review phase\nidentifies issues to fix, followed by the final phase that produces the\npolished implementation.\n\nWorkflow: Draft \u2192 Review analysis \u2192 Final implementation", $ref: "#/$defs/IAutoBeTestGenerationWriteApplication.IReviseProps" } }, required: [ "think", "draft", "revise" ], additionalProperties: false, $defs: { "IAutoBeTestGenerationWriteApplication.IReviseProps": { type: "object", properties: { review: { description: "Step 3: Code review and quality assessment.\n\nAI performs a thorough review of the draft implementation for:\n\n**Compilation & Syntax:**\n\n- TypeScript compilation errors and type mismatches\n- Correct import statements and module references\n- Proper function signatures and parameter types\n- Import path format for types (must be", type: "string" }, final: { description: "Step 4: Final production-ready generation function code.\n\nAI produces the final, polished version of the generation function\nincorporating all review feedback. This code represents the completed\nimplementation, ready for use in test scenarios. When the draft code is\nalready perfect with no issues found during review, this value can be\nnull, indicating no revisions were necessary.\n\nAll identified issues must be resolved, and the code must meet the\nhighest quality standards. A null value indicates the draft code already\nmeets all requirements without modification.\n\nWorkflow: Review feedback \u2192 Apply fixes \u2192 Production-ready implementation\n(or null if no changes needed)\n\nThis is the ultimate deliverable that will be used in the test generation\nsystem when provided, otherwise the draft is used as-is.", anyOf: [ { type: "null" }, { type: "string" } ] } }, required: [ "review", "final" ] } } }, description: "Main entry point for AI Function Call - generates resource generation\nfunction.\n\nThe AI executes this function to create a generation function that produces\ntest data resources for E2E testing. The generation function uses prepare\nfunctions to create valid test data and calls the appropriate API to create\nactual resources.", validate: (() => { const _io0 = input => "string" === typeof input.think && "string" === typeof input.draft && ("object" === typeof input.revise && null !== input.revise && _io1(input.revise)); const _io1 = input => "string" === typeof input.review && (null === input.final || "string" === typeof input.final); const _vo0 = (input, _path, _exceptionable = true) => ["string" === typeof input.think || _report(_exceptionable, { path: _path + ".think", expected: "string", value: input.think }), "string" === typeof input.draft || _report(_exceptionable, { path: _path + ".draft", expected: "string", value: input.draft }), ("object" === typeof input.revise && null !== input.revise || _report(_exceptionable, { path: _path + ".revise", expected: "IAutoBeTestGenerationWriteApplication.IReviseProps", value: input.revise })) && _vo1(input.revise, _path + ".revise", true && _exceptionable) || _report(_exceptionable, { path: _path + ".revise", expected: "IAutoBeTestGenerationWriteApplication.IReviseProps", value: input.revise })].every(flag => flag); const _vo1 = (input, _path, _exceptionable = true) => ["string" === typeof input.review || _report(_exceptionable, { path: _path + ".review", expected: "string", value: input.review }), null === input.final || "string" === typeof input.final || _report(_exceptionable, { path: _path + ".final", expected: "(null | 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: "IAutoBeTestGenerationWriteApplication.IProps", value: input })) && _vo0(input, _path + "", true) || _report(true, { path: _path + "", expected: "IAutoBeTestGenerationWriteApplication.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 }; }; })() } ] }, { validate: { generate: validate, }, }); return { protocol: "class", name: "testGenerationWrite", application, execute: { generate: (next) => { props.build(next); }, }, }; } //# sourceMappingURL=orchestrateTestGenerateWrite.js.map