CodexBar 是一个 macOS 菜单栏应用,用于查看本机 Codex 账号状态、额度、Token 用量、详细统计信息
Swift appkitcodexcodex-clideveloper-toolsmacosmacos-appmenu-bar-appopenai-codexproductivityswiftswiftuiusage-tracker
fix: 适配 Codex App 内置 CLI 的清单路径
Bobdb7eed49133f
diff --git a/CodexBar/Services/CodexCLI/CodexCLIResolver.swift b/CodexBar/Services/CodexCLI/CodexCLIResolver.swiftindex 7a2a59e..995f8c2 100644--- a/CodexBar/Services/CodexCLI/CodexCLIResolver.swift+++ b/CodexBar/Services/CodexCLI/CodexCLIResolver.swift@@ -89,12 +89,13 @@ nonisolated struct CodexCLIInstallations: Equatable {/// 解析真实用户环境下的 Codex 可执行文件, 避免使用 Xcode/container 的 HOMEnonisolated enum CodexCLIResolver {- static let bundledExecutablePaths = [- "/Applications/ChatGPT.app/Contents/Resources/codex",- "/Applications/Codex.app/Contents/Resources/codex"- ]+ static let bundledResourceURL = URL(fileURLWithPath: "/Applications/ChatGPT.app/Contents/Resources", isDirectory: true)static let environment = appServerEnvironment()+ private struct PackageManifest: Decodable {+ let entrypoint: String+ }+/// 从已解析的安装信息派生命令, 避免重复扫描 PATHstatic func command(from installations: CodexCLIInstallations,@@ -131,7 +132,14 @@ nonisolated enum CodexCLIResolver {return value?.isEmpty == false ? value : nil}- static func resolveInstallations(environment: [String: String] = environment) -> CodexCLIInstallations {+ static func resolveInstallations(+ environment: [String: String] = environment,+ bundledResourceURL: URL = bundledResourceURL+ ) -> CodexCLIInstallations {+ let bundledExecutablePaths = [+ packageExecutablePath(in: bundledResourceURL),+ bundledResourceURL.appendingPathComponent("codex").path+ ].compactMap(\.self)let cliPath = findExecutable(named: "codex", environment: environment)let cliIsBundled = cliPath.map { path inbundledExecutablePaths.contains { pathsAreEquivalent(path, $0) }@@ -147,6 +155,17 @@ nonisolated enum CodexCLIResolver {)}+ private static func packageExecutablePath(in resources: URL) -> String? {+ let packageDirectory = resources.appendingPathComponent("codex-cli", isDirectory: true)+ let manifestURL = packageDirectory.appendingPathComponent("codex-package.json")+ guard let data = try? Data(contentsOf: manifestURL),+ let manifest = try? JSONDecoder().decode(PackageManifest.self, from: data),+ !manifest.entrypoint.isEmpty else {+ return nil+ }+ return packageDirectory.appendingPathComponent(manifest.entrypoint).path+ }+private static func appServerEnvironment() -> [String: String] {var environment = ProcessInfo.processInfo.environmentlet homeDirectory = realUserHomeDirectory()diff --git a/CodexBarTests/CodexVersionAndProxyTests.swift b/CodexBarTests/CodexVersionAndProxyTests.swiftindex 46f90da..d5a0760 100644--- a/CodexBarTests/CodexVersionAndProxyTests.swift+++ b/CodexBarTests/CodexVersionAndProxyTests.swift@@ -42,6 +42,80 @@ struct CodexVersionAndProxyTests {#expect(CodexCLIResolver.codexHomeDirectory(environment: ["CODEX_HOME": " ", "HOME": "/tmp/home"]).path == "/tmp/home/.codex")}+ @Test func bundledCLIUsesManifestEntrypointBeforeLegacyPath() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ _ = try directory.write(#"{"entrypoint":"custom/cli","version":"0.158.0","layoutVersion":1}"#, to: "codex-cli/codex-package.json")+ let entrypoint = try writeExecutable(in: directory, path: "codex-cli/custom/cli")+ _ = try writeExecutable(in: directory, path: "codex")++ let installations = CodexCLIResolver.resolveInstallations(environment: ["PATH": ""], bundledResourceURL: directory.url)+ #expect(installations.bundledPath == entrypoint.path)+ #expect(installations.globalPath == nil)+ #expect(try CodexCLIResolver.command(from: installations).executablePath == entrypoint.path)+ }++ @Test(arguments: [nil, "invalid", "{}", #"{"entrypoint":42}"#, #"{"entrypoint":""}"#, #"{"entrypoint":"bin/missing"}"#, #"{"entrypoint":"bin/codex"}"#])+ func unavailablePackageFallsBackToLegacyPath(_ manifest: String?) throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ if let manifest {+ _ = try directory.write(manifest, to: "codex-cli/codex-package.json")+ }+ let nonExecutable = try directory.write("", to: "codex-cli/bin/codex")+ try FileManager.default.setAttributes([.posixPermissions: 0o644], ofItemAtPath: nonExecutable.path)+ let legacy = try writeExecutable(in: directory, path: "codex")++ let installations = CodexCLIResolver.resolveInstallations(environment: ["PATH": ""], bundledResourceURL: directory.url)+ #expect(installations.bundledPath == legacy.path)+ #expect(installations.globalPath == nil)+ try FileManager.default.removeItem(at: legacy)+ let missing = CodexCLIResolver.resolveInstallations(environment: ["PATH": ""], bundledResourceURL: directory.url)+ #expect(missing.bundledPath == nil)+ }++ @Test func globalCLIStillTakesPriorityOverManifestEntrypoint() throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ _ = try directory.write(#"{"entrypoint":"bin/codex"}"#, to: "codex-cli/codex-package.json")+ let bundled = try writeExecutable(in: directory, path: "codex-cli/bin/codex")+ let global = try writeExecutable(in: directory, path: "global/codex")++ let installations = CodexCLIResolver.resolveInstallations(+ environment: ["PATH": global.deletingLastPathComponent().path],+ bundledResourceURL: directory.url+ )+ #expect(installations.globalPath == global.path)+ #expect(installations.bundledPath == bundled.path)+ #expect(try CodexCLIResolver.command(from: installations).source == .global)+ #expect(try CodexCLIResolver.command(from: installations, source: .bundled).executablePath == bundled.path)+ }++ @Test(arguments: ["codex-cli/bin/codex", "codex"])+ func pathSymlinkToBundledCLIIsNotClassifiedAsGlobal(_ target: String) throws {+ let directory = try TestDirectory()+ defer { try? directory.remove() }+ _ = try directory.write(#"{"entrypoint":"bin/codex"}"#, to: "codex-cli/codex-package.json")+ let bundled = try writeExecutable(in: directory, path: "codex-cli/bin/codex")+ _ = try writeExecutable(in: directory, path: "codex")+ let link = directory.url.appendingPathComponent("global/codex")+ try FileManager.default.createDirectory(at: link.deletingLastPathComponent(), withIntermediateDirectories: true)+ try FileManager.default.createSymbolicLink(at: link, withDestinationURL: directory.url.appendingPathComponent(target))++ let installations = CodexCLIResolver.resolveInstallations(+ environment: ["PATH": link.deletingLastPathComponent().path],+ bundledResourceURL: directory.url+ )+ #expect(installations.globalPath == nil)+ #expect(installations.bundledPath == bundled.path)+ }++ private func writeExecutable(in directory: TestDirectory, path: String) throws -> URL {+ let url = try directory.write("#!/bin/sh\nexit 0\n", to: path)+ try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: url.path)+ return url+ }+@Test(arguments: ["https://proxy.example", "host/path", "user@host", "host?query", "host#fragment", "host name", "[invalid]", "fe80::1%en0"])func proxyRejectsAmbiguousHostInput(_ host: String) {let configuration = CodexProxyConfiguration(host: host, port: "8080")diff --git a/Config/Version.xcconfig b/Config/Version.xcconfigindex 8fff83b..7d35abc 100644--- a/Config/Version.xcconfig+++ b/Config/Version.xcconfig@@ -1,2 +1,2 @@-MARKETING_VERSION = 4.0.6-CURRENT_PROJECT_VERSION = 106+MARKETING_VERSION = 4.0.7+CURRENT_PROJECT_VERSION = 107diff --git a/Docs/DeveloperGuide/app-server.md b/Docs/DeveloperGuide/app-server.mdindex 37ee9c5..a90644e 100644--- a/Docs/DeveloperGuide/app-server.md+++ b/Docs/DeveloperGuide/app-server.md@@ -33,13 +33,14 @@ CodexCLIResolver## Codex CLI 定位-[`CodexCLIResolver.swift`](../../CodexBar/Services/CodexCLI/CodexCLIResolver.swift) 先从进程 `PATH` 查找全局 `codex`,再检查 App 内置路径:+[`CodexCLIResolver.swift`](../../CodexBar/Services/CodexCLI/CodexCLIResolver.swift) 从进程 `PATH` 查找全局 `codex`,并检查 App 的资源目录:```text-/Applications/ChatGPT.app/Contents/Resources/codex-/Applications/Codex.app/Contents/Resources/codex+/Applications/ChatGPT.app/Contents/Resources```+先读取资源目录下的 `codex-cli/codex-package.json`,将 `entrypoint` 相对于 `codex-cli` 目录解析为可执行文件路径。清单无法解析或入口不可执行时,检查资源目录下的 `codex`。+菜单栏 App 从 Finder 启动时可能没有交互式 shell 的完整 `PATH`,因此 resolver 还会补充常见安装目录:```textdiff --git a/Docs/en/DeveloperGuide/app-server.md b/Docs/en/DeveloperGuide/app-server.mdindex e060560..d9004b8 100644--- a/Docs/en/DeveloperGuide/app-server.md+++ b/Docs/en/DeveloperGuide/app-server.md@@ -33,13 +33,14 @@ Settings reuses this session for Hook and TUI configuration.## Locating Codex CLI-[`CodexCLIResolver.swift`](../../../CodexBar/Services/CodexCLI/CodexCLIResolver.swift) first searches the process `PATH` for global `codex`, then checks bundled paths:+[`CodexCLIResolver.swift`](../../../CodexBar/Services/CodexCLI/CodexCLIResolver.swift) searches the process `PATH` for global `codex` and checks the app resource directory:```text-/Applications/ChatGPT.app/Contents/Resources/codex-/Applications/Codex.app/Contents/Resources/codex+/Applications/ChatGPT.app/Contents/Resources```+The resolver reads `codex-cli/codex-package.json` in the resource directory and resolves `entrypoint` relative to the `codex-cli` directory. If the manifest cannot be decoded or the entrypoint is not executable, it checks `codex` in the resource directory.+A menu bar app launched from Finder may lack the complete `PATH` of an interactive shell, so the resolver also adds common installation directories:```textdiff --git a/appcast.xml b/appcast.xmlindex 688d835..334aea1 100644--- a/appcast.xml+++ b/appcast.xml@@ -7,6 +7,15 @@<link>https://codexbar.zabrian.app/appcast.xml</link><description>CodexBar appcast feed</description><language>zh-CN</language>+ <item>+ <title>CodexBar.app 4.0.7</title>+ <sparkle:version>107</sparkle:version>+ <sparkle:shortVersionString>4.0.7</sparkle:shortVersionString>+ <sparkle:minimumSystemVersion>15.0</sparkle:minimumSystemVersion>+ <sparkle:releaseNotesLink>https://codexbar.zabrian.app/notes#4.0.7</sparkle:releaseNotesLink>+ <pubDate>Sun, 27 Sep 2026 09:21:21 +0800</pubDate>+ <enclosure url="https://codexbar.zabrian.app/download/CodexBar-v4.0.7.dmg" sparkle:edSignature="my1b6buFf1H9oCLCHiZmQ9pCycQyt5Te3IQ8rTzk/hh/yeVkdcqOCzRC+aIFbqZke7yybmDogEBDSqYkJ6xpBg==" length="8395264" type="application/octet-stream"/>+ </item><item><title>CodexBar.app 4.0.6</title><sparkle:version>106</sparkle:version>