@@ -33,7 +33,8 @@ public final class LlamaServerAgent implements Disposable {
3333
3434 private static final Logger LOG = Logger .getInstance (LlamaServerAgent .class );
3535
36- private @ Nullable OSProcessHandler makeProcessHandler ;
36+ private @ Nullable OSProcessHandler makeSetupProcessHandler ;
37+ private @ Nullable OSProcessHandler makeBuildProcessHandler ;
3738 private @ Nullable OSProcessHandler startServerProcessHandler ;
3839 private ServerProgressPanel activeServerProgressPanel ;
3940 private boolean stoppedByUser ;
@@ -49,11 +50,44 @@ public void startAgent(
4950 stoppedByUser = false ;
5051 serverProgressPanel .displayText (
5152 CodeGPTBundle .get ("llamaServerAgent.buildingProject.description" ));
52- makeProcessHandler = new OSProcessHandler (
53- getMakeCommandLine (params ));
54- makeProcessHandler .addProcessListener (
55- getMakeProcessListener (params , onSuccess , onServerStopped ));
56- makeProcessHandler .startNotify ();
53+
54+ makeSetupProcessHandler = new OSProcessHandler (getCMakeSetupCommandLine (params ));
55+ makeSetupProcessHandler .addProcessListener (new ProcessAdapter () {
56+ private final List <String > errorLines = new CopyOnWriteArrayList <>();
57+
58+ @ Override
59+ public void onTextAvailable (@ NotNull ProcessEvent event , @ NotNull Key outputType ) {
60+ if (ProcessOutputType .isStderr (outputType )) {
61+ errorLines .add (event .getText ());
62+ return ;
63+ }
64+ LOG .info (event .getText ());
65+ }
66+
67+ @ Override
68+ public void processTerminated (@ NotNull ProcessEvent event ) {
69+ int exitCode = event .getExitCode ();
70+ LOG .info (format ("CMake setup exited with code %d" , exitCode ));
71+ if (stoppedByUser ) {
72+ onServerStopped .accept (activeServerProgressPanel );
73+ return ;
74+ }
75+ if (exitCode != 0 ) {
76+ showServerError (String .join ("," , errorLines ), onServerStopped );
77+ return ;
78+ }
79+
80+ try {
81+ makeBuildProcessHandler = new OSProcessHandler (getCMakeBuildCommandLine (params ));
82+ makeBuildProcessHandler .addProcessListener (
83+ getMakeProcessListener (params , onSuccess , onServerStopped ));
84+ makeBuildProcessHandler .startNotify ();
85+ } catch (ExecutionException e ) {
86+ showServerError (e .getMessage (), onServerStopped );
87+ }
88+ }
89+ });
90+ makeSetupProcessHandler .startNotify ();
5791 } catch (ExecutionException e ) {
5892 showServerError (e .getMessage (), onServerStopped );
5993 }
@@ -62,18 +96,18 @@ public void startAgent(
6296
6397 public void stopAgent () {
6498 stoppedByUser = true ;
65- if (makeProcessHandler != null ) {
66- makeProcessHandler .destroyProcess ();
99+ if (makeSetupProcessHandler != null ) {
100+ makeSetupProcessHandler .destroyProcess ();
67101 }
68102 if (startServerProcessHandler != null ) {
69103 startServerProcessHandler .destroyProcess ();
70104 }
71105 }
72106
73107 public boolean isServerRunning () {
74- return (makeProcessHandler != null
75- && makeProcessHandler .isStartNotified ()
76- && !makeProcessHandler .isProcessTerminated ())
108+ return (makeSetupProcessHandler != null
109+ && makeSetupProcessHandler .isStartNotified ()
110+ && !makeSetupProcessHandler .isProcessTerminated ())
77111 || (startServerProcessHandler != null
78112 && startServerProcessHandler .isStartNotified ()
79113 && !startServerProcessHandler .isProcessTerminated ());
@@ -147,25 +181,14 @@ public void processTerminated(@NotNull ProcessEvent event) {
147181
148182 @ Override
149183 public void onTextAvailable (@ NotNull ProcessEvent event , @ NotNull Key outputType ) {
150- if (ProcessOutputType .isStderr (outputType )) {
151- errorLines .add (event .getText ());
152- }
153-
154- if (ProcessOutputType .isStdout (outputType )) {
155- LOG .info (event .getText ());
184+ LOG .info (event .getText ());
156185
157- try {
158- var serverMessage = objectMapper .readValue (event .getText (), LlamaServerMessage .class );
159- // hack
160- if ("HTTP server listening" .equals (serverMessage .msg ())) {
161- LOG .info ("Server up and running!" );
186+ // TODO: Use proper successful boot up validation
187+ if (event .getText ().contains ("server is listening" )) {
188+ LOG .info ("Server up and running!" );
162189
163- LlamaSettings .getCurrentState ().setServerPort (port );
164- onSuccess .run ();
165- }
166- } catch (Exception ignore ) {
167- // ignore
168- }
190+ LlamaSettings .getCurrentState ().setServerPort (port );
191+ onSuccess .run ();
169192 }
170193 }
171194 };
@@ -177,20 +200,32 @@ private void showServerError(String errorText, Consumer<ServerProgressPanel> onS
177200 OverlayUtil .showClosableBalloon (errorText , MessageType .ERROR , activeServerProgressPanel );
178201 }
179202
180- private static GeneralCommandLine getMakeCommandLine (LlamaServerStartupParams params ) {
181- GeneralCommandLine commandLine = new GeneralCommandLine ().withCharset (StandardCharsets .UTF_8 );
182- commandLine .setExePath ("make" );
183- commandLine .withWorkDirectory (CodeGPTPlugin .getLlamaSourcePath ());
184- commandLine .addParameters ("-j" );
185- commandLine .addParameters (params .additionalBuildParameters ());
186- commandLine .withEnvironment (params .additionalEnvironmentVariables ());
187- commandLine .setRedirectErrorStream (false );
188- return commandLine ;
203+ private static GeneralCommandLine getCMakeSetupCommandLine (LlamaServerStartupParams params ) {
204+ GeneralCommandLine cmakeSetupCommand = new GeneralCommandLine ().withCharset (
205+ StandardCharsets .UTF_8 );
206+ cmakeSetupCommand .setExePath ("cmake" );
207+ cmakeSetupCommand .withWorkDirectory (CodeGPTPlugin .getLlamaSourcePath ());
208+ cmakeSetupCommand .addParameters ("-B" , "build" );
209+ cmakeSetupCommand .withEnvironment (params .additionalEnvironmentVariables ());
210+ cmakeSetupCommand .setRedirectErrorStream (false );
211+ return cmakeSetupCommand ;
212+ }
213+
214+ private static GeneralCommandLine getCMakeBuildCommandLine (LlamaServerStartupParams params ) {
215+ GeneralCommandLine cmakeBuildCommand = new GeneralCommandLine ().withCharset (
216+ StandardCharsets .UTF_8 );
217+ cmakeBuildCommand .setExePath ("cmake" );
218+ cmakeBuildCommand .withWorkDirectory (CodeGPTPlugin .getLlamaSourcePath ());
219+ cmakeBuildCommand .addParameters ("--build" , "build" , "--config" , "Release" , "-t" , "llama-server" ,
220+ "-j" , "4" );
221+ cmakeBuildCommand .withEnvironment (params .additionalEnvironmentVariables ());
222+ cmakeBuildCommand .setRedirectErrorStream (false );
223+ return cmakeBuildCommand ;
189224 }
190225
191226 private GeneralCommandLine getServerCommandLine (LlamaServerStartupParams params ) {
192227 GeneralCommandLine commandLine = new GeneralCommandLine ().withCharset (StandardCharsets .UTF_8 );
193- commandLine .setExePath ("./server" );
228+ commandLine .setExePath ("./build/bin/llama- server" );
194229 commandLine .withWorkDirectory (CodeGPTPlugin .getLlamaSourcePath ());
195230 commandLine .addParameters (
196231 "-m" , params .modelPath (),
@@ -210,8 +245,8 @@ public void setActiveServerProgressPanel(
210245
211246 @ Override
212247 public void dispose () {
213- if (makeProcessHandler != null && !makeProcessHandler .isProcessTerminated ()) {
214- makeProcessHandler .destroyProcess ();
248+ if (makeSetupProcessHandler != null && !makeSetupProcessHandler .isProcessTerminated ()) {
249+ makeSetupProcessHandler .destroyProcess ();
215250 }
216251 if (startServerProcessHandler != null && !startServerProcessHandler .isProcessTerminated ()) {
217252 startServerProcessHandler .destroyProcess ();
0 commit comments