vc-extras 0.1.0 → 0.1.1
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 +128 -71
- package/agent-install.mjs +159 -0
- package/agents/tester.md +23 -23
- package/install-extras.mjs +41 -109
- package/install-menu.mjs +22 -43
- package/install-skills.mjs +112 -112
- package/package.json +72 -28
- package/plugin-registry.mjs +73 -0
- package/plugins/config-ui/index.ts +159 -159
- package/plugins/config-ui/lib/arguments.mjs +24 -24
- package/plugins/config-ui/lib/documents.mjs +67 -67
- package/plugins/config-ui/lib/frontmatter.mjs +91 -91
- package/plugins/config-ui/lib/open.mjs +16 -16
- package/plugins/config-ui/lib/ownership.mjs +99 -99
- package/plugins/config-ui/lib/page.mjs +11 -11
- package/plugins/config-ui/lib/patch.mjs +35 -35
- package/plugins/config-ui/lib/paths.mjs +14 -14
- package/plugins/config-ui/lib/plugin-fields.mjs +86 -86
- package/plugins/config-ui/lib/plugin-validation.mjs +28 -28
- package/plugins/config-ui/lib/redaction.mjs +8 -8
- package/plugins/config-ui/lib/registry.mjs +282 -282
- package/plugins/config-ui/lib/secrets.mjs +44 -44
- package/plugins/config-ui/lib/server.mjs +250 -250
- package/plugins/config-ui/lib/sources.mjs +96 -96
- package/plugins/config-ui/lib/state.mjs +156 -156
- package/plugins/config-ui/lib/themes.mjs +113 -113
- package/plugins/config-ui/lib/writer.mjs +690 -690
- package/plugins/config-ui/project-local-owner.ts +106 -106
- package/plugins/config-ui/vc-extras.plugin.json +13 -0
- package/plugins/config-ui/web/app.mjs +997 -997
- package/plugins/config-ui/web/index.html +342 -342
- package/plugins/pi-blackhole/vc-extras.plugin.json +12 -0
- package/plugins/pi-btw/vc-extras.plugin.json +12 -0
- package/plugins/pi-claude-code/vc-extras.plugin.json +12 -0
- package/plugins/pi-fff/vc-extras.plugin.json +12 -0
- package/plugins/pi-jev-auto-mode/vc-extras.plugin.json +12 -0
- package/plugins/pi-mcp-adapter/vc-extras.plugin.json +12 -0
- package/plugins/pi-web-access/vc-extras.plugin.json +12 -0
- package/plugins/vc-ask/claude-hook.mjs +37 -0
- package/plugins/vc-ask/codex-native.mjs +28 -0
- package/plugins/vc-ask/host-channel.mjs +75 -0
- package/plugins/vc-ask/index.ts +57 -0
- package/plugins/vc-ask/native-questions.mjs +33 -0
- package/plugins/vc-ask/package.json +10 -0
- package/plugins/vc-ask/payload.ts +170 -0
- package/plugins/vc-ask/questionnaire/LICENSE.ask-user-question +21 -0
- package/plugins/vc-ask/questionnaire/UPSTREAM.md +104 -0
- package/plugins/vc-ask/questionnaire/answer-channel.ts +135 -0
- package/plugins/vc-ask/questionnaire/config.ts +180 -0
- package/plugins/vc-ask/questionnaire/events.ts +154 -0
- package/plugins/vc-ask/questionnaire/execute.ts +688 -0
- package/plugins/vc-ask/questionnaire/i18n.ts +73 -0
- package/plugins/vc-ask/questionnaire/inline-entry.ts +312 -0
- package/plugins/vc-ask/questionnaire/locales/de.json +43 -0
- package/plugins/vc-ask/questionnaire/locales/en.json +42 -0
- package/plugins/vc-ask/questionnaire/locales/es.json +43 -0
- package/plugins/vc-ask/questionnaire/locales/fr.json +43 -0
- package/plugins/vc-ask/questionnaire/locales/pt-BR.json +43 -0
- package/plugins/vc-ask/questionnaire/locales/pt.json +43 -0
- package/plugins/vc-ask/questionnaire/locales/ru.json +43 -0
- package/plugins/vc-ask/questionnaire/locales/uk.json +43 -0
- package/plugins/vc-ask/questionnaire/locales/zh.json +46 -0
- package/plugins/vc-ask/questionnaire/presentation.ts +11 -0
- package/plugins/vc-ask/questionnaire/question-preview.ts +6 -0
- package/plugins/vc-ask/questionnaire/rpc.ts +221 -0
- package/plugins/vc-ask/questionnaire/session-timeout.ts +67 -0
- package/plugins/vc-ask/questionnaire/state/build-questionnaire.ts +144 -0
- package/plugins/vc-ask/questionnaire/state/external-editor.ts +62 -0
- package/plugins/vc-ask/questionnaire/state/geometry.ts +131 -0
- package/plugins/vc-ask/questionnaire/state/key-router.ts +382 -0
- package/plugins/vc-ask/questionnaire/state/panel-router.ts +32 -0
- package/plugins/vc-ask/questionnaire/state/questionnaire-session.ts +642 -0
- package/plugins/vc-ask/questionnaire/state/row-intent.ts +143 -0
- package/plugins/vc-ask/questionnaire/state/selectors/contract.ts +24 -0
- package/plugins/vc-ask/questionnaire/state/selectors/derivations.ts +40 -0
- package/plugins/vc-ask/questionnaire/state/selectors/focus.ts +17 -0
- package/plugins/vc-ask/questionnaire/state/selectors/projections.ts +99 -0
- package/plugins/vc-ask/questionnaire/state/state-reducer.ts +455 -0
- package/plugins/vc-ask/questionnaire/state/state.ts +90 -0
- package/plugins/vc-ask/questionnaire/state/vertical.ts +158 -0
- package/plugins/vc-ask/questionnaire/timeout.ts +59 -0
- package/plugins/vc-ask/questionnaire/tool/external-answer.ts +103 -0
- package/plugins/vc-ask/questionnaire/tool/format-answer.ts +24 -0
- package/plugins/vc-ask/questionnaire/tool/normalize-params.ts +37 -0
- package/plugins/vc-ask/questionnaire/tool/response-envelope.ts +75 -0
- package/plugins/vc-ask/questionnaire/tool/types.ts +74 -0
- package/plugins/vc-ask/questionnaire/tool/validate-questionnaire.ts +75 -0
- package/plugins/vc-ask/questionnaire/view/card-theme.ts +245 -0
- package/plugins/vc-ask/questionnaire/view/components/inline-input.ts +66 -0
- package/plugins/vc-ask/questionnaire/view/components/multi-select-view.ts +177 -0
- package/plugins/vc-ask/questionnaire/view/components/option-list-view.ts +65 -0
- package/plugins/vc-ask/questionnaire/view/components/preview/markdown-content-cache.ts +101 -0
- package/plugins/vc-ask/questionnaire/view/components/preview/preview-block-renderer.ts +130 -0
- package/plugins/vc-ask/questionnaire/view/components/preview/preview-box-renderer.ts +86 -0
- package/plugins/vc-ask/questionnaire/view/components/preview/preview-layout-decider.ts +204 -0
- package/plugins/vc-ask/questionnaire/view/components/preview/preview-pane.ts +257 -0
- package/plugins/vc-ask/questionnaire/view/components/submit-picker.ts +65 -0
- package/plugins/vc-ask/questionnaire/view/components/tab-bar.ts +57 -0
- package/plugins/vc-ask/questionnaire/view/components/wrapping-select.ts +269 -0
- package/plugins/vc-ask/questionnaire/view/dialog-builder.ts +319 -0
- package/plugins/vc-ask/questionnaire/view/document-theme.ts +53 -0
- package/plugins/vc-ask/questionnaire/view/document-view.ts +306 -0
- package/plugins/vc-ask/questionnaire/view/panel-chrome.ts +133 -0
- package/plugins/vc-ask/questionnaire/view/props-adapter.ts +77 -0
- package/plugins/vc-ask/questionnaire/view/stateful-view.ts +24 -0
- package/plugins/vc-ask/questionnaire/view/tab-components.ts +16 -0
- package/plugins/vc-ask/questionnaire/view/tab-content-strategy.ts +280 -0
- package/plugins/vc-ask/questionnaire/view/vertical-view.ts +218 -0
- package/plugins/vc-ask/user-input-events.ts +34 -0
- package/plugins/vc-ask/vc-extras.plugin.json +18 -0
- package/plugins/vc-footer/agents/claude-code/adapter.mjs +29 -0
- package/plugins/vc-footer/agents/claude-code/cli.mjs +23 -0
- package/plugins/vc-footer/agents/claude-code/renderer.mjs +1 -0
- package/plugins/vc-footer/agents/claude-code/types.d.ts +38 -0
- package/plugins/vc-footer/agents/codex/adapter.mjs +13 -0
- package/plugins/vc-footer/agents/codex/mcp.mjs +45 -0
- package/plugins/vc-footer/agents/codex/native.mjs +19 -0
- package/plugins/vc-footer/agents/codex/renderer.mjs +2 -0
- package/plugins/vc-footer/agents/pi/adapter.mjs +23 -0
- package/plugins/vc-footer/agents/pi/index.js +84 -0
- package/plugins/vc-footer/agents/pi/project-local-owner.mjs +97 -0
- package/plugins/vc-footer/agents/pi/renderer.mjs +5 -0
- package/plugins/vc-footer/core/data.mjs +218 -0
- package/plugins/vc-footer/core/render.mjs +291 -0
- package/plugins/vc-footer/core/types.d.ts +29 -0
- package/plugins/vc-footer/package.json +22 -0
- package/plugins/vc-footer/vc-extras.plugin.json +32 -0
- package/skills/animate/LICENSE +21 -21
- package/skills/animate/RECIPES.md +324 -324
- package/skills/animate/SKILL.md +208 -208
- package/skills/animate/UPSTREAM.md +19 -19
- package/skills/animation-performance/LICENSE +21 -21
- package/skills/animation-performance/SKILL.md +223 -223
- package/skills/animation-performance/UPSTREAM.md +19 -19
- package/skills/answer-me-with-html/SKILL.md +174 -174
- package/skills/answer-me-with-html/scripts/am.mjs +12896 -12896
- package/skills/apple-design/LICENSE +21 -21
- package/skills/apple-design/SKILL.md +291 -291
- package/skills/apple-design/UPSTREAM.md +19 -19
- package/skills/bad-smell/SKILL.md +44 -44
- package/skills/commit/SKILL.md +90 -90
- package/skills/emil-design-eng/LICENSE +21 -21
- package/skills/emil-design-eng/SKILL.md +677 -677
- package/skills/emil-design-eng/UPSTREAM.md +19 -19
- package/skills/find-the-truth/SKILL.md +38 -38
- package/skills/find-the-truth/references/trace-method.md +48 -48
- package/skills/find-the-truth/references/truth-method.md +39 -39
- package/skills/gestures/LICENSE +21 -21
- package/skills/gestures/SKILL.md +107 -107
- package/skills/gestures/UPSTREAM.md +19 -19
- package/skills/humanizer-zh/LICENSE +21 -21
- package/skills/humanizer-zh/SKILL.md +409 -409
- package/skills/humanizer-zh/UPSTREAM.md +7 -7
- package/skills/impeccable/LICENSE +191 -191
- package/skills/impeccable/NOTICE.md +11 -11
- package/skills/impeccable/SKILL.md +89 -89
- package/skills/impeccable/licenses/modern-screenshot-MIT.txt +8 -8
- package/skills/impeccable/licenses/platform-design-skills-MIT.txt +21 -21
- package/skills/impeccable/reference/adapt.md +318 -318
- package/skills/impeccable/reference/adapt.native.md +58 -58
- package/skills/impeccable/reference/android.md +46 -46
- package/skills/impeccable/reference/animate.md +89 -89
- package/skills/impeccable/reference/audit.md +139 -139
- package/skills/impeccable/reference/audit.native.md +139 -139
- package/skills/impeccable/reference/bolder.md +33 -33
- package/skills/impeccable/reference/clarify.md +94 -94
- package/skills/impeccable/reference/colorize.md +86 -86
- package/skills/impeccable/reference/component-review.md +63 -63
- package/skills/impeccable/reference/craft-floor.md +44 -44
- package/skills/impeccable/reference/craft.md +5 -5
- package/skills/impeccable/reference/critique.md +819 -819
- package/skills/impeccable/reference/degraded/asset-producer.md +41 -41
- package/skills/impeccable/reference/degraded/documenter.md +23 -23
- package/skills/impeccable/reference/degraded/finish-reviewer.md +37 -37
- package/skills/impeccable/reference/degraded/manual-edit-applier.md +92 -92
- package/skills/impeccable/reference/delight.md +70 -70
- package/skills/impeccable/reference/distill.md +111 -111
- package/skills/impeccable/reference/doctor.md +54 -54
- package/skills/impeccable/reference/document.md +416 -416
- package/skills/impeccable/reference/extract.md +69 -69
- package/skills/impeccable/reference/generate.md +101 -101
- package/skills/impeccable/reference/harden.md +362 -362
- package/skills/impeccable/reference/hooks.md +114 -114
- package/skills/impeccable/reference/init.md +131 -131
- package/skills/impeccable/reference/ios.md +51 -51
- package/skills/impeccable/reference/layout.md +84 -84
- package/skills/impeccable/reference/live-setup.md +104 -104
- package/skills/impeccable/reference/live.md +327 -327
- package/skills/impeccable/reference/mode-operate.md +21 -21
- package/skills/impeccable/reference/mode-persuade.md +19 -19
- package/skills/impeccable/reference/mode-read.md +21 -21
- package/skills/impeccable/reference/new-work.md +152 -152
- package/skills/impeccable/reference/onboard.md +234 -234
- package/skills/impeccable/reference/operate.md +61 -61
- package/skills/impeccable/reference/optimize.md +257 -257
- package/skills/impeccable/reference/overdrive.md +127 -127
- package/skills/impeccable/reference/polish.md +105 -105
- package/skills/impeccable/reference/quieter.md +99 -99
- package/skills/impeccable/reference/region-map.md +26 -26
- package/skills/impeccable/reference/routing.md +24 -24
- package/skills/impeccable/reference/shape.md +59 -59
- package/skills/impeccable/reference/typeset.md +90 -90
- package/skills/impeccable/reference/visualize.md +47 -47
- package/skills/impeccable/scripts/VERSION +1 -1
- package/skills/impeccable/scripts/command-metadata.json +98 -98
- package/skills/impeccable/scripts/data/font-index-failures.json +120 -120
- package/skills/impeccable/scripts/engine-checksums.txt +9 -9
- package/skills/impeccable/scripts/impeccable +249 -249
- package/skills/impeccable/scripts/impeccable.cmd +278 -278
- package/skills/impeccable/scripts/live-browser-dom.js +320 -320
- package/skills/impeccable/scripts/live-browser-ignores.js +246 -246
- package/skills/impeccable/scripts/live-browser-session.js +183 -183
- package/skills/impeccable/scripts/live-browser.js +13862 -13862
- package/skills/impeccable/scripts/modern-screenshot.umd.js +14 -14
- package/skills/improve-animations/AUDIT.md +115 -115
- package/skills/improve-animations/LICENSE +21 -21
- package/skills/improve-animations/PLAN-TEMPLATE.md +73 -73
- package/skills/improve-animations/SKILL.md +110 -110
- package/skills/improve-animations/UPSTREAM.md +19 -19
- package/skills/pick-ui-library/LICENSE +21 -21
- package/skills/pick-ui-library/SKILL.md +85 -85
- package/skills/pick-ui-library/UPSTREAM.md +19 -19
- package/skills/review-animations/LICENSE +21 -21
- package/skills/review-animations/SKILL.md +120 -120
- package/skills/review-animations/STANDARDS.md +187 -187
- package/skills/review-animations/UPSTREAM.md +19 -19
- package/skills/test/SKILL.md +29 -29
- package/skills/test/references/test-method.md +60 -60
- package/skills/write-doc/SKILL.md +115 -115
- package/skills/write-doc/examples/result-report.md +53 -53
- package/skills/write-doc/examples/task-summary.md +45 -45
- package/skills/write-doc/references/terminology.md +147 -147
- package/specs/ask-host-protocol-v1.md +58 -0
- package/specs/design-demo.md +1102 -1102
- package/specs/frontend-immersive.md +28 -28
- package/specs/plugin-install-v2.md +123 -0
- package/specs/plugin-install.md +38 -0
- package/specs/web-modes/explore.md +31 -31
- package/specs/web-modes/risk.md +15 -15
- package/specs/web-modes/verify.md +16 -16
- package/vc-extras.mjs +46 -62
- package/plugins/essrj/context.ts +0 -47
- package/plugins/essrj/data.ts +0 -190
- package/plugins/essrj/index.ts +0 -139
- package/plugins/essrj/package.json +0 -12
- package/plugins/essrj/project-local-owner.ts +0 -106
- package/plugins/essrj/render.ts +0 -217
- package/plugins/essrj/types.ts +0 -40
- package/plugins.json +0 -38
|
@@ -1,160 +1,160 @@
|
|
|
1
|
-
// `/config` — the browser configuration page for Emma's files.
|
|
2
|
-
//
|
|
3
|
-
// Two paths, one writer:
|
|
4
|
-
// /config starts a loopback service, opens the page, and writes on
|
|
5
|
-
// save; the service exits after a save or ten idle minutes.
|
|
6
|
-
// /config --no-server writes the page under <agentDir>/emma-config, nothing else:
|
|
7
|
-
// saving downloads a patch file.
|
|
8
|
-
// /config apply <patch> applies such a patch from the terminal, with --dry-run.
|
|
9
|
-
import * as fs from "node:fs";
|
|
10
|
-
import * as os from "node:os";
|
|
11
|
-
import * as path from "node:path";
|
|
12
|
-
import { fileURLToPath } from "node:url";
|
|
13
|
-
import { shouldYieldToProject } from "./project-local-owner.ts";
|
|
14
|
-
import { resolveAgentDir } from "./lib/paths.mjs";
|
|
15
|
-
import { prepareSources } from "./lib/sources.mjs";
|
|
16
|
-
import { readPatch, applyPatch } from "./lib/patch.mjs";
|
|
17
|
-
import { openBrowser } from "./lib/open.mjs";
|
|
18
|
-
import { startConfigServer, inject } from "./lib/server.mjs";
|
|
19
|
-
import { inlinePage } from "./lib/page.mjs";
|
|
20
|
-
import { readState } from "./lib/state.mjs";
|
|
21
|
-
import { discoverThemes, themeWritten } from "./lib/themes.mjs";
|
|
22
|
-
import { listBackups, restoreBackups } from "./lib/writer.mjs";
|
|
23
|
-
import { parseArguments } from "./lib/arguments.mjs";
|
|
24
|
-
|
|
25
|
-
const PAGE_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "web");
|
|
26
|
-
|
|
27
|
-
// Both offline artifacts live under the agent directory, never in the project: a patch
|
|
28
|
-
// can carry a key the user just typed, and a project file is one `git add -A` away from
|
|
29
|
-
// being committed.
|
|
30
|
-
const exportDir = (configContext) => path.join(configContext.agentDir, "emma-config");
|
|
31
|
-
|
|
32
|
-
export default function configUi(pi) {
|
|
33
|
-
let lifetime = new AbortController();
|
|
34
|
-
const servers = new Set();
|
|
35
|
-
pi.on("session_shutdown", () => {
|
|
36
|
-
lifetime.abort();
|
|
37
|
-
for (const server of servers) server.close();
|
|
38
|
-
servers.clear();
|
|
39
|
-
});
|
|
40
|
-
// Pi has determined the trust state by session_start. On current Pi, the
|
|
41
|
-
// registered commands are collected after this event completes.
|
|
42
|
-
pi.on("session_start", (_event, context) => {
|
|
43
|
-
if (lifetime.signal.aborted) lifetime = new AbortController();
|
|
44
|
-
if (shouldYieldToProject(import.meta.url, "./runtime/config-ui/index.ts", context.cwd ?? process.cwd(), context.isProjectTrusted?.() === true)) return;
|
|
45
|
-
pi.registerCommand("config", command);
|
|
46
|
-
});
|
|
47
|
-
const command = {
|
|
48
|
-
description: "在浏览器中编辑 Emma 的配置文件(/config --no-server 仅导出补丁;/config apply <补丁> 应用补丁;/config restore <stamp> 从备份回滚)",
|
|
49
|
-
handler: async (args, context) => {
|
|
50
|
-
const { signal } = lifetime;
|
|
51
|
-
if (signal.aborted) return;
|
|
52
|
-
let argv;
|
|
53
|
-
try { argv = parseArguments(args); }
|
|
54
|
-
catch (error) { context.ui.notify(error.message, "error"); return; }
|
|
55
|
-
const cwd = context.cwd ?? process.cwd();
|
|
56
|
-
// Project-level writes only happen in a trusted project: the files are loaded by
|
|
57
|
-
// the next Pi session with the user's own permissions. A context that cannot
|
|
58
|
-
// answer is treated as untrusted — the guard fails closed.
|
|
59
|
-
const trusted = typeof context.isProjectTrusted === "function" ? context.isProjectTrusted() === true : false;
|
|
60
|
-
const configContext = await prepareSources({ agentDir: resolveAgentDir(), projectRoot: cwd, homeDir: os.homedir(), trusted, modelRegistry: context.modelRegistry });
|
|
61
|
-
// Pi owns theme resolution, so its own list is what the page offers and what a
|
|
62
|
-
// save is checked against — a name built from the directories could differ.
|
|
63
|
-
configContext.knownThemes = context.ui.getAllThemes?.() ?? [];
|
|
64
|
-
if (signal.aborted) return;
|
|
65
|
-
|
|
66
|
-
if (argv[0] === "apply") return applyFromTerminal(argv.slice(1), configContext, context);
|
|
67
|
-
if (argv[0] === "restore") return restoreFromTerminal(argv.slice(1), configContext, context);
|
|
68
|
-
if (argv.includes("--no-server")) return exportOnly(configContext, context);
|
|
69
|
-
|
|
70
|
-
let server;
|
|
71
|
-
try {
|
|
72
|
-
server = await startConfigServer({
|
|
73
|
-
context: configContext,
|
|
74
|
-
pageDir: PAGE_DIR,
|
|
75
|
-
signal,
|
|
76
|
-
onApplied: ({ values, results }) => {
|
|
77
|
-
if (!signal.aborted && themeWritten(results)) applyThemeLive(values?.piTheme, context);
|
|
78
|
-
},
|
|
79
|
-
onClose: () => {
|
|
80
|
-
if (server) servers.delete(server);
|
|
81
|
-
if (!signal.aborted) context.ui.notify("配置页服务已退出", "info");
|
|
82
|
-
},
|
|
83
|
-
});
|
|
84
|
-
} catch (error) {
|
|
85
|
-
if (signal.aborted) return;
|
|
86
|
-
throw error;
|
|
87
|
-
}
|
|
88
|
-
if (signal.aborted) { server.close(); return; }
|
|
89
|
-
servers.add(server);
|
|
90
|
-
context.ui.notify(`配置页已就绪:${server.url}(保存即写入;空闲 10 分钟或保存 30 秒后自动退出)`, "info");
|
|
91
|
-
await openBrowser(server.url);
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
async function exportOnly(configContext, context) {
|
|
97
|
-
const state = readState({ context: configContext });
|
|
98
|
-
const page = inlinePage(
|
|
99
|
-
fs.readFileSync(path.join(PAGE_DIR, "index.html"), "utf8"),
|
|
100
|
-
fs.readFileSync(path.join(PAGE_DIR, "app.mjs"), "utf8"),
|
|
101
|
-
);
|
|
102
|
-
const file = path.join(exportDir(configContext), "emma-config.html");
|
|
103
|
-
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
104
|
-
fs.writeFileSync(file, inject(page, { token: "", origin: "", state, themes: discoverThemes(configContext) }));
|
|
105
|
-
context.ui.notify(`已生成配置页:${file}。导出补丁后,运行 /config apply "<下载文件的完整路径>" --dry-run 检查。`, "info");
|
|
106
|
-
await openBrowser(`file:///${file.replace(/\\/g, "/")}`);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
async function applyFromTerminal(argv, configContext, context) {
|
|
110
|
-
const dryRun = argv.includes("--dry-run");
|
|
111
|
-
const file = argv.filter((item) => !item.startsWith("--"))[0] ?? path.join(exportDir(configContext), "emma-config-patch.json");
|
|
112
|
-
const patch = readPatch(file);
|
|
113
|
-
const applied = applyPatch({ patch, context: configContext, dryRun });
|
|
114
|
-
const lines = applied.results.map((result) => `${result.status} · ${result.path}`).join("\n") || "没有需要写入的变更";
|
|
115
|
-
const skipped = applied.skipped?.length ? `\n跳过(只读):${applied.skipped.map((entry) => entry.label).join(", ")}` : "";
|
|
116
|
-
// A rejected value must say why: otherwise an invalid patch just reports "nothing to write".
|
|
117
|
-
const problems = applied.plan.problems.length
|
|
118
|
-
? `\n被拒:${applied.plan.problems.map((problem) => `${problem.label ?? problem.id}(${problem.reason})`).join("; ")}`
|
|
119
|
-
: "";
|
|
120
|
-
const failure = describeFailure(applied.failure);
|
|
121
|
-
context.ui.notify(`${dryRun ? "[dry-run] " : ""}${lines}${problems}${skipped}${applied.unknown.length ? `\n忽略未知字段:${applied.unknown.join(", ")}` : ""}${failure}`, failure || problems || applied.results.some(result => result.status === "conflict") ? "warning" : "info");
|
|
122
|
-
// A patch applied from the terminal changes settings.json just as a save does.
|
|
123
|
-
if (!dryRun && themeWritten(applied.results)) applyThemeLive(patch.values.piTheme, context);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
// Pi keeps its own in-memory theme; writing settings.json alone would leave the running
|
|
127
|
-
// session on the old colors until restart, so the change is pushed through the UI API.
|
|
128
|
-
function applyThemeLive(theme, context) {
|
|
129
|
-
if (!theme) return;
|
|
130
|
-
const result = context.ui.setTheme(theme);
|
|
131
|
-
if (result?.success === false) {
|
|
132
|
-
context.ui.notify(`主题已写入配置,但当前会话未能切换:${result.error ?? "未知原因"}`, "warning");
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
context.ui.notify(`主题已切换:${theme}`, "info");
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
// `/config restore` rolls a registry file back to a backup taken before a save. The
|
|
139
|
-
// stamp addresses a directory, never a path: a backup cannot redirect a write.
|
|
140
|
-
async function restoreFromTerminal(argv, configContext, context) {
|
|
141
|
-
const dryRun = argv.includes("--dry-run");
|
|
142
|
-
const stamp = argv.filter((item) => !item.startsWith("--"))[0];
|
|
143
|
-
if (!stamp) {
|
|
144
|
-
const backups = listBackups(configContext);
|
|
145
|
-
const lines = backups.slice(0, 10).map((entry) => `${entry.stamp} · ${entry.files.length} 个文件`).join("\n");
|
|
146
|
-
context.ui.notify(backups.length ? `可回滚的备份:\n${lines}\n用法:/config restore <stamp> [--dry-run]` : "没有可回滚的备份", "info");
|
|
147
|
-
return;
|
|
148
|
-
}
|
|
149
|
-
const restored = restoreBackups({ context: configContext, stamp, dryRun });
|
|
150
|
-
const lines = restored.results.map((result) => `${result.status} · ${result.path}`).join("\n") || `备份 ${stamp} 与当前文件一致,无需回滚`;
|
|
151
|
-
const failure = describeFailure(restored.failure);
|
|
152
|
-
const incomplete = restored.results.some(result => !["planned", "restored"].includes(result.status));
|
|
153
|
-
context.ui.notify(`${dryRun ? "[dry-run] " : ""}${lines}${failure}`, failure || incomplete ? "warning" : "info");
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
function describeFailure(failure) {
|
|
157
|
-
if (!failure) return "";
|
|
158
|
-
const recovery = failure.stamp ? `\n恢复记录: ${failure.stamp}` : "";
|
|
159
|
-
return `\n未完成(${failure.phase}): ${failure.message}${recovery}`;
|
|
1
|
+
// `/config` — the browser configuration page for Emma's files.
|
|
2
|
+
//
|
|
3
|
+
// Two paths, one writer:
|
|
4
|
+
// /config starts a loopback service, opens the page, and writes on
|
|
5
|
+
// save; the service exits after a save or ten idle minutes.
|
|
6
|
+
// /config --no-server writes the page under <agentDir>/emma-config, nothing else:
|
|
7
|
+
// saving downloads a patch file.
|
|
8
|
+
// /config apply <patch> applies such a patch from the terminal, with --dry-run.
|
|
9
|
+
import * as fs from "node:fs";
|
|
10
|
+
import * as os from "node:os";
|
|
11
|
+
import * as path from "node:path";
|
|
12
|
+
import { fileURLToPath } from "node:url";
|
|
13
|
+
import { shouldYieldToProject } from "./project-local-owner.ts";
|
|
14
|
+
import { resolveAgentDir } from "./lib/paths.mjs";
|
|
15
|
+
import { prepareSources } from "./lib/sources.mjs";
|
|
16
|
+
import { readPatch, applyPatch } from "./lib/patch.mjs";
|
|
17
|
+
import { openBrowser } from "./lib/open.mjs";
|
|
18
|
+
import { startConfigServer, inject } from "./lib/server.mjs";
|
|
19
|
+
import { inlinePage } from "./lib/page.mjs";
|
|
20
|
+
import { readState } from "./lib/state.mjs";
|
|
21
|
+
import { discoverThemes, themeWritten } from "./lib/themes.mjs";
|
|
22
|
+
import { listBackups, restoreBackups } from "./lib/writer.mjs";
|
|
23
|
+
import { parseArguments } from "./lib/arguments.mjs";
|
|
24
|
+
|
|
25
|
+
const PAGE_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), "web");
|
|
26
|
+
|
|
27
|
+
// Both offline artifacts live under the agent directory, never in the project: a patch
|
|
28
|
+
// can carry a key the user just typed, and a project file is one `git add -A` away from
|
|
29
|
+
// being committed.
|
|
30
|
+
const exportDir = (configContext) => path.join(configContext.agentDir, "emma-config");
|
|
31
|
+
|
|
32
|
+
export default function configUi(pi) {
|
|
33
|
+
let lifetime = new AbortController();
|
|
34
|
+
const servers = new Set();
|
|
35
|
+
pi.on("session_shutdown", () => {
|
|
36
|
+
lifetime.abort();
|
|
37
|
+
for (const server of servers) server.close();
|
|
38
|
+
servers.clear();
|
|
39
|
+
});
|
|
40
|
+
// Pi has determined the trust state by session_start. On current Pi, the
|
|
41
|
+
// registered commands are collected after this event completes.
|
|
42
|
+
pi.on("session_start", (_event, context) => {
|
|
43
|
+
if (lifetime.signal.aborted) lifetime = new AbortController();
|
|
44
|
+
if (shouldYieldToProject(import.meta.url, "./runtime/config-ui/index.ts", context.cwd ?? process.cwd(), context.isProjectTrusted?.() === true)) return;
|
|
45
|
+
pi.registerCommand("config", command);
|
|
46
|
+
});
|
|
47
|
+
const command = {
|
|
48
|
+
description: "在浏览器中编辑 Emma 的配置文件(/config --no-server 仅导出补丁;/config apply <补丁> 应用补丁;/config restore <stamp> 从备份回滚)",
|
|
49
|
+
handler: async (args, context) => {
|
|
50
|
+
const { signal } = lifetime;
|
|
51
|
+
if (signal.aborted) return;
|
|
52
|
+
let argv;
|
|
53
|
+
try { argv = parseArguments(args); }
|
|
54
|
+
catch (error) { context.ui.notify(error.message, "error"); return; }
|
|
55
|
+
const cwd = context.cwd ?? process.cwd();
|
|
56
|
+
// Project-level writes only happen in a trusted project: the files are loaded by
|
|
57
|
+
// the next Pi session with the user's own permissions. A context that cannot
|
|
58
|
+
// answer is treated as untrusted — the guard fails closed.
|
|
59
|
+
const trusted = typeof context.isProjectTrusted === "function" ? context.isProjectTrusted() === true : false;
|
|
60
|
+
const configContext = await prepareSources({ agentDir: resolveAgentDir(), projectRoot: cwd, homeDir: os.homedir(), trusted, modelRegistry: context.modelRegistry });
|
|
61
|
+
// Pi owns theme resolution, so its own list is what the page offers and what a
|
|
62
|
+
// save is checked against — a name built from the directories could differ.
|
|
63
|
+
configContext.knownThemes = context.ui.getAllThemes?.() ?? [];
|
|
64
|
+
if (signal.aborted) return;
|
|
65
|
+
|
|
66
|
+
if (argv[0] === "apply") return applyFromTerminal(argv.slice(1), configContext, context);
|
|
67
|
+
if (argv[0] === "restore") return restoreFromTerminal(argv.slice(1), configContext, context);
|
|
68
|
+
if (argv.includes("--no-server")) return exportOnly(configContext, context);
|
|
69
|
+
|
|
70
|
+
let server;
|
|
71
|
+
try {
|
|
72
|
+
server = await startConfigServer({
|
|
73
|
+
context: configContext,
|
|
74
|
+
pageDir: PAGE_DIR,
|
|
75
|
+
signal,
|
|
76
|
+
onApplied: ({ values, results }) => {
|
|
77
|
+
if (!signal.aborted && themeWritten(results)) applyThemeLive(values?.piTheme, context);
|
|
78
|
+
},
|
|
79
|
+
onClose: () => {
|
|
80
|
+
if (server) servers.delete(server);
|
|
81
|
+
if (!signal.aborted) context.ui.notify("配置页服务已退出", "info");
|
|
82
|
+
},
|
|
83
|
+
});
|
|
84
|
+
} catch (error) {
|
|
85
|
+
if (signal.aborted) return;
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
if (signal.aborted) { server.close(); return; }
|
|
89
|
+
servers.add(server);
|
|
90
|
+
context.ui.notify(`配置页已就绪:${server.url}(保存即写入;空闲 10 分钟或保存 30 秒后自动退出)`, "info");
|
|
91
|
+
await openBrowser(server.url);
|
|
92
|
+
},
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async function exportOnly(configContext, context) {
|
|
97
|
+
const state = readState({ context: configContext });
|
|
98
|
+
const page = inlinePage(
|
|
99
|
+
fs.readFileSync(path.join(PAGE_DIR, "index.html"), "utf8"),
|
|
100
|
+
fs.readFileSync(path.join(PAGE_DIR, "app.mjs"), "utf8"),
|
|
101
|
+
);
|
|
102
|
+
const file = path.join(exportDir(configContext), "emma-config.html");
|
|
103
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
104
|
+
fs.writeFileSync(file, inject(page, { token: "", origin: "", state, themes: discoverThemes(configContext) }));
|
|
105
|
+
context.ui.notify(`已生成配置页:${file}。导出补丁后,运行 /config apply "<下载文件的完整路径>" --dry-run 检查。`, "info");
|
|
106
|
+
await openBrowser(`file:///${file.replace(/\\/g, "/")}`);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function applyFromTerminal(argv, configContext, context) {
|
|
110
|
+
const dryRun = argv.includes("--dry-run");
|
|
111
|
+
const file = argv.filter((item) => !item.startsWith("--"))[0] ?? path.join(exportDir(configContext), "emma-config-patch.json");
|
|
112
|
+
const patch = readPatch(file);
|
|
113
|
+
const applied = applyPatch({ patch, context: configContext, dryRun });
|
|
114
|
+
const lines = applied.results.map((result) => `${result.status} · ${result.path}`).join("\n") || "没有需要写入的变更";
|
|
115
|
+
const skipped = applied.skipped?.length ? `\n跳过(只读):${applied.skipped.map((entry) => entry.label).join(", ")}` : "";
|
|
116
|
+
// A rejected value must say why: otherwise an invalid patch just reports "nothing to write".
|
|
117
|
+
const problems = applied.plan.problems.length
|
|
118
|
+
? `\n被拒:${applied.plan.problems.map((problem) => `${problem.label ?? problem.id}(${problem.reason})`).join("; ")}`
|
|
119
|
+
: "";
|
|
120
|
+
const failure = describeFailure(applied.failure);
|
|
121
|
+
context.ui.notify(`${dryRun ? "[dry-run] " : ""}${lines}${problems}${skipped}${applied.unknown.length ? `\n忽略未知字段:${applied.unknown.join(", ")}` : ""}${failure}`, failure || problems || applied.results.some(result => result.status === "conflict") ? "warning" : "info");
|
|
122
|
+
// A patch applied from the terminal changes settings.json just as a save does.
|
|
123
|
+
if (!dryRun && themeWritten(applied.results)) applyThemeLive(patch.values.piTheme, context);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Pi keeps its own in-memory theme; writing settings.json alone would leave the running
|
|
127
|
+
// session on the old colors until restart, so the change is pushed through the UI API.
|
|
128
|
+
function applyThemeLive(theme, context) {
|
|
129
|
+
if (!theme) return;
|
|
130
|
+
const result = context.ui.setTheme(theme);
|
|
131
|
+
if (result?.success === false) {
|
|
132
|
+
context.ui.notify(`主题已写入配置,但当前会话未能切换:${result.error ?? "未知原因"}`, "warning");
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
context.ui.notify(`主题已切换:${theme}`, "info");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// `/config restore` rolls a registry file back to a backup taken before a save. The
|
|
139
|
+
// stamp addresses a directory, never a path: a backup cannot redirect a write.
|
|
140
|
+
async function restoreFromTerminal(argv, configContext, context) {
|
|
141
|
+
const dryRun = argv.includes("--dry-run");
|
|
142
|
+
const stamp = argv.filter((item) => !item.startsWith("--"))[0];
|
|
143
|
+
if (!stamp) {
|
|
144
|
+
const backups = listBackups(configContext);
|
|
145
|
+
const lines = backups.slice(0, 10).map((entry) => `${entry.stamp} · ${entry.files.length} 个文件`).join("\n");
|
|
146
|
+
context.ui.notify(backups.length ? `可回滚的备份:\n${lines}\n用法:/config restore <stamp> [--dry-run]` : "没有可回滚的备份", "info");
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
const restored = restoreBackups({ context: configContext, stamp, dryRun });
|
|
150
|
+
const lines = restored.results.map((result) => `${result.status} · ${result.path}`).join("\n") || `备份 ${stamp} 与当前文件一致,无需回滚`;
|
|
151
|
+
const failure = describeFailure(restored.failure);
|
|
152
|
+
const incomplete = restored.results.some(result => !["planned", "restored"].includes(result.status));
|
|
153
|
+
context.ui.notify(`${dryRun ? "[dry-run] " : ""}${lines}${failure}`, failure || incomplete ? "warning" : "info");
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function describeFailure(failure) {
|
|
157
|
+
if (!failure) return "";
|
|
158
|
+
const recovery = failure.stamp ? `\n恢复记录: ${failure.stamp}` : "";
|
|
159
|
+
return `\n未完成(${failure.phase}): ${failure.message}${recovery}`;
|
|
160
160
|
}
|
|
@@ -1,24 +1,24 @@
|
|
|
1
|
-
// Parse command arguments without treating Windows path backslashes as escapes.
|
|
2
|
-
export function parseArguments(input) {
|
|
3
|
-
const args = [];
|
|
4
|
-
let value = "";
|
|
5
|
-
let quote = null;
|
|
6
|
-
let started = false;
|
|
7
|
-
for (const char of String(input ?? "")) {
|
|
8
|
-
if (quote) {
|
|
9
|
-
if (char === quote) quote = null;
|
|
10
|
-
else value += char;
|
|
11
|
-
} else if (char === '"' || char === "'") {
|
|
12
|
-
quote = char;
|
|
13
|
-
started = true;
|
|
14
|
-
} else if (/\s/.test(char)) {
|
|
15
|
-
if (started) { args.push(value); value = ""; started = false; }
|
|
16
|
-
} else {
|
|
17
|
-
value += char;
|
|
18
|
-
started = true;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
if (quote) throw new Error("路径引号未闭合。用法:/config apply \"完整路径\" [--dry-run]");
|
|
22
|
-
if (started) args.push(value);
|
|
23
|
-
return args;
|
|
24
|
-
}
|
|
1
|
+
// Parse command arguments without treating Windows path backslashes as escapes.
|
|
2
|
+
export function parseArguments(input) {
|
|
3
|
+
const args = [];
|
|
4
|
+
let value = "";
|
|
5
|
+
let quote = null;
|
|
6
|
+
let started = false;
|
|
7
|
+
for (const char of String(input ?? "")) {
|
|
8
|
+
if (quote) {
|
|
9
|
+
if (char === quote) quote = null;
|
|
10
|
+
else value += char;
|
|
11
|
+
} else if (char === '"' || char === "'") {
|
|
12
|
+
quote = char;
|
|
13
|
+
started = true;
|
|
14
|
+
} else if (/\s/.test(char)) {
|
|
15
|
+
if (started) { args.push(value); value = ""; started = false; }
|
|
16
|
+
} else {
|
|
17
|
+
value += char;
|
|
18
|
+
started = true;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (quote) throw new Error("路径引号未闭合。用法:/config apply \"完整路径\" [--dry-run]");
|
|
22
|
+
if (started) args.push(value);
|
|
23
|
+
return args;
|
|
24
|
+
}
|
|
@@ -1,67 +1,67 @@
|
|
|
1
|
-
import * as fs from "node:fs";
|
|
2
|
-
import { isDeepStrictEqual } from "node:util";
|
|
3
|
-
import * as toml from "smol-toml";
|
|
4
|
-
import { parse, modify, applyEdits } from "jsonc-parser";
|
|
5
|
-
|
|
6
|
-
export function parseDocument(raw, kind) {
|
|
7
|
-
try {
|
|
8
|
-
const clean = raw.replace(/^\uFEFF/, "");
|
|
9
|
-
let value;
|
|
10
|
-
if (kind === "toml") value = toml.parse(clean, { integersAsBigInt: true, unsafeKeyBehaviour: "throw" });
|
|
11
|
-
else if (kind === "jsonc") {
|
|
12
|
-
const errors = [];
|
|
13
|
-
value = parse(clean, errors, { allowTrailingComma: true });
|
|
14
|
-
if (errors.length) throw new Error("invalid JSONC");
|
|
15
|
-
} else value = JSON.parse(clean);
|
|
16
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("expected object");
|
|
17
|
-
return value;
|
|
18
|
-
} catch { throw new Error("配置语法或根对象不正确,请先修正文件;原文件未改动。"); }
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
export function readDocument(file, kind) {
|
|
22
|
-
const raw = fs.existsSync(file) ? fs.readFileSync(file, "utf8") : "";
|
|
23
|
-
return { raw, value: raw || fs.existsSync(file) ? parseDocument(raw, kind) : {} };
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
export function encodeDocument(raw, before, after, kind) {
|
|
27
|
-
if (kind === "toml") {
|
|
28
|
-
const encoded = toml.stringify(after, { numbersAsFloat: true });
|
|
29
|
-
if (!sameToml(toml.parse(encoded, { integersAsBigInt: true, unsafeKeyBehaviour: "throw" }), after))
|
|
30
|
-
throw new Error("TOML 值或类型无法无损保留,拒绝写入。");
|
|
31
|
-
return encoded;
|
|
32
|
-
}
|
|
33
|
-
if (kind === "jsonc") {
|
|
34
|
-
let next = raw || "{}";
|
|
35
|
-
const visit = (left, right, keys = []) => {
|
|
36
|
-
const record = v => v && typeof v === "object" && !Array.isArray(v);
|
|
37
|
-
if (record(left) && record(right)) {
|
|
38
|
-
for (const key of new Set([...Object.keys(left), ...Object.keys(right)])) visit(left[key], right[key], [...keys, key]);
|
|
39
|
-
} else if (!isDeepStrictEqual(left, right)) {
|
|
40
|
-
next = applyEdits(next, modify(next, keys, right, { formattingOptions: { insertSpaces: true, tabSize: 2 } }));
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
visit(before, after);
|
|
44
|
-
return next.endsWith("\n") ? next : next + "\n";
|
|
45
|
-
}
|
|
46
|
-
return JSON.stringify(after, null, 2) + "\n";
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function sameToml(left, right) {
|
|
50
|
-
if (Object.is(left, right)) return true;
|
|
51
|
-
if (left instanceof Date || right instanceof Date) return isDeepStrictEqual(left, right);
|
|
52
|
-
if (!left || !right || typeof left !== "object" || typeof right !== "object" || Array.isArray(left) !== Array.isArray(right)) return false;
|
|
53
|
-
const keys = Object.keys(left);
|
|
54
|
-
return keys.length === Object.keys(right).length && keys.every(key => Object.hasOwn(right, key) && sameToml(left[key], right[key]));
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
export function cloneDocument(value) {
|
|
58
|
-
if (Array.isArray(value)) return value.map(cloneDocument);
|
|
59
|
-
if (value instanceof Date) return value;
|
|
60
|
-
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, cloneDocument(item)]));
|
|
61
|
-
return value;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
export function pageValue(value) {
|
|
65
|
-
if (typeof value === "bigint") return value >= BigInt(Number.MIN_SAFE_INTEGER) && value <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(value) : null;
|
|
66
|
-
return value;
|
|
67
|
-
}
|
|
1
|
+
import * as fs from "node:fs";
|
|
2
|
+
import { isDeepStrictEqual } from "node:util";
|
|
3
|
+
import * as toml from "smol-toml";
|
|
4
|
+
import { parse, modify, applyEdits } from "jsonc-parser";
|
|
5
|
+
|
|
6
|
+
export function parseDocument(raw, kind) {
|
|
7
|
+
try {
|
|
8
|
+
const clean = raw.replace(/^\uFEFF/, "");
|
|
9
|
+
let value;
|
|
10
|
+
if (kind === "toml") value = toml.parse(clean, { integersAsBigInt: true, unsafeKeyBehaviour: "throw" });
|
|
11
|
+
else if (kind === "jsonc") {
|
|
12
|
+
const errors = [];
|
|
13
|
+
value = parse(clean, errors, { allowTrailingComma: true });
|
|
14
|
+
if (errors.length) throw new Error("invalid JSONC");
|
|
15
|
+
} else value = JSON.parse(clean);
|
|
16
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error("expected object");
|
|
17
|
+
return value;
|
|
18
|
+
} catch { throw new Error("配置语法或根对象不正确,请先修正文件;原文件未改动。"); }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function readDocument(file, kind) {
|
|
22
|
+
const raw = fs.existsSync(file) ? fs.readFileSync(file, "utf8") : "";
|
|
23
|
+
return { raw, value: raw || fs.existsSync(file) ? parseDocument(raw, kind) : {} };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function encodeDocument(raw, before, after, kind) {
|
|
27
|
+
if (kind === "toml") {
|
|
28
|
+
const encoded = toml.stringify(after, { numbersAsFloat: true });
|
|
29
|
+
if (!sameToml(toml.parse(encoded, { integersAsBigInt: true, unsafeKeyBehaviour: "throw" }), after))
|
|
30
|
+
throw new Error("TOML 值或类型无法无损保留,拒绝写入。");
|
|
31
|
+
return encoded;
|
|
32
|
+
}
|
|
33
|
+
if (kind === "jsonc") {
|
|
34
|
+
let next = raw || "{}";
|
|
35
|
+
const visit = (left, right, keys = []) => {
|
|
36
|
+
const record = v => v && typeof v === "object" && !Array.isArray(v);
|
|
37
|
+
if (record(left) && record(right)) {
|
|
38
|
+
for (const key of new Set([...Object.keys(left), ...Object.keys(right)])) visit(left[key], right[key], [...keys, key]);
|
|
39
|
+
} else if (!isDeepStrictEqual(left, right)) {
|
|
40
|
+
next = applyEdits(next, modify(next, keys, right, { formattingOptions: { insertSpaces: true, tabSize: 2 } }));
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
visit(before, after);
|
|
44
|
+
return next.endsWith("\n") ? next : next + "\n";
|
|
45
|
+
}
|
|
46
|
+
return JSON.stringify(after, null, 2) + "\n";
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function sameToml(left, right) {
|
|
50
|
+
if (Object.is(left, right)) return true;
|
|
51
|
+
if (left instanceof Date || right instanceof Date) return isDeepStrictEqual(left, right);
|
|
52
|
+
if (!left || !right || typeof left !== "object" || typeof right !== "object" || Array.isArray(left) !== Array.isArray(right)) return false;
|
|
53
|
+
const keys = Object.keys(left);
|
|
54
|
+
return keys.length === Object.keys(right).length && keys.every(key => Object.hasOwn(right, key) && sameToml(left[key], right[key]));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function cloneDocument(value) {
|
|
58
|
+
if (Array.isArray(value)) return value.map(cloneDocument);
|
|
59
|
+
if (value instanceof Date) return value;
|
|
60
|
+
if (value && typeof value === "object") return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, cloneDocument(item)]));
|
|
61
|
+
return value;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export function pageValue(value) {
|
|
65
|
+
if (typeof value === "bigint") return value >= BigInt(Number.MIN_SAFE_INTEGER) && value <= BigInt(Number.MAX_SAFE_INTEGER) ? Number(value) : null;
|
|
66
|
+
return value;
|
|
67
|
+
}
|