mirror of
https://github.com/chaitin/MonkeyCode.git
synced 2026-11-23 04:09:15 +08:00
核心改动: - TaskUsecase: 移除 tasker 依赖,改用 lifecycle.Transition - CreateTaskReq 临时存储到 Redis (10 分钟过期) - InternalHostHandler: 移除 tasker 依赖 - TaskRepo: 实现 UpdateStatus 方法 完整流程: 1. TaskUsecase.Create() 存储 CreateTaskReq 到 Redis 2. VM 就绪时 TaskCreateHook 从 Redis 读取并创建 taskflow 任务 3. VMTaskHook 自动同步任务状态
508 lines
14 KiB
Go
508 lines
14 KiB
Go
package usecase
|
||
|
||
import (
|
||
"bytes"
|
||
"context"
|
||
"fmt"
|
||
"log/slog"
|
||
"os"
|
||
"path/filepath"
|
||
"sort"
|
||
"strings"
|
||
"text/template"
|
||
"time"
|
||
|
||
"github.com/gogo/protobuf/proto"
|
||
"github.com/google/uuid"
|
||
"github.com/redis/go-redis/v9"
|
||
"github.com/samber/do"
|
||
|
||
"github.com/chaitin/MonkeyCode/backend/config"
|
||
"github.com/chaitin/MonkeyCode/backend/consts"
|
||
"github.com/chaitin/MonkeyCode/backend/db"
|
||
"github.com/chaitin/MonkeyCode/backend/domain"
|
||
"github.com/chaitin/MonkeyCode/backend/ent/types"
|
||
"github.com/chaitin/MonkeyCode/backend/errcode"
|
||
"github.com/chaitin/MonkeyCode/backend/pkg/cvt"
|
||
"github.com/chaitin/MonkeyCode/backend/pkg/delayqueue"
|
||
"github.com/chaitin/MonkeyCode/backend/pkg/entx"
|
||
"github.com/chaitin/MonkeyCode/backend/pkg/lifecycle"
|
||
"github.com/chaitin/MonkeyCode/backend/pkg/loki"
|
||
"github.com/chaitin/MonkeyCode/backend/pkg/notify/dispatcher"
|
||
"github.com/chaitin/MonkeyCode/backend/pkg/taskflow"
|
||
"github.com/chaitin/MonkeyCode/backend/templates"
|
||
)
|
||
|
||
// TaskUsecase 任务业务逻辑实现
|
||
type TaskUsecase struct {
|
||
cfg *config.Config
|
||
repo domain.TaskRepo
|
||
modelRepo domain.ModelRepo
|
||
logger *slog.Logger
|
||
taskflow taskflow.Clienter
|
||
loki *loki.Client
|
||
vmexpireQueue *delayqueue.VMExpireQueue
|
||
redis *redis.Client
|
||
notifyDispatcher *dispatcher.Dispatcher
|
||
taskHook domain.TaskHook
|
||
taskLifecycle *lifecycle.Manager[uuid.UUID, consts.TaskStatus, lifecycle.TaskMetadata]
|
||
vmLifecycle *lifecycle.Manager[string, lifecycle.VMState, lifecycle.VMMetadata]
|
||
}
|
||
|
||
// NewTaskUsecase 创建任务业务逻辑实例
|
||
func NewTaskUsecase(i *do.Injector) (domain.TaskUsecase, error) {
|
||
u := &TaskUsecase{
|
||
cfg: do.MustInvoke[*config.Config](i),
|
||
repo: do.MustInvoke[domain.TaskRepo](i),
|
||
modelRepo: do.MustInvoke[domain.ModelRepo](i),
|
||
logger: do.MustInvoke[*slog.Logger](i).With("module", "usecase.TaskUsecase"),
|
||
taskflow: do.MustInvoke[taskflow.Clienter](i),
|
||
loki: do.MustInvoke[*loki.Client](i),
|
||
vmexpireQueue: do.MustInvoke[*delayqueue.VMExpireQueue](i),
|
||
redis: do.MustInvoke[*redis.Client](i),
|
||
notifyDispatcher: do.MustInvoke[*dispatcher.Dispatcher](i),
|
||
taskLifecycle: do.MustInvoke[*lifecycle.Manager[uuid.UUID, consts.TaskStatus, lifecycle.TaskMetadata]](i),
|
||
vmLifecycle: do.MustInvoke[*lifecycle.Manager[string, lifecycle.VMState, lifecycle.VMMetadata]](i),
|
||
}
|
||
|
||
// 可选注入 TaskHook
|
||
if hook, err := do.Invoke[domain.TaskHook](i); err == nil {
|
||
u.taskHook = hook
|
||
}
|
||
|
||
return u, nil
|
||
}
|
||
|
||
// AutoApprove implements domain.TaskUsecase.
|
||
func (a *TaskUsecase) AutoApprove(ctx context.Context, _ *domain.User, id uuid.UUID, approve bool) error {
|
||
return a.taskflow.TaskManager().AutoApprove(ctx, taskflow.TaskApproveReq{
|
||
ID: id,
|
||
AutoApprove: &approve,
|
||
})
|
||
}
|
||
|
||
// Info implements domain.TaskUsecase.
|
||
func (a *TaskUsecase) Info(ctx context.Context, user *domain.User, id uuid.UUID) (*domain.Task, bool, error) {
|
||
ctx = entx.SkipSoftDelete(ctx)
|
||
|
||
t, err := a.repo.Info(ctx, user, id)
|
||
if err != nil {
|
||
return nil, false, err
|
||
}
|
||
|
||
owner := user.ID == t.UserID
|
||
|
||
tk := cvt.From(t, &domain.Task{})
|
||
if vm := tk.VirtualMachine; vm != nil {
|
||
resp, _ := a.taskflow.VirtualMachiner().IsOnline(ctx, &taskflow.IsOnlineReq[string]{
|
||
IDs: []string{vm.ID},
|
||
})
|
||
|
||
if resp != nil && resp.OnlineMap[vm.ID] {
|
||
vm.Status = taskflow.VirtualMachineStatusOnline
|
||
} else {
|
||
vm.Status = taskflow.VirtualMachineStatusPending
|
||
|
||
for _, cond := range vm.Conditions {
|
||
switch cond.Type {
|
||
case types.ConditionTypeFailed:
|
||
vm.Status = taskflow.VirtualMachineStatusOffline
|
||
case types.ConditionTypeReady:
|
||
if time.Since(time.Unix(vm.CreatedAt, 0)) > 2*time.Minute {
|
||
vm.Status = taskflow.VirtualMachineStatusOffline
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 端口信息
|
||
ports, _ := a.taskflow.PortForwarder().List(ctx, vm.ID)
|
||
if ports == nil {
|
||
ports = []*taskflow.PortForwardInfo{}
|
||
}
|
||
vmPorts := cvt.Iter(ports, func(_ int, port *taskflow.PortForwardInfo) *domain.VMPort {
|
||
return &domain.VMPort{
|
||
ForwardID: port.ForwardID,
|
||
Port: uint16(port.Port),
|
||
Status: consts.PortStatus(port.Status),
|
||
WhiteList: port.WhitelistIPs,
|
||
ErrorMessage: port.ErrorMessage,
|
||
PreviewURL: port.AccessURL,
|
||
}
|
||
})
|
||
sort.Slice(vmPorts, func(i, j int) bool {
|
||
return vmPorts[i].Port < vmPorts[j].Port
|
||
})
|
||
vm.Ports = vmPorts
|
||
}
|
||
|
||
if stat, err := a.repo.Stat(ctx, id); err == nil {
|
||
tk.Stats = stat
|
||
}
|
||
|
||
return tk, owner, nil
|
||
}
|
||
|
||
// List implements domain.TaskUsecase.
|
||
func (a *TaskUsecase) List(ctx context.Context, user *domain.User, req domain.TaskListReq) (*domain.ListTaskResp, error) {
|
||
ctx = entx.SkipSoftDelete(ctx)
|
||
projectTasks, pageInfo, err := a.repo.List(ctx, user, req)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
stat, err := a.repo.StatByIDs(ctx, cvt.Iter(projectTasks, func(_ int, pt *db.ProjectTask) uuid.UUID {
|
||
return pt.TaskID
|
||
}))
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
tasks := cvt.Iter(projectTasks, func(_ int, pt *db.ProjectTask) *domain.ProjectTask {
|
||
tmp := cvt.From(pt, &domain.ProjectTask{})
|
||
if tmp.Task != nil {
|
||
tmp.Task.Stats = stat[tmp.Task.ID]
|
||
}
|
||
return tmp
|
||
})
|
||
|
||
resp := &domain.ListTaskResp{Tasks: tasks}
|
||
if pageInfo != nil {
|
||
resp.PageInfo = pageInfo
|
||
}
|
||
return resp, nil
|
||
}
|
||
|
||
// Stop implements domain.TaskUsecase.
|
||
func (a *TaskUsecase) Stop(ctx context.Context, user *domain.User, id uuid.UUID) error {
|
||
return a.repo.Stop(ctx, user, id, func(t *db.Task) error {
|
||
return a.taskflow.TaskManager().Stop(ctx, taskflow.TaskReq{
|
||
Task: &taskflow.Task{
|
||
ID: id,
|
||
},
|
||
})
|
||
})
|
||
}
|
||
|
||
// Cancel implements domain.TaskUsecase.
|
||
func (a *TaskUsecase) Cancel(ctx context.Context, user *domain.User, id uuid.UUID) error {
|
||
t, err := a.repo.Info(ctx, user, id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
tk := cvt.From(t, &domain.Task{})
|
||
|
||
if err := a.taskflow.TaskManager().Cancel(ctx, taskflow.TaskReq{
|
||
VirtualMachine: &taskflow.VirtualMachine{ID: tk.VirtualMachine.ID},
|
||
Task: &taskflow.Task{
|
||
ID: id,
|
||
},
|
||
}); err != nil {
|
||
return err
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// Continue implements domain.TaskUsecase.
|
||
func (a *TaskUsecase) Continue(ctx context.Context, user *domain.User, id uuid.UUID, content string) error {
|
||
t, err := a.repo.Info(ctx, user, id)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
tk := cvt.From(t, &domain.Task{})
|
||
|
||
if err := a.taskflow.TaskManager().Continue(ctx, taskflow.TaskReq{
|
||
VirtualMachine: &taskflow.VirtualMachine{ID: tk.VirtualMachine.ID},
|
||
Task: &taskflow.Task{
|
||
ID: id,
|
||
Text: content,
|
||
},
|
||
}); err != nil {
|
||
return err
|
||
}
|
||
|
||
// 缓存最近一次 user-input,供通知推送使用
|
||
a.redis.Set(ctx, fmt.Sprintf("mcai:task:%s:last_input", id.String()), content, 24*time.Hour)
|
||
|
||
return nil
|
||
}
|
||
|
||
// Create implements domain.TaskUsecase.
|
||
func (a *TaskUsecase) Create(ctx context.Context, user *domain.User, req domain.CreateTaskReq, token string) (*domain.ProjectTask, error) {
|
||
r, err := a.taskflow.Host().IsOnline(ctx, &taskflow.IsOnlineReq[string]{
|
||
IDs: []string{req.HostID},
|
||
})
|
||
if err != nil {
|
||
return nil, errcode.ErrHostOffline.Wrap(err)
|
||
}
|
||
if !r.OnlineMap[req.HostID] {
|
||
return nil, errcode.ErrHostOffline
|
||
}
|
||
|
||
TTLType := taskflow.TTLCountDown
|
||
if req.Resource.Life <= 0 {
|
||
TTLType = taskflow.TTLForever
|
||
}
|
||
|
||
req.Now = time.Now()
|
||
|
||
// 如果有 TaskHook,获取系统提示词
|
||
if a.taskHook != nil && req.SystemPrompt == "" {
|
||
if prompt, err := a.taskHook.GetSystemPrompt(ctx, req.Type, req.SubType); err == nil && prompt != "" {
|
||
req.SystemPrompt = prompt
|
||
}
|
||
}
|
||
|
||
pt, err := a.repo.Create(ctx, user, req, token, func(pt *db.ProjectTask, m *db.Model, i *db.Image) (*taskflow.VirtualMachine, error) {
|
||
t := pt.Edges.Task
|
||
if t == nil {
|
||
return nil, fmt.Errorf("task edge is nil")
|
||
}
|
||
|
||
coding, configs, err := a.getCodingConfigs(req.CliName, m, req.Extra.SkillIDs)
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
git := taskflow.Git{
|
||
URL: pt.RepoURL,
|
||
Branch: pt.Branch,
|
||
}
|
||
if token != "" {
|
||
git.Token = token
|
||
}
|
||
|
||
vm, err := a.taskflow.VirtualMachiner().Create(ctx, &taskflow.CreateVirtualMachineReq{
|
||
UserID: user.ID.String(),
|
||
HostID: req.HostID,
|
||
HostName: t.ID.String(),
|
||
Git: git,
|
||
ZipUrl: req.RepoReq.ZipURL,
|
||
ImageURL: i.Name,
|
||
ProxyURL: "",
|
||
TTL: taskflow.TTL{
|
||
Kind: TTLType,
|
||
Seconds: req.Resource.Life,
|
||
},
|
||
TaskID: t.ID,
|
||
LLM: taskflow.LLMProviderReq{
|
||
Provider: taskflow.LlmProviderOpenAI,
|
||
ApiKey: m.APIKey,
|
||
BaseURL: m.BaseURL,
|
||
Model: m.Model,
|
||
},
|
||
Cores: fmt.Sprintf("%d", req.Resource.Core),
|
||
Memory: req.Resource.Memory,
|
||
})
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
|
||
if vm == nil {
|
||
return nil, fmt.Errorf("vm is nil")
|
||
}
|
||
|
||
if req.Resource.Life > 0 {
|
||
if _, err := a.vmexpireQueue.Enqueue(ctx, consts.VM_EXPIRE_QUEUE_KEY, &domain.VmExpireInfo{
|
||
UID: user.ID,
|
||
VmID: vm.ID,
|
||
HostID: req.HostID,
|
||
EnvID: vm.EnvironmentID,
|
||
}, time.Now().Add(time.Duration(req.Resource.Life)*time.Second), vm.ID); err != nil {
|
||
a.logger.With("error", err, "vm", vm).ErrorContext(ctx, "failed to enqueue countdown vm")
|
||
}
|
||
}
|
||
|
||
mcps := []taskflow.McpServerConfig{
|
||
{
|
||
Type: "http",
|
||
Name: "mcaiBuiltin",
|
||
Url: proto.String(fmt.Sprintf("http://127.0.0.1:65510/mcp?task_id=%s", t.ID.String())),
|
||
},
|
||
{
|
||
Type: "http",
|
||
Name: "context7",
|
||
Url: proto.String("https://mcp.context7.com/mcp"),
|
||
Headers: []*taskflow.McpHttpHeader{
|
||
{
|
||
Name: "CONTEXT7_API_KEY",
|
||
Value: a.cfg.Context7ApiKey,
|
||
},
|
||
},
|
||
},
|
||
}
|
||
|
||
taskMeta := lifecycle.TaskMetadata{
|
||
TaskID: t.ID,
|
||
UserID: user.ID,
|
||
}
|
||
if err := a.taskLifecycle.Transition(ctx, t.ID, consts.TaskStatusPending, taskMeta); err != nil {
|
||
a.logger.WarnContext(ctx, "task lifecycle transition failed", "error", err)
|
||
}
|
||
|
||
vmMeta := lifecycle.VMMetadata{
|
||
VMID: vm.ID,
|
||
TaskID: &t.ID,
|
||
UserID: user.ID,
|
||
}
|
||
if err := a.vmLifecycle.Transition(ctx, vm.ID, lifecycle.VMStatePending, vmMeta); err != nil {
|
||
a.logger.WarnContext(ctx, "vm lifecycle transition failed", "error", err)
|
||
}
|
||
|
||
// 存储 CreateTaskReq 到 Redis(10 分钟过期),供 Lifecycle Manager 消费
|
||
createTaskReq := &taskflow.CreateTaskReq{
|
||
ID: t.ID,
|
||
VMID: vm.ID,
|
||
Text: req.Content,
|
||
SystemPrompt: req.SystemPrompt,
|
||
CodingAgent: coding,
|
||
LLM: taskflow.LLM{
|
||
ApiKey: m.APIKey,
|
||
BaseURL: m.BaseURL,
|
||
Model: m.Model,
|
||
},
|
||
Configs: configs,
|
||
McpConfigs: mcps,
|
||
}
|
||
reqKey := fmt.Sprintf("task:create_req:%s", t.ID.String())
|
||
if err := a.redis.Set(ctx, reqKey, createTaskReq, 10*time.Minute).Err(); err != nil {
|
||
a.logger.WarnContext(ctx, "failed to store CreateTaskReq in Redis", "error", err)
|
||
}
|
||
|
||
return vm, nil
|
||
})
|
||
if err != nil {
|
||
a.logger.With("error", err, "req", req).ErrorContext(ctx, "failed to create task")
|
||
return nil, err
|
||
}
|
||
a.logger.With("req", req).InfoContext(ctx, "task created")
|
||
|
||
result := cvt.From(pt, &domain.ProjectTask{})
|
||
|
||
// 通知 TaskHook(如内部项目的 git task 创建等)
|
||
if a.taskHook != nil {
|
||
if err := a.taskHook.OnTaskCreated(ctx, result); err != nil {
|
||
a.logger.WarnContext(ctx, "taskHook.OnTaskCreated failed", "error", err)
|
||
}
|
||
}
|
||
|
||
return result, nil
|
||
}
|
||
|
||
func (a *TaskUsecase) getCodingConfigs(cli consts.CliName, m *db.Model, skillIDs []string) (taskflow.CodingAgent, []taskflow.ConfigFile, error) {
|
||
var tmp string
|
||
var path string
|
||
var coding taskflow.CodingAgent
|
||
cfs := make([]taskflow.ConfigFile, 0)
|
||
switch cli {
|
||
case consts.CliNameClaude:
|
||
tmp = string(templates.Claude)
|
||
path = "~/.claude/settings.json"
|
||
coding = taskflow.CodingAgentClaude
|
||
m.BaseURL = strings.ReplaceAll(m.BaseURL, "/v1", "")
|
||
|
||
case consts.CliNameCodex:
|
||
tmp = string(templates.Codex)
|
||
path = "~/.codex/config.toml"
|
||
coding = taskflow.CodingAgentCodex
|
||
|
||
case consts.CliNameOpencode:
|
||
tmp = string(templates.OpenCode)
|
||
path = "~/.config/opencode/opencode.json"
|
||
coding = taskflow.CodingAgentOpenCode
|
||
|
||
authtemp, err := template.New("auth").Parse(string(templates.OpenCodeAuth))
|
||
if err != nil {
|
||
return coding, nil, err
|
||
}
|
||
|
||
var authBuf bytes.Buffer
|
||
if err := authtemp.Execute(&authBuf, map[string]any{
|
||
"api_key": m.APIKey,
|
||
}); err != nil {
|
||
return coding, nil, err
|
||
}
|
||
cfs = append(cfs, taskflow.ConfigFile{
|
||
Path: "~/.local/share/opencode/auth.json",
|
||
Content: authBuf.String(),
|
||
})
|
||
|
||
default:
|
||
return coding, nil, fmt.Errorf("unexpected consts.CliName: %#v", cli)
|
||
}
|
||
|
||
temp, err := template.New("config").Parse(tmp)
|
||
if err != nil {
|
||
return coding, nil, err
|
||
}
|
||
|
||
var buf bytes.Buffer
|
||
if err := temp.Execute(&buf, map[string]any{
|
||
"model": m.Model,
|
||
"base_url": m.BaseURL,
|
||
"api_key": m.APIKey,
|
||
}); err != nil {
|
||
return coding, nil, err
|
||
}
|
||
|
||
cfs = append(cfs, taskflow.ConfigFile{
|
||
Path: path,
|
||
Content: buf.String(),
|
||
})
|
||
|
||
if len(skillIDs) == 0 {
|
||
return coding, cfs, nil
|
||
}
|
||
|
||
for _, skillID := range skillIDs {
|
||
skilldir := filepath.Join(consts.SkillBaseDir, skillID)
|
||
if _, err := os.Stat(skilldir); os.IsNotExist(err) {
|
||
continue
|
||
}
|
||
filepath.Walk(skilldir, func(path string, info os.FileInfo, err error) error {
|
||
if err != nil {
|
||
return err
|
||
}
|
||
if info.IsDir() {
|
||
return nil
|
||
}
|
||
content, err := os.ReadFile(path)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
// 获取相对于 skilldir 的相对路径,保留目录结构
|
||
relPath, err := filepath.Rel(skilldir, path)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
realSkillID := filepath.Base(skilldir)
|
||
agentSkillDir := "/tmp/codingmatrix-project-tpl/.ai-ready/skills/"
|
||
cfs = append(cfs, taskflow.ConfigFile{
|
||
Path: filepath.Join(agentSkillDir, realSkillID, relPath),
|
||
Content: string(content),
|
||
})
|
||
return nil
|
||
})
|
||
}
|
||
return coding, cfs, nil
|
||
}
|
||
|
||
// GetPublic implements domain.TaskUsecase.
|
||
func (a *TaskUsecase) GetPublic(ctx context.Context, _ *domain.User, id uuid.UUID) (*domain.Task, error) {
|
||
t, err := a.repo.GetByID(ctx, id)
|
||
if err != nil {
|
||
return nil, errcode.ErrNotFound.Wrap(err)
|
||
}
|
||
|
||
return cvt.From(t, &domain.Task{}), nil
|
||
}
|
||
|
||
// GitTask implements domain.TaskUsecase.
|
||
func (a *TaskUsecase) GitTask(ctx context.Context, id uuid.UUID) (*domain.GitTask, error) {
|
||
if a.taskHook != nil {
|
||
return a.taskHook.GitTask(ctx, id)
|
||
}
|
||
return nil, errcode.ErrNotFound
|
||
}
|