@fabric-harness/databricks 4.4.1 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/agent.js CHANGED
@@ -1799,6 +1799,340 @@ import {
1799
1799
  mergeCapabilityPolicies
1800
1800
  } from "@fabric-harness/sdk/strict";
1801
1801
 
1802
+ // src/governance.ts
1803
+ import { currentSubmissionContext, redactJson, sameApprovalOperation } from "@fabric-harness/sdk";
1804
+ function metaString(tool7, key) {
1805
+ const value = tool7.metadata?.[key];
1806
+ return typeof value === "string" ? value : void 0;
1807
+ }
1808
+ function inputString(input, key) {
1809
+ if (input && typeof input === "object" && key in input) {
1810
+ const value = input[key];
1811
+ if (typeof value === "string") return value;
1812
+ }
1813
+ return void 0;
1814
+ }
1815
+ function governedObject(input) {
1816
+ for (const key of [
1817
+ "fullName",
1818
+ "table",
1819
+ "index",
1820
+ "pipelineId",
1821
+ "jobId",
1822
+ "endpoint",
1823
+ "notebookPath"
1824
+ ]) {
1825
+ const value = inputString(input, key);
1826
+ if (value) return value;
1827
+ }
1828
+ const catalog = inputString(input, "catalog");
1829
+ const schema = inputString(input, "schema");
1830
+ return catalog ? [catalog, schema].filter(Boolean).join(".") : void 0;
1831
+ }
1832
+ function catalogFromInput(input) {
1833
+ const explicit = inputString(input, "catalog");
1834
+ if (explicit) return explicit;
1835
+ const object2 = governedObject(input);
1836
+ return object2?.includes(".") ? object2.split(".")[0] : void 0;
1837
+ }
1838
+ function governanceMetadata(tool7) {
1839
+ const value = tool7.metadata?.governance;
1840
+ if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
1841
+ const resources = value.resources;
1842
+ if (!Array.isArray(resources)) return void 0;
1843
+ return {
1844
+ resources: resources.map(
1845
+ (resource, index) => parseResourceDescriptor(resource, tool7.name, index)
1846
+ ),
1847
+ ...value.secretFields !== void 0 ? {
1848
+ secretFields: parseStringArray(
1849
+ value.secretFields,
1850
+ `${tool7.name} governance.secretFields`
1851
+ )
1852
+ } : {}
1853
+ };
1854
+ }
1855
+ function validateDatabricksGovernanceDescriptor(tool7, required2 = false) {
1856
+ const metadata = governanceMetadata(tool7);
1857
+ if (!metadata) {
1858
+ if (required2)
1859
+ throw new Error(
1860
+ `databricks governance: authoring tool "${tool7.name}" must declare metadata.governance.resources.`
1861
+ );
1862
+ return;
1863
+ }
1864
+ if (metadata.resources.length === 0)
1865
+ throw new Error(`databricks governance: tool "${tool7.name}" declares no governed resources.`);
1866
+ if (!tool7.inputSchema || typeof tool7.inputSchema !== "object")
1867
+ throw new Error(
1868
+ `databricks governance: tool "${tool7.name}" needs an inputSchema for resource validation.`
1869
+ );
1870
+ for (const descriptor of metadata.resources)
1871
+ validateSchemaPath(tool7.name, tool7.inputSchema, descriptor);
1872
+ }
1873
+ function extractDatabricksGovernedResources(tool7, input) {
1874
+ const metadata = governanceMetadata(tool7);
1875
+ if (!metadata) return legacyResources(input);
1876
+ const resources = [];
1877
+ for (const descriptor of metadata.resources) {
1878
+ const values = pointerValues(input, descriptor.path);
1879
+ if (values.length === 0 && !descriptor.optional) {
1880
+ throw new Error(
1881
+ `databricks governance: required resource path "${descriptor.path}" matched no values for tool "${tool7.name}".`
1882
+ );
1883
+ }
1884
+ for (const value of values) {
1885
+ if (typeof value !== "string" && typeof value !== "number") {
1886
+ throw new Error(
1887
+ `databricks governance: resource path "${descriptor.path}" on tool "${tool7.name}" must resolve to strings or numbers.`
1888
+ );
1889
+ }
1890
+ const text = String(value);
1891
+ if (!text)
1892
+ throw new Error(
1893
+ `databricks governance: resource path "${descriptor.path}" on tool "${tool7.name}" resolved to an empty value.`
1894
+ );
1895
+ resources.push({
1896
+ kind: descriptor.kind,
1897
+ value: text,
1898
+ path: descriptor.path,
1899
+ ...descriptor.catalogQualified && (descriptor.kind === "catalog" || text.includes(".")) ? { catalog: descriptor.kind === "catalog" ? text : text.split(".")[0] } : {}
1900
+ });
1901
+ }
1902
+ }
1903
+ return resources;
1904
+ }
1905
+ function withGovernance(tool7, options = {}) {
1906
+ const inner = tool7.execute;
1907
+ if (!inner) return tool7;
1908
+ validateDatabricksGovernanceDescriptor(tool7);
1909
+ const now = options.now ?? (() => /* @__PURE__ */ new Date());
1910
+ return {
1911
+ ...tool7,
1912
+ metadata: { ...tool7.metadata ?? {}, governed: true },
1913
+ async execute(input, context) {
1914
+ const resources = extractDatabricksGovernedResources(tool7, input);
1915
+ const catalogs = [
1916
+ ...new Set(resources.flatMap((resource) => resource.catalog ? [resource.catalog] : []))
1917
+ ];
1918
+ const catalog = catalogs[0] ?? catalogFromInput(input);
1919
+ const schema = inputString(input, "schema");
1920
+ const object2 = governedObject(input);
1921
+ const startedAt = now().toISOString();
1922
+ const service = metaString(tool7, "service");
1923
+ const provider = metaString(tool7, "provider");
1924
+ const operation = metaString(tool7, "effect");
1925
+ const submission = currentSubmissionContext();
1926
+ const principal = options.principal ?? options.executingPrincipal?.ucPrincipal ?? options.executingPrincipal?.id ?? submission?.actor?.principal?.id;
1927
+ const base2 = {
1928
+ tool: tool7.name,
1929
+ ...service !== void 0 ? { service } : {},
1930
+ ...provider !== void 0 ? { provider } : {},
1931
+ ...principal !== void 0 ? { principal } : {},
1932
+ ...submission ? {
1933
+ submissionId: submission.submissionId,
1934
+ attemptId: submission.attemptId,
1935
+ ...submission.tenantId !== void 0 ? { tenantId: submission.tenantId } : {}
1936
+ } : {},
1937
+ ...catalog !== void 0 ? { catalog } : {},
1938
+ ...schema !== void 0 ? { schema } : {},
1939
+ ...object2 !== void 0 ? { object: object2 } : {},
1940
+ ...resources.length > 0 ? { resources } : {},
1941
+ ...context?.approval?.approvers.length ? { approvers: context.approval.approvers } : {},
1942
+ ...operation !== void 0 ? { operation } : {},
1943
+ // Redact secret-ish keys before the input enters the audit/lineage record.
1944
+ input: redactToolInput(input, governanceMetadata(tool7)?.secretFields),
1945
+ startedAt
1946
+ };
1947
+ const deniedCatalog = options.catalogs ? catalogs.find((candidate) => !options.catalogs?.includes(candidate)) : void 0;
1948
+ if (options.catalogs && deniedCatalog) {
1949
+ const error51 = `databricks governance: catalog "${deniedCatalog}" is not in the allowed list (${options.catalogs.join(", ")}).`;
1950
+ options.onLineage?.({ ...base2, finishedAt: now().toISOString(), ok: false, error: error51 });
1951
+ throw new Error(error51);
1952
+ }
1953
+ try {
1954
+ if (service && (operation === "write" || operation === "execute") && (options.requireApprovalGrantFor?.includes(service) || options.requireApprovalGrantForTools?.includes(tool7.name))) {
1955
+ if (!options.executingPrincipal)
1956
+ throw new Error(
1957
+ `databricks governance: service "${service}" requires a verified executing principal.`
1958
+ );
1959
+ if (!context?.approval || !context.toolCallId || !sameApprovalOperation(context.approval, {
1960
+ toolCallId: context.toolCallId,
1961
+ toolInput: input,
1962
+ principal: options.executingPrincipal
1963
+ })) {
1964
+ throw new Error(
1965
+ `databricks governance: tool "${tool7.name}" requires an ApprovalGrant bound to this call, input, and executing principal.`
1966
+ );
1967
+ }
1968
+ }
1969
+ const result = await inner(input, context);
1970
+ options.onLineage?.({ ...base2, finishedAt: now().toISOString(), ok: true });
1971
+ return result;
1972
+ } catch (error51) {
1973
+ options.onLineage?.({
1974
+ ...base2,
1975
+ finishedAt: now().toISOString(),
1976
+ ok: false,
1977
+ error: error51 instanceof Error ? error51.message : String(error51)
1978
+ });
1979
+ throw error51;
1980
+ }
1981
+ }
1982
+ };
1983
+ }
1984
+ function parseResourceDescriptor(value, toolName, index) {
1985
+ if (!value || typeof value !== "object" || Array.isArray(value))
1986
+ throw new Error(
1987
+ `databricks governance: invalid resource descriptor ${index} on tool "${toolName}".`
1988
+ );
1989
+ const row = value;
1990
+ if (typeof row.kind !== "string" || !row.kind || typeof row.path !== "string" || !row.path.startsWith("/")) {
1991
+ throw new Error(
1992
+ `databricks governance: descriptor ${index} on tool "${toolName}" requires kind and an absolute path.`
1993
+ );
1994
+ }
1995
+ return {
1996
+ kind: row.kind,
1997
+ path: row.path,
1998
+ ...row.catalogQualified === true ? { catalogQualified: true } : {},
1999
+ ...row.optional === true ? { optional: true } : {}
2000
+ };
2001
+ }
2002
+ function parseStringArray(value, label) {
2003
+ if (!Array.isArray(value) || value.some((item) => typeof item !== "string"))
2004
+ throw new Error(`databricks governance: ${label} must be a string array.`);
2005
+ return value;
2006
+ }
2007
+ function pointerSegments(path) {
2008
+ return path.slice(1).split("/").map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~"));
2009
+ }
2010
+ function pointerValues(input, path) {
2011
+ let current = [input];
2012
+ for (const segment of pointerSegments(path)) {
2013
+ current = current.flatMap((value) => {
2014
+ if (segment === "*") return Array.isArray(value) ? value : [];
2015
+ if (!value || typeof value !== "object" || Array.isArray(value)) return [];
2016
+ return segment in value ? [value[segment]] : [];
2017
+ }).filter((value) => value !== void 0 && value !== null);
2018
+ }
2019
+ return current;
2020
+ }
2021
+ function validateSchemaPath(toolName, schema, descriptor) {
2022
+ let current = [schema];
2023
+ for (const segment of pointerSegments(descriptor.path)) {
2024
+ const variants = current.flatMap(schemaVariants);
2025
+ const next = [];
2026
+ let missing = 0;
2027
+ for (const variant of variants) {
2028
+ if (segment === "*") {
2029
+ const items = variant.items;
2030
+ if (variant.type === "array" && items && typeof items === "object" && !Array.isArray(items))
2031
+ next.push(items);
2032
+ else missing += 1;
2033
+ continue;
2034
+ }
2035
+ const properties = variant.properties;
2036
+ const child = properties && typeof properties === "object" && !Array.isArray(properties) ? properties[segment] : void 0;
2037
+ if (child && typeof child === "object" && !Array.isArray(child))
2038
+ next.push(child);
2039
+ else missing += 1;
2040
+ }
2041
+ if (next.length === 0)
2042
+ throw new Error(
2043
+ `databricks governance: path "${descriptor.path}" does not exist in inputSchema for tool "${toolName}".`
2044
+ );
2045
+ if (!descriptor.optional && missing > 0)
2046
+ throw new Error(
2047
+ `databricks governance: required path "${descriptor.path}" is not present in every inputSchema branch for tool "${toolName}". Mark the descriptor optional or make the schema branch mandatory.`
2048
+ );
2049
+ current = next;
2050
+ }
2051
+ }
2052
+ function schemaVariants(schema) {
2053
+ const variants = Array.isArray(schema.oneOf) ? schema.oneOf : Array.isArray(schema.anyOf) ? schema.anyOf : void 0;
2054
+ if (!variants) return [schema];
2055
+ return variants.filter(
2056
+ (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value)
2057
+ );
2058
+ }
2059
+ function legacyResources(input) {
2060
+ const value = governedObject(input);
2061
+ if (!value) return [];
2062
+ const catalog = catalogFromInput(input);
2063
+ return [{ kind: "object", value, path: "legacy-heuristic", ...catalog ? { catalog } : {} }];
2064
+ }
2065
+ function redactToolInput(input, secretFields) {
2066
+ const clone2 = redactNamedFields(input ?? {}, new Set(secretFields ?? []));
2067
+ return redactJson(clone2);
2068
+ }
2069
+ function redactNamedFields(value, names) {
2070
+ if (Array.isArray(value)) return value.map((item) => redactNamedFields(item, names));
2071
+ if (!value || typeof value !== "object") return value;
2072
+ return Object.fromEntries(
2073
+ Object.entries(value).map(([key, child]) => [
2074
+ key,
2075
+ names.has(key) ? "[REDACTED]" : redactNamedFields(child, names)
2076
+ ])
2077
+ );
2078
+ }
2079
+ function withGovernanceTools(tools, options = {}) {
2080
+ return tools.map((tool7) => withGovernance(tool7, options));
2081
+ }
2082
+ function shouldGate(tool7, services) {
2083
+ const effect = tool7.metadata?.effect;
2084
+ if (effect !== "write" && effect !== "execute") return false;
2085
+ return !services || services.includes(metaString(tool7, "service") ?? "");
2086
+ }
2087
+ function databricksApprovalRules(tools, options) {
2088
+ return tools.filter((tool7) => shouldGate(tool7, options.services)).map((tool7) => ({
2089
+ pattern: tool7.name,
2090
+ audience: options.audience,
2091
+ // ${name} is templated by the policy layer at request time — keep it literal here.
2092
+ reason: options.reason ?? "Databricks ${name} requires data-steward approval.",
2093
+ ...options.ttlSeconds !== void 0 ? { ttlSeconds: options.ttlSeconds } : {}
2094
+ }));
2095
+ }
2096
+ function analyticsCopilotGovernance(options) {
2097
+ const stewardAudience = options.stewardAudience.trim();
2098
+ if (!stewardAudience)
2099
+ throw new Error("databricks analytics copilot governance requires a steward audience.");
2100
+ return {
2101
+ stewardAudience,
2102
+ approvalServices: ["sql", "genie"],
2103
+ ...options.catalogs ? { catalogs: [...options.catalogs] } : {},
2104
+ ...options.approvalTtlSeconds !== void 0 ? { approvalTtlSeconds: options.approvalTtlSeconds } : {},
2105
+ ...options.principalLabel ? { principalLabel: options.principalLabel } : {},
2106
+ ...options.onLineage ? { onLineage: options.onLineage } : {}
2107
+ };
2108
+ }
2109
+ function toHostPatterns(host) {
2110
+ if (!host) return [];
2111
+ const list = Array.isArray(host) ? host : [host];
2112
+ return list.map((entry) => {
2113
+ try {
2114
+ return new URL(entry.includes("://") ? entry : `https://${entry}`).host;
2115
+ } catch {
2116
+ return entry;
2117
+ }
2118
+ });
2119
+ }
2120
+ function databricksGovernancePolicy(options) {
2121
+ const policy = {};
2122
+ if (options.stewardAudience) {
2123
+ policy.toolPolicy = {
2124
+ approvalRules: databricksApprovalRules(options.tools, {
2125
+ audience: options.stewardAudience,
2126
+ ...options.approvalServices ? { services: options.approvalServices } : {},
2127
+ ...options.ttlSeconds !== void 0 ? { ttlSeconds: options.ttlSeconds } : {}
2128
+ })
2129
+ };
2130
+ }
2131
+ const hosts = toHostPatterns(options.host);
2132
+ if (hosts.length > 0) policy.network = { mode: "allowlist", hosts };
2133
+ return policy;
2134
+ }
2135
+
1802
2136
  // ../../node_modules/.pnpm/@databricks+sdk-auth@0.21.0/node_modules/@databricks/sdk-auth/package.json
1803
2137
  var package_default = {
1804
2138
  name: "@databricks/sdk-auth",
@@ -17263,6 +17597,295 @@ function fabricPrincipalFor(principal, overrides = {}) {
17263
17597
  };
17264
17598
  }
17265
17599
 
17600
+ // src/cost-budget-actuals.ts
17601
+ import { parseScopeKey, tenantCostLimit } from "@fabric-harness/sdk";
17602
+
17603
+ // src/sql-exec.ts
17604
+ var TERMINAL_STATES = /* @__PURE__ */ new Set(["SUCCEEDED", "FAILED", "CANCELED", "CLOSED"]);
17605
+ async function runStatement(client, warehouseId, statement, parameters, poll) {
17606
+ const submit = await client.executeStatement({
17607
+ warehouseId,
17608
+ statement,
17609
+ waitTimeout: "30s",
17610
+ disposition: "INLINE",
17611
+ ...parameters.length > 0 ? { parameters } : {}
17612
+ });
17613
+ if (TERMINAL_STATES.has(submit.status?.state ?? "")) return submit;
17614
+ const id = submit.statementId;
17615
+ if (!id) return submit;
17616
+ const deadline = Date.now() + poll.timeoutMs;
17617
+ let current = submit;
17618
+ while (!TERMINAL_STATES.has(current.status?.state ?? "") && Date.now() < deadline) {
17619
+ await new Promise((resolve2) => setTimeout(resolve2, poll.intervalMs));
17620
+ current = await client.getStatementResult({ statementId: id });
17621
+ }
17622
+ return current;
17623
+ }
17624
+ function parseStatementRows(response) {
17625
+ const res = response;
17626
+ const columns = res.manifest?.schema?.columns ?? [];
17627
+ const names = columns.map((c2) => c2.name ?? "");
17628
+ const rows = res.result?.dataArray ?? [];
17629
+ return rows.map((row) => {
17630
+ const record2 = {};
17631
+ names.forEach((name, i2) => {
17632
+ record2[name] = row[i2];
17633
+ });
17634
+ return record2;
17635
+ });
17636
+ }
17637
+
17638
+ // src/cost-budget-actuals.ts
17639
+ function toNumber(value) {
17640
+ const n2 = typeof value === "number" ? value : Number(value);
17641
+ return Number.isFinite(n2) ? n2 : 0;
17642
+ }
17643
+ function deriveDateWindow(parsed) {
17644
+ const period = parsed.period;
17645
+ if (!period) return void 0;
17646
+ const parts = period.split(":");
17647
+ const periodType = parts[0];
17648
+ const periodValue = parts.slice(1).join(":");
17649
+ if (!periodValue) return void 0;
17650
+ switch (periodType) {
17651
+ case "day": {
17652
+ return { startDate: periodValue, endDate: periodValue };
17653
+ }
17654
+ case "hour": {
17655
+ const date5 = periodValue.match(/^(\d{4}-\d{2}-\d{2})/)?.[1];
17656
+ return date5 ? { startDate: date5, endDate: date5 } : void 0;
17657
+ }
17658
+ case "month": {
17659
+ const [year, month] = periodValue.split("-");
17660
+ if (!year || !month) return void 0;
17661
+ const lastDay = new Date(Number(year), Number(month), 0).getDate();
17662
+ return {
17663
+ startDate: `${year}-${month}-01`,
17664
+ endDate: `${year}-${month}-${String(lastDay).padStart(2, "0")}`
17665
+ };
17666
+ }
17667
+ default:
17668
+ return void 0;
17669
+ }
17670
+ }
17671
+ function buildCostQuery(tagPrefix, attribution) {
17672
+ const tagAgent = `${tagPrefix}.agent`;
17673
+ const tagTenant = `${tagPrefix}.tenant`;
17674
+ const tagUser = `${tagPrefix}.user`;
17675
+ const conditions = [
17676
+ "u.usage_date >= :start_date AND u.usage_date <= :end_date"
17677
+ ];
17678
+ if (attribution?.agentId) {
17679
+ conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
17680
+ }
17681
+ if (attribution?.tenantId) {
17682
+ conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
17683
+ }
17684
+ if (attribution?.userId) {
17685
+ conditions.push(`u.tags['${tagUser}'] = :user_id`);
17686
+ }
17687
+ return [
17688
+ "SELECT",
17689
+ " SUM(u.usage_quantity * lp.pricing.default) AS list_cost_usd,",
17690
+ " SUM(u.usage_quantity) AS quantity",
17691
+ "FROM system.billing.usage u",
17692
+ "LEFT JOIN system.billing.list_prices lp",
17693
+ " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
17694
+ " AND u.usage_end_time >= lp.price_start_time",
17695
+ " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
17696
+ `WHERE ${conditions.join(" AND ")}`
17697
+ ].join("\n");
17698
+ }
17699
+ function buildAttributionQuery(tagPrefix, filters) {
17700
+ const tagAgent = `${tagPrefix}.agent`;
17701
+ const tagTenant = `${tagPrefix}.tenant`;
17702
+ const tagUser = `${tagPrefix}.user`;
17703
+ const conditions = [
17704
+ "u.usage_date >= :start_date AND u.usage_date <= :end_date"
17705
+ ];
17706
+ const parameters = [
17707
+ { name: "start_date", value: filters.startDate ?? "", type: "DATE" },
17708
+ { name: "end_date", value: filters.endDate ?? "", type: "DATE" }
17709
+ ];
17710
+ if (filters.agentId) {
17711
+ conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
17712
+ parameters.push({ name: "agent_id", value: filters.agentId });
17713
+ }
17714
+ if (filters.tenantId) {
17715
+ conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
17716
+ parameters.push({ name: "tenant_id", value: filters.tenantId });
17717
+ }
17718
+ if (filters.userId) {
17719
+ conditions.push(`u.tags['${tagUser}'] = :user_id`);
17720
+ parameters.push({ name: "user_id", value: filters.userId });
17721
+ }
17722
+ if (filters.model) {
17723
+ conditions.push("u.sku_name = :model");
17724
+ parameters.push({ name: "model", value: filters.model });
17725
+ }
17726
+ if (filters.provider) {
17727
+ conditions.push("u.cloud = :provider");
17728
+ parameters.push({ name: "provider", value: filters.provider });
17729
+ }
17730
+ const groupBy = filters.groupBy ?? ["agentId", "tenantId", "userId", "model", "provider"];
17731
+ const groupColumns = [];
17732
+ const selectColumns = [];
17733
+ for (const dim of groupBy) {
17734
+ switch (dim) {
17735
+ case "agentId":
17736
+ selectColumns.push(`u.tags['${tagAgent}'] AS agentId`);
17737
+ groupColumns.push(`u.tags['${tagAgent}']`);
17738
+ break;
17739
+ case "tenantId":
17740
+ selectColumns.push(`u.tags['${tagTenant}'] AS tenantId`);
17741
+ groupColumns.push(`u.tags['${tagTenant}']`);
17742
+ break;
17743
+ case "userId":
17744
+ selectColumns.push(`u.tags['${tagUser}'] AS userId`);
17745
+ groupColumns.push(`u.tags['${tagUser}']`);
17746
+ break;
17747
+ case "model":
17748
+ selectColumns.push("u.sku_name AS model");
17749
+ groupColumns.push("u.sku_name");
17750
+ break;
17751
+ case "provider":
17752
+ selectColumns.push("u.cloud AS provider");
17753
+ groupColumns.push("u.cloud");
17754
+ break;
17755
+ case "sessionId":
17756
+ break;
17757
+ case "turnId":
17758
+ break;
17759
+ }
17760
+ }
17761
+ selectColumns.push(
17762
+ "CONCAT('day:', u.usage_date) AS period",
17763
+ "SUM(u.usage_quantity * lp.pricing.default) AS actualUsd",
17764
+ "SUM(u.usage_quantity) AS actualDbus",
17765
+ "COUNT(*) AS calls"
17766
+ );
17767
+ groupColumns.push("CONCAT('day:', u.usage_date)");
17768
+ const query = [
17769
+ "SELECT",
17770
+ ` ${selectColumns.join(",\n ")}`,
17771
+ "FROM system.billing.usage u",
17772
+ "LEFT JOIN system.billing.list_prices lp",
17773
+ " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
17774
+ " AND u.usage_end_time >= lp.price_start_time",
17775
+ " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
17776
+ `WHERE ${conditions.join(" AND ")}`,
17777
+ `GROUP BY ${groupColumns.join(", ")}`,
17778
+ "ORDER BY actualUsd DESC"
17779
+ ].join("\n");
17780
+ return { query, parameters };
17781
+ }
17782
+ function databricksActualCostSource(client, warehouseId, options = {}) {
17783
+ const cacheTtlMs = options.cacheTtlMs ?? 6e4;
17784
+ const tagPrefix = options.tagPrefix ?? "fabric";
17785
+ const poll = options.poll ?? { intervalMs: 2e3, timeoutMs: 12e4 };
17786
+ const cache = /* @__PURE__ */ new Map();
17787
+ return {
17788
+ async getUsd(scopeKey, attribution, queryOptions) {
17789
+ const ttl = queryOptions?.cacheTtlMs ?? cacheTtlMs;
17790
+ const now = Date.now();
17791
+ const cached4 = cache.get(scopeKey);
17792
+ if (cached4 && cached4.expiresAt > now) {
17793
+ const result2 = {
17794
+ usd: cached4.usd
17795
+ };
17796
+ if (cached4.dbus !== void 0) result2.dbus = cached4.dbus;
17797
+ result2.actualsFetchedAt = cached4.fetchedAt;
17798
+ result2.cacheTtlMs = ttl;
17799
+ return result2;
17800
+ }
17801
+ const parsed = parseScopeKey(scopeKey);
17802
+ if (!parsed) {
17803
+ return { usd: 0, cacheTtlMs: ttl };
17804
+ }
17805
+ const dateWindow = deriveDateWindow(parsed);
17806
+ if (!dateWindow) {
17807
+ return { usd: 0, cacheTtlMs: ttl };
17808
+ }
17809
+ const parameters = [
17810
+ { name: "start_date", value: dateWindow.startDate, type: "DATE" },
17811
+ { name: "end_date", value: dateWindow.endDate, type: "DATE" }
17812
+ ];
17813
+ if (attribution?.agentId) {
17814
+ parameters.push({ name: "agent_id", value: attribution.agentId });
17815
+ }
17816
+ if (attribution?.tenantId) {
17817
+ parameters.push({ name: "tenant_id", value: attribution.tenantId });
17818
+ }
17819
+ if (attribution?.userId) {
17820
+ parameters.push({ name: "user_id", value: attribution.userId });
17821
+ }
17822
+ const response = await runStatement(
17823
+ client,
17824
+ warehouseId,
17825
+ buildCostQuery(tagPrefix, attribution),
17826
+ parameters,
17827
+ poll
17828
+ );
17829
+ const rows = parseStatementRows(response);
17830
+ const usd = rows.reduce((sum, r2) => sum + toNumber(r2.list_cost_usd), 0);
17831
+ const dbus = rows.reduce((sum, r2) => sum + toNumber(r2.quantity), 0);
17832
+ const fetchedAt = (/* @__PURE__ */ new Date()).toISOString();
17833
+ cache.set(scopeKey, {
17834
+ usd,
17835
+ dbus: dbus || void 0,
17836
+ fetchedAt,
17837
+ expiresAt: now + ttl
17838
+ });
17839
+ const result = {
17840
+ usd
17841
+ };
17842
+ if (dbus) result.dbus = dbus;
17843
+ result.actualsFetchedAt = fetchedAt;
17844
+ result.cacheTtlMs = ttl;
17845
+ return result;
17846
+ },
17847
+ async queryAttribution(filters) {
17848
+ if (!filters.startDate || !filters.endDate) {
17849
+ throw new Error("queryAttribution requires startDate and endDate");
17850
+ }
17851
+ const { query, parameters } = buildAttributionQuery(tagPrefix, filters);
17852
+ const response = await runStatement(client, warehouseId, query, parameters, poll);
17853
+ const rows = parseStatementRows(response);
17854
+ return rows.map((r2) => {
17855
+ const row = {
17856
+ period: String(r2.period ?? ""),
17857
+ estimatedUsd: 0,
17858
+ actualUsd: toNumber(r2.actualUsd),
17859
+ actualDbus: toNumber(r2.actualDbus),
17860
+ deltaUsd: toNumber(r2.actualUsd),
17861
+ calls: toNumber(r2.calls)
17862
+ };
17863
+ if (r2.agentId) row.agentId = String(r2.agentId);
17864
+ if (r2.userId) row.userId = String(r2.userId);
17865
+ if (r2.tenantId) row.tenantId = String(r2.tenantId);
17866
+ if (r2.model) row.model = String(r2.model);
17867
+ if (r2.provider) row.provider = String(r2.provider);
17868
+ return row;
17869
+ });
17870
+ }
17871
+ };
17872
+ }
17873
+ function databricksTenantCostLimit(client, warehouseId, tenantId, options) {
17874
+ const base2 = tenantCostLimit(tenantId, {
17875
+ ...options.perHourUsd !== void 0 ? { perHourUsd: options.perHourUsd } : {},
17876
+ ...options.perDayUsd !== void 0 ? { perDayUsd: options.perDayUsd } : {},
17877
+ ...options.perMonthUsd !== void 0 ? { perMonthUsd: options.perMonthUsd } : {},
17878
+ ...options.onExceed ? { onExceed: options.onExceed } : {}
17879
+ });
17880
+ const opts = {};
17881
+ if (options.cacheTtlMs !== void 0) opts.cacheTtlMs = options.cacheTtlMs;
17882
+ return {
17883
+ ...base2,
17884
+ scopeSource: "external",
17885
+ actualSource: databricksActualCostSource(client, warehouseId, opts)
17886
+ };
17887
+ }
17888
+
17266
17889
  // src/index.ts
17267
17890
  import { createSearchTool } from "@fabric-harness/sdk";
17268
17891
 
@@ -17455,6 +18078,22 @@ async function listDatabricksModelServices(client, options = {}) {
17455
18078
  }
17456
18079
  throw new Error(`databricks model-services pagination exceeded ${maxPages} pages.`);
17457
18080
  }
18081
+ async function listDatabricksModelProviderServices(client) {
18082
+ const payload = await client.get("/api/2.1/unity-catalog/model-provider-services");
18083
+ return (payload.model_provider_services ?? []).flatMap((service) => {
18084
+ const rawName = service.name?.trim();
18085
+ if (!rawName) return [];
18086
+ const config2 = service.config ?? {};
18087
+ return [
18088
+ {
18089
+ name: rawName.replace(/^model-provider-services\//, ""),
18090
+ providerType: (config2.provider_type ?? "").replace(/^EXTERNAL_MODEL_PROVIDER_TYPE_/, "").toLowerCase(),
18091
+ targets: (config2.targets ?? []).map((target) => target.model?.trim()).filter((model) => Boolean(model)),
18092
+ allowAllTargets: Boolean(config2.allow_all_targets)
18093
+ }
18094
+ ];
18095
+ }).sort((left, right) => left.name.localeCompare(right.name));
18096
+ }
17458
18097
  function normalizeModelService(raw, requiredPrefix) {
17459
18098
  const rawName = typeof raw.name === "string" ? raw.name.trim() : "";
17460
18099
  const name = rawName.replace(/^model-services\//, "");
@@ -17713,41 +18352,6 @@ function jitter(ms) {
17713
18352
  return Math.max(1, Math.round(ms * (0.9 + Math.random() * 0.2)));
17714
18353
  }
17715
18354
 
17716
- // src/sql-exec.ts
17717
- var TERMINAL_STATES = /* @__PURE__ */ new Set(["SUCCEEDED", "FAILED", "CANCELED", "CLOSED"]);
17718
- async function runStatement(client, warehouseId, statement, parameters, poll) {
17719
- const submit = await client.executeStatement({
17720
- warehouseId,
17721
- statement,
17722
- waitTimeout: "30s",
17723
- disposition: "INLINE",
17724
- ...parameters.length > 0 ? { parameters } : {}
17725
- });
17726
- if (TERMINAL_STATES.has(submit.status?.state ?? "")) return submit;
17727
- const id = submit.statementId;
17728
- if (!id) return submit;
17729
- const deadline = Date.now() + poll.timeoutMs;
17730
- let current = submit;
17731
- while (!TERMINAL_STATES.has(current.status?.state ?? "") && Date.now() < deadline) {
17732
- await new Promise((resolve2) => setTimeout(resolve2, poll.intervalMs));
17733
- current = await client.getStatementResult({ statementId: id });
17734
- }
17735
- return current;
17736
- }
17737
- function parseStatementRows(response) {
17738
- const res = response;
17739
- const columns = res.manifest?.schema?.columns ?? [];
17740
- const names = columns.map((c2) => c2.name ?? "");
17741
- const rows = res.result?.dataArray ?? [];
17742
- return rows.map((row) => {
17743
- const record2 = {};
17744
- names.forEach((name, i2) => {
17745
- record2[name] = row[i2];
17746
- });
17747
- return record2;
17748
- });
17749
- }
17750
-
17751
18355
  // src/ai.ts
17752
18356
  function databricksAiQueryTool(client, options) {
17753
18357
  const poll = { intervalMs: options.pollIntervalMs ?? 2e3, timeoutMs: options.timeoutMs ?? 12e4 };
@@ -17792,7 +18396,7 @@ function buildQuery(groupBy) {
17792
18396
  "ORDER BY list_cost_usd DESC"
17793
18397
  ].join("\n");
17794
18398
  }
17795
- function toNumber(value) {
18399
+ function toNumber2(value) {
17796
18400
  const n2 = typeof value === "number" ? value : Number(value);
17797
18401
  return Number.isFinite(n2) ? n2 : 0;
17798
18402
  }
@@ -17817,8 +18421,8 @@ function databricksConsumption(client, options) {
17817
18421
  endDate: opts.endDate,
17818
18422
  groupBy,
17819
18423
  rows,
17820
- totalDbus: rows.reduce((sum, r2) => sum + toNumber(r2.quantity), 0),
17821
- totalListCostUsd: rows.reduce((sum, r2) => sum + toNumber(r2.list_cost_usd), 0)
18424
+ totalDbus: rows.reduce((sum, r2) => sum + toNumber2(r2.quantity), 0),
18425
+ totalListCostUsd: rows.reduce((sum, r2) => sum + toNumber2(r2.list_cost_usd), 0)
17822
18426
  };
17823
18427
  }
17824
18428
  };
@@ -19956,327 +20560,6 @@ function snakeValue2(value) {
19956
20560
  return value;
19957
20561
  }
19958
20562
 
19959
- // src/governance.ts
19960
- import { currentSubmissionContext, redactJson, sameApprovalOperation } from "@fabric-harness/sdk";
19961
- function metaString(tool7, key) {
19962
- const value = tool7.metadata?.[key];
19963
- return typeof value === "string" ? value : void 0;
19964
- }
19965
- function inputString(input, key) {
19966
- if (input && typeof input === "object" && key in input) {
19967
- const value = input[key];
19968
- if (typeof value === "string") return value;
19969
- }
19970
- return void 0;
19971
- }
19972
- function governedObject(input) {
19973
- for (const key of [
19974
- "fullName",
19975
- "table",
19976
- "index",
19977
- "pipelineId",
19978
- "jobId",
19979
- "endpoint",
19980
- "notebookPath"
19981
- ]) {
19982
- const value = inputString(input, key);
19983
- if (value) return value;
19984
- }
19985
- const catalog = inputString(input, "catalog");
19986
- const schema = inputString(input, "schema");
19987
- return catalog ? [catalog, schema].filter(Boolean).join(".") : void 0;
19988
- }
19989
- function catalogFromInput(input) {
19990
- const explicit = inputString(input, "catalog");
19991
- if (explicit) return explicit;
19992
- const object2 = governedObject(input);
19993
- return object2?.includes(".") ? object2.split(".")[0] : void 0;
19994
- }
19995
- function governanceMetadata(tool7) {
19996
- const value = tool7.metadata?.governance;
19997
- if (!value || typeof value !== "object" || Array.isArray(value)) return void 0;
19998
- const resources = value.resources;
19999
- if (!Array.isArray(resources)) return void 0;
20000
- return {
20001
- resources: resources.map(
20002
- (resource, index) => parseResourceDescriptor(resource, tool7.name, index)
20003
- ),
20004
- ...value.secretFields !== void 0 ? {
20005
- secretFields: parseStringArray(
20006
- value.secretFields,
20007
- `${tool7.name} governance.secretFields`
20008
- )
20009
- } : {}
20010
- };
20011
- }
20012
- function validateDatabricksGovernanceDescriptor(tool7, required2 = false) {
20013
- const metadata = governanceMetadata(tool7);
20014
- if (!metadata) {
20015
- if (required2)
20016
- throw new Error(
20017
- `databricks governance: authoring tool "${tool7.name}" must declare metadata.governance.resources.`
20018
- );
20019
- return;
20020
- }
20021
- if (metadata.resources.length === 0)
20022
- throw new Error(`databricks governance: tool "${tool7.name}" declares no governed resources.`);
20023
- if (!tool7.inputSchema || typeof tool7.inputSchema !== "object")
20024
- throw new Error(
20025
- `databricks governance: tool "${tool7.name}" needs an inputSchema for resource validation.`
20026
- );
20027
- for (const descriptor of metadata.resources)
20028
- validateSchemaPath(tool7.name, tool7.inputSchema, descriptor);
20029
- }
20030
- function extractDatabricksGovernedResources(tool7, input) {
20031
- const metadata = governanceMetadata(tool7);
20032
- if (!metadata) return legacyResources(input);
20033
- const resources = [];
20034
- for (const descriptor of metadata.resources) {
20035
- const values = pointerValues(input, descriptor.path);
20036
- if (values.length === 0 && !descriptor.optional) {
20037
- throw new Error(
20038
- `databricks governance: required resource path "${descriptor.path}" matched no values for tool "${tool7.name}".`
20039
- );
20040
- }
20041
- for (const value of values) {
20042
- if (typeof value !== "string" && typeof value !== "number") {
20043
- throw new Error(
20044
- `databricks governance: resource path "${descriptor.path}" on tool "${tool7.name}" must resolve to strings or numbers.`
20045
- );
20046
- }
20047
- const text = String(value);
20048
- if (!text)
20049
- throw new Error(
20050
- `databricks governance: resource path "${descriptor.path}" on tool "${tool7.name}" resolved to an empty value.`
20051
- );
20052
- resources.push({
20053
- kind: descriptor.kind,
20054
- value: text,
20055
- path: descriptor.path,
20056
- ...descriptor.catalogQualified && (descriptor.kind === "catalog" || text.includes(".")) ? { catalog: descriptor.kind === "catalog" ? text : text.split(".")[0] } : {}
20057
- });
20058
- }
20059
- }
20060
- return resources;
20061
- }
20062
- function withGovernance(tool7, options = {}) {
20063
- const inner = tool7.execute;
20064
- if (!inner) return tool7;
20065
- validateDatabricksGovernanceDescriptor(tool7);
20066
- const now = options.now ?? (() => /* @__PURE__ */ new Date());
20067
- return {
20068
- ...tool7,
20069
- metadata: { ...tool7.metadata ?? {}, governed: true },
20070
- async execute(input, context) {
20071
- const resources = extractDatabricksGovernedResources(tool7, input);
20072
- const catalogs = [
20073
- ...new Set(resources.flatMap((resource) => resource.catalog ? [resource.catalog] : []))
20074
- ];
20075
- const catalog = catalogs[0] ?? catalogFromInput(input);
20076
- const schema = inputString(input, "schema");
20077
- const object2 = governedObject(input);
20078
- const startedAt = now().toISOString();
20079
- const service = metaString(tool7, "service");
20080
- const provider = metaString(tool7, "provider");
20081
- const operation = metaString(tool7, "effect");
20082
- const submission = currentSubmissionContext();
20083
- const principal = options.principal ?? options.executingPrincipal?.ucPrincipal ?? options.executingPrincipal?.id ?? submission?.actor?.principal?.id;
20084
- const base2 = {
20085
- tool: tool7.name,
20086
- ...service !== void 0 ? { service } : {},
20087
- ...provider !== void 0 ? { provider } : {},
20088
- ...principal !== void 0 ? { principal } : {},
20089
- ...submission ? {
20090
- submissionId: submission.submissionId,
20091
- attemptId: submission.attemptId,
20092
- ...submission.tenantId !== void 0 ? { tenantId: submission.tenantId } : {}
20093
- } : {},
20094
- ...catalog !== void 0 ? { catalog } : {},
20095
- ...schema !== void 0 ? { schema } : {},
20096
- ...object2 !== void 0 ? { object: object2 } : {},
20097
- ...resources.length > 0 ? { resources } : {},
20098
- ...context?.approval?.approvers.length ? { approvers: context.approval.approvers } : {},
20099
- ...operation !== void 0 ? { operation } : {},
20100
- // Redact secret-ish keys before the input enters the audit/lineage record.
20101
- input: redactToolInput(input, governanceMetadata(tool7)?.secretFields),
20102
- startedAt
20103
- };
20104
- const deniedCatalog = options.catalogs ? catalogs.find((candidate) => !options.catalogs?.includes(candidate)) : void 0;
20105
- if (options.catalogs && deniedCatalog) {
20106
- const error51 = `databricks governance: catalog "${deniedCatalog}" is not in the allowed list (${options.catalogs.join(", ")}).`;
20107
- options.onLineage?.({ ...base2, finishedAt: now().toISOString(), ok: false, error: error51 });
20108
- throw new Error(error51);
20109
- }
20110
- try {
20111
- if (service && (operation === "write" || operation === "execute") && (options.requireApprovalGrantFor?.includes(service) || options.requireApprovalGrantForTools?.includes(tool7.name))) {
20112
- if (!options.executingPrincipal)
20113
- throw new Error(
20114
- `databricks governance: service "${service}" requires a verified executing principal.`
20115
- );
20116
- if (!context?.approval || !context.toolCallId || !sameApprovalOperation(context.approval, {
20117
- toolCallId: context.toolCallId,
20118
- toolInput: input,
20119
- principal: options.executingPrincipal
20120
- })) {
20121
- throw new Error(
20122
- `databricks governance: tool "${tool7.name}" requires an ApprovalGrant bound to this call, input, and executing principal.`
20123
- );
20124
- }
20125
- }
20126
- const result = await inner(input, context);
20127
- options.onLineage?.({ ...base2, finishedAt: now().toISOString(), ok: true });
20128
- return result;
20129
- } catch (error51) {
20130
- options.onLineage?.({
20131
- ...base2,
20132
- finishedAt: now().toISOString(),
20133
- ok: false,
20134
- error: error51 instanceof Error ? error51.message : String(error51)
20135
- });
20136
- throw error51;
20137
- }
20138
- }
20139
- };
20140
- }
20141
- function parseResourceDescriptor(value, toolName, index) {
20142
- if (!value || typeof value !== "object" || Array.isArray(value))
20143
- throw new Error(
20144
- `databricks governance: invalid resource descriptor ${index} on tool "${toolName}".`
20145
- );
20146
- const row = value;
20147
- if (typeof row.kind !== "string" || !row.kind || typeof row.path !== "string" || !row.path.startsWith("/")) {
20148
- throw new Error(
20149
- `databricks governance: descriptor ${index} on tool "${toolName}" requires kind and an absolute path.`
20150
- );
20151
- }
20152
- return {
20153
- kind: row.kind,
20154
- path: row.path,
20155
- ...row.catalogQualified === true ? { catalogQualified: true } : {},
20156
- ...row.optional === true ? { optional: true } : {}
20157
- };
20158
- }
20159
- function parseStringArray(value, label) {
20160
- if (!Array.isArray(value) || value.some((item) => typeof item !== "string"))
20161
- throw new Error(`databricks governance: ${label} must be a string array.`);
20162
- return value;
20163
- }
20164
- function pointerSegments(path) {
20165
- return path.slice(1).split("/").map((segment) => segment.replace(/~1/g, "/").replace(/~0/g, "~"));
20166
- }
20167
- function pointerValues(input, path) {
20168
- let current = [input];
20169
- for (const segment of pointerSegments(path)) {
20170
- current = current.flatMap((value) => {
20171
- if (segment === "*") return Array.isArray(value) ? value : [];
20172
- if (!value || typeof value !== "object" || Array.isArray(value)) return [];
20173
- return segment in value ? [value[segment]] : [];
20174
- }).filter((value) => value !== void 0 && value !== null);
20175
- }
20176
- return current;
20177
- }
20178
- function validateSchemaPath(toolName, schema, descriptor) {
20179
- let current = [schema];
20180
- for (const segment of pointerSegments(descriptor.path)) {
20181
- const variants = current.flatMap(schemaVariants);
20182
- const next = [];
20183
- let missing = 0;
20184
- for (const variant of variants) {
20185
- if (segment === "*") {
20186
- const items = variant.items;
20187
- if (variant.type === "array" && items && typeof items === "object" && !Array.isArray(items))
20188
- next.push(items);
20189
- else missing += 1;
20190
- continue;
20191
- }
20192
- const properties = variant.properties;
20193
- const child = properties && typeof properties === "object" && !Array.isArray(properties) ? properties[segment] : void 0;
20194
- if (child && typeof child === "object" && !Array.isArray(child))
20195
- next.push(child);
20196
- else missing += 1;
20197
- }
20198
- if (next.length === 0)
20199
- throw new Error(
20200
- `databricks governance: path "${descriptor.path}" does not exist in inputSchema for tool "${toolName}".`
20201
- );
20202
- if (!descriptor.optional && missing > 0)
20203
- throw new Error(
20204
- `databricks governance: required path "${descriptor.path}" is not present in every inputSchema branch for tool "${toolName}". Mark the descriptor optional or make the schema branch mandatory.`
20205
- );
20206
- current = next;
20207
- }
20208
- }
20209
- function schemaVariants(schema) {
20210
- const variants = Array.isArray(schema.oneOf) ? schema.oneOf : Array.isArray(schema.anyOf) ? schema.anyOf : void 0;
20211
- if (!variants) return [schema];
20212
- return variants.filter(
20213
- (value) => Boolean(value) && typeof value === "object" && !Array.isArray(value)
20214
- );
20215
- }
20216
- function legacyResources(input) {
20217
- const value = governedObject(input);
20218
- if (!value) return [];
20219
- const catalog = catalogFromInput(input);
20220
- return [{ kind: "object", value, path: "legacy-heuristic", ...catalog ? { catalog } : {} }];
20221
- }
20222
- function redactToolInput(input, secretFields) {
20223
- const clone2 = redactNamedFields(input ?? {}, new Set(secretFields ?? []));
20224
- return redactJson(clone2);
20225
- }
20226
- function redactNamedFields(value, names) {
20227
- if (Array.isArray(value)) return value.map((item) => redactNamedFields(item, names));
20228
- if (!value || typeof value !== "object") return value;
20229
- return Object.fromEntries(
20230
- Object.entries(value).map(([key, child]) => [
20231
- key,
20232
- names.has(key) ? "[REDACTED]" : redactNamedFields(child, names)
20233
- ])
20234
- );
20235
- }
20236
- function withGovernanceTools(tools, options = {}) {
20237
- return tools.map((tool7) => withGovernance(tool7, options));
20238
- }
20239
- function shouldGate(tool7, services) {
20240
- const effect = tool7.metadata?.effect;
20241
- if (effect !== "write" && effect !== "execute") return false;
20242
- return !services || services.includes(metaString(tool7, "service") ?? "");
20243
- }
20244
- function databricksApprovalRules(tools, options) {
20245
- return tools.filter((tool7) => shouldGate(tool7, options.services)).map((tool7) => ({
20246
- pattern: tool7.name,
20247
- audience: options.audience,
20248
- // ${name} is templated by the policy layer at request time — keep it literal here.
20249
- reason: options.reason ?? "Databricks ${name} requires data-steward approval.",
20250
- ...options.ttlSeconds !== void 0 ? { ttlSeconds: options.ttlSeconds } : {}
20251
- }));
20252
- }
20253
- function toHostPatterns(host) {
20254
- if (!host) return [];
20255
- const list = Array.isArray(host) ? host : [host];
20256
- return list.map((entry) => {
20257
- try {
20258
- return new URL(entry.includes("://") ? entry : `https://${entry}`).host;
20259
- } catch {
20260
- return entry;
20261
- }
20262
- });
20263
- }
20264
- function databricksGovernancePolicy(options) {
20265
- const policy = {};
20266
- if (options.stewardAudience) {
20267
- policy.toolPolicy = {
20268
- approvalRules: databricksApprovalRules(options.tools, {
20269
- audience: options.stewardAudience,
20270
- ...options.approvalServices ? { services: options.approvalServices } : {},
20271
- ...options.ttlSeconds !== void 0 ? { ttlSeconds: options.ttlSeconds } : {}
20272
- })
20273
- };
20274
- }
20275
- const hosts = toHostPatterns(options.host);
20276
- if (hosts.length > 0) policy.network = { mode: "allowlist", hosts };
20277
- return policy;
20278
- }
20279
-
20280
20563
  // src/jobs-authoring.ts
20281
20564
  function validateDatabricksComputePolicy(policy) {
20282
20565
  for (const [name, value] of [
@@ -56891,7 +57174,12 @@ var DATABRICKS_RAW_PROTOCOL_RULES = [
56891
57174
  {
56892
57175
  id: "ai-gateway-discovery",
56893
57176
  methods: ["GET"],
56894
- path: /^\/api\/(?:ai-gateway\/v2\/endpoints|2\.1\/unity-catalog\/model-services)$/u
57177
+ path: /^\/api\/(?:ai-gateway\/v2\/endpoints|2\.1\/unity-catalog\/model-(?:provider-)?services)$/u
57178
+ },
57179
+ {
57180
+ id: "sql-warehouse-discovery",
57181
+ methods: ["GET"],
57182
+ path: /^\/api\/2\.0\/sql\/warehouses$/u
56895
57183
  },
56896
57184
  {
56897
57185
  id: "agent-services-beta",
@@ -57543,7 +57831,146 @@ function jitter3(ms) {
57543
57831
  return Math.max(1, Math.round(ms * (0.9 + Math.random() * 0.2)));
57544
57832
  }
57545
57833
 
57834
+ // src/sql-warehouses.ts
57835
+ async function listDatabricksSqlWarehouses(client) {
57836
+ const payload = await client.get("/api/2.0/sql/warehouses");
57837
+ return (payload.warehouses ?? []).filter(
57838
+ (warehouse) => Boolean(warehouse.id)
57839
+ ).map((warehouse) => ({
57840
+ id: warehouse.id,
57841
+ ...warehouse.name ? { name: warehouse.name } : {},
57842
+ ...warehouse.state ? { state: warehouse.state } : {}
57843
+ }));
57844
+ }
57845
+
57546
57846
  // src/tools.ts
57847
+ var UNSAFE_SQL_KEYWORDS = /* @__PURE__ */ new Set([
57848
+ "ADD",
57849
+ "ALTER",
57850
+ "ANALYZE",
57851
+ "CACHE",
57852
+ "CALL",
57853
+ "COMMENT",
57854
+ "COPY",
57855
+ "CREATE",
57856
+ "DELETE",
57857
+ "DROP",
57858
+ "EXECUTE",
57859
+ "GRANT",
57860
+ "INSERT",
57861
+ "INTO",
57862
+ "LIST",
57863
+ "MERGE",
57864
+ "MSCK",
57865
+ "OPTIMIZE",
57866
+ "PUT",
57867
+ "REFRESH",
57868
+ "REMOVE",
57869
+ "RESET",
57870
+ "RESTORE",
57871
+ "REVOKE",
57872
+ "SET",
57873
+ "TRANSFORM",
57874
+ "TRUNCATE",
57875
+ "UNCACHE",
57876
+ "UPDATE",
57877
+ "USE",
57878
+ "VACUUM"
57879
+ ]);
57880
+ function lexSqlReadStatement(statement) {
57881
+ const tokens = [];
57882
+ const semicolons = [];
57883
+ let depth = 0;
57884
+ for (let index = 0; index < statement.length; ) {
57885
+ const char = statement.charAt(index);
57886
+ const next = statement.charAt(index + 1);
57887
+ if (/\s/u.test(char)) {
57888
+ index += 1;
57889
+ continue;
57890
+ }
57891
+ if (char === "-" && next === "-") {
57892
+ index += 2;
57893
+ while (index < statement.length && statement[index] !== "\n") index += 1;
57894
+ continue;
57895
+ }
57896
+ if (char === "/" && next === "*") {
57897
+ const end = statement.indexOf("*/", index + 2);
57898
+ if (end < 0) throw new Error("Databricks SQL read denied: unterminated block comment.");
57899
+ index = end + 2;
57900
+ continue;
57901
+ }
57902
+ if (char === "'" || char === '"' || char === "`") {
57903
+ const quote = char;
57904
+ index += 1;
57905
+ let closed = false;
57906
+ while (index < statement.length) {
57907
+ if (statement[index] === quote) {
57908
+ if (statement[index + 1] === quote) {
57909
+ index += 2;
57910
+ continue;
57911
+ }
57912
+ index += 1;
57913
+ closed = true;
57914
+ break;
57915
+ }
57916
+ if (statement[index] === "\\" && quote === "'" && index + 1 < statement.length) {
57917
+ index += 2;
57918
+ continue;
57919
+ }
57920
+ index += 1;
57921
+ }
57922
+ if (!closed) throw new Error("Databricks SQL read denied: unterminated quoted value.");
57923
+ continue;
57924
+ }
57925
+ if (char === ";") {
57926
+ semicolons.push(index);
57927
+ index += 1;
57928
+ continue;
57929
+ }
57930
+ if (char === "(") {
57931
+ depth += 1;
57932
+ index += 1;
57933
+ continue;
57934
+ }
57935
+ if (char === ")") {
57936
+ depth -= 1;
57937
+ if (depth < 0) throw new Error("Databricks SQL read denied: unbalanced parentheses.");
57938
+ index += 1;
57939
+ continue;
57940
+ }
57941
+ if (/[A-Za-z_]/u.test(char)) {
57942
+ const start = index;
57943
+ index += 1;
57944
+ while (index < statement.length && /[A-Za-z0-9_$]/u.test(statement.charAt(index))) index += 1;
57945
+ tokens.push({ value: statement.slice(start, index).toUpperCase(), depth });
57946
+ continue;
57947
+ }
57948
+ index += 1;
57949
+ }
57950
+ if (depth !== 0) throw new Error("Databricks SQL read denied: unbalanced parentheses.");
57951
+ return { tokens, semicolons };
57952
+ }
57953
+ function assertDatabricksSqlReadStatement(statement) {
57954
+ if (!statement.trim()) throw new Error("Databricks SQL read denied: statement cannot be empty.");
57955
+ const { tokens, semicolons } = lexSqlReadStatement(statement);
57956
+ if (tokens.length === 0) throw new Error("Databricks SQL read denied: expected a SELECT statement.");
57957
+ if (semicolons.length > 1)
57958
+ throw new Error("Databricks SQL read denied: multiple statements are not allowed.");
57959
+ if (semicolons.length === 1) {
57960
+ const tail = statement.slice((semicolons[0] ?? -1) + 1);
57961
+ const trailing = lexSqlReadStatement(tail);
57962
+ if (trailing.tokens.length > 0 || trailing.semicolons.length > 0)
57963
+ throw new Error("Databricks SQL read denied: multiple statements are not allowed.");
57964
+ }
57965
+ const first = tokens[0]?.value;
57966
+ const topLevel = tokens.filter((token) => token.depth === 0).map((token) => token.value);
57967
+ const mainKeyword = first === "WITH" ? topLevel.find((token) => token === "SELECT") : first;
57968
+ if (mainKeyword !== "SELECT")
57969
+ throw new Error("Databricks SQL read denied: only SELECT queries are allowed.");
57970
+ const unsafe = tokens.find((token) => UNSAFE_SQL_KEYWORDS.has(token.value));
57971
+ if (unsafe)
57972
+ throw new Error(`Databricks SQL read denied: unsafe keyword ${unsafe.value} is not allowed.`);
57973
+ }
57547
57974
  function databricksSqlTool(client, options) {
57548
57975
  return {
57549
57976
  name: options.name ?? "databricks_sql",
@@ -57569,6 +57996,43 @@ function databricksSqlTool(client, options) {
57569
57996
  })
57570
57997
  };
57571
57998
  }
57999
+ function databricksSqlReadTool(client, options) {
58000
+ return {
58001
+ name: options.name ?? "sql_read",
58002
+ description: options.description ?? "Run one SELECT-only query on a Databricks SQL Warehouse. Mutations and multiple statements are denied.",
58003
+ inputSchema: {
58004
+ type: "object",
58005
+ additionalProperties: false,
58006
+ properties: {
58007
+ statement: { type: "string" },
58008
+ catalog: { type: "string" },
58009
+ schema: { type: "string" }
58010
+ },
58011
+ required: ["statement"]
58012
+ },
58013
+ metadata: {
58014
+ effect: "read",
58015
+ provider: "databricks",
58016
+ service: "sql",
58017
+ governance: {
58018
+ resources: [
58019
+ { kind: "catalog", path: "/catalog", catalogQualified: true, optional: true }
58020
+ ]
58021
+ }
58022
+ },
58023
+ async execute(input) {
58024
+ assertDatabricksSqlReadStatement(input.statement);
58025
+ return await client.executeStatement({
58026
+ warehouseId: options.warehouseId,
58027
+ statement: input.statement,
58028
+ waitTimeout: options.waitTimeout ?? "30s",
58029
+ disposition: options.disposition ?? "INLINE",
58030
+ ...input.catalog ? { catalog: input.catalog } : {},
58031
+ ...input.schema ? { schema: input.schema } : {}
58032
+ });
58033
+ }
58034
+ };
58035
+ }
57572
58036
  function unityCatalogTablesTool(client, options = {}) {
57573
58037
  return {
57574
58038
  name: options.name ?? "databricks_unity_catalog_tables",
@@ -58281,6 +58745,7 @@ function buildDatabricksBundle(config2, scope) {
58281
58745
  });
58282
58746
  }
58283
58747
  const rawTools = [
58748
+ ...config2.sqlRead && config2.warehouseId ? [databricksSqlReadTool(native.statements, { warehouseId: config2.warehouseId })] : [],
58284
58749
  ...config2.warehouseId ? [databricksSqlTool(native.statements, { warehouseId: config2.warehouseId })] : [],
58285
58750
  unityCatalogTablesTool(native.tables),
58286
58751
  databricksTableInfoTool(native.tables),
@@ -58407,8 +58872,10 @@ function buildDatabricksBundle(config2, scope) {
58407
58872
  ...agentServicesClient ? { agentServices: agentServicesClient } : {},
58408
58873
  aiGateway: {
58409
58874
  ensure: () => ensureDatabricksAiGateway(client),
58410
- listModelServices: (options) => listDatabricksModelServices(client, options)
58875
+ listModelServices: (options) => listDatabricksModelServices(client, options),
58876
+ listModelProviderServices: () => listDatabricksModelProviderServices(client)
58411
58877
  },
58878
+ sqlWarehouses: { list: () => listDatabricksSqlWarehouses(client) },
58412
58879
  mlflowTraceExporter: (options) => mlflowTraceExporter({ client, ...options }),
58413
58880
  forPrincipal(input) {
58414
58881
  const scoped = buildDatabricksBundle(config2, {
@@ -58539,6 +59006,7 @@ function defineDatabricksAgent(options = {}) {
58539
59006
  const merged = options ? mergeOptions(options) : {};
58540
59007
  const {
58541
59008
  databricks: explicitConfig,
59009
+ analyticsCopilot = false,
58542
59010
  modelProvider,
58543
59011
  tools = [],
58544
59012
  sandbox,
@@ -58556,8 +59024,9 @@ function defineDatabricksAgent(options = {}) {
58556
59024
  ...directTools.length > 0 ? { tools: directTools } : {},
58557
59025
  run: async (context) => {
58558
59026
  const env2 = context.env ?? {};
58559
- const config2 = explicitConfig ?? databricksConfigFromEnv(env2);
59027
+ const config2 = explicitConfig ?? databricksConfigFromEnv(env2, { analyticsCopilot });
58560
59028
  const bundle = databricks(config2);
59029
+ const analyticsCostLimit = analyticsCopilot ? databricksAnalyticsCostLimitFromEnv(env2, config2, bundle) : void 0;
58561
59030
  if (toolRefs.length > 0) resolveToolRefs(bundle, toolRefs);
58562
59031
  const directNames = new Set(directTools.map((tool7) => tool7.name));
58563
59032
  const bundleTools = bundle.tools.filter((tool7) => !directNames.has(tool7.name));
@@ -58567,6 +59036,7 @@ function defineDatabricksAgent(options = {}) {
58567
59036
  ...initOptions,
58568
59037
  modelProvider: initOptions.modelProvider ?? modelProvider ?? bundle.modelProvider,
58569
59038
  tools: [...bundleTools, ...initOptions.tools ?? []],
59039
+ ...analyticsCostLimit && initOptions.costLimit === void 0 ? { costLimit: analyticsCostLimit } : {},
58570
59040
  ...policy ? { policy } : {},
58571
59041
  ...bundle.store && initOptions.store === void 0 ? { store: bundle.store } : {},
58572
59042
  ...sandbox && initOptions.sandbox === void 0 ? { sandbox } : {}
@@ -58580,7 +59050,22 @@ function defineDatabricksAgent(options = {}) {
58580
59050
  };
58581
59051
  return defineAgent(def);
58582
59052
  }
58583
- function databricksConfigFromEnv(env2) {
59053
+ function databricksAnalyticsCostLimitFromEnv(env2, config2, bundle) {
59054
+ if (env2.FABRIC_MOCK === "1") return void 0;
59055
+ const warehouseId = config2.warehouseId;
59056
+ const tenantId = env2.DATABRICKS_COST_TENANT_ID?.trim();
59057
+ const perDayUsd = Number(env2.DATABRICKS_COST_PER_DAY_USD);
59058
+ if (!warehouseId) throw new Error("Analytics copilot cost enforcement requires a SQL Warehouse.");
59059
+ if (!tenantId) throw new Error("Analytics copilot requires DATABRICKS_COST_TENANT_ID.");
59060
+ if (!Number.isFinite(perDayUsd) || perDayUsd <= 0) {
59061
+ throw new Error("Analytics copilot requires a positive DATABRICKS_COST_PER_DAY_USD.");
59062
+ }
59063
+ return databricksTenantCostLimit(bundle.sdk.statements, warehouseId, tenantId, {
59064
+ perDayUsd,
59065
+ onExceed: "throw"
59066
+ });
59067
+ }
59068
+ function databricksConfigFromEnv(env2, options = {}) {
58584
59069
  const rawHost = env2.DATABRICKS_HOST;
58585
59070
  const host = rawHost ? rawHost.includes("://") ? rawHost : `https://${rawHost}` : void 0;
58586
59071
  if (!host && env2.FABRIC_MOCK !== "1") {
@@ -58589,12 +59074,38 @@ function databricksConfigFromEnv(env2) {
58589
59074
  );
58590
59075
  }
58591
59076
  const principal = env2.FABRIC_MOCK === "1" ? { kind: "pat", token: "mock-token" } : databricksPrincipalFromEnv(env2);
59077
+ const analyticsCopilot = options.analyticsCopilot === true;
59078
+ const genieAgentId = env2.DATABRICKS_GENIE_SPACE_ID;
59079
+ const stewardAudience = env2.DATABRICKS_ANALYTICS_STEWARD_AUDIENCE;
59080
+ if (analyticsCopilot && env2.FABRIC_MOCK !== "1" && !env2.DATABRICKS_WAREHOUSE_ID) {
59081
+ throw new Error("Analytics copilot requires DATABRICKS_WAREHOUSE_ID.");
59082
+ }
59083
+ if (analyticsCopilot && env2.FABRIC_MOCK !== "1" && !genieAgentId) {
59084
+ throw new Error("Analytics copilot requires DATABRICKS_GENIE_SPACE_ID.");
59085
+ }
59086
+ if (analyticsCopilot && env2.FABRIC_MOCK !== "1" && !stewardAudience) {
59087
+ throw new Error("Analytics copilot requires DATABRICKS_ANALYTICS_STEWARD_AUDIENCE.");
59088
+ }
59089
+ const catalog = env2.DATABRICKS_CATALOG?.trim();
58592
59090
  return {
58593
59091
  host: host ?? "https://mock.databricks.com",
58594
59092
  principal,
58595
59093
  ...env2.DATABRICKS_MODEL ? { model: env2.DATABRICKS_MODEL } : {},
58596
59094
  ...env2.DATABRICKS_WAREHOUSE_ID ? { warehouseId: env2.DATABRICKS_WAREHOUSE_ID } : {},
58597
- ...env2.FABRIC_MOCK === "1" ? { warehouseId: "mock-warehouse-id" } : {}
59095
+ ...env2.FABRIC_MOCK === "1" ? { warehouseId: "mock-warehouse-id" } : {},
59096
+ ...analyticsCopilot ? {
59097
+ sqlRead: true,
59098
+ consumption: true,
59099
+ genie: {
59100
+ conversations: {
59101
+ agentId: genieAgentId ?? "mock-genie-agent"
59102
+ }
59103
+ },
59104
+ governance: analyticsCopilotGovernance({
59105
+ stewardAudience: stewardAudience ?? "analytics-stewards",
59106
+ ...catalog ? { catalogs: [catalog] } : {}
59107
+ })
59108
+ } : {}
58598
59109
  };
58599
59110
  }
58600
59111
  function resolveToolRefs(bundle, refs) {
@@ -58604,6 +59115,7 @@ function resolveToolRefs(bundle, refs) {
58604
59115
  }
58605
59116
  const shortNameMap = {
58606
59117
  sql: "databricks_sql",
59118
+ "sql-read": "sql_read",
58607
59119
  tables: "databricks_unity_catalog_tables",
58608
59120
  "table-info": "databricks_table_info",
58609
59121
  search: "search",