Skip to content

Private AI meeting notes that stay on your computer. No meeting bot. Free and open source for Mac, Windows and Linux. An alternative to Granola, Otter and Fireflies.

15.0 KB
Raw Blame
import SwiftUI
import YogurtKit

/// Ask across meetings, like desktop's Ask: a question box with scope chips, and answers whose numbered sources open
/// the meeting at the moment they come from. The computer answers when it's on the same Wi-Fi, else Apple's model here.
struct AskView: View {
    /// opened from a note: asks about that meeting first
    let meetingId: String?
    @Environment(AppModel.self) private var model
    @State private var session = AskSession.shared
    @State private var text = ""
    @State private var scope: Choice?
    @FocusState private var focused: Bool

    enum Choice: Hashable { case meeting(String), folder(String), all }

    private var key: String { meetingId ?? "home" }

    var body: some View {
        let _ = model.revision
        let turns = session.turns(key)
        let answerer = AskSession.whereAnswered(model)
        ScrollViewReader { proxy in
            ScrollView {
                VStack(alignment: .leading, spacing: 0) {
                    if turns.isEmpty {
                        intro(answerer)
                    }
                    ForEach(turns) { turn in
                        TurnView(turn: turn) { session.retry(turn.id, key: key, model: model) }
                            .padding(.top, 24)
                            .id(turn.id)
                    }
                }
                .padding(.horizontal, 20)
                .padding(.bottom, 24)
            }
            .scrollDismissesKeyboard(.interactively)
            .onChange(of: turns.count) { _, _ in
                if let last = turns.last { withAnimation(.easeOut(duration: 0.2)) { proxy.scrollTo(last.id, anchor: .top) } }
            }
        }
        .background(Theme.bg)
        .navigationTitle(turns.isEmpty ? "" : S.ask)
        .navigationBarTitleDisplayMode(.inline)
        .toolbar {
            if !turns.isEmpty {
                ToolbarItem(placement: .topBarTrailing) {
                    Button { withAnimation(.easeOut(duration: 0.2)) { session.newChat(key) }; text = "" } label: {
                        Label(S.newChat, systemImage: "square.and.pencil")
                    }
                    .accessibilityIdentifier("ask-new-chat")
                }
            }
        }
        .safeAreaInset(edge: .bottom, spacing: 0) {
            if answerer == .nowhere {
                NavigationLink(value: Route.search) { Label(S.searchMeetings, systemImage: "magnifyingglass") }
                    .buttonStyle(PrimaryButtonStyle())
                    .padding(.horizontal, 20)
                    .padding(.vertical, 12)
                    .background(Theme.bg)
                    .accessibilityIdentifier("ask-search")
            } else {
                askBar
            }
        }
        .onAppear(perform: launch)
    }

    // MARK: - Before the first question

    @ViewBuilder private func intro(_ answerer: AskSession.Where) -> some View {
        Text(S.askTitle)
            .font(.largeTitle.weight(.semibold))
            .accessibilityAddTraits(.isHeader)
            .foregroundStyle(Theme.heading)
            .padding(.top, 12)
        if answerer == .nowhere {
            unavailable.padding(.top, 20)
        } else {
            VStack(alignment: .leading, spacing: 0) {
                ForEach(starters, id: \.self) { q in
                    Button { send(q) } label: {
                        HStack(alignment: .firstTextBaseline, spacing: 12) {
                            Text(q)
                                .font(.body)
                                .foregroundStyle(Theme.ink)
                                .multilineTextAlignment(.leading)
                            Spacer(minLength: 0)
                            Image(systemName: "arrow.up.right")
                                .font(.footnote)
                                .foregroundStyle(Theme.muted)
                                .accessibilityHidden(true)
                        }
                        .padding(.horizontal, 16)
                        .padding(.vertical, 12)
                        .frame(minHeight: 54)
                    }
                    .buttonStyle(RowButtonStyle())
                }
            }
            .background(Theme.card, in: RoundedRectangle(cornerRadius: 18))
            .padding(.top, 24)
        }
    }

    /// No computer nearby and no Apple Intelligence here: say so, and offer Search, which always works.
    private var unavailable: some View {
        VStack(alignment: .leading, spacing: 12) {
            // on its own the phone doesn't mention a computer outside Settings
            Label(model.engine.status.isPaired ? S.askNeedsModel : S.askNeedsModelAlone, systemImage: "iphone")
                .font(.subheadline.weight(.medium)).foregroundStyle(Theme.ink)
                .fixedSize(horizontal: false, vertical: true)
            Text(S.askSearchInstead).font(.subheadline).foregroundStyle(Theme.muted)
        }
        .frame(maxWidth: .infinity, alignment: .leading)
    }

    private var starters: [String] {
        meetingId != nil && chosen == .meeting(meetingId!)
            ? [S.askMeetingStarter1, S.askMeetingStarter2, S.askMeetingStarter3]
            : [S.askStarter1, S.askStarter2, S.askStarter3]
    }

    // MARK: - The question box

    private var chosen: Choice {
        let folders = model.store.folders()
        switch scope {
        case .folder(let id) where !folders.contains { $0.id == id }: return .all
        case let s?: return s
        case nil: return meetingId.map(Choice.meeting) ?? .all
        }
    }

    private var askBar: some View {
        let busy = session.busy
        let q = text.trimmingCharacters(in: .whitespacesAndNewlines)
        return VStack(spacing: 2) {
            chips
            HStack(alignment: .bottom, spacing: 8) {
                TextField(chosen == .meeting(meetingId ?? "") ? S.askAnything : S.askPrompt, text: $text, axis: .vertical)
                    .lineLimit(1...5)
                    .focused($focused)
                    .submitLabel(.send)
                    .onSubmit { send(q) }
                    .padding(.leading, 14).padding(.vertical, 12)
                    .accessibilityIdentifier("ask-field")
                Button { send(q) } label: {
                    Image(systemName: "arrow.up")
                        .font(.body.weight(.semibold)).foregroundStyle(q.isEmpty || busy ? Theme.accent : Theme.onAccent)
                        .frame(width: 38, height: 38)
                        .background(q.isEmpty || busy ? Theme.accentSoft : Theme.accent, in: Circle())
                        .frame(width: 44, height: 44)
                        .contentShape(Rectangle())
                }
                .buttonStyle(.plain)
                .disabled(q.isEmpty || busy)
                .accessibilityLabel(S.askSend)
                .accessibilityIdentifier("ask-send")
            }
            .padding(4)
            .background(Theme.card, in: RoundedRectangle(cornerRadius: 24))
            .padding(.horizontal, 20)
        }
        .padding(.top, 6).padding(.bottom, 8)
        .background(Theme.bg)
    }

    /// This meeting (from a note), each folder, All meetings
    private var chips: some View {
        let folders = model.store.folders()
        return ScrollView(.horizontal) {
            HStack(spacing: 8) {
                if let meetingId { chip(S.askThisMeeting, .meeting(meetingId)) }
                chip(S.allMeetings, .all)
                ForEach(folders) { chip($0.name, .folder($0.id)) }
            }
            .padding(.horizontal, 20)
        }
        .scrollIndicators(.hidden)
    }

    private func chip(_ name: String, _ choice: Choice) -> some View {
        let on = chosen == choice
        return Button { withAnimation(.easeOut(duration: 0.15)) { scope = choice } } label: {
            Text(name).lineLimit(1)
            .font(.footnote.weight(.medium))
            .foregroundStyle(on ? Theme.accent : Theme.muted)
            .padding(.horizontal, 11)
            .frame(minHeight: 30)
            .background(on ? Theme.accentSoft : .clear, in: Capsule())
            .frame(minHeight: 44)
            .contentShape(Rectangle())
        }
        .buttonStyle(.plain)
        .accessibilityAddTraits(on ? .isSelected : [])
    }

    private func send(_ question: String) {
        let q = question.trimmingCharacters(in: .whitespacesAndNewlines)
        guard !q.isEmpty, !session.busy else { return }
        let (scope, name): (AskScope?, String) = switch chosen {
        case .meeting(let id): (AskScope(meetingIds: [id]), S.askThisMeeting)
        case .folder(let id): (AskScope(folderId: id), model.store.folders().first { $0.id == id }?.name ?? S.allMeetings)
        case .all: (nil, S.allMeetings)
        }
        session.ask(q, scope: scope, scopeName: name, key: key, model: model)
        text = ""
        focused = false
    }

    private func launch() {
        #if DEBUG
        // screenshots: -openAsk "question" asks it once
        if let q = UserDefaults.standard.string(forKey: "openAsk") {
            if !q.isEmpty, session.turns(key).isEmpty { send(q) }
            return
        }
        #endif
        if session.turns(key).isEmpty && AskSession.whereAnswered(model) != .nowhere { focused = true }
    }
}

/// One question and its answer.
private struct TurnView: View {
    let turn: AskSession.Turn
    let retry: () -> Void

    var body: some View {
        VStack(alignment: .leading, spacing: 14) {
            Text(turn.question)
                .font(.body.weight(.medium)).foregroundStyle(Theme.ink)
                .padding(.horizontal, 14).padding(.vertical, 10)
                .background(Theme.accentSoft, in: RoundedRectangle(cornerRadius: 16, style: .continuous))
                .frame(maxWidth: .infinity, alignment: .trailing)
                .padding(.leading, 40)
            switch turn.state {
            case .answering:
                HStack(spacing: 8) {
                    ProgressView().controlSize(.small)
                    Text(S.askReading).font(.subheadline).foregroundStyle(Theme.muted)
                }
            case .failed:
                VStack(alignment: .leading, spacing: 8) {
                    Text(S.askFailed).font(.subheadline).foregroundStyle(Theme.danger)
                    Button(S.tryAgain, action: retry).font(.subheadline.weight(.semibold))
                }
            case .answered(let a):
                AnswerBody(text: a.text).textSelection(.enabled)
                HStack(spacing: 6) {
                    Image(systemName: a.by == .computer ? "laptopcomputer" : "iphone").font(.caption)
                    Text([a.by == .computer ? S.askByComputer : S.askByPhone, S.askIn(turn.scopeName)].joined(separator: " · "))
                }
                .font(.caption).foregroundStyle(Theme.muted)
                .accessibilityElement(children: .combine)
                if !a.sources.isEmpty { SourcesView(sources: a.sources, answer: a.text) }
            }
        }
    }
}

/// The answer's Markdown (bold, "- " bullets) with each [n] in purple, matching the numbered sources below it.
private struct AnswerBody: View {
    let text: String

    var body: some View {
        VStack(alignment: .leading, spacing: 8) {
            ForEach(Array(text.components(separatedBy: "\n").enumerated()), id: \.offset) { _, line in
                let t = line.trimmingCharacters(in: .whitespaces)
                if t.hasPrefix("- ") || t.hasPrefix("* ") {
                    HStack(alignment: .firstTextBaseline, spacing: 8) {
                        Text("•").foregroundStyle(Theme.muted)
                        styled(String(t.dropFirst(2)))
                    }
                } else if !t.isEmpty {
                    styled(t)
                }
            }
        }
        .font(.body)
        .foregroundStyle(Theme.ink)
        .lineSpacing(3)
        .frame(maxWidth: .infinity, alignment: .leading)
    }

    private func styled(_ line: String) -> Text {
        var out = AttributedString()
        var rest = Substring(line)
        while let m = rest.firstMatch(of: /\[\d+(?:,\s*\d+)*\]/) {
            out += inline(String(rest[..<m.range.lowerBound]))
            var cite = AttributedString(String(rest[m.range]))
            cite.font = .footnote.weight(.semibold)
            cite.foregroundColor = Theme.accent
            out += cite
            rest = rest[m.range.upperBound...]
        }
        out += inline(String(rest))
        return Text(out)
    }

    private func inline(_ s: String) -> AttributedString {
        (try? AttributedString(markdown: s, options: .init(interpretedSyntax: .inlineOnlyPreservingWhitespace))) ?? AttributedString(s)
    }
}

/// The numbered sources; each opens its meeting's transcript at the moment. The notes have no moment of their own:
/// they open the transcript where it says what the answer took from them, else the notes.
private struct SourcesView: View {
    let sources: [AskSource]
    let answer: String

    var body: some View {
        VStack(alignment: .leading, spacing: 2) {
            Text(S.askSources).font(.footnote.weight(.medium)).foregroundStyle(Theme.muted).padding(.bottom, 4)
            ForEach(sources) { s in
                NavigationLink(value: s.atMs.map { Route.transcript(s.meetingId, $0) }
                               ?? Route.cited(s.meetingId, CitedMoment.claim(for: s.n, in: answer))) {
                    row(s)
                }
                .buttonStyle(RowButtonStyle())
                .padding(.horizontal, -8)
                .accessibilityHint(S.openInTranscript)
            }
        }
        .padding(.top, 4)
    }

    /// the excerpt as one line of plain words: no headings, bullets or bold marks
    private func plain(_ text: String) -> String {
        text.components(separatedBy: "\n")
            .map { $0.trimmingCharacters(in: .whitespaces).replacing(/^(#+|[-*]|\d+\.)\s+/, with: "") }
            .filter { !$0.isEmpty }
            .joined(separator: " ")
            .replacingOccurrences(of: "**", with: "")
    }

    private func row(_ s: AskSource) -> some View {
        let title = s.title.isEmpty ? S.untitled : s.title
        return HStack(alignment: .firstTextBaseline, spacing: 10) {
            Text("\(s.n)")
                .font(.caption.weight(.semibold).monospacedDigit())
                .foregroundStyle(Theme.accent)
                .frame(minWidth: 22, minHeight: 22)
                .background(Theme.accentSoft, in: Circle())
            VStack(alignment: .leading, spacing: 3) {
                Text(s.atMs.map { S.sourceAt(title, Format.offset($0)) } ?? S.sourceAt(title, S.notesSource))
                    .font(.subheadline.weight(.medium)).foregroundStyle(Theme.ink).lineLimit(1)
                if !s.excerpt.isEmpty {
                    Text(plain(s.excerpt))
                        .font(.footnote).foregroundStyle(Theme.muted).lineLimit(2)
                }
            }
            Spacer(minLength: 4)
            Image(systemName: "chevron.forward").font(.caption.weight(.semibold)).foregroundStyle(Theme.muted)
        }
        .padding(.vertical, 10).padding(.horizontal, 8)
        .frame(maxWidth: .infinity, alignment: .leading)
    }
}