Add Yahoo Finance stock data integration

- Add StockDataSource to fetch quotes from Yahoo Finance API
- Add StockSettingsStore for persisting user's stock symbols
- Add StockSettingsView with UI to manage symbols (max 5)
- Add STOCK feed item type and ranker weight (0.3)
- Integrate stock fetch into ContextOrchestrator pipeline
- Stock cards appear in FYI bucket and sync to Glass via BLE

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-10 20:30:18 +00:00
parent 3b73a092d1
commit b00bf670e6
7 changed files with 415 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import SwiftUI
struct ContentView: View { struct ContentView: View {
@EnvironmentObject private var orchestrator: ContextOrchestrator @EnvironmentObject private var orchestrator: ContextOrchestrator
@StateObject private var stockSettings = StockSettingsStore()
var body: some View { var body: some View {
TabView { TabView {
@@ -18,6 +19,10 @@ struct ContentView: View {
.tabItem { Label("Orchestrator", systemImage: "bolt.horizontal.circle") } .tabItem { Label("Orchestrator", systemImage: "bolt.horizontal.circle") }
TodosView() TodosView()
.tabItem { Label("Todos", systemImage: "checklist") } .tabItem { Label("Todos", systemImage: "checklist") }
NavigationStack {
StockSettingsView(store: stockSettings)
}
.tabItem { Label("Stocks", systemImage: "chart.line.uptrend.xyaxis") }
SettingsView() SettingsView()
.tabItem { Label("Settings", systemImage: "gearshape") } .tabItem { Label("Settings", systemImage: "gearshape") }
} }

View File

@@ -0,0 +1,186 @@
//
// StockDataSource.swift
// iris
//
import Foundation
struct StockDataSourceConfig: Sendable {
var maxSymbols: Int = 5
var cacheValiditySec: Int = 300
var ttlSec: Int = 600
init() {}
}
final class StockDataSource {
struct StockQuote: Sendable, Equatable {
let symbol: String
let shortName: String
let price: Double
let change: Double
let changePercent: Double
let marketState: String
}
struct StockData: Sendable, Equatable {
let quotes: [StockQuote]
}
struct Snapshot: Sendable {
let data: StockData
let diagnostics: [String: String]
}
enum StockError: Error, LocalizedError, Sendable {
case noSymbolsConfigured
case networkFailed(message: String, diagnostics: [String: String])
case rateLimited(diagnostics: [String: String])
case invalidResponse(diagnostics: [String: String])
var errorDescription: String? {
switch self {
case .noSymbolsConfigured:
return "No symbols configured"
case .networkFailed(let message, _):
return message
case .rateLimited:
return "Rate limited by Yahoo Finance"
case .invalidResponse:
return "Invalid response from Yahoo Finance"
}
}
}
private let config: StockDataSourceConfig
private var cache: (timestamp: Int, data: StockData)?
init(config: StockDataSourceConfig = .init()) {
self.config = config
}
func dataWithDiagnostics(symbols: [String], now: Int) async throws -> Snapshot {
var diagnostics: [String: String] = [
"now": String(now),
"symbols_requested": symbols.joined(separator: ","),
"max_symbols": String(config.maxSymbols),
]
guard !symbols.isEmpty else {
diagnostics["result"] = "no_symbols"
return Snapshot(data: StockData(quotes: []), diagnostics: diagnostics)
}
let limitedSymbols = Array(symbols.prefix(config.maxSymbols))
diagnostics["symbols_queried"] = limitedSymbols.joined(separator: ",")
if let cache = cache, now - cache.timestamp < config.cacheValiditySec {
diagnostics["source"] = "cache"
diagnostics["cache_age_sec"] = String(now - cache.timestamp)
return Snapshot(data: cache.data, diagnostics: diagnostics)
}
let symbolsParam = limitedSymbols.joined(separator: ",")
guard let encodedSymbols = symbolsParam.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed),
let url = URL(string: "https://query1.finance.yahoo.com/v7/finance/quote?symbols=\(encodedSymbols)") else {
diagnostics["error"] = "invalid_url"
throw StockError.networkFailed(message: "Invalid URL", diagnostics: diagnostics)
}
var request = URLRequest(url: url)
request.setValue("Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X)", forHTTPHeaderField: "User-Agent")
request.setValue("application/json", forHTTPHeaderField: "Accept")
let data: Data
let response: URLResponse
do {
(data, response) = try await URLSession.shared.data(for: request)
} catch {
diagnostics["error"] = "network_error"
diagnostics["error_message"] = String(describing: error)
if let cached = cache {
diagnostics["source"] = "cache_fallback"
return Snapshot(data: cached.data, diagnostics: diagnostics)
}
throw StockError.networkFailed(message: String(describing: error), diagnostics: diagnostics)
}
if let httpResponse = response as? HTTPURLResponse {
diagnostics["http_status"] = String(httpResponse.statusCode)
if httpResponse.statusCode == 403 {
diagnostics["error"] = "rate_limited"
if let cached = cache {
diagnostics["source"] = "cache_fallback"
return Snapshot(data: cached.data, diagnostics: diagnostics)
}
throw StockError.rateLimited(diagnostics: diagnostics)
}
guard (200..<300).contains(httpResponse.statusCode) else {
diagnostics["error"] = "http_error"
if let cached = cache {
diagnostics["source"] = "cache_fallback"
return Snapshot(data: cached.data, diagnostics: diagnostics)
}
throw StockError.networkFailed(message: "HTTP \(httpResponse.statusCode)", diagnostics: diagnostics)
}
}
let yahooResponse: YahooQuoteResponse
do {
yahooResponse = try JSONDecoder().decode(YahooQuoteResponse.self, from: data)
} catch {
diagnostics["error"] = "json_decode_error"
diagnostics["error_message"] = String(describing: error)
if let cached = cache {
diagnostics["source"] = "cache_fallback"
return Snapshot(data: cached.data, diagnostics: diagnostics)
}
throw StockError.invalidResponse(diagnostics: diagnostics)
}
let quotes: [StockQuote] = (yahooResponse.quoteResponse.result ?? []).compactMap { quote in
guard let price = quote.regularMarketPrice else { return nil }
return StockQuote(
symbol: quote.symbol,
shortName: quote.shortName ?? quote.symbol,
price: price,
change: quote.regularMarketChange ?? 0,
changePercent: quote.regularMarketChangePercent ?? 0,
marketState: quote.marketState ?? "CLOSED"
)
}
diagnostics["source"] = "network"
diagnostics["quotes_returned"] = String(quotes.count)
let stockData = StockData(quotes: quotes)
cache = (timestamp: now, data: stockData)
return Snapshot(data: stockData, diagnostics: diagnostics)
}
}
// MARK: - Yahoo Finance API Response Models
private struct YahooQuoteResponse: Codable {
let quoteResponse: QuoteResponse
struct QuoteResponse: Codable {
let result: [Quote]?
let error: QuoteError?
}
struct Quote: Codable {
let symbol: String
let shortName: String?
let regularMarketPrice: Double?
let regularMarketChange: Double?
let regularMarketChangePercent: Double?
let marketState: String?
}
struct QuoteError: Codable {
let code: String?
let description: String?
}
}

View File

@@ -90,6 +90,7 @@ final class HeuristicRanker {
case .transit: return 0.75 case .transit: return 0.75
case .poiNearby: return 0.6 case .poiNearby: return 0.6
case .info: return 0.4 case .info: return 0.4
case .stock: return 0.3
case .nowPlaying: return 0.25 case .nowPlaying: return 0.25
case .currentWeather: return 0.0 case .currentWeather: return 0.0
case .allQuiet: return 0.0 case .allQuiet: return 0.0

View File

@@ -0,0 +1,48 @@
//
// StockSettingsStore.swift
// iris
//
import Foundation
import Combine
@MainActor
final class StockSettingsStore: ObservableObject {
nonisolated static let userDefaultsKey = "iris.stock.symbols"
@Published private(set) var symbols: [String] = []
private let maxSymbols = 5
init() {
loadSymbols()
}
private func loadSymbols() {
symbols = UserDefaults.standard.stringArray(forKey: Self.userDefaultsKey) ?? []
}
func saveSymbols(_ newSymbols: [String]) {
let cleaned = newSymbols
.map { $0.uppercased().trimmingCharacters(in: .whitespacesAndNewlines) }
.filter { !$0.isEmpty }
.prefix(maxSymbols)
symbols = Array(cleaned)
UserDefaults.standard.set(symbols, forKey: Self.userDefaultsKey)
}
@discardableResult
func addSymbol(_ symbol: String) -> Bool {
guard symbols.count < maxSymbols else { return false }
let cleaned = symbol.uppercased().trimmingCharacters(in: .whitespacesAndNewlines)
guard !cleaned.isEmpty, !symbols.contains(cleaned) else { return false }
symbols.append(cleaned)
UserDefaults.standard.set(symbols, forKey: Self.userDefaultsKey)
return true
}
func removeSymbol(_ symbol: String) {
symbols.removeAll { $0 == symbol.uppercased() }
UserDefaults.standard.set(symbols, forKey: Self.userDefaultsKey)
}
}

View File

@@ -16,5 +16,6 @@ enum FeedItemType: String, Codable, CaseIterable {
case nowPlaying = "NOW_PLAYING" case nowPlaying = "NOW_PLAYING"
case currentWeather = "CURRENT_WEATHER" case currentWeather = "CURRENT_WEATHER"
case calendarEvent = "CALENDAR_EVENT" case calendarEvent = "CALENDAR_EVENT"
case stock = "STOCK"
case allQuiet = "ALL_QUIET" case allQuiet = "ALL_QUIET"
} }

View File

@@ -42,6 +42,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
private let weatherDataSource = WeatherDataSource() private let weatherDataSource = WeatherDataSource()
private let calendarDataSource = CalendarDataSource() private let calendarDataSource = CalendarDataSource()
private let poiDataSource = POIDataSource() private let poiDataSource = POIDataSource()
private let stockDataSource = StockDataSource()
private let ranker: HeuristicRanker private let ranker: HeuristicRanker
private let store: FeedStore private let store: FeedStore
private let server: LocalServer private let server: LocalServer
@@ -253,10 +254,15 @@ final class ContextOrchestrator: NSObject, ObservableObject {
async let poiResult = withTimeoutResult(seconds: 6) { async let poiResult = withTimeoutResult(seconds: 6) {
try await self.poiDataSource.data(for: location, now: nowEpoch) try await self.poiDataSource.data(for: location, now: nowEpoch)
} }
let stockSymbols = UserDefaults.standard.stringArray(forKey: StockSettingsStore.userDefaultsKey) ?? []
async let stockResult = withTimeoutResult(seconds: 6) {
try await self.stockDataSource.dataWithDiagnostics(symbols: stockSymbols, now: nowEpoch)
}
let wxRes = await weatherResult let wxRes = await weatherResult
let calRes = await calendarResult let calRes = await calendarResult
let poiRes = await poiResult let poiRes = await poiResult
let stockRes = await stockResult
func calendarTTL(endAt: Int, now: Int) -> Int { func calendarTTL(endAt: Int, now: Int) -> Int {
let ttl = endAt - now let ttl = endAt - now
@@ -272,6 +278,7 @@ final class ContextOrchestrator: NSObject, ObservableObject {
var rightNowCandidates: [HeuristicRanker.Ranked] = [] var rightNowCandidates: [HeuristicRanker.Ranked] = []
var calendarItems: [FeedItem] = [] var calendarItems: [FeedItem] = []
var poiItems: [FeedItem] = [] var poiItems: [FeedItem] = []
var stockItems: [FeedItem] = []
var weatherNowItem: FeedItem? = nil var weatherNowItem: FeedItem? = nil
var fetchFailed = false var fetchFailed = false
var wxDiagnostics: [String: String] = [:] var wxDiagnostics: [String: String] = [:]
@@ -434,13 +441,26 @@ final class ContextOrchestrator: NSObject, ObservableObject {
} }
} }
switch stockRes {
case .success(let snapshot):
for quote in snapshot.data.quotes.prefix(3) {
let item = stockQuoteToFeedItem(quote: quote, now: nowEpoch)
stockItems.append(item)
}
if !snapshot.data.quotes.isEmpty {
logger.info("stock quotes fetched count=\(snapshot.data.quotes.count)")
}
case .failure(let error):
logger.warning("stock fetch failed: \(String(describing: error), privacy: .public)")
}
let elapsedMs = Int(Date().timeIntervalSince(start) * 1000) let elapsedMs = Int(Date().timeIntervalSince(start) * 1000)
lastPipelineElapsedMs = elapsedMs lastPipelineElapsedMs = elapsedMs
lastFetchFailed = fetchFailed lastFetchFailed = fetchFailed
lastWeatherDiagnostics = wxDiagnostics lastWeatherDiagnostics = wxDiagnostics
lastCalendarDiagnostics = calDiagnostics lastCalendarDiagnostics = calDiagnostics
logger.info("pipeline right_now_candidates=\(rightNowCandidates.count) calendar_items=\(calendarItems.count) poi_items=\(poiItems.count) fetchFailed=\(fetchFailed) elapsed_ms=\(elapsedMs)") logger.info("pipeline right_now_candidates=\(rightNowCandidates.count) calendar_items=\(calendarItems.count) poi_items=\(poiItems.count) stock_items=\(stockItems.count) fetchFailed=\(fetchFailed) elapsed_ms=\(elapsedMs)")
if fetchFailed, rightNowCandidates.isEmpty, calendarItems.isEmpty, weatherNowItem == nil { if fetchFailed, rightNowCandidates.isEmpty, calendarItems.isEmpty, weatherNowItem == nil {
let fallbackFeed = store.getFeed(now: nowEpoch) let fallbackFeed = store.getFeed(now: nowEpoch)
@@ -515,6 +535,27 @@ final class ContextOrchestrator: NSObject, ObservableObject {
fyi.append(weatherNowItem) fyi.append(weatherNowItem)
} }
let fyiStocks = stockItems
.filter { $0.id != winnerItem.id }
.filter { !store.isSuppressed(id: $0.id, type: $0.type, now: nowEpoch) }
.prefix(3)
fyi.append(contentsOf: fyiStocks.map { item in
FeedItem(
id: item.id,
type: item.type,
title: item.title.truncated(maxLength: TextConstraints.titleMax),
subtitle: item.subtitle.truncated(maxLength: TextConstraints.subtitleMax),
priority: min(max(item.priority, 0.0), 1.0),
ttlSec: max(1, item.ttlSec),
condition: item.condition,
startsAt: item.startsAt,
poiType: item.poiType,
bucket: .fyi,
actions: ["DISMISS"]
)
})
let items = [winnerItem] + fyi let items = [winnerItem] + fyi
let feedEnvelope = FeedEnvelope( let feedEnvelope = FeedEnvelope(
schema: 1, schema: 1,
@@ -596,6 +637,34 @@ final class ContextOrchestrator: NSObject, ObservableObject {
} }
return "\(Int(meters.rounded())) m" return "\(Int(meters.rounded())) m"
} }
private func stockQuoteToFeedItem(quote: StockDataSource.StockQuote, now: Int) -> FeedItem {
let direction = quote.change >= 0 ? "+" : ""
let priceStr: String
if quote.price >= 100000 {
priceStr = String(format: "%.1fK", quote.price / 1000)
} else if quote.price >= 1000 {
priceStr = String(format: "%.0f", quote.price)
} else {
priceStr = String(format: "%.2f", quote.price)
}
let title = "\(quote.symbol) $\(priceStr)"
let subtitle = "\(direction)\(String(format: "%.2f", quote.change)) (\(direction)\(String(format: "%.2f", quote.changePercent))%)"
return FeedItem(
id: "stock:\(quote.symbol):\(now / 300)",
type: .stock,
title: title.truncated(maxLength: TextConstraints.titleMax),
subtitle: subtitle.truncated(maxLength: TextConstraints.subtitleMax),
priority: 0.3,
ttlSec: 600,
condition: nil,
startsAt: nil,
poiType: nil,
bucket: .fyi,
actions: ["DISMISS"]
)
}
} }
extension ContextOrchestrator: CLLocationManagerDelegate { extension ContextOrchestrator: CLLocationManagerDelegate {

View File

@@ -0,0 +1,104 @@
//
// StockSettingsView.swift
// iris
//
import SwiftUI
struct StockSettingsView: View {
@ObservedObject var store: StockSettingsStore
@State private var newSymbol: String = ""
@State private var showError: Bool = false
@State private var errorMessage: String = ""
var body: some View {
Form {
Section {
ForEach(store.symbols, id: \.self) { symbol in
HStack {
Text(symbol)
.font(.body.monospaced())
Spacer()
Button(role: .destructive) {
store.removeSymbol(symbol)
} label: {
Image(systemName: "trash")
.foregroundStyle(.red)
}
.buttonStyle(.plain)
}
}
.onDelete { indexSet in
for index in indexSet {
store.removeSymbol(store.symbols[index])
}
}
if store.symbols.count < 5 {
HStack {
TextField("Symbol (e.g. AAPL)", text: $newSymbol)
.textInputAutocapitalization(.characters)
.autocorrectionDisabled()
.font(.body.monospaced())
Button("Add") {
addSymbol()
}
.disabled(newSymbol.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty)
}
}
} header: {
Text("Stock Symbols")
} footer: {
Text("Enter up to 5 stock symbols. Cards appear in the FYI section.")
}
Section {
VStack(alignment: .leading, spacing: 8) {
Text("Examples:")
.font(.subheadline.bold())
Text("AAPL - Apple Inc.")
Text("GOOGL - Alphabet Inc.")
Text("^GSPC - S&P 500 Index")
Text("^DJI - Dow Jones")
Text("^IXIC - NASDAQ Composite")
}
.font(.caption)
.foregroundStyle(.secondary)
}
}
.navigationTitle("Stocks")
.alert("Error", isPresented: $showError) {
Button("OK") {}
} message: {
Text(errorMessage)
}
}
private func addSymbol() {
let cleaned = newSymbol.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
guard !cleaned.isEmpty else { return }
if store.symbols.contains(cleaned) {
errorMessage = "Symbol '\(cleaned)' already exists."
showError = true
return
}
if store.symbols.count >= 5 {
errorMessage = "Maximum of 5 symbols allowed."
showError = true
return
}
store.addSymbol(cleaned)
newSymbol = ""
}
}
struct StockSettingsView_Previews: PreviewProvider {
static var previews: some View {
NavigationStack {
StockSettingsView(store: StockSettingsStore())
}
}
}