feat: implement AI gateway phase one runtime

This commit is contained in:
2026-05-09 21:18:32 +08:00
parent a5e66e79cd
commit fdcdcd477b
46 changed files with 5678 additions and 768 deletions
+38 -1
View File
@@ -3,8 +3,11 @@ CREATE EXTENSION IF NOT EXISTS pgcrypto;
CREATE TABLE IF NOT EXISTS model_catalog_providers (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
provider_key text NOT NULL UNIQUE,
provider_code text NOT NULL,
display_name text NOT NULL,
provider_type text NOT NULL DEFAULT 'openai_compatible',
provider_type text NOT NULL DEFAULT 'openai',
icon_path text,
source text NOT NULL DEFAULT 'gateway',
capability_schema jsonb NOT NULL DEFAULT '{}'::jsonb,
default_rate_limit_policy jsonb NOT NULL DEFAULT '{}'::jsonb,
status text NOT NULL DEFAULT 'active',
@@ -16,6 +19,9 @@ CREATE TABLE IF NOT EXISTS model_catalog_providers (
CREATE INDEX IF NOT EXISTS idx_model_catalog_provider_status
ON model_catalog_providers(status);
CREATE UNIQUE INDEX IF NOT EXISTS idx_model_catalog_provider_code
ON model_catalog_providers(provider_code);
CREATE TABLE IF NOT EXISTS base_model_catalog (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
provider_id uuid REFERENCES model_catalog_providers(id) ON DELETE SET NULL,
@@ -54,6 +60,7 @@ CREATE TABLE IF NOT EXISTS integration_platforms (
tenant_key text,
default_pricing_mode text NOT NULL DEFAULT 'inherit_discount',
default_discount_factor numeric NOT NULL DEFAULT 1,
pricing_rule_set_id uuid,
retry_policy jsonb NOT NULL DEFAULT '{}'::jsonb,
rate_limit_policy jsonb NOT NULL DEFAULT '{}'::jsonb,
priority integer NOT NULL DEFAULT 100,
@@ -78,8 +85,24 @@ CREATE INDEX IF NOT EXISTS idx_integration_platforms_cooldown
CREATE INDEX IF NOT EXISTS idx_integration_platforms_tenant_scope
ON integration_platforms(visibility_scope, tenant_id, tenant_key, status);
CREATE TABLE IF NOT EXISTS model_pricing_rule_sets (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
rule_set_key text NOT NULL UNIQUE,
name text NOT NULL,
description text,
category text NOT NULL DEFAULT 'general',
currency text NOT NULL DEFAULT 'resource',
status text NOT NULL DEFAULT 'active',
metadata jsonb NOT NULL DEFAULT '{}'::jsonb,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE IF NOT EXISTS model_pricing_rules (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
rule_set_id uuid REFERENCES model_pricing_rule_sets(id) ON DELETE CASCADE,
rule_key text NOT NULL DEFAULT ('rule_' || replace(gen_random_uuid()::text, '-', '')),
display_name text NOT NULL DEFAULT '',
scope_type text NOT NULL,
scope_id uuid,
resource_type text NOT NULL,
@@ -88,6 +111,12 @@ CREATE TABLE IF NOT EXISTS model_pricing_rules (
currency text NOT NULL DEFAULT 'resource',
base_weight jsonb NOT NULL DEFAULT '{}'::jsonb,
dynamic_weight jsonb NOT NULL DEFAULT '{}'::jsonb,
calculator_type text NOT NULL DEFAULT 'unit_weight',
dimension_schema jsonb NOT NULL DEFAULT '{}'::jsonb,
formula_config jsonb NOT NULL DEFAULT '{}'::jsonb,
priority integer NOT NULL DEFAULT 100,
status text NOT NULL DEFAULT 'active',
metadata jsonb NOT NULL DEFAULT '{}'::jsonb,
effective_from timestamptz,
effective_to timestamptz,
created_at timestamptz NOT NULL DEFAULT now(),
@@ -97,6 +126,13 @@ CREATE TABLE IF NOT EXISTS model_pricing_rules (
CREATE INDEX IF NOT EXISTS idx_model_pricing_scope
ON model_pricing_rules(scope_type, scope_id, resource_type);
CREATE INDEX IF NOT EXISTS idx_model_pricing_rule_set
ON model_pricing_rules(rule_set_id, resource_type, priority);
CREATE UNIQUE INDEX IF NOT EXISTS idx_model_pricing_rule_set_key
ON model_pricing_rules(rule_set_id, rule_key)
WHERE rule_set_id IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_model_pricing_effective
ON model_pricing_rules(effective_from, effective_to);
@@ -343,6 +379,7 @@ CREATE TABLE IF NOT EXISTS platform_models (
capabilities jsonb NOT NULL DEFAULT '{}'::jsonb,
pricing_mode text NOT NULL DEFAULT 'inherit_discount',
discount_factor numeric,
pricing_rule_set_id uuid REFERENCES model_pricing_rule_sets(id) ON DELETE SET NULL,
billing_config_override jsonb NOT NULL DEFAULT '{}'::jsonb,
billing_config jsonb NOT NULL DEFAULT '{}'::jsonb,
permission_config jsonb NOT NULL DEFAULT '{}'::jsonb,