@autobe/agent
Version:
AI backend server code generator
118 lines (90 loc) • 9.6 kB
JavaScript
"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.transformTestGenerateWriteHistory = transformTestGenerateWriteHistory;
const utils_1 = require("@autobe/utils");
const uuid_1 = require("uuid");
const getTestExternalDeclarations_1 = require("../compile/getTestExternalDeclarations");
const AutoBeTestGenerateProgrammer_1 = require("../programmers/AutoBeTestGenerateProgrammer");
const transformTestOperationWriteHistory_1 = require("./transformTestOperationWriteHistory");
function transformTestGenerateWriteHistory(ctx, props) {
return __awaiter(this, void 0, void 0, function* () {
return {
histories: [
{
id: (0, uuid_1.v7)(),
created_at: new Date().toISOString(),
type: "systemMessage",
text: "<!--\nfilename: TEST_GENERATE_WRITE.md\n-->\n# Test Generation Function Agent\n\nYou are the **Test Generation Function Agent**, creating resource generation functions for E2E testing.\n\n**Function calling is MANDATORY** - call the provided function immediately.\n\n## 1. Input Materials\n\n1. **Prepare Function**: Function that creates test data (you MUST use this)\n2. **API Operation**: Target endpoint with method, path, request/response types, parameters\n3. **DTO Types**: Data transfer object type definitions\n4. **SDK Functions**: Available API SDK functions with accessors\n5. **Template Code**: Pre-defined function signature (match exactly)\n\n## 2. Three-Step Workflow\n\n### 2.1. think - Analysis\n\n- Analyze prepare function's input type (DeepPartial<T>)\n- Identify API operation's response type from `operation.responseBody.typeName`\n- Determine SDK function accessor\n- Check if URL parameters are required\n\n### 2.2. draft - Implementation\n\nGenerate complete TypeScript function following the template.\n\n### 2.3. revise - Review\n\n- `revise.review`: Check compilation, types, SDK accessor\n- `revise.final`: Apply fixes or `null` if draft is perfect\n\n## 3. Function Signature\n\n```typescript\nexport async function generate_random_{resource}(\n connection: api.IConnection,\n props: {\n body?: DeepPartial<{ResourceType}.ICreate>,\n params?: { articleId: string } // If URL parameters needed\n }\n): Promise<{ResponseType}> {\n const prepared: {ResourceType}.ICreate = prepare_random_{resource}(props.body);\n const result: {ResponseType} = await api.functional.{accessor}(\n connection,\n {\n articleId: props.params?.articleId, // URL params if needed\n body: prepared,\n },\n );\n return result;\n}\n```\n\n## 4. Critical Rules\n\n### 4.1. Function Declaration Syntax\n\n```typescript\n// \u2705 CORRECT\nexport async function generate_random_user(...): Promise<IUser> { ... }\n\n// \u274C WRONG - Arrow functions, namespace/class wrapping\nexport const generate_random_user = async (...) => { ... };\nexport namespace X { export async function generate_random_user(...) { ... } }\n```\n\n### 4.2. Immutability\n\n- **ALWAYS use `const`**, never `let`\n- Use ternary or IIFE for conditional logic\n\n### 4.3. No Try-Catch\n\nLet API errors propagate naturally. No error wrapping.\n\n```typescript\n// \u2705 CORRECT\nconst result = await api.functional.users.create(connection, { body: prepared });\nreturn result;\n\n// \u274C WRONG\ntry {\n const result = await api.functional.users.create(connection, { body: prepared });\n return result;\n} catch (error) {\n throw error; // Pointless\n}\n```\n\n### 4.4. Type Matching\n\n- Input type: Same as prepare function's DeepPartial type\n- Return type: EXACTLY `operation.responseBody.typeName`\n\n### 4.5. ALWAYS Use Prepare Function\n\nNever generate data inline. Always call `prepare_random_{resource}(props.body)`.\n\n## 5. JSDoc Comment Style (CRITICAL)\n\nEvery generation function MUST have a JSDoc comment. Style: **summary sentence first, `\\n\\n`, then paragraphs grouped by topic**.\n\n---\n\n## 6. Examples\n\n### 6.1. Without URL Parameters\n\n```typescript\n/**\n * Generate a random article via the API for E2E testing.\n *\n * Prepares random article data using the prepare function, then calls the creation endpoint. ...\n */\nexport async function generate_random_article(\n connection: api.IConnection,\n props: { body?: DeepPartial<IArticle.ICreate> }\n): Promise<IArticle> {\n const prepared: IArticle.ICreate = prepare_random_article(props.body);\n const result: IArticle = await api.functional.articles.create(\n connection,\n { body: prepared },\n );\n return result;\n}\n```\n\n### 6.2. With URL Parameters\n\n```typescript\n/**\n * Generate a random comment on an existing article for E2E testing.\n *\n * Creates a comment attached to the article specified by articleId. ...\n */\nexport async function generate_random_comment(\n connection: api.IConnection,\n props: {\n body?: DeepPartial<IComment.ICreate>,\n params?: { articleId: string },\n }\n): Promise<IComment> {\n const prepared: IComment.ICreate = prepare_random_comment(props.body);\n const result: IComment = await api.functional.articles.comments.create(\n connection,\n {\n articleId: props.params?.articleId,\n body: prepared,\n },\n );\n return result;\n}\n```\n\n## 7. Note\n\nAuthentication is handled separately in test scenarios, not in generation functions." /* AutoBeSystemPromptConstant.TEST_GENERATE_WRITE */,
},
{
id: (0, uuid_1.v7)(),
created_at: new Date().toISOString(),
type: "assistantMessage",
text: utils_1.StringUtil.trim `
Here is the list of input material composition.
Generate a resource generation function based on the following information.
## Instructions
The following instructions were extracted from the user's requirements
and conversations. These instructions may contain specific guidance about
how generation functions should be implemented, including authentication
patterns, error handling approaches, and data transformation strategies.
Follow these instructions when implementing the generation function.
Carefully distinguish between:
- Suggestions or recommendations (consider these as guidance)
- Direct specifications or explicit commands (these must be followed exactly)
${props.instruction}
## Prepare Function
Here is the prepare function that creates test data for this resource.
Your generation function must use this prepare function to create the input data.
\`\`\`json
${JSON.stringify(props.prepare)}
\`\`\`
## API Operation
Here is the API operation that your generation function will call.
Pay special attention to:
- responseBody.typeName: This is the EXACT type name you must import and return
- endpoint (method and path): To find the matching SDK function
- requestBody.typeName: To understand the input type structure
- parameters: URL path parameters that may be needed for the API call
\`\`\`json
${JSON.stringify(props.operation)}
\`\`\`
## DTO Definitions
These are the DTO type definitions available in the codebase.
Use these to understand the structure of request and response types.
${yield transformTestOperationWriteHistory_1.transformTestOperationWriteHistory.structures(ctx, props.artifacts)}
## API SDK Functions
Here are the available SDK functions you can use to call the API.
Find the appropriate function that matches the operation endpoint.
${transformTestOperationWriteHistory_1.transformTestOperationWriteHistory.functional(props.artifacts, [])}
## E2E Mockup Functions
Just reference, and never follow this code as it is.
\`\`\`json
${JSON.stringify(props.artifacts.e2e)}
\`\`\`
## External Definitions
Here is the external declaration files (d.ts) you can reference.
\`\`\`json
${JSON.stringify(yield (0, getTestExternalDeclarations_1.getTestExternalDeclarations)(ctx))}
\`\`\`
## Template Code
Here is the template code you have to implement.
Reference the template code, and fill the proper code to each section.
${AutoBeTestGenerateProgrammer_1.AutoBeTestGenerateProgrammer.writeTemplateCode(props)}
`,
},
],
userMessage: `Generate the resource generation function based on the prepare function "${props.prepare.name}" and the API operation.`,
};
});
}
//# sourceMappingURL=transformTestGenerationWriteHistory.js.map