mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
6acdd191c7
commit bcdb026ae43e4f95d0b2c4f9bd440a2df9d6b514 Author: Ran <16112591+chen-ran@users.noreply.github.com> Date: Thu Feb 12 17:10:32 2026 +0800 chore: update DEVELOPMENT.md commit30281742efMerge:ca5c6a15b05f13Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Thu Feb 12 15:49:17 2026 +0800 merge(github/main): integrate fx dependency injection framework Merge upstream fx refactor and adapt all services to use go.uber.org/fx for dependency injection. Resolve conflicts in main.go, server.go, and service constructors while preserving our domain model changes. - Fix telegram adapter panic on shutdown (double close channel) - Fix feishu adapter processing messages after stop - Increase directory lookup timeout from 2s to 5s commitca5c6a1866Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Thu Feb 12 15:33:09 2026 +0800 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 commit75e2ef0467Merge:d99ba3801cb6c8Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Thu Feb 12 14:45:49 2026 +0800 merge(github): merge github/main, resolve index.ts URL conflict Keep our defensive absolute-URL check in createAuthFetcher. commitd99ba38b7dMerge:860e20f35ce7d1Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Thu Feb 12 05:20:18 2026 +0800 merge(github): merge github/main, keep our code and docs/spec commit860e20fe70Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Wed Feb 11 22:13:27 2026 +0800 docs(docs): add concepts and style guides for VitePress site - Add concepts: identity-and-binding, index (en/zh) - Add style: terminology (en/zh) - Update index and zh/index - Update .vitepress/config.ts commita75fdb8040Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Wed Feb 11 17:37:16 2026 +0800 refactor(mcp): standardize unified tool gateway on go-sdk Split business executors from federation sources and migrate unified tool/federation transports to the official go-sdk for stricter MCP compliance and safer session lifecycle handling. Add targeted regression tests for accept compatibility, initialization retries, pending cleanup, and include updated swagger artifacts. commit02b33c8e85Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Wed Feb 11 15:42:21 2026 +0800 refactor(core): finalize user-centric identity and policy cleanup Unify auth and chat identity semantics around user_id, enforce personal-bot owner-only authorization, and remove legacy compatibility branches in integration tests. commit06e8619a37Author: BBQ <bbq@BBQdeMacBook-Air.local> Date: Wed Feb 11 14:47:03 2026 +0800 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.
679 lines
19 KiB
Go
679 lines
19 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: conversations.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const addChatParticipant = `-- name: AddChatParticipant :one
|
|
|
|
INSERT INTO bot_members (bot_id, user_id, role)
|
|
VALUES ($1, $2, $3)
|
|
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
|
|
`
|
|
|
|
type AddChatParticipantParams struct {
|
|
ChatID pgtype.UUID `json:"chat_id"`
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
Role string `json:"role"`
|
|
}
|
|
|
|
type AddChatParticipantRow struct {
|
|
ChatID pgtype.UUID `json:"chat_id"`
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
Role string `json:"role"`
|
|
JoinedAt pgtype.Timestamptz `json:"joined_at"`
|
|
}
|
|
|
|
// chat_participants
|
|
func (q *Queries) AddChatParticipant(ctx context.Context, arg AddChatParticipantParams) (AddChatParticipantRow, error) {
|
|
row := q.db.QueryRow(ctx, addChatParticipant, arg.ChatID, arg.UserID, arg.Role)
|
|
var i AddChatParticipantRow
|
|
err := row.Scan(
|
|
&i.ChatID,
|
|
&i.UserID,
|
|
&i.Role,
|
|
&i.JoinedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const copyParticipantsToChat = `-- name: CopyParticipantsToChat :exec
|
|
INSERT INTO bot_members (bot_id, user_id, role)
|
|
SELECT $1, bm.user_id, bm.role
|
|
FROM bot_members bm
|
|
WHERE bm.bot_id = $2
|
|
ON CONFLICT (bot_id, user_id) DO NOTHING
|
|
`
|
|
|
|
type CopyParticipantsToChatParams struct {
|
|
ChatID2 pgtype.UUID `json:"chat_id_2"`
|
|
ChatID pgtype.UUID `json:"chat_id"`
|
|
}
|
|
|
|
func (q *Queries) CopyParticipantsToChat(ctx context.Context, arg CopyParticipantsToChatParams) error {
|
|
_, err := q.db.Exec(ctx, copyParticipantsToChat, arg.ChatID2, arg.ChatID)
|
|
return err
|
|
}
|
|
|
|
const createChat = `-- name: CreateChat :one
|
|
SELECT
|
|
b.id AS id,
|
|
b.id AS bot_id,
|
|
(COALESCE(NULLIF($1::text, ''), CASE WHEN b.type = 'public' THEN 'group' ELSE 'direct' END))::text AS kind,
|
|
CASE WHEN $1 = 'thread' THEN $2::uuid ELSE NULL::uuid END AS parent_chat_id,
|
|
COALESCE(NULLIF($3::text, ''), b.display_name) AS title,
|
|
COALESCE($4::uuid, b.owner_user_id) AS created_by_user_id,
|
|
COALESCE($5::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 = $6
|
|
LIMIT 1
|
|
`
|
|
|
|
type CreateChatParams struct {
|
|
Kind string `json:"kind"`
|
|
ParentChatID pgtype.UUID `json:"parent_chat_id"`
|
|
Title string `json:"title"`
|
|
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
|
|
Metadata []byte `json:"metadata"`
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
}
|
|
|
|
type CreateChatRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
Kind string `json:"kind"`
|
|
ParentChatID pgtype.UUID `json:"parent_chat_id"`
|
|
Title pgtype.Text `json:"title"`
|
|
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
|
|
Metadata []byte `json:"metadata"`
|
|
ModelID pgtype.Text `json:"model_id"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) CreateChat(ctx context.Context, arg CreateChatParams) (CreateChatRow, error) {
|
|
row := q.db.QueryRow(ctx, createChat,
|
|
arg.Kind,
|
|
arg.ParentChatID,
|
|
arg.Title,
|
|
arg.CreatedByUserID,
|
|
arg.Metadata,
|
|
arg.BotID,
|
|
)
|
|
var i CreateChatRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.BotID,
|
|
&i.Kind,
|
|
&i.ParentChatID,
|
|
&i.Title,
|
|
&i.CreatedByUserID,
|
|
&i.Metadata,
|
|
&i.ModelID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const deleteChat = `-- name: DeleteChat :exec
|
|
WITH deleted_messages AS (
|
|
DELETE FROM bot_history_messages
|
|
WHERE bot_id = $1
|
|
)
|
|
DELETE FROM bot_channel_routes bcr
|
|
WHERE bcr.bot_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteChat(ctx context.Context, chatID pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteChat, chatID)
|
|
return err
|
|
}
|
|
|
|
const getChatByID = `-- 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
|
|
`
|
|
|
|
type GetChatByIDRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
Kind string `json:"kind"`
|
|
ParentChatID pgtype.UUID `json:"parent_chat_id"`
|
|
Title pgtype.Text `json:"title"`
|
|
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
|
|
Metadata []byte `json:"metadata"`
|
|
ModelID pgtype.Text `json:"model_id"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) GetChatByID(ctx context.Context, id pgtype.UUID) (GetChatByIDRow, error) {
|
|
row := q.db.QueryRow(ctx, getChatByID, id)
|
|
var i GetChatByIDRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.BotID,
|
|
&i.Kind,
|
|
&i.ParentChatID,
|
|
&i.Title,
|
|
&i.CreatedByUserID,
|
|
&i.Metadata,
|
|
&i.ModelID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getChatParticipant = `-- 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 = $1 AND b.owner_user_id = $2
|
|
),
|
|
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 = $1 AND bm.user_id = $2
|
|
)
|
|
SELECT chat_id, user_id, role, joined_at
|
|
FROM (
|
|
SELECT chat_id, user_id, role, joined_at FROM owner_participant
|
|
UNION ALL
|
|
SELECT chat_id, user_id, role, joined_at FROM member_participant
|
|
) p
|
|
ORDER BY CASE WHEN role = 'owner' THEN 0 ELSE 1 END
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetChatParticipantParams struct {
|
|
ChatID pgtype.UUID `json:"chat_id"`
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
}
|
|
|
|
type GetChatParticipantRow struct {
|
|
ChatID pgtype.UUID `json:"chat_id"`
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
Role string `json:"role"`
|
|
JoinedAt pgtype.Timestamptz `json:"joined_at"`
|
|
}
|
|
|
|
func (q *Queries) GetChatParticipant(ctx context.Context, arg GetChatParticipantParams) (GetChatParticipantRow, error) {
|
|
row := q.db.QueryRow(ctx, getChatParticipant, arg.ChatID, arg.UserID)
|
|
var i GetChatParticipantRow
|
|
err := row.Scan(
|
|
&i.ChatID,
|
|
&i.UserID,
|
|
&i.Role,
|
|
&i.JoinedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const getChatReadAccessByUser = `-- name: GetChatReadAccessByUser :one
|
|
SELECT
|
|
'participant'::text AS access_mode,
|
|
(CASE
|
|
WHEN b.owner_user_id = $1 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 = $1
|
|
WHERE b.id = $2
|
|
AND (b.owner_user_id = $1 OR bm.user_id IS NOT NULL)
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetChatReadAccessByUserParams struct {
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
ChatID pgtype.UUID `json:"chat_id"`
|
|
}
|
|
|
|
type GetChatReadAccessByUserRow struct {
|
|
AccessMode string `json:"access_mode"`
|
|
ParticipantRole string `json:"participant_role"`
|
|
LastObservedAt pgtype.Timestamptz `json:"last_observed_at"`
|
|
}
|
|
|
|
func (q *Queries) GetChatReadAccessByUser(ctx context.Context, arg GetChatReadAccessByUserParams) (GetChatReadAccessByUserRow, error) {
|
|
row := q.db.QueryRow(ctx, getChatReadAccessByUser, arg.UserID, arg.ChatID)
|
|
var i GetChatReadAccessByUserRow
|
|
err := row.Scan(&i.AccessMode, &i.ParticipantRole, &i.LastObservedAt)
|
|
return i, err
|
|
}
|
|
|
|
const getChatSettings = `-- 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
|
|
`
|
|
|
|
type GetChatSettingsRow struct {
|
|
ChatID pgtype.UUID `json:"chat_id"`
|
|
ModelID pgtype.Text `json:"model_id"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) GetChatSettings(ctx context.Context, id pgtype.UUID) (GetChatSettingsRow, error) {
|
|
row := q.db.QueryRow(ctx, getChatSettings, id)
|
|
var i GetChatSettingsRow
|
|
err := row.Scan(&i.ChatID, &i.ModelID, &i.UpdatedAt)
|
|
return i, err
|
|
}
|
|
|
|
const listChatParticipants = `-- 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 = $1
|
|
),
|
|
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 = $1
|
|
AND bm.user_id <> (SELECT owner_user_id FROM bots WHERE id = $1)
|
|
)
|
|
SELECT chat_id, user_id, role, joined_at
|
|
FROM (
|
|
SELECT chat_id, user_id, role, joined_at FROM owner_participant
|
|
UNION ALL
|
|
SELECT chat_id, user_id, role, joined_at FROM member_participant
|
|
) p
|
|
ORDER BY joined_at ASC
|
|
`
|
|
|
|
type ListChatParticipantsRow struct {
|
|
ChatID pgtype.UUID `json:"chat_id"`
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
Role string `json:"role"`
|
|
JoinedAt pgtype.Timestamptz `json:"joined_at"`
|
|
}
|
|
|
|
func (q *Queries) ListChatParticipants(ctx context.Context, chatID pgtype.UUID) ([]ListChatParticipantsRow, error) {
|
|
rows, err := q.db.Query(ctx, listChatParticipants, chatID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListChatParticipantsRow
|
|
for rows.Next() {
|
|
var i ListChatParticipantsRow
|
|
if err := rows.Scan(
|
|
&i.ChatID,
|
|
&i.UserID,
|
|
&i.Role,
|
|
&i.JoinedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listChatsByBotAndUser = `-- 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 = $1
|
|
LEFT JOIN models chat_models ON chat_models.id = b.chat_model_id
|
|
WHERE b.id = $2
|
|
AND (b.owner_user_id = $1 OR bm.user_id IS NOT NULL)
|
|
ORDER BY b.updated_at DESC
|
|
`
|
|
|
|
type ListChatsByBotAndUserParams struct {
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
}
|
|
|
|
type ListChatsByBotAndUserRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
Kind string `json:"kind"`
|
|
ParentChatID pgtype.UUID `json:"parent_chat_id"`
|
|
Title pgtype.Text `json:"title"`
|
|
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
|
|
Metadata []byte `json:"metadata"`
|
|
ModelID pgtype.Text `json:"model_id"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) ListChatsByBotAndUser(ctx context.Context, arg ListChatsByBotAndUserParams) ([]ListChatsByBotAndUserRow, error) {
|
|
rows, err := q.db.Query(ctx, listChatsByBotAndUser, arg.UserID, arg.BotID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListChatsByBotAndUserRow
|
|
for rows.Next() {
|
|
var i ListChatsByBotAndUserRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.BotID,
|
|
&i.Kind,
|
|
&i.ParentChatID,
|
|
&i.Title,
|
|
&i.CreatedByUserID,
|
|
&i.Metadata,
|
|
&i.ModelID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listThreadsByParent = `-- 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
|
|
`
|
|
|
|
type ListThreadsByParentRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
Kind string `json:"kind"`
|
|
ParentChatID pgtype.UUID `json:"parent_chat_id"`
|
|
Title pgtype.Text `json:"title"`
|
|
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
|
|
Metadata []byte `json:"metadata"`
|
|
ModelID pgtype.Text `json:"model_id"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) ListThreadsByParent(ctx context.Context, id pgtype.UUID) ([]ListThreadsByParentRow, error) {
|
|
rows, err := q.db.Query(ctx, listThreadsByParent, id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListThreadsByParentRow
|
|
for rows.Next() {
|
|
var i ListThreadsByParentRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.BotID,
|
|
&i.Kind,
|
|
&i.ParentChatID,
|
|
&i.Title,
|
|
&i.CreatedByUserID,
|
|
&i.Metadata,
|
|
&i.ModelID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listVisibleChatsByBotAndUser = `-- 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 = $1 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 = $1
|
|
WHERE b.id = $2
|
|
AND (b.owner_user_id = $1 OR bm.user_id IS NOT NULL)
|
|
ORDER BY b.updated_at DESC
|
|
`
|
|
|
|
type ListVisibleChatsByBotAndUserParams struct {
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
}
|
|
|
|
type ListVisibleChatsByBotAndUserRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
Kind string `json:"kind"`
|
|
ParentChatID pgtype.UUID `json:"parent_chat_id"`
|
|
Title pgtype.Text `json:"title"`
|
|
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
|
|
Metadata []byte `json:"metadata"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
AccessMode string `json:"access_mode"`
|
|
ParticipantRole string `json:"participant_role"`
|
|
LastObservedAt pgtype.Timestamptz `json:"last_observed_at"`
|
|
}
|
|
|
|
func (q *Queries) ListVisibleChatsByBotAndUser(ctx context.Context, arg ListVisibleChatsByBotAndUserParams) ([]ListVisibleChatsByBotAndUserRow, error) {
|
|
rows, err := q.db.Query(ctx, listVisibleChatsByBotAndUser, arg.UserID, arg.BotID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ListVisibleChatsByBotAndUserRow
|
|
for rows.Next() {
|
|
var i ListVisibleChatsByBotAndUserRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.BotID,
|
|
&i.Kind,
|
|
&i.ParentChatID,
|
|
&i.Title,
|
|
&i.CreatedByUserID,
|
|
&i.Metadata,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.AccessMode,
|
|
&i.ParticipantRole,
|
|
&i.LastObservedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const removeChatParticipant = `-- name: RemoveChatParticipant :exec
|
|
DELETE FROM bot_members
|
|
WHERE bot_id = $1
|
|
AND user_id = $2
|
|
AND user_id <> (SELECT owner_user_id FROM bots WHERE id = $1)
|
|
`
|
|
|
|
type RemoveChatParticipantParams struct {
|
|
ChatID pgtype.UUID `json:"chat_id"`
|
|
UserID pgtype.UUID `json:"user_id"`
|
|
}
|
|
|
|
func (q *Queries) RemoveChatParticipant(ctx context.Context, arg RemoveChatParticipantParams) error {
|
|
_, err := q.db.Exec(ctx, removeChatParticipant, arg.ChatID, arg.UserID)
|
|
return err
|
|
}
|
|
|
|
const touchChat = `-- name: TouchChat :exec
|
|
UPDATE bots
|
|
SET updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) TouchChat(ctx context.Context, chatID pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, touchChat, chatID)
|
|
return err
|
|
}
|
|
|
|
const updateChatTitle = `-- name: UpdateChatTitle :one
|
|
UPDATE bots
|
|
SET display_name = $1,
|
|
updated_at = now()
|
|
WHERE id = $2
|
|
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
|
|
`
|
|
|
|
type UpdateChatTitleParams struct {
|
|
Title pgtype.Text `json:"title"`
|
|
ID pgtype.UUID `json:"id"`
|
|
}
|
|
|
|
type UpdateChatTitleRow struct {
|
|
ID pgtype.UUID `json:"id"`
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
Kind string `json:"kind"`
|
|
ParentChatID pgtype.UUID `json:"parent_chat_id"`
|
|
Title pgtype.Text `json:"title"`
|
|
CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
|
|
Metadata []byte `json:"metadata"`
|
|
ModelID pgtype.Text `json:"model_id"`
|
|
CreatedAt pgtype.Timestamptz `json:"created_at"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
func (q *Queries) UpdateChatTitle(ctx context.Context, arg UpdateChatTitleParams) (UpdateChatTitleRow, error) {
|
|
row := q.db.QueryRow(ctx, updateChatTitle, arg.Title, arg.ID)
|
|
var i UpdateChatTitleRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.BotID,
|
|
&i.Kind,
|
|
&i.ParentChatID,
|
|
&i.Title,
|
|
&i.CreatedByUserID,
|
|
&i.Metadata,
|
|
&i.ModelID,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const upsertChatSettings = `-- name: UpsertChatSettings :one
|
|
|
|
WITH resolved_model AS (
|
|
SELECT id
|
|
FROM models
|
|
WHERE model_id = NULLIF($1::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 = $2
|
|
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
|
|
`
|
|
|
|
type UpsertChatSettingsParams struct {
|
|
ModelID pgtype.Text `json:"model_id"`
|
|
ID pgtype.UUID `json:"id"`
|
|
}
|
|
|
|
type UpsertChatSettingsRow struct {
|
|
ChatID pgtype.UUID `json:"chat_id"`
|
|
ModelID pgtype.Text `json:"model_id"`
|
|
UpdatedAt pgtype.Timestamptz `json:"updated_at"`
|
|
}
|
|
|
|
// chat_settings
|
|
func (q *Queries) UpsertChatSettings(ctx context.Context, arg UpsertChatSettingsParams) (UpsertChatSettingsRow, error) {
|
|
row := q.db.QueryRow(ctx, upsertChatSettings, arg.ModelID, arg.ID)
|
|
var i UpsertChatSettingsRow
|
|
err := row.Scan(&i.ChatID, &i.ModelID, &i.UpdatedAt)
|
|
return i, err
|
|
}
|