@mcviseron/dsh-qq-bridge 0.3.11 → 0.3.12

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.
Files changed (3) hide show
  1. package/bot/main.py +18 -0
  2. package/lib/index.js +45 -24
  3. package/package.json +1 -1
package/bot/main.py CHANGED
@@ -31,6 +31,24 @@ from viseron_qqbotpy import configure_logging, load_env, get_logger, load_panels
31
31
 
32
32
  from bridge import BridgeClient, BridgeError, download_image
33
33
 
34
+
35
+ def _ensure_std_streams() -> None:
36
+ """pythonw 启动时没有控制台,sys.stdout/stderr 是 None,print 会直接抛错。"""
37
+ if sys.stdout is not None and sys.stderr is not None:
38
+ return
39
+ try:
40
+ log_dir = Path(__file__).resolve().parent / "state"
41
+ log_dir.mkdir(parents=True, exist_ok=True)
42
+ stream = open(log_dir / "stdout.log", "a", encoding="utf-8", buffering=1)
43
+ except OSError:
44
+ stream = open(os.devnull, "w", encoding="utf-8")
45
+ if sys.stdout is None:
46
+ sys.stdout = stream
47
+ if sys.stderr is None:
48
+ sys.stderr = stream
49
+
50
+
51
+ _ensure_std_streams()
34
52
  ROOT = Path(__file__).resolve().parent
35
53
  # 机器人本身是无窗运行的;它再拉 tasklist / taskkill 时如果没带这个标志,Windows 会
36
54
  # 给它分配一个新控制台(Win11 上就是弹出的 Windows Terminal 窗口),所以统一带上。
package/lib/index.js CHANGED
@@ -114,6 +114,45 @@ export function apply(ctx, rawConfig) {
114
114
  const approvals = new Map()
115
115
  /** Sessions this process already applied the model selection to. */
116
116
  const selected = new Set()
117
+ // 每个 Session 只塞一次人设:新会话、以及 /新会话|/分组 这类由 rebind 建出来的会话
118
+ // 都算「没收到过」,否则 rebind 建完再发消息时 freshSession 已是 false,人设永远不生效。
119
+ const personaSent = new Set()
120
+
121
+ // 拉起机器人 supervisor。Windows 上必须完全没有控制台窗口:
122
+ // python.exe 是控制台程序,detached 时 CREATE_NO_WINDOW 会被忽略,用户就会看到黑窗、
123
+ // 关掉窗等于把机器人杀了。所以优先用 pythonw.exe(GUI 子系统,没有控制台可弹);
124
+ // pythonw 下 sys.stdout 是 None 会让 print 抛错,因此用 pipe 接管输出写进这里。
125
+ const spawnSupervisor = async (pythonPath, botDir) => {
126
+ const { spawn } = await import('node:child_process')
127
+ const fs = await import('node:fs')
128
+ const { join } = await import('node:path')
129
+ let exe = pythonPath
130
+ if (/python\.exe$/i.test(pythonPath)) {
131
+ const pyw = pythonPath.replace(/python\.exe$/i, 'pythonw.exe')
132
+ if (fs.existsSync(pyw)) exe = pyw
133
+ }
134
+ // 子进程的 stdout/stderr 指到一个日志文件句柄:句柄有效(pythonw 下 print 不会因
135
+ // sys.stdout 为 None 崩),父进程退出不会留下断掉的管道,而且 pythonw 没有控制台可弹。
136
+ let fd = -1
137
+ try {
138
+ fs.mkdirSync(config.dataDir, { recursive: true })
139
+ fd = fs.openSync(join(config.dataDir, 'supervisor.log'), 'a')
140
+ } catch (error) {
141
+ fd = -1
142
+ }
143
+ const child = spawn(exe, ['-u', 'main.py', '--supervise'], {
144
+ cwd: botDir,
145
+ detached: true,
146
+ windowsHide: true,
147
+ stdio: fd >= 0 ? ['ignore', fd, fd] : 'ignore',
148
+ })
149
+ if (fd >= 0) {
150
+ try { fs.closeSync(fd) } catch (error) { /* child 已继承句柄 */ }
151
+ }
152
+ child.on('error', (error) => log(`bot supervisor spawn failed: ${error.message}`))
153
+ child.unref()
154
+ return child
155
+ }
117
156
  /** Last time the bot long-polled us; approvals go to QQ only while this is fresh. */
118
157
  let lastPullAt = 0
119
158
 
@@ -128,16 +167,7 @@ export function apply(ctx, rawConfig) {
128
167
  sourceDir: join(pluginRoot, 'bot'),
129
168
  log,
130
169
  launch: async (pythonPath, botDir) => {
131
- const { spawn } = await import('node:child_process')
132
- const child = spawn(pythonPath, ['-u', 'main.py', '--supervise'], {
133
- cwd: botDir,
134
- detached: true,
135
- stdio: 'ignore',
136
- windowsHide: true,
137
- creationflags: process.platform === 'win32' ? 0x08000000 : 0,
138
- })
139
- child.on('error', (error) => log(`bot supervisor spawn failed: ${error.message}`))
140
- child.unref()
170
+ const child = await spawnSupervisor(pythonPath, botDir)
141
171
  log(`bot supervisor spawned after install (pid ${child.pid})`)
142
172
  return child.pid
143
173
  },
@@ -527,8 +557,10 @@ export function apply(ctx, rawConfig) {
527
557
  selected.add(sessionId)
528
558
  }
529
559
 
530
- // 人设随请求带过来;只有刚建的新会话才塞进提示词,之后不再重复
531
- const persona = freshSession && typeof body?.persona === 'string' ? body.persona : ''
560
+ // 人设随请求带过来;这个 Session 还没收到过就塞进提示词,之后不再重复
561
+ const wantsPersona = typeof body?.persona === 'string' && body.persona.trim() !== '' && !personaSent.has(sessionId)
562
+ if (wantsPersona) personaSent.add(sessionId)
563
+ const persona = wantsPersona ? body.persona : ''
532
564
  const content = [{ type: 'text', text: composePrompt({ text, convKey, senderName, isOwner: body?.isOwner === true, chime: body?.chime === true, quote, persona }) }]
533
565
  for (const image of images.slice(0, 4)) {
534
566
  const mediaType = typeof image?.mediaType === 'string' ? image.mediaType : ''
@@ -829,18 +861,7 @@ export function apply(ctx, rawConfig) {
829
861
  log(`bot autostart skipped: 解释器不存在 ${pythonPath}`)
830
862
  return
831
863
  }
832
- const { spawn } = await import('node:child_process')
833
- const child = spawn(pythonPath, ['-u', 'main.py', '--supervise'], {
834
- cwd: botDir,
835
- detached: true,
836
- stdio: 'ignore',
837
- windowsHide: true,
838
- // detached 会在 Windows 上给子进程一个新控制台,windowsHide 压不住它:
839
- // 用户会看到一个黑窗口,关掉它就等于把机器人杀了。CREATE_NO_WINDOW 才真正无窗。
840
- creationflags: process.platform === 'win32' ? 0x08000000 : 0,
841
- })
842
- child.on('error', (error) => log(`bot autostart failed: ${error.message}`))
843
- child.unref()
864
+ const child = await spawnSupervisor(pythonPath, botDir)
844
865
  log(`bot supervisor spawned (pid ${child.pid})`)
845
866
  } catch (error) {
846
867
  log(`bot autostart failed: ${error instanceof Error ? error.message : String(error)}`)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mcviseron/dsh-qq-bridge",
3
- "version": "0.3.11",
3
+ "version": "0.3.12",
4
4
  "description": "DeepSeek Harness plugin: bridge QQ conversations (driven by an external viseron-botpy bot) to DSH sessions — injects user messages, captures assistant replies, streams C2C answers, and relays tool approvals to the bot owner.",
5
5
  "license": "Apache-2.0",
6
6
  "type": "module",