@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/ai/reasoner.ts
ADDED
|
@@ -0,0 +1,446 @@
|
|
|
1
|
+
// /app/ai/reasoner.ts — the reasoner boundary (the PROJECT-PLAN §10c `Model` seam)
|
|
2
|
+
// plus a stub standing in for the real cloud/local model during frontend work.
|
|
3
|
+
//
|
|
4
|
+
// The reasoner is the SINGLE WRITER: it decides, then writes + renders through
|
|
5
|
+
// SCOPED TOOLS (its action vocabulary). Swapping the stub for a real model later is
|
|
6
|
+
// an implementation change behind this interface — not a rewrite. The stub is
|
|
7
|
+
// PLUMBING, never faked judgment (MVP §"Build Order").
|
|
8
|
+
|
|
9
|
+
import type { Intent, Decision, Surface, RenderOp } from "./contract.ts";
|
|
10
|
+
import { ACTIONS, PUSH_SURFACES, surfaceId, isSafeNavigateHref, isSafeFieldValue, FIELD_VALUE_CAP } from "./contract.ts";
|
|
11
|
+
// The reusable reasoner primitives — the stub DOGFOODS them so the exported kit (what a consumer's
|
|
12
|
+
// real model composes with) and the shipped chat markup can never drift apart.
|
|
13
|
+
import { esc, chatBubble, narrationLine, navigateOp, noteAppendOp, noteReplaceOp, fillOp } from "./reasoner-kit.ts";
|
|
14
|
+
|
|
15
|
+
// The scoped capabilities the reasoner is allowed to use — its tool surface. The
|
|
16
|
+
// real reasoner reaches storage through least-privilege tools exactly like this.
|
|
17
|
+
export interface ReasonTools {
|
|
18
|
+
archiveItem(id: string): Promise<void>;
|
|
19
|
+
/** Render the COMMITTED (clean) HTML for a surface, after a write. */
|
|
20
|
+
renderSurface(surface: Surface): Promise<string>;
|
|
21
|
+
/** Push a render op NOW (mid-decision) — used to stream tokens as they're made. */
|
|
22
|
+
emit(op: RenderOp): void;
|
|
23
|
+
/** True if the user asked the AI to stop — checked between steps for a graceful halt. */
|
|
24
|
+
cancelled(): boolean;
|
|
25
|
+
/** Co-operative delay so the heavy path / thinking-state is exercised. */
|
|
26
|
+
delay(ms: number): Promise<void>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface Reasoner {
|
|
30
|
+
decide(intent: Intent, tools: ReasonTools): Promise<Decision>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// One calm, deliberate rhythm for ALL AI motion — same beats everywhere so the flow
|
|
34
|
+
// reads consistently (e-ink implies stillness, not a teletype burst).
|
|
35
|
+
const TYPE_MS = 32; // per character while typing
|
|
36
|
+
const BACK_MS = 16; // per character while ERASING — quicker than typing, reads as scrubbing
|
|
37
|
+
const SETTLE_MS = 700; // after the spotlight lands on a surface, before it acts (≥ the focus glide, --ai-focus-move)
|
|
38
|
+
const HOLD_MS = 650; // after an action finishes, before attention moves on
|
|
39
|
+
|
|
40
|
+
export interface StubOptions {
|
|
41
|
+
/** 0 = always commit; 1 = always fail (exercises rollback). */
|
|
42
|
+
failRate?: number;
|
|
43
|
+
/** ms the stub "thinks" before acting; 0 in tests. */
|
|
44
|
+
thinkMs?: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function makeStubReasoner(opts: StubOptions = {}): Reasoner {
|
|
48
|
+
const failRate = opts.failRate ?? 0;
|
|
49
|
+
const thinkMs = opts.thinkMs ?? 250;
|
|
50
|
+
let chatSeq = 0; // unique id per streamed AI chat bubble (DOM surface to type into)
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
async decide(intent: Intent, tools: ReasonTools): Promise<Decision> {
|
|
54
|
+
const def = ACTIONS[intent.action];
|
|
55
|
+
|
|
56
|
+
const beat = (ms: number) => tools.delay(thinkMs > 0 ? ms : 0);
|
|
57
|
+
|
|
58
|
+
// Spotlight the surface the AI is touching — the "AI is acting" treatment.
|
|
59
|
+
// on = pending (in progress); off = committed (done → also releases the trigger).
|
|
60
|
+
const spot = (target: string, active: boolean, click = false) =>
|
|
61
|
+
tools.emit({ target, op: "spotlight", active, click, provenance: "ai", commit: active ? "pending" : "committed" });
|
|
62
|
+
|
|
63
|
+
// Move attention to a surface, then let it SETTLE before acting — the one
|
|
64
|
+
// consistent transition used everywhere (so no step feels faster than another).
|
|
65
|
+
const moveTo = async (target: string, click = false) => { spot(target, true, click); await beat(SETTLE_MS); };
|
|
66
|
+
|
|
67
|
+
// Type a line out, ONE CHARACTER at a time over SSE at a single steady cadence,
|
|
68
|
+
// so every typing effect reads identically regardless of word length. Each char
|
|
69
|
+
// arrives at grain with a caret; `done` settles it to clean (DESIGN-SYSTEM §3).
|
|
70
|
+
// Shared by both text verbs so the input and the button feel the same.
|
|
71
|
+
const stream = async (surface: string, line: string, done = true): Promise<Decision> => {
|
|
72
|
+
for (const ch of [...line]) { // spread = per code point (handles "—")
|
|
73
|
+
if (tools.cancelled()) break; // stop promptly at a clean boundary…
|
|
74
|
+
await tools.delay(thinkMs > 0 ? TYPE_MS : 0);
|
|
75
|
+
tools.emit({ target: surface, op: "type", text: ch, provenance: "ai", commit: "pending" });
|
|
76
|
+
}
|
|
77
|
+
if (done) tools.emit({ target: surface, op: "type", done: true, provenance: "ai", commit: "committed" }); // settle / submit
|
|
78
|
+
return { ok: true, ops: [], reply: line };
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// Narrate a step to the bottom console as an ACTION BADGE — the AI's verb vocabulary
|
|
82
|
+
// (contract.ts) made visible. The shell surfaces this feed while it takes over.
|
|
83
|
+
const narrate = (verb: string, desc: string) =>
|
|
84
|
+
tools.emit({ target: PUSH_SURFACES.console, op: "append", provenance: "ai", commit: "committed",
|
|
85
|
+
html: narrationLine(verb, desc) });
|
|
86
|
+
const clearConsole = () =>
|
|
87
|
+
tools.emit({ target: PUSH_SURFACES.console, op: "replace", provenance: "ai", commit: "committed",
|
|
88
|
+
html: `<div class="console__feed" data-surface="console"></div>` });
|
|
89
|
+
|
|
90
|
+
// One chat bubble, one place — your message (clean, human) or the AI's (grain, provenance
|
|
91
|
+
// persists). Shared by chat.send AND the demo scenarios so the markup never diverges. `who`
|
|
92
|
+
// defaults from the role ("You"/"AI"); pass it to override (the /grain showcase signs "GRAIN").
|
|
93
|
+
const bubble = chatBubble; // dogfood the exported kit — one definition, no drift
|
|
94
|
+
|
|
95
|
+
// --- say.stream: button → the AI types a reflection. Spotlit: a human clicked, but
|
|
96
|
+
// the AI is the one writing, so show where it acts (grade = AI as actor, §5c). ---
|
|
97
|
+
if (intent.action === "say.stream") {
|
|
98
|
+
clearConsole(); narrate("reads", "checking your week");
|
|
99
|
+
await moveTo(intent.surface);
|
|
100
|
+
const d = await stream(intent.surface, "On it — checking your week. You have room on Thursday.");
|
|
101
|
+
await beat(HOLD_MS);
|
|
102
|
+
spot(intent.surface, false);
|
|
103
|
+
return d;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// --- say.set: input → the AI writes back the line it noted from your text ---
|
|
107
|
+
if (intent.action === "say.set") {
|
|
108
|
+
const text = String(intent.payload.text ?? "").trim();
|
|
109
|
+
clearConsole(); narrate(text ? "writes" : "reads", text ? "noting your request" : "listening");
|
|
110
|
+
await moveTo(intent.surface);
|
|
111
|
+
const d = await stream(intent.surface, text ? `Noted: ${text}` : "Nothing to note.");
|
|
112
|
+
await beat(HOLD_MS);
|
|
113
|
+
spot(intent.surface, false);
|
|
114
|
+
return d;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// --- navigate: change screens. Minimal on purpose — this stub is PLUMBING (file header), so
|
|
118
|
+
// it only proves the mechanism (a reasoner CAN emit a validated navigate op) rather than
|
|
119
|
+
// deciding where to go; a real reasoner supplies the judgment. The href travels in the
|
|
120
|
+
// payload (a control wiring data-action="navigate" would set it via data-payload or
|
|
121
|
+
// equivalent); an unsafe/missing one fails the request rather than the dispatcher, per
|
|
122
|
+
// CLAUDE.md lesson #3/#5 (contracts must not fail silently). ---
|
|
123
|
+
if (intent.action === "navigate") {
|
|
124
|
+
const href = intent.payload.href;
|
|
125
|
+
if (!isSafeNavigateHref(href)) {
|
|
126
|
+
return {
|
|
127
|
+
ok: false,
|
|
128
|
+
reason: "stub: missing or unsafe navigate href",
|
|
129
|
+
ops: [{ target: intent.surface, op: "flash", message: "Couldn't navigate — invalid destination.",
|
|
130
|
+
provenance: "system", commit: "committed" }],
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
return { ok: true, ops: [navigateOp(intent.surface, href)] };
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// --- field.set: prefill a registered form field with drafted text (plans/field-set-op.md).
|
|
137
|
+
// The value is validated HERE (the reject echoes the constraint so a reasoner can shorten
|
|
138
|
+
// and retry — informative rejections, AI-INTERFACE §0); the emitted `fill` op persists for
|
|
139
|
+
// human review. The AI never submits — no submit verb exists. The door already brackets an
|
|
140
|
+
// ai-sourced intent with spotlight ops + timeline entries; nothing extra needed here. ---
|
|
141
|
+
if (intent.action === "field.set") {
|
|
142
|
+
const value = intent.payload.value;
|
|
143
|
+
// Empty rejects too (like an empty note — lesson #3): a prefill with nothing in it is a
|
|
144
|
+
// silent no-op to the eye; "clear the field" is not a move this verb offers.
|
|
145
|
+
if (!isSafeFieldValue(value) || !value.trim()) {
|
|
146
|
+
const why = typeof value !== "string" || !value.trim() ? "value must be non-empty plain text"
|
|
147
|
+
: value.length > FIELD_VALUE_CAP ? `over ${FIELD_VALUE_CAP} chars`
|
|
148
|
+
: "control characters not allowed";
|
|
149
|
+
return {
|
|
150
|
+
ok: false,
|
|
151
|
+
reason: `field.set value rejected: ${why}`,
|
|
152
|
+
ops: [{ target: intent.surface, op: "flash", message: "Couldn't fill that field.",
|
|
153
|
+
provenance: "system", commit: "committed" }],
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
narrate("fills", `prefilling ${surfaceId(intent.surface) || intent.surface} — yours to review and send`);
|
|
157
|
+
return { ok: true, reply: "Drafted it into the field — review and send when ready.",
|
|
158
|
+
ops: [fillOp(intent.surface, value)] };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// --- chat.send: the assistant conversation. Your message settles CLEAN (human), then
|
|
162
|
+
// the AI's reply STREAMS into a fresh bubble that stays GRAIN (AI). Same door,
|
|
163
|
+
// same ops — no chat-specific machinery. User text is escaped at the writer. ---
|
|
164
|
+
if (intent.action === "chat.send") {
|
|
165
|
+
const text = String(intent.payload.text ?? "").trim();
|
|
166
|
+
// append to the chat-log the intent TARGETED (the shell's "chat-log", or a page's own
|
|
167
|
+
// "chat-log:<id>" — same kind), so multiple chat surfaces on a page don't collide.
|
|
168
|
+
const log = intent.surface;
|
|
169
|
+
// 1) your message — clean (human, committed)
|
|
170
|
+
tools.emit({ target: log, op: "append", provenance: "user", commit: "committed",
|
|
171
|
+
html: bubble("you", "", `<span class="chat-message__body">${esc(text || "…")}</span>`) });
|
|
172
|
+
// 2) an empty AI bubble to stream into — grain (AI), pending until it settles
|
|
173
|
+
const id = `chat-msg:${++chatSeq}`;
|
|
174
|
+
tools.emit({ target: log, op: "append", provenance: "ai", commit: "pending",
|
|
175
|
+
html: bubble("ai", "grain", `<span class="chat-message__body" data-surface="${id}"></span>`) });
|
|
176
|
+
// 3) the AI thinks a beat, then types its reply into the bubble (grain persists)
|
|
177
|
+
await beat(HOLD_MS);
|
|
178
|
+
const reply = text ? `Noted — “${text}”. I'll fold that into your plan.` : "I'm here — what would you like to plan?";
|
|
179
|
+
return stream(id, reply);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// --- note.append / note.replace: the notepad — the AI's memory as a visible, editable
|
|
183
|
+
// surface, and the strongest symmetry demo (DEMO-PLAN piece 2). The SAME verbs serve
|
|
184
|
+
// both operators through the one door: the AI appends a digest it wrote; a human's
|
|
185
|
+
// explicit "commit" rewrites the pad with note.replace. Provenance (intent.source) drives
|
|
186
|
+
// the grade — the AI's entry stays grain, the human's settles clean. The op lands on the
|
|
187
|
+
// `notepad-body` surface; the client island mirrors the source to localStorage. An empty
|
|
188
|
+
// note fails rather than writing a blank fact (lesson #3: never fail silently). ---
|
|
189
|
+
if (intent.action === "note.append" || intent.action === "note.replace") {
|
|
190
|
+
const text = String(intent.payload.text ?? "").trim();
|
|
191
|
+
if (!text) {
|
|
192
|
+
return {
|
|
193
|
+
ok: false,
|
|
194
|
+
reason: "note: empty text",
|
|
195
|
+
ops: [{ target: intent.surface, op: "flash", message: "Nothing to note.",
|
|
196
|
+
provenance: "system", commit: "committed" }],
|
|
197
|
+
};
|
|
198
|
+
}
|
|
199
|
+
const from = intent.source; // "user" | "ai" — provenance persists onto the entry's grade
|
|
200
|
+
const op = intent.action === "note.append" ? noteAppendOp(text, from) : noteReplaceOp(text, from);
|
|
201
|
+
// The AI narrates its own writes (legible run); a human commit needs no narration.
|
|
202
|
+
if (from === "ai")
|
|
203
|
+
narrate(intent.action === "note.append" ? "notes" : "rewrites",
|
|
204
|
+
intent.action === "note.append" ? "adding to the notepad" : "rewriting the notepad");
|
|
205
|
+
return {
|
|
206
|
+
ok: true,
|
|
207
|
+
reply: from === "ai"
|
|
208
|
+
? (intent.action === "note.append" ? "Added that to the notepad." : "Rewrote the notepad.")
|
|
209
|
+
: undefined,
|
|
210
|
+
ops: [op],
|
|
211
|
+
};
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// --- demo.run: a useful end-to-end scenario the user can WATCH — the AI drafts a
|
|
215
|
+
// Thursday plan as a bullet LIST, then REVISES one specific item (backspacing it,
|
|
216
|
+
// retyping) to show targeted editing. The spotlight follows what it touches; the
|
|
217
|
+
// backdrop stays up across the turn, then releases (AI-INTERFACE §5c).
|
|
218
|
+
// Re-runnable: it drives demo surfaces with render ops and mutates no storage. ---
|
|
219
|
+
if (intent.action === "demo.run") {
|
|
220
|
+
const handBack: Decision = { ok: true, ops: [], reply: "Stopped — handed back to you." };
|
|
221
|
+
// graceful stop: hand back cleanly if asked — never a force-kill (PROJECT-PLAN §9).
|
|
222
|
+
const stopped = (): boolean => { if (!tools.cancelled()) return false; spot("screen", false); return true; };
|
|
223
|
+
|
|
224
|
+
// --- /grain showcase scenario: the SAME door drives the showcase surface. This is the
|
|
225
|
+
// page whose whole claim is "no privileged AI→DOM back channel" — so its "Watch the
|
|
226
|
+
// AI act" demo goes through POST /intent → RenderOps over SSE, exactly like /loop, and
|
|
227
|
+
// targets the /grain composition's own surface addresses. (Stub-demo choreography; the
|
|
228
|
+
// live model at M★ reads the manifest instead of hard-coded surfaces.) ---
|
|
229
|
+
if (intent.screen === "grain") {
|
|
230
|
+
// Narrate every step to the terminal so a run is always LEGIBLE (never reads as "stuck").
|
|
231
|
+
// The /grain page shows a console feed (data-surface="console") while acting.
|
|
232
|
+
clearConsole();
|
|
233
|
+
|
|
234
|
+
// 1) read the surface
|
|
235
|
+
narrate("reads", "checking today's tasks");
|
|
236
|
+
await moveTo("grain-rail");
|
|
237
|
+
await beat(HOLD_MS);
|
|
238
|
+
if (stopped()) return handBack;
|
|
239
|
+
|
|
240
|
+
// 2) compose a request in the Ask field like a human, then "submit" (clears it)
|
|
241
|
+
narrate("types", "composing “Plan Thursday…”");
|
|
242
|
+
await moveTo("grain-ask");
|
|
243
|
+
await stream("grain-ask", "Plan Thursday around the review", false);
|
|
244
|
+
await beat(HOLD_MS);
|
|
245
|
+
tools.emit({ target: "grain-ask", op: "type", done: true, provenance: "ai", commit: "committed" });
|
|
246
|
+
if (stopped()) return handBack;
|
|
247
|
+
|
|
248
|
+
// 3) reply into the surface's OWN chat (chat-log:grain — visible in the main pane during
|
|
249
|
+
// the takeover, since the shell assistant slides away). A fresh grain bubble.
|
|
250
|
+
narrate("writes", "replying in the thread");
|
|
251
|
+
await moveTo("chat-log:grain");
|
|
252
|
+
tools.emit({ target: "chat-log:grain", op: "append", provenance: "ai", commit: "pending",
|
|
253
|
+
html: bubble("ai", "grain", `<span class="chat-message__body" data-surface="grain-reply"></span>`, "GRAIN") });
|
|
254
|
+
await stream("grain-reply", "On it — three deep-work blocks, review at 2.");
|
|
255
|
+
await beat(HOLD_MS);
|
|
256
|
+
if (stopped()) return handBack;
|
|
257
|
+
|
|
258
|
+
// 4) complete a task — a click that flips its badge to done (committed)
|
|
259
|
+
narrate("commits", "completing “Draft the Q3 plan”");
|
|
260
|
+
await moveTo("grain-task", true);
|
|
261
|
+
tools.emit({ target: "grain-task-badge", op: "replace", provenance: "ai", commit: "committed",
|
|
262
|
+
html: `<span class="badge" data-status="archived" data-surface="grain-task-badge">done</span>` });
|
|
263
|
+
await beat(HOLD_MS);
|
|
264
|
+
if (stopped()) return handBack;
|
|
265
|
+
|
|
266
|
+
// 5) draft a follow-up task of its own — AI-authored, so it STAYS grain
|
|
267
|
+
narrate("types", "drafting a follow-up");
|
|
268
|
+
tools.emit({ target: "grain-tasks", op: "append", provenance: "ai", commit: "pending",
|
|
269
|
+
html: `<li class="list__item" data-grade="grain" data-surface="grain-draft"></li>` });
|
|
270
|
+
await moveTo("grain-draft");
|
|
271
|
+
await stream("grain-draft", "Block 9–11am for deep work");
|
|
272
|
+
await beat(HOLD_MS);
|
|
273
|
+
|
|
274
|
+
// 6) hand back
|
|
275
|
+
narrate("done", "handed back to you");
|
|
276
|
+
spot("screen", false);
|
|
277
|
+
return { ok: true, ops: [], reply: "(demo) the AI read, replied, completed a task, and drafted one — all through the door." };
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// --- /notes screen: "What have you been up to?" — the AI travels the newest notes
|
|
281
|
+
// (real content, addressed by MILL's data-surface="note:<slug>", mill/serve.ts
|
|
282
|
+
// itemSurfacePrefix), then writes a digest into the SIDEBAR CHAT (owner feedback,
|
|
283
|
+
// NOTES-PAGE-PLAN.md 2026-07-07) — like anything else the AI says to you, not a
|
|
284
|
+
// standalone card. A fresh grain bubble in the global "chat-log", same idiom as
|
|
285
|
+
// chat.send's AI reply. Stub-demo choreography, hardcoded to today's newest three
|
|
286
|
+
// (same bar as the /grain scenario above); the live model at M★ reads the manifest. ---
|
|
287
|
+
if (intent.screen === "notes") {
|
|
288
|
+
clearConsole();
|
|
289
|
+
narrate("reads", "checking the notebook");
|
|
290
|
+
|
|
291
|
+
const recent: [slug: string, title: string][] = [
|
|
292
|
+
["feels-like-an-app", "This Site Feels Like an App"],
|
|
293
|
+
["the-browser-grew-up", "The Browser Grew Up"],
|
|
294
|
+
["how-i-turned-github-into-a-classroom", "How I Turned a GitHub Org Into My Classroom"],
|
|
295
|
+
];
|
|
296
|
+
for (const [slug, title] of recent) {
|
|
297
|
+
narrate("reads", title);
|
|
298
|
+
await moveTo(`note:${slug}`);
|
|
299
|
+
await beat(HOLD_MS);
|
|
300
|
+
if (stopped()) return handBack;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
narrate("writes", "summarising what's new");
|
|
304
|
+
await moveTo("chat-log");
|
|
305
|
+
tools.emit({ target: "chat-log", op: "append", provenance: "ai", commit: "pending",
|
|
306
|
+
html: bubble("ai", "grain", `<span class="chat-message__body" data-surface="notes-digest"></span>`) });
|
|
307
|
+
await stream("notes-digest",
|
|
308
|
+
"New this week: an app-like feel for the site, the case for betting on the browser, and a GitHub-native classroom.");
|
|
309
|
+
await beat(HOLD_MS);
|
|
310
|
+
spot("screen", false);
|
|
311
|
+
return { ok: true, ops: [], reply: "(demo) the AI read the newest notes and wrote a digest into the chat." };
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// Write a committed VALUE into a surface: stream it in grain (you watch it type),
|
|
315
|
+
// then settle to the CLEAN committed HTML. Speech stays grain; ground-truth data
|
|
316
|
+
// settles clean — the same grain→clean an item.archive shows (AI-INTERFACE §5).
|
|
317
|
+
const commitText = async (surface: string, grainLine: string, cleanHtml: string) => {
|
|
318
|
+
await moveTo(surface);
|
|
319
|
+
await stream(surface, grainLine, false); // type in grain, no settle-to-grain
|
|
320
|
+
await beat(HOLD_MS);
|
|
321
|
+
tools.emit({ target: surface, op: "replace", html: cleanHtml, provenance: "ai", commit: "committed" });
|
|
322
|
+
};
|
|
323
|
+
// Append a bullet to the plan list, then write its text and settle it clean.
|
|
324
|
+
const liHtml = (n: number, text: string) => `<li class="list__item" data-surface="plan-item:${n}">${text}</li>`;
|
|
325
|
+
const addBullet = async (n: number, text: string) => {
|
|
326
|
+
tools.emit({ target: PUSH_SURFACES.plan, op: "append", html: liHtml(n, ""), provenance: "ai", commit: "pending" });
|
|
327
|
+
await commitText(`plan-item:${n}`, text, liHtml(n, text));
|
|
328
|
+
};
|
|
329
|
+
// REVISE an already-written surface: backspace the old text one char at a time
|
|
330
|
+
// (the AI thinking again), then type the replacement and settle it clean.
|
|
331
|
+
const overwrite = async (surface: string, oldText: string, newText: string, cleanHtml: string) => {
|
|
332
|
+
await moveTo(surface);
|
|
333
|
+
for (let i = 0; i < [...oldText].length; i++) {
|
|
334
|
+
if (tools.cancelled()) break;
|
|
335
|
+
await tools.delay(thinkMs > 0 ? BACK_MS : 0); // erase faster than we type
|
|
336
|
+
tools.emit({ target: surface, op: "type", back: 1, provenance: "ai", commit: "pending" });
|
|
337
|
+
}
|
|
338
|
+
await beat(220); // a pause on the empty line — "reconsidering"
|
|
339
|
+
await stream(surface, newText, false); // retype in grain
|
|
340
|
+
await beat(HOLD_MS);
|
|
341
|
+
tools.emit({ target: surface, op: "replace", html: cleanHtml, provenance: "ai", commit: "committed" });
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
clearConsole();
|
|
345
|
+
narrate("reads", "checking what's in view — 3 tasks");
|
|
346
|
+
|
|
347
|
+
// 1) compose the request in the input like a human would, then "submit"
|
|
348
|
+
await moveTo(PUSH_SURFACES.askInput);
|
|
349
|
+
narrate("types", 'composing "Plan my Thursday"');
|
|
350
|
+
await stream(PUSH_SURFACES.askInput, "Plan my Thursday", false);
|
|
351
|
+
await beat(HOLD_MS);
|
|
352
|
+
tools.emit({ target: PUSH_SURFACES.askInput, op: "type", done: true, provenance: "ai", commit: "committed" }); // Enter → clears it
|
|
353
|
+
if (stopped()) return handBack;
|
|
354
|
+
|
|
355
|
+
// 2) reflect — the AI SPEAKING, so it stays grain
|
|
356
|
+
await moveTo("reflection");
|
|
357
|
+
narrate("writes", "reflecting on your week");
|
|
358
|
+
await stream("reflection", "Thursday's light — 09:00–11:00 is free. Here's a plan…");
|
|
359
|
+
await beat(HOLD_MS);
|
|
360
|
+
if (stopped()) return handBack;
|
|
361
|
+
|
|
362
|
+
// 3) write the plan as a bullet list — each item commits to clean
|
|
363
|
+
narrate("writes", "drafting a 4-line plan");
|
|
364
|
+
await addBullet(1, "Deep-work block — 09:00–11:00");
|
|
365
|
+
if (stopped()) return handBack;
|
|
366
|
+
await addBullet(2, "Clear the inbox");
|
|
367
|
+
if (stopped()) return handBack;
|
|
368
|
+
await addBullet(3, "Review architecture doc");
|
|
369
|
+
if (stopped()) return handBack;
|
|
370
|
+
|
|
371
|
+
// 4) revise just the 3rd item — backspace it, retype (targeted edit + flexibility)
|
|
372
|
+
narrate("revises", "line 3 — deep-work done");
|
|
373
|
+
await overwrite("plan-item:3", "Review architecture doc", "Review doc — done, archived",
|
|
374
|
+
liHtml(3, "Review doc — done, archived"));
|
|
375
|
+
if (stopped()) return handBack;
|
|
376
|
+
|
|
377
|
+
// 5) triage today's task CARDS — spotlight each (it goes grain), flip its b-badge,
|
|
378
|
+
// settle clean. Drives the loop-card + the demo card + badge, all AI-driven.
|
|
379
|
+
const badge = (surface: string, status: string, label: string) =>
|
|
380
|
+
`<span class="badge" data-status="${status}" data-surface="${surface}">${label}</span>`;
|
|
381
|
+
// archive the finished task — a REAL write through the scoped tool (not a cosmetic flip):
|
|
382
|
+
// the AI mutates state via the service, then renders the COMMITTED card back over SSE.
|
|
383
|
+
// Re-runnable: archiveItem is idempotent, so replaying re-archives the same demo fixture
|
|
384
|
+
// (a harmless no-op write). The id is a demo fixture; the live model reads the manifest.
|
|
385
|
+
const reviewSurface = "item:ITM-demo-1";
|
|
386
|
+
narrate("clicks", "archiving “Review doc”");
|
|
387
|
+
await moveTo(reviewSurface, true); // the finished one → click to archive it
|
|
388
|
+
await tools.archiveItem(surfaceId(reviewSurface));
|
|
389
|
+
tools.emit({ target: reviewSurface, op: "replace", html: await tools.renderSurface(reviewSurface), provenance: "ai", commit: "committed" });
|
|
390
|
+
await beat(HOLD_MS);
|
|
391
|
+
if (stopped()) return handBack;
|
|
392
|
+
narrate("clicks", "scheduling the deep-work block");
|
|
393
|
+
await moveTo("item:ITM-demo-2"); // the deep-work one → schedule it
|
|
394
|
+
tools.emit({ target: PUSH_SURFACES.demoTaskBadge, op: "replace", html: badge(PUSH_SURFACES.demoTaskBadge, "active", "Thu 09:00"), provenance: "ai", commit: "committed" });
|
|
395
|
+
await beat(HOLD_MS);
|
|
396
|
+
if (stopped()) return handBack;
|
|
397
|
+
|
|
398
|
+
// 6) press Commit to finalise — a button the AI CLICKS (pulse), like a human (b-button)
|
|
399
|
+
narrate("commits", "committing the plan");
|
|
400
|
+
await moveTo("commit-btn", true);
|
|
401
|
+
await beat(SETTLE_MS);
|
|
402
|
+
if (stopped()) return handBack;
|
|
403
|
+
|
|
404
|
+
// 7) summarise — speech again (grain) — then hand back
|
|
405
|
+
narrate("writes", "summarising");
|
|
406
|
+
await moveTo(PUSH_SURFACES.summary);
|
|
407
|
+
await stream(PUSH_SURFACES.summary, "Plan's set — deep-work at nine, one already done. Two to go.");
|
|
408
|
+
await beat(HOLD_MS);
|
|
409
|
+
spot("screen", false); // hand back to you
|
|
410
|
+
return { ok: true, ops: [], reply: "(demo) the AI planned, triaged, committed, then handed back." };
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
// Even the light path takes a beat so the optimistic grain state is visible;
|
|
414
|
+
// a real heavy path would push a "thinking" flash here first.
|
|
415
|
+
await tools.delay(def.depth === "heavy" ? thinkMs * 3 : thinkMs);
|
|
416
|
+
|
|
417
|
+
// Simulated refusal / failed write → rollback (MVP §"Rollback").
|
|
418
|
+
const failed = failRate >= 1 || (failRate > 0 && Math.random() < failRate);
|
|
419
|
+
if (failed) {
|
|
420
|
+
return {
|
|
421
|
+
ok: false,
|
|
422
|
+
reason: "stub: simulated write failure",
|
|
423
|
+
reply: "Couldn't complete that — left it as it was.",
|
|
424
|
+
ops: [{
|
|
425
|
+
target: intent.surface, op: "flash",
|
|
426
|
+
message: "Couldn't archive — try again.",
|
|
427
|
+
provenance: "system", commit: "committed",
|
|
428
|
+
}],
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// item.archive: commit the write, then emit the confirmed (clean) fragment.
|
|
433
|
+
const id = surfaceId(intent.surface);
|
|
434
|
+
await tools.archiveItem(id);
|
|
435
|
+
const html = await tools.renderSurface(intent.surface);
|
|
436
|
+
return {
|
|
437
|
+
ok: true,
|
|
438
|
+
reply: "Archived.",
|
|
439
|
+
ops: [{
|
|
440
|
+
target: intent.surface, op: "replace", html,
|
|
441
|
+
provenance: "ai", commit: "committed",
|
|
442
|
+
}],
|
|
443
|
+
};
|
|
444
|
+
},
|
|
445
|
+
};
|
|
446
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// grain/ai/timeline-log.test.ts — the LogSink that renders crossings into the live timeline.
|
|
2
|
+
import { test, expect } from "bun:test";
|
|
3
|
+
import { createStreamLogSink, timelineEntryHtml } from "./timeline-log.ts";
|
|
4
|
+
import { OP_EVENT, PUSH_SURFACES } from "./contract.ts";
|
|
5
|
+
import type { OpChannel, RenderOp, LogEntry } from "./contract.ts";
|
|
6
|
+
|
|
7
|
+
function fakeStream() {
|
|
8
|
+
const pushed: Array<{ session: string; event: string; data: unknown }> = [];
|
|
9
|
+
const stream: OpChannel = { push: (session, event, data) => { pushed.push({ session, event, data }); } };
|
|
10
|
+
return { stream, pushed };
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const entry = (over: Partial<LogEntry> = {}): LogEntry => ({
|
|
14
|
+
session: "sess-1", source: "user", kind: "intent",
|
|
15
|
+
screen: "grain", surface: "item:ITM-1", action: "item.archive", ok: true, ops: 0, ...over,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
test("records a `log` op at the timeline surface, on the entry's session, colour-coded by provenance", () => {
|
|
19
|
+
const { stream, pushed } = fakeStream();
|
|
20
|
+
const sink = createStreamLogSink(stream);
|
|
21
|
+
sink.record(entry({ source: "ai", kind: "response", ops: 2 }));
|
|
22
|
+
|
|
23
|
+
expect(pushed).toHaveLength(1);
|
|
24
|
+
expect(pushed[0]!.session).toBe("sess-1");
|
|
25
|
+
expect(pushed[0]!.event).toBe(OP_EVENT);
|
|
26
|
+
const op = pushed[0]!.data as RenderOp;
|
|
27
|
+
expect(op.op).toBe("log");
|
|
28
|
+
expect(op.target).toBe(PUSH_SURFACES.timeline);
|
|
29
|
+
expect(op.provenance).toBe("ai"); // the client colours the entry off this
|
|
30
|
+
expect(op.commit).toBe("committed"); // a log line is a fact, never optimistic
|
|
31
|
+
expect(op.html).toContain('data-provenance="ai"');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("entry markup carries provenance, kind, and a monotonic seq; escapes untrusted surface text", () => {
|
|
35
|
+
const html = timelineEntryHtml(entry({ surface: 'item:<img src=x>"' }), 7);
|
|
36
|
+
expect(html).toContain('data-kind="intent"');
|
|
37
|
+
expect(html).toContain('data-seq="7"');
|
|
38
|
+
expect(html).not.toContain("<img"); // surface text is escaped, never injected
|
|
39
|
+
expect(html).toContain("<img");
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
test("a failed crossing is marked so the component can strike it through", () => {
|
|
43
|
+
const html = timelineEntryHtml(entry({ kind: "response", ok: false, ops: 1 }), 1);
|
|
44
|
+
expect(html).toContain('data-ok="false"');
|
|
45
|
+
expect(html).toContain("✗");
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
test("seq increments across records (newest is highest)", () => {
|
|
49
|
+
const { stream, pushed } = fakeStream();
|
|
50
|
+
const sink = createStreamLogSink(stream);
|
|
51
|
+
sink.record(entry());
|
|
52
|
+
sink.record(entry());
|
|
53
|
+
expect((pushed[0]!.data as RenderOp).html).toContain('data-seq="1"');
|
|
54
|
+
expect((pushed[1]!.data as RenderOp).html).toContain('data-seq="2"');
|
|
55
|
+
});
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// grain/ai/timeline-log.ts — the interaction TIMELINE, made visible (AI-INTERFACE §5g).
|
|
2
|
+
//
|
|
3
|
+
// A LogSink implementation that turns each recorded door crossing (contract.ts LogEntry) into a
|
|
4
|
+
// `log` RenderOp pushed to the `timeline` surface, so the unified human-and-AI log renders live.
|
|
5
|
+
// It depends only on the OpChannel port — like any other push surface, it reaches the client the
|
|
6
|
+
// same way render ops do. Another product on GRAIN would wire this exactly as the console is wired;
|
|
7
|
+
// nothing product-specific lives here. (The audit-journal / observability sink is a SEPARATE LogSink
|
|
8
|
+
// impl — the door records to whichever one the composition root wires.)
|
|
9
|
+
|
|
10
|
+
import type { LogSink, LogEntry, OpChannel, RenderOp } from "./contract.ts";
|
|
11
|
+
import { PUSH_SURFACES, OP_EVENT } from "./contract.ts";
|
|
12
|
+
|
|
13
|
+
const esc = (s: string) =>
|
|
14
|
+
s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
|
|
15
|
+
|
|
16
|
+
// The one-line entry markup — the `timeline` component's row (its CSS colours + grades it by
|
|
17
|
+
// [data-provenance]). Built server-side, like the reasoner builds its console__line / chat-message
|
|
18
|
+
// fragments (BATCH: the server owns rendering; the client stays dumb).
|
|
19
|
+
export function timelineEntryHtml(entry: LogEntry, seq: number): string {
|
|
20
|
+
const mark = entry.kind === "response" ? (entry.ok ? "✓" : "✗") : "→";
|
|
21
|
+
const detail = entry.kind === "response"
|
|
22
|
+
? `${entry.ops} op${entry.ops === 1 ? "" : "s"}`
|
|
23
|
+
: esc(entry.surface);
|
|
24
|
+
return (
|
|
25
|
+
`<li class="timeline__entry" data-provenance="${entry.source}" data-kind="${entry.kind}"` +
|
|
26
|
+
(entry.ok ? "" : ` data-ok="false"`) + ` data-seq="${seq}">` +
|
|
27
|
+
`<span class="timeline__who">${entry.source}</span>` +
|
|
28
|
+
`<span class="timeline__mark" aria-hidden="true">${mark}</span>` +
|
|
29
|
+
`<span class="timeline__verb">${esc(entry.action)}</span>` +
|
|
30
|
+
`<span class="timeline__detail">${detail}</span>` +
|
|
31
|
+
`</li>`
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Wire a LogSink that renders each crossing into the live interaction timeline over `stream`. */
|
|
36
|
+
export function createStreamLogSink(stream: OpChannel): LogSink {
|
|
37
|
+
let seq = 0;
|
|
38
|
+
return {
|
|
39
|
+
record(entry: LogEntry): void {
|
|
40
|
+
const op: RenderOp = {
|
|
41
|
+
target: PUSH_SURFACES.timeline,
|
|
42
|
+
op: "log",
|
|
43
|
+
html: timelineEntryHtml(entry, ++seq),
|
|
44
|
+
provenance: entry.source, // color-code the entry by who authored the crossing
|
|
45
|
+
commit: "committed", // a log line is a fact, never optimistic
|
|
46
|
+
};
|
|
47
|
+
stream.push(entry.session, OP_EVENT, op);
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// grain/ai/vocab-reference.test.ts
|
|
2
|
+
import { test, expect } from "bun:test";
|
|
3
|
+
import { buildVocabReference, RENDER_OP_KINDS } from "./vocab-reference.ts";
|
|
4
|
+
import { ACTIONS } from "./contract.ts";
|
|
5
|
+
|
|
6
|
+
// File-relative (not cwd-relative) so grain's own suite resolves whether grain/ is a subdir
|
|
7
|
+
// of the monorepo or the repo root after the split (SPLIT-PLAN.md).
|
|
8
|
+
const VARIABLES_CSS = import.meta.dir + "/../styles/variables.css";
|
|
9
|
+
|
|
10
|
+
test("renders every ACTIONS entry as a row — never hand-copied", async () => {
|
|
11
|
+
const html = await buildVocabReference(VARIABLES_CSS);
|
|
12
|
+
for (const name of Object.keys(ACTIONS)) expect(html).toContain(`<code>${name}</code>`);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
test("renders each verb's payload schema + description — derived, not hand-copied", async () => {
|
|
16
|
+
const html = await buildVocabReference(VARIABLES_CSS);
|
|
17
|
+
// a required text field renders as text*:string; the description column carries the prose
|
|
18
|
+
expect(html).toContain(`<code>text*:string</code>`);
|
|
19
|
+
expect(html).toContain(ACTIONS["note.append"].description);
|
|
20
|
+
// a no-argument verb reads "no args", not a blank cell
|
|
21
|
+
expect(html).toContain("no args");
|
|
22
|
+
// MCP-style hint flags render too (note.replace is destructive + idempotent)
|
|
23
|
+
expect(html).toContain(`<code>destructive</code>`);
|
|
24
|
+
expect(html).toContain(`<code>idempotent</code>`);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("renders the token slots read from variables.css", async () => {
|
|
28
|
+
const html = await buildVocabReference(VARIABLES_CSS);
|
|
29
|
+
expect(html).toContain("--ink");
|
|
30
|
+
expect(html).toContain("--paper");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
// DRIFT GUARD: RENDER_OP_KINDS can't be derived from a runtime value (RenderOpKind is a
|
|
34
|
+
// pure type alias), so it's hand-maintained in vocab-reference.ts — this test fails loudly
|
|
35
|
+
// the moment contract.ts's union changes without a matching update here.
|
|
36
|
+
test("RENDER_OP_KINDS matches contract.ts's RenderOpKind union", async () => {
|
|
37
|
+
const src = await Bun.file(new URL("./contract.ts", import.meta.url)).text();
|
|
38
|
+
const m = src.match(/export type RenderOpKind\s*=\s*([^;]+);/);
|
|
39
|
+
expect(m).not.toBeNull();
|
|
40
|
+
const declared = new Set([...m![1].matchAll(/"([a-z]+)"/g)].map((x) => x[1]));
|
|
41
|
+
const listed = new Set(RENDER_OP_KINDS.map((o) => o.kind));
|
|
42
|
+
expect(listed).toEqual(declared);
|
|
43
|
+
});
|