Files
Memoh/db/migrations/0041_provider_model_refactor.down.sql
Acbox Liu b88ca96064 refactor: provider & models (#277)
* 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.
2026-03-22 17:24:45 +08:00

39 lines
1.7 KiB
SQL

-- 0041_provider_model_refactor (rollback)
-- Restore client_type/dimensions/input_modalities/supports_reasoning to models, remove from providers.
-- 1. Re-add old columns to models
ALTER TABLE models
ADD COLUMN IF NOT EXISTS client_type TEXT,
ADD COLUMN IF NOT EXISTS dimensions INTEGER,
ADD COLUMN IF NOT EXISTS input_modalities TEXT[] NOT NULL DEFAULT ARRAY['text']::TEXT[],
ADD COLUMN IF NOT EXISTS supports_reasoning BOOLEAN NOT NULL DEFAULT false;
-- 2. Migrate config back to columns
UPDATE models SET
dimensions = (config->>'dimensions')::INTEGER,
supports_reasoning = COALESCE(config->'compatibilities' @> '"reasoning"', false),
input_modalities = CASE
WHEN config->'compatibilities' @> '"vision"' THEN ARRAY['text','image']::TEXT[]
ELSE ARRAY['text']::TEXT[]
END;
-- 3. Back-fill model client_type from provider
UPDATE models m
SET client_type = p.client_type
FROM llm_providers p
WHERE m.llm_provider_id = p.id;
-- 4. Re-add constraints
ALTER TABLE models
ADD CONSTRAINT models_client_type_check CHECK (client_type IS NULL OR client_type IN ('openai-responses', 'openai-completions', 'anthropic-messages', 'google-generative-ai')),
ADD CONSTRAINT models_chat_client_type_check CHECK (type != 'chat' OR client_type IS NOT NULL),
ADD CONSTRAINT models_dimensions_check CHECK (type != 'embedding' OR dimensions IS NOT NULL);
-- 5. Drop config from models
ALTER TABLE models DROP COLUMN IF EXISTS config;
-- 6. Drop provider columns and constraint
ALTER TABLE llm_providers DROP CONSTRAINT IF EXISTS llm_providers_client_type_check;
ALTER TABLE llm_providers DROP COLUMN IF EXISTS client_type;
ALTER TABLE llm_providers DROP COLUMN IF EXISTS icon;