package plugin.config; import cn.hutool.core.bean.BeanUtil; import lombok.Getter; import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import plugin.chat.constant.Constant; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; @Setter @Getter @Slf4j public class Config { /** * owner QQ */ private String owner; /** * bot QQ */ private String bot; /** * 对话记录过期时间 */ private String timeout; /** * 检测对话记录间隔 */ private String timeCheck; /** * 群聊黑名单 */ private List blacklist; /** * 阿里OCR配置 */ private AliOCRConfig ocrConfig; /** * 模板配置 */ private List modelConfigTemplates; /** * 预设配置 */ private ArrayList customCommandTemplates; private Config() { } public static class ConfigLoader { private static final Yaml yaml; @Getter private static Config config; static { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); yaml = new Yaml(options); } public static Config load() throws IOException { Config config; DumperOptions dumperOptions = new DumperOptions(); dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(dumperOptions); File file = new File(Constant.Path.CONFIG_PATH); if (file.exists()) { FileInputStream fileInputStream = new FileInputStream(Constant.Path.CONFIG_PATH); config = BeanUtil.toBean(yaml.load(fileInputStream), Config.class); fileInputStream.close(); log.info("load config success"); } else { file.getParentFile().mkdirs(); file.createNewFile(); config = new Config(); //基础配置 config.setOwner("your_bot_owner_qq_number(e.g. Q1145141919810)"); config.setBot("your_bot_qq_number(e.g. Q1145141919810)"); config.setTimeout("M3600000"); config.setTimeCheck("M60000"); ArrayList blacklist = new ArrayList<>(); blacklist.add(123456789L); blacklist.add(987654321L); config.setBlacklist(blacklist); //OCR配置 config.setOcrConfig(new AliOCRConfig("ocr_accessKeyId", "ocr_accessKeySecret")); //模型模板 List templateList = new ArrayList<>(); templateList.add(new ModelConfigTemplate("zhipu", "https://open.bigmodel.cn/api/paas/v4", "zhipu_apikey")); templateList.add(new ModelConfigTemplate("deepseek", "https://api.deepseek.com/v1", "deepseek_apikey")); config.setModelConfigTemplates(templateList); //自定义预设 ArrayList customCommands = new ArrayList<>(); customCommands.add(new CustomCommandTemplate("default", 0, "glm-4-flash", "null")); customCommands.add(new CustomCommandTemplate("/c", 0, "glm-4-flash", "你是一位智能编程助手,你会为用户回答关于编程、代码、计算机方面的任何问题,并提供格式规范、可以执行、准确安全的代码,并在必要时提供详细的解释。 请用中文回答。")); config.setCustomCommandTemplates(customCommands); dump(); log.warn("配置文件创建成功,请关闭后进行配置"); System.exit(0); } ConfigLoader.config = config; return config; } private static void dump() throws IOException { FileWriter fileWriter = new FileWriter(Constant.Path.CONFIG_PATH); yaml.dump(BeanUtil.beanToMap(config), fileWriter); } /** * 关闭当前群聊发言 * * @param id 群聊id */ public static String shutUp(long id) { config.getBlacklist().add(id); return "ChatAI-InGroup 已关闭"; } /** * 开启当前群聊发言 * * @param id 群聊id */ public static String speak(long id) { config.getBlacklist().remove(id); return "ChatAI-InGroup 已开启"; } /** * 添加预设 * * @param command * @param modelTemplateIndex * @param customModel * @param customPromotion * @return * @throws IOException */ public static String addCustom(String command, int modelTemplateIndex, String customModel, String customPromotion) throws IOException { ArrayList customCommands = config.getCustomCommandTemplates(); synchronized (customCommands) { for (CustomCommandTemplate customCommand : customCommands) { if (customCommand.getCommand().equals(command)) { return "当前指令已存在!\r\n" + customCommand; } } CustomCommandTemplate customCommand = new CustomCommandTemplate(command, modelTemplateIndex, customModel, customPromotion); customCommands.add(customCommand); dump(); return "预设添加完毕!\r\n" + customCommand; } } /** * 删除预设 * * @param command * @return */ public static String removeCustom(String command) { if (command.equals(Constant.Order.DEFAULT)) { return "该预设不可删除!"; } ArrayList customCommands = config.getCustomCommandTemplates(); int index = -1; for (int i = 0; i < customCommands.size(); i++) { if (customCommands.get(i).getCommand().equals(command)) { index = i; } } if (index == -1) { return "该预设不存在!"; } return "预设删除完毕" + customCommands.remove(index); } } }