feat: max context tokens

This commit is contained in:
Acbox
2026-02-18 17:20:22 +08:00
parent 46e8a48ab8
commit 77e9f585a1
26 changed files with 277 additions and 51 deletions
+12 -6
View File
@@ -14,7 +14,7 @@ import (
const createBot = `-- name: CreateBot :one
INSERT INTO bots (owner_user_id, type, display_name, avatar_url, is_active, metadata, status)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, max_context_tokens, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
`
type CreateBotParams struct {
@@ -47,6 +47,7 @@ func (q *Queries) CreateBot(ctx context.Context, arg CreateBotParams) (Bot, erro
&i.IsActive,
&i.Status,
&i.MaxContextLoadTime,
&i.MaxContextTokens,
&i.Language,
&i.AllowGuest,
&i.ChatModelID,
@@ -84,7 +85,7 @@ func (q *Queries) DeleteBotMember(ctx context.Context, arg DeleteBotMemberParams
}
const getBotByID = `-- name: GetBotByID :one
SELECT id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
SELECT id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, max_context_tokens, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
FROM bots
WHERE id = $1
`
@@ -101,6 +102,7 @@ func (q *Queries) GetBotByID(ctx context.Context, id pgtype.UUID) (Bot, error) {
&i.IsActive,
&i.Status,
&i.MaxContextLoadTime,
&i.MaxContextTokens,
&i.Language,
&i.AllowGuest,
&i.ChatModelID,
@@ -171,7 +173,7 @@ func (q *Queries) ListBotMembers(ctx context.Context, botID pgtype.UUID) ([]BotM
}
const listBotsByMember = `-- name: ListBotsByMember :many
SELECT b.id, b.owner_user_id, b.type, b.display_name, b.avatar_url, b.is_active, b.status, b.max_context_load_time, b.language, b.allow_guest, b.chat_model_id, b.memory_model_id, b.embedding_model_id, b.search_provider_id, b.metadata, b.created_at, b.updated_at
SELECT b.id, b.owner_user_id, b.type, b.display_name, b.avatar_url, b.is_active, b.status, b.max_context_load_time, b.max_context_tokens, b.language, b.allow_guest, b.chat_model_id, b.memory_model_id, b.embedding_model_id, b.search_provider_id, b.metadata, b.created_at, b.updated_at
FROM bots b
JOIN bot_members m ON m.bot_id = b.id
WHERE m.user_id = $1
@@ -196,6 +198,7 @@ func (q *Queries) ListBotsByMember(ctx context.Context, userID pgtype.UUID) ([]B
&i.IsActive,
&i.Status,
&i.MaxContextLoadTime,
&i.MaxContextTokens,
&i.Language,
&i.AllowGuest,
&i.ChatModelID,
@@ -217,7 +220,7 @@ func (q *Queries) ListBotsByMember(ctx context.Context, userID pgtype.UUID) ([]B
}
const listBotsByOwner = `-- name: ListBotsByOwner :many
SELECT id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
SELECT id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, max_context_tokens, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
FROM bots
WHERE owner_user_id = $1
ORDER BY created_at DESC
@@ -241,6 +244,7 @@ func (q *Queries) ListBotsByOwner(ctx context.Context, ownerUserID pgtype.UUID)
&i.IsActive,
&i.Status,
&i.MaxContextLoadTime,
&i.MaxContextTokens,
&i.Language,
&i.AllowGuest,
&i.ChatModelID,
@@ -266,7 +270,7 @@ UPDATE bots
SET owner_user_id = $2,
updated_at = now()
WHERE id = $1
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, max_context_tokens, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
`
type UpdateBotOwnerParams struct {
@@ -286,6 +290,7 @@ func (q *Queries) UpdateBotOwner(ctx context.Context, arg UpdateBotOwnerParams)
&i.IsActive,
&i.Status,
&i.MaxContextLoadTime,
&i.MaxContextTokens,
&i.Language,
&i.AllowGuest,
&i.ChatModelID,
@@ -307,7 +312,7 @@ SET display_name = $2,
metadata = $5,
updated_at = now()
WHERE id = $1
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, max_context_tokens, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
`
type UpdateBotProfileParams struct {
@@ -336,6 +341,7 @@ func (q *Queries) UpdateBotProfile(ctx context.Context, arg UpdateBotProfilePara
&i.IsActive,
&i.Status,
&i.MaxContextLoadTime,
&i.MaxContextTokens,
&i.Language,
&i.AllowGuest,
&i.ChatModelID,
+1 -1
View File
@@ -590,7 +590,7 @@ WITH updated AS (
SET display_name = $1,
updated_at = now()
WHERE bots.id = $2
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, max_context_tokens, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, search_provider_id, metadata, created_at, updated_at
)
SELECT
updated.id AS id,
+21 -2
View File
@@ -22,7 +22,8 @@ INSERT INTO bot_history_messages (
source_reply_to_message_id,
role,
content,
metadata
metadata,
usage
)
VALUES (
$1,
@@ -34,7 +35,8 @@ VALUES (
$7::text,
$8,
$9,
$10
$10,
$11
)
RETURNING
id,
@@ -48,6 +50,7 @@ RETURNING
role,
content,
metadata,
usage,
created_at
`
@@ -62,6 +65,7 @@ type CreateMessageParams struct {
Role string `json:"role"`
Content []byte `json:"content"`
Metadata []byte `json:"metadata"`
Usage []byte `json:"usage"`
}
type CreateMessageRow struct {
@@ -76,6 +80,7 @@ type CreateMessageRow struct {
Role string `json:"role"`
Content []byte `json:"content"`
Metadata []byte `json:"metadata"`
Usage []byte `json:"usage"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
@@ -91,6 +96,7 @@ func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (C
arg.Role,
arg.Content,
arg.Metadata,
arg.Usage,
)
var i CreateMessageRow
err := row.Scan(
@@ -105,6 +111,7 @@ func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (C
&i.Role,
&i.Content,
&i.Metadata,
&i.Usage,
&i.CreatedAt,
)
return i, err
@@ -133,6 +140,7 @@ SELECT
m.role,
m.content,
m.metadata,
m.usage,
m.created_at,
ci.display_name AS sender_display_name,
ci.avatar_url AS sender_avatar_url
@@ -155,6 +163,7 @@ type ListMessagesRow struct {
Role string `json:"role"`
Content []byte `json:"content"`
Metadata []byte `json:"metadata"`
Usage []byte `json:"usage"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
SenderDisplayName pgtype.Text `json:"sender_display_name"`
SenderAvatarUrl pgtype.Text `json:"sender_avatar_url"`
@@ -181,6 +190,7 @@ func (q *Queries) ListMessages(ctx context.Context, botID pgtype.UUID) ([]ListMe
&i.Role,
&i.Content,
&i.Metadata,
&i.Usage,
&i.CreatedAt,
&i.SenderDisplayName,
&i.SenderAvatarUrl,
@@ -208,6 +218,7 @@ SELECT
m.role,
m.content,
m.metadata,
m.usage,
m.created_at,
ci.display_name AS sender_display_name,
ci.avatar_url AS sender_avatar_url
@@ -237,6 +248,7 @@ type ListMessagesBeforeRow struct {
Role string `json:"role"`
Content []byte `json:"content"`
Metadata []byte `json:"metadata"`
Usage []byte `json:"usage"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
SenderDisplayName pgtype.Text `json:"sender_display_name"`
SenderAvatarUrl pgtype.Text `json:"sender_avatar_url"`
@@ -263,6 +275,7 @@ func (q *Queries) ListMessagesBefore(ctx context.Context, arg ListMessagesBefore
&i.Role,
&i.Content,
&i.Metadata,
&i.Usage,
&i.CreatedAt,
&i.SenderDisplayName,
&i.SenderAvatarUrl,
@@ -290,6 +303,7 @@ SELECT
m.role,
m.content,
m.metadata,
m.usage,
m.created_at,
ci.display_name AS sender_display_name,
ci.avatar_url AS sender_avatar_url
@@ -317,6 +331,7 @@ type ListMessagesLatestRow struct {
Role string `json:"role"`
Content []byte `json:"content"`
Metadata []byte `json:"metadata"`
Usage []byte `json:"usage"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
SenderDisplayName pgtype.Text `json:"sender_display_name"`
SenderAvatarUrl pgtype.Text `json:"sender_avatar_url"`
@@ -343,6 +358,7 @@ func (q *Queries) ListMessagesLatest(ctx context.Context, arg ListMessagesLatest
&i.Role,
&i.Content,
&i.Metadata,
&i.Usage,
&i.CreatedAt,
&i.SenderDisplayName,
&i.SenderAvatarUrl,
@@ -370,6 +386,7 @@ SELECT
m.role,
m.content,
m.metadata,
m.usage,
m.created_at,
ci.display_name AS sender_display_name,
ci.avatar_url AS sender_avatar_url
@@ -397,6 +414,7 @@ type ListMessagesSinceRow struct {
Role string `json:"role"`
Content []byte `json:"content"`
Metadata []byte `json:"metadata"`
Usage []byte `json:"usage"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
SenderDisplayName pgtype.Text `json:"sender_display_name"`
SenderAvatarUrl pgtype.Text `json:"sender_avatar_url"`
@@ -423,6 +441,7 @@ func (q *Queries) ListMessagesSince(ctx context.Context, arg ListMessagesSincePa
&i.Role,
&i.Content,
&i.Metadata,
&i.Usage,
&i.CreatedAt,
&i.SenderDisplayName,
&i.SenderAvatarUrl,
+2
View File
@@ -17,6 +17,7 @@ type Bot struct {
IsActive bool `json:"is_active"`
Status string `json:"status"`
MaxContextLoadTime int32 `json:"max_context_load_time"`
MaxContextTokens int32 `json:"max_context_tokens"`
Language string `json:"language"`
AllowGuest bool `json:"allow_guest"`
ChatModelID pgtype.UUID `json:"chat_model_id"`
@@ -69,6 +70,7 @@ type BotHistoryMessage struct {
Role string `json:"role"`
Content []byte `json:"content"`
Metadata []byte `json:"metadata"`
Usage []byte `json:"usage"`
CreatedAt pgtype.Timestamptz `json:"created_at"`
}
+18 -8
View File
@@ -14,6 +14,7 @@ import (
const deleteSettingsByBotID = `-- name: DeleteSettingsByBotID :exec
UPDATE bots
SET max_context_load_time = 1440,
max_context_tokens = 0,
language = 'auto',
allow_guest = false,
chat_model_id = NULL,
@@ -33,6 +34,7 @@ const getSettingsByBotID = `-- name: GetSettingsByBotID :one
SELECT
bots.id AS bot_id,
bots.max_context_load_time,
bots.max_context_tokens,
bots.language,
bots.allow_guest,
chat_models.model_id AS chat_model_id,
@@ -50,6 +52,7 @@ WHERE bots.id = $1
type GetSettingsByBotIDRow struct {
BotID pgtype.UUID `json:"bot_id"`
MaxContextLoadTime int32 `json:"max_context_load_time"`
MaxContextTokens int32 `json:"max_context_tokens"`
Language string `json:"language"`
AllowGuest bool `json:"allow_guest"`
ChatModelID pgtype.Text `json:"chat_model_id"`
@@ -64,6 +67,7 @@ func (q *Queries) GetSettingsByBotID(ctx context.Context, id pgtype.UUID) (GetSe
err := row.Scan(
&i.BotID,
&i.MaxContextLoadTime,
&i.MaxContextTokens,
&i.Language,
&i.AllowGuest,
&i.ChatModelID,
@@ -78,19 +82,21 @@ const upsertBotSettings = `-- name: UpsertBotSettings :one
WITH updated AS (
UPDATE bots
SET max_context_load_time = $1,
language = $2,
allow_guest = $3,
chat_model_id = COALESCE($4::uuid, bots.chat_model_id),
memory_model_id = COALESCE($5::uuid, bots.memory_model_id),
embedding_model_id = COALESCE($6::uuid, bots.embedding_model_id),
search_provider_id = COALESCE($7::uuid, bots.search_provider_id),
max_context_tokens = $2,
language = $3,
allow_guest = $4,
chat_model_id = COALESCE($5::uuid, bots.chat_model_id),
memory_model_id = COALESCE($6::uuid, bots.memory_model_id),
embedding_model_id = COALESCE($7::uuid, bots.embedding_model_id),
search_provider_id = COALESCE($8::uuid, bots.search_provider_id),
updated_at = now()
WHERE bots.id = $8
RETURNING bots.id, bots.max_context_load_time, bots.language, bots.allow_guest, bots.chat_model_id, bots.memory_model_id, bots.embedding_model_id, bots.search_provider_id
WHERE bots.id = $9
RETURNING bots.id, bots.max_context_load_time, bots.max_context_tokens, bots.language, bots.allow_guest, bots.chat_model_id, bots.memory_model_id, bots.embedding_model_id, bots.search_provider_id
)
SELECT
updated.id AS bot_id,
updated.max_context_load_time,
updated.max_context_tokens,
updated.language,
updated.allow_guest,
chat_models.model_id AS chat_model_id,
@@ -106,6 +112,7 @@ LEFT JOIN search_providers ON search_providers.id = updated.search_provider_id
type UpsertBotSettingsParams struct {
MaxContextLoadTime int32 `json:"max_context_load_time"`
MaxContextTokens int32 `json:"max_context_tokens"`
Language string `json:"language"`
AllowGuest bool `json:"allow_guest"`
ChatModelID pgtype.UUID `json:"chat_model_id"`
@@ -118,6 +125,7 @@ type UpsertBotSettingsParams struct {
type UpsertBotSettingsRow struct {
BotID pgtype.UUID `json:"bot_id"`
MaxContextLoadTime int32 `json:"max_context_load_time"`
MaxContextTokens int32 `json:"max_context_tokens"`
Language string `json:"language"`
AllowGuest bool `json:"allow_guest"`
ChatModelID pgtype.Text `json:"chat_model_id"`
@@ -129,6 +137,7 @@ type UpsertBotSettingsRow struct {
func (q *Queries) UpsertBotSettings(ctx context.Context, arg UpsertBotSettingsParams) (UpsertBotSettingsRow, error) {
row := q.db.QueryRow(ctx, upsertBotSettings,
arg.MaxContextLoadTime,
arg.MaxContextTokens,
arg.Language,
arg.AllowGuest,
arg.ChatModelID,
@@ -141,6 +150,7 @@ func (q *Queries) UpsertBotSettings(ctx context.Context, arg UpsertBotSettingsPa
err := row.Scan(
&i.BotID,
&i.MaxContextLoadTime,
&i.MaxContextTokens,
&i.Language,
&i.AllowGuest,
&i.ChatModelID,