dsh-shlr-bilitoolkit 0.1.4 → 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 +47 -52
- 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() {
|
|
@@ -468,14 +468,15 @@ export const TOOLS = [
|
|
|
468
468
|
},
|
|
469
469
|
{
|
|
470
470
|
id: "bili_gen_qr",
|
|
471
|
-
description: "生成 B
|
|
471
|
+
description: "生成 B 站扫码登录二维码图片。返回二维码内容链接 url、轮询用的 qrcodeKey,以及可供 dsh 自行获取/渲染的二维码图片:qrDataUrl(base64 data URL)、qrImageUrl(file:// 链接)、qrImagePath(本地 PNG 绝对路径)。用户扫码确认后,调用 bili_login_status 完成登录并保存 Cookie。",
|
|
472
472
|
parameters: { type: "object", properties: {} },
|
|
473
473
|
handler: async () => {
|
|
474
474
|
const c = await getClient();
|
|
475
475
|
const [url, key] = await c.auth.generate();
|
|
476
476
|
pendingQrKey = key;
|
|
477
|
-
//
|
|
478
|
-
let
|
|
477
|
+
// 渲染成 PNG,并给出多种「dsh 可自行获取图片」的形式
|
|
478
|
+
let qrImagePath = null;
|
|
479
|
+
let qrDataUrl = null;
|
|
479
480
|
try {
|
|
480
481
|
const png = await QRCode.toBuffer(url, {
|
|
481
482
|
type: "png",
|
|
@@ -483,62 +484,58 @@ export const TOOLS = [
|
|
|
483
484
|
margin: 2,
|
|
484
485
|
width: 320,
|
|
485
486
|
});
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
mediaType: "image/png",
|
|
490
|
-
name: "bili-login-qr.png",
|
|
491
|
-
});
|
|
492
|
-
}
|
|
487
|
+
qrDataUrl = `data:image/png;base64,${png.toString("base64")}`;
|
|
488
|
+
qrImagePath = resolve(process.cwd(), "bili_qrcode.png");
|
|
489
|
+
writeFileSync(qrImagePath, png);
|
|
493
490
|
}
|
|
494
491
|
catch {
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
// ascii 仅作为附件不可用时的兜底
|
|
498
|
-
let ascii = null;
|
|
499
|
-
if (!qrImage) {
|
|
500
|
-
try {
|
|
501
|
-
ascii = await QRCode.toString(url, { type: "utf8" });
|
|
502
|
-
}
|
|
503
|
-
catch {
|
|
504
|
-
ascii = null;
|
|
505
|
-
}
|
|
492
|
+
qrImagePath = null;
|
|
493
|
+
qrDataUrl = null;
|
|
506
494
|
}
|
|
507
|
-
return {
|
|
495
|
+
return {
|
|
496
|
+
url,
|
|
497
|
+
qrcodeKey: key,
|
|
498
|
+
qrImagePath,
|
|
499
|
+
qrImageUrl: qrImagePath ? pathToFileURL(qrImagePath).href : null,
|
|
500
|
+
qrDataUrl,
|
|
501
|
+
};
|
|
508
502
|
},
|
|
509
|
-
//
|
|
510
|
-
render: (_args, value) =>
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
|
|
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 用)
|
|
519
516
|
presentationMeta: (_args, value) => ({
|
|
520
|
-
|
|
521
|
-
|
|
517
|
+
qrDataUrl: value?.qrDataUrl ?? null,
|
|
518
|
+
qrImageUrl: value?.qrImageUrl ?? null,
|
|
519
|
+
qrImagePath: value?.qrImagePath ?? null,
|
|
522
520
|
url: value?.url ?? null,
|
|
523
521
|
}),
|
|
524
|
-
// UI
|
|
522
|
+
// UI 呈现:用自包含的 base64 data URL 以 markdown 图片渲染,dsh 自行获取
|
|
525
523
|
presentResult: (_args, result) => {
|
|
526
524
|
const meta = (result?.meta ?? {});
|
|
527
525
|
const content = [];
|
|
528
|
-
if (meta.
|
|
529
|
-
content.push({ type: "
|
|
530
|
-
content.push({
|
|
531
|
-
type: "text",
|
|
532
|
-
text: "请用「哔哩哔哩」App 扫描上方二维码,并在手机上确认登录。",
|
|
533
|
-
});
|
|
526
|
+
if (meta.qrDataUrl) {
|
|
527
|
+
content.push({ type: "text", text: `` });
|
|
534
528
|
}
|
|
535
|
-
else if (meta.
|
|
536
|
-
content.push({ type: "text", text:
|
|
537
|
-
content.push({ type: "text", text: `二维码链接:${meta.url ?? ""}` });
|
|
529
|
+
else if (meta.qrImageUrl) {
|
|
530
|
+
content.push({ type: "text", text: `` });
|
|
538
531
|
}
|
|
539
|
-
|
|
540
|
-
content.push({ type: "text", text:
|
|
532
|
+
if (meta.qrImagePath) {
|
|
533
|
+
content.push({ type: "text", text: `二维码图片已保存到:${meta.qrImagePath}` });
|
|
541
534
|
}
|
|
535
|
+
content.push({
|
|
536
|
+
type: "text",
|
|
537
|
+
text: "请用「哔哩哔哩」App 扫描上方二维码,并在手机上确认登录。",
|
|
538
|
+
});
|
|
542
539
|
return { card: "generic", title: "扫码登录 B 站", content };
|
|
543
540
|
},
|
|
544
541
|
},
|
|
@@ -555,8 +552,6 @@ export const TOOLS = [
|
|
|
555
552
|
},
|
|
556
553
|
];
|
|
557
554
|
export function apply(ctx) {
|
|
558
|
-
// 捕获附件服务,供 bili_gen_qr 把二维码登记成可在 UI 渲染的图片
|
|
559
|
-
attachmentStore = ctx?.attachments ?? null;
|
|
560
555
|
for (const tool of TOOLS) {
|
|
561
556
|
const output = {
|
|
562
557
|
schema: { type: "object", additionalProperties: true },
|