mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
refactor(core): restructure conversation, channel and message domains
- Rename chat module to conversation with flow-based architecture - Move channelidentities into channel/identities subpackage - Add channel/route for routing logic - Add message service with event hub - Add MCP providers: container, directory, schedule - Refactor Feishu/Telegram adapters with directory and stream support - Add platform management page and channel badges in web UI - Update database schema for conversations, messages and channel routes - Add @memoh/shared package for cross-package type definitions
This commit is contained in:
@@ -4,11 +4,8 @@ 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 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 bot_history_messages;
|
||||
DROP TABLE IF EXISTS bot_channel_routes;
|
||||
DROP TABLE IF EXISTS channel_identity_bind_codes;
|
||||
DROP TABLE IF EXISTS bot_preauth_keys;
|
||||
DROP TABLE IF EXISTS bot_channel_configs;
|
||||
|
||||
@@ -36,13 +36,13 @@ CREATE TABLE IF NOT EXISTS users (
|
||||
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_type 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)
|
||||
CONSTRAINT channel_identities_channel_type_subject_unique UNIQUE (channel_type, channel_subject_id)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_channel_identities_user_id ON channel_identities(user_id);
|
||||
@@ -51,11 +51,11 @@ CREATE INDEX IF NOT EXISTS idx_channel_identities_user_id ON channel_identities(
|
||||
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,
|
||||
channel_type 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)
|
||||
CONSTRAINT user_channel_bindings_unique UNIQUE (user_id, channel_type)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_user_channel_bindings_user_id ON user_channel_bindings(user_id);
|
||||
@@ -108,6 +108,7 @@ CREATE TABLE IF NOT EXISTS bots (
|
||||
display_name TEXT,
|
||||
avatar_url TEXT,
|
||||
is_active BOOLEAN NOT NULL DEFAULT true,
|
||||
status TEXT NOT NULL DEFAULT 'ready',
|
||||
max_context_load_time INTEGER NOT NULL DEFAULT 1440,
|
||||
language TEXT NOT NULL DEFAULT 'auto',
|
||||
allow_guest BOOLEAN NOT NULL DEFAULT false,
|
||||
@@ -117,7 +118,8 @@ CREATE TABLE IF NOT EXISTS bots (
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
CONSTRAINT bots_type_check CHECK (type IN ('personal', 'public'))
|
||||
CONSTRAINT bots_type_check CHECK (type IN ('personal', 'public')),
|
||||
CONSTRAINT bots_status_check CHECK (status IN ('creating', 'ready', 'deleting'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bots_owner_user_id ON bots(owner_user_id);
|
||||
@@ -148,85 +150,7 @@ CREATE TABLE IF NOT EXISTS mcp_connections (
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_mcp_connections_bot_id ON mcp_connections(bot_id);
|
||||
|
||||
-- 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,
|
||||
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,
|
||||
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()
|
||||
);
|
||||
|
||||
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);
|
||||
-- Bot history is bot-scoped (one history container per bot).
|
||||
|
||||
CREATE TABLE IF NOT EXISTS bot_channel_configs (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
@@ -269,7 +193,7 @@ CREATE TABLE IF NOT EXISTS channel_identity_bind_codes (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
token TEXT NOT NULL,
|
||||
issued_by_user_id UUID NOT NULL REFERENCES users(id),
|
||||
platform TEXT,
|
||||
channel_type TEXT,
|
||||
expires_at TIMESTAMPTZ,
|
||||
used_at TIMESTAMPTZ,
|
||||
used_by_channel_identity_id UUID REFERENCES channel_identities(id),
|
||||
@@ -277,27 +201,48 @@ CREATE TABLE IF NOT EXISTS channel_identity_bind_codes (
|
||||
CONSTRAINT channel_identity_bind_codes_token_unique UNIQUE (token)
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_channel_identity_bind_codes_platform ON channel_identity_bind_codes(platform);
|
||||
CREATE INDEX IF NOT EXISTS idx_channel_identity_bind_codes_channel_type ON channel_identity_bind_codes(channel_type);
|
||||
|
||||
-- chat_routes: routing mapping (replaces channel_sessions)
|
||||
CREATE TABLE IF NOT EXISTS chat_routes (
|
||||
-- bot_channel_routes: route mapping for inbound channel threads to bot history.
|
||||
CREATE TABLE IF NOT EXISTS bot_channel_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,
|
||||
platform TEXT NOT NULL,
|
||||
channel_type TEXT NOT NULL,
|
||||
channel_config_id UUID REFERENCES bot_channel_configs(id) ON DELETE SET NULL,
|
||||
conversation_id TEXT NOT NULL,
|
||||
thread_id TEXT,
|
||||
reply_target TEXT,
|
||||
external_conversation_id TEXT NOT NULL,
|
||||
external_thread_id TEXT,
|
||||
default_reply_target TEXT,
|
||||
metadata JSONB NOT NULL DEFAULT '{}'::jsonb,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
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 UNIQUE INDEX IF NOT EXISTS idx_bot_channel_routes_unique
|
||||
ON bot_channel_routes (bot_id, channel_type, external_conversation_id, COALESCE(external_thread_id, ''));
|
||||
CREATE INDEX IF NOT EXISTS idx_bot_channel_routes_bot ON bot_channel_routes(bot_id);
|
||||
|
||||
-- bot_history_messages: unified message history under bot scope.
|
||||
CREATE TABLE IF NOT EXISTS bot_history_messages (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
bot_id UUID NOT NULL REFERENCES bots(id) ON DELETE CASCADE,
|
||||
route_id UUID REFERENCES bot_channel_routes(id) ON DELETE SET NULL,
|
||||
sender_channel_identity_id UUID REFERENCES channel_identities(id),
|
||||
sender_account_user_id UUID REFERENCES users(id),
|
||||
channel_type TEXT,
|
||||
source_message_id TEXT,
|
||||
source_reply_to_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()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_bot_history_messages_bot_created ON bot_history_messages(bot_id, created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_bot_history_messages_route ON bot_history_messages(route_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_bot_history_messages_source_lookup
|
||||
ON bot_history_messages(channel_type, source_message_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_bot_history_messages_reply_lookup
|
||||
ON bot_history_messages(channel_type, source_reply_to_message_id);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS containers (
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
|
||||
+5
-5
@@ -1,15 +1,15 @@
|
||||
-- name: CreateBindCode :one
|
||||
INSERT INTO channel_identity_bind_codes (token, issued_by_user_id, platform, expires_at)
|
||||
INSERT INTO channel_identity_bind_codes (token, issued_by_user_id, channel_type, 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;
|
||||
RETURNING id, token, issued_by_user_id, channel_type, 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
|
||||
SELECT id, token, issued_by_user_id, channel_type, 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
|
||||
SELECT id, token, issued_by_user_id, channel_type, expires_at, used_at, used_by_channel_identity_id, created_at
|
||||
FROM channel_identity_bind_codes
|
||||
WHERE token = $1
|
||||
FOR UPDATE;
|
||||
@@ -19,4 +19,4 @@ 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;
|
||||
RETURNING id, token, issued_by_user_id, channel_type, expires_at, used_at, used_by_channel_identity_id, created_at;
|
||||
|
||||
+14
-8
@@ -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, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, metadata, created_at, updated_at;
|
||||
INSERT INTO bots (owner_user_id, type, display_name, avatar_url, is_active, metadata, status)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, 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, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, metadata, created_at, updated_at
|
||||
SELECT id, owner_user_id, type, display_name, avatar_url, is_active, status, 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, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, metadata, created_at, updated_at
|
||||
SELECT id, owner_user_id, type, display_name, avatar_url, is_active, status, 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.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
|
||||
SELECT b.id, b.owner_user_id, b.type, b.display_name, b.avatar_url, b.is_active, b.status, 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,20 @@ SET display_name = $2,
|
||||
metadata = $5,
|
||||
updated_at = now()
|
||||
WHERE id = $1
|
||||
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;
|
||||
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, 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, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, metadata, created_at, updated_at;
|
||||
RETURNING id, owner_user_id, type, display_name, avatar_url, is_active, status, max_context_load_time, language, allow_guest, chat_model_id, memory_model_id, embedding_model_id, metadata, created_at, updated_at;
|
||||
|
||||
-- name: UpdateBotStatus :exec
|
||||
UPDATE bots
|
||||
SET status = $2,
|
||||
updated_at = now()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: DeleteBotByID :exec
|
||||
DELETE FROM bots WHERE id = $1;
|
||||
|
||||
@@ -1,37 +1,37 @@
|
||||
-- name: CreateChannelIdentity :one
|
||||
INSERT INTO channel_identities (user_id, channel, channel_subject_id, display_name, metadata)
|
||||
INSERT INTO channel_identities (user_id, channel_type, 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;
|
||||
RETURNING id, user_id, channel_type, 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
|
||||
SELECT id, user_id, channel_type, 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
|
||||
SELECT id, user_id, channel_type, 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
|
||||
SELECT id, user_id, channel_type, channel_subject_id, display_name, metadata, created_at, updated_at
|
||||
FROM channel_identities
|
||||
WHERE channel = $1 AND channel_subject_id = $2;
|
||||
WHERE channel_type = $1 AND channel_subject_id = $2;
|
||||
|
||||
-- name: UpsertChannelIdentityByChannelSubject :one
|
||||
INSERT INTO channel_identities (user_id, channel, channel_subject_id, display_name, metadata)
|
||||
INSERT INTO channel_identities (user_id, channel_type, channel_subject_id, display_name, metadata)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
ON CONFLICT (channel, channel_subject_id)
|
||||
ON CONFLICT (channel_type, channel_subject_id)
|
||||
DO UPDATE SET
|
||||
display_name = EXCLUDED.display_name,
|
||||
display_name = COALESCE(NULLIF(EXCLUDED.display_name, ''), channel_identities.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;
|
||||
RETURNING id, user_id, channel_type, 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
|
||||
SELECT id, user_id, channel_type, channel_subject_id, display_name, metadata, created_at, updated_at
|
||||
FROM channel_identities
|
||||
WHERE user_id = $1
|
||||
ORDER BY created_at DESC;
|
||||
@@ -40,10 +40,10 @@ ORDER BY created_at DESC;
|
||||
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;
|
||||
RETURNING id, user_id, channel_type, 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;
|
||||
RETURNING id, user_id, channel_type, channel_subject_id, display_name, metadata, created_at, updated_at;
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
-- name: CreateChatRoute :one
|
||||
INSERT INTO bot_channel_routes (
|
||||
bot_id, channel_type, channel_config_id, external_conversation_id, external_thread_id, default_reply_target, metadata
|
||||
)
|
||||
VALUES (
|
||||
sqlc.arg(bot_id),
|
||||
sqlc.arg(platform),
|
||||
sqlc.narg(channel_config_id)::uuid,
|
||||
sqlc.arg(conversation_id),
|
||||
sqlc.narg(thread_id)::text,
|
||||
sqlc.narg(reply_target)::text,
|
||||
sqlc.arg(metadata)
|
||||
)
|
||||
RETURNING
|
||||
id,
|
||||
sqlc.arg(chat_id)::uuid AS chat_id,
|
||||
bot_id,
|
||||
channel_type AS platform,
|
||||
channel_config_id,
|
||||
external_conversation_id AS conversation_id,
|
||||
external_thread_id AS thread_id,
|
||||
default_reply_target AS reply_target,
|
||||
metadata,
|
||||
created_at,
|
||||
updated_at;
|
||||
|
||||
-- name: FindChatRoute :one
|
||||
SELECT
|
||||
id,
|
||||
bot_id AS chat_id,
|
||||
bot_id,
|
||||
channel_type AS platform,
|
||||
channel_config_id,
|
||||
external_conversation_id AS conversation_id,
|
||||
external_thread_id AS thread_id,
|
||||
default_reply_target AS reply_target,
|
||||
metadata,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM bot_channel_routes
|
||||
WHERE bot_id = $1
|
||||
AND channel_type = sqlc.arg(platform)
|
||||
AND external_conversation_id = sqlc.arg(conversation_id)
|
||||
AND COALESCE(external_thread_id, '') = COALESCE(sqlc.narg(thread_id), '')
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetChatRouteByID :one
|
||||
SELECT
|
||||
id,
|
||||
bot_id AS chat_id,
|
||||
bot_id,
|
||||
channel_type AS platform,
|
||||
channel_config_id,
|
||||
external_conversation_id AS conversation_id,
|
||||
external_thread_id AS thread_id,
|
||||
default_reply_target AS reply_target,
|
||||
metadata,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM bot_channel_routes
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: ListChatRoutes :many
|
||||
SELECT
|
||||
id,
|
||||
bot_id AS chat_id,
|
||||
bot_id,
|
||||
channel_type AS platform,
|
||||
channel_config_id,
|
||||
external_conversation_id AS conversation_id,
|
||||
external_thread_id AS thread_id,
|
||||
default_reply_target AS reply_target,
|
||||
metadata,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM bot_channel_routes
|
||||
WHERE bot_id = sqlc.arg(chat_id)
|
||||
ORDER BY created_at ASC;
|
||||
|
||||
-- name: UpdateChatRouteReplyTarget :exec
|
||||
UPDATE bot_channel_routes
|
||||
SET default_reply_target = sqlc.arg(reply_target), updated_at = now()
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
-- name: DeleteChatRoute :exec
|
||||
DELETE FROM bot_channel_routes
|
||||
WHERE id = $1;
|
||||
@@ -34,23 +34,23 @@ WHERE channel_type = $1
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
-- name: GetUserChannelBinding :one
|
||||
SELECT id, user_id, platform, config, created_at, updated_at
|
||||
SELECT id, user_id, channel_type, config, created_at, updated_at
|
||||
FROM user_channel_bindings
|
||||
WHERE user_id = $1 AND platform = $2
|
||||
WHERE user_id = $1 AND channel_type = $2
|
||||
LIMIT 1;
|
||||
|
||||
-- name: UpsertUserChannelBinding :one
|
||||
INSERT INTO user_channel_bindings (user_id, platform, config)
|
||||
INSERT INTO user_channel_bindings (user_id, channel_type, config)
|
||||
VALUES ($1, $2, $3)
|
||||
ON CONFLICT (user_id, platform)
|
||||
ON CONFLICT (user_id, channel_type)
|
||||
DO UPDATE SET
|
||||
config = EXCLUDED.config,
|
||||
updated_at = now()
|
||||
RETURNING id, user_id, platform, config, created_at, updated_at;
|
||||
RETURNING id, user_id, channel_type, config, created_at, updated_at;
|
||||
|
||||
-- name: ListUserChannelBindingsByPlatform :many
|
||||
SELECT id, user_id, platform, config, created_at, updated_at
|
||||
SELECT id, user_id, channel_type, config, created_at, updated_at
|
||||
FROM user_channel_bindings
|
||||
WHERE platform = $1
|
||||
WHERE channel_type = $1
|
||||
ORDER BY created_at DESC;
|
||||
|
||||
|
||||
@@ -1,214 +0,0 @@
|
||||
-- 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;
|
||||
@@ -52,3 +52,6 @@ WHERE bot_id = sqlc.arg(bot_id);
|
||||
UPDATE containers
|
||||
SET status = 'stopped', last_stopped_at = now(), updated_at = now()
|
||||
WHERE bot_id = sqlc.arg(bot_id);
|
||||
|
||||
-- name: ListAutoStartContainers :many
|
||||
SELECT * FROM containers WHERE auto_start = true ORDER BY updated_at DESC;
|
||||
|
||||
@@ -0,0 +1,229 @@
|
||||
-- name: CreateChat :one
|
||||
SELECT
|
||||
b.id AS id,
|
||||
b.id AS bot_id,
|
||||
(COALESCE(NULLIF(sqlc.arg(kind)::text, ''), CASE WHEN b.type = 'public' THEN 'group' ELSE 'direct' END))::text AS kind,
|
||||
CASE WHEN sqlc.arg(kind) = 'thread' THEN sqlc.arg(parent_chat_id)::uuid ELSE NULL::uuid END AS parent_chat_id,
|
||||
COALESCE(NULLIF(sqlc.arg(title)::text, ''), b.display_name) AS title,
|
||||
COALESCE(sqlc.arg(created_by_user_id)::uuid, b.owner_user_id) AS created_by_user_id,
|
||||
COALESCE(sqlc.arg(metadata)::jsonb, b.metadata) AS metadata,
|
||||
chat_models.model_id AS model_id,
|
||||
b.created_at,
|
||||
b.updated_at
|
||||
FROM bots b
|
||||
LEFT JOIN models chat_models ON chat_models.id = b.chat_model_id
|
||||
WHERE b.id = sqlc.arg(bot_id)
|
||||
LIMIT 1;
|
||||
|
||||
-- name: GetChatByID :one
|
||||
SELECT
|
||||
b.id AS id,
|
||||
b.id AS bot_id,
|
||||
CASE WHEN b.type = 'public' THEN 'group' ELSE 'direct' END AS kind,
|
||||
NULL::uuid AS parent_chat_id,
|
||||
b.display_name AS title,
|
||||
b.owner_user_id AS created_by_user_id,
|
||||
b.metadata AS metadata,
|
||||
chat_models.model_id AS model_id,
|
||||
b.created_at,
|
||||
b.updated_at
|
||||
FROM bots b
|
||||
LEFT JOIN models chat_models ON chat_models.id = b.chat_model_id
|
||||
WHERE b.id = $1;
|
||||
|
||||
-- name: ListChatsByBotAndUser :many
|
||||
SELECT
|
||||
b.id AS id,
|
||||
b.id AS bot_id,
|
||||
CASE WHEN b.type = 'public' THEN 'group' ELSE 'direct' END AS kind,
|
||||
NULL::uuid AS parent_chat_id,
|
||||
b.display_name AS title,
|
||||
b.owner_user_id AS created_by_user_id,
|
||||
b.metadata AS metadata,
|
||||
chat_models.model_id AS model_id,
|
||||
b.created_at,
|
||||
b.updated_at
|
||||
FROM bots b
|
||||
LEFT JOIN bot_members bm ON bm.bot_id = b.id AND bm.user_id = sqlc.arg(user_id)
|
||||
LEFT JOIN models chat_models ON chat_models.id = b.chat_model_id
|
||||
WHERE b.id = sqlc.arg(bot_id)
|
||||
AND (b.owner_user_id = sqlc.arg(user_id) OR bm.user_id IS NOT NULL)
|
||||
ORDER BY b.updated_at DESC;
|
||||
|
||||
-- name: ListVisibleChatsByBotAndUser :many
|
||||
SELECT
|
||||
b.id AS id,
|
||||
b.id AS bot_id,
|
||||
CASE WHEN b.type = 'public' THEN 'group' ELSE 'direct' END AS kind,
|
||||
NULL::uuid AS parent_chat_id,
|
||||
b.display_name AS title,
|
||||
b.owner_user_id AS created_by_user_id,
|
||||
b.metadata AS metadata,
|
||||
b.created_at,
|
||||
b.updated_at,
|
||||
'participant'::text AS access_mode,
|
||||
(CASE
|
||||
WHEN b.owner_user_id = sqlc.arg(user_id) THEN 'owner'
|
||||
ELSE COALESCE(bm.role, ''::text)
|
||||
END)::text AS participant_role,
|
||||
NULL::timestamptz AS last_observed_at
|
||||
FROM bots b
|
||||
LEFT JOIN bot_members bm ON bm.bot_id = b.id AND bm.user_id = sqlc.arg(user_id)
|
||||
WHERE b.id = sqlc.arg(bot_id)
|
||||
AND (b.owner_user_id = sqlc.arg(user_id) OR bm.user_id IS NOT NULL)
|
||||
ORDER BY b.updated_at DESC;
|
||||
|
||||
-- name: GetChatReadAccessByUser :one
|
||||
SELECT
|
||||
'participant'::text AS access_mode,
|
||||
(CASE
|
||||
WHEN b.owner_user_id = sqlc.arg(user_id) THEN 'owner'
|
||||
ELSE COALESCE(bm.role, ''::text)
|
||||
END)::text AS participant_role,
|
||||
NULL::timestamptz AS last_observed_at
|
||||
FROM bots b
|
||||
LEFT JOIN bot_members bm ON bm.bot_id = b.id AND bm.user_id = sqlc.arg(user_id)
|
||||
WHERE b.id = sqlc.arg(chat_id)
|
||||
AND (b.owner_user_id = sqlc.arg(user_id) OR bm.user_id IS NOT NULL)
|
||||
LIMIT 1;
|
||||
|
||||
-- name: ListThreadsByParent :many
|
||||
SELECT
|
||||
b.id AS id,
|
||||
b.id AS bot_id,
|
||||
CASE WHEN b.type = 'public' THEN 'group' ELSE 'direct' END AS kind,
|
||||
NULL::uuid AS parent_chat_id,
|
||||
b.display_name AS title,
|
||||
b.owner_user_id AS created_by_user_id,
|
||||
b.metadata AS metadata,
|
||||
chat_models.model_id AS model_id,
|
||||
b.created_at,
|
||||
b.updated_at
|
||||
FROM bots b
|
||||
LEFT JOIN models chat_models ON chat_models.id = b.chat_model_id
|
||||
WHERE b.id = $1
|
||||
AND false
|
||||
ORDER BY b.created_at DESC;
|
||||
|
||||
-- name: UpdateChatTitle :one
|
||||
UPDATE bots
|
||||
SET display_name = sqlc.arg(title),
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)
|
||||
RETURNING
|
||||
id,
|
||||
id AS bot_id,
|
||||
CASE WHEN type = 'public' THEN 'group' ELSE 'direct' END AS kind,
|
||||
NULL::uuid AS parent_chat_id,
|
||||
display_name AS title,
|
||||
owner_user_id AS created_by_user_id,
|
||||
metadata,
|
||||
NULL::text AS model_id,
|
||||
created_at,
|
||||
updated_at;
|
||||
|
||||
-- name: TouchChat :exec
|
||||
UPDATE bots
|
||||
SET updated_at = now()
|
||||
WHERE id = sqlc.arg(chat_id);
|
||||
|
||||
-- name: DeleteChat :exec
|
||||
WITH deleted_messages AS (
|
||||
DELETE FROM bot_history_messages
|
||||
WHERE bot_id = sqlc.arg(chat_id)
|
||||
)
|
||||
DELETE FROM bot_channel_routes bcr
|
||||
WHERE bcr.bot_id = sqlc.arg(chat_id);
|
||||
|
||||
-- chat_participants
|
||||
|
||||
-- name: AddChatParticipant :one
|
||||
INSERT INTO bot_members (bot_id, user_id, role)
|
||||
VALUES (sqlc.arg(chat_id), sqlc.arg(user_id), sqlc.arg(role))
|
||||
ON CONFLICT (bot_id, user_id) DO UPDATE SET role = EXCLUDED.role
|
||||
RETURNING bot_id AS chat_id, user_id, role, created_at AS joined_at;
|
||||
|
||||
-- name: GetChatParticipant :one
|
||||
WITH owner_participant AS (
|
||||
SELECT b.id AS chat_id, b.owner_user_id AS user_id, 'owner'::text AS role, b.created_at AS joined_at
|
||||
FROM bots b
|
||||
WHERE b.id = sqlc.arg(chat_id) AND b.owner_user_id = sqlc.arg(user_id)
|
||||
),
|
||||
member_participant AS (
|
||||
SELECT bm.bot_id AS chat_id, bm.user_id, bm.role, bm.created_at AS joined_at
|
||||
FROM bot_members bm
|
||||
WHERE bm.bot_id = sqlc.arg(chat_id) AND bm.user_id = sqlc.arg(user_id)
|
||||
)
|
||||
SELECT chat_id, user_id, role, joined_at
|
||||
FROM (
|
||||
SELECT * FROM owner_participant
|
||||
UNION ALL
|
||||
SELECT * FROM member_participant
|
||||
) p
|
||||
ORDER BY CASE WHEN role = 'owner' THEN 0 ELSE 1 END
|
||||
LIMIT 1;
|
||||
|
||||
-- name: ListChatParticipants :many
|
||||
WITH owner_participant AS (
|
||||
SELECT b.id AS chat_id, b.owner_user_id AS user_id, 'owner'::text AS role, b.created_at AS joined_at
|
||||
FROM bots b
|
||||
WHERE b.id = sqlc.arg(chat_id)
|
||||
),
|
||||
member_participant AS (
|
||||
SELECT bm.bot_id AS chat_id, bm.user_id, bm.role, bm.created_at AS joined_at
|
||||
FROM bot_members bm
|
||||
WHERE bm.bot_id = sqlc.arg(chat_id)
|
||||
AND bm.user_id <> (SELECT owner_user_id FROM bots WHERE id = sqlc.arg(chat_id))
|
||||
)
|
||||
SELECT chat_id, user_id, role, joined_at
|
||||
FROM (
|
||||
SELECT * FROM owner_participant
|
||||
UNION ALL
|
||||
SELECT * FROM member_participant
|
||||
) p
|
||||
ORDER BY joined_at ASC;
|
||||
|
||||
-- name: RemoveChatParticipant :exec
|
||||
DELETE FROM bot_members
|
||||
WHERE bot_id = sqlc.arg(chat_id)
|
||||
AND user_id = sqlc.arg(user_id)
|
||||
AND user_id <> (SELECT owner_user_id FROM bots WHERE id = sqlc.arg(chat_id));
|
||||
|
||||
-- name: CopyParticipantsToChat :exec
|
||||
INSERT INTO bot_members (bot_id, user_id, role)
|
||||
SELECT sqlc.arg(chat_id_2), bm.user_id, bm.role
|
||||
FROM bot_members bm
|
||||
WHERE bm.bot_id = sqlc.arg(chat_id)
|
||||
ON CONFLICT (bot_id, user_id) DO NOTHING;
|
||||
|
||||
-- chat_settings
|
||||
|
||||
-- name: UpsertChatSettings :one
|
||||
WITH resolved_model AS (
|
||||
SELECT id
|
||||
FROM models
|
||||
WHERE model_id = NULLIF(sqlc.narg(model_id)::text, '')
|
||||
LIMIT 1
|
||||
),
|
||||
updated AS (
|
||||
UPDATE bots
|
||||
SET chat_model_id = COALESCE((SELECT id FROM resolved_model), bots.chat_model_id),
|
||||
updated_at = now()
|
||||
WHERE bots.id = sqlc.arg(id)
|
||||
RETURNING bots.id, bots.chat_model_id, bots.updated_at
|
||||
)
|
||||
SELECT
|
||||
updated.id AS chat_id,
|
||||
chat_models.model_id AS model_id,
|
||||
updated.updated_at
|
||||
FROM updated
|
||||
LEFT JOIN models chat_models ON chat_models.id = updated.chat_model_id;
|
||||
|
||||
-- name: GetChatSettings :one
|
||||
SELECT
|
||||
b.id AS chat_id,
|
||||
chat_models.model_id AS model_id,
|
||||
b.updated_at
|
||||
FROM bots b
|
||||
LEFT JOIN models chat_models ON chat_models.id = b.chat_model_id
|
||||
WHERE b.id = $1;
|
||||
@@ -0,0 +1,118 @@
|
||||
-- name: CreateMessage :one
|
||||
INSERT INTO bot_history_messages (
|
||||
bot_id,
|
||||
route_id,
|
||||
sender_channel_identity_id,
|
||||
sender_account_user_id,
|
||||
channel_type,
|
||||
source_message_id,
|
||||
source_reply_to_message_id,
|
||||
role,
|
||||
content,
|
||||
metadata
|
||||
)
|
||||
VALUES (
|
||||
sqlc.arg(bot_id),
|
||||
sqlc.narg(route_id)::uuid,
|
||||
sqlc.narg(sender_channel_identity_id)::uuid,
|
||||
sqlc.narg(sender_user_id)::uuid,
|
||||
sqlc.narg(platform)::text,
|
||||
sqlc.narg(external_message_id)::text,
|
||||
sqlc.narg(source_reply_to_message_id)::text,
|
||||
sqlc.arg(role),
|
||||
sqlc.arg(content),
|
||||
sqlc.arg(metadata)
|
||||
)
|
||||
RETURNING
|
||||
id,
|
||||
bot_id,
|
||||
route_id,
|
||||
sender_channel_identity_id,
|
||||
sender_account_user_id AS sender_user_id,
|
||||
channel_type AS platform,
|
||||
source_message_id AS external_message_id,
|
||||
source_reply_to_message_id,
|
||||
role,
|
||||
content,
|
||||
metadata,
|
||||
created_at;
|
||||
|
||||
-- name: ListMessages :many
|
||||
SELECT
|
||||
id,
|
||||
bot_id,
|
||||
route_id,
|
||||
sender_channel_identity_id,
|
||||
sender_account_user_id AS sender_user_id,
|
||||
channel_type AS platform,
|
||||
source_message_id AS external_message_id,
|
||||
source_reply_to_message_id,
|
||||
role,
|
||||
content,
|
||||
metadata,
|
||||
created_at
|
||||
FROM bot_history_messages
|
||||
WHERE bot_id = sqlc.arg(bot_id)
|
||||
ORDER BY created_at ASC;
|
||||
|
||||
-- name: ListMessagesSince :many
|
||||
SELECT
|
||||
id,
|
||||
bot_id,
|
||||
route_id,
|
||||
sender_channel_identity_id,
|
||||
sender_account_user_id AS sender_user_id,
|
||||
channel_type AS platform,
|
||||
source_message_id AS external_message_id,
|
||||
source_reply_to_message_id,
|
||||
role,
|
||||
content,
|
||||
metadata,
|
||||
created_at
|
||||
FROM bot_history_messages
|
||||
WHERE bot_id = sqlc.arg(bot_id)
|
||||
AND created_at >= sqlc.arg(created_at)
|
||||
ORDER BY created_at ASC;
|
||||
|
||||
-- name: ListMessagesBefore :many
|
||||
SELECT
|
||||
id,
|
||||
bot_id,
|
||||
route_id,
|
||||
sender_channel_identity_id,
|
||||
sender_account_user_id AS sender_user_id,
|
||||
channel_type AS platform,
|
||||
source_message_id AS external_message_id,
|
||||
source_reply_to_message_id,
|
||||
role,
|
||||
content,
|
||||
metadata,
|
||||
created_at
|
||||
FROM bot_history_messages
|
||||
WHERE bot_id = sqlc.arg(bot_id)
|
||||
AND created_at < sqlc.arg(created_at)
|
||||
ORDER BY created_at DESC
|
||||
LIMIT sqlc.arg(max_count);
|
||||
|
||||
-- name: ListMessagesLatest :many
|
||||
SELECT
|
||||
id,
|
||||
bot_id,
|
||||
route_id,
|
||||
sender_channel_identity_id,
|
||||
sender_account_user_id AS sender_user_id,
|
||||
channel_type AS platform,
|
||||
source_message_id AS external_message_id,
|
||||
source_reply_to_message_id,
|
||||
role,
|
||||
content,
|
||||
metadata,
|
||||
created_at
|
||||
FROM bot_history_messages
|
||||
WHERE bot_id = sqlc.arg(bot_id)
|
||||
ORDER BY created_at DESC
|
||||
LIMIT sqlc.arg(max_count);
|
||||
|
||||
-- name: DeleteMessagesByBot :exec
|
||||
DELETE FROM bot_history_messages
|
||||
WHERE bot_id = sqlc.arg(bot_id);
|
||||
Reference in New Issue
Block a user