import { DyNTS_OAI_LLMChat_ServiceBase } from './../../ai/_modules/open-ai/_services/oai-llm-chat.service-base'; import { Client, Guild, Message, TextChannel } from 'discord.js'; import { DyNTS_DiBo_Main_ControlService } from '../../discord-bot/_services/dibo-main.control-service'; import { DyNTS_DiAs_global_settings } from '../_collections/dias-global-settings.const'; import { DyNTS_global_settings } from '../../../_collections/global-settings.const'; import { DyFM_AI_Message } from '@futdevpro/fsm-dynamo/ai'; import { DyNTS_DiAs_Util } from '../_collections/dias.util'; import { DyNTS_DiAs_IO_ControlService } from './dias-io.control-service'; export abstract class DyNTS_DiAs_ServiceBase extends DyNTS_DiBo_Main_ControlService { getLLMChatControlService(): DyNTS_OAI_LLMChat_ServiceBase { return new DyNTS_OAI_LLMChat_ServiceBase(); } readonly llmChat_CS: DyNTS_OAI_LLMChat_ServiceBase = this.getLLMChatControlService(); protected abstract override botIO_CS: DyNTS_DiAs_IO_ControlService; protected abstract override getBotIOControlService(): DyNTS_DiAs_IO_ControlService; defaultSystemPrompt: string = DyNTS_DiAs_global_settings.defaultSystemPrompt; protected constructor( setupAutomatically?: boolean, ) { super(setupAutomatically); DyNTS_global_settings.dias_settings ??= DyNTS_DiAs_global_settings; this.llmChat_CS.defaultSettings.systemPrompt = this.defaultSystemPrompt; } async gatherDiscordMessagesForMessage(message: Message, limit: number = 100): Promise { return this.gatherDiscordMessagesForChannel(message.channel as TextChannel, message.author.id, limit); } async gatherDiscordMessagesForChannel( channel: TextChannel, userId: string, limit: number = 100, /** If this is true, then we will add the sender's name to each message */ addSourceInfo?: boolean, ): Promise { const messages: Message[] = await channel.messages.fetch({ limit: limit }).then( messages => messages.filter( msg => !DyNTS_DiAs_global_settings.skipConversationMessagesFlags.some( flag => msg.content.includes(flag) ) ).map(msg => msg) // this last part creates simple array from Discord Collection ); if (addSourceInfo && messages.some(msg => ![ this.botClientId, userId ].includes(msg.author.id))) { messages.forEach(msg => { msg.content = `**${msg.author.displayName}**: ${msg.content}`; }); } messages.sort((a, b) => a.createdTimestamp - b.createdTimestamp); return messages; } async gatherMessagesAsOAIConversation( message: Message, issuer: string, ): Promise { const messages: Message[] = await this.gatherDiscordMessagesForMessage(message); return DyNTS_DiAs_Util.convertDiscordMessagesToOAIConversation({ messages: messages, botClientId: this.botClientId, botDisplayName: this.botDisplayName, issuer: issuer, }); } }