@dsh-workbench/shared 1.5.2-alpha.4 → 1.5.2-rc.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/dist/components/Activity.css +69 -0
- package/dist/components/Activity.d.ts +56 -0
- package/dist/components/Activity.js +47 -0
- package/dist/components/ApprovalDetail.css +3 -0
- package/dist/components/ApprovalDetail.d.ts +42 -0
- package/dist/components/ApprovalDetail.js +53 -0
- package/dist/components/ApprovalPlan.css +39 -0
- package/dist/components/ApprovalPlan.d.ts +27 -0
- package/dist/components/ApprovalPlan.js +19 -0
- package/dist/components/Card.css +19 -0
- package/dist/components/Card.d.ts +26 -0
- package/dist/components/Card.js +18 -0
- package/dist/components/CardAnchor.d.ts +21 -0
- package/dist/components/CardAnchor.js +19 -0
- package/dist/components/CardAnchor.module.css +3 -0
- package/dist/components/CardBoundary.d.ts +42 -0
- package/dist/components/CardBoundary.js +58 -0
- package/dist/components/CardBoundary.module.css +5 -0
- package/dist/components/CardHead.css +87 -0
- package/dist/components/CardHead.d.ts +30 -0
- package/dist/components/CardHead.js +27 -0
- package/dist/components/ChoiceGroup.css +182 -0
- package/dist/components/ChoiceGroup.d.ts +54 -0
- package/dist/components/ChoiceGroup.js +63 -0
- package/dist/components/ConfirmBar.css +26 -0
- package/dist/components/ConfirmBar.d.ts +36 -0
- package/dist/components/ConfirmBar.js +26 -0
- package/dist/components/EmptyIllustration.d.ts +12 -0
- package/dist/components/EmptyIllustration.js +32 -0
- package/dist/components/Field.css +20 -0
- package/dist/components/Field.d.ts +18 -0
- package/dist/components/Field.js +15 -0
- package/dist/components/Form.css +6 -0
- package/dist/components/Form.d.ts +22 -0
- package/dist/components/Form.js +60 -0
- package/dist/components/FormActions.css +13 -0
- package/dist/components/FormActions.d.ts +29 -0
- package/dist/components/FormActions.js +43 -0
- package/dist/components/FormNodes.d.ts +107 -0
- package/dist/components/FormNodes.js +165 -0
- package/dist/components/FormRows.css +107 -0
- package/dist/components/FormRows.d.ts +36 -0
- package/dist/components/FormRows.js +26 -0
- package/dist/components/FormTypes.d.ts +84 -0
- package/dist/components/FormTypes.js +1 -0
- package/dist/components/Link.css +22 -0
- package/dist/components/Link.d.ts +22 -0
- package/dist/components/Link.js +17 -0
- package/dist/components/LongTaskRow.css +171 -0
- package/dist/components/LongTaskRow.d.ts +94 -0
- package/dist/components/LongTaskRow.js +103 -0
- package/dist/components/Note.css +13 -0
- package/dist/components/Note.d.ts +20 -0
- package/dist/components/Note.js +14 -0
- package/dist/components/ResourceStates.css +114 -0
- package/dist/components/ResourceStates.d.ts +44 -0
- package/dist/components/ResourceStates.js +41 -0
- package/dist/components/Rows.css +42 -0
- package/dist/components/Rows.d.ts +28 -0
- package/dist/components/Rows.js +21 -0
- package/dist/components/SaveBar.d.ts +24 -0
- package/dist/components/SaveBar.js +25 -0
- package/dist/components/SelectField.css +182 -0
- package/dist/components/SelectField.d.ts +89 -0
- package/dist/components/SelectField.js +260 -0
- package/dist/components/SelectFieldNodes.d.ts +49 -0
- package/dist/components/SelectFieldNodes.js +43 -0
- package/dist/components/SettingsSection.css +25 -0
- package/dist/components/SettingsSection.d.ts +20 -0
- package/dist/components/SettingsSection.js +17 -0
- package/dist/components/SetupShell.d.ts +93 -0
- package/dist/components/SetupShell.js +64 -0
- package/dist/components/SetupShell.module.css +81 -0
- package/dist/components/Spinner.css +5 -0
- package/dist/components/Spinner.d.ts +15 -0
- package/dist/components/Spinner.js +13 -0
- package/dist/components/Stack.css +6 -0
- package/dist/components/Stack.d.ts +14 -0
- package/dist/components/Stack.js +10 -0
- package/dist/components/StateBlock.css +38 -0
- package/dist/components/StateBlock.d.ts +24 -0
- package/dist/components/StateBlock.js +22 -0
- package/dist/components/TextField.css +32 -0
- package/dist/components/TextField.d.ts +14 -0
- package/dist/components/TextField.js +10 -0
- package/dist/components/board-module.d.ts +307 -0
- package/dist/components/board-module.js +1 -0
- package/dist/components/card-view-validate.d.ts +5 -0
- package/dist/components/card-view-validate.js +256 -0
- package/dist/components/icons.d.ts +63 -0
- package/dist/components/icons.js +107 -0
- package/dist/components/layout/icon-command.d.ts +8 -0
- package/dist/components/layout/icon-command.js +21 -0
- package/dist/contract/capture.d.ts +16 -0
- package/dist/contract/capture.js +1 -0
- package/dist/contract/diagnostics.d.ts +34 -0
- package/dist/contract/diagnostics.js +124 -0
- package/dist/contract/needs.d.ts +3 -1
- package/dist/contract/needs.js +4 -2
- package/dist/contract/protocol/bundle-install.d.ts +1 -1
- package/dist/contract/protocol/unit-manager.d.ts +50 -0
- package/dist/contract/protocol/unit-manager.js +2 -0
- package/dist/contract/unit.js +2 -1
- package/dist/host/adapter-contract.d.ts +3 -0
- package/dist/host/adapter-contract.js +95 -0
- package/dist/host/adapter-set.d.ts +27 -0
- package/dist/host/adapter-set.js +258 -0
- package/dist/host/adapter-types.d.ts +87 -0
- package/dist/host/adapter-types.js +23 -0
- package/dist/host/adapters/platform.d.ts +14 -0
- package/dist/host/adapters/platform.js +40 -0
- package/dist/host/adapters.d.ts +28 -0
- package/dist/host/adapters.js +9 -0
- package/dist/host/auth-loss.d.ts +33 -0
- package/dist/host/auth-loss.js +107 -0
- package/dist/host/board-fact.d.ts +8 -0
- package/dist/host/board-fact.js +16 -0
- package/dist/host/boot-record.d.ts +11 -0
- package/dist/host/boot-record.js +31 -0
- package/dist/host/browser.d.ts +3 -0
- package/dist/host/browser.js +3 -0
- package/dist/host/bundle-manager.d.ts +4 -0
- package/dist/host/bundle-manager.js +338 -0
- package/dist/host/changelog.d.ts +28 -0
- package/dist/host/changelog.js +201 -0
- package/dist/host/cli-error.d.ts +8 -0
- package/dist/host/cli-error.js +70 -0
- package/dist/host/cli.d.ts +4 -0
- package/dist/host/cli.js +9 -0
- package/dist/host/config-sync.d.ts +40 -0
- package/dist/host/config-sync.js +267 -0
- package/dist/host/context-ref.d.ts +13 -0
- package/dist/host/context-ref.js +41 -0
- package/dist/host/data-error.d.ts +15 -0
- package/dist/host/data-error.js +23 -0
- package/dist/host/data-plan-check.d.ts +10 -0
- package/dist/host/data-plan-check.js +315 -0
- package/dist/host/data-plan.d.ts +15 -0
- package/dist/host/data-plan.js +283 -0
- package/dist/host/data-root.d.ts +5 -0
- package/dist/host/data-root.js +23 -0
- package/dist/host/diy-debug.d.ts +80 -0
- package/dist/host/diy-debug.js +89 -0
- package/dist/host/diy-preset.d.ts +26 -0
- package/dist/host/diy-preset.js +115 -0
- package/dist/host/diy-record.d.ts +135 -0
- package/dist/host/diy-record.js +163 -0
- package/dist/host/dsh-compat.d.ts +27 -0
- package/dist/host/dsh-compat.js +75 -0
- package/dist/host/dsh-plugins.d.ts +5 -0
- package/dist/host/dsh-plugins.js +51 -0
- package/dist/host/env-policy.d.ts +82 -0
- package/dist/host/env-policy.js +181 -0
- package/dist/host/extension.d.ts +7 -0
- package/dist/host/extension.js +40 -0
- package/dist/host/facts.d.ts +31 -0
- package/dist/host/facts.js +58 -0
- package/dist/host/fs-write.d.ts +96 -0
- package/dist/host/fs-write.js +183 -0
- package/dist/host/git.d.ts +5 -0
- package/dist/host/git.js +1 -0
- package/dist/host/host-actions.d.ts +2 -0
- package/dist/host/host-actions.js +5 -0
- package/dist/host/host-kit.d.ts +37 -0
- package/dist/host/host-kit.js +27 -0
- package/dist/host/http.d.ts +47 -0
- package/dist/host/http.js +289 -0
- package/dist/host/install-failure.d.ts +50 -0
- package/dist/host/install-failure.js +184 -0
- package/dist/host/installed-module.d.ts +25 -0
- package/dist/host/installed-module.js +289 -0
- package/dist/host/installed-sources.d.ts +10 -0
- package/dist/host/installed-sources.js +63 -0
- package/dist/host/lease.d.ts +70 -0
- package/dist/host/lease.js +264 -0
- package/dist/host/lib-capabilities.d.ts +22 -0
- package/dist/host/lib-capabilities.js +37 -0
- package/dist/host/lib-install.d.ts +19 -0
- package/dist/host/lib-install.js +40 -0
- package/dist/host/lib-runners.d.ts +18 -0
- package/dist/host/lib-runners.js +46 -0
- package/dist/host/local-declaration.d.ts +7 -0
- package/dist/host/local-declaration.js +33 -0
- package/dist/host/long-task.d.ts +97 -0
- package/dist/host/long-task.js +297 -0
- package/dist/host/market-index-claim.d.ts +19 -0
- package/dist/host/market-index-claim.js +87 -0
- package/dist/host/market-index-doc.d.ts +12 -0
- package/dist/host/market-index-doc.js +1 -0
- package/dist/host/market-index-parse.d.ts +28 -0
- package/dist/host/market-index-parse.js +413 -0
- package/dist/host/market-index.d.ts +13 -0
- package/dist/host/market-index.js +105 -0
- package/dist/host/module-root.d.ts +1 -0
- package/dist/host/module-root.js +5 -0
- package/dist/host/needs.d.ts +4 -0
- package/dist/host/needs.js +2 -0
- package/dist/host/npm-registry.d.ts +10 -0
- package/dist/host/npm-registry.js +16 -0
- package/dist/host/package-content.d.ts +8 -0
- package/dist/host/package-content.js +90 -0
- package/dist/host/package-kind.d.ts +13 -0
- package/dist/host/package-kind.js +51 -0
- package/dist/host/package-route.d.ts +33 -0
- package/dist/host/package-route.js +68 -0
- package/dist/host/panel-state.d.ts +120 -0
- package/dist/host/panel-state.js +177 -0
- package/dist/host/patch-blocks.d.ts +13 -0
- package/dist/host/patch-blocks.js +52 -0
- package/dist/host/patch.d.ts +4 -0
- package/dist/host/patch.js +7 -0
- package/dist/host/paths.d.ts +12 -0
- package/dist/host/paths.js +20 -0
- package/dist/host/payload.d.ts +12 -0
- package/dist/host/payload.js +16 -0
- package/dist/host/plan-results.d.ts +5 -0
- package/dist/host/plan-results.js +71 -0
- package/dist/host/plan-run.d.ts +41 -0
- package/dist/host/plan-run.js +87 -0
- package/dist/host/platform.d.ts +14 -0
- package/dist/host/platform.js +40 -0
- package/dist/host/plugin-ledger.d.ts +32 -0
- package/dist/host/plugin-ledger.js +106 -0
- package/dist/host/plugin.d.ts +3 -0
- package/dist/host/plugin.js +38 -0
- package/dist/host/port-types.d.ts +233 -0
- package/dist/host/port-types.js +1 -0
- package/dist/host/preflight.d.ts +32 -0
- package/dist/host/preflight.js +1 -0
- package/dist/host/process-alive.d.ts +6 -0
- package/dist/host/process-alive.js +16 -0
- package/dist/host/profile-bundles.d.ts +20 -0
- package/dist/host/profile-bundles.js +48 -0
- package/dist/host/profile-create.d.ts +19 -0
- package/dist/host/profile-create.js +124 -0
- package/dist/host/profile-port.d.ts +4 -0
- package/dist/host/profile-port.js +4 -0
- package/dist/host/profile-registries.d.ts +10 -0
- package/dist/host/profile-registries.js +149 -0
- package/dist/host/provision.d.ts +32 -0
- package/dist/host/provision.js +105 -0
- package/dist/host/read-json.d.ts +1 -0
- package/dist/host/read-json.js +11 -0
- package/dist/host/recipe-rules.d.ts +5 -0
- package/dist/host/recipe-rules.js +412 -0
- package/dist/host/recipe.d.ts +14 -0
- package/dist/host/recipe.js +332 -0
- package/dist/host/registry-declaration.d.ts +21 -0
- package/dist/host/registry-declaration.js +42 -0
- package/dist/host/reinstall-record.d.ts +16 -0
- package/dist/host/reinstall-record.js +54 -0
- package/dist/host/release-age.d.ts +24 -0
- package/dist/host/release-age.js +92 -0
- package/dist/host/release-packages.generated.d.ts +1 -0
- package/dist/host/release-packages.generated.js +10 -0
- package/dist/host/release-probe.d.ts +37 -0
- package/dist/host/release-probe.js +156 -0
- package/dist/host/runtime/browser-argv.d.ts +1 -0
- package/dist/host/runtime/browser-argv.js +9 -0
- package/dist/host/runtime/dsh-compat.d.ts +4 -0
- package/dist/host/runtime/dsh-compat.js +32 -16
- package/dist/host/runtime/front-app.d.ts +6 -0
- package/dist/host/runtime/front-app.js +70 -0
- package/dist/host/runtime/verified-dsh.d.ts +5 -0
- package/dist/host/runtime/verified-dsh.js +20 -0
- package/dist/host/shell-render.d.ts +52 -0
- package/dist/host/shell-render.js +117 -0
- package/dist/host/shell-text.d.ts +30 -0
- package/dist/host/shell-text.js +40 -0
- package/dist/host/shell.d.ts +25 -0
- package/dist/host/shell.js +55 -0
- package/dist/host/site.d.ts +45 -0
- package/dist/host/site.js +182 -0
- package/dist/host/spec.d.ts +8 -0
- package/dist/host/spec.js +24 -0
- package/dist/host/submit-confirm.d.ts +19 -0
- package/dist/host/submit-confirm.js +45 -0
- package/dist/host/supply/builtin-catalog.d.ts +3 -0
- package/dist/host/supply/builtin-catalog.js +38 -0
- package/dist/host/supply/preflight.d.ts +14 -1
- package/dist/host/supply/supply-index.js +6 -0
- package/dist/host/supply-index.d.ts +38 -0
- package/dist/host/supply-index.js +209 -0
- package/dist/host/supply-port.d.ts +5 -0
- package/dist/host/supply-port.js +50 -0
- package/dist/host/time.d.ts +22 -0
- package/dist/host/time.js +33 -0
- package/dist/host/tokens.d.ts +26 -0
- package/dist/host/tokens.js +154 -0
- package/dist/host/tool-approval.d.ts +86 -0
- package/dist/host/tool-approval.js +118 -0
- package/dist/host/tool-kit.d.ts +8 -0
- package/dist/host/tool-kit.js +22 -0
- package/dist/host/toolbox-port.d.ts +16 -0
- package/dist/host/toolbox-port.js +1 -0
- package/dist/host/unit-config.d.ts +34 -0
- package/dist/host/unit-config.js +28 -0
- package/dist/host/unit-data.d.ts +3 -0
- package/dist/host/unit-data.js +127 -0
- package/dist/host/unit-face.d.ts +95 -0
- package/dist/host/unit-face.js +1 -0
- package/dist/host/unit-plugin.d.ts +24 -0
- package/dist/host/unit-plugin.js +25 -0
- package/dist/host/update-journal-manifest.d.ts +19 -0
- package/dist/host/update-journal-manifest.js +134 -0
- package/dist/host/update-journal-types.d.ts +33 -0
- package/dist/host/update-journal-types.js +2 -0
- package/dist/host/update-journal.d.ts +43 -0
- package/dist/host/update-journal.js +409 -0
- package/dist/host/value-handoff.d.ts +16 -0
- package/dist/host/value-handoff.js +22 -0
- package/dist/host/version-range.d.ts +2 -0
- package/dist/host/version-range.js +13 -0
- package/dist/host/version.d.ts +4 -0
- package/dist/host/version.js +32 -0
- package/dist/host/workspace-store.d.ts +19 -0
- package/dist/host/workspace-store.js +133 -0
- package/dist/host/write-result.d.ts +1 -0
- package/dist/host/write-result.js +18 -0
- package/dist/host.d.ts +2 -0
- package/dist/host.js +2 -0
- package/dist/i18n/en.js +14 -3
- package/dist/i18n/index.d.ts +15 -4
- package/dist/i18n/zh.d.ts +14 -3
- package/dist/i18n/zh.js +14 -3
- package/dist/lib/bundle-requirements.d.ts +1 -0
- package/dist/lib/bundle-requirements.js +9 -0
- package/package.json +1 -1
- package/src/contract/__tests__/unit.test.ts +7 -0
- package/src/contract/needs.ts +5 -3
- package/src/contract/protocol/bundle-install.ts +1 -0
- package/src/contract/unit.ts +2 -1
- package/src/host/runtime/__tests__/dsh-compat.test.ts +19 -1
- package/src/host/runtime/__tests__/verified-dsh.test.ts +55 -0
- package/src/host/runtime/dsh-compat.ts +36 -12
- package/src/host/runtime/verified-dsh.ts +22 -0
- package/src/host/supply/__tests__/builtin-catalog.test.ts +44 -0
- package/src/host/supply/builtin-catalog.ts +38 -0
- package/src/host/supply/preflight.ts +16 -1
- package/src/host/supply/supply-index.ts +3 -0
- package/src/host.ts +2 -0
- package/src/i18n/en.ts +17 -3
- package/src/i18n/zh.ts +16 -3
- package/src/lib/bundle-requirements.ts +11 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
export { APPROVAL_NOTE_ARG } from '../contract/approval.js';
|
|
2
|
+
import { APPROVAL_NOTE_ARG } from '../contract/approval.js';
|
|
3
|
+
export const APPROVAL_NOTE_LIMIT = 40;
|
|
4
|
+
export const APPROVAL_DETAIL_LIMIT = 20;
|
|
5
|
+
export const APPROVAL_QUESTION_LIMIT = 40;
|
|
6
|
+
export const APPROVAL_SEPARATOR_LIMIT = 8;
|
|
7
|
+
export const APPROVAL_HEADLINE_LIMIT = APPROVAL_DETAIL_LIMIT + APPROVAL_QUESTION_LIMIT + APPROVAL_NOTE_LIMIT + APPROVAL_SEPARATOR_LIMIT;
|
|
8
|
+
const NOTE_SCHEMA = {
|
|
9
|
+
type: 'string',
|
|
10
|
+
description: '一句话说明你这次想拿它干什么(要点头时显示在审批屏上;40 字以内)。',
|
|
11
|
+
};
|
|
12
|
+
export function withApprovalNote(parameters, options = {}) {
|
|
13
|
+
const properties = parameters['properties'];
|
|
14
|
+
if (typeof properties !== 'object' || properties === null)
|
|
15
|
+
return parameters;
|
|
16
|
+
const map = properties;
|
|
17
|
+
if (APPROVAL_NOTE_ARG in map)
|
|
18
|
+
return parameters;
|
|
19
|
+
const declared = Array.isArray(parameters['required']) ? parameters['required'] : [];
|
|
20
|
+
const kept = declared.filter((name) => name !== APPROVAL_NOTE_ARG);
|
|
21
|
+
const required = options.required === true ? [...kept, APPROVAL_NOTE_ARG] : kept;
|
|
22
|
+
return {
|
|
23
|
+
...parameters,
|
|
24
|
+
properties: { ...map, [APPROVAL_NOTE_ARG]: NOTE_SCHEMA },
|
|
25
|
+
...(required.length === 0 ? {} : { required }),
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function oneLine(text) {
|
|
29
|
+
return text.replace(/\s+/g, ' ').trim();
|
|
30
|
+
}
|
|
31
|
+
function approvalNoteOf(call) {
|
|
32
|
+
const raw = call.arguments;
|
|
33
|
+
const value = typeof raw === 'object' && raw !== null ? raw[APPROVAL_NOTE_ARG] : undefined;
|
|
34
|
+
if (typeof value !== 'string')
|
|
35
|
+
return undefined;
|
|
36
|
+
const line = oneLine(value);
|
|
37
|
+
if (line === '')
|
|
38
|
+
return undefined;
|
|
39
|
+
return line.length > APPROVAL_NOTE_LIMIT ? `${line.slice(0, APPROVAL_NOTE_LIMIT)}…` : line;
|
|
40
|
+
}
|
|
41
|
+
function messageOf(error) {
|
|
42
|
+
return error instanceof Error ? error.message : String(error);
|
|
43
|
+
}
|
|
44
|
+
function compose(detail, verdict, note) {
|
|
45
|
+
const base = detail === undefined || typeof detail.zh !== 'string' || detail.zh.trim() === ''
|
|
46
|
+
? verdict.fallback
|
|
47
|
+
: {
|
|
48
|
+
en: `${detail.en} — ${verdict.question.en}`,
|
|
49
|
+
zh: `${detail.zh} —— ${verdict.question.zh}`,
|
|
50
|
+
};
|
|
51
|
+
const line = note === undefined ? base : { en: `${base.en} — ${note}`, zh: `${base.zh} —— ${note}` };
|
|
52
|
+
return { en: oneLine(line.en), zh: oneLine(line.zh) };
|
|
53
|
+
}
|
|
54
|
+
export async function approvalFor(call, context = {}) {
|
|
55
|
+
const asked = context.writes === true || context.elevates === true;
|
|
56
|
+
if (!asked)
|
|
57
|
+
return undefined;
|
|
58
|
+
let verdict;
|
|
59
|
+
try {
|
|
60
|
+
verdict = context.policy?.(call);
|
|
61
|
+
}
|
|
62
|
+
catch (error) {
|
|
63
|
+
return {
|
|
64
|
+
kind: 'deny',
|
|
65
|
+
reason: `问"要不要审批"的时候套件的策略抛了错,已经停下:${messageOf(error)}`,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (verdict?.kind === 'read' || verdict?.kind === 'allow')
|
|
69
|
+
return undefined;
|
|
70
|
+
if (verdict?.kind === 'deny')
|
|
71
|
+
return { kind: 'deny', reason: verdict.reason };
|
|
72
|
+
if (verdict?.kind !== 'ask') {
|
|
73
|
+
return {
|
|
74
|
+
kind: 'deny',
|
|
75
|
+
reason: `${call.name ?? '这把工具'} 在清单里声明` +
|
|
76
|
+
(context.elevates === true ? '会抬高工具面' : '会写东西') +
|
|
77
|
+
',但它的套件没认领这次调用 —— 停下:' +
|
|
78
|
+
'在套件的 needsApproval 里补一条审批(含"这次会发生什么"),别让它静默过去',
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
let facts;
|
|
82
|
+
try {
|
|
83
|
+
facts = await context.detailOf?.(call);
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
return { kind: 'deny', reason: `拿不到"这次会发生什么",已经停下:${messageOf(error)}` };
|
|
87
|
+
}
|
|
88
|
+
if (facts !== undefined && 'blocked' in facts)
|
|
89
|
+
return { kind: 'deny', reason: facts.blocked };
|
|
90
|
+
if (context.skipAsk === true)
|
|
91
|
+
return undefined;
|
|
92
|
+
const note = context.declaredNote === true && verdict.noteInDetail !== true ? approvalNoteOf(call) : undefined;
|
|
93
|
+
return {
|
|
94
|
+
kind: 'ask',
|
|
95
|
+
reason: verdict.reason,
|
|
96
|
+
displayReason: compose(facts, verdict, note),
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* 「写工具要点头」那一对的默认口径:点名的工具一律 `ask` + 同一份明细,其余一律 `read`。
|
|
101
|
+
* 把它直接摊在 `mount()` 的返回值里:`...writeApprovalOf({ … })`。
|
|
102
|
+
* @param options.tools 需要点头的工具名。
|
|
103
|
+
* @param options.reason 为什么问(给 agent 看的理由)。
|
|
104
|
+
* @param options.question 问句(zh / en,≤40 字)。
|
|
105
|
+
* @param options.fallback 没有明细时的整句(zh / en,≤40 字)。
|
|
106
|
+
* @param options.detail 审批屏头行那句明细(zh / en,≤20 字)。
|
|
107
|
+
* @returns `{ needsApproval, approvalDetail }`。
|
|
108
|
+
*/
|
|
109
|
+
export function writeApprovalOf(options) {
|
|
110
|
+
const tools = new Set(options.tools);
|
|
111
|
+
const named = (call) => call.name !== undefined && tools.has(call.name);
|
|
112
|
+
return {
|
|
113
|
+
needsApproval: (call) => named(call)
|
|
114
|
+
? { kind: 'ask', reason: options.reason, question: options.question, fallback: options.fallback }
|
|
115
|
+
: { kind: 'read' },
|
|
116
|
+
approvalDetail: (call) => (named(call) ? options.detail : undefined),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ToolOutputDefinition, ToolRunContext } from '../contract/module-context.js';
|
|
2
|
+
import type { ContentBlock } from '@deepseek-ai/dsh-llm';
|
|
3
|
+
export declare function sessionWorkspace(exec: ToolRunContext | undefined): string | null;
|
|
4
|
+
export declare function sessionIdOf(exec: ToolRunContext | undefined): string | null;
|
|
5
|
+
export declare function textOutput(text: string): ContentBlock[];
|
|
6
|
+
export declare function textRender(_args: unknown, value: unknown): ContentBlock[];
|
|
7
|
+
export declare const TEXT_OUTPUT: ToolOutputDefinition;
|
|
8
|
+
export declare function jsonText(value: unknown): string;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function sessionWorkspace(exec) {
|
|
2
|
+
const cwd = exec?.agent?.session?.header?.cwd;
|
|
3
|
+
return typeof cwd === 'string' && cwd.trim() !== '' ? cwd.trim() : null;
|
|
4
|
+
}
|
|
5
|
+
export function sessionIdOf(exec) {
|
|
6
|
+
const id = exec?.agent?.session?.id;
|
|
7
|
+
return typeof id === 'string' && id.trim() !== '' ? id.trim() : null;
|
|
8
|
+
}
|
|
9
|
+
export function textOutput(text) {
|
|
10
|
+
return [{ type: 'text', text }];
|
|
11
|
+
}
|
|
12
|
+
export function textRender(_args, value) {
|
|
13
|
+
const text = value?.text;
|
|
14
|
+
return textOutput(typeof text === 'string' ? text : JSON.stringify(value));
|
|
15
|
+
}
|
|
16
|
+
export const TEXT_OUTPUT = {
|
|
17
|
+
schema: { type: 'object', properties: {}, additionalProperties: true },
|
|
18
|
+
render: textRender,
|
|
19
|
+
};
|
|
20
|
+
export function jsonText(value) {
|
|
21
|
+
return JSON.stringify(value);
|
|
22
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export interface ToolboxCapability {
|
|
2
|
+
readonly name: string;
|
|
3
|
+
readonly description: string;
|
|
4
|
+
readonly content: string;
|
|
5
|
+
}
|
|
6
|
+
export interface ToolboxPort {
|
|
7
|
+
packsOf(session: string | null, preset: string | null): ReadonlySet<string>;
|
|
8
|
+
capabilityBrief?(workspace: string | null): ToolboxCapability;
|
|
9
|
+
}
|
|
10
|
+
export interface McpServerPacks {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
readonly tools: readonly string[];
|
|
13
|
+
}
|
|
14
|
+
export interface McpToolsPort {
|
|
15
|
+
servers(): readonly McpServerPacks[];
|
|
16
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 单元配置的落盘接口:`deps.store.for(unit, workspace)` 交出来的那个形状。
|
|
3
|
+
*/
|
|
4
|
+
export interface UnitConfigStore {
|
|
5
|
+
read(name: string): unknown | null;
|
|
6
|
+
write(name: string, value: unknown): void;
|
|
7
|
+
remove(name: string): void;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* `createUnitConfig` 的入参。
|
|
11
|
+
*/
|
|
12
|
+
export interface UnitConfigOptions<T> {
|
|
13
|
+
readonly store: UnitConfigStore;
|
|
14
|
+
readonly file: string;
|
|
15
|
+
readonly fallback: T;
|
|
16
|
+
readonly normalize: (raw: unknown) => T;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* 一份单元配置的读写器:读坏了退回兜底,写入前先规范化。
|
|
20
|
+
*/
|
|
21
|
+
export interface UnitConfig<T> {
|
|
22
|
+
read(): T;
|
|
23
|
+
write(payload: unknown): T;
|
|
24
|
+
reset(): void;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* 把「一个 JSON 文件 + 一份兜底 + 一个规范化函数」接成模块配置的读写器。
|
|
28
|
+
* @param options.store - 落盘位置:模块自己的 `deps.store.for(unit, workspace)`。
|
|
29
|
+
* @param options.file - 文件名(如 `config.json`)。
|
|
30
|
+
* @param options.fallback - 没存过或存坏了时返回的那份。
|
|
31
|
+
* @param options.normalize - 把任意输入收成 `T`;写口不合法时直接抛。
|
|
32
|
+
* @returns 读 / 写 / 清空三件事。
|
|
33
|
+
*/
|
|
34
|
+
export declare function createUnitConfig<T>({ store, file, fallback, normalize, }: UnitConfigOptions<T>): UnitConfig<T>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 把「一个 JSON 文件 + 一份兜底 + 一个规范化函数」接成模块配置的读写器。
|
|
3
|
+
* @param options.store - 落盘位置:模块自己的 `deps.store.for(unit, workspace)`。
|
|
4
|
+
* @param options.file - 文件名(如 `config.json`)。
|
|
5
|
+
* @param options.fallback - 没存过或存坏了时返回的那份。
|
|
6
|
+
* @param options.normalize - 把任意输入收成 `T`;写口不合法时直接抛。
|
|
7
|
+
* @returns 读 / 写 / 清空三件事。
|
|
8
|
+
*/
|
|
9
|
+
export function createUnitConfig({ store, file, fallback, normalize, }) {
|
|
10
|
+
return {
|
|
11
|
+
read() {
|
|
12
|
+
try {
|
|
13
|
+
return normalize(store.read(file));
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return fallback;
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
write(payload) {
|
|
20
|
+
const value = normalize(payload);
|
|
21
|
+
store.write(file, value);
|
|
22
|
+
return value;
|
|
23
|
+
},
|
|
24
|
+
reset() {
|
|
25
|
+
store.remove(file);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { allSame } from './plan-results.js';
|
|
2
|
+
import { resolvePlanOf, resolveRecipeOf } from './site.js';
|
|
3
|
+
function denied(origin, write = false) {
|
|
4
|
+
return {
|
|
5
|
+
ok: false,
|
|
6
|
+
error: {
|
|
7
|
+
code: 'invalid-request',
|
|
8
|
+
message: write
|
|
9
|
+
? `这个模块没声明写 ${origin}:在清单 host.origins 里加上它`
|
|
10
|
+
: `这个模块没声明读 ${origin}:在清单 host.origins 里加上它`,
|
|
11
|
+
pageCode: 'origin-not-declared',
|
|
12
|
+
},
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function unreadable(origin) {
|
|
16
|
+
return {
|
|
17
|
+
ok: false,
|
|
18
|
+
error: {
|
|
19
|
+
code: 'invalid-request',
|
|
20
|
+
message: `读不了 ${origin}:它不是一个能解析的地址(要带 http / https)`,
|
|
21
|
+
pageCode: 'origin-invalid',
|
|
22
|
+
},
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function unresolved(problem) {
|
|
26
|
+
return {
|
|
27
|
+
ok: false,
|
|
28
|
+
error: { code: problem.code, message: problem.message, pageCode: problem.pageCode },
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
function failuresOf(calls, error) {
|
|
32
|
+
const names = calls !== null && typeof calls === 'object' ? Object.keys(calls) : [];
|
|
33
|
+
return Object.fromEntries(names.map((name) => [name, { ok: false, error }]));
|
|
34
|
+
}
|
|
35
|
+
function planDenied(plan, origin, write = false) {
|
|
36
|
+
const error = originOfValue(origin) === undefined ? unreadable(origin).error : denied(origin, write).error;
|
|
37
|
+
return allSame(plan, error);
|
|
38
|
+
}
|
|
39
|
+
function originOfValue(value) {
|
|
40
|
+
try {
|
|
41
|
+
return new URL(value).origin;
|
|
42
|
+
}
|
|
43
|
+
catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
export function scopedDataReader(data, origins, sites) {
|
|
48
|
+
if (origins === undefined && sites === undefined)
|
|
49
|
+
return data;
|
|
50
|
+
const originOf = originOfValue;
|
|
51
|
+
const siteOrigins = sites === undefined
|
|
52
|
+
? []
|
|
53
|
+
: Object.values(sites).flatMap((site) => typeof site?.origin === 'string' && site.origin.trim() !== '' ? [site.origin] : []);
|
|
54
|
+
const declared = origins === undefined ? (siteOrigins.length === 0 ? undefined : siteOrigins) : origins;
|
|
55
|
+
const allowed = declared === undefined ? undefined : new Set(declared.map(originOf).filter((one) => one !== undefined));
|
|
56
|
+
const allows = (value) => {
|
|
57
|
+
const origin = originOf(value);
|
|
58
|
+
return origin !== undefined && (allowed === undefined || allowed.has(origin));
|
|
59
|
+
};
|
|
60
|
+
const denyOf = (value, write = false) => originOf(value) === undefined ? unreadable(value) : denied(value, write);
|
|
61
|
+
const shapedPlan = (plan) => typeof plan === 'object' && plan !== null && Array.isArray(plan.steps) ? undefined : '计划要是对象且带 steps';
|
|
62
|
+
const planInputOf = (plan, write) => {
|
|
63
|
+
const bad = shapedPlan(plan);
|
|
64
|
+
if (bad !== undefined)
|
|
65
|
+
return { failure: allSame(plan, { code: 'invalid-request', message: bad }) };
|
|
66
|
+
const resolved = resolvePlanOf(plan, sites);
|
|
67
|
+
if (!resolved.ok)
|
|
68
|
+
return { failure: allSame(plan, unresolved(resolved.problem).error) };
|
|
69
|
+
if (!allows(resolved.value.origin))
|
|
70
|
+
return { failure: planDenied(plan, resolved.value.origin, write) };
|
|
71
|
+
return { plan: resolved.value };
|
|
72
|
+
};
|
|
73
|
+
const scoped = {
|
|
74
|
+
read: (request, options) => {
|
|
75
|
+
if (typeof request !== 'object' || request === null) {
|
|
76
|
+
return Promise.resolve({
|
|
77
|
+
ok: false,
|
|
78
|
+
error: { code: 'invalid-request', message: '请求要是对象' },
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
const raw = request.kind === 'http' ? request.url : request.kind === 'browser' ? request.url : undefined;
|
|
82
|
+
const target = raw === undefined || raw === '' ? undefined : raw;
|
|
83
|
+
if (target !== undefined && !allows(target))
|
|
84
|
+
return Promise.resolve(denyOf(target));
|
|
85
|
+
return data.read(request, options);
|
|
86
|
+
},
|
|
87
|
+
request: (recipe, overrides, options) => {
|
|
88
|
+
const resolved = resolveRecipeOf(recipe, sites);
|
|
89
|
+
if (!resolved.ok)
|
|
90
|
+
return Promise.resolve(unresolved(resolved.problem));
|
|
91
|
+
if (!allows(resolved.value.origin))
|
|
92
|
+
return Promise.resolve(denyOf(resolved.value.origin));
|
|
93
|
+
return data.request(resolved.value, overrides, options);
|
|
94
|
+
},
|
|
95
|
+
requestMany: (recipe, calls, options) => {
|
|
96
|
+
const resolved = resolveRecipeOf(recipe, sites);
|
|
97
|
+
if (!resolved.ok) {
|
|
98
|
+
return Promise.resolve(failuresOf(calls, unresolved(resolved.problem).error));
|
|
99
|
+
}
|
|
100
|
+
if (!allows(resolved.value.origin)) {
|
|
101
|
+
return Promise.resolve(failuresOf(calls, denyOf(resolved.value.origin).error));
|
|
102
|
+
}
|
|
103
|
+
return data.requestMany(resolved.value, calls, options);
|
|
104
|
+
},
|
|
105
|
+
plan: (plan, options) => {
|
|
106
|
+
const input = planInputOf(plan, false);
|
|
107
|
+
if ('failure' in input)
|
|
108
|
+
return Promise.resolve(input.failure);
|
|
109
|
+
return data.plan(input.plan, options);
|
|
110
|
+
},
|
|
111
|
+
submit: (recipe, overrides, options) => {
|
|
112
|
+
const resolved = resolveRecipeOf(recipe, sites);
|
|
113
|
+
if (!resolved.ok)
|
|
114
|
+
return Promise.resolve(unresolved(resolved.problem));
|
|
115
|
+
if (!allows(resolved.value.origin))
|
|
116
|
+
return Promise.resolve(denyOf(resolved.value.origin, true));
|
|
117
|
+
return data.submit(resolved.value, overrides, options);
|
|
118
|
+
},
|
|
119
|
+
submitPlan: (plan, options) => {
|
|
120
|
+
const input = planInputOf(plan, true);
|
|
121
|
+
if ('failure' in input)
|
|
122
|
+
return Promise.resolve(input.failure);
|
|
123
|
+
return data.submitPlan(input.plan, options);
|
|
124
|
+
},
|
|
125
|
+
};
|
|
126
|
+
return scoped;
|
|
127
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import type { HostContext } from '../contract/module-context.js';
|
|
2
|
+
import type { UnitManifest } from '../contract/unit.js';
|
|
3
|
+
import type { RouteTable } from '../host/http.js';
|
|
4
|
+
import type { LongTaskBase, LongTaskHost, LongTaskRegistry } from '../host/long-task.js';
|
|
5
|
+
import type { ProvisionHandler } from './provision.js';
|
|
6
|
+
import type { UnitCapability } from '../contract/unit-capability.js';
|
|
7
|
+
import type { ApprovalCall, ApprovalFacts, ApprovalPolicy } from '../host/tool-approval.js';
|
|
8
|
+
import type { ValueHandoff } from '../host/value-handoff.js';
|
|
9
|
+
import type { WorkspaceStore } from '../host/workspace-store.js';
|
|
10
|
+
import type { DataReader } from '../contract/data.js';
|
|
11
|
+
import type { LibReader } from '../contract/lib.js';
|
|
12
|
+
import type { GitReader } from '../contract/git.js';
|
|
13
|
+
import type { CommandReader } from '../contract/command.js';
|
|
14
|
+
import type { HostKit } from './host-kit.js';
|
|
15
|
+
export interface ToolScopeContext {
|
|
16
|
+
readonly workspace: string | null;
|
|
17
|
+
readonly preset: string | null;
|
|
18
|
+
readonly session: string | null;
|
|
19
|
+
readonly scope?: unknown;
|
|
20
|
+
}
|
|
21
|
+
export interface UnitMountDeps extends HostKit {
|
|
22
|
+
readonly manifest: UnitManifest;
|
|
23
|
+
readonly ctx: HostContext;
|
|
24
|
+
readonly logger?: {
|
|
25
|
+
info?(message: string): void;
|
|
26
|
+
warn?(message: string): void;
|
|
27
|
+
} | undefined;
|
|
28
|
+
readonly manifests: () => readonly UnitManifest[];
|
|
29
|
+
readonly ownedUnits: () => readonly string[];
|
|
30
|
+
readonly installable?: (() => readonly string[]) | undefined;
|
|
31
|
+
readonly data: DataReader;
|
|
32
|
+
readonly env?: Readonly<Record<string, string | undefined>> | undefined;
|
|
33
|
+
readonly libs?: LibReader | undefined;
|
|
34
|
+
readonly git?: GitReader | undefined;
|
|
35
|
+
readonly commands?: CommandReader | undefined;
|
|
36
|
+
readonly store: WorkspaceStore;
|
|
37
|
+
readonly values?: ValueHandoff | undefined;
|
|
38
|
+
readonly probe?: (unit: string, options?: {
|
|
39
|
+
readonly force?: boolean;
|
|
40
|
+
}) => Promise<UnitCapability | undefined>;
|
|
41
|
+
readonly longTasks?: LongTaskHost | undefined;
|
|
42
|
+
readonly longTaskRuns?: (() => ReadonlyMap<string, LongTaskRegistry<LongTaskBase>>) | undefined;
|
|
43
|
+
}
|
|
44
|
+
export type ContextRenderer = (ref: {
|
|
45
|
+
readonly kind: string;
|
|
46
|
+
readonly parts: readonly string[];
|
|
47
|
+
}) => Promise<string> | string;
|
|
48
|
+
export type UnitWarmRead = readonly string[];
|
|
49
|
+
export interface UnitCall {
|
|
50
|
+
readonly source: string;
|
|
51
|
+
readonly args: Readonly<Record<string, unknown>>;
|
|
52
|
+
readonly result: unknown;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* 挂载结果:路由表 + 工具注册。
|
|
56
|
+
* `registerTools()` 是**调用时才注册**,返回一组清理函数(卸载 / 热换时外壳逐个调);
|
|
57
|
+
* 工具名必须与清单 `dsh.workbench.tools` 一字不差,没有工具就 `() => []`。
|
|
58
|
+
*/
|
|
59
|
+
export interface UnitMountResult {
|
|
60
|
+
readonly routes: RouteTable;
|
|
61
|
+
registerTools(): Array<() => void>;
|
|
62
|
+
readonly factsOf?: ((call: UnitCall) => readonly string[] | undefined) | undefined;
|
|
63
|
+
readonly gate?: {
|
|
64
|
+
deniedTools(context: ToolScopeContext): readonly string[];
|
|
65
|
+
denyReason?(name: string, context: ToolScopeContext): string | undefined;
|
|
66
|
+
subscribe(listener: () => void): () => void;
|
|
67
|
+
};
|
|
68
|
+
readonly context?: {
|
|
69
|
+
readonly kinds: readonly string[];
|
|
70
|
+
render(ref: {
|
|
71
|
+
readonly kind: string;
|
|
72
|
+
readonly parts: readonly string[];
|
|
73
|
+
}): Promise<string> | string;
|
|
74
|
+
};
|
|
75
|
+
readonly warmReads?: (() => readonly UnitWarmRead[]) | undefined;
|
|
76
|
+
readonly prefs?: {
|
|
77
|
+
readonly resource: string;
|
|
78
|
+
handle(payload: unknown): unknown;
|
|
79
|
+
};
|
|
80
|
+
readonly actions?: readonly {
|
|
81
|
+
readonly resource: string;
|
|
82
|
+
handle(payload: unknown): unknown;
|
|
83
|
+
}[];
|
|
84
|
+
readonly provision?: ProvisionHandler;
|
|
85
|
+
readonly longTasks?: LongTaskRegistry<LongTaskBase> | undefined;
|
|
86
|
+
readonly approvalDetail?: ((call: ApprovalCall) => ApprovalFacts | undefined | Promise<ApprovalFacts | undefined>) | undefined;
|
|
87
|
+
readonly needsApproval?: ApprovalPolicy | undefined;
|
|
88
|
+
}
|
|
89
|
+
export interface UnitModuleFace {
|
|
90
|
+
readonly unit: string;
|
|
91
|
+
mount(deps: UnitMountDeps): UnitMountResult;
|
|
92
|
+
}
|
|
93
|
+
export interface UnitOwnedFace extends UnitModuleFace {
|
|
94
|
+
readonly manifest: UnitManifest;
|
|
95
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { HostContext } from '../contract/module-context.js';
|
|
2
|
+
import type { UnitModuleFace } from './unit-face.js';
|
|
3
|
+
/**
|
|
4
|
+
* 一个宿主插件导出的三件:`name` / `inject` / `apply`。
|
|
5
|
+
*/
|
|
6
|
+
export interface UnitHostPlugin {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly inject: readonly string[];
|
|
9
|
+
apply(ctx: HostContext): void;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* 把「宿主面注册进外壳」这段在每个模块里都一样的接线收成一份。
|
|
13
|
+
* 用法:先 `const plugin = defineUnitHostPlugin({ … })`,再逐个导出 `name` / `inject` / `apply`
|
|
14
|
+
* (逐名 `export const` 才过 arch 的导出对账;解构导出它认不出)。
|
|
15
|
+
* @param options.name 插件名(包名)。
|
|
16
|
+
* @param options.unit 单元 id。
|
|
17
|
+
* @param options.face 这个单元的宿主面。
|
|
18
|
+
* @returns `{ name, inject, apply }`;`apply` 里取 `workbenchUnits`、缺了就 warn 一声。
|
|
19
|
+
*/
|
|
20
|
+
export declare function defineUnitHostPlugin(options: {
|
|
21
|
+
readonly name: string;
|
|
22
|
+
readonly unit: string;
|
|
23
|
+
readonly face: UnitModuleFace;
|
|
24
|
+
}): UnitHostPlugin;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { UNITS_SERVICE } from '../config/constants.js';
|
|
2
|
+
/**
|
|
3
|
+
* 把「宿主面注册进外壳」这段在每个模块里都一样的接线收成一份。
|
|
4
|
+
* 用法:先 `const plugin = defineUnitHostPlugin({ … })`,再逐个导出 `name` / `inject` / `apply`
|
|
5
|
+
* (逐名 `export const` 才过 arch 的导出对账;解构导出它认不出)。
|
|
6
|
+
* @param options.name 插件名(包名)。
|
|
7
|
+
* @param options.unit 单元 id。
|
|
8
|
+
* @param options.face 这个单元的宿主面。
|
|
9
|
+
* @returns `{ name, inject, apply }`;`apply` 里取 `workbenchUnits`、缺了就 warn 一声。
|
|
10
|
+
*/
|
|
11
|
+
export function defineUnitHostPlugin(options) {
|
|
12
|
+
const { name, unit, face } = options;
|
|
13
|
+
return {
|
|
14
|
+
name,
|
|
15
|
+
inject: [UNITS_SERVICE],
|
|
16
|
+
apply(ctx) {
|
|
17
|
+
const units = ctx.get(UNITS_SERVICE);
|
|
18
|
+
if (units === undefined) {
|
|
19
|
+
ctx.logger?.warn?.(`[${unit}] 外壳没有提供 workbenchUnits 服务:这个模块装不进来`);
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
ctx.effect(() => units.register(face), `${name}: 注册 ${unit}`);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { UpdateJournalRecord } from './update-journal-types.js';
|
|
2
|
+
/** 用 Pick 绑定日志结构,字段增删会变成编译错误而不是静默漂移。 */
|
|
3
|
+
export type ManifestRecord = Pick<UpdateJournalRecord, 'from' | 'to' | 'profileDir' | 'versionDir' | 'manifestPath' | 'manifestText' | 'manifestDeps' | 'links' | 'carry'>;
|
|
4
|
+
/**
|
|
5
|
+
* 回滚清单:新日志只回写被接管的几条依赖,避免把等待期间用户/插件新增的依赖整份吞掉。
|
|
6
|
+
* 旧日志没有逐条快照,只能退回整份原文。
|
|
7
|
+
*/
|
|
8
|
+
export declare function restoreManifest(record: ManifestRecord): void;
|
|
9
|
+
export type LinkFn = (target: string, path: string) => void;
|
|
10
|
+
/**
|
|
11
|
+
* 新版已经启动过时按日志前滚:把链接与清单都补成新版,而不是留半截现场。
|
|
12
|
+
*
|
|
13
|
+
* 先整体校验(新版本目录里的包都在、类型可接),再逐条换链;中途失败会尽力把
|
|
14
|
+
* 已经换过的链接按快照退回去,避免留下“一半新版、一半旧版”的现场。
|
|
15
|
+
*/
|
|
16
|
+
export declare function rollForward(record: ManifestRecord, link?: LinkFn): {
|
|
17
|
+
readonly ok: boolean;
|
|
18
|
+
readonly detail: string;
|
|
19
|
+
};
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { existsSync } from 'node:fs';
|
|
2
|
+
import { dirname, join, relative, resolve, sep } from 'node:path';
|
|
3
|
+
import { copyDirInto, linkDir, removePath, writeJsonFile, writeTextFile } from './fs-write.js';
|
|
4
|
+
import { readJsonObject } from './read-json.js';
|
|
5
|
+
/**
|
|
6
|
+
* 回滚清单:新日志只回写被接管的几条依赖,避免把等待期间用户/插件新增的依赖整份吞掉。
|
|
7
|
+
* 旧日志没有逐条快照,只能退回整份原文。
|
|
8
|
+
*/
|
|
9
|
+
export function restoreManifest(record) {
|
|
10
|
+
const deps = record.manifestDeps;
|
|
11
|
+
if (deps === undefined) {
|
|
12
|
+
writeTextFile(record.manifestPath, record.manifestText, { fsync: true });
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const current = readManifestObject(record);
|
|
16
|
+
const dependencies = { ...(dependenciesOf(current) ?? {}) };
|
|
17
|
+
for (const [pkg, spec] of Object.entries(deps)) {
|
|
18
|
+
if (spec === null)
|
|
19
|
+
delete dependencies[pkg];
|
|
20
|
+
else
|
|
21
|
+
dependencies[pkg] = spec;
|
|
22
|
+
}
|
|
23
|
+
writeJsonFile(record.manifestPath, { ...current, dependencies }, { fsync: true });
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 新版已经启动过时按日志前滚:把链接与清单都补成新版,而不是留半截现场。
|
|
27
|
+
*
|
|
28
|
+
* 先整体校验(新版本目录里的包都在、类型可接),再逐条换链;中途失败会尽力把
|
|
29
|
+
* 已经换过的链接按快照退回去,避免留下“一半新版、一半旧版”的现场。
|
|
30
|
+
*/
|
|
31
|
+
export function rollForward(record, link = linkDir) {
|
|
32
|
+
const failures = [];
|
|
33
|
+
const planned = [];
|
|
34
|
+
for (const link of record.links) {
|
|
35
|
+
if (link.kind === 'other') {
|
|
36
|
+
failures.push(`${link.pkg}:现场那条既不是链接也不是目录`);
|
|
37
|
+
continue;
|
|
38
|
+
}
|
|
39
|
+
const target = join(record.versionDir, 'node_modules', link.pkg);
|
|
40
|
+
if (!existsSync(target)) {
|
|
41
|
+
failures.push(`${link.pkg}:新版本目录里找不到`);
|
|
42
|
+
continue;
|
|
43
|
+
}
|
|
44
|
+
planned.push({ link, target });
|
|
45
|
+
}
|
|
46
|
+
if (failures.length > 0)
|
|
47
|
+
return { ok: false, detail: `换新版的链接没补齐(${failures.join(';')})` };
|
|
48
|
+
const changed = [];
|
|
49
|
+
const specs = {};
|
|
50
|
+
for (const item of planned) {
|
|
51
|
+
changed.push(item);
|
|
52
|
+
try {
|
|
53
|
+
link(item.target, item.link.path);
|
|
54
|
+
specs[item.link.pkg] = linkSpecOf(record.profileDir, item.target);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
failures.push(`${item.link.pkg}:${messageOf(error)}`);
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (failures.length > 0) {
|
|
62
|
+
for (const item of changed.reverse())
|
|
63
|
+
revertLink(record, item.link, link);
|
|
64
|
+
return { ok: false, detail: `换新版的链接没补齐(${failures.join(';')})` };
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const current = readJsonObject(record.manifestPath);
|
|
68
|
+
if (current === undefined)
|
|
69
|
+
throw new Error('清单读不出来');
|
|
70
|
+
const dependencies = { ...(dependenciesOf(current) ?? {}) };
|
|
71
|
+
for (const [pkg, spec] of Object.entries(specs))
|
|
72
|
+
dependencies[pkg] = spec;
|
|
73
|
+
writeJsonFile(record.manifestPath, { ...current, dependencies }, { fsync: true });
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
for (const item of changed.reverse())
|
|
77
|
+
revertLink(record, item.link, link);
|
|
78
|
+
return { ok: false, detail: `换新版的清单没写成(${messageOf(error)})` };
|
|
79
|
+
}
|
|
80
|
+
return { ok: true, detail: `已经按 ${record.to} 补好链接与清单` };
|
|
81
|
+
}
|
|
82
|
+
function revertLink(record, link, apply) {
|
|
83
|
+
try {
|
|
84
|
+
if (link.kind === 'symlink' && link.target !== undefined) {
|
|
85
|
+
apply(resolve(dirname(link.path), link.target), link.path);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
if (link.kind === 'missing') {
|
|
89
|
+
removePath(link.path);
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
if (link.kind !== 'dir')
|
|
93
|
+
return;
|
|
94
|
+
const carried = record.carry.find((item) => item.from === link.path);
|
|
95
|
+
if (carried !== undefined && existsSync(carried.to)) {
|
|
96
|
+
copyDirInto(carried.to, link.path);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
if (record.to === record.from) {
|
|
100
|
+
const target = join(record.versionDir, 'node_modules', link.pkg);
|
|
101
|
+
if (existsSync(target))
|
|
102
|
+
apply(target, link.path);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
catch {
|
|
106
|
+
// 尽力而为:退不回去就保留日志,下次启动还会再试
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
function linkSpecOf(profileDir, target) {
|
|
110
|
+
return `link:${relative(profileDir, target).split(sep).join('/')}`;
|
|
111
|
+
}
|
|
112
|
+
function readManifestObject(record) {
|
|
113
|
+
const current = readJsonObject(record.manifestPath);
|
|
114
|
+
if (current !== undefined)
|
|
115
|
+
return current;
|
|
116
|
+
try {
|
|
117
|
+
const parsed = JSON.parse(record.manifestText);
|
|
118
|
+
if (typeof parsed === 'object' && parsed !== null)
|
|
119
|
+
return parsed;
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
// 落到下面统一报错
|
|
123
|
+
}
|
|
124
|
+
throw new Error('清单读不出来');
|
|
125
|
+
}
|
|
126
|
+
function dependenciesOf(manifest) {
|
|
127
|
+
const table = manifest['dependencies'];
|
|
128
|
+
if (typeof table !== 'object' || table === null || Array.isArray(table))
|
|
129
|
+
return undefined;
|
|
130
|
+
return { ...table };
|
|
131
|
+
}
|
|
132
|
+
function messageOf(error) {
|
|
133
|
+
return error instanceof Error ? error.message : String(error);
|
|
134
|
+
}
|