Fix: signal that llm is done

This commit is contained in:
Grail Finder
2025-05-17 13:33:00 +03:00
parent d05d904747
commit f7d1fbf73c
2 changed files with 29 additions and 8 deletions

10
bot.go
View File

@@ -334,12 +334,6 @@ func chatRound(userMsg, role string, tv *tview.TextView, regen, resume bool) {
} }
} }
respText := strings.Builder{} respText := strings.Builder{}
// if tts is enabled
// var audioStream *extra.AudioStream
// if cfg.TTS_ENABLED {
// audioStream = extra.RunOrator(orator)
// // defer close(audioStream.DoneChan)
// }
out: out:
for { for {
select { select {
@@ -354,6 +348,10 @@ out:
} }
case <-streamDone: case <-streamDone:
botRespMode = false botRespMode = false
if cfg.TTS_ENABLED {
// audioStream.TextChan <- chunk
extra.TTSFlushChan <- true
}
break out break out
} }
} }

View File

@@ -8,6 +8,7 @@ import (
"io" "io"
"log/slog" "log/slog"
"net/http" "net/http"
"strings"
"time" "time"
"github.com/gopxl/beep" "github.com/gopxl/beep"
@@ -17,8 +18,9 @@ import (
) )
var ( var (
TTSTextChan = make(chan string, 1000) TTSTextChan = make(chan string, 1000)
TTSDoneChan = make(chan bool, 1) TTSFlushChan = make(chan bool, 1)
TTSDoneChan = make(chan bool, 1)
) )
type Orator interface { type Orator interface {
@@ -39,6 +41,7 @@ type KokoroOrator struct {
func readroutine(orator Orator) { func readroutine(orator Orator) {
tokenizer, _ := english.NewSentenceTokenizer(nil) tokenizer, _ := english.NewSentenceTokenizer(nil)
var sentenceBuf bytes.Buffer var sentenceBuf bytes.Buffer
var remainder strings.Builder
for { for {
select { select {
case chunk := <-TTSTextChan: case chunk := <-TTSTextChan:
@@ -56,6 +59,26 @@ func readroutine(orator Orator) {
orator.GetLogger().Error("tts failed", "sentence", sentence.Text, "error", err) orator.GetLogger().Error("tts failed", "sentence", sentence.Text, "error", err)
} }
} }
case <-TTSFlushChan:
// lln is done get the whole message out
// FIXME: loses one token
for chunk := range TTSTextChan {
// orator.GetLogger().Info("flushing", "chunk", chunk)
// sentenceBuf.WriteString(chunk)
remainder.WriteString(chunk) // I get text here
if len(TTSTextChan) == 0 {
break
}
}
// Flush remaining text
remaining := remainder.String()
orator.GetLogger().Info("flushing", "rem", remaining)
if remaining != "" { // but nothing is here?
orator.GetLogger().Info("flushing", "remaining", remaining)
if err := orator.Speak(remaining); err != nil {
orator.GetLogger().Error("tts failed", "sentence", remaining, "error", err)
}
}
case <-TTSDoneChan: case <-TTSDoneChan:
// Flush remaining text // Flush remaining text
if remaining := sentenceBuf.String(); remaining != "" { if remaining := sentenceBuf.String(); remaining != "" {