import type { AskDbAiProviderId, AskDbDialectId, AskDbLogLevel, AskDbModeV1, AskDbRagEmbedder, AskDbRagStore, AskDbReasoningEffort, AskDbStudioExecuteProvider, } from "./constants.js"; // --------------------------------------------------------------------------- // AI provider connections // --------------------------------------------------------------------------- /** OpenAI connection (`ai.providerConfig.openai`). */ export type OpenaiConnection = { apiKey?: string; /** Use it for an OpenAI-compatible endpoint. */ baseUrl?: string; }; /** Azure OpenAI connection (`ai.providerConfig.azure`). */ export type AzureConnection = { apiKey?: string; /** Used when `apiKey` is unset, for example while a key rotates. */ secondaryApiKey?: string; /** * Azure resource name — the subdomain of your endpoint, e.g. `"my-foundry"` * for `https://my-foundry.openai.azure.com`. One of `resourceName` or * `baseUrl` is required. */ resourceName?: string; /** Full endpoint URL. Overrides `resourceName` when both are set. */ baseUrl?: string; apiVersion?: string; }; /** Microsoft Foundry connection (`ai.providerConfig.foundry`): the same fields as {@link AzureConnection}. */ export type FoundryConnection = AzureConnection; /** Anthropic connection (`ai.providerConfig.anthropic`). Anthropic has no embeddings API. */ export type AnthropicConnection = { apiKey?: string; baseUrl?: string; }; /** Google Gemini connection (`ai.providerConfig.google`). */ export type GoogleConnection = { apiKey?: string; baseUrl?: string; }; /** Vercel AI Gateway connection (`ai.providerConfig.gateway`), built into `ai` — no extra provider package. */ export type GatewayConnection = { /** AI Gateway API key. */ apiKey?: string; baseUrl?: string; }; /** * Connection for a provider AskDB has no dedicated type for, keyed by its provider id * (`ai.providerConfig.mistral`). Works end to end only when the consuming registry has an adapter * registered under that provider id. */ export type CustomConnection = { apiKey?: string; baseUrl?: string; }; /** A connection plus its name, unique within its provider. */ export type AiNamedConnection = T & { /** Defaults to `"default"`. Unique within its provider. */ name?: string; }; /** One connection, or a list of them when a provider needs more than one (for example two Azure resources). */ export type AiConnections = AiNamedConnection | readonly AiNamedConnection[]; /** * `ai.providerConfig`: provider connections only (keys, endpoints, resources, API versions), * keyed by provider id. The model choice lives in `ai.language` and `ai.embedding`. */ export type AiProviderConnections = { openai?: AiConnections; azure?: AiConnections; foundry?: AiConnections; anthropic?: AiConnections; google?: AiConnections; gateway?: AiConnections; /** * Any other key is a custom provider id (an adapter registered under that name), whose * connection is a {@link CustomConnection}. TypeScript also checks the keys above against this * signature, so it accepts the widest built-in connection (Azure's) too. */ [provider: string]: AiConnections | AiConnections | undefined; }; /** * Provider-portable reasoning/latency effort for AskDB model calls. Unset * (the default), no reasoning `providerOptions` are sent, so the * provider/model's own default applies. */ export type AskDbAiReasoningConfig = { /** Global default applied to every AskDB model-call site unless overridden below. */ effort?: AskDbReasoningEffort; /** * Override for NL→SQL generation calls. This is the accuracy-sensitive * path — leave unset (provider default) or use `"medium"`/`"high"`. */ nlToSql?: AskDbReasoningEffort; /** * Override for enrichment/suggestion calls. These are non-critical, so * it's safe to bias toward `"low"`/`"minimal"` for latency/cost. */ enrichment?: AskDbReasoningEffort; }; /** `ai.language`: the language model AskDB generates SQL and enrichment suggestions with. */ export type AskDbAiLanguageConfig = { /** Defaults to `ai.provider`. */ provider?: AskDbAiProviderId | (string & {}); /** Name of a connection in `ai.providerConfig.`. Defaults to `"default"`. */ connection?: string; /** Defaults to the provider's default language model (none for a custom provider). On Azure, the deployment name. */ model?: string; /** * Underlying model id when `model` is an alias, e.g. `"gpt-5"` or `"o3-mini"` behind an Azure * deployment name. Deployment names are arbitrary, so AskDB can't always infer reasoning-model * support from `model` alone. Read on Azure and Foundry. */ modelFamily?: string; reasoning?: AskDbAiReasoningConfig; }; /** `ai.embedding`: the embedding model behind `rag.embedder: "ai"`. Ignored when `rag.embedder` is `"mock"`. */ export type AskDbAiEmbeddingConfig = { /** Defaults to `ai.provider`. Anthropic has no embeddings API, so it can't be this section's provider. */ provider?: AskDbAiProviderId | (string & {}); /** Name of a connection in `ai.providerConfig.`. Defaults to `"default"`. */ connection?: string; /** Required when `rag.embedder` is `"ai"`: AskDB has no default embedding model. On Azure, the deployment name. */ model?: string; /** * Vector size to request from the provider, as a positive integer or a string holding one, for * models that let you choose one (OpenAI's text-embedding-3 models, Gemini's). Unset: the * model's own width, which AskDB learns from the model when it builds an index. */ dimensions?: string | number; }; /** * `ai`: provider connections plus one section per use of a model. * * - **`provider`**: the default provider for both sections. * - **`providerConfig`**: provider connections, keyed by provider id. * - **`language`**: the language model (NL→SQL, enrichment suggestions). * - **`embedding`**: the embedding model behind `rag.embedder: "ai"`. * * A section's provider is `section.provider ?? ai.provider`, and its connection is * `section.connection ?? "default"` within that provider. Loading fails when a section resolves * to no provider or to a connection that doesn't exist. */ export type AskDbAiConfig = { provider?: AskDbAiProviderId | (string & {}); providerConfig?: AiProviderConnections; language?: AskDbAiLanguageConfig; embedding?: AskDbAiEmbeddingConfig; }; // --------------------------------------------------------------------------- // RAG configs // --------------------------------------------------------------------------- export type FileStoreConfig = { /** * Passed to `createFileStore({ basePath })` — vector files use `.embeddings.*`. * When unset, the CLI and Studio use `/schema` (`./askdb/schema` with the * default `introspection.outputDir`). */ basePath?: string; }; /** The in-memory store has no options. */ export type MemoryStoreConfig = Record; export type PgvectorStoreConfig = { /** Connection string for pgvector. */ databaseUrl?: string; table?: string; /** When unset, AskDB uses `hnsw`. */ indexStrategy?: string; }; // --------------------------------------------------------------------------- // Introspection configs // --------------------------------------------------------------------------- /** All introspection provider-specific configs as optional fields — allows storing multiple provider configs simultaneously. */ export type IntrospectionProviderConfigs = { postgres?: { /** * Postgres connection URL for live introspection. * When omitted, pass `--url` to `askdb introspect`. */ databaseUrl?: string; }; prisma?: { /** * Path to a `schema.prisma` file or directory containing `.prisma` files. * When omitted, `@askdb/prisma` auto-discovers `prisma/schema.prisma` or `schema.prisma` * in the project root — no explicit path needed. */ schemaPath?: string; }; mysql?: { /** * MySQL connection URL (e.g. `mysql://user:pass@host:port/database`). * When omitted, pass `--url` to `askdb introspect`. */ databaseUrl?: string; }; sqlite?: { /** * Path to an existing `.db` / `.sqlite` file, which introspection opens * read-only, so `:memory:` doesn't work. Required — * SQLite has no URL-shaped fallback because `DATABASE_URL` is typically a URL * for a network engine. */ file?: string; }; sqlserver?: { /** * Microsoft SQL Server connection URL (mssql URI form or the equivalent * `Server=...;` connection string). When omitted, pass `--url` to `askdb introspect`. */ databaseUrl?: string; }; }; /** Discriminated union branch for `introspection` when `provider` is `"postgres"`. */ export type PostgresIntrospectionConfig = { provider: "postgres"; providerConfig?: IntrospectionProviderConfigs; /** * Default introspection output directory. * When unset/blank, AskDB uses the package default `./askdb/`. */ outputDir?: string; /** * Schemas to introspect, like `askdb introspect --schemas` (which overrides it), e.g. * `["public", "sales"]`. On MySQL/MariaDB these are databases: each becomes its own * namespace and cross-database foreign keys are kept; without a list, only the * connection URL's database is read, under the `public` namespace. On Postgres and SQL * Server, the default is every non-system schema. */ schemas?: string[]; }; /** Discriminated union branch for `introspection` when `provider` is `"prisma"`. */ export type PrismaIntrospectionConfig = { provider: "prisma"; providerConfig?: IntrospectionProviderConfigs; /** * Default introspection output directory. * When unset/blank, AskDB uses the package default `./askdb/`. */ outputDir?: string; /** * Schemas to introspect, like `askdb introspect --schemas` (which overrides it), e.g. * `["public", "sales"]`. On MySQL/MariaDB these are databases: each becomes its own * namespace and cross-database foreign keys are kept; without a list, only the * connection URL's database is read, under the `public` namespace. On Postgres and SQL * Server, the default is every non-system schema. */ schemas?: string[]; }; /** Discriminated union branch for `introspection` when `provider` is `"mysql"`. */ export type MysqlIntrospectionConfig = { provider: "mysql"; providerConfig?: IntrospectionProviderConfigs; /** * Default introspection output directory. * When unset/blank, AskDB uses the package default `./askdb/`. */ outputDir?: string; /** * Schemas to introspect, like `askdb introspect --schemas` (which overrides it), e.g. * `["public", "sales"]`. On MySQL/MariaDB these are databases: each becomes its own * namespace and cross-database foreign keys are kept; without a list, only the * connection URL's database is read, under the `public` namespace. On Postgres and SQL * Server, the default is every non-system schema. */ schemas?: string[]; }; /** Discriminated union branch for `introspection` when `provider` is `"sqlite"`. */ export type SqliteIntrospectionConfig = { provider: "sqlite"; providerConfig?: IntrospectionProviderConfigs; /** * Default introspection output directory. * When unset/blank, AskDB uses the package default `./askdb/`. */ outputDir?: string; /** * Schemas to introspect, like `askdb introspect --schemas` (which overrides it), e.g. * `["public", "sales"]`. On MySQL/MariaDB these are databases: each becomes its own * namespace and cross-database foreign keys are kept; without a list, only the * connection URL's database is read, under the `public` namespace. On Postgres and SQL * Server, the default is every non-system schema. */ schemas?: string[]; }; /** Discriminated union branch for `introspection` when `provider` is `"sqlserver"`. */ export type SqlServerIntrospectionConfig = { provider: "sqlserver"; providerConfig?: IntrospectionProviderConfigs; /** * Default introspection output directory. * When unset/blank, AskDB uses the package default `./askdb/`. */ outputDir?: string; /** * Schemas to introspect, like `askdb introspect --schemas` (which overrides it), e.g. * `["public", "sales"]`. On MySQL/MariaDB these are databases: each becomes its own * namespace and cross-database foreign keys are kept; without a list, only the * connection URL's database is read, under the `public` namespace. On Postgres and SQL * Server, the default is every non-system schema. */ schemas?: string[]; }; /** Discriminated union of all supported introspection provider branches. */ export type AskDbIntrospectionConfig = | PostgresIntrospectionConfig | PrismaIntrospectionConfig | MysqlIntrospectionConfig | SqliteIntrospectionConfig | SqlServerIntrospectionConfig; // --------------------------------------------------------------------------- // Root config // --------------------------------------------------------------------------- /** * Root shape for `export default defineConfig({ ... })` in `askdb.config.*`. * * - **`ai`**: provider connections (`providerConfig`) plus the `language` and `embedding` model sections. * - **`introspection`**: target engine for `askdb introspect` (postgres / prisma / mysql / sqlite / sqlserver) — `provider` picks which `providerConfig` branch is read. Each branch holds the connection URL/path for that engine. * - **`rag`** (optional, experimental): the embedder (`"mock"`, or `"ai"` for `ai.embedding`) and the vector store. Omitting it means the mock embedder and the in-memory store. * - **`logging` | `modes` | `host`**: optional operational defaults. * - **`studio`** (optional, experimental): Studio's listen address and Query Playground execution. */ export type AskDbConfig = { ai: AskDbAiConfig; introspection: AskDbIntrospectionConfig; /** * Override the NL→SQL dialect. When unset, `createAskDb` uses the provider recorded in the * schema artifact at introspection time, else `postgres`. Use this when that provider is * wrong — e.g. Prisma `schema.prisma` declares `provider = "postgresql"` but you actually * target a different engine, or the artifact came from MariaDB, which records `mysql`. * * Keep aligned with `DialectId` in `@askdb/core`. Shipped specs: see `ASKDB_DIALECTS`. */ dialect?: AskDbDialectId; /** * Retrieval (RAG): the embedder and the vector store. Optional: omit it when you don't use * retrieval, and AskDB behaves as if `{ embedder: "mock", store: "memory", storeConfig: {} }` * were set, so no store keys are written. Setting `ai.embedding` without it is an error, since * nothing would use that model. When present, `embedder`, `store` and `storeConfig` are required. * * Experimental: `rag` follows the 0.x `@askdb/rag` package, so a minor release of * `@askdb/config` may change it (https://github.com/Ygilany/AskDB/blob/main/docs/adrs/0030-1-0-promises-checked-sql-only.md). */ rag?: { /** `"mock"` (a local lexical embedder) or `"ai"` (the `ai.embedding` model). */ embedder: AskDbRagEmbedder; store: AskDbRagStore; storeConfig: { file?: FileStoreConfig; memory?: MemoryStoreConfig; pgvector?: PgvectorStoreConfig; }; }; logging?: { level?: AskDbLogLevel; correlationId?: string; /** A file to append the JSON logs to. */ logFile?: string; /** When true, also writes the JSON logs to stdout. */ logStdout?: boolean; }; modes?: { askdbMode?: AskDbModeV1; omitSensitiveFromPrompt?: boolean }; host?: { schemaPath?: string; schemaJson?: string }; /** Deterministic NL→SQL for tests / local dev: this SQL instead of a model call. */ dev?: { mockSql?: string }; /** * Studio browser server listen and query-execution defaults. * * Experimental: `studio` follows the 0.x `@askdb/studio` package, so a minor release of * `@askdb/config` may change it (https://github.com/Ygilany/AskDB/blob/main/docs/adrs/0030-1-0-promises-checked-sql-only.md). */ studio?: { listen?: { host?: string; port?: number }; /** * Query execution against a live database from the Studio playground. * Off by default — set `enabled: true` and give Studio its own connection * (ideally a read-only database role). */ execute?: { /** * Turn on `POST /api/execute` and the Playground's **Execute Query** button. * Default `false`: Studio generates SQL but never runs it. */ enabled?: boolean; /** * Reuse the introspection connection (`introspection.providerConfig.`) * when `databaseUrl` / `file` is not set. Default `false` — Studio execute needs * its own explicit connection so introspection credentials are never used to run * ad-hoc SQL by accident. */ useIntrospectionConnection?: boolean; /** * Per-query timeout in milliseconds (positive integer). Default `30000`. * Enforced server-side on Postgres, MySQL/MariaDB, and SQL Server; not enforced * for SQLite. */ timeoutMs?: number; /** * Maximum rows returned per query (positive integer). Default `500`. Studio fetches * at most `maxRows + 1` rows and reports `truncated: true` when more exist. */ maxRows?: number; /** * Explicit live-execute provider. Any other value fails to load. When omitted, Studio * falls back to the active introspection provider when it is a live engine, then * defaults to `"postgres"`. */ provider?: AskDbStudioExecuteProvider; /** Connection URL used by `POST /api/execute` for network databases. */ databaseUrl?: string; /** * SQLite file path used by `POST /api/execute` when `provider === "sqlite"`. */ file?: string; }; }; /** HTTP API server defaults (first-party `apps/http-api`). */ httpApi?: { listen?: { /** * An integer from 0 to 65535. When unset or `NaN`, servers default to `3000`. Another * value fails when the config loads. For platforms that inject `PORT`, use * `Number(env("PORT") ?? 3000)`: `env()` returns a string. */ port?: number; host?: string; }; /** * Accept a per-request `schemaJson` override on `POST /ask`. Default `false`: an override * lets any caller send arbitrary schema/prompt content through the server's model key, so * enable it only for tests or trusted multi-schema deployments. */ allowSchemaOverride?: boolean; /** * Abort the model call for a `POST /ask` request after this many milliseconds (positive * integer, at most `2147483647`, the Node timer maximum; larger values are a config error). * Default `60000`. */ requestTimeoutMs?: number; }; };