/** * A file as its fields, each kept in order by the relay — SPEC §6.2, §6.13. * * Two halves, both for any type the relay keeps: * * - **Reading** a list or one file with the relay's list read * (`"fields": "newest"`), which answers each file's first record and, for * each field, only its current change: a list never carries the text, and * opening one file (`"text": true` beside `#d`) carries its current text and * none of its history. Each change's id is that field's next `prev`. * - **Writing** a change that names the one it follows, and answering the * relay's `conflict:` as §6.2 says: a field set again keeps the later, and a * text merges by §13.3's rule, or stops and asks when a block was changed by * both or edited against a delete. It never writes over someone's newer * words. * * Only for a relay whose NIP-11 advertises `file_list` (`relayFileReadsOf` in * `@estiva-app/protocol`), and `"text"` only where it says `text`: a relay * without them answers these reads as if they were plain ones. * * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-file-is-its-fields-each-kept-in-order-by-the-relay.md * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/opening-a-file-reads-its-current-text-not-its-history.md * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/merging-two-changes-to-one-text-never-loses-words.md */ import { type MergeAsk, type MergeText, type SignedEvent, type UnsignedEvent } from '@estiva-app/protocol'; import { type QueryFn } from './projection.js'; /** The field that holds a file's text (§6.10): the one field that merges rather than re-sets. */ export declare const TEXT_FIELD = "description"; /** One field's current value, and the change that set it. */ export interface CurrentField { value: string; /** The value's `content-format`, for the text; absent for marker text. */ format?: string; /** The change's id: the field's next `prev`. */ id: string; /** Who set it. */ pubkey: string; /** When, in ms: `ts` where it agrees with `created_at` to the second (§6.2), else `created_at`. */ at: number; event: SignedEvent; } /** A file read by its fields. */ export interface FileFields { root: SignedEvent; /** `::`. */ address: string; /** Each field's current change. The text is here only when the file was opened with it. */ fields: Record; /** The root and each current change, as signed — what a fold of every change would read the same. */ events: SignedEvent[]; } /** When a change happened, in ms, by §6.3's order. */ export declare function changedAt(event: SignedEvent): number; /** * The fields one change sets, with their values: one `field`/`value`, or one * per `set` (§6.2). A change setting the text carries its `content-format`. */ export declare function fieldsSetBy(event: { tags: string[][]; }): { field: string; value: string; format?: string; }[]; /** * Each file the events hold, by its first record and the current change of each * of its fields. * * A change counts only where §6.2 says a reader folds it: its `a` names the * file, its `O` (if any) is the file's id, and its `h` is the file's home. The * text counts only when `withText`: a list never takes the text from a change * that set several fields, since a later text-only change may have replaced it * (§6.13). */ export declare function filesOf(events: readonly SignedEvent[], options?: { withText?: boolean; }): FileFields[]; /** * A list of files by the relay's list read: each first record the filter picks, * with its fields' current changes and never its text (§6.13). * * `filter` picks first records, as §6.13 says: `#h` for a Folder's issues, * `kinds` alone for the workspace's projects, `#d` for named ones, `search` * for typing. Only for a relay advertising `file_list`. */ export declare function listFiles(query: QueryFn, filter: Record): Promise; /** * One file, opened: its first record, its fields' current changes and, where * the relay says `file_list.text`, its current text — one text however long * its history (§6.13, C40). Undefined when the relay holds no such file the * reader may read. * * Without `text` the answer has no text field at all, which is not "the text * was never changed": a caller that needs it reads it the old way. */ export declare function openFile(query: QueryFn, file: { kind: number; pubkey: string; d: string; }, options: { text: boolean; }): Promise; /** * The text a change or first record holds: a change's `description`, or a * first record's seed — a project's `description` tag, then its `content` * (§6.2, §7.2). */ export declare function textOf(event: SignedEvent): MergeText | undefined; /** * The relay's refusal of a change that names no change it follows, on a type * it keeps in order (§6.13): it has made the switch since this tab read its * NIP-11. Read it again, switch, and write the change again in order * ({@link prevsAfterSwitch}): the person sees nothing (Miky, 9 Nov, ALO-8). * * Decided on the relay's words after `invalid:`, never on what follows them, * so a relay that adds what to do keeps being recognised. * * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/a-file-is-its-fields-each-kept-in-order-by-the-relay.md */ export declare function isSwitchRefusal(reason: string | undefined): boolean; /** * Each field's `prev` for a change written again after the switch: the * field's current change, or the first record's id when it never changed — * one open of the file, with its text when the text is among them. A tab from * before the switch wrote with no `prev`, which meant "mine is the newest"; * following the current change keeps that for a field set again. The text * also passes the text the tab read as `base` to {@link writeChange}, so * words saved since are merged, never written over. A `member:

` field * names no change it follows (§6.13) and gets no `prev`. Undefined when the * file cannot be read. * * Write again once: a second refusal of the same kind is shown as any refusal. */ export declare function prevsAfterSwitch(query: QueryFn, file: { kind: number; pubkey: string; d: string; }, fields: readonly string[]): Promise | undefined>; /** What a `conflict:` refusal says (§6.2), as the relay words it. */ export type Conflict = { kind: 'stale'; field: string; current?: string; } | { kind: 'sorts-before'; field: string; } | { kind: 'other'; }; export declare function conflictOf(reason: string): Conflict | undefined; /** What {@link writeChange} is handed. */ export interface WriteChangeArgs { field: string; /** The value to set; for the text, mine. */ value: string; /** The text's `content-format`, when it is one. */ format?: string; /** * The change this one follows, as the writer read it: the field's current * change, or the first record's id for its first change. For the text it is * also the base a merge starts from — the version the person, or the agent, * last read. */ prev: string; /** * For the text: the text the writer read, when it cannot name the change * it came from — a tab that read the file before the switch, writing again * after {@link prevsAfterSwitch}. Where `prev`'s text differs from it, * mine is merged onto `prev`'s by §13.3 before anything is sent, so * someone's newer words are kept. */ base?: MergeText; /** * The unsigned change for this value, `prev` and time — `buildActionEvent` * bound to the field's action, so the manifest decides its shape and every * attempt carries the same tags. */ build: (input: { value: string; format?: string; prev: string; createdAtMs: number; }) => UnsignedEvent | string; sign: (event: UnsignedEvent) => Promise; publish: (event: SignedEvent) => Promise<{ ok: boolean; reason?: string; eventId?: string; }>; /** Reads events by id, and the field's current change when a refusal names none. */ query: QueryFn; /** The field's current change, read again: {@link openFile}'s, for this field. */ current: (field: string) => Promise; /** The clock, in ms. */ now?: () => number; /** How many sends before giving up; 4 unless said. */ tries?: number; } export type WriteChangeResult = { ok: true; /** The id the relay accepted: the field's next `prev`. */ id: string; event: SignedEvent; /** Set when the text was merged onto someone's newer change, which it names. */ mergedOnto?: SignedEvent; } | { ok: true; /** Nothing was sent: mine, merged onto theirs, says nothing theirs does not. */ unchanged: true; id: string; mergedOnto: SignedEvent; } | { ok: false; /** Why nothing was saved, in the relay's words or the builder's. */ refused: string; } | { ok: false; /** The text asks: these blocks were changed by both, or edited against a delete. Nothing was saved. */ asks: MergeAsk[]; /** The change now current, whose words must be kept. */ theirs: SignedEvent; }; /** * Write one field's change, naming the one it follows, and answer a * `conflict:` as §6.2 says. * * - **Stale `prev`, a field other than the text**: read the current change * (by the id the relay named, or {@link WriteChangeArgs.current} when it * named none) and set the value again after it. Two setting one field is not * a conflict to show: the later stands. * - **Stale `prev`, the text**: merge mine onto the current text by §13.3, * with the text `prev` named as the base, and send the merge after it. When * a block asks, nothing is sent and the answer carries the blocks and * theirs. Refused again, it merges again with what it last sent as mine and * what it merged onto as the base. * - **Does not sort after its `prev`**: the clock is behind; stamp it 1 ms * after `prev`, `ts` and `created_at` agreeing. * - **Anything else** (`invalid:` and the rest) is never sent again. * * A few tries, then it stops with the relay's last answer. */ export declare function writeChange(args: WriteChangeArgs): Promise; /** * One change setting several fields of one file (§6.2), from the one-field * changes for each — `buildActionEvent`'s, each with its `prev`, so the * manifest has checked every value and staged every status. Tag order: `a`, * `O`, `h`, `ts`, one `["set", field, value, prev]` per field, then the * text's `content-format`, any other tag once (`A`, `o`, `imeta`, `p`), then * one `stage` per staged field. The relay takes it whole or refuses it whole. * * A string when they are not one file's changes in one Folder, when one sets * no field or follows no change, when a field is named twice, or when the * file has no id for the `O` a several-field change must carry. * * why: https://github.com/estiva-app/estiva-docs/blob/main/decisions/one-change-may-set-several-fields-taken-whole-or-refused-whole.md */ export declare function combineChanges(changes: readonly UnsignedEvent[]): UnsignedEvent | string; /** One field of a several-field change, as {@link writeChanges} is handed it. */ export interface FieldWrite { field: string; value: string; format?: string; /** As {@link WriteChangeArgs.prev}: this field's current change as the writer read it. */ prev: string; } /** What {@link writeChanges} is handed: {@link WriteChangeArgs}, for several fields of one file. */ export interface WriteChangesArgs extends Omit { fields: readonly FieldWrite[]; /** * The types the relay keeps in order (`RelayFileReads.inOrder`). A change * setting several fields is sent only for one of them: a reader older than * interop 0.76.0 folds nothing from a `set`, and the switch is what says * every app reads it (record one-change-may-set-several-fields…). */ inOrder: readonly string[]; /** The unsigned one-field change for one field: `buildActionEvent` bound to that field's action. */ build: (input: FieldWrite & { createdAtMs: number; }) => UnsignedEvent | string; } /** * Write several fields of one file as one change, and answer a `conflict:` as * §6.2 says for one: read every field's current change, set again each field * other than the text that someone changed since, merge the text by §13.3, * and send them again as one change. A text that asks stops it, and nothing is * saved; a text whose merge says nothing theirs does not is left out of the * change. Each attempt is landed whole or refused whole by the relay. */ export declare function writeChanges(args: WriteChangesArgs): Promise; //# sourceMappingURL=fields.d.ts.map