@zeyos/zx 5.5.0 → 5.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +56 -0
- package/dist/types/components/agent-thread/agent-thread.d.ts +583 -0
- package/dist/types/components/approval-card/approval-card.d.ts +596 -0
- package/dist/types/components/avatar/avatar.d.ts +91 -4
- package/dist/types/components/block-view/block-view.d.ts +205 -0
- package/dist/types/components/composer/composer.d.ts +736 -0
- package/dist/types/components/run-status/run-status.d.ts +393 -0
- package/dist/types/components/run-trace/run-trace.d.ts +624 -0
- package/dist/types/components/source-list/source-list.d.ts +177 -0
- package/dist/types/core/messages.d.ts +135 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/internal/agent-model.d.ts +587 -0
- package/dist/types/internal/highlight.d.ts +24 -0
- package/dist/types/internal/presence-loop.d.ts +148 -0
- package/dist/zx-zeyos.esm.js +4648 -4513
- package/dist/zx-zeyos.esm.min.js +2 -2
- package/dist/zx.css +1927 -1
- package/dist/zx.esm.js +15769 -9194
- package/dist/zx.esm.min.js +27 -8
- package/dist/zx.global.js +15769 -9194
- package/dist/zx.global.min.js +27 -8
- package/dist/zx.min.css +1 -1
- package/docs/api.json +1166 -5
- package/docs/llms.md +455 -2
- package/docs/llms.txt +10 -1
- package/package.json +1 -1
- package/src/components/agent-thread/agent-thread.css +417 -0
- package/src/components/agent-thread/agent-thread.js +1365 -0
- package/src/components/approval-card/approval-card.css +289 -0
- package/src/components/approval-card/approval-card.js +1377 -0
- package/src/components/avatar/avatar.css +56 -1
- package/src/components/avatar/avatar.js +148 -26
- package/src/components/block-view/block-view.css +300 -0
- package/src/components/block-view/block-view.js +523 -0
- package/src/components/composer/composer.css +403 -0
- package/src/components/composer/composer.js +1441 -0
- package/src/components/run-status/run-status.css +208 -0
- package/src/components/run-status/run-status.js +782 -0
- package/src/components/run-trace/run-trace.css +443 -0
- package/src/components/run-trace/run-trace.js +1209 -0
- package/src/components/source-list/source-list.css +208 -0
- package/src/components/source-list/source-list.js +355 -0
- package/src/core/messages.js +143 -0
- package/src/index.js +8 -0
- package/src/internal/agent-model.js +488 -0
- package/src/internal/highlight.js +104 -0
- package/src/internal/presence-loop.js +343 -0
- package/styles/zx.css +7 -0
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
import { Component } from '../../core/component.js';
|
|
3
|
+
import { h, restoreTarget, snapshotTarget } from '../../core/dom.js';
|
|
4
|
+
import { icon } from '../../core/icons.js';
|
|
5
|
+
import { blocksToMarkdown, blocksToText, figureRow, parseInline } from '../../internal/agent-model.js';
|
|
6
|
+
import { highlight } from '../../internal/highlight.js';
|
|
7
|
+
import { copyButton } from '../copy/copy-button.js';
|
|
8
|
+
|
|
9
|
+
/** @typedef {import('../../internal/agent-model.js').Block} Block */
|
|
10
|
+
/** @typedef {import('../../internal/agent-model.js').Source} Source */
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* @typedef {Object} BlockViewOptions
|
|
14
|
+
* @property {Block[]} [blocks=[]] The answer's blocks. Model text is data: every string reaches
|
|
15
|
+
* the page as text, through the inline grammar of `**bold**`, `` `code` `` and `[n]` citations.
|
|
16
|
+
* @property {2|3|4|5} [headingLevel=3] Element level of a `heading` block of level 2; a level-3
|
|
17
|
+
* heading goes one deeper. Set it one below the heading that names the answer.
|
|
18
|
+
* @property {'sm'|'md'} [size='sm'] Prose size: `'sm'` sets the answer at the interface size, for
|
|
19
|
+
* a conversation; `'md'` at the reading size, for a document.
|
|
20
|
+
* @property {boolean} [streaming=false] Whether more is still being written. The last block stays
|
|
21
|
+
* open to `appendText()`, and the root is busy until `end()` or `stop()`.
|
|
22
|
+
* @property {Source[]|null} [sources=null] The sources citations point at; their titles name the
|
|
23
|
+
* citation buttons.
|
|
24
|
+
* @property {Record<string, string>|Record<string, Record<string, string>>} [msg] Localized messages.
|
|
25
|
+
* @property {(event: CustomEvent<{n: number, source: Source|null}>) => void} [oncite] Citation listener.
|
|
26
|
+
* @property {(event: CustomEvent<{id: string}>) => void} [onartifact] Artifact listener.
|
|
27
|
+
* @property {(event: CustomEvent<Record<string, never>>) => void} [onend] End-of-stream listener.
|
|
28
|
+
*/
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Options for drawing blocks without a component.
|
|
32
|
+
* @typedef {Object} RenderBlocksOptions
|
|
33
|
+
* @property {2|3|4|5} [headingLevel=3] Element level of a level-2 `heading` block.
|
|
34
|
+
* @property {(key: string, ...args: unknown[]) => string} [msg] Message lookup for the few words
|
|
35
|
+
* the blocks add (code copy, artifact kinds).
|
|
36
|
+
*/
|
|
37
|
+
|
|
38
|
+
const MESSAGES = Object.freeze({
|
|
39
|
+
'blockView.cite': 'Source %1',
|
|
40
|
+
'blockView.citeTitled': 'Source %1: %2',
|
|
41
|
+
'blockView.copyCode': 'Copy code',
|
|
42
|
+
'blockView.code': 'Code',
|
|
43
|
+
'blockView.document': 'Document',
|
|
44
|
+
'blockView.versions': '%1 versions',
|
|
45
|
+
'blockView.version': '1 version',
|
|
46
|
+
'blockView.table': 'Table',
|
|
47
|
+
'blockView.stopped': 'Stopped here.'
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
/** Glyph for each callout tone. */
|
|
51
|
+
const CALLOUT_ICONS = Object.freeze({ info: 'info', success: 'success', warning: 'warning', danger: 'error' });
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* An answer an agent wrote, drawn block by block as semantic elements inside `.zx-prose`, and
|
|
55
|
+
* written to as it streams. Finished blocks are never redrawn: `appendText()` and `append()` touch
|
|
56
|
+
* only the last block, so a reader's selection or focus in an earlier paragraph survives the rest
|
|
57
|
+
* of the answer arriving.
|
|
58
|
+
*
|
|
59
|
+
* @fires BlockView#cite
|
|
60
|
+
* @fires BlockView#artifact
|
|
61
|
+
* @fires BlockView#end
|
|
62
|
+
* @extends {Component<BlockViewOptions>}
|
|
63
|
+
*/
|
|
64
|
+
export class BlockView extends Component {
|
|
65
|
+
static cssName = 'block-view';
|
|
66
|
+
|
|
67
|
+
/** @type {Readonly<BlockViewOptions>} */
|
|
68
|
+
static defaults = {
|
|
69
|
+
blocks: [],
|
|
70
|
+
headingLevel: 3,
|
|
71
|
+
size: 'sm',
|
|
72
|
+
streaming: false,
|
|
73
|
+
sources: null,
|
|
74
|
+
msg: MESSAGES
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** @returns {HTMLElement} */
|
|
78
|
+
render() {
|
|
79
|
+
const created = !this.el;
|
|
80
|
+
const root = /** @type {HTMLElement} */ (this.el ?? h('div'));
|
|
81
|
+
this.el = root;
|
|
82
|
+
this._original = created ? null : snapshotTarget(root);
|
|
83
|
+
if (!created) root.replaceChildren();
|
|
84
|
+
root.classList.add('zx-prose');
|
|
85
|
+
/** @type {Block[]} */
|
|
86
|
+
this._blocks = [];
|
|
87
|
+
/** @type {HTMLElement[]} */
|
|
88
|
+
this._nodes = [];
|
|
89
|
+
/** @type {Source[]} */
|
|
90
|
+
this._sources = Array.isArray(this.options.sources) ? [...this.options.sources] : [];
|
|
91
|
+
this._streaming = Boolean(this.options.streaming);
|
|
92
|
+
this._stopped = false;
|
|
93
|
+
/** @type {HTMLElement|null} */
|
|
94
|
+
this._stoppedNote = null;
|
|
95
|
+
if (this.options.size !== 'md') root.dataset.size = 'sm';
|
|
96
|
+
root.dataset.zxMeasure = 'full';
|
|
97
|
+
this._syncState();
|
|
98
|
+
this.listen(root, 'click', (event) => this._onClick(/** @type {MouseEvent} */ (event)));
|
|
99
|
+
this._observer = typeof ResizeObserver === 'function'
|
|
100
|
+
? new ResizeObserver(() => this._syncScrollers())
|
|
101
|
+
: null;
|
|
102
|
+
this._observer?.observe(root);
|
|
103
|
+
for (const block of Array.isArray(this.options.blocks) ? this.options.blocks : []) this._push(block);
|
|
104
|
+
return root;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Replaces every block, clearing a stopped state.
|
|
109
|
+
* @param {Block[]} blocks
|
|
110
|
+
* @param {{streaming?: boolean}} [options={}] `streaming: true` starts a new streamed answer
|
|
111
|
+
* from these blocks; otherwise the streaming state is kept.
|
|
112
|
+
* @returns {this}
|
|
113
|
+
*/
|
|
114
|
+
setBlocks(blocks, options = {}) {
|
|
115
|
+
for (const node of this._nodes) node.remove();
|
|
116
|
+
this._blocks = [];
|
|
117
|
+
this._nodes = [];
|
|
118
|
+
this._stoppedNote?.remove();
|
|
119
|
+
this._stoppedNote = null;
|
|
120
|
+
this._stopped = false;
|
|
121
|
+
if (typeof options.streaming === 'boolean') this._streaming = options.streaming;
|
|
122
|
+
for (const block of Array.isArray(blocks) ? blocks : []) this._push(block);
|
|
123
|
+
this._syncState();
|
|
124
|
+
return this;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Adds a block after the last one. The blocks before it are left as they are.
|
|
129
|
+
* @param {Block} block
|
|
130
|
+
* @returns {this}
|
|
131
|
+
*/
|
|
132
|
+
append(block) {
|
|
133
|
+
this._push(block);
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Streams text into the answer. It continues the last paragraph, or opens one when the answer
|
|
139
|
+
* ends in anything else; a blank line in the text starts a new paragraph. Only the last
|
|
140
|
+
* paragraph is redrawn, so a marker split across two calls (`**bo` … `ld**`) resolves as soon as
|
|
141
|
+
* its close arrives.
|
|
142
|
+
* @param {string} text
|
|
143
|
+
* @returns {this}
|
|
144
|
+
*/
|
|
145
|
+
appendText(text) {
|
|
146
|
+
const chunk = String(text ?? '');
|
|
147
|
+
if (!chunk) return this;
|
|
148
|
+
const last = this._blocks.at(-1);
|
|
149
|
+
if (last?.type !== 'p') {
|
|
150
|
+
this._push({ type: 'p', text: '' });
|
|
151
|
+
}
|
|
152
|
+
const tail = /** @type {{type: 'p', text: string}} */ (this._blocks.at(-1));
|
|
153
|
+
// Split on a blank line only once text follows it: a chunk may end between the two newlines,
|
|
154
|
+
// or right after them, and the paragraph must not open until there is something to put in it.
|
|
155
|
+
const parts = (tail.text + chunk).split(/\n{2,}(?=\S)/);
|
|
156
|
+
tail.text = /** @type {string} */ (parts.shift());
|
|
157
|
+
this._redrawLast();
|
|
158
|
+
for (const part of parts) this._push({ type: 'p', text: part });
|
|
159
|
+
return this;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Marks the answer finished.
|
|
164
|
+
* @returns {this}
|
|
165
|
+
*/
|
|
166
|
+
end() {
|
|
167
|
+
const wasStreaming = this._streaming;
|
|
168
|
+
this._streaming = false;
|
|
169
|
+
this._syncState();
|
|
170
|
+
if (wasStreaming) this.emit('end', {});
|
|
171
|
+
return this;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Marks the answer stopped where it is, with a line saying so.
|
|
176
|
+
* @returns {this}
|
|
177
|
+
*/
|
|
178
|
+
stop() {
|
|
179
|
+
this._streaming = false;
|
|
180
|
+
if (!this._stopped) {
|
|
181
|
+
this._stopped = true;
|
|
182
|
+
this._stoppedNote = h('p', { class: 'zx-block-view__stopped' }, this.msg('blockView.stopped'));
|
|
183
|
+
this.el.append(this._stoppedNote);
|
|
184
|
+
}
|
|
185
|
+
this._syncState();
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Sets the sources citations point at.
|
|
191
|
+
* @param {Source[]} sources
|
|
192
|
+
* @returns {this}
|
|
193
|
+
*/
|
|
194
|
+
setSources(sources) {
|
|
195
|
+
this._sources = Array.isArray(sources) ? [...sources] : [];
|
|
196
|
+
for (const button of this.el.querySelectorAll('[data-cite]')) {
|
|
197
|
+
button.setAttribute('aria-label', this._citeLabel(Number(/** @type {HTMLElement} */ (button).dataset.cite)));
|
|
198
|
+
}
|
|
199
|
+
return this;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Says whether an artifact's card has its artifact open somewhere — an inspector, a drawer — so
|
|
204
|
+
* the card reads as pressed and names what it controls. The application owns the open state;
|
|
205
|
+
* the card only reflects it.
|
|
206
|
+
* @param {string} id Artifact id.
|
|
207
|
+
* @param {{expanded?: boolean|null, controls?: string|null}} state `expanded` sets or (null)
|
|
208
|
+
* removes `aria-expanded`; `controls` sets or (null) removes `aria-controls`.
|
|
209
|
+
* @returns {this}
|
|
210
|
+
*/
|
|
211
|
+
setArtifactState(id, state = {}) {
|
|
212
|
+
for (const card of /** @type {NodeListOf<HTMLElement>} */ (this.el.querySelectorAll('button[data-artifact]'))) {
|
|
213
|
+
if (card.dataset.artifact !== String(id)) continue;
|
|
214
|
+
if ('expanded' in state) {
|
|
215
|
+
if (state.expanded == null) card.removeAttribute('aria-expanded');
|
|
216
|
+
else card.setAttribute('aria-expanded', String(Boolean(state.expanded)));
|
|
217
|
+
}
|
|
218
|
+
if ('controls' in state) {
|
|
219
|
+
if (state.controls) card.setAttribute('aria-controls', String(state.controls));
|
|
220
|
+
else card.removeAttribute('aria-controls');
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
return this;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** @returns {Block[]} A copy of the blocks. */
|
|
227
|
+
getBlocks() {
|
|
228
|
+
return structuredClone(this._blocks);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** @returns {boolean} Whether more is still being written. */
|
|
232
|
+
isStreaming() {
|
|
233
|
+
return this._streaming;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** @returns {string} The answer as plain text. */
|
|
237
|
+
toText() {
|
|
238
|
+
return blocksToText(this._blocks);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/** @returns {string} The answer as Markdown. */
|
|
242
|
+
toMarkdown() {
|
|
243
|
+
return blocksToMarkdown(this._blocks);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** @returns {void} */
|
|
247
|
+
destroy() {
|
|
248
|
+
if (this._destroyed) return;
|
|
249
|
+
this._destroyed = true;
|
|
250
|
+
this._observer?.disconnect();
|
|
251
|
+
this._observer = null;
|
|
252
|
+
super.destroy();
|
|
253
|
+
if (this._original) restoreTarget(this.el, this._original);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* @private
|
|
258
|
+
* @param {Block} block
|
|
259
|
+
* @returns {void}
|
|
260
|
+
*/
|
|
261
|
+
_push(block) {
|
|
262
|
+
if (!block || typeof block !== 'object') return;
|
|
263
|
+
const copy = /** @type {Block} */ (structuredClone(block));
|
|
264
|
+
const node = renderBlock(copy, this._renderOptions());
|
|
265
|
+
this._blocks.push(copy);
|
|
266
|
+
this._nodes.push(node);
|
|
267
|
+
if (this._stoppedNote) this._stoppedNote.before(node);
|
|
268
|
+
else this.el.append(node);
|
|
269
|
+
if (copy.type === 'table') this._syncScrollers();
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** @private @returns {void} */
|
|
273
|
+
_redrawLast() {
|
|
274
|
+
const index = this._blocks.length - 1;
|
|
275
|
+
if (index < 0) return;
|
|
276
|
+
const node = renderBlock(this._blocks[index], this._renderOptions());
|
|
277
|
+
this._nodes[index].replaceWith(node);
|
|
278
|
+
this._nodes[index] = node;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** @private @returns {RenderBlocksOptions & {cite: (n: number) => Node, artifact: boolean}} */
|
|
282
|
+
_renderOptions() {
|
|
283
|
+
return {
|
|
284
|
+
headingLevel: this.options.headingLevel,
|
|
285
|
+
msg: (key, ...args) => this.msg(key, ...args),
|
|
286
|
+
cite: (n) => h('sup', { class: 'zx-block-view__cite' }, h('button', {
|
|
287
|
+
type: 'button', class: 'zx-block-view__cite-button', dataset: { cite: String(n) }, 'aria-label': this._citeLabel(n)
|
|
288
|
+
}, String(n))),
|
|
289
|
+
artifact: true
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** @private @param {number} n @returns {string} */
|
|
294
|
+
_citeLabel(n) {
|
|
295
|
+
const source = this._sourceAt(n);
|
|
296
|
+
return source ? this.msg('blockView.citeTitled', n, source.title) : this.msg('blockView.cite', n);
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/** @private @param {number} n @returns {Source|null} */
|
|
300
|
+
_sourceAt(n) {
|
|
301
|
+
return this._sources[n - 1] ?? null;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** @private @returns {void} */
|
|
305
|
+
_syncState() {
|
|
306
|
+
const root = /** @type {HTMLElement} */ (this.el);
|
|
307
|
+
root.dataset.state = this._stopped ? 'stopped' : this._streaming ? 'streaming' : 'done';
|
|
308
|
+
if (this._streaming) root.setAttribute('aria-busy', 'true');
|
|
309
|
+
else root.removeAttribute('aria-busy');
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* A wide table scrolls inside the answer; while it does, its wrapper is a named, focusable
|
|
314
|
+
* region so the keyboard can scroll it too. A table that fits is not a tab stop.
|
|
315
|
+
* @private
|
|
316
|
+
* @returns {void}
|
|
317
|
+
*/
|
|
318
|
+
_syncScrollers() {
|
|
319
|
+
for (const wrap of /** @type {NodeListOf<HTMLElement>} */ (this.el.querySelectorAll('.zx-block-view__table'))) {
|
|
320
|
+
const overflowing = wrap.scrollWidth > wrap.clientWidth + 1;
|
|
321
|
+
if (overflowing && !wrap.hasAttribute('tabindex')) {
|
|
322
|
+
wrap.tabIndex = 0;
|
|
323
|
+
wrap.setAttribute('role', 'region');
|
|
324
|
+
wrap.setAttribute('aria-label', this.msg('blockView.table'));
|
|
325
|
+
} else if (!overflowing && wrap.hasAttribute('tabindex')) {
|
|
326
|
+
wrap.removeAttribute('tabindex');
|
|
327
|
+
wrap.removeAttribute('role');
|
|
328
|
+
wrap.removeAttribute('aria-label');
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/** @private @param {MouseEvent} event @returns {void} */
|
|
334
|
+
_onClick(event) {
|
|
335
|
+
const target = /** @type {Element|null} */ (event.target instanceof Element ? event.target : null);
|
|
336
|
+
const cite = /** @type {HTMLElement|null} */ (target?.closest('[data-cite]') ?? null);
|
|
337
|
+
if (cite && this.el.contains(cite)) {
|
|
338
|
+
const n = Number(cite.dataset.cite);
|
|
339
|
+
this.emit('cite', { n, source: this._sourceAt(n) });
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
const artifact = /** @type {HTMLElement|null} */ (target?.closest('[data-artifact]') ?? null);
|
|
343
|
+
if (artifact && this.el.contains(artifact)) this.emit('artifact', { id: String(artifact.dataset.artifact) });
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Citation listener payload.
|
|
349
|
+
* @event BlockView#cite
|
|
350
|
+
* @type {CustomEvent<{n: number, source: Source|null}>}
|
|
351
|
+
*/
|
|
352
|
+
/**
|
|
353
|
+
* Artifact card listener payload.
|
|
354
|
+
* @event BlockView#artifact
|
|
355
|
+
* @type {CustomEvent<{id: string}>}
|
|
356
|
+
*/
|
|
357
|
+
/**
|
|
358
|
+
* Fired when a streaming answer is marked finished.
|
|
359
|
+
* @event BlockView#end
|
|
360
|
+
* @type {CustomEvent<Record<string, never>>}
|
|
361
|
+
*/
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Draws blocks without a component, for a static answer: a transcript, a print view, a preview.
|
|
365
|
+
* Citations are plain superscripts and artifacts plain cards; use `BlockView` for them to act.
|
|
366
|
+
* @param {Block[]} blocks
|
|
367
|
+
* @param {RenderBlocksOptions} [options={}]
|
|
368
|
+
* @returns {DocumentFragment}
|
|
369
|
+
*/
|
|
370
|
+
export function renderBlocks(blocks, options = {}) {
|
|
371
|
+
const fragment = document.createDocumentFragment();
|
|
372
|
+
for (const block of Array.isArray(blocks) ? blocks : []) {
|
|
373
|
+
if (block && typeof block === 'object') fragment.append(renderBlock(block, options));
|
|
374
|
+
}
|
|
375
|
+
return fragment;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* @param {string} key
|
|
380
|
+
* @param {...unknown} args
|
|
381
|
+
* @returns {string}
|
|
382
|
+
*/
|
|
383
|
+
function englishMessage(key, ...args) {
|
|
384
|
+
const text = MESSAGES[/** @type {keyof typeof MESSAGES} */ (key)] ?? key;
|
|
385
|
+
return text.replace(/%(\d+)/g, (match, index) => String(args[Number(index) - 1] ?? match));
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Draws a run of model text as text nodes, `<strong>`, `<code>`, and citations — never as HTML.
|
|
390
|
+
* @param {string} text
|
|
391
|
+
* @param {((n: number) => Node)|undefined} cite Draws a citation; a plain superscript without it.
|
|
392
|
+
* @returns {Array<Node|string>}
|
|
393
|
+
*/
|
|
394
|
+
function inlineNodes(text, cite) {
|
|
395
|
+
return parseInline(text).map((segment) => {
|
|
396
|
+
if (segment.kind === 'strong') return h('strong', {}, segment.text);
|
|
397
|
+
if (segment.kind === 'code') return h('code', {}, segment.text);
|
|
398
|
+
if (segment.kind === 'cite') return cite ? cite(segment.n) : h('sup', { class: 'zx-block-view__cite' }, String(segment.n));
|
|
399
|
+
return segment.text;
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* @param {Block} block
|
|
405
|
+
* @param {RenderBlocksOptions & {cite?: (n: number) => Node, artifact?: boolean}} options
|
|
406
|
+
* @returns {HTMLElement}
|
|
407
|
+
*/
|
|
408
|
+
function renderBlock(block, options) {
|
|
409
|
+
const msg = options.msg ?? englishMessage;
|
|
410
|
+
const cite = options.cite;
|
|
411
|
+
const base = Math.min(5, Math.max(2, Number(options.headingLevel) || 3));
|
|
412
|
+
switch (block.type) {
|
|
413
|
+
case 'heading':
|
|
414
|
+
return h(`h${block.level === 3 ? base + 1 : base}`, {}, inlineNodes(block.text, cite));
|
|
415
|
+
case 'quote':
|
|
416
|
+
return h('blockquote', { class: 'zx-block-view__quote' },
|
|
417
|
+
String(block.text ?? '').split(/\n{2,}/).map((paragraph) => h('p', {}, inlineNodes(paragraph, cite))));
|
|
418
|
+
case 'list':
|
|
419
|
+
return h(block.ordered ? 'ol' : 'ul', {},
|
|
420
|
+
(Array.isArray(block.items) ? block.items : []).map((item) => h('li', {}, inlineNodes(item, cite))));
|
|
421
|
+
case 'code':
|
|
422
|
+
return renderCode(block, msg);
|
|
423
|
+
case 'table':
|
|
424
|
+
return renderTable(block, cite);
|
|
425
|
+
case 'figures':
|
|
426
|
+
return h('dl', { class: 'zx-block-view__figures' }, (Array.isArray(block.rows) ? block.rows : []).map((entry) => {
|
|
427
|
+
const row = figureRow(entry);
|
|
428
|
+
return h('div', { class: 'zx-block-view__figure' },
|
|
429
|
+
h('dt', { class: 'zx-block-view__figure-label' }, row.label),
|
|
430
|
+
row.detail ? h('dd', { class: 'zx-block-view__figure-detail' }, row.detail) : null,
|
|
431
|
+
h('dd', { class: 'zx-block-view__figure-value' }, row.value));
|
|
432
|
+
}));
|
|
433
|
+
case 'callout': {
|
|
434
|
+
const tone = Object.hasOwn(CALLOUT_ICONS, String(block.tone)) ? String(block.tone) : null;
|
|
435
|
+
return h('div', { class: 'zx-callout zx-block-view__callout', dataset: { kind: tone } },
|
|
436
|
+
tone ? h('span', { class: 'zx-callout__icon', 'aria-hidden': 'true' },
|
|
437
|
+
icon(CALLOUT_ICONS[/** @type {keyof typeof CALLOUT_ICONS} */ (tone)], { size: 16 })) : null,
|
|
438
|
+
h('div', { class: 'zx-callout__body' },
|
|
439
|
+
block.title ? h('p', { class: 'zx-callout__title' }, block.title) : null,
|
|
440
|
+
h('p', {}, inlineNodes(block.text, cite))));
|
|
441
|
+
}
|
|
442
|
+
case 'artifact':
|
|
443
|
+
return renderArtifact(block, msg, Boolean(options.artifact));
|
|
444
|
+
default:
|
|
445
|
+
return h('p', {}, inlineNodes(/** @type {{text?: string}} */ (block).text ?? '', cite));
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* @param {Extract<Block, {type: 'code'}>} block
|
|
451
|
+
* @param {(key: string, ...args: unknown[]) => string} msg
|
|
452
|
+
* @returns {HTMLElement}
|
|
453
|
+
*/
|
|
454
|
+
function renderCode(block, msg) {
|
|
455
|
+
const language = typeof block.language === 'string' ? block.language : '';
|
|
456
|
+
return h('figure', { class: 'zx-block-view__code' },
|
|
457
|
+
h('figcaption', { class: 'zx-block-view__code-head' },
|
|
458
|
+
h('span', { class: 'zx-block-view__code-language' }, language || msg('blockView.code')),
|
|
459
|
+
copyButton({ text: String(block.code ?? ''), title: msg('blockView.copyCode'), size: 'sm' })),
|
|
460
|
+
block.lines
|
|
461
|
+
? h('pre', { class: 'zx-block-view__pre', dataset: { lines: '' }, tabindex: '0' }, h('code', {},
|
|
462
|
+
String(block.code ?? '').split('\n').map((line) => h('span', {
|
|
463
|
+
class: 'zx-block-view__line', style: { '--zx-block-view-indent': `${/^\s*/.exec(line)?.[0].length ?? 0}ch` }
|
|
464
|
+
}, codeSegments(language, line)))))
|
|
465
|
+
: h('pre', { class: 'zx-block-view__pre', tabindex: '0' }, h('code', {}, codeSegments(language, block.code))));
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Coloured code as text nodes and spans, never HTML.
|
|
470
|
+
* @param {string} language
|
|
471
|
+
* @param {string} code
|
|
472
|
+
* @returns {Array<Node|string>}
|
|
473
|
+
*/
|
|
474
|
+
function codeSegments(language, code) {
|
|
475
|
+
return highlight(language, code).map((segment) => (segment.token
|
|
476
|
+
? h('span', { class: 'zx-block-view__token', dataset: { token: segment.token } }, segment.text)
|
|
477
|
+
: segment.text));
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* @param {Extract<Block, {type: 'table'}>} block
|
|
482
|
+
* @param {((n: number) => Node)|undefined} cite
|
|
483
|
+
* @returns {HTMLElement}
|
|
484
|
+
*/
|
|
485
|
+
function renderTable(block, cite) {
|
|
486
|
+
const columns = Array.isArray(block.columns) ? block.columns : [];
|
|
487
|
+
const rows = Array.isArray(block.rows) ? block.rows : [];
|
|
488
|
+
const align = (/** @type {number} */ index) => {
|
|
489
|
+
const value = block.align?.[index];
|
|
490
|
+
return value === 'end' || value === 'center' ? value : null;
|
|
491
|
+
};
|
|
492
|
+
return h('div', { class: 'zx-block-view__table' }, h('table', {},
|
|
493
|
+
h('thead', {}, h('tr', {}, columns.map((column, index) => h('th', { scope: 'col', dataset: { align: align(index) } },
|
|
494
|
+
inlineNodes(column, cite))))),
|
|
495
|
+
h('tbody', {}, rows.map((row) => h('tr', {}, (Array.isArray(row) ? row : []).map((cell, index) => h('td', {
|
|
496
|
+
dataset: { align: align(index) }
|
|
497
|
+
}, inlineNodes(cell, cite))))))));
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
/**
|
|
501
|
+
* @param {Extract<Block, {type: 'artifact'}>} block
|
|
502
|
+
* @param {(key: string, ...args: unknown[]) => string} msg
|
|
503
|
+
* @param {boolean} interactive
|
|
504
|
+
* @returns {HTMLElement}
|
|
505
|
+
*/
|
|
506
|
+
function renderArtifact(block, msg, interactive) {
|
|
507
|
+
const kind = block.kind === 'code' ? 'code' : 'document';
|
|
508
|
+
const versions = Number(block.versions) || 0;
|
|
509
|
+
const meta = [msg(kind === 'code' ? 'blockView.code' : 'blockView.document'),
|
|
510
|
+
versions > 1 ? msg('blockView.versions', versions) : versions === 1 ? msg('blockView.version') : null].filter(Boolean).join(' · ');
|
|
511
|
+
const content = [
|
|
512
|
+
h('span', { class: 'zx-block-view__artifact-tile', dataset: { kind }, 'aria-hidden': 'true' },
|
|
513
|
+
icon(kind === 'code' ? 'code' : 'file', { size: 16 })),
|
|
514
|
+
h('span', { class: 'zx-block-view__artifact-copy' },
|
|
515
|
+
h('span', { class: 'zx-block-view__artifact-name' }, String(block.title ?? '')),
|
|
516
|
+
h('span', { class: 'zx-block-view__artifact-meta' }, meta)),
|
|
517
|
+
interactive ? h('span', { class: 'zx-block-view__artifact-chevron', 'aria-hidden': 'true' }, icon('chevron-right', { size: 14 })) : null
|
|
518
|
+
];
|
|
519
|
+
return interactive
|
|
520
|
+
? h('p', { class: 'zx-block-view__artifact-row' },
|
|
521
|
+
h('button', { type: 'button', class: 'zx-block-view__artifact', dataset: { artifact: String(block.id ?? '') } }, content))
|
|
522
|
+
: h('p', { class: 'zx-block-view__artifact-row' }, h('span', { class: 'zx-block-view__artifact' }, content));
|
|
523
|
+
}
|