55 "context"
66 "fmt"
77 "net/http"
8- "strings"
98
109 "github.com/cqfn/refrax/internal/brain"
1110 "github.com/cqfn/refrax/internal/domain"
@@ -17,48 +16,20 @@ import (
1716// Critic represents the main struct responsible for analyzing code critiques.
1817type Critic struct {
1918 server protocol.Server
20- brain brain.Brain
2119 log log.Logger
2220 port int
23- tools []tool. Tool
21+ agent * agent
2422}
2523
26- const notFound = "No suggestions found"
27-
28- const prompt = `Analyze the following Java code:
29-
30- {{code}}
31-
32- Identify possible improvements or flaws such as:
33-
34- * grammar and spelling mistakes in comments,
35- * variables that can be inlined or removed without changing functionality,
36- * unnecessary comments inside methods,
37- * redundant code.
38-
39- Don't suggest any changes that would alter the functionality of the code.
40- Don't suggest any changes that would require moving code parts between files (like extract class or extract an interface).
41- Don't suggest method renaming or class renaming.
42-
43- Keep in mind the following imperfections with Java code, identified by automated static analysis system:
44-
45- {{imperfections}}
46-
47- Respond with a few most relevant and important suggestion for improvement, formatted as a few lines of text.
48- If there are no suggestions or they are insignificant, respond with "{{not-found}}".
49- Do not include any explanations, summaries, or extra text.
50- `
51-
5224// NewCritic creates and initializes a new instance of Critic.
5325func NewCritic (ai brain.Brain , port int , tools ... tool.Tool ) * Critic {
5426 logger := log .NewPrefixed ("critic" , log .NewColored (log .Default (), log .Cyan ))
5527 server := protocol .NewServer (agentCard (port ), port )
5628 critic := & Critic {
5729 server : server ,
58- brain : ai ,
5930 log : logger ,
6031 port : port ,
61- tools : tools ,
32+ agent : & agent { brain : ai , log : logger , tools : tools } ,
6233 }
6334 server .MsgHandler (critic .think )
6435 critic .log .Debug ("preparing the Critic server on port %d with ai provider %s" , port , ai )
@@ -122,62 +93,8 @@ func (c *Critic) thinkLong(m *protocol.Message) (*protocol.Message, error) {
12293 if err != nil {
12394 return nil , fmt .Errorf ("failed to parse task from message: %w" , err )
12495 }
125- class := tsk .Classes [0 ]
126- c .log .Info ("received class %q for analysis" , class .Name ())
127- replacer := strings .NewReplacer (
128- "{{code}}" , class .Content (),
129- "{{imperfections}}" , tool .NewCombined (c .tools ... ).Imperfections (),
130- "{{not-found}}" , notFound ,
131- )
132- answer , err := c .brain .Ask (replacer .Replace (prompt ))
133- if err != nil {
134- return nil , fmt .Errorf ("failed to get answer from brain: %w" , err )
135- }
136- suggestions := parseAnswer (answer )
137- suggestions = associated (suggestions , class .Path ())
138- logSuggestions (c .log , suggestions )
139- all := make ([]domain.Suggestion , 0 , len (suggestions ))
140- for _ , suggestion := range suggestions {
141- if strings .EqualFold (suggestion , notFound ) {
142- c .log .Info ("no suggestions found for class %s" , class .Name ())
143- continue
144- }
145- s := domain .NewSuggestion (suggestion )
146- all = append (all , s )
147- }
148- artifacts := domain.Artifacts {
149- Descr : & domain.Description {
150- Text : fmt .Sprintf ("Critique for class %s" , class .Name ()),
151- },
152-
153- Suggestions : all ,
154- }
155- return artifacts .Marshal ().Message , nil
156- }
157-
158- func logSuggestions (logger log.Logger , suggestions []string ) {
159- for i , suggestion := range suggestions {
160- logger .Info ("suggestion #%d: %s" , i + 1 , suggestion )
161- }
162- }
163-
164- func parseAnswer (answer string ) []string {
165- lines := strings .Split (strings .TrimSpace (answer ), "\n " )
166- var suggestions []string
167- for _ , line := range lines {
168- suggestion := strings .TrimSpace (line )
169- if suggestion != "" {
170- suggestions = append (suggestions , suggestion )
171- }
172- }
173- return suggestions
174- }
175-
176- func associated (suggestions []string , classPath string ) []string {
177- for i , suggestion := range suggestions {
178- suggestions [i ] = fmt .Sprintf ("%s: %s" , classPath , suggestion )
179- }
180- return suggestions
96+ artifacts , err := c .agent .Review (tsk )
97+ return artifacts .Marshal ().Message , err
18198}
18299
183100func agentCard (port int ) * protocol.AgentCard {
0 commit comments