Cotton code
1080 lines · 28.3 KB · JSONRaw
{  "format_version": 1,  "id": "document",  "name": "Document",  "plural": "Documents",  "builtin": true,  "visibility": "owner",  "date": "document_date",  "title": "title",  "columns": [    {      "key": "title",      "type": "text",      "readonly": false    },    {      "key": "original_name",      "name": "File name",      "type": "text",      "readonly": true    },    {      "key": "mime",      "name": "Type",      "type": "text",      "readonly": true    },    {      "key": "byte_size",      "name": "Size",      "type": "number",      "format": "bytes",      "readonly": true    },    {      "key": "sha256",      "type": "text",      "readonly": true    },    {      "key": "storage_path",      "name": "File",      "type": "file",      "readonly": true,      "presentation": "file"    },    {      "key": "decrypted_path",      "type": "text",      "readonly": true    },    {      "key": "password",      "type": "text",      "readonly": true    },    {      "key": "page_count",      "name": "Pages",      "type": "number",      "readonly": true,      "units": {        "one": "page",        "other": "pages",        "zero": "0 pages"      }    },    {      "key": "status",      "type": "choice",      "choices": [        "processing",        "summarizing",        "ready",        "locked",        "failed"      ],      "labels": {        "processing": "Being read",        "summarizing": "Being summarised",        "ready": "Ready",        "locked": "Locked — needs its password",        "failed": "Could not be read"      },      "readonly": true    },    {      "key": "failed_stage",      "name": "Failed at",      "type": "choice",      "choices": [        "unlock",        "extract",        "summarize",        "land"      ],      "readonly": true    },    {      "key": "error_code",      "name": "Problem",      "type": "text",      "readonly": true    },    {      "key": "status_changed_at",      "type": "date",      "format": "calendar-datetime",      "readonly": true    },    {      "key": "doc_type",      "name": "Kind",      "type": "choice",      "choices": [        "bank_statement",        "card_statement",        "invoice",        "receipt",        "tax",        "id",        "insurance",        "medical",        "legal",        "ticket",        "statement_other",        "letter",        "manual",        "other"      ],      "labels": {        "bank_statement": "Bank statement",        "card_statement": "Card statement",        "invoice": "Invoice",        "receipt": "Receipt",        "tax": "Tax",        "id": "ID",        "insurance": "Insurance",        "medical": "Medical",        "legal": "Legal",        "ticket": "Ticket",        "statement_other": "Statement",        "letter": "Letter",        "manual": "Manual",        "other": "Other"      },      "readonly": true    },    {      "key": "document_date",      "name": "Dated",      "type": "date",      "readonly": true    },    {      "key": "period_start",      "name": "Covers from",      "type": "date",      "readonly": true    },    {      "key": "period_end",      "name": "Covers to",      "type": "date",      "readonly": true    },    {      "key": "issuer",      "name": "From",      "type": "text",      "readonly": true    },    {      "key": "summary",      "name": "Reading",      "type": "longtext",      "readonly": true    },    {      "key": "facts",      "name": "Facts",      "type": "longtext",      "readonly": true    },    {      "key": "text_extracted",      "type": "bool",      "readonly": true    },    {      "key": "source",      "name": "Added from",      "type": "choice",      "choices": [        "app",        "agent",        "share"      ],      "labels": {        "app": "This app",        "agent": "An assistant",        "share": "Shared in"      },      "readonly": true    },    {      "key": "mcp_token_id",      "type": "text",      "readonly": true    },    {      "key": "origin_handle",      "type": "text",      "readonly": true    },    {      "key": "secret_id",      "name": "Password",      "type": "link",      "link_to": "secret",      "readonly": true    },    {      "key": "trashed_at",      "type": "date",      "format": "calendar-datetime",      "readonly": true    },    {      "key": "created_at",      "name": "Added",      "type": "date",      "readonly": true    },    {      "key": "statement_id",      "name": "Statement",      "type": "link",      "link_to": "statement",      "readonly": true    },    {      "key": "pages_text",      "name": "Pages",      "type": "longtext",      "readonly": true    },    {      "key": "state_message",      "name": "Reading status",      "type": "longtext",      "readonly": true    },    {      "key": "type_line",      "name": "Kind and date",      "type": "text",      "readonly": true    },    {      "key": "page_line",      "name": "Pages",      "type": "text",      "readonly": true    },    {      "key": "provenance",      "name": "Added",      "type": "text",      "readonly": true    },    {      "key": "period_years",      "name": "Year",      "type": "text",      "readonly": true    },    {      "key": "projects",      "name": "Project",      "type": "link",      "link_to": "project"    },    {      "key": "people",      "name": "People",      "type": "link",      "link_to": "person",      "readonly": true,      "multiple": true,      "edges": true    },    {      "key": "unlock_password",      "name": "File password",      "type": "text",      "shown_when": {        "column": "status",        "values": [          "locked"        ]      },      "presentation": "password"    }  ],  "seal": {    "clear": [      "mime",      "byte_size",      "sha256",      "storage_path",      "page_count",      "status",      "failed_stage",      "error_code",      "status_changed_at",      "doc_type",      "document_date",      "period_start",      "period_end",      "text_extracted",      "source",      "mcp_token_id",      "secret_id",      "trashed_at",      "created_at",      "statement_id",      "projects",      "people",      "period_years"    ],    "blind": [      "title"    ]  },  "instructions": "A document is a filed file, usually a PDF, with what was read from it: its kind, issuer, dates and a short summary. A locked document needs its password before it can be read, and that password is kept as a password record, never in the document. Use the dated day or the covered period exactly as printed; never guess them. Engine projection must omit protected page text and a drawable file reference while locked without a decrypted rendition. File replacement/upload invokes existing Rust document intake through the file-column editor. unlock_password is a write-only engine input; never project/store its plaintext in a document. Facts keeps all extracted amounts/account/people fields as readable labelled lines. period_years is a list of every covered year. Title retains original-name fallback; type_line waits for a ready, non-other classification.",  "tables": [    {      "name": "cotton_document_pages",      "columns": [        {          "name": "id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": true,          "declaration": "\n    id TEXT PRIMARY KEY",          "position": 0        },        {          "name": "document_id",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    document_id TEXT NOT NULL",          "position": 1        },        {          "name": "workspace_id",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    workspace_id TEXT NOT NULL",          "position": 2        },        {          "name": "page",          "type": "INTEGER",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    page INTEGER NOT NULL",          "position": 3        },        {          "name": "text",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    text TEXT NOT NULL",          "position": 4        },        {          "name": "created_at",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    created_at TEXT NOT NULL",          "position": 5        },        {          "name": "_hlc",          "type": "TEXT",          "default": "''",          "nullable": false,          "primary_key": false,          "declaration": "\n    _hlc TEXT NOT NULL DEFAULT ''",          "position": 6        },        {          "name": "_born",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    _born TEXT",          "position": 7        }      ],      "constraints": [        {          "declaration": "\n    UNIQUE (document_id, page)",          "position": 8        },        {          "declaration": "\n    CHECK (page >= 1)",          "position": 9        },        {          "declaration": "\n    FOREIGN KEY (document_id) REFERENCES cotton_documents(id)",          "position": 10        },        {          "declaration": "\n    FOREIGN KEY (workspace_id) REFERENCES cotton_workspaces(id) ON DELETE CASCADE\n  ",          "position": 11        }      ],      "prefix": "CREATE TABLE cotton_document_pages (",      "suffix": ")",      "indexes": [        "CREATE INDEX idx_cotton_document_pages_workspace ON cotton_document_pages (workspace_id)",        "CREATE INDEX IF NOT EXISTS idx_cotton_document_pages_author ON cotton_document_pages (substr(_hlc, 20), _hlc)"      ],      "full_text": [        "CREATE VIRTUAL TABLE fts_document_pages USING fts5(\n       text, content = 'cotton_document_pages', content_rowid = 'rowid',\n       tokenize = 'unicode61 remove_diacritics 2'\n     )"      ],      "triggers": [        "CREATE TRIGGER fts_document_pages_ai AFTER INSERT ON cotton_document_pages WHEN new.rowid >= coalesce((SELECT floor FROM _fts_fill WHERE fts = 'fts_document_pages'), 0) BEGIN\n       INSERT INTO fts_document_pages (rowid, text) VALUES (new.rowid, new.text);\n     END",        "CREATE TRIGGER fts_document_pages_ad AFTER DELETE ON cotton_document_pages WHEN old.rowid >= coalesce((SELECT floor FROM _fts_fill WHERE fts = 'fts_document_pages'), 0) BEGIN\n       INSERT INTO fts_document_pages (fts_document_pages, rowid, text) VALUES ('delete', old.rowid, old.text);\n     END",        "CREATE TRIGGER fts_document_pages_au AFTER UPDATE OF text ON cotton_document_pages\n       FOR EACH ROW WHEN old.rowid >= coalesce((SELECT floor FROM _fts_fill WHERE fts = 'fts_document_pages'), 0) AND (new.text IS NOT old.text) BEGIN\n       INSERT INTO fts_document_pages (fts_document_pages, rowid, text) VALUES ('delete', old.rowid, old.text);\n       INSERT INTO fts_document_pages (rowid, text) VALUES (new.rowid, new.text);\n     END"      ]    },    {      "name": "cotton_documents",      "columns": [        {          "name": "id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": true,          "declaration": "\n    id TEXT PRIMARY KEY",          "position": 0        },        {          "name": "workspace_id",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    workspace_id TEXT NOT NULL",          "position": 1        },        {          "name": "title",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    title TEXT",          "position": 2        },        {          "name": "original_name",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    original_name TEXT NOT NULL",          "position": 3        },        {          "name": "mime",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    mime TEXT NOT NULL",          "position": 4        },        {          "name": "byte_size",          "type": "INTEGER",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    byte_size INTEGER NOT NULL",          "position": 5        },        {          "name": "sha256",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    sha256 TEXT NOT NULL",          "position": 6        },        {          "name": "storage_path",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    storage_path TEXT NOT NULL",          "position": 7        },        {          "name": "decrypted_path",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    decrypted_path TEXT",          "position": 8        },        {          "name": "password",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    password TEXT",          "position": 9        },        {          "name": "page_count",          "type": "INTEGER",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    page_count INTEGER",          "position": 10        },        {          "name": "status",          "type": "TEXT",          "default": "'processing'",          "nullable": false,          "primary_key": false,          "declaration": "\n    status TEXT NOT NULL DEFAULT 'processing'",          "position": 11        },        {          "name": "failed_stage",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    failed_stage TEXT",          "position": 12        },        {          "name": "error_code",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    error_code TEXT",          "position": 13        },        {          "name": "status_changed_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    status_changed_at TEXT",          "position": 14        },        {          "name": "doc_type",          "type": "TEXT",          "default": "'other'",          "nullable": false,          "primary_key": false,          "declaration": "\n    doc_type TEXT NOT NULL DEFAULT 'other'",          "position": 15        },        {          "name": "document_date",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    document_date TEXT",          "position": 16        },        {          "name": "period_start",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    period_start TEXT",          "position": 17        },        {          "name": "period_end",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    period_end TEXT",          "position": 18        },        {          "name": "issuer",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    issuer TEXT",          "position": 19        },        {          "name": "summary",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    summary TEXT",          "position": 20        },        {          "name": "facts",          "type": "TEXT",          "default": "'{}'",          "nullable": false,          "primary_key": false,          "declaration": "\n    facts TEXT NOT NULL DEFAULT '{}'",          "position": 21        },        {          "name": "text_extracted",          "type": "INTEGER",          "default": "0",          "nullable": false,          "primary_key": false,          "declaration": "\n    text_extracted INTEGER NOT NULL DEFAULT 0",          "position": 22        },        {          "name": "source",          "type": "TEXT",          "default": "'app'",          "nullable": false,          "primary_key": false,          "declaration": "\n    source TEXT NOT NULL DEFAULT 'app'",          "position": 23        },        {          "name": "mcp_token_id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    mcp_token_id TEXT",          "position": 24        },        {          "name": "created_at",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    created_at TEXT NOT NULL",          "position": 25        },        {          "name": "updated_at",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    updated_at TEXT NOT NULL",          "position": 26        },        {          "name": "_hlc",          "type": "TEXT",          "default": "''",          "nullable": false,          "primary_key": false,          "declaration": "\n    _hlc TEXT NOT NULL DEFAULT ''",          "position": 27        },        {          "name": "origin_handle",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": " origin_handle TEXT CHECK (origin_handle IS NULL OR length(origin_handle) > 0)",          "position": 28        },        {          "name": "secret_id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": " secret_id TEXT",          "position": 29        },        {          "name": "_born",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    _born TEXT",          "position": 30        },        {          "name": "trashed_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    trashed_at TEXT",          "position": 31        }      ],      "constraints": [        {          "declaration": "\n    UNIQUE (workspace_id, sha256)",          "position": 32        },        {          "declaration": "\n    CHECK (byte_size >= 0)",          "position": 33        },        {          "declaration": "\n    CHECK (doc_type IN ('bank_statement', 'card_statement', 'invoice', 'receipt', 'tax', 'id', 'insurance', 'medical', 'legal', 'ticket', 'statement_other', 'letter', 'manual', 'other'))",          "position": 34        },        {          "declaration": "\n    CHECK ((status = 'failed') = (failed_stage IS NOT NULL))",          "position": 35        },        {          "declaration": "\n    CHECK (failed_stage IS NULL OR failed_stage IN ('unlock', 'extract', 'summarize', 'land'))",          "position": 36        },        {          "declaration": "\n    CHECK ((page_count IS NULL) OR (page_count >= 0))",          "position": 37        },        {          "declaration": "\n    CHECK ((period_start IS NULL) OR (period_end IS NULL) OR (period_end >= period_start))",          "position": 38        },        {          "declaration": "\n    CHECK (length(sha256) = 64 AND NOT sha256 GLOB '*[^0-9a-f]*')",          "position": 39        },        {          "declaration": "\n    CHECK (source IN ('app', 'agent', 'share'))",          "position": 40        },        {          "declaration": "\n    CHECK (status IN ('processing', 'summarizing', 'ready', 'locked', 'failed'))",          "position": 41        },        {          "declaration": "\n    FOREIGN KEY (workspace_id) REFERENCES cotton_workspaces(id) ON DELETE CASCADE\n  ",          "position": 42        }      ],      "prefix": "CREATE TABLE \"cotton_documents\" (",      "suffix": ")",      "indexes": [        "CREATE INDEX idx_cotton_documents_pipeline ON cotton_documents (status, status_changed_at) WHERE status IN ('processing', 'summarizing')",        "CREATE INDEX idx_cotton_documents_type ON cotton_documents (workspace_id, doc_type)",        "CREATE INDEX idx_cotton_documents_workspace_date ON cotton_documents (workspace_id, document_date DESC, created_at DESC)",        "CREATE INDEX idx_cotton_documents_origin ON cotton_documents (origin_handle) WHERE origin_handle IS NOT NULL",        "CREATE INDEX IF NOT EXISTS idx_cotton_documents_author ON cotton_documents (substr(_hlc, 20), _hlc)"      ],      "full_text": [        "CREATE VIRTUAL TABLE fts_documents USING fts5(\n       title, issuer, summary, original_name, content = 'cotton_documents', content_rowid = 'rowid',\n       tokenize = 'unicode61 remove_diacritics 2'\n     )"      ],      "triggers": [        "CREATE TRIGGER fts_documents_ai AFTER INSERT ON cotton_documents WHEN new.rowid >= coalesce((SELECT floor FROM _fts_fill WHERE fts = 'fts_documents'), 0) BEGIN\n       INSERT INTO fts_documents (rowid, title, issuer, summary, original_name) VALUES (new.rowid, new.title, new.issuer, new.summary, new.original_name);\n     END",        "CREATE TRIGGER fts_documents_ad AFTER DELETE ON cotton_documents WHEN old.rowid >= coalesce((SELECT floor FROM _fts_fill WHERE fts = 'fts_documents'), 0) BEGIN\n       INSERT INTO fts_documents (fts_documents, rowid, title, issuer, summary, original_name) VALUES ('delete', old.rowid, old.title, old.issuer, old.summary, old.original_name);\n     END",        "CREATE TRIGGER fts_documents_au AFTER UPDATE OF title, issuer, summary, original_name ON cotton_documents\n       FOR EACH ROW WHEN old.rowid >= coalesce((SELECT floor FROM _fts_fill WHERE fts = 'fts_documents'), 0) AND (new.title IS NOT old.title OR new.issuer IS NOT old.issuer OR new.summary IS NOT old.summary OR new.original_name IS NOT old.original_name) BEGIN\n       INSERT INTO fts_documents (fts_documents, rowid, title, issuer, summary, original_name) VALUES ('delete', old.rowid, old.title, old.issuer, old.summary, old.original_name);\n       INSERT INTO fts_documents (rowid, title, issuer, summary, original_name) VALUES (new.rowid, new.title, new.issuer, new.summary, new.original_name);\n     END"      ]    }  ],  "card": {    "face": "document",    "fields": [      {        "slot": "title",        "column": "title"      },      {        "slot": "subtitle",        "column": "type_line",        "label": "label.document.file"      },      {        "slot": "count",        "column": "page_count",        "label": "label.document.pages"      },      {        "slot": "date",        "column": "created_at",        "form": "relative",        "line": "field.received"      },      {        "slot": "source",        "column": "issuer",        "line": "field.issuer"      },      {        "slot": "span",        "column": "period_start",        "also": [          "period_end"        ],        "label": "label.document.period"      },      {        "slot": "amount",        "column": "facts",        "at": "total_minor",        "label": "label.document.amount"      },      {        "slot": "expires",        "column": "facts",        "at": "expires_on"      }    ]  },  "picture": {    "photo": "photo:e1f1afc03a39eee6facd9c74b47b0eb62bf0d48378636a2e0cd0d363a5ba4ad3",    "object": "tool-documents"  },  "engine": "documents",  "project": "projects",  "order": null,  "slip": {    "title": "title",    "subtitle": [      "doc_type",      "issuer"    ],    "trailing": "document_date",    "photo": null,    "toggle": null,    "title_fallback": "original_name",    "edge": "receipt",    "status": "state_message"  },  "tile": {    "face": "cover-grid"  },  "page": [    {      "heading": null,      "columns": [        "title",        "storage_path",        "original_name",        "status",        "state_message"      ]    },    {      "heading": "Facts",      "columns": [        "facts",        "doc_type",        "statement_id",        "issuer",        "document_date",        "period_start",        "period_end",        "page_count",        "byte_size"      ]    },    {      "heading": "Cotton’s reading",      "columns": [        "summary"      ]    },    {      "heading": "Pages",      "columns": [        "pages_text"      ]    },    {      "heading": "Password",      "columns": [        "unlock_password",        "secret_id"      ]    },    {      "heading": "Source",      "columns": [        "provenance",        "source",        "created_at",        "projects"      ]    }  ],  "add": false,  "record_route": "/documents/document/[id]",  "takes_files": true,  "sort": {    "column": "created_at",    "descending": true  },  "layout": "cards",  "cover": {    "footer": [      "type_line",      "page_line",      "state_message"    ]  },  "empty": {    "title": "No documents yet.",    "message": "Add a bill, a statement, an ID or a letter — its file, or a photograph of the paper."  },  "create": {    "label": "Add document",    "path": "/documents/import"  },  "deletion": {    "bin": true  },  "links": [    {      "label": "Deleted",      "table": "bin",      "where": {        "table": "document"      }    }  ],  "viewer": {},  "commands": [    {      "id": "statement",      "label": "Open",      "object": "open",      "kind": "open",      "opens": "statement_id"    },    {      "id": "delete",      "label": "Delete",      "kind": "delete",      "verb": "delete_record",      "data": {        "table": "document"      },      "confirm": "Undo on the receipt brings it back.",      "bindings": {        "id": "$id"      },      "when": {        "statement_id": null      },      "receipt": "Deleted",      "does": "destroys",      "object": "bin"    }  ],  "groups": {    "stack": "covers"  },  "tool": {    "views": [      {        "id": "all",        "label": "All",        "visibility": "owner",        "is_default": true,        "read": {          "table": "document"        }      }    ],    "name": "Documents",    "purpose": "Your files and the information inside them.",    "key": "documents",    "icon": "file-text",    "card_stock": "cream",    "route": "/documents",    "figures": [      {        "read": "lockedDocuments",        "one": "1 needs a password",        "many": "{n} need a password"      }    ]  },  "parts": {    "shape": "desk",    "head": {      "facts": []    },    "links": [],    "body": "facts",    "lists": [],    "acts": {      "more": [        "statement"      ],      "away": "delete"    },    "strip": [      {        "column": "summary"      },      {        "column": "issuer"      },      {        "column": "document_date"      },      {        "column": "status",        "when": {          "column": "status",          "values": [            "processing",            "summarizing",            "locked",            "failed"          ]        }      },      {        "column": "state_message",        "when": {          "column": "status",          "values": [            "processing",            "summarizing",            "locked",            "failed"          ]        }      },      {        "column": "unlock_password"      },      {        "column": "period_start",        "until": "period_end"      }    ]  }}