-
Notifications
You must be signed in to change notification settings - Fork 694
feat: Auto-select provider based on active AI app #582
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
gamithasam
wants to merge
8
commits into
steipete:main
Choose a base branch
from
gamithasam:feature/auto-select-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
42fb21e
Add dynamic menu bar icon based on active AI app
gamithasam e0e23e6
Merge branch 'steipete:main' into feature/auto-select-provider
gamithasam b61359a
Select provider when auto-select triggers, not just update icon
gamithasam 35ad9a4
Merge branch 'feature/auto-select-provider' of https://github.com/gam…
gamithasam b3b0c79
Add support for Electron Ollama provider in ActiveAppDetector
gamithasam 9e58b5b
Add active provider mapping and settings persistence tests
gamithasam 361f481
Merge branch 'steipete:main' into feature/auto-select-provider
gamithasam 79dcb09
fix: sync selectedMenuProvider with icon in primaryProviderForUnified…
gamithasam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import AppKit | ||
| import CodexBarCore | ||
|
|
||
| /// Maps active application bundle identifiers to their corresponding UsageProvider. | ||
| /// Only providers with desktop apps can be detected; CLI-only and web-only providers return nil. | ||
| enum ActiveAppDetector { | ||
| /// Maps bundle identifier prefixes to their corresponding provider. | ||
| /// Order matters: more specific prefixes should come first. | ||
| private static let bundleIdToProvider: [(prefix: String, provider: UsageProvider)] = [ | ||
| // Desktop AI apps | ||
| ("com.openai.codex", .codex), | ||
| ("com.anthropic.claude", .claude), | ||
| ("com.cursor.sh", .cursor), | ||
| ("com.opencodeos.opencode", .opencode), | ||
| ("com.google.antigravity", .antigravity), | ||
| ("com.augmentcode.augment", .augment), | ||
| ("com.minimax.agent", .minimax), | ||
| ("dev.warp.Warp-Stable", .warp), | ||
| ("com.electron.ollama", .ollama), | ||
|
|
||
| // VS Code (GitHub Copilot - must check before generic JetBrains) | ||
| ("com.microsoft.VSCode", .copilot), | ||
|
|
||
| // JetBrains IDEs with Copilot support | ||
| ("com.jetbrains.", .copilot), | ||
|
|
||
| // Local AI servers (ollama runs locally, so it may show as active window) | ||
| ("com.ollama", .ollama), | ||
| ] | ||
|
|
||
| /// Detects the provider associated with the currently active frontmost application. | ||
| /// - Returns: The provider for the active AI app, or nil if no relevant AI app is active. | ||
| static func activeProvider() -> UsageProvider? { | ||
| guard let frontmostApp = NSWorkspace.shared.frontmostApplication, | ||
| let bundleId = frontmostApp.bundleIdentifier | ||
| else { | ||
| return nil | ||
| } | ||
|
|
||
| return self.provider(for: bundleId) | ||
| } | ||
|
|
||
| /// Looks up the provider for a given bundle identifier. | ||
| /// - Parameter bundleId: The application's bundle identifier. | ||
| /// - Returns: The corresponding provider, or nil if no match found. | ||
| static func provider(for bundleId: String) -> UsageProvider? { | ||
| // Try exact match first for special cases | ||
| for (prefix, provider) in self.bundleIdToProvider where bundleId == prefix { | ||
| return provider | ||
| } | ||
|
|
||
| // Then try prefix match (handles versioned bundle IDs like com.anthropic.claude-2) | ||
| for (prefix, provider) in self.bundleIdToProvider where bundleId.hasPrefix(prefix) { | ||
| return provider | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| import CodexBarCore | ||
| import Testing | ||
| @testable import CodexBar | ||
|
|
||
| struct ActiveAppDetectorTests { | ||
| @Test | ||
| func `provider maps expected exact and prefix bundle identifiers`() { | ||
| let cases: [(bundleID: String, expected: UsageProvider?)] = [ | ||
| // Codex | ||
| ("com.openai.codex", .codex), | ||
| ("com.openai.codex.desktop", .codex), | ||
| // Claude | ||
| ("com.anthropic.claude", .claude), | ||
| ("com.anthropic.claude-2", .claude), | ||
| // Copilot (VS Code and JetBrains) | ||
| ("com.microsoft.VSCode", .copilot), | ||
| ("com.microsoft.VSCodeInsiders", .copilot), | ||
| ("com.jetbrains.intellij", .copilot), | ||
| // Ollama | ||
| ("com.electron.ollama", .ollama), | ||
| ("com.ollama", .ollama), | ||
| ("com.ollama.desktop", .ollama), | ||
| // Unknown | ||
| ("com.apple.Safari", nil), | ||
| ] | ||
|
|
||
| for testCase in cases { | ||
| #expect(ActiveAppDetector.provider(for: testCase.bundleID) == testCase.expected) | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| func `provider returns nil for unknown bundle identifier`() { | ||
| #expect(ActiveAppDetector.provider(for: "com.example.unknown-ai-app") == nil) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
Tests/CodexBarTests/SettingsStoreActiveProviderTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import CodexBarCore | ||
| import Foundation | ||
| import Testing | ||
| @testable import CodexBar | ||
|
|
||
| @MainActor | ||
| struct SettingsStoreActiveProviderTests { | ||
| @Test | ||
| func `menu bar shows active provider defaults to false`() { | ||
| let settings = Self.makeSettingsStore(suite: "SettingsStoreActiveProviderTests-default") | ||
|
|
||
| #expect(settings.menuBarShowsActiveProvider == false) | ||
| } | ||
|
|
||
| @Test | ||
| func `menu bar shows active provider persists across instances`() throws { | ||
| let suite = "SettingsStoreActiveProviderTests-persist-active-toggle" | ||
| let defaultsA = try #require(UserDefaults(suiteName: suite)) | ||
| defaultsA.removePersistentDomain(forName: suite) | ||
| let configStore = testConfigStore(suiteName: suite) | ||
| let storeA = Self.makeSettingsStore(userDefaults: defaultsA, configStore: configStore) | ||
|
|
||
| storeA.menuBarShowsActiveProvider = true | ||
|
|
||
| let defaultsB = try #require(UserDefaults(suiteName: suite)) | ||
| let storeB = Self.makeSettingsStore(userDefaults: defaultsB, configStore: configStore) | ||
|
|
||
| #expect(storeB.menuBarShowsActiveProvider == true) | ||
| } | ||
|
|
||
| @Test | ||
| func `last active provider persists across instances`() throws { | ||
| let suite = "SettingsStoreActiveProviderTests-persist-last-provider" | ||
| let defaultsA = try #require(UserDefaults(suiteName: suite)) | ||
| defaultsA.removePersistentDomain(forName: suite) | ||
| let configStore = testConfigStore(suiteName: suite) | ||
| let storeA = Self.makeSettingsStore(userDefaults: defaultsA, configStore: configStore) | ||
|
|
||
| storeA.lastActiveProvider = .claude | ||
|
|
||
| let defaultsB = try #require(UserDefaults(suiteName: suite)) | ||
| let storeB = Self.makeSettingsStore(userDefaults: defaultsB, configStore: configStore) | ||
|
|
||
| #expect(storeB.lastActiveProvider == .claude) | ||
| } | ||
|
|
||
| @Test | ||
| func `last active provider invalid raw resolves nil and clearing removes persisted value`() throws { | ||
| let suite = "SettingsStoreActiveProviderTests-invalid-and-clear" | ||
| let defaults = try #require(UserDefaults(suiteName: suite)) | ||
| defaults.removePersistentDomain(forName: suite) | ||
| defaults.set("invalid-provider", forKey: "lastActiveProvider") | ||
|
|
||
| let configStore = testConfigStore(suiteName: suite) | ||
| let store = Self.makeSettingsStore(userDefaults: defaults, configStore: configStore) | ||
|
|
||
| #expect(store.lastActiveProvider == nil) | ||
|
|
||
| store.lastActiveProvider = .codex | ||
| #expect(defaults.string(forKey: "lastActiveProvider") == UsageProvider.codex.rawValue) | ||
|
|
||
| store.lastActiveProvider = nil | ||
| #expect(defaults.string(forKey: "lastActiveProvider") == nil) | ||
| } | ||
|
|
||
| private static func makeSettingsStore(suite: String) -> SettingsStore { | ||
| let defaults = UserDefaults(suiteName: suite)! | ||
| defaults.removePersistentDomain(forName: suite) | ||
| let configStore = testConfigStore(suiteName: suite) | ||
| return Self.makeSettingsStore(userDefaults: defaults, configStore: configStore) | ||
| } | ||
|
|
||
| private static func makeSettingsStore( | ||
| userDefaults: UserDefaults, | ||
| configStore: CodexBarConfigStore) -> SettingsStore | ||
| { | ||
| SettingsStore( | ||
| userDefaults: userDefaults, | ||
| configStore: configStore, | ||
| zaiTokenStore: NoopZaiTokenStore(), | ||
| syntheticTokenStore: NoopSyntheticTokenStore()) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.