feat: mac语音转换silk格式修复

This commit is contained in:
Wxw-Gu committed 2026-08-31 10:55:52 +08:00
1 parent 5acfa14475
commit 6cbf9af662
14 files changed
+970 -12

No files matched your search

+44
View File
@@ -115,6 +115,7 @@ import { personalWechatSendService } from './services/personal-wechat-send-servi
import { getPersonalWechatSendCapability } from './services/personal-wechat-capability-service'
import { scheduledReportService } from './services/scheduled-report-service'
import { PersonalWechatRuntimeManager } from './services/personal-wechat-runtime-manager'
import { personalWechatVoiceEnvironmentService } from './services/personal-wechat-voice-environment-service'
import type { PersonalWechatSendRequest } from '../shared/personal-wechat'
import type {
ScheduledReportCreateInput,
@@ -1860,6 +1861,48 @@ app.whenReady().then(async () => {
ipcMain.handle('wechat-personal:checkStatus', (_, port?: string) =>
personalWechatSendService.checkWindowsStatus(port)
)
ipcMain.handle('wechat-personal:checkVoiceEnvironment', () =>
personalWechatVoiceEnvironmentService.check()
)
ipcMain.handle('wechat-personal:installPilk', async () => {
const result = await personalWechatVoiceEnvironmentService.installPilk()
if (!result.success || process.platform !== 'darwin') return result
const senderStatus = await personalWechatSendService.getStatus()
if (!senderStatus.oneBotPid) return result
try {
const restartedStatus = await personalWechatSendService.restartRuntime()
return {
...result,
restarted: restartedStatus.state !== 'error',
...(restartedStatus.state === 'error'
? { restartError: restartedStatus.error || restartedStatus.message }
: {})
}
} catch (error) {
return {
...result,
restarted: false,
restartError: error instanceof Error ? error.message : String(error)
}
}
})
ipcMain.handle('wechat-personal:openVoicePythonDownload', async () => {
try {
await shell.openExternal('https://www.python.org/downloads/macos/')
return { success: true }
} catch {
return { success: false, error: '无法打开 Python 下载页面' }
}
})
ipcMain.handle('wechat-personal:openVoiceFfmpegDownload', async () => {
try {
await shell.openExternal('https://brew.sh/')
return { success: true }
} catch {
return { success: false, error: '无法打开 FFmpeg 安装页面' }
}
})
ipcMain.handle('wechat-personal:getRuntimeStatus', () => personalWechatRuntimeManager.getStatus())
ipcMain.handle('wechat-personal:downloadRuntime', () => personalWechatRuntimeManager.download())
ipcMain.handle('wechat-personal:cancelRuntimeDownload', () => ({
@@ -1886,6 +1929,7 @@ app.whenReady().then(async () => {
const self = await chat.getSelfAccountInfoAsync()
fromId = String(self?.wxid || '').trim()
} catch {
// The sender can still operate without an account wxid for non-voice messages.
}
return personalWechatSendService.send(fromId ? { ...request, fromId } : request)
})
@@ -254,7 +254,7 @@ export function buildPersonalWechatOneBotRequest(
}
}
function buildRuntimePath(): string {
export function buildPersonalWechatRuntimePath(): string {
const existing = String(process.env['PATH'] || '')
const bundledFfmpeg = String(ffmpegStaticPath || '')
.replace('app.asar', 'app.asar.unpacked')
@@ -263,12 +263,25 @@ function buildRuntimePath(): string {
return [dirname(bundledFfmpeg), existing].filter(Boolean).join(delimiter)
}
function buildRuntimePythonPath(runtimeRoot: string): string {
export function buildPersonalWechatRuntimePythonPath(runtimeRoot: string): string {
return [join(runtimeRoot, 'python'), String(process.env['PYTHONPATH'] || '')]
.filter(Boolean)
.join(delimiter)
}
/**
* Build the same environment used when TraceMemo starts OneBot.
* Keep this in one place so runtime checks cannot accidentally inspect a
* different Python or ffmpeg than the sender process.
*/
export function buildPersonalWechatRuntimeEnvironment(runtimeRoot?: string): NodeJS.ProcessEnv {
return {
...process.env,
PATH: buildPersonalWechatRuntimePath(),
...(runtimeRoot ? { PYTHONPATH: buildPersonalWechatRuntimePythonPath(runtimeRoot) } : {})
}
}
function bundledFfmpegExecutable(): string {
const bundledFfmpeg = String(ffmpegStaticPath || '')
.replace('app.asar', 'app.asar.unpacked')
@@ -282,7 +295,7 @@ async function convertAudioToPcm(audioData: Buffer): Promise<Buffer> {
bundledFfmpegExecutable(),
['-v', 'error', '-i', 'pipe:0', '-f', 's16le', '-ar', '16000', '-ac', '1', 'pipe:1'],
{
env: { ...process.env, PATH: buildRuntimePath() },
env: buildPersonalWechatRuntimeEnvironment(),
stdio: ['pipe', 'pipe', 'pipe'],
windowsHide: true
}
@@ -1224,6 +1237,13 @@ export class PersonalWechatSendService {
this.lastError = ''
}
/** Restart the existing OneBot runtime after an environment change. */
async restartRuntime(): Promise<PersonalWechatSenderStatus> {
if (process.platform === 'win32') return this.getWindowsStatus()
await this.terminate(true)
return this.rebind()
}
private async ensureRunning(): Promise<PersonalWechatSenderStatus> {
const currentStatus = await this.getStatus()
if (currentStatus.state === 'online' || currentStatus.state === 'hook_not_ready') {
@@ -1375,6 +1395,7 @@ export class PersonalWechatSendService {
try {
unlinkSync(temporaryVoicePath)
} catch {
// Temporary files are best-effort cleanup only.
}
}
}
@@ -1386,6 +1407,32 @@ export class PersonalWechatSendService {
return preflight.status
}
try {
const { personalWechatVoiceEnvironmentService } =
await import('./personal-wechat-voice-environment-service')
const environment = await personalWechatVoiceEnvironmentService.check()
if (!environment.pilk.ready) {
appLogger.write({
level: 'warn',
scope: 'VoiceRuntime',
message: 'WARNING: pilk unavailable, OneBot may fallback to go-silk.',
details: {
python: environment.python.executable,
pythonReady: environment.python.ready,
ffmpegReady: environment.ffmpeg.ready,
runtimeReady: environment.runtimeReady
}
})
}
} catch (error) {
appLogger.write({
level: 'warn',
scope: 'VoiceRuntime',
message: 'Voice encoding environment check failed before OneBot start',
details: { error: error instanceof Error ? error.message : String(error) }
})
}
const pid = preflight.status.wechatPid
const imagePath = findWechatImagePath()
this.lastError = ''
@@ -1405,11 +1452,7 @@ export class PersonalWechatSendService {
detached: true,
stdio: 'ignore',
windowsHide: true,
env: {
...process.env,
PATH: buildRuntimePath(),
PYTHONPATH: buildRuntimePythonPath(preflight.runtime.root)
}
env: buildPersonalWechatRuntimeEnvironment(preflight.runtime.root)
}
)
this.child = child
@@ -0,0 +1,274 @@
import { execFile } from 'child_process'
import { promisify } from 'util'
import { isAbsolute } from 'path'
import type {
PersonalWechatVoiceEncodingEnvironment,
PersonalWechatVoiceEncodingEnvironmentResult,
PersonalWechatVoiceRuntimeComponent
} from '../../shared/personal-wechat-voice-runtime'
import { PERSONAL_WECHAT_PILK_VERSION } from '../../shared/personal-wechat-voice-runtime'
import {
buildPersonalWechatRuntimeEnvironment,
findPersonalWechatRuntime
} from './personal-wechat-send-service'
import type { RuntimeLayout } from './personal-wechat-send-service'
import { appLogger } from '../app-logger'
const execFileAsync = promisify(execFile)
const COMMAND_TIMEOUT_MS = 10_000
const INSTALL_TIMEOUT_MS = 120_000
const PYTHON_PROBE =
'import json,sys; print(json.dumps({"executable":sys.executable,"version":".".join(map(str,sys.version_info[:3]))}))'
const PILK_PROBE =
'import json,pilk; print(json.dumps({"version":getattr(pilk,"__version__", ""),"path":getattr(pilk,"__file__", "")}))'
interface CommandOptions {
env: NodeJS.ProcessEnv
timeout?: number
}
type CommandRunner = (
executable: string,
args: string[],
options: CommandOptions
) => Promise<{ stdout: string; stderr: string }>
interface PersonalWechatVoiceEnvironmentServiceOptions {
platform?: NodeJS.Platform
architecture?: string
findRuntime?: () => RuntimeLayout | null
buildEnvironment?: (runtimeRoot?: string) => NodeJS.ProcessEnv
runCommand?: CommandRunner
now?: () => Date
}
function blankComponent(): PersonalWechatVoiceRuntimeComponent {
return { ready: false }
}
function commandError(error: unknown): string {
const message = error instanceof Error ? error.message : String(error)
return message.replace(/\s+/g, ' ').trim().slice(0, 500) || '命令执行失败'
}
function logEnvironmentLine(message: string, details?: Record<string, unknown>): void {
appLogger.write({ level: 'info', scope: 'VoiceRuntime', message, details })
}
function logEnvironmentWarning(message: string, details?: Record<string, unknown>): void {
appLogger.write({ level: 'warn', scope: 'VoiceRuntime', message, details })
}
async function runCommand(
executable: string,
args: string[],
options: CommandOptions
): Promise<{ stdout: string; stderr: string }> {
return execFileAsync(executable, args, {
env: options.env,
timeout: options.timeout || COMMAND_TIMEOUT_MS,
maxBuffer: 64 * 1024,
windowsHide: true
}) as Promise<{ stdout: string; stderr: string }>
}
async function findCommandPath(
command: string,
env: NodeJS.ProcessEnv,
runner: CommandRunner = runCommand
): Promise<string | undefined> {
try {
const { stdout } = await runner('/usr/bin/which', [command], { env })
const path = stdout.trim().split(/\r?\n/)[0]
return path && isAbsolute(path) ? path : undefined
} catch {
return undefined
}
}
function unsupportedEnvironment(): PersonalWechatVoiceEncodingEnvironment {
return {
state: 'unsupported',
ready: false,
runtimeReady: false,
python: blankComponent(),
pilk: blankComponent(),
ffmpeg: blankComponent(),
encoder: 'unavailable',
message: '语音编码环境检查当前仅支持 Apple Silicon Mac'
}
}
export class PersonalWechatVoiceEnvironmentService {
private readonly platform: NodeJS.Platform
private readonly architecture: string
private readonly findRuntime: () => RuntimeLayout | null
private readonly buildEnvironment: (runtimeRoot?: string) => NodeJS.ProcessEnv
private readonly runCommand: CommandRunner
private readonly now: () => Date
constructor(options: PersonalWechatVoiceEnvironmentServiceOptions = {}) {
this.platform = options.platform || process.platform
this.architecture = options.architecture || process.arch
this.findRuntime = options.findRuntime || findPersonalWechatRuntime
this.buildEnvironment = options.buildEnvironment || buildPersonalWechatRuntimeEnvironment
this.runCommand = options.runCommand || runCommand
this.now = options.now || (() => new Date())
}
async check(): Promise<PersonalWechatVoiceEncodingEnvironment> {
if (this.platform !== 'darwin' || this.architecture !== 'arm64') {
return unsupportedEnvironment()
}
logEnvironmentLine('Checking voice encoding environment')
const runtime = this.findRuntime()
const environment = this.buildEnvironment(runtime?.root)
const python = blankComponent()
const pilk = blankComponent()
const ffmpeg = blankComponent()
let pythonExecutable = ''
try {
const result = await this.runCommand('python3', ['-c', PYTHON_PROBE], { env: environment })
const payload = JSON.parse(result.stdout.trim()) as {
executable?: string
version?: string
}
pythonExecutable = String(payload.executable || '').trim()
const version = String(payload.version || '').trim()
if (!pythonExecutable || !version) throw new Error('Python 未返回可执行路径或版本')
python.ready = true
python.executable = pythonExecutable
python.version = version
logEnvironmentLine(`Python: ${pythonExecutable}`)
logEnvironmentLine(`Python version: ${version}`)
} catch (error) {
python.error = commandError(error)
logEnvironmentWarning('Python: unavailable', { error: python.error })
}
if (pythonExecutable) {
try {
const result = await this.runCommand(pythonExecutable, ['-c', PILK_PROBE], {
env: environment
})
const payload = JSON.parse(result.stdout.trim()) as { version?: string; path?: string }
const version = String(payload.version || '').trim()
const path = String(payload.path || '').trim()
if (!version || !path) throw new Error('pilk 未返回版本或安装路径')
pilk.version = version
pilk.path = path
pilk.ready = version === PERSONAL_WECHAT_PILK_VERSION
if (!pilk.ready) {
pilk.error = `版本不匹配,需要 ${PERSONAL_WECHAT_PILK_VERSION}`
}
logEnvironmentLine(`pilk: ${pilk.ready ? 'available' : 'incompatible'}`, {
version,
path
})
logEnvironmentLine(`pilk version: ${version}`)
} catch (error) {
pilk.error = commandError(error)
logEnvironmentWarning('pilk: unavailable', { error: pilk.error })
}
} else {
pilk.error = 'Python 未找到,无法检查 pilk'
}
try {
const [result, executable] = await Promise.all([
this.runCommand('ffmpeg', ['-version'], { env: environment }),
findCommandPath('ffmpeg', environment, this.runCommand)
])
const firstLine = result.stdout.split(/\r?\n/).find((line) => line.trim()) || ''
const version = firstLine.match(/^ffmpeg version\s+([^\s]+)/i)?.[1] || firstLine.trim()
if (!version) throw new Error('ffmpeg 未返回版本')
ffmpeg.ready = true
ffmpeg.version = version
ffmpeg.executable = executable || 'ffmpeg'
logEnvironmentLine('ffmpeg: available', {
executable: ffmpeg.executable,
version
})
} catch (error) {
ffmpeg.error = commandError(error)
logEnvironmentWarning('ffmpeg: unavailable', { error: ffmpeg.error })
}
const ready = Boolean(runtime && python.ready && pilk.ready && ffmpeg.ready)
const result: PersonalWechatVoiceEncodingEnvironment = {
state: ready ? 'ready' : 'incomplete',
ready,
checkedAt: this.now().toISOString(),
runtimeReady: Boolean(runtime),
...(runtime ? { runtimeRoot: runtime.root } : {}),
python,
pilk,
ffmpeg,
encoder: pilk.ready
? 'pilk'
: runtime && python.ready && ffmpeg.ready
? 'go-silk'
: 'unavailable',
message: ready
? '语音编码环境正常,可以使用 pilk 编码'
: runtime
? '语音编码环境不完整,OneBot 可能回退到 go-silk'
: '微信发送组件尚未安装,请先准备 OneBot 运行时'
}
logEnvironmentLine(
ready ? 'Voice encoding environment is ready' : 'Voice encoding environment is NOT ready',
{ runtimeReady: result.runtimeReady, encoder: result.encoder }
)
logEnvironmentLine(`Encoder: ${result.encoder}`)
return result
}
async installPilk(): Promise<PersonalWechatVoiceEncodingEnvironmentResult> {
const before = await this.check()
const pythonExecutable = before.python.executable
if (!before.python.ready || !pythonExecutable) {
return {
success: false,
environment: before,
error: '未找到 TraceMemo 实际使用的 Python,无法安装 pilk'
}
}
logEnvironmentLine(`Installing pilk ${PERSONAL_WECHAT_PILK_VERSION}`, {
python: pythonExecutable
})
try {
await this.runCommand(
pythonExecutable,
['-m', 'pip', 'install', '--user', `pilk==${PERSONAL_WECHAT_PILK_VERSION}`],
{
env: this.buildEnvironment(before.runtimeRoot || undefined),
timeout: INSTALL_TIMEOUT_MS
}
)
} catch (error) {
const message = commandError(error)
logEnvironmentWarning('pilk installation failed', { error: message })
return {
success: false,
environment: await this.check(),
error: `pilk 安装失败:${message}`
}
}
const after = await this.check()
if (!after.pilk.ready) {
const message = after.pilk.error || '安装后仍无法 import pilk'
logEnvironmentWarning('pilk installation did not produce a usable module', {
error: message
})
return { success: false, environment: after, error: message }
}
return { success: true, environment: after }
}
}
export const personalWechatVoiceEnvironmentService = new PersonalWechatVoiceEnvironmentService()