// The generic verbs, as the Swift surfaces call them. One set for every table// by its id; the root's are the core's (`CoreRecords`, over cotton-ffi), and// `FixtureRecords` answers them from the bundled fixtures for tests and// previews only.importCottonKitimportCottonValuesimportFoundationpublicstructRecordPage: Sendable {publicvar records: [TableRecord]/// The page to ask for next, or nil when this was the last.publicvar next: Int?/// How many records every page of this read holds together, as the store/// answers it; nil from a store that does not say. A count is read from/// here, never by paging through the whole table.publicvar total: Int?/// A list read as a day plan (the core's `day_plan`: a calendar, a todo/// list) says every section it holds and how many stand in each, counted/// over the whole table; nil from a store that plans nothing.publicvar sections: [PlanSection]?/// How many records the same read finds archived, on a table that/// archives (S7: what is archived can be found again) — what the list's/// "Archived · n" link counts; nil from a store that does not say.publicvar archived: Int?/// A grouped list whose group says figures (`group.summary`): each/// group's head lines, by the value its rows hold in the group's column,/// as the core says them over the WHOLE group; nil from a store that/// says none.publicvar groupHeads: [String: [String]]?publicinit( records: [TableRecord], next: Int?, total: Int? = nil, sections: [PlanSection]? = nil, archived: Int? = nil, groupHeads: [String: [String]]? = nil ) {self.records = recordsself.next = nextself.total = totalself.sections = sectionsself.archived = archivedself.groupHeads = groupHeads }}/// One section of a planned read: its key (`coming`, `earlier`, `today`,/// `late`, `later`, `undated`, or `day` with the day), and its whole count.publicstructPlanSection: Hashable, Sendable {publicvar key: Stringpublicvar day: String?publicvar count: Intpublicinit(key: String, day: String? = nil, count: Int) {self.key = keyself.day = dayself.count = count }/// The key as the plan names the section ("day:2026-10-06" for a day).publicvar fullKey: String { key == "day" ? "day:\(day ?? "")" : key }}/// How a list asks to be read as the reader's day plan: the one section it/// wants, in the reader's zone. The core's `day_plan` takes it.publicstructPlanView: Hashable, Sendable {publicvar section: Stringpublicvar timezone: String/// The day, for the section `day` (one of the days ahead), YYYY-MM-DD.publicvar day: String?/// The day the plan is read from, YYYY-MM-DD — "coming" is that day and/// after — where a route names one (a date slip opens its own day, walker/// 298); nil reads from this device's today.publicvar today: String? = nilpublicinit(section: String, timezone: String = TimeZone.current.identifier, day: String? = nil) {self.section = sectionself.timezone = timezoneself.day = day }/// The key the plan's `sections` name this view by ("day:2026-10-06").publicvar key: String { day.map { "day:\($0)" } ?? section }/// The view that reads one of the plan's sections by its key.publicstaticfunc reading(key: String) -> PlanView {if key.hasPrefix("day:") { returnPlanView(section: "day", day: String(key.dropFirst(4))) }returnPlanView(section: key) }/// The where key a narrowed list carries its plan section under: not a/// column, so the store never filters by it (`RecordListModel.where`).publicstaticlet whereKey = "_section"/// A calendar's first page: today and after, soonest first.publicstaticlet coming = PlanView(section: "coming")/// A calendar's past, newest first, behind one closed heading.publicstaticlet earlier = PlanView(section: "earlier")}publicstructRecordLink: Hashable, Sendable {publiclet table: Stringpubliclet id: Stringpublicinit(table: String, id: String) { self.table = table; self.id = id }}extensionTableDefinition {/// Every record `records` link to, once: a single link's one id and each/// id of a several-record link (a chat's people, a photo's albums) — the/// tags of one whose titles were never asked for draw as nothing.publicfunc links(in records: [TableRecord]) -> [RecordLink] {// A column linking many (a photo's people, its albums) holds a list// of ids: every one is named, or its tags draw nothing.Array(Set(records.flatMap { record in columns.flatMap { column -> [RecordLink] inguard column.type == .link, let table = column.linkTo else { return [] }returnRecordFilter.options(record[column.key]).filter { !$0.isEmpty }.map { RecordLink(table: table, id: $0) } } })) }}publicprotocolRecords: AnyObject, Sendable {func tableDefinitions() asyncthrows -> [TableDefinition]/// Total rows in the table, independent of search, project and pagination.func count(table: String) asyncthrows -> Int/// Latest mutation timestamp, when the store supplies explicit metadata.func lastChanged(table: String) asyncthrows -> String?func toolDefinitions() asyncthrows -> [ToolDefinition]/// One page of `table`, narrowed to `project` and `query` when given, and/// to the records whose columns hold every value in `where` (column key →/// value; empty narrows nothing) — a conversation's messages by its id.func list(table: String, project: String?, query: String?, page: Int, where: [String: RecordValue]) asyncthrows -> RecordPage/// The same read, asking for at most `limit` records on the page — a/// capped section's few, a count's one. A store with a page size of its/// own answers its own page (the default below); `total` still counts/// the whole.func list( table: String, project: String?, query: String?, page: Int, where: [String: RecordValue], limit: Int ) asyncthrows -> RecordPage/// The same read kept to a Home `list` part's rows (`BoardRowKeep`): its/// `where`, and its `period` by the table's own date. A store that keeps/// no period refuses one by name (the default below), never answering/// the whole table as if it were the part.func list(table: String, project: String?, page: Int, keep: BoardRowKeep, limit: Int) asyncthrows -> RecordPage/// The same read as one section of the reader's day plan (`PlanView`):/// the core cuts a calendar into what is coming and what is earlier. A/// store that plans nothing answers the plain page (the default below).func list( table: String, project: String?, query: String?, page: Int, where: [String: RecordValue], limit: Int?, plan: PlanView ) asyncthrows -> RecordPage/// The same read as a tool's view reads it (`view`: `<tool>/<id>`): the/// core lists the table in the view's own order with its own slip/// (`ToolView::over`), one section of the day plan when `plan` names one./// A store that keeps no views answers the read without it (the default/// below).func list( table: String, project: String?, query: String?, page: Int, where: [String: RecordValue], limit: Int?, plan: PlanView?, view: String ) asyncthrows -> RecordPage/// Only the records put away (S7), newest put away first, narrowed as/// any read. A store that keeps the archived in its ordinary pages (the/// fixtures) answers the plain page and the list keeps what is archived.func listArchived( table: String, project: String?, query: String?, page: Int, where: [String: RecordValue] ) asyncthrows -> RecordPage/// How many records that archived read holds — the list's "Archived ·/// n" link; nil from a store that does not say.func archivedCount(table: String, project: String?, query: String?, where: [String: RecordValue]) asyncthrows -> Int?/// The figures `table`'s tile asks for (`TileLayout.summary`), computed/// by the core over every live record; empty when the tile asks for none.func summarize(table: String) asyncthrows -> [SummaryFigure]/// The points of `table`'s sparkline face (`TileLayout.sparkline`), worked/// out by the core; nil when the tile draws none.func sparkline(table: String) asyncthrows -> SparklineLine?/// The series of `table`'s chart face (`TileLayout.chart`), else of its/// chart view, worked out by the core over the window ending/// with `today`'s period; nil when the table draws no chart.func chart(table: String, today: String, at: String) asyncthrows -> ChartSeries?/// Resolves all targets of one surface page in one batch.func linkTitles(for links: [RecordLink]) asyncthrows -> LinkTitlesfunc get(table: String, id: String) asyncthrows -> TableRecord?func create(table: String, data: [String: RecordValue]) asyncthrows -> ChangeRecordfunc update(table: String, id: String, patch: [String: RecordValue]) asyncthrows -> ChangeRecordfunc delete(table: String, id: String) asyncthrows -> ChangeRecord/// The same writes to a record that repeats, saying how far they reach/// (`WriteScope`). A store that cannot carry the reach refuses rather/// than write the whole series silently (the default below).func update(table: String, id: String, patch: [String: RecordValue], scope: WriteScope?) asyncthrows -> ChangeRecordfunc delete(table: String, id: String, scope: WriteScope?) asyncthrows -> ChangeRecord/// Writes `ids` as the table's order, first to last; records `ids` does/// not name keep their order after them.func reorder(table: String, ids: [String]) asyncthrows -> ChangeRecordfunc link(table: String, id: String, column: String, target: String) asyncthrows -> ChangeRecordfunc undo(change: String) asyncthrows -> ChangeRecord}extensionRecords {/// A store with no core behind it has no figures to answer.publicfunc summarize(table: String) asyncthrows -> [SummaryFigure] { [] }publicfunc sparkline(table: String) asyncthrows -> SparklineLine? { nil }publicfunc chart(table: String, today: String, at: String) asyncthrows -> ChartSeries? { nil }publicfunc toolDefinitions() asyncthrows -> [ToolDefinition] { [] }/// Stores without a dedicated batch read scan each target table once,/// page by page; they never issue a get for each row.publicfunc linkTitles(for links: [RecordLink]) asyncthrows -> LinkTitles {guard !links.isEmpty else { returnLinkTitles() }let definitions = tryawait tableDefinitions()var titles: [String: String] = [:]var pictures: [String: String] = [:]for table inSet(links.map(\.table)) {guardlet definition = definitions.first(where: { $0.id == table }) else { continue }let ids = Set(links.filter { $0.table == table }.map(\.id))var next: Int? = 0whilelet page = next {let rows = tryawait list(table: table, project: nil, query: nil, page: page)for row in rows.records where ids.contains(row.id) { titles[row.id] = row.said?.slip.name ?? row[definition.title].text pictures[row.id] = LinkTitles.picture(of: row, in: definition) } next = ids.allSatisfy { titles[$0] != nil } ? nil : rows.next } }returnLinkTitles(titles, pictures: pictures) }publicfunc list( table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue], limit: Int ) asyncthrows -> RecordPage {tryawait list(table: table, project: project, query: query, page: page, where: filter) }publicfunc listArchived( table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue] ) asyncthrows -> RecordPage {tryawait list(table: table, project: project, query: query, page: page, where: filter) }publicfunc archivedCount( table: String, project: String?, query: String?, where filter: [String: RecordValue] ) asyncthrows -> Int? { nil }/// The same read as one section of the reader's day plan. A store that/// plans nothing (the fixtures) answers the plain page and no sections.publicfunc list( table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue], limit: Int?, plan: PlanView ) asyncthrows -> RecordPage {iflet limit {returntryawait list(table: table, project: project, query: query, page: page, where: filter, limit: limit) }returntryawait list(table: table, project: project, query: query, page: page, where: filter) }/// A tool view's read, from a store that keeps no views: the read itself.publicfunc list( table: String, project: String?, query: String?, page: Int, where filter: [String: RecordValue], limit: Int?, plan: PlanView?, view: String ) asyncthrows -> RecordPage {iflet plan {returntryawait list(table: table, project: project, query: query, page: page, where: filter, limit: limit, plan: plan) }iflet limit {returntryawait list(table: table, project: project, query: query, page: page, where: filter, limit: limit) }returntryawait list(table: table, project: project, query: query, page: page, where: filter) }/// How many records a narrowed read holds, from the store's `total` on a/// page of one. Nil from a store that answers no total.publicfunc total(table: String, query: String? = nil, where filter: [String: RecordValue]) asyncthrows -> Int? {tryawait list(table: table, project: nil, query: query, page: 0, where: filter, limit: 1).total }/// One page of the whole table, narrowed only by project and search.publicfunc list(table: String, project: String?, query: String?, page: Int) asyncthrows -> RecordPage {tryawait list(table: table, project: project, query: query, page: page, where: [:]) }publicfunc list(table: String, project: String?, page: Int, keep: BoardRowKeep, limit: Int) asyncthrows -> RecordPage {guard keep.period == nilelse { throwRecordsPeriodUnread(table: table) }returntryawait list(table: table, project: project, query: nil, page: page, where: keep.filter, limit: limit) }}extensionRecords {publicfunc update(table: String, id: String, patch: [String: RecordValue], scope: WriteScope?) asyncthrows -> ChangeRecord {guard scope == nilelse { throwScopeNotCarried() }returntryawait update(table: table, id: id, patch: patch) }publicfunc delete(table: String, id: String, scope: WriteScope?) asyncthrows -> ChangeRecord {guard scope == nilelse { throwScopeNotCarried() }returntryawait delete(table: table, id: id) }}/// A store that keeps no period was asked for a list kept to one.structRecordsPeriodUnread: Error, Equatable {publiclet table: String}publicenumRecordsError: Error, Equatable {case unknownTable(String)case unknownRecord(String)case unknownChange(String)case noOrderColumn(String)}extensionRecords {publicfunc lastChanged(table: String) asyncthrows -> String? { nil }}/// What `create` answers when the store needs one more value before it can/// make the record: its question, the column the answer goes in, and the/// answers it offers (each a value to write and its words). Nothing was made./// The words are the store's, never composed here; `table` and `data` are the/// create it was asked about, so an answer re-sends that same create with/// `data[column]` set (`answered`).publicstructCreateQuestion: Error, Hashable, Sendable, Decodable {publicstructOption: Hashable, Sendable, Decodable, Identifiable {publicvar id: Stringpublicvar label: Stringpublicinit(id: String, label: String) {self.id = idself.label = label } }publicvar question: Stringpublicvar column: Stringpublicvar options: [Option]/// The table the create was for.publicvar table: String = ""/// The values the create was sent with.publicvar data: [String: RecordValue] = [:]/// The words the create was made from, when it came from the one input.publicvar words: String = ""/// A create command opens its resulting record after the question is answered.publicvar openCreated = falsepublicinit(question: String, column: String, options: [Option], table: String = "", data: [String: RecordValue] = [:]) {self.question = questionself.column = columnself.options = optionsself.table = tableself.data = data }enumCodingKeys: String, CodingKey { case question, column, options, table, data }publicinit(from decoder: Decoder) throws {let c = try decoder.container(keyedBy: CodingKeys.self) question = try c.decode(String.self, forKey: .question) column = try c.decode(String.self, forKey: .column) options = try c.decodeIfPresent([Option].self, forKey: .options) ?? [] table = try c.decodeIfPresent(String.self, forKey: .table) ?? "" data = try c.decodeIfPresent([String: RecordValue].self, forKey: .data) ?? [:] }/// The same create's values with `option` written into the asked column.publicfunc answered(_ option: Option) -> [String: RecordValue] {var values = data values[column] = .text(option.id)return values }}