Branchwell

Inkspring / app · inkspring.ai / native-mac / Sources / Inkspring / IdeaQueuePlan.swift

Raw 1.8 KB on 332c2458022ae8f6648ea05f3c733b528a595172
1import Foundation
2
3enum IdeaQueuePlan {
4 static let defaultTarget = 20
5 static let targets = [10, defaultTarget, 30, 40, 50]
6 private static let mix = ["trend", "work", "data", "trend", "work"]
7
8 static func target(_ preference: Any?) -> Int {
9 guard let value = preference as? Int, targets.contains(value) else { return defaultTarget }
10 return value
11 }
12
13 struct Slot {
14 let id: Int
15 let kind: String
16 var long = false
17
18 var fields: [String: Any] { ["slot": id, "kind": kind, "long": long] }
19 }
20
21 static func slots(open: [WorkspaceRecord], target: Int) -> [Slot] {
22 let count = max(0, target - open.count)
23 let order = (0..<target).map { mix[$0 % mix.count] }
24 let wanted = Dictionary(grouping: order, by: { $0 }).mapValues(\.count)
25 var current = Dictionary(grouping: open) { idea in
26 ["trend", "data"].contains(idea["kind"]) ? idea["kind"] : "work"
27 }.mapValues(\.count)
28 var slots = (0..<count).map { index in
29 let kind = if open.isEmpty {
30 order[index]
31 } else {
32 ["trend", "work", "data"].max { left, right in
33 wanted[left, default: 0] - current[left, default: 0] < wanted[right, default: 0] - current[right, default: 0]
34 } ?? "work"
35 }
36 current[kind, default: 0] += 1
37 return Slot(id: index + 1, kind: kind)
38 }
39 let existingLong = open.filter { $0.fields["long"] as? Bool == true }.count
40 let longNeeded = max(0, (target >= 20 ? 2 : 1) - existingLong)
41 for index in slots.indices.filter({ slots[$0].kind != "data" }).shuffled().prefix(longNeeded) {
42 slots[index].long = true
43 }
44 return slots
45 }
46}