// // MentorPaneView.swift // Museion // // M0 ACP bridge (#14): the debug pane — the M0 demo surface. Post-gate main // surface replacing the content placeholder (map decision: pane placement). // import SwiftUI /// The mentor debug pane: transcript, freeform input, Stop while streaming, /// restart prompt on crash. Post-gate main surface (map decision). struct MentorPaneView: View { @State private var draft = "" @State private var model: MentorSessionModel /// Set post-gate by ContentView: the probe's winning binary (no re-probe). var binaryPath: String? init(model: MentorSessionModel, binaryPath: String?) { _model = State(initialValue: model) self.binaryPath = binaryPath } var body: some View { VStack(spacing: 0) { header Divider() switch model.phase { case .idle, .starting: ProgressView("Starting the mentor engine…") .frame(maxWidth: .infinity, maxHeight: .infinity) case .ready: transcript Divider() inputBar case let .crashed(lastPrompt): crashCard(lastPrompt: lastPrompt) } } .frame(minWidth: 560, minHeight: 420) .task { model.binaryPath = binaryPath ?? model.binaryPath // Hosted unit tests run this app as their runner — never spawn a // real engine child there (it outlives the test session and kills // the runner). guard ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] == nil else { return } await model.openSession() } } // MARK: Header private var header: some View { HStack(spacing: 8) { Image(systemName: "bubble.left.and.text.bubble.right") .foregroundStyle(.secondary) Text("Mentor Session") .font(.headline) if let version = model.engineVersion { Text(version) .font(.caption) .foregroundStyle(.tertiary) } if let note = model.versionNote { Text(note) .font(.caption) .foregroundStyle(.orange) .help(note) } Spacer() if case let .ready(sessionId) = model.phase { Text("session \(sessionId.prefix(8))") .font(.caption2) .foregroundStyle(.tertiary) .textSelection(.enabled) } } .padding(.horizontal, 12) .padding(.vertical, 8) } // MARK: Transcript private var transcript: some View { ScrollViewReader { proxy in ScrollView { LazyVStack(alignment: .leading, spacing: 10) { ForEach(model.rows) { row in rowView(row) .id(row.id) } if !model.streamingText.isEmpty { textRow("Mentor: \(model.streamingText)") .id("streaming") } } .padding(12) .frame(maxWidth: .infinity, alignment: .leading) } .onChange(of: model.rows.count) { scrollToBottom(proxy) } .onChange(of: model.streamingText) { scrollToBottom(proxy) } } } private func scrollToBottom(_ proxy: ScrollViewProxy) { if let last = model.rows.last?.id { proxy.scrollTo(last, anchor: .bottom) } else { proxy.scrollTo("streaming", anchor: .bottom) } } @ViewBuilder private func rowView(_ row: SessionRow) -> some View { switch row.content { case let .text(text): textRow(text) case let .fixCard(card): FixCardRow(card: card) case let .raw(kind, json): RawUpdateRow(kind: kind, json: json) } } private func textRow(_ text: String) -> some View { Text(text) .textSelection(.enabled) .frame(maxWidth: .infinity, alignment: .leading) } // MARK: Input private var inputBar: some View { HStack(spacing: 8) { TextField("Ask the mentor…", text: $draft, axis: .vertical) .textFieldStyle(.plain) .lineLimit(1 ... 4) .onSubmit { send() } Button { send() } label: { Image(systemName: "arrow.up.circle.fill") .font(.title2) } .buttonStyle(.plain) .disabled(draft.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty || model.isStreaming) if model.isStreaming { Button { model.stopStreaming() } label: { Text("Stop") } .controlSize(.regular) } } .padding(12) .disabled(model.phase.isReady == false) } private func send() { let text = draft draft = "" Task { await model.sendPrompt(text) } } // MARK: Crash → restart prompt (map decision: EOF/crash surfaces an // in-pane Restart prompt, never data loss) private func crashCard(lastPrompt: String?) -> some View { VStack(spacing: 14) { Image(systemName: "exclamationmark.triangle.fill") .font(.system(size: 30)) .foregroundStyle(.orange) Text("The mentor engine stopped") .font(.title3) Text("Nothing is lost — the transcript stays, and the engine can be restarted.") .font(.callout) .foregroundStyle(.secondary) .multilineTextAlignment(.center) if let lastPrompt, !lastPrompt.isEmpty { Text("Last prompt: \(lastPrompt)") .font(.caption) .foregroundStyle(.tertiary) .lineLimit(2) } Button("Restart") { Task { await model.restart() } } .buttonStyle(.borderedProminent) if !model.stderrTail.isEmpty { DisclosureGroup("Technical details") { Text(model.stderrTail.joined(separator: "\n")) .font(.system(size: 10, design: .monospaced)) .foregroundStyle(.secondary) .textSelection(.enabled) .frame(maxWidth: .infinity, alignment: .leading) } .font(.system(size: 11)) .frame(maxWidth: .infinity, alignment: .leading) } } .padding(24) .frame(maxWidth: .infinity, maxHeight: .infinity) } // MARK: Rows private struct FixCardRow: View { let card: FixCard var body: some View { VStack(alignment: .leading, spacing: 6) { Label(card.pattern, systemImage: "rectangle.and.pencil.and.ellipsis") .font(.callout.weight(.semibold)) if !card.anchor.isEmpty { Text("“\(card.anchor)”") .font(.system(size: 11, design: .monospaced)) .foregroundStyle(.secondary) } if !card.problem.isEmpty { Text(card.problem).font(.callout) } if !card.diagnosticQuestion.isEmpty { Text("Diagnostic: \(card.diagnosticQuestion)") .font(.callout) .foregroundStyle(.secondary) } if !card.fix.isEmpty { Text("Fix: \(card.fix)").font(.callout) } Text(card.provenance + " · " + card.status) .font(.caption2) .foregroundStyle(.tertiary) } .padding(10) .frame(maxWidth: .infinity, alignment: .leading) .background(.quaternary.opacity(0.3), in: RoundedRectangle(cornerRadius: 8)) } } private struct RawUpdateRow: View { let kind: String let json: String var body: some View { DisclosureGroup { Text(json) .font(.system(size: 10, design: .monospaced)) .textSelection(.enabled) } label: { Text(kind) .font(.system(size: 11, design: .monospaced)) .foregroundStyle(.secondary) } .frame(maxWidth: .infinity, alignment: .leading) } } } #Preview { MentorPaneView(model: MentorSessionModel(), binaryPath: nil) }