Files
libredesk/internal/ai/queries.sql
T
Abhinav Raut 1fc606b820 fix help center issues and reuse global i18n keys
Media:
- media rows now take the private flag from the caller instead of
  guessing it from the model type
- agent avatars upload as public so help center readers can load them,
  contact and visitor avatars stay private
- backfill existing agent avatars to public

Public pages:
- dedupe article feedback by reader IP for 24 hours
- keep the article locale when submitting feedback
- skip view count bumps from crawlers on the JSON API
- only log a search when the search itself worked

Admin and data:
- lock the help center row while creating an article, since slugs are
  unique per help center but the DB index is per collection
- block changing a collection language when it still has articles or
  sub collections
- validate article locale on every update, not only when the collection
  changes
- reject slugs with characters that break /hc/ URLs
- exclude the whole subtree from the parent picker so a collection
  cannot be moved into itself
- drop articles of a paused help center from the AI index
- take the embed generation at enqueue time so jobs commit in edit order

Editor and UI:
- show an error when a YouTube URL is not valid
- do not break the icon picker when the sprite fails to load
- ignore a slow article fetch after another article is opened

i18n: move generic strings like Published, Archived, Slug, Icon, Author,
Language, Text color and Platform to globals, and drop hint text that
just repeated the field label.
2026-08-01 22:34:12 +05:30

124 lines
5.1 KiB
SQL

-- name: get-provider-by-type
SELECT id, created_at, updated_at, name, provider, type, config, is_default FROM ai_providers WHERE type = $1;
-- name: update-provider-config
UPDATE ai_providers SET config = $2, updated_at = now() WHERE type = $1;
-- name: get-prompt
SELECT id, created_at, updated_at, key, title, content FROM ai_prompts WHERE key = $1;
-- name: get-prompts
SELECT id, created_at, updated_at, key, title FROM ai_prompts ORDER BY title;
-- name: get-knowledge-base-items
SELECT id, created_at, updated_at, type, title, content, enabled, source, source_url, embedded_fingerprint FROM ai_knowledge_base ORDER BY updated_at DESC;
-- name: get-knowledge-base-item
SELECT id, created_at, updated_at, type, title, content, enabled, source, source_url, embedded_fingerprint FROM ai_knowledge_base WHERE id = $1;
-- name: knowledge-base-item-exists
SELECT EXISTS(SELECT 1 FROM ai_knowledge_base WHERE id = $1);
-- name: insert-knowledge-base-item
INSERT INTO ai_knowledge_base (type, title, content, enabled, source, source_url) VALUES ($1, $2, $3, $4, $5, $6) RETURNING *;
-- name: update-knowledge-base-item
UPDATE ai_knowledge_base SET title = $2, content = $3, enabled = $4, updated_at = now() WHERE id = $1 RETURNING *;
-- name: delete-knowledge-base-item
DELETE FROM ai_knowledge_base WHERE id = $1;
-- name: set-knowledge-base-embedded-fingerprint
UPDATE ai_knowledge_base SET embedded_fingerprint = $2 WHERE id = $1;
-- name: get-embeddable-help-articles
WITH RECURSIVE published_collections AS (
SELECT c.id FROM article_collections c
JOIN help_centers hc ON hc.id = c.help_center_id AND hc.is_active
WHERE c.parent_id IS NULL AND c.is_published = true
UNION
SELECT c.id FROM article_collections c JOIN published_collections p ON c.parent_id = p.id
WHERE c.is_published = true
)
SELECT a.id, a.title, a.content, a.status, a.ai_enabled, a.embedded_fingerprint,
a.collection_id IN (SELECT id FROM published_collections) AS is_reachable
FROM help_articles a
WHERE (a.status = 'published' AND a.ai_enabled) OR a.embedded_fingerprint <> '';
-- name: get-embeddable-help-article
WITH RECURSIVE published_collections AS (
SELECT c.id FROM article_collections c
JOIN help_centers hc ON hc.id = c.help_center_id AND hc.is_active
WHERE c.parent_id IS NULL AND c.is_published = true
UNION
SELECT c.id FROM article_collections c JOIN published_collections p ON c.parent_id = p.id
WHERE c.is_published = true
)
SELECT a.id, a.title, a.content, a.status, a.ai_enabled, a.embedded_fingerprint,
a.collection_id IN (SELECT id FROM published_collections) AS is_reachable
FROM help_articles a
WHERE a.id = $1;
-- name: help-article-exists
SELECT EXISTS(SELECT 1 FROM help_articles WHERE id = $1);
-- name: set-help-article-embedded-fingerprint
UPDATE help_articles SET embedded_fingerprint = $2 WHERE id = $1;
-- name: delete-orphan-help-article-embeddings
DELETE FROM embeddings e
WHERE e.source_type = 'help_article' AND NOT EXISTS (SELECT 1 FROM help_articles a WHERE a.id = e.source_id)
RETURNING e.source_id;
-- name: insert-embedding
INSERT INTO embeddings (source_type, source_id, chunk_text, embedding, dimensions) VALUES ($1, $2, $3, $4, $5);
-- name: delete-embeddings-by-source
DELETE FROM embeddings WHERE source_type = $1 AND source_id = $2;
-- name: delete-embeddings-by-source-ids
DELETE FROM embeddings WHERE source_type = $1 AND source_id = ANY($2);
-- name: delete-embeddings-by-source-type
DELETE FROM embeddings WHERE source_type = $1;
-- name: get-tags
SELECT id, name FROM tags ORDER BY id;
-- name: get-all-embeddings
SELECT id, source_type, source_id, chunk_text, embedding, dimensions FROM embeddings;
-- name: get-tools
SELECT id, created_at, updated_at, name, description, url, method, auth, parameters, enabled, requires_verification FROM ai_tools ORDER BY updated_at DESC;
-- name: get-enabled-tools-by-ids
SELECT id, created_at, updated_at, name, description, url, method, auth, parameters, enabled, requires_verification FROM ai_tools WHERE enabled = true AND id = ANY($1);
-- name: get-tool
SELECT id, created_at, updated_at, name, description, url, method, auth, parameters, enabled, requires_verification FROM ai_tools WHERE id = $1;
-- name: get-tool-auth
SELECT auth FROM ai_tools WHERE id = $1;
-- name: insert-tool
INSERT INTO ai_tools (name, description, url, method, auth, parameters, enabled, requires_verification)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;
-- name: update-tool
UPDATE ai_tools SET name = $2, description = $3, url = $4, method = $5, auth = $6, parameters = $7, enabled = $8, requires_verification = $9, updated_at = now()
WHERE id = $1 RETURNING *;
-- name: delete-tool
DELETE FROM ai_tools WHERE id = $1;
-- name: get-copilot-messages
SELECT role, content FROM (
SELECT id, role, content FROM copilot_messages WHERE conversation_id = $1 AND user_id = $2 ORDER BY id DESC LIMIT $3
) latest ORDER BY id;
-- name: insert-copilot-message
INSERT INTO copilot_messages (conversation_id, user_id, role, content) VALUES ($1, $2, $3, $4);
-- name: delete-copilot-messages
DELETE FROM copilot_messages WHERE conversation_id = $1 AND user_id = $2;