调用工具:终端操作取

This commit is contained in:
huangge1199 2025-05-28 15:52:25 +08:00
parent 642edae0be
commit 4d4b1fcf86
4 changed files with 56 additions and 4 deletions

View File

@ -20,4 +20,6 @@ public interface ToolsService {
List<String> webSearch(String question); List<String> webSearch(String question);
List<String> webScrap(String question); List<String> webScrap(String question);
String terminalTool(String command);
} }

View File

@ -2,10 +2,7 @@ package com.huangge1199.aiagent.Service.impl;
import com.huangge1199.aiagent.Service.ToolsService; import com.huangge1199.aiagent.Service.ToolsService;
import com.huangge1199.aiagent.config.MyLoggerAdvisor; import com.huangge1199.aiagent.config.MyLoggerAdvisor;
import com.huangge1199.aiagent.tools.FileTool; import com.huangge1199.aiagent.tools.*;
import com.huangge1199.aiagent.tools.WeatherTool;
import com.huangge1199.aiagent.tools.WebScrapTool;
import com.huangge1199.aiagent.tools.WebSearchTool;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient; import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.ollama.OllamaChatModel; import org.springframework.ai.ollama.OllamaChatModel;
@ -71,4 +68,10 @@ public class ToolsServiceImpl implements ToolsService {
WebScrapTool webScrapTool = new WebScrapTool(); WebScrapTool webScrapTool = new WebScrapTool();
return List.of(webScrapTool.scrapeWebPage(url).split(",")); return List.of(webScrapTool.scrapeWebPage(url).split(","));
} }
@Override
public String terminalTool(String command) {
TerminalTool terminalTool = new TerminalTool();
return terminalTool.executeTerminalCommand(command);
}
} }

View File

@ -78,4 +78,12 @@ public class ToolController {
List<String> result = toolsService.webScrap(question); List<String> result = toolsService.webScrap(question);
return R.ok(result); return R.ok(result);
} }
@PostMapping("/terminalTool")
@Operation(summary = "终端操作")
public R<String> terminalTool(@RequestBody String command) {
CheckUtils.checkEmpty(command, "命令");
String result = toolsService.terminalTool(command);
return R.ok(result);
}
} }

View File

@ -0,0 +1,39 @@
package com.huangge1199.aiagent.tools;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* TerminlTool
*
* @author huangge1199
* @since 2025/5/28 15:34:30
*/
public class TerminalTool {
@Tool(description = "Execute a command in the terminal")
public String executeTerminalCommand(@ToolParam(description = "Command to execute in the terminal") String command) {
StringBuilder output = new StringBuilder();
try {
Process process = Runtime.getRuntime().exec("cmd.exe /c " + command);
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
}
int exitCode = process.waitFor();
if (exitCode != 0) {
output.append("Command execution failed with exit code: ").append(exitCode);
}
} catch (IOException | InterruptedException e) {
output.append("Error executing command: ").append(e.getMessage());
}
return output.toString();
}
}