refactor: replace context_token_budget with model context_window for context trimming

The per-bot context_token_budget column was unused (no frontend UI) and
has been removed. Context trimming now derives the budget from the chat
model's context_window setting, which is already configured per model.
This commit is contained in:
Acbox
2026-04-14 21:04:42 +08:00
parent cb44408277
commit 84f1d0612a
10 changed files with 16 additions and 42 deletions
+2 -3
View File
@@ -287,10 +287,9 @@ func (r *Resolver) resolve(ctx context.Context, req conversation.ChatRequest) (r
}
}
botSettings, _ := r.loadBotSettings(ctx, req.BotID)
contextTokenBudget := 0
if botSettings.ContextTokenBudget > 0 {
contextTokenBudget = botSettings.ContextTokenBudget
if chatModel.Config.ContextWindow != nil && *chatModel.Config.ContextWindow > 0 {
contextTokenBudget = *chatModel.Config.ContextWindow
}
var messages []conversation.ModelMessage
+1 -1
View File
@@ -511,7 +511,7 @@ WITH updated AS (
SET display_name = $1,
updated_at = now()
WHERE bots.id = $2
RETURNING id, owner_user_id, display_name, avatar_url, timezone, is_active, status, language, reasoning_enabled, reasoning_effort, chat_model_id, search_provider_id, memory_provider_id, heartbeat_enabled, heartbeat_interval, heartbeat_prompt, heartbeat_model_id, compaction_enabled, compaction_threshold, compaction_ratio, compaction_model_id, title_model_id, image_model_id, discuss_probe_model_id, tts_model_id, browser_context_id, context_token_budget, persist_full_tool_results, metadata, created_at, updated_at, acl_default_effect
RETURNING id, owner_user_id, display_name, avatar_url, timezone, is_active, status, language, reasoning_enabled, reasoning_effort, chat_model_id, search_provider_id, memory_provider_id, heartbeat_enabled, heartbeat_interval, heartbeat_prompt, heartbeat_model_id, compaction_enabled, compaction_threshold, compaction_ratio, compaction_model_id, title_model_id, image_model_id, discuss_probe_model_id, tts_model_id, browser_context_id, persist_full_tool_results, metadata, created_at, updated_at, acl_default_effect
)
SELECT
updated.id AS id,
-1
View File
@@ -35,7 +35,6 @@ type Bot struct {
DiscussProbeModelID pgtype.UUID `json:"discuss_probe_model_id"`
TtsModelID pgtype.UUID `json:"tts_model_id"`
BrowserContextID pgtype.UUID `json:"browser_context_id"`
ContextTokenBudget pgtype.Int4 `json:"context_token_budget"`
PersistFullToolResults bool `json:"persist_full_tool_results"`
Metadata []byte `json:"metadata"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
+3 -13
View File
@@ -31,7 +31,6 @@ SET language = 'auto',
memory_provider_id = NULL,
tts_model_id = NULL,
browser_context_id = NULL,
context_token_budget = NULL,
persist_full_tool_results = false,
updated_at = now()
WHERE id = $1
@@ -64,7 +63,6 @@ SELECT
image_models.id AS image_model_id,
tts_models.id AS tts_model_id,
browser_contexts.id AS browser_context_id,
bots.context_token_budget,
bots.persist_full_tool_results
FROM bots
LEFT JOIN models AS chat_models ON chat_models.id = bots.chat_model_id
@@ -100,7 +98,6 @@ type GetSettingsByBotIDRow struct {
ImageModelID pgtype.UUID `json:"image_model_id"`
TtsModelID pgtype.UUID `json:"tts_model_id"`
BrowserContextID pgtype.UUID `json:"browser_context_id"`
ContextTokenBudget pgtype.Int4 `json:"context_token_budget"`
PersistFullToolResults bool `json:"persist_full_tool_results"`
}
@@ -128,7 +125,6 @@ func (q *Queries) GetSettingsByBotID(ctx context.Context, id pgtype.UUID) (GetSe
&i.ImageModelID,
&i.TtsModelID,
&i.BrowserContextID,
&i.ContextTokenBudget,
&i.PersistFullToolResults,
)
return i, err
@@ -156,11 +152,10 @@ WITH updated AS (
image_model_id = COALESCE($17::uuid, bots.image_model_id),
tts_model_id = COALESCE($18::uuid, bots.tts_model_id),
browser_context_id = COALESCE($19::uuid, bots.browser_context_id),
context_token_budget = COALESCE($20::integer, bots.context_token_budget),
persist_full_tool_results = $21,
persist_full_tool_results = $20,
updated_at = now()
WHERE bots.id = $22
RETURNING bots.id, bots.language, bots.reasoning_enabled, bots.reasoning_effort, bots.heartbeat_enabled, bots.heartbeat_interval, bots.heartbeat_prompt, bots.compaction_enabled, bots.compaction_threshold, bots.compaction_ratio, bots.timezone, bots.chat_model_id, bots.heartbeat_model_id, bots.compaction_model_id, bots.title_model_id, bots.image_model_id, bots.search_provider_id, bots.memory_provider_id, bots.tts_model_id, bots.browser_context_id, bots.context_token_budget, bots.persist_full_tool_results
WHERE bots.id = $21
RETURNING bots.id, bots.language, bots.reasoning_enabled, bots.reasoning_effort, bots.heartbeat_enabled, bots.heartbeat_interval, bots.heartbeat_prompt, bots.compaction_enabled, bots.compaction_threshold, bots.compaction_ratio, bots.timezone, bots.chat_model_id, bots.heartbeat_model_id, bots.compaction_model_id, bots.title_model_id, bots.image_model_id, bots.search_provider_id, bots.memory_provider_id, bots.tts_model_id, bots.browser_context_id, bots.persist_full_tool_results
)
SELECT
updated.id AS bot_id,
@@ -183,7 +178,6 @@ SELECT
image_models.id AS image_model_id,
tts_models.id AS tts_model_id,
browser_contexts.id AS browser_context_id,
updated.context_token_budget,
updated.persist_full_tool_results
FROM updated
LEFT JOIN models AS chat_models ON chat_models.id = updated.chat_model_id
@@ -217,7 +211,6 @@ type UpsertBotSettingsParams struct {
ImageModelID pgtype.UUID `json:"image_model_id"`
TtsModelID pgtype.UUID `json:"tts_model_id"`
BrowserContextID pgtype.UUID `json:"browser_context_id"`
ContextTokenBudget pgtype.Int4 `json:"context_token_budget"`
PersistFullToolResults bool `json:"persist_full_tool_results"`
ID pgtype.UUID `json:"id"`
}
@@ -243,7 +236,6 @@ type UpsertBotSettingsRow struct {
ImageModelID pgtype.UUID `json:"image_model_id"`
TtsModelID pgtype.UUID `json:"tts_model_id"`
BrowserContextID pgtype.UUID `json:"browser_context_id"`
ContextTokenBudget pgtype.Int4 `json:"context_token_budget"`
PersistFullToolResults bool `json:"persist_full_tool_results"`
}
@@ -268,7 +260,6 @@ func (q *Queries) UpsertBotSettings(ctx context.Context, arg UpsertBotSettingsPa
arg.ImageModelID,
arg.TtsModelID,
arg.BrowserContextID,
arg.ContextTokenBudget,
arg.PersistFullToolResults,
arg.ID,
)
@@ -294,7 +285,6 @@ func (q *Queries) UpsertBotSettings(ctx context.Context, arg UpsertBotSettingsPa
&i.ImageModelID,
&i.TtsModelID,
&i.BrowserContextID,
&i.ContextTokenBudget,
&i.PersistFullToolResults,
)
return i, err
-16
View File
@@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"log/slog"
"math"
"strings"
"github.com/google/uuid"
@@ -184,14 +183,6 @@ func (s *Service) UpsertBot(ctx context.Context, botID string, req UpsertRequest
}
browserContextUUID = ctxID
}
contextTokenBudgetValue := pgtype.Int4{}
if req.ContextTokenBudget != nil && *req.ContextTokenBudget >= 0 {
v := *req.ContextTokenBudget
if v > math.MaxInt32 {
v = math.MaxInt32
}
contextTokenBudgetValue = pgtype.Int4{Int32: int32(v), Valid: true} //nolint:gosec // G115: clamped above
}
updated, err := s.queries.UpsertBotSettings(ctx, sqlc.UpsertBotSettingsParams{
ID: pgID,
@@ -214,7 +205,6 @@ func (s *Service) UpsertBot(ctx context.Context, botID string, req UpsertRequest
MemoryProviderID: memoryProviderUUID,
TtsModelID: ttsModelUUID,
BrowserContextID: browserContextUUID,
ContextTokenBudget: contextTokenBudgetValue,
PersistFullToolResults: current.PersistFullToolResults,
})
if err != nil {
@@ -309,7 +299,6 @@ func normalizeBotSettingsReadRow(row sqlc.GetSettingsByBotIDRow) Settings {
row.MemoryProviderID,
row.TtsModelID,
row.BrowserContextID,
row.ContextTokenBudget,
row.PersistFullToolResults,
)
}
@@ -334,7 +323,6 @@ func normalizeBotSettingsWriteRow(row sqlc.UpsertBotSettingsRow) Settings {
row.MemoryProviderID,
row.TtsModelID,
row.BrowserContextID,
row.ContextTokenBudget,
row.PersistFullToolResults,
)
}
@@ -358,7 +346,6 @@ func normalizeBotSettingsFields(
memoryProviderID pgtype.UUID,
ttsModelID pgtype.UUID,
browserContextID pgtype.UUID,
contextTokenBudget pgtype.Int4,
persistFullToolResults bool,
) Settings {
settings := normalizeBotSetting(language, "", reasoningEnabled, reasoningEffort, heartbeatEnabled, heartbeatInterval, compactionEnabled, compactionThreshold, compactionRatio)
@@ -392,9 +379,6 @@ func normalizeBotSettingsFields(
if browserContextID.Valid {
settings.BrowserContextID = uuid.UUID(browserContextID.Bytes).String()
}
if contextTokenBudget.Valid {
settings.ContextTokenBudget = int(contextTokenBudget.Int32)
}
settings.PersistFullToolResults = persistFullToolResults
return settings
}
-2
View File
@@ -27,7 +27,6 @@ type Settings struct {
CompactionRatio int `json:"compaction_ratio"`
CompactionModelID string `json:"compaction_model_id,omitempty"`
DiscussProbeModelID string `json:"discuss_probe_model_id,omitempty"`
ContextTokenBudget int `json:"context_token_budget"`
PersistFullToolResults bool `json:"persist_full_tool_results"`
}
@@ -52,6 +51,5 @@ type UpsertRequest struct {
CompactionRatio *int `json:"compaction_ratio,omitempty"`
CompactionModelID *string `json:"compaction_model_id,omitempty"`
DiscussProbeModelID string `json:"discuss_probe_model_id,omitempty"`
ContextTokenBudget *int `json:"context_token_budget,omitempty"`
PersistFullToolResults *bool `json:"persist_full_tool_results,omitempty"`
}