aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/assistant.py
diff options
context:
space:
mode:
Diffstat (limited to 'cogs/assistant.py')
-rw-r--r--cogs/assistant.py95
1 files changed, 0 insertions, 95 deletions
diff --git a/cogs/assistant.py b/cogs/assistant.py
deleted file mode 100644
index ffb664e..0000000
--- a/cogs/assistant.py
+++ /dev/null
@@ -1,95 +0,0 @@
1# This file is part of Senator Sperma
2# Licensed under the GPL-3.0 License
3# Copyright (C) 2026 Salmonik @ salmonik@tuta.io
4# https://codeberg.org/salmonik/senator-sperma
5
6import discord
7from discord.ext import commands
8import logging
9import os
10from cogs.utils.llm import llm_completion
11
12logger = logging.getLogger(__name__)
13
14class Jarvis(commands.Cog):
15 def __init__(self, bot):
16 self.bot = bot
17 self.keyword = os.getenv("ASSISTANT_KEYWORD", "jarvis").lower()
18
19 @commands.Cog.listener()
20 async def on_message(self, message):
21 if message.author == self.bot.user:
22 return
23
24 logger.info(message.content)
25
26 if message.mentions:
27 if message.mentions[0].id == self.bot.user.id:
28 await self.assistant(message)
29
30 elif self.keyword in message.content.lower():
31 await self.assistant(message)
32
33 @staticmethod
34 async def assistant(message):
35 chain = await Jarvis._build_message_chain(message)
36 messages = Jarvis._build_messages(message, chain)
37
38 extra = None
39 api_url = os.getenv("LLM_API_URL", "https://openrouter.ai/api/v1/chat/completions")
40 if "openrouter" in api_url.lower():
41 extra = {"plugins": [{"id": "response-healing"}]}
42
43 response_text = await llm_completion(messages, extra=extra)
44 if response_text is None:
45 await message.reply("Przepraszam, nie mogę teraz odpowiedzieć. Spróbuj ponownie później.")
46 return
47
48 await message.reply(response_text)
49
50 @staticmethod
51 async def _build_message_chain(message):
52 chain = []
53 current = message
54 max_depth = 5
55
56 while current and len(chain) < max_depth:
57 chain.append(current)
58 if not current.reference:
59 break
60 try:
61 if current.reference.resolved:
62 current = current.reference.resolved
63 else:
64 current = await current.channel.fetch_message(current.reference.message_id)
65 except discord.NotFound:
66 break
67
68 return list(reversed(chain))
69
70 @staticmethod
71 def _build_messages(message, chain=None):
72 messages = [{
73 "role": "system",
74 "content": "Jesteś Senator Sperma Armostrong, także znany jako Jarvis."
75 "Twoim zadaniem jest asystowanie użytkownika"
76 "zachowuj sie jak Jarvis, bądź osobistym asystentem użytkownika, nie używaj emotikon"
77 "nie pytaj sie użytkownika czy chce abyś coś zrobił, użytkownik ci sam powie abyś coś wykonał"
78 "Nie pozostawiaj wiadomości pustej oraz nie dawaj swojej wiadomości w pole myślenia"
79 }]
80
81 if chain is None:
82 chain = [message]
83
84 for msg in chain:
85 messages.append({
86 "role": "user",
87 "content": f"USER: {msg.author.display_name}\nMESSAGE: {msg.content}"
88 })
89
90 return messages
91
92
93
94def setup(bot):
95 bot.add_cog(Jarvis(bot))