feat: add wechat publish recovery and conversation memory

This commit is contained in:
john
2026-06-29 06:59:37 +08:00
parent 615a1e9bf4
commit 66acbc902c
12 changed files with 2308 additions and 38 deletions
+46
View File
@@ -232,6 +232,52 @@ export async function migrateSchema(pool) {
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`);
// Per-user conversation memory: raw dialogue rows plus lightweight extracted
// memory items. This is intentionally additive and can be disabled by env.
await pool.query(`
CREATE TABLE IF NOT EXISTS h5_conversation_messages (
id CHAR(64) PRIMARY KEY,
user_id CHAR(36) NOT NULL,
agent_session_id VARCHAR(128) NOT NULL,
message_key VARCHAR(128) NOT NULL,
sequence_no INT NOT NULL DEFAULT 0,
role VARCHAR(32) NOT NULL,
text MEDIUMTEXT NOT NULL,
raw_json LONGTEXT NULL,
analyzed_at BIGINT NULL,
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
UNIQUE KEY uq_h5_conversation_message (agent_session_id, message_key),
KEY idx_h5_conversation_user_created (user_id, created_at),
KEY idx_h5_conversation_user_analyzed (user_id, analyzed_at),
CONSTRAINT fk_h5_conversation_user FOREIGN KEY (user_id) REFERENCES h5_users(id) ON DELETE CASCADE,
CONSTRAINT fk_h5_conversation_session FOREIGN KEY (agent_session_id) REFERENCES h5_user_sessions(agent_session_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`);
await pool.query(`
CREATE TABLE IF NOT EXISTS h5_user_memory_items (
id CHAR(64) PRIMARY KEY,
user_id CHAR(36) NOT NULL,
label ENUM('preference', 'habit', 'interest', 'goal', 'fact', 'experience', 'knowledge') NOT NULL DEFAULT 'fact',
memory_hash CHAR(64) NOT NULL,
memory_text TEXT NOT NULL,
evidence_message_id CHAR(64) NULL,
source_session_id VARCHAR(128) NULL,
confidence DECIMAL(4,3) NOT NULL DEFAULT 0.600,
status ENUM('active', 'archived', 'deleted') NOT NULL DEFAULT 'active',
raw_json JSON NULL,
created_at BIGINT NOT NULL,
updated_at BIGINT NOT NULL,
UNIQUE KEY uq_h5_user_memory_hash (user_id, memory_hash),
KEY idx_h5_user_memory_user_label (user_id, label, updated_at),
KEY idx_h5_user_memory_session (source_session_id),
CONSTRAINT fk_h5_user_memory_user FOREIGN KEY (user_id) REFERENCES h5_users(id) ON DELETE CASCADE,
CONSTRAINT fk_h5_user_memory_evidence FOREIGN KEY (evidence_message_id) REFERENCES h5_conversation_messages(id) ON DELETE SET NULL,
CONSTRAINT fk_h5_user_memory_session FOREIGN KEY (source_session_id) REFERENCES h5_user_sessions(agent_session_id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
`);
const publishColumns = [
['user_confirmed_at', 'BIGINT NULL AFTER expires_at'],
['plaza_view_count', 'BIGINT NOT NULL DEFAULT 0'],