mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
b88ca96064
* refactor: move client_type to provider, replace model fields with config JSONB - Move `client_type` from `models` to `llm_providers` table - Add `icon` field to `llm_providers` - Replace `dimensions`, `input_modalities`, `supports_reasoning` on `models` with a single `config` JSONB column containing `dimensions`, `compatibilities` (vision, tool-call, image-output, reasoning), and `context_window` - Auto-imported models default to vision + tool-call + reasoning - Update all backend consumers (agent, flow resolver, handlers, memory) - Regenerate sqlc, swagger, and TypeScript SDK - Update frontend forms, display, and i18n for new schema * ui: show provider icon avatar in sidebar and detail header, remove icon input * feat: add built-in provider registry with YAML definitions and enable toggle - Add `enable` column to llm_providers (default true, backward-compatible) - Create internal/registry package to load YAML provider/model definitions on startup and upsert into database (new providers disabled by default) - Add conf/providers/ with OpenAI, Anthropic, Google YAML definitions - Add RegistryConfig to TOML config (providers_dir, default conf/providers) - Model listing APIs and conversation flow now filter by enabled providers - Frontend: enable switch in provider form, green status dot in sidebar, enabled providers sorted to top * fix: make 0041 migration idempotent for fresh databases Guard data migration steps with column-existence checks so the migration succeeds on databases created from the updated init schema.
866 lines
19 KiB
Go
866 lines
19 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: models.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const countLlmProviders = `-- name: CountLlmProviders :one
|
|
SELECT COUNT(*) FROM llm_providers
|
|
`
|
|
|
|
func (q *Queries) CountLlmProviders(ctx context.Context) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countLlmProviders)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const countModels = `-- name: CountModels :one
|
|
SELECT COUNT(*) FROM models
|
|
`
|
|
|
|
func (q *Queries) CountModels(ctx context.Context) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countModels)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const countModelsByType = `-- name: CountModelsByType :one
|
|
SELECT COUNT(*) FROM models WHERE type = $1
|
|
`
|
|
|
|
func (q *Queries) CountModelsByType(ctx context.Context, type_ string) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countModelsByType, type_)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const createLlmProvider = `-- name: CreateLlmProvider :one
|
|
INSERT INTO llm_providers (name, base_url, api_key, client_type, icon, enable, metadata)
|
|
VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7
|
|
)
|
|
RETURNING id, name, base_url, api_key, icon, enable, metadata, created_at, updated_at, client_type
|
|
`
|
|
|
|
type CreateLlmProviderParams struct {
|
|
Name string `json:"name"`
|
|
BaseUrl string `json:"base_url"`
|
|
ApiKey string `json:"api_key"`
|
|
ClientType string `json:"client_type"`
|
|
Icon pgtype.Text `json:"icon"`
|
|
Enable bool `json:"enable"`
|
|
Metadata []byte `json:"metadata"`
|
|
}
|
|
|
|
func (q *Queries) CreateLlmProvider(ctx context.Context, arg CreateLlmProviderParams) (LlmProvider, error) {
|
|
row := q.db.QueryRow(ctx, createLlmProvider,
|
|
arg.Name,
|
|
arg.BaseUrl,
|
|
arg.ApiKey,
|
|
arg.ClientType,
|
|
arg.Icon,
|
|
arg.Enable,
|
|
arg.Metadata,
|
|
)
|
|
var i LlmProvider
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.BaseUrl,
|
|
&i.ApiKey,
|
|
&i.Icon,
|
|
&i.Enable,
|
|
&i.Metadata,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ClientType,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createModel = `-- name: CreateModel :one
|
|
INSERT INTO models (model_id, name, llm_provider_id, type, config)
|
|
VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5
|
|
)
|
|
RETURNING id, model_id, name, llm_provider_id, type, config, created_at, updated_at
|
|
`
|
|
|
|
type CreateModelParams struct {
|
|
ModelID string `json:"model_id"`
|
|
Name pgtype.Text `json:"name"`
|
|
LlmProviderID pgtype.UUID `json:"llm_provider_id"`
|
|
Type string `json:"type"`
|
|
Config []byte `json:"config"`
|
|
}
|
|
|
|
func (q *Queries) CreateModel(ctx context.Context, arg CreateModelParams) (Model, error) {
|
|
row := q.db.QueryRow(ctx, createModel,
|
|
arg.ModelID,
|
|
arg.Name,
|
|
arg.LlmProviderID,
|
|
arg.Type,
|
|
arg.Config,
|
|
)
|
|
var i Model
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ModelID,
|
|
&i.Name,
|
|
&i.LlmProviderID,
|
|
&i.Type,
|
|
&i.Config,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const createModelVariant = `-- name: CreateModelVariant :one
|
|
INSERT INTO model_variants (model_uuid, variant_id, weight, metadata)
|
|
VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4
|
|
)
|
|
RETURNING id, model_uuid, variant_id, weight, metadata, created_at, updated_at
|
|
`
|
|
|
|
type CreateModelVariantParams struct {
|
|
ModelUuid pgtype.UUID `json:"model_uuid"`
|
|
VariantID string `json:"variant_id"`
|
|
Weight int32 `json:"weight"`
|
|
Metadata []byte `json:"metadata"`
|
|
}
|
|
|
|
func (q *Queries) CreateModelVariant(ctx context.Context, arg CreateModelVariantParams) (ModelVariant, error) {
|
|
row := q.db.QueryRow(ctx, createModelVariant,
|
|
arg.ModelUuid,
|
|
arg.VariantID,
|
|
arg.Weight,
|
|
arg.Metadata,
|
|
)
|
|
var i ModelVariant
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ModelUuid,
|
|
&i.VariantID,
|
|
&i.Weight,
|
|
&i.Metadata,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteLlmProvider = `-- name: DeleteLlmProvider :exec
|
|
DELETE FROM llm_providers WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteLlmProvider(ctx context.Context, id pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteLlmProvider, id)
|
|
return err
|
|
}
|
|
|
|
const deleteModel = `-- name: DeleteModel :exec
|
|
DELETE FROM models WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteModel(ctx context.Context, id pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteModel, id)
|
|
return err
|
|
}
|
|
|
|
const deleteModelByModelID = `-- name: DeleteModelByModelID :exec
|
|
DELETE FROM models WHERE model_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteModelByModelID(ctx context.Context, modelID string) error {
|
|
_, err := q.db.Exec(ctx, deleteModelByModelID, modelID)
|
|
return err
|
|
}
|
|
|
|
const getLlmProviderByID = `-- name: GetLlmProviderByID :one
|
|
SELECT id, name, base_url, api_key, icon, enable, metadata, created_at, updated_at, client_type FROM llm_providers WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetLlmProviderByID(ctx context.Context, id pgtype.UUID) (LlmProvider, error) {
|
|
row := q.db.QueryRow(ctx, getLlmProviderByID, id)
|
|
var i LlmProvider
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.BaseUrl,
|
|
&i.ApiKey,
|
|
&i.Icon,
|
|
&i.Enable,
|
|
&i.Metadata,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ClientType,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getLlmProviderByName = `-- name: GetLlmProviderByName :one
|
|
SELECT id, name, base_url, api_key, icon, enable, metadata, created_at, updated_at, client_type FROM llm_providers WHERE name = $1
|
|
`
|
|
|
|
func (q *Queries) GetLlmProviderByName(ctx context.Context, name string) (LlmProvider, error) {
|
|
row := q.db.QueryRow(ctx, getLlmProviderByName, name)
|
|
var i LlmProvider
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.BaseUrl,
|
|
&i.ApiKey,
|
|
&i.Icon,
|
|
&i.Enable,
|
|
&i.Metadata,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ClientType,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getModelByID = `-- name: GetModelByID :one
|
|
SELECT id, model_id, name, llm_provider_id, type, config, created_at, updated_at FROM models WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetModelByID(ctx context.Context, id pgtype.UUID) (Model, error) {
|
|
row := q.db.QueryRow(ctx, getModelByID, id)
|
|
var i Model
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ModelID,
|
|
&i.Name,
|
|
&i.LlmProviderID,
|
|
&i.Type,
|
|
&i.Config,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getModelByModelID = `-- name: GetModelByModelID :one
|
|
SELECT id, model_id, name, llm_provider_id, type, config, created_at, updated_at FROM models WHERE model_id = $1
|
|
`
|
|
|
|
func (q *Queries) GetModelByModelID(ctx context.Context, modelID string) (Model, error) {
|
|
row := q.db.QueryRow(ctx, getModelByModelID, modelID)
|
|
var i Model
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ModelID,
|
|
&i.Name,
|
|
&i.LlmProviderID,
|
|
&i.Type,
|
|
&i.Config,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listEnabledModels = `-- name: ListEnabledModels :many
|
|
SELECT m.id, m.model_id, m.name, m.llm_provider_id, m.type, m.config, m.created_at, m.updated_at
|
|
FROM models m
|
|
JOIN llm_providers p ON m.llm_provider_id = p.id
|
|
WHERE p.enable = true
|
|
ORDER BY m.created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListEnabledModels(ctx context.Context) ([]Model, error) {
|
|
rows, err := q.db.Query(ctx, listEnabledModels)
|
|
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.Type,
|
|
&i.Config,
|
|
&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 listEnabledModelsByProviderClientType = `-- name: ListEnabledModelsByProviderClientType :many
|
|
SELECT m.id, m.model_id, m.name, m.llm_provider_id, m.type, m.config, m.created_at, m.updated_at
|
|
FROM models m
|
|
JOIN llm_providers p ON m.llm_provider_id = p.id
|
|
WHERE p.enable = true
|
|
AND p.client_type = $1
|
|
ORDER BY m.created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListEnabledModelsByProviderClientType(ctx context.Context, clientType string) ([]Model, error) {
|
|
rows, err := q.db.Query(ctx, listEnabledModelsByProviderClientType, clientType)
|
|
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.Type,
|
|
&i.Config,
|
|
&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 listEnabledModelsByType = `-- name: ListEnabledModelsByType :many
|
|
SELECT m.id, m.model_id, m.name, m.llm_provider_id, m.type, m.config, m.created_at, m.updated_at
|
|
FROM models m
|
|
JOIN llm_providers p ON m.llm_provider_id = p.id
|
|
WHERE p.enable = true
|
|
AND m.type = $1
|
|
ORDER BY m.created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListEnabledModelsByType(ctx context.Context, type_ string) ([]Model, error) {
|
|
rows, err := q.db.Query(ctx, listEnabledModelsByType, type_)
|
|
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.Type,
|
|
&i.Config,
|
|
&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 listLlmProviders = `-- name: ListLlmProviders :many
|
|
SELECT id, name, base_url, api_key, icon, enable, metadata, created_at, updated_at, client_type FROM llm_providers
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListLlmProviders(ctx context.Context) ([]LlmProvider, error) {
|
|
rows, err := q.db.Query(ctx, listLlmProviders)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []LlmProvider
|
|
for rows.Next() {
|
|
var i LlmProvider
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.BaseUrl,
|
|
&i.ApiKey,
|
|
&i.Icon,
|
|
&i.Enable,
|
|
&i.Metadata,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ClientType,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listModelVariantsByModelUUID = `-- name: ListModelVariantsByModelUUID :many
|
|
SELECT id, model_uuid, variant_id, weight, metadata, created_at, updated_at FROM model_variants
|
|
WHERE model_uuid = $1
|
|
ORDER BY weight DESC, created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListModelVariantsByModelUUID(ctx context.Context, modelUuid pgtype.UUID) ([]ModelVariant, error) {
|
|
rows, err := q.db.Query(ctx, listModelVariantsByModelUUID, modelUuid)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ModelVariant
|
|
for rows.Next() {
|
|
var i ModelVariant
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.ModelUuid,
|
|
&i.VariantID,
|
|
&i.Weight,
|
|
&i.Metadata,
|
|
&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 listModels = `-- name: ListModels :many
|
|
SELECT id, model_id, name, llm_provider_id, type, config, created_at, updated_at FROM models
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListModels(ctx context.Context) ([]Model, error) {
|
|
rows, err := q.db.Query(ctx, listModels)
|
|
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.Type,
|
|
&i.Config,
|
|
&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 listModelsByModelID = `-- name: ListModelsByModelID :many
|
|
SELECT id, model_id, name, llm_provider_id, type, config, 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.Type,
|
|
&i.Config,
|
|
&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 listModelsByProviderClientType = `-- name: ListModelsByProviderClientType :many
|
|
SELECT m.id, m.model_id, m.name, m.llm_provider_id, m.type, m.config, m.created_at, m.updated_at
|
|
FROM models m
|
|
JOIN llm_providers p ON m.llm_provider_id = p.id
|
|
WHERE p.client_type = $1
|
|
ORDER BY m.created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListModelsByProviderClientType(ctx context.Context, clientType string) ([]Model, error) {
|
|
rows, err := q.db.Query(ctx, listModelsByProviderClientType, clientType)
|
|
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.Type,
|
|
&i.Config,
|
|
&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, type, config, created_at, updated_at FROM models
|
|
WHERE llm_provider_id = $1
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListModelsByProviderID(ctx context.Context, llmProviderID pgtype.UUID) ([]Model, error) {
|
|
rows, err := q.db.Query(ctx, listModelsByProviderID, llmProviderID)
|
|
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.Type,
|
|
&i.Config,
|
|
&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 listModelsByProviderIDAndType = `-- name: ListModelsByProviderIDAndType :many
|
|
SELECT id, model_id, name, llm_provider_id, type, config, created_at, updated_at FROM models
|
|
WHERE llm_provider_id = $1
|
|
AND type = $2
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
type ListModelsByProviderIDAndTypeParams struct {
|
|
LlmProviderID pgtype.UUID `json:"llm_provider_id"`
|
|
Type string `json:"type"`
|
|
}
|
|
|
|
func (q *Queries) ListModelsByProviderIDAndType(ctx context.Context, arg ListModelsByProviderIDAndTypeParams) ([]Model, error) {
|
|
rows, err := q.db.Query(ctx, listModelsByProviderIDAndType, arg.LlmProviderID, arg.Type)
|
|
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.Type,
|
|
&i.Config,
|
|
&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 listModelsByType = `-- name: ListModelsByType :many
|
|
SELECT id, model_id, name, llm_provider_id, type, config, created_at, updated_at FROM models
|
|
WHERE type = $1
|
|
ORDER BY created_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListModelsByType(ctx context.Context, type_ string) ([]Model, error) {
|
|
rows, err := q.db.Query(ctx, listModelsByType, type_)
|
|
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.Type,
|
|
&i.Config,
|
|
&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 updateLlmProvider = `-- name: UpdateLlmProvider :one
|
|
UPDATE llm_providers
|
|
SET
|
|
name = $1,
|
|
base_url = $2,
|
|
api_key = $3,
|
|
client_type = $4,
|
|
icon = $5,
|
|
enable = $6,
|
|
metadata = $7,
|
|
updated_at = now()
|
|
WHERE id = $8
|
|
RETURNING id, name, base_url, api_key, icon, enable, metadata, created_at, updated_at, client_type
|
|
`
|
|
|
|
type UpdateLlmProviderParams struct {
|
|
Name string `json:"name"`
|
|
BaseUrl string `json:"base_url"`
|
|
ApiKey string `json:"api_key"`
|
|
ClientType string `json:"client_type"`
|
|
Icon pgtype.Text `json:"icon"`
|
|
Enable bool `json:"enable"`
|
|
Metadata []byte `json:"metadata"`
|
|
ID pgtype.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateLlmProvider(ctx context.Context, arg UpdateLlmProviderParams) (LlmProvider, error) {
|
|
row := q.db.QueryRow(ctx, updateLlmProvider,
|
|
arg.Name,
|
|
arg.BaseUrl,
|
|
arg.ApiKey,
|
|
arg.ClientType,
|
|
arg.Icon,
|
|
arg.Enable,
|
|
arg.Metadata,
|
|
arg.ID,
|
|
)
|
|
var i LlmProvider
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.BaseUrl,
|
|
&i.ApiKey,
|
|
&i.Icon,
|
|
&i.Enable,
|
|
&i.Metadata,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ClientType,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const updateModel = `-- name: UpdateModel :one
|
|
UPDATE models
|
|
SET
|
|
model_id = $1,
|
|
name = $2,
|
|
llm_provider_id = $3,
|
|
type = $4,
|
|
config = $5,
|
|
updated_at = now()
|
|
WHERE id = $6
|
|
RETURNING id, model_id, name, llm_provider_id, type, config, created_at, updated_at
|
|
`
|
|
|
|
type UpdateModelParams struct {
|
|
ModelID string `json:"model_id"`
|
|
Name pgtype.Text `json:"name"`
|
|
LlmProviderID pgtype.UUID `json:"llm_provider_id"`
|
|
Type string `json:"type"`
|
|
Config []byte `json:"config"`
|
|
ID pgtype.UUID `json:"id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateModel(ctx context.Context, arg UpdateModelParams) (Model, error) {
|
|
row := q.db.QueryRow(ctx, updateModel,
|
|
arg.ModelID,
|
|
arg.Name,
|
|
arg.LlmProviderID,
|
|
arg.Type,
|
|
arg.Config,
|
|
arg.ID,
|
|
)
|
|
var i Model
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ModelID,
|
|
&i.Name,
|
|
&i.LlmProviderID,
|
|
&i.Type,
|
|
&i.Config,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const upsertRegistryModel = `-- name: UpsertRegistryModel :one
|
|
INSERT INTO models (model_id, name, llm_provider_id, type, config)
|
|
VALUES ($1, $2, $3, $4, $5)
|
|
ON CONFLICT (llm_provider_id, model_id) DO UPDATE SET
|
|
name = EXCLUDED.name,
|
|
type = EXCLUDED.type,
|
|
config = EXCLUDED.config,
|
|
updated_at = now()
|
|
RETURNING id, model_id, name, llm_provider_id, type, config, created_at, updated_at
|
|
`
|
|
|
|
type UpsertRegistryModelParams struct {
|
|
ModelID string `json:"model_id"`
|
|
Name pgtype.Text `json:"name"`
|
|
LlmProviderID pgtype.UUID `json:"llm_provider_id"`
|
|
Type string `json:"type"`
|
|
Config []byte `json:"config"`
|
|
}
|
|
|
|
func (q *Queries) UpsertRegistryModel(ctx context.Context, arg UpsertRegistryModelParams) (Model, error) {
|
|
row := q.db.QueryRow(ctx, upsertRegistryModel,
|
|
arg.ModelID,
|
|
arg.Name,
|
|
arg.LlmProviderID,
|
|
arg.Type,
|
|
arg.Config,
|
|
)
|
|
var i Model
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.ModelID,
|
|
&i.Name,
|
|
&i.LlmProviderID,
|
|
&i.Type,
|
|
&i.Config,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const upsertRegistryProvider = `-- name: UpsertRegistryProvider :one
|
|
INSERT INTO llm_providers (name, base_url, api_key, client_type, icon, enable, metadata)
|
|
VALUES ($1, $2, '', $3, $4, false, '{}')
|
|
ON CONFLICT (name) DO UPDATE SET
|
|
icon = EXCLUDED.icon,
|
|
client_type = EXCLUDED.client_type,
|
|
updated_at = now()
|
|
RETURNING id, name, base_url, api_key, icon, enable, metadata, created_at, updated_at, client_type
|
|
`
|
|
|
|
type UpsertRegistryProviderParams struct {
|
|
Name string `json:"name"`
|
|
BaseUrl string `json:"base_url"`
|
|
ClientType string `json:"client_type"`
|
|
Icon pgtype.Text `json:"icon"`
|
|
}
|
|
|
|
func (q *Queries) UpsertRegistryProvider(ctx context.Context, arg UpsertRegistryProviderParams) (LlmProvider, error) {
|
|
row := q.db.QueryRow(ctx, upsertRegistryProvider,
|
|
arg.Name,
|
|
arg.BaseUrl,
|
|
arg.ClientType,
|
|
arg.Icon,
|
|
)
|
|
var i LlmProvider
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.Name,
|
|
&i.BaseUrl,
|
|
&i.ApiKey,
|
|
&i.Icon,
|
|
&i.Enable,
|
|
&i.Metadata,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.ClientType,
|
|
)
|
|
return i, err
|
|
}
|