|
| 1 | +/** |
| 2 | + * Copyright 2025 ByteDance Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package agent |
| 18 | + |
| 19 | +import ( |
| 20 | + "bufio" |
| 21 | + "context" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "strings" |
| 25 | + |
| 26 | + "github.com/cloudwego/abcoder/llm" |
| 27 | + "github.com/cloudwego/abcoder/llm/log" |
| 28 | + "github.com/cloudwego/eino/compose" |
| 29 | + "github.com/cloudwego/eino/flow/agent" |
| 30 | + "github.com/cloudwego/eino/schema" |
| 31 | +) |
| 32 | + |
| 33 | +type AgentOptions struct { |
| 34 | + ASTsDir string |
| 35 | + MaxHistories int |
| 36 | + MaxSteps int |
| 37 | + Model llm.ModelConfig |
| 38 | +} |
| 39 | + |
| 40 | +type Agent struct { |
| 41 | + opts AgentOptions |
| 42 | + analyzer *llm.ReactAgent |
| 43 | + histories *Histories |
| 44 | +} |
| 45 | + |
| 46 | +// run agent as a repl cmd server |
| 47 | +func NewAgent(opts AgentOptions) *Agent { |
| 48 | + ag := NewRepoAnalyzer(context.Background(), RepoAnnalyzerOptions{ |
| 49 | + ASTsDir: opts.ASTsDir, |
| 50 | + MaxSteps: opts.MaxSteps, |
| 51 | + ModelConfig: opts.Model, |
| 52 | + }) |
| 53 | + |
| 54 | + histories := NewHistories(opts.MaxHistories) |
| 55 | + |
| 56 | + return &Agent{ |
| 57 | + opts: opts, |
| 58 | + analyzer: ag, |
| 59 | + histories: histories, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func (a *Agent) Generate(ctx context.Context, msgs []*schema.Message) (*schema.Message, error) { |
| 64 | + return a.analyzer.Generate(ctx, msgs, agent.WithComposeOptions(compose.WithCallbacks(llm.CallbackHandler{}))) |
| 65 | +} |
| 66 | + |
| 67 | +func (a *Agent) Run(ctx context.Context) { |
| 68 | + fmt.Fprintf(os.Stdout, "Hello! I'm ABCoder, your coding assistant. What can I do for you today?\n") |
| 69 | + |
| 70 | + sc := bufio.NewScanner(os.Stdin) |
| 71 | + |
| 72 | + for sc.Scan() { |
| 73 | + |
| 74 | + query := strings.TrimSpace(sc.Text()) |
| 75 | + if query == "" { |
| 76 | + continue |
| 77 | + } |
| 78 | + if query == "exit" { |
| 79 | + break |
| 80 | + } |
| 81 | + |
| 82 | + // get histories |
| 83 | + a.histories.Add(&schema.Message{ |
| 84 | + Role: schema.User, |
| 85 | + Content: query, |
| 86 | + }) |
| 87 | + |
| 88 | + resp, err := a.Generate(ctx, a.histories.Get()) |
| 89 | + if err != nil { |
| 90 | + log.Error("Failed to run agent: %v\n", err) |
| 91 | + continue |
| 92 | + } |
| 93 | + |
| 94 | + a.histories.Add(resp) |
| 95 | + |
| 96 | + fmt.Fprintf(os.Stdout, "\n%s\n", resp.Content) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +type Histories struct { |
| 101 | + max int |
| 102 | + hs []*schema.Message |
| 103 | +} |
| 104 | + |
| 105 | +func NewHistories(max int) *Histories { |
| 106 | + return &Histories{ |
| 107 | + max: max, |
| 108 | + hs: make([]*schema.Message, 0, max), |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func (h *Histories) Add(msg *schema.Message) { |
| 113 | + if len(h.hs) >= h.max { |
| 114 | + h.hs = h.hs[1:] |
| 115 | + } |
| 116 | + h.hs = append(h.hs, msg) |
| 117 | +} |
| 118 | + |
| 119 | +func (h *Histories) Get() []*schema.Message { |
| 120 | + return h.hs |
| 121 | +} |
0 commit comments