Add public AI prompt templates for websites.
Manage reusable templates in Super Admin, attach editable per-site prompts with Meshkee API public links that require no auth. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
4db9c7709d
commit
91a20a89d6
@@ -0,0 +1,32 @@
|
||||
-- Super Admin AI prompt templates (per website / domain), with public share ids.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ai_prompt_templates (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
domain_id BIGINT NOT NULL REFERENCES domains (id) ON DELETE CASCADE,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
body TEXT NOT NULL,
|
||||
public_id VARCHAR(32) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT ai_prompt_templates_public_id_unique UNIQUE (public_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_ai_prompt_templates_domain_id
|
||||
ON ai_prompt_templates (domain_id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_ai_prompt_templates_created_at
|
||||
ON ai_prompt_templates (created_at DESC);
|
||||
|
||||
CREATE OR REPLACE FUNCTION ai_prompt_templates_set_updated_at()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = now();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
DROP TRIGGER IF EXISTS ai_prompt_templates_set_updated_at ON ai_prompt_templates;
|
||||
CREATE TRIGGER ai_prompt_templates_set_updated_at
|
||||
BEFORE UPDATE ON ai_prompt_templates
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION ai_prompt_templates_set_updated_at();
|
||||
@@ -0,0 +1,55 @@
|
||||
-- Make AI prompt templates global; assign to websites via join + pack public id.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS domain_ai_prompt_templates (
|
||||
domain_id BIGINT NOT NULL REFERENCES domains (id) ON DELETE CASCADE,
|
||||
template_id BIGINT NOT NULL REFERENCES ai_prompt_templates (id) ON DELETE CASCADE,
|
||||
sort_order INT NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (domain_id, template_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_domain_ai_prompt_templates_template_id
|
||||
ON domain_ai_prompt_templates (template_id);
|
||||
|
||||
ALTER TABLE domains
|
||||
ADD COLUMN IF NOT EXISTS ai_prompts_public_id VARCHAR(32);
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF NOT EXISTS (
|
||||
SELECT 1 FROM pg_constraint WHERE conname = 'domains_ai_prompts_public_id_unique'
|
||||
) THEN
|
||||
ALTER TABLE domains
|
||||
ADD CONSTRAINT domains_ai_prompts_public_id_unique UNIQUE (ai_prompts_public_id);
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
-- Preserve any existing per-domain template links (from 075).
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM information_schema.columns
|
||||
WHERE table_name = 'ai_prompt_templates' AND column_name = 'domain_id'
|
||||
) THEN
|
||||
INSERT INTO domain_ai_prompt_templates (domain_id, template_id, sort_order)
|
||||
SELECT domain_id, id, 0
|
||||
FROM ai_prompt_templates
|
||||
WHERE domain_id IS NOT NULL
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
UPDATE domains d
|
||||
SET ai_prompts_public_id = t.public_id
|
||||
FROM (
|
||||
SELECT DISTINCT ON (domain_id) domain_id, public_id
|
||||
FROM ai_prompt_templates
|
||||
WHERE public_id IS NOT NULL
|
||||
ORDER BY domain_id, id
|
||||
) t
|
||||
WHERE d.id = t.domain_id
|
||||
AND d.ai_prompts_public_id IS NULL;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
ALTER TABLE ai_prompt_templates DROP COLUMN IF EXISTS domain_id;
|
||||
ALTER TABLE ai_prompt_templates DROP COLUMN IF EXISTS public_id;
|
||||
@@ -0,0 +1,73 @@
|
||||
-- Per-website AI prompt instances (editable copies) with their own public links.
|
||||
|
||||
CREATE TABLE IF NOT EXISTS domain_ai_prompts (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
domain_id BIGINT NOT NULL REFERENCES domains (id) ON DELETE CASCADE,
|
||||
source_template_id BIGINT REFERENCES ai_prompt_templates (id) ON DELETE SET NULL,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
body TEXT NOT NULL,
|
||||
public_id VARCHAR(32) NOT NULL,
|
||||
sort_order INT NOT NULL DEFAULT 0,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT domain_ai_prompts_public_id_unique UNIQUE (public_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_domain_ai_prompts_domain_id
|
||||
ON domain_ai_prompts (domain_id, sort_order, id);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_domain_ai_prompts_source_template_id
|
||||
ON domain_ai_prompts (source_template_id);
|
||||
|
||||
CREATE OR REPLACE FUNCTION domain_ai_prompts_set_updated_at()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at = now();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
DROP TRIGGER IF EXISTS domain_ai_prompts_set_updated_at ON domain_ai_prompts;
|
||||
CREATE TRIGGER domain_ai_prompts_set_updated_at
|
||||
BEFORE UPDATE ON domain_ai_prompts
|
||||
FOR EACH ROW
|
||||
EXECUTE FUNCTION domain_ai_prompts_set_updated_at();
|
||||
|
||||
-- Copy existing assignments into editable instances (one public id each).
|
||||
DO $$
|
||||
DECLARE
|
||||
r RECORD;
|
||||
pid TEXT;
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.tables
|
||||
WHERE table_name = 'domain_ai_prompt_templates'
|
||||
) THEN
|
||||
FOR r IN
|
||||
SELECT
|
||||
l.domain_id,
|
||||
l.template_id,
|
||||
l.sort_order,
|
||||
t.name,
|
||||
t.body
|
||||
FROM domain_ai_prompt_templates l
|
||||
JOIN ai_prompt_templates t ON t.id = l.template_id
|
||||
ORDER BY l.domain_id, l.sort_order, l.template_id
|
||||
LOOP
|
||||
LOOP
|
||||
pid := encode(gen_random_bytes(12), 'hex');
|
||||
EXIT WHEN NOT EXISTS (
|
||||
SELECT 1 FROM domain_ai_prompts WHERE public_id = pid
|
||||
);
|
||||
END LOOP;
|
||||
|
||||
INSERT INTO domain_ai_prompts (
|
||||
domain_id, source_template_id, name, body, public_id, sort_order
|
||||
) VALUES (
|
||||
r.domain_id, r.template_id, r.name, r.body, pid, r.sort_order
|
||||
);
|
||||
END LOOP;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
DROP TABLE IF EXISTS domain_ai_prompt_templates;
|
||||
Reference in New Issue
Block a user