UNPKG

@dxtmisha/scripts

Version:

Development scripts and CLI tools for DXT UI projects - automated component generation, library building and project management tools

144 lines (124 loc) 4.53 kB
import { PropertiesConfig } from '../Properties/PropertiesConfig' /** * Abstract AI base class providing common mechanics for AI integrations. * Handles prompt accumulation, model selection and unified response workflow. * * Абстрактный базовый класс AI, обеспечивающий общую механику интеграций: * накопление prompt, выбор модели и унифицированный цикл получения ответа. * * Responsibilities / Ответственности: * - Accumulate and compose prompt context / Накопление и компоновка контекста * - Select active model / Выбор активной модели * - Normalize generation call flow / Нормализация вызова генерации * * Extension points / Точки расширения: * - init(): client / auth / transport setup * - response(): concrete model invocation * * Расширение через методы: * - init(): инициализация клиента / авторизация / транспорт * - response(): конкретный вызов модели */ export abstract class AiAbstract<AI = any> { /** API key / API ключ */ protected key: string /** AI client instance / Экземпляр AI-клиента */ protected ai?: AI /** Active model id / Идентификатор активной модели */ protected model: string /** Prompt prefix / Префикс prompt */ protected prompt: string = '' /** * Constructor initializes implementation specific resources. * * Конструктор инициализирует ресурсы конкретной реализации. */ constructor() { this.key = PropertiesConfig.getAiKey() this.model = PropertiesConfig.getAiModel() } /** * Appends a new line to the accumulated prompt block. * * Добавляет новую строку к накопленному блоку prompt. */ addPrompt(prompt: string): void { this.prompt += `\n${prompt}` } /** * Clears entire accumulated prompt. * * Полностью очищает накопленный prompt. */ resetPrompt(): void { this.prompt = '' } /** * Sets / switches API key for authentication. * * Устанавливает / переключает API ключ для аутентификации. * @param key - new API key / новый API ключ */ setKey(key: string): this { this.key = key if (this.ai) { this.init() } return this } /** * Sets / switches current model for generation. * * Устанавливает / переключает текущую модель для генерации. */ setModel(model: string): this { this.model = model return this } /** * Generates AI response using active model and composed contents. * * Генерирует отклик ИИ, используя активную модель и собранный контент. */ async generate(contents: string): Promise<string> { if (!this.ai) { this.init() console.log( `[Ai] Initialized AI client with model: ${this.model}` ) } if (this.ai) { console.log('[Ai] Generating') return await this.response( this.model, this.getContents(contents) ) } return '' } /** * Combines persistent prompt prefix with provided contents. * * Объединяет постоянный префикс prompt с переданным содержимым. */ protected getContents(contents: string): string { return `${this.prompt}\n${contents}` } /** * Implementation hook: initialize client / transport / auth. * * Хук реализации: инициализация клиента / транспорта / авторизации. */ protected abstract init(): void /** * Implementation hook: perform model call and return textual result. * * Хук реализации: выполнить вызов модели и вернуть текстовый результат. * @param model - active model id / идентификатор активной модели * @param contents - composed prompt + contents / собранный prompt + содержимое */ protected abstract response( model: string, contents: string ): Promise<string> }