Files
runny 9058149576
Build & Publish (dsh-academy) / End-to-end inside dsh (latest, advisory) (push) Skipped
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Frontend / Docker multi-stage image (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-academy) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-academy) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-academy) / Publish to npm (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Helm / chart (push) Canceled after 0s
refactor: 项目更名 WeKnora → Academy
- 全量替换代码/配置/文档中的项目名(保留上游 GitHub 链接、weixin 文档域名、Docker Hub 镜像名)
- 重命名 32 个含项目名的文件/目录;Go module 路径改为 academy(含 client/cli 子模块)
- docreader proto 描述符采用等长替换,保持序列化字节布局有效
- Dockerfile.app 构建网络修复: browserskill 离线产物通道、PyPI/apt 国内镜像
- 运行验证: go build ✓ docker compose up ✓ 健康检查 ✓
2026-11-13 20:52:51 +08:00

140 lines
4.7 KiB
Go

package repository
import (
"context"
"errors"
"academy/internal/types"
"academy/internal/types/interfaces"
"gorm.io/gorm"
)
// ErrCustomAgentNotFound is returned when a custom agent is not found
var ErrCustomAgentNotFound = errors.New("custom agent not found")
// customAgentRepository implements the CustomAgentRepository interface
type customAgentRepository struct {
db *gorm.DB
}
// NewCustomAgentRepository creates a new custom agent repository
func NewCustomAgentRepository(db *gorm.DB) interfaces.CustomAgentRepository {
return &customAgentRepository{db: db}
}
// CreateAgent creates a new custom agent
func (r *customAgentRepository) CreateAgent(ctx context.Context, agent *types.CustomAgent) error {
return r.db.WithContext(ctx).Create(agent).Error
}
// GetAgentByID gets an agent by id and tenant
func (r *customAgentRepository) GetAgentByID(ctx context.Context, id string, tenantID uint64) (*types.CustomAgent, error) {
var agent types.CustomAgent
if err := r.db.WithContext(ctx).Where("id = ? AND tenant_id = ?", id, tenantID).First(&agent).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, ErrCustomAgentNotFound
}
return nil, err
}
return &agent, nil
}
// ListAgentsByTenantID lists all agents for a specific tenant
func (r *customAgentRepository) ListAgentsByTenantID(ctx context.Context, tenantID uint64) ([]*types.CustomAgent, error) {
var agents []*types.CustomAgent
if err := r.db.WithContext(ctx).
Where("tenant_id = ?", tenantID).
Order("created_at DESC").
Find(&agents).Error; err != nil {
return nil, err
}
return agents, nil
}
// UpdateAgent updates an agent
func (r *customAgentRepository) UpdateAgent(ctx context.Context, agent *types.CustomAgent) error {
return r.db.WithContext(ctx).Save(agent).Error
}
// DeleteAgent deletes an agent (soft delete)
func (r *customAgentRepository) DeleteAgent(ctx context.Context, id string, tenantID uint64) error {
return r.db.WithContext(ctx).Where("id = ? AND tenant_id = ?", id, tenantID).Delete(&types.CustomAgent{}).Error
}
// CountByModelID counts active agents whose config references modelID.
func (r *customAgentRepository) CountByModelID(
ctx context.Context, tenantID uint64, modelID string,
) (int64, error) {
var count int64
query := r.db.WithContext(ctx).
Model(&types.CustomAgent{}).
Where("tenant_id = ?", tenantID)
query = scopeCustomAgentsByModelID(query, modelID)
err := query.Count(&count).Error
return count, err
}
// ListModelUsages returns active agents that reference modelID. Only id, name,
// and config are loaded; the config is decoded locally and never returned.
func (r *customAgentRepository) ListModelUsages(
ctx context.Context, tenantID uint64, modelID string,
) ([]types.ModelUsageResource, error) {
rows := make([]*types.CustomAgent, 0)
query := r.db.WithContext(ctx).
Model(&types.CustomAgent{}).
Select("id", "name", "config").
Where("tenant_id = ?", tenantID)
query = scopeCustomAgentsByModelID(query, modelID)
if err := query.Order("name ASC, id ASC").Limit(types.ModelUsageListLimit).Find(&rows).Error; err != nil {
return nil, err
}
usages := make([]types.ModelUsageResource, 0, len(rows))
for _, row := range rows {
bindings := customAgentModelUsageBindings(row, modelID)
if len(bindings) == 0 {
continue
}
usages = append(usages, types.ModelUsageResource{
ID: row.ID,
Name: row.Name,
Bindings: bindings,
})
}
return usages, nil
}
// CountBySandboxConfigID counts agents pointing at a sandbox config.
//
// Used only to warn the admin which agents reference a config; never use it to
// refuse operations. Agent references are permanent state, so blocking on them
// would make credential rotation impossible.
func (r *customAgentRepository) CountBySandboxConfigID(
ctx context.Context, tenantID uint64, configID string,
) (int64, error) {
var count int64
query := r.db.WithContext(ctx).
Model(&types.CustomAgent{}).
Where("tenant_id = ?", tenantID)
query = scopeCustomAgentsBySandboxConfigID(query, configID)
err := query.Count(&count).Error
return count, err
}
// ListNamesBySandboxConfigID returns agent names pointing at a sandbox config.
//
// Used only to warn the admin which agents reference a config; never use it to
// refuse operations. Agent references are permanent state, so blocking on them
// would make credential rotation impossible.
func (r *customAgentRepository) ListNamesBySandboxConfigID(
ctx context.Context, tenantID uint64, configID string,
) ([]string, error) {
var names []string
query := r.db.WithContext(ctx).
Model(&types.CustomAgent{}).
Where("tenant_id = ?", tenantID)
query = scopeCustomAgentsBySandboxConfigID(query, configID)
err := query.Order("name ASC").Pluck("name", &names).Error
return names, err
}