/** * Copyright (c) 2025 Elara AI Pty Ltd * Dual-licensed under AGPL-3.0 and commercial license. See LICENSE for details. */ /** * A query's one-shot call (#935, with #928 folded in): the builder runs a * query with no query code on the server. The query is checked once against * the whole root its data sources make, translated to East IR here, and run * as a function, as one-shot runs any caller's IR (#1031), with one dataset * argument per data source it reads (`devdocs/QUERY.md` §17). The call is * platform-free — east-c given no platform package, and a body that calls no * platform function — so a caller who may read the workspace may run it. * * The builder sends {@link PreparedQuery.request} with e3-api-client's * `oneShotExecute` and reads what comes back with {@link queryResultOf}. * Everything here is a pure function: nothing reaches the network or the DOM. * * @packageDocumentation */ import { type CheckJqResult, type EastType, type EastTypeValue, type JqMultiplicity, type JqNode, type ValueTypeOf } from "@elaraai/east"; import { type ExecuteResult, type OneShotRequest, type RunnerValue } from "@elaraai/e3-types"; import type { QueryResultType, QueryRootEntryType } from "@elaraai/e3-ui/internal"; /** * A data source of a query's root, as the builder binds it: the root field a * query reads it by, its dataset's path, and the dataset's type. * * @remarks * `name` and `path` are a {@link QueryRootEntryType} value, what a saved query * keeps of its root; `type` is the dataset's East type, as e3 lists it. */ export type QueryRootEntry = ValueTypeOf & { /** The dataset's East type, as e3 lists it. */ type: EastTypeValue; }; /** * A query's root: the type every query of the builder is checked against, and * the data sources it is made of. * * @property type - The root's type: a Struct of the data sources' types, in entry order * @property entries - The data sources, in root order */ export interface QueryRoot { /** The root's type: a Struct of the data sources' types, in entry order. */ readonly type: EastType; /** The data sources, in root order. */ readonly entries: QueryRootEntry[]; } /** * The root a query's data sources make: the builder's `useQueryRoot` gives it * from the data sources a page binds. * * @param entries - the data sources, in root order * @returns the root: a Struct of the data sources' types, in entry order, and * the data sources * @throws {Error} When a name is not a jq identifier, or is given twice, * naming it. * * @remarks * The root is only a type. Its entries become the translation's parameters, * one dataset each, because a lazy dataset put into a struct is read whole * (`devdocs/QUERY.md` §15.6). A name is a jq identifier — a letter or `_`, * then letters, digits and `_` — so `.name` reads it. A root of no data * sources is the empty Struct, and a query that reads a data source of it is * refused when it is checked. * * @example * ```ts * const root = queryRoot([ * { name: "customers", path: [variant("field", "inputs"), variant("field", "customers")], type: toEastTypeValue(Customers) }, * { name: "orders", path: [variant("field", "inputs"), variant("field", "orders")], type: toEastTypeValue(Orders) }, * ]); * // root.type is StructType({ customers: Customers, orders: Orders }) * * queryRoot([{ name: "order lines", path, type }]); * // throws: queryRoot: "order lines" is not a jq identifier: … * ``` */ export declare function queryRoot(entries: readonly QueryRootEntry[]): QueryRoot; /** * Options for {@link prepareQuery}: the call's limits, and its runner. * * @property maxOutputs - The most outputs a `many` query returns: 1 000 by default, at most 100 000 * @property maxBytes - The largest answer the call returns, in bytes: 1 MiB by default * @property timeoutMs - The call's time limit, in milliseconds: the server's when omitted * @property runner - The runner: east-c given no platform package, by default */ export interface QueryOptions { /** The most outputs a `many` query returns: 1 000 by default, at most 100 000. */ readonly maxOutputs?: number; /** The largest answer the call returns, in bytes: 1 MiB by default. */ readonly maxBytes?: number; /** * The call's time limit, in milliseconds. Omitted, the call names none and * the server's applies: its default for a call that names none, under the * longest a request to it may take (#1131). */ readonly timeoutMs?: number; /** The runner: east-c given no platform package by default, which any caller who may read the workspace may run. */ readonly runner?: RunnerValue; } /** What a run of a query answered: a {@link QueryResultType} value. */ export type QueryResult = ValueTypeOf; /** * What the checker made of a program that checks: the program as written, the * type of each output and how many there are, each present. */ export type CheckedProgram = CheckJqResult & { /** The program as written. */ readonly program: JqNode; /** The type of each output. */ readonly elementType: EastType; /** How many outputs the program gives. */ readonly multiplicity: JqMultiplicity; }; /** * Whether a program checks: its program, the type of each output and how many * there are are present exactly when no diagnostic is an error. * * @param checked - what `checkJq` made of the program * @returns whether it checks */ export declare function programChecks(checked: CheckJqResult): checked is CheckedProgram; /** * A query's one-shot call, ready to send, and what reading its answer needs. * * @property checked - What the checker made of the program: the program as written, the type of each output and how many there are, and the spans a runtime error is placed by * @property entries - The data sources the query reads, one per argument of the call, in its order * @property request - The call, which e3-api-client's `oneShotExecute` sends * @property maxOutputs - The most outputs a `many` query returns */ export interface PreparedQuery { /** What the checker made of the program: the program as written, the type of each output and how many there are, and the spans a runtime error is placed by. */ readonly checked: CheckedProgram; /** The data sources the query reads, one per argument of the call, in its order: the order the query first reads them. */ readonly entries: QueryRootEntry[]; /** The call, which e3-api-client's `oneShotExecute` sends. */ readonly request: OneShotRequest; /** The most outputs a `many` query returns; the call gives one more, which says the answer was cut short. */ readonly maxOutputs: number; } /** * What reading a call's answer needs ({@link queryResultOf}): the checker's * record of the query that ran — the type of each output, and how many there * are — the data sources in the call's argument order, and its most outputs. A * one-shot call's {@link PreparedQuery} is one; so is a split call's, which * the planner gives (`plan.ts`). */ export type QueryReading = Pick; /** A call's limits and runner, as its options set them: what a one-shot call and a split call both send. */ export interface QueryLimits { /** The most outputs a `many` query returns. */ readonly maxOutputs: number; /** The call's limits: its largest answer as `maxResultBytes`, and its time limit when the options name one. */ readonly limits: OneShotRequest["limits"]; /** The runner: east-c given no platform package, unless the options name another. */ readonly runner: RunnerValue; } /** * A call's limits and runner, from its options: its most outputs and its * largest answer their defaults when the options give none, and no time limit * unless the options name one, so the server's applies (#1131). * * @param options - the call's limits and its runner * @returns the most outputs, the limits and the runner * @throws {RangeError} When a limit is not a whole number of at least 1. */ export declare function queryLimits(options?: QueryOptions): QueryLimits; /** * Prepares a query's one-shot call: checks the program once against the whole * root, translates it, and builds the request. * * @param program - the query's jq text * @param root - the root its data sources make ({@link queryRoot}) * @param options - the call's limits and its runner * @returns the prepared call; or, for a program that does not check, the * `error` result a run of it gives * @throws {RangeError} When a limit is not a whole number of at least 1. * @throws {TranslationError} When the checked program holds something the * translator cannot express: a gap in the translation, never a mistake in * the query. * * @remarks * - **One check.** The program is checked with * `checkJq(program, root.type, { root: true })`, once, against the whole * root, which is never narrowed to what the query reads (#1041): `keys` on * the root answers every name. A problem of error severity is an `error` * result holding the checker's diagnostics, lints included, and no input, * since nothing ran; reading the whole root is one. * - **The translation.** `translateJq` translates it, a `many` query asked * for at most `maxOutputs + 1` outputs (#923). Its parameters are the * datasets it reads, in the order it first reads them. * - **The request.** `bodyIr` is the translation as an East function, its IR * encoded with its source map, so a runtime error names its place in the * jq; `args` is one `dataset` argument per dataset read, in the * translation's order; `runner` is east-c given no platform package, unless * the options name another; `limits` holds `maxBytes` as `maxResultBytes`, * and `timeoutMs` when the options give one. A call that names no time limit * runs under the server's: its default for a call that names none, under * the longest a request to it may take (#1131). * - **Platform-free.** A translation calls East's builtins only, so the body * holds no `Platform` node, and with the default runner any caller who may * read the workspace may run the call (#1031). * * @example * ```ts * const prepared = prepareQuery(".orders | map(.total) | add", root, { timeoutMs: 10_000 }); * if ("result" in prepared) return prepared.result; // the checker's problems * const result = await oneShotExecute(url, repo, workspace, prepared.prepared.request, { token }); * return queryResultOf(prepared.prepared, result); * ``` */ export declare function prepareQuery(program: string, root: QueryRoot, options?: QueryOptions): { prepared: PreparedQuery; } | { result: QueryResult; }; /** * Prepares a query's one-shot call from what the checker made of it: what * {@link prepareQuery} does once it has checked the program, for a caller that * has checked it already — the planner, which checks a query once to split it * and to call it. * * @param checked - what `checkJq` made of the program, against `root.type`, as an e3 root * @param root - the root * @param options - the call's limits and its runner * @returns the prepared call; or, for a program that does not check, the `error` result a run of it gives * @throws {RangeError} When a limit is not a whole number of at least 1. * @throws {TranslationError} When the checked program holds something the translator cannot express. */ export declare function prepareCheckedQuery(checked: CheckJqResult, root: QueryRoot, options?: QueryOptions): { prepared: PreparedQuery; } | { result: QueryResult; }; /** * The data source of a root a query reads by a name. * * @param root - the root * @param name - the root field the translation, or the split, reads * @param caller - what asks, for the error: `prepareQuery` by default * @returns the data source * @throws {Error} When the root has none of that name: its type was not made from its entries. */ export declare function entryNamed(root: QueryRoot, name: string | null, caller?: string): QueryRootEntry; /** * What a query's call answered: a one-shot call's, or a split call's. * * @param prepared - the call, as {@link prepareQuery} prepared it; or a split * call's reading, as the planner gives it — both a {@link QueryReading} * @param result - what e3-api-client's `oneShotExecute` returned for it, or a * split call's result (`splitCall`'s `result`) * @returns the answer: how the run ended, and the datasets it read * @throws {Error} When the call read a dataset other than the one the query * reads there, or ended in a draft's `conflict`, which a query's call, laying * no draft, never does: the result is not this call's. * * @remarks * - `success` is `ok`, the value decoded with the translation's result type. * A `many` answer of more than `maxOutputs` outputs — the translation gives * one more — is `truncated`, its last output dropped. `result` is * self-describing beast2 of the answer kept, at the result type, and * `outputs` counts its outputs: one for a `one` query, none or one for * `maybe`. * - `failed` for a platform function the runner could not find is * `needs_platform`, naming each: a function value in the data may call one * (`devdocs/QUERY.md` §9). * - `failed` otherwise is an `error` of one `runtime` diagnostic: the * runner's message, at the jq node that raised it, placed by east's * `runtimeErrorAt` as `evaluateJq` places one (`devdocs/QUERY.md` §15.3). * A failure with no message says what the runner last said, or its exit * code. * - `invalid` is an `error`: `no_value` for a dataset e3 found unassigned * ("orders has no value yet"), and `invalid` for anything else it refused. * - `timed_out` and `too_large` are as e3 reported them. * - `conflict` is no answer to a query's call, which lays no draft: it is * refused, as a result that is not the call's. * * `inputs` names each dataset the run read, with the hash e3 pinned it at, in * the call's argument order: empty when nothing ran. * * @example * ```ts * const result = queryResultOf(prepared, await oneShotExecute(url, repo, workspace, prepared.request, { token })); * if (result.outcome.type === "ok") show(decodeBeast2(result.outcome.value.result), result.outcome.value.truncated); * ``` */ export declare function queryResultOf(prepared: QueryReading, result: ExecuteResult): QueryResult; //# sourceMappingURL=one-shot.d.ts.map