mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-12-05 12:49:46 +08:00
* feat(kb) agent integration * feat: add knowledge base model settings and WebP imports * feat(kb): add embedded image learning and harden indexing workflows * feat: add knowledge base concurrency settings * feat: add select all files action to knowledge base * fix: retry vector learning failures and improve model error diagnostics
1823 lines
52 KiB
Go
1823 lines
52 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: query.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
)
|
|
|
|
const advanceKbLearningJobRevision = `-- name: AdvanceKbLearningJobRevision :exec
|
|
UPDATE knowledge_base_learning_jobs j
|
|
SET document_revision = d.revision,
|
|
updated_at = now()
|
|
FROM knowledge_base_documents d
|
|
WHERE j.id = $1
|
|
AND j.document_id = d.id
|
|
AND j.document_revision IS NOT NULL
|
|
AND j.status = 'running'
|
|
AND j.attempts = $2
|
|
AND j.locked_by = $3
|
|
`
|
|
|
|
type AdvanceKbLearningJobRevisionParams struct {
|
|
ID string
|
|
Attempts int32
|
|
WorkerID *string
|
|
}
|
|
|
|
// Preserve the revision guard across this attempt's own vector-failure write.
|
|
// External edits still invalidate/cancel the job through the normal write path.
|
|
func (q *Queries) AdvanceKbLearningJobRevision(ctx context.Context, arg AdvanceKbLearningJobRevisionParams) error {
|
|
_, err := q.db.Exec(ctx, advanceKbLearningJobRevision, arg.ID, arg.Attempts, arg.WorkerID)
|
|
return err
|
|
}
|
|
|
|
const avatarReferenced = `-- name: AvatarReferenced :one
|
|
SELECT EXISTS (SELECT 1 FROM knowledge_bases WHERE avatar_s3_key = $1)
|
|
`
|
|
|
|
func (q *Queries) AvatarReferenced(ctx context.Context, avatarS3Key string) (bool, error) {
|
|
row := q.db.QueryRow(ctx, avatarReferenced, avatarS3Key)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const bumpKbDocumentRevision = `-- name: BumpKbDocumentRevision :exec
|
|
UPDATE knowledge_base_documents SET revision = revision + 1 WHERE id = $1 AND revision = $2
|
|
`
|
|
|
|
type BumpKbDocumentRevisionParams struct {
|
|
ID string
|
|
Revision int32
|
|
}
|
|
|
|
func (q *Queries) BumpKbDocumentRevision(ctx context.Context, arg BumpKbDocumentRevisionParams) error {
|
|
_, err := q.db.Exec(ctx, bumpKbDocumentRevision, arg.ID, arg.Revision)
|
|
return err
|
|
}
|
|
|
|
const cancelKbLearningJobs = `-- name: CancelKbLearningJobs :exec
|
|
UPDATE knowledge_base_learning_jobs
|
|
SET status = 'canceled', updated_at = now(), finished_at = now()
|
|
WHERE kb_id = $1
|
|
AND status IN ('pending', 'running')
|
|
`
|
|
|
|
func (q *Queries) CancelKbLearningJobs(ctx context.Context, kbID string) error {
|
|
_, err := q.db.Exec(ctx, cancelKbLearningJobs, kbID)
|
|
return err
|
|
}
|
|
|
|
const cancelKbLearningJobsByDocument = `-- name: CancelKbLearningJobsByDocument :exec
|
|
UPDATE knowledge_base_learning_jobs
|
|
SET status = 'canceled', updated_at = now(), finished_at = now()
|
|
WHERE document_id = $1
|
|
AND status IN ('pending', 'running')
|
|
`
|
|
|
|
func (q *Queries) CancelKbLearningJobsByDocument(ctx context.Context, documentID *string) error {
|
|
_, err := q.db.Exec(ctx, cancelKbLearningJobsByDocument, documentID)
|
|
return err
|
|
}
|
|
|
|
const claimKbLearningJob = `-- name: ClaimKbLearningJob :one
|
|
WITH next_job AS (
|
|
SELECT id
|
|
FROM knowledge_base_learning_jobs
|
|
WHERE (status = 'pending' AND available_at <= now())
|
|
OR (status = 'running' AND locked_at < now() - interval '5 minutes')
|
|
ORDER BY created_at, id
|
|
FOR UPDATE SKIP LOCKED
|
|
LIMIT 1
|
|
)
|
|
UPDATE knowledge_base_learning_jobs j
|
|
SET status = 'running',
|
|
attempts = attempts + 1,
|
|
locked_at = now(),
|
|
locked_by = $1,
|
|
updated_at = now()
|
|
FROM next_job
|
|
WHERE j.id = next_job.id
|
|
RETURNING j.id, j.kb_id, j.document_id, j.operation, j.attempts, j.max_attempts,
|
|
j.document_revision, j.embedding_model_id, j.embedding_dimension
|
|
`
|
|
|
|
type ClaimKbLearningJobRow struct {
|
|
ID string
|
|
KbID string
|
|
DocumentID *string
|
|
Operation string
|
|
Attempts int32
|
|
MaxAttempts int32
|
|
DocumentRevision *int32
|
|
EmbeddingModelID *string
|
|
EmbeddingDimension *int32
|
|
}
|
|
|
|
func (q *Queries) ClaimKbLearningJob(ctx context.Context, workerID *string) (ClaimKbLearningJobRow, error) {
|
|
row := q.db.QueryRow(ctx, claimKbLearningJob, workerID)
|
|
var i ClaimKbLearningJobRow
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.KbID,
|
|
&i.DocumentID,
|
|
&i.Operation,
|
|
&i.Attempts,
|
|
&i.MaxAttempts,
|
|
&i.DocumentRevision,
|
|
&i.EmbeddingModelID,
|
|
&i.EmbeddingDimension,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const clearDocumentVectors = `-- name: ClearDocumentVectors :exec
|
|
UPDATE knowledge_base_chunks
|
|
SET embedding = NULL,
|
|
embedding_model_id = NULL,
|
|
metadata = metadata - 'vector_error',
|
|
updated_at = now()
|
|
WHERE document_id = $1
|
|
`
|
|
|
|
func (q *Queries) ClearDocumentVectors(ctx context.Context, documentID string) error {
|
|
_, err := q.db.Exec(ctx, clearDocumentVectors, documentID)
|
|
return err
|
|
}
|
|
|
|
const clearKbVectors = `-- name: ClearKbVectors :exec
|
|
UPDATE knowledge_base_chunks
|
|
SET embedding = NULL,
|
|
embedding_model_id = NULL,
|
|
metadata = metadata - 'vector_error',
|
|
updated_at = now()
|
|
WHERE kb_id = $1
|
|
`
|
|
|
|
func (q *Queries) ClearKbVectors(ctx context.Context, kbID string) error {
|
|
_, err := q.db.Exec(ctx, clearKbVectors, kbID)
|
|
return err
|
|
}
|
|
|
|
const countKbDocumentChildren = `-- name: CountKbDocumentChildren :one
|
|
SELECT count(*)
|
|
FROM knowledge_base_documents
|
|
WHERE parent_id = $1
|
|
`
|
|
|
|
func (q *Queries) CountKbDocumentChildren(ctx context.Context, parentID *string) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countKbDocumentChildren, parentID)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const countKbDocuments = `-- name: CountKbDocuments :one
|
|
SELECT count(*) FILTER (WHERE node_type = 'document')::bigint AS total,
|
|
count(*) FILTER (WHERE node_type = 'document' AND status = 'learned')::bigint AS learned,
|
|
count(*) FILTER (WHERE node_type = 'document' AND chunk_count > 0)::bigint AS indexed
|
|
FROM knowledge_base_documents
|
|
WHERE kb_id = $1
|
|
`
|
|
|
|
type CountKbDocumentsRow struct {
|
|
Total int64
|
|
Learned int64
|
|
Indexed int64
|
|
}
|
|
|
|
func (q *Queries) CountKbDocuments(ctx context.Context, kbID string) (CountKbDocumentsRow, error) {
|
|
row := q.db.QueryRow(ctx, countKbDocuments, kbID)
|
|
var i CountKbDocumentsRow
|
|
err := row.Scan(&i.Total, &i.Learned, &i.Indexed)
|
|
return i, err
|
|
}
|
|
|
|
const countKbLearningJobs = `-- name: CountKbLearningJobs :many
|
|
WITH current_summary AS (
|
|
SELECT history.operation, history.status
|
|
FROM knowledge_base_learning_jobs history
|
|
WHERE history.kb_id = $1 AND history.status <> 'failed'
|
|
UNION ALL
|
|
-- Failed documents are counted once. Historical job failures never
|
|
-- contribute to this count.
|
|
SELECT COALESCE(latest.operation, 'learn') AS operation, 'failed'::text AS status
|
|
FROM knowledge_base_documents d
|
|
LEFT JOIN LATERAL (
|
|
SELECT j.operation
|
|
FROM knowledge_base_learning_jobs j
|
|
WHERE j.kb_id = d.kb_id AND (j.document_id = d.id OR j.document_id IS NULL)
|
|
ORDER BY j.created_at DESC, j.id DESC
|
|
LIMIT 1
|
|
) latest ON true
|
|
WHERE d.kb_id = $1 AND d.node_type = 'document' AND d.status = 'failed'
|
|
AND NOT EXISTS (
|
|
SELECT 1 FROM knowledge_base_learning_jobs active
|
|
WHERE active.kb_id = d.kb_id
|
|
AND (active.document_id = d.id OR active.document_id IS NULL)
|
|
AND active.status IN ('pending', 'running')
|
|
)
|
|
)
|
|
SELECT operation, status, count(*)::bigint AS total
|
|
FROM current_summary
|
|
GROUP BY operation, status
|
|
`
|
|
|
|
type CountKbLearningJobsRow struct {
|
|
Operation string
|
|
Status string
|
|
Total int64
|
|
}
|
|
|
|
func (q *Queries) CountKbLearningJobs(ctx context.Context, kbID string) ([]CountKbLearningJobsRow, error) {
|
|
rows, err := q.db.Query(ctx, countKbLearningJobs, kbID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []CountKbLearningJobsRow{}
|
|
for rows.Next() {
|
|
var i CountKbLearningJobsRow
|
|
if err := rows.Scan(&i.Operation, &i.Status, &i.Total); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const countRunningKnowledgeJobs = `-- name: CountRunningKnowledgeJobs :one
|
|
SELECT count(*) FROM knowledge_base_learning_jobs WHERE status IN ('running', 'canceled') AND locked_at >= now() - interval '5 minutes'
|
|
`
|
|
|
|
func (q *Queries) CountRunningKnowledgeJobs(ctx context.Context) (int64, error) {
|
|
row := q.db.QueryRow(ctx, countRunningKnowledgeJobs)
|
|
var count int64
|
|
err := row.Scan(&count)
|
|
return count, err
|
|
}
|
|
|
|
const createKbDocument = `-- name: CreateKbDocument :exec
|
|
INSERT INTO knowledge_base_documents (
|
|
id, kb_id, title, source_type, content, file_name, mime_type, size_bytes,
|
|
status, created_by_user_id, parent_id, node_type, position, file_type, storage_key
|
|
)
|
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15)
|
|
`
|
|
|
|
type CreateKbDocumentParams struct {
|
|
ID string
|
|
KbID string
|
|
Title string
|
|
SourceType string
|
|
Content string
|
|
FileName *string
|
|
MimeType *string
|
|
SizeBytes int64
|
|
Status string
|
|
CreatedByUserID string
|
|
ParentID *string
|
|
NodeType string
|
|
Position float64
|
|
FileType *string
|
|
StorageKey *string
|
|
}
|
|
|
|
func (q *Queries) CreateKbDocument(ctx context.Context, arg CreateKbDocumentParams) error {
|
|
_, err := q.db.Exec(ctx, createKbDocument,
|
|
arg.ID,
|
|
arg.KbID,
|
|
arg.Title,
|
|
arg.SourceType,
|
|
arg.Content,
|
|
arg.FileName,
|
|
arg.MimeType,
|
|
arg.SizeBytes,
|
|
arg.Status,
|
|
arg.CreatedByUserID,
|
|
arg.ParentID,
|
|
arg.NodeType,
|
|
arg.Position,
|
|
arg.FileType,
|
|
arg.StorageKey,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const createKbLearningJob = `-- name: CreateKbLearningJob :exec
|
|
INSERT INTO knowledge_base_learning_jobs (
|
|
kb_id, document_id, operation, document_revision,
|
|
embedding_model_id, embedding_dimension
|
|
)
|
|
VALUES ($1, $2, $3, $4, $5, $6)
|
|
`
|
|
|
|
type CreateKbLearningJobParams struct {
|
|
KbID string
|
|
DocumentID *string
|
|
Operation string
|
|
DocumentRevision *int32
|
|
EmbeddingModelID *string
|
|
EmbeddingDimension *int32
|
|
}
|
|
|
|
func (q *Queries) CreateKbLearningJob(ctx context.Context, arg CreateKbLearningJobParams) error {
|
|
_, err := q.db.Exec(ctx, createKbLearningJob,
|
|
arg.KbID,
|
|
arg.DocumentID,
|
|
arg.Operation,
|
|
arg.DocumentRevision,
|
|
arg.EmbeddingModelID,
|
|
arg.EmbeddingDimension,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const createKbResource = `-- name: CreateKbResource :exec
|
|
INSERT INTO knowledge_bases (
|
|
name, description, id, owner_user_id, created_by_user_id, ownership_type,
|
|
enabled, embedding_model_id, embedding_dimension, fusion_config
|
|
)
|
|
VALUES (
|
|
($1::jsonb ->> 'name')::text,
|
|
($1::jsonb ->> 'description')::text,
|
|
($1::jsonb ->> 'id')::uuid,
|
|
($1::jsonb ->> 'actor_id')::uuid,
|
|
($1::jsonb ->> 'actor_id')::uuid,
|
|
COALESCE($1::jsonb ->> 'ownership_type', 'system'),
|
|
CASE WHEN $1::jsonb ? 'enabled' THEN ($1::jsonb ->> 'enabled')::boolean ELSE TRUE END,
|
|
CASE WHEN $1::jsonb ? 'embedding_model_id' AND NULLIF($1::jsonb ->> 'embedding_model_id', '') IS NOT NULL
|
|
THEN ($1::jsonb ->> 'embedding_model_id')::uuid ELSE NULL END,
|
|
CASE WHEN $1::jsonb ? 'embedding_dimension' AND NULLIF($1::jsonb ->> 'embedding_dimension', '') IS NOT NULL
|
|
THEN ($1::jsonb ->> 'embedding_dimension')::integer ELSE NULL END,
|
|
COALESCE($1::jsonb -> 'fusion_config', '{"type": "weighted_rrf", "k": 60, "weights": {"lexical": 0.3, "vector": 0.7}}'::jsonb)
|
|
)
|
|
`
|
|
|
|
func (q *Queries) CreateKbResource(ctx context.Context, dollar_1 []byte) error {
|
|
_, err := q.db.Exec(ctx, createKbResource, dollar_1)
|
|
return err
|
|
}
|
|
|
|
const deleteKbChunksByDocument = `-- name: DeleteKbChunksByDocument :exec
|
|
DELETE FROM knowledge_base_chunks
|
|
WHERE document_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteKbChunksByDocument(ctx context.Context, documentID string) error {
|
|
_, err := q.db.Exec(ctx, deleteKbChunksByDocument, documentID)
|
|
return err
|
|
}
|
|
|
|
const deleteKbChunksByKb = `-- name: DeleteKbChunksByKb :exec
|
|
DELETE FROM knowledge_base_chunks
|
|
WHERE kb_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteKbChunksByKb(ctx context.Context, kbID string) error {
|
|
_, err := q.db.Exec(ctx, deleteKbChunksByKb, kbID)
|
|
return err
|
|
}
|
|
|
|
const deleteKbLearningJobsByKb = `-- name: DeleteKbLearningJobsByKb :exec
|
|
DELETE FROM knowledge_base_learning_jobs
|
|
WHERE kb_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteKbLearningJobsByKb(ctx context.Context, kbID string) error {
|
|
_, err := q.db.Exec(ctx, deleteKbLearningJobsByKb, kbID)
|
|
return err
|
|
}
|
|
|
|
const deleteKbResource = `-- name: DeleteKbResource :exec
|
|
DELETE FROM knowledge_bases
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteKbResource(ctx context.Context, id string) error {
|
|
_, err := q.db.Exec(ctx, deleteKbResource, id)
|
|
return err
|
|
}
|
|
|
|
const failKbLearningJob = `-- name: FailKbLearningJob :exec
|
|
UPDATE knowledge_base_learning_jobs
|
|
SET status = CASE WHEN attempts >= max_attempts THEN 'failed' ELSE 'pending' END,
|
|
available_at = CASE WHEN attempts >= max_attempts THEN available_at ELSE now() + interval '15 seconds' END,
|
|
error_message = $1,
|
|
locked_at = NULL,
|
|
locked_by = NULL,
|
|
finished_at = CASE WHEN attempts >= max_attempts THEN now() ELSE NULL END,
|
|
updated_at = now()
|
|
WHERE id = $2
|
|
AND status = 'running'
|
|
AND attempts = $3
|
|
AND locked_by = $4
|
|
`
|
|
|
|
type FailKbLearningJobParams struct {
|
|
ErrorMessage *string
|
|
ID string
|
|
Attempts int32
|
|
WorkerID *string
|
|
}
|
|
|
|
func (q *Queries) FailKbLearningJob(ctx context.Context, arg FailKbLearningJobParams) error {
|
|
_, err := q.db.Exec(ctx, failKbLearningJob,
|
|
arg.ErrorMessage,
|
|
arg.ID,
|
|
arg.Attempts,
|
|
arg.WorkerID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const finishKbDocumentLearning = `-- name: FinishKbDocumentLearning :exec
|
|
UPDATE knowledge_base_documents
|
|
SET status = 'learned',
|
|
error_message = NULL,
|
|
chunk_count = $2,
|
|
learned_at = now(),
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
type FinishKbDocumentLearningParams struct {
|
|
ID string
|
|
ChunkCount int32
|
|
}
|
|
|
|
func (q *Queries) FinishKbDocumentLearning(ctx context.Context, arg FinishKbDocumentLearningParams) error {
|
|
_, err := q.db.Exec(ctx, finishKbDocumentLearning, arg.ID, arg.ChunkCount)
|
|
return err
|
|
}
|
|
|
|
const finishKbDocumentVectorLearning = `-- name: FinishKbDocumentVectorLearning :exec
|
|
UPDATE knowledge_base_documents
|
|
SET status = 'learned',
|
|
error_message = NULL,
|
|
learned_at = COALESCE(learned_at, now()),
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) FinishKbDocumentVectorLearning(ctx context.Context, id string) error {
|
|
_, err := q.db.Exec(ctx, finishKbDocumentVectorLearning, id)
|
|
return err
|
|
}
|
|
|
|
const finishKbLearningJob = `-- name: FinishKbLearningJob :exec
|
|
UPDATE knowledge_base_learning_jobs
|
|
SET status = 'succeeded',
|
|
error_message = NULL,
|
|
locked_at = NULL,
|
|
locked_by = NULL,
|
|
finished_at = now(),
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
AND status = 'running'
|
|
AND attempts = $2
|
|
AND locked_by = $3
|
|
`
|
|
|
|
type FinishKbLearningJobParams struct {
|
|
ID string
|
|
Attempts int32
|
|
WorkerID *string
|
|
}
|
|
|
|
func (q *Queries) FinishKbLearningJob(ctx context.Context, arg FinishKbLearningJobParams) error {
|
|
_, err := q.db.Exec(ctx, finishKbLearningJob, arg.ID, arg.Attempts, arg.WorkerID)
|
|
return err
|
|
}
|
|
|
|
const getDefaultEmbeddingModel = `-- name: GetDefaultEmbeddingModel :one
|
|
SELECT id::text AS id,
|
|
COALESCE((embedding_config ->> 'dimension')::integer, 0)::bigint AS dimension
|
|
FROM models
|
|
WHERE kind = 'embedding'
|
|
AND ownership_type = 'system'
|
|
AND enabled
|
|
AND deleted_at IS NULL
|
|
ORDER BY created_at DESC, id
|
|
LIMIT 1
|
|
`
|
|
|
|
type GetDefaultEmbeddingModelRow struct {
|
|
ID string
|
|
Dimension int64
|
|
}
|
|
|
|
func (q *Queries) GetDefaultEmbeddingModel(ctx context.Context) (GetDefaultEmbeddingModelRow, error) {
|
|
row := q.db.QueryRow(ctx, getDefaultEmbeddingModel)
|
|
var i GetDefaultEmbeddingModelRow
|
|
err := row.Scan(&i.ID, &i.Dimension)
|
|
return i, err
|
|
}
|
|
|
|
const getEmbeddingModel = `-- name: GetEmbeddingModel :one
|
|
SELECT COALESCE((embedding_config ->> 'dimension')::integer, 0)::bigint AS dimension
|
|
FROM models
|
|
WHERE id = $1
|
|
AND kind = 'embedding'
|
|
AND ownership_type = 'system'
|
|
AND enabled
|
|
AND deleted_at IS NULL
|
|
`
|
|
|
|
func (q *Queries) GetEmbeddingModel(ctx context.Context, id string) (int64, error) {
|
|
row := q.db.QueryRow(ctx, getEmbeddingModel, id)
|
|
var dimension int64
|
|
err := row.Scan(&dimension)
|
|
return dimension, err
|
|
}
|
|
|
|
const getKbChunk = `-- name: GetKbChunk :one
|
|
SELECT jsonb_build_object(
|
|
'chunk_id', c.id,
|
|
'document_id', c.document_id,
|
|
'kb_id', c.kb_id,
|
|
'chunk_index', c.chunk_index,
|
|
'content', c.content,
|
|
'title', d.title,
|
|
'token_count', c.token_count,
|
|
'has_vector', c.embedding IS NOT NULL,
|
|
'heading_path', c.heading_path,
|
|
'page_no', c.page_no,
|
|
'source_url', c.source_url
|
|
)
|
|
FROM knowledge_base_chunks c
|
|
JOIN knowledge_base_documents d ON d.id = c.document_id
|
|
WHERE c.id = $1
|
|
`
|
|
|
|
func (q *Queries) GetKbChunk(ctx context.Context, id string) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, getKbChunk, id)
|
|
var jsonb_build_object []byte
|
|
err := row.Scan(&jsonb_build_object)
|
|
return jsonb_build_object, err
|
|
}
|
|
|
|
const getKbDocument = `-- name: GetKbDocument :one
|
|
SELECT to_jsonb(d)
|
|
FROM knowledge_base_documents d
|
|
WHERE d.id = $1
|
|
`
|
|
|
|
func (q *Queries) GetKbDocument(ctx context.Context, id string) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, getKbDocument, id)
|
|
var to_jsonb []byte
|
|
err := row.Scan(&to_jsonb)
|
|
return to_jsonb, err
|
|
}
|
|
|
|
const getKbDocumentRevision = `-- name: GetKbDocumentRevision :one
|
|
SELECT revision
|
|
FROM knowledge_base_documents
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetKbDocumentRevision(ctx context.Context, id string) (int32, error) {
|
|
row := q.db.QueryRow(ctx, getKbDocumentRevision, id)
|
|
var revision int32
|
|
err := row.Scan(&revision)
|
|
return revision, err
|
|
}
|
|
|
|
const getKbEmbedding = `-- name: GetKbEmbedding :one
|
|
SELECT embedding_model_id, embedding_dimension
|
|
FROM knowledge_bases
|
|
WHERE id = $1
|
|
AND enabled
|
|
FOR SHARE
|
|
`
|
|
|
|
type GetKbEmbeddingRow struct {
|
|
EmbeddingModelID *string
|
|
EmbeddingDimension *int32
|
|
}
|
|
|
|
func (q *Queries) GetKbEmbedding(ctx context.Context, id string) (GetKbEmbeddingRow, error) {
|
|
row := q.db.QueryRow(ctx, getKbEmbedding, id)
|
|
var i GetKbEmbeddingRow
|
|
err := row.Scan(&i.EmbeddingModelID, &i.EmbeddingDimension)
|
|
return i, err
|
|
}
|
|
|
|
const getKbLearningJobStatus = `-- name: GetKbLearningJobStatus :one
|
|
SELECT status
|
|
FROM knowledge_base_learning_jobs
|
|
WHERE id = $1
|
|
AND attempts = $2
|
|
AND locked_by = $3
|
|
FOR UPDATE
|
|
`
|
|
|
|
type GetKbLearningJobStatusParams struct {
|
|
ID string
|
|
Attempts int32
|
|
WorkerID *string
|
|
}
|
|
|
|
func (q *Queries) GetKbLearningJobStatus(ctx context.Context, arg GetKbLearningJobStatusParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, getKbLearningJobStatus, arg.ID, arg.Attempts, arg.WorkerID)
|
|
var status string
|
|
err := row.Scan(&status)
|
|
return status, err
|
|
}
|
|
|
|
const getKbResource = `-- name: GetKbResource :one
|
|
SELECT to_jsonb(t)
|
|
FROM knowledge_bases t
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) GetKbResource(ctx context.Context, id string) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, getKbResource, id)
|
|
var to_jsonb []byte
|
|
err := row.Scan(&to_jsonb)
|
|
return to_jsonb, err
|
|
}
|
|
|
|
const getKnowledgeConcurrency = `-- name: GetKnowledgeConcurrency :one
|
|
SELECT value FROM settings WHERE key='kb_concurrency'
|
|
`
|
|
|
|
func (q *Queries) GetKnowledgeConcurrency(ctx context.Context) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, getKnowledgeConcurrency)
|
|
var value []byte
|
|
err := row.Scan(&value)
|
|
return value, err
|
|
}
|
|
|
|
const getKnowledgeModel = `-- name: GetKnowledgeModel :one
|
|
SELECT to_jsonb(m) FROM knowledge_base_models m WHERE m.kind=$1
|
|
UNION ALL
|
|
SELECT to_jsonb(e) FROM (SELECT source.id, source.ownership_type, source.owner_user_id, source.model_id, source.display_name, source.protocol, source.base_url, source.api_key, source.advanced_config, source.credit_multiplier, source.enabled, source.created_at, source.updated_at, source.deleted_at, source.kind, source.provider, source.provider_options, source.image_config, source.image_pricing, source.video_config, source.video_pricing, source.proxy_id, source.embedding_config FROM models source WHERE $1::text='embedding' AND source.kind='embedding' AND source.ownership_type='system' AND source.deleted_at IS NULL ORDER BY source.created_at DESC,source.id DESC LIMIT 1) e
|
|
`
|
|
|
|
func (q *Queries) GetKnowledgeModel(ctx context.Context, kind string) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, getKnowledgeModel, kind)
|
|
var to_jsonb []byte
|
|
err := row.Scan(&to_jsonb)
|
|
return to_jsonb, err
|
|
}
|
|
|
|
const insertKbChunk = `-- name: InsertKbChunk :exec
|
|
INSERT INTO knowledge_base_chunks (
|
|
kb_id, document_id, chunk_index, content, token_count, search_tokens,
|
|
search_tsv, embedding, embedding_model_id, metadata, heading_path, page_no,
|
|
source_url
|
|
)
|
|
VALUES (
|
|
$1, $2, $3, $4,
|
|
$5::integer, $6,
|
|
to_tsvector('simple', $6::text),
|
|
$7::text::vector, $8::uuid,
|
|
$9::text::jsonb, $10::text,
|
|
$11::integer, $12::text
|
|
)
|
|
ON CONFLICT (kb_id, document_id, chunk_index)
|
|
DO UPDATE SET content = EXCLUDED.content,
|
|
token_count = EXCLUDED.token_count,
|
|
search_tokens = EXCLUDED.search_tokens,
|
|
search_tsv = EXCLUDED.search_tsv,
|
|
embedding = EXCLUDED.embedding,
|
|
embedding_model_id = EXCLUDED.embedding_model_id,
|
|
metadata = EXCLUDED.metadata,
|
|
heading_path = EXCLUDED.heading_path,
|
|
page_no = EXCLUDED.page_no,
|
|
source_url = EXCLUDED.source_url,
|
|
updated_at = now()
|
|
`
|
|
|
|
type InsertKbChunkParams struct {
|
|
KbID string
|
|
DocumentID string
|
|
ChunkIndex int32
|
|
Content string
|
|
TokenCount *int32
|
|
SearchTokens string
|
|
Embedding *string
|
|
EmbeddingModelID *string
|
|
Metadata string
|
|
HeadingPath *string
|
|
PageNo *int32
|
|
SourceUrl *string
|
|
}
|
|
|
|
func (q *Queries) InsertKbChunk(ctx context.Context, arg InsertKbChunkParams) error {
|
|
_, err := q.db.Exec(ctx, insertKbChunk,
|
|
arg.KbID,
|
|
arg.DocumentID,
|
|
arg.ChunkIndex,
|
|
arg.Content,
|
|
arg.TokenCount,
|
|
arg.SearchTokens,
|
|
arg.Embedding,
|
|
arg.EmbeddingModelID,
|
|
arg.Metadata,
|
|
arg.HeadingPath,
|
|
arg.PageNo,
|
|
arg.SourceUrl,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const kbActorActive = `-- name: KbActorActive :one
|
|
SELECT EXISTS(SELECT 1 FROM users WHERE id = $1 AND status = 'active' AND deleted_at IS NULL)::boolean
|
|
`
|
|
|
|
func (q *Queries) KbActorActive(ctx context.Context, id string) (bool, error) {
|
|
row := q.db.QueryRow(ctx, kbActorActive, id)
|
|
var column_1 bool
|
|
err := row.Scan(&column_1)
|
|
return column_1, err
|
|
}
|
|
|
|
const kbContentAccess = `-- name: KbContentAccess :one
|
|
WITH RECURSIVE user_groups(id) AS (
|
|
SELECT g.id FROM groups g JOIN group_users gu ON gu.group_id = g.id
|
|
WHERE gu.user_id = $2::uuid AND gu.removed_at IS NULL AND g.deleted_at IS NULL
|
|
UNION
|
|
SELECT p.id FROM groups g JOIN user_groups ug ON ug.id = g.id
|
|
JOIN groups p ON p.id = g.parent_id WHERE p.deleted_at IS NULL
|
|
)
|
|
SELECT COALESCE(max(CASE WHEN r.access_level = 'read_write' THEN 2 ELSE 1 END), 0)::integer AS level
|
|
FROM effective_resource_grants r
|
|
WHERE r.resource_type = 'kb' AND r.resource_id = $1::uuid
|
|
AND (r.user_id = $2::uuid OR r.all_users OR r.group_id IN (SELECT id FROM user_groups))
|
|
AND EXISTS (SELECT 1 FROM users u WHERE u.id = $2::uuid AND u.status = 'active' AND u.deleted_at IS NULL)
|
|
`
|
|
|
|
type KbContentAccessParams struct {
|
|
KbID string
|
|
Actor string
|
|
}
|
|
|
|
func (q *Queries) KbContentAccess(ctx context.Context, arg KbContentAccessParams) (int32, error) {
|
|
row := q.db.QueryRow(ctx, kbContentAccess, arg.KbID, arg.Actor)
|
|
var level int32
|
|
err := row.Scan(&level)
|
|
return level, err
|
|
}
|
|
|
|
const kbImportActorActive = `-- name: KbImportActorActive :one
|
|
SELECT EXISTS(SELECT 1 FROM users WHERE id=$1::uuid AND status='active' AND deleted_at IS NULL AND ($2::boolean=false OR role='admin'))
|
|
`
|
|
|
|
type KbImportActorActiveParams struct {
|
|
Actor string
|
|
Admin bool
|
|
}
|
|
|
|
func (q *Queries) KbImportActorActive(ctx context.Context, arg KbImportActorActiveParams) (bool, error) {
|
|
row := q.db.QueryRow(ctx, kbImportActorActive, arg.Actor, arg.Admin)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const kbLockImageImport = `-- name: KbLockImageImport :one
|
|
SELECT pg_try_advisory_xact_lock(hashtextextended($1::text, 0))::boolean
|
|
`
|
|
|
|
func (q *Queries) KbLockImageImport(ctx context.Context, importLock string) (bool, error) {
|
|
row := q.db.QueryRow(ctx, kbLockImageImport, importLock)
|
|
var column_1 bool
|
|
err := row.Scan(&column_1)
|
|
return column_1, err
|
|
}
|
|
|
|
const kbOriginalReferenced = `-- name: KbOriginalReferenced :one
|
|
SELECT EXISTS(SELECT 1 FROM knowledge_base_documents WHERE storage_key=$1::text)
|
|
`
|
|
|
|
func (q *Queries) KbOriginalReferenced(ctx context.Context, storageKey string) (bool, error) {
|
|
row := q.db.QueryRow(ctx, kbOriginalReferenced, storageKey)
|
|
var exists bool
|
|
err := row.Scan(&exists)
|
|
return exists, err
|
|
}
|
|
|
|
const listKbChunks = `-- name: ListKbChunks :many
|
|
SELECT jsonb_build_object(
|
|
'chunk_id', c.id,
|
|
'document_id', c.document_id,
|
|
'kb_id', c.kb_id,
|
|
'chunk_index', c.chunk_index,
|
|
'content', c.content,
|
|
'token_count', c.token_count,
|
|
'has_vector', c.embedding IS NOT NULL,
|
|
'embedding_model_id', c.embedding_model_id,
|
|
'heading_path', c.heading_path,
|
|
'page_no', c.page_no,
|
|
'source_url', c.source_url,
|
|
'created_at', c.created_at,
|
|
'updated_at', c.updated_at
|
|
)
|
|
FROM knowledge_base_chunks c
|
|
WHERE c.document_id = $1
|
|
ORDER BY c.chunk_index
|
|
`
|
|
|
|
func (q *Queries) ListKbChunks(ctx context.Context, documentID string) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, listKbChunks, documentID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var jsonb_build_object []byte
|
|
if err := rows.Scan(&jsonb_build_object); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, jsonb_build_object)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listKbChunksForRebuild = `-- name: ListKbChunksForRebuild :many
|
|
SELECT c.id::text AS id, c.document_id::text AS document_id, c.content, COALESCE(d.file_type, '')::text AS file_type
|
|
FROM knowledge_base_chunks c
|
|
JOIN knowledge_base_documents d ON d.id = c.document_id
|
|
WHERE c.kb_id = $1
|
|
ORDER BY c.document_id, c.chunk_index
|
|
`
|
|
|
|
type ListKbChunksForRebuildRow struct {
|
|
ID string
|
|
DocumentID string
|
|
Content string
|
|
FileType string
|
|
}
|
|
|
|
func (q *Queries) ListKbChunksForRebuild(ctx context.Context, kbID string) ([]ListKbChunksForRebuildRow, error) {
|
|
rows, err := q.db.Query(ctx, listKbChunksForRebuild, kbID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListKbChunksForRebuildRow{}
|
|
for rows.Next() {
|
|
var i ListKbChunksForRebuildRow
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.DocumentID,
|
|
&i.Content,
|
|
&i.FileType,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listKbChunksNeedingVector = `-- name: ListKbChunksNeedingVector :many
|
|
SELECT c.id::text AS id, c.document_id::text AS document_id, c.content
|
|
FROM knowledge_base_chunks c
|
|
JOIN knowledge_base_documents d ON d.id = c.document_id
|
|
WHERE c.kb_id = $1
|
|
AND c.document_id = $2
|
|
AND (
|
|
c.embedding IS NULL
|
|
OR c.embedding_model_id IS DISTINCT FROM $3::uuid
|
|
)
|
|
ORDER BY c.chunk_index, c.id
|
|
`
|
|
|
|
type ListKbChunksNeedingVectorParams struct {
|
|
KbID string
|
|
DocumentID string
|
|
EmbeddingModelID string
|
|
}
|
|
|
|
type ListKbChunksNeedingVectorRow struct {
|
|
ID string
|
|
DocumentID string
|
|
Content string
|
|
}
|
|
|
|
func (q *Queries) ListKbChunksNeedingVector(ctx context.Context, arg ListKbChunksNeedingVectorParams) ([]ListKbChunksNeedingVectorRow, error) {
|
|
rows, err := q.db.Query(ctx, listKbChunksNeedingVector, arg.KbID, arg.DocumentID, arg.EmbeddingModelID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []ListKbChunksNeedingVectorRow{}
|
|
for rows.Next() {
|
|
var i ListKbChunksNeedingVectorRow
|
|
if err := rows.Scan(&i.ID, &i.DocumentID, &i.Content); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listKbDocuments = `-- name: ListKbDocuments :many
|
|
SELECT to_jsonb(d)
|
|
FROM knowledge_base_documents d
|
|
WHERE d.kb_id = $1
|
|
ORDER BY d.position, d.created_at, d.id
|
|
`
|
|
|
|
func (q *Queries) ListKbDocuments(ctx context.Context, kbID string) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, listKbDocuments, kbID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var to_jsonb []byte
|
|
if err := rows.Scan(&to_jsonb); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, to_jsonb)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listKbEmbeddingSyncCandidates = `-- name: ListKbEmbeddingSyncCandidates :many
|
|
SELECT k.id::text
|
|
FROM knowledge_bases k
|
|
LEFT JOIN models m ON m.id = k.embedding_model_id
|
|
AND m.kind = 'embedding'
|
|
AND m.ownership_type = 'system'
|
|
AND m.enabled
|
|
AND m.deleted_at IS NULL
|
|
AND (m.embedding_config ->> 'dimension')::integer > 0
|
|
WHERE k.enabled AND (
|
|
(
|
|
k.embedding_model_id IS NULL
|
|
AND EXISTS (
|
|
SELECT 1 FROM models available
|
|
WHERE available.kind = 'embedding'
|
|
AND available.ownership_type = 'system'
|
|
AND available.enabled
|
|
AND available.deleted_at IS NULL
|
|
AND (available.embedding_config ->> 'dimension')::integer > 0
|
|
)
|
|
)
|
|
OR (
|
|
k.embedding_model_id IS NOT NULL
|
|
AND (
|
|
(m.id IS NULL AND k.embedding_dimension IS NOT NULL)
|
|
OR (
|
|
m.id IS NOT NULL
|
|
AND k.embedding_dimension IS DISTINCT FROM (m.embedding_config ->> 'dimension')::integer
|
|
)
|
|
)
|
|
)
|
|
)
|
|
ORDER BY k.created_at, k.id
|
|
`
|
|
|
|
func (q *Queries) ListKbEmbeddingSyncCandidates(ctx context.Context) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, listKbEmbeddingSyncCandidates)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []string{}
|
|
for rows.Next() {
|
|
var k_id string
|
|
if err := rows.Scan(&k_id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, k_id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listKbResources = `-- name: ListKbResources :many
|
|
SELECT to_jsonb(t)
|
|
FROM knowledge_bases t
|
|
ORDER BY lower(name), id
|
|
`
|
|
|
|
func (q *Queries) ListKbResources(ctx context.Context) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, listKbResources)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var to_jsonb []byte
|
|
if err := rows.Scan(&to_jsonb); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, to_jsonb)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listKnowledgeModelAffectedBases = `-- name: ListKnowledgeModelAffectedBases :many
|
|
SELECT id::text FROM knowledge_bases WHERE embedding_model_id=$1::uuid OR embedding_model_id IS NULL ORDER BY id
|
|
`
|
|
|
|
func (q *Queries) ListKnowledgeModelAffectedBases(ctx context.Context, dollar_1 string) ([]string, error) {
|
|
rows, err := q.db.Query(ctx, listKnowledgeModelAffectedBases, dollar_1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := []string{}
|
|
for rows.Next() {
|
|
var id string
|
|
if err := rows.Scan(&id); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, id)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const listKnowledgeModels = `-- name: ListKnowledgeModels :many
|
|
SELECT to_jsonb(m) FROM knowledge_base_models m
|
|
UNION ALL
|
|
SELECT to_jsonb(e) FROM (SELECT id, ownership_type, owner_user_id, model_id, display_name, protocol, base_url, api_key, advanced_config, credit_multiplier, enabled, created_at, updated_at, deleted_at, kind, provider, provider_options, image_config, image_pricing, video_config, video_pricing, proxy_id, embedding_config FROM models WHERE kind='embedding' AND ownership_type='system' AND deleted_at IS NULL ORDER BY created_at DESC,id DESC LIMIT 1) e
|
|
`
|
|
|
|
func (q *Queries) ListKnowledgeModels(ctx context.Context) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, listKnowledgeModels)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var to_jsonb []byte
|
|
if err := rows.Scan(&to_jsonb); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, to_jsonb)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const lockKbDocument = `-- name: LockKbDocument :one
|
|
SELECT to_jsonb(d)
|
|
FROM knowledge_base_documents d
|
|
WHERE d.id = $1
|
|
FOR UPDATE
|
|
`
|
|
|
|
func (q *Queries) LockKbDocument(ctx context.Context, id string) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, lockKbDocument, id)
|
|
var to_jsonb []byte
|
|
err := row.Scan(&to_jsonb)
|
|
return to_jsonb, err
|
|
}
|
|
|
|
const lockKbResource = `-- name: LockKbResource :one
|
|
SELECT to_jsonb(t)
|
|
FROM knowledge_bases t
|
|
WHERE id = $1
|
|
FOR UPDATE
|
|
`
|
|
|
|
func (q *Queries) LockKbResource(ctx context.Context, id string) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, lockKbResource, id)
|
|
var to_jsonb []byte
|
|
err := row.Scan(&to_jsonb)
|
|
return to_jsonb, err
|
|
}
|
|
|
|
const lockKnowledgeLearningAdmission = `-- name: LockKnowledgeLearningAdmission :exec
|
|
SELECT pg_advisory_xact_lock(731246801)
|
|
`
|
|
|
|
func (q *Queries) LockKnowledgeLearningAdmission(ctx context.Context) error {
|
|
_, err := q.db.Exec(ctx, lockKnowledgeLearningAdmission)
|
|
return err
|
|
}
|
|
|
|
const lockKnowledgeModel = `-- name: LockKnowledgeModel :exec
|
|
SELECT pg_advisory_xact_lock(hashtextextended('knowledge-model:' || $1::text, 0))
|
|
`
|
|
|
|
func (q *Queries) LockKnowledgeModel(ctx context.Context, dollar_1 string) error {
|
|
_, err := q.db.Exec(ctx, lockKnowledgeModel, dollar_1)
|
|
return err
|
|
}
|
|
|
|
const markKbDocumentFailed = `-- name: MarkKbDocumentFailed :exec
|
|
UPDATE knowledge_base_documents
|
|
SET status = 'failed',
|
|
error_message = $2,
|
|
chunk_count = 0,
|
|
learned_at = NULL,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
type MarkKbDocumentFailedParams struct {
|
|
ID string
|
|
ErrorMessage *string
|
|
}
|
|
|
|
func (q *Queries) MarkKbDocumentFailed(ctx context.Context, arg MarkKbDocumentFailedParams) error {
|
|
_, err := q.db.Exec(ctx, markKbDocumentFailed, arg.ID, arg.ErrorMessage)
|
|
return err
|
|
}
|
|
|
|
const markKbDocumentVectorFailed = `-- name: MarkKbDocumentVectorFailed :exec
|
|
UPDATE knowledge_base_documents
|
|
SET status = 'failed',
|
|
error_message = $2,
|
|
chunk_count = $3,
|
|
learned_at = COALESCE(learned_at, now()),
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
type MarkKbDocumentVectorFailedParams struct {
|
|
ID string
|
|
ErrorMessage *string
|
|
ChunkCount int32
|
|
}
|
|
|
|
func (q *Queries) MarkKbDocumentVectorFailed(ctx context.Context, arg MarkKbDocumentVectorFailedParams) error {
|
|
_, err := q.db.Exec(ctx, markKbDocumentVectorFailed, arg.ID, arg.ErrorMessage, arg.ChunkCount)
|
|
return err
|
|
}
|
|
|
|
const moveKbDocument = `-- name: MoveKbDocument :exec
|
|
UPDATE knowledge_base_documents
|
|
SET parent_id = $2,
|
|
position = $3,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
type MoveKbDocumentParams struct {
|
|
ID string
|
|
ParentID *string
|
|
Position float64
|
|
}
|
|
|
|
func (q *Queries) MoveKbDocument(ctx context.Context, arg MoveKbDocumentParams) error {
|
|
_, err := q.db.Exec(ctx, moveKbDocument, arg.ID, arg.ParentID, arg.Position)
|
|
return err
|
|
}
|
|
|
|
const nextKbDocumentPosition = `-- name: NextKbDocumentPosition :one
|
|
SELECT (COALESCE(MAX(position), 0) + 1000)::float8
|
|
FROM knowledge_base_documents
|
|
WHERE kb_id = $1
|
|
AND parent_id IS NOT DISTINCT FROM $2
|
|
`
|
|
|
|
type NextKbDocumentPositionParams struct {
|
|
KbID string
|
|
ParentID *string
|
|
}
|
|
|
|
func (q *Queries) NextKbDocumentPosition(ctx context.Context, arg NextKbDocumentPositionParams) (float64, error) {
|
|
row := q.db.QueryRow(ctx, nextKbDocumentPosition, arg.KbID, arg.ParentID)
|
|
var column_1 float64
|
|
err := row.Scan(&column_1)
|
|
return column_1, err
|
|
}
|
|
|
|
const pageKbResources = `-- name: PageKbResources :many
|
|
SELECT to_jsonb(t)
|
|
FROM knowledge_bases t
|
|
WHERE ($1::jsonb ->> 'ownership' = '' OR t.ownership_type = $1::jsonb ->> 'ownership')
|
|
AND (strpos(lower(name), lower($1::jsonb ->> 'search')) > 0
|
|
OR strpos(lower(description), lower($1::jsonb ->> 'search')) > 0)
|
|
AND ($1::jsonb ->> 'cursor' = '' OR (lower(t.name), t.id) > (
|
|
SELECT lower(cursor_kb.name), cursor_kb.id FROM knowledge_bases cursor_kb
|
|
WHERE cursor_kb.id::text = $1::jsonb ->> 'cursor'
|
|
))
|
|
ORDER BY lower(name), id
|
|
LIMIT ($1::jsonb ->> 'limit')::integer
|
|
`
|
|
|
|
func (q *Queries) PageKbResources(ctx context.Context, dollar_1 []byte) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, pageKbResources, dollar_1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var to_jsonb []byte
|
|
if err := rows.Scan(&to_jsonb); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, to_jsonb)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const releaseCanceledKnowledgeJob = `-- name: ReleaseCanceledKnowledgeJob :exec
|
|
UPDATE knowledge_base_learning_jobs
|
|
SET locked_at=NULL, locked_by=NULL, updated_at=now()
|
|
WHERE id=$1 AND attempts=$2
|
|
AND locked_by=$3 AND status='canceled'
|
|
`
|
|
|
|
type ReleaseCanceledKnowledgeJobParams struct {
|
|
ID string
|
|
Attempts int32
|
|
WorkerID *string
|
|
}
|
|
|
|
func (q *Queries) ReleaseCanceledKnowledgeJob(ctx context.Context, arg ReleaseCanceledKnowledgeJobParams) error {
|
|
_, err := q.db.Exec(ctx, releaseCanceledKnowledgeJob, arg.ID, arg.Attempts, arg.WorkerID)
|
|
return err
|
|
}
|
|
|
|
const renewKbLearningJob = `-- name: RenewKbLearningJob :execrows
|
|
UPDATE knowledge_base_learning_jobs
|
|
SET locked_at = now(), updated_at = now()
|
|
WHERE id = $1 AND status = 'running'
|
|
AND attempts = $2 AND locked_by = $3
|
|
`
|
|
|
|
type RenewKbLearningJobParams struct {
|
|
ID string
|
|
Attempts int32
|
|
WorkerID *string
|
|
}
|
|
|
|
func (q *Queries) RenewKbLearningJob(ctx context.Context, arg RenewKbLearningJobParams) (int64, error) {
|
|
result, err := q.db.Exec(ctx, renewKbLearningJob, arg.ID, arg.Attempts, arg.WorkerID)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const resolveKbImportTag = `-- name: ResolveKbImportTag :one
|
|
INSERT INTO tags(name,created_by_user_id) VALUES($1,$2)
|
|
ON CONFLICT (lower(name)) WHERE deleted_at IS NULL
|
|
DO UPDATE SET name=tags.name RETURNING id::text
|
|
`
|
|
|
|
type ResolveKbImportTagParams struct {
|
|
Name string
|
|
CreatedByUserID string
|
|
}
|
|
|
|
func (q *Queries) ResolveKbImportTag(ctx context.Context, arg ResolveKbImportTagParams) (string, error) {
|
|
row := q.db.QueryRow(ctx, resolveKbImportTag, arg.Name, arg.CreatedByUserID)
|
|
var id string
|
|
err := row.Scan(&id)
|
|
return id, err
|
|
}
|
|
|
|
const saveKnowledgeConcurrency = `-- name: SaveKnowledgeConcurrency :exec
|
|
INSERT INTO settings(key,value,schema_version,updated_by_user_id)
|
|
VALUES('kb_concurrency',$1::jsonb,1,$2::uuid)
|
|
ON CONFLICT(key) DO UPDATE SET value=EXCLUDED.value, updated_by_user_id=EXCLUDED.updated_by_user_id, updated_at=now()
|
|
`
|
|
|
|
type SaveKnowledgeConcurrencyParams struct {
|
|
Value []byte
|
|
Actor *string
|
|
}
|
|
|
|
func (q *Queries) SaveKnowledgeConcurrency(ctx context.Context, arg SaveKnowledgeConcurrencyParams) error {
|
|
_, err := q.db.Exec(ctx, saveKnowledgeConcurrency, arg.Value, arg.Actor)
|
|
return err
|
|
}
|
|
|
|
const saveKnowledgeEmbeddingModel = `-- name: SaveKnowledgeEmbeddingModel :one
|
|
INSERT INTO models(id,kind,ownership_type,owner_user_id,model_id,display_name,base_url,api_key,protocol,proxy_id,embedding_config,advanced_config,enabled)
|
|
VALUES (($1::jsonb->>'id')::uuid,'embedding','system',($1::jsonb->>'owner_user_id')::uuid,
|
|
$1::jsonb->>'model_id',$1::jsonb->>'model_id',$1::jsonb->>'base_url',$1::jsonb->>'api_key',
|
|
'openai_embeddings',$1::jsonb->>'proxy_id',$1::jsonb->'embedding_config','{}',($1::jsonb->>'enabled')::boolean)
|
|
ON CONFLICT(id) DO UPDATE SET model_id=EXCLUDED.model_id,display_name=EXCLUDED.display_name,
|
|
base_url=EXCLUDED.base_url,api_key=EXCLUDED.api_key,proxy_id=EXCLUDED.proxy_id,
|
|
embedding_config=EXCLUDED.embedding_config,enabled=EXCLUDED.enabled,updated_at=now()
|
|
RETURNING to_jsonb(models.*)
|
|
`
|
|
|
|
func (q *Queries) SaveKnowledgeEmbeddingModel(ctx context.Context, dollar_1 []byte) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, saveKnowledgeEmbeddingModel, dollar_1)
|
|
var to_jsonb []byte
|
|
err := row.Scan(&to_jsonb)
|
|
return to_jsonb, err
|
|
}
|
|
|
|
const saveKnowledgeModel = `-- name: SaveKnowledgeModel :one
|
|
INSERT INTO knowledge_base_models(id,kind,model_id,base_url,api_key,protocol,proxy_id,advanced_config,enabled)
|
|
VALUES (($1::jsonb->>'id')::uuid,$1::jsonb->>'kind',$1::jsonb->>'model_id',$1::jsonb->>'base_url',
|
|
$1::jsonb->>'api_key',$1::jsonb->>'protocol',$1::jsonb->>'proxy_id',
|
|
$1::jsonb->'advanced_config',($1::jsonb->>'enabled')::boolean)
|
|
ON CONFLICT(kind) DO UPDATE SET id=EXCLUDED.id,model_id=EXCLUDED.model_id,base_url=EXCLUDED.base_url,
|
|
api_key=EXCLUDED.api_key,protocol=EXCLUDED.protocol,proxy_id=EXCLUDED.proxy_id,
|
|
advanced_config=EXCLUDED.advanced_config,enabled=EXCLUDED.enabled,
|
|
updated_at=now()
|
|
RETURNING to_jsonb(knowledge_base_models.*)
|
|
`
|
|
|
|
func (q *Queries) SaveKnowledgeModel(ctx context.Context, dollar_1 []byte) ([]byte, error) {
|
|
row := q.db.QueryRow(ctx, saveKnowledgeModel, dollar_1)
|
|
var to_jsonb []byte
|
|
err := row.Scan(&to_jsonb)
|
|
return to_jsonb, err
|
|
}
|
|
|
|
const searchKbChunksLexical = `-- name: SearchKbChunksLexical :many
|
|
SELECT jsonb_build_object(
|
|
'kb_id', c.kb_id,
|
|
'document_id', c.document_id,
|
|
'chunk_id', c.id,
|
|
'chunk_index', c.chunk_index,
|
|
'content', c.content,
|
|
'title', d.title,
|
|
'file_type', d.file_type,
|
|
'has_original', d.storage_key IS NOT NULL AND d.storage_key <> '',
|
|
'heading_path', c.heading_path,
|
|
'page_no', c.page_no,
|
|
'source_url', c.source_url,
|
|
'score', ts_rank_cd(c.search_tsv, to_tsquery('simple', $1))
|
|
)
|
|
FROM knowledge_base_chunks c
|
|
JOIN knowledge_base_documents d ON d.id = c.document_id
|
|
WHERE c.kb_id = $2
|
|
AND (COALESCE(cardinality($3::text[]), 0) = 0 OR d.file_type = ANY($3::text[]))
|
|
AND c.search_tsv @@ to_tsquery('simple', $1)
|
|
ORDER BY ts_rank_cd(c.search_tsv, to_tsquery('simple', $1)) DESC,
|
|
c.chunk_index,
|
|
c.id
|
|
LIMIT $4::integer
|
|
`
|
|
|
|
type SearchKbChunksLexicalParams struct {
|
|
QueryExpression string
|
|
KbID string
|
|
FileTypes []string
|
|
RowLimit int32
|
|
}
|
|
|
|
func (q *Queries) SearchKbChunksLexical(ctx context.Context, arg SearchKbChunksLexicalParams) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, searchKbChunksLexical,
|
|
arg.QueryExpression,
|
|
arg.KbID,
|
|
arg.FileTypes,
|
|
arg.RowLimit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var jsonb_build_object []byte
|
|
if err := rows.Scan(&jsonb_build_object); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, jsonb_build_object)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const searchKbChunksVector = `-- name: SearchKbChunksVector :many
|
|
SELECT jsonb_build_object(
|
|
'kb_id', c.kb_id,
|
|
'document_id', c.document_id,
|
|
'chunk_id', c.id,
|
|
'chunk_index', c.chunk_index,
|
|
'content', c.content,
|
|
'title', d.title,
|
|
'file_type', d.file_type,
|
|
'has_original', d.storage_key IS NOT NULL AND d.storage_key <> '',
|
|
'heading_path', c.heading_path,
|
|
'page_no', c.page_no,
|
|
'source_url', c.source_url,
|
|
'score', 1 - (c.embedding <=> $1::text::vector)
|
|
)
|
|
FROM knowledge_base_chunks c
|
|
JOIN knowledge_base_documents d ON d.id = c.document_id
|
|
WHERE c.kb_id = $2
|
|
AND (COALESCE(cardinality($3::text[]), 0) = 0 OR d.file_type = ANY($3::text[]))
|
|
AND c.embedding IS NOT NULL
|
|
AND c.embedding_model_id = $4::uuid
|
|
ORDER BY c.embedding <=> $1::text::vector
|
|
LIMIT $5::integer
|
|
`
|
|
|
|
type SearchKbChunksVectorParams struct {
|
|
Embedding string
|
|
KbID string
|
|
FileTypes []string
|
|
EmbeddingModelID string
|
|
RowLimit int32
|
|
}
|
|
|
|
func (q *Queries) SearchKbChunksVector(ctx context.Context, arg SearchKbChunksVectorParams) ([][]byte, error) {
|
|
rows, err := q.db.Query(ctx, searchKbChunksVector,
|
|
arg.Embedding,
|
|
arg.KbID,
|
|
arg.FileTypes,
|
|
arg.EmbeddingModelID,
|
|
arg.RowLimit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
items := [][]byte{}
|
|
for rows.Next() {
|
|
var jsonb_build_object []byte
|
|
if err := rows.Scan(&jsonb_build_object); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, jsonb_build_object)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const selectKbEmbeddingModel = `-- name: SelectKbEmbeddingModel :one
|
|
SELECT id::text AS id,
|
|
(embedding_config ->> 'dimension')::integer AS dimension
|
|
FROM models
|
|
WHERE kind = 'embedding'
|
|
AND ownership_type = 'system'
|
|
AND enabled
|
|
AND deleted_at IS NULL
|
|
AND (embedding_config ->> 'dimension')::integer > 0
|
|
ORDER BY (id::text = $1::text) DESC, created_at, id
|
|
LIMIT 1
|
|
`
|
|
|
|
type SelectKbEmbeddingModelRow struct {
|
|
ID string
|
|
Dimension int32
|
|
}
|
|
|
|
func (q *Queries) SelectKbEmbeddingModel(ctx context.Context, preferredID string) (SelectKbEmbeddingModelRow, error) {
|
|
row := q.db.QueryRow(ctx, selectKbEmbeddingModel, preferredID)
|
|
var i SelectKbEmbeddingModelRow
|
|
err := row.Scan(&i.ID, &i.Dimension)
|
|
return i, err
|
|
}
|
|
|
|
const setAvatar = `-- name: SetAvatar :exec
|
|
UPDATE knowledge_bases SET avatar_s3_key = $2 WHERE id = $1
|
|
`
|
|
|
|
type SetAvatarParams struct {
|
|
ID string
|
|
AvatarS3Key string
|
|
}
|
|
|
|
func (q *Queries) SetAvatar(ctx context.Context, arg SetAvatarParams) error {
|
|
_, err := q.db.Exec(ctx, setAvatar, arg.ID, arg.AvatarS3Key)
|
|
return err
|
|
}
|
|
|
|
const setKbDocumentLearning = `-- name: SetKbDocumentLearning :exec
|
|
UPDATE knowledge_base_documents
|
|
SET status = 'learning',
|
|
error_message = NULL,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) SetKbDocumentLearning(ctx context.Context, id string) error {
|
|
_, err := q.db.Exec(ctx, setKbDocumentLearning, id)
|
|
return err
|
|
}
|
|
|
|
const setKbDocumentLexicallyLearned = `-- name: SetKbDocumentLexicallyLearned :exec
|
|
UPDATE knowledge_base_documents
|
|
SET status = 'learned',
|
|
error_message = NULL,
|
|
learned_at = COALESCE(learned_at, now()),
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) SetKbDocumentLexicallyLearned(ctx context.Context, id string) error {
|
|
_, err := q.db.Exec(ctx, setKbDocumentLexicallyLearned, id)
|
|
return err
|
|
}
|
|
|
|
const setKbDocumentPending = `-- name: SetKbDocumentPending :one
|
|
UPDATE knowledge_base_documents
|
|
SET status = 'pending',
|
|
error_message = NULL,
|
|
chunk_count = 0,
|
|
learned_at = NULL,
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
RETURNING revision
|
|
`
|
|
|
|
func (q *Queries) SetKbDocumentPending(ctx context.Context, id string) (int32, error) {
|
|
row := q.db.QueryRow(ctx, setKbDocumentPending, id)
|
|
var revision int32
|
|
err := row.Scan(&revision)
|
|
return revision, err
|
|
}
|
|
|
|
const setKbDocumentVectorLearning = `-- name: SetKbDocumentVectorLearning :one
|
|
UPDATE knowledge_base_documents
|
|
SET status = 'learning',
|
|
error_message = NULL,
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
RETURNING revision
|
|
`
|
|
|
|
func (q *Queries) SetKbDocumentVectorLearning(ctx context.Context, id string) (int32, error) {
|
|
row := q.db.QueryRow(ctx, setKbDocumentVectorLearning, id)
|
|
var revision int32
|
|
err := row.Scan(&revision)
|
|
return revision, err
|
|
}
|
|
|
|
const setKbEmbedding = `-- name: SetKbEmbedding :exec
|
|
UPDATE knowledge_bases
|
|
SET embedding_model_id = $2,
|
|
embedding_dimension = $3,
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
type SetKbEmbeddingParams struct {
|
|
ID string
|
|
EmbeddingModelID *string
|
|
EmbeddingDimension *int32
|
|
}
|
|
|
|
func (q *Queries) SetKbEmbedding(ctx context.Context, arg SetKbEmbeddingParams) error {
|
|
_, err := q.db.Exec(ctx, setKbEmbedding, arg.ID, arg.EmbeddingModelID, arg.EmbeddingDimension)
|
|
return err
|
|
}
|
|
|
|
const setKbEmbeddingModel = `-- name: SetKbEmbeddingModel :exec
|
|
UPDATE knowledge_bases
|
|
SET embedding_model_id = NULLIF($1::text, '')::uuid,
|
|
embedding_dimension = $2::integer,
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE knowledge_bases.id = $3
|
|
`
|
|
|
|
type SetKbEmbeddingModelParams struct {
|
|
EmbeddingModelID string
|
|
EmbeddingDimension *int32
|
|
ID string
|
|
}
|
|
|
|
func (q *Queries) SetKbEmbeddingModel(ctx context.Context, arg SetKbEmbeddingModelParams) error {
|
|
_, err := q.db.Exec(ctx, setKbEmbeddingModel, arg.EmbeddingModelID, arg.EmbeddingDimension, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const setKbResourceEnabled = `-- name: SetKbResourceEnabled :exec
|
|
UPDATE knowledge_bases
|
|
SET enabled = ($1::jsonb ->> 'enabled')::boolean,
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = ($1::jsonb ->> 'id')::uuid
|
|
`
|
|
|
|
func (q *Queries) SetKbResourceEnabled(ctx context.Context, dollar_1 []byte) error {
|
|
_, err := q.db.Exec(ctx, setKbResourceEnabled, dollar_1)
|
|
return err
|
|
}
|
|
|
|
const softDeleteKbDocument = `-- name: SoftDeleteKbDocument :execrows
|
|
DELETE FROM knowledge_base_documents
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) SoftDeleteKbDocument(ctx context.Context, id string) (int64, error) {
|
|
result, err := q.db.Exec(ctx, softDeleteKbDocument, id)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return result.RowsAffected(), nil
|
|
}
|
|
|
|
const softDeleteKbDocumentsByKb = `-- name: SoftDeleteKbDocumentsByKb :exec
|
|
DELETE FROM knowledge_base_documents
|
|
WHERE kb_id = $1
|
|
`
|
|
|
|
func (q *Queries) SoftDeleteKbDocumentsByKb(ctx context.Context, kbID string) error {
|
|
_, err := q.db.Exec(ctx, softDeleteKbDocumentsByKb, kbID)
|
|
return err
|
|
}
|
|
|
|
const touchKbDocumentAuthor = `-- name: TouchKbDocumentAuthor :exec
|
|
UPDATE knowledge_base_documents SET updated_by_user_id = $2, content_updated_at = now() WHERE id = $1
|
|
`
|
|
|
|
type TouchKbDocumentAuthorParams struct {
|
|
ID string
|
|
UpdatedByUserID *string
|
|
}
|
|
|
|
func (q *Queries) TouchKbDocumentAuthor(ctx context.Context, arg TouchKbDocumentAuthorParams) error {
|
|
_, err := q.db.Exec(ctx, touchKbDocumentAuthor, arg.ID, arg.UpdatedByUserID)
|
|
return err
|
|
}
|
|
|
|
const touchKbResource = `-- name: TouchKbResource :exec
|
|
UPDATE knowledge_bases
|
|
SET revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
func (q *Queries) TouchKbResource(ctx context.Context, id string) error {
|
|
_, err := q.db.Exec(ctx, touchKbResource, id)
|
|
return err
|
|
}
|
|
|
|
const updateKbChunkLexical = `-- name: UpdateKbChunkLexical :exec
|
|
UPDATE knowledge_base_chunks
|
|
SET search_tokens = $1,
|
|
search_tsv = to_tsvector('simple', $1::text),
|
|
updated_at = now()
|
|
WHERE id = $2
|
|
`
|
|
|
|
type UpdateKbChunkLexicalParams struct {
|
|
SearchTokens string
|
|
ID string
|
|
}
|
|
|
|
func (q *Queries) UpdateKbChunkLexical(ctx context.Context, arg UpdateKbChunkLexicalParams) error {
|
|
_, err := q.db.Exec(ctx, updateKbChunkLexical, arg.SearchTokens, arg.ID)
|
|
return err
|
|
}
|
|
|
|
const updateKbChunkVector = `-- name: UpdateKbChunkVector :exec
|
|
UPDATE knowledge_base_chunks
|
|
SET embedding = $1::text::vector,
|
|
embedding_model_id = $2::uuid,
|
|
metadata = $3::text::jsonb,
|
|
updated_at = now()
|
|
WHERE id = $4
|
|
`
|
|
|
|
type UpdateKbChunkVectorParams struct {
|
|
Embedding *string
|
|
EmbeddingModelID *string
|
|
Metadata string
|
|
ID string
|
|
}
|
|
|
|
func (q *Queries) UpdateKbChunkVector(ctx context.Context, arg UpdateKbChunkVectorParams) error {
|
|
_, err := q.db.Exec(ctx, updateKbChunkVector,
|
|
arg.Embedding,
|
|
arg.EmbeddingModelID,
|
|
arg.Metadata,
|
|
arg.ID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const updateKbDocument = `-- name: UpdateKbDocument :exec
|
|
UPDATE knowledge_base_documents
|
|
SET title = $2,
|
|
source_type = $3,
|
|
content = $4,
|
|
file_name = COALESCE($5, file_name),
|
|
mime_type = COALESCE($6, mime_type),
|
|
size_bytes = $7,
|
|
status = 'pending',
|
|
error_message = NULL,
|
|
chunk_count = 0,
|
|
learned_at = NULL,
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateKbDocumentParams struct {
|
|
ID string
|
|
Title string
|
|
SourceType string
|
|
Content string
|
|
FileName *string
|
|
MimeType *string
|
|
SizeBytes int64
|
|
}
|
|
|
|
func (q *Queries) UpdateKbDocument(ctx context.Context, arg UpdateKbDocumentParams) error {
|
|
_, err := q.db.Exec(ctx, updateKbDocument,
|
|
arg.ID,
|
|
arg.Title,
|
|
arg.SourceType,
|
|
arg.Content,
|
|
arg.FileName,
|
|
arg.MimeType,
|
|
arg.SizeBytes,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const updateKbDocumentMetadata = `-- name: UpdateKbDocumentMetadata :exec
|
|
UPDATE knowledge_base_documents
|
|
SET title = $2,
|
|
source_type = $3,
|
|
file_name = COALESCE($4, file_name),
|
|
mime_type = COALESCE($5, mime_type),
|
|
size_bytes = $6,
|
|
updated_at = now()
|
|
WHERE id = $1
|
|
`
|
|
|
|
type UpdateKbDocumentMetadataParams struct {
|
|
ID string
|
|
Title string
|
|
SourceType string
|
|
FileName *string
|
|
MimeType *string
|
|
SizeBytes int64
|
|
}
|
|
|
|
func (q *Queries) UpdateKbDocumentMetadata(ctx context.Context, arg UpdateKbDocumentMetadataParams) error {
|
|
_, err := q.db.Exec(ctx, updateKbDocumentMetadata,
|
|
arg.ID,
|
|
arg.Title,
|
|
arg.SourceType,
|
|
arg.FileName,
|
|
arg.MimeType,
|
|
arg.SizeBytes,
|
|
)
|
|
return err
|
|
}
|
|
|
|
const updateKbResource = `-- name: UpdateKbResource :exec
|
|
UPDATE knowledge_bases
|
|
SET name = CASE WHEN $1::jsonb ? 'name' THEN ($1::jsonb ->> 'name')::text ELSE name END,
|
|
description = CASE WHEN $1::jsonb ? 'description' THEN ($1::jsonb ->> 'description')::text ELSE description END,
|
|
enabled = CASE WHEN $1::jsonb ? 'enabled' THEN ($1::jsonb ->> 'enabled')::boolean ELSE enabled END,
|
|
embedding_model_id = CASE WHEN $1::jsonb ? 'embedding_model_id'
|
|
THEN NULLIF($1::jsonb ->> 'embedding_model_id', '')::uuid ELSE embedding_model_id END,
|
|
embedding_dimension = CASE WHEN $1::jsonb ? 'embedding_dimension'
|
|
THEN NULLIF($1::jsonb ->> 'embedding_dimension', '')::integer ELSE embedding_dimension END,
|
|
fusion_config = CASE WHEN $1::jsonb ? 'fusion_config'
|
|
THEN ($1::jsonb -> 'fusion_config') ELSE fusion_config END,
|
|
revision = revision + 1,
|
|
updated_at = now()
|
|
WHERE id = ($1::jsonb ->> 'id')::uuid
|
|
`
|
|
|
|
func (q *Queries) UpdateKbResource(ctx context.Context, dollar_1 []byte) error {
|
|
_, err := q.db.Exec(ctx, updateKbResource, dollar_1)
|
|
return err
|
|
}
|