1
1
package ee .carlrobert .codegpt .completions .llama ;
2
2
3
+ import static java .lang .String .format ;
4
+
3
5
import com .fasterxml .jackson .databind .ObjectMapper ;
4
6
import com .intellij .execution .ExecutionException ;
5
7
import com .intellij .execution .configurations .GeneralCommandLine ;
9
11
import com .intellij .execution .process .ProcessListener ;
10
12
import com .intellij .execution .process .ProcessOutputType ;
11
13
import com .intellij .icons .AllIcons .Actions ;
14
+ import com .intellij .notification .NotificationType ;
12
15
import com .intellij .openapi .Disposable ;
13
16
import com .intellij .openapi .application .ApplicationManager ;
14
17
import com .intellij .openapi .components .Service ;
15
18
import com .intellij .openapi .diagnostic .Logger ;
16
19
import com .intellij .openapi .util .Key ;
17
20
import com .intellij .ui .components .JBLabel ;
21
+ import ee .carlrobert .codegpt .CodeGPTBundle ;
18
22
import ee .carlrobert .codegpt .CodeGPTPlugin ;
19
- import ee .carlrobert .codegpt .settings .service .LlamaServiceSelectionForm ;
20
23
import ee .carlrobert .codegpt .settings .service .ServerProgressPanel ;
21
24
import ee .carlrobert .codegpt .settings .state .LlamaSettingsState ;
25
+ import ee .carlrobert .codegpt .util .OverlayUtil ;
22
26
import java .nio .charset .StandardCharsets ;
23
27
import java .util .List ;
28
+ import java .util .concurrent .CopyOnWriteArrayList ;
24
29
import javax .swing .SwingConstants ;
25
30
import org .jetbrains .annotations .NotNull ;
26
31
import org .jetbrains .annotations .Nullable ;
@@ -30,19 +35,20 @@ public final class LlamaServerAgent implements Disposable {
30
35
31
36
private static final Logger LOG = Logger .getInstance (LlamaServerAgent .class );
32
37
33
- private static @ Nullable OSProcessHandler makeProcessHandler ;
34
- private static @ Nullable OSProcessHandler startServerProcessHandler ;
38
+ private @ Nullable OSProcessHandler makeProcessHandler ;
39
+ private @ Nullable OSProcessHandler startServerProcessHandler ;
35
40
36
41
public void startAgent (
37
- LlamaServiceSelectionForm llamaServiceSelectionForm ,
42
+ LlamaServerStartupParams params ,
38
43
ServerProgressPanel serverProgressPanel ,
39
44
Runnable onSuccess ) {
40
45
ApplicationManager .getApplication ().invokeLater (() -> {
41
46
try {
42
- serverProgressPanel .updateText ("Building llama.cpp..." );
47
+ serverProgressPanel .updateText (
48
+ CodeGPTBundle .get ("llamaServerAgent.buildingProject.description" ));
43
49
makeProcessHandler = new OSProcessHandler (getMakeCommandLinde ());
44
50
makeProcessHandler .addProcessListener (
45
- getMakeProcessListener (llamaServiceSelectionForm , serverProgressPanel , onSuccess ));
51
+ getMakeProcessListener (params , serverProgressPanel , onSuccess ));
46
52
makeProcessHandler .startNotify ();
47
53
} catch (ExecutionException e ) {
48
54
throw new RuntimeException (e );
@@ -63,9 +69,11 @@ public boolean isServerRunning() {
63
69
}
64
70
65
71
private ProcessListener getMakeProcessListener (
66
- LlamaServiceSelectionForm serviceSelectionForm ,
72
+ LlamaServerStartupParams params ,
67
73
ServerProgressPanel serverProgressPanel ,
68
74
Runnable onSuccess ) {
75
+ LOG .info ("Building llama project" );
76
+
69
77
return new ProcessAdapter () {
70
78
@ Override
71
79
public void onTextAvailable (@ NotNull ProcessEvent event , @ NotNull Key outputType ) {
@@ -75,21 +83,16 @@ public void onTextAvailable(@NotNull ProcessEvent event, @NotNull Key outputType
75
83
@ Override
76
84
public void processTerminated (@ NotNull ProcessEvent event ) {
77
85
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 ));
90
93
startServerProcessHandler .startNotify ();
91
94
} catch (ExecutionException ex ) {
92
- LOG .error ("Unable to start the server" , ex );
95
+ LOG .error ("Unable to start llama server" , ex );
93
96
throw new RuntimeException (ex );
94
97
}
95
98
}
@@ -102,9 +105,18 @@ private ProcessListener getProcessListener(
102
105
Runnable onSuccess ) {
103
106
return new ProcessAdapter () {
104
107
private final ObjectMapper objectMapper = new ObjectMapper ();
108
+ private final List <String > errorLines = new CopyOnWriteArrayList <>();
105
109
106
110
@ Override
107
111
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
+
108
120
serverProgressPanel .displayComponent (new JBLabel (
109
121
"Server terminated" ,
110
122
Actions .Cancel ,
@@ -113,12 +125,19 @@ public void processTerminated(@NotNull ProcessEvent event) {
113
125
114
126
@ Override
115
127
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 ());
117
135
118
- if (outputType == ProcessOutputType .STDOUT ) {
119
136
try {
120
137
var serverMessage = objectMapper .readValue (event .getText (), LlamaServerMessage .class );
121
138
if ("HTTP server listening" .equals (serverMessage .getMessage ())) {
139
+ LOG .info ("Server up and running!" );
140
+
122
141
LlamaSettingsState .getInstance ().setServerPort (port );
123
142
onSuccess .run ();
124
143
}
@@ -139,21 +158,16 @@ private static GeneralCommandLine getMakeCommandLinde() {
139
158
return commandLine ;
140
159
}
141
160
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 ) {
148
162
GeneralCommandLine commandLine = new GeneralCommandLine ().withCharset (StandardCharsets .UTF_8 );
149
163
commandLine .setExePath ("./server" );
150
164
commandLine .withWorkDirectory (CodeGPTPlugin .getLlamaSourcePath ());
151
165
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 () );
157
171
commandLine .setRedirectErrorStream (false );
158
172
return commandLine ;
159
173
}
0 commit comments