@fabric-harness/databricks 5.2.0 → 6.0.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
@@ -1803,6 +1803,295 @@ import {
1803
1803
  mergeCapabilityPolicies
1804
1804
  } from "@fabric-harness/sdk/strict";
1805
1805
 
1806
+ // src/cost-budget-actuals.ts
1807
+ import { parseScopeKey, tenantCostLimit } from "@fabric-harness/sdk";
1808
+
1809
+ // src/sql-exec.ts
1810
+ var TERMINAL_STATES = /* @__PURE__ */ new Set(["SUCCEEDED", "FAILED", "CANCELED", "CLOSED"]);
1811
+ async function runStatement(client, warehouseId, statement, parameters, poll) {
1812
+ const submit = await client.executeStatement({
1813
+ warehouseId,
1814
+ statement,
1815
+ waitTimeout: "30s",
1816
+ disposition: "INLINE",
1817
+ ...parameters.length > 0 ? { parameters } : {}
1818
+ });
1819
+ if (TERMINAL_STATES.has(submit.status?.state ?? "")) return submit;
1820
+ const id = submit.statementId;
1821
+ if (!id) return submit;
1822
+ const deadline = Date.now() + poll.timeoutMs;
1823
+ let current = submit;
1824
+ while (!TERMINAL_STATES.has(current.status?.state ?? "") && Date.now() < deadline) {
1825
+ await new Promise((resolve2) => setTimeout(resolve2, poll.intervalMs));
1826
+ current = await client.getStatementResult({ statementId: id });
1827
+ }
1828
+ return current;
1829
+ }
1830
+ function parseStatementRows(response) {
1831
+ const res = response;
1832
+ const columns = res.manifest?.schema?.columns ?? [];
1833
+ const names = columns.map((c2) => c2.name ?? "");
1834
+ const rows = res.result?.dataArray ?? [];
1835
+ return rows.map((row) => {
1836
+ const record2 = {};
1837
+ names.forEach((name, i2) => {
1838
+ record2[name] = row[i2];
1839
+ });
1840
+ return record2;
1841
+ });
1842
+ }
1843
+
1844
+ // src/cost-budget-actuals.ts
1845
+ function toNumber(value) {
1846
+ const n2 = typeof value === "number" ? value : Number(value);
1847
+ return Number.isFinite(n2) ? n2 : 0;
1848
+ }
1849
+ function deriveDateWindow(parsed) {
1850
+ const period = parsed.period;
1851
+ if (!period) return void 0;
1852
+ const parts = period.split(":");
1853
+ const periodType = parts[0];
1854
+ const periodValue = parts.slice(1).join(":");
1855
+ if (!periodValue) return void 0;
1856
+ switch (periodType) {
1857
+ case "day": {
1858
+ return { startDate: periodValue, endDate: periodValue };
1859
+ }
1860
+ case "hour": {
1861
+ const date5 = periodValue.match(/^(\d{4}-\d{2}-\d{2})/)?.[1];
1862
+ return date5 ? { startDate: date5, endDate: date5 } : void 0;
1863
+ }
1864
+ case "month": {
1865
+ const [year, month] = periodValue.split("-");
1866
+ if (!year || !month) return void 0;
1867
+ const lastDay = new Date(Number(year), Number(month), 0).getDate();
1868
+ return {
1869
+ startDate: `${year}-${month}-01`,
1870
+ endDate: `${year}-${month}-${String(lastDay).padStart(2, "0")}`
1871
+ };
1872
+ }
1873
+ default:
1874
+ return void 0;
1875
+ }
1876
+ }
1877
+ function buildCostQuery(tagPrefix, attribution) {
1878
+ const tagAgent = `${tagPrefix}.agent`;
1879
+ const tagTenant = `${tagPrefix}.tenant`;
1880
+ const tagUser = `${tagPrefix}.user`;
1881
+ const conditions = [
1882
+ "u.usage_date >= :start_date AND u.usage_date <= :end_date"
1883
+ ];
1884
+ if (attribution?.agentId) {
1885
+ conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
1886
+ }
1887
+ if (attribution?.tenantId) {
1888
+ conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
1889
+ }
1890
+ if (attribution?.userId) {
1891
+ conditions.push(`u.tags['${tagUser}'] = :user_id`);
1892
+ }
1893
+ return [
1894
+ "SELECT",
1895
+ " SUM(u.usage_quantity * lp.pricing.default) AS list_cost_usd,",
1896
+ " SUM(u.usage_quantity) AS quantity",
1897
+ "FROM system.billing.usage u",
1898
+ "LEFT JOIN system.billing.list_prices lp",
1899
+ " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
1900
+ " AND u.usage_end_time >= lp.price_start_time",
1901
+ " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
1902
+ `WHERE ${conditions.join(" AND ")}`
1903
+ ].join("\n");
1904
+ }
1905
+ function buildAttributionQuery(tagPrefix, filters) {
1906
+ const tagAgent = `${tagPrefix}.agent`;
1907
+ const tagTenant = `${tagPrefix}.tenant`;
1908
+ const tagUser = `${tagPrefix}.user`;
1909
+ const conditions = [
1910
+ "u.usage_date >= :start_date AND u.usage_date <= :end_date"
1911
+ ];
1912
+ const parameters = [
1913
+ { name: "start_date", value: filters.startDate ?? "", type: "DATE" },
1914
+ { name: "end_date", value: filters.endDate ?? "", type: "DATE" }
1915
+ ];
1916
+ if (filters.agentId) {
1917
+ conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
1918
+ parameters.push({ name: "agent_id", value: filters.agentId });
1919
+ }
1920
+ if (filters.tenantId) {
1921
+ conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
1922
+ parameters.push({ name: "tenant_id", value: filters.tenantId });
1923
+ }
1924
+ if (filters.userId) {
1925
+ conditions.push(`u.tags['${tagUser}'] = :user_id`);
1926
+ parameters.push({ name: "user_id", value: filters.userId });
1927
+ }
1928
+ if (filters.model) {
1929
+ conditions.push("u.sku_name = :model");
1930
+ parameters.push({ name: "model", value: filters.model });
1931
+ }
1932
+ if (filters.provider) {
1933
+ conditions.push("u.cloud = :provider");
1934
+ parameters.push({ name: "provider", value: filters.provider });
1935
+ }
1936
+ const groupBy = filters.groupBy ?? ["agentId", "tenantId", "userId", "model", "provider"];
1937
+ const groupColumns = [];
1938
+ const selectColumns = [];
1939
+ for (const dim of groupBy) {
1940
+ switch (dim) {
1941
+ case "agentId":
1942
+ selectColumns.push(`u.tags['${tagAgent}'] AS agentId`);
1943
+ groupColumns.push(`u.tags['${tagAgent}']`);
1944
+ break;
1945
+ case "tenantId":
1946
+ selectColumns.push(`u.tags['${tagTenant}'] AS tenantId`);
1947
+ groupColumns.push(`u.tags['${tagTenant}']`);
1948
+ break;
1949
+ case "userId":
1950
+ selectColumns.push(`u.tags['${tagUser}'] AS userId`);
1951
+ groupColumns.push(`u.tags['${tagUser}']`);
1952
+ break;
1953
+ case "model":
1954
+ selectColumns.push("u.sku_name AS model");
1955
+ groupColumns.push("u.sku_name");
1956
+ break;
1957
+ case "provider":
1958
+ selectColumns.push("u.cloud AS provider");
1959
+ groupColumns.push("u.cloud");
1960
+ break;
1961
+ case "sessionId":
1962
+ break;
1963
+ case "turnId":
1964
+ break;
1965
+ }
1966
+ }
1967
+ selectColumns.push(
1968
+ "CONCAT('day:', u.usage_date) AS period",
1969
+ "SUM(u.usage_quantity * lp.pricing.default) AS actualUsd",
1970
+ "SUM(u.usage_quantity) AS actualDbus",
1971
+ "COUNT(*) AS calls"
1972
+ );
1973
+ groupColumns.push("CONCAT('day:', u.usage_date)");
1974
+ const query = [
1975
+ "SELECT",
1976
+ ` ${selectColumns.join(",\n ")}`,
1977
+ "FROM system.billing.usage u",
1978
+ "LEFT JOIN system.billing.list_prices lp",
1979
+ " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
1980
+ " AND u.usage_end_time >= lp.price_start_time",
1981
+ " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
1982
+ `WHERE ${conditions.join(" AND ")}`,
1983
+ `GROUP BY ${groupColumns.join(", ")}`,
1984
+ "ORDER BY actualUsd DESC"
1985
+ ].join("\n");
1986
+ return { query, parameters };
1987
+ }
1988
+ function databricksActualCostSource(client, warehouseId, options = {}) {
1989
+ const cacheTtlMs = options.cacheTtlMs ?? 6e4;
1990
+ const tagPrefix = options.tagPrefix ?? "fabric";
1991
+ const poll = options.poll ?? { intervalMs: 2e3, timeoutMs: 12e4 };
1992
+ const cache = /* @__PURE__ */ new Map();
1993
+ return {
1994
+ async getUsd(scopeKey, attribution, queryOptions) {
1995
+ const ttl = queryOptions?.cacheTtlMs ?? cacheTtlMs;
1996
+ const now = Date.now();
1997
+ const cached4 = cache.get(scopeKey);
1998
+ if (cached4 && cached4.expiresAt > now) {
1999
+ const result2 = {
2000
+ usd: cached4.usd
2001
+ };
2002
+ if (cached4.dbus !== void 0) result2.dbus = cached4.dbus;
2003
+ result2.actualsFetchedAt = cached4.fetchedAt;
2004
+ result2.cacheTtlMs = ttl;
2005
+ return result2;
2006
+ }
2007
+ const parsed = parseScopeKey(scopeKey);
2008
+ if (!parsed) {
2009
+ return { usd: 0, cacheTtlMs: ttl };
2010
+ }
2011
+ const dateWindow = deriveDateWindow(parsed);
2012
+ if (!dateWindow) {
2013
+ return { usd: 0, cacheTtlMs: ttl };
2014
+ }
2015
+ const parameters = [
2016
+ { name: "start_date", value: dateWindow.startDate, type: "DATE" },
2017
+ { name: "end_date", value: dateWindow.endDate, type: "DATE" }
2018
+ ];
2019
+ if (attribution?.agentId) {
2020
+ parameters.push({ name: "agent_id", value: attribution.agentId });
2021
+ }
2022
+ if (attribution?.tenantId) {
2023
+ parameters.push({ name: "tenant_id", value: attribution.tenantId });
2024
+ }
2025
+ if (attribution?.userId) {
2026
+ parameters.push({ name: "user_id", value: attribution.userId });
2027
+ }
2028
+ const response = await runStatement(
2029
+ client,
2030
+ warehouseId,
2031
+ buildCostQuery(tagPrefix, attribution),
2032
+ parameters,
2033
+ poll
2034
+ );
2035
+ const rows = parseStatementRows(response);
2036
+ const usd = rows.reduce((sum, r2) => sum + toNumber(r2.list_cost_usd), 0);
2037
+ const dbus = rows.reduce((sum, r2) => sum + toNumber(r2.quantity), 0);
2038
+ const fetchedAt = (/* @__PURE__ */ new Date()).toISOString();
2039
+ cache.set(scopeKey, {
2040
+ usd,
2041
+ dbus: dbus || void 0,
2042
+ fetchedAt,
2043
+ expiresAt: now + ttl
2044
+ });
2045
+ const result = {
2046
+ usd
2047
+ };
2048
+ if (dbus) result.dbus = dbus;
2049
+ result.actualsFetchedAt = fetchedAt;
2050
+ result.cacheTtlMs = ttl;
2051
+ return result;
2052
+ },
2053
+ async queryAttribution(filters) {
2054
+ if (!filters.startDate || !filters.endDate) {
2055
+ throw new Error("queryAttribution requires startDate and endDate");
2056
+ }
2057
+ const { query, parameters } = buildAttributionQuery(tagPrefix, filters);
2058
+ const response = await runStatement(client, warehouseId, query, parameters, poll);
2059
+ const rows = parseStatementRows(response);
2060
+ return rows.map((r2) => {
2061
+ const row = {
2062
+ period: String(r2.period ?? ""),
2063
+ estimatedUsd: 0,
2064
+ actualUsd: toNumber(r2.actualUsd),
2065
+ actualDbus: toNumber(r2.actualDbus),
2066
+ deltaUsd: toNumber(r2.actualUsd),
2067
+ calls: toNumber(r2.calls)
2068
+ };
2069
+ if (r2.agentId) row.agentId = String(r2.agentId);
2070
+ if (r2.userId) row.userId = String(r2.userId);
2071
+ if (r2.tenantId) row.tenantId = String(r2.tenantId);
2072
+ if (r2.model) row.model = String(r2.model);
2073
+ if (r2.provider) row.provider = String(r2.provider);
2074
+ return row;
2075
+ });
2076
+ }
2077
+ };
2078
+ }
2079
+ function databricksTenantCostLimit(client, warehouseId, tenantId, options) {
2080
+ const base2 = tenantCostLimit(tenantId, {
2081
+ ...options.perHourUsd !== void 0 ? { perHourUsd: options.perHourUsd } : {},
2082
+ ...options.perDayUsd !== void 0 ? { perDayUsd: options.perDayUsd } : {},
2083
+ ...options.perMonthUsd !== void 0 ? { perMonthUsd: options.perMonthUsd } : {},
2084
+ ...options.onExceed ? { onExceed: options.onExceed } : {}
2085
+ });
2086
+ const opts = {};
2087
+ if (options.cacheTtlMs !== void 0) opts.cacheTtlMs = options.cacheTtlMs;
2088
+ return {
2089
+ ...base2,
2090
+ scopeSource: "external",
2091
+ actualSource: databricksActualCostSource(client, warehouseId, opts)
2092
+ };
2093
+ }
2094
+
1806
2095
  // src/governance.ts
1807
2096
  import {
1808
2097
  approvalInputDigest,
@@ -17633,295 +17922,6 @@ function fabricPrincipalFor(principal, overrides = {}) {
17633
17922
  };
17634
17923
  }
17635
17924
 
17636
- // src/cost-budget-actuals.ts
17637
- import { parseScopeKey, tenantCostLimit } from "@fabric-harness/sdk";
17638
-
17639
- // src/sql-exec.ts
17640
- var TERMINAL_STATES = /* @__PURE__ */ new Set(["SUCCEEDED", "FAILED", "CANCELED", "CLOSED"]);
17641
- async function runStatement(client, warehouseId, statement, parameters, poll) {
17642
- const submit = await client.executeStatement({
17643
- warehouseId,
17644
- statement,
17645
- waitTimeout: "30s",
17646
- disposition: "INLINE",
17647
- ...parameters.length > 0 ? { parameters } : {}
17648
- });
17649
- if (TERMINAL_STATES.has(submit.status?.state ?? "")) return submit;
17650
- const id = submit.statementId;
17651
- if (!id) return submit;
17652
- const deadline = Date.now() + poll.timeoutMs;
17653
- let current = submit;
17654
- while (!TERMINAL_STATES.has(current.status?.state ?? "") && Date.now() < deadline) {
17655
- await new Promise((resolve2) => setTimeout(resolve2, poll.intervalMs));
17656
- current = await client.getStatementResult({ statementId: id });
17657
- }
17658
- return current;
17659
- }
17660
- function parseStatementRows(response) {
17661
- const res = response;
17662
- const columns = res.manifest?.schema?.columns ?? [];
17663
- const names = columns.map((c2) => c2.name ?? "");
17664
- const rows = res.result?.dataArray ?? [];
17665
- return rows.map((row) => {
17666
- const record2 = {};
17667
- names.forEach((name, i2) => {
17668
- record2[name] = row[i2];
17669
- });
17670
- return record2;
17671
- });
17672
- }
17673
-
17674
- // src/cost-budget-actuals.ts
17675
- function toNumber(value) {
17676
- const n2 = typeof value === "number" ? value : Number(value);
17677
- return Number.isFinite(n2) ? n2 : 0;
17678
- }
17679
- function deriveDateWindow(parsed) {
17680
- const period = parsed.period;
17681
- if (!period) return void 0;
17682
- const parts = period.split(":");
17683
- const periodType = parts[0];
17684
- const periodValue = parts.slice(1).join(":");
17685
- if (!periodValue) return void 0;
17686
- switch (periodType) {
17687
- case "day": {
17688
- return { startDate: periodValue, endDate: periodValue };
17689
- }
17690
- case "hour": {
17691
- const date5 = periodValue.match(/^(\d{4}-\d{2}-\d{2})/)?.[1];
17692
- return date5 ? { startDate: date5, endDate: date5 } : void 0;
17693
- }
17694
- case "month": {
17695
- const [year, month] = periodValue.split("-");
17696
- if (!year || !month) return void 0;
17697
- const lastDay = new Date(Number(year), Number(month), 0).getDate();
17698
- return {
17699
- startDate: `${year}-${month}-01`,
17700
- endDate: `${year}-${month}-${String(lastDay).padStart(2, "0")}`
17701
- };
17702
- }
17703
- default:
17704
- return void 0;
17705
- }
17706
- }
17707
- function buildCostQuery(tagPrefix, attribution) {
17708
- const tagAgent = `${tagPrefix}.agent`;
17709
- const tagTenant = `${tagPrefix}.tenant`;
17710
- const tagUser = `${tagPrefix}.user`;
17711
- const conditions = [
17712
- "u.usage_date >= :start_date AND u.usage_date <= :end_date"
17713
- ];
17714
- if (attribution?.agentId) {
17715
- conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
17716
- }
17717
- if (attribution?.tenantId) {
17718
- conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
17719
- }
17720
- if (attribution?.userId) {
17721
- conditions.push(`u.tags['${tagUser}'] = :user_id`);
17722
- }
17723
- return [
17724
- "SELECT",
17725
- " SUM(u.usage_quantity * lp.pricing.default) AS list_cost_usd,",
17726
- " SUM(u.usage_quantity) AS quantity",
17727
- "FROM system.billing.usage u",
17728
- "LEFT JOIN system.billing.list_prices lp",
17729
- " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
17730
- " AND u.usage_end_time >= lp.price_start_time",
17731
- " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
17732
- `WHERE ${conditions.join(" AND ")}`
17733
- ].join("\n");
17734
- }
17735
- function buildAttributionQuery(tagPrefix, filters) {
17736
- const tagAgent = `${tagPrefix}.agent`;
17737
- const tagTenant = `${tagPrefix}.tenant`;
17738
- const tagUser = `${tagPrefix}.user`;
17739
- const conditions = [
17740
- "u.usage_date >= :start_date AND u.usage_date <= :end_date"
17741
- ];
17742
- const parameters = [
17743
- { name: "start_date", value: filters.startDate ?? "", type: "DATE" },
17744
- { name: "end_date", value: filters.endDate ?? "", type: "DATE" }
17745
- ];
17746
- if (filters.agentId) {
17747
- conditions.push(`u.tags['${tagAgent}'] = :agent_id`);
17748
- parameters.push({ name: "agent_id", value: filters.agentId });
17749
- }
17750
- if (filters.tenantId) {
17751
- conditions.push(`u.tags['${tagTenant}'] = :tenant_id`);
17752
- parameters.push({ name: "tenant_id", value: filters.tenantId });
17753
- }
17754
- if (filters.userId) {
17755
- conditions.push(`u.tags['${tagUser}'] = :user_id`);
17756
- parameters.push({ name: "user_id", value: filters.userId });
17757
- }
17758
- if (filters.model) {
17759
- conditions.push("u.sku_name = :model");
17760
- parameters.push({ name: "model", value: filters.model });
17761
- }
17762
- if (filters.provider) {
17763
- conditions.push("u.cloud = :provider");
17764
- parameters.push({ name: "provider", value: filters.provider });
17765
- }
17766
- const groupBy = filters.groupBy ?? ["agentId", "tenantId", "userId", "model", "provider"];
17767
- const groupColumns = [];
17768
- const selectColumns = [];
17769
- for (const dim of groupBy) {
17770
- switch (dim) {
17771
- case "agentId":
17772
- selectColumns.push(`u.tags['${tagAgent}'] AS agentId`);
17773
- groupColumns.push(`u.tags['${tagAgent}']`);
17774
- break;
17775
- case "tenantId":
17776
- selectColumns.push(`u.tags['${tagTenant}'] AS tenantId`);
17777
- groupColumns.push(`u.tags['${tagTenant}']`);
17778
- break;
17779
- case "userId":
17780
- selectColumns.push(`u.tags['${tagUser}'] AS userId`);
17781
- groupColumns.push(`u.tags['${tagUser}']`);
17782
- break;
17783
- case "model":
17784
- selectColumns.push("u.sku_name AS model");
17785
- groupColumns.push("u.sku_name");
17786
- break;
17787
- case "provider":
17788
- selectColumns.push("u.cloud AS provider");
17789
- groupColumns.push("u.cloud");
17790
- break;
17791
- case "sessionId":
17792
- break;
17793
- case "turnId":
17794
- break;
17795
- }
17796
- }
17797
- selectColumns.push(
17798
- "CONCAT('day:', u.usage_date) AS period",
17799
- "SUM(u.usage_quantity * lp.pricing.default) AS actualUsd",
17800
- "SUM(u.usage_quantity) AS actualDbus",
17801
- "COUNT(*) AS calls"
17802
- );
17803
- groupColumns.push("CONCAT('day:', u.usage_date)");
17804
- const query = [
17805
- "SELECT",
17806
- ` ${selectColumns.join(",\n ")}`,
17807
- "FROM system.billing.usage u",
17808
- "LEFT JOIN system.billing.list_prices lp",
17809
- " ON u.cloud = lp.cloud AND u.sku_name = lp.sku_name AND u.usage_unit = lp.usage_unit",
17810
- " AND u.usage_end_time >= lp.price_start_time",
17811
- " AND (lp.price_end_time IS NULL OR u.usage_end_time < lp.price_end_time)",
17812
- `WHERE ${conditions.join(" AND ")}`,
17813
- `GROUP BY ${groupColumns.join(", ")}`,
17814
- "ORDER BY actualUsd DESC"
17815
- ].join("\n");
17816
- return { query, parameters };
17817
- }
17818
- function databricksActualCostSource(client, warehouseId, options = {}) {
17819
- const cacheTtlMs = options.cacheTtlMs ?? 6e4;
17820
- const tagPrefix = options.tagPrefix ?? "fabric";
17821
- const poll = options.poll ?? { intervalMs: 2e3, timeoutMs: 12e4 };
17822
- const cache = /* @__PURE__ */ new Map();
17823
- return {
17824
- async getUsd(scopeKey, attribution, queryOptions) {
17825
- const ttl = queryOptions?.cacheTtlMs ?? cacheTtlMs;
17826
- const now = Date.now();
17827
- const cached4 = cache.get(scopeKey);
17828
- if (cached4 && cached4.expiresAt > now) {
17829
- const result2 = {
17830
- usd: cached4.usd
17831
- };
17832
- if (cached4.dbus !== void 0) result2.dbus = cached4.dbus;
17833
- result2.actualsFetchedAt = cached4.fetchedAt;
17834
- result2.cacheTtlMs = ttl;
17835
- return result2;
17836
- }
17837
- const parsed = parseScopeKey(scopeKey);
17838
- if (!parsed) {
17839
- return { usd: 0, cacheTtlMs: ttl };
17840
- }
17841
- const dateWindow = deriveDateWindow(parsed);
17842
- if (!dateWindow) {
17843
- return { usd: 0, cacheTtlMs: ttl };
17844
- }
17845
- const parameters = [
17846
- { name: "start_date", value: dateWindow.startDate, type: "DATE" },
17847
- { name: "end_date", value: dateWindow.endDate, type: "DATE" }
17848
- ];
17849
- if (attribution?.agentId) {
17850
- parameters.push({ name: "agent_id", value: attribution.agentId });
17851
- }
17852
- if (attribution?.tenantId) {
17853
- parameters.push({ name: "tenant_id", value: attribution.tenantId });
17854
- }
17855
- if (attribution?.userId) {
17856
- parameters.push({ name: "user_id", value: attribution.userId });
17857
- }
17858
- const response = await runStatement(
17859
- client,
17860
- warehouseId,
17861
- buildCostQuery(tagPrefix, attribution),
17862
- parameters,
17863
- poll
17864
- );
17865
- const rows = parseStatementRows(response);
17866
- const usd = rows.reduce((sum, r2) => sum + toNumber(r2.list_cost_usd), 0);
17867
- const dbus = rows.reduce((sum, r2) => sum + toNumber(r2.quantity), 0);
17868
- const fetchedAt = (/* @__PURE__ */ new Date()).toISOString();
17869
- cache.set(scopeKey, {
17870
- usd,
17871
- dbus: dbus || void 0,
17872
- fetchedAt,
17873
- expiresAt: now + ttl
17874
- });
17875
- const result = {
17876
- usd
17877
- };
17878
- if (dbus) result.dbus = dbus;
17879
- result.actualsFetchedAt = fetchedAt;
17880
- result.cacheTtlMs = ttl;
17881
- return result;
17882
- },
17883
- async queryAttribution(filters) {
17884
- if (!filters.startDate || !filters.endDate) {
17885
- throw new Error("queryAttribution requires startDate and endDate");
17886
- }
17887
- const { query, parameters } = buildAttributionQuery(tagPrefix, filters);
17888
- const response = await runStatement(client, warehouseId, query, parameters, poll);
17889
- const rows = parseStatementRows(response);
17890
- return rows.map((r2) => {
17891
- const row = {
17892
- period: String(r2.period ?? ""),
17893
- estimatedUsd: 0,
17894
- actualUsd: toNumber(r2.actualUsd),
17895
- actualDbus: toNumber(r2.actualDbus),
17896
- deltaUsd: toNumber(r2.actualUsd),
17897
- calls: toNumber(r2.calls)
17898
- };
17899
- if (r2.agentId) row.agentId = String(r2.agentId);
17900
- if (r2.userId) row.userId = String(r2.userId);
17901
- if (r2.tenantId) row.tenantId = String(r2.tenantId);
17902
- if (r2.model) row.model = String(r2.model);
17903
- if (r2.provider) row.provider = String(r2.provider);
17904
- return row;
17905
- });
17906
- }
17907
- };
17908
- }
17909
- function databricksTenantCostLimit(client, warehouseId, tenantId, options) {
17910
- const base2 = tenantCostLimit(tenantId, {
17911
- ...options.perHourUsd !== void 0 ? { perHourUsd: options.perHourUsd } : {},
17912
- ...options.perDayUsd !== void 0 ? { perDayUsd: options.perDayUsd } : {},
17913
- ...options.perMonthUsd !== void 0 ? { perMonthUsd: options.perMonthUsd } : {},
17914
- ...options.onExceed ? { onExceed: options.onExceed } : {}
17915
- });
17916
- const opts = {};
17917
- if (options.cacheTtlMs !== void 0) opts.cacheTtlMs = options.cacheTtlMs;
17918
- return {
17919
- ...base2,
17920
- scopeSource: "external",
17921
- actualSource: databricksActualCostSource(client, warehouseId, opts)
17922
- };
17923
- }
17924
-
17925
17925
  // src/index.ts
17926
17926
  import { createSearchTool } from "@fabric-harness/sdk";
17927
17927
 
@@ -18390,26 +18390,87 @@ function jitter(ms) {
18390
18390
  return Math.max(1, Math.round(ms * (0.9 + Math.random() * 0.2)));
18391
18391
  }
18392
18392
 
18393
+ // src/resource-policy.ts
18394
+ var DatabricksResourceNotAllowedError = class extends Error {
18395
+ code = "DATABRICKS_RESOURCE_NOT_ALLOWED";
18396
+ resourceKind;
18397
+ constructor(resourceKind) {
18398
+ super(`Databricks ${resourceKind} is not allowed by the configured policy.`);
18399
+ this.name = "DatabricksResourceNotAllowedError";
18400
+ this.resourceKind = resourceKind;
18401
+ }
18402
+ };
18403
+ function resolveExactResourcePolicy(policy, allowedKey, allowAnyKey, resourceKind) {
18404
+ if (!policy || typeof policy !== "object" || Array.isArray(policy))
18405
+ throw new TypeError(`Databricks ${resourceKind} policy must be an object.`);
18406
+ const value = policy;
18407
+ const allowed = value[allowedKey];
18408
+ const allowAny = value[allowAnyKey];
18409
+ if (allowAny === true && allowed === void 0) return void 0;
18410
+ if (Array.isArray(allowed) && allowed.length > 0 && allowed.every((entry) => typeof entry === "string" && entry.trim().length > 0) && allowAny === void 0)
18411
+ return [...new Set(allowed.map((entry) => entry.trim()))];
18412
+ throw new TypeError(
18413
+ `Databricks ${resourceKind} policy must set either non-empty ${allowedKey} or ${allowAnyKey}: true.`
18414
+ );
18415
+ }
18416
+ function assertExactResourceAllowed(resourceKind, value, allowed) {
18417
+ if (allowed && !allowed.includes(value.trim()))
18418
+ throw new DatabricksResourceNotAllowedError(resourceKind);
18419
+ }
18420
+ function pinnedStringResourceSchema(allowed) {
18421
+ if (!allowed) return { type: "string" };
18422
+ if (allowed.length === 1) return { type: "string", const: allowed[0] };
18423
+ return { type: "string", enum: [...allowed] };
18424
+ }
18425
+
18393
18426
  // src/ai.ts
18394
- function databricksAiQueryTool(client, options) {
18395
- const poll = { intervalMs: options.pollIntervalMs ?? 2e3, timeoutMs: options.timeoutMs ?? 12e4 };
18427
+ function databricksAiQueryTool(client, endpointPolicy, options) {
18428
+ const poll = {
18429
+ intervalMs: options.pollIntervalMs ?? 2e3,
18430
+ timeoutMs: options.timeoutMs ?? 12e4
18431
+ };
18432
+ const allowedEndpoints = resolveExactResourcePolicy(
18433
+ endpointPolicy,
18434
+ "allowedEndpoints",
18435
+ "allowAnyEndpoint",
18436
+ "AI Functions endpoint"
18437
+ );
18396
18438
  return {
18397
18439
  name: options.name ?? "databricks_ai_query",
18398
18440
  description: options.description ?? "Run model inference inside the SQL warehouse via ai_query() against a Databricks serving endpoint.",
18399
18441
  inputSchema: {
18400
18442
  type: "object",
18401
18443
  additionalProperties: false,
18402
- properties: { endpoint: { type: "string" }, request: { type: "string" } },
18444
+ properties: {
18445
+ endpoint: pinnedStringResourceSchema(allowedEndpoints),
18446
+ request: { type: "string" }
18447
+ },
18403
18448
  required: ["endpoint", "request"]
18404
18449
  },
18405
- metadata: { effect: "execute", provider: "databricks", service: "ai-functions" },
18406
- execute: (input) => runStatement(
18407
- client,
18408
- options.warehouseId,
18409
- "SELECT ai_query(:endpoint, :request) AS result",
18410
- [{ name: "endpoint", value: input.endpoint }, { name: "request", value: input.request }],
18411
- poll
18412
- )
18450
+ metadata: {
18451
+ effect: "execute",
18452
+ provider: "databricks",
18453
+ service: "ai-functions",
18454
+ governance: {
18455
+ resources: [
18456
+ { kind: "serving-endpoint", path: "/endpoint" },
18457
+ { kind: "sql-warehouse", value: options.warehouseId }
18458
+ ]
18459
+ }
18460
+ },
18461
+ async execute(input) {
18462
+ assertExactResourceAllowed("AI Functions endpoint", input.endpoint, allowedEndpoints);
18463
+ return await runStatement(
18464
+ client,
18465
+ options.warehouseId,
18466
+ "SELECT ai_query(:endpoint, :request) AS result",
18467
+ [
18468
+ { name: "endpoint", value: input.endpoint },
18469
+ { name: "request", value: input.request }
18470
+ ],
18471
+ poll
18472
+ );
18473
+ }
18413
18474
  };
18414
18475
  }
18415
18476
 
@@ -22101,43 +22162,76 @@ function databricksPipelineStatusTool(client, options = {}) {
22101
22162
  execute: (input) => client.get({ pipelineId: input.pipelineId })
22102
22163
  };
22103
22164
  }
22104
- function databricksPipelineStartTool(client, options = {}) {
22165
+ function databricksPipelineStartTool(client, runPolicy, options = {}) {
22166
+ const allowedPipelineIds = resolveExactResourcePolicy(
22167
+ runPolicy,
22168
+ "allowedPipelineIds",
22169
+ "allowAnyPipelineId",
22170
+ "Lakeflow pipeline"
22171
+ );
22105
22172
  return {
22106
22173
  name: options.name ?? "databricks_pipeline_start",
22107
22174
  description: options.description ?? "Trigger a Lakeflow pipeline update (run). Set fullRefresh to reprocess all data.",
22108
22175
  inputSchema: {
22109
22176
  type: "object",
22110
22177
  additionalProperties: false,
22111
- properties: { pipelineId: { type: "string" }, fullRefresh: { type: "boolean" } },
22178
+ properties: {
22179
+ pipelineId: pinnedStringResourceSchema(allowedPipelineIds),
22180
+ fullRefresh: { type: "boolean" }
22181
+ },
22112
22182
  required: ["pipelineId"]
22113
22183
  },
22114
- metadata: { effect: "execute", provider: "databricks", service: "lakeflow" },
22115
- execute: (input) => client.start({
22116
- pipelineId: input.pipelineId,
22117
- ...input.fullRefresh !== void 0 ? { fullRefresh: input.fullRefresh } : {}
22118
- })
22184
+ metadata: {
22185
+ effect: "execute",
22186
+ provider: "databricks",
22187
+ service: "lakeflow",
22188
+ governance: { resources: [{ kind: "pipeline", path: "/pipelineId" }] }
22189
+ },
22190
+ async execute(input) {
22191
+ assertExactResourceAllowed("Lakeflow pipeline", input.pipelineId, allowedPipelineIds);
22192
+ return await client.start({
22193
+ pipelineId: input.pipelineId,
22194
+ ...input.fullRefresh !== void 0 ? { fullRefresh: input.fullRefresh } : {}
22195
+ });
22196
+ }
22119
22197
  };
22120
22198
  }
22121
- function databricksPipelineStopTool(client, options = {}) {
22199
+ function databricksPipelineStopTool(client, runPolicy, options = {}) {
22200
+ const allowedPipelineIds = resolveExactResourcePolicy(
22201
+ runPolicy,
22202
+ "allowedPipelineIds",
22203
+ "allowAnyPipelineId",
22204
+ "Lakeflow pipeline"
22205
+ );
22122
22206
  return {
22123
22207
  name: options.name ?? "databricks_pipeline_stop",
22124
22208
  description: options.description ?? "Stop the active update of a Lakeflow pipeline.",
22125
22209
  inputSchema: {
22126
22210
  type: "object",
22127
22211
  additionalProperties: false,
22128
- properties: { pipelineId: { type: "string" } },
22212
+ properties: { pipelineId: pinnedStringResourceSchema(allowedPipelineIds) },
22129
22213
  required: ["pipelineId"]
22130
22214
  },
22131
- metadata: { effect: "execute", provider: "databricks", service: "lakeflow" },
22132
- execute: (input) => client.stop({ pipelineId: input.pipelineId })
22215
+ metadata: {
22216
+ effect: "execute",
22217
+ provider: "databricks",
22218
+ service: "lakeflow",
22219
+ governance: { resources: [{ kind: "pipeline", path: "/pipelineId" }] }
22220
+ },
22221
+ async execute(input) {
22222
+ assertExactResourceAllowed("Lakeflow pipeline", input.pipelineId, allowedPipelineIds);
22223
+ return await client.stop({ pipelineId: input.pipelineId });
22224
+ }
22133
22225
  };
22134
22226
  }
22135
- function databricksLakeflowTools(client) {
22227
+ function databricksLakeflowTools(client, runPolicy) {
22136
22228
  return [
22137
22229
  databricksPipelineListTool(client),
22138
22230
  databricksPipelineStatusTool(client),
22139
- databricksPipelineStartTool(client),
22140
- databricksPipelineStopTool(client)
22231
+ ...runPolicy ? [
22232
+ databricksPipelineStartTool(client, runPolicy),
22233
+ databricksPipelineStopTool(client, runPolicy)
22234
+ ] : []
22141
22235
  ];
22142
22236
  }
22143
22237
 
@@ -58755,7 +58849,8 @@ function lexSqlReadStatement(statement) {
58755
58849
  function assertDatabricksSqlReadStatement(statement) {
58756
58850
  if (!statement.trim()) throw new Error("Databricks SQL read denied: statement cannot be empty.");
58757
58851
  const { tokens, semicolons } = lexSqlReadStatement(statement);
58758
- if (tokens.length === 0) throw new Error("Databricks SQL read denied: expected a SELECT statement.");
58852
+ if (tokens.length === 0)
58853
+ throw new Error("Databricks SQL read denied: expected a SELECT statement.");
58759
58854
  if (semicolons.length > 1)
58760
58855
  throw new Error("Databricks SQL read denied: multiple statements are not allowed.");
58761
58856
  if (semicolons.length === 1) {
@@ -58773,7 +58868,8 @@ function assertDatabricksSqlReadStatement(statement) {
58773
58868
  if (unsafe)
58774
58869
  throw new Error(`Databricks SQL read denied: unsafe keyword ${unsafe.value} is not allowed.`);
58775
58870
  }
58776
- function databricksSqlTool(client, options) {
58871
+ function databricksSqlTool(client, policy, options) {
58872
+ const executionPolicy = resolveSqlExecutionPolicy(policy);
58777
58873
  return {
58778
58874
  name: options.name ?? "databricks_sql",
58779
58875
  description: options.description ?? "Run a SQL statement on a Databricks SQL Warehouse.",
@@ -58781,23 +58877,55 @@ function databricksSqlTool(client, options) {
58781
58877
  type: "object",
58782
58878
  additionalProperties: false,
58783
58879
  properties: {
58784
- statement: { type: "string" },
58880
+ statement: executionPolicy.kind === "allowlist" ? pinnedStringResourceSchema(executionPolicy.statements) : { type: "string" },
58785
58881
  catalog: { type: "string" },
58786
58882
  schema: { type: "string" }
58787
58883
  },
58788
58884
  required: ["statement"]
58789
58885
  },
58790
- metadata: { effect: "execute", provider: "databricks", service: "sql" },
58791
- execute: (input) => client.executeStatement({
58792
- warehouseId: options.warehouseId,
58793
- statement: input.statement,
58794
- waitTimeout: options.waitTimeout ?? "30s",
58795
- disposition: options.disposition ?? "INLINE",
58796
- ...input.catalog ? { catalog: input.catalog } : {},
58797
- ...input.schema ? { schema: input.schema } : {}
58798
- })
58886
+ metadata: {
58887
+ effect: "execute",
58888
+ provider: "databricks",
58889
+ service: "sql",
58890
+ governance: {
58891
+ resources: [{ kind: "sql-warehouse", value: options.warehouseId }]
58892
+ }
58893
+ },
58894
+ async execute(input) {
58895
+ await assertSqlExecutionAllowed(input, executionPolicy);
58896
+ return await client.executeStatement({
58897
+ warehouseId: options.warehouseId,
58898
+ statement: input.statement,
58899
+ waitTimeout: options.waitTimeout ?? "30s",
58900
+ disposition: options.disposition ?? "INLINE",
58901
+ ...input.catalog ? { catalog: input.catalog } : {},
58902
+ ...input.schema ? { schema: input.schema } : {}
58903
+ });
58904
+ }
58799
58905
  };
58800
58906
  }
58907
+ function resolveSqlExecutionPolicy(policy) {
58908
+ if (!policy || typeof policy !== "object" || Array.isArray(policy))
58909
+ throw new TypeError("Databricks SQL execution policy must be an object.");
58910
+ if ("validateStatement" in policy && typeof policy.validateStatement === "function")
58911
+ return { kind: "validator", validate: policy.validateStatement };
58912
+ const statements = resolveExactResourcePolicy(
58913
+ policy,
58914
+ "allowedStatements",
58915
+ "allowAnyStatement",
58916
+ "SQL statement"
58917
+ );
58918
+ return statements ? { kind: "allowlist", statements } : { kind: "allow-any" };
58919
+ }
58920
+ async function assertSqlExecutionAllowed(input, policy) {
58921
+ if (policy.kind === "allow-any") return;
58922
+ if (policy.kind === "allowlist") {
58923
+ assertExactResourceAllowed("SQL statement", input.statement, policy.statements);
58924
+ return;
58925
+ }
58926
+ if (await policy.validate(input) !== true)
58927
+ throw new DatabricksResourceNotAllowedError("SQL statement");
58928
+ }
58801
58929
  function databricksSqlReadTool(client, options) {
58802
58930
  return {
58803
58931
  name: options.name ?? "sql_read",
@@ -58817,9 +58945,7 @@ function databricksSqlReadTool(client, options) {
58817
58945
  provider: "databricks",
58818
58946
  service: "sql",
58819
58947
  governance: {
58820
- resources: [
58821
- { kind: "catalog", path: "/catalog", catalogQualified: true, optional: true }
58822
- ]
58948
+ resources: [{ kind: "catalog", path: "/catalog", catalogQualified: true, optional: true }]
58823
58949
  }
58824
58950
  },
58825
58951
  async execute(input) {
@@ -59687,11 +59813,19 @@ function buildDatabricksBundle(config2, scope) {
59687
59813
  }
59688
59814
  const rawTools = [
59689
59815
  ...config2.sqlRead && config2.warehouseId ? [databricksSqlReadTool(native.statements, { warehouseId: config2.warehouseId })] : [],
59690
- ...config2.warehouseId ? [databricksSqlTool(native.statements, { warehouseId: config2.warehouseId })] : [],
59816
+ ...config2.sqlExecute && config2.warehouseId ? [
59817
+ databricksSqlTool(native.statements, config2.sqlExecute, {
59818
+ warehouseId: config2.warehouseId
59819
+ })
59820
+ ] : [],
59691
59821
  unityCatalogTablesTool(native.tables),
59692
59822
  databricksTableInfoTool(native.tables),
59693
59823
  ...searchTool ? [searchTool] : [],
59694
- ...config2.aiFunctions && config2.warehouseId ? [databricksAiQueryTool(native.statements, { warehouseId: config2.warehouseId })] : [],
59824
+ ...config2.aiFunctions && config2.warehouseId ? [
59825
+ databricksAiQueryTool(native.statements, config2.aiFunctions, {
59826
+ warehouseId: config2.warehouseId
59827
+ })
59828
+ ] : [],
59695
59829
  ...genie.conversations ? [
59696
59830
  databricksGenieTool(native.genie, {
59697
59831
  ...genie.conversations,
@@ -59717,7 +59851,10 @@ function buildDatabricksBundle(config2, scope) {
59717
59851
  ...genie.manage.sqlPolicy ? { sqlPolicy: genie.manage.sqlPolicy } : {}
59718
59852
  }
59719
59853
  ) : [],
59720
- ...config2.lakeflow ? databricksLakeflowTools(native.pipelines) : [],
59854
+ ...config2.lakeflow ? databricksLakeflowTools(
59855
+ native.pipelines,
59856
+ typeof config2.lakeflow === "object" ? config2.lakeflow.runPolicy : void 0
59857
+ ) : [],
59721
59858
  ...config2.consumption && config2.warehouseId ? [
59722
59859
  databricksConsumptionTool(native.statements, {
59723
59860
  warehouseId: config2.warehouseId
@@ -59754,16 +59891,19 @@ function buildDatabricksBundle(config2, scope) {
59754
59891
  ...config2.tools ?? []
59755
59892
  ];
59756
59893
  const authoringServices = enabledAuthoringServices(config2);
59757
- const customTools = new Set(config2.tools ?? []);
59758
- const authoringToolNames = new Set(
59894
+ const approvalServices = config2.governance?.approvalServices;
59895
+ const approvalRequiredToolNames = new Set(
59759
59896
  rawTools.filter((tool7) => {
59760
59897
  const effect = tool7.metadata?.effect;
59761
- return (effect === "write" || effect === "execute") && Boolean(governanceMetadata(tool7));
59898
+ if (effect !== "write" && effect !== "execute") return false;
59899
+ if (!approvalServices) return true;
59900
+ const service = tool7.metadata?.service;
59901
+ return typeof service === "string" && approvalServices.includes(service);
59762
59902
  }).map((tool7) => tool7.name)
59763
59903
  );
59764
59904
  for (const tool7 of rawTools) {
59765
59905
  const effect = tool7.metadata?.effect;
59766
- if ((authoringToolNames.has(tool7.name) || customTools.has(tool7)) && (effect === "write" || effect === "execute"))
59906
+ if (effect === "write" || effect === "execute")
59767
59907
  validateDatabricksGovernanceDescriptor(tool7, true);
59768
59908
  }
59769
59909
  const tools = withGovernanceTools(rawTools, {
@@ -59771,7 +59911,7 @@ function buildDatabricksBundle(config2, scope) {
59771
59911
  ...config2.governance?.onLineage ? { onLineage: config2.governance.onLineage } : {},
59772
59912
  ...config2.governance?.catalogs ? { catalogs: config2.governance.catalogs } : {},
59773
59913
  executingPrincipal,
59774
- ...config2.governance?.stewardAudience && !config2.allowUnapprovedAuthoring ? { requireApprovalGrantForTools: [...authoringToolNames] } : {}
59914
+ ...config2.governance?.stewardAudience && !config2.allowUnapprovedAuthoring ? { requireApprovalGrantForTools: [...approvalRequiredToolNames] } : {}
59775
59915
  });
59776
59916
  const policy = databricksGovernancePolicy({
59777
59917
  tools,
@@ -59902,11 +60042,9 @@ function validateAuthoringConfig(config2, genie = normalizeGenieConfig(config2))
59902
60042
  if (config2.jobAuthoring || config2.oneOffCompute)
59903
60043
  validateDatabricksComputePolicy(config2.computePolicy ?? {});
59904
60044
  const audience = config2.governance?.stewardAudience;
59905
- const excluded = config2.governance?.approvalServices ? services.filter((service) => !config2.governance?.approvalServices?.includes(service)) : [];
59906
- if (!config2.allowUnapprovedAuthoring && (!audience || excluded.length > 0)) {
59907
- const ungated = !audience ? services : excluded;
60045
+ if (!config2.allowUnapprovedAuthoring && !audience) {
59908
60046
  throw new Error(
59909
- `databricks: authoring services are not approval-gated: ${ungated.join(", ")}. Configure governance.stewardAudience${excluded.length ? " and include every service in governance.approvalServices" : ""}, or explicitly set allowUnapprovedAuthoring: true.`
60047
+ `databricks: authoring services are not approval-gated: ${services.join(", ")}. Configure governance.stewardAudience, or explicitly set allowUnapprovedAuthoring: true.`
59910
60048
  );
59911
60049
  }
59912
60050
  }
@@ -60000,7 +60138,8 @@ function defineDatabricksAgent(options = {}) {
60000
60138
  ...directTools.length > 0 ? { tools: directTools } : {},
60001
60139
  run: async (context) => {
60002
60140
  const env2 = context.env ?? {};
60003
- const config2 = explicitConfig ?? databricksConfigFromEnv(env2, { analyticsCopilot });
60141
+ const baseConfig = explicitConfig ?? databricksConfigFromEnv(env2, { analyticsCopilot });
60142
+ const config2 = baseConfig.warehouseId && !baseConfig.sqlRead && toolRefs.some((ref) => ref === "sql" || ref === "sql-read" || ref === "sql_read") ? { ...baseConfig, sqlRead: true } : baseConfig;
60004
60143
  const bundle = config2.mcp?.length ? await databricksWithManagedMcp(config2) : databricks(config2);
60005
60144
  try {
60006
60145
  const analyticsCostLimit = analyticsCopilot ? databricksAnalyticsCostLimitFromEnv(env2, config2, bundle) : void 0;
@@ -60101,7 +60240,7 @@ function resolveToolRefs(bundle, refs) {
60101
60240
  toolMap.set(tool7.name, tool7);
60102
60241
  }
60103
60242
  const shortNameMap = {
60104
- sql: "databricks_sql",
60243
+ sql: "sql_read",
60105
60244
  "sql-read": "sql_read",
60106
60245
  tables: "databricks_unity_catalog_tables",
60107
60246
  "table-info": "databricks_table_info",