-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathProgram.cs
More file actions
826 lines (690 loc) · 36 KB
/
Program.cs
File metadata and controls
826 lines (690 loc) · 36 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using Newtonsoft.Json.Linq;
using SallyBot.Backends;
using SallyBot.Chat;
using SallyBot.Extras;
namespace SallyBot
{
class Program
{
// ── Timer ──────────────────────────────────────────────────────────────
private static System.Timers.Timer _loop;
// ── Discord ────────────────────────────────────────────────────────────
private DiscordSocketClient _client;
// ── State flags ────────────────────────────────────────────────────────
private static bool _dalaiThinking = false;
private static int _llmThinkingTicks = 0;
private static int _loopCounts = 0;
private static bool _newMsgReceived = false;
private static int _checkTimeout = 0;
private static int _checkTimeoutCount = 0;
private static bool _longMsgWarningGiven = false;
// ── Config ─────────────────────────────────────────────────────────────
private static int _maxChatHistoryLength = 500;
// ── Bot identity ───────────────────────────────────────────────────────
internal static ulong BotUserId = 0;
internal static string BotName = "sally03";
// ── Prompt templates ───────────────────────────────────────────────────
private string CharacterPrompt =>
$"[INST]\r\nEnter chat mode. You shall reply to other users while staying in character. " +
$"This is not a roleplay, do not include actions. " +
$"Keep your replies short, natural, casual and realistic.\r\n" +
$"# About {BotName}:\r\nName: {BotName}\r\n[/INST]";
private string CompletionSuffix => $"### Response:\n[{BotName}]:";
private static readonly string InputPromptStartPic =
"### Instruction: Take the scene and create a single line comma separated list of " +
"descriptions of the scene and keywords/tags to describe what things are visible.\r\n\r\n" +
"Ignore things that are not visible, such as thoughts, feelings, speech or sound.\r\n\r\n" +
"Minimum Requirements:\r\n" +
"1. List of keywords for characters in the scene\r\n" +
"2. List of keywords to describe the location\r\n\r\n" +
"Use only the top 15 keywords in the list. Reply with nothing but the single line list.";
private const string InputPromptEndPic = "### Response:";
// ── Content filters ────────────────────────────────────────────────────
private static readonly List<string> BannedWords = new()
{
"p0rnography", "h3ntai"
};
private const string BannedWordsExact = @"\b(fuck|shit|cock)\b";
private const string BannedWordsExactPic = @"\b(booty|erotic|naked|topless|butt|ass|tentacle|tentacles|nude|r34)\b";
// ── Regex ──────────────────────────────────────────────────────────────
private const string TakeAPicRegexStr =
@"\b(take|post|paint|generate|make|draw|create|show|give|snap|capture|send|display|share|shoot|see|provide|another)\b.*(\S\s{0,10})?(image|picture|screenshot|screenie|painting|pic|photo|photograph|portrait|selfie)\b";
private const string PromptEndDetectionRegexStr =
@"(?:\r\n?)|(\n\[|\n#|\[end|<end|]:|>:|<nooutput|<noinput|\[human|\[chat|\[sally|\[cc|<chat|<cc|\[@chat|\[@cc|bot\]:|\.]|<@chat|<@cc|\[.*]: |<\/s>|\[.*] : |\[[^\]]+\]\s*:)";
private const string PromptSpoofDetectionRegexStr = @"\[[^\]]+[\]:\\]\:|\:\]|\[^\]]";
private const string ToneIndicatorDetector = @"^(\[[^\]]+\]|\([^)]+\))";
private const string LinkDetectionRegexStr =
@"[a-zA-Z0-9]((?i) dot |(?i) dotcom|(?i)dotcom|(?i)dotcom |\.|\. | \.| \. |\,)[a-zA-Z]*((?i) slash |(?i) slash|(?i)slash |(?i)slash|\/|\/ | \/| \/ ).+[a-zA-Z0-9]";
public const string PingAndChannelTagDetectFilterRegexStr = @"<[@#]\d{15,}>";
private string BotNameMatchRegexStr => @$"(?:{BotName}\?|{BotName},)";
private readonly Regex _takeAPicRegex = new(TakeAPicRegexStr, RegexOptions.IgnoreCase);
// ── Message buffer ─────────────────────────────────────────────────────
internal static SocketUserMessage? BufferChatMsg;
internal static string BufferInputMsgFiltered = string.Empty;
// ── Chat history ───────────────────────────────────────────────────────
private static readonly ChatHistory _history = new();
private static bool _historyDownloaded = false;
// ── Reply state ────────────────────────────────────────────────────────
public static bool BotWillReply = false;
private static string _botLastReply = "<noinput>";
private static readonly CancellationTokenSource _appCts = new();
// ======================================================================
// Entry point
// ======================================================================
public static async Task Main() => await new Program().AsyncMain();
private async Task AsyncMain()
{
// Validate required config before doing anything else
if (MainGlobal.DiscordToken.Contains("goes here") || MainGlobal.DiscordToken.Length < 10)
{
Console.WriteLine("[CONFIG] Set MainGlobal.DiscordToken to your Discord bot token.");
return;
}
if (MainGlobal.GuildId == 0)
{
Console.WriteLine("[CONFIG] Set MainGlobal.GuildId to your Discord server (guild) ID.");
return;
}
try
{
_client = new DiscordSocketClient(new DiscordSocketConfig
{
MessageCacheSize = 1200,
LogLevel = LogSeverity.Debug,
AlwaysDownloadUsers = true,
GatewayIntents =
GatewayIntents.MessageContent |
GatewayIntents.Guilds |
GatewayIntents.GuildMessages |
GatewayIntents.GuildMembers,
});
_client.Log += Client_Log;
_client.Ready += OnReady;
_client.MessageReceived += Client_MessageReceived;
_client.GuildMemberUpdated += Client_GuildMemberUpdated;
MainGlobal.Client = _client;
await _client.LoginAsync(TokenType.Bot, MainGlobal.DiscordToken);
await _client.StartAsync();
// Attempt Dalai connection (non-fatal)
MainGlobal.DalaiBackend = new DalaiBackend(MainGlobal.DalaiUrl, MainGlobal.DalaiModel);
await MainGlobal.DalaiBackend.ConnectAsync();
// Auto-detect first available backend
MainGlobal.ActiveBackend =
await BackendDetector.DetectAsync(BuildBackendCandidates());
_loop = new System.Timers.Timer { Interval = 5500, AutoReset = true, Enabled = true };
_loop.Elapsed += Tick;
Console.WriteLine($"|{DateTime.Now} | Main loop initialised");
AppDomain.CurrentDomain.FirstChanceException += (_, args) =>
{
var ex = args.Exception;
Console.WriteLine(
$"\u001b[45;1m[ DISC ]\u001b[41;1m[ ERR ]\u001b[0m " +
$"MSG: {ex.Message}\n WHERE: {ex.StackTrace}\n");
};
await Task.Delay(-1);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
// ── Backend candidate list ─────────────────────────────────────────────
private static IEnumerable<ILlmBackend> BuildBackendCandidates()
{
if (MainGlobal.DalaiBackend.IsConnected)
yield return MainGlobal.DalaiBackend;
yield return new OobaboogaBackend(MainGlobal.OobaboogaUrl);
yield return new LmStudioBackend(MainGlobal.LmStudioUrl);
if (!string.IsNullOrWhiteSpace(MainGlobal.OpenAiApiKey))
yield return new OpenAiBackend(MainGlobal.OpenAiApiKey, MainGlobal.OpenAiModel);
if (!string.IsNullOrWhiteSpace(MainGlobal.OpenRouterApiKey))
yield return new OpenRouterBackend(MainGlobal.OpenRouterApiKey, MainGlobal.OpenRouterModel);
if (!string.IsNullOrWhiteSpace(MainGlobal.GoogleApiKey))
yield return new GeminiBackend(MainGlobal.GoogleApiKey, MainGlobal.GeminiModel);
}
// ======================================================================
// Discord callbacks
// ======================================================================
private Task OnReady() => MainLoop.StartLoop();
private static readonly string[] _logBlacklist = new[]
{
"PRESENCE_UPDATE", "TYPING_START", "MESSAGE_CREATE", "MESSAGE_DELETE",
"MESSAGE_UPDATE", "CHANNEL_UPDATE", "GUILD_", "REACTION_",
"VOICE_STATE_UPDATE", "DELETE channels/", "POST channels/",
"Heartbeat", "GET ", "PUT ", "Latency = ", "handler is blocking the",
};
private Task Client_Log(LogMessage msg)
{
if (msg.Message != null && _logBlacklist.All(x => !msg.Message.Contains(x)))
Console.WriteLine($"|{DateTime.Now} - {msg.Source}| {msg.Message}");
else if (msg.Exception != null)
Console.WriteLine($"|{DateTime.Now} - {msg.Source}| {msg.Exception}");
return Task.CompletedTask;
}
private Task Client_GuildMemberUpdated(
Cacheable<SocketGuildUser, ulong> before, SocketGuildUser after)
{
if (before.Value?.Id != _client.CurrentUser.Id) return Task.CompletedTask;
if (after.DisplayName != null && before.Value?.DisplayName != after.DisplayName)
BotName = after.DisplayName;
else if (after.Nickname != null && before.Value?.Nickname != after.Nickname)
BotName = after.Nickname;
else if (before.Value?.Username != after.Username)
BotName = after.Username;
return Task.CompletedTask;
}
// ── Message received ───────────────────────────────────────────────────
private async Task Client_MessageReceived(SocketMessage msgParam)
{
try
{
var msg = msgParam as SocketUserMessage;
if (msg == null) return;
var ctx = new SocketCommandContext(_client, msg);
var user = ctx.User as SocketGuildUser;
if (msg.Author == MainGlobal.Server.Owner)
if (await HandleOwnerCommand(msg)) return;
if (msg.Author.Id == _client.CurrentUser.Id) return;
// One-time download of recent history
if (!_historyDownloaded && !MainGlobal.DalaiBackend.IsConnected)
{
_historyDownloaded = true;
await DownloadChatHistory(msg);
}
// Resolve display name
string userName;
if (user?.DisplayName != null) userName = user.DisplayName;
else if (user?.Nickname != null) userName = user.Nickname;
else userName = msg.Author.Username;
string userNameClean = Regex.Replace(userName, "[^a-zA-Z0-9]+", "");
if (userNameClean.Length < 1) userNameClean = "User";
string imagePresent = string.Empty;
if (msg.Attachments.Count > 0)
imagePresent = $"\n[System]: {userName} attached an image";
// Sanitise incoming message
string inputMsg = Functions.FilterPingsAndChannelTags(msg.Content);
inputMsg = Regex.Replace(inputMsg, @"\n", " ");
inputMsg = Regex.Replace(inputMsg, PromptSpoofDetectionRegexStr, "");
string inputMsgFiltered = $"[{userNameClean}]: {inputMsg.Trim()}";
inputMsgFiltered = Regex.Replace(inputMsgFiltered, BannedWordsExact, "****");
string detected = Functions.IsSimilarToBannedWords(inputMsgFiltered, BannedWords);
if (detected.Length > 2)
{
foreach (string w in detected.Split(' '))
{
string wt = w.Trim();
if (wt.Length > 2)
{
inputMsgFiltered = inputMsgFiltered.Replace(wt, "****");
if (inputMsgFiltered.Contains(" "))
inputMsgFiltered = inputMsgFiltered.Replace(" ", " ");
}
}
Console.WriteLine($"{inputMsgFiltered} <Banned or similar words removed.>{imagePresent}");
}
else if (!MainGlobal.DalaiBackend.IsConnected)
{
Console.WriteLine($"{inputMsgFiltered}{imagePresent}");
}
// Add to history
string safeContent = Regex.Replace(inputMsg.Trim(), LinkDetectionRegexStr, "<url>");
_history.Add(new ChatMessage(userNameClean, safeContent));
if (!string.IsNullOrEmpty(imagePresent))
_history.Add(new ChatMessage("System",
$"{userName} attached an image", isSystem: true));
if (msg.Author.IsBot) return;
// Wait for current generation
int timeout = 45;
while (_llmThinkingTicks > 0 && timeout-- > 0)
await Task.Delay(1000, _appCts.Token);
_newMsgReceived = true;
if (msg.MentionedUsers.Contains(MainGlobal.Server.GetUser(BotUserId))
&& msg.Content.Length > 22)
{
BotWillReply = true;
_newMsgReceived = false;
}
BufferChatMsg = msg;
BufferInputMsgFiltered = inputMsgFiltered;
if (BotWillReply)
{
BotWillReply = false;
_llmThinkingTicks = 10;
_history.TrimToMaxLength(_maxChatHistoryLength);
_ = Task.Run(() => Reply(msg, inputMsgFiltered), _appCts.Token);
}
}
catch (Exception ex)
{
Console.WriteLine($"MessageReceived error: {ex.Message}");
}
}
// ── Owner runtime commands ─────────────────────────────────────────────
private static async Task<bool> HandleOwnerCommand(SocketUserMessage msg)
{
string lower = msg.Content.Trim().ToLower();
// "use backend <name>" – switch to a specific backend
if (lower.StartsWith("use backend "))
{
string wanted = msg.Content.Trim().Substring("use backend ".Length).Trim();
foreach (var b in BuildBackendCandidates())
{
if (b.Name.Equals(wanted, StringComparison.OrdinalIgnoreCase)
&& await b.IsAvailableAsync())
{
MainGlobal.ActiveBackend = b;
Console.WriteLine($"| Owner switched backend to: {b.Name}");
return true;
}
}
Console.WriteLine($"| Could not switch to backend '{wanted}'.");
return true;
}
// Legacy Gemini toggles
if (lower == "enable gemini" && !string.IsNullOrWhiteSpace(MainGlobal.GoogleApiKey))
{
MainGlobal.ActiveBackend =
new GeminiBackend(MainGlobal.GoogleApiKey, MainGlobal.GeminiModel);
Console.WriteLine("| Switched to Google Gemini backend.");
return true;
}
if (lower == "disable gemini")
{
var fallback = BuildBackendCandidates().Where(b => b is not GeminiBackend);
MainGlobal.ActiveBackend = await BackendDetector.DetectAsync(fallback);
return true;
}
return false;
}
// ======================================================================
// Timer Tick
// ======================================================================
private async void Tick(object? sender, ElapsedEventArgs e)
{
if (_llmThinkingTicks > 0) _llmThinkingTicks--;
if (_newMsgReceived && _checkTimeoutCount <= 0 && _llmThinkingTicks <= 0)
{
_newMsgReceived = false;
if (BufferChatMsg != null)
await ReplyCheck(BufferChatMsg, BufferInputMsgFiltered);
}
else if (_checkTimeoutCount > 0)
{
_checkTimeoutCount--;
}
}
// ======================================================================
// Chat-history download
// ======================================================================
private async Task DownloadChatHistory(SocketUserMessage triggerMsg)
{
var downloaded = await triggerMsg.Channel.GetMessagesAsync(10).FlattenAsync();
IGuild guild = MainGlobal.Server;
string removedNote = string.Empty;
foreach (var dlMsg in downloaded)
{
if (dlMsg == null || dlMsg.Id == triggerMsg.Id) continue;
var dlUser = await guild.GetUserAsync(dlMsg.Author.Id);
string dlName = dlUser?.DisplayName ?? dlUser?.Nickname ?? dlUser?.Username
?? dlMsg.Author.Username;
string dlClean = Regex.Replace(dlName, "[^a-zA-Z0-9]+", "");
if (dlClean.Length < 1) dlClean = "User";
string dlContent = Regex.Replace(dlMsg.Content, PromptSpoofDetectionRegexStr, "");
dlContent = Functions.FilterPingsAndChannelTags(dlContent);
dlContent = Regex.Replace(dlContent, LinkDetectionRegexStr, "<url>");
dlContent = Regex.Replace(dlContent, BannedWordsExact, "****");
_history.Add(new ChatMessage(dlClean, dlContent,
isBot: dlMsg.Author.Id == BotUserId));
if (dlMsg.Attachments.Count > 0)
_history.Add(new ChatMessage("System",
$"{dlName} attached an image", isSystem: true));
}
// Fuzzy ban sweep
string allText = _history.ToCompletionString();
string histBanned = Functions.IsSimilarToBannedWords(allText, BannedWords);
if (histBanned.Length > 2)
{
foreach (string w in histBanned.Split(' '))
{
string wt = w.Trim();
if (wt.Length > 2) _history.ReplaceInAll(wt, "****");
}
removedNote = " Removed all banned or similar words.";
}
Console.WriteLine(_history.ToCompletionString().Trim());
Console.WriteLine($" <Downloaded chat history successfully.{removedNote}>");
}
// ======================================================================
// Reply-check (should the bot proactively reply?)
// ======================================================================
private async Task ReplyCheck(SocketUserMessage msg, string inputMsgFiltered)
{
var backend = MainGlobal.ActiveBackend;
if (backend == null)
{
MainGlobal.ActiveBackend = await BackendDetector.DetectAsync(BuildBackendCandidates());
backend = MainGlobal.ActiveBackend;
if (backend == null) return;
}
var recent = _history.GetLast(10).ToList();
// Inject referenced-message context
var referencedMsg = msg.ReferencedMessage as SocketUserMessage;
if (referencedMsg != null)
{
string refName = referencedMsg.Author.Id == BotUserId
? BotName
: Regex.Replace(referencedMsg.Author.Username, "[^a-zA-Z0-9]+", "");
if (recent.Count > 0)
recent.Insert(recent.Count - 1, new ChatMessage(refName, referencedMsg.Content));
}
bool shouldReply;
try
{
shouldReply = await backend.ShouldReplyAsync(BotName, recent, _appCts.Token);
}
catch (Exception ex)
{
Console.WriteLine($"| ReplyCheck error ({backend.Name}): {ex.Message}");
await TryFallbackBackendAsync();
return;
}
if (shouldReply)
{
_checkTimeout = 0;
_llmThinkingTicks = 10;
_history.TrimToMaxLength(_maxChatHistoryLength);
_ = Task.Run(() => Reply(msg, inputMsgFiltered), _appCts.Token);
}
else
{
if (_checkTimeout < 3)
_checkTimeoutCount = ++_checkTimeout;
}
}
// ======================================================================
// Main reply generation
// ======================================================================
private async Task Reply(SocketUserMessage msg, string inputMsgFiltered)
{
inputMsgFiltered = inputMsgFiltered
.Replace("\n", " ")
.Replace("\\n", " ");
bool takeAPicMatch = _takeAPicRegex.IsMatch(inputMsgFiltered);
var backend = MainGlobal.ActiveBackend;
if (backend == null)
{
MainGlobal.ActiveBackend = await BackendDetector.DetectAsync(BuildBackendCandidates());
backend = MainGlobal.ActiveBackend;
if (backend == null) { _llmThinkingTicks = 0; return; }
}
_history.TrimToMaxLength(_maxChatHistoryLength);
// Build message list with optional reply context
var referencedMsg = msg.ReferencedMessage as SocketUserMessage;
var messages = _history.Messages.ToList();
if (referencedMsg != null)
{
string refName = referencedMsg.Author.Id == BotUserId
? BotName
: Regex.Replace(referencedMsg.Author.Username, "[^a-zA-Z0-9]+", "");
if (messages.Count > 0)
messages.Insert(messages.Count - 1,
new ChatMessage(refName, referencedMsg.Content));
}
// Build request
LlmRequest request;
if (takeAPicMatch)
{
string rawInput = msg.Content;
if (referencedMsg != null)
rawInput = referencedMsg.Content + "\n" + rawInput;
rawInput = Regex.Replace(rawInput, PingAndChannelTagDetectFilterRegexStr, "");
// Filter image prompt
string imgPrompt = rawInput;
imgPrompt = Regex.Replace(imgPrompt, BannedWordsExact, "");
imgPrompt = Regex.Replace(imgPrompt, BannedWordsExactPic, "");
imgPrompt = Regex.Replace(imgPrompt, BotNameMatchRegexStr, "",
RegexOptions.IgnoreCase);
imgPrompt = imgPrompt.Replace(BotName, "");
string imgDetected = Functions.IsSimilarToBannedWords(imgPrompt, BannedWords);
if (imgDetected.Length > 2)
{
foreach (string w in imgDetected.Split(' '))
{
string wt = w.Trim();
if (wt.Length > 2)
{
imgPrompt = imgPrompt.Replace(wt, "");
if (imgPrompt.Contains(" ")) imgPrompt = imgPrompt.Replace(" ", " ");
}
}
}
request = new LlmRequest
{
SystemPrompt = InputPromptStartPic,
Messages = new[] { new ChatMessage("User", $"### User request: {imgPrompt}") },
CompletionSuffix = InputPromptEndPic,
MaxTokens = 140,
Temperature = 1.0,
};
}
else
{
request = new LlmRequest
{
SystemPrompt = CharacterPrompt,
Messages = messages,
CompletionSuffix = CompletionSuffix,
MaxTokens = 280,
Temperature = 1.0,
};
}
msg.Channel.TriggerTypingAsync();
// Strip control characters (cures emoji psychosis in some models)
// No-op for chat backends but harmless
string? rawReply;
try
{
rawReply = await backend.GenerateAsync(request, _appCts.Token);
}
catch (Exception ex)
{
Console.WriteLine($"| Reply error ({backend.Name}): {ex.Message}");
await TryFallbackBackendAsync();
_llmThinkingTicks = 0;
return;
}
if (string.IsNullOrWhiteSpace(rawReply))
{
Console.WriteLine("| Empty reply from backend.");
_llmThinkingTicks = 0;
return;
}
// Sanitise reply
string replyBanned = Functions.IsSimilarToBannedWords(rawReply, BannedWords);
if (replyBanned.Length > 2)
{
foreach (string w in replyBanned.Split(' '))
{
string wt = w.Trim();
if (wt.Length > 2)
{
rawReply = rawReply.Replace(wt, "");
if (rawReply.Contains(" ")) rawReply = rawReply.Replace(" ", " ");
}
}
Console.WriteLine("| Removed banned or similar words from reply.");
}
// Safety-strip the completion suffix (some text backends echo it)
string trimmed = rawReply.Replace(CompletionSuffix, "").Trim();
var promptEndMatch = Regex.Match(trimmed, PromptEndDetectionRegexStr);
if (takeAPicMatch)
await HandleImageReply(msg, trimmed, promptEndMatch, inputMsgFiltered);
else
await HandleChatReply(msg, trimmed, promptEndMatch, inputMsgFiltered);
_llmThinkingTicks = 0;
}
// ── Image reply ────────────────────────────────────────────────────────
private async Task HandleImageReply(
SocketUserMessage msg,
string trimmed,
Match promptEndMatch,
string inputMsgFiltered)
{
string llmPromptPic = trimmed;
string llmSubsequent = string.Empty;
int endIdx = promptEndMatch.Index;
if (endIdx >= 3)
{
llmPromptPic = trimmed.Substring(0, endIdx);
llmSubsequent = trimmed.Substring(endIdx);
}
await Functions.TakeAPic(msg, llmPromptPic, inputMsgFiltered);
string botImgLine =
$"\n[System]: {BotName} attached an image: {BotName}, " +
$"{Functions.imgFormatString}{llmPromptPic.Replace("\n", ", ")}\n";
Functions.imgFormatString = string.Empty;
_history.Add(new ChatMessage("System", botImgLine.Trim(), isSystem: true));
Console.WriteLine(botImgLine.Trim());
// Any LLM text following the image prompt
if (llmSubsequent.Length > 0 && llmSubsequent.Contains(CompletionSuffix))
{
string afterSuffix = llmSubsequent
.Substring(llmSubsequent.IndexOf(CompletionSuffix) + CompletionSuffix.Length)
.Replace(CompletionSuffix, "")
.Trim();
if (afterSuffix.Length > 0)
await msg.ReplyAsync(afterSuffix);
}
}
// ── Chat reply ─────────────────────────────────────────────────────────
private async Task HandleChatReply(
SocketUserMessage msg,
string trimmed,
Match promptEndMatch,
string inputMsgFiltered)
{
int endIdx = promptEndMatch.Index;
string llmMsg = endIdx > 0 ? trimmed.Substring(0, endIdx) : trimmed;
// Remove tone indicators e.g. "(excited)" at start
llmMsg = Regex.Replace(llmMsg, ToneIndicatorDetector, "").Trim();
// Reject gibberish
bool hasText = Regex.IsMatch(llmMsg, @"[a-zA-Z0-9]");
if (!hasText || llmMsg == promptEndMatch.Value)
{
await HandleLoop(msg, inputMsgFiltered, llmMsg, isGibberish: true);
return;
}
// Similarity-based loop detection
bool botLooping = false;
string loopLineToRemove = string.Empty;
foreach (var line in _history.Messages)
{
if (line.Content.Length > 0 &&
Functions.LevenshteinDistance(
Regex.Replace(llmMsg, @"\s+", ""),
Regex.Replace(line.Content, @"\s+", "")) < llmMsg.Length / 3)
{
Console.WriteLine("| Loop prevention: bot said a very similar sentence.");
loopLineToRemove = line.Content;
botLooping = true;
break;
}
}
if (llmMsg == _botLastReply && _loopCounts < 2)
{
await HandleLoop(msg, inputMsgFiltered, llmMsg, isGibberish: false);
return;
}
else if (llmMsg == _botLastReply && _loopCounts >= 2)
{
_loopCounts = 0;
Console.WriteLine("| Giving up on loop prevention — sending anyway.");
await msg.Channel.SendMessageAsync(llmMsg);
return;
}
else if (botLooping && loopLineToRemove.Length > 0)
{
_history.RemoveWhere(m => m.Content == loopLineToRemove);
_ = Task.Run(() => Reply(msg, inputMsgFiltered), _appCts.Token);
return;
}
string finalMsg = TrimRepeatedEdges(llmMsg);
if (!botLooping)
{
_history.Add(new ChatMessage(BotName, finalMsg, isBot: true));
string botLine = Regex.Replace(
$"{CompletionSuffix}{finalMsg}\n", LinkDetectionRegexStr, "url removed");
Console.WriteLine(botLine.Trim());
}
if (finalMsg.Trim().Length > 0)
{
_botLastReply = finalMsg;
await msg.Channel.SendMessageAsync(finalMsg);
float ratio = (float)trimmed.Length / Math.Max(1, llmMsg.Length);
if (!_longMsgWarningGiven && ratio >= 1.5f)
{
_longMsgWarningGiven = true;
Console.WriteLine(
$"| Warning: raw reply was {ratio:F1}x longer than used portion. " +
"Consider tightening your prompts.");
}
}
_loopCounts = 0;
}
// ── Loop recovery ──────────────────────────────────────────────────────
private async Task HandleLoop(
SocketUserMessage msg, string inputMsgFiltered, string llmMsg, bool isGibberish)
{
_loopCounts++;
if (!isGibberish)
{
var kept = _history.Messages.Skip(2).ToList();
_history.Clear();
foreach (var m in kept) _history.Add(m);
_history.ReplaceInAll(_botLastReply, "");
Console.WriteLine(
$"| Duplicate reply detected — cleared history and retrying.\n Msg: {llmMsg}");
}
else
{
Console.WriteLine($"| Gibberish reply — retrying.\n Msg: {llmMsg}");
}
_ = Task.Run(() => Reply(msg, inputMsgFiltered), _appCts.Token);
}
// ── Repeated-edge trim ─────────────────────────────────────────────────
private string TrimRepeatedEdges(string msg)
{
if (string.IsNullOrEmpty(_botLastReply) || string.IsNullOrEmpty(msg))
return msg;
int prefixLen = 1;
while (prefixLen < msg.Length && prefixLen < _botLastReply.Length &&
string.Compare(msg, 0, _botLastReply, 0, prefixLen,
StringComparison.OrdinalIgnoreCase) == 0)
prefixLen++;
return prefixLen > 3 ? msg[(prefixLen - 1)..] : msg;
}
// ── Fallback backend ───────────────────────────────────────────────────
private static async Task TryFallbackBackendAsync()
{
Console.WriteLine("| Searching for fallback backend...");
var next = await BackendDetector.DetectAsync(BuildBackendCandidates());
if (next != null)
{
Console.WriteLine($"| Switched to fallback: {next.Name}");
MainGlobal.ActiveBackend = next;
}
}
}
}