@kosdev-code/kos-ddk-cli 0.0.0-stage → 0.1.0-dev.50

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.
Files changed (116) hide show
  1. package/README.md +129 -2
  2. package/agent-plugin/.claude-plugin/marketplace.json +13 -0
  3. package/agent-plugin/kos-ddk-tools/.claude-plugin/plugin.json +8 -0
  4. package/agent-plugin/kos-ddk-tools/.mcp.json +11 -0
  5. package/agent-plugin/kos-ddk-tools/agents/kos-ddk-plugin-planner.md +40 -0
  6. package/agent-plugin/kos-ddk-tools/agents/kos-ddk-plugin-reviewer.md +33 -0
  7. package/agent-plugin/kos-ddk-tools/catalog/contributions.json +888 -0
  8. package/agent-plugin/kos-ddk-tools/mcp.json +12 -0
  9. package/agent-plugin/kos-ddk-tools/plugin.json +7 -0
  10. package/agent-plugin/kos-ddk-tools/reference/reference.json +19288 -0
  11. package/agent-plugin/kos-ddk-tools/servers/kos-ddk.mjs +48632 -0
  12. package/agent-plugin/kos-ddk-tools/skills/build-ddk-plugin/SKILL.md +71 -0
  13. package/agent-plugin/kos-ddk-tools/skills/ddk-content-component/SKILL.md +49 -0
  14. package/agent-plugin/kos-ddk-tools/skills/ddk-storybook/SKILL.md +39 -0
  15. package/agent-plugin/kos-ddk-tools/templates/cui/__nameDashCase__.stories.tsx__template__ +30 -0
  16. package/agent-plugin/kos-ddk-tools/templates/cui/__nameDashCase__.tsx__template__ +35 -0
  17. package/agent-plugin/kos-ddk-tools/templates/cui/index.ts__template__ +1 -0
  18. package/agent-plugin/kos-ddk-tools/templates/dashboard-detail/detail/__nameDashCase__.stories.tsx__template__ +32 -0
  19. package/agent-plugin/kos-ddk-tools/templates/dashboard-detail/detail/__nameDashCase__.tsx__template__ +52 -0
  20. package/agent-plugin/kos-ddk-tools/templates/dashboard-detail/detail/index.ts__template__ +1 -0
  21. package/agent-plugin/kos-ddk-tools/templates/dashboard-summary/summary/__nameDashCase__.stories.tsx__template__ +32 -0
  22. package/agent-plugin/kos-ddk-tools/templates/dashboard-summary/summary/__nameDashCase__.tsx__template__ +41 -0
  23. package/agent-plugin/kos-ddk-tools/templates/dashboard-summary/summary/index.ts__template__ +1 -0
  24. package/agent-plugin/kos-ddk-tools/templates/macro-summary/__nameDashCase__.stories.tsx__template__ +32 -0
  25. package/agent-plugin/kos-ddk-tools/templates/macro-summary/__nameDashCase__.tsx__template__ +24 -0
  26. package/agent-plugin/kos-ddk-tools/templates/macro-summary/index.ts__template__ +1 -0
  27. package/agent-plugin/kos-ddk-tools/templates/nav/__nameDashCase__.stories.tsx__template__ +30 -0
  28. package/agent-plugin/kos-ddk-tools/templates/nav/__nameDashCase__.tsx__template__ +27 -0
  29. package/agent-plugin/kos-ddk-tools/templates/nav/index.ts__template__ +1 -0
  30. package/agent-plugin/kos-ddk-tools/templates/plugin-project/.storybook/preview.tsx.template +80 -0
  31. package/agent-plugin/kos-ddk-tools/templates/plugin-project/.storybook/registration.ts.template +6 -0
  32. package/agent-plugin/kos-ddk-tools/templates/pump-detail/__nameDashCase__.stories.tsx__template__ +32 -0
  33. package/agent-plugin/kos-ddk-tools/templates/pump-detail/__nameDashCase__.tsx__template__ +38 -0
  34. package/agent-plugin/kos-ddk-tools/templates/pump-detail/index.ts__template__ +1 -0
  35. package/agent-plugin/kos-ddk-tools/templates/pump-summary/__nameDashCase__.stories.tsx__template__ +32 -0
  36. package/agent-plugin/kos-ddk-tools/templates/pump-summary/__nameDashCase__.tsx__template__ +25 -0
  37. package/agent-plugin/kos-ddk-tools/templates/pump-summary/index.ts__template__ +1 -0
  38. package/agent-plugin/kos-ddk-tools/templates/setting/__nameDashCase__.stories.tsx__template__ +30 -0
  39. package/agent-plugin/kos-ddk-tools/templates/setting/__nameDashCase__.tsx__template__ +27 -0
  40. package/agent-plugin/kos-ddk-tools/templates/setting/index.ts__template__ +1 -0
  41. package/agent-plugin/kos-ddk-tools/templates/setup/__nameDashCase__.stories.tsx__template__ +30 -0
  42. package/agent-plugin/kos-ddk-tools/templates/setup/__nameDashCase__.tsx__template__ +33 -0
  43. package/agent-plugin/kos-ddk-tools/templates/setup/index.ts__template__ +1 -0
  44. package/agent-plugin/kos-ddk-tools/templates/theme-project/theme/assets/component.css.template +2 -0
  45. package/agent-plugin/kos-ddk-tools/templates/theme-project/theme/assets/generic.css.template +2 -0
  46. package/agent-plugin/kos-ddk-tools/templates/theme-project/theme/assets/semantic.css.template +2 -0
  47. package/agent-plugin/kos-ddk-tools/templates/theme-project/theme/assets/theme-overlay.css.template +7 -0
  48. package/agent-plugin/kos-ddk-tools/templates/trouble-action/__nameDashCase__.stories.tsx__template__ +29 -0
  49. package/agent-plugin/kos-ddk-tools/templates/trouble-action/__nameDashCase__.tsx__template__ +57 -0
  50. package/agent-plugin/kos-ddk-tools/templates/trouble-action/index.ts__template__ +1 -0
  51. package/agent-plugin/kos-ddk-tools/templates/utility/__nameDashCase__.stories.tsx__template__ +30 -0
  52. package/agent-plugin/kos-ddk-tools/templates/utility/__nameDashCase__.tsx__template__ +47 -0
  53. package/agent-plugin/kos-ddk-tools/templates/utility/index.ts__template__ +1 -0
  54. package/catalog/contributions.json +888 -0
  55. package/guides/ddk-dev.md +46 -0
  56. package/guides/storybook.md +69 -0
  57. package/package.json +33 -3
  58. package/reference/reference.json +19288 -0
  59. package/src/build-paths.mjs +20 -0
  60. package/src/codegen/contribution.mjs +255 -0
  61. package/src/codegen/overrides.mjs +179 -0
  62. package/src/codegen/project.mjs +144 -0
  63. package/src/extension.mjs +22 -0
  64. package/src/generators/agent-install.mjs +79 -0
  65. package/src/generators/catalog.mjs +70 -0
  66. package/src/generators/contributions.mjs +116 -0
  67. package/src/generators/dashboard.mjs +238 -0
  68. package/src/generators/dev.mjs +221 -0
  69. package/src/generators/override-prompts.mjs +27 -0
  70. package/src/generators/plugin-project.mjs +34 -0
  71. package/src/generators/theme-project.mjs +34 -0
  72. package/src/handlers.mjs +325 -0
  73. package/src/mcp/pkg-info.mjs +7 -0
  74. package/src/mcp/plugin-inspect.mjs +257 -0
  75. package/src/mcp/reference.mjs +248 -0
  76. package/src/mcp/server.mjs +12 -0
  77. package/src/mcp/tools.mjs +478 -0
  78. package/templates/cui/__nameDashCase__.stories.tsx__template__ +30 -0
  79. package/templates/cui/__nameDashCase__.tsx__template__ +35 -0
  80. package/templates/cui/index.ts__template__ +1 -0
  81. package/templates/dashboard-detail/detail/__nameDashCase__.stories.tsx__template__ +32 -0
  82. package/templates/dashboard-detail/detail/__nameDashCase__.tsx__template__ +52 -0
  83. package/templates/dashboard-detail/detail/index.ts__template__ +1 -0
  84. package/templates/dashboard-summary/summary/__nameDashCase__.stories.tsx__template__ +32 -0
  85. package/templates/dashboard-summary/summary/__nameDashCase__.tsx__template__ +41 -0
  86. package/templates/dashboard-summary/summary/index.ts__template__ +1 -0
  87. package/templates/macro-summary/__nameDashCase__.stories.tsx__template__ +32 -0
  88. package/templates/macro-summary/__nameDashCase__.tsx__template__ +24 -0
  89. package/templates/macro-summary/index.ts__template__ +1 -0
  90. package/templates/nav/__nameDashCase__.stories.tsx__template__ +30 -0
  91. package/templates/nav/__nameDashCase__.tsx__template__ +27 -0
  92. package/templates/nav/index.ts__template__ +1 -0
  93. package/templates/plugin-project/.storybook/preview.tsx.template +80 -0
  94. package/templates/plugin-project/.storybook/registration.ts.template +6 -0
  95. package/templates/pump-detail/__nameDashCase__.stories.tsx__template__ +32 -0
  96. package/templates/pump-detail/__nameDashCase__.tsx__template__ +38 -0
  97. package/templates/pump-detail/index.ts__template__ +1 -0
  98. package/templates/pump-summary/__nameDashCase__.stories.tsx__template__ +32 -0
  99. package/templates/pump-summary/__nameDashCase__.tsx__template__ +25 -0
  100. package/templates/pump-summary/index.ts__template__ +1 -0
  101. package/templates/setting/__nameDashCase__.stories.tsx__template__ +30 -0
  102. package/templates/setting/__nameDashCase__.tsx__template__ +27 -0
  103. package/templates/setting/index.ts__template__ +1 -0
  104. package/templates/setup/__nameDashCase__.stories.tsx__template__ +30 -0
  105. package/templates/setup/__nameDashCase__.tsx__template__ +33 -0
  106. package/templates/setup/index.ts__template__ +1 -0
  107. package/templates/theme-project/theme/assets/component.css.template +2 -0
  108. package/templates/theme-project/theme/assets/generic.css.template +2 -0
  109. package/templates/theme-project/theme/assets/semantic.css.template +2 -0
  110. package/templates/theme-project/theme/assets/theme-overlay.css.template +7 -0
  111. package/templates/trouble-action/__nameDashCase__.stories.tsx__template__ +29 -0
  112. package/templates/trouble-action/__nameDashCase__.tsx__template__ +57 -0
  113. package/templates/trouble-action/index.ts__template__ +1 -0
  114. package/templates/utility/__nameDashCase__.stories.tsx__template__ +30 -0
  115. package/templates/utility/__nameDashCase__.tsx__template__ +47 -0
  116. package/templates/utility/index.ts__template__ +1 -0
@@ -0,0 +1,20 @@
1
+ // Where the build writes the contribution catalog and the reference; the agent-plugin
2
+ // bundle swaps this module for its own copies' locations.
3
+ import path from "node:path";
4
+ import { fileURLToPath } from "node:url";
5
+
6
+ const packageRoot = path.resolve(
7
+ path.dirname(fileURLToPath(import.meta.url)),
8
+ ".."
9
+ );
10
+
11
+ export const CATALOG_PATH = path.join(
12
+ packageRoot,
13
+ "catalog",
14
+ "contributions.json"
15
+ );
16
+ export const REFERENCE_PATH = path.join(
17
+ packageRoot,
18
+ "reference",
19
+ "reference.json"
20
+ );
@@ -0,0 +1,255 @@
1
+ // DDK contributions (component, story, index, .kos.json entry, i18n title) in a
2
+ // plugin project. Shared by the ddk:* commands and the DDK MCP tools.
3
+ import { generateComponent } from "@kosdev-code/kos-codegen-core";
4
+ import { TEMPLATES_DIR, registerDdkHandlers } from "../handlers.mjs";
5
+ import { resolveOverride } from "./overrides.mjs";
6
+
7
+ /** Extension point → plugin type and what a contribution of it needs. */
8
+ export const CONTRIBUTION_TYPES = {
9
+ cui: { pluginType: "ddk.cui", label: "DDK CUI Configuration" },
10
+ setup: {
11
+ pluginType: "ddk.setup",
12
+ label: "DDK Setup Step",
13
+ overridable: "setupDescriptor",
14
+ },
15
+ utility: {
16
+ pluginType: "ddk.utility",
17
+ label: "DDK Utility",
18
+ overridable: "utilDescriptor",
19
+ },
20
+ setting: { pluginType: "ddk.setting", label: "DDK Setting", group: true },
21
+ nav: {
22
+ pluginType: "ddk.nav",
23
+ label: "DDK Navigation View",
24
+ overridable: "navDescriptor",
25
+ },
26
+ };
27
+
28
+ function generate(codegenFs, pluginType, { name, project, ...extra }) {
29
+ registerDdkHandlers();
30
+ generateComponent(codegenFs, TEMPLATES_DIR, {
31
+ name,
32
+ appProject: project,
33
+ type: "components",
34
+ pluginType,
35
+ useEmotionCss: false,
36
+ appDirectory: "",
37
+ modelDirectory: "",
38
+ ...extra,
39
+ });
40
+ }
41
+
42
+ function requireOption(value, flag) {
43
+ if (!value) throw new Error(`${flag} is required`);
44
+ }
45
+
46
+ const rankField = (rank) => (rank === undefined ? {} : { rank });
47
+
48
+ /**
49
+ * @param {{type: keyof CONTRIBUTION_TYPES, name: string, project: string, group?: string,
50
+ * descriptor?: string, override?: string, rank?: number|string}} options
51
+ * `descriptor`, `override` and `rank` apply to the rankable types (setup, utility, nav).
52
+ */
53
+ export function generateDdkContribution(
54
+ codegenFs,
55
+ { type, name, project, group, descriptor, override, rank }
56
+ ) {
57
+ const spec = CONTRIBUTION_TYPES[type];
58
+ if (!spec) {
59
+ throw new Error(
60
+ `Unknown contribution type "${type}". Choose from ${Object.keys(
61
+ CONTRIBUTION_TYPES
62
+ ).join(", ")}.`
63
+ );
64
+ }
65
+ if (spec.group && !group) {
66
+ throw new Error("Settings group is required for setting plugin type");
67
+ }
68
+ const ranked = override || descriptor || (rank !== undefined && rank !== "");
69
+ if (!spec.overridable && ranked) {
70
+ throw new Error(
71
+ `${type} contributions are not ranked; --override, --descriptor and --rank do not apply`
72
+ );
73
+ }
74
+ const resolved = spec.overridable
75
+ ? resolveOverride(type, {
76
+ override,
77
+ rank,
78
+ explicit: { [spec.overridable]: descriptor },
79
+ })
80
+ : { fields: {}, notes: [] };
81
+ const slot = resolved.fields[spec.overridable] ?? descriptor;
82
+ generate(codegenFs, spec.pluginType, {
83
+ name,
84
+ project,
85
+ ...(group ? { group } : {}),
86
+ ...(slot ? { descriptor: slot } : {}),
87
+ ...rankField(resolved.rank),
88
+ });
89
+ return { label: spec.label, notes: resolved.notes };
90
+ }
91
+
92
+ /**
93
+ * A dashboard summary card and, unless `detail` is false, the detail its card
94
+ * opens; both share `dashboardKey` and `rank`. `override` names a standard
95
+ * summary or detail whose dashboardKey to take over.
96
+ */
97
+ export function generateDashboardCard(
98
+ codegenFs,
99
+ { name, project, dashboardKey, detail = true, override, rank }
100
+ ) {
101
+ const resolved = resolveOverride("dashboard-card", {
102
+ override,
103
+ rank,
104
+ explicit: { dashboardKey },
105
+ });
106
+ const key = resolved.fields.dashboardKey ?? dashboardKey;
107
+ requireOption(key, "dashboardKey");
108
+ const options = {
109
+ name,
110
+ project,
111
+ dashboardKey: key,
112
+ ...rankField(resolved.rank),
113
+ };
114
+ generate(codegenFs, "ddk.dashboardSummary", options);
115
+ if (detail) {
116
+ generate(codegenFs, "ddk.dashboardDetail", options);
117
+ }
118
+ return {
119
+ label: detail ? "DDK Dashboard Card" : "DDK Dashboard Summary",
120
+ notes: [
121
+ `The dashboard draws one card per active model at assembly.${key} (or the FreestyleAssembly companion's ${key}); no such model, no card.`,
122
+ ...(detail
123
+ ? ["The detail opens when a technician selects the card."]
124
+ : []),
125
+ ...resolved.notes,
126
+ ],
127
+ };
128
+ }
129
+
130
+ /** Pump detail for holders whose dashboard key is `dashboardKey`. */
131
+ export function generatePumpDetail(
132
+ codegenFs,
133
+ { name, project, dashboardKey, override, rank }
134
+ ) {
135
+ const resolved = resolveOverride("pump-detail", {
136
+ override,
137
+ rank,
138
+ explicit: { dashboardKey },
139
+ });
140
+ const key = resolved.fields.dashboardKey ?? dashboardKey;
141
+ requireOption(key, "dashboardKey");
142
+ generate(codegenFs, "ddk.pumpDetail", {
143
+ name,
144
+ project,
145
+ dashboardKey: key,
146
+ ...rankField(resolved.rank),
147
+ });
148
+ return {
149
+ label: "DDK Pump Detail",
150
+ notes: [
151
+ `Shown in the holder modal when the dashboard key factory returns "${key}" for the holder (default: its first pump's type), unless the holder has its own content.`,
152
+ ...resolved.notes,
153
+ ],
154
+ };
155
+ }
156
+
157
+ /**
158
+ * A pump card for holders whose dashboard key is `dashboardKey`: the summary body on
159
+ * the holder's dashboard card and, unless `detail` is false, the pump detail in its
160
+ * modal; both share `dashboardKey` and `rank`. `override` names a standard pump
161
+ * summary or detail whose dashboardKey to take over.
162
+ */
163
+ export function generatePumpCard(
164
+ codegenFs,
165
+ { name, project, dashboardKey, detail = true, override, rank }
166
+ ) {
167
+ const resolved = resolveOverride("pump-card", {
168
+ override,
169
+ rank,
170
+ explicit: { dashboardKey },
171
+ });
172
+ const key = resolved.fields.dashboardKey ?? dashboardKey;
173
+ requireOption(key, "dashboardKey");
174
+ const options = {
175
+ name,
176
+ project,
177
+ dashboardKey: key,
178
+ ...rankField(resolved.rank),
179
+ };
180
+ generate(codegenFs, "ddk.pumpSummary", options);
181
+ if (detail) {
182
+ generate(codegenFs, "ddk.pumpDetail", options);
183
+ }
184
+ return {
185
+ label: detail ? "DDK Pump Card" : "DDK Pump Summary",
186
+ notes: [
187
+ `Used for holders whose dashboard key factory returns "${key}" (default: the holder's first pump type, which the backend assembly assigns); other holders keep the default card body.`,
188
+ ...(detail
189
+ ? [
190
+ "The detail shows in the holder modal a technician opens from the card.",
191
+ ]
192
+ : []),
193
+ ...resolved.notes,
194
+ ],
195
+ };
196
+ }
197
+
198
+ /** The macro holder card body for `dashboardKey`. */
199
+ export function generateMacroSummary(
200
+ codegenFs,
201
+ { name, project, dashboardKey, override, rank }
202
+ ) {
203
+ const resolved = resolveOverride("macro-summary", {
204
+ override,
205
+ rank,
206
+ explicit: { dashboardKey },
207
+ });
208
+ const key = resolved.fields.dashboardKey ?? dashboardKey;
209
+ requireOption(key, "dashboardKey");
210
+ generate(codegenFs, "ddk.macroSummary", {
211
+ name,
212
+ project,
213
+ dashboardKey: key,
214
+ ...rankField(resolved.rank),
215
+ });
216
+ return {
217
+ label: "DDK Macro Summary",
218
+ notes: [
219
+ `Matched to a macro holder by holder.name "${key}" first, then by the dashboard key factory.`,
220
+ ...resolved.notes,
221
+ ],
222
+ };
223
+ }
224
+
225
+ /**
226
+ * The experience shown while the device has an active `troubleType` trouble.
227
+ * Contributions sharing a stateKey share one experience.
228
+ */
229
+ export function generateTroubleAction(
230
+ codegenFs,
231
+ { name, project, troubleType, stateKey, stateValue, override, rank }
232
+ ) {
233
+ const resolved = resolveOverride("trouble-action", {
234
+ override,
235
+ rank,
236
+ explicit: { troubleType, stateKey, stateValue },
237
+ });
238
+ const type = resolved.fields.troubleType ?? troubleType;
239
+ requireOption(type, "troubleType");
240
+ generate(codegenFs, "ddk.troubleAction", {
241
+ name,
242
+ project,
243
+ troubleType: type,
244
+ stateKey: resolved.fields.stateKey ?? (stateKey || type),
245
+ stateValue: resolved.fields.stateValue ?? (stateValue || type),
246
+ ...rankField(resolved.rank),
247
+ });
248
+ return {
249
+ label: "DDK Trouble Action",
250
+ notes: [
251
+ `troubleType "${type}" must be the exact type the device raises; the experience shows while one is active and goes away once it is resolved.`,
252
+ ...resolved.notes,
253
+ ],
254
+ };
255
+ }
@@ -0,0 +1,179 @@
1
+ // Overriding a contribution of a rankable extension point: a new contribution to
2
+ // the same point, with the same key, whose rank the host prefers. Lower rank wins
3
+ // and an unranked contribution loses to any ranked one.
4
+ import { existsSync, readFileSync } from "node:fs";
5
+ import { CATALOG_PATH } from "../build-paths.mjs";
6
+
7
+ export { CATALOG_PATH };
8
+
9
+ /** Rank given to an override of an unranked contribution. */
10
+ export const DEFAULT_OVERRIDE_RANK = 100;
11
+ export const MAX_RANK = 1000;
12
+
13
+ /** Command → the .kos.json keys it contributes to and the fields that name the slot. */
14
+ export const OVERRIDABLE_POINTS = {
15
+ utility: { contributionKeys: ["utilities"], keys: ["utilDescriptor"] },
16
+ setup: { contributionKeys: ["setupStep"], keys: ["setupDescriptor"] },
17
+ nav: { contributionKeys: ["navViews"], keys: ["navDescriptor"] },
18
+ "dashboard-card": {
19
+ contributionKeys: ["dashboardSummary", "dashboardDetail"],
20
+ keys: ["dashboardKey"],
21
+ },
22
+ "pump-detail": {
23
+ contributionKeys: ["dashboardPumpDetail"],
24
+ keys: ["dashboardKey"],
25
+ },
26
+ "pump-card": {
27
+ contributionKeys: ["dashboardPumpSummary", "dashboardPumpDetail"],
28
+ keys: ["dashboardKey"],
29
+ },
30
+ "macro-summary": {
31
+ contributionKeys: ["dashboardMacroSummary"],
32
+ keys: ["dashboardKey"],
33
+ },
34
+ "trouble-action": {
35
+ contributionKeys: ["troubleActions"],
36
+ keys: ["troubleType", "stateKey", "stateValue"],
37
+ },
38
+ };
39
+
40
+ let catalog;
41
+
42
+ /** The standard plugins' contributions to rankable points, extracted at build time. */
43
+ export function loadCatalog() {
44
+ if (!catalog) {
45
+ if (!existsSync(CATALOG_PATH)) {
46
+ throw new Error(
47
+ `Contribution catalog missing at ${CATALOG_PATH}; it is written by the kos-ddk-cli build.`
48
+ );
49
+ }
50
+ catalog = JSON.parse(readFileSync(CATALOG_PATH, "utf-8"));
51
+ }
52
+ return catalog;
53
+ }
54
+
55
+ /**
56
+ * Each standard contribution once, though a pump detail is listed under both
57
+ * ddk:pump-detail and ddk:pump-card.
58
+ */
59
+ export function uniqueContributions(entries) {
60
+ const seen = new Set();
61
+ return entries.filter((entry) => {
62
+ const key = `${entry.plugin} ${entry.contributionKey} ${entry.id}`;
63
+ if (seen.has(key)) return false;
64
+ seen.add(key);
65
+ return true;
66
+ });
67
+ }
68
+
69
+ /** Contributions a ddk:<point> command can override, optionally for one point. */
70
+ export function listOverridableContributions(point) {
71
+ if (point && !OVERRIDABLE_POINTS[point]) {
72
+ throw new Error(
73
+ `Unknown point "${point}". Choose from ${Object.keys(
74
+ OVERRIDABLE_POINTS
75
+ ).join(", ")}.`
76
+ );
77
+ }
78
+ return loadCatalog().contributions.filter(
79
+ (entry) => !point || entry.point === point
80
+ );
81
+ }
82
+
83
+ /** Parses a rank from the CLI or a tool call; undefined when none was given. */
84
+ export function parseRank(rank) {
85
+ if (rank === undefined || rank === null || rank === "") return undefined;
86
+ const value = typeof rank === "number" ? rank : Number(rank);
87
+ if (!Number.isInteger(value) || value < 0 || value > MAX_RANK) {
88
+ throw new Error(
89
+ `rank must be a whole number from 0 to ${MAX_RANK}; got "${rank}"`
90
+ );
91
+ }
92
+ return value;
93
+ }
94
+
95
+ const describeRank = (rank) =>
96
+ typeof rank === "number" ? `rank ${rank}` : "unranked";
97
+
98
+ /**
99
+ * Resolves `--override <id>` and `--rank` for a ddk:<point> command.
100
+ *
101
+ * Returns the slot fields copied from the overridden contribution, the rank to
102
+ * write, and notes for the report. `explicit` holds slot fields the caller also
103
+ * gave, which must agree with the overridden contribution.
104
+ */
105
+ export function resolveOverride(point, { override, rank, explicit = {} }) {
106
+ const spec = OVERRIDABLE_POINTS[point];
107
+ const requested = parseRank(rank);
108
+ if (!override) {
109
+ return { fields: {}, rank: requested, notes: [] };
110
+ }
111
+
112
+ // The standard and compact plugins can share an id; both fill the same slot.
113
+ const matches = loadCatalog().contributions.filter(
114
+ (entry) => entry.id === override && entry.point === point
115
+ );
116
+ const target = matches[0];
117
+ if (!target) {
118
+ throw new Error(
119
+ `No ${point} contribution "${override}" to override. List them with: kosui ddk:contributions --point ${point}`
120
+ );
121
+ }
122
+
123
+ const fields = Object.fromEntries(
124
+ spec.keys
125
+ .filter((key) => target.keys[key] !== undefined)
126
+ .map((key) => [key, target.keys[key]])
127
+ );
128
+ const sameSlot = (entry) =>
129
+ spec.keys.every((key) => entry.keys[key] === target.keys[key]);
130
+ if (!matches.every(sameSlot)) {
131
+ throw new Error(
132
+ `${override} fills different slots in ${matches
133
+ .map((entry) => entry.plugin)
134
+ .join(" and ")}; give the slot and --rank instead of --override`
135
+ );
136
+ }
137
+ for (const [key, value] of Object.entries(explicit)) {
138
+ if (value && fields[key] !== undefined && value !== fields[key]) {
139
+ throw new Error(
140
+ `--${key} "${value}" conflicts with ${override}, whose ${key} is "${fields[key]}"`
141
+ );
142
+ }
143
+ }
144
+
145
+ const ranks = matches
146
+ .map((entry) => entry.rank)
147
+ .filter((rank) => typeof rank === "number");
148
+ const targetRank = ranks.length ? Math.min(...ranks) : undefined;
149
+ let resolved = requested;
150
+ if (resolved === undefined) {
151
+ if (typeof targetRank !== "number") resolved = DEFAULT_OVERRIDE_RANK;
152
+ else if (targetRank > 0) resolved = targetRank - 1;
153
+ else
154
+ throw new Error(
155
+ `${override} has rank 0, which no rank can beat; ties are broken by id`
156
+ );
157
+ } else if (typeof targetRank === "number" && resolved >= targetRank) {
158
+ throw new Error(
159
+ `rank ${resolved} does not outrank ${override} (rank ${targetRank}); lower rank wins`
160
+ );
161
+ }
162
+
163
+ const slot = Object.entries(fields)
164
+ .map(([key, value]) => `${key} "${value}"`)
165
+ .join(", ");
166
+ return {
167
+ fields,
168
+ rank: resolved,
169
+ notes: [
170
+ `Overrides ${override} from ${matches
171
+ .map((entry) => entry.plugin)
172
+ .join(
173
+ " and "
174
+ )} (${slot}) with rank ${resolved}; lower rank wins, and ${override} is ${describeRank(
175
+ targetRank
176
+ )}.`,
177
+ ],
178
+ };
179
+ }
@@ -0,0 +1,144 @@
1
+ // DDK plugin and theme projects: kos-nx-plugin's generic project generators run with
2
+ // the DDK KAB type, then the DDK layer from templates/<kind>-project is written on top.
3
+ import { spawnSync } from "node:child_process";
4
+ import fs from "node:fs";
5
+ import path from "node:path";
6
+ import { findProjectByName } from "@kosdev-code/kos-codegen-core";
7
+ import { TEMPLATES_DIR } from "../handlers.mjs";
8
+
9
+ export const DDK_VERSION = "~3.1.0";
10
+
11
+ const DDK_PACKAGES = [
12
+ "@kosdev-code/kos-ddk",
13
+ "@kosdev-code/kos-ddk-components",
14
+ "@kosdev-code/kos-ddk-model-components",
15
+ "@kosdev-code/kos-ddk-models",
16
+ "@kosdev-code/kos-ddk-styles",
17
+ "@kosdev-code/kos-ddk-story-containers",
18
+ ];
19
+
20
+ const PLUGIN_CONTRIBUTES = {
21
+ cui: [],
22
+ cuiView: [],
23
+ utilities: [],
24
+ utilityView: [],
25
+ views: {},
26
+ };
27
+
28
+ const readJson = (file) => JSON.parse(fs.readFileSync(file, "utf-8"));
29
+ const writeJson = (file, json) =>
30
+ fs.writeFileSync(file, JSON.stringify(json, null, 2) + "\n");
31
+
32
+ function templateFiles(kind) {
33
+ const dir = path.join(TEMPLATES_DIR, `${kind}-project`);
34
+ return fs
35
+ .readdirSync(dir, { recursive: true, withFileTypes: true })
36
+ .filter((entry) => entry.isFile())
37
+ .map((entry) =>
38
+ path
39
+ .relative(dir, path.join(entry.parentPath, entry.name))
40
+ .replace(/\.template$/, "")
41
+ )
42
+ .sort();
43
+ }
44
+
45
+ function writeTemplates(kind, projectRoot) {
46
+ const dir = path.join(TEMPLATES_DIR, `${kind}-project`);
47
+ for (const file of templateFiles(kind)) {
48
+ const target = path.join(projectRoot, file);
49
+ fs.mkdirSync(path.dirname(target), { recursive: true });
50
+ fs.copyFileSync(path.join(dir, `${file}.template`), target);
51
+ }
52
+ }
53
+
54
+ function overlayPlugin(workspaceRoot, projectRoot) {
55
+ writeTemplates("plugin", projectRoot);
56
+
57
+ const kosJsonPath = path.join(projectRoot, ".kos.json");
58
+ const kosJson = readJson(kosJsonPath);
59
+ const plugin = kosJson.kos.ui.plugin;
60
+ plugin.contributes = { ...PLUGIN_CONTRIBUTES, ...plugin.contributes };
61
+ writeJson(kosJsonPath, kosJson);
62
+
63
+ const packageJsonPath = path.join(workspaceRoot, "package.json");
64
+ const packageJson = readJson(packageJsonPath);
65
+ const installed = {
66
+ ...packageJson.dependencies,
67
+ ...packageJson.devDependencies,
68
+ };
69
+ const missing = DDK_PACKAGES.filter((name) => !installed[name]);
70
+ if (missing.length) {
71
+ packageJson.dependencies = { ...packageJson.dependencies };
72
+ for (const name of missing) packageJson.dependencies[name] = DDK_VERSION;
73
+ writeJson(packageJsonPath, packageJson);
74
+ }
75
+ }
76
+
77
+ function overlayTheme(_workspaceRoot, projectRoot) {
78
+ writeTemplates("theme", projectRoot);
79
+ fs.rmSync(path.join(projectRoot, "theme", "assets", ".gitkeep"), {
80
+ force: true,
81
+ });
82
+ }
83
+
84
+ const PROJECT_KINDS = {
85
+ plugin: {
86
+ generator: "@kosdev-code/kos-nx-plugin:kos-plugin-project",
87
+ uiType: "ddk.ncui.plugin",
88
+ overlay: overlayPlugin,
89
+ },
90
+ theme: {
91
+ generator: "@kosdev-code/kos-nx-plugin:kos-theme-project",
92
+ uiType: "ddk.ncui.theme",
93
+ overlay: overlayTheme,
94
+ },
95
+ };
96
+
97
+ /** Writes the DDK layer over a generic plugin or theme project at `projectRoot`. */
98
+ export function applyDdkLayer(kind, workspaceRoot, projectRoot) {
99
+ PROJECT_KINDS[kind].overlay(workspaceRoot, projectRoot);
100
+ }
101
+
102
+ /**
103
+ * Creates a DDK plugin or theme project. A dry run lists the generic generator's
104
+ * changes and the DDK files that would be written over them.
105
+ *
106
+ * @param {"plugin" | "theme"} kind
107
+ * @param {{workspaceRoot: string, name: string, dryRun?: boolean, stdio?: "pipe" | "inherit"}} options
108
+ */
109
+ export function createDdkProject(
110
+ kind,
111
+ { workspaceRoot, name, dryRun = false, stdio = "pipe" }
112
+ ) {
113
+ const spec = PROJECT_KINDS[kind];
114
+ const result = spawnSync(
115
+ "npx",
116
+ [
117
+ "nx",
118
+ "generate",
119
+ spec.generator,
120
+ `--name=${name}`,
121
+ `--uiType=${spec.uiType}`,
122
+ "--no-interactive",
123
+ ...(dryRun ? ["--dry-run"] : []),
124
+ ],
125
+ {
126
+ cwd: workspaceRoot,
127
+ encoding: "utf-8",
128
+ stdio,
129
+ shell: process.platform === "win32",
130
+ }
131
+ );
132
+ if (result.status !== 0) {
133
+ throw new Error(
134
+ `nx generate exited with ${result.status}\n${result.stderr ?? ""}`
135
+ );
136
+ }
137
+ const output = result.stdout?.trim() ?? "";
138
+ if (dryRun) return { output, ddkFiles: templateFiles(kind) };
139
+
140
+ const project = findProjectByName(workspaceRoot, name);
141
+ if (!project) throw new Error(`nx generate did not create project ${name}`);
142
+ applyDdkLayer(kind, workspaceRoot, path.join(workspaceRoot, project.root));
143
+ return { output, root: project.root };
144
+ }
@@ -0,0 +1,22 @@
1
+ // kosui extension entry: adds the ddk:* commands when this package is installed in a
2
+ // KOS workspace (or globally beside kosui). kosui passes its CLI helpers as `host`.
3
+ import * as agentInstall from "./generators/agent-install.mjs";
4
+ import * as catalog from "./generators/catalog.mjs";
5
+ import * as contributions from "./generators/contributions.mjs";
6
+ import * as dashboard from "./generators/dashboard.mjs";
7
+ import * as dev from "./generators/dev.mjs";
8
+ import * as pluginProject from "./generators/plugin-project.mjs";
9
+ import * as themeProject from "./generators/theme-project.mjs";
10
+
11
+ export default {
12
+ id: "ddk",
13
+ generators: [
14
+ { metadata: contributions.metadata, register: contributions.register },
15
+ { metadata: dashboard.metadata, register: dashboard.register },
16
+ { metadata: catalog.metadata, register: catalog.register },
17
+ { metadata: pluginProject.metadata, register: pluginProject.register },
18
+ { metadata: themeProject.metadata, register: themeProject.register },
19
+ { metadata: dev.metadata, register: dev.register },
20
+ { metadata: agentInstall.metadata, register: agentInstall.register },
21
+ ],
22
+ };
@@ -0,0 +1,79 @@
1
+ // ddk:agent-install: registers this package's kos-ddk-tools agent plugin with an agent
2
+ // client, through kosui's `plugin install --from`.
3
+ import { spawnSync } from "node:child_process";
4
+ import { existsSync } from "node:fs";
5
+ import path from "node:path";
6
+ import { fileURLToPath } from "node:url";
7
+
8
+ export const AGENT_PLUGIN_DIR = path.resolve(
9
+ path.dirname(fileURLToPath(import.meta.url)),
10
+ "../../agent-plugin"
11
+ );
12
+
13
+ export const metadata = {
14
+ key: "ddk:agent-install",
15
+ name: "DDK Agent Plugin Install",
16
+ invalidateCache: false,
17
+ namedArguments: {
18
+ client: "client",
19
+ scope: "scope",
20
+ interactive: "interactive",
21
+ },
22
+ };
23
+
24
+ /** The `plugin install` arguments for the bundle, the client and the scope. */
25
+ export function installArgs({ client, scope }, dir = AGENT_PLUGIN_DIR) {
26
+ return [
27
+ "plugin",
28
+ "install",
29
+ "--from",
30
+ dir,
31
+ ...(client ? ["--client", client] : []),
32
+ ...(scope ? ["--scope", scope] : []),
33
+ ];
34
+ }
35
+
36
+ export async function register(plop, { host }) {
37
+ plop.setActionType("ddk:agentInstall", (answers) => {
38
+ if (!existsSync(AGENT_PLUGIN_DIR)) {
39
+ throw new Error(
40
+ `Agent plugin bundle missing at ${AGENT_PLUGIN_DIR}; it is written by the kos-ddk-cli build.`
41
+ );
42
+ }
43
+ // process.argv[1] is the kosui entry running this extension.
44
+ const result = spawnSync(
45
+ process.execPath,
46
+ [process.argv[1], ...installArgs(answers)],
47
+ { stdio: "inherit" }
48
+ );
49
+ if (result.status !== 0) {
50
+ throw new Error(`kosui plugin install exited with ${result.status}`);
51
+ }
52
+ return "kos-ddk-tools agent plugin installed";
53
+ });
54
+
55
+ plop.setGenerator("ddk:agent-install", {
56
+ description:
57
+ "Install the kos-ddk-tools agent plugin (kos-ddk MCP server) for an agent client",
58
+ prompts: [
59
+ ...host.DEFAULT_PROMPTS,
60
+ {
61
+ type: "input",
62
+ name: "client",
63
+ message: "Agent client, e.g. claude (blank: detect)?",
64
+ default: "",
65
+ },
66
+ {
67
+ type: "list",
68
+ name: "scope",
69
+ message: "Install scope?",
70
+ choices: [
71
+ { name: "user: every repo", value: "user" },
72
+ { name: "repo: this checkout only", value: "repo" },
73
+ ],
74
+ default: "user",
75
+ },
76
+ ],
77
+ actions: host.actionFactory("ddk:agentInstall", metadata),
78
+ });
79
+ }