mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
refactor(core): migrate channel identity and binding across app
Align channel identity and bind flow across backend and app-facing layers, including generated swagger artifacts and package lock updates while excluding docs content changes.
This commit is contained in:
@@ -1,26 +1,24 @@
|
||||
DROP TABLE IF EXISTS user_settings;
|
||||
DROP TABLE IF EXISTS subagents;
|
||||
DROP TABLE IF EXISTS schedule;
|
||||
DROP TABLE IF EXISTS lifecycle_events;
|
||||
DROP TABLE IF EXISTS container_versions;
|
||||
DROP TABLE IF EXISTS snapshots;
|
||||
DROP TABLE IF EXISTS containers;
|
||||
DROP TABLE IF EXISTS channel_sessions;
|
||||
DROP TABLE IF EXISTS contact_channels;
|
||||
DROP TABLE IF EXISTS chat_routes;
|
||||
DROP TABLE IF EXISTS chat_messages;
|
||||
DROP TABLE IF EXISTS chat_channel_identity_presence;
|
||||
DROP TABLE IF EXISTS chat_participants;
|
||||
DROP TABLE IF EXISTS chats;
|
||||
DROP TABLE IF EXISTS channel_identity_bind_codes;
|
||||
DROP TABLE IF EXISTS bot_preauth_keys;
|
||||
DROP TABLE IF EXISTS bot_channel_configs;
|
||||
DROP TABLE IF EXISTS user_channel_bindings;
|
||||
DROP TABLE IF EXISTS history;
|
||||
DROP TABLE IF EXISTS conversations;
|
||||
DROP TABLE IF EXISTS mcp_connections;
|
||||
DROP TABLE IF EXISTS bot_model_configs;
|
||||
DROP TABLE IF EXISTS bot_settings;
|
||||
DROP TABLE IF EXISTS bot_members;
|
||||
DROP TABLE IF EXISTS contact_bind_tokens;
|
||||
DROP TABLE IF EXISTS bots;
|
||||
DROP TABLE IF EXISTS model_variants;
|
||||
DROP TABLE IF EXISTS models;
|
||||
DROP TABLE IF EXISTS llm_providers;
|
||||
DROP TABLE IF EXISTS user_channel_bindings;
|
||||
DROP TABLE IF EXISTS channel_identities;
|
||||
DROP TABLE IF EXISTS users;
|
||||
DROP TABLE IF EXISTS contacts;
|
||||
-- DROP TABLE IF EXISTS model_variants;
|
||||
-- DROP TABLE IF EXISTS models;
|
||||
-- DROP TABLE IF EXISTS llm_providers;
|
||||
DROP TYPE IF EXISTS user_role;
|
||||
|
||||
+139
-97
@@ -8,23 +8,58 @@ BEGIN
|
||||
END
|
||||
$$;
|
||||
|
||||
-- users: Memoh user principal
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
username TEXT NOT NULL,
|
||||
username TEXT,
|
||||
email TEXT,
|
||||
password_hash TEXT NOT NULL,
|
||||
password_hash TEXT,
|
||||
role user_role NOT NULL DEFAULT 'member',
|
||||
display_name TEXT,
|
||||
avatar_url TEXT,
|
||||
is_active BOOLEAN NOT NULL DEFAULT true,
|
||||
data_root TEXT,
|
||||
last_login_at TIMESTAMPTZ,
|
||||
chat_model_id TEXT,
|
||||
memory_model_id TEXT,
|
||||
embedding_model_id TEXT,
|
||||
max_context_load_time INTEGER NOT NULL DEFAULT 1440,
|
||||
language TEXT NOT NULL DEFAULT 'auto',
|
||||
is_active BOOLEAN NOT NULL DEFAULT true,
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
last_login_at TIMESTAMPTZ,
|
||||
CONSTRAINT users_email_unique UNIQUE (email),
|
||||
CONSTRAINT users_username_unique UNIQUE (username)
|
||||
);
|
||||
|
||||
-- channel_identities: unified inbound identity subject
|
||||
CREATE TABLE IF NOT EXISTS channel_identities (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
channel TEXT NOT NULL,
|
||||
channel_subject_id TEXT NOT NULL,
|
||||
display_name TEXT,
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT channel_identities_channel_subject_unique UNIQUE (channel, channel_subject_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_channel_identities_user_id ON channel_identities(user_id);
|
||||
|
||||
-- user_channel_bindings: outbound delivery config
|
||||
CREATE TABLE IF NOT EXISTS user_channel_bindings (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
platform TEXT NOT NULL,
|
||||
config JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT user_channel_bindings_unique UNIQUE (user_id, platform)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_channel_bindings_user_id ON user_channel_bindings(user_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS llm_providers (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
name TEXT NOT NULL,
|
||||
@@ -73,6 +108,12 @@ CREATE TABLE IF NOT EXISTS bots (
|
||||
display_name TEXT,
|
||||
avatar_url TEXT,
|
||||
is_active BOOLEAN NOT NULL DEFAULT true,
|
||||
max_context_load_time INTEGER NOT NULL DEFAULT 1440,
|
||||
language TEXT NOT NULL DEFAULT 'auto',
|
||||
allow_guest BOOLEAN NOT NULL DEFAULT false,
|
||||
chat_model_id UUID REFERENCES models(id) ON DELETE SET NULL,
|
||||
memory_model_id UUID REFERENCES models(id) ON DELETE SET NULL,
|
||||
embedding_model_id UUID REFERENCES models(id) ON DELETE SET NULL,
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
@@ -92,20 +133,6 @@ CREATE TABLE IF NOT EXISTS bot_members (
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bot_members_user_id ON bot_members(user_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS bot_settings (
|
||||
bot_id UUID PRIMARY KEY REFERENCES bots(id) ON DELETE CASCADE,
|
||||
max_context_load_time INTEGER NOT NULL DEFAULT 1440,
|
||||
language TEXT NOT NULL DEFAULT 'auto',
|
||||
allow_guest BOOLEAN NOT NULL DEFAULT false
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS bot_model_configs (
|
||||
bot_id UUID PRIMARY KEY REFERENCES bots(id) ON DELETE CASCADE,
|
||||
chat_model_id UUID REFERENCES models(id) ON DELETE SET NULL,
|
||||
embedding_model_id UUID REFERENCES models(id) ON DELETE SET NULL,
|
||||
memory_model_id UUID REFERENCES models(id) ON DELETE SET NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS mcp_connections (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
|
||||
@@ -121,45 +148,85 @@ CREATE TABLE IF NOT EXISTS mcp_connections (
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_mcp_connections_bot_id ON mcp_connections(bot_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS conversations (
|
||||
-- chats: first-class conversation container
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
|
||||
session_id TEXT NOT NULL,
|
||||
channel_type TEXT NOT NULL,
|
||||
chat_id TEXT,
|
||||
sender_id TEXT,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT conversations_session_unique UNIQUE (bot_id, session_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_conversations_bot_id ON conversations(bot_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS history (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
|
||||
session_id TEXT NOT NULL,
|
||||
messages JSONB NOT NULL,
|
||||
kind TEXT NOT NULL CHECK (kind IN ('direct', 'group', 'thread')),
|
||||
parent_chat_id UUID REFERENCES chats(id) ON DELETE CASCADE,
|
||||
title TEXT,
|
||||
created_by_user_id UUID REFERENCES users(id),
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
skills TEXT[] NOT NULL DEFAULT '{}'::text[],
|
||||
timestamp TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_history_bot ON history(bot_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_history_session ON history(session_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_history_timestamp ON history(timestamp);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_channel_bindings (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
channel_type TEXT NOT NULL,
|
||||
config JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
enable_chat_memory BOOLEAN NOT NULL DEFAULT true,
|
||||
enable_private_memory BOOLEAN NOT NULL DEFAULT true,
|
||||
enable_public_memory BOOLEAN NOT NULL DEFAULT false,
|
||||
model_id TEXT,
|
||||
settings_metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT user_channel_bindings_unique UNIQUE (user_id, channel_type)
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_channel_bindings_user_id ON user_channel_bindings(user_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_chats_bot_id ON chats(bot_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_chats_parent ON chats(parent_chat_id);
|
||||
|
||||
-- chat_participants: chat membership
|
||||
CREATE TABLE IF NOT EXISTS chat_participants (
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
role TEXT NOT NULL DEFAULT 'member' CHECK (role IN ('owner', 'admin', 'member')),
|
||||
joined_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_chat_participants_user ON chat_participants(user_id);
|
||||
|
||||
-- chat_messages: per-message storage (replaces history)
|
||||
CREATE TABLE IF NOT EXISTS chat_messages (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
|
||||
route_id UUID,
|
||||
sender_channel_identity_id UUID REFERENCES channel_identities(id),
|
||||
sender_user_id UUID REFERENCES users(id),
|
||||
platform TEXT,
|
||||
external_message_id TEXT,
|
||||
role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'system', 'tool')),
|
||||
content JSONB NOT NULL,
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Backfill newly introduced columns for existing deployments where chat_messages
|
||||
-- was created before route/platform traceability fields were added.
|
||||
ALTER TABLE IF EXISTS chat_messages
|
||||
ADD COLUMN IF NOT EXISTS route_id UUID;
|
||||
|
||||
ALTER TABLE IF EXISTS chat_messages
|
||||
ADD COLUMN IF NOT EXISTS platform TEXT;
|
||||
|
||||
ALTER TABLE IF EXISTS chat_messages
|
||||
ADD COLUMN IF NOT EXISTS external_message_id TEXT;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_chat_messages_chat_created ON chat_messages(chat_id, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_chat_messages_bot ON chat_messages(bot_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_chat_messages_route ON chat_messages(route_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_chat_messages_external_lookup
|
||||
ON chat_messages(platform, external_message_id);
|
||||
|
||||
-- chat_channel_identity_presence: derived cache of channel identities observed in chats
|
||||
CREATE TABLE IF NOT EXISTS chat_channel_identity_presence (
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
channel_identity_id UUID NOT NULL REFERENCES channel_identities(id) ON DELETE CASCADE,
|
||||
first_seen_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
last_seen_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
message_count BIGINT NOT NULL DEFAULT 1,
|
||||
PRIMARY KEY (chat_id, channel_identity_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_chat_channel_identity_presence_identity_last_seen
|
||||
ON chat_channel_identity_presence(channel_identity_id, last_seen_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_chat_channel_identity_presence_chat_last_seen
|
||||
ON chat_channel_identity_presence(chat_id, last_seen_at DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS bot_channel_configs (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
@@ -183,26 +250,6 @@ CREATE UNIQUE INDEX IF NOT EXISTS idx_bot_channel_external_identity
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bot_channel_bot_id ON bot_channel_configs(bot_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS contacts (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
|
||||
user_id UUID REFERENCES users(id) ON DELETE SET NULL,
|
||||
display_name TEXT,
|
||||
alias TEXT,
|
||||
tags TEXT[] NOT NULL DEFAULT '{}'::text[],
|
||||
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(),
|
||||
CONSTRAINT contacts_status_check CHECK (status IN ('active', 'blocked', 'pending'))
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_contacts_bot_user_unique
|
||||
ON contacts(bot_id, user_id)
|
||||
WHERE user_id IS NOT NULL;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_contacts_bot_id ON contacts(bot_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS bot_preauth_keys (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
|
||||
@@ -217,37 +264,40 @@ CREATE TABLE IF NOT EXISTS bot_preauth_keys (
|
||||
CREATE INDEX IF NOT EXISTS idx_bot_preauth_keys_bot_id ON bot_preauth_keys(bot_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_bot_preauth_keys_expires ON bot_preauth_keys(expires_at);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS contact_channels (
|
||||
-- channel_identity_bind_codes: one-time codes for channel identity->user linking
|
||||
CREATE TABLE IF NOT EXISTS channel_identity_bind_codes (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
|
||||
contact_id UUID NOT NULL REFERENCES contacts(id) ON DELETE CASCADE,
|
||||
platform TEXT NOT NULL,
|
||||
external_id TEXT NOT NULL,
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
token TEXT NOT NULL,
|
||||
issued_by_user_id UUID NOT NULL REFERENCES users(id),
|
||||
platform TEXT,
|
||||
expires_at TIMESTAMPTZ,
|
||||
used_at TIMESTAMPTZ,
|
||||
used_by_channel_identity_id UUID REFERENCES channel_identities(id),
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT contact_channels_unique UNIQUE (bot_id, platform, external_id)
|
||||
CONSTRAINT channel_identity_bind_codes_token_unique UNIQUE (token)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_contact_channels_contact_id ON contact_channels(contact_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_contact_channels_platform_external ON contact_channels(platform, external_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_channel_identity_bind_codes_platform ON channel_identity_bind_codes(platform);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS channel_sessions (
|
||||
session_id TEXT PRIMARY KEY,
|
||||
-- chat_routes: routing mapping (replaces channel_sessions)
|
||||
CREATE TABLE IF NOT EXISTS chat_routes (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
|
||||
channel_config_id UUID REFERENCES bot_channel_configs(id) ON DELETE SET NULL,
|
||||
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
|
||||
contact_id UUID REFERENCES contacts(id) ON DELETE SET NULL,
|
||||
platform TEXT NOT NULL,
|
||||
reply_target TEXT,
|
||||
channel_config_id UUID REFERENCES bot_channel_configs(id) ON DELETE SET NULL,
|
||||
conversation_id TEXT NOT NULL,
|
||||
thread_id TEXT,
|
||||
reply_target TEXT,
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_channel_sessions_bot_id ON channel_sessions(bot_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_channel_sessions_user_id ON channel_sessions(user_id);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_chat_routes_unique
|
||||
ON chat_routes (bot_id, platform, conversation_id, COALESCE(thread_id, ''));
|
||||
CREATE INDEX IF NOT EXISTS idx_chat_routes_chat ON chat_routes(chat_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_chat_routes_bot ON chat_routes(bot_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS containers (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
@@ -339,11 +389,3 @@ CREATE TABLE IF NOT EXISTS subagents (
|
||||
CREATE INDEX IF NOT EXISTS idx_subagents_bot_id ON subagents(bot_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_subagents_deleted ON subagents(deleted);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS user_settings (
|
||||
user_id UUID PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
|
||||
chat_model_id TEXT,
|
||||
memory_model_id TEXT,
|
||||
embedding_model_id TEXT,
|
||||
max_context_load_time INTEGER NOT NULL DEFAULT 1440,
|
||||
language TEXT NOT NULL DEFAULT 'auto'
|
||||
);
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
-- name: CreateBindCode :one
|
||||
INSERT INTO channel_identity_bind_codes (token, issued_by_user_id, platform, expires_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, token, issued_by_user_id, platform, expires_at, used_at, used_by_channel_identity_id, created_at;
|
||||
|
||||
-- name: GetBindCode :one
|
||||
SELECT id, token, issued_by_user_id, platform, expires_at, used_at, used_by_channel_identity_id, created_at
|
||||
FROM channel_identity_bind_codes
|
||||
WHERE token = $1;
|
||||
|
||||
-- name: GetBindCodeForUpdate :one
|
||||
SELECT id, token, issued_by_user_id, platform, expires_at, used_at, used_by_channel_identity_id, created_at
|
||||
FROM channel_identity_bind_codes
|
||||
WHERE token = $1
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: MarkBindCodeUsed :one
|
||||
UPDATE channel_identity_bind_codes
|
||||
SET used_at = now(), used_by_channel_identity_id = $2
|
||||
WHERE id = $1
|
||||
AND used_at IS NULL
|
||||
RETURNING id, token, issued_by_user_id, platform, expires_at, used_at, used_by_channel_identity_id, created_at;
|
||||
+6
-6
@@ -1,21 +1,21 @@
|
||||
-- name: CreateBot :one
|
||||
INSERT INTO bots (owner_user_id, type, display_name, avatar_url, is_active, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, metadata, created_at, updated_at;
|
||||
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, metadata, created_at, updated_at;
|
||||
|
||||
-- name: GetBotByID :one
|
||||
SELECT id, owner_user_id, type, display_name, avatar_url, is_active, metadata, created_at, updated_at
|
||||
SELECT id, owner_user_id, type, display_name, avatar_url, is_active, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, metadata, created_at, updated_at
|
||||
FROM bots
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: ListBotsByOwner :many
|
||||
SELECT id, owner_user_id, type, display_name, avatar_url, is_active, metadata, created_at, updated_at
|
||||
SELECT id, owner_user_id, type, display_name, avatar_url, is_active, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, metadata, created_at, updated_at
|
||||
FROM bots
|
||||
WHERE owner_user_id = $1
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: ListBotsByMember :many
|
||||
SELECT b.id, b.owner_user_id, b.type, b.display_name, b.avatar_url, b.is_active, b.metadata, b.created_at, b.updated_at
|
||||
SELECT b.id, b.owner_user_id, b.type, b.display_name, b.avatar_url, b.is_active, b.max_context_load_time, b.language, b.allow_guest, b.chat_model_id, b.memory_model_id, b.embedding_model_id, b.metadata, b.created_at, b.updated_at
|
||||
FROM bots b
|
||||
JOIN bot_members m ON m.bot_id = b.id
|
||||
WHERE m.user_id = $1
|
||||
@@ -29,14 +29,14 @@ SET display_name = $2,
|
||||
metadata = $5,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, metadata, created_at, updated_at;
|
||||
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, metadata, created_at, updated_at;
|
||||
|
||||
-- name: UpdateBotOwner :one
|
||||
UPDATE bots
|
||||
SET owner_user_id = $2,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, metadata, created_at, updated_at;
|
||||
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, metadata, created_at, updated_at;
|
||||
|
||||
-- name: DeleteBotByID :exec
|
||||
DELETE FROM bots WHERE id = $1;
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
-- name: CreateChannelIdentity :one
|
||||
INSERT INTO channel_identities (user_id, channel, channel_subject_id, display_name, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id, user_id, channel, channel_subject_id, display_name, metadata, created_at, updated_at;
|
||||
|
||||
-- name: GetChannelIdentityByID :one
|
||||
SELECT id, user_id, channel, channel_subject_id, display_name, metadata, created_at, updated_at
|
||||
FROM channel_identities
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: GetChannelIdentityByIDForUpdate :one
|
||||
SELECT id, user_id, channel, channel_subject_id, display_name, metadata, created_at, updated_at
|
||||
FROM channel_identities
|
||||
WHERE id = $1
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: GetChannelIdentityByChannelSubject :one
|
||||
SELECT id, user_id, channel, channel_subject_id, display_name, metadata, created_at, updated_at
|
||||
FROM channel_identities
|
||||
WHERE channel = $1 AND channel_subject_id = $2;
|
||||
|
||||
-- name: UpsertChannelIdentityByChannelSubject :one
|
||||
INSERT INTO channel_identities (user_id, channel, channel_subject_id, display_name, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (channel, channel_subject_id)
|
||||
DO UPDATE SET
|
||||
display_name = EXCLUDED.display_name,
|
||||
metadata = EXCLUDED.metadata,
|
||||
user_id = COALESCE(channel_identities.user_id, EXCLUDED.user_id),
|
||||
updated_at = now()
|
||||
RETURNING id, user_id, channel, channel_subject_id, display_name, metadata, created_at, updated_at;
|
||||
|
||||
-- name: ListChannelIdentitiesByUserID :many
|
||||
SELECT id, user_id, channel, channel_subject_id, display_name, metadata, created_at, updated_at
|
||||
FROM channel_identities
|
||||
WHERE user_id = $1
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: SetChannelIdentityLinkedUser :one
|
||||
UPDATE channel_identities
|
||||
SET user_id = $2, updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id, user_id, channel, channel_subject_id, display_name, metadata, created_at, updated_at;
|
||||
|
||||
-- name: ClearChannelIdentityLinkedUser :one
|
||||
UPDATE channel_identities
|
||||
SET user_id = NULL, updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id, user_id, channel, channel_subject_id, display_name, metadata, created_at, updated_at;
|
||||
+8
-39
@@ -34,54 +34,23 @@ WHERE channel_type = $1
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: GetUserChannelBinding :one
|
||||
SELECT id, user_id, channel_type, config, created_at, updated_at
|
||||
SELECT id, user_id, platform, config, created_at, updated_at
|
||||
FROM user_channel_bindings
|
||||
WHERE user_id = $1 AND channel_type = $2
|
||||
WHERE user_id = $1 AND platform = $2
|
||||
LIMIT 1;
|
||||
|
||||
-- name: UpsertUserChannelBinding :one
|
||||
INSERT INTO user_channel_bindings (user_id, channel_type, config)
|
||||
INSERT INTO user_channel_bindings (user_id, platform, config)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (user_id, channel_type)
|
||||
ON CONFLICT (user_id, platform)
|
||||
DO UPDATE SET
|
||||
config = EXCLUDED.config,
|
||||
updated_at = now()
|
||||
RETURNING id, user_id, channel_type, config, created_at, updated_at;
|
||||
RETURNING id, user_id, platform, config, created_at, updated_at;
|
||||
|
||||
-- name: ListUserChannelBindingsByType :many
|
||||
SELECT id, user_id, channel_type, config, created_at, updated_at
|
||||
-- name: ListUserChannelBindingsByPlatform :many
|
||||
SELECT id, user_id, platform, config, created_at, updated_at
|
||||
FROM user_channel_bindings
|
||||
WHERE channel_type = $1
|
||||
WHERE platform = $1
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: GetChannelSessionByID :one
|
||||
SELECT session_id, bot_id, channel_config_id, user_id, contact_id, platform, reply_target, thread_id, metadata, created_at, updated_at
|
||||
FROM channel_sessions
|
||||
WHERE session_id = $1
|
||||
LIMIT 1;
|
||||
|
||||
-- name: ListChannelSessionsByBotPlatform :many
|
||||
SELECT session_id, bot_id, channel_config_id, user_id, contact_id, platform, reply_target, thread_id, metadata, created_at, updated_at
|
||||
FROM channel_sessions
|
||||
WHERE bot_id = $1 AND platform = $2
|
||||
ORDER BY updated_at DESC;
|
||||
|
||||
-- name: UpsertChannelSession :one
|
||||
INSERT INTO channel_sessions (session_id, bot_id, channel_config_id, user_id, contact_id, platform, reply_target, thread_id, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
|
||||
ON CONFLICT (session_id)
|
||||
DO UPDATE SET
|
||||
bot_id = EXCLUDED.bot_id,
|
||||
channel_config_id = EXCLUDED.channel_config_id,
|
||||
user_id = EXCLUDED.user_id,
|
||||
contact_id = EXCLUDED.contact_id,
|
||||
platform = EXCLUDED.platform,
|
||||
reply_target = EXCLUDED.reply_target,
|
||||
thread_id = EXCLUDED.thread_id,
|
||||
metadata = EXCLUDED.metadata,
|
||||
updated_at = now()
|
||||
RETURNING session_id, bot_id, channel_config_id, user_id, contact_id, platform, reply_target, thread_id, metadata, created_at, updated_at;
|
||||
|
||||
-- name: DeleteChannelSession :exec
|
||||
DELETE FROM channel_sessions
|
||||
WHERE session_id = $1;
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
-- name: CreateChat :one
|
||||
INSERT INTO chats (bot_id, kind, parent_chat_id, title, created_by_user_id, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, bot_id, kind, parent_chat_id, title, created_by_user_id, metadata, enable_chat_memory, enable_private_memory, enable_public_memory, model_id, settings_metadata, created_at, updated_at;
|
||||
|
||||
-- name: GetChatByID :one
|
||||
SELECT id, bot_id, kind, parent_chat_id, title, created_by_user_id, metadata, enable_chat_memory, enable_private_memory, enable_public_memory, model_id, settings_metadata, created_at, updated_at
|
||||
FROM chats
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: ListChatsByBotAndUser :many
|
||||
SELECT c.id, c.bot_id, c.kind, c.parent_chat_id, c.title, c.created_by_user_id, c.metadata, c.enable_chat_memory, c.enable_private_memory, c.enable_public_memory, c.model_id, c.settings_metadata, c.created_at, c.updated_at
|
||||
FROM chats c
|
||||
JOIN chat_participants cp ON cp.chat_id = c.id
|
||||
WHERE c.bot_id = $1 AND cp.user_id = $2
|
||||
ORDER BY c.updated_at DESC;
|
||||
|
||||
-- name: ListVisibleChatsByBotAndUser :many
|
||||
WITH participant_chats AS (
|
||||
SELECT c.id, c.bot_id, c.kind, c.parent_chat_id, c.title, c.created_by_user_id, c.metadata, c.created_at, c.updated_at,
|
||||
'participant'::text AS access_mode,
|
||||
cp.role AS participant_role,
|
||||
NULL::timestamptz AS last_observed_at
|
||||
FROM chats c
|
||||
JOIN chat_participants cp ON cp.chat_id = c.id
|
||||
WHERE c.bot_id = $1 AND cp.user_id = $2
|
||||
),
|
||||
observed_chats AS (
|
||||
SELECT c.id, c.bot_id, c.kind, c.parent_chat_id, c.title, c.created_by_user_id, c.metadata, c.created_at, c.updated_at,
|
||||
'channel_identity_observed'::text AS access_mode,
|
||||
''::text AS participant_role,
|
||||
MAX(cap.last_seen_at) AS last_observed_at
|
||||
FROM chats c
|
||||
JOIN chat_channel_identity_presence cap ON cap.chat_id = c.id
|
||||
JOIN channel_identities ci ON ci.id = cap.channel_identity_id
|
||||
WHERE c.bot_id = $1
|
||||
AND ci.user_id = $2
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM chat_participants cp
|
||||
WHERE cp.chat_id = c.id AND cp.user_id = $2
|
||||
)
|
||||
GROUP BY c.id, c.bot_id, c.kind, c.parent_chat_id, c.title, c.created_by_user_id, c.metadata, c.created_at, c.updated_at
|
||||
)
|
||||
SELECT id, bot_id, kind, parent_chat_id, title, created_by_user_id, metadata, created_at, updated_at,
|
||||
access_mode, participant_role, last_observed_at
|
||||
FROM (
|
||||
SELECT * FROM participant_chats
|
||||
UNION ALL
|
||||
SELECT * FROM observed_chats
|
||||
) v
|
||||
ORDER BY v.updated_at DESC, v.last_observed_at DESC NULLS LAST;
|
||||
|
||||
-- name: GetChatReadAccessByUser :one
|
||||
WITH participant_access AS (
|
||||
SELECT 'participant'::text AS access_mode,
|
||||
cp.role AS participant_role,
|
||||
NULL::timestamptz AS last_observed_at
|
||||
FROM chat_participants cp
|
||||
WHERE cp.chat_id = $1 AND cp.user_id = $2
|
||||
),
|
||||
observed_access AS (
|
||||
SELECT 'channel_identity_observed'::text AS access_mode,
|
||||
''::text AS participant_role,
|
||||
MAX(cap.last_seen_at) AS last_observed_at
|
||||
FROM chat_channel_identity_presence cap
|
||||
JOIN channel_identities ci ON ci.id = cap.channel_identity_id
|
||||
WHERE cap.chat_id = $1 AND ci.user_id = $2
|
||||
GROUP BY cap.chat_id
|
||||
),
|
||||
all_access AS (
|
||||
SELECT * FROM participant_access
|
||||
UNION ALL
|
||||
SELECT * FROM observed_access
|
||||
)
|
||||
SELECT access_mode, participant_role, last_observed_at
|
||||
FROM all_access
|
||||
ORDER BY CASE WHEN access_mode = 'participant' THEN 0 ELSE 1 END, last_observed_at DESC NULLS LAST
|
||||
LIMIT 1;
|
||||
|
||||
-- name: ListThreadsByParent :many
|
||||
SELECT id, bot_id, kind, parent_chat_id, title, created_by_user_id, metadata, enable_chat_memory, enable_private_memory, enable_public_memory, model_id, settings_metadata, created_at, updated_at
|
||||
FROM chats
|
||||
WHERE parent_chat_id = $1 AND kind = 'thread'
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: UpdateChatTitle :one
|
||||
UPDATE chats SET title = $2, updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id, bot_id, kind, parent_chat_id, title, created_by_user_id, metadata, enable_chat_memory, enable_private_memory, enable_public_memory, model_id, settings_metadata, created_at, updated_at;
|
||||
|
||||
-- name: TouchChat :exec
|
||||
UPDATE chats SET updated_at = now() WHERE id = $1;
|
||||
|
||||
-- name: DeleteChat :exec
|
||||
DELETE FROM chats WHERE id = $1;
|
||||
|
||||
-- chat_participants
|
||||
|
||||
-- name: AddChatParticipant :one
|
||||
INSERT INTO chat_participants (chat_id, user_id, role)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (chat_id, user_id) DO UPDATE SET role = EXCLUDED.role
|
||||
RETURNING chat_id, user_id, role, joined_at;
|
||||
|
||||
-- name: GetChatParticipant :one
|
||||
SELECT chat_id, user_id, role, joined_at
|
||||
FROM chat_participants
|
||||
WHERE chat_id = $1 AND user_id = $2;
|
||||
|
||||
-- name: ListChatParticipants :many
|
||||
SELECT chat_id, user_id, role, joined_at
|
||||
FROM chat_participants
|
||||
WHERE chat_id = $1
|
||||
ORDER BY joined_at ASC;
|
||||
|
||||
-- name: RemoveChatParticipant :exec
|
||||
DELETE FROM chat_participants WHERE chat_id = $1 AND user_id = $2;
|
||||
|
||||
-- name: CopyParticipantsToChat :exec
|
||||
INSERT INTO chat_participants (chat_id, user_id, role)
|
||||
SELECT $2, cp.user_id, cp.role FROM chat_participants cp WHERE cp.chat_id = $1
|
||||
ON CONFLICT (chat_id, user_id) DO NOTHING;
|
||||
|
||||
-- chat_settings
|
||||
|
||||
-- name: UpsertChatSettings :one
|
||||
UPDATE chats
|
||||
SET enable_chat_memory = $2,
|
||||
enable_private_memory = $3,
|
||||
enable_public_memory = $4,
|
||||
model_id = $5,
|
||||
settings_metadata = $6
|
||||
WHERE id = $1
|
||||
RETURNING id AS chat_id, enable_chat_memory, enable_private_memory, enable_public_memory, model_id, settings_metadata AS metadata, updated_at;
|
||||
|
||||
-- name: GetChatSettings :one
|
||||
SELECT id AS chat_id, enable_chat_memory, enable_private_memory, enable_public_memory, model_id, settings_metadata AS metadata, updated_at
|
||||
FROM chats
|
||||
WHERE id = $1;
|
||||
|
||||
-- chat_routes
|
||||
|
||||
-- name: CreateChatRoute :one
|
||||
INSERT INTO chat_routes (chat_id, bot_id, platform, channel_config_id, conversation_id, thread_id, reply_target, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING id, chat_id, bot_id, platform, channel_config_id, conversation_id, thread_id, reply_target, metadata, created_at, updated_at;
|
||||
|
||||
-- name: FindChatRoute :one
|
||||
SELECT id, chat_id, bot_id, platform, channel_config_id, conversation_id, thread_id, reply_target, metadata, created_at, updated_at
|
||||
FROM chat_routes
|
||||
WHERE bot_id = $1 AND platform = $2 AND conversation_id = $3
|
||||
AND COALESCE(thread_id, '') = COALESCE(sqlc.narg('thread_id'), '')
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetChatRouteByID :one
|
||||
SELECT id, chat_id, bot_id, platform, channel_config_id, conversation_id, thread_id, reply_target, metadata, created_at, updated_at
|
||||
FROM chat_routes
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: ListChatRoutes :many
|
||||
SELECT id, chat_id, bot_id, platform, channel_config_id, conversation_id, thread_id, reply_target, metadata, created_at, updated_at
|
||||
FROM chat_routes
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at ASC;
|
||||
|
||||
-- name: UpdateChatRouteReplyTarget :exec
|
||||
UPDATE chat_routes SET reply_target = $2, updated_at = now() WHERE id = $1;
|
||||
|
||||
-- name: DeleteChatRoute :exec
|
||||
DELETE FROM chat_routes WHERE id = $1;
|
||||
|
||||
-- chat_messages
|
||||
|
||||
-- name: CreateChatMessage :one
|
||||
INSERT INTO chat_messages (chat_id, bot_id, route_id, sender_channel_identity_id, sender_user_id, platform, external_message_id, role, content, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
RETURNING id, chat_id, bot_id, route_id, sender_channel_identity_id, sender_user_id, platform, external_message_id, role, content, metadata, created_at;
|
||||
|
||||
-- name: UpsertChatChannelIdentityPresence :exec
|
||||
INSERT INTO chat_channel_identity_presence (chat_id, channel_identity_id, first_seen_at, last_seen_at, message_count)
|
||||
VALUES ($1, $2, now(), now(), 1)
|
||||
ON CONFLICT (chat_id, channel_identity_id)
|
||||
DO UPDATE SET
|
||||
last_seen_at = now(),
|
||||
message_count = chat_channel_identity_presence.message_count + 1;
|
||||
|
||||
-- name: ListChatMessages :many
|
||||
SELECT id, chat_id, bot_id, route_id, sender_channel_identity_id, sender_user_id, platform, external_message_id, role, content, metadata, created_at
|
||||
FROM chat_messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at ASC;
|
||||
|
||||
-- name: ListChatMessagesSince :many
|
||||
SELECT id, chat_id, bot_id, route_id, sender_channel_identity_id, sender_user_id, platform, external_message_id, role, content, metadata, created_at
|
||||
FROM chat_messages
|
||||
WHERE chat_id = $1 AND created_at >= $2
|
||||
ORDER BY created_at ASC;
|
||||
|
||||
-- name: ListChatMessagesBefore :many
|
||||
SELECT id, chat_id, bot_id, route_id, sender_channel_identity_id, sender_user_id, platform, external_message_id, role, content, metadata, created_at
|
||||
FROM chat_messages
|
||||
WHERE chat_id = $1 AND created_at < $2
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $3;
|
||||
|
||||
-- name: ListChatMessagesLatest :many
|
||||
SELECT id, chat_id, bot_id, route_id, sender_channel_identity_id, sender_user_id, platform, external_message_id, role, content, metadata, created_at
|
||||
FROM chat_messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at DESC
|
||||
LIMIT $2;
|
||||
|
||||
-- name: DeleteChatMessagesByChat :exec
|
||||
DELETE FROM chat_messages WHERE chat_id = $1;
|
||||
@@ -1,76 +0,0 @@
|
||||
-- name: CreateContact :one
|
||||
INSERT INTO contacts (bot_id, user_id, display_name, alias, tags, status, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id, bot_id, user_id, display_name, alias, tags, status, metadata, created_at, updated_at;
|
||||
|
||||
-- name: GetContactByID :one
|
||||
SELECT id, bot_id, user_id, display_name, alias, tags, status, metadata, created_at, updated_at
|
||||
FROM contacts
|
||||
WHERE id = $1
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetContactByUserID :one
|
||||
SELECT id, bot_id, user_id, display_name, alias, tags, status, metadata, created_at, updated_at
|
||||
FROM contacts
|
||||
WHERE bot_id = $1 AND user_id = $2
|
||||
LIMIT 1;
|
||||
|
||||
-- name: ListContactsByBot :many
|
||||
SELECT id, bot_id, user_id, display_name, alias, tags, status, metadata, created_at, updated_at
|
||||
FROM contacts
|
||||
WHERE bot_id = $1
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: SearchContacts :many
|
||||
SELECT id, bot_id, user_id, display_name, alias, tags, status, metadata, created_at, updated_at
|
||||
FROM contacts
|
||||
WHERE bot_id = $1
|
||||
AND (
|
||||
display_name ILIKE sqlc.arg(query)
|
||||
OR alias ILIKE sqlc.arg(query)
|
||||
OR EXISTS (
|
||||
SELECT 1 FROM unnest(tags) AS tag WHERE tag ILIKE sqlc.arg(query)
|
||||
)
|
||||
)
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: UpdateContact :one
|
||||
UPDATE contacts
|
||||
SET display_name = COALESCE(sqlc.narg(display_name), display_name),
|
||||
alias = COALESCE(sqlc.narg(alias), alias),
|
||||
tags = COALESCE(sqlc.narg(tags), tags),
|
||||
status = COALESCE(NULLIF(sqlc.arg(status)::text, ''), status),
|
||||
metadata = COALESCE(sqlc.narg(metadata), metadata),
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)
|
||||
RETURNING id, bot_id, user_id, display_name, alias, tags, status, metadata, created_at, updated_at;
|
||||
|
||||
-- name: UpdateContactUser :one
|
||||
UPDATE contacts
|
||||
SET user_id = $2,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id, bot_id, user_id, display_name, alias, tags, status, metadata, created_at, updated_at;
|
||||
|
||||
-- name: UpsertContactChannel :one
|
||||
INSERT INTO contact_channels (bot_id, contact_id, platform, external_id, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (bot_id, platform, external_id)
|
||||
DO UPDATE SET
|
||||
contact_id = EXCLUDED.contact_id,
|
||||
metadata = EXCLUDED.metadata,
|
||||
updated_at = now()
|
||||
RETURNING id, bot_id, contact_id, platform, external_id, metadata, created_at, updated_at;
|
||||
|
||||
-- name: GetContactChannelByIdentity :one
|
||||
SELECT id, bot_id, contact_id, platform, external_id, metadata, created_at, updated_at
|
||||
FROM contact_channels
|
||||
WHERE bot_id = $1 AND platform = $2 AND external_id = $3
|
||||
LIMIT 1;
|
||||
|
||||
-- name: ListContactChannelsByContact :many
|
||||
SELECT id, bot_id, contact_id, platform, external_id, metadata, created_at, updated_at
|
||||
FROM contact_channels
|
||||
WHERE contact_id = $1
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
-- name: CreateHistory :one
|
||||
INSERT INTO history (bot_id, session_id, messages, metadata, skills, timestamp)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING id, bot_id, session_id, messages, metadata, skills, timestamp;
|
||||
|
||||
-- name: ListHistoryByBotSessionSince :many
|
||||
SELECT id, bot_id, session_id, messages, metadata, skills, timestamp
|
||||
FROM history
|
||||
WHERE bot_id = $1 AND session_id = $2 AND timestamp >= $3
|
||||
ORDER BY timestamp ASC;
|
||||
|
||||
-- name: GetHistoryByID :one
|
||||
SELECT id, bot_id, session_id, messages, metadata, skills, timestamp
|
||||
FROM history
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: ListHistoryByBotSession :many
|
||||
SELECT id, bot_id, session_id, messages, metadata, skills, timestamp
|
||||
FROM history
|
||||
WHERE bot_id = $1 AND session_id = $2
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT $3;
|
||||
|
||||
-- name: DeleteHistoryByID :exec
|
||||
DELETE FROM history
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: DeleteHistoryByBotSession :exec
|
||||
DELETE FROM history
|
||||
WHERE bot_id = $1 AND session_id = $2;
|
||||
|
||||
+51
-42
@@ -1,55 +1,64 @@
|
||||
-- name: GetSettingsByUserID :one
|
||||
SELECT user_id, chat_model_id, memory_model_id, embedding_model_id, max_context_load_time, language
|
||||
FROM user_settings
|
||||
WHERE user_id = $1;
|
||||
SELECT id AS user_id, chat_model_id, memory_model_id, embedding_model_id, max_context_load_time, language
|
||||
FROM users
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: UpsertUserSettings :one
|
||||
INSERT INTO user_settings (user_id, chat_model_id, memory_model_id, embedding_model_id, max_context_load_time, language)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT (user_id) DO UPDATE SET
|
||||
chat_model_id = EXCLUDED.chat_model_id,
|
||||
memory_model_id = EXCLUDED.memory_model_id,
|
||||
embedding_model_id = EXCLUDED.embedding_model_id,
|
||||
max_context_load_time = EXCLUDED.max_context_load_time,
|
||||
language = EXCLUDED.language
|
||||
RETURNING user_id, chat_model_id, memory_model_id, embedding_model_id, max_context_load_time, language;
|
||||
UPDATE users
|
||||
SET chat_model_id = $2,
|
||||
memory_model_id = $3,
|
||||
embedding_model_id = $4,
|
||||
max_context_load_time = $5,
|
||||
language = $6,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING id AS user_id, chat_model_id, memory_model_id, embedding_model_id, max_context_load_time, language;
|
||||
|
||||
-- name: GetSettingsByBotID :one
|
||||
SELECT bot_id, max_context_load_time, language, allow_guest
|
||||
FROM bot_settings
|
||||
WHERE bot_id = $1;
|
||||
|
||||
-- name: GetBotModelConfigByBotID :one
|
||||
SELECT
|
||||
bot_model_configs.bot_id,
|
||||
bots.id AS bot_id,
|
||||
bots.max_context_load_time,
|
||||
bots.language,
|
||||
bots.allow_guest,
|
||||
chat_models.model_id AS chat_model_id,
|
||||
memory_models.model_id AS memory_model_id,
|
||||
embedding_models.model_id AS embedding_model_id
|
||||
FROM bot_model_configs
|
||||
LEFT JOIN models AS chat_models ON chat_models.id = bot_model_configs.chat_model_id
|
||||
LEFT JOIN models AS memory_models ON memory_models.id = bot_model_configs.memory_model_id
|
||||
LEFT JOIN models AS embedding_models ON embedding_models.id = bot_model_configs.embedding_model_id
|
||||
WHERE bot_model_configs.bot_id = $1;
|
||||
FROM bots
|
||||
LEFT JOIN models AS chat_models ON chat_models.id = bots.chat_model_id
|
||||
LEFT JOIN models AS memory_models ON memory_models.id = bots.memory_model_id
|
||||
LEFT JOIN models AS embedding_models ON embedding_models.id = bots.embedding_model_id
|
||||
WHERE bots.id = $1;
|
||||
|
||||
-- name: UpsertBotSettings :one
|
||||
INSERT INTO bot_settings (bot_id, max_context_load_time, language, allow_guest)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (bot_id) DO UPDATE SET
|
||||
max_context_load_time = EXCLUDED.max_context_load_time,
|
||||
language = EXCLUDED.language,
|
||||
allow_guest = EXCLUDED.allow_guest
|
||||
RETURNING bot_id, max_context_load_time, language, allow_guest;
|
||||
|
||||
-- name: UpsertBotModelConfig :one
|
||||
INSERT INTO bot_model_configs (bot_id, chat_model_id, memory_model_id, embedding_model_id)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (bot_id) DO UPDATE SET
|
||||
chat_model_id = COALESCE(EXCLUDED.chat_model_id, bot_model_configs.chat_model_id),
|
||||
memory_model_id = COALESCE(EXCLUDED.memory_model_id, bot_model_configs.memory_model_id),
|
||||
embedding_model_id = COALESCE(EXCLUDED.embedding_model_id, bot_model_configs.embedding_model_id)
|
||||
RETURNING bot_id, chat_model_id, memory_model_id, embedding_model_id;
|
||||
WITH updated AS (
|
||||
UPDATE bots
|
||||
SET max_context_load_time = sqlc.arg(max_context_load_time),
|
||||
language = sqlc.arg(language),
|
||||
allow_guest = sqlc.arg(allow_guest),
|
||||
chat_model_id = COALESCE(sqlc.narg(chat_model_id)::uuid, bots.chat_model_id),
|
||||
memory_model_id = COALESCE(sqlc.narg(memory_model_id)::uuid, bots.memory_model_id),
|
||||
embedding_model_id = COALESCE(sqlc.narg(embedding_model_id)::uuid, bots.embedding_model_id),
|
||||
updated_at = now()
|
||||
WHERE bots.id = sqlc.arg(id)
|
||||
RETURNING bots.id, bots.max_context_load_time, bots.language, bots.allow_guest, bots.chat_model_id, bots.memory_model_id, bots.embedding_model_id
|
||||
)
|
||||
SELECT
|
||||
updated.id AS bot_id,
|
||||
updated.max_context_load_time,
|
||||
updated.language,
|
||||
updated.allow_guest,
|
||||
chat_models.model_id AS chat_model_id,
|
||||
memory_models.model_id AS memory_model_id,
|
||||
embedding_models.model_id AS embedding_model_id
|
||||
FROM updated
|
||||
LEFT JOIN models AS chat_models ON chat_models.id = updated.chat_model_id
|
||||
LEFT JOIN models AS memory_models ON memory_models.id = updated.memory_model_id
|
||||
LEFT JOIN models AS embedding_models ON embedding_models.id = updated.embedding_model_id;
|
||||
|
||||
-- name: DeleteSettingsByBotID :exec
|
||||
DELETE FROM bot_settings
|
||||
WHERE bot_id = $1;
|
||||
|
||||
UPDATE bots
|
||||
SET max_context_load_time = 1440,
|
||||
language = 'auto',
|
||||
allow_guest = false,
|
||||
updated_at = now()
|
||||
WHERE id = $1;
|
||||
|
||||
+49
-43
@@ -1,20 +1,38 @@
|
||||
-- name: CreateUser :one
|
||||
INSERT INTO users (username, email, password_hash, role, display_name, avatar_url, is_active, data_root)
|
||||
VALUES (
|
||||
sqlc.arg(username),
|
||||
sqlc.arg(email),
|
||||
sqlc.arg(password_hash),
|
||||
sqlc.arg(role)::user_role,
|
||||
sqlc.arg(display_name),
|
||||
sqlc.arg(avatar_url),
|
||||
sqlc.arg(is_active),
|
||||
sqlc.arg(data_root)
|
||||
)
|
||||
INSERT INTO users (is_active, metadata)
|
||||
VALUES ($1, $2)
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpsertUserByUsername :one
|
||||
INSERT INTO users (username, email, password_hash, role, display_name, avatar_url, is_active, data_root)
|
||||
-- name: GetUserByID :one
|
||||
SELECT *
|
||||
FROM users
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: UpdateUserStatus :one
|
||||
UPDATE users
|
||||
SET is_active = $2,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: CreateAccount :one
|
||||
UPDATE users
|
||||
SET username = sqlc.arg(username),
|
||||
email = sqlc.arg(email),
|
||||
password_hash = sqlc.arg(password_hash),
|
||||
role = sqlc.arg(role)::user_role,
|
||||
display_name = sqlc.arg(display_name),
|
||||
avatar_url = sqlc.arg(avatar_url),
|
||||
is_active = sqlc.arg(is_active),
|
||||
data_root = sqlc.arg(data_root),
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(user_id)
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpsertAccountByUsername :one
|
||||
INSERT INTO users (id, username, email, password_hash, role, display_name, avatar_url, is_active, data_root, metadata)
|
||||
VALUES (
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(username),
|
||||
sqlc.arg(email),
|
||||
sqlc.arg(password_hash),
|
||||
@@ -22,7 +40,8 @@ VALUES (
|
||||
sqlc.arg(display_name),
|
||||
sqlc.arg(avatar_url),
|
||||
sqlc.arg(is_active),
|
||||
sqlc.arg(data_root)
|
||||
sqlc.arg(data_root),
|
||||
'{}'::jsonb
|
||||
)
|
||||
ON CONFLICT (username) DO UPDATE SET
|
||||
email = EXCLUDED.email,
|
||||
@@ -35,39 +54,27 @@ ON CONFLICT (username) DO UPDATE SET
|
||||
updated_at = now()
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetUserByUsername :one
|
||||
-- name: GetAccountByUsername :one
|
||||
SELECT * FROM users WHERE username = sqlc.arg(username);
|
||||
|
||||
-- name: GetUserByIdentity :one
|
||||
-- name: GetAccountByIdentity :one
|
||||
SELECT * FROM users WHERE username = sqlc.arg(identity) OR email = sqlc.arg(identity);
|
||||
|
||||
-- name: GetUserByID :one
|
||||
SELECT * FROM users WHERE id = sqlc.arg(id);
|
||||
-- name: GetAccountByUserID :one
|
||||
SELECT * FROM users WHERE id = sqlc.arg(user_id);
|
||||
|
||||
-- name: CreateUserWithID :one
|
||||
INSERT INTO users (id, username, email, password_hash, role, display_name, avatar_url, is_active, data_root)
|
||||
VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(username),
|
||||
sqlc.arg(email),
|
||||
sqlc.arg(password_hash),
|
||||
sqlc.arg(role)::user_role,
|
||||
sqlc.arg(display_name),
|
||||
sqlc.arg(avatar_url),
|
||||
sqlc.arg(is_active),
|
||||
sqlc.arg(data_root)
|
||||
)
|
||||
RETURNING *;
|
||||
-- name: CountAccounts :one
|
||||
SELECT COUNT(*)::bigint AS count
|
||||
FROM users
|
||||
WHERE username IS NOT NULL
|
||||
AND password_hash IS NOT NULL;
|
||||
|
||||
-- name: CountUsers :one
|
||||
SELECT COUNT(*)::bigint AS count FROM users;
|
||||
|
||||
-- name: ListUsers :many
|
||||
-- name: ListAccounts :many
|
||||
SELECT * FROM users
|
||||
WHERE username IS NOT NULL
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
|
||||
-- name: UpdateUserProfile :one
|
||||
-- name: UpdateAccountProfile :one
|
||||
UPDATE users
|
||||
SET display_name = $2,
|
||||
avatar_url = $3,
|
||||
@@ -76,27 +83,26 @@ SET display_name = $2,
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateUserAdmin :one
|
||||
-- name: UpdateAccountAdmin :one
|
||||
UPDATE users
|
||||
SET role = sqlc.arg(role)::user_role,
|
||||
display_name = sqlc.arg(display_name),
|
||||
avatar_url = sqlc.arg(avatar_url),
|
||||
is_active = sqlc.arg(is_active),
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)
|
||||
WHERE id = sqlc.arg(user_id)
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateUserPassword :one
|
||||
-- name: UpdateAccountPassword :one
|
||||
UPDATE users
|
||||
SET password_hash = $2,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
-- name: UpdateUserLastLogin :one
|
||||
-- name: UpdateAccountLastLogin :one
|
||||
UPDATE users
|
||||
SET last_login_at = now(),
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
RETURNING *;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user