@tjakoen/grain 0.1.12
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/LICENSE +202 -0
- package/PLAN.md +27 -0
- package/README.md +243 -0
- package/ai/accepts.test.ts +69 -0
- package/ai/accepts.ts +57 -0
- package/ai/ai.css +127 -0
- package/ai/client-door.test.ts +70 -0
- package/ai/client-door.ts +56 -0
- package/ai/contract.test.ts +158 -0
- package/ai/contract.ts +285 -0
- package/ai/interaction-layer.test.ts +270 -0
- package/ai/interaction-layer.ts +138 -0
- package/ai/manifest-dom.test.ts +176 -0
- package/ai/manifest-dom.ts +187 -0
- package/ai/manifest.test.ts +71 -0
- package/ai/manifest.ts +57 -0
- package/ai/markdown.test.ts +68 -0
- package/ai/markdown.ts +74 -0
- package/ai/model-chat.test.ts +131 -0
- package/ai/model-chat.ts +151 -0
- package/ai/model-reasoner.test.ts +152 -0
- package/ai/model-reasoner.ts +146 -0
- package/ai/model.test.ts +148 -0
- package/ai/model.ts +200 -0
- package/ai/reasoner-kit.test.ts +142 -0
- package/ai/reasoner-kit.ts +159 -0
- package/ai/reasoner.test.ts +227 -0
- package/ai/reasoner.ts +446 -0
- package/ai/timeline-log.test.ts +55 -0
- package/ai/timeline-log.ts +50 -0
- package/ai/vocab-reference.test.ts +43 -0
- package/ai/vocab-reference.ts +107 -0
- package/ai/webllm.test.ts +79 -0
- package/ai/webllm.ts +125 -0
- package/assets/figure-sample.svg +14 -0
- package/assets/sprite.svg +22 -0
- package/catalog/catalog.test.ts +37 -0
- package/catalog/catalog.ts +424 -0
- package/components/atoms/action-badge/action-badge.css +17 -0
- package/components/atoms/action-badge/action-badge.html +6 -0
- package/components/atoms/action-badge/action-badge.md +23 -0
- package/components/atoms/b-badge/b-badge.css +15 -0
- package/components/atoms/b-badge/b-badge.html +2 -0
- package/components/atoms/b-badge/b-badge.md +16 -0
- package/components/atoms/b-button/b-button.ai.md +22 -0
- package/components/atoms/b-button/b-button.css +87 -0
- package/components/atoms/b-button/b-button.html +5 -0
- package/components/atoms/b-button/b-button.md +77 -0
- package/components/atoms/b-icon/b-icon.css +10 -0
- package/components/atoms/b-icon/b-icon.html +5 -0
- package/components/atoms/b-icon/b-icon.md +36 -0
- package/components/atoms/b-icon-button/b-icon-button.ai.md +11 -0
- package/components/atoms/b-icon-button/b-icon-button.css +43 -0
- package/components/atoms/b-icon-button/b-icon-button.html +8 -0
- package/components/atoms/b-icon-button/b-icon-button.md +33 -0
- package/components/atoms/b-input/b-input.css +36 -0
- package/components/atoms/b-input/b-input.html +8 -0
- package/components/atoms/b-input/b-input.md +59 -0
- package/components/atoms/b-kbd/b-kbd.css +11 -0
- package/components/atoms/b-kbd/b-kbd.html +3 -0
- package/components/atoms/b-kbd/b-kbd.md +13 -0
- package/components/atoms/b-list/b-list.css +21 -0
- package/components/atoms/b-list/b-list.html +6 -0
- package/components/atoms/b-list/b-list.md +24 -0
- package/components/atoms/b-meter/b-meter.css +39 -0
- package/components/atoms/b-meter/b-meter.md +44 -0
- package/components/atoms/b-select/b-select.css +35 -0
- package/components/atoms/b-select/b-select.md +75 -0
- package/components/atoms/b-switch/b-switch.css +61 -0
- package/components/atoms/b-switch/b-switch.html +6 -0
- package/components/atoms/b-switch/b-switch.md +40 -0
- package/components/atoms/b-text/b-text.ai.md +29 -0
- package/components/atoms/b-text/b-text.css +2 -0
- package/components/atoms/b-text/b-text.html +3 -0
- package/components/atoms/b-text/b-text.md +23 -0
- package/components/atoms/code-block/code-block.css +42 -0
- package/components/atoms/code-block/code-block.md +23 -0
- package/components/examples.test.ts +23 -0
- package/components/molecules/callout/callout.css +19 -0
- package/components/molecules/callout/callout.md +15 -0
- package/components/molecules/card/card.css +28 -0
- package/components/molecules/card/card.md +30 -0
- package/components/molecules/chat-message/chat-message.css +83 -0
- package/components/molecules/chat-message/chat-message.md +52 -0
- package/components/molecules/chip-group/chip-group.css +35 -0
- package/components/molecules/chip-group/chip-group.md +42 -0
- package/components/molecules/content-index/content-index.css +87 -0
- package/components/molecules/content-index/content-index.md +27 -0
- package/components/molecules/content-source/content-source.css +6 -0
- package/components/molecules/content-source/content-source.md +13 -0
- package/components/molecules/figure/figure.css +19 -0
- package/components/molecules/figure/figure.md +20 -0
- package/components/molecules/lede/lede.css +17 -0
- package/components/molecules/lede/lede.md +17 -0
- package/components/molecules/made-with/made-with.css +25 -0
- package/components/molecules/made-with/made-with.md +35 -0
- package/components/molecules/nav-item/nav-item.css +19 -0
- package/components/molecules/nav-item/nav-item.md +21 -0
- package/components/molecules/stat-tile/stat-tile.css +34 -0
- package/components/molecules/stat-tile/stat-tile.md +50 -0
- package/components/molecules/status-list/status-list.css +47 -0
- package/components/molecules/status-list/status-list.md +62 -0
- package/components/molecules/tab/tab.css +32 -0
- package/components/molecules/tab/tab.md +27 -0
- package/components/molecules/table/table.css +23 -0
- package/components/molecules/table/table.md +18 -0
- package/components/organisms/activity-bar/activity-bar.css +36 -0
- package/components/organisms/activity-bar/activity-bar.md +43 -0
- package/components/organisms/app-shell/app-shell.css +193 -0
- package/components/organisms/app-shell/app-shell.md +58 -0
- package/components/organisms/app-shell/app-shell.test.ts +74 -0
- package/components/organisms/app-window/app-window.css +111 -0
- package/components/organisms/app-window/app-window.md +53 -0
- package/components/organisms/chat-log/chat-log.css +5 -0
- package/components/organisms/chat-log/chat-log.md +14 -0
- package/components/organisms/code-editor/code-editor.ai.md +31 -0
- package/components/organisms/code-editor/code-editor.css +111 -0
- package/components/organisms/code-editor/code-editor.md +122 -0
- package/components/organisms/console/console.css +112 -0
- package/components/organisms/console/console.md +66 -0
- package/components/organisms/file-tree/file-tree.css +85 -0
- package/components/organisms/file-tree/file-tree.md +49 -0
- package/components/organisms/note/note.css +19 -0
- package/components/organisms/note/note.md +20 -0
- package/components/organisms/notepad/notepad.css +60 -0
- package/components/organisms/notepad/notepad.md +40 -0
- package/components/organisms/side-rail/side-rail.css +55 -0
- package/components/organisms/side-rail/side-rail.md +24 -0
- package/components/organisms/sidebar-panel/sidebar-panel.css +69 -0
- package/components/organisms/sidebar-panel/sidebar-panel.md +47 -0
- package/components/organisms/status-bar/status-bar.css +33 -0
- package/components/organisms/status-bar/status-bar.md +28 -0
- package/components/organisms/tab-bar/tab-bar.css +48 -0
- package/components/organisms/tab-bar/tab-bar.md +26 -0
- package/components/organisms/timeline/timeline.css +51 -0
- package/components/organisms/timeline/timeline.md +42 -0
- package/components/organisms/topbar/topbar.css +8 -0
- package/components/organisms/topbar/topbar.md +26 -0
- package/components/responsive-polish.test.ts +43 -0
- package/components/tokens.test.ts +39 -0
- package/fonts/redaction-35-400.woff2 +0 -0
- package/fonts/redaction-400.woff2 +0 -0
- package/fonts/redaction-50-400.woff2 +0 -0
- package/fonts/redaction-70-400.woff2 +0 -0
- package/package.json +46 -0
- package/scripts/ai-dispatch.js +583 -0
- package/scripts/ai-dispatch.test.ts +85 -0
- package/scripts/ai-spotlight.d.ts +23 -0
- package/scripts/ai-spotlight.js +139 -0
- package/scripts/ai-spotlight.test.ts +31 -0
- package/scripts/catalog-peek.js +156 -0
- package/scripts/catalog-peek.test.ts +31 -0
- package/scripts/cmdk.js +159 -0
- package/scripts/demo-box.js +53 -0
- package/scripts/handoff.js +64 -0
- package/scripts/handoff.test.ts +36 -0
- package/scripts/lightbox.js +144 -0
- package/scripts/lightbox.test.ts +23 -0
- package/scripts/made-with.d.ts +2 -0
- package/scripts/made-with.js +22 -0
- package/scripts/made-with.test.ts +25 -0
- package/scripts/notepad.js +100 -0
- package/scripts/shell.js +204 -0
- package/scripts/shell.test.ts +50 -0
- package/scripts/tabs.js +127 -0
- package/scripts/tabs.test.ts +40 -0
- package/scripts/terminal.js +271 -0
- package/scripts/terminal.test.ts +36 -0
- package/scripts/theme-boot.js +15 -0
- package/scripts/theme.js +87 -0
- package/scripts/type-effect.js +25 -0
- package/scripts/xray.js +79 -0
- package/scripts/xray.test.ts +43 -0
- package/styles/cmdk.css +65 -0
- package/styles/global.css +80 -0
- package/styles/grain.css +47 -0
- package/styles/lightbox.css +78 -0
- package/styles/themes/_template.css +48 -0
- package/styles/themes/baguette.css +30 -0
- package/styles/themes/brioche.css +30 -0
- package/styles/variables.css +217 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/PLAN.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# GRAIN — plan / roadmap
|
|
2
|
+
|
|
3
|
+
> **Status:** the **AI-interaction layer works today** — the closed action vocabulary
|
|
4
|
+
> (`grain/ai/contract.ts`), the one door (`grain/ai/interaction-layer.ts`), the harvested manifest
|
|
5
|
+
> (`/ai/manifest`), grade-as-signal, and the SSE dispatcher island. This file is GRAIN's **roadmap**:
|
|
6
|
+
> planned/deferred features, one line each. It is an *index*, **not** a second source of truth — the AI
|
|
7
|
+
> contract lives in [AI-INTERFACE](https://tjakoen.github.io/grain/docs/ai-interface), the build rules in
|
|
8
|
+
> [CONVENTIONS](https://tjakoen.github.io/batch/docs/conventions), the beliefs in [`../tjakoen.github.io/PHILOSOPHY.md`](../tjakoen.github.io/PHILOSOPHY.md). When a
|
|
9
|
+
> feature is built, follow the CLAUDE.md alignment table (contract → reasoner → tests → docs).
|
|
10
|
+
|
|
11
|
+
## Planned
|
|
12
|
+
|
|
13
|
+
1. ~~**Unified interaction log (human + AI).**~~ **BUILT** (2026-07-08) — the **interaction timeline**
|
|
14
|
+
(<https://tjakoen.github.io/grain/docs/ai-interface> §5g). A `LogSink` port (`contract.ts`, like `OpChannel`) recorded in
|
|
15
|
+
`handleIntent` for *every* crossing — request + response, `source`-tagged — so the door logs human
|
|
16
|
+
and AI **identically**. The visible timeline is one impl (`ai/timeline-log.ts` `createStreamLogSink`)
|
|
17
|
+
pushing a `log` render op to the `timeline` push surface; the `timeline` component color-codes each
|
|
18
|
+
entry by provenance **through grade** (grain font = AI). Live on `/grain`. Unit + integration tested.
|
|
19
|
+
2. **Direct-write seam (`/kb/*`).** User-owned ground-truth writes that bypass the AI door
|
|
20
|
+
(AI-INTERFACE §5b — a documented seam, not yet built).
|
|
21
|
+
3. **Reconnect & durability.** A durable op store + per-actor turn status, so a refresh mid-turn shows a
|
|
22
|
+
coherent state (AI-INTERFACE §5d — deferred to the real-reasoner step).
|
|
23
|
+
4. **Retrieval / knowledge port.** A GRAIN seam (`KnowledgeSource` / `retrieve`) so the AI can query
|
|
24
|
+
content smartly; the concrete model + embeddings are injected by the consumer (memory
|
|
25
|
+
`ai-content-retrieval-layer`).
|
|
26
|
+
5. **Workflow / actions registry.** Named higher-level workflows composed from the atomic verbs, plus a
|
|
27
|
+
discoverable "what the AI can do" list (memory `actions-workflow-registry-idea`).
|
package/README.md
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# 🌾 GRAIN: usage
|
|
2
|
+
|
|
3
|
+
[](https://tjakoen.github.io/notes/ten-times-zero)
|
|
4
|
+
[](LICENSE)
|
|
5
|
+
|
|
6
|
+
GRAIN is the **AI-interaction design system**: a UI where every surface is addressable
|
|
7
|
+
and operable by both a human and an AI through one shared vocabulary, with the AI's
|
|
8
|
+
presence shown as a visible signal (*grain = AI*). It runs on a **substrate** (BATCH is
|
|
9
|
+
the reference one) but imports nothing from it.
|
|
10
|
+
|
|
11
|
+
- **The beliefs behind it:** [PHILOSOPHY.md](https://github.com/tjakoen/tjakoen.github.io/blob/main/docs/PHILOSOPHY.md)
|
|
12
|
+
- **Overview / why:** [GRAIN](https://tjakoen.github.io/grain/docs/grain)
|
|
13
|
+
- **The full contract** (intent envelope, render ops, manifest, the AI-acts protocol,
|
|
14
|
+
the two write paths): [AI-INTERFACE](https://tjakoen.github.io/grain/docs/ai-interface)
|
|
15
|
+
- **Built on top of GRAIN:** [`MILL`](../mill/PLAN.md), the Markdown→pages CMS (a layer above).
|
|
16
|
+
- **Roadmap / planned features:** [`PLAN.md`](PLAN.md)
|
|
17
|
+
|
|
18
|
+
> **Docs live at [tjakoen.github.io/grain/docs](https://tjakoen.github.io/grain/docs).** GRAIN's
|
|
19
|
+
> explanatory docs are canonically homed in the portfolio repo (`tjakoen.github.io/docs/grain/`) and
|
|
20
|
+
> rendered there through MILL. This repo keeps its components, code, PLAN, and this README; it no
|
|
21
|
+
> longer ships a `docs/` folder.
|
|
22
|
+
|
|
23
|
+
This file is the practical reference: what a host must provide, the markup conventions,
|
|
24
|
+
and how to wire it.
|
|
25
|
+
|
|
26
|
+
**What GRAIN gives you** (the full, tiered list is the source in [GRAIN](https://tjakoen.github.io/grain/docs/grain) §"What GRAIN gives you"):
|
|
27
|
+
|
|
28
|
+
- **Hero:** one vocabulary, two operators (human + AI, one door → `RenderOp`s) · grade-as-signal
|
|
29
|
+
(the AI's presence is visible) · self-documenting (a live `/catalog`).
|
|
30
|
+
- **Also:** the manifest (a drift-proof machine map the AI reads) · auditable by design (every
|
|
31
|
+
interaction is a source-tagged Intent → an interaction log is a server-side drop-in) · the design
|
|
32
|
+
system works with no AI · a **client-side door** (the same vocabulary runs on static hosts, no
|
|
33
|
+
server) · re-skin by token override · machine-readable (SEO/AEO) · **built on the modern web
|
|
34
|
+
platform** (native View Transitions, `<dialog>`, `<details>`, `:has()`, `color-mix()`, not
|
|
35
|
+
framework JS).
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## 0. Two layers: the AI interface is optional
|
|
40
|
+
|
|
41
|
+
GRAIN is **two things, one-directional**:
|
|
42
|
+
|
|
43
|
+
- **The design system** (always usable): the `b-*` atoms, the **default theme**
|
|
44
|
+
(`styles/variables.css` = tokens + `@font-face`, `styles/global.css` = base/skin,
|
|
45
|
+
`fonts/` = the Redaction grades), and the **grade-as-signal** mechanism
|
|
46
|
+
(`styles/grain.css`: the `--type-font` atom, `data-grade` / `.field` / `data-commit`
|
|
47
|
+
rules, caret, settle). Grade is useful with **no AI at all**: draft vs. saved,
|
|
48
|
+
focus/editing, in-transit vs. committed. It depends on nothing in `ai/`.
|
|
49
|
+
- **The AI-interaction layer** (opt-in): `ai/*` (the door, contract, reasoner boundary,
|
|
50
|
+
manifest, accepts), the dispatcher island `scripts/ai-dispatch.js`, and its styling
|
|
51
|
+
`ai/ai.css` (the "AI is acting" spotlight). This layer *uses* the design system
|
|
52
|
+
(sets `data-grade` by provenance); the design system never reaches back into it.
|
|
53
|
+
|
|
54
|
+
So you can adopt **just the design system** in a plain BATCH app: link GRAIN's three
|
|
55
|
+
stylesheets (`variables.css` → `global.css` → `grain.css`) + the `b-*` atoms, and skip
|
|
56
|
+
`ai/` entirely. It looks like GRAIN out of the box; override token slots (§4) to re-skin.
|
|
57
|
+
Add the AI layer later by wiring §5 and bundling `ai/ai.css`. (This repo's app uses both;
|
|
58
|
+
a no-AI consumer simply drops `grain/ai` from its style roots.)
|
|
59
|
+
|
|
60
|
+
---
|
|
61
|
+
|
|
62
|
+
## 1. The substrate contract: what a host must provide
|
|
63
|
+
|
|
64
|
+
GRAIN is portable to any host that supplies three things:
|
|
65
|
+
|
|
66
|
+
1. **An `OpChannel`**: `push(session, event, data)` (`ai/contract.ts`). How render ops
|
|
67
|
+
reach a client. BATCH = SSE; a WebSocket hub would do. GRAIN imports the *interface*
|
|
68
|
+
from its own contract, never the implementation. *(Future, additive: a durable
|
|
69
|
+
sibling, a per-actor turn-status store, lets a reconnecting client reflect a
|
|
70
|
+
still-running turn. It sits beside this port; the component conventions don't change.
|
|
71
|
+
See <https://tjakoen.github.io/grain/docs/ai-interface> §5d.)*
|
|
72
|
+
2. **A renderer that understands the binding vocabulary** below (§3). BATCH's
|
|
73
|
+
composition engine implements it; another substrate must too.
|
|
74
|
+
3. **A filesystem** (`ai/accepts.ts` reads component files to harvest the manifest):
|
|
75
|
+
any JS runtime, not substrate-specific.
|
|
76
|
+
|
|
77
|
+
Everything else (the scoped write capability, "render a surface to HTML") is **injected**
|
|
78
|
+
by the composition root: GRAIN names no concrete dependency.
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## 2. GRAIN's own conventions (read by the dispatcher + harvest)
|
|
83
|
+
|
|
84
|
+
Put these on your components/markup; GRAIN's island + door act on them:
|
|
85
|
+
|
|
86
|
+
| Attribute | On | Means |
|
|
87
|
+
|---|---|---|
|
|
88
|
+
| `data-surface="kind:id"` | any region | its **address**: what render ops target (built with `surface(kind, id)`) |
|
|
89
|
+
| `data-action="verb"` | a control | clicking it (or Enter in an input) becomes `POST /intent` with that verb |
|
|
90
|
+
| `data-target="surface"` | a control | the surface the action affects (defaults to the nearest `data-surface`) |
|
|
91
|
+
| `data-kind` + `data-accepts="v1 v2"` | a component root | harvested into the AI manifest (what verbs this kind accepts) |
|
|
92
|
+
| `data-grade="grain\|smooth\|accent"` · `.is-ai` | any ancestor | provenance: grain = AI, smooth = human (distributed via `--type-font`) |
|
|
93
|
+
| `data-commit="pending"` | any ancestor | in-transit / not-yet-committed → reads grain |
|
|
94
|
+
|
|
95
|
+
The verbs themselves live in the closed registry (`ai/contract.ts`: `ActionName` /
|
|
96
|
+
`SurfaceKind` / `ACTIONS`): the single source of truth.
|
|
97
|
+
|
|
98
|
+
**State invariant (what keeps it from getting fidgety).** UI state is **declarative
|
|
99
|
+
attributes** components wear: the dispatcher acts on attributes, never component names,
|
|
100
|
+
so any component participates with no bespoke JS. Transient state (`data-commit="pending"`
|
|
101
|
+
/ `ai-spotlit`) follows one rule: **every optimistic "pending" is cleared by a
|
|
102
|
+
server-sent terminal op**: a `committed` op, a `flash` (rollback), a `remove`, or a
|
|
103
|
+
`type {done}`. Backstops cover the rest: a held control is also released when the AI
|
|
104
|
+
turn ends (`spotlight active:false`), re-triggering the same surface clears the old
|
|
105
|
+
holder first, and a 20s timeout releases a stuck spotlight. Net: the server is the
|
|
106
|
+
source of truth; the DOM is derived, and never strands a "working" state.
|
|
107
|
+
|
|
108
|
+
---
|
|
109
|
+
|
|
110
|
+
## 3. The binding vocabulary GRAIN's components are authored in
|
|
111
|
+
|
|
112
|
+
These are the host-renderer conventions (BATCH defines them in `../batch/render/render.ts`).
|
|
113
|
+
A substrate must interpret them to render GRAIN's `b-*` atoms. This is the one coupling
|
|
114
|
+
that lives in markup rather than code.
|
|
115
|
+
|
|
116
|
+
| Form | Means |
|
|
117
|
+
|---|---|
|
|
118
|
+
| `data-field="path"` | set this element's **text** from a data path (HTML-escaped); `"."` = the scope itself |
|
|
119
|
+
| `data-bind-<attr>="path"` | set **attribute** `<attr>` from a data path; empty → omit; URL attrs are scheme-guarded |
|
|
120
|
+
| `<slot-tag prop-as="default" prop-attr-X="prop" prop-text="prop">` | the **polymorphic** element: become `as`; set attrs/text from config props |
|
|
121
|
+
| `each="path"` | **repeat** the component once per array item (item = the scope) |
|
|
122
|
+
| `data="path"` | **scope** a child component to a sub-slice |
|
|
123
|
+
| `<b-button>` … (hyphenated tag) | a **component**: expanded server-side; may self-close |
|
|
124
|
+
|
|
125
|
+
Example (GRAIN's text atom):
|
|
126
|
+
```html
|
|
127
|
+
<slot-tag prop-as="span" prop-attr-class="class" data-field=".">Example text</slot-tag>
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## 4. The token slots (GRAIN's default theme: override to re-skin)
|
|
133
|
+
|
|
134
|
+
GRAIN ships a **default theme** in `styles/variables.css` (the *Sourdough* look:
|
|
135
|
+
monochrome paper/ink + self-hosted Redaction grades). The mechanism (`styles/grain.css`)
|
|
136
|
+
and atoms read these token *slots*, so a consumer re-skins by **overriding the slots** in
|
|
137
|
+
its own sheet (linked after GRAIN's), no component changes:
|
|
138
|
+
|
|
139
|
+
- **fonts:** `--type-font` (the inherited switch), `--font-grain`, `--font-smooth`, `--font-accent`
|
|
140
|
+
- **ink/paper:** `--ink`, `--paper`, `--color-fg`, `--color-muted`, `--line-soft`
|
|
141
|
+
- **scale:** `--space-1..8`, `--text-sm`, `--border`, `--radius-sm`, `--radius-md`
|
|
142
|
+
- **AI spotlight:** `--ai-veil` (the "AI is acting" backdrop, three on-theme picks:
|
|
143
|
+
`color-mix(in srgb, var(--ink) 22%, transparent)` dim · `color-mix(in srgb, var(--paper) 70%, transparent)` wash ·
|
|
144
|
+
`transparent` lift) and `--ai-focus-move` (how slowly the spotlight glides between
|
|
145
|
+
surfaces). Both set once in the theme so every page behaves identically.
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## 5. Wiring it (composition root)
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
import { createInteractionLayer } from "grain/ai/interaction-layer.ts";
|
|
153
|
+
import { createAccepts } from "grain/ai/accepts.ts";
|
|
154
|
+
|
|
155
|
+
const layer = createInteractionLayer({
|
|
156
|
+
reasoner, // your Model-backed reasoner (or the stub)
|
|
157
|
+
stream, // an OpChannel (e.g. BATCH's createStream())
|
|
158
|
+
archiveItem, // app-scoped write capabilities the reasoner may use…
|
|
159
|
+
renderSurface, // …and "render this surface to committed HTML"
|
|
160
|
+
});
|
|
161
|
+
// mount: POST /intent → layer.handleIntent ; GET /stream → the channel ;
|
|
162
|
+
// GET /ai/manifest → buildManifest(screen, targets) ;
|
|
163
|
+
// include the island: <script src="/scripts/ai-dispatch.js" defer></script>
|
|
164
|
+
// link GRAIN's stylesheets in <head>: variables.css → global.css → grain.css, then your
|
|
165
|
+
// component bundle; include grain/ai/ai.css (bundled or linked) for the AI spotlight.
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
> **Rough edge (honest):** the action *vocabulary values* and the reasoner's *tools*
|
|
169
|
+
> (`archiveItem` …) are currently entangled with GRAIN rather than fully app-injected.
|
|
170
|
+
> Pushing the vocabulary down to the consumer is a known next step (see
|
|
171
|
+
> <https://tjakoen.github.io/grain/docs/ai-interface> §1b note). Until then, edit `ai/contract.ts` to add verbs.
|
|
172
|
+
|
|
173
|
+
### Optional standalone islands (drop-in, no wiring)
|
|
174
|
+
|
|
175
|
+
Self-contained `<dialog>` mechanisms: one `<script>` plus its stylesheet, driven entirely by
|
|
176
|
+
data-attributes, no composition-root wiring and no AI layer required:
|
|
177
|
+
|
|
178
|
+
- **`scripts/cmdk.js`** + `styles/cmdk.css`: the ⌘K command palette. Fetches `/search.json`;
|
|
179
|
+
any `[data-cmdk-open]` element also opens it.
|
|
180
|
+
- **`scripts/lightbox.js`** + `styles/lightbox.css`: the image viewer. `[data-lightbox]` marks a
|
|
181
|
+
trigger (if it's an `<a href>`, the href is the full image, so it stays a real link with no JS);
|
|
182
|
+
a `[data-lightbox-group]` ancestor turns its triggers into one gallery with prev/next + a dot rail.
|
|
183
|
+
Caption comes from `data-lightbox-caption` or the child `<img>` alt.
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## 6. Build your own: a theme + components (consumer guide)
|
|
188
|
+
|
|
189
|
+
GRAIN is meant to be **consumed, not forked.** Two independent extension points, and they
|
|
190
|
+
compose: **one token override re-skins your components and GRAIN's together.** The decider for
|
|
191
|
+
*where* something lives is one question: **"would another product on GRAIN want this?"** Yes →
|
|
192
|
+
it's reusable design, contribute it to `grain/`. No → it's yours, keep it in your app. This repo
|
|
193
|
+
is the worked example: `project/` and `tjakoen.github.io/` are two consumers doing exactly the below.
|
|
194
|
+
|
|
195
|
+
### A · A theme (re-skin): override token slots, never components
|
|
196
|
+
|
|
197
|
+
1. Write a stylesheet that redefines the §4 token slots (`--paper`, `--ink`, `--font-*`,
|
|
198
|
+
`--space-*`, `--radius-*`, `--ai-veil`, …). **Touch no component CSS.**
|
|
199
|
+
2. Link it **after** GRAIN's `variables.css` in your pages. Every `b-*` atom re-skins.
|
|
200
|
+
|
|
201
|
+
```css
|
|
202
|
+
/* my-theme.css — linked after grain/styles/variables.css */
|
|
203
|
+
:root { --paper:#0f1115; --ink:#e8e6df; --font-grain:"IBM Plex Mono"; --radius-md:12px;
|
|
204
|
+
--ai-veil: color-mix(in srgb, var(--ink) 30%, transparent); }
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
To make it **switchable at runtime** (light/dark or multiple flavours), scope the block to an
|
|
208
|
+
attribute and flip it on `<html>`: `[data-theme="mine"] { … }` / `[data-color-scheme="dark"] { … }`.
|
|
209
|
+
Wire the two theming scripts once at the composition root: `scripts/theme.js` (deferred: the
|
|
210
|
+
declarative controls + localStorage persistence) and `scripts/theme-boot.js` **render-blocking in
|
|
211
|
+
`<head>`** (pre-sets the saved attributes before first paint; without it every navigation flashes
|
|
212
|
+
the default theme). Inject them on *every* page shell (including any page that builds its own
|
|
213
|
+
`<html>` outside the page server), or that page silently ignores the saved theme.
|
|
214
|
+
Grade-as-signal is load-bearing: keep `--font-grain` visibly distinct from `--font-smooth` in
|
|
215
|
+
every theme, and verify it still reads in dark.
|
|
216
|
+
|
|
217
|
+
### B · A component: author in the binding vocabulary
|
|
218
|
+
|
|
219
|
+
1. Create your own **component-root dir** (e.g. `myapp/components/`). Drop a component:
|
|
220
|
+
`mywidget/mywidget.html` + `mywidget.css` (+ `mywidget.md` for the catalog). One **root
|
|
221
|
+
class**, variants as **attributes** (`[data-variant]`), and **read token slots**, so it
|
|
222
|
+
inherits any theme + dark mode for free. Author the markup in the §3 vocabulary
|
|
223
|
+
(`data-field` / `data-bind-*` / `each` / `<slot-tag>`).
|
|
224
|
+
2. Register the dir at your **composition root** so the host composes `<mywidget>` and bundles
|
|
225
|
+
its CSS: add it to the renderer's component roots + the style bundle + the catalog
|
|
226
|
+
(this repo: `componentRoots` / `styleRoots` in `tjakoen.github.io/config.ts`; a standalone consumer
|
|
227
|
+
passes its dir to `createRenderer` / the style bundle / `createCatalog`). It then appears
|
|
228
|
+
in `/catalog` automatically.
|
|
229
|
+
|
|
230
|
+
### C · Make it AI-operable (optional: the payoff)
|
|
231
|
+
|
|
232
|
+
Add the §2 conventions and the shared dispatcher drives it with **no bespoke JS**:
|
|
233
|
+
`data-surface="kind:id"` (its address), `data-kind` + `data-accepts="verb"` (harvested into the
|
|
234
|
+
manifest), a `data-action="verb"` trigger, and express AI-mode off `data-commit`/`data-grade`.
|
|
235
|
+
Register the verb in the vocabulary (`ai/contract.ts`, see the §5 rough-edge note). That's the
|
|
236
|
+
whole path: a themed, catalogued, AI-operable component without editing GRAIN.
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
🤖 **Built with Claude, on the same buttons you'd click.** I called the shots, Claude typed them,
|
|
241
|
+
and the AI never got a backdoor into the DOM, same door as you, every time. **I don't prompt and
|
|
242
|
+
pray, I prompt and prove.**
|
|
243
|
+
[How I actually work with AI, receipts and all →](https://tjakoen.github.io/notes/ten-times-zero)
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// grain/ai/accepts.test.ts — UNIT: component affordance harvester in isolation.
|
|
2
|
+
// Uses tmp files to simulate component HTML so no real fs state is needed.
|
|
3
|
+
import { test, expect } from "bun:test";
|
|
4
|
+
import { mkdirSync, writeFileSync, rmSync } from "fs";
|
|
5
|
+
import { join } from "path";
|
|
6
|
+
import { createAccepts } from "./accepts.ts";
|
|
7
|
+
|
|
8
|
+
const TMP = join(import.meta.dir, "__accepts_test_tmp__");
|
|
9
|
+
function setup(files: Record<string, string>) {
|
|
10
|
+
rmSync(TMP, { recursive: true, force: true });
|
|
11
|
+
mkdirSync(TMP, { recursive: true });
|
|
12
|
+
for (const [name, html] of Object.entries(files)) writeFileSync(join(TMP, name), html, "utf8");
|
|
13
|
+
}
|
|
14
|
+
function teardown() { rmSync(TMP, { recursive: true, force: true }); }
|
|
15
|
+
|
|
16
|
+
test("byKind: double-quoted attributes are harvested", () => {
|
|
17
|
+
setup({ "item.html": `<div data-kind="item" data-accepts="item.archive"></div>` });
|
|
18
|
+
const a = createAccepts(TMP);
|
|
19
|
+
expect(a.byKind()).toEqual({ item: ["item.archive"] });
|
|
20
|
+
teardown();
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
test("byKind: single-quoted attributes are harvested", () => {
|
|
24
|
+
setup({ "item.html": `<div data-kind='item' data-accepts='item.archive'></div>` });
|
|
25
|
+
const a = createAccepts(TMP);
|
|
26
|
+
expect(a.byKind()).toEqual({ item: ["item.archive"] });
|
|
27
|
+
teardown();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("byKind: multiple accepts are split on whitespace", () => {
|
|
31
|
+
setup({ "item.html": `<div data-kind="item" data-accepts="item.archive say.set"></div>` });
|
|
32
|
+
const a = createAccepts(TMP);
|
|
33
|
+
expect(a.byKind()["item"]).toEqual(["item.archive", "say.set"]);
|
|
34
|
+
teardown();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test("byKind: data-kind without data-accepts is excluded with a warning", () => {
|
|
38
|
+
const warns: string[] = [];
|
|
39
|
+
const orig = console.warn;
|
|
40
|
+
console.warn = (...args: unknown[]) => warns.push(String(args[0]));
|
|
41
|
+
setup({ "item.html": `<div data-kind="item"></div>` });
|
|
42
|
+
const a = createAccepts(TMP);
|
|
43
|
+
expect(a.byKind()["item"]).toBeUndefined();
|
|
44
|
+
expect(warns.some((w) => w.includes("[accepts]") && w.includes("data-kind"))).toBe(true);
|
|
45
|
+
console.warn = orig;
|
|
46
|
+
teardown();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
test("actions: data-action verbs are collected across components", () => {
|
|
50
|
+
setup({
|
|
51
|
+
"a.html": `<button data-action="item.archive"></button>`,
|
|
52
|
+
"b.html": `<input data-action="say.set">`,
|
|
53
|
+
});
|
|
54
|
+
const a = createAccepts(TMP);
|
|
55
|
+
const verbs = a.actions();
|
|
56
|
+
expect(verbs).toContain("item.archive");
|
|
57
|
+
expect(verbs).toContain("say.set");
|
|
58
|
+
teardown();
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test("refresh: clears cache so a second call re-reads files", () => {
|
|
62
|
+
setup({ "item.html": `<div data-kind="item" data-accepts="item.archive"></div>` });
|
|
63
|
+
const a = createAccepts(TMP);
|
|
64
|
+
expect(a.byKind()).toEqual({ item: ["item.archive"] });
|
|
65
|
+
writeFileSync(join(TMP, "item.html"), `<div data-kind="item" data-accepts="say.set"></div>`, "utf8");
|
|
66
|
+
a.refresh();
|
|
67
|
+
expect(a.byKind()).toEqual({ item: ["say.set"] });
|
|
68
|
+
teardown();
|
|
69
|
+
});
|
package/ai/accepts.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// /framework/render/accepts.ts — harvest component-declared affordances.
|
|
2
|
+
//
|
|
3
|
+
// Scans each component's .html for `data-kind` + `data-accepts` and returns a
|
|
4
|
+
// kind → action-names map. This is the single source the AI manifest reads, so it
|
|
5
|
+
// can't drift from what the components actually expose (https://tjakoen.github.io/grain/docs/ai-interface §4).
|
|
6
|
+
// Generic: it knows nothing about the app's specific verbs or surfaces.
|
|
7
|
+
import { readdirSync, readFileSync } from "fs";
|
|
8
|
+
import { join } from "path";
|
|
9
|
+
|
|
10
|
+
const KIND = /\bdata-kind=["']([^"']+)["']/;
|
|
11
|
+
const ACCEPTS = /\bdata-accepts=["']([^"']+)["']/;
|
|
12
|
+
// every wired trigger (global — a component may have several). The negative lookbehind skips
|
|
13
|
+
// the BINDING directive `data-bind-data-action="path"` (a bound view-model field, not a literal verb).
|
|
14
|
+
const ACTION = /(?<![-\w])data-action="([^"]+)"/g;
|
|
15
|
+
|
|
16
|
+
interface Harvest {
|
|
17
|
+
byKind: Record<string, string[]>;
|
|
18
|
+
actions: string[]; // distinct verbs any component WIRES via data-action
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface Accepts {
|
|
22
|
+
/** kind → declared action names, e.g. { item: ["item.archive"] }. */
|
|
23
|
+
byKind(): Record<string, string[]>;
|
|
24
|
+
/** every distinct verb wired via `data-action` in a component template (drift-guarded in server.ts). */
|
|
25
|
+
actions(): string[];
|
|
26
|
+
refresh(): void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export function createAccepts(componentsDir: string | string[]): Accepts {
|
|
30
|
+
let cache: Harvest | null = null;
|
|
31
|
+
|
|
32
|
+
function build(): Harvest {
|
|
33
|
+
const byKind: Record<string, string[]> = {};
|
|
34
|
+
const actions = new Set<string>();
|
|
35
|
+
function walk(dir: string) {
|
|
36
|
+
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
37
|
+
const full = join(dir, e.name);
|
|
38
|
+
if (e.isDirectory()) { walk(full); continue; }
|
|
39
|
+
if (!e.name.endsWith(".html")) continue;
|
|
40
|
+
const html = readFileSync(full, "utf8");
|
|
41
|
+
const kind = KIND.exec(html)?.[1];
|
|
42
|
+
const accepts = ACCEPTS.exec(html)?.[1];
|
|
43
|
+
if (kind && accepts) byKind[kind] = accepts.split(/\s+/).filter(Boolean);
|
|
44
|
+
else if (kind) console.warn(`[accepts] ${full}: data-kind="${kind}" found but no data-accepts — kind excluded from manifest`);
|
|
45
|
+
for (const m of html.matchAll(ACTION)) actions.add(m[1]); // data-action="verb" on any control
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
for (const root of ([] as string[]).concat(componentsDir)) walk(root); // one or many roots
|
|
49
|
+
return { byKind, actions: [...actions] };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
byKind() { return (cache ??= build()).byKind; },
|
|
54
|
+
actions() { return (cache ??= build()).actions; },
|
|
55
|
+
refresh() { cache = null; },
|
|
56
|
+
};
|
|
57
|
+
}
|