refactor backend packages and global db service

This commit is contained in:
2026-07-18 21:58:15 +08:00
parent 62bd3d0455
commit 7800b07d42
51 changed files with 719 additions and 647 deletions

View File

@@ -0,0 +1,18 @@
package models
import "time"
type SenlinAgentAICallLog struct {
ID uint `gorm:"primaryKey"`
UserID uint `gorm:"index;not null"`
Provider string `gorm:"not null"`
UsedKeyType string `gorm:"not null"`
Action string `gorm:"not null"`
Status string `gorm:"not null"`
Error string
CreatedAt time.Time
}
func (SenlinAgentAICallLog) TableName() string {
return "senlin_agent_ai_call_logs"
}

View File

@@ -0,0 +1,16 @@
package models
import "time"
type SenlinAgentAIKey struct {
ID uint `gorm:"primaryKey"`
UserID uint `gorm:"uniqueIndex;not null"`
Provider string `gorm:"not null"`
EncryptedAPIKey string `gorm:"not null"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (SenlinAgentAIKey) TableName() string {
return "senlin_agent_ai_keys"
}

View File

@@ -0,0 +1,17 @@
package models
import "time"
type SenlinAgentAISession struct {
ID uint `gorm:"primaryKey"`
ProjectID uint `gorm:"index;not null"`
CreatedBy uint `gorm:"index;not null"`
Title string `gorm:"not null"`
Context string `gorm:"type:text"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (SenlinAgentAISession) TableName() string {
return "senlin_agent_ai_sessions"
}

View File

@@ -0,0 +1,19 @@
package models
import "time"
type SenlinAgentInboxItem struct {
ID uint `gorm:"primaryKey"`
ProjectID uint `gorm:"index;not null"`
CreatedBy uint `gorm:"index;not null"`
SourceType string `gorm:"not null"`
Title string
Body string
Status string `gorm:"not null;default:open"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (SenlinAgentInboxItem) TableName() string {
return "senlin_agent_inbox_items"
}

View File

@@ -0,0 +1,39 @@
package models
import (
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
var DBService *gorm.DB
func New(dsn string) error {
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
return err
}
if err := AutoMigrate(db); err != nil {
return err
}
DBService = db
return nil
}
func AutoMigrate(database *gorm.DB) error {
return database.AutoMigrate(
&SenlinAgentUser{},
&SenlinAgentProject{},
&SenlinAgentInboxItem{},
&SenlinAgentTask{},
&SenlinAgentNote{},
&SenlinAgentSource{},
&SenlinAgentAISession{},
&SenlinAgentTag{},
&SenlinAgentProjectEvent{},
&SenlinAgentAIKey{},
&SenlinAgentAICallLog{},
&SenlinAgentTaskShare{},
)
}

View File

@@ -0,0 +1,18 @@
package models
import "time"
type SenlinAgentNote struct {
ID uint `gorm:"primaryKey"`
ProjectID uint `gorm:"index;not null"`
CreatedBy uint `gorm:"index;not null"`
SourceInboxItemID *uint `gorm:"index"`
Title string `gorm:"not null"`
Markdown string `gorm:"type:text"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (SenlinAgentNote) TableName() string {
return "senlin_agent_notes"
}

View File

@@ -0,0 +1,24 @@
//go:build integration
package models
import (
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestPostgresPing(t *testing.T) {
dsn := os.Getenv("DATABASE_DSN")
if dsn == "" {
dsn = os.Getenv("DATABASE_URL")
}
require.NotEmpty(t, dsn, "DATABASE_DSN or DATABASE_URL is required for integration tests")
database, err := Open(dsn)
require.NoError(t, err)
sqlDB, err := database.DB()
require.NoError(t, err)
require.NoError(t, sqlDB.Ping())
}

View File

@@ -0,0 +1,16 @@
package models
import "time"
type SenlinAgentProject struct {
ID uint `gorm:"primaryKey"`
OwnerID uint `gorm:"index;not null"`
Name string `gorm:"not null"`
Description string
CreatedAt time.Time
UpdatedAt time.Time
}
func (SenlinAgentProject) TableName() string {
return "senlin_agent_projects"
}

View File

@@ -0,0 +1,18 @@
package models
import "time"
type SenlinAgentProjectEvent struct {
ID uint `gorm:"primaryKey"`
ProjectID uint `gorm:"index;not null"`
ActorID uint `gorm:"index;not null"`
EventType string `gorm:"not null"`
EntityType string `gorm:"not null"`
EntityID uint `gorm:"not null"`
Summary string `gorm:"not null"`
CreatedAt time.Time
}
func (SenlinAgentProjectEvent) TableName() string {
return "senlin_agent_project_events"
}

View File

@@ -0,0 +1,21 @@
package models
import "time"
type SenlinAgentSource struct {
ID uint `gorm:"primaryKey"`
ProjectID uint `gorm:"index;not null"`
CreatedBy uint `gorm:"index;not null"`
SourceInboxItemID *uint `gorm:"index"`
Kind string `gorm:"not null"`
Title string `gorm:"not null"`
URL string
FilePath string
ContentText string `gorm:"type:text"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (SenlinAgentSource) TableName() string {
return "senlin_agent_sources"
}

View File

@@ -0,0 +1,14 @@
package models
import "time"
type SenlinAgentTag struct {
ID uint `gorm:"primaryKey"`
ProjectID uint `gorm:"index;not null"`
Name string `gorm:"not null"`
CreatedAt time.Time
}
func (SenlinAgentTag) TableName() string {
return "senlin_agent_tags"
}

View File

@@ -0,0 +1,22 @@
package models
import "time"
type SenlinAgentTask struct {
ID uint `gorm:"primaryKey"`
ProjectID uint `gorm:"index;not null"`
CreatedBy uint `gorm:"index;not null"`
AssigneeID *uint `gorm:"index"`
SourceInboxItemID *uint `gorm:"index"`
Title string `gorm:"not null"`
Description string
Status string `gorm:"not null;default:open"`
SortOrder int `gorm:"not null;default:0"`
DueAt *time.Time
CreatedAt time.Time
UpdatedAt time.Time
}
func (SenlinAgentTask) TableName() string {
return "senlin_agent_tasks"
}

View File

@@ -0,0 +1,15 @@
package models
import "time"
type SenlinAgentTaskShare struct {
ID uint `gorm:"primaryKey"`
TaskID uint `gorm:"index;not null"`
ObjectType string `gorm:"not null"`
ObjectID uint `gorm:"not null"`
CreatedAt time.Time
}
func (SenlinAgentTaskShare) TableName() string {
return "senlin_agent_task_shares"
}

View File

@@ -0,0 +1,17 @@
package models
import "time"
type SenlinAgentUser struct {
ID uint `gorm:"primaryKey"`
Email string `gorm:"uniqueIndex;not null"`
DisplayName string `gorm:"not null"`
PasswordHash string `gorm:"not null"`
Role string `gorm:"not null;default:user"`
CreatedAt time.Time
UpdatedAt time.Time
}
func (SenlinAgentUser) TableName() string {
return "senlin_agent_users"
}