156 lines
5.1 KiB
Go
156 lines
5.1 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/messaging"
|
|
)
|
|
|
|
// TitleGenerator proposes a title for an untitled session. It must not write
|
|
// to the store. An empty title declines; errors leave the session untitled.
|
|
type TitleGenerator interface {
|
|
GenerateTitle(ctx context.Context, sessionID, message, reply string) (string, error)
|
|
}
|
|
|
|
// titleReplyBytes bounds how much of the reply a title proposal reads: the
|
|
// opening of a reply names its subject, and a title is not worth more tokens.
|
|
const titleReplyBytes = 2000
|
|
|
|
// clipTitleReply cuts reply to titleReplyBytes on a rune boundary.
|
|
func clipTitleReply(reply string) string {
|
|
if len(reply) <= titleReplyBytes {
|
|
return reply
|
|
}
|
|
return strings.ToValidUTF8(reply[:titleReplyBytes], "")
|
|
}
|
|
|
|
// maxTitleRunes caps how long a generated session title may be. Titles
|
|
// stay short enough to render on one line in a chat bubble.
|
|
const maxTitleRunes = 60
|
|
|
|
// titleGenerationTimeout bounds one title proposal. Titles are cosmetic;
|
|
// a stuck model call must not pin a goroutine forever.
|
|
const titleGenerationTimeout = 30 * time.Second
|
|
|
|
// maybeAutoTitle proposes a title in the background after a successful turn
|
|
// on an untitled session, from the triggering message and its reply.
|
|
func (r *Router) maybeAutoTitle(ctx context.Context, msg messaging.Message, reply string) {
|
|
if r.Titles == nil || r.sessionTracker == nil {
|
|
return
|
|
}
|
|
sessionID := r.sessionTracker.getActive(ctx, msg.ConversationID.ChannelID, msg.ConversationID.ThreadID)
|
|
if sessionID == "" || !r.claimTitleInFlight(sessionID) {
|
|
return
|
|
}
|
|
go r.generateTitle(context.WithoutCancel(ctx), sessionID, msg.Text, clipTitleReply(reply))
|
|
}
|
|
|
|
// generateTitle runs one title proposal to completion: ask the
|
|
// generator, then persist the result if the session is still untitled.
|
|
func (r *Router) generateTitle(ctx context.Context, sessionID, message, reply string) {
|
|
defer r.releaseTitleInFlight(sessionID)
|
|
|
|
// A proposal runs outside the turn lane, which is where the repo
|
|
// recovers panics for turns; a panic here would take the daemon down
|
|
// over a cosmetic title. Contain it: the title is optional, and a
|
|
// panic only loses that title.
|
|
defer func() {
|
|
if p := recover(); p != nil {
|
|
if r.Log != nil {
|
|
r.Log.ErrorContext(ctx, "session title generation panicked", "session", sessionID, "panic", p)
|
|
}
|
|
}
|
|
}()
|
|
|
|
gctx, cancel := context.WithTimeout(ctx, titleGenerationTimeout)
|
|
defer cancel()
|
|
|
|
// A title may have landed since the turn (a manual /title, or a
|
|
// racing proposal that won the slot earlier and finished first). Ask
|
|
// for one only if the session still has none -- the proposal is the
|
|
// expensive part and must never run against a titled session.
|
|
sc, err := r.sessionTracker.sessions.Get(gctx, sessionID)
|
|
if err != nil {
|
|
r.debugTitle(sessionID, "session title skipped: read failed", err)
|
|
return
|
|
}
|
|
if sc == nil || sc.Title != "" {
|
|
return
|
|
}
|
|
title, err := r.Titles.GenerateTitle(gctx, sessionID, message, reply)
|
|
if err != nil {
|
|
return
|
|
}
|
|
// NONE leaves the session untitled, so the next turn's message is asked.
|
|
title = cleanGeneratedTitle(title)
|
|
if title == "" || strings.EqualFold(title, "none") {
|
|
return
|
|
}
|
|
// Re-read so a manual /title made meanwhile wins.
|
|
sc, err = r.sessionTracker.sessions.Get(gctx, sessionID)
|
|
if err != nil {
|
|
r.debugTitle(sessionID, "session title skipped: re-read failed", err)
|
|
return
|
|
}
|
|
if sc == nil || sc.Title != "" {
|
|
return
|
|
}
|
|
sc.Title = title
|
|
if err := r.sessionTracker.sessions.Save(gctx, *sc); err != nil {
|
|
r.debugTitle(sessionID, "session title not persisted", err)
|
|
}
|
|
}
|
|
|
|
// debugTitle logs a background title failure at debug level, if a logger is
|
|
// set.
|
|
func (r *Router) debugTitle(sessionID, msg string, err error) {
|
|
if r.Log != nil {
|
|
r.Log.Debug(msg, "session", sessionID, "err", err)
|
|
}
|
|
}
|
|
|
|
// claimTitleInFlight records that a title proposal is running for
|
|
// sessionID, reporting whether this call won the slot. Concurrent turns
|
|
// in one untitled session would otherwise each spawn a proposal; only
|
|
// the first one does, and the rest skip.
|
|
func (r *Router) claimTitleInFlight(sessionID string) bool {
|
|
r.titlingMu.Lock()
|
|
defer r.titlingMu.Unlock()
|
|
if r.titling == nil {
|
|
r.titling = make(map[string]struct{})
|
|
}
|
|
if _, ok := r.titling[sessionID]; ok {
|
|
return false
|
|
}
|
|
r.titling[sessionID] = struct{}{}
|
|
return true
|
|
}
|
|
|
|
// releaseTitleInFlight drops the in-flight marker once a proposal has
|
|
// finished, so a later turn can try again (the proposal may have failed).
|
|
func (r *Router) releaseTitleInFlight(sessionID string) {
|
|
r.titlingMu.Lock()
|
|
defer r.titlingMu.Unlock()
|
|
delete(r.titling, sessionID)
|
|
}
|
|
|
|
// cleanGeneratedTitle trims a generated title to one line without quotes or a
|
|
// trailing period, or returns "" if unusable.
|
|
func cleanGeneratedTitle(text string) string {
|
|
s := strings.Join(strings.Fields(text), " ")
|
|
s = strings.TrimSpace(s)
|
|
if len(s) >= 2 {
|
|
if (s[0] == '"' && s[len(s)-1] == '"') || (s[0] == '\'' && s[len(s)-1] == '\'') {
|
|
s = strings.TrimSpace(s[1 : len(s)-1])
|
|
}
|
|
}
|
|
s = strings.TrimSuffix(s, ".")
|
|
s = strings.TrimSpace(s)
|
|
if r := []rune(s); len(r) > maxTitleRunes {
|
|
s = string(r[:maxTitleRunes]) + "…"
|
|
}
|
|
return s
|
|
}
|