@mountaya/studio 0.0.0-stage → 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 (521) hide show
  1. package/CHANGELOG.md +69 -0
  2. package/LICENCE.fr.txt +280 -0
  3. package/LICENSE.txt +262 -0
  4. package/README.md +917 -2
  5. package/dist/agent/index.cjs +2600 -0
  6. package/dist/agent/index.cjs.map +1 -0
  7. package/dist/agent/index.d.cts +1237 -0
  8. package/dist/agent/index.d.ts +1237 -0
  9. package/dist/agent/index.js +2581 -0
  10. package/dist/agent/index.js.map +1 -0
  11. package/dist/chunk-3Oi_Zrtm.js +9 -0
  12. package/dist/chunk-3Oi_Zrtm.js.map +1 -0
  13. package/dist/chunk-B18OPIu_.js +6 -0
  14. package/dist/chunk-B18OPIu_.js.map +1 -0
  15. package/dist/chunk-B5Uz3Qct.cjs +15 -0
  16. package/dist/chunk-B5Uz3Qct.cjs.map +1 -0
  17. package/dist/chunk-BBEJwkd3.d.cts +839 -0
  18. package/dist/chunk-BIEc_SkR.cjs +29 -0
  19. package/dist/chunk-BIEc_SkR.cjs.map +1 -0
  20. package/dist/chunk-BKMFOe-N.js +171 -0
  21. package/dist/chunk-BKMFOe-N.js.map +1 -0
  22. package/dist/chunk-BP4uy7E1.d.ts +11583 -0
  23. package/dist/chunk-BSHTsFiB.js +1403 -0
  24. package/dist/chunk-BSHTsFiB.js.map +1 -0
  25. package/dist/chunk-BTUUABGI.js +17 -0
  26. package/dist/chunk-BTUUABGI.js.map +1 -0
  27. package/dist/chunk-BaPiaz5X.cjs +115 -0
  28. package/dist/chunk-BaPiaz5X.cjs.map +1 -0
  29. package/dist/chunk-BeCVpkYr.cjs +542 -0
  30. package/dist/chunk-BeCVpkYr.cjs.map +1 -0
  31. package/dist/chunk-BgngsvK8.cjs +6202 -0
  32. package/dist/chunk-BgngsvK8.cjs.map +1 -0
  33. package/dist/chunk-BhInd7CJ.d.cts +253 -0
  34. package/dist/chunk-Bn6ZbxkS.cjs +27 -0
  35. package/dist/chunk-By9ZnkVI.js +60 -0
  36. package/dist/chunk-By9ZnkVI.js.map +1 -0
  37. package/dist/chunk-BzqPhxsz.cjs +367 -0
  38. package/dist/chunk-BzqPhxsz.cjs.map +1 -0
  39. package/dist/chunk-C2mMX81q.d.ts +443 -0
  40. package/dist/chunk-C2rr9Q0V.cjs +1402 -0
  41. package/dist/chunk-C2rr9Q0V.cjs.map +1 -0
  42. package/dist/chunk-CDJCbwIQ.d.cts +78 -0
  43. package/dist/chunk-CLGvBOVH.d.cts +1654 -0
  44. package/dist/chunk-COdw1dc6.d.ts +525 -0
  45. package/dist/chunk-CQCJnT2i.d.ts +511 -0
  46. package/dist/chunk-CSyHY67q.cjs +353 -0
  47. package/dist/chunk-CSyHY67q.cjs.map +1 -0
  48. package/dist/chunk-CiK1Iqrv.js +950 -0
  49. package/dist/chunk-CiK1Iqrv.js.map +1 -0
  50. package/dist/chunk-CmJBgSwZ.cjs +24 -0
  51. package/dist/chunk-CmJBgSwZ.cjs.map +1 -0
  52. package/dist/chunk-CnrsRK2u.cjs +83 -0
  53. package/dist/chunk-CnrsRK2u.cjs.map +1 -0
  54. package/dist/chunk-Co782_9r.js +326 -0
  55. package/dist/chunk-Co782_9r.js.map +1 -0
  56. package/dist/chunk-Csr3KDpZ.cjs +32 -0
  57. package/dist/chunk-Csr3KDpZ.cjs.map +1 -0
  58. package/dist/chunk-CwkmccXv.js +89 -0
  59. package/dist/chunk-CwkmccXv.js.map +1 -0
  60. package/dist/chunk-D0Q4iL9B.cjs +255 -0
  61. package/dist/chunk-D0Q4iL9B.cjs.map +1 -0
  62. package/dist/chunk-DFwiSzUS.cjs +72 -0
  63. package/dist/chunk-DFwiSzUS.cjs.map +1 -0
  64. package/dist/chunk-DIFayl2P.d.cts +443 -0
  65. package/dist/chunk-DIGF58ux.d.cts +525 -0
  66. package/dist/chunk-DKuuuld5.js +101 -0
  67. package/dist/chunk-DKuuuld5.js.map +1 -0
  68. package/dist/chunk-DMePkFEo.js +5054 -0
  69. package/dist/chunk-DMePkFEo.js.map +1 -0
  70. package/dist/chunk-DO5SIBgu.js +305 -0
  71. package/dist/chunk-DO5SIBgu.js.map +1 -0
  72. package/dist/chunk-DOQ_7eE5.d.ts +6662 -0
  73. package/dist/chunk-DThaaNeb.d.cts +15 -0
  74. package/dist/chunk-DThaaNeb.d.ts +15 -0
  75. package/dist/chunk-DUQzjUJy.d.ts +78 -0
  76. package/dist/chunk-DX37IPhV.cjs +1903 -0
  77. package/dist/chunk-DX37IPhV.cjs.map +1 -0
  78. package/dist/chunk-Damps5Tk.cjs +155 -0
  79. package/dist/chunk-Damps5Tk.cjs.map +1 -0
  80. package/dist/chunk-DbK5PhpJ.d.ts +1654 -0
  81. package/dist/chunk-De0vAASv.d.ts +253 -0
  82. package/dist/chunk-DhSZ7YZt.d.cts +6662 -0
  83. package/dist/chunk-DjEvqvPA.cjs +43 -0
  84. package/dist/chunk-DjEvqvPA.cjs.map +1 -0
  85. package/dist/chunk-DkwDzwvu.d.cts +121 -0
  86. package/dist/chunk-DkwDzwvu.d.ts +121 -0
  87. package/dist/chunk-Dmszv9If.js +71 -0
  88. package/dist/chunk-Dmszv9If.js.map +1 -0
  89. package/dist/chunk-Dp_ftNWY.d.cts +511 -0
  90. package/dist/chunk-DsHQySz0.js +31 -0
  91. package/dist/chunk-DsHQySz0.js.map +1 -0
  92. package/dist/chunk-DwoUyHHh.js +281 -0
  93. package/dist/chunk-DwoUyHHh.js.map +1 -0
  94. package/dist/chunk-IaIDQflj.d.cts +2315 -0
  95. package/dist/chunk-IaIDQflj.d.ts +2315 -0
  96. package/dist/chunk-cVXLsZfL.d.ts +839 -0
  97. package/dist/chunk-mIaCX9EJ.d.cts +11583 -0
  98. package/dist/chunk-nJQxbOwQ.cjs +8962 -0
  99. package/dist/chunk-nJQxbOwQ.cjs.map +1 -0
  100. package/dist/chunk-o9jaxhCO.js +8852 -0
  101. package/dist/chunk-o9jaxhCO.js.map +1 -0
  102. package/dist/chunk-p-Tt_Lwz.js +14 -0
  103. package/dist/chunk-p-Tt_Lwz.js.map +1 -0
  104. package/dist/engines/index.cjs +2091 -0
  105. package/dist/engines/index.cjs.map +1 -0
  106. package/dist/engines/index.d.cts +2176 -0
  107. package/dist/engines/index.d.ts +2176 -0
  108. package/dist/engines/index.js +2075 -0
  109. package/dist/engines/index.js.map +1 -0
  110. package/dist/handlers/index.cjs +53 -0
  111. package/dist/handlers/index.cjs.map +1 -0
  112. package/dist/handlers/index.d.cts +403 -0
  113. package/dist/handlers/index.d.ts +403 -0
  114. package/dist/handlers/index.js +33 -0
  115. package/dist/handlers/index.js.map +1 -0
  116. package/dist/index.cjs +3511 -0
  117. package/dist/index.cjs.map +1 -0
  118. package/dist/index.d.cts +8 -0
  119. package/dist/index.d.ts +8 -0
  120. package/dist/index.js +3033 -0
  121. package/dist/index.js.map +1 -0
  122. package/dist/plugins/index.cjs +510 -0
  123. package/dist/plugins/index.cjs.map +1 -0
  124. package/dist/plugins/index.d.cts +296 -0
  125. package/dist/plugins/index.d.ts +296 -0
  126. package/dist/plugins/index.js +489 -0
  127. package/dist/plugins/index.js.map +1 -0
  128. package/dist/seams/index.cjs +89 -0
  129. package/dist/seams/index.cjs.map +1 -0
  130. package/dist/seams/index.d.cts +452 -0
  131. package/dist/seams/index.d.ts +452 -0
  132. package/dist/seams/index.js +49 -0
  133. package/dist/seams/index.js.map +1 -0
  134. package/dist/strings/fr/index.cjs +2135 -0
  135. package/dist/strings/fr/index.cjs.map +1 -0
  136. package/dist/strings/fr/index.d.cts +17 -0
  137. package/dist/strings/fr/index.d.ts +17 -0
  138. package/dist/strings/fr/index.js +2130 -0
  139. package/dist/strings/fr/index.js.map +1 -0
  140. package/dist/strings/index.cjs +2384 -0
  141. package/dist/strings/index.cjs.map +1 -0
  142. package/dist/strings/index.d.cts +3 -0
  143. package/dist/strings/index.d.ts +3 -0
  144. package/dist/strings/index.js +2353 -0
  145. package/dist/strings/index.js.map +1 -0
  146. package/dist/testing/index.cjs +230 -0
  147. package/dist/testing/index.cjs.map +1 -0
  148. package/dist/testing/index.d.cts +246 -0
  149. package/dist/testing/index.d.ts +246 -0
  150. package/dist/testing/index.js +219 -0
  151. package/dist/testing/index.js.map +1 -0
  152. package/dist/time/index.cjs +28 -0
  153. package/dist/time/index.cjs.map +1 -0
  154. package/dist/time/index.d.cts +41 -0
  155. package/dist/time/index.d.ts +41 -0
  156. package/dist/time/index.js +23 -0
  157. package/dist/time/index.js.map +1 -0
  158. package/package.json +224 -4
  159. package/src/agent/engines/agentThreads.ts +1601 -0
  160. package/src/agent/engines/directorChat.ts +500 -0
  161. package/src/agent/index.ts +67 -0
  162. package/src/agent/rules/agentEditLock.ts +115 -0
  163. package/src/agent/rules/agentErrorCopy.ts +44 -0
  164. package/src/agent/rules/copilotOrganization.ts +36 -0
  165. package/src/agent/rules/playbook.gen.ts +444 -0
  166. package/src/agent/rules/router.ts +518 -0
  167. package/src/agent/rules/stateDelta.ts +29 -0
  168. package/src/agent/rules/studioRouter.ts +123 -0
  169. package/src/agent/rules/surfaces.gen.ts +633 -0
  170. package/src/agent/rules/threadsUrl.ts +48 -0
  171. package/src/agent/rules/transcriptObserver.ts +90 -0
  172. package/src/agent/stores/agentThreads.ts +451 -0
  173. package/src/bus/contracts/noData.ts +18 -0
  174. package/src/bus/contracts/shared.ts +172 -0
  175. package/src/bus/contracts/studioTools.ts +224 -0
  176. package/src/bus/rules/bus.ts +857 -0
  177. package/src/bus/rules/confirmation.ts +22 -0
  178. package/src/bus/rules/defineRegistry.ts +24 -0
  179. package/src/bus/rules/errors.ts +401 -0
  180. package/src/bus/rules/handlerErrors.ts +103 -0
  181. package/src/bus/rules/handlerRegistry.ts +627 -0
  182. package/src/bus/rules/inputSchema.ts +232 -0
  183. package/src/bus/rules/invalidInput.ts +441 -0
  184. package/src/bus/rules/lateBoundTools.ts +100 -0
  185. package/src/bus/rules/studioRegistry.ts +324 -0
  186. package/src/bus/rules/studioToolBus.ts +73 -0
  187. package/src/bus/rules/types.ts +804 -0
  188. package/src/chrome/contracts/alert.ts +42 -0
  189. package/src/chrome/contracts/itinerary.ts +26 -0
  190. package/src/chrome/contracts/map.ts +23 -0
  191. package/src/chrome/contracts/overlay.ts +46 -0
  192. package/src/chrome/contracts/panel.ts +29 -0
  193. package/src/chrome/contracts/question.ts +52 -0
  194. package/src/chrome/contracts/selection.ts +26 -0
  195. package/src/chrome/contracts/sidenav.ts +23 -0
  196. package/src/chrome/contracts/snapshot.ts +319 -0
  197. package/src/chrome/contracts/state.ts +22 -0
  198. package/src/chrome/contracts/tabs.ts +56 -0
  199. package/src/chrome/handlers/alert.ts +91 -0
  200. package/src/chrome/handlers/introspection.ts +180 -0
  201. package/src/chrome/handlers/panels.ts +42 -0
  202. package/src/chrome/handlers/question.ts +24 -0
  203. package/src/chrome/handlers/sidenav.ts +116 -0
  204. package/src/chrome/handlers/snapshot.ts +451 -0
  205. package/src/chrome/handlers/stateDelta.ts +125 -0
  206. package/src/chrome/handlers/tabs.ts +143 -0
  207. package/src/chrome/handlers/timestamp.ts +19 -0
  208. package/src/chrome/rules/agentSelectors.ts +210 -0
  209. package/src/chrome/rules/mapAlert.ts +13 -0
  210. package/src/chrome/rules/question.ts +25 -0
  211. package/src/chrome/rules/tabs.ts +13 -0
  212. package/src/chrome/stores/activeDraftSession.ts +134 -0
  213. package/src/chrome/stores/chrome.ts +527 -0
  214. package/src/chrome/stores/uiStore.ts +575 -0
  215. package/src/collab/contracts/collab.ts +261 -0
  216. package/src/collab/handlers/collab.ts +468 -0
  217. package/src/collab/rules/toolNames.ts +33 -0
  218. package/src/collab/stores/collab.ts +899 -0
  219. package/src/collections/contracts/collection.ts +182 -0
  220. package/src/collections/handlers/collectionAttach.ts +141 -0
  221. package/src/collections/handlers/collections.ts +499 -0
  222. package/src/collections/handlers/heldCollection.ts +42 -0
  223. package/src/collections/handlers/restCollections.ts +386 -0
  224. package/src/collections/rules/collections.ts +138 -0
  225. package/src/collections/rules/entityCollections.ts +53 -0
  226. package/src/collections/stores/collections.ts +133 -0
  227. package/src/data/contracts/data.ts +233 -0
  228. package/src/data/contracts/itinerary.ts +71 -0
  229. package/src/data/handlers/activityResolve.ts +76 -0
  230. package/src/data/handlers/data.ts +200 -0
  231. package/src/data/handlers/graphql.ts +26 -0
  232. package/src/data/handlers/mutationSink.ts +1577 -0
  233. package/src/data/handlers/profile.ts +83 -0
  234. package/src/data/handlers/writeThrough.ts +417 -0
  235. package/src/data/rules/cache.ts +181 -0
  236. package/src/data/rules/dataTypes.ts +40 -0
  237. package/src/data/rules/keys.ts +106 -0
  238. package/src/data/rules/persistence.ts +1058 -0
  239. package/src/data/rules/requestBody.ts +43 -0
  240. package/src/data/rules/restErrors.ts +354 -0
  241. package/src/data/rules/sessionSurface.ts +156 -0
  242. package/src/data/rules/toolResultData.ts +73 -0
  243. package/src/data/rules/uuid.ts +24 -0
  244. package/src/edit/contracts/edit.ts +37 -0
  245. package/src/edit/contracts/itinerary.ts +57 -0
  246. package/src/edit/handlers/draftCommit.ts +257 -0
  247. package/src/edit/handlers/editDiscard.ts +34 -0
  248. package/src/edit/handlers/editLock.ts +949 -0
  249. package/src/edit/handlers/editor.ts +203 -0
  250. package/src/edit/handlers/formUnsaved.ts +24 -0
  251. package/src/edit/rules/draftMetadata.ts +117 -0
  252. package/src/edit/rules/editKeyboard.ts +222 -0
  253. package/src/edit/rules/editShortcuts.ts +102 -0
  254. package/src/edit/rules/editTooltip.ts +174 -0
  255. package/src/edit/rules/editorTaken.ts +49 -0
  256. package/src/edit/rules/history.ts +26 -0
  257. package/src/edit/rules/unsavedWork.ts +60 -0
  258. package/src/edit/stores/editorReservation.ts +70 -0
  259. package/src/edit/stores/formsUnsaved.ts +54 -0
  260. package/src/embed/contracts/embed.ts +48 -0
  261. package/src/embed/engines/embedFraming.ts +1329 -0
  262. package/src/embed/engines/embedReady.ts +353 -0
  263. package/src/embed/engines/framingPageSession.ts +169 -0
  264. package/src/embed/handlers/embed.ts +130 -0
  265. package/src/embed/rules/allowedDomains.ts +71 -0
  266. package/src/embed/rules/bridgeMessages.ts +224 -0
  267. package/src/embed/rules/defaults.ts +173 -0
  268. package/src/embed/rules/displaySeed.ts +115 -0
  269. package/src/embed/rules/embedAllowlist.ts +146 -0
  270. package/src/embed/rules/embedConstraints.ts +215 -0
  271. package/src/embed/rules/embedElements.ts +277 -0
  272. package/src/embed/rules/embedSectionFields.ts +126 -0
  273. package/src/embed/rules/embedSecurity.ts +116 -0
  274. package/src/embed/rules/fieldDependencies.ts +89 -0
  275. package/src/embed/rules/fieldOptions.ts +138 -0
  276. package/src/embed/rules/layerOptions.ts +161 -0
  277. package/src/embed/rules/readyProtocol.ts +66 -0
  278. package/src/embed/rules/safetyRules.ts +215 -0
  279. package/src/embed/rules/snippet.ts +531 -0
  280. package/src/embed/rules/tableCode.ts +270 -0
  281. package/src/embed/stores/embed.ts +60 -0
  282. package/src/embed/stores/embedContent.ts +77 -0
  283. package/src/engines/index.ts +81 -0
  284. package/src/handlers/index.ts +66 -0
  285. package/src/index.ts +1065 -0
  286. package/src/itinerary/contracts/itinerary.ts +705 -0
  287. package/src/itinerary/engines/itineraryPace.ts +612 -0
  288. package/src/itinerary/engines/profileMarker.ts +238 -0
  289. package/src/itinerary/handlers/importStaging.ts +352 -0
  290. package/src/itinerary/handlers/itinerary.ts +572 -0
  291. package/src/itinerary/handlers/itineraryDuplicate.ts +177 -0
  292. package/src/itinerary/handlers/itineraryExport.ts +117 -0
  293. package/src/itinerary/handlers/itineraryImport.ts +783 -0
  294. package/src/itinerary/handlers/itineraryPublish.ts +118 -0
  295. package/src/itinerary/handlers/itinerarySelection.ts +354 -0
  296. package/src/itinerary/handlers/itinerarySuggest.ts +1186 -0
  297. package/src/itinerary/handlers/itineraryUnpublish.ts +101 -0
  298. package/src/itinerary/handlers/publicationScope.ts +118 -0
  299. package/src/itinerary/handlers/restItineraries.ts +752 -0
  300. package/src/itinerary/handlers/restItineraryExport.ts +70 -0
  301. package/src/itinerary/handlers/restItineraryImport.ts +311 -0
  302. package/src/itinerary/handlers/restItineraryPace.ts +87 -0
  303. package/src/itinerary/handlers/restItineraryRecord.ts +36 -0
  304. package/src/itinerary/handlers/suggestParams.ts +379 -0
  305. package/src/itinerary/handlers/suggestStaging.ts +363 -0
  306. package/src/itinerary/handlers/surfaceOpen.ts +389 -0
  307. package/src/itinerary/rules/activity.ts +139 -0
  308. package/src/itinerary/rules/analysis.ts +42 -0
  309. package/src/itinerary/rules/ceiling.ts +49 -0
  310. package/src/itinerary/rules/createFlow.ts +107 -0
  311. package/src/itinerary/rules/densify.ts +440 -0
  312. package/src/itinerary/rules/draftSurface.ts +219 -0
  313. package/src/itinerary/rules/editorView.ts +83 -0
  314. package/src/itinerary/rules/export.ts +159 -0
  315. package/src/itinerary/rules/friendlyName.ts +77 -0
  316. package/src/itinerary/rules/import.ts +1050 -0
  317. package/src/itinerary/rules/importFiles.ts +140 -0
  318. package/src/itinerary/rules/importTotals.ts +77 -0
  319. package/src/itinerary/rules/liveSegment.ts +712 -0
  320. package/src/itinerary/rules/lngLat.ts +17 -0
  321. package/src/itinerary/rules/mapSync.ts +100 -0
  322. package/src/itinerary/rules/previewRender.ts +416 -0
  323. package/src/itinerary/rules/profileReadout.ts +216 -0
  324. package/src/itinerary/rules/routing.ts +707 -0
  325. package/src/itinerary/rules/suggest.ts +522 -0
  326. package/src/itinerary/rules/suggestionPreview.ts +119 -0
  327. package/src/itinerary/rules/view.ts +61 -0
  328. package/src/itinerary/stores/importSession.ts +319 -0
  329. package/src/itinerary/stores/itinerary.ts +331 -0
  330. package/src/itinerary/stores/itineraryDuplicate.ts +76 -0
  331. package/src/itinerary/stores/sessionIdentity.ts +43 -0
  332. package/src/itinerary/stores/suggestSession.ts +418 -0
  333. package/src/layers/contracts/itinerary.ts +46 -0
  334. package/src/layers/contracts/overlay.ts +132 -0
  335. package/src/layers/handlers/enrichment.ts +36 -0
  336. package/src/layers/handlers/overlay.ts +406 -0
  337. package/src/layers/handlers/restLayers.ts +94 -0
  338. package/src/layers/handlers/tiles.ts +25 -0
  339. package/src/layers/rules/layers.ts +212 -0
  340. package/src/layers/rules/overlayInstant.ts +12 -0
  341. package/src/layers/rules/reachabilityIcon.ts +18 -0
  342. package/src/layers/rules/readout.ts +102 -0
  343. package/src/layers/stores/layers.ts +262 -0
  344. package/src/map/contracts/contextmenu.ts +33 -0
  345. package/src/map/contracts/geolocate.ts +37 -0
  346. package/src/map/contracts/map.ts +146 -0
  347. package/src/map/contracts/streetview.ts +53 -0
  348. package/src/map/contracts/terrain.ts +152 -0
  349. package/src/map/handlers/contextMenu.ts +135 -0
  350. package/src/map/handlers/geolocate.ts +52 -0
  351. package/src/map/handlers/map.ts +391 -0
  352. package/src/map/handlers/streetview.ts +94 -0
  353. package/src/map/handlers/terrain.ts +663 -0
  354. package/src/map/rules/baseTerrain.ts +75 -0
  355. package/src/map/rules/clickTargets.ts +464 -0
  356. package/src/map/rules/constraints.ts +32 -0
  357. package/src/map/rules/contextMenuReadout.ts +67 -0
  358. package/src/map/rules/coordinates.ts +71 -0
  359. package/src/map/rules/itineraryFit.ts +8 -0
  360. package/src/map/rules/mapEvents.ts +64 -0
  361. package/src/map/rules/mapProjection.ts +476 -0
  362. package/src/map/rules/terrainReading.ts +62 -0
  363. package/src/plugins/contracts/ids.ts +66 -0
  364. package/src/plugins/contracts/plugin.ts +202 -0
  365. package/src/plugins/contracts/slots.ts +137 -0
  366. package/src/plugins/engines/studioPlugin.ts +1365 -0
  367. package/src/plugins/index.ts +111 -0
  368. package/src/plugins/rules/contributions.ts +152 -0
  369. package/src/plugins/rules/errors.ts +60 -0
  370. package/src/plugins/rules/ids.ts +42 -0
  371. package/src/plugins/rules/validation.ts +249 -0
  372. package/src/rules/listeners.ts +57 -0
  373. package/src/rules/origin.ts +13 -0
  374. package/src/rules/storeWrite.ts +37 -0
  375. package/src/rules/types.ts +136 -0
  376. package/src/seams/index.ts +79 -0
  377. package/src/seams/rules/clock.ts +28 -0
  378. package/src/seams/rules/formatters.ts +211 -0
  379. package/src/seams/rules/ids.ts +102 -0
  380. package/src/seams/rules/links.ts +66 -0
  381. package/src/seams/rules/navigation.ts +222 -0
  382. package/src/seams/rules/notify.ts +49 -0
  383. package/src/seams/rules/persistenceDrawer.ts +220 -0
  384. package/src/seams/rules/saveFile.ts +101 -0
  385. package/src/seams/rules/seams.ts +156 -0
  386. package/src/seams/rules/storage.ts +116 -0
  387. package/src/search/contracts/overlay.ts +74 -0
  388. package/src/search/contracts/search.ts +197 -0
  389. package/src/search/handlers/pois.ts +57 -0
  390. package/src/search/handlers/search.ts +280 -0
  391. package/src/search/rules/params.ts +925 -0
  392. package/src/search/rules/poiGroups.ts +138 -0
  393. package/src/search/rules/searchFetch.ts +139 -0
  394. package/src/search/rules/timeWindow.ts +30 -0
  395. package/src/search/stores/search.ts +51 -0
  396. package/src/segments/contracts/itinerary.ts +433 -0
  397. package/src/segments/contracts/segment.ts +35 -0
  398. package/src/segments/engines/segmentDraft.ts +926 -0
  399. package/src/segments/handlers/chainGuards.ts +67 -0
  400. package/src/segments/handlers/directions.ts +44 -0
  401. package/src/segments/handlers/itineraryEcho.ts +45 -0
  402. package/src/segments/handlers/roadAccessDialog.ts +34 -0
  403. package/src/segments/handlers/segment.ts +111 -0
  404. package/src/segments/handlers/segmentEdit.ts +1341 -0
  405. package/src/segments/handlers/segmentGroups.ts +347 -0
  406. package/src/segments/handlers/segmentStructure.ts +1144 -0
  407. package/src/segments/rules/chain.ts +196 -0
  408. package/src/segments/rules/chainModel.ts +217 -0
  409. package/src/segments/rules/colors.ts +11 -0
  410. package/src/segments/rules/commit.ts +76 -0
  411. package/src/segments/rules/draft.ts +90 -0
  412. package/src/segments/rules/draftDisposition.ts +43 -0
  413. package/src/segments/rules/draftSegmentEdit.ts +129 -0
  414. package/src/segments/rules/engine.ts +222 -0
  415. package/src/segments/rules/groupSave.ts +234 -0
  416. package/src/segments/rules/hazardPrompt.ts +60 -0
  417. package/src/segments/rules/itineraryEcho.ts +40 -0
  418. package/src/segments/rules/liveSegmentProjection.ts +55 -0
  419. package/src/segments/rules/merge.ts +51 -0
  420. package/src/segments/rules/metrics.ts +456 -0
  421. package/src/segments/rules/naming.ts +168 -0
  422. package/src/segments/rules/segmentArming.ts +144 -0
  423. package/src/segments/rules/splitPick.ts +84 -0
  424. package/src/segments/rules/viaSimplify.ts +230 -0
  425. package/src/segments/stores/directions.ts +77 -0
  426. package/src/segments/stores/roadAccessDialog.ts +58 -0
  427. package/src/segments/stores/segmentEdit.ts +922 -0
  428. package/src/segments/stores/segmentGroupEdit.ts +84 -0
  429. package/src/segments/stores/segmentSplit.ts +77 -0
  430. package/src/share/contracts/share.ts +39 -0
  431. package/src/share/contracts/shareLink.ts +63 -0
  432. package/src/share/engines/shareLink.ts +1053 -0
  433. package/src/share/handlers/share.ts +47 -0
  434. package/src/share/handlers/shareLink.ts +231 -0
  435. package/src/share/handlers/shareScope.ts +54 -0
  436. package/src/share/rules/link.ts +89 -0
  437. package/src/share/rules/shareLink.ts +218 -0
  438. package/src/share/rules/targets.ts +92 -0
  439. package/src/share/rules/viewConfig.ts +95 -0
  440. package/src/share/stores/share.ts +58 -0
  441. package/src/strings/fr/index.ts +18 -0
  442. package/src/strings/fr/rules/fr.ts +2368 -0
  443. package/src/strings/index.ts +98 -0
  444. package/src/strings/rules/confirmCopy.ts +156 -0
  445. package/src/strings/rules/draftToolNames.ts +94 -0
  446. package/src/strings/rules/en.ts +2519 -0
  447. package/src/strings/rules/failureAlert.ts +115 -0
  448. package/src/strings/rules/formError.ts +400 -0
  449. package/src/strings/rules/maneuvers.ts +107 -0
  450. package/src/strings/rules/outputOptions.ts +126 -0
  451. package/src/strings/rules/publicationState.ts +100 -0
  452. package/src/strings/rules/routeHazardCopy.ts +62 -0
  453. package/src/strings/rules/searchLabels.ts +50 -0
  454. package/src/strings/rules/segmentChainVerbs.ts +62 -0
  455. package/src/strings/rules/studioCopy.ts +130 -0
  456. package/src/strings/rules/unitOptions.ts +119 -0
  457. package/src/testing/checkedBus.ts +226 -0
  458. package/src/testing/index.ts +30 -0
  459. package/src/testing/plugins.ts +106 -0
  460. package/src/testing/studio.ts +267 -0
  461. package/src/testing/urlSeam.ts +153 -0
  462. package/src/time/index.ts +12 -0
  463. package/src/time/rules/sun.ts +78 -0
  464. package/src/video/contracts/cameraSchemas.ts +266 -0
  465. package/src/video/contracts/profileSchemas.ts +37 -0
  466. package/src/video/contracts/video.ts +601 -0
  467. package/src/video/handlers/video.ts +1638 -0
  468. package/src/video/handlers/videoDirector.ts +114 -0
  469. package/src/video/rules/previewRefresh.ts +76 -0
  470. package/src/video/rules/profileSource.ts +51 -0
  471. package/src/video/rules/routeSpans.ts +79 -0
  472. package/src/video/rules/shotDefaults.ts +28 -0
  473. package/src/video/rules/stepOrder.ts +17 -0
  474. package/src/video/rules/videoSnapshot.ts +136 -0
  475. package/src/video/stores/directorThreads.ts +167 -0
  476. package/src/video/stores/videoExport.ts +290 -0
  477. package/src/viewer/contracts/preferences.ts +40 -0
  478. package/src/viewer/handlers/preferences.ts +91 -0
  479. package/src/viewer/rules/capabilities.ts +42 -0
  480. package/src/viewer/rules/config.ts +266 -0
  481. package/src/viewer/rules/context.ts +375 -0
  482. package/src/viewer/rules/ownership.ts +134 -0
  483. package/src/viewer/rules/platformViewer.ts +199 -0
  484. package/src/viewer/rules/scope.ts +197 -0
  485. package/src/viewer/rules/url.ts +145 -0
  486. package/src/viewer/rules/viewer.ts +307 -0
  487. package/src/viewer/rules/viewerSeam.ts +49 -0
  488. package/src/viewer/rules/viewerUnits.ts +34 -0
  489. package/src/waypoints/contracts/itinerary.ts +417 -0
  490. package/src/waypoints/handlers/poiWaypointStyle.ts +32 -0
  491. package/src/waypoints/handlers/waypointDetailsDialog.ts +38 -0
  492. package/src/waypoints/handlers/waypointEdit.ts +1073 -0
  493. package/src/waypoints/rules/amenities.ts +30 -0
  494. package/src/waypoints/rules/defaults.ts +31 -0
  495. package/src/waypoints/rules/details.ts +213 -0
  496. package/src/waypoints/rules/detailsDraft.ts +312 -0
  497. package/src/waypoints/rules/detailsPreview.ts +76 -0
  498. package/src/waypoints/rules/detailsSave.ts +356 -0
  499. package/src/waypoints/rules/draftDirty.ts +45 -0
  500. package/src/waypoints/rules/icons.ts +32 -0
  501. package/src/waypoints/rules/liveWaypoint.ts +63 -0
  502. package/src/waypoints/rules/naming.ts +185 -0
  503. package/src/waypoints/rules/sections.ts +77 -0
  504. package/src/waypoints/rules/waypointDraft.ts +62 -0
  505. package/src/waypoints/stores/waypointDetails.ts +62 -0
  506. package/src/waypoints/stores/waypointEdit.ts +566 -0
  507. package/src/zones/contracts/collection.ts +37 -0
  508. package/src/zones/contracts/zone.ts +528 -0
  509. package/src/zones/engines/zoneForm.ts +1773 -0
  510. package/src/zones/handlers/restZones.ts +287 -0
  511. package/src/zones/handlers/zonePublish.ts +206 -0
  512. package/src/zones/handlers/zoneUnpublish.ts +56 -0
  513. package/src/zones/handlers/zones.ts +2254 -0
  514. package/src/zones/rules/geometry.ts +946 -0
  515. package/src/zones/rules/zoneDefaults.ts +34 -0
  516. package/src/zones/rules/zoneDraft.ts +54 -0
  517. package/src/zones/rules/zoneForm.ts +563 -0
  518. package/src/zones/rules/zoneMetadata.ts +29 -0
  519. package/src/zones/rules/zoneSource.ts +87 -0
  520. package/src/zones/rules/zoneSourceRefusal.ts +104 -0
  521. package/src/zones/stores/zoneEdit.ts +998 -0
@@ -0,0 +1,663 @@
1
+ import type { Layers, Mountaya } from "@mountaya/client";
2
+ import { segmentsForSelection } from "@mountaya/elements/data";
3
+ import { segmentLineCoordinates } from "@mountaya/elements/geo";
4
+ import type { MapClient } from "@mountaya/elements/map-client";
5
+ import { classifySteepness } from "@mountaya/elements/profile";
6
+
7
+ import type { StudioToolInputs, StudioToolOutputs } from "../../bus/contracts/studioTools.js";
8
+ import type { ToolFailure, ToolResult } from "../../bus/rules/types.js";
9
+ import { selectBaseMapLocked } from "../../chrome/stores/uiStore.js";
10
+ import type { StudioCache } from "../../data/rules/cache.js";
11
+ import { fetchLayers, loadedLayers } from "../../layers/handlers/restLayers.js";
12
+ import type { TerrainVariant } from "../../search/rules/params.js";
13
+ import { pointAtFraction } from "../../segments/rules/metrics.js";
14
+ import type { StudioToolContext, StudioToolDefinition } from "../../viewer/rules/context.js";
15
+ import { AREA_GRID_MIN } from "../contracts/terrain.js";
16
+ import { type OverlaySampleResult, terrainChannelsOf } from "../rules/terrainReading.js";
17
+
18
+ /** What `studio.terrain.set` is asked for: a terrain's slug and, when one is named, its tint. */
19
+ type TerrainChoice = StudioToolInputs["studio.terrain.set"];
20
+
21
+ /** listsTerrain answers whether `layers` lists a terrain under `slug`. */
22
+ const listsTerrain = (layers: Layers, slug: string): boolean =>
23
+ layers.terrains.some((terrain) => terrain.slug === slug);
24
+
25
+ /**
26
+ * The `studio.terrain.set` dispatched last per studio, counted and keyed by its UI store.
27
+ *
28
+ * A switch whose manifest is read settles after the switches dispatched meanwhile. Each
29
+ * dispatch takes the next count as it begins, on every path, and a switch whose manifest is
30
+ * read again writes the store only while its count is still the latest: an earlier pick never
31
+ * overwrites a later one. The count is also the name a pick is recorded under while a pick
32
+ * written ahead waits on its manifest ({@link UnheldPicks}).
33
+ */
34
+ const latestSwitches = new WeakMap<StudioToolContext["ui"], number>();
35
+
36
+ /** The base terrain a studio's store holds: the slug, and the tint it is drawn in. */
37
+ interface StoredTerrain {
38
+ readonly terrain: string;
39
+ readonly variant: TerrainVariant;
40
+ }
41
+
42
+ /**
43
+ * RecordedWrite is one entry of a studio's {@link UnheldPicks}: a pick the verb stored, or a
44
+ * value another hand stored that the verb met in the store.
45
+ */
46
+ interface RecordedWrite {
47
+ /** What the store was put on. */
48
+ readonly choice: TerrainChoice;
49
+ /**
50
+ * The count of the dispatch that stored it ({@link latestSwitches}), or null for a value no
51
+ * pick of the verb stored (a host writing its own store), which no refusal takes back.
52
+ */
53
+ readonly ticket: number | null;
54
+ }
55
+
56
+ /**
57
+ * UnheldPicks is the record of one studio's store while a pick written to it ahead of its
58
+ * manifest ({@link storeAhead}) still waits on the read: what the store held before the first
59
+ * such pick, and every write since that the verb made or met, on any of its paths.
60
+ *
61
+ * It is what a refused pick is taken back against ({@link takeBack}). The refusal lands after
62
+ * later picks were stored, by the same path or by another, so what the refused pick alone wrote
63
+ * is told apart from theirs by playing the record with it and without it.
64
+ */
65
+ interface UnheldPicks {
66
+ /** What the store held before the first pick written ahead. */
67
+ readonly before: StoredTerrain;
68
+ /** How many picks written ahead still wait on their read. */
69
+ pending: number;
70
+ /**
71
+ * Every write since, in the order stored. A pick is stored in the order dispatched: one whose
72
+ * manifest is read again is stored only while it is the latest. A refused pick leaves the
73
+ * list, and a value another hand stored never does.
74
+ */
75
+ readonly written: RecordedWrite[];
76
+ }
77
+
78
+ /** The {@link UnheldPicks} of each studio, keyed by its UI store, while any waits on its read. */
79
+ const unheldPicks = new WeakMap<StudioToolContext["ui"], UnheldPicks>();
80
+
81
+ /**
82
+ * standingOf plays a record: what a store that held `before` holds once each pick of `written`
83
+ * is applied in order, a pick that names no tint keeping the one before it.
84
+ */
85
+ const standingOf = (before: StoredTerrain, written: Iterable<RecordedWrite>): StoredTerrain => {
86
+ let standing = before;
87
+ for (const { choice } of written) {
88
+ standing = { terrain: choice.terrain, variant: choice.variant ?? standing.variant };
89
+ }
90
+
91
+ return standing;
92
+ };
93
+
94
+ /**
95
+ * recordWhatStands puts on a record what the store holds where that is none of the record's: a
96
+ * value another hand stored since the last write the record holds (a host writing its own
97
+ * store). It goes on under no count, ahead of the pick about to be stored, so a pick that is
98
+ * taken back lands on it.
99
+ */
100
+ const recordWhatStands = (ui: StudioToolContext["ui"], picks: UnheldPicks): void => {
101
+ const held = ui.getState();
102
+ const standing = standingOf(picks.before, picks.written);
103
+ if (held.terrain !== standing.terrain || held.terrainVariant !== standing.variant) {
104
+ picks.written.push({
105
+ choice: { terrain: held.terrain, variant: held.terrainVariant },
106
+ ticket: null,
107
+ });
108
+ }
109
+ };
110
+
111
+ /**
112
+ * storeTerrain writes the chosen terrain, and its tint when one is named, to the UI store and
113
+ * answers the switch. While an edit holds the base map the answer says the switch waits for
114
+ * the edit's end.
115
+ *
116
+ * `ticket` is the dispatch's count. While a pick written ahead waits on its manifest, the pick
117
+ * stored here goes on the studio's record under it, whichever path stores it, behind what
118
+ * another hand stored before it ({@link recordWhatStands}).
119
+ */
120
+ const storeTerrain = (
121
+ ctx: StudioToolContext,
122
+ choice: TerrainChoice,
123
+ ticket: number,
124
+ ): ToolResult<undefined> => {
125
+ const picks = unheldPicks.get(ctx.ui);
126
+ if (picks) {
127
+ recordWhatStands(ctx.ui, picks);
128
+ }
129
+
130
+ ctx.ui.getState().setTerrain(choice.terrain);
131
+ if (choice.variant) {
132
+ ctx.ui.getState().setTerrainVariant(choice.variant);
133
+ }
134
+
135
+ picks?.written.push({ choice, ticket });
136
+
137
+ return {
138
+ ok: true,
139
+ summary: selectBaseMapLocked(ctx.ui.getState())
140
+ ? `Terrain set to ${choice.terrain}. An edit holds the base map, so the switch applies when the edit ends.`
141
+ : `Terrain set to ${choice.terrain}.`,
142
+ };
143
+ };
144
+
145
+ /**
146
+ * takeBack undoes a pick that was written ahead and then refused: the pick leaves the record,
147
+ * and the store is put on what the record plays to without it.
148
+ *
149
+ * Member by member, and only where the store still holds what the record played to with the
150
+ * pick. A later pick's slug, and a tint a later pick named, are what the record plays to either
151
+ * way, so they stay. A value another hand stored after the pick (a host writing its own store)
152
+ * is not what the record played to, and stays as well. One stored before the pick is on the
153
+ * record ({@link recordWhatStands}), and is what the store is put back on.
154
+ */
155
+ const takeBack = (ui: StudioToolContext["ui"], picks: UnheldPicks, ticket: number): void => {
156
+ const withIt = standingOf(picks.before, picks.written);
157
+ const at = picks.written.findIndex((write) => write.ticket === ticket);
158
+ if (at !== -1) {
159
+ picks.written.splice(at, 1);
160
+ }
161
+
162
+ const withoutIt = standingOf(picks.before, picks.written);
163
+
164
+ const held = ui.getState();
165
+ if (held.terrain === withIt.terrain && held.terrain !== withoutIt.terrain) {
166
+ held.setTerrain(withoutIt.terrain);
167
+ }
168
+
169
+ if (held.terrainVariant === withIt.variant && held.terrainVariant !== withoutIt.variant) {
170
+ held.setTerrainVariant(withoutIt.variant);
171
+ }
172
+ };
173
+
174
+ /**
175
+ * terrainNotFound is the refusal for a slug `layers` does not list. It names the terrains the
176
+ * manifest does list, so a caller picks one of them, and says so when it lists none.
177
+ */
178
+ const terrainNotFound = (layers: Layers, slug: string): ToolFailure => {
179
+ const held = layers.terrains.map((terrain) => terrain.slug);
180
+
181
+ return {
182
+ code: "not-found",
183
+ error: `set terrain: the layers manifest holds no terrain "${slug}". ${
184
+ held.length > 0 ? `It holds: ${held.join(", ")}.` : "It lists no terrain."
185
+ }`,
186
+ ok: false,
187
+ };
188
+ };
189
+
190
+ /**
191
+ * The first read of the manifest in flight for each client, so the picks written ahead of it
192
+ * share one request and are answered in the order they were dispatched.
193
+ */
194
+ const firstReads = new WeakMap<Mountaya, Promise<Layers | null>>();
195
+
196
+ /**
197
+ * readFirstManifest reads the manifest for a client that has read none, once however many picks
198
+ * wait on it, and resolves null for a read that failed. It never rejects.
199
+ */
200
+ const readFirstManifest = (
201
+ client: Mountaya,
202
+ cache: StudioCache | undefined,
203
+ ): Promise<Layers | null> => {
204
+ const inFlight = firstReads.get(client);
205
+ if (inFlight) {
206
+ return inFlight;
207
+ }
208
+
209
+ const read = fetchLayers(client, cache)
210
+ .catch(() => null)
211
+ .finally(() => {
212
+ firstReads.delete(client);
213
+ });
214
+ firstReads.set(client, read);
215
+
216
+ return read;
217
+ };
218
+
219
+ /**
220
+ * storeAhead is `studio.terrain.set` on a client that has read no manifest, where the host's
221
+ * cache holds none either: the pick is in the store before the dispatch yields, and is held
222
+ * against the manifest once it is read.
223
+ *
224
+ * The write comes first because a cold start relies on it: the verb seeds a terrain from a link
225
+ * or an embed's configuration, and a request awaited ahead of the write would let the map draw
226
+ * the default terrain first. The answer waits on the read, and nothing else does.
227
+ *
228
+ * A slug the manifest lists stands. One it lacks is refused with `not-found`, and what the pick
229
+ * wrote is taken back ({@link takeBack}): the store reads as if the pick had not been made,
230
+ * whatever was picked after it and whichever path stored that. A manifest that cannot be read
231
+ * refuses nothing, since nothing then says the slug is wrong, and the pick stands as the
232
+ * preference it is.
233
+ */
234
+ const storeAhead = (
235
+ ctx: StudioToolContext,
236
+ client: Mountaya,
237
+ choice: TerrainChoice,
238
+ ticket: number,
239
+ ): Promise<ToolResult<undefined>> => {
240
+ const { ui } = ctx;
241
+ const before = ui.getState();
242
+ const picks = unheldPicks.get(ui) ?? {
243
+ before: { terrain: before.terrain, variant: before.terrainVariant },
244
+ pending: 0,
245
+ written: [],
246
+ };
247
+ picks.pending += 1;
248
+ unheldPicks.set(ui, picks);
249
+ const stored = storeTerrain(ctx, choice, ticket);
250
+
251
+ return readFirstManifest(client, ctx.api?.cache).then((layers) => {
252
+ picks.pending -= 1;
253
+ if (picks.pending === 0) {
254
+ unheldPicks.delete(ui);
255
+ }
256
+
257
+ if (layers === null || listsTerrain(layers, choice.terrain)) {
258
+ return stored;
259
+ }
260
+
261
+ takeBack(ui, picks, ticket);
262
+
263
+ return terrainNotFound(layers, choice.terrain);
264
+ });
265
+ };
266
+
267
+ /**
268
+ * setTerrain switches the studio's base layer to a terrain by slug and, when one is supplied,
269
+ * its light or dark tint, through the UI store. It is a display verb: the embedded studio seeds
270
+ * its base terrain and tint from the organization's embed configuration through it, and the
271
+ * in-app terrain switcher changes the slug alone.
272
+ *
273
+ * A slug the layers manifest does not hold is refused with `not-found`, naming the slugs it
274
+ * does hold, and nothing is stored: answering "set" for a mistyped slug would report a switch
275
+ * the map never makes, since a stored slug the manifest lacks draws the first terrain
276
+ * (`resolveBaseTerrain`). The slug is held against the manifest this context's client last
277
+ * read (`loadedLayers`) and, when that one lacks it, against the manifest read again
278
+ * (`fetchLayers`): a manifest gains terrains while a client lives, and a binding that reads it
279
+ * through its own cache can offer one the remembered copy does not list. A read that fails
280
+ * leaves the remembered manifest to answer.
281
+ *
282
+ * A slug the remembered manifest lists is in the store before the dispatch yields, and no
283
+ * request is made for it. So is any slug while the client has read no manifest, which is what a
284
+ * cold start relies on ({@link storeAhead}): that pick is then held against the manifest the
285
+ * handler reads for it, once for the client, and a slug it lacks is refused with what the pick
286
+ * wrote taken back. The store then reads as if the pick had not been made: on what stood before
287
+ * it, with every pick stored since, by this path or another, as it stands. A context with no
288
+ * data seam has no manifest to read, and its slug is stored as the preference it is.
289
+ *
290
+ * While an edit holds the base map (a segment, a waypoint or a zone session) the terrain is
291
+ * stored all the same and the answer says so: a map client keeps the switch and applies it
292
+ * when the edit ends, so the answer tells a caller the base layer has not changed yet.
293
+ *
294
+ * Of two switches in flight the later dispatch is the one stored ({@link latestSwitches}). A
295
+ * switch whose manifest is read again stores nothing once a later one was dispatched, and
296
+ * answers `ok` with a summary that says so. A slug the fresh manifest lacks is refused all the
297
+ * same.
298
+ */
299
+ export const setTerrain: StudioToolDefinition<"studio.terrain.set"> = {
300
+ handler: (ctx, input) => {
301
+ const ticket = (latestSwitches.get(ctx.ui) ?? 0) + 1;
302
+ latestSwitches.set(ctx.ui, ticket);
303
+
304
+ const client = ctx.api?.client;
305
+ if (!client) {
306
+ return storeTerrain(ctx, input, ticket);
307
+ }
308
+
309
+ const remembered = loadedLayers(client, ctx.api?.cache);
310
+ if (!remembered) {
311
+ return storeAhead(ctx, client, input, ticket);
312
+ }
313
+
314
+ if (listsTerrain(remembered, input.terrain)) {
315
+ return storeTerrain(ctx, input, ticket);
316
+ }
317
+
318
+ return fetchLayers(client, ctx.api?.cache)
319
+ .catch(() => remembered)
320
+ .then((layers) => {
321
+ if (!listsTerrain(layers, input.terrain)) {
322
+ return terrainNotFound(layers, input.terrain);
323
+ }
324
+
325
+ if (latestSwitches.get(ctx.ui) !== ticket) {
326
+ return {
327
+ ok: true,
328
+ summary: `Left the terrain as it is: a later switch replaced the one to ${input.terrain}.`,
329
+ };
330
+ }
331
+
332
+ return storeTerrain(ctx, input, ticket);
333
+ });
334
+ },
335
+ };
336
+
337
+ /**
338
+ * SAMPLES_DEFAULT is how closely this studio reads a segment when the caller did not say. A
339
+ * handler's judgement rather than the contract's: twelve points read a day's route closely
340
+ * enough to see the surface change along it.
341
+ */
342
+ const SAMPLES_DEFAULT = 12;
343
+
344
+ /** AREA_GRID_DEFAULT is the per-axis resolution an omitted cols/rows resolves to. */
345
+ const AREA_GRID_DEFAULT = 8;
346
+
347
+ /**
348
+ * AREA_CELLS_MAX caps the total cell count of an area sample (cols × rows). Each
349
+ * cell is one DEM read through the same per-point reader the point/segment
350
+ * samplers use, so an unbounded grid would issue thousands of synchronous reads
351
+ * and stall the frame. When a caller's cols × rows exceeds this, the grid is
352
+ * clamped down to the largest cols × rows that fits while preserving the
353
+ * requested aspect, and the clamp is reported in the result so the agent can
354
+ * coarsen deliberately rather than silently losing resolution.
355
+ */
356
+ const AREA_CELLS_MAX = 256;
357
+
358
+ /**
359
+ * TERRAIN_NOT_LOADED_REASON is the reason of the structured outcome the DEM samplers return
360
+ * when the client DEM yields no value at a point: terrain is not enabled in this session, or
361
+ * the point falls outside the loaded terrain coverage. It is an `ok` result (not a throw)
362
+ * carrying a `loaded: false` flag and a `reason` the model reads to decide whether to enable
363
+ * terrain rather than conclude the surface is flat. The studio bakes 3D terrain into every
364
+ * session, so an unloaded sample usually means the point is off coverage: frame it first, then
365
+ * sample again.
366
+ */
367
+ const TERRAIN_NOT_LOADED_REASON =
368
+ "no terrain sample at this point: 3D terrain is not loaded here. Frame the point on the map (it must be in view with terrain active) and re-sample, or enable terrain.";
369
+
370
+ /**
371
+ * sampleDem reads the client DEM at one [lng, lat] through the map's `sampleOverlayAt` and
372
+ * answers the elevation, slope and aspect it holds, with the steepness band of a slope that
373
+ * has one (`classifySteepness`, the derivation the right-click readout applies, so a tool and
374
+ * the tooltip state one number). Null when the DEM has no value there: terrain off, or the
375
+ * point off the loaded coverage.
376
+ *
377
+ * The reading is read through {@link terrainChannelsOf}, as the context menu reads it: a
378
+ * channel is kept when it is a finite number and dropped otherwise, and a reading that holds
379
+ * no channel at all is no reading. Nothing here throws, since each sampler promises a
380
+ * structured outcome. Every terrain sampler reads the DEM through it, so all read it alike.
381
+ */
382
+ const sampleDem = (map: MapClient, lngLat: [number, number]): OverlaySampleResult | null => {
383
+ const sample = terrainChannelsOf(map.sampleOverlayAt(lngLat));
384
+ if (!sample) {
385
+ return null;
386
+ }
387
+
388
+ if (sample.slope !== undefined) {
389
+ const steepnessClass = classifySteepness(sample.slope);
390
+ if (steepnessClass !== null) {
391
+ sample.steepness_class = steepnessClass;
392
+ }
393
+ }
394
+
395
+ return sample;
396
+ };
397
+
398
+ /**
399
+ * terrainSampleAt reads the surface at one [lng, lat] from the client DEM: elevation
400
+ * (meters), slope (degrees), and aspect (0..360° compass bearing pointing downhill). It is the
401
+ * surface-analysis path the dem-surface overlays (steepness, aspectslope) are read through,
402
+ * distinct from the vector overlay sampleAt: the "how steep is it here" and "which way does
403
+ * this face" reader.
404
+ *
405
+ * It needs the map, since the DEM it samples is the one the browser holds.
406
+ * Returns the not-loaded outcome ({@link TERRAIN_NOT_LOADED_REASON}), an `ok` result and not a
407
+ * throw, when the DEM has no value at the point, with terrain off or the point off the loaded
408
+ * coverage, so the model can frame and sample again or enable terrain rather than read the
409
+ * absence as flat ground. Returns a structured failure when no map is mounted. ctx is the
410
+ * first positional parameter.
411
+ */
412
+ export const terrainSampleAt: StudioToolDefinition<"studio.terrain.sampleAt"> = {
413
+ // Hidden from the model's manifest: DEM sampling in the browser reads only tiles
414
+ // the map has ALREADY rendered, so it is blind outside the viewport and
415
+ // reports loaded:false for ground it simply has not drawn. The agent reads the
416
+ // surface through the Data API's terrainSamples / terrainProfile / terrainGrid
417
+ // operations, which have no such limit. This stays for interactive
418
+ // UI, where an instant GPU read drives the hover readout and the live
419
+ // elevation profile and a network call would wreck the feel.
420
+ agentHidden: true,
421
+ handler: (ctx, input) => {
422
+ const { map } = ctx;
423
+ if (!map) {
424
+ return { code: "no-map", error: "no map is currently mounted", ok: false };
425
+ }
426
+
427
+ const sample = sampleDem(map, input.lngLat);
428
+ if (!sample) {
429
+ return {
430
+ data: { lngLat: input.lngLat, loaded: false, reason: TERRAIN_NOT_LOADED_REASON },
431
+ ok: true,
432
+ summary: "No terrain sample at this point (terrain not loaded here).",
433
+ };
434
+ }
435
+
436
+ return {
437
+ data: { lngLat: input.lngLat, loaded: true, sample },
438
+ ok: true,
439
+ summary: `Surface at [${input.lngLat[0]}, ${input.lngLat[1]}]: ${describeSample(sample)}.`,
440
+ };
441
+ },
442
+ };
443
+
444
+ /**
445
+ * TerrainSegmentSample is one DEM reading along a segment, as the verb's output states it: its
446
+ * fraction (0 to 1) along the line, the [lng, lat] sampled, and the surface triple there, or
447
+ * `loaded: false` when the DEM has no value at that point, so a partially-covered segment
448
+ * reports per-point coverage rather than failing the whole read.
449
+ */
450
+ type TerrainSegmentSample =
451
+ StudioToolOutputs["studio.terrain.sampleAlongSegment"]["samples"][number];
452
+
453
+ /**
454
+ * terrainSampleAlongSegment reads the surface at evenly spaced points along an itinerary
455
+ * segment from the client DEM (elevation, slope, and aspect per point): the along-route
456
+ * surface-analysis read behind "how steep is the second half of this segment". It reuses
457
+ * pointAtFraction so the sample points land on the true along-path locations, and reuses the
458
+ * same DEM reader as the point sampler.
459
+ *
460
+ * It needs the map, since the DEM it samples is the one the browser holds. It answers
461
+ * `no-map` with no map mounted, `no-session` with no itinerary open, `not-found` for a
462
+ * segment the open itinerary does not hold and `invalid-input` for one with no line to read
463
+ * along: no geometry, a line under two points, which is the line
464
+ * `studio.overlay.sampleAlongSegment` refuses, or a line that cannot be measured (a coordinate
465
+ * that is no finite longitude and latitude). Per-point coverage is reported inline
466
+ * (loaded:false for an off-coverage point) rather than failing the whole sample. With no
467
+ * `samples` named it reads `SAMPLES_DEFAULT` points. ctx is the first positional parameter.
468
+ */
469
+ export const terrainSampleAlongSegment: StudioToolDefinition<"studio.terrain.sampleAlongSegment"> =
470
+ {
471
+ // Hidden from the model's manifest, for the reason stated on terrainSampleAt's handler:
472
+ // a browser DEM read is blind to ground the map has not drawn.
473
+ agentHidden: true,
474
+ handler: (ctx, input) => {
475
+ const { map } = ctx;
476
+ if (!map) {
477
+ return { code: "no-map", error: "no map is currently mounted", ok: false };
478
+ }
479
+
480
+ const itinerary = ctx.ui.getState().activeItinerary;
481
+ if (!itinerary) {
482
+ return { code: "no-session", error: "no itinerary is currently open", ok: false };
483
+ }
484
+
485
+ const segment = segmentsForSelection(itinerary, input.segmentId)[0];
486
+ if (!segment) {
487
+ return { code: "not-found", error: `segment not found: ${input.segmentId}`, ok: false };
488
+ }
489
+
490
+ // One point is a place and no line: every fraction of it is that point, and a sample
491
+ // along it would read one place as many times as it was asked for points.
492
+ if (segmentLineCoordinates(segment.geojson).length < 2) {
493
+ return {
494
+ code: "invalid-input",
495
+ error: `segment ${input.segmentId} has no line geometry`,
496
+ ok: false,
497
+ };
498
+ }
499
+
500
+ // A line of two points can still hold nowhere to read: the walk answers no location, at
501
+ // any fraction, for a line that cannot be measured.
502
+ if (pointAtFraction(segment.geojson, 0) === null) {
503
+ return {
504
+ code: "invalid-input",
505
+ error: `segment ${input.segmentId} holds a line that cannot be measured`,
506
+ ok: false,
507
+ };
508
+ }
509
+
510
+ const samples = input.samples ?? SAMPLES_DEFAULT;
511
+ const readings: TerrainSegmentSample[] = [];
512
+ for (let index = 0; index < samples; index += 1) {
513
+ const fraction = index / (samples - 1);
514
+ const point = pointAtFraction(segment.geojson, fraction);
515
+ if (!point) {
516
+ continue;
517
+ }
518
+
519
+ const sample = sampleDem(map, point);
520
+ readings.push(
521
+ sample
522
+ ? { fraction, lngLat: point, loaded: true, sample }
523
+ : { fraction, lngLat: point, loaded: false },
524
+ );
525
+ }
526
+
527
+ const loaded = readings.filter((reading) => reading.loaded).length;
528
+
529
+ return {
530
+ data: { samples: readings, segmentId: input.segmentId },
531
+ ok: true,
532
+ summary: `Sampled the surface at ${readings.length} points along segment ${input.segmentId} (${loaded} with terrain).`,
533
+ };
534
+ },
535
+ };
536
+
537
+ /**
538
+ * TerrainAreaCell is one DEM reading on the area grid, as the verb's output states it: its
539
+ * integer row (0 at the south edge) and col (0 at the west edge), the [lng, lat] sampled, and
540
+ * the surface triple there, or `loaded: false` when the DEM has no value at that point, so a
541
+ * partially-covered area reports per-cell coverage rather than failing the whole read. It
542
+ * mirrors the along-segment sampler's per-point coverage shape so a caller reads area and
543
+ * segment samples the same way.
544
+ */
545
+ type TerrainAreaCell = StudioToolOutputs["studio.terrain.sampleArea"]["cells"][number];
546
+
547
+ /**
548
+ * clampGrid reduces a requested cols × rows grid to the largest grid whose total
549
+ * cell count is within AREA_CELLS_MAX while preserving the requested aspect ratio
550
+ * and keeping at least AREA_GRID_MIN on each axis, the floor the contract holds a
551
+ * request to: two points along an axis still span the bbox edges. It returns the resolved grid
552
+ * and whether it had to clamp, so the tool can report a deliberate coarsening to
553
+ * the agent. A grid already within the cap passes through unchanged.
554
+ */
555
+ const clampGrid = (
556
+ cols: number,
557
+ rows: number,
558
+ ): { clamped: boolean; cols: number; rows: number } => {
559
+ if (cols * rows <= AREA_CELLS_MAX) {
560
+ return { clamped: false, cols, rows };
561
+ }
562
+
563
+ // Scale both axes by the same factor so the grid keeps its shape, then floor to
564
+ // at least the minimum so neither axis collapses below a spanning 2 points.
565
+ const scale = Math.sqrt(AREA_CELLS_MAX / (cols * rows));
566
+ const clampedCols = Math.max(AREA_GRID_MIN, Math.floor(cols * scale));
567
+ const clampedRows = Math.max(AREA_GRID_MIN, Math.floor(rows * scale));
568
+
569
+ return { clamped: true, cols: clampedCols, rows: clampedRows };
570
+ };
571
+
572
+ /**
573
+ * terrainSampleArea reads the surface over a bounding box from the client DEM, sampling a
574
+ * cols × rows grid of evenly spaced points and returning the same
575
+ * elevation/slope/aspect/steepness reading per cell as the point and along-segment samplers.
576
+ * It is the area read that lets the agent threshold or contour the grid into a zone (a >30°
577
+ * slope-band polygon, an N-facing aspect sector, an above-2500 m elevation band). It reuses
578
+ * the one DEM reader (sampleDem → map.sampleOverlayAt), so the area read honors the exact same
579
+ * DEM contract; raw terrain-RGB is never exposed. The grid edges land on the bbox corners
580
+ * (col 0 = west, row 0 = south), and the total cell count is clamped to AREA_CELLS_MAX: a
581
+ * clamp is reported in the result so the agent coarsens deliberately rather than silently
582
+ * losing resolution.
583
+ *
584
+ * It needs the map, since the DEM it samples is the one the browser holds. Returns a
585
+ * structured failure when no map is mounted; per-cell coverage is reported inline
586
+ * (loaded:false for an off-coverage cell) rather than failing the whole sample. ctx is
587
+ * the first positional parameter.
588
+ */
589
+ export const terrainSampleArea: StudioToolDefinition<"studio.terrain.sampleArea"> = {
590
+ // Hidden from the model's manifest, for the reason stated on terrainSampleAt's handler:
591
+ // a browser DEM read is blind to ground the map has not drawn.
592
+ agentHidden: true,
593
+ handler: (ctx, input) => {
594
+ const { map } = ctx;
595
+ if (!map) {
596
+ return { code: "no-map", error: "no map is currently mounted", ok: false };
597
+ }
598
+
599
+ const [west, south, east, north] = input.bbox;
600
+ const requestedCols = input.cols ?? AREA_GRID_DEFAULT;
601
+ const requestedRows = input.rows ?? AREA_GRID_DEFAULT;
602
+ const { clamped, cols, rows } = clampGrid(requestedCols, requestedRows);
603
+
604
+ const cells: TerrainAreaCell[] = [];
605
+ let loaded = 0;
606
+ for (let row = 0; row < rows; row += 1) {
607
+ // Step the grid edges onto the bbox corners: fraction 0 at the south/west
608
+ // edge, 1 at the north/east edge. Both axes are at least 2 (the schema floor
609
+ // and the clamp lower bound), so the denominators never divide by zero.
610
+ const lat = south + (row / (rows - 1)) * (north - south);
611
+ for (let col = 0; col < cols; col += 1) {
612
+ const lng = west + (col / (cols - 1)) * (east - west);
613
+ const lngLat: [number, number] = [lng, lat];
614
+
615
+ const sample = sampleDem(map, lngLat);
616
+ if (sample) {
617
+ loaded += 1;
618
+ cells.push({ col, lngLat, loaded: true, row, sample });
619
+ } else {
620
+ cells.push({ col, lngLat, loaded: false, row });
621
+ }
622
+ }
623
+ }
624
+
625
+ return {
626
+ data: {
627
+ bbox: input.bbox,
628
+ cells,
629
+ clamped,
630
+ cols,
631
+ rows,
632
+ },
633
+ ok: true,
634
+ summary: `Sampled the surface over a ${cols}×${rows} grid (${cells.length} cells, ${loaded} with terrain)${clamped ? ", with the grid clamped to the cell cap" : ""}.`,
635
+ };
636
+ },
637
+ };
638
+
639
+ /**
640
+ * describeSample renders the elevation/slope/aspect triple into the human/LLM summary line,
641
+ * dropping any field the DEM did not resolve. Used only for the one-line
642
+ * summary; the structured `data` carries the raw triple.
643
+ */
644
+ const describeSample = (sample: OverlaySampleResult): string => {
645
+ const parts: string[] = [];
646
+ if (sample.elevation !== undefined) {
647
+ parts.push(`${sample.elevation} m`);
648
+ }
649
+
650
+ if (sample.slope !== undefined) {
651
+ parts.push(`${sample.slope}° slope`);
652
+ }
653
+
654
+ if (sample.aspect !== undefined) {
655
+ parts.push(`aspect ${sample.aspect}°`);
656
+ }
657
+
658
+ if (sample.steepness_class !== undefined) {
659
+ parts.push(`steepness ${sample.steepness_class}`);
660
+ }
661
+
662
+ return parts.length > 0 ? parts.join(", ") : "no data";
663
+ };