You've already forked agentic-coding-workflow
feat(chat): sqlc queries for conversations/messages/attachments/templates/endpoints/models/usage
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
-- name: InsertAttachment :one
|
||||
INSERT INTO message_attachments (id, user_id, message_id, filename, mime_type, size_bytes, sha256, storage_path)
|
||||
VALUES ($1, $2, NULL, $3, $4, $5, $6, $7)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetAttachment :one
|
||||
SELECT * FROM message_attachments WHERE id = $1;
|
||||
|
||||
-- name: ListAttachmentsForMessage :many
|
||||
SELECT * FROM message_attachments WHERE message_id = $1 ORDER BY created_at ASC;
|
||||
|
||||
-- name: ListOrphanAttachmentsForUser :many
|
||||
SELECT * FROM message_attachments
|
||||
WHERE user_id = $1 AND message_id IS NULL AND id = ANY($2::uuid[]);
|
||||
|
||||
-- name: AttachToMessage :exec
|
||||
UPDATE message_attachments SET message_id = $2 WHERE id = ANY($1::uuid[]);
|
||||
|
||||
-- name: DeleteAttachment :exec
|
||||
DELETE FROM message_attachments WHERE id = $1;
|
||||
|
||||
-- name: ListExpiredOrphans :many
|
||||
SELECT id, storage_path FROM message_attachments
|
||||
WHERE message_id IS NULL AND created_at < NOW() - ($1::interval);
|
||||
|
||||
-- name: ListAttachmentsForSoftDeletedConversations :many
|
||||
SELECT a.id, a.storage_path
|
||||
FROM message_attachments a
|
||||
JOIN messages m ON m.id = a.message_id
|
||||
JOIN conversations c ON c.id = m.conversation_id
|
||||
WHERE c.deleted_at IS NOT NULL
|
||||
AND c.deleted_at < NOW() - ($1::interval);
|
||||
@@ -0,0 +1,28 @@
|
||||
-- name: InsertConversation :one
|
||||
INSERT INTO conversations (id, user_id, project_id, workspace_id, issue_id, model_id, system_prompt, title)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetConversation :one
|
||||
SELECT * FROM conversations WHERE id = $1 AND deleted_at IS NULL;
|
||||
|
||||
-- name: ListConversationsByUser :many
|
||||
SELECT * FROM conversations
|
||||
WHERE user_id = $1 AND deleted_at IS NULL
|
||||
AND ($2::uuid IS NULL OR project_id = $2)
|
||||
AND ($3::uuid IS NULL OR workspace_id = $3)
|
||||
AND ($4::uuid IS NULL OR issue_id = $4)
|
||||
AND ($5::text = '' OR title ILIKE '%' || $5 || '%')
|
||||
ORDER BY updated_at DESC
|
||||
LIMIT $6;
|
||||
|
||||
-- name: UpdateConversationTitle :exec
|
||||
UPDATE conversations SET title = $2, title_generated_at = NOW(), updated_at = NOW()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: SetConversationTitleManual :exec
|
||||
UPDATE conversations SET title = $2, updated_at = NOW() WHERE id = $1;
|
||||
|
||||
-- name: SoftDeleteConversation :exec
|
||||
UPDATE conversations SET deleted_at = NOW(), updated_at = NOW()
|
||||
WHERE id = $1 AND deleted_at IS NULL;
|
||||
@@ -0,0 +1,21 @@
|
||||
-- name: InsertLLMEndpoint :one
|
||||
INSERT INTO llm_endpoints (id, provider, display_name, base_url, api_key_encrypted, created_by)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetLLMEndpoint :one
|
||||
SELECT * FROM llm_endpoints WHERE id = $1;
|
||||
|
||||
-- name: ListLLMEndpoints :many
|
||||
SELECT * FROM llm_endpoints ORDER BY created_at DESC;
|
||||
|
||||
-- name: UpdateLLMEndpoint :exec
|
||||
UPDATE llm_endpoints
|
||||
SET display_name = COALESCE($2, display_name),
|
||||
base_url = COALESCE($3, base_url),
|
||||
api_key_encrypted = COALESCE($4, api_key_encrypted),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: DeleteLLMEndpoint :exec
|
||||
DELETE FROM llm_endpoints WHERE id = $1;
|
||||
@@ -0,0 +1,47 @@
|
||||
-- name: InsertMessage :one
|
||||
INSERT INTO messages (conversation_id, role, content, status)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetMessage :one
|
||||
SELECT * FROM messages WHERE id = $1;
|
||||
|
||||
-- name: ListMessagesByConversation :many
|
||||
SELECT * FROM messages
|
||||
WHERE conversation_id = $1
|
||||
AND ($2::bigint = 0 OR id < $2)
|
||||
ORDER BY id DESC
|
||||
LIMIT $3;
|
||||
|
||||
-- name: ListMessagesAscending :many
|
||||
SELECT * FROM messages WHERE conversation_id = $1 ORDER BY id ASC;
|
||||
|
||||
-- name: UpdateMessageOK :exec
|
||||
UPDATE messages
|
||||
SET content = $2, thinking = $3,
|
||||
prompt_tokens = $4, completion_tokens = $5, thinking_tokens = $6,
|
||||
status = 'ok', updated_at = NOW()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: UpdateMessageError :exec
|
||||
UPDATE messages
|
||||
SET status = 'error', error_message = $2, updated_at = NOW()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: UpdateMessageCancelled :exec
|
||||
UPDATE messages
|
||||
SET status = 'cancelled', updated_at = NOW()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: DeleteMessage :exec
|
||||
DELETE FROM messages WHERE id = $1;
|
||||
|
||||
-- name: GetFirstUserMessage :one
|
||||
SELECT * FROM messages
|
||||
WHERE conversation_id = $1 AND role = 'user'
|
||||
ORDER BY id ASC LIMIT 1;
|
||||
|
||||
-- name: MarkPendingAsErrorOnStartup :exec
|
||||
UPDATE messages
|
||||
SET status = 'error', error_message = 'server restart', updated_at = NOW()
|
||||
WHERE status = 'pending';
|
||||
@@ -0,0 +1,37 @@
|
||||
-- name: InsertLLMModel :one
|
||||
INSERT INTO llm_models (
|
||||
id, endpoint_id, model_id, display_name, capabilities,
|
||||
context_window, max_output_tokens,
|
||||
prompt_price_per_million_usd, completion_price_per_million_usd, thinking_price_per_million_usd,
|
||||
is_title_generator, enabled, sort_order
|
||||
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetLLMModel :one
|
||||
SELECT * FROM llm_models WHERE id = $1;
|
||||
|
||||
-- name: GetTitleGeneratorModel :one
|
||||
SELECT * FROM llm_models WHERE is_title_generator = true AND enabled = true LIMIT 1;
|
||||
|
||||
-- name: ListLLMModels :many
|
||||
SELECT * FROM llm_models
|
||||
WHERE ($1::boolean = false) OR enabled = true
|
||||
ORDER BY sort_order, display_name;
|
||||
|
||||
-- name: UpdateLLMModel :exec
|
||||
UPDATE llm_models
|
||||
SET display_name = COALESCE($2, display_name),
|
||||
capabilities = COALESCE($3, capabilities),
|
||||
context_window = COALESCE($4, context_window),
|
||||
max_output_tokens = COALESCE($5, max_output_tokens),
|
||||
prompt_price_per_million_usd = COALESCE($6, prompt_price_per_million_usd),
|
||||
completion_price_per_million_usd = COALESCE($7, completion_price_per_million_usd),
|
||||
thinking_price_per_million_usd = COALESCE($8, thinking_price_per_million_usd),
|
||||
is_title_generator = COALESCE($9, is_title_generator),
|
||||
enabled = COALESCE($10, enabled),
|
||||
sort_order = COALESCE($11, sort_order),
|
||||
updated_at = NOW()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: DeleteLLMModel :exec
|
||||
DELETE FROM llm_models WHERE id = $1;
|
||||
@@ -0,0 +1,19 @@
|
||||
-- name: InsertPromptTemplate :one
|
||||
INSERT INTO prompt_templates (id, name, content, scope, owner_id)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetPromptTemplate :one
|
||||
SELECT * FROM prompt_templates WHERE id = $1;
|
||||
|
||||
-- name: ListPromptTemplatesForUser :many
|
||||
SELECT * FROM prompt_templates
|
||||
WHERE scope = 'system' OR (scope = 'user' AND owner_id = $1)
|
||||
ORDER BY scope, name;
|
||||
|
||||
-- name: UpdatePromptTemplate :exec
|
||||
UPDATE prompt_templates SET name = $2, content = $3, updated_at = NOW()
|
||||
WHERE id = $1;
|
||||
|
||||
-- name: DeletePromptTemplate :exec
|
||||
DELETE FROM prompt_templates WHERE id = $1;
|
||||
@@ -0,0 +1,49 @@
|
||||
-- name: InsertUsage :exec
|
||||
INSERT INTO llm_usage (
|
||||
conversation_id, message_id, user_id, endpoint_id, model_id,
|
||||
prompt_tokens, completion_tokens, thinking_tokens, cost_usd
|
||||
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9);
|
||||
|
||||
-- name: SummarizeUsageByUser :many
|
||||
SELECT user_id,
|
||||
SUM(prompt_tokens)::int AS prompt_tokens,
|
||||
SUM(completion_tokens)::int AS completion_tokens,
|
||||
SUM(thinking_tokens)::int AS thinking_tokens,
|
||||
SUM(cost_usd)::numeric AS cost_usd
|
||||
FROM llm_usage
|
||||
WHERE created_at >= $1 AND created_at < $2
|
||||
GROUP BY user_id
|
||||
ORDER BY cost_usd DESC;
|
||||
|
||||
-- name: SummarizeUsageByModel :many
|
||||
SELECT model_id,
|
||||
SUM(prompt_tokens)::int AS prompt_tokens,
|
||||
SUM(completion_tokens)::int AS completion_tokens,
|
||||
SUM(thinking_tokens)::int AS thinking_tokens,
|
||||
SUM(cost_usd)::numeric AS cost_usd
|
||||
FROM llm_usage
|
||||
WHERE created_at >= $1 AND created_at < $2
|
||||
GROUP BY model_id
|
||||
ORDER BY cost_usd DESC;
|
||||
|
||||
-- name: SummarizeUsageByDay :many
|
||||
SELECT date_trunc('day', created_at) AS day,
|
||||
SUM(prompt_tokens)::int AS prompt_tokens,
|
||||
SUM(completion_tokens)::int AS completion_tokens,
|
||||
SUM(thinking_tokens)::int AS thinking_tokens,
|
||||
SUM(cost_usd)::numeric AS cost_usd
|
||||
FROM llm_usage
|
||||
WHERE created_at >= $1 AND created_at < $2
|
||||
GROUP BY day
|
||||
ORDER BY day;
|
||||
|
||||
-- name: SummarizeUserDaily :many
|
||||
SELECT date_trunc('day', created_at) AS day,
|
||||
SUM(prompt_tokens)::int AS prompt_tokens,
|
||||
SUM(completion_tokens)::int AS completion_tokens,
|
||||
SUM(thinking_tokens)::int AS thinking_tokens,
|
||||
SUM(cost_usd)::numeric AS cost_usd
|
||||
FROM llm_usage
|
||||
WHERE user_id = $1 AND created_at >= $2 AND created_at < $3
|
||||
GROUP BY day
|
||||
ORDER BY day;
|
||||
Reference in New Issue
Block a user