feat(proxy,chat): add per-task/schedule/chat proxy pool routing and @task mentions

- Support pool auto-rotation mode alongside inherit/direct/node/custom proxy routes across gateway and MITM proxy
- Add GET /api/proxy/routes/{scope}/{owner_id}, task create/DTO proxy_route, and schedule proxy persistence + propagation
- Add list_proxy_pool and set_task_proxy chat agent tools and @task mention search/context injection
- Wire shared proxy/pool selectors into tasks, schedules, chat sandbox popover, and container routes UI
This commit is contained in:
ji ruo committed 2026-12-03 00:10:50 +08:00
1 parent b18cf89b77
commit 778e49317d
28 files changed
+1683 -124

No files matched your search

+54 -1
View File
@@ -1065,6 +1065,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("DELETE /api/proxy/blacklist/rules/{id}", s.deleteEgressBlacklistRule)
mux.HandleFunc("DELETE /api/proxy/blacklist/events", s.clearEgressBlockEvents)
mux.HandleFunc("GET /api/proxy/routes", s.getProxyRoutes)
mux.HandleFunc("GET /api/proxy/routes/{scope}/{owner_id}", s.getProxyRoute)
mux.HandleFunc("PUT /api/proxy/routes/{scope}/{owner_id}", s.putProxyRoute)
mux.HandleFunc("DELETE /api/proxy/routes/{scope}/{owner_id}", s.deleteProxyRoute)
mux.HandleFunc("PUT /api/proxy/gateway/settings", s.updateGatewaySettings)
@@ -1344,6 +1345,7 @@ func (s *Server) listTasks(w http.ResponseWriter, r *http.Request) {
dto := taskDTO(t, s.resolvedTaskStatus(t))
dto.WorkerActualCount, dto.WorkerTargetCount = s.engine.WorkerCounts(t)
applyTaskArchiveBlocker(&dto, archiveBlockers)
s.applyTaskProxyDTO(&dto)
metric := metrics[t.ExpID]
dto.Tokens = tokenTotalDTO(metric.Tokens)
// prefer the live in-memory heartbeat (fresher) and fall back to the
@@ -1823,6 +1825,7 @@ type createTaskReq struct {
WorkerCount int `json:"worker_count,omitempty"` // 本任务 Worker 数;0/省略=用全局默认;上限50
// InterceptRules 任务级资产拦截/允许规则(创建时录入,存 task_intercept_rules,不进全局表)。
InterceptRules []taskInterceptRuleReq `json:"intercept_rules,omitempty"`
ProxyRoute *ProxyRouteInput `json:"proxy_route,omitempty"` // 任务容器出口代理配置(inherit/direct/pool/proxy)
}
// taskCreateError is a rejected task creation: an HTTP-style status plus a
@@ -1843,7 +1846,9 @@ func (s *Server) createTask(w http.ResponseWriter, r *http.Request) {
writeErr(w, cerr.Status, cerr.Msg)
return
}
writeJSON(w, 201, taskDTO(t, s.resolvedTaskStatus(t)))
dto := taskDTO(t, s.resolvedTaskStatus(t))
s.applyTaskProxyDTO(&dto)
writeJSON(w, 201, dto)
}
// createTaskFromRequest validates a creation request, stores the task and
@@ -1897,8 +1902,37 @@ func (s *Server) createTaskFromRequest(req createTaskReq) (*Task, *taskCreateErr
if err := db.ValidateTaskScheduleInput(input); err != nil {
return nil, &taskCreateError{400, err.Error()}
}
if err := s.validateScheduleProxy(&input, nil); err != nil {
return nil, &taskCreateError{400, err.Error()}
}
schedule = &input
}
var validatedProxy *ProxyRouteInput
if req.ProxyRoute != nil {
norm, err := s.validateAndNormalizeProxyRoute(*req.ProxyRoute, nil)
if err != nil {
return nil, &taskCreateError{400, "任务代理配置无效:" + err.Error()}
}
if norm.Mode != "inherit" {
validatedProxy = &ProxyRouteInput{
Mode: norm.Mode,
ProxyPoolID: norm.ProxyPoolID,
ProxyURL: norm.ProxyURL,
Note: norm.Note,
}
}
} else if schedule != nil && schedule.ProxyMode != "" && schedule.ProxyMode != "inherit" {
validatedProxy = &ProxyRouteInput{
Mode: schedule.ProxyMode,
ProxyPoolID: schedule.ProxyPoolID,
ProxyURL: schedule.ProxyURL,
}
}
if validatedProxy != nil && schedule != nil && (schedule.ProxyMode == "" || schedule.ProxyMode == "inherit") {
schedule.ProxyMode = validatedProxy.Mode
schedule.ProxyPoolID = validatedProxy.ProxyPoolID
schedule.ProxyURL = validatedProxy.ProxyURL
}
remark, err := normalizeRemark(req.Remark)
if err != nil {
return nil, &taskCreateError{400, err.Error()}
@@ -1937,6 +1971,11 @@ func (s *Server) createTaskFromRequest(req createTaskReq) (*Task, *taskCreateErr
_, _ = s.m.pg.Exec(`UPDATE tasks SET description=$2, goal=$3 WHERE id=$1`, id, newDesc, newGoal)
}
}
if validatedProxy != nil {
if _, err := s.setContainerProxyRoute("task", t.ID, *validatedProxy); err != nil {
log.Printf("[task] 新建任务 #%s 应用代理路由失败: %v", t.ID, err)
}
}
log.Printf("[task] 新建任务 #%s «%s» 目标: %s", t.ID, req.Description, req.Goal)
// 共享的建后流程(seed + 种子意图 + 后台目标分解 + engine.Run),与 spawn_task 复用同一段。
// launchTask 内部异步,不阻塞 UI —— 目标分解在后台可见地进行。
@@ -2152,9 +2191,23 @@ func (s *Server) getTask(w http.ResponseWriter, r *http.Request) {
dto.WorkerActualCount, dto.WorkerTargetCount = s.engine.WorkerCounts(t)
archiveBlockers, _ := s.m.PG().TaskArchiveBlockers()
applyTaskArchiveBlocker(&dto, archiveBlockers)
s.applyTaskProxyDTO(&dto)
writeJSON(w, 200, dto)
}
func (s *Server) applyTaskProxyDTO(dto *TaskDTO) {
if dto == nil || dto.ID == "" {
return
}
item, _ := s.containerProxyRouteView("task", dto.ID)
dto.ProxyRoute = &TaskProxyRouteDTO{
Mode: item.Mode,
ProxyPoolID: item.ProxyPoolID,
ProxyURL: item.ProxyURL,
EffectiveProxy: item.EffectiveProxy,
}
}
// taskCoverage returns a task's rough asset test coverage (denominator/tested/backlog).
func (s *Server) taskCoverage(w http.ResponseWriter, r *http.Request) {
t, ok := s.m.Task(r.PathValue("id"))