Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
98 lines
3.8 KiB
Go
98 lines
3.8 KiB
Go
package repository
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gochat/gochat/internal/model"
|
|
"github.com/pgvector/pgvector-go"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// CaptainAssistantResponseRepo provides data access for CaptainAssistantResponse.
|
|
type CaptainAssistantResponseRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func (r *CaptainAssistantResponseRepo) DB() *gorm.DB {
|
|
return r.db
|
|
}
|
|
|
|
func NewCaptainAssistantResponseRepo(db *gorm.DB) *CaptainAssistantResponseRepo {
|
|
return &CaptainAssistantResponseRepo{db: db}
|
|
}
|
|
|
|
func (r *CaptainAssistantResponseRepo) Create(ctx context.Context, resp *model.CaptainAssistantResponse) error {
|
|
return r.db.WithContext(ctx).Create(resp).Error
|
|
}
|
|
|
|
func (r *CaptainAssistantResponseRepo) GetByID(ctx context.Context, id uint) (*model.CaptainAssistantResponse, error) {
|
|
var resp model.CaptainAssistantResponse
|
|
if err := r.db.WithContext(ctx).Preload("Assistant").First(&resp, id).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
}
|
|
|
|
func (r *CaptainAssistantResponseRepo) GetByAccountAndID(ctx context.Context, accountID, id uint) (*model.CaptainAssistantResponse, error) {
|
|
var resp model.CaptainAssistantResponse
|
|
if err := r.db.WithContext(ctx).Preload("Assistant").Where("account_id = ? AND id = ?", accountID, id).First(&resp).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &resp, nil
|
|
}
|
|
|
|
func (r *CaptainAssistantResponseRepo) Update(ctx context.Context, resp *model.CaptainAssistantResponse) error {
|
|
return r.db.WithContext(ctx).Save(resp).Error
|
|
}
|
|
|
|
func (r *CaptainAssistantResponseRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&model.CaptainAssistantResponse{}, id).Error
|
|
}
|
|
|
|
func (r *CaptainAssistantResponseRepo) DeleteByAccount(ctx context.Context, accountID, id uint) error {
|
|
return r.db.WithContext(ctx).Where("account_id = ? AND id = ?", accountID, id).Delete(&model.CaptainAssistantResponse{}).Error
|
|
}
|
|
|
|
func (r *CaptainAssistantResponseRepo) ListByAssistant(ctx context.Context, assistantID uint, offset, limit int) ([]model.CaptainAssistantResponse, int64, error) {
|
|
var responses []model.CaptainAssistantResponse
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.CaptainAssistantResponse{}).Where("assistant_id = ?", assistantID)
|
|
db.Count(&count)
|
|
if err := db.Offset(offset).Limit(limit).Find(&responses).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return responses, count, nil
|
|
}
|
|
|
|
func (r *CaptainAssistantResponseRepo) ListByDocument(ctx context.Context, documentableID uint, documentableType string, offset, limit int) ([]model.CaptainAssistantResponse, int64, error) {
|
|
var responses []model.CaptainAssistantResponse
|
|
var count int64
|
|
db := r.db.WithContext(ctx).Model(&model.CaptainAssistantResponse{}).
|
|
Where("documentable_id = ? AND documentable_type = ?", documentableID, documentableType)
|
|
db.Count(&count)
|
|
if err := db.Offset(offset).Limit(limit).Find(&responses).Error; err != nil {
|
|
return nil, 0, err
|
|
}
|
|
return responses, count, nil
|
|
}
|
|
|
|
// SimilaritySearch performs pgvector cosine similarity search.
|
|
// Reference: Chatwoot Captain::AssistantResponsesSearchService
|
|
func (r *CaptainAssistantResponseRepo) SimilaritySearch(ctx context.Context, assistantID uint, embedding pgvector.Vector, limit int) ([]model.CaptainAssistantResponse, error) {
|
|
var responses []model.CaptainAssistantResponse
|
|
// Cosine distance (<=>) orders by closest first.
|
|
if err := r.db.WithContext(ctx).
|
|
Where("assistant_id = ? AND status = ?", assistantID, model.ResponseStatusApproved).
|
|
Order(gorm.Expr("embedding <=> ?", embedding)).
|
|
Limit(limit).
|
|
Find(&responses).Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return responses, nil
|
|
}
|
|
|
|
// SearchByEmbedding is an alias for SimilaritySearch for backward compatibility.
|
|
func (r *CaptainAssistantResponseRepo) SearchByEmbedding(ctx context.Context, assistantID uint, embedding pgvector.Vector, limit int) ([]model.CaptainAssistantResponse, error) {
|
|
return r.SimilaritySearch(ctx, assistantID, embedding, limit)
|
|
}
|