feat(chat): stop active llm responses
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user