|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "regexp" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/sirupsen/logrus" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + defaultSystemMessage = "You are an experienced software developer. You will act as a reviewer for a GitHub Pull Request, and you should answer by markdown format." |
| 15 | + defaultPromte = "Please identify potential problems and give some fixing suggestions." |
| 16 | + defaultPrPatchIntroducePromte = "This is the diff for the pull request:" |
| 17 | + defaultMaxResponseTokens = 500 |
| 18 | + defaultTemperature = 0.7 |
| 19 | + defaultStaticOutHeadnote = `> **I have already done a preliminary review for you, and I hope to help you do a better job.** |
| 20 | +------ |
| 21 | +` |
| 22 | +) |
| 23 | + |
| 24 | +// TasksConfig represent the all tasks store for the plugin. |
| 25 | +// |
| 26 | +// layer: org|repo / task-name / task-config |
| 27 | +type TasksConfig map[string]RepoTasks |
| 28 | + |
| 29 | +// RepoTasks represent the tasks for a repo or ORG. |
| 30 | +type RepoTasks map[string]*Task |
| 31 | + |
| 32 | +// Task reprensent the config for AI task item. |
| 33 | +// |
| 34 | +// $SystemMessage |
| 35 | +// -------------- |
| 36 | +// < $Prompt |
| 37 | +// < Here are the serval context contents: |
| 38 | +// $ExternalContexts.each do |
| 39 | +// |
| 40 | +// < - format(it.PromptTpl, fetch(it.ResURL)) |
| 41 | +// |
| 42 | +// < $PatchIntroducerPrompt |
| 43 | +// < ```diff |
| 44 | +// < diff content |
| 45 | +// < ``` |
| 46 | +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 47 | +// > <OutputStaticHeadNote> |
| 48 | +// > responses from AI server. |
| 49 | +// |
| 50 | +// TODO(wuhuizuo): using go template to comose the question. |
| 51 | +type Task struct { |
| 52 | + Description string `yaml:"description,omitempty" json:"description,omitempty"` |
| 53 | + SystemMessage string `yaml:"system_message,omitempty" json:"system_message,omitempty"` |
| 54 | + UserPrompt string `yaml:"user_prompt,omitempty" json:"user_prompt,omitempty"` |
| 55 | + PatchIntroducePrompt string `yaml:"patch_introduce_prompt,omitempty" json:"patch_introduce_prompt,omitempty"` |
| 56 | + OutputStaticHeadNote string `yaml:"output_static_head_note,omitempty" json:"output_static_head_note,omitempty"` |
| 57 | + MaxResponseTokens int `yaml:"max_response_tokens,omitempty" json:"max_response_tokens,omitempty"` |
| 58 | + ExternalContexts []*ExternalContext `yaml:"external_contexts,omitempty" json:"external_contexts,omitempty"` |
| 59 | + |
| 60 | + AlwaysRun bool `yaml:"always_run,omitempty" json:"always_run,omitempty"` // automatic run or should triggered by comments. |
| 61 | + SkipAuthors []string `yaml:"skip_authors,omitempty" json:"skip_authors,omitempty"` // skip the pull request created by the authors. |
| 62 | + SkipBrancheRegs []string `yaml:"skip_branche_regs,omitempty" json:"skip_branche_regs,omitempty"` // skip the pull requests whiches target branch matched the regex. |
| 63 | + SkipLabelRegs []string `yaml:"skip_label_regs,omitempty" json:"skip_label_regs,omitempty"` // skip the pull reqeusts when any labels matched on the pull request. |
| 64 | + |
| 65 | + skipBrancheRegs []*regexp.Regexp `yaml:"-" json:"-"` |
| 66 | + skipLabelRegs []*regexp.Regexp `yaml:"-" json:"-"` |
| 67 | +} |
| 68 | + |
| 69 | +func (t *Task) initRegexps() error { |
| 70 | + if len(t.SkipBrancheRegs) != 0 { |
| 71 | + for _, br := range t.SkipBrancheRegs { |
| 72 | + reg, err := regexp.Compile(br) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + t.skipBrancheRegs = append(t.skipBrancheRegs, reg) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if len(t.SkipLabelRegs) != 0 { |
| 81 | + for _, br := range t.SkipLabelRegs { |
| 82 | + reg, err := regexp.Compile(br) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + t.skipLabelRegs = append(t.skipLabelRegs, reg) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +type ExternalContext struct { |
| 94 | + PromptTpl string `yaml:"prompt_tpl,omitempty" json:"prompt_tpl,omitempty"` |
| 95 | + ResURL string `yaml:"res_url,omitempty" json:"res_url,omitempty"` |
| 96 | + resContent []byte //nolint: unused // to be implemented. |
| 97 | +} |
| 98 | + |
| 99 | +func (ec *ExternalContext) Content() ([]byte, error) { |
| 100 | + return nil, errors.New("not implemented") |
| 101 | +} |
| 102 | + |
| 103 | +// TaskAgent agent for fetch tasks with watching and hot reload. |
| 104 | +type TaskAgent struct { |
| 105 | + ConfigAgent[TasksConfig] |
| 106 | +} |
| 107 | + |
| 108 | +// NewTaskAgent returns a new ConfigLoader. |
| 109 | +func NewTaskAgent(path string, watchInterval time.Duration) (*TaskAgent, error) { |
| 110 | + c := &TaskAgent{ConfigAgent: ConfigAgent[TasksConfig]{path: path}} |
| 111 | + if err := c.Reload(path); err != nil { |
| 112 | + return nil, err |
| 113 | + } |
| 114 | + |
| 115 | + go c.WatchConfig(context.Background(), watchInterval, c.Reload) |
| 116 | + |
| 117 | + return c, nil |
| 118 | +} |
| 119 | + |
| 120 | +func (a *TaskAgent) Reload(file string) error { |
| 121 | + return a.ConfigAgent.Reload(file, func() error { |
| 122 | + for _, repoCfg := range a.config { |
| 123 | + for _, task := range repoCfg { |
| 124 | + if err := task.initRegexps(); err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + return nil |
| 130 | + }) |
| 131 | +} |
| 132 | + |
| 133 | +// Get return the config data. |
| 134 | +func (c *TaskAgent) TasksFor(org, repo string) (map[string]*Task, error) { |
| 135 | + c.mu.RLock() |
| 136 | + defer c.mu.RUnlock() |
| 137 | + |
| 138 | + fullName := fmt.Sprintf("%s/%s", org, repo) |
| 139 | + repoTasks, ok := c.Data()[fullName] |
| 140 | + if ok { |
| 141 | + return repoTasks, nil |
| 142 | + } |
| 143 | + logrus.Debugf("no tasks for repo: %s", fullName) |
| 144 | + |
| 145 | + orgTasks, ok := c.config[org] |
| 146 | + if ok { |
| 147 | + return orgTasks, nil |
| 148 | + } |
| 149 | + logrus.Debugf("no tasks for org %s", org) |
| 150 | + logrus.Debugf("all tasks: %#v", c.config) |
| 151 | + return nil, nil |
| 152 | +} |
| 153 | + |
| 154 | +// Task return the given task config. |
| 155 | +func (c *TaskAgent) Task(org, repo, taskName string, needDefault bool) (*Task, error) { |
| 156 | + tasks, err := c.TasksFor(org, repo) |
| 157 | + if err != nil { |
| 158 | + return nil, err |
| 159 | + } |
| 160 | + |
| 161 | + task := tasks[taskName] |
| 162 | + if task != nil { |
| 163 | + return task, nil |
| 164 | + } |
| 165 | + |
| 166 | + if needDefault { |
| 167 | + return c.DefaultTask(), nil |
| 168 | + } |
| 169 | + |
| 170 | + return nil, nil |
| 171 | +} |
| 172 | + |
| 173 | +func (c *TaskAgent) DefaultTask() *Task { |
| 174 | + return &Task{ |
| 175 | + SystemMessage: defaultSystemMessage, |
| 176 | + UserPrompt: defaultPromte, |
| 177 | + MaxResponseTokens: defaultMaxResponseTokens, |
| 178 | + PatchIntroducePrompt: defaultPrPatchIntroducePromte, |
| 179 | + } |
| 180 | +} |
0 commit comments