dsh-sixlayer-memory 2.3.1 → 2.4.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,41 @@
1
1
  # 变更记录 · dsh-sixlayer-memory(六层记忆底座)
2
2
 
3
+ ## 2.4.0 — 2026-12-05
4
+
5
+ 主题:**说完直接发出去,不看焦点、不碰剪贴板**。
6
+
7
+ ### 新增
8
+
9
+ - **语音结果直接投递进当前会话**:`agent/pre-step` 时记下活着的 agent,识别完把文字用
10
+ `agent.followup()` 当成一条用户消息送进去——不需要点输入框、不需要光标、不依赖剪贴板。
11
+ 拿不到 agent 或投递失败才回退到"粘到输入框"。
12
+ - 配置 `asr.deliver`:`agent`(默认,直接发)/ `paste`(只粘输入框)
13
+ - 每次投递在审计里记 `op:'voice'` + `delivered: agent|paste|failed`
14
+
15
+ ### 修复
16
+
17
+ - 2.3.2 的 SendKeys 粘贴在部分窗口仍不生效——现在这条路径只是兜底,不再是主路径
18
+
19
+ ### 回归
20
+
21
+ - 全量 35 个套件全绿 · 工具 35 个
22
+
23
+ ---
24
+
25
+ ## 2.3.2 — 2026-12-05
26
+
27
+ 修「识别出来了但没粘进输入框、也没提示音」——两处都是进程环境问题。
28
+
29
+ ### 修复
30
+
31
+ - **粘贴**:`keybd_event` 从独立进程发 Ctrl+V,在 Electron 窗口里收不到(用户实测要手动按)。
32
+ 改用 `System.Windows.Forms.SendKeys::SendWait('^v')`(走"当前活动窗口"的正路),
33
+ 失败时再回退到 `keybd_event` 老路,两条都失败才报错。
34
+ - **提示音**:`[Console]::Beep` 在隐藏窗口的 PowerShell 里没有控制台,调用直接哑掉。
35
+ 改用 `System.Media.SoundPlayer` 播系统自带 wav(开始 `Windows Notify.wav`、结束 `Windows Notify Calendar.wav`)。
36
+
37
+ ---
38
+
3
39
  ## 2.3.1 — 2026-12-05
4
40
 
5
41
  修两个让「按一下说话」实际不可用的问题(2.3.0 当天发现)。
package/README.md CHANGED
@@ -11,7 +11,7 @@
11
11
  dsh plugin --profile desktop add dsh-sixlayer-memory
12
12
  ```
13
13
 
14
- **当前版本**:2.3.1 · **35 个工具** · schema v9 · 零外部依赖 · 回归 35 个套件全绿
14
+ **当前版本**:2.4.0 · **35 个工具** · schema v9 · 零外部依赖 · 回归 35 个套件全绿
15
15
 
16
16
  📦 [npm](https://www.npmjs.com/package/dsh-sixlayer-memory) · 🎬 [40 秒介绍视频](media/intro-40s.mp4) · 📝 [变更记录](CHANGELOG.md)
17
17
 
package/lib/index.js CHANGED
@@ -13,7 +13,7 @@ import { fileURLToPath } from 'node:url'
13
13
  import MemoryStore from './memory/store.js'
14
14
  import { buildRegistry } from './memory/tools.js'
15
15
  import { Injector } from './memory/inject.js'
16
- import { attachLifecycle, lifecycleSummary } from './memory/lifecycle.js'
16
+ import { attachLifecycle, lifecycleSummary, resolveMessageFactory } from './memory/lifecycle.js'
17
17
  import { decorateRows, explainRecord } from './memory/explain.js'
18
18
  import { readDetail } from './memory/detail.js'
19
19
  import { createQqBridge } from './memory/qq.js'
@@ -203,6 +203,9 @@ export function apply(ctx, config = {}) {
203
203
  })
204
204
  let lastVoice = null
205
205
  let lastHotkeyAt = 0
206
+ // pre-step 时记下活着的 agent:语音识别完直接把文字当消息投进去,不靠剪贴板
207
+ let lastAgent = null
208
+ let lastSessionId = ''
206
209
  const toggleVoice = async () => {
207
210
  // 正在录 → 停 + 转写 + 粘贴
208
211
  if (voice.recording) {
@@ -236,9 +239,32 @@ export function apply(ctx, config = {}) {
236
239
  diag('asr: 转写失败 ' + result.reason)
237
240
  return lastVoice
238
241
  }
239
- const pasted = await pasteText(result.text, { autoEnter: asrConfig.autoEnter })
242
+ // 优先直接把文字当一条用户消息送进当前会话(不看焦点、不用剪贴板);
243
+ // 拿不到 agent 或失败时才退回"粘到输入框"。
244
+ let delivered = 'none'
245
+ const factory = resolveMessageFactory()
246
+ if (asrConfig.deliver !== 'paste' && lastAgent && typeof lastAgent.followup === 'function' && factory) {
247
+ try {
248
+ lastAgent.followup(factory({ content: [{ type: 'text', text: result.text }], source: { kind: 'user' } }))
249
+ delivered = 'agent'
250
+ } catch (error) {
251
+ diag('asr: 直接投递失败(回退粘贴)' + (error?.message ?? error))
252
+ }
253
+ }
254
+ if (delivered === 'none') {
255
+ const pasted = await pasteText(result.text, { autoEnter: asrConfig.autoEnter })
256
+ delivered = pasted.ok ? 'paste' : 'failed'
257
+ }
240
258
  try {
241
- store?.recorder?.record?.({ op: 'voice', via, key_source: source, chars: result.text.length, ms: result.durationMs ?? stopped.ms })
259
+ store?.recorder?.record?.({
260
+ op: 'voice',
261
+ via,
262
+ delivered,
263
+ session: lastSessionId,
264
+ key_source: source,
265
+ chars: result.text.length,
266
+ ms: result.durationMs ?? stopped.ms,
267
+ })
242
268
  } catch {
243
269
  /* 审计失败不影响 */
244
270
  }
@@ -248,8 +274,9 @@ export function apply(ctx, config = {}) {
248
274
  stage: 'done',
249
275
  text: result.text,
250
276
  via,
277
+ delivered,
251
278
  ms: result.durationMs ?? stopped.ms,
252
- pasted: pasted.ok,
279
+ pasted: delivered !== 'failed',
253
280
  file: stopped.file,
254
281
  }
255
282
  return lastVoice
@@ -562,6 +589,10 @@ export function apply(ctx, config = {}) {
562
589
  store,
563
590
  config: config.lifecycle ?? {},
564
591
  diag,
592
+ onAgent: (agent, sessionId) => {
593
+ if (agent) lastAgent = agent
594
+ if (sessionId) lastSessionId = sessionId
595
+ },
565
596
  })
566
597
  return () => {
567
598
  try {
package/lib/memory/asr.js CHANGED
@@ -256,6 +256,8 @@ export function resolveAsrConfig(config = {}, dataDir = '') {
256
256
  localCommand: String(pick('localCommand', '')),
257
257
  device: String(pick('device', '')),
258
258
  autoEnter: Boolean(pick('autoEnter', false)),
259
+ // agent = 直接把文字当消息送进当前会话(不看焦点);paste = 粘到输入框
260
+ deliver: String(pick('deliver', 'agent')) === 'paste' ? 'paste' : 'agent',
259
261
  apiKey: config?.apiKey ? String(config.apiKey) : '',
260
262
  }
261
263
  }
@@ -263,7 +265,7 @@ export function resolveAsrConfig(config = {}, dataDir = '') {
263
265
  /** 写覆盖文件(工具改配置时用)。apiKey 不进文件。 */
264
266
  export function saveAsrConfig(dataDir, patch = {}) {
265
267
  const next = { ...readAsrOverrides(dataDir) }
266
- for (const k of ['enabled', 'baseUrl', 'localCommand', 'device', 'autoEnter']) {
268
+ for (const k of ['enabled', 'baseUrl', 'localCommand', 'device', 'autoEnter', 'deliver']) {
267
269
  if (patch[k] !== undefined) next[k] = patch[k]
268
270
  }
269
271
  const file = asrOverridesPath(dataDir)
@@ -315,14 +317,19 @@ export function saveAsrConfig(dataDir, patch = {}) {
315
317
 
316
318
  /**
317
319
  * 提示音:录音开始/结束各响一声。没有界面可看时,声音就是"我在听"的反馈。
318
- * 失败静默——没声音不该打断录音。
320
+ *
321
+ * 不用 `[Console]::Beep`:隐藏窗口的 PowerShell 没有控制台,调用直接哑掉(用户实测没声音)。
322
+ * 改用 SoundPlayer 放系统自带的 wav,任何进程都能出声。
319
323
  */
320
324
  export function beep({ freq = 880, ms = 80 } = {}) {
321
325
  try {
322
- const child = spawn('powershell.exe', ['-NoProfile', '-Command', `[Console]::Beep(${Number(freq) || 880},${Number(ms) || 80})`], {
323
- windowsHide: true,
324
- stdio: 'ignore',
325
- })
326
+ // 高音/低音用两个系统 wav 代替,避免依赖 freq(系统 wav 不支持调频,但够区分开始和结束)
327
+ const file = freq >= 700 ? 'Windows Notify.wav' : 'Windows Notify Calendar.wav'
328
+ const child = spawn(
329
+ 'powershell.exe',
330
+ ['-NoProfile', '-Command', `$p = New-Object System.Media.SoundPlayer "$env:SystemRoot\\Media\\${file}"; $p.PlaySync()`],
331
+ { windowsHide: true, stdio: 'ignore' },
332
+ )
326
333
  child.on('error', () => {})
327
334
  } catch {
328
335
  /* 忽略 */
@@ -330,28 +337,59 @@ export function beep({ freq = 880, ms = 80 } = {}) {
330
337
  }
331
338
 
332
339
  /**
333
- * 把文本送进当前焦点输入框:写剪贴板 + 模拟 Ctrl+V。
340
+ * 把文本送进当前焦点输入框:写剪贴板 + SendKeys 发 Ctrl+V。
341
+ *
342
+ * 为什么改成 SendKeys:`keybd_event` 从独立进程发键时,在部分应用(Electron 窗口)
343
+ * 里收不到,用户实测要手动按 Ctrl+V 才能粘上。SendKeys 走的是"当前活动窗口"这条正路。
344
+ *
334
345
  * 故意**不自动回车**——粘错了还能改,自动发送就救不回来了。
335
346
  */
336
347
  export function pasteText(text, { autoEnter = false } = {}) {
337
- return new Promise((resolve) => {
338
- const esc = String(text).replace(/`/g, '``').replace(/'/g, "''")
339
- const script = [
340
- `Add-Type -AssemblyName System.Windows.Forms`,
341
- `Set-Clipboard -Value '${esc}'`,
342
- `Add-Type @"`,
343
- `using System;using System.Runtime.InteropServices;`,
344
- `public class P { [DllImport("user32.dll")] public static extern void keybd_event(byte vk, byte scan, uint flags, UIntPtr extra); }`,
345
- `"@`,
346
- `[P]::keybd_event(0x11,0,0,[UIntPtr]::Zero)`,
347
- `[P]::keybd_event(0x56,0,0,[UIntPtr]::Zero)`,
348
- `Start-Sleep -Milliseconds 40`,
349
- `[P]::keybd_event(0x56,0,2,[UIntPtr]::Zero)`,
350
- `[P]::keybd_event(0x11,0,2,[UIntPtr]::Zero)`,
351
- autoEnter ? `Start-Sleep -Milliseconds 80; [P]::keybd_event(0x0D,0,0,[UIntPtr]::Zero); [P]::keybd_event(0x0D,0,2,[UIntPtr]::Zero)` : `'pasted'`,
352
- ].join('\n')
353
- const child = spawn('powershell.exe', ['-NoProfile', '-Command', script], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] })
354
- child.on('error', (error) => resolve({ ok: false, reason: error.message }))
355
- child.on('exit', (code) => resolve({ ok: code === 0, reason: code === 0 ? '' : `粘贴进程退出 ${code}` }))
356
- })
348
+ const one = String(text).replace(/\r?\n/g, ' ').replace(/'/g, "''")
349
+
350
+ const runSendKeys = () =>
351
+ new Promise((resolve) => {
352
+ const script = [
353
+ 'Add-Type -AssemblyName System.Windows.Forms',
354
+ `Set-Clipboard -Value '${one}'`,
355
+ 'Start-Sleep -Milliseconds 60',
356
+ "[System.Windows.Forms.SendKeys]::SendWait('^v')",
357
+ autoEnter ? "Start-Sleep -Milliseconds 120; [System.Windows.Forms.SendKeys]::SendWait('{ENTER}')" : "Write-Output 'pasted'",
358
+ ].join('\n')
359
+ const child = spawn('powershell.exe', ['-NoProfile', '-STA', '-Command', script], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] })
360
+ let err = ''
361
+ child.stderr.on('data', (c) => {
362
+ err += c.toString('utf8')
363
+ })
364
+ child.stdout.on('data', () => {})
365
+ child.on('error', (e) => resolve({ ok: false, reason: e.message }))
366
+ child.on('exit', (code) =>
367
+ resolve({ ok: code === 0, reason: code === 0 ? '' : `SendKeys 退出 ${code}${err ? ':' + err.trim().slice(0, 120) : ''}` }),
368
+ )
369
+ })
370
+
371
+ // 老路:keybd_event。SendKeys 在某些环境被拦时还能兜住。
372
+ const runKeybd = () =>
373
+ new Promise((resolve) => {
374
+ const script = [
375
+ 'Add-Type -AssemblyName System.Windows.Forms',
376
+ `Set-Clipboard -Value '${one}'`,
377
+ 'Add-Type @"',
378
+ 'using System;using System.Runtime.InteropServices;',
379
+ 'public class PK { [DllImport("user32.dll")] public static extern void keybd_event(byte vk, byte scan, uint flags, UIntPtr extra); }',
380
+ '"@',
381
+ 'Start-Sleep -Milliseconds 40',
382
+ '[PK]::keybd_event(0x11,0,0,[UIntPtr]::Zero)',
383
+ '[PK]::keybd_event(0x56,0,0,[UIntPtr]::Zero)',
384
+ 'Start-Sleep -Milliseconds 40',
385
+ '[PK]::keybd_event(0x56,0,2,[UIntPtr]::Zero)',
386
+ '[PK]::keybd_event(0x11,0,2,[UIntPtr]::Zero)',
387
+ autoEnter ? 'Start-Sleep -Milliseconds 100; [PK]::keybd_event(0x0D,0,0,[UIntPtr]::Zero); [PK]::keybd_event(0x0D,0,2,[UIntPtr]::Zero)' : "Write-Output 'pasted'",
388
+ ].join('\n')
389
+ const child = spawn('powershell.exe', ['-NoProfile', '-Command', script], { windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] })
390
+ child.on('error', (e) => resolve({ ok: false, reason: e.message }))
391
+ child.on('exit', (code) => resolve({ ok: code === 0, reason: code === 0 ? '' : `keybd 退出 ${code}` }))
392
+ })
393
+
394
+ return runSendKeys().then((r) => (r.ok ? r : runKeybd().then((r2) => (r2.ok ? { ok: true, via: 'keybd' } : { ok: false, reason: `${r.reason} / ${r2.reason}` }))))
357
395
  }
@@ -237,7 +237,7 @@ function deepFreeze(value) {
237
237
  * @param {any} ctx 宿主上下文
238
238
  * @param {{store:any, config?:any, diag?:(m:string)=>void, createUserMessage?:Function}} options
239
239
  */
240
- export function attachLifecycle(ctx, { store, config, diag = () => {}, createUserMessage } = {}) {
240
+ export function attachLifecycle(ctx, { store, config, diag = () => {}, createUserMessage, onAgent = null } = {}) {
241
241
  const cfg = normalizeLifecycle(config)
242
242
  if (!store) return () => {}
243
243
  if (cfg.enabled === false) {
@@ -311,6 +311,13 @@ export function attachLifecycle(ctx, { store, config, diag = () => {}, createUse
311
311
  if (!query) return decision
312
312
 
313
313
  const sessionId = String(payload?.agent?.session?.id ?? '')
314
+ // 把当前活着的 agent 交出去:语音输入要能"直接把文字当一条消息发进去",
315
+ // 而不是靠模拟 Ctrl+V 去猜用户焦点在哪个窗口。
316
+ try {
317
+ onAgent?.(payload?.agent ?? null, sessionId)
318
+ } catch (error) {
319
+ diag('lifecycle: onAgent 回调出错 ' + (error?.message ?? error))
320
+ }
314
321
  const hash = sha256(query)
315
322
  const now = Date.now()
316
323
  const cooldown = Number(cfg.recall.cooldownMs ?? 30_000)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-sixlayer-memory",
3
- "version": "2.3.1",
3
+ "version": "2.4.0",
4
4
  "private": false,
5
5
  "description": "DSH 工作台:六层记忆底座(每轮自动召回 + 候选沉淀 + 闸门审计)+ 零件化,支持 QQ 随身使用。",
6
6
  "type": "module",