@cmach_socket/opencode-qq-bot 0.5.1 → 0.6.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.
@@ -0,0 +1,70 @@
1
+ import type { Plugin } from "@opencode/plugin"
2
+ import { type CommandDefinition, commandTargetFor, splitArgs, tuiCommandName } from "./commands"
3
+ import type { QQBotConfig } from "./config"
4
+ import type { Logger } from "./logger"
5
+ import type { ChatOverrides } from "./overrides"
6
+ import type { QQClient } from "./naplink/client"
7
+ import type { SendRateLimiter } from "./rate-limit"
8
+ import type { SessionManager } from "./session/manager"
9
+ import type { ToolPolicy } from "./tools/policy"
10
+ import { createPlainSender } from "./tools/qq"
11
+
12
+ export { TUI_COMMAND_PREFIX, tuiCommandName } from "./commands"
13
+
14
+ export interface TuiCommandDeps {
15
+ ctx: Plugin.Context
16
+ config: QQBotConfig
17
+ logger: Logger
18
+ manager: SessionManager
19
+ policy: ToolPolicy
20
+ /** 会话行为覆盖,命令要展示各聊天的唤醒方式。 */
21
+ overrides: ChatOverrides
22
+ client: QQClient
23
+ /** 命令结果同样走限流。 */
24
+ limiter: SendRateLimiter
25
+ /** QQ 与 TUI 共用的命令表。 */
26
+ commands: readonly CommandDefinition[]
27
+ }
28
+
29
+ /**
30
+ * 把命令表以 `qq-` 前缀注册成 TUI 命令,与 QQ 侧共用同一份实现。
31
+ *
32
+ * 结果发到会话绑定的 QQ 聊天;没有绑定就发第一个配置目标。
33
+ */
34
+ export async function registerTuiCommands(
35
+ deps: TuiCommandDeps,
36
+ ): Promise<{ dispose(): Promise<void> }> {
37
+ const { ctx, config, logger, manager, policy, overrides, client, limiter, commands } = deps
38
+
39
+ const toolNames = async () => (await ctx.tool.list()).map((tool) => tool.id)
40
+
41
+ const send = createPlainSender(client, {
42
+ delayMs: config.sendDelayMs,
43
+ limiter,
44
+ logger,
45
+ failure: () => "发送 TUI 命令结果失败",
46
+ })
47
+
48
+ return await ctx.command.transform((editor) => {
49
+ for (const command of commands) {
50
+ const name = tuiCommandName(command.name)
51
+ editor.add({
52
+ name,
53
+ description: command.description,
54
+ execute: async (input) => {
55
+ const target = commandTargetFor(config, manager, input.sessionID)
56
+ if (!target) {
57
+ logger.warn(`没有可用的 QQ 目标,无法执行 /${name}`)
58
+ return
59
+ }
60
+ const args = splitArgs(input.prompt.text ?? "", name)
61
+ const body = await command.run(
62
+ { config, manager, policy, overrides, target, toolNames, commands },
63
+ args,
64
+ )
65
+ if (body !== undefined) await send(target, body)
66
+ },
67
+ })
68
+ }
69
+ })
70
+ }
package/src/version.ts ADDED
@@ -0,0 +1,12 @@
1
+ import { readFileSync } from "node:fs"
2
+
3
+ /** 读取 package.json 里的插件版本号;读取失败时返回 `unknown`。 */
4
+ export function pluginVersion(): string {
5
+ try {
6
+ const text = readFileSync(new URL("../package.json", import.meta.url), "utf8")
7
+ const pkg = JSON.parse(text) as { version?: unknown }
8
+ return typeof pkg.version === "string" && pkg.version ? pkg.version : "unknown"
9
+ } catch {
10
+ return "unknown"
11
+ }
12
+ }