@dsh-workbench/shared 0.3.10 → 1.0.0-beta.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/LICENSE +21 -0
- package/README.md +97 -0
- package/dist/api/blocker-table.d.ts +3 -3
- package/dist/api/blocker-table.js +47 -23
- package/dist/api/dsh.d.ts +19 -1
- package/dist/api/host-write.d.ts +0 -15
- package/dist/api/host-write.js +1 -35
- package/dist/api/workbench-client.js +2 -1
- package/dist/components/Activity.css +0 -1
- package/dist/components/ApprovalDetail.css +1 -2
- package/dist/components/ApprovalDetail.d.ts +1 -1
- package/dist/components/ApprovalPlan.css +0 -1
- package/dist/components/Card.css +0 -1
- package/dist/components/Card.d.ts +1 -1
- package/dist/components/CardBoundary.d.ts +1 -1
- package/dist/components/CardHead.css +0 -1
- package/dist/components/CardHead.d.ts +1 -1
- package/dist/components/ChoiceGroup.css +1 -2
- package/dist/components/ConfirmBar.d.ts +1 -1
- package/dist/components/ConfirmBar.js +1 -1
- package/dist/components/Form.css +1 -1
- package/dist/components/FormActions.js +1 -1
- package/dist/components/LongTaskRow.css +1 -2
- package/dist/components/LongTaskRow.d.ts +1 -1
- package/dist/components/LongTaskRow.js +1 -1
- package/dist/components/Note.css +0 -1
- package/dist/components/ResourceStates.css +0 -1
- package/dist/components/SettingsSection.css +0 -1
- package/dist/components/Spinner.css +0 -1
- package/dist/components/Stack.css +0 -1
- package/dist/components/StateBlock.css +0 -1
- package/dist/components/board-module.d.ts +3 -3
- package/dist/components/icons.d.ts +1 -7
- package/dist/components/icons.js +11 -16
- package/dist/config/constants.d.ts +2 -2
- package/dist/config/constants.js +2 -2
- package/dist/config/storage.d.ts +0 -4
- package/dist/config/storage.js +0 -11
- package/dist/contract/adapters.d.ts +10 -0
- package/dist/contract/cli.d.ts +3 -0
- package/dist/contract/data.d.ts +126 -0
- package/dist/{api → contract}/facts.d.ts +12 -3
- package/dist/{api → contract}/facts.js +19 -1
- package/dist/contract/needs.d.ts +132 -0
- package/dist/contract/needs.js +232 -0
- package/dist/contract/plugin-source.d.ts +1 -0
- package/dist/contract/plugin-source.js +1 -0
- package/dist/contract/protocol/bundle-install.d.ts +136 -0
- package/dist/contract/protocol/bundle-install.js +12 -0
- package/dist/contract/protocol/config-sync.d.ts +55 -0
- package/dist/contract/protocol/config-sync.js +2 -0
- package/dist/contract/protocol/diy.d.ts +4 -0
- package/dist/contract/protocol/diy.js +4 -0
- package/dist/contract/protocol/dsh-plugins.d.ts +11 -0
- package/dist/contract/protocol/dsh-plugins.js +1 -0
- package/dist/contract/protocol/long-tasks.d.ts +13 -0
- package/dist/contract/protocol/long-tasks.js +1 -0
- package/dist/contract/protocol/market-index.d.ts +195 -0
- package/dist/contract/protocol/market-index.js +7 -0
- package/dist/contract/protocol/platform.d.ts +44 -0
- package/dist/contract/protocol/platform.js +1 -0
- package/dist/contract/protocol/preflight-wake.d.ts +7 -0
- package/dist/contract/protocol/preflight-wake.js +1 -0
- package/dist/contract/protocol/profile-create.d.ts +30 -0
- package/dist/contract/protocol/profile-create.js +2 -0
- package/dist/contract/protocol/sidebar-edition.d.ts +14 -0
- package/dist/contract/protocol/sidebar-edition.js +2 -0
- package/dist/contract/protocol/unit-manifests.d.ts +6 -0
- package/dist/contract/protocol/unit-manifests.js +1 -0
- package/dist/contract/unit-board.d.ts +18 -0
- package/dist/contract/unit-board.js +1 -0
- package/dist/contract/unit-capability.d.ts +34 -0
- package/dist/contract/unit-capability.js +1 -0
- package/dist/{api → contract}/unit-declaration.d.ts +7 -5
- package/dist/{api → contract}/unit-declaration.js +19 -13
- package/dist/contract/unit.d.ts +57 -0
- package/dist/contract/unit.js +24 -0
- package/dist/contract/workbench-contract.d.ts +15 -0
- package/dist/contract/workbench-contract.js +1 -0
- package/dist/host/adapter-set.d.ts +26 -0
- package/dist/host/adapter-set.js +243 -0
- package/dist/host/adapter-types.d.ts +79 -0
- package/dist/host/adapter-types.js +23 -0
- package/dist/host/adapters.d.ts +22 -0
- package/dist/host/adapters.js +9 -0
- package/dist/host/browser.d.ts +3 -0
- package/dist/host/browser.js +3 -0
- package/dist/host/changelog.d.ts +21 -0
- package/dist/host/changelog.js +188 -0
- package/dist/host/channel-plugin.d.ts +15 -0
- package/dist/host/channel-plugin.js +42 -0
- package/dist/host/cli.d.ts +4 -0
- package/dist/host/cli.js +7 -0
- package/dist/host/command-name.d.ts +1 -0
- package/dist/host/command-name.js +14 -0
- package/dist/host/data-error.d.ts +13 -0
- package/dist/host/data-error.js +20 -0
- package/dist/host/data-root.js +3 -0
- package/dist/host/diy-debug.js +2 -1
- package/dist/host/diy-record.js +2 -1
- package/dist/host/facts.d.ts +1 -1
- package/dist/host/facts.js +3 -2
- package/dist/host/git.js +2 -2
- package/dist/host/http.js +1 -1
- package/dist/host/lib-capabilities.d.ts +22 -0
- package/dist/host/lib-capabilities.js +37 -0
- package/dist/host/lib-install.d.ts +9 -0
- package/dist/host/lib-install.js +10 -0
- package/dist/host/long-task.d.ts +1 -1
- package/dist/host/long-task.js +4 -3
- package/dist/host/market-index.d.ts +22 -0
- package/dist/host/market-index.js +420 -0
- package/dist/host/needs.d.ts +4 -0
- package/dist/host/needs.js +2 -0
- package/dist/host/patch.d.ts +9 -0
- package/dist/host/patch.js +78 -0
- package/dist/host/paths.d.ts +12 -0
- package/dist/host/paths.js +28 -0
- package/dist/host/platform.d.ts +14 -0
- package/dist/host/platform.js +40 -0
- package/dist/host/plugin-source.d.ts +8 -6
- package/dist/host/plugin-source.js +43 -33
- package/dist/host/plugin.d.ts +4 -0
- package/dist/host/plugin.js +39 -0
- package/dist/host/port-types.d.ts +191 -0
- package/dist/host/port-types.js +1 -0
- package/dist/host/preflight.d.ts +28 -0
- package/dist/host/preflight.js +1 -0
- package/dist/host/profile-port.d.ts +4 -0
- package/dist/host/profile-port.js +4 -0
- package/dist/host/provision.d.ts +2 -2
- package/dist/host/provision.js +5 -3
- package/dist/host/recipe-rules.d.ts +5 -0
- package/dist/host/recipe-rules.js +382 -0
- package/dist/host/recipe.d.ts +14 -0
- package/dist/host/recipe.js +283 -0
- package/dist/host/shell-text.d.ts +9 -2
- package/dist/host/shell-text.js +15 -3
- package/dist/host/shell.d.ts +2 -0
- package/dist/host/shell.js +7 -0
- package/dist/host/spec.d.ts +8 -0
- package/dist/host/spec.js +24 -0
- package/dist/host/supply-index.d.ts +34 -0
- package/dist/host/supply-index.js +202 -0
- package/dist/host/supply-port.d.ts +5 -0
- package/dist/host/supply-port.js +39 -0
- package/dist/host/time.d.ts +12 -0
- package/dist/host/time.js +19 -0
- package/dist/host/tokens.d.ts +26 -0
- package/dist/host/tokens.js +154 -0
- package/dist/host/tool-approval.d.ts +12 -0
- package/dist/host/tool-approval.js +4 -0
- package/dist/host/tool-kit.js +1 -1
- package/dist/host/unit-face.d.ts +5 -4
- package/dist/host/version-range.d.ts +2 -0
- package/dist/host/version-range.js +73 -0
- package/dist/host/version.d.ts +3 -0
- package/dist/host/version.js +29 -0
- package/dist/host.d.ts +47 -16
- package/dist/host.js +47 -16
- package/dist/i18n/en.js +259 -86
- package/dist/i18n/index.d.ts +333 -88
- package/dist/i18n/merge.js +1 -1
- package/dist/i18n/need-text.d.ts +26 -0
- package/dist/i18n/need-text.js +26 -0
- package/dist/i18n/settings.d.ts +70 -0
- package/dist/i18n/settings.js +136 -0
- package/dist/i18n/zh.d.ts +332 -87
- package/dist/i18n/zh.js +260 -87
- package/dist/index.d.ts +24 -8
- package/dist/index.js +24 -8
- package/dist/lib/blocker-action.d.ts +0 -15
- package/dist/lib/blocker-action.js +1 -25
- package/dist/{api → lib}/dsh-client.d.ts +12 -1
- package/dist/lib/dsh-client.js +1 -0
- package/dist/lib/market-index.d.ts +7 -0
- package/dist/lib/market-index.js +25 -0
- package/dist/lib/react-types.d.ts +5 -0
- package/dist/lib/react-types.js +1 -0
- package/dist/lib/react.d.ts +1 -40
- package/dist/lib/react.js +0 -36
- package/dist/lib/ui.js +3 -9
- package/dist/testing/data.d.ts +13 -0
- package/dist/testing/data.js +46 -0
- package/dist/testing/declaration.d.ts +1 -1
- package/dist/testing/declaration.js +1 -1
- package/dist/testing/host-fakes.d.ts +0 -13
- package/dist/testing/host-fakes.js +0 -24
- package/dist/testing/needs.d.ts +16 -0
- package/dist/testing/needs.js +46 -0
- package/dist/testing.d.ts +2 -0
- package/dist/testing.js +2 -0
- package/package.json +18 -2
- package/src/api/__tests__/blocker-table.test.ts +135 -86
- package/src/api/blocker-table.ts +52 -26
- package/src/api/dsh.ts +17 -1
- package/src/api/host-write.ts +0 -43
- package/src/api/workbench-client.test.ts +2 -2
- package/src/api/workbench-client.ts +2 -1
- package/src/components/Activity.css +0 -1
- package/src/components/ApprovalDetail.css +1 -2
- package/src/components/ApprovalDetail.tsx +1 -1
- package/src/components/ApprovalPlan.css +0 -1
- package/src/components/Card.css +0 -1
- package/src/components/Card.tsx +1 -1
- package/src/components/CardAnchor.test.tsx +1 -1
- package/src/components/CardBoundary.tsx +1 -1
- package/src/components/CardHead.css +0 -1
- package/src/components/CardHead.tsx +1 -1
- package/src/components/ChoiceGroup.css +1 -2
- package/src/components/ConfirmBar.tsx +1 -1
- package/src/components/Form.css +1 -1
- package/src/components/Form.tsx +1 -1
- package/src/components/FormActions.tsx +1 -1
- package/src/components/LongTaskRow.css +1 -2
- package/src/components/LongTaskRow.tsx +1 -1
- package/src/components/Note.css +0 -1
- package/src/components/ResourceStates.css +0 -1
- package/src/components/SelectFieldNodes.tsx +0 -1
- package/src/components/SettingsSection.css +0 -1
- package/src/components/Spinner.css +0 -1
- package/src/components/Stack.css +0 -1
- package/src/components/StateBlock.css +0 -1
- package/src/components/__tests__/long-task-row.test.ts +1 -1
- package/src/components/board-module.ts +3 -3
- package/src/components/icons.tsx +16 -24
- package/src/config/constants.ts +2 -2
- package/src/config/storage.test.ts +1 -25
- package/src/config/storage.ts +0 -12
- package/src/{api → contract}/__tests__/facts.test.ts +73 -25
- package/src/{api → contract}/__tests__/long-task.test.ts +3 -3
- package/src/contract/__tests__/needs.test.ts +74 -0
- package/src/{host → contract}/__tests__/unit-declaration.test.ts +1 -1
- package/src/contract/__tests__/unit.test.ts +43 -0
- package/src/contract/adapters.ts +9 -0
- package/src/contract/cli.ts +3 -0
- package/src/contract/data.ts +145 -0
- package/src/{api → contract}/facts.ts +34 -4
- package/src/contract/needs.ts +331 -0
- package/src/contract/plugin-source.ts +1 -0
- package/src/contract/protocol/bundle-install.ts +198 -0
- package/src/contract/protocol/config-sync.ts +65 -0
- package/src/contract/protocol/diy.ts +4 -0
- package/src/contract/protocol/dsh-plugins.ts +13 -0
- package/src/contract/protocol/long-tasks.ts +16 -0
- package/src/contract/protocol/market-index.ts +246 -0
- package/src/contract/protocol/platform.ts +47 -0
- package/src/contract/protocol/preflight-wake.ts +17 -0
- package/src/contract/protocol/profile-create.ts +36 -0
- package/src/contract/protocol/sidebar-edition.ts +19 -0
- package/src/contract/protocol/unit-manifests.ts +8 -0
- package/src/contract/unit-board.ts +23 -0
- package/src/contract/unit-capability.ts +37 -0
- package/src/{host → contract}/unit-declaration.ts +22 -18
- package/src/contract/unit.ts +76 -0
- package/src/contract/workbench-contract.ts +30 -0
- package/src/host/__tests__/adapter-set.test.ts +228 -0
- package/src/host/__tests__/adapters.test.ts +27 -0
- package/src/host/__tests__/changelog.test.ts +102 -0
- package/src/host/__tests__/command-name.test.ts +16 -0
- package/src/host/__tests__/diy-record.test.ts +3 -3
- package/src/host/__tests__/facts.test.ts +5 -5
- package/src/host/__tests__/long-task.test.ts +1 -1
- package/src/host/__tests__/market-index.test.ts +179 -0
- package/src/host/__tests__/plugin-ledger.test.ts +10 -10
- package/src/host/__tests__/provision.test.ts +7 -6
- package/src/host/__tests__/shell-text.test.ts +16 -1
- package/src/host/__tests__/supply-index.test.ts +137 -0
- package/src/host/__tests__/tokens.test.ts +110 -0
- package/src/host/__tests__/version-range.test.ts +21 -0
- package/src/host/adapter-set.ts +298 -0
- package/src/host/adapter-types.ts +105 -0
- package/src/host/adapters.ts +44 -0
- package/src/host/browser.ts +3 -0
- package/src/host/changelog.ts +212 -0
- package/src/host/channel-plugin.ts +55 -0
- package/src/host/cli.ts +11 -0
- package/src/host/command-name.ts +15 -0
- package/src/host/data-error.ts +21 -0
- package/src/host/data-root.ts +2 -0
- package/src/host/diy-debug.ts +2 -1
- package/src/host/diy-record.ts +2 -1
- package/src/host/facts.ts +4 -3
- package/src/host/git.ts +2 -2
- package/src/host/http.ts +1 -1
- package/src/host/lib-capabilities.ts +52 -0
- package/src/host/lib-install.ts +18 -0
- package/src/host/long-task.ts +4 -3
- package/src/host/market-index.ts +413 -0
- package/src/host/needs.ts +6 -0
- package/src/host/patch.ts +93 -0
- package/src/host/paths.ts +37 -0
- package/src/host/platform.ts +48 -0
- package/src/host/plugin-source.ts +48 -56
- package/src/host/plugin.ts +39 -0
- package/src/host/port-types.ts +222 -0
- package/src/host/preflight.ts +31 -0
- package/src/host/profile-port.ts +9 -0
- package/src/host/provision.ts +7 -4
- package/src/host/recipe-rules.ts +330 -0
- package/src/host/recipe.test.ts +334 -0
- package/src/host/recipe.ts +323 -0
- package/src/host/shell-text.ts +15 -3
- package/src/host/shell.ts +9 -0
- package/src/host/spec.ts +32 -0
- package/src/host/supply-index.ts +256 -0
- package/src/host/supply-port.ts +45 -0
- package/src/host/time.ts +22 -0
- package/src/host/tokens.ts +186 -0
- package/src/host/tool-approval.ts +17 -0
- package/src/host/tool-kit.ts +1 -1
- package/src/host/unit-face.ts +5 -4
- package/src/host/version-range.ts +71 -0
- package/src/host/version.ts +30 -0
- package/src/host.ts +47 -16
- package/src/i18n/en.ts +261 -86
- package/src/i18n/merge.test.ts +6 -6
- package/src/i18n/merge.ts +1 -1
- package/src/i18n/need-text.ts +27 -0
- package/src/i18n/settings.ts +141 -0
- package/src/i18n/zh.ts +263 -87
- package/src/index.ts +24 -8
- package/src/lib/blocker-action.ts +0 -28
- package/src/{api → lib}/dsh-client.ts +16 -2
- package/src/lib/market-index.ts +36 -0
- package/src/lib/react-types.ts +5 -0
- package/src/lib/react.ts +1 -40
- package/src/lib/text.test.ts +2 -2
- package/src/lib/ui.test.ts +6 -5
- package/src/lib/ui.ts +3 -9
- package/src/testing/__tests__/declaration.test.ts +1 -1
- package/src/testing/data.ts +66 -0
- package/src/testing/declaration.ts +2 -2
- package/src/testing/host-fakes.ts +0 -38
- package/src/testing/needs.ts +80 -0
- package/src/testing.ts +2 -0
- package/dist/api/unit-board.d.ts +0 -39
- package/dist/api/unit-board.js +0 -45
- package/dist/api/unit.d.ts +0 -85
- package/dist/api/unit.js +0 -8
- package/dist/api/workbench-contract.d.ts +0 -44
- package/dist/components/MockStrip.css +0 -39
- package/dist/components/MockStrip.d.ts +0 -15
- package/dist/components/MockStrip.js +0 -8
- package/dist/components/SaveBar.css +0 -7
- package/dist/host/browser-find.d.ts +0 -38
- package/dist/host/browser-find.js +0 -242
- package/dist/host/browser-read.d.ts +0 -28
- package/dist/host/browser-read.js +0 -243
- package/dist/host/opencli-runtime.d.ts +0 -25
- package/dist/host/opencli-runtime.js +0 -56
- package/dist/host/opencli-window.d.ts +0 -2
- package/dist/host/opencli-window.js +0 -8
- package/dist/host/opencli.d.ts +0 -37
- package/dist/host/opencli.js +0 -155
- package/dist/host/unit-declaration.d.ts +0 -65
- package/dist/host/unit-declaration.js +0 -213
- package/dist/host/unit-libs.d.ts +0 -37
- package/dist/lib/diy-debug.d.ts +0 -80
- package/dist/lib/diy-debug.js +0 -88
- package/dist/lib/diy-record.d.ts +0 -135
- package/dist/lib/diy-record.js +0 -162
- package/dist/lib/lint-probe.d.ts +0 -1
- package/dist/lib/lint-probe.js +0 -6
- package/dist/lib/panel-state.d.ts +0 -120
- package/dist/lib/panel-state.js +0 -176
- package/src/api/__tests__/host-write.test.ts +0 -75
- package/src/api/__tests__/unit-board.test.ts +0 -76
- package/src/api/__tests__/unit.test.ts +0 -38
- package/src/api/unit-board.ts +0 -69
- package/src/api/unit.ts +0 -93
- package/src/api/workbench-contract.ts +0 -59
- package/src/host/__tests__/browser-find.test.ts +0 -330
- package/src/host/__tests__/browser-read.test.ts +0 -228
- package/src/host/__tests__/opencli-runtime.test.ts +0 -46
- package/src/host/browser-find.ts +0 -311
- package/src/host/browser-read.ts +0 -311
- package/src/host/opencli-runtime.ts +0 -79
- package/src/host/opencli-window.ts +0 -11
- package/src/host/opencli.ts +0 -213
- package/src/host/unit-libs.ts +0 -41
- package/src/lib/blocker-action.test.ts +0 -40
- /package/dist/{api/blocker.js → contract/adapters.js} +0 -0
- /package/dist/{api → contract}/blocker.d.ts +0 -0
- /package/dist/{api/dsh-client.js → contract/blocker.js} +0 -0
- /package/dist/{api/workbench-contract.js → contract/cli.js} +0 -0
- /package/dist/{host/unit-libs.js → contract/data.js} +0 -0
- /package/dist/{api → contract}/long-task.d.ts +0 -0
- /package/dist/{api → contract}/long-task.js +0 -0
- /package/src/{api → contract}/blocker.ts +0 -0
- /package/src/{api → contract}/long-task.ts +0 -0
package/src/host/browser-find.ts
DELETED
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
import type { DshShell } from '../api/dsh.js';
|
|
2
|
-
import { parseJsonLoose } from '../lib/text.js';
|
|
3
|
-
import { BROWSER_READ_SESSION } from './browser-read.js';
|
|
4
|
-
import { backgroundLine } from './opencli-window.js';
|
|
5
|
-
import { runShell } from './shell.js';
|
|
6
|
-
|
|
7
|
-
const DEFAULT_TIMEOUT_MS = 25000;
|
|
8
|
-
const DEFAULT_SETTLE_MS = 2500;
|
|
9
|
-
const URL_TIMEOUT_MS = 8000;
|
|
10
|
-
const DEFAULT_LIMIT = 50;
|
|
11
|
-
const DEFAULT_TEXT_MAX = 200;
|
|
12
|
-
|
|
13
|
-
export interface BrowserFindEntry {
|
|
14
|
-
readonly text: string;
|
|
15
|
-
readonly tag: string;
|
|
16
|
-
readonly nth: number;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface BrowserFindResult {
|
|
20
|
-
readonly ok: boolean;
|
|
21
|
-
readonly code: string;
|
|
22
|
-
readonly at: number;
|
|
23
|
-
readonly entries: readonly BrowserFindEntry[];
|
|
24
|
-
readonly count: number;
|
|
25
|
-
readonly message: string;
|
|
26
|
-
readonly hint?: string;
|
|
27
|
-
readonly detail?: string;
|
|
28
|
-
readonly helpUrl?: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export interface BrowserFindOptions {
|
|
32
|
-
readonly shell: DshShell;
|
|
33
|
-
readonly css: string;
|
|
34
|
-
readonly url?: string;
|
|
35
|
-
readonly command?: string;
|
|
36
|
-
readonly session?: string;
|
|
37
|
-
readonly limit?: number;
|
|
38
|
-
readonly textMax?: number;
|
|
39
|
-
readonly open?: boolean;
|
|
40
|
-
readonly timeoutMs?: number;
|
|
41
|
-
readonly settleMs?: number;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function cellRowsOf(
|
|
45
|
-
entries: readonly { readonly text?: string }[],
|
|
46
|
-
minCells = 2,
|
|
47
|
-
): string[][] {
|
|
48
|
-
const rows: string[][] = [];
|
|
49
|
-
for (const entry of entries) {
|
|
50
|
-
const text = typeof entry?.text === 'string' ? entry.text : '';
|
|
51
|
-
const cells = text
|
|
52
|
-
.split('\n')
|
|
53
|
-
.map((one) => one.trim())
|
|
54
|
-
.filter((one) => one !== '');
|
|
55
|
-
if (cells.length >= minCells) rows.push(cells);
|
|
56
|
-
}
|
|
57
|
-
return rows;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export function cellRows(text: string): string[] {
|
|
61
|
-
return text
|
|
62
|
-
.split('\n')
|
|
63
|
-
.map((one) => one.trim())
|
|
64
|
-
.filter((one) => one !== '');
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
68
|
-
return typeof value === 'object' && value !== null ? (value as Record<string, unknown>) : undefined;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
function textOf(value: unknown): string {
|
|
72
|
-
return typeof value === 'string' ? value.trim() : '';
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
export function entriesOf(value: unknown): BrowserFindEntry[] {
|
|
76
|
-
const record = asRecord(value);
|
|
77
|
-
const raw = record === undefined ? undefined : record['entries'];
|
|
78
|
-
if (!Array.isArray(raw)) return [];
|
|
79
|
-
const entries: BrowserFindEntry[] = [];
|
|
80
|
-
for (const one of raw) {
|
|
81
|
-
const item = asRecord(one);
|
|
82
|
-
if (item === undefined) continue;
|
|
83
|
-
const nth = typeof item['nth'] === 'number' ? item['nth'] : entries.length;
|
|
84
|
-
entries.push({ text: textOf(item['text']), tag: textOf(item['tag']), nth });
|
|
85
|
-
}
|
|
86
|
-
return entries;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
export function reasonOf(stdout: string, stderr: string): string {
|
|
90
|
-
const parsed = asRecord(parseJsonLoose(stdout));
|
|
91
|
-
const error = parsed === undefined ? undefined : asRecord(parsed['error']);
|
|
92
|
-
const message = error === undefined ? '' : textOf(error['message']);
|
|
93
|
-
if (message !== '') return message;
|
|
94
|
-
const source = stderr.trim() === '' ? stdout : stderr;
|
|
95
|
-
const lines = source
|
|
96
|
-
.split('\n')
|
|
97
|
-
.map((one) => one.trim())
|
|
98
|
-
.filter((one) => one !== '');
|
|
99
|
-
return (lines[lines.length - 1] ?? '').slice(-200);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function hintOf(stdout: string): string | undefined {
|
|
103
|
-
const parsed = asRecord(parseJsonLoose(stdout));
|
|
104
|
-
const error = parsed === undefined ? undefined : asRecord(parsed['error']);
|
|
105
|
-
const hint = error === undefined ? '' : textOf(error['hint']);
|
|
106
|
-
return hint === '' ? undefined : hint;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
export function sameOrigin(left: string, right: string): boolean {
|
|
110
|
-
try {
|
|
111
|
-
return new URL(left).origin === new URL(right).origin;
|
|
112
|
-
} catch {
|
|
113
|
-
return false;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
export function urlOf(stdout: string): string {
|
|
118
|
-
const lines = stdout
|
|
119
|
-
.split('\n')
|
|
120
|
-
.map((one) => one.trim())
|
|
121
|
-
.filter((one) => one !== '');
|
|
122
|
-
const first = (lines[0] ?? '').replace(/^url:\s*/i, '').trim();
|
|
123
|
-
const bare = first.replace(/^["']+|["']+$/g, '');
|
|
124
|
-
if (bare.startsWith('{')) {
|
|
125
|
-
const parsed = asRecord(parseJsonLoose(bare));
|
|
126
|
-
const url = parsed === undefined ? '' : textOf(parsed['url']);
|
|
127
|
-
if (url !== '') return url;
|
|
128
|
-
}
|
|
129
|
-
return bare;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
async function currentUrl(
|
|
133
|
-
shell: DshShell,
|
|
134
|
-
command: string,
|
|
135
|
-
session: string,
|
|
136
|
-
): Promise<string> {
|
|
137
|
-
const line = backgroundLine(command, ['browser', session, 'get', 'url', '--window', 'background']);
|
|
138
|
-
try {
|
|
139
|
-
const result = await runShell(shell, line, { timeoutMs: URL_TIMEOUT_MS });
|
|
140
|
-
if (result.exitCode !== 0) return '';
|
|
141
|
-
return urlOf(result.stdout);
|
|
142
|
-
} catch {
|
|
143
|
-
return '';
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
function sleep(ms: number): Promise<void> {
|
|
148
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
149
|
-
}
|
|
150
|
-
|
|
151
|
-
type FindOutcome =
|
|
152
|
-
| { readonly kind: 'ok'; readonly entries: readonly BrowserFindEntry[]; readonly stdout: string }
|
|
153
|
-
| { readonly kind: 'timeout'; readonly detail: string }
|
|
154
|
-
| { readonly kind: 'failed'; readonly detail: string; readonly hint?: string };
|
|
155
|
-
|
|
156
|
-
async function findOnce(
|
|
157
|
-
shell: DshShell,
|
|
158
|
-
command: string,
|
|
159
|
-
session: string,
|
|
160
|
-
css: string,
|
|
161
|
-
limit: number,
|
|
162
|
-
textMax: number,
|
|
163
|
-
timeoutMs: number,
|
|
164
|
-
): Promise<FindOutcome | undefined> {
|
|
165
|
-
const line = backgroundLine(command, [
|
|
166
|
-
'browser',
|
|
167
|
-
session,
|
|
168
|
-
'find',
|
|
169
|
-
'--css',
|
|
170
|
-
css,
|
|
171
|
-
'--limit',
|
|
172
|
-
String(limit),
|
|
173
|
-
'--text-max',
|
|
174
|
-
String(textMax),
|
|
175
|
-
'--window',
|
|
176
|
-
'background',
|
|
177
|
-
]);
|
|
178
|
-
const result = await runShell(shell, line, { timeoutMs });
|
|
179
|
-
if (result.timedOut) {
|
|
180
|
-
return { kind: 'timeout', detail: `没在 ${Math.round(timeoutMs / 1000)} 秒内返回` };
|
|
181
|
-
}
|
|
182
|
-
if (result.exitCode !== 0) {
|
|
183
|
-
return {
|
|
184
|
-
kind: 'failed',
|
|
185
|
-
detail: reasonOf(result.stdout, result.stderr),
|
|
186
|
-
...(hintOf(result.stdout) === undefined ? {} : { hint: hintOf(result.stdout) }),
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
const entries = entriesOf(parseJsonLoose(result.stdout));
|
|
190
|
-
return { kind: 'ok', entries, stdout: result.stdout };
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async function openOnce(
|
|
194
|
-
shell: DshShell,
|
|
195
|
-
command: string,
|
|
196
|
-
session: string,
|
|
197
|
-
url: string,
|
|
198
|
-
timeoutMs: number,
|
|
199
|
-
): Promise<string | undefined> {
|
|
200
|
-
const line = backgroundLine(command, ['browser', session, 'open', url, '--window', 'background']);
|
|
201
|
-
let result: Awaited<ReturnType<typeof runShell>>;
|
|
202
|
-
try {
|
|
203
|
-
result = await runShell(shell, line, { timeoutMs });
|
|
204
|
-
} catch (error) {
|
|
205
|
-
return error instanceof Error ? error.message : String(error);
|
|
206
|
-
}
|
|
207
|
-
if (result.timedOut) return `没在 ${Math.round(timeoutMs / 1000)} 秒内打开`;
|
|
208
|
-
if (result.exitCode !== 0) {
|
|
209
|
-
const reason = reasonOf(result.stdout, result.stderr);
|
|
210
|
-
return reason === '' ? `opencli 打开失败(退出码 ${String(result.exitCode ?? '?')})` : reason;
|
|
211
|
-
}
|
|
212
|
-
return undefined;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
export async function browserFind(options: BrowserFindOptions): Promise<BrowserFindResult> {
|
|
216
|
-
const { shell, css, url, open = true } = options;
|
|
217
|
-
const command = options.command ?? 'opencli';
|
|
218
|
-
const session = options.session ?? BROWSER_READ_SESSION;
|
|
219
|
-
const limit = options.limit ?? DEFAULT_LIMIT;
|
|
220
|
-
const textMax = options.textMax ?? DEFAULT_TEXT_MAX;
|
|
221
|
-
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
222
|
-
const settleMs = options.settleMs ?? DEFAULT_SETTLE_MS;
|
|
223
|
-
const at = Date.now();
|
|
224
|
-
|
|
225
|
-
const find = async (): Promise<FindOutcome | undefined> => {
|
|
226
|
-
try {
|
|
227
|
-
return await findOnce(shell, command, session, css, limit, textMax, timeoutMs);
|
|
228
|
-
} catch (error) {
|
|
229
|
-
return {
|
|
230
|
-
kind: 'failed',
|
|
231
|
-
detail: error instanceof Error ? error.message : String(error),
|
|
232
|
-
hint: '确认这台机器上 opencli 能跑(工作台会自己把它挂好)',
|
|
233
|
-
};
|
|
234
|
-
}
|
|
235
|
-
};
|
|
236
|
-
|
|
237
|
-
const canMove = open && url !== undefined;
|
|
238
|
-
|
|
239
|
-
if (canMove) {
|
|
240
|
-
const here = await currentUrl(shell, command, session);
|
|
241
|
-
if (here !== '' && !sameOrigin(here, url)) {
|
|
242
|
-
const opened = await openOnce(shell, command, session, url, timeoutMs);
|
|
243
|
-
if (opened !== undefined) {
|
|
244
|
-
return {
|
|
245
|
-
ok: false,
|
|
246
|
-
code: 'cli-exec-failed',
|
|
247
|
-
at,
|
|
248
|
-
entries: [],
|
|
249
|
-
count: 0,
|
|
250
|
-
message: '打不开那个页面',
|
|
251
|
-
hint: '在浏览器里手动打开一次,然后重试',
|
|
252
|
-
detail: opened,
|
|
253
|
-
helpUrl: url,
|
|
254
|
-
};
|
|
255
|
-
}
|
|
256
|
-
await sleep(settleMs);
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
let outcome = await find();
|
|
261
|
-
if (outcome === undefined) {
|
|
262
|
-
return { ok: false, code: 'cli-exec-failed', at, entries: [], count: 0, message: '没找到元素' };
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
if (outcome.kind === 'failed' && canMove) {
|
|
266
|
-
const opened = await openOnce(shell, command, session, url, timeoutMs);
|
|
267
|
-
if (opened === undefined) {
|
|
268
|
-
await sleep(settleMs);
|
|
269
|
-
const second = await find();
|
|
270
|
-
if (second !== undefined) outcome = second;
|
|
271
|
-
}
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
if (outcome.kind === 'timeout') {
|
|
275
|
-
return {
|
|
276
|
-
ok: false,
|
|
277
|
-
code: 'cli-timeout',
|
|
278
|
-
at,
|
|
279
|
-
entries: [],
|
|
280
|
-
count: 0,
|
|
281
|
-
message: '这次没找到元素(等超时了)',
|
|
282
|
-
hint: '稍后重试',
|
|
283
|
-
detail: outcome.detail,
|
|
284
|
-
...(url === undefined ? {} : { helpUrl: url }),
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
if (outcome.kind === 'failed') {
|
|
289
|
-
const detail = outcome.detail;
|
|
290
|
-
return {
|
|
291
|
-
ok: false,
|
|
292
|
-
code: 'cli-exec-failed',
|
|
293
|
-
at,
|
|
294
|
-
entries: [],
|
|
295
|
-
count: 0,
|
|
296
|
-
message: detail === '' ? '没找到元素(opencli 命令失败)' : `没找到元素:${detail}`,
|
|
297
|
-
...(outcome.hint === undefined ? {} : { hint: outcome.hint }),
|
|
298
|
-
...(detail === '' ? {} : { detail }),
|
|
299
|
-
...(url === undefined ? {} : { helpUrl: url }),
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
return {
|
|
304
|
-
ok: true,
|
|
305
|
-
code: 'ok',
|
|
306
|
-
at,
|
|
307
|
-
entries: outcome.entries,
|
|
308
|
-
count: outcome.entries.length,
|
|
309
|
-
message: outcome.entries.length === 0 ? '页面上没有匹配的元素' : `找到 ${String(outcome.entries.length)} 个元素`,
|
|
310
|
-
};
|
|
311
|
-
}
|
package/src/host/browser-read.ts
DELETED
|
@@ -1,311 +0,0 @@
|
|
|
1
|
-
import type { DshShell } from '../api/dsh.js';
|
|
2
|
-
import { parseJsonLoose } from '../lib/text.js';
|
|
3
|
-
import { tailLines } from './shell-text.js';
|
|
4
|
-
import { backgroundLine } from './opencli-window.js';
|
|
5
|
-
import { runShell } from './shell.js';
|
|
6
|
-
|
|
7
|
-
export const BROWSER_SCREEN_SESSION = 'workbench';
|
|
8
|
-
export const BROWSER_READ_SESSION = 'workbench-read';
|
|
9
|
-
export const BROWSER_RECORD_SESSION = 'workbench-rec';
|
|
10
|
-
|
|
11
|
-
const DEFAULT_TIMEOUT_MS = 25000;
|
|
12
|
-
const DEFAULT_RETRY_MS = 1200;
|
|
13
|
-
const DEFAULT_SETTLE_MS = 900;
|
|
14
|
-
|
|
15
|
-
const AUTH_CODES = new Set([
|
|
16
|
-
'NOT_LOGGED_IN',
|
|
17
|
-
'NOT_LOGIN',
|
|
18
|
-
'AUTH_FAILED',
|
|
19
|
-
'AUTH_REQUIRED',
|
|
20
|
-
'UNAUTHENTICATED',
|
|
21
|
-
'UNAUTHORIZED',
|
|
22
|
-
'TOKEN_EXPIRED',
|
|
23
|
-
]);
|
|
24
|
-
|
|
25
|
-
const PERMISSION_CODES = new Set(['FORBIDDEN', 'PERMISSION_DENIED', 'NO_PERMISSION', 'EACCES']);
|
|
26
|
-
|
|
27
|
-
export interface BrowserReadResult {
|
|
28
|
-
readonly ok: boolean;
|
|
29
|
-
readonly code: string;
|
|
30
|
-
readonly at: number;
|
|
31
|
-
readonly value?: unknown;
|
|
32
|
-
readonly pageCode?: string;
|
|
33
|
-
readonly message: string;
|
|
34
|
-
readonly hint?: string;
|
|
35
|
-
readonly detail?: string;
|
|
36
|
-
readonly helpUrl?: string;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
export interface BrowserReadOptions {
|
|
40
|
-
readonly shell: DshShell;
|
|
41
|
-
readonly script: string;
|
|
42
|
-
readonly url?: string;
|
|
43
|
-
readonly loginUrl?: string;
|
|
44
|
-
readonly command?: string;
|
|
45
|
-
readonly session?: string;
|
|
46
|
-
readonly open?: boolean;
|
|
47
|
-
readonly timeoutMs?: number;
|
|
48
|
-
readonly retryMs?: number;
|
|
49
|
-
readonly settleMs?: number;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
|
53
|
-
return typeof value === 'object' && value !== null ? (value as Record<string, unknown>) : undefined;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function textOf(value: unknown): string {
|
|
57
|
-
return typeof value === 'string' ? value.trim() : '';
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function codeOf(page: unknown): string {
|
|
61
|
-
const record = asRecord(page);
|
|
62
|
-
return record === undefined ? '' : textOf(record['code']);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function messageOf(page: unknown): string {
|
|
66
|
-
const record = asRecord(page);
|
|
67
|
-
return record === undefined ? '' : textOf(record['message']);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function hintOf(page: unknown): string | undefined {
|
|
71
|
-
const record = asRecord(page);
|
|
72
|
-
const hint = record === undefined ? '' : textOf(record['hint']);
|
|
73
|
-
return hint === '' ? undefined : hint;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function pageFailed(page: unknown): boolean {
|
|
77
|
-
const record = asRecord(page);
|
|
78
|
-
return record !== undefined && record['ok'] === false;
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function blockerFor(code: string): 'auth-required' | 'permission-denied' | undefined {
|
|
82
|
-
const normalized = code.trim().toUpperCase().replace(/-/g, '_');
|
|
83
|
-
if (normalized === '') return undefined;
|
|
84
|
-
const matches = (set: Set<string>): boolean =>
|
|
85
|
-
[...set].some((one) => normalized === one || normalized.endsWith(`_${one}`));
|
|
86
|
-
if (matches(PERMISSION_CODES)) return 'permission-denied';
|
|
87
|
-
if (matches(AUTH_CODES) || normalized.includes('UNAUTH')) return 'auth-required';
|
|
88
|
-
return undefined;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
function sleep(ms: number): Promise<void> {
|
|
92
|
-
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
type Attempt =
|
|
96
|
-
| { readonly kind: 'value'; readonly value: unknown }
|
|
97
|
-
| { readonly kind: 'failure'; readonly result: Omit<BrowserReadResult, 'at'> };
|
|
98
|
-
|
|
99
|
-
type ShellOutcome =
|
|
100
|
-
| { readonly kind: 'ok'; readonly stdout: string }
|
|
101
|
-
| { readonly kind: 'timeout'; readonly detail: string; readonly stderr: string }
|
|
102
|
-
| { readonly kind: 'failed'; readonly detail: string; readonly stderr: string };
|
|
103
|
-
|
|
104
|
-
async function shellRun(
|
|
105
|
-
shell: DshShell,
|
|
106
|
-
command: string,
|
|
107
|
-
argv: readonly string[],
|
|
108
|
-
timeoutMs: number,
|
|
109
|
-
): Promise<ShellOutcome> {
|
|
110
|
-
const result = await runShell(shell, backgroundLine(command, argv), { timeoutMs });
|
|
111
|
-
if (result.timedOut) {
|
|
112
|
-
return { kind: 'timeout', detail: `没在 ${Math.round(timeoutMs / 1000)} 秒内返回`, stderr: result.stderr };
|
|
113
|
-
}
|
|
114
|
-
if (result.exitCode !== 0) {
|
|
115
|
-
return { kind: 'failed', detail: tailLines(result.stderr || result.stdout, 4), stderr: result.stderr };
|
|
116
|
-
}
|
|
117
|
-
return { kind: 'ok', stdout: result.stdout };
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
async function evalOnce(
|
|
121
|
-
shell: DshShell,
|
|
122
|
-
command: string,
|
|
123
|
-
session: string,
|
|
124
|
-
script: string,
|
|
125
|
-
timeoutMs: number,
|
|
126
|
-
retryMs: number,
|
|
127
|
-
): Promise<Attempt> {
|
|
128
|
-
const run = async (): Promise<Attempt> => {
|
|
129
|
-
let attempt: Awaited<ReturnType<typeof shellRun>>;
|
|
130
|
-
try {
|
|
131
|
-
attempt = await shellRun(
|
|
132
|
-
shell,
|
|
133
|
-
command,
|
|
134
|
-
['browser', session, 'eval', script, '--window', 'background'],
|
|
135
|
-
timeoutMs,
|
|
136
|
-
);
|
|
137
|
-
} catch (error) {
|
|
138
|
-
return {
|
|
139
|
-
kind: 'failure',
|
|
140
|
-
result: {
|
|
141
|
-
ok: false,
|
|
142
|
-
code: 'cli-exec-failed',
|
|
143
|
-
message: '读不了:opencli 起不来',
|
|
144
|
-
hint: '确认这台机器上 opencli 能跑(工作台会自己把它挂好)',
|
|
145
|
-
detail: error instanceof Error ? error.message : String(error),
|
|
146
|
-
},
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
if (attempt.kind === 'timeout') {
|
|
150
|
-
return {
|
|
151
|
-
kind: 'failure',
|
|
152
|
-
result: {
|
|
153
|
-
ok: false,
|
|
154
|
-
code: 'cli-timeout',
|
|
155
|
-
message: '这次没读到(等超时了)',
|
|
156
|
-
hint: '稍后重试',
|
|
157
|
-
detail: attempt.detail,
|
|
158
|
-
},
|
|
159
|
-
};
|
|
160
|
-
}
|
|
161
|
-
if (attempt.kind === 'failed') {
|
|
162
|
-
const normalized = attempt.stderr.trim().toUpperCase().replace(/-/g, '_');
|
|
163
|
-
const forbidden = [...PERMISSION_CODES].some((one) => normalized.includes(one));
|
|
164
|
-
return {
|
|
165
|
-
kind: 'failure',
|
|
166
|
-
result: {
|
|
167
|
-
ok: false,
|
|
168
|
-
code: forbidden ? 'permission-denied' : 'cli-exec-failed',
|
|
169
|
-
message: forbidden ? '没有权限在这台机器上读' : '读不了(命令失败)',
|
|
170
|
-
hint: forbidden ? '换一个有权限的账号,或让用户开权限' : '把下面这段原始输出看一眼再重试',
|
|
171
|
-
detail: attempt.detail,
|
|
172
|
-
},
|
|
173
|
-
};
|
|
174
|
-
}
|
|
175
|
-
const value = parseJsonLoose(attempt.stdout);
|
|
176
|
-
if (value === undefined) {
|
|
177
|
-
return {
|
|
178
|
-
kind: 'failure',
|
|
179
|
-
result: {
|
|
180
|
-
ok: false,
|
|
181
|
-
code: 'cli-exec-failed',
|
|
182
|
-
message: '读到了、但不是 JSON',
|
|
183
|
-
hint: '脚本必须回 JSON',
|
|
184
|
-
detail: tailLines(attempt.stdout, 4),
|
|
185
|
-
},
|
|
186
|
-
};
|
|
187
|
-
}
|
|
188
|
-
return { kind: 'value', value };
|
|
189
|
-
};
|
|
190
|
-
|
|
191
|
-
const first = await run();
|
|
192
|
-
if (first.kind === 'value') return first;
|
|
193
|
-
if (first.result.code !== 'cli-exec-failed') return first;
|
|
194
|
-
await sleep(retryMs);
|
|
195
|
-
return run();
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
async function openOnce(
|
|
199
|
-
shell: DshShell,
|
|
200
|
-
command: string,
|
|
201
|
-
session: string,
|
|
202
|
-
url: string,
|
|
203
|
-
timeoutMs: number,
|
|
204
|
-
): Promise<Omit<BrowserReadResult, 'at'> | undefined> {
|
|
205
|
-
let attempt: Awaited<ReturnType<typeof shellRun>>;
|
|
206
|
-
try {
|
|
207
|
-
attempt = await shellRun(shell, command, ['browser', session, 'open', url, '--window', 'background'], timeoutMs);
|
|
208
|
-
} catch (error) {
|
|
209
|
-
return {
|
|
210
|
-
ok: false,
|
|
211
|
-
code: 'cli-exec-failed',
|
|
212
|
-
message: '打不开那个页面',
|
|
213
|
-
hint: '确认 opencli 能跑起来',
|
|
214
|
-
detail: error instanceof Error ? error.message : String(error),
|
|
215
|
-
helpUrl: url,
|
|
216
|
-
};
|
|
217
|
-
}
|
|
218
|
-
if (attempt.kind === 'ok') return undefined;
|
|
219
|
-
return {
|
|
220
|
-
ok: false,
|
|
221
|
-
code: attempt.kind === 'timeout' ? 'cli-timeout' : 'cli-exec-failed',
|
|
222
|
-
message: '打不开那个页面',
|
|
223
|
-
hint: '在浏览器里手动打开一次,然后重试',
|
|
224
|
-
detail: attempt.detail,
|
|
225
|
-
helpUrl: url,
|
|
226
|
-
};
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
export async function browserRead(options: BrowserReadOptions): Promise<BrowserReadResult> {
|
|
230
|
-
const { shell, script, url, loginUrl, open = true } = options;
|
|
231
|
-
const command = options.command ?? 'opencli';
|
|
232
|
-
const session = options.session ?? BROWSER_READ_SESSION;
|
|
233
|
-
const timeoutMs = options.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
234
|
-
const retryMs = options.retryMs ?? DEFAULT_RETRY_MS;
|
|
235
|
-
const settleMs = options.settleMs ?? DEFAULT_SETTLE_MS;
|
|
236
|
-
const at = Date.now();
|
|
237
|
-
|
|
238
|
-
const first = await evalOnce(shell, command, session, script, timeoutMs, retryMs);
|
|
239
|
-
if (first.kind === 'failure') return { ...first.result, at };
|
|
240
|
-
let page = first.value;
|
|
241
|
-
|
|
242
|
-
if (codeOf(page) === 'not-on-site') {
|
|
243
|
-
if (!open || url === undefined) {
|
|
244
|
-
return {
|
|
245
|
-
ok: false,
|
|
246
|
-
code: 'cli-exec-failed',
|
|
247
|
-
at,
|
|
248
|
-
message: '没停在那套系统上',
|
|
249
|
-
hint: '先在那个浏览器里把它打开,再读一次',
|
|
250
|
-
detail: JSON.stringify(page),
|
|
251
|
-
...(url === undefined ? {} : { helpUrl: url }),
|
|
252
|
-
};
|
|
253
|
-
}
|
|
254
|
-
const opened = await openOnce(shell, command, session, url, timeoutMs);
|
|
255
|
-
if (opened !== undefined) return { ...opened, at };
|
|
256
|
-
await sleep(settleMs);
|
|
257
|
-
const second = await evalOnce(shell, command, session, script, timeoutMs, retryMs);
|
|
258
|
-
if (second.kind === 'failure') return { ...second.result, at };
|
|
259
|
-
page = second.value;
|
|
260
|
-
if (codeOf(page) === 'not-on-site') {
|
|
261
|
-
return {
|
|
262
|
-
ok: false,
|
|
263
|
-
code: 'cli-exec-failed',
|
|
264
|
-
at,
|
|
265
|
-
message: '打开了页面、又读了一遍,仍然不在那套系统上',
|
|
266
|
-
hint: '确认这个地址就是那套系统(或者它跳到了别的域)',
|
|
267
|
-
detail: JSON.stringify(page),
|
|
268
|
-
helpUrl: url,
|
|
269
|
-
};
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
if (!pageFailed(page)) return { ok: true, code: 'ok', at, value: page, message: '读到了' };
|
|
274
|
-
|
|
275
|
-
const pageCode = codeOf(page) || 'read-failed';
|
|
276
|
-
const message = messageOf(page) || `读不到(${pageCode})`;
|
|
277
|
-
const blocker = blockerFor(pageCode);
|
|
278
|
-
if (blocker === 'auth-required') {
|
|
279
|
-
return {
|
|
280
|
-
ok: false,
|
|
281
|
-
code: blocker,
|
|
282
|
-
at,
|
|
283
|
-
pageCode,
|
|
284
|
-
message,
|
|
285
|
-
hint: '去登录一次,再重试',
|
|
286
|
-
detail: JSON.stringify(page),
|
|
287
|
-
...(loginUrl === undefined && url === undefined ? {} : { helpUrl: loginUrl ?? url }),
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
if (blocker === 'permission-denied') {
|
|
291
|
-
return {
|
|
292
|
-
ok: false,
|
|
293
|
-
code: blocker,
|
|
294
|
-
at,
|
|
295
|
-
pageCode,
|
|
296
|
-
message,
|
|
297
|
-
hint: '这个账号没有权限',
|
|
298
|
-
detail: JSON.stringify(page),
|
|
299
|
-
...(loginUrl === undefined && url === undefined ? {} : { helpUrl: loginUrl ?? url }),
|
|
300
|
-
};
|
|
301
|
-
}
|
|
302
|
-
return {
|
|
303
|
-
ok: false,
|
|
304
|
-
code: pageCode,
|
|
305
|
-
at,
|
|
306
|
-
pageCode,
|
|
307
|
-
message,
|
|
308
|
-
detail: JSON.stringify(page),
|
|
309
|
-
...(hintOf(page) === undefined ? {} : { hint: hintOf(page) }),
|
|
310
|
-
};
|
|
311
|
-
}
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { OPENCLI_PIN } from '../config/constants.js';
|
|
2
|
-
import type { UnitLibNeed } from '../api/unit.js';
|
|
3
|
-
import { quoteShellArg } from './shell-text.js';
|
|
4
|
-
|
|
5
|
-
import { dshHome } from './data-root.js';
|
|
6
|
-
|
|
7
|
-
export { dshHome };
|
|
8
|
-
|
|
9
|
-
export function isolatedRoot(): string {
|
|
10
|
-
return `${dshHome()}/workbench/opencli`;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export function isolatedHome(): string {
|
|
14
|
-
return `${isolatedRoot()}/home`;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
export const OPENCLI_NPM_PACKAGE = '@jackwener/opencli';
|
|
18
|
-
|
|
19
|
-
export function toolchainDir(): string {
|
|
20
|
-
return `${isolatedRoot()}/toolchain`;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function toolchainOpencliDir(): string {
|
|
24
|
-
return `${toolchainDir()}/node_modules/${OPENCLI_NPM_PACKAGE}`;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export function vendorPrefix(): string {
|
|
28
|
-
return `${isolatedRoot()}/plugins-npm`;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
export function vendorPluginsDir(): string {
|
|
32
|
-
return `${vendorPrefix()}/node_modules`;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export const OPENCLI_CATALOG_ENV = 'DSH_WB_OPENCLI_CATALOG';
|
|
36
|
-
|
|
37
|
-
export function cliCatalogPath(): string {
|
|
38
|
-
const override = process.env[OPENCLI_CATALOG_ENV]?.trim();
|
|
39
|
-
return override === undefined || override === ''
|
|
40
|
-
? `${toolchainOpencliDir()}/cli-manifest.json`
|
|
41
|
-
: override;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
export function vendorPeerLink(): { readonly dir: string; readonly parent: string } {
|
|
45
|
-
const base = `${vendorPrefix()}/node_modules`;
|
|
46
|
-
const [scope] = OPENCLI_NPM_PACKAGE.split('/');
|
|
47
|
-
return { dir: `${base}/${OPENCLI_NPM_PACKAGE}`, parent: `${base}/${scope}` };
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
export function npmCacheDir(): string {
|
|
51
|
-
return `${isolatedRoot()}/npm-cache`;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
export function isolatedCommand(): string {
|
|
55
|
-
return `${isolatedRoot()}/bin/opencli`;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
export function libsPrefix(): string {
|
|
59
|
-
return `${isolatedRoot()}/libs`;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export function libDir(name: string): string {
|
|
63
|
-
return `${libsPrefix()}/node_modules/${name}`;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
export function libInstallCommand(options: {
|
|
67
|
-
readonly need: UnitLibNeed;
|
|
68
|
-
readonly version?: string | undefined;
|
|
69
|
-
}): string {
|
|
70
|
-
const { need, version } = options;
|
|
71
|
-
const registry =
|
|
72
|
-
process.env[need.registryEnv]?.trim() || need.registryDefault;
|
|
73
|
-
const spec = version ?? need.version ?? 'latest';
|
|
74
|
-
return `npm i --prefix '${libsPrefix()}' ${quoteShellArg(`${need.name}@${spec}`)} --registry '${registry}' --cache '${npmCacheDir()}'`;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
export function isolatedInstallCommand(): string {
|
|
78
|
-
return `npm i --prefix '${toolchainDir()}' ${OPENCLI_NPM_PACKAGE}@${OPENCLI_PIN} --cache '${npmCacheDir()}'`;
|
|
79
|
-
}
|