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.

81 lines 2.79 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 chat_interface_1 = __importDefault(require("./chat_interface")); const role_1 = __importDefault(require("../enums/role")); const zod_to_json_schema_1 = __importDefault(require("zod-to-json-schema")); class Ollama extends chat_interface_1.default { model; chatParams; history; constructor(model) { super(); this.model = model; this.chatParams = null; this.history = []; } startChat(params) { this.chatParams = { ...params, stream: false }; if (params.messages && params.messages.length > 0) { this.history = params.messages; } } async sendMessage(message, format) { if (!this.chatParams) { console.trace("Chat not started"); return ""; } this.history.push({ content: message, role: role_1.default.User }); const formatSchema = format ? (0, zod_to_json_schema_1.default)(format) : undefined; this.chatParams = { ...this.chatParams, format: formatSchema, messages: this.history, }; try { const response = await this.model.chat(this.chatParams); const responseText = response.message.content; if (!responseText) { return ""; } this.history.push({ content: responseText, role: role_1.default.Assistant }); return responseText; } catch (err) { (0, utils_1.printError)(err); return ""; } } resetChatHistory() { this.history = []; } rollbackLastMessage() { if (this.history[this.history.length - 1].role === role_1.default.Assistant) { // Remove the last two messages (user and assistant) // so we can get back to the last successful state in history this.history.pop(); this.history.pop(); } else if (this.history[this.history.length - 1].role === role_1.default.User) { // The model didn't respond, so we only need to remove the user message this.history.pop(); } } invalidTranslation() { this.history.push({ content: this.invalidTranslationMessage(), role: role_1.default.System, }); } invalidStyling() { this.history.push({ content: this.invalidStylingMessage(), role: role_1.default.System, }); } } exports.default = Ollama; //# sourceMappingURL=ollama.js.map