mirror of
https://github.com/Louis-CFM/coucou.git
synced 2026-12-07 07:32:35 +09:00
The three blocks become one card: Most useful, Most used (week, month, year, all), Never used, Archived. Never used memories can be archived one by one or all at once (after a confirmation); Archived brings them back. brain archive / brain restore: an archived memory leaves the state files (so the hooks, MCP, search index, pages and galaxy no longer see it) and is kept in .state/archived.json. Saving a state leaves archived ids out, so a later ingestion can't bring one back. Nothing is deleted. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LJm3SRL9k3gBPaVtfPhwcT
349 lines
14 KiB
Swift
349 lines
14 KiB
Swift
#if !APPSTORE
|
||
import SwiftUI
|
||
|
||
// Memory › Usage: which memories the agents actually use (`brain usage --json`).
|
||
// brain counts every memory that reaches an agent (the session hook, the prompt hook, the MCP tools),
|
||
// on the Mac only. This page shows the ones that serve the most, the most useful ones, the ones that
|
||
// never served.
|
||
|
||
struct BrainUsageItem: Decodable, Identifiable {
|
||
let id: String
|
||
let kind: String
|
||
let text: String
|
||
let project: String
|
||
let date: String
|
||
let uses: Int
|
||
let sessions: Int
|
||
let picks: Int
|
||
let last: String?
|
||
}
|
||
|
||
struct BrainUsageReport: Decodable {
|
||
struct Day: Decodable { let date: String; let n: Int }
|
||
struct Project: Decodable { let name: String; let n: Int }
|
||
let since: String?
|
||
let memories: Int
|
||
let used: Int
|
||
let uses30: Int
|
||
let picks30: Int
|
||
let sessions30: Int
|
||
let days: [Day]
|
||
let top: [String: [BrainUsageItem]] // week, month, year, all
|
||
let useful: [BrainUsageItem]
|
||
let unused: [BrainUsageItem]
|
||
let unusedCount: Int
|
||
let archived: [BrainUsageItem]
|
||
let archivedCount: Int
|
||
let projects: [Project]
|
||
}
|
||
|
||
struct HubMemoryUsagePage: View {
|
||
@State private var report: BrainUsageReport?
|
||
@State private var loading = true
|
||
@State private var failed = false
|
||
@State private var list: UsageList = .useful
|
||
@State private var period: UsagePeriod = .month
|
||
@State private var shown = 25
|
||
@State private var working: Set<String> = []
|
||
@State private var confirmAll = false
|
||
|
||
enum UsageList: String, CaseIterable, Identifiable {
|
||
case useful, used, never, archived
|
||
var id: String { rawValue }
|
||
var label: String {
|
||
switch self {
|
||
case .useful: return String(localized: "hub.memory.usage.useful")
|
||
case .used: return String(localized: "hub.memory.usage.top")
|
||
case .never: return String(localized: "hub.memory.usage.never")
|
||
case .archived: return String(localized: "hub.memory.usage.archived")
|
||
}
|
||
}
|
||
}
|
||
|
||
enum UsagePeriod: String, CaseIterable, Identifiable {
|
||
case week, month, year, all
|
||
var id: String { rawValue }
|
||
var label: String {
|
||
switch self {
|
||
case .week: return String(localized: "hub.period.week")
|
||
case .month: return String(localized: "hub.period.month")
|
||
case .year: return String(localized: "hub.memory.usage.period.year")
|
||
case .all: return String(localized: "hub.period.all")
|
||
}
|
||
}
|
||
}
|
||
|
||
var body: some View {
|
||
HubPageScaffold(title: String(localized: "hub.page.memory-usage"),
|
||
subtitle: String(localized: "hub.memory.usage.subtitle")) {
|
||
if loading {
|
||
ProgressView()
|
||
.frame(maxWidth: .infinity, alignment: .center)
|
||
.padding(.top, 40)
|
||
} else if let r = report, r.used > 0 || r.archivedCount > 0 {
|
||
content(r)
|
||
} else {
|
||
HubEmptyState(text: failed ? String(localized: "hub.memory.usage.error") : String(localized: "hub.memory.usage.empty"),
|
||
state: .sleeping, color: "#22D3EE")
|
||
}
|
||
}
|
||
.task { await load() }
|
||
}
|
||
|
||
private func load() async {
|
||
do {
|
||
report = try await BrainRunner.shared.run(["usage", "--json"], as: BrainUsageReport.self)
|
||
failed = false
|
||
} catch {
|
||
failed = true
|
||
}
|
||
loading = false
|
||
}
|
||
|
||
/// Archive (or bring back) memories, then read the page again.
|
||
private func move(_ ids: [String], restore: Bool) {
|
||
guard !ids.isEmpty else { return }
|
||
working.formUnion(ids)
|
||
Task {
|
||
_ = try? await BrainRunner.shared.run([restore ? "restore" : "archive", "--json"] + ids)
|
||
await load()
|
||
working.subtract(ids)
|
||
if list == .archived, (report?.archivedCount ?? 0) == 0 { list = .never }
|
||
}
|
||
}
|
||
|
||
@ViewBuilder
|
||
private func content(_ r: BrainUsageReport) -> some View {
|
||
let share = Double(r.used) / Double(max(1, r.memories))
|
||
HStack(spacing: 14) {
|
||
HubStatCard(label: String(localized: "hub.memory.usage.uses"),
|
||
value: "\(r.uses30)",
|
||
note: String(format: String(localized: "hub.memory.usage.uses-note"), r.sessions30))
|
||
HubStatCard(label: String(localized: "hub.memory.usage.served"),
|
||
value: share.formatted(.percent.precision(.fractionLength(0))),
|
||
note: String(format: String(localized: "hub.memory.usage.served-note"), r.used, r.memories))
|
||
HubStatCard(label: String(localized: "hub.memory.usage.picks"),
|
||
value: "\(r.picks30)",
|
||
note: String(localized: "hub.memory.usage.picks-note"))
|
||
HubStatCard(label: String(localized: "hub.memory.usage.never"),
|
||
value: "\(r.unusedCount)",
|
||
note: String(localized: "hub.memory.usage.never-note"))
|
||
}
|
||
HStack(alignment: .top, spacing: 14) {
|
||
HubCard(padding: 20) {
|
||
Text("hub.memory.usage.per-day").font(.system(size: 14, weight: .bold))
|
||
HubBars(days: r.days.map { (date: Self.day($0.date), words: $0.n) })
|
||
.frame(height: 170)
|
||
}
|
||
.frame(maxWidth: .infinity)
|
||
HubCard(padding: 20) {
|
||
Text("hub.memory.usage.projects").font(.system(size: 14, weight: .bold))
|
||
HubAppBars(apps: Dictionary(r.projects.map { ($0.name, $0.n) }, uniquingKeysWith: +),
|
||
empty: "hub.memory.usage.projects-empty")
|
||
}
|
||
.frame(width: 300)
|
||
}
|
||
listCard(r)
|
||
}
|
||
|
||
// MARK: The list: one card, a selector on top
|
||
|
||
@ViewBuilder
|
||
private func listCard(_ r: BrainUsageReport) -> some View {
|
||
let tabs = UsageList.allCases.filter { $0 != .archived || r.archivedCount > 0 }
|
||
HubCard(padding: 18) {
|
||
HStack(alignment: .center, spacing: 12) {
|
||
Picker("", selection: $list) {
|
||
ForEach(tabs) { Text($0.label).tag($0) }
|
||
}
|
||
.pickerStyle(.segmented)
|
||
.labelsHidden()
|
||
.fixedSize()
|
||
Spacer(minLength: 8)
|
||
if list == .used {
|
||
Picker("", selection: $period) {
|
||
ForEach(UsagePeriod.allCases) { Text($0.label).tag($0) }
|
||
}
|
||
.pickerStyle(.menu)
|
||
.labelsHidden()
|
||
.fixedSize()
|
||
} else if list == .never, !r.unused.isEmpty {
|
||
Button(String(format: String(localized: "hub.memory.usage.archive-all"), r.unused.count)) {
|
||
confirmAll = true
|
||
}
|
||
.buttonStyle(HubButtonStyle())
|
||
.disabled(!working.isEmpty)
|
||
} else if list == .archived, !r.archived.isEmpty {
|
||
Button(String(localized: "hub.memory.usage.restore-all")) {
|
||
move(r.archived.map(\.id), restore: true)
|
||
}
|
||
.buttonStyle(HubButtonStyle())
|
||
.disabled(!working.isEmpty)
|
||
}
|
||
}
|
||
if let line = note(r) {
|
||
Text(line)
|
||
.font(.system(size: 12))
|
||
.foregroundColor(HubTheme.secondary)
|
||
.fixedSize(horizontal: false, vertical: true)
|
||
}
|
||
let rows = items(r)
|
||
if rows.isEmpty {
|
||
Text(String(localized: "hub.memory.usage.list-empty"))
|
||
.font(.system(size: 13))
|
||
.foregroundColor(HubTheme.secondary)
|
||
.padding(.vertical, 8)
|
||
} else {
|
||
VStack(spacing: 0) {
|
||
ForEach(Array(rows.prefix(shown).enumerated()), id: \.element.id) { i, u in
|
||
if i > 0 { HubDivider().padding(.horizontal, 10) }
|
||
UsageRow(item: u, stat: stat(u, r), action: action(u), busy: working.contains(u.id))
|
||
}
|
||
}
|
||
.padding(.horizontal, -12)
|
||
if rows.count > shown {
|
||
Button(String(format: String(localized: "hub.memory.usage.more"), rows.count - shown)) { shown += 25 }
|
||
.buttonStyle(.plain)
|
||
.font(.system(size: 12.5, weight: .semibold))
|
||
.foregroundColor(Color(hex: "#22D3EE"))
|
||
.padding(.top, 2)
|
||
}
|
||
}
|
||
}
|
||
.onChange(of: list) { shown = 25 }
|
||
.alert(String(format: String(localized: "hub.memory.usage.archive-confirm-title"), r.unused.count), isPresented: $confirmAll) {
|
||
Button(String(localized: "hub.memory.usage.archive")) { move(r.unused.map(\.id), restore: false) }
|
||
Button(String(localized: "hub.memory.settings.wipe.cancel"), role: .cancel) {}
|
||
} message: {
|
||
Text(String(localized: "hub.memory.usage.archive-confirm-message"))
|
||
}
|
||
}
|
||
|
||
private func items(_ r: BrainUsageReport) -> [BrainUsageItem] {
|
||
switch list {
|
||
case .useful: return r.useful
|
||
case .used: return r.top[period.rawValue] ?? []
|
||
case .never: return r.unused
|
||
case .archived: return r.archived
|
||
}
|
||
}
|
||
|
||
private func note(_ r: BrainUsageReport) -> String? {
|
||
switch list {
|
||
case .useful: return String(localized: "hub.memory.usage.useful-detail")
|
||
case .used: return nil
|
||
case .never: return String(format: String(localized: "hub.memory.usage.never-detail"), r.unusedCount)
|
||
case .archived: return String(localized: "hub.memory.usage.archived-detail")
|
||
}
|
||
}
|
||
|
||
private func stat(_ u: BrainUsageItem, _ r: BrainUsageReport) -> String {
|
||
switch list {
|
||
case .useful:
|
||
return u.picks > 0
|
||
? String(format: String(localized: "hub.memory.usage.sessions-picks"), u.sessions, u.picks)
|
||
: String(format: String(localized: "hub.memory.usage.sessions"), u.sessions)
|
||
case .used: return String(format: String(localized: "hub.memory.usage.times"), u.uses)
|
||
case .never, .archived: return Self.shortDate(u.date)
|
||
}
|
||
}
|
||
|
||
private func action(_ u: BrainUsageItem) -> UsageRow.Action? {
|
||
switch list {
|
||
case .never: return .init(symbol: "archivebox", help: String(localized: "hub.memory.usage.archive")) { move([u.id], restore: false) }
|
||
case .archived: return .init(symbol: "arrow.uturn.backward", help: String(localized: "hub.memory.usage.restore")) { move([u.id], restore: true) }
|
||
default: return nil
|
||
}
|
||
}
|
||
|
||
private static let parser: DateFormatter = {
|
||
let f = DateFormatter()
|
||
f.dateFormat = "yyyy-MM-dd"
|
||
f.locale = Locale(identifier: "en_US_POSIX")
|
||
return f
|
||
}()
|
||
|
||
static func day(_ s: String) -> Date { parser.date(from: String(s.prefix(10))) ?? Date() }
|
||
|
||
static func shortDate(_ s: String) -> String {
|
||
guard let d = parser.date(from: String(s.prefix(10))) else { return s }
|
||
return d.formatted(.dateTime.day().month(.abbreviated).year())
|
||
}
|
||
}
|
||
|
||
/// One memory: its kind, the text, its project, and the number that ranks it.
|
||
private struct UsageRow: View {
|
||
struct Action { let symbol: String; let help: String; let run: () -> Void }
|
||
let item: BrainUsageItem
|
||
let stat: String
|
||
var action: Action? = nil
|
||
var busy = false
|
||
@State private var hover = false
|
||
|
||
var body: some View {
|
||
HStack(alignment: .top, spacing: 12) {
|
||
Text(Self.kindTitle(item.kind))
|
||
.font(.system(size: 10, weight: .bold))
|
||
.padding(.horizontal, 6)
|
||
.padding(.vertical, 2)
|
||
.background(RoundedRectangle(cornerRadius: 4).fill(Self.kindColor(item.kind).opacity(0.2)))
|
||
.foregroundColor(Self.kindColor(item.kind))
|
||
.frame(width: 84, alignment: .leading)
|
||
VStack(alignment: .leading, spacing: 4) {
|
||
Text(verbatim: item.text)
|
||
.font(.system(size: 13))
|
||
.foregroundColor(HubTheme.body)
|
||
.lineLimit(hover ? 6 : 2)
|
||
.frame(maxWidth: .infinity, alignment: .leading)
|
||
Text(verbatim: item.project)
|
||
.font(.system(size: 11.5))
|
||
.foregroundColor(HubTheme.muted)
|
||
}
|
||
Text(stat)
|
||
.font(.system(size: 12, weight: .semibold))
|
||
.foregroundColor(HubTheme.secondary)
|
||
.monospacedDigit()
|
||
.lineLimit(1)
|
||
.fixedSize()
|
||
if let action {
|
||
if busy {
|
||
ProgressView().controlSize(.small).frame(width: 28, height: 28)
|
||
} else {
|
||
HubIconButton(symbol: action.symbol, help: action.help, action: action.run)
|
||
.opacity(hover ? 1 : 0.35)
|
||
}
|
||
}
|
||
}
|
||
.opacity(busy ? 0.5 : 1)
|
||
.padding(.horizontal, 12)
|
||
.padding(.vertical, 10)
|
||
.background(RoundedRectangle(cornerRadius: 9).fill(Color.white.opacity(hover ? 0.035 : 0)))
|
||
.contentShape(Rectangle())
|
||
.onHover { h in withAnimation(.easeOut(duration: 0.15)) { hover = h } }
|
||
.help(item.text)
|
||
}
|
||
|
||
static func kindTitle(_ kind: String) -> String {
|
||
switch kind {
|
||
case "décision": return GalaxyKind.decision.title
|
||
case "leçon": return GalaxyKind.lecon.title
|
||
case "fait": return GalaxyKind.fait.title
|
||
case "préférence": return GalaxyKind.pref.title
|
||
case "tâche": return String(localized: "hub.memory.usage.kind.task")
|
||
default: return String(localized: "hub.memory.usage.kind.other")
|
||
}
|
||
}
|
||
|
||
static func kindColor(_ kind: String) -> Color {
|
||
switch kind {
|
||
case "décision": return Color(hex: "#A78BFA")
|
||
case "leçon": return Color(hex: "#34D399")
|
||
case "fait": return Color(hex: "#F7B32B")
|
||
case "préférence": return Color(hex: "#F472B6")
|
||
case "tâche": return Color(hex: "#3B9EFF")
|
||
default: return HubTheme.secondary
|
||
}
|
||
}
|
||
}
|
||
#endif
|