dsh-shlr-bilitoolkit 0.1.2 → 0.1.3

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/dist/dsh.d.ts CHANGED
@@ -18,6 +18,9 @@ export interface DshTool {
18
18
  description: string;
19
19
  parameters: Record<string, unknown>;
20
20
  handler: (args: any) => Promise<unknown>;
21
+ render?: (args: any, value: any) => any[];
22
+ presentationMeta?: (args: any, value: any) => unknown;
23
+ presentResult?: (args: any, result: any) => any;
21
24
  }
22
25
  export declare const TOOLS: DshTool[];
23
26
  export declare function apply(ctx: any): void;
package/dist/dsh.js CHANGED
@@ -13,10 +13,16 @@ import { defineTool } from "@deepseek-ai/dsh-tools";
13
13
  import QRCode from "qrcode";
14
14
  import { BiliClient } from "./client.js";
15
15
  import { Collection, Page, PlayInfo, Subtitle, User, Video } from "./models.js";
16
- // 插件入口必须导出的字段(Cordis 据此识别并注入 tools 服务)
16
+ import { QR_STATUS_EXPIRED, QR_STATUS_SCANNED, QR_STATUS_SUCCESS, } from "./apis/auth.js";
17
+ // 插件入口必须导出的字段(Cordis 据此识别并注入相应服务)
17
18
  export const name = "bili-toolkit";
18
- export const inject = ["tools"];
19
+ // tools:注册工具;attachments:把二维码等图片登记成可在 UI 渲染的持久附件
20
+ export const inject = ["tools", "attachments"];
19
21
  let cached = null;
22
+ // 附件服务(apply 时从 ctx 捕获);缺失时二维码退化为文本
23
+ let attachmentStore = null;
24
+ // 最近一次生成、等待扫码确认的二维码 key(登录完成链路用)
25
+ let pendingQrKey = null;
20
26
  async function getClient() {
21
27
  if (!cached) {
22
28
  cached = new BiliClient({
@@ -406,27 +412,127 @@ export const TOOLS = [
406
412
  // ---- 登录状态 ----
407
413
  {
408
414
  id: "bili_login_status",
409
- description: "查询当前是否已登录 B 站(返回用户名)。未登录时先让用户执行 `npm run login` 扫码,或调用 bili_gen_qr。",
415
+ description: "查询当前是否已登录 B 站(返回用户名)。若此前调用过 bili_gen_qr,本工具会顺带轮询扫码状态:用户扫码并在手机确认后,这里返回登录成功并自动保存 Cookie。",
410
416
  parameters: { type: "object", properties: {} },
411
417
  handler: async () => {
412
418
  const c = await getClient();
413
419
  const account = await c.getAccount();
420
+ if (account) {
421
+ return { isLoggedIn: true, username: account.uname, mid: account.mid };
422
+ }
423
+ // 未登录:若有等待确认的扫码,轮询一次
424
+ if (pendingQrKey) {
425
+ const [code, message] = await c.auth.poll(pendingQrKey);
426
+ if (code === QR_STATUS_SUCCESS) {
427
+ await c.saveCookies();
428
+ pendingQrKey = null;
429
+ const acc = await c.getAccount();
430
+ return {
431
+ isLoggedIn: true,
432
+ justLoggedIn: true,
433
+ username: acc?.uname ?? null,
434
+ mid: acc?.mid ?? null,
435
+ };
436
+ }
437
+ if (code === QR_STATUS_EXPIRED) {
438
+ pendingQrKey = null;
439
+ return {
440
+ isLoggedIn: false,
441
+ qrStatus: "expired",
442
+ hint: "二维码已过期,请重新调用 bili_gen_qr 生成新的二维码。",
443
+ };
444
+ }
445
+ return {
446
+ isLoggedIn: false,
447
+ qrStatus: code === QR_STATUS_SCANNED ? "scanned" : "waiting",
448
+ hint: code === QR_STATUS_SCANNED
449
+ ? "已扫码,请在手机上点击「确认登录」,然后再调用一次 bili_login_status。"
450
+ : "请用「哔哩哔哩」App 扫描刚生成的二维码,确认后再调用 bili_login_status。",
451
+ message,
452
+ };
453
+ }
414
454
  return {
415
- isLoggedIn: !!account,
416
- username: account?.uname ?? null,
417
- mid: account?.mid ?? null,
455
+ isLoggedIn: false,
456
+ username: null,
457
+ mid: null,
458
+ hint: "未登录,请先调用 bili_gen_qr 生成登录二维码。",
418
459
  };
419
460
  },
420
461
  },
421
462
  {
422
463
  id: "bili_gen_qr",
423
- description: "生成 B 站扫码登录二维码。返回二维码内容链接 url、轮询用的 qrcodeKey,以及可直接在终端 / 对话里展示的 ascii 二维码(用「哔哩哔哩」App 扫码确认后,再调用 bili_login_status 查看登录态)。",
464
+ description: "生成 B 站扫码登录二维码,并在对话中以「图片」形式展示(用「哔哩哔哩」App 扫码)。返回二维码内容链接 url 与轮询用的 qrcodeKey。用户扫码确认后,调用 bili_login_status 完成登录并保存 Cookie。",
424
465
  parameters: { type: "object", properties: {} },
425
466
  handler: async () => {
426
467
  const c = await getClient();
427
468
  const [url, key] = await c.auth.generate();
428
- const ascii = await QRCode.toString(url, { type: "utf8" });
429
- return { url, qrcodeKey: key, ascii };
469
+ pendingQrKey = key;
470
+ // 生成 PNG 二维码并登记为附件,供 UI 以图片渲染
471
+ let qrImage = null;
472
+ try {
473
+ const png = await QRCode.toBuffer(url, {
474
+ type: "png",
475
+ errorCorrectionLevel: "M",
476
+ margin: 2,
477
+ width: 320,
478
+ });
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
+ }
486
+ }
487
+ catch {
488
+ qrImage = null; // 附件不可用时退化为下方 ascii
489
+ }
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 };
501
+ },
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 用)
512
+ presentationMeta: (_args, value) => ({
513
+ qrImage: value?.qrImage ?? null,
514
+ ascii: value?.ascii ?? null,
515
+ url: value?.url ?? null,
516
+ }),
517
+ // UI 呈现:优先渲染图片二维码;无附件时退化为 ascii 代码块 + 链接
518
+ presentResult: (_args, result) => {
519
+ const meta = (result?.meta ?? {});
520
+ const content = [];
521
+ if (meta.qrImage) {
522
+ content.push({ type: "image", attachment: meta.qrImage });
523
+ content.push({
524
+ type: "text",
525
+ text: "请用「哔哩哔哩」App 扫描上方二维码,并在手机上确认登录。",
526
+ });
527
+ }
528
+ else if (meta.ascii) {
529
+ content.push({ type: "text", text: "```\n" + meta.ascii + "\n```" });
530
+ content.push({ type: "text", text: `二维码链接:${meta.url ?? ""}` });
531
+ }
532
+ else {
533
+ content.push({ type: "text", text: `二维码链接:${meta.url ?? ""}` });
534
+ }
535
+ return { card: "generic", title: "扫码登录 B 站", content };
430
536
  },
431
537
  },
432
538
  // ---- 缓存维护 ----
@@ -442,22 +548,31 @@ export const TOOLS = [
442
548
  },
443
549
  ];
444
550
  export function apply(ctx) {
551
+ // 捕获附件服务,供 bili_gen_qr 把二维码登记成可在 UI 渲染的图片
552
+ attachmentStore = ctx?.attachments ?? null;
445
553
  for (const tool of TOOLS) {
446
- // 官方路径:ctx.tools.register(defineTool({...}))
447
- const def = defineTool({
554
+ const output = {
555
+ schema: { type: "object", additionalProperties: true },
556
+ render: tool.render ??
557
+ ((_args, value) => [{ type: "text", text: textOf(value) }]),
558
+ };
559
+ if (tool.presentationMeta) {
560
+ output.presentationMeta = tool.presentationMeta;
561
+ }
562
+ const options = {
448
563
  name: tool.id,
449
564
  description: tool.description,
450
565
  parameters: toDshParameters(tool.parameters),
451
- output: {
452
- schema: { type: "object", additionalProperties: true },
453
- render: (_args, value) => [
454
- { type: "text", text: textOf(value) },
455
- ],
456
- },
566
+ output,
457
567
  async execute(args) {
458
568
  return await tool.handler(args);
459
569
  },
460
- });
570
+ };
571
+ if (tool.presentResult) {
572
+ options.presentResult = tool.presentResult;
573
+ }
574
+ // 官方路径:ctx.tools.register(defineTool({...}))
575
+ const def = defineTool(options);
461
576
  if (ctx?.tools?.register) {
462
577
  ctx.tools.register(def);
463
578
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-shlr-bilitoolkit",
3
- "version": "0.1.2",
4
- "description": "B 站(哔哩哔哩)接口工具箱 dsh 插件:视频 / 搜索 / 空间 / 专栏 / 动态 / 评论 / 关注 / 消息中心读取,以及点赞、收藏、投币、关注、评论、私信等写操作。",
3
+ "version": "0.1.3",
4
+ "description": "深海猎人专用B站接口工具箱 dsh 插件。",
5
5
  "type": "module",
6
6
  "main": "dist/dsh.js",
7
7
  "types": "dist/dsh.d.ts",