@autobe/agent
Version:
AI backend server code generator
113 lines (111 loc) • 4.86 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getRealizeWriteCodeTemplate = getRealizeWriteCodeTemplate;
const utils_1 = require("@autobe/utils");
const getRealizeWriteImportStatements_1 = require("./getRealizeWriteImportStatements");
/**
* Generate a TypeScript function template for the Realize Agent to complete
*
* This utility creates a function skeleton with proper imports, parameters, and
* return type that the AI needs to implement. The template includes:
*
* - Import statements for required dependencies
* - Function signature with all parameters
* - Return type specification
*
* @example
* // Returns a template like:
* ```typescript
* import { MyGlobal } from "../MyGlobal";
* // ... other imports
*
* async function post__users_create(props: {
* body: IUserCreateRequest
* }): Promise<IUserResponse> {
* ...
* }
* ```;
*
* @param scenario - The realize scenario containing function metadata
* @param operation - OpenAPI operation definition
* @param authorization - Authorization context if endpoint is authenticated
* @returns Complete TypeScript code template as a formatted string
*/
function getRealizeWriteCodeTemplate(props) {
var _a, _b, _c;
// Collect all function parameters in order
const functionParameters = [];
// Add authentication parameter if needed (e.g., user: IUser, admin: IAdmin)
if (props.authorization && props.authorization.actor.name) {
// Debug: Log the values to check what's being used
const authParameter = `${props.authorization.actor.name}: ${props.authorization.payload.name}`;
functionParameters.push(authParameter);
}
// Add path parameters (e.g., id, postId, etc.)
const pathParameters = props.operation.parameters.map((param) => {
return `${param.name}: ${writeParameterType(param.schema)}`;
});
functionParameters.push(...pathParameters);
// Add request body parameter if present
if ((_a = props.operation.requestBody) === null || _a === void 0 ? void 0 : _a.typeName) {
const bodyParameter = `body: ${props.operation.requestBody.typeName}`;
functionParameters.push(bodyParameter);
}
// Format parameters for props object
const hasParameters = functionParameters.length > 0;
let formattedSignature;
if (hasParameters) {
const propsFields = functionParameters
.map((param) => ` ${param}`)
.join(";\n");
formattedSignature = `props: {\n${propsFields};\n}`;
}
else {
formattedSignature = "";
}
// Determine return type
const returnType = (_c = (_b = props.operation.responseBody) === null || _b === void 0 ? void 0 : _b.typeName) !== null && _c !== void 0 ? _c : "void";
// Generate the complete template
return utils_1.StringUtil.trim `
Complete the code below, disregard the import part and return only the function part.
\`\`\`typescript
${(0, getRealizeWriteImportStatements_1.getRealizeWriteImportStatements)(props).join("\n")}
// DON'T CHANGE FUNCTION NAME AND PARAMETERS,
// ONLY YOU HAVE TO WRITE THIS FUNCTION BODY, AND USE IMPORTED.
export async function ${props.scenario.functionName}(${formattedSignature}): Promise<${returnType}> {
...
}
\`\`\`
`;
}
const writeParameterType = (schema) => {
const elements = schema.type === "integer"
? ["number", `tags.Type<"int32">`]
: [schema.type];
if (schema.type === "number") {
if (schema.minimum !== undefined)
elements.push(`tags.Minimum<${schema.minimum}>`);
if (schema.maximum !== undefined)
elements.push(`tags.Maximum<${schema.maximum}>`);
if (schema.exclusiveMinimum !== undefined)
elements.push(`tags.ExclusiveMinimum<${schema.exclusiveMinimum}>`);
if (schema.exclusiveMaximum !== undefined)
elements.push(`tags.ExclusiveMaximum<${schema.exclusiveMaximum}>`);
if (schema.multipleOf !== undefined)
elements.push(`tags.MultipleOf<${schema.multipleOf}>`);
}
else if (schema.type === "string") {
if (schema.format !== undefined)
elements.push(`tags.Format<${JSON.stringify(schema.format)}>`);
if (schema.contentMediaType !== undefined)
elements.push(`tags.ContentMediaType<${JSON.stringify(schema.contentMediaType)}>`);
if (schema.pattern !== undefined)
elements.push(`tags.Pattern<${JSON.stringify(schema.pattern)}>`);
if (schema.minLength !== undefined)
elements.push(`tags.MinLength<${schema.minLength}>`);
if (schema.maxLength !== undefined)
elements.push(`tags.MaxLength<${schema.maxLength}>`);
}
return elements.join(" & ");
};
//# sourceMappingURL=getRealizeWriteCodeTemplate.js.map