Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions app/Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
// 从菜单栏“退出”触发的才真正退出
// 清理完成后的第二次 terminate 才真正退出。
if shouldReallyQuit {
return .terminateNow
}
// 否则(Dock 右键退出 / Cmd+Q):关闭控制台窗口,隐藏 Dock 图标
for window in NSApp.windows where window.isVisible && !window.className.contains("StatusBar") {
window.close()

// Cmd+Q、Dock“退出”和菜单栏“退出”必须走同一套本地代理清理流程。
// quitApp 完成异步 stop 后会设置 shouldReallyQuit 并再次 terminate。
if menuBarController != nil {
Task { @MainActor [weak self] in
self?.menuBarController.quitApp()
}
return .terminateCancel
}
NSApp.setActivationPolicy(.accessory)
return .terminateCancel

// 启动尚未完成、控制器还未建立时没有需要清理的子进程。
shouldReallyQuit = true
return .terminateNow
}

func applicationDidFinishLaunching(_ notification: Notification) {
Expand Down
120 changes: 73 additions & 47 deletions app/Sources/MenuBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ enum ServiceState {
}

enum LocalServiceControl {
enum ShutdownPlan: Equatable {
case stop
case alreadyStopped
}

static func resolvedPort(pidFileContents: String?, storedPort: Int) -> Int {
guard let pidFileContents,
let data = pidFileContents.data(using: .utf8),
Expand All @@ -23,6 +28,28 @@ enum LocalServiceControl {
static func healthURL(port: Int) -> URL {
URL(string: "http://127.0.0.1:\(port)/admin/health")!
}

/// 退出 App 和安装更新只关心本机进程是否真实存在,与当前管理连接是
/// 本地还是远程无关。未运行时跳过 stop,避免 CLI 的“无进程”错误阻断更新。
static func shutdownPlan(isLocalServiceRunning: Bool) -> ShutdownPlan {
isLocalServiceRunning ? .stop : .alreadyStopped
}
}

enum ForegroundAlertPresentation {
static func configure(_ alert: NSAlert) {
alert.window.level = .modalPanel
alert.window.collectionBehavior.insert(.moveToActiveSpace)
}

@MainActor
@discardableResult
static func run(_ alert: NSAlert) -> NSApplication.ModalResponse {
configure(alert)
NSApp.activate(ignoringOtherApps: true)
alert.window.orderFrontRegardless()
return alert.runModal()
}
}

class MenuBarController: NSObject, NSMenuDelegate {
Expand Down Expand Up @@ -1153,21 +1180,6 @@ class MenuBarController: NSObject, NSMenuDelegate {
guard !isQuitting else { return }
isQuitting = true

// 远程管理模式下退出 App 只结束本地 UI,不触碰任何服务进程。
if client.usesCustomManagementURL {
finishQuit()
return
}

// 服务已经停止时不再调用 stop CLI。旧路径会等待一个没有目标进程的
// stop 流程,导致菜单项高亮但应用迟迟不退出。
if serviceState == .stopped {
finishQuit()
return
}

setTransientStatus(loc("status.stopping"))

// 如果已有 start/stop/restart 正在进行,先等它完成,再发起唯一的
// stop。这样退出不会和后台生命周期操作同时争抢 PID 文件。
Task { @MainActor [weak self] in
Expand All @@ -1176,7 +1188,17 @@ class MenuBarController: NSObject, NSMenuDelegate {
await operation.value
}

let result = await self.runCLIAndWait("stop", port: self.localServiceControlPort)
let localPort = self.localServiceControlPort
let plan = LocalServiceControl.shutdownPlan(
isLocalServiceRunning: await self.isLocalServiceRunning(port: localPort)
)
guard plan == .stop else {
self.finishQuit()
return
}

self.setTransientStatus(loc("status.stopping"))
let result = await self.runCLIAndWait("stop", port: localPort)
if result.exitCode != 0 {
self.isQuitting = false
let alert = NSAlert()
Expand All @@ -1186,21 +1208,19 @@ class MenuBarController: NSObject, NSMenuDelegate {
: result.output
alert.alertStyle = .warning
alert.addButton(withTitle: loc("common.close"))
alert.runModal()
ForegroundAlertPresentation.run(alert)
NSLog("[LLMProxy] ❌ 后台服务停止失败 (exit=\(result.exitCode)): \(result.output)")
return
}

await self.waitForStoppedAndRefresh()
let stillRunning = (try? await self.client.fetchHealth()) == true
if stillRunning {
if !(await self.waitForLocalServiceStopped(port: localPort)) {
self.isQuitting = false
let alert = NSAlert()
alert.messageText = loc("quit.serviceStopFailed.title")
alert.informativeText = loc("quit.serviceStopFailed.body")
alert.alertStyle = .warning
alert.addButton(withTitle: loc("common.close"))
alert.runModal()
ForegroundAlertPresentation.run(alert)
NSLog("[LLMProxy] ❌ 停止命令完成后服务仍可访问,拒绝退出菜单栏")
return
}
Expand Down Expand Up @@ -1283,7 +1303,7 @@ class MenuBarController: NSObject, NSMenuDelegate {
alert.accessoryView = input
input.becomeFirstResponder()

let response = alert.runModal()
let response = ForegroundAlertPresentation.run(alert)
if response == .alertFirstButtonReturn {
let portStr = input.stringValue.trimmingCharacters(in: .whitespaces)
guard let port = Int(portStr), port >= 1, port <= 65535 else {
Expand Down Expand Up @@ -1397,7 +1417,7 @@ class MenuBarController: NSObject, NSMenuDelegate {
alert.informativeText = loc("update.downloadConfirm", update.version)
alert.addButton(withTitle: loc("action.download"))
alert.addButton(withTitle: "Cancel")
if alert.runModal() == .alertFirstButtonReturn {
if ForegroundAlertPresentation.run(alert) == .alertFirstButtonReturn {
await performDownloadAndInstall(update)
} else {
// 用户点了 Cancel,24 小时内不再提醒
Expand All @@ -1413,7 +1433,7 @@ class MenuBarController: NSObject, NSMenuDelegate {
alert.messageText = loc("app.title")
alert.informativeText = loc("update.noUpdates", currentVersion())
alert.addButton(withTitle: "OK")
alert.runModal()
ForegroundAlertPresentation.run(alert)
}
}
} catch {
Expand All @@ -1425,7 +1445,7 @@ class MenuBarController: NSObject, NSMenuDelegate {
alert.messageText = loc("app.title")
alert.informativeText = loc("update.checkFailed", error.localizedDescription)
alert.addButton(withTitle: "OK")
alert.runModal()
ForegroundAlertPresentation.run(alert)
}
}
}
Expand Down Expand Up @@ -1502,7 +1522,7 @@ class MenuBarController: NSObject, NSMenuDelegate {
alert.informativeText = loc("update.installPrompt", update.version)
alert.addButton(withTitle: loc("action.install"))
alert.addButton(withTitle: loc("action.later24h"))
if alert.runModal() == .alertFirstButtonReturn {
if ForegroundAlertPresentation.run(alert) == .alertFirstButtonReturn {
await performInstall(localURL, version: update.version)
} else {
// 用户选了 Later,24 小时内不再提醒
Expand All @@ -1519,7 +1539,7 @@ class MenuBarController: NSObject, NSMenuDelegate {
alert.informativeText = loc("update.downloadFailed", error.localizedDescription)
alert.addButton(withTitle: loc("action.retry"))
alert.addButton(withTitle: "Cancel")
if alert.runModal() == .alertFirstButtonReturn {
if ForegroundAlertPresentation.run(alert) == .alertFirstButtonReturn {
// 重试
await performDownloadAndInstall(update)
}
Expand All @@ -1534,24 +1554,29 @@ class MenuBarController: NSObject, NSMenuDelegate {
// 更新 App 只停止本机代理。当前管理连接可能指向远程服务,因此端口
// 和停止确认都必须使用独立的 loopback 路径,不能读取 client.baseURL。
let localPort = localServiceControlPort
let stopResult = await runCLIAndWait("stop", port: localPort)
guard stopResult.exitCode == 0 else {
let alert = NSAlert()
alert.messageText = loc("app.title")
alert.informativeText = stopResult.output.isEmpty
? loc("quit.serviceStopFailed.body")
: stopResult.output
alert.addButton(withTitle: loc("common.close"))
alert.runModal()
return
}
if !(await waitForLocalServiceStopped(port: localPort)) {
let alert = NSAlert()
alert.messageText = loc("app.title")
alert.informativeText = loc("quit.serviceStopFailed.body")
alert.addButton(withTitle: loc("common.close"))
alert.runModal()
return
let plan = LocalServiceControl.shutdownPlan(
isLocalServiceRunning: await isLocalServiceRunning(port: localPort)
)
if plan == .stop {
let stopResult = await runCLIAndWait("stop", port: localPort)
guard stopResult.exitCode == 0 else {
let alert = NSAlert()
alert.messageText = loc("app.title")
alert.informativeText = stopResult.output.isEmpty
? loc("quit.serviceStopFailed.body")
: stopResult.output
alert.addButton(withTitle: loc("common.close"))
ForegroundAlertPresentation.run(alert)
return
}
if !(await waitForLocalServiceStopped(port: localPort)) {
let alert = NSAlert()
alert.messageText = loc("app.title")
alert.informativeText = loc("quit.serviceStopFailed.body")
alert.addButton(withTitle: loc("common.close"))
ForegroundAlertPresentation.run(alert)
return
}
}

do {
Expand All @@ -1561,7 +1586,7 @@ class MenuBarController: NSObject, NSMenuDelegate {
alert.messageText = loc("app.title")
alert.informativeText = loc("update.installFailed", error.localizedDescription)
alert.addButton(withTitle: "OK")
alert.runModal()
ForegroundAlertPresentation.run(alert)
return
}

Expand Down Expand Up @@ -1596,11 +1621,12 @@ class MenuBarController: NSObject, NSMenuDelegate {



@MainActor
func showError(_ msg: String) {
let alert = NSAlert()
alert.messageText = loc("app.title")
alert.informativeText = msg
alert.runModal()
ForegroundAlertPresentation.run(alert)
}
}

Expand Down
19 changes: 19 additions & 0 deletions app/Tests/LLMProxyTests/MenuBarPreviewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,23 @@ final class MenuBarPreviewTests: XCTestCase {
"http://127.0.0.1:9123/admin/health"
)
}

func testLocalServiceShutdownDependsOnLocalProcessInsteadOfManagementMode() {
XCTAssertEqual(
LocalServiceControl.shutdownPlan(isLocalServiceRunning: true),
.stop
)
XCTAssertEqual(
LocalServiceControl.shutdownPlan(isLocalServiceRunning: false),
.alreadyStopped
)
}

func testForegroundAlertsUseModalLevelAndMoveToActiveSpace() {
let alert = NSAlert()
ForegroundAlertPresentation.configure(alert)

XCTAssertEqual(alert.window.level, .modalPanel)
XCTAssertTrue(alert.window.collectionBehavior.contains(.moveToActiveSpace))
}
}
Loading