Files
AIclinicalresearch/backend/restore_pgboss_functions.sql
HaHafeng 5523ef36ea feat(admin): Complete Phase 3.5.1-3.5.4 Prompt Management System (83%)
Summary:
- Implement Prompt management infrastructure and core services
- Build admin portal frontend with light theme
- Integrate CodeMirror 6 editor for non-technical users

Phase 3.5.1: Infrastructure Setup
- Create capability_schema for Prompt storage
- Add prompt_templates and prompt_versions tables
- Add prompt:view/edit/debug/publish permissions
- Migrate RVW prompts to database (RVW_EDITORIAL, RVW_METHODOLOGY)

Phase 3.5.2: PromptService Core
- Implement gray preview logic (DRAFT for debuggers, ACTIVE for users)
- Module-level debug control (setDebugMode)
- Handlebars template rendering
- Variable extraction and validation (extractVariables, validateVariables)
- Three-level disaster recovery (database -> cache -> hardcoded fallback)

Phase 3.5.3: Management API
- 8 RESTful endpoints (/api/admin/prompts/*)
- Permission control (PROMPT_ENGINEER can edit, SUPER_ADMIN can publish)

Phase 3.5.4: Frontend Management UI
- Build admin portal architecture (AdminLayout, OrgLayout)
- Add route system (/admin/*, /org/*)
- Implement PromptListPage (filter, search, debug switch)
- Implement PromptEditor (CodeMirror 6 simplified for clinical users)
- Implement PromptEditorPage (edit, save, publish, test, version history)

Technical Details:
- Backend: 6 files, ~2044 lines (prompt.service.ts 596 lines)
- Frontend: 9 files, ~1735 lines (PromptEditorPage.tsx 399 lines)
- CodeMirror 6: Line numbers, auto-wrap, variable highlight, search, undo/redo
- Chinese-friendly: 15px font, 1.8 line-height, system fonts

Next Step: Phase 3.5.5 - Integrate RVW module with PromptService

Tested: Backend API tests passed (8/8), Frontend pending user testing
Status: Ready for Phase 3.5.5 RVW integration
2026-01-11 21:25:16 +08:00

103 lines
4.5 KiB
PL/PgSQL

-- 恢复 pg-boss 需要的函数
-- 从备份文件 rds_init_20251224_154529.sql 提取
-- 1. create_queue 函数
CREATE OR REPLACE FUNCTION platform_schema.create_queue(queue_name text, options jsonb) RETURNS void
LANGUAGE plpgsql
AS $_$
DECLARE
tablename varchar := CASE WHEN options->>'partition' = 'true'
THEN 'j' || encode(sha224(queue_name::bytea), 'hex')
ELSE 'job_common'
END;
queue_created_on timestamptz;
BEGIN
WITH q as (
INSERT INTO platform_schema.queue (
name,
policy,
retry_limit,
retry_delay,
retry_backoff,
retry_delay_max,
expire_seconds,
retention_seconds,
deletion_seconds,
warning_queued,
dead_letter,
partition,
table_name
)
VALUES (
queue_name,
options->>'policy',
COALESCE((options->>'retryLimit')::int, 2),
COALESCE((options->>'retryDelay')::int, 0),
COALESCE((options->>'retryBackoff')::bool, false),
(options->>'retryDelayMax')::int,
COALESCE((options->>'expireInSeconds')::int, 900),
COALESCE((options->>'retentionSeconds')::int, 1209600),
COALESCE((options->>'deleteAfterSeconds')::int, 604800),
COALESCE((options->>'warningQueueSize')::int, 0),
options->>'deadLetter',
COALESCE((options->>'partition')::bool, false),
tablename
)
ON CONFLICT DO NOTHING
RETURNING created_on
)
SELECT created_on into queue_created_on from q;
IF queue_created_on IS NULL OR options->>'partition' IS DISTINCT FROM 'true' THEN
RETURN;
END IF;
EXECUTE format('CREATE TABLE platform_schema.%I (LIKE platform_schema.job INCLUDING DEFAULTS)', tablename);
EXECUTE format('ALTER TABLE platform_schema.%1$I ADD PRIMARY KEY (name, id)', tablename);
EXECUTE format('ALTER TABLE platform_schema.%1$I ADD CONSTRAINT q_fkey FOREIGN KEY (name) REFERENCES platform_schema.queue (name) ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED', tablename);
EXECUTE format('ALTER TABLE platform_schema.%1$I ADD CONSTRAINT dlq_fkey FOREIGN KEY (dead_letter) REFERENCES platform_schema.queue (name) ON DELETE RESTRICT DEFERRABLE INITIALLY DEFERRED', tablename);
EXECUTE format('CREATE INDEX %1$s_i5 ON platform_schema.%1$I (name, start_after) INCLUDE (priority, created_on, id) WHERE state < ''active''', tablename);
EXECUTE format('CREATE UNIQUE INDEX %1$s_i4 ON platform_schema.%1$I (name, singleton_on, COALESCE(singleton_key, '''')) WHERE state <> ''cancelled'' AND singleton_on IS NOT NULL', tablename);
IF options->>'policy' = 'short' THEN
EXECUTE format('CREATE UNIQUE INDEX %1$s_i1 ON platform_schema.%1$I (name, COALESCE(singleton_key, '''')) WHERE state = ''created'' AND policy = ''short''', tablename);
ELSIF options->>'policy' = 'singleton' THEN
EXECUTE format('CREATE UNIQUE INDEX %1$s_i2 ON platform_schema.%1$I (name, COALESCE(singleton_key, '''')) WHERE state = ''active'' AND policy = ''singleton''', tablename);
ELSIF options->>'policy' = 'stately' THEN
EXECUTE format('CREATE UNIQUE INDEX %1$s_i3 ON platform_schema.%1$I (name, state, COALESCE(singleton_key, '''')) WHERE state <= ''active'' AND policy = ''stately''', tablename);
ELSIF options->>'policy' = 'exclusive' THEN
EXECUTE format('CREATE UNIQUE INDEX %1$s_i6 ON platform_schema.%1$I (name, COALESCE(singleton_key, '''')) WHERE state <= ''active'' AND policy = ''exclusive''', tablename);
END IF;
EXECUTE format('ALTER TABLE platform_schema.%I ADD CONSTRAINT cjc CHECK (name=%L)', tablename, queue_name);
EXECUTE format('ALTER TABLE platform_schema.job ATTACH PARTITION platform_schema.%I FOR VALUES IN (%L)', tablename, queue_name);
END;
$_$;
-- 2. delete_queue 函数
CREATE OR REPLACE FUNCTION platform_schema.delete_queue(queue_name text) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_table varchar;
v_partition bool;
BEGIN
SELECT table_name, partition
FROM platform_schema.queue
WHERE name = queue_name
INTO v_table, v_partition;
IF v_partition THEN
EXECUTE format('DROP TABLE IF EXISTS platform_schema.%I', v_table);
ELSE
EXECUTE format('DELETE FROM platform_schema.%I WHERE name = %L', v_table, queue_name);
END IF;
DELETE FROM platform_schema.queue WHERE name = queue_name;
END;
$$;