koishi-plugin-cocoyyy-console 1.1.0-beta.5 → 1.1.0-beta.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -38,7 +38,7 @@ __export(src_exports, {
38
38
  savePath: () => savePath
39
39
  });
40
40
  module.exports = __toCommonJS(src_exports);
41
- var import_koishi12 = require("koishi");
41
+ var import_koishi13 = require("koishi");
42
42
 
43
43
  // src/utils/configs/function_config.ts
44
44
  var import_koishi = require("koishi");
@@ -2075,56 +2075,100 @@ function registerKuroCommands(ctx, connected) {
2075
2075
  }
2076
2076
  __name(registerKuroCommands, "registerKuroCommands");
2077
2077
 
2078
+ // src/services/game_func/game_command.ts
2079
+ var import_koishi12 = require("koishi");
2080
+
2078
2081
  // src/services/game_func/game_service.ts
2079
2082
  var is_gaming = false;
2080
- var is_thinking = false;
2083
+ var chatPrompt = [];
2081
2084
  var chatHistory2 = [];
2085
+ var gameTimeout = null;
2086
+ var GAME_TIMEOUT_MINUTES = local_config2?.game?.timeout_minutes || 10;
2082
2087
  async function startSituationPuzzle(config) {
2083
- if (is_gaming)
2084
- return "海龟汤正在进行中,请先结束游戏";
2088
+ chatPrompt = [];
2085
2089
  chatHistory2 = [];
2086
2090
  const prompt_start = local_config2?.game?.situation_puzzle;
2087
- chatHistory2.push({ role: "system", content: [{ type: "text", text: prompt_start }] });
2088
- const { err, resp } = await callOpenRouter(config.api_url, config.api_key, config.api_model, chatHistory2);
2091
+ chatPrompt.push({ role: "system", content: [{ type: "text", text: prompt_start }] });
2092
+ const { err, resp } = await callOpenRouter(config.api_url, config.api_key, config.api_model, chatPrompt);
2089
2093
  if (err != null) {
2090
2094
  logger.error("[startSituationPuzzle Error]: " + err);
2091
- chatHistory2.pop();
2095
+ chatPrompt.pop();
2092
2096
  return null;
2093
2097
  }
2094
- chatHistory2.push({ role: "assistant", content: [{ type: "text", text: resp }] });
2098
+ chatPrompt.push({ role: "assistant", content: [{ type: "text", text: resp }] });
2095
2099
  is_gaming = true;
2100
+ clearGameTimeout();
2101
+ gameTimeout = setTimeout(async () => {
2102
+ logger.info(`[SituationPuzzle Timeout]: 游戏已超时(${GAME_TIMEOUT_MINUTES}分钟),自动结束游戏`);
2103
+ is_gaming = false;
2104
+ chatPrompt = [];
2105
+ chatHistory2 = [];
2106
+ gameTimeout = null;
2107
+ }, GAME_TIMEOUT_MINUTES * 60 * 1e3);
2108
+ logger.info("[startSituationPuzzle Info]: ai api send message success");
2096
2109
  return resp;
2097
2110
  }
2098
2111
  __name(startSituationPuzzle, "startSituationPuzzle");
2112
+ function clearGameTimeout() {
2113
+ if (gameTimeout) {
2114
+ clearTimeout(gameTimeout);
2115
+ gameTimeout = null;
2116
+ }
2117
+ }
2118
+ __name(clearGameTimeout, "clearGameTimeout");
2099
2119
  async function questSituationPuzzle(content, config) {
2120
+ chatHistory2 = [...chatPrompt];
2100
2121
  chatHistory2.push({ role: "user", content: [{ type: "text", text: content }] });
2101
- is_thinking = true;
2102
2122
  const { err, resp } = await callOpenRouter(config.api_url, config.api_key, config.api_model, chatHistory2);
2103
- is_thinking = false;
2104
2123
  if (err != null) {
2105
2124
  logger.error("[questSituationPuzzle Error]: " + err);
2106
- chatHistory2.pop();
2107
2125
  return null;
2108
2126
  }
2109
- chatHistory2.push({ role: "assistant", content: [{ type: "text", text: resp }] });
2127
+ logger.info("[questSituationPuzzle Info]: ai api send message success");
2128
+ if (resp.includes("猜中了")) {
2129
+ is_gaming = false;
2130
+ chatPrompt = [];
2131
+ chatHistory2 = [];
2132
+ gameTimeout = null;
2133
+ }
2110
2134
  return resp;
2111
2135
  }
2112
2136
  __name(questSituationPuzzle, "questSituationPuzzle");
2113
2137
  async function endSituationPuzzle(config) {
2114
2138
  is_gaming = false;
2139
+ chatHistory2 = [...chatPrompt];
2115
2140
  chatHistory2.push({ role: "user", content: [{ type: "text", text: "结束游戏,请给出答案" }] });
2116
2141
  const { err, resp } = await callOpenRouter(config.api_url, config.api_key, config.api_model, chatHistory2);
2117
2142
  if (err != null) {
2118
2143
  logger.error("[endSituationPuzzle Error]: " + err);
2119
- chatHistory2.pop();
2120
2144
  return null;
2121
2145
  }
2146
+ logger.info("[endSituationPuzzle Info]: ai api send message success");
2122
2147
  return resp;
2123
2148
  }
2124
2149
  __name(endSituationPuzzle, "endSituationPuzzle");
2150
+ async function rerollSituationPuzzle(config) {
2151
+ chatHistory2 = [...chatPrompt];
2152
+ chatHistory2.push({ role: "user", content: [{ type: "text", text: "重新生成新谜题" }] });
2153
+ const { err, resp } = await callOpenRouter(config.api_url, config.api_key, config.api_model, chatHistory2);
2154
+ if (err != null) {
2155
+ logger.error("[rerollSituationPuzzle Error]: " + err);
2156
+ chatPrompt.pop();
2157
+ return null;
2158
+ }
2159
+ chatPrompt = [];
2160
+ const prompt_start = local_config2?.game?.situation_puzzle;
2161
+ chatPrompt.push({ role: "system", content: [{ type: "text", text: prompt_start }] });
2162
+ chatPrompt.push({ role: "assistant", content: [{ type: "text", text: resp }] });
2163
+ is_gaming = true;
2164
+ logger.info("[rerollSituationPuzzle Info]: ai api send message success");
2165
+ return resp;
2166
+ }
2167
+ __name(rerollSituationPuzzle, "rerollSituationPuzzle");
2125
2168
 
2126
2169
  // src/services/game_func/game_command.ts
2127
2170
  var local_config2 = null;
2171
+ var is_thinking = false;
2128
2172
  var gameList = [
2129
2173
  {
2130
2174
  name: "AI海龟汤",
@@ -2132,6 +2176,12 @@ var gameList = [
2132
2176
  command: "puzzle"
2133
2177
  }
2134
2178
  ];
2179
+ var commandList = [
2180
+ "endgame",
2181
+ "gamelist",
2182
+ "startgame",
2183
+ "reroll"
2184
+ ];
2135
2185
  function registerGameCommands(ctx, game_config, ai_config) {
2136
2186
  if (game_config?.config_path) {
2137
2187
  local_config2 = loadYamlConfig(game_config.config_path);
@@ -2153,23 +2203,49 @@ function registerGameCommands(ctx, game_config, ai_config) {
2153
2203
  if (!dev_mode) {
2154
2204
  if (!is_at_bot(session)) return;
2155
2205
  }
2206
+ if (is_gaming) return "游戏正在进行中,请先结束游戏";
2207
+ if (is_thinking) return "正在思考,请耐心等待...";
2156
2208
  const game = args?.[0];
2157
2209
  if (!game) return "请提供正确格式,如:@bot startgame [游戏]";
2158
- switch (game) {
2159
- case gameList[0].command:
2160
- await session.send("正在开始游戏...后续问答请参照格式:@bot [问题]");
2161
- const resp = await startSituationPuzzle(ai_config);
2162
- return checkResp(resp);
2163
- default:
2164
- return `游戏不存在,请输入gamelist查看所有游戏`;
2210
+ try {
2211
+ is_thinking = true;
2212
+ switch (game) {
2213
+ case gameList[0].command:
2214
+ await session.send("正在开始游戏...后续问答请参照格式:@bot [问题]");
2215
+ const resp = await startSituationPuzzle(ai_config);
2216
+ return checkResp(resp);
2217
+ default:
2218
+ return `游戏不存在,请输入gamelist查看所有游戏`;
2219
+ }
2220
+ } finally {
2221
+ is_thinking = false;
2222
+ }
2223
+ });
2224
+ ctx.command("endgame", "结束游戏").action(async ({ session }) => {
2225
+ if (!dev_mode) {
2226
+ if (!is_at_bot(session)) return;
2227
+ }
2228
+ if (is_thinking) return "正在思考,请耐心等待...";
2229
+ try {
2230
+ is_thinking = true;
2231
+ const resp = await endSituationPuzzle(ai_config);
2232
+ return checkResp(resp);
2233
+ } finally {
2234
+ is_thinking = false;
2165
2235
  }
2166
2236
  });
2167
- ctx.command("endgame", "结束游戏").action(async ({ session }, ...args) => {
2237
+ ctx.command("reroll", "重新生成新谜题").action(async ({ session }) => {
2168
2238
  if (!dev_mode) {
2169
2239
  if (!is_at_bot(session)) return;
2170
2240
  }
2171
- const resp = await endSituationPuzzle(ai_config);
2172
- return checkResp(resp);
2241
+ if (is_thinking) return "正在思考,请耐心等待...";
2242
+ try {
2243
+ is_thinking = true;
2244
+ const resp = await rerollSituationPuzzle(ai_config);
2245
+ return checkResp(resp);
2246
+ } finally {
2247
+ is_thinking = false;
2248
+ }
2173
2249
  });
2174
2250
  }
2175
2251
  __name(registerGameCommands, "registerGameCommands");
@@ -2182,8 +2258,11 @@ function registerGameMiddleware(ctx, ai_config) {
2182
2258
  if (is_thinking) return "正在思考,请耐心等待...";
2183
2259
  const withoutTags = content.replace(/<[^>]+>/g, " ");
2184
2260
  const text = withoutTags.replace(/\s+/g, " ").trim();
2261
+ commandList.forEach((element) => {
2262
+ if (text.includes(element)) return next();
2263
+ });
2185
2264
  const resp = await questSituationPuzzle(text, ai_config);
2186
- return checkResp(resp);
2265
+ return (0, import_koishi12.h)("at", { id: userId }) + " " + checkResp(resp);
2187
2266
  });
2188
2267
  }
2189
2268
  __name(registerGameMiddleware, "registerGameMiddleware");
@@ -2198,7 +2277,7 @@ __name(checkResp, "checkResp");
2198
2277
  // src/index.ts
2199
2278
  var name = "cocoyyy-console";
2200
2279
  var dev_mode;
2201
- var Config = import_koishi12.Schema.object({
2280
+ var Config = import_koishi13.Schema.object({
2202
2281
  function_config: FunctionConfigSchema.description("功能开关配置"),
2203
2282
  ai_config: AiAPIConfigSchema.description("AI API配置"),
2204
2283
  mysql_config: MysqlConfigSchema.description("MySQL 数据库配置"),
@@ -2206,9 +2285,9 @@ var Config = import_koishi12.Schema.object({
2206
2285
  repeat_config: RepeatConfigSchema.description("复读配置"),
2207
2286
  son_config: ShitOrNotConfigSchema.description("SON配置"),
2208
2287
  game_config: GameConfigSchema.description("游戏配置"),
2209
- test: import_koishi12.Schema.string().description("测试")
2288
+ test: import_koishi13.Schema.string().description("测试")
2210
2289
  });
2211
- var logger = new import_koishi12.Logger(name);
2290
+ var logger = new import_koishi13.Logger(name);
2212
2291
  var savePath = null;
2213
2292
  async function apply(ctx, config) {
2214
2293
  ctx = ctx.guild();
@@ -1,7 +1,7 @@
1
1
  import { AiAPIConfig } from "../../utils/config";
2
2
  declare let is_gaming: boolean;
3
- declare let is_thinking: boolean;
4
3
  declare function startSituationPuzzle(config: AiAPIConfig): Promise<string>;
5
4
  declare function questSituationPuzzle(content: string, config: AiAPIConfig): Promise<string>;
6
5
  declare function endSituationPuzzle(config: AiAPIConfig): Promise<string>;
7
- export { is_gaming, is_thinking, startSituationPuzzle, questSituationPuzzle, endSituationPuzzle };
6
+ declare function rerollSituationPuzzle(config: AiAPIConfig): Promise<string>;
7
+ export { is_gaming, startSituationPuzzle, questSituationPuzzle, endSituationPuzzle, rerollSituationPuzzle };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-cocoyyy-console",
3
3
  "description": "自用koishi插件,功能包含复读,记录黑历史,*人等",
4
- "version": "1.1.0-beta.5",
4
+ "version": "1.1.0-beta.7",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "contributors": [