// Capture keeps the original thought in Personal with where it was said. // A parsed plan becomes an immutable proposal in the same local transaction; // the owner's separate approval performs the filing. A capture without a plan // stays a thought. import Foundation /// What the composer sends. **A LOCAL write, and it is finished** — there is no /// `/v1/route` call on this path, ever. /// /// `personIds` is deliberately absent and the crate REFUSES it rather than /// dropping it: a person edge hangs on the ROUTED RECORD, not on the capture, /// so there is no row that argument could write and accepting it would tell the /// app a link had been made. /// /// It lives here, beside the one actor that sends it, rather than in a boundary /// file of shapes: the ANSWERS crossing this boundary are untyped trees now /// (I3), and a REQUEST is still a thing this app composes and therefore still a /// type. public struct CaptureRequest: Encodable, Sendable { public var text: String? /// The object key of the recording, when the capture was spoken. The column /// is still called `audio_url` for historical reasons; the argument is /// named for what it actually carries. public var audioKey: String? /// The plan shown at send becomes an immutable proposal. Filing waits /// for the owner’s separate approval. public var plan: JSONValue? /// True when the person re-kinded, retitled or dropped a record before /// sending. Meaningless without a plan, and the crate stores NULL then: /// "they left the plan alone" and "there was nothing to leave alone" are /// different facts and the classifier reads the column. public var userEdited: Bool /// The `#project` chips. Ids, never slugs — a slug is a name the person can /// change, and the app already holds the id it drew the chip from. public var projectIds: [String] /// The pictures on the capture, in the order the composer holds them — /// each one a record `capture.media` already wrote, named by `{kind, id}` /// and never by bytes. The crate runs `update_note`'s own validator over /// the list and files it inside the SAME write as the row, so a picture /// this file does not hold refuses the whole capture rather than leaving a /// thought behind without its screenshot. /// /// Always encoded, `[]` included: "no pictures" is the ordinary capture, /// and the crate reads an empty list and an absent one the same way. /// /// **Pictures never stand in for words.** A capture with pictures and no /// text is refused below the boundary ("a capture needs text or audio"), /// which is why `DockModel.canSend` still reads the words alone. public var attachments: [CaptureAttachment] /// Where the thought was said (Spec 7): the tool's table when it was said /// inside one, and the record when it was said on a record's page. Both /// absent for a thought said on the database's own page. public var saidTable: String? public var saidRecordId: String? public init( text: String? = nil, audioKey: String? = nil, plan: JSONValue? = nil, userEdited: Bool = false, projectIds: [String] = [], attachments: [CaptureAttachment] = [], saidTable: String? = nil, saidRecordId: String? = nil ) { self.saidTable = saidTable self.saidRecordId = saidRecordId self.text = text self.audioKey = audioKey self.plan = plan self.userEdited = userEdited self.projectIds = projectIds self.attachments = attachments } } /// One entry of `capture.create`'s `attachments` — the pair `update_note` /// and `update_thought` take, and nothing else. /// /// **No `caption` key, deliberately.** An entry without one leaves the stored /// caption alone (`RecordAttachments.argument`'s rule), and a fresh capture has /// no caption to write; a person captions a picture where the picture is, not /// in the composer. public struct CaptureAttachment: Encodable, Sendable, Equatable { /// `photo`, `video` or `document` — `CaptureMediaKind`'s own words. public var kind: String /// The record `capture.media` answered (`MediaCaptured.recordId`). public var id: String public init(kind: CaptureMediaKind, id: String) { self.kind = kind.rawValue self.id = id } } /// A picture already in the file, waiting on a capture that has not been sent. /// /// What `capture.media` answered, kept whole: the record the capture will /// NAME (`recordId`), the object key it is stored under (`key`), and for a /// clip the key of its still (`posterKey`). A tile draws ``tileKey``: the /// photo's own key, or the clip's still — never the film's key, which no /// image decoder reads. /// /// **The row exists before the thought does, and that is the order the /// boundary asks for**: bytes into the store, the record written, and only /// then something pointing at it. A capture that is never sent leaves the /// picture in the library, which is where a photograph the person chose was /// always going to be. /// /// It lives in CottonKit because TWO lanes hand it on: the dock's own `+` /// fan, and the share sheet's picture branch (`ShareIntake.swift` branch 2), /// which lands on the composer carrying one. public struct CapturedPicture: Sendable, Equatable { public var kind: CaptureMediaKind public var recordId: String public var key: String public var posterKey: String? public init(kind: CaptureMediaKind, recordId: String, key: String, posterKey: String? = nil) { self.kind = kind self.recordId = recordId self.key = key self.posterKey = posterKey } /// What a tile draws: a photo's own key, a clip's still, and nothing for a /// clip with no still or a document — the tile then draws its window. public var tileKey: String? { switch kind { case .photo: key case .video: posterKey case .document: nil } } /// What `capture.create` is handed for it. public var attachment: CaptureAttachment { CaptureAttachment(kind: kind, id: recordId) } } /// What the capture dock (`DockModel`) needs to put a picture on the draft: a file on this /// device in, a record in the file out. /// /// A seam for `CaptureSending`'s reason — a dock test drives the attach path /// with no core, no image decoder and no scratch directory. The one /// implementation is ``CoreMediaCapture``, and it lives here in CottonKit /// beside the request it sends, because a type holding the core belongs below /// the screens (`ScaffoldSweepTests`, rule 3). public protocol MediaCapturing: Sendable { func capture( file url: URL, mime: String, kind: CaptureMediaKind, filename: String? ) async throws -> MediaCaptured } /// `capture.media`, with the renditions made first. /// /// The order the note editor attached in: the tiers are made /// HERE, from a file already on this device and off the main actor, so the /// tile has a thumbnail to draw and nothing touches the radio; then the crate /// copies the bytes and every tier into the store and writes the row. The /// scratch directory goes the moment this returns. The staged SOURCE file is /// the caller's to delete — it may be a file the person picked, which is /// theirs. public struct CoreMediaCapture: MediaCapturing { private let core: any CottonCalling /// Every picture this captures rides a NEW THOUGHT (`into: "thought"`): /// kept in Personal, where the thought arrives. Otherwise a picture is /// kept in the database `core` names — the file whatever names it is in. private let forThought: Bool public init(core: any CottonCalling, forThought: Bool = false) { self.core = core self.forThought = forThought } public func capture( file url: URL, mime: String, kind: CaptureMediaKind, filename: String? ) async throws -> MediaCaptured { try await send( CottonMethod.captureMedia, file: url, mime: mime, kind: kind, filename: filename, forThought: forThought) } /// `capture.media` with how its bytes go up: Albums' Keep sends a copy of a Photos original, Wi-Fi only unless he /// allowed mobile data (`CaptureUpload`). public func capture( file url: URL, mime: String, kind: CaptureMediaKind, filename: String?, upload: CaptureUpload ) async throws -> MediaCaptured { try await send( CottonMethod.captureMedia, file: url, mime: mime, kind: kind, filename: filename, forThought: forThought, upload: upload) } /// A Camera tool shot: the same flow through `camera.shot`, held to a /// photo or a video. public func shoot( file url: URL, mime: String, kind: CaptureMediaKind, filename: String? ) async throws -> MediaCaptured { try await send(CottonMethod.cameraShot, file: url, mime: mime, kind: kind, filename: filename) } private func send( _ method: String, file url: URL, mime: String, kind: CaptureMediaKind, filename: String?, forThought: Bool = false, upload: CaptureUpload = CaptureUpload() ) async throws -> MediaCaptured { let scratch = MediaTiers.scratchDirectory() defer { MediaTiers.discard(scratch) } let tiers = kind == .document ? MediaTiers.none : await MediaTiers.make(for: url, film: kind == .video, in: scratch) return try await core.call( method, CaptureMediaRequest( path: url.path(percentEncoded: false), mime: mime, kind: kind, filename: filename, tiers: tiers, forThought: forThought, upload: upload ), as: MediaCaptured.self ) } } /// Send a capture, and remember the chips it was carrying. /// /// An `actor` rather than a `@MainActor` class because it holds no UI state and /// both of its steps are awaits across the boundary: the write goes to the core /// (which blocks the calling thread inside Rust, so it must never be the main /// one — design P5), and the remembering goes to ``PendingLinks``, an actor of /// its own. A `@MainActor` class here would put the ordering of those two on the /// main thread for no reason, and the screen that calls this is already the /// thing that owns main-thread state. public actor CaptureService { private let core: any CottonCalling private let links: PendingLinks public init(core: any CottonCalling, links: PendingLinks) { self.core = core self.links = links } /// Write the capture, then remember the link intents. /// /// Throws whatever the boundary threw — the host shows the error toast and /// keeps the draft. There is no success toast: the capture appearing in the /// chat IS the receipt, and a toast for the ordinary case is a sentence /// nobody reads. /// /// `personIds` is a separate argument and NOT part of ``CaptureRequest`` /// because `capture.create` refuses it: a person edge hangs on the ROUTED /// RECORD, not on the capture, so there is no row that argument could write /// and accepting it would tell the app a link had been made. The person /// intents live in ``PendingLinks`` until the record exists, exactly as the /// project ones do — the difference is only that the projects ALSO ride the /// capture row, where the server's filer can see them. /// /// The answer is the tree `capture.create` wrote — `{ "id": … }` and /// nothing else. An answer that somehow carries no `id` reads as `""`, /// which is the same generosity the typed shape had and the same reason: /// the crate may land a field before this app reads it. @discardableResult public func capture(_ request: CaptureRequest, personIds: [String]) async throws -> JSONValue { let created = try await core.callJSON(CottonMethod.captureCreate, request) // The intent is filed under the identity that made it: it belongs to ONE // file, and a drain running as somebody else must never find it. That is // structural here — `links` was built for one identity — rather than a // pair of arguments passed on every call. // // It is remembered AFTER the write and only on success: a capture that // did not land has no id to hang an intent on, and inventing one would // leave a chip waiting a week for a record that was never going to exist. await links.remember( inboxItemId: created["id"]?.string ?? "", projectIds: request.projectIds, personIds: personIds ) // ── The lone-URL page shortcut would branch HERE ────────────────────── // // The previous app asked whether the text was a bare URL at this point // and, for a capture that is one and carries no plan, filed it as a page at // once — then drained the intents immediately, because unlike a routed // capture that record already existed. // // Neither half exists below the boundary today. The RULE (a capture whose // whole trimmed text is one http(s) URL is a page) has no Rust home; the // FILER does not exist at all — `cotton-tools`' catalogue has no page tool // and no `fileInboxItem` verb, so there is nothing below the boundary that // creates the page or stamps the capture routed. A tool name invented here // would be an `unknown_method` at runtime rather than a compile error. // // Landing `pending` is the correct fallback and not a degraded one: the // server's routing worker files the page when the op arrives, and the chips // remembered above are applied by the drain when it does. What is lost is // only the instant, model-free filing — so when the page tool lands, this // is the one place that changes. return created } }