@astryxdesign/cli 0.7.16 → 0.7.17-canary.gb36275e

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 (239) hide show
  1. package/README.md +13 -23
  2. package/api/build/_adapter.d.mts +5 -0
  3. package/api/build/_adapter.mjs +49 -34
  4. package/api/build/blocks/blocks.d.mts +7 -5
  5. package/api/build/blocks/blocks.mjs +113 -26
  6. package/api/build/build.doc.mjs +2 -1
  7. package/api/build/kit/rank.mjs +59 -12
  8. package/api/build/kit/weights.d.mts +16 -3
  9. package/api/build/kit/weights.json +1 -1
  10. package/api/build/kit/weights.mjs +238 -31
  11. package/api/build/kit/word-model.d.mts +23 -0
  12. package/api/build/kit/word-model.mjs +46 -0
  13. package/api/build/kit/words.d.mts +42 -0
  14. package/api/build/kit/words.mjs +82 -1
  15. package/api/component/__fixtures__/resolutions/modules.txt +4 -5
  16. package/api/component/list/list.d.mts +1 -1
  17. package/api/component/list/list.mjs +46 -5
  18. package/api/discover/_adapter.d.mts +1 -1
  19. package/api/docs/_adapter.d.mts +26 -2
  20. package/api/docs/_adapter.mjs +217 -5
  21. package/api/docs/docs.d.mts +3 -0
  22. package/api/docs/docs.doc.mjs +7 -1
  23. package/api/docs/docs.mjs +12 -0
  24. package/api/docs/docs.type.d.mts +5 -0
  25. package/api/docs/docs.type.mjs +2 -0
  26. package/api/docs/node/node.mjs +1 -1
  27. package/api/doctor/_adapter.d.mts +27 -5
  28. package/api/doctor/_adapter.mjs +77 -6
  29. package/api/doctor/built-checks.mjs +38 -13
  30. package/api/doctor/doctor.d.mts +4 -3
  31. package/api/doctor/doctor.mjs +14 -7
  32. package/api/doctor/theme-checks.mjs +121 -17
  33. package/api/render/_adapter.mjs +531 -192
  34. package/api/render/render.doc.mjs +11 -3
  35. package/api/render/render.type.d.mts +30 -0
  36. package/api/render/render.type.mjs +19 -0
  37. package/api/search/ORDERING.md +1 -1
  38. package/api/search/_candidate-score.mjs +51 -32
  39. package/api/search/_fit-order.mjs +3 -3
  40. package/api/search/_result.d.mts +5 -5
  41. package/api/search/_score.d.mts +6 -1
  42. package/api/search/_score.mjs +15 -3
  43. package/api/search/search.d.mts +1 -1
  44. package/api/theme/_adapter.mjs +10 -0
  45. package/api/theme/build/build.d.mts +47 -0
  46. package/api/theme/build/build.mjs +147 -25
  47. package/api/theme/theme.type.d.mts +10 -0
  48. package/api/theme/theme.type.mjs +7 -1
  49. package/api/theme-edit/_adapter.d.mts +48 -0
  50. package/api/theme-edit/_adapter.mjs +675 -42
  51. package/api/theme-edit/_check.d.mts +28 -22
  52. package/api/theme-edit/_check.mjs +1481 -252
  53. package/api/theme-edit/_color.d.mts +9 -0
  54. package/api/theme-edit/_color.mjs +41 -1
  55. package/api/theme-edit/_compiler.mjs +9 -0
  56. package/api/theme-edit/_context.mjs +60 -3
  57. package/api/theme-edit/_css.mjs +21 -1
  58. package/api/theme-edit/_edit.d.mts +22 -0
  59. package/api/theme-edit/_edit.mjs +1196 -88
  60. package/api/theme-edit/_how-commands.d.mts +57 -0
  61. package/api/theme-edit/_how-commands.mjs +536 -1
  62. package/api/theme-edit/_how-question.d.mts +65 -0
  63. package/api/theme-edit/_how-question.mjs +4401 -225
  64. package/api/theme-edit/_how.mjs +104 -4
  65. package/api/theme-edit/_marks.d.mts +31 -5
  66. package/api/theme-edit/_marks.mjs +57 -5
  67. package/api/theme-edit/_normal.d.mts +153 -0
  68. package/api/theme-edit/_normal.mjs +661 -0
  69. package/api/theme-edit/_palette.mjs +10 -4
  70. package/api/theme-edit/_parts.d.mts +8 -0
  71. package/api/theme-edit/_parts.mjs +103 -2
  72. package/api/theme-edit/_preview-app.d.mts +7 -1
  73. package/api/theme-edit/_preview-app.mjs +205 -17
  74. package/api/theme-edit/_preview.d.mts +64 -8
  75. package/api/theme-edit/_preview.mjs +447 -89
  76. package/api/theme-edit/_settings.d.mts +176 -5
  77. package/api/theme-edit/_settings.mjs +1936 -128
  78. package/api/theme-edit/_tables.d.mts +6 -0
  79. package/api/theme-edit/_tables.mjs +32 -1
  80. package/api/theme-edit/theme-edit.type.d.mts +10 -1
  81. package/api/theme-edit/theme-edit.type.mjs +4 -1
  82. package/api/theme-edit/themeEditPreviewApp.doc.mjs +3 -3
  83. package/assets/codemods/run-codemod.mjs +73 -6
  84. package/assets/codemods/transforms/next/index.mjs +11 -1
  85. package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
  86. package/assets/docs/author-a-theme.doc.mjs +1 -0
  87. package/assets/docs/theme.doc.dense.mjs +2 -2
  88. package/assets/docs/theme.doc.mjs +1 -1
  89. package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
  90. package/assets/docs/tree/cli-command.doc.mjs +2 -3
  91. package/assets/docs/tree/render-pages.doc.mjs +43 -2
  92. package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
  93. package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
  94. package/assets/docs/use-a-theme.doc.mjs +1 -1
  95. package/assets/docs.generated.json +2 -2
  96. package/assets/self-audit.generated.json +1 -1
  97. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
  98. package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
  99. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
  100. package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
  101. package/assets/templates/components.generated.json +1 -1
  102. package/assets/templates/pages/ai-chat/template.doc.mjs +5 -4
  103. package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
  104. package/assets/templates/pages/blank/template.doc.mjs +1 -1
  105. package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
  106. package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
  107. package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
  108. package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
  109. package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
  110. package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
  111. package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
  112. package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
  113. package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
  114. package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
  115. package/assets/templates/pages/documentation/template.doc.mjs +4 -1
  116. package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
  117. package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
  118. package/assets/templates/pages/editor/template.doc.mjs +2 -8
  119. package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
  120. package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
  121. package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
  122. package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
  123. package/assets/templates/pages/ide/page.tsx +24 -2
  124. package/assets/templates/pages/kanban-board/template.doc.mjs +1 -9
  125. package/assets/templates/pages/library/template.doc.mjs +4 -1
  126. package/assets/templates/pages/login/template.doc.mjs +2 -1
  127. package/assets/templates/pages/login-card/template.doc.mjs +2 -1
  128. package/assets/templates/pages/login-split/template.doc.mjs +2 -1
  129. package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
  130. package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
  131. package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
  132. package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
  133. package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
  134. package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
  135. package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
  136. package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
  137. package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
  138. package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
  139. package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
  140. package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
  141. package/assets/templates/pages/table-page/template.doc.mjs +5 -4
  142. package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
  143. package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -9
  144. package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
  145. package/authoring/debug/parse.d.mts +3 -3
  146. package/authoring/doctypes/_schema.d.mts +3 -3
  147. package/authoring/doctypes/command/command.doc.mjs +1 -1
  148. package/authoring/doctypes/template/template.doc.mjs +1 -1
  149. package/authoring/doctypes/template/type.ts +9 -0
  150. package/clients/cli/bin/astryx.mjs +6 -9
  151. package/clients/cli/build-id.generated.json +1 -1
  152. package/clients/cli/commands/component.doc.mjs +1 -1
  153. package/clients/cli/commands/component.mjs +20 -2
  154. package/clients/cli/commands/docs.doc.mjs +7 -1
  155. package/clients/cli/commands/docs.mjs +3 -1
  156. package/clients/cli/commands/doctor.mjs +22 -14
  157. package/clients/cli/commands/experimental/render.mjs +32 -1
  158. package/clients/cli/commands/experimental/theme.mjs +65 -14
  159. package/clients/cli/commands/experimental-render.doc.mjs +2 -2
  160. package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
  161. package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
  162. package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
  163. package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
  164. package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
  165. package/clients/cli/commands/search.doc.mjs +2 -1
  166. package/clients/cli/commands/search.mjs +6 -1
  167. package/clients/cli/commands/template.mjs +4 -2
  168. package/clients/cli/commands/theme-build.doc.mjs +5 -1
  169. package/clients/cli/commands/theme.doc.mjs +1 -1
  170. package/clients/cli/commands/theme.mjs +153 -5
  171. package/clients/cli/commands/upgrade.mjs +42 -7
  172. package/clients/cli/index.mjs +1 -1
  173. package/clients/cli/lib/component-format.mjs +205 -98
  174. package/clients/cli/lib/hook-format.mjs +9 -19
  175. package/clients/cli/manifest.generated.json +1 -1
  176. package/foundation/agent-docs/agent-docs.mjs +1 -1
  177. package/foundation/config/theme-state.d.mts +13 -1
  178. package/foundation/config/theme-state.mjs +47 -7
  179. package/foundation/discovery/component-discovery.d.mts +11 -0
  180. package/foundation/discovery/component-discovery.mjs +27 -2
  181. package/foundation/discovery/component-replacement.mjs +130 -1
  182. package/foundation/discovery/entry-name-files.d.mts +43 -0
  183. package/foundation/discovery/entry-name-files.mjs +264 -0
  184. package/foundation/discovery/hook-discovery.d.mts +8 -2
  185. package/foundation/discovery/hook-discovery.mjs +20 -7
  186. package/foundation/discovery/package-exports.d.mts +10 -0
  187. package/foundation/discovery/package-exports.mjs +56 -3
  188. package/foundation/discovery/runner-allowlist.d.mts +1 -0
  189. package/foundation/discovery/runner-allowlist.mjs +2 -0
  190. package/foundation/discovery/template-adapter.d.mts +52 -0
  191. package/foundation/discovery/template-adapter.mjs +173 -10
  192. package/foundation/discovery/theme-imports.d.mts +14 -0
  193. package/foundation/discovery/theme-imports.mjs +43 -1
  194. package/foundation/doc-compiler/import.mjs +7 -1
  195. package/foundation/doc-compiler/parse-readable.mjs +4 -1
  196. package/foundation/doc-compiler/stored-docs.d.mts +22 -0
  197. package/foundation/doc-compiler/stored-docs.mjs +49 -0
  198. package/foundation/fs/cache-eviction.d.mts +21 -0
  199. package/foundation/fs/cache-eviction.mjs +73 -0
  200. package/foundation/fs/gather-cache.d.mts +25 -4
  201. package/foundation/fs/gather-cache.mjs +163 -53
  202. package/foundation/fs/jiti-cache.d.mts +19 -1
  203. package/foundation/fs/jiti-cache.mjs +130 -6
  204. package/foundation/fs/kept-tree.d.mts +60 -0
  205. package/foundation/fs/kept-tree.mjs +154 -0
  206. package/foundation/fs/module-loader.d.mts +18 -2
  207. package/foundation/fs/module-loader.mjs +92 -20
  208. package/foundation/identity/provider-identity.d.mts +6 -0
  209. package/foundation/identity/provider-identity.mjs +10 -1
  210. package/foundation/render/embedded-imports.d.mts +5 -4
  211. package/foundation/render/embedded-imports.mjs +7 -16
  212. package/foundation/render/file-header.d.mts +66 -0
  213. package/foundation/render/file-header.mjs +118 -0
  214. package/foundation/render/host-answer.d.mts +18 -0
  215. package/foundation/render/host-answer.mjs +46 -0
  216. package/foundation/render/host-exit.d.mts +1 -0
  217. package/foundation/render/host-exit.mjs +6 -1
  218. package/foundation/render/html-reader.d.mts +55 -2
  219. package/foundation/render/html-reader.mjs +145 -26
  220. package/foundation/render/markup-edits.d.mts +57 -0
  221. package/foundation/render/markup-edits.mjs +182 -0
  222. package/foundation/render/page-host.mjs +29 -12
  223. package/foundation/render/page-worker.mjs +83 -13
  224. package/foundation/render/render-page.d.mts +34 -2
  225. package/foundation/render/render-page.mjs +210 -29
  226. package/foundation/render/source-block.d.mts +73 -6
  227. package/foundation/render/source-block.mjs +170 -19
  228. package/foundation/render/ts-paths.d.mts +53 -6
  229. package/foundation/render/ts-paths.mjs +286 -41
  230. package/foundation/response/error-codes.d.mts +3 -1
  231. package/foundation/response/error-codes.d.ts +1 -0
  232. package/foundation/response/error-codes.doc.mjs +12 -1
  233. package/foundation/response/error-codes.mjs +10 -0
  234. package/foundation/text/string-utils.d.mts +3 -2
  235. package/foundation/text/string-utils.mjs +16 -3
  236. package/foundation/text/suggest-order.d.mts +9 -0
  237. package/foundation/text/suggest-order.mjs +13 -0
  238. package/host/index.mjs +8 -6
  239. package/package.json +6 -6
package/README.md CHANGED
@@ -87,7 +87,7 @@ Options:
87
87
 
88
88
  - `--type <component|hook|doc|template|theme>`: restrict to a single domain (`doc` and `theme` work outside an app too)
89
89
  - `--limit <n>`: cap the number of results (default 20)
90
- - `--verbose`: also print each result's match score and reason
90
+ - `--verbose`: also print each result's match score and reason, and its whole description
91
91
  - `--json`: typed `{ apiVersion, type: 'search', data: { query, matchCount, results } }` envelope — `matchCount` is how many candidates matched in total, `results` the slice `--limit` allowed
92
92
 
93
93
  ## Commands
@@ -221,6 +221,7 @@ if (isError(result)) {
221
221
  | `ERR_FILE_EXISTS` | Refused to overwrite an existing file. |
222
222
  | `ERR_PATH_TRAVERSAL` | A path escaped its allowed root, or a name contained traversal markers. |
223
223
  | `ERR_WRITE_FAILED` | Writing output files failed (and was rolled back). |
224
+ | `ERR_READ_FAILED` | An input file cannot be read: no permission on it or on a folder on its path, a folder or other entry that is not a regular file, or another error from the file system. |
224
225
  | `ERR_THEME_INVALID` | A theme definition or contributed theme descriptor is invalid. |
225
226
  | `ERR_THEME_LOAD` | A theme file could not be loaded / parsed into a defineTheme result. |
226
227
  | `ERR_THEME_STALE` | `theme build --check` found stale or missing outputs: exit 1, in the check result (`code`), not an error. |
@@ -298,7 +299,7 @@ Shape:
298
299
  {
299
300
  "flag": "--props",
300
301
  "type": "boolean",
301
- "description": "Print only the props table",
302
+ "description": "Print only the props list",
302
303
  },
303
304
  ],
304
305
  "json": true,
@@ -543,34 +544,23 @@ Every response has a `type` discriminant. The full set is below (generated from
543
544
 
544
545
  `astryx doctor` runs health checks against your project and environment. It
545
546
  changes nothing in the project; once the per-user cache folder exists, it
546
- keeps its docs checks and integration check results there. Each record uses `[ok]`, `[warn]`, `[fail]`, or `[info]`, and
547
- includes an actionable `fix` when one is available. Field names match the
548
- `--json` keys. The exact checks and values depend on the project; the output
549
- shape is stable:
547
+ keeps its docs checks, its theme-wiring scan and integration check results there. Each check prints on one
548
+ line: its status (`[ok]`, `[warn]`, `[fail]`, or `[info]`), then its id, label and message, and an actionable
549
+ `fix` when one is available. The legend under the heading names each field by its `--json` key. The exact
550
+ checks and values depend on the project; the output shape is stable:
550
551
 
551
552
  ```
552
553
  $ astryx doctor
553
- astryx doctor - diagnosing your setup
554
-
555
- id: node-version
556
- status: [ok]
557
- label: Node.js version
558
- message: Node v24.18.1 meets the minimum (>=22.13.0).
559
-
560
- id: themes
561
- status: [warn]
562
- label: Theme packages
563
- message: No @astryxdesign/theme-* packages are installed.
564
- fix: Install a theme, e.g. `npm install @astryxdesign/theme-neutral`, then import its CSS or set astryx.theme.
554
+ astryx doctor
555
+ Each check: status, id, label, message, fix
565
556
 
557
+ [ok] node-version - Node.js version - Node v24.18.1 meets the minimum (>=22.13.0).
558
+ [warn] themes - Theme packages - No @astryxdesign/theme-* packages are installed. - fix: Install a theme, e.g. `npm
559
+ install @astryxdesign/theme-neutral`, then import its CSS or set astryx.theme.
566
560
  ...
567
561
 
568
562
  summary
569
-
570
- pass: 4
571
- warn: 2
572
- fail: 0
573
- info: 2
563
+ pass: 4, warn: 2, fail: 0, info: 2
574
564
 
575
565
  No failures - but review the [warn] warnings above when you can.
576
566
  ```
@@ -12,6 +12,7 @@
12
12
  * @property {string} category The template's own `Family - Variant` label; empty when it declares none.
13
13
  * @property {string[]} keywords The ideas the page serves, as its own descriptor names them; empty when it declares none.
14
14
  * @property {string} filePath Absolute path to the template source file on disk.
15
+ * @property {string[]} components The components its page composes, read from its source on first use; empty when the source cannot be read.
15
16
  */
16
17
  /**
17
18
  * A component the project can use, as the ranker reads it.
@@ -158,6 +159,10 @@ export type PageTemplate = {
158
159
  * Absolute path to the template source file on disk.
159
160
  */
160
161
  filePath: string;
162
+ /**
163
+ * The components its page composes, read from its source on first use; empty when the source cannot be read.
164
+ */
165
+ components: string[];
161
166
  };
162
167
  /**
163
168
  * A component the project can use, as the ranker reads it.
@@ -28,6 +28,8 @@ import {componentKeywords} from '../search/search.mjs';
28
28
  import {findCoreDir, requireCoreDir} from '../../foundation/fs/paths.mjs';
29
29
  import {persistedDerived} from '../../foundation/fs/gather-cache.mjs';
30
30
  import {analyzeTemplateNeeds} from '../../foundation/discovery/template-needs.mjs';
31
+ import {extractComponents} from '../../foundation/discovery/template-adapter.mjs';
32
+ import {isWordModel} from './kit/word-model.mjs';
31
33
 
32
34
  /**
33
35
  * Runs one build with a single directory listing per path: the kit resolves
@@ -47,6 +49,7 @@ export {withListingCache} from '../../foundation/fs/listing-cache.mjs';
47
49
  * @property {string} category The template's own `Family - Variant` label; empty when it declares none.
48
50
  * @property {string[]} keywords The ideas the page serves, as its own descriptor names them; empty when it declares none.
49
51
  * @property {string} filePath Absolute path to the template source file on disk.
52
+ * @property {string[]} components The components its page composes, read from its source on first use; empty when the source cannot be read.
50
53
  */
51
54
 
52
55
  /**
@@ -99,18 +102,23 @@ export async function loadTemplates(cwd) {
99
102
  }
100
103
  const pages = templates
101
104
  .filter(t => t.type === 'page' && t.isReady !== false)
102
- .map(t => ({
103
- name: t.dirName,
104
- displayName: t.displayName || t.name,
105
- description: t.description || '',
106
- package: t.package ?? '@astryxdesign/core',
107
- category: t.category || '',
108
- keywords: t.keywords ?? [],
109
- filePath: t.filePath,
110
- // The id `template()` resolves back to this entry: an active replacement
111
- // owns the Core id it names, so that id selects it, not its own.
112
- command: `astryx template ${t.replaces ?? t.dirName} --type page`,
113
- }));
105
+ .map(t =>
106
+ withComponents(
107
+ {
108
+ name: t.dirName,
109
+ displayName: t.displayName || t.name,
110
+ description: t.description || '',
111
+ package: t.package ?? '@astryxdesign/core',
112
+ category: t.category || '',
113
+ keywords: t.keywords ?? [],
114
+ filePath: t.filePath,
115
+ // The id `template()` resolves back to this entry: an active
116
+ // replacement owns the Core id it names, so that id selects it.
117
+ command: `astryx template ${t.replaces ?? t.dirName} --type page`,
118
+ },
119
+ t.filePath,
120
+ ),
121
+ );
114
122
  const blocks = templates
115
123
  .filter(t => t.type === 'block')
116
124
  .map(t => ({
@@ -261,28 +269,7 @@ export function loadBlockPicker() {
261
269
  */
262
270
  export function isWeightsFile(file) {
263
271
  const n = Array.isArray(file?.candidates) ? file.candidates.length : 0;
264
- return (
265
- n > 0 &&
266
- Array.isArray(file.tables) &&
267
- file.tables.length > 0 &&
268
- file.tables.every(
269
- (/** @type {any} */ t) =>
270
- Array.isArray(t?.words) &&
271
- Array.isArray(t.rows) &&
272
- t.rows.length === t.words.length &&
273
- t.rows.every(
274
- (/** @type {any} */ r) => typeof r === 'string' && r.length === n,
275
- ) &&
276
- Array.isArray(t.bias) &&
277
- t.bias.length === n &&
278
- t.bias.every((/** @type {any} */ b) => Number.isFinite(b)) &&
279
- Number.isFinite(t.clip) &&
280
- Number.isFinite(t.step),
281
- ) &&
282
- Array.isArray(file.blend) &&
283
- file.blend.length === 3 * (file.tables.length + 1) + 1 &&
284
- file.blend.every((/** @type {any} */ x) => Number.isFinite(x))
285
- );
272
+ return isWordModel(file, n);
286
273
  }
287
274
 
288
275
  /** The ranking coefficients the picker reads. */
@@ -362,3 +349,31 @@ export function templateSetupNotes(template, cwd) {
362
349
  export function requireBuildCoreDir(cwd) {
363
350
  return requireCoreDir(cwd);
364
351
  }
352
+
353
+ /**
354
+ * Adds a lazy, non-enumerable `components` getter to a page entry: the page's
355
+ * source is read and parsed only when a caller reads it, once, so loading the
356
+ * catalog stays cheap and the entry's own fields are unchanged.
357
+ * @template {object} T
358
+ * @param {T} entry
359
+ * @param {string | undefined} filePath
360
+ * @returns {T & {readonly components: string[]}}
361
+ */
362
+ function withComponents(entry, filePath) {
363
+ /** @type {string[] | undefined} */
364
+ let components;
365
+ return /** @type {T & {readonly components: string[]}} */ (
366
+ Object.defineProperty(entry, 'components', {
367
+ get() {
368
+ if (components === undefined) {
369
+ try {
370
+ components = filePath ? extractComponents(filePath) : [];
371
+ } catch {
372
+ components = [];
373
+ }
374
+ }
375
+ return components;
376
+ },
377
+ })
378
+ );
379
+ }
@@ -47,17 +47,19 @@ export function requestSideOf(request: string, weights: BlockPickerWeights): {
47
47
  /**
48
48
  * The picker's match of a request to a docs text: their projections' dot
49
49
  * product plus the lexical weight times their plain overlap (the cosine of
50
- * their word vectors). Build's ranking also reads the overlap alone.
51
- * @param {{vector: Map<number, number>, projected: Float64Array}} request
52
- * @param {{vector: Map<number, number>, projected: Float64Array}} doc
50
+ * their word vectors). The vectors are keyed by vocabulary index (search's
51
+ * fit) or by word (the block picker's overlap, which also counts single
52
+ * words outside the vocabulary).
53
+ * @param {{vector: Map<number | string, number>, projected: Float64Array}} request
54
+ * @param {{vector: Map<number | string, number>, projected: Float64Array}} doc
53
55
  * @param {BlockPickerWeights} weights
54
56
  * @returns {{match: number, overlap: number}}
55
57
  */
56
58
  export function matchOf(request: {
57
- vector: Map<number, number>;
59
+ vector: Map<number | string, number>;
58
60
  projected: Float64Array;
59
61
  }, doc: {
60
- vector: Map<number, number>;
62
+ vector: Map<number | string, number>;
61
63
  projected: Float64Array;
62
64
  }, weights: BlockPickerWeights): {
63
65
  match: number;
@@ -27,7 +27,11 @@
27
27
  * request's vector through the request matrix, against the docs' vector
28
28
  * through the doc matrix, plus the lexical weight times their plain overlap
29
29
  * (cosine), so a block the weights were never fitted with, such as a new one,
30
- * is scored from its docs the same way. The candidates are the best matches,
30
+ * is scored from its docs the same way. The plain overlap also counts single
31
+ * words outside the vocabulary the matrices were fitted with (such a word
32
+ * weighs as much as the vocabulary's rarest word), so a newer block's own
33
+ * words still tie it to a request that uses them. The
34
+ * candidates are the best matches,
31
35
  * build's own blocks, search's first results, and the best plain overlaps.
32
36
  * Each candidate's final score adds the ranking coefficients times: its match
33
37
  * relative to the best, its place among the matches, its lead over the
@@ -197,10 +201,42 @@ function vectorOf(terms, d) {
197
201
  return v;
198
202
  }
199
203
 
204
+ /**
205
+ * A text's sparse unit vector over its words, for the plain overlap:
206
+ * (1 + ln count) x idf. A single word outside the vocabulary weighs as much
207
+ * as the vocabulary's rarest word (no fitted text used it often enough to
208
+ * keep). It counts only when `known` has it: a request word no block's docs
209
+ * use cannot tie the request to any block. A word pair counts only when the
210
+ * vocabulary has it.
211
+ * @param {string[]} terms
212
+ * @param {{index: Map<string, number>, idf: Float64Array, rarest: number}} d
213
+ * @param {Set<string>} [known] the words outside the vocabulary that count; all when absent
214
+ * @returns {Map<string, number>}
215
+ */
216
+ function wordVectorOf(terms, d, known) {
217
+ /** @type {Map<string, number>} */
218
+ const counts = new Map();
219
+ for (const t of terms) counts.set(t, (counts.get(t) ?? 0) + 1);
220
+ /** @type {Map<string, number>} */
221
+ const v = new Map();
222
+ let sq = 0;
223
+ for (const [t, c] of counts) {
224
+ const i = d.index.get(t);
225
+ if (i === undefined && (t.includes('_') || (known && !known.has(t))))
226
+ continue;
227
+ const x = (1 + Math.log(c)) * (i === undefined ? d.rarest : d.idf[i]);
228
+ v.set(t, x);
229
+ sq += x * x;
230
+ }
231
+ const norm = Math.sqrt(sq) || 1;
232
+ for (const [t, x] of v) v.set(t, x / norm);
233
+ return v;
234
+ }
235
+
200
236
  /**
201
237
  * The vocabulary index and idf, read once per weights object, and each matrix
202
238
  * row decoded when a side first uses it.
203
- * @type {WeakMap<BlockPickerWeights, {index: Map<string, number>, idf: Float64Array, rows: Map<string, Int8Array>}>}
239
+ * @type {WeakMap<BlockPickerWeights, {index: Map<string, number>, idf: Float64Array, rarest: number, rows: Map<string, Int8Array>}>}
204
240
  */
205
241
  const vocabularies = new WeakMap();
206
242
 
@@ -208,9 +244,11 @@ const vocabularies = new WeakMap();
208
244
  function vocabularyOf(weights) {
209
245
  let v = vocabularies.get(weights);
210
246
  if (!v) {
247
+ const idf = Float64Array.from(weights.idf, x => x / 1024);
211
248
  v = {
212
249
  index: new Map(weights.vocab.map((w, i) => [w, i])),
213
- idf: Float64Array.from(weights.idf, x => x / 1024),
250
+ idf,
251
+ rarest: idf.reduce((a, x) => Math.max(a, x), 0),
214
252
  rows: new Map(),
215
253
  };
216
254
  vocabularies.set(weights, v);
@@ -292,9 +330,11 @@ export function requestSideOf(request, weights) {
292
330
  /**
293
331
  * The picker's match of a request to a docs text: their projections' dot
294
332
  * product plus the lexical weight times their plain overlap (the cosine of
295
- * their word vectors). Build's ranking also reads the overlap alone.
296
- * @param {{vector: Map<number, number>, projected: Float64Array}} request
297
- * @param {{vector: Map<number, number>, projected: Float64Array}} doc
333
+ * their word vectors). The vectors are keyed by vocabulary index (search's
334
+ * fit) or by word (the block picker's overlap, which also counts single
335
+ * words outside the vocabulary).
336
+ * @param {{vector: Map<number | string, number>, projected: Float64Array}} request
337
+ * @param {{vector: Map<number | string, number>, projected: Float64Array}} doc
298
338
  * @param {BlockPickerWeights} weights
299
339
  * @returns {{match: number, overlap: number}}
300
340
  */
@@ -325,7 +365,7 @@ function componentOf(block, componentNames) {
325
365
  /**
326
366
  * The catalog, read once per weights and catalog contents: each block's
327
367
  * component, docs vector and projection, and each component's name phrases.
328
- * @type {WeakMap<BlockPickerWeights, {key: string, blocks: Map<string, {component: string, vector: Map<number, number>, projected: Float64Array}>, phrases: Map<string, [string[], number][]>}>}
368
+ * @type {WeakMap<BlockPickerWeights, {key: string, blocks: Map<string, {component: string, vector: Map<number, number>, projected: Float64Array, words: Map<string, number>}>, phrases: Map<string, [string[], number][]>}>}
329
369
  */
330
370
  const catalogs = new WeakMap();
331
371
 
@@ -357,19 +397,41 @@ function readCatalog(catalog, components, weights, keep) {
357
397
  return read;
358
398
  }
359
399
 
400
+ /**
401
+ * Every word the catalog's block docs use, per read catalog.
402
+ * @type {WeakMap<object, Set<string>>}
403
+ */
404
+ const docWords = new WeakMap();
405
+
406
+ /**
407
+ * @param {{blocks: Map<string, {words: Map<string, number>}>}} cat
408
+ * @returns {Set<string>}
409
+ */
410
+ function docWordsOf(cat) {
411
+ let words = docWords.get(cat);
412
+ if (!words) {
413
+ words = new Set();
414
+ for (const b of cat.blocks.values())
415
+ for (const t of b.words.keys()) words.add(t);
416
+ docWords.set(cat, words);
417
+ }
418
+ return words;
419
+ }
420
+
360
421
  /**
361
422
  * readCatalog's value, worked out.
362
423
  * @param {string} key
363
424
  * @param {CatalogBlock[]} catalog
364
425
  * @param {CatalogComponent[]} components
365
426
  * @param {BlockPickerWeights} weights
366
- * @returns {{key: string, blocks: Map<string, {component: string, vector: Map<number, number>, projected: Float64Array}>, phrases: Map<string, [string[], number][]>}}
427
+ * @returns {{key: string, blocks: Map<string, {component: string, vector: Map<number, number>, projected: Float64Array, words: Map<string, number>}>, phrases: Map<string, [string[], number][]>}}
367
428
  */
368
429
  function computeCatalog(key, catalog, components, weights) {
369
430
  const names = components.map(c => c.name).sort((a, b) => b.length - a.length);
370
431
  const keywords = new Map(components.map(c => [c.name, c.keywords ?? []]));
371
- /** @type {Map<string, {component: string, vector: Map<number, number>, projected: Float64Array}>} */
432
+ /** @type {Map<string, {component: string, vector: Map<number, number>, projected: Float64Array, words: Map<string, number>}>} */
372
433
  const blocks = new Map();
434
+ const vocabulary = vocabularyOf(weights);
373
435
  for (const b of catalog) {
374
436
  if (blocks.has(b.name)) continue;
375
437
  const component = componentOf(b, names);
@@ -380,7 +442,11 @@ function computeCatalog(key, catalog, components, weights) {
380
442
  wordsOf(component).join(' '),
381
443
  (keywords.get(component) ?? []).join(' '),
382
444
  ].join(' ');
383
- blocks.set(b.name, {component, ...docSideOf(text, weights)});
445
+ blocks.set(b.name, {
446
+ component,
447
+ ...docSideOf(text, weights),
448
+ words: wordVectorOf(termsOf(text), vocabulary),
449
+ });
384
450
  }
385
451
  // A component is named by its own words, or by a keyword at most three
386
452
  // components share (weighted by how few share it).
@@ -412,7 +478,8 @@ function computeCatalog(key, catalog, components, weights) {
412
478
  const CATALOG_CODEC = {
413
479
  /**
414
480
  * What decode can get wrong in a read-back catalog: its key, each block's
415
- * component, and projections of one width with finite numbers.
481
+ * component and word vector, and projections of one width with finite
482
+ * numbers.
416
483
  * @param {any} read
417
484
  */
418
485
  valid: read => {
@@ -422,6 +489,7 @@ const CATALOG_CODEC = {
422
489
  return blocks.every(
423
490
  (/** @type {any} */ b) =>
424
491
  typeof b.component === 'string' &&
492
+ b.words instanceof Map &&
425
493
  b.projected.length > 0 &&
426
494
  b.projected.length === width &&
427
495
  [...b.projected].every(Number.isFinite),
@@ -435,6 +503,7 @@ const CATALOG_CODEC = {
435
503
  b.component,
436
504
  [...b.vector],
437
505
  [...b.projected],
506
+ [...b.words],
438
507
  ]),
439
508
  phrases: [...read.phrases],
440
509
  }),
@@ -447,11 +516,12 @@ const CATALOG_CODEC = {
447
516
  blocks: new Map(
448
517
  stored.blocks.map(
449
518
  (
450
- /** @type {[string, string, [number, number][], number[]]} */ [
519
+ /** @type {[string, string, [number, number][], number[], [string, number][]]} */ [
451
520
  name,
452
521
  component,
453
522
  vector,
454
523
  projected,
524
+ words,
455
525
  ],
456
526
  ) => [
457
527
  name,
@@ -459,6 +529,7 @@ const CATALOG_CODEC = {
459
529
  component,
460
530
  vector: new Map(vector),
461
531
  projected: Float64Array.from(projected),
532
+ words: new Map(words),
462
533
  },
463
534
  ],
464
535
  ),
@@ -540,10 +611,22 @@ export function pickBlocks(request, signals, weights, keep) {
540
611
  if (own.length === 0) return {names: [], entries};
541
612
  const cat = readCatalog(signals.catalog, signals.components, weights, keep);
542
613
  const side = requestSideOf(request, weights);
614
+ const words = wordVectorOf(
615
+ termsOf(request),
616
+ vocabularyOf(weights),
617
+ docWordsOf(cat),
618
+ );
543
619
  /** @type {Map<string, {match: number, overlap: number}>} */
544
620
  const scored = new Map();
545
621
  for (const [name, b] of cat.blocks)
546
- scored.set(name, matchOf(side, b, weights));
622
+ scored.set(
623
+ name,
624
+ matchOf(
625
+ {vector: words, projected: side.projected},
626
+ {vector: b.words, projected: b.projected},
627
+ weights,
628
+ ),
629
+ );
547
630
  const get = (/** @type {string} */ b) =>
548
631
  scored.get(b) ?? {match: -Infinity, overlap: 0};
549
632
  const ranked = [...scored.keys()].sort((a, b) => get(b).match - get(a).match);
@@ -593,8 +676,8 @@ export function pickBlocks(request, signals, weights, keep) {
593
676
  });
594
677
  final.sort((a, b) => b.score - a.score || a.pos - b.pos);
595
678
  const foreign = new Set(signals.foreign);
596
- const words = wordsOf(request);
597
- const keeps = own.map(b => foreign.has(b) || writesName(words, b));
679
+ const requestWords = wordsOf(request);
680
+ const keeps = own.map(b => foreign.has(b) || writesName(requestWords, b));
598
681
  const kept = new Set(own.filter((_, k) => keeps[k]));
599
682
  const order = [
600
683
  ...final.map(x => x.b).filter(b => !kept.has(b) && !foreign.has(b)),
@@ -626,7 +709,7 @@ export function pickBlocks(request, signals, weights, keep) {
626
709
  name,
627
710
  package: block.package ?? '',
628
711
  score: Math.max(1, Math.round(100 * Math.exp(x.score - max))),
629
- reason: reasonFor(request, side, cat.blocks.get(name), weights),
712
+ reason: reasonFor(request, side, words, cat.blocks.get(name), weights),
630
713
  description: block.description ?? '',
631
714
  displayName: block.displayName,
632
715
  kind: 'block',
@@ -640,10 +723,11 @@ export function pickBlocks(request, signals, weights, keep) {
640
723
  * Why a block fits: the request words that tie it to its docs most.
641
724
  * @param {string} request
642
725
  * @param {{vector: Map<number, number>, projected: Float64Array}} side the request's side
643
- * @param {{vector: Map<number, number>, projected: Float64Array} | undefined} block
726
+ * @param {Map<string, number>} words the request's word vector for the plain overlap
727
+ * @param {{projected: Float64Array, words: Map<string, number>} | undefined} block
644
728
  * @param {BlockPickerWeights} weights
645
729
  */
646
- function reasonFor(request, side, block, weights) {
730
+ function reasonFor(request, side, words, block, weights) {
647
731
  if (!block) return 'fits by meaning';
648
732
  const raw =
649
733
  String(request)
@@ -653,17 +737,20 @@ function reasonFor(request, side, block, weights) {
653
737
  const wordOf = new Map();
654
738
  for (const w of raw)
655
739
  if (!STOP.has(w) && !wordOf.has(stemOf(w))) wordOf.set(stemOf(w), w);
740
+ const {index} = vocabularyOf(weights);
656
741
  /** @type {[string, number][]} */
657
742
  const parts = [];
658
- for (const [i, x] of side.vector) {
659
- const term = weights.vocab[i];
743
+ for (const [term, x] of words) {
660
744
  if (term.includes('_')) continue;
661
- const row = rowOf(weights, 'request', i);
662
- let s = 0;
663
- for (let k = 0; k < weights.dims; k++) s += row[k] * block.projected[k];
664
- s =
665
- x * s * weights.requestScale +
666
- weights.lexical * x * (block.vector.get(i) ?? 0);
745
+ let s = weights.lexical * x * (block.words.get(term) ?? 0);
746
+ const i = index.get(term);
747
+ const v = i === undefined ? undefined : side.vector.get(i);
748
+ if (i !== undefined && v !== undefined) {
749
+ const row = rowOf(weights, 'request', i);
750
+ let dot = 0;
751
+ for (let k = 0; k < weights.dims; k++) dot += row[k] * block.projected[k];
752
+ s += v * dot * weights.requestScale;
753
+ }
667
754
  if (s > 0) parts.push([wordOf.get(term) ?? term, s]);
668
755
  }
669
756
  parts.sort((a, b) => b[1] - a[1]);
@@ -20,7 +20,8 @@ export const doc = {
20
20
  'how-to-build-a-page playbook as data: the workflow steps with their ' +
21
21
  'commands, the on-system rules, and related lookups. Called with a query it names the page template to ' +
22
22
  'START from (always one: the page template a ranker built for long descriptions puts first; for a part ' +
23
- 'of a page, the page it names; else the app shell) and the next two templates, ' +
23
+ 'of a page, the page it names; else the app shell, unless the idea reads as a whole page, which starts ' +
24
+ 'from the closest page template) and the next two templates, ' +
24
25
  'and the unified search grouped around it: the other close page templates, drop-in blocks, and ' +
25
26
  'idea-specific components/hooks, plus the always-on frame + foundation. A template carries the page ' +
26
27
  'frame and spacing, so the kit never recommends composing a page from components.',