refactor: 后端仓库层收口(A1:课程/产品/素材)+ 收进工作区既有对象化重构

本提交含两部分。第一部分是本轮工作;第二部分是此前一直留在工作区、
从未提交的对象化重构,与第一部分在文件上互相咬合(internal/repository
整个包都是未跟踪状态,且 api 层已有文件引用它),无法拆成两个可编译的提交。

一、仓库层收口 A1 批(本轮工作)

把 api 层手写的 store.DB 查询收进具名仓库方法,只给真正获益的对象做方法,
不机械包裹全量。本批迁移 22 处裸查询(courses.go 9 / media.go 12 / products.go 1),
新增方法:

- MediaFileRepo.ListByBind / ListForAudit / MarkExtracted
- KnowledgeChunkRepo.CountByMediaFile
- ProductRepo.GetVisibleByID

两条业务口径改由仓库单点持有,避免各处手写漂移:
「只有 approved 素材出现在课程详情」与「已停用产品不在课程详情露出」。

修掉两个真实缺陷:
- ProductRepo.GetByID 缺 Where 条件。此前 GET /api/products/{id} 对任意 id 都返回
  第一条产品、对不存在的 id 返回 200,且 PUT /api/products/{id} 会覆盖第一条产品
  —— 数据损坏级。全仓扫描确认这是唯一一处同型写法。
- ProductRepo.Delete 写 status="deleted",而 DELETE 处理器文档与回包都声称
  "inactive",接口在说谎;管理员用 status=all 拉列表会看到前端不认识的状态。
  已对齐为 inactive(与 CourseRepo.Delete 一致)。

删除 8 个零调用且列名不存在的死方法(一调即 SQL 报错):
- media_file 上的 file_path / file_type / approval_status 三列并不存在,
  GetByPath / ListByType / UpdateStatus 全废
- knowledge_chunk 上的 space_id 列不存在(模型早已改为 knowledge_space_key),
  List / Total / ListBySpaceIDs / DeleteBySpace / SearchByVector 全废
取舍边界:能对当前 schema 跑通的死方法保留,跑不通的删或修。

CourseRepo.List 补齐 status=all 档(此前传给它会当作 status='all' 过滤出空列表)。
该方法此前零调用,现与产品列表语义对齐。

验证:go build ./... 与 go test ./... 全绿;另用真实 HTTP 请求验证 34 项
(课程 17 / 产品 3 / 素材 14),跑在数据库副本与独立 KB_DATA_DIR 上,
含 multipart 真上传 → 审批 → pdftotext 提取 → 分片入库的完整链路。

二、此前未提交的对象化重构(非本轮工作)

- 新增 internal/repository 仓库层、connectors、skills、specialists、xapps、jsonutil,
  model/task_record|task_run|task_artifact、api/task_runtime|action_definition|chat_message
- 删除 api/app_definition、connectors、my_app_center、notification、office_skill、
  export_docx|pptx|xlsx、official_account_* 等,随 XApp/Skill/Specialist/Connector
  可插拔打包方向(AR10/AR11)调整
- 资产目录归位:backend-go/knowledge_source → assets/knowledge/source、
  training_materials → assets/training/materials;README 内相对路径同步加深两级;
  deploy env 补 ASSET_ROOT_DIR 并改 KNOWLEDGE_SOURCE_DIR / TRAINING_MATERIALS_DIR
- 前端新增 skills/ specialists/ connectors/ xapps/ 目录与对应页面

验证:前端 npm run build 通过(7.26s)。

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
eaiadmin
2026-09-19 01:23:51 +08:00
co-authored by Claude Code
parent ddd2d2cbd8
commit 14f303459e
448 changed files with 18792 additions and 17027 deletions
@@ -13,36 +13,45 @@ import (
// 按用户算力点计费
// ──────────────────────────────────────────────
// AI 能力常量
// AI 用量类型常量
const (
CapabilityAIChat = "ai_chat" // PathCoach 对话(每轮扣 1 点)
CapabilityTextGen = "text_gen" // 快捷动作(情景演练/查佣金/产品对比,扣 1 点)
CapabilityImageGen = "image_gen" // 文生图(按次扣点)
CapabilityEmbed = "embed" // 知识检索内部 embedding(不扣点,仅审计)
CapabilityEssayGrade = "essay_grade" // 简答题 LLM 评分(系统自动,不扣点,仅审计)
UsageKindAIChat = "ai_chat" // PathCoach 对话(每轮扣 1 点)
UsageKindTextGen = "text_gen" // 快捷动作(情景演练/查佣金/产品对比,扣 1 点)
UsageKindImageGen = "image_gen" // 文生图(按次扣点)
UsageKindEmbed = "embed" // 知识检索内部 embedding(不扣点,仅审计)
UsageKindEssayGrade = "essay_grade" // 简答题 LLM 评分(系统自动,不扣点,仅审计)
// 以下 5 类为工具型 AI 调用,当前只审计、不扣点:
// 它们不在 UsageKindCredits 表里,ComputeCredits 查不到即返回 0。
// 将来若要计费,只需把它们加进 UsageKindCredits,扣点逻辑自动生效。
UsageKindBatchExtract = "batch_extract"
UsageKindContractReview = "contract_review"
UsageKindAudioTranscribe = "audio_transcribe"
UsageKindCopyProofread = "copy_proofread"
UsageKindDocumentTranslate = "document_translate"
)
// CapabilityCredits 各能力扣点成本
var CapabilityCredits = map[string]int{
CapabilityAIChat: 1,
CapabilityTextGen: 1,
CapabilityImageGen: 1,
CapabilityEmbed: 0,
CapabilityEssayGrade: 0,
// UsageKindCredits 各用量类型扣点成本
var UsageKindCredits = map[string]int{
UsageKindAIChat: 1,
UsageKindTextGen: 1,
UsageKindImageGen: 1,
UsageKindEmbed: 0,
UsageKindEssayGrade: 0,
}
// ComputeCredits 扣点决策:仅「成功」才扣点;失败不扣。
func ComputeCredits(capability string, success bool) int {
func ComputeCredits(usageKind string, success bool) int {
if !success {
return 0
}
return CapabilityCredits[capability]
return UsageKindCredits[usageKind]
}
// LogEntry 一次 AI 调用的审计入参
type LogEntry struct {
UserID uint
Capability string
UsageKind string
// SpecialistKey 本次调用以哪个专员的身份进行;空 = 通用助手
SpecialistKey string
Provider string
@@ -57,14 +66,14 @@ type LogEntry struct {
// LogCall 写 ai_call_log;成功且需扣点时从用户余额扣点。审计写入失败不阻断主流程。
func LogCall(e LogEntry) {
credits := ComputeCredits(e.Capability, e.Success)
credits := ComputeCredits(e.UsageKind, e.Success)
status := "success"
if !e.Success {
status = "failed"
}
rec := model.AiCallLog{
UserID: e.UserID,
Capability: e.Capability,
UsageKind: e.UsageKind,
SpecialistKey: e.SpecialistKey,
Provider: e.Provider,
AIRouteID: e.AIRouteID,
@@ -11,8 +11,6 @@ import (
"time"
"eai_agentplatform/backend/internal/config"
"eai_agentplatform/backend/internal/model"
"eai_agentplatform/backend/internal/store"
)
// ──────────────────────────────────────────────
@@ -64,18 +62,6 @@ func NewClient(aiRoute *config.RouteConfig) *Client {
}
}
// NewClientLegacy 兼容旧接口(从 LLMConfig 创建)
func NewClientLegacy(cfg LLMConfig) *Client {
return &Client{
baseURL: strings.TrimRight(cfg.BaseURL, "/"),
apiKey: cfg.APIKey,
model: cfg.Model,
maxTokens: cfg.MaxTokens,
temperature: cfg.Temperature,
hc: &http.Client{Timeout: 120 * time.Second},
}
}
func (c *Client) url(path string) string {
return c.baseURL + path
}
@@ -450,59 +436,6 @@ func requiresAPIKey(aiRoute *config.RouteConfig) bool {
return provider == "openrouter" || provider == "openai"
}
// ──────────────────────────────────────────────
// 配置解析(兼容旧接口)
// ──────────────────────────────────────────────
// LLMConfig 简单的 LLM 连接配置(旧版,逐步淘汰)
type LLMConfig struct {
BaseURL string
APIKey string
Model string
EmbedModel string
MaxTokens int
Temperature float64
}
// ResolveLLM 旧版:从 DB/环境 解析 LLM 配置
func ResolveLLM(cfg *config.Config) (LLMConfig, bool) {
get := func(key, def string) string {
var sc model.SystemConfig
if err := store.DB.Where("config_key = ?", key).First(&sc).Error; err == nil && strings.TrimSpace(sc.ConfigValue) != "" {
return sc.ConfigValue
}
return def
}
baseURL := get("llm_base_url", cfg.LLMBaseURL)
apiKey := get("llm_api_key", cfg.LLMAPIKey)
modelName := get("llm_model", cfg.LLMModel)
embedModel := get("embed_model", cfg.EmbedModel)
// DB 空时降级到 JSON secrets(flat 格式)
if baseURL == "" || apiKey == "" {
if baseURL == "" {
baseURL = config.GetProviderBaseURL("ollama")
}
if apiKey == "" {
apiKey = config.GetProviderAPIKey("ollama")
}
}
c := LLMConfig{
BaseURL: baseURL,
APIKey: apiKey,
Model: modelName,
EmbedModel: embedModel,
MaxTokens: 2048,
Temperature: 0.7,
}
if c.BaseURL == "" || c.Model == "" {
return c, false
}
return c, true
}
func truncate(s string, n int) string {
r := []rune(s)
if len(r) <= n {