@astryxdesign/cli 0.6.5-vanilla-poc.6e25cfc → 0.6.6

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 (197) hide show
  1. package/CHANGELOG.md +84 -0
  2. package/README.md +1 -34
  3. package/api/build/_adapter.d.mts +14 -0
  4. package/api/build/_adapter.mjs +58 -1
  5. package/api/build/build.test.mjs +74 -1
  6. package/api/build/kit/kit.mjs +39 -7
  7. package/api/build/kit/rank.d.mts +8 -0
  8. package/api/build/kit/rank.mjs +14 -0
  9. package/api/build/kit/weights.d.mts +82 -0
  10. package/api/build/kit/weights.json +1 -0
  11. package/api/build/kit/weights.mjs +305 -0
  12. package/api/build/kit/weights.test.mjs +190 -0
  13. package/api/component/_adapter.d.mts +1 -51
  14. package/api/component/_adapter.mjs +3 -110
  15. package/api/component/component.d.mts +1 -9
  16. package/api/component/component.doc.mjs +7 -52
  17. package/api/component/component.mjs +3 -276
  18. package/api/component/component.type.d.mts +1 -28
  19. package/api/component/component.type.mjs +1 -20
  20. package/api/docs/docs.d.mts +6 -0
  21. package/api/docs/docs.depth.test.mjs +132 -0
  22. package/api/docs/docs.doc.mjs +31 -2
  23. package/api/docs/docs.mjs +44 -1
  24. package/api/docs/docs.type.d.mts +38 -1
  25. package/api/docs/docs.type.mjs +18 -1
  26. package/api/docs/node/node.d.mts +28 -3
  27. package/api/docs/node/node.mjs +146 -23
  28. package/api/hook/list/list.d.mts +1 -1
  29. package/api/index.d.mts +0 -8
  30. package/api/index.mjs +0 -8
  31. package/api/integration/add-theme.mjs +93 -3
  32. package/api/integration/add-theme.test.mjs +100 -0
  33. package/api/integration/integrationAddTheme.doc.mjs +1 -1
  34. package/api/search/search.d.mts +8 -1
  35. package/api/search/search.doc.mjs +3 -3
  36. package/api/search/search.mjs +58 -73
  37. package/api/search/search.test.mjs +122 -21
  38. package/api/search/search.type.d.mts +0 -10
  39. package/api/search/search.type.mjs +0 -1
  40. package/api/template/cdn/cdn.test.mjs +2 -50
  41. package/api/template/template.d.mts +0 -6
  42. package/api/template/template.doc.mjs +4 -56
  43. package/api/template/template.mjs +0 -82
  44. package/api/template/template.type.d.mts +0 -12
  45. package/api/template/template.type.mjs +0 -7
  46. package/assets/cdn.template.html +5 -19
  47. package/assets/docs/tokens.doc.mjs +3 -3
  48. package/assets/docs/tree/add-a-theme.doc.mjs +4 -0
  49. package/assets/docs/tree/troubleshooting.doc.mjs +4 -4
  50. package/clients/cli/commands/component/index.mjs +9 -66
  51. package/clients/cli/commands/component.doc.mjs +4 -26
  52. package/clients/cli/commands/docs.depth.test.mjs +219 -0
  53. package/clients/cli/commands/docs.doc.mjs +15 -2
  54. package/clients/cli/commands/docs.mjs +222 -5
  55. package/clients/cli/commands/integration-add.doc.mjs +1 -1
  56. package/clients/cli/commands/search.doc.mjs +2 -3
  57. package/clients/cli/commands/search.mjs +11 -8
  58. package/clients/cli/commands/search.test.mjs +35 -40
  59. package/clients/cli/commands/template.doc.mjs +3 -31
  60. package/clients/cli/commands/template.mjs +2 -37
  61. package/foundation/discovery/theme-discovery.d.mts +6 -0
  62. package/foundation/discovery/theme-discovery.mjs +9 -0
  63. package/foundation/response/response-types.doc.mjs +0 -38
  64. package/package.json +5 -9
  65. package/api/_vanilla-assets.d.mts +0 -10
  66. package/api/_vanilla-assets.mjs +0 -59
  67. package/api/component/html/html.d.mts +0 -12
  68. package/api/component/html/html.doc.d.mts +0 -5
  69. package/api/component/html/html.doc.mjs +0 -66
  70. package/api/component/html/html.mjs +0 -25
  71. package/api/component/html/html.test.mjs +0 -61
  72. package/api/component/html/html.type.d.mts +0 -23
  73. package/api/component/html/html.type.mjs +0 -27
  74. package/api/component/web-components/web-components.d.mts +0 -12
  75. package/api/component/web-components/web-components.doc.d.mts +0 -5
  76. package/api/component/web-components/web-components.doc.mjs +0 -70
  77. package/api/component/web-components/web-components.mjs +0 -32
  78. package/api/component/web-components/web-components.test.mjs +0 -50
  79. package/api/component/web-components/web-components.type.d.mts +0 -23
  80. package/api/component/web-components/web-components.type.mjs +0 -27
  81. package/api/template/_adapter.d.mts +0 -38
  82. package/api/template/_adapter.mjs +0 -90
  83. package/api/template/html/html.d.mts +0 -17
  84. package/api/template/html/html.doc.d.mts +0 -5
  85. package/api/template/html/html.doc.mjs +0 -72
  86. package/api/template/html/html.mjs +0 -61
  87. package/api/template/html/html.test.mjs +0 -71
  88. package/api/template/html/html.type.d.mts +0 -26
  89. package/api/template/html/html.type.mjs +0 -28
  90. package/api/template/web-components/web-components.d.mts +0 -13
  91. package/api/template/web-components/web-components.doc.d.mts +0 -5
  92. package/api/template/web-components/web-components.doc.mjs +0 -69
  93. package/api/template/web-components/web-components.mjs +0 -51
  94. package/api/template/web-components/web-components.test.mjs +0 -73
  95. package/api/template/web-components/web-components.type.d.mts +0 -26
  96. package/api/template/web-components/web-components.type.mjs +0 -28
  97. package/assets/generated/core-component-docs.json +0 -1
  98. package/assets/vanilla/elements-markup/Avatar.html +0 -27
  99. package/assets/vanilla/elements-markup/Badge.html +0 -16
  100. package/assets/vanilla/elements-markup/Banner.html +0 -32
  101. package/assets/vanilla/elements-markup/Breadcrumbs.html +0 -23
  102. package/assets/vanilla/elements-markup/Button.html +0 -37
  103. package/assets/vanilla/elements-markup/Card.html +0 -24
  104. package/assets/vanilla/elements-markup/CheckboxInput.html +0 -28
  105. package/assets/vanilla/elements-markup/Collapsible.html +0 -26
  106. package/assets/vanilla/elements-markup/DateInput.html +0 -28
  107. package/assets/vanilla/elements-markup/Dialog.html +0 -33
  108. package/assets/vanilla/elements-markup/Divider.html +0 -20
  109. package/assets/vanilla/elements-markup/DropdownMenu.html +0 -26
  110. package/assets/vanilla/elements-markup/EmptyState.html +0 -27
  111. package/assets/vanilla/elements-markup/Heading.html +0 -18
  112. package/assets/vanilla/elements-markup/HoverCard.html +0 -39
  113. package/assets/vanilla/elements-markup/Layout.html +0 -28
  114. package/assets/vanilla/elements-markup/Lightbox.html +0 -33
  115. package/assets/vanilla/elements-markup/Link.html +0 -23
  116. package/assets/vanilla/elements-markup/NumberInput.html +0 -34
  117. package/assets/vanilla/elements-markup/ProgressBar.html +0 -47
  118. package/assets/vanilla/elements-markup/RadioList.html +0 -28
  119. package/assets/vanilla/elements-markup/Selector.html +0 -34
  120. package/assets/vanilla/elements-markup/SideNav.html +0 -32
  121. package/assets/vanilla/elements-markup/Skeleton.html +0 -18
  122. package/assets/vanilla/elements-markup/Slider.html +0 -31
  123. package/assets/vanilla/elements-markup/Stack.html +0 -33
  124. package/assets/vanilla/elements-markup/StatusDot.html +0 -19
  125. package/assets/vanilla/elements-markup/Switch.html +0 -28
  126. package/assets/vanilla/elements-markup/Tab.html +0 -18
  127. package/assets/vanilla/elements-markup/TabList.html +0 -31
  128. package/assets/vanilla/elements-markup/Table.html +0 -31
  129. package/assets/vanilla/elements-markup/Text.html +0 -24
  130. package/assets/vanilla/elements-markup/TextArea.html +0 -32
  131. package/assets/vanilla/elements-markup/TextInput.html +0 -92
  132. package/assets/vanilla/elements-markup/Theme.html +0 -16
  133. package/assets/vanilla/elements-markup/TimeInput.html +0 -29
  134. package/assets/vanilla/elements-markup/Toast.html +0 -22
  135. package/assets/vanilla/elements-markup/Tooltip.html +0 -30
  136. package/assets/vanilla/elements-markup/TopNav.html +0 -85
  137. package/assets/vanilla/elements-markup/Typeahead.html +0 -31
  138. package/assets/vanilla/elements-templates/dashboard.html +0 -167
  139. package/assets/vanilla/elements-templates/detail-page.html +0 -145
  140. package/assets/vanilla/elements-templates/form-two-column.html +0 -135
  141. package/assets/vanilla/elements-templates/kanban-board.html +0 -141
  142. package/assets/vanilla/elements-templates/login.html +0 -100
  143. package/assets/vanilla/elements-templates/product-detail.html +0 -128
  144. package/assets/vanilla/elements-templates/settings.html +0 -130
  145. package/assets/vanilla/elements-templates/shell-side-nav.html +0 -160
  146. package/assets/vanilla/elements-templates/table-filter.html +0 -154
  147. package/assets/vanilla/markup/Avatar.html +0 -24
  148. package/assets/vanilla/markup/Badge.html +0 -12
  149. package/assets/vanilla/markup/Banner.html +0 -49
  150. package/assets/vanilla/markup/Breadcrumbs.html +0 -38
  151. package/assets/vanilla/markup/Button.html +0 -37
  152. package/assets/vanilla/markup/Card.html +0 -16
  153. package/assets/vanilla/markup/CheckboxInput.html +0 -45
  154. package/assets/vanilla/markup/Collapsible.html +0 -9
  155. package/assets/vanilla/markup/DateInput.html +0 -42
  156. package/assets/vanilla/markup/Dialog.html +0 -45
  157. package/assets/vanilla/markup/Divider.html +0 -9
  158. package/assets/vanilla/markup/DropdownMenu.html +0 -30
  159. package/assets/vanilla/markup/EmptyState.html +0 -41
  160. package/assets/vanilla/markup/Heading.html +0 -9
  161. package/assets/vanilla/markup/HoverCard.html +0 -85
  162. package/assets/vanilla/markup/Layout.html +0 -35
  163. package/assets/vanilla/markup/Lightbox.html +0 -107
  164. package/assets/vanilla/markup/Link.html +0 -6
  165. package/assets/vanilla/markup/NumberInput.html +0 -55
  166. package/assets/vanilla/markup/ProgressBar.html +0 -101
  167. package/assets/vanilla/markup/RadioList.html +0 -90
  168. package/assets/vanilla/markup/Selector.html +0 -92
  169. package/assets/vanilla/markup/SideNav.html +0 -82
  170. package/assets/vanilla/markup/Skeleton.html +0 -26
  171. package/assets/vanilla/markup/Slider.html +0 -65
  172. package/assets/vanilla/markup/Stack.html +0 -13
  173. package/assets/vanilla/markup/StatusDot.html +0 -22
  174. package/assets/vanilla/markup/Switch.html +0 -47
  175. package/assets/vanilla/markup/TabList.html +0 -56
  176. package/assets/vanilla/markup/Table.html +0 -75
  177. package/assets/vanilla/markup/Text.html +0 -9
  178. package/assets/vanilla/markup/TextArea.html +0 -37
  179. package/assets/vanilla/markup/TextInput.html +0 -80
  180. package/assets/vanilla/markup/Theme.html +0 -48
  181. package/assets/vanilla/markup/TimeInput.html +0 -43
  182. package/assets/vanilla/markup/Toast.html +0 -42
  183. package/assets/vanilla/markup/Tooltip.html +0 -18
  184. package/assets/vanilla/markup/TopNav.html +0 -81
  185. package/assets/vanilla/markup/Typeahead.html +0 -60
  186. package/assets/vanilla/templates/dashboard.html +0 -462
  187. package/assets/vanilla/templates/detail-page.html +0 -536
  188. package/assets/vanilla/templates/form-two-column.html +0 -438
  189. package/assets/vanilla/templates/kanban-board.html +0 -443
  190. package/assets/vanilla/templates/login.html +0 -199
  191. package/assets/vanilla/templates/product-detail.html +0 -335
  192. package/assets/vanilla/templates/settings.html +0 -374
  193. package/assets/vanilla/templates/shell-side-nav.html +0 -440
  194. package/assets/vanilla/templates/table-filter.html +0 -514
  195. package/clients/cli/commands/vanilla-html.test.mjs +0 -245
  196. package/foundation/discovery/bundled-component-docs.d.mts +0 -39
  197. package/foundation/discovery/bundled-component-docs.mjs +0 -92
@@ -0,0 +1,305 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file Word weights for `build`'s START (spec:AST-048): a checked-in table of
5
+ * per-template word weights, blended with the page ranker's own scores, picks
6
+ * the template an idea starts from.
7
+ *
8
+ * @input An idea, the ranker's output for it (`rankPages`, `pickStart`), the
9
+ * project's ready page templates, and the weights (`weights.json` beside this
10
+ * file, read by the adapter): the candidates (the app shell as `SHELL`, then page template ids), one or more
11
+ * word tables (stemmed words, a bias per candidate, and one row of weights
12
+ * per word, a character per candidate) and the blend numbers.
13
+ * @output `weighStart(idea, ranked, pick, catalog)`: the template id to start
14
+ * from, null for the app shell, or undefined when no table is available, in
15
+ * which case the ranker's own pick stands.
16
+ * @position Beside rank.mjs (api/build/kit/); kit.mjs calls it for every
17
+ * whole page (a part or an edit follows the ranker's placement rules). The
18
+ * ranker's pick of a template the tables do not list stands, so a new
19
+ * template can start a build.
20
+ */
21
+
22
+ /** The app shell, as one candidate. */
23
+ export const SHELL = 'SHELL';
24
+ const SHELL_FAMILY = 'Shell';
25
+
26
+ // The Porter (1980) stemmer.
27
+ /** @type {Record<string, string>} */
28
+ const STEP2 = {
29
+ ational: 'ate',
30
+ tional: 'tion',
31
+ enci: 'ence',
32
+ anci: 'ance',
33
+ izer: 'ize',
34
+ bli: 'ble',
35
+ alli: 'al',
36
+ entli: 'ent',
37
+ eli: 'e',
38
+ ousli: 'ous',
39
+ ization: 'ize',
40
+ ation: 'ate',
41
+ ator: 'ate',
42
+ alism: 'al',
43
+ iveness: 'ive',
44
+ fulness: 'ful',
45
+ ousness: 'ous',
46
+ aliti: 'al',
47
+ iviti: 'ive',
48
+ biliti: 'ble',
49
+ logi: 'log',
50
+ };
51
+ /** @type {Record<string, string>} */
52
+ const STEP3 = {
53
+ icate: 'ic',
54
+ ative: '',
55
+ alize: 'al',
56
+ iciti: 'ic',
57
+ ical: 'ic',
58
+ ful: '',
59
+ ness: '',
60
+ };
61
+ const CONS = '[^aeiou][^aeiouy]*';
62
+ const VOW = '[aeiouy][aeiou]*';
63
+ const MGR0 = new RegExp(`^(${CONS})?${VOW}${CONS}`);
64
+ const MEQ1 = new RegExp(`^(${CONS})?${VOW}${CONS}(${VOW})?$`);
65
+ const MGR1 = new RegExp(`^(${CONS})?${VOW}${CONS}${VOW}${CONS}`);
66
+ const HAS_VOWEL = new RegExp(`^(${CONS})?[aeiouy]`);
67
+ const CVC = new RegExp(`^${CONS}[aeiouy][^aeiouwxy]$`);
68
+ const stems = new Map();
69
+
70
+ /**
71
+ * The Porter stem of a lowercase word.
72
+ * @param {string} word
73
+ * @returns {string}
74
+ */
75
+ export function stem(word) {
76
+ const cached = stems.get(word);
77
+ if (cached !== undefined) return cached;
78
+ let w = word;
79
+ if (w.length < 3 || /\d/.test(w)) {
80
+ stems.set(word, w);
81
+ return w;
82
+ }
83
+ const leadingY = w[0] === 'y';
84
+ if (leadingY) w = 'Y' + w.slice(1);
85
+ let m;
86
+ if ((m = /^(.+?)(ss|i)es$/.exec(w))) w = m[1] + m[2];
87
+ else if ((m = /^(.+?)([^s])s$/.exec(w))) w = m[1] + m[2];
88
+ if ((m = /^(.+?)eed$/.exec(w))) {
89
+ if (MGR0.test(m[1])) w = w.slice(0, -1);
90
+ } else if ((m = /^(.+?)(ed|ing)$/.exec(w)) && HAS_VOWEL.test(m[1])) {
91
+ w = m[1];
92
+ if (/(at|bl|iz)$/.test(w)) w += 'e';
93
+ else if (/([^aeiouylsz])\1$/.test(w)) w = w.slice(0, -1);
94
+ else if (CVC.test(w)) w += 'e';
95
+ }
96
+ if ((m = /^(.+?)y$/.exec(w)) && HAS_VOWEL.test(m[1])) w = m[1] + 'i';
97
+ if (
98
+ (m =
99
+ /^(.+?)(ational|tional|enci|anci|izer|bli|alli|entli|eli|ousli|ization|ation|ator|alism|iveness|fulness|ousness|aliti|iviti|biliti|logi)$/.exec(
100
+ w,
101
+ )) &&
102
+ MGR0.test(m[1])
103
+ ) {
104
+ w = m[1] + STEP2[m[2]];
105
+ }
106
+ if (
107
+ (m = /^(.+?)(icate|ative|alize|iciti|ical|ful|ness)$/.exec(w)) &&
108
+ MGR0.test(m[1])
109
+ ) {
110
+ w = m[1] + STEP3[m[2]];
111
+ }
112
+ if (
113
+ (m =
114
+ /^(.+?)(al|ance|ence|er|ic|able|ible|ant|ement|ment|ent|ou|ism|ate|iti|ous|ive|ize)$/.exec(
115
+ w,
116
+ ))
117
+ ) {
118
+ if (MGR1.test(m[1])) w = m[1];
119
+ } else if ((m = /^(.+?)(s|t)(ion)$/.exec(w)) && MGR1.test(m[1] + m[2])) {
120
+ w = m[1] + m[2];
121
+ }
122
+ if (
123
+ (m = /^(.+?)e$/.exec(w)) &&
124
+ (MGR1.test(m[1]) || (MEQ1.test(m[1]) && !CVC.test(m[1])))
125
+ ) {
126
+ w = m[1];
127
+ }
128
+ if (/ll$/.test(w) && MGR1.test(w)) w = w.slice(0, -1);
129
+ if (leadingY) w = 'y' + w.slice(1);
130
+ stems.set(word, w);
131
+ return w;
132
+ }
133
+
134
+ /**
135
+ * Lowercase words of an idea that can carry weight: letters only, two or more.
136
+ * @param {string} idea
137
+ * @returns {string[]}
138
+ */
139
+ export function weightWords(idea) {
140
+ return String(idea ?? '')
141
+ .toLowerCase()
142
+ .split(/[^a-z0-9]+/)
143
+ .filter(w => w.length >= 2 && !/\d/.test(w));
144
+ }
145
+
146
+ /**
147
+ * @typedef {object} WeightTable
148
+ * @property {string[]} words stemmed words
149
+ * @property {number[]} bias one per candidate
150
+ * @property {number} clip
151
+ * @property {number} step
152
+ * @property {string[]} rows one per word: a character per candidate,
153
+ * weight = (charCode - 97) * step - clip
154
+ *
155
+ * @typedef {object} WeightsFile
156
+ * @property {number} version
157
+ * @property {string[]} candidates `SHELL`, then page template ids
158
+ * @property {WeightTable[]} tables
159
+ * @property {number[]} blend three numbers per member (the tables, in order,
160
+ * with the ranker second), then one for `SHELL`
161
+ */
162
+
163
+ /**
164
+ * A table's score for every candidate.
165
+ * @param {WeightTable} table
166
+ * @param {Map<string, number>} index word -> row
167
+ * @param {Set<string>} words the idea's stemmed words
168
+ * @param {number} n candidates
169
+ */
170
+ function tableScores(table, index, words, n) {
171
+ const out = Float64Array.from(table.bias.slice(0, n));
172
+ for (const w of words) {
173
+ const i = index.get(w);
174
+ if (i === undefined) continue;
175
+ const row = table.rows[i];
176
+ for (let c = 0; c < n; c++) {
177
+ out[c] += (row.charCodeAt(c) - 97) * table.step - table.clip;
178
+ }
179
+ }
180
+ return out;
181
+ }
182
+
183
+ /** @param {number} x */
184
+ const round4 = x => Math.round(x * 1e4) / 1e4;
185
+
186
+ /**
187
+ * The ranker's scores as a blend member: every non-shell template's rank
188
+ * score, the ranker's own start lifted above the rest, and the app shell above
189
+ * them all when the ranker would start from it.
190
+ * @param {import('./rank.mjs').RankedPage[]} ranked
191
+ * @param {import('./rank.mjs').RankedPage | null} pick
192
+ */
193
+ function rankerScores(ranked, pick) {
194
+ /** @type {Map<string, number>} */
195
+ const out = new Map();
196
+ for (const r of ranked)
197
+ if (r.family !== SHELL_FAMILY) out.set(r.name, r.score);
198
+ const top = Math.max(0, ...out.values());
199
+ const pageStart = pick && pick.family !== SHELL_FAMILY ? pick.name : null;
200
+ out.set(SHELL, pageStart ? -1 : top + 1);
201
+ if (pageStart) out.set(pageStart, top + 2);
202
+ return out;
203
+ }
204
+
205
+ /**
206
+ * Per-candidate features of one member's scores: z-score, 1/(rank+1), is-top.
207
+ * @param {(number | undefined)[]} v a score per candidate (undefined: none)
208
+ */
209
+ function memberFeatures(v) {
210
+ const finite = /** @type {number[]} */ (v.filter(x => x !== undefined));
211
+ const mean = finite.reduce((a, b) => a + b, 0) / Math.max(1, finite.length);
212
+ const sd =
213
+ Math.sqrt(
214
+ finite.reduce((a, b) => a + (b - mean) ** 2, 0) /
215
+ Math.max(1, finite.length),
216
+ ) || 1;
217
+ const val = v.map(x => (x === undefined ? -1e9 : x));
218
+ const order = [...val.keys()].sort((a, b) => val[b] - val[a]);
219
+ const rank = new Array(v.length);
220
+ order.forEach((ci, r) => (rank[ci] = r));
221
+ return v.map((x, i) => [
222
+ x === undefined ? -4 : Math.max(-4, Math.min(4, (x - mean) / sd)),
223
+ 1 / (rank[i] + 1),
224
+ rank[i] === 0 ? 1 : 0,
225
+ ]);
226
+ }
227
+
228
+ /**
229
+ * Where an idea starts, by the blended weights.
230
+ * @param {string} idea
231
+ * @param {import('./rank.mjs').RankedPage[]} ranked the ranker's output for the idea
232
+ * @param {import('./rank.mjs').RankedPage | null} pick the ranker's own start
233
+ * @param {{name: string}[]} catalog the project's ready page templates
234
+ * @param {{weights?: WeightsFile | null, newPage?: boolean}} [options]
235
+ * `newPage`: the idea asks for a new page (rank.mjs `asksForNewPage`)
236
+ * @returns {string | null | undefined} a template id, null for the app shell,
237
+ * undefined without weights
238
+ */
239
+ export function weighStart(
240
+ idea,
241
+ ranked,
242
+ pick,
243
+ catalog,
244
+ {weights = null, newPage = false} = {},
245
+ ) {
246
+ if (!weights) return undefined;
247
+ const ready = new Set(catalog.map(t => t.name));
248
+ const shellIds = new Set(
249
+ ranked.filter(r => r.family === SHELL_FAMILY).map(r => r.name),
250
+ );
251
+ // Candidates: the app shell and every ready, non-shell template, the
252
+ // table's first, then any the table does not list.
253
+ const listed = weights.candidates.filter(c => c === SHELL || ready.has(c));
254
+ const extra = catalog
255
+ .map(t => t.name)
256
+ .filter(n => !shellIds.has(n) && !weights.candidates.includes(n));
257
+ const cands = [...listed, ...extra];
258
+ const position = new Map(weights.candidates.map((c, i) => [c, i]));
259
+ const words = new Set(weightWords(idea).map(stem));
260
+ /** @type {(number | undefined)[][]} */
261
+ const members = weights.tables.map(table => {
262
+ const index = new Map(table.words.map((w, i) => [w, i]));
263
+ const s = tableScores(table, index, words, weights.candidates.length);
264
+ return cands.map(c => {
265
+ const i = position.get(c);
266
+ return i === undefined ? undefined : round4(s[i]);
267
+ });
268
+ });
269
+ const fromRanker = rankerScores(ranked, pick);
270
+ const rankerMember = cands.map(c => {
271
+ const x = fromRanker.get(c);
272
+ return x === undefined ? undefined : round4(x);
273
+ });
274
+ // The ranker is the blend's second member, after the first table.
275
+ members.splice(1, 0, rankerMember);
276
+ const feats = members.map(memberFeatures);
277
+ const scores = cands.map((c, i) => {
278
+ let s = 0;
279
+ let j = 0;
280
+ for (const f of feats) for (const x of f[i]) s += x * weights.blend[j++];
281
+ return s + (c === SHELL ? 1 : 0) * weights.blend[j];
282
+ });
283
+ /** @param {(c: string) => boolean} ok */
284
+ const best = ok => {
285
+ let at = -1;
286
+ for (let c = 0; c < cands.length; c++) {
287
+ if (ok(cands[c]) && (at < 0 || scores[c] > scores[at])) at = c;
288
+ }
289
+ return at < 0 ? null : cands[at];
290
+ };
291
+ const kept =
292
+ pick && pick.family !== SHELL_FAMILY && cands.includes(pick.name)
293
+ ? pick.name
294
+ : null;
295
+ // The tables cannot judge a template they do not list, and a template the
296
+ // ranker placed by its frame stays (spec:AST-048/FR3): the ranker's pick
297
+ // stands for both.
298
+ if (kept && (!position.has(kept) || pick?.containerMatched)) return kept;
299
+ const top = best(() => true);
300
+ if (top !== SHELL) return top;
301
+ // An idea that asks for a new page keeps the ranker's template rather than
302
+ // the shell.
303
+ if (newPage && kept) return kept;
304
+ return null;
305
+ }
@@ -0,0 +1,190 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ import {describe, expect, it} from 'vitest';
4
+
5
+ import {isWeightsFile} from '../_adapter.mjs';
6
+ import {SHELL, stem, weighStart, weightWords} from './weights.mjs';
7
+
8
+ // A made-up table for tests only: three page templates and the app shell.
9
+ const CANDIDATES = [SHELL, 'board', 'charts', 'grid'];
10
+
11
+ /**
12
+ * @param {Record<string, number[]>} weights per word, one value per candidate
13
+ * @param {number[]} [bias]
14
+ * @param {number} [step]
15
+ * @param {number} [clip]
16
+ */
17
+ function table(weights, bias = [0, 0, 0, 0], step = 0.5, clip = 4) {
18
+ return {
19
+ words: Object.keys(weights),
20
+ bias,
21
+ clip,
22
+ step,
23
+ rows: Object.values(weights).map(ws =>
24
+ ws
25
+ .map(w => String.fromCharCode(97 + Math.round((w + clip) / step)))
26
+ .join(''),
27
+ ),
28
+ };
29
+ }
30
+
31
+ const WEIGHTS = {
32
+ version: 1,
33
+ candidates: CANDIDATES,
34
+ tables: [
35
+ table({
36
+ [stem('kanban')]: [0, 3, 0, 0],
37
+ [stem('existing')]: [3, 0, 0, 0],
38
+ [stem('charts')]: [0, 0, 2, 0],
39
+ }),
40
+ table({[stem('cards')]: [0, 0, 0, 1]}, [0, 0, 0, 0], 1, 4),
41
+ ],
42
+ // three numbers per member (table 1, the ranker, table 2), then one for SHELL
43
+ blend: [1, 0, 0, 0.5, 0, 0, 0.2, 0, 0, 0],
44
+ };
45
+ const CATALOG = [
46
+ {name: 'board'},
47
+ {name: 'charts'},
48
+ {name: 'grid'},
49
+ {name: 'top-nav'},
50
+ ];
51
+
52
+ /**
53
+ * @param {string} name
54
+ * @param {number} score
55
+ * @param {string} family
56
+ */
57
+ const page = (name, score, family) => ({
58
+ name,
59
+ score,
60
+ hits: 1,
61
+ family,
62
+ base: true,
63
+ familyNamed: false,
64
+ containerMatched: false,
65
+ matched: new Set(),
66
+ });
67
+ /** A ranker output that prefers `charts`. */
68
+ const RANKED = [
69
+ page('charts', 3, 'Charts'),
70
+ page('grid', 1, 'Grid'),
71
+ page('board', 0.5, 'Board'),
72
+ page('top-nav', 0, 'Shell'),
73
+ ];
74
+
75
+ describe('weighStart', () => {
76
+ it('lets the word weights outvote the ranker', () => {
77
+ const start = weighStart(
78
+ 'a kanban for launches',
79
+ RANKED,
80
+ RANKED[0],
81
+ CATALOG,
82
+ {
83
+ weights: WEIGHTS,
84
+ },
85
+ );
86
+ expect(start).toBe('board');
87
+ });
88
+
89
+ it('keeps the ranker when the words carry no weight', () => {
90
+ const start = weighStart('something else', RANKED, RANKED[0], CATALOG, {
91
+ weights: WEIGHTS,
92
+ });
93
+ expect(start).toBe('charts');
94
+ });
95
+
96
+ it('returns null for the app shell', () => {
97
+ const start = weighStart('tweak the existing page', RANKED, null, CATALOG, {
98
+ weights: WEIGHTS,
99
+ });
100
+ expect(start).toBeNull();
101
+ });
102
+
103
+ it('returns undefined without weights, so the ranker decides', () => {
104
+ const start = weighStart('a kanban', RANKED, RANKED[0], CATALOG, {
105
+ weights: null,
106
+ });
107
+ expect(start).toBeUndefined();
108
+ });
109
+
110
+ it('never starts from a template the project does not have', () => {
111
+ const catalog = CATALOG.filter(t => t.name !== 'board');
112
+ const ranked = RANKED.filter(r => r.name !== 'board');
113
+ const start = weighStart('a kanban', ranked, ranked[0], catalog, {
114
+ weights: WEIGHTS,
115
+ });
116
+ expect(start).not.toBe('board');
117
+ });
118
+
119
+ it("keeps the ranker's pick of a template the tables do not list", () => {
120
+ const catalog = [...CATALOG, {name: 'fresh'}];
121
+ const ranked = [page('fresh', 9, 'Fresh'), ...RANKED];
122
+ const start = weighStart('a kanban', ranked, ranked[0], catalog, {
123
+ weights: WEIGHTS,
124
+ });
125
+ expect(start).toBe('fresh');
126
+ });
127
+
128
+ it("keeps the ranker's template for an idea that asks for a new page", () => {
129
+ const start = weighStart('the existing page', RANKED, RANKED[0], CATALOG, {
130
+ weights: WEIGHTS,
131
+ newPage: true,
132
+ });
133
+ expect(start).toBe('charts');
134
+ });
135
+
136
+ it('starts a new page from the shell when the ranker has no template', () => {
137
+ const start = weighStart('the existing page', RANKED, null, CATALOG, {
138
+ weights: WEIGHTS,
139
+ newPage: true,
140
+ });
141
+ expect(start).toBeNull();
142
+ });
143
+
144
+ it('keeps a template the ranker placed by its frame', () => {
145
+ const framed = {...RANKED[1], containerMatched: true};
146
+ for (const idea of ['the existing page', 'a kanban']) {
147
+ const start = weighStart(idea, RANKED, framed, CATALOG, {
148
+ weights: WEIGHTS,
149
+ });
150
+ expect(start).toBe('grid');
151
+ }
152
+ });
153
+
154
+ it('is deterministic', () => {
155
+ const run = () =>
156
+ weighStart('kanban charts cards', RANKED, RANKED[0], CATALOG, {
157
+ weights: WEIGHTS,
158
+ });
159
+ expect(run()).toBe(run());
160
+ });
161
+ });
162
+
163
+ describe('weightWords', () => {
164
+ it('keeps letter words of two or more characters', () => {
165
+ expect(weightWords('A 3-col Dashboard, v2 KPIs!')).toEqual([
166
+ 'col',
167
+ 'dashboard',
168
+ 'kpis',
169
+ ]);
170
+ });
171
+ });
172
+
173
+ describe('isWeightsFile', () => {
174
+ it('accepts a well-formed file', () => {
175
+ expect(isWeightsFile(WEIGHTS)).toBe(true);
176
+ });
177
+
178
+ it('rejects a file whose rows, bias or blend do not fit the candidates', () => {
179
+ const [first, second] = WEIGHTS.tables;
180
+ expect(isWeightsFile({...WEIGHTS, blend: [1, 2, 3]})).toBe(false);
181
+ expect(
182
+ isWeightsFile({...WEIGHTS, tables: [{...first, rows: ['ab']}, second]}),
183
+ ).toBe(false);
184
+ expect(
185
+ isWeightsFile({...WEIGHTS, tables: [{...first, bias: [0]}, second]}),
186
+ ).toBe(false);
187
+ expect(isWeightsFile({version: 1})).toBe(false);
188
+ expect(isWeightsFile(null)).toBe(false);
189
+ });
190
+ });
@@ -54,56 +54,12 @@
54
54
  * @property {string} resolvedOwnerPackage
55
55
  * @property {string|null} resolvedSourcePath
56
56
  */
57
- /**
58
- * Locate Core without failing so documentation callers can select the bundled
59
- * fallback when a no-build project has no local installation.
60
- * @param {string} cwd
61
- * @returns {string|null}
62
- */
63
- export function findOptionalCoreDir(cwd: string): string | null;
64
57
  /**
65
58
  * Locate `@astryxdesign/core`, throwing the stable not-found error when absent.
66
59
  * @param {string} cwd
67
60
  * @returns {string}
68
61
  */
69
62
  export function requireCoreDir(cwd: string): string;
70
- /**
71
- * List the component markup files for a vanilla framework.
72
- * @param {string} cwd
73
- * @param {'html'|'web-components'} framework
74
- * @returns {{name: string, file: string}[]}
75
- */
76
- export function listVanillaComponentMarkup(cwd: string, framework: "html" | "web-components"): {
77
- name: string;
78
- file: string;
79
- }[];
80
- /** @param {string} cwd @returns {{name: string, file: string}[]} */
81
- export function listVanillaComponentHtml(cwd: string): {
82
- name: string;
83
- file: string;
84
- }[];
85
- /**
86
- * Read one exact component markup file without permitting path traversal.
87
- * @param {string} name
88
- * @param {string} cwd
89
- * @param {'html'|'web-components'} framework
90
- * @returns {{component: string, file: string, source: string}}
91
- */
92
- export function readVanillaComponentMarkup(name: string, cwd: string, framework: "html" | "web-components"): {
93
- component: string;
94
- file: string;
95
- source: string;
96
- };
97
- /**
98
- * @param {string} name
99
- * @param {string} cwd
100
- * @returns {{component: string, file: string, source: string}}
101
- */
102
- export function readVanillaComponentHtml(name: string, cwd: string): {
103
- component: string;
104
- file: string;
105
- source: string;
106
- };
107
63
  /**
108
64
  * Load the configured integrations for `cwd`, swallowing any config errors so
109
65
  * component discovery never hard-fails on a malformed/absent integration. An
@@ -233,6 +189,7 @@ export function scopeSubComponent(docs: LoadedComponentDoc, dirName: string, cor
233
189
  };
234
190
  matchingComponent: any;
235
191
  } | null;
192
+ export { CORE_PACKAGE };
236
193
  /**
237
194
  * Internal ambiguity marker. Single-component callers still receive the same
238
195
  * AstryxError code, message, and suggestions; batch callers can additionally
@@ -325,11 +282,4 @@ export type ResolvedUnscopedDoc = {
325
282
  resolvedSourcePath: string | null;
326
283
  };
327
284
  import { CORE_PACKAGE } from '../../foundation/discovery/component-discovery.mjs';
328
- import { getBundledCoreVersion } from '../../foundation/discovery/bundled-component-docs.mjs';
329
- import { getBundledComponentDoc } from '../../foundation/discovery/bundled-component-docs.mjs';
330
- import { getBundledComponentDocsMeta } from '../../foundation/discovery/bundled-component-docs.mjs';
331
- import { getBundledComponentGroups } from '../../foundation/discovery/bundled-component-docs.mjs';
332
- import { getBundledComponentRecords } from '../../foundation/discovery/bundled-component-docs.mjs';
333
- import { levenshteinDistance } from '../../foundation/text/string-utils.mjs';
334
285
  import { AstryxError } from '../error.mjs';
335
- export { CORE_PACKAGE, getBundledCoreVersion, getBundledComponentDoc, getBundledComponentDocsMeta, getBundledComponentGroups, getBundledComponentRecords, levenshteinDistance };