diff --git a/.github/workflows/auto-close-pr.yml b/.github/workflows/auto-close-pr.yml index 752e32b3..90beda84 100644 --- a/.github/workflows/auto-close-pr.yml +++ b/.github/workflows/auto-close-pr.yml @@ -15,6 +15,7 @@ jobs: "At the moment we are not accepting contributions to the repository. Feedback for GitHub Copilot for Xcode can be given in the [Copilot community discussions](https://github.com/github/CopilotForXcode/discussions)." + if: ${{ !(startsWith(github.head_ref, 'release/') && github.event.pull_request.head.repo.full_name == github.repository) }} env: GH_REPO: ${{ github.repository }} GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/auto-create-release-pr.yml b/.github/workflows/auto-create-release-pr.yml new file mode 100644 index 00000000..e11d7d2a --- /dev/null +++ b/.github/workflows/auto-create-release-pr.yml @@ -0,0 +1,56 @@ +name: Auto-create Release PR + +on: + push: + branches: + - 'release/**' + +jobs: + create-pr: + runs-on: ubuntu-latest + permissions: + contents: write + pull-requests: write + steps: + - uses: actions/checkout@v4 + + - name: Create pull request + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + existing_pr_count="$(gh pr list \ + --state open \ + --base main \ + --head "${{ github.ref_name }}" \ + --json number \ + --jq 'length')" + if [ "${existing_pr_count}" -gt 0 ]; then + echo "Open pull request already exists for branch '${{ github.ref_name }}' into 'main'; skipping creation." + else + gh pr create \ + --title "$(git log -1 --pretty=%s)" \ + --body "Automated release PR." \ + --base main \ + --head "${{ github.ref_name }}" + fi + + - name: Approve pull request + env: + # PAT stored in github/CopilotForXcode, with write permissions to pull requests + GH_TOKEN: ${{ secrets.XCODE_AUTO_APPROVE }} + run: | + gh pr review --approve "${{ github.ref_name }}" + + - name: Wait for required checks + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh pr checks "${{ github.ref_name }}" --watch --required + + - name: Merge pull request + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh pr merge "${{ github.ref_name }}" \ + --merge \ + --delete-branch diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 78e35963..9c414bc1 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -27,6 +27,10 @@ jobs: fail-fast: false matrix: include: + - language: actions + build-mode: none + - language: javascript-typescript + build-mode: none - language: python build-mode: none - language: swift @@ -37,7 +41,7 @@ jobs: # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v3 + uses: github/codeql-action/init@v4 with: languages: ${{ matrix.language }} build-mode: ${{ matrix.build-mode }} @@ -66,6 +70,6 @@ jobs: CODE_SIGNING_ALLOWED="NO" - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v3 + uses: github/codeql-action/analyze@v4 with: category: "/language:${{matrix.language}}" diff --git a/CHANGELOG.md b/CHANGELOG.md index f380bf27..861c5037 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,31 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## 0.49.0 - May 15, 2026 +### Added +- Native Anthropic Messages API (`/v1/messages`) endpoint support. +- Thinking support in chat for reasoning-capable models. +- Enhanced rate limit notifications and error messages. + +### Changed +- Refined tool call item UI in agent progress: removed border and divider, repositioned chevron, and adjusted spacing for better readability. +- Updated Copilot language server to 1.465.5. + +## 0.48.0 - April 23, 2026 +### Added +- Context window usage details in chat, including a token breakdown for system instructions, messages, attached files, and tool results. +- Auto Compress setting to compact conversation history and save context tokens. +- Install flow for Xcode's built-in MCP server from settings. + +### Changed +- Custom agents and the Auto model are now generally available. +- Removed support for macOS 12. +- Improved UI for model picker tooltips. + +### Fixed +- Fixed an issue where GPT-5.4 requests could return a 400 error. +- Fixed an issue where the MCP allowlist did not work correctly. + ## 0.47.0 - February 4, 2026 ### Added - Auto approval for MCP tools, sensitive files, and terminal commands. diff --git a/CommunicationBridge/ServiceDelegate.swift b/CommunicationBridge/ServiceDelegate.swift index 4e289e57..6dbb0e0f 100644 --- a/CommunicationBridge/ServiceDelegate.swift +++ b/CommunicationBridge/ServiceDelegate.swift @@ -175,7 +175,7 @@ actor ExtensionServiceLauncher { return configuration }() ) { app, error in - if let error = error { + if error != nil { continuation.resume(returning: nil) } else { continuation.resume(returning: app) diff --git a/Copilot for Xcode.xcodeproj/project.pbxproj b/Copilot for Xcode.xcodeproj/project.pbxproj index c762e625..d232491a 100644 --- a/Copilot for Xcode.xcodeproj/project.pbxproj +++ b/Copilot for Xcode.xcodeproj/project.pbxproj @@ -845,7 +845,7 @@ "@executable_path/../Frameworks", "@executable_path/../../../../Frameworks", ); - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; MARKETING_VERSION = "$(APP_VERSION)"; PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE).EditorExtension"; PRODUCT_NAME = Copilot; @@ -874,7 +874,7 @@ "@executable_path/../Frameworks", "@executable_path/../../../../Frameworks", ); - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; MARKETING_VERSION = "$(APP_VERSION)"; PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE).EditorExtension"; PRODUCT_NAME = Copilot; @@ -936,7 +936,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; MTL_FAST_MATH = YES; ONLY_ACTIVE_ARCH = YES; @@ -991,7 +991,7 @@ GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; GCC_WARN_UNUSED_FUNCTION = YES; GCC_WARN_UNUSED_VARIABLE = YES; - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; MTL_ENABLE_DEBUG_INFO = NO; MTL_FAST_MATH = YES; SDKROOT = macosx; @@ -1022,7 +1022,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; MARKETING_VERSION = "$(APP_VERSION)"; PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE)"; PRODUCT_MODULE_NAME = Copilot_for_Xcode; @@ -1056,7 +1056,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; MARKETING_VERSION = "$(APP_VERSION)"; PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE)"; PRODUCT_NAME = "$(HOST_APP_NAME)"; @@ -1072,7 +1072,7 @@ CODE_SIGN_STYLE = Automatic; DEVELOPMENT_TEAM = VEKTX9H2N7; ENABLE_HARDENED_RUNTIME = YES; - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; SWIFT_VERSION = 5.0; @@ -1087,7 +1087,7 @@ DEVELOPMENT_TEAM = ""; "DEVELOPMENT_TEAM[sdk=macosx*]" = VEKTX9H2N7; ENABLE_HARDENED_RUNTIME = YES; - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; SKIP_INSTALL = YES; @@ -1117,7 +1117,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; MARKETING_VERSION = "$(APP_VERSION)"; PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE).ExtensionService"; PRODUCT_NAME = "$(EXTENSION_SERVICE_NAME)"; @@ -1151,7 +1151,7 @@ "$(inherited)", "@executable_path/../Frameworks", ); - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; MARKETING_VERSION = "$(APP_VERSION)"; PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE).ExtensionService"; PRODUCT_NAME = "$(EXTENSION_SERVICE_NAME)"; @@ -1172,7 +1172,7 @@ ENABLE_USER_SCRIPT_SANDBOXING = YES; GCC_C_LANGUAGE_STANDARD = gnu17; LOCALIZATION_PREFERS_STRING_CATALOGS = YES; - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE).CommunicationBridge"; PRODUCT_NAME = "$(TARGET_NAME)"; SKIP_INSTALL = YES; @@ -1193,7 +1193,7 @@ ENABLE_USER_SCRIPT_SANDBOXING = YES; GCC_C_LANGUAGE_STANDARD = gnu17; LOCALIZATION_PREFERS_STRING_CATALOGS = YES; - MACOSX_DEPLOYMENT_TARGET = 12.0; + MACOSX_DEPLOYMENT_TARGET = 13.0; PRODUCT_BUNDLE_IDENTIFIER = "$(BUNDLE_IDENTIFIER_BASE).CommunicationBridge"; PRODUCT_NAME = "$(TARGET_NAME)"; PROVISIONING_PROFILE_SPECIFIER = ""; diff --git a/Copilot for Xcode/App.swift b/Copilot for Xcode/App.swift index 0da62760..d45adb9f 100644 --- a/Copilot for Xcode/App.swift +++ b/Copilot for Xcode/App.swift @@ -26,19 +26,15 @@ class AppDelegate: NSObject, NSApplicationDelegate { } func applicationDidFinishLaunching(_ notification: Notification) { - if #available(macOS 13.0, *) { - checkBackgroundPermissions() - } - + checkBackgroundPermissions() + let launchMode = determineLaunchMode() handleLaunchMode(launchMode) } func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { - if #available(macOS 13.0, *) { - checkBackgroundPermissions() - } - + checkBackgroundPermissions() + let launchMode = determineLaunchMode() handleLaunchMode(launchMode) return true @@ -113,7 +109,6 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } - @available(macOS 13.0, *) private func checkBackgroundPermissions() { Task { // Direct check of permission status @@ -122,7 +117,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { if !isPermissionGranted { // Only show alert if permission isn't granted - DispatchQueue.main.async { + await MainActor.run { if !self.permissionAlertShown { showBackgroundPermissionAlert() self.permissionAlertShown = true @@ -130,7 +125,9 @@ class AppDelegate: NSObject, NSApplicationDelegate { } } else { // Permission is granted, reset flag - self.permissionAlertShown = false + await MainActor.run { + self.permissionAlertShown = false + } } } } @@ -147,7 +144,7 @@ class AppDelegate: NSObject, NSApplicationDelegate { // Start cleanup in background without waiting Task { - let quitTask = Task { + _ = Task { let service = try? getService() try? await service?.quitService() } @@ -272,10 +269,8 @@ func activateAndOpenSettings() { if #available(macOS 14.0, *) { let environment = SettingsEnvironment() environment.open() - } else if #available(macOS 13.0, *) { - NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil) } else { - NSApp.sendAction(Selector(("showPreferencesWindow:")), to: nil, from: nil) + NSApp.sendAction(Selector(("showSettingsWindow:")), to: nil, from: nil) } } diff --git a/Core/Package.swift b/Core/Package.swift index 8e2e58cc..70c40b3c 100644 --- a/Core/Package.swift +++ b/Core/Package.swift @@ -8,7 +8,7 @@ import PackageDescription let package = Package( name: "Core", - platforms: [.macOS(.v12)], + platforms: [.macOS(.v13)], products: [ .library( name: "Service", @@ -75,6 +75,7 @@ let package = Package( dependencies: [ "SuggestionWidget", "SuggestionService", + "SuggestionInjector", "ChatService", "PromptToCodeService", "ConversationTab", @@ -123,6 +124,7 @@ let package = Package( "Client", "LaunchAgentManager", "GitHubCopilotViewModel", + "UpdateChecker", .product(name: "SuggestionProvider", package: "Tool"), .product(name: "Toast", package: "Tool"), .product(name: "SharedUIComponents", package: "Tool"), @@ -202,6 +204,7 @@ let package = Package( name: "ConversationTab", dependencies: [ "ChatService", + "GitHubCopilotViewModel", .product(name: "SharedUIComponents", package: "Tool"), .product(name: "ChatAPIService", package: "Tool"), .product(name: "Logger", package: "Tool"), @@ -225,6 +228,7 @@ let package = Package( "ConversationTab", "GitHubCopilotViewModel", "PersistMiddleware", + .product(name: "CGEventOverride", package: "CGEventOverride"), .product(name: "GitHubCopilotService", package: "Tool"), .product(name: "Toast", package: "Tool"), .product(name: "UserDefaultsObserver", package: "Tool"), diff --git a/Core/Sources/ChatService/ChatService.swift b/Core/Sources/ChatService/ChatService.swift index ac75d819..d9dcf0cd 100644 --- a/Core/Sources/ChatService/ChatService.swift +++ b/Core/Sources/ChatService/ChatService.swift @@ -64,11 +64,14 @@ public final class ChatService: ChatServiceType, ObservableObject { public var memory: ContextAwareAutoManagedChatMemory @Published public internal(set) var chatHistory: [ChatMessage] = [] @Published public internal(set) var isReceivingMessage = false + @Published public internal(set) var isSummarizingConversation = false @Published public internal(set) var fileEditMap: OrderedDictionary = [:] + @Published public internal(set) var contextSizeInfo: ContextSizeInfo? = nil public internal(set) var requestType: RequestType? = nil public private(set) var chatTabInfo: ChatTabInfo private let conversationProvider: ConversationServiceProvider? private let conversationProgressHandler: ConversationProgressHandler + private let compressionHandler: CompressionHandler private let conversationContextHandler: ConversationContextHandler = ConversationContextHandlerImpl.shared // sync all the files in the workspace to watch for changes. private let watchedFilesHandler: WatchedFilesHandler = WatchedFilesHandlerImpl.shared @@ -81,14 +84,25 @@ public final class ChatService: ChatServiceType, ObservableObject { private var pendingToolCallRequests: [String: ToolCallRequest] = [:] // Workaround: toolConfirmation request does not have parent turnId private var conversationTurnTracking = ConversationTurnTrackingState() + + /// Single source of truth for an in-flight streaming thinking block. Sealed when the turn ends + /// or a non-thinking payload arrives. `clientEntryId` is stable across server delta `id` churn. + private struct ActiveThinkingCursor { + let clientEntryId: UUID + let targetMessageId: String + let originTurnId: String + } + private var activeThinking: ActiveThinkingCursor? = nil init(provider: any ConversationServiceProvider, memory: ContextAwareAutoManagedChatMemory = ContextAwareAutoManagedChatMemory(), conversationProgressHandler: ConversationProgressHandler = ConversationProgressHandlerImpl.shared, + compressionHandler: CompressionHandler = CompressionHandlerImpl.shared, chatTabInfo: ChatTabInfo) { self.memory = memory self.conversationProvider = provider self.conversationProgressHandler = conversationProgressHandler + self.compressionHandler = compressionHandler self.chatTabInfo = chatTabInfo memory.chatService = self @@ -134,6 +148,19 @@ public final class ChatService: ChatServiceType, ObservableObject { conversationProgressHandler.onEnd.sink { [weak self] (token, progress) in self?.handleProgressEnd(token: token, progress: progress) }.store(in: &cancellables) + + compressionHandler.onCompressionStarted.sink { [weak self] compressionConversationId in + guard let self, self.conversationId == compressionConversationId else { return } + self.isSummarizingConversation = true + }.store(in: &cancellables) + + compressionHandler.onCompressionCompleted.sink { [weak self] completedNotification in + guard let self, self.conversationId == completedNotification.conversationId else { return } + self.isSummarizingConversation = false + if let contextInfo = completedNotification.contextInfo { + self.contextSizeInfo = contextInfo + } + }.store(in: &cancellables) } private func subscribeToConversationContextRequest() { @@ -223,14 +250,24 @@ public final class ChatService: ChatServiceType, ObservableObject { // this will be triggerred in conversation tab if needed public func restoreIfNeeded() { guard self.isRestored == false else { return } - + Task { - let storedChatMessages = fetchAllChatMessagesFromStorage() + var storedChatMessages = fetchAllChatMessagesFromStorage() + // Force-seal any thinking entries that were persisted mid-stream (e.g. app crashed + // before the seal sweep ran). Otherwise they'd render with the placeholder "Thinking" + // title forever. + for messageIndex in storedChatMessages.indices where storedChatMessages[messageIndex].role == .assistant { + for path in Self.allThinkingPaths(in: storedChatMessages[messageIndex]) { + Self.mutateThinking(at: path, in: &storedChatMessages[messageIndex]) { entry in + if !entry.isComplete { entry.isComplete = true } + } + } + } await mutateHistory { history in history.append(contentsOf: storedChatMessages) } } - + self.isRestored = true } @@ -745,7 +782,11 @@ public final class ChatService: ChatServiceType, ObservableObject { guard let workDownToken = activeRequestId, workDownToken == token else { return } - + + if let contextSize = progress.contextSize { + self.contextSizeInfo = contextSize + } + let id = progress.turnId var content = "" var references: [ConversationReference] = [] @@ -756,28 +797,68 @@ public final class ChatService: ChatServiceType, ObservableObject { if let reply = progress.reply { content = reply } - + if let progressReferences = progress.references, !progressReferences.isEmpty { references = progressReferences.toConversationReferences() } - + if let progressSteps = progress.steps, !progressSteps.isEmpty { steps = progressSteps } - + if let progressAgentRounds = progress.editAgentRounds, !progressAgentRounds.isEmpty { editAgentRounds = progressAgentRounds } - - if content.isEmpty && references.isEmpty && steps.isEmpty && editAgentRounds.isEmpty && parentTurnId == nil { + + let progressThinkingDelta = progress.thinking + let hasThinking = !(progressThinkingDelta?.text?.allSatisfy { $0.isEmpty } ?? true) + let hasNonThinking = !content.isEmpty || !references.isEmpty || !steps.isEmpty || !editAgentRounds.isEmpty + + // Resolve the in-flight cursor against this event. The cursor is sealed when the active + // turn changes, or when a non-thinking payload arrives signalling that reasoning has + // ended and the model is now speaking/acting. + if let cursor = activeThinking, cursor.originTurnId != id { + sealActiveThinking() + } + if !hasThinking, hasNonThinking, activeThinking != nil { + sealActiveThinking() + } + + if content.isEmpty && references.isEmpty && steps.isEmpty && editAgentRounds.isEmpty && parentTurnId == nil && !hasThinking { return } - + let messageContent = content let messageReferences = references let messageSteps = steps - let messageAgentRounds = editAgentRounds + var messageAgentRounds = editAgentRounds let messageParentTurnId = parentTurnId + var messageThinking: [MessageThinking] = [] + + if hasThinking, let progressThinkingDelta { + // Open a cursor on the first delta of a streaming block. Subsequent deltas reuse the + // same `clientEntryId` so `mergeThinking` concatenates into one entry even when the + // server's `id` changes mid-stream. + let cursor = activeThinking ?? { + let opened = ActiveThinkingCursor( + clientEntryId: UUID(), + targetMessageId: parentTurnId ?? id, + originTurnId: id + ) + activeThinking = opened + return opened + }() + let entry = MessageThinking(from: progressThinkingDelta, clientEntryId: cursor.clientEntryId) + // Route the entry: into the last agent round when this event carries one (mid-tool-loop + // reasoning, including sub-agent rounds), otherwise onto the message itself (pre-tool + // reasoning). For sub-agent events, ChatMemory.appendMessage's parent-turn merge will + // forward the round's thinking into the parent's last sub-round via `mergeThinking`. + if let lastIndex = messageAgentRounds.indices.last { + messageAgentRounds[lastIndex].thinking.append(entry) + } else { + messageThinking = [entry] + } + } Task { let message = ChatMessage( @@ -787,6 +868,7 @@ public final class ChatService: ChatServiceType, ObservableObject { references: messageReferences, steps: messageSteps, editAgentRounds: messageAgentRounds, + thinking: messageThinking, parentTurnId: messageParentTurnId, turnStatus: .inProgress ) @@ -795,8 +877,132 @@ public final class ChatService: ChatServiceType, ObservableObject { } } + /// Seals the cursor's entry: marks it `isComplete`, persists the owning message, and kicks off + /// the LSP title-generation request. Looking up by `clientEntryId` (set when the cursor was + /// opened) makes this independent of the server's per-delta `id` and of which location the + /// entry was routed to (top-level message, agent round, or sub-agent round). + private func sealActiveThinking() { + guard let cursor = activeThinking else { return } + activeThinking = nil + Task { + var sealedText: String? = nil + var sealedMessage: ChatMessage? = nil + await memory.mutateHistory { history in + guard let messageIndex = history.firstIndex(where: { $0.id == cursor.targetMessageId }), + history[messageIndex].role == .assistant, + let path = Self.findThinkingPath(clientEntryId: cursor.clientEntryId, in: history[messageIndex]) + else { return } + Self.mutateThinking(at: path, in: &history[messageIndex]) { entry in + guard !entry.isComplete else { return } + entry.isComplete = true + if let text = entry.text?.joined(), !text.isEmpty { + sealedText = text + } + } + sealedMessage = history[messageIndex] + } + if let sealedMessage { + saveChatMessageToStorage(sealedMessage) + } + guard let sealedText else { return } + await requestThinkingTitle(for: sealedText, cursor: cursor) + } + } + + private func requestThinkingTitle(for thinkingText: String, cursor: ActiveThinkingCursor) async { + let extractedTitles = MessageThinking.parseSections(from: thinkingText).compactMap { $0.title } + let params = GenerateThinkingTitleParams( + thinkingContent: extractedTitles.isEmpty ? thinkingText : nil, + extractedTitles: extractedTitles.isEmpty ? nil : extractedTitles + ) + do { + guard let response = try await conversationProvider?.generateThinkingTitle(params), + !response.title.isEmpty else { return } + let trimmed = response.title.trimmingCharacters(in: .whitespacesAndNewlines) + let title = trimmed.count > 80 ? String(trimmed.prefix(80)) + "\u{2026}" : trimmed + guard !title.isEmpty else { return } + var titledMessage: ChatMessage? = nil + await memory.mutateHistory { history in + guard let messageIndex = history.firstIndex(where: { $0.id == cursor.targetMessageId }), + history[messageIndex].role == .assistant, + let path = Self.findThinkingPath(clientEntryId: cursor.clientEntryId, in: history[messageIndex]) + else { return } + Self.mutateThinking(at: path, in: &history[messageIndex]) { $0.title = title } + titledMessage = history[messageIndex] + } + if let titledMessage { + saveChatMessageToStorage(titledMessage) + } + } catch { + Logger.gitHubCopilot.debug("Failed to generate thinking title: \(error)") + } + } + + /// Path to a `MessageThinking` entry inside an assistant `ChatMessage`. Covers the three + /// places thinking can live: top-level on the message, on an agent round, or on a sub-agent + /// round under an agent round. + private enum ThinkingPath { + case message(entryIndex: Int) + case round(roundIndex: Int, entryIndex: Int) + case subRound(roundIndex: Int, subRoundIndex: Int, entryIndex: Int) + } + + private static func findThinkingPath(clientEntryId: UUID, in message: ChatMessage) -> ThinkingPath? { + let predicate: (MessageThinking) -> Bool = { $0.clientEntryId == clientEntryId } + if let entryIndex = message.thinking.firstIndex(where: predicate) { + return .message(entryIndex: entryIndex) + } + for (roundIndex, round) in message.editAgentRounds.enumerated() { + if let entryIndex = round.thinking.firstIndex(where: predicate) { + return .round(roundIndex: roundIndex, entryIndex: entryIndex) + } + for (subRoundIndex, subRound) in (round.subAgentRounds ?? []).enumerated() { + if let entryIndex = subRound.thinking.firstIndex(where: predicate) { + return .subRound(roundIndex: roundIndex, subRoundIndex: subRoundIndex, entryIndex: entryIndex) + } + } + } + return nil + } + + /// All `ThinkingPath`s in the message, in stable visit order. Used by sweeps that need to + /// touch every entry without knowing the cursor's `clientEntryId`. + private static func allThinkingPaths(in message: ChatMessage) -> [ThinkingPath] { + var paths: [ThinkingPath] = [] + for entryIndex in message.thinking.indices { + paths.append(.message(entryIndex: entryIndex)) + } + for (roundIndex, round) in message.editAgentRounds.enumerated() { + for entryIndex in round.thinking.indices { + paths.append(.round(roundIndex: roundIndex, entryIndex: entryIndex)) + } + for (subRoundIndex, subRound) in (round.subAgentRounds ?? []).enumerated() { + for entryIndex in subRound.thinking.indices { + paths.append(.subRound(roundIndex: roundIndex, subRoundIndex: subRoundIndex, entryIndex: entryIndex)) + } + } + } + return paths + } + + private static func mutateThinking(at path: ThinkingPath, in message: inout ChatMessage, _ mutate: (inout MessageThinking) -> Void) { + switch path { + case .message(let entryIndex): + mutate(&message.thinking[entryIndex]) + case .round(let roundIndex, let entryIndex): + mutate(&message.editAgentRounds[roundIndex].thinking[entryIndex]) + case .subRound(let roundIndex, let subRoundIndex, let entryIndex): + guard var subRounds = message.editAgentRounds[roundIndex].subAgentRounds else { return } + mutate(&subRounds[subRoundIndex].thinking[entryIndex]) + message.editAgentRounds[roundIndex].subAgentRounds = subRounds + } + } + private func handleProgressEnd(token: String, progress: ConversationProgressEnd) { guard let workDoneToken = activeRequestId, workDoneToken == token else { return } + + sealActiveThinking() + let followUp = progress.followUp if let CLSError = progress.error { @@ -880,7 +1086,7 @@ public final class ChatService: ChatServiceType, ObservableObject { Task { let message = ChatMessage( - assistantMessageWithId: progress.turnId, + assistantMessageWithId: progress.turnId, chatTabID: chatTabInfo.id, followUp: followUp, suggestedTitle: progress.suggestedTitle, @@ -895,8 +1101,13 @@ public final class ChatService: ChatServiceType, ObservableObject { private func resetOngoingRequest(with turnStatus: ChatMessage.TurnStatus = .success) { activeRequestId = nil isReceivingMessage = false + isSummarizingConversation = false requestType = nil - + // The cursor is normally cleared by sealActiveThinking() in handleProgressEnd; clear it + // here as a safety net for cancellation/error paths that bypass the end handler. The + // belt-and-suspenders sweep below catches any orphan unsealed entries. + activeThinking = nil + // Clear turn tracking data conversationTurnTracking.reset() @@ -929,6 +1140,15 @@ public final class ChatService: ChatServiceType, ObservableObject { history[lastIndex].steps[i].status = .cancelled } } + + // Belt-and-suspenders: mark any orphan unsealed thinking complete on turn end. The + // cursor seal in handleProgressEnd handles the normal path; this catches cancel/ + // error cases that bypass it. + for path in Self.allThinkingPaths(in: history[lastIndex]) { + Self.mutateThinking(at: path, in: &history[lastIndex]) { entry in + if !entry.isComplete { entry.isComplete = true } + } + } for i in 0.. Bool { diff --git a/Core/Sources/ChatService/ToolCalls/AutoApproval/AutoApprovalScope.swift b/Core/Sources/ChatService/ToolCalls/AutoApproval/AutoApprovalScope.swift index d3a47556..8be738f6 100644 --- a/Core/Sources/ChatService/ToolCalls/AutoApproval/AutoApprovalScope.swift +++ b/Core/Sources/ChatService/ToolCalls/AutoApproval/AutoApprovalScope.swift @@ -2,7 +2,7 @@ import Foundation public typealias ConversationID = String -public enum AutoApprovalScope: Hashable { +public enum AutoApprovalScope: Hashable, Sendable { case session(ConversationID) /// Applies to all workspaces. Persisted in `UserDefaults.autoApproval`. case global diff --git a/Core/Sources/ConversationTab/Chat.swift b/Core/Sources/ConversationTab/Chat.swift index e6ca55e0..2c727bfc 100644 --- a/Core/Sources/ConversationTab/Chat.swift +++ b/Core/Sources/ConversationTab/Chat.swift @@ -30,6 +30,7 @@ public struct DisplayedChatMessage: Equatable { public var suggestedTitle: String? = nil public var errorMessages: [String] = [] public var steps: [ConversationProgressStep] = [] + public var thinking: [MessageThinking] = [] public var editAgentRounds: [AgentRound] = [] public var parentTurnId: String? = nil public var panelMessages: [CopilotShowMessageParams] = [] @@ -50,6 +51,7 @@ public struct DisplayedChatMessage: Equatable { suggestedTitle: String? = nil, errorMessages: [String] = [], steps: [ConversationProgressStep] = [], + thinking: [MessageThinking] = [], editAgentRounds: [AgentRound] = [], parentTurnId: String? = nil, panelMessages: [CopilotShowMessageParams] = [], @@ -69,6 +71,7 @@ public struct DisplayedChatMessage: Equatable { self.suggestedTitle = suggestedTitle self.errorMessages = errorMessages self.steps = steps + self.thinking = thinking self.editAgentRounds = editAgentRounds self.parentTurnId = parentTurnId self.panelMessages = panelMessages @@ -295,16 +298,22 @@ struct Chat { struct ConversationState: Equatable { var history: [DisplayedChatMessage] var isReceivingMessage: Bool + var isSummarizingConversation: Bool var requestType: RequestType? + var contextSizeInfo: ContextSizeInfo? init( history: [DisplayedChatMessage] = [], isReceivingMessage: Bool = false, - requestType: RequestType? = nil + isSummarizingConversation: Bool = false, + requestType: RequestType? = nil, + contextSizeInfo: ContextSizeInfo? = nil ) { self.history = history self.isReceivingMessage = isReceivingMessage + self.isSummarizingConversation = isSummarizingConversation self.requestType = requestType + self.contextSizeInfo = contextSizeInfo } func subsequentMessages(after messageId: MessageID) -> [DisplayedChatMessage] { @@ -454,11 +463,21 @@ struct Chat { set { conversation.isReceivingMessage = newValue } } + var isSummarizingConversation: Bool { + get { conversation.isSummarizingConversation } + set { conversation.isSummarizingConversation = newValue } + } + var requestType: RequestType? { get { conversation.requestType } set { conversation.requestType = newValue } } + var contextSizeInfo: ContextSizeInfo? { + get { conversation.contextSizeInfo } + set { conversation.contextSizeInfo = newValue } + } + var handOffClicked: Bool { get { editor.handOffClicked } set { editor.handOffClicked = newValue } @@ -590,10 +609,12 @@ struct Chat { case observeHistoryChange case observeIsReceivingMessageChange case observeFileEditChange + case observeContextSizeInfoChange case historyChanged case isReceivingMessageChanged case fileEditChanged + case contextSizeInfoChanged case chatMenu(ChatMenu.Action) @@ -651,6 +672,7 @@ struct Chat { case observeIsReceivingMessageChange(UUID) case sendMessage(UUID) case observeFileEditChange(UUID) + case observeContextSizeInfoChange(UUID) case observeFixErrorNotification(UUID) } @@ -942,6 +964,7 @@ struct Chat { await send(.observeHistoryChange) await send(.observeIsReceivingMessageChange) await send(.observeFileEditChange) + await send(.observeContextSizeInfoChange) } case .observeHistoryChange: @@ -967,6 +990,7 @@ struct Chat { return .run { send in let stream = AsyncStream { continuation in let cancellable = service.$isReceivingMessage + .merge(with: service.$isSummarizingConversation) .sink { _ in continuation.yield() } @@ -1001,6 +1025,25 @@ struct Chat { cancelInFlight: true ) + case .observeContextSizeInfoChange: + return .run { send in + let stream = AsyncStream { continuation in + let cancellable = service.$contextSizeInfo + .sink { _ in + continuation.yield() + } + continuation.onTermination = { _ in + cancellable.cancel() + } + } + for await _ in stream { + await send(.contextSizeInfoChanged) + } + }.cancellable( + id: CancelID.observeContextSizeInfoChange(id), + cancelInFlight: true + ) + case .historyChanged: state.history = service.chatHistory.flatMap { message in var all = [DisplayedChatMessage]() @@ -1027,6 +1070,7 @@ struct Chat { suggestedTitle: message.suggestedTitle, errorMessages: message.errorMessages, steps: message.steps, + thinking: message.thinking, editAgentRounds: message.editAgentRounds, parentTurnId: message.parentTurnId, panelMessages: message.panelMessages, @@ -1045,9 +1089,14 @@ struct Chat { case .isReceivingMessageChanged: state.isReceivingMessage = service.isReceivingMessage + state.isSummarizingConversation = service.isSummarizingConversation state.requestType = service.requestType return .none - + + case .contextSizeInfoChanged: + state.conversation.contextSizeInfo = service.contextSizeInfo + return .none + case .fileEditChanged: state.fileEditMap = service.fileEditMap let fileEditMap = state.fileEditMap @@ -1185,7 +1234,7 @@ struct Chat { return .none // MARK: - Code Review - case let .codeReview(.request(group)): + case .codeReview(.request(_)): return .run { send in await send(.discardCheckPoint) } diff --git a/Core/Sources/ConversationTab/ChatPanel.swift b/Core/Sources/ConversationTab/ChatPanel.swift index f4794206..f866156f 100644 --- a/Core/Sources/ConversationTab/ChatPanel.swift +++ b/Core/Sources/ConversationTab/ChatPanel.swift @@ -23,6 +23,7 @@ private let r: Double = 4 public struct ChatPanel: View { @Perception.Bindable var chat: StoreOf @Namespace var inputAreaNamespace + @ObservedObject private var rateLimitNotifier = RateLimitNotifierImpl.shared public var body: some View { WithPerceptionTracking { @@ -55,12 +56,20 @@ public struct ChatPanel: View { } } + if let warning = rateLimitNotifier.currentWarning { + RateLimitWarningBanner(message: warning.message) { + rateLimitNotifier.dismissWarning() + } + .scaledPadding(.horizontal, 24) + .scaledPadding(.vertical, 8) + } + if chat.fileEditMap.count > 0 { WorkingSetView(chat: chat) .dimWithExitEditMode(chat) .scaledPadding(.horizontal, 24) } - + ChatPanelInputArea(chat: chat, r: r, editorMode: .input) .dimWithExitEditMode(chat) .scaledPadding(.horizontal, 16) @@ -135,6 +144,36 @@ private struct ListHeightPreferenceKey: PreferenceKey { } } +private struct ScrollViewConfigurator: NSViewRepresentable { + let configure: (NSScrollView) -> Void + + final class Coordinator { + var didConfigure = false + } + + func makeCoordinator() -> Coordinator { Coordinator() } + + func makeNSView(context: Context) -> NSView { + let view = NSView() + applyOnce(view: view, coordinator: context.coordinator) + return view + } + + func updateNSView(_ nsView: NSView, context: Context) { + applyOnce(view: nsView, coordinator: context.coordinator) + } + + private func applyOnce(view: NSView, coordinator: Coordinator) { + guard !coordinator.didConfigure else { return } + DispatchQueue.main.async { + guard !coordinator.didConfigure, + let scrollView = view.enclosingScrollView else { return } + coordinator.didConfigure = true + configure(scrollView) + } + } +} + struct ChatPanelMessages: View { let chat: StoreOf @State var cancellable = Set() @@ -154,17 +193,34 @@ struct ChatPanelMessages: View { WithPerceptionTracking { ScrollViewReader { proxy in GeometryReader { listGeo in - List { - Group { + ScrollView(.vertical, showsIndicators: true) { + // VStack with a flexible trailing Spacer absorbs empty space when + // content is shorter than the viewport, so content stays naturally + // top-aligned. When content grows past the viewport, the Spacer + // collapses to its minLength and the VStack overflows the + // ScrollView's content area as expected. This avoids the List's + // remembered-bottom-anchor behavior that pushed earlier content up + // whenever a child view's height changed. + VStack(alignment: .leading, spacing: 0) { + ScrollViewConfigurator { scrollView in + scrollView.scrollerStyle = .overlay + scrollView.verticalScroller?.scrollerStyle = .overlay + scrollView.autohidesScrollers = true + } + .frame(width: 0, height: 0) + + Color.clear + .frame(height: 1) + .id(topID) ChatHistory(chat: chat) .fixedSize(horizontal: false, vertical: true) ExtraSpacingInResponding(chat: chat) - Spacer(minLength: 12) + Color.clear + .frame(height: 12) .id(bottomID) - .listRowInsets(EdgeInsets()) .onAppear { isBottomHidden = false if !didScrollToBottomOnAppearOnce { @@ -182,25 +238,16 @@ struct ChatPanelMessages: View { value: offset ) }) + + Spacer(minLength: 0) } - .modify { view in - if #available(macOS 13.0, *) { - view - .listRowSeparator(.hidden) - } else { - view - } - } - } - .listStyle(.plain) - .scaledPadding(.leading, 8) - .listRowBackground(EmptyView()) - .modify { view in - if #available(macOS 13.0, *) { - view.scrollContentBackground(.hidden) - } else { - view - } + .frame( + minWidth: 0, + maxWidth: .infinity, + minHeight: listGeo.size.height, + alignment: .topLeading + ) + .scaledPadding(.horizontal, 16) } .coordinateSpace(name: scrollSpace) .preference( diff --git a/Core/Sources/ConversationTab/ModeAndModelPicker/ModeAndModelPickerPicker.swift b/Core/Sources/ConversationTab/ModeAndModelPicker/ModeAndModelPickerPicker.swift index ec1abafb..7e03aad1 100644 --- a/Core/Sources/ConversationTab/ModeAndModelPicker/ModeAndModelPickerPicker.swift +++ b/Core/Sources/ConversationTab/ModeAndModelPicker/ModeAndModelPickerPicker.swift @@ -13,13 +13,10 @@ struct ModeAndModelPicker: View { @Binding var selectedAgent: ConversationMode @State private var selectedModel: LLMModel? - @State private var isHovered = false - @State private var isPressed = false @ObservedObject private var modelManager = CopilotModelManagerObservable.shared static var lastRefreshModelsTime: Date = .init(timeIntervalSince1970: 0) @State private var chatMode = "Ask" - @State private var isAgentPickerHovered = false // Separate caches for both scopes @State private var askScopeCache: ScopeCache = ScopeCache() @@ -27,14 +24,7 @@ struct ModeAndModelPicker: View { @State var isMCPFFEnabled: Bool @State var isBYOKFFEnabled: Bool - @State var isEditorPreviewEnabled: Bool @State private var cancellables = Set() - - @StateObject private var fontScaleManager = FontScaleManager.shared - - var fontScale: Double { - fontScaleManager.currentScale - } let attributes: [NSAttributedString.Key: NSFont] = ModelMenuItemFormatter.attributes @@ -46,7 +36,6 @@ struct ModeAndModelPicker: View { self._selectedModel = State(initialValue: initialModel) self.isMCPFFEnabled = FeatureFlagNotifierImpl.shared.featureFlags.mcp self.isBYOKFFEnabled = FeatureFlagNotifierImpl.shared.featureFlags.byok - self.isEditorPreviewEnabled = FeatureFlagNotifierImpl.shared.featureFlags.editorPreviewFeatures updateAgentPicker() } @@ -54,7 +43,6 @@ struct ModeAndModelPicker: View { FeatureFlagNotifierImpl.shared.featureFlagsDidChange.sink(receiveValue: { featureFlags in isMCPFFEnabled = featureFlags.mcp isBYOKFFEnabled = featureFlags.byok - isEditorPreviewEnabled = featureFlags.editorPreviewFeatures }) .store(in: &cancellables) } @@ -78,26 +66,11 @@ struct ModeAndModelPicker: View { AppState.shared.isAgentModeEnabled() ? agentScopeCache : askScopeCache } - // Helper method to format multiplier text - func formatMultiplierText(for billing: CopilotModelBilling?) -> String { - guard let billingInfo = billing else { return "" } - - let multiplier = billingInfo.multiplier - if multiplier == 0 { - return "Included" - } else { - let numberPart = multiplier.truncatingRemainder(dividingBy: 1) == 0 - ? String(format: "%.0f", multiplier) - : String(format: "%.2f", multiplier) - return "\(numberPart)x" - } - } - // Update cache for specific scope only if models changed func updateModelCacheIfNeeded(for scope: PromptTemplateScope) { - let currentModels = scope == .agentPanel ? - modelManager.availableAgentModels + modelManager.availableAgentBYOKModels : - modelManager.availableChatModels + modelManager.availableChatBYOKModels + let clsModels = scope == .agentPanel ? modelManager.availableAgentModels : modelManager.availableChatModels + let byokModels = isBYOKFFEnabled ? (scope == .agentPanel ? modelManager.availableAgentBYOKModels : modelManager.availableChatBYOKModels) : [] + let currentModels = clsModels + byokModels let modelsHash = currentModels.hashValue if scope == .agentPanel { @@ -143,28 +116,13 @@ struct ModeAndModelPicker: View { allAvailableModels += byokModels } - // If editor preview is disabled and current model is auto, switch away from it - if !isEditorPreviewEnabled && currentModel?.isAutoModel == true { - // Try default model first - if let defaultModel = defaultModel, !defaultModel.isAutoModel { - AppState.shared.setSelectedModel(defaultModel) - selectedModel = defaultModel - return - } - // If default is also auto, use first non-auto available model - if let firstNonAuto = allAvailableModels.first(where: { !$0.isAutoModel }) { - AppState.shared.setSelectedModel(firstNonAuto) - selectedModel = firstNonAuto - return - } - } - - // Check if current model exists in available models for current scope using model comparison - let modelExists = allAvailableModels.contains { model in + // Find the fresh model from available models that matches the persisted selection. + // This ensures transient fields like degradationReason stay up to date. + let freshModel = allAvailableModels.first { model in model == currentModel } - - if !modelExists && currentModel != nil { + + if freshModel == nil && currentModel != nil { // Switch to default model if current model is not available if let fallbackModel = defaultModel { AppState.shared.setSelectedModel(fallbackModel) @@ -177,7 +135,7 @@ struct ModeAndModelPicker: View { selectedModel = nil } } else { - selectedModel = currentModel ?? defaultModel + selectedModel = freshModel ?? defaultModel } } @@ -258,85 +216,6 @@ struct ModeAndModelPicker: View { } } - // Model picker menu component - private var modelPickerMenu: some View { - Menu { - // Group models by premium status - let premiumModels = copilotModels.filter { $0.isPremiumModel } - let standardModels = copilotModels.filter { - $0.isStandardModel && !$0.isAutoModel - } - let autoModel = isEditorPreviewEnabled ? copilotModels.first(where: { $0.isAutoModel }) : nil - - // Always `Auto Model` on top if available - if let autoModel { - modelButton(for: autoModel) - } - - // Display standard models section if available - modelSection(title: "Standard Models", models: standardModels) - - // Display premium models section if available - modelSection(title: "Premium Models", models: premiumModels) - - if isBYOKFFEnabled { - // Display byok models section if available - modelSection(title: "Other Models", models: byokModels) - - Button("Manage Models...") { - try? launchHostAppBYOKSettings() - } - } - - if standardModels.isEmpty { - Link("Add Premium Models", destination: URL(string: "https://aka.ms/github-copilot-upgrade-plan")!) - } - } label: { - Text(selectedModel?.displayName ?? selectedModel?.modelName ?? "") - // scaledFont not work here. workaround by direclty use the fontScale - .font(.system(size: 13 * fontScale)) - } - .menuStyle(BorderlessButtonMenuStyle()) - .frame(maxWidth: labelWidth()) - .scaledPadding(4) - .background( - RoundedRectangle(cornerRadius: 5) - .fill(isHovered ? Color.gray.opacity(0.1) : Color.clear) - ) - .onHover { hovering in - isHovered = hovering - } - } - - // Helper function to create a section of model options - @ViewBuilder - private func modelSection(title: String, models: [LLMModel]) -> some View { - if !models.isEmpty { - Section(title) { - ForEach(models, id: \.self) { model in - modelButton(for: model) - } - } - } - } - - // Helper function to create a model selection button - private func modelButton(for model: LLMModel) -> some View { - Button { - AppState.shared.setSelectedModel(model) - } label: { - Text(createModelMenuItemAttributedString( - modelName: model.displayName ?? model.modelName, - isSelected: selectedModel == model, - cachedMultiplierText: currentCache.modelMultiplierCache[model.id.appending(model.providerName ?? "")] ?? "" - )) - } - .help( - model.isAutoModel - ? "Auto selects the best model for your request based on capacity and performance." - : model.displayName ?? model.modelName) - } - private var mcpButton: some View { Group { if isMCPFFEnabled { @@ -393,7 +272,13 @@ struct ModeAndModelPicker: View { // Model Picker Group { if !copilotModels.isEmpty && selectedModel != nil { - modelPickerMenu + ChatModelPicker( + selectedModel: selectedModel, + copilotModels: copilotModels, + byokModels: byokModels, + isBYOKFFEnabled: isBYOKFFEnabled, + currentCache: currentCache + ) } else { EmptyView() } @@ -433,9 +318,6 @@ struct ModeAndModelPicker: View { .onChange(of: isBYOKFFEnabled) { _ in updateCurrentModel() } - .onChange(of: isEditorPreviewEnabled) { _ in - updateCurrentModel() - } .onReceive(NotificationCenter.default.publisher(for: .gitHubCopilotSelectedModelDidChange)) { _ in updateCurrentModel() } @@ -445,15 +327,6 @@ struct ModeAndModelPicker: View { } } - func labelWidth() -> CGFloat { - guard let selectedModel = selectedModel else { return 100 } - let displayName = selectedModel.displayName ?? selectedModel.modelName - let width = displayName.size( - withAttributes: attributes - ).width - return CGFloat(width * fontScale + 20) - } - @MainActor func refreshModels() async { let now = Date() @@ -468,18 +341,6 @@ struct ModeAndModelPicker: View { } } - private func createModelMenuItemAttributedString( - modelName: String, - isSelected: Bool, - cachedMultiplierText: String - ) -> AttributedString { - return ModelMenuItemFormatter.createModelMenuItemAttributedString( - modelName: modelName, - isSelected: isSelected, - multiplierText: cachedMultiplierText, - targetWidth: currentCache.cachedMaxWidth - ) - } } struct ModelPicker_Previews: PreviewProvider { diff --git a/Core/Sources/ConversationTab/ModeAndModelPicker/ModePicker/AgentModeButtonMenuItem.swift b/Core/Sources/ConversationTab/ModeAndModelPicker/ModePicker/AgentModeButtonMenuItem.swift index 322bac6d..5ed180f8 100644 --- a/Core/Sources/ConversationTab/ModeAndModelPicker/ModePicker/AgentModeButtonMenuItem.swift +++ b/Core/Sources/ConversationTab/ModeAndModelPicker/ModePicker/AgentModeButtonMenuItem.swift @@ -466,27 +466,11 @@ class AgentModeButtonMenuItem: NSView { super.draw(dirtyRect) if isHovered { - NSGraphicsContext.saveGraphicsState() - - let hoverColor = NSColor(.accentColor) - hoverColor.setFill() - - let cornerRadius: CGFloat - if #available(macOS 26.0, *) { - cornerRadius = 8.0 * fontScale - } else { - cornerRadius = 4.0 * fontScale - } - - // Use frame dimensions instead of bounds to avoid layout recursion - let viewWidth = frame.width - let viewHeight = frame.height - let hoverWidth = viewWidth - (scaledConstants.hoverEdgeInset * 2) - let insetRect = NSRect(x: scaledConstants.hoverEdgeInset, y: 0, width: hoverWidth, height: viewHeight) - let path = NSBezierPath(roundedRect: insetRect, xRadius: cornerRadius, yRadius: cornerRadius) - path.fill() - - NSGraphicsContext.restoreGraphicsState() + ModelMenuItemFormatter.drawMenuItemHighlight( + in: frame, + fontScale: fontScale, + hoverEdgeInset: scaledConstants.hoverEdgeInset + ) } } diff --git a/Core/Sources/ConversationTab/ModeAndModelPicker/ModePicker/ChatModePicker.swift b/Core/Sources/ConversationTab/ModeAndModelPicker/ModePicker/ChatModePicker.swift index 97268560..641a4489 100644 --- a/Core/Sources/ConversationTab/ModeAndModelPicker/ModePicker/ChatModePicker.swift +++ b/Core/Sources/ConversationTab/ModeAndModelPicker/ModePicker/ChatModePicker.swift @@ -23,7 +23,6 @@ public struct ChatModePicker: View { let projectRootURL: URL? @Environment(\.colorScheme) var colorScheme @State var isAgentModeFFEnabled: Bool - @State var isEditorPreviewFFEnabled: Bool @State var isCustomAgentPolicyEnabled: Bool @State private var cancellables = Set() @State private var builtInAgents: [ConversationMode] = [] @@ -44,7 +43,6 @@ public struct ChatModePicker: View { self.projectRootURL = projectRootURL self.onScopeChange = onScopeChange isAgentModeFFEnabled = FeatureFlagNotifierImpl.shared.featureFlags.agentMode - isEditorPreviewFFEnabled = FeatureFlagNotifierImpl.shared.featureFlags.editorPreviewFeatures isCustomAgentPolicyEnabled = CopilotPolicyNotifierImpl.shared.copilotPolicy.customAgentEnabled } @@ -78,7 +76,6 @@ public struct ChatModePicker: View { private func subscribeToFeatureFlagsDidChangeEvent() { FeatureFlagNotifierImpl.shared.featureFlagsDidChange.sink(receiveValue: { featureFlags in isAgentModeFFEnabled = featureFlags.agentMode - isEditorPreviewFFEnabled = featureFlags.editorPreviewFeatures }) .store(in: &cancellables) } @@ -188,7 +185,7 @@ public struct ChatModePicker: View { customAgents: customAgents, selectedAgent: selectedAgent, selectedIconName: displayIconName, - isCustomAgentEnabled: isEditorPreviewFFEnabled && isCustomAgentPolicyEnabled, + isCustomAgentEnabled: isCustomAgentPolicyEnabled, onSelectAgent: { setAgentMode($0) }, onEditAgent: { openAgentFileInXcode($0) }, onDeleteAgent: { deleteCustomAgent($0) }, @@ -219,13 +216,6 @@ public struct ChatModePicker: View { setAskMode() } } - .onChange(of: isEditorPreviewFFEnabled) { newValue in - // If editor preview is disabled and current agent is not the default agent, reset to default - if !newValue && chatMode == ChatMode.Agent.rawValue && !selectedAgent.isDefaultAgent { - let defaultAgent = builtInAgents.first(where: { $0.isDefaultAgent }) ?? .defaultAgent - setAgentMode(defaultAgent) - } - } .onChange(of: isCustomAgentPolicyEnabled) { newValue in // If custom agent policy is disabled and current agent is not the default agent, reset to default if !newValue && chatMode == ChatMode.Agent.rawValue && !selectedAgent.isDefaultAgent { @@ -277,7 +267,7 @@ public struct ChatModePicker: View { // Try to find the agent if let agent = findAgent(byId: subMode) { // If it's not the default agent and custom agents are disabled, reset to default - if !agent.isDefaultAgent && (!isEditorPreviewFFEnabled || !isCustomAgentPolicyEnabled) { + if !agent.isDefaultAgent && !isCustomAgentPolicyEnabled { selectedAgent = builtInAgents.first(where: { $0.isDefaultAgent }) ?? .defaultAgent AppState.shared.setSelectedAgentSubMode("Agent") return diff --git a/Core/Sources/ConversationTab/ModeAndModelPicker/ModelManagerUtils.swift b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelManagerUtils.swift index 53eeeb6e..3e0100e7 100644 --- a/Core/Sources/ConversationTab/ModeAndModelPicker/ModelManagerUtils.swift +++ b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelManagerUtils.swift @@ -34,7 +34,8 @@ public extension AppState { let displayName = savedModel["displayName"]?.stringValue let providerName = savedModel["providerName"]?.stringValue let supportVision = savedModel["supportVision"]?.boolValue ?? false - + let degradationReason = savedModel["degradationReason"]?.stringValue + // Try to reconstruct billing info if available var billing: CopilotModelBilling? if let isPremium = savedModel["billing"]?["isPremium"]?.boolValue, @@ -44,7 +45,7 @@ public extension AppState { multiplier: Float(multiplier) ) } - + return LLMModel( displayName: displayName, modelName: modelName, @@ -52,7 +53,8 @@ public extension AppState { id: id, billing: billing, providerName: providerName, - supportVision: supportVision + supportVision: supportVision, + degradationReason: degradationReason ) } @@ -154,7 +156,8 @@ public class CopilotModelManagerObservable: ObservableObject { modelFamily: fallbackModel.modelFamily, id: fallbackModel.id, billing: fallbackModel.billing, - supportVision: fallbackModel.capabilities.supports.vision + supportVision: fallbackModel.capabilities.supports.vision, + degradationReason: fallbackModel.degradationReason ) ) } @@ -175,7 +178,8 @@ public extension CopilotModelManager { modelFamily: $0.isChatFallback ? $0.id : $0.modelFamily, id: $0.id, billing: $0.billing, - supportVision: $0.capabilities.supports.vision + supportVision: $0.capabilities.supports.vision, + degradationReason: $0.degradationReason ) } } @@ -183,7 +187,8 @@ public extension CopilotModelManager { static func getDefaultChatModel(scope: PromptTemplateScope = .chatPanel) -> LLMModel? { let LLMs = CopilotModelManager.getAvailableLLMs() let LLMsInScope = LLMs.filter({ $0.scopes.contains(scope) }) - let defaultModel = LLMsInScope.first(where: { $0.isChatDefault && !$0.isAutoModel }) + let defaultModel = LLMsInScope.first(where: { $0.isChatDefault && $0.isAutoModel }) + ?? LLMsInScope.first(where: { $0.isChatDefault }) // If a default model is found, return it if let defaultModel = defaultModel { return LLMModel( @@ -191,7 +196,8 @@ public extension CopilotModelManager { modelFamily: defaultModel.modelFamily, id: defaultModel.id, billing: defaultModel.billing, - supportVision: defaultModel.capabilities.supports.vision + supportVision: defaultModel.capabilities.supports.vision, + degradationReason: defaultModel.degradationReason ) } @@ -203,18 +209,20 @@ public extension CopilotModelManager { modelFamily: gpt4_1.modelFamily, id: gpt4_1.id, billing: gpt4_1.billing, - supportVision: gpt4_1.capabilities.supports.vision + supportVision: gpt4_1.capabilities.supports.vision, + degradationReason: gpt4_1.degradationReason ) } // If no default model is found, fallback to the first available model - if let firstModel = LLMsInScope.first(where: { !$0.isAutoModel }) { + if let firstModel = LLMsInScope.first { return LLMModel( modelName: firstModel.modelName, modelFamily: firstModel.modelFamily, id: firstModel.id, billing: firstModel.billing, - supportVision: firstModel.capabilities.supports.vision + supportVision: firstModel.capabilities.supports.vision, + degradationReason: firstModel.degradationReason ) } @@ -253,6 +261,7 @@ public struct LLMModel: Codable, Hashable, Equatable { public let billing: CopilotModelBilling? public let providerName: String? public let supportVision: Bool + public let degradationReason: String? public init( displayName: String? = nil, @@ -261,7 +270,8 @@ public struct LLMModel: Codable, Hashable, Equatable { id: String, billing: CopilotModelBilling?, providerName: String? = nil, - supportVision: Bool + supportVision: Bool, + degradationReason: String? = nil ) { self.displayName = displayName self.modelName = modelName @@ -270,6 +280,28 @@ public struct LLMModel: Codable, Hashable, Equatable { self.billing = billing self.providerName = providerName self.supportVision = supportVision + self.degradationReason = degradationReason + } + + // Exclude degradationReason from equality — it's transient status, not model identity + public static func == (lhs: LLMModel, rhs: LLMModel) -> Bool { + lhs.displayName == rhs.displayName && + lhs.modelName == rhs.modelName && + lhs.modelFamily == rhs.modelFamily && + lhs.id == rhs.id && + lhs.billing == rhs.billing && + lhs.providerName == rhs.providerName && + lhs.supportVision == rhs.supportVision + } + + public func hash(into hasher: inout Hasher) { + hasher.combine(displayName) + hasher.combine(modelName) + hasher.combine(modelFamily) + hasher.combine(id) + hasher.combine(billing) + hasher.combine(providerName) + hasher.combine(supportVision) } } diff --git a/Core/Sources/ConversationTab/ModeAndModelPicker/ModelMenuItemFormatter.swift b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelMenuItemFormatter.swift index 7b32efc8..e97027cc 100644 --- a/Core/Sources/ConversationTab/ModeAndModelPicker/ModelMenuItemFormatter.swift +++ b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelMenuItemFormatter.swift @@ -9,7 +9,7 @@ public struct ScopeCache { // MARK: - Model Menu Item Formatting public struct ModelMenuItemFormatter { - public static let minimumPadding: Int = 48 + public static let minimumPadding: Int = 24 public static let attributes: [NSAttributedString.Key: NSFont] = [.font: NSFont.systemFont(ofSize: NSFont.systemFontSize)] @@ -26,9 +26,18 @@ public struct ModelMenuItemFormatter { modelName: String, isSelected: Bool, multiplierText: String, - targetWidth: CGFloat? = nil + targetWidth: CGFloat? = nil, + isDegraded: Bool = false ) -> AttributedString { - let displayName = isSelected ? "✓ \(modelName)" : " \(modelName)" + let prefix: String + if isDegraded { + prefix = "⚠ " + } else if isSelected { + prefix = "✓ " + } else { + prefix = " " + } + let displayName = "\(prefix)\(modelName)" var fullString = displayName var attributedString = AttributedString(fullString) @@ -84,4 +93,36 @@ public struct ModelMenuItemFormatter { return "" } } + + /// Draws the standard menu-item highlight background (accent-colored rounded rect). + static func drawMenuItemHighlight( + in frame: NSRect, + fontScale: Double, + hoverEdgeInset: CGFloat + ) { + NSGraphicsContext.saveGraphicsState() + NSColor.controlAccentColor.setFill() + + let cornerRadius: CGFloat + if #available(macOS 26.0, *) { + cornerRadius = 8.0 * fontScale + } else { + cornerRadius = 4.0 * fontScale + } + + let hoverWidth = frame.width - (hoverEdgeInset * 2) + let insetRect = NSRect( + x: hoverEdgeInset, + y: 0, + width: hoverWidth, + height: frame.height + ) + let path = NSBezierPath( + roundedRect: insetRect, + xRadius: cornerRadius, + yRadius: cornerRadius + ) + path.fill() + NSGraphicsContext.restoreGraphicsState() + } } diff --git a/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ChatModelPicker.swift b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ChatModelPicker.swift new file mode 100644 index 00000000..be81b51a --- /dev/null +++ b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ChatModelPicker.swift @@ -0,0 +1,28 @@ +import SharedUIComponents +import SwiftUI + +struct ChatModelPicker: View { + let selectedModel: LLMModel? + let copilotModels: [LLMModel] + let byokModels: [LLMModel] + let isBYOKFFEnabled: Bool + let currentCache: ScopeCache + + @StateObject private var fontScaleManager = FontScaleManager.shared + + private var fontScale: Double { + fontScaleManager.currentScale + } + + var body: some View { + ModelPickerButton( + selectedModel: selectedModel, + copilotModels: copilotModels, + byokModels: byokModels, + isBYOKFFEnabled: isBYOKFFEnabled, + currentCache: currentCache, + fontScale: fontScale + ) + .fixedSize(horizontal: false, vertical: true) + } +} diff --git a/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerButton.swift b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerButton.swift new file mode 100644 index 00000000..d2a92b5f --- /dev/null +++ b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerButton.swift @@ -0,0 +1,270 @@ +import AppKit +import SwiftUI + +// MARK: - Model Picker Button (NSViewRepresentable) + +struct ModelPickerButton: NSViewRepresentable { + let selectedModel: LLMModel? + let copilotModels: [LLMModel] + let byokModels: [LLMModel] + let isBYOKFFEnabled: Bool + let currentCache: ScopeCache + let fontScale: Double + + func makeNSView(context: Context) -> NSView { + let container = ModelPickerContainerView(fontScale: fontScale) + container.translatesAutoresizingMaskIntoConstraints = false + + let button = ClickThroughButton() + button.title = "" + button.bezelStyle = .inline + button.setButtonType(.momentaryPushIn) + button.isBordered = false + button.target = context.coordinator + button.action = #selector(Coordinator.buttonClicked(_:)) + button.translatesAutoresizingMaskIntoConstraints = false + button.wantsLayer = true + + let titleLabel = NSTextField(labelWithString: "") + titleLabel.isEditable = false + titleLabel.isBordered = false + titleLabel.backgroundColor = .clear + titleLabel.drawsBackground = false + titleLabel.translatesAutoresizingMaskIntoConstraints = false + titleLabel.setContentHuggingPriority(.defaultLow, for: .horizontal) + titleLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) + titleLabel.alignment = .center + titleLabel.usesSingleLineMode = true + titleLabel.lineBreakMode = .byTruncatingMiddle + + let chevronView = NSImageView() + let chevronImage = NSImage( + systemSymbolName: "chevron.down", + accessibilityDescription: nil + ) + let symbolConfig = NSImage.SymbolConfiguration( + pointSize: 8 * fontScale, weight: .semibold + ) + chevronView.image = chevronImage?.withSymbolConfiguration(symbolConfig) + chevronView.translatesAutoresizingMaskIntoConstraints = false + + let stackView = NSStackView(views: [titleLabel, chevronView]) + stackView.orientation = .horizontal + stackView.spacing = 2 * fontScale + stackView.translatesAutoresizingMaskIntoConstraints = false + stackView.alignment = .centerY + stackView.setHuggingPriority(.required, for: .horizontal) + + button.addSubview(stackView) + container.addSubview(button) + + NSLayoutConstraint.activate([ + button.leadingAnchor.constraint(equalTo: container.leadingAnchor), + button.trailingAnchor.constraint(equalTo: container.trailingAnchor), + button.topAnchor.constraint(equalTo: container.topAnchor), + button.bottomAnchor.constraint(equalTo: container.bottomAnchor), + + stackView.leadingAnchor.constraint( + equalTo: button.leadingAnchor, constant: 6 * fontScale + ), + stackView.trailingAnchor.constraint( + equalTo: button.trailingAnchor, constant: -6 * fontScale + ), + stackView.topAnchor.constraint( + equalTo: button.topAnchor, constant: 2 * fontScale + ), + stackView.bottomAnchor.constraint( + equalTo: button.bottomAnchor, constant: -2 * fontScale + ), + + chevronView.widthAnchor.constraint(equalToConstant: 8 * fontScale), + chevronView.heightAnchor.constraint(equalToConstant: 8 * fontScale), + ]) + + context.coordinator.button = button + context.coordinator.titleLabel = titleLabel + context.coordinator.chevronView = chevronView + + // Setup tracking for hover + let trackingArea = NSTrackingArea( + rect: .zero, + options: [.mouseEnteredAndExited, .activeInActiveApp, .inVisibleRect], + owner: context.coordinator, + userInfo: nil + ) + button.addTrackingArea(trackingArea) + context.coordinator.trackingArea = trackingArea + + return container + } + + func updateNSView(_ nsView: NSView, context: Context) { + guard let titleLabel = context.coordinator.titleLabel, + let button = context.coordinator.button, + let chevronView = context.coordinator.chevronView + else { return } + + let label = selectedModelLabel + titleLabel.stringValue = label + titleLabel.font = NSFont.systemFont(ofSize: 13 * fontScale) + titleLabel.textColor = .labelColor + + let chevronConfig = NSImage.SymbolConfiguration( + pointSize: 8 * fontScale, weight: .semibold + ) + chevronView.image = NSImage( + systemSymbolName: "chevron.down", + accessibilityDescription: nil + )?.withSymbolConfiguration(chevronConfig) + chevronView.contentTintColor = .tertiaryLabelColor + + // Update coordinator data + context.coordinator.selectedModel = selectedModel + context.coordinator.copilotModels = copilotModels + context.coordinator.byokModels = byokModels + context.coordinator.isBYOKFFEnabled = isBYOKFFEnabled + context.coordinator.currentCache = currentCache + context.coordinator.fontScale = fontScale + + // Hover background + let isHovered = context.coordinator.isHovered + button.layer?.backgroundColor = isHovered + ? NSColor.gray.withAlphaComponent(0.1).cgColor + : NSColor.clear.cgColor + button.layer?.cornerRadius = 5 * fontScale + button.layer?.cornerCurve = .continuous + + // Ideal width based on text (allows shrinking when parent is tight) + let textWidth = labelWidth(label: label) + context.coordinator.widthConstraint?.constant = textWidth + if context.coordinator.widthConstraint == nil { + let wc = nsView.widthAnchor.constraint(lessThanOrEqualToConstant: textWidth) + wc.priority = .defaultHigh + wc.isActive = true + context.coordinator.widthConstraint = wc + } + + // Report ideal width so SwiftUI can size us properly + if let container = nsView as? ModelPickerContainerView { + container.fontScale = fontScale + container.idealWidth = textWidth + container.invalidateIntrinsicContentSize() + } + } + + func makeCoordinator() -> Coordinator { + Coordinator( + selectedModel: selectedModel, + copilotModels: copilotModels, + byokModels: byokModels, + isBYOKFFEnabled: isBYOKFFEnabled, + currentCache: currentCache, + fontScale: fontScale + ) + } + + private var selectedModelLabel: String { + let name = selectedModel?.displayName ?? selectedModel?.modelName ?? "" + if selectedModel?.degradationReason != nil { + return "\u{26A0} \(name)" + } + return name + } + + private func labelWidth(label: String) -> CGFloat { + let font = NSFont.systemFont(ofSize: 13 * fontScale) + let attrs: [NSAttributedString.Key: Any] = [.font: font] + let textWidth = ceil((label as NSString).size(withAttributes: attrs).width) + // text + left padding(6) + right padding(6) + chevron(8) + stack spacing(2) + text field internal margin(6) + return textWidth + 28 * fontScale + } + + // MARK: - Coordinator + + class Coordinator: NSObject { + var selectedModel: LLMModel? + var copilotModels: [LLMModel] + var byokModels: [LLMModel] + var isBYOKFFEnabled: Bool + var currentCache: ScopeCache + var fontScale: Double + + var button: NSButton? + var titleLabel: NSTextField? + var chevronView: NSImageView? + var trackingArea: NSTrackingArea? + var widthConstraint: NSLayoutConstraint? + var isHovered = false + + init( + selectedModel: LLMModel?, + copilotModels: [LLMModel], + byokModels: [LLMModel], + isBYOKFFEnabled: Bool, + currentCache: ScopeCache, + fontScale: Double + ) { + self.selectedModel = selectedModel + self.copilotModels = copilotModels + self.byokModels = byokModels + self.isBYOKFFEnabled = isBYOKFFEnabled + self.currentCache = currentCache + self.fontScale = fontScale + } + + @objc func buttonClicked(_ sender: NSButton) { + let menuBuilder = ModelPickerMenu( + selectedModel: selectedModel, + copilotModels: copilotModels, + byokModels: byokModels, + isBYOKFFEnabled: isBYOKFFEnabled, + currentCache: currentCache, + fontScale: fontScale + ) + menuBuilder.showMenu(relativeTo: sender) + } + + @objc(mouseEntered:) func mouseEntered(with event: NSEvent) { + isHovered = true + NSAnimationContext.runAnimationGroup { context in + context.duration = 0.15 + button?.animator().layer?.backgroundColor = NSColor.gray + .withAlphaComponent(0.1).cgColor + } + NSCursor.pointingHand.push() + } + + @objc(mouseExited:) func mouseExited(with event: NSEvent) { + isHovered = false + NSAnimationContext.runAnimationGroup { context in + context.duration = 0.15 + button?.animator().layer?.backgroundColor = NSColor.clear.cgColor + } + NSCursor.pop() + } + } +} + +// MARK: - Container view that constrains intrinsic height + +private class ModelPickerContainerView: NSView { + var fontScale: Double + var idealWidth: CGFloat = NSView.noIntrinsicMetric + + init(fontScale: Double) { + self.fontScale = fontScale + super.init(frame: .zero) + setContentCompressionResistancePriority(.defaultLow, for: .horizontal) + setContentHuggingPriority(.defaultHigh, for: .horizontal) + } + + @available(*, unavailable) + required init?(coder _: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override var intrinsicContentSize: NSSize { + let height = 20 * fontScale + return NSSize(width: idealWidth, height: height) + } +} diff --git a/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerDetailPanel.swift b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerDetailPanel.swift new file mode 100644 index 00000000..7ea03f64 --- /dev/null +++ b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerDetailPanel.swift @@ -0,0 +1,256 @@ +import AppKit + +// MARK: - Floating Detail Panel (shown on menu item hover) + +class ModelPickerDetailPanel: NSPanel { + static let shared = ModelPickerDetailPanel() + + private let contentLabel = NSTextField(wrappingLabelWithString: "") + private let nameLabel = NSTextField(labelWithString: "") + private let separatorView = NSBox() + private let containerView = NSView() + private var hideTimer: Timer? + + private var containerConstraints: [NSLayoutConstraint] = [] + private var currentFontScale: CGFloat = 1.0 + + private init() { + super.init( + contentRect: NSRect(x: 0, y: 0, width: 260, height: 100), + styleMask: [.borderless, .nonactivatingPanel], + backing: .buffered, + defer: true + ) + self.isFloatingPanel = true + self.level = .popUpMenu + 1 + self.isOpaque = false + self.backgroundColor = .clear + self.hidesOnDeactivate = false + self.hasShadow = true + self.isMovable = false + self.collectionBehavior = [.canJoinAllSpaces, .fullScreenAuxiliary] + setupContent() + } + + private func setupContent() { + let visual = NSVisualEffectView() + visual.material = .popover + visual.state = .active + visual.wantsLayer = true + visual.layer?.cornerRadius = 8 + visual.layer?.masksToBounds = true + visual.translatesAutoresizingMaskIntoConstraints = false + + containerView.translatesAutoresizingMaskIntoConstraints = false + + nameLabel.textColor = .labelColor + nameLabel.isEditable = false + nameLabel.isBordered = false + nameLabel.backgroundColor = .clear + nameLabel.drawsBackground = false + nameLabel.translatesAutoresizingMaskIntoConstraints = false + nameLabel.lineBreakMode = .byTruncatingTail + + separatorView.boxType = .separator + separatorView.translatesAutoresizingMaskIntoConstraints = false + + contentLabel.isEditable = false + contentLabel.isBordered = false + contentLabel.backgroundColor = .clear + contentLabel.drawsBackground = false + contentLabel.textColor = .secondaryLabelColor + contentLabel.usesSingleLineMode = false + contentLabel.maximumNumberOfLines = 0 + contentLabel.translatesAutoresizingMaskIntoConstraints = false + + containerView.addSubview(nameLabel) + containerView.addSubview(separatorView) + containerView.addSubview(contentLabel) + + visual.addSubview(containerView) + self.contentView = visual + + // Static constraints that don't depend on font scale + NSLayoutConstraint.activate([ + nameLabel.topAnchor.constraint(equalTo: containerView.topAnchor), + nameLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor), + nameLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor), + + separatorView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor), + separatorView.trailingAnchor.constraint( + equalTo: containerView.trailingAnchor + ), + + contentLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor), + contentLabel.trailingAnchor.constraint( + equalTo: containerView.trailingAnchor + ), + contentLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor), + ]) + + applyScaledConstraints(to: visual, fontScale: 1.0) + } + + private func applyScaledConstraints( + to visual: NSView, + fontScale: CGFloat + ) { + NSLayoutConstraint.deactivate(containerConstraints) + + let padding: CGFloat = 10 * fontScale + let horizontalPadding: CGFloat = 12 * fontScale + let spacing: CGFloat = 6 * fontScale + + containerConstraints = [ + containerView.topAnchor.constraint( + equalTo: visual.topAnchor, constant: padding + ), + containerView.leadingAnchor.constraint( + equalTo: visual.leadingAnchor, constant: horizontalPadding + ), + containerView.trailingAnchor.constraint( + equalTo: visual.trailingAnchor, constant: -horizontalPadding + ), + containerView.bottomAnchor.constraint( + equalTo: visual.bottomAnchor, constant: -padding + ), + separatorView.topAnchor.constraint( + equalTo: nameLabel.bottomAnchor, constant: spacing + ), + contentLabel.topAnchor.constraint( + equalTo: separatorView.bottomAnchor, constant: spacing + ), + ] + + NSLayoutConstraint.activate(containerConstraints) + + nameLabel.font = NSFont.systemFont( + ofSize: 13 * fontScale, weight: .semibold + ) + contentLabel.font = NSFont.systemFont(ofSize: 12 * fontScale) + contentLabel.preferredMaxLayoutWidth = 236 * fontScale + + visual.layer?.cornerRadius = 8 * fontScale + + currentFontScale = fontScale + } + + func show( + for model: LLMModel, + nearRect: NSRect, + preferRight: Bool = true, + fontScale: CGFloat = 1.0 + ) { + hideTimer?.invalidate() + hideTimer = nil + + if let visual = self.contentView { + applyScaledConstraints(to: visual, fontScale: fontScale) + } + + let displayName = model.displayName ?? model.modelName + nameLabel.stringValue = displayName + + var details: [String] = [] + + // Provider + if let provider = model.providerName, !provider.isEmpty { + details.append("Provider: \(provider)") + } + + // Billing + if let billing = model.billing { + if billing.multiplier == 0 { + details.append("Cost: Included") + } else { + let formatted = billing.multiplier + .truncatingRemainder(dividingBy: 1) == 0 + ? String(format: "%.0f", billing.multiplier) + : String(format: "%.2f", billing.multiplier) + details.append("Cost: \(formatted)x premium") + } + } + + // Vision support + if model.supportVision { + details.append("Supports: Vision") + } + + // Degradation + if let reason = model.degradationReason { + details.append("\n\u{26A0} \(reason)") + } + + // Auto model description + if model.isAutoModel { + details = [ + "Automatically selects the best model for your request based on capacity and performance.", + "\nCost may vary based on the selected model.", + ] + } + + contentLabel.stringValue = details.joined(separator: "\n") + + // Size to fit content + let fittingSize = containerView.fittingSize + let panelWidth: CGFloat = 260 * fontScale + let panelHeight = fittingSize.height + 20 * fontScale + + let gap: CGFloat = 4 * fontScale + var origin: NSPoint + if preferRight { + origin = NSPoint( + x: nearRect.maxX + gap, y: nearRect.midY - panelHeight / 2 + ) + } else { + origin = NSPoint( + x: nearRect.minX - panelWidth - gap, + y: nearRect.midY - panelHeight / 2 + ) + } + + // Find the screen that contains the menu item + let menuScreen = NSScreen.screens.first(where: { + $0.frame.contains(nearRect.origin) + }) ?? NSScreen.main + + // Ensure the panel stays fully visible on that screen + if let screen = menuScreen { + let screenFrame = screen.visibleFrame + if origin.x + panelWidth > screenFrame.maxX { + origin.x = nearRect.minX - panelWidth - gap + } + if origin.x < screenFrame.minX { + origin.x = nearRect.maxX + gap + } + // Clamp horizontally as last resort + origin.x = max(origin.x, screenFrame.minX) + origin.x = min(origin.x, screenFrame.maxX - panelWidth) + // Clamp vertically + origin.y = max(origin.y, screenFrame.minY) + origin.y = min(origin.y, screenFrame.maxY - panelHeight) + } + + setContentSize(NSSize(width: panelWidth, height: panelHeight)) + setFrameOrigin(origin) + orderFront(nil) + } + + func scheduleHide() { + hideTimer?.invalidate() + hideTimer = Timer.scheduledTimer(withTimeInterval: 0.15, repeats: false) { [weak self] _ in + self?.orderOut(nil) + } + } + + func cancelHide() { + hideTimer?.invalidate() + hideTimer = nil + } + + override func close() { + hideTimer?.invalidate() + hideTimer = nil + super.close() + } +} diff --git a/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerMenu.swift b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerMenu.swift new file mode 100644 index 00000000..a11f8b6f --- /dev/null +++ b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerMenu.swift @@ -0,0 +1,418 @@ +import AppKit +import HostAppActivator +import Persist + +// MARK: - Search Field View for Menu + +private class ModelSearchFieldView: NSView, NSSearchFieldDelegate { + let searchField = NSSearchField() + var onSearchTextChanged: ((String) -> Void)? + weak var parentMenu: NSMenu? + + init(fontScale: Double, width: CGFloat) { + let height = 30 * fontScale + super.init(frame: NSRect(x: 0, y: 0, width: width, height: height + 8 * fontScale)) + + searchField.placeholderString = "Search models..." + searchField.font = NSFont.systemFont(ofSize: 12 * fontScale) + searchField.translatesAutoresizingMaskIntoConstraints = false + searchField.focusRingType = .none + searchField.delegate = self + addSubview(searchField) + + NSLayoutConstraint.activate([ + searchField.leadingAnchor.constraint( + equalTo: leadingAnchor, constant: 8 * fontScale + ), + searchField.trailingAnchor.constraint( + equalTo: trailingAnchor, constant: -8 * fontScale + ), + searchField.centerYAnchor.constraint(equalTo: centerYAnchor), + searchField.heightAnchor.constraint(equalToConstant: height), + ]) + } + + @available(*, unavailable) + required init?(coder _: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func controlTextDidChange(_ obj: Notification) { + guard let field = obj.object as? NSSearchField else { return } + onSearchTextChanged?(field.stringValue) + } + + /// Intercept Return / Enter in the search field to select the highlighted + /// menu item. NSMenu doesn't do this automatically for custom-view items. + func control( + _ control: NSControl, + textView _: NSTextView, + doCommandBy commandSelector: Selector + ) -> Bool { + if commandSelector == #selector(NSResponder.insertNewline(_:)) { + if let menu = parentMenu, + let highlightedItem = menu.highlightedItem, + let menuItemView = highlightedItem.view as? ModelPickerMenuItem + { + menuItemView.performSelect() + return true + } + } + return false + } + + override func viewDidMoveToWindow() { + super.viewDidMoveToWindow() + if window != nil { + DispatchQueue.main.async { [weak self] in + self?.searchField.becomeFirstResponder() + } + } + } +} + +// MARK: - Custom Menu (allows key events to reach search field) + +private class ModelPickerNSMenu: NSMenu { + weak var searchField: NSSearchField? + + override func performKeyEquivalent(with event: NSEvent) -> Bool { + guard event.type == .keyDown else { + return super.performKeyEquivalent(with: event) + } + + // Return / Enter: NSMenu won't fire the action for items with custom + // views, so we find the currently highlighted ModelPickerMenuItem and + // invoke its selection callback directly. + let confirmKeyCodes: Set = [ + 36, // return + 76, // enter (numpad) + ] + if confirmKeyCodes.contains(event.keyCode) { + if let highlightedItem = highlightedItem, + let menuItemView = highlightedItem.view as? ModelPickerMenuItem + { + menuItemView.performSelect() + return true + } + return super.performKeyEquivalent(with: event) + } + + // Forward printable character input and delete keys to the search + // field. Navigation keys (arrows, Escape, Space, Tab) fall through + // to super so NSMenu handles them normally. + if let searchField = searchField, + Self.shouldForwardToSearchField(event) + { + if let window = searchField.window { + window.makeFirstResponder(searchField) + searchField.currentEditor()?.keyDown(with: event) + return true + } + } + return super.performKeyEquivalent(with: event) + } + + /// Returns `true` for key events that should be forwarded to the search + /// field: printable characters and delete/backspace. Returns `false` for + /// navigation and control keys so NSMenu can handle them. + private static func shouldForwardToSearchField(_ event: NSEvent) -> Bool { + // Always allow delete / forward-delete so the user can edit the query + let deleteKeyCodes: Set = [ + 51, // delete (backspace) + 117, // forward delete + ] + if deleteKeyCodes.contains(event.keyCode) { + return true + } + + // Reject keys that NSMenu uses for navigation / activation + let navigationKeyCodes: Set = [ + 123, // left arrow + 124, // right arrow + 125, // down arrow + 126, // up arrow + 53, // escape + 49, // space + 48, // tab + ] + if navigationKeyCodes.contains(event.keyCode) { + return false + } + + // Don't forward Cmd-key shortcuts (Cmd+A, Cmd+C, etc.) + if event.modifierFlags.contains(.command) { + return false + } + + // Forward if the key produces printable characters + if let chars = event.characters, !chars.isEmpty { + return true + } + + return false + } +} + +// MARK: - Model Picker Menu Builder + +struct ModelPickerMenu { + let selectedModel: LLMModel? + let copilotModels: [LLMModel] + let byokModels: [LLMModel] + let isBYOKFFEnabled: Bool + let currentCache: ScopeCache + let fontScale: Double + + private let detailPanel = ModelPickerDetailPanel.shared + + func showMenu(relativeTo button: NSButton) { + let menu = createMenu(allCopilotModels: copilotModels, allBYOKModels: byokModels) + let buttonFrame = button.frame + let menuOrigin = NSPoint(x: buttonFrame.minX, y: buttonFrame.maxY) + menu.popUp(positioning: nil, at: menuOrigin, in: button.superview) + detailPanel.orderOut(nil) + } + + private func createMenu( + allCopilotModels: [LLMModel], + allBYOKModels: [LLMModel] + ) -> NSMenu { + let menu = ModelPickerNSMenu() + menu.autoenablesItems = false + + let maxWidth = calculateMaxWidth( + copilotModels: allCopilotModels, + byokModels: allBYOKModels + ) + + // Search bar at top (sized to match content) + let searchItem = NSMenuItem() + let searchView = ModelSearchFieldView(fontScale: fontScale, width: maxWidth) + searchView.parentMenu = menu + searchItem.view = searchView + menu.addItem(searchItem) + menu.searchField = searchView.searchField + + // Separator after search + menu.addItem(.separator()) + + // Build initial menu items + rebuildMenuItems( + menu: menu, + copilotModels: allCopilotModels, + byokModels: allBYOKModels, + maxWidth: maxWidth, + searchText: "" + ) + + // Handle search + searchView.onSearchTextChanged = { [weak menu] searchText in + guard let menu = menu else { return } + self.rebuildMenuItems( + menu: menu, + copilotModels: allCopilotModels, + byokModels: allBYOKModels, + maxWidth: maxWidth, + searchText: searchText + ) + } + + return menu + } + + private func rebuildMenuItems( + menu: NSMenu, + copilotModels: [LLMModel], + byokModels: [LLMModel], + maxWidth: CGFloat, + searchText: String + ) { + // Remove all items except the search bar and separator (first 2 items) + while menu.items.count > 2 { + menu.removeItem(at: menu.items.count - 1) + } + + let query = searchText.lowercased().trimmingCharacters(in: .whitespaces) + + let filteredCopilotModels: [LLMModel] + let filteredBYOKModels: [LLMModel] + if query.isEmpty { + filteredCopilotModels = copilotModels + filteredBYOKModels = byokModels + } else { + filteredCopilotModels = copilotModels.filter { + ($0.displayName ?? $0.modelName).lowercased().contains(query) + || $0.modelFamily.lowercased().contains(query) + } + filteredBYOKModels = byokModels.filter { + ($0.displayName ?? $0.modelName).lowercased().contains(query) + || $0.modelFamily.lowercased().contains(query) + || ($0.providerName ?? "").lowercased().contains(query) + } + } + + let premiumModels = filteredCopilotModels.filter { $0.isPremiumModel } + let standardModels = filteredCopilotModels.filter { + $0.isStandardModel && !$0.isAutoModel + } + let autoModel = filteredCopilotModels.first(where: { $0.isAutoModel }) + + // Auto model + if let autoModel = autoModel { + addModelItem( + to: menu, model: autoModel, maxWidth: maxWidth + ) + } + + // Standard models section + addSection( + to: menu, title: "Standard Models", models: standardModels, + maxWidth: maxWidth + ) + + // Premium models section + addSection( + to: menu, title: "Premium Models", models: premiumModels, + maxWidth: maxWidth + ) + + // BYOK models section + if isBYOKFFEnabled { + addSection( + to: menu, title: "Other Models", models: filteredBYOKModels, + maxWidth: maxWidth + ) + + if query.isEmpty { + menu.addItem(.separator()) + let manageItem = NSMenuItem( + title: "Manage Models...", + action: #selector(ModelPickerMenuActions.manageModels), + keyEquivalent: "" + ) + manageItem.target = ModelPickerMenuActions.shared + menu.addItem(manageItem) + } + } + + if standardModels.isEmpty, premiumModels.isEmpty, autoModel == nil, + filteredBYOKModels.isEmpty + { + if query.isEmpty { + let addItem = NSMenuItem( + title: "Add Premium Models", + action: #selector(ModelPickerMenuActions.addPremiumModels), + keyEquivalent: "" + ) + addItem.target = ModelPickerMenuActions.shared + menu.addItem(addItem) + } else { + let noResults = NSMenuItem(title: "No models found", action: nil, keyEquivalent: "") + noResults.isEnabled = false + menu.addItem(noResults) + } + } + } + + private func addSection( + to menu: NSMenu, + title: String, + models: [LLMModel], + maxWidth: CGFloat + ) { + guard !models.isEmpty else { return } + + // Section header + menu.addItem(.separator()) + let headerItem = NSMenuItem(title: title, action: nil, keyEquivalent: "") + headerItem.isEnabled = false + let headerFont = NSFont.systemFont(ofSize: 11 * fontScale, weight: .semibold) + headerItem.attributedTitle = NSAttributedString( + string: title, + attributes: [ + .font: headerFont, + .foregroundColor: NSColor.secondaryLabelColor, + ] + ) + menu.addItem(headerItem) + + for model in models { + addModelItem(to: menu, model: model, maxWidth: maxWidth) + } + } + + private func addModelItem( + to menu: NSMenu, + model: LLMModel, + maxWidth: CGFloat + ) { + let item = NSMenuItem() + let multiplierText = currentCache + .modelMultiplierCache[model.id.appending(model.providerName ?? "")] + ?? ModelMenuItemFormatter.getMultiplierText(for: model) + + let menuItemView = ModelPickerMenuItem( + model: model, + isSelected: selectedModel == model, + multiplierText: multiplierText, + fontScale: fontScale, + fixedWidth: maxWidth, + onSelect: { + AppState.shared.setSelectedModel(model) + menu.cancelTracking() + self.detailPanel.orderOut(nil) + }, + onHover: { hoveredModel, itemRect in + self.detailPanel.show( + for: hoveredModel, + nearRect: itemRect, + fontScale: self.fontScale + ) + }, + onHoverExit: { + self.detailPanel.scheduleHide() + } + ) + item.view = menuItemView + menu.addItem(item) + } + + private func calculateMaxWidth( + copilotModels: [LLMModel], + byokModels: [LLMModel] + ) -> CGFloat { + var maxWidth: CGFloat = 0 + let allModels = isBYOKFFEnabled ? copilotModels + byokModels : copilotModels + + for model in allModels { + let multiplierText = currentCache + .modelMultiplierCache[model.id.appending(model.providerName ?? "")] + ?? ModelMenuItemFormatter.getMultiplierText(for: model) + let width = ModelPickerMenuItem.calculateItemWidth( + model: model, + multiplierText: multiplierText, + fontScale: fontScale + ) + maxWidth = max(maxWidth, width) + } + + return maxWidth + } +} + +// MARK: - Menu Action Target + +private class ModelPickerMenuActions: NSObject { + static let shared = ModelPickerMenuActions() + + @objc func manageModels() { + try? launchHostAppBYOKSettings() + } + + @objc func addPremiumModels() { + if let url = URL(string: "https://aka.ms/github-copilot-upgrade-plan") { + NSWorkspace.shared.open(url) + } + } +} diff --git a/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerMenuItem.swift b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerMenuItem.swift new file mode 100644 index 00000000..a395256e --- /dev/null +++ b/Core/Sources/ConversationTab/ModeAndModelPicker/ModelPicker/ModelPickerMenuItem.swift @@ -0,0 +1,296 @@ +import AppKit + +// MARK: - Model Menu Item View + +class ModelPickerMenuItem: NSView { + private let fontScale: Double + private let model: LLMModel + private let isSelected: Bool + private let multiplierText: String + private let onSelect: () -> Void + private let onHover: ((LLMModel, NSRect) -> Void)? + private let onHoverExit: (() -> Void)? + + private var wasHighlighted = false + + private let nameLabel = NSTextField(labelWithString: "") + private let multiplierLabel = NSTextField(labelWithString: "") + private let checkmarkImageView = NSImageView() + private let warningImageView = NSImageView() + + private struct LayoutConstants { + let fontScale: Double + + var menuHeight: CGFloat { 22 * fontScale } + var checkmarkSize: CGFloat { 13 * fontScale } + var hoverEdgeInset: CGFloat { 5 * fontScale } + var fontSize: CGFloat { 13 * fontScale } + var leadingPadding: CGFloat { 9 * fontScale } + var trailingPadding: CGFloat { 9 * fontScale } + var checkmarkToText: CGFloat { 5 * fontScale } + var nameToMultiplier: CGFloat { 8 * fontScale } + } + + private lazy var constants = LayoutConstants(fontScale: fontScale) + + init( + model: LLMModel, + isSelected: Bool, + multiplierText: String, + fontScale: Double, + fixedWidth: CGFloat, + onSelect: @escaping () -> Void, + onHover: ((LLMModel, NSRect) -> Void)? = nil, + onHoverExit: (() -> Void)? = nil + ) { + self.model = model + self.isSelected = isSelected + self.multiplierText = multiplierText + self.fontScale = fontScale + self.onSelect = onSelect + self.onHover = onHover + self.onHoverExit = onHoverExit + + let constants = LayoutConstants(fontScale: fontScale) + super.init( + frame: NSRect(x: 0, y: 0, width: fixedWidth, height: constants.menuHeight) + ) + setupView() + } + + @available(*, unavailable) + required init?(coder _: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + // MARK: - Highlight state (driven by NSMenu) + + private var isHighlighted: Bool { + enclosingMenuItem?.isHighlighted ?? false + } + + private func setupView() { + wantsLayer = true + layer?.masksToBounds = true + + setupCheckmark() + setupWarningIcon() + setupLabels() + } + + private func setupCheckmark() { + let config = NSImage.SymbolConfiguration( + pointSize: constants.checkmarkSize, + weight: .medium + ) + checkmarkImageView.image = NSImage( + systemSymbolName: "checkmark", + accessibilityDescription: nil + )?.withSymbolConfiguration(config) + checkmarkImageView.contentTintColor = .labelColor + checkmarkImageView.translatesAutoresizingMaskIntoConstraints = false + checkmarkImageView.isHidden = !isSelected || model.degradationReason != nil + addSubview(checkmarkImageView) + + NSLayoutConstraint.activate([ + checkmarkImageView.leadingAnchor.constraint( + equalTo: leadingAnchor, constant: constants.leadingPadding + ), + checkmarkImageView.centerYAnchor.constraint(equalTo: centerYAnchor), + checkmarkImageView.widthAnchor.constraint( + equalToConstant: constants.checkmarkSize + ), + checkmarkImageView.heightAnchor.constraint( + equalToConstant: constants.checkmarkSize + ), + ]) + } + + private func setupWarningIcon() { + guard model.degradationReason != nil else { return } + + let config = NSImage.SymbolConfiguration( + pointSize: constants.checkmarkSize, + weight: .medium + ) + warningImageView.image = NSImage( + systemSymbolName: "exclamationmark.triangle", + accessibilityDescription: "Degraded" + )?.withSymbolConfiguration(config) + warningImageView.contentTintColor = .labelColor + warningImageView.translatesAutoresizingMaskIntoConstraints = false + warningImageView.isHidden = false + addSubview(warningImageView) + + NSLayoutConstraint.activate([ + warningImageView.leadingAnchor.constraint( + equalTo: leadingAnchor, constant: constants.leadingPadding + ), + warningImageView.centerYAnchor.constraint(equalTo: centerYAnchor), + warningImageView.widthAnchor.constraint( + equalToConstant: constants.checkmarkSize + ), + warningImageView.heightAnchor.constraint( + equalToConstant: constants.checkmarkSize + ), + ]) + } + + private func setupLabels() { + let displayName = model.displayName ?? model.modelName + + // Name label — left-aligned, truncates tail, fills remaining space + nameLabel.stringValue = displayName + nameLabel.font = NSFont.systemFont(ofSize: constants.fontSize, weight: .regular) + nameLabel.textColor = .labelColor + nameLabel.isEditable = false + nameLabel.isBordered = false + nameLabel.backgroundColor = .clear + nameLabel.drawsBackground = false + nameLabel.lineBreakMode = .byTruncatingTail + nameLabel.translatesAutoresizingMaskIntoConstraints = false + nameLabel.setContentHuggingPriority(.defaultLow, for: .horizontal) + nameLabel.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) + addSubview(nameLabel) + + // Multiplier label — right-aligned, never truncates + multiplierLabel.stringValue = multiplierText + multiplierLabel.font = NSFont.systemFont( + ofSize: constants.fontSize, weight: .regular + ) + multiplierLabel.textColor = .secondaryLabelColor + multiplierLabel.isEditable = false + multiplierLabel.isBordered = false + multiplierLabel.backgroundColor = .clear + multiplierLabel.drawsBackground = false + multiplierLabel.alignment = .right + multiplierLabel.translatesAutoresizingMaskIntoConstraints = false + multiplierLabel.setContentHuggingPriority(.required, for: .horizontal) + multiplierLabel.setContentCompressionResistancePriority( + .required, for: .horizontal + ) + multiplierLabel.isHidden = multiplierText.isEmpty + addSubview(multiplierLabel) + + let textLeading = checkmarkImageView.trailingAnchor + + NSLayoutConstraint.activate([ + nameLabel.leadingAnchor.constraint( + equalTo: textLeading, constant: constants.checkmarkToText + ), + nameLabel.centerYAnchor.constraint(equalTo: centerYAnchor), + + multiplierLabel.trailingAnchor.constraint( + equalTo: trailingAnchor, constant: -constants.trailingPadding + ), + multiplierLabel.centerYAnchor.constraint(equalTo: centerYAnchor), + + nameLabel.trailingAnchor.constraint( + lessThanOrEqualTo: multiplierLabel.leadingAnchor, + constant: -constants.nameToMultiplier + ), + ]) + } + + // MARK: - Mouse handling + + override func mouseUp(with _: NSEvent) { + onSelect() + } + + // MARK: - Keyboard selection + + /// Called by the menu's `performKeyEquivalent` when Return/Enter is pressed + /// while this item is highlighted. Custom-view menu items don't receive + /// the default NSMenu action, so the menu triggers selection explicitly. + func performSelect() { + onSelect() + } + + override var acceptsFirstResponder: Bool { true } + + override func keyDown(with event: NSEvent) { + let confirmKeyCodes: Set = [ + 36, // return + 76, // enter (numpad) + ] + if confirmKeyCodes.contains(event.keyCode) { + onSelect() + } else { + super.keyDown(with: event) + } + } + + // MARK: - Drawing (highlight driven by NSMenu) + + private func updateColors() { + let highlighted = isHighlighted + if highlighted { + nameLabel.textColor = .white + multiplierLabel.textColor = .white.withAlphaComponent(0.8) + checkmarkImageView.contentTintColor = .white + warningImageView.contentTintColor = .white + } else { + nameLabel.textColor = .labelColor + multiplierLabel.textColor = .secondaryLabelColor + checkmarkImageView.contentTintColor = .labelColor + warningImageView.contentTintColor = .labelColor + } + } + + override func draw(_ dirtyRect: NSRect) { + super.draw(dirtyRect) + + let highlighted = isHighlighted + + // Trigger detail panel on highlight change + if highlighted != wasHighlighted { + wasHighlighted = highlighted + if highlighted { + if let onHover = onHover { + let screenRect = + window?.convertToScreen(convert(bounds, to: nil)) ?? .zero + onHover(model, screenRect) + } + } else { + onHoverExit?() + } + } + + updateColors() + + if highlighted { + ModelMenuItemFormatter.drawMenuItemHighlight( + in: frame, + fontScale: fontScale, + hoverEdgeInset: constants.hoverEdgeInset + ) + } + } + + // MARK: - Width Calculation + + static func calculateItemWidth( + model: LLMModel, + multiplierText: String, + fontScale: Double + ) -> CGFloat { + let constants = LayoutConstants(fontScale: fontScale) + let font = NSFont.systemFont(ofSize: constants.fontSize, weight: .regular) + let attrs: [NSAttributedString.Key: Any] = [.font: font] + let displayName = model.displayName ?? model.modelName + let nameWidth = (displayName as NSString).size(withAttributes: attrs).width + + var width = constants.leadingPadding + constants.checkmarkSize + + constants.checkmarkToText + ceil(nameWidth) + constants.trailingPadding + + if !multiplierText.isEmpty { + let multWidth = ceil( + (multiplierText as NSString).size(withAttributes: attrs).width + ) + width += constants.nameToMultiplier + multWidth + } + + return width + } +} diff --git a/Core/Sources/ConversationTab/TerminalViews/RunInTerminalToolView.swift b/Core/Sources/ConversationTab/TerminalViews/RunInTerminalToolView.swift index 7ac6ef3c..7f6725a5 100644 --- a/Core/Sources/ConversationTab/TerminalViews/RunInTerminalToolView.swift +++ b/Core/Sources/ConversationTab/TerminalViews/RunInTerminalToolView.swift @@ -161,8 +161,7 @@ struct RunInTerminalToolView: View { .scaledFont(.body) } - if #available(macOS 13.0, *), - FeatureFlagNotifierImpl.shared.featureFlags.agentModeAutoApproval && + if FeatureFlagNotifierImpl.shared.featureFlags.agentModeAutoApproval && CopilotPolicyNotifierImpl.shared.copilotPolicy.agentModeAutoApprovalEnabled, let command, !command.isEmpty { SplitButton( @@ -191,7 +190,6 @@ struct RunInTerminalToolView: View { } } - @available(macOS 13.0, *) private func terminalMenuItems(command: String) -> [SplitButtonMenuItem] { var items: [SplitButtonMenuItem] = [] diff --git a/Core/Sources/ConversationTab/Views/BotMessage.swift b/Core/Sources/ConversationTab/Views/BotMessage.swift index c690a208..8d75db57 100644 --- a/Core/Sources/ConversationTab/Views/BotMessage.swift +++ b/Core/Sources/ConversationTab/Views/BotMessage.swift @@ -21,6 +21,7 @@ struct BotMessage: View { var followUp: ConversationFollowUp? { message.followUp } var errorMessages: [String] { message.errorMessages } var steps: [ConversationProgressStep] { message.steps } + var thinking: [MessageThinking] { message.thinking } var editAgentRounds: [AgentRound] { message.editAgentRounds } var panelMessages: [CopilotShowMessageParams] { message.panelMessages } var codeReviewRound: CodeReviewRound? { message.codeReviewRound } @@ -90,9 +91,16 @@ struct BotMessage: View { // progress step if steps.count > 0 { ProgressStep(steps: steps) - + } - + + ForEach(Array(thinking.enumerated()), id: \.offset) { index, entry in + ThinkingView( + thinking: entry, + isStreaming: index == thinking.count - 1 && isThinkingStreaming() + ) + } + if !panelMessages.isEmpty { WithPerceptionTracking { ForEach(panelMessages.indices, id: \.self) { index in @@ -100,11 +108,11 @@ struct BotMessage: View { } } } - + if editAgentRounds.count > 0 { - ProgressAgentRound(rounds: editAgentRounds, chat: chat) + ProgressAgentRound(rounds: editAgentRounds, chat: chat, isStreaming: isThinkingStreaming()) } - + if !text.isEmpty { Group{ ThemedMarkdownText(text: text, chat: chat) @@ -126,7 +134,10 @@ struct BotMessage: View { HStack { if shouldShowTurnStatus() { - TurnStatusView(message: message) + TurnStatusView( + message: message, + isSummarizingConversation: chat.isSummarizingConversation + ) .modify { view in if message.turnStatus == .inProgress { view @@ -238,17 +249,28 @@ struct BotMessage: View { let lastMessage = chat.history.last return lastMessage?.role == .assistant && lastMessage?.id == id } + + private func isThinkingStreaming() -> Bool { + guard isLatestAssistantMessage(), chat.isReceivingMessage else { return false } + switch message.turnStatus { + case .success, .error, .cancelled: return false + default: return true + } + } } private struct TurnStatusView: View { - + let message: DisplayedChatMessage - + let isSummarizingConversation: Bool + @AppStorage(\.chatFontSize) var chatFontSize - + var body: some View { HStack(spacing: 0) { - if let turnStatus = message.turnStatus { + if isSummarizingConversation { + summarizingStatus + } else if let turnStatus = message.turnStatus { switch turnStatus { case .inProgress: inProgressStatus @@ -271,12 +293,25 @@ private struct TurnStatusView: View { .controlSize(.small) .scaledScaleEffect(0.7) .scaledFrame(width: 16, height: 16) - + Text("Generating...") .scaledFont(size: chatFontSize - 1) .foregroundColor(.secondary) } } + + private var summarizingStatus: some View { + HStack(spacing: 4) { + ProgressView() + .controlSize(.small) + .scaledScaleEffect(0.7) + .scaledFrame(width: 16, height: 16) + + Text("Summarizing conversation...") + .scaledFont(size: chatFontSize - 1) + .foregroundColor(.secondary) + } + } private var completedStatus: some View { statusView(icon: "checkmark.circle.fill", iconColor: .successLightGreen, text: "Completed") diff --git a/Core/Sources/ConversationTab/Views/ChatPanelInputArea/ChatPanelInputArea.swift b/Core/Sources/ConversationTab/Views/ChatPanelInputArea/ChatPanelInputArea.swift index 251f4022..346981ef 100644 --- a/Core/Sources/ConversationTab/Views/ChatPanelInputArea/ChatPanelInputArea.swift +++ b/Core/Sources/ConversationTab/Views/ChatPanelInputArea/ChatPanelInputArea.swift @@ -19,22 +19,15 @@ struct ChatPanelInputArea: View { Button(action: { chat.send(.clearButtonTap) }) { - Group { - if #available(macOS 13.0, *) { - Image(systemName: "eraser.line.dashed.fill") - .scaledFont(.body) - } else { - Image(systemName: "trash.fill") - .scaledFont(.body) + Image(systemName: "eraser.line.dashed.fill") + .scaledFont(.body) + .padding(6) + .background { + Circle().fill(Color(nsColor: .controlBackgroundColor)) + } + .overlay { + Circle().stroke(Color(nsColor: .controlColor), lineWidth: 1) } - } - .padding(6) - .background { - Circle().fill(Color(nsColor: .controlBackgroundColor)) - } - .overlay { - Circle().stroke(Color(nsColor: .controlColor), lineWidth: 1) - } } .buttonStyle(.plain) } diff --git a/Core/Sources/ConversationTab/Views/ChatPanelInputArea/ContextSizeButton.swift b/Core/Sources/ConversationTab/Views/ChatPanelInputArea/ContextSizeButton.swift new file mode 100644 index 00000000..fdf3be11 --- /dev/null +++ b/Core/Sources/ConversationTab/Views/ChatPanelInputArea/ContextSizeButton.swift @@ -0,0 +1,196 @@ +import ConversationServiceProvider +import SharedUIComponents +import SwiftUI + +struct ContextSizeButton: View { + let contextSizeInfo: ContextSizeInfo + @State private var isHovering = false + @State private var showPopover = false + @State private var isClickTriggered = false + @State private var hoverTask: Task? + @State private var dismissTask: Task? + + private let ringSize: CGFloat = 11 + private let lineWidth: CGFloat = 1.5 + + var body: some View { + Button(action: { + hoverTask?.cancel() + dismissTask?.cancel() + isClickTriggered = true + showPopover = true + }) { + HStack(spacing: 4) { + DonutChart( + percentage: contextSizeInfo.utilizationPercentage, + ringColor: ringColor, + size: ringSize, + lineWidth: lineWidth + ) + + if isHovering { + Text("\(Int(contextSizeInfo.utilizationPercentage))%") + .scaledFont(size: 11, weight: .medium) + .foregroundColor(.primary) + .transition(.opacity) + } + } + .scaledPadding(.horizontal, 6) + .scaledPadding(.vertical, 4) + } + .buttonStyle(HoverButtonStyle(padding: 0)) + .accessibilityLabel("Context size") + .accessibilityValue(Text("\(Int(contextSizeInfo.utilizationPercentage)) percent of context tokens used")) + .accessibilityHint("Shows details about the current context size and token usage.") + .animation(.easeInOut(duration: 0.15), value: isHovering) + .onHover { hovering in + isHovering = hovering + hoverTask?.cancel() + if hovering { + dismissTask?.cancel() + hoverTask = Task { + try? await Task.sleep(nanoseconds: 2_000_000_000) + guard !Task.isCancelled else { return } + isClickTriggered = false + showPopover = true + } + } else if !isClickTriggered { + scheduleDismiss() + } + } + .onChange(of: showPopover) { newValue in + if !newValue { + isClickTriggered = false + } + } + .popover(isPresented: $showPopover, arrowEdge: .bottom) { + ContextSizePopover(info: contextSizeInfo) + .onHover { hovering in + if hovering { + dismissTask?.cancel() + } else if !isClickTriggered { + scheduleDismiss() + } + } + } + } + + private func scheduleDismiss() { + dismissTask?.cancel() + dismissTask = Task { + try? await Task.sleep(nanoseconds: 200_000_000) + guard !Task.isCancelled else { return } + showPopover = false + } + } + + private var ringColor: Color { + let pct = contextSizeInfo.utilizationPercentage + if pct >= 80 { return Color("WarningYellow") } + return .secondary + } +} + +private struct DonutChart: View { + let percentage: Double + let ringColor: Color + let size: CGFloat + let lineWidth: CGFloat + + var body: some View { + ZStack { + Circle() + .stroke(Color(nsColor: .quaternaryLabelColor), lineWidth: lineWidth) + + Circle() + .trim(from: 0, to: min(percentage / 100, 1.0)) + .stroke(ringColor, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round)) + .rotationEffect(.degrees(-90)) + } + .scaledFrame(width: size, height: size) + } +} + +private struct ContextSizePopover: View { + let info: ContextSizeInfo + + var body: some View { + VStack(alignment: .leading, spacing: 16) { + // MARK: Context Window + VStack(alignment: .leading, spacing: 6) { + Text("Context Window") + .scaledFont(.headline) + + HStack { + Text("\(formatTokens(info.totalUsedTokens)) / \(formatTokens(info.totalTokenLimit)) tokens") + Spacer() + Text(formatPercentage(info.utilizationPercentage)) + } + .scaledFont(.callout) + + ProgressView(value: min(info.utilizationPercentage, 100), total: 100) + .tint(progressColor) + } + + // MARK: System + VStack(alignment: .leading, spacing: 4) { + Text("System") + .scaledFont(.headline) + .scaledPadding(.bottom, 4) + + tokenRow("System Instructions", tokens: info.systemPromptTokens) + tokenRow("Tool Definitions", tokens: info.toolDefinitionTokens) + } + + // MARK: User + VStack(alignment: .leading, spacing: 4) { + Text("User") + .scaledFont(.headline) + .scaledPadding(.bottom, 4) + + tokenRow("Messages", tokens: info.userMessagesTokens + info.assistantMessagesTokens) + tokenRow("Attached Files", tokens: info.attachedFilesTokens) + tokenRow("Tool Results", tokens: info.toolResultsTokens) + } + + // TODO: Depends on CLS for manual compression + } + .scaledPadding(.vertical, 20) + .scaledPadding(.horizontal, 16) + .scaledFrame(width: 240) + } + + private var progressColor: Color { + let pct = info.utilizationPercentage + if pct >= 80 { return Color(nsColor: .systemYellow) } + return .accentColor + } + + private func tokenRow(_ label: String, tokens: Int) -> some View { + HStack { + Text(label) + Spacer() + Text(percentage(for: tokens)) + } + .scaledFont(.callout) + } + + private func percentage(for tokens: Int) -> String { + guard info.totalTokenLimit > 0 else { return "0%" } + let pct = Double(tokens) / Double(info.totalTokenLimit) * 100 + return formatPercentage(pct) + } + + private func formatPercentage(_ pct: Double) -> String { + if pct == 0 { return "0%" } + return String(format: "%.1f%%", pct) + } + + private func formatTokens(_ count: Int) -> String { + if count >= 1000 { + let k = Double(count) / 1000.0 + return String(format: "%.1fK", k) + } + return "\(count)" + } +} diff --git a/Core/Sources/ConversationTab/Views/ChatPanelInputArea/InputAreaTextEditor.swift b/Core/Sources/ConversationTab/Views/ChatPanelInputArea/InputAreaTextEditor.swift index 88373b42..0659dbf8 100644 --- a/Core/Sources/ConversationTab/Views/ChatPanelInputArea/InputAreaTextEditor.swift +++ b/Core/Sources/ConversationTab/Views/ChatPanelInputArea/InputAreaTextEditor.swift @@ -173,13 +173,18 @@ struct InputAreaTextEditor: View { ModeAndModelPicker(projectRootURL: projectRootURL, selectedAgent: $chat.selectedAgent) Spacer() - - if chat.editorMode.isDefault { + + if let contextSizeInfo = chat.contextSizeInfo { + ContextSizeButton(contextSizeInfo: contextSizeInfo) + .padding(.trailing, 4) + } + + if chat.editorMode.isDefault && !isRequestingConversation { codeReviewButton .buttonStyle(HoverButtonStyle(padding: 0, hoverColor: .clear)) - .opacity(isRequestingConversation ? 0 : 1) + .padding(.trailing, 4) } - + ZStack { sendButton .opacity(isRequestingConversation || isRequestingCodeReview ? 0 : 1) diff --git a/Core/Sources/ConversationTab/Views/ConversationAgentProgressView/ConversationAgentProgressView.swift b/Core/Sources/ConversationTab/Views/ConversationAgentProgressView/ConversationAgentProgressView.swift index 95f0e91a..f002f643 100644 --- a/Core/Sources/ConversationTab/Views/ConversationAgentProgressView/ConversationAgentProgressView.swift +++ b/Core/Sources/ConversationTab/Views/ConversationAgentProgressView/ConversationAgentProgressView.swift @@ -10,13 +10,25 @@ import SwiftUI struct ProgressAgentRound: View { let rounds: [AgentRound] let chat: StoreOf + var isStreaming: Bool = false var body: some View { WithPerceptionTracking { VStack(alignment: .leading, spacing: 8) { - ForEach(rounds, id: \.roundId) { round in + ForEach(Array(rounds.enumerated()), id: \.element.roundId) { roundIndex, round in + let isLastRound = roundIndex == rounds.count - 1 VStack(alignment: .leading, spacing: 8) { - ThemedMarkdownText(text: round.reply, chat: chat) + ForEach(Array(round.thinking.enumerated()), id: \.offset) { entryIndex, entry in + ThinkingView( + thinking: entry, + isStreaming: isStreaming + && isLastRound + && entryIndex == round.thinking.count - 1 + ) + } + if !round.reply.isEmpty { + ThemedMarkdownText(text: round.reply, chat: chat) + } if let toolCalls = round.toolCalls, !toolCalls.isEmpty { ProgressToolCalls(tools: toolCalls, chat: chat) } @@ -42,7 +54,12 @@ struct SubAgentRounds: View { VStack(alignment: .leading, spacing: 8) { ForEach(rounds, id: \.roundId) { round in VStack(alignment: .leading, spacing: 8) { - ThemedMarkdownText(text: round.reply, chat: chat) + ForEach(Array(round.thinking.enumerated()), id: \.offset) { _, entry in + ThinkingView(thinking: entry, isStreaming: false) + } + if !round.reply.isEmpty { + ThemedMarkdownText(text: round.reply, chat: chat) + } if let toolCalls = round.toolCalls, !toolCalls.isEmpty { ProgressToolCalls(tools: toolCalls, chat: chat) } @@ -103,8 +120,7 @@ struct ToolConfirmationView: View { @ViewBuilder private var confirmationActionView: some View { - if #available(macOS 13.0, *), - FeatureFlagNotifierImpl.shared.featureFlags.agentModeAutoApproval && + if FeatureFlagNotifierImpl.shared.featureFlags.agentModeAutoApproval && CopilotPolicyNotifierImpl.shared.copilotPolicy.agentModeAutoApprovalEnabled { if tool.isToolcallingLoopContinueTool { continueButton @@ -150,7 +166,6 @@ struct ToolConfirmationView: View { .buttonStyle(.borderedProminent) } - @available(macOS 13.0, *) private var sensitiveFileMenuItems: [SplitButtonMenuItem] { var items: [SplitButtonMenuItem] = [] @@ -200,7 +215,6 @@ struct ToolConfirmationView: View { return items } - @available(macOS 13.0, *) private var sensitiveFileSplitButton: some View { SplitButton( title: "Allow", @@ -213,7 +227,6 @@ struct ToolConfirmationView: View { ) } - @available(macOS 13.0, *) private func mcpMenuItems(serverName: String) -> [SplitButtonMenuItem] { var items: [SplitButtonMenuItem] = [] @@ -288,7 +301,6 @@ struct ToolConfirmationView: View { return items } - @available(macOS 13.0, *) private func mcpSplitButton(serverName: String) -> some View { SplitButton( title: "Allow", @@ -389,23 +401,56 @@ struct GenericToolTitleView: View { struct ProgressAgentRound_Preview: PreviewProvider { static let agentRounds: [AgentRound] = [ .init(roundId: 1, reply: "this is agent step", toolCalls: [ + // Completed read file .init( id: "toolcall_001", - name: "Tool Call 1", - progressMessage: "Read Tool Call 1", - status: .completed, - error: nil), + name: ServerToolName.readFile.rawValue, + progressMessage: "Read src/AppDelegate.swift", + status: .completed), + // Completed file search with results .init( id: "toolcall_002", - name: "Tool Call 2", - progressMessage: "Running Tool Call 2", + name: ServerToolName.findFiles.rawValue, + progressMessage: "Searched for files matching query: **/*.swift", + status: .completed, + resultDetails: [ + .fileLocation(.init(uri: "file:///src/App.swift", range: .init(start: .init(line: 0, character: 0), end: .init(line: 10, character: 0)))), + .fileLocation(.init(uri: "file:///src/Model.swift", range: .init(start: .init(line: 0, character: 0), end: .init(line: 5, character: 0)))), + .fileLocation(.init(uri: "file:///src/ViewModel.swift", range: .init(start: .init(line: 0, character: 0), end: .init(line: 8, character: 0)))), + ]), + // Completed create file (expandable) + .init( + id: "toolcall_003", + name: ToolName.createFile.rawValue, + progressMessage: "Created src/NewFeature.swift", + status: .completed, + result: [.text("```swift\nstruct NewFeature {\n var name: String\n}\n```")]), + // Completed replace string (expandable) + .init( + id: "toolcall_004", + name: ServerToolName.replaceString.rawValue, + progressMessage: "Edited src/Config.swift", + status: .completed, + result: [.text("```diff\n- let version = \"1.0\"\n+ let version = \"2.0\"\n```")]), + // Running tool + .init( + id: "toolcall_005", + name: ServerToolName.codebase.rawValue, + progressMessage: "Searching codebase for references", status: .running), + // Error tool + .init( + id: "toolcall_006", + name: ServerToolName.readFile.rawValue, + progressMessage: "Read missing_file.swift", + status: .error, + error: "File not found"), ]), ] static var previews: some View { let chatTabInfo = ChatTabInfo(id: "id", workspacePath: "path", username: "name") ProgressAgentRound(rounds: agentRounds, chat: .init(initialState: .init(), reducer: { Chat(service: ChatService.service(for: chatTabInfo)) })) - .frame(width: 300, height: 300) + .frame(width: 400, height: 500) } } diff --git a/Core/Sources/ConversationTab/Views/ConversationAgentProgressView/ToolStatusItemView.swift b/Core/Sources/ConversationTab/Views/ConversationAgentProgressView/ToolStatusItemView.swift index 9238a932..fc87e3bb 100644 --- a/Core/Sources/ConversationTab/Views/ConversationAgentProgressView/ToolStatusItemView.swift +++ b/Core/Sources/ConversationTab/Views/ConversationAgentProgressView/ToolStatusItemView.swift @@ -510,22 +510,22 @@ private struct ToolStatusDetailsView: View { @AppStorage(\.fontScale) var fontScale var body: some View { - VStack(alignment: .leading, spacing: 6) { + VStack(alignment: .leading, spacing: 2) { Button(action: { isExpanded.toggle() }) { - HStack(spacing: 8) { + HStack(spacing: 2) { title - Spacer() - Image(systemName: isExpanded ? "chevron.down" : "chevron.right") .resizable() .scaledToFit() .padding(4) .scaledFrame(width: 16, height: 16) .scaledFont(size: 10, weight: .medium) + + Spacer() } .contentShape(RoundedRectangle(cornerRadius: 6)) } @@ -534,9 +534,6 @@ private struct ToolStatusDetailsView: View { .toolStatusStyle(withBackground: !isExpanded, fontScale: fontScale) if isExpanded { - Divider() - .background(Color.agentToolStatusDividerColor) - content .scaledPadding(.horizontal, 8) } @@ -552,10 +549,6 @@ private extension View { if withBackground { view .scaledPadding(.vertical, 6) - .background( - RoundedRectangle(cornerRadius: 6) - .stroke(Color.agentToolStatusOutlineColor, lineWidth: 1 * fontScale) - ) } else { view } @@ -578,3 +571,64 @@ private extension View { } } } + +// MARK: - Preview + +struct ToolStatusItemView_Preview: PreviewProvider { + static var previews: some View { + VStack(alignment: .leading, spacing: 4) { + // Completed read file + ToolStatusItemView(tool: .init( + id: "1", + name: ServerToolName.readFile.rawValue, + progressMessage: "Read src/AppDelegate.swift", + status: .completed + )) + // Completed file search + ToolStatusItemView(tool: .init( + id: "2", + name: ServerToolName.findFiles.rawValue, + progressMessage: "Searched for files matching query: **/*.swift", + status: .completed, + resultDetails: [ + .fileLocation(.init(uri: "file:///src/App.swift", range: .init(start: .init(line: 0, character: 0), end: .init(line: 10, character: 0)))), + .fileLocation(.init(uri: "file:///src/Model.swift", range: .init(start: .init(line: 0, character: 0), end: .init(line: 5, character: 0)))), + ] + )) + // Completed create file (expandable) + ToolStatusItemView(tool: .init( + id: "3", + name: ToolName.createFile.rawValue, + progressMessage: "Created src/NewFeature.swift", + status: .completed, + result: [.text("struct NewFeature {\n var name: String\n}")] + )) + // Completed replace string (expandable) + ToolStatusItemView(tool: .init( + id: "4", + name: ServerToolName.replaceString.rawValue, + progressMessage: "Edited src/Config.swift", + status: .completed, + result: [.text("- let version = \"1.0\"\n+ let version = \"2.0\"")] + )) + // Running + ToolStatusItemView(tool: .init( + id: "5", + name: ServerToolName.codebase.rawValue, + progressMessage: "Searching codebase", + status: .running + )) + // Error + ToolStatusItemView(tool: .init( + id: "6", + name: ServerToolName.readFile.rawValue, + progressMessage: "Read missing_file.swift", + status: .error, + error: "File not found" + )) + } + .padding() + .frame(width: 400) + .colorScheme(.dark) + } +} diff --git a/Core/Sources/ConversationTab/Views/NotificationBanner.swift b/Core/Sources/ConversationTab/Views/NotificationBanner.swift index f5047793..89062b0a 100644 --- a/Core/Sources/ConversationTab/Views/NotificationBanner.swift +++ b/Core/Sources/ConversationTab/Views/NotificationBanner.swift @@ -2,16 +2,19 @@ import SwiftUI import SharedUIComponents public enum BannerStyle { + case info case warning var iconName: String { switch self { - case .warning: return "exclamationmark.triangle" + case .info: return "info.circle.fill" + case .warning: return "exclamationmark.triangle.fill" } } var color: Color { switch self { + case .info: return .blue case .warning: return .orange } } @@ -19,6 +22,8 @@ public enum BannerStyle { struct NotificationBanner: View { var style: BannerStyle + var isDismissable: Bool = false + var onDismiss: (() -> Void)? = nil @ViewBuilder var content: () -> Content @AppStorage(\.chatFontSize) var chatFontSize @@ -31,15 +36,25 @@ struct NotificationBanner: View { VStack(alignment: .leading, spacing: 8) { content() } + .frame(maxWidth: .infinity, alignment: .leading) + + if isDismissable { + Button(action: { onDismiss?() }) { + Image(systemName: "xmark") + .foregroundColor(.secondary) + } + .buttonStyle(HoverButtonStyle()) + } } .scaledFont(size: chatFontSize - 1) } .frame(maxWidth: .infinity, alignment: .topLeading) .scaledPadding(.vertical, 10) .scaledPadding(.horizontal, 12) + .background(Color("BannerBackgroundColor")) .overlay( - RoundedRectangle(cornerRadius: 6) - .stroke(Color(nsColor: .separatorColor), lineWidth: 1) + RoundedRectangle(cornerRadius: 8) + .stroke(Color("BannerBorderColor"), lineWidth: 1) ) } } diff --git a/Core/Sources/ConversationTab/Views/RateLimitWarningBanner.swift b/Core/Sources/ConversationTab/Views/RateLimitWarningBanner.swift new file mode 100644 index 00000000..7fd01e4c --- /dev/null +++ b/Core/Sources/ConversationTab/Views/RateLimitWarningBanner.swift @@ -0,0 +1,43 @@ +import SharedUIComponents +import SwiftUI + +struct RateLimitWarningBanner: View { + let message: String + let onDismiss: () -> Void + + @State private var isLinkHovered = false + + var body: some View { + NotificationBanner(style: .info, isDismissable: true, onDismiss: onDismiss) { + VStack(alignment: .leading, spacing: 8) { + Text(message) + .foregroundColor(.primary) + .frame(maxWidth: .infinity, alignment: .leading) + + Button(action: { + if let url = URL(string: "https://aka.ms/github-copilot-rate-limit-error") { + NSWorkspace.shared.open(url) + } + }) { + Text("Learn more") + .underline(isLinkHovered) + .foregroundColor(.accentColor) + } + .buttonStyle(.plain) + .onHover { isHovered in + isLinkHovered = isHovered + DispatchQueue.main.async { + if isHovered { + NSCursor.pointingHand.push() + } else { + NSCursor.pop() + } + } + } + .onDisappear { + NSCursor.pop() + } + } + } + } +} diff --git a/Core/Sources/ConversationTab/Views/ThemedMarkdownText.swift b/Core/Sources/ConversationTab/Views/ThemedMarkdownText.swift index 086d724e..f9a1409b 100644 --- a/Core/Sources/ConversationTab/Views/ThemedMarkdownText.swift +++ b/Core/Sources/ConversationTab/Views/ThemedMarkdownText.swift @@ -174,15 +174,14 @@ struct MarkdownCodeBlockView: View { struct ThemedMarkdownText_Previews: PreviewProvider { static var previews: some View { - let chatTabInfo = ChatTabInfo(id: "id", workspacePath: "path", username: "name") ThemedMarkdownText( - text:""" - ```swift - let sumClosure: (Int, Int) -> Int = { (a: Int, b: Int) in - return a + b - } - ``` - """, - context: .init(onInsert: {_ in print("Inserted") })) + text: """ + ```swift + let sumClosure: (Int, Int) -> Int = { (a: Int, b: Int) in + return a + b + } + ``` + """, + context: .init(onInsert: { _ in print("Inserted") })) } } diff --git a/Core/Sources/ConversationTab/Views/ThinkingView.swift b/Core/Sources/ConversationTab/Views/ThinkingView.swift new file mode 100644 index 00000000..777bf7e2 --- /dev/null +++ b/Core/Sources/ConversationTab/Views/ThinkingView.swift @@ -0,0 +1,144 @@ +import SwiftUI +import ComposableArchitecture +import ConversationServiceProvider +import SharedUIComponents + +struct ThinkingView: View { + let thinking: MessageThinking + let isStreaming: Bool + + @AppStorage(\.chatFontSize) var chatFontSize + @State private var isExpandedOverride: Bool? = nil + + private var sections: [ThinkingSection] { + MessageThinking.parseSections(from: thinking.text?.joined() ?? "") + } + + private var titleText: String { + if isStreaming { + return "Thinking..." + } + if let title = thinking.title, !title.isEmpty { + return title + } + return "Thinking" + } + + private var isExpanded: Bool { + if let override = isExpandedOverride { return override } + return isStreaming + } + + private var isAutoExpandedWhileStreaming: Bool { + isStreaming && isExpandedOverride == nil + } + + private static let autoExpandMaxHeight: CGFloat = 180 + private static let scrollAnchorID = "thinking-bottom-anchor" + + var body: some View { + WithPerceptionTracking { + let sections = sections + let hasContent = sections.contains { $0.title != nil || !$0.body.isEmpty } + if hasContent || isStreaming { + content(sections: sections, hasContent: hasContent) + } + } + } + + @ViewBuilder + private func content(sections: [ThinkingSection], hasContent: Bool) -> some View { + VStack(alignment: .leading, spacing: 8) { + Button { + isExpandedOverride = !isExpanded + } label: { + HStack(spacing: 2) { + Text(titleText) + .scaledFont(size: chatFontSize - 1) + .lineLimit(1) + .truncationMode(.tail) + Image(systemName: isExpanded ? "chevron.down" : "chevron.right") + .resizable() + .scaledToFit() + .padding(4) + .scaledFrame(width: 16, height: 16) + .scaledFont(size: 10, weight: .medium) + } + .foregroundStyle(.secondary) + } + .buttonStyle(.plain) + + if isExpanded, hasContent { + sectionsContainer(sections: sections) + } + } + } + + @ViewBuilder + private func sectionsContainer(sections: [ThinkingSection]) -> some View { + let stack = VStack(alignment: .leading, spacing: 8) { + ForEach(Array(sections.enumerated()), id: \.offset) { _, section in + sectionView(section) + } + Color.clear + .frame(height: 0) + .id(Self.scrollAnchorID) + } + .fixedSize(horizontal: false, vertical: true) + + if isAutoExpandedWhileStreaming { + ScrollViewReader { proxy in + ScrollView(.vertical, showsIndicators: false) { + stack + } + .frame(maxHeight: Self.autoExpandMaxHeight) + .onChange(of: thinking.text?.joined() ?? "") { _ in + withAnimation(.easeOut(duration: 0.15)) { + proxy.scrollTo(Self.scrollAnchorID, anchor: .bottom) + } + } + .onAppear { + proxy.scrollTo(Self.scrollAnchorID, anchor: .bottom) + } + } + } else { + stack + } + } + + @ViewBuilder + private func sectionView(_ section: ThinkingSection) -> some View { + HStack(alignment: .top, spacing: 8) { + VStack(spacing: 4) { + Circle() + .fill(Color.secondary.opacity(0.3)) + .frame(width: 4, height: 4) + .padding(.top, 6) + Rectangle() + .fill(Color.secondary.opacity(0.3)) + .frame(width: 1) + .frame(maxHeight: .infinity) + } + .frame(width: 4) + + VStack(alignment: .leading, spacing: 4) { + if let title = section.title, !title.isEmpty { + Text(title) + .scaledFont(size: chatFontSize - 1) + .fontWeight(.semibold) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + } + if !section.body.isEmpty { + ThemedMarkdownText( + text: section.body, + context: MarkdownActionProvider(supportInsert: false), + foregroundColor: .secondary + ) + .fixedSize(horizontal: false, vertical: true) + } + } + } + .fixedSize(horizontal: false, vertical: true) + } +} diff --git a/Core/Sources/HostApp/AdvancedSettings/ChatSection.swift b/Core/Sources/HostApp/AdvancedSettings/ChatSection.swift index fc44276e..a8910979 100644 --- a/Core/Sources/HostApp/AdvancedSettings/ChatSection.swift +++ b/Core/Sources/HostApp/AdvancedSettings/ChatSection.swift @@ -36,30 +36,31 @@ struct ChatSection: View { .padding(SettingsToggle.defaultPadding) Divider() + + } - if featureFlags.isAgentModeEnabled && copilotPolicy.isCustomAgentEnabled { - // Custom Agents - .github/agents/*.agent.md - AgentFileSetting(promptType: .agent) - .padding(SettingsToggle.defaultPadding) - - Divider() - - // SubAgent toggle - SettingsToggle( - title: "Enable Subagent", - subtitle: "Allows Copilot Agent mode to call custom agents as subagent. Requires GitHub Copilot for Xcode restart to take effect.", - isOn: Binding( - get: { enableSubagent && copilotPolicy.isSubagentEnabled }, - set: { if copilotPolicy.isSubagentEnabled { enableSubagent = $0 } } - ), - badge: copilotPolicy.isSubagentEnabled - ? nil - : .disabledByPolicy(feature: "Subagents", isPlural: true) - ) - .disabled(!copilotPolicy.isSubagentEnabled) + if featureFlags.isAgentModeEnabled && copilotPolicy.isCustomAgentEnabled { + // Custom Agents - .github/agents/*.agent.md + AgentFileSetting(promptType: .agent) + .padding(SettingsToggle.defaultPadding) - Divider() - } + Divider() + + // SubAgent toggle + SettingsToggle( + title: "Enable Subagent", + subtitle: "Allows Copilot Agent mode to call custom agents as subagent. Requires GitHub Copilot for Xcode restart to take effect.", + isOn: Binding( + get: { enableSubagent && copilotPolicy.isSubagentEnabled }, + set: { if copilotPolicy.isSubagentEnabled { enableSubagent = $0 } } + ), + badge: copilotPolicy.isSubagentEnabled + ? nil + : .disabledByPolicy(feature: "Subagents", isPlural: true) + ) + .disabled(!copilotPolicy.isSubagentEnabled) + + Divider() } // Auto Attach toggle @@ -88,13 +89,18 @@ struct ChatSection: View { FontSizeSetting() .padding(SettingsToggle.defaultPadding) + Divider() + if featureFlags.isAgentModeEnabled { - Divider() - // Agent Max Tool Calling Requests AgentMaxToolCallLoopSetting() .padding(SettingsToggle.defaultPadding) + + Divider() } + + // Auto Compress + AgentAutoCompressSetting() } } } @@ -336,6 +342,26 @@ struct AgentMaxToolCallLoopSetting: View { } } +struct AgentAutoCompressSetting: View { + @AppStorage(\.autoCompress) var autoCompress + + var body: some View { + SettingsToggle( + title: "Auto Compress", + subtitle: "Automatically compact the conversation history to save contect tokens.", + isOn: Binding( + get: { autoCompress }, + set: { + autoCompress = $0 + DistributedNotificationCenter + .default() + .post(name: .githubCopilotAgentAutoCompressDidChange, object: nil) + } + ) + ) + } +} + struct CopilotInstructionSetting: View { @State var isGlobalInstructionsViewOpen = false @Environment(\.toast) var toast diff --git a/Core/Sources/HostApp/AdvancedSettings/DisabledLanguageList.swift b/Core/Sources/HostApp/AdvancedSettings/DisabledLanguageList.swift index d869b9ca..2ccbdd71 100644 --- a/Core/Sources/HostApp/AdvancedSettings/DisabledLanguageList.swift +++ b/Core/Sources/HostApp/AdvancedSettings/DisabledLanguageList.swift @@ -5,13 +5,8 @@ import SharedUIComponents extension List { @ViewBuilder func removeBackground() -> some View { - if #available(macOS 13.0, *) { - scrollContentBackground(.hidden) - .listRowBackground(EmptyView()) - } else { - background(Color.clear) - .listRowBackground(EmptyView()) - } + scrollContentBackground(.hidden) + .listRowBackground(EmptyView()) } } @@ -80,11 +75,7 @@ struct DisabledLanguageList: View { } } .modify { view in - if #available(macOS 13.0, *) { - view.listRowSeparator(.hidden).listSectionSeparator(.hidden) - } else { - view - } + view.listRowSeparator(.hidden).listSectionSeparator(.hidden) } } .removeBackground() diff --git a/Core/Sources/HostApp/ToolsConfigView.swift b/Core/Sources/HostApp/ToolsConfigView.swift index 7dbb1ba1..6ece9ade 100644 --- a/Core/Sources/HostApp/ToolsConfigView.swift +++ b/Core/Sources/HostApp/ToolsConfigView.swift @@ -23,7 +23,7 @@ struct MCPConfigView: View { @Environment(\.colorScheme) var colorScheme private var isCustomAgentEnabled: Bool { - featureFlags.isEditorPreviewEnabled && copilotPolicy.isCustomAgentEnabled + copilotPolicy.isCustomAgentEnabled } private static var lastSyncTimestamp: Date? = nil @@ -64,6 +64,8 @@ struct MCPConfigView: View { MCPRegistryURLView() } + MCPXcodeServerInstallView() + MCPToolsListView( selectedMode: $selectedMode, isCustomAgentEnabled: isCustomAgentEnabled diff --git a/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPRegistryInstallation.swift b/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPRegistryInstallation.swift index c54712ca..52b1264b 100644 --- a/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPRegistryInstallation.swift +++ b/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPRegistryInstallation.swift @@ -90,9 +90,55 @@ public class MCPRegistryService: ObservableObject { public static let shared = MCPRegistryService() public static let apiVersion = "v0.1" @AppStorage(\.mcpRegistryBaseURL) var mcpRegistryBaseURL - + @Published public private(set) var mcpRegistryEntries: [MCPRegistryEntry]? + private init() {} + /// Fetches the MCP registry allowlist from the language server and updates + /// ``mcpRegistryEntries``. Safe to call from any view's `onAppear` – + /// duplicate in-flight calls are coalesced via the `isRefreshing` flag. + private var isRefreshing = false + + public func refreshAllowlist() async { + guard !isRefreshing else { return } + isRefreshing = true + defer { isRefreshing = false } + + do { + let service = try getService() + + let authStatus = try await service.getXPCServiceAuthStatus() + guard authStatus?.status == .loggedIn else { + Logger.client.info("User not logged in, skipping MCP registry allowlist fetch") + mcpRegistryEntries = nil + return + } + + let result = try await service.getMCPRegistryAllowlist() + + guard let result = result, !result.mcpRegistries.isEmpty else { + if result == nil { + Logger.client.error("Failed to get allowlist result") + } else { + mcpRegistryEntries = [] + } + return + } + + if let firstRegistry = result.mcpRegistries.first { + let entry = MCPRegistryEntry( + url: firstRegistry.url, + registryAccess: firstRegistry.registryAccess, + owner: firstRegistry.owner + ) + mcpRegistryEntries = [entry] + Logger.client.info("Current MCP Registry Entry: \(entry)") + } + } catch { + Logger.client.error("Failed to get MCP allowlist from registry: \(error)") + } + } + public static func getServerName(from serverDetail: MCPRegistryServerDetail) -> String { return serverDetail.name } @@ -304,7 +350,7 @@ public class MCPRegistryService: ObservableObject { // Save configuration let jsonData = try JSONSerialization.data(withJSONObject: config, options: [.prettyPrinted]) - try jsonData.write(to: configFileURL) + try jsonData.write(to: configFileURL, options: .atomic) // Note: UserDefaults update and notification will be handled by ToolsConfigView's file monitor // with debouncing to prevent duplicate notifications diff --git a/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPRegistryURLView.swift b/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPRegistryURLView.swift index b3cb3537..e01b90c5 100644 --- a/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPRegistryURLView.swift +++ b/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPRegistryURLView.swift @@ -14,7 +14,7 @@ struct MCPRegistryURLView: View { @State private var isLoading: Bool = false @State private var tempURLText: String = "" @State private var errorMessage: String = "" - @State private var mcpRegistry: [MCPRegistryEntry]? = nil + @ObservedObject private var registryService = MCPRegistryService.shared private let maxURLLength = 2048 private let mcpRegistryUrlVersion = "/v0.1/servers" @@ -48,7 +48,7 @@ struct MCPRegistryURLView: View { } .buttonStyle(.bordered) .help("Configure your MCP Registry Base URL") - .disabled(mcpRegistry?.first?.registryAccess == .registryOnly) + .disabled(registryService.mcpRegistryEntries?.first?.registryAccess == .registryOnly) Button { Task{ await loadMCPServers() } } label: { HStack(spacing: 0) { @@ -74,7 +74,7 @@ struct MCPRegistryURLView: View { urlText: $tempURLText, maxURLLength: maxURLLength, isSheet: false, - mcpRegistryEntry: mcpRegistry?.first, + mcpRegistryEntry: registryService.mcpRegistryEntries?.first, onValidationChange: { _ in // Only validate, don't update mcpRegistryURL here }, @@ -115,7 +115,7 @@ struct MCPRegistryURLView: View { tempURLText = newValue Task { await updateGalleryWindowIfOpen() } } - .onChange(of: mcpRegistry) { _ in + .onChange(of: registryService.mcpRegistryEntries) { _ in Task { await updateGalleryWindowIfOpen() } } } @@ -145,7 +145,7 @@ struct MCPRegistryURLView: View { mcpRegistryBaseURLHistory.addToHistory(mcpRegistryBaseURL) errorMessage = "" - MCPServerGalleryWindow.open(serverList: serverList, mcpRegistryEntry: mcpRegistry?.first) + MCPServerGalleryWindow.open(serverList: serverList, mcpRegistryEntry: registryService.mcpRegistryEntries?.first) } catch { Logger.client.error("Failed to load MCP servers from registry: \(error.localizedDescription)") if let serviceError = error as? XPCExtensionServiceError { @@ -160,44 +160,14 @@ struct MCPRegistryURLView: View { private func getMCPRegistryAllowlist() async { isLoading = true defer { isLoading = false } - do { - let service = try getService() - - // Only fetch allowlist if user is logged in - let authStatus = try await service.getXPCServiceAuthStatus() - guard authStatus?.status == .loggedIn else { - Logger.client.info("User not logged in, skipping MCP registry allowlist fetch") - return - } - - let result = try await service.getMCPRegistryAllowlist() - - guard let result = result, !result.mcpRegistries.isEmpty else { - if result == nil { - Logger.client.error("Failed to get allowlist result") - } else { - mcpRegistry = [] - } - return - } - - if let firstRegistry = result.mcpRegistries.first { - let entry = MCPRegistryEntry( - url: firstRegistry.url, - registryAccess: firstRegistry.registryAccess, - owner: firstRegistry.owner - ) - mcpRegistry = [entry] - Logger.client.info("Current MCP Registry Entry: \(entry)") - - // If registryOnly, force the URL to be the registry URL - if entry.registryAccess == .registryOnly { - mcpRegistryBaseURL = entry.url - tempURLText = entry.url - } - } - } catch { - Logger.client.error("Failed to get MCP allowlist from registry: \(error)") + + await registryService.refreshAllowlist() + + // If registryOnly, force the URL to be the registry URL + if let entry = registryService.mcpRegistryEntries?.first, + entry.registryAccess == .registryOnly { + mcpRegistryBaseURL = entry.url + tempURLText = entry.url } } @@ -211,7 +181,7 @@ struct MCPRegistryURLView: View { defer { isLoading = false } // Let the view model handle the entire update flow including clearing and fetching - if let error = await MCPServerGalleryWindow.refreshFromURL(mcpRegistryEntry: mcpRegistry?.first) { + if let error = await MCPServerGalleryWindow.refreshFromURL(mcpRegistryEntry: registryService.mcpRegistryEntries?.first) { // Display error in the URL view if let serviceError = error as? XPCExtensionServiceError { errorMessage = serviceError.underlyingError?.localizedDescription ?? serviceError.localizedDescription diff --git a/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPServerDetailSheet.swift b/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPServerDetailSheet.swift index 6c086ba6..b462519a 100644 --- a/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPServerDetailSheet.swift +++ b/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPServerDetailSheet.swift @@ -4,7 +4,6 @@ import GitHubCopilotService import SharedUIComponents import Foundation -@available(macOS 13.0, *) struct MCPServerDetailSheet: View { let server: MCPRegistryServerDetail let meta: ServerMeta? diff --git a/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPServerGalleryView.swift b/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPServerGalleryView.swift index 31e138fa..0082b480 100644 --- a/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPServerGalleryView.swift +++ b/Core/Sources/HostApp/ToolsSettings/MCPRegistry/MCPServerGalleryView.swift @@ -287,38 +287,28 @@ struct MCPServerGalleryView: View { .buttonStyle(DestructiveButtonStyle()) .help("Uninstall") } else { - if #available(macOS 13.0, *) { - SplitButton( - title: "Install", - isDisabled: viewModel.hasNoDeployments(response.server), - primaryAction: { - // Install with default configuration - Task { - await viewModel.installServer(response.server) - } - }, - menuItems: { - let options = viewModel.getInstallationOptions(for: response.server) - guard !options.isEmpty else { return [] } - return [SplitButtonMenuItem.header("Install Server With")] + options.map { option in - SplitButtonMenuItem(title: option.displayName) { - Task { - await viewModel.installServer(response.server, configuration: option.displayName) - } - } - } - }() - ) - .help("Install") - } else { - Button("Install") { + SplitButton( + title: "Install", + isDisabled: viewModel.hasNoDeployments(response.server), + primaryAction: { + // Install with default configuration Task { await viewModel.installServer(response.server) } - } - .disabled(viewModel.hasNoDeployments(response.server)) - .help("Install") - } + }, + menuItems: { + let options = viewModel.getInstallationOptions(for: response.server) + guard !options.isEmpty else { return [] } + return [SplitButtonMenuItem.header("Install Server With")] + options.map { option in + SplitButtonMenuItem(title: option.displayName) { + Task { + await viewModel.installServer(response.server, configuration: option.displayName) + } + } + } + }() + ) + .help("Install") } Button { @@ -340,11 +330,7 @@ struct MCPServerGalleryView: View { } private func infoSheet(_ response: MCPRegistryServerResponse) -> some View { - if #available(macOS 13.0, *) { - return AnyView(MCPServerDetailSheet(response: response)) - } else { - return AnyView(EmptyView()) - } + MCPServerDetailSheet(response: response) } } diff --git a/Core/Sources/HostApp/ToolsSettings/MCPXcodeServerInstallView.swift b/Core/Sources/HostApp/ToolsSettings/MCPXcodeServerInstallView.swift new file mode 100644 index 00000000..ccf83061 --- /dev/null +++ b/Core/Sources/HostApp/ToolsSettings/MCPXcodeServerInstallView.swift @@ -0,0 +1,234 @@ +import GitHubCopilotService +import Logger +import SharedUIComponents +import SwiftUI +import SystemUtils + +struct MCPXcodeServerInstallView: View { + @State private var xcodeVersion: String? = SystemUtils.xcodeVersion + @State private var isConfigured: Bool = false + @State private var isInstalling: Bool = false + @State private var installError: String? = nil + /// Server names from mcp.json whose config matches xcrun mcpbridge. + /// Cached to avoid repeated file I/O during SwiftUI rendering. + @State private var configuredXcodeServerNames: Set = [] + @ObservedObject private var mcpToolManager = CopilotMCPToolManagerObservable.shared + @ObservedObject private var registryService = MCPRegistryService.shared + + private let requiredXcodeVersion = "26.4" + private let serverName = "xcode" + + private var meetsVersionRequirement: Bool { + guard let version = xcodeVersion else { return false } + return version.compare(requiredXcodeVersion, options: .numeric) != .orderedAscending + } + + private var isConnected: Bool { + mcpToolManager.availableMCPServerTools.contains { server in + configuredXcodeServerNames.contains(server.name) && + server.status == .running && + !server.tools.isEmpty + } + } + + /// Configured in mcp.json but not yet showing in available tools from the language server + private var isConfiguredButNotConnected: Bool { + isConfigured && !isConnected + } + + private var isAlreadyInstalled: Bool { + isConfigured || isConnected + } + + private var isRegistryOnly: Bool { + registryService.mcpRegistryEntries?.first?.registryAccess == .registryOnly + } + + var body: some View { + HStack(alignment: .center, spacing: 16) { + VStack(alignment: .leading, spacing: 0) { + Text("Xcode MCP Server") + .font(.headline) + .padding(.vertical, 4) + + subtitleView() + .font(.subheadline) + .foregroundColor(.secondary) + } + + Spacer() + + actionsView() + .padding(.vertical, 12) + } + .padding(EdgeInsets(top: 8, leading: 20, bottom: 8, trailing: 20)) + .background(QuaternarySystemFillColor.opacity(0.75)) + .settingsContainerStyle(isExpanded: false) + .onAppear { + checkInstallationStatus() + Task { await registryService.refreshAllowlist() } + } + .onChange(of: mcpToolManager.availableMCPServerTools) { _ in + checkInstallationStatus() + } + } + + // MARK: - Subviews + + @ViewBuilder + private func subtitleView() -> some View { + if !meetsVersionRequirement { + let versionText = xcodeVersion ?? "unknown" + Text("Requires Xcode \(requiredXcodeVersion) or later. Current version: \(versionText).") + } else if isConnected { + Text("Xcode's built-in MCP server is connected, enabling richer editor integration.") + } else if isRegistryOnly { + Text("Manual installation of Xcode's built-in MCP server is blocked by your organization's registry policy. Please check the MCP Registry for an approved installation option, or contact your enterprise IT administrator.") + } else if isConfiguredButNotConnected { + Text("Please confirm in Xcode to allow the built-in MCP server.") + } else { + VStack(alignment: .leading, spacing: 4) { + Text("Connect Copilot to Xcode's built-in MCP server to enable richer editor integration.") + if let installError { + Text(installError) + .font(.caption) + .foregroundColor(.red) + } + } + } + } + + @ViewBuilder + private func actionsView() -> some View { + if !meetsVersionRequirement { + EmptyView() + } else if isConnected { + Text("Connected").foregroundColor(.secondary) + } else if isRegistryOnly { + EmptyView() + } else if isConfiguredButNotConnected { + HStack(spacing: 6) { + ProgressView() + .controlSize(.small) + Text("Waiting for connection...") + .foregroundColor(.secondary) + } + } else { + Button { + installXcodeMCPServer() + } label: { + HStack(spacing: 4) { + if isInstalling { + ProgressView() + .controlSize(.small) + } else { + Image(systemName: "plus.circle") + .resizable() + .aspectRatio(contentMode: .fit) + .frame(width: 12, height: 12, alignment: .center) + .padding(2) + } + Text("Install") + } + .conditionalFontWeight(.semibold) + } + .buttonStyle(.bordered) + .disabled(isInstalling) + } + } + + // MARK: - Actions + + private func checkInstallationStatus() { + let (configured, names) = readXcodeMCPServerNamesFromConfig() + isConfigured = configured + configuredXcodeServerNames = names + } + + /// Returns (isConfigured, setOfMatchingServerNames) by reading mcp.json once. + private func readXcodeMCPServerNamesFromConfig() -> (Bool, Set) { + let configFileURL = URL(fileURLWithPath: mcpConfigFilePath) + guard FileManager.default.fileExists(atPath: configFileURL.path), + let data = try? Data(contentsOf: configFileURL), + let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any], + let servers = json["servers"] as? [String: Any] + else { + return (false, []) + } + + var names = Set() + for (key, value) in servers { + guard let serverConfig = value as? [String: Any] else { continue } + let command = serverConfig["command"] as? String ?? "" + let args = serverConfig["args"] as? [String] ?? [] + if command.contains("xcrun") && args.contains(where: { $0.contains("mcpbridge") }) { + names.insert(key) + } + } + return (!names.isEmpty, names) + } + + private func installXcodeMCPServer() { + isInstalling = true + installError = nil + + let configFileURL = URL(fileURLWithPath: mcpConfigFilePath) + let fileManager = FileManager.default + + do { + if !fileManager.fileExists(atPath: configDirectory.path) { + try fileManager.createDirectory( + at: configDirectory, + withIntermediateDirectories: true + ) + } + + var config: [String: Any] + if fileManager.fileExists(atPath: configFileURL.path), + let data = try? Data(contentsOf: configFileURL), + let existing = try? JSONSerialization.jsonObject(with: data) as? [String: Any] + { + config = existing + } else { + config = ["servers": [String: Any]()] + } + + var servers = config["servers"] as? [String: Any] ?? [:] + + // Skip write if the entry already points to xcrun mcpbridge + if let existing = servers[serverName] as? [String: Any], + let command = existing["command"] as? String, + let args = existing["args"] as? [String], + command.contains("xcrun") && args.contains(where: { $0.contains("mcpbridge") }) + { + isConfigured = true + configuredXcodeServerNames.insert(serverName) + isInstalling = false + return + } + + servers[serverName] = [ + "type": "stdio", + "command": "xcrun", + "args": ["mcpbridge"] + ] + + config["servers"] = servers + + let jsonData = try JSONSerialization.data( + withJSONObject: config, + options: [.prettyPrinted, .sortedKeys] + ) + try jsonData.write(to: configFileURL, options: .atomic) + + isConfigured = true + configuredXcodeServerNames.insert(serverName) + Logger.client.info("Successfully added Xcode MCP Server to configuration") + } catch { + installError = "Failed to update configuration: \(error.localizedDescription)" + Logger.client.error("Failed to install Xcode MCP Server: \(error)") + } + + isInstalling = false + } +} diff --git a/Core/Sources/KeyBindingManager/TabToAcceptSuggestion.swift b/Core/Sources/KeyBindingManager/TabToAcceptSuggestion.swift index 07568796..4ebfe1a2 100644 --- a/Core/Sources/KeyBindingManager/TabToAcceptSuggestion.swift +++ b/Core/Sources/KeyBindingManager/TabToAcceptSuggestion.swift @@ -262,7 +262,7 @@ extension TabToAcceptSuggestion { return (false, "No filespace", nil) } - var codeSuggestionType: CodeSuggestionType? = { + let codeSuggestionType: CodeSuggestionType? = { if let _ = filespace.presentingSuggestion { return .codeCompletion } if let _ = filespace.presentingNESSuggestion { return .nes } return nil diff --git a/Core/Sources/LaunchAgentManager/LaunchAgentManager.swift b/Core/Sources/LaunchAgentManager/LaunchAgentManager.swift index c311439d..1cee8bf2 100644 --- a/Core/Sources/LaunchAgentManager/LaunchAgentManager.swift +++ b/Core/Sources/LaunchAgentManager/LaunchAgentManager.swift @@ -24,17 +24,10 @@ public struct LaunchAgentManager { } public func setupLaunchAgentForTheFirstTimeIfNeeded() async throws { - if #available(macOS 13, *) { - await removeObsoleteLaunchAgent() - try await setupLaunchAgent() - } else { - guard !FileManager.default.fileExists(atPath: launchAgentPath) else { return } - try await setupLaunchAgent() - await removeObsoleteLaunchAgent() - } + await removeObsoleteLaunchAgent() + try await setupLaunchAgent() } - @available(macOS 13.0, *) public func isBackgroundPermissionGranted() async -> Bool { // On macOS 13+, check SMAppService status let bridgeLaunchAgent = SMAppService.agent(plistName: "bridgeLaunchAgent.plist") @@ -43,87 +36,41 @@ public struct LaunchAgentManager { } public func setupLaunchAgent() async throws { - if #available(macOS 13, *) { - Logger.client.info("Registering bridge launch agent") - let bridgeLaunchAgent = SMAppService.agent(plistName: "bridgeLaunchAgent.plist") - try bridgeLaunchAgent.register() - } else { - Logger.client.info("Creating and loading bridge launch agent") - let content = """ - - - - - Label - \(serviceIdentifier) - Program - \(executablePath) - MachServices - - \(serviceIdentifier) - - - AssociatedBundleIdentifiers - - \(bundleIdentifier) - \(serviceIdentifier) - - - - """ - if !FileManager.default.fileExists(atPath: launchAgentDirURL.path) { - try FileManager.default.createDirectory( - at: launchAgentDirURL, - withIntermediateDirectories: false - ) - } - FileManager.default.createFile( - atPath: launchAgentPath, - contents: content.data(using: .utf8) - ) - try await launchctl("load", launchAgentPath) - } + Logger.client.info("Registering bridge launch agent") + let bridgeLaunchAgent = SMAppService.agent(plistName: "bridgeLaunchAgent.plist") + try bridgeLaunchAgent.register() let buildNumber = Bundle.main.infoDictionary?["CFBundleVersion"] as? String UserDefaults.standard.set(buildNumber, forKey: lastLaunchAgentVersionKey) } public func removeLaunchAgent() async throws { - if #available(macOS 13, *) { - Logger.client.info("Unregistering bridge launch agent") - let bridgeLaunchAgent = SMAppService.agent(plistName: "bridgeLaunchAgent.plist") - try await bridgeLaunchAgent.unregister() - } else { - Logger.client.info("Unloading and removing bridge launch agent") - try await launchctl("unload", launchAgentPath) - try FileManager.default.removeItem(atPath: launchAgentPath) - } + Logger.client.info("Unregistering bridge launch agent") + let bridgeLaunchAgent = SMAppService.agent(plistName: "bridgeLaunchAgent.plist") + try await bridgeLaunchAgent.unregister() } public func reloadLaunchAgent() async throws { - if #unavailable(macOS 13) { - Logger.client.info("Reloading bridge launch agent") - try await helper("reload-launch-agent", "--service-identifier", serviceIdentifier) - } + // No-op: macOS 13+ uses SMAppService which doesn't need manual reload } public func removeObsoleteLaunchAgent() async { - if #available(macOS 13, *) { - let path = launchAgentPath - if FileManager.default.fileExists(atPath: path) { - Logger.client.info("Unloading and removing old bridge launch agent") - try? await launchctl("unload", path) - try? FileManager.default.removeItem(atPath: path) - } - } else { - let path = launchAgentPath.replacingOccurrences( - of: "ExtensionService", - with: "XPCService" - ) - if FileManager.default.fileExists(atPath: path) { - Logger.client.info("Removing old bridge launch agent plist") - try? FileManager.default.removeItem(atPath: path) - } + let path = launchAgentPath + if FileManager.default.fileExists(atPath: path) { + Logger.client.info("Unloading and removing old bridge launch agent") + try? await launchctl("unload", path) + try? FileManager.default.removeItem(atPath: path) + } + + // Also remove legacy plist that used "XPCService" instead of "ExtensionService" + let legacyIdentifier = serviceIdentifier + .replacingOccurrences(of: "ExtensionService", with: "XPCService") + let legacyPath = launchAgentDirURL + .appendingPathComponent("\(legacyIdentifier).plist").path + if FileManager.default.fileExists(atPath: legacyPath) { + Logger.client.info("Unloading and removing legacy XPCService launch agent") + try? await launchctl("unload", legacyPath) + try? FileManager.default.removeItem(atPath: legacyPath) } } } diff --git a/Core/Sources/PersistMiddleware/Extensions/ChatMessage+Storage.swift b/Core/Sources/PersistMiddleware/Extensions/ChatMessage+Storage.swift index d1837411..33590569 100644 --- a/Core/Sources/PersistMiddleware/Extensions/ChatMessage+Storage.swift +++ b/Core/Sources/PersistMiddleware/Extensions/ChatMessage+Storage.swift @@ -15,6 +15,7 @@ extension ChatMessage { var suggestedTitle: String? var errorMessages: [String] = [] var steps: [ConversationProgressStep] + var thinking: [MessageThinking] var editAgentRounds: [AgentRound] var parentTurnId: String? var panelMessages: [CopilotShowMessageParams] @@ -35,6 +36,14 @@ extension ChatMessage { suggestedTitle = try container.decodeIfPresent(String.self, forKey: .suggestedTitle) errorMessages = try container.decodeIfPresent([String].self, forKey: .errorMessages) ?? [] steps = try container.decodeIfPresent([ConversationProgressStep].self, forKey: .steps) ?? [] + // Decode thinking as either an array (current format) or a single value (legacy format). + if let array = try? container.decodeIfPresent([MessageThinking].self, forKey: .thinking) { + thinking = array + } else if let single = try? container.decodeIfPresent(MessageThinking.self, forKey: .thinking) { + thinking = [single] + } else { + thinking = [] + } editAgentRounds = try container.decodeIfPresent([AgentRound].self, forKey: .editAgentRounds) ?? [] parentTurnId = try container.decodeIfPresent(String.self, forKey: .parentTurnId) panelMessages = try container.decodeIfPresent([CopilotShowMessageParams].self, forKey: .panelMessages) ?? [] @@ -55,6 +64,7 @@ extension ChatMessage { suggestedTitle: String?, errorMessages: [String] = [], steps: [ConversationProgressStep]?, + thinking: [MessageThinking] = [], editAgentRounds: [AgentRound]? = nil, parentTurnId: String? = nil, panelMessages: [CopilotShowMessageParams]? = nil, @@ -72,6 +82,7 @@ extension ChatMessage { self.suggestedTitle = suggestedTitle self.errorMessages = errorMessages self.steps = steps ?? [] + self.thinking = thinking self.editAgentRounds = editAgentRounds ?? [] self.parentTurnId = parentTurnId self.panelMessages = panelMessages ?? [] @@ -93,6 +104,7 @@ extension ChatMessage { suggestedTitle: self.suggestedTitle, errorMessages: self.errorMessages, steps: self.steps, + thinking: self.thinking, editAgentRounds: self.editAgentRounds, parentTurnId: self.parentTurnId, panelMessages: self.panelMessages, @@ -133,6 +145,7 @@ extension ChatMessage { rating: turnItemData.rating, steps: turnItemData.steps, editAgentRounds: turnItemData.editAgentRounds, + thinking: turnItemData.thinking, parentTurnId: turnItemData.parentTurnId, panelMessages: turnItemData.panelMessages, fileEdits: turnItemData.fileEdits, diff --git a/Core/Sources/Service/RealtimeSuggestionController.swift b/Core/Sources/Service/RealtimeSuggestionController.swift index b3fd109a..d583d792 100644 --- a/Core/Sources/Service/RealtimeSuggestionController.swift +++ b/Core/Sources/Service/RealtimeSuggestionController.swift @@ -88,16 +88,9 @@ public actor RealtimeSuggestionController { ) } - if #available(macOS 13.0, *) { - for await _ in selectedTextChanged._throttle(for: .milliseconds(200)) { - if Task.isCancelled { return } - await handler() - } - } else { - for await _ in selectedTextChanged { - if Task.isCancelled { return } - await handler() - } + for await _ in selectedTextChanged._throttle(for: .milliseconds(200)) { + if Task.isCancelled { return } + await handler() } } diff --git a/Core/Sources/SuggestionWidget/AgentConfigurationWidgetView.swift b/Core/Sources/SuggestionWidget/AgentConfigurationWidgetView.swift index fd423990..c6275778 100644 --- a/Core/Sources/SuggestionWidget/AgentConfigurationWidgetView.swift +++ b/Core/Sources/SuggestionWidget/AgentConfigurationWidgetView.swift @@ -239,7 +239,7 @@ struct AgentConfigurationWidgetView: View { } private func parseYAMLFrontmatter(content: String) -> YAMLFrontmatterInfo { - var lines = content.components(separatedBy: .newlines) + let lines = content.components(separatedBy: .newlines) var inFrontmatter = false var frontmatterEndIndex: Int? var modelLineIndex: Int? @@ -1063,7 +1063,8 @@ struct AgentConfigurationWidgetView: View { Text(createModelMenuItemAttributedString( modelName: model.displayName ?? model.modelName, isSelected: isModelSelected(model), - multiplierText: modelCache[model.modelName] ?? "Variable" + multiplierText: modelCache[model.modelName] ?? "Variable", + isDegraded: model.degradationReason != nil )) } @@ -1078,7 +1079,8 @@ struct AgentConfigurationWidgetView: View { Text(createModelMenuItemAttributedString( modelName: model.displayName ?? model.modelName, isSelected: isModelSelected(model), - multiplierText: modelCache[model.modelName] ?? "" + multiplierText: modelCache[model.modelName] ?? "", + isDegraded: model.degradationReason != nil )) } } @@ -1173,13 +1175,15 @@ struct AgentConfigurationWidgetView: View { private func createModelMenuItemAttributedString( modelName: String, isSelected: Bool, - multiplierText: String + multiplierText: String, + isDegraded: Bool = false ) -> AttributedString { return ModelMenuItemFormatter.createModelMenuItemAttributedString( modelName: modelName, isSelected: isSelected, multiplierText: multiplierText, targetWidth: targetMenuItemWidth, + isDegraded: isDegraded ) } } diff --git a/Core/Sources/SuggestionWidget/ChatWindowView.swift b/Core/Sources/SuggestionWidget/ChatWindowView.swift index 665ccd70..0e1fc9e4 100644 --- a/Core/Sources/SuggestionWidget/ChatWindowView.swift +++ b/Core/Sources/SuggestionWidget/ChatWindowView.swift @@ -232,11 +232,7 @@ struct ChatTitleBar: View { private extension View { func hideScrollIndicator() -> some View { - if #available(macOS 13.0, *) { - return scrollIndicators(.hidden) - } else { - return self - } + scrollIndicators(.hidden) } } diff --git a/Core/Sources/SuggestionWidget/Extensions/WidgetWindowsController+AgentConfigurationWidget.swift b/Core/Sources/SuggestionWidget/Extensions/WidgetWindowsController+AgentConfigurationWidget.swift index 6493f842..d68f297c 100644 --- a/Core/Sources/SuggestionWidget/Extensions/WidgetWindowsController+AgentConfigurationWidget.swift +++ b/Core/Sources/SuggestionWidget/Extensions/WidgetWindowsController+AgentConfigurationWidget.swift @@ -23,7 +23,7 @@ extension WidgetWindowsController { let state = store.withState { $0.panelState.agentConfigurationWidgetState } guard let noFocus = noFocus, !noFocus, - let focusedEditor = state.focusedEditor + state.focusedEditor != nil else { hideAgentConfigurationWidgetWindow() return diff --git a/Core/Sources/SuggestionWidget/FeatureReducers/CodeReviewFeature.swift b/Core/Sources/SuggestionWidget/FeatureReducers/CodeReviewFeature.swift index ed7b4375..56ed2632 100644 --- a/Core/Sources/SuggestionWidget/FeatureReducers/CodeReviewFeature.swift +++ b/Core/Sources/SuggestionWidget/FeatureReducers/CodeReviewFeature.swift @@ -135,7 +135,7 @@ public struct CodeReviewPanelFeature { return .none - case let .close(id): + case .close(_): state.isPanelDisplayed = false state.closedByUser = true diff --git a/Core/Sources/SuggestionWidget/NES/NESMenu/NESMenuController.swift b/Core/Sources/SuggestionWidget/NES/NESMenu/NESMenuController.swift index 071b1dd2..5ce61b0e 100644 --- a/Core/Sources/SuggestionWidget/NES/NESMenu/NESMenuController.swift +++ b/Core/Sources/SuggestionWidget/NES/NESMenu/NESMenuController.swift @@ -48,7 +48,6 @@ class NESMenuController: ObservableObject { let settingsItem = createSettingItem() let goToAcceptItem = createGoToAcceptItem() let rejectItem = createRejectItem() - let moreInfoItem = createGetMoreInfoItem() menu.addItem(titleItem) menu.addItem(NSMenuItem.separator()) @@ -56,8 +55,6 @@ class NESMenuController: ObservableObject { menu.addItem(NSMenuItem.separator()) menu.addItem(goToAcceptItem) menu.addItem(rejectItem) -// menu.addItem(NSMenuItem.separator()) -// menu.addItem(moreInfoItem) self.menu = menu return menu diff --git a/Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift b/Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift index 682d9c79..9462717f 100644 --- a/Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift +++ b/Core/Sources/SuggestionWidget/SuggestionPanelContent/PromptToCodePanel.swift @@ -332,13 +332,7 @@ extension PromptToCodePanel { codeForegroundColor: codeForegroundColor ) } - .modify { - if #available(macOS 13.0, *) { - $0.scrollIndicators(.hidden) - } else { - $0 - } - } + .scrollIndicators(.hidden) } } } diff --git a/Core/Sources/SuggestionWidget/WidgetWindowsController.swift b/Core/Sources/SuggestionWidget/WidgetWindowsController.swift index e790da75..e3d19b8a 100644 --- a/Core/Sources/SuggestionWidget/WidgetWindowsController.swift +++ b/Core/Sources/SuggestionWidget/WidgetWindowsController.swift @@ -240,42 +240,24 @@ private extension WidgetWindowsController { let valueChange = await editor.axNotifications.notifications() .filter { $0.kind == .valueChanged } - if #available(macOS 13.0, *) { - for await notification in merge( - scroll, - selectionRangeChange.debounce(for: Duration.milliseconds(0)), - valueChange.debounce(for: Duration.milliseconds(100)) - ) { - guard await xcodeInspector.safe.latestActiveXcode != nil else { return } - try Task.checkCancellation() - - // for better looking - if notification.kind == .scrollPositionChanged { - await hideSuggestionPanelWindow() - } + for await notification in merge( + scroll, + selectionRangeChange.debounce(for: Duration.milliseconds(0)), + valueChange.debounce(for: Duration.milliseconds(100)) + ) { + guard await xcodeInspector.safe.latestActiveXcode != nil else { return } + try Task.checkCancellation() - updateWindowLocation(animated: false, immediately: false) - updateWindowOpacity(immediately: false) - await updateCodeReviewWindowLocation(.onSourceEditorNotification(notification)) - - await handleFixErrorEditorNotification(notification: notification) + // for better looking + if notification.kind == .scrollPositionChanged { + await hideSuggestionPanelWindow() } - } else { - for await notification in merge(selectionRangeChange, scroll, valueChange) { - guard await xcodeInspector.safe.latestActiveXcode != nil else { return } - try Task.checkCancellation() - // for better looking - if notification.kind == .scrollPositionChanged { - await hideSuggestionPanelWindow() - } + updateWindowLocation(animated: false, immediately: false) + updateWindowOpacity(immediately: false) + await updateCodeReviewWindowLocation(.onSourceEditorNotification(notification)) - updateWindowLocation(animated: false, immediately: false) - updateWindowOpacity(immediately: false) - await updateCodeReviewWindowLocation(.onSourceEditorNotification(notification)) - - await handleFixErrorEditorNotification(notification: notification) - } + await handleFixErrorEditorNotification(notification: notification) } } } @@ -862,8 +844,8 @@ extension WidgetWindowsController { guard state.isPanelDisplayed, let comment = state.currentSelectedComment, await currentXcodeApp?.realtimeDocumentURL?.absoluteString == comment.uri, - let reviewWindowFittingSize = windows.codeReviewPanelWindow.contentView?.fittingSize - else { + windows.codeReviewPanelWindow.contentView?.fittingSize != nil + else { hideCodeReviewWindow() return } @@ -871,7 +853,7 @@ extension WidgetWindowsController { guard let originalContent = state.originalContent, let screen = NSScreen.screens.first(where: { $0.frame.origin == .zero }), let scrollViewRect = sourceEditorElement.parent?.rect, - let scrollScreenFrame = sourceEditorElement.parent?.maxIntersectionScreen?.frame, + sourceEditorElement.parent?.maxIntersectionScreen?.frame != nil, let currentContent: String = try? sourceEditorElement.copyValue(key: kAXValueAttribute) else { return } diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/Contents.json b/ExtensionService/Assets.xcassets/AppIcon.appiconset/Contents.json deleted file mode 100644 index 0a30d46d..00000000 --- a/ExtensionService/Assets.xcassets/AppIcon.appiconset/Contents.json +++ /dev/null @@ -1,68 +0,0 @@ -{ - "images" : [ - { - "filename" : "CopilotforXcode-Icon@16w_1x.png", - "idiom" : "mac", - "scale" : "1x", - "size" : "16x16" - }, - { - "filename" : "CopilotforXcode-Icon@16w_2x.png", - "idiom" : "mac", - "scale" : "2x", - "size" : "16x16" - }, - { - "filename" : "CopilotforXcode-Icon@32w_1x.png", - "idiom" : "mac", - "scale" : "1x", - "size" : "32x32" - }, - { - "filename" : "CopilotforXcode-Icon@32w_2x.png", - "idiom" : "mac", - "scale" : "2x", - "size" : "32x32" - }, - { - "filename" : "CopilotforXcode-Icon@128w_1x.png", - "idiom" : "mac", - "scale" : "1x", - "size" : "128x128" - }, - { - "filename" : "CopilotforXcode-Icon@128w_2x.png", - "idiom" : "mac", - "scale" : "2x", - "size" : "128x128" - }, - { - "filename" : "CopilotforXcode-Icon@256w_1x.png", - "idiom" : "mac", - "scale" : "1x", - "size" : "256x256" - }, - { - "filename" : "CopilotforXcode-Icon@256w_2x.png", - "idiom" : "mac", - "scale" : "2x", - "size" : "256x256" - }, - { - "filename" : "CopilotforXcode-Icon@512w_1x.png", - "idiom" : "mac", - "scale" : "1x", - "size" : "512x512" - }, - { - "filename" : "CopilotforXcode-Icon@512w_2x.png", - "idiom" : "mac", - "scale" : "2x", - "size" : "512x512" - } - ], - "info" : { - "author" : "xcode", - "version" : 1 - } -} diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@128w_1x.png b/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@128w_1x.png deleted file mode 100644 index 3ee52427..00000000 Binary files a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@128w_1x.png and /dev/null differ diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@128w_2x.png b/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@128w_2x.png deleted file mode 100644 index 88b20d1d..00000000 Binary files a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@128w_2x.png and /dev/null differ diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@16w_1x.png b/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@16w_1x.png deleted file mode 100644 index 2bb554dc..00000000 Binary files a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@16w_1x.png and /dev/null differ diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@16w_2x.png b/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@16w_2x.png deleted file mode 100644 index ce02bac7..00000000 Binary files a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@16w_2x.png and /dev/null differ diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@256w_1x.png b/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@256w_1x.png deleted file mode 100644 index 7674f663..00000000 Binary files a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@256w_1x.png and /dev/null differ diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@256w_2x.png b/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@256w_2x.png deleted file mode 100644 index fc705969..00000000 Binary files a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@256w_2x.png and /dev/null differ diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@32w_1x.png b/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@32w_1x.png deleted file mode 100644 index ce02bac7..00000000 Binary files a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@32w_1x.png and /dev/null differ diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@32w_2x.png b/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@32w_2x.png deleted file mode 100644 index 4d52c81b..00000000 Binary files a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@32w_2x.png and /dev/null differ diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@512w_1x.png b/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@512w_1x.png deleted file mode 100644 index fc705969..00000000 Binary files a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@512w_1x.png and /dev/null differ diff --git a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@512w_2x.png b/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@512w_2x.png deleted file mode 100644 index 54da6e3f..00000000 Binary files a/ExtensionService/Assets.xcassets/AppIcon.appiconset/CopilotforXcode-Icon@512w_2x.png and /dev/null differ diff --git a/ExtensionService/Assets.xcassets/BannerBackgroundColor.colorset/Contents.json b/ExtensionService/Assets.xcassets/BannerBackgroundColor.colorset/Contents.json new file mode 100644 index 00000000..ee2a2c8e --- /dev/null +++ b/ExtensionService/Assets.xcassets/BannerBackgroundColor.colorset/Contents.json @@ -0,0 +1,38 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "display-p3", + "components" : { + "alpha" : "1.000", + "blue" : "0xFE", + "green" : "0xF8", + "red" : "0xF5" + } + }, + "idiom" : "universal" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "color" : { + "color-space" : "display-p3", + "components" : { + "alpha" : "1.000", + "blue" : "0x4D", + "green" : "0x32", + "red" : "0x25" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/ExtensionService/Assets.xcassets/BannerBorderColor.colorset/Contents.json b/ExtensionService/Assets.xcassets/BannerBorderColor.colorset/Contents.json new file mode 100644 index 00000000..fa914237 --- /dev/null +++ b/ExtensionService/Assets.xcassets/BannerBorderColor.colorset/Contents.json @@ -0,0 +1,38 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "display-p3", + "components" : { + "alpha" : "1.000", + "blue" : "0xFC", + "green" : "0xD6", + "red" : "0xC2" + } + }, + "idiom" : "universal" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "color" : { + "color-space" : "display-p3", + "components" : { + "alpha" : "1.000", + "blue" : "0x8F", + "green" : "0x53", + "red" : "0x35" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/ExtensionService/Assets.xcassets/CopilotLogo.imageset/Contents.json b/ExtensionService/Assets.xcassets/CopilotLogo.imageset/Contents.json deleted file mode 100644 index 2e35661e..00000000 --- a/ExtensionService/Assets.xcassets/CopilotLogo.imageset/Contents.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "images" : [ - { - "filename" : "copilot.svg", - "idiom" : "universal" - } - ], - "info" : { - "author" : "xcode", - "version" : 1 - }, - "properties" : { - "preserves-vector-representation" : true - } -} diff --git a/ExtensionService/Assets.xcassets/CopilotLogo.imageset/copilot.svg b/ExtensionService/Assets.xcassets/CopilotLogo.imageset/copilot.svg deleted file mode 100644 index 8284dce7..00000000 --- a/ExtensionService/Assets.xcassets/CopilotLogo.imageset/copilot.svg +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - - - - - - - diff --git a/ExtensionService/Assets.xcassets/WarningYellow.colorset/Contents.json b/ExtensionService/Assets.xcassets/WarningYellow.colorset/Contents.json new file mode 100644 index 00000000..fb5231df --- /dev/null +++ b/ExtensionService/Assets.xcassets/WarningYellow.colorset/Contents.json @@ -0,0 +1,38 @@ +{ + "colors" : [ + { + "color" : { + "color-space" : "display-p3", + "components" : { + "alpha" : "1.000", + "blue" : "0x04", + "green" : "0x7D", + "red" : "0xC2" + } + }, + "idiom" : "universal" + }, + { + "appearances" : [ + { + "appearance" : "luminosity", + "value" : "dark" + } + ], + "color" : { + "color-space" : "display-p3", + "components" : { + "alpha" : "1.000", + "blue" : "0x5C", + "green" : "0xC5", + "red" : "0xF2" + } + }, + "idiom" : "universal" + } + ], + "info" : { + "author" : "xcode", + "version" : 1 + } +} diff --git a/ExtensionService/XPCController.swift b/ExtensionService/XPCController.swift index 02656f85..d47ea0ef 100644 --- a/ExtensionService/XPCController.swift +++ b/ExtensionService/XPCController.swift @@ -59,7 +59,7 @@ final class XPCController: XPCServiceDelegate { // No log, but you should run CommunicationBridge, too. #else if consecutiveFailures == 5 { - if #available(macOS 13.0, *) { + await MainActor.run { showBackgroundPermissionAlert() } } diff --git a/README.md b/README.md index eea0b39a..5cfeeabf 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ You can receive auto-complete type suggestions from GitHub Copilot either by sta ## Requirements -- macOS 12+ -- Xcode 8+ +- macOS 13+ +- Xcode 14+ - A GitHub account ## Getting Started diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 54e04ee5..787aab78 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -1,18 +1,12 @@ -### GitHub Copilot for Xcode 0.47.0 +### GitHub Copilot for Xcode 0.49.0 **🚀 Highlights** -- **Toolcall Auto Approval**: Streamlined workflow with auto-approval support for MCP tools, sensitive files, and terminal commands. -- **MCP Registry**: The MCP registry and allowlist features are now available (requires editor preview feature flag). +- **Native Anthropic Messages API**: Adds support for the native Anthropic Messages API (`/v1/messages`) endpoint, enabling direct integration with Anthropic-compatible providers. +- **Thinking in Chat**: Reasoning-capable models can now show their thinking process in chat for greater transparency. +- **Better Rate Limit Feedback**: Enhanced notifications and error messages help you understand and respond to rate limit events. -**💪 Improvements** +**💪 Changes** -- Refined the working set header. -- Improved the details view for MCP tool calls. - -**🛠️ Bug Fixes** - -- Fixed layout issues with tool calls. -- Resolved display issues for Next Edit Suggestions (NES). -- Improved error messaging for SSL certificate failures. -- Addressed various performance issues. +- Refined the tool call item UI in agent progress for a cleaner, more readable layout. +- Updated Copilot language server to 1.465.5. diff --git a/Server/package-lock.json b/Server/package-lock.json index 4ff704de..72d6707e 100644 --- a/Server/package-lock.json +++ b/Server/package-lock.json @@ -8,19 +8,19 @@ "name": "@github/copilot-xcode", "version": "0.0.1", "dependencies": { - "@github/copilot-language-server": "1.411.0", - "@github/copilot-language-server-darwin-arm64": "1.411.0", - "@github/copilot-language-server-darwin-x64": "1.411.0", + "@github/copilot-language-server": "1.465.5", + "@github/copilot-language-server-darwin-arm64": "1.465.5", + "@github/copilot-language-server-darwin-x64": "1.465.5", "@xterm/addon-fit": "^0.10.0", "@xterm/xterm": "^5.5.0", "monaco-editor": "0.52.2" }, "devDependencies": { "@types/node": "^22.15.17", - "copy-webpack-plugin": "^13.0.1", + "copy-webpack-plugin": "^14.0.0", "css-loader": "^7.1.2", "style-loader": "^4.0.0", - "terser-webpack-plugin": "^5.3.14", + "terser-webpack-plugin": "^5.4.0", "ts-loader": "^9.5.4", "typescript": "^5.8.3", "webpack": "^5.99.9", @@ -38,9 +38,9 @@ } }, "node_modules/@github/copilot-language-server": { - "version": "1.411.0", - "resolved": "https://registry.npmjs.org/@github/copilot-language-server/-/copilot-language-server-1.411.0.tgz", - "integrity": "sha512-KxuvWq3DT4qTujxtgDQTHmynWawDiwqsRC9BmuBVi5PyzdyejJEj6rgcuwH7WcOMgNQJlHVQmSxN6uYurqp26w==", + "version": "1.465.5", + "resolved": "https://registry.npmjs.org/@github/copilot-language-server/-/copilot-language-server-1.465.5.tgz", + "integrity": "sha512-dT0/MWx9wfImhQzNgVMIwWW81MkJBd0HtctYtmg2/nITyDsaSSjQLkYryzcsMMBx98JYwnyldQQc1QFx7q6mJw==", "license": "MIT", "dependencies": { "vscode-languageserver-protocol": "^3.17.5" @@ -49,17 +49,18 @@ "copilot-language-server": "dist/language-server.js" }, "optionalDependencies": { - "@github/copilot-language-server-darwin-arm64": "1.411.0", - "@github/copilot-language-server-darwin-x64": "1.411.0", - "@github/copilot-language-server-linux-arm64": "1.411.0", - "@github/copilot-language-server-linux-x64": "1.411.0", - "@github/copilot-language-server-win32-x64": "1.411.0" + "@github/copilot-language-server-darwin-arm64": "1.465.5", + "@github/copilot-language-server-darwin-x64": "1.465.5", + "@github/copilot-language-server-linux-arm64": "1.465.5", + "@github/copilot-language-server-linux-x64": "1.465.5", + "@github/copilot-language-server-win32-arm64": "1.465.5", + "@github/copilot-language-server-win32-x64": "1.465.5" } }, "node_modules/@github/copilot-language-server-darwin-arm64": { - "version": "1.411.0", - "resolved": "https://registry.npmjs.org/@github/copilot-language-server-darwin-arm64/-/copilot-language-server-darwin-arm64-1.411.0.tgz", - "integrity": "sha512-fTRodMIdHRgsLDhfhlpOT6OvyR3rLD4JwkbjlRCa+KDHAQd/kFN8+G5KnzqMckIFtGAvQ1zY7d8oKiT7Z11ayg==", + "version": "1.465.5", + "resolved": "https://registry.npmjs.org/@github/copilot-language-server-darwin-arm64/-/copilot-language-server-darwin-arm64-1.465.5.tgz", + "integrity": "sha512-yi4QvjsLDw4NiKnnJghYuGD00veI+4+6mtp6S0E3OWodWk3jdTPmmSOrgp049f2Dok5fb+Xg28Zau5XZy3FU/A==", "cpu": [ "arm64" ], @@ -69,9 +70,9 @@ ] }, "node_modules/@github/copilot-language-server-darwin-x64": { - "version": "1.411.0", - "resolved": "https://registry.npmjs.org/@github/copilot-language-server-darwin-x64/-/copilot-language-server-darwin-x64-1.411.0.tgz", - "integrity": "sha512-CA9l1MvMmfgDgaKmzP4inEx6P8sG1x+pF12HY9nwwH01XmeJre+obQM8M3Nm5BUIklmpS07Vk5fbu9X3fOpWkg==", + "version": "1.465.5", + "resolved": "https://registry.npmjs.org/@github/copilot-language-server-darwin-x64/-/copilot-language-server-darwin-x64-1.465.5.tgz", + "integrity": "sha512-SsYnfyTb7/8INZfxGnfVsOTVuzaWeTz02HMxzKPdk/VAdDF5bTlX5Fy2wW3vTJuGSAWDDz0yCTj9pEsP3hU4CQ==", "cpu": [ "x64" ], @@ -81,9 +82,9 @@ ] }, "node_modules/@github/copilot-language-server-linux-arm64": { - "version": "1.411.0", - "resolved": "https://registry.npmjs.org/@github/copilot-language-server-linux-arm64/-/copilot-language-server-linux-arm64-1.411.0.tgz", - "integrity": "sha512-6T0SVveZlfVTcUS98vqTPhJSFA3Ia3FCPubOeYHF3SqHdLTokJtKrYGzD6gux+0ik1/9pPmx4bj8cMfHkhp1SA==", + "version": "1.465.5", + "resolved": "https://registry.npmjs.org/@github/copilot-language-server-linux-arm64/-/copilot-language-server-linux-arm64-1.465.5.tgz", + "integrity": "sha512-qTVHFJrrlqm5dDUDanaot1KFh4aI0bRxrhYRss+/0aXPV+FhlfurxlQ9y44bWo3Bj3mBMs2Xn6c5F1BxcAFeXw==", "cpu": [ "arm64" ], @@ -94,9 +95,9 @@ ] }, "node_modules/@github/copilot-language-server-linux-x64": { - "version": "1.411.0", - "resolved": "https://registry.npmjs.org/@github/copilot-language-server-linux-x64/-/copilot-language-server-linux-x64-1.411.0.tgz", - "integrity": "sha512-OKKZqCH2x7OL71pzDQQP4lZfsVnqiOlpcnz9UXoP4QFnkGunx5PhAmbYvZwTQiCNAwippkSaUjDnj9YneNkytw==", + "version": "1.465.5", + "resolved": "https://registry.npmjs.org/@github/copilot-language-server-linux-x64/-/copilot-language-server-linux-x64-1.465.5.tgz", + "integrity": "sha512-aAJB5uYLwOdvbXSKLkroDFLaL3WBE/zwR2GVLdwL3rvW7/oR/u5GkzfLzYDSH8+8d4qJpNQWQN+71EbomHd24w==", "cpu": [ "x64" ], @@ -106,10 +107,23 @@ "linux" ] }, + "node_modules/@github/copilot-language-server-win32-arm64": { + "version": "1.465.5", + "resolved": "https://registry.npmjs.org/@github/copilot-language-server-win32-arm64/-/copilot-language-server-win32-arm64-1.465.5.tgz", + "integrity": "sha512-jLPMguKEWg8qFB+6acEGyBnPNOza5m6Pws4OZnuFmj4aazrt6Bw40M8KhcQ9XCdp8phc4xt67SNOrCrITkOcIw==", + "cpu": [ + "arm64" + ], + "license": "MIT", + "optional": true, + "os": [ + "win32" + ] + }, "node_modules/@github/copilot-language-server-win32-x64": { - "version": "1.411.0", - "resolved": "https://registry.npmjs.org/@github/copilot-language-server-win32-x64/-/copilot-language-server-win32-x64-1.411.0.tgz", - "integrity": "sha512-kJK/qoiMeydpy1K/uBgVwTqXjeLZmkMwJupbvZFXWjYFbHU2iCe6fHiUsROYPoVbqX52tjX5C+Rw/plhARDuRA==", + "version": "1.465.5", + "resolved": "https://registry.npmjs.org/@github/copilot-language-server-win32-x64/-/copilot-language-server-win32-x64-1.465.5.tgz", + "integrity": "sha512-kVBnzO24HAaQZflC/lGG9bWJ3c4aJPLQFBHQUkXPWE+s0SjPX4T+cdiH2ZAPGn90m2PxhJ9EsknHKIJHALFOrA==", "cpu": [ "x64" ], @@ -707,20 +721,20 @@ "license": "MIT" }, "node_modules/copy-webpack-plugin": { - "version": "13.0.1", - "resolved": "https://registry.npmjs.org/copy-webpack-plugin/-/copy-webpack-plugin-13.0.1.tgz", - "integrity": "sha512-J+YV3WfhY6W/Xf9h+J1znYuqTye2xkBUIGyTPWuBAT27qajBa5mR4f8WBmfDY3YjRftT2kqZZiLi1qf0H+UOFw==", + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/copy-webpack-plugin/-/copy-webpack-plugin-14.0.0.tgz", + "integrity": "sha512-3JLW90aBGeaTLpM7mYQKpnVdgsUZRExY55giiZgLuX/xTQRUs1dOCwbBnWnvY6Q6rfZoXMNwzOQJCSZPppfqXA==", "dev": true, "license": "MIT", "dependencies": { "glob-parent": "^6.0.1", "normalize-path": "^3.0.0", "schema-utils": "^4.2.0", - "serialize-javascript": "^6.0.2", + "serialize-javascript": "^7.0.3", "tinyglobby": "^0.2.12" }, "engines": { - "node": ">= 18.12.0" + "node": ">= 20.9.0" }, "funding": { "type": "opencollective", @@ -1550,16 +1564,6 @@ "dev": true, "license": "MIT" }, - "node_modules/randombytes": { - "version": "2.1.0", - "resolved": "https://registry.npmjs.org/randombytes/-/randombytes-2.1.0.tgz", - "integrity": "sha512-vYl3iOX+4CKUWuxGi9Ukhie6fsqXqS9FE2Zaic4tNFD2N2QQaXOMFbuKK4QmDHC0JO6B1Zp41J0LpT0oR68amQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "safe-buffer": "^5.1.0" - } - }, "node_modules/rechoir": { "version": "0.8.0", "resolved": "https://registry.npmjs.org/rechoir/-/rechoir-0.8.0.tgz", @@ -1627,27 +1631,6 @@ "node": ">=8" } }, - "node_modules/safe-buffer": { - "version": "5.2.1", - "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", - "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", - "dev": true, - "funding": [ - { - "type": "github", - "url": "https://github.com/sponsors/feross" - }, - { - "type": "patreon", - "url": "https://www.patreon.com/feross" - }, - { - "type": "consulting", - "url": "https://feross.org/support" - } - ], - "license": "MIT" - }, "node_modules/schema-utils": { "version": "4.3.2", "resolved": "https://registry.npmjs.org/schema-utils/-/schema-utils-4.3.2.tgz", @@ -1682,13 +1665,13 @@ } }, "node_modules/serialize-javascript": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-6.0.2.tgz", - "integrity": "sha512-Saa1xPByTTq2gdeFZYLLo+RFE35NHZkAbqZeWNd3BpzppeVisAqpDjcp8dyf6uIvEqJRd46jemmyA4iFIeVk8g==", + "version": "7.0.5", + "resolved": "https://registry.npmjs.org/serialize-javascript/-/serialize-javascript-7.0.5.tgz", + "integrity": "sha512-F4LcB0UqUl1zErq+1nYEEzSHJnIwb3AF2XWB94b+afhrekOUijwooAYqFyRbjYkm2PAKBabx6oYv/xDxNi8IBw==", "dev": true, "license": "BSD-3-Clause", - "dependencies": { - "randombytes": "^2.1.0" + "engines": { + "node": ">=20.0.0" } }, "node_modules/shallow-clone": { @@ -1834,16 +1817,15 @@ } }, "node_modules/terser-webpack-plugin": { - "version": "5.3.14", - "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-5.3.14.tgz", - "integrity": "sha512-vkZjpUjb6OMS7dhV+tILUW6BhpDR7P2L/aQSAv+Uwk+m8KATX9EccViHTJR2qDtACKPIYndLGCyl3FMo+r2LMw==", + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-5.4.0.tgz", + "integrity": "sha512-Bn5vxm48flOIfkdl5CaD2+1CiUVbonWQ3KQPyP7/EuIl9Gbzq/gQFOzaMFUEgVjB1396tcK0SG8XcNJ/2kDH8g==", "dev": true, "license": "MIT", "dependencies": { "@jridgewell/trace-mapping": "^0.3.25", "jest-worker": "^27.4.5", "schema-utils": "^4.3.0", - "serialize-javascript": "^6.0.2", "terser": "^5.31.1" }, "engines": { diff --git a/Server/package.json b/Server/package.json index 62a20e1d..03e3d6e9 100644 --- a/Server/package.json +++ b/Server/package.json @@ -7,19 +7,19 @@ "build": "webpack" }, "dependencies": { - "@github/copilot-language-server": "1.411.0", - "@github/copilot-language-server-darwin-arm64": "1.411.0", - "@github/copilot-language-server-darwin-x64": "1.411.0", + "@github/copilot-language-server": "1.465.5", + "@github/copilot-language-server-darwin-arm64": "1.465.5", + "@github/copilot-language-server-darwin-x64": "1.465.5", "@xterm/addon-fit": "^0.10.0", "@xterm/xterm": "^5.5.0", "monaco-editor": "0.52.2" }, "devDependencies": { "@types/node": "^22.15.17", - "copy-webpack-plugin": "^13.0.1", + "copy-webpack-plugin": "^14.0.0", "css-loader": "^7.1.2", "style-loader": "^4.0.0", - "terser-webpack-plugin": "^5.3.14", + "terser-webpack-plugin": "^5.4.0", "ts-loader": "^9.5.4", "typescript": "^5.8.3", "webpack": "^5.99.9", diff --git a/Tool/Package.swift b/Tool/Package.swift index b54bd789..cf45e947 100644 --- a/Tool/Package.swift +++ b/Tool/Package.swift @@ -5,7 +5,7 @@ import PackageDescription let package = Package( name: "Tool", - platforms: [.macOS(.v12)], + platforms: [.macOS(.v13)], products: [ .library(name: "XPCShared", targets: ["XPCShared"]), .library(name: "Terminal", targets: ["Terminal"]), @@ -66,7 +66,8 @@ let package = Package( .library(name: "StatusBarItemView", targets: ["StatusBarItemView"]), .library(name: "HostAppActivator", targets: ["HostAppActivator"]), .library(name: "AppKitExtension", targets: ["AppKitExtension"]), - .library(name: "GitHelper", targets: ["GitHelper"]) + .library(name: "GitHelper", targets: ["GitHelper"]), + .library(name: "NotificationCenterCoordinator", targets: ["NotificationCenterCoordinator"]) ], dependencies: [ // TODO: Update LanguageClient some day. @@ -189,6 +190,7 @@ let package = Package( dependencies: [ "SuggestionBasic", "SuggestionProvider", + "TelemetryServiceProvider", "Workspace", .product(name: "CopilotForXcodeKit", package: "CopilotForXcodeKit"), ] @@ -266,7 +268,8 @@ let package = Package( dependencies: [ "Logger", "Status", - .product(name: "SQLite", package: "SQLite.Swift") + .product(name: "SQLite", package: "SQLite.Swift"), + .product(name: "JSONRPC", package: "JSONRPC") ] ), @@ -304,6 +307,8 @@ let package = Package( // MARK: - GitHub Copilot + .target(name: "NotificationCenterCoordinator"), + .target( name: "GitHubCopilotService", dependencies: [ @@ -320,6 +325,7 @@ let package = Package( "Workspace", "Persist", "SuggestionProvider", + "NotificationCenterCoordinator", .product(name: "LanguageServerProtocol", package: "LanguageServerProtocol"), .product(name: "CopilotForXcodeKit", package: "CopilotForXcodeKit"), ] diff --git a/Tool/Sources/AXExtension/AXUIElement.swift b/Tool/Sources/AXExtension/AXUIElement.swift index 677a8264..6d0a2584 100644 --- a/Tool/Sources/AXExtension/AXUIElement.swift +++ b/Tool/Sources/AXExtension/AXUIElement.swift @@ -373,6 +373,8 @@ public extension AXUIElement { } #if hasFeature(RetroactiveAttribute) +extension AXError: @retroactive _BridgedNSError {} +extension AXError: @retroactive _ObjectiveCBridgeableError {} extension AXError: @retroactive Error {} #else extension AXError: Error {} diff --git a/Tool/Sources/BuiltinExtension/BuiltinExtensionConversationServiceProvider.swift b/Tool/Sources/BuiltinExtension/BuiltinExtensionConversationServiceProvider.swift index 14625052..fbf5852b 100644 --- a/Tool/Sources/BuiltinExtension/BuiltinExtensionConversationServiceProvider.swift +++ b/Tool/Sources/BuiltinExtension/BuiltinExtensionConversationServiceProvider.swift @@ -211,7 +211,19 @@ public final class BuiltinExtensionConversationServiceProvider< Logger.service.error("Could not get active workspace info") return nil } - + return (try? await conversationService.reviewChanges(workspace: workspaceInfo, changes: changes)) } + + public func generateThinkingTitle(_ params: GenerateThinkingTitleParams) async throws -> GenerateThinkingTitleResponse? { + guard let conversationService else { + Logger.service.error("Builtin chat service not found.") + return nil + } + guard let workspaceInfo = await activeWorkspace() else { + Logger.service.error("Could not get active workspace info") + return nil + } + return try? await conversationService.generateThinkingTitle(workspace: workspaceInfo, params: params) + } } diff --git a/Tool/Sources/ChatAPIService/Memory/ChatMemory.swift b/Tool/Sources/ChatAPIService/Memory/ChatMemory.swift index d988a91e..bdfe7672 100644 --- a/Tool/Sources/ChatAPIService/Memory/ChatMemory.swift +++ b/Tool/Sources/ChatAPIService/Memory/ChatMemory.swift @@ -22,14 +22,14 @@ public extension ChatMemory { if !message.editAgentRounds.isEmpty { var parentRounds = parentMessage.editAgentRounds - + if let lastParentRoundIndex = parentRounds.indices.last { var existingSubRounds = parentRounds[lastParentRoundIndex].subAgentRounds ?? [] - + for messageRound in message.editAgentRounds { if let subIndex = existingSubRounds.firstIndex(where: { $0.roundId == messageRound.roundId }) { existingSubRounds[subIndex].reply = existingSubRounds[subIndex].reply + messageRound.reply - + mergeThinking(into: &existingSubRounds[subIndex].thinking, from: messageRound.thinking) if let messageToolCalls = messageRound.toolCalls, !messageToolCalls.isEmpty { var mergedToolCalls = existingSubRounds[subIndex].toolCalls ?? [] for newToolCall in messageToolCalls { @@ -77,7 +77,7 @@ public extension ChatMemory { parentMessage.editAgentRounds = parentRounds } } - + history[parentIndex] = parentMessage } else if let index = history.firstIndex(where: { $0.id == message.id }) { history[index].mergeMessage(with: message) @@ -137,15 +137,17 @@ extension ChatMessage { self.steps = mergedSteps } - + if !message.editAgentRounds.isEmpty { let mergedAgentRounds = mergeEditAgentRounds( - oldRounds: self.editAgentRounds, + oldRounds: self.editAgentRounds, newRounds: message.editAgentRounds ) - + self.editAgentRounds = mergedAgentRounds } + + mergeThinking(into: &self.thinking, from: message.thinking) self.parentTurnId = message.parentTurnId ?? self.parentTurnId @@ -166,7 +168,9 @@ extension ChatMessage { for newRound in newRounds { if let index = mergedAgentRounds.firstIndex(where: { $0.roundId == newRound.roundId }) { mergedAgentRounds[index].reply = mergedAgentRounds[index].reply + newRound.reply - + + mergeThinking(into: &mergedAgentRounds[index].thinking, from: newRound.thinking) + if newRound.toolCalls != nil, !newRound.toolCalls!.isEmpty { var mergedToolCalls = mergedAgentRounds[index].toolCalls ?? [] for newToolCall in newRound.toolCalls! { @@ -266,3 +270,34 @@ extension ChatMessage { return edits } } + +/// Merges incoming thinking deltas into an accumulated thinking array. Deltas are matched by +/// `clientEntryId` (a stable client-generated key), so server delta `id` churn does not split a + /// streaming block. New entries (different `clientEntryId`) append; for the same entry, text + /// concatenates, `id` is replaced with the latest server value, `encrypted` and `title` keep + /// their existing values when the incoming delta omits them, and `isComplete` remains `true` + /// once any delta marks it complete. +internal func mergeThinking(into accumulator: inout [MessageThinking], from incoming: [MessageThinking]) { + for newThinking in incoming { + let hasNewText = !(newThinking.text?.allSatisfy { $0.isEmpty } ?? true) + let hasNewTitle = newThinking.title != nil + + if let index = accumulator.firstIndex(where: { $0.clientEntryId == newThinking.clientEntryId }) { + let existing = accumulator[index] + var mergedText = existing.text ?? [] + if let new = newThinking.text { + mergedText.append(contentsOf: new) + } + accumulator[index] = MessageThinking( + clientEntryId: existing.clientEntryId, + id: newThinking.id, + text: mergedText.isEmpty ? nil : mergedText, + encrypted: newThinking.encrypted ?? existing.encrypted, + title: newThinking.title ?? existing.title, + isComplete: newThinking.isComplete || existing.isComplete + ) + } else if hasNewText || hasNewTitle { + accumulator.append(newThinking) + } + } +} diff --git a/Tool/Sources/ChatAPIService/Models.swift b/Tool/Sources/ChatAPIService/Models.swift index 82337095..0ee0fd1d 100644 --- a/Tool/Sources/ChatAPIService/Models.swift +++ b/Tool/Sources/ChatAPIService/Models.swift @@ -158,7 +158,9 @@ public struct ChatMessage: Equatable, Codable { /// The steps of conversation progress public var steps: [ConversationProgressStep] - + + public var thinking: [MessageThinking] + public var editAgentRounds: [AgentRound] public var parentTurnId: String? @@ -198,6 +200,7 @@ public struct ChatMessage: Equatable, Codable { rating: ConversationRating = .unrated, steps: [ConversationProgressStep] = [], editAgentRounds: [AgentRound] = [], + thinking: [MessageThinking] = [], parentTurnId: String? = nil, panelMessages: [CopilotShowMessageParams] = [], codeReviewRound: CodeReviewRound? = nil, @@ -221,6 +224,7 @@ public struct ChatMessage: Equatable, Codable { self.errorMessages = errorMessages self.rating = rating self.steps = steps + self.thinking = thinking self.editAgentRounds = editAgentRounds self.parentTurnId = parentTurnId self.panelMessages = panelMessages @@ -264,6 +268,7 @@ public struct ChatMessage: Equatable, Codable { suggestedTitle: String? = nil, steps: [ConversationProgressStep] = [], editAgentRounds: [AgentRound] = [], + thinking: [MessageThinking] = [], parentTurnId: String? = nil, codeReviewRound: CodeReviewRound? = nil, fileEdits: [FileEdit] = [], @@ -283,6 +288,7 @@ public struct ChatMessage: Equatable, Codable { suggestedTitle: suggestedTitle, steps: steps, editAgentRounds: editAgentRounds, + thinking: thinking, parentTurnId: parentTurnId, codeReviewRound: codeReviewRound, fileEdits: fileEdits, diff --git a/Tool/Sources/ChatTab/ChatTab.swift b/Tool/Sources/ChatTab/ChatTab.swift index 0612cca5..bcaeac23 100644 --- a/Tool/Sources/ChatTab/ChatTab.swift +++ b/Tool/Sources/ChatTab/ChatTab.swift @@ -19,7 +19,7 @@ public struct ChatTabPreviewInfo: Identifiable, Equatable, Codable { /// The information of a tab. @ObservableState -public struct ChatTabInfo: Identifiable, Equatable, Codable { +public struct ChatTabInfo: Identifiable, Equatable, Hashable, Codable { public var id: String public var title: String? = nil public var isTitleSet: Bool { diff --git a/Tool/Sources/ConversationServiceProvider/ConversationServiceProvider.swift b/Tool/Sources/ConversationServiceProvider/ConversationServiceProvider.swift index d16369a7..0094483e 100644 --- a/Tool/Sources/ConversationServiceProvider/ConversationServiceProvider.swift +++ b/Tool/Sources/ConversationServiceProvider/ConversationServiceProvider.swift @@ -20,6 +20,7 @@ public protocol ConversationServiceType { workspace: WorkspaceInfo, changes: [ReviewChangesParams.Change] ) async throws -> CodeReviewResult? + func generateThinkingTitle(workspace: WorkspaceInfo, params: GenerateThinkingTitleParams) async throws -> GenerateThinkingTitleResponse? } public protocol ConversationServiceProvider { @@ -36,6 +37,7 @@ public protocol ConversationServiceProvider { func agents() async throws -> [ChatAgent]? func notifyChangeTextDocument(fileURL: URL, content: String, version: Int, workspaceURL: URL?) async throws func reviewChanges(_ changes: [ReviewChangesParams.Change]) async throws -> CodeReviewResult? + func generateThinkingTitle(_ params: GenerateThinkingTitleParams) async throws -> GenerateThinkingTitleResponse? } public struct ConversationFileReference: Hashable, Codable, Equatable { @@ -451,6 +453,146 @@ public struct ConversationProgressStep: Codable, Equatable, Identifiable { } } +public struct Thinking: Codable, Equatable { + public let id: String + public let text: [String]? + public let encrypted: String? + + public init(id: String, text: [String]?, encrypted: String?) { + self.id = id + self.text = text + self.encrypted = encrypted + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + id = try container.decode(String.self, forKey: .id) + encrypted = try container.decodeIfPresent(String.self, forKey: .encrypted) + text = try container.decodeStringOrArray(forKey: .text) + } +} + +/// Internal, message-level thinking state. +/// +/// Distinct from the wire/server `Thinking` payload above: that type carries deltas +/// streamed from the LSP, while `MessageThinking` is the accumulated UI state stored on +/// a `ChatMessage` (or `AgentRound`) and persisted across sessions. `isComplete` is a +/// UI/state flag the server never sends — it's set when a thinking block ends. +public struct MessageThinking: Codable, Equatable { + /// Stable client-generated key for this entry. Survives server delta `id` churn (e.g. + /// CodeX models emit a new `id` per delta) and is what the seal/title-attach code paths + /// look up. Persisted; older saved messages without it get a fresh UUID on decode. + public var clientEntryId: UUID + public var id: String + public var text: [String]? + public var encrypted: String? + public var title: String? + public var isComplete: Bool + + public init( + clientEntryId: UUID = UUID(), + id: String, + text: [String]?, + encrypted: String?, + title: String? = nil, + isComplete: Bool = false + ) { + self.clientEntryId = clientEntryId + self.id = id + self.text = text + self.encrypted = encrypted + self.title = title + self.isComplete = isComplete + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + clientEntryId = try container.decodeIfPresent(UUID.self, forKey: .clientEntryId) ?? UUID() + id = try container.decode(String.self, forKey: .id) + encrypted = try container.decodeIfPresent(String.self, forKey: .encrypted) + title = try container.decodeIfPresent(String.self, forKey: .title) + isComplete = try container.decodeIfPresent(Bool.self, forKey: .isComplete) ?? false + text = try container.decodeStringOrArray(forKey: .text) + } + + public init(from server: Thinking, clientEntryId: UUID = UUID(), isComplete: Bool = false) { + self.clientEntryId = clientEntryId + self.id = server.id + self.text = server.text + self.encrypted = server.encrypted + self.title = nil + self.isComplete = isComplete + } + + /// Parses thinking text into title-paired sections. + /// + /// Each "title-only" line (`**Title**` on its own) starts a new section. All lines that + /// follow up to the next title (or end of text) become that section's body. Lines before + /// any title go into a leading section with `title == nil`. + public static func parseSections(from raw: String) -> [ThinkingSection] { + if raw.isEmpty { return [] } + var sections: [ThinkingSection] = [] + var currentTitle: String? = nil + var currentBody: [String] = [] + + func flush() { + let body = currentBody.joined().trimmingCharacters(in: .whitespacesAndNewlines) + if currentTitle != nil || !body.isEmpty { + sections.append(ThinkingSection(title: currentTitle, body: body)) + } + } + + for line in raw.components(separatedBy: "\n") { + let trimmed = line.trimmingCharacters(in: .whitespaces) + if trimmed.hasPrefix("**"), trimmed.hasSuffix("**"), trimmed.count > 4 { + let inner = String(trimmed.dropFirst(2).dropLast(2)) + if !inner.isEmpty, !inner.contains("*") { + flush() + currentTitle = inner + currentBody = [] + continue + } + } + currentBody.append(line + "\n") + } + flush() + return sections + } +} + +public struct ThinkingSection: Equatable { + public let title: String? + public let body: String + + public init(title: String?, body: String) { + self.title = title + self.body = body + } +} + +public extension KeyedDecodingContainer { + /// Decodes a value that the wire format may emit as either a single `String` or `[String]`, + /// normalizing to `[String]?`. Returns `nil` if the key is absent. + func decodeStringOrArray(forKey key: Key) throws -> [String]? { + if let single = try? decode(String.self, forKey: key) { + return [single] + } + return try decodeIfPresent([String].self, forKey: key) + } +} + +public struct ContextSizeInfo: Codable, Equatable { + public let totalTokenLimit: Int + public let systemPromptTokens: Int + public let toolDefinitionTokens: Int + public let userMessagesTokens: Int + public let assistantMessagesTokens: Int + public let attachedFilesTokens: Int + public let toolResultsTokens: Int + public let totalUsedTokens: Int + public let utilizationPercentage: Double +} + public struct DidChangeWatchedFilesEvent: Codable { public var workspaceUri: String public var changes: [FileEvent] diff --git a/Tool/Sources/ConversationServiceProvider/LSPTypes+AgentRound.swift b/Tool/Sources/ConversationServiceProvider/LSPTypes+AgentRound.swift index 69124626..95579d41 100644 --- a/Tool/Sources/ConversationServiceProvider/LSPTypes+AgentRound.swift +++ b/Tool/Sources/ConversationServiceProvider/LSPTypes+AgentRound.swift @@ -7,12 +7,29 @@ public struct AgentRound: Codable, Equatable { public var reply: String public var toolCalls: [AgentToolCall]? public var subAgentRounds: [AgentRound]? - - public init(roundId: Int, reply: String, toolCalls: [AgentToolCall]? = [], subAgentRounds: [AgentRound]? = []) { + public var thinking: [MessageThinking] + + public init(roundId: Int, reply: String, toolCalls: [AgentToolCall]? = [], subAgentRounds: [AgentRound]? = [], thinking: [MessageThinking] = []) { self.roundId = roundId self.reply = reply self.toolCalls = toolCalls self.subAgentRounds = subAgentRounds + self.thinking = thinking + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + roundId = try container.decode(Int.self, forKey: .roundId) + reply = try container.decode(String.self, forKey: .reply) + toolCalls = try container.decodeIfPresent([AgentToolCall].self, forKey: .toolCalls) + subAgentRounds = try container.decodeIfPresent([AgentRound].self, forKey: .subAgentRounds) + if let array = try? container.decodeIfPresent([MessageThinking].self, forKey: .thinking) { + thinking = array + } else if let single = try? container.decodeIfPresent(MessageThinking.self, forKey: .thinking) { + thinking = [single] + } else { + thinking = [] + } } } diff --git a/Tool/Sources/ConversationServiceProvider/LSPTypes.swift b/Tool/Sources/ConversationServiceProvider/LSPTypes.swift index 289fcdbd..cd150aa6 100644 --- a/Tool/Sources/ConversationServiceProvider/LSPTypes.swift +++ b/Tool/Sources/ConversationServiceProvider/LSPTypes.swift @@ -24,6 +24,7 @@ public enum PromptTemplateScope: String, Codable, Equatable { case agentPanel = "agent-panel" case editor = "editor" case inline = "inline" + case inlineAgent = "inline-agent" case completion = "completion" } @@ -46,6 +47,7 @@ public struct CopilotModel: Codable, Equatable { public let isChatFallback: Bool public let capabilities: CopilotModelCapabilities public let billing: CopilotModelBilling? + public let degradationReason: String? } public struct CopilotModelPolicy: Codable, Equatable { @@ -76,6 +78,7 @@ public enum ChatMode: String, Codable { case Ask = "Ask" case Edit = "Edit" case Agent = "Agent" + case InlineAgent = "InlineAgent" } public struct ConversationMode: Codable, Equatable { @@ -512,6 +515,20 @@ public struct ActionCommand: Codable, Equatable, Hashable { // MARK: - Copilot Code Review +public struct GenerateThinkingTitleParams: Codable { + public var thinkingContent: String? + public var extractedTitles: [String]? + + public init(thinkingContent: String? = nil, extractedTitles: [String]? = nil) { + self.thinkingContent = thinkingContent + self.extractedTitles = extractedTitles + } +} + +public struct GenerateThinkingTitleResponse: Codable { + public var title: String +} + public struct ReviewChangesParams: Codable, Equatable { public struct Change: Codable, Equatable { public let uri: DocumentUri @@ -539,8 +556,8 @@ public struct ReviewChangesParams: Codable, Equatable { } public struct ReviewComment: Codable, Equatable, Hashable { - // Self-defined `id` for using in comment operation. Add an init value to bypass decoding - public let id: String = UUID().uuidString + // Self-defined `id` for using in comment operation. Generated when missing from payload. + public let id: String public let uri: DocumentUri public let range: LSPRange public let message: String @@ -549,7 +566,7 @@ public struct ReviewComment: Codable, Equatable, Hashable { // enum: low, medium, high public let severity: String public let suggestion: String? - + public init( uri: DocumentUri, range: LSPRange, @@ -558,6 +575,7 @@ public struct ReviewComment: Codable, Equatable, Hashable { severity: String, suggestion: String? ) { + self.id = UUID().uuidString self.uri = uri self.range = range self.message = message @@ -565,6 +583,21 @@ public struct ReviewComment: Codable, Equatable, Hashable { self.severity = severity self.suggestion = suggestion } + + private enum CodingKeys: String, CodingKey { + case id, uri, range, message, kind, severity, suggestion + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.id = try container.decodeIfPresent(String.self, forKey: .id) ?? UUID().uuidString + self.uri = try container.decode(DocumentUri.self, forKey: .uri) + self.range = try container.decode(LSPRange.self, forKey: .range) + self.message = try container.decode(String.self, forKey: .message) + self.kind = try container.decode(String.self, forKey: .kind) + self.severity = try container.decode(String.self, forKey: .severity) + self.suggestion = try container.decodeIfPresent(String.self, forKey: .suggestion) + } } public struct CodeReviewResult: Codable, Equatable { diff --git a/Tool/Sources/GitHubCopilotService/Conversation/CompressionHandler.swift b/Tool/Sources/GitHubCopilotService/Conversation/CompressionHandler.swift new file mode 100644 index 00000000..841d75d7 --- /dev/null +++ b/Tool/Sources/GitHubCopilotService/Conversation/CompressionHandler.swift @@ -0,0 +1,14 @@ +import Combine +import Foundation + +public protocol CompressionHandler { + var onCompressionStarted: PassthroughSubject { get } // conversationId + var onCompressionCompleted: PassthroughSubject { get } +} + +public final class CompressionHandlerImpl: CompressionHandler { + public static let shared = CompressionHandlerImpl() + + public var onCompressionStarted = PassthroughSubject() + public var onCompressionCompleted = PassthroughSubject() +} diff --git a/Tool/Sources/GitHubCopilotService/Conversation/ShowMessageRequestHandler.swift b/Tool/Sources/GitHubCopilotService/Conversation/ShowMessageRequestHandler.swift index ad2de6a7..05e811bf 100644 --- a/Tool/Sources/GitHubCopilotService/Conversation/ShowMessageRequestHandler.swift +++ b/Tool/Sources/GitHubCopilotService/Conversation/ShowMessageRequestHandler.swift @@ -4,6 +4,7 @@ import Combine import Logger import AppKit import LanguageServerProtocol +import NotificationCenterCoordinator import UserNotifications public protocol ShowMessageRequestHandler { @@ -13,28 +14,10 @@ public protocol ShowMessageRequestHandler { ) } -public final class ShowMessageRequestHandlerImpl: NSObject, ShowMessageRequestHandler, UNUserNotificationCenterDelegate { +public final class ShowMessageRequestHandlerImpl: ShowMessageRequestHandler { public static let shared = ShowMessageRequestHandlerImpl() - - private var isNotificationSetup = false - - private override init() { - super.init() - } - - @MainActor - private func setupNotificationCenterIfNeeded() async { - guard !isNotificationSetup else { return } - guard Bundle.main.bundleIdentifier != nil else { - // Skip notification setup in test environment - return - } - - isNotificationSetup = true - UNUserNotificationCenter.current().delegate = self - _ = try? await UNUserNotificationCenter.current() - .requestAuthorization(options: [.alert, .sound]) - } + + private init() {} public func handleShowMessageRequest( _ request: ShowMessageRequest, @@ -43,8 +26,8 @@ public final class ShowMessageRequestHandlerImpl: NSObject, ShowMessageRequestHa guard let params = request.params else { return } Logger.gitHubCopilot.debug("Received Show Message Request: \(params)") Task { @MainActor in - await setupNotificationCenterIfNeeded() - + await NotificationCenterCoordinator.shared.setupIfNeeded() + let actionCount = params.actions?.count ?? 0 // Use notification for messages with no action, alert for messages with actions @@ -103,16 +86,4 @@ public final class ShowMessageRequestHandlerImpl: NSObject, ShowMessageRequestHa return actions[buttonIndex] } - - // MARK: - UNUserNotificationCenterDelegate - - // This method is called when a notification is delivered while the app is in the foreground - public func userNotificationCenter( - _ center: UNUserNotificationCenter, - willPresent notification: UNNotification, - withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void - ) { - // Show the notification banner even when app is in foreground - completionHandler([.banner, .list, .badge, .sound]) - } } diff --git a/Tool/Sources/GitHubCopilotService/LanguageServer/CopilotLocalProcessServer.swift b/Tool/Sources/GitHubCopilotService/LanguageServer/CopilotLocalProcessServer.swift index 7b380443..ad691ce7 100644 --- a/Tool/Sources/GitHubCopilotService/LanguageServer/CopilotLocalProcessServer.swift +++ b/Tool/Sources/GitHubCopilotService/LanguageServer/CopilotLocalProcessServer.swift @@ -225,6 +225,15 @@ class CopilotLocalProcessServer { case "policy/didChange": notificationPublisher.send(anyNotification) return true + case "$/copilot/compressionStarted": + notificationPublisher.send(anyNotification) + return true + case "$/copilot/compressionCompleted": + notificationPublisher.send(anyNotification) + return true + case "$/copilot/rateLimitWarning": + notificationPublisher.send(anyNotification) + return true case "conversation/preconditionsNotification", "statusNotification": // Ignore return true diff --git a/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotRequest.swift b/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotRequest.swift index 0ada31e5..284c27c7 100644 --- a/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotRequest.swift +++ b/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotRequest.swift @@ -108,6 +108,9 @@ public func editorConfiguration(includeMCP: Bool) -> JSONValue { let trustToolAnnotations = UserDefaults.shared.value(for: \.trustToolAnnotations) d["trustToolAnnotations"] = .bool(trustToolAnnotations) + let autoCompress = UserDefaults.shared.value(for: \.autoCompress) + d["autoCompress"] = .bool(autoCompress) + let state = UserDefaults.autoApproval.value(for: \.sensitiveFilesGlobalApprovals) var autoApproveList: [JSONValue] = [] for (key, rule) in state.rules { @@ -715,6 +718,20 @@ enum GitHubCopilotRequest { return .custom("copilot/byok/listApiKeys", dict, ClientRequest.NullHandler) } } + + // MARK: Thinking + struct GenerateThinkingTitle: GitHubCopilotRequestType { + typealias Response = GenerateThinkingTitleResponse + + var params: GenerateThinkingTitleParams + + var request: ClientRequest { + let data = (try? JSONEncoder().encode(params)) ?? Data() + let dict = (try? JSONDecoder().decode(JSONValue.self, from: data)) ?? .hash([:]) + return .custom("thinking/generateTitle", dict, ClientRequest.NullHandler) + } + } + } // MARK: Notifications @@ -750,6 +767,36 @@ public enum GitHubCopilotNotification { } } + public enum CompressionTrigger: String, Codable { + case preTurn = "pre-turn" + case postToolCall = "post-tool-call" + case manual = "manual" + } + + public struct CompressionStartedNotification: Codable { + public var conversationId: String + public var partitionId: Int + public var reason: CompressionTrigger + + public static func decode(fromParams params: JSONValue?) -> CompressionStartedNotification? { + try? JSONDecoder().decode(Self.self, from: (try? JSONEncoder().encode(params)) ?? Data()) + } + } + + public struct CompressionCompletedNotification: Codable { + public var conversationId: String + public var archivedPartitionId: Int + public var newPartitionId: Int + public var summaryLength: Int + public var turnCount: Int + public var durationMs: Int + public var contextInfo: ContextSizeInfo? + + public static func decode(fromParams params: JSONValue?) -> CompressionCompletedNotification? { + try? JSONDecoder().decode(Self.self, from: (try? JSONEncoder().encode(params)) ?? Data()) + } + } + public struct MCPRuntimeNotification: Codable { public enum MCPRuntimeLogLevel: String, Codable { case Info = "info" diff --git a/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotRequestTypes/Conversation.swift b/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotRequestTypes/Conversation.swift index 95bff025..72fbbc00 100644 --- a/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotRequestTypes/Conversation.swift +++ b/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotRequestTypes/Conversation.swift @@ -35,6 +35,8 @@ public struct ConversationProgressReport: BaseConversationProgress { public let steps: [ConversationProgressStep]? public let editAgentRounds: [AgentRound]? public let parentTurnId: String? + public let thinking: Thinking? + public let contextSize: ContextSizeInfo? } public struct ConversationProgressEnd: BaseConversationProgress { diff --git a/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotService.swift b/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotService.swift index 978c6e92..ef45792c 100644 --- a/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotService.swift +++ b/Tool/Sources/GitHubCopilotService/LanguageServer/GitHubCopilotService.swift @@ -101,6 +101,7 @@ public protocol GitHubCopilotConversationServiceType { func models() async throws -> [CopilotModel] func registerTools(tools: [LanguageModelToolInformation]) async throws -> [LanguageModelTool] func updateToolsStatus(params: UpdateToolsStatusParams) async throws -> [LanguageModelTool] + func generateThinkingTitle(params: GenerateThinkingTitleParams) async throws -> GenerateThinkingTitleResponse } protocol GitHubCopilotLSP { @@ -170,6 +171,8 @@ public extension Notification.Name { .Name("com.github.CopilotForXcode.GithubCopilotAgentAutoApprovalDidChange") static let githubCopilotAgentTrustToolAnnotationsDidChange = Notification .Name("com.github.CopilotForXcode.GithubCopilotAgentTrustToolAnnotationsDidChange") + static let githubCopilotAgentAutoCompressDidChange = Notification + .Name("com.github.CopilotForXcode.GithubCopilotAgentAutoCompressDidChange") } public class GitHubCopilotBaseService { @@ -808,6 +811,11 @@ public final class GitHubCopilotService: } } + @GitHubCopilotSuggestionActor + public func generateThinkingTitle(params: GenerateThinkingTitleParams) async throws -> GenerateThinkingTitleResponse { + try await sendRequest(GitHubCopilotRequest.GenerateThinkingTitle(params: params)) + } + @GitHubCopilotSuggestionActor public func registerTools(tools: [LanguageModelToolInformation]) async throws -> [LanguageModelTool] { do { @@ -1483,6 +1491,10 @@ public final class GitHubCopilotService: DistributedNotificationCenter.default() .publisher(for: .githubCopilotAgentTrustToolAnnotationsDidChange) .map { _ in "agentTrustToolAnnotations" } + .eraseToAnyPublisher(), + DistributedNotificationCenter.default() + .publisher(for: .githubCopilotAgentAutoCompressDidChange) + .map { _ in "agentAutoCompress" } .eraseToAnyPublisher() ) diff --git a/Tool/Sources/GitHubCopilotService/LanguageServer/ServerNotificationHandler.swift b/Tool/Sources/GitHubCopilotService/LanguageServer/ServerNotificationHandler.swift index e7f9eba9..bbc7c4c6 100644 --- a/Tool/Sources/GitHubCopilotService/LanguageServer/ServerNotificationHandler.swift +++ b/Tool/Sources/GitHubCopilotService/LanguageServer/ServerNotificationHandler.swift @@ -14,6 +14,8 @@ class ServerNotificationHandlerImpl: ServerNotificationHandler { var conversationProgressHandler: ConversationProgressHandler = ConversationProgressHandlerImpl.shared var featureFlagNotifier: FeatureFlagNotifier = FeatureFlagNotifierImpl.shared var copilotPolicyNotifier: CopilotPolicyNotifier = CopilotPolicyNotifierImpl.shared + var compressionHandler: CompressionHandler = CompressionHandlerImpl.shared + var rateLimitNotifier: RateLimitNotifier = RateLimitNotifierImpl.shared init() { self.protocolProgressSubject = PassthroughSubject() @@ -54,6 +56,27 @@ class ServerNotificationHandlerImpl: ServerNotificationHandler { copilotPolicyNotifier.handleCopilotPolicyNotification(policy) } break + case "$/copilot/compressionStarted": + if let payload = GitHubCopilotNotification.CompressionStartedNotification + .decode(fromParams: notification.params) { + compressionHandler.onCompressionStarted.send(payload.conversationId) + } + break + case "$/copilot/compressionCompleted": + if let payload = GitHubCopilotNotification.CompressionCompletedNotification + .decode(fromParams: notification.params) { + compressionHandler.onCompressionCompleted.send(payload) + } + break + case "$/copilot/rateLimitWarning": + if let data = try? JSONEncoder().encode(notification.params), + let params = try? JSONDecoder().decode( + RateLimitWarningParams.self, + from: data + ) { + rateLimitNotifier.handleRateLimitWarning(params) + } + break default: break } diff --git a/Tool/Sources/GitHubCopilotService/Services/GitHubCopilotConversationService.swift b/Tool/Sources/GitHubCopilotService/Services/GitHubCopilotConversationService.swift index b99c854f..62f12e6f 100644 --- a/Tool/Sources/GitHubCopilotService/Services/GitHubCopilotConversationService.swift +++ b/Tool/Sources/GitHubCopilotService/Services/GitHubCopilotConversationService.swift @@ -116,9 +116,8 @@ public final class GitHubCopilotConversationService: ConversationServiceType { public func modes(workspace: WorkspaceInfo) async throws -> [ConversationMode]? { guard let service = await serviceLocator.getService(from: workspace) else { return nil } - let isPreviewEnabled = FeatureFlagNotifierImpl.shared.featureFlags.editorPreviewFeatures let isCustomAgentEnabled = CopilotPolicyNotifierImpl.shared.copilotPolicy.customAgentEnabled - let workspaceFolders = isPreviewEnabled && isCustomAgentEnabled ? getWorkspaceFolders( + let workspaceFolders = isCustomAgentEnabled ? getWorkspaceFolders( workspace: workspace ) : nil return try await service.modes(workspaceFolders: workspaceFolders) @@ -154,5 +153,13 @@ public final class GitHubCopilotConversationService: ConversationServiceType { workspaceFolders: getWorkspaceFolders(workspace: workspace)) ) } + + public func generateThinkingTitle( + workspace: WorkspaceInfo, + params: GenerateThinkingTitleParams + ) async throws -> GenerateThinkingTitleResponse? { + guard let service = await serviceLocator.getService(from: workspace) else { return nil } + return try await service.generateThinkingTitle(params: params) + } } diff --git a/Tool/Sources/GitHubCopilotService/Services/RateLimitNotifier.swift b/Tool/Sources/GitHubCopilotService/Services/RateLimitNotifier.swift new file mode 100644 index 00000000..30bb3f25 --- /dev/null +++ b/Tool/Sources/GitHubCopilotService/Services/RateLimitNotifier.swift @@ -0,0 +1,106 @@ +import AppKit +import Combine +import Foundation +import Logger +import NotificationCenterCoordinator +import UserNotifications + +public struct UsageRateLimit: Hashable, Codable { + public var entitlement: Int + public var percentRemaining: Double + public var resetDate: String +} + +public struct RateLimitWarningParams: Hashable, Codable { + public var type: String // "weekly" or "session" + public var rateLimit: UsageRateLimit + public var message: String +} + +public protocol RateLimitNotifier { + func handleRateLimitWarning(_ params: RateLimitWarningParams) +} + +public class RateLimitNotifierImpl: NSObject, RateLimitNotifier, ObservableObject { + public static let shared = RateLimitNotifierImpl() + + @Published public var currentWarning: RateLimitWarningParams? + + private static let categoryIdentifier = "rateLimitWarningCategory" + private static let learnMoreActionIdentifier = "rateLimitLearnMoreAction" + private static let learnMoreURL = URL( + string: "https://aka.ms/github-copilot-rate-limit-error" + )! + + private var isCategoryRegistered = false + + private override init() { + super.init() + } + + private func registerCategoryIfNeeded() { + guard !isCategoryRegistered else { return } + isCategoryRegistered = true + + let learnMoreAction = UNNotificationAction( + identifier: Self.learnMoreActionIdentifier, + title: "Learn more", + options: [.foreground] + ) + let category = UNNotificationCategory( + identifier: Self.categoryIdentifier, + actions: [learnMoreAction], + intentIdentifiers: [], + options: [] + ) + + NotificationCenterCoordinator.shared.register( + category: category, + handler: { response in + if response.actionIdentifier == Self.learnMoreActionIdentifier { + NSWorkspace.shared.open(Self.learnMoreURL) + } + }, + for: Self.categoryIdentifier + ) + } + + public func handleRateLimitWarning(_ params: RateLimitWarningParams) { + DispatchQueue.main.async { [weak self] in + self?.currentWarning = params + } + + Task { @MainActor in + await NotificationCenterCoordinator.shared.setupIfNeeded() + self.registerCategoryIfNeeded() + await sendAppleNotification(params) + } + } + + public func dismissWarning() { + DispatchQueue.main.async { [weak self] in + self?.currentWarning = nil + } + } + + @MainActor + private func sendAppleNotification(_ params: RateLimitWarningParams) async { + let content = UNMutableNotificationContent() + content.title = "GitHub Copilot for Xcode" + content.body = params.message + content.sound = .default + content.categoryIdentifier = Self.categoryIdentifier + + let request = UNNotificationRequest( + identifier: "rateLimitWarning-\(UUID().uuidString)", + content: content, + trigger: nil + ) + + do { + try await UNUserNotificationCenter.current().add(request) + } catch { + Logger.gitHubCopilot.error("Failed to show rate limit notification: \(error)") + } + } +} diff --git a/Tool/Sources/HostAppActivator/HostAppActivator.swift b/Tool/Sources/HostAppActivator/HostAppActivator.swift index 6e52319b..2274c13d 100644 --- a/Tool/Sources/HostAppActivator/HostAppActivator.swift +++ b/Tool/Sources/HostAppActivator/HostAppActivator.swift @@ -45,17 +45,14 @@ public func launchHostAppSettings() throws { let activated = hostApp.activate(options: [.activateIgnoringOtherApps]) Logger.ui.info("\(hostAppName()) activated: \(activated)") - let scriptSuccess = tryLaunchWithAppleScript() - - // If AppleScript fails, fall back to notification center - if !scriptSuccess { - DistributedNotificationCenter.default().postNotificationName( - .openSettingsWindowRequest, - object: nil - ) - Logger.ui.info("\(hostAppName()) settings notification sent after activation") - return - } + _ = tryLaunchWithAppleScript() + + DistributedNotificationCenter.default().postNotificationName( + .openSettingsWindowRequest, + object: nil + ) + Logger.ui.info("\(hostAppName()) settings notification sent after activation") + return } else { // If app is not running, launch it with the settings flag try launchHostAppWithArgs(args: ["--settings"]) diff --git a/Tool/Sources/NotificationCenterCoordinator/NotificationCenterCoordinator.swift b/Tool/Sources/NotificationCenterCoordinator/NotificationCenterCoordinator.swift new file mode 100644 index 00000000..4e69163e --- /dev/null +++ b/Tool/Sources/NotificationCenterCoordinator/NotificationCenterCoordinator.swift @@ -0,0 +1,82 @@ +import Foundation +import UserNotifications + +/// A single shared delegate for `UNUserNotificationCenter`. +/// +/// `UNUserNotificationCenter` has only one `delegate` and one set of categories. +/// Multiple features (rate limit warnings, show-message requests, etc.) need to +/// post notifications and handle action taps, so they register handlers here +/// keyed by category identifier instead of each assigning themselves as the +/// delegate. +public final class NotificationCenterCoordinator: NSObject, UNUserNotificationCenterDelegate { + public static let shared = NotificationCenterCoordinator() + + public typealias ActionHandler = (UNNotificationResponse) -> Void + + private var isNotificationSetup = false + private var categories: [String: UNNotificationCategory] = [:] + private var actionHandlers: [String: ActionHandler] = [:] + private let lock = NSLock() + + private override init() { + super.init() + } + + /// Ensures the notification center delegate is set and authorization has + /// been requested. Safe to call multiple times. + @MainActor + public func setupIfNeeded() async { + guard !isNotificationSetup else { return } + guard Bundle.main.bundleIdentifier != nil else { + // Skip notification setup in test environment. + return + } + isNotificationSetup = true + UNUserNotificationCenter.current().delegate = self + _ = try? await UNUserNotificationCenter.current() + .requestAuthorization(options: [.alert, .sound]) + } + + /// Registers a category (optional) and an action handler for notifications + /// whose `categoryIdentifier` matches. + public func register( + category: UNNotificationCategory?, + handler: @escaping ActionHandler, + for categoryIdentifier: String + ) { + lock.lock() + if let category { + categories[categoryIdentifier] = category + } + actionHandlers[categoryIdentifier] = handler + let allCategories = Set(categories.values) + lock.unlock() + + UNUserNotificationCenter.current().setNotificationCategories(allCategories) + } + + // MARK: - UNUserNotificationCenterDelegate + + public func userNotificationCenter( + _ center: UNUserNotificationCenter, + willPresent notification: UNNotification, + withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void + ) { + completionHandler([.banner, .list, .badge, .sound]) + } + + public func userNotificationCenter( + _ center: UNUserNotificationCenter, + didReceive response: UNNotificationResponse, + withCompletionHandler completionHandler: @escaping () -> Void + ) { + let categoryIdentifier = response.notification.request.content.categoryIdentifier + lock.lock() + let handler = actionHandlers[categoryIdentifier] + lock.unlock() + Task { @MainActor in + handler?(response) + completionHandler() + } + } +} diff --git a/Tool/Sources/Persist/Storage/ConversationStorage/ConversationStorage.swift b/Tool/Sources/Persist/Storage/ConversationStorage/ConversationStorage.swift index 2ec2f53c..765d35e4 100644 --- a/Tool/Sources/Persist/Storage/ConversationStorage/ConversationStorage.swift +++ b/Tool/Sources/Persist/Storage/ConversationStorage/ConversationStorage.swift @@ -80,8 +80,6 @@ public final class ConversationStorage: ConversationStorageProtocol { try withDBTransaction { db in - let now = Date().timeIntervalSince1970 - for operation in request.operations { switch operation { case .upsertConversation(let conversationItems): @@ -137,7 +135,7 @@ public final class ConversationStorage: ConversationStorageProtocol { let table = turnTable.table let column = turnTable.column - var query = table + let query = table .filter(column.conversationID == conversationID) .order(column.rowID.asc) let rowIterator = try db.prepareRowIterator(query) diff --git a/Tool/Sources/Preferences/Keys.swift b/Tool/Sources/Preferences/Keys.swift index 50ead5c8..400c07e3 100644 --- a/Tool/Sources/Preferences/Keys.swift +++ b/Tool/Sources/Preferences/Keys.swift @@ -336,6 +336,10 @@ public extension UserDefaultPreferenceKeys { var enableSubagent: PreferenceKey { .init(defaultValue: true, key: "EnableSubagent") } + + var autoCompress: PreferenceKey { + .init(defaultValue: true, key: "AutoCompress") + } } // MARK: - Theme diff --git a/Tool/Sources/Preferences/Types/Locale.swift b/Tool/Sources/Preferences/Types/Locale.swift index 6b50d82d..6bd25fe2 100644 --- a/Tool/Sources/Preferences/Types/Locale.swift +++ b/Tool/Sources/Preferences/Types/Locale.swift @@ -2,14 +2,14 @@ import Foundation public extension Locale { static var availableLocalizedLocales: [String] { - let localizedLocales = Locale.isoLanguageCodes.compactMap { - Locale(identifier: "en-US").localizedString(forLanguageCode: $0) + let localizedLocales = Locale.LanguageCode.isoLanguageCodes.compactMap { + Locale(identifier: "en-US").localizedString(forLanguageCode: $0.identifier) } .sorted() return localizedLocales } var languageName: String { - localizedString(forLanguageCode: languageCode ?? "") ?? "" + localizedString(forLanguageCode: language.languageCode?.identifier ?? "") ?? "" } } diff --git a/Tool/Sources/SharedUIComponents/ConditionalFontWeight.swift b/Tool/Sources/SharedUIComponents/ConditionalFontWeight.swift index 55cc15c7..b09c94ba 100644 --- a/Tool/Sources/SharedUIComponents/ConditionalFontWeight.swift +++ b/Tool/Sources/SharedUIComponents/ConditionalFontWeight.swift @@ -8,11 +8,7 @@ public struct ConditionalFontWeight: ViewModifier { } public func body(content: Content) -> some View { - if #available(macOS 13.0, *), weight != nil { - content.fontWeight(weight) - } else { - content - } + content.fontWeight(weight) } } diff --git a/Tool/Sources/SharedUIComponents/CustomScrollView.swift b/Tool/Sources/SharedUIComponents/CustomScrollView.swift index 0eb486f0..91c73fae 100644 --- a/Tool/Sources/SharedUIComponents/CustomScrollView.swift +++ b/Tool/Sources/SharedUIComponents/CustomScrollView.swift @@ -44,11 +44,7 @@ public struct CustomScrollView: View { } .listStyle(.plain) .modify { view in - if #available(macOS 13.0, *) { - view.listRowSeparator(.hidden).listSectionSeparator(.hidden) - } else { - view - } + view.listRowSeparator(.hidden).listSectionSeparator(.hidden) } .frame(idealHeight: max(10, height)) .onPreferenceChange(CustomScrollViewHeightPreferenceKey.self) { newHeight in diff --git a/Tool/Sources/SharedUIComponents/FontPicker.swift b/Tool/Sources/SharedUIComponents/FontPicker.swift index 2f91c9d0..cc2f4f4a 100644 --- a/Tool/Sources/SharedUIComponents/FontPicker.swift +++ b/Tool/Sources/SharedUIComponents/FontPicker.swift @@ -14,17 +14,10 @@ public struct FontPicker: View { } public var body: some View { - if #available(macOS 13.0, *) { - LabeledContent { - button - } label: { - label - } - } else { - HStack { - label - button - } + LabeledContent { + button + } label: { + label } } diff --git a/Tool/Sources/SharedUIComponents/SplitButton.swift b/Tool/Sources/SharedUIComponents/SplitButton.swift index 8ddead7b..b3388850 100644 --- a/Tool/Sources/SharedUIComponents/SplitButton.swift +++ b/Tool/Sources/SharedUIComponents/SplitButton.swift @@ -35,7 +35,6 @@ public struct SplitButtonMenuItem: Identifiable { } } -@available(macOS 13.0, *) private enum SplitButtonMenuBuilder { static func buildMenu( items: [SplitButtonMenuItem], @@ -87,7 +86,6 @@ private enum SplitButtonMenuBuilder { // MARK: - SplitButton using NSComboButton -@available(macOS 13.0, *) public struct SplitButton: View { let title: String let primaryAction: () -> Void @@ -155,7 +153,6 @@ public struct SplitButton: View { } } -@available(macOS 13.0, *) private struct ProminentMenuButton: NSViewRepresentable { let menuItems: [SplitButtonMenuItem] let isDisabled: Bool @@ -215,7 +212,6 @@ private struct ProminentMenuButton: NSViewRepresentable { } } -@available(macOS 13.0, *) struct SplitButtonRepresentable: NSViewRepresentable { let title: String let primaryAction: () -> Void diff --git a/Tool/Sources/TelemetryService/TelemetryCleaner.swift b/Tool/Sources/TelemetryService/TelemetryCleaner.swift index 069ad843..5dcd6987 100644 --- a/Tool/Sources/TelemetryService/TelemetryCleaner.swift +++ b/Tool/Sources/TelemetryService/TelemetryCleaner.swift @@ -69,7 +69,7 @@ public struct TelemetryCleaner { ("Email", "@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-]+") ] - var cleanedValue = value + let cleanedValue = value for (label, pattern) in patterns { if let regex = try? NSRegularExpression(pattern: pattern) { if regex.firstMatch( diff --git a/Tool/Sources/Toast/Toast.swift b/Tool/Sources/Toast/Toast.swift index 704af7df..bb989885 100644 --- a/Tool/Sources/Toast/Toast.swift +++ b/Tool/Sources/Toast/Toast.swift @@ -265,28 +265,11 @@ public extension NSWorkspace { /// Opens the System Preferences/Settings app at the Extensions pane /// - Parameter extensionPointIdentifier: Optional identifier for specific extension type static func openExtensionsPreferences(extensionPointIdentifier: String? = nil) { - if #available(macOS 13.0, *) { - var urlString = "x-apple.systempreferences:com.apple.ExtensionsPreferences" - if let extensionPointIdentifier = extensionPointIdentifier { - urlString += "?extensionPointIdentifier=\(extensionPointIdentifier)" - } - NSWorkspace.shared.open(URL(string: urlString)!) - } else { - let process = Process() - process.executableURL = URL(fileURLWithPath: "/usr/bin/open") - process.arguments = [ - "-b", - "com.apple.systempreferences", - "/System/Library/PreferencePanes/Extensions.prefPane" - ] - - do { - try process.run() - } catch { - // Handle error silently - return - } + var urlString = "x-apple.systempreferences:com.apple.ExtensionsPreferences" + if let extensionPointIdentifier = extensionPointIdentifier { + urlString += "?extensionPointIdentifier=\(extensionPointIdentifier)" } + NSWorkspace.shared.open(URL(string: urlString)!) } /// Opens the Xcode Extensions preferences directly diff --git a/Tool/Sources/Workspace/Workspace.swift b/Tool/Sources/Workspace/Workspace.swift index a179bc83..9d262102 100644 --- a/Tool/Sources/Workspace/Workspace.swift +++ b/Tool/Sources/Workspace/Workspace.swift @@ -260,7 +260,6 @@ extension Workspace { // Handle empty old content (new file) if oldContent.isEmpty { - let endPosition = calculateEndPosition(content: oldContent) return [TextDocumentContentChangeEvent( range: LSPRange( start: Position(line: 0, character: 0), diff --git a/Tool/Sources/WorkspaceSuggestionService/Filespace+SuggestionService.swift b/Tool/Sources/WorkspaceSuggestionService/Filespace+SuggestionService.swift index 03656855..d99a744f 100644 --- a/Tool/Sources/WorkspaceSuggestionService/Filespace+SuggestionService.swift +++ b/Tool/Sources/WorkspaceSuggestionService/Filespace+SuggestionService.swift @@ -153,7 +153,7 @@ public extension Filespace { /// - Returns: `true` if the nes suggestion is still valid @WorkspaceActor func validateNESSuggestions(lines: [String], cursorPosition: CursorPosition) -> Bool { - guard let presentingNESSuggestion else { return false } + guard presentingNESSuggestion != nil else { return false } let updatedSnapshot = FilespaceSuggestionSnapshot(lines: lines, cursorPosition: cursorPosition) diff --git a/Tool/Sources/XPCShared/XPCCommunicationBridge.swift b/Tool/Sources/XPCShared/XPCCommunicationBridge.swift index 4b7d09cb..d54976d4 100644 --- a/Tool/Sources/XPCShared/XPCCommunicationBridge.swift +++ b/Tool/Sources/XPCShared/XPCCommunicationBridge.swift @@ -80,7 +80,6 @@ extension XPCCommunicationBridge { } } -@available(macOS 13.0, *) public func showBackgroundPermissionAlert() { let alert = NSAlert() alert.messageText = "Background Permission Required" diff --git a/Tool/Sources/XcodeInspector/XcodeInspector.swift b/Tool/Sources/XcodeInspector/XcodeInspector.swift index 6a8c5d5e..8c93c60b 100644 --- a/Tool/Sources/XcodeInspector/XcodeInspector.swift +++ b/Tool/Sources/XcodeInspector/XcodeInspector.swift @@ -326,15 +326,13 @@ public final class XcodeInspector: ObservableObject { .value(for: \.restartXcodeInspectorIfAccessibilityAPIIsMalfunctioning) { let malfunctionCheck = Task { @XcodeInspectorActor [weak self] in - if #available(macOS 13.0, *) { - let notifications = await xcode.axNotifications.notifications().filter { - $0.kind == .uiElementDestroyed - }.debounce(for: .milliseconds(1000)) - for await _ in notifications { - guard let self else { return } - try Task.checkCancellation() - self.checkForAccessibilityMalfunction("Element Destroyed") - } + let notifications = await xcode.axNotifications.notifications().filter { + $0.kind == .uiElementDestroyed + }.debounce(for: .milliseconds(1000)) + for await _ in notifications { + guard let self else { return } + try Task.checkCancellation() + self.checkForAccessibilityMalfunction("Element Destroyed") } }