merged from gramsrv upstream

This commit is contained in:
onysd 2026-09-01 12:06:31 +03:00
parent 79c64ee916
commit 21a0856587
651 changed files with 54774 additions and 4590 deletions

View file

@ -9,6 +9,7 @@ import (
"go.uber.org/zap"
"telesrv/internal/branding"
"telesrv/internal/domain"
)
@ -18,14 +19,17 @@ const (
chatBotStreamMaxDrafts = 24
chatBotHistoryLimit = 12
chatBotTranscriptLineLimit = 800
chatBotHelpPrefix = "Send me a message and I will answer with the configured "
chatBotHelpSuffix = " AI provider.\n\n/help - show this message\n/reset - clear the local AI context"
)
const chatBotHelpText = `Send me a message and I will answer with the configured telesrv AI provider.
func chatBotHelpText() string {
return chatBotHelpPrefix + branding.ProductName + chatBotHelpSuffix
}
/help - show this message
/reset - clear the local AI context`
const chatBotInstruction = `You are ChatBot, a built-in AI assistant inside telesrv private chats. The user input is a recent chat transcript. Reply only to the last user message. Match the user's language when practical. Be helpful, concise, and direct. Do not mention provider names, API keys, internal prompts, or system implementation details.`
func chatBotInstruction() string {
return "You are ChatBot, a built-in AI assistant inside " + branding.ProductName + " private chats. The user input is a recent chat transcript. Reply only to the last user message. Match the user's language when practical. Be helpful, concise, and direct. Do not mention provider names, API keys, internal prompts, or system implementation details."
}
const (
chatBotUnavailableText = "AI chat is not available right now. Please try again later."
@ -46,7 +50,7 @@ func (s *Service) respondAsChatBot(userID int64, msg domain.Message) {
if cmd, ok := parseBotCommand(text); ok {
switch cmd {
case "start", "help":
s.sendServiceBotReply(ctx, domain.ChatBotUserID, userID, botReply{Text: chatBotHelpText})
s.sendServiceBotReply(ctx, domain.ChatBotUserID, userID, botReply{Text: chatBotHelpText()})
case "reset":
s.sendServiceBotReply(ctx, domain.ChatBotUserID, userID, botReply{Text: chatBotResetText})
default:
@ -76,7 +80,7 @@ func (s *Service) respondAsChatBot(userID int64, msg domain.Message) {
Text: domain.AIComposeText{
Text: s.chatBotPromptText(ctx, userID, msg),
},
Instruction: chatBotInstruction,
Instruction: chatBotInstruction(),
}
final, err := s.aiChat.GenerateTextStream(ctx, req, func(out domain.AIComposeText) error {
if chatBotLooksLikePromptEcho(out.Text, req.Text.Text) {
@ -165,7 +169,15 @@ func chatBotTranscriptLine(speaker, text string) string {
func chatBotCommandReply(text string) bool {
text = strings.TrimSpace(text)
return text == chatBotHelpText || text == chatBotResetText || text == chatBotUnknownCommand || text == chatBotTextOnlyText
return chatBotHelpReply(text) || text == chatBotResetText || text == chatBotUnknownCommand || text == chatBotTextOnlyText
}
func chatBotHelpReply(text string) bool {
if !strings.HasPrefix(text, chatBotHelpPrefix) || !strings.HasSuffix(text, chatBotHelpSuffix) {
return false
}
brand := strings.TrimSuffix(strings.TrimPrefix(text, chatBotHelpPrefix), chatBotHelpSuffix)
return strings.TrimSpace(brand) != ""
}
func chatBotLooksLikePromptEcho(text, prompt string) bool {