@astryxdesign/cli 0.7.3-canary.04c7468 → 0.7.3-canary.05bfcc5

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.
@@ -18,36 +18,16 @@ export function weightWords(idea: string): string[];
18
18
  * @param {string} idea
19
19
  * @param {import('./rank.mjs').RankedPage[]} ranked the ranker's output for the idea
20
20
  * @param {import('./rank.mjs').RankedPage | null} pick the ranker's own start
21
- * @param {{name: string}[]} catalog the project's ready page templates
21
+ * @param {DescribedTemplate[]} catalog the project's ready page templates
22
22
  * @param {{weights?: WeightsFile | null, newPage?: boolean}} [options]
23
23
  * `newPage`: the idea asks for a new page (rank.mjs `asksForNewPage`)
24
24
  * @returns {string | null | undefined} a template id, null for the app shell,
25
25
  * undefined without weights
26
26
  */
27
- export function weighStart(idea: string, ranked: import("./rank.mjs").RankedPage[], pick: import("./rank.mjs").RankedPage | null, catalog: {
28
- name: string;
29
- }[], { weights, newPage }?: {
27
+ export function weighStart(idea: string, ranked: import("./rank.mjs").RankedPage[], pick: import("./rank.mjs").RankedPage | null, catalog: DescribedTemplate[], { weights, newPage }?: {
30
28
  weights?: WeightsFile | null;
31
29
  newPage?: boolean;
32
30
  }): string | null | undefined;
33
- /**
34
- * @file Word weights for `build`'s START (spec:AST-048): a checked-in table of
35
- * per-template word weights, blended with the page ranker's own scores, picks
36
- * the template an idea starts from.
37
- *
38
- * @input An idea, the ranker's output for it (`rankPages`, `pickStart`), the
39
- * project's ready page templates, and the weights (`weights.json` beside this
40
- * file, read by the adapter): the candidates (the app shell as `SHELL`, then page template ids), one or more
41
- * word tables (stemmed words, a bias per candidate, and one row of weights
42
- * per word, a character per candidate) and the blend numbers.
43
- * @output `weighStart(idea, ranked, pick, catalog)`: the template id to start
44
- * from, null for the app shell, or undefined when no table is available, in
45
- * which case the ranker's own pick stands.
46
- * @position Beside rank.mjs (api/build/kit/); kit.mjs calls it for every
47
- * whole page (a part or an edit follows the ranker's placement rules). The
48
- * ranker's pick of a template the tables do not list stands, so a new
49
- * template can start a build.
50
- */
51
31
  /** The app shell, as one candidate. */
52
32
  export const SHELL: "SHELL";
53
33
  export type WeightTable = {
@@ -80,3 +60,9 @@ export type WeightsFile = {
80
60
  */
81
61
  blend: number[];
82
62
  };
63
+ export type DescribedTemplate = {
64
+ name: string;
65
+ displayName?: string | undefined;
66
+ category?: string | undefined;
67
+ keywords?: string[] | undefined;
68
+ };
@@ -14,11 +14,13 @@
14
14
  * from, null for the app shell, or undefined when no table is available, in
15
15
  * which case the ranker's own pick stands.
16
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.
17
+ * whole page (a part or an edit follows the ranker's placement rules). A
18
+ * template the tables do not list competes on its own descriptor's words
19
+ * (`ownWords`), so a new template can start a build.
20
20
  */
21
21
 
22
+ import {STOPWORDS} from '../../search/search.mjs';
23
+
22
24
  /** The app shell, as one candidate. */
23
25
  export const SHELL = 'SHELL';
24
26
  const SHELL_FAMILY = 'Shell';
@@ -225,12 +227,65 @@ function memberFeatures(v) {
225
227
  ]);
226
228
  }
227
229
 
230
+ /**
231
+ * @typedef {object} DescribedTemplate
232
+ * @property {string} name
233
+ * @property {string} [displayName]
234
+ * @property {string} [category]
235
+ * @property {string[]} [keywords]
236
+ */
237
+
238
+ /**
239
+ * How many of the idea's stemmed words a template's own descriptor names: its
240
+ * display name, its category, and its keywords, less search's stopwords.
241
+ * @param {string} name
242
+ * @param {DescribedTemplate[]} catalog
243
+ * @param {Set<string>} words the idea's stemmed words, less search's stopwords
244
+ */
245
+ function ownWords(name, catalog, words) {
246
+ const t = catalog.find(c => c.name === name);
247
+ if (!t) return 0;
248
+ // Search's stopwords are no evidence: "to" in a "to-do list" keyword must
249
+ // not answer the "to" of "rows to track orders".
250
+ const own = new Set(
251
+ weightWords(
252
+ [t.displayName ?? '', t.category ?? '', ...(t.keywords ?? [])].join(' '),
253
+ )
254
+ .filter(w => !STOPWORDS.has(w))
255
+ .map(stem),
256
+ );
257
+ let n = 0;
258
+ for (const w of words) if (own.has(w)) n++;
259
+ return n;
260
+ }
261
+
262
+ /** How many fewer own words a template whose family the idea names first may
263
+ * name than the listed template it competes with, and still start. */
264
+ const FAMILY_SLACK = 2;
265
+
266
+ /**
267
+ * Where the idea first names a template's family, the part of its category
268
+ * before ` - ` ("Dashboard" in "Dashboard - Analytics"): the index of that
269
+ * word among the idea's stemmed words, or Infinity when it does not.
270
+ * @param {string} name
271
+ * @param {DescribedTemplate[]} catalog
272
+ * @param {string[]} order the idea's stemmed words, in order
273
+ */
274
+ function familyAt(name, catalog, order) {
275
+ const t = catalog.find(c => c.name === name);
276
+ const family = weightWords(String(t?.category ?? '').split(' - ')[0]).map(
277
+ stem,
278
+ );
279
+ const at = family.length ? order.findIndex(w => family.includes(w)) : -1;
280
+ return at < 0 ? Infinity : at;
281
+ }
282
+
228
283
  /**
229
284
  * Where an idea starts, by the blended weights.
230
285
  * @param {string} idea
231
286
  * @param {import('./rank.mjs').RankedPage[]} ranked the ranker's output for the idea
232
287
  * @param {import('./rank.mjs').RankedPage | null} pick the ranker's own start
233
- * @param {{name: string}[]} catalog the project's ready page templates
288
+ * @param {DescribedTemplate[]} catalog the project's ready page templates
234
289
  * @param {{weights?: WeightsFile | null, newPage?: boolean}} [options]
235
290
  * `newPage`: the idea asks for a new page (rank.mjs `asksForNewPage`)
236
291
  * @returns {string | null | undefined} a template id, null for the app shell,
@@ -292,10 +347,34 @@ export function weighStart(
292
347
  pick && pick.family !== SHELL_FAMILY && cands.includes(pick.name)
293
348
  ? pick.name
294
349
  : 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;
350
+ // A template the ranker placed by its frame stays (spec:AST-048/FR3).
351
+ if (kept && pick?.containerMatched) return kept;
352
+ // The tables cannot judge a template they do not list (an integration's
353
+ // template, or one added since they were fit), so its own descriptor
354
+ // decides: the ranker's pick stands when the idea names as many words of its
355
+ // display name, category and keywords as of the tables' first listed
356
+ // template's, or names its family first and nearly as many; otherwise that
357
+ // listed template starts. Nothing learned about the template is involved,
358
+ // so a new template competes on its own words.
359
+ if (kept && !position.has(kept)) {
360
+ const listed = best(c => c !== SHELL && position.has(c));
361
+ if (!listed) return kept;
362
+ // The idea's own words, less search's stopwords: "basically" must not
363
+ // answer the "Basic" of a category.
364
+ const content = weightWords(idea).filter(w => !STOPWORDS.has(w));
365
+ const said = new Set(content.map(stem));
366
+ const mine = ownWords(kept, catalog, said);
367
+ const theirs = ownWords(listed, catalog, said);
368
+ if (mine >= theirs) return kept;
369
+ // An idea that names the template's family before the listed one's
370
+ // ("a dashboard with a data table") is that family's page, unless it
371
+ // names far more of the listed template's words.
372
+ const order = content.map(stem);
373
+ return familyAt(kept, catalog, order) < familyAt(listed, catalog, order) &&
374
+ mine >= theirs - FAMILY_SLACK
375
+ ? kept
376
+ : listed;
377
+ }
299
378
  const top = best(() => true);
300
379
  if (top !== SHELL) return top;
301
380
  // An idea that asks for a new page keeps the ranker's template rather than
@@ -125,6 +125,102 @@ describe('weighStart', () => {
125
125
  expect(start).toBe('fresh');
126
126
  });
127
127
 
128
+ describe('a template the tables do not list (cold start)', () => {
129
+ // A made-up family the tables have never seen, described only by its own
130
+ // descriptor, next to listed templates with descriptors of their own.
131
+ const catalog = [
132
+ {name: 'board', displayName: 'Kanban Board', keywords: ['kanban']},
133
+ {name: 'charts', displayName: 'Charts', category: 'Charts - Overview'},
134
+ {name: 'grid', displayName: 'Grid'},
135
+ {
136
+ name: 'gauge-fleet',
137
+ displayName: 'Fleet Gauges',
138
+ category: 'Gauges - Fleet',
139
+ keywords: ['gauges', 'fleet', 'meters'],
140
+ },
141
+ ];
142
+
143
+ it('starts from it when the idea names its own words', () => {
144
+ const ranked = [page('gauge-fleet', 9, 'Gauges'), ...RANKED];
145
+ const start = weighStart(
146
+ 'fleet gauges with meters per kanban lane',
147
+ ranked,
148
+ ranked[0],
149
+ catalog,
150
+ {weights: WEIGHTS},
151
+ );
152
+ expect(start).toBe('gauge-fleet');
153
+ });
154
+
155
+ it('starts from it when the idea names its family first', () => {
156
+ const ranked = [page('gauge-fleet', 9, 'Gauges'), ...RANKED];
157
+ const start = weighStart(
158
+ 'gauges page with a kanban board',
159
+ ranked,
160
+ ranked[0],
161
+ catalog,
162
+ {weights: WEIGHTS},
163
+ );
164
+ expect(start).toBe('gauge-fleet');
165
+ });
166
+
167
+ it("never counts a stopword from another template's keywords", () => {
168
+ // "side" in grid's "side by side" keyword is one of search's stopwords;
169
+ // it must not answer the idea's "sides", which is not.
170
+ const own = [
171
+ {name: 'board'},
172
+ {name: 'charts'},
173
+ {
174
+ name: 'grid',
175
+ displayName: 'Grid',
176
+ keywords: ['side by side', 'orders'],
177
+ },
178
+ {name: 'fold', displayName: 'Folding Rows'},
179
+ ];
180
+ const ranked = [page('fold', 9, 'Fold'), ...RANKED];
181
+ const start = weighStart(
182
+ 'rows on two sides with orders on cards',
183
+ ranked,
184
+ ranked[0],
185
+ own,
186
+ {weights: WEIGHTS},
187
+ );
188
+ expect(start).toBe('fold');
189
+ });
190
+
191
+ it("never counts a stopword from the idea's words", () => {
192
+ // "basically" is one of search's stopwords and stems to "basic", the
193
+ // word of grid's category; it must not tip the count to grid.
194
+ const own = [
195
+ {name: 'board'},
196
+ {name: 'charts'},
197
+ {name: 'grid', displayName: 'Grid', category: 'Grid - Basic'},
198
+ {name: 'fold', displayName: 'Folding Rows'},
199
+ ];
200
+ const ranked = [page('fold', 9, 'Fold'), ...RANKED];
201
+ const start = weighStart(
202
+ 'basically a grid of rows on cards',
203
+ ranked,
204
+ ranked[0],
205
+ own,
206
+ {weights: WEIGHTS},
207
+ );
208
+ expect(start).toBe('fold');
209
+ });
210
+
211
+ it('yields to the listed template whose own words the idea names more', () => {
212
+ const ranked = [page('gauge-fleet', 9, 'Gauges'), ...RANKED];
213
+ const start = weighStart(
214
+ 'a kanban board with a fleet column',
215
+ ranked,
216
+ ranked[0],
217
+ catalog,
218
+ {weights: WEIGHTS},
219
+ );
220
+ expect(start).toBe('board');
221
+ });
222
+ });
223
+
128
224
  it("keeps the ranker's template for an idea that asks for a new page", () => {
129
225
  const start = weighStart('the existing page', RANKED, RANKED[0], CATALOG, {
130
226
  weights: WEIGHTS,
@@ -1,7 +1,6 @@
1
1
  // @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
- /** @typedef {import('./_lexical.mjs').Candidate} Candidate */
5
4
  /**
6
5
  * Build template candidates (page + block) from the template discovery API.
7
6
  * @param {string} cwd
@@ -8,9 +8,31 @@ import {CORE_PACKAGE} from '../../foundation/discovery/component-discovery.mjs';
8
8
  import {discoverTemplates, extractComponents} from '../template/template.mjs';
9
9
  import {listAvailableThemes} from '../theme/_adapter.mjs';
10
10
  import {templateLookupIds} from '../../foundation/discovery/template-adapter.mjs';
11
+ import {sameWord, wordsOf} from './_words.mjs';
11
12
 
12
13
  /** @typedef {import('./_lexical.mjs').Candidate} Candidate */
13
14
 
15
+ /**
16
+ * A template's authored display name as another name and title of it, when the
17
+ * name stands for the template: two words or more that hold every word of the
18
+ * component the block shows (`exampleFor`), as "Rich Text Editor" does for
19
+ * RichTextEditor. A query that is the name then names the entry whole, and one
20
+ * that holds it as a phrase ("rich text") reaches search's title tier, above
21
+ * entries that match one of its words. A label of the variant alone ("Inside a
22
+ * card" for a Comments block) does not name the block, so it stays out.
23
+ * @param {{displayName?: string, exampleFor?: string}} t
24
+ * @returns {{aliases?: string[], titles?: string[]}}
25
+ */
26
+ function subjectName(t) {
27
+ const name = String(t.displayName ?? '').trim();
28
+ const words = wordsOf(name);
29
+ const subject = wordsOf(String(t.exampleFor ?? ''));
30
+ if (words.length < 2 || subject.length === 0) return {};
31
+ return subject.every(s => words.some(w => sameWord(s, w)))
32
+ ? {aliases: [name], titles: [name]}
33
+ : {};
34
+ }
35
+
14
36
  /**
15
37
  * Build template candidates (page + block) from the template discovery API.
16
38
  * @param {string} cwd
@@ -67,6 +89,9 @@ export async function gatherTemplates(cwd) {
67
89
  // as its prose does, not as a name: one of them matching one word of a
68
90
  // short idea is not a direct match.
69
91
  prose: t.keywords ?? [],
92
+ // A display name that names the block's subject is another name of it
93
+ // and its title (subjectName).
94
+ ...subjectName(t),
70
95
  _displayName: t.name,
71
96
  _package: t.package ?? CORE_PACKAGE,
72
97
  _kind: t.type, // 'page' | 'block'
@@ -0,0 +1,100 @@
1
+ // Copyright (c) Meta Platforms, Inc. and affiliates.
2
+
3
+ /**
4
+ * @file An integration's own block answers a query that names it, though no
5
+ * learned table lists it: its authored display name is a title search reads,
6
+ * so "rich text editor" finds the rich text package's block first and build
7
+ * offers it, while a variant label that does not name the block (Lab's "Inside
8
+ * a card") captures nothing.
9
+ */
10
+
11
+ import fs from 'node:fs';
12
+ import os from 'node:os';
13
+ import path from 'node:path';
14
+ import {fileURLToPath} from 'node:url';
15
+ import {afterAll, beforeAll, describe, expect, it} from 'vitest';
16
+ import {search} from './search.mjs';
17
+ import {build} from '../build/build.mjs';
18
+
19
+ const SLOW = 60_000;
20
+ const packages = path.resolve(
21
+ path.dirname(fileURLToPath(import.meta.url)),
22
+ '../../..',
23
+ );
24
+
25
+ describe('an integration block found by its display name', () => {
26
+ /** @type {string} */
27
+ let dir;
28
+ beforeAll(() => {
29
+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-integration-block-'));
30
+ fs.writeFileSync(
31
+ path.join(dir, 'package.json'),
32
+ JSON.stringify({
33
+ name: 'consumer',
34
+ dependencies: {
35
+ '@astryxdesign/core': '*',
36
+ '@astryxdesign/richtext': '*',
37
+ '@astryxdesign/lab': '*',
38
+ },
39
+ }),
40
+ );
41
+ const scope = path.join(dir, 'node_modules', '@astryxdesign');
42
+ fs.mkdirSync(scope, {recursive: true});
43
+ for (const name of ['core', 'richtext', 'lab'])
44
+ fs.symlinkSync(path.join(packages, name), path.join(scope, name), 'dir');
45
+ });
46
+ afterAll(() => fs.rmSync(dir, {recursive: true, force: true}));
47
+
48
+ it.each(['rich text editor', 'rich text', 'text editor'])(
49
+ '`%s` ranks the rich text block first among templates',
50
+ async query => {
51
+ const {results} = (await search(query, {cwd: dir, type: 'template'}))
52
+ .data;
53
+ expect(results[0], JSON.stringify(results.slice(0, 3))).toMatchObject({
54
+ name: 'RichTextEditorShowcase',
55
+ });
56
+ },
57
+ SLOW,
58
+ );
59
+
60
+ it(
61
+ 'a whole display name names the block outright',
62
+ async () => {
63
+ const {results} = (
64
+ await search('rich text editor', {cwd: dir, type: 'template'})
65
+ ).data;
66
+ expect(results[0]).toMatchObject({
67
+ name: 'RichTextEditorShowcase',
68
+ reason: 'exact name',
69
+ });
70
+ },
71
+ SLOW,
72
+ );
73
+
74
+ it(
75
+ "a variant label that does not name the block's subject is no title",
76
+ async () => {
77
+ const {results} = (
78
+ await search('table inside a card', {cwd: dir, type: 'template'})
79
+ ).data;
80
+ expect(results[0], JSON.stringify(results.slice(0, 3))).toMatchObject({
81
+ name: 'TableInCard',
82
+ });
83
+ const kit = (await build('a settings form inside a card', {cwd: dir}))
84
+ .data;
85
+ expect(kit.blocks[0]?.name).not.toBe('CommentsInCard');
86
+ },
87
+ SLOW,
88
+ );
89
+
90
+ it(
91
+ 'build offers the rich text block for a request that names it',
92
+ async () => {
93
+ const kit = (
94
+ await build('a comment box with a rich text editor', {cwd: dir})
95
+ ).data;
96
+ expect(kit.blocks.map(b => b.name)).toContain('RichTextEditorShowcase');
97
+ },
98
+ SLOW,
99
+ );
100
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astryxdesign/cli",
3
- "version": "0.7.3-canary.04c7468",
3
+ "version": "0.7.3-canary.05bfcc5",
4
4
  "displayName": "CLI",
5
5
  "description": "Scaffold projects, browse templates, generate themes, and get agent-ready docs from the command line.",
6
6
  "author": "Meta Open Source",
@@ -106,10 +106,10 @@
106
106
  "zod": "^4.4.3"
107
107
  },
108
108
  "peerDependencies": {
109
- "@astryxdesign/charts": "0.7.3-canary.04c7468",
110
- "@astryxdesign/core": "0.7.3-canary.04c7468",
111
- "@astryxdesign/lab": "0.7.3-canary.04c7468",
112
- "@astryxdesign/theme-neutral": "0.7.3-canary.04c7468"
109
+ "@astryxdesign/charts": "0.7.3-canary.05bfcc5",
110
+ "@astryxdesign/core": "0.7.3-canary.05bfcc5",
111
+ "@astryxdesign/lab": "0.7.3-canary.05bfcc5",
112
+ "@astryxdesign/theme-neutral": "0.7.3-canary.05bfcc5"
113
113
  },
114
114
  "peerDependenciesMeta": {
115
115
  "@astryxdesign/charts": {
@@ -126,10 +126,10 @@
126
126
  }
127
127
  },
128
128
  "devDependencies": {
129
- "@astryxdesign/charts": "0.7.3-canary.04c7468",
130
- "@astryxdesign/core": "0.7.3-canary.04c7468",
131
- "@astryxdesign/lab": "0.7.3-canary.04c7468",
132
- "@astryxdesign/theme-neutral": "0.7.3-canary.04c7468",
129
+ "@astryxdesign/charts": "0.7.3-canary.05bfcc5",
130
+ "@astryxdesign/core": "0.7.3-canary.05bfcc5",
131
+ "@astryxdesign/lab": "0.7.3-canary.05bfcc5",
132
+ "@astryxdesign/theme-neutral": "0.7.3-canary.05bfcc5",
133
133
  "@heroicons/react": "^2.2.0",
134
134
  "@stylexjs/stylex": "^0.19.0",
135
135
  "@types/babel__core": "^7.20.5",