mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
a04b8fd564
- Rename `llm_providers` → `providers`, `llm_provider_oauth_tokens` → `provider_oauth_tokens` - Remove `tts_providers` and `tts_models` tables; speech models now live in the unified `models` table with `type = 'speech'` - Replace top-level `api_key`/`base_url` columns with a JSONB `config` field on `providers` - Rename `llm_provider_id` → `provider_id` across all references - Add `edge-speech` client type and `conf/providers/edge.yaml` default provider - Create new read-only speech endpoints (`/speech-providers`, `/speech-models`) backed by filtered views of the unified tables - Remove old TTS CRUD handlers; simplify speech page to read-only + test - Update registry loader to skip malformed YAML files instead of failing entirely - Fix YAML quoting for model names containing colons in openrouter.yaml - Regenerate sqlc, swagger, and TypeScript SDK
71 lines
1.9 KiB
Go
71 lines
1.9 KiB
Go
package flow
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
|
|
"github.com/memohai/memoh/internal/compaction"
|
|
"github.com/memohai/memoh/internal/conversation"
|
|
"github.com/memohai/memoh/internal/models"
|
|
"github.com/memohai/memoh/internal/providers"
|
|
)
|
|
|
|
func (r *Resolver) maybeCompact(ctx context.Context, req conversation.ChatRequest, rc resolvedContext, inputTokens int) {
|
|
if r.compactionService == nil || r.settingsService == nil {
|
|
return
|
|
}
|
|
settings, err := r.settingsService.GetBot(ctx, req.BotID)
|
|
if err != nil {
|
|
r.logger.Warn("compaction: failed to load settings", slog.Any("error", err))
|
|
return
|
|
}
|
|
if !settings.CompactionEnabled || settings.CompactionThreshold <= 0 {
|
|
return
|
|
}
|
|
if !compaction.ShouldCompact(inputTokens, settings.CompactionThreshold) {
|
|
return
|
|
}
|
|
|
|
modelID := settings.CompactionModelID
|
|
if modelID == "" {
|
|
modelID = rc.model.ID
|
|
}
|
|
|
|
ratio := settings.CompactionRatio
|
|
if ratio <= 0 || ratio > 100 {
|
|
ratio = 80
|
|
}
|
|
|
|
cfg := compaction.TriggerConfig{
|
|
BotID: req.BotID,
|
|
SessionID: req.SessionID,
|
|
Ratio: ratio,
|
|
TotalInputTokens: inputTokens,
|
|
}
|
|
|
|
model, err := r.modelsService.GetByID(ctx, modelID)
|
|
if err != nil {
|
|
r.logger.Warn("compaction: failed to resolve model", slog.Any("error", err))
|
|
return
|
|
}
|
|
cfg.ModelID = model.ModelID
|
|
|
|
provider, err := models.FetchProviderByID(ctx, r.queries, model.ProviderID)
|
|
if err != nil {
|
|
r.logger.Warn("compaction: failed to fetch provider", slog.Any("error", err))
|
|
return
|
|
}
|
|
authResolver := providers.NewService(nil, r.queries, "")
|
|
creds, err := authResolver.ResolveModelCredentials(ctx, provider)
|
|
if err != nil {
|
|
r.logger.Warn("compaction: failed to resolve provider credentials", slog.Any("error", err))
|
|
return
|
|
}
|
|
cfg.ClientType = provider.ClientType
|
|
cfg.APIKey = creds.APIKey
|
|
cfg.CodexAccountID = creds.CodexAccountID
|
|
cfg.BaseURL = providers.ProviderConfigString(provider, "base_url")
|
|
|
|
r.compactionService.TriggerCompaction(ctx, cfg)
|
|
}
|