11package ee .carlrobert .codegpt .completions .llama ;
22
3+ import static java .lang .String .format ;
4+
35import com .fasterxml .jackson .databind .ObjectMapper ;
46import com .intellij .execution .ExecutionException ;
57import com .intellij .execution .configurations .GeneralCommandLine ;
911import com .intellij .execution .process .ProcessListener ;
1012import com .intellij .execution .process .ProcessOutputType ;
1113import com .intellij .icons .AllIcons .Actions ;
14+ import com .intellij .notification .NotificationType ;
1215import com .intellij .openapi .Disposable ;
1316import com .intellij .openapi .application .ApplicationManager ;
1417import com .intellij .openapi .components .Service ;
1518import com .intellij .openapi .diagnostic .Logger ;
1619import com .intellij .openapi .util .Key ;
1720import com .intellij .ui .components .JBLabel ;
21+ import ee .carlrobert .codegpt .CodeGPTBundle ;
1822import ee .carlrobert .codegpt .CodeGPTPlugin ;
19- import ee .carlrobert .codegpt .settings .service .LlamaServiceSelectionForm ;
2023import ee .carlrobert .codegpt .settings .service .ServerProgressPanel ;
2124import ee .carlrobert .codegpt .settings .state .LlamaSettingsState ;
25+ import ee .carlrobert .codegpt .util .OverlayUtil ;
2226import java .nio .charset .StandardCharsets ;
2327import java .util .List ;
28+ import java .util .concurrent .CopyOnWriteArrayList ;
2429import javax .swing .SwingConstants ;
2530import org .jetbrains .annotations .NotNull ;
2631import org .jetbrains .annotations .Nullable ;
@@ -30,19 +35,20 @@ public final class LlamaServerAgent implements Disposable {
3035
3136 private static final Logger LOG = Logger .getInstance (LlamaServerAgent .class );
3237
33- private static @ Nullable OSProcessHandler makeProcessHandler ;
34- private static @ Nullable OSProcessHandler startServerProcessHandler ;
38+ private @ Nullable OSProcessHandler makeProcessHandler ;
39+ private @ Nullable OSProcessHandler startServerProcessHandler ;
3540
3641 public void startAgent (
37- LlamaServiceSelectionForm llamaServiceSelectionForm ,
42+ LlamaServerStartupParams params ,
3843 ServerProgressPanel serverProgressPanel ,
3944 Runnable onSuccess ) {
4045 ApplicationManager .getApplication ().invokeLater (() -> {
4146 try {
42- serverProgressPanel .updateText ("Building llama.cpp..." );
47+ serverProgressPanel .updateText (
48+ CodeGPTBundle .get ("llamaServerAgent.buildingProject.description" ));
4349 makeProcessHandler = new OSProcessHandler (getMakeCommandLinde ());
4450 makeProcessHandler .addProcessListener (
45- getMakeProcessListener (llamaServiceSelectionForm , serverProgressPanel , onSuccess ));
51+ getMakeProcessListener (params , serverProgressPanel , onSuccess ));
4652 makeProcessHandler .startNotify ();
4753 } catch (ExecutionException e ) {
4854 throw new RuntimeException (e );
@@ -63,9 +69,11 @@ public boolean isServerRunning() {
6369 }
6470
6571 private ProcessListener getMakeProcessListener (
66- LlamaServiceSelectionForm serviceSelectionForm ,
72+ LlamaServerStartupParams params ,
6773 ServerProgressPanel serverProgressPanel ,
6874 Runnable onSuccess ) {
75+ LOG .info ("Building llama project" );
76+
6977 return new ProcessAdapter () {
7078 @ Override
7179 public void onTextAvailable (@ NotNull ProcessEvent event , @ NotNull Key outputType ) {
@@ -75,21 +83,16 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
7583 @ Override
7684 public void processTerminated (@ NotNull ProcessEvent event ) {
7785 try {
78- serverProgressPanel .updateText ("Booting up server..." );
79- startServerProcessHandler = new OSProcessHandler .Silent (
80- getServerCommandLine (
81- serviceSelectionForm .getLlamaModelPreferencesForm ().getActualModelPath (),
82- serviceSelectionForm .getContextSize (),
83- serviceSelectionForm .getThreads (),
84- serviceSelectionForm .getServerPort (),
85- serviceSelectionForm .getListOfAdditionalParameters ()));
86- startServerProcessHandler .addProcessListener (getProcessListener (
87- serviceSelectionForm .getServerPort (),
88- serverProgressPanel ,
89- onSuccess ));
86+ LOG .info ("Booting up llama server" );
87+
88+ serverProgressPanel .updateText (
89+ CodeGPTBundle .get ("llamaServerAgent.serverBootup.description" ));
90+ startServerProcessHandler = new OSProcessHandler .Silent (getServerCommandLine (params ));
91+ startServerProcessHandler .addProcessListener (
92+ getProcessListener (params .getPort (), serverProgressPanel , onSuccess ));
9093 startServerProcessHandler .startNotify ();
9194 } catch (ExecutionException ex ) {
92- LOG .error ("Unable to start the server" , ex );
95+ LOG .error ("Unable to start llama server" , ex );
9396 throw new RuntimeException (ex );
9497 }
9598 }
@@ -102,9 +105,18 @@ private ProcessListener getProcessListener(
102105 Runnable onSuccess ) {
103106 return new ProcessAdapter () {
104107 private final ObjectMapper objectMapper = new ObjectMapper ();
108+ private final List <String > errorLines = new CopyOnWriteArrayList <>();
105109
106110 @ Override
107111 public void processTerminated (@ NotNull ProcessEvent event ) {
112+ if (errorLines .isEmpty ()) {
113+ LOG .info (format ("Server terminated with code %d" , event .getExitCode ()));
114+ } else {
115+ var error = String .join ("" , errorLines );
116+ OverlayUtil .showNotification (error , NotificationType .ERROR );
117+ LOG .error (error );
118+ }
119+
108120 serverProgressPanel .displayComponent (new JBLabel (
109121 "Server terminated" ,
110122 Actions .Cancel ,
@@ -113,12 +125,19 @@ public void processTerminated(@NotNull ProcessEvent event) {
113125
114126 @ Override
115127 public void onTextAvailable (@ NotNull ProcessEvent event , @ NotNull Key outputType ) {
116- LOG .debug (event .getText ());
128+ if (ProcessOutputType .isStderr (outputType )) {
129+ errorLines .add (event .getText ());
130+ return ;
131+ }
132+
133+ if (ProcessOutputType .isStdout (outputType )) {
134+ LOG .info (event .getText ());
117135
118- if (outputType == ProcessOutputType .STDOUT ) {
119136 try {
120137 var serverMessage = objectMapper .readValue (event .getText (), LlamaServerMessage .class );
121138 if ("HTTP server listening" .equals (serverMessage .getMessage ())) {
139+ LOG .info ("Server up and running!" );
140+
122141 LlamaSettingsState .getInstance ().setServerPort (port );
123142 onSuccess .run ();
124143 }
@@ -139,21 +158,16 @@ private static GeneralCommandLine getMakeCommandLinde() {
139158 return commandLine ;
140159 }
141160
142- private GeneralCommandLine getServerCommandLine (
143- String modelPath ,
144- int contextLength ,
145- int threads ,
146- int port ,
147- List <String > additionalParameters ) {
161+ private GeneralCommandLine getServerCommandLine (LlamaServerStartupParams params ) {
148162 GeneralCommandLine commandLine = new GeneralCommandLine ().withCharset (StandardCharsets .UTF_8 );
149163 commandLine .setExePath ("./server" );
150164 commandLine .withWorkDirectory (CodeGPTPlugin .getLlamaSourcePath ());
151165 commandLine .addParameters (
152- "-m" , modelPath ,
153- "-c" , String .valueOf (contextLength ),
154- "--port" , String .valueOf (port ),
155- "-t" , String .valueOf (threads ));
156- commandLine .addParameters (additionalParameters );
166+ "-m" , params . getModelPath () ,
167+ "-c" , String .valueOf (params . getContextLength () ),
168+ "--port" , String .valueOf (params . getPort () ),
169+ "-t" , String .valueOf (params . getThreads () ));
170+ commandLine .addParameters (params . getAdditionalParameters () );
157171 commandLine .setRedirectErrorStream (false );
158172 return commandLine ;
159173 }
0 commit comments