Skip to content

Commit

Permalink
Fix server isOn bug, update Server Manager view
Browse files Browse the repository at this point in the history
commit 5fd5141
Author: Ronald Mannak <[email protected]>
Date:   Thu Mar 21 22:25:53 2024 -0700

    stop all servers: Update servers status

    Use Table in ServerListView

    Fix table update and server duplicate bug

commit 53296d6
Author: Ronald Mannak <[email protected]>
Date:   Thu Mar 21 22:06:23 2024 -0700

    Add Show Cache… option

commit 8c6de30
Author: Ronald Mannak <[email protected]>
Date:   Thu Mar 21 21:32:31 2024 -0700

    Fix SwiftUI sync issue in SwiftUI views

commit 03db2d6
Author: Ronald Mannak <[email protected]>
Date:   Thu Mar 21 16:52:42 2024 -0700

    Update menu structure and settings view
  • Loading branch information
ronaldmannak committed Mar 25, 2024
1 parent 1f07bda commit e5f1928
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 82 deletions.
4 changes: 0 additions & 4 deletions PicoMLXServer.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
F818D97D2B96A7B400BA4664 /* MenuExtra.swift in Sources */ = {isa = PBXBuildFile; fileRef = F818D97C2B96A7B400BA4664 /* MenuExtra.swift */; };
F818D97F2B96A80500BA4664 /* SettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F818D97E2B96A80500BA4664 /* SettingsView.swift */; };
F846FDC92BACBFB5005EE38A /* Bundle.swift in Sources */ = {isa = PBXBuildFile; fileRef = F846FDC82BACBFB5005EE38A /* Bundle.swift */; };
F84D2A6A2BAA4C5900FB2D41 /* ServerCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F84D2A692BAA4C5900FB2D41 /* ServerCellView.swift */; };
F861C3842BAA67E4004BBB86 /* ServerLogView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F861C3832BAA67E4004BBB86 /* ServerLogView.swift */; };
F88F11102BA92D5B004743A5 /* ServerManagerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = F88F110F2BA92D5B004743A5 /* ServerManagerView.swift */; };
F88F11122BA92D75004743A5 /* ServerListMenu.swift in Sources */ = {isa = PBXBuildFile; fileRef = F88F11112BA92D75004743A5 /* ServerListMenu.swift */; };
Expand Down Expand Up @@ -99,7 +98,6 @@
F818D97C2B96A7B400BA4664 /* MenuExtra.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MenuExtra.swift; sourceTree = "<group>"; };
F818D97E2B96A80500BA4664 /* SettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SettingsView.swift; sourceTree = "<group>"; };
F846FDC82BACBFB5005EE38A /* Bundle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Bundle.swift; sourceTree = "<group>"; };
F84D2A692BAA4C5900FB2D41 /* ServerCellView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerCellView.swift; sourceTree = "<group>"; };
F861C3832BAA67E4004BBB86 /* ServerLogView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerLogView.swift; sourceTree = "<group>"; };
F88F110F2BA92D5B004743A5 /* ServerManagerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerManagerView.swift; sourceTree = "<group>"; };
F88F11112BA92D75004743A5 /* ServerListMenu.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ServerListMenu.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -282,7 +280,6 @@
F88F110E2BA92CEC004743A5 /* Models */,
F88F110F2BA92D5B004743A5 /* ServerManagerView.swift */,
F8907A3D2BA8D83700246735 /* ServerListView.swift */,
F84D2A692BAA4C5900FB2D41 /* ServerCellView.swift */,
F861C3832BAA67E4004BBB86 /* ServerLogView.swift */,
);
path = "Server Manager";
Expand Down Expand Up @@ -453,7 +450,6 @@
F8907A3E2BA8D83700246735 /* ServerListView.swift in Sources */,
F8145FF12BA226170086CBE7 /* NSApplication.swift in Sources */,
F8145FD82BA16DD90086CBE7 /* String.swift in Sources */,
F84D2A6A2BAA4C5900FB2D41 /* ServerCellView.swift in Sources */,
F81460002BA283290086CBE7 /* DependencyViewModel+Operations.swift in Sources */,
F88F11102BA92D5B004743A5 /* ServerManagerView.swift in Sources */,
F861C3842BAA67E4004BBB86 /* ServerLogView.swift in Sources */,
Expand Down
5 changes: 5 additions & 0 deletions PicoMLXServer/Menu/MenuExtra.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ struct MenuExtra: View {

ServerListMenu()

Button("Manage Servers...") {
NSApplication.show()
openWindow(id: "servers")
}

Button("Open Chat client") {
// TODO: make this a setting in Settings
// Can we ad an optional link to the app store if app isn't installed?
Expand Down
19 changes: 3 additions & 16 deletions PicoMLXServer/Menu/ServerListMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,9 @@ struct ServerListMenu: View {
.disabled(true)
} else {
ForEach(serverController.servers, id: \.self) { server in
MenuToggle(server: server, isOn: server.isOn)
MenuToggle(server: server)
}
}

Divider()

Button("New Server...") {
NSApplication.show()
openWindow(id: "servers")
}

Button("Show cache...") {
let url = FileManager.default.homeDirectoryForCurrentUser.appending(path: ".cache/huggingface/hub/")
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: url.path)
}
.controlSize(.small)
}
}
}
Expand All @@ -46,10 +33,10 @@ struct ServerListMenu: View {
.environment(ServerController())
}

struct MenuToggle: View {
/// This is a workaround for an issue where an inline toggle in the menu wouldn't update
fileprivate struct MenuToggle: View {

let server: Server
@State var isOn: Bool

var body: some View {
@Bindable var server = server
Expand Down
5 changes: 2 additions & 3 deletions PicoMLXServer/Server Manager/Models/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,11 @@ final class Server: Identifiable {
let operation = try serverOperation()
Queue.shared.serverQueue.addOperation(operation)
self.operation = operation
isOn = true
}
}
}

private func stop() {
operation?.cancel()
isOn = false
}

/// Creates a server operation
Expand All @@ -68,6 +66,7 @@ final class Server: Identifiable {
operation.outputClosure = { [weak self] in self?.log.append($0) }
operation.completionBlock = { [self] in
self.log.append("\nServer \(self.model) on port \(self.port) terminated")
if self.isOn == true { self.isOn = false }
}
return operation
}
Expand Down
7 changes: 4 additions & 3 deletions PicoMLXServer/Server Manager/Models/ServerController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ final class ServerController {

var servers = [Server]()

func addServer(model: String = "mlx-community/Nous-Hermes-2-Mistral-7B-DPO-4bit-MLX", port: Int = 8080) throws {

func addServer(model: String = "mlx-community/Nous-Hermes-2-Mistral-7B-DPO-4bit-MLX", port: Int = 8080) throws {
let server = Server(model: model, port: port)
servers.append(server)
server.isOn = true
}

func stopAllServers() {
for server in servers {
server.isOn = false
}
Queue.shared.cancelAllOperations()
}
}
43 changes: 0 additions & 43 deletions PicoMLXServer/Server Manager/ServerCellView.swift

This file was deleted.

69 changes: 60 additions & 9 deletions PicoMLXServer/Server Manager/ServerListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,74 @@ struct ServerListView: View {

@Binding var showError: Bool
@Binding var error: Error?

var body: some View {
GroupBox {
ScrollView() {
Grid(alignment: .leading, horizontalSpacing: 10, verticalSpacing: 10) {
ForEach(serverController.servers, id: \.self) { server in
ServerCellView(server: server, showError: $showError, error: $error)
Divider()
}

GroupBox {
Table(serverController.servers) {

TableColumn("Model") { server in
ModelView(server: server)
}

TableColumn("Port") { Text($0.port, format: .number.grouping(.never))}
.width(min: 30, ideal: 50, max: 100)

TableColumn("Status") { server in
StatusView(server: server)
}
.width(min: 60, ideal: 60, max: 100)

TableColumn("Action") { server in
OnOffButton(server: server)
}
.width(min: 40, ideal: 40, max: 100)

TableColumn("Logs") { server in
Button("View") {
openWindow(id: "serverLog", value: server.id)
}
.buttonStyle(.bordered)
.controlSize(.small)
}
.frame(maxWidth: .infinity)
.width(min: 40, ideal: 40, max: 100)
}
.tableStyle(.bordered)
} label: {
Label("Servers", systemImage: "server.rack")
}
}
}

/// This is a workaround for an issue where an inline button in the Table wouldn't update
/// https://forums.swift.org/t/why-swiftui-table-doesn-t-track-changes-in-observable-object-is-there-are-bug-in-swiftui/70415
fileprivate struct OnOffButton: View {
var server: Server
var body: some View {
Button(server.isOn ? "Stop" : "Start") {
server.isOn.toggle()
}
.buttonStyle(.bordered)
.controlSize(.small)
}
}

fileprivate struct StatusView: View {
var server: Server
var body: some View {
Text(server.isOn ? "Running" : "Stopped")
}
}

fileprivate struct ModelView: View {
var server: Server
var body: some View {
Text(server.model)
.fontWeight(.bold)
.truncationMode(.head)
.foregroundStyle(server.isOn ? .primary : .secondary)
}
}

#Preview {
struct Preview: View {
Expand Down
13 changes: 12 additions & 1 deletion PicoMLXServer/Server Manager/ServerManagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,19 @@ struct ServerManagerView: View {
VStack {

ServerListView(showError: $showError, error: $error)
.padding()
.padding([.leading, .top, .trailing])
.frame(maxWidth: .infinity)

HStack {
Button("Open model cache in Finder") {
let url = FileManager.default.homeDirectoryForCurrentUser.appending(path: ".cache/huggingface/hub/")
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: url.path)
}
.buttonStyle(.bordered)
.controlSize(.small)
.padding(.leading)
Spacer()
}

GroupBox {
HStack {
Expand Down
2 changes: 1 addition & 1 deletion PicoMLXServer/Setup/Models/Dependencies.plist
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@
</dict>
<dict>
<key>name</key>
<string>MLX using pip</string>
<string>MLX using pip (optional)</string>
<key>projectUrl</key>
<string>https://github.com/ml-explore</string>
<key>documentationUrl</key>
Expand Down
3 changes: 1 addition & 2 deletions PicoMLXServer/Setup/SetupView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ struct SetupView: View {
}
} catch {
self.error = error
showError = true

showError = true
}
}
.disabled(dependency.state.disableButton)
Expand Down

0 comments on commit e5f1928

Please sign in to comment.