dsh-shlr-bilitoolkit 0.1.3 → 0.1.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.
Files changed (2) hide show
  1. package/dist/dsh.js +55 -53
  2. package/package.json +1 -1
package/dist/dsh.js CHANGED
@@ -11,16 +11,16 @@
11
11
  */
12
12
  import { defineTool } from "@deepseek-ai/dsh-tools";
13
13
  import QRCode from "qrcode";
14
+ import { writeFileSync } from "node:fs";
15
+ import { resolve } from "node:path";
16
+ import { pathToFileURL } from "node:url";
14
17
  import { BiliClient } from "./client.js";
15
18
  import { Collection, Page, PlayInfo, Subtitle, User, Video } from "./models.js";
16
19
  import { QR_STATUS_EXPIRED, QR_STATUS_SCANNED, QR_STATUS_SUCCESS, } from "./apis/auth.js";
17
20
  // 插件入口必须导出的字段(Cordis 据此识别并注入相应服务)
18
21
  export const name = "bili-toolkit";
19
- // tools:注册工具;attachments:把二维码等图片登记成可在 UI 渲染的持久附件
20
- export const inject = ["tools", "attachments"];
22
+ export const inject = ["tools"];
21
23
  let cached = null;
22
- // 附件服务(apply 时从 ctx 捕获);缺失时二维码退化为文本
23
- let attachmentStore = null;
24
24
  // 最近一次生成、等待扫码确认的二维码 key(登录完成链路用)
25
25
  let pendingQrKey = null;
26
26
  async function getClient() {
@@ -28,7 +28,14 @@ async function getClient() {
28
28
  cached = new BiliClient({
29
29
  cookieFile: process.env.BILI_COOKIE_FILE ?? "bili_cookies.json",
30
30
  });
31
- await cached.loadCookies();
31
+ // 没有 Cookie 文件(或为空 / 损坏)= 未登录(匿名)。
32
+ // 不能因此让所有工具崩掉——公开接口、以及最关键的扫码登录入口都依赖这里。
33
+ try {
34
+ await cached.loadCookies();
35
+ }
36
+ catch {
37
+ // 按未登录处理:匿名可用公开接口;需要登录的操作会再由接口层报错
38
+ }
32
39
  }
33
40
  return cached;
34
41
  }
@@ -461,14 +468,15 @@ export const TOOLS = [
461
468
  },
462
469
  {
463
470
  id: "bili_gen_qr",
464
- description: "生成 B 站扫码登录二维码,并在对话中以「图片」形式展示(用「哔哩哔哩」App 扫码)。返回二维码内容链接 url 与轮询用的 qrcodeKey。用户扫码确认后,调用 bili_login_status 完成登录并保存 Cookie。",
471
+ description: "生成 B 站扫码登录二维码图片。返回二维码内容链接 url、轮询用的 qrcodeKey,以及可供 dsh 自行获取/渲染的二维码图片:qrDataUrl(base64 data URL)、qrImageUrl(file:// 链接)、qrImagePath(本地 PNG 绝对路径)。用户扫码确认后,调用 bili_login_status 完成登录并保存 Cookie。",
465
472
  parameters: { type: "object", properties: {} },
466
473
  handler: async () => {
467
474
  const c = await getClient();
468
475
  const [url, key] = await c.auth.generate();
469
476
  pendingQrKey = key;
470
- // 生成 PNG 二维码并登记为附件,供 UI 以图片渲染
471
- let qrImage = null;
477
+ // 渲染成 PNG,并给出多种「dsh 可自行获取图片」的形式
478
+ let qrImagePath = null;
479
+ let qrDataUrl = null;
472
480
  try {
473
481
  const png = await QRCode.toBuffer(url, {
474
482
  type: "png",
@@ -476,62 +484,58 @@ export const TOOLS = [
476
484
  margin: 2,
477
485
  width: 320,
478
486
  });
479
- if (attachmentStore?.saveImage) {
480
- qrImage = await attachmentStore.saveImage({
481
- data: new Uint8Array(png),
482
- mediaType: "image/png",
483
- name: "bili-login-qr.png",
484
- });
485
- }
487
+ qrDataUrl = `data:image/png;base64,${png.toString("base64")}`;
488
+ qrImagePath = resolve(process.cwd(), "bili_qrcode.png");
489
+ writeFileSync(qrImagePath, png);
486
490
  }
487
491
  catch {
488
- qrImage = null; // 附件不可用时退化为下方 ascii
492
+ qrImagePath = null;
493
+ qrDataUrl = null;
489
494
  }
490
- // ascii 仅作为附件不可用时的兜底
491
- let ascii = null;
492
- if (!qrImage) {
493
- try {
494
- ascii = await QRCode.toString(url, { type: "utf8" });
495
- }
496
- catch {
497
- ascii = null;
498
- }
499
- }
500
- return { url, qrcodeKey: key, qrImage, ascii };
495
+ return {
496
+ url,
497
+ qrcodeKey: key,
498
+ qrImagePath,
499
+ qrImageUrl: qrImagePath ? pathToFileURL(qrImagePath).href : null,
500
+ qrDataUrl,
501
+ };
501
502
  },
502
- // 面向模型的文本:不塞图片 / ascii,给指引即可
503
- render: (_args, value) => [
504
- {
505
- type: "text",
506
- text: "已生成 B 站登录二维码(已在界面上以图片展示)。请让用户用「哔哩哔哩」App 扫码," +
507
- "并在手机上确认登录;用户确认后,调用 bili_login_status 查询登录态并保存 Cookie。" +
508
- `qrcodeKey=${value?.qrcodeKey ?? ""}`,
509
- },
510
- ],
511
- // 把图片引用 / ascii / url 透传给 presentResult(仅 UI 用)
503
+ // 面向模型:简洁文本 + 一个 file:// 图片引用(小,不塞 base64)
504
+ render: (_args, value) => {
505
+ const lines = ["已生成 B 站登录二维码,请展示给用户扫码:"];
506
+ if (value?.qrImageUrl)
507
+ lines.push(`![B站登录二维码](${value.qrImageUrl})`);
508
+ else if (value?.qrDataUrl)
509
+ lines.push(`![B站登录二维码](${value.qrDataUrl})`);
510
+ lines.push(`二维码链接:${value?.url ?? ""}`);
511
+ lines.push(`qrcodeKey=${value?.qrcodeKey ?? ""}`);
512
+ lines.push("用户扫码并在手机确认后,调用 bili_login_status 完成登录。");
513
+ return [{ type: "text", text: lines.join("\n") }];
514
+ },
515
+ // 把图片引用透传给 presentResult(仅 UI 用)
512
516
  presentationMeta: (_args, value) => ({
513
- qrImage: value?.qrImage ?? null,
514
- ascii: value?.ascii ?? null,
517
+ qrDataUrl: value?.qrDataUrl ?? null,
518
+ qrImageUrl: value?.qrImageUrl ?? null,
519
+ qrImagePath: value?.qrImagePath ?? null,
515
520
  url: value?.url ?? null,
516
521
  }),
517
- // UI 呈现:优先渲染图片二维码;无附件时退化为 ascii 代码块 + 链接
522
+ // UI 呈现:用自包含的 base64 data URL 以 markdown 图片渲染,dsh 自行获取
518
523
  presentResult: (_args, result) => {
519
524
  const meta = (result?.meta ?? {});
520
525
  const content = [];
521
- if (meta.qrImage) {
522
- content.push({ type: "image", attachment: meta.qrImage });
523
- content.push({
524
- type: "text",
525
- text: "请用「哔哩哔哩」App 扫描上方二维码,并在手机上确认登录。",
526
- });
526
+ if (meta.qrDataUrl) {
527
+ content.push({ type: "text", text: `![B站登录二维码](${meta.qrDataUrl})` });
527
528
  }
528
- else if (meta.ascii) {
529
- content.push({ type: "text", text: "```\n" + meta.ascii + "\n```" });
530
- content.push({ type: "text", text: `二维码链接:${meta.url ?? ""}` });
529
+ else if (meta.qrImageUrl) {
530
+ content.push({ type: "text", text: `![B站登录二维码](${meta.qrImageUrl})` });
531
531
  }
532
- else {
533
- content.push({ type: "text", text: `二维码链接:${meta.url ?? ""}` });
532
+ if (meta.qrImagePath) {
533
+ content.push({ type: "text", text: `二维码图片已保存到:${meta.qrImagePath}` });
534
534
  }
535
+ content.push({
536
+ type: "text",
537
+ text: "请用「哔哩哔哩」App 扫描上方二维码,并在手机上确认登录。",
538
+ });
535
539
  return { card: "generic", title: "扫码登录 B 站", content };
536
540
  },
537
541
  },
@@ -548,8 +552,6 @@ export const TOOLS = [
548
552
  },
549
553
  ];
550
554
  export function apply(ctx) {
551
- // 捕获附件服务,供 bili_gen_qr 把二维码登记成可在 UI 渲染的图片
552
- attachmentStore = ctx?.attachments ?? null;
553
555
  for (const tool of TOOLS) {
554
556
  const output = {
555
557
  schema: { type: "object", additionalProperties: true },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-shlr-bilitoolkit",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "深海猎人专用B站接口工具箱 dsh 插件。",
5
5
  "type": "module",
6
6
  "main": "dist/dsh.js",