@colin_kerr/query-agent-client 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +20 -2
- package/dist/cjs/agent-client.d.ts +107 -0
- package/dist/cjs/agent-client.js +142 -0
- package/dist/cjs/agent-client.js.map +1 -0
- package/dist/cjs/caps.d.ts +43 -0
- package/dist/cjs/caps.js +51 -0
- package/dist/cjs/caps.js.map +1 -0
- package/dist/cjs/client.d.ts +145 -0
- package/dist/cjs/client.js +270 -0
- package/dist/cjs/client.js.map +1 -0
- package/dist/cjs/index.d.ts +11 -0
- package/dist/cjs/index.js +28 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/node/index.d.ts +27 -0
- package/dist/cjs/node/index.js +59 -0
- package/dist/cjs/node/index.js.map +1 -0
- package/dist/cjs/query-runner.d.ts +34 -0
- package/dist/cjs/query-runner.js +41 -0
- package/dist/cjs/query-runner.js.map +1 -0
- package/dist/cjs/result-pages.d.ts +69 -0
- package/dist/cjs/result-pages.js +176 -0
- package/dist/cjs/result-pages.js.map +1 -0
- package/dist/cjs/selection.d.ts +90 -0
- package/dist/cjs/selection.js +222 -0
- package/dist/cjs/selection.js.map +1 -0
- package/dist/cjs/startup-queries.json +182 -0
- package/dist/cjs/turn-api.d.ts +156 -0
- package/dist/cjs/turn-api.js +19 -0
- package/dist/cjs/turn-api.js.map +1 -0
- package/dist/esm/agent-client.d.ts +107 -0
- package/dist/esm/agent-client.js +134 -0
- package/dist/esm/agent-client.js.map +1 -0
- package/dist/esm/caps.d.ts +43 -0
- package/dist/esm/caps.js +48 -0
- package/dist/esm/caps.js.map +1 -0
- package/dist/esm/client.d.ts +145 -0
- package/dist/esm/client.js +260 -0
- package/dist/esm/client.js.map +1 -0
- package/dist/esm/index.d.ts +11 -0
- package/dist/esm/index.js +12 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/query-runner.d.ts +34 -0
- package/dist/esm/query-runner.js +38 -0
- package/dist/esm/query-runner.js.map +1 -0
- package/dist/esm/result-pages.d.ts +69 -0
- package/dist/esm/result-pages.js +172 -0
- package/dist/esm/result-pages.js.map +1 -0
- package/dist/esm/selection.d.ts +90 -0
- package/dist/esm/selection.js +212 -0
- package/dist/esm/selection.js.map +1 -0
- package/dist/esm/startup-queries.json +182 -0
- package/dist/esm/turn-api.d.ts +156 -0
- package/dist/esm/turn-api.js +16 -0
- package/dist/esm/turn-api.js.map +1 -0
- package/package.json +67 -4
package/dist/esm/caps.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading a query's rows up to the caps the function sends: the client's half of the row format.
|
|
3
|
+
*
|
|
4
|
+
* The function writes the text the model sees from these rows. The two halves must agree exactly,
|
|
5
|
+
* so the rules here are the turn API's, and the function is tested against them.
|
|
6
|
+
*/
|
|
7
|
+
/** A string cut to the cap, as the function will show it: used only to measure the line. */
|
|
8
|
+
function asShown(value, maxCellChars) {
|
|
9
|
+
if (typeof value !== "string" || value.length <= maxCellChars)
|
|
10
|
+
return value;
|
|
11
|
+
return `${value.slice(0, maxCellChars)}…[clipped, ${value.length} chars]`;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Read a result up to the caps:
|
|
15
|
+
*
|
|
16
|
+
* 1. stop before a row once there are `maxRows` rows (`truncatedBy: "rows"`);
|
|
17
|
+
* 2. stop before a row whose line would take the total past `maxChars` (`truncatedBy: "chars"`),
|
|
18
|
+
* but always keep the first row. A line is the row as a JSON array, with each long string as
|
|
19
|
+
* the function will show it, plus one character; the total starts at the length of
|
|
20
|
+
* `columns: ` and the column names as a JSON array;
|
|
21
|
+
* 3. cut each string to `maxCellChars`, and add `[row, column, full length]` to `clipped`.
|
|
22
|
+
*/
|
|
23
|
+
export async function readCapped(result, caps) {
|
|
24
|
+
let chars = `columns: ${JSON.stringify(result.columns)}`.length;
|
|
25
|
+
const rows = [];
|
|
26
|
+
const clipped = [];
|
|
27
|
+
let truncatedBy;
|
|
28
|
+
for await (const row of result.rows) {
|
|
29
|
+
if (rows.length >= caps.maxRows) {
|
|
30
|
+
truncatedBy = "rows";
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
const line = JSON.stringify(row.map((value) => asShown(value, caps.maxCellChars)));
|
|
34
|
+
if (chars + line.length + 1 > caps.maxChars && rows.length > 0) {
|
|
35
|
+
truncatedBy = "chars";
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
rows.push(row.map((value, column) => {
|
|
39
|
+
if (typeof value !== "string" || value.length <= caps.maxCellChars)
|
|
40
|
+
return value;
|
|
41
|
+
clipped.push([rows.length, column, value.length]);
|
|
42
|
+
return value.slice(0, caps.maxCellChars);
|
|
43
|
+
}));
|
|
44
|
+
chars += line.length + 1;
|
|
45
|
+
}
|
|
46
|
+
return { columns: result.columns, rows, ...(clipped.length > 0 ? { clipped } : {}), ...(truncatedBy ? { truncatedBy } : {}) };
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=caps.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"caps.js","sourceRoot":"","sources":["../../src/caps.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AA6BH,4FAA4F;AAC5F,SAAS,OAAO,CAAC,KAAc,EAAE,YAAoB;IACnD,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,CAAC,MAAM,IAAI,YAAY;QAC3D,OAAO,KAAK,CAAC;IACf,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,YAAY,CAAC,cAAc,KAAK,CAAC,MAAM,SAAS,CAAC;AAC5E,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,UAAU,CAAC,MAAsB,EAAE,IAAgB;IACvE,IAAI,KAAK,GAAG,YAAY,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,OAAO,CAAC,EAAE,CAAC,MAAM,C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,CAAC,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE,WAAW,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;AAChI,CAAC"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client's half of a turn: running the queries the function asks for and reading their rows,
|
|
3
|
+
* and running the startup query list before the first turn.
|
|
4
|
+
*
|
|
5
|
+
* The function never reaches the iModel. Each response names the queries to run and the caps to
|
|
6
|
+
* read them to; the client runs them on its own iModel and sends the rows with the next turn. The
|
|
7
|
+
* startup list's rows go with every turn, and the function writes them into the instructions.
|
|
8
|
+
*/
|
|
9
|
+
import { type RunRawQuery } from "./caps.js";
|
|
10
|
+
import type { ResultPages } from "./result-pages.js";
|
|
11
|
+
import { type AnswerMode, type ResultType, type EarlierQuestion, type QueryRows, type ResultCaps, type StartupResult, type StopReason, type SubmittedAnswer, type TurnRequest, type TurnResponse } from "./turn-api.js";
|
|
12
|
+
/** What the client supplies to a run, wherever the function runs. */
|
|
13
|
+
export interface ClientOptions {
|
|
14
|
+
/** Absent when no iModel is open: the agent is told so and `runQuery` is not offered. */
|
|
15
|
+
runQuery?: RunRawQuery;
|
|
16
|
+
/** The open iModel: its name, and the rows of the startup query list (`runStartupQueries`), run once per iModel. */
|
|
17
|
+
imodel?: {
|
|
18
|
+
name: string;
|
|
19
|
+
startup?: StartupResult[];
|
|
20
|
+
};
|
|
21
|
+
editorText?: string;
|
|
22
|
+
/** The chat's earlier questions, oldest first (`earlierQuestion`). The function shortens them to fit. */
|
|
23
|
+
earlier?: readonly EarlierQuestion[];
|
|
24
|
+
/** The token of the chat's last finished run. Opaque: kept and sent back, never read. */
|
|
25
|
+
token?: string;
|
|
26
|
+
/** How the answer is to be given (`AnswerMode`). Sent with every turn. */
|
|
27
|
+
answerMode?: AnswerMode;
|
|
28
|
+
/** Called before each turn is sent (`thinking`), and before each query runs (`querying`, with its ECSql). */
|
|
29
|
+
onPhase?: (phase: "thinking" | "querying", ecsql?: string) => void;
|
|
30
|
+
signal?: AbortSignal;
|
|
31
|
+
}
|
|
32
|
+
/** The finished run, as the client keeps it: the answer, and what the chat's next question needs. */
|
|
33
|
+
export interface AgentRun {
|
|
34
|
+
/** The final prose answer, which should carry exactly one fenced sql block. */
|
|
35
|
+
answer: string;
|
|
36
|
+
/** The query pulled out of the answer, if there was one. */
|
|
37
|
+
query?: string;
|
|
38
|
+
stoppedBecause: StopReason;
|
|
39
|
+
/** The answer as submitted with `submitAnswer`, when the run ended that way. */
|
|
40
|
+
structured?: SubmittedAnswer;
|
|
41
|
+
/** Names the run's record, where the function keeps one. */
|
|
42
|
+
runId: string;
|
|
43
|
+
/** The token for the chat's next question. Opaque: the agent's own state, sealed. */
|
|
44
|
+
token: string;
|
|
45
|
+
/** The answer mode the run was asked in, as the host sent it. */
|
|
46
|
+
answerMode?: AnswerMode;
|
|
47
|
+
/**
|
|
48
|
+
* In the `auto` and `select` modes: what the answer is (`done.resultType`). `selection`: its
|
|
49
|
+
* query is the selection, and no rows clears it. `answer`: select its rows only when they carry
|
|
50
|
+
* `ECInstanceId` and `ClassName`.
|
|
51
|
+
*/
|
|
52
|
+
resultType?: ResultType;
|
|
53
|
+
/** The answer's query's rows, read once when the run ended (`AgentClient.ask`), or its error. */
|
|
54
|
+
answerRows?: AnswerRows;
|
|
55
|
+
/**
|
|
56
|
+
* All the answer's rows, with no limit, read a page at a time as a table asks for them. Present
|
|
57
|
+
* when the answer has a query and there is an iModel. For the host only: never sent to the function.
|
|
58
|
+
*/
|
|
59
|
+
results?: ResultPages;
|
|
60
|
+
}
|
|
61
|
+
/** An answer's query's rows, as the client read them, or the error it gave. */
|
|
62
|
+
export type AnswerRows = Pick<QueryRows, "columns" | "rows" | "clipped" | "truncatedBy" | "error">;
|
|
63
|
+
/**
|
|
64
|
+
* An answered question as a later question of the chat sends it: as the user saw it, with the
|
|
65
|
+
* answer's rows cut to `HISTORY_CAPS`.
|
|
66
|
+
*/
|
|
67
|
+
export declare function earlierQuestion(question: string, run: AgentRun): Promise<EarlierQuestion>;
|
|
68
|
+
/** One call to the function: over HTTP (`fetchTurns`), or in the same process, for tests. */
|
|
69
|
+
export type TurnCall = (request: TurnRequest, signal?: AbortSignal) => Promise<TurnResponse>;
|
|
70
|
+
/**
|
|
71
|
+
* Drive a run: send a turn, run the queries the response asks for, send their rows with the next
|
|
72
|
+
* turn, until the response says the run is over. When the user stops the run, the next turn says
|
|
73
|
+
* so, and the function ends it.
|
|
74
|
+
*/
|
|
75
|
+
export declare function driveTurns(question: string, client: ClientOptions, turn: TurnCall): Promise<AgentRun>;
|
|
76
|
+
/**
|
|
77
|
+
* The function refused a turn for a reason the user can act on. `message` is the function's one
|
|
78
|
+
* sentence, meant to be shown as it is; `status` says which: 401 or 403 ask for an access key, 429
|
|
79
|
+
* means the key is used up, which can happen in the middle of a question, 413 means the request is
|
|
80
|
+
* too long, 400 means the chat must start again.
|
|
81
|
+
*/
|
|
82
|
+
export declare class TurnRefused extends Error {
|
|
83
|
+
readonly status: number;
|
|
84
|
+
constructor(status: number, message: string);
|
|
85
|
+
}
|
|
86
|
+
export interface FetchTurnsOptions {
|
|
87
|
+
/**
|
|
88
|
+
* The user's credential for the function, sent with every turn as `Authorization: Bearer <it>`:
|
|
89
|
+
* the access key the owner gave them. A function that checks access refuses a turn without one
|
|
90
|
+
* (`TurnRefused`, 401).
|
|
91
|
+
*/
|
|
92
|
+
getAccessToken?: () => Promise<string | undefined>;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Turns over HTTP: `POST <url>/turn`, the same code in a browser and in Node. When the user stops
|
|
96
|
+
* the run while a turn is in flight, the same turn is sent again, saying the run was stopped, so
|
|
97
|
+
* the function ends it with what it has.
|
|
98
|
+
*/
|
|
99
|
+
export declare function fetchTurns(url: string, options?: FetchTurnsOptions): TurnCall;
|
|
100
|
+
/**
|
|
101
|
+
* Run one query and read it up to the caps. A failure, at the start or part way through the
|
|
102
|
+
* rows, is sent as the query's error: the function hands it to the model to correct.
|
|
103
|
+
*/
|
|
104
|
+
export declare function readQuery(runQuery: RunRawQuery, ecsql: string, caps: ResultCaps): Promise<QueryRows>;
|
|
105
|
+
/** One query of the startup list. */
|
|
106
|
+
export interface StartupQuery {
|
|
107
|
+
rule: string;
|
|
108
|
+
id: string;
|
|
109
|
+
ecsql: string;
|
|
110
|
+
/** Rows sent. The rest are counted, not sent. */
|
|
111
|
+
max?: number;
|
|
112
|
+
/** Run only when these queries of the same rule were empty. */
|
|
113
|
+
onlyIfEmpty?: string[];
|
|
114
|
+
/** A second query run for every row sent, with the row's columns as `{0}`, `{1}`. It always runs to the end. */
|
|
115
|
+
each?: string;
|
|
116
|
+
}
|
|
117
|
+
/** The startup query list (`startup-queries.json`), which the client library carries. */
|
|
118
|
+
export interface StartupList {
|
|
119
|
+
description?: string[];
|
|
120
|
+
lists: Record<string, string[]>;
|
|
121
|
+
queries: StartupQuery[];
|
|
122
|
+
}
|
|
123
|
+
/** One startup query run, for a report of what was asked and how long it took. */
|
|
124
|
+
export interface QueryReport {
|
|
125
|
+
rule: string;
|
|
126
|
+
query: string;
|
|
127
|
+
ecsql: string;
|
|
128
|
+
ms: number;
|
|
129
|
+
rows: number;
|
|
130
|
+
skipped?: boolean;
|
|
131
|
+
/** A failed query is an empty one: the class or schema is not in this file. */
|
|
132
|
+
error?: string;
|
|
133
|
+
}
|
|
134
|
+
/** Expand `{{in:list}}`, `{{like:expr:list}}` and `{{notlike:expr:list}}`. An unknown list throws. */
|
|
135
|
+
export declare function expandLists(ecsql: string, lists: Record<string, string[]>): string;
|
|
136
|
+
/**
|
|
137
|
+
* Run the startup list on the open iModel. Never throws for a query: a failure is an empty
|
|
138
|
+
* result. Only a list that names a list it does not define throws.
|
|
139
|
+
*/
|
|
140
|
+
export declare function runStartupQueries(list: StartupList, runQuery: RunRawQuery): Promise<{
|
|
141
|
+
results: StartupResult[];
|
|
142
|
+
reports: QueryReport[];
|
|
143
|
+
}>;
|
|
144
|
+
/** The earlier name of `fetchTurns`. */
|
|
145
|
+
export declare const httpTurns: typeof fetchTurns;
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The client's half of a turn: running the queries the function asks for and reading their rows,
|
|
3
|
+
* and running the startup query list before the first turn.
|
|
4
|
+
*
|
|
5
|
+
* The function never reaches the iModel. Each response names the queries to run and the caps to
|
|
6
|
+
* read them to; the client runs them on its own iModel and sends the rows with the next turn. The
|
|
7
|
+
* startup list's rows go with every turn, and the function writes them into the instructions.
|
|
8
|
+
*/
|
|
9
|
+
import { readCapped } from "./caps.js";
|
|
10
|
+
import { HISTORY_CAPS, } from "./turn-api.js";
|
|
11
|
+
/**
|
|
12
|
+
* An answered question as a later question of the chat sends it: as the user saw it, with the
|
|
13
|
+
* answer's rows cut to `HISTORY_CAPS`.
|
|
14
|
+
*/
|
|
15
|
+
export async function earlierQuestion(question, run) {
|
|
16
|
+
const query = run.structured?.query || run.query;
|
|
17
|
+
return {
|
|
18
|
+
question,
|
|
19
|
+
answer: run.answer,
|
|
20
|
+
...(query ? { query } : {}),
|
|
21
|
+
...(run.answerRows ? { rows: await historyRows(run.answerRows) } : {}),
|
|
22
|
+
stoppedBecause: run.stoppedBecause,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Rows read with the live caps, cut again to `HISTORY_CAPS`, by the same rule as a read. A cell the
|
|
27
|
+
* first read already cut keeps its full length.
|
|
28
|
+
*/
|
|
29
|
+
async function historyRows(read) {
|
|
30
|
+
if (read.error !== undefined)
|
|
31
|
+
return { error: read.error };
|
|
32
|
+
const cut = await readCapped({ columns: read.columns ?? [], rows: (async function* () { yield* read.rows ?? []; })() }, HISTORY_CAPS);
|
|
33
|
+
const before = new Map((read.clipped ?? []).map(([row, column, length]) => [`${row},${column}`, length]));
|
|
34
|
+
const clipped = (cut.clipped ?? []).map(([row, column, length]) => [row, column, before.get(`${row},${column}`) ?? length]);
|
|
35
|
+
for (const [key, length] of before) {
|
|
36
|
+
const [row, column] = key.split(",").map(Number);
|
|
37
|
+
if (row < (cut.rows?.length ?? 0) && !clipped.some(([r, c]) => r === row && c === column))
|
|
38
|
+
clipped.push([row, column, length]);
|
|
39
|
+
}
|
|
40
|
+
const truncatedBy = cut.truncatedBy ?? read.truncatedBy;
|
|
41
|
+
return { columns: cut.columns, rows: cut.rows, ...(clipped.length > 0 ? { clipped } : {}), ...(truncatedBy ? { truncatedBy } : {}) };
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Drive a run: send a turn, run the queries the response asks for, send their rows with the next
|
|
45
|
+
* turn, until the response says the run is over. When the user stops the run, the next turn says
|
|
46
|
+
* so, and the function ends it.
|
|
47
|
+
*/
|
|
48
|
+
export async function driveTurns(question, client, turn) {
|
|
49
|
+
const { runQuery } = client;
|
|
50
|
+
let token = client.token;
|
|
51
|
+
let results;
|
|
52
|
+
for (;;) {
|
|
53
|
+
const request = {
|
|
54
|
+
question, canQuery: runQuery !== undefined, imodel: client.imodel, editorText: client.editorText,
|
|
55
|
+
...(client.earlier && client.earlier.length > 0 ? { earlier: [...client.earlier] } : {}),
|
|
56
|
+
...(token ? { token } : {}),
|
|
57
|
+
...(results ? { results } : {}),
|
|
58
|
+
...(client.signal?.aborted ? { aborted: true } : {}),
|
|
59
|
+
...(client.answerMode ? { answerMode: client.answerMode } : {}),
|
|
60
|
+
};
|
|
61
|
+
client.onPhase?.("thinking");
|
|
62
|
+
const response = await turn(request, client.signal);
|
|
63
|
+
token = response.token;
|
|
64
|
+
const done = response.done;
|
|
65
|
+
if (done) {
|
|
66
|
+
return {
|
|
67
|
+
answer: done.answer,
|
|
68
|
+
...(done.query ? { query: done.query } : {}),
|
|
69
|
+
stoppedBecause: done.stoppedBecause,
|
|
70
|
+
...(done.structured ? { structured: done.structured } : {}),
|
|
71
|
+
runId: done.runId,
|
|
72
|
+
token: response.token,
|
|
73
|
+
...(done.resultType ? { resultType: done.resultType } : {}),
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
results = [];
|
|
77
|
+
for (const ecsql of response.queries) {
|
|
78
|
+
client.onPhase?.("querying", ecsql);
|
|
79
|
+
results.push(await readQuery(runQuery, ecsql, response.caps));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/** The refusals a user can act on: a chat to start again (400), a key to give or replace (401, 403), too long a request (413), a key used up (429). */
|
|
84
|
+
const REFUSALS = new Set([400, 401, 403, 413, 429]);
|
|
85
|
+
/**
|
|
86
|
+
* The function refused a turn for a reason the user can act on. `message` is the function's one
|
|
87
|
+
* sentence, meant to be shown as it is; `status` says which: 401 or 403 ask for an access key, 429
|
|
88
|
+
* means the key is used up, which can happen in the middle of a question, 413 means the request is
|
|
89
|
+
* too long, 400 means the chat must start again.
|
|
90
|
+
*/
|
|
91
|
+
export class TurnRefused extends Error {
|
|
92
|
+
status;
|
|
93
|
+
constructor(status, message) {
|
|
94
|
+
super(message);
|
|
95
|
+
this.status = status;
|
|
96
|
+
this.name = "TurnRefused";
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Turns over HTTP: `POST <url>/turn`, the same code in a browser and in Node. When the user stops
|
|
101
|
+
* the run while a turn is in flight, the same turn is sent again, saying the run was stopped, so
|
|
102
|
+
* the function ends it with what it has.
|
|
103
|
+
*/
|
|
104
|
+
export function fetchTurns(url, options = {}) {
|
|
105
|
+
const post = async (request, signal) => {
|
|
106
|
+
const token = await options.getAccessToken?.();
|
|
107
|
+
const response = await fetch(`${url.replace(/\/+$/, "")}/turn`, {
|
|
108
|
+
method: "POST",
|
|
109
|
+
headers: { "Content-Type": "application/json", ...(token ? { Authorization: `Bearer ${token}` } : {}) },
|
|
110
|
+
body: JSON.stringify(request),
|
|
111
|
+
signal,
|
|
112
|
+
});
|
|
113
|
+
const body = await response.json();
|
|
114
|
+
const reason = body.error;
|
|
115
|
+
if (!response.ok && REFUSALS.has(response.status) && reason)
|
|
116
|
+
throw new TurnRefused(response.status, reason);
|
|
117
|
+
if (!response.ok)
|
|
118
|
+
throw new Error(`The agent function refused the turn (HTTP ${response.status}): ${reason ?? "no reason given"}`);
|
|
119
|
+
return body;
|
|
120
|
+
};
|
|
121
|
+
return async (request, signal) => {
|
|
122
|
+
// The turn that says the run was stopped must get through, so it is not tied to the signal.
|
|
123
|
+
if (request.aborted)
|
|
124
|
+
return post(request);
|
|
125
|
+
try {
|
|
126
|
+
return await post(request, signal);
|
|
127
|
+
}
|
|
128
|
+
catch (err) {
|
|
129
|
+
if (!signal?.aborted)
|
|
130
|
+
throw err;
|
|
131
|
+
return post({ ...request, aborted: true });
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Run one query and read it up to the caps. A failure, at the start or part way through the
|
|
137
|
+
* rows, is sent as the query's error: the function hands it to the model to correct.
|
|
138
|
+
*/
|
|
139
|
+
export async function readQuery(runQuery, ecsql, caps) {
|
|
140
|
+
const started = Date.now();
|
|
141
|
+
try {
|
|
142
|
+
const read = await readCapped(await runQuery(ecsql), caps);
|
|
143
|
+
return { ecsql, ...read, ms: Date.now() - started };
|
|
144
|
+
}
|
|
145
|
+
catch (err) {
|
|
146
|
+
return { ecsql, error: err instanceof Error ? err.message : String(err), ms: Date.now() - started };
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/** How many result rows are read at most, whatever `max` says: a guard, not a display limit. */
|
|
150
|
+
const ROW_GUARD = 500;
|
|
151
|
+
function quote(value) {
|
|
152
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
153
|
+
}
|
|
154
|
+
/** Expand `{{in:list}}`, `{{like:expr:list}}` and `{{notlike:expr:list}}`. An unknown list throws. */
|
|
155
|
+
export function expandLists(ecsql, lists) {
|
|
156
|
+
return ecsql.replace(/\{\{(in|like|notlike):([^}]+)\}\}/g, (_whole, kind, rest) => {
|
|
157
|
+
const at = rest.lastIndexOf(":");
|
|
158
|
+
const name = kind === "in" ? rest : rest.slice(at + 1);
|
|
159
|
+
const values = lists[name];
|
|
160
|
+
if (!values || values.length === 0)
|
|
161
|
+
throw new Error(`startup list: no list named "${name}"`);
|
|
162
|
+
if (kind === "in")
|
|
163
|
+
return values.map(quote).join(", ");
|
|
164
|
+
const expression = rest.slice(0, at);
|
|
165
|
+
const terms = values.map((value) => `${expression} ${kind === "like" ? "LIKE" : "NOT LIKE"} ${quote(value.includes("%") ? value : `%${value}%`)}`);
|
|
166
|
+
return `(${terms.join(kind === "like" ? " OR " : " AND ")})`;
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* A row's values inside the `each` query. They come from the file and land inside `[...]`, so
|
|
171
|
+
* a value that could close the bracket is refused; the row then sends null for `each`.
|
|
172
|
+
*/
|
|
173
|
+
function fillIdentifiers(template, row) {
|
|
174
|
+
let safe = true;
|
|
175
|
+
const text = template.replace(/\{(\d+)\}/g, (_whole, index) => {
|
|
176
|
+
const value = String(row[Number(index)] ?? "");
|
|
177
|
+
if (!/^[A-Za-z0-9_]+$/.test(value))
|
|
178
|
+
safe = false;
|
|
179
|
+
return value;
|
|
180
|
+
});
|
|
181
|
+
return safe ? text : undefined;
|
|
182
|
+
}
|
|
183
|
+
async function readRows(runQuery, ecsql) {
|
|
184
|
+
const result = await runQuery(ecsql);
|
|
185
|
+
const rows = [];
|
|
186
|
+
for await (const row of result.rows) {
|
|
187
|
+
rows.push(row);
|
|
188
|
+
if (rows.length >= ROW_GUARD)
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
return rows;
|
|
192
|
+
}
|
|
193
|
+
/** No rows, or the single cell of a count that came back 0. The function applies the same test. */
|
|
194
|
+
function isEmpty(rows) {
|
|
195
|
+
return rows.length === 0 || (rows.length === 1 && rows[0].length === 1 && (rows[0][0] === 0 || rows[0][0] === null));
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Run the startup list on the open iModel. Never throws for a query: a failure is an empty
|
|
199
|
+
* result. Only a list that names a list it does not define throws.
|
|
200
|
+
*/
|
|
201
|
+
export async function runStartupQueries(list, runQuery) {
|
|
202
|
+
const results = [];
|
|
203
|
+
const reports = [];
|
|
204
|
+
const emptyByRule = new Map();
|
|
205
|
+
for (const query of list.queries) {
|
|
206
|
+
const empty = emptyByRule.get(query.rule) ?? new Set();
|
|
207
|
+
emptyByRule.set(query.rule, empty);
|
|
208
|
+
const ecsql = expandLists(query.ecsql, list.lists);
|
|
209
|
+
if (query.onlyIfEmpty && !query.onlyIfEmpty.every((id) => empty.has(id))) {
|
|
210
|
+
reports.push({ rule: query.rule, query: query.id, ecsql, ms: 0, rows: 0, skipped: true });
|
|
211
|
+
results.push({ rule: query.rule, id: query.id, rows: [], count: 0, skipped: true, ms: 0 });
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
const started = Date.now();
|
|
215
|
+
let rows = [];
|
|
216
|
+
let error;
|
|
217
|
+
try {
|
|
218
|
+
rows = await readRows(runQuery, ecsql);
|
|
219
|
+
}
|
|
220
|
+
catch (caught) {
|
|
221
|
+
error = caught.message;
|
|
222
|
+
}
|
|
223
|
+
const ms = Date.now() - started;
|
|
224
|
+
reports.push({ rule: query.rule, query: query.id, ecsql, ms, rows: rows.length, error });
|
|
225
|
+
const shown = rows.slice(0, query.max ?? 20);
|
|
226
|
+
const result = {
|
|
227
|
+
rule: query.rule, id: query.id, rows: shown, count: rows.length, ms,
|
|
228
|
+
...(rows.length >= ROW_GUARD ? { guarded: true } : {}), ...(error !== undefined ? { error } : {}),
|
|
229
|
+
};
|
|
230
|
+
if (isEmpty(rows)) {
|
|
231
|
+
empty.add(query.id);
|
|
232
|
+
}
|
|
233
|
+
else if (query.each) {
|
|
234
|
+
const each = [];
|
|
235
|
+
for (const row of shown) {
|
|
236
|
+
const eachSql = fillIdentifiers(query.each, row);
|
|
237
|
+
if (!eachSql) {
|
|
238
|
+
each.push(null);
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
const eachStarted = Date.now();
|
|
242
|
+
try {
|
|
243
|
+
const found = await readRows(runQuery, eachSql);
|
|
244
|
+
each.push(String(found[0]?.[0] ?? 0));
|
|
245
|
+
reports.push({ rule: query.rule, query: `${query.id}/each`, ecsql: eachSql, ms: Date.now() - eachStarted, rows: found.length });
|
|
246
|
+
}
|
|
247
|
+
catch (caught) {
|
|
248
|
+
each.push(null);
|
|
249
|
+
reports.push({ rule: query.rule, query: `${query.id}/each`, ecsql: eachSql, ms: Date.now() - eachStarted, rows: 0, error: caught.message });
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
result.each = each;
|
|
253
|
+
}
|
|
254
|
+
results.push(result);
|
|
255
|
+
}
|
|
256
|
+
return { results, reports };
|
|
257
|
+
}
|
|
258
|
+
/** The earlier name of `fetchTurns`. */
|
|
259
|
+
export const httpTurns = fetchTurns;
|
|
260
|
+
//# sourceMappingURL=client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.js","sourceRoot":"","sources":["../../src/client.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,UAAU,EAAoB,MAAM,WAAW,CAAC;AAEzD,OAAO,EACL,YAAY,GAEb,MAAM,eAAe,CAAC;AAqDvB;;;GAGG;AACH,MAAM,CAAC,KAAK,UAAU,eAAe,CAAC,QAAgB,EAAE,GAAa;IACnE,MAAM,KAAK,GAAG,GAAG,CAAC,UAAU,EAAE,KAAK,IAAI,GAAG,CAAC,KAAK,CAAC;IACjD,OAAO;QACL,QAAQ;QACR,MAAM,EAAE,GAAG,CAAC,MAAM;QAClB,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;QAC3B,GAAG,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,EAAE,IA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AI,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;gBAClI,CAAC;gBAAC,OAAO,MAAM,EAAE,CAAC;oBAChB,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;oBAChB,OAAO,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,GAAG,KAAK,CAAC,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,WAAW,EAAE,IAAI,EAAE,CAAC,EAAE,KAAK,EAAG,MAAgB,CAAC,OAAO,EAAE,CAAC,CAAC;gBACzJ,CAAC;YACH,CAAC;YACD,MAAM,CAAC,IAAI,GAAG,IAAI,CAAC;QACrB,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;IACvB,CAAC;IACD,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,CAAC;AAC9B,CAAC;AAED,wCAAwC;AACxC,MAAM,CAAC,MAAM,SAAS,GAAG,UAAU,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The query agent's client library: it runs the queries on the host's iModel, and drives the
|
|
3
|
+
* turns with the agent function (`docs/design.md`).
|
|
4
|
+
*/
|
|
5
|
+
export * from "./turn-api.js";
|
|
6
|
+
export * from "./caps.js";
|
|
7
|
+
export * from "./client.js";
|
|
8
|
+
export * from "./query-runner.js";
|
|
9
|
+
export * from "./agent-client.js";
|
|
10
|
+
export * from "./selection.js";
|
|
11
|
+
export * from "./result-pages.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The query agent's client library: it runs the queries on the host's iModel, and drives the
|
|
3
|
+
* turns with the agent function (`docs/design.md`).
|
|
4
|
+
*/
|
|
5
|
+
export * from "./turn-api.js";
|
|
6
|
+
export * from "./caps.js";
|
|
7
|
+
export * from "./client.js";
|
|
8
|
+
export * from "./query-runner.js";
|
|
9
|
+
export * from "./agent-client.js";
|
|
10
|
+
export * from "./selection.js";
|
|
11
|
+
export * from "./result-pages.js";
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,eAAe,CAAC;AAC9B,cAAc,WAAW,CAAC;AAC1B,cAAc,aAAa,CAAC;AAC5B,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Running ECSql on the host's iModel. Anything with `createQueryReader` will do: an
|
|
3
|
+
* `IModelConnection` in a browser (the queries go to the backend by RPC), or an `IModelDb` in
|
|
4
|
+
* Node. They are typed here by what is used, so this package depends on neither.
|
|
5
|
+
*/
|
|
6
|
+
import type { QueryPageOptions, RunRawQuery } from "./caps.js";
|
|
7
|
+
/** The part of iTwin.js's `ECSqlReader` that the runner uses. */
|
|
8
|
+
export interface QueryReader {
|
|
9
|
+
getMetaData(): Promise<{
|
|
10
|
+
name: string;
|
|
11
|
+
}[]>;
|
|
12
|
+
step(): Promise<boolean>;
|
|
13
|
+
readonly current: {
|
|
14
|
+
toArray(): unknown[];
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/** An `IModelConnection` or an `IModelDb`. */
|
|
18
|
+
export interface QuerySource {
|
|
19
|
+
createQueryReader(ecsql: string, params?: undefined, options?: {
|
|
20
|
+
abbreviateBlobs?: boolean;
|
|
21
|
+
} & QueryPageOptions): QueryReader;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* The query function the client runs the agent's queries with.
|
|
25
|
+
*
|
|
26
|
+
* - **Blobs are abbreviated.** One `GeometryStream` serialises to thousands of values, so one
|
|
27
|
+
* row could cost more than the whole budget, and the row cap cannot catch it.
|
|
28
|
+
* - **A bad query throws before the first row**, when the reader reads its metadata, so the
|
|
29
|
+
* driver sends the error for the model to correct.
|
|
30
|
+
* - **Short rows are padded with nulls.** `toArray()` drops trailing nulls, and a model reading
|
|
31
|
+
* `["Synchronization"]` under three columns guesses at the missing values.
|
|
32
|
+
* - **A page is the reader's own** (`QueryPageOptions`): its `limit` and `restartToken` options.
|
|
33
|
+
*/
|
|
34
|
+
export declare function queryRunner(source: QuerySource): RunRawQuery;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Running ECSql on the host's iModel. Anything with `createQueryReader` will do: an
|
|
3
|
+
* `IModelConnection` in a browser (the queries go to the backend by RPC), or an `IModelDb` in
|
|
4
|
+
* Node. They are typed here by what is used, so this package depends on neither.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The query function the client runs the agent's queries with.
|
|
8
|
+
*
|
|
9
|
+
* - **Blobs are abbreviated.** One `GeometryStream` serialises to thousands of values, so one
|
|
10
|
+
* row could cost more than the whole budget, and the row cap cannot catch it.
|
|
11
|
+
* - **A bad query throws before the first row**, when the reader reads its metadata, so the
|
|
12
|
+
* driver sends the error for the model to correct.
|
|
13
|
+
* - **Short rows are padded with nulls.** `toArray()` drops trailing nulls, and a model reading
|
|
14
|
+
* `["Synchronization"]` under three columns guesses at the missing values.
|
|
15
|
+
* - **A page is the reader's own** (`QueryPageOptions`): its `limit` and `restartToken` options.
|
|
16
|
+
*/
|
|
17
|
+
export function queryRunner(source) {
|
|
18
|
+
return async (ecsql, page) => {
|
|
19
|
+
const reader = source.createQueryReader(ecsql, undefined, {
|
|
20
|
+
abbreviateBlobs: true,
|
|
21
|
+
...(page?.limit ? { limit: page.limit } : {}),
|
|
22
|
+
...(page?.restartToken ? { restartToken: page.restartToken } : {}),
|
|
23
|
+
});
|
|
24
|
+
const columns = (await reader.getMetaData()).map((column) => column.name);
|
|
25
|
+
return {
|
|
26
|
+
columns,
|
|
27
|
+
rows: (async function* () {
|
|
28
|
+
while (await reader.step()) {
|
|
29
|
+
const row = reader.current.toArray();
|
|
30
|
+
while (row.length < columns.length)
|
|
31
|
+
row.push(null);
|
|
32
|
+
yield row;
|
|
33
|
+
}
|
|
34
|
+
})(),
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=query-runner.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-runner.js","sourceRoot":"","sources":["../../src/query-runner.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAgBH;;;;;;;;;;GAUG;AACH,MAAM,UAAU,WAAW,CAAC,MAAmB;IAC7C,OAAO,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;QAC3B,MAAM,MAAM,GAAG,MAAM,CAAC,iBAAiB,CAAC,KAAK,EAAE,SAAS,EAAE;YACxD,eAAe,EAAE,IAAI;YACrB,GAAG,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;YAC7C,GAAG,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,EAAE,YAAY,EAAE,IAAI,CAAC,YAAY,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;SACnE,CAAC,CAAC;QACH,MAAM,OAAO,GAAG,CAAC,MAAM,MAAM,CAAC,WAAW,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAC1E,OAAO;YACL,OAAO;YACP,IAAI,EAAE,CAAC,KAAK,SAAS,CAAC;gBACpB,OAAO,MAAM,MAAM,CAAC,IAAI,EAAE,EAAE,CAAC;oBAC3B,MAAM,GAAG,GAAG,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,CAAC;oBACrC,OAAO,GAAG,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM;wBAChC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;oBACjB,MAAM,GAAG,CAAC;gBACZ,CAAC;YACH,CAAC,CAAC,EAAE;SACL,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An answer's results with no row limit, read a page at a time for a virtualized table.
|
|
3
|
+
*
|
|
4
|
+
* Each page is its own read through the iTwin.js query reader, with the reader's `limit` option
|
|
5
|
+
* (`QueryPageOptions`), which is how the reader pages internally too: every page it reads is a
|
|
6
|
+
* request with an offset and a count. Nothing here rewrites the query. The total is read once,
|
|
7
|
+
* with `SELECT COUNT(*)` over the query, as the iTwin.js tests count a paged result; until it is
|
|
8
|
+
* known, or when the query cannot be counted that way, the rows read so far are the lower bound.
|
|
9
|
+
*
|
|
10
|
+
* Pages are read one at a time, for the rows the table last asked for: a fast scroll does not queue
|
|
11
|
+
* up every page it passed. Read pages are kept, so scrolling back costs nothing.
|
|
12
|
+
*/
|
|
13
|
+
import type { RunRawQuery } from "./caps.js";
|
|
14
|
+
/** How many rows one page reads. */
|
|
15
|
+
export declare const RESULT_PAGE_ROWS = 1000;
|
|
16
|
+
/** Rows already read, which the results start from: the answer's rows as the client first read them. */
|
|
17
|
+
export interface FirstRows {
|
|
18
|
+
columns: string[];
|
|
19
|
+
rows: unknown[][];
|
|
20
|
+
/** The first rows are all the rows. */
|
|
21
|
+
complete: boolean;
|
|
22
|
+
}
|
|
23
|
+
export declare class ResultPages {
|
|
24
|
+
private readonly runQuery;
|
|
25
|
+
readonly ecsql: string;
|
|
26
|
+
readonly pageRows: number;
|
|
27
|
+
private readonly pages;
|
|
28
|
+
private readonly listeners;
|
|
29
|
+
private _columns;
|
|
30
|
+
private _count;
|
|
31
|
+
private _error;
|
|
32
|
+
/** The highest row index known to exist, plus one: the lower bound while the count is unknown. */
|
|
33
|
+
private _known;
|
|
34
|
+
private wanted;
|
|
35
|
+
private loading;
|
|
36
|
+
private counting;
|
|
37
|
+
constructor(runQuery: RunRawQuery, ecsql: string, first?: FirstRows, pageRows?: number);
|
|
38
|
+
/** What a run carrying this says of it when written out as JSON: the query and the count, not the rows. */
|
|
39
|
+
toJSON(): {
|
|
40
|
+
ecsql: string;
|
|
41
|
+
count?: number;
|
|
42
|
+
};
|
|
43
|
+
/** The column names, once the first page (or the first rows) has been read. */
|
|
44
|
+
get columns(): string[] | undefined;
|
|
45
|
+
/** The number of rows, when known. */
|
|
46
|
+
get count(): number | undefined;
|
|
47
|
+
/** How many rows there are at least: the count when known, else the rows found so far. */
|
|
48
|
+
get atLeast(): number;
|
|
49
|
+
/** The error the query gave, if a page failed. */
|
|
50
|
+
get error(): string | undefined;
|
|
51
|
+
/** A row, if its page has been read. */
|
|
52
|
+
row(index: number): unknown[] | undefined;
|
|
53
|
+
/** Called whenever a page, the count or an error arrives. Returns a function that stops it. */
|
|
54
|
+
subscribe(listener: () => void): () => void;
|
|
55
|
+
/**
|
|
56
|
+
* Ask for the rows from `start` up to `end`: their pages are read, one at a time, unless a later
|
|
57
|
+
* call asks for other rows first. The count is read alongside the first call.
|
|
58
|
+
*/
|
|
59
|
+
want(start: number, end: number): void;
|
|
60
|
+
/** Read every page up to `end` rows (all of them when undefined): for a host or a test. */
|
|
61
|
+
loadUpTo(end?: number): Promise<void>;
|
|
62
|
+
private notify;
|
|
63
|
+
private readCount;
|
|
64
|
+
/** The next page the wanted rows need that has not been read, or undefined. */
|
|
65
|
+
private nextWantedPage;
|
|
66
|
+
private loadWanted;
|
|
67
|
+
/** Read one page (or return it, read before); undefined when the query failed. */
|
|
68
|
+
private readPage;
|
|
69
|
+
}
|