/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * A query's editing session (#935) — the open query as the shared editing * session (#879) holds it, one session per open query, as Studio's is one per * open page. * * - **Entries.** A query is its header — its name, its description, the data * source it starts from, and its jq when it is not steps — and its steps, in * order, each drafted whole (`DraftField(QueryEntry)`), as Studio drafts a * placement. The header's id is `$query`; a step's is its own. * - **The snapshot** is the saved query's header and its steps, or a new * query's header alone. A saved query's steps are parsed once per saved * entry and kept, because a parse gives fresh ids: the same entry must give * the same snapshot, render after render. * - **Apply** applies the batch to the snapshot, prints the steps as canonical * jq, checks and prepares the program over the root, and commits the query * as one patch through the record's patch write. The entries it applied * become the snapshot of the query it saved, so the session acknowledges its * own request when the record reads back as it wrote it. * - **Readiness** is the check's: a problem is an `invalid` issue, an * unfinished slot an `incomplete` one, so Apply waits for both. * * @packageDocumentation */ import { ArrayType, OptionType, StringType, StructType, VariantType, type EastType, type ValueTypeOf, type option } from "@elaraai/east"; import { Editing } from "@elaraai/east-ui/internal"; import { SavedQueriesType, SavedQueryType, type QueriesHandleType } from "@elaraai/e3-ui/internal"; import type { BatchReadiness, EditSession, EntryVersion, Origin } from "@elaraai/east-ui-components"; import type { UIStoreInterface } from "@elaraai/east-ui-components"; import { type CheckedSteps } from "./steps/check.js"; import type { Step, StepQuery } from "./steps/values.js"; import type { SavedOffer } from "./model/slots.js"; import { type QueryWords } from "./model/words.js"; import { type QueryRoot } from "./one-shot.js"; import type { QueryOpen } from "./open-query.js"; /** The header entry's id. */ export declare const QUERY_HEADER_ID = "$query"; /** * A query's header, as its editing session drafts it. * * @property id - Always {@link QUERY_HEADER_ID} * @property name - Its name: its key in the saved queries, once saved * @property description - Its description; `none` shows the generated sentence * @property source - The data source its steps start from * @property jq - The program as jq, when it is not steps — a program that * does not start from a data source; its steps are then empty */ export declare const QueryHeaderType: StructType<{ readonly id: StringType; readonly name: StringType; readonly description: OptionType; readonly source: StringType; readonly jq: OptionType; }>; /** A query's header. */ export type QueryHeader = ValueTypeOf; /** * One entry of a query's editing session. * * @property header - Its header * @property step - One of its steps */ export declare const QueryEntryType: VariantType<{ readonly header: StructType<{ readonly id: StringType; readonly name: StringType; readonly description: OptionType; readonly source: StringType; readonly jq: OptionType; }>; readonly step: VariantType<{ readonly filter: StructType<{ readonly id: StringType; readonly match: VariantType<{ readonly all: import("@elaraai/east").NullType; readonly any: import("@elaraai/east").NullType; }>; readonly conds: ArrayType; readonly cmp: OptionType>; readonly value: OptionType>; readonly inner: OptionType; readonly whole: import("@elaraai/east").BooleanType; }>; readonly group: StructType<{ readonly id: StringType; readonly match: VariantType<{ readonly all: import("@elaraai/east").NullType; readonly any: import("@elaraai/east").NullType; }>; readonly conds: ArrayType; }>; }>>>; }>; readonly lookup: StructType<{ readonly id: StringType; readonly dataset: OptionType; readonly key: OptionType; readonly fields: ArrayType; }>; readonly group: StructType<{ readonly id: StringType; readonly by: OptionType>; readonly aggs: ArrayType; readonly field: OptionType; readonly as: StringType; }>>; }>; readonly sort: StructType<{ readonly id: StringType; readonly field: OptionType; readonly dir: VariantType<{ readonly asc: import("@elaraai/east").NullType; readonly desc: import("@elaraai/east").NullType; }>; }>; readonly limit: StructType<{ readonly id: StringType; readonly n: VariantType<{ readonly number: import("@elaraai/east").FloatType; readonly text: StringType; }>; }>; readonly count: StructType<{ readonly id: StringType; }>; readonly pick: StructType<{ readonly id: StringType; readonly fields: ArrayType; readonly as: StringType; }>>; }>; readonly fill: StructType<{ readonly id: StringType; readonly field: OptionType; readonly value: OptionType>; }>; readonly drill: StructType<{ readonly id: StringType; readonly field: OptionType; }>; readonly datepart: StructType<{ readonly id: StringType; readonly field: OptionType; readonly part: VariantType<{ readonly year: import("@elaraai/east").NullType; readonly month: import("@elaraai/east").NullType; readonly weekday: import("@elaraai/east").NullType; }>; readonly as: StringType; }>; readonly walk: StructType<{ readonly id: StringType; readonly via: OptionType; }>; readonly tabulate: StructType<{ readonly id: StringType; readonly over: StringType; readonly from: VariantType<{ readonly number: import("@elaraai/east").FloatType; readonly text: StringType; }>; readonly to: VariantType<{ readonly number: import("@elaraai/east").FloatType; readonly text: StringType; }>; readonly step: VariantType<{ readonly number: import("@elaraai/east").FloatType; readonly text: StringType; }>; readonly fixed: import("@elaraai/east").DictType>; readonly as: StringType; }>; readonly jq: StructType<{ readonly id: StringType; readonly text: StringType; }>; }>; }>; /** One entry of a query's editing session. */ export type QueryEntry = ValueTypeOf; /** The session's entry schema, as its wire names it. */ export declare const QUERY_ENTRY_TYPE: import("@elaraai/east").EastTypeValue; /** The session's draft schema: each entry drafted whole. */ export declare const QUERY_DRAFT_TYPE: import("@elaraai/east").EastTypeValue; /** The session's answer to a batch. */ type ApplyResult = ValueTypeOf; /** A saved query. */ export type SavedQuery = ValueTypeOf; /** The saved queries record. */ export type SavedQueries = ValueTypeOf; /** The bound saved queries record. */ export type QueriesHandle = ValueTypeOf; /** * The id an entry has in the session. * * @param entry - The entry * @returns Its id: `$query` for the header, a step's own */ export declare function entryId(entry: QueryEntry): string; /** * A query as entries: its header, then its steps. * * @param header - Its header * @param steps - Its steps, in order * @returns The entries */ export declare function queryEntries(header: QueryHeader, steps: readonly Step[]): QueryEntry[]; /** * The query a session's entries make. * * @param entries - The entries * @returns The header, and the steps as a query on its data source * @throws {Error} When the entries hold no header */ export declare function entriesQuery(entries: readonly QueryEntry[]): { header: QueryHeader; query: StepQuery; }; /** * The program a query's entries make: its jq when it is not steps, else its * steps printed as canonical jq. * * @param header - Its header * @param query - Its steps * @param root - The root's type * @returns The program's text */ export declare function queryProgram(header: QueryHeader, query: StepQuery, root: EastType): string; /** * What a query's session begins from: the saved entry, and its entries. * * @property saved - The saved query the entries are of; `undefined` for a query never saved * @property entries - The header and the steps */ export interface QueryBase { readonly saved: SavedQuery | undefined; readonly entries: readonly QueryEntry[]; } /** * A saved query's entries: its header, and its steps parsed from its * program — or, for a program that is not steps, its jq. * * @param saved - The saved query * @param root - The root's type * @returns The entries; the steps' ids are fresh */ export declare function savedEntries(saved: SavedQuery, root: EastType): QueryEntry[]; /** * The base the open query's session begins from: a save it committed; else * the base it was given last, while the record still holds the entry it is * of; else the saved query's entries, parsed once; else a new query's header * alone — or, for a new query begun as a run never saved, the run's entries, * its name and description with them. * * @remarks * A save the session committed is its base from the moment it commits, while * the record has not read it back yet — the session acknowledges its own * request by it, whichever comes first, its answer or the record's re-read — * and its base for good once the record holds it. A record that holds another * entry under its name has moved on since: the record's entry is the base * again. * * @param store - The UI store the sessions live in * @param sourceId - The open query's session's source id * @param open - The open query * @param record - The saved queries * @param root - The root's type * @param untitled - A new query's name on a data source * @returns The base, or `undefined` when the open saved query is not in the record */ export declare function queryBase(store: UIStoreInterface, sourceId: string, open: QueryOpen, record: SavedQueries, root: EastType, untitled: (source: string) => string): QueryBase | undefined; /** * Remembers a save a session's Apply committed, as the base it continues * from: the saved entry it wrote, and the entries it applied — for the session * that applied them, and for the saved query's own, which a new query and a * rename open next ({@link queryBase}). * * @param store - The UI store the sessions live in * @param sourceIds - The source ids to remember it under * @param base - The saved entry and the entries */ export declare function rememberSave(store: UIStoreInterface, sourceIds: readonly string[], base: QueryBase): void; /** * The snapshot a base is, as the session's wire carries it. * * @param base - The base * @returns Its entries, encoded */ export declare function encodeBase(base: QueryBase): Uint8Array; /** * One entry of a base, by id, as the session's wire carries it. * * @param base - The base * @param id - The entry's id * @returns The entry, encoded, or `none` when the base has no entry of that id */ export declare function readBaseEntry(base: QueryBase, id: string): option; /** * The saved queries a builder offers to start from, as the add-step list of a * query with no steps offers them (#934): each whose data sources are all * bound here, by the same name and at the same path (`QueryInternal.rootBound`), * with the data source its steps start from. * * @param record - The saved queries * @param root - The root the builder's data sources make * @returns The offers, in name order */ export declare function savedOffers(record: SavedQueries, root: QueryRoot): SavedOffer[]; /** * Why a saved query — or a recent run — can't open here, in the builder's * words (`Query Editor Spec.md` §4.10): a data source it reads that no binding * here has the name of, or one bound here at another path * (`QueryInternal.rootBound`). * * @param saved - The saved query, or the run * @param root - The root the builder's data sources make * @param words - The words * @returns The reason — "Reads customers, which isn't here", "Reads * .inputs.archive.orders, not this builder's orders" — or `undefined` when * it can open */ export declare function rootRefusal(saved: SavedQuery, root: QueryRoot, words: QueryWords): string | undefined; /** * The query's problems as the session's readiness: each error an `invalid` * issue, each unfinished slot an `incomplete` one, in plain words, on the step * and the slot they are about. * * @param header - The query's header * @param query - Its steps * @param root - The root, or a reason it cannot be queried * @param words - The words * @returns The readiness, and the check it was made from */ export declare function queryReadiness(header: QueryHeader, query: StepQuery, root: QueryRoot | string, words: QueryWords): { readiness: BatchReadiness; checked: CheckedSteps | undefined; }; /** What a query's Apply needs. */ export interface QueryApplyContext { /** The saved queries record, bound with its patch. */ readonly handle: QueriesHandle; /** The open query. */ readonly open: QueryOpen; /** The base its session began from. */ readonly base: QueryBase; /** The root. */ readonly root: QueryRoot; /** The UI store the sessions live in. */ readonly store: UIStoreInterface; /** The session's own source id. */ readonly sourceId: string; /** The source id a query of a name has. */ readonly sourceIdOf: (name: string) => string; /** What a refused write says, in the builder's words. */ readonly refused: (outcome: "changed" | "renamed", name: string, by: string | undefined) => string; /** Told the name a commit saved the query under, once its save is remembered: the base is the save's from then on. */ readonly onSaved: (name: string) => void; /** The clock a save is stamped with. */ readonly now?: () => Date; } /** * A query's `onApply`: the batch applied to the base, the program printed, * checked and prepared over the root, and the query committed as one patch. * * @param ctx - The record, the open query, its base and root, and the words * @returns The session's async apply, over the batch's bytes */ export declare function queryApply(ctx: QueryApplyContext): (bytes: Uint8Array) => Promise; /** * Records one gesture: the query as it was and as the gesture leaves it, as * one undoable transaction of every entry it changed, added, removed or moved. * * @param session - The open query's session * @param original - An entry's version before any draft of it * @param current - The entries as the drafts stand * @param next - The entries after the gesture * @param origin - The gesture * @param label - Its label in the history * @returns Whether anything changed */ export declare function recordGesture(session: EditSession, original: (id: string) => EntryVersion, current: readonly QueryEntry[], next: readonly QueryEntry[], origin: Origin, label: string): boolean; export {}; //# sourceMappingURL=session.d.ts.map