@serene-pub/ui-preview 0.6.0-pr-1
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/NOTICE +17 -0
- package/README.md +157 -0
- package/package.json +66 -0
- package/server.d.ts +15 -0
- package/server.js +48 -0
- package/src/app.css +144 -0
- package/src/app.html +34 -0
- package/src/lib/ComponentStage.svelte +224 -0
- package/src/lib/FrameStage.svelte +478 -0
- package/src/lib/Problems.svelte +20 -0
- package/src/lib/SurfaceSettings.svelte +86 -0
- package/src/lib/ThemePicker.svelte +51 -0
- package/src/lib/defaults.ts +16 -0
- package/src/lib/fixtures.ts +108 -0
- package/src/lib/framePort.ts +295 -0
- package/src/lib/samples.ts +90 -0
- package/src/lib/theme.svelte.ts +59 -0
- package/src/lib/virtual.d.ts +15 -0
- package/src/routes/+layout.svelte +122 -0
- package/src/routes/+layout.ts +7 -0
- package/src/routes/+page.svelte +89 -0
- package/src/routes/controls/+page.svelte +157 -0
- package/src/routes/t/[id]/+page.svelte +205 -0
- package/static/favicon.svg +1 -0
- package/svelte.config.js +16 -0
- package/vite/surfaces.ts +371 -0
- package/vite.config.ts +55 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What the harness pushes down the channel.
|
|
3
|
+
*
|
|
4
|
+
* The point of fixtures is that a surface can only ever render *what the host
|
|
5
|
+
* chose to post* (20 §12) — so the honest way to develop one is against the
|
|
6
|
+
* host's actual posting shape, not against a live instance where it is easy to
|
|
7
|
+
* accidentally depend on something core never sends. These mirror what the
|
|
8
|
+
* session page hands `PluginFrame` today: a thin session (`{id, name}`), the
|
|
9
|
+
* session's message rows, and per-panel props.
|
|
10
|
+
*
|
|
11
|
+
* Replace them wholesale with `serene-pub ui --fixtures my.json`, or edit them
|
|
12
|
+
* live in the harness — the editor pushes on every valid parse.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
export interface Fixtures {
|
|
16
|
+
session: Record<string, unknown>
|
|
17
|
+
messages: Array<Record<string, unknown>>
|
|
18
|
+
props: Record<string, unknown>
|
|
19
|
+
/** What a component receives as `ctx` (10 §5). */
|
|
20
|
+
ctx: Record<string, unknown>
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Coerce parsed JSON into the shape the stage feeds a surface.
|
|
25
|
+
*
|
|
26
|
+
* The editor accepts anything that *parses*, and parsing is a much weaker
|
|
27
|
+
* promise than "has the fields the pushers read". Deleting `messages`, or
|
|
28
|
+
* making it an object, threw inside the push and took the whole stage down —
|
|
29
|
+
* a JSON editor whose worst case is a blank page is a JSON editor nobody
|
|
30
|
+
* edits. Missing or wrong-typed pieces fall back to the defaults, and the
|
|
31
|
+
* caller is told which ones, so the substitution is visible rather than
|
|
32
|
+
* mysterious.
|
|
33
|
+
*/
|
|
34
|
+
export function normalizeFixtures(
|
|
35
|
+
raw: unknown,
|
|
36
|
+
base: Fixtures,
|
|
37
|
+
): { fixtures: Fixtures; substituted: string[] } {
|
|
38
|
+
const substituted: string[] = []
|
|
39
|
+
const src = (raw ?? {}) as Record<string, unknown>
|
|
40
|
+
const obj = (v: unknown, key: keyof Fixtures, fallback: Record<string, unknown>) => {
|
|
41
|
+
if (v && typeof v === 'object' && !Array.isArray(v)) return v as Record<string, unknown>
|
|
42
|
+
if (v !== undefined) substituted.push(key)
|
|
43
|
+
return fallback
|
|
44
|
+
}
|
|
45
|
+
let messages = base.messages
|
|
46
|
+
if (Array.isArray(src.messages)) messages = src.messages as Array<Record<string, unknown>>
|
|
47
|
+
else if (src.messages !== undefined) substituted.push('messages')
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
fixtures: {
|
|
51
|
+
session: obj(src.session, 'session', base.session),
|
|
52
|
+
messages,
|
|
53
|
+
props: obj(src.props, 'props', base.props),
|
|
54
|
+
ctx: obj(src.ctx, 'ctx', base.ctx),
|
|
55
|
+
},
|
|
56
|
+
substituted,
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const message = (
|
|
61
|
+
id: number,
|
|
62
|
+
channel: string,
|
|
63
|
+
role: string,
|
|
64
|
+
speakerLabel: string,
|
|
65
|
+
text: string,
|
|
66
|
+
extras: Record<string, unknown> = {},
|
|
67
|
+
) => ({
|
|
68
|
+
id,
|
|
69
|
+
sessionId: 1,
|
|
70
|
+
channel,
|
|
71
|
+
kind: 'core:chat',
|
|
72
|
+
version: '1.0',
|
|
73
|
+
role,
|
|
74
|
+
speakerLabel,
|
|
75
|
+
status: 'settled',
|
|
76
|
+
activeRevisions: { '0': 0 },
|
|
77
|
+
extras,
|
|
78
|
+
content: text,
|
|
79
|
+
createdAt: '2026-08-28T12:00:00.000Z',
|
|
80
|
+
})
|
|
81
|
+
|
|
82
|
+
export const DEFAULT_FIXTURES: Fixtures = {
|
|
83
|
+
session: { id: 1, name: 'The Sunken Vault' },
|
|
84
|
+
messages: [
|
|
85
|
+
message(1, 'main', 'user', 'Wren', 'We push the door open. What do we see?'),
|
|
86
|
+
message(
|
|
87
|
+
2,
|
|
88
|
+
'main',
|
|
89
|
+
'assistant',
|
|
90
|
+
'Narrator',
|
|
91
|
+
'Cold air, and a stairwell going down further than the lantern reaches.',
|
|
92
|
+
),
|
|
93
|
+
message(3, 'map', 'assistant', 'Cartographer', 'Vault — level 1', {
|
|
94
|
+
'acme.map': { x: 4, y: 7, revealed: ['stair', 'antechamber'] },
|
|
95
|
+
}),
|
|
96
|
+
message(4, 'tasks', 'assistant', 'Quartermaster', 'Find the vault key', {
|
|
97
|
+
'acme.tasks': { done: false, priority: 'high' },
|
|
98
|
+
}),
|
|
99
|
+
],
|
|
100
|
+
props: { title: 'Vault', zoom: 2 },
|
|
101
|
+
ctx: {
|
|
102
|
+
user: { id: 1, role: 'admin', display: 'Wren' },
|
|
103
|
+
theme: 'catppuccin',
|
|
104
|
+
locale: 'en',
|
|
105
|
+
settings: {},
|
|
106
|
+
state: {},
|
|
107
|
+
},
|
|
108
|
+
}
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The host half of **frame protocol 2**, with no DOM in it.
|
|
3
|
+
*
|
|
4
|
+
* `FrameStage.svelte` owns the iframe, the `MessageChannel` and the chrome;
|
|
5
|
+
* everything it *decides* lives here, for the reason `playground/src/runner.ts`
|
|
6
|
+
* has no `document` in it: the decisions are what a test can pin, and a harness
|
|
7
|
+
* whose protocol half could only be checked by driving a browser is a harness
|
|
8
|
+
* whose protocol half is never checked. If someone puts DOM in this file, the
|
|
9
|
+
* SDK suite stops compiling, which is the intended alarm.
|
|
10
|
+
*
|
|
11
|
+
* ## What protocol 2 added, and why the harness had to catch up (G11)
|
|
12
|
+
*
|
|
13
|
+
* The SDK has declared `FRAME_PROTOCOL = 2` since the three frame → host
|
|
14
|
+
* messages landed, and this harness went on posting `{ protocol: 1 }` and
|
|
15
|
+
* logging all three as *"not a protocol v1 message — the host ignores it"*.
|
|
16
|
+
* A surface author building against `error`, `request` or `save-state` here
|
|
17
|
+
* saw nothing happen and had no way to tell a harness that ignored them from a
|
|
18
|
+
* surface that never sent them. The three are:
|
|
19
|
+
*
|
|
20
|
+
* · **`error`** — a frame that failed to boot was indistinguishable from one
|
|
21
|
+
* that was merely slow. Now it is surfaced in the chrome, and `fatal` says
|
|
22
|
+
* the surface has given up rather than merely complained.
|
|
23
|
+
* · **`request`** — a frame showing a long channel could not page. Now the
|
|
24
|
+
* host answers with `page`, echoing the `requestId`.
|
|
25
|
+
* · **`save-state`** — a frame with any view state lost it on every remount.
|
|
26
|
+
* Now the host holds it for the harness session and returns it as `state`.
|
|
27
|
+
*
|
|
28
|
+
* A **v1 frame keeps working unchanged**: it never sends the three, and `init`
|
|
29
|
+
* carries a version number it is free to ignore.
|
|
30
|
+
*
|
|
31
|
+
* ## Where this harness is not production
|
|
32
|
+
*
|
|
33
|
+
* Core answers none of the three yet — `PluginFrame.svelte` is still a
|
|
34
|
+
* protocol-1 host. So a surface must treat `page` and `state` as things that
|
|
35
|
+
* may never arrive, exactly as it already treats `theme` and declared props.
|
|
36
|
+
* The paging rules below (the default and maximum page, the offset cursor) are
|
|
37
|
+
* the **harness's** choice, not a published contract: core will make its own,
|
|
38
|
+
* and a surface that depends on the cursor's spelling is depending on this
|
|
39
|
+
* file. Read `nextCursor` and hand it back; never parse it.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
import { FRAME_PROTOCOL, type FrameHostMessage, type FramePoint, type HostFrameMessage } from '@serene-pub/sdk'
|
|
43
|
+
|
|
44
|
+
/** The rows the host pages over — the fixtures, as the stage holds them. */
|
|
45
|
+
export type FrameRow = Record<string, unknown>
|
|
46
|
+
|
|
47
|
+
/** What a page request is answered from. */
|
|
48
|
+
export interface FramePortSource {
|
|
49
|
+
messages: FrameRow[]
|
|
50
|
+
/** The lanes this surface declared, when it declared any (a panel's scoping). */
|
|
51
|
+
channels?: string[]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** The default page a `request` gets when it asks for no size. */
|
|
55
|
+
export const FRAME_PAGE_DEFAULT = 50
|
|
56
|
+
/** The most rows one page may carry, whatever a frame asks for. */
|
|
57
|
+
export const FRAME_PAGE_MAX = 200
|
|
58
|
+
/**
|
|
59
|
+
* The most view state the host will hold for a surface. `save-state` is not
|
|
60
|
+
* storage — the declaration says the host may cap or drop it — so it does,
|
|
61
|
+
* loudly, rather than quietly becoming a database a surface comes to rely on.
|
|
62
|
+
*/
|
|
63
|
+
export const FRAME_STATE_MAX_BYTES = 16 * 1024
|
|
64
|
+
|
|
65
|
+
/** `init`, as a protocol-2 host sends it. */
|
|
66
|
+
export const initMessage = (surface: FramePoint): Extract<HostFrameMessage, { t: 'init' }> => ({
|
|
67
|
+
t: 'init',
|
|
68
|
+
protocol: FRAME_PROTOCOL,
|
|
69
|
+
surface,
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
// ── Saved view state ────────────────────────────────────────────────────────
|
|
73
|
+
|
|
74
|
+
export interface FrameStateStore {
|
|
75
|
+
get(surfaceId: string): Record<string, unknown> | undefined
|
|
76
|
+
/** Returns what the host did: kept it, or dropped it and why. */
|
|
77
|
+
set(surfaceId: string, state: unknown): { kept: boolean; reason?: string; bytes: number }
|
|
78
|
+
clear(surfaceId?: string): void
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function frameStateStore(): FrameStateStore {
|
|
82
|
+
const held = new Map<string, Record<string, unknown>>()
|
|
83
|
+
return {
|
|
84
|
+
get: (surfaceId) => held.get(surfaceId),
|
|
85
|
+
set: (surfaceId, state) => {
|
|
86
|
+
if (!state || typeof state !== 'object' || Array.isArray(state))
|
|
87
|
+
return { kept: false, reason: 'state is an object of small values', bytes: 0 }
|
|
88
|
+
let json: string
|
|
89
|
+
try {
|
|
90
|
+
json = JSON.stringify(state)
|
|
91
|
+
} catch {
|
|
92
|
+
return { kept: false, reason: 'state must be JSON a host can store', bytes: 0 }
|
|
93
|
+
}
|
|
94
|
+
const bytes = new TextEncoder().encode(json).length
|
|
95
|
+
if (bytes > FRAME_STATE_MAX_BYTES)
|
|
96
|
+
return {
|
|
97
|
+
kept: false,
|
|
98
|
+
bytes,
|
|
99
|
+
reason: `state is ${bytes} bytes, over the host's ${FRAME_STATE_MAX_BYTES} — the host may cap or drop it`,
|
|
100
|
+
}
|
|
101
|
+
held.set(surfaceId, JSON.parse(json) as Record<string, unknown>)
|
|
102
|
+
return { kept: true, bytes }
|
|
103
|
+
},
|
|
104
|
+
clear: (surfaceId) => {
|
|
105
|
+
if (surfaceId === undefined) held.clear()
|
|
106
|
+
else held.delete(surfaceId)
|
|
107
|
+
},
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The harness's own store, module-level on purpose: a surface's state has to
|
|
113
|
+
* survive the remount that a document reload is, which is the whole point of
|
|
114
|
+
* `save-state`, and a store owned by the component would die with it.
|
|
115
|
+
*/
|
|
116
|
+
export const savedFrameState: FrameStateStore = frameStateStore()
|
|
117
|
+
|
|
118
|
+
// ── Paging ──────────────────────────────────────────────────────────────────
|
|
119
|
+
|
|
120
|
+
/** An offset cursor. Opaque to the frame by contract; this is the harness's spelling. */
|
|
121
|
+
const CURSOR = /^o:([0-9]+)$/
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* One page of a channel's messages.
|
|
125
|
+
*
|
|
126
|
+
* Scoped before it is cut: a panel that declared lanes is answered out of those
|
|
127
|
+
* lanes only, here as in core, so a surface cannot discover that the harness
|
|
128
|
+
* was the lenient one. A request naming a channel the surface did not declare
|
|
129
|
+
* is answered with an empty page rather than with somebody else's lane.
|
|
130
|
+
*/
|
|
131
|
+
export function pageOf(
|
|
132
|
+
source: FramePortSource,
|
|
133
|
+
req: { channel?: string; cursor?: string; limit?: number },
|
|
134
|
+
): { rows: FrameRow[]; nextCursor?: string; refused?: string } {
|
|
135
|
+
const declared = source.channels ?? []
|
|
136
|
+
if (req.channel !== undefined && declared.length && !declared.includes(req.channel))
|
|
137
|
+
return { rows: [], refused: `'${req.channel}' is not a lane this surface declared` }
|
|
138
|
+
const lanes = req.channel !== undefined ? [req.channel] : declared
|
|
139
|
+
const rows = lanes.length
|
|
140
|
+
? source.messages.filter((m) => lanes.includes(String(m.channel ?? 'main')))
|
|
141
|
+
: source.messages
|
|
142
|
+
|
|
143
|
+
let start = 0
|
|
144
|
+
if (req.cursor !== undefined) {
|
|
145
|
+
const m = CURSOR.exec(String(req.cursor))
|
|
146
|
+
if (!m) return { rows: [], refused: `cursor '${req.cursor}' is not one this host issued` }
|
|
147
|
+
start = Number(m[1])
|
|
148
|
+
}
|
|
149
|
+
const want = Math.floor(Number(req.limit))
|
|
150
|
+
const limit = Math.max(1, Math.min(FRAME_PAGE_MAX, Number.isFinite(want) && want > 0 ? want : FRAME_PAGE_DEFAULT))
|
|
151
|
+
const slice = rows.slice(start, start + limit)
|
|
152
|
+
return start + limit < rows.length ? { rows: slice, nextCursor: `o:${start + limit}` } : { rows: slice }
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// ── The reducer ─────────────────────────────────────────────────────────────
|
|
156
|
+
|
|
157
|
+
/** One line for the channel log, as the stage renders it. */
|
|
158
|
+
export interface FramePortLog {
|
|
159
|
+
t: string
|
|
160
|
+
detail: string
|
|
161
|
+
/** An `error` the frame reported. `fatal` means it has given up. */
|
|
162
|
+
level?: 'error' | 'fatal'
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** What the host does about one frame → host message. */
|
|
166
|
+
export interface FramePortReply {
|
|
167
|
+
/** Posts to send back down the port, in order. */
|
|
168
|
+
post: HostFrameMessage[]
|
|
169
|
+
log: FramePortLog
|
|
170
|
+
/** The frame announced itself — the stage starts pushing. */
|
|
171
|
+
ready?: boolean
|
|
172
|
+
/** The frame reported a failure; `fatal` means it is not coming back. */
|
|
173
|
+
error?: { message: string; fatal: boolean }
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export interface FramePortContext {
|
|
177
|
+
/** The stage's id for this surface — what saved state is keyed on. */
|
|
178
|
+
surfaceId: string
|
|
179
|
+
source: FramePortSource
|
|
180
|
+
state?: FrameStateStore
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const detailOf = (v: unknown): string => {
|
|
184
|
+
if (typeof v === 'string') return v
|
|
185
|
+
try {
|
|
186
|
+
return JSON.stringify(v) ?? ''
|
|
187
|
+
} catch {
|
|
188
|
+
return '[unserializable]'
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Answer one message from the frame. Pure but for the state store — a frame
|
|
194
|
+
* that says nothing the protocol declares is *told* it was ignored, because
|
|
195
|
+
* "the host ignored it" and "the frame never sent it" have to be
|
|
196
|
+
* distinguishable in the log.
|
|
197
|
+
*/
|
|
198
|
+
export function answerFrameMessage(raw: unknown, ctx: FramePortContext): FramePortReply | null {
|
|
199
|
+
if (!raw || typeof raw !== 'object') return null
|
|
200
|
+
const m = raw as FrameHostMessage
|
|
201
|
+
|
|
202
|
+
if (m.t === 'ready') {
|
|
203
|
+
// The one place saved state is returned: a remount is exactly when a
|
|
204
|
+
// frame has forgotten, and the host answers before the first push so a
|
|
205
|
+
// surface can restore its tab before it has any rows to put in it.
|
|
206
|
+
const held = (ctx.state ?? savedFrameState).get(ctx.surfaceId)
|
|
207
|
+
return {
|
|
208
|
+
post: held ? [{ t: 'state', state: held }] : [],
|
|
209
|
+
log: { t: 'ready', detail: held ? `restored ${Object.keys(held).length} saved key(s)` : '' },
|
|
210
|
+
ready: true,
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
if (m.t === 'invoke' && typeof m.key === 'string') {
|
|
215
|
+
return {
|
|
216
|
+
post: [],
|
|
217
|
+
log: {
|
|
218
|
+
t: 'invoke',
|
|
219
|
+
detail: `← invoke ${m.key}${m.blockId ? ` blockId=${m.blockId}` : ''}${
|
|
220
|
+
typeof m.messageId === 'number' ? ` #${m.messageId}` : ''
|
|
221
|
+
}${m.payload ? ` ${detailOf(m.payload)}` : ''}`,
|
|
222
|
+
},
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
if (m.t === 'action' && typeof m.fn === 'string') {
|
|
227
|
+
const base = `${m.fn}${typeof m.messageId === 'number' ? ` #${m.messageId}` : ''} ${
|
|
228
|
+
m.payload ? detailOf(m.payload) : ''
|
|
229
|
+
}`.trim()
|
|
230
|
+
return {
|
|
231
|
+
post: [],
|
|
232
|
+
log: {
|
|
233
|
+
t: 'action',
|
|
234
|
+
detail: `${base} — deprecated, send 'invoke' instead`,
|
|
235
|
+
},
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
if (m.t === 'error' && typeof m.message === 'string') {
|
|
240
|
+
const fatal = m.fatal === true
|
|
241
|
+
return {
|
|
242
|
+
post: [],
|
|
243
|
+
log: {
|
|
244
|
+
t: 'error',
|
|
245
|
+
level: fatal ? 'fatal' : 'error',
|
|
246
|
+
detail: `${fatal ? 'FATAL — ' : ''}${m.message}${m.detail !== undefined ? ` ${detailOf(m.detail)}` : ''}`,
|
|
247
|
+
},
|
|
248
|
+
error: { message: m.message, fatal },
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
if (m.t === 'request' && typeof m.requestId === 'string') {
|
|
253
|
+
if (m.what !== 'messages')
|
|
254
|
+
return {
|
|
255
|
+
post: [],
|
|
256
|
+
log: { t: 'request', detail: `declined: '${String(m.what)}' is not something a host answers` },
|
|
257
|
+
}
|
|
258
|
+
const page = pageOf(ctx.source, m)
|
|
259
|
+
if (page.refused)
|
|
260
|
+
// Declined silently down the port — a request is not a grant — but
|
|
261
|
+
// never silently in the log, or an author cannot tell a refusal from
|
|
262
|
+
// a host that dropped the message.
|
|
263
|
+
return { post: [], log: { t: 'request', detail: `declined: ${page.refused}` } }
|
|
264
|
+
return {
|
|
265
|
+
post: [{ t: 'page', requestId: m.requestId, rows: page.rows, ...(page.nextCursor ? { nextCursor: page.nextCursor } : {}) }],
|
|
266
|
+
log: {
|
|
267
|
+
t: 'request',
|
|
268
|
+
detail:
|
|
269
|
+
`${m.channel ?? 'every declared lane'} → ${page.rows.length} row(s)` +
|
|
270
|
+
(page.nextCursor ? ', more to come' : ''),
|
|
271
|
+
},
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (m.t === 'save-state') {
|
|
276
|
+
const outcome = (ctx.state ?? savedFrameState).set(ctx.surfaceId, m.state)
|
|
277
|
+
return {
|
|
278
|
+
post: [],
|
|
279
|
+
log: {
|
|
280
|
+
t: 'save-state',
|
|
281
|
+
detail: outcome.kept
|
|
282
|
+
? `held ${outcome.bytes} bytes for this surface — returned as 'state' on the next mount`
|
|
283
|
+
: `DROPPED — ${outcome.reason}`,
|
|
284
|
+
},
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
return {
|
|
289
|
+
post: [],
|
|
290
|
+
log: {
|
|
291
|
+
t: String((m as { t?: unknown }).t ?? '?'),
|
|
292
|
+
detail: `not a protocol ${FRAME_PROTOCOL} message — the host ignores it`,
|
|
293
|
+
},
|
|
294
|
+
}
|
|
295
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One representative declaration per shipped value kind, minted through the
|
|
3
|
+
* real toolkit rather than hand-written JSON.
|
|
4
|
+
*
|
|
5
|
+
* That matters: a sandbox whose declarations were literals would happily show
|
|
6
|
+
* a control for a shape `v.weights({…})` refuses to produce, and the first
|
|
7
|
+
* person to trust it would author something the packager rejects. Going
|
|
8
|
+
* through the toolkit means every sample in the sandbox is a declaration an
|
|
9
|
+
* author could actually write.
|
|
10
|
+
*/
|
|
11
|
+
import { makeValueToolkit, valueKind, type ValueDecl } from '@serene-pub/sdk'
|
|
12
|
+
|
|
13
|
+
const v = makeValueToolkit()
|
|
14
|
+
|
|
15
|
+
export interface Sample {
|
|
16
|
+
kind: string
|
|
17
|
+
decl: ValueDecl
|
|
18
|
+
/** A value that satisfies the declaration, for the first render. */
|
|
19
|
+
value: unknown
|
|
20
|
+
/** What the author writes to get it. */
|
|
21
|
+
source: string
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const sample = (decl: ValueDecl, value: unknown, source: string): Sample => ({
|
|
25
|
+
kind: valueKind(decl),
|
|
26
|
+
decl,
|
|
27
|
+
value,
|
|
28
|
+
source,
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
export const SAMPLES: Sample[] = [
|
|
32
|
+
sample(
|
|
33
|
+
v.integer({ min: 1, max: 32, default: 8 }),
|
|
34
|
+
8,
|
|
35
|
+
'v.integer({ min: 1, max: 32, default: 8 })',
|
|
36
|
+
),
|
|
37
|
+
sample(
|
|
38
|
+
v.number({ min: 0, max: 2, step: 0.05, default: 0.7 }),
|
|
39
|
+
0.7,
|
|
40
|
+
'v.number({ min: 0, max: 2, step: 0.05, default: 0.7 })',
|
|
41
|
+
),
|
|
42
|
+
sample(v.fraction({ default: 0.25 }), 0.25, 'v.fraction({ default: 0.25 })'),
|
|
43
|
+
sample(v.boolean({ default: true }), true, 'v.boolean({ default: true })'),
|
|
44
|
+
sample(
|
|
45
|
+
v.text({ maxLength: 80, default: 'A tavern at dusk' }),
|
|
46
|
+
'A tavern at dusk',
|
|
47
|
+
'v.text({ maxLength: 80 })',
|
|
48
|
+
),
|
|
49
|
+
sample(
|
|
50
|
+
v.text({ multiline: true, default: 'Line one\nLine two' }),
|
|
51
|
+
'Line one\nLine two',
|
|
52
|
+
'v.text({ multiline: true })',
|
|
53
|
+
),
|
|
54
|
+
sample(
|
|
55
|
+
v.select([
|
|
56
|
+
{ value: 'rag', label: 'Retrieval', description: 'Pull from the lorebook' },
|
|
57
|
+
{ value: 'none', label: 'None', description: 'Send the log as-is' },
|
|
58
|
+
]),
|
|
59
|
+
'rag',
|
|
60
|
+
"v.select([{ value: 'rag', label: 'Retrieval', … }, …])",
|
|
61
|
+
),
|
|
62
|
+
sample(
|
|
63
|
+
v.ranking(['recency', 'relevance', 'importance']),
|
|
64
|
+
['relevance', 'recency', 'importance'],
|
|
65
|
+
"v.ranking(['recency', 'relevance', 'importance'])",
|
|
66
|
+
),
|
|
67
|
+
sample(
|
|
68
|
+
v.stackedBar({ parts: { lore: 40, history: 45, persona: 15 }, total: 100, step: 1 }),
|
|
69
|
+
{ lore: 40, history: 45, persona: 15 },
|
|
70
|
+
'v.stackedBar({ parts: { … }, total: 100, step: 1 })',
|
|
71
|
+
),
|
|
72
|
+
sample(
|
|
73
|
+
v.weights({ parts: { alpha: 1, beta: 2 }, min: 0 }),
|
|
74
|
+
{ alpha: 1, beta: 2 },
|
|
75
|
+
'v.weights({ parts: { alpha: 1, beta: 2 }, min: 0 })',
|
|
76
|
+
),
|
|
77
|
+
sample(v.prompt(), null, 'v.prompt()'),
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A kind the registry does not know, so the safe degradation is visible
|
|
82
|
+
* rather than described. It is minted by hand precisely because the toolkit
|
|
83
|
+
* would refuse it — that is the situation being demonstrated.
|
|
84
|
+
*/
|
|
85
|
+
export const UNKNOWN_SAMPLE: Sample = {
|
|
86
|
+
kind: 'acme.dice:roll@1',
|
|
87
|
+
decl: { 'acme.dice:roll@1': { sides: 20 } },
|
|
88
|
+
value: { sides: 20 },
|
|
89
|
+
source: "v.custom('roll', 1, { sides: 20 }) — from a package this build has never seen",
|
|
90
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The active theme and mode, applied the way the app applies them.
|
|
3
|
+
*
|
|
4
|
+
* `data-theme="<bare name>"` and `data-mode="light|dark"` on <html> — the
|
|
5
|
+
* same two attributes `Layout.svelte` sets in Serene Pub, so what a modder
|
|
6
|
+
* sees here is what the shipped Skeleton version does. `data-mode` is not a
|
|
7
|
+
* Skeleton convention; it is the app's, turned into `color-scheme` by the one
|
|
8
|
+
* `@custom-variant` line in `app.css`, and copied verbatim for that reason.
|
|
9
|
+
*
|
|
10
|
+
* Persisted, because a harness that forgot the theme every reload would make
|
|
11
|
+
* "check it on every theme" a chore instead of a click.
|
|
12
|
+
*/
|
|
13
|
+
const KEY = 'serene-pub:harness-theme'
|
|
14
|
+
|
|
15
|
+
export interface ThemeState {
|
|
16
|
+
theme: string
|
|
17
|
+
mode: 'light' | 'dark'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const DEFAULTS: ThemeState = { theme: 'cerberus', mode: 'dark' }
|
|
21
|
+
|
|
22
|
+
function load(): ThemeState {
|
|
23
|
+
if (typeof localStorage === 'undefined') return DEFAULTS
|
|
24
|
+
try {
|
|
25
|
+
const raw = JSON.parse(localStorage.getItem(KEY) ?? 'null')
|
|
26
|
+
if (raw && typeof raw.theme === 'string' && (raw.mode === 'light' || raw.mode === 'dark'))
|
|
27
|
+
return raw as ThemeState
|
|
28
|
+
} catch {
|
|
29
|
+
// A corrupt preference is not worth a broken harness.
|
|
30
|
+
}
|
|
31
|
+
return DEFAULTS
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
class Theme {
|
|
35
|
+
current = $state<ThemeState>(load())
|
|
36
|
+
|
|
37
|
+
set(next: Partial<ThemeState>) {
|
|
38
|
+
this.current = { ...this.current, ...next }
|
|
39
|
+
try {
|
|
40
|
+
localStorage.setItem(KEY, JSON.stringify(this.current))
|
|
41
|
+
} catch {
|
|
42
|
+
// Private-mode storage refusal: switching still works, it just
|
|
43
|
+
// does not survive a reload. Not worth surfacing.
|
|
44
|
+
}
|
|
45
|
+
this.apply()
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
toggleMode() {
|
|
49
|
+
this.set({ mode: this.current.mode === 'dark' ? 'light' : 'dark' })
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
apply() {
|
|
53
|
+
if (typeof document === 'undefined') return
|
|
54
|
+
document.documentElement.setAttribute('data-theme', this.current.theme)
|
|
55
|
+
document.documentElement.setAttribute('data-mode', this.current.mode)
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export const theme = new Theme()
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare module 'virtual:serene-pub/surfaces' {
|
|
2
|
+
import type { PreviewManifest } from '@serene-pub/sdk'
|
|
3
|
+
export const manifest: PreviewManifest
|
|
4
|
+
export const packageDir: string
|
|
5
|
+
/** target id → dev URL of the frame document. */
|
|
6
|
+
export const frames: Record<string, string>
|
|
7
|
+
/** target id → dynamic import of the component module. */
|
|
8
|
+
export const components: Record<string, () => Promise<Record<string, unknown>>>
|
|
9
|
+
/** Parsed `--fixtures` file, or null for the built-ins. */
|
|
10
|
+
export const fixtureOverride: unknown
|
|
11
|
+
/** Stock Skeleton theme names, read off the installed package. */
|
|
12
|
+
export const themes: string[]
|
|
13
|
+
/** Where the frame-tier token stylesheet is served. */
|
|
14
|
+
export const themeCssPath: string
|
|
15
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/**
|
|
3
|
+
* The harness shell: what this package announces, on the left; whichever
|
|
4
|
+
* surface is selected, on the right. The list is the announcement's, never
|
|
5
|
+
* a directory scan — a surface visible here is a surface an instance would
|
|
6
|
+
* be offered, or one of the two is wrong and worth finding out now.
|
|
7
|
+
*
|
|
8
|
+
* The chrome is themed with the product's own Skeleton tokens rather than
|
|
9
|
+
* a neutral palette. That is a reversal of an earlier decision, and the
|
|
10
|
+
* reason is concrete: the controls are styled by Skeleton `@utility`
|
|
11
|
+
* classes, so the harness has to load that layer to render a generated
|
|
12
|
+
* form at all. Once it is loaded, dressing the tool in a different palette
|
|
13
|
+
* than the surface would only make the two harder to compare.
|
|
14
|
+
*/
|
|
15
|
+
import '../app.css'
|
|
16
|
+
import { page } from '$app/state'
|
|
17
|
+
import { manifest, packageDir } from 'virtual:serene-pub/surfaces'
|
|
18
|
+
import { theme } from '$lib/theme.svelte.js'
|
|
19
|
+
import ThemePicker from '$lib/ThemePicker.svelte'
|
|
20
|
+
|
|
21
|
+
let { children } = $props()
|
|
22
|
+
|
|
23
|
+
// app.html carries a default so the first paint is themed; this makes the
|
|
24
|
+
// persisted choice win from the first frame the client owns.
|
|
25
|
+
$effect(() => theme.apply())
|
|
26
|
+
|
|
27
|
+
const frames = $derived(manifest.targets.filter((t) => t.kind === 'frame'))
|
|
28
|
+
const components = $derived(manifest.targets.filter((t) => t.kind === 'component'))
|
|
29
|
+
const withSettings = $derived(components.filter((t) => t.settings).length)
|
|
30
|
+
const current = $derived(page.params.id)
|
|
31
|
+
</script>
|
|
32
|
+
|
|
33
|
+
<div class="flex h-screen">
|
|
34
|
+
<nav
|
|
35
|
+
class="bg-surface-50-950 border-surface-200-800 harness-scroll flex w-64 flex-none flex-col overflow-y-auto border-r p-3"
|
|
36
|
+
>
|
|
37
|
+
<div>
|
|
38
|
+
<strong class="block text-base">{manifest.title}</strong>
|
|
39
|
+
<div class="text-surface-600-400 harness-mono text-xs">
|
|
40
|
+
{manifest.id}{manifest.version ? ` @ ${manifest.version}` : ''}
|
|
41
|
+
</div>
|
|
42
|
+
</div>
|
|
43
|
+
|
|
44
|
+
<a
|
|
45
|
+
href="/"
|
|
46
|
+
class="mt-3 rounded px-2 py-1 text-sm {page.url.pathname === '/'
|
|
47
|
+
? 'preset-tonal-primary'
|
|
48
|
+
: 'hover:preset-tonal-surface'}">Overview</a
|
|
49
|
+
>
|
|
50
|
+
<a
|
|
51
|
+
href="/controls"
|
|
52
|
+
class="rounded px-2 py-1 text-sm {page.url.pathname === '/controls'
|
|
53
|
+
? 'preset-tonal-primary'
|
|
54
|
+
: 'hover:preset-tonal-surface'}"
|
|
55
|
+
>
|
|
56
|
+
Controls sandbox
|
|
57
|
+
</a>
|
|
58
|
+
|
|
59
|
+
{#if frames.length}
|
|
60
|
+
<div class="text-surface-600-400 mt-4 mb-1 text-[0.66rem] tracking-widest uppercase">
|
|
61
|
+
Frame surfaces
|
|
62
|
+
</div>
|
|
63
|
+
{#each frames as t (t.id)}
|
|
64
|
+
<a
|
|
65
|
+
href="/t/{t.id}"
|
|
66
|
+
class="rounded px-2 py-1 leading-tight {current === t.id
|
|
67
|
+
? 'preset-tonal-primary'
|
|
68
|
+
: 'hover:preset-tonal-surface'}"
|
|
69
|
+
>
|
|
70
|
+
<span class="text-sm">{t.label}</span>
|
|
71
|
+
<span class="text-surface-600-400 block text-xs">
|
|
72
|
+
{t.point}
|
|
73
|
+
</span>
|
|
74
|
+
</a>
|
|
75
|
+
{/each}
|
|
76
|
+
{/if}
|
|
77
|
+
|
|
78
|
+
{#if components.length}
|
|
79
|
+
<div class="text-surface-600-400 mt-4 mb-1 text-[0.66rem] tracking-widest uppercase">
|
|
80
|
+
Components
|
|
81
|
+
</div>
|
|
82
|
+
{#each components as t (t.id)}
|
|
83
|
+
<a
|
|
84
|
+
href="/t/{t.id}"
|
|
85
|
+
class="rounded px-2 py-1 leading-tight {current === t.id
|
|
86
|
+
? 'preset-tonal-primary'
|
|
87
|
+
: 'hover:preset-tonal-surface'}"
|
|
88
|
+
>
|
|
89
|
+
<span class="text-sm">{t.label}</span>
|
|
90
|
+
<span class="text-surface-600-400 block text-xs">
|
|
91
|
+
{t.framework ?? 'svelte'}{t.settings ? ' · settings' : ''}
|
|
92
|
+
</span>
|
|
93
|
+
</a>
|
|
94
|
+
{/each}
|
|
95
|
+
{/if}
|
|
96
|
+
|
|
97
|
+
{#if !manifest.targets.length}
|
|
98
|
+
<div class="text-surface-600-400 mt-4 mb-1 text-[0.66rem] tracking-widest uppercase">
|
|
99
|
+
Nothing announced
|
|
100
|
+
</div>
|
|
101
|
+
<p class="text-surface-600-400 px-2 text-xs">
|
|
102
|
+
This package announces no UI surfaces yet.
|
|
103
|
+
</p>
|
|
104
|
+
{/if}
|
|
105
|
+
|
|
106
|
+
<div class="mt-auto flex flex-col gap-2 pt-4">
|
|
107
|
+
{#if withSettings === 0}
|
|
108
|
+
<p class="text-surface-600-400 text-xs">
|
|
109
|
+
No component declares settings yet, so there are no generated controls to
|
|
110
|
+
preview. The sandbox still shows every control.
|
|
111
|
+
</p>
|
|
112
|
+
{/if}
|
|
113
|
+
<ThemePicker />
|
|
114
|
+
<div class="text-surface-600-400 harness-mono truncate text-[0.7rem]" title={packageDir}>
|
|
115
|
+
{packageDir}
|
|
116
|
+
</div>
|
|
117
|
+
</div>
|
|
118
|
+
</nav>
|
|
119
|
+
<main class="flex min-w-0 flex-1 flex-col">
|
|
120
|
+
{@render children?.()}
|
|
121
|
+
</main>
|
|
122
|
+
</div>
|