Skip to content

Commit cf0c3a4

Browse files
committed
Color management
1 parent 02b637d commit cf0c3a4

1 file changed

Lines changed: 69 additions & 38 deletions

File tree

keyext.llm/src/main/java/org/key_project/key/llm/LlmPrompt.java

Lines changed: 69 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@
1313

1414
import de.uka.ilkd.key.gui.MainWindow;
1515
import de.uka.ilkd.key.gui.actions.KeyAction;
16+
import de.uka.ilkd.key.gui.colors.ColorSettings;
1617
import de.uka.ilkd.key.gui.extension.api.TabPanel;
1718

1819
import com.google.gson.GsonBuilder;
20+
import net.miginfocom.layout.CC;
21+
import net.miginfocom.layout.LC;
22+
import net.miginfocom.swing.MigLayout;
1923
import org.jspecify.annotations.NonNull;
2024
import org.slf4j.Logger;
2125
import org.slf4j.LoggerFactory;
@@ -27,18 +31,33 @@
2731
*/
2832
public class LlmPrompt extends JPanel implements TabPanel {
2933
private static final Logger LOGGER = LoggerFactory.getLogger(LlmPrompt.class);
34+
public static final ColorSettings.ColorProperty COLOR_BG_INPUT = ColorSettings.define(
35+
"llm.output.bg.input",
36+
"Background color in chat of LLM answers", new Color(130, 180, 220, 255));
37+
38+
public static final ColorSettings.ColorProperty COLOR_BG_ERROR =
39+
ColorSettings.define("llm.output.bg.error", "Background color in chat of LLM answers",
40+
new Color(255, 180, 180, 255));
41+
42+
private static final ColorSettings.ColorProperty COLOR_BG_ANSWER = ColorSettings.define(
43+
"llm.output.bg.answer", "Background color in chat of LLM answers", Color.LIGHT_GRAY);
44+
3045
private final JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
3146

3247
private final JEditorPane txtInput = new JEditorPane();
3348

34-
private final Box pOutput = new Box(BoxLayout.Y_AXIS);
49+
private final JPanel pOutput =
50+
new JPanel(new MigLayout(new LC().fillX().debug().topToBottom().wrapAfter(1)));
3551

3652
private final KeyAction actionSwitchOrientation = new SwitchOrientationAction();
53+
private final SendPromptAction actionSendPrompt = new SendPromptAction();
3754

3855
public LlmPrompt() {
3956
setLayout(new BorderLayout());
4057
add(splitPane, BorderLayout.CENTER);
41-
splitPane.add(new JScrollPane(pOutput));
58+
final var comp = new JScrollPane(pOutput);
59+
comp.getVerticalScrollBar().setUnitIncrement(16);
60+
splitPane.add(comp);
4261
splitPane.add(new JScrollPane(txtInput));
4362

4463
handle(new Exception("Test Exception"));
@@ -53,42 +72,17 @@ public LlmPrompt() {
5372
public void keyTyped(KeyEvent e) {
5473
if (e.getKeyChar() == KeyEvent.VK_ENTER
5574
&& (e.getModifiersEx() & InputEvent.CTRL_DOWN_MASK) > 0) {
56-
var proof = MainWindow.getInstance().getMediator().getSelectedProof();
57-
var node = MainWindow.getInstance().getMediator().getSelectedNode();
58-
59-
LlmSession session = LlmUtils.getSession(proof);
60-
var txt = txtInput.getText();
61-
LlmClient client = new LlmClient(session, new LlmContext(), txt);
62-
addInput(txt);
63-
txtInput.setText("");
64-
65-
var sw = new SwingWorker<Map<String, Object>, Void>() {
66-
@Override
67-
protected Map<String, Object> doInBackground() throws Exception {
68-
return client.call();
69-
}
70-
71-
@Override
72-
protected void done() {
73-
try {
74-
handle(resultNow());
75-
} catch (IllegalStateException ex) {
76-
LOGGER.error("Exceptional case", exceptionNow());
77-
handle(exceptionNow());
78-
}
79-
}
80-
};
81-
ForkJoinPool.commonPool().submit(sw);
75+
actionSendPrompt.run();
8276
}
8377
}
8478
});
8579
}
8680

8781
public static class OutputBox<T> extends JPanel {
88-
private final T userData;
89-
private final JEditorPane output = new JEditorPane();
90-
private final JPanel buttons = new JPanel();
91-
private final JPopupMenu menu = new JPopupMenu();
82+
protected final T userData;
83+
protected final JEditorPane output = new JEditorPane();
84+
protected final JPanel buttons = new JPanel();
85+
protected final JPopupMenu menu = new JPopupMenu();
9286

9387
public OutputBox(T userData) {
9488
this(userData, userData.toString());
@@ -127,13 +121,16 @@ public void setBackground(Color bg) {
127121

128122
private OutputBox<String> addInput(String text) {
129123
var o = addBox(text, new RepromptAction(text));
130-
o.setBackground(new Color(130, 180, 220, 255));
124+
o.setBackground(COLOR_BG_INPUT.get());
131125
return o;
132126
}
133127

134-
private <T> OutputBox<T> addBox(T data, Action... action) {
128+
private <T> OutputBox<T> addBox(T data, Action... actions) {
135129
OutputBox<T> box = new OutputBox<>(data);
136-
pOutput.add(box);
130+
for (Action it : actions) {
131+
box.menu.add(it);
132+
}
133+
pOutput.add(box, new CC().growX());
137134
return box;
138135
}
139136

@@ -142,13 +139,14 @@ private void handle(Map<String, Object> jsonResponse) {
142139
var o = new OutputBox<>(jsonResponse,
143140
((Map<String, Object>) ((Map<String, Object>) ((List<?>) jsonResponse.get("choices"))
144141
.get(0)).get("message")).get("content").toString());
145-
pOutput.add(o);
142+
pOutput.add(o, new CC().growX());
143+
o.setBackground(COLOR_BG_ANSWER.get());
146144
}
147145

148146
private void handle(Throwable e) {
149147
LOGGER.error("Error during LLM prompt", e);
150148
var box = addBox(e);
151-
box.setBackground(new Color(255, 180, 180, 255));
149+
box.setBackground(COLOR_BG_ERROR.get());
152150
}
153151

154152
@Override
@@ -189,9 +187,42 @@ public void actionPerformed(ActionEvent e) {
189187
}
190188

191189
class SendPromptAction extends KeyAction {
190+
public SendPromptAction() {
191+
setName("Send Prompt");
192+
}
193+
192194
@Override
193195
public void actionPerformed(ActionEvent e) {
194-
String prompt = txtInput.getText();
196+
run();
197+
}
198+
199+
public void run() {
200+
var proof = MainWindow.getInstance().getMediator().getSelectedProof();
201+
var node = MainWindow.getInstance().getMediator().getSelectedNode();
202+
203+
LlmSession session = LlmUtils.getSession(proof);
204+
var txt = txtInput.getText();
205+
LlmClient client = new LlmClient(session, new LlmContext(), txt);
206+
addInput(txt);
207+
txtInput.setText("");
208+
209+
var sw = new SwingWorker<Map<String, Object>, Void>() {
210+
@Override
211+
protected Map<String, Object> doInBackground() throws Exception {
212+
return client.call();
213+
}
214+
215+
@Override
216+
protected void done() {
217+
try {
218+
handle(resultNow());
219+
} catch (IllegalStateException ex) {
220+
LOGGER.error("Exceptional case", exceptionNow());
221+
handle(exceptionNow());
222+
}
223+
}
224+
};
225+
ForkJoinPool.commonPool().submit(sw);
195226
}
196227
}
197228

0 commit comments

Comments
 (0)