koishi-plugin-sy-bot 0.0.1 → 0.0.5

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
@@ -65,7 +65,9 @@ async function markdownToImage(ctx, markdown) {
65
65
  language: lang
66
66
  }).value}</code></pre>`;
67
67
  }
68
- return `<pre class="hljs"><code>${md.utils.escapeHtml(code)}</code></pre>`;
68
+ return `<pre class="hljs"><code>${md.utils.escapeHtml(
69
+ code
70
+ )}</code></pre>`;
69
71
  }
70
72
  });
71
73
  md.use(import_markdown_it_katex.default);
@@ -160,28 +162,45 @@ ${hljsCss}
160
162
  </body>
161
163
  </html>
162
164
  `;
163
- const page = await ctx.puppeteer.page();
164
- await page.setViewport({
165
- width: 1e3,
166
- height: 10,
167
- deviceScaleFactor: 2
168
- });
169
- await page.setContent(html, {
170
- waitUntil: "networkidle0"
171
- });
172
- const body = await page.$("body");
173
- const box = await body.boundingBox();
174
- const buffer = await page.screenshot({
175
- type: "png",
176
- clip: {
177
- x: 0,
178
- y: 0,
179
- width: Math.ceil(box.width),
180
- height: Math.ceil(box.height)
181
- }
182
- });
183
- await page.close();
184
- return buffer;
165
+ try {
166
+ console.log("开始创建页面...");
167
+ const page = await ctx.puppeteer.page();
168
+ console.log("页面创建成功!");
169
+ console.log("开始设置视口...");
170
+ await page.setViewport({
171
+ width: 1e3,
172
+ height: 800,
173
+ deviceScaleFactor: 2
174
+ });
175
+ console.log("视口设置成功");
176
+ console.log("开始加载内容...");
177
+ await page.setContent(html, {
178
+ waitUntil: "domcontentloaded",
179
+ timeout: 3e4
180
+ });
181
+ console.log("内容加载成功");
182
+ console.log("开始设置body...");
183
+ const body = await page.$("body");
184
+ console.log("设置box...");
185
+ const box = await body.boundingBox();
186
+ console.log("获取page截图缓冲区...");
187
+ const buffer = await page.screenshot({
188
+ type: "png",
189
+ clip: {
190
+ x: 0,
191
+ y: 0,
192
+ width: Math.ceil(box.width),
193
+ height: Math.ceil(box.height)
194
+ }
195
+ });
196
+ console.log("正在关闭页面...");
197
+ await page.close();
198
+ console.log("成功关闭页面...");
199
+ return buffer;
200
+ } catch (err) {
201
+ console.log(err);
202
+ throw "pptr 建立报错:" + (err instanceof Error ? err.message : String(err));
203
+ }
185
204
  }
186
205
  __name(markdownToImage, "markdownToImage");
187
206
 
@@ -247,6 +266,7 @@ async function getAiChatResult({ config, user_query }) {
247
266
  }
248
267
  }
249
268
  );
269
+ console.log("Gemini调用结果:", data);
250
270
  const { content } = data.candidates[0];
251
271
  const result = content.parts[1].text;
252
272
  return result;
@@ -329,10 +349,17 @@ async function sendOnlyText({ session, ctx, config, query }) {
329
349
  user_query: question
330
350
  });
331
351
  console.log("AI的回复:", raw_answer);
332
- const h_ans_img = await textToImage(ctx, raw_answer);
333
- session.send(
334
- /* @__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"
335
- );
352
+ if (!raw_answer) {
353
+ return "AI没有回复";
354
+ }
355
+ try {
356
+ const h_ans_img = await textToImage(ctx, raw_answer);
357
+ session.send(
358
+ /* @__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
+ );
360
+ } catch (err) {
361
+ return "调用 textToImage 报错:" + (err instanceof Error ? err.message : String(err));
362
+ }
336
363
  }
337
364
  __name(sendOnlyText, "sendOnlyText");
338
365
  async function startEmoji(session) {
@@ -379,20 +406,24 @@ function registerAsk(ctx, config) {
379
406
  console.log("用户输入的图像:", finalImages);
380
407
  console.log("用户输入的文字:", finalText);
381
408
  await startEmoji(session);
382
- if (finalImages.length > 0) {
383
- await sendNotOnlyText({
384
- ctx,
385
- session,
386
- config,
387
- url_list: finalImages,
388
- query: finalText
389
- });
390
- } else {
391
- await sendOnlyText({ session, ctx, config, query: finalText });
409
+ try {
410
+ if (finalImages.length > 0) {
411
+ await sendNotOnlyText({
412
+ ctx,
413
+ session,
414
+ config,
415
+ url_list: finalImages,
416
+ query: finalText
417
+ });
418
+ } else {
419
+ await sendOnlyText({ session, ctx, config, query: finalText });
420
+ }
421
+ } finally {
422
+ await finishEmoji(session);
392
423
  }
393
- await finishEmoji(session);
394
424
  } catch (err) {
395
- console.error(err);
425
+ console.error("ask 命令执行错误:", err);
426
+ await finishEmoji(session);
396
427
  return "调用ask发生错误:" + (err instanceof Error ? err.message : String(err));
397
428
  }
398
429
  });
@@ -527,7 +558,7 @@ var inject = {
527
558
  var Config = import_koishi3.Schema.object({
528
559
  api: import_koishi3.Schema.string().required(),
529
560
  model: import_koishi3.Schema.string().required(),
530
- prompt: import_koishi3.Schema.string().required()
561
+ prompt: import_koishi3.Schema.string()
531
562
  });
532
563
  function apply(ctx, config) {
533
564
  registerCommand(ctx, config);
@@ -32,7 +32,7 @@ export declare function sendOnlyText({ session, ctx, config, query }: {
32
32
  ctx: any;
33
33
  config: any;
34
34
  query: any;
35
- }): Promise<void>;
35
+ }): Promise<string>;
36
36
  export declare function sendEmpty(query: any): boolean;
37
37
  export declare function startEmoji(session: Session): Promise<void>;
38
38
  export declare function finishEmoji(session: Session): Promise<void>;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-sy-bot",
3
- "description": "个人qq群Bot - 风纪委员",
4
- "version": "0.0.1",
3
+ "description": "考研群答疑Bot,接入Gemini。",
4
+ "version": "0.0.5",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
7
7
  "files": [
@@ -9,14 +9,19 @@
9
9
  "dist"
10
10
  ],
11
11
  "license": "MIT",
12
- "scripts": {},
12
+ "scripts": {
13
+ "dev": "cd ../.. && npm run dev",
14
+ "build": "cd ../.. && npm run build",
15
+ "bump": "cd ../.. && npm run bump",
16
+ "pub": "cd ../.. && npm run pub"
17
+ },
13
18
  "keywords": [
14
19
  "chatbot",
15
20
  "koishi",
16
21
  "plugin"
17
22
  ],
18
23
  "peerDependencies": {
19
- "koishi": "^4.18.7"
24
+ "koishi": "^4.18.10"
20
25
  },
21
26
  "dependencies": {
22
27
  "@satorijs/element": "^3.1.8",
package/readme.md CHANGED
@@ -2,4 +2,8 @@
2
2
 
3
3
  [![npm](https://img.shields.io/npm/v/koishi-plugin-sy-bot?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-sy-bot)
4
4
 
5
- test
5
+ ## 考研群答疑机器人
6
+
7
+
8
+ Gemini API取自:[https://api.qingyuntop.top/](https://api.qingyuntop.top/)
9
+