fix: mac arm通过文件夹打开被判定为intel芯片

mac os升级27 权限问题解决文档
This commit is contained in:
Wxw-Gu committed 2026-11-06 11:10:01 +08:00
1 parent 80e0f43529
commit 05b881d79e
18 files changed
+1066 -29

No files matched your search

+10 -1
View File
@@ -76,6 +76,8 @@ import type {
} from '../shared/image-insight'
import type { SystemOcrCapability, SystemOcrRequest, SystemOcrResult } from '../shared/system-ocr'
import { KeyServiceMac } from './key-service-mac'
import { getMacArchSnapshot } from './macos-arch'
import { shouldProbeIntelEnvironment } from '../shared/macos-arch'
import { KeyService as KeyServiceWin } from './key-service-win'
import * as chat from './services/chat-service'
import {
@@ -1772,11 +1774,18 @@ app.whenReady().then(async () => {
)
const self = chat.getSelfAccountInfo()
const settings = loadSettings()
const intelMac = process.platform === 'darwin' && process.arch === 'x64'
const arch = getMacArchSnapshot()
// 只有真正的 Intel Mac 才需要探测 Intel 运行环境。
// Apple 芯片上的 x64 进程(Rosetta)不能走这条路径,否则每次刷新环境都会起一个 x86 子进程。
const intelMac = shouldProbeIntelEnvironment(arch.macKeyFlow)
const intelEnvironment = intelMac ? await keyServiceMac.getIntelEnvironmentStatus() : undefined
const environment = {
platform: process.platform,
architecture: process.arch,
hardwareArchitecture: arch.hardwareArch,
underARM64Translation: arch.underARM64Translation,
macKeyFlow: arch.macKeyFlow,
archAnomalies: arch.anomalies,
osVersion: getOsVersionLabel(),
appVersion: `v${app.getVersion()}`,
wechatVersion: await detectWechatVersion(),
+34 -2
View File
@@ -7,6 +7,8 @@ import { isValidDatabaseKey } from './database-key-store'
import crypto from 'crypto'
import { findResource, getResourceCandidates } from './resource-paths'
import { detectWechatVersion } from './services/connection-diagnostics'
import { getMacArchSnapshot } from './macos-arch'
import { ARCH_MISMATCH_X64_ON_ARM_ERROR, shouldProbeIntelEnvironment } from '../shared/macos-arch'
const execFileAsync = promisify(execFile)
@@ -239,6 +241,15 @@ export class KeyServiceMac {
fridaAvailable: boolean
wechatAdhocSigned: boolean
}> {
// 方法内部再守一道:调用方漏判时也不能起 x86 helper。
if (!shouldProbeIntelEnvironment(getMacArchSnapshot().macKeyFlow)) {
return {
sipDisabled: false,
pythonAvailable: false,
fridaAvailable: false,
wechatAdhocSigned: false
}
}
const payload = await this.runMacKeyTool(
['status', '--runtime-dir', this.getMacKeyRuntimeDir()],
15_000
@@ -254,7 +265,15 @@ export class KeyServiceMac {
async installIntelKeyRuntime(
onStatus?: (message: string) => void
): Promise<{ success: boolean; error?: string; code?: string }> {
if (process.platform !== 'darwin' || process.arch !== 'x64') {
const arch = getMacArchSnapshot()
if (arch.macKeyFlow === 'arch-mismatch') {
return {
success: false,
code: 'ARCH_MISMATCH_X64_ON_ARM',
error: ARCH_MISMATCH_X64_ON_ARM_ERROR
}
}
if (arch.macKeyFlow !== 'intel') {
return { success: false, code: 'UNSUPPORTED_PLATFORM', error: '仅 Intel Mac 需要安装此运行环境' }
}
onStatus?.('正在准备连接环境,请保持网络连接…')
@@ -354,12 +373,25 @@ export class KeyServiceMac {
if (process.platform !== 'darwin') {
return { success: false, error: '自动获取密钥目前仅支持 macOS' }
}
// 架构判定排在 SIP 之前:不匹配时先说明「当前跑的是 Intel 版」,
// 不能让用户先看到 SIP 提示、关闭 SIP 之后才发现问题依旧。
const arch = getMacArchSnapshot()
if (arch.macKeyFlow === 'arch-mismatch') {
return {
success: false,
code: 'ARCH_MISMATCH_X64_ON_ARM',
error: ARCH_MISMATCH_X64_ON_ARM_ERROR
}
}
if (arch.macKeyFlow === 'unsupported') {
return { success: false, error: '自动获取密钥目前仅支持 macOS' }
}
if (await this.isSipEnabled()) {
return { success: false, code: 'SIP_ENABLED', error: SIP_ENABLED_ERROR }
}
try {
if (process.arch === 'x64') {
if (arch.macKeyFlow === 'intel') {
onStatus?.('Intel Mac 将通过 Frida 获取微信主密钥,请让微信停留在未登录界面')
const result = await this.captureIntelDbKey(accountRoot, timeoutMs, onStatus)
onStatus?.(result.success ? '密钥获取成功' : '密钥获取失败')
+60
View File
@@ -0,0 +1,60 @@
import { execFileSync } from 'node:child_process'
import { app } from 'electron'
import { buildMacArchSnapshot, type MacArchSnapshot, type MacHardwareArch } from '../shared/macos-arch'
let cachedSnapshot: MacArchSnapshot | null = null
/**
* `hw.optional.arm64` 是唯一可靠的硬件判据:它在 ARM 上恒为 1,
* 在 Intel 上不存在或为 0,且与进程是否被翻译无关。
*
* 读不到时返回 `unknown`,由上层保留兼容路径 —— 不能猜测硬件,
* 猜错会让真正的 Intel Mac 被误拦。
*/
function readHardwareArch(): MacHardwareArch {
try {
const stdout = execFileSync('/usr/sbin/sysctl', ['-n', 'hw.optional.arm64'], {
encoding: 'utf8',
timeout: 1000,
stdio: ['ignore', 'pipe', 'ignore']
})
const value = stdout.trim()
if (value === '1') return 'arm64'
if (value === '0') return 'x64'
return 'unknown'
} catch {
return 'unknown'
}
}
function readTranslationFlag(platform: NodeJS.Platform): boolean {
if (platform !== 'darwin') return false
try {
return app.runningUnderARM64Translation === true
} catch {
return false
}
}
/**
* 取当前进程的架构快照。
*
* 结果缓存一次:调用方包括每次刷新连接页都会走的 `key:getEnvironment`,
* 缓存后 sysctl 只在首次执行,避免刷 UI 反复起子进程。
*/
export function getMacArchSnapshot(): MacArchSnapshot {
if (cachedSnapshot) return cachedSnapshot
const platform = process.platform
cachedSnapshot = buildMacArchSnapshot({
platform,
hardwareArch: platform === 'darwin' ? readHardwareArch() : 'unknown',
runtimeArch: process.arch,
underARM64Translation: readTranslationFlag(platform)
})
return cachedSnapshot
}
/** 仅供测试重置缓存;运行期不调用。 */
export function resetMacArchSnapshotCache(): void {
cachedSnapshot = null
}
+31 -7
View File
@@ -96,7 +96,15 @@ export class AIProviderRequestError extends Error {
constructor(
message: string,
details: { status?: number; code?: unknown; type?: unknown; responseBody?: unknown; contentType?: string; elapsedMs?: number; htmlInsteadOfJson?: boolean } = {}
details: {
status?: number
code?: unknown
type?: unknown
responseBody?: unknown
contentType?: string
elapsedMs?: number
htmlInsteadOfJson?: boolean
} = {}
) {
super(message)
this.name = 'AIProviderRequestError'
@@ -358,7 +366,10 @@ export class AIProviderService {
try {
if (options?.apiKey) throw new Error('Tool Calling 不支持 legacy provider 配置')
const resolved = this.resolveProvider(options)
if (resolved.provider.type !== 'openai-compatible' || resolved.provider.advanced.apiProtocol === 'responses') {
if (
resolved.provider.type !== 'openai-compatible' ||
resolved.provider.advanced.apiProtocol === 'responses'
) {
throw new Error('当前 Provider 协议暂不支持 Tool Calling POC')
}
const result = await requestOpenAICompatibleWithTools(
@@ -377,7 +388,8 @@ export class AIProviderService {
success: false,
error: safeAIError(error),
timedOut,
htmlInsteadOfJson: error instanceof AIProviderRequestError ? Boolean(error.htmlInsteadOfJson) : false,
htmlInsteadOfJson:
error instanceof AIProviderRequestError ? Boolean(error.htmlInsteadOfJson) : false,
...(error instanceof AIProviderRequestError
? {
...(error.status !== undefined ? { errorStatus: error.status } : {}),
@@ -638,7 +650,7 @@ function deepSeekProvider(baseUrl?: string, model?: string): AIProviderSummary {
}
],
defaultModel: modelId,
advanced: { timeoutMs: 120_000, temperature: 0.7, maxTokens: 4096, extraHeaders: {} },
advanced: { timeoutMs: 120_000, temperature: 0.7, maxTokens: 40960, extraHeaders: {} },
hasApiKey: false,
isDefault: true,
status: 'untested'
@@ -981,7 +993,12 @@ async function requestOpenAICompatibleWithTools(
messages: Array<Record<string, unknown>>,
tools: AIChatToolDefinition[],
signal?: AbortSignal
): Promise<{ data: string; toolCalls: AIChatToolCall[]; usage?: AIRequestResult['usage']; elapsedMs?: number }> {
): Promise<{
data: string
toolCalls: AIChatToolCall[]
usage?: AIRequestResult['usage']
elapsedMs?: number
}> {
const endpoint = provider.baseUrl.endsWith('/chat/completions')
? provider.baseUrl
: `${provider.baseUrl.replace(/\/+$/, '')}/chat/completions`
@@ -1013,7 +1030,8 @@ async function requestOpenAICompatibleWithTools(
}
if (!response.ok) {
throw new AIProviderRequestError(
payload?.error?.message || (parseError ? safeAIError(parseError) : `AI 请求失败 (${response.status})`),
payload?.error?.message ||
(parseError ? safeAIError(parseError) : `AI 请求失败 (${response.status})`),
{
status: timing.status,
contentType: timing.contentType,
@@ -1034,7 +1052,13 @@ async function requestOpenAICompatibleWithTools(
toolCalls: (message?.tool_calls || []).flatMap((call, index) => {
const name = call.function?.name
if (!name) return []
return [{ id: call.id || `tool-call-${index + 1}`, name, arguments: call.function?.arguments || '{}' }]
return [
{
id: call.id || `tool-call-${index + 1}`,
name,
arguments: call.function?.arguments || '{}'
}
]
}),
usage: toOpenAIUsage(payload.usage),
elapsedMs: timing.elapsedMs