SPB Git

Repository Public License

NotesPanel.swift 13.3 KB 248 lines swift

1//2//  ORIX Audio3//  File: ORIXAudio/Features/Detail/NotesPanel.swift4//  Description: Meeting intelligence: grounded summary, key points, action items, decisions, chapters and questions, with sources and editing.5//6//  Author: Simon-Pierre Boucher7//  Contact: contact@spboucher.ai8//9import ORIXAudioCore10import SwiftUI1112struct NotesPanel: View {13    @Environment(AppModel.self) private var model14    let store: DetailStore15    let onSeek: (Double) -> Void16    @State private var question = ""17    @State private var answer: TranscriptAnswer?18    @State private var asking = false19    @State private var askError: String?20    @State private var editingSummary = false21    @State private var summaryDraft = ""22    @State private var editingItem: GroundedItem?23    @State private var itemDraft = ""2425    private var recording: Recording { store.recording }26    private var availability: IntelligenceAvailability { MeetingIntelligence.availability }2728    var body: some View {29        VStack(spacing: ORIX.Space.m) {30            if availability != .available {31                ORIXCard {32                    VStack(alignment: .leading, spacing: 8) {33                        Label("Summaries aren't available on this device", systemImage: "sparkles.slash").font(.subheadline.weight(.semibold)).foregroundStyle(ORIX.Palette.warning)34                        Text(CoreText.explain(availability)).font(.footnote).foregroundStyle(ORIX.Palette.textSecondary)35                    }36                }37            }38            if store.transcript == nil {39                ORIXCard { EmptyStateView(symbol: "text.quote", title: "Transcript needed", message: "Notes are written from the transcript. Transcribe this recording first.") }40            } else if let job = model.processing.job(recording.id, .notes) {41                ORIXCard { ProgressRow(title: "Writing notes on this device…", progress: job.progress) { model.processing.cancel(recording.id, .notes) } }42            } else if store.notes == nil, availability == .available {43                ORIXCard {44                    VStack(alignment: .leading, spacing: 10) {45                        SectionTitle("Notes", symbol: "sparkles")46                        Text("Creates a summary, key points, action items and decisions from the transcript, on this device. Each point links to the line it came from.")47                            .font(.footnote).foregroundStyle(ORIX.Palette.textSecondary)48                        if let error = model.processing.errors[recording.id] { Text(error).font(.footnote).foregroundStyle(ORIX.Palette.record) }49                        Button("Generate notes", systemImage: "sparkles") { model.processing.summarizeInBackground(recording) }.buttonStyle(.borderedProminent)50                            .accessibilityIdentifier("generateNotesButton")51                    }52                }53            }54            if let notes = store.notes { notesView(notes) }55            if store.transcript != nil, availability == .available { questionCard }56        }57        .alert("Edit summary", isPresented: $editingSummary) {58            TextField("Summary", text: $summaryDraft, axis: .vertical)59            Button("Save") { if var n = store.notes { n.summary = summaryDraft; save(n) } }60            Button("Cancel", role: .cancel) {}61        }62        .alert("Edit", isPresented: Binding(get: { editingItem != nil }, set: { if !$0 { editingItem = nil } })) {63            TextField("Text", text: $itemDraft, axis: .vertical)64            Button("Save") { saveItem() }65            Button("Delete", role: .destructive) { deleteItem() }66            Button("Cancel", role: .cancel) { editingItem = nil }67        }68    }6970    private func save(_ notes: MeetingNotes) {71        var n = notes; n.isEdited = true72        store.notes = n73        model.processing.saveNotes(n, for: recording)74    }7576    private func mutateItems(_ body: (inout [GroundedItem]) -> Void) {77        guard var n = store.notes else { return }78        body(&n.keyPoints); body(&n.actionItems); body(&n.decisions)79        save(n)80    }81    private func saveItem() {82        guard let item = editingItem else { return }83        let text = itemDraft.trimmingCharacters(in: .whitespacesAndNewlines)84        if !text.isEmpty { mutateItems { list in if let i = list.firstIndex(where: { $0.id == item.id }) { list[i].text = text } } }85        editingItem = nil86    }87    private func deleteItem() {88        guard let item = editingItem else { return }89        mutateItems { $0.removeAll { $0.id == item.id } }90        editingItem = nil91    }9293    private func time(of item: GroundedItem) -> Double? { item.segmentID.flatMap { id in store.transcript?.segments.first { $0.id == id }?.start } }9495    private func notesView(_ notes: MeetingNotes) -> some View {96        VStack(spacing: ORIX.Space.m) {97            ORIXCard {98                VStack(alignment: .leading, spacing: 10) {99                    HStack {100                        Chip(text: String(localized: "AI-generated on-device"), symbol: "sparkles", tint: ORIX.Palette.violet)101                        if notes.isEdited { Chip(text: String(localized: "Edited")) }102                        Spacer()103                        Menu {104                            Button("Edit summary", systemImage: "pencil") { summaryDraft = notes.summary; editingSummary = true }105                            Button("Use the title for this recording", systemImage: "textformat") { recording.title = notes.title }106                            Button("Generate again", systemImage: "arrow.clockwise") { model.processing.summarizeInBackground(recording) }.disabled(availability != .available)107                        } label: { Image(systemName: "ellipsis.circle").font(.title3) }.accessibilityLabel(Text("Notes options"))108                    }109                    Text(notes.title).font(.title3.weight(.semibold))110                    Text(notes.summary).font(.body)111                    Text("Written by \(notes.generator) from the transcript only. It can be wrong or incomplete — tap a point to hear its source.").font(.caption2).foregroundStyle(ORIX.Palette.textSecondary)112                }113            }114            itemsCard("Key points", "list.bullet", notes.keyPoints)115            itemsCard("Action items", "checklist", notes.actionItems)116            itemsCard("Decisions", "checkmark.seal", notes.decisions)117            if !notes.topics.isEmpty {118                ORIXCard {119                    VStack(alignment: .leading, spacing: 8) {120                        SectionTitle("Topics", symbol: "tag")121                        FlowLayout(spacing: 6) { ForEach(notes.topics, id: \.self) { Chip(text: $0, tint: ORIX.Palette.cyan) } }122                    }123                }124            }125            if notes.chapters.count > 1 {126                ORIXCard {127                    VStack(alignment: .leading, spacing: 8) {128                        SectionTitle("Chapters", symbol: "list.number")129                        ForEach(notes.chapters) { chapter in130                            Button { onSeek(chapter.start) } label: {131                                HStack(alignment: .top, spacing: 10) {132                                    Text(Timecode.clock(chapter.start)).font(.orixReadout).foregroundStyle(ORIX.Palette.cyan).frame(width: 54, alignment: .leading)133                                    VStack(alignment: .leading, spacing: 2) {134                                        Text(chapter.title).font(.subheadline.weight(.semibold)).foregroundStyle(ORIX.Palette.text)135                                        Text(chapter.summary).font(.footnote).foregroundStyle(ORIX.Palette.textSecondary).multilineTextAlignment(.leading)136                                    }137                                }138                            }.buttonStyle(.plain)139                        }140                    }141                }142            }143        }144    }145146    @ViewBuilder private func itemsCard(_ title: LocalizedStringKey, _ symbol: String, _ items: [GroundedItem]) -> some View {147        if !items.isEmpty {148            let grounded = items.filter(\.isGrounded), loose = items.filter { !$0.isGrounded }149            ORIXCard {150                VStack(alignment: .leading, spacing: 8) {151                    SectionTitle(title, symbol: symbol)152                    ForEach(grounded) { itemRow($0) }153                    if !loose.isEmpty {154                        Label("Not traced to a transcript line — verify before relying on these", systemImage: "questionmark.circle").font(.caption).foregroundStyle(ORIX.Palette.warning).padding(.top, 4)155                        ForEach(loose) { itemRow($0) }156                    }157                }158            }159        }160    }161162    private func itemRow(_ item: GroundedItem) -> some View {163        Button { if let t = time(of: item) { onSeek(t) } } label: {164            HStack(alignment: .top, spacing: 8) {165                Image(systemName: item.isGrounded ? "circle.fill" : "circle.dashed").font(.system(size: 6)).padding(.top, 7).foregroundStyle(item.isGrounded ? ORIX.Palette.cyan : ORIX.Palette.warning)166                VStack(alignment: .leading, spacing: 2) {167                    Text(item.text).font(.subheadline).foregroundStyle(item.isGrounded ? ORIX.Palette.text : ORIX.Palette.textSecondary).multilineTextAlignment(.leading)168                    HStack(spacing: 6) {169                        if let owner = item.owner { Chip(text: owner, symbol: "person.fill", tint: ORIX.Palette.violet) }170                        if let t = time(of: item) { Chip(text: Timecode.clock(t), symbol: "play.fill", tint: ORIX.Palette.cyan) }171                    }172                }173                Spacer(minLength: 0)174            }175        }176        .buttonStyle(.plain)177        .contextMenu { Button("Edit or delete", systemImage: "pencil") { itemDraft = item.text; editingItem = item } }178        .accessibilityHint(item.isGrounded ? Text("Plays the source in the recording") : Text("No source found in the transcript"))179    }180181    private var questionCard: some View {182        ORIXCard {183            VStack(alignment: .leading, spacing: 10) {184                SectionTitle("Ask about this recording", symbol: "questionmark.bubble")185                HStack {186                    TextField("What was decided about…?", text: $question, axis: .vertical).lineLimit(1...3).submitLabel(.send).onSubmit(ask)187                    if asking { ProgressView() } else {188                        Button(action: ask) { Image(systemName: "arrow.up.circle.fill").font(.title2) }.disabled(question.trimmingCharacters(in: .whitespaces).isEmpty)189                            .accessibilityLabel(Text("Ask"))190                    }191                }192                if let askError { Text(askError).font(.footnote).foregroundStyle(ORIX.Palette.record) }193                if let answer {194                    VStack(alignment: .leading, spacing: 6) {195                        if answer.isAnswerable {196                            Text(answer.text).font(.subheadline)197                            FlowLayout(spacing: 6) {198                                ForEach(answer.segmentIDs, id: \.self) { id in199                                    if let seg = store.transcript?.segments.first(where: { $0.id == id }) {200                                        Button { onSeek(seg.start) } label: { Chip(text: Timecode.clock(seg.start), symbol: "play.fill", tint: ORIX.Palette.cyan) }.buttonStyle(.plain)201                                    }202                                }203                            }204                        } else {205                            Label("The transcript doesn't appear to answer that.", systemImage: "text.badge.xmark").font(.subheadline).foregroundStyle(ORIX.Palette.textSecondary)206                        }207                        Text("Answered on-device from transcript excerpts only.").font(.caption2).foregroundStyle(ORIX.Palette.textSecondary)208                    }209                }210            }211        }212    }213214    private func ask() {215        let q = question.trimmingCharacters(in: .whitespaces)216        guard !q.isEmpty, let transcript = store.transcript, !asking else { return }217        asking = true; askError = nil; answer = nil218        Task {219            do { answer = try await MeetingIntelligence.answer(question: q, transcript: transcript) } catch { askError = CoreText.describe(error) }220            asking = false221        }222    }223}224225/// Wrapping horizontal layout for chips.226struct FlowLayout: Layout {227    var spacing: CGFloat = 6228    func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {229        let width = proposal.width ?? .infinity230        var x: CGFloat = 0, y: CGFloat = 0, row: CGFloat = 0, maxX: CGFloat = 0231        for s in subviews {232            let size = s.sizeThatFits(.unspecified)233            if x + size.width > width, x > 0 { x = 0; y += row + spacing; row = 0 }234            x += size.width + spacing; row = max(row, size.height); maxX = max(maxX, x)235        }236        return CGSize(width: min(width, maxX), height: y + row)237    }238    func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {239        var x = bounds.minX, y = bounds.minY, row: CGFloat = 0240        for s in subviews {241            let size = s.sizeThatFits(.unspecified)242            if x + size.width > bounds.maxX, x > bounds.minX { x = bounds.minX; y += row + spacing; row = 0 }243            s.place(at: CGPoint(x: x, y: y), proposal: ProposedViewSize(size))244            x += size.width + spacing; row = max(row, size.height)245        }246    }247}248
Files main
Menu

Theme