koishi-plugin-cocoyyy-console 1.0.15 → 1.0.16-beta.2
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.d.ts +1 -0
- package/lib/index.js +95 -38
- package/lib/services/test_command.d.ts +2 -1
- package/lib/utils/ai_axios.d.ts +17 -0
- package/package.json +1 -1
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -321,7 +321,7 @@ function getMenuList(command = null, functionConfig) {
|
|
|
321
321
|
return `[所有命令都需要@bot]
|
|
322
322
|
当前可用功能列表:
|
|
323
323
|
${menuList.map((item) => item.name + ": " + item.description).join("\n ")}
|
|
324
|
-
输入"help 功能名"查看特定功能的指令和使用示例。`;
|
|
324
|
+
输入"help [功能名]"查看特定功能的指令和使用示例。`;
|
|
325
325
|
}
|
|
326
326
|
}
|
|
327
327
|
__name(getMenuList, "getMenuList");
|
|
@@ -1351,38 +1351,95 @@ function registerRbqCommands(ctx, connected) {
|
|
|
1351
1351
|
}
|
|
1352
1352
|
__name(registerRbqCommands, "registerRbqCommands");
|
|
1353
1353
|
|
|
1354
|
-
// src/
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
try {
|
|
1358
|
-
} catch (err) {
|
|
1359
|
-
console.error(err);
|
|
1360
|
-
return `test error: ${err.message}`;
|
|
1361
|
-
}
|
|
1362
|
-
});
|
|
1363
|
-
}
|
|
1364
|
-
__name(registerTestCommands, "registerTestCommands");
|
|
1365
|
-
|
|
1366
|
-
// src/infra/redis_init.ts
|
|
1367
|
-
var import_ioredis = __toESM(require("ioredis"));
|
|
1368
|
-
var redis;
|
|
1369
|
-
function init_redis(config) {
|
|
1354
|
+
// src/utils/ai_axios.ts
|
|
1355
|
+
var import_axios = __toESM(require("axios"));
|
|
1356
|
+
async function callOpenRouter(apiKey, messages, model = "gpt-4o-mini") {
|
|
1370
1357
|
try {
|
|
1371
|
-
|
|
1372
|
-
|
|
1358
|
+
const { data } = await import_axios.default.post(
|
|
1359
|
+
"https://openrouter.ai/api/v1/chat/completions",
|
|
1360
|
+
{
|
|
1361
|
+
model,
|
|
1362
|
+
messages
|
|
1363
|
+
},
|
|
1364
|
+
{
|
|
1365
|
+
headers: {
|
|
1366
|
+
Authorization: `Bearer ${apiKey}`,
|
|
1367
|
+
"Content-Type": "application/json"
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
);
|
|
1371
|
+
const choices = data?.choices ?? [];
|
|
1372
|
+
const reply = choices[0]?.message?.content;
|
|
1373
|
+
let responseText;
|
|
1374
|
+
if (typeof reply === "string") {
|
|
1375
|
+
responseText = reply.trim();
|
|
1376
|
+
} else if (Array.isArray(reply)) {
|
|
1377
|
+
responseText = reply.map((part) => part?.text ?? "").join("").trim();
|
|
1378
|
+
if (!responseText) {
|
|
1379
|
+
responseText = "OpenRouter 返回空内容";
|
|
1380
|
+
}
|
|
1381
|
+
} else {
|
|
1382
|
+
responseText = "OpenRouter 返回了未知格式,请检查日志";
|
|
1373
1383
|
}
|
|
1374
|
-
|
|
1375
|
-
logger.info("redis连接成功");
|
|
1376
|
-
return true;
|
|
1384
|
+
return { err: null, resp: responseText };
|
|
1377
1385
|
} catch (error) {
|
|
1378
|
-
|
|
1379
|
-
|
|
1386
|
+
const err = error;
|
|
1387
|
+
const status = err.response?.status;
|
|
1388
|
+
const detail = err.response?.data?.error || err.response?.data?.message || err.message;
|
|
1389
|
+
logger.error(error, "OpenRouter 请求失败");
|
|
1390
|
+
const errorMessage = `test error${status ? `(${status})` : ""}: ${detail}`;
|
|
1391
|
+
return { err: errorMessage, resp: null };
|
|
1380
1392
|
}
|
|
1381
1393
|
}
|
|
1382
|
-
__name(
|
|
1394
|
+
__name(callOpenRouter, "callOpenRouter");
|
|
1395
|
+
|
|
1396
|
+
// src/services/test_command.ts
|
|
1397
|
+
var CONSTANTS = {
|
|
1398
|
+
ESU: "前提条件"
|
|
1399
|
+
};
|
|
1400
|
+
var chatHistory = [];
|
|
1401
|
+
function registerTestCommands(ctx, config) {
|
|
1402
|
+
ctx.command("test1 <prompt:text>", "调用 OpenRouter 模型进行测试").action(async ({ session }, prompt) => {
|
|
1403
|
+
const apiKey = config?.test || process.env.OPENROUTER_API_KEY;
|
|
1404
|
+
if (!apiKey) return "未配置 OpenRouter API Key(config.test 或环境变量 OPENROUTER_API_KEY)";
|
|
1405
|
+
const content = prompt?.trim();
|
|
1406
|
+
if (!content) return "请输入要测试的内容";
|
|
1407
|
+
chatHistory.push({ role: "user", content });
|
|
1408
|
+
const messages = [
|
|
1409
|
+
{ role: "system", content: CONSTANTS.ESU },
|
|
1410
|
+
...chatHistory
|
|
1411
|
+
];
|
|
1412
|
+
const { err, resp } = await callOpenRouter(apiKey, messages);
|
|
1413
|
+
if (err) {
|
|
1414
|
+
return err;
|
|
1415
|
+
}
|
|
1416
|
+
if (!resp) {
|
|
1417
|
+
return "OpenRouter 返回空响应";
|
|
1418
|
+
}
|
|
1419
|
+
chatHistory.push({ role: "assistant", content: resp });
|
|
1420
|
+
return resp;
|
|
1421
|
+
});
|
|
1422
|
+
ctx.command("test <text>", "调用 OpenRouter 模型进行测试").action(async ({ session }, text) => {
|
|
1423
|
+
return text;
|
|
1424
|
+
});
|
|
1425
|
+
ctx.middleware((session, next) => {
|
|
1426
|
+
const { content, uid, userId } = session;
|
|
1427
|
+
const cid = session.cid;
|
|
1428
|
+
if (session.guildId != "829263238") return next();
|
|
1429
|
+
if (ctx.bots[uid]) return next();
|
|
1430
|
+
if (session.quote) {
|
|
1431
|
+
const quoteContent = session.quote.content;
|
|
1432
|
+
const quoteUserId = session.quote.user.id;
|
|
1433
|
+
logger.error(`quoteContent: ${quoteContent}, quoteUserId: ${quoteUserId},content: ${content} userId: ${userId}`);
|
|
1434
|
+
} else {
|
|
1435
|
+
logger.error(`content: ${content} userId: ${userId}`);
|
|
1436
|
+
}
|
|
1437
|
+
});
|
|
1438
|
+
}
|
|
1439
|
+
__name(registerTestCommands, "registerTestCommands");
|
|
1383
1440
|
|
|
1384
1441
|
// src/services/kuro_func/kuro_service.ts
|
|
1385
|
-
var
|
|
1442
|
+
var import_axios2 = __toESM(require("axios"));
|
|
1386
1443
|
var import_qs = __toESM(require("qs"));
|
|
1387
1444
|
|
|
1388
1445
|
// src/models/kuro_user.ts
|
|
@@ -1491,7 +1548,7 @@ __name(createUpdateUser, "createUpdateUser");
|
|
|
1491
1548
|
|
|
1492
1549
|
// src/services/kuro_func/kuro_service.ts
|
|
1493
1550
|
var userTokenMap = /* @__PURE__ */ new Map();
|
|
1494
|
-
var
|
|
1551
|
+
var CONSTANTS2 = {
|
|
1495
1552
|
BASE_URL: "https://api.kurobbs.com",
|
|
1496
1553
|
LOGIN_URL: "/user/sdkLogin",
|
|
1497
1554
|
GAME_DATA_URL: "/gamer/role/list",
|
|
@@ -1515,11 +1572,11 @@ var CONSTANTS = {
|
|
|
1515
1572
|
"source": "ios"
|
|
1516
1573
|
}
|
|
1517
1574
|
};
|
|
1518
|
-
var wavesApi =
|
|
1575
|
+
var wavesApi = import_axios2.default.create();
|
|
1519
1576
|
wavesApi.interceptors.request.use(
|
|
1520
1577
|
async (config) => {
|
|
1521
1578
|
if (config.url.startsWith("/")) {
|
|
1522
|
-
config.url =
|
|
1579
|
+
config.url = CONSTANTS2.BASE_URL + config.url;
|
|
1523
1580
|
}
|
|
1524
1581
|
return config;
|
|
1525
1582
|
},
|
|
@@ -1573,7 +1630,7 @@ async function getToken(session, mobile, code) {
|
|
|
1573
1630
|
code
|
|
1574
1631
|
});
|
|
1575
1632
|
try {
|
|
1576
|
-
const user_response = await wavesApi.post(
|
|
1633
|
+
const user_response = await wavesApi.post(CONSTANTS2.LOGIN_URL, data, { headers: { ...CONSTANTS2.REQUEST_HEADERS_BASE, devCode } });
|
|
1577
1634
|
if (user_response.data.code != 200) {
|
|
1578
1635
|
logger.error(`验证码登录失败: ${user_response.data.msg}`);
|
|
1579
1636
|
return { status: false, msg: user_response.data.msg };
|
|
@@ -1613,7 +1670,7 @@ async function getGameData(token) {
|
|
|
1613
1670
|
let data = import_qs.default.stringify({
|
|
1614
1671
|
"gameId": 3
|
|
1615
1672
|
});
|
|
1616
|
-
const response = await wavesApi.post(
|
|
1673
|
+
const response = await wavesApi.post(CONSTANTS2.GAME_DATA_URL, data, { headers: { ...CONSTANTS2.REQUEST_HEADERS_BASE, "token": token } });
|
|
1617
1674
|
if (response.data.code === 200) {
|
|
1618
1675
|
return { status: true, data: response.data.data[0] };
|
|
1619
1676
|
} else {
|
|
@@ -1632,7 +1689,7 @@ async function isAvailable(serverId, roleId, token) {
|
|
|
1632
1689
|
"roleId": roleId
|
|
1633
1690
|
});
|
|
1634
1691
|
try {
|
|
1635
|
-
const response = await wavesApi.post(
|
|
1692
|
+
const response = await wavesApi.post(CONSTANTS2.TOKEN_REFRESH_URL, data, { headers: { ...CONSTANTS2.REQUEST_HEADERS_BASE, "token": token } });
|
|
1636
1693
|
if (response.data.code === 220) {
|
|
1637
1694
|
logger.info(`${roleId} 获取可用性成功,账号已过期`);
|
|
1638
1695
|
return { status: false, msg: "账号已过期" };
|
|
@@ -1653,7 +1710,7 @@ async function refreshData(bat, serverId, roleId, token) {
|
|
|
1653
1710
|
"roleId": roleId
|
|
1654
1711
|
});
|
|
1655
1712
|
try {
|
|
1656
|
-
const response = await wavesApi.post(
|
|
1713
|
+
const response = await wavesApi.post(CONSTANTS2.REFRESH_URL, data, { headers: { ...CONSTANTS2.REQUEST_HEADERS_BASE, "token": token, "b-at": bat } });
|
|
1657
1714
|
if (response.data.code === 10902 || response.data.code === 200) {
|
|
1658
1715
|
return { status: true, data: response.data.data };
|
|
1659
1716
|
} else {
|
|
@@ -1680,7 +1737,7 @@ async function signIn(qq_uid) {
|
|
|
1680
1737
|
"reqMonth": ((/* @__PURE__ */ new Date()).getMonth() + 1).toString().padStart(2, "0")
|
|
1681
1738
|
});
|
|
1682
1739
|
try {
|
|
1683
|
-
const response = await wavesApi.post(
|
|
1740
|
+
const response = await wavesApi.post(CONSTANTS2.SIGNIN_URL, data, { headers: { ...CONSTANTS2.REQUEST_HEADERS_BASE, "token": userInfo.token, devcode: "", "b-at": userInfo.bat } });
|
|
1684
1741
|
if (response.data.code === 200) {
|
|
1685
1742
|
if (response.data.data === null) {
|
|
1686
1743
|
logger.info(`[signIn Info]: ${qq_uid} 签到失败,返回空数据`);
|
|
@@ -1709,7 +1766,7 @@ async function getRoleData(qq_uid) {
|
|
|
1709
1766
|
"roleId": userInfo.role_id
|
|
1710
1767
|
});
|
|
1711
1768
|
try {
|
|
1712
|
-
const response = await wavesApi.post(
|
|
1769
|
+
const response = await wavesApi.post(CONSTANTS2.ROLE_DATA_URL, data, { headers: { ...CONSTANTS2.REQUEST_HEADERS_BASE, "token": userInfo.token, "b-at": userInfo.bat } });
|
|
1713
1770
|
if (response.data.code === 10902 || response.data.code === 200) {
|
|
1714
1771
|
response.data.data = JSON.parse(response.data.data);
|
|
1715
1772
|
if (response.data.data === null || !response.data.data.showToGuest) {
|
|
@@ -1797,7 +1854,8 @@ var Config = import_koishi9.Schema.object({
|
|
|
1797
1854
|
redis_config: RedisConfigSchema.description("Redis配置"),
|
|
1798
1855
|
mysql_config: MysqlConfigSchema.description("MySQL 数据库配置"),
|
|
1799
1856
|
tag_config: SaveConfigSchema.description("tag图片保存配置"),
|
|
1800
|
-
repeat_config: RepeatConfigSchema.description("复读配置")
|
|
1857
|
+
repeat_config: RepeatConfigSchema.description("复读配置"),
|
|
1858
|
+
test: import_koishi9.Schema.string().description("测试")
|
|
1801
1859
|
});
|
|
1802
1860
|
var logger = new import_koishi9.Logger(name);
|
|
1803
1861
|
var savePath = null;
|
|
@@ -1805,7 +1863,6 @@ async function apply(ctx, config) {
|
|
|
1805
1863
|
ctx = ctx.guild();
|
|
1806
1864
|
dev_mode = config.function_config.dev_mode;
|
|
1807
1865
|
initSequelize(config?.mysql_config);
|
|
1808
|
-
init_redis(config?.redis_config);
|
|
1809
1866
|
const connected = await testConnect();
|
|
1810
1867
|
if (connected) {
|
|
1811
1868
|
await getTagsModel().sync();
|
|
@@ -1825,7 +1882,7 @@ async function apply(ctx, config) {
|
|
|
1825
1882
|
if (config.function_config)
|
|
1826
1883
|
registerKuroCommands(ctx, connected);
|
|
1827
1884
|
if (dev_mode)
|
|
1828
|
-
registerTestCommands(ctx);
|
|
1885
|
+
registerTestCommands(ctx, config);
|
|
1829
1886
|
}
|
|
1830
1887
|
__name(apply, "apply");
|
|
1831
1888
|
// Annotate the CommonJS export names for ESM import in node:
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export interface OpenRouterMessage {
|
|
2
|
+
role: 'system' | 'user' | 'assistant';
|
|
3
|
+
content: string;
|
|
4
|
+
}
|
|
5
|
+
export interface OpenRouterResult {
|
|
6
|
+
err: string | null;
|
|
7
|
+
resp: string | null;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* 调用 OpenRouter API 进行对话
|
|
11
|
+
* @param apiKey OpenRouter API Key
|
|
12
|
+
* @param messages 消息数组,包含系统消息和用户消息
|
|
13
|
+
* @param model 模型名称,默认为 gpt-4o-mini
|
|
14
|
+
* @returns 返回包含错误和响应的对象 { err: string | null, resp: string | null }
|
|
15
|
+
* err 不为 null 时表示出错,resp 不为 null 时表示成功
|
|
16
|
+
*/
|
|
17
|
+
export declare function callOpenRouter(apiKey: string, messages: OpenRouterMessage[], model?: string): Promise<OpenRouterResult>;
|