var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { Config: () => Config, apply: () => apply2, inject: () => inject }); module.exports = __toCommonJS(src_exports); var import_koishi = require("koishi"); // src/commands.ts var import_request = require("koishi-plugin-chatluna/utils/request"); var import_promises = __toESM(require("fs/promises"), 1); var import_logger = require("koishi-plugin-chatluna/utils/logger"); // src/utils.ts function mergeMarketPresets(markets) { const mergedMarkets = []; const presetMap = /* @__PURE__ */ new Map(); for (const market of markets) { for (const preset of market) { const keywordString = preset.keywords.join(","); if (!presetMap.has(keywordString)) { presetMap.set(keywordString, preset); mergedMarkets.push(preset); } } } return mergedMarkets; } __name(mergeMarketPresets, "mergeMarketPresets"); // src/commands.ts var logger; function apply(ctx, config) { logger = (0, import_logger.createLogger)(ctx, "chatluna-preset-market"); let marketPresets; ctx.command("chatluna.preset-market", "chathub 预设仓库相关命令"); ctx.command("chatluna.preset-market.list", "列出预设仓库的预设").alias("预设仓库列表").option("page", "-p 选择页数", { authority: 1 }).action(async ({ options, session }) => { const presets = await getPresetLists(config); marketPresets = presets; const page = options.page ?? 1; const pageSize = 10; const start = (page - 1) * pageSize; const end = start + pageSize; const presetList = presets.slice(start, end); const buffer = ["预设列表:\n"]; for (const preset of presetList) { buffer.push(`名称:${preset.name}`); buffer.push(`关键词: ${preset.keywords.join(" ,")}`); buffer.push(""); } buffer.push( `当前页数:(${page}/${Math.ceil(presets.length / pageSize)})` ); await session.send(buffer.join("\n")); }); ctx.command("chatluna.preset-market.search ", "搜索预设").alias("搜索预设").option("page", "-p 选择页数", { authority: 1 }).action(async ({ options, session }, keyword) => { const presets = await getPresetLists(config); marketPresets = presets; const presetList = presets.filter( (preset) => preset.keywords.some( (_keyword) => _keyword.indexOf(keyword) !== -1 ) ); const page = options.page ?? 1; const pageSize = 10; const start = (page - 1) * pageSize; const end = start + pageSize; if (presetList.length === 0) { await session.send(`没有找到预设 ${keyword}`); return; } const buffer = [`以下是关于预设 ${keyword} 的结果: `]; for (const preset of presetList) { buffer.push(`名称:${preset.name}`); buffer.push(`关键词: ${preset.keywords.join(" ,")}`); buffer.push(""); } buffer.push( `当前页数:(${page}/${Math.ceil(presetList.length / pageSize)})` ); await session.send(buffer.join("\n")); }); ctx.command("chatluna.preset-market.refresh", "刷新预设仓库").alias("刷新预设仓库").action(async ({ options, session }) => { const presets = await getPresetLists(config); marketPresets = presets; return `刷新预设仓库成功,快使用 chatluna.preset.list 查看吧`; }); ctx.command("chatluna.preset-market.download-all", "下载所有预设").alias("下载所有预设").action(async ({ options, session }) => { await session.send( `以下操作将会覆盖你本地的所有预设,请先备份你本地的预设,是否继续?输入 Y 确认,否则取消。` ); const prompt = await session.prompt(1e3 * 30); if (!prompt || prompt !== "Y") { await session.send(`已取消下载所有预设`); return; } marketPresets = await getPresetLists(config); await session.send( `开始下载所有预设,总计 ${marketPresets.length} 个预设。可在控制台查看下载进度。` ); const result = await downloadAllPreset( ctx, marketPresets, ctx.chatluna.preset.resolvePresetDir() ); await session.send(result); }); ctx.command( "chatluna.preset-market.download ", "下载预设" ).alias("下载预设").action(async ({ options, session }, presetName) => { const presets = marketPresets ?? await getPresetLists(config); const localPresetRepository = ctx.chatluna.preset; marketPresets = presets; const preset = presets.find( (preset2) => preset2.name === presetName || preset2.keywords.includes(presetName) ); if (!preset) { await session.send(`没有找到预设 ${presetName}`); return; } let localPreset; for (const presetKeyWord of preset.keywords) { localPreset = await localPresetRepository.getPreset( presetKeyWord, false, false ); } if (localPreset) { await session.send( `已经存在使用了同一关键词的预设 ${presetName},回复大写 Y 则覆盖,否则取消。是否覆盖?` ); const input = await session.prompt(1e3 * 30); if (!input) { await session.send(`超时,已取消下载预设 ${presetName}`); return; } else if (input !== "Y") { await session.send(`已取消下载预设 ${presetName}`); return; } } const downloadPath = localPreset ? localPreset.path : localPresetRepository.resolvePresetDir() + `/${presetName}.yml`; await downloadPreset(preset, downloadPath); return `下载预设 ${presetName} 成功,快使用 chatluna.preset.list 查看吧`; }); ctx.command("chatluna.preset-market.upload", "上传预设").alias("上传预设").action(async ({ options, session }) => { return "非常抱歉,由于我们使用 GitHub 作为预设仓库,请有需要上传预设的用户前往此仓库提交 Pull Request: https://github.com/ChatHubLab/awesome-chathub-presets"; }); } __name(apply, "apply"); async function getPresetList(repositoryEndpoint) { try { const response = await (0, import_request.chatLunaFetch)( `${repositoryEndpoint}/preset/presets.json` ); const rawText = await response.text(); const presetList = JSON.parse(rawText); for (const preset of presetList) { preset.repositoryEndpoint = repositoryEndpoint; } import_promises.default.writeFile("./data/chathub/temp/preset_market.json", rawText); return presetList; } catch (error) { logger.error(error); if (error.cause) { logger.error(error.cause); } const rawText = (await import_promises.default.readFile("./data/chathub/temp/preset_market.json")).toString("utf-8"); const presetList = JSON.parse(rawText); return presetList; } } __name(getPresetList, "getPresetList"); async function getPresetLists(config) { const presetList = []; for (const repositoryEndpoint of config.repositoryEndpoints) { const preset = await getPresetList(repositoryEndpoint); presetList.push(preset); } return mergeMarketPresets(presetList); } __name(getPresetLists, "getPresetLists"); async function downloadPreset(preset, downloadPath) { const { relativePath, repositoryEndpoint } = preset; const url = repositoryEndpoint.concat("/", relativePath); const response = await (0, import_request.chatLunaFetch)(url); const rawText = await response.text(); await import_promises.default.writeFile(downloadPath, rawText); } __name(downloadPreset, "downloadPreset"); async function downloadAllPreset(ctx, presets, presetDir) { const total = presets.length; let success = 0; let failed = 0; for (const preset of presets) { const presetName = preset.name; const downloadPath = ctx.chatluna.preset.resolvePresetDir() + `/${presetName}.yml`; let isSuccess = false; try { await downloadPreset(preset, downloadPath); success++; isSuccess = true; } catch (error) { ctx.logger.error(error); if (error.cause) { ctx.logger.error(error.cause); } failed++; } ctx.logger.success( `下载预设 ${presetName} ${isSuccess ? "成功" : "失败"}, 成功: ${success}, 失败: ${failed}, 总数: ${total}` ); } return `下载预设完成,成功: ${success}, 失败: ${failed}, 总数: ${total}`; } __name(downloadAllPreset, "downloadAllPreset"); // src/index.ts function apply2(ctx, config) { ctx.on("ready", async () => { apply( ctx, config ); }); } __name(apply2, "apply"); var Config = import_koishi.Schema.intersect([ import_koishi.Schema.object({ repositoryEndpoints: import_koishi.Schema.array( import_koishi.Schema.string() ).description("预设市场的接入地址(可以輸入多个仓库,前面仓库含有的预设将会被后面的覆盖,理解为先后覆盖)").default(["https://mirror.ghproxy.com/https://raw.githubusercontent.com/ChatLunaLab/awesome-chatluna-presets"]) }).description("请求配置") ]); var inject = ["chatluna"]; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Config, apply, inject });