UNPKG

i18n-ai-translate

Version:

AI-powered localization CLI, Node library, and GitHub Action. Translate i18next JSON, Gettext PO, Java .properties, and iOS .strings with ChatGPT, Claude, Gemini, or local Ollama models.

93 lines 3.08 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const utils_1 = require("../utils"); const gemini_zod_1 = require("gemini-zod"); const chat_interface_1 = __importDefault(require("./chat_interface")); const role_1 = __importDefault(require("../enums/role")); class Gemini extends chat_interface_1.default { model; chat; history; params; rateLimiter; constructor(model, rateLimiter) { super(); this.model = model; this.chat = null; this.history = []; this.params = null; this.rateLimiter = rateLimiter; } startChat(params) { this.params = params; if (this.history.length > 0) { params.history = this.history.map((x) => ({ parts: [{ text: x.parts }], role: x.role === role_1.default.User ? "user" : "model", })); } this.chat = this.model.startChat(params); } async sendMessage(message, format) { if (!this.chat) { console.trace("Chat not started"); return ""; } await this.rateLimiter.wait(); this.rateLimiter.apiCalled(); if (format) { this.model.generationConfig.responseMimeType = "application/json"; this.model.generationConfig.responseSchema = (0, gemini_zod_1.toGeminiSchema)(format); } else { this.model.generationConfig.responseMimeType = ""; this.model.generationConfig.responseSchema = undefined; } try { const generatedContent = await this.chat.sendMessage(message); const response = generatedContent.response.text(); if (!response) { (0, utils_1.printError)(`Gemini exception encountered. err = ${JSON.stringify(generatedContent?.response, null, 4)}`); } return response.trimEnd(); } catch (err) { (0, utils_1.printError)(err); return ""; } } resetChatHistory() { this.history = []; this.startChat(this.params); } rollbackLastMessage() { if (this.history.length === 0) { return; } if (this.history[this.history.length - 1].role === role_1.default.Assistant) { this.history.pop(); this.history.pop(); } else if (this.history[this.history.length - 1].role === role_1.default.User) { this.history.pop(); } this.startChat(this.params); } invalidTranslation() { this.history.push({ parts: this.invalidTranslationMessage(), role: role_1.default.System, }); } invalidStyling() { this.history.push({ parts: this.invalidStylingMessage(), role: role_1.default.System, }); } } exports.default = Gemini; //# sourceMappingURL=gemini.js.map