Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Core/Models/Workspace.swift3// Description: The agent workspace of a person: long-term memory, portfolio positions, scheduled watches, shared reports, usage.4import Foundation56public struct Memory: Sendable, Equatable, Identifiable, Codable {7 public var id: String8 public var content: String9 public var category: String10 public var createdAt: Double11 public var created: Date { Date(timeIntervalSince1970: createdAt / 1000) }12 public static let categories = ["profil", "preference", "instruction", "suivi"]13 public static func categoryTitle(_ c: String) -> String {14 switch c { case "profil": "Profile"; case "preference": "Preference"; case "instruction": "Instruction"; case "suivi": "Follow-up"; default: c }15 }16}1718public struct Position: Sendable, Equatable, Identifiable, Codable {19 public var symbol: String20 public var quantity: Double21 public var costBasis: Double?22 public var note: String?23 public var updatedAt: Double24 public var id: String { symbol }25 public init(symbol: String, quantity: Double, costBasis: Double?, note: String?, updatedAt: Double) { self.symbol = symbol; self.quantity = quantity; self.costBasis = costBasis; self.note = note; self.updatedAt = updatedAt }26}2728public struct Watch: Sendable, Equatable, Identifiable, Codable {29 public var id: String30 public var title: String31 public var prompt: String32 public var model: String33 public var deep: Bool34 /// 24 h "HH:mm", America/New_York (the server's scheduler zone)35 public var time: String36 /// "weekdays" or "daily"37 public var days: String38 public var enabled: Bool39 public var lastRunAt: Double?40 public var lastSessionId: String?41 public var lastStatus: String?42 public var lastRun: Date? { lastRunAt.map { Date(timeIntervalSince1970: $0 / 1000) } }43}4445public struct RecallStats: Sendable, Equatable, Codable {46 public var passages: Int47 public var conversations: Int48}4950public struct Workspace: Sendable, Equatable, Codable {51 public var user: Account52 public var memories: [Memory]53 public var positions: [Position]54 public var watches: [Watch]55 public var recall: RecallStats56}5758public struct SharedReportSummary: Sendable, Equatable, Identifiable, Codable {59 public var shareId: String60 public var question: String61 /// first 500 characters62 public var answer: String63 public var createdAt: String64 public var id: String { shareId }65 enum CodingKeys: String, CodingKey { case shareId, question, answer, createdAt }66}6768public struct SharedReport: Sendable, Equatable, Codable {69 public var question: String70 public var answer: String71 public var sources: [WebSource]72 public var createdAt: String73 enum CodingKeys: String, CodingKey { case question, answer, sources, createdAt }74 public init(from decoder: Decoder) throws {75 let c = try decoder.container(keyedBy: CodingKeys.self)76 question = try c.decode(String.self, forKey: .question)77 answer = try c.decode(String.self, forKey: .answer)78 sources = (try? c.decodeIfPresent([WebSource].self, forKey: .sources)) ?? []79 createdAt = (try? c.decode(String.self, forKey: .createdAt)) ?? ""80 }81}8283public struct UsageStats: Sendable, Equatable, Codable {84 public struct Sessions: Sendable, Equatable, Codable { public var total: Int; public var today: Int }85 public struct Tokens: Sendable, Equatable, Codable { public var total: Double; public var input: Double; public var output: Double }86 public struct Cost: Sendable, Equatable, Codable { public var total: Double; public var today: Double }87 public struct Errors: Sendable, Equatable, Codable { public var lastHour: Int; public var rate: Double }88 public var sessions: Sessions89 public var tokens: Tokens90 public var cost: Cost91 public var errors: Errors92}9394/// Market value of a portfolio from the server's positions and the latest quotes. A position without a quote is95/// reported as unpriced and excluded from the totals: nothing is assumed about its value.96public struct PortfolioValuation: Sendable, Equatable {97 public struct Line: Sendable, Equatable, Identifiable {98 public var position: Position99 public var quote: Quote?100 public var id: String { position.symbol }101 public var marketValue: Double? { quote.map { $0.price * position.quantity } }102 public var cost: Double? { position.costBasis.map { $0 * position.quantity } }103 public var unrealized: Double? { if let mv = marketValue, let c = cost { mv - c } else { nil } }104 public var unrealizedPercent: Double? { if let u = unrealized, let c = cost, c != 0 { u / abs(c) * 100 } else { nil } }105 public var dayChange: Double? { quote?.change.map { $0 * position.quantity } }106 public var weight: Double?107 }108 public var lines: [Line]109 public var marketValue: Double110 public var cost: Double?111 public var unrealized: Double?112 public var dayChange: Double113 public var unpriced: [String]114 /// The oldest quote timestamp among priced lines: the valuation is only as recent as its stalest price.115 public var asOf: Date?116117 public init(positions: [Position], quotes: [String: Quote]) {118 var lines = positions.map { Line(position: $0, quote: quotes[$0.symbol]) }119 let total = lines.compactMap(\.marketValue).reduce(0, +)120 for i in lines.indices { if let mv = lines[i].marketValue, total != 0 { lines[i].weight = mv / total * 100 } }121 self.lines = lines122 marketValue = total123 unpriced = lines.filter { $0.quote == nil }.map(\.position.symbol)124 let priced = lines.filter { $0.quote != nil }125 // cost and P&L are totals over priced lines that all carry a cost basis; otherwise they would mix scopes126 if !priced.isEmpty, priced.allSatisfy({ $0.cost != nil }) {127 let c = priced.compactMap(\.cost).reduce(0, +)128 cost = c; unrealized = total - c129 } else { cost = nil; unrealized = nil }130 dayChange = priced.compactMap(\.dayChange).reduce(0, +)131 asOf = priced.compactMap { $0.quote?.asOf }.min()132 }133}134