@cmach_socket/opencode-qq-bot 2.0.0 → 2.0.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cmach_socket/opencode-qq-bot",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "OpenCode V2 plugin bridging a QQ group chat (NapCat via NapLink) to an OpenCode session.",
5
5
  "repository": {
6
6
  "type": "git",
package/src/home.ts CHANGED
@@ -3,12 +3,6 @@ import { homedir } from "node:os"
3
3
  import { isAbsolute, join } from "node:path"
4
4
  import type { Logger } from "./logger"
5
5
 
6
- /**
7
- * 机器人固定服务的工作目录。
8
- *
9
- * 工作目录不再可配置:`config/` 自己就放在这个目录里,位置固定才不会自举。
10
- */
11
-
12
6
  /** 机器人固定服务的工作目录。 */
13
7
  export interface BotHome {
14
8
  /** 工作目录:配置、日记、全局媒体与各聊天目录都在这里。 */
@@ -17,21 +11,32 @@ export interface BotHome {
17
11
  lockPath?: string
18
12
  }
19
13
 
20
- /** 默认工作目录:`$XDG_DATA_HOME/opencode-qq-bot`。 */
14
+ /**
15
+ * 默认工作目录:`$XDG_DATA_HOME/opencode-qq-bot`。
16
+ *
17
+ * 目录固定且不可配置:`config/` 就在这个目录里,位置固定才不会自举。
18
+ */
21
19
  export function defaultHome(): string {
22
20
  const data = process.env.XDG_DATA_HOME
23
21
  const base = data && isAbsolute(data) ? data : join(homedir(), ".local", "share")
24
22
  return join(base, "opencode-qq-bot")
25
23
  }
26
24
 
25
+ /** 启动时就建好的基础目录:配置、角色设定、日记、全局媒体与各聊天目录。 */
26
+ const BASE_DIRS = ["config", "souls", "diary", "media", "chats"] as const
27
+
27
28
  /**
28
29
  * 解析机器人工作目录并确保它存在。
29
30
  *
30
- * 目录固定在 XDG 数据目录下的工作区,启动时自动创建。
31
+ * 目录固定在 XDG 数据目录下,启动时把基础目录一并建好,
32
+ * 免得第一次用到某个目录时才临时创建。
31
33
  */
32
34
  export async function resolveHome(logger: Logger): Promise<BotHome> {
33
35
  const directory = defaultHome()
34
36
  await mkdir(directory, { recursive: true, mode: 0o700 })
37
+ await Promise.all(
38
+ BASE_DIRS.map((name) => mkdir(join(directory, name), { recursive: true, mode: 0o700 })),
39
+ )
35
40
  // 写锁和数据目录里的其它状态放在一起,不另占 XDG 根目录。
36
41
  const lockPath = join(directory, "state.lock")
37
42
  logger.debug(`机器人工作目录:${directory}`)
@@ -1,3 +1,4 @@
1
+ import { mkdirSync } from "node:fs"
1
2
  import { resolve } from "node:path"
2
3
  import type { Plugin } from "@opencode/plugin"
3
4
  import type { Location } from "@opencode/schema"
@@ -489,6 +490,11 @@ export class SessionManager {
489
490
  ) {}
490
491
 
491
492
  /** 每个聊天的会话各自落在自己的工作目录里。 */
493
+ /** 聊天目录是会话的工作目录,创建会话之前必须先落到磁盘上。 */
494
+ private ensureChatDirectory(target: ChatTarget): void {
495
+ mkdirSync(this.locationFor(target).directory as string, { recursive: true, mode: 0o700 })
496
+ }
497
+
492
498
  private locationFor(target: ChatTarget): Location.PublicRef {
493
499
  const { directory } = chatWorkspace(this.home.directory, target)
494
500
  // 目录由聊天目标拼出来,这里只是把字符串还原成品牌类型。
@@ -2142,6 +2148,7 @@ export class SessionManager {
2142
2148
 
2143
2149
  private async resolveSession(target: ChatTarget): Promise<string | undefined> {
2144
2150
  await this.ensureBindings()
2151
+ this.ensureChatDirectory(target)
2145
2152
  const state = this.state(target)
2146
2153
  const label = targetLabel(target)
2147
2154
  // 覆盖优先于全局配置;没写覆盖就用全局的。
@@ -2243,6 +2250,7 @@ export class SessionManager {
2243
2250
  target: ChatTarget,
2244
2251
  exclude?: string,
2245
2252
  ): Promise<ListedSession[]> {
2253
+ this.ensureChatDirectory(target)
2246
2254
  const byMetadata = (session: ListedSession) => {
2247
2255
  const metadata = session.metadata
2248
2256
  if (!metadata) return false
@@ -2353,6 +2361,7 @@ export class SessionManager {
2353
2361
 
2354
2362
  /** 会话是否还在这个聊天的工作目录里;直查口径不一致时的兜底判断。 */
2355
2363
  private async sessionInDirectory(target: ChatTarget, sessionID: string): Promise<boolean> {
2364
+ this.ensureChatDirectory(target)
2356
2365
  const found = await findSessions(
2357
2366
  this.ctx,
2358
2367
  this.locationFor(target).directory,