Skip to content

Commit 741b147

Browse files
committed
Adding Clipboard tools (generated by Gemini)
1 parent 6292d01 commit 741b147

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/main/java/org/fxapps/llmfx/tools/ToolsInfo.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.fxapps.llmfx.tools.graphics.JFXReportingTool;
1616
import org.fxapps.llmfx.tools.graphics.JFXShapesTool;
1717
import org.fxapps.llmfx.tools.graphics.JFXWebRenderingTool;
18+
import org.fxapps.llmfx.tools.system.ClipboardTool;
1819
import org.fxapps.llmfx.tools.time.DateTimeTool;
1920
import org.fxapps.llmfx.tools.web.WebOpenTool;
2021
import org.fxapps.llmfx.tools.web.WebSearchTool;
@@ -40,6 +41,7 @@ public class ToolsInfo {
4041
public static final String _3D = "3D";
4142
public static final String SHAPES = "Shapes";
4243
public static final String DRAWER = "Drawer";
44+
public static final String CLIPBOARD = "Clipboard";
4345

4446
@Inject
4547
private FilesReaderTool filesReaderTool;
@@ -69,6 +71,8 @@ public class ToolsInfo {
6971
private JFXShapesTool shapesTool;
7072
@Inject
7173
private JFXDrawerTool drawerTool;
74+
@Inject
75+
private ClipboardTool clipboardTool;
7276

7377
Map<String, Object> toolsMap;
7478

@@ -77,6 +81,7 @@ public class ToolsInfo {
7781
"Web", List.of(WEB_SEARCH, WEB_OPEN),
7882
"Date and Time", List.of(DATE_TIME),
7983
"Execute", List.of(COMMANDS, PYTHON),
84+
"System", List.of(CLIPBOARD),
8085
"Graphics", List.of(DRAWER, REPORTING, WEB_RENDER, _3D, SHAPES));
8186

8287
@PostConstruct
@@ -100,7 +105,8 @@ void init() {
100105
FILE_WRITE, filesWriterTool,
101106
DATE_TIME, dateTimeTool,
102107
COMMANDS, commandsTool,
103-
PYTHON, pythonTool));
108+
PYTHON, pythonTool,
109+
CLIPBOARD, clipboardTool));
104110

105111
}
106112

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package org.fxapps.llmfx.tools.system;
2+
3+
import java.util.concurrent.CompletableFuture;
4+
import java.util.concurrent.ExecutionException;
5+
import java.util.concurrent.TimeUnit;
6+
import java.util.concurrent.TimeoutException;
7+
8+
import dev.langchain4j.agent.tool.P;
9+
import dev.langchain4j.agent.tool.Tool;
10+
import jakarta.inject.Singleton;
11+
import javafx.application.Platform;
12+
import javafx.scene.input.Clipboard;
13+
import javafx.scene.input.ClipboardContent;
14+
15+
@Singleton
16+
public class ClipboardTool {
17+
18+
@Tool("Copies the given text to the system clipboard")
19+
public void copyToClipboard(@P("The text to be copied") String text) {
20+
Platform.runLater(() -> {
21+
var content = new ClipboardContent();
22+
content.putString(text);
23+
Clipboard.getSystemClipboard().setContent(content);
24+
});
25+
}
26+
27+
@Tool("Reads the text content from the system clipboard")
28+
public String readFromClipboard() {
29+
var future = new CompletableFuture<String>();
30+
Platform.runLater(() -> {
31+
var clipboard = Clipboard.getSystemClipboard();
32+
if (clipboard.hasString()) {
33+
future.complete(clipboard.getString());
34+
} else {
35+
future.complete("Clipboard is empty or does not contain text.");
36+
}
37+
});
38+
try {
39+
return future.get(5, TimeUnit.SECONDS);
40+
} catch (InterruptedException | ExecutionException | TimeoutException e) {
41+
return "Error reading clipboard: " + e.getMessage();
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)