157 lines
7.2 KiB
Go
157 lines
7.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/kandev/kandev/internal/common/logger"
|
|
"github.com/kandev/kandev/internal/orchestrator"
|
|
"github.com/kandev/kandev/internal/task/dto"
|
|
"github.com/kandev/kandev/internal/task/models"
|
|
"github.com/kandev/kandev/internal/task/service"
|
|
ws "github.com/kandev/kandev/pkg/websocket"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
// handlerRepo is the minimal repository interface needed by task handlers.
|
|
type handlerRepo interface {
|
|
UpsertSessionFileReview(ctx context.Context, review *models.SessionFileReview) error
|
|
GetSessionFileReviews(ctx context.Context, sessionID string) ([]*models.SessionFileReview, error)
|
|
DeleteSessionFileReviews(ctx context.Context, sessionID string) error
|
|
ListTurnsBySession(ctx context.Context, sessionID string) ([]*models.Turn, error)
|
|
CountToolCallMessagesBySession(ctx context.Context, sessionIDs []string) (map[string]int, error)
|
|
// ListChildren returns the direct, non-archived, non-ephemeral
|
|
// subtasks of parentID. Used by httpTaskSubtaskCount to drive the
|
|
// "Also archive/delete subtasks" checkbox in the frontend dialog.
|
|
ListChildren(ctx context.Context, parentID string) ([]*models.Task, error)
|
|
}
|
|
|
|
type TaskHandlers struct {
|
|
service *service.Service
|
|
orchestrator OrchestratorStarter
|
|
repo handlerRepo
|
|
planService *service.PlanService
|
|
handoffSvc *service.HandoffService
|
|
onTaskCreatedWithPR func(ctx context.Context, taskID, sessionID, prURL, branch string)
|
|
logger *logger.Logger
|
|
}
|
|
|
|
// SetHandoffService wires the office task-handoffs service used by the
|
|
// Kanban subtask path to attach workspace-group membership and the
|
|
// sequential blocker chain (handoffs phase 5). Optional — nil disables
|
|
// post-create attachment, matching the pre-handoffs behaviour.
|
|
func (h *TaskHandlers) SetHandoffService(svc *service.HandoffService) {
|
|
h.handoffSvc = svc
|
|
}
|
|
|
|
// SetOnTaskCreatedWithPR sets a callback invoked when a task is created with a PR URL
|
|
// in one of its repository inputs. The callback runs in a background goroutine.
|
|
func (h *TaskHandlers) SetOnTaskCreatedWithPR(fn func(ctx context.Context, taskID, sessionID, prURL, branch string)) {
|
|
h.onTaskCreatedWithPR = fn
|
|
}
|
|
|
|
type OrchestratorStarter interface {
|
|
// LaunchSession is the unified entry point for all session operations.
|
|
LaunchSession(ctx context.Context, req *orchestrator.LaunchSessionRequest) (*orchestrator.LaunchSessionResponse, error)
|
|
// EnsureSession returns the task's existing primary/newest session if any,
|
|
// otherwise resolves the agent profile server-side and creates one.
|
|
EnsureSession(ctx context.Context, taskID string, opts ...orchestrator.EnsureSessionOptions) (*orchestrator.EnsureSessionResponse, error)
|
|
}
|
|
|
|
func NewTaskHandlers(svc *service.Service, orchestrator OrchestratorStarter, repo handlerRepo, planService *service.PlanService, log *logger.Logger) *TaskHandlers {
|
|
return &TaskHandlers{
|
|
service: svc,
|
|
orchestrator: orchestrator,
|
|
repo: repo,
|
|
planService: planService,
|
|
logger: log.WithFields(zap.String("component", "task-task-handlers")),
|
|
}
|
|
}
|
|
|
|
func RegisterTaskRoutes(router *gin.Engine, dispatcher *ws.Dispatcher, svc *service.Service, orchestrator OrchestratorStarter, repo handlerRepo, planService *service.PlanService, log *logger.Logger) *TaskHandlers {
|
|
handlers := NewTaskHandlers(svc, orchestrator, repo, planService, log)
|
|
handlers.registerHTTP(router)
|
|
handlers.registerWS(dispatcher)
|
|
return handlers
|
|
}
|
|
|
|
func (h *TaskHandlers) registerHTTP(router *gin.Engine) {
|
|
api := router.Group("/api/v1")
|
|
api.GET("/workflows/:id/tasks", h.httpListTasks)
|
|
api.GET("/workspaces/:id/tasks", h.httpListTasksByWorkspace)
|
|
api.GET("/tasks/:id", h.httpGetTask)
|
|
api.GET("/tasks/:id/context", h.httpGetTaskContext)
|
|
api.GET("/task-sessions/:id", h.httpGetTaskSession)
|
|
api.GET("/tasks/:id/sessions", h.httpListTaskSessions)
|
|
api.POST("/tasks/:id/sessions/ensure", h.httpEnsureTaskSession)
|
|
api.GET("/tasks/:id/environment", h.httpGetTaskEnvironment)
|
|
api.GET("/tasks/:id/environment/live", h.httpGetTaskEnvironmentLive)
|
|
api.POST("/tasks/:id/environment/reset", h.httpResetTaskEnvironment)
|
|
api.GET("/task-sessions/:id/turns", h.httpListSessionTurns)
|
|
api.POST("/tasks", h.httpCreateTask)
|
|
api.PATCH("/tasks/:id", h.httpUpdateTask)
|
|
api.POST("/tasks/:id/move", h.httpMoveTask)
|
|
api.DELETE("/tasks/:id", h.httpDeleteTask)
|
|
api.POST("/tasks/:id/archive", h.httpArchiveTask)
|
|
api.POST("/tasks/:id/unarchive", h.httpUnarchiveTask)
|
|
api.GET("/tasks/:id/subtask-count", h.httpTaskSubtaskCount)
|
|
|
|
api.POST("/tasks/bulk-move", h.httpBulkMoveTasks)
|
|
api.GET("/workflows/:id/task-count", h.httpGetWorkflowTaskCount)
|
|
api.GET("/workflow/steps/:id/task-count", h.httpGetStepTaskCount)
|
|
|
|
// Session workflow review endpoints
|
|
api.POST("/sessions/:id/approve", h.httpApproveSession)
|
|
|
|
// Quick chat endpoint - creates ephemeral task with prepared session
|
|
api.POST("/workspaces/:id/quick-chat", h.httpStartQuickChat)
|
|
|
|
// Config chat endpoint - creates ephemeral task with config-mode MCP tools
|
|
api.POST("/workspaces/:id/config-chat", h.httpStartConfigChat)
|
|
}
|
|
|
|
func (h *TaskHandlers) registerWS(dispatcher *ws.Dispatcher) {
|
|
dispatcher.RegisterFunc(ws.ActionTaskList, h.wsListTasks)
|
|
dispatcher.RegisterFunc(ws.ActionTaskCreate, h.wsCreateTask)
|
|
dispatcher.RegisterFunc(ws.ActionTaskGet, h.wsGetTask)
|
|
dispatcher.RegisterFunc(ws.ActionTaskUpdate, h.wsUpdateTask)
|
|
dispatcher.RegisterFunc(ws.ActionTaskDelete, h.wsDeleteTask)
|
|
dispatcher.RegisterFunc(ws.ActionTaskMove, h.wsMoveTask)
|
|
dispatcher.RegisterFunc(ws.ActionTaskState, h.wsUpdateTaskState)
|
|
dispatcher.RegisterFunc(ws.ActionTaskArchive, h.wsArchiveTask)
|
|
dispatcher.RegisterFunc(ws.ActionTaskSessionList, h.wsListTaskSessions)
|
|
// Git snapshot handler (commits and cumulative diff are handled by agent/handlers/git_handlers.go)
|
|
dispatcher.RegisterFunc(ws.ActionSessionGitSnapshots, h.wsGetGitSnapshots)
|
|
// Session file review handlers
|
|
dispatcher.RegisterFunc(ws.ActionSessionFileReviewGet, h.wsGetSessionFileReviews)
|
|
dispatcher.RegisterFunc(ws.ActionSessionFileReviewUpdate, h.wsUpdateSessionFileReview)
|
|
dispatcher.RegisterFunc(ws.ActionSessionFileReviewReset, h.wsResetSessionFileReviews)
|
|
// Task plan handlers
|
|
dispatcher.RegisterFunc(ws.ActionTaskPlanCreate, h.wsCreateTaskPlan)
|
|
dispatcher.RegisterFunc(ws.ActionTaskPlanGet, h.wsGetTaskPlan)
|
|
dispatcher.RegisterFunc(ws.ActionTaskPlanUpdate, h.wsUpdateTaskPlan)
|
|
dispatcher.RegisterFunc(ws.ActionTaskPlanDelete, h.wsDeleteTaskPlan)
|
|
dispatcher.RegisterFunc(ws.ActionTaskPlanRevisionsList, h.wsListTaskPlanRevisions)
|
|
dispatcher.RegisterFunc(ws.ActionTaskPlanRevisionGet, h.wsGetTaskPlanRevision)
|
|
dispatcher.RegisterFunc(ws.ActionTaskPlanRevert, h.wsRevertTaskPlan)
|
|
}
|
|
|
|
// convertToServiceRepos converts dto.TaskRepositoryInput slice to service.TaskRepositoryInput slice.
|
|
func convertToServiceRepos(repos []dto.TaskRepositoryInput) []service.TaskRepositoryInput {
|
|
result := make([]service.TaskRepositoryInput, len(repos))
|
|
for i, r := range repos {
|
|
result[i] = service.TaskRepositoryInput{
|
|
RepositoryID: r.RepositoryID,
|
|
BaseBranch: r.BaseBranch,
|
|
CheckoutBranch: r.CheckoutBranch,
|
|
PRNumber: r.PRNumber,
|
|
LocalPath: r.LocalPath,
|
|
Name: r.Name,
|
|
DefaultBranch: r.DefaultBranch,
|
|
GitHubURL: r.GitHubURL,
|
|
}
|
|
}
|
|
return result
|
|
}
|