modularizado de prompts
This commit is contained in:
50
db/migrations/20260125100000_prompt_templates.sql
Normal file
50
db/migrations/20260125100000_prompt_templates.sql
Normal file
@@ -0,0 +1,50 @@
|
||||
-- migrate:up
|
||||
|
||||
-- Tabla para almacenar prompts editables por tenant con versionado
|
||||
CREATE TABLE prompt_templates (
|
||||
id SERIAL PRIMARY KEY,
|
||||
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
|
||||
prompt_key VARCHAR(50) NOT NULL, -- 'router', 'greeting', 'orders', 'shipping', 'payment', 'browse'
|
||||
content TEXT NOT NULL,
|
||||
model VARCHAR(50) DEFAULT 'gpt-4-turbo',
|
||||
version INTEGER NOT NULL DEFAULT 1,
|
||||
is_active BOOLEAN DEFAULT true,
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
created_by TEXT, -- email o identificador del usuario que creó el prompt
|
||||
|
||||
CONSTRAINT uq_prompt_version UNIQUE(tenant_id, prompt_key, version)
|
||||
);
|
||||
|
||||
-- Índice para búsqueda rápida del prompt activo
|
||||
CREATE INDEX idx_prompt_active ON prompt_templates(tenant_id, prompt_key)
|
||||
WHERE is_active = true;
|
||||
|
||||
-- Índice para listar versiones
|
||||
CREATE INDEX idx_prompt_versions ON prompt_templates(tenant_id, prompt_key, version DESC);
|
||||
|
||||
-- Función para auto-incrementar versión al insertar
|
||||
CREATE OR REPLACE FUNCTION increment_prompt_version()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
-- Calcular siguiente versión
|
||||
NEW.version := COALESCE(
|
||||
(SELECT MAX(version) + 1 FROM prompt_templates
|
||||
WHERE tenant_id = NEW.tenant_id AND prompt_key = NEW.prompt_key),
|
||||
1
|
||||
);
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Trigger para auto-versión
|
||||
CREATE TRIGGER prompt_version_trigger
|
||||
BEFORE INSERT ON prompt_templates
|
||||
FOR EACH ROW EXECUTE FUNCTION increment_prompt_version();
|
||||
|
||||
-- migrate:down
|
||||
|
||||
DROP TRIGGER IF EXISTS prompt_version_trigger ON prompt_templates;
|
||||
DROP FUNCTION IF EXISTS increment_prompt_version();
|
||||
DROP INDEX IF EXISTS idx_prompt_versions;
|
||||
DROP INDEX IF EXISTS idx_prompt_active;
|
||||
DROP TABLE IF EXISTS prompt_templates;
|
||||
56
db/migrations/20260125110000_human_takeovers.sql
Normal file
56
db/migrations/20260125110000_human_takeovers.sql
Normal file
@@ -0,0 +1,56 @@
|
||||
-- migrate:up
|
||||
|
||||
-- Tabla para manejar conversaciones que requieren intervención humana
|
||||
CREATE TABLE human_takeovers (
|
||||
id SERIAL PRIMARY KEY,
|
||||
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
|
||||
chat_id TEXT NOT NULL,
|
||||
|
||||
-- Info del takeover
|
||||
pending_query TEXT NOT NULL, -- La query/producto que no se encontró
|
||||
reason TEXT NOT NULL DEFAULT 'product_not_found', -- Razón del takeover
|
||||
context_snapshot JSONB, -- Snapshot del contexto al momento del takeover
|
||||
|
||||
-- Estado
|
||||
status TEXT NOT NULL DEFAULT 'pending', -- 'pending', 'responded', 'cancelled'
|
||||
|
||||
-- Respuesta del humano
|
||||
human_response TEXT, -- Texto de la respuesta
|
||||
responded_at TIMESTAMPTZ,
|
||||
responded_by TEXT, -- Email/ID del usuario que respondió
|
||||
|
||||
-- Metadatos
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
|
||||
-- Índices
|
||||
CONSTRAINT valid_status CHECK (status IN ('pending', 'responded', 'cancelled'))
|
||||
);
|
||||
|
||||
-- Índice para buscar takeovers pendientes por tenant
|
||||
CREATE INDEX idx_takeovers_pending ON human_takeovers(tenant_id, status, created_at DESC)
|
||||
WHERE status = 'pending';
|
||||
|
||||
-- Índice para buscar por chat_id
|
||||
CREATE INDEX idx_takeovers_chat ON human_takeovers(tenant_id, chat_id, created_at DESC);
|
||||
|
||||
-- Función para actualizar updated_at
|
||||
CREATE OR REPLACE FUNCTION update_takeover_timestamp()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at := NOW();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER takeover_updated_trigger
|
||||
BEFORE UPDATE ON human_takeovers
|
||||
FOR EACH ROW EXECUTE FUNCTION update_takeover_timestamp();
|
||||
|
||||
-- migrate:down
|
||||
|
||||
DROP TRIGGER IF EXISTS takeover_updated_trigger ON human_takeovers;
|
||||
DROP FUNCTION IF EXISTS update_takeover_timestamp();
|
||||
DROP INDEX IF EXISTS idx_takeovers_chat;
|
||||
DROP INDEX IF EXISTS idx_takeovers_pending;
|
||||
DROP TABLE IF EXISTS human_takeovers;
|
||||
58
db/migrations/20260125120000_tenant_settings.sql
Normal file
58
db/migrations/20260125120000_tenant_settings.sql
Normal file
@@ -0,0 +1,58 @@
|
||||
-- migrate:up
|
||||
|
||||
-- Tabla para configuración del tenant (variables de prompts, horarios, etc.)
|
||||
CREATE TABLE tenant_settings (
|
||||
id SERIAL PRIMARY KEY,
|
||||
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
|
||||
|
||||
-- Info básica del negocio
|
||||
store_name VARCHAR(100) NOT NULL DEFAULT 'Mi Negocio',
|
||||
bot_name VARCHAR(50) NOT NULL DEFAULT 'Piaf',
|
||||
store_address TEXT,
|
||||
store_phone VARCHAR(50),
|
||||
|
||||
-- Horarios de delivery
|
||||
delivery_enabled BOOLEAN DEFAULT true,
|
||||
delivery_days VARCHAR(50) DEFAULT 'lun,mar,mie,jue,vie,sab', -- días separados por coma
|
||||
delivery_hours_start TIME DEFAULT '09:00',
|
||||
delivery_hours_end TIME DEFAULT '18:00',
|
||||
delivery_min_order DECIMAL(10,2) DEFAULT 0,
|
||||
|
||||
-- Horarios de retiro en tienda
|
||||
pickup_enabled BOOLEAN DEFAULT true,
|
||||
pickup_days VARCHAR(50) DEFAULT 'lun,mar,mie,jue,vie,sab',
|
||||
pickup_hours_start TIME DEFAULT '08:00',
|
||||
pickup_hours_end TIME DEFAULT '20:00',
|
||||
|
||||
-- Metadatos
|
||||
created_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
updated_at TIMESTAMPTZ DEFAULT NOW(),
|
||||
|
||||
-- Solo un registro por tenant
|
||||
CONSTRAINT uq_tenant_settings UNIQUE(tenant_id)
|
||||
);
|
||||
|
||||
-- Trigger para actualizar updated_at
|
||||
CREATE OR REPLACE FUNCTION update_tenant_settings_timestamp()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at := NOW();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER tenant_settings_updated_trigger
|
||||
BEFORE UPDATE ON tenant_settings
|
||||
FOR EACH ROW EXECUTE FUNCTION update_tenant_settings_timestamp();
|
||||
|
||||
-- Crear registro default para tenants existentes
|
||||
INSERT INTO tenant_settings (tenant_id, store_name, bot_name)
|
||||
SELECT id, 'Mi Carnicería', 'Piaf'
|
||||
FROM tenants
|
||||
ON CONFLICT (tenant_id) DO NOTHING;
|
||||
|
||||
-- migrate:down
|
||||
|
||||
DROP TRIGGER IF EXISTS tenant_settings_updated_trigger ON tenant_settings;
|
||||
DROP FUNCTION IF EXISTS update_tenant_settings_timestamp();
|
||||
DROP TABLE IF EXISTS tenant_settings;
|
||||
Reference in New Issue
Block a user