@askdb/core 1.0.0-beta.42 → 1.0.0-beta.44
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/README.md +14 -8
- package/dist/ask.d.ts +101 -4
- package/dist/ask.d.ts.map +1 -1
- package/dist/ask.js +243 -25
- package/dist/ask.js.map +1 -1
- package/dist/enrichment/suggest.d.ts.map +1 -1
- package/dist/enrichment/suggest.js +3 -1
- package/dist/enrichment/suggest.js.map +1 -1
- package/dist/errors.d.ts +41 -5
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +9 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +8 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/retrieval/synthesize-ddl.d.ts +4 -0
- package/dist/retrieval/synthesize-ddl.d.ts.map +1 -1
- package/dist/retrieval/synthesize-ddl.js +5 -3
- package/dist/retrieval/synthesize-ddl.js.map +1 -1
- package/dist/schema/normalize.d.ts +10 -0
- package/dist/schema/normalize.d.ts.map +1 -1
- package/dist/schema/normalize.js.map +1 -1
- package/dist/schema/v2/format.d.ts +7 -0
- package/dist/schema/v2/format.d.ts.map +1 -1
- package/dist/schema/v2/format.js +20 -7
- package/dist/schema/v2/format.js.map +1 -1
- package/dist/schema/v2/index.d.ts +3 -1
- package/dist/schema/v2/index.d.ts.map +1 -1
- package/dist/schema/v2/index.js +1 -0
- package/dist/schema/v2/index.js.map +1 -1
- package/dist/schema/v2/loader.d.ts +32 -1
- package/dist/schema/v2/loader.d.ts.map +1 -1
- package/dist/schema/v2/loader.js +173 -49
- package/dist/schema/v2/loader.js.map +1 -1
- package/dist/schema/v2/mentions.d.ts +31 -0
- package/dist/schema/v2/mentions.d.ts.map +1 -0
- package/dist/schema/v2/mentions.js +47 -0
- package/dist/schema/v2/mentions.js.map +1 -0
- package/dist/schema/v2/normalized.d.ts +38 -0
- package/dist/schema/v2/normalized.d.ts.map +1 -1
- package/dist/schema/v2/parser.d.ts +9 -0
- package/dist/schema/v2/parser.d.ts.map +1 -1
- package/dist/schema/v2/parser.js +18 -2
- package/dist/schema/v2/parser.js.map +1 -1
- package/dist/schema/v2/tenant-policy-loader.d.ts.map +1 -1
- package/dist/schema/v2/tenant-policy-loader.js +7 -3
- package/dist/schema/v2/tenant-policy-loader.js.map +1 -1
- package/dist/schema/v2/tenant-policy.d.ts +28 -21
- package/dist/schema/v2/tenant-policy.d.ts.map +1 -1
- package/dist/schema/v2/tenant-policy.js +67 -11
- package/dist/schema/v2/tenant-policy.js.map +1 -1
- package/dist/sql/bind.d.ts +17 -15
- package/dist/sql/bind.d.ts.map +1 -1
- package/dist/sql/bind.js +38 -173
- package/dist/sql/bind.js.map +1 -1
- package/dist/sql/dialect-spec.d.ts +48 -3
- package/dist/sql/dialect-spec.d.ts.map +1 -1
- package/dist/sql/dialect-spec.js +89 -3
- package/dist/sql/dialect-spec.js.map +1 -1
- package/dist/sql/generate.d.ts +14 -0
- package/dist/sql/generate.d.ts.map +1 -1
- package/dist/sql/generate.js +31 -17
- package/dist/sql/generate.js.map +1 -1
- package/dist/sql/index.d.ts +2 -1
- package/dist/sql/index.d.ts.map +1 -1
- package/dist/sql/index.js +2 -1
- package/dist/sql/index.js.map +1 -1
- package/dist/sql/lexer.d.ts +114 -0
- package/dist/sql/lexer.d.ts.map +1 -0
- package/dist/sql/lexer.js +397 -0
- package/dist/sql/lexer.js.map +1 -0
- package/dist/sql/prompt-identifiers.d.ts +20 -0
- package/dist/sql/prompt-identifiers.d.ts.map +1 -0
- package/dist/sql/prompt-identifiers.js +64 -0
- package/dist/sql/prompt-identifiers.js.map +1 -0
- package/dist/sql/prompt.d.ts +7 -1
- package/dist/sql/prompt.d.ts.map +1 -1
- package/dist/sql/prompt.js +22 -2
- package/dist/sql/prompt.js.map +1 -1
- package/dist/sql/reserved-words.d.ts +15 -0
- package/dist/sql/reserved-words.d.ts.map +1 -0
- package/dist/sql/reserved-words.js +102 -0
- package/dist/sql/reserved-words.js.map +1 -0
- package/dist/sql/sensitive-guardrail.d.ts +18 -3
- package/dist/sql/sensitive-guardrail.d.ts.map +1 -1
- package/dist/sql/sensitive-guardrail.js +446 -157
- package/dist/sql/sensitive-guardrail.js.map +1 -1
- package/dist/sql/tenant-guardrail.d.ts +70 -8
- package/dist/sql/tenant-guardrail.d.ts.map +1 -1
- package/dist/sql/tenant-guardrail.js +799 -52
- package/dist/sql/tenant-guardrail.js.map +1 -1
- package/dist/sql/tenant-hierarchy.d.ts +19 -0
- package/dist/sql/tenant-hierarchy.d.ts.map +1 -0
- package/dist/sql/tenant-hierarchy.js +53 -0
- package/dist/sql/tenant-hierarchy.js.map +1 -0
- package/dist/sql/tenant-placeholders.d.ts +47 -19
- package/dist/sql/tenant-placeholders.d.ts.map +1 -1
- package/dist/sql/tenant-placeholders.js +229 -58
- package/dist/sql/tenant-placeholders.js.map +1 -1
- package/dist/sql/tenant-prompt.d.ts +4 -0
- package/dist/sql/tenant-prompt.d.ts.map +1 -1
- package/dist/sql/tenant-prompt.js +79 -11
- package/dist/sql/tenant-prompt.js.map +1 -1
- package/dist/sql/tenant-scope-validate.d.ts +3 -1
- package/dist/sql/tenant-scope-validate.d.ts.map +1 -1
- package/dist/sql/tenant-scope-validate.js +5 -2
- package/dist/sql/tenant-scope-validate.js.map +1 -1
- package/dist/sql/validate.d.ts +34 -4
- package/dist/sql/validate.d.ts.map +1 -1
- package/dist/sql/validate.js +138 -37
- package/dist/sql/validate.js.map +1 -1
- package/package.json +11 -7
- package/dist/exec/postgres.d.ts +0 -6
- package/dist/exec/postgres.d.ts.map +0 -1
- package/dist/exec/postgres.js +0 -30
- package/dist/exec/postgres.js.map +0 -1
- package/dist/modes/post-execute-log.d.ts +0 -9
- package/dist/modes/post-execute-log.d.ts.map +0 -1
- package/dist/modes/post-execute-log.js +0 -26
- package/dist/modes/post-execute-log.js.map +0 -1
package/README.md
CHANGED
|
@@ -2,28 +2,34 @@
|
|
|
2
2
|
|
|
3
3
|
Dialect-agnostic NL→SQL pipeline for AskDB. Provides `ask()` orchestration, schema/IR types, modes, logging, and retrieval input. Bring your own dialect adapter (e.g. `@askdb/postgres`) and your own model.
|
|
4
4
|
|
|
5
|
-
> **Status:** pre-1.0. `0.3.0` moved the Postgres dialect and `@askdb/core/postgres` to `@askdb/postgres`. Runtime AI provider construction lives in `@askdb/ai`; core remains BYO-model and does not read `process.env`. See [`docs/adrs/0002-integration-package-layout.md`](
|
|
5
|
+
> **Status:** pre-release beta (`1.0.0-beta.x`); public APIs may still change before 1.0. `0.3.0` moved the Postgres dialect and `@askdb/core/postgres` to `@askdb/postgres`. Runtime AI provider construction lives in `@askdb/ai`; core remains BYO-model and does not read `process.env`. See [`docs/adrs/0002-integration-package-layout.md`](https://github.com/Ygilany/AskDB/blob/main/docs/adrs/0002-integration-package-layout.md), [`docs/adrs/0005-askdb-config-and-env-bootstrap.md`](https://github.com/Ygilany/AskDB/blob/main/docs/adrs/0005-askdb-config-and-env-bootstrap.md), and [`docs/adrs/0006-ai-provider-integration-strategy.md`](https://github.com/Ygilany/AskDB/blob/main/docs/adrs/0006-ai-provider-integration-strategy.md).
|
|
6
|
+
|
|
7
|
+
> **Security model:** `ask()` returns SQL and never executes it. Its SQL checks (single `SELECT`/`WITH` statement (with a SQL Server caveat), no comments, no write/DDL keywords, no known side-effecting functions, and the tenant and sensitive-column checks) are heuristic defense in depth that catch common model mistakes. The read-only and sensitive-column checks use a dialect-aware lexer; the tenant check uses a simpler scanner of its own ([#342](https://github.com/Ygilany/AskDB/issues/342)). They are not a SQL parser and not a security boundary. Run generated SQL under a read-only, least-privilege database role and enforce tenant isolation in the database. See [Safety boundaries](https://askdb.tools/concepts/safety-boundaries/).
|
|
6
8
|
|
|
7
9
|
## Install
|
|
8
10
|
|
|
9
11
|
```bash
|
|
10
|
-
|
|
12
|
+
# `ai` (Vercel AI SDK) is a required peer dependency — your app owns its version
|
|
13
|
+
pnpm add @askdb/core ai
|
|
11
14
|
# Plus a dialect adapter for the engine you target:
|
|
12
15
|
pnpm add @askdb/postgres
|
|
13
|
-
# Plus a model
|
|
16
|
+
# Plus a model — either construct one directly with an AI SDK provider:
|
|
14
17
|
pnpm add @ai-sdk/openai
|
|
15
|
-
# Optional AskDB config/env model factory:
|
|
18
|
+
# Optional AskDB config/env model factory (uses the @ai-sdk/* package above):
|
|
16
19
|
pnpm add @askdb/ai
|
|
17
|
-
pnpm add @askdb/ai-openai
|
|
18
20
|
```
|
|
19
21
|
|
|
22
|
+
`ai` is a **peer dependency** (`^6.0.0 || ^7.0.51`: AI SDK 6, or 7.0.51 and later), not a bundled dependency: `ask()` receives a `LanguageModel` your app constructs, so core must use the same `ai` instance your app does. Hosts on AI SDK 6 (e.g. `@ai-sdk/openai@3`) and AI SDK 7 (`@ai-sdk/openai@4`) are both supported. The config-driven path (`@askdb/ai` and `@askdb/client`) currently requires AI SDK 7.
|
|
23
|
+
|
|
24
|
+
`ai` is a required peer, not an optional one, and core loads it at import time. So any package that depends on `@askdb/core` needs `ai` installed, even for introspection only. That covers `@askdb/client`, `@askdb/connectors`, `@askdb/introspect`, the engine packages, `@askdb/prisma`, `@askdb/enrich` and `@askdb/rag`. npm 7+ and pnpm install the missing peer for you. Yarn doesn't, so add `ai` yourself. ADR 0006 records why (2026-09 amendment).
|
|
25
|
+
|
|
20
26
|
`@askdb/core` itself does not depend on `pg`. The optional `pg` peer lives on `@askdb/postgres` for live Postgres introspection.
|
|
21
27
|
|
|
22
|
-
Runtime AI configuration helpers live in `@askdb/ai
|
|
28
|
+
Runtime AI configuration helpers live in `@askdb/ai`, which has built-in providers for OpenAI, Azure/Foundry, Google, Anthropic, and the Vercel AI Gateway (install the matching `@ai-sdk/*` package). If you use [`@askdb/config`](https://github.com/Ygilany/AskDB/blob/main/packages/config/README.md), call `bootstrapAskDbEnv()`, create an AI registry, then pass **`getAskDbRuntimeConfig().ai.aiEnv`** to `registry.createLanguageModelFromEnv(...)`.
|
|
23
29
|
|
|
24
30
|
## Schema format
|
|
25
31
|
|
|
26
|
-
`@askdb/core` uses **Schema v2** — a split artifact designed for business-context enrichment and RAG chunking. See [`docs/contracts/schema-v2.md`](
|
|
32
|
+
`@askdb/core` uses **Schema v2** — a split artifact designed for business-context enrichment and RAG chunking. See [`docs/contracts/schema-v2.md`](https://github.com/Ygilany/AskDB/blob/main/docs/contracts/schema-v2.md) for the full contract.
|
|
27
33
|
|
|
28
34
|
### Directory layout
|
|
29
35
|
|
|
@@ -100,7 +106,7 @@ Key rules:
|
|
|
100
106
|
|
|
101
107
|
## What you get
|
|
102
108
|
|
|
103
|
-
- `ask({ question, schema, model, dialect })` — generate
|
|
109
|
+
- `ask({ question, schema, model, dialect })` — generate checked SQL (plus optional `unboundSql` / `params` / `parameters` / `preparedQuery`).
|
|
104
110
|
- `bindPreparedQuery(prepared, values)` — pure local rebind of a `PreparedQuery` (no model call).
|
|
105
111
|
- `AskDbLanguageModel` — AskDB's public name for the AI SDK language model contract.
|
|
106
112
|
- `AskDialect` — the dialect adapter contract. `@askdb/postgres` exports a ready-made one.
|
package/dist/ask.d.ts
CHANGED
|
@@ -16,6 +16,8 @@ export type AskDialectGenerateOptions = {
|
|
|
16
16
|
omitSensitiveIdentifiersFromNlToSqlPrompt?: boolean;
|
|
17
17
|
generateText?: typeof defaultGenerateText;
|
|
18
18
|
providerOptions?: Record<string, unknown>;
|
|
19
|
+
/** The caller's {@link AskPipelineOptions.abortSignal}. A custom dialect passes it to its model call. */
|
|
20
|
+
abortSignal?: AbortSignal;
|
|
19
21
|
prebuiltDdl?: string;
|
|
20
22
|
tenantPolicy?: import("./schema/v2/tenant-policy.js").NormalizedTenantPolicy;
|
|
21
23
|
tenantScope?: TenantScope;
|
|
@@ -31,10 +33,16 @@ export type AskUsage = {
|
|
|
31
33
|
completionTokens: number | null;
|
|
32
34
|
totalTokens: number | null;
|
|
33
35
|
};
|
|
34
|
-
/** Output of a dialect's generator:
|
|
36
|
+
/** Output of a dialect's generator: the generated SQL plus optional dialect-specific explain metadata. */
|
|
35
37
|
export type AskDialectGenerateResult = {
|
|
36
38
|
sql: string;
|
|
37
39
|
explain?: unknown;
|
|
40
|
+
/**
|
|
41
|
+
* Optional tenant guardrail result from a custom generator. When the schema has a
|
|
42
|
+
* tenant policy, `ask()` merges its warnings into its own check of `sql` and
|
|
43
|
+
* `unboundNamedSql` (before tenant rendering); it can add failures but never replace
|
|
44
|
+
* or relax that check.
|
|
45
|
+
*/
|
|
38
46
|
tenantGuardrail?: import("./sql/tenant-guardrail.js").TenantGuardrailResult;
|
|
39
47
|
usage?: AskUsage;
|
|
40
48
|
unboundNamedSql?: string;
|
|
@@ -48,6 +56,19 @@ export type AskDialectGenerateResult = {
|
|
|
48
56
|
* a {@link DialectSpec} to `ask({ dialect })` and let the centralized pipeline
|
|
49
57
|
* handle prompt assembly + validation. Implement `AskDialect` only when those
|
|
50
58
|
* defaults won't fit.
|
|
59
|
+
*
|
|
60
|
+
* Safety responsibilities:
|
|
61
|
+
* - **Read-only validation is yours.** `ask()` does not run the SELECT-only
|
|
62
|
+
* validator on SQL returned by a custom dialect (custom dialects may target
|
|
63
|
+
* non-SELECT statements on purpose). If your generator should only emit
|
|
64
|
+
* read-only SQL, call the exported `validateSelectSql(spec, sql)` yourself
|
|
65
|
+
* before returning.
|
|
66
|
+
* - **Tenant enforcement is not yours to skip.** When the schema has a tenant
|
|
67
|
+
* policy, `ask()` runs the tenant guardrail on the returned `sql` (and
|
|
68
|
+
* `unboundNamedSql`), with its `:tenant_<root>_ids` placeholders still in place,
|
|
69
|
+
* then substitutes them, regardless of dialect; `strict` policies throw
|
|
70
|
+
* `TenantGuardrailError`. A `tenantGuardrail` your
|
|
71
|
+
* generator returns is merged into that result, never used in place of it.
|
|
51
72
|
*/
|
|
52
73
|
export type AskDialect = {
|
|
53
74
|
generate(question: string, schema: AnyNormalizedSchema, model: AskDbLanguageModel, options?: AskDialectGenerateOptions): Promise<AskDialectGenerateResult>;
|
|
@@ -71,6 +92,28 @@ export type AskGenerateDeps = {
|
|
|
71
92
|
*/
|
|
72
93
|
providerOptions?: Record<string, unknown>;
|
|
73
94
|
};
|
|
95
|
+
/**
|
|
96
|
+
* A subtree's IDs grouped by tenant root: each key is a root table id from the
|
|
97
|
+
* tenant policy (`"table:public.clients"`), and its value holds IDs of that root's
|
|
98
|
+
* own `tenantIdColumn`, never IDs of another root.
|
|
99
|
+
*
|
|
100
|
+
* Root tables have separate ID spaces, so client `5` and agency `5` are different
|
|
101
|
+
* tenants. Keying the IDs by root lets `ask()` bind each root's IDs to that root's
|
|
102
|
+
* own `:tenant_<label>_ids` placeholder.
|
|
103
|
+
*/
|
|
104
|
+
export type TenantIdsByRoot = Readonly<Record<string, readonly string[]>>;
|
|
105
|
+
/**
|
|
106
|
+
* Host callback that expands a `subtree` tenant scope. Given the tenant root id and
|
|
107
|
+
* the seed IDs, return the subtree's IDs grouped by root ({@link TenantIdsByRoot}):
|
|
108
|
+
*
|
|
109
|
+
* - under `tenantRoot`: the seeds and any same-table descendants (e.g. child
|
|
110
|
+
* agencies through `agencies.parent_agency_id`);
|
|
111
|
+
* - under each descendant root the policy declares (`roots[].parent` or
|
|
112
|
+
* `hierarchy[]`): that root's IDs in the subtree.
|
|
113
|
+
*
|
|
114
|
+
* Pass it to `ask()` as {@link AskPipelineOptions.resolveTenantDescendants}.
|
|
115
|
+
*/
|
|
116
|
+
export type ResolveTenantDescendants = (tenantRoot: string, seedIds: readonly string[]) => Promise<TenantIdsByRoot> | TenantIdsByRoot;
|
|
74
117
|
export type AskPipelineOptions = {
|
|
75
118
|
question: string;
|
|
76
119
|
schema: AnyNormalizedSchema;
|
|
@@ -88,6 +131,13 @@ export type AskPipelineOptions = {
|
|
|
88
131
|
*/
|
|
89
132
|
omitSensitiveIdentifiersFromNlToSqlPrompt?: boolean;
|
|
90
133
|
deps?: AskGenerateDeps;
|
|
134
|
+
/**
|
|
135
|
+
* Cancels the NL→SQL model call (`generateText({ abortSignal })`), e.g.
|
|
136
|
+
* `AbortSignal.timeout(60_000)` for a per-request timeout. With a built-in dialect an
|
|
137
|
+
* aborted call rejects with `SqlGenerationError` (the abort reason is its `cause`). A
|
|
138
|
+
* custom {@link AskDialect} receives it in its `generate()` options and maps its own errors.
|
|
139
|
+
*/
|
|
140
|
+
abortSignal?: AbortSignal;
|
|
91
141
|
/** Optional structured logger (host-provided — e.g. `createAskDbLogger` wraps Pino). */
|
|
92
142
|
logger?: AskDbLogger;
|
|
93
143
|
/**
|
|
@@ -130,9 +180,30 @@ export type AskPipelineOptions = {
|
|
|
130
180
|
tenantScope?: TenantScope;
|
|
131
181
|
/**
|
|
132
182
|
* SQL output mode for tenant placeholders. Default `"sql-only"` inlines
|
|
133
|
-
* literal values
|
|
183
|
+
* escaped literal values. `"sql-params"` replaces them with the dialect's
|
|
184
|
+
* driver markers (`$N` for Postgres/CockroachDB and custom `AskDialect`s, `?`
|
|
185
|
+
* for MySQL/MariaDB/SQLite, `@pN` for SQL Server): run `sql` with
|
|
186
|
+
* `tenantParams`, or `unboundSql` with `params`.
|
|
134
187
|
*/
|
|
135
188
|
tenantSqlMode?: TenantSqlOutputMode;
|
|
189
|
+
/**
|
|
190
|
+
* Expands a `subtree` tenant scope into IDs per tenant root.
|
|
191
|
+
*
|
|
192
|
+
* Called with the tenant root id and the seed IDs from
|
|
193
|
+
* `tenantScope.access.rootIds`; returns the subtree's IDs keyed by root table id
|
|
194
|
+
* ({@link TenantIdsByRoot}). `ask()` unions the seeds into the `tenantRoot` entry
|
|
195
|
+
* and turns the result into a `multi_root` scope (an `ids` scope when only
|
|
196
|
+
* `tenantRoot` has IDs), so each root's IDs bind only to that root's placeholder.
|
|
197
|
+
* The host is the right place for this: it already knows its own hierarchy, can
|
|
198
|
+
* cache the closure, and can apply its own authorization rules.
|
|
199
|
+
*
|
|
200
|
+
* Required when `tenantScope.access.kind === "subtree"` — AskDB never opens a
|
|
201
|
+
* database connection of its own. Without it, or when it returns a flat array,
|
|
202
|
+
* a key that isn't a root in the subtree, a value that isn't an array of
|
|
203
|
+
* non-empty strings, or no IDs at all, `ask()` throws `TenantScopeError`
|
|
204
|
+
* (`SUBTREE_NOT_RESOLVABLE`) before calling the model.
|
|
205
|
+
*/
|
|
206
|
+
resolveTenantDescendants?: ResolveTenantDescendants;
|
|
136
207
|
/**
|
|
137
208
|
* Ask the model to also return the SQL in unbound form plus a JSON manifest
|
|
138
209
|
* of the values it parameterized, populating `unboundSql`, `params`,
|
|
@@ -152,10 +223,23 @@ export type AskPipelineOptions = {
|
|
|
152
223
|
sensitiveGuardrailMode?: SensitiveGuardrailMode | "off";
|
|
153
224
|
};
|
|
154
225
|
export type AskPipelineResult = {
|
|
226
|
+
/**
|
|
227
|
+
* The model's bound SQL: business values inlined as literals. Tenant IDs are
|
|
228
|
+
* inlined literals (`tenantSqlMode: "sql-only"`) or driver markers numbered from
|
|
229
|
+
* the first slot (`"sql-params"`) — then execute it with `tenantParams`.
|
|
230
|
+
*/
|
|
155
231
|
sql: string;
|
|
156
|
-
/**
|
|
232
|
+
/**
|
|
233
|
+
* Driver markers for every value (business and, in `"sql-params"` mode, tenant).
|
|
234
|
+
* Execute with `params` — never with `tenantParams`.
|
|
235
|
+
*/
|
|
157
236
|
unboundSql?: string;
|
|
158
|
-
/**
|
|
237
|
+
/**
|
|
238
|
+
* Values for `unboundSql`, in driver-marker order (array slots on
|
|
239
|
+
* Postgres/CockroachDB listBinding). In `"sql-params"` mode this already
|
|
240
|
+
* includes the tenant values — for `?` dialects interleaved in source order,
|
|
241
|
+
* otherwise after the business values. Do not concatenate `tenantParams`.
|
|
242
|
+
*/
|
|
159
243
|
params?: QueryParamSlot[];
|
|
160
244
|
/** Named bindings for form UIs (includes runtime values). */
|
|
161
245
|
parameters?: QueryParameterBinding[];
|
|
@@ -167,7 +251,20 @@ export type AskPipelineResult = {
|
|
|
167
251
|
* at least one `sensitive` table/column and `sensitiveGuardrailMode` is not `"off"`.
|
|
168
252
|
*/
|
|
169
253
|
sensitiveGuardrail?: SensitiveGuardrailResult;
|
|
254
|
+
/**
|
|
255
|
+
* Tenant guardrail result for the model's SQL, checked before tenant rendering: the
|
|
256
|
+
* bound `sql` and its `sql-unbound` block, with the `:tenant_<root>_ids` placeholders
|
|
257
|
+
* still in place. `sql` and `unboundSql` differ from those only by the substituted
|
|
258
|
+
* tenant IDs or markers. Present whenever the schema has a tenant policy, for every
|
|
259
|
+
* dialect form. In `strict` mode a failure
|
|
260
|
+
* throws `TenantGuardrailError` instead, so a returned result is always `passed`
|
|
261
|
+
* under `strict`.
|
|
262
|
+
*/
|
|
170
263
|
tenantGuardrail?: import("./sql/tenant-guardrail.js").TenantGuardrailResult;
|
|
264
|
+
/**
|
|
265
|
+
* `"sql-params"` mode only: the tenant IDs for the markers in `sql`, in marker
|
|
266
|
+
* order. `sql` + `tenantParams` is an executable pair on its own.
|
|
267
|
+
*/
|
|
171
268
|
tenantParams?: unknown[];
|
|
172
269
|
tenantBindings?: TenantBinding[];
|
|
173
270
|
/** Token usage for the LLM generation call. Absent when the provider does not report usage. */
|
package/dist/ask.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ask.d.ts","sourceRoot":"","sources":["../src/ask.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,IAAI,mBAAmB,EAAE,MAAM,IAAI,CAAC;AAC9D,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAE7D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAsB,KAAK,WAAW,EAAE,MAAM,kBAAkB,CAAC;AACxE,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;
|
|
1
|
+
{"version":3,"file":"ask.d.ts","sourceRoot":"","sources":["../src/ask.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAA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AAC;IACpB;;;;;OAKG;IACH,MAAM,CAAC,EAAE,cAAc,EAAE,CAAC;IAC1B,6DAA6D;IAC7D,UAAU,CAAC,EAAE,qBAAqB,EAAE,CAAC;IACrC,uDAAuD;IACvD,aAAa,CAAC,EAAE,aAAa,CAAC;IAC9B,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB;;;OAGG;IACH,kBAAkB,CAAC,EAAE,wBAAwB,CAAC;IAC9C;;;;;;;;OAQG;IACH,eAAe,CAAC,EAAE,OAAO,2BAA2B,EAAE,qBAAqB,CAAC;IAC5E;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,EAAE,CAAC;IACzB,cAAc,CAAC,EAAE,aAAa,EAAE,CAAC;IACjC,+FAA+F;IAC/F,KAAK,CAAC,EAAE,QAAQ,CAAC;CAClB,CAAC;AAEF,wBAAsB,GAAG,CAAC,OAAO,EAAE,kBAAkB,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAkOjF"}
|
package/dist/ask.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { AskDbLogEvent } from "./logging/log-events.js";
|
|
2
2
|
import { DEFAULT_ASKDB_MODE } from "./modes/types.js";
|
|
3
3
|
import { synthesizeRetrievedDdl } from "./retrieval/synthesize-ddl.js";
|
|
4
|
+
import { unqualifiedNamespaceFor } from "./sql/prompt.js";
|
|
5
|
+
import { promptIdentifierQuoter } from "./sql/prompt-identifiers.js";
|
|
4
6
|
import { getDialectSpec, isBuiltInDialectId, } from "./sql/dialect-spec.js";
|
|
5
|
-
import {
|
|
7
|
+
import { generateSelectSqlWithoutTenantGuardrail } from "./sql/generate.js";
|
|
8
|
+
import { enforceTenantGuardrails } from "./sql/tenant-guardrail.js";
|
|
6
9
|
import { resolveTenantSql, } from "./sql/tenant-placeholders.js";
|
|
10
|
+
import { subtreeRootIds } from "./sql/tenant-hierarchy.js";
|
|
7
11
|
import { validateTenantScope } from "./sql/tenant-scope-validate.js";
|
|
8
12
|
import { formatSensitiveReference, schemaHasSensitiveIdentifiers, validateSensitiveReferences, } from "./sql/sensitive-guardrail.js";
|
|
9
|
-
import { SensitiveReferenceError } from "./errors.js";
|
|
10
|
-
import { bindPreparedQuery, sqlStructurallyEqual, } from "./sql/bind.js";
|
|
13
|
+
import { SensitiveReferenceError, TenantScopeError, UnknownDialectError, } from "./errors.js";
|
|
14
|
+
import { bindPreparedQuery, markerStyleForDialect, scanPlaceholders, sqlStructurallyEqual, } from "./sql/bind.js";
|
|
11
15
|
export async function ask(options) {
|
|
12
16
|
const logger = options.logger;
|
|
13
17
|
const mode = options.mode ?? DEFAULT_ASKDB_MODE;
|
|
@@ -22,15 +26,23 @@ export async function ask(options) {
|
|
|
22
26
|
enforcement: tenantPolicy.enforcement,
|
|
23
27
|
}, "tenant scope validated");
|
|
24
28
|
}
|
|
29
|
+
// A `subtree` scope is expanded to IDs per root here, before generation, so the
|
|
30
|
+
// prompt, guardrail, and placeholder substitution all see the same per-root
|
|
31
|
+
// access — and a missing or malformed resolver fails before any model call is spent.
|
|
32
|
+
const tenantScope = tenantPolicy && options.tenantScope
|
|
33
|
+
? await expandSubtreeScope(tenantPolicy, options.tenantScope, options.resolveTenantDescendants)
|
|
34
|
+
: options.tenantScope;
|
|
25
35
|
const explainRequested = options.explain ?? false;
|
|
26
36
|
const omitSensitive = options.omitSensitiveIdentifiersFromNlToSqlPrompt ?? false;
|
|
27
37
|
const parameterize = options.parameterize !== false; // default true
|
|
38
|
+
const dialectSpec = resolveDialectSpec(options.dialect);
|
|
28
39
|
const prebuiltDdl = await maybeRetrieveDdl({
|
|
29
40
|
options,
|
|
30
41
|
logger,
|
|
31
42
|
omitSensitive,
|
|
43
|
+
unqualifiedNamespace: unqualifiedNamespaceFor(options.schema, dialectSpec?.unqualifiedNamespace),
|
|
44
|
+
quoteIdentifier: dialectSpec ? promptIdentifierQuoter(dialectSpec) : undefined,
|
|
32
45
|
});
|
|
33
|
-
const dialectSpec = resolveDialectSpec(options.dialect);
|
|
34
46
|
const dialect = resolveDialect(options.dialect);
|
|
35
47
|
const generated = await dialect.generate(options.question, options.schema, options.model, {
|
|
36
48
|
logger,
|
|
@@ -38,9 +50,10 @@ export async function ask(options) {
|
|
|
38
50
|
omitSensitiveIdentifiersFromNlToSqlPrompt: omitSensitive || undefined,
|
|
39
51
|
generateText: options.deps?.generateText,
|
|
40
52
|
providerOptions: options.deps?.providerOptions,
|
|
53
|
+
abortSignal: options.abortSignal,
|
|
41
54
|
prebuiltDdl,
|
|
42
55
|
tenantPolicy,
|
|
43
|
-
tenantScope
|
|
56
|
+
tenantScope,
|
|
44
57
|
// Custom AskDialect implementations ignore this; built-in path uses it.
|
|
45
58
|
parameterize: dialectSpec ? parameterize : undefined,
|
|
46
59
|
});
|
|
@@ -49,8 +62,11 @@ export async function ask(options) {
|
|
|
49
62
|
const result = { sql: generated.sql };
|
|
50
63
|
if (generated.explain !== undefined)
|
|
51
64
|
result.explain = generated.explain;
|
|
52
|
-
|
|
65
|
+
// With a tenant policy, `tenantGuardrail` is computed below, before tenant rendering;
|
|
66
|
+
// without one, pass through whatever a custom dialect reported.
|
|
67
|
+
if (!tenantPolicy && generated.tenantGuardrail !== undefined) {
|
|
53
68
|
result.tenantGuardrail = generated.tenantGuardrail;
|
|
69
|
+
}
|
|
54
70
|
if (generated.usage !== undefined)
|
|
55
71
|
result.usage = generated.usage;
|
|
56
72
|
// Parameterize extras: build PreparedQuery, consistency-check via bindPreparedQuery,
|
|
@@ -127,10 +143,21 @@ export async function ask(options) {
|
|
|
127
143
|
}, "parameterize extras dropped");
|
|
128
144
|
}
|
|
129
145
|
}
|
|
130
|
-
if (tenantPolicy &&
|
|
146
|
+
if (tenantPolicy && tenantScope) {
|
|
147
|
+
// Tenant guardrail on the untrusted SQL, before rendering: the model's bound
|
|
148
|
+
// statement and its unbound block, with the `:tenant_<root>_ids` placeholders
|
|
149
|
+
// still in place. Rendering below only swaps each placeholder for literals or
|
|
150
|
+
// driver markers, so one check covers every tenantSqlMode, dialect and output
|
|
151
|
+
// form (#315). Runs for every dialect (built-in, DialectSpec, or custom
|
|
152
|
+
// AskDialect); the built-in generator skips its own check so this is the single
|
|
153
|
+
// report. `dialectSpec` is undefined for a custom AskDialect: the guardrail then
|
|
154
|
+
// requires the statement to pass under the standard-SQL, Postgres and MySQL readings.
|
|
155
|
+
result.tenantGuardrail = enforceTenantGuardrails([result.sql, generated.unboundNamedSql], tenantPolicy, tenantScope, logger, generated.tenantGuardrail, dialectSpec);
|
|
131
156
|
const tenantMode = options.tenantSqlMode ?? "sql-only";
|
|
132
|
-
|
|
133
|
-
|
|
157
|
+
// `sql` carries business values as inlined literals, so its only markers are
|
|
158
|
+
// tenant markers, numbered from the first slot: `sql` runs with `tenantParams`
|
|
159
|
+
// alone. `unboundSql` runs with the combined `params` (handled below).
|
|
160
|
+
const resolved = resolveTenantSql(result.sql, tenantPolicy, tenantScope, tenantMode, 1, dialectSpec);
|
|
134
161
|
result.sql = resolved.sql;
|
|
135
162
|
if (resolved.bindings.length > 0)
|
|
136
163
|
result.tenantBindings = resolved.bindings;
|
|
@@ -139,36 +166,138 @@ export async function ask(options) {
|
|
|
139
166
|
}
|
|
140
167
|
if (result.preparedQuery && result.unboundSql) {
|
|
141
168
|
if (tenantMode === "sql-only") {
|
|
142
|
-
const unboundWithTenant = resolveTenantSql(result.unboundSql, tenantPolicy,
|
|
169
|
+
const unboundWithTenant = resolveTenantSql(result.unboundSql, tenantPolicy, tenantScope, "sql-only", 1, dialectSpec);
|
|
143
170
|
result.unboundSql = unboundWithTenant.sql;
|
|
144
171
|
}
|
|
145
|
-
else {
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
172
|
+
else if (!bindTenantIntoUnboundSql(result, {
|
|
173
|
+
namedSql: result.preparedQuery.namedSql,
|
|
174
|
+
businessParamCount,
|
|
175
|
+
tenantPolicy,
|
|
176
|
+
tenantScope,
|
|
177
|
+
dialectSpec,
|
|
178
|
+
})) {
|
|
179
|
+
dropParameterizeExtras(result);
|
|
180
|
+
logger?.debug?.({
|
|
181
|
+
event: AskDbLogEvent.PipelineParameterized,
|
|
182
|
+
parameterCount: 0,
|
|
183
|
+
listParameterCount: 0,
|
|
184
|
+
reason: "tenant_param_alignment",
|
|
185
|
+
}, "parameterize extras dropped");
|
|
154
186
|
}
|
|
155
187
|
}
|
|
156
188
|
}
|
|
157
|
-
applySensitiveGuardrail(result, options, logger);
|
|
189
|
+
applySensitiveGuardrail(result, options, dialectSpec, logger);
|
|
158
190
|
return result;
|
|
159
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* Replace a `subtree` access with the per-root access it expands to: `multi_root`
|
|
194
|
+
* with one entry per root the subtree covers (the scope's root first, then its
|
|
195
|
+
* descendant roots breadth-first), including a level with no IDs (`ids: []`), or `ids`
|
|
196
|
+
* when the subtree is the scope's root alone. Other access kinds pass through untouched.
|
|
197
|
+
*
|
|
198
|
+
* Each root's IDs stay under that root, so they bind only to its own placeholder:
|
|
199
|
+
* root tables have separate ID spaces, and folding a client ID into the agency
|
|
200
|
+
* placeholder would match another agency (#338). This relies on every root deriving a distinct
|
|
201
|
+
* placeholder, which `validateTenantScope()` and the policy loader enforce. The seeds are unioned into the
|
|
202
|
+
* `tenantRoot` entry here, not trusted to the resolver, so an ancestor never loses
|
|
203
|
+
* its own rows when a host returns strict descendants only.
|
|
204
|
+
*
|
|
205
|
+
* Fails closed with `SUBTREE_NOT_RESOLVABLE`: no resolver; a result that is an
|
|
206
|
+
* array (the old flat shape) or not a plain object; a key that isn't a root in
|
|
207
|
+
* this subtree; a value that isn't an array of non-empty strings; or no IDs at all.
|
|
208
|
+
*/
|
|
209
|
+
async function expandSubtreeScope(policy, scope, resolve) {
|
|
210
|
+
const access = scope.access;
|
|
211
|
+
if (access.kind !== "subtree")
|
|
212
|
+
return scope;
|
|
213
|
+
const { tenantRoot, rootIds } = access;
|
|
214
|
+
const levels = subtreeRootIds(policy, tenantRoot);
|
|
215
|
+
const shape = `{ ${levels.map((root) => `"${root}": [...]`).join(", ")} }`;
|
|
216
|
+
const fail = (message) => new TenantScopeError(message, "SUBTREE_NOT_RESOLVABLE");
|
|
217
|
+
if (!resolve) {
|
|
218
|
+
throw fail(`tenantScope.access is a 'subtree' of '${tenantRoot}', but no ` +
|
|
219
|
+
"resolveTenantDescendants was passed to ask(). AskDB does not query your database " +
|
|
220
|
+
"to find descendants: pass resolveTenantDescendants to expand the seed IDs, or pass " +
|
|
221
|
+
"an 'ids' or 'multi_root' access with each root's IDs already expanded.");
|
|
222
|
+
}
|
|
223
|
+
const result = await resolve(tenantRoot, rootIds);
|
|
224
|
+
if (Array.isArray(result)) {
|
|
225
|
+
throw fail(`resolveTenantDescendants for '${tenantRoot}' returned an array. It must return IDs per ` +
|
|
226
|
+
`tenant root, keyed by root table id: ${shape}. Root tables have separate ID spaces, so ` +
|
|
227
|
+
"a flat list can't say which root each ID belongs to, and binding them all to " +
|
|
228
|
+
`'${tenantRoot}' would match other tenants. Put the seeds and any same-table ` +
|
|
229
|
+
`descendants under '${tenantRoot}', and each descendant root's IDs under that root.`);
|
|
230
|
+
}
|
|
231
|
+
if (!isPlainObject(result)) {
|
|
232
|
+
throw fail(`resolveTenantDescendants for '${tenantRoot}' must return an object mapping each ` +
|
|
233
|
+
`tenant root in the subtree to its IDs: ${shape}.`);
|
|
234
|
+
}
|
|
235
|
+
// Read the result exactly once: validate and build from this snapshot, so a getter
|
|
236
|
+
// can't return one value to validation and another to the scope, and a property
|
|
237
|
+
// validation can't see (non-enumerable) is never read at all.
|
|
238
|
+
const idsByRoot = new Map();
|
|
239
|
+
const knownRoots = new Set(policy.roots.map((root) => root.id));
|
|
240
|
+
let returned = 0;
|
|
241
|
+
for (const [root, value] of Object.entries(result)) {
|
|
242
|
+
const ids = Array.isArray(value) ? [...value] : value;
|
|
243
|
+
if (!knownRoots.has(root)) {
|
|
244
|
+
throw fail(`resolveTenantDescendants returned IDs under a key the subtree can't have: '${root}' is ` +
|
|
245
|
+
`not a tenant root in the policy. Allowed keys for a subtree of '${tenantRoot}': ` +
|
|
246
|
+
`${levels.join(", ")}.`);
|
|
247
|
+
}
|
|
248
|
+
if (!levels.includes(root)) {
|
|
249
|
+
throw fail(`resolveTenantDescendants returned IDs under a key the subtree can't have: '${root}' is ` +
|
|
250
|
+
`not in the subtree of '${tenantRoot}' (the policy's hierarchy doesn't reach it from ` +
|
|
251
|
+
`there). Allowed keys: ${levels.join(", ")}.`);
|
|
252
|
+
}
|
|
253
|
+
if (!isTenantIdArray(ids)) {
|
|
254
|
+
throw fail(`resolveTenantDescendants: the IDs for '${root}' must be an array of non-empty strings.`);
|
|
255
|
+
}
|
|
256
|
+
idsByRoot.set(root, ids);
|
|
257
|
+
returned += ids.length;
|
|
258
|
+
}
|
|
259
|
+
if (returned === 0) {
|
|
260
|
+
throw fail(`resolveTenantDescendants returned no IDs for '${tenantRoot}' (seeds: ${rootIds.join(", ")}). ` +
|
|
261
|
+
`Return at least the seeds under '${tenantRoot}'; refusing to build an empty tenant scope.`);
|
|
262
|
+
}
|
|
263
|
+
// Every level the subtree covers stays in the scope, even one with no IDs. The
|
|
264
|
+
// guardrail then checks a read of that root on its own placeholder, and binding that
|
|
265
|
+
// placeholder fails closed (UNRESOLVED_TENANT_PLACEHOLDER). Dropping the level would
|
|
266
|
+
// leave its rows readable through a parent's foreign key or an ancestor, which is
|
|
267
|
+
// less restricted than a level the resolver narrowed to some IDs.
|
|
268
|
+
const scopes = levels.map((root) => {
|
|
269
|
+
const own = idsByRoot.get(root) ?? [];
|
|
270
|
+
return { tenantRoot: root, ids: [...new Set(root === tenantRoot ? [...rootIds, ...own] : own)] };
|
|
271
|
+
});
|
|
272
|
+
const expanded = scopes.length === 1 ? { kind: "ids", ...scopes[0] } : { kind: "multi_root", scopes };
|
|
273
|
+
return { ...scope, access: expanded };
|
|
274
|
+
}
|
|
275
|
+
function isTenantIdArray(value) {
|
|
276
|
+
return Array.isArray(value) && value.every((id) => typeof id === "string" && id.length > 0);
|
|
277
|
+
}
|
|
278
|
+
function isPlainObject(value) {
|
|
279
|
+
if (typeof value !== "object" || value === null)
|
|
280
|
+
return false;
|
|
281
|
+
const proto = Object.getPrototypeOf(value);
|
|
282
|
+
return proto === Object.prototype || proto === null;
|
|
283
|
+
}
|
|
160
284
|
/**
|
|
161
285
|
* Run the sensitive-identifier guardrail over the SQL the host is about to receive.
|
|
162
286
|
* Runs after tenant resolution so it sees exactly the statement in `result.sql`.
|
|
287
|
+
* With a built-in id or `DialectSpec`, the SQL is lexed the way that engine reads it;
|
|
288
|
+
* for a custom `AskDialect` (no spec) references are unioned across every built-in reading.
|
|
163
289
|
*/
|
|
164
|
-
function applySensitiveGuardrail(result, options, logger) {
|
|
290
|
+
function applySensitiveGuardrail(result, options, dialectSpec, logger) {
|
|
165
291
|
const mode = options.sensitiveGuardrailMode ?? "warn";
|
|
166
292
|
if (mode === "off")
|
|
167
293
|
return;
|
|
168
294
|
if (!schemaHasSensitiveIdentifiers(options.schema))
|
|
169
295
|
return;
|
|
170
296
|
try {
|
|
171
|
-
const guardrail = validateSensitiveReferences(result.sql, options.schema, {
|
|
297
|
+
const guardrail = validateSensitiveReferences(result.sql, options.schema, {
|
|
298
|
+
mode,
|
|
299
|
+
dialect: dialectSpec,
|
|
300
|
+
});
|
|
172
301
|
result.sensitiveGuardrail = guardrail;
|
|
173
302
|
logSensitiveReferences(logger, guardrail.references);
|
|
174
303
|
}
|
|
@@ -189,6 +318,91 @@ function logSensitiveReferences(logger, references) {
|
|
|
189
318
|
sensitiveColumns,
|
|
190
319
|
}, "generated SQL references sensitive identifiers");
|
|
191
320
|
}
|
|
321
|
+
/**
|
|
322
|
+
* Substitute tenant placeholders in `result.unboundSql` with driver markers and
|
|
323
|
+
* fold their values into `result.params`, so `unboundSql` + `params` is a single
|
|
324
|
+
* executable pair (`sql-params` mode).
|
|
325
|
+
*
|
|
326
|
+
* - `$N` / `@pN` dialects: markers are explicitly numbered, so tenant markers
|
|
327
|
+
* continue after the business slots and tenant values are appended.
|
|
328
|
+
* - `?` dialects: markers are positional, so `params` must follow source order.
|
|
329
|
+
* Business and tenant values are interleaved by walking the named template in
|
|
330
|
+
* order, and `parameters[].indices` are remapped to the new positions.
|
|
331
|
+
*
|
|
332
|
+
* Returns false when the business binding and the tenant substitution disagree
|
|
333
|
+
* about the statement's shape; the caller then drops the extras rather than ship
|
|
334
|
+
* misaligned params.
|
|
335
|
+
*/
|
|
336
|
+
function bindTenantIntoUnboundSql(result, ctx) {
|
|
337
|
+
if (result.unboundSql === undefined)
|
|
338
|
+
return false;
|
|
339
|
+
const unbound = resolveTenantSql(result.unboundSql, ctx.tenantPolicy, ctx.tenantScope, "sql-params", ctx.businessParamCount + 1, ctx.dialectSpec);
|
|
340
|
+
if (unbound.mode !== "sql-params")
|
|
341
|
+
return false;
|
|
342
|
+
const business = result.params ?? [];
|
|
343
|
+
const tenantValues = unbound.params;
|
|
344
|
+
const dialectId = ctx.dialectSpec?.id;
|
|
345
|
+
const style = dialectId !== undefined && isBuiltInDialectId(dialectId)
|
|
346
|
+
? markerStyleForDialect(dialectId)
|
|
347
|
+
: "dollar";
|
|
348
|
+
if (style !== "question") {
|
|
349
|
+
result.unboundSql = unbound.sql;
|
|
350
|
+
result.params = [...business, ...tenantValues];
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
const idsByPlaceholder = new Map(unbound.bindings.map((b) => [b.placeholder, b.ids]));
|
|
354
|
+
const bindingByName = new Map((result.parameters ?? []).map((b) => [b.name, b]));
|
|
355
|
+
// Same lexer reading as bindPreparedQuery and the tenant substitution above.
|
|
356
|
+
const occurrences = scanPlaceholders(ctx.namedSql, ctx.dialectSpec);
|
|
357
|
+
const occurrenceCount = new Map();
|
|
358
|
+
for (const occ of occurrences) {
|
|
359
|
+
occurrenceCount.set(occ.name, (occurrenceCount.get(occ.name) ?? 0) + 1);
|
|
360
|
+
}
|
|
361
|
+
const combined = [];
|
|
362
|
+
const indexMap = new Map();
|
|
363
|
+
const seen = new Map();
|
|
364
|
+
for (const occ of occurrences) {
|
|
365
|
+
const tenantIds = idsByPlaceholder.get(occ.placeholder);
|
|
366
|
+
if (tenantIds) {
|
|
367
|
+
combined.push(...tenantIds);
|
|
368
|
+
continue;
|
|
369
|
+
}
|
|
370
|
+
// bindPreparedQuery pushes each occurrence's values contiguously, in source
|
|
371
|
+
// order, so occurrence k of a name owns the k-th equal slice of its indices.
|
|
372
|
+
const binding = bindingByName.get(occ.name);
|
|
373
|
+
const total = occurrenceCount.get(occ.name);
|
|
374
|
+
if (!binding || binding.indices.length % total !== 0)
|
|
375
|
+
return false;
|
|
376
|
+
const per = binding.indices.length / total;
|
|
377
|
+
const k = seen.get(occ.name) ?? 0;
|
|
378
|
+
seen.set(occ.name, k + 1);
|
|
379
|
+
for (const idx of binding.indices.slice(k * per, (k + 1) * per)) {
|
|
380
|
+
if (idx >= business.length || indexMap.has(idx))
|
|
381
|
+
return false;
|
|
382
|
+
indexMap.set(idx, combined.length);
|
|
383
|
+
combined.push(business[idx]);
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
if (indexMap.size !== business.length ||
|
|
387
|
+
combined.length !== business.length + tenantValues.length) {
|
|
388
|
+
return false;
|
|
389
|
+
}
|
|
390
|
+
result.unboundSql = unbound.sql;
|
|
391
|
+
result.params = combined;
|
|
392
|
+
if (result.parameters) {
|
|
393
|
+
result.parameters = result.parameters.map((b) => ({
|
|
394
|
+
...b,
|
|
395
|
+
indices: b.indices.map((i) => indexMap.get(i)),
|
|
396
|
+
}));
|
|
397
|
+
}
|
|
398
|
+
return true;
|
|
399
|
+
}
|
|
400
|
+
function dropParameterizeExtras(result) {
|
|
401
|
+
delete result.unboundSql;
|
|
402
|
+
delete result.params;
|
|
403
|
+
delete result.parameters;
|
|
404
|
+
delete result.preparedQuery;
|
|
405
|
+
}
|
|
192
406
|
const TENANT_MASK_RE = /:tenant_([a-z0-9_]+)_ids/g;
|
|
193
407
|
const TENANT_UNMASK_RE = /__askdb_tenant_([a-z0-9_]+)_ids__/g;
|
|
194
408
|
function maskTenantPlaceholders(sql) {
|
|
@@ -235,7 +449,7 @@ function resolveDialectSpec(input) {
|
|
|
235
449
|
function resolveDialect(input) {
|
|
236
450
|
if (typeof input === "string") {
|
|
237
451
|
if (!isBuiltInDialectId(input)) {
|
|
238
|
-
throw new
|
|
452
|
+
throw new UnknownDialectError(`Unknown dialect id '${input}'. Pass a built-in DialectId, a DialectSpec object, or a custom AskDialect.`, input);
|
|
239
453
|
}
|
|
240
454
|
return specToDialect(getDialectSpec(input));
|
|
241
455
|
}
|
|
@@ -245,14 +459,16 @@ function resolveDialect(input) {
|
|
|
245
459
|
}
|
|
246
460
|
function specToDialect(spec) {
|
|
247
461
|
return {
|
|
248
|
-
|
|
462
|
+
// ask() runs the tenant guardrail itself on the final SQL, so the built-in
|
|
463
|
+
// generator's copy is skipped to avoid a second (pre-substitution) report.
|
|
464
|
+
generate: (question, schema, model, options) => generateSelectSqlWithoutTenantGuardrail(spec, question, schema, model, options),
|
|
249
465
|
};
|
|
250
466
|
}
|
|
251
467
|
/** Default chunk-count threshold below which the full DDL is preferred. */
|
|
252
468
|
const DEFAULT_RETRIEVAL_THRESHOLD_CHUNKS = 30;
|
|
253
469
|
const DEFAULT_RETRIEVAL_K = 8;
|
|
254
470
|
async function maybeRetrieveDdl(args) {
|
|
255
|
-
const { options, logger, omitSensitive } = args;
|
|
471
|
+
const { options, logger, omitSensitive, unqualifiedNamespace, quoteIdentifier } = args;
|
|
256
472
|
const retriever = options.retriever;
|
|
257
473
|
if (!retriever)
|
|
258
474
|
return undefined;
|
|
@@ -291,6 +507,8 @@ async function maybeRetrieveDdl(args) {
|
|
|
291
507
|
schema: options.schema,
|
|
292
508
|
results,
|
|
293
509
|
omitSensitiveIdentifiersFromPrompt: omitSensitive,
|
|
510
|
+
unqualifiedNamespace,
|
|
511
|
+
quoteIdentifier,
|
|
294
512
|
});
|
|
295
513
|
logger?.info({
|
|
296
514
|
event: AskDbLogEvent.PipelineRetrievalUsed,
|