SPB Git

Repository Public

Navigation.swift 5.8 KB 126 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: App/Navigation.swift3// Description: The sections of the app, the restorable description of a window, and the per-window navigation state.4import SwiftUI5import VQuantCore67/// Sections of a main window. VQuant is, at its core, the autonomous research agent: Research comes first and the8/// terminal, the lab, the portfolios and the studio are surfaces around it. There is still no trading: the backend9/// has no brokerage connection (docs/FEATURE_PARITY.md).10enum AppSection: String, CaseIterable, Identifiable, Codable {11    // the order is the order of the View menu; the first nine get ⌘1…⌘912    case research, studio, overview, markets, watchlists, charting, statements, quant, portfolio, data, risk, jobs, agent, documents13    var id: String { rawValue }14    var title: String {15        switch self {16        case .overview: "Overview"; case .markets: "Markets"; case .watchlists: "Watchlists"; case .charting: "Charting"17        case .research: "Research"; case .studio: "Research Studio"; case .quant: "Quant Lab"; case .data: "Data Explorer"; case .statements: "Statements"18        case .portfolio: "Portfolios"; case .risk: "Risk Analytics"19        case .agent: "Agent"; case .jobs: "Jobs"; case .documents: "Documents"20        }21    }22    var symbol: String {23        switch self {24        case .overview: "square.grid.2x2"; case .markets: "globe.americas"; case .watchlists: "star"; case .charting: "chart.xyaxis.line"25        case .research: "sparkles"; case .studio: "books.vertical"; case .quant: "function"; case .data: "tablecells"; case .statements: "doc.text.magnifyingglass"26        case .portfolio: "briefcase"; case .risk: "gauge.with.dots.needle.67percent"27        case .agent: "brain"; case .jobs: "list.bullet.rectangle"; case .documents: "doc.text"28        }29    }30    static let groups: [(String, [AppSection])] = [31        ("Research", [.research, .studio]),32        ("Terminal", [.overview, .markets, .watchlists, .charting, .statements, .data]),33        ("Quant", [.quant, .jobs]),34        ("Portfolio", [.portfolio, .risk]),35        ("Workspace", [.agent, .documents]),36    ]37}3839/// What a main window shows. It is the value of its WindowGroup, so macOS restores each window with its own section,40/// symbol and conversation after a relaunch.41struct MainSpec: Codable, Hashable {42    var id = UUID()43    var section: AppSection = .research44    var symbol: String = "AAPL"45    var sessionId: String?46    var quantJobId: String?47    var projectId: String?48    var portfolioId: String?49}5051/// What a detached chart window shows.52struct ChartSpec: Codable, Hashable {53    var id = UUID()54    var symbol: String55    var interval: BarInterval = .d156    var range: String = "1Y"57    var compare: [String] = []58}5960/// Navigation state of one window. Two windows never share one.61@Observable final class WindowModel {62    var section: AppSection63    var symbol: String64    /// Conversation shown in Research (nil = a new one).65    var sessionId: String?66    /// Identity of the new conversation being composed, until the server names it.67    var draftKey = UUID()68    var quantJobId: String?69    var quantTool: String?70    var dataset: Dataset = .income71    var searchFocusTick = 072    weak var window: NSWindow?7374    // VQuant Next75    /// Research Studio: the open project (a synced `project` object id).76    var projectId: String?77    /// Portfolios: the open portfolio (`main` = the one the agent reasons about).78    var portfolioId: String?79    /// Watchlists: the open list (a synced `watchlist` object id).80    var watchlistId: String?81    /// The command palette (⌘K) of this window.82    var paletteOpen = false83    var paletteQuery = ""84    /// DEBUG launch argument `-VQTab`: the sub-tab a section opens on (consumed by the section).85    var launchTab: String?86    /// A question prepared elsewhere in the window (a chart's visible range, a lab result, a statement) for the87    /// Research composer. Research consumes it (sets it back to nil) when it appears.88    var pendingAsk: AskDraft?8990    init(spec: MainSpec) {91        section = spec.section; symbol = spec.symbol; sessionId = spec.sessionId; quantJobId = spec.quantJobId92        projectId = spec.projectId; portfolioId = spec.portfolioId93    }9495    /// Hands a prepared question to Research in a new conversation.96    func ask(_ draft: AskDraft) { pendingAsk = draft; openConversation(nil) }97    func openProject(_ id: String?) { projectId = id; section = .studio }98    func openPortfolio(_ id: String?) { portfolioId = id; section = .portfolio }99    func openLabTool(_ id: String) { quantTool = id; quantJobId = nil; section = .quant }100101    func show(_ s: AppSection, symbol: String? = nil) {102        if let symbol, let n = Watchlist.normalize(symbol) { self.symbol = n }103        section = s104    }105    func openConversation(_ id: String?) { sessionId = id; if id == nil { draftKey = UUID() }; section = .research }106    func openQuantJob(_ id: String) { quantJobId = id; section = .quant }107}108109/// A question prepared for the Research composer, with the context the agent needs stated in words (the server110/// receives plain text: there is no hidden channel).111struct AskDraft: Equatable {112    /// What is put in the composer (the person can edit it before sending).113    var text: String114    /// Factual context appended to the question when it is sent (symbol, interval, visible dates, levels…).115    var context: String = ""116    /// The mode proposed for it (nil = the person's default).117    var mode: ResearchMode?118    /// File the conversation in this project (Research Studio).119    var projectId: String?120    /// Send at once instead of waiting for the person.121    var sendImmediately = false122}123124struct WindowModelKey: FocusedValueKey { typealias Value = WindowModel }125extension FocusedValues { var windowModel: WindowModel? { get { self[WindowModelKey.self] } set { self[WindowModelKey.self] = newValue } } }126
Files main
Menu

Theme