@astryxdesign/cli 0.7.3-canary.f94e962 → 0.7.3

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 (205) hide show
  1. package/CHANGELOG.md +115 -0
  2. package/README.md +68 -68
  3. package/api/build/_adapter.mjs +37 -25
  4. package/api/build/blocks/blocks.d.mts +68 -18
  5. package/api/build/blocks/blocks.mjs +475 -116
  6. package/api/build/blocks/blocks.test.mjs +191 -61
  7. package/api/build/blocks/blocks.weights.json +1 -1
  8. package/api/build/build.type.mjs +1 -1
  9. package/api/build/kit/answer.d.mts +9 -5
  10. package/api/build/kit/answer.mjs +123 -13
  11. package/api/build/kit/answer.test.mjs +73 -3
  12. package/api/build/kit/signals.d.mts +19 -0
  13. package/api/build/kit/signals.mjs +18 -7
  14. package/api/build/kit/weights.d.mts +8 -22
  15. package/api/build/kit/weights.mjs +87 -8
  16. package/api/build/kit/weights.test.mjs +96 -0
  17. package/api/build/tree.mjs +2 -1
  18. package/api/build/tree.test.mjs +37 -29
  19. package/api/component/_adapter.mjs +39 -5
  20. package/api/component/component-replacement.test.mjs +1 -0
  21. package/api/component/component.mjs +1 -1
  22. package/api/component/doc-links.test.mjs +2 -2
  23. package/api/discover/_adapter.d.mts +1 -2
  24. package/api/discover/_adapter.mjs +6 -8
  25. package/api/discover/discover.mjs +0 -1
  26. package/api/docs/_adapter.d.mts +2 -2
  27. package/api/docs/_adapter.mjs +65 -18
  28. package/api/docs/docs-homes.test.mjs +127 -0
  29. package/api/docs/docs.doc.mjs +3 -3
  30. package/api/docs/docs.test.mjs +28 -18
  31. package/api/docs/integration-tree.test.mjs +20 -20
  32. package/api/docs/integrationDocs.test.mjs +1 -1
  33. package/api/docs/namespace-section.test.mjs +6 -6
  34. package/api/docs/reference-blocks.test.mjs +2 -2
  35. package/api/doctor/_adapter.d.mts +33 -13
  36. package/api/doctor/_adapter.mjs +55 -25
  37. package/api/doctor/doctor.d.mts +23 -5
  38. package/api/doctor/doctor.mjs +97 -28
  39. package/api/doctor/doctor.test.mjs +67 -9
  40. package/api/hook/hook.mjs +1 -1
  41. package/api/integration/validate-integration-fixes.test.mjs +6 -5
  42. package/api/integration/validate-integration-gaps.test.mjs +12 -1
  43. package/api/integration/validate-integration.d.mts +4 -0
  44. package/api/integration/validate-integration.mjs +48 -7
  45. package/api/integration/validate-integration.type.d.mts +36 -5
  46. package/api/integration/validate-integration.type.mjs +27 -6
  47. package/api/integration/validateIntegration.doc.mjs +1 -1
  48. package/api/search/_adapter.d.mts +16 -0
  49. package/api/search/_adapter.mjs +39 -0
  50. package/api/search/_adapter.test.mjs +173 -0
  51. package/api/search/_candidate-score.mjs +1 -1
  52. package/api/search/_entries.d.mts +11 -0
  53. package/api/search/_entries.mjs +35 -17
  54. package/api/search/_entries.test.mjs +48 -0
  55. package/api/search/_gather-docs.mjs +3 -3
  56. package/api/search/_gather-templates.d.mts +0 -1
  57. package/api/search/_gather-templates.mjs +25 -0
  58. package/api/search/search.doc.mjs +1 -1
  59. package/api/search/search.integration-block.test.mjs +100 -0
  60. package/api/search/search.mjs +6 -2
  61. package/api/search/search.test.mjs +14 -14
  62. package/api/template/template.mjs +5 -1
  63. package/assets/docs/author-a-theme.doc.mjs +2 -2
  64. package/assets/docs/tree/add-a-component.doc.mjs +1 -1
  65. package/assets/docs/tree/add-a-theme.doc.mjs +1 -1
  66. package/assets/docs/tree/add-a-topic.doc.mjs +1 -1
  67. package/assets/docs/tree/agent-guidance.doc.mjs +1 -1
  68. package/assets/docs/tree/{integrations.doc.mjs → authoring.doc.mjs} +15 -6
  69. package/assets/docs/tree/block-template.doc.mjs +1 -1
  70. package/assets/docs/tree/build-the-template.doc.mjs +1 -1
  71. package/assets/docs/tree/building-blocks.doc.mjs +2 -2
  72. package/assets/docs/tree/check-the-props.doc.mjs +1 -1
  73. package/assets/docs/tree/check-your-docs.doc.mjs +2 -2
  74. package/assets/docs/tree/checks.doc.mjs +2 -2
  75. package/assets/docs/tree/ci-and-containers.doc.mjs +13 -0
  76. package/assets/docs/tree/cli-command.doc.mjs +1 -1
  77. package/assets/docs/tree/cli.doc.mjs +1 -1
  78. package/assets/docs/tree/codemods.doc.mjs +1 -1
  79. package/assets/docs/tree/component-family.doc.mjs +1 -1
  80. package/assets/docs/tree/component-imports.doc.mjs +1 -1
  81. package/assets/docs/tree/components.doc.mjs +1 -1
  82. package/assets/docs/tree/configuration.doc.mjs +1 -1
  83. package/assets/docs/tree/debug-and-gap-reports.doc.mjs +1 -1
  84. package/assets/docs/tree/define-the-theme.doc.mjs +1 -1
  85. package/assets/docs/tree/describe-the-component.doc.mjs +1 -1
  86. package/assets/docs/tree/docs.doc.mjs +1 -1
  87. package/assets/docs/tree/document-the-template.doc.mjs +1 -1
  88. package/assets/docs/tree/document-the-theme.doc.mjs +1 -1
  89. package/assets/docs/tree/export-template-assets.doc.mjs +1 -1
  90. package/assets/docs/tree/extend-or-replace.doc.mjs +1 -1
  91. package/assets/docs/{authoring.doc.mjs → tree/field-reference.doc.mjs} +4 -3
  92. package/assets/docs/tree/fonts-and-assets.doc.mjs +1 -1
  93. package/assets/docs/tree/generate-a-palette.doc.mjs +1 -1
  94. package/assets/docs/tree/global-integrations.doc.mjs +2 -2
  95. package/assets/docs/tree/grade-template-with-agent.doc.mjs +4 -4
  96. package/assets/docs/tree/help.doc.mjs +2 -2
  97. package/assets/docs/tree/links.doc.mjs +1 -1
  98. package/assets/docs/tree/package-and-test.doc.mjs +1 -1
  99. package/assets/docs/tree/page-template.doc.mjs +1 -1
  100. package/assets/docs/tree/publishing.doc.mjs +1 -1
  101. package/assets/docs/tree/quick-start.doc.mjs +1 -1
  102. package/assets/docs/tree/replace-a-core-component.doc.mjs +1 -1
  103. package/assets/docs/tree/replace-a-core-template.doc.mjs +1 -1
  104. package/assets/docs/tree/sections-and-placement.doc.mjs +1 -1
  105. package/assets/docs/tree/see-it-in-an-app.doc.mjs +1 -1
  106. package/assets/docs/tree/ship.doc.mjs +2 -2
  107. package/assets/docs/tree/short-and-findable.doc.mjs +1 -1
  108. package/assets/docs/tree/single-component.doc.mjs +1 -1
  109. package/assets/docs/tree/start-a-template.doc.mjs +1 -1
  110. package/assets/docs/tree/subcomponent.doc.mjs +1 -1
  111. package/assets/docs/tree/template-assets.doc.mjs +1 -1
  112. package/assets/docs/tree/template-doc-overview.doc.mjs +2 -2
  113. package/assets/docs/tree/template-fonts.doc.mjs +1 -1
  114. package/assets/docs/tree/template-grading-rubric.doc.mjs +1 -1
  115. package/assets/docs/tree/template-icons.doc.mjs +1 -1
  116. package/assets/docs/tree/template-images-media.doc.mjs +1 -1
  117. package/assets/docs/tree/template-styles.doc.mjs +1 -1
  118. package/assets/docs/tree/templates.doc.mjs +1 -1
  119. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  120. package/assets/docs/tree/test-template-in-app.doc.mjs +1 -1
  121. package/assets/docs/tree/themes.doc.mjs +1 -1
  122. package/assets/docs/tree/troubleshooting.doc.mjs +1 -1
  123. package/assets/docs/tree/upgrading.doc.mjs +1 -1
  124. package/assets/docs/tree/use-a-theme-in-an-app.doc.mjs +1 -1
  125. package/assets/docs/tree/verify-packed-template.doc.mjs +1 -1
  126. package/assets/docs/tree/versioning.doc.mjs +5 -1
  127. package/assets/docs/tree/write-good-templates.doc.mjs +1 -1
  128. package/assets/docs/tree/write-the-template-file.doc.mjs +1 -1
  129. package/assets/docs/use-integrations.doc.mjs +120 -0
  130. package/authoring/doctypes/schema/schema.doc.mjs +1 -1
  131. package/authoring/integration/integration.doc.mjs +1 -1
  132. package/authoring/integration/type.ts +1 -1
  133. package/clients/cli/command-result-coverage.test.mjs +6 -5
  134. package/clients/cli/commands/build-theme.color-scheme.test.mjs +1 -1
  135. package/clients/cli/commands/{component/index.mjs → component.mjs} +37 -37
  136. package/clients/cli/commands/component.test.mjs +6 -6
  137. package/clients/cli/commands/docs.doc.mjs +2 -2
  138. package/clients/cli/commands/docs.mjs +1 -1
  139. package/clients/cli/commands/docs.test.mjs +14 -14
  140. package/clients/cli/commands/doctor-integration.test.mjs +49 -1
  141. package/clients/cli/commands/doctor.doc.mjs +1 -1
  142. package/clients/cli/commands/doctor.mjs +10 -0
  143. package/clients/cli/commands/{hook/index.mjs → hook.mjs} +22 -22
  144. package/clients/cli/commands/import-hint-correctness.test.mjs +1 -1
  145. package/clients/cli/commands/integration.doc.mjs +1 -1
  146. package/clients/cli/commands/manifest.doc.mjs +1 -1
  147. package/clients/cli/commands/manifest.mjs +67 -0
  148. package/clients/cli/commands/outside-project.test.mjs +99 -0
  149. package/clients/cli/commands/postinstall.doc.mjs +23 -0
  150. package/clients/cli/commands/postinstall.mjs +31 -0
  151. package/clients/cli/commands/setup-nudge.test.mjs +17 -0
  152. package/clients/cli/commands/{build-theme.mjs → theme.mjs} +1 -1
  153. package/clients/cli/index.mjs +35 -93
  154. package/clients/cli/lib/component-format.mjs +1 -1
  155. package/clients/cli/lib/define-command.mjs +12 -7
  156. package/clients/cli/lib/exit-codes.test.mjs +5 -0
  157. package/clients/cli/lib/manifest.test.mjs +12 -0
  158. package/clients/cli/lib/welcome-box.mjs +50 -0
  159. package/foundation/agent-docs/agent-doc-state.d.mts +12 -0
  160. package/foundation/agent-docs/agent-doc-state.mjs +24 -2
  161. package/foundation/agent-docs/agent-docs.mjs +7 -6
  162. package/foundation/agent-docs/agent-docs.test.mjs +5 -4
  163. package/foundation/agent-docs/computer-agent-text.d.mts +39 -2
  164. package/foundation/agent-docs/computer-agent-text.mjs +135 -8
  165. package/foundation/agent-docs/computer-agent-text.test.mjs +99 -2
  166. package/foundation/discovery/authoring-self-docs.d.mts +7 -0
  167. package/foundation/discovery/authoring-self-docs.mjs +16 -4
  168. package/foundation/discovery/authoring-self-docs.test.mjs +15 -3
  169. package/foundation/discovery/authoring-surface.d.mts +1 -1
  170. package/foundation/discovery/authoring-surface.mjs +2 -2
  171. package/foundation/discovery/authoring-surface.test.mjs +1 -1
  172. package/foundation/discovery/cli-self-docs.d.mts +2 -1
  173. package/foundation/discovery/cli-self-docs.mjs +4 -3
  174. package/foundation/discovery/cli-self-docs.test.mjs +2 -2
  175. package/foundation/discovery/runner-commands.mjs +1 -0
  176. package/foundation/discovery/stray-pointers.test.mjs +1 -1
  177. package/foundation/doc-compiler/doc-loads.test.mjs +21 -9
  178. package/foundation/doc-compiler/split-topic-keywords.test.mjs +3 -2
  179. package/foundation/doc-compiler/tree.d.mts +32 -6
  180. package/foundation/doc-compiler/tree.mjs +77 -43
  181. package/foundation/doc-compiler/tree.test.mjs +7 -7
  182. package/foundation/fs/read-record.d.mts +18 -0
  183. package/foundation/fs/read-record.mjs +131 -0
  184. package/foundation/fs/read-record.test.mjs +77 -0
  185. package/foundation/integrations/cli-peer-version.d.mts +36 -0
  186. package/foundation/integrations/cli-peer-version.mjs +132 -0
  187. package/foundation/integrations/cli-peer-version.test.mjs +354 -0
  188. package/foundation/integrations/exports-subpaths.d.mts +57 -0
  189. package/foundation/integrations/exports-subpaths.mjs +113 -0
  190. package/foundation/integrations/public-imports.d.mts +118 -0
  191. package/foundation/integrations/public-imports.mjs +1769 -0
  192. package/foundation/integrations/public-imports.test.mjs +1793 -0
  193. package/foundation/release/cache.test.mjs +24 -21
  194. package/foundation/release/global-state.d.mts +8 -4
  195. package/foundation/release/global-state.mjs +4 -2
  196. package/foundation/release/notices.d.mts +62 -8
  197. package/foundation/release/notices.mjs +136 -25
  198. package/foundation/release/notices.test.mjs +21 -0
  199. package/foundation/response/response-types.doc.mjs +1 -1
  200. package/foundation/text/string-utils.d.mts +2 -1
  201. package/foundation/text/string-utils.mjs +16 -5
  202. package/foundation/text/suggest-order.d.mts +72 -0
  203. package/foundation/text/suggest-order.mjs +363 -0
  204. package/foundation/text/suggest-order.test.mjs +259 -0
  205. package/package.json +9 -9
@@ -77,7 +77,7 @@
77
77
  * @property {boolean} data.directMatch True when the top page template is a confident direct match.
78
78
  * @property {BuildStart | null} data.start The page template to start from: the ranker's pick, else the app shell. Null only when the kit is narrowed to components or hooks (`type`), or when the project has no page template to offer.
79
79
  * @property {import('../search/search.type.mjs').SearchResultEntry[]} data.pages Closest page templates by search (≤3). Each entry's `command` carries `--skeleton` when `directMatch` is false, so it previews the layout; `start.command` is the scaffold.
80
- * @property {import('../search/search.type.mjs').SearchResultEntry[]} data.blocks Drop-in block patterns covering parts of the idea (≤5).
80
+ * @property {import('../search/search.type.mjs').SearchResultEntry[]} data.blocks Drop-in block patterns covering parts of the idea (≤5), best first. A block search returned keeps search's score and reason; any other block carries the block picker's relative score (100 for its best candidate, never below 1) and reason, so the two kinds of score are not comparable.
81
81
  * @property {import('../search/search.type.mjs').SearchResultEntry[]} data.domain Idea-specific components/hooks (≤6), excluding frame/foundation.
82
82
  * @property {string[]} data.frame Always-on page-shell component names. Every page template already uses them.
83
83
  * @property {string[]} data.foundation Always-on layout/typography/action component names. Every page template already uses them.
@@ -2,13 +2,17 @@
2
2
  // DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
3
3
 
4
4
  /**
5
- * The kit for a request. `blocks` re-orders the blocks build names itself:
6
- * each keeps build's own entry (its score and reason), and a name build did
7
- * not give is dropped, so the kit never lists more blocks than build's own.
8
- * Blocks of the same name from two packages are both kept.
5
+ * The kit for a request. `blocks` names the blocks to list, best first: a
6
+ * block build names itself keeps build's entry, another keeps search's own
7
+ * entry (its real score and reason), a name search did not return is
8
+ * dropped, and the kit never lists more blocks than build's own. Blocks of
9
+ * the same name from two packages are both kept.
9
10
  *
10
11
  * @param {import('./signals.mjs').KitSignals} signals
11
12
  * @param {string[]} [blocks] block names; build's own when omitted
13
+ * @param {Map<string, import('../../search/search.type.mjs').SearchResultEntry>} [picked]
14
+ * the block picker's own entry (its real score and reason) for each named
15
+ * block search did not return
12
16
  * @returns {import('../build.type.mjs').BuildKitResponse}
13
17
  */
14
- export function kitAnswer(signals: import("./signals.mjs").KitSignals, blocks?: string[]): import("../build.type.mjs").BuildKitResponse;
18
+ export function kitAnswer(signals: import("./signals.mjs").KitSignals, blocks?: string[], picked?: Map<string, import("../../search/search.type.mjs").SearchResultEntry>): import("../build.type.mjs").BuildKitResponse;
@@ -10,6 +10,7 @@
10
10
  * calls it with the block picker's list.
11
11
  */
12
12
 
13
+ import {firstSentence} from '../../../foundation/text/string-utils.mjs';
13
14
  import {FOUNDATION, FRAME} from './signals.mjs';
14
15
 
15
16
  /**
@@ -23,16 +24,24 @@ import {FOUNDATION, FRAME} from './signals.mjs';
23
24
  const THIN_KIT = 3;
24
25
 
25
26
  /**
26
- * The kit for a request. `blocks` re-orders the blocks build names itself:
27
- * each keeps build's own entry (its score and reason), and a name build did
28
- * not give is dropped, so the kit never lists more blocks than build's own.
29
- * Blocks of the same name from two packages are both kept.
27
+ * The kit for a request. `blocks` names the blocks to list, best first: a
28
+ * block build names itself keeps build's entry, another keeps search's own
29
+ * entry (its real score and reason), a name search did not return is
30
+ * dropped, and the kit never lists more blocks than build's own. Blocks of
31
+ * the same name from two packages are both kept.
30
32
  *
31
33
  * @param {import('./signals.mjs').KitSignals} signals
32
34
  * @param {string[]} [blocks] block names; build's own when omitted
35
+ * @param {Map<string, import('../../search/search.type.mjs').SearchResultEntry>} [picked]
36
+ * the block picker's own entry (its real score and reason) for each named
37
+ * block search did not return
33
38
  * @returns {import('../build.type.mjs').BuildKitResponse}
34
39
  */
35
- export function kitAnswer(signals, blocks = signals.kitBlocks) {
40
+ export function kitAnswer(
41
+ signals,
42
+ blocks = signals.kitBlocks,
43
+ picked = new Map(),
44
+ ) {
36
45
  // Entries by name, in build's order: two packages can ship a block with the
37
46
  // same name, and each name in `blocks` takes the next entry of that name, so
38
47
  // both stay listed.
@@ -40,10 +49,23 @@ export function kitAnswer(signals, blocks = signals.kitBlocks) {
40
49
  const own = new Map();
41
50
  for (const b of signals.blocks)
42
51
  own.set(b.name, [...(own.get(b.name) ?? []), b]);
43
- const named = blocks.flatMap(name => {
44
- const entry = own.get(name)?.shift();
45
- return entry ? [entry] : [];
46
- });
52
+ /** @type {Set<string>} */
53
+ const added = new Set();
54
+ const named = blocks
55
+ .flatMap(name => {
56
+ const entry = own.get(name)?.shift();
57
+ if (entry) return [entry];
58
+ // A block build does not name itself keeps search's own entry, once;
59
+ // one search did not return keeps the picker's entry.
60
+ const found =
61
+ own.has(name) || added.has(name)
62
+ ? undefined
63
+ : (signals.searchedEntries.get(name) ?? picked.get(name));
64
+ if (!found) return [];
65
+ added.add(name);
66
+ return [found];
67
+ })
68
+ .slice(0, signals.blocks.length);
47
69
 
48
70
  // What to try when the kit comes back thin. Keyword search over a design
49
71
  // system misses in a predictable way — the reader's words and the package's
@@ -64,8 +86,8 @@ export function kitAnswer(signals, blocks = signals.kitBlocks) {
64
86
  }
65
87
  : undefined;
66
88
 
67
- return {
68
- type: 'build.kit',
89
+ const answer = (/** @type {typeof named} */ list) => ({
90
+ type: /** @type {const} */ ('build.kit'),
69
91
  data: {
70
92
  query: signals.query,
71
93
  // Distinguishes "search found nothing" from a weak-but-non-empty result
@@ -75,11 +97,99 @@ export function kitAnswer(signals, blocks = signals.kitBlocks) {
75
97
  directMatch: signals.directMatch,
76
98
  start: signals.start,
77
99
  pages: signals.pages,
78
- blocks: named,
100
+ blocks: list,
79
101
  domain: signals.domain,
80
102
  frame: FRAME,
81
103
  foundation: FOUNDATION,
82
104
  hint,
83
105
  },
84
- };
106
+ });
107
+
108
+ // An added block must not make the answer longer than build's own, as JSON
109
+ // or as text: trim the added blocks' descriptions, never which blocks are
110
+ // listed.
111
+ const fitted = fitDescriptions(named, signals.blocks, {
112
+ json: list => bytes(answer(list)),
113
+ text: textSize,
114
+ });
115
+ return answer(fitted);
116
+ }
117
+
118
+ /** @param {unknown} value */
119
+ const bytes = value => Buffer.byteLength(JSON.stringify(value));
120
+
121
+ /**
122
+ * What the blocks add to the text answer, which prints each block's name,
123
+ * package, and the first sentence of its description.
124
+ * @param {{name: string, package?: string, description?: string}[]} list
125
+ */
126
+ const textSize = list =>
127
+ list.reduce(
128
+ (n, b) =>
129
+ n +
130
+ Buffer.byteLength(
131
+ `${b.name}${b.package ?? ''}${firstSentence(b.description ?? '')}`,
132
+ ),
133
+ 0,
134
+ );
135
+
136
+ /** An added block keeps at least this much of its description. */
137
+ const MIN_DESCRIPTION = 24;
138
+
139
+ /**
140
+ * A description cut to about `keep` characters (never fewer than
141
+ * {@link MIN_DESCRIPTION}) at a word boundary, with an ellipsis. Never empty:
142
+ * a description already that short is kept whole.
143
+ * @param {string} description
144
+ * @param {number} keep
145
+ */
146
+ function cutTo(description, keep) {
147
+ const room = Math.max(keep, MIN_DESCRIPTION);
148
+ if (description.length <= room) return description;
149
+ const cut = description.slice(0, room);
150
+ const at = cut.lastIndexOf(' ');
151
+ return `${(at > 0 ? cut.slice(0, at) : cut).trimEnd()}…`;
152
+ }
153
+
154
+ /**
155
+ * The listed blocks with the added blocks' descriptions shortened until
156
+ * neither the JSON nor the text answer is longer than with build's own
157
+ * blocks, or every added description is as short as it may get. A
158
+ * description is never emptied, and build's own entries never change.
159
+ * @template {{name: string, package?: string, description?: string}} B
160
+ * @param {B[]} list
161
+ * @param {B[]} own build's own entries
162
+ * @param {{json: (list: B[]) => number, text: (list: B[]) => number}} size
163
+ * @returns {B[]}
164
+ */
165
+ function fitDescriptions(list, own, size) {
166
+ const limit = {json: size.json(own), text: size.text(own)};
167
+ let out = list;
168
+ /** The bytes over each limit; 0 when within. */
169
+ const over = (/** @type {B[]} */ l) => ({
170
+ text: Math.max(0, size.text(l) - limit.text),
171
+ json: Math.max(0, size.json(l) - limit.json),
172
+ });
173
+ // The last added block first, each cut by what is still over and never
174
+ // below MIN_DESCRIPTION; when one is as short as it may get, the next one
175
+ // up is cut, and passes repeat while any cut still helps.
176
+ for (let changed = true; changed;) {
177
+ changed = false;
178
+ for (let k = out.length - 1; k >= 0; k--) {
179
+ const o = over(out);
180
+ if (o.text === 0 && o.json === 0) return out;
181
+ const b = out[k];
182
+ if (own.includes(b) || !b.description) continue;
183
+ const first = firstSentence(b.description);
184
+ const keep = Math.min(
185
+ o.text > 0 ? first.length - o.text - 1 : b.description.length,
186
+ o.json > 0 ? b.description.length - o.json - 1 : b.description.length,
187
+ );
188
+ const description = cutTo(b.description, keep);
189
+ if (description.length >= b.description.length) continue;
190
+ out = out.map((x, i) => (i === k ? {...x, description} : x));
191
+ changed = true;
192
+ }
193
+ }
194
+ return out;
85
195
  }
@@ -3,8 +3,11 @@
3
3
  import {describe, it, expect} from 'vitest';
4
4
  import {kitAnswer} from './answer.mjs';
5
5
 
6
- /** @param {any[]} blocks */
7
- const signals = blocks =>
6
+ /**
7
+ * @param {any[]} blocks
8
+ * @param {any[]} [searched] blocks search returned that build did not name
9
+ */
10
+ const signals = (blocks, searched = []) =>
8
11
  /** @type {any} */ ({
9
12
  query: 'q',
10
13
  matchCount: blocks.length,
@@ -14,6 +17,7 @@ const signals = blocks =>
14
17
  blocks,
15
18
  kitBlocks: blocks.map(b => b.name),
16
19
  domain: [],
20
+ searchedEntries: new Map(searched.map(b => [b.name, b])),
17
21
  });
18
22
 
19
23
  describe('kitAnswer', () => {
@@ -31,8 +35,74 @@ describe('kitAnswer', () => {
31
35
  expect(kit.data.blocks).toEqual([c, a, b]);
32
36
  });
33
37
 
34
- it('never lists a block build did not give, or more than it gave', () => {
38
+ it('never lists a block neither build nor search gave, or more than build gave', () => {
35
39
  const kit = kitAnswer(signals([a, c]), ['Hero', 'Hero', 'Nav', 'Footer']);
36
40
  expect(kit.data.blocks).toEqual([a, c]);
37
41
  });
42
+
43
+ it('lists a block search returned with search’s own entry, within build’s count', () => {
44
+ const nav = {name: 'Nav', package: '@a/one', score: 60, reason: 'keyword'};
45
+ const kit = kitAnswer(signals([a, c], [nav]), [
46
+ 'Nav',
47
+ 'Nav',
48
+ 'Hero',
49
+ 'Footer',
50
+ ]);
51
+ expect(kit.data.blocks).toEqual([nav, a]);
52
+ });
53
+
54
+ it('trims an added block’s description so the answer is never longer than build’s own', () => {
55
+ const hero = {
56
+ ...a,
57
+ description: 'A wide hero with a title and two buttons.',
58
+ };
59
+ const foot = {...c, description: 'A footer with links in three columns.'};
60
+ const nav = {
61
+ name: 'Nav',
62
+ package: '@a/one',
63
+ score: 60,
64
+ reason: 'keyword',
65
+ description: 'word '.repeat(200).trim(),
66
+ };
67
+ const own = kitAnswer(signals([hero, foot]));
68
+ const kit = kitAnswer(signals([hero, foot], [nav]), ['Nav', 'Hero']);
69
+ expect(kit.data.blocks.map(b => b.name)).toEqual(['Nav', 'Hero']);
70
+ expect(kit.data.blocks[1]).toEqual(hero);
71
+ expect(Buffer.byteLength(JSON.stringify(kit))).toBeLessThanOrEqual(
72
+ Buffer.byteLength(JSON.stringify(own)),
73
+ );
74
+ expect(kit.data.blocks[0].description).toMatch(/…$/);
75
+ });
76
+ it('trims an added block’s first sentence so the text answer is never longer either', () => {
77
+ const long = ' More words follow here. '.repeat(20);
78
+ const hero = {...a, description: `A hero.${long}`};
79
+ const foot = {...c, description: `A footer.${long}`};
80
+ const nav = {
81
+ name: 'Nav',
82
+ package: '@a/one',
83
+ score: 60,
84
+ reason: 'keyword',
85
+ description:
86
+ 'A navigation bar with a logo, five links, a search box and a menu.',
87
+ };
88
+ const kit = kitAnswer(signals([hero, foot], [nav]), ['Nav', 'Hero']);
89
+ const d = kit.data.blocks[0].description;
90
+ expect(d).toMatch(/…$/);
91
+ expect(d.length).toBeLessThan(nav.description.length);
92
+ expect(nav.description.startsWith(d.slice(0, -1))).toBe(true);
93
+ expect(kit.data.blocks[1]).toEqual(hero);
94
+ });
95
+ it('never empties an added block’s description', () => {
96
+ const nav = {
97
+ name: 'Nav',
98
+ package: '@a/one',
99
+ score: 60,
100
+ reason: 'keyword',
101
+ description:
102
+ 'A navigation bar with a logo, five links, a search box and a menu.',
103
+ };
104
+ const kit = kitAnswer(signals([a, c], [nav]), ['Nav', 'Hero']);
105
+ expect(kit.data.blocks[0].name).toBe('Nav');
106
+ expect(kit.data.blocks[0].description.length).toBeGreaterThan(0);
107
+ });
38
108
  });
@@ -53,8 +53,27 @@ export type KitSignals = {
53
53
  * every block search returned, in its order
54
54
  */
55
55
  searched: BlockScore[];
56
+ /**
57
+ * search's entry for each of those blocks (the first of a name)
58
+ */
59
+ searchedEntries: Map<string, SearchResultEntry>;
60
+ /**
61
+ * each of those blocks' display name and description, from the catalog
62
+ */
63
+ blockText: Map<string, string>;
56
64
  /**
57
65
  * the blocks, named, searched or scaffoldable, whose package is not Core's
58
66
  */
59
67
  foreign: string[];
68
+ /**
69
+ * every block the project can scaffold
70
+ */
71
+ catalog: import("../_adapter.mjs").BlockTemplate[];
72
+ /**
73
+ * every component, with its keywords
74
+ */
75
+ components: {
76
+ name: string;
77
+ keywords?: string[];
78
+ }[];
60
79
  };
@@ -100,7 +100,11 @@ const ALWAYS = new Set([...FRAME, ...FOUNDATION]);
100
100
  * @property {SearchResultEntry[]} domain
101
101
  * @property {string[]} kitBlocks the names of `blocks`
102
102
  * @property {BlockScore[]} searched every block search returned, in its order
103
+ * @property {Map<string, SearchResultEntry>} searchedEntries search's entry for each of those blocks (the first of a name)
104
+ * @property {Map<string, string>} blockText each of those blocks' display name and description, from the catalog
103
105
  * @property {string[]} foreign the blocks, named, searched or scaffoldable, whose package is not Core's
106
+ * @property {import('../_adapter.mjs').BlockTemplate[]} catalog every block the project can scaffold
107
+ * @property {{name: string, keywords?: string[]}[]} components every component, with its keywords
104
108
  */
105
109
 
106
110
  /**
@@ -230,13 +234,10 @@ export async function kitSignals(query, options = {}) {
230
234
  // A part of a page starts where it lives (spec:AST-048/FR3); the project's
231
235
  // own components say what a part is. The search above already gathered them
232
236
  // unless it was narrowed to templates.
233
- const kind = wantsPages
234
- ? ideaKind(
235
- query,
236
- catalog,
237
- searchedComponents(result) ?? (await loadComponents(cwd)),
238
- )
239
- : 'page';
237
+ const components = wantsPages
238
+ ? (searchedComponents(result) ?? (await loadComponents(cwd)))
239
+ : [];
240
+ const kind = wantsPages ? ideaKind(query, catalog, components) : 'page';
240
241
  const chosen = wantsPages
241
242
  ? chooseStart(ranked, kind, matchedPages, directMatch, catalog, query)
242
243
  : null;
@@ -273,6 +274,16 @@ export async function kitSignals(query, options = {}) {
273
274
  domain: domainKept,
274
275
  kitBlocks: blocksKept.map(r => r.name),
275
276
  searched: searchedBlocks.map(blockScore),
277
+ searchedEntries: new Map(
278
+ [...searchedBlocks].reverse().map(r => [r.name, r]),
279
+ ),
280
+ blockText: new Map(
281
+ templates.blocks
282
+ .filter(b => searchedBlocks.some(r => r.name === b.name))
283
+ .map(b => [b.name, `${b.displayName} ${b.description}`]),
284
+ ),
285
+ catalog: templates.blocks,
286
+ components,
276
287
  foreign: [
277
288
  ...new Set(
278
289
  [...blocksKept, ...searchedBlocks, ...templates.blocks]
@@ -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,
@@ -35,7 +35,8 @@ const main = defineSearcher({
35
35
  run: async (request, ctx) => {
36
36
  const signals = await kitSignals(request.query, request.options);
37
37
  if (!ctx.blocks) return kitAnswer(signals);
38
- return kitAnswer(signals, pickBlocks(request.query, signals, ctx.blocks));
38
+ const picked = pickBlocks(request.query, signals, ctx.blocks);
39
+ return kitAnswer(signals, picked.names, picked.entries);
39
40
  },
40
41
  });
41
42