@linjz/opencode-qqbot 0.3.0 → 0.3.2

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/README.md CHANGED
@@ -71,6 +71,8 @@ openqq # npm 全局安装后
71
71
 
72
72
  Windows 常驻:`deploy/` 目录内有任务计划(开机自启、看门狗心跳守护)脚本与说明。
73
73
 
74
+ > **跨平台**:bot 本体零 OS 分支(纯 TS/Bun + HTTP/WS,与 QQ 官方 SDK 同形态)。Linux/macOS 常驻用 `systemd --user` 或 `nohup bun run src/index.ts &` 即可;端口自动发现在三个系统都可用(Linux 走 `ss -tln`,macOS 走 `netstat -an -p tcp`,Windows 走 `netstat -ano -p tcp`)。
75
+
74
76
  ## 指令一览
75
77
 
76
78
  | 指令 | 说明 |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@linjz/opencode-qqbot",
3
- "version": "0.3.0",
3
+ "version": "0.3.2",
4
4
  "type": "module",
5
5
  "description": "QQ Bot for OpenCode AI - chat with AI coding assistant via QQ",
6
6
  "bin": {
@@ -20,7 +20,13 @@
20
20
  "src",
21
21
  "tsconfig.json"
22
22
  ],
23
- "keywords": ["qq", "bot", "opencode", "ai", "coding-assistant"],
23
+ "keywords": [
24
+ "qq",
25
+ "bot",
26
+ "opencode",
27
+ "ai",
28
+ "coding-assistant"
29
+ ],
24
30
  "license": "MIT",
25
31
  "repository": {
26
32
  "type": "git",
@@ -40,31 +40,49 @@ export function isOpencodeInfo(body: unknown): boolean {
40
40
  )
41
41
  }
42
42
 
43
- // netstat 输出 → 本机监听口列表(去重,保持首次出现序)。
44
- // 兼容 IPv4(127.0.0.1:P / 0.0.0.0:P)、IPv6([::1]:P / [::]:P)与 TCPv6 行;
45
- // 非监听态(ESTABLISHED/TIME_WAIT/…)与表头噪声一律忽略。
43
+ // netstat/ss 输出 → 本机监听口列表(去重,保持首次出现序)。
44
+ // 跨平台兼容三形态(QQ 官方插件纯 TS 零 OS 分支为参照——我们也要不挑系统):
45
+ // Windows: TCP 127.0.0.1:28894 0.0.0.0:0 LISTENING 44152
46
+ // Linux ss: LISTEN 0 128 127.0.0.1:28894 0.0.0.0:*
47
+ // macOS: tcp4 0 0 127.0.0.1.28894 *.* LISTEN(注意 macOS 是点分隔)
48
+ // 规则:LISTEN(ING) 行内,取含 : 或 . 分隔的地址列,末段数字即端口;
49
+ // 裸 IP 末段可能误当端口(如 127.0.0.1 → 1),无害——探测时签名不符即跳过。
46
50
  export function parseListenPorts(output: string): number[] {
47
51
  const ports = new Set<number>()
48
52
  for (const raw of output.split(/\r?\n/)) {
49
53
  const line = raw.trim()
50
- if (!/^(TCP|TCPv6)\s/i.test(line) || !/LISTENING/i.test(line)) continue
51
- const cols = line.split(/\s+/)
52
- if (cols.length < 4) continue
53
- const local = cols[1] ?? ""
54
- const port = Number(local.slice(local.lastIndexOf(":") + 1))
55
- if (Number.isInteger(port) && port > 0 && port < 65536) ports.add(port)
54
+ if (!/\bLISTEN(?:ING)?\b/i.test(line)) continue
55
+ for (const col of line.split(/\s+/)) {
56
+ const m = /^[\w.[\]:%*]*[:.](\d+)$/.exec(col)
57
+ if (!m) continue
58
+ const port = Number(m[1])
59
+ if (Number.isInteger(port) && port > 0 && port < 65536) ports.add(port)
60
+ }
56
61
  }
57
62
  return [...ports]
58
63
  }
59
64
 
65
+ // 跨平台监听口枚举:Windows=netstat -ano -p tcp;Linux=ss -tln(兜底 netstat -tln);
66
+ // macOS=netstat -an -p tcp(兜底 ss)。命令失败→换备选→全灭则记日志返回空。
60
67
  async function listListenPorts(): Promise<number[]> {
61
- try {
62
- const { stdout } = await execAsync("netstat -ano -p tcp", { maxBuffer: 10 * 1024 * 1024, timeout: 10_000 })
63
- return parseListenPorts(stdout)
64
- } catch (error) {
65
- console.log(`[discovery] netstat 不可用,跳过端口扫描: ${error instanceof Error ? error.message : error}`)
66
- return []
68
+ const platform = process.platform
69
+ const candidates: string[] =
70
+ platform === "win32"
71
+ ? ["netstat -ano -p tcp"]
72
+ : platform === "darwin"
73
+ ? ["netstat -an -p tcp", "ss -tln"]
74
+ : ["ss -tln", "netstat -tln"]
75
+ for (const cmd of candidates) {
76
+ try {
77
+ const { stdout } = await execAsync(cmd, { maxBuffer: 10 * 1024 * 1024, timeout: 10_000 })
78
+ const ports = parseListenPorts(stdout)
79
+ if (ports.length > 0) return ports
80
+ } catch (error) {
81
+ console.log(`[discovery] ${cmd.split(" ")[0]} 不可用: ${error instanceof Error ? error.message : error}`)
82
+ }
67
83
  }
84
+ console.log("[discovery] 监听口枚举工具全部不可用,跳过端口扫描")
85
+ return []
68
86
  }
69
87
 
70
88
  // 探测单个 /api/info URL;命中签名返回版本号,否则 null
package/src/qq/api.ts CHANGED
@@ -5,6 +5,11 @@ import type { MessageContext } from "./types.js"
5
5
 
6
6
  const API_BASE = "https://api.sgroup.qq.com"
7
7
  const TOKEN_URL = "https://bots.qq.com/app/getAppAccessToken"
8
+ // 沙箱模式(QQ_SANDBOX=true):API 底座切沙箱域(QQ 官方文档约定
9
+ // sandbox.api.sgroup.qq.com);token 端点两环境共用 bots.qq.com。
10
+ // 注意:沙箱 bot 在非沙箱场景会被网关以 4914(offline/sandbox-only)拒绝。
11
+ const API_BASE_SANDBOX = "https://sandbox.api.sgroup.qq.com"
12
+ const API_BASE_EFFECTIVE = process.env.QQ_SANDBOX === "true" ? API_BASE_SANDBOX : API_BASE
8
13
 
9
14
  let cachedToken: { token: string; expiresAt: number; appId: string } | null = null
10
15
  // Singleflight:防止并发获取 Token 时重复请求
@@ -152,7 +157,7 @@ export async function apiRequest<T = unknown>(
152
157
  body?: unknown,
153
158
  timeoutMs?: number,
154
159
  ): Promise<T> {
155
- const url = `${API_BASE}${path}`
160
+ const url = `${API_BASE_EFFECTIVE}${path}`
156
161
  const headers: Record<string, string> = {
157
162
  Authorization: `QQBot ${accessToken}`,
158
163
  "Content-Type": "application/json",