interplane.Get Omega
Read in your language

hop

Hop: a small native macOS launcher. Clipboard history, find, on-device dictation and your coding agent behind one key.

Brought by drakestapleton

branch main Branches Tags History

hop / Sources / FactoryWorkspaceView.swift

212 lines · 13039 bytes Raw Blame History
1import AppKit
2import SwiftUI
3
4// AppKit/SwiftUI host surface for the seeded Omega factory core, not a second launcher.
5@MainActor
6struct FactoryWorkspaceView: View {
7 @ObservedObject var m: AppModel
8 @StateObject private var board = FactoryWorkspace()
9 @StateObject private var swarm = SwarmStore()
10 @State private var section = "Stations"
11 @State private var selectedStation: String?
12 @State private var selectedGroup: String?
13 @State private var selectedAgent: String?
14 @State private var detail: FactoryBoardJob?
15 @State private var drafts: [String: String] = [:]
16
17 var body: some View {
18 VStack(spacing: 0) {
19 HStack(spacing: 8) {
20 Image(systemName: board.connected ? "bolt.horizontal.circle" : "network.slash")
21 .accessibilityLabel(board.connected ? "Factory connection active" : "Factory connection unavailable")
22 Text(board.note).font(.callout).lineLimit(2)
23 Spacer()
24 if let date = board.projection?.generated {
25 Text(date, style: .time).font(.caption.monospacedDigit()).foregroundStyle(.secondary)
26 .help("Last source update, not a task-completion timestamp")
27 }
28 Button(board.connected ? "Reconnect" : "Connect") { board.refresh(m.config.factoryURL) }
29 .accessibilityIdentifier("factory.connect")
30 }.padding(.horizontal, 14).padding(.vertical, 9)
31 Picker("Factory section", selection: $section) {
32 ForEach(["Stations", "Queues", "Agents", "Swarm", "Chat"], id: \.self) { Text($0) }
33 }.pickerStyle(.segmented).padding(.horizontal, 14).padding(.bottom, 8)
34 Divider()
35 Group {
36 switch section {
37 case "Queues": queuePane
38 case "Agents": agentPane
39 case "Swarm": SwarmSectionView(store: swarm, config: m.config)
40 case "Chat":
41 TranscriptView(log: m.factoryLog, empty: "Use the search field above to ask the dispatcher. To address an agent directly without a routing-model call, use Agents.",
42 running: m.factoryRunning, stop: m.stopFactory, clear: { m.factoryLog.removeAll() })
43 default: stationPane
44 }
45 }.frame(maxWidth: .infinity, maxHeight: .infinity)
46 Divider()
47 HStack {
48 Text(section == "Swarm" ? "I only read the swarm's board. I never post to it."
49 : "Omega classifies factory state. Delivery is not completion.")
50 .font(.caption2).foregroundStyle(.secondary)
51 Spacer()
52 ForEach(m.config.factoryActions) { action in Button(action.name) { m.runAction(action); section = "Chat" } }
53 }.controlSize(.small).padding(.horizontal, 14).padding(.vertical, 8)
54 }
55 .onAppear { board.connect(m.config.factoryURL) }
56 // A plain-English request typed in the search field answers in Chat: show it there.
57 .onChange(of: m.factoryLog.count) { old, new in if new > old { section = "Chat" } }
58 .onDisappear { board.disconnect() }
59 .onReceive(NotificationCenter.default.publisher(for: NSWindow.didBecomeKeyNotification)) { notification in
60 if notification.object is HopPanel { board.connect(m.config.factoryURL) }
61 }
62 .onReceive(NotificationCenter.default.publisher(for: NSWindow.didResignKeyNotification)) { notification in
63 if let panel = notification.object as? HopPanel, NSApp.modalWindow == nil, panel.attachedSheet == nil { board.disconnect() }
64 }
65 .sheet(item: $detail) { job in
66 VStack(alignment: .leading, spacing: 12) {
67 Text(job.title).font(.headline).textSelection(.enabled)
68 if let head = job.head {
69 Text(head).font(.system(.body, design: .monospaced)).textSelection(.enabled)
70 Button("Copy revision") { NSPasteboard.general.clearContents(); NSPasteboard.general.setString(head, forType: .string) }
71 }
72 if let crew = job.crew { Text("Owner: \(crew)").foregroundStyle(.secondary) }
73 Text("This is the factory's reported item, not an approval. Review and merge gates still apply.")
74 .font(.callout).foregroundStyle(.secondary)
75 HStack {
76 Button("Prepare question") {
77 m.query = "Explain the current state and required evidence for \(job.title)\(job.head.map { " at " + $0 } ?? "")."
78 section = "Chat"; detail = nil
79 }.help("Fills the query field only. Press Return yourself to send.")
80 Spacer()
81 Button("Close") { detail = nil }.keyboardShortcut(.cancelAction)
82 }
83 }.padding(20).frame(width: 520)
84 }
85 }
86
87 private var stationPane: some View {
88 HSplitView {
89 List(board.projection?.stations ?? [], selection: $selectedStation) { station in
90 VStack(alignment: .leading, spacing: 3) {
91 Text(station.name).font(.callout.weight(.medium))
92 Text(station.status.isEmpty ? "State not supplied" : station.status)
93 .font(.caption).foregroundStyle(.secondary)
94 }.tag(station.id).padding(.vertical, 3)
95 }.listStyle(.sidebar).frame(minWidth: 190, idealWidth: 240, maxWidth: 300)
96 if let station = board.projection?.stations.first(where: { $0.id == selectedStation }) {
97 ScrollView {
98 VStack(alignment: .leading, spacing: 10) {
99 Text(station.name).font(.headline)
100 Text(station.about).font(.callout).foregroundStyle(.secondary)
101 HStack(spacing: 20) {
102 stat("Open", station.open); stat("Moving", station.moving); stat("Done in 24h", station.done)
103 }
104 Divider()
105 Text("Reported jobs").font(.subheadline.weight(.semibold))
106 if station.jobs.isEmpty { Text("No job details supplied.").foregroundStyle(.secondary) }
107 ForEach(station.jobs) { job in jobButton(job) }
108 }.padding(14).frame(maxWidth: .infinity, alignment: .leading)
109 }.frame(minWidth: 280)
110 } else { placeholder("Select a station", "Live station data arrives through one event stream while this view is open.") }
111 }
112 }
113 private var queuePane: some View {
114 HSplitView {
115 List(board.projection?.groups ?? [], selection: $selectedGroup) { group in
116 HStack {
117 Text(group.label).lineLimit(2)
118 Spacer()
119 Text(group.jobs.count.formatted()).monospacedDigit().foregroundStyle(.secondary)
120 }.tag(group.id).padding(.vertical, 3)
121 }.listStyle(.sidebar).frame(minWidth: 210, idealWidth: 260, maxWidth: 320)
122 if let group = board.projection?.groups.first(where: { $0.id == selectedGroup }) {
123 ScrollView {
124 VStack(alignment: .leading, spacing: 10) {
125 Text(group.label).font(.headline)
126 Text("Ordered by the Omega core. Missing groups are unavailable, not zero.")
127 .font(.caption).foregroundStyle(.secondary)
128 if group.jobs.isEmpty { Text("No items reported in this group.").foregroundStyle(.secondary) }
129 ForEach(group.jobs) { job in jobButton(job) }
130 }.padding(14).frame(maxWidth: .infinity, alignment: .leading)
131 }.frame(minWidth: 280)
132 } else { placeholder("Select a queue", "Inspect the exact revision and owner without opening a browser. A held item is not automatically a request for your approval.") }
133 }
134 }
135 private var agentPane: some View {
136 HSplitView {
137 List(board.projection?.agents ?? [], selection: $selectedAgent) { agent in
138 VStack(alignment: .leading, spacing: 3) {
139 Text(agent.name).font(.callout.weight(.medium))
140 Text(agent.station).font(.caption).foregroundStyle(.secondary)
141 }.tag(agent.id).padding(.vertical, 3)
142 }.listStyle(.sidebar).frame(minWidth: 190, idealWidth: 240, maxWidth: 300)
143 if let agent = board.projection?.agents.first(where: { $0.id == selectedAgent }) {
144 VStack(alignment: .leading, spacing: 8) {
145 HStack {
146 VStack(alignment: .leading, spacing: 3) {
147 Text(agent.name).font(.headline)
148 Text(agent.id).font(.caption.monospaced()).textSelection(.enabled).foregroundStyle(.secondary)
149 }
150 Spacer()
151 if board.screenLoading { ProgressView().controlSize(.small) }
152 Button("Read screen") { board.inspect(agent) }.disabled(board.screenLoading || !board.connected)
153 .accessibilityIdentifier("factory.screen")
154 }
155 ScrollView {
156 Text(board.screen.isEmpty ? "Read the agent's screen on demand. Nothing is polled." : board.screen)
157 .font(.system(.caption, design: .monospaced)).textSelection(.enabled)
158 .frame(maxWidth: .infinity, alignment: .leading)
159 }.frame(maxHeight: .infinity)
160 Divider()
161 TextField("One-line instruction to this agent", text: Binding(
162 get: { drafts[agent.id] ?? "" }, set: { drafts[agent.id] = $0 }))
163 .textFieldStyle(.roundedBorder).accessibilityIdentifier("factory.message")
164 HStack {
165 Text("Direct route. No dispatcher-model call.").font(.caption2).foregroundStyle(.secondary)
166 Spacer()
167 Button("Review send…") { confirmSend(agent) }
168 .disabled(board.sending || !board.connected || (drafts[agent.id] ?? "").trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
169 .accessibilityIdentifier("factory.send")
170 }
171 if !board.sendNote.isEmpty { Text(board.sendNote).font(.caption).textSelection(.enabled) }
172 }.padding(14).frame(minWidth: 300)
173 .onChange(of: selectedAgent) { _, _ in board.screen = ""; board.sendNote = "" }
174 } else { placeholder("Select an agent", "Read its screen or send an explicit instruction to that exact session. No automatic retries or hidden conversations.") }
175 }
176 }
177 private func confirmSend(_ agent: FactoryBoardAgent) {
178 guard let state = board.projection, FactoryCoreState.evaluate(state.input).permitsFreshRead else {
179 board.sendNote = "Factory data is not current. Reconnect before sending."; return
180 }
181 let text = drafts[agent.id] ?? ""
182 let alert = NSAlert()
183 alert.messageText = "Send to \(agent.name)?"
184 alert.informativeText = "Session: \(agent.id)\n\n\(text)\n\nThis sends an instruction to the current agent in that session. It does not approve a merge or prove completion."
185 alert.addButton(withTitle: "Send instruction"); alert.addButton(withTitle: "Cancel")
186 guard alert.runModal() == .alertFirstButtonReturn else { return }
187 Task {
188 if await board.send(text, to: agent, confirmed: true), drafts[agent.id] == text { drafts[agent.id] = "" }
189 }
190 }
191 private func stat(_ title: String, _ value: Int?) -> some View {
192 VStack(alignment: .leading, spacing: 2) {
193 Text(value.map(String.init) ?? "Unknown").font(.title3.monospacedDigit())
194 Text(title).font(.caption).foregroundStyle(.secondary)
195 }
196 }
197 private func jobButton(_ job: FactoryBoardJob) -> some View {
198 Button { detail = job } label: {
199 VStack(alignment: .leading, spacing: 3) {
200 Text(job.title).lineLimit(2)
201 if let head = job.head { Text(String(head.prefix(12))).font(.caption.monospaced()).foregroundStyle(.secondary) }
202 }.frame(maxWidth: .infinity, alignment: .leading).padding(.vertical, 5)
203 }.buttonStyle(.plain)
204 }
205 private func placeholder(_ title: String, _ subtitle: String) -> some View {
206 VStack(alignment: .leading, spacing: 8) {
207 Text(title).font(.headline)
208 Text(subtitle).font(.callout).foregroundStyle(.secondary)
209 Spacer()
210 }.padding(18).frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
211 }
212}