@askdb/core 0.5.0-beta.0
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 +201 -0
- package/NOTICE +10 -0
- package/README.md +74 -0
- package/dist/ai/provider.d.ts +122 -0
- package/dist/ai/provider.d.ts.map +1 -0
- package/dist/ai/provider.js +224 -0
- package/dist/ai/provider.js.map +1 -0
- package/dist/ask.d.ts +81 -0
- package/dist/ask.d.ts.map +1 -0
- package/dist/ask.js +83 -0
- package/dist/ask.js.map +1 -0
- package/dist/enrichment/index.d.ts +4 -0
- package/dist/enrichment/index.d.ts.map +1 -0
- package/dist/enrichment/index.js +3 -0
- package/dist/enrichment/index.js.map +1 -0
- package/dist/enrichment/prompt.d.ts +4 -0
- package/dist/enrichment/prompt.d.ts.map +1 -0
- package/dist/enrichment/prompt.js +72 -0
- package/dist/enrichment/prompt.js.map +1 -0
- package/dist/enrichment/suggest.d.ts +23 -0
- package/dist/enrichment/suggest.d.ts.map +1 -0
- package/dist/enrichment/suggest.js +38 -0
- package/dist/enrichment/suggest.js.map +1 -0
- package/dist/enrichment/types.d.ts +44 -0
- package/dist/enrichment/types.d.ts.map +1 -0
- package/dist/enrichment/types.js +2 -0
- package/dist/enrichment/types.js.map +1 -0
- package/dist/errors.d.ts +21 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +33 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/logging/askdb-logger.d.ts +13 -0
- package/dist/logging/askdb-logger.d.ts.map +1 -0
- package/dist/logging/askdb-logger.js +2 -0
- package/dist/logging/askdb-logger.js.map +1 -0
- package/dist/logging/create-askdb-logger.d.ts +25 -0
- package/dist/logging/create-askdb-logger.d.ts.map +1 -0
- package/dist/logging/create-askdb-logger.js +47 -0
- package/dist/logging/create-askdb-logger.js.map +1 -0
- package/dist/logging/log-contract.d.ts +16 -0
- package/dist/logging/log-contract.d.ts.map +1 -0
- package/dist/logging/log-contract.js +23 -0
- package/dist/logging/log-contract.js.map +1 -0
- package/dist/logging/log-events.d.ts +32 -0
- package/dist/logging/log-events.d.ts.map +1 -0
- package/dist/logging/log-events.js +31 -0
- package/dist/logging/log-events.js.map +1 -0
- package/dist/logging/pino-supported-levels.d.ts +9 -0
- package/dist/logging/pino-supported-levels.d.ts.map +1 -0
- package/dist/logging/pino-supported-levels.js +16 -0
- package/dist/logging/pino-supported-levels.js.map +1 -0
- package/dist/modes/types.d.ts +8 -0
- package/dist/modes/types.d.ts.map +1 -0
- package/dist/modes/types.js +20 -0
- package/dist/modes/types.js.map +1 -0
- package/dist/retrieval/synthesize-ddl.d.ts +29 -0
- package/dist/retrieval/synthesize-ddl.d.ts.map +1 -0
- package/dist/retrieval/synthesize-ddl.js +160 -0
- package/dist/retrieval/synthesize-ddl.js.map +1 -0
- package/dist/retrieval/types.d.ts +31 -0
- package/dist/retrieval/types.d.ts.map +1 -0
- package/dist/retrieval/types.js +2 -0
- package/dist/retrieval/types.js.map +1 -0
- package/dist/schema/format.d.ts +76 -0
- package/dist/schema/format.d.ts.map +1 -0
- package/dist/schema/format.js +20 -0
- package/dist/schema/format.js.map +1 -0
- package/dist/schema/normalize.d.ts +31 -0
- package/dist/schema/normalize.d.ts.map +1 -0
- package/dist/schema/normalize.js +89 -0
- package/dist/schema/normalize.js.map +1 -0
- package/dist/schema/parse.d.ts +5 -0
- package/dist/schema/parse.d.ts.map +1 -0
- package/dist/schema/parse.js +37 -0
- package/dist/schema/parse.js.map +1 -0
- package/dist/schema/types.d.ts +23 -0
- package/dist/schema/types.d.ts.map +1 -0
- package/dist/schema/types.js +2 -0
- package/dist/schema/types.js.map +1 -0
- package/dist/schema/v2/describable.d.ts +155 -0
- package/dist/schema/v2/describable.d.ts.map +1 -0
- package/dist/schema/v2/describable.js +44 -0
- package/dist/schema/v2/describable.js.map +1 -0
- package/dist/schema/v2/format.d.ts +15 -0
- package/dist/schema/v2/format.d.ts.map +1 -0
- package/dist/schema/v2/format.js +97 -0
- package/dist/schema/v2/format.js.map +1 -0
- package/dist/schema/v2/index.d.ts +10 -0
- package/dist/schema/v2/index.d.ts.map +1 -0
- package/dist/schema/v2/index.js +7 -0
- package/dist/schema/v2/index.js.map +1 -0
- package/dist/schema/v2/loader.d.ts +18 -0
- package/dist/schema/v2/loader.d.ts.map +1 -0
- package/dist/schema/v2/loader.js +230 -0
- package/dist/schema/v2/loader.js.map +1 -0
- package/dist/schema/v2/normalized.d.ts +58 -0
- package/dist/schema/v2/normalized.d.ts.map +1 -0
- package/dist/schema/v2/normalized.js +2 -0
- package/dist/schema/v2/normalized.js.map +1 -0
- package/dist/schema/v2/parser.d.ts +12 -0
- package/dist/schema/v2/parser.d.ts.map +1 -0
- package/dist/schema/v2/parser.js +48 -0
- package/dist/schema/v2/parser.js.map +1 -0
- package/dist/schema/v2/physical.d.ts +229 -0
- package/dist/schema/v2/physical.d.ts.map +1 -0
- package/dist/schema/v2/physical.js +28 -0
- package/dist/schema/v2/physical.js.map +1 -0
- package/dist/schema/v2/writer.d.ts +14 -0
- package/dist/schema/v2/writer.d.ts.map +1 -0
- package/dist/schema/v2/writer.js +49 -0
- package/dist/schema/v2/writer.js.map +1 -0
- package/dist/sql/extract-sql.d.ts +5 -0
- package/dist/sql/extract-sql.d.ts.map +1 -0
- package/dist/sql/extract-sql.js +12 -0
- package/dist/sql/extract-sql.js.map +1 -0
- package/package.json +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for describing the origin of the Work and
|
|
141
|
+
reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may accept and charge a
|
|
167
|
+
fee for, the acceptance of support, warranty, indemnity, or
|
|
168
|
+
other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Yahya Gilany
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
200
|
+
implied. See the License for the specific language governing
|
|
201
|
+
permissions and limitations under the License.
|
package/NOTICE
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
AskDB
|
|
2
|
+
Copyright 2026 Yahya Gilany
|
|
3
|
+
|
|
4
|
+
This product includes software developed by Yahya Gilany and contributors.
|
|
5
|
+
|
|
6
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
you may not use this file except in compliance with the License.
|
|
8
|
+
You may obtain a copy of the License at
|
|
9
|
+
|
|
10
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
package/README.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# `@askdb/core`
|
|
2
|
+
|
|
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
|
+
|
|
5
|
+
> **Status:** pre-1.0. `0.3.0` moved the Postgres dialect and `@askdb/core/postgres` to `@askdb/postgres`. `0.4.0` documents runtime AI config: pass **`getAskDbRuntimeConfig().ai.aiEnv`** from `@askdb/config` (after `bootstrapAskDbEnv`) into env-aware helpers — core does not read `process.env` by default. See [`docs/adrs/0002-integration-package-layout.md`](../../docs/adrs/0002-integration-package-layout.md) and [`docs/adrs/0005-askdb-config-and-env-bootstrap.md`](../../docs/adrs/0005-askdb-config-and-env-bootstrap.md).
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pnpm add @askdb/core
|
|
11
|
+
# Plus a dialect adapter for the engine you target:
|
|
12
|
+
pnpm add @askdb/postgres
|
|
13
|
+
# Plus a model provider, for example:
|
|
14
|
+
pnpm add ai @ai-sdk/openai
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
`@askdb/core` itself does not depend on `pg`. The optional `pg` peer lives on `@askdb/postgres` for live Postgres introspection.
|
|
18
|
+
|
|
19
|
+
Runtime AI configuration is resolved from an **explicit env map** you pass to `resolveAskDbAiConfig` / `createAskDbLanguageModelFromEnv` (type `AskDbAiEnv`). If you use [`@askdb/config`](../../packages/config/README.md), call `bootstrapAskDbEnv()` then pass **`getAskDbRuntimeConfig().ai.aiEnv`** so `askdb.config.*` / `.config/askdb.*` populate that map.
|
|
20
|
+
|
|
21
|
+
## Schema format
|
|
22
|
+
|
|
23
|
+
`@askdb/core` uses **Schema v2** — a split artifact designed for business-context enrichment and RAG chunking. See [`docs/contracts/schema-v2.md`](../../docs/contracts/schema-v2.md) for the full contract.
|
|
24
|
+
|
|
25
|
+
### Directory layout
|
|
26
|
+
|
|
27
|
+
```text
|
|
28
|
+
my-app.schema/
|
|
29
|
+
schema.json # physical layer — tables, columns, types, FKs, sensitive flags
|
|
30
|
+
tables/
|
|
31
|
+
users.md # describable layer — descriptions, aliases, common query language
|
|
32
|
+
orders.md
|
|
33
|
+
concepts.md # optional — cross-table vocabulary
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Load a v2 directory
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import { loadSchema } from "@askdb/core";
|
|
40
|
+
|
|
41
|
+
const schema = loadSchema("./fixtures/schemas/orders-users.schema");
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Minimal example — with the Postgres dialect adapter
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
import { ask, loadSchema } from "@askdb/core";
|
|
48
|
+
import { postgresDialect } from "@askdb/postgres";
|
|
49
|
+
import { openai } from "@ai-sdk/openai";
|
|
50
|
+
|
|
51
|
+
const schema = loadSchema("./fixtures/schemas/orders-users.schema");
|
|
52
|
+
|
|
53
|
+
const { sql } = await ask({
|
|
54
|
+
question: "Top 5 customers by lifetime value?",
|
|
55
|
+
schema,
|
|
56
|
+
model: openai("gpt-4o"),
|
|
57
|
+
dialect: postgresDialect,
|
|
58
|
+
});
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
## What you get
|
|
62
|
+
|
|
63
|
+
- `ask({ question, schema, model, dialect })` — generate validated SQL.
|
|
64
|
+
- `AskDialect` — the dialect adapter contract. `@askdb/postgres` exports a ready-made one.
|
|
65
|
+
- `loadSchema(path)` — load a Schema v2 directory, bundled JSON, or `schema.json` path.
|
|
66
|
+
- `loadSchemaFromJson(raw)` — parse a Schema v2 bundled JSON string (e.g. from an env var).
|
|
67
|
+
- `parseTableMarkdown` / `writeTableMarkdown` — round-trippable describable-layer parser/writer.
|
|
68
|
+
- `extractSqlFromModelText` — generic fenced-code extractor (dialect-agnostic).
|
|
69
|
+
- Structured logging hooks (`createAskDbLogger`, log-event contract).
|
|
70
|
+
- Modes (`schema_only`, etc.) per `docs/contracts/modes-v1.md`.
|
|
71
|
+
|
|
72
|
+
## License
|
|
73
|
+
|
|
74
|
+
Apache-2.0 © Yahya Gilany. See [LICENSE](./LICENSE) and [NOTICE](./NOTICE).
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { EmbeddingModel, LanguageModel } from "ai";
|
|
2
|
+
/**
|
|
3
|
+
* AI provider selector. AskDB is BYO-LanguageModel at the function level
|
|
4
|
+
* (see `ask()`), but the bundled apps (CLI, HTTP API, Studio, TUI) all need
|
|
5
|
+
* to construct one from environment variables. This module is the single
|
|
6
|
+
* place that does that, so adding a new provider only touches one file.
|
|
7
|
+
*
|
|
8
|
+
* - `openai` — OpenAI or any OpenAI-compatible REST endpoint
|
|
9
|
+
* (set `ASKDB_AI_BASE_URL` for compatible endpoints, including
|
|
10
|
+
* Azure AI Foundry's `/openai/v1` route and self-hosted gateways).
|
|
11
|
+
* - `azure` — Azure OpenAI Service / Azure AI Foundry deployments via
|
|
12
|
+
* `@ai-sdk/azure`. Uses the `resourceName` + deployment-name
|
|
13
|
+
* URL shape with the `api-key` header.
|
|
14
|
+
*/
|
|
15
|
+
export type AskDbAiProvider = "openai" | "azure";
|
|
16
|
+
export type AskDbAiConfig = {
|
|
17
|
+
provider: AskDbAiProvider;
|
|
18
|
+
apiKey: string;
|
|
19
|
+
/** OpenAI: model id (e.g. `gpt-4o-mini`). Azure: deployment name. */
|
|
20
|
+
model: string;
|
|
21
|
+
/** Custom REST base URL. Optional for OpenAI; for Azure, overrides resourceName. */
|
|
22
|
+
baseURL?: string;
|
|
23
|
+
/** Azure only: resource subdomain (e.g. `my-foundry` for `https://my-foundry.openai.azure.com`). */
|
|
24
|
+
resourceName?: string;
|
|
25
|
+
/** Azure only: API version (e.g. `2024-10-21`). */
|
|
26
|
+
apiVersion?: string;
|
|
27
|
+
};
|
|
28
|
+
export type AskDbAiEnv = Record<string, string | undefined>;
|
|
29
|
+
export type ResolveAskDbAiConfigOptions = {
|
|
30
|
+
/** Default model when no env override is set. */
|
|
31
|
+
modelDefault?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Per-app model env var checked first (e.g. `ASKDB_TUI_MODEL`, `ASKDB_STUDIO_MODEL`).
|
|
34
|
+
* Falls through to `ASKDB_AI_MODEL`, `ASKDB_MODEL`, `OPENAI_MODEL`, `modelDefault`.
|
|
35
|
+
*/
|
|
36
|
+
modelEnvVar?: string;
|
|
37
|
+
};
|
|
38
|
+
/**
|
|
39
|
+
* Resolves a provider config from environment variables. Returns `undefined`
|
|
40
|
+
* if no API key is configured (callers treat this as "AI is disabled" rather
|
|
41
|
+
* than erroring, so AI features can be optional).
|
|
42
|
+
*
|
|
43
|
+
* BYO-key design: each provider has its native env vars (e.g. `OPENAI_API_KEY`
|
|
44
|
+
* for OpenAI, `AZURE_OPENAI_API_KEY` for Azure / Foundry). The `ASKDB_AI_*`
|
|
45
|
+
* family is a universal alias set that works across providers — useful when
|
|
46
|
+
* you want a single set of names across deployments.
|
|
47
|
+
*
|
|
48
|
+
* Precedence for the API key (within the selected provider):
|
|
49
|
+
* 1. `ASKDB_AI_API_KEY` — universal alias (primary)
|
|
50
|
+
* 2. provider-native primary — `OPENAI_API_KEY` or `AZURE_OPENAI_API_KEY` / `AZURE_API_KEY`
|
|
51
|
+
* 3. provider-native secondary — `OPENAI_API_KEY_SECONDARY` or `AZURE_OPENAI_API_KEY_SECONDARY`
|
|
52
|
+
* 4. `ASKDB_AI_API_KEY_SECONDARY` — universal rotation fallback
|
|
53
|
+
*
|
|
54
|
+
* **Important:** Only `@askdb/config` reads `process.env` (during dotenv load and while evaluating
|
|
55
|
+
* `askdb.config.*`). Pass `getAskDbRuntimeConfig().ai.aiEnv` from `@askdb/config` after
|
|
56
|
+
* `bootstrapAskDbEnv()`, or an explicit plain object in tests.
|
|
57
|
+
*
|
|
58
|
+
* @param env - String map in the shape of canonical AskDB env keys (from the runtime snapshot).
|
|
59
|
+
* @param options - Optional per-app overrides (e.g. a per-app model env var).
|
|
60
|
+
*/
|
|
61
|
+
export declare function resolveAskDbAiConfig(env: AskDbAiEnv, options?: ResolveAskDbAiConfigOptions): AskDbAiConfig | undefined;
|
|
62
|
+
export type ResolveAskDbEmbeddingConfigOptions = {
|
|
63
|
+
/** Default embedding model/deployment when no env override is set. */
|
|
64
|
+
modelDefault?: string;
|
|
65
|
+
/**
|
|
66
|
+
* Per-app embedding model env var checked first
|
|
67
|
+
* (e.g. `ASKDB_STUDIO_RAG_EMBEDDER_MODEL`).
|
|
68
|
+
*/
|
|
69
|
+
modelEnvVar?: string;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Resolves an embedding model config from the same provider/key/base URL
|
|
73
|
+
* connection as `resolveAskDbAiConfig`, but with embedding-specific model
|
|
74
|
+
* precedence so chat model ids (for example `gpt-4o`) are not used as
|
|
75
|
+
* embedding model ids by accident.
|
|
76
|
+
*
|
|
77
|
+
* **Important:** Only `@askdb/config` reads `process.env` (during dotenv load and while evaluating
|
|
78
|
+
* `askdb.config.*`). Pass `getAskDbRuntimeConfig().ai.aiEnv` from `@askdb/config` after
|
|
79
|
+
* `bootstrapAskDbEnv()`, or an explicit plain object in tests.
|
|
80
|
+
*
|
|
81
|
+
* @param env - String map in the shape of canonical AskDB env keys (from the runtime snapshot).
|
|
82
|
+
* @param options - Optional per-app overrides (e.g. a per-app embedding model env var).
|
|
83
|
+
*/
|
|
84
|
+
export declare function resolveAskDbEmbeddingConfig(env: AskDbAiEnv, options?: ResolveAskDbEmbeddingConfigOptions): AskDbAiConfig | undefined;
|
|
85
|
+
/** Build a {@link LanguageModel} from an already-resolved config. */
|
|
86
|
+
export declare function createAskDbLanguageModel(config: AskDbAiConfig): Promise<LanguageModel>;
|
|
87
|
+
export type CreateAskDbEmbeddingModelOptions = {
|
|
88
|
+
/** Optional dimensionality override for providers that support it. */
|
|
89
|
+
dimensions?: number;
|
|
90
|
+
/** Optional end-user id forwarded to providers that support it. */
|
|
91
|
+
user?: string;
|
|
92
|
+
};
|
|
93
|
+
/** Build an AI SDK text embedding model from an already-resolved config. */
|
|
94
|
+
export declare function createAskDbEmbeddingModel(config: AskDbAiConfig, options?: CreateAskDbEmbeddingModelOptions): Promise<EmbeddingModel<string>>;
|
|
95
|
+
/**
|
|
96
|
+
* Convenience wrapper: resolve config from env, then construct a model.
|
|
97
|
+
* Returns `undefined` when no API key is configured.
|
|
98
|
+
*
|
|
99
|
+
* **Important:** Only `@askdb/config` reads `process.env` (during dotenv load and while evaluating
|
|
100
|
+
* `askdb.config.*`). Pass `getAskDbRuntimeConfig().ai.aiEnv` from `@askdb/config` after
|
|
101
|
+
* `bootstrapAskDbEnv()`, or an explicit plain object in tests.
|
|
102
|
+
*
|
|
103
|
+
* @param env - String map in the shape of canonical AskDB env keys (from the runtime snapshot).
|
|
104
|
+
* @param options - Optional per-app overrides (e.g. a per-app model env var).
|
|
105
|
+
*/
|
|
106
|
+
export declare function createAskDbLanguageModelFromEnv(env: AskDbAiEnv, options?: ResolveAskDbAiConfigOptions): Promise<LanguageModel | undefined>;
|
|
107
|
+
/** Resolve embedding config from env, then construct an AI SDK embedding model.
|
|
108
|
+
*
|
|
109
|
+
* **Important:** Only `@askdb/config` reads `process.env` (during dotenv load and while evaluating
|
|
110
|
+
* `askdb.config.*`). Pass `getAskDbRuntimeConfig().ai.aiEnv` after `bootstrapAskDbEnv()`, or
|
|
111
|
+
* an explicit plain object in tests.
|
|
112
|
+
*
|
|
113
|
+
* @param env - String map in the shape of canonical AskDB env keys (from the runtime snapshot).
|
|
114
|
+
* @param options - Optional per-app overrides plus embedding-specific model/dimensions settings.
|
|
115
|
+
*/
|
|
116
|
+
export declare function createAskDbEmbeddingModelFromEnv(env: AskDbAiEnv, options?: ResolveAskDbEmbeddingConfigOptions & CreateAskDbEmbeddingModelOptions): Promise<EmbeddingModel<string> | undefined>;
|
|
117
|
+
/**
|
|
118
|
+
* Human-readable message describing how to configure AI, used by callers
|
|
119
|
+
* when no key is configured.
|
|
120
|
+
*/
|
|
121
|
+
export declare function askDbAiKeyMissingMessage(context: string): string;
|
|
122
|
+
//# sourceMappingURL=provider.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../../src/ai/provider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,aAAa,EAAE,MAAM,IAAI,CAAC;AAGxD;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,OAAO,CAAC;AAEjD,MAAM,MAAM,aAAa,GAAG;IAC1B,QAAQ,EAAE,eAAe,CAAC;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,qEAAqE;IACrE,KAAK,EAAE,MAAM,CAAC;IACd,oFAAoF;IACpF,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,oGAAoG;IACpG,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,mDAAmD;IACnD,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;AAE5D,MAAM,MAAM,2BAA2B,GAAG;IACxC,iDAAiD;IACjD,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAA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
|
1
|
+
import { createOpenAI } from "@ai-sdk/openai";
|
|
2
|
+
const DEFAULT_MODEL = "gpt-4o-mini";
|
|
3
|
+
const DEFAULT_EMBEDDING_MODEL = "text-embedding-3-small";
|
|
4
|
+
function readProvider(env) {
|
|
5
|
+
const raw = (env.ASKDB_AI_PROVIDER ?? "").toLowerCase().trim();
|
|
6
|
+
if (raw === "" || raw === "openai")
|
|
7
|
+
return "openai";
|
|
8
|
+
if (raw === "azure" || raw === "azure-openai" || raw === "foundry")
|
|
9
|
+
return "azure";
|
|
10
|
+
throw new Error(`Unknown ASKDB_AI_PROVIDER "${env.ASKDB_AI_PROVIDER}". Expected "openai" or "azure".`);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Resolves a provider config from environment variables. Returns `undefined`
|
|
14
|
+
* if no API key is configured (callers treat this as "AI is disabled" rather
|
|
15
|
+
* than erroring, so AI features can be optional).
|
|
16
|
+
*
|
|
17
|
+
* BYO-key design: each provider has its native env vars (e.g. `OPENAI_API_KEY`
|
|
18
|
+
* for OpenAI, `AZURE_OPENAI_API_KEY` for Azure / Foundry). The `ASKDB_AI_*`
|
|
19
|
+
* family is a universal alias set that works across providers — useful when
|
|
20
|
+
* you want a single set of names across deployments.
|
|
21
|
+
*
|
|
22
|
+
* Precedence for the API key (within the selected provider):
|
|
23
|
+
* 1. `ASKDB_AI_API_KEY` — universal alias (primary)
|
|
24
|
+
* 2. provider-native primary — `OPENAI_API_KEY` or `AZURE_OPENAI_API_KEY` / `AZURE_API_KEY`
|
|
25
|
+
* 3. provider-native secondary — `OPENAI_API_KEY_SECONDARY` or `AZURE_OPENAI_API_KEY_SECONDARY`
|
|
26
|
+
* 4. `ASKDB_AI_API_KEY_SECONDARY` — universal rotation fallback
|
|
27
|
+
*
|
|
28
|
+
* **Important:** Only `@askdb/config` reads `process.env` (during dotenv load and while evaluating
|
|
29
|
+
* `askdb.config.*`). Pass `getAskDbRuntimeConfig().ai.aiEnv` from `@askdb/config` after
|
|
30
|
+
* `bootstrapAskDbEnv()`, or an explicit plain object in tests.
|
|
31
|
+
*
|
|
32
|
+
* @param env - String map in the shape of canonical AskDB env keys (from the runtime snapshot).
|
|
33
|
+
* @param options - Optional per-app overrides (e.g. a per-app model env var).
|
|
34
|
+
*/
|
|
35
|
+
export function resolveAskDbAiConfig(env, options = {}) {
|
|
36
|
+
const provider = readProvider(env);
|
|
37
|
+
const providerNativeKey = provider === "azure"
|
|
38
|
+
? env.AZURE_OPENAI_API_KEY || env.AZURE_API_KEY
|
|
39
|
+
: env.OPENAI_API_KEY;
|
|
40
|
+
const providerNativeKeySecondary = provider === "azure"
|
|
41
|
+
? env.AZURE_OPENAI_API_KEY_SECONDARY || env.AZURE_API_KEY_SECONDARY
|
|
42
|
+
: env.OPENAI_API_KEY_SECONDARY;
|
|
43
|
+
const apiKey = env.ASKDB_AI_API_KEY ||
|
|
44
|
+
providerNativeKey ||
|
|
45
|
+
providerNativeKeySecondary ||
|
|
46
|
+
env.ASKDB_AI_API_KEY_SECONDARY ||
|
|
47
|
+
undefined;
|
|
48
|
+
if (!apiKey)
|
|
49
|
+
return undefined;
|
|
50
|
+
const perAppModel = options.modelEnvVar ? env[options.modelEnvVar] : undefined;
|
|
51
|
+
const providerNativeModel = provider === "azure"
|
|
52
|
+
? env.AZURE_OPENAI_DEPLOYMENT || env.AZURE_DEPLOYMENT_NAME
|
|
53
|
+
: env.OPENAI_MODEL;
|
|
54
|
+
const model = perAppModel ||
|
|
55
|
+
env.ASKDB_AI_MODEL ||
|
|
56
|
+
env.ASKDB_MODEL ||
|
|
57
|
+
providerNativeModel ||
|
|
58
|
+
options.modelDefault ||
|
|
59
|
+
DEFAULT_MODEL;
|
|
60
|
+
const providerNativeBaseURL = provider === "azure"
|
|
61
|
+
? env.AZURE_OPENAI_BASE_URL || env.AZURE_OPENAI_ENDPOINT || env.AZURE_BASE_URL
|
|
62
|
+
: env.OPENAI_BASE_URL;
|
|
63
|
+
const baseURL = env.ASKDB_AI_BASE_URL || providerNativeBaseURL || undefined;
|
|
64
|
+
const resourceName = env.ASKDB_AI_AZURE_RESOURCE_NAME || env.AZURE_RESOURCE_NAME || undefined;
|
|
65
|
+
const apiVersion = env.ASKDB_AI_AZURE_API_VERSION || env.AZURE_OPENAI_API_VERSION || env.AZURE_API_VERSION || undefined;
|
|
66
|
+
if (provider === "azure" && !baseURL && !resourceName) {
|
|
67
|
+
throw new Error("Azure provider requires ASKDB_AI_AZURE_RESOURCE_NAME (e.g. 'my-foundry') " +
|
|
68
|
+
"or ASKDB_AI_BASE_URL pointing at the full endpoint.");
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
provider,
|
|
72
|
+
apiKey,
|
|
73
|
+
model,
|
|
74
|
+
...(baseURL ? { baseURL } : {}),
|
|
75
|
+
...(resourceName ? { resourceName } : {}),
|
|
76
|
+
...(apiVersion ? { apiVersion } : {}),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Resolves an embedding model config from the same provider/key/base URL
|
|
81
|
+
* connection as `resolveAskDbAiConfig`, but with embedding-specific model
|
|
82
|
+
* precedence so chat model ids (for example `gpt-4o`) are not used as
|
|
83
|
+
* embedding model ids by accident.
|
|
84
|
+
*
|
|
85
|
+
* **Important:** Only `@askdb/config` reads `process.env` (during dotenv load and while evaluating
|
|
86
|
+
* `askdb.config.*`). Pass `getAskDbRuntimeConfig().ai.aiEnv` from `@askdb/config` after
|
|
87
|
+
* `bootstrapAskDbEnv()`, or an explicit plain object in tests.
|
|
88
|
+
*
|
|
89
|
+
* @param env - String map in the shape of canonical AskDB env keys (from the runtime snapshot).
|
|
90
|
+
* @param options - Optional per-app overrides (e.g. a per-app embedding model env var).
|
|
91
|
+
*/
|
|
92
|
+
export function resolveAskDbEmbeddingConfig(env, options = {}) {
|
|
93
|
+
const provider = readProvider(env);
|
|
94
|
+
const providerNativeKey = provider === "azure"
|
|
95
|
+
? env.AZURE_OPENAI_API_KEY || env.AZURE_API_KEY
|
|
96
|
+
: env.OPENAI_API_KEY;
|
|
97
|
+
const providerNativeKeySecondary = provider === "azure"
|
|
98
|
+
? env.AZURE_OPENAI_API_KEY_SECONDARY || env.AZURE_API_KEY_SECONDARY
|
|
99
|
+
: env.OPENAI_API_KEY_SECONDARY;
|
|
100
|
+
const apiKey = env.ASKDB_AI_API_KEY ||
|
|
101
|
+
providerNativeKey ||
|
|
102
|
+
providerNativeKeySecondary ||
|
|
103
|
+
env.ASKDB_AI_API_KEY_SECONDARY ||
|
|
104
|
+
undefined;
|
|
105
|
+
if (!apiKey)
|
|
106
|
+
return undefined;
|
|
107
|
+
const perAppModel = options.modelEnvVar ? env[options.modelEnvVar] : undefined;
|
|
108
|
+
const providerNativeModel = provider === "azure"
|
|
109
|
+
? env.AZURE_OPENAI_EMBEDDING_DEPLOYMENT ||
|
|
110
|
+
env.AZURE_EMBEDDING_DEPLOYMENT_NAME
|
|
111
|
+
: env.OPENAI_EMBEDDING_MODEL;
|
|
112
|
+
const model = perAppModel ||
|
|
113
|
+
env.ASKDB_AI_EMBEDDING_MODEL ||
|
|
114
|
+
env.ASKDB_EMBEDDING_MODEL ||
|
|
115
|
+
providerNativeModel ||
|
|
116
|
+
options.modelDefault ||
|
|
117
|
+
DEFAULT_EMBEDDING_MODEL;
|
|
118
|
+
const providerNativeBaseURL = provider === "azure"
|
|
119
|
+
? env.AZURE_OPENAI_BASE_URL || env.AZURE_OPENAI_ENDPOINT || env.AZURE_BASE_URL
|
|
120
|
+
: env.OPENAI_BASE_URL;
|
|
121
|
+
const baseURL = env.ASKDB_AI_BASE_URL || providerNativeBaseURL || undefined;
|
|
122
|
+
const resourceName = env.ASKDB_AI_AZURE_RESOURCE_NAME || env.AZURE_RESOURCE_NAME || undefined;
|
|
123
|
+
const apiVersion = env.ASKDB_AI_AZURE_API_VERSION || env.AZURE_OPENAI_API_VERSION || env.AZURE_API_VERSION || undefined;
|
|
124
|
+
if (provider === "azure" && !baseURL && !resourceName) {
|
|
125
|
+
throw new Error("Azure provider requires ASKDB_AI_AZURE_RESOURCE_NAME (e.g. 'my-foundry') " +
|
|
126
|
+
"or ASKDB_AI_BASE_URL pointing at the full endpoint.");
|
|
127
|
+
}
|
|
128
|
+
return {
|
|
129
|
+
provider,
|
|
130
|
+
apiKey,
|
|
131
|
+
model,
|
|
132
|
+
...(baseURL ? { baseURL } : {}),
|
|
133
|
+
...(resourceName ? { resourceName } : {}),
|
|
134
|
+
...(apiVersion ? { apiVersion } : {}),
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
/** Build a {@link LanguageModel} from an already-resolved config. */
|
|
138
|
+
export async function createAskDbLanguageModel(config) {
|
|
139
|
+
if (config.provider === "azure") {
|
|
140
|
+
// Lazy-loaded so `@ai-sdk/azure` is only required when actually used.
|
|
141
|
+
const { createAzure } = await import("@ai-sdk/azure");
|
|
142
|
+
const azure = createAzure({
|
|
143
|
+
apiKey: config.apiKey,
|
|
144
|
+
...(config.resourceName ? { resourceName: config.resourceName } : {}),
|
|
145
|
+
...(config.baseURL ? { baseURL: config.baseURL } : {}),
|
|
146
|
+
...(config.apiVersion ? { apiVersion: config.apiVersion } : {}),
|
|
147
|
+
});
|
|
148
|
+
return azure(config.model);
|
|
149
|
+
}
|
|
150
|
+
const openai = createOpenAI({
|
|
151
|
+
apiKey: config.apiKey,
|
|
152
|
+
...(config.baseURL ? { baseURL: config.baseURL } : {}),
|
|
153
|
+
});
|
|
154
|
+
return openai(config.model);
|
|
155
|
+
}
|
|
156
|
+
/** Build an AI SDK text embedding model from an already-resolved config. */
|
|
157
|
+
export async function createAskDbEmbeddingModel(config, options = {}) {
|
|
158
|
+
if (config.provider === "azure") {
|
|
159
|
+
const { createAzure } = await import("@ai-sdk/azure");
|
|
160
|
+
const azure = createAzure({
|
|
161
|
+
apiKey: config.apiKey,
|
|
162
|
+
...(config.resourceName ? { resourceName: config.resourceName } : {}),
|
|
163
|
+
...(config.baseURL ? { baseURL: config.baseURL } : {}),
|
|
164
|
+
...(config.apiVersion ? { apiVersion: config.apiVersion } : {}),
|
|
165
|
+
});
|
|
166
|
+
return azure.embedding(config.model, {
|
|
167
|
+
dimensions: options.dimensions,
|
|
168
|
+
user: options.user,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
const openai = createOpenAI({
|
|
172
|
+
apiKey: config.apiKey,
|
|
173
|
+
...(config.baseURL ? { baseURL: config.baseURL } : {}),
|
|
174
|
+
});
|
|
175
|
+
return openai.embedding(config.model, {
|
|
176
|
+
dimensions: options.dimensions,
|
|
177
|
+
user: options.user,
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Convenience wrapper: resolve config from env, then construct a model.
|
|
182
|
+
* Returns `undefined` when no API key is configured.
|
|
183
|
+
*
|
|
184
|
+
* **Important:** Only `@askdb/config` reads `process.env` (during dotenv load and while evaluating
|
|
185
|
+
* `askdb.config.*`). Pass `getAskDbRuntimeConfig().ai.aiEnv` from `@askdb/config` after
|
|
186
|
+
* `bootstrapAskDbEnv()`, or an explicit plain object in tests.
|
|
187
|
+
*
|
|
188
|
+
* @param env - String map in the shape of canonical AskDB env keys (from the runtime snapshot).
|
|
189
|
+
* @param options - Optional per-app overrides (e.g. a per-app model env var).
|
|
190
|
+
*/
|
|
191
|
+
export async function createAskDbLanguageModelFromEnv(env, options = {}) {
|
|
192
|
+
const config = resolveAskDbAiConfig(env, options);
|
|
193
|
+
if (!config)
|
|
194
|
+
return undefined;
|
|
195
|
+
return createAskDbLanguageModel(config);
|
|
196
|
+
}
|
|
197
|
+
/** Resolve embedding config from env, then construct an AI SDK embedding model.
|
|
198
|
+
*
|
|
199
|
+
* **Important:** Only `@askdb/config` reads `process.env` (during dotenv load and while evaluating
|
|
200
|
+
* `askdb.config.*`). Pass `getAskDbRuntimeConfig().ai.aiEnv` after `bootstrapAskDbEnv()`, or
|
|
201
|
+
* an explicit plain object in tests.
|
|
202
|
+
*
|
|
203
|
+
* @param env - String map in the shape of canonical AskDB env keys (from the runtime snapshot).
|
|
204
|
+
* @param options - Optional per-app overrides plus embedding-specific model/dimensions settings.
|
|
205
|
+
*/
|
|
206
|
+
export async function createAskDbEmbeddingModelFromEnv(env, options = {}) {
|
|
207
|
+
const config = resolveAskDbEmbeddingConfig(env, options);
|
|
208
|
+
if (!config)
|
|
209
|
+
return undefined;
|
|
210
|
+
return createAskDbEmbeddingModel(config, options);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Human-readable message describing how to configure AI, used by callers
|
|
214
|
+
* when no key is configured.
|
|
215
|
+
*/
|
|
216
|
+
export function askDbAiKeyMissingMessage(context) {
|
|
217
|
+
return (`${context}: no AI API key configured. ` +
|
|
218
|
+
`For OpenAI, set OPENAI_API_KEY (or ASKDB_AI_API_KEY). ` +
|
|
219
|
+
`For Azure / Microsoft Foundry, set ASKDB_AI_PROVIDER=azure plus ` +
|
|
220
|
+
`AZURE_OPENAI_API_KEY (or ASKDB_AI_API_KEY), ` +
|
|
221
|
+
`ASKDB_AI_AZURE_RESOURCE_NAME (or ASKDB_AI_BASE_URL), and a deployment name ` +
|
|
222
|
+
`via ASKDB_AI_MODEL.`);
|
|
223
|
+
}
|
|
224
|
+
//# sourceMappingURL=provider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"provider.js","sourceRoot":"","sources":["../../src/ai/provider.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AA0C9C,MAAM,aAAa,GAAG,aAAa,CAAC;AACpC,MAAM,uBAAuB,GAAG,wBAAwB,CAAC;AAEzD,SAAS,YAAY,CAAC,GAAe;IACnC,MAAM,GAAG,GAAG,CAAC,GAAG,CAAC,iBAAiB,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC,IAAI,EAAE,CAAC;IAC/D,IAAI,GAAG,KAAK,EAAE,IAAI,GAAG,KAAK,QAAQ;QAAE,OAAO,QAAQ,CAAC;IACpD,IAAI,GA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