| 1 | import Foundation |
| 2 | |
| 3 | /// The task page's card belongs to its creator. Unknown card kinds and |
| 4 | /// options stay on the web until the native client understands their meaning. |
| 5 | struct TaskDecision { |
| 6 | var card: ThinCard? |
| 7 | var choices: [DecisionView.Choice] = [] |
| 8 | init() {} |
| 9 | init(task: ThinTask, cards: [ThinCard], mine: Bool, owner: Bool) { |
| 10 | guard mine, owner, !["done", "cancelled", "merging"].contains(task.stage), |
| 11 | task.moving != true, task.stopping != true, task.copyHold != true, task.copyCanSkip != true else { return } |
| 12 | let openCards = cards.filter { $0.taskId == task.id && $0.isOpen } |
| 13 | .sorted { ($0.createdAt, $0.id) < ($1.createdAt, $1.id) } |
| 14 | card = openCards.last { ["merge", "direction", "held"].contains($0.kind) } |
| 15 | ?? openCards.last { $0.kind == "question" } |
| 16 | guard let card else { return } |
| 17 | if card.kind == "merge", task.kind == "explore" { self.card = nil; return } |
| 18 | // Questions carry model-written options, rendered by LiveQuestionCard. |
| 19 | guard card.kind != "question" else { return } |
| 20 | if let decision = Feed.decision(card, task: task), decision.open { choices = decision.choices; return } |
| 21 | let allowed: Set<String> |
| 22 | switch card.kind { |
| 23 | case "direction": allowed = ["build", "again", "drop"] |
| 24 | case "held": allowed = ["run_now", "wait"] |
| 25 | default: return |
| 26 | } |
| 27 | var result: [DecisionView.Choice] = [] |
| 28 | for value in card.options ?? [] { |
| 29 | guard allowed.contains(value), let label = Feed.webText("chat.card." + value) else { return } |
| 30 | result.append(.init(value: value, label: label, primary: value == "build" || value == "run_now")) |
| 31 | } |
| 32 | choices = result |
| 33 | } |
| 34 | func canAnswer(_ choice: String, note: String) -> Bool { |
| 35 | guard choices.contains(where: { $0.value == choice }), note.count <= 1000 else { return false } |
| 36 | return card?.kind != "direction" || choice != "again" || !note.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty |
| 37 | } |
| 38 | } |
| 39 | |