koishi-plugin-dynamic-bot 0.0.0-dev
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/AGENTS.md +6 -0
- package/LICENSE +201 -0
- package/package.json +33 -0
- package/src/dbk/avatar.ts +58 -0
- package/src/dbk/bots.ts +261 -0
- package/src/dbk/codec.ts +55 -0
- package/src/dbk/error.ts +11 -0
- package/src/dbk/gateway.ts +155 -0
- package/src/dbk/get-target.ts +275 -0
- package/src/dbk/handlers.ts +29 -0
- package/src/dbk/list-targets.ts +459 -0
- package/src/dbk/protocol.ts +100 -0
- package/src/dbk/recall.ts +222 -0
- package/src/dbk/send.ts +398 -0
- package/src/dbk/session.ts +239 -0
- package/src/dbk/socket.ts +52 -0
- package/src/dbk/version.ts +65 -0
- package/src/gen/dbk/v1/common_pb.ts +238 -0
- package/src/gen/dbk/v1/frame_pb.ts +114 -0
- package/src/gen/dbk/v1/rpc_pb.ts +868 -0
- package/src/index.ts +104 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Context, Schema } from "koishi";
|
|
2
|
+
import type {} from "@koishijs/plugin-http";
|
|
3
|
+
import type {} from "@koishijs/plugin-server";
|
|
4
|
+
import { DbkGateway, watchBots } from "./dbk/gateway";
|
|
5
|
+
import { createGatewayHandlers } from "./dbk/handlers";
|
|
6
|
+
|
|
7
|
+
export const name = "dynamic-bot";
|
|
8
|
+
|
|
9
|
+
export const reusable = true;
|
|
10
|
+
|
|
11
|
+
export const inject = ["server", "http"];
|
|
12
|
+
|
|
13
|
+
export enum WebsocketType {
|
|
14
|
+
SERVER = "server",
|
|
15
|
+
CLIENT = "client",
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface Config {
|
|
19
|
+
websocketType: WebsocketType;
|
|
20
|
+
accessToken: string;
|
|
21
|
+
path?: string;
|
|
22
|
+
host?: string;
|
|
23
|
+
port?: number;
|
|
24
|
+
reconnect?: boolean;
|
|
25
|
+
dev: boolean;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const Config: Schema<Config> = Schema.intersect([
|
|
29
|
+
Schema.object({
|
|
30
|
+
websocketType: Schema.union([
|
|
31
|
+
Schema.const(WebsocketType.SERVER).description(
|
|
32
|
+
"正向连接(dynamic-bot 连接本插件,复用 Koishi HTTP 服务)",
|
|
33
|
+
),
|
|
34
|
+
Schema.const(WebsocketType.CLIENT).description(
|
|
35
|
+
"反向连接(本插件连接 dynamic-bot)",
|
|
36
|
+
),
|
|
37
|
+
])
|
|
38
|
+
.description("连接模式")
|
|
39
|
+
.required(),
|
|
40
|
+
accessToken: Schema.string()
|
|
41
|
+
.role("secret")
|
|
42
|
+
.required()
|
|
43
|
+
.description("与 dynamic-bot 插件共享的访问令牌"),
|
|
44
|
+
dev: Schema.boolean()
|
|
45
|
+
.default(false)
|
|
46
|
+
.description("使用 protojson 文本帧(仅本地调试)"),
|
|
47
|
+
}).description("动态机器人连接配置"),
|
|
48
|
+
Schema.union([
|
|
49
|
+
Schema.object({
|
|
50
|
+
websocketType: Schema.const(WebsocketType.SERVER).required(),
|
|
51
|
+
path: Schema.string()
|
|
52
|
+
.default("/dbk")
|
|
53
|
+
.description(
|
|
54
|
+
"挂载在 Koishi HTTP 服务上的 WebSocket 路径,无需单独开端口",
|
|
55
|
+
),
|
|
56
|
+
}).description("正向连接配置"),
|
|
57
|
+
Schema.object({
|
|
58
|
+
websocketType: Schema.const(WebsocketType.CLIENT).required(),
|
|
59
|
+
host: Schema.string()
|
|
60
|
+
.default("127.0.0.1")
|
|
61
|
+
.description("dynamic-bot 反向 WebSocket 监听地址"),
|
|
62
|
+
port: Schema.number()
|
|
63
|
+
.min(1)
|
|
64
|
+
.max(65535)
|
|
65
|
+
.default(9800)
|
|
66
|
+
.description("dynamic-bot 反向 WebSocket 端口"),
|
|
67
|
+
reconnect: Schema.boolean().default(true).description("断开后自动重连"),
|
|
68
|
+
}).description("反向连接配置"),
|
|
69
|
+
]),
|
|
70
|
+
]);
|
|
71
|
+
|
|
72
|
+
export function apply(ctx: Context, config: Config) {
|
|
73
|
+
const gateway = new DbkGateway(
|
|
74
|
+
ctx,
|
|
75
|
+
{
|
|
76
|
+
encoding: config.dev ? "json" : "binary",
|
|
77
|
+
accessToken: config.accessToken ?? "",
|
|
78
|
+
path: config.path ?? "/dbk",
|
|
79
|
+
host: config.host ?? "127.0.0.1",
|
|
80
|
+
port: config.port ?? 9800,
|
|
81
|
+
reconnect: config.reconnect ?? true,
|
|
82
|
+
},
|
|
83
|
+
createGatewayHandlers(ctx),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
if (config.websocketType === WebsocketType.CLIENT) {
|
|
87
|
+
gateway.startReverse();
|
|
88
|
+
} else {
|
|
89
|
+
gateway.startForward();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
watchBots(ctx, gateway);
|
|
93
|
+
ctx.on("dispose", () => gateway.stop());
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { DbkEvent, DbkMethod, PROTOCOL_VERSION } from "./dbk/protocol";
|
|
97
|
+
export type {
|
|
98
|
+
DbkEncoding,
|
|
99
|
+
DbkEventMap,
|
|
100
|
+
DbkGatewayHandlers,
|
|
101
|
+
DbkRpcMap,
|
|
102
|
+
} from "./dbk/protocol";
|
|
103
|
+
export { DbkRpcError } from "./dbk/error";
|
|
104
|
+
export { DbkGateway } from "./dbk/gateway";
|