UNPKG

@autobe/agent

Version:

AI backend server code generator

231 lines 13 kB
"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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.orchestratePreliminary = void 0; const openapi_1 = require("@samchon/openapi"); const typia_1 = __importDefault(require("typia")); const uuid_1 = require("uuid"); const orchestratePreliminary = (ctx, props) => __awaiter(void 0, void 0, void 0, function* () { const executes = props.histories.filter((h) => h.type === "execute"); if (executes.length === 0) throw new Error("Failed to function calling"); for (const exec of executes) { // ANALYSIS if (isAnalysisFiles(props.preliminary, exec.arguments)) { const pa = props.preliminary; orchestrateAnalyzeFiles(ctx, { source: props.source, source_id: props.source_id, trial: props.trial, all: pa.getAll().analysisFiles, local: pa.getLocal().analysisFiles, arguments: exec.arguments, }); } // PRISMA SCHEMAS else if (isPrismaSchemas(props.preliminary, exec.arguments)) { const pp = props.preliminary; orchestratePrismaSchemas(ctx, { source: props.source, source_id: props.source_id, trial: props.trial, all: pp.getAll().prismaSchemas, local: pp.getLocal().prismaSchemas, arguments: exec.arguments, }); } // INTERFACE OPERATIONS else if (isInterfaceOperations(props.preliminary, exec.arguments)) { const pi = props.preliminary; orchestrateInterfaceOperations(ctx, { source: props.source, source_id: props.source_id, trial: props.trial, all: { operations: pi.getAll().interfaceOperations, schemas: pi.getAll().interfaceSchemas, }, local: { operations: pi.getLocal().interfaceOperations, schemas: pi.getLocal().interfaceSchemas, }, arguments: exec.arguments, }); } // INTERFACE SCHEMAS else if (isInterfaceSchemas(props.preliminary, exec.arguments)) { const ps = props.preliminary; orchestrateInterfaceSchemas(ctx, { source: props.source, source_id: props.source_id, trial: props.trial, all: ps.getAll().interfaceSchemas, local: ps.getLocal().interfaceSchemas, arguments: exec.arguments, }); } } }); exports.orchestratePreliminary = orchestratePreliminary; /* ----------------------------------------------------------- TYPE CHECKERS ----------------------------------------------------------- */ const isAnalysisFiles = (preliminary, input) => (() => { const _io0 = input => "string" === typeof input.thinking && ("object" === typeof input.request && null !== input.request && _io1(input.request)); const _io1 = input => "getAnalysisFiles" === input.type && (Array.isArray(input.fileNames) && (1 <= input.fileNames.length && input.fileNames.every(elem => "string" === typeof elem))); return input => "object" === typeof input && null !== input && _io0(input); })()(input) && preliminary.getAll()[[ "analysisFiles" ][0]] !== undefined; const isPrismaSchemas = (preliminary, input) => (() => { const _io0 = input => "string" === typeof input.thinking && ("object" === typeof input.request && null !== input.request && _io1(input.request)); const _io1 = input => "getPrismaSchemas" === input.type && (Array.isArray(input.schemaNames) && (1 <= input.schemaNames.length && input.schemaNames.every(elem => "string" === typeof elem))); return input => "object" === typeof input && null !== input && _io0(input); })()(input) && preliminary.getAll()[[ "prismaSchemas" ][0]] !== undefined; const isInterfaceOperations = (preliminary, input) => (() => { const _io0 = input => "string" === typeof input.thinking && ("object" === typeof input.request && null !== input.request && _io1(input.request)); const _io1 = input => "getInterfaceOperations" === input.type && (Array.isArray(input.endpoints) && (1 <= input.endpoints.length && input.endpoints.every(elem => "object" === typeof elem && null !== elem && _io2(elem)))); const _io2 = input => "string" === typeof input.path && RegExp("^\\/[a-zA-Z0-9\\/_{}.-]*$").test(input.path) && ("get" === input.method || "post" === input.method || "put" === input.method || "delete" === input.method || "patch" === input.method); return input => "object" === typeof input && null !== input && _io0(input); })()(input) && preliminary.getAll()[[ "interfaceOperations" ][0]] !== undefined; const isInterfaceSchemas = (preliminary, input) => (() => { const _io0 = input => "string" === typeof input.thinking && ("object" === typeof input.request && null !== input.request && _io1(input.request)); const _io1 = input => "getInterfaceSchemas" === input.type && (Array.isArray(input.typeNames) && (1 <= input.typeNames.length && input.typeNames.every(elem => "string" === typeof elem))); return input => "object" === typeof input && null !== input && _io0(input); })()(input) && preliminary.getAll()[[ "interfaceSchemas" ][0]] !== undefined; /* ----------------------------------------------------------- ORCHESTRATORS ----------------------------------------------------------- */ const orchestrateAnalyzeFiles = (ctx, props) => { if ((() => { const _io0 = input => "string" === typeof input.thinking && ("object" === typeof input.request && null !== input.request && _io1(input.request)); const _io1 = input => "getAnalysisFiles" === input.type && (Array.isArray(input.fileNames) && (1 <= input.fileNames.length && input.fileNames.every(elem => "string" === typeof elem))); return input => "object" === typeof input && null !== input && _io0(input); })()(props.arguments) === false) return; // unreachable const existing = props.local.map((f) => f.filename); for (const filename of props.arguments.request.fileNames) { const file = props.all.find((f) => f.filename === filename); if (file === undefined) continue; else if (props.local.find((x) => x.filename === filename) === undefined) props.local.push(file); } ctx.dispatch({ type: "preliminary", id: (0, uuid_1.v7)(), function: "analysisFiles", source: props.source, source_id: props.source_id, existing, requested: props.arguments.request.fileNames, trial: props.trial, created_at: new Date().toISOString(), }); }; const orchestratePrismaSchemas = (ctx, props) => { if ((() => { const _io0 = input => "string" === typeof input.thinking && ("object" === typeof input.request && null !== input.request && _io1(input.request)); const _io1 = input => "getPrismaSchemas" === input.type && (Array.isArray(input.schemaNames) && (1 <= input.schemaNames.length && input.schemaNames.every(elem => "string" === typeof elem))); return input => "object" === typeof input && null !== input && _io0(input); })()(props.arguments) === false) return; // unreachable const existing = props.local.map((m) => m.name); for (const name of props.arguments.request.schemaNames) { const model = props.all.find((m) => m.name === name); if (model === undefined) continue; else if (props.local.find((m) => m.name === name) === undefined) props.local.push(model); } ctx.dispatch({ type: "preliminary", id: (0, uuid_1.v7)(), function: "prismaSchemas", source: props.source, source_id: props.source_id, existing, requested: props.arguments.request.schemaNames, trial: props.trial, created_at: new Date().toISOString(), }); }; const orchestrateInterfaceOperations = (ctx, props) => { if ((() => { const _io0 = input => "string" === typeof input.thinking && ("object" === typeof input.request && null !== input.request && _io1(input.request)); const _io1 = input => "getInterfaceOperations" === input.type && (Array.isArray(input.endpoints) && (1 <= input.endpoints.length && input.endpoints.every(elem => "object" === typeof elem && null !== elem && _io2(elem)))); const _io2 = input => "string" === typeof input.path && RegExp("^\\/[a-zA-Z0-9\\/_{}.-]*$").test(input.path) && ("get" === input.method || "post" === input.method || "put" === input.method || "delete" === input.method || "patch" === input.method); return input => "object" === typeof input && null !== input && _io0(input); })()(props.arguments) === false) return; // unreachable const existing = props.local.operations.map((o) => ({ method: o.method, path: o.path, })); const typeNames = new Set(); for (const endpoint of props.arguments.request.endpoints) { if (props.local.operations.find((v) => v.method === endpoint.method && v.path === endpoint.path) !== undefined) continue; // duplicated const operation = props.all.operations.find((v) => v.method === endpoint.method && v.path === endpoint.path); if (operation === undefined) continue; // not found (???) props.local.operations.push(operation); if (operation.requestBody !== null) typeNames.add(operation.requestBody.typeName); if (operation.responseBody !== null) typeNames.add(operation.responseBody.typeName); } ctx.dispatch({ type: "preliminary", id: (0, uuid_1.v7)(), function: "interfaceOperations", source: props.source, source_id: props.source_id, existing, requested: props.arguments.request.endpoints, trial: props.trial, created_at: new Date().toISOString(), }); orchestrateInterfaceSchemas(ctx, { source: props.source, source_id: props.source_id, trial: props.trial, all: props.all.schemas, local: props.local.schemas, arguments: { thinking: "interface schemas for detailed schema information", request: { type: "getInterfaceSchemas", typeNames: Array.from(typeNames), }, }, }, false); }; const orchestrateInterfaceSchemas = (ctx, props, dispatch = true) => { if ((() => { const _io0 = input => "string" === typeof input.thinking && ("object" === typeof input.request && null !== input.request && _io1(input.request)); const _io1 = input => "getInterfaceSchemas" === input.type && (Array.isArray(input.typeNames) && (1 <= input.typeNames.length && input.typeNames.every(elem => "string" === typeof elem))); return input => "object" === typeof input && null !== input && _io0(input); })()(props.arguments) == false) return; // unreachable const existing = Object.keys(props.local); const collected = {}; for (const key of props.arguments.request.typeNames) { openapi_1.OpenApiTypeChecker.visit({ components: { schemas: props.all, }, schema: { $ref: `#/components/schemas/${key}` }, closure: (next) => { if (openapi_1.OpenApiTypeChecker.isReference(next)) { const last = next.$ref.split("/").pop(); collected[last] = props.all[last]; } }, }); } Object.assign(props.local, collected); if (dispatch === true) ctx.dispatch({ type: "preliminary", id: (0, uuid_1.v7)(), function: "interfaceSchemas", source: props.source, source_id: props.source_id, existing, requested: props.arguments.request.typeNames, trial: props.trial, created_at: new Date().toISOString(), }); }; //# sourceMappingURL=orchestratePreliminary.js.map