@astryxdesign/cli 0.7.16 → 0.7.17-canary.g9a957aa
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.
- package/README.md +12 -23
- package/api/build/_adapter.d.mts +5 -0
- package/api/build/_adapter.mjs +49 -34
- package/api/build/blocks/blocks.d.mts +7 -5
- package/api/build/blocks/blocks.mjs +113 -26
- package/api/build/build.doc.mjs +2 -1
- package/api/build/kit/rank.mjs +47 -9
- package/api/build/kit/weights.d.mts +16 -3
- package/api/build/kit/weights.json +1 -1
- package/api/build/kit/weights.mjs +238 -31
- package/api/build/kit/word-model.d.mts +23 -0
- package/api/build/kit/word-model.mjs +46 -0
- package/api/build/kit/words.d.mts +32 -0
- package/api/build/kit/words.mjs +49 -1
- package/api/component/__fixtures__/resolutions/modules.txt +4 -5
- package/api/component/list/list.d.mts +1 -1
- package/api/component/list/list.mjs +48 -5
- package/api/discover/_adapter.d.mts +1 -1
- package/api/docs/_adapter.d.mts +26 -2
- package/api/docs/_adapter.mjs +217 -5
- package/api/docs/docs.d.mts +3 -0
- package/api/docs/docs.doc.mjs +7 -1
- package/api/docs/docs.mjs +12 -0
- package/api/docs/docs.type.d.mts +5 -0
- package/api/docs/docs.type.mjs +2 -0
- package/api/docs/node/node.mjs +1 -1
- package/api/doctor/_adapter.d.mts +27 -5
- package/api/doctor/_adapter.mjs +77 -6
- package/api/doctor/built-checks.mjs +38 -13
- package/api/doctor/doctor.d.mts +4 -3
- package/api/doctor/doctor.mjs +14 -7
- package/api/doctor/theme-checks.mjs +121 -17
- package/api/render/_adapter.mjs +491 -186
- package/api/render/render.doc.mjs +10 -3
- package/api/render/render.type.d.mts +30 -0
- package/api/render/render.type.mjs +19 -0
- package/api/search/ORDERING.md +1 -1
- package/api/search/_candidate-score.mjs +51 -32
- package/api/search/_fit-order.mjs +3 -3
- package/api/search/_result.d.mts +5 -5
- package/api/search/_score.d.mts +6 -1
- package/api/search/_score.mjs +15 -3
- package/api/search/search.d.mts +1 -1
- package/api/theme/_adapter.mjs +10 -0
- package/api/theme/build/build.d.mts +38 -0
- package/api/theme/build/build.mjs +144 -24
- package/api/theme/theme.type.d.mts +10 -0
- package/api/theme/theme.type.mjs +7 -1
- package/api/theme-edit/_adapter.d.mts +35 -0
- package/api/theme-edit/_adapter.mjs +477 -41
- package/api/theme-edit/_check.d.mts +28 -22
- package/api/theme-edit/_check.mjs +1077 -200
- package/api/theme-edit/_compiler.mjs +9 -0
- package/api/theme-edit/_context.mjs +60 -3
- package/api/theme-edit/_css.mjs +21 -1
- package/api/theme-edit/_edit.d.mts +22 -0
- package/api/theme-edit/_edit.mjs +932 -82
- package/api/theme-edit/_how-commands.d.mts +49 -0
- package/api/theme-edit/_how-commands.mjs +386 -1
- package/api/theme-edit/_how-question.d.mts +56 -0
- package/api/theme-edit/_how-question.mjs +3509 -168
- package/api/theme-edit/_how.mjs +104 -4
- package/api/theme-edit/_marks.d.mts +31 -5
- package/api/theme-edit/_marks.mjs +57 -5
- package/api/theme-edit/_normal.d.mts +137 -0
- package/api/theme-edit/_normal.mjs +612 -0
- package/api/theme-edit/_palette.mjs +10 -4
- package/api/theme-edit/_parts.d.mts +4 -0
- package/api/theme-edit/_parts.mjs +28 -2
- package/api/theme-edit/_preview-app.d.mts +7 -1
- package/api/theme-edit/_preview-app.mjs +191 -15
- package/api/theme-edit/_preview.d.mts +64 -8
- package/api/theme-edit/_preview.mjs +401 -83
- package/api/theme-edit/_settings.d.mts +139 -5
- package/api/theme-edit/_settings.mjs +1659 -112
- package/api/theme-edit/_tables.mjs +17 -1
- package/api/theme-edit/theme-edit.type.d.mts +10 -1
- package/api/theme-edit/theme-edit.type.mjs +4 -1
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +2 -2
- package/assets/codemods/run-codemod.mjs +73 -6
- package/assets/codemods/transforms/next/index.mjs +11 -1
- package/assets/codemods/transforms/next/migrate-item-swipe-actions-to-swipe.mjs +394 -0
- package/assets/docs/author-a-theme.doc.mjs +1 -0
- package/assets/docs/theme.doc.dense.mjs +2 -2
- package/assets/docs/theme.doc.mjs +1 -1
- package/assets/docs/tree/check-your-docs.doc.mjs +1 -1
- package/assets/docs/tree/cli-command.doc.mjs +2 -3
- package/assets/docs/tree/render-pages.doc.mjs +43 -2
- package/assets/docs/tree/template-doc-overview.doc.mjs +1 -1
- package/assets/docs/tree/test-in-an-app.doc.mjs +1 -1
- package/assets/docs/use-a-theme.doc.mjs +1 -1
- package/assets/docs.generated.json +2 -2
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/ChatComposerInput/useChatDictationHookUsage.tsx +38 -0
- package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/SyntaxTheme/useSyntaxThemeHookUsage.tsx +36 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/ai-chat/template.doc.mjs +8 -4
- package/assets/templates/pages/ai-chat-landing/template.doc.mjs +2 -1
- package/assets/templates/pages/blank/template.doc.mjs +1 -1
- package/assets/templates/pages/canvas-editor/template.doc.mjs +4 -2
- package/assets/templates/pages/centered-hero/template.doc.mjs +9 -1
- package/assets/templates/pages/classic-gallery/template.doc.mjs +1 -1
- package/assets/templates/pages/contact-form/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard/template.doc.mjs +3 -2
- package/assets/templates/pages/dashboard-alert-rail/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-cohort-funnel/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-composition/template.doc.mjs +2 -1
- package/assets/templates/pages/dashboard-progress/template.doc.mjs +6 -3
- package/assets/templates/pages/detail-page/template.doc.mjs +10 -3
- package/assets/templates/pages/documentation/template.doc.mjs +4 -1
- package/assets/templates/pages/documentation-design/template.doc.mjs +7 -1
- package/assets/templates/pages/documentation-technical/template.doc.mjs +7 -1
- package/assets/templates/pages/editor/template.doc.mjs +1 -7
- package/assets/templates/pages/file-explorer/template.doc.mjs +1 -1
- package/assets/templates/pages/form-two-column/template.doc.mjs +4 -3
- package/assets/templates/pages/form-wizard/template.doc.mjs +5 -3
- package/assets/templates/pages/gallery-hero/template.doc.mjs +4 -2
- package/assets/templates/pages/ide/page.tsx +24 -2
- package/assets/templates/pages/library/template.doc.mjs +4 -1
- package/assets/templates/pages/login/template.doc.mjs +2 -1
- package/assets/templates/pages/login-card/template.doc.mjs +2 -1
- package/assets/templates/pages/login-split/template.doc.mjs +2 -1
- package/assets/templates/pages/login-sso/template.doc.mjs +4 -2
- package/assets/templates/pages/mixed-gallery/template.doc.mjs +2 -1
- package/assets/templates/pages/product-detail/template.doc.mjs +4 -2
- package/assets/templates/pages/product-gallery/template.doc.mjs +2 -1
- package/assets/templates/pages/settings-dialog/template.doc.mjs +2 -1
- package/assets/templates/pages/settings-sidebar/template.doc.mjs +2 -1
- package/assets/templates/pages/shell-nav/template.doc.mjs +4 -2
- package/assets/templates/pages/shell-side-nav/template.doc.mjs +5 -3
- package/assets/templates/pages/shell-top-nav/template.doc.mjs +2 -1
- package/assets/templates/pages/side-gallery/template.doc.mjs +9 -1
- package/assets/templates/pages/table-filter/template.doc.mjs +9 -8
- package/assets/templates/pages/table-grouped/template.doc.mjs +4 -2
- package/assets/templates/pages/table-page/template.doc.mjs +5 -4
- package/assets/templates/pages/table-tree/template.doc.mjs +6 -3
- package/assets/templates/pages/theme-showcase/template.doc.mjs +2 -1
- package/assets/templates/pages/work-item-detail/template.doc.mjs +1 -0
- package/authoring/debug/parse.d.mts +3 -3
- package/authoring/doctypes/_schema.d.mts +3 -3
- package/authoring/doctypes/command/command.doc.mjs +1 -1
- package/authoring/doctypes/template/template.doc.mjs +1 -1
- package/authoring/doctypes/template/type.ts +9 -0
- package/clients/cli/bin/astryx.mjs +6 -9
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/component.doc.mjs +1 -1
- package/clients/cli/commands/component.mjs +20 -2
- package/clients/cli/commands/docs.doc.mjs +7 -1
- package/clients/cli/commands/docs.mjs +3 -1
- package/clients/cli/commands/doctor.mjs +22 -14
- package/clients/cli/commands/experimental/render.mjs +32 -1
- package/clients/cli/commands/experimental/theme.mjs +27 -2
- package/clients/cli/commands/experimental-render.doc.mjs +2 -2
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +3 -3
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +1 -1
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +1 -1
- package/clients/cli/commands/search.doc.mjs +2 -1
- package/clients/cli/commands/search.mjs +6 -1
- package/clients/cli/commands/template.mjs +4 -2
- package/clients/cli/commands/theme-build.doc.mjs +5 -1
- package/clients/cli/commands/theme.doc.mjs +1 -1
- package/clients/cli/commands/theme.mjs +138 -3
- package/clients/cli/commands/upgrade.mjs +42 -7
- package/clients/cli/index.mjs +1 -1
- package/clients/cli/lib/component-format.mjs +205 -98
- package/clients/cli/lib/hook-format.mjs +9 -19
- package/clients/cli/manifest.generated.json +1 -1
- package/foundation/agent-docs/agent-docs.mjs +1 -1
- package/foundation/config/theme-state.d.mts +13 -1
- package/foundation/config/theme-state.mjs +47 -7
- package/foundation/discovery/component-discovery.d.mts +11 -0
- package/foundation/discovery/component-discovery.mjs +27 -2
- package/foundation/discovery/component-replacement.mjs +130 -1
- package/foundation/discovery/entry-name-files.d.mts +43 -0
- package/foundation/discovery/entry-name-files.mjs +264 -0
- package/foundation/discovery/hook-discovery.d.mts +8 -2
- package/foundation/discovery/hook-discovery.mjs +20 -7
- package/foundation/discovery/runner-allowlist.d.mts +1 -0
- package/foundation/discovery/runner-allowlist.mjs +2 -0
- package/foundation/discovery/template-adapter.d.mts +52 -0
- package/foundation/discovery/template-adapter.mjs +169 -0
- package/foundation/discovery/theme-imports.d.mts +14 -0
- package/foundation/discovery/theme-imports.mjs +43 -1
- package/foundation/doc-compiler/import.mjs +7 -1
- package/foundation/doc-compiler/parse-readable.mjs +4 -1
- package/foundation/doc-compiler/stored-docs.d.mts +22 -0
- package/foundation/doc-compiler/stored-docs.mjs +49 -0
- package/foundation/fs/cache-eviction.d.mts +21 -0
- package/foundation/fs/cache-eviction.mjs +73 -0
- package/foundation/fs/gather-cache.d.mts +25 -4
- package/foundation/fs/gather-cache.mjs +138 -52
- package/foundation/fs/jiti-cache.d.mts +19 -1
- package/foundation/fs/jiti-cache.mjs +130 -6
- package/foundation/fs/kept-tree.d.mts +60 -0
- package/foundation/fs/kept-tree.mjs +154 -0
- package/foundation/fs/module-loader.d.mts +18 -2
- package/foundation/fs/module-loader.mjs +92 -20
- package/foundation/identity/provider-identity.d.mts +6 -0
- package/foundation/identity/provider-identity.mjs +10 -1
- package/foundation/render/file-header.d.mts +66 -0
- package/foundation/render/file-header.mjs +118 -0
- package/foundation/render/host-answer.d.mts +18 -0
- package/foundation/render/host-answer.mjs +46 -0
- package/foundation/render/host-exit.d.mts +1 -0
- package/foundation/render/host-exit.mjs +6 -1
- package/foundation/render/html-reader.d.mts +55 -2
- package/foundation/render/html-reader.mjs +145 -26
- package/foundation/render/markup-edits.d.mts +57 -0
- package/foundation/render/markup-edits.mjs +182 -0
- package/foundation/render/page-host.mjs +29 -12
- package/foundation/render/page-worker.mjs +79 -10
- package/foundation/render/render-page.d.mts +34 -2
- package/foundation/render/render-page.mjs +210 -29
- package/foundation/render/source-block.d.mts +73 -6
- package/foundation/render/source-block.mjs +170 -19
- package/foundation/render/ts-paths.d.mts +4 -1
- package/foundation/render/ts-paths.mjs +7 -3
- package/foundation/response/error-codes.d.mts +2 -1
- package/foundation/response/error-codes.doc.mjs +7 -1
- package/foundation/response/error-codes.mjs +7 -0
- package/foundation/text/string-utils.d.mts +3 -2
- package/foundation/text/string-utils.mjs +16 -3
- package/foundation/text/suggest-order.d.mts +9 -0
- package/foundation/text/suggest-order.mjs +13 -0
- 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
|
|
@@ -298,7 +298,7 @@ Shape:
|
|
|
298
298
|
{
|
|
299
299
|
"flag": "--props",
|
|
300
300
|
"type": "boolean",
|
|
301
|
-
"description": "Print only the props
|
|
301
|
+
"description": "Print only the props list",
|
|
302
302
|
},
|
|
303
303
|
],
|
|
304
304
|
"json": true,
|
|
@@ -543,34 +543,23 @@ Every response has a `type` discriminant. The full set is below (generated from
|
|
|
543
543
|
|
|
544
544
|
`astryx doctor` runs health checks against your project and environment. It
|
|
545
545
|
changes nothing in the project; once the per-user cache folder exists, it
|
|
546
|
-
keeps its docs checks and integration check results there. Each
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
shape is stable:
|
|
546
|
+
keeps its docs checks, its theme-wiring scan and integration check results there. Each check prints on one
|
|
547
|
+
line: its status (`[ok]`, `[warn]`, `[fail]`, or `[info]`), then its id, label and message, and an actionable
|
|
548
|
+
`fix` when one is available. The legend under the heading names each field by its `--json` key. The exact
|
|
549
|
+
checks and values depend on the project; the output shape is stable:
|
|
550
550
|
|
|
551
551
|
```
|
|
552
552
|
$ astryx doctor
|
|
553
|
-
astryx doctor
|
|
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.
|
|
553
|
+
astryx doctor
|
|
554
|
+
Each check: status, id, label, message, fix
|
|
565
555
|
|
|
556
|
+
[ok] node-version - Node.js version - Node v24.18.1 meets the minimum (>=22.13.0).
|
|
557
|
+
[warn] themes - Theme packages - No @astryxdesign/theme-* packages are installed. - fix: Install a theme, e.g. `npm
|
|
558
|
+
install @astryxdesign/theme-neutral`, then import its CSS or set astryx.theme.
|
|
566
559
|
...
|
|
567
560
|
|
|
568
561
|
summary
|
|
569
|
-
|
|
570
|
-
pass: 4
|
|
571
|
-
warn: 2
|
|
572
|
-
fail: 0
|
|
573
|
-
info: 2
|
|
562
|
+
pass: 4, warn: 2, fail: 0, info: 2
|
|
574
563
|
|
|
575
564
|
No failures - but review the [warn] warnings above when you can.
|
|
576
565
|
```
|
package/api/build/_adapter.d.mts
CHANGED
|
@@ -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.
|
package/api/build/_adapter.mjs
CHANGED
|
@@ -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
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
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).
|
|
51
|
-
*
|
|
52
|
-
*
|
|
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
|
|
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
|
|
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).
|
|
296
|
-
*
|
|
297
|
-
*
|
|
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, {
|
|
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
|
|
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(
|
|
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
|
|
597
|
-
const keeps = own.map(b => foreign.has(b) || writesName(
|
|
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 {
|
|
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 [
|
|
659
|
-
const term = weights.vocab[i];
|
|
743
|
+
for (const [term, x] of words) {
|
|
660
744
|
if (term.includes('_')) continue;
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
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]);
|
package/api/build/build.doc.mjs
CHANGED
|
@@ -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
|
|
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.',
|
package/api/build/kit/rank.mjs
CHANGED
|
@@ -56,6 +56,8 @@ import {
|
|
|
56
56
|
normalized,
|
|
57
57
|
SYNONYMS_OF,
|
|
58
58
|
familyWordsOf,
|
|
59
|
+
keywordUnits,
|
|
60
|
+
keywordWordsFor,
|
|
59
61
|
} from './words.mjs';
|
|
60
62
|
|
|
61
63
|
/**
|
|
@@ -136,14 +138,17 @@ const START_TERMS = 2;
|
|
|
136
138
|
export function rankPages(query, pages) {
|
|
137
139
|
const docs = pages.map(page => {
|
|
138
140
|
const [family = '', ...variantParts] = page.category.split(' - ');
|
|
141
|
+
const keywords = page.keywords ?? [];
|
|
142
|
+
const phrases = keywordUnits(keywords, terms).filter(u => u.includes(' '));
|
|
139
143
|
/** @type {Record<keyof typeof FIELD_WEIGHT, string>} */
|
|
140
144
|
const fields = {
|
|
141
145
|
id: page.name.replace(/-/g, ' '),
|
|
142
146
|
variant: variantParts.join(' - '),
|
|
143
147
|
family,
|
|
144
148
|
display: page.displayName,
|
|
145
|
-
//
|
|
146
|
-
|
|
149
|
+
// A one-word keyword is a term like any other. A longer one gives its
|
|
150
|
+
// words only to an idea that names the whole phrase (keywordWordsFor).
|
|
151
|
+
keywords: keywords.filter(k => terms(k).length === 1).join(', '),
|
|
147
152
|
description: page.description,
|
|
148
153
|
};
|
|
149
154
|
/** @type {Map<string, number>} */
|
|
@@ -158,7 +163,9 @@ export function rankPages(query, pages) {
|
|
|
158
163
|
page,
|
|
159
164
|
family: family.trim(),
|
|
160
165
|
bag,
|
|
161
|
-
|
|
166
|
+
phrases,
|
|
167
|
+
// Each keyword is its own phrase: a pair never spans two of them.
|
|
168
|
+
pairs: pairsOf([...Object.values(fields), keywords.join(', ')]),
|
|
162
169
|
};
|
|
163
170
|
});
|
|
164
171
|
|
|
@@ -169,18 +176,34 @@ export function rankPages(query, pages) {
|
|
|
169
176
|
for (const {bag} of docs)
|
|
170
177
|
for (const t of bag.keys()) df.set(t, (df.get(t) ?? 0) + 1);
|
|
171
178
|
const n = docs.length;
|
|
172
|
-
/**
|
|
173
|
-
|
|
174
|
-
|
|
179
|
+
/**
|
|
180
|
+
* @param {string} t
|
|
181
|
+
* @param {number} [own] 1 when the template scored carries `t` only through
|
|
182
|
+
* a phrase keyword the idea names, so it counts among the templates that
|
|
183
|
+
* carry `t`
|
|
184
|
+
*/
|
|
185
|
+
const idf = (t, own = 0) => {
|
|
186
|
+
const d = (df.get(t) ?? 0) + own;
|
|
175
187
|
return d ? Math.log(1 + (n - d + 0.5) / (d + 0.5)) : 0;
|
|
176
188
|
};
|
|
177
189
|
|
|
190
|
+
// How many templates of each family carry each term, for how typical of its
|
|
191
|
+
// family a template's descriptor is.
|
|
192
|
+
/** @type {Map<string, Map<string, number>>} */
|
|
193
|
+
const familyDf = new Map();
|
|
194
|
+
for (const {family, bag} of docs) {
|
|
195
|
+
const counts = familyDf.get(family) ?? new Map();
|
|
196
|
+
familyDf.set(family, counts);
|
|
197
|
+
for (const t of bag.keys()) counts.set(t, (counts.get(t) ?? 0) + 1);
|
|
198
|
+
}
|
|
199
|
+
|
|
178
200
|
const familyOfWord = familyWordsOf(pages);
|
|
179
201
|
/** @param {string[]} ts */
|
|
180
202
|
const familiesIn = ts =>
|
|
181
203
|
new Set(ts.filter(t => familyOfWord.has(t)).map(t => familyOfWord.get(t)));
|
|
182
204
|
|
|
183
|
-
const
|
|
205
|
+
const queryWords = terms(query);
|
|
206
|
+
const queryTerms = [...new Set(queryWords)];
|
|
184
207
|
const headFamilies = familiesIn(
|
|
185
208
|
terms(String(query).split(HEAD_END)[0] ?? ''),
|
|
186
209
|
);
|
|
@@ -189,7 +212,15 @@ export function rankPages(query, pages) {
|
|
|
189
212
|
const containers = containersOf(query);
|
|
190
213
|
|
|
191
214
|
const ranked = docs
|
|
192
|
-
.map(({page, family, bag, pairs}) => {
|
|
215
|
+
.map(({page, family, bag, phrases, pairs}) => {
|
|
216
|
+
// The words of the phrase keywords this idea names count as keywords.
|
|
217
|
+
const phraseWords = new Set(keywordWordsFor(phrases, queryWords));
|
|
218
|
+
/** @param {string} t */
|
|
219
|
+
const weightOf = t =>
|
|
220
|
+
Math.max(
|
|
221
|
+
bag.get(t) ?? 0,
|
|
222
|
+
phraseWords.has(t) ? FIELD_WEIGHT.keywords : 0,
|
|
223
|
+
);
|
|
193
224
|
let score = 0;
|
|
194
225
|
let hits = 0;
|
|
195
226
|
let containerMatched = false;
|
|
@@ -202,7 +233,10 @@ export function rankPages(query, pages) {
|
|
|
202
233
|
const discounted =
|
|
203
234
|
modified !== undefined &&
|
|
204
235
|
![...modified].some(n => pairs.has(`${t} ${n}`));
|
|
205
|
-
const direct =
|
|
236
|
+
const direct =
|
|
237
|
+
idf(t, bag.has(t) ? 0 : Number(phraseWords.has(t))) *
|
|
238
|
+
weightOf(t) *
|
|
239
|
+
(discounted ? MODIFIER : 1);
|
|
206
240
|
let viaSynonym = 0;
|
|
207
241
|
for (const s of SYNONYMS_OF.get(t) ?? []) {
|
|
208
242
|
if (queryTerms.includes(s)) continue;
|
|
@@ -226,8 +260,12 @@ export function rankPages(query, pages) {
|
|
|
226
260
|
else if (familyNamed) score += BODY_FAMILY;
|
|
227
261
|
const base = normalized(page.name) === normalized(family);
|
|
228
262
|
if (familyNamed && base) score += FAMILY_BASE;
|
|
263
|
+
const inFamily = familyDf.get(family);
|
|
264
|
+
let shared = 0;
|
|
265
|
+
for (const t of bag.keys()) shared += (inFamily?.get(t) ?? 1) - 1;
|
|
229
266
|
return {
|
|
230
267
|
name: page.name,
|
|
268
|
+
typical: bag.size ? shared / bag.size : 0,
|
|
231
269
|
score,
|
|
232
270
|
hits,
|
|
233
271
|
familyNamed,
|