Files
GovAI/server/internal/handler/research.go
T
freedakgmail c949204662 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 应用迁移并烟测。
2026-06-17 17:52:47 +08:00

132 lines
3.9 KiB
Go

package handler
import (
"encoding/json"
"errors"
"net/http"
"time"
"github.com/enterprise-ai-platform/server/internal/middleware"
"github.com/enterprise-ai-platform/server/internal/response"
"github.com/enterprise-ai-platform/server/internal/service/research"
"github.com/go-chi/chi/v5"
)
// ResearchHandler 深度研究任务的 HTTP 入口(薄层:仅解析参数与组织响应)。
type ResearchHandler struct {
svc *research.Service
}
func NewResearchHandler(svc *research.Service) *ResearchHandler {
return &ResearchHandler{svc: svc}
}
type createResearchRequest struct {
Topic string `json:"topic"`
AppID string `json:"app_id,omitempty"`
Config map[string]any `json:"config"`
}
type researchTaskResponse struct {
TaskID string `json:"task_id"`
Topic string `json:"topic"`
Status string `json:"status"`
Progress int `json:"progress"`
StatusMessage *string `json:"status_message,omitempty"`
ErrorMessage *string `json:"error_message,omitempty"`
Report *string `json:"report,omitempty"`
Sources json.RawMessage `json:"sources,omitempty"`
TokensUsed int `json:"tokens_used"`
CreatedAt string `json:"created_at"`
}
func toResearchResponse(t *research.Task) researchTaskResponse {
resp := researchTaskResponse{
TaskID: t.ID,
Topic: t.Topic,
Status: t.Status,
Progress: t.Progress,
StatusMessage: t.StatusMessage,
ErrorMessage: t.ErrorMessage,
Report: t.Report,
TokensUsed: t.TokensUsed,
CreatedAt: t.CreatedAt.Format(time.RFC3339),
}
if len(t.Sources) > 0 {
resp.Sources = json.RawMessage(t.Sources)
}
return resp
}
// CreateTask 创建深度研究任务。
func (h *ResearchHandler) CreateTask(w http.ResponseWriter, r *http.Request) {
userID := middleware.GetUserID(r.Context())
var req createResearchRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
response.BadRequest(w, "无效的请求格式")
return
}
in := research.CreateInput{UserID: userID.String(), Topic: req.Topic, Config: req.Config}
if req.AppID != "" {
in.AppID = &req.AppID
}
id, err := h.svc.Create(r.Context(), in)
if err != nil {
if errors.Is(err, research.ErrEmptyTopic) {
response.BadRequest(w, "研究题目不能为空")
return
}
response.InternalError(w, "创建任务失败")
return
}
response.JSON(w, http.StatusCreated, map[string]string{"task_id": id, "status": "pending"})
}
// GetTaskStatus 查询任务状态/结果。
func (h *ResearchHandler) GetTaskStatus(w http.ResponseWriter, r *http.Request) {
userID := middleware.GetUserID(r.Context())
taskID := chi.URLParam(r, "taskId")
t, err := h.svc.Status(r.Context(), userID.String(), taskID)
if err != nil {
response.NotFound(w, "任务不存在")
return
}
response.JSON(w, http.StatusOK, toResearchResponse(t))
}
// ListTasks 列出当前用户的研究任务。
func (h *ResearchHandler) ListTasks(w http.ResponseWriter, r *http.Request) {
userID := middleware.GetUserID(r.Context())
tasks, err := h.svc.List(r.Context(), userID.String())
if err != nil {
response.InternalError(w, "查询失败")
return
}
out := make([]researchTaskResponse, 0, len(tasks))
for i := range tasks {
out = append(out, toResearchResponse(&tasks[i]))
}
response.JSON(w, http.StatusOK, out)
}
// CancelTask 取消进行中的研究任务。
func (h *ResearchHandler) CancelTask(w http.ResponseWriter, r *http.Request) {
userID := middleware.GetUserID(r.Context())
taskID := chi.URLParam(r, "taskId")
if err := h.svc.Cancel(r.Context(), userID.String(), taskID); err != nil {
if errors.Is(err, research.ErrNotFound) {
response.NotFound(w, "任务不存在或无法取消")
return
}
response.InternalError(w, "取消失败")
return
}
response.JSON(w, http.StatusOK, map[string]string{"message": "已取消"})
}