@chengxianglibra/dsh-data-analysis 0.1.1 → 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 (303) hide show
  1. package/README.md +139 -53
  2. package/cordis.patch.yml +1 -1
  3. package/lib/bin/environment.js +9 -7
  4. package/lib/bin/presentation-lint.js +21 -0
  5. package/lib/bridges.js +2 -2
  6. package/lib/client/credentials/changes.js +74 -0
  7. package/lib/client/credentials/create-datasource.js +171 -0
  8. package/lib/client/credentials/credential-draft.js +42 -0
  9. package/lib/client/credentials/defaults.js +11 -0
  10. package/lib/client/credentials/descriptions.js +46 -0
  11. package/lib/client/credentials/install.js +319 -0
  12. package/lib/client/credentials/model.js +766 -0
  13. package/lib/client/credentials/report-publishing.js +96 -0
  14. package/lib/client/credentials/styles.js +21 -0
  15. package/lib/client/i18n/context.js +22 -0
  16. package/lib/client/i18n/copy.js +57 -0
  17. package/lib/client/i18n/credentials.js +475 -0
  18. package/lib/client/i18n/host.js +27 -0
  19. package/lib/client/i18n/navigation.js +41 -0
  20. package/lib/client/i18n/presentation.js +577 -0
  21. package/lib/client/i18n/python.js +36 -0
  22. package/lib/client/i18n/semantic.js +441 -0
  23. package/lib/client/input-context.js +21 -0
  24. package/lib/client/python-tool/card.js +35 -0
  25. package/lib/client/python-tool/execution-error.js +35 -0
  26. package/lib/client/python-tool/install.js +9 -0
  27. package/lib/client/python-tool/model.js +101 -0
  28. package/lib/client/right-tabs/install.js +535 -0
  29. package/lib/client/right-tabs/live-delivery.js +58 -0
  30. package/lib/client/right-tabs/navigation.js +47 -0
  31. package/lib/client/right-tabs/page.js +178 -0
  32. package/lib/client/right-tabs/styles.js +11 -0
  33. package/lib/client/rpc.js +5 -0
  34. package/lib/client/semantic-reference.js +10 -0
  35. package/lib/client/workspace-header-action.js +24 -0
  36. package/lib/client/workspace-shortcuts.js +26 -0
  37. package/lib/client.js +1250 -730
  38. package/lib/compatibility.js +2 -2
  39. package/lib/datasource/authoring.js +109 -0
  40. package/lib/datasource/bridge-programs.js +42 -27
  41. package/lib/datasource/bridge.js +144 -9
  42. package/lib/datasource/changes-contract.js +41 -0
  43. package/lib/datasource/changes.js +49 -0
  44. package/lib/datasource/configure.js +39 -0
  45. package/lib/datasource/defaults.js +25 -0
  46. package/lib/datasource/index.js +4 -2
  47. package/lib/datasource/python-options.js +21 -0
  48. package/lib/datasource/python.js +206 -0
  49. package/lib/datasource/resolver-program.js +112 -0
  50. package/lib/datasource/rpc.js +150 -0
  51. package/lib/datasource/service.js +962 -0
  52. package/lib/datasource/shell-env.js +2 -333
  53. package/lib/datasource/test.js +25 -51
  54. package/lib/disclosure/activation.js +19 -7
  55. package/lib/disclosure/execution-guidance.js +52 -0
  56. package/lib/disclosure/help.js +4 -1
  57. package/lib/environment/binding.js +54 -9
  58. package/lib/environment/runtime.js +120 -108
  59. package/lib/environment/subprocess.js +6 -1
  60. package/lib/environment/summary.js +3 -3
  61. package/lib/environment/workspace.js +23 -1
  62. package/lib/index.js +1 -1
  63. package/lib/lifecycle.js +33 -0
  64. package/lib/plugin-agents.js +154 -0
  65. package/lib/plugin.js +172 -133
  66. package/lib/presentation/asset.js +34 -0
  67. package/lib/presentation/assets/portable.js +606 -0
  68. package/lib/presentation/assets/static.js +1216 -0
  69. package/lib/presentation/build/index.js +72 -0
  70. package/lib/presentation/commit.js +163 -0
  71. package/lib/presentation/contracts/asset-path.js +9 -0
  72. package/lib/presentation/contracts/catalog.js +62 -0
  73. package/lib/presentation/contracts/charts.js +453 -0
  74. package/lib/presentation/contracts/editing.js +58 -0
  75. package/lib/presentation/contracts/index.js +663 -0
  76. package/lib/presentation/contracts/interaction.js +62 -0
  77. package/lib/presentation/contracts/types.js +15 -0
  78. package/lib/presentation/contracts/values.js +28 -0
  79. package/lib/presentation/delivery.js +71 -0
  80. package/lib/presentation/export-html.js +214 -0
  81. package/lib/presentation/files.js +82 -0
  82. package/lib/presentation/index.js +6 -0
  83. package/lib/presentation/lint.js +46 -0
  84. package/lib/presentation/projection/files.js +85 -0
  85. package/lib/presentation/projection/index.js +196 -0
  86. package/lib/presentation/projection/program.js +137 -0
  87. package/lib/presentation/projection/sql-code.js +79 -0
  88. package/lib/presentation/receipt.js +34 -0
  89. package/lib/presentation/reports.js +219 -0
  90. package/lib/presentation/rpc.js +186 -0
  91. package/lib/presentation/tool.js +118 -0
  92. package/lib/python-execution-contracts.js +1 -0
  93. package/lib/python-execution.js +232 -0
  94. package/lib/report-publishing/adapters.js +111 -0
  95. package/lib/report-publishing/config.js +69 -0
  96. package/lib/report-publishing/service.js +180 -0
  97. package/lib/rpc.js +49 -0
  98. package/lib/semantic-browser/contracts.js +97 -0
  99. package/lib/semantic-browser/definition.js +64 -0
  100. package/lib/semantic-browser/program.js +129 -0
  101. package/lib/semantic-browser/service.js +106 -0
  102. package/lib/semantic-reference/bridge.js +127 -0
  103. package/lib/semantic-reference/contracts.js +127 -0
  104. package/lib/semantic-reference/environment.js +56 -0
  105. package/lib/semantic-reference/index.js +3 -0
  106. package/lib/semantic-reference/labels.js +16 -0
  107. package/lib/semantic-reference/rpc.js +143 -0
  108. package/lib/semantic-reference/search.js +95 -0
  109. package/lib/semantic-reference/usage.js +152 -0
  110. package/lib/tool-lifecycle.js +22 -0
  111. package/lib/types/bridges.d.ts +2 -2
  112. package/lib/types/client/credentials/changes.d.ts +9 -0
  113. package/lib/types/client/credentials/create-datasource.d.ts +7 -0
  114. package/lib/types/client/credentials/credential-draft.d.ts +14 -0
  115. package/lib/types/client/credentials/defaults.d.ts +2 -0
  116. package/lib/types/client/credentials/descriptions.d.ts +1 -0
  117. package/lib/types/client/credentials/install.d.ts +19 -0
  118. package/lib/types/client/credentials/model.d.ts +79 -0
  119. package/lib/types/client/credentials/report-publishing.d.ts +5 -0
  120. package/lib/types/client/credentials/styles.d.ts +1 -0
  121. package/lib/types/client/i18n/context.d.ts +17 -0
  122. package/lib/types/client/i18n/copy.d.ts +806 -0
  123. package/lib/types/client/i18n/credentials.d.ts +475 -0
  124. package/lib/types/client/i18n/host.d.ts +16 -0
  125. package/lib/types/client/i18n/navigation.d.ts +41 -0
  126. package/lib/types/client/i18n/presentation.d.ts +577 -0
  127. package/lib/types/client/i18n/python.d.ts +19 -0
  128. package/lib/types/client/i18n/semantic.d.ts +441 -0
  129. package/lib/types/client/input-context.d.ts +19 -0
  130. package/lib/types/client/presentation/ask-dsh.d.ts +5 -0
  131. package/lib/types/client/presentation/catalog-model.d.ts +24 -0
  132. package/lib/types/client/presentation/catalog.d.ts +20 -0
  133. package/lib/types/client/presentation/chart-explorer.d.ts +12 -0
  134. package/lib/types/client/presentation/chart-geometry.d.ts +30 -0
  135. package/lib/types/client/presentation/chart-renderer.d.ts +10 -0
  136. package/lib/types/client/presentation/chart-specials.d.ts +10 -0
  137. package/lib/types/client/presentation/chart-styles.d.ts +1 -0
  138. package/lib/types/client/presentation/chart-tooltip.d.ts +13 -0
  139. package/lib/types/client/presentation/chart-view.d.ts +23 -0
  140. package/lib/types/client/presentation/context-reference.d.ts +12 -0
  141. package/lib/types/client/presentation/copy-context.d.ts +5 -0
  142. package/lib/types/client/presentation/delivery-model.d.ts +75 -0
  143. package/lib/types/client/presentation/delivery.d.ts +78 -0
  144. package/lib/types/client/presentation/download.d.ts +2 -0
  145. package/lib/types/client/presentation/editor-controls.d.ts +12 -0
  146. package/lib/types/client/presentation/export-menu.d.ts +26 -0
  147. package/lib/types/client/presentation/export-view.d.ts +63 -0
  148. package/lib/types/client/presentation/global-filters.d.ts +6 -0
  149. package/lib/types/client/presentation/host-entry.d.ts +18 -0
  150. package/lib/types/client/presentation/icons.d.ts +4 -0
  151. package/lib/types/client/presentation/markdown.d.ts +5 -0
  152. package/lib/types/client/presentation/model.d.ts +61 -0
  153. package/lib/types/client/presentation/reader.d.ts +24 -0
  154. package/lib/types/client/presentation/reference-source.d.ts +9 -0
  155. package/lib/types/client/presentation/source-code-model.d.ts +17 -0
  156. package/lib/types/client/presentation/source-code.d.ts +6 -0
  157. package/lib/types/client/presentation/source-dialog.d.ts +12 -0
  158. package/lib/types/client/presentation/source-facts.d.ts +20 -0
  159. package/lib/types/client/presentation/source-format.d.ts +5 -0
  160. package/lib/types/client/presentation/source-highlight.d.ts +7 -0
  161. package/lib/types/client/presentation/sources.d.ts +13 -0
  162. package/lib/types/client/presentation/styles.d.ts +1 -0
  163. package/lib/types/client/presentation/table.d.ts +16 -0
  164. package/lib/types/client/presentation/use-element-width.d.ts +5 -0
  165. package/lib/types/client/python-tool/card.d.ts +2 -0
  166. package/lib/types/client/python-tool/execution-error.d.ts +3 -0
  167. package/lib/types/client/python-tool/install.d.ts +2 -0
  168. package/lib/types/client/python-tool/model.d.ts +20 -0
  169. package/lib/types/client/right-tabs/install.d.ts +18 -0
  170. package/lib/types/client/right-tabs/live-delivery.d.ts +8 -0
  171. package/lib/types/client/right-tabs/navigation.d.ts +17 -0
  172. package/lib/types/client/right-tabs/page.d.ts +36 -0
  173. package/lib/types/client/right-tabs/styles.d.ts +2 -0
  174. package/lib/types/client/rpc.d.ts +3 -0
  175. package/lib/types/client/semantic-reference.d.ts +4 -0
  176. package/lib/types/client/workspace-header-action.d.ts +7 -0
  177. package/lib/types/client/workspace-shortcuts.d.ts +15 -0
  178. package/lib/types/client.d.ts +9 -124
  179. package/lib/types/compatibility.d.ts +3 -3
  180. package/lib/types/datasource/authoring.d.ts +35 -0
  181. package/lib/types/datasource/bridge.d.ts +43 -4
  182. package/lib/types/datasource/changes-contract.d.ts +18 -0
  183. package/lib/types/datasource/changes.d.ts +15 -0
  184. package/lib/types/datasource/configure.d.ts +7 -0
  185. package/lib/types/datasource/defaults.d.ts +7 -0
  186. package/lib/types/datasource/index.d.ts +8 -5
  187. package/lib/types/datasource/python-options.d.ts +9 -0
  188. package/lib/types/datasource/python.d.ts +19 -0
  189. package/lib/types/datasource/resolver-program.d.ts +6 -0
  190. package/lib/types/datasource/rpc.d.ts +5 -0
  191. package/lib/types/datasource/service.d.ts +155 -0
  192. package/lib/types/datasource/shell-env.d.ts +0 -29
  193. package/lib/types/datasource/test.d.ts +6 -29
  194. package/lib/types/disclosure/activation.d.ts +2 -1
  195. package/lib/types/disclosure/execution-guidance.d.ts +6 -0
  196. package/lib/types/disclosure/help.d.ts +1 -1
  197. package/lib/types/environment/binding.d.ts +5 -1
  198. package/lib/types/environment/errors.d.ts +1 -1
  199. package/lib/types/environment/runtime.d.ts +5 -5
  200. package/lib/types/environment/types.d.ts +15 -3
  201. package/lib/types/environment/workspace.d.ts +1 -0
  202. package/lib/types/index.d.ts +1 -1
  203. package/lib/types/lifecycle.d.ts +8 -0
  204. package/lib/types/plugin-agents.d.ts +27 -0
  205. package/lib/types/plugin.d.ts +14 -24
  206. package/lib/types/presentation/asset.d.ts +6 -0
  207. package/lib/types/presentation/build/index.d.ts +16 -0
  208. package/lib/types/presentation/commit.d.ts +4 -0
  209. package/lib/types/presentation/contracts/asset-path.d.ts +2 -0
  210. package/lib/types/presentation/contracts/catalog.d.ts +27 -0
  211. package/lib/types/presentation/contracts/charts.d.ts +11 -0
  212. package/lib/types/presentation/contracts/editing.d.ts +8 -0
  213. package/lib/types/presentation/contracts/index.d.ts +11 -0
  214. package/lib/types/presentation/contracts/interaction.d.ts +8 -0
  215. package/lib/types/presentation/contracts/types.d.ts +255 -0
  216. package/lib/types/presentation/contracts/values.d.ts +9 -0
  217. package/lib/types/presentation/delivery.d.ts +3 -0
  218. package/lib/types/presentation/export-html.d.ts +13 -0
  219. package/lib/types/presentation/files.d.ts +7 -0
  220. package/lib/types/presentation/index.d.ts +6 -0
  221. package/lib/types/presentation/lint.d.ts +13 -0
  222. package/lib/types/presentation/projection/files.d.ts +2 -0
  223. package/lib/types/presentation/projection/index.d.ts +20 -0
  224. package/lib/types/presentation/projection/program.d.ts +2 -0
  225. package/lib/types/presentation/projection/sql-code.d.ts +2 -0
  226. package/lib/types/presentation/receipt.d.ts +14 -0
  227. package/lib/types/presentation/reports.d.ts +10 -0
  228. package/lib/types/presentation/rpc.d.ts +25 -0
  229. package/lib/types/presentation/tool.d.ts +12 -0
  230. package/lib/types/python-execution-contracts.d.ts +10 -0
  231. package/lib/types/python-execution.d.ts +10 -0
  232. package/lib/types/report-publishing/adapters.d.ts +6 -0
  233. package/lib/types/report-publishing/config.d.ts +22 -0
  234. package/lib/types/report-publishing/service.d.ts +68 -0
  235. package/lib/types/rpc.d.ts +3 -0
  236. package/lib/types/semantic-browser/contracts.d.ts +47 -0
  237. package/lib/types/semantic-browser/definition.d.ts +20 -0
  238. package/lib/types/semantic-browser/program.d.ts +2 -0
  239. package/lib/types/semantic-browser/service.d.ts +26 -0
  240. package/lib/types/semantic-reference/bridge.d.ts +12 -0
  241. package/lib/types/semantic-reference/contracts.d.ts +54 -0
  242. package/lib/types/semantic-reference/environment.d.ts +22 -0
  243. package/lib/types/semantic-reference/index.d.ts +3 -0
  244. package/lib/types/semantic-reference/labels.d.ts +16 -0
  245. package/lib/types/semantic-reference/rpc.d.ts +15 -0
  246. package/lib/types/semantic-reference/search.d.ts +10 -0
  247. package/lib/types/semantic-reference/usage.d.ts +43 -0
  248. package/lib/types/tool-lifecycle.d.ts +4 -0
  249. package/lib/types/workspace-identity.d.ts +6 -0
  250. package/lib/workspace-identity.js +9 -0
  251. package/package.json +99 -53
  252. package/python/presentation-kit/dist/dsh_data_analysis_presentation_kit-1.1.0-py3-none-any.whl +0 -0
  253. package/skills/dsh-data-analysis-files/SKILL.md +32 -0
  254. package/skills/dsh-data-analysis-files/references/examples.md +93 -0
  255. package/skills/dsh-data-analysis-presentation/SKILL.md +28 -0
  256. package/skills/dsh-data-analysis-presentation/references/chart-examples.md +18 -0
  257. package/skills/dsh-data-analysis-presentation/references/charts.md +55 -0
  258. package/skills/dsh-data-analysis-presentation/references/examples/artifact.draft.json +36 -0
  259. package/skills/dsh-data-analysis-presentation/references/examples/charts-bins.dataset.json +33 -0
  260. package/skills/dsh-data-analysis-presentation/references/examples/charts-boxes.dataset.json +45 -0
  261. package/skills/dsh-data-analysis-presentation/references/examples/charts-composition.dataset.json +33 -0
  262. package/skills/dsh-data-analysis-presentation/references/examples/charts-funnel.dataset.json +27 -0
  263. package/skills/dsh-data-analysis-presentation/references/examples/charts-precision.dataset.json +21 -0
  264. package/skills/dsh-data-analysis-presentation/references/examples/charts-series.dataset.json +58 -0
  265. package/skills/dsh-data-analysis-presentation/references/examples/charts-shares.dataset.json +33 -0
  266. package/skills/dsh-data-analysis-presentation/references/examples/charts-waterfall.dataset.json +44 -0
  267. package/skills/dsh-data-analysis-presentation/references/examples/charts.draft.json +393 -0
  268. package/skills/dsh-data-analysis-presentation/references/examples/comparison-selected.dataset.json +24 -0
  269. package/skills/dsh-data-analysis-presentation/references/examples/comparison-summary.dataset.json +22 -0
  270. package/skills/dsh-data-analysis-presentation/references/examples/comparison.dataset.json +18 -0
  271. package/skills/dsh-data-analysis-presentation/references/examples/comparison.draft.json +80 -0
  272. package/skills/dsh-data-analysis-presentation/references/examples/computed-sources.draft.json +42 -0
  273. package/skills/dsh-data-analysis-presentation/references/examples/computed.dataset.json +12 -0
  274. package/skills/dsh-data-analysis-presentation/references/examples/computed.draft.json +58 -0
  275. package/skills/dsh-data-analysis-presentation/references/examples/interaction-detail.dataset.json +30 -0
  276. package/skills/dsh-data-analysis-presentation/references/examples/interaction-summary.dataset.json +22 -0
  277. package/skills/dsh-data-analysis-presentation/references/examples/interaction.draft.json +279 -0
  278. package/skills/dsh-data-analysis-presentation/references/examples/source-only.draft.json +27 -0
  279. package/skills/dsh-data-analysis-presentation/references/examples/write-charts.py +73 -0
  280. package/skills/dsh-data-analysis-presentation/references/examples/write-comparison.py +42 -0
  281. package/skills/dsh-data-analysis-presentation/references/examples/write-computed.py +21 -0
  282. package/skills/dsh-data-analysis-presentation/references/examples/write-interaction.py +33 -0
  283. package/skills/dsh-data-analysis-presentation/references/examples.md +44 -0
  284. package/skills/dsh-data-analysis-presentation/references/export-html.md +7 -0
  285. package/skills/dsh-data-analysis-presentation/references/interaction.md +56 -0
  286. package/skills/dsh-data-analysis-presentation/references/narrative.md +37 -0
  287. package/skills/dsh-data-analysis-presentation/references/publishing.md +3 -0
  288. package/skills/dsh-data-analysis-presentation/references/schema.md +162 -0
  289. package/lib/datasource/access.js +0 -60
  290. package/lib/datasource/credentials.js +0 -24
  291. package/lib/evidence/bridge-program.js +0 -153
  292. package/lib/evidence/bridge.js +0 -166
  293. package/lib/evidence/index.js +0 -2
  294. package/lib/evidence/sources.js +0 -263
  295. package/lib/types/datasource/access.d.ts +0 -27
  296. package/lib/types/evidence/bridge.d.ts +0 -40
  297. package/lib/types/evidence/index.d.ts +0 -4
  298. package/lib/types/evidence/sources.d.ts +0 -57
  299. package/python/report-kit/dist/dsh_data_analysis_report_kit-3.0.0-py3-none-any.whl +0 -0
  300. package/skills/dsh-data-analysis-report/SKILL.md +0 -57
  301. package/skills/dsh-data-analysis-report/assets/marivo-artifact.js +0 -113
  302. package/skills/dsh-data-analysis-report/assets/marivo-session-dag.js +0 -1201
  303. package/skills/dsh-data-analysis-report/assets/report-data.js +0 -449
@@ -0,0 +1,766 @@
1
+ import { credentialRevisionSchema } from "../../datasource/changes-contract.js";
2
+ import { PendingTasks } from "../../lifecycle.js";
3
+ import { CopyError, errorMessage, message } from "./../i18n/copy.js";
4
+ const CHANNEL = '/dsh-data-analysis-credentials';
5
+ const STORAGE_KEY = 'marivo-credential-operation';
6
+ const messages = {
7
+ 'datasource-remove-unavailable': 'marivo.credentials.this-runtime-does-not-support-datasource-deletion',
8
+ 'datasource-not-removable': 'marivo.credentials.this-datasource-is-not-a-removable-project-local-definition',
9
+ 'datasource-remove-failed': 'marivo.credentials.datasource-deletion-is-unconfirmed-refresh-the-list-to-check',
10
+ 'credential-delete-failed': 'marivo.credentials.datasource-deleted-but-some-credentials-could-not-be-deleted',
11
+ 'datasource-config-changed': 'marivo.credentials.configuration-changed-reopen-the-editor-before-saving',
12
+ 'datasource-identity-fixed': 'marivo.credentials.datasource-name-and-engine-cannot-be-changed',
13
+ 'context-changed': 'marivo.credentials.workspace-or-datasource-definition-changed-reload-before-continuing',
14
+ 'credentials-changed': 'marivo.credentials.credential-configuration-changed-reload-before-validating',
15
+ 'call-ended': 'marivo.credentials.the-original-call-ended-saved-values-remain-start-the',
16
+ 'credential-missing': 'marivo.credentials.some-credentials-are-missing-complete-them-before-validating',
17
+ 'credential-save-failed': 'marivo.credentials.some-credentials-could-not-be-saved-successfully-saved-entries',
18
+ 'operation-busy': 'marivo.credentials.this-operation-is-in-progress-wait-for-its-result',
19
+ 'credential-state-unavailable': 'marivo.credentials.credential-status-is-temporarily-unavailable-retry-later',
20
+ 'capacity-exceeded': 'marivo.credentials.the-operation-limit-has-been-reached-retry-later',
21
+ 'datasource-already-exists': 'marivo.credentials.this-datasource-already-exists-choose-another-name',
22
+ 'datasource-definition-invalid': 'marivo.credentials.invalid-datasource-definition-check-the-name-field-types-and',
23
+ 'datasource-credential-ref-invalid': 'marivo.credentials.invalid-credential-reference-in-env-fields-enter-a-reference',
24
+ 'datasource-authoring-unavailable': 'marivo.credentials.this-runtime-does-not-support-datasource-creation',
25
+ 'datasource-defaults-invalid': 'marivo.credentials.invalid-datasourcedefaults-format-use-a-json-mapping-from-engines',
26
+ 'datasource-defaults-backend-invalid': 'marivo.credentials.datasourcedefaults-contains-an-engine-unsupported-by-the-current-runtime',
27
+ 'datasource-defaults-field-invalid': 'marivo.credentials.datasourcedefaults-contains-a-field-unsupported-by-the-current-runtime',
28
+ 'datasource-defaults-type-invalid': 'marivo.credentials.datasourcedefaults-field-types-do-not-match-the-current-runtime',
29
+ 'datasource-defaults-credential-forbidden': 'marivo.credentials.datasourcedefaults-does-not-support-credential-reference-fields-use-the',
30
+ };
31
+ export function credentialMessage(code) {
32
+ return (messages[code] ??
33
+ 'marivo.credentials.credential-operation-failed-check-the-configuration-and-retry');
34
+ }
35
+ class CredentialResponseError extends Error {
36
+ code;
37
+ constructor(code) {
38
+ super(credentialMessage(code));
39
+ this.code = code;
40
+ }
41
+ }
42
+ function delay(ms, signal) {
43
+ return new Promise((resolve) => {
44
+ const done = () => {
45
+ clearTimeout(timer);
46
+ signal.removeEventListener('abort', done);
47
+ resolve();
48
+ };
49
+ const timer = setTimeout(done, ms);
50
+ signal.addEventListener('abort', done, { once: true });
51
+ if (signal.aborted)
52
+ done();
53
+ });
54
+ }
55
+ export class CredentialClientModel {
56
+ #state = {
57
+ open: false,
58
+ workspaceId: '',
59
+ sessionId: '',
60
+ generation: '',
61
+ datasources: [],
62
+ requests: [],
63
+ requestId: '',
64
+ selected: '',
65
+ loading: false,
66
+ error: '',
67
+ operations: [],
68
+ outcomes: {},
69
+ };
70
+ #changes;
71
+ #watchTasks = new PendingTasks();
72
+ #rpc;
73
+ #storage;
74
+ #listeners = new Set();
75
+ #lifetime = new AbortController();
76
+ #suspended = false;
77
+ #watch;
78
+ #read;
79
+ #refresh;
80
+ #polls = new Map();
81
+ #operations = new Map();
82
+ #opened = new Set();
83
+ constructor(rpc, storage, changes) {
84
+ this.#changes = changes;
85
+ this.#rpc = rpc;
86
+ this.#storage = storage;
87
+ }
88
+ getSnapshot = () => this.#state;
89
+ subscribe = (listener) => {
90
+ this.#listeners.add(listener);
91
+ return () => {
92
+ this.#listeners.delete(listener);
93
+ };
94
+ };
95
+ #patch(change) {
96
+ if (this.#lifetime.signal.aborted)
97
+ return;
98
+ this.#state = { ...this.#state, ...change };
99
+ for (const listener of this.#listeners)
100
+ listener();
101
+ }
102
+ async #call(endpoint, payload, signal = this.#lifetime.signal) {
103
+ const result = (await this.#rpc.call(CHANNEL, endpoint, payload, signal));
104
+ if (!result.ok)
105
+ throw new CredentialResponseError(result.error?.message ?? '');
106
+ return result.value;
107
+ }
108
+ async publishingCredentials(endpoint, workspaceId, payload, signal) {
109
+ const result = (await this.#rpc.call('/dsh-report-publishing', endpoint, { workspaceId, ...payload }, AbortSignal.any([signal, this.#lifetime.signal])));
110
+ if (!result.ok)
111
+ throw new Error(result.error?.message === 'report-publishing-config-changed'
112
+ ? 'report-publishing-config-changed'
113
+ : 'report-publishing-credential-operation-failed');
114
+ return result.value;
115
+ }
116
+ async show(workspaceId, selectedToken) {
117
+ this.#suspended = false;
118
+ this.#patch({ open: true, requestId: '' });
119
+ for (const entry of this.#operations.values())
120
+ void this.#query(entry.handle);
121
+ await this.selectWorkspace(workspaceId, selectedToken);
122
+ }
123
+ async authoring(workspaceId, signal, mode = 'create') {
124
+ return (await this.#call('authoring', { workspaceId, mode }, signal));
125
+ }
126
+ async createDatasource(workspaceId, schema, input) {
127
+ let result;
128
+ try {
129
+ result = (await this.#call('create-datasource', {
130
+ workspaceId,
131
+ generation: schema.generation,
132
+ fingerprint: schema.fingerprint,
133
+ ...input,
134
+ }));
135
+ }
136
+ catch (error) {
137
+ if (error instanceof CredentialResponseError &&
138
+ [
139
+ 'datasource-credential-ref-invalid',
140
+ 'datasource-already-exists',
141
+ 'context-changed',
142
+ ].includes(error.code))
143
+ throw error;
144
+ throw new CopyError(message('marivo.credentials.value-if-submission-is-unconfirmed-refresh-the-list-to', {
145
+ p0: error instanceof Error ? errorMessage(error) : 'marivo.credentials.create-failed',
146
+ }));
147
+ }
148
+ if (!this.#state.open || this.#state.workspaceId !== workspaceId)
149
+ return;
150
+ await this.selectWorkspace(workspaceId);
151
+ if (this.#state.workspaceId !== workspaceId)
152
+ return;
153
+ const created = this.#state.datasources.find((item) => item.name === result.name);
154
+ if (created)
155
+ this.select(created.token);
156
+ }
157
+ async configuration(workspaceId, name, signal) {
158
+ return (await this.#call('configuration', { workspaceId, name }, signal));
159
+ }
160
+ async saveConfiguration(workspaceId, schema, input, original, requestId, changes = {}) {
161
+ try {
162
+ let result;
163
+ try {
164
+ result = (await this.#call(original ? 'update-datasource' : 'create-datasource', {
165
+ workspaceId,
166
+ generation: schema.generation,
167
+ fingerprint: schema.fingerprint,
168
+ ...input,
169
+ ...(original ? { name: original.name, version: original.version } : {}),
170
+ ...(requestId ? { requestId } : {}),
171
+ }));
172
+ }
173
+ catch (error) {
174
+ if (error instanceof CredentialResponseError &&
175
+ [
176
+ 'datasource-credential-ref-invalid',
177
+ 'datasource-already-exists',
178
+ 'datasource-config-changed',
179
+ 'datasource-identity-fixed',
180
+ 'context-changed',
181
+ 'operation-busy',
182
+ ].includes(error.code))
183
+ throw error;
184
+ throw new CopyError(message('marivo.credentials.value-if-saving-is-unconfirmed-reload-the-configuration-to', {
185
+ p0: error instanceof Error ? errorMessage(error) : 'marivo.credentials.save-failed',
186
+ }));
187
+ }
188
+ if (!this.#state.open ||
189
+ this.#state.workspaceId !== workspaceId ||
190
+ (requestId && this.#state.requestId !== requestId))
191
+ return;
192
+ if (result.request) {
193
+ this.#acceptRequest(result.request);
194
+ const context = result.request.context;
195
+ if (context) {
196
+ this.#checkCredentialWrites(context, changes);
197
+ const ready = context.refs.every((ref) => context.credentials[ref]?.configured || changes[ref]);
198
+ if (ready || Object.keys(changes).length)
199
+ await this.start(context, 'submit', changes);
200
+ }
201
+ }
202
+ else {
203
+ await this.selectWorkspace(workspaceId);
204
+ if (this.#state.workspaceId !== workspaceId)
205
+ return;
206
+ const selected = this.#state.datasources.find((item) => item.name === result.name);
207
+ if (selected) {
208
+ this.select(selected.token);
209
+ this.#checkCredentialWrites(selected, changes);
210
+ if (Object.keys(changes).length)
211
+ await this.start(selected, 'save', changes);
212
+ }
213
+ }
214
+ }
215
+ finally {
216
+ for (const ref of Object.keys(changes))
217
+ delete changes[ref];
218
+ }
219
+ }
220
+ #checkCredentialWrites(context, changes) {
221
+ if (Object.keys(changes).some((ref) => context.credentials[ref]?.configured))
222
+ throw new Error('marivo.credentials.configuration-saved-but-the-reference-already-exists-existing-credentials');
223
+ }
224
+ #acceptRequest(request) {
225
+ this.#patch({
226
+ requests: [...this.#state.requests.filter((item) => item.id !== request.id), request],
227
+ });
228
+ }
229
+ async selectConfiguration(workspaceId, requestId, name) {
230
+ const request = (await this.#call('select-configuration', {
231
+ workspaceId,
232
+ requestId,
233
+ name,
234
+ }));
235
+ if (this.#state.workspaceId !== workspaceId ||
236
+ this.#state.requestId !== requestId ||
237
+ !this.#state.open)
238
+ return;
239
+ this.#acceptRequest(request);
240
+ }
241
+ close() {
242
+ this.#read?.abort();
243
+ this.#refresh?.abort();
244
+ this.#patch({ open: false, loading: false });
245
+ }
246
+ suspend() {
247
+ this.#suspended = true;
248
+ for (const controller of this.#polls.values())
249
+ controller.abort();
250
+ this.#polls.clear();
251
+ this.close();
252
+ }
253
+ openRequest(id) {
254
+ this.#read?.abort();
255
+ this.#refresh?.abort();
256
+ this.#patch({ open: true, requestId: id, loading: false });
257
+ }
258
+ select(token) {
259
+ this.#patch({ selected: token, requestId: '', error: '' });
260
+ }
261
+ async selectWorkspace(workspaceId, selectedToken) {
262
+ this.#read?.abort();
263
+ this.#refresh?.abort();
264
+ const flight = new AbortController();
265
+ this.#read = flight;
266
+ const selected = selectedToken ?? (this.#state.workspaceId === workspaceId ? this.#state.selected : '');
267
+ const activity = new Map([...Object.values(this.#state.outcomes), ...this.#operations.values()].map((entry) => [
268
+ entry.handle.scope,
269
+ entry.handle.id,
270
+ ]));
271
+ this.#patch({
272
+ workspaceId,
273
+ datasources: [],
274
+ selected: '',
275
+ requestId: '',
276
+ loading: !!workspaceId,
277
+ error: '',
278
+ });
279
+ if (!workspaceId)
280
+ return;
281
+ try {
282
+ const value = (await this.#call('overview', { workspaceId }, flight.signal));
283
+ if (flight.signal.aborted)
284
+ return;
285
+ this.#patch({
286
+ generation: value.generation,
287
+ datasources: value.datasources,
288
+ outcomes: this.#currentOutcomes(workspaceId, value.generation, value.datasources, activity),
289
+ selected: value.datasources.some((item) => item.token === selected)
290
+ ? selected
291
+ : (value.datasources[0]?.token ?? ''),
292
+ loading: false,
293
+ });
294
+ }
295
+ catch (error) {
296
+ if (!flight.signal.aborted)
297
+ this.#patch({
298
+ loading: false,
299
+ error: error instanceof Error
300
+ ? errorMessage(error)
301
+ : 'marivo.credentials.cannot-read-credential-status',
302
+ });
303
+ }
304
+ }
305
+ /** Receive the shared Host watch without sharing a tab's selection or form state. */
306
+ syncRequests(sessionId, state) {
307
+ const requests = state.requests.filter((request) => request.sessionId === sessionId);
308
+ const changed = JSON.stringify(requests) !== JSON.stringify(this.#state.requests) ||
309
+ state.generation !== this.#state.generation ||
310
+ state.revision !== this.#state.revision;
311
+ this.#patch({ sessionId, requests, generation: state.generation, revision: state.revision });
312
+ if (changed && this.#state.open)
313
+ void this.refreshDatasources();
314
+ }
315
+ async refreshDatasources() {
316
+ const workspaceId = this.#state.workspaceId;
317
+ if (!workspaceId || !this.#state.open)
318
+ return;
319
+ this.#refresh?.abort();
320
+ const flight = new AbortController();
321
+ this.#refresh = flight;
322
+ try {
323
+ const value = (await this.#call('overview', { workspaceId }, flight.signal));
324
+ if (!flight.signal.aborted && this.#state.open && this.#state.workspaceId === workspaceId) {
325
+ const name = this.#state.datasources.find((item) => item.token === this.#state.selected)?.name;
326
+ this.#patch({
327
+ datasources: value.datasources,
328
+ generation: value.generation,
329
+ selected: value.datasources.find((item) => item.name === name)?.token ??
330
+ value.datasources[0]?.token ??
331
+ '',
332
+ });
333
+ }
334
+ }
335
+ catch {
336
+ /* Keep the visible form; explicit refresh can surface read failures. */
337
+ }
338
+ }
339
+ session(sessionId) {
340
+ if (sessionId === this.#state.sessionId && this.#watch)
341
+ return;
342
+ this.#watch?.abort();
343
+ this.#read?.abort();
344
+ this.#refresh?.abort();
345
+ const controller = new AbortController();
346
+ this.#watch = controller;
347
+ this.#patch({ sessionId, requests: [], requestId: '', open: false });
348
+ if (sessionId)
349
+ void this.#watchTasks.track(this.#watchSession(sessionId, controller.signal));
350
+ }
351
+ async #watchSession(sessionId, caller) {
352
+ const lifetime = new AbortController();
353
+ const signal = AbortSignal.any([caller, lifetime.signal, this.#lifetime.signal]);
354
+ let feed;
355
+ let reading;
356
+ let pending;
357
+ const refresh = async () => {
358
+ let attempt = 0;
359
+ while (pending && !signal.aborted) {
360
+ const item = pending;
361
+ const readSignal = AbortSignal.any([signal, item.signal]);
362
+ if (readSignal.aborted) {
363
+ if (pending === item)
364
+ pending = undefined;
365
+ continue;
366
+ }
367
+ try {
368
+ // Never pass a cursor here: this is an immediate snapshot, not a long poll.
369
+ const value = (await this.#call('watch', { sessionId }, readSignal));
370
+ if (readSignal.aborted)
371
+ continue;
372
+ credentialRevisionSchema.parse({ generation: value.generation, cursor: value.cursor });
373
+ if (value.generation !== item.value.generation)
374
+ throw new Error('credential-generation-changed');
375
+ const requests = [...value.requests, ...(value.configurationRequests ?? [])];
376
+ this.#patch({
377
+ generation: value.generation,
378
+ revision: value.cursor,
379
+ requests,
380
+ ...([
381
+ 'marivo.credentials.credential-request-connection-interrupted-reconnecting-host-waits-remain-subject',
382
+ 'marivo.credentials.credential-request-notifications-are-unavailable-check-the-host-connection',
383
+ ].includes(String(this.#state.error))
384
+ ? { error: '' }
385
+ : {}),
386
+ });
387
+ item.accept();
388
+ if (pending === item ||
389
+ (pending?.signal === item.signal && pending.value.cursor === value.cursor)) {
390
+ pending?.accept();
391
+ pending = undefined;
392
+ }
393
+ attempt = 0;
394
+ const request = requests.find((r) => r.endedAt === undefined && !this.#opened.has(r.id));
395
+ if (request) {
396
+ this.#opened.add(request.id);
397
+ this.openRequest(request.id);
398
+ }
399
+ }
400
+ catch {
401
+ if (!readSignal.aborted) {
402
+ this.#patch({
403
+ error: 'marivo.credentials.credential-request-connection-interrupted-reconnecting-host-waits-remain-subject',
404
+ });
405
+ await delay([1000, 2000, 5000][Math.min(attempt++, 2)], readSignal);
406
+ }
407
+ }
408
+ }
409
+ };
410
+ const startRead = () => {
411
+ if (reading || !pending || signal.aborted)
412
+ return;
413
+ reading = refresh().finally(() => {
414
+ reading = undefined;
415
+ startRead();
416
+ });
417
+ };
418
+ try {
419
+ if (!this.#changes)
420
+ throw new Error('credential-notification-unavailable');
421
+ feed = this.#changes(sessionId);
422
+ const stop = () => {
423
+ void feed?.dispose().catch(() => { });
424
+ };
425
+ signal.addEventListener('abort', stop, { once: true });
426
+ try {
427
+ for await (const item of feed) {
428
+ if (signal.aborted)
429
+ break;
430
+ credentialRevisionSchema.parse(item.value);
431
+ pending = item;
432
+ startRead();
433
+ }
434
+ }
435
+ finally {
436
+ signal.removeEventListener('abort', stop);
437
+ }
438
+ }
439
+ catch {
440
+ if (!signal.aborted)
441
+ this.#patch({
442
+ error: 'marivo.credentials.credential-request-notifications-are-unavailable-check-the-host-connection',
443
+ });
444
+ }
445
+ finally {
446
+ lifetime.abort();
447
+ await feed?.dispose().catch(() => { });
448
+ await reading;
449
+ }
450
+ }
451
+ #persist() {
452
+ try {
453
+ const pending = [...this.#operations.values()]
454
+ .filter((entry) => !entry.error && (!entry.operation || entry.operation.status === 'running'))
455
+ .map(({ handle, name, workspaceId }) => ({ handle, name, workspaceId }));
456
+ if (pending.length)
457
+ this.#storage?.setItem(STORAGE_KEY, JSON.stringify(pending));
458
+ else
459
+ this.#storage?.removeItem(STORAGE_KEY);
460
+ }
461
+ catch {
462
+ /* in-memory queries still work */
463
+ }
464
+ }
465
+ #visible(scope) {
466
+ const selected = this.#state.requestId
467
+ ? this.#state.requests.find((request) => request.id === this.#state.requestId)?.context?.token
468
+ : this.#state.selected;
469
+ return selected === scope;
470
+ }
471
+ #currentOutcomes(workspaceId, generation, datasources, activity) {
472
+ const scopes = new Set(datasources.map((context) => context.token));
473
+ return Object.fromEntries(Object.entries(this.#state.outcomes)
474
+ .filter(([scope, entry]) => entry.handle.generation === generation &&
475
+ (entry.workspaceId !== workspaceId ||
476
+ scopes.has(scope) ||
477
+ entry.operation?.action === 'delete-datasource'))
478
+ .map(([scope, entry]) => [
479
+ scope,
480
+ scopes.has(scope) && activity.get(scope) === entry.handle.id
481
+ ? { ...entry, overviewUpdated: true }
482
+ : entry,
483
+ ]));
484
+ }
485
+ #activity(scope) {
486
+ return ([...this.#operations.values()].find((entry) => entry.handle.scope === scope)?.handle.id ??
487
+ this.#state.outcomes[scope]?.handle.id);
488
+ }
489
+ async #refreshOverview(handle) {
490
+ const { workspaceId, generation, datasources, open, requestId } = this.#state;
491
+ if (!open ||
492
+ requestId ||
493
+ handle.generation !== generation ||
494
+ !datasources.some((context) => context.token === handle.scope))
495
+ return;
496
+ this.#refresh?.abort();
497
+ const flight = new AbortController(), read = this.#read, activity = new Map(datasources.map((context) => [context.token, this.#activity(context.token)]));
498
+ this.#refresh = flight;
499
+ try {
500
+ const value = (await this.#call('overview', { workspaceId }, flight.signal));
501
+ if (flight.signal.aborted ||
502
+ read !== this.#read ||
503
+ workspaceId !== this.#state.workspaceId ||
504
+ generation !== this.#state.generation ||
505
+ value.generation !== generation)
506
+ return;
507
+ const current = new Map(this.#state.datasources.map((context) => [context.token, context]));
508
+ const updated = value.datasources.map((context) => current.has(context.token) && activity.get(context.token) !== this.#activity(context.token)
509
+ ? current.get(context.token)
510
+ : context);
511
+ this.#patch({
512
+ datasources: updated,
513
+ outcomes: this.#currentOutcomes(workspaceId, generation, updated, activity),
514
+ selected: updated.some((context) => context.token === this.#state.selected)
515
+ ? this.#state.selected
516
+ : (updated[0]?.token ?? ''),
517
+ });
518
+ }
519
+ catch {
520
+ // Keep the last authoritative overview and scoped outcome if a passive refresh fails.
521
+ }
522
+ }
523
+ #publish(handle, operation) {
524
+ const entry = this.#operations.get(handle.id);
525
+ if (!entry)
526
+ return;
527
+ if (operation.status !== 'running') {
528
+ this.#settle({ ...entry, operation });
529
+ return;
530
+ }
531
+ this.#operations.set(handle.id, { ...entry, operation });
532
+ this.#patch({
533
+ operations: [...this.#operations.values()],
534
+ ...(this.#state.handle?.id === handle.id
535
+ ? { operation, ...(this.#visible(handle.scope) ? { error: '' } : {}) }
536
+ : {}),
537
+ });
538
+ this.#persist();
539
+ }
540
+ #settle(entry) {
541
+ this.#operations.delete(entry.handle.id);
542
+ this.#patch({
543
+ operations: [...this.#operations.values()],
544
+ outcomes: { ...this.#state.outcomes, [entry.handle.scope]: entry },
545
+ ...(this.#state.handle?.id === entry.handle.id
546
+ ? {
547
+ handle: undefined,
548
+ operation: undefined,
549
+ ...(this.#visible(entry.handle.scope) ? { error: entry.error ?? '' } : {}),
550
+ }
551
+ : {}),
552
+ });
553
+ this.#persist();
554
+ }
555
+ selectOperation(id) {
556
+ const entry = this.#operations.get(id);
557
+ if (entry)
558
+ this.#patch({ handle: entry.handle, operation: entry.operation, error: entry.error ?? '' });
559
+ }
560
+ dismissOutcome(scope) {
561
+ const outcomes = { ...this.#state.outcomes };
562
+ delete outcomes[scope];
563
+ this.#patch({ outcomes });
564
+ }
565
+ async start(context, action, changes = {}, reference, deleteCredentials) {
566
+ if ([...this.#operations.values()].some((entry) => entry.handle.scope === context.token &&
567
+ !entry.error &&
568
+ (!entry.operation || entry.operation.status === 'running'))) {
569
+ for (const ref of Object.keys(changes))
570
+ delete changes[ref];
571
+ this.#patch({ error: credentialMessage('operation-busy') });
572
+ return;
573
+ }
574
+ const outcomes = { ...this.#state.outcomes };
575
+ delete outcomes[context.token];
576
+ const handle = {
577
+ generation: this.#state.generation,
578
+ id: crypto.randomUUID(),
579
+ scope: context.token,
580
+ };
581
+ const requestId = action === 'submit' || action === 'diagnose' ? this.#state.requestId : undefined;
582
+ const operation = {
583
+ id: handle.id,
584
+ scope: handle.scope,
585
+ action,
586
+ status: 'running',
587
+ phase: 'saving',
588
+ saved: [],
589
+ errors: [],
590
+ };
591
+ this.#operations.set(handle.id, {
592
+ handle,
593
+ name: context.name,
594
+ workspaceId: context.workspaceId,
595
+ operation,
596
+ });
597
+ this.#patch({
598
+ handle,
599
+ operation,
600
+ operations: [...this.#operations.values()],
601
+ outcomes,
602
+ error: '',
603
+ });
604
+ this.#persist();
605
+ try {
606
+ // Only operation queries publish authoritative progress. A late submit reply cannot
607
+ // overwrite another operation or an already newer query result for this ID.
608
+ await this.#call('start', {
609
+ ...handle,
610
+ action,
611
+ version: context.version,
612
+ ...(requestId ? { requestId } : {}),
613
+ changes,
614
+ ...(reference ? { reference } : {}),
615
+ ...(action === 'delete-datasource'
616
+ ? { deleteCredentials: deleteCredentials === true }
617
+ : {}),
618
+ });
619
+ }
620
+ catch (error) {
621
+ if (action === 'delete-datasource' && error instanceof CredentialResponseError) {
622
+ const entry = this.#operations.get(handle.id);
623
+ if (entry)
624
+ this.#settle({
625
+ ...entry,
626
+ operation: {
627
+ ...operation,
628
+ status: 'failed',
629
+ phase: 'settled',
630
+ errors: [error.code],
631
+ },
632
+ });
633
+ await this.#refreshOverview(handle);
634
+ return;
635
+ }
636
+ if (this.#state.handle?.id === handle.id && this.#visible(handle.scope))
637
+ this.#patch({
638
+ error: 'marivo.credentials.submission-response-unconfirmed-checking-operation-status-without-resending-secret',
639
+ });
640
+ }
641
+ finally {
642
+ for (const ref of Object.keys(changes))
643
+ delete changes[ref];
644
+ }
645
+ await this.#query(handle);
646
+ }
647
+ async #query(handle) {
648
+ const entry = this.#operations.get(handle.id);
649
+ if (this.#suspended ||
650
+ !entry ||
651
+ entry.error ||
652
+ this.#polls.has(handle.id) ||
653
+ (entry.operation && entry.operation.status !== 'running'))
654
+ return;
655
+ const controller = new AbortController();
656
+ this.#polls.set(handle.id, controller);
657
+ const signal = AbortSignal.any([controller.signal, this.#lifetime.signal]);
658
+ try {
659
+ while (!signal.aborted) {
660
+ try {
661
+ const operation = (await this.#call('operation', handle, signal));
662
+ if (signal.aborted)
663
+ return;
664
+ if (!operation) {
665
+ const error = entry.operation?.action === 'delete-datasource'
666
+ ? 'marivo.credentials.deletion-status-cannot-be-recovered-some-deletion-may-have'
667
+ : 'marivo.credentials.operation-status-cannot-be-recovered-saving-may-have-occurred';
668
+ this.#settle({ ...entry, operation: undefined, error });
669
+ return;
670
+ }
671
+ this.#publish(handle, operation);
672
+ if (operation.status !== 'running') {
673
+ await this.#refreshOverview(handle);
674
+ return;
675
+ }
676
+ }
677
+ catch {
678
+ if (!signal.aborted &&
679
+ this.#state.handle?.id === handle.id &&
680
+ this.#visible(handle.scope))
681
+ this.#patch({
682
+ error: 'marivo.credentials.recovering-operation-results-submitted-saves-will-not-be-resent',
683
+ });
684
+ }
685
+ await delay(1000, signal);
686
+ }
687
+ }
688
+ finally {
689
+ if (this.#polls.get(handle.id) === controller)
690
+ this.#polls.delete(handle.id);
691
+ }
692
+ }
693
+ recover() {
694
+ try {
695
+ const raw = this.#storage?.getItem(STORAGE_KEY);
696
+ if (!raw)
697
+ return;
698
+ const entries = JSON.parse(raw);
699
+ if (!Array.isArray(entries))
700
+ return;
701
+ for (const entry of entries) {
702
+ const handle = entry?.handle;
703
+ if (!handle ||
704
+ ![handle.generation, handle.id, handle.scope].every((v) => typeof v === 'string' && /^[a-f0-9-]{36}$/i.test(v)) ||
705
+ typeof entry.name !== 'string')
706
+ continue;
707
+ if (!this.#operations.has(handle.id))
708
+ this.#operations.set(handle.id, {
709
+ handle,
710
+ name: entry.name,
711
+ ...(typeof entry.workspaceId === 'string' ? { workspaceId: entry.workspaceId } : {}),
712
+ });
713
+ if (!this.#state.handle)
714
+ this.selectOperation(handle.id);
715
+ void this.#query(handle);
716
+ }
717
+ this.#patch({ operations: [...this.#operations.values()] });
718
+ }
719
+ catch {
720
+ /* invalid or unavailable local handles are not replayed */
721
+ }
722
+ }
723
+ async cancelRequest(id) {
724
+ try {
725
+ await this.#call('cancel-request', { requestId: id });
726
+ }
727
+ catch (error) {
728
+ this.#patch({
729
+ error: error instanceof Error ? errorMessage(error) : 'marivo.credentials.the-call-has-ended',
730
+ });
731
+ }
732
+ }
733
+ async cancelOperation(scope) {
734
+ const handle = scope
735
+ ? [...this.#operations.values()].find((entry) => entry.handle.scope === scope &&
736
+ !entry.error &&
737
+ (!entry.operation || entry.operation.status === 'running'))?.handle
738
+ : this.#state.handle;
739
+ if (!handle)
740
+ return;
741
+ try {
742
+ await this.#call('cancel-operation', handle);
743
+ }
744
+ catch {
745
+ this.#patch({
746
+ error: 'marivo.credentials.cancellation-is-unconfirmed-continue-checking-status',
747
+ });
748
+ }
749
+ }
750
+ reset() {
751
+ this.#watch?.abort();
752
+ this.#watch = undefined;
753
+ this.session(this.#state.sessionId);
754
+ this.recover();
755
+ }
756
+ dispose() {
757
+ this.#lifetime.abort();
758
+ this.#watch?.abort();
759
+ this.#read?.abort();
760
+ this.#refresh?.abort();
761
+ for (const controller of this.#polls.values())
762
+ controller.abort();
763
+ this.#listeners.clear();
764
+ return this.#watchTasks.drain();
765
+ }
766
+ }