feat(govai): 0617 优化首批 — 安全/私有化/深度研究/服务层/可观测性
借鉴 odysseus 的能力设计,全程净室实现、零 AGPL 代码、不引入 AGPL 依赖。
T1 提示注入防护: pkg/promptguard 包裹外部/知识库内容为不可信数据,buildMessages 移出 system 指令区。 T2 安全 CI: .github/workflows(ci+security: govulncheck/gitleaks/actionlint/hadolint/trivy)+dependabot+.hadolint.yaml;go.mod 加 toolchain go1.25.11 修复 20 个 stdlib CVE。 T3 管理员 2FA: 迁移 000016 + RFC6238 TOTP/备份码(pkg/auth, 零依赖) + 登录流程集成(后端)。 T4 本地模型: LLM/embedding 支持本地 vLLM/Ollama(OpenAI 兼容, 鉴权头条件发送, NoAuth) + docs/local-deploy.md。 T6 深度研究: 迁移 000017 + Python research-worker(净室多步流水线, 检索避开 SearXNG) + Go research 服务/handler/路由。 T7 service 层: 新增 internal/service/{research,twofa}, 2FA 业务逻辑从胖 handler 下沉, 接口注入可单测。 T10 缓存/可观测性: internal/cache(Redis+内存, 优雅降级) 接入 store 热点列表; Prometheus 指标+/metrics; docs/openapi.yaml。 验证: go build/vet/test ./... 全绿(8 包); research-worker 12 单测过; 真实 PG 应用迁移并烟测。
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
package research
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// pgxRepository 是基于 pgx 连接池的 Repository 实现。
|
||||
type pgxRepository struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewPgxRepository(pool *pgxpool.Pool) Repository {
|
||||
return &pgxRepository{pool: pool}
|
||||
}
|
||||
|
||||
func (r *pgxRepository) Insert(ctx context.Context, id, userID string, appID *string, topic string, config map[string]any) error {
|
||||
if config == nil {
|
||||
config = map[string]any{}
|
||||
}
|
||||
configJSON, err := json.Marshal(config)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = r.pool.Exec(ctx,
|
||||
`INSERT INTO research_tasks (id, user_id, app_id, topic, config)
|
||||
VALUES ($1, $2, $3, $4, $5)`,
|
||||
id, userID, appID, topic, configJSON,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *pgxRepository) Get(ctx context.Context, userID, taskID string) (*Task, error) {
|
||||
var t Task
|
||||
var sources []byte
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT id, topic, status, progress, status_message, error_message, report, sources, tokens_used, created_at
|
||||
FROM research_tasks WHERE id = $1 AND user_id = $2`, taskID, userID,
|
||||
).Scan(&t.ID, &t.Topic, &t.Status, &t.Progress, &t.StatusMessage, &t.ErrorMessage,
|
||||
&t.Report, &sources, &t.TokensUsed, &t.CreatedAt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.Sources = sources
|
||||
return &t, nil
|
||||
}
|
||||
|
||||
func (r *pgxRepository) List(ctx context.Context, userID string, limit int) ([]Task, error) {
|
||||
rows, err := r.pool.Query(ctx,
|
||||
`SELECT id, topic, status, progress, status_message, error_message, tokens_used, created_at
|
||||
FROM research_tasks WHERE user_id = $1 ORDER BY created_at DESC LIMIT $2`, userID, limit,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var tasks []Task
|
||||
for rows.Next() {
|
||||
var t Task
|
||||
if err := rows.Scan(&t.ID, &t.Topic, &t.Status, &t.Progress, &t.StatusMessage,
|
||||
&t.ErrorMessage, &t.TokensUsed, &t.CreatedAt); err != nil {
|
||||
continue
|
||||
}
|
||||
tasks = append(tasks, t)
|
||||
}
|
||||
return tasks, nil
|
||||
}
|
||||
|
||||
func (r *pgxRepository) Cancel(ctx context.Context, userID, taskID string) (bool, error) {
|
||||
tag, err := r.pool.Exec(ctx,
|
||||
`UPDATE research_tasks SET status = 'canceled', updated_at = NOW()
|
||||
WHERE id = $1 AND user_id = $2
|
||||
AND status IN ('pending','planning','searching','reading','synthesizing')`,
|
||||
taskID, userID,
|
||||
)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
return tag.RowsAffected() > 0, nil
|
||||
}
|
||||
Reference in New Issue
Block a user