UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

106 lines (105 loc) 6.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AIService = void 0; const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const openrouter_api_1 = require("../modules/ai/openrouter-api"); const fs_extra_1 = require("../plugins/fs-extra"); const string_1 = require("../plugins/string"); class AIService { constructor(ownership) { this.ownership = ownership; } async generateDockerfileByDirectoryStructure(structure, options) { var _a, _b, _c, _d, _e, _f; if (!structure) throw new Error(`Directory structure (string) is required.`); // ask AI to generate a Dockerfile: let askMessage = `Act as a code generator tool, based on this directory structure: ${structure}`; askMessage += `\nGenerate content of a Dockerfile satisfied these conditions:`; // askMessage += "\n- Use single-stage build when you think this is a static html project"; askMessage += "\n- Use multi-stage if this is a Javascript, TypeScript, Node.js, Rust, Python or Go lang project"; // askMessage += "\n- In each build stage, pick the right base image with optimal latest tag"; askMessage += "\n- Use latest tag in FROM"; // askMessage += "\n- Only copy dotenv file when the input directory structure contains dotenv file."; askMessage += "\n- Optimize the container image size, make sure it is as lightweight image size as possible"; askMessage += `\nDo not include any explanations or markdown in your response`; if (options === null || options === void 0 ? void 0 : options.isDebugging) console.log("askMessage :>> ", askMessage); const dto = { // model: "openai/gpt-3.5-turbo", model: "qwen/qwen-2.5-coder-32b-instruct", messages: [ { role: "system", content: "You are a helpful code generator tool.", }, { role: "user", content: askMessage, }, ], }; const response = await (0, openrouter_api_1.aiApi)({ baseUrl: (_c = (_b = (_a = this.workspace) === null || _a === void 0 ? void 0 : _a.settings) === null || _b === void 0 ? void 0 : _b.ai) === null || _c === void 0 ? void 0 : _c.apiBaseUrl, apiKey: (_f = (_e = (_d = this.workspace) === null || _d === void 0 ? void 0 : _d.settings) === null || _e === void 0 ? void 0 : _e.ai) === null || _f === void 0 ? void 0 : _f.apiKey, data: dto, }); // console.log("response :>> ", response); // write it down into a file: const { content } = response.choices[0].message; // if (options?.isDebugging) console.log("generateDockerfile() > content :>> ", content); let dockerfileContent = (0, string_1.extractTextBetweenBackticks)(content); return dockerfileContent; } async generateDockerfile(dir = process.cwd(), options) { if (!(0, fs_1.existsSync)(dir)) throw new Error(`Directory not existed.`); // scan directory for file structure: const filesInStr = await (0, fs_extra_1.getFolderStructure)(dir); // ask AI to generate: const dockerfileContent = await this.generateDockerfileByDirectoryStructure(filesInStr, options); if (options === null || options === void 0 ? void 0 : options.isDebugging) console.log("generateDockerfile() > dockerfileContent :>> ", dockerfileContent); (0, fs_1.writeFileSync)(path_1.default.resolve(dir, `Dockerfile.generated`), dockerfileContent, "utf8"); return dockerfileContent; } async analyzeErrorLog(log, options) { var _a, _b, _c, _d, _e, _f; if (!log) throw new Error(`Error log (string) is required.`); // ask AI to analyze: let askMessage = `Act as a code analyzer tool, analyze the error log and provide a detailed explanation of the root cause of the error based on this context:`; if (options === null || options === void 0 ? void 0 : options.context) askMessage += `\n## Context:`; if (options === null || options === void 0 ? void 0 : options.context) askMessage += `\n${options === null || options === void 0 ? void 0 : options.context}`; askMessage += `\n## Instructions:`; askMessage += `\nAnalyze the error log and provide a detailed explanation of the root cause of the error.`; askMessage += `\nDo not include any explanations or markdown in your response`; askMessage += `\n## Content of the container log:`; askMessage += `\n\`\`\`\n${log}\n\`\`\``; const dto = { model: "qwen/qwen-2.5-coder-32b-instruct", messages: [ { role: "system", content: "You are a helpful code analyzer tool." }, { role: "user", content: askMessage }, ], }; if (options === null || options === void 0 ? void 0 : options.isDebugging) console.log("analyzeErrorLog() > dto :>> ", dto); const response = await (0, openrouter_api_1.aiApi)({ baseUrl: (_c = (_b = (_a = this.workspace) === null || _a === void 0 ? void 0 : _a.settings) === null || _b === void 0 ? void 0 : _b.ai) === null || _c === void 0 ? void 0 : _c.apiBaseUrl, apiKey: (_f = (_e = (_d = this.workspace) === null || _d === void 0 ? void 0 : _d.settings) === null || _e === void 0 ? void 0 : _e.ai) === null || _f === void 0 ? void 0 : _f.apiKey, data: dto, }); if (options === null || options === void 0 ? void 0 : options.isDebugging) console.log("analyzeErrorLog() > response :>> ", response); const { content } = response.choices[0].message; return content; } } exports.AIService = AIService;