derive-dashboards 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (410) hide show
  1. package/README.md +8 -5
  2. package/dist/adapter/sql-adapter.mjs +4 -4
  3. package/dist/body.mjs +11 -1
  4. package/dist/cache.d.mts +1 -1
  5. package/dist/cli/add.mjs +130 -0
  6. package/dist/cli/check.mjs +14 -1
  7. package/dist/cli/cli.mjs +14 -12
  8. package/dist/cli/config.mjs +12 -1
  9. package/dist/cli/doctor.mjs +147 -0
  10. package/dist/cli/import/dbt-schema.mjs +2 -2
  11. package/dist/cli/import/index.mjs +2 -2
  12. package/dist/cli/init.mjs +3 -2
  13. package/dist/cli/json.mjs +21 -0
  14. package/dist/cli/layers.mjs +33 -0
  15. package/dist/core/conformance/cases.d.mts +3 -0
  16. package/dist/core/conformance/cases.mjs +20 -4
  17. package/dist/core/conformance/check.mjs +6 -3
  18. package/dist/core/conformance/drill-cases.mjs +120 -0
  19. package/dist/core/document/layout.mjs +1 -1
  20. package/dist/core/document/view-layout.d.mts +14 -0
  21. package/dist/core/document/view-layout.mjs +157 -0
  22. package/dist/core/document/view.d.mts +26 -7
  23. package/dist/core/document/view.mjs +42 -11
  24. package/dist/core/engine/filter.mjs +2 -2
  25. package/dist/core/engine/index.d.mts +14 -2
  26. package/dist/core/engine/index.mjs +12 -1
  27. package/dist/core/engine/leaf.mjs +9 -4
  28. package/dist/core/engine/members.mjs +2 -2
  29. package/dist/core/engine/postgres.mjs +1 -1
  30. package/dist/core/engine/record-layout.d.mts +11 -0
  31. package/dist/core/engine/record-layout.mjs +7 -7
  32. package/dist/core/engine/records.d.mts +3 -2
  33. package/dist/core/engine/records.mjs +2 -1
  34. package/dist/core/engine/segmented.mjs +5 -2
  35. package/dist/core/engine/sql-records.mjs +2 -1
  36. package/dist/core/engine/statement.mjs +37 -3
  37. package/dist/core/engine/types.d.mts +3 -0
  38. package/dist/core/engine/version.d.mts +1 -1
  39. package/dist/core/engine/version.mjs +1 -1
  40. package/dist/core/error/codes.d.mts +33 -1
  41. package/dist/core/error/codes.mjs +30 -2
  42. package/dist/core/index.d.mts +13 -7
  43. package/dist/core/index.mjs +9 -5
  44. package/dist/core/model/catalog/check.mjs +1 -1
  45. package/dist/core/model/hints.d.mts +36 -0
  46. package/dist/core/model/member.d.mts +4 -2
  47. package/dist/core/model/member.mjs +19 -3
  48. package/dist/core/model/types.d.mts +21 -19
  49. package/dist/core/model/validate/drill.mjs +70 -0
  50. package/dist/core/model/validate/index.mjs +3 -0
  51. package/dist/core/model/validate/members.mjs +7 -4
  52. package/dist/core/model/validate/shape.mjs +150 -0
  53. package/dist/core/model/validate/source.mjs +5 -3
  54. package/dist/core/policy/bind-query.d.mts +12 -0
  55. package/dist/core/policy/bind-query.mjs +53 -0
  56. package/dist/core/query/alias.mjs +7 -1
  57. package/dist/core/query/drill/compose.d.mts +22 -0
  58. package/dist/core/query/drill/compose.mjs +85 -0
  59. package/dist/core/query/drill/fields.d.mts +21 -0
  60. package/dist/core/query/drill/fields.mjs +91 -0
  61. package/dist/core/query/drill/internal.d.mts +11 -0
  62. package/dist/core/query/drill/items.d.mts +29 -0
  63. package/dist/core/query/drill/items.mjs +98 -0
  64. package/dist/core/query/drill/line.d.mts +31 -0
  65. package/dist/core/query/drill/line.mjs +183 -0
  66. package/dist/core/query/drill/links.d.mts +33 -0
  67. package/dist/core/query/drill/links.mjs +31 -0
  68. package/dist/core/query/drill/operand.mjs +7 -0
  69. package/dist/core/query/drill/scopes.d.mts +22 -0
  70. package/dist/core/query/drill/scopes.mjs +58 -0
  71. package/dist/core/query/drill/selection.d.mts +37 -0
  72. package/dist/core/query/drill/selection.mjs +144 -0
  73. package/dist/core/query/drill/steps.d.mts +14 -0
  74. package/dist/core/query/drill/steps.mjs +35 -0
  75. package/dist/core/query/drill/synthetic-expr.mjs +85 -0
  76. package/dist/core/query/drill/synthetic.d.mts +22 -0
  77. package/dist/core/query/drill/synthetic.mjs +165 -0
  78. package/dist/core/query/drill/time.d.mts +11 -0
  79. package/dist/core/query/drill/time.mjs +71 -0
  80. package/dist/core/query/drill/types.d.mts +155 -0
  81. package/dist/core/query/drill/types.mjs +4 -0
  82. package/dist/core/query/drill/views.d.mts +62 -0
  83. package/dist/core/query/drill/views.mjs +257 -0
  84. package/dist/core/query/filter.d.mts +6 -0
  85. package/dist/core/query/picks.d.mts +8 -2
  86. package/dist/core/query/picks.mjs +11 -4
  87. package/dist/core/query/segments.d.mts +12 -2
  88. package/dist/core/query/segments.mjs +5 -2
  89. package/dist/core/result/columns.mjs +20 -4
  90. package/dist/core/result/types.d.mts +5 -1
  91. package/dist/core/schema/aliases.d.mts +7 -0
  92. package/dist/core/schema/aliases.mjs +77 -0
  93. package/dist/core/schema/answer-parts.mjs +117 -0
  94. package/dist/core/schema/answers.d.mts +2109 -128
  95. package/dist/core/schema/answers.mjs +11 -113
  96. package/dist/core/schema/commands.d.mts +2 -2
  97. package/dist/core/schema/document.d.mts +1 -1
  98. package/dist/core/schema/drill-answers.mjs +179 -0
  99. package/dist/core/schema/drill.d.mts +260 -0
  100. package/dist/core/schema/drill.mjs +107 -0
  101. package/dist/core/schema/index.d.mts +6 -3
  102. package/dist/core/schema/index.mjs +6 -3
  103. package/dist/core/schema/picks.d.mts +35 -0
  104. package/dist/core/schema/picks.mjs +23 -0
  105. package/dist/core/schema/query.d.mts +82 -1
  106. package/dist/core/schema/query.mjs +9 -5
  107. package/dist/core/schema/tools.mjs +1 -1
  108. package/dist/core/schema/view.d.mts +0 -35
  109. package/dist/core/schema/view.mjs +19 -24
  110. package/dist/core/store/schema.d.mts +5 -0
  111. package/dist/core/store/schema.mjs +10 -0
  112. package/dist/core/types/api.d.mts +21 -1
  113. package/dist/core/types/api.mjs +1 -1
  114. package/dist/core/types/drill-api.d.mts +210 -0
  115. package/dist/core/types/options.d.mts +21 -0
  116. package/dist/core/types/viewer.d.mts +5 -0
  117. package/dist/cursor-seal.d.mts +8 -0
  118. package/dist/cursor-seal.mjs +98 -0
  119. package/dist/dashboard-id.mjs +1 -1
  120. package/dist/dashboards.mjs +4 -0
  121. package/dist/drill-access.mjs +93 -0
  122. package/dist/drill-change.mjs +76 -0
  123. package/dist/drill-crumbs.mjs +113 -0
  124. package/dist/drill-distinct.mjs +130 -0
  125. package/dist/drill-explain.mjs +64 -0
  126. package/dist/drill-identity.mjs +35 -0
  127. package/dist/drill-item-id.mjs +35 -0
  128. package/dist/drill-items.mjs +132 -0
  129. package/dist/drill-open.mjs +169 -0
  130. package/dist/drill-seal.mjs +170 -0
  131. package/dist/drill-secret.mjs +14 -0
  132. package/dist/drill-share.mjs +53 -0
  133. package/dist/drill-summary.mjs +158 -0
  134. package/dist/drill-targets.mjs +53 -0
  135. package/dist/drill-totals.mjs +8 -0
  136. package/dist/drill-views.mjs +175 -0
  137. package/dist/drill.mjs +157 -0
  138. package/dist/editing.mjs +3 -3
  139. package/dist/endpoints/dashboards.mjs +28 -5
  140. package/dist/endpoints/drill.mjs +55 -0
  141. package/dist/endpoints/index.d.mts +245 -4
  142. package/dist/endpoints/index.mjs +5 -1
  143. package/dist/endpoints/meta.mjs +10 -1
  144. package/dist/endpoints/query.mjs +26 -4
  145. package/dist/execute.d.mts +19 -1
  146. package/dist/execute.mjs +34 -27
  147. package/dist/handler.mjs +5 -1
  148. package/dist/index.d.mts +3 -1
  149. package/dist/index.mjs +29 -2
  150. package/dist/llms.txt +22 -0
  151. package/dist/mcp/drill.mjs +61 -0
  152. package/dist/mcp/format.mjs +69 -11
  153. package/dist/mcp/index.d.mts +15 -4
  154. package/dist/mcp/index.mjs +13 -7
  155. package/dist/mcp/query.d.mts +7 -2
  156. package/dist/mcp/query.mjs +40 -6
  157. package/dist/node-postgres.d.mts +8 -4
  158. package/dist/node-postgres.mjs +29 -6
  159. package/dist/owner.mjs +3 -3
  160. package/dist/postgres-connection.d.mts +8 -2
  161. package/dist/postgres-connection.mjs +2 -1
  162. package/dist/postgres-store.mjs +1 -0
  163. package/dist/react/Canvas.mjs +28 -5
  164. package/dist/react/Cell.mjs +2 -1
  165. package/dist/react/Dashboard.d.mts +10 -5
  166. package/dist/react/Dashboard.mjs +62 -22
  167. package/dist/react/DashboardList.mjs +52 -24
  168. package/dist/react/DashboardListRow.mjs +49 -0
  169. package/dist/react/Discard.mjs +12 -4
  170. package/dist/react/Editing.mjs +19 -5
  171. package/dist/react/FilterBar.d.mts +7 -9
  172. package/dist/react/Grid.mjs +19 -24
  173. package/dist/react/Header.mjs +10 -1
  174. package/dist/react/History.mjs +5 -0
  175. package/dist/react/Parts.mjs +6 -2
  176. package/dist/react/ReadFailed.mjs +23 -0
  177. package/dist/react/ReviewPanel.mjs +13 -2
  178. package/dist/react/RowBody.d.mts +19 -0
  179. package/dist/react/RowBody.mjs +55 -0
  180. package/dist/react/Spaces.mjs +15 -1
  181. package/dist/react/Tile.d.mts +3 -1
  182. package/dist/react/Tile.mjs +84 -32
  183. package/dist/react/_virtual/_rolldown/runtime.mjs +12 -0
  184. package/dist/react/arrange-steps.mjs +16 -13
  185. package/dist/react/arrange.mjs +10 -3
  186. package/dist/react/builder/Calcs.mjs +35 -11
  187. package/dist/react/builder/FilterEditor.mjs +196 -0
  188. package/dist/react/builder/Filters.mjs +172 -116
  189. package/dist/react/builder/Formula.mjs +82 -0
  190. package/dist/react/builder/Groups.mjs +1 -0
  191. package/dist/react/builder/Preview.mjs +1 -1
  192. package/dist/react/builder/RangeEditor.mjs +184 -0
  193. package/dist/react/builder/Sections.mjs +41 -6
  194. package/dist/react/builder/Time.mjs +138 -60
  195. package/dist/react/builder/Values.mjs +85 -0
  196. package/dist/react/builder/WidgetBuilder.mjs +50 -49
  197. package/dist/react/builder/base.d.mts +5 -0
  198. package/dist/react/builder/base.mjs +33 -8
  199. package/dist/react/builder/calc.mjs +15 -1
  200. package/dist/react/builder/escape.d.mts +1 -0
  201. package/dist/react/builder/escape.mjs +62 -0
  202. package/dist/react/builder/filter-tree.d.mts +9 -0
  203. package/dist/react/builder/filter-tree.mjs +186 -0
  204. package/dist/react/builder/formula-parse.d.mts +18 -0
  205. package/dist/react/builder/formula-parse.mjs +136 -0
  206. package/dist/react/builder/labels.d.mts +1 -1
  207. package/dist/react/builder/state.d.mts +2 -1
  208. package/dist/react/builder/state.mjs +23 -37
  209. package/dist/react/builder/useBuilder.d.mts +12 -1
  210. package/dist/react/builder/useBuilder.mjs +6 -3
  211. package/dist/react/builder/words.d.mts +59 -0
  212. package/dist/react/builder/words.mjs +73 -0
  213. package/dist/react/client.d.mts +2 -0
  214. package/dist/react/client.mjs +1 -0
  215. package/dist/react/context.d.mts +28 -1
  216. package/dist/react/context.mjs +25 -4
  217. package/dist/react/drill/Bars.mjs +179 -0
  218. package/dist/react/drill/Controls.mjs +173 -0
  219. package/dist/react/drill/DashboardTile.d.mts +18 -0
  220. package/dist/react/drill/DashboardTile.mjs +107 -0
  221. package/dist/react/drill/DrillPanel.d.mts +31 -0
  222. package/dist/react/drill/DrillPanel.mjs +104 -0
  223. package/dist/react/drill/DrillTabs.mjs +52 -0
  224. package/dist/react/drill/Header.mjs +131 -0
  225. package/dist/react/drill/Items.d.mts +22 -0
  226. package/dist/react/drill/Items.mjs +164 -0
  227. package/dist/react/drill/ItemsFoot.mjs +60 -0
  228. package/dist/react/drill/Notes.mjs +58 -0
  229. package/dist/react/drill/PanelBody.mjs +74 -0
  230. package/dist/react/drill/PanelStates.mjs +37 -0
  231. package/dist/react/drill/board.d.mts +18 -0
  232. package/dist/react/drill/board.mjs +91 -0
  233. package/dist/react/drill/counts.mjs +14 -0
  234. package/dist/react/drill/host.mjs +26 -0
  235. package/dist/react/drill/item-parts.d.mts +5 -0
  236. package/dist/react/drill/item-parts.mjs +76 -0
  237. package/dist/react/drill/item-type.d.mts +26 -0
  238. package/dist/react/drill/item-type.mjs +7 -0
  239. package/dist/react/drill/lazy.d.mts +6 -0
  240. package/dist/react/drill/lazy.mjs +17 -0
  241. package/dist/react/drill/pages.mjs +48 -0
  242. package/dist/react/drill/panel-focus.mjs +51 -0
  243. package/dist/react/drill/props.d.mts +25 -0
  244. package/dist/react/drill/props.mjs +72 -0
  245. package/dist/react/drill/state.d.mts +18 -0
  246. package/dist/react/drill/state.mjs +68 -0
  247. package/dist/react/drill/useDrill.d.mts +46 -0
  248. package/dist/react/drill/useDrill.mjs +194 -0
  249. package/dist/react/drill/words.d.mts +101 -0
  250. package/dist/react/drill/words.mjs +79 -0
  251. package/dist/react/english.mjs +29 -6
  252. package/dist/react/filters/bar.d.mts +12 -0
  253. package/dist/react/filters/bar.mjs +135 -0
  254. package/dist/react/filters/calendar.mjs +193 -0
  255. package/dist/react/filters/chip.mjs +174 -0
  256. package/dist/react/filters/context.d.mts +22 -0
  257. package/dist/react/filters/context.mjs +78 -0
  258. package/dist/react/filters/dates.mjs +89 -0
  259. package/dist/react/filters/days.d.mts +36 -0
  260. package/dist/react/filters/days.mjs +87 -0
  261. package/dist/react/filters/field-step.mjs +81 -0
  262. package/dist/react/filters/hooks.d.mts +34 -0
  263. package/dist/react/filters/hooks.mjs +94 -0
  264. package/dist/react/filters/listbox.mjs +131 -0
  265. package/dist/react/filters/logic.d.mts +59 -0
  266. package/dist/react/filters/logic.mjs +109 -0
  267. package/dist/react/filters/menu-search.mjs +211 -0
  268. package/dist/react/filters/menu.mjs +85 -0
  269. package/dist/react/filters/number.mjs +127 -0
  270. package/dist/react/filters/numbers.d.mts +47 -0
  271. package/dist/react/filters/numbers.mjs +151 -0
  272. package/dist/react/filters/operator.mjs +104 -0
  273. package/dist/react/filters/phone.mjs +104 -0
  274. package/dist/react/filters/popover.d.mts +19 -0
  275. package/dist/react/filters/popover.mjs +153 -0
  276. package/dist/react/filters/rules.d.mts +8 -0
  277. package/dist/react/filters/rules.mjs +55 -0
  278. package/dist/react/filters/sheet.mjs +196 -0
  279. package/dist/react/filters/state.mjs +132 -0
  280. package/dist/react/filters/suggest.d.mts +47 -0
  281. package/dist/react/filters/suggest.mjs +97 -0
  282. package/dist/react/filters/values.mjs +166 -0
  283. package/dist/react/filters/words.d.mts +81 -0
  284. package/dist/react/filters/words.mjs +73 -0
  285. package/dist/react/format.d.mts +3 -3
  286. package/dist/react/format.mjs +45 -16
  287. package/dist/react/index.d.mts +34 -5
  288. package/dist/react/index.mjs +15 -4
  289. package/dist/react/messages.d.mts +47 -2
  290. package/dist/react/place.mjs +7 -3
  291. package/dist/react/renderers/index.d.mts +10 -2
  292. package/dist/react/renderers/index.mjs +116 -14
  293. package/dist/react/renderers/list.d.mts +22 -0
  294. package/dist/react/renderers/list.mjs +118 -0
  295. package/dist/react/review-words.mjs +1 -1
  296. package/dist/react/review.mjs +2 -2
  297. package/dist/react/scope.mjs +2 -7
  298. package/dist/react/series.d.mts +54 -0
  299. package/dist/react/series.mjs +191 -0
  300. package/dist/react/theme.css +2734 -1296
  301. package/dist/react/theme.css.d.ts +1 -0
  302. package/dist/react/tile-store.mjs +15 -3
  303. package/dist/react/useBoard.d.mts +7 -0
  304. package/dist/react/useBoard.mjs +4 -1
  305. package/dist/react/useDashboard.d.mts +4 -0
  306. package/dist/react/useDashboard.mjs +8 -1
  307. package/dist/react/useTiles.mjs +50 -15
  308. package/dist/react/view-grid.mjs +16 -0
  309. package/dist/react/view.d.mts +9 -0
  310. package/dist/react/view.mjs +22 -10
  311. package/dist/reading.d.mts +2 -0
  312. package/dist/reading.mjs +9 -1
  313. package/dist/recharts/axes.mjs +28 -0
  314. package/dist/recharts/axis.mjs +84 -0
  315. package/dist/recharts/bar.d.mts +7 -0
  316. package/dist/recharts/bar.mjs +140 -0
  317. package/dist/recharts/charts.d.mts +4 -6
  318. package/dist/recharts/charts.mjs +2 -114
  319. package/dist/recharts/index.d.mts +5 -3
  320. package/dist/recharts/index.mjs +6 -3
  321. package/dist/recharts/line.d.mts +2 -0
  322. package/dist/recharts/line.mjs +111 -0
  323. package/dist/recharts/marks.mjs +112 -0
  324. package/dist/recharts/parts.mjs +88 -0
  325. package/dist/recharts/series.d.mts +2 -22
  326. package/dist/recharts/series.mjs +1 -64
  327. package/dist/recharts/table.mjs +71 -0
  328. package/dist/records-read.mjs +44 -0
  329. package/dist/registry/filter-bar.json +26 -0
  330. package/dist/skills/derive-dashboards/SKILL.md +69 -0
  331. package/dist/store/apply.mjs +5 -5
  332. package/dist/store/commands.mjs +4 -4
  333. package/dist/store/discard.mjs +7 -7
  334. package/dist/store/drafts.mjs +5 -5
  335. package/dist/store/editor.mjs +1 -1
  336. package/dist/store/publish.mjs +12 -12
  337. package/dist/store/reader.mjs +41 -22
  338. package/dist/store/review.mjs +16 -16
  339. package/dist/store/rows.mjs +20 -20
  340. package/dist/store/undo.mjs +3 -3
  341. package/dist/store/unpublished.mjs +4 -4
  342. package/dist/store/versions.mjs +14 -14
  343. package/dist/testing/adapter-suite.mjs +4 -4
  344. package/dist/tiles.mjs +37 -3
  345. package/package.json +12 -10
  346. package/dist/react/FilterBar.mjs +0 -119
  347. package/dist/react/Records.d.mts +0 -12
  348. package/dist/react/Records.mjs +0 -97
  349. package/src/adapter/sql-adapter.ts +0 -95
  350. package/src/adapter/statement.ts +0 -185
  351. package/src/authorize.ts +0 -73
  352. package/src/body.ts +0 -169
  353. package/src/bounded.ts +0 -43
  354. package/src/cache.ts +0 -192
  355. package/src/checks.ts +0 -170
  356. package/src/custom-source.ts +0 -59
  357. package/src/dashboard-id.ts +0 -36
  358. package/src/dashboards.ts +0 -132
  359. package/src/editing.ts +0 -148
  360. package/src/endpoints/access.ts +0 -41
  361. package/src/endpoints/dashboards.ts +0 -92
  362. package/src/endpoints/drafts.ts +0 -138
  363. package/src/endpoints/index.ts +0 -21
  364. package/src/endpoints/meta.ts +0 -137
  365. package/src/endpoints/query.ts +0 -86
  366. package/src/endpoints/read.ts +0 -92
  367. package/src/endpoints/review.ts +0 -89
  368. package/src/endpoints/values.ts +0 -62
  369. package/src/endpoints/versions.ts +0 -104
  370. package/src/execute.ts +0 -207
  371. package/src/handler.ts +0 -232
  372. package/src/http.ts +0 -66
  373. package/src/index.ts +0 -163
  374. package/src/introspect.ts +0 -205
  375. package/src/mcp/api.ts +0 -59
  376. package/src/mcp/format.ts +0 -67
  377. package/src/mcp/index.ts +0 -151
  378. package/src/mcp/query.ts +0 -153
  379. package/src/mcp/resolve.ts +0 -114
  380. package/src/node-postgres.ts +0 -81
  381. package/src/owner.ts +0 -36
  382. package/src/postgres-connection.ts +0 -143
  383. package/src/postgres-store.ts +0 -82
  384. package/src/principal.ts +0 -39
  385. package/src/reading.ts +0 -56
  386. package/src/request-rows.ts +0 -23
  387. package/src/request.ts +0 -22
  388. package/src/storage.ts +0 -63
  389. package/src/store/apply.ts +0 -129
  390. package/src/store/commands.ts +0 -75
  391. package/src/store/conflicts.ts +0 -98
  392. package/src/store/create-tables.ts +0 -104
  393. package/src/store/discard.ts +0 -42
  394. package/src/store/drafts.ts +0 -133
  395. package/src/store/editor.ts +0 -28
  396. package/src/store/log.ts +0 -28
  397. package/src/store/publish.ts +0 -199
  398. package/src/store/publisher.ts +0 -21
  399. package/src/store/reader.ts +0 -139
  400. package/src/store/review.ts +0 -239
  401. package/src/store/rows.ts +0 -250
  402. package/src/store/tenant.ts +0 -28
  403. package/src/store/undo.ts +0 -56
  404. package/src/store/unpublished.ts +0 -33
  405. package/src/store/versions.ts +0 -106
  406. package/src/store/writer.ts +0 -19
  407. package/src/testing/adapter-suite.ts +0 -239
  408. package/src/testing/index.ts +0 -2
  409. package/src/tile-doc.ts +0 -46
  410. package/src/tiles.ts +0 -120
@@ -1,10 +1,28 @@
1
+ import "./reading.mjs";
1
2
  import { Cache } from "./cache.mjs";
2
- import { DashOptions, Principal, Result, WireQuery } from "derive-dashboards/core";
3
+ import { cursorKey, macKey } from "./cursor-seal.mjs";
4
+ import { Cuts, DashOptions, Model, Principal, Result, WireQuery } from "derive-dashboards/core";
5
+ import "derive-dashboards/core/engine";
3
6
  /** The runtime's state for one handler: its options and its result cache. */
4
7
  export type Runtime = {
5
8
  options: DashOptions;
6
9
  cache: Cache | undefined;
10
+ /** Seals records cursors and a drill's parts (`cursor-seal`). */
11
+ cursorKey: ReturnType<typeof cursorKey>;
12
+ /** Signs what must be stable and unreadable: a hidden item id, a viewer's access fingerprint. */
13
+ macKey: ReturnType<typeof macKey>;
14
+ /** The model a drill reads (`drillModel`), built once. */
15
+ drillModel: Model;
16
+ /** Whether the missing-`secret` warning was given, so it is given once. */
17
+ warned: {
18
+ secret: boolean;
19
+ };
7
20
  };
21
+ /**
22
+ * An answer as the server keeps it: with what each dimension's own limit kept (`cuts`), which a
23
+ * drill reads its Other from. `cuts` never leaves the server (`wire`).
24
+ */
8
25
  export type Answered = Result & {
9
26
  unreadable?: true;
27
+ cuts?: Cuts;
10
28
  };
package/dist/execute.mjs CHANGED
@@ -1,15 +1,24 @@
1
1
  import { checkSensitive } from "./authorize.mjs";
2
- import { manifest, sha256 } from "./cache.mjs";
2
+ import { manifest } from "./cache.mjs";
3
+ import { macHex } from "./cursor-seal.mjs";
3
4
  import { adapterFor } from "./dashboards.mjs";
4
5
  import { withReader } from "./reading.mjs";
5
6
  import { logQuery } from "./store/log.mjs";
6
- import { canonical, isBound, markedRows, recordColumns, resultColumns } from "derive-dashboards/core";
7
- import { EngineError, readQuery, readRecords } from "derive-dashboards/core/engine";
7
+ import { bindQueryViewer, bindViewer, canonical, isBound, markedRows, resultColumns } from "derive-dashboards/core";
8
+ import { EngineError, readQuery } from "derive-dashboards/core/engine";
8
9
  // src/execute.ts
9
- const DEFAULT_QUANTUM = 60;
10
- function boundEntries(entries, what) {
11
- return entries.map((entry) => {
12
- if (!isBound(entry.filter)) throw new EngineError("FILTER_UNBOUND", `A ${what} that uses a viewer value or a variable is not supported yet.`);
10
+ /** An answer as a client reads it: without what only the server reads. */
11
+ function wire(answer) {
12
+ const { cuts: _cuts, ...rest } = answer;
13
+ return rest;
14
+ }
15
+ function boundEntries(entries, what, principal) {
16
+ return entries.map((given) => {
17
+ const entry = {
18
+ ...given,
19
+ filter: bindViewer(given.filter, principal)
20
+ };
21
+ if (!isBound(entry.filter)) throw new EngineError("FILTER_UNBOUND", `A ${what} that uses a variable is not supported yet.`);
13
22
  return {
14
23
  ...entry,
15
24
  filter: entry.filter
@@ -25,28 +34,33 @@ function receiptFilter(bound) {
25
34
  ...bound.required.map((e) => e.filter)
26
35
  ] };
27
36
  }
28
- /** The query bound for the engine: no sensitive member, and no viewer value or variable left in the lock or required filters. */
37
+ /**
38
+ * The query bound for the engine: no sensitive member, and the signed-in viewer's values in
39
+ * place of every viewer reference (query, measure, lock and required filters). The bound query
40
+ * is what keys the cache, the receipt and a cursor, so two viewers never share an answer.
41
+ */
29
42
  function bind(x) {
30
43
  const { options } = x.rt;
31
44
  checkSensitive(options, x.principal, x.query);
32
- const lock = boundEntries(x.lock, "lock");
45
+ const query = bindQueryViewer(x.query, x.principal);
46
+ const lock = boundEntries(x.lock, "lock", x.principal);
33
47
  const required = boundEntries(options.compose?.required?.({
34
48
  principal: x.principal,
35
49
  calendar: x.calendar,
36
50
  ...x.dashboardId === void 0 ? {} : { dashboardId: x.dashboardId }
37
- }) ?? [], "required filter");
51
+ }) ?? [], "required filter", x.principal);
38
52
  const maxRows = options.query?.maxRows ?? 5e3;
39
53
  return {
40
54
  input: {
41
55
  model: options.model,
42
- query: x.query,
56
+ query,
43
57
  principal: x.principal,
44
58
  locks: lock,
45
59
  required,
46
60
  now: x.now,
47
61
  calendar: x.calendar,
48
62
  maxRows,
49
- quantumSeconds: options.cache?.quantum ?? DEFAULT_QUANTUM
63
+ quantumSeconds: options.cache?.quantum ?? 60
50
64
  },
51
65
  lock,
52
66
  required,
@@ -65,6 +79,7 @@ async function compute(x, bound) {
65
79
  const started = performance.now();
66
80
  const computedAt = (/* @__PURE__ */ new Date()).toISOString();
67
81
  const { value, consistency, custom } = await reading(x, bound.maxRows, (reader) => readQuery(bound.input, reader));
82
+ const cuts = value.cuts === void 0 ? {} : { cuts: value.cuts };
68
83
  const { truncated, unreadable, compare, ...levels } = value.answer;
69
84
  const warnings = truncated === true ? [{
70
85
  code: "ROW_CAP",
@@ -80,9 +95,10 @@ async function compute(x, bound) {
80
95
  } },
81
96
  status: truncated === true ? "truncated" : "complete",
82
97
  ...unreadable === true ? { unreadable } : {},
98
+ ...cuts,
83
99
  receipt: {
84
- queryHash: await sha256(canonical({
85
- query: x.query,
100
+ queryHash: await macHex(await x.rt.macKey, canonical({
101
+ query: bound.input.query,
86
102
  lock: bound.lock,
87
103
  required: bound.required
88
104
  })),
@@ -126,27 +142,18 @@ function answered(x) {
126
142
  const bound = bind(x);
127
143
  const { cache, options } = x.rt;
128
144
  if (cache === void 0) return compute(x, bound);
129
- const quantum = (options.cache?.quantum ?? DEFAULT_QUANTUM) * 1e3;
145
+ const quantum = (options.cache?.quantum ?? 60) * 1e3;
130
146
  const on = [...bound.lock, ...bound.required].map((e) => e.on);
131
147
  const key = {
132
148
  principal: x.principal,
133
- query: x.query,
149
+ query: bound.input.query,
134
150
  lock: bound.lock,
135
151
  required: bound.required,
136
152
  calendar: x.calendar,
137
153
  maxRows: bound.maxRows,
138
154
  now: x.now.getTime() - x.now.getTime() % quantum,
139
- sources: manifest(options.model, x.query, on)
155
+ sources: manifest(options.model, bound.input.query, on)
140
156
  };
141
157
  return cache.answer(key, () => compute(x, bound));
142
158
  }
143
- /** A page of the rows behind a query, under the same access, filters and range as its answer. */
144
- async function executeRecords(x, page) {
145
- const bound = bind(x);
146
- const { value } = await reading(x, page.limit, (reader) => readRecords(bound.input, page, reader));
147
- return {
148
- ...value,
149
- columns: recordColumns(x.rt.options.model, x.query.source, value.columns)
150
- };
151
- }
152
- export { execute, executeRecords };
159
+ export { bind, execute, reading, wire };
package/dist/handler.mjs CHANGED
@@ -30,7 +30,7 @@ function checkOrigin(options, endpoint, request) {
30
30
  const origin = request.headers.get("origin");
31
31
  if (origin === null || origin === new URL(request.url).origin) return;
32
32
  if ((options.trustedOrigins ?? []).includes(origin)) return;
33
- throw new HttpError(403, "FORBIDDEN", `Requests from ${origin} are not trusted.`);
33
+ throw new HttpError(403, "FORBIDDEN", `Requests from ${origin} are not trusted. If that is your app, add it to deriveDashboards({ trustedOrigins: ["${origin}"] }).`);
34
34
  }
35
35
  async function readBody(request) {
36
36
  if (request.method === "GET") return Object.fromEntries(new URL(request.url).searchParams);
@@ -123,6 +123,10 @@ function errorOf(rt, error, request) {
123
123
  code: error.code,
124
124
  message: error.message
125
125
  } });
126
+ if (request.signal.aborted) return json(499, { error: {
127
+ code: "CANCELLED",
128
+ message: BASE_ERROR_CODES.CANCELLED.message
129
+ } });
126
130
  (rt.options.onError ?? console.error)(error, request);
127
131
  return json(500, { error: {
128
132
  code: "INTERNAL",
package/dist/index.d.mts CHANGED
@@ -6,7 +6,7 @@ import { PostgresConnectionOptions, RunSql, postgresConnection } from "./postgre
6
6
  import { PostgresStoreOptions, RunRows, postgresStore } from "./postgres-store.mjs";
7
7
  import { memoryStorage } from "./storage.mjs";
8
8
  import { SchemaDiff, SchemaMismatchError, createTables, schemaDiff } from "./store/create-tables.mjs";
9
- import { Awaitable, Dash, DashOptions, UniqueIds, ViewerFor } from "derive-dashboards/core";
9
+ import { Awaitable, Dash, DashOptions, ItemLinksFor, UniqueIds, ViewerFor } from "derive-dashboards/core";
10
10
  /** What `deriveDashboards` returns: `api` calls every endpoint; `$Infer.Endpoints` types a client. */
11
11
  export type DashServer<O extends DashOptions> = Dash<O, ReturnType<typeof coreEndpoints>>;
12
12
  /**
@@ -20,5 +20,7 @@ export declare function deriveDashboards<const O extends DashOptions>(options: O
20
20
  plugins?: UniqueIds<O["plugins"]>;
21
21
  /** Typed from the model: with tenant keys, the viewer must carry a value for each. */
22
22
  viewer: (request: Request) => Awaitable<ViewerFor<O["model"]> | null>;
23
+ /** Typed from the model: a template per source, and no source the model lacks. */
24
+ itemLinks?: ItemLinksFor<O["model"], O["itemLinks"]>;
23
25
  }): DashServer<O>;
24
26
  export { type CustomSourceOptions, HttpError, type PgPool, type PgQueryable, type PostgresConnectionOptions, type PostgresStoreOptions, type RunRows, type RunSql, type SchemaDiff, SchemaMismatchError, createTables, customSource, memoryStorage, nodePostgres, postgresConnection, postgresStore, schemaDiff };
package/dist/index.mjs CHANGED
@@ -1,4 +1,6 @@
1
1
  import { createCache } from "./cache.mjs";
2
+ import { cursorKey, macKey } from "./cursor-seal.mjs";
3
+ import { warnSecretAtStart } from "./drill-secret.mjs";
2
4
  import { coreEndpoints } from "./endpoints/index.mjs";
3
5
  import { createHandler, serverCall } from "./handler.mjs";
4
6
  import { HttpError } from "./http.mjs";
@@ -9,7 +11,8 @@ import { postgresConnection } from "./postgres-connection.mjs";
9
11
  import { postgresStore } from "./postgres-store.mjs";
10
12
  import { memoryStorage } from "./storage.mjs";
11
13
  import { SchemaMismatchError, createTables, schemaDiff } from "./store/create-tables.mjs";
12
- import { BASE_ERROR_CODES, defineModel } from "derive-dashboards/core";
14
+ import { BASE_ERROR_CODES, defineModel, linkTemplateIssues } from "derive-dashboards/core";
15
+ import { drillModel } from "derive-dashboards/core/engine";
13
16
  import { loadDocument } from "derive-dashboards/core/schema";
14
17
  // src/index.ts
15
18
  /**
@@ -54,6 +57,24 @@ function checkPluginIds(options, taken) {
54
57
  function isStream(output) {
55
58
  return typeof output === "object" && output !== null && typeof Reflect.get(output, Symbol.asyncIterator) === "function";
56
59
  }
60
+ /**
61
+ * Each `itemLinks` template names a source of the model and only parts its items have: `{id}`,
62
+ * or `{key.<column>}` for a column of the source's key. A misspelled source or column would
63
+ * otherwise only show as links that never appear: refuse to start.
64
+ */
65
+ /**
66
+ * Each `itemLinks` template names a source of the model and is one core's link filler reads:
67
+ * it starts with "/" or a scheme, and names only {id} and {key.<column>} of that source's key. A
68
+ * misspelling would otherwise only show as links that never appear: refuse to start.
69
+ */
70
+ function checkItemLinks(options) {
71
+ for (const [source, template] of Object.entries(options.itemLinks ?? {})) {
72
+ const def = options.model.sources[source];
73
+ if (def === void 0) throw new Error(`itemLinks names "${source}", which is not a source of the model.`);
74
+ const [problem] = linkTemplateIssues(template, def.key);
75
+ if (problem !== void 0) throw new Error(`itemLinks.${source} ${problem}`);
76
+ }
77
+ }
57
78
  /** One endpoint by path; a streamed answer is read to its end and comes back as its lines. */
58
79
  async function callPath(byPath, path, input) {
59
80
  const call = byPath.get(path);
@@ -74,14 +95,20 @@ async function callPath(byPath, path, input) {
74
95
  function deriveDashboards(options) {
75
96
  defineModel(options.model);
76
97
  checkConnections(options);
98
+ checkItemLinks(options);
77
99
  const checked = {
78
100
  ...options,
79
101
  dashboards: checkedDashboards(options)
80
102
  };
81
103
  const rt = {
82
104
  options: checked,
83
- cache: createCache(checked)
105
+ cache: createCache(checked),
106
+ cursorKey: cursorKey(options.secret),
107
+ macKey: macKey(options.secret),
108
+ drillModel: drillModel(checked.model),
109
+ warned: { secret: false }
84
110
  };
111
+ warnSecretAtStart(rt);
85
112
  const library = coreEndpoints(rt);
86
113
  checkPluginIds(options, ["cache", ...Object.keys(library)]);
87
114
  const plugins = options.plugins ?? [];
package/dist/llms.txt ADDED
@@ -0,0 +1,22 @@
1
+ # Derive Dashboards
2
+
3
+ > Embedded, editable dashboards for TypeScript apps over your own Postgres: a semantic model of your tables, a server handler with tenancy and row policies, React dashboards with drill-in, editing and a built-in filter bar, a CLI, and MCP tools for agents. One npm package: `derive-dashboards`.
4
+
5
+ Install skill for coding agents: `npx skills add derive-to/dashboards --skill derive-dashboards` (skills/derive-dashboards/SKILL.md).
6
+
7
+ ## Docs
8
+
9
+ - [Quick start](https://github.com/derive-to/dashboards/blob/main/docs/quick-start.md): install, describe your data, mount the handler, show a dashboard, drill in.
10
+ - [Concepts](https://github.com/derive-to/dashboards/blob/main/docs/concepts.md): the model, measures, joins, tenancy and row policies, access, documents, drill-in.
11
+ - [Filters](https://github.com/derive-to/dashboards/blob/main/docs/filters.md): the built-in filter bar, theming, options, words, the shadcn copy, custom bars.
12
+ - [React](https://github.com/derive-to/dashboards/blob/main/docs/react.md): provider, Dashboard, editing UI, URL state, drill parts, widget builder.
13
+ - [CLI](https://github.com/derive-to/dashboards/blob/main/docs/cli.md): init, check, generate, create-tables, add, importing from dbt or Cube.
14
+ - [Endpoints](https://github.com/derive-to/dashboards/blob/main/docs/endpoints.md): every handler endpoint, its body and answer.
15
+ - [MCP](https://github.com/derive-to/dashboards/blob/main/docs/mcp.md): tools that let agents query and edit dashboards as themselves.
16
+ - [Store](https://github.com/derive-to/dashboards/blob/main/docs/store.md): where drafts, versions and history live.
17
+ - [A custom data layer](https://github.com/derive-to/dashboards/blob/main/docs/custom-data-layer.md): other databases, with the conformance kit.
18
+ - [Errors](https://github.com/derive-to/dashboards/blob/main/docs/errors.md): every error code and what to do.
19
+
20
+ ## Example
21
+
22
+ - [Acme](https://github.com/derive-to/dashboards/tree/main/examples/acme): a complete host (Hono, PGlite) with tenants, row policies, editing, filters and agents.
@@ -0,0 +1,61 @@
1
+ import { ok, table } from "./format.mjs";
2
+ import { itemLink } from "derive-dashboards/core";
3
+ // src/mcp/drill.ts
4
+ /** A value as text: a plain value as itself, an exact number by its digits. */
5
+ const text = (cell) => typeof cell === "object" && cell !== null ? cell.v : String(cell);
6
+ /** One breadcrumb in words: Other only for an Other step, "not set" for a group with no value. */
7
+ function crumbText(p) {
8
+ if (p.parts !== void 0) return p.parts.map((q) => crumbText(q)).join(", ");
9
+ if (p.kind === "other") return `${p.label}: Other`;
10
+ if (p.kind === "search") return `${p.label} contains "${text(p.value ?? "")}"`;
11
+ if (p.kind === "total") return "Total";
12
+ return `${p.label}: ${p.value === null ? "not set" : text(p.value)}`;
13
+ }
14
+ /** What the answer is about, in one line: how many rows, behind which path. */
15
+ function heading(answer, shown) {
16
+ const where = answer.path.map((p) => crumbText(p)).join(" › ");
17
+ return `${text(answer.summary?.itemCount ?? shown)} rows behind ${where === "" ? "the tile" : where}`;
18
+ }
19
+ /** The drill's answer, asked as the agent's person: of a dashboard's tile, or of a free query. */
20
+ function ask(c, drill, page) {
21
+ const body = {
22
+ state: {
23
+ ...drill,
24
+ view: { by: "items" }
25
+ },
26
+ page: {
27
+ limit: page.limit ?? 50,
28
+ ...page.after === void 0 ? {} : { after: page.after }
29
+ }
30
+ };
31
+ return drill.origin.kind === "dashboard" ? c.dashboards.api.drill({
32
+ body,
33
+ viewer: c.viewer
34
+ }) : c.dashboards.api.queryDrill({
35
+ body,
36
+ viewer: c.viewer
37
+ });
38
+ }
39
+ /**
40
+ * The rows behind one mark, for an agent: the same drill a person opens, with the same checks,
41
+ * as a table of its items, each with its link in the host's app when `itemLinks` gives one.
42
+ */
43
+ async function drillRows(c, drill, page) {
44
+ const answer = await ask(c, drill, page);
45
+ const view = answer.view.by === "items" ? answer.view : void 0;
46
+ const items = (view?.items ?? []).map((item) => ({
47
+ ...item,
48
+ link: item.link ?? itemLink(c.itemLinks, item)
49
+ }));
50
+ const columns = view?.columns ?? [];
51
+ const labels = [...columns.map((col) => col.label), "Link"];
52
+ const rows = items.map((item) => [...columns.map((col) => item.values[col.member] ?? null), item.link ?? ""]);
53
+ return ok(`${heading(answer, items.length)}\n\n${table(labels, rows)}`, {
54
+ ...answer,
55
+ view: {
56
+ ...view,
57
+ items
58
+ }
59
+ });
60
+ }
61
+ export { drillRows };
@@ -1,34 +1,69 @@
1
1
  import { HttpError } from "../http.mjs";
2
+ import { EngineError } from "derive-dashboards/core/engine";
2
3
  // src/mcp/format.ts
3
4
  /** The most rows a table shows the model; the rest are counted, and all are in the structured answer. */
4
5
  const SHOWN = 30;
5
- /** A cell as text: exact numbers as their digits, no value as a blank. */
6
- function cellText(cell) {
7
- if (cell === null || cell === void 0) return "";
6
+ /** At most one decimal, without a trailing ".0": 64.3, 12, 0.5. */
7
+ const short = (value) => String(Math.round(value * 10) / 10);
8
+ const DURATION = [
9
+ ["d", 86400],
10
+ ["h", 3600],
11
+ ["min", 60],
12
+ ["s", 1]
13
+ ];
14
+ /** A percent or a duration as a person reads it (64.3%, 3.2 d); anything else as its digits. */
15
+ function formatted(value, column) {
16
+ if (column?.format === "percent") return `${short(column.fraction === true ? value * 100 : value)}%`;
17
+ if (column?.format === "duration") {
18
+ const [unit, size] = DURATION.find(([, n]) => Math.abs(value) >= n) ?? DURATION[3];
19
+ return `${short(value / size)} ${unit}`;
20
+ }
21
+ }
22
+ /** A cell's number, for the formats that read it; undefined for text and blanks. */
23
+ const numberOf = (cell) => {
24
+ const value = typeof cell === "number" ? cell : typeof cell === "object" && cell !== null && "v" in cell ? Number(cell.v) : void 0;
25
+ return value === void 0 || Number.isNaN(value) ? void 0 : value;
26
+ };
27
+ /** A cell as raw text: exact numbers as their digits, no value as "none". */
28
+ function rawText(cell) {
29
+ if (cell === null || cell === void 0) return "none";
8
30
  if (typeof cell === "object" && "v" in cell) return String(cell.v);
9
31
  if (typeof cell === "string") return cell.replaceAll("|", "\\|");
10
32
  if (typeof cell === "number" || typeof cell === "boolean") return String(cell);
11
33
  return JSON.stringify(cell);
12
34
  }
35
+ /** A cell as text: percents and durations as people read them, everything else raw. */
36
+ function cellText(cell, column) {
37
+ const value = numberOf(cell);
38
+ return (value === void 0 ? void 0 : formatted(value, column)) ?? rawText(cell);
39
+ }
13
40
  /** Rows under their column labels, as a Markdown table, cut at SHOWN rows with a count. */
14
- function table(labels, rows) {
41
+ function table(labels, rows, columns) {
15
42
  if (rows.length === 0) return "No rows.";
16
43
  const lines = [
17
44
  `| ${labels.join(" | ")} |`,
18
45
  `| ${labels.map(() => "---").join(" | ")} |`,
19
- ...rows.slice(0, SHOWN).map((row) => `| ${row.map((c) => cellText(c)).join(" | ")} |`)
46
+ ...rows.slice(0, SHOWN).map((row) => `| ${row.map((c, i) => cellText(c, columns?.[i])).join(" | ")} |`)
20
47
  ];
21
48
  if (rows.length > SHOWN) lines.push(`\n${rows.length - SHOWN} more rows are in the structured answer.`);
22
49
  return lines.join("\n");
23
50
  }
51
+ /** An instant as a person reads it: the day alone at midnight, else the day and minute, UTC. */
52
+ function instantText(value) {
53
+ const at = new Date(value);
54
+ if (Number.isNaN(at.getTime())) return value;
55
+ const [day, time = ""] = at.toISOString().split("T");
56
+ const minute = time.slice(0, 5);
57
+ return minute === "00:00" ? day ?? value : `${day} ${minute} UTC`;
58
+ }
24
59
  /** A query's answer: its table, the comparison period's beside it, and whether it was cut. */
25
60
  function answerText(answer) {
26
61
  if (answer.unreadable === true) return "This viewer may not read the rows this question asks about.";
27
62
  const labels = answer.columns.map((c) => c.label);
28
- const parts = [table(labels, answer.rows)];
63
+ const parts = [table(labels, answer.rows, answer.columns)];
29
64
  if (answer.compare !== void 0) {
30
65
  const { from, to } = answer.compare.range;
31
- parts.push(`Compared with ${from} to ${to}:`, table(labels, answer.compare.rows));
66
+ parts.push(`Compared with ${instantText(from)} to ${instantText(to)}:`, table(labels, answer.compare.rows, answer.columns));
32
67
  }
33
68
  if (answer.status === "truncated") parts.push("The answer was cut at its row limit.");
34
69
  return parts.join("\n\n");
@@ -40,9 +75,32 @@ const ok = (text, data) => ({
40
75
  }],
41
76
  ...data === void 0 ? {} : { structuredContent: { result: data } }
42
77
  });
43
- /** A refusal the model can act on: the code, the reason, each problem with the input, and a hint. */
44
- function refused(error, hint) {
45
- if (!(error instanceof HttpError)) throw error;
78
+ /**
79
+ * A refusal the model can act on: the code, the reason, each problem with the input, and a hint.
80
+ * An engine refusal keeps its code; anything else goes to `onError` and the model reads only
81
+ * INTERNAL, never a database's own message, which can name columns or echo stored values.
82
+ */
83
+ function refused(error, hint, onError = (e) => {
84
+ console.error(e);
85
+ }) {
86
+ if (error instanceof EngineError) return {
87
+ content: [{
88
+ type: "text",
89
+ text: `${error.code}: ${error.message}`
90
+ }],
91
+ isError: true
92
+ };
93
+ if (!(error instanceof HttpError)) {
94
+ onError(error);
95
+ return {
96
+ content: [{
97
+ type: "text",
98
+ text: "INTERNAL: Something went wrong answering this. It was logged."
99
+ }],
100
+ isError: true
101
+ };
102
+ }
103
+ if (error.cause !== void 0) onError(error.cause);
46
104
  const issues = (error.issues ?? []).map((i) => `- ${i.path.join(".") || "input"}: ${i.message}`);
47
105
  return {
48
106
  content: [{
@@ -56,4 +114,4 @@ function refused(error, hint) {
56
114
  isError: true
57
115
  };
58
116
  }
59
- export { answerText, ok, refused, table };
117
+ export { answerText, instantText, ok, refused, table };
@@ -1,6 +1,6 @@
1
1
  import { ApiServer } from "./api.mjs";
2
2
  import { ToolResult } from "./format.mjs";
3
- import { Viewer } from "derive-dashboards/core";
3
+ import { ItemLinks, Viewer, ViewerFor } from "derive-dashboards/core";
4
4
  import { z } from "zod";
5
5
  /** MCP's tool annotations: what a client may assume about a tool before it runs it. */
6
6
  export type ToolAnnotations = {
@@ -29,6 +29,10 @@ export type DashboardToolsOptions = {
29
29
  api?: boolean;
30
30
  /** The link to a dashboard in your app, given with every dashboard the agent opens. */
31
31
  linkFor?: (dashboardId: string) => string;
32
+ /** Where each source's items open in your app; by default the server's own `itemLinks`. */
33
+ itemLinks?: ItemLinks;
34
+ /** Where an unexpected error goes (its message never reaches the model); the console without one. */
35
+ onError?: (error: unknown) => void;
32
36
  };
33
37
  /** The two tools, as plain data: `name`, `description`, `inputSchema` (Zod), `annotations`, `run(input, viewer)`. */
34
38
  export declare function dashboardTools(dashboards: ApiServer, options?: DashboardToolsOptions): DashboardTool[];
@@ -51,11 +55,18 @@ export type McpServerLike = {
51
55
  annotations?: ToolAnnotations;
52
56
  }, callback: (input: Record<string, unknown>, extra: McpCallExtra) => Promise<ToolResult>): unknown;
53
57
  };
58
+ /** The model a dashboards server was made with, when its type says; else any. */
59
+ type ModelOf<S> = S extends {
60
+ options: {
61
+ model: infer M;
62
+ };
63
+ } ? M : unknown;
54
64
  /**
55
65
  * Adds the two tools to an MCP server you already run. `viewer` reads the person from the
56
- * call (`extra.authInfo` from your MCP auth); none refuses the call.
66
+ * call (`extra.authInfo` from your MCP auth); none refuses the call. It is typed from the
67
+ * server's model, as `deriveDashboards`'s own `viewer` is: with tenant keys, each is required.
57
68
  */
58
- export declare function addDashboardTools(server: McpServerLike, dashboards: ApiServer, options: DashboardToolsOptions & {
59
- viewer: (extra: McpCallExtra) => Viewer | null | Promise<Viewer | null>;
69
+ export declare function addDashboardTools<S extends ApiServer>(server: McpServerLike, dashboards: S, options: DashboardToolsOptions & {
70
+ viewer: (extra: McpCallExtra) => ViewerFor<ModelOf<S>> | null | Promise<ViewerFor<ModelOf<S>> | null>;
60
71
  }): void;
61
72
  export type { ToolResult };
@@ -4,7 +4,11 @@ import { refused } from "./format.mjs";
4
4
  import { queryInput, runQuery } from "./query.mjs";
5
5
  import { z } from "zod";
6
6
  // src/mcp/index.ts
7
- /** An agent acts for its person: marked so, its edits are its own draft and history says an agent made them. */
7
+ /**
8
+ * An agent acts for its person: it keeps the person's id, so tenancy, row rules and `can` judge
9
+ * it exactly as the person, and is marked an agent, so its edits are a draft of its own and
10
+ * history says an agent made them. A viewer already marked as an agent is kept as it is.
11
+ */
8
12
  const asAgent = (viewer) => viewer.agent === void 0 ? {
9
13
  ...viewer,
10
14
  agent: { for: viewer.id }
@@ -15,14 +19,14 @@ const HINT = "Fix the input and call again; dashboards_query with op describe li
15
19
  * then run as the agent acting for the viewer. A refusal becomes a result the model can act on;
16
20
  * anything else is a bug and throws.
17
21
  */
18
- function guarded(schema, run) {
22
+ function guarded(schema, run, onError) {
19
23
  return async (raw, viewer) => {
20
24
  const parsed = schema.safeParse(raw);
21
25
  if (!parsed.success) return refused(new HttpError(400, "BODY_INVALID", z.prettifyError(parsed.error)), HINT);
22
26
  try {
23
27
  return await run(parsed.data, asAgent(viewer));
24
28
  } catch (error) {
25
- return refused(error, HINT);
29
+ return refused(error, HINT, onError);
26
30
  }
27
31
  };
28
32
  }
@@ -41,8 +45,9 @@ function dashboardTools(dashboards, options = {}) {
41
45
  run: guarded(queryInput, (input, viewer) => runQuery({
42
46
  dashboards,
43
47
  viewer,
44
- linkFor: options.linkFor
45
- }, input))
48
+ linkFor: options.linkFor,
49
+ itemLinks: options.itemLinks ?? dashboards.options?.itemLinks
50
+ }, input), options.onError)
46
51
  };
47
52
  if (options.api === false) return [query];
48
53
  const api = apiInput(dashboards.routes);
@@ -56,12 +61,13 @@ function dashboardTools(dashboards, options = {}) {
56
61
  destructiveHint: true,
57
62
  openWorldHint: false
58
63
  },
59
- run: guarded(api, (input, viewer) => runApi(dashboards, viewer, input))
64
+ run: guarded(api, (input, viewer) => runApi(dashboards, viewer, input), options.onError)
60
65
  }];
61
66
  }
62
67
  /**
63
68
  * Adds the two tools to an MCP server you already run. `viewer` reads the person from the
64
- * call (`extra.authInfo` from your MCP auth); none refuses the call.
69
+ * call (`extra.authInfo` from your MCP auth); none refuses the call. It is typed from the
70
+ * server's model, as `deriveDashboards`'s own `viewer` is: with tenant keys, each is required.
65
71
  */
66
72
  function addDashboardTools(server, dashboards, options) {
67
73
  for (const tool of dashboardTools(dashboards, options)) {
@@ -1,6 +1,11 @@
1
1
  import { DashServer } from "../index.mjs";
2
2
  import "./format.mjs";
3
- import { DashOptions, Viewer } from "derive-dashboards/core";
3
+ import { DashOptions, ItemLinks, Viewer } from "derive-dashboards/core";
4
4
  import { z } from "zod";
5
5
  /** What the tools need of `deriveDashboards(...)`: its calls, by name and by path. */
6
- export type Callable = Pick<DashServer<DashOptions>, "api" | "call" | "routes">;
6
+ export type Callable = Pick<DashServer<DashOptions>, "api" | "call" | "routes"> & {
7
+ options?: {
8
+ model: unknown;
9
+ itemLinks?: ItemLinks | undefined;
10
+ };
11
+ };
@@ -1,23 +1,29 @@
1
1
  import { HttpError } from "../http.mjs";
2
+ import { drillRows } from "./drill.mjs";
2
3
  import { answerText, ok, table } from "./format.mjs";
3
4
  import { dashboardOf, resolveQuery, sourceOf } from "./resolve.mjs";
4
- import { picksSchema, wireQuerySchema } from "derive-dashboards/core/schema";
5
+ import { drillStateSchema, picksSchema, positional, wireQueryShape } from "derive-dashboards/core/schema";
5
6
  import { z } from "zod";
6
7
  // src/mcp/query.ts
7
8
  /** A question, with `kind` optional since there is only one kind. */
8
- const question = wireQuerySchema.extend({ kind: z.literal("aggregate").optional() });
9
- const queryInput = z.object({
9
+ const question = wireQueryShape.extend({ kind: z.literal("aggregate").optional() }).transform((query) => positional({
10
+ ...query,
11
+ kind: "aggregate"
12
+ }));
13
+ const queryInput = z.strictObject({
10
14
  op: z.enum([
11
15
  "describe",
12
16
  "ask",
13
17
  "rows",
14
18
  "list",
15
19
  "open"
16
- ]).describe("describe: what can be asked (sources, measures, fields). ask: answer a question. rows: the records behind a question. list: the dashboards. open: one dashboard, every tile answered."),
17
- query: question.optional().describe("For ask and rows. Sources, measures and fields may be named by id or label. Ids: measures m0, m1...; dimensions d0, d1..."),
20
+ ]).describe("describe: what can be asked (sources, measures, fields). ask: answer a question. rows: the records behind a question, or behind a mark (drill). list: the dashboards. open: one dashboard, every tile answered."),
21
+ query: question.optional().describe("For ask and rows. Sources, measures and fields may be named by id or label. Give each measure, dimension and calc an id (m1, d1, c1...) and use those ids in order, rankBy and formulas."),
18
22
  source: z.string().optional().describe("For describe: one source only."),
19
23
  dashboard: z.string().optional().describe("For open: the dashboard's title or id."),
20
24
  filters: picksSchema.optional().describe("For open: values for the dashboard's filters, by control id."),
25
+ drill: drillStateSchema.optional().describe("For rows: the rows behind one mark. Take origin, execution and a selection from an answer's drill (ask or open), as { origin, execution, path: [selection], view: { by: 'items' } }."),
26
+ after: z.string().optional().describe("For rows of a drill: the next page, from the answer before."),
21
27
  limit: z.number().int().min(1).max(200).optional().describe("For rows: how many (50 by default).")
22
28
  });
23
29
  const meta = (c) => c.dashboards.api.meta({ viewer: c.viewer });
@@ -31,7 +37,34 @@ async function describe(c, only) {
31
37
  `Fields: ${s.dimensions.map((d) => `${d.label} (${d.id}, ${d.type})`).join("; ")}`,
32
38
  ...s.joins.length === 0 ? [] : [`Joins: ${s.joins.map((j) => `${j.label} (${j.name} to ${j.source})`).join("; ")}`]
33
39
  ].join("\n"));
34
- return ok(text.join("\n\n") || "Nothing this viewer may read.", { sources });
40
+ const example = exampleOf(sources[0]);
41
+ const body = [...text, ...example === void 0 ? [] : [example]].join("\n\n");
42
+ return ok(body || "Nothing this viewer may read.", { sources });
43
+ }
44
+ /**
45
+ * One question to copy: the first source's first measure, by a text field. The first text field
46
+ * usually names each row (one group per row), so a later one is the better grouping.
47
+ */
48
+ function exampleOf(source) {
49
+ const measure = source?.measures[0];
50
+ if (source === void 0 || measure === void 0) return void 0;
51
+ const text = source.dimensions.filter((d) => d.type === "string");
52
+ const group = text[1] ?? text[0];
53
+ const query = {
54
+ source: source.name,
55
+ measures: [{
56
+ id: "m0",
57
+ measure: measure.id
58
+ }],
59
+ ...group === void 0 ? {} : { dimensions: [{
60
+ id: "d0",
61
+ member: group.id
62
+ }] }
63
+ };
64
+ return `Ask like: ${JSON.stringify({
65
+ op: "ask",
66
+ query
67
+ })}`;
35
68
  }
36
69
  /** The question, every name turned into its id. */
37
70
  async function resolved(c, input) {
@@ -50,6 +83,7 @@ async function ask(c, input) {
50
83
  return ok(answerText(answer), answer);
51
84
  }
52
85
  async function rows(c, input) {
86
+ if (input.drill !== void 0) return drillRows(c, input.drill, input);
53
87
  const body = {
54
88
  query: await resolved(c, input),
55
89
  limit: input.limit ?? 50