14
14
import com .intellij .openapi .application .ApplicationManager ;
15
15
import com .intellij .openapi .components .Service ;
16
16
import com .intellij .openapi .diagnostic .Logger ;
17
+ import com .intellij .openapi .ui .MessageType ;
17
18
import com .intellij .openapi .util .Key ;
18
19
import ee .carlrobert .codegpt .CodeGPTBundle ;
19
20
import ee .carlrobert .codegpt .CodeGPTPlugin ;
20
21
import ee .carlrobert .codegpt .settings .service .llama .LlamaSettings ;
21
22
import ee .carlrobert .codegpt .settings .service .llama .form .ServerProgressPanel ;
23
+ import ee .carlrobert .codegpt .ui .OverlayUtil ;
22
24
import java .nio .charset .StandardCharsets ;
23
25
import java .util .List ;
24
26
import java .util .concurrent .CopyOnWriteArrayList ;
27
+ import java .util .function .Consumer ;
25
28
import org .jetbrains .annotations .NotNull ;
26
29
import org .jetbrains .annotations .Nullable ;
27
30
@@ -32,65 +35,94 @@ public final class LlamaServerAgent implements Disposable {
32
35
33
36
private @ Nullable OSProcessHandler makeProcessHandler ;
34
37
private @ Nullable OSProcessHandler startServerProcessHandler ;
38
+ private ServerProgressPanel activeServerProgressPanel ;
39
+ private boolean stoppedByUser ;
35
40
36
41
public void startAgent (
37
42
LlamaServerStartupParams params ,
38
43
ServerProgressPanel serverProgressPanel ,
39
44
Runnable onSuccess ,
40
- Runnable onServerTerminated ) {
45
+ Consumer <ServerProgressPanel > onServerTerminated ) {
46
+ this .activeServerProgressPanel = serverProgressPanel ;
41
47
ApplicationManager .getApplication ().invokeLater (() -> {
42
48
try {
43
- serverProgressPanel .updateText (
49
+ stoppedByUser = false ;
50
+ serverProgressPanel .displayText (
44
51
CodeGPTBundle .get ("llamaServerAgent.buildingProject.description" ));
45
- makeProcessHandler = new OSProcessHandler (getMakeCommandLinde ());
52
+ makeProcessHandler = new OSProcessHandler (
53
+ getMakeCommandLine (params .additionalBuildParameters ()));
46
54
makeProcessHandler .addProcessListener (
47
- getMakeProcessListener (params , serverProgressPanel , onSuccess , onServerTerminated ));
55
+ getMakeProcessListener (params , onSuccess , onServerTerminated ));
48
56
makeProcessHandler .startNotify ();
49
57
} catch (ExecutionException e ) {
50
- throw new RuntimeException ( e );
58
+ showServerError ( e . getMessage (), onServerTerminated );
51
59
}
52
60
});
53
61
}
54
62
55
63
public void stopAgent () {
64
+ stoppedByUser = true ;
65
+ if (makeProcessHandler != null ) {
66
+ makeProcessHandler .destroyProcess ();
67
+ }
56
68
if (startServerProcessHandler != null ) {
57
69
startServerProcessHandler .destroyProcess ();
58
70
}
59
71
}
60
72
61
73
public boolean isServerRunning () {
62
- return startServerProcessHandler != null
74
+ return (makeProcessHandler != null
75
+ && makeProcessHandler .isStartNotified ()
76
+ && !makeProcessHandler .isProcessTerminated ())
77
+ || (startServerProcessHandler != null
63
78
&& startServerProcessHandler .isStartNotified ()
64
- && !startServerProcessHandler .isProcessTerminated ();
79
+ && !startServerProcessHandler .isProcessTerminated ()) ;
65
80
}
66
81
67
82
private ProcessListener getMakeProcessListener (
68
83
LlamaServerStartupParams params ,
69
- ServerProgressPanel serverProgressPanel ,
70
84
Runnable onSuccess ,
71
- Runnable onServerTerminated ) {
85
+ Consumer < ServerProgressPanel > onServerTerminated ) {
72
86
LOG .info ("Building llama project" );
73
87
74
88
return new ProcessAdapter () {
89
+
90
+ private final List <String > errorLines = new CopyOnWriteArrayList <>();
91
+
75
92
@ Override
76
93
public void onTextAvailable (@ NotNull ProcessEvent event , @ NotNull Key outputType ) {
94
+ if (ProcessOutputType .isStderr (outputType )) {
95
+ errorLines .add (event .getText ());
96
+ return ;
97
+ }
77
98
LOG .info (event .getText ());
78
99
}
79
100
80
101
@ Override
81
102
public void processTerminated (@ NotNull ProcessEvent event ) {
103
+ int exitCode = event .getExitCode ();
104
+ LOG .info (format ("Server build exited with code %d" , exitCode ));
105
+ if (stoppedByUser ) {
106
+ onServerTerminated .accept (activeServerProgressPanel );
107
+ return ;
108
+ }
109
+ if (exitCode != 0 ) {
110
+ showServerError (String .join ("," , errorLines ), onServerTerminated );
111
+ return ;
112
+ }
113
+
82
114
try {
83
115
LOG .info ("Booting up llama server" );
84
116
85
- serverProgressPanel . updateText (
117
+ activeServerProgressPanel . displayText (
86
118
CodeGPTBundle .get ("llamaServerAgent.serverBootup.description" ));
87
119
startServerProcessHandler = new OSProcessHandler .Silent (getServerCommandLine (params ));
88
120
startServerProcessHandler .addProcessListener (
89
- getProcessListener (params .port (), onSuccess , onServerTerminated ));
121
+ getProcessListener (params .port (), onSuccess ,
122
+ onServerTerminated ));
90
123
startServerProcessHandler .startNotify ();
91
124
} catch (ExecutionException ex ) {
92
- LOG .error ("Unable to start llama server" , ex );
93
- throw new RuntimeException (ex );
125
+ showServerError (ex .getMessage (), onServerTerminated );
94
126
}
95
127
}
96
128
};
@@ -99,27 +131,25 @@ public void processTerminated(@NotNull ProcessEvent event) {
99
131
private ProcessListener getProcessListener (
100
132
int port ,
101
133
Runnable onSuccess ,
102
- Runnable onServerTerminated ) {
134
+ Consumer < ServerProgressPanel > onServerTerminated ) {
103
135
return new ProcessAdapter () {
104
136
private final ObjectMapper objectMapper = new ObjectMapper ();
105
137
private final List <String > errorLines = new CopyOnWriteArrayList <>();
106
138
107
139
@ Override
108
140
public void processTerminated (@ NotNull ProcessEvent event ) {
109
- if (errorLines .isEmpty ()) {
110
- LOG .info (format ("Server terminated with code %d" , event .getExitCode ()));
141
+ LOG .info (format ("Server terminated with code %d" , event .getExitCode ()));
142
+ if (stoppedByUser ) {
143
+ onServerTerminated .accept (activeServerProgressPanel );
111
144
} else {
112
- LOG . info (String .join ("" , errorLines ));
145
+ showServerError (String .join (", " , errorLines ), onServerTerminated );
113
146
}
114
-
115
- onServerTerminated .run ();
116
147
}
117
148
118
149
@ Override
119
150
public void onTextAvailable (@ NotNull ProcessEvent event , @ NotNull Key outputType ) {
120
151
if (ProcessOutputType .isStderr (outputType )) {
121
152
errorLines .add (event .getText ());
122
- return ;
123
153
}
124
154
125
155
if (ProcessOutputType .isStdout (outputType )) {
@@ -141,11 +171,18 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
141
171
};
142
172
}
143
173
144
- private static GeneralCommandLine getMakeCommandLinde () {
174
+ private void showServerError (String errorText , Consumer <ServerProgressPanel > onServerTerminated ) {
175
+ onServerTerminated .accept (activeServerProgressPanel );
176
+ LOG .info ("Unable to start llama server:\n " + errorText );
177
+ OverlayUtil .showClosableBalloon (errorText , MessageType .ERROR , activeServerProgressPanel );
178
+ }
179
+
180
+ private static GeneralCommandLine getMakeCommandLine (List <String > additionalCompileParameters ) {
145
181
GeneralCommandLine commandLine = new GeneralCommandLine ().withCharset (StandardCharsets .UTF_8 );
146
182
commandLine .setExePath ("make" );
147
183
commandLine .withWorkDirectory (CodeGPTPlugin .getLlamaSourcePath ());
148
184
commandLine .addParameters ("-j" );
185
+ commandLine .addParameters (additionalCompileParameters );
149
186
commandLine .setRedirectErrorStream (false );
150
187
return commandLine ;
151
188
}
@@ -159,11 +196,16 @@ private GeneralCommandLine getServerCommandLine(LlamaServerStartupParams params)
159
196
"-c" , String .valueOf (params .contextLength ()),
160
197
"--port" , String .valueOf (params .port ()),
161
198
"-t" , String .valueOf (params .threads ()));
162
- commandLine .addParameters (params .additionalParameters ());
199
+ commandLine .addParameters (params .additionalRunParameters ());
163
200
commandLine .setRedirectErrorStream (false );
164
201
return commandLine ;
165
202
}
166
203
204
+ public void setActiveServerProgressPanel (
205
+ ServerProgressPanel activeServerProgressPanel ) {
206
+ this .activeServerProgressPanel = activeServerProgressPanel ;
207
+ }
208
+
167
209
@ Override
168
210
public void dispose () {
169
211
if (makeProcessHandler != null && !makeProcessHandler .isProcessTerminated ()) {
0 commit comments