@behavioral-agentic-design-system/core 0.48.0 → 0.49.1-beta-20261119021055
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/package.json +2 -1
- package/scripts/bads.mjs +247 -0
- package/scripts/build-tokens.cjs +24 -3
- package/scripts/lib/connect/check.mjs +102 -0
- package/scripts/lib/connect/connect.mjs +411 -0
- package/scripts/lib/connect/edits.mjs +361 -0
- package/scripts/lib/connect/tools.mjs +170 -0
- package/scripts/lib/connect/update-check.mjs +29 -0
- package/scripts/lint-ethics-contract.mjs +19 -1
- package/scripts/ux-smoke.mjs +5 -1
- package/src/api/save-tokens.ts +170 -24
- package/src/app/layout.tsx +12 -1
- package/src/app/preview/components/page.tsx +25 -18
- package/src/app/preview/global-font-client.tsx +2 -0
- package/src/app/providers.tsx +2 -0
- package/src/app/shell.tsx +50 -156
- package/src/components/customize/brand-mark-section.tsx +65 -95
- package/src/components/customize/font-pickers.tsx +25 -2
- package/src/components/customize/save-bar.tsx +120 -7
- package/src/components/customize/theme-io.tsx +62 -21
- package/src/components/customize-tab.tsx +3 -5
- package/src/components/home-recent.tsx +58 -25
- package/src/components/motion/reveal.tsx +168 -12
- package/src/components/studio/control-panel.tsx +98 -19
- package/src/components/studio/import-theme-dialog.tsx +167 -0
- package/src/components/studio/tokens-panel.tsx +215 -0
- package/src/components/studio/types.ts +5 -2
- package/src/components/studio/use-studio-tweaks.ts +36 -4
- package/src/components/studio/use-token-edits.ts +160 -0
- package/src/components/ui/date-picker.tsx +1 -8
- package/src/components/ui/date-range-picker.tsx +1 -8
- package/src/components/ui/json-input.tsx +0 -1
- package/src/components/ui/navigation-progress.tsx +5 -8
- package/src/components/ui/segmented-control.tsx +1 -5
- package/src/components/ui/sortable-list.tsx +1 -1
- package/src/components/ui/toggle-tip.tsx +38 -29
- package/src/components/ui/tree-view.tsx +0 -2
- package/src/data/component-stories.json +1214 -0
- package/src/data/docs-index.ts +10 -2
- package/src/data/hidden-views.ts +22 -0
- package/src/data/history.json +38 -38
- package/src/data/sticker-sheet.json +1878 -1821
- package/src/fonts/Figtree-OFL.txt +93 -0
- package/src/fonts/figtree-latin-ext-wght-normal.woff2 +0 -0
- package/src/fonts/figtree-latin-wght-italic.woff2 +0 -0
- package/src/fonts/figtree-latin-wght-normal.woff2 +0 -0
- package/src/fonts-cdn.css +3 -1
- package/src/fonts.css +44 -0
- package/src/hooks/use-global-heading-font.ts +104 -0
- package/src/hooks/use-global-sans-font.ts +8 -3
- package/src/hooks/use-global-theme.ts +2 -2
- package/src/hooks/use-token-overrides.ts +62 -14
- package/src/lib/component-contract.ts +148 -0
- package/src/lib/css-color.ts +42 -0
- package/src/lib/format-date.ts +7 -0
- package/src/lib/storybook-url.ts +28 -0
- package/src/lib/theme-editor.ts +7 -1
- package/src/lib/theme-import.ts +99 -0
- package/src/lib/theme-transition.ts +93 -0
- package/src/lib/token-list.ts +133 -0
- package/src/lib/token-value.ts +31 -0
- package/src/stories/Accordion.stories.tsx +1 -1
- package/src/stories/Affix.stories.tsx +1 -1
- package/src/stories/AiBadge.stories.tsx +1 -1
- package/src/stories/Alert.stories.tsx +1 -1
- package/src/stories/AlertDialog.stories.tsx +1 -1
- package/src/stories/AngleSlider.stories.tsx +1 -1
- package/src/stories/AnnouncementBar.stories.tsx +2 -2
- package/src/stories/AspectRatio.stories.tsx +1 -1
- package/src/stories/Attachment.stories.tsx +1 -1
- package/src/stories/Autocomplete.stories.tsx +1 -1
- package/src/stories/Avatar.stories.tsx +1 -1
- package/src/stories/Badge.stories.tsx +1 -1
- package/src/stories/Blockquote.stories.tsx +1 -1
- package/src/stories/Burger.stories.tsx +1 -1
- package/src/stories/Button.stories.tsx +1 -1
- package/src/stories/ButtonGroup.stories.tsx +1 -1
- package/src/stories/Calendar.stories.tsx +1 -1
- package/src/stories/Callout.stories.tsx +1 -1
- package/src/stories/Canvas.stories.tsx +1 -1
- package/src/stories/Card.stories.tsx +1 -1
- package/src/stories/Carousel.stories.tsx +1 -1
- package/src/stories/Cascader.stories.tsx +1 -1
- package/src/stories/Chart.stories.tsx +1 -1
- package/src/stories/ChatBubble.stories.tsx +1 -1
- package/src/stories/ChatMarker.stories.tsx +1 -1
- package/src/stories/Checkbox.stories.tsx +1 -1
- package/src/stories/CheckboxCard.stories.tsx +1 -1
- package/src/stories/CheckboxGroup.stories.tsx +1 -1
- package/src/stories/Chip.stories.tsx +1 -1
- package/src/stories/CircularProgress.stories.tsx +1 -1
- package/src/stories/Clipboard.stories.tsx +1 -1
- package/src/stories/CloseButton.stories.tsx +1 -1
- package/src/stories/Code.stories.tsx +1 -1
- package/src/stories/Collapsible.stories.tsx +1 -1
- package/src/stories/ColorPicker.stories.tsx +1 -1
- package/src/stories/ColorSwatch.stories.tsx +1 -1
- package/src/stories/Combobox.stories.tsx +1 -1
- package/src/stories/Container.stories.tsx +1 -1
- package/src/stories/ContextMenu.stories.tsx +1 -1
- package/src/stories/CopyButton.stories.tsx +1 -1
- package/src/stories/DataList.stories.tsx +1 -1
- package/src/stories/DataTable.stories.tsx +1 -1
- package/src/stories/DateField.stories.tsx +1 -1
- package/src/stories/DatePicker.stories.tsx +1 -1
- package/src/stories/DateRangePicker.stories.tsx +1 -1
- package/src/stories/Dialog.stories.tsx +1 -1
- package/src/stories/Drawer.stories.tsx +1 -1
- package/src/stories/DropdownMenu.stories.tsx +1 -1
- package/src/stories/Editable.stories.tsx +1 -1
- package/src/stories/EmptyState.stories.tsx +1 -1
- package/src/stories/FacetedFilter.stories.tsx +1 -1
- package/src/stories/Field.stories.tsx +1 -1
- package/src/stories/Fieldset.stories.tsx +1 -1
- package/src/stories/FileButton.stories.tsx +1 -1
- package/src/stories/FileUpload.stories.tsx +1 -1
- package/src/stories/FloatLabel.stories.tsx +1 -1
- package/src/stories/FloatingActionButton.stories.tsx +1 -1
- package/src/stories/FloatingIndicator.stories.tsx +1 -1
- package/src/stories/FloatingPanel.stories.tsx +1 -1
- package/src/stories/FocusTrap.stories.tsx +1 -1
- package/src/stories/FormatByte.stories.tsx +1 -1
- package/src/stories/FormatDate.stories.tsx +1 -1
- package/src/stories/FormatNumber.stories.tsx +1 -1
- package/src/stories/FormatRelativeTime.stories.tsx +1 -1
- package/src/stories/GaugeChart.stories.tsx +1 -1
- package/src/stories/Grid.stories.tsx +1 -1
- package/src/stories/Heatmap.stories.tsx +1 -1
- package/src/stories/Highlight.stories.tsx +1 -1
- package/src/stories/HoverCard.stories.tsx +1 -1
- package/src/stories/Icon.stories.tsx +1 -1
- package/src/stories/Image.stories.tsx +1 -1
- package/src/stories/Indicator.stories.tsx +1 -1
- package/src/stories/InfiniteScroll.stories.tsx +1 -1
- package/src/stories/InlineEdit.stories.tsx +1 -1
- package/src/stories/Input.stories.tsx +1 -1
- package/src/stories/InputClearButton.stories.tsx +1 -1
- package/src/stories/InputGroup.stories.tsx +1 -1
- package/src/stories/JsonInput.stories.tsx +2 -2
- package/src/stories/Kbd.stories.tsx +1 -1
- package/src/stories/KeyboardShortcut.stories.tsx +1 -1
- package/src/stories/Label.stories.tsx +1 -1
- package/src/stories/List.stories.tsx +1 -1
- package/src/stories/Listbox.stories.tsx +1 -1
- package/src/stories/LoadingDots.stories.tsx +1 -1
- package/src/stories/LoadingOverlay.stories.tsx +1 -1
- package/src/stories/Mark.stories.tsx +1 -1
- package/src/stories/MarkdownPreview.stories.tsx +1 -1
- package/src/stories/Marquee.stories.tsx +1 -1
- package/src/stories/Masonry.stories.tsx +1 -1
- package/src/stories/Meter.stories.tsx +1 -1
- package/src/stories/MonthPicker.stories.tsx +1 -1
- package/src/stories/MotionToggle.stories.tsx +1 -1
- package/src/stories/MultiSelect.stories.tsx +1 -1
- package/src/stories/NativeSelect.stories.tsx +1 -1
- package/src/stories/NavigationProgress.stories.tsx +1 -1
- package/src/stories/Notification.stories.tsx +1 -1
- package/src/stories/NumberField.stories.tsx +1 -1
- package/src/stories/NumberInput.stories.tsx +1 -1
- package/src/stories/OtpInput.stories.tsx +1 -1
- package/src/stories/Overlay.stories.tsx +1 -1
- package/src/stories/PageHeader.stories.tsx +1 -1
- package/src/stories/PasswordInput.stories.tsx +1 -1
- package/src/stories/PasswordStrengthInput.stories.tsx +1 -1
- package/src/stories/PhoneInput.stories.tsx +1 -1
- package/src/stories/Pill.stories.tsx +1 -1
- package/src/stories/PinInput.stories.tsx +1 -1
- package/src/stories/Popover.stories.tsx +1 -1
- package/src/stories/Portal.stories.tsx +1 -1
- package/src/stories/Presence.stories.tsx +1 -1
- package/src/stories/Progress.stories.tsx +1 -1
- package/src/stories/ProgressStack.stories.tsx +1 -1
- package/src/stories/Prose.stories.tsx +1 -1
- package/src/stories/QrCode.stories.tsx +1 -1
- package/src/stories/Questionnaire.stories.tsx +1 -1
- package/src/stories/RadioGroup.stories.tsx +1 -1
- package/src/stories/RatingGroup.stories.tsx +1 -1
- package/src/stories/Resizable.stories.tsx +1 -1
- package/src/stories/Result.stories.tsx +1 -1
- package/src/stories/Reveal.stories.tsx +1 -1
- package/src/stories/RollingNumber.stories.tsx +1 -1
- package/src/stories/ScrollArea.stories.tsx +1 -1
- package/src/stories/ScrollProgress.stories.tsx +1 -1
- package/src/stories/SearchInput.stories.tsx +1 -1
- package/src/stories/SegmentedControl.stories.tsx +2 -2
- package/src/stories/Select.stories.tsx +1 -1
- package/src/stories/Separator.stories.tsx +1 -1
- package/src/stories/Sheet.stories.tsx +1 -1
- package/src/stories/SignaturePad.stories.tsx +1 -1
- package/src/stories/SimpleGrid.stories.tsx +1 -1
- package/src/stories/Skeleton.stories.tsx +1 -1
- package/src/stories/SkeletonTable.stories.tsx +1 -1
- package/src/stories/Slider.stories.tsx +1 -1
- package/src/stories/Snackbar.stories.tsx +1 -1
- package/src/stories/Sonner.stories.tsx +1 -1
- package/src/stories/SortableList.stories.tsx +2 -1
- package/src/stories/Sparkline.stories.tsx +1 -1
- package/src/stories/Spinner.stories.tsx +1 -1
- package/src/stories/Spoiler.stories.tsx +1 -1
- package/src/stories/Squircle.stories.tsx +1 -1
- package/src/stories/Stack.stories.tsx +1 -1
- package/src/stories/StatusBadge.stories.tsx +1 -1
- package/src/stories/Switch.stories.tsx +1 -1
- package/src/stories/Table.stories.tsx +1 -1
- package/src/stories/Tag.stories.tsx +1 -1
- package/src/stories/TagsInput.stories.tsx +1 -1
- package/src/stories/Text.stories.tsx +1 -1
- package/src/stories/Textarea.stories.tsx +1 -1
- package/src/stories/ThemeIcon.stories.tsx +1 -1
- package/src/stories/TimePicker.stories.tsx +1 -1
- package/src/stories/Timeline.stories.tsx +1 -1
- package/src/stories/Timer.stories.tsx +1 -1
- package/src/stories/Toggle.stories.tsx +1 -1
- package/src/stories/ToggleGroup.stories.tsx +1 -1
- package/src/stories/ToggleTip.stories.tsx +106 -1
- package/src/stories/Tooltip.stories.tsx +56 -1
- package/src/stories/TransferList.stories.tsx +1 -1
- package/src/stories/TreeSelect.stories.tsx +1 -1
- package/src/stories/TreeView.stories.tsx +1 -0
- package/src/stories/TypingIndicator.stories.tsx +1 -1
- package/src/stories/VisuallyHidden.stories.tsx +1 -1
- package/src/stories/Watermark.stories.tsx +1 -1
- package/src/stories/WeekPicker.stories.tsx +1 -1
- package/src/stories/WordRotate.stories.tsx +1 -1
- package/src/stories/YearPicker.stories.tsx +1 -1
- package/src/stories/story-groups.json +216 -224
- package/src/styles.css +20 -3
- package/src/tokens.default.json +6 -1
- package/src/tokens.json +7 -2
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `bads connect`: plan, write, list and undo the MCP entry and rules file for each agent tool.
|
|
3
|
+
*
|
|
4
|
+
* Safety contract (each property has a test in scripts/bads-connect.test.mjs):
|
|
5
|
+
* - The default writes only inside the project. A user-level file needs --global, and the CLI shows its exact
|
|
6
|
+
* diff and asks before writing.
|
|
7
|
+
* - --dry-run computes the same plan and writes nothing, not even the manifest.
|
|
8
|
+
* - An existing file keeps its unrelated content: JSON gains one key, Markdown and TOML gain one marked block.
|
|
9
|
+
* A re-run with nothing new writes nothing.
|
|
10
|
+
* - Every edited file is backed up once, before its first edit. --remove restores the backup byte for byte
|
|
11
|
+
* when the file is unchanged since connect wrote it, and otherwise takes out only what connect added.
|
|
12
|
+
* - Nothing here reads, asks for or writes a secret. The only environment value written is BADS_ROOT, a path.
|
|
13
|
+
* - Nothing here touches the network.
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync, mkdirSync, readFileSync, readdirSync, rmSync, rmdirSync, statSync, writeFileSync } from "node:fs";
|
|
16
|
+
import { delimiter, dirname, isAbsolute, join, relative, sep } from "node:path";
|
|
17
|
+
import {
|
|
18
|
+
EditConflict,
|
|
19
|
+
TOML_END,
|
|
20
|
+
TOML_START,
|
|
21
|
+
findBlock,
|
|
22
|
+
getPath,
|
|
23
|
+
mergeJsonEntry,
|
|
24
|
+
parseJsonConfig,
|
|
25
|
+
removeBlock,
|
|
26
|
+
removeJsonEntry,
|
|
27
|
+
sha256,
|
|
28
|
+
upsertBlock,
|
|
29
|
+
wrapBlock,
|
|
30
|
+
} from "./edits.mjs";
|
|
31
|
+
import { TOOLS, TOOL_IDS, SERVER_NAME } from "./tools.mjs";
|
|
32
|
+
|
|
33
|
+
export const STATE_DIR = join(".bads", "connect");
|
|
34
|
+
const MANIFEST = join(STATE_DIR, "manifest.json");
|
|
35
|
+
const BACKUPS = join(STATE_DIR, "backups");
|
|
36
|
+
const SERVER_CANDIDATES = ["packages/mcp/bin/stdio.mjs", "node_modules/@behavioral-agentic-design-system/mcp/bin/stdio.mjs"];
|
|
37
|
+
const LOOKUP_CANDIDATES = ["packages/mcp/bin/lookup.mjs", "node_modules/@behavioral-agentic-design-system/mcp/bin/lookup.mjs"];
|
|
38
|
+
|
|
39
|
+
const readText = (p) => (existsSync(p) ? readFileSync(p, "utf8") : null);
|
|
40
|
+
const readJson = (p) => {
|
|
41
|
+
try {
|
|
42
|
+
return JSON.parse(readFileSync(p, "utf8"));
|
|
43
|
+
} catch {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function onPath(names, env, platform) {
|
|
49
|
+
const dirs = (env.PATH || "").split(delimiter).filter(Boolean);
|
|
50
|
+
const exts = platform === "win32" ? ["", ".exe", ".cmd", ".bat"] : [""];
|
|
51
|
+
return names.filter((n) => dirs.some((d) => exts.some((x) => existsSync(join(d, n + x)))));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Everything connect needs to know about the project and the machine. Pass `home` and `env` in tests. */
|
|
55
|
+
export function makeContext({ cwd = process.cwd(), home, env = process.env, platform = process.platform } = {}) {
|
|
56
|
+
const root = cwd;
|
|
57
|
+
const h = home ?? env.HOME ?? env.USERPROFILE;
|
|
58
|
+
if (!h) throw new Error("No home directory: set HOME");
|
|
59
|
+
const identityRaw = readJson(join(root, ".client-identity.json"));
|
|
60
|
+
const sot = identityRaw?.sourceOfTruth ?? {};
|
|
61
|
+
const identity = identityRaw
|
|
62
|
+
? { name: identityRaw.name || "BADS", components: sot.components === "client" ? "client" : "bads", clientLibrary: sot.clientLibrary || null }
|
|
63
|
+
: { name: "BADS", components: "bads", clientLibrary: null };
|
|
64
|
+
const pm = existsSync(join(root, "pnpm-lock.yaml")) ? "pnpm" : existsSync(join(root, "yarn.lock")) ? "yarn" : "npm";
|
|
65
|
+
const pkg = readJson(join(root, "package.json")) ?? {};
|
|
66
|
+
const serverRel = SERVER_CANDIDATES.find((p) => existsSync(join(root, p))) ?? null;
|
|
67
|
+
const lookupRel = LOOKUP_CANDIDATES.find((p) => existsSync(join(root, p)));
|
|
68
|
+
const lookup = lookupRel ? `node ${lookupRel}` : existsSync(join(root, "node_modules", ".bin", "bads-lookup")) ? "npx bads-lookup" : null;
|
|
69
|
+
const contracts = pkg.scripts?.contracts
|
|
70
|
+
? { run: (args) => (pm === "npm" ? `npm run contracts -- ${args}` : `${pm} contracts ${args}`), argv: pm === "npm" ? ["npm", ["run", "-s", "contracts", "--"]] : [pm, ["-s", "contracts"]] }
|
|
71
|
+
: null;
|
|
72
|
+
return { root, home: h, env, platform, identity, pm, server: serverRel ? { rel: serverRel } : null, lookup, contracts };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function detectTools(ctx) {
|
|
76
|
+
return TOOLS.map((tool) => {
|
|
77
|
+
const project = tool.detect.project.filter((p) => existsSync(join(ctx.root, p)));
|
|
78
|
+
const homeHits = (tool.detect.home ?? []).filter((p) => existsSync(join(ctx.home, p))).map((p) => `~/${p}`);
|
|
79
|
+
const absHits = (tool.detect.homeAbs?.(ctx) ?? []).filter((p) => existsSync(p));
|
|
80
|
+
const bins = onPath(tool.detect.bin, ctx.env, ctx.platform).map((b) => `${b} on PATH`);
|
|
81
|
+
const machine = [...homeHits, ...absHits, ...bins];
|
|
82
|
+
return { tool, project, machine, detected: project.length + machine.length > 0 };
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The text of the rules block. Plain instructions an agent can follow. */
|
|
87
|
+
export function renderRules(ctx, { mcp }) {
|
|
88
|
+
const { identity, lookup, contracts } = ctx;
|
|
89
|
+
const lines = [`## Design system: ${identity.name}`, ""];
|
|
90
|
+
const intro = identity.name === "BADS" ? "the BADS design system" : `the ${identity.name} design system, built on BADS`;
|
|
91
|
+
lines.push(`This project uses ${intro}. The full rules are in \`AGENTS.md\`. Read them before you build UI.`, "");
|
|
92
|
+
if (identity.components === "client") {
|
|
93
|
+
const lib = identity.clientLibrary ? `\`${identity.clientLibrary}\`` : "the client's own component library";
|
|
94
|
+
lines.push(`- This fork owns its components. Build from ${lib}. Never import \`@behavioral-agentic-design-system/core/components/ui/*\`.`);
|
|
95
|
+
}
|
|
96
|
+
if (mcp) {
|
|
97
|
+
lines.push(`- The \`${SERVER_NAME}\` MCP server is connected. Before you use a component, call \`get_contract\` with its name. Before you finish UI work, call \`validate_usage\` on the JSX and fix every error. When no component fits, call \`find_alternative\`.`);
|
|
98
|
+
}
|
|
99
|
+
if (contracts) {
|
|
100
|
+
lines.push(`- From a terminal, ask the fork's own registry: \`${contracts.run("get_contract <Name>")}\`, \`${contracts.run("validate_usage <file>")}\`, and \`${contracts.run("find_alternative <need>")}\`.`);
|
|
101
|
+
} else if (lookup) {
|
|
102
|
+
lines.push(`- From a terminal, run \`${lookup} contract <Name>\`, \`${lookup} validate <file>\`, and \`${lookup} alternative <need>\`.`);
|
|
103
|
+
}
|
|
104
|
+
lines.push("- If a component, prop, variant, or pattern is not in the registry, it does not exist. Say so and ask. Never invent one.");
|
|
105
|
+
lines.push("- Use semantic tokens. Never use hex colors or px spacing in component code.");
|
|
106
|
+
lines.push("", "This block is written by `bads connect`. To take it out, run `bads connect --remove`. Edit outside the markers.");
|
|
107
|
+
return lines.join("\n");
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function serverEntry(ctx, target) {
|
|
111
|
+
const rel = ctx.server.rel;
|
|
112
|
+
let entry;
|
|
113
|
+
if (target.style === "relative") entry = { command: "node", args: [rel] };
|
|
114
|
+
else if (target.style === "workspaceFolder") entry = { command: "node", args: [`\${workspaceFolder}/${rel}`], env: { BADS_ROOT: "${workspaceFolder}" } };
|
|
115
|
+
else {
|
|
116
|
+
entry = { command: "node", args: [join(ctx.root, rel)], env: { BADS_ROOT: ctx.root } };
|
|
117
|
+
if (target.cwdField || target.kind === "toml") entry.cwd = ctx.root;
|
|
118
|
+
}
|
|
119
|
+
return target.typeField ? { type: "stdio", ...entry } : entry;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
const tomlString = (s) => JSON.stringify(s);
|
|
123
|
+
export function renderToml(entry) {
|
|
124
|
+
const lines = [`[mcp_servers.${SERVER_NAME}]`, `command = ${tomlString(entry.command)}`, `args = [${entry.args.map(tomlString).join(", ")}]`];
|
|
125
|
+
if (entry.cwd) lines.push(`cwd = ${tomlString(entry.cwd)}`);
|
|
126
|
+
if (entry.env) lines.push(`env = { ${Object.entries(entry.env).map(([k, v]) => `${k} = ${tomlString(v)}`).join(", ")} }`);
|
|
127
|
+
return wrapBlock(lines.join("\n"), "toml");
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/** Read back the entry connect wrote in a TOML block. Returns null when there is none. */
|
|
131
|
+
export function parseTomlBlock(text) {
|
|
132
|
+
const found = text && findBlock(text, "toml");
|
|
133
|
+
if (!found) return null;
|
|
134
|
+
const body = text.slice(found.start, found.end);
|
|
135
|
+
const val = (k) => {
|
|
136
|
+
const m = new RegExp(`^${k} = (.+)$`, "m").exec(body);
|
|
137
|
+
return m ? m[1] : null;
|
|
138
|
+
};
|
|
139
|
+
const entry = { command: JSON.parse(val("command")), args: JSON.parse(val("args")) };
|
|
140
|
+
if (val("cwd")) entry.cwd = JSON.parse(val("cwd"));
|
|
141
|
+
const env = val("env");
|
|
142
|
+
if (env) entry.env = Object.fromEntries([...env.matchAll(/(\w+) = ("(?:[^"\\]|\\.)*")/g)].map((m) => [m[1], JSON.parse(m[2])]));
|
|
143
|
+
return entry;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function tomlHasForeignTable(text) {
|
|
147
|
+
if (!text) return false;
|
|
148
|
+
const found = findBlock(text, "toml");
|
|
149
|
+
const rest = found ? text.slice(0, found.start) + text.slice(found.end) : text;
|
|
150
|
+
return new RegExp(`^\\s*\\[mcp_servers\\.("?)${SERVER_NAME}\\1\\]`, "m").test(rest);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
export function targetPath(ctx, target) {
|
|
154
|
+
return target.scope === "global" ? target.globalPath(ctx) : join(ctx.root, target.path);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export function displayPath(ctx, abs) {
|
|
158
|
+
const inProject = relative(ctx.root, abs);
|
|
159
|
+
if (!inProject.startsWith("..") && !isAbsolute(inProject)) return inProject.split(sep).join("/");
|
|
160
|
+
const inHome = relative(ctx.home, abs);
|
|
161
|
+
if (!inHome.startsWith("..") && !isAbsolute(inHome)) return `~/${inHome.split(sep).join("/")}`;
|
|
162
|
+
return abs;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
export function readManifest(ctx) {
|
|
166
|
+
return readJson(join(ctx.root, MANIFEST)) ?? { version: 1, files: {} };
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
const editId = (target) => (target.kind === "json" ? `json:${target.keyPath.join(".")}` : target.kind);
|
|
170
|
+
|
|
171
|
+
/** Which tools to act on: explicit ids, --all, or the ones detected. */
|
|
172
|
+
export function selectTools(ctx, { tools, all } = {}) {
|
|
173
|
+
const detections = detectTools(ctx);
|
|
174
|
+
if (tools?.length) {
|
|
175
|
+
const unknown = tools.filter((t) => !TOOL_IDS.includes(t));
|
|
176
|
+
if (unknown.length) throw new Error(`Unknown tool: ${unknown.join(", ")}. Known: ${TOOL_IDS.join(", ")}`);
|
|
177
|
+
return detections.filter((d) => tools.includes(d.tool.id));
|
|
178
|
+
}
|
|
179
|
+
return all ? detections : detections.filter((d) => d.detected);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Build the plan. Nothing is written. Returns { changes, unchanged, skipped }. A change carries the file's text
|
|
184
|
+
* before and after. Several edits to one file chain, so a file is written once.
|
|
185
|
+
*/
|
|
186
|
+
export function plan(ctx, { tools, all, global = false, unverified = false } = {}) {
|
|
187
|
+
const selected = selectTools(ctx, { tools, all });
|
|
188
|
+
const manifest = readManifest(ctx);
|
|
189
|
+
const files = new Map();
|
|
190
|
+
const skipped = [];
|
|
191
|
+
const unchanged = [];
|
|
192
|
+
for (const { tool } of selected) {
|
|
193
|
+
const mcpWired = !!ctx.server;
|
|
194
|
+
for (const target of tool.targets) {
|
|
195
|
+
const abs = targetPath(ctx, target);
|
|
196
|
+
const shown = target.scope === "global" ? displayPath(ctx, abs) : target.path;
|
|
197
|
+
const skip = (reason) => skipped.push({ tool: tool.id, path: shown, scope: target.scope, reason });
|
|
198
|
+
if (target.scope === "global" && !global) {
|
|
199
|
+
if (target.verified || unverified) skip("user-level file: run again with --global to write it");
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (!target.verified && !unverified) {
|
|
203
|
+
skip(`format not confirmed by the docs, so off by default (--unverified to write it). ${target.note ?? ""}`.trim());
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (target.role === "mcp" && !ctx.server) {
|
|
207
|
+
skip("no BADS MCP server in this project (packages/mcp or the mcp package), so only the rules are written");
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
let file = files.get(abs);
|
|
211
|
+
if (!file) {
|
|
212
|
+
const before = readText(abs);
|
|
213
|
+
file = { abs, display: shown, scope: target.scope, before, after: before, edits: [], tools: new Set() };
|
|
214
|
+
files.set(abs, file);
|
|
215
|
+
}
|
|
216
|
+
const id = editId(target);
|
|
217
|
+
if (file.edits.some((e) => e.id === id)) {
|
|
218
|
+
file.tools.add(tool.id);
|
|
219
|
+
continue;
|
|
220
|
+
}
|
|
221
|
+
const owned = !!manifest.files[abs]?.edits?.some((e) => e.id === id);
|
|
222
|
+
try {
|
|
223
|
+
let res;
|
|
224
|
+
if (target.kind === "json") {
|
|
225
|
+
res = mergeJsonEntry(file.after, target.keyPath, serverEntry(ctx, target), { owned });
|
|
226
|
+
} else if (target.kind === "toml") {
|
|
227
|
+
if (tomlHasForeignTable(file.after)) throw new EditConflict(`[mcp_servers.${SERVER_NAME}] already exists outside connect's block. connect leaves it as it is`);
|
|
228
|
+
res = upsertBlock(file.after, renderToml(serverEntry(ctx, target)), { style: "toml" });
|
|
229
|
+
} else {
|
|
230
|
+
res = upsertBlock(file.after, wrapBlock(renderRules(ctx, { mcp: mcpWired })), { header: target.header ?? "" });
|
|
231
|
+
}
|
|
232
|
+
file.tools.add(tool.id);
|
|
233
|
+
const edit = { id, kind: target.kind, keyPath: target.keyPath ?? null, role: target.role, createdParents: res.createdParents ?? 0, docs: target.docs, verified: target.verified };
|
|
234
|
+
if (res.changed) {
|
|
235
|
+
file.after = res.text;
|
|
236
|
+
file.edits.push(edit);
|
|
237
|
+
} else {
|
|
238
|
+
file.edits.push({ ...edit, noop: true });
|
|
239
|
+
}
|
|
240
|
+
} catch (e) {
|
|
241
|
+
if (!(e instanceof EditConflict)) throw e;
|
|
242
|
+
skip(e.message);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
const changes = [];
|
|
247
|
+
for (const f of files.values()) {
|
|
248
|
+
const entry = { ...f, tools: [...f.tools] };
|
|
249
|
+
if (f.after !== f.before) changes.push(entry);
|
|
250
|
+
else if (f.edits.length) unchanged.push(entry);
|
|
251
|
+
}
|
|
252
|
+
return { selected: selected.map((d) => d.tool.id), changes, unchanged, skipped };
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
function backupName(ctx, abs) {
|
|
256
|
+
return join(ctx.root, BACKUPS, relative(ctx.root, abs).split(sep).join("__"));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
function mkdirRecorded(dir) {
|
|
260
|
+
const created = [];
|
|
261
|
+
let d = dir;
|
|
262
|
+
while (!existsSync(d)) {
|
|
263
|
+
created.unshift(d);
|
|
264
|
+
d = dirname(d);
|
|
265
|
+
}
|
|
266
|
+
if (created.length) mkdirSync(dir, { recursive: true });
|
|
267
|
+
return created;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** Write the plan. `onlyScopes` limits it (the CLI writes project changes, then global ones after asking). */
|
|
271
|
+
export function apply(ctx, planned, { onlyScopes = ["project", "global"] } = {}) {
|
|
272
|
+
const changes = planned.changes.filter((c) => onlyScopes.includes(c.scope));
|
|
273
|
+
if (!changes.length) return { written: [] };
|
|
274
|
+
const manifest = readManifest(ctx);
|
|
275
|
+
const stateCreatedDirs = existsSync(join(ctx.root, STATE_DIR)) ? null : mkdirRecorded(join(ctx.root, STATE_DIR));
|
|
276
|
+
if (stateCreatedDirs) {
|
|
277
|
+
writeFileSync(join(ctx.root, STATE_DIR, ".gitignore"), "# Written by `bads connect`: the manifest and backups stay local.\n*\n");
|
|
278
|
+
manifest.stateDirs = stateCreatedDirs.map((d) => relative(ctx.root, d));
|
|
279
|
+
}
|
|
280
|
+
const written = [];
|
|
281
|
+
for (const c of changes) {
|
|
282
|
+
const prior = manifest.files[c.abs];
|
|
283
|
+
const record = prior ?? { display: c.display, scope: c.scope, created: c.before == null, createdDirs: [], backup: null, edits: [] };
|
|
284
|
+
if (!prior && c.before != null) {
|
|
285
|
+
let b = c.scope === "global" ? `${c.abs}.bads-backup` : backupName(ctx, c.abs);
|
|
286
|
+
if (existsSync(b)) b = `${b}-${Date.now()}`;
|
|
287
|
+
mkdirSync(dirname(b), { recursive: true });
|
|
288
|
+
writeFileSync(b, c.before);
|
|
289
|
+
record.backup = b;
|
|
290
|
+
}
|
|
291
|
+
const dirs = mkdirRecorded(dirname(c.abs));
|
|
292
|
+
record.createdDirs.push(...dirs);
|
|
293
|
+
writeFileSync(c.abs, c.after);
|
|
294
|
+
record.sha256 = sha256(c.after);
|
|
295
|
+
record.tools = [...new Set([...(record.tools ?? []), ...c.tools])];
|
|
296
|
+
for (const e of c.edits.filter((x) => !x.noop)) {
|
|
297
|
+
if (!record.edits.some((x) => x.id === e.id)) record.edits.push({ id: e.id, kind: e.kind, keyPath: e.keyPath, createdParents: e.createdParents });
|
|
298
|
+
}
|
|
299
|
+
manifest.files[c.abs] = record;
|
|
300
|
+
written.push(c);
|
|
301
|
+
}
|
|
302
|
+
writeFileSync(join(ctx.root, MANIFEST), `${JSON.stringify(manifest, null, 2)}\n`);
|
|
303
|
+
return { written };
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
function removeEmptyDirs(dirs) {
|
|
307
|
+
for (const d of [...dirs].sort((a, b) => b.length - a.length)) {
|
|
308
|
+
try {
|
|
309
|
+
if (existsSync(d) && statSync(d).isDirectory() && readdirSync(d).length === 0) rmdirSync(d);
|
|
310
|
+
} catch {
|
|
311
|
+
/* a directory someone else filled stays */
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/** The text with connect's edits taken out, for a file that changed after connect wrote it. */
|
|
317
|
+
function stripEdits(text, record) {
|
|
318
|
+
let out = text;
|
|
319
|
+
for (const e of record.edits) {
|
|
320
|
+
if (e.kind === "json") out = removeJsonEntry(out, e.keyPath, { dropParents: e.createdParents }).text;
|
|
321
|
+
else out = removeBlock(out, { style: e.kind === "toml" ? "toml" : "md" }).text;
|
|
322
|
+
}
|
|
323
|
+
return out;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
const isEmptyLeftover = (text) => {
|
|
327
|
+
const t = text.replace(/^---\n[\s\S]*?\n---\n/, "").trim();
|
|
328
|
+
return t === "" || t === "{}";
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Undo what the manifest says connect wrote. Global files are undone only with `global`. Returns
|
|
333
|
+
* { restored, stripped, deleted, kept, missing } as lists of display paths.
|
|
334
|
+
*/
|
|
335
|
+
export function remove(ctx, { global = false, dryRun = false } = {}) {
|
|
336
|
+
const manifest = readManifest(ctx);
|
|
337
|
+
const out = { restored: [], stripped: [], deleted: [], kept: [], missing: [], diffs: [] };
|
|
338
|
+
const remaining = {};
|
|
339
|
+
const dirsToRemove = [];
|
|
340
|
+
for (const [abs, record] of Object.entries(manifest.files)) {
|
|
341
|
+
if (record.scope === "global" && !global) {
|
|
342
|
+
out.kept.push(record.display);
|
|
343
|
+
remaining[abs] = record;
|
|
344
|
+
continue;
|
|
345
|
+
}
|
|
346
|
+
const current = readText(abs);
|
|
347
|
+
if (current == null) {
|
|
348
|
+
out.missing.push(record.display);
|
|
349
|
+
} else if (sha256(current) === record.sha256) {
|
|
350
|
+
const next = record.created ? null : readText(record.backup);
|
|
351
|
+
out.diffs.push({ display: record.display, scope: record.scope, before: current, after: next });
|
|
352
|
+
if (!dryRun) {
|
|
353
|
+
if (next == null) rmSync(abs);
|
|
354
|
+
else writeFileSync(abs, next);
|
|
355
|
+
}
|
|
356
|
+
(next == null ? out.deleted : out.restored).push(record.display);
|
|
357
|
+
} else {
|
|
358
|
+
let next = stripEdits(current, record);
|
|
359
|
+
if (record.created && isEmptyLeftover(next)) next = null;
|
|
360
|
+
out.diffs.push({ display: record.display, scope: record.scope, before: current, after: next });
|
|
361
|
+
if (!dryRun) {
|
|
362
|
+
if (next == null) rmSync(abs);
|
|
363
|
+
else writeFileSync(abs, next);
|
|
364
|
+
}
|
|
365
|
+
out.stripped.push(record.display);
|
|
366
|
+
}
|
|
367
|
+
if (!dryRun) {
|
|
368
|
+
dirsToRemove.push(...(record.createdDirs ?? []));
|
|
369
|
+
if (record.backup && existsSync(record.backup)) rmSync(record.backup);
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
if (dryRun) return out;
|
|
373
|
+
// Folders connect created go last and deepest first, so a folder two files shared is empty by then.
|
|
374
|
+
removeEmptyDirs(dirsToRemove);
|
|
375
|
+
const manifestPath = join(ctx.root, MANIFEST);
|
|
376
|
+
if (Object.keys(remaining).length) {
|
|
377
|
+
writeFileSync(manifestPath, `${JSON.stringify({ ...manifest, files: remaining }, null, 2)}\n`);
|
|
378
|
+
} else if (existsSync(manifestPath)) {
|
|
379
|
+
rmSync(manifestPath);
|
|
380
|
+
const gi = join(ctx.root, STATE_DIR, ".gitignore");
|
|
381
|
+
if (existsSync(gi)) rmSync(gi);
|
|
382
|
+
removeEmptyDirs([join(ctx.root, BACKUPS)]);
|
|
383
|
+
removeEmptyDirs((manifest.stateDirs ?? []).map((d) => join(ctx.root, d)));
|
|
384
|
+
}
|
|
385
|
+
return out;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/** Whether a target's file holds a BADS entry now. "wired", "absent", "foreign" (someone else's entry) or "unreadable". */
|
|
389
|
+
export function targetStatus(ctx, target) {
|
|
390
|
+
const abs = targetPath(ctx, target);
|
|
391
|
+
const text = readText(abs);
|
|
392
|
+
if (text == null) return "absent";
|
|
393
|
+
if (target.kind === "block") return findBlock(text, "md") ? "wired" : "absent";
|
|
394
|
+
if (target.kind === "toml") return parseTomlBlock(text) ? "wired" : tomlHasForeignTable(text) ? "foreign" : "absent";
|
|
395
|
+
const parsed = parseJsonConfig(text);
|
|
396
|
+
if (!parsed.ok) return "unreadable";
|
|
397
|
+
return getPath(parsed.value, target.keyPath) ? "wired" : "absent";
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/** The server entry a target's file holds now, resolved for spawning, or null. */
|
|
401
|
+
export function readEntry(ctx, target) {
|
|
402
|
+
const abs = targetPath(ctx, target);
|
|
403
|
+
const text = readText(abs);
|
|
404
|
+
if (text == null) return null;
|
|
405
|
+
if (target.kind === "toml") return parseTomlBlock(text);
|
|
406
|
+
if (target.kind !== "json") return null;
|
|
407
|
+
const parsed = parseJsonConfig(text);
|
|
408
|
+
return parsed.ok ? getPath(parsed.value, target.keyPath) ?? null : null;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
export { TOML_START, TOML_END };
|