@arkitektio/bsx 5.0.0 → 6.0.0

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 (220) hide show
  1. package/README.md +2 -2
  2. package/dist/bsx/analyze.d.ts +6 -5
  3. package/dist/bsx/analyze.d.ts.map +1 -1
  4. package/dist/bsx/analyze.js +12 -10
  5. package/dist/bsx/analyze.js.map +1 -1
  6. package/dist/bsx/declared.d.ts +111 -0
  7. package/dist/bsx/declared.d.ts.map +1 -0
  8. package/dist/bsx/declared.js +311 -0
  9. package/dist/bsx/declared.js.map +1 -0
  10. package/dist/bsx/index.d.ts +1 -0
  11. package/dist/bsx/index.d.ts.map +1 -1
  12. package/dist/bsx/index.js +1 -0
  13. package/dist/bsx/index.js.map +1 -1
  14. package/dist/bsx/infer.d.ts +40 -17
  15. package/dist/bsx/infer.d.ts.map +1 -1
  16. package/dist/bsx/infer.js +105 -49
  17. package/dist/bsx/infer.js.map +1 -1
  18. package/dist/bsx/language.d.ts +6 -2
  19. package/dist/bsx/language.d.ts.map +1 -1
  20. package/dist/bsx/language.js +65 -21
  21. package/dist/bsx/language.js.map +1 -1
  22. package/dist/bsx/parse.d.ts +6 -1
  23. package/dist/bsx/parse.d.ts.map +1 -1
  24. package/dist/bsx/parse.js +32 -3
  25. package/dist/bsx/parse.js.map +1 -1
  26. package/dist/bsx/print.d.ts.map +1 -1
  27. package/dist/bsx/print.js +3 -0
  28. package/dist/bsx/print.js.map +1 -1
  29. package/dist/bsx/references.d.ts +45 -51
  30. package/dist/bsx/references.d.ts.map +1 -1
  31. package/dist/bsx/references.js +265 -138
  32. package/dist/bsx/references.js.map +1 -1
  33. package/dist/catalogs/html.d.ts +2 -2
  34. package/dist/catalogs/index.d.ts +49 -71
  35. package/dist/catalogs/index.d.ts.map +1 -1
  36. package/dist/catalogs/index.js +1 -1
  37. package/dist/catalogs/logo.d.ts +14 -28
  38. package/dist/catalogs/logo.d.ts.map +1 -1
  39. package/dist/catalogs/logo.js +1 -2
  40. package/dist/catalogs/logo.js.map +1 -1
  41. package/dist/catalogs/parts/data.d.ts +1 -1
  42. package/dist/catalogs/parts/feedback.d.ts +3 -3
  43. package/dist/catalogs/parts/forms.d.ts +4 -4
  44. package/dist/catalogs/parts/html.d.ts +2 -2
  45. package/dist/catalogs/parts/icons.d.ts +2 -2
  46. package/dist/catalogs/parts/index.d.ts +35 -57
  47. package/dist/catalogs/parts/index.d.ts.map +1 -1
  48. package/dist/catalogs/parts/logo.d.ts +10 -25
  49. package/dist/catalogs/parts/logo.d.ts.map +1 -1
  50. package/dist/catalogs/parts/logo.js +12 -14
  51. package/dist/catalogs/parts/logo.js.map +1 -1
  52. package/dist/catalogs/parts/navigation.d.ts +4 -4
  53. package/dist/catalogs/parts/overlays.d.ts +11 -11
  54. package/dist/catalogs/parts/run.d.ts +2 -10
  55. package/dist/catalogs/parts/run.d.ts.map +1 -1
  56. package/dist/catalogs/parts/scene.d.ts +7 -15
  57. package/dist/catalogs/parts/scene.d.ts.map +1 -1
  58. package/dist/catalogs/parts/scene.js +1 -5
  59. package/dist/catalogs/parts/scene.js.map +1 -1
  60. package/dist/catalogs/parts/typography.d.ts +3 -3
  61. package/dist/catalogs/props.d.ts +2 -2
  62. package/dist/catalogs/scene.d.ts +5 -13
  63. package/dist/catalogs/scene.d.ts.map +1 -1
  64. package/dist/catalogs/shadcn.d.ts +11 -11
  65. package/dist/catalogs/ui.d.ts +16 -16
  66. package/dist/logo/actionLogoSource.d.ts +3 -3
  67. package/dist/logo/actionLogoSource.d.ts.map +1 -1
  68. package/dist/logo/actionLogoSource.js +4 -4
  69. package/dist/logo/actionLogoSource.js.map +1 -1
  70. package/dist/renderer/BlokRenderer.d.ts +10 -6
  71. package/dist/renderer/BlokRenderer.d.ts.map +1 -1
  72. package/dist/renderer/BlokRenderer.js +23 -9
  73. package/dist/renderer/BlokRenderer.js.map +1 -1
  74. package/dist/renderer/baseCatalog.d.ts +3 -3
  75. package/dist/renderer/baseCatalog.js +2 -2
  76. package/dist/renderer/bloks/control.d.ts +18 -2
  77. package/dist/renderer/bloks/control.d.ts.map +1 -1
  78. package/dist/renderer/bloks/control.js +85 -14
  79. package/dist/renderer/bloks/control.js.map +1 -1
  80. package/dist/renderer/bloks/data.d.ts +1 -1
  81. package/dist/renderer/bloks/feedback.d.ts +3 -3
  82. package/dist/renderer/bloks/forms.d.ts +4 -4
  83. package/dist/renderer/bloks/html.d.ts +2 -2
  84. package/dist/renderer/bloks/icons.d.ts +1 -1
  85. package/dist/renderer/bloks/index.d.ts +30 -37
  86. package/dist/renderer/bloks/index.d.ts.map +1 -1
  87. package/dist/renderer/bloks/logo/index.d.ts +1 -8
  88. package/dist/renderer/bloks/logo/index.d.ts.map +1 -1
  89. package/dist/renderer/bloks/logo/index.js +1 -2
  90. package/dist/renderer/bloks/logo/index.js.map +1 -1
  91. package/dist/renderer/bloks/logo/logo.fixture.json +266 -250
  92. package/dist/renderer/bloks/navigation.d.ts +4 -4
  93. package/dist/renderer/bloks/overlays.d.ts +11 -11
  94. package/dist/renderer/bloks/run/components.d.ts +1 -5
  95. package/dist/renderer/bloks/run/components.d.ts.map +1 -1
  96. package/dist/renderer/bloks/scene/index.d.ts +4 -8
  97. package/dist/renderer/bloks/scene/index.d.ts.map +1 -1
  98. package/dist/renderer/bloks/scene/interaction.d.ts.map +1 -1
  99. package/dist/renderer/bloks/scene/interaction.js +3 -2
  100. package/dist/renderer/bloks/scene/interaction.js.map +1 -1
  101. package/dist/renderer/bloks/shared/props.d.ts +2 -2
  102. package/dist/renderer/bloks/typography.d.ts +3 -3
  103. package/dist/renderer/declarations.d.ts +40 -33
  104. package/dist/renderer/declarations.d.ts.map +1 -1
  105. package/dist/renderer/declarations.js +41 -56
  106. package/dist/renderer/declarations.js.map +1 -1
  107. package/dist/renderer/declare.js +4 -4
  108. package/dist/renderer/declare.js.map +1 -1
  109. package/dist/renderer/persist.d.ts +22 -13
  110. package/dist/renderer/persist.d.ts.map +1 -1
  111. package/dist/renderer/persist.js +40 -21
  112. package/dist/renderer/persist.js.map +1 -1
  113. package/dist/renderer/runtime/catalog.d.ts +4 -4
  114. package/dist/renderer/runtime/catalog.js +3 -3
  115. package/dist/renderer/runtime/components.d.ts +4 -4
  116. package/dist/renderer/runtime/components.d.ts.map +1 -1
  117. package/dist/renderer/runtime/components.js +5 -5
  118. package/dist/renderer/runtime/components.js.map +1 -1
  119. package/dist/renderer/runtime/context.d.ts +5 -5
  120. package/dist/renderer/runtime/context.d.ts.map +1 -1
  121. package/dist/renderer/runtime/context.js +11 -5
  122. package/dist/renderer/runtime/context.js.map +1 -1
  123. package/dist/renderer/runtime/describe.d.ts +3 -3
  124. package/dist/renderer/runtime/describe.d.ts.map +1 -1
  125. package/dist/renderer/runtime/describe.js +2 -2
  126. package/dist/renderer/runtime/describe.js.map +1 -1
  127. package/dist/renderer/runtime/hooks.d.ts.map +1 -1
  128. package/dist/renderer/runtime/hooks.js +8 -4
  129. package/dist/renderer/runtime/hooks.js.map +1 -1
  130. package/dist/renderer/runtime/index.d.ts +1 -0
  131. package/dist/renderer/runtime/index.d.ts.map +1 -1
  132. package/dist/renderer/runtime/index.js +1 -0
  133. package/dist/renderer/runtime/index.js.map +1 -1
  134. package/dist/renderer/runtime/locals.d.ts +57 -0
  135. package/dist/renderer/runtime/locals.d.ts.map +1 -0
  136. package/dist/renderer/runtime/locals.js +87 -0
  137. package/dist/renderer/runtime/locals.js.map +1 -0
  138. package/dist/renderer/runtime/scope.d.ts +14 -0
  139. package/dist/renderer/runtime/scope.d.ts.map +1 -1
  140. package/dist/renderer/runtime/scope.js +25 -3
  141. package/dist/renderer/runtime/scope.js.map +1 -1
  142. package/dist/renderer/runtime/tree.d.ts.map +1 -1
  143. package/dist/renderer/runtime/tree.js +7 -1
  144. package/dist/renderer/runtime/tree.js.map +1 -1
  145. package/dist/renderer/runtime/types.d.ts +8 -3
  146. package/dist/renderer/runtime/types.d.ts.map +1 -1
  147. package/dist/shadcn.d.ts +36 -43
  148. package/dist/shadcn.d.ts.map +1 -1
  149. package/dist/shadcn.js +1 -2
  150. package/dist/shadcn.js.map +1 -1
  151. package/dist/spec/bsx_v2.json +11067 -6321
  152. package/dist/spec/catalogs/{base@3.json → base@4.json} +434 -288
  153. package/dist/spec/catalogs/{base@3.json.js → base@4.json.js} +434 -288
  154. package/dist/spec/catalogs/orkestrator@1.json +10 -10
  155. package/dist/spec/catalogs/orkestrator@1.json.js +10 -10
  156. package/dist/spec/catalogs.d.ts +4 -4
  157. package/dist/spec/catalogs.d.ts.map +1 -1
  158. package/dist/spec/catalogs.js +7 -7
  159. package/dist/spec/catalogs.js.map +1 -1
  160. package/dist/spec/vocabulary.d.ts +25 -4
  161. package/dist/spec/vocabulary.d.ts.map +1 -1
  162. package/dist/spec/vocabulary.js +50 -5
  163. package/dist/spec/vocabulary.js.map +1 -1
  164. package/dist/spec/vocabulary.json +58 -32
  165. package/dist/spec/vocabulary.json.js +58 -32
  166. package/package.json +1 -1
  167. package/src/bsx/analyze.ts +14 -12
  168. package/src/bsx/bsx.test.ts +4 -4
  169. package/src/bsx/declared.ts +346 -0
  170. package/src/bsx/index.ts +1 -0
  171. package/src/bsx/infer.test.ts +42 -32
  172. package/src/bsx/infer.ts +128 -71
  173. package/src/bsx/language.test.ts +37 -25
  174. package/src/bsx/language.ts +72 -25
  175. package/src/bsx/parse.ts +34 -3
  176. package/src/bsx/print.ts +3 -0
  177. package/src/bsx/references.test.ts +82 -81
  178. package/src/bsx/references.ts +313 -178
  179. package/src/catalogs/catalogs.test.ts +3 -2
  180. package/src/catalogs/index.ts +1 -1
  181. package/src/catalogs/logo.ts +1 -2
  182. package/src/catalogs/parts/logo.ts +13 -17
  183. package/src/catalogs/parts/scene.ts +1 -7
  184. package/src/logo/actionLogoSource.ts +4 -4
  185. package/src/renderer/BlokComponentHost.test.tsx +5 -5
  186. package/src/renderer/BlokRenderer.tsx +46 -14
  187. package/src/renderer/addBlokComponents.test.tsx +1 -1
  188. package/src/renderer/baseCatalog.ts +2 -2
  189. package/src/renderer/bloks/control.tsx +119 -16
  190. package/src/renderer/bloks/logo/index.tsx +2 -3
  191. package/src/renderer/bloks/logo/logo.fixture.json +266 -250
  192. package/src/renderer/bloks/logo/logo.test.tsx +18 -7
  193. package/src/renderer/bloks/run/space.test.tsx +12 -4
  194. package/src/renderer/bloks/scene/interaction.tsx +11 -6
  195. package/src/renderer/bloks/scene/transform.test.ts +8 -7
  196. package/src/renderer/declarations.test.ts +38 -51
  197. package/src/renderer/declarations.ts +67 -88
  198. package/src/renderer/declare.test.ts +4 -4
  199. package/src/renderer/declare.ts +4 -4
  200. package/src/renderer/persist.test.ts +70 -49
  201. package/src/renderer/persist.ts +57 -34
  202. package/src/renderer/runtime/catalog.test.ts +2 -2
  203. package/src/renderer/runtime/catalog.ts +4 -4
  204. package/src/renderer/runtime/components.tsx +8 -8
  205. package/src/renderer/runtime/context.tsx +17 -10
  206. package/src/renderer/runtime/describe.ts +5 -5
  207. package/src/renderer/runtime/hooks.ts +6 -4
  208. package/src/renderer/runtime/index.ts +1 -0
  209. package/src/renderer/runtime/locals.tsx +105 -0
  210. package/src/renderer/runtime/scope.tsx +31 -3
  211. package/src/renderer/runtime/tree.tsx +16 -4
  212. package/src/renderer/runtime/types.ts +8 -3
  213. package/src/shadcn.ts +1 -2
  214. package/src/spec/bsx_v2.json +11067 -6321
  215. package/src/spec/catalogs/{base@3.json → base@4.json} +434 -288
  216. package/src/spec/catalogs/orkestrator@1.json +10 -10
  217. package/src/spec/catalogs.ts +8 -8
  218. package/src/spec/spec.test.ts +19 -16
  219. package/src/spec/vocabulary.json +58 -32
  220. package/src/spec/vocabulary.ts +69 -10
@@ -31,6 +31,12 @@ const mount = (canSave: boolean, room = 'room:lab') =>
31
31
  </BlokSpaceHostProvider>,
32
32
  );
33
33
 
34
+
35
+ /** The props of an action's panel `<Blok>`. */
36
+ const PANEL = [
37
+ {key: 'kind', static_value: 'action'},
38
+ {key: 'context', static_value: 'panel'},
39
+ ];
34
40
  describe('a space shown on its own', () => {
35
41
  it('opens a scene of its own, with Arrange where the host keeps poses', () => {
36
42
  mount(true);
@@ -52,7 +58,7 @@ describe('a space shown on its own', () => {
52
58
 
53
59
  describe('the argument form an assign blok places', () => {
54
60
  it('draws what the host gives for <args />, and nothing without a host', () => {
55
- const tree = [{id: 'root', component: 'Html', children: [{id: 'form', component: 'args'}]}];
61
+ const tree = [{id: 'root', component: 'Blok', props: PANEL, children: [{id: 'form', component: 'args'}]}];
56
62
  const run: BlokRunHost = {agentsFor: () => [], renderAsk: () => null, renderArgs: () => <div data-testid="args-form" />};
57
63
  const hosted = render(
58
64
  <BlokRunHostProvider value={run}>
@@ -68,7 +74,7 @@ describe('the argument form an assign blok places', () => {
68
74
 
69
75
  describe('the time slider a task blok places', () => {
70
76
  it('draws what the host gives for <timeline />, and nothing without a host', () => {
71
- const tree = [{id: 'root', component: 'Html', children: [{id: 'slider', component: 'timeline'}]}];
77
+ const tree = [{id: 'root', component: 'Blok', props: PANEL, children: [{id: 'slider', component: 'timeline'}]}];
72
78
  const run: BlokRunHost = {agentsFor: () => [], renderAsk: () => null, renderTimeline: () => <div data-testid="timeline" />};
73
79
  const hosted = render(
74
80
  <BlokRunHostProvider value={run}>
@@ -87,7 +93,8 @@ describe('what an assign blok places one by one', () => {
87
93
  const tree = [
88
94
  {
89
95
  id: 'root',
90
- component: 'Html',
96
+ component: 'Blok',
97
+ props: PANEL,
91
98
  children: [
92
99
  {id: 'one', component: 'arg', props: [{key: 'key', static_value: 'exposure'}]},
93
100
  {id: 'rest', component: 'args', props: [{key: 'omit', static_value: 'gain offset'}, {key: 'given', static_value: 'show'}]},
@@ -122,7 +129,8 @@ describe('what a task blok places of its run', () => {
122
129
  const tree = [
123
130
  {
124
131
  id: 'root',
125
- component: 'Html',
132
+ component: 'Blok',
133
+ props: PANEL,
126
134
  children: [
127
135
  {id: 'status', component: 'status'},
128
136
  {id: 'result', component: 'returns', props: [{key: 'only', static_value: ['image']}]},
@@ -2,12 +2,14 @@ import * as React from 'react';
2
2
  import type * as THREE from 'three';
3
3
  import {onClickSchema, popoutSchema, type sceneContracts} from '../../../catalogs/parts/scene';
4
4
  import {
5
+ BlokItemKeysBridge,
5
6
  BlokRuntimeProvider,
6
7
  BlokScopeProvider,
7
8
  BlokTreeProvider,
8
9
  useBlok,
9
10
  useBlokRuntimeStoreApi,
10
11
  useBlokScope,
12
+ useBlokItemKeys,
11
13
  useBlokTree,
12
14
  useEventAction,
13
15
  useValue,
@@ -129,21 +131,24 @@ export const PopoutRender: BlokRender<Contracts['Popout']> = ({component, buildC
129
131
  // So are the blok's runtime and tree: in a scene shared by several bloks
130
132
  // that half is outside each of them.
131
133
  const scope = useBlokScope();
134
+ const itemKeys = useBlokItemKeys();
132
135
  const runtime = useBlokRuntimeStoreApi();
133
136
  const tree = useBlokTree();
134
137
  const content = React.useMemo(
135
138
  () => (
136
139
  <BlokRuntimeProvider store={runtime}>
137
140
  <BlokTreeProvider tree={tree}>
138
- <BlokScopeProvider aliases={scope.aliases} values={scope.values} basePath={scope.basePath}>
139
- {(component.children ?? []).map(child => (
140
- <React.Fragment key={child.id}>{buildChild(child.id)}</React.Fragment>
141
- ))}
142
- </BlokScopeProvider>
141
+ <BlokItemKeysBridge keys={itemKeys}>
142
+ <BlokScopeProvider aliases={scope.aliases} values={scope.values} basePath={scope.basePath} defaults={scope.defaults}>
143
+ {(component.children ?? []).map(child => (
144
+ <React.Fragment key={child.id}>{buildChild(child.id)}</React.Fragment>
145
+ ))}
146
+ </BlokScopeProvider>
147
+ </BlokItemKeysBridge>
143
148
  </BlokTreeProvider>
144
149
  </BlokRuntimeProvider>
145
150
  ),
146
- [buildChild, component, runtime, scope, tree],
151
+ [buildChild, component, itemKeys, runtime, scope, tree],
147
152
  );
148
153
  useSceneOverlay({
149
154
  visible: Boolean(node) && visible,
@@ -1,18 +1,17 @@
1
1
  // @vitest-environment jsdom
2
2
  import { describe, expect, it } from "vitest";
3
3
  import { analyzeBsx } from "../../../bsx/analyze";
4
- import { emptyBlokScope } from "../../../bsx/references";
4
+ import { mountedBlokScope } from "../../../bsx/references";
5
5
 
6
- const analyze = (source: string) =>
7
- analyzeBsx(source, {
8
- scope: { ...emptyBlokScope("agent", "SCENE"), locals: [{ key: "pose", default: null, persist: true }] },
9
- });
6
+ /** A scene blok that declares the local `pose`, around `body` (a `<Scene>`). */
7
+ const analyze = (body: string) =>
8
+ analyzeBsx(`<Blok kind="agent" context="scene"><local name="pose" persist />${body}</Blok>`, { scope: mountedBlokScope() });
10
9
 
11
10
  describe("Transform", () => {
12
11
  it("places what it holds in a scene, bound to a local the gizmo writes", () => {
13
12
  const { tree, markers } = analyze('<Scene><Transform bind={pose} mode="rotate"><Transform /></Transform></Scene>');
14
13
  expect(markers).toEqual([]);
15
- expect(tree?.children?.[0]?.component).toBe("Transform");
14
+ expect(tree?.children?.[1]?.children?.[0]?.component).toBe("Transform");
16
15
  });
17
16
 
18
17
  it("keeps its own pose by id when it persists", () => {
@@ -21,6 +20,8 @@ describe("Transform", () => {
21
20
  });
22
21
 
23
22
  it("stands only in a scene", () => {
24
- expect(analyzeBsx('<Html><Transform bind={pose} /></Html>', { scope: { ...emptyBlokScope("agent", "HTML"), locals: [{ key: "pose" }] } }).markers.length).toBeGreaterThan(0);
23
+ expect(
24
+ analyzeBsx('<Blok kind="agent" context="panel"><local name="pose" /><Transform bind={pose} /></Blok>', { scope: mountedBlokScope() }).markers.length,
25
+ ).toBeGreaterThan(0);
25
26
  });
26
27
  });
@@ -1,72 +1,59 @@
1
1
  import {describe, expect, it} from 'vitest';
2
- import {declaredLocals, demoModel, parseBlokDeclarations} from './declarations';
2
+ import {parseBsx} from '../bsx/parse';
3
+ import {declaredLocals, demoModel, parseBlokDeclarations, resolveTreeLocals, treeQueries, treeSubscriptions} from './declarations';
4
+
5
+ const tree = (source: string) => [parseBsx(source).tree];
3
6
 
4
7
  describe('parseBlokDeclarations', () => {
5
- it('reads the declarations a service serves as JSON', () => {
8
+ it('reads the locals a service resolved, each by its <local> node', () => {
6
9
  const declarations = parseBlokDeclarations({
7
10
  vocab: 2,
8
- locals: [
9
- {key: 'iterations', kind: 'INT', default: 3, persist: true},
10
- {key: 'mode', kind: 'ENUM', choices: [{value: 'fast', label: 'Fast'}], default: 'fast'},
11
- {key: 'form', kind: 'DICT', children: [{key: 'name', kind: 'STRING'}]},
12
- {key: 'kindless'},
11
+ resolvedLocals: [
12
+ {node: 'Blok[0]/local[0]', key: 'iterations', kind: 'INT', default: 3, persist: true},
13
+ {node: 'Blok[0]/local[1]', key: 'mode', kind: 'ENUM', choices: [{value: 'fast', label: 'Fast'}], default: 'fast'},
14
+ {node: 'Blok[0]/Card[0]/local[0]', key: 'form', kind: 'DICT', children: [{key: 'name', kind: 'STRING'}]},
15
+ {key: 'nodeless', kind: 'INT'},
16
+ {node: 'Blok[0]/local[2]', key: 'kindless'},
13
17
  ],
14
- demo: {args: {scene: 1}, queries: {stats: {mean: 2}}},
15
- queries: [{key: 'stats', service: 'mikro', document: 'query Stats { stats }', variables: {table_id: 1}, poll: 5, more: 'stats.rows'}],
18
+ demo: {args: {scene: 1}},
16
19
  });
17
20
  const port = {identifier: null, nullable: false, default: null, children: null, choices: null, label: null, description: null};
18
- // A local is a port plus persist; one left without a kind (and no tree to tell) is ANY.
19
21
  expect(declaredLocals(declarations)).toEqual([
20
- {...port, key: 'iterations', kind: 'INT', default: 3, persist: true},
21
- {...port, key: 'mode', kind: 'ENUM', default: 'fast', choices: [{value: 'fast', label: 'Fast', description: null}], persist: false},
22
- {...port, key: 'form', kind: 'DICT', children: [{...port, key: 'name', kind: 'STRING'}], persist: false},
23
- {...port, key: 'kindless', kind: 'ANY', persist: false},
24
- ]);
25
- expect(declarations.queries).toEqual([
26
- {key: 'stats', service: 'mikro', document: 'query Stats { stats }', variables: {table_id: 1}, poll: 5, more: 'stats.rows', keep: null, history: null},
22
+ {...port, node: 'Blok[0]/local[0]', key: 'iterations', kind: 'INT', default: 3, persist: true},
23
+ {...port, node: 'Blok[0]/local[1]', key: 'mode', kind: 'ENUM', default: 'fast', choices: [{value: 'fast', label: 'Fast', description: null}], persist: false},
24
+ {...port, node: 'Blok[0]/Card[0]/local[0]', key: 'form', kind: 'DICT', children: [{...port, key: 'name', kind: 'STRING'}], persist: false},
27
25
  ]);
28
- expect(declarations.subscriptions).toEqual([]);
29
26
  });
30
27
 
31
- it('drops malformed entries and non-list values', () => {
32
- const declarations = parseBlokDeclarations({locals: [{default: 1}, 'x', null], queries: {key: 'q'}, subscriptions: [{key: 's', service: 'mikro'}]});
33
- expect(declarations.resolvedLocals).toEqual([]);
34
- expect(declarations.queries).toEqual([]);
35
- expect(declarations.subscriptions).toEqual([]);
36
- });
37
-
38
- it('resolves locals served as written against the tree, as the server does', () => {
28
+ it('resolves the tree\'s locals when the service sent none, as the server does', () => {
39
29
  const declarations = parseBlokDeclarations({
40
- componentTree: [
41
- {
42
- id: 'Card[0]',
43
- component: 'Card',
44
- children: [
45
- {id: 'Card[0]/Switch[0]', component: 'Switch', props: [{key: 'bind', dynamic_value: {path: 'on'}}]},
46
- {id: 'Card[0]/Text[0]', component: 'Text', props: [{key: 'text', dynamic_value: {path: 'note'}}]},
47
- ],
48
- },
49
- ],
50
- locals: [{key: 'on'}, {key: 'count', default: 2}, {key: 'note'}, {key: 'given', kind: 'STRING', default: 1}],
30
+ componentTree: tree(
31
+ '<Blok kind="custom" context="panel"><local name="on" /><local name="count" default={2} /><Card><local name="note" /><Switch bind={on} /><Text text={note} /></Card></Blok>',
32
+ ),
51
33
  });
52
- expect(declarations.resolvedLocals.map(local => [local.key, local.kind])).toEqual([
53
- ['on', 'BOOL'],
54
- ['count', 'INT'],
55
- ['note', 'ANY'],
56
- ['given', 'STRING'],
34
+ expect(declarations.resolvedLocals.map(local => [local.node, local.key, local.kind])).toEqual([
35
+ ['Blok[0]/local[0]', 'on', 'BOOL'],
36
+ ['Blok[0]/local[1]', 'count', 'INT'],
37
+ ['Blok[0]/Card[0]/local[0]', 'note', 'ANY'],
57
38
  ]);
39
+ expect(resolveTreeLocals([])).toEqual([]);
58
40
  });
41
+ });
59
42
 
60
- it('gives a preview its locals at their defaults and its reads answered by the demo', () => {
61
- const model = demoModel(
62
- [],
63
- parseBlokDeclarations({
64
- locals: [{key: 'iterations', kind: 'INT', default: 3}],
65
- demo: {queries: {stats: {mean: 2}}},
66
- queries: [{key: 'stats', service: 'mikro', document: 'query Stats { stats }'}],
67
- }),
43
+ describe('the reads a tree declares', () => {
44
+ it('reads its <query> and <subscription> children, with their <var> variables', () => {
45
+ const roots = tree(
46
+ '<Blok kind="custom" context="panel"><query name="stats" service="mikro" document="query Stats($t: ID) { stats(t: $t) }" poll={5} more="stats.rows"><var key="t" value={args.table} /></query><subscription name="live" service="mikro" document="subscription { live }" keep={3} /><Text /></Blok>',
68
47
  );
69
- expect(model.iterations).toBe(3);
48
+ expect(treeQueries(roots)).toEqual([
49
+ {key: 'stats', service: 'mikro', document: 'query Stats($t: ID) { stats(t: $t) }', variables: [{key: 't', value_path: 'args.table'}], poll: 5, more: 'stats.rows'},
50
+ ]);
51
+ expect(treeSubscriptions(roots)).toEqual([{key: 'live', service: 'mikro', document: 'subscription { live }', variables: [], keep: 3, history: null}]);
52
+ });
53
+
54
+ it('gives a preview its reads answered by the demo', () => {
55
+ const roots = tree('<Blok kind="custom" context="panel"><query name="stats" service="mikro" document="query Stats { stats }" /><Text /></Blok>');
56
+ const model = demoModel(roots, {demo: {queries: {stats: {mean: 2}}}});
70
57
  expect(model.queries).toEqual({stats: {status: 'ready', value: {mean: 2}, error: null, refetching: false, fetching_more: false}});
71
58
  });
72
59
  });
@@ -1,7 +1,6 @@
1
- import {resolveBlokScope} from '../bsx/infer';
2
- import {emptyBlokScope, type BlokScopeLocal} from '../bsx/references';
3
- import {defaultBlokView} from '../spec/catalogs';
4
- import {LOCAL_ANY_KIND} from '../spec/vocabulary';
1
+ import {declaredQueries as queriesOfTree, declaredSubscriptions as subscriptionsOfTree} from '../bsx/declared';
2
+ import {resolveLocals} from '../bsx/infer';
3
+ import {viewForTree} from '../bsx/references';
5
4
  import type {BlokLocal, BlokLocalChoice, BlokLocalPort} from './persist';
6
5
  import {
7
6
  idleTaskRecord,
@@ -18,9 +17,9 @@ import {
18
17
  } from './runtime';
19
18
 
20
19
  /**
21
- * What a v2 blok declares beside its tree, as a host gets it from the server
22
- * (`Blok`/`BlokRevision`: `vocab`, `resolvedLocals`, `demo`, `queries`,
23
- * `subscriptions`), and the data model a renderer starts from.
20
+ * What a host gets of a v2 blok beside its tree (`Blok`/`BlokRevision`:
21
+ * `vocab`, `resolvedLocals`, `demo`), what its tree declares (its `<query>`
22
+ * and `<subscription>` children), and the data model a renderer starts from.
24
23
  *
25
24
  * The roots a blok reads (vocabulary `roots.data`) are laid out as:
26
25
  *
@@ -29,19 +28,23 @@ import {
29
28
  * args: {...}, task: TaskRecord, viewer: {...}, then: null | "...",
30
29
  * queries: {<name>: {status, value, error, refetching, fetching_more}},
31
30
  * subscriptions: {<name>: {status, value, error}},
32
- * <local>: value, <into name>: TaskRecord,
31
+ * <into name>: TaskRecord,
32
+ * $locals: {<local's runtime id, encoded>: value},
33
33
  * }
34
34
  *
35
- * `self` reads `deps.self`.
35
+ * `self` reads `deps.self`; a `<local>` is read by its name through its
36
+ * parent's scope (`runtime/locals`).
36
37
  */
37
38
 
38
39
  /**
39
40
  * A local as its tree resolves it (`resolvedLocals`): an arg port (`ArgPort`
40
41
  * fields) plus `persist`, its kind always said (inferred where the blok left
41
- * it out, ANY when nothing tells). A renderer never types a local with the
42
- * as-written `locals`, whose kind may be missing.
42
+ * it out, ANY when nothing tells), and `node`, the id of the `<local>` that
43
+ * declares it. A renderer types a local's values (a kept one too) by its node.
43
44
  */
44
45
  export type BlokDeclaredLocal = {
46
+ /** The id of the `<local>` node that declares it. */
47
+ node: string;
45
48
  key: string;
46
49
  kind: string;
47
50
  identifier?: string | null;
@@ -55,7 +58,7 @@ export type BlokDeclaredLocal = {
55
58
  };
56
59
 
57
60
  /** A child port of a local (a list's item, a dict's or model's fields). */
58
- export type BlokDeclaredLocalPort = Omit<BlokDeclaredLocal, 'persist'>;
61
+ export type BlokDeclaredLocalPort = Omit<BlokDeclaredLocal, 'persist' | 'node'>;
59
62
 
60
63
  export type BlokDeclaredRead = {
61
64
  key: string;
@@ -70,13 +73,16 @@ export type BlokDeclaredRead = {
70
73
  history?: string | null;
71
74
  };
72
75
 
76
+ /**
77
+ * What a host gets of a blok beside its tree (`Blok`/`BlokRevision`: `vocab`,
78
+ * `resolvedLocals`, `demo`). Its locals, queries and subscriptions are nodes
79
+ * of the tree; `resolvedLocals` is how the server typed the locals.
80
+ */
73
81
  export type BlokDeclarations = {
74
82
  vocab?: number | null;
75
- /** The locals as the tree resolves them: what the renderer types them with. */
83
+ /** The `<local>` nodes as the tree resolves them, each with its `node`: what the renderer types them with. */
76
84
  resolvedLocals: readonly BlokDeclaredLocal[];
77
85
  demo?: unknown;
78
- queries?: readonly BlokDeclaredRead[] | null;
79
- subscriptions?: readonly BlokDeclaredRead[] | null;
80
86
  };
81
87
 
82
88
  const isRecord = (value: unknown): value is Record<string, unknown> =>
@@ -98,10 +104,18 @@ const localPort = (port: BlokDeclaredLocalPort): BlokLocalPort => ({
98
104
  });
99
105
 
100
106
  export const declaredLocals = (declarations: BlokDeclarations): BlokLocal[] =>
101
- declarations.resolvedLocals.map(local => ({...localPort(local), persist: local.persist === true}));
107
+ declarations.resolvedLocals.map(local => ({...localPort(local), node: local.node, persist: local.persist === true}));
108
+
109
+ /** The one root of a tree as the wire spells it (a list of roots, or one), or null. */
110
+ const rootOf = (roots: ReadonlyArray<unknown> | unknown): BlokComponentNode | null => {
111
+ const list = Array.isArray(roots) ? roots : [roots];
112
+ const [root] = list;
113
+ return list.length === 1 && isRecord(root) && typeof root.component === 'string' ? (root as BlokComponentNode) : null;
114
+ };
102
115
 
103
- export const declaredQueries = (declarations: BlokDeclarations): BlokQuery[] =>
104
- (declarations.queries ?? []).map(query => ({
116
+ /** The reads a blok declares: its `<Blok>`'s `<query>` children. */
117
+ export const treeQueries = (roots: ReadonlyArray<unknown> | unknown): BlokQuery[] =>
118
+ queriesOfTree(rootOf(roots)).map(query => ({
105
119
  key: query.key,
106
120
  service: query.service,
107
121
  document: query.document,
@@ -110,8 +124,9 @@ export const declaredQueries = (declarations: BlokDeclarations): BlokQuery[] =>
110
124
  more: query.more ?? null,
111
125
  }));
112
126
 
113
- export const declaredSubscriptions = (declarations: BlokDeclarations): BlokSubscription[] =>
114
- (declarations.subscriptions ?? []).map(subscription => ({
127
+ /** The streams a blok declares: its `<Blok>`'s `<subscription>` children. */
128
+ export const treeSubscriptions = (roots: ReadonlyArray<unknown> | unknown): BlokSubscription[] =>
129
+ subscriptionsOfTree(rootOf(roots)).map(subscription => ({
115
130
  key: subscription.key,
116
131
  service: subscription.service,
117
132
  document: subscription.document,
@@ -120,10 +135,6 @@ export const declaredSubscriptions = (declarations: BlokDeclarations): BlokSubsc
120
135
  history: subscription.history ?? null,
121
136
  }));
122
137
 
123
- /** Each declared local at its default. */
124
- export const localsModel = (locals: readonly BlokLocal[]): Record<string, unknown> =>
125
- Object.fromEntries(locals.map(local => [local.key, local.default ?? null]));
126
-
127
138
  /** The names `into` gives a task call's record, in the tree. */
128
139
  export const intoNames = (roots: ReadonlyArray<unknown>): string[] => {
129
140
  const names = new Set<string>();
@@ -143,42 +154,41 @@ export const intoNames = (roots: ReadonlyArray<unknown>): string[] => {
143
154
  export const intoModel = (roots: ReadonlyArray<unknown>): Record<string, BlokTaskRecord> =>
144
155
  Object.fromEntries(intoNames(roots).map(name => [name, idleTaskRecord()]));
145
156
 
146
- /** What a renderer shows live before anything synced: locals, idle `into` records, pending reads. */
147
- export const liveModel = (
148
- roots: ReadonlyArray<unknown>,
149
- declarations: BlokDeclarations,
150
- ): {data: Record<string, unknown>; statuses: Record<string, BlokSourceStatus>} => {
151
- const reads = pendingReads(declaredQueries(declarations), declaredSubscriptions(declarations));
157
+ /**
158
+ * What a renderer shows live before anything synced: idle `into` records and
159
+ * pending reads. Locals read their defaults until written (`runtime/locals`).
160
+ */
161
+ export const liveModel = (roots: ReadonlyArray<unknown>): {data: Record<string, unknown>; statuses: Record<string, BlokSourceStatus>} => {
162
+ const reads = pendingReads(treeQueries(roots), treeSubscriptions(roots));
152
163
  return {
153
- data: {...localsModel(declaredLocals(declarations)), ...intoModel(roots), ...reads.data},
164
+ data: {...intoModel(roots), ...reads.data},
154
165
  statuses: reads.statuses,
155
166
  };
156
167
  };
157
168
 
158
169
  /**
159
170
  * What a preview renders: the blok's `demo`, mirroring the roots
160
- * (`{deps, args, task, queries, subscriptions}`), with every declared read
161
- * answered by its demo value. Nothing is pending in a preview.
171
+ * (`{deps, args, task, queries, subscriptions}`), with every read the tree
172
+ * declares answered by its demo value. Nothing is pending in a preview.
162
173
  */
163
- export const demoModel = (roots: ReadonlyArray<unknown>, declarations: BlokDeclarations): Record<string, unknown> => {
174
+ export const demoModel = (roots: ReadonlyArray<unknown>, declarations: Pick<BlokDeclarations, 'demo'>): Record<string, unknown> => {
164
175
  const demo = isRecord(declarations.demo) ? declarations.demo : {};
165
176
  const demoQueries = isRecord(demo.queries) ? demo.queries : {};
166
177
  const demoSubscriptions = isRecord(demo.subscriptions) ? demo.subscriptions : {};
167
178
  const queries = Object.fromEntries(
168
- declaredQueries(declarations).map(query => [
179
+ treeQueries(roots).map(query => [
169
180
  query.key,
170
181
  {status: 'ready', value: demoQueries[query.key] ?? null, error: null, refetching: false, fetching_more: false} satisfies BlokQueryRecord,
171
182
  ]),
172
183
  );
173
184
  const subscriptions = Object.fromEntries(
174
- declaredSubscriptions(declarations).map(subscription => [
185
+ treeSubscriptions(roots).map(subscription => [
175
186
  subscription.key,
176
187
  {status: 'ready', value: demoSubscriptions[subscription.key] ?? null, error: null} satisfies BlokSubscriptionRecord,
177
188
  ]),
178
189
  );
179
190
  const task = isRecord(demo.task) ? {...idleTaskRecord(), ...demo.task} : undefined;
180
191
  return {
181
- ...localsModel(declaredLocals(declarations)),
182
192
  ...intoModel(roots),
183
193
  deps: isRecord(demo.deps) ? demo.deps : {},
184
194
  args: isRecord(demo.args) ? demo.args : {},
@@ -189,20 +199,17 @@ export const demoModel = (roots: ReadonlyArray<unknown>, declarations: BlokDecla
189
199
  };
190
200
 
191
201
  /**
192
- * What a service serves of a blok's declarations as JSON (kabinet's `locals`,
193
- * `queries`, ...: snake_case values). Its `locals` are as the app wrote them
194
- * (a kind may be left out), so they are resolved here against its tree.
202
+ * What a service serves of a blok as JSON (snake_case values): its tree, and
203
+ * the locals as it resolved them (`resolved_locals`, each with its `node`),
204
+ * when it did.
195
205
  */
196
206
  export type BlokDeclarationsJson = {
197
207
  vocab?: number | null;
198
208
  componentTree?: unknown;
199
- locals?: unknown;
209
+ resolvedLocals?: unknown;
200
210
  demo?: unknown;
201
- queries?: unknown;
202
- subscriptions?: unknown;
203
211
  };
204
212
 
205
- const optionalNumber = (value: unknown): number | null => (typeof value === 'number' ? value : null);
206
213
  const optionalString = (value: unknown): string | null => (typeof value === 'string' ? value : null);
207
214
 
208
215
  const jsonChoices = (raw: unknown): BlokLocalChoice[] | null =>
@@ -210,16 +217,12 @@ const jsonChoices = (raw: unknown): BlokLocalChoice[] | null =>
210
217
  ? raw.filter(isRecord).map(choice => ({value: choice.value ?? null, label: optionalString(choice.label), description: optionalString(choice.description)}))
211
218
  : null;
212
219
 
213
- /** A port from JSON; a child without a key or kind is dropped. */
220
+ /** A port from JSON; one without a key or kind is dropped. */
214
221
  const jsonLocalPort = (port: Record<string, unknown>): BlokDeclaredLocalPort | null =>
215
- typeof port.kind === 'string' ? jsonLocalWritten(port, port.kind) : null;
216
-
217
- /** A port from JSON with `kind` as given; null without a key. */
218
- const jsonLocalWritten = <Kind extends string | null>(port: Record<string, unknown>, kind: Kind): (Omit<BlokDeclaredLocalPort, 'kind'> & {kind: Kind}) | null =>
219
- typeof port.key === 'string'
222
+ typeof port.key === 'string' && typeof port.kind === 'string'
220
223
  ? {
221
224
  key: port.key,
222
- kind,
225
+ kind: port.kind,
223
226
  identifier: optionalString(port.identifier),
224
227
  nullable: port.nullable === true,
225
228
  default: port.default ?? null,
@@ -235,51 +238,27 @@ const jsonLocalWritten = <Kind extends string | null>(port: Record<string, unkno
235
238
  }
236
239
  : null;
237
240
 
238
- /** Locals from JSON as written (snake_case port fields plus `persist`, a kind maybe left out); one without a key is dropped. */
239
- const jsonLocals = (raw: unknown): BlokScopeLocal[] =>
241
+ /** Resolved locals from JSON; one without a node, a key or a kind is dropped. */
242
+ const jsonResolvedLocals = (raw: unknown): BlokDeclaredLocal[] =>
240
243
  (Array.isArray(raw) ? raw : []).filter(isRecord).flatMap(local => {
241
- const port = jsonLocalWritten(local, typeof local.kind === 'string' ? local.kind : null);
242
- return port ? [{...port, persist: local.persist === true}] : [];
244
+ const port = jsonLocalPort(local);
245
+ return port && typeof local.node === 'string' ? [{...port, node: local.node, persist: local.persist === true}] : [];
243
246
  });
244
247
 
245
- const jsonRoots = (raw: unknown): BlokComponentNode[] =>
246
- (Array.isArray(raw) ? raw : [raw]).filter((node): node is BlokComponentNode => isRecord(node) && typeof node.component === 'string');
247
-
248
248
  /**
249
- * Locals as written, resolved against the tree the way the spec and the
250
- * server resolve them (`resolveBlokScope`: default, task argument, bind, else
251
- * ANY); a given kind is kept.
249
+ * The `<local>` nodes of a tree resolved the way the spec and the server
250
+ * resolve them (default, task argument, bind, else ANY), checked unmounted:
251
+ * what a host uses when the server sent no `resolvedLocals`.
252
252
  */
253
- export const resolveWrittenLocals = (roots: readonly BlokComponentNode[], locals: readonly BlokScopeLocal[]): BlokDeclaredLocal[] => {
254
- if (!locals.length) return [];
255
- const [root] = roots;
256
- if (!root || roots.length !== 1) return locals.map(local => ({...local, kind: local.kind ?? LOCAL_ANY_KIND}));
257
- return resolveBlokScope(root, {...emptyBlokScope(), locals}, defaultBlokView()).locals.map(local => ({...local}));
253
+ export const resolveTreeLocals = (roots: ReadonlyArray<unknown> | unknown): BlokDeclaredLocal[] => {
254
+ const root = rootOf(roots);
255
+ if (!root) return [];
256
+ return [...resolveLocals(root, null, viewForTree(root))].map(([node, {local}]) => ({...local, node}) as BlokDeclaredLocal);
258
257
  };
259
258
 
260
- const jsonReads = (raw: unknown): BlokDeclaredRead[] =>
261
- (Array.isArray(raw) ? raw : []).filter(isRecord).flatMap(read =>
262
- typeof read.key === 'string' && typeof read.service === 'string' && typeof read.document === 'string'
263
- ? [
264
- {
265
- key: read.key,
266
- service: read.service,
267
- document: read.document,
268
- variables: read.variables,
269
- poll: optionalNumber(read.poll),
270
- more: optionalString(read.more),
271
- keep: optionalNumber(read.keep),
272
- history: optionalString(read.history),
273
- },
274
- ]
275
- : [],
276
- );
277
-
278
- /** A blok's declarations from JSON a service serves, keeping only well-formed entries. */
259
+ /** A blok's declarations from JSON a service serves: its resolved locals as served, or resolved from its tree. */
279
260
  export const parseBlokDeclarations = (raw: BlokDeclarationsJson): BlokDeclarations => ({
280
261
  vocab: raw.vocab ?? null,
281
- resolvedLocals: resolveWrittenLocals(jsonRoots(raw.componentTree), jsonLocals(raw.locals)),
262
+ resolvedLocals: raw.resolvedLocals !== undefined && raw.resolvedLocals !== null ? jsonResolvedLocals(raw.resolvedLocals) : resolveTreeLocals(raw.componentTree),
282
263
  demo: raw.demo ?? null,
283
- queries: jsonReads(raw.queries),
284
- subscriptions: jsonReads(raw.subscriptions),
285
264
  });
@@ -13,12 +13,12 @@ const gauge = createBlokComponent({name: 'Gauge', service: 'lab', schema: z.obje
13
13
  describe('declareBlokUi', () => {
14
14
  it('speaks the base catalog in the contexts it draws whole', () => {
15
15
  const whole = declareBlokUi({name: 'whole', implements: [shadcnUi, shadcnHtml, shadcnLogo, shadcnExtras, threeScene, hostedStructure]});
16
- expect(whole.contexts).toEqual(['HTML', 'SCENE', 'LOGO']);
17
- expect(whole.claims).toEqual(['base@3']);
16
+ expect(whole.contexts).toEqual(['PANEL', 'SCENE', 'LOGO']);
17
+ expect(whole.claims).toEqual(['base@4']);
18
18
  const flat = declareBlokUi({name: 'flat', implements: [shadcnUi, shadcnHtml, shadcnLogo, shadcnExtras, hostedStructure]});
19
- expect(flat.contexts).toEqual(['HTML', 'LOGO']);
19
+ expect(flat.contexts).toEqual(['PANEL', 'LOGO']);
20
20
  const logos = declareBlokUi({name: 'logos', implements: [shadcnLogo]});
21
- expect([logos.contexts, logos.claims]).toEqual([['LOGO'], ['base@3']]);
21
+ expect([logos.contexts, logos.claims]).toEqual([['LOGO'], ['base@4']]);
22
22
  expect(declareBlokUi({name: 'none', implements: []}).claims).toEqual([]);
23
23
  });
24
24
 
@@ -36,15 +36,15 @@ export type BlokUi = {
36
36
  claims: readonly string[];
37
37
  };
38
38
 
39
- const CONTEXTS: readonly BlokContextName[] = ['HTML', 'SCENE', 'LOGO'];
39
+ const CONTEXTS: readonly BlokContextName[] = ['PANEL', 'SCENE', 'LOGO'];
40
40
 
41
41
  /**
42
42
  * The context a base component counts towards: its own, and for one that
43
- * bridges html and another context (`Scene` opens a scene inside html), the
44
- * other one: a renderer that draws no 3D still draws html whole.
43
+ * bridges a panel and another context (`Scene` opens a scene inside a panel),
44
+ * the other one: a renderer that draws no 3D still draws panels whole.
45
45
  */
46
46
  const contextOf = (component: {context: BlokContextName; childrenContext: BlokContextName | null}): BlokContextName =>
47
- component.context !== 'HTML' ? component.context : (component.childrenContext ?? 'HTML');
47
+ component.context !== 'PANEL' ? component.context : (component.childrenContext ?? 'PANEL');
48
48
 
49
49
  /** The contexts in which `catalog` draws every component of the base catalog. */
50
50
  export const baseContextsOf = (catalog: BlokCatalog): BlokContextName[] => {