SPB Git

Repository Public

ChartAsk.swift 3.4 KB 66 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Charting/ChartAsk.swift3// Description: "Ask the agent about the visible range": builds the question and its factual context from a chart (nothing is sent here), and carries it from a detached chart window to the Research composer of a main window.4import SwiftUI5import VQuantCore67/// A question waiting for a main window (a detached chart window has no Research composer of its own).8@Observable final class ChartAskRouter: SessionScoped {9    var pending: AskDraft?10    func reset() { pending = nil }11}12extension AppEnvironment { var chartAsk: ChartAskRouter { store { ChartAskRouter() } } }1314enum ChartAsk {15    static func indicatorNames(_ i: IndicatorSet) -> [String] {16        var out: [String] = []17        if i.sma20 { out.append("SMA(20)") }; if i.sma50 { out.append("SMA(50)") }; if i.sma200 { out.append("SMA(200)") }18        if i.ema20 { out.append("EMA(20)") }; if i.bollinger { out.append("Bollinger bands(20, 2)") }19        if i.volume { out.append("Volume") }; if i.rsi { out.append("RSI(14)") }; if i.macd { out.append("MACD(12, 26, 9)") }20        return out21    }2223    /// What the chart shows right now, in words (see `ChartAskContext`); nil while there is no bar on screen.24    static func context(_ chart: ChartModel) -> ChartAskContext? {25        let vis = chart.visibleRange26        guard !vis.isEmpty else { return nil }27        let bars = chart.bars[vis]28        guard let first = bars.first, let last = bars.last else { return nil }29        let p = chart.provenance30        return ChartAskContext(symbol: chart.symbol, interval: chart.interval.v2, intraday: chart.isIntraday, timeZone: p?.route == .v2 ? p?.timezone : nil,31                               firstVisible: first.time, lastVisible: last.time, visibleBars: bars.count, firstClose: first.close, lastClose: last.close,32                               visibleHigh: bars.map(\.high).max(), visibleLow: bars.map(\.low).min(),33                               chartType: chart.style.rawValue, logScale: chart.logScale && chart.compareOrder.isEmpty, indicators: indicatorNames(chart.indicators),34                               compare: chart.compareOrder, drawings: chart.drawings, provider: p?.provider, adjust: p?.adjust, currency: p?.currency)35    }3637    static func draft(_ chart: ChartModel) -> AskDraft? {38        guard let c = context(chart) else { return nil }39        return AskDraft(text: c.question, context: c.text)40    }4142    /// From a detached chart window: a main window takes the question (the frontmost one that is listening), or one43    /// is opened for it.44    static func route(_ draft: AskDraft, app: AppEnvironment) {45        app.chartAsk.pending = draft46        DispatchQueue.main.asyncAfter(deadline: .now() + 0.25) {47            if app.chartAsk.pending != nil { MainWindowOpener.open?(); NSApp.activate(ignoringOtherApps: true) }48        }49    }50}5152/// Installed on every main window: takes a question routed from a chart window and shows it in Research.53struct ChartAskReceiver: ViewModifier {54    let app: AppEnvironment55    let model: WindowModel56    func body(content: Content) -> some View {57        content.onChange(of: app.chartAsk.pending, initial: true) { _, draft in58            guard let draft else { return }59            app.chartAsk.pending = nil60            model.ask(draft)61            model.window?.makeKeyAndOrderFront(nil)62            NSApp.activate(ignoringOtherApps: true)63        }64    }65}66
Files main
Menu

Theme