@databricks/appkit 0.59.0 → 0.61.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.
Files changed (118) hide show
  1. package/NOTICE.md +11 -10
  2. package/dist/agents/databricks.js +1 -2
  3. package/dist/agents/databricks.js.map +1 -1
  4. package/dist/agents/supervisor-api.js +1 -2
  5. package/dist/agents/supervisor-api.js.map +1 -1
  6. package/dist/appkit/package.js +1 -1
  7. package/dist/cache/index.js +2 -3
  8. package/dist/cache/index.js.map +1 -1
  9. package/dist/cli/commands/doctor/bundle.js +3 -11
  10. package/dist/cli/commands/doctor/bundle.js.map +1 -1
  11. package/dist/cli/commands/doctor/databricks-client.js +18 -22
  12. package/dist/cli/commands/doctor/databricks-client.js.map +1 -1
  13. package/dist/cli/commands/registry/add.js +410 -0
  14. package/dist/cli/commands/registry/add.js.map +1 -0
  15. package/dist/cli/commands/registry/client.js +94 -0
  16. package/dist/cli/commands/registry/client.js.map +1 -0
  17. package/dist/cli/commands/registry/config-plan.js +110 -0
  18. package/dist/cli/commands/registry/config-plan.js.map +1 -0
  19. package/dist/cli/commands/registry/config-writer.js +176 -0
  20. package/dist/cli/commands/registry/config-writer.js.map +1 -0
  21. package/dist/cli/commands/registry/constants.js +62 -0
  22. package/dist/cli/commands/registry/constants.js.map +1 -0
  23. package/dist/cli/commands/registry/env-reconcile.js +116 -0
  24. package/dist/cli/commands/registry/env-reconcile.js.map +1 -0
  25. package/dist/cli/commands/registry/env-writer.js +198 -0
  26. package/dist/cli/commands/registry/env-writer.js.map +1 -0
  27. package/dist/cli/commands/registry/index.js +25 -0
  28. package/dist/cli/commands/registry/index.js.map +1 -0
  29. package/dist/cli/commands/registry/info.js +45 -0
  30. package/dist/cli/commands/registry/info.js.map +1 -0
  31. package/dist/cli/commands/registry/list.js +130 -0
  32. package/dist/cli/commands/registry/list.js.map +1 -0
  33. package/dist/cli/commands/registry/requirements.js +92 -0
  34. package/dist/cli/commands/registry/requirements.js.map +1 -0
  35. package/dist/cli/commands/registry/server-register.js +124 -0
  36. package/dist/cli/commands/registry/server-register.js.map +1 -0
  37. package/dist/cli/commands/registry/workspace-picker.js +353 -0
  38. package/dist/cli/commands/registry/workspace-picker.js.map +1 -0
  39. package/dist/cli/deploy-config.js +36 -0
  40. package/dist/cli/deploy-config.js.map +1 -0
  41. package/dist/cli/index.js +4 -0
  42. package/dist/cli/index.js.map +1 -1
  43. package/dist/connectors/context.js +1 -2
  44. package/dist/connectors/context.js.map +1 -1
  45. package/dist/connectors/files/client.js +1 -2
  46. package/dist/connectors/files/client.js.map +1 -1
  47. package/dist/connectors/genie/client.js +1 -2
  48. package/dist/connectors/genie/client.js.map +1 -1
  49. package/dist/connectors/jobs/client.js +1 -2
  50. package/dist/connectors/jobs/client.js.map +1 -1
  51. package/dist/connectors/sql-warehouse/client.js +1 -2
  52. package/dist/connectors/sql-warehouse/client.js.map +1 -1
  53. package/dist/context/execution-context.d.ts +0 -1
  54. package/dist/context/execution-context.d.ts.map +1 -1
  55. package/dist/context/service-context.d.ts +1 -2
  56. package/dist/context/service-context.d.ts.map +1 -1
  57. package/dist/context/service-context.js +2 -3
  58. package/dist/context/service-context.js.map +1 -1
  59. package/dist/core/appkit.d.ts +1 -2
  60. package/dist/core/appkit.d.ts.map +1 -1
  61. package/dist/index.d.ts +3 -5
  62. package/dist/index.js +2 -3
  63. package/dist/plugins/agents/agents.d.ts.map +1 -1
  64. package/dist/plugins/agents/agents.js +117 -80
  65. package/dist/plugins/agents/agents.js.map +1 -1
  66. package/dist/plugins/agents/manifest.js +10 -0
  67. package/dist/plugins/agents/mlflow.js +123 -0
  68. package/dist/plugins/agents/mlflow.js.map +1 -0
  69. package/dist/plugins/agents/schemas.js +4 -2
  70. package/dist/plugins/agents/schemas.js.map +1 -1
  71. package/dist/plugins/files/plugin.js +1 -2
  72. package/dist/plugins/files/plugin.js.map +1 -1
  73. package/dist/plugins/files/types.d.ts +1 -2
  74. package/dist/plugins/files/types.d.ts.map +1 -1
  75. package/dist/plugins/jobs/types.d.ts +1 -2
  76. package/dist/plugins/jobs/types.d.ts.map +1 -1
  77. package/dist/plugins/lakebase/lakebase.js +1 -2
  78. package/dist/plugins/lakebase/lakebase.js.map +1 -1
  79. package/dist/schemas/manifest.js +4 -4
  80. package/dist/schemas/manifest.js.map +1 -1
  81. package/dist/shared/src/index.d.ts +4 -1
  82. package/dist/shared/src/schemas/manifest.d.ts +6 -6
  83. package/dist/shared/src/workspace-client/client.js +57 -0
  84. package/dist/shared/src/workspace-client/client.js.map +1 -0
  85. package/dist/shared/src/workspace-client/errors.d.ts +2 -0
  86. package/dist/shared/src/workspace-client/errors.js +3 -0
  87. package/dist/shared/src/workspace-client/factory.d.ts +19 -0
  88. package/dist/shared/src/workspace-client/factory.d.ts.map +1 -0
  89. package/dist/shared/src/workspace-client/factory.js +24 -0
  90. package/dist/shared/src/workspace-client/factory.js.map +1 -0
  91. package/dist/shared/src/workspace-client/index.d.ts +4 -0
  92. package/dist/shared/src/workspace-client/legacy.d.ts +31 -0
  93. package/dist/shared/src/workspace-client/legacy.d.ts.map +1 -0
  94. package/dist/shared/src/workspace-client/legacy.js +23 -0
  95. package/dist/shared/src/workspace-client/legacy.js.map +1 -0
  96. package/dist/shared/src/workspace-client/types.d.ts +49 -0
  97. package/dist/shared/src/workspace-client/types.d.ts.map +1 -0
  98. package/dist/type-generator/index.js +1 -2
  99. package/dist/type-generator/index.js.map +1 -1
  100. package/dist/type-generator/query-registry.js +1 -2
  101. package/dist/type-generator/query-registry.js.map +1 -1
  102. package/dist/type-generator/serving/fetcher.js +1 -2
  103. package/dist/type-generator/serving/fetcher.js.map +1 -1
  104. package/dist/type-generator/serving/generator.js +1 -2
  105. package/dist/type-generator/serving/generator.js.map +1 -1
  106. package/dist/type-generator/vite-plugin.js +1 -2
  107. package/dist/type-generator/vite-plugin.js.map +1 -1
  108. package/dist/workspace-client/index.d.ts +4 -3
  109. package/dist/workspace-client/index.js +2 -2
  110. package/dist/workspace-client/legacy.d.ts +9 -4
  111. package/dist/workspace-client/legacy.d.ts.map +1 -1
  112. package/dist/workspace-client/legacy.js +3 -3
  113. package/dist/workspace-client/legacy.js.map +1 -1
  114. package/dist/workspace-client/types.d.ts +2 -2
  115. package/docs/api/appkit/Function.createWorkspaceClient.md +4 -4
  116. package/docs/api/appkit/Interface.WorkspaceClientOptions.md +11 -0
  117. package/package.json +6 -4
  118. package/sbom.cdx.json +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"query-registry.js","names":[],"sources":["../../src/type-generator/query-registry.ts"],"sourcesContent":["import fs from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { tableFromIPC } from \"apache-arrow\";\nimport pc from \"picocolors\";\nimport { createLogger } from \"../logging/logger\";\nimport { createWorkspaceClient } from \"../workspace-client\";\nimport { CACHE_VERSION, hashSQL, loadCache, saveCache } from \"./cache\";\nimport {\n classifyBlockingFailure,\n classifyEnvironmentalCause,\n getErrorDiagnostic,\n isConnectivityError,\n} from \"./errors\";\nimport { decidePreflight, type PreflightMode } from \"./preflight\";\nimport { Spinner } from \"./spinner\";\nimport { type DescribeFormatMemo, describeAdaptive } from \"./statement-result\";\nimport {\n type DatabricksStatementExecutionResponse,\n type QueryFatalError,\n type QueryGenerationResult,\n type QuerySchema,\n type QuerySyntaxError,\n sqlTypeToHelper,\n sqlTypeToMarker,\n} from \"./types\";\nimport {\n getWarehouseState,\n startWarehouse,\n waitUntilRunning,\n} from \"./warehouse-status\";\n\nconst logger = createLogger(\"type-generator:query-registry\");\n\n/**\n * Upper bound on how long a `blocking`-mode preflight will wait for a starting\n * warehouse to reach RUNNING before giving up (~5 min). Generous enough to ride\n * out a cold start without hanging an interactive CLI invocation indefinitely.\n */\nconst PREFLIGHT_WAIT_MAX_MS = 300_000;\n\n/**\n * Regex breakdown:\n * '(?:[^']|'')*' — matches a SQL string literal, including escaped '' pairs\n * | — alternation: whichever branch matches first at a position wins\n * --[^\\n]* — matches a single-line SQL comment\n *\n * Because the regex engine scans left-to-right, a `'` is consumed as a string\n * literal before any `--` inside it could match as a comment — giving us\n * correct single-pass ordering without a manual state machine.\n *\n * V1: no block-comment support (deferred to next PR).\n */\nconst PROTECTED_RANGE_RE = /'(?:[^']|'')*'|--[^\\n]*/g;\n\n/**\n * Numeric-context patterns for positional type inference.\n * Hoisted to module scope — safe because matchAll() clones the regex internally.\n */\nconst NUMERIC_PATTERNS: RegExp[] = [\n /\\bLIMIT\\s+:([a-zA-Z_]\\w*)/gi,\n /\\bOFFSET\\s+:([a-zA-Z_]\\w*)/gi,\n /\\bTOP\\s+:([a-zA-Z_]\\w*)/gi,\n /\\bFETCH\\s+FIRST\\s+:([a-zA-Z_]\\w*)\\s+ROWS/gi,\n // V1 limitation: arithmetic operators may false-positive for date\n // expressions like `:start_date - INTERVAL '1 day'`. A smarter\n // heuristic (e.g. look-ahead for INTERVAL) is deferred to a future PR.\n /[+\\-*/]\\s*:([a-zA-Z_]\\w*)/g,\n /:([a-zA-Z_]\\w*)\\s*[+\\-*/]/g,\n];\n\nexport function getProtectedRanges(sql: string): Array<[number, number]> {\n const ranges: Array<[number, number]> = [];\n for (const m of sql.matchAll(PROTECTED_RANGE_RE)) {\n ranges.push([m.index, m.index + m[0].length]);\n }\n return ranges;\n}\n\nfunction isInsideProtectedRange(\n offset: number,\n ranges: Array<[number, number]>,\n): boolean {\n return ranges.some(([start, end]) => offset >= start && offset < end);\n}\n\n/**\n * Parse a raw API/SDK error into a structured code + message.\n * Handles Databricks-style JSON bodies embedded in the message string,\n * e.g. `Response from server (Bad Request) {\"error_code\":\"...\",\"message\":\"...\"}`.\n */\nfunction parseError(raw: string): { code?: string; message: string } {\n const jsonMatch = raw.match(/\\{[\\s\\S]*\\}/);\n if (jsonMatch) {\n try {\n const parsed = JSON.parse(jsonMatch[0]);\n if (parsed.error_code || parsed.message) {\n return {\n code: parsed.error_code,\n message: parsed.message || raw,\n };\n }\n } catch {\n // not valid JSON, fall through\n }\n }\n return { message: raw };\n}\n\n/**\n * Extract parameters from a SQL query\n * @param sql - the SQL query to extract parameters from\n * @returns an array of parameter names\n */\nexport function extractParameters(\n sql: string,\n ranges?: Array<[number, number]>,\n): string[] {\n const protectedRanges = ranges ?? getProtectedRanges(sql);\n const matches = sql.matchAll(/(?<!:):([a-zA-Z_]\\w*)/g);\n const params = new Set<string>();\n for (const match of matches) {\n if (!isInsideProtectedRange(match.index, protectedRanges)) {\n params.add(match[1]);\n }\n }\n return Array.from(params);\n}\n\n// parameters that are injected by the server\nexport const SERVER_INJECTED_PARAMS = [\"workspaceId\"];\n\n/**\n * Generates the TypeScript type literal for query parameters from SQL.\n * Shared by both the success and failure paths.\n */\nfunction formatParametersType(sql: string): string {\n const params = extractParameters(sql).filter(\n (p) => !SERVER_INJECTED_PARAMS.includes(p),\n );\n const paramTypes = extractParameterTypes(sql);\n\n return params.length > 0\n ? `{\\n ${params\n .map((p) => {\n const sqlType = paramTypes[p];\n const markerType = sqlType\n ? sqlTypeToMarker[sqlType]\n : \"SQLTypeMarker\";\n const helper = sqlType ? sqlTypeToHelper[sqlType] : \"sql.*()\";\n return `/** ${sqlType || \"any\"} - use ${helper} */\\n ${p}: ${markerType}`;\n })\n .join(\";\\n \")};\\n }`\n : \"Record<string, never>\";\n}\n\n/**\n * Decode a base64 Arrow IPC attachment from a DESCRIBE QUERY response and\n * extract column metadata. Returns the same shape as rows parsed from the\n * legacy data_array path.\n *\n * IMPORTANT: a DESCRIBE QUERY response is itself a result *table* with rows\n * shaped like `(col_name, data_type, comment)` describing the user query's\n * output schema. We must read those rows — NOT `table.schema.fields`, which\n * would describe DESCRIBE QUERY's own output (`col_name`, `data_type`,\n * `comment`) and yield bogus types for every query.\n */\nfunction columnsFromArrowAttachment(\n attachment: string,\n): Array<{ name: string; type_name: string; comment: string | undefined }> {\n const buf = Buffer.from(attachment, \"base64\");\n const table = tableFromIPC(buf);\n return table.toArray().map((row) => {\n const obj = row.toJSON() as {\n col_name?: unknown;\n data_type?: unknown;\n comment?: unknown;\n };\n return {\n name: typeof obj.col_name === \"string\" ? obj.col_name : \"\",\n type_name:\n typeof obj.data_type === \"string\"\n ? obj.data_type.toUpperCase()\n : \"STRING\",\n comment:\n typeof obj.comment === \"string\" && obj.comment !== \"\"\n ? obj.comment\n : undefined,\n };\n });\n}\n\nexport function convertToQueryType(\n result: DatabricksStatementExecutionResponse,\n sql: string,\n queryName: string,\n): { type: string; hasResults: boolean } {\n const dataRows = result.result?.data_array || [];\n let columns = dataRows.map((row) => ({\n name: row[0] || \"\",\n type_name: row[1]?.toUpperCase() || \"STRING\",\n comment: row[2] || undefined,\n }));\n\n // Fallback: serverless warehouses return ARROW_STREAM format with an inline\n // base64 attachment instead of data_array. Decode the Arrow IPC rows (the\n // DESCRIBE QUERY result table) to extract column names and types.\n if (columns.length === 0 && result.result?.attachment) {\n logger.debug(\"data_array empty, decoding Arrow IPC attachment for schema\");\n try {\n columns = columnsFromArrowAttachment(result.result.attachment);\n } catch (err) {\n logger.warn(\n \"Failed to decode Arrow IPC attachment: %s\",\n err instanceof Error ? err.message : String(err),\n );\n }\n }\n\n const paramsType = formatParametersType(sql);\n\n // generate result fields with JSDoc\n const resultFields = columns.map((column) => {\n const normalizedType = normalizeTypeName(column.type_name);\n const mappedType = typeMap[normalizedType] || \"unknown\";\n // validate column name is a valid identifier\n const name = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(column.name)\n ? column.name\n : `\"${column.name}\"`;\n\n // generate comment for column\n const comment = column.comment\n ? `/** ${column.comment.replace(/\\*\\//g, \"* /\")} */\\n `\n : `/** @sqlType ${column.type_name.replace(/\\*\\//g, \"* /\")} */\\n `;\n\n return `${comment}${name}: ${mappedType}`;\n });\n\n const hasResults = resultFields.length > 0;\n\n const type = `{\n name: \"${queryName}\";\n parameters: ${paramsType};\n result: ${\n hasResults\n ? `Array<{\n ${resultFields.join(\";\\n \")};\n }>`\n : \"unknown\"\n };\n }`;\n\n return { type, hasResults };\n}\n\n/**\n * Used when DESCRIBE QUERY fails so the query still appears in QueryRegistry.\n * Generates a type with unknown result from SQL alone (no warehouse call).\n */\nfunction generateUnknownResultQuery(sql: string, queryName: string): string {\n const paramsType = formatParametersType(sql);\n\n return `{\n name: \"${queryName}\";\n parameters: ${paramsType};\n result: unknown;\n }`;\n}\n\n/**\n * Degrade gracefully when DESCRIBE can't produce a fresh schema (transient\n * connectivity outage, or a warehouse that's reachable but not ready). Reuse\n * the last-good cached type when the SQL hash is unchanged, otherwise emit\n * `unknown` from SQL alone. Never persists `result: unknown`.\n */\nfunction degradedType(\n cache: Awaited<ReturnType<typeof loadCache>>,\n queryName: string,\n sql: string,\n sqlHash: string,\n): Pick<QuerySchema, \"type\" | \"degraded\"> {\n const prior = cache.queries[queryName];\n const canReusePrior = prior?.hash === sqlHash && !prior.retry;\n return canReusePrior\n ? { type: prior.type }\n : {\n type: generateUnknownResultQuery(sql, queryName),\n degraded: true,\n };\n}\n\n// Single source of truth for the `@param` type alternation, shared by\n// extractParameterTypes and extractParameterDefaults so the two can't drift.\n// Alternation order matters: TIMESTAMP_NTZ must precede TIMESTAMP so the regex\n// engine doesn't greedy-match TIMESTAMP and leave `_NTZ` unconsumed.\nconst PARAM_TYPE_ALTERNATION =\n \"STRING|NUMERIC|DECIMAL|BIGINT|TINYINT|SMALLINT|INT|FLOAT|DOUBLE|BOOLEAN|DATE|TIMESTAMP_NTZ|TIMESTAMP|BINARY\";\n\nexport function extractParameterTypes(sql: string): Record<string, string> {\n const paramTypes: Record<string, string> = {};\n const regex = new RegExp(\n `--\\\\s*@param\\\\s+(\\\\w+)\\\\s+(${PARAM_TYPE_ALTERNATION})\\\\b`,\n \"gi\",\n );\n const matches = sql.matchAll(regex);\n for (const match of matches) {\n const [, paramName, paramType] = match;\n paramTypes[paramName] = paramType.toUpperCase();\n }\n\n return paramTypes;\n}\n\nexport function defaultForType(sqlType: string | undefined): string {\n switch (sqlType?.toUpperCase()) {\n case \"NUMERIC\":\n case \"DECIMAL\":\n case \"BIGINT\":\n case \"TINYINT\":\n case \"SMALLINT\":\n case \"INT\":\n return \"0\";\n case \"FLOAT\":\n case \"DOUBLE\":\n return \"0.0\";\n case \"STRING\":\n return \"''\";\n case \"BOOLEAN\":\n return \"true\";\n case \"DATE\":\n return \"'2000-01-01'\";\n case \"TIMESTAMP\":\n return \"'2000-01-01T00:00:00Z'\";\n case \"TIMESTAMP_NTZ\":\n return \"'2000-01-01T00:00:00'\";\n case \"BINARY\":\n return \"X'00'\";\n default:\n return \"''\";\n }\n}\n\n/**\n * True when `raw` is already a single, well-formed SQL single-quoted string\n * literal — i.e. it opens and closes with `'` and every interior quote is part\n * of an escaped `''` pair. `'2024-01-01'` and `'O''Brien'` qualify;\n * `'a' OR 1=1 OR 'b'` does not (it has lone interior quotes), so it is treated\n * as raw content and re-escaped rather than trusted. A backslash also\n * disqualifies it: Databricks/Spark treats `\\` as an escape inside literals (so\n * `'a\\'` is unterminated), and we never trust such input — it is re-escaped.\n */\nfunction isWellFormedStringLiteral(raw: string): boolean {\n if (raw.length < 2 || !raw.startsWith(\"'\") || !raw.endsWith(\"'\")) {\n return false;\n }\n const inner = raw.slice(1, -1);\n if (inner.includes(\"\\\\\")) return false;\n return !inner.replace(/''/g, \"\").includes(\"'\");\n}\n\n/**\n * Format a user-supplied sample value as a SQL literal for substitution into\n * the build-time DESCRIBE statement. Returns `null` when the value isn't valid\n * for its type, so the caller falls back to the safe type-based placeholder\n * instead of substituting attacker-controllable text.\n *\n * The value comes from a `.sql` file that may be shared via a template or\n * dependency, so it must not be able to inject SQL into `DESCRIBE QUERY`:\n * - string-like types are always emitted as one well-formed, fully-escaped\n * single-quoted literal (a pre-quoted literal is kept as-is, anything else is\n * quoted with both `\\` and `'` doubled so neither can terminate the literal),\n * so the value can never break out of the string;\n * - numeric / boolean / binary values must match a strict literal shape and are\n * rejected (`null`) otherwise, rather than being passed through verbatim.\n */\nfunction formatSampleValue(\n sqlType: string | undefined,\n raw: string,\n): string | null {\n switch (sqlType?.toUpperCase()) {\n case \"STRING\":\n case \"DATE\":\n case \"TIMESTAMP\":\n case \"TIMESTAMP_NTZ\":\n return isWellFormedStringLiteral(raw)\n ? raw\n : `'${raw.replace(/\\\\/g, \"\\\\\\\\\").replace(/'/g, \"''\")}'`;\n case \"NUMERIC\":\n case \"DECIMAL\":\n case \"BIGINT\":\n case \"TINYINT\":\n case \"SMALLINT\":\n case \"INT\":\n case \"FLOAT\":\n case \"DOUBLE\":\n return /^[+-]?\\d+(\\.\\d+)?$/.test(raw) ? raw : null;\n case \"BOOLEAN\":\n return /^(?:true|false)$/i.test(raw) ? raw.toLowerCase() : null;\n case \"BINARY\":\n return /^X'[0-9a-fA-F]*'$/i.test(raw) ? raw : null;\n default:\n return null;\n }\n}\n\n/**\n * Parse optional describe-time sample values from `@param` annotations, e.g.\n * `-- @param target_catalog STRING = main`. The value is substituted into the\n * SQL **only during DESCRIBE QUERY** so type generation can resolve queries\n * whose shape depends on a parameter value — most notably dynamic table names\n * via `IDENTIFIER(:target_catalog || '.schema.table')`, where the empty-string\n * default would otherwise produce malformed SQL. Runtime binding is unaffected:\n * the analytics plugin still binds the real parameter at execution time, so the\n * query stays portable across environments.\n *\n * Returns a map of parameter name to the formatted SQL literal to substitute.\n */\nexport function extractParameterDefaults(sql: string): Record<string, string> {\n const defaults: Record<string, string> = {};\n // Reuses PARAM_TYPE_ALTERNATION, then requires `= <value>` on the same line.\n // All inter-token whitespace is horizontal-only (`[^\\S\\r\\n]`, not `\\s`): `\\s`\n // matches newlines, so a value-less line like `-- @param x STRING =` would let\n // `\\s*=\\s*(.+?)` swallow the *next* line as the sample value. Restricting to\n // same-line whitespace makes such a line simply not match, so it correctly\n // falls back to the type placeholder.\n const regex = new RegExp(\n `--[^\\\\S\\\\r\\\\n]*@param[^\\\\S\\\\r\\\\n]+(\\\\w+)[^\\\\S\\\\r\\\\n]+(${PARAM_TYPE_ALTERNATION})[^\\\\S\\\\r\\\\n]*=[^\\\\S\\\\r\\\\n]*(.+?)[^\\\\S\\\\r\\\\n]*$`,\n \"gim\",\n );\n for (const match of sql.matchAll(regex)) {\n const [, paramName, paramType, rawValue] = match;\n const formatted = formatSampleValue(paramType, rawValue);\n // A value that fails type validation is dropped, not substituted: the param\n // then falls back to the safe type-based placeholder during DESCRIBE.\n if (formatted !== null) {\n defaults[paramName] = formatted;\n }\n }\n return defaults;\n}\n\n/**\n * Replace `:param` placeholders with describe-time literals so `DESCRIBE QUERY`\n * can run without bound parameters. Resolution order per parameter:\n * 1. An explicit `-- @param name TYPE = value` sample value (wins), which lets\n * dynamic table names via `IDENTIFIER(...)` resolve to a real table.\n * 2. Otherwise a placeholder default derived from the annotated/inferred type.\n * Placeholders inside string literals or comments are left untouched.\n */\nexport function substituteParametersForDescribe(sql: string): string {\n const protectedRanges = getProtectedRanges(sql);\n const annotatedTypes = extractParameterTypes(sql);\n const inferredTypes = inferParameterTypes(sql, protectedRanges);\n const parameterTypes = { ...inferredTypes, ...annotatedTypes };\n const parameterDefaults = extractParameterDefaults(sql);\n return sql.replace(\n /(?<!:):([a-zA-Z_]\\w*)/g,\n (original, paramName, offset) => {\n if (isInsideProtectedRange(offset, protectedRanges)) {\n return original;\n }\n const sampleValue = parameterDefaults[paramName];\n if (sampleValue !== undefined) {\n return sampleValue;\n }\n return defaultForType(parameterTypes[paramName]);\n },\n );\n}\n\n/**\n * Append a remediation hint when a DESCRIBE failure looks like a dynamic\n * identifier that couldn't be resolved: the query calls `IDENTIFIER(...)` and\n * has at least one parameter without a describe-time sample value. These fail\n * because typegen substitutes a placeholder default (e.g. `''`) that yields a\n * malformed or non-existent table name. Steering the user to the `= value`\n * annotation turns the fatal error into a one-line fix.\n */\nfunction withIdentifierHint(\n error: { code?: string; message: string },\n sql: string,\n): { code?: string; message: string } {\n if (!/\\bIDENTIFIER\\s*\\(/i.test(sql)) {\n return error;\n }\n const protectedRanges = getProtectedRanges(sql);\n const params = extractParameters(sql, protectedRanges);\n const defaults = extractParameterDefaults(sql);\n const unresolved = params.filter(\n (p) => !SERVER_INJECTED_PARAMS.includes(p) && defaults[p] === undefined,\n );\n if (unresolved.length === 0) {\n return error;\n }\n const example = unresolved[0];\n return {\n ...error,\n message: `${error.message}\\n Hint: this query uses IDENTIFIER() with parameter(s) ${unresolved\n .map((p) => `:${p}`)\n .join(\n \", \",\n )}. Give type generation a sample value so it can resolve the table, e.g. \\`-- @param ${example} STRING = my_catalog\\`. The runtime query still binds the real parameter.`,\n };\n}\n\n/**\n * Infer parameter types from positional context in SQL.\n * V1 only infers NUMERIC from patterns like LIMIT, OFFSET, TOP,\n * FETCH FIRST ... ROWS, and arithmetic operators.\n * Parameters inside string literals or SQL comments are ignored.\n */\nexport function inferParameterTypes(\n sql: string,\n ranges?: Array<[number, number]>,\n): Record<string, string> {\n const inferred: Record<string, string> = {};\n const protectedRanges = ranges ?? getProtectedRanges(sql);\n\n for (const pattern of NUMERIC_PATTERNS) {\n for (const match of sql.matchAll(pattern)) {\n if (!isInsideProtectedRange(match.index, protectedRanges)) {\n inferred[match[1]] = \"NUMERIC\";\n }\n }\n }\n\n return inferred;\n}\n\n/**\n * Generate query schemas from a folder of SQL files\n * It uses DESCRIBE QUERY to get the schema without executing the query\n * @param queryFolder - the folder containing the SQL files\n * @param warehouseId - the warehouse id to use for schema analysis\n * @param options - options for the query generation\n * @param options.noCache - if true, skip the cache and regenerate all types\n * @param options.mode - preflight policy: \"non-blocking\" never probes the\n * warehouse and never describes (emits cached/`unknown` types and returns\n * immediately), \"blocking\" waits for a starting warehouse and starts (then\n * waits for) a stopped one, treating only a deleted/deleting warehouse as\n * fatal. Defaults to \"non-blocking\".\n * @returns an array of query schemas\n */\nexport async function generateQueriesFromDescribe(\n queryFolder: string,\n warehouseId: string,\n options: {\n noCache?: boolean;\n concurrency?: number;\n mode?: PreflightMode;\n } = {},\n): Promise<QueryGenerationResult> {\n const {\n noCache = false,\n concurrency: rawConcurrency = 10,\n mode = \"non-blocking\",\n } = options;\n const concurrency =\n typeof rawConcurrency === \"number\" && Number.isFinite(rawConcurrency)\n ? Math.max(1, Math.floor(rawConcurrency))\n : 10;\n\n // read all query files and cache in parallel\n const [allFiles, cache] = await Promise.all([\n fs.readdir(queryFolder),\n noCache\n ? ({ version: CACHE_VERSION, queries: {} } as Awaited<\n ReturnType<typeof loadCache>\n >)\n : loadCache(),\n ]);\n\n const queryFiles = allFiles.filter((file) => file.endsWith(\".sql\"));\n logger.debug(\"Found %d SQL queries\", queryFiles.length);\n\n const client = createWorkspaceClient();\n const spinner = new Spinner();\n\n // Read all SQL files in parallel\n const sqlContents = await Promise.all(\n queryFiles.map((file) => fs.readFile(path.join(queryFolder, file), \"utf8\")),\n );\n\n const startTime = performance.now();\n\n // Phase 1: Check cache, separate cached vs uncached\n const cachedResults: Array<{ index: number; schema: QuerySchema }> = [];\n const uncachedQueries: Array<{\n index: number;\n queryName: string;\n sql: string;\n sqlHash: string;\n cleanedSql: string;\n }> = [];\n const logEntries: Array<{\n queryName: string;\n status: \"HIT\" | \"MISS\";\n // Absent for clean hits/misses. \"syntax\" = bad SQL on a reachable warehouse;\n // \"connectivity\" = warehouse unreachable; \"empty\" = described but no columns;\n // \"fatal\" = non-SQL setup/request failure surfaced after .d.ts emission.\n kind?: \"syntax\" | \"connectivity\" | \"empty\" | \"fatal\";\n error?: { code?: string; message: string };\n }> = [];\n\n for (let i = 0; i < queryFiles.length; i++) {\n const file = queryFiles[i];\n const rawName = path.basename(file, \".sql\");\n const queryName = normalizeQueryName(rawName);\n\n const sql = sqlContents[i];\n const sqlHash = hashSQL(sql);\n\n const cached = cache.queries[queryName];\n if (cached && cached.hash === sqlHash && !cached.retry) {\n cachedResults.push({\n index: i,\n schema: { name: queryName, type: cached.type },\n });\n logEntries.push({ queryName, status: \"HIT\" });\n } else {\n const protectedRanges = getProtectedRanges(sql);\n const annotatedTypes = extractParameterTypes(sql);\n const inferredTypes = inferParameterTypes(sql, protectedRanges);\n const parameterTypes = { ...inferredTypes, ...annotatedTypes };\n // substituteParametersForDescribe applies `-- @param name TYPE = value`\n // sample values (e.g. for IDENTIFIER table names) ahead of type defaults;\n // parameterDefaults is recomputed here only to skip them in the warn loop.\n const parameterDefaults = extractParameterDefaults(sql);\n const sqlWithDefaults = substituteParametersForDescribe(sql);\n\n // Warn about unresolved parameters\n const allParams = extractParameters(sql, protectedRanges);\n for (const param of allParams) {\n if (SERVER_INJECTED_PARAMS.includes(param)) continue;\n if (parameterDefaults[param]) continue;\n if (parameterTypes[param]) continue;\n logger.warn(\n '%s: parameter \":%s\" has no type annotation or inference. Add %s to the query file.',\n queryFiles[i],\n param,\n `-- @param ${param} <TYPE>`,\n );\n }\n\n const cleanedSql = sqlWithDefaults.trim().replace(/;\\s*$/, \"\");\n uncachedQueries.push({ index: i, queryName, sql, sqlHash, cleanedSql });\n }\n }\n\n // Phase 2: Execute all uncached DESCRIBE calls in parallel\n type DescribeResult =\n | {\n // Described successfully with a result schema — the only case we cache.\n status: \"ok\";\n index: number;\n schema: QuerySchema;\n cacheEntry: { hash: string; type: string; retry: boolean };\n }\n | {\n // Reachable warehouse ran DESCRIBE and rejected the statement — a\n // genuine SQL error. Eligible to fail the build; never cached.\n status: \"syntax\";\n index: number;\n schema: QuerySchema;\n error: { code?: string; message: string };\n }\n | {\n // DESCRIBE succeeded but returned no columns — soft `unknown`. Not a\n // failure, not cached, retried next run.\n status: \"empty\";\n index: number;\n schema: QuerySchema;\n }\n | {\n // Warehouse reachable but returned a non-terminal state (PENDING/\n // RUNNING) with no rows — stopped/cold-starting/busy. Degrade like a\n // transient outage (reuse cache or `unknown`); not cached, not empty.\n status: \"unavailable\";\n index: number;\n schema: QuerySchema;\n };\n\n const freshResults: Array<{ index: number; schema: QuerySchema }> = [];\n // Genuine SQL errors (reachable warehouse). Connectivity failures are NOT\n // recorded here — they degrade silently so a transient outage isn't fatal.\n const syntaxErrors: QuerySyntaxError[] = [];\n // Deterministic fatal errors only (404/400). Environmental failures are\n // tracked separately below.\n const fatalErrors: QueryFatalError[] = [];\n // Track whether an environmental failure occurred in blocking mode (for the\n // has-types gate in generateFromEntryPoint), plus its coarse cause so the\n // gate's warning can say why generation fell back to committed types.\n let hadEnvironmentalFailure = false;\n let environmentalCause: \"auth\" | \"unreachable\" | \"unavailable\" | undefined;\n\n if (uncachedQueries.length > 0) {\n // One-time warehouse preflight (before issuing any DESCRIBE). A single\n // warehouses.get classifies the warehouse so we can skip the whole describe\n // batch when it can't serve this run, instead of letting every query fail\n // (and re-fail next run). Reuses this file's degrade/classify helpers so a\n // not-ready warehouse degrades exactly like a per-query outage.\n let decision: ReturnType<typeof decidePreflight> = \"proceed\";\n let fatalMessage = \"\";\n // Track whether an environmental failure occurred so the caller's has-types\n // gate can decide crash-vs-fall-back.\n let isEnvironmental = false;\n if (mode === \"non-blocking\") {\n // `non-blocking` never describes and must make ZERO warehouse round-trips:\n // skip the probe entirely (no getWarehouseState) and go straight to\n // degradeAll. A foreground/one-shot run can't describe in the background,\n // so it emits best-available types (reused cache or `unknown`) and returns\n // now.\n decision = \"degradeAll\";\n } else {\n try {\n const state = await getWarehouseState(client, warehouseId);\n decision = decidePreflight(state, mode);\n if (decision === \"fatal\") {\n // DELETED/DELETING is state-based and environmental.\n fatalMessage = `warehouse ${warehouseId} is ${state}`;\n isEnvironmental = true;\n environmentalCause = \"unavailable\";\n }\n if (decision === \"startWaitProceed\") {\n // Stopped/stopping warehouse: nudge it out of the stopped state, then\n // poll to RUNNING. treatStoppedAsTransient rides out the stale\n // pre-start STOPPED/STOPPING reading the start hasn't propagated past\n // yet — only DELETED/DELETING (or the deadline) ends the wait early.\n await startWarehouse(client, warehouseId);\n const final = await waitUntilRunning(client, warehouseId, {\n maxMs: PREFLIGHT_WAIT_MAX_MS,\n treatStoppedAsTransient: true,\n });\n if (final === \"RUNNING\") {\n decision = \"proceed\";\n } else {\n decision = \"fatal\";\n fatalMessage = `warehouse ${warehouseId} did not reach RUNNING (now ${final})`;\n isEnvironmental = true; // DELETED/DELETING or timeout is environmental\n environmentalCause = \"unavailable\";\n }\n }\n if (decision === \"waitThenProceed\") {\n const final = await waitUntilRunning(client, warehouseId, {\n maxMs: PREFLIGHT_WAIT_MAX_MS,\n });\n if (final === \"RUNNING\") {\n decision = \"proceed\";\n } else {\n decision = \"fatal\";\n fatalMessage = `warehouse ${warehouseId} did not reach RUNNING (now ${final})`;\n isEnvironmental = true; // DELETED/DELETING or timeout is environmental\n environmentalCause = \"unavailable\";\n }\n }\n } catch (err) {\n if (isConnectivityError(err)) {\n // Warehouse unreachable (transient outage): degrade rather than fail —\n // never fail a build on a blip. Still environmental, so the caller's\n // has-types gate decides warn-and-fall-back (committed types present)\n // vs crash (fresh checkout with nothing to fall back to).\n decision = \"degradeAll\";\n isEnvironmental = true;\n environmentalCause = \"unreachable\";\n } else {\n const classification = classifyBlockingFailure(err);\n if (classification === \"deterministic\") {\n decision = \"fatal\";\n fatalMessage = `warehouse ${warehouseId}: ${getErrorDiagnostic(err)}`;\n } else {\n decision = \"degradeAll\";\n isEnvironmental = true;\n environmentalCause = classifyEnvironmentalCause(err);\n }\n }\n }\n }\n\n if (mode === \"blocking\" && decision !== \"proceed\" && isEnvironmental) {\n hadEnvironmentalFailure = true;\n }\n\n if (decision !== \"proceed\") {\n // degradeAll or fatal: skip DESCRIBE entirely. Every uncached query gets a\n // degraded schema (reused cache or `unknown`); fatal additionally records\n // a fatalError per query so the caller fails the build after writing. Only\n // deterministic fatals are recorded; environmental degradations go silent\n // so the has-types gate can decide.\n const kind =\n decision === \"fatal\" && !isEnvironmental ? \"fatal\" : \"connectivity\";\n for (const { index, queryName, sql, sqlHash } of uncachedQueries) {\n freshResults.push({\n index,\n schema: {\n name: queryName,\n ...degradedType(cache, queryName, sql, sqlHash),\n },\n });\n if (decision === \"fatal\" && !isEnvironmental) {\n // Only deterministic fatals record an error; environmental failures\n // degrade silently for the has-types gate.\n fatalErrors.push({ name: queryName, message: fatalMessage });\n logEntries.push({\n queryName,\n status: \"MISS\",\n kind,\n error: { message: fatalMessage },\n });\n } else {\n logEntries.push({ queryName, status: \"MISS\", kind });\n }\n }\n } else {\n let completed = 0;\n const total = uncachedQueries.length;\n spinner.start(\n `Describing ${total} ${total === 1 ? \"query\" : \"queries\"} (0/${total})`,\n );\n\n // Shared across this run's DESCRIBE QUERY calls: discover the warehouse's\n // result format once, then reuse it.\n const describeFormat: DescribeFormatMemo = {};\n\n const describeOne = async ({\n index,\n queryName,\n sql,\n sqlHash,\n cleanedSql,\n }: (typeof uncachedQueries)[number]): Promise<DescribeResult> => {\n // describeAdaptive negotiates the result format with the warehouse\n // (JSON_ARRAY for standard DBSQL, ARROW_STREAM for Reyden) and returns\n // an already-normalized response, so the state classification below\n // reads it directly.\n const result = await describeAdaptive(\n client,\n `DESCRIBE QUERY ${cleanedSql}`,\n warehouseId,\n describeFormat,\n );\n\n completed++;\n spinner.update(\n `Describing ${total} ${total === 1 ? \"query\" : \"queries\"} (${completed}/${total})`,\n );\n\n logger.debug(\n \"DESCRIBE result for %s: state=%s, rows=%d, hasAttachment=%s\",\n queryName,\n result.status.state,\n result.result?.data_array?.length ?? 0,\n !!result.result?.attachment,\n );\n\n if (result.status.state === \"FAILED\") {\n // The warehouse was reachable and ran DESCRIBE, but the statement\n // failed — a genuine SQL error (bad table, syntax, incompatible type).\n const sqlError =\n result.status.error?.message || \"Query execution failed\";\n // The failure is surfaced once, formatted, by the aggregated\n // TypegenSyntaxError (and the summary table) — don't also log the raw\n // message here or every SQL error prints twice in dev.\n const type = generateUnknownResultQuery(sql, queryName);\n return {\n status: \"syntax\",\n index,\n schema: { name: queryName, type, degraded: true },\n error: withIdentifierHint(parseError(sqlError), sql),\n };\n }\n\n if (result.status.state !== \"SUCCEEDED\") {\n // Non-terminal state (PENDING/RUNNING) with no result rows: the\n // warehouse is reachable but not ready (stopped, cold-starting, or\n // busy). Degrade like a transient outage — reuse the last-good cached\n // type when the SQL is unchanged, else emit `unknown`. Never \"empty\":\n // treating this as empty would emit `result: unknown` AND discard the\n // good cached type, silently throwing away working types.\n return {\n status: \"unavailable\",\n index,\n schema: {\n name: queryName,\n ...degradedType(cache, queryName, sql, sqlHash),\n },\n };\n }\n\n const { type, hasResults } = convertToQueryType(result, sql, queryName);\n if (!hasResults) {\n // Described, but no result columns. Emit `unknown` and retry next run;\n // do not cache (we never persist `result: unknown`).\n return {\n status: \"empty\",\n index,\n schema: { name: queryName, type, degraded: true },\n };\n }\n return {\n status: \"ok\",\n index,\n schema: { name: queryName, type },\n cacheEntry: { hash: sqlHash, type, retry: false },\n };\n };\n\n // Process in chunks, saving cache after each chunk\n const processBatchResults = (\n settled: PromiseSettledResult<DescribeResult>[],\n batchOffset: number,\n ) => {\n for (let i = 0; i < settled.length; i++) {\n const entry = settled[i];\n const { queryName } = uncachedQueries[batchOffset + i];\n\n if (entry.status === \"fulfilled\") {\n const res = entry.value;\n freshResults.push({ index: res.index, schema: res.schema });\n\n if (res.status === \"ok\") {\n // Only a successful describe with a result schema is cached.\n cache.queries[queryName] = res.cacheEntry;\n logEntries.push({ queryName, status: \"MISS\" });\n } else if (res.status === \"syntax\") {\n // Genuine SQL error — record it for the caller's prod/dev gate.\n // Not cached: re-described next run so a fixed query recovers.\n syntaxErrors.push({\n name: queryName,\n message: res.error.message,\n });\n logEntries.push({\n queryName,\n status: \"MISS\",\n kind: \"syntax\",\n error: res.error,\n });\n } else if (res.status === \"empty\") {\n // status === \"empty\": described, no columns. Soft unknown, not cached.\n logEntries.push({ queryName, status: \"MISS\", kind: \"empty\" });\n } else {\n // status === \"unavailable\": non-terminal DESCRIBE (warehouse\n // stopped/cold-starting/busy). Degrade like a transient outage:\n // tag OFFLINE, count as degraded, never cache.\n if (mode === \"blocking\") {\n hadEnvironmentalFailure = true;\n environmentalCause = environmentalCause ?? \"unavailable\";\n }\n logEntries.push({\n queryName,\n status: \"MISS\",\n kind: \"connectivity\",\n });\n }\n } else {\n // executeStatement rejected without a normal StatementExecution result.\n // Only structured transport/connectivity failures are treated as\n // offline; auth, bad warehouse IDs, malformed requests, and SDK/config\n // failures stay fatal so users fix the underlying setup issue.\n completed++;\n spinner.update(\n `Describing ${total} ${total === 1 ? \"query\" : \"queries\"} (${completed}/${total})`,\n );\n\n const { sql, sqlHash, index } = uncachedQueries[batchOffset + i];\n const reason = getErrorDiagnostic(entry.reason);\n const error = parseError(reason);\n const priorEntry = cache.queries[queryName];\n const canReusePrior =\n priorEntry?.hash === sqlHash && !priorEntry.retry;\n const degraded = degradedType(cache, queryName, sql, sqlHash);\n freshResults.push({\n index,\n schema: { name: queryName, ...degraded },\n });\n\n if (!isConnectivityError(entry.reason)) {\n fatalErrors.push({ name: queryName, message: error.message });\n logEntries.push({\n queryName,\n status: \"MISS\",\n kind: \"fatal\",\n error,\n });\n continue;\n }\n\n // Environmental for the same reason as the preflight connectivity\n // branch above, so the has-types gate still sees it.\n if (mode === \"blocking\") {\n hadEnvironmentalFailure = true;\n environmentalCause = environmentalCause ?? \"unreachable\";\n }\n\n logger.warn(\n \"DESCRIBE unreachable for %s: %s — %s\",\n queryName,\n reason,\n canReusePrior\n ? \"reusing last cached type\"\n : \"emitting unknown (no matching cache)\",\n );\n logEntries.push({\n queryName,\n status: \"MISS\",\n kind: \"connectivity\",\n error,\n });\n }\n }\n };\n\n if (uncachedQueries.length > concurrency) {\n for (let b = 0; b < uncachedQueries.length; b += concurrency) {\n const batch = uncachedQueries.slice(b, b + concurrency);\n const batchResults = await Promise.allSettled(batch.map(describeOne));\n processBatchResults(batchResults, b);\n await saveCache(cache);\n }\n } else {\n const settled = await Promise.allSettled(\n uncachedQueries.map(describeOne),\n );\n processBatchResults(settled, 0);\n await saveCache(cache);\n }\n\n spinner.stop(\"\");\n }\n }\n\n const elapsed = ((performance.now() - startTime) / 1000).toFixed(2);\n\n // Print formatted table\n if (logEntries.length > 0) {\n const maxNameLen = Math.max(...logEntries.map((e) => e.queryName.length));\n const separator = pc.dim(\"─\".repeat(50));\n console.log(\"\");\n console.log(\n ` ${pc.bold(\"Typegen Queries\")} ${pc.dim(`(${logEntries.length})`)}`,\n );\n console.log(` ${separator}`);\n for (const entry of logEntries) {\n let tag: string;\n switch (entry.kind) {\n case \"syntax\":\n tag = pc.bold(pc.red(\"SQL ERR\"));\n break;\n case \"connectivity\":\n tag = pc.bold(pc.yellow(\"OFFLINE\"));\n break;\n case \"empty\":\n tag = pc.dim(\"EMPTY \");\n break;\n case \"fatal\":\n tag = pc.bold(pc.red(\"FATAL \"));\n break;\n default:\n tag =\n entry.status === \"HIT\"\n ? `cache ${pc.bold(pc.green(\"HIT \"))}`\n : `cache ${pc.bold(pc.yellow(\"MISS \"))}`;\n }\n const rawName = entry.queryName.padEnd(maxNameLen);\n // Only genuine SQL errors are struck through. Connectivity/empty kept a\n // usable type (reused or unknown), so they read as degraded, not broken.\n const name =\n entry.kind === \"syntax\" || entry.kind === \"fatal\"\n ? pc.dim(pc.strikethrough(rawName))\n : rawName;\n const errorCode = entry.error?.message.match(/\\[([^\\]]+)\\]/)?.[1];\n const reason = errorCode ? ` ${pc.dim(errorCode)}` : \"\";\n console.log(` ${tag} ${name}${reason}`);\n }\n const newCount = logEntries.filter(\n (e) => e.status === \"MISS\" && !e.kind,\n ).length;\n const cacheCount = logEntries.filter((e) => e.status === \"HIT\").length;\n const syntaxCount = logEntries.filter((e) => e.kind === \"syntax\").length;\n const offlineCount = logEntries.filter(\n (e) => e.kind === \"connectivity\",\n ).length;\n const emptyCount = logEntries.filter((e) => e.kind === \"empty\").length;\n const fatalCount = logEntries.filter((e) => e.kind === \"fatal\").length;\n console.log(` ${separator}`);\n const parts = [`${newCount} new`, `${cacheCount} from cache`];\n if (syntaxCount > 0)\n parts.push(\n `${syntaxCount} SQL ${syntaxCount === 1 ? \"error\" : \"errors\"}`,\n );\n if (offlineCount > 0) parts.push(`${offlineCount} degraded`);\n if (emptyCount > 0) parts.push(`${emptyCount} empty`);\n if (fatalCount > 0)\n parts.push(\n `${fatalCount} fatal ${fatalCount === 1 ? \"error\" : \"errors\"}`,\n );\n console.log(` ${parts.join(\", \")}. ${pc.dim(`${elapsed}s`)}`);\n console.log(\"\");\n }\n\n // Merge and sort by original file index for deterministic output\n const schemas = [...cachedResults, ...freshResults]\n .sort((a, b) => a.index - b.index)\n .map((r) => r.schema);\n\n return {\n schemas,\n syntaxErrors,\n fatalErrors,\n hadEnvironmentalFailure,\n environmentalCause: hadEnvironmentalFailure\n ? environmentalCause\n : undefined,\n };\n}\n\n/**\n * Normalize query name by removing the .obo extension\n * @param queryName - the query name to normalize\n * @returns the normalized query name\n */\nfunction normalizeQueryName(fileName: string): string {\n return fileName.replace(/\\.obo$/, \"\");\n}\n\n/**\n * Normalize SQL type name by removing parameters/generics\n * Examples:\n * DECIMAL(38,6) -> DECIMAL\n * ARRAY<STRING> -> ARRAY\n * MAP<STRING,INT> -> MAP\n * STRUCT<name:STRING> -> STRUCT\n * INTERVAL DAY TO SECOND -> INTERVAL\n * GEOGRAPHY(4326) -> GEOGRAPHY\n */\nexport function normalizeTypeName(typeName: string): string {\n return typeName\n .replace(/\\(.*\\)$/, \"\") // remove (p, s) eg: DECIMAL(38,6) -> DECIMAL\n .replace(/<.*>$/, \"\") // remove <T> eg: ARRAY<STRING> -> ARRAY\n .split(\" \")[0]; // take first word eg: INTERVAL DAY TO SECOND -> INTERVAL\n}\n\n/** Type Map for Databricks data types to JavaScript types */\nconst typeMap: Record<string, string> = {\n // string types\n STRING: \"string\",\n BINARY: \"string\",\n // boolean\n BOOLEAN: \"boolean\",\n // numeric types\n TINYINT: \"number\",\n SMALLINT: \"number\",\n INT: \"number\",\n BIGINT: \"number\",\n FLOAT: \"number\",\n DOUBLE: \"number\",\n DECIMAL: \"number\",\n // date/time types\n DATE: \"string\",\n TIMESTAMP: \"string\",\n TIMESTAMP_NTZ: \"string\",\n INTERVAL: \"string\",\n // complex types\n ARRAY: \"unknown[]\",\n MAP: \"Record<string, unknown>\",\n STRUCT: \"Record<string, unknown>\",\n OBJECT: \"Record<string, unknown>\",\n VARIANT: \"unknown\",\n // spatial types\n GEOGRAPHY: \"unknown\",\n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V,KAAK;CACL,QAAQ;CACR,OAAO;CACP,QAAQ;CACR,SAAS;CAET,MAAM;CACN,WAAW;CACX,eAAe;CACf,UAAU;CAEV,OAAO;CACP,KAAK;CACL,QAAQ;CACR,QAAQ;CACR,SAAS;CAET,WAAW;CACX,UAAU;CAEV,MAAM;CACP"}
1
+ {"version":3,"file":"query-registry.js","names":[],"sources":["../../src/type-generator/query-registry.ts"],"sourcesContent":["import fs from \"node:fs/promises\";\nimport path from \"node:path\";\nimport { tableFromIPC } from \"apache-arrow\";\nimport pc from \"picocolors\";\nimport { createLogger } from \"../logging/logger\";\nimport { createWorkspaceClient } from \"../workspace-client\";\nimport { CACHE_VERSION, hashSQL, loadCache, saveCache } from \"./cache\";\nimport {\n classifyBlockingFailure,\n classifyEnvironmentalCause,\n getErrorDiagnostic,\n isConnectivityError,\n} from \"./errors\";\nimport { decidePreflight, type PreflightMode } from \"./preflight\";\nimport { Spinner } from \"./spinner\";\nimport { type DescribeFormatMemo, describeAdaptive } from \"./statement-result\";\nimport {\n type DatabricksStatementExecutionResponse,\n type QueryFatalError,\n type QueryGenerationResult,\n type QuerySchema,\n type QuerySyntaxError,\n sqlTypeToHelper,\n sqlTypeToMarker,\n} from \"./types\";\nimport {\n getWarehouseState,\n startWarehouse,\n waitUntilRunning,\n} from \"./warehouse-status\";\n\nconst logger = createLogger(\"type-generator:query-registry\");\n\n/**\n * Upper bound on how long a `blocking`-mode preflight will wait for a starting\n * warehouse to reach RUNNING before giving up (~5 min). Generous enough to ride\n * out a cold start without hanging an interactive CLI invocation indefinitely.\n */\nconst PREFLIGHT_WAIT_MAX_MS = 300_000;\n\n/**\n * Regex breakdown:\n * '(?:[^']|'')*' — matches a SQL string literal, including escaped '' pairs\n * | — alternation: whichever branch matches first at a position wins\n * --[^\\n]* — matches a single-line SQL comment\n *\n * Because the regex engine scans left-to-right, a `'` is consumed as a string\n * literal before any `--` inside it could match as a comment — giving us\n * correct single-pass ordering without a manual state machine.\n *\n * V1: no block-comment support (deferred to next PR).\n */\nconst PROTECTED_RANGE_RE = /'(?:[^']|'')*'|--[^\\n]*/g;\n\n/**\n * Numeric-context patterns for positional type inference.\n * Hoisted to module scope — safe because matchAll() clones the regex internally.\n */\nconst NUMERIC_PATTERNS: RegExp[] = [\n /\\bLIMIT\\s+:([a-zA-Z_]\\w*)/gi,\n /\\bOFFSET\\s+:([a-zA-Z_]\\w*)/gi,\n /\\bTOP\\s+:([a-zA-Z_]\\w*)/gi,\n /\\bFETCH\\s+FIRST\\s+:([a-zA-Z_]\\w*)\\s+ROWS/gi,\n // V1 limitation: arithmetic operators may false-positive for date\n // expressions like `:start_date - INTERVAL '1 day'`. A smarter\n // heuristic (e.g. look-ahead for INTERVAL) is deferred to a future PR.\n /[+\\-*/]\\s*:([a-zA-Z_]\\w*)/g,\n /:([a-zA-Z_]\\w*)\\s*[+\\-*/]/g,\n];\n\nexport function getProtectedRanges(sql: string): Array<[number, number]> {\n const ranges: Array<[number, number]> = [];\n for (const m of sql.matchAll(PROTECTED_RANGE_RE)) {\n ranges.push([m.index, m.index + m[0].length]);\n }\n return ranges;\n}\n\nfunction isInsideProtectedRange(\n offset: number,\n ranges: Array<[number, number]>,\n): boolean {\n return ranges.some(([start, end]) => offset >= start && offset < end);\n}\n\n/**\n * Parse a raw API/SDK error into a structured code + message.\n * Handles Databricks-style JSON bodies embedded in the message string,\n * e.g. `Response from server (Bad Request) {\"error_code\":\"...\",\"message\":\"...\"}`.\n */\nfunction parseError(raw: string): { code?: string; message: string } {\n const jsonMatch = raw.match(/\\{[\\s\\S]*\\}/);\n if (jsonMatch) {\n try {\n const parsed = JSON.parse(jsonMatch[0]);\n if (parsed.error_code || parsed.message) {\n return {\n code: parsed.error_code,\n message: parsed.message || raw,\n };\n }\n } catch {\n // not valid JSON, fall through\n }\n }\n return { message: raw };\n}\n\n/**\n * Extract parameters from a SQL query\n * @param sql - the SQL query to extract parameters from\n * @returns an array of parameter names\n */\nexport function extractParameters(\n sql: string,\n ranges?: Array<[number, number]>,\n): string[] {\n const protectedRanges = ranges ?? getProtectedRanges(sql);\n const matches = sql.matchAll(/(?<!:):([a-zA-Z_]\\w*)/g);\n const params = new Set<string>();\n for (const match of matches) {\n if (!isInsideProtectedRange(match.index, protectedRanges)) {\n params.add(match[1]);\n }\n }\n return Array.from(params);\n}\n\n// parameters that are injected by the server\nexport const SERVER_INJECTED_PARAMS = [\"workspaceId\"];\n\n/**\n * Generates the TypeScript type literal for query parameters from SQL.\n * Shared by both the success and failure paths.\n */\nfunction formatParametersType(sql: string): string {\n const params = extractParameters(sql).filter(\n (p) => !SERVER_INJECTED_PARAMS.includes(p),\n );\n const paramTypes = extractParameterTypes(sql);\n\n return params.length > 0\n ? `{\\n ${params\n .map((p) => {\n const sqlType = paramTypes[p];\n const markerType = sqlType\n ? sqlTypeToMarker[sqlType]\n : \"SQLTypeMarker\";\n const helper = sqlType ? sqlTypeToHelper[sqlType] : \"sql.*()\";\n return `/** ${sqlType || \"any\"} - use ${helper} */\\n ${p}: ${markerType}`;\n })\n .join(\";\\n \")};\\n }`\n : \"Record<string, never>\";\n}\n\n/**\n * Decode a base64 Arrow IPC attachment from a DESCRIBE QUERY response and\n * extract column metadata. Returns the same shape as rows parsed from the\n * legacy data_array path.\n *\n * IMPORTANT: a DESCRIBE QUERY response is itself a result *table* with rows\n * shaped like `(col_name, data_type, comment)` describing the user query's\n * output schema. We must read those rows — NOT `table.schema.fields`, which\n * would describe DESCRIBE QUERY's own output (`col_name`, `data_type`,\n * `comment`) and yield bogus types for every query.\n */\nfunction columnsFromArrowAttachment(\n attachment: string,\n): Array<{ name: string; type_name: string; comment: string | undefined }> {\n const buf = Buffer.from(attachment, \"base64\");\n const table = tableFromIPC(buf);\n return table.toArray().map((row) => {\n const obj = row.toJSON() as {\n col_name?: unknown;\n data_type?: unknown;\n comment?: unknown;\n };\n return {\n name: typeof obj.col_name === \"string\" ? obj.col_name : \"\",\n type_name:\n typeof obj.data_type === \"string\"\n ? obj.data_type.toUpperCase()\n : \"STRING\",\n comment:\n typeof obj.comment === \"string\" && obj.comment !== \"\"\n ? obj.comment\n : undefined,\n };\n });\n}\n\nexport function convertToQueryType(\n result: DatabricksStatementExecutionResponse,\n sql: string,\n queryName: string,\n): { type: string; hasResults: boolean } {\n const dataRows = result.result?.data_array || [];\n let columns = dataRows.map((row) => ({\n name: row[0] || \"\",\n type_name: row[1]?.toUpperCase() || \"STRING\",\n comment: row[2] || undefined,\n }));\n\n // Fallback: serverless warehouses return ARROW_STREAM format with an inline\n // base64 attachment instead of data_array. Decode the Arrow IPC rows (the\n // DESCRIBE QUERY result table) to extract column names and types.\n if (columns.length === 0 && result.result?.attachment) {\n logger.debug(\"data_array empty, decoding Arrow IPC attachment for schema\");\n try {\n columns = columnsFromArrowAttachment(result.result.attachment);\n } catch (err) {\n logger.warn(\n \"Failed to decode Arrow IPC attachment: %s\",\n err instanceof Error ? err.message : String(err),\n );\n }\n }\n\n const paramsType = formatParametersType(sql);\n\n // generate result fields with JSDoc\n const resultFields = columns.map((column) => {\n const normalizedType = normalizeTypeName(column.type_name);\n const mappedType = typeMap[normalizedType] || \"unknown\";\n // validate column name is a valid identifier\n const name = /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(column.name)\n ? column.name\n : `\"${column.name}\"`;\n\n // generate comment for column\n const comment = column.comment\n ? `/** ${column.comment.replace(/\\*\\//g, \"* /\")} */\\n `\n : `/** @sqlType ${column.type_name.replace(/\\*\\//g, \"* /\")} */\\n `;\n\n return `${comment}${name}: ${mappedType}`;\n });\n\n const hasResults = resultFields.length > 0;\n\n const type = `{\n name: \"${queryName}\";\n parameters: ${paramsType};\n result: ${\n hasResults\n ? `Array<{\n ${resultFields.join(\";\\n \")};\n }>`\n : \"unknown\"\n };\n }`;\n\n return { type, hasResults };\n}\n\n/**\n * Used when DESCRIBE QUERY fails so the query still appears in QueryRegistry.\n * Generates a type with unknown result from SQL alone (no warehouse call).\n */\nfunction generateUnknownResultQuery(sql: string, queryName: string): string {\n const paramsType = formatParametersType(sql);\n\n return `{\n name: \"${queryName}\";\n parameters: ${paramsType};\n result: unknown;\n }`;\n}\n\n/**\n * Degrade gracefully when DESCRIBE can't produce a fresh schema (transient\n * connectivity outage, or a warehouse that's reachable but not ready). Reuse\n * the last-good cached type when the SQL hash is unchanged, otherwise emit\n * `unknown` from SQL alone. Never persists `result: unknown`.\n */\nfunction degradedType(\n cache: Awaited<ReturnType<typeof loadCache>>,\n queryName: string,\n sql: string,\n sqlHash: string,\n): Pick<QuerySchema, \"type\" | \"degraded\"> {\n const prior = cache.queries[queryName];\n const canReusePrior = prior?.hash === sqlHash && !prior.retry;\n return canReusePrior\n ? { type: prior.type }\n : {\n type: generateUnknownResultQuery(sql, queryName),\n degraded: true,\n };\n}\n\n// Single source of truth for the `@param` type alternation, shared by\n// extractParameterTypes and extractParameterDefaults so the two can't drift.\n// Alternation order matters: TIMESTAMP_NTZ must precede TIMESTAMP so the regex\n// engine doesn't greedy-match TIMESTAMP and leave `_NTZ` unconsumed.\nconst PARAM_TYPE_ALTERNATION =\n \"STRING|NUMERIC|DECIMAL|BIGINT|TINYINT|SMALLINT|INT|FLOAT|DOUBLE|BOOLEAN|DATE|TIMESTAMP_NTZ|TIMESTAMP|BINARY\";\n\nexport function extractParameterTypes(sql: string): Record<string, string> {\n const paramTypes: Record<string, string> = {};\n const regex = new RegExp(\n `--\\\\s*@param\\\\s+(\\\\w+)\\\\s+(${PARAM_TYPE_ALTERNATION})\\\\b`,\n \"gi\",\n );\n const matches = sql.matchAll(regex);\n for (const match of matches) {\n const [, paramName, paramType] = match;\n paramTypes[paramName] = paramType.toUpperCase();\n }\n\n return paramTypes;\n}\n\nexport function defaultForType(sqlType: string | undefined): string {\n switch (sqlType?.toUpperCase()) {\n case \"NUMERIC\":\n case \"DECIMAL\":\n case \"BIGINT\":\n case \"TINYINT\":\n case \"SMALLINT\":\n case \"INT\":\n return \"0\";\n case \"FLOAT\":\n case \"DOUBLE\":\n return \"0.0\";\n case \"STRING\":\n return \"''\";\n case \"BOOLEAN\":\n return \"true\";\n case \"DATE\":\n return \"'2000-01-01'\";\n case \"TIMESTAMP\":\n return \"'2000-01-01T00:00:00Z'\";\n case \"TIMESTAMP_NTZ\":\n return \"'2000-01-01T00:00:00'\";\n case \"BINARY\":\n return \"X'00'\";\n default:\n return \"''\";\n }\n}\n\n/**\n * True when `raw` is already a single, well-formed SQL single-quoted string\n * literal — i.e. it opens and closes with `'` and every interior quote is part\n * of an escaped `''` pair. `'2024-01-01'` and `'O''Brien'` qualify;\n * `'a' OR 1=1 OR 'b'` does not (it has lone interior quotes), so it is treated\n * as raw content and re-escaped rather than trusted. A backslash also\n * disqualifies it: Databricks/Spark treats `\\` as an escape inside literals (so\n * `'a\\'` is unterminated), and we never trust such input — it is re-escaped.\n */\nfunction isWellFormedStringLiteral(raw: string): boolean {\n if (raw.length < 2 || !raw.startsWith(\"'\") || !raw.endsWith(\"'\")) {\n return false;\n }\n const inner = raw.slice(1, -1);\n if (inner.includes(\"\\\\\")) return false;\n return !inner.replace(/''/g, \"\").includes(\"'\");\n}\n\n/**\n * Format a user-supplied sample value as a SQL literal for substitution into\n * the build-time DESCRIBE statement. Returns `null` when the value isn't valid\n * for its type, so the caller falls back to the safe type-based placeholder\n * instead of substituting attacker-controllable text.\n *\n * The value comes from a `.sql` file that may be shared via a template or\n * dependency, so it must not be able to inject SQL into `DESCRIBE QUERY`:\n * - string-like types are always emitted as one well-formed, fully-escaped\n * single-quoted literal (a pre-quoted literal is kept as-is, anything else is\n * quoted with both `\\` and `'` doubled so neither can terminate the literal),\n * so the value can never break out of the string;\n * - numeric / boolean / binary values must match a strict literal shape and are\n * rejected (`null`) otherwise, rather than being passed through verbatim.\n */\nfunction formatSampleValue(\n sqlType: string | undefined,\n raw: string,\n): string | null {\n switch (sqlType?.toUpperCase()) {\n case \"STRING\":\n case \"DATE\":\n case \"TIMESTAMP\":\n case \"TIMESTAMP_NTZ\":\n return isWellFormedStringLiteral(raw)\n ? raw\n : `'${raw.replace(/\\\\/g, \"\\\\\\\\\").replace(/'/g, \"''\")}'`;\n case \"NUMERIC\":\n case \"DECIMAL\":\n case \"BIGINT\":\n case \"TINYINT\":\n case \"SMALLINT\":\n case \"INT\":\n case \"FLOAT\":\n case \"DOUBLE\":\n return /^[+-]?\\d+(\\.\\d+)?$/.test(raw) ? raw : null;\n case \"BOOLEAN\":\n return /^(?:true|false)$/i.test(raw) ? raw.toLowerCase() : null;\n case \"BINARY\":\n return /^X'[0-9a-fA-F]*'$/i.test(raw) ? raw : null;\n default:\n return null;\n }\n}\n\n/**\n * Parse optional describe-time sample values from `@param` annotations, e.g.\n * `-- @param target_catalog STRING = main`. The value is substituted into the\n * SQL **only during DESCRIBE QUERY** so type generation can resolve queries\n * whose shape depends on a parameter value — most notably dynamic table names\n * via `IDENTIFIER(:target_catalog || '.schema.table')`, where the empty-string\n * default would otherwise produce malformed SQL. Runtime binding is unaffected:\n * the analytics plugin still binds the real parameter at execution time, so the\n * query stays portable across environments.\n *\n * Returns a map of parameter name to the formatted SQL literal to substitute.\n */\nexport function extractParameterDefaults(sql: string): Record<string, string> {\n const defaults: Record<string, string> = {};\n // Reuses PARAM_TYPE_ALTERNATION, then requires `= <value>` on the same line.\n // All inter-token whitespace is horizontal-only (`[^\\S\\r\\n]`, not `\\s`): `\\s`\n // matches newlines, so a value-less line like `-- @param x STRING =` would let\n // `\\s*=\\s*(.+?)` swallow the *next* line as the sample value. Restricting to\n // same-line whitespace makes such a line simply not match, so it correctly\n // falls back to the type placeholder.\n const regex = new RegExp(\n `--[^\\\\S\\\\r\\\\n]*@param[^\\\\S\\\\r\\\\n]+(\\\\w+)[^\\\\S\\\\r\\\\n]+(${PARAM_TYPE_ALTERNATION})[^\\\\S\\\\r\\\\n]*=[^\\\\S\\\\r\\\\n]*(.+?)[^\\\\S\\\\r\\\\n]*$`,\n \"gim\",\n );\n for (const match of sql.matchAll(regex)) {\n const [, paramName, paramType, rawValue] = match;\n const formatted = formatSampleValue(paramType, rawValue);\n // A value that fails type validation is dropped, not substituted: the param\n // then falls back to the safe type-based placeholder during DESCRIBE.\n if (formatted !== null) {\n defaults[paramName] = formatted;\n }\n }\n return defaults;\n}\n\n/**\n * Replace `:param` placeholders with describe-time literals so `DESCRIBE QUERY`\n * can run without bound parameters. Resolution order per parameter:\n * 1. An explicit `-- @param name TYPE = value` sample value (wins), which lets\n * dynamic table names via `IDENTIFIER(...)` resolve to a real table.\n * 2. Otherwise a placeholder default derived from the annotated/inferred type.\n * Placeholders inside string literals or comments are left untouched.\n */\nexport function substituteParametersForDescribe(sql: string): string {\n const protectedRanges = getProtectedRanges(sql);\n const annotatedTypes = extractParameterTypes(sql);\n const inferredTypes = inferParameterTypes(sql, protectedRanges);\n const parameterTypes = { ...inferredTypes, ...annotatedTypes };\n const parameterDefaults = extractParameterDefaults(sql);\n return sql.replace(\n /(?<!:):([a-zA-Z_]\\w*)/g,\n (original, paramName, offset) => {\n if (isInsideProtectedRange(offset, protectedRanges)) {\n return original;\n }\n const sampleValue = parameterDefaults[paramName];\n if (sampleValue !== undefined) {\n return sampleValue;\n }\n return defaultForType(parameterTypes[paramName]);\n },\n );\n}\n\n/**\n * Append a remediation hint when a DESCRIBE failure looks like a dynamic\n * identifier that couldn't be resolved: the query calls `IDENTIFIER(...)` and\n * has at least one parameter without a describe-time sample value. These fail\n * because typegen substitutes a placeholder default (e.g. `''`) that yields a\n * malformed or non-existent table name. Steering the user to the `= value`\n * annotation turns the fatal error into a one-line fix.\n */\nfunction withIdentifierHint(\n error: { code?: string; message: string },\n sql: string,\n): { code?: string; message: string } {\n if (!/\\bIDENTIFIER\\s*\\(/i.test(sql)) {\n return error;\n }\n const protectedRanges = getProtectedRanges(sql);\n const params = extractParameters(sql, protectedRanges);\n const defaults = extractParameterDefaults(sql);\n const unresolved = params.filter(\n (p) => !SERVER_INJECTED_PARAMS.includes(p) && defaults[p] === undefined,\n );\n if (unresolved.length === 0) {\n return error;\n }\n const example = unresolved[0];\n return {\n ...error,\n message: `${error.message}\\n Hint: this query uses IDENTIFIER() with parameter(s) ${unresolved\n .map((p) => `:${p}`)\n .join(\n \", \",\n )}. Give type generation a sample value so it can resolve the table, e.g. \\`-- @param ${example} STRING = my_catalog\\`. The runtime query still binds the real parameter.`,\n };\n}\n\n/**\n * Infer parameter types from positional context in SQL.\n * V1 only infers NUMERIC from patterns like LIMIT, OFFSET, TOP,\n * FETCH FIRST ... ROWS, and arithmetic operators.\n * Parameters inside string literals or SQL comments are ignored.\n */\nexport function inferParameterTypes(\n sql: string,\n ranges?: Array<[number, number]>,\n): Record<string, string> {\n const inferred: Record<string, string> = {};\n const protectedRanges = ranges ?? getProtectedRanges(sql);\n\n for (const pattern of NUMERIC_PATTERNS) {\n for (const match of sql.matchAll(pattern)) {\n if (!isInsideProtectedRange(match.index, protectedRanges)) {\n inferred[match[1]] = \"NUMERIC\";\n }\n }\n }\n\n return inferred;\n}\n\n/**\n * Generate query schemas from a folder of SQL files\n * It uses DESCRIBE QUERY to get the schema without executing the query\n * @param queryFolder - the folder containing the SQL files\n * @param warehouseId - the warehouse id to use for schema analysis\n * @param options - options for the query generation\n * @param options.noCache - if true, skip the cache and regenerate all types\n * @param options.mode - preflight policy: \"non-blocking\" never probes the\n * warehouse and never describes (emits cached/`unknown` types and returns\n * immediately), \"blocking\" waits for a starting warehouse and starts (then\n * waits for) a stopped one, treating only a deleted/deleting warehouse as\n * fatal. Defaults to \"non-blocking\".\n * @returns an array of query schemas\n */\nexport async function generateQueriesFromDescribe(\n queryFolder: string,\n warehouseId: string,\n options: {\n noCache?: boolean;\n concurrency?: number;\n mode?: PreflightMode;\n } = {},\n): Promise<QueryGenerationResult> {\n const {\n noCache = false,\n concurrency: rawConcurrency = 10,\n mode = \"non-blocking\",\n } = options;\n const concurrency =\n typeof rawConcurrency === \"number\" && Number.isFinite(rawConcurrency)\n ? Math.max(1, Math.floor(rawConcurrency))\n : 10;\n\n // read all query files and cache in parallel\n const [allFiles, cache] = await Promise.all([\n fs.readdir(queryFolder),\n noCache\n ? ({ version: CACHE_VERSION, queries: {} } as Awaited<\n ReturnType<typeof loadCache>\n >)\n : loadCache(),\n ]);\n\n const queryFiles = allFiles.filter((file) => file.endsWith(\".sql\"));\n logger.debug(\"Found %d SQL queries\", queryFiles.length);\n\n const client = createWorkspaceClient();\n const spinner = new Spinner();\n\n // Read all SQL files in parallel\n const sqlContents = await Promise.all(\n queryFiles.map((file) => fs.readFile(path.join(queryFolder, file), \"utf8\")),\n );\n\n const startTime = performance.now();\n\n // Phase 1: Check cache, separate cached vs uncached\n const cachedResults: Array<{ index: number; schema: QuerySchema }> = [];\n const uncachedQueries: Array<{\n index: number;\n queryName: string;\n sql: string;\n sqlHash: string;\n cleanedSql: string;\n }> = [];\n const logEntries: Array<{\n queryName: string;\n status: \"HIT\" | \"MISS\";\n // Absent for clean hits/misses. \"syntax\" = bad SQL on a reachable warehouse;\n // \"connectivity\" = warehouse unreachable; \"empty\" = described but no columns;\n // \"fatal\" = non-SQL setup/request failure surfaced after .d.ts emission.\n kind?: \"syntax\" | \"connectivity\" | \"empty\" | \"fatal\";\n error?: { code?: string; message: string };\n }> = [];\n\n for (let i = 0; i < queryFiles.length; i++) {\n const file = queryFiles[i];\n const rawName = path.basename(file, \".sql\");\n const queryName = normalizeQueryName(rawName);\n\n const sql = sqlContents[i];\n const sqlHash = hashSQL(sql);\n\n const cached = cache.queries[queryName];\n if (cached && cached.hash === sqlHash && !cached.retry) {\n cachedResults.push({\n index: i,\n schema: { name: queryName, type: cached.type },\n });\n logEntries.push({ queryName, status: \"HIT\" });\n } else {\n const protectedRanges = getProtectedRanges(sql);\n const annotatedTypes = extractParameterTypes(sql);\n const inferredTypes = inferParameterTypes(sql, protectedRanges);\n const parameterTypes = { ...inferredTypes, ...annotatedTypes };\n // substituteParametersForDescribe applies `-- @param name TYPE = value`\n // sample values (e.g. for IDENTIFIER table names) ahead of type defaults;\n // parameterDefaults is recomputed here only to skip them in the warn loop.\n const parameterDefaults = extractParameterDefaults(sql);\n const sqlWithDefaults = substituteParametersForDescribe(sql);\n\n // Warn about unresolved parameters\n const allParams = extractParameters(sql, protectedRanges);\n for (const param of allParams) {\n if (SERVER_INJECTED_PARAMS.includes(param)) continue;\n if (parameterDefaults[param]) continue;\n if (parameterTypes[param]) continue;\n logger.warn(\n '%s: parameter \":%s\" has no type annotation or inference. Add %s to the query file.',\n queryFiles[i],\n param,\n `-- @param ${param} <TYPE>`,\n );\n }\n\n const cleanedSql = sqlWithDefaults.trim().replace(/;\\s*$/, \"\");\n uncachedQueries.push({ index: i, queryName, sql, sqlHash, cleanedSql });\n }\n }\n\n // Phase 2: Execute all uncached DESCRIBE calls in parallel\n type DescribeResult =\n | {\n // Described successfully with a result schema — the only case we cache.\n status: \"ok\";\n index: number;\n schema: QuerySchema;\n cacheEntry: { hash: string; type: string; retry: boolean };\n }\n | {\n // Reachable warehouse ran DESCRIBE and rejected the statement — a\n // genuine SQL error. Eligible to fail the build; never cached.\n status: \"syntax\";\n index: number;\n schema: QuerySchema;\n error: { code?: string; message: string };\n }\n | {\n // DESCRIBE succeeded but returned no columns — soft `unknown`. Not a\n // failure, not cached, retried next run.\n status: \"empty\";\n index: number;\n schema: QuerySchema;\n }\n | {\n // Warehouse reachable but returned a non-terminal state (PENDING/\n // RUNNING) with no rows — stopped/cold-starting/busy. Degrade like a\n // transient outage (reuse cache or `unknown`); not cached, not empty.\n status: \"unavailable\";\n index: number;\n schema: QuerySchema;\n };\n\n const freshResults: Array<{ index: number; schema: QuerySchema }> = [];\n // Genuine SQL errors (reachable warehouse). Connectivity failures are NOT\n // recorded here — they degrade silently so a transient outage isn't fatal.\n const syntaxErrors: QuerySyntaxError[] = [];\n // Deterministic fatal errors only (404/400). Environmental failures are\n // tracked separately below.\n const fatalErrors: QueryFatalError[] = [];\n // Track whether an environmental failure occurred in blocking mode (for the\n // has-types gate in generateFromEntryPoint), plus its coarse cause so the\n // gate's warning can say why generation fell back to committed types.\n let hadEnvironmentalFailure = false;\n let environmentalCause: \"auth\" | \"unreachable\" | \"unavailable\" | undefined;\n\n if (uncachedQueries.length > 0) {\n // One-time warehouse preflight (before issuing any DESCRIBE). A single\n // warehouses.get classifies the warehouse so we can skip the whole describe\n // batch when it can't serve this run, instead of letting every query fail\n // (and re-fail next run). Reuses this file's degrade/classify helpers so a\n // not-ready warehouse degrades exactly like a per-query outage.\n let decision: ReturnType<typeof decidePreflight> = \"proceed\";\n let fatalMessage = \"\";\n // Track whether an environmental failure occurred so the caller's has-types\n // gate can decide crash-vs-fall-back.\n let isEnvironmental = false;\n if (mode === \"non-blocking\") {\n // `non-blocking` never describes and must make ZERO warehouse round-trips:\n // skip the probe entirely (no getWarehouseState) and go straight to\n // degradeAll. A foreground/one-shot run can't describe in the background,\n // so it emits best-available types (reused cache or `unknown`) and returns\n // now.\n decision = \"degradeAll\";\n } else {\n try {\n const state = await getWarehouseState(client, warehouseId);\n decision = decidePreflight(state, mode);\n if (decision === \"fatal\") {\n // DELETED/DELETING is state-based and environmental.\n fatalMessage = `warehouse ${warehouseId} is ${state}`;\n isEnvironmental = true;\n environmentalCause = \"unavailable\";\n }\n if (decision === \"startWaitProceed\") {\n // Stopped/stopping warehouse: nudge it out of the stopped state, then\n // poll to RUNNING. treatStoppedAsTransient rides out the stale\n // pre-start STOPPED/STOPPING reading the start hasn't propagated past\n // yet — only DELETED/DELETING (or the deadline) ends the wait early.\n await startWarehouse(client, warehouseId);\n const final = await waitUntilRunning(client, warehouseId, {\n maxMs: PREFLIGHT_WAIT_MAX_MS,\n treatStoppedAsTransient: true,\n });\n if (final === \"RUNNING\") {\n decision = \"proceed\";\n } else {\n decision = \"fatal\";\n fatalMessage = `warehouse ${warehouseId} did not reach RUNNING (now ${final})`;\n isEnvironmental = true; // DELETED/DELETING or timeout is environmental\n environmentalCause = \"unavailable\";\n }\n }\n if (decision === \"waitThenProceed\") {\n const final = await waitUntilRunning(client, warehouseId, {\n maxMs: PREFLIGHT_WAIT_MAX_MS,\n });\n if (final === \"RUNNING\") {\n decision = \"proceed\";\n } else {\n decision = \"fatal\";\n fatalMessage = `warehouse ${warehouseId} did not reach RUNNING (now ${final})`;\n isEnvironmental = true; // DELETED/DELETING or timeout is environmental\n environmentalCause = \"unavailable\";\n }\n }\n } catch (err) {\n if (isConnectivityError(err)) {\n // Warehouse unreachable (transient outage): degrade rather than fail —\n // never fail a build on a blip. Still environmental, so the caller's\n // has-types gate decides warn-and-fall-back (committed types present)\n // vs crash (fresh checkout with nothing to fall back to).\n decision = \"degradeAll\";\n isEnvironmental = true;\n environmentalCause = \"unreachable\";\n } else {\n const classification = classifyBlockingFailure(err);\n if (classification === \"deterministic\") {\n decision = \"fatal\";\n fatalMessage = `warehouse ${warehouseId}: ${getErrorDiagnostic(err)}`;\n } else {\n decision = \"degradeAll\";\n isEnvironmental = true;\n environmentalCause = classifyEnvironmentalCause(err);\n }\n }\n }\n }\n\n if (mode === \"blocking\" && decision !== \"proceed\" && isEnvironmental) {\n hadEnvironmentalFailure = true;\n }\n\n if (decision !== \"proceed\") {\n // degradeAll or fatal: skip DESCRIBE entirely. Every uncached query gets a\n // degraded schema (reused cache or `unknown`); fatal additionally records\n // a fatalError per query so the caller fails the build after writing. Only\n // deterministic fatals are recorded; environmental degradations go silent\n // so the has-types gate can decide.\n const kind =\n decision === \"fatal\" && !isEnvironmental ? \"fatal\" : \"connectivity\";\n for (const { index, queryName, sql, sqlHash } of uncachedQueries) {\n freshResults.push({\n index,\n schema: {\n name: queryName,\n ...degradedType(cache, queryName, sql, sqlHash),\n },\n });\n if (decision === \"fatal\" && !isEnvironmental) {\n // Only deterministic fatals record an error; environmental failures\n // degrade silently for the has-types gate.\n fatalErrors.push({ name: queryName, message: fatalMessage });\n logEntries.push({\n queryName,\n status: \"MISS\",\n kind,\n error: { message: fatalMessage },\n });\n } else {\n logEntries.push({ queryName, status: \"MISS\", kind });\n }\n }\n } else {\n let completed = 0;\n const total = uncachedQueries.length;\n spinner.start(\n `Describing ${total} ${total === 1 ? \"query\" : \"queries\"} (0/${total})`,\n );\n\n // Shared across this run's DESCRIBE QUERY calls: discover the warehouse's\n // result format once, then reuse it.\n const describeFormat: DescribeFormatMemo = {};\n\n const describeOne = async ({\n index,\n queryName,\n sql,\n sqlHash,\n cleanedSql,\n }: (typeof uncachedQueries)[number]): Promise<DescribeResult> => {\n // describeAdaptive negotiates the result format with the warehouse\n // (JSON_ARRAY for standard DBSQL, ARROW_STREAM for Reyden) and returns\n // an already-normalized response, so the state classification below\n // reads it directly.\n const result = await describeAdaptive(\n client,\n `DESCRIBE QUERY ${cleanedSql}`,\n warehouseId,\n describeFormat,\n );\n\n completed++;\n spinner.update(\n `Describing ${total} ${total === 1 ? \"query\" : \"queries\"} (${completed}/${total})`,\n );\n\n logger.debug(\n \"DESCRIBE result for %s: state=%s, rows=%d, hasAttachment=%s\",\n queryName,\n result.status.state,\n result.result?.data_array?.length ?? 0,\n !!result.result?.attachment,\n );\n\n if (result.status.state === \"FAILED\") {\n // The warehouse was reachable and ran DESCRIBE, but the statement\n // failed — a genuine SQL error (bad table, syntax, incompatible type).\n const sqlError =\n result.status.error?.message || \"Query execution failed\";\n // The failure is surfaced once, formatted, by the aggregated\n // TypegenSyntaxError (and the summary table) — don't also log the raw\n // message here or every SQL error prints twice in dev.\n const type = generateUnknownResultQuery(sql, queryName);\n return {\n status: \"syntax\",\n index,\n schema: { name: queryName, type, degraded: true },\n error: withIdentifierHint(parseError(sqlError), sql),\n };\n }\n\n if (result.status.state !== \"SUCCEEDED\") {\n // Non-terminal state (PENDING/RUNNING) with no result rows: the\n // warehouse is reachable but not ready (stopped, cold-starting, or\n // busy). Degrade like a transient outage — reuse the last-good cached\n // type when the SQL is unchanged, else emit `unknown`. Never \"empty\":\n // treating this as empty would emit `result: unknown` AND discard the\n // good cached type, silently throwing away working types.\n return {\n status: \"unavailable\",\n index,\n schema: {\n name: queryName,\n ...degradedType(cache, queryName, sql, sqlHash),\n },\n };\n }\n\n const { type, hasResults } = convertToQueryType(result, sql, queryName);\n if (!hasResults) {\n // Described, but no result columns. Emit `unknown` and retry next run;\n // do not cache (we never persist `result: unknown`).\n return {\n status: \"empty\",\n index,\n schema: { name: queryName, type, degraded: true },\n };\n }\n return {\n status: \"ok\",\n index,\n schema: { name: queryName, type },\n cacheEntry: { hash: sqlHash, type, retry: false },\n };\n };\n\n // Process in chunks, saving cache after each chunk\n const processBatchResults = (\n settled: PromiseSettledResult<DescribeResult>[],\n batchOffset: number,\n ) => {\n for (let i = 0; i < settled.length; i++) {\n const entry = settled[i];\n const { queryName } = uncachedQueries[batchOffset + i];\n\n if (entry.status === \"fulfilled\") {\n const res = entry.value;\n freshResults.push({ index: res.index, schema: res.schema });\n\n if (res.status === \"ok\") {\n // Only a successful describe with a result schema is cached.\n cache.queries[queryName] = res.cacheEntry;\n logEntries.push({ queryName, status: \"MISS\" });\n } else if (res.status === \"syntax\") {\n // Genuine SQL error — record it for the caller's prod/dev gate.\n // Not cached: re-described next run so a fixed query recovers.\n syntaxErrors.push({\n name: queryName,\n message: res.error.message,\n });\n logEntries.push({\n queryName,\n status: \"MISS\",\n kind: \"syntax\",\n error: res.error,\n });\n } else if (res.status === \"empty\") {\n // status === \"empty\": described, no columns. Soft unknown, not cached.\n logEntries.push({ queryName, status: \"MISS\", kind: \"empty\" });\n } else {\n // status === \"unavailable\": non-terminal DESCRIBE (warehouse\n // stopped/cold-starting/busy). Degrade like a transient outage:\n // tag OFFLINE, count as degraded, never cache.\n if (mode === \"blocking\") {\n hadEnvironmentalFailure = true;\n environmentalCause = environmentalCause ?? \"unavailable\";\n }\n logEntries.push({\n queryName,\n status: \"MISS\",\n kind: \"connectivity\",\n });\n }\n } else {\n // executeStatement rejected without a normal StatementExecution result.\n // Only structured transport/connectivity failures are treated as\n // offline; auth, bad warehouse IDs, malformed requests, and SDK/config\n // failures stay fatal so users fix the underlying setup issue.\n completed++;\n spinner.update(\n `Describing ${total} ${total === 1 ? \"query\" : \"queries\"} (${completed}/${total})`,\n );\n\n const { sql, sqlHash, index } = uncachedQueries[batchOffset + i];\n const reason = getErrorDiagnostic(entry.reason);\n const error = parseError(reason);\n const priorEntry = cache.queries[queryName];\n const canReusePrior =\n priorEntry?.hash === sqlHash && !priorEntry.retry;\n const degraded = degradedType(cache, queryName, sql, sqlHash);\n freshResults.push({\n index,\n schema: { name: queryName, ...degraded },\n });\n\n if (!isConnectivityError(entry.reason)) {\n fatalErrors.push({ name: queryName, message: error.message });\n logEntries.push({\n queryName,\n status: \"MISS\",\n kind: \"fatal\",\n error,\n });\n continue;\n }\n\n // Environmental for the same reason as the preflight connectivity\n // branch above, so the has-types gate still sees it.\n if (mode === \"blocking\") {\n hadEnvironmentalFailure = true;\n environmentalCause = environmentalCause ?? \"unreachable\";\n }\n\n logger.warn(\n \"DESCRIBE unreachable for %s: %s — %s\",\n queryName,\n reason,\n canReusePrior\n ? \"reusing last cached type\"\n : \"emitting unknown (no matching cache)\",\n );\n logEntries.push({\n queryName,\n status: \"MISS\",\n kind: \"connectivity\",\n error,\n });\n }\n }\n };\n\n if (uncachedQueries.length > concurrency) {\n for (let b = 0; b < uncachedQueries.length; b += concurrency) {\n const batch = uncachedQueries.slice(b, b + concurrency);\n const batchResults = await Promise.allSettled(batch.map(describeOne));\n processBatchResults(batchResults, b);\n await saveCache(cache);\n }\n } else {\n const settled = await Promise.allSettled(\n uncachedQueries.map(describeOne),\n );\n processBatchResults(settled, 0);\n await saveCache(cache);\n }\n\n spinner.stop(\"\");\n }\n }\n\n const elapsed = ((performance.now() - startTime) / 1000).toFixed(2);\n\n // Print formatted table\n if (logEntries.length > 0) {\n const maxNameLen = Math.max(...logEntries.map((e) => e.queryName.length));\n const separator = pc.dim(\"─\".repeat(50));\n console.log(\"\");\n console.log(\n ` ${pc.bold(\"Typegen Queries\")} ${pc.dim(`(${logEntries.length})`)}`,\n );\n console.log(` ${separator}`);\n for (const entry of logEntries) {\n let tag: string;\n switch (entry.kind) {\n case \"syntax\":\n tag = pc.bold(pc.red(\"SQL ERR\"));\n break;\n case \"connectivity\":\n tag = pc.bold(pc.yellow(\"OFFLINE\"));\n break;\n case \"empty\":\n tag = pc.dim(\"EMPTY \");\n break;\n case \"fatal\":\n tag = pc.bold(pc.red(\"FATAL \"));\n break;\n default:\n tag =\n entry.status === \"HIT\"\n ? `cache ${pc.bold(pc.green(\"HIT \"))}`\n : `cache ${pc.bold(pc.yellow(\"MISS \"))}`;\n }\n const rawName = entry.queryName.padEnd(maxNameLen);\n // Only genuine SQL errors are struck through. Connectivity/empty kept a\n // usable type (reused or unknown), so they read as degraded, not broken.\n const name =\n entry.kind === \"syntax\" || entry.kind === \"fatal\"\n ? pc.dim(pc.strikethrough(rawName))\n : rawName;\n const errorCode = entry.error?.message.match(/\\[([^\\]]+)\\]/)?.[1];\n const reason = errorCode ? ` ${pc.dim(errorCode)}` : \"\";\n console.log(` ${tag} ${name}${reason}`);\n }\n const newCount = logEntries.filter(\n (e) => e.status === \"MISS\" && !e.kind,\n ).length;\n const cacheCount = logEntries.filter((e) => e.status === \"HIT\").length;\n const syntaxCount = logEntries.filter((e) => e.kind === \"syntax\").length;\n const offlineCount = logEntries.filter(\n (e) => e.kind === \"connectivity\",\n ).length;\n const emptyCount = logEntries.filter((e) => e.kind === \"empty\").length;\n const fatalCount = logEntries.filter((e) => e.kind === \"fatal\").length;\n console.log(` ${separator}`);\n const parts = [`${newCount} new`, `${cacheCount} from cache`];\n if (syntaxCount > 0)\n parts.push(\n `${syntaxCount} SQL ${syntaxCount === 1 ? \"error\" : \"errors\"}`,\n );\n if (offlineCount > 0) parts.push(`${offlineCount} degraded`);\n if (emptyCount > 0) parts.push(`${emptyCount} empty`);\n if (fatalCount > 0)\n parts.push(\n `${fatalCount} fatal ${fatalCount === 1 ? \"error\" : \"errors\"}`,\n );\n console.log(` ${parts.join(\", \")}. ${pc.dim(`${elapsed}s`)}`);\n console.log(\"\");\n }\n\n // Merge and sort by original file index for deterministic output\n const schemas = [...cachedResults, ...freshResults]\n .sort((a, b) => a.index - b.index)\n .map((r) => r.schema);\n\n return {\n schemas,\n syntaxErrors,\n fatalErrors,\n hadEnvironmentalFailure,\n environmentalCause: hadEnvironmentalFailure\n ? environmentalCause\n : undefined,\n };\n}\n\n/**\n * Normalize query name by removing the .obo extension\n * @param queryName - the query name to normalize\n * @returns the normalized query name\n */\nfunction normalizeQueryName(fileName: string): string {\n return fileName.replace(/\\.obo$/, \"\");\n}\n\n/**\n * Normalize SQL type name by removing parameters/generics\n * Examples:\n * DECIMAL(38,6) -> DECIMAL\n * ARRAY<STRING> -> ARRAY\n * MAP<STRING,INT> -> MAP\n * STRUCT<name:STRING> -> STRUCT\n * INTERVAL DAY TO SECOND -> INTERVAL\n * GEOGRAPHY(4326) -> GEOGRAPHY\n */\nexport function normalizeTypeName(typeName: string): string {\n return typeName\n .replace(/\\(.*\\)$/, \"\") // remove (p, s) eg: DECIMAL(38,6) -> DECIMAL\n .replace(/<.*>$/, \"\") // remove <T> eg: ARRAY<STRING> -> ARRAY\n .split(\" \")[0]; // take first word eg: INTERVAL DAY TO SECOND -> INTERVAL\n}\n\n/** Type Map for Databricks data types to JavaScript types */\nconst typeMap: Record<string, string> = {\n // string types\n STRING: \"string\",\n BINARY: \"string\",\n // boolean\n BOOLEAN: \"boolean\",\n // numeric types\n TINYINT: \"number\",\n SMALLINT: \"number\",\n INT: \"number\",\n BIGINT: \"number\",\n FLOAT: \"number\",\n DOUBLE: \"number\",\n DECIMAL: \"number\",\n // date/time types\n DATE: \"string\",\n TIMESTAMP: \"string\",\n TIMESTAMP_NTZ: \"string\",\n INTERVAL: \"string\",\n // complex types\n ARRAY: \"unknown[]\",\n MAP: \"Record<string, unknown>\",\n STRUCT: \"Record<string, unknown>\",\n OBJECT: \"Record<string, unknown>\",\n VARIANT: \"unknown\",\n // spatial types\n GEOGRAPHY: \"unknown\",\n GEOMETRY: \"unknown\",\n // null type\n VOID: \"null\",\n};\n"],"mappings":";;;;;;;;;;;;;;;AA+BA,MAAM,SAAS,aAAa,gCAA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V,OAAO;CACP,KAAK;CACL,QAAQ;CACR,QAAQ;CACR,SAAS;CAET,WAAW;CACX,UAAU;CAEV,MAAM;CACP"}
@@ -1,6 +1,5 @@
1
+ import { ApiError } from "../../shared/src/workspace-client/errors.js";
1
2
  import { createLogger } from "../../logging/logger.js";
2
- import { ApiError } from "../../workspace-client/errors.js";
3
- import "../../workspace-client/index.js";
4
3
 
5
4
  //#region src/type-generator/serving/fetcher.ts
6
5
  const logger = createLogger("type-generator:serving:fetcher");
@@ -1 +1 @@
1
- {"version":3,"file":"fetcher.js","names":[],"sources":["../../../src/type-generator/serving/fetcher.ts"],"sourcesContent":["import { createLogger } from \"../../logging/logger\";\nimport { ApiError, type WorkspaceClient } from \"../../workspace-client\";\n\nconst logger = createLogger(\"type-generator:serving:fetcher\");\n\ninterface OpenApiSpec {\n openapi: string;\n info: { title: string; version: string };\n paths: Record<string, Record<string, OpenApiOperation>>;\n}\n\nexport interface OpenApiOperation {\n requestBody?: {\n content: {\n \"application/json\": {\n schema: OpenApiSchema;\n };\n };\n };\n responses?: Record<\n string,\n {\n content?: {\n \"application/json\": {\n schema: OpenApiSchema;\n };\n };\n }\n >;\n}\n\nexport interface OpenApiSchema {\n type?: string;\n properties?: Record<string, OpenApiSchema>;\n required?: string[];\n items?: OpenApiSchema;\n enum?: string[];\n nullable?: boolean;\n oneOf?: OpenApiSchema[];\n format?: string;\n}\n\n/**\n * Fetches the OpenAPI schema for a serving endpoint using the SDK.\n * Returns null if the endpoint is not found or access is denied.\n */\nexport async function fetchOpenApiSchema(\n client: WorkspaceClient,\n endpointName: string,\n servedModel?: string,\n): Promise<{ spec: OpenApiSpec; pathKey: string } | null> {\n try {\n const response = await client.servingEndpoints.getOpenApi({\n name: endpointName,\n });\n\n if (!response.contents) {\n logger.warn(\n \"Empty OpenAPI response for '%s', skipping type generation\",\n endpointName,\n );\n return null;\n }\n\n const text = await new Response(response.contents).text();\n const rawSpec: unknown = JSON.parse(text);\n\n if (\n typeof rawSpec !== \"object\" ||\n rawSpec === null ||\n !(\"paths\" in rawSpec) ||\n typeof (rawSpec as OpenApiSpec).paths !== \"object\"\n ) {\n logger.warn(\n \"Invalid OpenAPI schema structure for '%s', skipping\",\n endpointName,\n );\n return null;\n }\n const spec = rawSpec as OpenApiSpec;\n\n // Find the right path key\n const pathKeys = Object.keys(spec.paths ?? {});\n if (pathKeys.length === 0) {\n logger.warn(\"No paths in OpenAPI schema for '%s'\", endpointName);\n return null;\n }\n\n let pathKey: string;\n if (servedModel) {\n const match = pathKeys.find((k) => k.includes(`/${servedModel}/`));\n if (!match) {\n logger.warn(\n \"Served model '%s' not found in schema for '%s', using first path\",\n servedModel,\n endpointName,\n );\n pathKey = pathKeys[0];\n } else {\n pathKey = match;\n }\n } else {\n pathKey = pathKeys[0];\n }\n\n return { spec, pathKey };\n } catch (err) {\n if (err instanceof ApiError) {\n const status = err.statusCode ?? 0;\n if (status === 404) {\n logger.warn(\n \"Endpoint '%s' not found, skipping type generation\",\n endpointName,\n );\n } else if (status === 403) {\n logger.warn(\n \"Access denied to endpoint '%s' schema, skipping type generation\",\n endpointName,\n );\n } else {\n logger.warn(\n \"Failed to fetch schema for '%s' (HTTP %d), skipping: %s\",\n endpointName,\n status,\n err.message,\n );\n }\n } else {\n logger.warn(\n \"Error fetching schema for '%s': %s\",\n endpointName,\n (err as Error).message,\n );\n }\n return null;\n }\n}\n"],"mappings":";;;;;AAGA,MAAM,SAAS,aAAa,iCAAiC;;;;;AA2C7D,eAAsB,mBACpB,QACA,cACA,aACwD;AACxD,KAAI;EACF,MAAM,WAAW,MAAM,OAAO,iBAAiB,WAAW,EACxD,MAAM,cACP,CAAC;AAEF,MAAI,CAAC,SAAS,UAAU;AACtB,UAAO,KACL,6DACA,aACD;AACD,UAAO;;EAGT,MAAM,OAAO,MAAM,IAAI,SAAS,SAAS,SAAS,CAAC,MAAM;EACzD,MAAM,UAAmB,KAAK,MAAM,KAAK;AAEzC,MACE,OAAO,YAAY,YACnB,YAAY,QACZ,EAAE,WAAW,YACb,OAAQ,QAAwB,UAAU,UAC1C;AACA,UAAO,KACL,uDACA,aACD;AACD,UAAO;;EAET,MAAM,OAAO;EAGb,MAAM,WAAW,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;AAC9C,MAAI,SAAS,WAAW,GAAG;AACzB,UAAO,KAAK,uCAAuC,aAAa;AAChE,UAAO;;EAGT,IAAI;AACJ,MAAI,aAAa;GACf,MAAM,QAAQ,SAAS,MAAM,MAAM,EAAE,SAAS,IAAI,YAAY,GAAG,CAAC;AAClE,OAAI,CAAC,OAAO;AACV,WAAO,KACL,oEACA,aACA,aACD;AACD,cAAU,SAAS;SAEnB,WAAU;QAGZ,WAAU,SAAS;AAGrB,SAAO;GAAE;GAAM;GAAS;UACjB,KAAK;AACZ,MAAI,eAAe,UAAU;GAC3B,MAAM,SAAS,IAAI,cAAc;AACjC,OAAI,WAAW,IACb,QAAO,KACL,qDACA,aACD;YACQ,WAAW,IACpB,QAAO,KACL,mEACA,aACD;OAED,QAAO,KACL,2DACA,cACA,QACA,IAAI,QACL;QAGH,QAAO,KACL,sCACA,cACC,IAAc,QAChB;AAEH,SAAO"}
1
+ {"version":3,"file":"fetcher.js","names":[],"sources":["../../../src/type-generator/serving/fetcher.ts"],"sourcesContent":["import { createLogger } from \"../../logging/logger\";\nimport { ApiError, type WorkspaceClient } from \"../../workspace-client\";\n\nconst logger = createLogger(\"type-generator:serving:fetcher\");\n\ninterface OpenApiSpec {\n openapi: string;\n info: { title: string; version: string };\n paths: Record<string, Record<string, OpenApiOperation>>;\n}\n\nexport interface OpenApiOperation {\n requestBody?: {\n content: {\n \"application/json\": {\n schema: OpenApiSchema;\n };\n };\n };\n responses?: Record<\n string,\n {\n content?: {\n \"application/json\": {\n schema: OpenApiSchema;\n };\n };\n }\n >;\n}\n\nexport interface OpenApiSchema {\n type?: string;\n properties?: Record<string, OpenApiSchema>;\n required?: string[];\n items?: OpenApiSchema;\n enum?: string[];\n nullable?: boolean;\n oneOf?: OpenApiSchema[];\n format?: string;\n}\n\n/**\n * Fetches the OpenAPI schema for a serving endpoint using the SDK.\n * Returns null if the endpoint is not found or access is denied.\n */\nexport async function fetchOpenApiSchema(\n client: WorkspaceClient,\n endpointName: string,\n servedModel?: string,\n): Promise<{ spec: OpenApiSpec; pathKey: string } | null> {\n try {\n const response = await client.servingEndpoints.getOpenApi({\n name: endpointName,\n });\n\n if (!response.contents) {\n logger.warn(\n \"Empty OpenAPI response for '%s', skipping type generation\",\n endpointName,\n );\n return null;\n }\n\n const text = await new Response(response.contents).text();\n const rawSpec: unknown = JSON.parse(text);\n\n if (\n typeof rawSpec !== \"object\" ||\n rawSpec === null ||\n !(\"paths\" in rawSpec) ||\n typeof (rawSpec as OpenApiSpec).paths !== \"object\"\n ) {\n logger.warn(\n \"Invalid OpenAPI schema structure for '%s', skipping\",\n endpointName,\n );\n return null;\n }\n const spec = rawSpec as OpenApiSpec;\n\n // Find the right path key\n const pathKeys = Object.keys(spec.paths ?? {});\n if (pathKeys.length === 0) {\n logger.warn(\"No paths in OpenAPI schema for '%s'\", endpointName);\n return null;\n }\n\n let pathKey: string;\n if (servedModel) {\n const match = pathKeys.find((k) => k.includes(`/${servedModel}/`));\n if (!match) {\n logger.warn(\n \"Served model '%s' not found in schema for '%s', using first path\",\n servedModel,\n endpointName,\n );\n pathKey = pathKeys[0];\n } else {\n pathKey = match;\n }\n } else {\n pathKey = pathKeys[0];\n }\n\n return { spec, pathKey };\n } catch (err) {\n if (err instanceof ApiError) {\n const status = err.statusCode ?? 0;\n if (status === 404) {\n logger.warn(\n \"Endpoint '%s' not found, skipping type generation\",\n endpointName,\n );\n } else if (status === 403) {\n logger.warn(\n \"Access denied to endpoint '%s' schema, skipping type generation\",\n endpointName,\n );\n } else {\n logger.warn(\n \"Failed to fetch schema for '%s' (HTTP %d), skipping: %s\",\n endpointName,\n status,\n err.message,\n );\n }\n } else {\n logger.warn(\n \"Error fetching schema for '%s': %s\",\n endpointName,\n (err as Error).message,\n );\n }\n return null;\n }\n}\n"],"mappings":";;;;AAGA,MAAM,SAAS,aAAa,iCAAiC;;;;;AA2C7D,eAAsB,mBACpB,QACA,cACA,aACwD;AACxD,KAAI;EACF,MAAM,WAAW,MAAM,OAAO,iBAAiB,WAAW,EACxD,MAAM,cACP,CAAC;AAEF,MAAI,CAAC,SAAS,UAAU;AACtB,UAAO,KACL,6DACA,aACD;AACD,UAAO;;EAGT,MAAM,OAAO,MAAM,IAAI,SAAS,SAAS,SAAS,CAAC,MAAM;EACzD,MAAM,UAAmB,KAAK,MAAM,KAAK;AAEzC,MACE,OAAO,YAAY,YACnB,YAAY,QACZ,EAAE,WAAW,YACb,OAAQ,QAAwB,UAAU,UAC1C;AACA,UAAO,KACL,uDACA,aACD;AACD,UAAO;;EAET,MAAM,OAAO;EAGb,MAAM,WAAW,OAAO,KAAK,KAAK,SAAS,EAAE,CAAC;AAC9C,MAAI,SAAS,WAAW,GAAG;AACzB,UAAO,KAAK,uCAAuC,aAAa;AAChE,UAAO;;EAGT,IAAI;AACJ,MAAI,aAAa;GACf,MAAM,QAAQ,SAAS,MAAM,MAAM,EAAE,SAAS,IAAI,YAAY,GAAG,CAAC;AAClE,OAAI,CAAC,OAAO;AACV,WAAO,KACL,oEACA,aACA,aACD;AACD,cAAU,SAAS;SAEnB,WAAU;QAGZ,WAAU,SAAS;AAGrB,SAAO;GAAE;GAAM;GAAS;UACjB,KAAK;AACZ,MAAI,eAAe,UAAU;GAC3B,MAAM,SAAS,IAAI,cAAc;AACjC,OAAI,WAAW,IACb,QAAO,KACL,qDACA,aACD;YACQ,WAAW,IACpB,QAAO,KACL,mEACA,aACD;OAED,QAAO,KACL,2DACA,cACA,QACA,IAAI,QACL;QAGH,QAAO,KACL,sCACA,cACC,IAAc,QAChB;AAEH,SAAO"}
@@ -1,6 +1,5 @@
1
+ import { createWorkspaceClient } from "../../shared/src/workspace-client/factory.js";
1
2
  import { createLogger } from "../../logging/logger.js";
2
- import { createWorkspaceClient } from "../../workspace-client/factory.js";
3
- import "../../workspace-client/index.js";
4
3
  import { migrateProjectConfig, removeOldGeneratedTypes, resolveProjectRoot } from "../migration.js";
5
4
  import { CACHE_VERSION, hashSchema, loadServingCache, saveServingCache } from "./cache.js";
6
5
  import { convertRequestSchema, convertResponseSchema, deriveChunkType, extractRequestKeys } from "./converter.js";
@@ -1 +1 @@
1
- {"version":3,"file":"generator.js","names":[],"sources":["../../../src/type-generator/serving/generator.ts"],"sourcesContent":["import fs from \"node:fs/promises\";\nimport path from \"node:path\";\nimport pc from \"picocolors\";\nimport { createLogger } from \"../../logging/logger\";\nimport type { EndpointConfig } from \"../../plugins/serving/types\";\nimport {\n createWorkspaceClient,\n type WorkspaceClient,\n} from \"../../workspace-client\";\nimport {\n migrateProjectConfig,\n removeOldGeneratedTypes,\n resolveProjectRoot,\n} from \"../migration\";\nimport {\n CACHE_VERSION,\n hashSchema,\n loadServingCache,\n type ServingCache,\n saveServingCache,\n} from \"./cache\";\nimport {\n convertRequestSchema,\n convertResponseSchema,\n deriveChunkType,\n extractRequestKeys,\n} from \"./converter\";\nimport { fetchOpenApiSchema } from \"./fetcher\";\nimport {\n extractServingEndpoints,\n findServerFile,\n} from \"./server-file-extractor\";\n\nconst logger = createLogger(\"type-generator:serving\");\n\nconst GENERIC_REQUEST = \"Record<string, unknown>\";\nconst GENERIC_RESPONSE = \"unknown\";\nconst GENERIC_CHUNK = \"unknown\";\n\ninterface GenerateServingTypesOptions {\n outFile: string;\n endpoints?: Record<string, EndpointConfig>;\n noCache?: boolean;\n}\n\n/**\n * Generates TypeScript type declarations for serving endpoints\n * by fetching their OpenAPI schemas and converting to TypeScript.\n *\n * Endpoint discovery order (when `endpoints` is not provided):\n * 1. AST extraction from server file (server/index.ts or server/server.ts)\n * 2. DATABRICKS_SERVING_ENDPOINT_NAME env var (single default endpoint)\n */\nexport async function generateServingTypes(\n options: GenerateServingTypesOptions,\n): Promise<void> {\n const { outFile, noCache } = options;\n const projectRoot = resolveProjectRoot(outFile);\n\n // Resolve endpoints: explicit > AST extraction from server file > env var fallback\n const endpoints =\n options.endpoints ??\n resolveEndpointsFromServerFile() ??\n resolveDefaultEndpoints();\n if (Object.keys(endpoints).length === 0) {\n logger.debug(\"No serving endpoints configured, skipping type generation\");\n return;\n }\n\n const startTime = performance.now();\n\n const cache = noCache\n ? { version: CACHE_VERSION, endpoints: {} }\n : await loadServingCache();\n\n let client: WorkspaceClient | undefined;\n let updated = false;\n\n const registryEntries: string[] = [];\n const logEntries: Array<{\n alias: string;\n status: \"HIT\" | \"MISS\";\n error?: string;\n }> = [];\n\n for (const [alias, config] of Object.entries(endpoints)) {\n client ??= createWorkspaceClient();\n const result = await processEndpoint(alias, config, client, cache);\n if (result.cacheUpdated) updated = true;\n registryEntries.push(result.entry);\n logEntries.push(result.log);\n }\n\n printLogTable(logEntries, startTime);\n\n const output = generateTypeDeclarations(registryEntries);\n await fs.mkdir(path.dirname(outFile), { recursive: true });\n await fs.writeFile(outFile, output, \"utf-8\");\n\n // One-time migration: remove old generated file and patch project configs\n await removeOldGeneratedTypes(projectRoot, \"appKitServingTypes.d.ts\");\n await migrateProjectConfig(projectRoot);\n\n if (registryEntries.length === 0) {\n logger.debug(\n \"Wrote empty serving types to %s (no endpoints resolved)\",\n outFile,\n );\n } else {\n logger.debug(\"Wrote serving types to %s\", outFile);\n }\n\n if (updated) {\n await saveServingCache(cache as ServingCache);\n }\n}\n\ninterface EndpointResult {\n entry: string;\n log: { alias: string; status: \"HIT\" | \"MISS\"; error?: string };\n cacheUpdated: boolean;\n}\n\nfunction genericEntry(alias: string): string {\n return buildRegistryEntry(\n alias,\n GENERIC_REQUEST,\n GENERIC_RESPONSE,\n GENERIC_CHUNK,\n );\n}\n\nasync function processEndpoint(\n alias: string,\n config: EndpointConfig,\n client: WorkspaceClient,\n cache: { endpoints: Record<string, any> },\n): Promise<EndpointResult> {\n const endpointName = process.env[config.env];\n if (!endpointName) {\n return {\n entry: genericEntry(alias),\n log: { alias, status: \"MISS\", error: `env ${config.env} not set` },\n cacheUpdated: false,\n };\n }\n\n const result = await fetchOpenApiSchema(\n client,\n endpointName,\n config.servedModel,\n );\n if (!result) {\n return {\n entry: genericEntry(alias),\n log: { alias, status: \"MISS\", error: \"schema fetch failed\" },\n cacheUpdated: false,\n };\n }\n\n const { spec, pathKey } = result;\n const hash = hashSchema(JSON.stringify(spec));\n\n // Cache hit\n const cached = cache.endpoints[alias];\n if (cached && cached.hash === hash) {\n return {\n entry: buildRegistryEntry(\n alias,\n cached.requestType,\n cached.responseType,\n cached.chunkType,\n ),\n log: { alias, status: \"HIT\" },\n cacheUpdated: false,\n };\n }\n\n // Cache miss — convert schema to types\n const operation = spec.paths[pathKey]?.post;\n if (!operation) {\n return {\n entry: genericEntry(alias),\n log: { alias, status: \"MISS\", error: \"no POST operation\" },\n cacheUpdated: false,\n };\n }\n\n try {\n const requestType = convertRequestSchema(operation);\n const responseType = convertResponseSchema(operation);\n const chunkType = deriveChunkType(operation);\n const requestKeys = extractRequestKeys(operation);\n\n cache.endpoints[alias] = {\n hash,\n requestType,\n responseType,\n chunkType,\n requestKeys,\n };\n\n return {\n entry: buildRegistryEntry(alias, requestType, responseType, chunkType),\n log: { alias, status: \"MISS\" },\n cacheUpdated: true,\n };\n } catch (convErr) {\n logger.warn(\n \"Schema conversion failed for '%s': %s\",\n alias,\n (convErr as Error).message,\n );\n return {\n entry: genericEntry(alias),\n log: { alias, status: \"MISS\", error: \"schema conversion failed\" },\n cacheUpdated: false,\n };\n }\n}\n\nfunction printLogTable(\n logEntries: Array<{ alias: string; status: \"HIT\" | \"MISS\"; error?: string }>,\n startTime: number,\n): void {\n if (logEntries.length === 0) return;\n\n const maxNameLen = Math.max(...logEntries.map((e) => e.alias.length));\n const separator = pc.dim(\"─\".repeat(50));\n console.log(\"\");\n console.log(\n ` ${pc.bold(\"Typegen Serving\")} ${pc.dim(`(${logEntries.length})`)}`,\n );\n console.log(` ${separator}`);\n for (const entry of logEntries) {\n const tag =\n entry.status === \"HIT\"\n ? `cache ${pc.bold(pc.green(\"HIT \"))}`\n : `cache ${pc.bold(pc.yellow(\"MISS \"))}`;\n const rawName = entry.alias.padEnd(maxNameLen);\n const reason = entry.error ? ` ${pc.dim(entry.error)}` : \"\";\n console.log(` ${tag} ${rawName}${reason}`);\n }\n const elapsed = ((performance.now() - startTime) / 1000).toFixed(2);\n const newCount = logEntries.filter((e) => e.status === \"MISS\").length;\n const cacheCount = logEntries.filter((e) => e.status === \"HIT\").length;\n console.log(` ${separator}`);\n console.log(\n ` ${newCount} new, ${cacheCount} from cache. ${pc.dim(`${elapsed}s`)}`,\n );\n console.log(\"\");\n}\n\nfunction resolveEndpointsFromServerFile():\n | Record<string, EndpointConfig>\n | undefined {\n try {\n const serverFile = findServerFile(process.cwd());\n if (!serverFile) return undefined;\n return extractServingEndpoints(serverFile) ?? undefined;\n } catch (error) {\n logger.debug(\n \"Failed to extract endpoints from server file: %s\",\n (error as Error).message,\n );\n return undefined;\n }\n}\n\nfunction resolveDefaultEndpoints(): Record<string, EndpointConfig> {\n if (process.env.DATABRICKS_SERVING_ENDPOINT_NAME) {\n return { default: { env: \"DATABRICKS_SERVING_ENDPOINT_NAME\" } };\n }\n return {};\n}\n\nfunction buildRegistryEntry(\n alias: string,\n requestType: string,\n responseType: string,\n chunkType: string | null,\n): string {\n const indent = \" \";\n const chunkEntry = chunkType ? chunkType : \"unknown\";\n return ` ${alias}: {\n${indent}request: ${indentType(requestType, indent)};\n${indent}response: ${indentType(responseType, indent)};\n${indent}chunk: ${indentType(chunkEntry, indent)};\n };`;\n}\n\nfunction indentType(typeStr: string, baseIndent: string): string {\n if (!typeStr.includes(\"\\n\")) return typeStr;\n return typeStr\n .split(\"\\n\")\n .map((line, i) => (i === 0 ? line : `${baseIndent}${line}`))\n .join(\"\\n\");\n}\n\nfunction generateTypeDeclarations(entries: string[]): string {\n return `// Auto-generated by AppKit - DO NOT EDIT\n// Generated from serving endpoint OpenAPI schemas\nimport \"@databricks/appkit\";\nimport \"@databricks/appkit-ui/react\";\n\ndeclare module \"@databricks/appkit\" {\n interface ServingEndpointRegistry {\n${entries.join(\"\\n\")}\n }\n}\n\ndeclare module \"@databricks/appkit-ui/react\" {\n interface ServingEndpointRegistry {\n${entries.join(\"\\n\")}\n }\n}\n`;\n}\n"],"mappings":";;;;;;;;;;;;;AAiCA,MAAM,SAAS,aAAa,yBAAyB;AAErD,MAAM,kBAAkB;AACxB,MAAM,mBAAmB;AACzB,MAAM,gBAAgB;;;;;;;;;AAgBtB,eAAsB,qBACpB,SACe;CACf,MAAM,EAAE,SAAS,YAAY;CAC7B,MAAM,cAAc,mBAAmB,QAAQ;CAG/C,MAAM,YACJ,QAAQ,aACR,gCAAgC,IAChC,yBAAyB;AAC3B,KAAI,OAAO,KAAK,UAAU,CAAC,WAAW,GAAG;AACvC,SAAO,MAAM,4DAA4D;AACzE;;CAGF,MAAM,YAAY,YAAY,KAAK;CAEnC,MAAM,QAAQ,UACV;EAAE,SAAS;EAAe,WAAW,EAAE;EAAE,GACzC,MAAM,kBAAkB;CAE5B,IAAI;CACJ,IAAI,UAAU;CAEd,MAAM,kBAA4B,EAAE;CACpC,MAAM,aAID,EAAE;AAEP,MAAK,MAAM,CAAC,OAAO,WAAW,OAAO,QAAQ,UAAU,EAAE;AACvD,aAAW,uBAAuB;EAClC,MAAM,SAAS,MAAM,gBAAgB,OAAO,QAAQ,QAAQ,MAAM;AAClE,MAAI,OAAO,aAAc,WAAU;AACnC,kBAAgB,KAAK,OAAO,MAAM;AAClC,aAAW,KAAK,OAAO,IAAI;;AAG7B,eAAc,YAAY,UAAU;CAEpC,MAAM,SAAS,yBAAyB,gBAAgB;AACxD,OAAM,GAAG,MAAM,KAAK,QAAQ,QAAQ,EAAE,EAAE,WAAW,MAAM,CAAC;AAC1D,OAAM,GAAG,UAAU,SAAS,QAAQ,QAAQ;AAG5C,OAAM,wBAAwB,aAAa,0BAA0B;AACrE,OAAM,qBAAqB,YAAY;AAEvC,KAAI,g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AAS,iCAEK;AACZ,KAAI;EACF,MAAM,aAAa,eAAe,QAAQ,KAAK,CAAC;AAChD,MAAI,CAAC,WAAY,QAAO;AACxB,SAAO,wBAAwB,WAAW,IAAI;UACvC,OAAO;AACd,SAAO,MACL,oDACC,MAAgB,QAClB;AACD;;;AAIJ,SAAS,0BAA0D;AACjE,KAAI,QAAQ,IAAI,iCACd,QAAO,EAAE,SAAS,EAAE,KAAK,oCAAoC,EAAE;AAEjE,QAAO,EAAE;;AAGX,SAAS,mBACP,OACA,aACA,cACA,WACQ;CACR,MAAM,SAAS;CACf,MAAM,aAAa,YAAY,YAAY;AAC3C,QAAO,OAAO,MAAM;EACpB,OAAO,WAAW,WAAW,aAAa,OAAO,CAAC;EAClD,OAAO,YAAY,WAAW,cAAc,OAAO,CAAC;EACpD,OAAO,SAAS,WAAW,YAAY,OAAO,CAAC;;;AAIjD,SAAS,WAAW,SAAiB,YAA4B;AAC/D,KAAI,CAAC,QAAQ,SAAS,KAAK,CAAE,QAAO;AACpC,QAAO,QACJ,MAAM,KAAK,CACX,KAAK,MAAM,MAAO,MAAM,IAAI,OAAO,GAAG,aAAa,OAAQ,CAC3D,KAAK,KAAK;;AAGf,SAAS,yBAAyB,SAA2B;AAC3D,QAAO;;;;;;;EAOP,QAAQ,KAAK,KAAK,CAAC;;;;;;EAMnB,QAAQ,KAAK,KAAK,CAAC"}
1
+ {"version":3,"file":"generator.js","names":[],"sources":["../../../src/type-generator/serving/generator.ts"],"sourcesContent":["import fs from \"node:fs/promises\";\nimport path from \"node:path\";\nimport pc from \"picocolors\";\nimport { createLogger } from \"../../logging/logger\";\nimport type { EndpointConfig } from \"../../plugins/serving/types\";\nimport {\n createWorkspaceClient,\n type WorkspaceClient,\n} from \"../../workspace-client\";\nimport {\n migrateProjectConfig,\n removeOldGeneratedTypes,\n resolveProjectRoot,\n} from \"../migration\";\nimport {\n CACHE_VERSION,\n hashSchema,\n loadServingCache,\n type ServingCache,\n saveServingCache,\n} from \"./cache\";\nimport {\n convertRequestSchema,\n convertResponseSchema,\n deriveChunkType,\n extractRequestKeys,\n} from \"./converter\";\nimport { fetchOpenApiSchema } from \"./fetcher\";\nimport {\n extractServingEndpoints,\n findServerFile,\n} from \"./server-file-extractor\";\n\nconst logger = createLogger(\"type-generator:serving\");\n\nconst GENERIC_REQUEST = \"Record<string, unknown>\";\nconst GENERIC_RESPONSE = \"unknown\";\nconst GENERIC_CHUNK = \"unknown\";\n\ninterface GenerateServingTypesOptions {\n outFile: string;\n endpoints?: Record<string, EndpointConfig>;\n noCache?: boolean;\n}\n\n/**\n * Generates TypeScript type declarations for serving endpoints\n * by fetching their OpenAPI schemas and converting to TypeScript.\n *\n * Endpoint discovery order (when `endpoints` is not provided):\n * 1. AST extraction from server file (server/index.ts or server/server.ts)\n * 2. DATABRICKS_SERVING_ENDPOINT_NAME env var (single default endpoint)\n */\nexport async function generateServingTypes(\n options: GenerateServingTypesOptions,\n): Promise<void> {\n const { outFile, noCache } = options;\n const projectRoot = resolveProjectRoot(outFile);\n\n // Resolve endpoints: explicit > AST extraction from server file > env var fallback\n const endpoints =\n options.endpoints ??\n resolveEndpointsFromServerFile() ??\n resolveDefaultEndpoints();\n if (Object.keys(endpoints).length === 0) {\n logger.debug(\"No serving endpoints configured, skipping type generation\");\n return;\n }\n\n const startTime = performance.now();\n\n const cache = noCache\n ? { version: CACHE_VERSION, endpoints: {} }\n : await loadServingCache();\n\n let client: WorkspaceClient | undefined;\n let updated = false;\n\n const registryEntries: string[] = [];\n const logEntries: Array<{\n alias: string;\n status: \"HIT\" | \"MISS\";\n error?: string;\n }> = [];\n\n for (const [alias, config] of Object.entries(endpoints)) {\n client ??= createWorkspaceClient();\n const result = await processEndpoint(alias, config, client, cache);\n if (result.cacheUpdated) updated = true;\n registryEntries.push(result.entry);\n logEntries.push(result.log);\n }\n\n printLogTable(logEntries, startTime);\n\n const output = generateTypeDeclarations(registryEntries);\n await fs.mkdir(path.dirname(outFile), { recursive: true });\n await fs.writeFile(outFile, output, \"utf-8\");\n\n // One-time migration: remove old generated file and patch project configs\n await removeOldGeneratedTypes(projectRoot, \"appKitServingTypes.d.ts\");\n await migrateProjectConfig(projectRoot);\n\n if (registryEntries.length === 0) {\n logger.debug(\n \"Wrote empty serving types to %s (no endpoints resolved)\",\n outFile,\n );\n } else {\n logger.debug(\"Wrote serving types to %s\", outFile);\n }\n\n if (updated) {\n await saveServingCache(cache as ServingCache);\n }\n}\n\ninterface EndpointResult {\n entry: string;\n log: { alias: string; status: \"HIT\" | \"MISS\"; error?: string };\n cacheUpdated: boolean;\n}\n\nfunction genericEntry(alias: string): string {\n return buildRegistryEntry(\n alias,\n GENERIC_REQUEST,\n GENERIC_RESPONSE,\n GENERIC_CHUNK,\n );\n}\n\nasync function processEndpoint(\n alias: string,\n config: EndpointConfig,\n client: WorkspaceClient,\n cache: { endpoints: Record<string, any> },\n): Promise<EndpointResult> {\n const endpointName = process.env[config.env];\n if (!endpointName) {\n return {\n entry: genericEntry(alias),\n log: { alias, status: \"MISS\", error: `env ${config.env} not set` },\n cacheUpdated: false,\n };\n }\n\n const result = await fetchOpenApiSchema(\n client,\n endpointName,\n config.servedModel,\n );\n if (!result) {\n return {\n entry: genericEntry(alias),\n log: { alias, status: \"MISS\", error: \"schema fetch failed\" },\n cacheUpdated: false,\n };\n }\n\n const { spec, pathKey } = result;\n const hash = hashSchema(JSON.stringify(spec));\n\n // Cache hit\n const cached = cache.endpoints[alias];\n if (cached && cached.hash === hash) {\n return {\n entry: buildRegistryEntry(\n alias,\n cached.requestType,\n cached.responseType,\n cached.chunkType,\n ),\n log: { alias, status: \"HIT\" },\n cacheUpdated: false,\n };\n }\n\n // Cache miss — convert schema to types\n const operation = spec.paths[pathKey]?.post;\n if (!operation) {\n return {\n entry: genericEntry(alias),\n log: { alias, status: \"MISS\", error: \"no POST operation\" },\n cacheUpdated: false,\n };\n }\n\n try {\n const requestType = convertRequestSchema(operation);\n const responseType = convertResponseSchema(operation);\n const chunkType = deriveChunkType(operation);\n const requestKeys = extractRequestKeys(operation);\n\n cache.endpoints[alias] = {\n hash,\n requestType,\n responseType,\n chunkType,\n requestKeys,\n };\n\n return {\n entry: buildRegistryEntry(alias, requestType, responseType, chunkType),\n log: { alias, status: \"MISS\" },\n cacheUpdated: true,\n };\n } catch (convErr) {\n logger.warn(\n \"Schema conversion failed for '%s': %s\",\n alias,\n (convErr as Error).message,\n );\n return {\n entry: genericEntry(alias),\n log: { alias, status: \"MISS\", error: \"schema conversion failed\" },\n cacheUpdated: false,\n };\n }\n}\n\nfunction printLogTable(\n logEntries: Array<{ alias: string; status: \"HIT\" | \"MISS\"; error?: string }>,\n startTime: number,\n): void {\n if (logEntries.length === 0) return;\n\n const maxNameLen = Math.max(...logEntries.map((e) => e.alias.length));\n const separator = pc.dim(\"─\".repeat(50));\n console.log(\"\");\n console.log(\n ` ${pc.bold(\"Typegen Serving\")} ${pc.dim(`(${logEntries.length})`)}`,\n );\n console.log(` ${separator}`);\n for (const entry of logEntries) {\n const tag =\n entry.status === \"HIT\"\n ? `cache ${pc.bold(pc.green(\"HIT \"))}`\n : `cache ${pc.bold(pc.yellow(\"MISS \"))}`;\n const rawName = entry.alias.padEnd(maxNameLen);\n const reason = entry.error ? ` ${pc.dim(entry.error)}` : \"\";\n console.log(` ${tag} ${rawName}${reason}`);\n }\n const elapsed = ((performance.now() - startTime) / 1000).toFixed(2);\n const newCount = logEntries.filter((e) => e.status === \"MISS\").length;\n const cacheCount = logEntries.filter((e) => e.status === \"HIT\").length;\n console.log(` ${separator}`);\n console.log(\n ` ${newCount} new, ${cacheCount} from cache. ${pc.dim(`${elapsed}s`)}`,\n );\n console.log(\"\");\n}\n\nfunction resolveEndpointsFromServerFile():\n | Record<string, EndpointConfig>\n | undefined {\n try {\n const serverFile = findServerFile(process.cwd());\n if (!serverFile) return undefined;\n return extractServingEndpoints(serverFile) ?? undefined;\n } catch (error) {\n logger.debug(\n \"Failed to extract endpoints from server file: %s\",\n (error as Error).message,\n );\n return undefined;\n }\n}\n\nfunction resolveDefaultEndpoints(): Record<string, EndpointConfig> {\n if (process.env.DATABRICKS_SERVING_ENDPOINT_NAME) {\n return { default: { env: \"DATABRICKS_SERVING_ENDPOINT_NAME\" } };\n }\n return {};\n}\n\nfunction buildRegistryEntry(\n alias: string,\n requestType: string,\n responseType: string,\n chunkType: string | null,\n): string {\n const indent = \" \";\n const chunkEntry = chunkType ? chunkType : \"unknown\";\n return ` ${alias}: {\n${indent}request: ${indentType(requestType, indent)};\n${indent}response: ${indentType(responseType, indent)};\n${indent}chunk: ${indentType(chunkEntry, indent)};\n };`;\n}\n\nfunction indentType(typeStr: string, baseIndent: string): string {\n if (!typeStr.includes(\"\\n\")) return typeStr;\n return typeStr\n .split(\"\\n\")\n .map((line, i) => (i === 0 ? line : `${baseIndent}${line}`))\n .join(\"\\n\");\n}\n\nfunction generateTypeDeclarations(entries: string[]): string {\n return `// Auto-generated by AppKit - DO NOT EDIT\n// Generated from serving endpoint OpenAPI schemas\nimport \"@databricks/appkit\";\nimport \"@databricks/appkit-ui/react\";\n\ndeclare module \"@databricks/appkit\" {\n interface ServingEndpointRegistry {\n${entries.join(\"\\n\")}\n }\n}\n\ndeclare module \"@databricks/appkit-ui/react\" {\n interface ServingEndpointRegistry {\n${entries.join(\"\\n\")}\n }\n}\n`;\n}\n"],"mappings":";;;;;;;;;;;;AAiCA,MAAM,SAAS,aAAa,yBAAyB;AAErD,MAAM,kBAAkB;AACxB,MAAM,mBAAmB;AACzB,MAAM,gBAAgB;;;;;;;;;AAgBtB,eAAsB,qBACpB,SACe;CACf,MAAM,EAAE,SAAS,YAAY;CAC7B,MAAM,cAAc,mBAAmB,QAAQ;CAG/C,MAAM,YACJ,QAAQ,aACR,gCAAgC,IAChC,yBAAyB;AAC3B,KAAI,OAAO,KAAK,UAAU,CAAC,WAAW,GAAG;AACvC,SAAO,MAAM,4DAA4D;AACzE;;CAGF,MAAM,YAAY,YAAY,KAAK;CAEnC,MAAM,QAAQ,UACV;EAA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KAAK,CAAE,QAAO;AACpC,QAAO,QACJ,MAAM,KAAK,CACX,KAAK,MAAM,MAAO,MAAM,IAAI,OAAO,GAAG,aAAa,OAAQ,CAC3D,KAAK,KAAK;;AAGf,SAAS,yBAAyB,SAA2B;AAC3D,QAAO;;;;;;;EAOP,QAAQ,KAAK,KAAK,CAAC;;;;;;EAMnB,QAAQ,KAAK,KAAK,CAAC"}
@@ -1,6 +1,5 @@
1
+ import { createWorkspaceClient } from "../shared/src/workspace-client/factory.js";
1
2
  import { createLogger } from "../logging/logger.js";
2
- import { createWorkspaceClient } from "../workspace-client/factory.js";
3
- import "../workspace-client/index.js";
4
3
  import { METRIC_CONFIG_FILE } from "../shared/src/schemas/metric-fqn.js";
5
4
  import { getWarehouseState, startWarehouse, waitUntilRunning } from "./warehouse-status.js";
6
5
  import { ANALYTICS_TYPES_FILE, TYPES_DIR, TypegenFatalError, TypegenSyntaxError, generateFromEntryPoint } from "./index.js";
@@ -1 +1 @@
1
- {"version":3,"file":"vite-plugin.js","names":[],"sources":["../../src/type-generator/vite-plugin.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport path from \"node:path\";\nimport type { Plugin } from \"vite\";\nimport { METRIC_CONFIG_FILE } from \"../../../shared/src/schemas/metric-fqn\";\nimport { createLogger } from \"../logging/logger\";\nimport { createWorkspaceClient } from \"../workspace-client\";\nimport {\n ANALYTICS_TYPES_FILE,\n generateFromEntryPoint,\n TYPES_DIR,\n TypegenFatalError,\n TypegenSyntaxError,\n} from \"./index\";\nimport type { PreflightMode } from \"./preflight\";\nimport {\n getWarehouseState,\n startWarehouse,\n waitUntilRunning,\n} from \"./warehouse-status\";\n\nconst logger = createLogger(\"type-generator:vite-plugin\");\n\n/**\n * How long the DEV background watcher waits for a STARTING warehouse to reach\n * RUNNING before giving up. Short relative to the CLI's preflight budget: this\n * is a best-effort \"regenerate once the warehouse warms up\" convenience, not a\n * gate, so we'd rather stop polling than hold a detached task open for minutes.\n */\nconst DEV_WAREHOUSE_WATCH_MAX_MS = 60_000;\n\n/**\n * Options for the AppKit types plugin.\n */\ninterface AppKitTypesPluginOptions {\n /* Path to the output d.ts file (relative to client folder). */\n outFile?: string;\n /**\n * Path to the metric registry `.ts` file (relative to client folder).\n * Defaults to a sibling of `outFile`, computed by the generator. The\n * generated source carries both the `declare module` augmentation and the\n * runtime `metricViewsMetadata` const, so it is a real `.ts`, not a `.d.ts`.\n */\n mvOutFile?: string;\n /**\n * Folders to watch for changes. Defaults to `config/queries` and\n * `config/metric-views`. When overridden, include a `queries` folder and/or a\n * `metric-views` folder — they are resolved by their trailing path segment.\n */\n watchFolders?: string[];\n}\n\n/**\n * Vite plugin to generate types for AppKit queries.\n * Calls generateFromEntryPoint under the hood.\n * @param options - Options to override default values.\n * @returns Vite plugin to generate types for AppKit queries.\n */\nexport function appKitTypesPlugin(options?: AppKitTypesPluginOptions): Plugin {\n let outFile: string;\n let mvOutFile: string | undefined;\n let watchFolders: string[];\n // The queries + metric-views config folders, resolved in `configResolved`.\n // Passed explicitly into generateFromEntryPoint so neither is inferred from\n // `watchFolders` ordering (which used to assume queries was `watchFolders[0]`).\n let queryFolder: string | undefined;\n let metricViewsFolder: string | undefined;\n\n // Single-flight state for runGenerate(). `inFlight` is the promise of the\n // currently-running drain (null when idle); `queued` records that a trigger\n // arrived while a run was active so exactly ONE trailing run fires afterwards\n // (latest-wins — coalesces any number of overlapping triggers into a single\n // rerun). `queued` is read/cleared synchronously inside the drain loop so a\n // trigger landing in any window is caught before the drain exits.\n //\n // `pendingMode` is the mode the next generate should run in (latest-wins, like\n // `queued`): the foreground build runs non-blocking in dev (instant degrade)\n // while the background warehouse watch runs blocking (real DESCRIBEs). A\n // blocking watch trigger that lands while a non-blocking foreground run is in\n // flight therefore still describes when its trailing run fires.\n let inFlight: Promise<void> | null = null;\n let queued = false;\n let pendingMode: PreflightMode = \"non-blocking\";\n\n // The currently-armed DEV background warehouse watch, if any. Aborting it\n // stops a pending waitUntilRunning (server shutdown, or a newer arm replacing\n // an older one).\n let watchController: AbortController | null = null;\n\n /**\n * Generate types once in the given preflight {@link PreflightMode}. Never\n * throws in dev (logs instead); in production it rethrows so the build fails.\n * This is the un-guarded core — callers should go through {@link runGenerate}\n * so concurrent triggers can't race-write the .d.ts.\n *\n * @param mode - preflight policy for this run. The foreground build passes a\n * NODE_ENV-derived mode (blocking in production, non-blocking in dev so it\n * degrades instantly); the background warehouse watch passes \"blocking\" so\n * its regenerate actually DESCRIBEs and lands real (non-degraded) types.\n */\n async function generateOnce(mode: PreflightMode) {\n try {\n const warehouseId = process.env.DATABRICKS_WAREHOUSE_ID || \"\";\n\n if (!warehouseId) {\n logger.debug(\"Warehouse ID not found. Skipping type generation.\");\n return;\n }\n\n await generateFromEntryPoint({\n outFile,\n queryFolder,\n metricViewsFolder,\n warehouseId,\n noCache: false,\n mode,\n mvOutFile,\n });\n } catch (error) {\n // TypegenSyntaxError / TypegenFatalError carry a complete, actionable\n // report in their message. Their stack frames and attached query arrays\n // point into appkit internals and only add noise, so surface just the\n // message — both when failing the prod build and when logging in dev.\n const isTypegenError =\n error instanceof TypegenSyntaxError ||\n error instanceof TypegenFatalError;\n\n // throw in production to fail the build\n if (process.env.NODE_ENV === \"production\") {\n if (isTypegenError) error.stack = error.message;\n throw error;\n }\n\n if (isTypegenError) {\n logger.error(\"%s\", error.message);\n } else {\n logger.error(\"Error generating types: %O\", error);\n }\n }\n }\n\n /**\n * Single-flight wrapper around {@link generateOnce}. The initial build, the\n * .sql watcher, and the DEV warehouse watch all route through here so they can\n * never run typegen concurrently (which would race-write the .d.ts).\n *\n * If a run is already in flight, this does NOT start a second one — it records\n * the requested mode and sets a trailing flag so exactly one more run fires\n * after the current finishes, coalescing any number of overlapping triggers\n * (latest-wins, including the mode: a blocking watch trigger that arrives mid\n * non-blocking foreground run still describes when its trailing run fires).\n *\n * @param mode - preflight policy for this run. Recorded into `pendingMode`,\n * which the drain reads for each generate (latest trigger wins).\n * @returns A promise that resolves when this trigger's work (including any\n * trailing run it scheduled) has completed.\n */\n function runGenerate(mode: PreflightMode): Promise<void> {\n pendingMode = mode;\n\n if (inFlight) {\n // A run is active: remember that another trigger arrived and ride out the\n // current run. One trailing run then covers all coalesced triggers and\n // runs in the latest requested mode (recorded above).\n queued = true;\n return inFlight;\n }\n\n // Drain in a loop rather than recursing after a single queued-check: a\n // trigger can land in the window between generateOnce() resolving and the\n // check, so we re-test `queued` until it's clear. Critically, `inFlight` is\n // cleared synchronously in the SAME tick as the final `queued === false`\n // observation — never deferred to a .finally microtask — so there's no\n // window where a trigger sees `inFlight` set but the drain has already\n // decided to exit. The guard stays held for the whole drain, so concurrent\n // triggers only ever set the flag; they never start a parallel generate.\n const drain = async (): Promise<void> => {\n while (true) {\n queued = false;\n // Snapshot the mode synchronously alongside clearing `queued` so a\n // trigger landing during this generate is observed (via `queued`) on the\n // next loop with its own mode, not silently dropped.\n const runMode = pendingMode;\n await generateOnce(runMode);\n // Synchronous check + clear, atomic w.r.t. other (synchronous) callers.\n if (!queued) {\n inFlight = null;\n return;\n }\n }\n };\n\n inFlight = drain();\n return inFlight;\n }\n\n /**\n * DEV-only: get the warehouse to RUNNING in the background and regenerate with\n * real (non-degraded) types once it is — without blocking dev startup. The\n * foreground build only ever degrades in dev (instant `unknown`/cached types),\n * so this is what lands actual DESCRIBE results in the editor for EVERY\n * reachable warehouse state, not just one that happens to already be warm.\n *\n * Post-probe behaviour by state:\n * - RUNNING → describe right away (the dev foreground degraded, so a running\n * warehouse would otherwise never get real types). `waitUntilRunning`\n * returns immediately for an already-running warehouse, then the blocking\n * regenerate fires.\n * - STARTING → it's already coming up; just wait for RUNNING, then describe.\n * - STOPPED / STOPPING → kick off a start, wait for RUNNING, then describe.\n * - DELETED / DELETING → return (a deleted warehouse can't be started, and\n * blocking typegen would treat it as fatal); leave the degraded types.\n *\n * No-op in production or without a warehouse id. Replaces any previously-armed\n * watch (aborting it first). Fully self-contained: it never throws into the\n * caller and never re-arms itself. The whole lifecycle is abortable via the\n * shared {@link watchController} — its signal is threaded into\n * `waitUntilRunning`, so a dev-server shutdown cancels a pending wait — and the\n * regenerate routes through {@link runGenerate} so it can't race-write the\n * .d.ts with the foreground degrade or a `.sql` re-trigger.\n *\n * The regenerate runs in \"blocking\" mode (not the foreground's non-blocking)\n * so it actually DESCRIBEs the now-RUNNING warehouse and lands real types —\n * the whole point of warming the warehouse in the background.\n */\n function armWarehouseWatch(): void {\n if (process.env.NODE_ENV === \"production\") return;\n\n const warehouseId = process.env.DATABRICKS_WAREHOUSE_ID || \"\";\n if (!warehouseId) return;\n\n // Supersede any in-flight watch so we never run two concurrently.\n watchController?.abort();\n const controller = new AbortController();\n watchController = controller;\n const { signal } = controller;\n\n void (async () => {\n try {\n const client = createWorkspaceClient();\n const state = await getWarehouseState(client, warehouseId);\n\n // A deleted/deleting warehouse can't be started and blocking typegen\n // would treat it as fatal — leave the degraded types and stop. Every\n // other state (including RUNNING) proceeds to wait-then-describe so the\n // dev editor gets real types, not just the foreground's degraded ones.\n if (state === \"DELETED\" || state === \"DELETING\") {\n return;\n }\n\n // Stopped/stopping won't reach RUNNING on its own — nudge it. RUNNING and\n // STARTING need no start (RUNNING is already up; STARTING is coming up),\n // so don't issue a redundant one. A failed start is non-fatal: give up\n // silently rather than throw out of the detached task (the developer\n // still has degraded/cached types).\n let startedByUs = false;\n if (state === \"STOPPED\" || state === \"STOPPING\") {\n try {\n logger.debug(\"Warehouse is %s; starting it.\", state);\n await startWarehouse(client, warehouseId);\n startedByUs = true;\n } catch {\n return;\n }\n }\n\n // Wait for RUNNING. For an already-RUNNING warehouse this returns on the\n // first poll; for STARTING/STOPPED it polls (abortably) until the\n // warehouse warms up, a terminal state, or the deadline.\n const final = await waitUntilRunning(client, warehouseId, {\n maxMs: DEV_WAREHOUSE_WATCH_MAX_MS,\n signal,\n // We just issued the start, so the first poll(s) often still report\n // STOPPED/STOPPING before the start propagates. Poll through those\n // instead of bailing, or the regenerate would never fire. When we\n // didn't start it (RUNNING/STARTING branch), keep the default terminal\n // states.\n treatStoppedAsTransient: startedByUs,\n });\n\n if (final === \"RUNNING\" && !signal.aborted) {\n logger.debug(\"Warehouse is RUNNING; regenerating types.\");\n // Blocking: the warehouse is RUNNING now, so describe it and emit real\n // (non-degraded) types — unlike the foreground dev run, which degraded.\n // Routed through the single-flight guard so it coalesces with the\n // foreground degrade / any `.sql` re-trigger instead of racing them.\n await runGenerate(\"blocking\");\n }\n } catch {\n // Detached background task: any failure (timeout, abort, connectivity,\n // auth) is non-fatal — the developer still has degraded/cached types.\n }\n })();\n }\n\n return {\n name: \"appkit-types\",\n\n apply() {\n const warehouseId = process.env.DATABRICKS_WAREHOUSE_ID || \"\";\n\n if (!warehouseId) {\n logger.debug(\"Warehouse ID not found. Skipping type generation.\");\n return false;\n }\n\n // Run when either config surface exists. Metric-view types are\n // independent of `.sql` queries, so a metric-only project (a\n // `config/metric-views/` with no `config/queries/`) must still activate\n // the plugin.\n const hasQueries = existsSync(\n path.join(process.cwd(), \"config\", \"queries\"),\n );\n const hasMetricViews = existsSync(\n path.join(process.cwd(), \"config\", \"metric-views\"),\n );\n if (!hasQueries && !hasMetricViews) {\n return false;\n }\n\n return true;\n },\n\n configResolved(config) {\n const projectRoot = path.resolve(config.root, \"..\");\n outFile = path.resolve(\n projectRoot,\n options?.outFile ?? `shared/${TYPES_DIR}/${ANALYTICS_TYPES_FILE}`,\n );\n // The metric out-path resolves against projectRoot only when explicitly\n // provided; an unset option passes through as undefined so the generator\n // computes its sibling-of-outFile default. In the all-defaults case the\n // final path is identical (the default outFile above lives in\n // shared/<TYPES_DIR>/), and a customized outFile now keeps its metric\n // sibling next to it instead of pinning it under shared/.\n mvOutFile =\n options?.mvOutFile !== undefined\n ? path.resolve(projectRoot, options.mvOutFile)\n : undefined;\n\n const defaultQueryFolder = path.join(process.cwd(), \"config\", \"queries\");\n const defaultMetricViewsFolder = path.join(\n process.cwd(),\n \"config\",\n \"metric-views\",\n );\n watchFolders = options?.watchFolders ?? [\n defaultQueryFolder,\n defaultMetricViewsFolder,\n ];\n\n // Resolve the two config folders explicitly rather than assuming a\n // position in `watchFolders`. With a custom `watchFolders`, match by the\n // trailing segment; otherwise use the computed defaults.\n if (options?.watchFolders) {\n queryFolder = watchFolders.find((f) => path.basename(f) === \"queries\");\n metricViewsFolder = watchFolders.find(\n (f) => path.basename(f) === \"metric-views\",\n );\n } else {\n queryFolder = defaultQueryFolder;\n metricViewsFolder = defaultMetricViewsFolder;\n }\n },\n\n buildStart() {\n // Production: block the build on this generate (and surface failures).\n // The watch is a dev-only no-op, so just run typegen.\n if (process.env.NODE_ENV === \"production\") {\n return runGenerate(\"blocking\");\n }\n\n // Dev: don't block startup waiting on typegen. The foreground generate runs\n // non-blocking — it skips the warehouse entirely and writes degraded\n // (cached/`unknown`) types instantly. Then arm the warehouse watch so the\n // warehouse gets a one-shot BLOCKING regenerate (real types) in the\n // background for EVERY reachable state: RUNNING describes right away, while\n // STARTING/STOPPED are waited (and started) until they reach RUNNING.\n void runGenerate(\"non-blocking\");\n armWarehouseWatch();\n },\n\n configureServer(server) {\n server.watcher.add(watchFolders);\n\n server.watcher.on(\"change\", (changedFile) => {\n const isWatchedFile = watchFolders.some((folder) =>\n changedFile.startsWith(folder),\n );\n\n // The metric config is `definitions.json` — a far more generic name\n // than the old `metric-views.json`. Match it by DIRECTORY, not bare\n // basename: only a `definitions.json` sitting directly in the\n // metric-views folder is the config (a `definitions.json` elsewhere in\n // a watched tree must not trigger a regenerate).\n const isMetricConfig =\n metricViewsFolder !== undefined &&\n path.basename(changedFile) === METRIC_CONFIG_FILE &&\n path.dirname(path.resolve(changedFile)) ===\n path.resolve(metricViewsFolder);\n\n if (isWatchedFile && (changedFile.endsWith(\".sql\") || isMetricConfig)) {\n // Route through the single-flight runner (was fire-and-forget\n // generate(), which could race the initial build / watch). This is a\n // dev-only hook, so degrade instantly (non-blocking), then re-arm the\n // warehouse watch so the edited query or metric-view source is\n // re-described in the background against the running warehouse (or\n // once a still-starting one warms up), landing fresh\n // blocking-described types.\n void runGenerate(\"non-blocking\");\n armWarehouseWatch();\n }\n });\n\n // Tear down any pending warehouse watch when the dev server closes so a\n // long backoff can't keep the process alive after shutdown.\n server.httpServer?.once(\"close\", () => {\n watchController?.abort();\n });\n },\n };\n}\n"],"mappings":";;;;;;;;;;AAoBA,MAAM,SAAS,aAAa,6BAA6B;;;;;;;AAQzD,MAAM,6BAA6B;;;;;;;AA6BnC,SAAgB,kBAAkB,SAA4C;CAC5E,IAAI;CACJ,IAAI;CACJ,IAAI;CAIJ,IAAI;CACJ,IAAI;CAcJ,IAAI,WAAiC;CACrC,IAAI,SAAS;CACb,IAAI,cAA6B;CAKjC,IAAI,kBAA0C;;;;;;;;;;;;CAa9C,eAAe,aAAa,MAAqB;AAC/C,MAAI;GACF,MAAM,cAAc,QAAQ,IAAI,2BAA2B;AAE3D,OAAI,CAAC,aAAa;AAChB,WAAO,MAAM,oDAAoD;AACjE;;AAGF,SAAM,uBAAuB;IAC3B;IACA;IACA;IACA;IACA,SAAS;IACT;IACA;IACD,CAAC;WACK,OAAO;GAKd,MAAM,iBACJ,iBAAiB,sBACjB,iBAAiB;AAGnB,OAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,QAAI,eAAgB,OAAM,QAAQ,MAAM;AACxC,UAAM;;AAGR,OAAI,eACF,QAAO,MAAM,MAAM,MAAM,QAAQ;OAEjC,QAAO,MAAM,8BAA8B,MAAM;;;;;;;;;;;;;;;;;;;CAqBvD,SAAS,YAAY,MAAoC;AACvD,gBAAc;AAEd,MAAI,UAAU;AAIZ,YAAS;AACT,UAAO;;EAWT,MAAM,QAAQ,YAA2B;AACvC,UAAO,MAAM;AACX,aAAS;AAKT,UAAM,aADU,YACW;AAE3B,QAAI,CAAC,QAAQ;AACX,gBAAW;AACX;;;;AAKN,aAAW,OAAO;AAClB,SAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAgCT,SAAS,oBAA0B;AACjC,MAAI,QAAQ,IAAI,aAA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U;GACxE,MAAM,2BAA2B,KAAK,KACpC,QAAQ,KAAK,EACb,UACA,eACD;AACD,kBAAe,SAAS,gBAAgB,CACtC,oBACA,yBACD;AAKD,OAAI,SAAS,cAAc;AACzB,kBAAc,aAAa,MAAM,MAAM,KAAK,SAAS,EAAE,KAAK,UAAU;AACtE,wBAAoB,aAAa,MAC9B,MAAM,KAAK,SAAS,EAAE,KAAK,eAC7B;UACI;AACL,kBAAc;AACd,wBAAoB;;;EAIxB,aAAa;AAGX,OAAI,QAAQ,IAAI,aAAa,aAC3B,QAAO,YAAY,WAAW;AAShC,GAAK,YAAY,eAAe;AAChC,sBAAmB;;EAGrB,gBAAgB,QAAQ;AACtB,UAAO,QAAQ,IAAI,aAAa;AAEhC,UAAO,QAAQ,GAAG,WAAW,gBAAgB;IAC3C,MAAM,gBAAgB,aAAa,MAAM,WACvC,YAAY,WAAW,OAAO,CAC/B;IAOD,MAAM,iBACJ,sBAAsB,UACtB,KAAK,SAAS,YAAY,KAAK,sBAC/B,KAAK,QAAQ,KAAK,QAAQ,YAAY,CAAC,KACrC,KAAK,QAAQ,kBAAkB;AAEnC,QAAI,kBAAkB,YAAY,SAAS,OAAO,IAAI,iBAAiB;AAQrE,KAAK,YAAY,eAAe;AAChC,wBAAmB;;KAErB;AAIF,UAAO,YAAY,KAAK,eAAe;AACrC,qBAAiB,OAAO;KACxB;;EAEL"}
1
+ {"version":3,"file":"vite-plugin.js","names":[],"sources":["../../src/type-generator/vite-plugin.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport path from \"node:path\";\nimport type { Plugin } from \"vite\";\nimport { METRIC_CONFIG_FILE } from \"../../../shared/src/schemas/metric-fqn\";\nimport { createLogger } from \"../logging/logger\";\nimport { createWorkspaceClient } from \"../workspace-client\";\nimport {\n ANALYTICS_TYPES_FILE,\n generateFromEntryPoint,\n TYPES_DIR,\n TypegenFatalError,\n TypegenSyntaxError,\n} from \"./index\";\nimport type { PreflightMode } from \"./preflight\";\nimport {\n getWarehouseState,\n startWarehouse,\n waitUntilRunning,\n} from \"./warehouse-status\";\n\nconst logger = createLogger(\"type-generator:vite-plugin\");\n\n/**\n * How long the DEV background watcher waits for a STARTING warehouse to reach\n * RUNNING before giving up. Short relative to the CLI's preflight budget: this\n * is a best-effort \"regenerate once the warehouse warms up\" convenience, not a\n * gate, so we'd rather stop polling than hold a detached task open for minutes.\n */\nconst DEV_WAREHOUSE_WATCH_MAX_MS = 60_000;\n\n/**\n * Options for the AppKit types plugin.\n */\ninterface AppKitTypesPluginOptions {\n /* Path to the output d.ts file (relative to client folder). */\n outFile?: string;\n /**\n * Path to the metric registry `.ts` file (relative to client folder).\n * Defaults to a sibling of `outFile`, computed by the generator. The\n * generated source carries both the `declare module` augmentation and the\n * runtime `metricViewsMetadata` const, so it is a real `.ts`, not a `.d.ts`.\n */\n mvOutFile?: string;\n /**\n * Folders to watch for changes. Defaults to `config/queries` and\n * `config/metric-views`. When overridden, include a `queries` folder and/or a\n * `metric-views` folder — they are resolved by their trailing path segment.\n */\n watchFolders?: string[];\n}\n\n/**\n * Vite plugin to generate types for AppKit queries.\n * Calls generateFromEntryPoint under the hood.\n * @param options - Options to override default values.\n * @returns Vite plugin to generate types for AppKit queries.\n */\nexport function appKitTypesPlugin(options?: AppKitTypesPluginOptions): Plugin {\n let outFile: string;\n let mvOutFile: string | undefined;\n let watchFolders: string[];\n // The queries + metric-views config folders, resolved in `configResolved`.\n // Passed explicitly into generateFromEntryPoint so neither is inferred from\n // `watchFolders` ordering (which used to assume queries was `watchFolders[0]`).\n let queryFolder: string | undefined;\n let metricViewsFolder: string | undefined;\n\n // Single-flight state for runGenerate(). `inFlight` is the promise of the\n // currently-running drain (null when idle); `queued` records that a trigger\n // arrived while a run was active so exactly ONE trailing run fires afterwards\n // (latest-wins — coalesces any number of overlapping triggers into a single\n // rerun). `queued` is read/cleared synchronously inside the drain loop so a\n // trigger landing in any window is caught before the drain exits.\n //\n // `pendingMode` is the mode the next generate should run in (latest-wins, like\n // `queued`): the foreground build runs non-blocking in dev (instant degrade)\n // while the background warehouse watch runs blocking (real DESCRIBEs). A\n // blocking watch trigger that lands while a non-blocking foreground run is in\n // flight therefore still describes when its trailing run fires.\n let inFlight: Promise<void> | null = null;\n let queued = false;\n let pendingMode: PreflightMode = \"non-blocking\";\n\n // The currently-armed DEV background warehouse watch, if any. Aborting it\n // stops a pending waitUntilRunning (server shutdown, or a newer arm replacing\n // an older one).\n let watchController: AbortController | null = null;\n\n /**\n * Generate types once in the given preflight {@link PreflightMode}. Never\n * throws in dev (logs instead); in production it rethrows so the build fails.\n * This is the un-guarded core — callers should go through {@link runGenerate}\n * so concurrent triggers can't race-write the .d.ts.\n *\n * @param mode - preflight policy for this run. The foreground build passes a\n * NODE_ENV-derived mode (blocking in production, non-blocking in dev so it\n * degrades instantly); the background warehouse watch passes \"blocking\" so\n * its regenerate actually DESCRIBEs and lands real (non-degraded) types.\n */\n async function generateOnce(mode: PreflightMode) {\n try {\n const warehouseId = process.env.DATABRICKS_WAREHOUSE_ID || \"\";\n\n if (!warehouseId) {\n logger.debug(\"Warehouse ID not found. Skipping type generation.\");\n return;\n }\n\n await generateFromEntryPoint({\n outFile,\n queryFolder,\n metricViewsFolder,\n warehouseId,\n noCache: false,\n mode,\n mvOutFile,\n });\n } catch (error) {\n // TypegenSyntaxError / TypegenFatalError carry a complete, actionable\n // report in their message. Their stack frames and attached query arrays\n // point into appkit internals and only add noise, so surface just the\n // message — both when failing the prod build and when logging in dev.\n const isTypegenError =\n error instanceof TypegenSyntaxError ||\n error instanceof TypegenFatalError;\n\n // throw in production to fail the build\n if (process.env.NODE_ENV === \"production\") {\n if (isTypegenError) error.stack = error.message;\n throw error;\n }\n\n if (isTypegenError) {\n logger.error(\"%s\", error.message);\n } else {\n logger.error(\"Error generating types: %O\", error);\n }\n }\n }\n\n /**\n * Single-flight wrapper around {@link generateOnce}. The initial build, the\n * .sql watcher, and the DEV warehouse watch all route through here so they can\n * never run typegen concurrently (which would race-write the .d.ts).\n *\n * If a run is already in flight, this does NOT start a second one — it records\n * the requested mode and sets a trailing flag so exactly one more run fires\n * after the current finishes, coalescing any number of overlapping triggers\n * (latest-wins, including the mode: a blocking watch trigger that arrives mid\n * non-blocking foreground run still describes when its trailing run fires).\n *\n * @param mode - preflight policy for this run. Recorded into `pendingMode`,\n * which the drain reads for each generate (latest trigger wins).\n * @returns A promise that resolves when this trigger's work (including any\n * trailing run it scheduled) has completed.\n */\n function runGenerate(mode: PreflightMode): Promise<void> {\n pendingMode = mode;\n\n if (inFlight) {\n // A run is active: remember that another trigger arrived and ride out the\n // current run. One trailing run then covers all coalesced triggers and\n // runs in the latest requested mode (recorded above).\n queued = true;\n return inFlight;\n }\n\n // Drain in a loop rather than recursing after a single queued-check: a\n // trigger can land in the window between generateOnce() resolving and the\n // check, so we re-test `queued` until it's clear. Critically, `inFlight` is\n // cleared synchronously in the SAME tick as the final `queued === false`\n // observation — never deferred to a .finally microtask — so there's no\n // window where a trigger sees `inFlight` set but the drain has already\n // decided to exit. The guard stays held for the whole drain, so concurrent\n // triggers only ever set the flag; they never start a parallel generate.\n const drain = async (): Promise<void> => {\n while (true) {\n queued = false;\n // Snapshot the mode synchronously alongside clearing `queued` so a\n // trigger landing during this generate is observed (via `queued`) on the\n // next loop with its own mode, not silently dropped.\n const runMode = pendingMode;\n await generateOnce(runMode);\n // Synchronous check + clear, atomic w.r.t. other (synchronous) callers.\n if (!queued) {\n inFlight = null;\n return;\n }\n }\n };\n\n inFlight = drain();\n return inFlight;\n }\n\n /**\n * DEV-only: get the warehouse to RUNNING in the background and regenerate with\n * real (non-degraded) types once it is — without blocking dev startup. The\n * foreground build only ever degrades in dev (instant `unknown`/cached types),\n * so this is what lands actual DESCRIBE results in the editor for EVERY\n * reachable warehouse state, not just one that happens to already be warm.\n *\n * Post-probe behaviour by state:\n * - RUNNING → describe right away (the dev foreground degraded, so a running\n * warehouse would otherwise never get real types). `waitUntilRunning`\n * returns immediately for an already-running warehouse, then the blocking\n * regenerate fires.\n * - STARTING → it's already coming up; just wait for RUNNING, then describe.\n * - STOPPED / STOPPING → kick off a start, wait for RUNNING, then describe.\n * - DELETED / DELETING → return (a deleted warehouse can't be started, and\n * blocking typegen would treat it as fatal); leave the degraded types.\n *\n * No-op in production or without a warehouse id. Replaces any previously-armed\n * watch (aborting it first). Fully self-contained: it never throws into the\n * caller and never re-arms itself. The whole lifecycle is abortable via the\n * shared {@link watchController} — its signal is threaded into\n * `waitUntilRunning`, so a dev-server shutdown cancels a pending wait — and the\n * regenerate routes through {@link runGenerate} so it can't race-write the\n * .d.ts with the foreground degrade or a `.sql` re-trigger.\n *\n * The regenerate runs in \"blocking\" mode (not the foreground's non-blocking)\n * so it actually DESCRIBEs the now-RUNNING warehouse and lands real types —\n * the whole point of warming the warehouse in the background.\n */\n function armWarehouseWatch(): void {\n if (process.env.NODE_ENV === \"production\") return;\n\n const warehouseId = process.env.DATABRICKS_WAREHOUSE_ID || \"\";\n if (!warehouseId) return;\n\n // Supersede any in-flight watch so we never run two concurrently.\n watchController?.abort();\n const controller = new AbortController();\n watchController = controller;\n const { signal } = controller;\n\n void (async () => {\n try {\n const client = createWorkspaceClient();\n const state = await getWarehouseState(client, warehouseId);\n\n // A deleted/deleting warehouse can't be started and blocking typegen\n // would treat it as fatal — leave the degraded types and stop. Every\n // other state (including RUNNING) proceeds to wait-then-describe so the\n // dev editor gets real types, not just the foreground's degraded ones.\n if (state === \"DELETED\" || state === \"DELETING\") {\n return;\n }\n\n // Stopped/stopping won't reach RUNNING on its own — nudge it. RUNNING and\n // STARTING need no start (RUNNING is already up; STARTING is coming up),\n // so don't issue a redundant one. A failed start is non-fatal: give up\n // silently rather than throw out of the detached task (the developer\n // still has degraded/cached types).\n let startedByUs = false;\n if (state === \"STOPPED\" || state === \"STOPPING\") {\n try {\n logger.debug(\"Warehouse is %s; starting it.\", state);\n await startWarehouse(client, warehouseId);\n startedByUs = true;\n } catch {\n return;\n }\n }\n\n // Wait for RUNNING. For an already-RUNNING warehouse this returns on the\n // first poll; for STARTING/STOPPED it polls (abortably) until the\n // warehouse warms up, a terminal state, or the deadline.\n const final = await waitUntilRunning(client, warehouseId, {\n maxMs: DEV_WAREHOUSE_WATCH_MAX_MS,\n signal,\n // We just issued the start, so the first poll(s) often still report\n // STOPPED/STOPPING before the start propagates. Poll through those\n // instead of bailing, or the regenerate would never fire. When we\n // didn't start it (RUNNING/STARTING branch), keep the default terminal\n // states.\n treatStoppedAsTransient: startedByUs,\n });\n\n if (final === \"RUNNING\" && !signal.aborted) {\n logger.debug(\"Warehouse is RUNNING; regenerating types.\");\n // Blocking: the warehouse is RUNNING now, so describe it and emit real\n // (non-degraded) types — unlike the foreground dev run, which degraded.\n // Routed through the single-flight guard so it coalesces with the\n // foreground degrade / any `.sql` re-trigger instead of racing them.\n await runGenerate(\"blocking\");\n }\n } catch {\n // Detached background task: any failure (timeout, abort, connectivity,\n // auth) is non-fatal — the developer still has degraded/cached types.\n }\n })();\n }\n\n return {\n name: \"appkit-types\",\n\n apply() {\n const warehouseId = process.env.DATABRICKS_WAREHOUSE_ID || \"\";\n\n if (!warehouseId) {\n logger.debug(\"Warehouse ID not found. Skipping type generation.\");\n return false;\n }\n\n // Run when either config surface exists. Metric-view types are\n // independent of `.sql` queries, so a metric-only project (a\n // `config/metric-views/` with no `config/queries/`) must still activate\n // the plugin.\n const hasQueries = existsSync(\n path.join(process.cwd(), \"config\", \"queries\"),\n );\n const hasMetricViews = existsSync(\n path.join(process.cwd(), \"config\", \"metric-views\"),\n );\n if (!hasQueries && !hasMetricViews) {\n return false;\n }\n\n return true;\n },\n\n configResolved(config) {\n const projectRoot = path.resolve(config.root, \"..\");\n outFile = path.resolve(\n projectRoot,\n options?.outFile ?? `shared/${TYPES_DIR}/${ANALYTICS_TYPES_FILE}`,\n );\n // The metric out-path resolves against projectRoot only when explicitly\n // provided; an unset option passes through as undefined so the generator\n // computes its sibling-of-outFile default. In the all-defaults case the\n // final path is identical (the default outFile above lives in\n // shared/<TYPES_DIR>/), and a customized outFile now keeps its metric\n // sibling next to it instead of pinning it under shared/.\n mvOutFile =\n options?.mvOutFile !== undefined\n ? path.resolve(projectRoot, options.mvOutFile)\n : undefined;\n\n const defaultQueryFolder = path.join(process.cwd(), \"config\", \"queries\");\n const defaultMetricViewsFolder = path.join(\n process.cwd(),\n \"config\",\n \"metric-views\",\n );\n watchFolders = options?.watchFolders ?? [\n defaultQueryFolder,\n defaultMetricViewsFolder,\n ];\n\n // Resolve the two config folders explicitly rather than assuming a\n // position in `watchFolders`. With a custom `watchFolders`, match by the\n // trailing segment; otherwise use the computed defaults.\n if (options?.watchFolders) {\n queryFolder = watchFolders.find((f) => path.basename(f) === \"queries\");\n metricViewsFolder = watchFolders.find(\n (f) => path.basename(f) === \"metric-views\",\n );\n } else {\n queryFolder = defaultQueryFolder;\n metricViewsFolder = defaultMetricViewsFolder;\n }\n },\n\n buildStart() {\n // Production: block the build on this generate (and surface failures).\n // The watch is a dev-only no-op, so just run typegen.\n if (process.env.NODE_ENV === \"production\") {\n return runGenerate(\"blocking\");\n }\n\n // Dev: don't block startup waiting on typegen. The foreground generate runs\n // non-blocking — it skips the warehouse entirely and writes degraded\n // (cached/`unknown`) types instantly. Then arm the warehouse watch so the\n // warehouse gets a one-shot BLOCKING regenerate (real types) in the\n // background for EVERY reachable state: RUNNING describes right away, while\n // STARTING/STOPPED are waited (and started) until they reach RUNNING.\n void runGenerate(\"non-blocking\");\n armWarehouseWatch();\n },\n\n configureServer(server) {\n server.watcher.add(watchFolders);\n\n server.watcher.on(\"change\", (changedFile) => {\n const isWatchedFile = watchFolders.some((folder) =>\n changedFile.startsWith(folder),\n );\n\n // The metric config is `definitions.json` — a far more generic name\n // than the old `metric-views.json`. Match it by DIRECTORY, not bare\n // basename: only a `definitions.json` sitting directly in the\n // metric-views folder is the config (a `definitions.json` elsewhere in\n // a watched tree must not trigger a regenerate).\n const isMetricConfig =\n metricViewsFolder !== undefined &&\n path.basename(changedFile) === METRIC_CONFIG_FILE &&\n path.dirname(path.resolve(changedFile)) ===\n path.resolve(metricViewsFolder);\n\n if (isWatchedFile && (changedFile.endsWith(\".sql\") || isMetricConfig)) {\n // Route through the single-flight runner (was fire-and-forget\n // generate(), which could race the initial build / watch). This is a\n // dev-only hook, so degrade instantly (non-blocking), then re-arm the\n // warehouse watch so the edited query or metric-view source is\n // re-described in the background against the running warehouse (or\n // once a still-starting one warms up), landing fresh\n // blocking-described types.\n void runGenerate(\"non-blocking\");\n armWarehouseWatch();\n }\n });\n\n // Tear down any pending warehouse watch when the dev server closes so a\n // long backoff can't keep the process alive after shutdown.\n server.httpServer?.once(\"close\", () => {\n watchController?.abort();\n });\n },\n };\n}\n"],"mappings":";;;;;;;;;AAoBA,MAAM,SAAS,aAAa,6BAA6B;;;;;;;AAQzD,MAAM,6BAA6B;;;;;;;AA6BnC,SAAgB,kBAAkB,SAA4C;CAC5E,IAAI;CACJ,IAAI;CACJ,IAAI;CAIJ,IAAI;CACJ,IAAI;CAcJ,IAAI,WAAiC;CACrC,IAAI,SAAS;CACb,IAAI,cAA6B;CAKjC,IAAI,kBAA0C;;;;;;;;;;;;CAa9C,eAAe,aAAa,MAAqB;AAC/C,MAAI;GACF,MAAM,cAAc,QAAQ,IAAI,2BAA2B;AAE3D,OAAI,CAAC,aAAa;AAChB,WAAO,MAAM,oDAAoD;AACjE;;AAGF,SAAM,uBAAuB;IAC3B;IACA;IACA;IACA;IACA,SAAS;IACT;IACA;IACD,CAAC;WACK,OAAO;GAKd,MAAM,iBACJ,iBAAiB,sBACjB,iBAAiB;AAGnB,OAAI,QAAQ,IAAI,aAAa,cAAc;AACzC,QAAI,eAAgB,OAAM,QAAQ,MAAM;AACxC,UAAM;;AAGR,OAAI,eACF,QAAO,MAAM,MAAM,MAAM,QAAQ;OAEjC,QAAO,MAAM,8BAA8B,MAAM;;;;;;;;;;;;;;;;;;;CAqBvD,SAAS,YAAY,MAAoC;AACvD,gBAAc;AAEd,MAAI,UAAU;AAIZ,YAA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e,QAAQ;GACrB,MAAM,cAAc,KAAK,QAAQ,OAAO,MAAM,KAAK;AACnD,aAAU,KAAK,QACb,aACA,SAAS,WAAW,UAAU,UAAU,GAAG,uBAC5C;AAOD,eACE,SAAS,cAAc,SACnB,KAAK,QAAQ,aAAa,QAAQ,UAAU,GAC5C;GAEN,MAAM,qBAAqB,KAAK,KAAK,QAAQ,KAAK,EAAE,UAAU,UAAU;GACxE,MAAM,2BAA2B,KAAK,KACpC,QAAQ,KAAK,EACb,UACA,eACD;AACD,kBAAe,SAAS,gBAAgB,CACtC,oBACA,yBACD;AAKD,OAAI,SAAS,cAAc;AACzB,kBAAc,aAAa,MAAM,MAAM,KAAK,SAAS,EAAE,KAAK,UAAU;AACtE,wBAAoB,aAAa,MAC9B,MAAM,KAAK,SAAS,EAAE,KAAK,eAC7B;UACI;AACL,kBAAc;AACd,wBAAoB;;;EAIxB,aAAa;AAGX,OAAI,QAAQ,IAAI,aAAa,aAC3B,QAAO,YAAY,WAAW;AAShC,GAAK,YAAY,eAAe;AAChC,sBAAmB;;EAGrB,gBAAgB,QAAQ;AACtB,UAAO,QAAQ,IAAI,aAAa;AAEhC,UAAO,QAAQ,GAAG,WAAW,gBAAgB;IAC3C,MAAM,gBAAgB,aAAa,MAAM,WACvC,YAAY,WAAW,OAAO,CAC/B;IAOD,MAAM,iBACJ,sBAAsB,UACtB,KAAK,SAAS,YAAY,KAAK,sBAC/B,KAAK,QAAQ,KAAK,QAAQ,YAAY,CAAC,KACrC,KAAK,QAAQ,kBAAkB;AAEnC,QAAI,kBAAkB,YAAY,SAAS,OAAO,IAAI,iBAAiB;AAQrE,KAAK,YAAY,eAAe;AAChC,wBAAmB;;KAErB;AAIF,UAAO,YAAY,KAAK,eAAe;AACrC,qBAAiB,OAAO;KACxB;;EAEL"}
@@ -1,4 +1,5 @@
1
1
  import { ApiError } from "./errors.js";
2
- import { WorkspaceClientOptions } from "./legacy.js";
3
- import { WorkspaceClient, files, jobs } from "./types.js";
4
- import { createWorkspaceClient } from "./factory.js";
2
+ import { CancellationToken, ClientOptions, ConfigError, Context, GenieMessage, LegacyWorkspaceClient, Time, TimeUnits, Waiter, WorkspaceClientOptions, loadConfigFile } from "./legacy.js";
3
+ import { WorkspaceClient, files, jobs, serving, sql } from "./types.js";
4
+ import { createWorkspaceClient } from "./factory.js";
5
+ export { ApiError, type CancellationToken, type ClientOptions, ConfigError, Context, type GenieMessage, type LegacyWorkspaceClient, Time, TimeUnits, type Waiter, type WorkspaceClient, type WorkspaceClientOptions, createWorkspaceClient, type files, type jobs, loadConfigFile, type serving, type sql };
@@ -1,5 +1,5 @@
1
1
  import { ApiError } from "./errors.js";
2
- import { ConfigError, Context, Time, TimeUnits } from "./legacy.js";
2
+ import { ConfigError, Context, Time, TimeUnits, loadConfigFile } from "./legacy.js";
3
3
  import { createWorkspaceClient } from "./factory.js";
4
4
 
5
- export { };
5
+ export { ApiError, ConfigError, Context, Time, TimeUnits, createWorkspaceClient, loadConfigFile };
@@ -1,6 +1,7 @@
1
- import { ClientOptions, WorkspaceClient } from "@databricks/sdk-experimental";
2
- import "@databricks/sdk-experimental/dist/apis/dashboards";
3
- import "@databricks/sdk-experimental/dist/wait";
1
+ import * as SDK from "@databricks/sdk-experimental";
2
+ import { CancellationToken, ClientOptions, ClientOptions as ClientOptions$1, WorkspaceClient } from "@databricks/sdk-experimental";
3
+ import { GenieMessage } from "@databricks/sdk-experimental/dist/apis/dashboards";
4
+ import { Waiter } from "@databricks/sdk-experimental/dist/wait";
4
5
 
5
6
  //#region src/workspace-client/legacy.d.ts
6
7
  /** The concrete legacy SDK client type. */
@@ -13,6 +14,8 @@ type LegacyWorkspaceClient = WorkspaceClient;
13
14
  interface WorkspaceClientOptions {
14
15
  /** Databricks host, e.g. https://my-workspace.cloud.databricks.com. Defaults to DATABRICKS_HOST / profile resolution. */
15
16
  host?: string;
17
+ /** `~/.databrickscfg` profile name. Used when no host/token is provided. */
18
+ profile?: string;
16
19
  /** Bearer token. When set, `authType` defaults to "pat". */
17
20
  token?: string;
18
21
  /** Authentication strategy passed to the legacy client. */
@@ -24,6 +27,8 @@ interface WorkspaceClientOptions {
24
27
  */
25
28
  clientOptions?: ClientOptions;
26
29
  }
30
+ declare const ConfigError: typeof SDK.ConfigError, Context: typeof SDK.Context, TimeUnits: typeof SDK.TimeUnits, loadConfigFile: typeof SDK.loadConfigFile;
31
+ declare const Time: typeof SDK.Time;
27
32
  //#endregion
28
- export { LegacyWorkspaceClient, WorkspaceClientOptions };
33
+ export { type CancellationToken, type ClientOptions$1 as ClientOptions, ConfigError, Context, type GenieMessage, LegacyWorkspaceClient, Time, TimeUnits, type Waiter, WorkspaceClientOptions, loadConfigFile };
29
34
  //# sourceMappingURL=legacy.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"legacy.d.ts","names":[],"sources":["../../src/workspace-client/legacy.ts"],"mappings":";;;;;;KAqBY,qBAAA,GAAwB,eAAA;;;;;;UAOnB,sBAAA;;EAEf,IAAA;;EAEA,KAAA;;EAEA,QAAA;;;;;;EAMA,aAAA,GAAgB,aAAA;AAAA"}
1
+ {"version":3,"file":"legacy.d.ts","names":[],"sources":["../../src/workspace-client/legacy.ts"],"mappings":";;;;;;;KAqBY,qBAAA,GAAwB,eAAA;;;;;;UAOnB,sBAAA;EAcc;EAZ7B,IAAA;EA2Da;EAzDb,OAAA;;EAEA,KAAA;EAuDiC;EArDjC,QAAA;EAqD4D;;;;;EA/C5D,aAAA,GAAgB,aAAA;AAAA;AAAA,cA+CH,WAAA,SAAW,GAAA,CAAA,WAAA,EAAA,OAAA,SAAS,GAAA,CAAA,OAAA,EAAA,SAAA,SAAW,GAAA,CAAA,SAAA,EAAA,cAAA,SAAgB,GAAA,CAAA,cAAA;AAAA,cACjD,IAAA,SAAI,GAAA,CAAA,IAAA"}