koishi-plugin-sy-bot 0.0.6 → 0.0.8
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 +65 -41
- package/lib/utils/utils.d.ts +2 -7
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -162,28 +162,29 @@ ${hljsCss}
|
|
|
162
162
|
</body>
|
|
163
163
|
</html>
|
|
164
164
|
`;
|
|
165
|
+
const page = await ctx.puppeteer.page();
|
|
165
166
|
try {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
167
|
+
const logger = ctx.logger("sy-bot");
|
|
168
|
+
logger.info("开始创建页面...");
|
|
169
|
+
logger.info("页面创建成功!");
|
|
170
|
+
logger.info("开始设置视口...");
|
|
170
171
|
await page.setViewport({
|
|
171
172
|
width: 1e3,
|
|
172
173
|
height: 800,
|
|
173
174
|
deviceScaleFactor: 2
|
|
174
175
|
});
|
|
175
|
-
|
|
176
|
-
|
|
176
|
+
logger.info("视口设置成功");
|
|
177
|
+
logger.info("开始加载内容...");
|
|
177
178
|
await page.setContent(html, {
|
|
178
179
|
waitUntil: "domcontentloaded",
|
|
179
180
|
timeout: 3e4
|
|
180
181
|
});
|
|
181
|
-
|
|
182
|
-
|
|
182
|
+
logger.info("内容加载成功");
|
|
183
|
+
logger.info("开始设置body...");
|
|
183
184
|
const body = await page.$("body");
|
|
184
|
-
|
|
185
|
+
logger.info("设置box...");
|
|
185
186
|
const box = await body.boundingBox();
|
|
186
|
-
|
|
187
|
+
logger.info("获取page截图缓冲区...");
|
|
187
188
|
const buffer = await page.screenshot({
|
|
188
189
|
type: "png",
|
|
189
190
|
clip: {
|
|
@@ -193,13 +194,15 @@ ${hljsCss}
|
|
|
193
194
|
height: Math.ceil(box.height)
|
|
194
195
|
}
|
|
195
196
|
});
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
console.log("成功关闭页面...");
|
|
197
|
+
logger.info("正在关闭页面...");
|
|
198
|
+
logger.info("成功关闭页面...");
|
|
199
199
|
return buffer;
|
|
200
200
|
} catch (err) {
|
|
201
|
-
|
|
201
|
+
const logger = ctx.logger("sy-bot");
|
|
202
|
+
logger.error(err);
|
|
202
203
|
throw "pptr 建立报错:" + (err instanceof Error ? err.message : String(err));
|
|
204
|
+
} finally {
|
|
205
|
+
await page.close();
|
|
203
206
|
}
|
|
204
207
|
}
|
|
205
208
|
__name(markdownToImage, "markdownToImage");
|
|
@@ -233,8 +236,9 @@ async function sendGroupMessageReaction(session, face_id, ifSet = true) {
|
|
|
233
236
|
);
|
|
234
237
|
}
|
|
235
238
|
__name(sendGroupMessageReaction, "sendGroupMessageReaction");
|
|
236
|
-
async function getAiChatResult({ config, user_query }) {
|
|
239
|
+
async function getAiChatResult({ config, user_query, ctx }) {
|
|
237
240
|
try {
|
|
241
|
+
const logger = ctx.logger("sy-bot");
|
|
238
242
|
const { model: MODEL, api: API_KEY, prompt: system_prompt } = config;
|
|
239
243
|
const { data } = await import_axios.default.post(
|
|
240
244
|
`https://api.qingyuntop.top/v1beta/models/${MODEL}?key=${API_KEY}`,
|
|
@@ -266,12 +270,15 @@ async function getAiChatResult({ config, user_query }) {
|
|
|
266
270
|
}
|
|
267
271
|
}
|
|
268
272
|
);
|
|
269
|
-
console.log("Gemini
|
|
273
|
+
console.log("Gemini调用结果:\n", data);
|
|
270
274
|
const { content } = data.candidates[0];
|
|
271
|
-
|
|
275
|
+
logger.info("提取AI请求结果content:\n", content);
|
|
276
|
+
logger.info("过滤后的parts回复列表:\n", data.candidates[0].content.parts.filter((part) => part.text));
|
|
277
|
+
const result = content.parts.filter((part) => part.text).map((part) => part.text).join("");
|
|
278
|
+
logger.info("AI回复的文字:", result);
|
|
272
279
|
return result;
|
|
273
280
|
} catch (error) {
|
|
274
|
-
console.error("
|
|
281
|
+
console.error("getAiChatResult() 调用报错!\n", error);
|
|
275
282
|
}
|
|
276
283
|
}
|
|
277
284
|
__name(getAiChatResult, "getAiChatResult");
|
|
@@ -289,6 +296,7 @@ async function getAiImgUnderstandMultiple({
|
|
|
289
296
|
url_list,
|
|
290
297
|
query
|
|
291
298
|
}) {
|
|
299
|
+
const logger = ctx.logger("sy-bot");
|
|
292
300
|
try {
|
|
293
301
|
const { model: MODEL, api: API_KEY } = config;
|
|
294
302
|
let base64_list = [];
|
|
@@ -319,10 +327,13 @@ async function getAiImgUnderstandMultiple({
|
|
|
319
327
|
`https://api.qingyuntop.top/v1beta/models/${MODEL}?key=${API_KEY}`,
|
|
320
328
|
BODY
|
|
321
329
|
);
|
|
322
|
-
|
|
330
|
+
logger.info("axios 接口返回的数据 data: \n", data);
|
|
331
|
+
logger.info("过滤后的parts回复列表:\n", data.candidates[0].content.parts.filter((part) => part.text));
|
|
332
|
+
const text = data.candidates[0].content.parts.filter((part) => part.text).map((p) => p.text).join("");
|
|
333
|
+
logger.info("解析出的text:", text);
|
|
323
334
|
return text;
|
|
324
335
|
} catch (error) {
|
|
325
|
-
|
|
336
|
+
logger.error("getAiChatResult() 调用报错!\n", error);
|
|
326
337
|
}
|
|
327
338
|
}
|
|
328
339
|
__name(getAiImgUnderstandMultiple, "getAiImgUnderstandMultiple");
|
|
@@ -344,16 +355,19 @@ __name(getElementsImages, "getElementsImages");
|
|
|
344
355
|
async function sendOnlyText({ session, ctx, config, query }) {
|
|
345
356
|
const quote = session?.event?.message?.quote?.content;
|
|
346
357
|
const question = quote ? query + quote : query;
|
|
358
|
+
const logger = ctx.logger("sy-bot");
|
|
347
359
|
const raw_answer = await getAiChatResult({
|
|
348
360
|
config,
|
|
349
|
-
user_query: question
|
|
361
|
+
user_query: question,
|
|
362
|
+
ctx
|
|
350
363
|
});
|
|
351
|
-
console.log("AI的回复:", raw_answer);
|
|
352
364
|
if (!raw_answer) {
|
|
353
365
|
return "AI没有回复";
|
|
354
366
|
}
|
|
355
367
|
try {
|
|
368
|
+
logger.info("开始渲染markdown...");
|
|
356
369
|
const h_ans_img = await textToImage(ctx, raw_answer);
|
|
370
|
+
logger.info("markdown渲染完毕!");
|
|
357
371
|
session.send(
|
|
358
372
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("quote", { id: session.event.message.id }) + /* @__PURE__ */ (0, import_jsx_runtime.jsx)("at", { id: session.event.user.id }) + h_ans_img + "\n"
|
|
359
373
|
);
|
|
@@ -377,14 +391,18 @@ async function sendNotOnlyText({
|
|
|
377
391
|
url_list,
|
|
378
392
|
query
|
|
379
393
|
}) {
|
|
394
|
+
const logger = ctx.logger("sy-bot");
|
|
395
|
+
logger.info("开始执行 getAiImgUnderstandMultiple()");
|
|
380
396
|
const raw_answer = await getAiImgUnderstandMultiple({
|
|
381
397
|
ctx,
|
|
382
398
|
config,
|
|
383
399
|
url_list,
|
|
384
400
|
query
|
|
385
401
|
});
|
|
386
|
-
|
|
402
|
+
logger.info("getAiImgUnderstandMultiple执行完毕,得到AI的回答:", raw_answer);
|
|
403
|
+
logger.info("开始渲染markdown,执行textToImage");
|
|
387
404
|
const h_ans_img = await textToImage(ctx, raw_answer);
|
|
405
|
+
logger.info("渲染完毕,返回图片结果!");
|
|
388
406
|
session.send(
|
|
389
407
|
/* @__PURE__ */ (0, import_jsx_runtime.jsx)("quote", { id: session.event.message.id }) + /* @__PURE__ */ (0, import_jsx_runtime.jsx)("at", { id: session.event.user.id }) + h_ans_img + "\n"
|
|
390
408
|
);
|
|
@@ -395,6 +413,7 @@ __name(sendNotOnlyText, "sendNotOnlyText");
|
|
|
395
413
|
function registerAsk(ctx, config) {
|
|
396
414
|
ctx.command("ask", "哈基米?哈基米~").action(async ({ session }) => {
|
|
397
415
|
try {
|
|
416
|
+
const logger = ctx.logger("sy-bot");
|
|
398
417
|
const messageElements = session.event.message.elements;
|
|
399
418
|
const quoteElements = session.event.message.quote?.elements;
|
|
400
419
|
const quoteRealText = getElementsText(quoteElements);
|
|
@@ -403,28 +422,33 @@ function registerAsk(ctx, config) {
|
|
|
403
422
|
const messageImages = getElementsImages(messageElements);
|
|
404
423
|
const finalText = quoteRealText + messageRealText;
|
|
405
424
|
const finalImages = [...quoteImages, ...messageImages];
|
|
406
|
-
|
|
407
|
-
|
|
425
|
+
logger.info("用户输入的图像:", finalImages);
|
|
426
|
+
logger.info("用户输入的文字:", finalText);
|
|
408
427
|
await startEmoji(session);
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
}
|
|
422
|
-
|
|
428
|
+
logger.info("发送表情响应");
|
|
429
|
+
if (finalImages.length > 0) {
|
|
430
|
+
logger.info("用户发送了图片,调用图片理解api...");
|
|
431
|
+
logger.info("开始执行sendNotOnlyText...");
|
|
432
|
+
await sendNotOnlyText({
|
|
433
|
+
ctx,
|
|
434
|
+
session,
|
|
435
|
+
config,
|
|
436
|
+
url_list: finalImages,
|
|
437
|
+
query: finalText
|
|
438
|
+
});
|
|
439
|
+
logger.info("sendNotOnlyText执行结束");
|
|
440
|
+
} else {
|
|
441
|
+
logger.info("用户发送了纯文本,调用文字对话api...");
|
|
442
|
+
logger.info("开始执行sendOnlyText...");
|
|
443
|
+
await sendOnlyText({ session, ctx, config, query: finalText });
|
|
444
|
+
logger.info("sendOnlyText执行结束");
|
|
423
445
|
}
|
|
424
446
|
} catch (err) {
|
|
425
|
-
|
|
447
|
+
const logger = ctx.logger("sy-bot");
|
|
448
|
+
logger.error("ask 命令执行错误:", err);
|
|
449
|
+
return "oops!出了点叉子!";
|
|
450
|
+
} finally {
|
|
426
451
|
await finishEmoji(session);
|
|
427
|
-
return "调用ask发生错误:" + (err instanceof Error ? err.message : String(err));
|
|
428
452
|
}
|
|
429
453
|
});
|
|
430
454
|
}
|
package/lib/utils/utils.d.ts
CHANGED
|
@@ -7,17 +7,12 @@ import { Context, h, Session } from "koishi";
|
|
|
7
7
|
export declare function getKaoyanCountdownText(targetTime: any): string;
|
|
8
8
|
export declare function getPrompt(): void;
|
|
9
9
|
export declare function sendGroupMessageReaction(session: any, face_id: any, ifSet?: boolean): Promise<void>;
|
|
10
|
-
export declare function getAiChatResult({ config, user_query }: {
|
|
10
|
+
export declare function getAiChatResult({ config, user_query, ctx }: {
|
|
11
11
|
config: any;
|
|
12
12
|
user_query: any;
|
|
13
|
-
}): Promise<any>;
|
|
14
|
-
export declare function imageUrlToBase64(ctx: Context, url: string): Promise<string>;
|
|
15
|
-
export declare function getAiImgUnderstand({ ctx, config, url, query }: {
|
|
16
13
|
ctx: any;
|
|
17
|
-
config: any;
|
|
18
|
-
url: any;
|
|
19
|
-
query: any;
|
|
20
14
|
}): Promise<any>;
|
|
15
|
+
export declare function imageUrlToBase64(ctx: Context, url: string): Promise<string>;
|
|
21
16
|
export declare function getAiImgUnderstandMultiple({ ctx, config, url_list, query, }: {
|
|
22
17
|
ctx: any;
|
|
23
18
|
config: any;
|