/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * A run's answer landed on a record, key by key (#1409, east-workspace#1622, * rules 7, 8 and 11; `Optimise Spec.md` §6 "The one step", §9.2 R7). The * answer is East's patch of the record; e3-ui's `optimiseChangesFor` reads * it key by key — a patch of the whole Dict among it, never the record * replaced — and each key lands on the row the page holds: an insert adds the * row under the optimiser's key, an update changes the held row by its patch, * a delete removes it. A row the answer updates or deletes that the page does * not hold is named, to be read by its key before the answer lands (rule 8); * one whose own edit is still incomplete refuses the answer (rule 11). The * landing becomes the builder's changes — `{ kind, id, row, created }`, * `{ kind, id, row }`, `{ kind, id, remove }` — for one `record` call, and * the operations the landing draws and the provenance says (`data-op`, * `Was {previous} · Elara run #n`), kept until Apply. * * @packageDocumentation */ import { type DictType, type EastType, type option } from "@elaraai/east"; import type { ScheduleChange } from "../schedule/editing.js"; import type { OptimiseCounts, OptimiseMissing, OptimiseRefused } from "./use-optimise.js"; /** * One operation of a run on an entry (R7): the entry — its kind and its key's * text, which {@link opKey} keeps it under — what the run did, and the row * before it, for the provenance. * * @typeParam R - The entry's row */ export type OptimiseOp = { readonly kind: string; readonly id: string; readonly op: "add"; readonly row: R; } | { readonly kind: string; readonly id: string; readonly op: "chg"; readonly row: R; readonly previous: R; } | { readonly kind: string; readonly id: string; readonly op: "del"; readonly previous: R; }; /** * A run's diff: its changes for one `record`, its operations by entry, and * what it counted. * * @typeParam R - The entries' row */ export interface OptimiseDiff { readonly changes: readonly ScheduleChange[]; /** Each operation, under {@link opKey}. */ readonly ops: ReadonlyMap>; readonly counts: OptimiseCounts; } /** No operations. */ export declare const NO_OPTIMISE_DIFF: OptimiseDiff; /** * The key an entry's operation is kept under: its kind and its id, so two * kinds' entries of one id never meet. * * @param kind - The kind's slot, or the record's key * @param id - The entry's key's text * @returns The key */ export declare function opKey(kind: string, id: string): string; /** * An entry's key as its session names it: a String key as it is, any other * as East prints it — what a Schedule session's ids are. * * @param keyType - The record's key type * @returns The key's text */ export declare function optimiseIdOf(keyType: EastType): (key: K) => string; /** * An entry's key from its id, as its session names it: a String key is its * own id, any other is read from East's text of it — {@link optimiseIdOf}'s * inverse, for the rows a request carries by key. * * @param keyType - The record's key type * @returns The key; `undefined` for an id that names no key of the type */ export declare function optimiseKeyOf(keyType: EastType): (id: string) => K | undefined; /** * Why an answer does not land as it stands: rows it touches that the page does not hold, to be read by their keys first * (rule 8); or rows whose own edit is still incomplete, which refuse it (rule 11). Each row by its kind and its key's * text, as its session names it. */ export type OptimiseUnlanded = OptimiseMissing | { readonly incomplete: readonly OptimiseRowRef[]; }; /** A row an answer touches: its kind's slot, or its record's key, and its key's text. */ export interface OptimiseRowRef { readonly kind: string; readonly id: string; } /** * One record's answer landed key by key (rules 7, 8, 11). Each key the patch touches lands on the row the page holds: * an insert is an add under the optimiser's key — or, onto a row the page holds, a change of it; an update is a * change, the held row patched; a delete a remove, and of a key no row stands under, nothing. A key that lands as the * row it was is no edit, and a patch that changes nothing is none. * * @typeParam K - The record's key * @typeParam R - The record's row * @param kind - The kind's slot, or the record's key: what the changes name * @param record - The record's type, `Dict`: how the patch is read, and a row compared, patched and encoded * @param idOf - An entry's key as its session names it ({@link optimiseIdOf}) * @param patch - The answer's patch of the record * @param held - The row the page holds under a key's text — its draft where it has one; `none` for a key no row stands * under; `undefined` for one the page does not hold, or is still reading * @param incomplete - Whether the page's edit of a key is still incomplete: the function read it as saved * @returns The landing; the rows to read first, or those whose edit refuses it ({@link OptimiseUnlanded}); `undefined` * for a patch that does not fit the rows the page holds */ export declare function diffOptimisePatch(kind: string, record: DictType, idOf: (key: K) => string, patch: unknown, held: (id: string) => option | undefined, incomplete?: (id: string) => boolean): OptimiseDiff | OptimiseUnlanded | undefined; /** * Whether a landing was made, or what keeps it from landing. * * @param landed - What {@link diffOptimisePatch} answered * @returns Whether it is the landing */ export declare function isOptimiseDiff(landed: OptimiseDiff | OptimiseUnlanded | undefined): landed is OptimiseDiff; /** One record's answer: the kind's slot or the record's key its changes name, the record's type, and the answer's patch of it. */ export interface OptimiseRecordPatch { readonly kind: string; readonly record: DictType; readonly patch: unknown; } /** * Each record's answer landed on the rows the page holds ({@link diffOptimisePatch}), all or none, on a builder none of * whose drafts is half made — a schedule builder's, every draft a whole row — so no edit of the page's refuses an answer: * a row any record's answer touches that the page does not hold keeps every record's from landing until it is read. * * @typeParam R - The records' row * @param answers - Each record's patch, in the builder's order * @param held - The row the page holds under a kind's key ({@link diffOptimisePatch}) * @returns Each record's landing, in order; the rows to read first; `undefined` for an answer that does not fit the rows * the page holds */ export declare function landOptimisePatches(answers: readonly OptimiseRecordPatch[], held: (kind: string, id: string) => option | undefined): readonly OptimiseDiff[] | OptimiseMissing | undefined; /** * What a builder's landing of an answer came to (R7, R10): what it counted, and what the runs have left on its rows until * Apply — `undefined` from a run that changed nothing, which leaves what earlier runs left as it is. * * @typeParam O - What the runs leave on the builder's rows */ export interface OptimiseLanded { readonly counts: OptimiseCounts; readonly optimised: O | undefined; } /** * A builder's landing as its run takes it (`useOptimise`'s `onLanded`; rules 8 and 11, R10): rows to read first and a * refusal go as they are, and so does no landing, an answer that does not fit; a landing answers its counts, what it * left on the rows kept first where it left anything — a run that changed nothing leaves what earlier runs left. * * @typeParam O - What the runs leave on the builder's rows * @param landed - What the builder's landing answered * @param keep - Keeps what the runs have left: the builder's marks of them, and its landing's replay * @returns What the run is answered */ export declare function optimiseLandedCounts(landed: OptimiseLanded | OptimiseMissing | OptimiseRefused | undefined, keep: (optimised: O) => void): OptimiseCounts | OptimiseMissing | OptimiseRefused | undefined; /** * What records a run's changes as one step of a builder's history: its editing's `record`, or its session's. * * @typeParam C - A change, as the history records it */ export interface OptimiseRecorder { /** * Records the changes as one step. * * @param changes - The changes * @param origin - The step's origin, `optimise` * @param label - The step's label — `Elara run #7` * @returns Whether the history took the step */ record(changes: readonly C[], origin: "optimise", label: string): boolean; } /** * A run's changes recorded (R10, §12; east-workspace#1622, rule 10): a run that changes nothing records nothing and * leaves what earlier runs left as they are; one that changes something is recorded as one step of the builder's * history, origin `optimise`, and what the runs leave is made over it. * * @typeParam C - A change, as the history records it * @typeParam O - What the runs leave on the builder's rows * @param run - The run's changes, and what it counted * @param history - What records the changes as one step * @param label - The step's label — `Elara run #7` * @param left - What the runs leave, made once the step is recorded * @returns What the run counted, and what the runs leave — none from a run that changed nothing; `undefined` where the * history would not take the step */ export declare function recordOptimiseRun(run: { readonly changes: readonly C[]; readonly counts: OptimiseCounts; }, history: OptimiseRecorder, label: string, left: () => O): OptimiseLanded | undefined; /** * A diff's operations told by the entries' places (§9.5): on a builder whose * entries have a place against a backlog — the Calendar's and the Plan's * canvas — an entry the run gave a place is an add on the canvas, whatever * its key did, and one the run took off its place is a remove, its ghost * where it stood; an entry moved, or changed in place, stays a change. The * session's changes are the diff's own: an entry the request held is * rewritten, never created. * * @typeParam R - The row * @param diff - The diff by key * @param placed - Whether a row has a place: a scheduled event, a placed job — given the row, its operation's key * ({@link opKey}), and whether it is the row before the run (`was`) or the one it leaves (`now`) * @returns The diff, its operations and counts told by place */ export declare function optimisePlacementOps(diff: OptimiseDiff, placed: (row: R, key: string, side: "was" | "now") => boolean): OptimiseDiff; /** * Several kinds' diffs as one: one `record` call across every record the * builder writes (R8). * * @param diffs - Each kind's diff * @returns The one diff */ export declare function mergeOptimiseDiffs(diffs: readonly OptimiseDiff[]): OptimiseDiff; //# sourceMappingURL=optimise-diff.d.ts.map