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.
- package/dist/dsh.js +55 -53
- 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
|
-
|
|
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
|
-
|
|
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
|
|
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
|
-
//
|
|
471
|
-
let
|
|
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
|
-
|
|
480
|
-
|
|
481
|
-
|
|
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
|
-
|
|
492
|
+
qrImagePath = null;
|
|
493
|
+
qrDataUrl = null;
|
|
489
494
|
}
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
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
|
-
//
|
|
503
|
-
render: (_args, value) =>
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
}
|
|
510
|
-
|
|
511
|
-
|
|
503
|
+
// 面向模型:简洁文本 + 一个 file:// 图片引用(小,不塞 base64)
|
|
504
|
+
render: (_args, value) => {
|
|
505
|
+
const lines = ["已生成 B 站登录二维码,请展示给用户扫码:"];
|
|
506
|
+
if (value?.qrImageUrl)
|
|
507
|
+
lines.push(``);
|
|
508
|
+
else if (value?.qrDataUrl)
|
|
509
|
+
lines.push(``);
|
|
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
|
-
|
|
514
|
-
|
|
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
|
|
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.
|
|
522
|
-
content.push({ type: "
|
|
523
|
-
content.push({
|
|
524
|
-
type: "text",
|
|
525
|
-
text: "请用「哔哩哔哩」App 扫描上方二维码,并在手机上确认登录。",
|
|
526
|
-
});
|
|
526
|
+
if (meta.qrDataUrl) {
|
|
527
|
+
content.push({ type: "text", text: `` });
|
|
527
528
|
}
|
|
528
|
-
else if (meta.
|
|
529
|
-
content.push({ type: "text", text:
|
|
530
|
-
content.push({ type: "text", text: `二维码链接:${meta.url ?? ""}` });
|
|
529
|
+
else if (meta.qrImageUrl) {
|
|
530
|
+
content.push({ type: "text", text: `` });
|
|
531
531
|
}
|
|
532
|
-
|
|
533
|
-
content.push({ type: "text", text:
|
|
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 },
|