@autobe/agent
Version:
AI backend server code generator
501 lines • 31.4 kB
JavaScript
"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.orchestrateRealizeAuthorization = orchestrateRealizeAuthorization;
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 orchestrateRealizeAuthorizationCorrect_1 = require("./orchestrateRealizeAuthorizationCorrect");
const transformRealizeAuthorization_1 = require("./transformRealizeAuthorization");
const AuthorizationFileSystem_1 = require("./utils/AuthorizationFileSystem");
const InternalFileSystem_1 = require("./utils/InternalFileSystem");
/**
* 1. Create decorator and its parameters. and design the Authorization Provider.
* 2. According to Authorization Provider design, create the Provider.
*
* @param ctx
*/
function orchestrateRealizeAuthorization(ctx) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f;
const roles = (_b = (_a = ctx.state().analyze) === null || _a === void 0 ? void 0 : _a.roles.map((role) => role.name)) !== null && _b !== void 0 ? _b : [];
let completed = 0;
const templateFiles = yield (yield ctx.compiler()).realize.getTemplate();
ctx.dispatch({
type: "realizeAuthorizationStart",
step: (_d = (_c = ctx.state().test) === null || _c === void 0 ? void 0 : _c.step) !== null && _d !== void 0 ? _d : 0,
created_at: new Date().toISOString(),
});
const authorizations = yield Promise.all(roles.map((role) => __awaiter(this, void 0, void 0, function* () {
var _a, _b;
const authorization = yield process(ctx, role, InternalFileSystem_1.InternalFileSystem.DEFAULT.map((el) => ({
[el]: templateFiles[el],
})).reduce((acc, cur) => Object.assign(acc, cur), {}));
ctx.dispatch({
type: "realizeAuthorizationWrite",
created_at: new Date().toISOString(),
authorization: authorization,
completed: ++completed,
total: roles.length,
step: (_b = (_a = ctx.state().test) === null || _a === void 0 ? void 0 : _a.step) !== null && _b !== void 0 ? _b : 0,
});
return authorization;
})));
ctx.dispatch({
type: "realizeAuthorizationComplete",
created_at: new Date().toISOString(),
step: (_f = (_e = ctx.state().test) === null || _e === void 0 ? void 0 : _e.step) !== null && _f !== void 0 ? _f : 0,
});
return authorizations;
});
}
function process(ctx, role, templateFiles) {
return __awaiter(this, void 0, void 0, function* () {
var _a, _b;
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,
} }),
histories: (0, transformRealizeAuthorization_1.transformRealizeAuthorizationHistories)(ctx, role),
controllers: [
createApplication({
model: ctx.model,
build: (next) => {
pointer.value = next;
},
}),
],
});
(0, enforceToolCall_1.enforceToolCall)(agentica);
yield agentica
.conversate("Create Authorization Provider and Decorator.")
.finally(() => {
const tokenUsage = agentica.getTokenUsage();
ctx.usage().record(tokenUsage, ["realize"]);
});
if (pointer.value === null)
throw new Error("Failed to create decorator.");
const compiler = yield ctx.compiler();
const authorization = {
role,
decorator: {
location: AuthorizationFileSystem_1.AuthorizationFileSystem.decoratorPath(pointer.value.decorator.name),
name: pointer.value.decorator.name,
content: pointer.value.decorator.content,
},
payload: {
location: AuthorizationFileSystem_1.AuthorizationFileSystem.payloadPath(pointer.value.payload.name),
name: pointer.value.payload.name,
content: yield compiler.typescript.beautify(pointer.value.payload.content),
},
provider: {
location: AuthorizationFileSystem_1.AuthorizationFileSystem.providerPath(pointer.value.provider.name),
name: pointer.value.provider.name,
content: pointer.value.provider.content,
},
};
const compiled = (_b = ctx.state().prisma) === null || _b === void 0 ? void 0 : _b.compiled;
const prismaClients = (compiled === null || compiled === void 0 ? void 0 : compiled.type) === "success" ? compiled.nodeModules : {};
return (0, orchestrateRealizeAuthorizationCorrect_1.orchestrateRealizeAuthorizationCorrect)(ctx, authorization, prismaClients, templateFiles);
});
}
function createApplication(props) {
(0, assertSchemaModel_1.assertSchemaModel)(props.model);
const application = collection[props.model];
return {
protocol: "class",
name: "Create Decorator",
application,
execute: {
createDecorator: (next) => {
props.build(next);
},
},
};
}
const claude = {
model: "claude",
options: {
reference: true,
separate: null
},
functions: [
{
name: "createDecorator",
parameters: {
description: "Current Type: {@link IAutoBeRealizeAuthorizationApplication.IProps}",
type: "object",
properties: {
provider: {
description: "Authentication Provider function configuration containing the function\nname and implementation code. The Provider handles JWT token\nverification, role validation, and database queries to authenticate\nusers.",
$ref: "#/$defs/IAutoBeRealizeAuthorizationApplication.IProvider"
},
decorator: {
description: "Authentication Decorator configuration containing the decorator name and\nimplementation code. The Decorator integrates with NestJS parameter\ndecorators to automatically inject authenticated user data into\nController methods.",
$ref: "#/$defs/IAutoBeRealizeAuthorizationApplication.IDecorator"
},
payload: {
description: "Authentication Payload Type configuration containing the payload type\nname and implementation code. The Payload Type is used to define the\nstructure of the authenticated user data that will be injected into\nController methods when using the decorator. It serves as the TypeScript\ntype for the parameter in Controller method signatures.",
$ref: "#/$defs/IAutoBeRealizeAuthorizationApplication.IPayloadType"
}
},
required: [
"provider",
"decorator",
"payload"
],
additionalProperties: false,
$defs: {
"IAutoBeRealizeAuthorizationApplication.IProvider": {
type: "object",
properties: {
name: {
description: "The name of the authentication Provider function in {role}Authorize\nformat (e.g., adminAuthorize, userAuthorize). This function will be\ncalled by the decorator to verify JWT tokens and return authenticated\nuser information for the specified role.",
type: "string"
},
content: {
description: "Complete TypeScript code for the authentication Provider function. Must\ninclude: JWT token verification using jwtAuthorize function, role type\nchecking against payload.type, database query using\nMyGlobal.prisma.{tableName} pattern to verify user existence, and proper\nerror handling with ForbiddenException and UnauthorizedException. The\nfunction should return the authenticated user payload data.",
type: "string"
}
},
required: [
"name",
"content"
]
},
"IAutoBeRealizeAuthorizationApplication.IDecorator": {
type: "object",
properties: {
name: {
description: "The name of the Decorator to be generated in {Role}Auth format (e.g.,\nAdminAuth, UserAuth). This decorator will be used as a parameter\ndecorator in Controller methods to automatically authenticate and\nauthorize users for the specific role, injecting the authenticated user\npayload as a method parameter.",
type: "string"
},
content: {
description: "Complete TypeScript code for the authentication Decorator implementation.\nMust include: SwaggerCustomizer integration to add bearer token security\nschema to API documentation, createParamDecorator implementation that\ncalls the corresponding Provider function for authentication, Singleton\npattern using tstl library for efficient decorator instance management,\nand proper TypeScript typing for the ParameterDecorator interface.",
type: "string"
}
},
required: [
"name",
"content"
]
},
"IAutoBeRealizeAuthorizationApplication.IPayloadType": {
type: "object",
properties: {
name: {
description: "The name of the Payload type to be generated in {Role}Payload format\n(e.g., AdminPayload, UserPayload). This type defines the structure of the\nauthenticated user data that will be injected into Controller methods\nwhen using the decorator.",
type: "string"
},
content: {
description: "Complete TypeScript code for the Payload type interface in {Role}Payload\nformat (e.g., AdminPayload, UserPayload). Must include: id field with\nUUID format validation, type field as role discriminator, and proper\ntypia tags for validation. This interface defines the structure of the\nauthenticated user data that will be injected into Controller methods\nwhen using the decorator and serves as the TypeScript type for the\nparameter in Controller method signatures.",
type: "string"
}
},
required: [
"name",
"content"
]
}
}
},
validate: (() => { const _io0 = input => "object" === typeof input.provider && null !== input.provider && _io1(input.provider) && ("object" === typeof input.decorator && null !== input.decorator && _io2(input.decorator)) && ("object" === typeof input.payload && null !== input.payload && _io3(input.payload)); const _io1 = input => "string" === typeof input.name && "string" === typeof input.content; const _io2 = input => "string" === typeof input.name && "string" === typeof input.content; const _io3 = input => "string" === typeof input.name && "string" === typeof input.content; const _vo0 = (input, _path, _exceptionable = true) => [("object" === typeof input.provider && null !== input.provider || _report(_exceptionable, {
path: _path + ".provider",
expected: "IAutoBeRealizeAuthorizationApplication.IProvider",
value: input.provider
})) && _vo1(input.provider, _path + ".provider", true && _exceptionable) || _report(_exceptionable, {
path: _path + ".provider",
expected: "IAutoBeRealizeAuthorizationApplication.IProvider",
value: input.provider
}), ("object" === typeof input.decorator && null !== input.decorator || _report(_exceptionable, {
path: _path + ".decorator",
expected: "IAutoBeRealizeAuthorizationApplication.IDecorator",
value: input.decorator
})) && _vo2(input.decorator, _path + ".decorator", true && _exceptionable) || _report(_exceptionable, {
path: _path + ".decorator",
expected: "IAutoBeRealizeAuthorizationApplication.IDecorator",
value: input.decorator
}), ("object" === typeof input.payload && null !== input.payload || _report(_exceptionable, {
path: _path + ".payload",
expected: "IAutoBeRealizeAuthorizationApplication.IPayloadType",
value: input.payload
})) && _vo3(input.payload, _path + ".payload", true && _exceptionable) || _report(_exceptionable, {
path: _path + ".payload",
expected: "IAutoBeRealizeAuthorizationApplication.IPayloadType",
value: input.payload
})].every(flag => flag); const _vo1 = (input, _path, _exceptionable = true) => ["string" === typeof input.name || _report(_exceptionable, {
path: _path + ".name",
expected: "string",
value: input.name
}), "string" === typeof input.content || _report(_exceptionable, {
path: _path + ".content",
expected: "string",
value: input.content
})].every(flag => flag); const _vo2 = (input, _path, _exceptionable = true) => ["string" === typeof input.name || _report(_exceptionable, {
path: _path + ".name",
expected: "string",
value: input.name
}), "string" === typeof input.content || _report(_exceptionable, {
path: _path + ".content",
expected: "string",
value: input.content
})].every(flag => flag); const _vo3 = (input, _path, _exceptionable = true) => ["string" === typeof input.name || _report(_exceptionable, {
path: _path + ".name",
expected: "string",
value: input.name
}), "string" === typeof input.content || _report(_exceptionable, {
path: _path + ".content",
expected: "string",
value: input.content
})].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: "IAutoBeRealizeAuthorizationApplication.IProps",
value: input
})) && _vo0(input, _path + "", true) || _report(true, {
path: _path + "",
expected: "IAutoBeRealizeAuthorizationApplication.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: "createDecorator",
parameters: {
description: "Current Type: {@link IAutoBeRealizeAuthorizationApplication.IProps}\n\n### Description of {@link provider} property:\n\n> Authentication Provider function configuration containing the function\n> name and implementation code. The Provider handles JWT token\n> verification, role validation, and database queries to authenticate\n> users.\n\n### Description of {@link decorator} property:\n\n> Authentication Decorator configuration containing the decorator name and\n> implementation code. The Decorator integrates with NestJS parameter\n> decorators to automatically inject authenticated user data into\n> Controller methods.\n\n### Description of {@link payload} property:\n\n> Authentication Payload Type configuration containing the payload type\n> name and implementation code. The Payload Type is used to define the\n> structure of the authenticated user data that will be injected into\n> Controller methods when using the decorator. It serves as the TypeScript\n> type for the parameter in Controller method signatures.",
type: "object",
properties: {
provider: {
$ref: "#/$defs/IAutoBeRealizeAuthorizationApplication.IProvider"
},
decorator: {
$ref: "#/$defs/IAutoBeRealizeAuthorizationApplication.IDecorator"
},
payload: {
$ref: "#/$defs/IAutoBeRealizeAuthorizationApplication.IPayloadType"
}
},
required: [
"provider",
"decorator",
"payload"
],
additionalProperties: false,
$defs: {
"IAutoBeRealizeAuthorizationApplication.IProvider": {
type: "object",
properties: {
name: {
description: "The name of the authentication Provider function in {role}Authorize\nformat (e.g., adminAuthorize, userAuthorize). This function will be\ncalled by the decorator to verify JWT tokens and return authenticated\nuser information for the specified role.",
type: "string"
},
content: {
description: "Complete TypeScript code for the authentication Provider function. Must\ninclude: JWT token verification using jwtAuthorize function, role type\nchecking against payload.type, database query using\nMyGlobal.prisma.{tableName} pattern to verify user existence, and proper\nerror handling with ForbiddenException and UnauthorizedException. The\nfunction should return the authenticated user payload data.",
type: "string"
}
},
required: [
"name",
"content"
]
},
"IAutoBeRealizeAuthorizationApplication.IDecorator": {
type: "object",
properties: {
name: {
description: "The name of the Decorator to be generated in {Role}Auth format (e.g.,\nAdminAuth, UserAuth). This decorator will be used as a parameter\ndecorator in Controller methods to automatically authenticate and\nauthorize users for the specific role, injecting the authenticated user\npayload as a method parameter.",
type: "string"
},
content: {
description: "Complete TypeScript code for the authentication Decorator implementation.\nMust include: SwaggerCustomizer integration to add bearer token security\nschema to API documentation, createParamDecorator implementation that\ncalls the corresponding Provider function for authentication, Singleton\npattern using tstl library for efficient decorator instance management,\nand proper TypeScript typing for the ParameterDecorator interface.",
type: "string"
}
},
required: [
"name",
"content"
]
},
"IAutoBeRealizeAuthorizationApplication.IPayloadType": {
type: "object",
properties: {
name: {
description: "The name of the Payload type to be generated in {Role}Payload format\n(e.g., AdminPayload, UserPayload). This type defines the structure of the\nauthenticated user data that will be injected into Controller methods\nwhen using the decorator.",
type: "string"
},
content: {
description: "Complete TypeScript code for the Payload type interface in {Role}Payload\nformat (e.g., AdminPayload, UserPayload). Must include: id field with\nUUID format validation, type field as role discriminator, and proper\ntypia tags for validation. This interface defines the structure of the\nauthenticated user data that will be injected into Controller methods\nwhen using the decorator and serves as the TypeScript type for the\nparameter in Controller method signatures.",
type: "string"
}
},
required: [
"name",
"content"
]
}
}
},
validate: (() => { const _io0 = input => "object" === typeof input.provider && null !== input.provider && _io1(input.provider) && ("object" === typeof input.decorator && null !== input.decorator && _io2(input.decorator)) && ("object" === typeof input.payload && null !== input.payload && _io3(input.payload)); const _io1 = input => "string" === typeof input.name && "string" === typeof input.content; const _io2 = input => "string" === typeof input.name && "string" === typeof input.content; const _io3 = input => "string" === typeof input.name && "string" === typeof input.content; const _vo0 = (input, _path, _exceptionable = true) => [("object" === typeof input.provider && null !== input.provider || _report(_exceptionable, {
path: _path + ".provider",
expected: "IAutoBeRealizeAuthorizationApplication.IProvider",
value: input.provider
})) && _vo1(input.provider, _path + ".provider", true && _exceptionable) || _report(_exceptionable, {
path: _path + ".provider",
expected: "IAutoBeRealizeAuthorizationApplication.IProvider",
value: input.provider
}), ("object" === typeof input.decorator && null !== input.decorator || _report(_exceptionable, {
path: _path + ".decorator",
expected: "IAutoBeRealizeAuthorizationApplication.IDecorator",
value: input.decorator
})) && _vo2(input.decorator, _path + ".decorator", true && _exceptionable) || _report(_exceptionable, {
path: _path + ".decorator",
expected: "IAutoBeRealizeAuthorizationApplication.IDecorator",
value: input.decorator
}), ("object" === typeof input.payload && null !== input.payload || _report(_exceptionable, {
path: _path + ".payload",
expected: "IAutoBeRealizeAuthorizationApplication.IPayloadType",
value: input.payload
})) && _vo3(input.payload, _path + ".payload", true && _exceptionable) || _report(_exceptionable, {
path: _path + ".payload",
expected: "IAutoBeRealizeAuthorizationApplication.IPayloadType",
value: input.payload
})].every(flag => flag); const _vo1 = (input, _path, _exceptionable = true) => ["string" === typeof input.name || _report(_exceptionable, {
path: _path + ".name",
expected: "string",
value: input.name
}), "string" === typeof input.content || _report(_exceptionable, {
path: _path + ".content",
expected: "string",
value: input.content
})].every(flag => flag); const _vo2 = (input, _path, _exceptionable = true) => ["string" === typeof input.name || _report(_exceptionable, {
path: _path + ".name",
expected: "string",
value: input.name
}), "string" === typeof input.content || _report(_exceptionable, {
path: _path + ".content",
expected: "string",
value: input.content
})].every(flag => flag); const _vo3 = (input, _path, _exceptionable = true) => ["string" === typeof input.name || _report(_exceptionable, {
path: _path + ".name",
expected: "string",
value: input.name
}), "string" === typeof input.content || _report(_exceptionable, {
path: _path + ".content",
expected: "string",
value: input.content
})].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: "IAutoBeRealizeAuthorizationApplication.IProps",
value: input
})) && _vo0(input, _path + "", true) || _report(true, {
path: _path + "",
expected: "IAutoBeRealizeAuthorizationApplication.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=orchestrateRealizeAuthorization.js.map