feat(chat): stop active llm responses

This commit is contained in:
2026-05-02 16:26:10 -04:00
parent b5e60ff0e2
commit f359471a27
11 changed files with 130 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
package api
import (
"context"
"errors"
"slices"
"sync"
@@ -19,6 +20,8 @@ type generationManager struct {
type generation struct {
mu sync.RWMutex
ctx context.Context
cancel context.CancelFunc
subscribers map[chan *MessageChunk]struct{}
done chan struct{}
closed bool
@@ -36,7 +39,10 @@ func (m *generationManager) start(chatID uuid.UUID, prepare func(*generation) er
}
// Reserve Generation
ctx, cancel := context.WithCancel(context.Background())
gen := &generation{
ctx: ctx,
cancel: cancel,
subscribers: make(map[chan *MessageChunk]struct{}),
done: make(chan struct{}),
}
@@ -79,6 +85,19 @@ func (m *generationManager) subscribe(chatID uuid.UUID) (<-chan *MessageChunk, f
return ch, func() { gen.unsubscribe(ch) }, true
}
func (m *generationManager) stop(chatID uuid.UUID) bool {
m.mu.RLock()
gen, found := m.generations[chatID]
m.mu.RUnlock()
if !found {
return false
}
// Cancel Generation
gen.cancel()
return true
}
func (g *generation) subscribe() chan *MessageChunk {
ch := make(chan *MessageChunk, 64)
@@ -126,6 +145,7 @@ func (g *generation) close() {
return
}
g.closed = true
g.cancel()
close(g.done)
for subscriber := range g.subscribers {
close(subscriber)