fix(models,settings,conversation): scope model_id uniqueness per

provider and harden model reference resolution
This commit is contained in:
ringotypowriter
2026-02-21 22:31:32 +08:00
parent 9461f923df
commit 50bdbd519c
25 changed files with 376 additions and 107 deletions
+9 -14
View File
@@ -271,7 +271,7 @@ func (q *Queries) GetChatReadAccessByUser(ctx context.Context, arg GetChatReadAc
const getChatSettings = `-- name: GetChatSettings :one
SELECT
b.id AS chat_id,
chat_models.model_id AS model_id,
chat_models.id AS model_id,
b.updated_at
FROM bots b
LEFT JOIN models chat_models ON chat_models.id = b.chat_model_id
@@ -280,7 +280,7 @@ WHERE b.id = $1
type GetChatSettingsRow struct {
ChatID pgtype.UUID `json:"chat_id"`
ModelID pgtype.Text `json:"model_id"`
ModelID pgtype.UUID `json:"model_id"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
}
@@ -645,41 +645,36 @@ func (q *Queries) UpdateChatTitle(ctx context.Context, arg UpdateChatTitleParams
const upsertChatSettings = `-- name: UpsertChatSettings :one
WITH resolved_model AS (
SELECT id
FROM models
WHERE model_id = NULLIF($1::text, '')
LIMIT 1
),
WITH
updated AS (
UPDATE bots
SET chat_model_id = COALESCE((SELECT id FROM resolved_model), bots.chat_model_id),
SET chat_model_id = COALESCE($1::uuid, bots.chat_model_id),
updated_at = now()
WHERE bots.id = $2
RETURNING bots.id, bots.chat_model_id, bots.updated_at
)
SELECT
updated.id AS chat_id,
chat_models.model_id AS model_id,
chat_models.id AS model_id,
updated.updated_at
FROM updated
LEFT JOIN models chat_models ON chat_models.id = updated.chat_model_id
`
type UpsertChatSettingsParams struct {
ModelID pgtype.Text `json:"model_id"`
ID pgtype.UUID `json:"id"`
ChatModelID pgtype.UUID `json:"chat_model_id"`
ID pgtype.UUID `json:"id"`
}
type UpsertChatSettingsRow struct {
ChatID pgtype.UUID `json:"chat_id"`
ModelID pgtype.Text `json:"model_id"`
ModelID pgtype.UUID `json:"model_id"`
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
}
// chat_settings
func (q *Queries) UpsertChatSettings(ctx context.Context, arg UpsertChatSettingsParams) (UpsertChatSettingsRow, error) {
row := q.db.QueryRow(ctx, upsertChatSettings, arg.ModelID, arg.ID)
row := q.db.QueryRow(ctx, upsertChatSettings, arg.ChatModelID, arg.ID)
var i UpsertChatSettingsRow
err := row.Scan(&i.ChatID, &i.ModelID, &i.UpdatedAt)
return i, err
+47 -7
View File
@@ -419,6 +419,43 @@ func (q *Queries) ListModelsByClientType(ctx context.Context, clientType pgtype.
return items, nil
}
const listModelsByModelID = `-- name: ListModelsByModelID :many
SELECT id, model_id, name, llm_provider_id, client_type, dimensions, input_modalities, type, created_at, updated_at FROM models
WHERE model_id = $1
ORDER BY created_at DESC
`
func (q *Queries) ListModelsByModelID(ctx context.Context, modelID string) ([]Model, error) {
rows, err := q.db.Query(ctx, listModelsByModelID, modelID)
if err != nil {
return nil, err
}
defer rows.Close()
var items []Model
for rows.Next() {
var i Model
if err := rows.Scan(
&i.ID,
&i.ModelID,
&i.Name,
&i.LlmProviderID,
&i.ClientType,
&i.Dimensions,
&i.InputModalities,
&i.Type,
&i.CreatedAt,
&i.UpdatedAt,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listModelsByProviderID = `-- name: ListModelsByProviderID :many
SELECT id, model_id, name, llm_provider_id, client_type, dimensions, input_modalities, type, created_at, updated_at FROM models
WHERE llm_provider_id = $1
@@ -580,18 +617,20 @@ func (q *Queries) UpdateLlmProvider(ctx context.Context, arg UpdateLlmProviderPa
const updateModel = `-- name: UpdateModel :one
UPDATE models
SET
name = $1,
llm_provider_id = $2,
client_type = $3,
dimensions = $4,
input_modalities = $5,
type = $6,
model_id = $1,
name = $2,
llm_provider_id = $3,
client_type = $4,
dimensions = $5,
input_modalities = $6,
type = $7,
updated_at = now()
WHERE id = $7
WHERE id = $8
RETURNING id, model_id, name, llm_provider_id, client_type, dimensions, input_modalities, type, created_at, updated_at
`
type UpdateModelParams struct {
ModelID string `json:"model_id"`
Name pgtype.Text `json:"name"`
LlmProviderID pgtype.UUID `json:"llm_provider_id"`
ClientType pgtype.Text `json:"client_type"`
@@ -603,6 +642,7 @@ type UpdateModelParams struct {
func (q *Queries) UpdateModel(ctx context.Context, arg UpdateModelParams) (Model, error) {
row := q.db.QueryRow(ctx, updateModel,
arg.ModelID,
arg.Name,
arg.LlmProviderID,
arg.ClientType,
+12 -12
View File
@@ -37,9 +37,9 @@ SELECT
bots.max_context_tokens,
bots.language,
bots.allow_guest,
chat_models.model_id AS chat_model_id,
memory_models.model_id AS memory_model_id,
embedding_models.model_id AS embedding_model_id,
chat_models.id AS chat_model_id,
memory_models.id AS memory_model_id,
embedding_models.id AS embedding_model_id,
search_providers.id AS search_provider_id
FROM bots
LEFT JOIN models AS chat_models ON chat_models.id = bots.chat_model_id
@@ -55,9 +55,9 @@ type GetSettingsByBotIDRow struct {
MaxContextTokens int32 `json:"max_context_tokens"`
Language string `json:"language"`
AllowGuest bool `json:"allow_guest"`
ChatModelID pgtype.Text `json:"chat_model_id"`
MemoryModelID pgtype.Text `json:"memory_model_id"`
EmbeddingModelID pgtype.Text `json:"embedding_model_id"`
ChatModelID pgtype.UUID `json:"chat_model_id"`
MemoryModelID pgtype.UUID `json:"memory_model_id"`
EmbeddingModelID pgtype.UUID `json:"embedding_model_id"`
SearchProviderID pgtype.UUID `json:"search_provider_id"`
}
@@ -99,9 +99,9 @@ SELECT
updated.max_context_tokens,
updated.language,
updated.allow_guest,
chat_models.model_id AS chat_model_id,
memory_models.model_id AS memory_model_id,
embedding_models.model_id AS embedding_model_id,
chat_models.id AS chat_model_id,
memory_models.id AS memory_model_id,
embedding_models.id AS embedding_model_id,
search_providers.id AS search_provider_id
FROM updated
LEFT JOIN models AS chat_models ON chat_models.id = updated.chat_model_id
@@ -128,9 +128,9 @@ type UpsertBotSettingsRow struct {
MaxContextTokens int32 `json:"max_context_tokens"`
Language string `json:"language"`
AllowGuest bool `json:"allow_guest"`
ChatModelID pgtype.Text `json:"chat_model_id"`
MemoryModelID pgtype.Text `json:"memory_model_id"`
EmbeddingModelID pgtype.Text `json:"embedding_model_id"`
ChatModelID pgtype.UUID `json:"chat_model_id"`
MemoryModelID pgtype.UUID `json:"memory_model_id"`
EmbeddingModelID pgtype.UUID `json:"embedding_model_id"`
SearchProviderID pgtype.UUID `json:"search_provider_id"`
}