-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (59 loc) · 2.69 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import asyncio
import datetime
import conlog
import tokens
import discordbot
import assistant
import ticker
import logmanager
import Tools.Embedding
import Tools.Record
import Tools.RecordBank
import Tools.ReminderBank
import Tools.Reminders
import Tools.ToolManager
async def main():
conlog.log_system("Starting up")
masterqueue = asyncio.Queue()
assistantqueue = asyncio.Queue()
conlog.log_system("Created queues")
logger = logmanager.LogManager()
conlog.log_system("Created conversations.json logger")
records = Tools.RecordBank.RecordBank("data/records")
reminders = Tools.ReminderBank.ReminderBank("data/reminders")
conlog.log_system("RecordBank and ReminderBank loaded.")
client = discordbot.SetupDiscordClient(assistantqueue, tokens.user_id)
conlog.log_system("Setup discord client.")
ticking = ticker.Ticker(reminders, records, masterqueue)
conlog.log_system("Ticker created")
toolmanager = Tools.ToolManager.ToolManager(client, ticking, records, reminders, logger)
conlog.log_system("Tool manager created.")
assistant_handler = assistant.OpenAIChatHandler(assistantqueue, logger, toolmanager, client, tokens.openai_key, tokens.assistant_id, tokens.user_id)
conlog.log_system("OpenAI Assistant Handler instantiated.")
conlog.log_system("Starting Tasks...")
asyncio.create_task(client.start(tokens.discord_token))
asyncio.create_task(ticking.TickerLoop())
asyncio.create_task(assistant_handler.waitloop())
conlog.log_system("Exiting setup and starting main loop.")
await main_loop(masterqueue, reminders, assistant_handler)
async def main_loop(masterqueue: asyncio.Queue, reminders: Tools.ReminderBank.ReminderBank, assistant_handler: assistant.OpenAIChatHandler):
while True:
while not masterqueue.empty():
item = await masterqueue.get()
if isinstance(item, Tools.Embedding.Reminder):
try:
new_thread = await assistant_handler.external_thread("Reminder", item.abstract)
if not new_thread:
conlog.log_system("Companion is busy, appending back to list with a half minute delay...")
delayed: Tools.Embedding.Reminder = item
delayed.time = item.time + datetime.timedelta(seconds = 30)
reminders.reminders.append(delayed)
else:
conlog.log_system("Companion has been alerted of an internal ticker elapse.")
except Exception as e:
conlog.log_system(f"Internal Exception from creating new thread: \n{e}\n\n")
else:
conlog.log_system("Unhandled ticker type... Ignoring.")
await asyncio.sleep(0.5)
if __name__ == "__main__":
asyncio.run(main())