dsh-sixlayer-memory 2.1.0 → 2.3.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 +59 -0
- package/README.md +1 -1
- package/lib/index.js +120 -0
- package/lib/memory/asr.js +341 -0
- package/lib/memory/hotkey-helper.ps1 +74 -0
- package/lib/memory/hotkey.js +278 -0
- package/lib/memory/tools.js +161 -0
- package/package.json +3 -1
- package/tools/bump-market-version.mjs +100 -0
- package/tools/monitor.mjs +29 -0
- package/tools/p31.mjs +107 -0
- package/tools/p32.mjs +122 -0
package/tools/p31.mjs
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/** p31:全局热键(解析 + 真实注册 + 冲突检测) */
|
|
2
|
+
import { execFileSync } from 'node:child_process'
|
|
3
|
+
import { HotkeyListener, parseHotkey } from '../lib/memory/hotkey.js'
|
|
4
|
+
|
|
5
|
+
let pass = 0
|
|
6
|
+
let fail = 0
|
|
7
|
+
function ok(name, cond, extra = '') {
|
|
8
|
+
if (cond) { pass += 1; console.log(' ok ' + name) } else { fail += 1; console.log(' FAIL ' + name + (extra ? ' :: ' + extra : '')) }
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const sleep = (ms) => new Promise((r) => setTimeout(r, ms))
|
|
12
|
+
|
|
13
|
+
// ---------- 1. 解析 ----------
|
|
14
|
+
const a = parseHotkey('Alt+V')
|
|
15
|
+
ok('解析:Alt+V → mods 1 / vk 0x56', a.ok && a.mods === 1 && a.vk === 0x56 && a.label === 'Alt+V', JSON.stringify(a))
|
|
16
|
+
const b = parseHotkey('Ctrl+Shift+F9')
|
|
17
|
+
ok('解析:Ctrl+Shift+F9 → mods 6 / vk 0x78', b.ok && b.mods === 6 && b.vk === 0x78 && b.label === 'Ctrl+Shift+F9', JSON.stringify(b))
|
|
18
|
+
const c = parseHotkey('F8')
|
|
19
|
+
ok('解析:裸键 F8 被拒(会抢走正常输入)', c.ok === false && /修饰键/.test(c.reason), JSON.stringify(c))
|
|
20
|
+
const d = parseHotkey('V')
|
|
21
|
+
ok('解析:裸字母被拒', d.ok === false, JSON.stringify(d))
|
|
22
|
+
const e = parseHotkey('Alt+Space')
|
|
23
|
+
ok('解析:Alt+Space → vk 0x20', e.ok && e.vk === 0x20, JSON.stringify(e))
|
|
24
|
+
const f = parseHotkey('alt+v')
|
|
25
|
+
ok('解析:大小写不敏感', f.ok && f.label === 'Alt+V', JSON.stringify(f))
|
|
26
|
+
const g = parseHotkey('Ctrl+Alt+Shift+Win+F12')
|
|
27
|
+
ok('解析:四修饰键全叠 → mods 15', g.ok && g.mods === 15, JSON.stringify(g))
|
|
28
|
+
const h = parseHotkey('Alt+Q+Z')
|
|
29
|
+
ok('解析:中间键不是修饰键时报错', h.ok === false && /修饰键/.test(h.reason), JSON.stringify(h))
|
|
30
|
+
ok('解析:空串报错', parseHotkey('').ok === false)
|
|
31
|
+
|
|
32
|
+
// ---------- 2. 真实注册 + 按键 ----------
|
|
33
|
+
function pressCombo(mods, vk) {
|
|
34
|
+
// 用 PowerShell 合成按键,避免依赖原生模块
|
|
35
|
+
const keys = []
|
|
36
|
+
if (mods & 2) keys.push(0x11) // Ctrl
|
|
37
|
+
if (mods & 1) keys.push(0x12) // Alt
|
|
38
|
+
if (mods & 4) keys.push(0x10) // Shift
|
|
39
|
+
if (mods & 8) keys.push(0x5B) // Win
|
|
40
|
+
const downs = [...keys, vk].map((k) => `[K]::keybd_event(${k},0,0,[UIntPtr]::Zero)`).join(';')
|
|
41
|
+
const ups = [vk, ...[...keys].reverse()].map((k) => `[K]::keybd_event(${k},0,2,[UIntPtr]::Zero)`).join(';')
|
|
42
|
+
const script = `
|
|
43
|
+
Add-Type @"
|
|
44
|
+
using System;using System.Runtime.InteropServices;
|
|
45
|
+
public class K { [DllImport("user32.dll")] public static extern void keybd_event(byte vk, byte scan, uint flags, UIntPtr extra); }
|
|
46
|
+
"@
|
|
47
|
+
${downs}; Start-Sleep -Milliseconds 60; ${ups}
|
|
48
|
+
`
|
|
49
|
+
execFileSync('powershell.exe', ['-NoProfile', '-Command', script], { windowsHide: true, timeout: 15000 })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const events = []
|
|
53
|
+
const listener = new HotkeyListener({ key: 'Ctrl+Alt+Shift+F11', action: 'none', onEvent: (ev) => events.push(ev) })
|
|
54
|
+
const started = listener.start()
|
|
55
|
+
ok('启动:返回 ok', started.ok === true, JSON.stringify(started))
|
|
56
|
+
|
|
57
|
+
for (let i = 0; i < 40 && listener.state !== 'ready'; i += 1) await sleep(250)
|
|
58
|
+
ok('启动:进入 ready', listener.state === 'ready', `${listener.state} / ${listener.lastError}`)
|
|
59
|
+
|
|
60
|
+
if (listener.state === 'ready') {
|
|
61
|
+
pressCombo(2 | 1 | 4, 0x7a) // Ctrl+Alt+Shift+F11
|
|
62
|
+
for (let i = 0; i < 20 && listener.presses === 0; i += 1) await sleep(150)
|
|
63
|
+
ok('按键:收到一次 press', listener.presses === 1, String(listener.presses))
|
|
64
|
+
ok('按键:回调收到 press 事件', events.some((x) => x.type === 'press'), JSON.stringify(events.map((x) => x.type)))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// ---------- 3. 冲突 ----------
|
|
68
|
+
if (listener.state === 'ready') {
|
|
69
|
+
const second = new HotkeyListener({ key: 'Ctrl+Alt+Shift+F11', action: 'none' })
|
|
70
|
+
second.start()
|
|
71
|
+
for (let i = 0; i < 30 && second.state !== 'error'; i += 1) await sleep(200)
|
|
72
|
+
ok('冲突:同一个键第二次注册会被拒', second.state === 'error' && /占用/.test(second.lastError), `${second.state} / ${second.lastError}`)
|
|
73
|
+
second.stop()
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
listener.stop()
|
|
77
|
+
ok('停止:状态变 stopped', listener.state === 'stopped', listener.state)
|
|
78
|
+
ok('状态:status() 字段齐全', ['hotkey', 'action', 'state', 'presses', 'error'].every((k) => k in listener.status()), JSON.stringify(listener.status()))
|
|
79
|
+
|
|
80
|
+
// ---------- 4. 非法热键不抛 ----------
|
|
81
|
+
const bad = new HotkeyListener({ key: 'V', action: 'none' })
|
|
82
|
+
const badStart = bad.start()
|
|
83
|
+
ok('非法热键:start 返回 ok:false 而不是抛异常', badStart.ok === false && Boolean(badStart.reason), JSON.stringify(badStart))
|
|
84
|
+
|
|
85
|
+
// ---------- 5. 配置三层合并 ----------
|
|
86
|
+
import fs from 'node:fs'
|
|
87
|
+
import os from 'node:os'
|
|
88
|
+
import path from 'node:path'
|
|
89
|
+
import { resolveHotkeyConfig, saveHotkeyConfig, overridesPath } from '../lib/memory/hotkey.js'
|
|
90
|
+
|
|
91
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'p31-cfg-'))
|
|
92
|
+
ok('配置:默认关闭', resolveHotkeyConfig({}, tmp).enabled === false)
|
|
93
|
+
ok('配置:默认键 Alt+V', resolveHotkeyConfig({}, tmp).key === 'Alt+V')
|
|
94
|
+
ok('配置:默认动作 dictation', resolveHotkeyConfig({}, tmp).action === 'dictation')
|
|
95
|
+
|
|
96
|
+
saveHotkeyConfig(tmp, { key: 'Ctrl+Shift+F9', enabled: true })
|
|
97
|
+
const after = resolveHotkeyConfig({}, tmp)
|
|
98
|
+
ok('配置:写入后能读回', after.key === 'Ctrl+Shift+F9' && after.enabled === true, JSON.stringify(after))
|
|
99
|
+
ok('配置:覆盖文件落在 meta/hotkey.json', fs.existsSync(overridesPath(tmp)) && overridesPath(tmp).endsWith(path.join('meta', 'hotkey.json')))
|
|
100
|
+
|
|
101
|
+
const cordisWins = resolveHotkeyConfig({ key: 'Alt+F2' }, tmp)
|
|
102
|
+
ok('配置:cordis 配置优先于覆盖文件', cordisWins.key === 'Alt+F2', JSON.stringify(cordisWins))
|
|
103
|
+
ok('配置:非法 action 归一到 dictation', resolveHotkeyConfig({ action: 'whatever' }, tmp).action === 'dictation')
|
|
104
|
+
fs.rmSync(tmp, { recursive: true, force: true })
|
|
105
|
+
|
|
106
|
+
console.log(`\nP31 ${fail === 0 ? 'OK' : 'FAIL'} pass=${pass} fail=${fail}`)
|
|
107
|
+
process.exit(fail === 0 ? 0 : 1)
|
package/tools/p32.mjs
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/** p32:语音输入(DeepSeek ASR)—— 设备解析 / key 来源 / 真实转写 / 本地保底 */
|
|
2
|
+
import { execFileSync } from 'node:child_process'
|
|
3
|
+
import fs from 'node:fs'
|
|
4
|
+
import os from 'node:os'
|
|
5
|
+
import path from 'node:path'
|
|
6
|
+
import {
|
|
7
|
+
VoiceRecorder,
|
|
8
|
+
listAudioDevices,
|
|
9
|
+
loadDeepSeekKey,
|
|
10
|
+
parseAudioDevices,
|
|
11
|
+
resolveAsrConfig,
|
|
12
|
+
saveAsrConfig,
|
|
13
|
+
transcribe,
|
|
14
|
+
transcribeLocal,
|
|
15
|
+
} from '../lib/memory/asr.js'
|
|
16
|
+
|
|
17
|
+
let pass = 0
|
|
18
|
+
let fail = 0
|
|
19
|
+
function ok(name, cond, extra = '') {
|
|
20
|
+
if (cond) { pass += 1; console.log(' ok ' + name) } else { fail += 1; console.log(' FAIL ' + name + (extra ? ' :: ' + extra : '')) }
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// ---------- 1. 设备解析 ----------
|
|
24
|
+
const sample = [
|
|
25
|
+
'[dshow @ 000] "本机麦克风 (适用于数字麦克风的英特尔® 智音技术)" (audio)',
|
|
26
|
+
'[dshow @ 000] Alternative name "@device_cm_{...}"',
|
|
27
|
+
'[dshow @ 000] "OBS Virtual Camera" (video)',
|
|
28
|
+
'[dshow @ 000] "麦克风 (USB Audio)" (audio)',
|
|
29
|
+
].join('\r\n')
|
|
30
|
+
const devs = parseAudioDevices(sample)
|
|
31
|
+
ok('解析:只取 audio 设备', devs.length === 2 && devs[0].includes('本机麦克风'), JSON.stringify(devs))
|
|
32
|
+
ok('解析:video 设备不算', !devs.some((d) => d.includes('OBS')))
|
|
33
|
+
ok('解析:空输出返回空数组', parseAudioDevices('').length === 0)
|
|
34
|
+
|
|
35
|
+
// ---------- 2. key 来源 ----------
|
|
36
|
+
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'p32-'))
|
|
37
|
+
const home = path.join(tmp, 'home')
|
|
38
|
+
fs.mkdirSync(home, { recursive: true })
|
|
39
|
+
fs.writeFileSync(path.join(home, '.credentials.yaml'), 'version: 1\nrefs:\n DEEPSEEK_API_KEY: sk-test-123\n', 'utf8')
|
|
40
|
+
ok('key:配置优先', loadDeepSeekKey({ config: { apiKey: 'sk-cfg' }, env: { DSH_HOME: home } }).key === 'sk-cfg')
|
|
41
|
+
ok('key:环境变量次之', loadDeepSeekKey({ config: {}, env: { DSH_HOME: home, DEEPSEEK_API_KEY: 'sk-env' } }).key === 'sk-env')
|
|
42
|
+
const fromCred = loadDeepSeekKey({ config: {}, env: { DSH_HOME: home } })
|
|
43
|
+
ok('key:从 credentials 的 refs 读', fromCred.key === 'sk-test-123' && fromCred.source === 'credentials', JSON.stringify(fromCred))
|
|
44
|
+
ok('key:都没有时返回空', loadDeepSeekKey({ config: {}, env: { DSH_HOME: path.join(tmp, 'nope') } }).key === '')
|
|
45
|
+
|
|
46
|
+
// ---------- 3. 配置三层 ----------
|
|
47
|
+
ok('配置:默认关闭', resolveAsrConfig({}, tmp).enabled === false)
|
|
48
|
+
saveAsrConfig(tmp, { enabled: true, localCommand: 'whisper {file}' })
|
|
49
|
+
const cfg = resolveAsrConfig({}, tmp)
|
|
50
|
+
ok('配置:写入后读回', cfg.enabled === true && cfg.localCommand === 'whisper {file}', JSON.stringify(cfg))
|
|
51
|
+
ok('配置:apiKey 不落盘', !JSON.stringify(fs.readFileSync(path.join(tmp, 'meta', 'asr.json'), 'utf8')).includes('sk-'))
|
|
52
|
+
ok('配置:cordis 优先', resolveAsrConfig({ baseUrl: 'http://x' }, tmp).baseUrl === 'http://x')
|
|
53
|
+
|
|
54
|
+
// ---------- 4. 真机:列设备 + 录 2 秒 ----------
|
|
55
|
+
const devices = await listAudioDevices()
|
|
56
|
+
ok('真机:至少一个麦克风', devices.length >= 1, JSON.stringify(devices))
|
|
57
|
+
|
|
58
|
+
if (devices.length > 0) {
|
|
59
|
+
const rec = new VoiceRecorder({ outDir: path.join(tmp, 'voice'), device: devices[0] })
|
|
60
|
+
const started = await rec.start()
|
|
61
|
+
ok('录音:开始成功', started.ok === true, JSON.stringify(started))
|
|
62
|
+
await new Promise((r) => setTimeout(r, 2000))
|
|
63
|
+
const stopped = await rec.stop()
|
|
64
|
+
ok('录音:停止后有文件', stopped.ok === true && fs.existsSync(stopped.file), JSON.stringify(stopped))
|
|
65
|
+
if (stopped.ok) {
|
|
66
|
+
const size = fs.statSync(stopped.file).size
|
|
67
|
+
ok('录音:文件大小合理(>16KB)', size > 16000, `${size} 字节`)
|
|
68
|
+
const again = await rec.start()
|
|
69
|
+
ok('录音:停止后能再开', again.ok === true, JSON.stringify(again))
|
|
70
|
+
await new Promise((r) => setTimeout(r, 300))
|
|
71
|
+
await rec.stop()
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ---------- 5. 真实转写(SAPI 合成一句中文) ----------
|
|
76
|
+
const wav = path.join(tmp, 'speech.wav')
|
|
77
|
+
try {
|
|
78
|
+
execFileSync(
|
|
79
|
+
'powershell.exe',
|
|
80
|
+
['-NoProfile', '-Command', `
|
|
81
|
+
Add-Type -AssemblyName System.Speech
|
|
82
|
+
$s = New-Object System.Speech.Synthesis.SpeechSynthesizer
|
|
83
|
+
$v = $s.GetInstalledVoices() | Where-Object { $_.VoiceInfo.Culture.Name -like 'zh*' } | Select-Object -First 1
|
|
84
|
+
if (-not $v) { exit 2 }
|
|
85
|
+
$s.SelectVoice($v.VoiceInfo.Name)
|
|
86
|
+
$s.SetOutputToWaveFile('${wav.replace(/\\/g, '\\\\')}')
|
|
87
|
+
$s.Speak('帮我看一下后室第三关怎么走')
|
|
88
|
+
$s.Dispose()
|
|
89
|
+
`],
|
|
90
|
+
{ windowsHide: true, timeout: 30000 },
|
|
91
|
+
)
|
|
92
|
+
} catch {
|
|
93
|
+
/* 没中文语音就跳过 */
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const realKey = loadDeepSeekKey({})
|
|
97
|
+
if (fs.existsSync(wav) && realKey.key) {
|
|
98
|
+
const r = await transcribe({ file: wav, apiKey: realKey.key })
|
|
99
|
+
ok('转写:调用成功', r.ok === true, JSON.stringify(r))
|
|
100
|
+
if (r.ok) {
|
|
101
|
+
console.log(' 识别到:' + r.text)
|
|
102
|
+
ok('转写:内容对得上(含"后室/后世"与"第三关")', /后[室世]/.test(r.text) && /第三关/.test(r.text), r.text)
|
|
103
|
+
ok('转写:返回时长', Number(r.durationMs) > 1000, String(r.durationMs))
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
console.log(' (跳过真实转写:没有 wav 或没有 key)')
|
|
107
|
+
ok('转写:没 key 时给明确原因', (await transcribe({ file: wav || 'x', apiKey: '' })).ok === false)
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// ---------- 6. 错误路径 ----------
|
|
111
|
+
const noKey = await transcribe({ file: wav || 'x', apiKey: '' })
|
|
112
|
+
ok('转写:没 key → 明确报错而不是崩', noKey.ok === false && /key/i.test(noKey.reason), noKey.reason)
|
|
113
|
+
const badFile = await transcribe({ file: path.join(tmp, 'nope.wav'), apiKey: 'sk-x' })
|
|
114
|
+
ok('转写:文件不存在 → 报错', badFile.ok === false && /不存在/.test(badFile.reason), badFile.reason)
|
|
115
|
+
const local = await transcribeLocal({ file: 'x.wav', command: `powershell -NoProfile -Command "Write-Output '本地结果'"` })
|
|
116
|
+
ok('本地保底:能取到 stdout 第一行', local.ok === true && local.text === '本地结果', JSON.stringify(local))
|
|
117
|
+
const noLocal = await transcribeLocal({ file: 'x.wav', command: '' })
|
|
118
|
+
ok('本地保底:没配命令时报错不崩', noLocal.ok === false)
|
|
119
|
+
|
|
120
|
+
fs.rmSync(tmp, { recursive: true, force: true })
|
|
121
|
+
console.log(`\nP32 ${fail === 0 ? 'OK' : 'FAIL'} pass=${pass} fail=${fail}`)
|
|
122
|
+
process.exit(fail === 0 ? 0 : 1)
|