mirror of
https://wget.la/https://github.com/Wxw-Gu/WechatExplorer
synced 2026-12-01 01:05:30 +08:00
feat: 拓展中心加入机器人
This commit is contained in:
1 parent
4eaddc2d75
commit
a81b3733fa
31 files changed
+3510
-566
No files matched your search
@@ -74,11 +74,7 @@ export function buildPersonalWechatRuntimePath(): string {
|
||||
return [dirname(bundledFfmpeg), existing].filter(Boolean).join(delimiter)
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the environment used by TraceMemo's local voice encoding helpers.
|
||||
* Keep this in one place so environment checks cannot accidentally inspect a
|
||||
* different ffmpeg than the sender process.
|
||||
*/
|
||||
/** Build the environment used by TraceMemo's local voice encoding helpers.。 */
|
||||
export function buildPersonalWechatRuntimeEnvironment(): NodeJS.ProcessEnv {
|
||||
return {
|
||||
...process.env,
|
||||
@@ -217,13 +213,15 @@ async function prepareWindowsVoiceFile(filePath: string): Promise<{
|
||||
)
|
||||
const pcm = Buffer.isBuffer(result.stdout) ? result.stdout : Buffer.from(result.stdout || '')
|
||||
if (!pcm.length) throw new Error('语音转换失败:没有得到 PCM 音频数据')
|
||||
const pcmDurationMs = Math.round((pcm.length / 2 / 24_000) * 1000)
|
||||
const encoded = await new SilkAudioEncoder().encode(pcm, 24_000)
|
||||
if (!encoded.data.length) throw new Error('语音转换失败:没有得到 SILK 数据')
|
||||
const durationMs = encoded.durationMs >= 1000 ? encoded.durationMs : pcmDurationMs
|
||||
const convertedPath = join(tmpdir(), `tracememo-voice-${randomBytes(8).toString('hex')}.silk`)
|
||||
writeFileSync(convertedPath, encoded.data)
|
||||
return {
|
||||
filePath: convertedPath,
|
||||
...(encoded.durationMs > 0 ? { durationMs: encoded.durationMs } : {}),
|
||||
...(durationMs > 0 ? { durationMs } : {}),
|
||||
temporary: true
|
||||
}
|
||||
}
|
||||
@@ -297,12 +295,7 @@ function prepareMacWechatImageFile(
|
||||
senderWxid: string,
|
||||
wechatFilesRoot = WECHAT_FILES_ROOT
|
||||
): { filePath: string; md5: string } {
|
||||
/* Stage the image in WeChat's temporary image directory using the
|
||||
* filename/hash layout required by the macOS native send path:
|
||||
* - stage inside the active account's ImageTemp tree selected by
|
||||
* findWechatImagePath(), not an arbitrary readable sandbox directory;
|
||||
* - append a per-send salt before calculating MD5, so the staged image and
|
||||
* the MD5 passed to the CDN task describe the same unique byte stream. */
|
||||
/** Stage the image in WeChat's temporary image directory using the。 */
|
||||
const temporaryRoot = findWechatImagePath(wechatFilesRoot, new Date(), senderWxid)
|
||||
if (!temporaryRoot) throw new Error('无法定位当前微信账号的图片临时目录')
|
||||
mkdirSync(temporaryRoot, { recursive: true })
|
||||
@@ -346,12 +339,7 @@ function stageMacWechatImageForMessageSourceSend(
|
||||
const MAC_VIDEO_STAGE_PREFIX = 'tm-video-'
|
||||
const MAC_VIDEO_STAGE_RETENTION_MS = 10 * 60 * 1000
|
||||
|
||||
/**
|
||||
* macOS WeChat 运行在 App Sandbox 容器中,注入在微信进程内的 `libtmwechat.dylib`
|
||||
* 只能读取 `~/Library/Containers/com.tencent.xinWeChat/Data/...` 容器内路径,
|
||||
* 直接传 `~/Library/Application Support/TraceMemo/...` 会触发 `VIDEO_PREPARATION_FAILED:0`。
|
||||
* 因此发送前需将容器外视频暂存到微信容器的 `Documents` 目录。
|
||||
*/
|
||||
/** macOS WeChat 运行在 App Sandbox 容器中,注入在微信进程内的 `libtmwechat.dylib`。 */
|
||||
export function stageMacWechatVideoForSend(
|
||||
filePath: string,
|
||||
wechatFilesRoot = WECHAT_FILES_ROOT
|
||||
@@ -587,14 +575,7 @@ function isDefinitiveImageSourcePreparationFailure(message: string): boolean {
|
||||
return message.trim() === 'IMAGE_PREPARATION_FAILED:0'
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送链路诊断日志(排查「hook 报成功、群里却没有消息」时用)。
|
||||
*
|
||||
* 只有在设置里打开「调试模式」时才输出,所以正常使用不会刷屏。
|
||||
*
|
||||
* **隐私约束**:只允许输出类型、字节数、HTTP 状态码、hook 的 `ret` 这类结构性字段。
|
||||
* 文件名、完整路径、wxid、群名、消息正文不进日志。
|
||||
*/
|
||||
/** 发送链路诊断日志(排查「hook 报成功、群里却没有消息」时用)。 */
|
||||
function logSendDiagnostic(
|
||||
stage: string,
|
||||
detail: Record<string, string | number | boolean | null>
|
||||
@@ -607,16 +588,7 @@ function logSendDiagnostic(
|
||||
console.log(`[SendDiag] ${stage} ${JSON.stringify(detail)}`)
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送完成后**保留**图片临时文件的时长(毫秒)。默认 `0` = 立刻删除(现行行为)。
|
||||
*
|
||||
* 为什么留这个开关:`finally` 里的删除发生在 **hook 返回之后**,这隐含假设
|
||||
* 「hook 返回时发送已经完成」。如果 hook 其实是「先回 200、后台再读文件」,
|
||||
* 文件就可能在它读到之前已经被删掉 —— 现象正好是「hook 报成功、微信里没有消息」。
|
||||
*
|
||||
* 置为非 0 时可分辨两类失败:保留期间图片到了 ⇒ 删除时机问题(修法与 hook 约定文件生命周期);
|
||||
* 仍然没到 ⇒ 与文件生命周期无关。
|
||||
*/
|
||||
/** 发送完成后**保留**图片临时文件的时长(毫秒)。默认 `0` = 立刻删除(现行行为)。 */
|
||||
const WINDOWS_TEMP_IMAGE_GRACE_MS = 0
|
||||
|
||||
/** 视频上传在 send_message 返回后才开始,删早了接收端只能拿到空文件。 */
|
||||
@@ -784,10 +756,7 @@ export class PersonalWechatSendService {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* macOS native runtime 的文字发送:HTTP POST /sendText 到 tm-wechat-host。
|
||||
* 绑定状态由 host 侧保证;未绑定/未就绪时返回与旧契约一致的结果结构。
|
||||
*/
|
||||
/** macOS native runtime 的文字发送:HTTP POST /sendText 到 tm-wechat-host。 */
|
||||
private async sendMacText(
|
||||
request: Extract<PersonalWechatSendRequest, { type: 'text' }>
|
||||
): Promise<PersonalWechatSendResult> {
|
||||
@@ -856,12 +825,7 @@ export class PersonalWechatSendService {
|
||||
return { success: true, status: resultStatus }
|
||||
}
|
||||
|
||||
/*
|
||||
* Resolve the bound account's own wxid from the local WeChat data root.
|
||||
* The media proto embeds the sender, and the mac runtime does not learn it
|
||||
* from inbound traffic, so it is read from
|
||||
* the on-disk account identity instead.
|
||||
*/
|
||||
/** Resolve the bound account's own wxid from the local WeChat data root.。 */
|
||||
private async resolveMacSenderWxid(): Promise<string> {
|
||||
const identity = readLocalAccountIdentity(this.wechatFilesRoot)
|
||||
return identity?.wxid ?? ''
|
||||
@@ -881,11 +845,7 @@ export class PersonalWechatSendService {
|
||||
if (response === null || !response.ok) return null
|
||||
const payload = (await response.json().catch(() => null)) as Record<string, unknown> | null
|
||||
if (!payload) return null
|
||||
/*
|
||||
* 不同 Host 路由的响应形状不一致:`/sendText` 等返回 `{ok}`,
|
||||
* `/SendMsg` 兼容层可能返回 `{status:true}` 或 Windows 风格的 `{ret:0}`。
|
||||
* 这里统一收敛,避免「实际已发送、界面报失败」。
|
||||
*/
|
||||
/** 不同 Host 路由的响应形状不一致:`/sendText` 等返回 `{ok}`。 */
|
||||
const explicitFailure = payload.ok === false || payload.status === false
|
||||
const ok =
|
||||
!explicitFailure &&
|
||||
@@ -897,13 +857,7 @@ export class PersonalWechatSendService {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* macOS 视频发送:`POST /SendMsg`,body 为 `{toWxid, type:'video', msg:<绝对路径>}`。
|
||||
*
|
||||
* 微信运行在 App Sandbox 容器中,注入的 `libtmwechat.dylib` 在微信进程内执行,
|
||||
* 只能访问微信沙盒容器目录(`~/Library/Containers/com.tencent.xinWeChat/Data/...`)。
|
||||
* 因此先将容器外视频暂存到微信容器 `Documents` 目录,再调用 `/SendMsg`。
|
||||
*/
|
||||
/** macOS 视频发送:`POST /SendMsg`,body 为 `{toWxid, type:'video', msg:<绝对路径>}`。 */
|
||||
private async sendMacVideo(
|
||||
request: Extract<PersonalWechatSendRequest, { type: 'video' }>
|
||||
): Promise<PersonalWechatSendResult> {
|
||||
@@ -1057,8 +1011,7 @@ export class PersonalWechatSendService {
|
||||
if (!sender) return fail('无法识别当前微信账号 wxid')
|
||||
}
|
||||
|
||||
/* Reuse the Windows SILK preparation: it already normalizes any input to
|
||||
* SILK and reports the duration. */
|
||||
/** Reuse the Windows SILK preparation: it already normalizes any input to。 */
|
||||
let temporaryVoicePath: string | undefined
|
||||
try {
|
||||
const prepared = await prepareWindowsVoiceFile(filePath)
|
||||
|
||||
@@ -22,6 +22,20 @@ const FISH_AUDIO_PUBLIC_ASSET_URL = 'https://public-platform.r2.fish.audio/'
|
||||
const DEFAULT_PAGE_SIZE = 24
|
||||
const MAX_PAGE_SIZE = 100
|
||||
const GENERATED_AUDIO_MAX_AGE_MS = 24 * 60 * 60 * 1000
|
||||
const VOICE_META_CACHE_TTL_MS = 10 * 60 * 1000
|
||||
const voiceMetaCache = new Map<string, { voice: TextToSpeechVoice; cachedAt: number }>()
|
||||
|
||||
function cacheVoiceMeta(voice: TextToSpeechVoice): void {
|
||||
if (!voice?.id) return
|
||||
voiceMetaCache.set(voice.id, { voice, cachedAt: Date.now() })
|
||||
}
|
||||
|
||||
function getCachedVoiceMeta(id: string): TextToSpeechVoice | null {
|
||||
const entry = voiceMetaCache.get(id)
|
||||
if (!entry) return null
|
||||
if (Date.now() - entry.cachedAt > VOICE_META_CACHE_TTL_MS) return null
|
||||
return entry.voice
|
||||
}
|
||||
|
||||
interface FishAudioModelEntity {
|
||||
_id: string
|
||||
@@ -128,6 +142,7 @@ export class TextToSpeechSettingsService {
|
||||
let items = payload.items
|
||||
.filter((model) => model.type === 'tts' && model.state === 'trained')
|
||||
.map(toVoice)
|
||||
for (const item of items) cacheVoiceMeta(item)
|
||||
|
||||
const selectedVoiceId = normalizeSelectedVoiceId(loadSettings().ttsSelectedVoiceId)
|
||||
if (
|
||||
@@ -138,7 +153,10 @@ export class TextToSpeechSettingsService {
|
||||
) {
|
||||
if (!items.some((item) => item.id === selectedVoiceId)) {
|
||||
const selected = await this.getVoice(selectedVoiceId, resolved.key)
|
||||
if (selected) items = [selected, ...items]
|
||||
if (selected) {
|
||||
cacheVoiceMeta(selected)
|
||||
items = [selected, ...items]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,6 +247,37 @@ export class TextToSpeechSettingsService {
|
||||
}
|
||||
}
|
||||
|
||||
async getVoiceStatus(): Promise<{
|
||||
configured: boolean
|
||||
model: TextToSpeechModel
|
||||
selectedVoiceId: string
|
||||
selectedVoice: TextToSpeechVoice | null
|
||||
}> {
|
||||
const resolved = this.resolveKey()
|
||||
const settings = loadSettings()
|
||||
const selectedVoiceId = normalizeSelectedVoiceId(settings.ttsSelectedVoiceId)
|
||||
const model = normalizeModel(settings.ttsModel)
|
||||
const configured = Boolean(resolved.key && selectedVoiceId)
|
||||
if (!selectedVoiceId) {
|
||||
return { configured: false, model, selectedVoiceId: '', selectedVoice: null }
|
||||
}
|
||||
const cached = getCachedVoiceMeta(selectedVoiceId)
|
||||
if (cached) {
|
||||
return { configured, model, selectedVoiceId, selectedVoice: cached }
|
||||
}
|
||||
if (!resolved.key) {
|
||||
return { configured: false, model, selectedVoiceId, selectedVoice: null }
|
||||
}
|
||||
const fetched = await this.getVoice(selectedVoiceId, resolved.key)
|
||||
if (fetched) cacheVoiceMeta(fetched)
|
||||
return {
|
||||
configured,
|
||||
model,
|
||||
selectedVoiceId,
|
||||
selectedVoice: fetched
|
||||
}
|
||||
}
|
||||
|
||||
private resolveKey(): {
|
||||
success: boolean
|
||||
key?: string
|
||||
@@ -299,7 +348,7 @@ function toVoice(model: FishAudioModelEntity): TextToSpeechVoice {
|
||||
languages: Array.isArray(model.languages) ? model.languages : [],
|
||||
source: 'fish-audio',
|
||||
coverImage: resolvePublicAssetUrl(model.cover_image || model.author?.avatar),
|
||||
previewUrl: sample?.audio || undefined,
|
||||
previewUrl: resolvePublicAssetUrl(sample?.audio),
|
||||
previewText: sample?.text || model.default_text || undefined,
|
||||
authorName: model.author?.nickname || undefined,
|
||||
taskCount: model.task_count,
|
||||
|
||||
Reference in new issue
Block a user