1414import com .intellij .openapi .application .ApplicationManager ;
1515import com .intellij .openapi .components .Service ;
1616import com .intellij .openapi .diagnostic .Logger ;
17+ import com .intellij .openapi .ui .MessageType ;
1718import com .intellij .openapi .util .Key ;
1819import ee .carlrobert .codegpt .CodeGPTBundle ;
1920import ee .carlrobert .codegpt .CodeGPTPlugin ;
2021import ee .carlrobert .codegpt .settings .service .llama .LlamaSettings ;
2122import ee .carlrobert .codegpt .settings .service .llama .form .ServerProgressPanel ;
23+ import ee .carlrobert .codegpt .ui .OverlayUtil ;
2224import java .nio .charset .StandardCharsets ;
2325import java .util .List ;
2426import java .util .concurrent .CopyOnWriteArrayList ;
27+ import java .util .function .Consumer ;
2528import org .jetbrains .annotations .NotNull ;
2629import org .jetbrains .annotations .Nullable ;
2730
@@ -32,65 +35,94 @@ public final class LlamaServerAgent implements Disposable {
3235
3336 private @ Nullable OSProcessHandler makeProcessHandler ;
3437 private @ Nullable OSProcessHandler startServerProcessHandler ;
38+ private ServerProgressPanel activeServerProgressPanel ;
39+ private boolean stoppedByUser ;
3540
3641 public void startAgent (
3742 LlamaServerStartupParams params ,
3843 ServerProgressPanel serverProgressPanel ,
3944 Runnable onSuccess ,
40- Runnable onServerTerminated ) {
45+ Consumer <ServerProgressPanel > onServerTerminated ) {
46+ this .activeServerProgressPanel = serverProgressPanel ;
4147 ApplicationManager .getApplication ().invokeLater (() -> {
4248 try {
43- serverProgressPanel .updateText (
49+ stoppedByUser = false ;
50+ serverProgressPanel .displayText (
4451 CodeGPTBundle .get ("llamaServerAgent.buildingProject.description" ));
45- makeProcessHandler = new OSProcessHandler (getMakeCommandLinde ());
52+ makeProcessHandler = new OSProcessHandler (
53+ getMakeCommandLine (params .additionalBuildParameters ()));
4654 makeProcessHandler .addProcessListener (
47- getMakeProcessListener (params , serverProgressPanel , onSuccess , onServerTerminated ));
55+ getMakeProcessListener (params , onSuccess , onServerTerminated ));
4856 makeProcessHandler .startNotify ();
4957 } catch (ExecutionException e ) {
50- throw new RuntimeException ( e );
58+ showServerError ( e . getMessage (), onServerTerminated );
5159 }
5260 });
5361 }
5462
5563 public void stopAgent () {
64+ stoppedByUser = true ;
65+ if (makeProcessHandler != null ) {
66+ makeProcessHandler .destroyProcess ();
67+ }
5668 if (startServerProcessHandler != null ) {
5769 startServerProcessHandler .destroyProcess ();
5870 }
5971 }
6072
6173 public boolean isServerRunning () {
62- return startServerProcessHandler != null
74+ return (makeProcessHandler != null
75+ && makeProcessHandler .isStartNotified ()
76+ && !makeProcessHandler .isProcessTerminated ())
77+ || (startServerProcessHandler != null
6378 && startServerProcessHandler .isStartNotified ()
64- && !startServerProcessHandler .isProcessTerminated ();
79+ && !startServerProcessHandler .isProcessTerminated ()) ;
6580 }
6681
6782 private ProcessListener getMakeProcessListener (
6883 LlamaServerStartupParams params ,
69- ServerProgressPanel serverProgressPanel ,
7084 Runnable onSuccess ,
71- Runnable onServerTerminated ) {
85+ Consumer < ServerProgressPanel > onServerTerminated ) {
7286 LOG .info ("Building llama project" );
7387
7488 return new ProcessAdapter () {
89+
90+ private final List <String > errorLines = new CopyOnWriteArrayList <>();
91+
7592 @ Override
7693 public void onTextAvailable (@ NotNull ProcessEvent event , @ NotNull Key outputType ) {
94+ if (ProcessOutputType .isStderr (outputType )) {
95+ errorLines .add (event .getText ());
96+ return ;
97+ }
7798 LOG .info (event .getText ());
7899 }
79100
80101 @ Override
81102 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+
82114 try {
83115 LOG .info ("Booting up llama server" );
84116
85- serverProgressPanel . updateText (
117+ activeServerProgressPanel . displayText (
86118 CodeGPTBundle .get ("llamaServerAgent.serverBootup.description" ));
87119 startServerProcessHandler = new OSProcessHandler .Silent (getServerCommandLine (params ));
88120 startServerProcessHandler .addProcessListener (
89- getProcessListener (params .port (), onSuccess , onServerTerminated ));
121+ getProcessListener (params .port (), onSuccess ,
122+ onServerTerminated ));
90123 startServerProcessHandler .startNotify ();
91124 } catch (ExecutionException ex ) {
92- LOG .error ("Unable to start llama server" , ex );
93- throw new RuntimeException (ex );
125+ showServerError (ex .getMessage (), onServerTerminated );
94126 }
95127 }
96128 };
@@ -99,27 +131,25 @@ public void processTerminated(@NotNull ProcessEvent event) {
99131 private ProcessListener getProcessListener (
100132 int port ,
101133 Runnable onSuccess ,
102- Runnable onServerTerminated ) {
134+ Consumer < ServerProgressPanel > onServerTerminated ) {
103135 return new ProcessAdapter () {
104136 private final ObjectMapper objectMapper = new ObjectMapper ();
105137 private final List <String > errorLines = new CopyOnWriteArrayList <>();
106138
107139 @ Override
108140 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 );
111144 } else {
112- LOG . info (String .join ("" , errorLines ));
145+ showServerError (String .join (", " , errorLines ), onServerTerminated );
113146 }
114-
115- onServerTerminated .run ();
116147 }
117148
118149 @ Override
119150 public void onTextAvailable (@ NotNull ProcessEvent event , @ NotNull Key outputType ) {
120151 if (ProcessOutputType .isStderr (outputType )) {
121152 errorLines .add (event .getText ());
122- return ;
123153 }
124154
125155 if (ProcessOutputType .isStdout (outputType )) {
@@ -141,11 +171,18 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
141171 };
142172 }
143173
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 ) {
145181 GeneralCommandLine commandLine = new GeneralCommandLine ().withCharset (StandardCharsets .UTF_8 );
146182 commandLine .setExePath ("make" );
147183 commandLine .withWorkDirectory (CodeGPTPlugin .getLlamaSourcePath ());
148184 commandLine .addParameters ("-j" );
185+ commandLine .addParameters (additionalCompileParameters );
149186 commandLine .setRedirectErrorStream (false );
150187 return commandLine ;
151188 }
@@ -159,11 +196,16 @@ private GeneralCommandLine getServerCommandLine(LlamaServerStartupParams params)
159196 "-c" , String .valueOf (params .contextLength ()),
160197 "--port" , String .valueOf (params .port ()),
161198 "-t" , String .valueOf (params .threads ()));
162- commandLine .addParameters (params .additionalParameters ());
199+ commandLine .addParameters (params .additionalRunParameters ());
163200 commandLine .setRedirectErrorStream (false );
164201 return commandLine ;
165202 }
166203
204+ public void setActiveServerProgressPanel (
205+ ServerProgressPanel activeServerProgressPanel ) {
206+ this .activeServerProgressPanel = activeServerProgressPanel ;
207+ }
208+
167209 @ Override
168210 public void dispose () {
169211 if (makeProcessHandler != null && !makeProcessHandler .isProcessTerminated ()) {
0 commit comments