@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.
- package/README.md +139 -53
- package/cordis.patch.yml +1 -1
- package/lib/bin/environment.js +9 -7
- package/lib/bin/presentation-lint.js +21 -0
- package/lib/bridges.js +2 -2
- package/lib/client/credentials/changes.js +74 -0
- package/lib/client/credentials/create-datasource.js +171 -0
- package/lib/client/credentials/credential-draft.js +42 -0
- package/lib/client/credentials/defaults.js +11 -0
- package/lib/client/credentials/descriptions.js +46 -0
- package/lib/client/credentials/install.js +319 -0
- package/lib/client/credentials/model.js +766 -0
- package/lib/client/credentials/report-publishing.js +96 -0
- package/lib/client/credentials/styles.js +21 -0
- package/lib/client/i18n/context.js +22 -0
- package/lib/client/i18n/copy.js +57 -0
- package/lib/client/i18n/credentials.js +475 -0
- package/lib/client/i18n/host.js +27 -0
- package/lib/client/i18n/navigation.js +41 -0
- package/lib/client/i18n/presentation.js +577 -0
- package/lib/client/i18n/python.js +36 -0
- package/lib/client/i18n/semantic.js +441 -0
- package/lib/client/input-context.js +21 -0
- package/lib/client/python-tool/card.js +35 -0
- package/lib/client/python-tool/execution-error.js +35 -0
- package/lib/client/python-tool/install.js +9 -0
- package/lib/client/python-tool/model.js +101 -0
- package/lib/client/right-tabs/install.js +535 -0
- package/lib/client/right-tabs/live-delivery.js +58 -0
- package/lib/client/right-tabs/navigation.js +47 -0
- package/lib/client/right-tabs/page.js +178 -0
- package/lib/client/right-tabs/styles.js +11 -0
- package/lib/client/rpc.js +5 -0
- package/lib/client/semantic-reference.js +10 -0
- package/lib/client/workspace-header-action.js +24 -0
- package/lib/client/workspace-shortcuts.js +26 -0
- package/lib/client.js +1250 -730
- package/lib/compatibility.js +2 -2
- package/lib/datasource/authoring.js +109 -0
- package/lib/datasource/bridge-programs.js +42 -27
- package/lib/datasource/bridge.js +144 -9
- package/lib/datasource/changes-contract.js +41 -0
- package/lib/datasource/changes.js +49 -0
- package/lib/datasource/configure.js +39 -0
- package/lib/datasource/defaults.js +25 -0
- package/lib/datasource/index.js +4 -2
- package/lib/datasource/python-options.js +21 -0
- package/lib/datasource/python.js +206 -0
- package/lib/datasource/resolver-program.js +112 -0
- package/lib/datasource/rpc.js +150 -0
- package/lib/datasource/service.js +962 -0
- package/lib/datasource/shell-env.js +2 -333
- package/lib/datasource/test.js +25 -51
- package/lib/disclosure/activation.js +19 -7
- package/lib/disclosure/execution-guidance.js +52 -0
- package/lib/disclosure/help.js +4 -1
- package/lib/environment/binding.js +54 -9
- package/lib/environment/runtime.js +120 -108
- package/lib/environment/subprocess.js +6 -1
- package/lib/environment/summary.js +3 -3
- package/lib/environment/workspace.js +23 -1
- package/lib/index.js +1 -1
- package/lib/lifecycle.js +33 -0
- package/lib/plugin-agents.js +154 -0
- package/lib/plugin.js +172 -133
- package/lib/presentation/asset.js +34 -0
- package/lib/presentation/assets/portable.js +606 -0
- package/lib/presentation/assets/static.js +1216 -0
- package/lib/presentation/build/index.js +72 -0
- package/lib/presentation/commit.js +163 -0
- package/lib/presentation/contracts/asset-path.js +9 -0
- package/lib/presentation/contracts/catalog.js +62 -0
- package/lib/presentation/contracts/charts.js +453 -0
- package/lib/presentation/contracts/editing.js +58 -0
- package/lib/presentation/contracts/index.js +663 -0
- package/lib/presentation/contracts/interaction.js +62 -0
- package/lib/presentation/contracts/types.js +15 -0
- package/lib/presentation/contracts/values.js +28 -0
- package/lib/presentation/delivery.js +71 -0
- package/lib/presentation/export-html.js +214 -0
- package/lib/presentation/files.js +82 -0
- package/lib/presentation/index.js +6 -0
- package/lib/presentation/lint.js +46 -0
- package/lib/presentation/projection/files.js +85 -0
- package/lib/presentation/projection/index.js +196 -0
- package/lib/presentation/projection/program.js +137 -0
- package/lib/presentation/projection/sql-code.js +79 -0
- package/lib/presentation/receipt.js +34 -0
- package/lib/presentation/reports.js +219 -0
- package/lib/presentation/rpc.js +186 -0
- package/lib/presentation/tool.js +118 -0
- package/lib/python-execution-contracts.js +1 -0
- package/lib/python-execution.js +232 -0
- package/lib/report-publishing/adapters.js +111 -0
- package/lib/report-publishing/config.js +69 -0
- package/lib/report-publishing/service.js +180 -0
- package/lib/rpc.js +49 -0
- package/lib/semantic-browser/contracts.js +97 -0
- package/lib/semantic-browser/definition.js +64 -0
- package/lib/semantic-browser/program.js +129 -0
- package/lib/semantic-browser/service.js +106 -0
- package/lib/semantic-reference/bridge.js +127 -0
- package/lib/semantic-reference/contracts.js +127 -0
- package/lib/semantic-reference/environment.js +56 -0
- package/lib/semantic-reference/index.js +3 -0
- package/lib/semantic-reference/labels.js +16 -0
- package/lib/semantic-reference/rpc.js +143 -0
- package/lib/semantic-reference/search.js +95 -0
- package/lib/semantic-reference/usage.js +152 -0
- package/lib/tool-lifecycle.js +22 -0
- package/lib/types/bridges.d.ts +2 -2
- package/lib/types/client/credentials/changes.d.ts +9 -0
- package/lib/types/client/credentials/create-datasource.d.ts +7 -0
- package/lib/types/client/credentials/credential-draft.d.ts +14 -0
- package/lib/types/client/credentials/defaults.d.ts +2 -0
- package/lib/types/client/credentials/descriptions.d.ts +1 -0
- package/lib/types/client/credentials/install.d.ts +19 -0
- package/lib/types/client/credentials/model.d.ts +79 -0
- package/lib/types/client/credentials/report-publishing.d.ts +5 -0
- package/lib/types/client/credentials/styles.d.ts +1 -0
- package/lib/types/client/i18n/context.d.ts +17 -0
- package/lib/types/client/i18n/copy.d.ts +806 -0
- package/lib/types/client/i18n/credentials.d.ts +475 -0
- package/lib/types/client/i18n/host.d.ts +16 -0
- package/lib/types/client/i18n/navigation.d.ts +41 -0
- package/lib/types/client/i18n/presentation.d.ts +577 -0
- package/lib/types/client/i18n/python.d.ts +19 -0
- package/lib/types/client/i18n/semantic.d.ts +441 -0
- package/lib/types/client/input-context.d.ts +19 -0
- package/lib/types/client/presentation/ask-dsh.d.ts +5 -0
- package/lib/types/client/presentation/catalog-model.d.ts +24 -0
- package/lib/types/client/presentation/catalog.d.ts +20 -0
- package/lib/types/client/presentation/chart-explorer.d.ts +12 -0
- package/lib/types/client/presentation/chart-geometry.d.ts +30 -0
- package/lib/types/client/presentation/chart-renderer.d.ts +10 -0
- package/lib/types/client/presentation/chart-specials.d.ts +10 -0
- package/lib/types/client/presentation/chart-styles.d.ts +1 -0
- package/lib/types/client/presentation/chart-tooltip.d.ts +13 -0
- package/lib/types/client/presentation/chart-view.d.ts +23 -0
- package/lib/types/client/presentation/context-reference.d.ts +12 -0
- package/lib/types/client/presentation/copy-context.d.ts +5 -0
- package/lib/types/client/presentation/delivery-model.d.ts +75 -0
- package/lib/types/client/presentation/delivery.d.ts +78 -0
- package/lib/types/client/presentation/download.d.ts +2 -0
- package/lib/types/client/presentation/editor-controls.d.ts +12 -0
- package/lib/types/client/presentation/export-menu.d.ts +26 -0
- package/lib/types/client/presentation/export-view.d.ts +63 -0
- package/lib/types/client/presentation/global-filters.d.ts +6 -0
- package/lib/types/client/presentation/host-entry.d.ts +18 -0
- package/lib/types/client/presentation/icons.d.ts +4 -0
- package/lib/types/client/presentation/markdown.d.ts +5 -0
- package/lib/types/client/presentation/model.d.ts +61 -0
- package/lib/types/client/presentation/reader.d.ts +24 -0
- package/lib/types/client/presentation/reference-source.d.ts +9 -0
- package/lib/types/client/presentation/source-code-model.d.ts +17 -0
- package/lib/types/client/presentation/source-code.d.ts +6 -0
- package/lib/types/client/presentation/source-dialog.d.ts +12 -0
- package/lib/types/client/presentation/source-facts.d.ts +20 -0
- package/lib/types/client/presentation/source-format.d.ts +5 -0
- package/lib/types/client/presentation/source-highlight.d.ts +7 -0
- package/lib/types/client/presentation/sources.d.ts +13 -0
- package/lib/types/client/presentation/styles.d.ts +1 -0
- package/lib/types/client/presentation/table.d.ts +16 -0
- package/lib/types/client/presentation/use-element-width.d.ts +5 -0
- package/lib/types/client/python-tool/card.d.ts +2 -0
- package/lib/types/client/python-tool/execution-error.d.ts +3 -0
- package/lib/types/client/python-tool/install.d.ts +2 -0
- package/lib/types/client/python-tool/model.d.ts +20 -0
- package/lib/types/client/right-tabs/install.d.ts +18 -0
- package/lib/types/client/right-tabs/live-delivery.d.ts +8 -0
- package/lib/types/client/right-tabs/navigation.d.ts +17 -0
- package/lib/types/client/right-tabs/page.d.ts +36 -0
- package/lib/types/client/right-tabs/styles.d.ts +2 -0
- package/lib/types/client/rpc.d.ts +3 -0
- package/lib/types/client/semantic-reference.d.ts +4 -0
- package/lib/types/client/workspace-header-action.d.ts +7 -0
- package/lib/types/client/workspace-shortcuts.d.ts +15 -0
- package/lib/types/client.d.ts +9 -124
- package/lib/types/compatibility.d.ts +3 -3
- package/lib/types/datasource/authoring.d.ts +35 -0
- package/lib/types/datasource/bridge.d.ts +43 -4
- package/lib/types/datasource/changes-contract.d.ts +18 -0
- package/lib/types/datasource/changes.d.ts +15 -0
- package/lib/types/datasource/configure.d.ts +7 -0
- package/lib/types/datasource/defaults.d.ts +7 -0
- package/lib/types/datasource/index.d.ts +8 -5
- package/lib/types/datasource/python-options.d.ts +9 -0
- package/lib/types/datasource/python.d.ts +19 -0
- package/lib/types/datasource/resolver-program.d.ts +6 -0
- package/lib/types/datasource/rpc.d.ts +5 -0
- package/lib/types/datasource/service.d.ts +155 -0
- package/lib/types/datasource/shell-env.d.ts +0 -29
- package/lib/types/datasource/test.d.ts +6 -29
- package/lib/types/disclosure/activation.d.ts +2 -1
- package/lib/types/disclosure/execution-guidance.d.ts +6 -0
- package/lib/types/disclosure/help.d.ts +1 -1
- package/lib/types/environment/binding.d.ts +5 -1
- package/lib/types/environment/errors.d.ts +1 -1
- package/lib/types/environment/runtime.d.ts +5 -5
- package/lib/types/environment/types.d.ts +15 -3
- package/lib/types/environment/workspace.d.ts +1 -0
- package/lib/types/index.d.ts +1 -1
- package/lib/types/lifecycle.d.ts +8 -0
- package/lib/types/plugin-agents.d.ts +27 -0
- package/lib/types/plugin.d.ts +14 -24
- package/lib/types/presentation/asset.d.ts +6 -0
- package/lib/types/presentation/build/index.d.ts +16 -0
- package/lib/types/presentation/commit.d.ts +4 -0
- package/lib/types/presentation/contracts/asset-path.d.ts +2 -0
- package/lib/types/presentation/contracts/catalog.d.ts +27 -0
- package/lib/types/presentation/contracts/charts.d.ts +11 -0
- package/lib/types/presentation/contracts/editing.d.ts +8 -0
- package/lib/types/presentation/contracts/index.d.ts +11 -0
- package/lib/types/presentation/contracts/interaction.d.ts +8 -0
- package/lib/types/presentation/contracts/types.d.ts +255 -0
- package/lib/types/presentation/contracts/values.d.ts +9 -0
- package/lib/types/presentation/delivery.d.ts +3 -0
- package/lib/types/presentation/export-html.d.ts +13 -0
- package/lib/types/presentation/files.d.ts +7 -0
- package/lib/types/presentation/index.d.ts +6 -0
- package/lib/types/presentation/lint.d.ts +13 -0
- package/lib/types/presentation/projection/files.d.ts +2 -0
- package/lib/types/presentation/projection/index.d.ts +20 -0
- package/lib/types/presentation/projection/program.d.ts +2 -0
- package/lib/types/presentation/projection/sql-code.d.ts +2 -0
- package/lib/types/presentation/receipt.d.ts +14 -0
- package/lib/types/presentation/reports.d.ts +10 -0
- package/lib/types/presentation/rpc.d.ts +25 -0
- package/lib/types/presentation/tool.d.ts +12 -0
- package/lib/types/python-execution-contracts.d.ts +10 -0
- package/lib/types/python-execution.d.ts +10 -0
- package/lib/types/report-publishing/adapters.d.ts +6 -0
- package/lib/types/report-publishing/config.d.ts +22 -0
- package/lib/types/report-publishing/service.d.ts +68 -0
- package/lib/types/rpc.d.ts +3 -0
- package/lib/types/semantic-browser/contracts.d.ts +47 -0
- package/lib/types/semantic-browser/definition.d.ts +20 -0
- package/lib/types/semantic-browser/program.d.ts +2 -0
- package/lib/types/semantic-browser/service.d.ts +26 -0
- package/lib/types/semantic-reference/bridge.d.ts +12 -0
- package/lib/types/semantic-reference/contracts.d.ts +54 -0
- package/lib/types/semantic-reference/environment.d.ts +22 -0
- package/lib/types/semantic-reference/index.d.ts +3 -0
- package/lib/types/semantic-reference/labels.d.ts +16 -0
- package/lib/types/semantic-reference/rpc.d.ts +15 -0
- package/lib/types/semantic-reference/search.d.ts +10 -0
- package/lib/types/semantic-reference/usage.d.ts +43 -0
- package/lib/types/tool-lifecycle.d.ts +4 -0
- package/lib/types/workspace-identity.d.ts +6 -0
- package/lib/workspace-identity.js +9 -0
- package/package.json +99 -53
- package/python/presentation-kit/dist/dsh_data_analysis_presentation_kit-1.1.0-py3-none-any.whl +0 -0
- package/skills/dsh-data-analysis-files/SKILL.md +32 -0
- package/skills/dsh-data-analysis-files/references/examples.md +93 -0
- package/skills/dsh-data-analysis-presentation/SKILL.md +28 -0
- package/skills/dsh-data-analysis-presentation/references/chart-examples.md +18 -0
- package/skills/dsh-data-analysis-presentation/references/charts.md +55 -0
- package/skills/dsh-data-analysis-presentation/references/examples/artifact.draft.json +36 -0
- package/skills/dsh-data-analysis-presentation/references/examples/charts-bins.dataset.json +33 -0
- package/skills/dsh-data-analysis-presentation/references/examples/charts-boxes.dataset.json +45 -0
- package/skills/dsh-data-analysis-presentation/references/examples/charts-composition.dataset.json +33 -0
- package/skills/dsh-data-analysis-presentation/references/examples/charts-funnel.dataset.json +27 -0
- package/skills/dsh-data-analysis-presentation/references/examples/charts-precision.dataset.json +21 -0
- package/skills/dsh-data-analysis-presentation/references/examples/charts-series.dataset.json +58 -0
- package/skills/dsh-data-analysis-presentation/references/examples/charts-shares.dataset.json +33 -0
- package/skills/dsh-data-analysis-presentation/references/examples/charts-waterfall.dataset.json +44 -0
- package/skills/dsh-data-analysis-presentation/references/examples/charts.draft.json +393 -0
- package/skills/dsh-data-analysis-presentation/references/examples/comparison-selected.dataset.json +24 -0
- package/skills/dsh-data-analysis-presentation/references/examples/comparison-summary.dataset.json +22 -0
- package/skills/dsh-data-analysis-presentation/references/examples/comparison.dataset.json +18 -0
- package/skills/dsh-data-analysis-presentation/references/examples/comparison.draft.json +80 -0
- package/skills/dsh-data-analysis-presentation/references/examples/computed-sources.draft.json +42 -0
- package/skills/dsh-data-analysis-presentation/references/examples/computed.dataset.json +12 -0
- package/skills/dsh-data-analysis-presentation/references/examples/computed.draft.json +58 -0
- package/skills/dsh-data-analysis-presentation/references/examples/interaction-detail.dataset.json +30 -0
- package/skills/dsh-data-analysis-presentation/references/examples/interaction-summary.dataset.json +22 -0
- package/skills/dsh-data-analysis-presentation/references/examples/interaction.draft.json +279 -0
- package/skills/dsh-data-analysis-presentation/references/examples/source-only.draft.json +27 -0
- package/skills/dsh-data-analysis-presentation/references/examples/write-charts.py +73 -0
- package/skills/dsh-data-analysis-presentation/references/examples/write-comparison.py +42 -0
- package/skills/dsh-data-analysis-presentation/references/examples/write-computed.py +21 -0
- package/skills/dsh-data-analysis-presentation/references/examples/write-interaction.py +33 -0
- package/skills/dsh-data-analysis-presentation/references/examples.md +44 -0
- package/skills/dsh-data-analysis-presentation/references/export-html.md +7 -0
- package/skills/dsh-data-analysis-presentation/references/interaction.md +56 -0
- package/skills/dsh-data-analysis-presentation/references/narrative.md +37 -0
- package/skills/dsh-data-analysis-presentation/references/publishing.md +3 -0
- package/skills/dsh-data-analysis-presentation/references/schema.md +162 -0
- package/lib/datasource/access.js +0 -60
- package/lib/datasource/credentials.js +0 -24
- package/lib/evidence/bridge-program.js +0 -153
- package/lib/evidence/bridge.js +0 -166
- package/lib/evidence/index.js +0 -2
- package/lib/evidence/sources.js +0 -263
- package/lib/types/datasource/access.d.ts +0 -27
- package/lib/types/evidence/bridge.d.ts +0 -40
- package/lib/types/evidence/index.d.ts +0 -4
- package/lib/types/evidence/sources.d.ts +0 -57
- package/python/report-kit/dist/dsh_data_analysis_report_kit-3.0.0-py3-none-any.whl +0 -0
- package/skills/dsh-data-analysis-report/SKILL.md +0 -57
- package/skills/dsh-data-analysis-report/assets/marivo-artifact.js +0 -113
- package/skills/dsh-data-analysis-report/assets/marivo-session-dag.js +0 -1201
- 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
|
+
}
|