koishi-plugin-luogu-saver-bot 0.1.0

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.d.ts ADDED
@@ -0,0 +1,100 @@
1
+ import { Context, Schema } from 'koishi';
2
+ export declare const name = "luogu-saver-bot";
3
+ export interface Config {
4
+ endpoint?: string;
5
+ userAgent?: string;
6
+ }
7
+ export declare const Config: Schema<Config>;
8
+ export type Article = {
9
+ id: string;
10
+ title: string;
11
+ content: string;
12
+ authorId: number;
13
+ category: number;
14
+ upvote?: number;
15
+ favorCount?: number;
16
+ solutionForPid?: string | null;
17
+ priority?: number;
18
+ deleted?: number;
19
+ tags: string[];
20
+ createdAt?: string;
21
+ updatedAt?: string;
22
+ deleteReason?: string;
23
+ contentHash?: string | null;
24
+ viewCount?: number;
25
+ renderedContent?: string | null;
26
+ };
27
+ export type StdResponse<A> = {
28
+ code: number;
29
+ message: string;
30
+ data: A;
31
+ };
32
+ export type ArticleHistory = {
33
+ id: number;
34
+ articleId: string;
35
+ version: number;
36
+ title: string;
37
+ content: string;
38
+ createdAt: string;
39
+ };
40
+ export type CountResponse = {
41
+ count: number;
42
+ };
43
+ export type TaskQuery = Record<string, any> | null;
44
+ export type Task = {
45
+ id: string;
46
+ info?: string | null;
47
+ status: 0 | 1 | 2 | 3;
48
+ createdAt: string;
49
+ type: 'save' | 'ai_process';
50
+ target?: string | null;
51
+ payload: Record<string, any>;
52
+ };
53
+ export type TaskCreateBase = {
54
+ type: string;
55
+ payload: Record<string, any>;
56
+ };
57
+ export type TaskCreateSave = TaskCreateBase & {
58
+ type: 'save';
59
+ payload: {
60
+ target: string;
61
+ targetId: string;
62
+ metadata?: Record<string, any>;
63
+ };
64
+ };
65
+ export type TaskCreateAi = TaskCreateBase & {
66
+ type: 'ai_process';
67
+ payload: {
68
+ target: string;
69
+ metadata: Record<string, any>;
70
+ };
71
+ };
72
+ export type TaskCreateResponse = {
73
+ taskId: string;
74
+ };
75
+ declare class LuoguSaverClient {
76
+ private ctx;
77
+ endpoint: string;
78
+ userAgent: string;
79
+ constructor(ctx: Context, endpoint: string, userAgent: string);
80
+ private buildUrl;
81
+ private headers;
82
+ getArticle(id: string, extraHeaders?: Record<string, string>): Promise<Article>;
83
+ getRecent(opts?: {
84
+ count?: number;
85
+ updated_after?: string;
86
+ truncated_count?: number;
87
+ }, extraHeaders?: Record<string, string>): Promise<any>;
88
+ getCount(extraHeaders?: Record<string, string>): Promise<any>;
89
+ getRelevant(id: string, extraHeaders?: Record<string, string>): Promise<any>;
90
+ getHistory(id: string, extraHeaders?: Record<string, string>): Promise<any>;
91
+ createTask(body: TaskCreateSave | TaskCreateAi, extraHeaders?: Record<string, string>): Promise<string>;
92
+ getTask(id: string, extraHeaders?: Record<string, string>): Promise<Record<string, any>>;
93
+ }
94
+ declare module 'koishi' {
95
+ interface Context {
96
+ luogu_saver: LuoguSaverClient;
97
+ }
98
+ }
99
+ export declare function apply(ctx: Context, config?: Config): void;
100
+ export {};
package/lib/index.js ADDED
@@ -0,0 +1,145 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
+ var __export = (target, all) => {
7
+ for (var name2 in all)
8
+ __defProp(target, name2, { get: all[name2], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ Config: () => Config,
24
+ apply: () => apply,
25
+ name: () => name
26
+ });
27
+ module.exports = __toCommonJS(src_exports);
28
+ var import_koishi = require("koishi");
29
+
30
+ // src/task.ts
31
+ var TaskStatus = /* @__PURE__ */ ((TaskStatus3) => {
32
+ TaskStatus3[TaskStatus3["PENDING"] = 0] = "PENDING";
33
+ TaskStatus3[TaskStatus3["PROCESSING"] = 1] = "PROCESSING";
34
+ TaskStatus3[TaskStatus3["COMPLETED"] = 2] = "COMPLETED";
35
+ TaskStatus3[TaskStatus3["FAILED"] = 3] = "FAILED";
36
+ return TaskStatus3;
37
+ })(TaskStatus || {});
38
+ function statusToString(status) {
39
+ if (status == null) return "UNKNOWN";
40
+ return TaskStatus[status] ?? "UNKNOWN";
41
+ }
42
+ __name(statusToString, "statusToString");
43
+
44
+ // src/index.ts
45
+ var name = "luogu-saver-bot";
46
+ var Config = import_koishi.Schema.object({
47
+ endpoint: import_koishi.Schema.string().description("自定义 API endpoint,结尾无需斜杠").role("input").default(""),
48
+ userAgent: import_koishi.Schema.string().description("自定义 User-Agent").role("input").default("Uptime-Kuma")
49
+ });
50
+ var LuoguSaverClient = class {
51
+ constructor(ctx, endpoint, userAgent) {
52
+ this.ctx = ctx;
53
+ this.endpoint = endpoint;
54
+ this.userAgent = userAgent;
55
+ if (!this.endpoint) this.endpoint = "";
56
+ }
57
+ static {
58
+ __name(this, "LuoguSaverClient");
59
+ }
60
+ buildUrl(path) {
61
+ const base = this.endpoint.replace(/\/$/, "");
62
+ if (!base) return path;
63
+ console.log(`${base}${path}`);
64
+ if (path.startsWith("/")) return `${base}${path}`;
65
+ return `${base}/${path}`;
66
+ }
67
+ headers(extra) {
68
+ return Object.assign({ "User-Agent": this.userAgent }, extra || {});
69
+ }
70
+ async getArticle(id, extraHeaders) {
71
+ const url = this.buildUrl(`/article/query/${encodeURIComponent(id)}`);
72
+ const res = await this.ctx.http.get(url, { headers: this.headers(extraHeaders) });
73
+ if (res.code !== 200) return null;
74
+ return res.data;
75
+ }
76
+ async getRecent(opts, extraHeaders) {
77
+ const params = new URLSearchParams();
78
+ if (opts?.count != null) params.set("count", String(opts.count));
79
+ if (opts?.updated_after) params.set("updated_after", opts.updated_after);
80
+ if (opts?.truncated_count != null) params.set("truncated_count", String(opts.truncated_count));
81
+ const path = `/article/recent${params.toString() ? `?${params.toString()}` : ""}`;
82
+ const url = this.buildUrl(path);
83
+ const res = await this.ctx.http.get(url, { headers: this.headers(extraHeaders) });
84
+ return res?.data?.data ?? null;
85
+ }
86
+ async getCount(extraHeaders) {
87
+ const url = this.buildUrl("/article/count");
88
+ const res = await this.ctx.http.get(url, { headers: this.headers(extraHeaders) });
89
+ return res?.data?.data ?? null;
90
+ }
91
+ async getRelevant(id, extraHeaders) {
92
+ const url = this.buildUrl(`/article/relevant/${encodeURIComponent(id)}`);
93
+ const res = await this.ctx.http.get(url, { headers: this.headers(extraHeaders) });
94
+ return res?.data?.data ?? null;
95
+ }
96
+ async getHistory(id, extraHeaders) {
97
+ const url = this.buildUrl(`/article/history/${encodeURIComponent(id)}`);
98
+ const res = await this.ctx.http.get(url, { headers: this.headers(extraHeaders) });
99
+ return res?.data?.data ?? null;
100
+ }
101
+ async createTask(body, extraHeaders) {
102
+ const url = this.buildUrl("/task/create");
103
+ const res = await this.ctx.http.post(url, body, { headers: this.headers(extraHeaders) });
104
+ if (res.code !== 200) return null;
105
+ return res.data.taskId;
106
+ }
107
+ async getTask(id, extraHeaders) {
108
+ const url = this.buildUrl(`/task/query/${encodeURIComponent(id)}`);
109
+ const res = await this.ctx.http.get(url, { headers: this.headers(extraHeaders) });
110
+ if (res.code !== 200) return null;
111
+ return res.data;
112
+ }
113
+ };
114
+ function apply(ctx, config = {}) {
115
+ const endpoint = config.endpoint || "";
116
+ const userAgent = config.userAgent || "Uptime-Kuma";
117
+ ctx.luogu_saver = new LuoguSaverClient(ctx, endpoint, userAgent);
118
+ ctx.command("获取文章信息 <id>", "获取文章信息").action(async ({ options }, id) => {
119
+ if (!id) return "请提供文章 ID";
120
+ const art = await ctx.luogu_saver.getArticle(id);
121
+ console.log(art);
122
+ if (!art) return "未找到文章";
123
+ return `${art.title} by ${art.authorId}`;
124
+ });
125
+ ctx.command("创建保存任务 <target> <targetId>", "创建类型为 save 的任务").action(async (_, target, targetId) => {
126
+ const body = { type: "save", payload: { target, targetId } };
127
+ const id = await ctx.luogu_saver.createTask(body);
128
+ if (!id) return "创建失败";
129
+ return `保存任务已创建,ID: ${id}`;
130
+ });
131
+ ctx.command("查询任务状态 <id>", "查询任务状态").action(async ({ options }, id) => {
132
+ if (!id) return "请提供任务 ID";
133
+ const task = await ctx.luogu_saver.getTask(id);
134
+ if (task == null) return "任务不存在或返回为空";
135
+ if (typeof task === "object" && "status" in task) return `任务 ${id} 状态: ${statusToString(task.status)}`;
136
+ return JSON.stringify(task);
137
+ });
138
+ }
139
+ __name(apply, "apply");
140
+ // Annotate the CommonJS export names for ESM import in node:
141
+ 0 && (module.exports = {
142
+ Config,
143
+ apply,
144
+ name
145
+ });
package/lib/task.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ export declare enum TaskStatus {
2
+ PENDING = 0,
3
+ PROCESSING = 1,
4
+ COMPLETED = 2,
5
+ FAILED = 3
6
+ }
7
+ export declare function statusToString(status: number | null | undefined): string;
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "koishi-plugin-luogu-saver-bot",
3
+ "description": "洛谷保存站机器人",
4
+ "version": "0.1.0",
5
+ "main": "lib/index.js",
6
+ "typings": "lib/index.d.ts",
7
+ "files": [
8
+ "lib",
9
+ "dist"
10
+ ],
11
+ "license": "MIT",
12
+ "scripts": {},
13
+ "keywords": [
14
+ "chatbot",
15
+ "koishi",
16
+ "plugin"
17
+ ],
18
+ "devDependencies": {},
19
+ "peerDependencies": {
20
+ "koishi": "^4.18.7"
21
+ }
22
+ }
package/readme.md ADDED
@@ -0,0 +1,5 @@
1
+ # koishi-plugin-luogu-saver-bot
2
+
3
+ [![npm](https://img.shields.io/npm/v/koishi-plugin-luogu-saver-bot?style=flat-square)](https://www.npmjs.com/package/koishi-plugin-luogu-saver-bot)
4
+
5
+ 洛谷保存站机器人