Changeset 0.21.4 (#90)
This commit is contained in:
@@ -43,6 +43,7 @@ type Stores struct {
|
||||
Notifications NotificationStore
|
||||
NotifPrefs NotificationPreferenceStore
|
||||
Workspaces WorkspaceStore
|
||||
GitCredentials GitCredentialStore
|
||||
}
|
||||
|
||||
// =========================================
|
||||
@@ -545,6 +546,20 @@ type WorkspaceStore interface {
|
||||
DeleteChunksByFile(ctx context.Context, fileID string) error
|
||||
SimilaritySearch(ctx context.Context, workspaceID string, queryVec []float64, threshold float64, limit int) ([]models.WorkspaceChunkResult, error)
|
||||
UpdateFileIndexStatus(ctx context.Context, fileID, status string, chunkCount int) error
|
||||
|
||||
// Git (v0.21.4)
|
||||
SetGitLastSync(ctx context.Context, workspaceID string, t time.Time) error
|
||||
}
|
||||
|
||||
// =========================================
|
||||
// GIT CREDENTIAL STORE (v0.21.4)
|
||||
// =========================================
|
||||
|
||||
type GitCredentialStore interface {
|
||||
Create(ctx context.Context, cred *models.GitCredential) error
|
||||
GetByID(ctx context.Context, id string) (*models.GitCredential, error)
|
||||
ListByUser(ctx context.Context, userID string) ([]models.GitCredential, error)
|
||||
Delete(ctx context.Context, id, userID string) error
|
||||
}
|
||||
|
||||
// =========================================
|
||||
|
||||
76
server/store/postgres/git_credentials.go
Normal file
76
server/store/postgres/git_credentials.go
Normal file
@@ -0,0 +1,76 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
)
|
||||
|
||||
// GitCredentialStore implements store.GitCredentialStore for PostgreSQL.
|
||||
type GitCredentialStore struct{}
|
||||
|
||||
func (s *GitCredentialStore) Create(ctx context.Context, cred *models.GitCredential) error {
|
||||
if cred.ID != "" {
|
||||
return DB.QueryRowContext(ctx, `
|
||||
INSERT INTO git_credentials (id, user_id, name, auth_type, encrypted_data, nonce)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)
|
||||
RETURNING created_at`,
|
||||
cred.ID, cred.UserID, cred.Name, cred.AuthType,
|
||||
cred.EncryptedData, cred.Nonce,
|
||||
).Scan(&cred.CreatedAt)
|
||||
}
|
||||
return DB.QueryRowContext(ctx, `
|
||||
INSERT INTO git_credentials (user_id, name, auth_type, encrypted_data, nonce)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id, created_at`,
|
||||
cred.UserID, cred.Name, cred.AuthType,
|
||||
cred.EncryptedData, cred.Nonce,
|
||||
).Scan(&cred.ID, &cred.CreatedAt)
|
||||
}
|
||||
|
||||
func (s *GitCredentialStore) GetByID(ctx context.Context, id string) (*models.GitCredential, error) {
|
||||
var c models.GitCredential
|
||||
err := DB.QueryRowContext(ctx, `
|
||||
SELECT id, user_id, name, auth_type, encrypted_data, nonce, created_at
|
||||
FROM git_credentials WHERE id = $1`, id,
|
||||
).Scan(&c.ID, &c.UserID, &c.Name, &c.AuthType,
|
||||
&c.EncryptedData, &c.Nonce, &c.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (s *GitCredentialStore) ListByUser(ctx context.Context, userID string) ([]models.GitCredential, error) {
|
||||
rows, err := DB.QueryContext(ctx, `
|
||||
SELECT id, user_id, name, auth_type, encrypted_data, nonce, created_at
|
||||
FROM git_credentials WHERE user_id = $1 ORDER BY created_at DESC`, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var creds []models.GitCredential
|
||||
for rows.Next() {
|
||||
var c models.GitCredential
|
||||
if err := rows.Scan(&c.ID, &c.UserID, &c.Name, &c.AuthType,
|
||||
&c.EncryptedData, &c.Nonce, &c.CreatedAt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
creds = append(creds, c)
|
||||
}
|
||||
return creds, rows.Err()
|
||||
}
|
||||
|
||||
func (s *GitCredentialStore) Delete(ctx context.Context, id, userID string) error {
|
||||
res, err := DB.ExecContext(ctx, `DELETE FROM git_credentials WHERE id = $1 AND user_id = $2`, id, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
if n == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -36,5 +36,6 @@ func NewStores(db *sql.DB) store.Stores {
|
||||
Notifications: NewNotificationStore(),
|
||||
NotifPrefs: NewNotificationPreferenceStore(),
|
||||
Workspaces: NewWorkspaceStore(),
|
||||
GitCredentials: &GitCredentialStore{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ func NewWorkspaceStore() *WorkspaceStore { return &WorkspaceStore{} }
|
||||
|
||||
// ── columns ────────────────────────────────
|
||||
|
||||
const workspaceCols = `id, owner_type, owner_id, name, root_path, max_bytes, status, indexing_enabled, created_at, updated_at`
|
||||
const workspaceCols = `id, owner_type, owner_id, name, root_path, max_bytes, status, indexing_enabled, git_remote_url, git_branch, git_credential_id, git_last_sync, created_at, updated_at`
|
||||
const workspaceFileCols = `id, workspace_id, path, is_directory, content_type, size_bytes, sha256, index_status, chunk_count, created_at, updated_at`
|
||||
|
||||
// ── scanners ───────────────────────────────
|
||||
@@ -24,9 +24,12 @@ const workspaceFileCols = `id, workspace_id, path, is_directory, content_type, s
|
||||
func scanWorkspace(row interface{ Scan(dest ...interface{}) error }) (*models.Workspace, error) {
|
||||
var w models.Workspace
|
||||
var maxBytes sql.NullInt64
|
||||
var gitRemote, gitBranch, gitCredID sql.NullString
|
||||
var gitSync sql.NullTime
|
||||
err := row.Scan(
|
||||
&w.ID, &w.OwnerType, &w.OwnerID, &w.Name,
|
||||
&w.RootPath, &maxBytes, &w.Status, &w.IndexingEnabled,
|
||||
&gitRemote, &gitBranch, &gitCredID, &gitSync,
|
||||
&w.CreatedAt, &w.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -35,6 +38,18 @@ func scanWorkspace(row interface{ Scan(dest ...interface{}) error }) (*models.Wo
|
||||
if maxBytes.Valid {
|
||||
w.MaxBytes = &maxBytes.Int64
|
||||
}
|
||||
if gitRemote.Valid {
|
||||
w.GitRemoteURL = &gitRemote.String
|
||||
}
|
||||
if gitBranch.Valid {
|
||||
w.GitBranch = &gitBranch.String
|
||||
}
|
||||
if gitCredID.Valid {
|
||||
w.GitCredentialID = &gitCredID.String
|
||||
}
|
||||
if gitSync.Valid {
|
||||
w.GitLastSync = &gitSync.Time
|
||||
}
|
||||
return &w, nil
|
||||
}
|
||||
|
||||
@@ -106,6 +121,15 @@ func (s *WorkspaceStore) Update(ctx context.Context, id string, patch models.Wor
|
||||
if patch.IndexingEnabled != nil {
|
||||
b.Set("indexing_enabled", *patch.IndexingEnabled)
|
||||
}
|
||||
if patch.GitRemoteURL != nil {
|
||||
b.Set("git_remote_url", *patch.GitRemoteURL)
|
||||
}
|
||||
if patch.GitBranch != nil {
|
||||
b.Set("git_branch", *patch.GitBranch)
|
||||
}
|
||||
if patch.GitCredentialID != nil {
|
||||
b.Set("git_credential_id", *patch.GitCredentialID)
|
||||
}
|
||||
if !b.HasSets() {
|
||||
return nil
|
||||
}
|
||||
@@ -365,3 +389,10 @@ func (s *WorkspaceStore) UpdateFileIndexStatus(ctx context.Context, fileID, stat
|
||||
status, chunkCount, fileID)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *WorkspaceStore) SetGitLastSync(ctx context.Context, workspaceID string, t time.Time) error {
|
||||
_, err := DB.ExecContext(ctx,
|
||||
`UPDATE workspaces SET git_last_sync = $1, updated_at = NOW() WHERE id = $2`,
|
||||
t, workspaceID)
|
||||
return err
|
||||
}
|
||||
|
||||
86
server/store/sqlite/git_credentials.go
Normal file
86
server/store/sqlite/git_credentials.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
|
||||
"git.gobha.me/xcaliber/chat-switchboard/models"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// GitCredentialStore implements store.GitCredentialStore for SQLite.
|
||||
type GitCredentialStore struct{}
|
||||
|
||||
func (s *GitCredentialStore) Create(ctx context.Context, cred *models.GitCredential) error {
|
||||
if cred.ID == "" {
|
||||
cred.ID = uuid.NewString()
|
||||
}
|
||||
_, err := DB.ExecContext(ctx, `
|
||||
INSERT INTO git_credentials (id, user_id, name, auth_type, encrypted_data, nonce, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, datetime('now'))`,
|
||||
cred.ID, cred.UserID, cred.Name, cred.AuthType,
|
||||
base64.StdEncoding.EncodeToString(cred.EncryptedData),
|
||||
base64.StdEncoding.EncodeToString(cred.Nonce),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Read back created_at
|
||||
return DB.QueryRowContext(ctx,
|
||||
`SELECT created_at FROM git_credentials WHERE id = ?`, cred.ID,
|
||||
).Scan(st(&cred.CreatedAt))
|
||||
}
|
||||
|
||||
func (s *GitCredentialStore) GetByID(ctx context.Context, id string) (*models.GitCredential, error) {
|
||||
var c models.GitCredential
|
||||
var encData, nonce string
|
||||
err := DB.QueryRowContext(ctx, `
|
||||
SELECT id, user_id, name, auth_type, encrypted_data, nonce, created_at
|
||||
FROM git_credentials WHERE id = ?`, id,
|
||||
).Scan(&c.ID, &c.UserID, &c.Name, &c.AuthType,
|
||||
&encData, &nonce, st(&c.CreatedAt))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.EncryptedData, _ = base64.StdEncoding.DecodeString(encData)
|
||||
c.Nonce, _ = base64.StdEncoding.DecodeString(nonce)
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (s *GitCredentialStore) ListByUser(ctx context.Context, userID string) ([]models.GitCredential, error) {
|
||||
rows, err := DB.QueryContext(ctx, `
|
||||
SELECT id, user_id, name, auth_type, encrypted_data, nonce, created_at
|
||||
FROM git_credentials WHERE user_id = ? ORDER BY created_at DESC`, userID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var creds []models.GitCredential
|
||||
for rows.Next() {
|
||||
var c models.GitCredential
|
||||
var encData, nonce string
|
||||
if err := rows.Scan(&c.ID, &c.UserID, &c.Name, &c.AuthType,
|
||||
&encData, &nonce, st(&c.CreatedAt)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.EncryptedData, _ = base64.StdEncoding.DecodeString(encData)
|
||||
c.Nonce, _ = base64.StdEncoding.DecodeString(nonce)
|
||||
creds = append(creds, c)
|
||||
}
|
||||
return creds, rows.Err()
|
||||
}
|
||||
|
||||
func (s *GitCredentialStore) Delete(ctx context.Context, id, userID string) error {
|
||||
res, err := DB.ExecContext(ctx, `DELETE FROM git_credentials WHERE id = ? AND user_id = ?`, id, userID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
n, _ := res.RowsAffected()
|
||||
if n == 0 {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -36,5 +36,6 @@ func NewStores(db *sql.DB) store.Stores {
|
||||
Notifications: NewNotificationStore(),
|
||||
NotifPrefs: NewNotificationPreferenceStore(),
|
||||
Workspaces: NewWorkspaceStore(),
|
||||
GitCredentials: &GitCredentialStore{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ func NewWorkspaceStore() *WorkspaceStore { return &WorkspaceStore{} }
|
||||
|
||||
// ── columns ────────────────────────────────
|
||||
|
||||
const workspaceCols = `id, owner_type, owner_id, name, root_path, max_bytes, status, indexing_enabled, created_at, updated_at`
|
||||
const workspaceCols = `id, owner_type, owner_id, name, root_path, max_bytes, status, indexing_enabled, git_remote_url, git_branch, git_credential_id, git_last_sync, created_at, updated_at`
|
||||
const workspaceFileCols = `id, workspace_id, path, is_directory, content_type, size_bytes, sha256, index_status, chunk_count, created_at, updated_at`
|
||||
|
||||
// ── scanners ───────────────────────────────
|
||||
@@ -28,9 +28,11 @@ const workspaceFileCols = `id, workspace_id, path, is_directory, content_type, s
|
||||
func scanWorkspace(row interface{ Scan(dest ...interface{}) error }) (*models.Workspace, error) {
|
||||
var w models.Workspace
|
||||
var maxBytes sql.NullInt64
|
||||
var gitRemote, gitBranch, gitCredID sql.NullString
|
||||
err := row.Scan(
|
||||
&w.ID, &w.OwnerType, &w.OwnerID, &w.Name,
|
||||
&w.RootPath, &maxBytes, &w.Status, &w.IndexingEnabled,
|
||||
&gitRemote, &gitBranch, &gitCredID, stN(&w.GitLastSync),
|
||||
st(&w.CreatedAt), st(&w.UpdatedAt),
|
||||
)
|
||||
if err != nil {
|
||||
@@ -39,6 +41,15 @@ func scanWorkspace(row interface{ Scan(dest ...interface{}) error }) (*models.Wo
|
||||
if maxBytes.Valid {
|
||||
w.MaxBytes = &maxBytes.Int64
|
||||
}
|
||||
if gitRemote.Valid {
|
||||
w.GitRemoteURL = &gitRemote.String
|
||||
}
|
||||
if gitBranch.Valid {
|
||||
w.GitBranch = &gitBranch.String
|
||||
}
|
||||
if gitCredID.Valid {
|
||||
w.GitCredentialID = &gitCredID.String
|
||||
}
|
||||
return &w, nil
|
||||
}
|
||||
|
||||
@@ -107,6 +118,15 @@ func (s *WorkspaceStore) Update(ctx context.Context, id string, patch models.Wor
|
||||
if patch.IndexingEnabled != nil {
|
||||
b.Set("indexing_enabled", *patch.IndexingEnabled)
|
||||
}
|
||||
if patch.GitRemoteURL != nil {
|
||||
b.Set("git_remote_url", *patch.GitRemoteURL)
|
||||
}
|
||||
if patch.GitBranch != nil {
|
||||
b.Set("git_branch", *patch.GitBranch)
|
||||
}
|
||||
if patch.GitCredentialID != nil {
|
||||
b.Set("git_credential_id", *patch.GitCredentialID)
|
||||
}
|
||||
if !b.HasSets() {
|
||||
return nil
|
||||
}
|
||||
@@ -406,6 +426,13 @@ func (s *WorkspaceStore) UpdateFileIndexStatus(ctx context.Context, fileID, stat
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *WorkspaceStore) SetGitLastSync(ctx context.Context, workspaceID string, t time.Time) error {
|
||||
_, err := DB.ExecContext(ctx,
|
||||
`UPDATE workspaces SET git_last_sync = ?, updated_at = datetime('now') WHERE id = ?`,
|
||||
t.Format(timeFmt), workspaceID)
|
||||
return err
|
||||
}
|
||||
|
||||
// wsCosineSimilarity computes cosine similarity between two vectors.
|
||||
// Named to avoid collision with the KB package-level function.
|
||||
func wsCosineSimilarity(a, b []float64) float64 {
|
||||
|
||||
Reference in New Issue
Block a user