chore: import upstream snapshot with attribution

This commit is contained in:
wehub-resource-sync
2026-07-13 13:28:13 +08:00
commit 0878425be3
1160 changed files with 491311 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
-- name: GetFlowAgentLogs :many
SELECT
al.*
FROM agentlogs al
INNER JOIN flows f ON al.flow_id = f.id
WHERE al.flow_id = $1 AND f.deleted_at IS NULL
ORDER BY al.created_at ASC;
-- name: GetUserFlowAgentLogs :many
SELECT
al.*
FROM agentlogs al
INNER JOIN flows f ON al.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE al.flow_id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL
ORDER BY al.created_at ASC;
-- name: GetTaskAgentLogs :many
SELECT
al.*
FROM agentlogs al
INNER JOIN flows f ON al.flow_id = f.id
INNER JOIN tasks t ON al.task_id = t.id
WHERE al.task_id = $1 AND f.deleted_at IS NULL
ORDER BY al.created_at ASC;
-- name: GetSubtaskAgentLogs :many
SELECT
al.*
FROM agentlogs al
INNER JOIN flows f ON al.flow_id = f.id
INNER JOIN subtasks s ON al.subtask_id = s.id
WHERE al.subtask_id = $1 AND f.deleted_at IS NULL
ORDER BY al.created_at ASC;
-- name: GetFlowAgentLog :one
SELECT
al.*
FROM agentlogs al
INNER JOIN flows f ON al.flow_id = f.id
WHERE al.id = $1 AND al.flow_id = $2 AND f.deleted_at IS NULL;
-- name: CreateAgentLog :one
INSERT INTO agentlogs (
initiator,
executor,
task,
result,
flow_id,
task_id,
subtask_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7
)
RETURNING *;
+59
View File
@@ -0,0 +1,59 @@
-- name: GetFlowsForPeriodLastWeek :many
-- Get flow IDs created in the last week for analytics
SELECT id, title
FROM flows
WHERE created_at >= NOW() - INTERVAL '7 days' AND deleted_at IS NULL AND user_id = $1
ORDER BY created_at DESC;
-- name: GetFlowsForPeriodLastMonth :many
-- Get flow IDs created in the last month for analytics
SELECT id, title
FROM flows
WHERE created_at >= NOW() - INTERVAL '30 days' AND deleted_at IS NULL AND user_id = $1
ORDER BY created_at DESC;
-- name: GetFlowsForPeriodLast3Months :many
-- Get flow IDs created in the last 3 months for analytics
SELECT id, title
FROM flows
WHERE created_at >= NOW() - INTERVAL '90 days' AND deleted_at IS NULL AND user_id = $1
ORDER BY created_at DESC;
-- name: GetTasksForFlow :many
-- Get all tasks for a flow
SELECT id, title, created_at, updated_at
FROM tasks
WHERE flow_id = $1
ORDER BY id ASC;
-- name: GetSubtasksForTasks :many
-- Get all subtasks for multiple tasks
SELECT id, task_id, title, status, created_at, updated_at
FROM subtasks
WHERE task_id = ANY(@task_ids::BIGINT[])
ORDER BY id ASC;
-- name: GetMsgchainsForFlow :many
-- Get all msgchains for a flow (including task and subtask level)
SELECT id, type, flow_id, task_id, subtask_id, duration_seconds, created_at, updated_at
FROM msgchains
WHERE flow_id = $1
ORDER BY created_at ASC;
-- name: GetToolcallsForFlow :many
-- Get all toolcalls for a flow
SELECT tc.id, tc.status, tc.flow_id, tc.task_id, tc.subtask_id, tc.duration_seconds, tc.created_at, tc.updated_at
FROM toolcalls tc
LEFT JOIN tasks t ON tc.task_id = t.id
LEFT JOIN subtasks s ON tc.subtask_id = s.id
INNER JOIN flows f ON tc.flow_id = f.id
WHERE tc.flow_id = $1 AND f.deleted_at IS NULL
AND (tc.task_id IS NULL OR t.id IS NOT NULL)
AND (tc.subtask_id IS NULL OR s.id IS NOT NULL)
ORDER BY tc.created_at ASC;
-- name: GetAssistantsCountForFlow :one
-- Get total count of assistants for a specific flow
SELECT COALESCE(COUNT(id), 0)::bigint AS total_assistants_count
FROM assistants
WHERE flow_id = $1 AND deleted_at IS NULL;
+83
View File
@@ -0,0 +1,83 @@
-- name: GetAPITokens :many
SELECT
t.*
FROM api_tokens t
WHERE t.deleted_at IS NULL
ORDER BY t.created_at DESC;
-- name: GetAPIToken :one
SELECT
t.*
FROM api_tokens t
WHERE t.id = $1 AND t.deleted_at IS NULL;
-- name: GetAPITokenByTokenID :one
SELECT
t.*
FROM api_tokens t
WHERE t.token_id = $1 AND t.deleted_at IS NULL;
-- name: GetUserAPITokens :many
SELECT
t.*
FROM api_tokens t
INNER JOIN users u ON t.user_id = u.id
WHERE t.user_id = $1 AND t.deleted_at IS NULL
ORDER BY t.created_at DESC;
-- name: GetUserAPIToken :one
SELECT
t.*
FROM api_tokens t
INNER JOIN users u ON t.user_id = u.id
WHERE t.id = $1 AND t.user_id = $2 AND t.deleted_at IS NULL;
-- name: GetUserAPITokenByTokenID :one
SELECT
t.*
FROM api_tokens t
INNER JOIN users u ON t.user_id = u.id
WHERE t.token_id = $1 AND t.user_id = $2 AND t.deleted_at IS NULL;
-- name: CreateAPIToken :one
INSERT INTO api_tokens (
token_id,
user_id,
role_id,
name,
ttl,
status
) VALUES (
$1, $2, $3, $4, $5, $6
)
RETURNING *;
-- name: UpdateAPIToken :one
UPDATE api_tokens
SET name = $2, status = $3
WHERE id = $1
RETURNING *;
-- name: UpdateUserAPIToken :one
UPDATE api_tokens
SET name = $3, status = $4
WHERE id = $1 AND user_id = $2
RETURNING *;
-- name: DeleteAPIToken :one
UPDATE api_tokens
SET deleted_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING *;
-- name: DeleteUserAPIToken :one
UPDATE api_tokens
SET deleted_at = CURRENT_TIMESTAMP
WHERE id = $1 AND user_id = $2
RETURNING *;
-- name: DeleteUserAPITokenByTokenID :one
UPDATE api_tokens
SET deleted_at = CURRENT_TIMESTAMP
WHERE token_id = $1 AND user_id = $2
RETURNING *;
+76
View File
@@ -0,0 +1,76 @@
-- name: GetFlowAssistantLogs :many
SELECT
al.*
FROM assistantlogs al
INNER JOIN assistants a ON al.assistant_id = a.id
INNER JOIN flows f ON al.flow_id = f.id
WHERE al.flow_id = $1 AND al.assistant_id = $2 AND f.deleted_at IS NULL AND a.deleted_at IS NULL
ORDER BY al.created_at ASC;
-- name: GetUserFlowAssistantLogs :many
SELECT
al.*
FROM assistantlogs al
INNER JOIN assistants a ON al.assistant_id = a.id
INNER JOIN flows f ON al.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE al.flow_id = $1 AND al.assistant_id = $2 AND f.user_id = $3 AND f.deleted_at IS NULL AND a.deleted_at IS NULL
ORDER BY al.created_at ASC;
-- name: GetFlowAssistantLog :one
SELECT
al.*
FROM assistantlogs al
INNER JOIN assistants a ON al.assistant_id = a.id
INNER JOIN flows f ON al.flow_id = f.id
WHERE al.id = $1 AND f.deleted_at IS NULL AND a.deleted_at IS NULL;
-- name: CreateAssistantLog :one
INSERT INTO assistantlogs (
type,
message,
thinking,
flow_id,
assistant_id
)
VALUES (
$1, $2, $3, $4, $5
)
RETURNING *;
-- name: CreateResultAssistantLog :one
INSERT INTO assistantlogs (
type,
message,
thinking,
result,
result_format,
flow_id,
assistant_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7
)
RETURNING *;
-- name: UpdateAssistantLog :one
UPDATE assistantlogs
SET type = $1, message = $2, thinking = $3, result = $4, result_format = $5
WHERE id = $6
RETURNING *;
-- name: UpdateAssistantLogContent :one
UPDATE assistantlogs
SET type = $1, message = $2, thinking = $3
WHERE id = $4
RETURNING *;
-- name: UpdateAssistantLogResult :one
UPDATE assistantlogs
SET result = $1, result_format = $2
WHERE id = $3
RETURNING *;
-- name: DeleteFlowAssistantLog :exec
DELETE FROM assistantlogs
WHERE id = $1;
+98
View File
@@ -0,0 +1,98 @@
-- name: GetFlowAssistants :many
SELECT
a.*
FROM assistants a
INNER JOIN flows f ON a.flow_id = f.id
WHERE a.flow_id = $1 AND f.deleted_at IS NULL AND a.deleted_at IS NULL
ORDER BY a.created_at DESC;
-- name: GetUserFlowAssistants :many
SELECT
a.*
FROM assistants a
INNER JOIN flows f ON a.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE a.flow_id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL AND a.deleted_at IS NULL
ORDER BY a.created_at DESC;
-- name: GetFlowAssistant :one
SELECT
a.*
FROM assistants a
INNER JOIN flows f ON a.flow_id = f.id
WHERE a.id = $1 AND a.flow_id = $2 AND f.deleted_at IS NULL AND a.deleted_at IS NULL;
-- name: GetUserFlowAssistant :one
SELECT
a.*
FROM assistants a
INNER JOIN flows f ON a.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE a.id = $1 AND a.flow_id = $2 AND f.user_id = $3 AND f.deleted_at IS NULL AND a.deleted_at IS NULL;
-- name: GetAssistant :one
SELECT
a.*
FROM assistants a
WHERE a.id = $1 AND a.deleted_at IS NULL;
-- name: GetAssistantUseAgents :one
SELECT use_agents
FROM assistants
WHERE id = $1 AND deleted_at IS NULL;
-- name: CreateAssistant :one
INSERT INTO assistants (
title, status, model, model_provider_name, model_provider_type, language, tool_call_id_template, functions, flow_id, use_agents
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10
)
RETURNING *;
-- name: UpdateAssistant :one
UPDATE assistants
SET title = $1, model = $2, language = $3, tool_call_id_template = $4, functions = $5, trace_id = $6, msgchain_id = $7
WHERE id = $8
RETURNING *;
-- name: UpdateAssistantUseAgents :one
UPDATE assistants
SET use_agents = $1
WHERE id = $2
RETURNING *;
-- name: UpdateAssistantStatus :one
UPDATE assistants
SET status = $1
WHERE id = $2
RETURNING *;
-- name: UpdateAssistantTitle :one
UPDATE assistants
SET title = $1
WHERE id = $2
RETURNING *;
-- name: UpdateAssistantModel :one
UPDATE assistants
SET model = $1
WHERE id = $2
RETURNING *;
-- name: UpdateAssistantLanguage :one
UPDATE assistants
SET language = $1
WHERE id = $2
RETURNING *;
-- name: UpdateAssistantToolCallIDTemplate :one
UPDATE assistants
SET tool_call_id_template = $1
WHERE id = $2
RETURNING *;
-- name: DeleteAssistant :one
UPDATE assistants
SET deleted_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING *;
+85
View File
@@ -0,0 +1,85 @@
-- name: GetContainers :many
SELECT
c.*
FROM containers c
INNER JOIN flows f ON c.flow_id = f.id
WHERE f.deleted_at IS NULL
ORDER BY c.created_at DESC;
-- name: GetUserContainers :many
SELECT
c.*
FROM containers c
INNER JOIN flows f ON c.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE f.user_id = $1 AND f.deleted_at IS NULL
ORDER BY c.created_at DESC;
-- name: GetRunningContainers :many
SELECT
c.*
FROM containers c
INNER JOIN flows f ON c.flow_id = f.id
WHERE c.status = 'running' AND f.deleted_at IS NULL
ORDER BY c.created_at DESC;
-- name: GetFlowContainers :many
SELECT
c.*
FROM containers c
INNER JOIN flows f ON c.flow_id = f.id
WHERE c.flow_id = $1 AND f.deleted_at IS NULL
ORDER BY c.created_at DESC;
-- name: GetFlowPrimaryContainer :one
SELECT
c.*
FROM containers c
INNER JOIN flows f ON c.flow_id = f.id
WHERE c.flow_id = $1 AND c.type = 'primary' AND f.deleted_at IS NULL
ORDER BY c.created_at DESC
LIMIT 1;
-- name: GetUserFlowContainers :many
SELECT
c.*
FROM containers c
INNER JOIN flows f ON c.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE c.flow_id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL
ORDER BY c.created_at DESC;
-- name: CreateContainer :one
INSERT INTO containers (
type, name, image, status, flow_id, local_id, local_dir
)
VALUES (
$1, $2, $3, $4, $5, $6, $7
)
ON CONFLICT ON CONSTRAINT containers_local_id_unique
DO UPDATE SET
type = EXCLUDED.type,
name = EXCLUDED.name,
image = EXCLUDED.image,
status = EXCLUDED.status,
flow_id = EXCLUDED.flow_id,
local_dir = EXCLUDED.local_dir
RETURNING *;
-- name: UpdateContainerStatusLocalID :one
UPDATE containers
SET status = $1, local_id = $2
WHERE id = $3
RETURNING *;
-- name: UpdateContainerStatus :one
UPDATE containers
SET status = $1
WHERE id = $2
RETURNING *;
-- name: UpdateContainerImage :one
UPDATE containers
SET image = $1
WHERE id = $2
RETURNING *;
+32
View File
@@ -0,0 +1,32 @@
-- name: GetFlowTemplate :one
SELECT * FROM flow_templates
WHERE id = $1 AND user_id = $2 LIMIT 1;
-- name: GetFlowTemplatesByUserID :many
SELECT * FROM flow_templates
WHERE user_id = $1
ORDER BY created_at DESC;
-- name: CreateFlowTemplate :one
INSERT INTO flow_templates (
user_id,
title,
text
) VALUES (
$1,
$2,
$3
)
RETURNING *;
-- name: UpdateFlowTemplate :one
UPDATE flow_templates
SET
title = $3,
text = $4
WHERE id = $1 AND user_id = $2
RETURNING *;
-- name: DeleteFlowTemplate :exec
DELETE FROM flow_templates
WHERE id = $1 AND user_id = $2;
+153
View File
@@ -0,0 +1,153 @@
-- name: GetFlows :many
SELECT
f.*
FROM flows f
WHERE f.deleted_at IS NULL
ORDER BY f.created_at DESC;
-- name: GetUserFlows :many
SELECT
f.*
FROM flows f
INNER JOIN users u ON f.user_id = u.id
WHERE f.user_id = $1 AND f.deleted_at IS NULL
ORDER BY f.created_at DESC;
-- name: GetFlow :one
SELECT
f.*
FROM flows f
WHERE f.id = $1 AND f.deleted_at IS NULL;
-- name: GetUserFlow :one
SELECT
f.*
FROM flows f
INNER JOIN users u ON f.user_id = u.id
WHERE f.id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL;
-- name: CreateFlow :one
INSERT INTO flows (
title, status, model, model_provider_name, model_provider_type, language, tool_call_id_template, functions, user_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
)
RETURNING *;
-- name: UpdateFlow :one
UPDATE flows
SET title = $1, model = $2, language = $3, tool_call_id_template = $4, functions = $5, trace_id = $6
WHERE id = $7
RETURNING *;
-- name: UpdateFlowStatus :one
UPDATE flows
SET status = $1
WHERE id = $2
RETURNING *;
-- name: UpdateFlowTitle :one
UPDATE flows
SET title = $1
WHERE id = $2
RETURNING *;
-- name: UpdateFlowLanguage :one
UPDATE flows
SET language = $1
WHERE id = $2
RETURNING *;
-- name: UpdateFlowToolCallIDTemplate :one
UPDATE flows
SET tool_call_id_template = $1
WHERE id = $2
RETURNING *;
-- name: UpdateFlowProvider :one
UPDATE flows
SET model_provider_name = $1, model_provider_type = $2, tool_call_id_template = $3, model = $4
WHERE id = $5
RETURNING *;
-- name: DeleteFlow :one
UPDATE flows
SET deleted_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING *;
-- ==================== Flows Analytics Queries ====================
-- name: GetFlowStats :one
-- Get total count of tasks, subtasks, and assistants for a specific flow
SELECT
COALESCE(COUNT(DISTINCT t.id), 0)::bigint AS total_tasks_count,
COALESCE(COUNT(DISTINCT s.id), 0)::bigint AS total_subtasks_count,
COALESCE(COUNT(DISTINCT a.id), 0)::bigint AS total_assistants_count
FROM flows f
LEFT JOIN tasks t ON f.id = t.flow_id
LEFT JOIN subtasks s ON t.id = s.task_id
LEFT JOIN assistants a ON f.id = a.flow_id AND a.deleted_at IS NULL
WHERE f.id = $1 AND f.deleted_at IS NULL;
-- name: GetUserTotalFlowsStats :one
-- Get total count of flows, tasks, subtasks, and assistants for a user
SELECT
COALESCE(COUNT(DISTINCT f.id), 0)::bigint AS total_flows_count,
COALESCE(COUNT(DISTINCT t.id), 0)::bigint AS total_tasks_count,
COALESCE(COUNT(DISTINCT s.id), 0)::bigint AS total_subtasks_count,
COALESCE(COUNT(DISTINCT a.id), 0)::bigint AS total_assistants_count
FROM flows f
LEFT JOIN tasks t ON f.id = t.flow_id
LEFT JOIN subtasks s ON t.id = s.task_id
LEFT JOIN assistants a ON f.id = a.flow_id AND a.deleted_at IS NULL
WHERE f.user_id = $1 AND f.deleted_at IS NULL;
-- name: GetFlowsStatsByDayLastWeek :many
-- Get flows stats by day for the last week
SELECT
DATE(f.created_at) AS date,
COALESCE(COUNT(DISTINCT f.id), 0)::bigint AS total_flows_count,
COALESCE(COUNT(DISTINCT t.id), 0)::bigint AS total_tasks_count,
COALESCE(COUNT(DISTINCT s.id), 0)::bigint AS total_subtasks_count,
COALESCE(COUNT(DISTINCT a.id), 0)::bigint AS total_assistants_count
FROM flows f
LEFT JOIN tasks t ON f.id = t.flow_id
LEFT JOIN subtasks s ON t.id = s.task_id
LEFT JOIN assistants a ON f.id = a.flow_id AND a.deleted_at IS NULL
WHERE f.created_at >= NOW() - INTERVAL '7 days' AND f.deleted_at IS NULL AND f.user_id = $1
GROUP BY DATE(f.created_at)
ORDER BY date DESC;
-- name: GetFlowsStatsByDayLastMonth :many
-- Get flows stats by day for the last month
SELECT
DATE(f.created_at) AS date,
COALESCE(COUNT(DISTINCT f.id), 0)::bigint AS total_flows_count,
COALESCE(COUNT(DISTINCT t.id), 0)::bigint AS total_tasks_count,
COALESCE(COUNT(DISTINCT s.id), 0)::bigint AS total_subtasks_count,
COALESCE(COUNT(DISTINCT a.id), 0)::bigint AS total_assistants_count
FROM flows f
LEFT JOIN tasks t ON f.id = t.flow_id
LEFT JOIN subtasks s ON t.id = s.task_id
LEFT JOIN assistants a ON f.id = a.flow_id AND a.deleted_at IS NULL
WHERE f.created_at >= NOW() - INTERVAL '30 days' AND f.deleted_at IS NULL AND f.user_id = $1
GROUP BY DATE(f.created_at)
ORDER BY date DESC;
-- name: GetFlowsStatsByDayLast3Months :many
-- Get flows stats by day for the last 3 months
SELECT
DATE(f.created_at) AS date,
COALESCE(COUNT(DISTINCT f.id), 0)::bigint AS total_flows_count,
COALESCE(COUNT(DISTINCT t.id), 0)::bigint AS total_tasks_count,
COALESCE(COUNT(DISTINCT s.id), 0)::bigint AS total_subtasks_count,
COALESCE(COUNT(DISTINCT a.id), 0)::bigint AS total_assistants_count
FROM flows f
LEFT JOIN tasks t ON f.id = t.flow_id
LEFT JOIN subtasks s ON t.id = s.task_id
LEFT JOIN assistants a ON f.id = a.flow_id AND a.deleted_at IS NULL
WHERE f.created_at >= NOW() - INTERVAL '90 days' AND f.deleted_at IS NULL AND f.user_id = $1
GROUP BY DATE(f.created_at)
ORDER BY date DESC;
+155
View File
@@ -0,0 +1,155 @@
-- name: GetKnowledgeDocument :one
-- Fetch a single knowledge document by its UUID (admin view — no user_id check).
SELECT
e.uuid::text AS id,
COALESCE(e.document, '') AS document,
COALESCE(e.cmetadata::text, '{}') AS cmetadata
FROM langchain_pg_embedding e
INNER JOIN langchain_pg_collection c ON e.collection_id = c.uuid
WHERE c.name = 'langchain'
AND e.uuid::text = sqlc.arg(uuid);
-- name: GetUserKnowledgeDocument :one
-- Fetch a single knowledge document by UUID, scoped to a specific user.
SELECT
e.uuid::text AS id,
COALESCE(e.document, '') AS document,
COALESCE(e.cmetadata::text, '{}') AS cmetadata
FROM langchain_pg_embedding e
INNER JOIN langchain_pg_collection c ON e.collection_id = c.uuid
WHERE c.name = 'langchain'
AND e.uuid::text = sqlc.arg(uuid)
AND (e.cmetadata ->> 'user_id') = sqlc.arg(user_id);
-- name: ListAllKnowledgeDocuments :many
-- List all knowledge documents excluding the noisy memory type (admin view).
SELECT
e.uuid::text AS id,
COALESCE(e.document, '') AS document,
COALESCE(e.cmetadata::text, '{}') AS cmetadata
FROM langchain_pg_embedding e
INNER JOIN langchain_pg_collection c ON e.collection_id = c.uuid
WHERE c.name = 'langchain'
AND COALESCE(e.cmetadata ->> 'doc_type', '') NOT IN ('memory')
ORDER BY e.uuid;
-- name: ListFlowKnowledgeDocuments :many
-- List non-memory knowledge documents belonging to a specific flow (admin scoped).
SELECT
e.uuid::text AS id,
COALESCE(e.document, '') AS document,
COALESCE(e.cmetadata::text, '{}') AS cmetadata
FROM langchain_pg_embedding e
INNER JOIN langchain_pg_collection c ON e.collection_id = c.uuid
WHERE c.name = 'langchain'
AND COALESCE(e.cmetadata ->> 'doc_type', '') NOT IN ('memory')
AND (e.cmetadata ->> 'flow_id') = sqlc.arg(flow_id)
ORDER BY e.uuid;
-- name: ListUserKnowledgeDocuments :many
-- List all non-memory knowledge documents owned by a specific user (user-scoped view).
SELECT
e.uuid::text AS id,
COALESCE(e.document, '') AS document,
COALESCE(e.cmetadata::text, '{}') AS cmetadata
FROM langchain_pg_embedding e
INNER JOIN langchain_pg_collection c ON e.collection_id = c.uuid
WHERE c.name = 'langchain'
AND COALESCE(e.cmetadata ->> 'doc_type', '') NOT IN ('memory')
AND (e.cmetadata ->> 'user_id') = sqlc.arg(user_id)
ORDER BY e.uuid;
-- name: UpdateKnowledgeDocument :one
-- Update an existing document's embedding, text and metadata atomically.
-- embedding must be formatted as a PostgreSQL vector literal: '[f1,f2,...]'
-- cmetadata must be valid JSON text.
UPDATE langchain_pg_embedding
SET
embedding = sqlc.arg(embedding)::vector,
document = sqlc.arg(document),
cmetadata = sqlc.arg(cmetadata)::json
WHERE uuid::text = sqlc.arg(uuid)
AND collection_id = (SELECT uuid FROM langchain_pg_collection WHERE name = 'langchain')
RETURNING
uuid::text AS id,
COALESCE(document, '') AS document,
COALESCE(cmetadata::text, '{}') AS cmetadata;
-- name: DeleteKnowledgeDocument :exec
-- Delete a knowledge document by UUID (admin — no user_id check).
DELETE FROM langchain_pg_embedding
WHERE uuid::text = sqlc.arg(uuid)
AND collection_id = (SELECT uuid FROM langchain_pg_collection WHERE name = 'langchain');
-- name: DeleteUserKnowledgeDocument :exec
-- Delete a knowledge document by UUID, only if it belongs to the given user.
DELETE FROM langchain_pg_embedding
WHERE uuid::text = sqlc.arg(uuid)
AND (cmetadata ->> 'user_id') = sqlc.arg(user_id)
AND collection_id = (SELECT uuid FROM langchain_pg_collection WHERE name = 'langchain');
-- name: SearchKnowledgeDocuments :many
-- Vector similarity search over all knowledge documents (admin view, no user filter).
-- Returns rows ordered by cosine similarity descending (highest score first).
-- embedding query vector as a PostgreSQL vector literal, e.g. '[0.1,0.2,...]'
-- max_distance cosine-distance upper bound (exclusive); equals (1 - score_threshold),
-- e.g. pass 0.8 to get documents with similarity score > 0.2
-- lim maximum number of rows to return
SELECT
e.uuid::text AS id,
COALESCE(e.document, '') AS document,
COALESCE(e.cmetadata::text, '{}') AS cmetadata,
(1.0 - (e.embedding <=> sqlc.arg(embedding)::vector))::float8 AS score
FROM langchain_pg_embedding e
INNER JOIN langchain_pg_collection c ON e.collection_id = c.uuid
WHERE c.name = 'langchain'
AND COALESCE(e.cmetadata ->> 'doc_type', '') NOT IN ('memory')
AND (e.embedding <=> sqlc.arg(embedding)::vector)::float8 < sqlc.arg(max_distance)::float8
ORDER BY e.embedding <=> sqlc.arg(embedding)::vector
LIMIT sqlc.arg(lim)::int;
-- name: SearchUserKnowledgeDocuments :many
-- Vector similarity search scoped to a specific user (by cmetadata user_id).
-- Returns rows ordered by cosine similarity descending (highest score first).
-- embedding query vector as a PostgreSQL vector literal, e.g. '[0.1,0.2,...]'
-- max_distance cosine-distance upper bound (exclusive); equals (1 - score_threshold)
-- lim maximum number of rows to return
-- user_id owner filter as a decimal text string (e.g. "42")
SELECT
e.uuid::text AS id,
COALESCE(e.document, '') AS document,
COALESCE(e.cmetadata::text, '{}') AS cmetadata,
(1.0 - (e.embedding <=> sqlc.arg(embedding)::vector))::float8 AS score
FROM langchain_pg_embedding e
INNER JOIN langchain_pg_collection c ON e.collection_id = c.uuid
WHERE c.name = 'langchain'
AND COALESCE(e.cmetadata ->> 'doc_type', '') NOT IN ('memory')
AND (e.cmetadata ->> 'user_id') = sqlc.arg(user_id)
AND (e.embedding <=> sqlc.arg(embedding)::vector)::float8 < sqlc.arg(max_distance)::float8
ORDER BY e.embedding <=> sqlc.arg(embedding)::vector
LIMIT sqlc.arg(lim)::int;
-- name: DeleteFlowMemoryDocuments :exec
-- Delete all memory-type documents for a specific flow.
-- Called on flow deletion to free long-term memory that will never be re-used.
-- flow_id is the decimal text representation of the flow ID (e.g. "55"), matching the
-- text result of (cmetadata ->> 'flow_id') which uses JSON ->> extraction.
DELETE FROM langchain_pg_embedding
WHERE collection_id = (SELECT uuid FROM langchain_pg_collection WHERE name = 'langchain')
AND COALESCE(cmetadata ->> 'doc_type', '') = 'memory'
AND (cmetadata ->> 'flow_id') = sqlc.arg(flow_id);
-- name: InsertKnowledgeDocument :one
-- Insert a document with a pre-computed embedding vector and return its UUID.
-- embedding must be formatted as a PostgreSQL vector literal: '[f1,f2,...]'
-- cmetadata must be valid JSON text.
INSERT INTO langchain_pg_embedding (uuid, document, embedding, cmetadata, collection_id)
SELECT
sqlc.arg(uuid)::uuid,
sqlc.arg(document),
sqlc.arg(embedding)::vector,
sqlc.arg(cmetadata)::json,
c.uuid
FROM langchain_pg_collection c
WHERE c.name = 'langchain'
RETURNING uuid::text AS id;
+336
View File
@@ -0,0 +1,336 @@
-- name: GetSubtaskMsgChains :many
SELECT
mc.*
FROM msgchains mc
WHERE mc.subtask_id = $1
ORDER BY mc.created_at DESC;
-- name: GetSubtaskPrimaryMsgChains :many
SELECT
mc.*
FROM msgchains mc
WHERE mc.subtask_id = $1 AND mc.type = 'primary_agent'
ORDER BY mc.created_at DESC;
-- name: GetSubtaskTypeMsgChains :many
SELECT
mc.*
FROM msgchains mc
WHERE mc.subtask_id = $1 AND mc.type = $2
ORDER BY mc.created_at DESC;
-- name: GetTaskMsgChains :many
SELECT
mc.*
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
WHERE mc.task_id = $1 OR s.task_id = $1
ORDER BY mc.created_at DESC;
-- name: GetTaskPrimaryMsgChains :many
SELECT
mc.*
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
WHERE (mc.task_id = $1 OR s.task_id = $1) AND mc.type = 'primary_agent'
ORDER BY mc.created_at DESC;
-- name: GetTaskPrimaryMsgChainIDs :many
SELECT DISTINCT
mc.id,
mc.subtask_id
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
WHERE (mc.task_id = $1 OR s.task_id = $1) AND mc.type = 'primary_agent';
-- name: GetTaskTypeMsgChains :many
SELECT
mc.*
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
WHERE (mc.task_id = $1 OR s.task_id = $1) AND mc.type = $2
ORDER BY mc.created_at DESC;
-- name: GetFlowMsgChains :many
SELECT
mc.*
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id
WHERE mc.flow_id = $1 OR t.flow_id = $1
ORDER BY mc.created_at DESC;
-- name: GetFlowTypeMsgChains :many
SELECT
mc.*
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id
WHERE (mc.flow_id = $1 OR t.flow_id = $1) AND mc.type = $2
ORDER BY mc.created_at DESC;
-- name: GetFlowTaskTypeLastMsgChain :one
SELECT
mc.*
FROM msgchains mc
WHERE mc.flow_id = $1 AND (mc.task_id = $2 OR $2 IS NULL) AND mc.type = $3
ORDER BY mc.created_at DESC
LIMIT 1;
-- name: GetMsgChain :one
SELECT
mc.*
FROM msgchains mc
WHERE mc.id = $1;
-- name: CreateMsgChain :one
INSERT INTO msgchains (
type,
model,
model_provider,
usage_in,
usage_out,
usage_cache_in,
usage_cache_out,
usage_cost_in,
usage_cost_out,
duration_seconds,
chain,
flow_id,
task_id,
subtask_id
) VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14
)
RETURNING *;
-- name: UpdateMsgChain :one
UPDATE msgchains
SET chain = $1, duration_seconds = duration_seconds + $2, model = $3, model_provider = $4
WHERE id = $5
RETURNING *;
-- name: UpdateMsgChainUsage :one
UPDATE msgchains
SET
usage_in = usage_in + $1,
usage_out = usage_out + $2,
usage_cache_in = usage_cache_in + $3,
usage_cache_out = usage_cache_out + $4,
usage_cost_in = usage_cost_in + $5,
usage_cost_out = usage_cost_out + $6,
duration_seconds = duration_seconds + $7
WHERE id = $8
RETURNING *;
-- name: GetFlowUsageStats :one
SELECT
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE (mc.flow_id = $1 OR t.flow_id = $1) AND f.deleted_at IS NULL;
-- name: GetTaskUsageStats :one
SELECT
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON mc.task_id = t.id OR s.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE (mc.task_id = $1 OR s.task_id = $1) AND f.deleted_at IS NULL;
-- name: GetSubtaskUsageStats :one
SELECT
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE mc.subtask_id = $1 AND f.deleted_at IS NULL;
-- name: GetAllFlowsUsageStats :many
SELECT
COALESCE(mc.flow_id, t.flow_id) AS flow_id,
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE f.deleted_at IS NULL
GROUP BY COALESCE(mc.flow_id, t.flow_id)
ORDER BY COALESCE(mc.flow_id, t.flow_id);
-- name: GetUsageStatsByProvider :many
SELECT
mc.model_provider,
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE f.deleted_at IS NULL AND f.user_id = $1
GROUP BY mc.model_provider
ORDER BY mc.model_provider;
-- name: GetUsageStatsByModel :many
SELECT
mc.model,
mc.model_provider,
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE f.deleted_at IS NULL AND f.user_id = $1
GROUP BY mc.model, mc.model_provider
ORDER BY mc.model, mc.model_provider;
-- name: GetUsageStatsByType :many
SELECT
mc.type,
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE f.deleted_at IS NULL AND f.user_id = $1
GROUP BY mc.type
ORDER BY mc.type;
-- name: GetUsageStatsByTypeForFlow :many
SELECT
mc.type,
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE (mc.flow_id = $1 OR t.flow_id = $1) AND f.deleted_at IS NULL
GROUP BY mc.type
ORDER BY mc.type;
-- name: GetUsageStatsByModelAgentsForFlow :many
SELECT
mc.model,
mc.model_provider,
array_agg(DISTINCT mc.type::text)::text[] AS agent_types,
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE (mc.flow_id = $1 OR t.flow_id = $1) AND f.deleted_at IS NULL
GROUP BY mc.model, mc.model_provider
ORDER BY mc.model, mc.model_provider;
-- name: GetUsageStatsByDayLastWeek :many
SELECT
DATE(mc.created_at) AS date,
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE mc.created_at >= NOW() - INTERVAL '7 days' AND f.deleted_at IS NULL AND f.user_id = $1
GROUP BY DATE(mc.created_at)
ORDER BY date DESC;
-- name: GetUsageStatsByDayLastMonth :many
SELECT
DATE(mc.created_at) AS date,
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE mc.created_at >= NOW() - INTERVAL '30 days' AND f.deleted_at IS NULL AND f.user_id = $1
GROUP BY DATE(mc.created_at)
ORDER BY date DESC;
-- name: GetUsageStatsByDayLast3Months :many
SELECT
DATE(mc.created_at) AS date,
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE mc.created_at >= NOW() - INTERVAL '90 days' AND f.deleted_at IS NULL AND f.user_id = $1
GROUP BY DATE(mc.created_at)
ORDER BY date DESC;
-- name: GetUserTotalUsageStats :one
SELECT
COALESCE(SUM(mc.usage_in), 0)::bigint AS total_usage_in,
COALESCE(SUM(mc.usage_out), 0)::bigint AS total_usage_out,
COALESCE(SUM(mc.usage_cache_in), 0)::bigint AS total_usage_cache_in,
COALESCE(SUM(mc.usage_cache_out), 0)::bigint AS total_usage_cache_out,
COALESCE(SUM(mc.usage_cost_in), 0.0)::double precision AS total_usage_cost_in,
COALESCE(SUM(mc.usage_cost_out), 0.0)::double precision AS total_usage_cost_out
FROM msgchains mc
LEFT JOIN subtasks s ON mc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR mc.task_id = t.id
INNER JOIN flows f ON (mc.flow_id = f.id OR t.flow_id = f.id)
WHERE f.deleted_at IS NULL AND f.user_id = $1;
+71
View File
@@ -0,0 +1,71 @@
-- name: GetFlowMsgLogs :many
SELECT
ml.*
FROM msglogs ml
INNER JOIN flows f ON ml.flow_id = f.id
WHERE ml.flow_id = $1 AND f.deleted_at IS NULL
ORDER BY ml.created_at ASC;
-- name: GetUserFlowMsgLogs :many
SELECT
ml.*
FROM msglogs ml
INNER JOIN flows f ON ml.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE ml.flow_id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL
ORDER BY ml.created_at ASC;
-- name: GetTaskMsgLogs :many
SELECT
ml.*
FROM msglogs ml
INNER JOIN tasks t ON ml.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE ml.task_id = $1 AND f.deleted_at IS NULL
ORDER BY ml.created_at ASC;
-- name: GetSubtaskMsgLogs :many
SELECT
ml.*
FROM msglogs ml
INNER JOIN subtasks s ON ml.subtask_id = s.id
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE ml.subtask_id = $1 AND f.deleted_at IS NULL
ORDER BY ml.created_at ASC;
-- name: CreateMsgLog :one
INSERT INTO msglogs (
type,
message,
thinking,
flow_id,
task_id,
subtask_id
)
VALUES (
$1, $2, $3, $4, $5, $6
)
RETURNING *;
-- name: CreateResultMsgLog :one
INSERT INTO msglogs (
type,
message,
thinking,
result,
result_format,
flow_id,
task_id,
subtask_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)
RETURNING *;
-- name: UpdateMsgLogResult :one
UPDATE msglogs
SET result = $1, result_format = $2
WHERE id = $3
RETURNING *;
+64
View File
@@ -0,0 +1,64 @@
-- name: GetPrompts :many
SELECT
p.*
FROM prompts p
ORDER BY p.user_id ASC, p.type ASC;
-- name: GetUserPrompts :many
SELECT
p.*
FROM prompts p
INNER JOIN users u ON p.user_id = u.id
WHERE p.user_id = $1
ORDER BY p.type ASC;
-- name: GetUserPrompt :one
SELECT
p.*
FROM prompts p
INNER JOIN users u ON p.user_id = u.id
WHERE p.id = $1 AND p.user_id = $2;
-- name: GetUserPromptByType :one
SELECT
p.*
FROM prompts p
INNER JOIN users u ON p.user_id = u.id
WHERE p.type = $1 AND p.user_id = $2
LIMIT 1;
-- name: CreateUserPrompt :one
INSERT INTO prompts (
type,
user_id,
prompt
) VALUES (
$1, $2, $3
)
RETURNING *;
-- name: UpdatePrompt :one
UPDATE prompts
SET prompt = $1
WHERE id = $2
RETURNING *;
-- name: UpdateUserPrompt :one
UPDATE prompts
SET prompt = $1
WHERE id = $2 AND user_id = $3
RETURNING *;
-- name: UpdateUserPromptByType :one
UPDATE prompts
SET prompt = $1
WHERE type = $2 AND user_id = $3
RETURNING *;
-- name: DeletePrompt :exec
DELETE FROM prompts
WHERE id = $1;
-- name: DeleteUserPrompt :exec
DELETE FROM prompts
WHERE id = $1 AND user_id = $2;
+84
View File
@@ -0,0 +1,84 @@
-- name: GetProviders :many
SELECT
p.*
FROM providers p
WHERE p.deleted_at IS NULL
ORDER BY p.created_at ASC;
-- name: GetProvidersByType :many
SELECT
p.*
FROM providers p
WHERE p.type = $1 AND p.deleted_at IS NULL
ORDER BY p.created_at ASC;
-- name: GetProvider :one
SELECT
p.*
FROM providers p
WHERE p.id = $1 AND p.deleted_at IS NULL;
-- name: GetUserProvider :one
SELECT
p.*
FROM providers p
INNER JOIN users u ON p.user_id = u.id
WHERE p.id = $1 AND p.user_id = $2 AND p.deleted_at IS NULL;
-- name: GetUserProviders :many
SELECT
p.*
FROM providers p
INNER JOIN users u ON p.user_id = u.id
WHERE p.user_id = $1 AND p.deleted_at IS NULL
ORDER BY p.created_at ASC;
-- name: GetUserProvidersByType :many
SELECT
p.*
FROM providers p
INNER JOIN users u ON p.user_id = u.id
WHERE p.user_id = $1 AND p.type = $2 AND p.deleted_at IS NULL
ORDER BY p.created_at ASC;
-- name: GetUserProviderByName :one
SELECT
p.*
FROM providers p
INNER JOIN users u ON p.user_id = u.id
WHERE p.name = $1 AND p.user_id = $2 AND p.deleted_at IS NULL;
-- name: CreateProvider :one
INSERT INTO providers (
user_id,
type,
name,
config
) VALUES (
$1, $2, $3, $4
)
RETURNING *;
-- name: UpdateProvider :one
UPDATE providers
SET config = $2, name = $3
WHERE id = $1
RETURNING *;
-- name: UpdateUserProvider :one
UPDATE providers
SET config = $3, name = $4
WHERE id = $1 AND user_id = $2
RETURNING *;
-- name: DeleteProvider :one
UPDATE providers
SET deleted_at = CURRENT_TIMESTAMP
WHERE id = $1
RETURNING *;
-- name: DeleteUserProvider :one
UPDATE providers
SET deleted_at = CURRENT_TIMESTAMP
WHERE id = $1 AND user_id = $2
RETURNING *;
+61
View File
@@ -0,0 +1,61 @@
-- name: GetUserResourcesRoot :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE user_id = sqlc.arg(user_id) AND path NOT LIKE '%/%'
ORDER BY updated_at DESC, name ASC;
-- name: GetUserResourcesInDir :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE user_id = sqlc.arg(user_id)
AND ((path = sqlc.arg(dir_path) AND is_dir = true)
OR (path LIKE sqlc.arg(child_prefix) AND path NOT LIKE sqlc.arg(deep_prefix)))
ORDER BY updated_at DESC, name ASC;
-- name: GetUserResourcesRecursive :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE user_id = sqlc.arg(user_id)
AND (path = sqlc.arg(dir_path) OR path LIKE sqlc.arg(child_prefix))
ORDER BY updated_at DESC, name ASC;
-- name: GetUserResourcesAll :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE user_id = sqlc.arg(user_id)
ORDER BY updated_at DESC, name ASC;
-- name: GetAllResourcesRoot :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE path NOT LIKE '%/%'
ORDER BY updated_at DESC, name ASC;
-- name: GetAllResourcesInDir :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE (path = sqlc.arg(dir_path) AND is_dir = true)
OR (path LIKE sqlc.arg(child_prefix) AND path NOT LIKE sqlc.arg(deep_prefix))
ORDER BY updated_at DESC, name ASC;
-- name: GetAllResourcesRecursive :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE path = sqlc.arg(dir_path) OR path LIKE sqlc.arg(child_prefix)
ORDER BY updated_at DESC, name ASC;
-- name: GetAllResourcesAll :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
ORDER BY updated_at DESC, name ASC;
-- name: GetUserResourceByID :one
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE id = sqlc.arg(id);
-- name: GetUserResourcesByIDs :many
SELECT id, user_id, hash, name, path, size, is_dir, created_at, updated_at
FROM user_resources
WHERE id = ANY(sqlc.arg(ids)::bigint[])
ORDER BY id ASC;
+35
View File
@@ -0,0 +1,35 @@
-- name: GetRoles :many
SELECT
r.id,
r.name,
(
SELECT ARRAY_AGG(p.name)
FROM privileges p
WHERE p.role_id = r.id
) AS privileges
FROM roles r
ORDER BY r.id ASC;
-- name: GetRole :one
SELECT
r.id,
r.name,
(
SELECT ARRAY_AGG(p.name)
FROM privileges p
WHERE p.role_id = r.id
) AS privileges
FROM roles r
WHERE r.id = $1;
-- name: GetRoleByName :one
SELECT
r.id,
r.name,
(
SELECT ARRAY_AGG(p.name)
FROM privileges p
WHERE p.role_id = r.id
) AS privileges
FROM roles r
WHERE r.name = $1;
+53
View File
@@ -0,0 +1,53 @@
-- name: GetFlowScreenshots :many
SELECT
s.*
FROM screenshots s
INNER JOIN flows f ON s.flow_id = f.id
WHERE s.flow_id = $1 AND f.deleted_at IS NULL
ORDER BY s.created_at DESC;
-- name: GetUserFlowScreenshots :many
SELECT
s.*
FROM screenshots s
INNER JOIN flows f ON s.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE s.flow_id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL
ORDER BY s.created_at DESC;
-- name: GetTaskScreenshots :many
SELECT
s.*
FROM screenshots s
INNER JOIN flows f ON s.flow_id = f.id
INNER JOIN tasks t ON s.task_id = t.id
WHERE s.task_id = $1 AND f.deleted_at IS NULL
ORDER BY s.created_at DESC;
-- name: GetSubtaskScreenshots :many
SELECT
s.*
FROM screenshots s
INNER JOIN flows f ON s.flow_id = f.id
INNER JOIN subtasks st ON s.subtask_id = st.id
WHERE s.subtask_id = $1 AND f.deleted_at IS NULL
ORDER BY s.created_at DESC;
-- name: GetScreenshot :one
SELECT
s.*
FROM screenshots s
WHERE s.id = $1;
-- name: CreateScreenshot :one
INSERT INTO screenshots (
name,
url,
flow_id,
task_id,
subtask_id
)
VALUES (
$1, $2, $3, $4, $5
)
RETURNING *;
+57
View File
@@ -0,0 +1,57 @@
-- name: GetFlowSearchLogs :many
SELECT
sl.*
FROM searchlogs sl
INNER JOIN flows f ON sl.flow_id = f.id
WHERE sl.flow_id = $1 AND f.deleted_at IS NULL
ORDER BY sl.created_at ASC;
-- name: GetUserFlowSearchLogs :many
SELECT
sl.*
FROM searchlogs sl
INNER JOIN flows f ON sl.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE sl.flow_id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL
ORDER BY sl.created_at ASC;
-- name: GetTaskSearchLogs :many
SELECT
sl.*
FROM searchlogs sl
INNER JOIN flows f ON sl.flow_id = f.id
INNER JOIN tasks t ON sl.task_id = t.id
WHERE sl.task_id = $1 AND f.deleted_at IS NULL
ORDER BY sl.created_at ASC;
-- name: GetSubtaskSearchLogs :many
SELECT
sl.*
FROM searchlogs sl
INNER JOIN flows f ON sl.flow_id = f.id
INNER JOIN subtasks s ON sl.subtask_id = s.id
WHERE sl.subtask_id = $1 AND f.deleted_at IS NULL
ORDER BY sl.created_at ASC;
-- name: GetFlowSearchLog :one
SELECT
sl.*
FROM searchlogs sl
INNER JOIN flows f ON sl.flow_id = f.id
WHERE sl.id = $1 AND sl.flow_id = $2 AND f.deleted_at IS NULL;
-- name: CreateSearchLog :one
INSERT INTO searchlogs (
initiator,
executor,
engine,
query,
result,
flow_id,
task_id,
subtask_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8
)
RETURNING *;
+127
View File
@@ -0,0 +1,127 @@
-- name: GetFlowSubtasks :many
SELECT
s.*
FROM subtasks s
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE t.flow_id = $1 AND f.deleted_at IS NULL
ORDER BY s.created_at ASC;
-- name: GetFlowTaskSubtasks :many
SELECT
s.*
FROM subtasks s
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE s.task_id = $1 AND t.flow_id = $2 AND f.deleted_at IS NULL
ORDER BY s.created_at ASC;
-- name: GetUserFlowSubtasks :many
SELECT
s.*
FROM subtasks s
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE t.flow_id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL
ORDER BY s.created_at ASC;
-- name: GetUserFlowTaskSubtasks :many
SELECT
s.*
FROM subtasks s
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE s.task_id = $1 AND t.flow_id = $2 AND f.user_id = $3 AND f.deleted_at IS NULL
ORDER BY s.created_at ASC;
-- name: GetTaskSubtasks :many
SELECT
s.*
FROM subtasks s
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE s.task_id = $1 AND f.deleted_at IS NULL
ORDER BY s.created_at DESC;
-- name: GetTaskPlannedSubtasks :many
SELECT
s.*
FROM subtasks s
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE s.task_id = $1 AND (s.status = 'created' OR s.status = 'waiting') AND f.deleted_at IS NULL
ORDER BY s.id ASC;
-- name: GetTaskCompletedSubtasks :many
SELECT
s.*
FROM subtasks s
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE s.task_id = $1 AND (s.status != 'created' AND s.status != 'waiting') AND f.deleted_at IS NULL
ORDER BY s.id ASC;
-- name: GetSubtask :one
SELECT
s.*
FROM subtasks s
WHERE s.id = $1;
-- name: GetFlowSubtask :one
SELECT
s.*
FROM subtasks s
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE s.id = $1 AND t.flow_id = $2 AND f.deleted_at IS NULL;
-- name: CreateSubtask :one
INSERT INTO subtasks (
status,
title,
description,
task_id
) VALUES (
$1, $2, $3, $4
)
RETURNING *;
-- name: UpdateSubtaskStatus :one
UPDATE subtasks
SET status = $1
WHERE id = $2
RETURNING *;
-- name: UpdateSubtaskResult :one
UPDATE subtasks
SET result = $1
WHERE id = $2
RETURNING *;
-- name: UpdateSubtaskFinishedResult :one
UPDATE subtasks
SET status = 'finished', result = $1
WHERE id = $2
RETURNING *;
-- name: UpdateSubtaskFailedResult :one
UPDATE subtasks
SET status = 'failed', result = $1
WHERE id = $2
RETURNING *;
-- name: UpdateSubtaskContext :one
UPDATE subtasks
SET context = $1
WHERE id = $2
RETURNING *;
-- name: DeleteSubtask :exec
DELETE FROM subtasks
WHERE id = $1;
-- name: DeleteSubtasks :exec
DELETE FROM subtasks
WHERE id = ANY(@ids::BIGINT[]);
+72
View File
@@ -0,0 +1,72 @@
-- name: GetFlowTasks :many
SELECT
t.*
FROM tasks t
INNER JOIN flows f ON t.flow_id = f.id
WHERE t.flow_id = $1 AND f.deleted_at IS NULL
ORDER BY t.created_at ASC;
-- name: GetUserFlowTasks :many
SELECT
t.*
FROM tasks t
INNER JOIN flows f ON t.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE t.flow_id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL
ORDER BY t.created_at ASC;
-- name: GetFlowTask :one
SELECT
t.*
FROM tasks t
INNER JOIN flows f ON t.flow_id = f.id
WHERE t.id = $1 AND t.flow_id = $2 AND f.deleted_at IS NULL;
-- name: GetUserFlowTask :one
SELECT
t.*
FROM tasks t
INNER JOIN flows f ON t.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE t.id = $1 AND t.flow_id = $2 AND f.user_id = $3 AND f.deleted_at IS NULL;
-- name: GetTask :one
SELECT
t.*
FROM tasks t
WHERE t.id = $1;
-- name: CreateTask :one
INSERT INTO tasks (
status,
title,
input,
flow_id
) VALUES (
$1, $2, $3, $4
)
RETURNING *;
-- name: UpdateTaskStatus :one
UPDATE tasks
SET status = $1
WHERE id = $2
RETURNING *;
-- name: UpdateTaskResult :one
UPDATE tasks
SET result = $1
WHERE id = $2
RETURNING *;
-- name: UpdateTaskFinishedResult :one
UPDATE tasks
SET status = 'finished', result = $1
WHERE id = $2
RETURNING *;
-- name: UpdateTaskFailedResult :one
UPDATE tasks
SET status = 'failed', result = $1
WHERE id = $2
RETURNING *;
+60
View File
@@ -0,0 +1,60 @@
-- name: GetFlowTermLogs :many
SELECT
tl.*
FROM termlogs tl
INNER JOIN flows f ON tl.flow_id = f.id
WHERE tl.flow_id = $1 AND f.deleted_at IS NULL
ORDER BY tl.created_at ASC;
-- name: GetUserFlowTermLogs :many
SELECT
tl.*
FROM termlogs tl
INNER JOIN flows f ON tl.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE tl.flow_id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL
ORDER BY tl.created_at ASC;
-- name: GetTaskTermLogs :many
SELECT
tl.*
FROM termlogs tl
INNER JOIN flows f ON tl.flow_id = f.id
WHERE tl.task_id = $1 AND f.deleted_at IS NULL
ORDER BY tl.created_at ASC;
-- name: GetSubtaskTermLogs :many
SELECT
tl.*
FROM termlogs tl
INNER JOIN flows f ON tl.flow_id = f.id
WHERE tl.subtask_id = $1 AND f.deleted_at IS NULL
ORDER BY tl.created_at ASC;
-- name: GetContainerTermLogs :many
SELECT
tl.*
FROM termlogs tl
INNER JOIN flows f ON tl.flow_id = f.id
WHERE tl.container_id = $1 AND f.deleted_at IS NULL
ORDER BY tl.created_at ASC;
-- name: GetTermLog :one
SELECT
tl.*
FROM termlogs tl
WHERE tl.id = $1;
-- name: CreateTermLog :one
INSERT INTO termlogs (
type,
text,
container_id,
flow_id,
task_id,
subtask_id
)
VALUES (
$1, $2, $3, $4, $5, $6
)
RETURNING *;
+207
View File
@@ -0,0 +1,207 @@
-- name: GetFlowToolcalls :many
SELECT
tc.*
FROM toolcalls tc
INNER JOIN flows f ON tc.flow_id = f.id
WHERE tc.flow_id = $1 AND f.deleted_at IS NULL
ORDER BY tc.created_at ASC;
-- name: GetFlowToolcall :one
SELECT
tc.*
FROM toolcalls tc
INNER JOIN flows f ON tc.flow_id = f.id
WHERE tc.id = $1 AND tc.flow_id = $2 AND f.deleted_at IS NULL;
-- name: GetSubtaskToolcalls :many
SELECT
tc.*
FROM toolcalls tc
INNER JOIN subtasks s ON tc.subtask_id = s.id
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE tc.subtask_id = $1 AND f.deleted_at IS NULL
ORDER BY tc.created_at DESC;
-- name: GetCallToolcall :one
SELECT
tc.*
FROM toolcalls tc
WHERE tc.call_id = $1;
-- name: CreateToolcall :one
INSERT INTO toolcalls (
call_id,
status,
name,
args,
flow_id,
task_id,
subtask_id
) VALUES (
$1, $2, $3, $4, $5, $6, $7
)
RETURNING *;
-- name: UpdateToolcallStatus :one
UPDATE toolcalls
SET
status = $1,
duration_seconds = duration_seconds + $2
WHERE id = $3
RETURNING *;
-- name: UpdateToolcallFinishedResult :one
UPDATE toolcalls
SET
status = 'finished',
result = $1,
duration_seconds = duration_seconds + $2
WHERE id = $3
RETURNING *;
-- name: UpdateToolcallFailedResult :one
UPDATE toolcalls
SET
status = 'failed',
result = $1,
duration_seconds = duration_seconds + $2
WHERE id = $3
RETURNING *;
-- ==================== Toolcalls Analytics Queries ====================
-- name: GetFlowToolcallsStats :one
-- Get total execution time and count of toolcalls for a specific flow
SELECT
COALESCE(COUNT(CASE WHEN tc.status IN ('finished', 'failed') THEN 1 END), 0)::bigint AS total_count,
COALESCE(SUM(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE 0 END), 0.0)::double precision AS total_duration_seconds
FROM toolcalls tc
LEFT JOIN tasks t ON tc.task_id = t.id
LEFT JOIN subtasks s ON tc.subtask_id = s.id
INNER JOIN flows f ON tc.flow_id = f.id
WHERE tc.flow_id = $1 AND f.deleted_at IS NULL
AND (tc.task_id IS NULL OR t.id IS NOT NULL)
AND (tc.subtask_id IS NULL OR s.id IS NOT NULL);
-- name: GetTaskToolcallsStats :one
-- Get total execution time and count of toolcalls for a specific task
SELECT
COALESCE(COUNT(CASE WHEN tc.status IN ('finished', 'failed') THEN 1 END), 0)::bigint AS total_count,
COALESCE(SUM(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE 0 END), 0.0)::double precision AS total_duration_seconds
FROM toolcalls tc
LEFT JOIN subtasks s ON tc.subtask_id = s.id
INNER JOIN tasks t ON tc.task_id = t.id OR s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE (tc.task_id = $1 OR s.task_id = $1) AND f.deleted_at IS NULL
AND (tc.subtask_id IS NULL OR s.id IS NOT NULL);
-- name: GetSubtaskToolcallsStats :one
-- Get total execution time and count of toolcalls for a specific subtask
SELECT
COALESCE(COUNT(CASE WHEN tc.status IN ('finished', 'failed') THEN 1 END), 0)::bigint AS total_count,
COALESCE(SUM(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE 0 END), 0.0)::double precision AS total_duration_seconds
FROM toolcalls tc
INNER JOIN subtasks s ON tc.subtask_id = s.id
INNER JOIN tasks t ON s.task_id = t.id
INNER JOIN flows f ON t.flow_id = f.id
WHERE tc.subtask_id = $1 AND f.deleted_at IS NULL AND s.id IS NOT NULL AND t.id IS NOT NULL;
-- name: GetAllFlowsToolcallsStats :many
-- Get toolcalls stats for all flows
SELECT
COALESCE(tc.flow_id, t.flow_id) AS flow_id,
COALESCE(COUNT(CASE WHEN tc.status IN ('finished', 'failed') THEN 1 END), 0)::bigint AS total_count,
COALESCE(SUM(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE 0 END), 0.0)::double precision AS total_duration_seconds
FROM toolcalls tc
LEFT JOIN subtasks s ON tc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR tc.task_id = t.id
INNER JOIN flows f ON (tc.flow_id = f.id OR t.flow_id = f.id)
WHERE f.deleted_at IS NULL
GROUP BY COALESCE(tc.flow_id, t.flow_id)
ORDER BY COALESCE(tc.flow_id, t.flow_id);
-- name: GetToolcallsStatsByFunction :many
-- Get toolcalls stats grouped by function name for a user
SELECT
tc.name AS function_name,
COALESCE(COUNT(CASE WHEN tc.status IN ('finished', 'failed') THEN 1 END), 0)::bigint AS total_count,
COALESCE(SUM(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE 0 END), 0.0)::double precision AS total_duration_seconds,
COALESCE(AVG(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE NULL END), 0.0)::double precision AS avg_duration_seconds
FROM toolcalls tc
LEFT JOIN subtasks s ON tc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR tc.task_id = t.id
INNER JOIN flows f ON (tc.flow_id = f.id OR t.flow_id = f.id)
WHERE f.deleted_at IS NULL AND f.user_id = $1
GROUP BY tc.name
ORDER BY total_duration_seconds DESC;
-- name: GetToolcallsStatsByFunctionForFlow :many
-- Get toolcalls stats grouped by function name for a specific flow
SELECT
tc.name AS function_name,
COALESCE(COUNT(CASE WHEN tc.status IN ('finished', 'failed') THEN 1 END), 0)::bigint AS total_count,
COALESCE(SUM(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE 0 END), 0.0)::double precision AS total_duration_seconds,
COALESCE(AVG(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE NULL END), 0.0)::double precision AS avg_duration_seconds
FROM toolcalls tc
LEFT JOIN subtasks s ON tc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR tc.task_id = t.id
INNER JOIN flows f ON (tc.flow_id = f.id OR t.flow_id = f.id)
WHERE (tc.flow_id = $1 OR t.flow_id = $1) AND f.deleted_at IS NULL
GROUP BY tc.name
ORDER BY total_duration_seconds DESC;
-- name: GetToolcallsStatsByDayLastWeek :many
-- Get toolcalls stats by day for the last week
SELECT
DATE(tc.created_at) AS date,
COALESCE(COUNT(CASE WHEN tc.status IN ('finished', 'failed') THEN 1 END), 0)::bigint AS total_count,
COALESCE(SUM(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE 0 END), 0.0)::double precision AS total_duration_seconds
FROM toolcalls tc
LEFT JOIN subtasks s ON tc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR tc.task_id = t.id
INNER JOIN flows f ON (tc.flow_id = f.id OR t.flow_id = f.id)
WHERE tc.created_at >= NOW() - INTERVAL '7 days' AND f.deleted_at IS NULL AND f.user_id = $1
GROUP BY DATE(tc.created_at)
ORDER BY date DESC;
-- name: GetToolcallsStatsByDayLastMonth :many
-- Get toolcalls stats by day for the last month
SELECT
DATE(tc.created_at) AS date,
COALESCE(COUNT(CASE WHEN tc.status IN ('finished', 'failed') THEN 1 END), 0)::bigint AS total_count,
COALESCE(SUM(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE 0 END), 0.0)::double precision AS total_duration_seconds
FROM toolcalls tc
LEFT JOIN subtasks s ON tc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR tc.task_id = t.id
INNER JOIN flows f ON (tc.flow_id = f.id OR t.flow_id = f.id)
WHERE tc.created_at >= NOW() - INTERVAL '30 days' AND f.deleted_at IS NULL AND f.user_id = $1
GROUP BY DATE(tc.created_at)
ORDER BY date DESC;
-- name: GetToolcallsStatsByDayLast3Months :many
-- Get toolcalls stats by day for the last 3 months
SELECT
DATE(tc.created_at) AS date,
COALESCE(COUNT(CASE WHEN tc.status IN ('finished', 'failed') THEN 1 END), 0)::bigint AS total_count,
COALESCE(SUM(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE 0 END), 0.0)::double precision AS total_duration_seconds
FROM toolcalls tc
LEFT JOIN subtasks s ON tc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR tc.task_id = t.id
INNER JOIN flows f ON (tc.flow_id = f.id OR t.flow_id = f.id)
WHERE tc.created_at >= NOW() - INTERVAL '90 days' AND f.deleted_at IS NULL AND f.user_id = $1
GROUP BY DATE(tc.created_at)
ORDER BY date DESC;
-- name: GetUserTotalToolcallsStats :one
-- Get total toolcalls stats for a user
SELECT
COALESCE(COUNT(CASE WHEN tc.status IN ('finished', 'failed') THEN 1 END), 0)::bigint AS total_count,
COALESCE(SUM(CASE WHEN tc.status IN ('finished', 'failed') THEN tc.duration_seconds ELSE 0 END), 0.0)::double precision AS total_duration_seconds
FROM toolcalls tc
LEFT JOIN subtasks s ON tc.subtask_id = s.id
LEFT JOIN tasks t ON s.task_id = t.id OR tc.task_id = t.id
INNER JOIN flows f ON (tc.flow_id = f.id OR t.flow_id = f.id)
WHERE f.deleted_at IS NULL AND f.user_id = $1
AND (tc.task_id IS NULL OR t.id IS NOT NULL)
AND (tc.subtask_id IS NULL OR s.id IS NOT NULL);
+68
View File
@@ -0,0 +1,68 @@
-- name: GetUserPreferencesByUserID :one
SELECT * FROM user_preferences
WHERE user_id = $1 LIMIT 1;
-- name: CreateUserPreferences :one
INSERT INTO user_preferences (
user_id,
preferences
) VALUES (
$1,
$2
)
RETURNING *;
-- name: UpdateUserPreferences :one
UPDATE user_preferences
SET preferences = $2
WHERE user_id = $1
RETURNING *;
-- name: DeleteUserPreferences :exec
DELETE FROM user_preferences
WHERE user_id = $1;
-- name: UpsertUserPreferences :one
INSERT INTO user_preferences (
user_id,
preferences
) VALUES (
$1,
$2
)
ON CONFLICT (user_id) DO UPDATE
SET preferences = EXCLUDED.preferences
RETURNING *;
-- name: AddFavoriteFlow :one
INSERT INTO user_preferences (user_id, preferences)
VALUES (
sqlc.arg(user_id)::bigint,
jsonb_build_object('favoriteFlows', jsonb_build_array(sqlc.arg(flow_id)::bigint))
)
ON CONFLICT (user_id) DO UPDATE
SET preferences = jsonb_set(
user_preferences.preferences,
'{favoriteFlows}',
CASE
WHEN user_preferences.preferences->'favoriteFlows' @> to_jsonb(sqlc.arg(flow_id)::bigint) THEN
user_preferences.preferences->'favoriteFlows'
ELSE
user_preferences.preferences->'favoriteFlows' || to_jsonb(sqlc.arg(flow_id)::bigint)
END
)
RETURNING *;
-- name: DeleteFavoriteFlow :one
UPDATE user_preferences
SET preferences = jsonb_set(
preferences,
'{favoriteFlows}',
(
SELECT COALESCE(jsonb_agg(elem), '[]'::jsonb)
FROM jsonb_array_elements(preferences->'favoriteFlows') elem
WHERE elem::text::bigint != sqlc.arg(flow_id)::bigint
)
)
WHERE user_id = sqlc.arg(user_id)::bigint
RETURNING *;
+87
View File
@@ -0,0 +1,87 @@
-- name: GetUsers :many
SELECT
u.*,
r.name AS role_name,
(
SELECT ARRAY_AGG(p.name)
FROM privileges p
WHERE p.role_id = r.id
) AS privileges
FROM users u
INNER JOIN roles r ON u.role_id = r.id
ORDER BY u.created_at DESC;
-- name: GetUser :one
SELECT
u.*,
r.name AS role_name,
(
SELECT ARRAY_AGG(p.name)
FROM privileges p
WHERE p.role_id = r.id
) AS privileges
FROM users u
INNER JOIN roles r ON u.role_id = r.id
WHERE u.id = $1;
-- name: GetUserByHash :one
SELECT
u.*,
r.name AS role_name,
(
SELECT ARRAY_AGG(p.name)
FROM privileges p
WHERE p.role_id = r.id
) AS privileges
FROM users u
INNER JOIN roles r ON u.role_id = r.id
WHERE u.hash = $1;
-- name: CreateUser :one
INSERT INTO users (
type,
mail,
name,
password,
status,
role_id,
password_change_required
)
VALUES (
$1, $2, $3, $4, $5, $6, $7
)
RETURNING *;
-- name: UpdateUserStatus :one
UPDATE users
SET status = $1
WHERE id = $2
RETURNING *;
-- name: UpdateUserName :one
UPDATE users
SET name = $1
WHERE id = $2
RETURNING *;
-- name: UpdateUserPassword :one
UPDATE users
SET password = $1
WHERE id = $2
RETURNING *;
-- name: UpdateUserPasswordChangeRequired :one
UPDATE users
SET password_change_required = $1
WHERE id = $2
RETURNING *;
-- name: UpdateUserRole :one
UPDATE users
SET role_id = $1
WHERE id = $2
RETURNING *;
-- name: DeleteUser :exec
DELETE FROM users
WHERE id = $1;
+58
View File
@@ -0,0 +1,58 @@
-- name: GetFlowVectorStoreLogs :many
SELECT
vl.*
FROM vecstorelogs vl
INNER JOIN flows f ON vl.flow_id = f.id
WHERE vl.flow_id = $1 AND f.deleted_at IS NULL
ORDER BY vl.created_at ASC;
-- name: GetUserFlowVectorStoreLogs :many
SELECT
vl.*
FROM vecstorelogs vl
INNER JOIN flows f ON vl.flow_id = f.id
INNER JOIN users u ON f.user_id = u.id
WHERE vl.flow_id = $1 AND f.user_id = $2 AND f.deleted_at IS NULL
ORDER BY vl.created_at ASC;
-- name: GetTaskVectorStoreLogs :many
SELECT
vl.*
FROM vecstorelogs vl
INNER JOIN flows f ON vl.flow_id = f.id
INNER JOIN tasks t ON vl.task_id = t.id
WHERE vl.task_id = $1 AND f.deleted_at IS NULL
ORDER BY vl.created_at ASC;
-- name: GetSubtaskVectorStoreLogs :many
SELECT
vl.*
FROM vecstorelogs vl
INNER JOIN flows f ON vl.flow_id = f.id
INNER JOIN subtasks s ON vl.subtask_id = s.id
WHERE vl.subtask_id = $1 AND f.deleted_at IS NULL
ORDER BY vl.created_at ASC;
-- name: GetFlowVectorStoreLog :one
SELECT
vl.*
FROM vecstorelogs vl
INNER JOIN flows f ON vl.flow_id = f.id
WHERE vl.id = $1 AND vl.flow_id = $2 AND f.deleted_at IS NULL;
-- name: CreateVectorStoreLog :one
INSERT INTO vecstorelogs (
initiator,
executor,
filter,
query,
action,
result,
flow_id,
task_id,
subtask_id
)
VALUES (
$1, $2, $3, $4, $5, $6, $7, $8, $9
)
RETURNING *;
+37
View File
@@ -0,0 +1,37 @@
# SQLC configuration for PentAGI database layer code generation
# Generates type-safe Go code from SQL queries and PostgreSQL schema
version: "2"
cloud:
sql:
- engine: "postgresql"
queries: ["models/*.sql"]
schema: ["../migrations/sql/*.sql"]
# Go code generation settings
gen:
go:
package: "database"
out: "../pkg/database"
sql_package: "database/sql"
emit_interface: true # Generate Querier interface for mocking
emit_json_tags: true # Add JSON tags to generated structs
# PostgreSQL to Go type mappings
overrides:
- db_type: "pg_catalog.numeric"
go_type: "float64"
- db_type: "bigint"
go_type: "int64"
# pgvector extension type — never selected in queries, but sqlc needs a mapping
# to generate the LangchainPgEmbedding model struct in models.go.
- db_type: "pg_catalog.vector"
go_type: "string"
nullable: true
- db_type: "vector"
go_type: "string"
nullable: true
# Database connection for sqlc analysis
database:
uri: ${DATABASE_URL}